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 #include "platform/graphics/filters/custom/CustomFilterGlobalContext.h" 32 33 #include "platform/graphics/GraphicsContext3D.h" 34 #include "platform/graphics/filters/custom/CustomFilterValidatedProgram.h" 35 36 namespace WebCore { 37 38 CustomFilterGlobalContext::CustomFilterGlobalContext() 39 { 40 } 41 42 CustomFilterGlobalContext::~CustomFilterGlobalContext() 43 { 44 for (CustomFilterValidatedProgramsMap::iterator iter = m_programs.begin(); iter != m_programs.end(); ++iter) 45 iter->value->detachFromGlobalContext(); 46 } 47 48 ANGLEPlatformBridge* CustomFilterGlobalContext::webglShaderValidator() 49 { 50 if (!m_webglShaderValidator) 51 m_webglShaderValidator = createShaderValidator(SH_WEBGL_SPEC); 52 return m_webglShaderValidator.get(); 53 } 54 55 ANGLEPlatformBridge* CustomFilterGlobalContext::mixShaderValidator() 56 { 57 if (!m_mixShaderValidator) 58 m_mixShaderValidator = createShaderValidator(SH_CSS_SHADERS_SPEC); 59 return m_mixShaderValidator.get(); 60 } 61 62 PassOwnPtr<ANGLEPlatformBridge> CustomFilterGlobalContext::createShaderValidator(ShShaderSpec shaderSpec) 63 { 64 OwnPtr<ANGLEPlatformBridge> validator = adoptPtr(new ANGLEPlatformBridge(SH_ESSL_OUTPUT, shaderSpec)); 65 ShBuiltInResources resources; 66 ShInitBuiltInResources(&resources); 67 validator->setResources(resources); 68 return validator.release(); 69 } 70 71 void CustomFilterGlobalContext::prepareContextIfNeeded() 72 { 73 if (m_context.get()) 74 return; 75 76 GraphicsContext3D::Attributes attributes; 77 attributes.preserveDrawingBuffer = true; 78 attributes.premultipliedAlpha = false; 79 attributes.shareResources = true; 80 attributes.preferDiscreteGPU = true; 81 m_context = GraphicsContext3D::create(attributes); 82 if (!m_context) 83 return; 84 m_context->makeContextCurrent(); 85 m_context->enable(GL_DEPTH_TEST); 86 } 87 88 PassRefPtr<CustomFilterValidatedProgram> CustomFilterGlobalContext::getValidatedProgram(const CustomFilterProgramInfo& programInfo) 89 { 90 CustomFilterValidatedProgramsMap::iterator iter = m_programs.find(programInfo); 91 if (iter != m_programs.end()) 92 return iter->value; 93 94 RefPtr<CustomFilterValidatedProgram> validatedProgram = CustomFilterValidatedProgram::create(this, programInfo); 95 m_programs.set(programInfo, validatedProgram.get()); 96 return validatedProgram.release(); 97 } 98 99 void CustomFilterGlobalContext::removeValidatedProgram(const CustomFilterValidatedProgram* program) 100 { 101 CustomFilterValidatedProgramsMap::iterator iter = m_programs.find(program->programInfo()); 102 ASSERT_WITH_SECURITY_IMPLICATION(iter != m_programs.end()); 103 m_programs.remove(iter); 104 105 #ifndef NDEBUG 106 // Check that there's no way we could have the same program under a different key. 107 for (iter = m_programs.begin(); iter != m_programs.end(); ++iter) 108 ASSERT(iter->value != program); 109 #endif 110 } 111 112 } // namespace WebCore 113