ProgramCache.cpp revision 91a8ec0145ae0ce85782b40c964d16ba2465aec7
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#include <utils/String8.h> 18 19#include "Caches.h" 20#include "ProgramCache.h" 21#include "Properties.h" 22 23namespace android { 24namespace uirenderer { 25 26/////////////////////////////////////////////////////////////////////////////// 27// 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#define STR(x) STR1(x) 35#define STR1(x) #x 36 37/////////////////////////////////////////////////////////////////////////////// 38// Vertex shaders snippets 39/////////////////////////////////////////////////////////////////////////////// 40 41const char* gVS_Header_Start = 42 "#version 100\n" 43 "attribute vec4 position;\n"; 44const char* gVS_Header_Attributes_TexCoords = 45 "attribute vec2 texCoords;\n"; 46const char* gVS_Header_Attributes_Colors = 47 "attribute vec4 colors;\n"; 48const char* gVS_Header_Attributes_VertexAlphaParameters = 49 "attribute float vtxAlpha;\n"; 50const char* gVS_Header_Uniforms_TextureTransform = 51 "uniform mat4 mainTextureTransform;\n"; 52const char* gVS_Header_Uniforms = 53 "uniform mat4 projection;\n" \ 54 "uniform mat4 transform;\n"; 55const char* gVS_Header_Uniforms_HasGradient = 56 "uniform mat4 screenSpace;\n"; 57const char* gVS_Header_Uniforms_HasBitmap = 58 "uniform mat4 textureTransform;\n" 59 "uniform mediump vec2 textureDimension;\n"; 60const char* gVS_Header_Uniforms_HasRoundRectClip = 61 "uniform mat4 roundRectInvTransform;\n"; 62const char* gVS_Header_Varyings_HasTexture = 63 "varying vec2 outTexCoords;\n"; 64const char* gVS_Header_Varyings_HasColors = 65 "varying vec4 outColors;\n"; 66const char* gVS_Header_Varyings_HasVertexAlpha = 67 "varying float alpha;\n"; 68const char* gVS_Header_Varyings_HasBitmap = 69 "varying highp vec2 outBitmapTexCoords;\n"; 70const char* gVS_Header_Varyings_HasGradient[6] = { 71 // Linear 72 "varying highp vec2 linear;\n", 73 "varying float linear;\n", 74 75 // Circular 76 "varying highp vec2 circular;\n", 77 "varying highp vec2 circular;\n", 78 79 // Sweep 80 "varying highp vec2 sweep;\n", 81 "varying highp vec2 sweep;\n", 82}; 83const char* gVS_Header_Varyings_HasRoundRectClip = 84 "varying highp vec2 roundRectPos;\n"; 85const char* gVS_Main = 86 "\nvoid main(void) {\n"; 87const char* gVS_Main_OutTexCoords = 88 " outTexCoords = texCoords;\n"; 89const char* gVS_Main_OutColors = 90 " outColors = colors;\n"; 91const char* gVS_Main_OutTransformedTexCoords = 92 " outTexCoords = (mainTextureTransform * vec4(texCoords, 0.0, 1.0)).xy;\n"; 93const char* gVS_Main_OutGradient[6] = { 94 // Linear 95 " linear = vec2((screenSpace * position).x, 0.5);\n", 96 " linear = (screenSpace * position).x;\n", 97 98 // Circular 99 " circular = (screenSpace * position).xy;\n", 100 " circular = (screenSpace * position).xy;\n", 101 102 // Sweep 103 " sweep = (screenSpace * position).xy;\n", 104 " sweep = (screenSpace * position).xy;\n" 105}; 106const char* gVS_Main_OutBitmapTexCoords = 107 " outBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n"; 108const char* gVS_Main_Position = 109 " vec4 transformedPosition = projection * transform * position;\n" 110 " gl_Position = transformedPosition;\n"; 111 112const char* gVS_Main_VertexAlpha = 113 " alpha = vtxAlpha;\n"; 114 115const char* gVS_Main_HasRoundRectClip = 116 " roundRectPos = (roundRectInvTransform * transformedPosition).xy;\n"; 117const char* gVS_Footer = 118 "}\n\n"; 119 120/////////////////////////////////////////////////////////////////////////////// 121// Fragment shaders snippets 122/////////////////////////////////////////////////////////////////////////////// 123 124const char* gFS_Header_Start = 125 "#version 100\n"; 126const char* gFS_Header_Extension_FramebufferFetch = 127 "#extension GL_NV_shader_framebuffer_fetch : enable\n\n"; 128const char* gFS_Header_Extension_ExternalTexture = 129 "#extension GL_OES_EGL_image_external : require\n\n"; 130const char* gFS_Header = 131 "precision mediump float;\n\n"; 132const char* gFS_Uniforms_Color = 133 "uniform vec4 color;\n"; 134const char* gFS_Uniforms_TextureSampler = 135 "uniform sampler2D baseSampler;\n"; 136const char* gFS_Uniforms_ExternalTextureSampler = 137 "uniform samplerExternalOES baseSampler;\n"; 138const char* gFS_Uniforms_GradientSampler[2] = { 139 "uniform vec2 screenSize;\n" 140 "uniform sampler2D gradientSampler;\n", 141 142 "uniform vec2 screenSize;\n" 143 "uniform vec4 startColor;\n" 144 "uniform vec4 endColor;\n" 145}; 146const char* gFS_Uniforms_BitmapSampler = 147 "uniform sampler2D bitmapSampler;\n"; 148const char* gFS_Uniforms_BitmapExternalSampler = 149 "uniform samplerExternalOES bitmapSampler;\n"; 150const char* gFS_Uniforms_ColorOp[3] = { 151 // None 152 "", 153 // Matrix 154 "uniform mat4 colorMatrix;\n" 155 "uniform vec4 colorMatrixVector;\n", 156 // PorterDuff 157 "uniform vec4 colorBlend;\n" 158}; 159 160const char* gFS_Uniforms_HasRoundRectClip = 161 "uniform vec4 roundRectInnerRectLTRB;\n" 162 "uniform float roundRectRadius;\n"; 163 164const char* gFS_OETF[2] = { 165 "\nvec4 OETF(const vec4 linear) {\n" 166 " return linear;\n" 167 "}\n", 168 // We expect linear data to be scRGB so we mirror the gamma function 169 "\nvec4 OETF(const vec4 linear) {" 170 " return vec4(sign(linear.rgb) * OETF_sRGB(abs(linear.rgb)), linear.a);\n" 171 "}\n", 172}; 173 174const char* gFS_Transfer_Functions = R"__SHADER__( 175 float OETF_sRGB(const float linear) { 176 // IEC 61966-2-1:1999 177 return linear <= 0.0031308 ? linear * 12.92 : (pow(linear, 1.0 / 2.4) * 1.055) - 0.055; 178 } 179 180 vec3 OETF_sRGB(const vec3 linear) { 181 return vec3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b)); 182 } 183 184 float EOTF_sRGB(float srgb) { 185 // IEC 61966-2-1:1999 186 return srgb <= 0.04045 ? srgb / 12.92 : pow((srgb + 0.055) / 1.055, 2.4); 187 } 188)__SHADER__"; 189 190// Dithering must be done in the quantization space 191// When we are writing to an sRGB framebuffer, we must do the following: 192// EOTF(OETF(color) + dither) 193// The dithering pattern is generated with a triangle noise generator in the range [-0.0,1.0] 194// TODO: Handle linear fp16 render targets 195const char* gFS_Gradient_Functions = R"__SHADER__( 196 float triangleNoise(const highp vec2 n) { 197 highp vec2 p = fract(n * vec2(5.3987, 5.4421)); 198 p += dot(p.yx, p.xy + vec2(21.5351, 14.3137)); 199 highp float xy = p.x * p.y; 200 return fract(xy * 95.4307) + fract(xy * 75.04961) - 1.0; 201 } 202)__SHADER__"; 203const char* gFS_Gradient_Preamble[2] = { 204 // Linear framebuffer 205 "\nvec4 dither(const vec4 color) {\n" 206 " return vec4(color.rgb + (triangleNoise(gl_FragCoord.xy * screenSize.xy) / 255.0), color.a);\n" 207 "}\n" 208 "\nvec4 gammaMix(const vec4 a, const vec4 b, float v) {\n" 209 " vec4 c = mix(a, b, v);\n" 210 " c.a = EOTF_sRGB(c.a);\n" // This is technically incorrect but preserves compatibility 211 " return vec4(OETF_sRGB(c.rgb) * c.a, c.a);\n" 212 "}\n", 213 // sRGB framebuffer 214 "\nvec4 dither(const vec4 color) {\n" 215 " vec3 dithered = sqrt(color.rgb) + (triangleNoise(gl_FragCoord.xy * screenSize.xy) / 255.0);\n" 216 " return vec4(dithered * dithered, color.a);\n" 217 "}\n" 218 "\nvec4 gammaMix(const vec4 a, const vec4 b, float v) {\n" 219 " vec4 c = mix(a, b, v);\n" 220 " return vec4(c.rgb * c.a, c.a);\n" 221 "}\n" 222}; 223 224// Uses luminance coefficients from Rec.709 to choose the appropriate gamma 225// The gamma() function assumes that bright text will be displayed on a dark 226// background and that dark text will be displayed on bright background 227// The gamma coefficient is chosen to thicken or thin the text accordingly 228// The dot product used to compute the luminance could be approximated with 229// a simple max(color.r, color.g, color.b) 230const char* gFS_Gamma_Preamble = R"__SHADER__( 231 #define GAMMA (%.2f) 232 #define GAMMA_INV (%.2f) 233 234 float gamma(float a, const vec3 color) { 235 float luminance = dot(color, vec3(0.2126, 0.7152, 0.0722)); 236 return pow(a, luminance < 0.5 ? GAMMA_INV : GAMMA); 237 } 238)__SHADER__"; 239 240const char* gFS_Main = 241 "\nvoid main(void) {\n" 242 " vec4 fragColor;\n"; 243 244const char* gFS_Main_AddDither = 245 " fragColor = dither(fragColor);\n"; 246 247// General case 248const char* gFS_Main_FetchColor = 249 " fragColor = color;\n"; 250const char* gFS_Main_ModulateColor = 251 " fragColor *= color.a;\n"; 252const char* gFS_Main_ApplyVertexAlphaLinearInterp = 253 " fragColor *= alpha;\n"; 254const char* gFS_Main_ApplyVertexAlphaShadowInterp = 255 // map alpha through shadow alpha sampler 256 " fragColor *= texture2D(baseSampler, vec2(alpha, 0.5)).a;\n"; 257const char* gFS_Main_FetchTexture[2] = { 258 // Don't modulate 259 " fragColor = OETF(texture2D(baseSampler, outTexCoords));\n", 260 // Modulate 261 " fragColor = color * texture2D(baseSampler, outTexCoords);\n" 262}; 263const char* gFS_Main_FetchA8Texture[4] = { 264 // Don't modulate 265 " fragColor = texture2D(baseSampler, outTexCoords);\n", 266 " fragColor = texture2D(baseSampler, outTexCoords);\n", 267 // Modulate 268 " fragColor = color * texture2D(baseSampler, outTexCoords).a;\n", 269 " fragColor = color * gamma(texture2D(baseSampler, outTexCoords).a, color.rgb);\n", 270}; 271const char* gFS_Main_FetchGradient[6] = { 272 // Linear 273 " vec4 gradientColor = texture2D(gradientSampler, linear);\n", 274 275 " vec4 gradientColor = gammaMix(startColor, endColor, clamp(linear, 0.0, 1.0));\n", 276 277 // Circular 278 " vec4 gradientColor = texture2D(gradientSampler, vec2(length(circular), 0.5));\n", 279 280 " vec4 gradientColor = gammaMix(startColor, endColor, clamp(length(circular), 0.0, 1.0));\n", 281 282 // Sweep 283 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n" 284 " vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n", 285 286 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n" 287 " vec4 gradientColor = gammaMix(startColor, endColor, clamp(index - floor(index), 0.0, 1.0));\n" 288}; 289const char* gFS_Main_FetchBitmap = 290 " vec4 bitmapColor = OETF(texture2D(bitmapSampler, outBitmapTexCoords));\n"; 291const char* gFS_Main_FetchBitmapNpot = 292 " vec4 bitmapColor = OETF(texture2D(bitmapSampler, wrap(outBitmapTexCoords)));\n"; 293const char* gFS_Main_BlendShadersBG = 294 " fragColor = blendShaders(gradientColor, bitmapColor)"; 295const char* gFS_Main_BlendShadersGB = 296 " fragColor = blendShaders(bitmapColor, gradientColor)"; 297const char* gFS_Main_BlendShaders_Modulate[6] = { 298 // Don't modulate 299 ";\n", 300 ";\n", 301 // Modulate 302 " * color.a;\n", 303 " * color.a;\n", 304 // Modulate with alpha 8 texture 305 " * texture2D(baseSampler, outTexCoords).a;\n", 306 " * gamma(texture2D(baseSampler, outTexCoords).a, color.rgb);\n", 307}; 308const char* gFS_Main_GradientShader_Modulate[6] = { 309 // Don't modulate 310 " fragColor = gradientColor;\n", 311 " fragColor = gradientColor;\n", 312 // Modulate 313 " fragColor = gradientColor * color.a;\n", 314 " fragColor = gradientColor * color.a;\n", 315 // Modulate with alpha 8 texture 316 " fragColor = gradientColor * texture2D(baseSampler, outTexCoords).a;\n", 317 " fragColor = gradientColor * gamma(texture2D(baseSampler, outTexCoords).a, gradientColor.rgb);\n", 318 }; 319const char* gFS_Main_BitmapShader_Modulate[6] = { 320 // Don't modulate 321 " fragColor = bitmapColor;\n", 322 " fragColor = bitmapColor;\n", 323 // Modulate 324 " fragColor = bitmapColor * color.a;\n", 325 " fragColor = bitmapColor * color.a;\n", 326 // Modulate with alpha 8 texture 327 " fragColor = bitmapColor * texture2D(baseSampler, outTexCoords).a;\n", 328 " fragColor = bitmapColor * gamma(texture2D(baseSampler, outTexCoords).a, bitmapColor.rgb);\n", 329 }; 330const char* gFS_Main_FragColor = 331 " gl_FragColor = fragColor;\n"; 332const char* gFS_Main_FragColor_HasColors = 333 " gl_FragColor *= outColors;\n"; 334const char* gFS_Main_FragColor_Blend = 335 " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n"; 336const char* gFS_Main_FragColor_Blend_Swap = 337 " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n"; 338const char* gFS_Main_ApplyColorOp[3] = { 339 // None 340 "", 341 // Matrix 342 " fragColor.rgb /= (fragColor.a + 0.0019);\n" // un-premultiply 343 " fragColor *= colorMatrix;\n" 344 " fragColor += colorMatrixVector;\n" 345 " fragColor.rgb *= (fragColor.a + 0.0019);\n", // re-premultiply 346 // PorterDuff 347 " fragColor = blendColors(colorBlend, fragColor);\n" 348}; 349 350// Note: LTRB -> xyzw 351const char* gFS_Main_FragColor_HasRoundRectClip = 352 " mediump vec2 fragToLT = roundRectInnerRectLTRB.xy - roundRectPos;\n" 353 " mediump vec2 fragFromRB = roundRectPos - roundRectInnerRectLTRB.zw;\n" 354 355 // divide + multiply by 128 to avoid falling out of range in length() function 356 " mediump vec2 dist = max(max(fragToLT, fragFromRB), vec2(0.0, 0.0)) / 128.0;\n" 357 " mediump float linearDist = roundRectRadius - (length(dist) * 128.0);\n" 358 " gl_FragColor *= clamp(linearDist, 0.0, 1.0);\n"; 359 360const char* gFS_Main_DebugHighlight = 361 " gl_FragColor.rgb = vec3(0.0, gl_FragColor.a, 0.0);\n"; 362const char* gFS_Footer = 363 "}\n\n"; 364 365/////////////////////////////////////////////////////////////////////////////// 366// PorterDuff snippets 367/////////////////////////////////////////////////////////////////////////////// 368 369const char* gBlendOps[18] = { 370 // Clear 371 "return vec4(0.0, 0.0, 0.0, 0.0);\n", 372 // Src 373 "return src;\n", 374 // Dst 375 "return dst;\n", 376 // SrcOver 377 "return src + dst * (1.0 - src.a);\n", 378 // DstOver 379 "return dst + src * (1.0 - dst.a);\n", 380 // SrcIn 381 "return src * dst.a;\n", 382 // DstIn 383 "return dst * src.a;\n", 384 // SrcOut 385 "return src * (1.0 - dst.a);\n", 386 // DstOut 387 "return dst * (1.0 - src.a);\n", 388 // SrcAtop 389 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n", 390 // DstAtop 391 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n", 392 // Xor 393 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, " 394 "src.a + dst.a - 2.0 * src.a * dst.a);\n", 395 // Plus 396 "return min(src + dst, 1.0);\n", 397 // Modulate 398 "return src * dst;\n", 399 // Screen 400 "return src + dst - src * dst;\n", 401 // Overlay 402 "return clamp(vec4(mix(" 403 "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), " 404 "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), " 405 "step(dst.a, 2.0 * dst.rgb)), " 406 "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n", 407 // Darken 408 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + " 409 "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n", 410 // Lighten 411 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + " 412 "max(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n", 413}; 414 415/////////////////////////////////////////////////////////////////////////////// 416// Constructors/destructors 417/////////////////////////////////////////////////////////////////////////////// 418 419ProgramCache::ProgramCache(Extensions& extensions) 420 : mHasES3(extensions.getMajorGlVersion() >= 3) 421 , mHasSRGB(extensions.hasSRGB()) { 422} 423 424ProgramCache::~ProgramCache() { 425 clear(); 426} 427 428/////////////////////////////////////////////////////////////////////////////// 429// Cache management 430/////////////////////////////////////////////////////////////////////////////// 431 432void ProgramCache::clear() { 433 PROGRAM_LOGD("Clearing program cache"); 434 mCache.clear(); 435} 436 437Program* ProgramCache::get(const ProgramDescription& description) { 438 programid key = description.key(); 439 if (key == (PROGRAM_KEY_TEXTURE | PROGRAM_KEY_A8_TEXTURE)) { 440 // program for A8, unmodulated, texture w/o shader (black text/path textures) is equivalent 441 // to standard texture program (bitmaps, patches). Consider them equivalent. 442 key = PROGRAM_KEY_TEXTURE; 443 } 444 445 auto iter = mCache.find(key); 446 Program* program = nullptr; 447 if (iter == mCache.end()) { 448 description.log("Could not find program"); 449 program = generateProgram(description, key); 450 mCache[key] = std::unique_ptr<Program>(program); 451 } else { 452 program = iter->second.get(); 453 } 454 return program; 455} 456 457/////////////////////////////////////////////////////////////////////////////// 458// Program generation 459/////////////////////////////////////////////////////////////////////////////// 460 461Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) { 462 String8 vertexShader = generateVertexShader(description); 463 String8 fragmentShader = generateFragmentShader(description); 464 465 return new Program(description, vertexShader.string(), fragmentShader.string()); 466} 467 468static inline size_t gradientIndex(const ProgramDescription& description) { 469 return description.gradientType * 2 + description.isSimpleGradient; 470} 471 472String8 ProgramCache::generateVertexShader(const ProgramDescription& description) { 473 // Add attributes 474 String8 shader(gVS_Header_Start); 475 if (description.hasTexture || description.hasExternalTexture) { 476 shader.append(gVS_Header_Attributes_TexCoords); 477 } 478 if (description.hasVertexAlpha) { 479 shader.append(gVS_Header_Attributes_VertexAlphaParameters); 480 } 481 if (description.hasColors) { 482 shader.append(gVS_Header_Attributes_Colors); 483 } 484 // Uniforms 485 shader.append(gVS_Header_Uniforms); 486 if (description.hasTextureTransform) { 487 shader.append(gVS_Header_Uniforms_TextureTransform); 488 } 489 if (description.hasGradient) { 490 shader.append(gVS_Header_Uniforms_HasGradient); 491 } 492 if (description.hasBitmap) { 493 shader.append(gVS_Header_Uniforms_HasBitmap); 494 } 495 if (description.hasRoundRectClip) { 496 shader.append(gVS_Header_Uniforms_HasRoundRectClip); 497 } 498 // Varyings 499 if (description.hasTexture || description.hasExternalTexture) { 500 shader.append(gVS_Header_Varyings_HasTexture); 501 } 502 if (description.hasVertexAlpha) { 503 shader.append(gVS_Header_Varyings_HasVertexAlpha); 504 } 505 if (description.hasColors) { 506 shader.append(gVS_Header_Varyings_HasColors); 507 } 508 if (description.hasGradient) { 509 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]); 510 } 511 if (description.hasBitmap) { 512 shader.append(gVS_Header_Varyings_HasBitmap); 513 } 514 if (description.hasRoundRectClip) { 515 shader.append(gVS_Header_Varyings_HasRoundRectClip); 516 } 517 518 // Begin the shader 519 shader.append(gVS_Main); { 520 if (description.hasTextureTransform) { 521 shader.append(gVS_Main_OutTransformedTexCoords); 522 } else if (description.hasTexture || description.hasExternalTexture) { 523 shader.append(gVS_Main_OutTexCoords); 524 } 525 if (description.hasVertexAlpha) { 526 shader.append(gVS_Main_VertexAlpha); 527 } 528 if (description.hasColors) { 529 shader.append(gVS_Main_OutColors); 530 } 531 if (description.hasBitmap) { 532 shader.append(gVS_Main_OutBitmapTexCoords); 533 } 534 // Output transformed position 535 shader.append(gVS_Main_Position); 536 if (description.hasGradient) { 537 shader.append(gVS_Main_OutGradient[gradientIndex(description)]); 538 } 539 if (description.hasRoundRectClip) { 540 shader.append(gVS_Main_HasRoundRectClip); 541 } 542 } 543 // End the shader 544 shader.append(gVS_Footer); 545 546 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string()); 547 548 return shader; 549} 550 551static bool shaderOp(const ProgramDescription& description, String8& shader, 552 const int modulateOp, const char** snippets) { 553 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp; 554 op = op * 2 + description.hasGammaCorrection; 555 shader.append(snippets[op]); 556 return description.hasAlpha8Texture; 557} 558 559String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) { 560 String8 shader(gFS_Header_Start); 561 562 const bool blendFramebuffer = description.framebufferMode >= SkBlendMode::kPlus; 563 if (blendFramebuffer) { 564 shader.append(gFS_Header_Extension_FramebufferFetch); 565 } 566 if (description.hasExternalTexture 567 || (description.hasBitmap && description.isShaderBitmapExternal)) { 568 shader.append(gFS_Header_Extension_ExternalTexture); 569 } 570 571 shader.append(gFS_Header); 572 573 // Varyings 574 if (description.hasTexture || description.hasExternalTexture) { 575 shader.append(gVS_Header_Varyings_HasTexture); 576 } 577 if (description.hasVertexAlpha) { 578 shader.append(gVS_Header_Varyings_HasVertexAlpha); 579 } 580 if (description.hasColors) { 581 shader.append(gVS_Header_Varyings_HasColors); 582 } 583 if (description.hasGradient) { 584 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]); 585 } 586 if (description.hasBitmap) { 587 shader.append(gVS_Header_Varyings_HasBitmap); 588 } 589 if (description.hasRoundRectClip) { 590 shader.append(gVS_Header_Varyings_HasRoundRectClip); 591 } 592 593 // Uniforms 594 int modulateOp = MODULATE_OP_NO_MODULATE; 595 const bool singleColor = !description.hasTexture && !description.hasExternalTexture && 596 !description.hasGradient && !description.hasBitmap; 597 598 if (description.modulate || singleColor) { 599 shader.append(gFS_Uniforms_Color); 600 if (!singleColor) modulateOp = MODULATE_OP_MODULATE; 601 } 602 if (description.hasTexture || description.useShadowAlphaInterp) { 603 shader.append(gFS_Uniforms_TextureSampler); 604 } else if (description.hasExternalTexture) { 605 shader.append(gFS_Uniforms_ExternalTextureSampler); 606 } 607 if (description.hasGradient) { 608 shader.append(gFS_Uniforms_GradientSampler[description.isSimpleGradient]); 609 } 610 if (description.hasRoundRectClip) { 611 shader.append(gFS_Uniforms_HasRoundRectClip); 612 } 613 614 if (description.hasGammaCorrection) { 615 shader.appendFormat(gFS_Gamma_Preamble, Properties::textGamma, 1.0f / Properties::textGamma); 616 } 617 618 if (description.hasBitmap) { 619 if (description.isShaderBitmapExternal) { 620 shader.append(gFS_Uniforms_BitmapExternalSampler); 621 } else { 622 shader.append(gFS_Uniforms_BitmapSampler); 623 } 624 } 625 shader.append(gFS_Uniforms_ColorOp[static_cast<int>(description.colorOp)]); 626 627 // Generate required functions 628 if (description.hasGradient && description.hasBitmap) { 629 generateBlend(shader, "blendShaders", description.shadersMode); 630 } 631 if (description.colorOp == ProgramDescription::ColorFilterMode::Blend) { 632 generateBlend(shader, "blendColors", description.colorMode); 633 } 634 if (blendFramebuffer) { 635 generateBlend(shader, "blendFramebuffer", description.framebufferMode); 636 } 637 if (description.useShaderBasedWrap) { 638 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT); 639 } 640 if (description.hasGradient || description.hasLinearTexture) { 641 shader.append(gFS_Transfer_Functions); 642 } 643 if (description.hasBitmap || ((description.hasTexture || description.hasExternalTexture) && 644 !description.hasAlpha8Texture)) { 645 shader.append(gFS_OETF[description.hasLinearTexture && !mHasSRGB]); 646 } 647 if (description.hasGradient) { 648 shader.append(gFS_Gradient_Functions); 649 shader.append(gFS_Gradient_Preamble[mHasSRGB]); 650 } 651 652 // Begin the shader 653 shader.append(gFS_Main); { 654 // Stores the result in fragColor directly 655 if (description.hasTexture || description.hasExternalTexture) { 656 if (description.hasAlpha8Texture) { 657 if (!description.hasGradient && !description.hasBitmap) { 658 shader.append( 659 gFS_Main_FetchA8Texture[modulateOp * 2 + description.hasGammaCorrection]); 660 } 661 } else { 662 shader.append(gFS_Main_FetchTexture[modulateOp]); 663 } 664 } else { 665 if (!description.hasGradient && !description.hasBitmap) { 666 shader.append(gFS_Main_FetchColor); 667 } 668 } 669 if (description.hasGradient) { 670 shader.append(gFS_Main_FetchGradient[gradientIndex(description)]); 671 } 672 if (description.hasBitmap) { 673 if (!description.useShaderBasedWrap) { 674 shader.append(gFS_Main_FetchBitmap); 675 } else { 676 shader.append(gFS_Main_FetchBitmapNpot); 677 } 678 } 679 bool applyModulate = false; 680 // Case when we have two shaders set 681 if (description.hasGradient && description.hasBitmap) { 682 if (description.isBitmapFirst) { 683 shader.append(gFS_Main_BlendShadersBG); 684 } else { 685 shader.append(gFS_Main_BlendShadersGB); 686 } 687 applyModulate = shaderOp(description, shader, modulateOp, 688 gFS_Main_BlendShaders_Modulate); 689 } else { 690 if (description.hasGradient) { 691 applyModulate = shaderOp(description, shader, modulateOp, 692 gFS_Main_GradientShader_Modulate); 693 } else if (description.hasBitmap) { 694 applyModulate = shaderOp(description, shader, modulateOp, 695 gFS_Main_BitmapShader_Modulate); 696 } 697 } 698 699 if (description.modulate && applyModulate) { 700 shader.append(gFS_Main_ModulateColor); 701 } 702 703 // Apply the color op if needed 704 shader.append(gFS_Main_ApplyColorOp[static_cast<int>(description.colorOp)]); 705 706 if (description.hasVertexAlpha) { 707 if (description.useShadowAlphaInterp) { 708 shader.append(gFS_Main_ApplyVertexAlphaShadowInterp); 709 } else { 710 shader.append(gFS_Main_ApplyVertexAlphaLinearInterp); 711 } 712 } 713 714 if (description.hasGradient) { 715 shader.append(gFS_Main_AddDither); 716 } 717 718 // Output the fragment 719 if (!blendFramebuffer) { 720 shader.append(gFS_Main_FragColor); 721 } else { 722 shader.append(!description.swapSrcDst ? 723 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap); 724 } 725 if (description.hasColors) { 726 shader.append(gFS_Main_FragColor_HasColors); 727 } 728 if (description.hasRoundRectClip) { 729 shader.append(gFS_Main_FragColor_HasRoundRectClip); 730 } 731 if (description.hasDebugHighlight) { 732 shader.append(gFS_Main_DebugHighlight); 733 } 734 } 735 // End the shader 736 shader.append(gFS_Footer); 737 738#if DEBUG_PROGRAMS 739 PROGRAM_LOGD("*** Generated fragment shader:\n\n"); 740 printLongString(shader); 741#endif 742 743 return shader; 744} 745 746void ProgramCache::generateBlend(String8& shader, const char* name, SkBlendMode mode) { 747 shader.append("\nvec4 "); 748 shader.append(name); 749 shader.append("(vec4 src, vec4 dst) {\n"); 750 shader.append(" "); 751 shader.append(gBlendOps[(int)mode]); 752 shader.append("}\n"); 753} 754 755void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) { 756 shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n"); 757 if (wrapS == GL_MIRRORED_REPEAT) { 758 shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n"); 759 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n"); 760 } 761 if (wrapT == GL_MIRRORED_REPEAT) { 762 shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n"); 763 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n"); 764 } 765 shader.append(" return vec2("); 766 switch (wrapS) { 767 case GL_CLAMP_TO_EDGE: 768 shader.append("texCoords.x"); 769 break; 770 case GL_REPEAT: 771 shader.append("mod(texCoords.x, 1.0)"); 772 break; 773 case GL_MIRRORED_REPEAT: 774 shader.append("xMod2"); 775 break; 776 } 777 shader.append(", "); 778 switch (wrapT) { 779 case GL_CLAMP_TO_EDGE: 780 shader.append("texCoords.y"); 781 break; 782 case GL_REPEAT: 783 shader.append("mod(texCoords.y, 1.0)"); 784 break; 785 case GL_MIRRORED_REPEAT: 786 shader.append("yMod2"); 787 break; 788 } 789 shader.append(");\n"); 790 shader.append("}\n"); 791} 792 793void ProgramCache::printLongString(const String8& shader) const { 794 ssize_t index = 0; 795 ssize_t lastIndex = 0; 796 const char* str = shader.string(); 797 while ((index = shader.find("\n", index)) > -1) { 798 String8 line(str, index - lastIndex); 799 if (line.length() == 0) line.append("\n"); 800 ALOGD("%s", line.string()); 801 index++; 802 str += (index - lastIndex); 803 lastIndex = index; 804 } 805} 806 807}; // namespace uirenderer 808}; // namespace android 809