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 #ifndef CustomFilterValidatedProgram_h 31 #define CustomFilterValidatedProgram_h 32 33 #include "platform/graphics/filters/custom/CustomFilterCompiledProgram.h" 34 #include "platform/graphics/filters/custom/CustomFilterProgramInfo.h" 35 #include "wtf/PassRefPtr.h" 36 #include "wtf/RefCounted.h" 37 #include "wtf/RefPtr.h" 38 #include "wtf/text/WTFString.h" 39 40 // PlatformCompiledProgram defines a type that is compatible with the framework used to implement accelerated compositing on a particular platform. 41 namespace WebCore { 42 43 struct ANGLEShaderSymbol; 44 class CustomFilterCompiledProgram; 45 class CustomFilterGlobalContext; 46 47 // 48 // A unique combination of vertex shader and fragment shader is only validated and compiled once. 49 // All shaders are validated through ANGLE in CustomFilterValidatedProgram before being compiled by the GraphicsContext3D in CustomFilterCompiledProgram. 50 // For shaders that use the CSS mix function, CustomFilterValidatedProgram adds shader code to perform DOM texture access, blending, and compositing. 51 // 52 // The CustomFilterGlobalContext caches the validated programs. 53 // CustomFilterValidatedProgram owns a CustomFilterCompiledProgram if validation and compilation succeeds. 54 // Thus, compiled programs are cached via their validated program owners. 55 // 56 // CustomFilterGlobalContext has a weak reference to the CustomFilterValidatedProgram. 57 // Thus, the CustomFilterValidatedProgram destructor needs to notify the CustomFilterGlobalContext to remove the program from the cache. 58 // FECustomFilter is the reference owner of the CustomFilterValidatedProgram. 59 // Thus, a validated and compiled shader is only kept alive as long as there is at least one visible layer that applies the shader. 60 // 61 class PLATFORM_EXPORT CustomFilterValidatedProgram : public RefCounted<CustomFilterValidatedProgram> { 62 public: create(CustomFilterGlobalContext * globalContext,const CustomFilterProgramInfo & programInfo)63 static PassRefPtr<CustomFilterValidatedProgram> create(CustomFilterGlobalContext* globalContext, const CustomFilterProgramInfo& programInfo) 64 { 65 return adoptRef(new CustomFilterValidatedProgram(globalContext, programInfo)); 66 } 67 68 ~CustomFilterValidatedProgram(); 69 programInfo()70 const CustomFilterProgramInfo& programInfo() const { return m_programInfo; } 71 CustomFilterProgramInfo validatedProgramInfo() const; 72 73 PassRefPtr<CustomFilterCompiledProgram> compiledProgram(); 74 validatedVertexShader()75 const String& validatedVertexShader() const 76 { 77 ASSERT(m_isInitialized); 78 return m_validatedVertexShader; 79 } 80 validatedFragmentShader()81 const String& validatedFragmentShader() const 82 { 83 ASSERT(m_isInitialized); 84 return m_validatedFragmentShader; 85 } 86 isInitialized()87 bool isInitialized() const { return m_isInitialized; } 88 89 // 'detachFromGlobalContext' is called when the CustomFilterGlobalContext is deleted, and there's no need for the callback anymore. 90 // Note that CustomFilterGlobalContext does not keep a strong reference to the CustomFilterValidatedProgram. detachFromGlobalContext()91 void detachFromGlobalContext() { m_globalContext = 0; } 92 private: 93 CustomFilterValidatedProgram(CustomFilterGlobalContext*, const CustomFilterProgramInfo&); 94 95 void platformInit(); 96 void platformDestroy(); 97 98 static String defaultVertexShaderString(); 99 static String defaultFragmentShaderString(); 100 101 static String blendFunctionString(blink::WebBlendMode); 102 static String compositeFunctionString(CompositeOperator); 103 104 void rewriteMixVertexShader(const Vector<ANGLEShaderSymbol>& symbols); 105 void rewriteMixFragmentShader(); 106 107 bool needsInputTexture() const; 108 109 CustomFilterGlobalContext* m_globalContext; 110 CustomFilterProgramInfo m_programInfo; 111 112 String m_validatedVertexShader; 113 String m_validatedFragmentShader; 114 115 RefPtr<CustomFilterCompiledProgram> m_compiledProgram; 116 117 bool m_isInitialized; 118 }; 119 120 } 121 122 123 #endif 124