2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "OpenGLRenderer"
19 #include <utils/String8.h>
23 #include "ProgramCache.h"
26 namespace uirenderer {
28 ///////////////////////////////////////////////////////////////////////////////
30 ///////////////////////////////////////////////////////////////////////////////
32 #define MODULATE_OP_NO_MODULATE 0
33 #define MODULATE_OP_MODULATE 1
34 #define MODULATE_OP_MODULATE_A8 2
36 #define STR(x) STR1(x)
39 ///////////////////////////////////////////////////////////////////////////////
40 // Vertex shaders snippets
41 ///////////////////////////////////////////////////////////////////////////////
43 const char* gVS_Header_Attributes =
44 "attribute vec4 position;\n";
45 const char* gVS_Header_Attributes_TexCoords =
46 "attribute vec2 texCoords;\n";
47 const char* gVS_Header_Attributes_Colors =
48 "attribute vec4 colors;\n";
49 const char* gVS_Header_Attributes_VertexAlphaParameters =
50 "attribute float vtxAlpha;\n";
51 const char* gVS_Header_Uniforms_TextureTransform =
52 "uniform mat4 mainTextureTransform;\n";
53 const char* gVS_Header_Uniforms =
54 "uniform mat4 projection;\n" \
55 "uniform mat4 transform;\n";
56 const char* gVS_Header_Uniforms_HasGradient =
57 "uniform mat4 screenSpace;\n";
58 const char* gVS_Header_Uniforms_HasBitmap =
59 "uniform mat4 textureTransform;\n"
60 "uniform mediump vec2 textureDimension;\n";
61 const char* gVS_Header_Uniforms_HasRoundRectClip =
62 "uniform mat4 roundRectInvTransform;\n";
63 const char* gVS_Header_Varyings_HasTexture =
64 "varying vec2 outTexCoords;\n";
65 const char* gVS_Header_Varyings_HasColors =
66 "varying vec4 outColors;\n";
67 const char* gVS_Header_Varyings_HasVertexAlpha =
68 "varying float alpha;\n";
69 const char* gVS_Header_Varyings_HasBitmap =
70 "varying highp vec2 outBitmapTexCoords;\n";
71 const char* gVS_Header_Varyings_HasGradient[6] = {
73 "varying highp vec2 linear;\n"
74 "varying vec2 ditherTexCoords;\n",
75 "varying float linear;\n"
76 "varying vec2 ditherTexCoords;\n",
79 "varying highp vec2 circular;\n"
80 "varying vec2 ditherTexCoords;\n",
81 "varying highp vec2 circular;\n"
82 "varying vec2 ditherTexCoords;\n",
85 "varying highp vec2 sweep;\n"
86 "varying vec2 ditherTexCoords;\n",
87 "varying highp vec2 sweep;\n"
88 "varying vec2 ditherTexCoords;\n",
90 const char* gVS_Header_Varyings_HasRoundRectClip =
91 "varying highp vec2 roundRectPos;\n";
92 const char* gVS_Main =
93 "\nvoid main(void) {\n";
94 const char* gVS_Main_OutTexCoords =
95 " outTexCoords = texCoords;\n";
96 const char* gVS_Main_OutColors =
97 " outColors = colors;\n";
98 const char* gVS_Main_OutTransformedTexCoords =
99 " outTexCoords = (mainTextureTransform * vec4(texCoords, 0.0, 1.0)).xy;\n";
100 const char* gVS_Main_OutGradient[6] = {
102 " linear = vec2((screenSpace * position).x, 0.5);\n"
103 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
104 " linear = (screenSpace * position).x;\n"
105 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
108 " circular = (screenSpace * position).xy;\n"
109 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
110 " circular = (screenSpace * position).xy;\n"
111 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
114 " sweep = (screenSpace * position).xy;\n"
115 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
116 " sweep = (screenSpace * position).xy;\n"
117 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
119 const char* gVS_Main_OutBitmapTexCoords =
120 " outBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
121 const char* gVS_Main_Position =
122 " vec4 transformedPosition = projection * transform * position;\n"
123 " gl_Position = transformedPosition;\n";
125 const char* gVS_Main_VertexAlpha =
126 " alpha = vtxAlpha;\n";
128 const char* gVS_Main_HasRoundRectClip =
129 " roundRectPos = (roundRectInvTransform * transformedPosition).xy;\n";
130 const char* gVS_Footer =
133 ///////////////////////////////////////////////////////////////////////////////
134 // Fragment shaders snippets
135 ///////////////////////////////////////////////////////////////////////////////
137 const char* gFS_Header_Extension_FramebufferFetch =
138 "#extension GL_NV_shader_framebuffer_fetch : enable\n\n";
139 const char* gFS_Header_Extension_ExternalTexture =
140 "#extension GL_OES_EGL_image_external : require\n\n";
141 const char* gFS_Header =
142 "precision mediump float;\n\n";
143 const char* gFS_Uniforms_Color =
144 "uniform vec4 color;\n";
145 const char* gFS_Uniforms_TextureSampler =
146 "uniform sampler2D baseSampler;\n";
147 const char* gFS_Uniforms_ExternalTextureSampler =
148 "uniform samplerExternalOES baseSampler;\n";
149 const char* gFS_Uniforms_Dither =
150 "uniform sampler2D ditherSampler;";
151 const char* gFS_Uniforms_GradientSampler[2] = {
153 "uniform sampler2D gradientSampler;\n",
155 "uniform vec4 startColor;\n"
156 "uniform vec4 endColor;\n"
158 const char* gFS_Uniforms_BitmapSampler =
159 "uniform sampler2D bitmapSampler;\n";
160 const char* gFS_Uniforms_ColorOp[3] = {
164 "uniform mat4 colorMatrix;\n"
165 "uniform vec4 colorMatrixVector;\n",
167 "uniform vec4 colorBlend;\n"
169 const char* gFS_Uniforms_Gamma =
170 "uniform float gamma;\n";
172 const char* gFS_Uniforms_HasRoundRectClip =
173 "uniform vec4 roundRectInnerRectLTRB;\n"
174 "uniform float roundRectRadius;\n";
176 const char* gFS_Main =
177 "\nvoid main(void) {\n"
178 " lowp vec4 fragColor;\n";
180 const char* gFS_Main_Dither[2] = {
182 "texture2D(ditherSampler, ditherTexCoords).a * " STR(DITHER_KERNEL_SIZE_INV_SQUARE),
184 "texture2D(ditherSampler, ditherTexCoords).a"
186 const char* gFS_Main_AddDitherToGradient =
187 " gradientColor += %s;\n";
190 const char* gFS_Fast_SingleColor =
191 "\nvoid main(void) {\n"
192 " gl_FragColor = color;\n"
194 const char* gFS_Fast_SingleTexture =
195 "\nvoid main(void) {\n"
196 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n"
198 const char* gFS_Fast_SingleModulateTexture =
199 "\nvoid main(void) {\n"
200 " gl_FragColor = color.a * texture2D(baseSampler, outTexCoords);\n"
202 const char* gFS_Fast_SingleA8Texture =
203 "\nvoid main(void) {\n"
204 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n"
206 const char* gFS_Fast_SingleA8Texture_ApplyGamma =
207 "\nvoid main(void) {\n"
208 " gl_FragColor = vec4(0.0, 0.0, 0.0, pow(texture2D(baseSampler, outTexCoords).a, gamma));\n"
210 const char* gFS_Fast_SingleModulateA8Texture =
211 "\nvoid main(void) {\n"
212 " gl_FragColor = color * texture2D(baseSampler, outTexCoords).a;\n"
214 const char* gFS_Fast_SingleModulateA8Texture_ApplyGamma =
215 "\nvoid main(void) {\n"
216 " gl_FragColor = color * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
218 const char* gFS_Fast_SingleGradient[2] = {
219 "\nvoid main(void) {\n"
220 " gl_FragColor = %s + texture2D(gradientSampler, linear);\n"
222 "\nvoid main(void) {\n"
223 " gl_FragColor = %s + mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
226 const char* gFS_Fast_SingleModulateGradient[2] = {
227 "\nvoid main(void) {\n"
228 " gl_FragColor = %s + color.a * texture2D(gradientSampler, linear);\n"
230 "\nvoid main(void) {\n"
231 " gl_FragColor = %s + color.a * mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
236 const char* gFS_Main_FetchColor =
237 " fragColor = color;\n";
238 const char* gFS_Main_ModulateColor =
239 " fragColor *= color.a;\n";
240 const char* gFS_Main_ApplyVertexAlphaLinearInterp =
241 " fragColor *= alpha;\n";
242 const char* gFS_Main_ApplyVertexAlphaShadowInterp =
243 " fragColor *= (1.0 - cos(alpha)) / 2.0;\n";
245 const char* gFS_Main_FetchTexture[2] = {
247 " fragColor = texture2D(baseSampler, outTexCoords);\n",
249 " fragColor = color * texture2D(baseSampler, outTexCoords);\n"
251 const char* gFS_Main_FetchA8Texture[4] = {
253 " fragColor = texture2D(baseSampler, outTexCoords);\n",
254 " fragColor = texture2D(baseSampler, outTexCoords);\n",
256 " fragColor = color * texture2D(baseSampler, outTexCoords).a;\n",
257 " fragColor = color * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
259 const char* gFS_Main_FetchGradient[6] = {
261 " vec4 gradientColor = texture2D(gradientSampler, linear);\n",
263 " vec4 gradientColor = mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n",
266 " vec4 gradientColor = texture2D(gradientSampler, vec2(length(circular), 0.5));\n",
268 " vec4 gradientColor = mix(startColor, endColor, clamp(length(circular), 0.0, 1.0));\n",
271 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
272 " vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n",
274 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
275 " vec4 gradientColor = mix(startColor, endColor, clamp(index - floor(index), 0.0, 1.0));\n"
277 const char* gFS_Main_FetchBitmap =
278 " vec4 bitmapColor = texture2D(bitmapSampler, outBitmapTexCoords);\n";
279 const char* gFS_Main_FetchBitmapNpot =
280 " vec4 bitmapColor = texture2D(bitmapSampler, wrap(outBitmapTexCoords));\n";
281 const char* gFS_Main_BlendShadersBG =
282 " fragColor = blendShaders(gradientColor, bitmapColor)";
283 const char* gFS_Main_BlendShadersGB =
284 " fragColor = blendShaders(bitmapColor, gradientColor)";
285 const char* gFS_Main_BlendShaders_Modulate[6] = {
292 // Modulate with alpha 8 texture
293 " * texture2D(baseSampler, outTexCoords).a;\n",
294 " * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
296 const char* gFS_Main_GradientShader_Modulate[6] = {
298 " fragColor = gradientColor;\n",
299 " fragColor = gradientColor;\n",
301 " fragColor = gradientColor * color.a;\n",
302 " fragColor = gradientColor * color.a;\n",
303 // Modulate with alpha 8 texture
304 " fragColor = gradientColor * texture2D(baseSampler, outTexCoords).a;\n",
305 " fragColor = gradientColor * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
307 const char* gFS_Main_BitmapShader_Modulate[6] = {
309 " fragColor = bitmapColor;\n",
310 " fragColor = bitmapColor;\n",
312 " fragColor = bitmapColor * color.a;\n",
313 " fragColor = bitmapColor * color.a;\n",
314 // Modulate with alpha 8 texture
315 " fragColor = bitmapColor * texture2D(baseSampler, outTexCoords).a;\n",
316 " fragColor = bitmapColor * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
318 const char* gFS_Main_FragColor =
319 " gl_FragColor = fragColor;\n";
320 const char* gFS_Main_FragColor_HasColors =
321 " gl_FragColor *= outColors;\n";
322 const char* gFS_Main_FragColor_Blend =
323 " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n";
324 const char* gFS_Main_FragColor_Blend_Swap =
325 " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n";
326 const char* gFS_Main_ApplyColorOp[3] = {
330 " fragColor.rgb /= (fragColor.a + 0.0019);\n" // un-premultiply
331 " fragColor *= colorMatrix;\n"
332 " fragColor += colorMatrixVector;\n"
333 " fragColor.rgb *= (fragColor.a + 0.0019);\n", // re-premultiply
335 " fragColor = blendColors(colorBlend, fragColor);\n"
338 // Note: LTRB -> xyzw
339 const char* gFS_Main_FragColor_HasRoundRectClip =
340 " mediump vec2 fragToLT = roundRectInnerRectLTRB.xy - roundRectPos;\n"
341 " mediump vec2 fragFromRB = roundRectPos - roundRectInnerRectLTRB.zw;\n"
343 // divide + multiply by 128 to avoid falling out of range in length() function
344 " mediump vec2 dist = max(max(fragToLT, fragFromRB), vec2(0.0, 0.0)) / 128.0;\n"
345 " mediump float linearDist = roundRectRadius - (length(dist) * 128.0);\n"
346 " gl_FragColor *= clamp(linearDist, 0.0, 1.0);\n";
348 const char* gFS_Main_DebugHighlight =
349 " gl_FragColor.rgb = vec3(0.0, gl_FragColor.a, 0.0);\n";
350 const char* gFS_Footer =
353 ///////////////////////////////////////////////////////////////////////////////
354 // PorterDuff snippets
355 ///////////////////////////////////////////////////////////////////////////////
357 const char* gBlendOps[18] = {
359 "return vec4(0.0, 0.0, 0.0, 0.0);\n",
365 "return src + dst * (1.0 - src.a);\n",
367 "return dst + src * (1.0 - dst.a);\n",
369 "return src * dst.a;\n",
371 "return dst * src.a;\n",
373 "return src * (1.0 - dst.a);\n",
375 "return dst * (1.0 - src.a);\n",
377 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n",
379 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n",
381 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, "
382 "src.a + dst.a - 2.0 * src.a * dst.a);\n",
384 "return min(src + dst, 1.0);\n",
386 "return src * dst;\n",
388 "return src + dst - src * dst;\n",
390 "return clamp(vec4(mix("
391 "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), "
392 "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), "
393 "step(dst.a, 2.0 * dst.rgb)), "
394 "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n",
396 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
397 "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
399 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
400 "max(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
403 ///////////////////////////////////////////////////////////////////////////////
404 // Constructors/destructors
405 ///////////////////////////////////////////////////////////////////////////////
407 ProgramCache::ProgramCache(Extensions& extensions)
408 : mHasES3(extensions.getMajorGlVersion() >= 3) {
411 ProgramCache::~ProgramCache() {
415 ///////////////////////////////////////////////////////////////////////////////
417 ///////////////////////////////////////////////////////////////////////////////
419 void ProgramCache::clear() {
420 PROGRAM_LOGD("Clearing program cache");
424 Program* ProgramCache::get(const ProgramDescription& description) {
425 programid key = description.key();
426 if (key == (PROGRAM_KEY_TEXTURE | PROGRAM_KEY_A8_TEXTURE)) {
427 // program for A8, unmodulated, texture w/o shader (black text/path textures) is equivalent
428 // to standard texture program (bitmaps, patches). Consider them equivalent.
429 key = PROGRAM_KEY_TEXTURE;
432 auto iter = mCache.find(key);
433 Program* program = nullptr;
434 if (iter == mCache.end()) {
435 description.log("Could not find program");
436 program = generateProgram(description, key);
437 mCache[key] = std::unique_ptr<Program>(program);
439 program = iter->second.get();
444 ///////////////////////////////////////////////////////////////////////////////
445 // Program generation
446 ///////////////////////////////////////////////////////////////////////////////
448 Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) {
449 String8 vertexShader = generateVertexShader(description);
450 String8 fragmentShader = generateFragmentShader(description);
452 return new Program(description, vertexShader.string(), fragmentShader.string());
455 static inline size_t gradientIndex(const ProgramDescription& description) {
456 return description.gradientType * 2 + description.isSimpleGradient;
459 String8 ProgramCache::generateVertexShader(const ProgramDescription& description) {
461 String8 shader(gVS_Header_Attributes);
462 if (description.hasTexture || description.hasExternalTexture) {
463 shader.append(gVS_Header_Attributes_TexCoords);
465 if (description.hasVertexAlpha) {
466 shader.append(gVS_Header_Attributes_VertexAlphaParameters);
468 if (description.hasColors) {
469 shader.append(gVS_Header_Attributes_Colors);
472 shader.append(gVS_Header_Uniforms);
473 if (description.hasTextureTransform) {
474 shader.append(gVS_Header_Uniforms_TextureTransform);
476 if (description.hasGradient) {
477 shader.append(gVS_Header_Uniforms_HasGradient);
479 if (description.hasBitmap) {
480 shader.append(gVS_Header_Uniforms_HasBitmap);
482 if (description.hasRoundRectClip) {
483 shader.append(gVS_Header_Uniforms_HasRoundRectClip);
486 if (description.hasTexture || description.hasExternalTexture) {
487 shader.append(gVS_Header_Varyings_HasTexture);
489 if (description.hasVertexAlpha) {
490 shader.append(gVS_Header_Varyings_HasVertexAlpha);
492 if (description.hasColors) {
493 shader.append(gVS_Header_Varyings_HasColors);
495 if (description.hasGradient) {
496 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
498 if (description.hasBitmap) {
499 shader.append(gVS_Header_Varyings_HasBitmap);
501 if (description.hasRoundRectClip) {
502 shader.append(gVS_Header_Varyings_HasRoundRectClip);
506 shader.append(gVS_Main); {
507 if (description.hasTextureTransform) {
508 shader.append(gVS_Main_OutTransformedTexCoords);
509 } else if (description.hasTexture || description.hasExternalTexture) {
510 shader.append(gVS_Main_OutTexCoords);
512 if (description.hasVertexAlpha) {
513 shader.append(gVS_Main_VertexAlpha);
515 if (description.hasColors) {
516 shader.append(gVS_Main_OutColors);
518 if (description.hasBitmap) {
519 shader.append(gVS_Main_OutBitmapTexCoords);
521 // Output transformed position
522 shader.append(gVS_Main_Position);
523 if (description.hasGradient) {
524 shader.append(gVS_Main_OutGradient[gradientIndex(description)]);
526 if (description.hasRoundRectClip) {
527 shader.append(gVS_Main_HasRoundRectClip);
531 shader.append(gVS_Footer);
533 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string());
538 static bool shaderOp(const ProgramDescription& description, String8& shader,
539 const int modulateOp, const char** snippets) {
540 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
541 op = op * 2 + description.hasGammaCorrection;
542 shader.append(snippets[op]);
543 return description.hasAlpha8Texture;
546 String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) {
549 const bool blendFramebuffer = description.framebufferMode >= SkXfermode::kPlus_Mode;
550 if (blendFramebuffer) {
551 shader.append(gFS_Header_Extension_FramebufferFetch);
553 if (description.hasExternalTexture) {
554 shader.append(gFS_Header_Extension_ExternalTexture);
557 shader.append(gFS_Header);
560 if (description.hasTexture || description.hasExternalTexture) {
561 shader.append(gVS_Header_Varyings_HasTexture);
563 if (description.hasVertexAlpha) {
564 shader.append(gVS_Header_Varyings_HasVertexAlpha);
566 if (description.hasColors) {
567 shader.append(gVS_Header_Varyings_HasColors);
569 if (description.hasGradient) {
570 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
572 if (description.hasBitmap) {
573 shader.append(gVS_Header_Varyings_HasBitmap);
575 if (description.hasRoundRectClip) {
576 shader.append(gVS_Header_Varyings_HasRoundRectClip);
580 int modulateOp = MODULATE_OP_NO_MODULATE;
581 const bool singleColor = !description.hasTexture && !description.hasExternalTexture &&
582 !description.hasGradient && !description.hasBitmap;
584 if (description.modulate || singleColor) {
585 shader.append(gFS_Uniforms_Color);
586 if (!singleColor) modulateOp = MODULATE_OP_MODULATE;
588 if (description.hasTexture) {
589 shader.append(gFS_Uniforms_TextureSampler);
590 } else if (description.hasExternalTexture) {
591 shader.append(gFS_Uniforms_ExternalTextureSampler);
593 if (description.hasGradient) {
594 shader.appendFormat(gFS_Uniforms_GradientSampler[description.isSimpleGradient],
595 gFS_Uniforms_Dither);
597 if (description.hasGammaCorrection) {
598 shader.append(gFS_Uniforms_Gamma);
600 if (description.hasRoundRectClip) {
601 shader.append(gFS_Uniforms_HasRoundRectClip);
604 // Optimization for common cases
605 if (!description.hasVertexAlpha
607 && !description.hasColors
608 && description.colorOp == ProgramDescription::kColorNone
609 && !description.hasDebugHighlight
610 && !description.hasRoundRectClip) {
613 const bool noShader = !description.hasGradient && !description.hasBitmap;
614 const bool singleTexture = (description.hasTexture || description.hasExternalTexture) &&
615 !description.hasAlpha8Texture && noShader;
616 const bool singleA8Texture = description.hasTexture &&
617 description.hasAlpha8Texture && noShader;
618 const bool singleGradient = !description.hasTexture && !description.hasExternalTexture &&
619 description.hasGradient && !description.hasBitmap &&
620 description.gradientType == ProgramDescription::kGradientLinear;
623 shader.append(gFS_Fast_SingleColor);
625 } else if (singleTexture) {
626 if (!description.modulate) {
627 shader.append(gFS_Fast_SingleTexture);
629 shader.append(gFS_Fast_SingleModulateTexture);
632 } else if (singleA8Texture) {
633 if (!description.modulate) {
634 if (description.hasGammaCorrection) {
635 shader.append(gFS_Fast_SingleA8Texture_ApplyGamma);
637 shader.append(gFS_Fast_SingleA8Texture);
640 if (description.hasGammaCorrection) {
641 shader.append(gFS_Fast_SingleModulateA8Texture_ApplyGamma);
643 shader.append(gFS_Fast_SingleModulateA8Texture);
647 } else if (singleGradient) {
648 if (!description.modulate) {
649 shader.appendFormat(gFS_Fast_SingleGradient[description.isSimpleGradient],
650 gFS_Main_Dither[mHasES3]);
652 shader.appendFormat(gFS_Fast_SingleModulateGradient[description.isSimpleGradient],
653 gFS_Main_Dither[mHasES3]);
660 PROGRAM_LOGD("*** Fast case:\n");
661 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
662 printLongString(shader);
669 if (description.hasBitmap) {
670 shader.append(gFS_Uniforms_BitmapSampler);
672 shader.append(gFS_Uniforms_ColorOp[description.colorOp]);
674 // Generate required functions
675 if (description.hasGradient && description.hasBitmap) {
676 generateBlend(shader, "blendShaders", description.shadersMode);
678 if (description.colorOp == ProgramDescription::kColorBlend) {
679 generateBlend(shader, "blendColors", description.colorMode);
681 if (blendFramebuffer) {
682 generateBlend(shader, "blendFramebuffer", description.framebufferMode);
684 if (description.isBitmapNpot) {
685 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
689 shader.append(gFS_Main); {
690 // Stores the result in fragColor directly
691 if (description.hasTexture || description.hasExternalTexture) {
692 if (description.hasAlpha8Texture) {
693 if (!description.hasGradient && !description.hasBitmap) {
694 shader.append(gFS_Main_FetchA8Texture[modulateOp * 2 +
695 description.hasGammaCorrection]);
698 shader.append(gFS_Main_FetchTexture[modulateOp]);
701 if (!description.hasGradient && !description.hasBitmap) {
702 shader.append(gFS_Main_FetchColor);
705 if (description.hasGradient) {
706 shader.append(gFS_Main_FetchGradient[gradientIndex(description)]);
707 shader.appendFormat(gFS_Main_AddDitherToGradient, gFS_Main_Dither[mHasES3]);
709 if (description.hasBitmap) {
710 if (!description.isBitmapNpot) {
711 shader.append(gFS_Main_FetchBitmap);
713 shader.append(gFS_Main_FetchBitmapNpot);
716 bool applyModulate = false;
717 // Case when we have two shaders set
718 if (description.hasGradient && description.hasBitmap) {
719 if (description.isBitmapFirst) {
720 shader.append(gFS_Main_BlendShadersBG);
722 shader.append(gFS_Main_BlendShadersGB);
724 applyModulate = shaderOp(description, shader, modulateOp,
725 gFS_Main_BlendShaders_Modulate);
727 if (description.hasGradient) {
728 applyModulate = shaderOp(description, shader, modulateOp,
729 gFS_Main_GradientShader_Modulate);
730 } else if (description.hasBitmap) {
731 applyModulate = shaderOp(description, shader, modulateOp,
732 gFS_Main_BitmapShader_Modulate);
736 if (description.modulate && applyModulate) {
737 shader.append(gFS_Main_ModulateColor);
740 // Apply the color op if needed
741 shader.append(gFS_Main_ApplyColorOp[description.colorOp]);
743 if (description.hasVertexAlpha) {
744 if (description.useShadowAlphaInterp) {
745 shader.append(gFS_Main_ApplyVertexAlphaShadowInterp);
747 shader.append(gFS_Main_ApplyVertexAlphaLinearInterp);
751 // Output the fragment
752 if (!blendFramebuffer) {
753 shader.append(gFS_Main_FragColor);
755 shader.append(!description.swapSrcDst ?
756 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap);
758 if (description.hasColors) {
759 shader.append(gFS_Main_FragColor_HasColors);
761 if (description.hasRoundRectClip) {
762 shader.append(gFS_Main_FragColor_HasRoundRectClip);
764 if (description.hasDebugHighlight) {
765 shader.append(gFS_Main_DebugHighlight);
769 shader.append(gFS_Footer);
772 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
773 printLongString(shader);
779 void ProgramCache::generateBlend(String8& shader, const char* name, SkXfermode::Mode mode) {
780 shader.append("\nvec4 ");
782 shader.append("(vec4 src, vec4 dst) {\n");
784 shader.append(gBlendOps[mode]);
785 shader.append("}\n");
788 void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) {
789 shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n");
790 if (wrapS == GL_MIRRORED_REPEAT) {
791 shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n");
792 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n");
794 if (wrapT == GL_MIRRORED_REPEAT) {
795 shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n");
796 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n");
798 shader.append(" return vec2(");
800 case GL_CLAMP_TO_EDGE:
801 shader.append("texCoords.x");
804 shader.append("mod(texCoords.x, 1.0)");
806 case GL_MIRRORED_REPEAT:
807 shader.append("xMod2");
812 case GL_CLAMP_TO_EDGE:
813 shader.append("texCoords.y");
816 shader.append("mod(texCoords.y, 1.0)");
818 case GL_MIRRORED_REPEAT:
819 shader.append("yMod2");
822 shader.append(");\n");
823 shader.append("}\n");
826 void ProgramCache::printLongString(const String8& shader) const {
828 ssize_t lastIndex = 0;
829 const char* str = shader.string();
830 while ((index = shader.find("\n", index)) > -1) {
831 String8 line(str, index - lastIndex);
832 if (line.length() == 0) line.append("\n");
833 ALOGD("%s", line.string());
835 str += (index - lastIndex);
840 }; // namespace uirenderer
841 }; // namespace android