blob: 80b1917809ca71b4324188a1bc048d13896ceaa5 [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
21#include "ProgramCache.h"
22
23namespace android {
24namespace uirenderer {
25
26///////////////////////////////////////////////////////////////////////////////
Romain Guy707b2f72010-10-11 16:34:59 -070027// Defines
28///////////////////////////////////////////////////////////////////////////////
29
30#define MODULATE_OP_NO_MODULATE 0
31#define MODULATE_OP_MODULATE 1
32#define MODULATE_OP_MODULATE_A8 2
33
34///////////////////////////////////////////////////////////////////////////////
Romain Guyac670c02010-07-27 17:39:27 -070035// Vertex shaders snippets
36///////////////////////////////////////////////////////////////////////////////
37
Romain Guyac670c02010-07-27 17:39:27 -070038const char* gVS_Header_Attributes =
39 "attribute vec4 position;\n";
40const char* gVS_Header_Attributes_TexCoords =
41 "attribute vec2 texCoords;\n";
Chet Haase5b0200b2011-04-13 17:58:08 -070042const char* gVS_Header_Attributes_Distance =
43 "attribute float vtxDistance;\n";
Romain Guyac670c02010-07-27 17:39:27 -070044const char* gVS_Header_Uniforms =
45 "uniform mat4 transform;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -070046const char* gVS_Header_Uniforms_IsPoint =
Romain Guy80bbfb12011-03-23 16:56:28 -070047 "uniform mediump float pointSize;\n";
Romain Guyee916f12010-09-20 17:53:08 -070048const char* gVS_Header_Uniforms_HasGradient[3] = {
49 // Linear
Romain Guyee916f12010-09-20 17:53:08 -070050 "uniform mat4 screenSpace;\n",
51 // Circular
Romain Guyddb80be2010-09-20 19:04:33 -070052 "uniform mat4 screenSpace;\n",
Romain Guyee916f12010-09-20 17:53:08 -070053 // Sweep
Romain Guyee916f12010-09-20 17:53:08 -070054 "uniform mat4 screenSpace;\n"
55};
Romain Guy889f8d12010-07-29 14:37:42 -070056const char* gVS_Header_Uniforms_HasBitmap =
57 "uniform mat4 textureTransform;\n"
Romain Guy80bbfb12011-03-23 16:56:28 -070058 "uniform mediump vec2 textureDimension;\n";
Romain Guyac670c02010-07-27 17:39:27 -070059const char* gVS_Header_Varyings_HasTexture =
60 "varying vec2 outTexCoords;\n";
Chet Haase5b0200b2011-04-13 17:58:08 -070061const char* gVS_Header_Varyings_HasWidth =
62 "varying float distance;\n";
Romain Guyac670c02010-07-27 17:39:27 -070063const char* gVS_Header_Varyings_HasBitmap =
64 "varying vec2 outBitmapTexCoords;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -070065const char* gVS_Header_Varyings_PointHasBitmap =
66 "varying vec2 outPointBitmapTexCoords;\n";
Romain Guyee916f12010-09-20 17:53:08 -070067const char* gVS_Header_Varyings_HasGradient[3] = {
68 // Linear
Romain Guy7537f852010-10-11 14:38:28 -070069 "varying vec2 linear;\n",
Romain Guyee916f12010-09-20 17:53:08 -070070 // Circular
Romain Guyddb80be2010-09-20 19:04:33 -070071 "varying vec2 circular;\n",
Romain Guyee916f12010-09-20 17:53:08 -070072 // Sweep
73 "varying vec2 sweep;\n"
74};
Romain Guyac670c02010-07-27 17:39:27 -070075const char* gVS_Main =
76 "\nvoid main(void) {\n";
77const char* gVS_Main_OutTexCoords =
78 " outTexCoords = texCoords;\n";
Romain Guyee916f12010-09-20 17:53:08 -070079const char* gVS_Main_OutGradient[3] = {
80 // Linear
Romain Guy7537f852010-10-11 14:38:28 -070081 " linear = vec2((screenSpace * position).x, 0.5);\n",
Romain Guyee916f12010-09-20 17:53:08 -070082 // Circular
Romain Guy14830942010-10-07 15:07:45 -070083 " circular = (screenSpace * position).xy;\n",
Romain Guyee916f12010-09-20 17:53:08 -070084 // Sweep
Romain Guy14830942010-10-07 15:07:45 -070085 " sweep = (screenSpace * position).xy;\n"
Romain Guyee916f12010-09-20 17:53:08 -070086};
Romain Guy889f8d12010-07-29 14:37:42 -070087const char* gVS_Main_OutBitmapTexCoords =
Romain Guy707b2f72010-10-11 16:34:59 -070088 " outBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -070089const char* gVS_Main_OutPointBitmapTexCoords =
90 " outPointBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
Romain Guyac670c02010-07-27 17:39:27 -070091const char* gVS_Main_Position =
92 " gl_Position = transform * position;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -070093const char* gVS_Main_PointSize =
94 " gl_PointSize = pointSize;\n";
Chet Haase5b0200b2011-04-13 17:58:08 -070095const char* gVS_Main_Width =
96 " distance = vtxDistance;\n";
Romain Guyac670c02010-07-27 17:39:27 -070097const char* gVS_Footer =
98 "}\n\n";
99
100///////////////////////////////////////////////////////////////////////////////
101// Fragment shaders snippets
102///////////////////////////////////////////////////////////////////////////////
103
Romain Guya5aed0d2010-09-09 14:42:43 -0700104const char* gFS_Header_Extension_FramebufferFetch =
105 "#extension GL_NV_shader_framebuffer_fetch : enable\n\n";
Romain Guyac670c02010-07-27 17:39:27 -0700106const char* gFS_Header =
107 "precision mediump float;\n\n";
108const char* gFS_Uniforms_Color =
109 "uniform vec4 color;\n";
Chet Haase5b0200b2011-04-13 17:58:08 -0700110const char* gFS_Uniforms_Width =
111 "uniform float width;\n"
112 "uniform float boundaryWidth;\n"
113 "uniform float inverseBoundaryWidth;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -0700114const char* gFS_Header_Uniforms_PointHasBitmap =
115 "uniform vec2 textureDimension;\n"
116 "uniform float pointSize;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700117const char* gFS_Uniforms_TextureSampler =
118 "uniform sampler2D sampler;\n";
Romain Guyee916f12010-09-20 17:53:08 -0700119const char* gFS_Uniforms_GradientSampler[3] = {
120 // Linear
121 "uniform sampler2D gradientSampler;\n",
122 // Circular
123 "uniform sampler2D gradientSampler;\n",
124 // Sweep
125 "uniform sampler2D gradientSampler;\n"
126};
Romain Guyac670c02010-07-27 17:39:27 -0700127const char* gFS_Uniforms_BitmapSampler =
128 "uniform sampler2D bitmapSampler;\n";
129const char* gFS_Uniforms_ColorOp[4] = {
130 // None
131 "",
132 // Matrix
133 "uniform mat4 colorMatrix;\n"
134 "uniform vec4 colorMatrixVector;\n",
135 // Lighting
Romain Guydb1938e2010-08-02 18:50:22 -0700136 "uniform vec4 lightingMul;\n"
137 "uniform vec4 lightingAdd;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700138 // PorterDuff
Romain Guydb1938e2010-08-02 18:50:22 -0700139 "uniform vec4 colorBlend;\n"
Romain Guyac670c02010-07-27 17:39:27 -0700140};
141const char* gFS_Main =
142 "\nvoid main(void) {\n"
Romain Guy7fbcc042010-08-04 15:40:07 -0700143 " lowp vec4 fragColor;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700144
Romain Guyed6fcb02011-03-21 13:11:28 -0700145const char* gFS_Main_PointBitmapTexCoords =
146 " vec2 outBitmapTexCoords = outPointBitmapTexCoords + "
147 "((gl_PointCoord - vec2(0.5, 0.5)) * textureDimension * vec2(pointSize, pointSize));\n";
148
Romain Guy707b2f72010-10-11 16:34:59 -0700149// Fast cases
150const char* gFS_Fast_SingleColor =
151 "\nvoid main(void) {\n"
152 " gl_FragColor = color;\n"
153 "}\n\n";
154const char* gFS_Fast_SingleTexture =
155 "\nvoid main(void) {\n"
156 " gl_FragColor = texture2D(sampler, outTexCoords);\n"
157 "}\n\n";
158const char* gFS_Fast_SingleModulateTexture =
159 "\nvoid main(void) {\n"
160 " gl_FragColor = color.a * texture2D(sampler, outTexCoords);\n"
161 "}\n\n";
162const char* gFS_Fast_SingleA8Texture =
163 "\nvoid main(void) {\n"
Romain Guy9db91242010-10-12 13:13:09 -0700164 " gl_FragColor = texture2D(sampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700165 "}\n\n";
166const char* gFS_Fast_SingleModulateA8Texture =
167 "\nvoid main(void) {\n"
168 " gl_FragColor = color * texture2D(sampler, outTexCoords).a;\n"
169 "}\n\n";
170const char* gFS_Fast_SingleGradient =
171 "\nvoid main(void) {\n"
172 " gl_FragColor = texture2D(gradientSampler, linear);\n"
173 "}\n\n";
174const char* gFS_Fast_SingleModulateGradient =
175 "\nvoid main(void) {\n"
176 " gl_FragColor = color.a * texture2D(gradientSampler, linear);\n"
177 "}\n\n";
178
179// General case
Romain Guyac670c02010-07-27 17:39:27 -0700180const char* gFS_Main_FetchColor =
181 " fragColor = color;\n";
Romain Guy740bf2b2011-04-26 15:33:10 -0700182const char* gFS_Main_ModulateColor =
183 " fragColor *= color.a;\n";
Chet Haase5b0200b2011-04-13 17:58:08 -0700184const char* gFS_Main_AccountForWidth =
185 " if (distance < boundaryWidth) {\n"
186 " fragColor *= (distance * inverseBoundaryWidth);\n"
187 " } else if (distance > (1.0 - boundaryWidth)) {\n"
188 " fragColor *= ((1.0 - distance) * inverseBoundaryWidth);\n"
189 " }\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700190const char* gFS_Main_FetchTexture[2] = {
191 // Don't modulate
192 " fragColor = texture2D(sampler, outTexCoords);\n",
193 // Modulate
194 " fragColor = color * texture2D(sampler, outTexCoords);\n"
195};
196const char* gFS_Main_FetchA8Texture[2] = {
197 // Don't modulate
Romain Guy9db91242010-10-12 13:13:09 -0700198 " fragColor = texture2D(sampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700199 // Modulate
200 " fragColor = color * texture2D(sampler, outTexCoords).a;\n"
201};
Romain Guyee916f12010-09-20 17:53:08 -0700202const char* gFS_Main_FetchGradient[3] = {
203 // Linear
Romain Guy7537f852010-10-11 14:38:28 -0700204 " vec4 gradientColor = texture2D(gradientSampler, linear);\n",
Romain Guyee916f12010-09-20 17:53:08 -0700205 // Circular
Romain Guy14830942010-10-07 15:07:45 -0700206 " float index = length(circular);\n"
Romain Guyddb80be2010-09-20 19:04:33 -0700207 " vec4 gradientColor = texture2D(gradientSampler, vec2(index, 0.5));\n",
Romain Guyee916f12010-09-20 17:53:08 -0700208 // Sweep
209 " float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
210 " vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n"
211};
Romain Guyac670c02010-07-27 17:39:27 -0700212const char* gFS_Main_FetchBitmap =
213 " vec4 bitmapColor = texture2D(bitmapSampler, outBitmapTexCoords);\n";
Romain Guy889f8d12010-07-29 14:37:42 -0700214const char* gFS_Main_FetchBitmapNpot =
215 " vec4 bitmapColor = texture2D(bitmapSampler, wrap(outBitmapTexCoords));\n";
Romain Guyac670c02010-07-27 17:39:27 -0700216const char* gFS_Main_BlendShadersBG =
Romain Guyac670c02010-07-27 17:39:27 -0700217 " fragColor = blendShaders(gradientColor, bitmapColor)";
Romain Guy06f96e22010-07-30 19:18:16 -0700218const char* gFS_Main_BlendShadersGB =
219 " fragColor = blendShaders(bitmapColor, gradientColor)";
Romain Guy707b2f72010-10-11 16:34:59 -0700220const char* gFS_Main_BlendShaders_Modulate[3] = {
221 // Don't modulate
222 ";\n",
223 // Modulate
224 " * fragColor.a;\n",
225 // Modulate with alpha 8 texture
226 " * texture2D(sampler, outTexCoords).a;\n"
227};
228const char* gFS_Main_GradientShader_Modulate[3] = {
229 // Don't modulate
230 " fragColor = gradientColor;\n",
231 // Modulate
232 " fragColor = gradientColor * fragColor.a;\n",
233 // Modulate with alpha 8 texture
234 " fragColor = gradientColor * texture2D(sampler, outTexCoords).a;\n"
235 };
236const char* gFS_Main_BitmapShader_Modulate[3] = {
237 // Don't modulate
238 " fragColor = bitmapColor;\n",
239 // Modulate
240 " fragColor = bitmapColor * fragColor.a;\n",
241 // Modulate with alpha 8 texture
242 " fragColor = bitmapColor * texture2D(sampler, outTexCoords).a;\n"
243 };
Romain Guyac670c02010-07-27 17:39:27 -0700244const char* gFS_Main_FragColor =
245 " gl_FragColor = fragColor;\n";
Romain Guya5aed0d2010-09-09 14:42:43 -0700246const char* gFS_Main_FragColor_Blend =
247 " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n";
Romain Guyf607bdc2010-09-10 19:20:06 -0700248const char* gFS_Main_FragColor_Blend_Swap =
249 " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n";
Romain Guyac670c02010-07-27 17:39:27 -0700250const char* gFS_Main_ApplyColorOp[4] = {
251 // None
252 "",
253 // Matrix
Romain Guydb1938e2010-08-02 18:50:22 -0700254 // TODO: Fix premultiplied alpha computations for color matrix
Romain Guyac670c02010-07-27 17:39:27 -0700255 " fragColor *= colorMatrix;\n"
Romain Guydb1938e2010-08-02 18:50:22 -0700256 " fragColor += colorMatrixVector;\n"
257 " fragColor.rgb *= fragColor.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700258 // Lighting
Romain Guydb1938e2010-08-02 18:50:22 -0700259 " float lightingAlpha = fragColor.a;\n"
260 " fragColor = min(fragColor * lightingMul + (lightingAdd * lightingAlpha), lightingAlpha);\n"
261 " fragColor.a = lightingAlpha;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700262 // PorterDuff
263 " fragColor = blendColors(colorBlend, fragColor);\n"
264};
265const char* gFS_Footer =
266 "}\n\n";
267
268///////////////////////////////////////////////////////////////////////////////
269// PorterDuff snippets
270///////////////////////////////////////////////////////////////////////////////
271
Romain Guy48daa542010-08-10 19:21:34 -0700272const char* gBlendOps[18] = {
Romain Guyac670c02010-07-27 17:39:27 -0700273 // Clear
274 "return vec4(0.0, 0.0, 0.0, 0.0);\n",
275 // Src
276 "return src;\n",
277 // Dst
278 "return dst;\n",
279 // SrcOver
Romain Guy06f96e22010-07-30 19:18:16 -0700280 "return src + dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700281 // DstOver
Romain Guy06f96e22010-07-30 19:18:16 -0700282 "return dst + src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700283 // SrcIn
Romain Guy06f96e22010-07-30 19:18:16 -0700284 "return src * dst.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700285 // DstIn
Romain Guy06f96e22010-07-30 19:18:16 -0700286 "return dst * src.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700287 // SrcOut
Romain Guy06f96e22010-07-30 19:18:16 -0700288 "return src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700289 // DstOut
Romain Guy06f96e22010-07-30 19:18:16 -0700290 "return dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700291 // SrcAtop
292 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n",
293 // DstAtop
294 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n",
295 // Xor
Romain Guy48daa542010-08-10 19:21:34 -0700296 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, "
Romain Guyac670c02010-07-27 17:39:27 -0700297 "src.a + dst.a - 2.0 * src.a * dst.a);\n",
Romain Guy48daa542010-08-10 19:21:34 -0700298 // Add
299 "return min(src + dst, 1.0);\n",
300 // Multiply
301 "return src * dst;\n",
302 // Screen
303 "return src + dst - src * dst;\n",
304 // Overlay
305 "return clamp(vec4(mix("
306 "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), "
307 "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), "
308 "step(dst.a, 2.0 * dst.rgb)), "
309 "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n",
310 // Darken
311 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
312 "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
313 // Lighten
314 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
315 "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 -0700316};
317
318///////////////////////////////////////////////////////////////////////////////
319// Constructors/destructors
320///////////////////////////////////////////////////////////////////////////////
321
322ProgramCache::ProgramCache() {
323}
324
325ProgramCache::~ProgramCache() {
326 clear();
327}
328
329///////////////////////////////////////////////////////////////////////////////
330// Cache management
331///////////////////////////////////////////////////////////////////////////////
332
333void ProgramCache::clear() {
Romain Guy67f27952010-12-07 20:09:23 -0800334 PROGRAM_LOGD("Clearing program cache");
335
Romain Guyac670c02010-07-27 17:39:27 -0700336 size_t count = mCache.size();
337 for (size_t i = 0; i < count; i++) {
338 delete mCache.valueAt(i);
339 }
340 mCache.clear();
341}
342
343Program* ProgramCache::get(const ProgramDescription& description) {
344 programid key = description.key();
345 ssize_t index = mCache.indexOfKey(key);
346 Program* program = NULL;
347 if (index < 0) {
Romain Guyee916f12010-09-20 17:53:08 -0700348 description.log("Could not find program");
Romain Guyac670c02010-07-27 17:39:27 -0700349 program = generateProgram(description, key);
350 mCache.add(key, program);
351 } else {
352 program = mCache.valueAt(index);
353 }
354 return program;
355}
356
357///////////////////////////////////////////////////////////////////////////////
358// Program generation
359///////////////////////////////////////////////////////////////////////////////
360
361Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) {
362 String8 vertexShader = generateVertexShader(description);
363 String8 fragmentShader = generateFragmentShader(description);
364
365 Program* program = new Program(vertexShader.string(), fragmentShader.string());
366 return program;
367}
368
369String8 ProgramCache::generateVertexShader(const ProgramDescription& description) {
370 // Add attributes
371 String8 shader(gVS_Header_Attributes);
Romain Guy889f8d12010-07-29 14:37:42 -0700372 if (description.hasTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700373 shader.append(gVS_Header_Attributes_TexCoords);
374 }
Chet Haase5b0200b2011-04-13 17:58:08 -0700375 if (description.hasWidth) {
376 shader.append(gVS_Header_Attributes_Distance);
377 }
Romain Guyac670c02010-07-27 17:39:27 -0700378 // Uniforms
379 shader.append(gVS_Header_Uniforms);
380 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700381 shader.append(gVS_Header_Uniforms_HasGradient[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700382 }
Romain Guy889f8d12010-07-29 14:37:42 -0700383 if (description.hasBitmap) {
384 shader.append(gVS_Header_Uniforms_HasBitmap);
385 }
Romain Guyed6fcb02011-03-21 13:11:28 -0700386 if (description.isPoint) {
387 shader.append(gVS_Header_Uniforms_IsPoint);
388 }
Romain Guyac670c02010-07-27 17:39:27 -0700389 // Varyings
390 if (description.hasTexture) {
391 shader.append(gVS_Header_Varyings_HasTexture);
392 }
Chet Haase5b0200b2011-04-13 17:58:08 -0700393 if (description.hasWidth) {
394 shader.append(gVS_Header_Varyings_HasWidth);
395 }
Romain Guyac670c02010-07-27 17:39:27 -0700396 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700397 shader.append(gVS_Header_Varyings_HasGradient[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700398 }
399 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700400 shader.append(description.isPoint ?
401 gVS_Header_Varyings_PointHasBitmap :
402 gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700403 }
404
405 // Begin the shader
406 shader.append(gVS_Main); {
407 if (description.hasTexture) {
408 shader.append(gVS_Main_OutTexCoords);
409 }
Chet Haase5b0200b2011-04-13 17:58:08 -0700410 if (description.hasWidth) {
411 shader.append(gVS_Main_Width);
412 }
Romain Guyac670c02010-07-27 17:39:27 -0700413 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700414 shader.append(gVS_Main_OutGradient[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700415 }
Romain Guy889f8d12010-07-29 14:37:42 -0700416 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700417 shader.append(description.isPoint ?
418 gVS_Main_OutPointBitmapTexCoords :
419 gVS_Main_OutBitmapTexCoords);
420 }
421 if (description.isPoint) {
422 shader.append(gVS_Main_PointSize);
Romain Guy889f8d12010-07-29 14:37:42 -0700423 }
Romain Guyac670c02010-07-27 17:39:27 -0700424 // Output transformed position
425 shader.append(gVS_Main_Position);
426 }
427 // End the shader
428 shader.append(gVS_Footer);
429
430 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string());
431
432 return shader;
433}
434
435String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) {
436 // Set the default precision
Romain Guya5aed0d2010-09-09 14:42:43 -0700437 String8 shader;
438
Romain Guy707b2f72010-10-11 16:34:59 -0700439 const bool blendFramebuffer = description.framebufferMode >= SkXfermode::kPlus_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700440 if (blendFramebuffer) {
441 shader.append(gFS_Header_Extension_FramebufferFetch);
442 }
443
444 shader.append(gFS_Header);
Romain Guyac670c02010-07-27 17:39:27 -0700445
446 // Varyings
447 if (description.hasTexture) {
448 shader.append(gVS_Header_Varyings_HasTexture);
449 }
Chet Haase5b0200b2011-04-13 17:58:08 -0700450 if (description.hasWidth) {
451 shader.append(gVS_Header_Varyings_HasWidth);
452 }
Romain Guyac670c02010-07-27 17:39:27 -0700453 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700454 shader.append(gVS_Header_Varyings_HasGradient[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700455 }
456 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700457 shader.append(description.isPoint ?
458 gVS_Header_Varyings_PointHasBitmap :
459 gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700460 }
461
Romain Guyac670c02010-07-27 17:39:27 -0700462 // Uniforms
Romain Guy707b2f72010-10-11 16:34:59 -0700463 int modulateOp = MODULATE_OP_NO_MODULATE;
464 const bool singleColor = !description.hasTexture &&
465 !description.hasGradient && !description.hasBitmap;
466
467 if (description.modulate || singleColor) {
468 shader.append(gFS_Uniforms_Color);
469 if (!singleColor) modulateOp = MODULATE_OP_MODULATE;
470 }
Romain Guyac670c02010-07-27 17:39:27 -0700471 if (description.hasTexture) {
472 shader.append(gFS_Uniforms_TextureSampler);
473 }
Chet Haase5b0200b2011-04-13 17:58:08 -0700474 if (description.hasWidth) {
475 shader.append(gFS_Uniforms_Width);
476 }
Romain Guyac670c02010-07-27 17:39:27 -0700477 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700478 shader.append(gFS_Uniforms_GradientSampler[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700479 }
Romain Guyed6fcb02011-03-21 13:11:28 -0700480 if (description.hasBitmap && description.isPoint) {
481 shader.append(gFS_Header_Uniforms_PointHasBitmap);
482 }
Romain Guy707b2f72010-10-11 16:34:59 -0700483
484 // Optimization for common cases
Chet Haase5b0200b2011-04-13 17:58:08 -0700485 if (!description.hasWidth && !blendFramebuffer &&
486 description.colorOp == ProgramDescription::kColorNone && !description.isPoint) {
Romain Guy707b2f72010-10-11 16:34:59 -0700487 bool fast = false;
488
489 const bool noShader = !description.hasGradient && !description.hasBitmap;
490 const bool singleTexture = description.hasTexture &&
491 !description.hasAlpha8Texture && noShader;
492 const bool singleA8Texture = description.hasTexture &&
493 description.hasAlpha8Texture && noShader;
494 const bool singleGradient = !description.hasTexture &&
495 description.hasGradient && !description.hasBitmap &&
496 description.gradientType == ProgramDescription::kGradientLinear;
497
498 if (singleColor) {
499 shader.append(gFS_Fast_SingleColor);
500 fast = true;
501 } else if (singleTexture) {
502 if (!description.modulate) {
503 shader.append(gFS_Fast_SingleTexture);
504 } else {
505 shader.append(gFS_Fast_SingleModulateTexture);
506 }
507 fast = true;
508 } else if (singleA8Texture) {
509 if (!description.modulate) {
510 shader.append(gFS_Fast_SingleA8Texture);
511 } else {
512 shader.append(gFS_Fast_SingleModulateA8Texture);
513 }
514 fast = true;
515 } else if (singleGradient) {
516 if (!description.modulate) {
517 shader.append(gFS_Fast_SingleGradient);
518 } else {
519 shader.append(gFS_Fast_SingleModulateGradient);
520 }
521 fast = true;
522 }
523
524 if (fast) {
Romain Guyc15008e2010-11-10 11:59:15 -0800525#if DEBUG_PROGRAMS
Romain Guy707b2f72010-10-11 16:34:59 -0700526 PROGRAM_LOGD("*** Fast case:\n");
527 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
528 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800529#endif
Romain Guy707b2f72010-10-11 16:34:59 -0700530
531 return shader;
532 }
533 }
534
Romain Guyac670c02010-07-27 17:39:27 -0700535 if (description.hasBitmap) {
536 shader.append(gFS_Uniforms_BitmapSampler);
537 }
538 shader.append(gFS_Uniforms_ColorOp[description.colorOp]);
539
540 // Generate required functions
541 if (description.hasGradient && description.hasBitmap) {
Romain Guy48daa542010-08-10 19:21:34 -0700542 generateBlend(shader, "blendShaders", description.shadersMode);
Romain Guyac670c02010-07-27 17:39:27 -0700543 }
544 if (description.colorOp == ProgramDescription::kColorBlend) {
Romain Guy48daa542010-08-10 19:21:34 -0700545 generateBlend(shader, "blendColors", description.colorMode);
Romain Guyac670c02010-07-27 17:39:27 -0700546 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700547 if (blendFramebuffer) {
548 generateBlend(shader, "blendFramebuffer", description.framebufferMode);
549 }
Romain Guy889f8d12010-07-29 14:37:42 -0700550 if (description.isBitmapNpot) {
551 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
552 }
Romain Guyac670c02010-07-27 17:39:27 -0700553
554 // Begin the shader
555 shader.append(gFS_Main); {
556 // Stores the result in fragColor directly
557 if (description.hasTexture) {
558 if (description.hasAlpha8Texture) {
Romain Guy707b2f72010-10-11 16:34:59 -0700559 if (!description.hasGradient && !description.hasBitmap) {
560 shader.append(gFS_Main_FetchA8Texture[modulateOp]);
561 }
Romain Guyac670c02010-07-27 17:39:27 -0700562 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700563 shader.append(gFS_Main_FetchTexture[modulateOp]);
Romain Guyac670c02010-07-27 17:39:27 -0700564 }
565 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700566 if ((!description.hasGradient && !description.hasBitmap) || description.modulate) {
567 shader.append(gFS_Main_FetchColor);
568 }
Romain Guyac670c02010-07-27 17:39:27 -0700569 }
Chet Haase5b0200b2011-04-13 17:58:08 -0700570 if (description.hasWidth) {
571 shader.append(gFS_Main_AccountForWidth);
572 }
Romain Guyac670c02010-07-27 17:39:27 -0700573 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700574 shader.append(gFS_Main_FetchGradient[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700575 }
576 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700577 if (description.isPoint) {
578 shader.append(gFS_Main_PointBitmapTexCoords);
579 }
Romain Guy889f8d12010-07-29 14:37:42 -0700580 if (!description.isBitmapNpot) {
581 shader.append(gFS_Main_FetchBitmap);
582 } else {
583 shader.append(gFS_Main_FetchBitmapNpot);
584 }
Romain Guyac670c02010-07-27 17:39:27 -0700585 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700586 bool applyModulate = false;
Romain Guyac670c02010-07-27 17:39:27 -0700587 // Case when we have two shaders set
588 if (description.hasGradient && description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700589 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
Romain Guyac670c02010-07-27 17:39:27 -0700590 if (description.isBitmapFirst) {
591 shader.append(gFS_Main_BlendShadersBG);
592 } else {
593 shader.append(gFS_Main_BlendShadersGB);
594 }
Romain Guy707b2f72010-10-11 16:34:59 -0700595 shader.append(gFS_Main_BlendShaders_Modulate[op]);
Romain Guy740bf2b2011-04-26 15:33:10 -0700596 applyModulate = true;
Romain Guy889f8d12010-07-29 14:37:42 -0700597 } else {
598 if (description.hasGradient) {
Romain Guy707b2f72010-10-11 16:34:59 -0700599 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
600 shader.append(gFS_Main_GradientShader_Modulate[op]);
Romain Guy740bf2b2011-04-26 15:33:10 -0700601 applyModulate = true;
Romain Guy889f8d12010-07-29 14:37:42 -0700602 } else if (description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700603 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
604 shader.append(gFS_Main_BitmapShader_Modulate[op]);
Romain Guy740bf2b2011-04-26 15:33:10 -0700605 applyModulate = true;
Romain Guy889f8d12010-07-29 14:37:42 -0700606 }
Romain Guyac670c02010-07-27 17:39:27 -0700607 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700608 if (description.modulate && applyModulate) {
609 shader.append(gFS_Main_ModulateColor);
610 }
Romain Guyac670c02010-07-27 17:39:27 -0700611 // Apply the color op if needed
612 shader.append(gFS_Main_ApplyColorOp[description.colorOp]);
613 // Output the fragment
Romain Guya5aed0d2010-09-09 14:42:43 -0700614 if (!blendFramebuffer) {
615 shader.append(gFS_Main_FragColor);
616 } else {
Romain Guyf607bdc2010-09-10 19:20:06 -0700617 shader.append(!description.swapSrcDst ?
618 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap);
Romain Guya5aed0d2010-09-09 14:42:43 -0700619 }
Romain Guyac670c02010-07-27 17:39:27 -0700620 }
621 // End the shader
622 shader.append(gFS_Footer);
623
Romain Guyc15008e2010-11-10 11:59:15 -0800624#if DEBUG_PROGRAMS
Romain Guydb1938e2010-08-02 18:50:22 -0700625 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
626 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800627#endif
Romain Guydb1938e2010-08-02 18:50:22 -0700628
Romain Guyac670c02010-07-27 17:39:27 -0700629 return shader;
630}
631
Romain Guy48daa542010-08-10 19:21:34 -0700632void ProgramCache::generateBlend(String8& shader, const char* name, SkXfermode::Mode mode) {
Romain Guyac670c02010-07-27 17:39:27 -0700633 shader.append("\nvec4 ");
634 shader.append(name);
635 shader.append("(vec4 src, vec4 dst) {\n");
636 shader.append(" ");
Romain Guy48daa542010-08-10 19:21:34 -0700637 shader.append(gBlendOps[mode]);
Romain Guyac670c02010-07-27 17:39:27 -0700638 shader.append("}\n");
639}
640
Romain Guy889f8d12010-07-29 14:37:42 -0700641void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) {
642 shader.append("\nvec2 wrap(vec2 texCoords) {\n");
643 if (wrapS == GL_MIRRORED_REPEAT) {
644 shader.append(" float xMod2 = mod(texCoords.x, 2.0);\n");
645 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n");
646 }
647 if (wrapT == GL_MIRRORED_REPEAT) {
648 shader.append(" float yMod2 = mod(texCoords.y, 2.0);\n");
649 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n");
650 }
651 shader.append(" return vec2(");
652 switch (wrapS) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700653 case GL_CLAMP_TO_EDGE:
654 shader.append("texCoords.x");
655 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700656 case GL_REPEAT:
657 shader.append("mod(texCoords.x, 1.0)");
658 break;
659 case GL_MIRRORED_REPEAT:
660 shader.append("xMod2");
661 break;
662 }
663 shader.append(", ");
664 switch (wrapT) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700665 case GL_CLAMP_TO_EDGE:
666 shader.append("texCoords.y");
667 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700668 case GL_REPEAT:
669 shader.append("mod(texCoords.y, 1.0)");
670 break;
671 case GL_MIRRORED_REPEAT:
672 shader.append("yMod2");
673 break;
674 }
675 shader.append(");\n");
676 shader.append("}\n");
677}
678
Romain Guydb1938e2010-08-02 18:50:22 -0700679void ProgramCache::printLongString(const String8& shader) const {
680 ssize_t index = 0;
681 ssize_t lastIndex = 0;
682 const char* str = shader.string();
683 while ((index = shader.find("\n", index)) > -1) {
684 String8 line(str, index - lastIndex);
685 if (line.length() == 0) line.append("\n");
686 PROGRAM_LOGD("%s", line.string());
687 index++;
688 str += (index - lastIndex);
689 lastIndex = index;
690 }
691}
692
Romain Guyac670c02010-07-27 17:39:27 -0700693}; // namespace uirenderer
694}; // namespace android