ProgramCache.cpp revision 9fe7e16399aa9739b63ce9add1d04fd8ef00678f
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 164// Dithering must be done in the quantization space 165// When we are writing to an sRGB framebuffer, we must do the following: 166// EOCF(OECF(color) + dither) 167// The dithering pattern is generated with a triangle noise generator in the range [-0.0,1.0] 168// TODO: Handle linear fp16 render targets 169const char* gFS_Gradient_Functions = R"__SHADER__( 170 float triangleNoise(const highp vec2 n) { 171 highp vec2 p = fract(n * vec2(5.3987, 5.4421)); 172 p += dot(p.yx, p.xy + vec2(21.5351, 14.3137)); 173 highp float xy = p.x * p.y; 174 return fract(xy * 95.4307) + fract(xy * 75.04961) - 1.0; 175 } 176 177 float OECF_sRGB(const float linear) { 178 // IEC 61966-2-1:1999 179 return linear <= 0.0031308 ? linear * 12.92 : (pow(linear, 1.0 / 2.4) * 1.055) - 0.055; 180 } 181 182 vec3 OECF_sRGB(const vec3 linear) { 183 return vec3(OECF_sRGB(linear.r), OECF_sRGB(linear.g), OECF_sRGB(linear.b)); 184 } 185 186 float EOCF_sRGB(float srgb) { 187 // IEC 61966-2-1:1999 188 return srgb <= 0.04045 ? srgb / 12.92 : pow((srgb + 0.055) / 1.055, 2.4); 189 } 190)__SHADER__"; 191const char* gFS_Gradient_Preamble[2] = { 192 // Linear framebuffer 193 "\nvec4 dither(const vec4 color) {\n" 194 " return vec4(color.rgb + (triangleNoise(gl_FragCoord.xy * screenSize.xy) / 255.0), color.a);\n" 195 "}\n" 196 "\nvec4 gammaMix(const vec4 a, const vec4 b, float v) {\n" 197 " vec4 c = mix(a, b, v);\n" 198 " c.a = EOCF_sRGB(c.a);\n" // This is technically incorrect but preserves compatibility 199 " return vec4(OECF_sRGB(c.rgb) * c.a, c.a);\n" 200 "}\n", 201 // sRGB framebuffer 202 "\nvec4 dither(const vec4 color) {\n" 203 " vec3 dithered = sqrt(color.rgb) + (triangleNoise(gl_FragCoord.xy * screenSize.xy) / 255.0);\n" 204 " return vec4(dithered * dithered, color.a);\n" 205 "}\n" 206 "\nvec4 gammaMix(const vec4 a, const vec4 b, float v) {\n" 207 " vec4 c = mix(a, b, v);\n" 208 " return vec4(c.rgb * c.a, c.a);\n" 209 "}\n" 210}; 211 212// Uses luminance coefficients from Rec.709 to choose the appropriate gamma 213// The gamma() function assumes that bright text will be displayed on a dark 214// background and that dark text will be displayed on bright background 215// The gamma coefficient is chosen to thicken or thin the text accordingly 216// The dot product used to compute the luminance could be approximated with 217// a simple max(color.r, color.g, color.b) 218const char* gFS_Gamma_Preamble = R"__SHADER__( 219 #define GAMMA (%.2f) 220 #define GAMMA_INV (%.2f) 221 222 float gamma(float a, const vec3 color) { 223 float luminance = dot(color, vec3(0.2126, 0.7152, 0.0722)); 224 return pow(a, luminance < 0.5 ? GAMMA_INV : GAMMA); 225 } 226)__SHADER__"; 227 228const char* gFS_Main = 229 "\nvoid main(void) {\n" 230 " vec4 fragColor;\n"; 231 232const char* gFS_Main_AddDither = 233 " fragColor = dither(fragColor);\n"; 234 235// Fast cases 236const char* gFS_Fast_SingleColor = 237 "\nvoid main(void) {\n" 238 " gl_FragColor = color;\n" 239 "}\n\n"; 240const char* gFS_Fast_SingleTexture = 241 "\nvoid main(void) {\n" 242 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n" 243 "}\n\n"; 244const char* gFS_Fast_SingleModulateTexture = 245 "\nvoid main(void) {\n" 246 " gl_FragColor = color.a * texture2D(baseSampler, outTexCoords);\n" 247 "}\n\n"; 248const char* gFS_Fast_SingleA8Texture = 249 "\nvoid main(void) {\n" 250 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n" 251 "}\n\n"; 252const char* gFS_Fast_SingleA8Texture_ApplyGamma = 253 "\nvoid main(void) {\n" 254 " gl_FragColor = vec4(0.0, 0.0, 0.0, pow(texture2D(baseSampler, outTexCoords).a, GAMMA));\n" 255 "}\n\n"; 256const char* gFS_Fast_SingleModulateA8Texture = 257 "\nvoid main(void) {\n" 258 " gl_FragColor = color * texture2D(baseSampler, outTexCoords).a;\n" 259 "}\n\n"; 260const char* gFS_Fast_SingleModulateA8Texture_ApplyGamma = 261 "\nvoid main(void) {\n" 262 " gl_FragColor = color * gamma(texture2D(baseSampler, outTexCoords).a, color.rgb);\n" 263 "}\n\n"; 264const char* gFS_Fast_SingleGradient[2] = { 265 "\nvoid main(void) {\n" 266 " gl_FragColor = dither(texture2D(gradientSampler, linear));\n" 267 "}\n\n", 268 "\nvoid main(void) {\n" 269 " gl_FragColor = dither(gammaMix(startColor, endColor, clamp(linear, 0.0, 1.0)));\n" 270 "}\n\n", 271}; 272const char* gFS_Fast_SingleModulateGradient[2] = { 273 "\nvoid main(void) {\n" 274 " gl_FragColor = dither(color.a * texture2D(gradientSampler, linear));\n" 275 "}\n\n", 276 "\nvoid main(void) {\n" 277 " gl_FragColor = dither(color.a * gammaMix(startColor, endColor, clamp(linear, 0.0, 1.0)));\n" 278 "}\n\n" 279}; 280 281// General case 282const char* gFS_Main_FetchColor = 283 " fragColor = color;\n"; 284const char* gFS_Main_ModulateColor = 285 " fragColor *= color.a;\n"; 286const char* gFS_Main_ApplyVertexAlphaLinearInterp = 287 " fragColor *= alpha;\n"; 288const char* gFS_Main_ApplyVertexAlphaShadowInterp = 289 // map alpha through shadow alpha sampler 290 " fragColor *= texture2D(baseSampler, vec2(alpha, 0.5)).a;\n"; 291const char* gFS_Main_FetchTexture[2] = { 292 // Don't modulate 293 " fragColor = texture2D(baseSampler, outTexCoords);\n", 294 // Modulate 295 " fragColor = color * texture2D(baseSampler, outTexCoords);\n" 296}; 297const char* gFS_Main_FetchA8Texture[4] = { 298 // Don't modulate 299 " fragColor = texture2D(baseSampler, outTexCoords);\n", 300 " fragColor = texture2D(baseSampler, outTexCoords);\n", 301 // Modulate 302 " fragColor = color * texture2D(baseSampler, outTexCoords).a;\n", 303 " fragColor = color * gamma(texture2D(baseSampler, outTexCoords).a, color.rgb);\n", 304}; 305const char* gFS_Main_FetchGradient[6] = { 306 // Linear 307 " vec4 gradientColor = texture2D(gradientSampler, linear);\n", 308 309 " vec4 gradientColor = gammaMix(startColor, endColor, clamp(linear, 0.0, 1.0));\n", 310 311 // Circular 312 " vec4 gradientColor = texture2D(gradientSampler, vec2(length(circular), 0.5));\n", 313 314 " vec4 gradientColor = gammaMix(startColor, endColor, clamp(length(circular), 0.0, 1.0));\n", 315 316 // Sweep 317 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n" 318 " vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n", 319 320 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n" 321 " vec4 gradientColor = gammaMix(startColor, endColor, clamp(index - floor(index), 0.0, 1.0));\n" 322}; 323const char* gFS_Main_FetchBitmap = 324 " vec4 bitmapColor = texture2D(bitmapSampler, outBitmapTexCoords);\n"; 325const char* gFS_Main_FetchBitmapNpot = 326 " vec4 bitmapColor = texture2D(bitmapSampler, wrap(outBitmapTexCoords));\n"; 327const char* gFS_Main_BlendShadersBG = 328 " fragColor = blendShaders(gradientColor, bitmapColor)"; 329const char* gFS_Main_BlendShadersGB = 330 " fragColor = blendShaders(bitmapColor, gradientColor)"; 331const char* gFS_Main_BlendShaders_Modulate[6] = { 332 // Don't modulate 333 ";\n", 334 ";\n", 335 // Modulate 336 " * color.a;\n", 337 " * color.a;\n", 338 // Modulate with alpha 8 texture 339 " * texture2D(baseSampler, outTexCoords).a;\n", 340 " * gamma(texture2D(baseSampler, outTexCoords).a, color.rgb);\n", 341}; 342const char* gFS_Main_GradientShader_Modulate[6] = { 343 // Don't modulate 344 " fragColor = gradientColor;\n", 345 " fragColor = gradientColor;\n", 346 // Modulate 347 " fragColor = gradientColor * color.a;\n", 348 " fragColor = gradientColor * color.a;\n", 349 // Modulate with alpha 8 texture 350 " fragColor = gradientColor * texture2D(baseSampler, outTexCoords).a;\n", 351 " fragColor = gradientColor * gamma(texture2D(baseSampler, outTexCoords).a, gradientColor.rgb);\n", 352 }; 353const char* gFS_Main_BitmapShader_Modulate[6] = { 354 // Don't modulate 355 " fragColor = bitmapColor;\n", 356 " fragColor = bitmapColor;\n", 357 // Modulate 358 " fragColor = bitmapColor * color.a;\n", 359 " fragColor = bitmapColor * color.a;\n", 360 // Modulate with alpha 8 texture 361 " fragColor = bitmapColor * texture2D(baseSampler, outTexCoords).a;\n", 362 " fragColor = bitmapColor * gamma(texture2D(baseSampler, outTexCoords).a, bitmapColor.rgb);\n", 363 }; 364const char* gFS_Main_FragColor = 365 " gl_FragColor = fragColor;\n"; 366const char* gFS_Main_FragColor_HasColors = 367 " gl_FragColor *= outColors;\n"; 368const char* gFS_Main_FragColor_Blend = 369 " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n"; 370const char* gFS_Main_FragColor_Blend_Swap = 371 " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n"; 372const char* gFS_Main_ApplyColorOp[3] = { 373 // None 374 "", 375 // Matrix 376 " fragColor.rgb /= (fragColor.a + 0.0019);\n" // un-premultiply 377 " fragColor *= colorMatrix;\n" 378 " fragColor += colorMatrixVector;\n" 379 " fragColor.rgb *= (fragColor.a + 0.0019);\n", // re-premultiply 380 // PorterDuff 381 " fragColor = blendColors(colorBlend, fragColor);\n" 382}; 383 384// Note: LTRB -> xyzw 385const char* gFS_Main_FragColor_HasRoundRectClip = 386 " mediump vec2 fragToLT = roundRectInnerRectLTRB.xy - roundRectPos;\n" 387 " mediump vec2 fragFromRB = roundRectPos - roundRectInnerRectLTRB.zw;\n" 388 389 // divide + multiply by 128 to avoid falling out of range in length() function 390 " mediump vec2 dist = max(max(fragToLT, fragFromRB), vec2(0.0, 0.0)) / 128.0;\n" 391 " mediump float linearDist = roundRectRadius - (length(dist) * 128.0);\n" 392 " gl_FragColor *= clamp(linearDist, 0.0, 1.0);\n"; 393 394const char* gFS_Main_DebugHighlight = 395 " gl_FragColor.rgb = vec3(0.0, gl_FragColor.a, 0.0);\n"; 396const char* gFS_Footer = 397 "}\n\n"; 398 399/////////////////////////////////////////////////////////////////////////////// 400// PorterDuff snippets 401/////////////////////////////////////////////////////////////////////////////// 402 403const char* gBlendOps[18] = { 404 // Clear 405 "return vec4(0.0, 0.0, 0.0, 0.0);\n", 406 // Src 407 "return src;\n", 408 // Dst 409 "return dst;\n", 410 // SrcOver 411 "return src + dst * (1.0 - src.a);\n", 412 // DstOver 413 "return dst + src * (1.0 - dst.a);\n", 414 // SrcIn 415 "return src * dst.a;\n", 416 // DstIn 417 "return dst * src.a;\n", 418 // SrcOut 419 "return src * (1.0 - dst.a);\n", 420 // DstOut 421 "return dst * (1.0 - src.a);\n", 422 // SrcAtop 423 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n", 424 // DstAtop 425 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n", 426 // Xor 427 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, " 428 "src.a + dst.a - 2.0 * src.a * dst.a);\n", 429 // Plus 430 "return min(src + dst, 1.0);\n", 431 // Modulate 432 "return src * dst;\n", 433 // Screen 434 "return src + dst - src * dst;\n", 435 // Overlay 436 "return clamp(vec4(mix(" 437 "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), " 438 "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), " 439 "step(dst.a, 2.0 * dst.rgb)), " 440 "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n", 441 // Darken 442 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + " 443 "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n", 444 // Lighten 445 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + " 446 "max(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n", 447}; 448 449/////////////////////////////////////////////////////////////////////////////// 450// Constructors/destructors 451/////////////////////////////////////////////////////////////////////////////// 452 453ProgramCache::ProgramCache(Extensions& extensions) 454 : mHasES3(extensions.getMajorGlVersion() >= 3) 455 , mHasSRGB(extensions.hasSRGB()) { 456} 457 458ProgramCache::~ProgramCache() { 459 clear(); 460} 461 462/////////////////////////////////////////////////////////////////////////////// 463// Cache management 464/////////////////////////////////////////////////////////////////////////////// 465 466void ProgramCache::clear() { 467 PROGRAM_LOGD("Clearing program cache"); 468 mCache.clear(); 469} 470 471Program* ProgramCache::get(const ProgramDescription& description) { 472 programid key = description.key(); 473 if (key == (PROGRAM_KEY_TEXTURE | PROGRAM_KEY_A8_TEXTURE)) { 474 // program for A8, unmodulated, texture w/o shader (black text/path textures) is equivalent 475 // to standard texture program (bitmaps, patches). Consider them equivalent. 476 key = PROGRAM_KEY_TEXTURE; 477 } 478 479 auto iter = mCache.find(key); 480 Program* program = nullptr; 481 if (iter == mCache.end()) { 482 description.log("Could not find program"); 483 program = generateProgram(description, key); 484 mCache[key] = std::unique_ptr<Program>(program); 485 } else { 486 program = iter->second.get(); 487 } 488 return program; 489} 490 491/////////////////////////////////////////////////////////////////////////////// 492// Program generation 493/////////////////////////////////////////////////////////////////////////////// 494 495Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) { 496 String8 vertexShader = generateVertexShader(description); 497 String8 fragmentShader = generateFragmentShader(description); 498 499 return new Program(description, vertexShader.string(), fragmentShader.string()); 500} 501 502static inline size_t gradientIndex(const ProgramDescription& description) { 503 return description.gradientType * 2 + description.isSimpleGradient; 504} 505 506String8 ProgramCache::generateVertexShader(const ProgramDescription& description) { 507 // Add attributes 508 String8 shader(gVS_Header_Start); 509 if (description.hasTexture || description.hasExternalTexture) { 510 shader.append(gVS_Header_Attributes_TexCoords); 511 } 512 if (description.hasVertexAlpha) { 513 shader.append(gVS_Header_Attributes_VertexAlphaParameters); 514 } 515 if (description.hasColors) { 516 shader.append(gVS_Header_Attributes_Colors); 517 } 518 // Uniforms 519 shader.append(gVS_Header_Uniforms); 520 if (description.hasTextureTransform) { 521 shader.append(gVS_Header_Uniforms_TextureTransform); 522 } 523 if (description.hasGradient) { 524 shader.append(gVS_Header_Uniforms_HasGradient); 525 } 526 if (description.hasBitmap) { 527 shader.append(gVS_Header_Uniforms_HasBitmap); 528 } 529 if (description.hasRoundRectClip) { 530 shader.append(gVS_Header_Uniforms_HasRoundRectClip); 531 } 532 // Varyings 533 if (description.hasTexture || description.hasExternalTexture) { 534 shader.append(gVS_Header_Varyings_HasTexture); 535 } 536 if (description.hasVertexAlpha) { 537 shader.append(gVS_Header_Varyings_HasVertexAlpha); 538 } 539 if (description.hasColors) { 540 shader.append(gVS_Header_Varyings_HasColors); 541 } 542 if (description.hasGradient) { 543 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]); 544 } 545 if (description.hasBitmap) { 546 shader.append(gVS_Header_Varyings_HasBitmap); 547 } 548 if (description.hasRoundRectClip) { 549 shader.append(gVS_Header_Varyings_HasRoundRectClip); 550 } 551 552 // Begin the shader 553 shader.append(gVS_Main); { 554 if (description.hasTextureTransform) { 555 shader.append(gVS_Main_OutTransformedTexCoords); 556 } else if (description.hasTexture || description.hasExternalTexture) { 557 shader.append(gVS_Main_OutTexCoords); 558 } 559 if (description.hasVertexAlpha) { 560 shader.append(gVS_Main_VertexAlpha); 561 } 562 if (description.hasColors) { 563 shader.append(gVS_Main_OutColors); 564 } 565 if (description.hasBitmap) { 566 shader.append(gVS_Main_OutBitmapTexCoords); 567 } 568 // Output transformed position 569 shader.append(gVS_Main_Position); 570 if (description.hasGradient) { 571 shader.append(gVS_Main_OutGradient[gradientIndex(description)]); 572 } 573 if (description.hasRoundRectClip) { 574 shader.append(gVS_Main_HasRoundRectClip); 575 } 576 } 577 // End the shader 578 shader.append(gVS_Footer); 579 580 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string()); 581 582 return shader; 583} 584 585static bool shaderOp(const ProgramDescription& description, String8& shader, 586 const int modulateOp, const char** snippets) { 587 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp; 588 op = op * 2 + description.hasGammaCorrection; 589 shader.append(snippets[op]); 590 return description.hasAlpha8Texture; 591} 592 593String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) { 594 String8 shader(gFS_Header_Start); 595 596 const bool blendFramebuffer = description.framebufferMode >= SkBlendMode::kPlus; 597 if (blendFramebuffer) { 598 shader.append(gFS_Header_Extension_FramebufferFetch); 599 } 600 if (description.hasExternalTexture 601 || (description.hasBitmap && description.isShaderBitmapExternal)) { 602 shader.append(gFS_Header_Extension_ExternalTexture); 603 } 604 605 shader.append(gFS_Header); 606 607 // Varyings 608 if (description.hasTexture || description.hasExternalTexture) { 609 shader.append(gVS_Header_Varyings_HasTexture); 610 } 611 if (description.hasVertexAlpha) { 612 shader.append(gVS_Header_Varyings_HasVertexAlpha); 613 } 614 if (description.hasColors) { 615 shader.append(gVS_Header_Varyings_HasColors); 616 } 617 if (description.hasGradient) { 618 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]); 619 } 620 if (description.hasBitmap) { 621 shader.append(gVS_Header_Varyings_HasBitmap); 622 } 623 if (description.hasRoundRectClip) { 624 shader.append(gVS_Header_Varyings_HasRoundRectClip); 625 } 626 627 // Uniforms 628 int modulateOp = MODULATE_OP_NO_MODULATE; 629 const bool singleColor = !description.hasTexture && !description.hasExternalTexture && 630 !description.hasGradient && !description.hasBitmap; 631 632 if (description.modulate || singleColor) { 633 shader.append(gFS_Uniforms_Color); 634 if (!singleColor) modulateOp = MODULATE_OP_MODULATE; 635 } 636 if (description.hasTexture || description.useShadowAlphaInterp) { 637 shader.append(gFS_Uniforms_TextureSampler); 638 } else if (description.hasExternalTexture) { 639 shader.append(gFS_Uniforms_ExternalTextureSampler); 640 } 641 if (description.hasGradient) { 642 shader.append(gFS_Uniforms_GradientSampler[description.isSimpleGradient]); 643 } 644 if (description.hasRoundRectClip) { 645 shader.append(gFS_Uniforms_HasRoundRectClip); 646 } 647 648 if (description.hasGammaCorrection) { 649 shader.appendFormat(gFS_Gamma_Preamble, Properties::textGamma, 1.0f / Properties::textGamma); 650 } 651 652 // Optimization for common cases 653 if (!description.hasVertexAlpha 654 && !blendFramebuffer 655 && !description.hasColors 656 && description.colorOp == ProgramDescription::ColorFilterMode::None 657 && !description.hasDebugHighlight 658 && !description.hasRoundRectClip) { 659 bool fast = false; 660 661 const bool noShader = !description.hasGradient && !description.hasBitmap; 662 const bool singleTexture = (description.hasTexture || description.hasExternalTexture) && 663 !description.hasAlpha8Texture && noShader; 664 const bool singleA8Texture = description.hasTexture && 665 description.hasAlpha8Texture && noShader; 666 const bool singleGradient = !description.hasTexture && !description.hasExternalTexture && 667 description.hasGradient && !description.hasBitmap && 668 description.gradientType == ProgramDescription::kGradientLinear; 669 670 if (singleColor) { 671 shader.append(gFS_Fast_SingleColor); 672 fast = true; 673 } else if (singleTexture) { 674 if (!description.modulate) { 675 shader.append(gFS_Fast_SingleTexture); 676 } else { 677 shader.append(gFS_Fast_SingleModulateTexture); 678 } 679 fast = true; 680 } else if (singleA8Texture) { 681 if (!description.modulate) { 682 if (description.hasGammaCorrection) { 683 shader.append(gFS_Fast_SingleA8Texture_ApplyGamma); 684 } else { 685 shader.append(gFS_Fast_SingleA8Texture); 686 } 687 } else { 688 if (description.hasGammaCorrection) { 689 shader.append(gFS_Fast_SingleModulateA8Texture_ApplyGamma); 690 } else { 691 shader.append(gFS_Fast_SingleModulateA8Texture); 692 } 693 } 694 fast = true; 695 } else if (singleGradient) { 696 shader.append(gFS_Gradient_Functions); 697 shader.append(gFS_Gradient_Preamble[mHasSRGB]); 698 if (!description.modulate) { 699 shader.append(gFS_Fast_SingleGradient[description.isSimpleGradient]); 700 } else { 701 shader.append(gFS_Fast_SingleModulateGradient[description.isSimpleGradient]); 702 } 703 fast = true; 704 } 705 706 if (fast) { 707#if DEBUG_PROGRAMS 708 PROGRAM_LOGD("*** Fast case:\n"); 709 PROGRAM_LOGD("*** Generated fragment shader:\n\n"); 710 printLongString(shader); 711#endif 712 713 return shader; 714 } 715 } 716 717 if (description.hasBitmap) { 718 if (description.isShaderBitmapExternal) { 719 shader.append(gFS_Uniforms_BitmapExternalSampler); 720 } else { 721 shader.append(gFS_Uniforms_BitmapSampler); 722 } 723 } 724 shader.append(gFS_Uniforms_ColorOp[static_cast<int>(description.colorOp)]); 725 726 // Generate required functions 727 if (description.hasGradient && description.hasBitmap) { 728 generateBlend(shader, "blendShaders", description.shadersMode); 729 } 730 if (description.colorOp == ProgramDescription::ColorFilterMode::Blend) { 731 generateBlend(shader, "blendColors", description.colorMode); 732 } 733 if (blendFramebuffer) { 734 generateBlend(shader, "blendFramebuffer", description.framebufferMode); 735 } 736 if (description.useShaderBasedWrap) { 737 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT); 738 } 739 if (description.hasGradient) { 740 shader.append(gFS_Gradient_Functions); 741 shader.append(gFS_Gradient_Preamble[mHasSRGB]); 742 } 743 744 // Begin the shader 745 shader.append(gFS_Main); { 746 // Stores the result in fragColor directly 747 if (description.hasTexture || description.hasExternalTexture) { 748 if (description.hasAlpha8Texture) { 749 if (!description.hasGradient && !description.hasBitmap) { 750 shader.append( 751 gFS_Main_FetchA8Texture[modulateOp * 2 + description.hasGammaCorrection]); 752 } 753 } else { 754 shader.append(gFS_Main_FetchTexture[modulateOp]); 755 } 756 } else { 757 if (!description.hasGradient && !description.hasBitmap) { 758 shader.append(gFS_Main_FetchColor); 759 } 760 } 761 if (description.hasGradient) { 762 shader.append(gFS_Main_FetchGradient[gradientIndex(description)]); 763 } 764 if (description.hasBitmap) { 765 if (!description.useShaderBasedWrap) { 766 shader.append(gFS_Main_FetchBitmap); 767 } else { 768 shader.append(gFS_Main_FetchBitmapNpot); 769 } 770 } 771 bool applyModulate = false; 772 // Case when we have two shaders set 773 if (description.hasGradient && description.hasBitmap) { 774 if (description.isBitmapFirst) { 775 shader.append(gFS_Main_BlendShadersBG); 776 } else { 777 shader.append(gFS_Main_BlendShadersGB); 778 } 779 applyModulate = shaderOp(description, shader, modulateOp, 780 gFS_Main_BlendShaders_Modulate); 781 } else { 782 if (description.hasGradient) { 783 applyModulate = shaderOp(description, shader, modulateOp, 784 gFS_Main_GradientShader_Modulate); 785 } else if (description.hasBitmap) { 786 applyModulate = shaderOp(description, shader, modulateOp, 787 gFS_Main_BitmapShader_Modulate); 788 } 789 } 790 791 if (description.modulate && applyModulate) { 792 shader.append(gFS_Main_ModulateColor); 793 } 794 795 // Apply the color op if needed 796 shader.append(gFS_Main_ApplyColorOp[static_cast<int>(description.colorOp)]); 797 798 if (description.hasVertexAlpha) { 799 if (description.useShadowAlphaInterp) { 800 shader.append(gFS_Main_ApplyVertexAlphaShadowInterp); 801 } else { 802 shader.append(gFS_Main_ApplyVertexAlphaLinearInterp); 803 } 804 } 805 806 if (description.hasGradient) { 807 shader.append(gFS_Main_AddDither); 808 } 809 810 // Output the fragment 811 if (!blendFramebuffer) { 812 shader.append(gFS_Main_FragColor); 813 } else { 814 shader.append(!description.swapSrcDst ? 815 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap); 816 } 817 if (description.hasColors) { 818 shader.append(gFS_Main_FragColor_HasColors); 819 } 820 if (description.hasRoundRectClip) { 821 shader.append(gFS_Main_FragColor_HasRoundRectClip); 822 } 823 if (description.hasDebugHighlight) { 824 shader.append(gFS_Main_DebugHighlight); 825 } 826 } 827 // End the shader 828 shader.append(gFS_Footer); 829 830#if DEBUG_PROGRAMS 831 PROGRAM_LOGD("*** Generated fragment shader:\n\n"); 832 printLongString(shader); 833#endif 834 835 return shader; 836} 837 838void ProgramCache::generateBlend(String8& shader, const char* name, SkBlendMode mode) { 839 shader.append("\nvec4 "); 840 shader.append(name); 841 shader.append("(vec4 src, vec4 dst) {\n"); 842 shader.append(" "); 843 shader.append(gBlendOps[(int)mode]); 844 shader.append("}\n"); 845} 846 847void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) { 848 shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n"); 849 if (wrapS == GL_MIRRORED_REPEAT) { 850 shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n"); 851 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n"); 852 } 853 if (wrapT == GL_MIRRORED_REPEAT) { 854 shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n"); 855 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n"); 856 } 857 shader.append(" return vec2("); 858 switch (wrapS) { 859 case GL_CLAMP_TO_EDGE: 860 shader.append("texCoords.x"); 861 break; 862 case GL_REPEAT: 863 shader.append("mod(texCoords.x, 1.0)"); 864 break; 865 case GL_MIRRORED_REPEAT: 866 shader.append("xMod2"); 867 break; 868 } 869 shader.append(", "); 870 switch (wrapT) { 871 case GL_CLAMP_TO_EDGE: 872 shader.append("texCoords.y"); 873 break; 874 case GL_REPEAT: 875 shader.append("mod(texCoords.y, 1.0)"); 876 break; 877 case GL_MIRRORED_REPEAT: 878 shader.append("yMod2"); 879 break; 880 } 881 shader.append(");\n"); 882 shader.append("}\n"); 883} 884 885void ProgramCache::printLongString(const String8& shader) const { 886 ssize_t index = 0; 887 ssize_t lastIndex = 0; 888 const char* str = shader.string(); 889 while ((index = shader.find("\n", index)) > -1) { 890 String8 line(str, index - lastIndex); 891 if (line.length() == 0) line.append("\n"); 892 ALOGD("%s", line.string()); 893 index++; 894 str += (index - lastIndex); 895 lastIndex = index; 896 } 897} 898 899}; // namespace uirenderer 900}; // namespace android 901