blob: fc602795505ba1cb4f8fad68ccb352f1fa4b8bef [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 Guya60c3882011-08-01 15:28:16 -070068const char* gVS_Header_Varyings_HasBitmap[2] = {
69 // Default precision
70 "varying vec2 outBitmapTexCoords;\n",
71 // High precision
72 "varying highp vec2 outBitmapTexCoords;\n"
73};
74const char* gVS_Header_Varyings_PointHasBitmap[2] = {
75 // Default precision
76 "varying vec2 outPointBitmapTexCoords;\n",
77 // High precision
78 "varying highp vec2 outPointBitmapTexCoords;\n"
79};
Romain Guyee916f12010-09-20 17:53:08 -070080const char* gVS_Header_Varyings_HasGradient[3] = {
81 // Linear
Romain Guy7537f852010-10-11 14:38:28 -070082 "varying vec2 linear;\n",
Romain Guyee916f12010-09-20 17:53:08 -070083 // Circular
Romain Guyddb80be2010-09-20 19:04:33 -070084 "varying vec2 circular;\n",
Romain Guyee916f12010-09-20 17:53:08 -070085 // Sweep
86 "varying vec2 sweep;\n"
87};
Romain Guyac670c02010-07-27 17:39:27 -070088const char* gVS_Main =
89 "\nvoid main(void) {\n";
90const char* gVS_Main_OutTexCoords =
91 " outTexCoords = texCoords;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -070092const char* gVS_Main_OutTransformedTexCoords =
93 " outTexCoords = (mainTextureTransform * vec4(texCoords, 0.0, 1.0)).xy;\n";
Romain Guyee916f12010-09-20 17:53:08 -070094const char* gVS_Main_OutGradient[3] = {
95 // Linear
Romain Guy7537f852010-10-11 14:38:28 -070096 " linear = vec2((screenSpace * position).x, 0.5);\n",
Romain Guyee916f12010-09-20 17:53:08 -070097 // Circular
Romain Guy14830942010-10-07 15:07:45 -070098 " circular = (screenSpace * position).xy;\n",
Romain Guyee916f12010-09-20 17:53:08 -070099 // Sweep
Romain Guy14830942010-10-07 15:07:45 -0700100 " sweep = (screenSpace * position).xy;\n"
Romain Guyee916f12010-09-20 17:53:08 -0700101};
Romain Guy889f8d12010-07-29 14:37:42 -0700102const char* gVS_Main_OutBitmapTexCoords =
Romain Guy707b2f72010-10-11 16:34:59 -0700103 " outBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -0700104const char* gVS_Main_OutPointBitmapTexCoords =
105 " outPointBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700106const char* gVS_Main_Position =
107 " gl_Position = transform * position;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -0700108const char* gVS_Main_PointSize =
109 " gl_PointSize = pointSize;\n";
Chet Haase99585ad2011-05-02 15:00:16 -0700110const char* gVS_Main_AA =
111 " widthProportion = vtxWidth;\n"
112 " lengthProportion = vtxLength;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700113const char* gVS_Footer =
114 "}\n\n";
115
116///////////////////////////////////////////////////////////////////////////////
117// Fragment shaders snippets
118///////////////////////////////////////////////////////////////////////////////
119
Romain Guya5aed0d2010-09-09 14:42:43 -0700120const char* gFS_Header_Extension_FramebufferFetch =
121 "#extension GL_NV_shader_framebuffer_fetch : enable\n\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700122const char* gFS_Header_Extension_ExternalTexture =
123 "#extension GL_OES_EGL_image_external : require\n\n";
Romain Guyac670c02010-07-27 17:39:27 -0700124const char* gFS_Header =
125 "precision mediump float;\n\n";
126const char* gFS_Uniforms_Color =
127 "uniform vec4 color;\n";
Chet Haase99585ad2011-05-02 15:00:16 -0700128const char* gFS_Uniforms_AA =
Chet Haase5b0200b2011-04-13 17:58:08 -0700129 "uniform float boundaryWidth;\n"
Chet Haase99585ad2011-05-02 15:00:16 -0700130 "uniform float inverseBoundaryWidth;\n"
131 "uniform float boundaryLength;\n"
132 "uniform float inverseBoundaryLength;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -0700133const char* gFS_Header_Uniforms_PointHasBitmap =
134 "uniform vec2 textureDimension;\n"
135 "uniform float pointSize;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700136const char* gFS_Uniforms_TextureSampler =
137 "uniform sampler2D sampler;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700138const char* gFS_Uniforms_ExternalTextureSampler =
139 "uniform samplerExternalOES sampler;\n";
Romain Guyee916f12010-09-20 17:53:08 -0700140const 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};
Romain Guyac670c02010-07-27 17:39:27 -0700148const 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
Romain Guydb1938e2010-08-02 18:50:22 -0700157 "uniform vec4 lightingMul;\n"
158 "uniform vec4 lightingAdd;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700159 // PorterDuff
Romain Guydb1938e2010-08-02 18:50:22 -0700160 "uniform vec4 colorBlend;\n"
Romain Guyac670c02010-07-27 17:39:27 -0700161};
Romain Guy41210632012-07-16 17:04:24 -0700162const char* gFS_Uniforms_Gamma =
163 "uniform float gamma;\n";
164
Romain Guyac670c02010-07-27 17:39:27 -0700165const char* gFS_Main =
166 "\nvoid main(void) {\n"
Romain Guy7fbcc042010-08-04 15:40:07 -0700167 " lowp vec4 fragColor;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700168
Romain Guyed6fcb02011-03-21 13:11:28 -0700169const char* gFS_Main_PointBitmapTexCoords =
170 " vec2 outBitmapTexCoords = outPointBitmapTexCoords + "
171 "((gl_PointCoord - vec2(0.5, 0.5)) * textureDimension * vec2(pointSize, pointSize));\n";
172
Romain Guy707b2f72010-10-11 16:34:59 -0700173// 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"
Romain Guy9db91242010-10-12 13:13:09 -0700188 " gl_FragColor = texture2D(sampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700189 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700190const 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";
Romain Guy707b2f72010-10-11 16:34:59 -0700194const char* gFS_Fast_SingleModulateA8Texture =
195 "\nvoid main(void) {\n"
196 " gl_FragColor = color * texture2D(sampler, outTexCoords).a;\n"
197 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700198const char* gFS_Fast_SingleModulateA8Texture_ApplyGamma =
199 "\nvoid main(void) {\n"
200 " gl_FragColor = color * pow(texture2D(sampler, outTexCoords).a, gamma);\n"
201 "}\n\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700202const 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
Romain Guyac670c02010-07-27 17:39:27 -0700212const char* gFS_Main_FetchColor =
213 " fragColor = color;\n";
Romain Guy740bf2b2011-04-26 15:33:10 -0700214const char* gFS_Main_ModulateColor =
215 " fragColor *= color.a;\n";
Romain Guy41210632012-07-16 17:04:24 -0700216const char* gFS_Main_ModulateColor_ApplyGamma =
217 " fragColor *= pow(color.a, gamma);\n";
Chet Haase99585ad2011-05-02 15:00:16 -0700218const 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"
Chet Haase5b0200b2011-04-13 17:58:08 -0700228 " }\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700229const 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
Romain Guy9db91242010-10-12 13:13:09 -0700237 " fragColor = texture2D(sampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700238 // Modulate
239 " fragColor = color * texture2D(sampler, outTexCoords).a;\n"
240};
Romain Guyee916f12010-09-20 17:53:08 -0700241const char* gFS_Main_FetchGradient[3] = {
242 // Linear
Romain Guy7537f852010-10-11 14:38:28 -0700243 " vec4 gradientColor = texture2D(gradientSampler, linear);\n",
Romain Guyee916f12010-09-20 17:53:08 -0700244 // Circular
Romain Guy14830942010-10-07 15:07:45 -0700245 " float index = length(circular);\n"
Romain Guyddb80be2010-09-20 19:04:33 -0700246 " vec4 gradientColor = texture2D(gradientSampler, vec2(index, 0.5));\n",
Romain Guyee916f12010-09-20 17:53:08 -0700247 // 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};
Romain Guyac670c02010-07-27 17:39:27 -0700251const char* gFS_Main_FetchBitmap =
252 " vec4 bitmapColor = texture2D(bitmapSampler, outBitmapTexCoords);\n";
Romain Guy889f8d12010-07-29 14:37:42 -0700253const char* gFS_Main_FetchBitmapNpot =
254 " vec4 bitmapColor = texture2D(bitmapSampler, wrap(outBitmapTexCoords));\n";
Romain Guyac670c02010-07-27 17:39:27 -0700255const char* gFS_Main_BlendShadersBG =
Romain Guyac670c02010-07-27 17:39:27 -0700256 " fragColor = blendShaders(gradientColor, bitmapColor)";
Romain Guy06f96e22010-07-30 19:18:16 -0700257const char* gFS_Main_BlendShadersGB =
258 " fragColor = blendShaders(bitmapColor, gradientColor)";
Romain Guy707b2f72010-10-11 16:34:59 -0700259const 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 };
Romain Guyac670c02010-07-27 17:39:27 -0700283const char* gFS_Main_FragColor =
284 " gl_FragColor = fragColor;\n";
Romain Guya5aed0d2010-09-09 14:42:43 -0700285const char* gFS_Main_FragColor_Blend =
286 " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n";
Romain Guyf607bdc2010-09-10 19:20:06 -0700287const char* gFS_Main_FragColor_Blend_Swap =
288 " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n";
Romain Guyac670c02010-07-27 17:39:27 -0700289const char* gFS_Main_ApplyColorOp[4] = {
290 // None
291 "",
292 // Matrix
Romain Guydb1938e2010-08-02 18:50:22 -0700293 // TODO: Fix premultiplied alpha computations for color matrix
Romain Guyac670c02010-07-27 17:39:27 -0700294 " fragColor *= colorMatrix;\n"
Romain Guydb1938e2010-08-02 18:50:22 -0700295 " fragColor += colorMatrixVector;\n"
296 " fragColor.rgb *= fragColor.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700297 // Lighting
Romain Guydb1938e2010-08-02 18:50:22 -0700298 " float lightingAlpha = fragColor.a;\n"
299 " fragColor = min(fragColor * lightingMul + (lightingAdd * lightingAlpha), lightingAlpha);\n"
300 " fragColor.a = lightingAlpha;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700301 // PorterDuff
302 " fragColor = blendColors(colorBlend, fragColor);\n"
303};
304const char* gFS_Footer =
305 "}\n\n";
306
307///////////////////////////////////////////////////////////////////////////////
308// PorterDuff snippets
309///////////////////////////////////////////////////////////////////////////////
310
Romain Guy48daa542010-08-10 19:21:34 -0700311const char* gBlendOps[18] = {
Romain Guyac670c02010-07-27 17:39:27 -0700312 // 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
Romain Guy06f96e22010-07-30 19:18:16 -0700319 "return src + dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700320 // DstOver
Romain Guy06f96e22010-07-30 19:18:16 -0700321 "return dst + src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700322 // SrcIn
Romain Guy06f96e22010-07-30 19:18:16 -0700323 "return src * dst.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700324 // DstIn
Romain Guy06f96e22010-07-30 19:18:16 -0700325 "return dst * src.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700326 // SrcOut
Romain Guy06f96e22010-07-30 19:18:16 -0700327 "return src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700328 // DstOut
Romain Guy06f96e22010-07-30 19:18:16 -0700329 "return dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700330 // 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
Romain Guy48daa542010-08-10 19:21:34 -0700335 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, "
Romain Guyac670c02010-07-27 17:39:27 -0700336 "src.a + dst.a - 2.0 * src.a * dst.a);\n",
Romain Guy48daa542010-08-10 19:21:34 -0700337 // 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",
Romain Guyac670c02010-07-27 17:39:27 -0700355};
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() {
Romain Guy67f27952010-12-07 20:09:23 -0800373 PROGRAM_LOGD("Clearing program cache");
374
Romain Guyac670c02010-07-27 17:39:27 -0700375 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) {
Romain Guyee916f12010-09-20 17:53:08 -0700387 description.log("Could not find program");
Romain Guyac670c02010-07-27 17:39:27 -0700388 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
Romain Guyf3a910b42011-12-12 20:35:21 -0800404 Program* program = new Program(description, vertexShader.string(), fragmentShader.string());
Romain Guyac670c02010-07-27 17:39:27 -0700405 return program;
406}
407
408String8 ProgramCache::generateVertexShader(const ProgramDescription& description) {
409 // Add attributes
410 String8 shader(gVS_Header_Attributes);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700411 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700412 shader.append(gVS_Header_Attributes_TexCoords);
413 }
Chet Haase99585ad2011-05-02 15:00:16 -0700414 if (description.isAA) {
415 shader.append(gVS_Header_Attributes_AAParameters);
Chet Haase5b0200b2011-04-13 17:58:08 -0700416 }
Romain Guyac670c02010-07-27 17:39:27 -0700417 // Uniforms
418 shader.append(gVS_Header_Uniforms);
Romain Guy8f0095c2011-05-02 17:24:22 -0700419 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700420 shader.append(gVS_Header_Uniforms_TextureTransform);
421 }
Romain Guyac670c02010-07-27 17:39:27 -0700422 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700423 shader.append(gVS_Header_Uniforms_HasGradient[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700424 }
Romain Guy889f8d12010-07-29 14:37:42 -0700425 if (description.hasBitmap) {
426 shader.append(gVS_Header_Uniforms_HasBitmap);
427 }
Romain Guyed6fcb02011-03-21 13:11:28 -0700428 if (description.isPoint) {
429 shader.append(gVS_Header_Uniforms_IsPoint);
430 }
Romain Guyac670c02010-07-27 17:39:27 -0700431 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700432 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700433 shader.append(gVS_Header_Varyings_HasTexture);
434 }
Chet Haase99585ad2011-05-02 15:00:16 -0700435 if (description.isAA) {
436 shader.append(gVS_Header_Varyings_IsAA);
Chet Haase5b0200b2011-04-13 17:58:08 -0700437 }
Romain Guyac670c02010-07-27 17:39:27 -0700438 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700439 shader.append(gVS_Header_Varyings_HasGradient[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700440 }
441 if (description.hasBitmap) {
Romain Guya60c3882011-08-01 15:28:16 -0700442 int index = Caches::getInstance().extensions.needsHighpTexCoords() ? 1 : 0;
Romain Guyed6fcb02011-03-21 13:11:28 -0700443 shader.append(description.isPoint ?
Romain Guya60c3882011-08-01 15:28:16 -0700444 gVS_Header_Varyings_PointHasBitmap[index] :
445 gVS_Header_Varyings_HasBitmap[index]);
Romain Guyac670c02010-07-27 17:39:27 -0700446 }
447
448 // Begin the shader
449 shader.append(gVS_Main); {
Romain Guy8f0095c2011-05-02 17:24:22 -0700450 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700451 shader.append(gVS_Main_OutTransformedTexCoords);
Romain Guy8f0095c2011-05-02 17:24:22 -0700452 } else if (description.hasTexture || description.hasExternalTexture) {
453 shader.append(gVS_Main_OutTexCoords);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700454 }
Chet Haase99585ad2011-05-02 15:00:16 -0700455 if (description.isAA) {
456 shader.append(gVS_Main_AA);
Chet Haase5b0200b2011-04-13 17:58:08 -0700457 }
Romain Guyac670c02010-07-27 17:39:27 -0700458 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700459 shader.append(gVS_Main_OutGradient[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700460 }
Romain Guy889f8d12010-07-29 14:37:42 -0700461 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700462 shader.append(description.isPoint ?
463 gVS_Main_OutPointBitmapTexCoords :
464 gVS_Main_OutBitmapTexCoords);
465 }
466 if (description.isPoint) {
467 shader.append(gVS_Main_PointSize);
Romain Guy889f8d12010-07-29 14:37:42 -0700468 }
Romain Guyac670c02010-07-27 17:39:27 -0700469 // 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) {
Romain Guya5aed0d2010-09-09 14:42:43 -0700481 String8 shader;
482
Romain Guy707b2f72010-10-11 16:34:59 -0700483 const bool blendFramebuffer = description.framebufferMode >= SkXfermode::kPlus_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700484 if (blendFramebuffer) {
485 shader.append(gFS_Header_Extension_FramebufferFetch);
486 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700487 if (description.hasExternalTexture) {
488 shader.append(gFS_Header_Extension_ExternalTexture);
489 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700490
491 shader.append(gFS_Header);
Romain Guyac670c02010-07-27 17:39:27 -0700492
493 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700494 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700495 shader.append(gVS_Header_Varyings_HasTexture);
496 }
Chet Haase99585ad2011-05-02 15:00:16 -0700497 if (description.isAA) {
498 shader.append(gVS_Header_Varyings_IsAA);
Chet Haase5b0200b2011-04-13 17:58:08 -0700499 }
Romain Guyac670c02010-07-27 17:39:27 -0700500 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700501 shader.append(gVS_Header_Varyings_HasGradient[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700502 }
503 if (description.hasBitmap) {
Romain Guya60c3882011-08-01 15:28:16 -0700504 int index = Caches::getInstance().extensions.needsHighpTexCoords() ? 1 : 0;
Romain Guyed6fcb02011-03-21 13:11:28 -0700505 shader.append(description.isPoint ?
Romain Guya60c3882011-08-01 15:28:16 -0700506 gVS_Header_Varyings_PointHasBitmap[index] :
507 gVS_Header_Varyings_HasBitmap[index]);
Romain Guyac670c02010-07-27 17:39:27 -0700508 }
509
Romain Guyac670c02010-07-27 17:39:27 -0700510 // Uniforms
Romain Guy707b2f72010-10-11 16:34:59 -0700511 int modulateOp = MODULATE_OP_NO_MODULATE;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700512 const bool singleColor = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700513 !description.hasGradient && !description.hasBitmap;
514
515 if (description.modulate || singleColor) {
516 shader.append(gFS_Uniforms_Color);
517 if (!singleColor) modulateOp = MODULATE_OP_MODULATE;
518 }
Romain Guyac670c02010-07-27 17:39:27 -0700519 if (description.hasTexture) {
520 shader.append(gFS_Uniforms_TextureSampler);
Romain Guy8f0095c2011-05-02 17:24:22 -0700521 } else if (description.hasExternalTexture) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700522 shader.append(gFS_Uniforms_ExternalTextureSampler);
523 }
Chet Haase99585ad2011-05-02 15:00:16 -0700524 if (description.isAA) {
525 shader.append(gFS_Uniforms_AA);
Chet Haase5b0200b2011-04-13 17:58:08 -0700526 }
Romain Guyac670c02010-07-27 17:39:27 -0700527 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700528 shader.append(gFS_Uniforms_GradientSampler[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700529 }
Romain Guyed6fcb02011-03-21 13:11:28 -0700530 if (description.hasBitmap && description.isPoint) {
531 shader.append(gFS_Header_Uniforms_PointHasBitmap);
532 }
Romain Guy41210632012-07-16 17:04:24 -0700533 if (description.hasGammaCorrection) {
534 shader.append(gFS_Uniforms_Gamma);
535 }
Romain Guy707b2f72010-10-11 16:34:59 -0700536
537 // Optimization for common cases
Chet Haase99585ad2011-05-02 15:00:16 -0700538 if (!description.isAA && !blendFramebuffer &&
Chet Haase5b0200b2011-04-13 17:58:08 -0700539 description.colorOp == ProgramDescription::kColorNone && !description.isPoint) {
Romain Guy707b2f72010-10-11 16:34:59 -0700540 bool fast = false;
541
542 const bool noShader = !description.hasGradient && !description.hasBitmap;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700543 const bool singleTexture = (description.hasTexture || description.hasExternalTexture) &&
Romain Guy707b2f72010-10-11 16:34:59 -0700544 !description.hasAlpha8Texture && noShader;
545 const bool singleA8Texture = description.hasTexture &&
546 description.hasAlpha8Texture && noShader;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700547 const bool singleGradient = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700548 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) {
Romain Guy41210632012-07-16 17:04:24 -0700563 if (description.hasGammaCorrection) {
564 shader.append(gFS_Fast_SingleA8Texture_ApplyGamma);
565 } else {
566 shader.append(gFS_Fast_SingleA8Texture);
567 }
Romain Guy707b2f72010-10-11 16:34:59 -0700568 } else {
Romain Guy41210632012-07-16 17:04:24 -0700569 if (description.hasGammaCorrection) {
570 shader.append(gFS_Fast_SingleModulateA8Texture_ApplyGamma);
571 } else {
572 shader.append(gFS_Fast_SingleModulateA8Texture);
573 }
Romain Guy707b2f72010-10-11 16:34:59 -0700574 }
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) {
Romain Guyc15008e2010-11-10 11:59:15 -0800586#if DEBUG_PROGRAMS
Romain Guy707b2f72010-10-11 16:34:59 -0700587 PROGRAM_LOGD("*** Fast case:\n");
588 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
589 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800590#endif
Romain Guy707b2f72010-10-11 16:34:59 -0700591
592 return shader;
593 }
594 }
595
Romain Guyac670c02010-07-27 17:39:27 -0700596 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) {
Romain Guy48daa542010-08-10 19:21:34 -0700603 generateBlend(shader, "blendShaders", description.shadersMode);
Romain Guyac670c02010-07-27 17:39:27 -0700604 }
605 if (description.colorOp == ProgramDescription::kColorBlend) {
Romain Guy48daa542010-08-10 19:21:34 -0700606 generateBlend(shader, "blendColors", description.colorMode);
Romain Guyac670c02010-07-27 17:39:27 -0700607 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700608 if (blendFramebuffer) {
609 generateBlend(shader, "blendFramebuffer", description.framebufferMode);
610 }
Romain Guy889f8d12010-07-29 14:37:42 -0700611 if (description.isBitmapNpot) {
612 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
613 }
Romain Guyac670c02010-07-27 17:39:27 -0700614
615 // Begin the shader
616 shader.append(gFS_Main); {
617 // Stores the result in fragColor directly
Romain Guyaa6c24c2011-04-28 18:40:04 -0700618 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700619 if (description.hasAlpha8Texture) {
Romain Guy707b2f72010-10-11 16:34:59 -0700620 if (!description.hasGradient && !description.hasBitmap) {
621 shader.append(gFS_Main_FetchA8Texture[modulateOp]);
622 }
Romain Guyac670c02010-07-27 17:39:27 -0700623 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700624 shader.append(gFS_Main_FetchTexture[modulateOp]);
Romain Guyac670c02010-07-27 17:39:27 -0700625 }
626 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700627 if ((!description.hasGradient && !description.hasBitmap) || description.modulate) {
628 shader.append(gFS_Main_FetchColor);
629 }
Romain Guyac670c02010-07-27 17:39:27 -0700630 }
Chet Haase99585ad2011-05-02 15:00:16 -0700631 if (description.isAA) {
632 shader.append(gFS_Main_AccountForAA);
Chet Haase5b0200b2011-04-13 17:58:08 -0700633 }
Romain Guyac670c02010-07-27 17:39:27 -0700634 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700635 shader.append(gFS_Main_FetchGradient[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700636 }
637 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700638 if (description.isPoint) {
639 shader.append(gFS_Main_PointBitmapTexCoords);
640 }
Romain Guy889f8d12010-07-29 14:37:42 -0700641 if (!description.isBitmapNpot) {
642 shader.append(gFS_Main_FetchBitmap);
643 } else {
644 shader.append(gFS_Main_FetchBitmapNpot);
645 }
Romain Guyac670c02010-07-27 17:39:27 -0700646 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700647 bool applyModulate = false;
Romain Guyac670c02010-07-27 17:39:27 -0700648 // Case when we have two shaders set
649 if (description.hasGradient && description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700650 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
Romain Guyac670c02010-07-27 17:39:27 -0700651 if (description.isBitmapFirst) {
652 shader.append(gFS_Main_BlendShadersBG);
653 } else {
654 shader.append(gFS_Main_BlendShadersGB);
655 }
Romain Guy707b2f72010-10-11 16:34:59 -0700656 shader.append(gFS_Main_BlendShaders_Modulate[op]);
Romain Guy740bf2b2011-04-26 15:33:10 -0700657 applyModulate = true;
Romain Guy889f8d12010-07-29 14:37:42 -0700658 } else {
659 if (description.hasGradient) {
Romain Guy707b2f72010-10-11 16:34:59 -0700660 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
661 shader.append(gFS_Main_GradientShader_Modulate[op]);
Romain Guy740bf2b2011-04-26 15:33:10 -0700662 applyModulate = true;
Romain Guy889f8d12010-07-29 14:37:42 -0700663 } else if (description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700664 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
665 shader.append(gFS_Main_BitmapShader_Modulate[op]);
Romain Guy740bf2b2011-04-26 15:33:10 -0700666 applyModulate = true;
Romain Guy889f8d12010-07-29 14:37:42 -0700667 }
Romain Guyac670c02010-07-27 17:39:27 -0700668 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700669 if (description.modulate && applyModulate) {
Romain Guy41210632012-07-16 17:04:24 -0700670 if (description.hasGammaCorrection) {
671 shader.append(gFS_Main_ModulateColor_ApplyGamma);
672 } else {
673 shader.append(gFS_Main_ModulateColor);
674 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700675 }
Romain Guyac670c02010-07-27 17:39:27 -0700676 // Apply the color op if needed
677 shader.append(gFS_Main_ApplyColorOp[description.colorOp]);
678 // Output the fragment
Romain Guya5aed0d2010-09-09 14:42:43 -0700679 if (!blendFramebuffer) {
680 shader.append(gFS_Main_FragColor);
681 } else {
Romain Guyf607bdc2010-09-10 19:20:06 -0700682 shader.append(!description.swapSrcDst ?
683 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap);
Romain Guya5aed0d2010-09-09 14:42:43 -0700684 }
Romain Guyac670c02010-07-27 17:39:27 -0700685 }
686 // End the shader
687 shader.append(gFS_Footer);
688
Romain Guyc15008e2010-11-10 11:59:15 -0800689#if DEBUG_PROGRAMS
Romain Guydb1938e2010-08-02 18:50:22 -0700690 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
691 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800692#endif
Romain Guydb1938e2010-08-02 18:50:22 -0700693
Romain Guyac670c02010-07-27 17:39:27 -0700694 return shader;
695}
696
Romain Guy48daa542010-08-10 19:21:34 -0700697void ProgramCache::generateBlend(String8& shader, const char* name, SkXfermode::Mode mode) {
Romain Guyac670c02010-07-27 17:39:27 -0700698 shader.append("\nvec4 ");
699 shader.append(name);
700 shader.append("(vec4 src, vec4 dst) {\n");
701 shader.append(" ");
Romain Guy48daa542010-08-10 19:21:34 -0700702 shader.append(gBlendOps[mode]);
Romain Guyac670c02010-07-27 17:39:27 -0700703 shader.append("}\n");
704}
705
Romain Guy889f8d12010-07-29 14:37:42 -0700706void 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) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700718 case GL_CLAMP_TO_EDGE:
719 shader.append("texCoords.x");
720 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700721 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) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700730 case GL_CLAMP_TO_EDGE:
731 shader.append("texCoords.y");
732 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700733 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
Romain Guydb1938e2010-08-02 18:50:22 -0700744void 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
Romain Guyac670c02010-07-27 17:39:27 -0700758}; // namespace uirenderer
759}; // namespace android