ProgramCache.cpp revision d41c4d8c732095ae99c955b6b82f7306633004b1
1/* 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 "Caches.h" 22#include "Dither.h" 23#include "ProgramCache.h" 24 25namespace android { 26namespace uirenderer { 27 28/////////////////////////////////////////////////////////////////////////////// 29// Defines 30/////////////////////////////////////////////////////////////////////////////// 31 32#define MODULATE_OP_NO_MODULATE 0 33#define MODULATE_OP_MODULATE 1 34#define MODULATE_OP_MODULATE_A8 2 35 36#define STR(x) STR1(x) 37#define STR1(x) #x 38 39/////////////////////////////////////////////////////////////////////////////// 40// Vertex shaders snippets 41/////////////////////////////////////////////////////////////////////////////// 42 43const char* gVS_Header_Attributes = 44 "attribute vec4 position;\n"; 45const char* gVS_Header_Attributes_TexCoords = 46 "attribute vec2 texCoords;\n"; 47const char* gVS_Header_Attributes_Colors = 48 "attribute vec4 colors;\n"; 49const char* gVS_Header_Attributes_VertexAlphaParameters = 50 "attribute float vtxAlpha;\n"; 51const char* gVS_Header_Uniforms_TextureTransform = 52 "uniform mat4 mainTextureTransform;\n"; 53const char* gVS_Header_Uniforms = 54 "uniform mat4 projection;\n" \ 55 "uniform mat4 transform;\n"; 56const char* gVS_Header_Uniforms_HasGradient = 57 "uniform mat4 screenSpace;\n"; 58const char* gVS_Header_Uniforms_HasBitmap = 59 "uniform mat4 textureTransform;\n" 60 "uniform mediump vec2 textureDimension;\n"; 61const char* gVS_Header_Uniforms_HasRoundRectClip = 62 "uniform mat4 roundRectInvTransform;\n"; 63const char* gVS_Header_Varyings_HasTexture = 64 "varying vec2 outTexCoords;\n"; 65const char* gVS_Header_Varyings_HasColors = 66 "varying vec4 outColors;\n"; 67const char* gVS_Header_Varyings_HasVertexAlpha = 68 "varying float alpha;\n"; 69const char* gVS_Header_Varyings_HasBitmap = 70 "varying highp vec2 outBitmapTexCoords;\n"; 71const char* gVS_Header_Varyings_HasGradient[6] = { 72 // Linear 73 "varying highp vec2 linear;\n" 74 "varying vec2 ditherTexCoords;\n", 75 "varying float linear;\n" 76 "varying vec2 ditherTexCoords;\n", 77 78 // Circular 79 "varying highp vec2 circular;\n" 80 "varying vec2 ditherTexCoords;\n", 81 "varying highp vec2 circular;\n" 82 "varying vec2 ditherTexCoords;\n", 83 84 // Sweep 85 "varying highp vec2 sweep;\n" 86 "varying vec2 ditherTexCoords;\n", 87 "varying highp vec2 sweep;\n" 88 "varying vec2 ditherTexCoords;\n", 89}; 90const char* gVS_Header_Varyings_HasRoundRectClip = 91 "varying highp vec2 roundRectPos;\n"; 92const char* gVS_Main = 93 "\nvoid main(void) {\n"; 94const char* gVS_Main_OutTexCoords = 95 " outTexCoords = texCoords;\n"; 96const char* gVS_Main_OutColors = 97 " outColors = colors;\n"; 98const char* gVS_Main_OutTransformedTexCoords = 99 " outTexCoords = (mainTextureTransform * vec4(texCoords, 0.0, 1.0)).xy;\n"; 100const char* gVS_Main_OutGradient[6] = { 101 // Linear 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", 106 107 // Circular 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", 112 113 // Sweep 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", 118}; 119const char* gVS_Main_OutBitmapTexCoords = 120 " outBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n"; 121const char* gVS_Main_Position = 122 " vec4 transformedPosition = projection * transform * position;\n" 123 " gl_Position = transformedPosition;\n"; 124 125const char* gVS_Main_VertexAlpha = 126 " alpha = vtxAlpha;\n"; 127 128const char* gVS_Main_HasRoundRectClip = 129 " roundRectPos = (roundRectInvTransform * transformedPosition).xy;\n"; 130const char* gVS_Footer = 131 "}\n\n"; 132 133/////////////////////////////////////////////////////////////////////////////// 134// Fragment shaders snippets 135/////////////////////////////////////////////////////////////////////////////// 136 137const char* gFS_Header_Extension_FramebufferFetch = 138 "#extension GL_NV_shader_framebuffer_fetch : enable\n\n"; 139const char* gFS_Header_Extension_ExternalTexture = 140 "#extension GL_OES_EGL_image_external : require\n\n"; 141const char* gFS_Header = 142 "precision mediump float;\n\n"; 143const char* gFS_Uniforms_Color = 144 "uniform vec4 color;\n"; 145const char* gFS_Uniforms_TextureSampler = 146 "uniform sampler2D baseSampler;\n"; 147const char* gFS_Uniforms_ExternalTextureSampler = 148 "uniform samplerExternalOES baseSampler;\n"; 149const char* gFS_Uniforms_Dither = 150 "uniform sampler2D ditherSampler;"; 151const char* gFS_Uniforms_GradientSampler[2] = { 152 "%s\n" 153 "uniform sampler2D gradientSampler;\n", 154 "%s\n" 155 "uniform vec4 startColor;\n" 156 "uniform vec4 endColor;\n" 157}; 158const char* gFS_Uniforms_BitmapSampler = 159 "uniform sampler2D bitmapSampler;\n"; 160const char* gFS_Uniforms_ColorOp[3] = { 161 // None 162 "", 163 // Matrix 164 "uniform mat4 colorMatrix;\n" 165 "uniform vec4 colorMatrixVector;\n", 166 // PorterDuff 167 "uniform vec4 colorBlend;\n" 168}; 169const char* gFS_Uniforms_Gamma = 170 "uniform float gamma;\n"; 171 172const char* gFS_Uniforms_HasRoundRectClip = 173 "uniform vec4 roundRectInnerRectLTRB;\n" 174 "uniform float roundRectRadius;\n"; 175 176const char* gFS_Main = 177 "\nvoid main(void) {\n" 178 " lowp vec4 fragColor;\n"; 179 180const char* gFS_Main_Dither[2] = { 181 // ES 2.0 182 "texture2D(ditherSampler, ditherTexCoords).a * " STR(DITHER_KERNEL_SIZE_INV_SQUARE), 183 // ES 3.0 184 "texture2D(ditherSampler, ditherTexCoords).a" 185}; 186const char* gFS_Main_AddDitherToGradient = 187 " gradientColor += %s;\n"; 188 189// Fast cases 190const char* gFS_Fast_SingleColor = 191 "\nvoid main(void) {\n" 192 " gl_FragColor = color;\n" 193 "}\n\n"; 194const char* gFS_Fast_SingleTexture = 195 "\nvoid main(void) {\n" 196 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n" 197 "}\n\n"; 198const char* gFS_Fast_SingleModulateTexture = 199 "\nvoid main(void) {\n" 200 " gl_FragColor = color.a * texture2D(baseSampler, outTexCoords);\n" 201 "}\n\n"; 202const char* gFS_Fast_SingleA8Texture = 203 "\nvoid main(void) {\n" 204 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n" 205 "}\n\n"; 206const 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" 209 "}\n\n"; 210const char* gFS_Fast_SingleModulateA8Texture = 211 "\nvoid main(void) {\n" 212 " gl_FragColor = color * texture2D(baseSampler, outTexCoords).a;\n" 213 "}\n\n"; 214const char* gFS_Fast_SingleModulateA8Texture_ApplyGamma = 215 "\nvoid main(void) {\n" 216 " gl_FragColor = color * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n" 217 "}\n\n"; 218const char* gFS_Fast_SingleGradient[2] = { 219 "\nvoid main(void) {\n" 220 " gl_FragColor = %s + texture2D(gradientSampler, linear);\n" 221 "}\n\n", 222 "\nvoid main(void) {\n" 223 " gl_FragColor = %s + mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n" 224 "}\n\n", 225}; 226const char* gFS_Fast_SingleModulateGradient[2] = { 227 "\nvoid main(void) {\n" 228 " gl_FragColor = %s + color.a * texture2D(gradientSampler, linear);\n" 229 "}\n\n", 230 "\nvoid main(void) {\n" 231 " gl_FragColor = %s + color.a * mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n" 232 "}\n\n" 233}; 234 235// General case 236const char* gFS_Main_FetchColor = 237 " fragColor = color;\n"; 238const char* gFS_Main_ModulateColor = 239 " fragColor *= color.a;\n"; 240const char* gFS_Main_ApplyVertexAlphaLinearInterp = 241 " fragColor *= alpha;\n"; 242const char* gFS_Main_ApplyVertexAlphaShadowInterp = 243 " fragColor *= (1.0 - cos(alpha)) / 2.0;\n"; 244 245const char* gFS_Main_FetchTexture[2] = { 246 // Don't modulate 247 " fragColor = texture2D(baseSampler, outTexCoords);\n", 248 // Modulate 249 " fragColor = color * texture2D(baseSampler, outTexCoords);\n" 250}; 251const char* gFS_Main_FetchA8Texture[4] = { 252 // Don't modulate 253 " fragColor = texture2D(baseSampler, outTexCoords);\n", 254 " fragColor = texture2D(baseSampler, outTexCoords);\n", 255 // Modulate 256 " fragColor = color * texture2D(baseSampler, outTexCoords).a;\n", 257 " fragColor = color * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n" 258}; 259const char* gFS_Main_FetchGradient[6] = { 260 // Linear 261 " vec4 gradientColor = texture2D(gradientSampler, linear);\n", 262 263 " vec4 gradientColor = mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n", 264 265 // Circular 266 " vec4 gradientColor = texture2D(gradientSampler, vec2(length(circular), 0.5));\n", 267 268 " vec4 gradientColor = mix(startColor, endColor, clamp(length(circular), 0.0, 1.0));\n", 269 270 // Sweep 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", 273 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" 276}; 277const char* gFS_Main_FetchBitmap = 278 " vec4 bitmapColor = texture2D(bitmapSampler, outBitmapTexCoords);\n"; 279const char* gFS_Main_FetchBitmapNpot = 280 " vec4 bitmapColor = texture2D(bitmapSampler, wrap(outBitmapTexCoords));\n"; 281const char* gFS_Main_BlendShadersBG = 282 " fragColor = blendShaders(gradientColor, bitmapColor)"; 283const char* gFS_Main_BlendShadersGB = 284 " fragColor = blendShaders(bitmapColor, gradientColor)"; 285const char* gFS_Main_BlendShaders_Modulate[6] = { 286 // Don't modulate 287 ";\n", 288 ";\n", 289 // Modulate 290 " * color.a;\n", 291 " * color.a;\n", 292 // Modulate with alpha 8 texture 293 " * texture2D(baseSampler, outTexCoords).a;\n", 294 " * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n" 295}; 296const char* gFS_Main_GradientShader_Modulate[6] = { 297 // Don't modulate 298 " fragColor = gradientColor;\n", 299 " fragColor = gradientColor;\n", 300 // Modulate 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" 306 }; 307const char* gFS_Main_BitmapShader_Modulate[6] = { 308 // Don't modulate 309 " fragColor = bitmapColor;\n", 310 " fragColor = bitmapColor;\n", 311 // Modulate 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" 317 }; 318const char* gFS_Main_FragColor = 319 " gl_FragColor = fragColor;\n"; 320const char* gFS_Main_FragColor_HasColors = 321 " gl_FragColor *= outColors;\n"; 322const char* gFS_Main_FragColor_Blend = 323 " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n"; 324const char* gFS_Main_FragColor_Blend_Swap = 325 " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n"; 326const char* gFS_Main_ApplyColorOp[3] = { 327 // None 328 "", 329 // Matrix 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 334 // PorterDuff 335 " fragColor = blendColors(colorBlend, fragColor);\n" 336}; 337 338// Note: LTRB -> xyzw 339const char* gFS_Main_FragColor_HasRoundRectClip = 340 " mediump vec2 fragToLT = roundRectInnerRectLTRB.xy - roundRectPos;\n" 341 " mediump vec2 fragFromRB = roundRectPos - roundRectInnerRectLTRB.zw;\n" 342 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"; 347 348const char* gFS_Main_DebugHighlight = 349 " gl_FragColor.rgb = vec3(0.0, gl_FragColor.a, 0.0);\n"; 350const char* gFS_Footer = 351 "}\n\n"; 352 353/////////////////////////////////////////////////////////////////////////////// 354// PorterDuff snippets 355/////////////////////////////////////////////////////////////////////////////// 356 357const char* gBlendOps[18] = { 358 // Clear 359 "return vec4(0.0, 0.0, 0.0, 0.0);\n", 360 // Src 361 "return src;\n", 362 // Dst 363 "return dst;\n", 364 // SrcOver 365 "return src + dst * (1.0 - src.a);\n", 366 // DstOver 367 "return dst + src * (1.0 - dst.a);\n", 368 // SrcIn 369 "return src * dst.a;\n", 370 // DstIn 371 "return dst * src.a;\n", 372 // SrcOut 373 "return src * (1.0 - dst.a);\n", 374 // DstOut 375 "return dst * (1.0 - src.a);\n", 376 // SrcAtop 377 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n", 378 // DstAtop 379 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n", 380 // Xor 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", 383 // Add 384 "return min(src + dst, 1.0);\n", 385 // Multiply 386 "return src * dst;\n", 387 // Screen 388 "return src + dst - src * dst;\n", 389 // Overlay 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", 395 // Darken 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", 398 // Lighten 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", 401}; 402 403/////////////////////////////////////////////////////////////////////////////// 404// Constructors/destructors 405/////////////////////////////////////////////////////////////////////////////// 406 407ProgramCache::ProgramCache(): mHasES3(Extensions::getInstance().getMajorGlVersion() >= 3) { 408} 409 410ProgramCache::~ProgramCache() { 411 clear(); 412} 413 414/////////////////////////////////////////////////////////////////////////////// 415// Cache management 416/////////////////////////////////////////////////////////////////////////////// 417 418void ProgramCache::clear() { 419 PROGRAM_LOGD("Clearing program cache"); 420 mCache.clear(); 421} 422 423Program* ProgramCache::get(const ProgramDescription& description) { 424 programid key = description.key(); 425 if (key == (PROGRAM_KEY_TEXTURE | PROGRAM_KEY_A8_TEXTURE)) { 426 // program for A8, unmodulated, texture w/o shader (black text/path textures) is equivalent 427 // to standard texture program (bitmaps, patches). Consider them equivalent. 428 key = PROGRAM_KEY_TEXTURE; 429 } 430 431 auto iter = mCache.find(key); 432 Program* program = nullptr; 433 if (iter == mCache.end()) { 434 description.log("Could not find program"); 435 program = generateProgram(description, key); 436 mCache[key] = std::unique_ptr<Program>(program); 437 } else { 438 program = iter->second.get(); 439 } 440 return program; 441} 442 443/////////////////////////////////////////////////////////////////////////////// 444// Program generation 445/////////////////////////////////////////////////////////////////////////////// 446 447Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) { 448 String8 vertexShader = generateVertexShader(description); 449 String8 fragmentShader = generateFragmentShader(description); 450 451 return new Program(description, vertexShader.string(), fragmentShader.string()); 452} 453 454static inline size_t gradientIndex(const ProgramDescription& description) { 455 return description.gradientType * 2 + description.isSimpleGradient; 456} 457 458String8 ProgramCache::generateVertexShader(const ProgramDescription& description) { 459 // Add attributes 460 String8 shader(gVS_Header_Attributes); 461 if (description.hasTexture || description.hasExternalTexture) { 462 shader.append(gVS_Header_Attributes_TexCoords); 463 } 464 if (description.hasVertexAlpha) { 465 shader.append(gVS_Header_Attributes_VertexAlphaParameters); 466 } 467 if (description.hasColors) { 468 shader.append(gVS_Header_Attributes_Colors); 469 } 470 // Uniforms 471 shader.append(gVS_Header_Uniforms); 472 if (description.hasTextureTransform) { 473 shader.append(gVS_Header_Uniforms_TextureTransform); 474 } 475 if (description.hasGradient) { 476 shader.append(gVS_Header_Uniforms_HasGradient); 477 } 478 if (description.hasBitmap) { 479 shader.append(gVS_Header_Uniforms_HasBitmap); 480 } 481 if (description.hasRoundRectClip) { 482 shader.append(gVS_Header_Uniforms_HasRoundRectClip); 483 } 484 // Varyings 485 if (description.hasTexture || description.hasExternalTexture) { 486 shader.append(gVS_Header_Varyings_HasTexture); 487 } 488 if (description.hasVertexAlpha) { 489 shader.append(gVS_Header_Varyings_HasVertexAlpha); 490 } 491 if (description.hasColors) { 492 shader.append(gVS_Header_Varyings_HasColors); 493 } 494 if (description.hasGradient) { 495 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]); 496 } 497 if (description.hasBitmap) { 498 shader.append(gVS_Header_Varyings_HasBitmap); 499 } 500 if (description.hasRoundRectClip) { 501 shader.append(gVS_Header_Varyings_HasRoundRectClip); 502 } 503 504 // Begin the shader 505 shader.append(gVS_Main); { 506 if (description.hasTextureTransform) { 507 shader.append(gVS_Main_OutTransformedTexCoords); 508 } else if (description.hasTexture || description.hasExternalTexture) { 509 shader.append(gVS_Main_OutTexCoords); 510 } 511 if (description.hasVertexAlpha) { 512 shader.append(gVS_Main_VertexAlpha); 513 } 514 if (description.hasColors) { 515 shader.append(gVS_Main_OutColors); 516 } 517 if (description.hasBitmap) { 518 shader.append(gVS_Main_OutBitmapTexCoords); 519 } 520 // Output transformed position 521 shader.append(gVS_Main_Position); 522 if (description.hasGradient) { 523 shader.append(gVS_Main_OutGradient[gradientIndex(description)]); 524 } 525 if (description.hasRoundRectClip) { 526 shader.append(gVS_Main_HasRoundRectClip); 527 } 528 } 529 // End the shader 530 shader.append(gVS_Footer); 531 532 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string()); 533 534 return shader; 535} 536 537static bool shaderOp(const ProgramDescription& description, String8& shader, 538 const int modulateOp, const char** snippets) { 539 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp; 540 op = op * 2 + description.hasGammaCorrection; 541 shader.append(snippets[op]); 542 return description.hasAlpha8Texture; 543} 544 545String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) { 546 String8 shader; 547 548 const bool blendFramebuffer = description.framebufferMode >= SkXfermode::kPlus_Mode; 549 if (blendFramebuffer) { 550 shader.append(gFS_Header_Extension_FramebufferFetch); 551 } 552 if (description.hasExternalTexture) { 553 shader.append(gFS_Header_Extension_ExternalTexture); 554 } 555 556 shader.append(gFS_Header); 557 558 // Varyings 559 if (description.hasTexture || description.hasExternalTexture) { 560 shader.append(gVS_Header_Varyings_HasTexture); 561 } 562 if (description.hasVertexAlpha) { 563 shader.append(gVS_Header_Varyings_HasVertexAlpha); 564 } 565 if (description.hasColors) { 566 shader.append(gVS_Header_Varyings_HasColors); 567 } 568 if (description.hasGradient) { 569 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]); 570 } 571 if (description.hasBitmap) { 572 shader.append(gVS_Header_Varyings_HasBitmap); 573 } 574 if (description.hasRoundRectClip) { 575 shader.append(gVS_Header_Varyings_HasRoundRectClip); 576 } 577 578 // Uniforms 579 int modulateOp = MODULATE_OP_NO_MODULATE; 580 const bool singleColor = !description.hasTexture && !description.hasExternalTexture && 581 !description.hasGradient && !description.hasBitmap; 582 583 if (description.modulate || singleColor) { 584 shader.append(gFS_Uniforms_Color); 585 if (!singleColor) modulateOp = MODULATE_OP_MODULATE; 586 } 587 if (description.hasTexture) { 588 shader.append(gFS_Uniforms_TextureSampler); 589 } else if (description.hasExternalTexture) { 590 shader.append(gFS_Uniforms_ExternalTextureSampler); 591 } 592 if (description.hasGradient) { 593 shader.appendFormat(gFS_Uniforms_GradientSampler[description.isSimpleGradient], 594 gFS_Uniforms_Dither); 595 } 596 if (description.hasGammaCorrection) { 597 shader.append(gFS_Uniforms_Gamma); 598 } 599 if (description.hasRoundRectClip) { 600 shader.append(gFS_Uniforms_HasRoundRectClip); 601 } 602 603 // Optimization for common cases 604 if (!description.hasVertexAlpha 605 && !blendFramebuffer 606 && !description.hasColors 607 && description.colorOp == ProgramDescription::kColorNone 608 && !description.hasDebugHighlight 609 && !description.hasRoundRectClip) { 610 bool fast = false; 611 612 const bool noShader = !description.hasGradient && !description.hasBitmap; 613 const bool singleTexture = (description.hasTexture || description.hasExternalTexture) && 614 !description.hasAlpha8Texture && noShader; 615 const bool singleA8Texture = description.hasTexture && 616 description.hasAlpha8Texture && noShader; 617 const bool singleGradient = !description.hasTexture && !description.hasExternalTexture && 618 description.hasGradient && !description.hasBitmap && 619 description.gradientType == ProgramDescription::kGradientLinear; 620 621 if (singleColor) { 622 shader.append(gFS_Fast_SingleColor); 623 fast = true; 624 } else if (singleTexture) { 625 if (!description.modulate) { 626 shader.append(gFS_Fast_SingleTexture); 627 } else { 628 shader.append(gFS_Fast_SingleModulateTexture); 629 } 630 fast = true; 631 } else if (singleA8Texture) { 632 if (!description.modulate) { 633 if (description.hasGammaCorrection) { 634 shader.append(gFS_Fast_SingleA8Texture_ApplyGamma); 635 } else { 636 shader.append(gFS_Fast_SingleA8Texture); 637 } 638 } else { 639 if (description.hasGammaCorrection) { 640 shader.append(gFS_Fast_SingleModulateA8Texture_ApplyGamma); 641 } else { 642 shader.append(gFS_Fast_SingleModulateA8Texture); 643 } 644 } 645 fast = true; 646 } else if (singleGradient) { 647 if (!description.modulate) { 648 shader.appendFormat(gFS_Fast_SingleGradient[description.isSimpleGradient], 649 gFS_Main_Dither[mHasES3]); 650 } else { 651 shader.appendFormat(gFS_Fast_SingleModulateGradient[description.isSimpleGradient], 652 gFS_Main_Dither[mHasES3]); 653 } 654 fast = true; 655 } 656 657 if (fast) { 658#if DEBUG_PROGRAMS 659 PROGRAM_LOGD("*** Fast case:\n"); 660 PROGRAM_LOGD("*** Generated fragment shader:\n\n"); 661 printLongString(shader); 662#endif 663 664 return shader; 665 } 666 } 667 668 if (description.hasBitmap) { 669 shader.append(gFS_Uniforms_BitmapSampler); 670 } 671 shader.append(gFS_Uniforms_ColorOp[description.colorOp]); 672 673 // Generate required functions 674 if (description.hasGradient && description.hasBitmap) { 675 generateBlend(shader, "blendShaders", description.shadersMode); 676 } 677 if (description.colorOp == ProgramDescription::kColorBlend) { 678 generateBlend(shader, "blendColors", description.colorMode); 679 } 680 if (blendFramebuffer) { 681 generateBlend(shader, "blendFramebuffer", description.framebufferMode); 682 } 683 if (description.isBitmapNpot) { 684 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT); 685 } 686 687 // Begin the shader 688 shader.append(gFS_Main); { 689 // Stores the result in fragColor directly 690 if (description.hasTexture || description.hasExternalTexture) { 691 if (description.hasAlpha8Texture) { 692 if (!description.hasGradient && !description.hasBitmap) { 693 shader.append(gFS_Main_FetchA8Texture[modulateOp * 2 + 694 description.hasGammaCorrection]); 695 } 696 } else { 697 shader.append(gFS_Main_FetchTexture[modulateOp]); 698 } 699 } else { 700 if (!description.hasGradient && !description.hasBitmap) { 701 shader.append(gFS_Main_FetchColor); 702 } 703 } 704 if (description.hasGradient) { 705 shader.append(gFS_Main_FetchGradient[gradientIndex(description)]); 706 shader.appendFormat(gFS_Main_AddDitherToGradient, gFS_Main_Dither[mHasES3]); 707 } 708 if (description.hasBitmap) { 709 if (!description.isBitmapNpot) { 710 shader.append(gFS_Main_FetchBitmap); 711 } else { 712 shader.append(gFS_Main_FetchBitmapNpot); 713 } 714 } 715 bool applyModulate = false; 716 // Case when we have two shaders set 717 if (description.hasGradient && description.hasBitmap) { 718 if (description.isBitmapFirst) { 719 shader.append(gFS_Main_BlendShadersBG); 720 } else { 721 shader.append(gFS_Main_BlendShadersGB); 722 } 723 applyModulate = shaderOp(description, shader, modulateOp, 724 gFS_Main_BlendShaders_Modulate); 725 } else { 726 if (description.hasGradient) { 727 applyModulate = shaderOp(description, shader, modulateOp, 728 gFS_Main_GradientShader_Modulate); 729 } else if (description.hasBitmap) { 730 applyModulate = shaderOp(description, shader, modulateOp, 731 gFS_Main_BitmapShader_Modulate); 732 } 733 } 734 735 if (description.modulate && applyModulate) { 736 shader.append(gFS_Main_ModulateColor); 737 } 738 739 // Apply the color op if needed 740 shader.append(gFS_Main_ApplyColorOp[description.colorOp]); 741 742 if (description.hasVertexAlpha) { 743 if (description.useShadowAlphaInterp) { 744 shader.append(gFS_Main_ApplyVertexAlphaShadowInterp); 745 } else { 746 shader.append(gFS_Main_ApplyVertexAlphaLinearInterp); 747 } 748 } 749 750 // Output the fragment 751 if (!blendFramebuffer) { 752 shader.append(gFS_Main_FragColor); 753 } else { 754 shader.append(!description.swapSrcDst ? 755 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap); 756 } 757 if (description.hasColors) { 758 shader.append(gFS_Main_FragColor_HasColors); 759 } 760 if (description.hasRoundRectClip) { 761 shader.append(gFS_Main_FragColor_HasRoundRectClip); 762 } 763 if (description.hasDebugHighlight) { 764 shader.append(gFS_Main_DebugHighlight); 765 } 766 } 767 // End the shader 768 shader.append(gFS_Footer); 769 770#if DEBUG_PROGRAMS 771 PROGRAM_LOGD("*** Generated fragment shader:\n\n"); 772 printLongString(shader); 773#endif 774 775 return shader; 776} 777 778void ProgramCache::generateBlend(String8& shader, const char* name, SkXfermode::Mode mode) { 779 shader.append("\nvec4 "); 780 shader.append(name); 781 shader.append("(vec4 src, vec4 dst) {\n"); 782 shader.append(" "); 783 shader.append(gBlendOps[mode]); 784 shader.append("}\n"); 785} 786 787void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) { 788 shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n"); 789 if (wrapS == GL_MIRRORED_REPEAT) { 790 shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n"); 791 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n"); 792 } 793 if (wrapT == GL_MIRRORED_REPEAT) { 794 shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n"); 795 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n"); 796 } 797 shader.append(" return vec2("); 798 switch (wrapS) { 799 case GL_CLAMP_TO_EDGE: 800 shader.append("texCoords.x"); 801 break; 802 case GL_REPEAT: 803 shader.append("mod(texCoords.x, 1.0)"); 804 break; 805 case GL_MIRRORED_REPEAT: 806 shader.append("xMod2"); 807 break; 808 } 809 shader.append(", "); 810 switch (wrapT) { 811 case GL_CLAMP_TO_EDGE: 812 shader.append("texCoords.y"); 813 break; 814 case GL_REPEAT: 815 shader.append("mod(texCoords.y, 1.0)"); 816 break; 817 case GL_MIRRORED_REPEAT: 818 shader.append("yMod2"); 819 break; 820 } 821 shader.append(");\n"); 822 shader.append("}\n"); 823} 824 825void ProgramCache::printLongString(const String8& shader) const { 826 ssize_t index = 0; 827 ssize_t lastIndex = 0; 828 const char* str = shader.string(); 829 while ((index = shader.find("\n", index)) > -1) { 830 String8 line(str, index - lastIndex); 831 if (line.length() == 0) line.append("\n"); 832 PROGRAM_LOGD("%s", line.string()); 833 index++; 834 str += (index - lastIndex); 835 lastIndex = index; 836 } 837} 838 839}; // namespace uirenderer 840}; // namespace android 841