ProgramCache.cpp revision 4121063313ac0d6f69f6253cac821d0c1c122086
154481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu/*
254481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu * Copyright (C) 2010 The Android Open Source Project
354481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu *
454481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu * Licensed under the Apache License, Version 2.0 (the "License");
554481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu * you may not use this file except in compliance with the License.
654481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu * You may obtain a copy of the License at
754481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu *
854481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu *      http://www.apache.org/licenses/LICENSE-2.0
954481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu *
1054481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu * Unless required by applicable law or agreed to in writing, software
1154481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu * distributed under the License is distributed on an "AS IS" BASIS,
1254481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1354481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu * See the License for the specific language governing permissions and
1454481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu * limitations under the License.
1554481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu */
1654481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu
178ca4ac971a9b862fbd69c57af3a3c0029a6b9f4dPeter Qiu#define LOG_TAG "OpenGLRenderer"
186a3903fed590e369b576bddbe1ae2d788768ddfeJan Nordqvist
1954481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu#include <utils/String8.h>
2054481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu
2171a988c8e9859244b83cd55bb6b6ee913fcaf95cJan Nordqvist#include "Caches.h"
2254481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu#include "ProgramCache.h"
236a3903fed590e369b576bddbe1ae2d788768ddfeJan Nordqvist
2471a988c8e9859244b83cd55bb6b6ee913fcaf95cJan Nordqvistnamespace android {
2571a988c8e9859244b83cd55bb6b6ee913fcaf95cJan Nordqvistnamespace uirenderer {
2671a988c8e9859244b83cd55bb6b6ee913fcaf95cJan Nordqvist
2771a988c8e9859244b83cd55bb6b6ee913fcaf95cJan Nordqvist///////////////////////////////////////////////////////////////////////////////
286a3903fed590e369b576bddbe1ae2d788768ddfeJan Nordqvist// Defines
296a3903fed590e369b576bddbe1ae2d788768ddfeJan Nordqvist///////////////////////////////////////////////////////////////////////////////
3054481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu
3154481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu#define MODULATE_OP_NO_MODULATE 0
3254481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu#define MODULATE_OP_MODULATE 1
3354481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu#define MODULATE_OP_MODULATE_A8 2
3454481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu
3554481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu///////////////////////////////////////////////////////////////////////////////
3654481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu// Vertex shaders snippets
3754481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu///////////////////////////////////////////////////////////////////////////////
3854481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu
396a3903fed590e369b576bddbe1ae2d788768ddfeJan Nordqvistconst char* gVS_Header_Attributes =
406a3903fed590e369b576bddbe1ae2d788768ddfeJan Nordqvist        "attribute vec4 position;\n";
4154481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiuconst char* gVS_Header_Attributes_TexCoords =
4254481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        "attribute vec2 texCoords;\n";
4354481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiuconst char* gVS_Header_Attributes_AAParameters =
4454481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        "attribute float vtxWidth;\n"
4554481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        "attribute float vtxLength;\n";
466a3903fed590e369b576bddbe1ae2d788768ddfeJan Nordqvistconst char* gVS_Header_Uniforms_TextureTransform =
4754481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        "uniform mat4 mainTextureTransform;\n";
4854481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiuconst char* gVS_Header_Uniforms =
4954481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        "uniform mat4 transform;\n";
5054481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiuconst char* gVS_Header_Uniforms_IsPoint =
5154481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        "uniform mediump float pointSize;\n";
5254481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiuconst char* gVS_Header_Uniforms_HasGradient[3] = {
5354481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        // Linear
5454481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        "uniform mat4 screenSpace;\n",
5554481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        // Circular
5654481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        "uniform mat4 screenSpace;\n",
5754481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        // Sweep
5854481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        "uniform mat4 screenSpace;\n"
5954481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu};
6054481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiuconst char* gVS_Header_Uniforms_HasBitmap =
6154481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        "uniform mat4 textureTransform;\n"
6254481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        "uniform mediump vec2 textureDimension;\n";
6354481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiuconst char* gVS_Header_Varyings_HasTexture =
6471a988c8e9859244b83cd55bb6b6ee913fcaf95cJan Nordqvist        "varying vec2 outTexCoords;\n";
6554481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiuconst char* gVS_Header_Varyings_IsAA =
6654481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        "varying float widthProportion;\n"
6754481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        "varying float lengthProportion;\n";
6854481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiuconst char* gVS_Header_Varyings_HasBitmap[2] = {
6954481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        // Default precision
7071a988c8e9859244b83cd55bb6b6ee913fcaf95cJan Nordqvist        "varying vec2 outBitmapTexCoords;\n",
7154481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        // High precision
7254481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        "varying highp vec2 outBitmapTexCoords;\n"
7354481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu};
7454481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiuconst char* gVS_Header_Varyings_PointHasBitmap[2] = {
7554481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        // Default precision
7671a988c8e9859244b83cd55bb6b6ee913fcaf95cJan Nordqvist        "varying vec2 outPointBitmapTexCoords;\n",
7771a988c8e9859244b83cd55bb6b6ee913fcaf95cJan Nordqvist        // High precision
7854481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        "varying highp vec2 outPointBitmapTexCoords;\n"
7954481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu};
8071a988c8e9859244b83cd55bb6b6ee913fcaf95cJan Nordqvistconst char* gVS_Header_Varyings_HasGradient[3] = {
8154481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        // Linear
8271a988c8e9859244b83cd55bb6b6ee913fcaf95cJan Nordqvist        "varying vec2 linear;\n",
8354481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        // Circular
8471a988c8e9859244b83cd55bb6b6ee913fcaf95cJan Nordqvist        "varying vec2 circular;\n",
8571a988c8e9859244b83cd55bb6b6ee913fcaf95cJan Nordqvist        // Sweep
8654481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        "varying vec2 sweep;\n"
8771a988c8e9859244b83cd55bb6b6ee913fcaf95cJan Nordqvist};
8871a988c8e9859244b83cd55bb6b6ee913fcaf95cJan Nordqvistconst char* gVS_Main =
8954481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        "\nvoid main(void) {\n";
9054481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiuconst char* gVS_Main_OutTexCoords =
916a3903fed590e369b576bddbe1ae2d788768ddfeJan Nordqvist        "    outTexCoords = texCoords;\n";
926a3903fed590e369b576bddbe1ae2d788768ddfeJan Nordqvistconst char* gVS_Main_OutTransformedTexCoords =
9354481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        "    outTexCoords = (mainTextureTransform * vec4(texCoords, 0.0, 1.0)).xy;\n";
9454481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiuconst char* gVS_Main_OutGradient[3] = {
9554481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        // Linear
9654481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        "    linear = vec2((screenSpace * position).x, 0.5);\n",
9754481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        // Circular
9854481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        "    circular = (screenSpace * position).xy;\n",
9954481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        // Sweep
10054481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        "    sweep = (screenSpace * position).xy;\n"
10154481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu};
10254481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiuconst char* gVS_Main_OutBitmapTexCoords =
10354481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        "    outBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
10454481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiuconst char* gVS_Main_OutPointBitmapTexCoords =
10554481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        "    outPointBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
10654481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiuconst char* gVS_Main_Position =
10754481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiu        "    gl_Position = transform * position;\n";
10854481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiuconst char* gVS_Main_PointSize =
1096a3903fed590e369b576bddbe1ae2d788768ddfeJan Nordqvist        "    gl_PointSize = pointSize;\n";
1106a3903fed590e369b576bddbe1ae2d788768ddfeJan Nordqvistconst char* gVS_Main_AA =
1116a3903fed590e369b576bddbe1ae2d788768ddfeJan Nordqvist        "    widthProportion = vtxWidth;\n"
1126a3903fed590e369b576bddbe1ae2d788768ddfeJan Nordqvist        "    lengthProportion = vtxLength;\n";
11354481f724e41249c4e036a9f59e8cb3e6fb821d8Peter Qiuconst char* gVS_Footer =
1146a3903fed590e369b576bddbe1ae2d788768ddfeJan Nordqvist        "}\n\n";
1156a3903fed590e369b576bddbe1ae2d788768ddfeJan Nordqvist
116///////////////////////////////////////////////////////////////////////////////
117// Fragment shaders snippets
118///////////////////////////////////////////////////////////////////////////////
119
120const char* gFS_Header_Extension_FramebufferFetch =
121        "#extension GL_NV_shader_framebuffer_fetch : enable\n\n";
122const char* gFS_Header_Extension_ExternalTexture =
123        "#extension GL_OES_EGL_image_external : require\n\n";
124const char* gFS_Header =
125        "precision mediump float;\n\n";
126const char* gFS_Uniforms_Color =
127        "uniform vec4 color;\n";
128const char* gFS_Uniforms_AA =
129        "uniform float boundaryWidth;\n"
130        "uniform float inverseBoundaryWidth;\n"
131        "uniform float boundaryLength;\n"
132        "uniform float inverseBoundaryLength;\n";
133const char* gFS_Header_Uniforms_PointHasBitmap =
134        "uniform vec2 textureDimension;\n"
135        "uniform float pointSize;\n";
136const char* gFS_Uniforms_TextureSampler =
137        "uniform sampler2D sampler;\n";
138const char* gFS_Uniforms_ExternalTextureSampler =
139        "uniform samplerExternalOES sampler;\n";
140const char* gFS_Uniforms_GradientSampler[3] = {
141        // Linear
142        "uniform sampler2D gradientSampler;\n",
143        // Circular
144        "uniform sampler2D gradientSampler;\n",
145        // Sweep
146        "uniform sampler2D gradientSampler;\n"
147};
148const char* gFS_Uniforms_BitmapSampler =
149        "uniform sampler2D bitmapSampler;\n";
150const char* gFS_Uniforms_ColorOp[4] = {
151        // None
152        "",
153        // Matrix
154        "uniform mat4 colorMatrix;\n"
155        "uniform vec4 colorMatrixVector;\n",
156        // Lighting
157        "uniform vec4 lightingMul;\n"
158        "uniform vec4 lightingAdd;\n",
159        // PorterDuff
160        "uniform vec4 colorBlend;\n"
161};
162const char* gFS_Uniforms_Gamma =
163        "uniform float gamma;\n";
164
165const char* gFS_Main =
166        "\nvoid main(void) {\n"
167        "    lowp vec4 fragColor;\n";
168
169const char* gFS_Main_PointBitmapTexCoords =
170        "    vec2 outBitmapTexCoords = outPointBitmapTexCoords + "
171        "((gl_PointCoord - vec2(0.5, 0.5)) * textureDimension * vec2(pointSize, pointSize));\n";
172
173// Fast cases
174const char* gFS_Fast_SingleColor =
175        "\nvoid main(void) {\n"
176        "    gl_FragColor = color;\n"
177        "}\n\n";
178const char* gFS_Fast_SingleTexture =
179        "\nvoid main(void) {\n"
180        "    gl_FragColor = texture2D(sampler, outTexCoords);\n"
181        "}\n\n";
182const char* gFS_Fast_SingleModulateTexture =
183        "\nvoid main(void) {\n"
184        "    gl_FragColor = color.a * texture2D(sampler, outTexCoords);\n"
185        "}\n\n";
186const char* gFS_Fast_SingleA8Texture =
187        "\nvoid main(void) {\n"
188        "    gl_FragColor = texture2D(sampler, outTexCoords);\n"
189        "}\n\n";
190const char* gFS_Fast_SingleA8Texture_ApplyGamma =
191        "\nvoid main(void) {\n"
192        "    gl_FragColor = vec4(0.0, 0.0, 0.0, pow(texture2D(sampler, outTexCoords).a, gamma));\n"
193        "}\n\n";
194const char* gFS_Fast_SingleModulateA8Texture =
195        "\nvoid main(void) {\n"
196        "    gl_FragColor = color * texture2D(sampler, outTexCoords).a;\n"
197        "}\n\n";
198const char* gFS_Fast_SingleModulateA8Texture_ApplyGamma =
199        "\nvoid main(void) {\n"
200        "    gl_FragColor = color * pow(texture2D(sampler, outTexCoords).a, gamma);\n"
201        "}\n\n";
202const char* gFS_Fast_SingleGradient =
203        "\nvoid main(void) {\n"
204        "    gl_FragColor = texture2D(gradientSampler, linear);\n"
205        "}\n\n";
206const char* gFS_Fast_SingleModulateGradient =
207        "\nvoid main(void) {\n"
208        "    gl_FragColor = color.a * texture2D(gradientSampler, linear);\n"
209        "}\n\n";
210
211// General case
212const char* gFS_Main_FetchColor =
213        "    fragColor = color;\n";
214const char* gFS_Main_ModulateColor =
215        "    fragColor *= color.a;\n";
216const char* gFS_Main_ModulateColor_ApplyGamma =
217        "    fragColor *= pow(color.a, gamma);\n";
218const char* gFS_Main_AccountForAA =
219        "    if (widthProportion < boundaryWidth) {\n"
220        "        fragColor *= (widthProportion * inverseBoundaryWidth);\n"
221        "    } else if (widthProportion > (1.0 - boundaryWidth)) {\n"
222        "        fragColor *= ((1.0 - widthProportion) * inverseBoundaryWidth);\n"
223        "    }\n"
224        "    if (lengthProportion < boundaryLength) {\n"
225        "        fragColor *= (lengthProportion * inverseBoundaryLength);\n"
226        "    } else if (lengthProportion > (1.0 - boundaryLength)) {\n"
227        "        fragColor *= ((1.0 - lengthProportion) * inverseBoundaryLength);\n"
228        "    }\n";
229const char* gFS_Main_FetchTexture[2] = {
230        // Don't modulate
231        "    fragColor = texture2D(sampler, outTexCoords);\n",
232        // Modulate
233        "    fragColor = color * texture2D(sampler, outTexCoords);\n"
234};
235const char* gFS_Main_FetchA8Texture[2] = {
236        // Don't modulate
237        "    fragColor = texture2D(sampler, outTexCoords);\n",
238        // Modulate
239        "    fragColor = color * texture2D(sampler, outTexCoords).a;\n"
240};
241const char* gFS_Main_FetchGradient[3] = {
242        // Linear
243        "    vec4 gradientColor = texture2D(gradientSampler, linear);\n",
244        // Circular
245        "    float index = length(circular);\n"
246        "    vec4 gradientColor = texture2D(gradientSampler, vec2(index, 0.5));\n",
247        // Sweep
248        "    float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
249        "    vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n"
250};
251const char* gFS_Main_FetchBitmap =
252        "    vec4 bitmapColor = texture2D(bitmapSampler, outBitmapTexCoords);\n";
253const char* gFS_Main_FetchBitmapNpot =
254        "    vec4 bitmapColor = texture2D(bitmapSampler, wrap(outBitmapTexCoords));\n";
255const char* gFS_Main_BlendShadersBG =
256        "    fragColor = blendShaders(gradientColor, bitmapColor)";
257const char* gFS_Main_BlendShadersGB =
258        "    fragColor = blendShaders(bitmapColor, gradientColor)";
259const char* gFS_Main_BlendShaders_Modulate[3] = {
260        // Don't modulate
261        ";\n",
262        // Modulate
263        " * fragColor.a;\n",
264        // Modulate with alpha 8 texture
265        " * texture2D(sampler, outTexCoords).a;\n"
266};
267const char* gFS_Main_GradientShader_Modulate[3] = {
268        // Don't modulate
269        "    fragColor = gradientColor;\n",
270        // Modulate
271        "    fragColor = gradientColor * fragColor.a;\n",
272        // Modulate with alpha 8 texture
273        "    fragColor = gradientColor * texture2D(sampler, outTexCoords).a;\n"
274    };
275const char* gFS_Main_BitmapShader_Modulate[3] = {
276        // Don't modulate
277        "    fragColor = bitmapColor;\n",
278        // Modulate
279        "    fragColor = bitmapColor * fragColor.a;\n",
280        // Modulate with alpha 8 texture
281        "    fragColor = bitmapColor * texture2D(sampler, outTexCoords).a;\n"
282    };
283const char* gFS_Main_FragColor =
284        "    gl_FragColor = fragColor;\n";
285const char* gFS_Main_FragColor_Blend =
286        "    gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n";
287const char* gFS_Main_FragColor_Blend_Swap =
288        "    gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n";
289const char* gFS_Main_ApplyColorOp[4] = {
290        // None
291        "",
292        // Matrix
293        // TODO: Fix premultiplied alpha computations for color matrix
294        "    fragColor *= colorMatrix;\n"
295        "    fragColor += colorMatrixVector;\n"
296        "    fragColor.rgb *= fragColor.a;\n",
297        // Lighting
298        "    float lightingAlpha = fragColor.a;\n"
299        "    fragColor = min(fragColor * lightingMul + (lightingAdd * lightingAlpha), lightingAlpha);\n"
300        "    fragColor.a = lightingAlpha;\n",
301        // PorterDuff
302        "    fragColor = blendColors(colorBlend, fragColor);\n"
303};
304const char* gFS_Footer =
305        "}\n\n";
306
307///////////////////////////////////////////////////////////////////////////////
308// PorterDuff snippets
309///////////////////////////////////////////////////////////////////////////////
310
311const char* gBlendOps[18] = {
312        // Clear
313        "return vec4(0.0, 0.0, 0.0, 0.0);\n",
314        // Src
315        "return src;\n",
316        // Dst
317        "return dst;\n",
318        // SrcOver
319        "return src + dst * (1.0 - src.a);\n",
320        // DstOver
321        "return dst + src * (1.0 - dst.a);\n",
322        // SrcIn
323        "return src * dst.a;\n",
324        // DstIn
325        "return dst * src.a;\n",
326        // SrcOut
327        "return src * (1.0 - dst.a);\n",
328        // DstOut
329        "return dst * (1.0 - src.a);\n",
330        // SrcAtop
331        "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n",
332        // DstAtop
333        "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n",
334        // Xor
335        "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, "
336                "src.a + dst.a - 2.0 * src.a * dst.a);\n",
337        // Add
338        "return min(src + dst, 1.0);\n",
339        // Multiply
340        "return src * dst;\n",
341        // Screen
342        "return src + dst - src * dst;\n",
343        // Overlay
344        "return clamp(vec4(mix("
345                "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), "
346                "src.a * dst.a - 2.0 * (dst.a - dst.rgb) * (src.a - src.rgb) + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), "
347                "step(dst.a, 2.0 * dst.rgb)), "
348                "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n",
349        // Darken
350        "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
351                "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
352        // Lighten
353        "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
354                "max(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
355};
356
357///////////////////////////////////////////////////////////////////////////////
358// Constructors/destructors
359///////////////////////////////////////////////////////////////////////////////
360
361ProgramCache::ProgramCache() {
362}
363
364ProgramCache::~ProgramCache() {
365    clear();
366}
367
368///////////////////////////////////////////////////////////////////////////////
369// Cache management
370///////////////////////////////////////////////////////////////////////////////
371
372void ProgramCache::clear() {
373    PROGRAM_LOGD("Clearing program cache");
374
375    size_t count = mCache.size();
376    for (size_t i = 0; i < count; i++) {
377        delete mCache.valueAt(i);
378    }
379    mCache.clear();
380}
381
382Program* ProgramCache::get(const ProgramDescription& description) {
383    programid key = description.key();
384    ssize_t index = mCache.indexOfKey(key);
385    Program* program = NULL;
386    if (index < 0) {
387        description.log("Could not find program");
388        program = generateProgram(description, key);
389        mCache.add(key, program);
390    } else {
391        program = mCache.valueAt(index);
392    }
393    return program;
394}
395
396///////////////////////////////////////////////////////////////////////////////
397// Program generation
398///////////////////////////////////////////////////////////////////////////////
399
400Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) {
401    String8 vertexShader = generateVertexShader(description);
402    String8 fragmentShader = generateFragmentShader(description);
403
404    Program* program = new Program(description, vertexShader.string(), fragmentShader.string());
405    return program;
406}
407
408String8 ProgramCache::generateVertexShader(const ProgramDescription& description) {
409    // Add attributes
410    String8 shader(gVS_Header_Attributes);
411    if (description.hasTexture || description.hasExternalTexture) {
412        shader.append(gVS_Header_Attributes_TexCoords);
413    }
414    if (description.isAA) {
415        shader.append(gVS_Header_Attributes_AAParameters);
416    }
417    // Uniforms
418    shader.append(gVS_Header_Uniforms);
419    if (description.hasTextureTransform) {
420        shader.append(gVS_Header_Uniforms_TextureTransform);
421    }
422    if (description.hasGradient) {
423        shader.append(gVS_Header_Uniforms_HasGradient[description.gradientType]);
424    }
425    if (description.hasBitmap) {
426        shader.append(gVS_Header_Uniforms_HasBitmap);
427    }
428    if (description.isPoint) {
429        shader.append(gVS_Header_Uniforms_IsPoint);
430    }
431    // Varyings
432    if (description.hasTexture || description.hasExternalTexture) {
433        shader.append(gVS_Header_Varyings_HasTexture);
434    }
435    if (description.isAA) {
436        shader.append(gVS_Header_Varyings_IsAA);
437    }
438    if (description.hasGradient) {
439        shader.append(gVS_Header_Varyings_HasGradient[description.gradientType]);
440    }
441    if (description.hasBitmap) {
442        int index = Caches::getInstance().extensions.needsHighpTexCoords() ? 1 : 0;
443        shader.append(description.isPoint ?
444                gVS_Header_Varyings_PointHasBitmap[index] :
445                gVS_Header_Varyings_HasBitmap[index]);
446    }
447
448    // Begin the shader
449    shader.append(gVS_Main); {
450        if (description.hasTextureTransform) {
451            shader.append(gVS_Main_OutTransformedTexCoords);
452        } else if (description.hasTexture || description.hasExternalTexture) {
453            shader.append(gVS_Main_OutTexCoords);
454        }
455        if (description.isAA) {
456            shader.append(gVS_Main_AA);
457        }
458        if (description.hasGradient) {
459            shader.append(gVS_Main_OutGradient[description.gradientType]);
460        }
461        if (description.hasBitmap) {
462            shader.append(description.isPoint ?
463                    gVS_Main_OutPointBitmapTexCoords :
464                    gVS_Main_OutBitmapTexCoords);
465        }
466        if (description.isPoint) {
467            shader.append(gVS_Main_PointSize);
468        }
469        // Output transformed position
470        shader.append(gVS_Main_Position);
471    }
472    // End the shader
473    shader.append(gVS_Footer);
474
475    PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string());
476
477    return shader;
478}
479
480String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) {
481    String8 shader;
482
483    const bool blendFramebuffer = description.framebufferMode >= SkXfermode::kPlus_Mode;
484    if (blendFramebuffer) {
485        shader.append(gFS_Header_Extension_FramebufferFetch);
486    }
487    if (description.hasExternalTexture) {
488        shader.append(gFS_Header_Extension_ExternalTexture);
489    }
490
491    shader.append(gFS_Header);
492
493    // Varyings
494    if (description.hasTexture || description.hasExternalTexture) {
495        shader.append(gVS_Header_Varyings_HasTexture);
496    }
497    if (description.isAA) {
498        shader.append(gVS_Header_Varyings_IsAA);
499    }
500    if (description.hasGradient) {
501        shader.append(gVS_Header_Varyings_HasGradient[description.gradientType]);
502    }
503    if (description.hasBitmap) {
504        int index = Caches::getInstance().extensions.needsHighpTexCoords() ? 1 : 0;
505        shader.append(description.isPoint ?
506                gVS_Header_Varyings_PointHasBitmap[index] :
507                gVS_Header_Varyings_HasBitmap[index]);
508    }
509
510    // Uniforms
511    int modulateOp = MODULATE_OP_NO_MODULATE;
512    const bool singleColor = !description.hasTexture && !description.hasExternalTexture &&
513            !description.hasGradient && !description.hasBitmap;
514
515    if (description.modulate || singleColor) {
516        shader.append(gFS_Uniforms_Color);
517        if (!singleColor) modulateOp = MODULATE_OP_MODULATE;
518    }
519    if (description.hasTexture) {
520        shader.append(gFS_Uniforms_TextureSampler);
521    } else if (description.hasExternalTexture) {
522        shader.append(gFS_Uniforms_ExternalTextureSampler);
523    }
524    if (description.isAA) {
525        shader.append(gFS_Uniforms_AA);
526    }
527    if (description.hasGradient) {
528        shader.append(gFS_Uniforms_GradientSampler[description.gradientType]);
529    }
530    if (description.hasBitmap && description.isPoint) {
531        shader.append(gFS_Header_Uniforms_PointHasBitmap);
532    }
533    if (description.hasGammaCorrection) {
534        shader.append(gFS_Uniforms_Gamma);
535    }
536
537    // Optimization for common cases
538    if (!description.isAA && !blendFramebuffer &&
539            description.colorOp == ProgramDescription::kColorNone && !description.isPoint) {
540        bool fast = false;
541
542        const bool noShader = !description.hasGradient && !description.hasBitmap;
543        const bool singleTexture = (description.hasTexture || description.hasExternalTexture) &&
544                !description.hasAlpha8Texture && noShader;
545        const bool singleA8Texture = description.hasTexture &&
546                description.hasAlpha8Texture && noShader;
547        const bool singleGradient = !description.hasTexture && !description.hasExternalTexture &&
548                description.hasGradient && !description.hasBitmap &&
549                description.gradientType == ProgramDescription::kGradientLinear;
550
551        if (singleColor) {
552            shader.append(gFS_Fast_SingleColor);
553            fast = true;
554        } else if (singleTexture) {
555            if (!description.modulate) {
556                shader.append(gFS_Fast_SingleTexture);
557            } else {
558                shader.append(gFS_Fast_SingleModulateTexture);
559            }
560            fast = true;
561        } else if (singleA8Texture) {
562            if (!description.modulate) {
563                if (description.hasGammaCorrection) {
564                    shader.append(gFS_Fast_SingleA8Texture_ApplyGamma);
565                } else {
566                    shader.append(gFS_Fast_SingleA8Texture);
567                }
568            } else {
569                if (description.hasGammaCorrection) {
570                    shader.append(gFS_Fast_SingleModulateA8Texture_ApplyGamma);
571                } else {
572                    shader.append(gFS_Fast_SingleModulateA8Texture);
573                }
574            }
575            fast = true;
576        } else if (singleGradient) {
577            if (!description.modulate) {
578                shader.append(gFS_Fast_SingleGradient);
579            } else {
580                shader.append(gFS_Fast_SingleModulateGradient);
581            }
582            fast = true;
583        }
584
585        if (fast) {
586#if DEBUG_PROGRAMS
587                PROGRAM_LOGD("*** Fast case:\n");
588                PROGRAM_LOGD("*** Generated fragment shader:\n\n");
589                printLongString(shader);
590#endif
591
592            return shader;
593        }
594    }
595
596    if (description.hasBitmap) {
597        shader.append(gFS_Uniforms_BitmapSampler);
598    }
599    shader.append(gFS_Uniforms_ColorOp[description.colorOp]);
600
601    // Generate required functions
602    if (description.hasGradient && description.hasBitmap) {
603        generateBlend(shader, "blendShaders", description.shadersMode);
604    }
605    if (description.colorOp == ProgramDescription::kColorBlend) {
606        generateBlend(shader, "blendColors", description.colorMode);
607    }
608    if (blendFramebuffer) {
609        generateBlend(shader, "blendFramebuffer", description.framebufferMode);
610    }
611    if (description.isBitmapNpot) {
612        generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
613    }
614
615    // Begin the shader
616    shader.append(gFS_Main); {
617        // Stores the result in fragColor directly
618        if (description.hasTexture || description.hasExternalTexture) {
619            if (description.hasAlpha8Texture) {
620                if (!description.hasGradient && !description.hasBitmap) {
621                    shader.append(gFS_Main_FetchA8Texture[modulateOp]);
622                }
623            } else {
624                shader.append(gFS_Main_FetchTexture[modulateOp]);
625            }
626        } else {
627            if ((!description.hasGradient && !description.hasBitmap) || description.modulate) {
628                shader.append(gFS_Main_FetchColor);
629            }
630        }
631        if (description.isAA) {
632            shader.append(gFS_Main_AccountForAA);
633        }
634        if (description.hasGradient) {
635            shader.append(gFS_Main_FetchGradient[description.gradientType]);
636        }
637        if (description.hasBitmap) {
638            if (description.isPoint) {
639                shader.append(gFS_Main_PointBitmapTexCoords);
640            }
641            if (!description.isBitmapNpot) {
642                shader.append(gFS_Main_FetchBitmap);
643            } else {
644                shader.append(gFS_Main_FetchBitmapNpot);
645            }
646        }
647        bool applyModulate = false;
648        // Case when we have two shaders set
649        if (description.hasGradient && description.hasBitmap) {
650            int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
651            if (description.isBitmapFirst) {
652                shader.append(gFS_Main_BlendShadersBG);
653            } else {
654                shader.append(gFS_Main_BlendShadersGB);
655            }
656            shader.append(gFS_Main_BlendShaders_Modulate[op]);
657            applyModulate = true;
658        } else {
659            if (description.hasGradient) {
660                int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
661                shader.append(gFS_Main_GradientShader_Modulate[op]);
662                applyModulate = true;
663            } else if (description.hasBitmap) {
664                int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
665                shader.append(gFS_Main_BitmapShader_Modulate[op]);
666                applyModulate = true;
667            }
668        }
669        if (description.modulate && applyModulate) {
670            if (description.hasGammaCorrection) {
671                shader.append(gFS_Main_ModulateColor_ApplyGamma);
672            } else {
673                shader.append(gFS_Main_ModulateColor);
674            }
675        }
676        // Apply the color op if needed
677        shader.append(gFS_Main_ApplyColorOp[description.colorOp]);
678        // Output the fragment
679        if (!blendFramebuffer) {
680            shader.append(gFS_Main_FragColor);
681        } else {
682            shader.append(!description.swapSrcDst ?
683                    gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap);
684        }
685    }
686    // End the shader
687    shader.append(gFS_Footer);
688
689#if DEBUG_PROGRAMS
690        PROGRAM_LOGD("*** Generated fragment shader:\n\n");
691        printLongString(shader);
692#endif
693
694    return shader;
695}
696
697void ProgramCache::generateBlend(String8& shader, const char* name, SkXfermode::Mode mode) {
698    shader.append("\nvec4 ");
699    shader.append(name);
700    shader.append("(vec4 src, vec4 dst) {\n");
701    shader.append("    ");
702    shader.append(gBlendOps[mode]);
703    shader.append("}\n");
704}
705
706void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) {
707    shader.append("\nvec2 wrap(vec2 texCoords) {\n");
708    if (wrapS == GL_MIRRORED_REPEAT) {
709        shader.append("    float xMod2 = mod(texCoords.x, 2.0);\n");
710        shader.append("    if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n");
711    }
712    if (wrapT == GL_MIRRORED_REPEAT) {
713        shader.append("    float yMod2 = mod(texCoords.y, 2.0);\n");
714        shader.append("    if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n");
715    }
716    shader.append("    return vec2(");
717    switch (wrapS) {
718        case GL_CLAMP_TO_EDGE:
719            shader.append("texCoords.x");
720            break;
721        case GL_REPEAT:
722            shader.append("mod(texCoords.x, 1.0)");
723            break;
724        case GL_MIRRORED_REPEAT:
725            shader.append("xMod2");
726            break;
727    }
728    shader.append(", ");
729    switch (wrapT) {
730        case GL_CLAMP_TO_EDGE:
731            shader.append("texCoords.y");
732            break;
733        case GL_REPEAT:
734            shader.append("mod(texCoords.y, 1.0)");
735            break;
736        case GL_MIRRORED_REPEAT:
737            shader.append("yMod2");
738            break;
739    }
740    shader.append(");\n");
741    shader.append("}\n");
742}
743
744void ProgramCache::printLongString(const String8& shader) const {
745    ssize_t index = 0;
746    ssize_t lastIndex = 0;
747    const char* str = shader.string();
748    while ((index = shader.find("\n", index)) > -1) {
749        String8 line(str, index - lastIndex);
750        if (line.length() == 0) line.append("\n");
751        PROGRAM_LOGD("%s", line.string());
752        index++;
753        str += (index - lastIndex);
754        lastIndex = index;
755    }
756}
757
758}; // namespace uirenderer
759}; // namespace android
760