OSDN Git Service

d8447bf71df65686121311e486ead16709597d51
[android-x86/external-webkit.git] / Source / WebCore / platform / graphics / android / ShaderProgram.h
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 projectionMatrix() { return m_hProjectionMatrix; }
37     int alpha() { return m_hAlpha; }
38     int textureSampler() { return m_hTexSampler; }
39     int program() { return m_program; }
40
41     // Drawing
42     void setViewport(SkRect& viewport);
43     float zValue(const TransformationMatrix& drawMatrix, float w, float h);
44
45     // For drawQuad and drawLayerQuad, they can handle 3 cases for now:
46     // 1) textureTarget == GL_TEXTURE_2D
47     // Normal texture in GL_TEXTURE_2D target.
48     // 2) textureTarget == GL_TEXTURE_EXTERNAL_OES
49     // Surface texture in GL_TEXTURE_EXTERNAL_OES target.
50     // 3) textureTarget == 0 (Will be deprecated soon)
51     // Surface texture in GL_TEXTURE_2D target.
52     //
53     // TODO: Shrink the support modes into 2 (1 and 2) after media framework
54     // support Surface texture in GL_TEXTURE_EXTERNAL_OES target on all
55     // platforms.
56     void drawQuad(SkRect& geometry, int textureId, float opacity,
57                   GLenum textureTarget = GL_TEXTURE_2D);
58     void drawLayerQuad(const TransformationMatrix& drawMatrix,
59                        SkRect& geometry, int textureId, float opacity,
60                        bool forceBlending = false,
61                        GLenum textureTarget = GL_TEXTURE_2D);
62     void drawVideoLayerQuad(const TransformationMatrix& drawMatrix,
63                      float* textureMatrix, SkRect& geometry, int textureId);
64     void setViewRect(const IntRect& viewRect);
65     FloatRect rectInScreenCoord(const TransformationMatrix& drawMatrix,
66                                 const IntSize& size);
67     FloatRect rectInInvScreenCoord(const TransformationMatrix& drawMatrix,
68                                 const IntSize& size);
69
70     FloatRect rectInInvScreenCoord(const FloatRect& rect);
71     FloatRect rectInScreenCoord(const FloatRect& rect);
72     FloatRect convertInvScreenCoordToScreenCoord(const FloatRect& rect);
73     FloatRect convertScreenCoordToInvScreenCoord(const FloatRect& rect);
74
75     void setTitleBarHeight(int height) { m_titleBarHeight = height; }
76     void setWebViewRect(const IntRect& rect) { m_webViewRect = rect; }
77     void setScreenClip(const IntRect& clip);
78     void clip(const FloatRect& rect);
79     IntRect clippedRectWithViewport(const IntRect& rect, int margin = 0);
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
92 private:
93     GLuint loadShader(GLenum shaderType, const char* pSource);
94     GLuint createProgram(const char* vertexSource, const char* fragmentSource);
95     void setProjectionMatrix(SkRect& geometry, GLint projectionMatrixHandle);
96
97     void setBlendingState(bool enableBlending);
98
99     void drawQuadInternal(SkRect& geometry, GLint textureId, float opacity,
100                           GLint program, GLint projectionMatrixHandle,
101                           GLint texSampler, GLenum textureTarget,
102                           GLint position, GLint alpha, GLint contrast = -1);
103
104     void drawLayerQuadInternal(const GLfloat* projectionMatrix, int textureId,
105                                float opacity, GLenum textureTarget, GLint program,
106                                GLint matrix, GLint texSample,
107                                GLint position, GLint alpha, GLint contrast = -1);
108
109     bool m_blendingEnabled;
110
111     int m_program;
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     int m_hProjectionMatrix;
130     int m_hAlpha;
131     int m_hTexSampler;
132     int m_hVideoProjectionMatrix;
133     int m_hVideoTextureMatrix;
134     int m_hVideoTexSampler;
135
136     GLint m_hSTOESProjectionMatrix;
137     GLint m_hSTOESAlpha;
138     GLint m_hSTOESTexSampler;
139     GLint m_hSTOESPosition;
140
141     GLint m_hSTOESProjectionMatrixInverted;
142     GLint m_hSTOESAlphaInverted;
143     GLint m_hSTOESContrastInverted;
144     GLint m_hSTOESTexSamplerInverted;
145     GLint m_hSTOESPositionInverted;
146
147     float m_contrast;
148
149     // attribs
150     GLint m_hPosition;
151     GLint m_hVideoPosition;
152 };
153
154 } // namespace WebCore
155
156 #endif // USE(ACCELERATED_COMPOSITING)
157 #endif // ShaderProgram_h