ProgramCache.cpp revision 99585adeb4167ca357a72eb866f34c1af944f4b9
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 "ProgramCache.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/////////////////////////////////////////////////////////////////////////////// 35// Vertex shaders snippets 36/////////////////////////////////////////////////////////////////////////////// 37 38const char* gVS_Header_Attributes = 39 "attribute vec4 position;\n"; 40const char* gVS_Header_Attributes_TexCoords = 41 "attribute vec2 texCoords;\n"; 42const char* gVS_Header_Attributes_AAParameters = 43 "attribute float vtxWidth;\n" 44 "attribute float vtxLength;\n"; 45const char* gVS_Header_Uniforms_TextureTransform = 46 "uniform mat4 mainTextureTransform;\n"; 47const char* gVS_Header_Uniforms = 48 "uniform mat4 transform;\n"; 49const char* gVS_Header_Uniforms_IsPoint = 50 "uniform mediump float pointSize;\n"; 51const char* gVS_Header_Uniforms_HasGradient[3] = { 52 // Linear 53 "uniform mat4 screenSpace;\n", 54 // Circular 55 "uniform mat4 screenSpace;\n", 56 // Sweep 57 "uniform mat4 screenSpace;\n" 58}; 59const char* gVS_Header_Uniforms_HasBitmap = 60 "uniform mat4 textureTransform;\n" 61 "uniform mediump vec2 textureDimension;\n"; 62const char* gVS_Header_Varyings_HasTexture = 63 "varying vec2 outTexCoords;\n"; 64const char* gVS_Header_Varyings_IsAA = 65 "varying float widthProportion;\n" 66 "varying float lengthProportion;\n"; 67const char* gVS_Header_Varyings_HasBitmap = 68 "varying vec2 outBitmapTexCoords;\n"; 69const char* gVS_Header_Varyings_PointHasBitmap = 70 "varying vec2 outPointBitmapTexCoords;\n"; 71const char* gVS_Header_Varyings_HasGradient[3] = { 72 // Linear 73 "varying vec2 linear;\n", 74 // Circular 75 "varying vec2 circular;\n", 76 // Sweep 77 "varying vec2 sweep;\n" 78}; 79const char* gVS_Main = 80 "\nvoid main(void) {\n"; 81const char* gVS_Main_OutTexCoords = 82 " outTexCoords = texCoords;\n"; 83const char* gVS_Main_OutTransformedTexCoords = 84 " outTexCoords = (mainTextureTransform * vec4(texCoords, 0.0, 1.0)).xy;\n"; 85const char* gVS_Main_OutGradient[3] = { 86 // Linear 87 " linear = vec2((screenSpace * position).x, 0.5);\n", 88 // Circular 89 " circular = (screenSpace * position).xy;\n", 90 // Sweep 91 " sweep = (screenSpace * position).xy;\n" 92}; 93const char* gVS_Main_OutBitmapTexCoords = 94 " outBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n"; 95const char* gVS_Main_OutPointBitmapTexCoords = 96 " outPointBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n"; 97const char* gVS_Main_Position = 98 " gl_Position = transform * position;\n"; 99const char* gVS_Main_PointSize = 100 " gl_PointSize = pointSize;\n"; 101const char* gVS_Main_AA = 102 " widthProportion = vtxWidth;\n" 103 " lengthProportion = vtxLength;\n"; 104const char* gVS_Footer = 105 "}\n\n"; 106 107/////////////////////////////////////////////////////////////////////////////// 108// Fragment shaders snippets 109/////////////////////////////////////////////////////////////////////////////// 110 111const char* gFS_Header_Extension_FramebufferFetch = 112 "#extension GL_NV_shader_framebuffer_fetch : enable\n\n"; 113const char* gFS_Header_Extension_ExternalTexture = 114 "#extension GL_OES_EGL_image_external : require\n\n"; 115const char* gFS_Header = 116 "precision mediump float;\n\n"; 117const char* gFS_Uniforms_Color = 118 "uniform vec4 color;\n"; 119const char* gFS_Uniforms_AA = 120 "uniform float boundaryWidth;\n" 121 "uniform float inverseBoundaryWidth;\n" 122 "uniform float boundaryLength;\n" 123 "uniform float inverseBoundaryLength;\n"; 124const char* gFS_Header_Uniforms_PointHasBitmap = 125 "uniform vec2 textureDimension;\n" 126 "uniform float pointSize;\n"; 127const char* gFS_Uniforms_TextureSampler = 128 "uniform sampler2D sampler;\n"; 129const char* gFS_Uniforms_ExternalTextureSampler = 130 "uniform samplerExternalOES sampler;\n"; 131const char* gFS_Uniforms_GradientSampler[3] = { 132 // Linear 133 "uniform sampler2D gradientSampler;\n", 134 // Circular 135 "uniform sampler2D gradientSampler;\n", 136 // Sweep 137 "uniform sampler2D gradientSampler;\n" 138}; 139const char* gFS_Uniforms_BitmapSampler = 140 "uniform sampler2D bitmapSampler;\n"; 141const char* gFS_Uniforms_ColorOp[4] = { 142 // None 143 "", 144 // Matrix 145 "uniform mat4 colorMatrix;\n" 146 "uniform vec4 colorMatrixVector;\n", 147 // Lighting 148 "uniform vec4 lightingMul;\n" 149 "uniform vec4 lightingAdd;\n", 150 // PorterDuff 151 "uniform vec4 colorBlend;\n" 152}; 153const char* gFS_Main = 154 "\nvoid main(void) {\n" 155 " lowp vec4 fragColor;\n"; 156 157const char* gFS_Main_PointBitmapTexCoords = 158 " vec2 outBitmapTexCoords = outPointBitmapTexCoords + " 159 "((gl_PointCoord - vec2(0.5, 0.5)) * textureDimension * vec2(pointSize, pointSize));\n"; 160 161// Fast cases 162const char* gFS_Fast_SingleColor = 163 "\nvoid main(void) {\n" 164 " gl_FragColor = color;\n" 165 "}\n\n"; 166const char* gFS_Fast_SingleTexture = 167 "\nvoid main(void) {\n" 168 " gl_FragColor = texture2D(sampler, outTexCoords);\n" 169 "}\n\n"; 170const char* gFS_Fast_SingleModulateTexture = 171 "\nvoid main(void) {\n" 172 " gl_FragColor = color.a * texture2D(sampler, outTexCoords);\n" 173 "}\n\n"; 174const char* gFS_Fast_SingleA8Texture = 175 "\nvoid main(void) {\n" 176 " gl_FragColor = texture2D(sampler, outTexCoords);\n" 177 "}\n\n"; 178const char* gFS_Fast_SingleModulateA8Texture = 179 "\nvoid main(void) {\n" 180 " gl_FragColor = color * texture2D(sampler, outTexCoords).a;\n" 181 "}\n\n"; 182const char* gFS_Fast_SingleGradient = 183 "\nvoid main(void) {\n" 184 " gl_FragColor = texture2D(gradientSampler, linear);\n" 185 "}\n\n"; 186const char* gFS_Fast_SingleModulateGradient = 187 "\nvoid main(void) {\n" 188 " gl_FragColor = color.a * texture2D(gradientSampler, linear);\n" 189 "}\n\n"; 190 191// General case 192const char* gFS_Main_FetchColor = 193 " fragColor = color;\n"; 194const char* gFS_Main_ModulateColor = 195 " fragColor *= color.a;\n"; 196const char* gFS_Main_AccountForAA = 197 " if (widthProportion < boundaryWidth) {\n" 198 " fragColor *= (widthProportion * inverseBoundaryWidth);\n" 199 " } else if (widthProportion > (1.0 - boundaryWidth)) {\n" 200 " fragColor *= ((1.0 - widthProportion) * inverseBoundaryWidth);\n" 201 " }\n" 202 " if (lengthProportion < boundaryLength) {\n" 203 " fragColor *= (lengthProportion * inverseBoundaryLength);\n" 204 " } else if (lengthProportion > (1.0 - boundaryLength)) {\n" 205 " fragColor *= ((1.0 - lengthProportion) * inverseBoundaryLength);\n" 206 " }\n"; 207const char* gFS_Main_FetchTexture[2] = { 208 // Don't modulate 209 " fragColor = texture2D(sampler, outTexCoords);\n", 210 // Modulate 211 " fragColor = color * texture2D(sampler, outTexCoords);\n" 212}; 213const char* gFS_Main_FetchA8Texture[2] = { 214 // Don't modulate 215 " fragColor = texture2D(sampler, outTexCoords);\n", 216 // Modulate 217 " fragColor = color * texture2D(sampler, outTexCoords).a;\n" 218}; 219const char* gFS_Main_FetchGradient[3] = { 220 // Linear 221 " vec4 gradientColor = texture2D(gradientSampler, linear);\n", 222 // Circular 223 " float index = length(circular);\n" 224 " vec4 gradientColor = texture2D(gradientSampler, vec2(index, 0.5));\n", 225 // Sweep 226 " float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n" 227 " vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n" 228}; 229const char* gFS_Main_FetchBitmap = 230 " vec4 bitmapColor = texture2D(bitmapSampler, outBitmapTexCoords);\n"; 231const char* gFS_Main_FetchBitmapNpot = 232 " vec4 bitmapColor = texture2D(bitmapSampler, wrap(outBitmapTexCoords));\n"; 233const char* gFS_Main_BlendShadersBG = 234 " fragColor = blendShaders(gradientColor, bitmapColor)"; 235const char* gFS_Main_BlendShadersGB = 236 " fragColor = blendShaders(bitmapColor, gradientColor)"; 237const char* gFS_Main_BlendShaders_Modulate[3] = { 238 // Don't modulate 239 ";\n", 240 // Modulate 241 " * fragColor.a;\n", 242 // Modulate with alpha 8 texture 243 " * texture2D(sampler, outTexCoords).a;\n" 244}; 245const char* gFS_Main_GradientShader_Modulate[3] = { 246 // Don't modulate 247 " fragColor = gradientColor;\n", 248 // Modulate 249 " fragColor = gradientColor * fragColor.a;\n", 250 // Modulate with alpha 8 texture 251 " fragColor = gradientColor * texture2D(sampler, outTexCoords).a;\n" 252 }; 253const char* gFS_Main_BitmapShader_Modulate[3] = { 254 // Don't modulate 255 " fragColor = bitmapColor;\n", 256 // Modulate 257 " fragColor = bitmapColor * fragColor.a;\n", 258 // Modulate with alpha 8 texture 259 " fragColor = bitmapColor * texture2D(sampler, outTexCoords).a;\n" 260 }; 261const char* gFS_Main_FragColor = 262 " gl_FragColor = fragColor;\n"; 263const char* gFS_Main_FragColor_Blend = 264 " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n"; 265const char* gFS_Main_FragColor_Blend_Swap = 266 " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n"; 267const char* gFS_Main_ApplyColorOp[4] = { 268 // None 269 "", 270 // Matrix 271 // TODO: Fix premultiplied alpha computations for color matrix 272 " fragColor *= colorMatrix;\n" 273 " fragColor += colorMatrixVector;\n" 274 " fragColor.rgb *= fragColor.a;\n", 275 // Lighting 276 " float lightingAlpha = fragColor.a;\n" 277 " fragColor = min(fragColor * lightingMul + (lightingAdd * lightingAlpha), lightingAlpha);\n" 278 " fragColor.a = lightingAlpha;\n", 279 // PorterDuff 280 " fragColor = blendColors(colorBlend, fragColor);\n" 281}; 282const char* gFS_Footer = 283 "}\n\n"; 284 285/////////////////////////////////////////////////////////////////////////////// 286// PorterDuff snippets 287/////////////////////////////////////////////////////////////////////////////// 288 289const char* gBlendOps[18] = { 290 // Clear 291 "return vec4(0.0, 0.0, 0.0, 0.0);\n", 292 // Src 293 "return src;\n", 294 // Dst 295 "return dst;\n", 296 // SrcOver 297 "return src + dst * (1.0 - src.a);\n", 298 // DstOver 299 "return dst + src * (1.0 - dst.a);\n", 300 // SrcIn 301 "return src * dst.a;\n", 302 // DstIn 303 "return dst * src.a;\n", 304 // SrcOut 305 "return src * (1.0 - dst.a);\n", 306 // DstOut 307 "return dst * (1.0 - src.a);\n", 308 // SrcAtop 309 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n", 310 // DstAtop 311 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n", 312 // Xor 313 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, " 314 "src.a + dst.a - 2.0 * src.a * dst.a);\n", 315 // Add 316 "return min(src + dst, 1.0);\n", 317 // Multiply 318 "return src * dst;\n", 319 // Screen 320 "return src + dst - src * dst;\n", 321 // Overlay 322 "return clamp(vec4(mix(" 323 "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), " 324 "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), " 325 "step(dst.a, 2.0 * dst.rgb)), " 326 "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n", 327 // Darken 328 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + " 329 "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n", 330 // Lighten 331 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + " 332 "max(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n", 333}; 334 335/////////////////////////////////////////////////////////////////////////////// 336// Constructors/destructors 337/////////////////////////////////////////////////////////////////////////////// 338 339ProgramCache::ProgramCache() { 340} 341 342ProgramCache::~ProgramCache() { 343 clear(); 344} 345 346/////////////////////////////////////////////////////////////////////////////// 347// Cache management 348/////////////////////////////////////////////////////////////////////////////// 349 350void ProgramCache::clear() { 351 PROGRAM_LOGD("Clearing program cache"); 352 353 size_t count = mCache.size(); 354 for (size_t i = 0; i < count; i++) { 355 delete mCache.valueAt(i); 356 } 357 mCache.clear(); 358} 359 360Program* ProgramCache::get(const ProgramDescription& description) { 361 programid key = description.key(); 362 ssize_t index = mCache.indexOfKey(key); 363 Program* program = NULL; 364 if (index < 0) { 365 description.log("Could not find program"); 366 program = generateProgram(description, key); 367 mCache.add(key, program); 368 } else { 369 program = mCache.valueAt(index); 370 } 371 return program; 372} 373 374/////////////////////////////////////////////////////////////////////////////// 375// Program generation 376/////////////////////////////////////////////////////////////////////////////// 377 378Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) { 379 String8 vertexShader = generateVertexShader(description); 380 String8 fragmentShader = generateFragmentShader(description); 381 382 Program* program = new Program(vertexShader.string(), fragmentShader.string()); 383 return program; 384} 385 386String8 ProgramCache::generateVertexShader(const ProgramDescription& description) { 387 // Add attributes 388 String8 shader(gVS_Header_Attributes); 389 if (description.hasTexture || description.hasExternalTexture) { 390 shader.append(gVS_Header_Attributes_TexCoords); 391 } 392 if (description.isAA) { 393 shader.append(gVS_Header_Attributes_AAParameters); 394 } 395 // Uniforms 396 shader.append(gVS_Header_Uniforms); 397 if (description.hasExternalTexture) { 398 shader.append(gVS_Header_Uniforms_TextureTransform); 399 } 400 if (description.hasGradient) { 401 shader.append(gVS_Header_Uniforms_HasGradient[description.gradientType]); 402 } 403 if (description.hasBitmap) { 404 shader.append(gVS_Header_Uniforms_HasBitmap); 405 } 406 if (description.isPoint) { 407 shader.append(gVS_Header_Uniforms_IsPoint); 408 } 409 // Varyings 410 if (description.hasTexture || description.hasExternalTexture) { 411 shader.append(gVS_Header_Varyings_HasTexture); 412 } 413 if (description.isAA) { 414 shader.append(gVS_Header_Varyings_IsAA); 415 } 416 if (description.hasGradient) { 417 shader.append(gVS_Header_Varyings_HasGradient[description.gradientType]); 418 } 419 if (description.hasBitmap) { 420 shader.append(description.isPoint ? 421 gVS_Header_Varyings_PointHasBitmap : 422 gVS_Header_Varyings_HasBitmap); 423 } 424 425 // Begin the shader 426 shader.append(gVS_Main); { 427 if (description.hasTexture) { 428 shader.append(gVS_Main_OutTexCoords); 429 } 430 if (description.hasExternalTexture) { 431 shader.append(gVS_Main_OutTransformedTexCoords); 432 } 433 if (description.isAA) { 434 shader.append(gVS_Main_AA); 435 } 436 if (description.hasGradient) { 437 shader.append(gVS_Main_OutGradient[description.gradientType]); 438 } 439 if (description.hasBitmap) { 440 shader.append(description.isPoint ? 441 gVS_Main_OutPointBitmapTexCoords : 442 gVS_Main_OutBitmapTexCoords); 443 } 444 if (description.isPoint) { 445 shader.append(gVS_Main_PointSize); 446 } 447 // Output transformed position 448 shader.append(gVS_Main_Position); 449 } 450 // End the shader 451 shader.append(gVS_Footer); 452 453 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string()); 454 455 return shader; 456} 457 458String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) { 459 // Set the default precision 460 String8 shader; 461 462 const bool blendFramebuffer = description.framebufferMode >= SkXfermode::kPlus_Mode; 463 if (blendFramebuffer) { 464 shader.append(gFS_Header_Extension_FramebufferFetch); 465 } 466 if (description.hasExternalTexture) { 467 shader.append(gFS_Header_Extension_ExternalTexture); 468 } 469 470 shader.append(gFS_Header); 471 472 // Varyings 473 if (description.hasTexture || description.hasExternalTexture) { 474 shader.append(gVS_Header_Varyings_HasTexture); 475 } 476 if (description.isAA) { 477 shader.append(gVS_Header_Varyings_IsAA); 478 } 479 if (description.hasGradient) { 480 shader.append(gVS_Header_Varyings_HasGradient[description.gradientType]); 481 } 482 if (description.hasBitmap) { 483 shader.append(description.isPoint ? 484 gVS_Header_Varyings_PointHasBitmap : 485 gVS_Header_Varyings_HasBitmap); 486 } 487 488 // Uniforms 489 int modulateOp = MODULATE_OP_NO_MODULATE; 490 const bool singleColor = !description.hasTexture && !description.hasExternalTexture && 491 !description.hasGradient && !description.hasBitmap; 492 493 if (description.modulate || singleColor) { 494 shader.append(gFS_Uniforms_Color); 495 if (!singleColor) modulateOp = MODULATE_OP_MODULATE; 496 } 497 if (description.hasTexture) { 498 shader.append(gFS_Uniforms_TextureSampler); 499 } 500 if (description.hasExternalTexture) { 501 shader.append(gFS_Uniforms_ExternalTextureSampler); 502 } 503 if (description.isAA) { 504 shader.append(gFS_Uniforms_AA); 505 } 506 if (description.hasGradient) { 507 shader.append(gFS_Uniforms_GradientSampler[description.gradientType]); 508 } 509 if (description.hasBitmap && description.isPoint) { 510 shader.append(gFS_Header_Uniforms_PointHasBitmap); 511 } 512 513 // Optimization for common cases 514 if (!description.isAA && !blendFramebuffer && 515 description.colorOp == ProgramDescription::kColorNone && !description.isPoint) { 516 bool fast = false; 517 518 const bool noShader = !description.hasGradient && !description.hasBitmap; 519 const bool singleTexture = (description.hasTexture || description.hasExternalTexture) && 520 !description.hasAlpha8Texture && noShader; 521 const bool singleA8Texture = description.hasTexture && 522 description.hasAlpha8Texture && noShader; 523 const bool singleGradient = !description.hasTexture && !description.hasExternalTexture && 524 description.hasGradient && !description.hasBitmap && 525 description.gradientType == ProgramDescription::kGradientLinear; 526 527 if (singleColor) { 528 shader.append(gFS_Fast_SingleColor); 529 fast = true; 530 } else if (singleTexture) { 531 if (!description.modulate) { 532 shader.append(gFS_Fast_SingleTexture); 533 } else { 534 shader.append(gFS_Fast_SingleModulateTexture); 535 } 536 fast = true; 537 } else if (singleA8Texture) { 538 if (!description.modulate) { 539 shader.append(gFS_Fast_SingleA8Texture); 540 } else { 541 shader.append(gFS_Fast_SingleModulateA8Texture); 542 } 543 fast = true; 544 } else if (singleGradient) { 545 if (!description.modulate) { 546 shader.append(gFS_Fast_SingleGradient); 547 } else { 548 shader.append(gFS_Fast_SingleModulateGradient); 549 } 550 fast = true; 551 } 552 553 if (fast) { 554#if DEBUG_PROGRAMS 555 PROGRAM_LOGD("*** Fast case:\n"); 556 PROGRAM_LOGD("*** Generated fragment shader:\n\n"); 557 printLongString(shader); 558#endif 559 560 return shader; 561 } 562 } 563 564 if (description.hasBitmap) { 565 shader.append(gFS_Uniforms_BitmapSampler); 566 } 567 shader.append(gFS_Uniforms_ColorOp[description.colorOp]); 568 569 // Generate required functions 570 if (description.hasGradient && description.hasBitmap) { 571 generateBlend(shader, "blendShaders", description.shadersMode); 572 } 573 if (description.colorOp == ProgramDescription::kColorBlend) { 574 generateBlend(shader, "blendColors", description.colorMode); 575 } 576 if (blendFramebuffer) { 577 generateBlend(shader, "blendFramebuffer", description.framebufferMode); 578 } 579 if (description.isBitmapNpot) { 580 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT); 581 } 582 583 // Begin the shader 584 shader.append(gFS_Main); { 585 // Stores the result in fragColor directly 586 if (description.hasTexture || description.hasExternalTexture) { 587 if (description.hasAlpha8Texture) { 588 if (!description.hasGradient && !description.hasBitmap) { 589 shader.append(gFS_Main_FetchA8Texture[modulateOp]); 590 } 591 } else { 592 shader.append(gFS_Main_FetchTexture[modulateOp]); 593 } 594 } else { 595 if ((!description.hasGradient && !description.hasBitmap) || description.modulate) { 596 shader.append(gFS_Main_FetchColor); 597 } 598 } 599 if (description.isAA) { 600 shader.append(gFS_Main_AccountForAA); 601 } 602 if (description.hasGradient) { 603 shader.append(gFS_Main_FetchGradient[description.gradientType]); 604 } 605 if (description.hasBitmap) { 606 if (description.isPoint) { 607 shader.append(gFS_Main_PointBitmapTexCoords); 608 } 609 if (!description.isBitmapNpot) { 610 shader.append(gFS_Main_FetchBitmap); 611 } else { 612 shader.append(gFS_Main_FetchBitmapNpot); 613 } 614 } 615 bool applyModulate = false; 616 // Case when we have two shaders set 617 if (description.hasGradient && description.hasBitmap) { 618 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp; 619 if (description.isBitmapFirst) { 620 shader.append(gFS_Main_BlendShadersBG); 621 } else { 622 shader.append(gFS_Main_BlendShadersGB); 623 } 624 shader.append(gFS_Main_BlendShaders_Modulate[op]); 625 applyModulate = true; 626 } else { 627 if (description.hasGradient) { 628 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp; 629 shader.append(gFS_Main_GradientShader_Modulate[op]); 630 applyModulate = true; 631 } else if (description.hasBitmap) { 632 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp; 633 shader.append(gFS_Main_BitmapShader_Modulate[op]); 634 applyModulate = true; 635 } 636 } 637 if (description.modulate && applyModulate) { 638 shader.append(gFS_Main_ModulateColor); 639 } 640 // Apply the color op if needed 641 shader.append(gFS_Main_ApplyColorOp[description.colorOp]); 642 // Output the fragment 643 if (!blendFramebuffer) { 644 shader.append(gFS_Main_FragColor); 645 } else { 646 shader.append(!description.swapSrcDst ? 647 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap); 648 } 649 } 650 // End the shader 651 shader.append(gFS_Footer); 652 653#if DEBUG_PROGRAMS 654 PROGRAM_LOGD("*** Generated fragment shader:\n\n"); 655 printLongString(shader); 656#endif 657 658 return shader; 659} 660 661void ProgramCache::generateBlend(String8& shader, const char* name, SkXfermode::Mode mode) { 662 shader.append("\nvec4 "); 663 shader.append(name); 664 shader.append("(vec4 src, vec4 dst) {\n"); 665 shader.append(" "); 666 shader.append(gBlendOps[mode]); 667 shader.append("}\n"); 668} 669 670void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) { 671 shader.append("\nvec2 wrap(vec2 texCoords) {\n"); 672 if (wrapS == GL_MIRRORED_REPEAT) { 673 shader.append(" float xMod2 = mod(texCoords.x, 2.0);\n"); 674 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n"); 675 } 676 if (wrapT == GL_MIRRORED_REPEAT) { 677 shader.append(" float yMod2 = mod(texCoords.y, 2.0);\n"); 678 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n"); 679 } 680 shader.append(" return vec2("); 681 switch (wrapS) { 682 case GL_CLAMP_TO_EDGE: 683 shader.append("texCoords.x"); 684 break; 685 case GL_REPEAT: 686 shader.append("mod(texCoords.x, 1.0)"); 687 break; 688 case GL_MIRRORED_REPEAT: 689 shader.append("xMod2"); 690 break; 691 } 692 shader.append(", "); 693 switch (wrapT) { 694 case GL_CLAMP_TO_EDGE: 695 shader.append("texCoords.y"); 696 break; 697 case GL_REPEAT: 698 shader.append("mod(texCoords.y, 1.0)"); 699 break; 700 case GL_MIRRORED_REPEAT: 701 shader.append("yMod2"); 702 break; 703 } 704 shader.append(");\n"); 705 shader.append("}\n"); 706} 707 708void ProgramCache::printLongString(const String8& shader) const { 709 ssize_t index = 0; 710 ssize_t lastIndex = 0; 711 const char* str = shader.string(); 712 while ((index = shader.find("\n", index)) > -1) { 713 String8 line(str, index - lastIndex); 714 if (line.length() == 0) line.append("\n"); 715 PROGRAM_LOGD("%s", line.string()); 716 index++; 717 str += (index - lastIndex); 718 lastIndex = index; 719 } 720} 721 722}; // namespace uirenderer 723}; // namespace android 724