Home | History | Annotate | Download | only in custom
      1 /*
      2  * Copyright (C) 2012 Adobe Systems Incorporated. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  *
      8  * 1. Redistributions of source code must retain the above
      9  *    copyright notice, this list of conditions and the following
     10  *    disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above
     12  *    copyright notice, this list of conditions and the following
     13  *    disclaimer in the documentation and/or other materials
     14  *    provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AS IS AND ANY
     17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
     20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
     21  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
     25  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
     26  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  */
     29 
     30 #include "config.h"
     31 
     32 #include "core/platform/graphics/filters/custom/CustomFilterGlobalContext.h"
     33 
     34 #include "core/platform/graphics/GraphicsContext3D.h"
     35 #include "core/platform/graphics/filters/custom/CustomFilterValidatedProgram.h"
     36 
     37 namespace WebCore {
     38 
     39 CustomFilterGlobalContext::CustomFilterGlobalContext()
     40 {
     41 }
     42 
     43 CustomFilterGlobalContext::~CustomFilterGlobalContext()
     44 {
     45     for (CustomFilterValidatedProgramsMap::iterator iter = m_programs.begin(); iter != m_programs.end(); ++iter)
     46         iter->value->detachFromGlobalContext();
     47 }
     48 
     49 ANGLEWebKitBridge* CustomFilterGlobalContext::webglShaderValidator()
     50 {
     51     if (!m_webglShaderValidator)
     52         m_webglShaderValidator = createShaderValidator(SH_WEBGL_SPEC);
     53     return m_webglShaderValidator.get();
     54 }
     55 
     56 ANGLEWebKitBridge* CustomFilterGlobalContext::mixShaderValidator()
     57 {
     58     if (!m_mixShaderValidator)
     59         m_mixShaderValidator = createShaderValidator(SH_CSS_SHADERS_SPEC);
     60     return m_mixShaderValidator.get();
     61 }
     62 
     63 PassOwnPtr<ANGLEWebKitBridge> CustomFilterGlobalContext::createShaderValidator(ShShaderSpec shaderSpec)
     64 {
     65     OwnPtr<ANGLEWebKitBridge> validator = adoptPtr(new ANGLEWebKitBridge(SH_ESSL_OUTPUT, shaderSpec));
     66     ShBuiltInResources resources;
     67     ShInitBuiltInResources(&resources);
     68     validator->setResources(resources);
     69     return validator.release();
     70 }
     71 
     72 void CustomFilterGlobalContext::prepareContextIfNeeded()
     73 {
     74     if (m_context.get())
     75         return;
     76 
     77     GraphicsContext3D::Attributes attributes;
     78     attributes.preserveDrawingBuffer = true;
     79     attributes.premultipliedAlpha = false;
     80     attributes.shareResources = true;
     81     attributes.preferDiscreteGPU = true;
     82     m_context = GraphicsContext3D::create(attributes);
     83     if (!m_context)
     84         return;
     85     m_context->makeContextCurrent();
     86     m_context->enable(GraphicsContext3D::DEPTH_TEST);
     87 }
     88 
     89 PassRefPtr<CustomFilterValidatedProgram> CustomFilterGlobalContext::getValidatedProgram(const CustomFilterProgramInfo& programInfo)
     90 {
     91     CustomFilterValidatedProgramsMap::iterator iter = m_programs.find(programInfo);
     92     if (iter != m_programs.end())
     93         return iter->value;
     94 
     95     RefPtr<CustomFilterValidatedProgram> validatedProgram = CustomFilterValidatedProgram::create(this, programInfo);
     96     m_programs.set(programInfo, validatedProgram.get());
     97     return validatedProgram.release();
     98 }
     99 
    100 void CustomFilterGlobalContext::removeValidatedProgram(const CustomFilterValidatedProgram* program)
    101 {
    102     CustomFilterValidatedProgramsMap::iterator iter = m_programs.find(program->programInfo());
    103     ASSERT(iter != m_programs.end());
    104     m_programs.remove(iter);
    105 
    106 #ifndef NDEBUG
    107     // Check that there's no way we could have the same program under a different key.
    108     for (iter = m_programs.begin(); iter != m_programs.end(); ++iter)
    109         ASSERT(iter->value != program);
    110 #endif
    111 }
    112 
    113 } // namespace WebCore
    114