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