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(); 36 int program() { return m_program; } 37 38 // Drawing 39 void setViewport(SkRect& viewport, float scale); 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 convertScreenCoordToDocumentCoord(const FloatRect& rect); 71 FloatRect convertInvScreenCoordToScreenCoord(const FloatRect& rect); 72 FloatRect convertScreenCoordToInvScreenCoord(const FloatRect& rect); 73 74 void setTitleBarHeight(int height) { m_titleBarHeight = height; } 75 void setWebViewRect(const IntRect& rect) { m_webViewRect = rect; } 76 void setScreenClip(const IntRect& clip); 77 void clip(const FloatRect& rect); 78 IntRect clippedRectWithViewport(const IntRect& rect, int margin = 0); 79 FloatRect documentViewport() { return m_documentViewport; } 80 81 void resetBlending(); 82 float contrast() { return m_contrast; } 83 void setContrast(float c) { 84 float contrast = c; 85 if (contrast < 0) 86 contrast = 0; 87 if (contrast > MAX_CONTRAST) 88 contrast = MAX_CONTRAST; 89 m_contrast = contrast; 90 } 91 void setWebViewMatrix(const float* matrix, bool alphaLayer); 92 93 // This delta is the delta from the layout pos and the current animation pos. 94 // Basically, in terms of layout, the webview is still in the original layout 95 // pos, as without animation. Such that the viewport and visible rect etc are 96 // still in that pos, too, except the clipping info. 97 // Our rendering approach is after applying all the matrix, webView is 98 // rendered as if it was at the original layout pos, but then offset the 99 // glViewport to match the animation. 100 void calculateAnimationDelta(); 101 int getAnimationDeltaX() { return m_animationDelta.x(); } 102 int getAnimationDeltaY() { return m_animationDelta.y(); } 103 104 private: 105 GLuint loadShader(GLenum shaderType, const char* pSource); 106 GLuint createProgram(const char* vertexSource, const char* fragmentSource); 107 void setProjectionMatrix(SkRect& geometry, GLint projectionMatrixHandle); 108 109 void setBlendingState(bool enableBlending); 110 111 void drawQuadInternal(SkRect& geometry, GLint textureId, float opacity, 112 GLint program, GLint projectionMatrixHandle, 113 GLint texSampler, GLenum textureTarget, 114 GLint position, GLint alpha, 115 GLint texFilter, GLint contrast = -1); 116 117 void drawLayerQuadInternal(const GLfloat* projectionMatrix, int textureId, 118 float opacity, GLenum textureTarget, GLint program, 119 GLint matrix, GLint texSample, 120 GLint position, GLint alpha, GLint contrast = -1); 121 122 bool m_blendingEnabled; 123 124 int m_program; 125 int m_programInverted; 126 int m_videoProgram; 127 int m_surfTexOESProgram; 128 int m_surfTexOESProgramInverted; 129 130 TransformationMatrix m_projectionMatrix; 131 GLuint m_textureBuffer[1]; 132 133 TransformationMatrix m_documentToScreenMatrix; 134 TransformationMatrix m_documentToInvScreenMatrix; 135 SkRect m_viewport; 136 IntRect m_viewRect; 137 FloatRect m_clipRect; 138 IntRect m_screenClip; 139 int m_titleBarHeight; 140 IntRect m_webViewRect; 141 142 FloatRect m_documentViewport; 143 144 // uniforms 145 GLint m_hProjectionMatrix; 146 GLint m_hAlpha; 147 GLint m_hTexSampler; 148 GLint m_hProjectionMatrixInverted; 149 GLint m_hAlphaInverted; 150 GLint m_hContrastInverted; 151 GLint m_hTexSamplerInverted; 152 GLint m_hVideoProjectionMatrix; 153 GLint m_hVideoTextureMatrix; 154 GLint m_hVideoTexSampler; 155 156 GLint m_hSTOESProjectionMatrix; 157 GLint m_hSTOESAlpha; 158 GLint m_hSTOESTexSampler; 159 GLint m_hSTOESPosition; 160 161 GLint m_hSTOESProjectionMatrixInverted; 162 GLint m_hSTOESAlphaInverted; 163 GLint m_hSTOESContrastInverted; 164 GLint m_hSTOESTexSamplerInverted; 165 GLint m_hSTOESPositionInverted; 166 167 float m_contrast; 168 169 // attribs 170 GLint m_hPosition; 171 GLint m_hPositionInverted; 172 GLint m_hVideoPosition; 173 174 bool m_alphaLayer; 175 TransformationMatrix m_webViewMatrix; 176 float m_currentScale; 177 178 // After the webViewTranform, we need to reposition the rect to match our viewport. 179 // Basically, the webViewTransformMatrix should apply on the screen resolution. 180 // So we start by doing the scale and translate to get each tile into screen coordinates. 181 // After applying the webViewTransformMatrix, b/c the way it currently set up 182 // for scroll and titlebar, we need to offset both of them. 183 // Finally, map everything back to (-1, 1) by using the m_projectionMatrix. 184 // TODO: Given that m_webViewMatrix contains most of the tranformation 185 // information, we should be able to get rid of some parameter we got from 186 // Java side and simplify our code. 187 TransformationMatrix m_repositionMatrix; 188 IntPoint m_animationDelta; 189 }; 190 191 } // namespace WebCore 192 193 #endif // USE(ACCELERATED_COMPOSITING) 194 #endif // ShaderProgram_h 195