blob: 70bd1a85490b5c1538f650309dd3a1b9ac95babe [file] [log] [blame]
Romain Guyac670c02010-07-27 17:39:27 -07001/*
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#define LOG_TAG "OpenGLRenderer"
18
19#include <utils/String8.h>
20
Romain Guya60c3882011-08-01 15:28:16 -070021#include "Caches.h"
Romain Guyac670c02010-07-27 17:39:27 -070022#include "ProgramCache.h"
23
24namespace android {
25namespace uirenderer {
26
27///////////////////////////////////////////////////////////////////////////////
Romain Guy707b2f72010-10-11 16:34:59 -070028// Defines
29///////////////////////////////////////////////////////////////////////////////
30
31#define MODULATE_OP_NO_MODULATE 0
32#define MODULATE_OP_MODULATE 1
33#define MODULATE_OP_MODULATE_A8 2
34
35///////////////////////////////////////////////////////////////////////////////
Romain Guyac670c02010-07-27 17:39:27 -070036// Vertex shaders snippets
37///////////////////////////////////////////////////////////////////////////////
38
Romain Guyac670c02010-07-27 17:39:27 -070039const char* gVS_Header_Attributes =
40 "attribute vec4 position;\n";
41const char* gVS_Header_Attributes_TexCoords =
42 "attribute vec2 texCoords;\n";
Chet Haase99585ad2011-05-02 15:00:16 -070043const char* gVS_Header_Attributes_AAParameters =
44 "attribute float vtxWidth;\n"
45 "attribute float vtxLength;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -070046const char* gVS_Header_Uniforms_TextureTransform =
47 "uniform mat4 mainTextureTransform;\n";
Romain Guyac670c02010-07-27 17:39:27 -070048const char* gVS_Header_Uniforms =
49 "uniform mat4 transform;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -070050const char* gVS_Header_Uniforms_IsPoint =
Romain Guy80bbfb12011-03-23 16:56:28 -070051 "uniform mediump float pointSize;\n";
Romain Guyee916f12010-09-20 17:53:08 -070052const char* gVS_Header_Uniforms_HasGradient[3] = {
53 // Linear
Romain Guyee916f12010-09-20 17:53:08 -070054 "uniform mat4 screenSpace;\n",
55 // Circular
Romain Guyddb80be2010-09-20 19:04:33 -070056 "uniform mat4 screenSpace;\n",
Romain Guyee916f12010-09-20 17:53:08 -070057 // Sweep
Romain Guyee916f12010-09-20 17:53:08 -070058 "uniform mat4 screenSpace;\n"
59};
Romain Guy889f8d12010-07-29 14:37:42 -070060const char* gVS_Header_Uniforms_HasBitmap =
61 "uniform mat4 textureTransform;\n"
Romain Guy80bbfb12011-03-23 16:56:28 -070062 "uniform mediump vec2 textureDimension;\n";
Romain Guyac670c02010-07-27 17:39:27 -070063const char* gVS_Header_Varyings_HasTexture =
64 "varying vec2 outTexCoords;\n";
Chet Haase99585ad2011-05-02 15:00:16 -070065const char* gVS_Header_Varyings_IsAA =
66 "varying float widthProportion;\n"
67 "varying float lengthProportion;\n";
Romain Guy63553472012-07-18 20:04:14 -070068const char* gVS_Header_Varyings_HasBitmap =
69 "varying highp vec2 outBitmapTexCoords;\n";
70const char* gVS_Header_Varyings_PointHasBitmap =
71 "varying highp vec2 outPointBitmapTexCoords;\n";
Romain Guy8e025de2012-07-18 17:44:07 -070072// TODO: These values are used to sample from textures,
73// they may need to be highp
Romain Guyee916f12010-09-20 17:53:08 -070074const char* gVS_Header_Varyings_HasGradient[3] = {
75 // Linear
Romain Guy63553472012-07-18 20:04:14 -070076 "varying highp vec2 linear;\n",
Romain Guyee916f12010-09-20 17:53:08 -070077 // Circular
Romain Guy63553472012-07-18 20:04:14 -070078 "varying highp vec2 circular;\n",
Romain Guyee916f12010-09-20 17:53:08 -070079 // Sweep
Romain Guy63553472012-07-18 20:04:14 -070080 "varying highp vec2 sweep;\n"
Romain Guyee916f12010-09-20 17:53:08 -070081};
Romain Guyac670c02010-07-27 17:39:27 -070082const char* gVS_Main =
83 "\nvoid main(void) {\n";
84const char* gVS_Main_OutTexCoords =
85 " outTexCoords = texCoords;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -070086const char* gVS_Main_OutTransformedTexCoords =
87 " outTexCoords = (mainTextureTransform * vec4(texCoords, 0.0, 1.0)).xy;\n";
Romain Guyee916f12010-09-20 17:53:08 -070088const char* gVS_Main_OutGradient[3] = {
89 // Linear
Romain Guy7537f852010-10-11 14:38:28 -070090 " linear = vec2((screenSpace * position).x, 0.5);\n",
Romain Guyee916f12010-09-20 17:53:08 -070091 // Circular
Romain Guy14830942010-10-07 15:07:45 -070092 " circular = (screenSpace * position).xy;\n",
Romain Guyee916f12010-09-20 17:53:08 -070093 // Sweep
Romain Guy14830942010-10-07 15:07:45 -070094 " sweep = (screenSpace * position).xy;\n"
Romain Guyee916f12010-09-20 17:53:08 -070095};
Romain Guy889f8d12010-07-29 14:37:42 -070096const char* gVS_Main_OutBitmapTexCoords =
Romain Guy707b2f72010-10-11 16:34:59 -070097 " outBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -070098const char* gVS_Main_OutPointBitmapTexCoords =
99 " outPointBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700100const char* gVS_Main_Position =
101 " gl_Position = transform * position;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -0700102const char* gVS_Main_PointSize =
103 " gl_PointSize = pointSize;\n";
Chet Haase99585ad2011-05-02 15:00:16 -0700104const char* gVS_Main_AA =
105 " widthProportion = vtxWidth;\n"
106 " lengthProportion = vtxLength;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700107const char* gVS_Footer =
108 "}\n\n";
109
110///////////////////////////////////////////////////////////////////////////////
111// Fragment shaders snippets
112///////////////////////////////////////////////////////////////////////////////
113
Romain Guya5aed0d2010-09-09 14:42:43 -0700114const char* gFS_Header_Extension_FramebufferFetch =
115 "#extension GL_NV_shader_framebuffer_fetch : enable\n\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700116const char* gFS_Header_Extension_ExternalTexture =
117 "#extension GL_OES_EGL_image_external : require\n\n";
Romain Guyac670c02010-07-27 17:39:27 -0700118const char* gFS_Header =
119 "precision mediump float;\n\n";
120const char* gFS_Uniforms_Color =
121 "uniform vec4 color;\n";
Chet Haase99585ad2011-05-02 15:00:16 -0700122const char* gFS_Uniforms_AA =
Chet Haase5b0200b2011-04-13 17:58:08 -0700123 "uniform float boundaryWidth;\n"
Chet Haase99585ad2011-05-02 15:00:16 -0700124 "uniform float inverseBoundaryWidth;\n"
125 "uniform float boundaryLength;\n"
126 "uniform float inverseBoundaryLength;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -0700127const char* gFS_Header_Uniforms_PointHasBitmap =
128 "uniform vec2 textureDimension;\n"
129 "uniform float pointSize;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700130const char* gFS_Uniforms_TextureSampler =
131 "uniform sampler2D sampler;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700132const char* gFS_Uniforms_ExternalTextureSampler =
133 "uniform samplerExternalOES sampler;\n";
Romain Guyee916f12010-09-20 17:53:08 -0700134const char* gFS_Uniforms_GradientSampler[3] = {
135 // Linear
136 "uniform sampler2D gradientSampler;\n",
137 // Circular
138 "uniform sampler2D gradientSampler;\n",
139 // Sweep
140 "uniform sampler2D gradientSampler;\n"
141};
Romain Guyac670c02010-07-27 17:39:27 -0700142const char* gFS_Uniforms_BitmapSampler =
143 "uniform sampler2D bitmapSampler;\n";
144const char* gFS_Uniforms_ColorOp[4] = {
145 // None
146 "",
147 // Matrix
148 "uniform mat4 colorMatrix;\n"
149 "uniform vec4 colorMatrixVector;\n",
150 // Lighting
Romain Guydb1938e2010-08-02 18:50:22 -0700151 "uniform vec4 lightingMul;\n"
152 "uniform vec4 lightingAdd;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700153 // PorterDuff
Romain Guydb1938e2010-08-02 18:50:22 -0700154 "uniform vec4 colorBlend;\n"
Romain Guyac670c02010-07-27 17:39:27 -0700155};
Romain Guy41210632012-07-16 17:04:24 -0700156const char* gFS_Uniforms_Gamma =
157 "uniform float gamma;\n";
158
Romain Guyac670c02010-07-27 17:39:27 -0700159const char* gFS_Main =
160 "\nvoid main(void) {\n"
Romain Guy7fbcc042010-08-04 15:40:07 -0700161 " lowp vec4 fragColor;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700162
Romain Guyed6fcb02011-03-21 13:11:28 -0700163const char* gFS_Main_PointBitmapTexCoords =
Romain Guy63553472012-07-18 20:04:14 -0700164 " highp vec2 outBitmapTexCoords = outPointBitmapTexCoords + "
Romain Guyed6fcb02011-03-21 13:11:28 -0700165 "((gl_PointCoord - vec2(0.5, 0.5)) * textureDimension * vec2(pointSize, pointSize));\n";
166
Romain Guy707b2f72010-10-11 16:34:59 -0700167// Fast cases
168const char* gFS_Fast_SingleColor =
169 "\nvoid main(void) {\n"
170 " gl_FragColor = color;\n"
171 "}\n\n";
172const char* gFS_Fast_SingleTexture =
173 "\nvoid main(void) {\n"
174 " gl_FragColor = texture2D(sampler, outTexCoords);\n"
175 "}\n\n";
176const char* gFS_Fast_SingleModulateTexture =
177 "\nvoid main(void) {\n"
178 " gl_FragColor = color.a * texture2D(sampler, outTexCoords);\n"
179 "}\n\n";
180const char* gFS_Fast_SingleA8Texture =
181 "\nvoid main(void) {\n"
Romain Guy9db91242010-10-12 13:13:09 -0700182 " gl_FragColor = texture2D(sampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700183 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700184const char* gFS_Fast_SingleA8Texture_ApplyGamma =
185 "\nvoid main(void) {\n"
186 " gl_FragColor = vec4(0.0, 0.0, 0.0, pow(texture2D(sampler, outTexCoords).a, gamma));\n"
187 "}\n\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700188const char* gFS_Fast_SingleModulateA8Texture =
189 "\nvoid main(void) {\n"
190 " gl_FragColor = color * texture2D(sampler, outTexCoords).a;\n"
191 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700192const char* gFS_Fast_SingleModulateA8Texture_ApplyGamma =
193 "\nvoid main(void) {\n"
194 " gl_FragColor = color * pow(texture2D(sampler, outTexCoords).a, gamma);\n"
195 "}\n\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700196const char* gFS_Fast_SingleGradient =
197 "\nvoid main(void) {\n"
198 " gl_FragColor = texture2D(gradientSampler, linear);\n"
199 "}\n\n";
200const char* gFS_Fast_SingleModulateGradient =
201 "\nvoid main(void) {\n"
202 " gl_FragColor = color.a * texture2D(gradientSampler, linear);\n"
203 "}\n\n";
204
205// General case
Romain Guyac670c02010-07-27 17:39:27 -0700206const char* gFS_Main_FetchColor =
207 " fragColor = color;\n";
Romain Guy740bf2b2011-04-26 15:33:10 -0700208const char* gFS_Main_ModulateColor =
209 " fragColor *= color.a;\n";
Romain Guy41210632012-07-16 17:04:24 -0700210const char* gFS_Main_ModulateColor_ApplyGamma =
211 " fragColor *= pow(color.a, gamma);\n";
Chet Haase99585ad2011-05-02 15:00:16 -0700212const char* gFS_Main_AccountForAA =
213 " if (widthProportion < boundaryWidth) {\n"
214 " fragColor *= (widthProportion * inverseBoundaryWidth);\n"
215 " } else if (widthProportion > (1.0 - boundaryWidth)) {\n"
216 " fragColor *= ((1.0 - widthProportion) * inverseBoundaryWidth);\n"
217 " }\n"
218 " if (lengthProportion < boundaryLength) {\n"
219 " fragColor *= (lengthProportion * inverseBoundaryLength);\n"
220 " } else if (lengthProportion > (1.0 - boundaryLength)) {\n"
221 " fragColor *= ((1.0 - lengthProportion) * inverseBoundaryLength);\n"
Chet Haase5b0200b2011-04-13 17:58:08 -0700222 " }\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700223const char* gFS_Main_FetchTexture[2] = {
224 // Don't modulate
225 " fragColor = texture2D(sampler, outTexCoords);\n",
226 // Modulate
227 " fragColor = color * texture2D(sampler, outTexCoords);\n"
228};
229const char* gFS_Main_FetchA8Texture[2] = {
230 // Don't modulate
Romain Guy9db91242010-10-12 13:13:09 -0700231 " fragColor = texture2D(sampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700232 // Modulate
233 " fragColor = color * texture2D(sampler, outTexCoords).a;\n"
234};
Romain Guyee916f12010-09-20 17:53:08 -0700235const char* gFS_Main_FetchGradient[3] = {
236 // Linear
Romain Guy7537f852010-10-11 14:38:28 -0700237 " vec4 gradientColor = texture2D(gradientSampler, linear);\n",
Romain Guyee916f12010-09-20 17:53:08 -0700238 // Circular
Romain Guy63553472012-07-18 20:04:14 -0700239 " highp float index = length(circular);\n"
Romain Guyddb80be2010-09-20 19:04:33 -0700240 " vec4 gradientColor = texture2D(gradientSampler, vec2(index, 0.5));\n",
Romain Guyee916f12010-09-20 17:53:08 -0700241 // Sweep
Romain Guy63553472012-07-18 20:04:14 -0700242 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guyee916f12010-09-20 17:53:08 -0700243 " vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n"
244};
Romain Guyac670c02010-07-27 17:39:27 -0700245const char* gFS_Main_FetchBitmap =
246 " vec4 bitmapColor = texture2D(bitmapSampler, outBitmapTexCoords);\n";
Romain Guy889f8d12010-07-29 14:37:42 -0700247const char* gFS_Main_FetchBitmapNpot =
248 " vec4 bitmapColor = texture2D(bitmapSampler, wrap(outBitmapTexCoords));\n";
Romain Guyac670c02010-07-27 17:39:27 -0700249const char* gFS_Main_BlendShadersBG =
Romain Guyac670c02010-07-27 17:39:27 -0700250 " fragColor = blendShaders(gradientColor, bitmapColor)";
Romain Guy06f96e22010-07-30 19:18:16 -0700251const char* gFS_Main_BlendShadersGB =
252 " fragColor = blendShaders(bitmapColor, gradientColor)";
Romain Guy707b2f72010-10-11 16:34:59 -0700253const char* gFS_Main_BlendShaders_Modulate[3] = {
254 // Don't modulate
255 ";\n",
256 // Modulate
257 " * fragColor.a;\n",
258 // Modulate with alpha 8 texture
259 " * texture2D(sampler, outTexCoords).a;\n"
260};
261const char* gFS_Main_GradientShader_Modulate[3] = {
262 // Don't modulate
263 " fragColor = gradientColor;\n",
264 // Modulate
265 " fragColor = gradientColor * fragColor.a;\n",
266 // Modulate with alpha 8 texture
267 " fragColor = gradientColor * texture2D(sampler, outTexCoords).a;\n"
268 };
269const char* gFS_Main_BitmapShader_Modulate[3] = {
270 // Don't modulate
271 " fragColor = bitmapColor;\n",
272 // Modulate
273 " fragColor = bitmapColor * fragColor.a;\n",
274 // Modulate with alpha 8 texture
275 " fragColor = bitmapColor * texture2D(sampler, outTexCoords).a;\n"
276 };
Romain Guyac670c02010-07-27 17:39:27 -0700277const char* gFS_Main_FragColor =
278 " gl_FragColor = fragColor;\n";
Romain Guya5aed0d2010-09-09 14:42:43 -0700279const char* gFS_Main_FragColor_Blend =
280 " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n";
Romain Guyf607bdc2010-09-10 19:20:06 -0700281const char* gFS_Main_FragColor_Blend_Swap =
282 " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n";
Romain Guyac670c02010-07-27 17:39:27 -0700283const char* gFS_Main_ApplyColorOp[4] = {
284 // None
285 "",
286 // Matrix
Romain Guydb1938e2010-08-02 18:50:22 -0700287 // TODO: Fix premultiplied alpha computations for color matrix
Romain Guyac670c02010-07-27 17:39:27 -0700288 " fragColor *= colorMatrix;\n"
Romain Guydb1938e2010-08-02 18:50:22 -0700289 " fragColor += colorMatrixVector;\n"
290 " fragColor.rgb *= fragColor.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700291 // Lighting
Romain Guydb1938e2010-08-02 18:50:22 -0700292 " float lightingAlpha = fragColor.a;\n"
293 " fragColor = min(fragColor * lightingMul + (lightingAdd * lightingAlpha), lightingAlpha);\n"
294 " fragColor.a = lightingAlpha;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700295 // PorterDuff
296 " fragColor = blendColors(colorBlend, fragColor);\n"
297};
298const char* gFS_Footer =
299 "}\n\n";
300
301///////////////////////////////////////////////////////////////////////////////
302// PorterDuff snippets
303///////////////////////////////////////////////////////////////////////////////
304
Romain Guy48daa542010-08-10 19:21:34 -0700305const char* gBlendOps[18] = {
Romain Guyac670c02010-07-27 17:39:27 -0700306 // Clear
307 "return vec4(0.0, 0.0, 0.0, 0.0);\n",
308 // Src
309 "return src;\n",
310 // Dst
311 "return dst;\n",
312 // SrcOver
Romain Guy06f96e22010-07-30 19:18:16 -0700313 "return src + dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700314 // DstOver
Romain Guy06f96e22010-07-30 19:18:16 -0700315 "return dst + src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700316 // SrcIn
Romain Guy06f96e22010-07-30 19:18:16 -0700317 "return src * dst.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700318 // DstIn
Romain Guy06f96e22010-07-30 19:18:16 -0700319 "return dst * src.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700320 // SrcOut
Romain Guy06f96e22010-07-30 19:18:16 -0700321 "return src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700322 // DstOut
Romain Guy06f96e22010-07-30 19:18:16 -0700323 "return dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700324 // SrcAtop
325 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n",
326 // DstAtop
327 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n",
328 // Xor
Romain Guy48daa542010-08-10 19:21:34 -0700329 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, "
Romain Guyac670c02010-07-27 17:39:27 -0700330 "src.a + dst.a - 2.0 * src.a * dst.a);\n",
Romain Guy48daa542010-08-10 19:21:34 -0700331 // Add
332 "return min(src + dst, 1.0);\n",
333 // Multiply
334 "return src * dst;\n",
335 // Screen
336 "return src + dst - src * dst;\n",
337 // Overlay
338 "return clamp(vec4(mix("
339 "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), "
340 "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), "
341 "step(dst.a, 2.0 * dst.rgb)), "
342 "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n",
343 // Darken
344 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
345 "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
346 // Lighten
347 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
348 "max(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700349};
350
351///////////////////////////////////////////////////////////////////////////////
352// Constructors/destructors
353///////////////////////////////////////////////////////////////////////////////
354
355ProgramCache::ProgramCache() {
356}
357
358ProgramCache::~ProgramCache() {
359 clear();
360}
361
362///////////////////////////////////////////////////////////////////////////////
363// Cache management
364///////////////////////////////////////////////////////////////////////////////
365
366void ProgramCache::clear() {
Romain Guy67f27952010-12-07 20:09:23 -0800367 PROGRAM_LOGD("Clearing program cache");
368
Romain Guyac670c02010-07-27 17:39:27 -0700369 size_t count = mCache.size();
370 for (size_t i = 0; i < count; i++) {
371 delete mCache.valueAt(i);
372 }
373 mCache.clear();
374}
375
376Program* ProgramCache::get(const ProgramDescription& description) {
377 programid key = description.key();
378 ssize_t index = mCache.indexOfKey(key);
379 Program* program = NULL;
380 if (index < 0) {
Romain Guyee916f12010-09-20 17:53:08 -0700381 description.log("Could not find program");
Romain Guyac670c02010-07-27 17:39:27 -0700382 program = generateProgram(description, key);
383 mCache.add(key, program);
384 } else {
385 program = mCache.valueAt(index);
386 }
387 return program;
388}
389
390///////////////////////////////////////////////////////////////////////////////
391// Program generation
392///////////////////////////////////////////////////////////////////////////////
393
394Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) {
395 String8 vertexShader = generateVertexShader(description);
396 String8 fragmentShader = generateFragmentShader(description);
397
Romain Guyf3a910b42011-12-12 20:35:21 -0800398 Program* program = new Program(description, vertexShader.string(), fragmentShader.string());
Romain Guyac670c02010-07-27 17:39:27 -0700399 return program;
400}
401
402String8 ProgramCache::generateVertexShader(const ProgramDescription& description) {
403 // Add attributes
404 String8 shader(gVS_Header_Attributes);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700405 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700406 shader.append(gVS_Header_Attributes_TexCoords);
407 }
Chet Haase99585ad2011-05-02 15:00:16 -0700408 if (description.isAA) {
409 shader.append(gVS_Header_Attributes_AAParameters);
Chet Haase5b0200b2011-04-13 17:58:08 -0700410 }
Romain Guyac670c02010-07-27 17:39:27 -0700411 // Uniforms
412 shader.append(gVS_Header_Uniforms);
Romain Guy8f0095c2011-05-02 17:24:22 -0700413 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700414 shader.append(gVS_Header_Uniforms_TextureTransform);
415 }
Romain Guyac670c02010-07-27 17:39:27 -0700416 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700417 shader.append(gVS_Header_Uniforms_HasGradient[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700418 }
Romain Guy889f8d12010-07-29 14:37:42 -0700419 if (description.hasBitmap) {
420 shader.append(gVS_Header_Uniforms_HasBitmap);
421 }
Romain Guyed6fcb02011-03-21 13:11:28 -0700422 if (description.isPoint) {
423 shader.append(gVS_Header_Uniforms_IsPoint);
424 }
Romain Guyac670c02010-07-27 17:39:27 -0700425 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700426 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700427 shader.append(gVS_Header_Varyings_HasTexture);
428 }
Chet Haase99585ad2011-05-02 15:00:16 -0700429 if (description.isAA) {
430 shader.append(gVS_Header_Varyings_IsAA);
Chet Haase5b0200b2011-04-13 17:58:08 -0700431 }
Romain Guyac670c02010-07-27 17:39:27 -0700432 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700433 shader.append(gVS_Header_Varyings_HasGradient[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700434 }
435 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700436 shader.append(description.isPoint ?
Romain Guy63553472012-07-18 20:04:14 -0700437 gVS_Header_Varyings_PointHasBitmap :
438 gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700439 }
440
441 // Begin the shader
442 shader.append(gVS_Main); {
Romain Guy8f0095c2011-05-02 17:24:22 -0700443 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700444 shader.append(gVS_Main_OutTransformedTexCoords);
Romain Guy8f0095c2011-05-02 17:24:22 -0700445 } else if (description.hasTexture || description.hasExternalTexture) {
446 shader.append(gVS_Main_OutTexCoords);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700447 }
Chet Haase99585ad2011-05-02 15:00:16 -0700448 if (description.isAA) {
449 shader.append(gVS_Main_AA);
Chet Haase5b0200b2011-04-13 17:58:08 -0700450 }
Romain Guyac670c02010-07-27 17:39:27 -0700451 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700452 shader.append(gVS_Main_OutGradient[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700453 }
Romain Guy889f8d12010-07-29 14:37:42 -0700454 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700455 shader.append(description.isPoint ?
456 gVS_Main_OutPointBitmapTexCoords :
457 gVS_Main_OutBitmapTexCoords);
458 }
459 if (description.isPoint) {
460 shader.append(gVS_Main_PointSize);
Romain Guy889f8d12010-07-29 14:37:42 -0700461 }
Romain Guyac670c02010-07-27 17:39:27 -0700462 // Output transformed position
463 shader.append(gVS_Main_Position);
464 }
465 // End the shader
466 shader.append(gVS_Footer);
467
468 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string());
469
470 return shader;
471}
472
473String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) {
Romain Guya5aed0d2010-09-09 14:42:43 -0700474 String8 shader;
475
Romain Guy707b2f72010-10-11 16:34:59 -0700476 const bool blendFramebuffer = description.framebufferMode >= SkXfermode::kPlus_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700477 if (blendFramebuffer) {
478 shader.append(gFS_Header_Extension_FramebufferFetch);
479 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700480 if (description.hasExternalTexture) {
481 shader.append(gFS_Header_Extension_ExternalTexture);
482 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700483
484 shader.append(gFS_Header);
Romain Guyac670c02010-07-27 17:39:27 -0700485
486 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700487 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700488 shader.append(gVS_Header_Varyings_HasTexture);
489 }
Chet Haase99585ad2011-05-02 15:00:16 -0700490 if (description.isAA) {
491 shader.append(gVS_Header_Varyings_IsAA);
Chet Haase5b0200b2011-04-13 17:58:08 -0700492 }
Romain Guyac670c02010-07-27 17:39:27 -0700493 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700494 shader.append(gVS_Header_Varyings_HasGradient[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700495 }
496 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700497 shader.append(description.isPoint ?
Romain Guy63553472012-07-18 20:04:14 -0700498 gVS_Header_Varyings_PointHasBitmap :
499 gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700500 }
501
Romain Guyac670c02010-07-27 17:39:27 -0700502 // Uniforms
Romain Guy707b2f72010-10-11 16:34:59 -0700503 int modulateOp = MODULATE_OP_NO_MODULATE;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700504 const bool singleColor = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700505 !description.hasGradient && !description.hasBitmap;
506
507 if (description.modulate || singleColor) {
508 shader.append(gFS_Uniforms_Color);
509 if (!singleColor) modulateOp = MODULATE_OP_MODULATE;
510 }
Romain Guyac670c02010-07-27 17:39:27 -0700511 if (description.hasTexture) {
512 shader.append(gFS_Uniforms_TextureSampler);
Romain Guy8f0095c2011-05-02 17:24:22 -0700513 } else if (description.hasExternalTexture) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700514 shader.append(gFS_Uniforms_ExternalTextureSampler);
515 }
Chet Haase99585ad2011-05-02 15:00:16 -0700516 if (description.isAA) {
517 shader.append(gFS_Uniforms_AA);
Chet Haase5b0200b2011-04-13 17:58:08 -0700518 }
Romain Guyac670c02010-07-27 17:39:27 -0700519 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700520 shader.append(gFS_Uniforms_GradientSampler[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700521 }
Romain Guyed6fcb02011-03-21 13:11:28 -0700522 if (description.hasBitmap && description.isPoint) {
523 shader.append(gFS_Header_Uniforms_PointHasBitmap);
524 }
Romain Guy41210632012-07-16 17:04:24 -0700525 if (description.hasGammaCorrection) {
526 shader.append(gFS_Uniforms_Gamma);
527 }
Romain Guy707b2f72010-10-11 16:34:59 -0700528
529 // Optimization for common cases
Chet Haase99585ad2011-05-02 15:00:16 -0700530 if (!description.isAA && !blendFramebuffer &&
Chet Haase5b0200b2011-04-13 17:58:08 -0700531 description.colorOp == ProgramDescription::kColorNone && !description.isPoint) {
Romain Guy707b2f72010-10-11 16:34:59 -0700532 bool fast = false;
533
534 const bool noShader = !description.hasGradient && !description.hasBitmap;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700535 const bool singleTexture = (description.hasTexture || description.hasExternalTexture) &&
Romain Guy707b2f72010-10-11 16:34:59 -0700536 !description.hasAlpha8Texture && noShader;
537 const bool singleA8Texture = description.hasTexture &&
538 description.hasAlpha8Texture && noShader;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700539 const bool singleGradient = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700540 description.hasGradient && !description.hasBitmap &&
541 description.gradientType == ProgramDescription::kGradientLinear;
542
543 if (singleColor) {
544 shader.append(gFS_Fast_SingleColor);
545 fast = true;
546 } else if (singleTexture) {
547 if (!description.modulate) {
548 shader.append(gFS_Fast_SingleTexture);
549 } else {
550 shader.append(gFS_Fast_SingleModulateTexture);
551 }
552 fast = true;
553 } else if (singleA8Texture) {
554 if (!description.modulate) {
Romain Guy41210632012-07-16 17:04:24 -0700555 if (description.hasGammaCorrection) {
556 shader.append(gFS_Fast_SingleA8Texture_ApplyGamma);
557 } else {
558 shader.append(gFS_Fast_SingleA8Texture);
559 }
Romain Guy707b2f72010-10-11 16:34:59 -0700560 } else {
Romain Guy41210632012-07-16 17:04:24 -0700561 if (description.hasGammaCorrection) {
562 shader.append(gFS_Fast_SingleModulateA8Texture_ApplyGamma);
563 } else {
564 shader.append(gFS_Fast_SingleModulateA8Texture);
565 }
Romain Guy707b2f72010-10-11 16:34:59 -0700566 }
567 fast = true;
568 } else if (singleGradient) {
569 if (!description.modulate) {
570 shader.append(gFS_Fast_SingleGradient);
571 } else {
572 shader.append(gFS_Fast_SingleModulateGradient);
573 }
574 fast = true;
575 }
576
577 if (fast) {
Romain Guyc15008e2010-11-10 11:59:15 -0800578#if DEBUG_PROGRAMS
Romain Guy707b2f72010-10-11 16:34:59 -0700579 PROGRAM_LOGD("*** Fast case:\n");
580 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
581 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800582#endif
Romain Guy707b2f72010-10-11 16:34:59 -0700583
584 return shader;
585 }
586 }
587
Romain Guyac670c02010-07-27 17:39:27 -0700588 if (description.hasBitmap) {
589 shader.append(gFS_Uniforms_BitmapSampler);
590 }
591 shader.append(gFS_Uniforms_ColorOp[description.colorOp]);
592
593 // Generate required functions
594 if (description.hasGradient && description.hasBitmap) {
Romain Guy48daa542010-08-10 19:21:34 -0700595 generateBlend(shader, "blendShaders", description.shadersMode);
Romain Guyac670c02010-07-27 17:39:27 -0700596 }
597 if (description.colorOp == ProgramDescription::kColorBlend) {
Romain Guy48daa542010-08-10 19:21:34 -0700598 generateBlend(shader, "blendColors", description.colorMode);
Romain Guyac670c02010-07-27 17:39:27 -0700599 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700600 if (blendFramebuffer) {
601 generateBlend(shader, "blendFramebuffer", description.framebufferMode);
602 }
Romain Guy889f8d12010-07-29 14:37:42 -0700603 if (description.isBitmapNpot) {
604 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
605 }
Romain Guyac670c02010-07-27 17:39:27 -0700606
607 // Begin the shader
608 shader.append(gFS_Main); {
609 // Stores the result in fragColor directly
Romain Guyaa6c24c2011-04-28 18:40:04 -0700610 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700611 if (description.hasAlpha8Texture) {
Romain Guy707b2f72010-10-11 16:34:59 -0700612 if (!description.hasGradient && !description.hasBitmap) {
613 shader.append(gFS_Main_FetchA8Texture[modulateOp]);
614 }
Romain Guyac670c02010-07-27 17:39:27 -0700615 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700616 shader.append(gFS_Main_FetchTexture[modulateOp]);
Romain Guyac670c02010-07-27 17:39:27 -0700617 }
618 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700619 if ((!description.hasGradient && !description.hasBitmap) || description.modulate) {
620 shader.append(gFS_Main_FetchColor);
621 }
Romain Guyac670c02010-07-27 17:39:27 -0700622 }
Chet Haase99585ad2011-05-02 15:00:16 -0700623 if (description.isAA) {
624 shader.append(gFS_Main_AccountForAA);
Chet Haase5b0200b2011-04-13 17:58:08 -0700625 }
Romain Guyac670c02010-07-27 17:39:27 -0700626 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700627 shader.append(gFS_Main_FetchGradient[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700628 }
629 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700630 if (description.isPoint) {
631 shader.append(gFS_Main_PointBitmapTexCoords);
632 }
Romain Guy889f8d12010-07-29 14:37:42 -0700633 if (!description.isBitmapNpot) {
634 shader.append(gFS_Main_FetchBitmap);
635 } else {
636 shader.append(gFS_Main_FetchBitmapNpot);
637 }
Romain Guyac670c02010-07-27 17:39:27 -0700638 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700639 bool applyModulate = false;
Romain Guyac670c02010-07-27 17:39:27 -0700640 // Case when we have two shaders set
641 if (description.hasGradient && description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700642 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
Romain Guyac670c02010-07-27 17:39:27 -0700643 if (description.isBitmapFirst) {
644 shader.append(gFS_Main_BlendShadersBG);
645 } else {
646 shader.append(gFS_Main_BlendShadersGB);
647 }
Romain Guy707b2f72010-10-11 16:34:59 -0700648 shader.append(gFS_Main_BlendShaders_Modulate[op]);
Romain Guy740bf2b2011-04-26 15:33:10 -0700649 applyModulate = true;
Romain Guy889f8d12010-07-29 14:37:42 -0700650 } else {
651 if (description.hasGradient) {
Romain Guy707b2f72010-10-11 16:34:59 -0700652 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
653 shader.append(gFS_Main_GradientShader_Modulate[op]);
Romain Guy740bf2b2011-04-26 15:33:10 -0700654 applyModulate = true;
Romain Guy889f8d12010-07-29 14:37:42 -0700655 } else if (description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700656 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
657 shader.append(gFS_Main_BitmapShader_Modulate[op]);
Romain Guy740bf2b2011-04-26 15:33:10 -0700658 applyModulate = true;
Romain Guy889f8d12010-07-29 14:37:42 -0700659 }
Romain Guyac670c02010-07-27 17:39:27 -0700660 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700661 if (description.modulate && applyModulate) {
Romain Guy41210632012-07-16 17:04:24 -0700662 if (description.hasGammaCorrection) {
663 shader.append(gFS_Main_ModulateColor_ApplyGamma);
664 } else {
665 shader.append(gFS_Main_ModulateColor);
666 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700667 }
Romain Guyac670c02010-07-27 17:39:27 -0700668 // Apply the color op if needed
669 shader.append(gFS_Main_ApplyColorOp[description.colorOp]);
670 // Output the fragment
Romain Guya5aed0d2010-09-09 14:42:43 -0700671 if (!blendFramebuffer) {
672 shader.append(gFS_Main_FragColor);
673 } else {
Romain Guyf607bdc2010-09-10 19:20:06 -0700674 shader.append(!description.swapSrcDst ?
675 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap);
Romain Guya5aed0d2010-09-09 14:42:43 -0700676 }
Romain Guyac670c02010-07-27 17:39:27 -0700677 }
678 // End the shader
679 shader.append(gFS_Footer);
680
Romain Guyc15008e2010-11-10 11:59:15 -0800681#if DEBUG_PROGRAMS
Romain Guydb1938e2010-08-02 18:50:22 -0700682 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
683 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800684#endif
Romain Guydb1938e2010-08-02 18:50:22 -0700685
Romain Guyac670c02010-07-27 17:39:27 -0700686 return shader;
687}
688
Romain Guy48daa542010-08-10 19:21:34 -0700689void ProgramCache::generateBlend(String8& shader, const char* name, SkXfermode::Mode mode) {
Romain Guyac670c02010-07-27 17:39:27 -0700690 shader.append("\nvec4 ");
691 shader.append(name);
692 shader.append("(vec4 src, vec4 dst) {\n");
693 shader.append(" ");
Romain Guy48daa542010-08-10 19:21:34 -0700694 shader.append(gBlendOps[mode]);
Romain Guyac670c02010-07-27 17:39:27 -0700695 shader.append("}\n");
696}
697
Romain Guy889f8d12010-07-29 14:37:42 -0700698void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) {
Romain Guy63553472012-07-18 20:04:14 -0700699 shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700700 if (wrapS == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700701 shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700702 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n");
703 }
704 if (wrapT == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700705 shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700706 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n");
707 }
708 shader.append(" return vec2(");
709 switch (wrapS) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700710 case GL_CLAMP_TO_EDGE:
711 shader.append("texCoords.x");
712 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700713 case GL_REPEAT:
714 shader.append("mod(texCoords.x, 1.0)");
715 break;
716 case GL_MIRRORED_REPEAT:
717 shader.append("xMod2");
718 break;
719 }
720 shader.append(", ");
721 switch (wrapT) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700722 case GL_CLAMP_TO_EDGE:
723 shader.append("texCoords.y");
724 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700725 case GL_REPEAT:
726 shader.append("mod(texCoords.y, 1.0)");
727 break;
728 case GL_MIRRORED_REPEAT:
729 shader.append("yMod2");
730 break;
731 }
732 shader.append(");\n");
733 shader.append("}\n");
734}
735
Romain Guydb1938e2010-08-02 18:50:22 -0700736void ProgramCache::printLongString(const String8& shader) const {
737 ssize_t index = 0;
738 ssize_t lastIndex = 0;
739 const char* str = shader.string();
740 while ((index = shader.find("\n", index)) > -1) {
741 String8 line(str, index - lastIndex);
742 if (line.length() == 0) line.append("\n");
743 PROGRAM_LOGD("%s", line.string());
744 index++;
745 str += (index - lastIndex);
746 lastIndex = index;
747 }
748}
749
Romain Guyac670c02010-07-27 17:39:27 -0700750}; // namespace uirenderer
751}; // namespace android