ShaderProgram.h revision 485445e9a624fe92ca04a1d0e92c1f71aaf8cd8f
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 "Color.h"
23#include "FloatRect.h"
24#include "IntRect.h"
25#include "SkRect.h"
26#include "TransformationMatrix.h"
27#include "private/hwui/DrawGlInfo.h"
28#include <GLES2/gl2.h>
29
30#define MAX_CONTRAST 5
31#define DEBUG_MATRIX 0
32
33namespace WebCore {
34
35class DrawQuadData;
36class PureColorQuadData;
37class TextureQuadData;
38
39enum ShaderType {
40    UndefinedShader = -1,
41    PureColor,
42    Tex2D,
43    Tex2DInv,
44    TexOES,
45    TexOESInv,
46    Video,
47    // When growing this enum list, make sure to insert before the
48    // MaxShaderNumber and init the m_handleArray accordingly.
49    MaxShaderNumber
50};
51
52struct ShaderHandles {
53    ShaderHandles()
54        : alphaHandle(-1)
55        , contrastHandle(-1)
56        , positionHandle(-1)
57        , programHandle(-1)
58        , projMtxHandle(-1)
59        , pureColorHandle(-1)
60        , texSamplerHandle(-1)
61        , videoMtxHandle(-1)
62        , fillPortionHandle(-1)
63    {
64    }
65
66    void init(GLint alphaHdl, GLint contrastHdl, GLint posHdl, GLint pgmHdl,
67              GLint projMtxHdl, GLint colorHdl, GLint texSamplerHdl,
68              GLint videoMtxHdl, GLint fillPortionHdl)
69    {
70        alphaHandle = alphaHdl;
71        contrastHandle = contrastHdl;
72        positionHandle = posHdl;
73        programHandle = pgmHdl;
74        projMtxHandle = projMtxHdl;
75        pureColorHandle = colorHdl;
76        texSamplerHandle = texSamplerHdl;
77        videoMtxHandle = videoMtxHdl;
78        fillPortionHandle = fillPortionHdl;
79    }
80
81    GLint alphaHandle;
82    GLint contrastHandle;
83    GLint positionHandle;
84    GLint programHandle;
85    GLint projMtxHandle;
86    GLint pureColorHandle;
87    GLint texSamplerHandle;
88    GLint videoMtxHandle;
89    GLint fillPortionHandle;
90};
91
92struct ShaderResource {
93    ShaderResource()
94        : program(-1)
95        , vertexShader(-1)
96        , fragmentShader(-1)
97    {
98    };
99
100    ShaderResource(GLuint prog, GLuint vertex, GLuint fragment)
101        : program(prog)
102        , vertexShader(vertex)
103        , fragmentShader(fragment)
104    {
105    };
106
107    GLuint program;
108    GLuint vertexShader;
109    GLuint fragmentShader;
110};
111
112class ShaderProgram {
113public:
114    ShaderProgram();
115    void initGLResources();
116    void cleanupGLResources();
117    // Drawing
118    void setupDrawing(const IntRect& viewRect, const SkRect& visibleRect,
119                      const IntRect& webViewRect, int titleBarHeight,
120                      const IntRect& screenClip, float scale);
121    float zValue(const TransformationMatrix& drawMatrix, float w, float h);
122
123    // For drawQuad and drawLayerQuad, they can handle 3 cases for now:
124    // 1) textureTarget == GL_TEXTURE_2D
125    // Normal texture in GL_TEXTURE_2D target.
126    // 2) textureTarget == GL_TEXTURE_EXTERNAL_OES
127    // Surface texture in GL_TEXTURE_EXTERNAL_OES target.
128    // 3) textureId == 0
129    // No texture needed, just a pureColor quad.
130    void drawQuad(const DrawQuadData* data);
131    void drawVideoLayerQuad(const TransformationMatrix& drawMatrix,
132                     float* textureMatrix, SkRect& geometry, int textureId);
133    FloatRect rectInScreenCoord(const TransformationMatrix& drawMatrix,
134                                const IntSize& size);
135    FloatRect rectInInvScreenCoord(const TransformationMatrix& drawMatrix,
136                                const IntSize& size);
137
138    FloatRect rectInInvScreenCoord(const FloatRect& rect);
139    FloatRect rectInScreenCoord(const FloatRect& rect);
140    FloatRect convertScreenCoordToDocumentCoord(const FloatRect& rect);
141    FloatRect convertInvScreenCoordToScreenCoord(const FloatRect& rect);
142    FloatRect convertScreenCoordToInvScreenCoord(const FloatRect& rect);
143
144    void clip(const FloatRect& rect);
145    IntRect clippedRectWithViewport(const IntRect& rect, int margin = 0);
146    FloatRect documentViewport() { return m_documentViewport; }
147
148    float contrast() { return m_contrast; }
149    void setContrast(float c)
150    {
151        float contrast = c;
152        if (contrast < 0)
153            contrast = 0;
154        if (contrast > MAX_CONTRAST)
155            contrast = MAX_CONTRAST;
156        m_contrast = contrast;
157    }
158    void setGLDrawInfo(const android::uirenderer::DrawGlInfo* info);
159    void forceNeedsInit() { m_needsInit = true; }
160    bool needsInit() { return m_needsInit; }
161    bool usePointSampling(float tileScale, const TransformationMatrix* layerTransform);
162
163private:
164    GLuint loadShader(GLenum shaderType, const char* pSource);
165    GLint createProgram(const char* vertexSource, const char* fragmentSource);
166    GLfloat* getTileProjectionMatrix(const DrawQuadData* data);
167    void setBlendingState(bool enableBlending);
168    void drawQuadInternal(ShaderType type, const GLfloat* matrix, int textureId,
169                         float opacity, GLenum textureTarget, GLenum filter,
170                         const Color& pureColor,  const FloatRect& fillPortion);
171    Color shaderColor(Color pureColor, float opacity);
172    ShaderType getTextureShaderType(GLenum textureTarget);
173    void resetBlending();
174    void setupSurfaceProjectionMatrix();
175#if DEBUG_MATRIX
176    FloatRect debugMatrixTransform(const TransformationMatrix& matrix, const char* matrixName);
177    void debugMatrixInfo(float currentScale,
178                         const TransformationMatrix& clipProjectionMatrix,
179                         const TransformationMatrix& webViewMatrix,
180                         const TransformationMatrix& modifiedDrawMatrix,
181                         const TransformationMatrix* layerMatrix);
182#endif // DEBUG_MATRIX
183
184    bool m_blendingEnabled;
185
186    TransformationMatrix m_surfaceProjectionMatrix;
187    TransformationMatrix m_clipProjectionMatrix;
188    TransformationMatrix m_visibleRectProjectionMatrix;
189    GLuint m_textureBuffer[1];
190
191    TransformationMatrix m_documentToScreenMatrix;
192    TransformationMatrix m_documentToInvScreenMatrix;
193    SkRect m_viewport;
194    IntRect m_viewRect;
195    FloatRect m_clipRect;
196    IntRect m_screenClip;
197    int m_titleBarHeight;
198    // This is the layout position in screen coordinate and didn't contain the
199    // animation offset.
200    IntRect m_webViewRect;
201
202    FloatRect m_documentViewport;
203
204    float m_contrast;
205
206    // The height of the render target, either FBO or screen.
207    int m_targetHeight;
208    bool m_alphaLayer;
209    TransformationMatrix m_webViewMatrix;
210    float m_currentScale;
211
212    // Put all the uniform location (handle) info into an array, and group them
213    // by the shader's type, this can help to clean up the interface.
214    // TODO: use the type and data comparison to skip GL call if possible.
215    ShaderHandles m_handleArray[MaxShaderNumber];
216
217    // If there is any GL error happens such that the Shaders are not initialized
218    // successfully at the first time, then we need to init again when we draw.
219    bool m_needsInit;
220
221    // For transfer queue blitting, we need a special matrix map from (0,1) to
222    // (-1,1)
223    GLfloat m_transferProjMtx[16];
224
225    GLfloat m_tileProjMatrix[16];
226
227    Vector<ShaderResource> m_resources;
228};
229
230} // namespace WebCore
231
232#endif // USE(ACCELERATED_COMPOSITING)
233#endif // ShaderProgram_h
234