1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef ShaderProgram_h 18 #define ShaderProgram_h 19 20 #if USE(ACCELERATED_COMPOSITING) 21 22 #include "FloatRect.h" 23 #include "IntRect.h" 24 #include "SkRect.h" 25 #include "TransformationMatrix.h" 26 #include <GLES2/gl2.h> 27 28 #define MAX_CONTRAST 5 29 30 namespace WebCore { 31 32 class ShaderProgram { 33 public: 34 ShaderProgram(); 35 void init(); program()36 int program() { return m_program; } 37 38 // Drawing 39 void setViewport(SkRect& viewport); 40 float zValue(const TransformationMatrix& drawMatrix, float w, float h); 41 42 // For drawQuad and drawLayerQuad, they can handle 3 cases for now: 43 // 1) textureTarget == GL_TEXTURE_2D 44 // Normal texture in GL_TEXTURE_2D target. 45 // 2) textureTarget == GL_TEXTURE_EXTERNAL_OES 46 // Surface texture in GL_TEXTURE_EXTERNAL_OES target. 47 // 3) textureTarget == 0 (Will be deprecated soon) 48 // Surface texture in GL_TEXTURE_2D target. 49 // 50 // TODO: Shrink the support modes into 2 (1 and 2) after media framework 51 // support Surface texture in GL_TEXTURE_EXTERNAL_OES target on all 52 // platforms. 53 void drawQuad(SkRect& geometry, int textureId, float opacity, 54 GLenum textureTarget = GL_TEXTURE_2D, 55 GLint texFilter = GL_LINEAR); 56 void drawLayerQuad(const TransformationMatrix& drawMatrix, 57 const SkRect& geometry, int textureId, float opacity, 58 bool forceBlending = false, 59 GLenum textureTarget = GL_TEXTURE_2D); 60 void drawVideoLayerQuad(const TransformationMatrix& drawMatrix, 61 float* textureMatrix, SkRect& geometry, int textureId); 62 void setViewRect(const IntRect& viewRect); 63 FloatRect rectInScreenCoord(const TransformationMatrix& drawMatrix, 64 const IntSize& size); 65 FloatRect rectInInvScreenCoord(const TransformationMatrix& drawMatrix, 66 const IntSize& size); 67 68 FloatRect rectInInvScreenCoord(const FloatRect& rect); 69 FloatRect rectInScreenCoord(const FloatRect& rect); 70 FloatRect convertInvScreenCoordToScreenCoord(const FloatRect& rect); 71 FloatRect convertScreenCoordToInvScreenCoord(const FloatRect& rect); 72 setTitleBarHeight(int height)73 void setTitleBarHeight(int height) { m_titleBarHeight = height; } setWebViewRect(const IntRect & rect)74 void setWebViewRect(const IntRect& rect) { m_webViewRect = rect; } 75 void setScreenClip(const IntRect& clip); 76 void clip(const FloatRect& rect); 77 IntRect clippedRectWithViewport(const IntRect& rect, int margin = 0); 78 79 void resetBlending(); contrast()80 float contrast() { return m_contrast; } setContrast(float c)81 void setContrast(float c) { 82 float contrast = c; 83 if (contrast < 0) 84 contrast = 0; 85 if (contrast > MAX_CONTRAST) 86 contrast = MAX_CONTRAST; 87 m_contrast = contrast; 88 } 89 90 private: 91 GLuint loadShader(GLenum shaderType, const char* pSource); 92 GLuint createProgram(const char* vertexSource, const char* fragmentSource); 93 void setProjectionMatrix(SkRect& geometry, GLint projectionMatrixHandle); 94 95 void setBlendingState(bool enableBlending); 96 97 void drawQuadInternal(SkRect& geometry, GLint textureId, float opacity, 98 GLint program, GLint projectionMatrixHandle, 99 GLint texSampler, GLenum textureTarget, 100 GLint position, GLint alpha, 101 GLint texFilter, GLint contrast = -1); 102 103 void drawLayerQuadInternal(const GLfloat* projectionMatrix, int textureId, 104 float opacity, GLenum textureTarget, GLint program, 105 GLint matrix, GLint texSample, 106 GLint position, GLint alpha, GLint contrast = -1); 107 108 bool m_blendingEnabled; 109 110 int m_program; 111 int m_programInverted; 112 int m_videoProgram; 113 int m_surfTexOESProgram; 114 int m_surfTexOESProgramInverted; 115 116 TransformationMatrix m_projectionMatrix; 117 GLuint m_textureBuffer[1]; 118 119 TransformationMatrix m_documentToScreenMatrix; 120 TransformationMatrix m_documentToInvScreenMatrix; 121 SkRect m_viewport; 122 IntRect m_viewRect; 123 FloatRect m_clipRect; 124 IntRect m_screenClip; 125 int m_titleBarHeight; 126 IntRect m_webViewRect; 127 128 // uniforms 129 GLint m_hProjectionMatrix; 130 GLint m_hAlpha; 131 GLint m_hTexSampler; 132 GLint m_hProjectionMatrixInverted; 133 GLint m_hAlphaInverted; 134 GLint m_hContrastInverted; 135 GLint m_hTexSamplerInverted; 136 GLint m_hVideoProjectionMatrix; 137 GLint m_hVideoTextureMatrix; 138 GLint m_hVideoTexSampler; 139 140 GLint m_hSTOESProjectionMatrix; 141 GLint m_hSTOESAlpha; 142 GLint m_hSTOESTexSampler; 143 GLint m_hSTOESPosition; 144 145 GLint m_hSTOESProjectionMatrixInverted; 146 GLint m_hSTOESAlphaInverted; 147 GLint m_hSTOESContrastInverted; 148 GLint m_hSTOESTexSamplerInverted; 149 GLint m_hSTOESPositionInverted; 150 151 float m_contrast; 152 153 // attribs 154 GLint m_hPosition; 155 GLint m_hPositionInverted; 156 GLint m_hVideoPosition; 157 }; 158 159 } // namespace WebCore 160 161 #endif // USE(ACCELERATED_COMPOSITING) 162 #endif // ShaderProgram_h 163