blob: b873bb82091903641f6646b4048afbab3b48c3af [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";
Chet Haase5b0200b2011-04-13 17:58:08 -0700182const char* gFS_Main_AccountForWidth =
183 " if (distance < boundaryWidth) {\n"
184 " fragColor *= (distance * inverseBoundaryWidth);\n"
185 " } else if (distance > (1.0 - boundaryWidth)) {\n"
186 " fragColor *= ((1.0 - distance) * inverseBoundaryWidth);\n"
187 " }\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700188const char* gFS_Main_FetchTexture[2] = {
189 // Don't modulate
190 " fragColor = texture2D(sampler, outTexCoords);\n",
191 // Modulate
192 " fragColor = color * texture2D(sampler, outTexCoords);\n"
193};
194const char* gFS_Main_FetchA8Texture[2] = {
195 // Don't modulate
Romain Guy9db91242010-10-12 13:13:09 -0700196 " fragColor = texture2D(sampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700197 // Modulate
198 " fragColor = color * texture2D(sampler, outTexCoords).a;\n"
199};
Romain Guyee916f12010-09-20 17:53:08 -0700200const char* gFS_Main_FetchGradient[3] = {
201 // Linear
Romain Guy7537f852010-10-11 14:38:28 -0700202 " vec4 gradientColor = texture2D(gradientSampler, linear);\n",
Romain Guyee916f12010-09-20 17:53:08 -0700203 // Circular
Romain Guy14830942010-10-07 15:07:45 -0700204 " float index = length(circular);\n"
Romain Guyddb80be2010-09-20 19:04:33 -0700205 " vec4 gradientColor = texture2D(gradientSampler, vec2(index, 0.5));\n",
Romain Guyee916f12010-09-20 17:53:08 -0700206 // Sweep
207 " float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
208 " vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n"
209};
Romain Guyac670c02010-07-27 17:39:27 -0700210const char* gFS_Main_FetchBitmap =
211 " vec4 bitmapColor = texture2D(bitmapSampler, outBitmapTexCoords);\n";
Romain Guy889f8d12010-07-29 14:37:42 -0700212const char* gFS_Main_FetchBitmapNpot =
213 " vec4 bitmapColor = texture2D(bitmapSampler, wrap(outBitmapTexCoords));\n";
Romain Guyac670c02010-07-27 17:39:27 -0700214const char* gFS_Main_BlendShadersBG =
Romain Guyac670c02010-07-27 17:39:27 -0700215 " fragColor = blendShaders(gradientColor, bitmapColor)";
Romain Guy06f96e22010-07-30 19:18:16 -0700216const char* gFS_Main_BlendShadersGB =
217 " fragColor = blendShaders(bitmapColor, gradientColor)";
Romain Guy707b2f72010-10-11 16:34:59 -0700218const char* gFS_Main_BlendShaders_Modulate[3] = {
219 // Don't modulate
220 ";\n",
221 // Modulate
222 " * fragColor.a;\n",
223 // Modulate with alpha 8 texture
224 " * texture2D(sampler, outTexCoords).a;\n"
225};
226const char* gFS_Main_GradientShader_Modulate[3] = {
227 // Don't modulate
228 " fragColor = gradientColor;\n",
229 // Modulate
230 " fragColor = gradientColor * fragColor.a;\n",
231 // Modulate with alpha 8 texture
232 " fragColor = gradientColor * texture2D(sampler, outTexCoords).a;\n"
233 };
234const char* gFS_Main_BitmapShader_Modulate[3] = {
235 // Don't modulate
236 " fragColor = bitmapColor;\n",
237 // Modulate
238 " fragColor = bitmapColor * fragColor.a;\n",
239 // Modulate with alpha 8 texture
240 " fragColor = bitmapColor * texture2D(sampler, outTexCoords).a;\n"
241 };
Romain Guyac670c02010-07-27 17:39:27 -0700242const char* gFS_Main_FragColor =
243 " gl_FragColor = fragColor;\n";
Romain Guya5aed0d2010-09-09 14:42:43 -0700244const char* gFS_Main_FragColor_Blend =
245 " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n";
Romain Guyf607bdc2010-09-10 19:20:06 -0700246const char* gFS_Main_FragColor_Blend_Swap =
247 " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n";
Romain Guyac670c02010-07-27 17:39:27 -0700248const char* gFS_Main_ApplyColorOp[4] = {
249 // None
250 "",
251 // Matrix
Romain Guydb1938e2010-08-02 18:50:22 -0700252 // TODO: Fix premultiplied alpha computations for color matrix
Romain Guyac670c02010-07-27 17:39:27 -0700253 " fragColor *= colorMatrix;\n"
Romain Guydb1938e2010-08-02 18:50:22 -0700254 " fragColor += colorMatrixVector;\n"
255 " fragColor.rgb *= fragColor.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700256 // Lighting
Romain Guydb1938e2010-08-02 18:50:22 -0700257 " float lightingAlpha = fragColor.a;\n"
258 " fragColor = min(fragColor * lightingMul + (lightingAdd * lightingAlpha), lightingAlpha);\n"
259 " fragColor.a = lightingAlpha;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700260 // PorterDuff
261 " fragColor = blendColors(colorBlend, fragColor);\n"
262};
263const char* gFS_Footer =
264 "}\n\n";
265
266///////////////////////////////////////////////////////////////////////////////
267// PorterDuff snippets
268///////////////////////////////////////////////////////////////////////////////
269
Romain Guy48daa542010-08-10 19:21:34 -0700270const char* gBlendOps[18] = {
Romain Guyac670c02010-07-27 17:39:27 -0700271 // Clear
272 "return vec4(0.0, 0.0, 0.0, 0.0);\n",
273 // Src
274 "return src;\n",
275 // Dst
276 "return dst;\n",
277 // SrcOver
Romain Guy06f96e22010-07-30 19:18:16 -0700278 "return src + dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700279 // DstOver
Romain Guy06f96e22010-07-30 19:18:16 -0700280 "return dst + src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700281 // SrcIn
Romain Guy06f96e22010-07-30 19:18:16 -0700282 "return src * dst.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700283 // DstIn
Romain Guy06f96e22010-07-30 19:18:16 -0700284 "return dst * src.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700285 // SrcOut
Romain Guy06f96e22010-07-30 19:18:16 -0700286 "return src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700287 // DstOut
Romain Guy06f96e22010-07-30 19:18:16 -0700288 "return dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700289 // SrcAtop
290 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n",
291 // DstAtop
292 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n",
293 // Xor
Romain Guy48daa542010-08-10 19:21:34 -0700294 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, "
Romain Guyac670c02010-07-27 17:39:27 -0700295 "src.a + dst.a - 2.0 * src.a * dst.a);\n",
Romain Guy48daa542010-08-10 19:21:34 -0700296 // Add
297 "return min(src + dst, 1.0);\n",
298 // Multiply
299 "return src * dst;\n",
300 // Screen
301 "return src + dst - src * dst;\n",
302 // Overlay
303 "return clamp(vec4(mix("
304 "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), "
305 "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), "
306 "step(dst.a, 2.0 * dst.rgb)), "
307 "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n",
308 // Darken
309 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
310 "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
311 // Lighten
312 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
313 "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 -0700314};
315
316///////////////////////////////////////////////////////////////////////////////
317// Constructors/destructors
318///////////////////////////////////////////////////////////////////////////////
319
320ProgramCache::ProgramCache() {
321}
322
323ProgramCache::~ProgramCache() {
324 clear();
325}
326
327///////////////////////////////////////////////////////////////////////////////
328// Cache management
329///////////////////////////////////////////////////////////////////////////////
330
331void ProgramCache::clear() {
Romain Guy67f27952010-12-07 20:09:23 -0800332 PROGRAM_LOGD("Clearing program cache");
333
Romain Guyac670c02010-07-27 17:39:27 -0700334 size_t count = mCache.size();
335 for (size_t i = 0; i < count; i++) {
336 delete mCache.valueAt(i);
337 }
338 mCache.clear();
339}
340
341Program* ProgramCache::get(const ProgramDescription& description) {
342 programid key = description.key();
343 ssize_t index = mCache.indexOfKey(key);
344 Program* program = NULL;
345 if (index < 0) {
Romain Guyee916f12010-09-20 17:53:08 -0700346 description.log("Could not find program");
Romain Guyac670c02010-07-27 17:39:27 -0700347 program = generateProgram(description, key);
348 mCache.add(key, program);
349 } else {
350 program = mCache.valueAt(index);
351 }
352 return program;
353}
354
355///////////////////////////////////////////////////////////////////////////////
356// Program generation
357///////////////////////////////////////////////////////////////////////////////
358
359Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) {
360 String8 vertexShader = generateVertexShader(description);
361 String8 fragmentShader = generateFragmentShader(description);
362
363 Program* program = new Program(vertexShader.string(), fragmentShader.string());
364 return program;
365}
366
367String8 ProgramCache::generateVertexShader(const ProgramDescription& description) {
368 // Add attributes
369 String8 shader(gVS_Header_Attributes);
Romain Guy889f8d12010-07-29 14:37:42 -0700370 if (description.hasTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700371 shader.append(gVS_Header_Attributes_TexCoords);
372 }
Chet Haase5b0200b2011-04-13 17:58:08 -0700373 if (description.hasWidth) {
374 shader.append(gVS_Header_Attributes_Distance);
375 }
Romain Guyac670c02010-07-27 17:39:27 -0700376 // Uniforms
377 shader.append(gVS_Header_Uniforms);
378 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700379 shader.append(gVS_Header_Uniforms_HasGradient[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700380 }
Romain Guy889f8d12010-07-29 14:37:42 -0700381 if (description.hasBitmap) {
382 shader.append(gVS_Header_Uniforms_HasBitmap);
383 }
Romain Guyed6fcb02011-03-21 13:11:28 -0700384 if (description.isPoint) {
385 shader.append(gVS_Header_Uniforms_IsPoint);
386 }
Romain Guyac670c02010-07-27 17:39:27 -0700387 // Varyings
388 if (description.hasTexture) {
389 shader.append(gVS_Header_Varyings_HasTexture);
390 }
Chet Haase5b0200b2011-04-13 17:58:08 -0700391 if (description.hasWidth) {
392 shader.append(gVS_Header_Varyings_HasWidth);
393 }
Romain Guyac670c02010-07-27 17:39:27 -0700394 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700395 shader.append(gVS_Header_Varyings_HasGradient[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700396 }
397 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700398 shader.append(description.isPoint ?
399 gVS_Header_Varyings_PointHasBitmap :
400 gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700401 }
402
403 // Begin the shader
404 shader.append(gVS_Main); {
405 if (description.hasTexture) {
406 shader.append(gVS_Main_OutTexCoords);
407 }
Chet Haase5b0200b2011-04-13 17:58:08 -0700408 if (description.hasWidth) {
409 shader.append(gVS_Main_Width);
410 }
Romain Guyac670c02010-07-27 17:39:27 -0700411 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700412 shader.append(gVS_Main_OutGradient[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700413 }
Romain Guy889f8d12010-07-29 14:37:42 -0700414 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700415 shader.append(description.isPoint ?
416 gVS_Main_OutPointBitmapTexCoords :
417 gVS_Main_OutBitmapTexCoords);
418 }
419 if (description.isPoint) {
420 shader.append(gVS_Main_PointSize);
Romain Guy889f8d12010-07-29 14:37:42 -0700421 }
Romain Guyac670c02010-07-27 17:39:27 -0700422 // Output transformed position
423 shader.append(gVS_Main_Position);
424 }
425 // End the shader
426 shader.append(gVS_Footer);
427
428 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string());
429
430 return shader;
431}
432
433String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) {
434 // Set the default precision
Romain Guya5aed0d2010-09-09 14:42:43 -0700435 String8 shader;
436
Romain Guy707b2f72010-10-11 16:34:59 -0700437 const bool blendFramebuffer = description.framebufferMode >= SkXfermode::kPlus_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700438 if (blendFramebuffer) {
439 shader.append(gFS_Header_Extension_FramebufferFetch);
440 }
441
442 shader.append(gFS_Header);
Romain Guyac670c02010-07-27 17:39:27 -0700443
444 // Varyings
445 if (description.hasTexture) {
446 shader.append(gVS_Header_Varyings_HasTexture);
447 }
Chet Haase5b0200b2011-04-13 17:58:08 -0700448 if (description.hasWidth) {
449 shader.append(gVS_Header_Varyings_HasWidth);
450 }
Romain Guyac670c02010-07-27 17:39:27 -0700451 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700452 shader.append(gVS_Header_Varyings_HasGradient[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700453 }
454 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700455 shader.append(description.isPoint ?
456 gVS_Header_Varyings_PointHasBitmap :
457 gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700458 }
459
Romain Guyac670c02010-07-27 17:39:27 -0700460 // Uniforms
Romain Guy707b2f72010-10-11 16:34:59 -0700461 int modulateOp = MODULATE_OP_NO_MODULATE;
462 const bool singleColor = !description.hasTexture &&
463 !description.hasGradient && !description.hasBitmap;
464
465 if (description.modulate || singleColor) {
466 shader.append(gFS_Uniforms_Color);
467 if (!singleColor) modulateOp = MODULATE_OP_MODULATE;
468 }
Romain Guyac670c02010-07-27 17:39:27 -0700469 if (description.hasTexture) {
470 shader.append(gFS_Uniforms_TextureSampler);
471 }
Chet Haase5b0200b2011-04-13 17:58:08 -0700472 if (description.hasWidth) {
473 shader.append(gFS_Uniforms_Width);
474 }
Romain Guyac670c02010-07-27 17:39:27 -0700475 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700476 shader.append(gFS_Uniforms_GradientSampler[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700477 }
Romain Guyed6fcb02011-03-21 13:11:28 -0700478 if (description.hasBitmap && description.isPoint) {
479 shader.append(gFS_Header_Uniforms_PointHasBitmap);
480 }
Romain Guy707b2f72010-10-11 16:34:59 -0700481
482 // Optimization for common cases
Chet Haase5b0200b2011-04-13 17:58:08 -0700483 if (!description.hasWidth && !blendFramebuffer &&
484 description.colorOp == ProgramDescription::kColorNone && !description.isPoint) {
Romain Guy707b2f72010-10-11 16:34:59 -0700485 bool fast = false;
486
487 const bool noShader = !description.hasGradient && !description.hasBitmap;
488 const bool singleTexture = description.hasTexture &&
489 !description.hasAlpha8Texture && noShader;
490 const bool singleA8Texture = description.hasTexture &&
491 description.hasAlpha8Texture && noShader;
492 const bool singleGradient = !description.hasTexture &&
493 description.hasGradient && !description.hasBitmap &&
494 description.gradientType == ProgramDescription::kGradientLinear;
495
496 if (singleColor) {
497 shader.append(gFS_Fast_SingleColor);
498 fast = true;
499 } else if (singleTexture) {
500 if (!description.modulate) {
501 shader.append(gFS_Fast_SingleTexture);
502 } else {
503 shader.append(gFS_Fast_SingleModulateTexture);
504 }
505 fast = true;
506 } else if (singleA8Texture) {
507 if (!description.modulate) {
508 shader.append(gFS_Fast_SingleA8Texture);
509 } else {
510 shader.append(gFS_Fast_SingleModulateA8Texture);
511 }
512 fast = true;
513 } else if (singleGradient) {
514 if (!description.modulate) {
515 shader.append(gFS_Fast_SingleGradient);
516 } else {
517 shader.append(gFS_Fast_SingleModulateGradient);
518 }
519 fast = true;
520 }
521
522 if (fast) {
Romain Guyc15008e2010-11-10 11:59:15 -0800523#if DEBUG_PROGRAMS
Romain Guy707b2f72010-10-11 16:34:59 -0700524 PROGRAM_LOGD("*** Fast case:\n");
525 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
526 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800527#endif
Romain Guy707b2f72010-10-11 16:34:59 -0700528
529 return shader;
530 }
531 }
532
Romain Guyac670c02010-07-27 17:39:27 -0700533 if (description.hasBitmap) {
534 shader.append(gFS_Uniforms_BitmapSampler);
535 }
536 shader.append(gFS_Uniforms_ColorOp[description.colorOp]);
537
538 // Generate required functions
539 if (description.hasGradient && description.hasBitmap) {
Romain Guy48daa542010-08-10 19:21:34 -0700540 generateBlend(shader, "blendShaders", description.shadersMode);
Romain Guyac670c02010-07-27 17:39:27 -0700541 }
542 if (description.colorOp == ProgramDescription::kColorBlend) {
Romain Guy48daa542010-08-10 19:21:34 -0700543 generateBlend(shader, "blendColors", description.colorMode);
Romain Guyac670c02010-07-27 17:39:27 -0700544 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700545 if (blendFramebuffer) {
546 generateBlend(shader, "blendFramebuffer", description.framebufferMode);
547 }
Romain Guy889f8d12010-07-29 14:37:42 -0700548 if (description.isBitmapNpot) {
549 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
550 }
Romain Guyac670c02010-07-27 17:39:27 -0700551
552 // Begin the shader
553 shader.append(gFS_Main); {
554 // Stores the result in fragColor directly
555 if (description.hasTexture) {
556 if (description.hasAlpha8Texture) {
Romain Guy707b2f72010-10-11 16:34:59 -0700557 if (!description.hasGradient && !description.hasBitmap) {
558 shader.append(gFS_Main_FetchA8Texture[modulateOp]);
559 }
Romain Guyac670c02010-07-27 17:39:27 -0700560 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700561 shader.append(gFS_Main_FetchTexture[modulateOp]);
Romain Guyac670c02010-07-27 17:39:27 -0700562 }
563 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700564 if ((!description.hasGradient && !description.hasBitmap) || description.modulate) {
565 shader.append(gFS_Main_FetchColor);
566 }
Romain Guyac670c02010-07-27 17:39:27 -0700567 }
Chet Haase5b0200b2011-04-13 17:58:08 -0700568 if (description.hasWidth) {
569 shader.append(gFS_Main_AccountForWidth);
570 }
Romain Guyac670c02010-07-27 17:39:27 -0700571 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700572 shader.append(gFS_Main_FetchGradient[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700573 }
574 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700575 if (description.isPoint) {
576 shader.append(gFS_Main_PointBitmapTexCoords);
577 }
Romain Guy889f8d12010-07-29 14:37:42 -0700578 if (!description.isBitmapNpot) {
579 shader.append(gFS_Main_FetchBitmap);
580 } else {
581 shader.append(gFS_Main_FetchBitmapNpot);
582 }
Romain Guyac670c02010-07-27 17:39:27 -0700583 }
584 // Case when we have two shaders set
585 if (description.hasGradient && description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700586 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
Romain Guyac670c02010-07-27 17:39:27 -0700587 if (description.isBitmapFirst) {
588 shader.append(gFS_Main_BlendShadersBG);
589 } else {
590 shader.append(gFS_Main_BlendShadersGB);
591 }
Romain Guy707b2f72010-10-11 16:34:59 -0700592 shader.append(gFS_Main_BlendShaders_Modulate[op]);
Romain Guy889f8d12010-07-29 14:37:42 -0700593 } else {
594 if (description.hasGradient) {
Romain Guy707b2f72010-10-11 16:34:59 -0700595 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
596 shader.append(gFS_Main_GradientShader_Modulate[op]);
Romain Guy889f8d12010-07-29 14:37:42 -0700597 } else if (description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700598 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
599 shader.append(gFS_Main_BitmapShader_Modulate[op]);
Romain Guy889f8d12010-07-29 14:37:42 -0700600 }
Romain Guyac670c02010-07-27 17:39:27 -0700601 }
602 // Apply the color op if needed
603 shader.append(gFS_Main_ApplyColorOp[description.colorOp]);
604 // Output the fragment
Romain Guya5aed0d2010-09-09 14:42:43 -0700605 if (!blendFramebuffer) {
606 shader.append(gFS_Main_FragColor);
607 } else {
Romain Guyf607bdc2010-09-10 19:20:06 -0700608 shader.append(!description.swapSrcDst ?
609 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap);
Romain Guya5aed0d2010-09-09 14:42:43 -0700610 }
Romain Guyac670c02010-07-27 17:39:27 -0700611 }
612 // End the shader
613 shader.append(gFS_Footer);
614
Romain Guyc15008e2010-11-10 11:59:15 -0800615#if DEBUG_PROGRAMS
Romain Guydb1938e2010-08-02 18:50:22 -0700616 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
617 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800618#endif
Romain Guydb1938e2010-08-02 18:50:22 -0700619
Romain Guyac670c02010-07-27 17:39:27 -0700620 return shader;
621}
622
Romain Guy48daa542010-08-10 19:21:34 -0700623void ProgramCache::generateBlend(String8& shader, const char* name, SkXfermode::Mode mode) {
Romain Guyac670c02010-07-27 17:39:27 -0700624 shader.append("\nvec4 ");
625 shader.append(name);
626 shader.append("(vec4 src, vec4 dst) {\n");
627 shader.append(" ");
Romain Guy48daa542010-08-10 19:21:34 -0700628 shader.append(gBlendOps[mode]);
Romain Guyac670c02010-07-27 17:39:27 -0700629 shader.append("}\n");
630}
631
Romain Guy889f8d12010-07-29 14:37:42 -0700632void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) {
633 shader.append("\nvec2 wrap(vec2 texCoords) {\n");
634 if (wrapS == GL_MIRRORED_REPEAT) {
635 shader.append(" float xMod2 = mod(texCoords.x, 2.0);\n");
636 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n");
637 }
638 if (wrapT == GL_MIRRORED_REPEAT) {
639 shader.append(" float yMod2 = mod(texCoords.y, 2.0);\n");
640 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n");
641 }
642 shader.append(" return vec2(");
643 switch (wrapS) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700644 case GL_CLAMP_TO_EDGE:
645 shader.append("texCoords.x");
646 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700647 case GL_REPEAT:
648 shader.append("mod(texCoords.x, 1.0)");
649 break;
650 case GL_MIRRORED_REPEAT:
651 shader.append("xMod2");
652 break;
653 }
654 shader.append(", ");
655 switch (wrapT) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700656 case GL_CLAMP_TO_EDGE:
657 shader.append("texCoords.y");
658 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700659 case GL_REPEAT:
660 shader.append("mod(texCoords.y, 1.0)");
661 break;
662 case GL_MIRRORED_REPEAT:
663 shader.append("yMod2");
664 break;
665 }
666 shader.append(");\n");
667 shader.append("}\n");
668}
669
Romain Guydb1938e2010-08-02 18:50:22 -0700670void ProgramCache::printLongString(const String8& shader) const {
671 ssize_t index = 0;
672 ssize_t lastIndex = 0;
673 const char* str = shader.string();
674 while ((index = shader.find("\n", index)) > -1) {
675 String8 line(str, index - lastIndex);
676 if (line.length() == 0) line.append("\n");
677 PROGRAM_LOGD("%s", line.string());
678 index++;
679 str += (index - lastIndex);
680 lastIndex = index;
681 }
682}
683
Romain Guyac670c02010-07-27 17:39:27 -0700684}; // namespace uirenderer
685}; // namespace android