blob: 8a9a2ac00c78875bd3ebcea393597b938f064f8e [file] [log] [blame]
Romain Guyac670c02010-07-27 17:39:27 -07001/*
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
Romain Guya60c3882011-08-01 15:28:16 -070021#include "Caches.h"
Romain Guyac670c02010-07-27 17:39:27 -070022#include "ProgramCache.h"
23
24namespace android {
25namespace uirenderer {
26
27///////////////////////////////////////////////////////////////////////////////
Romain Guy707b2f72010-10-11 16:34:59 -070028// Defines
29///////////////////////////////////////////////////////////////////////////////
30
31#define MODULATE_OP_NO_MODULATE 0
32#define MODULATE_OP_MODULATE 1
33#define MODULATE_OP_MODULATE_A8 2
34
35///////////////////////////////////////////////////////////////////////////////
Romain Guyac670c02010-07-27 17:39:27 -070036// Vertex shaders snippets
37///////////////////////////////////////////////////////////////////////////////
38
Romain Guyac670c02010-07-27 17:39:27 -070039const char* gVS_Header_Attributes =
40 "attribute vec4 position;\n";
41const char* gVS_Header_Attributes_TexCoords =
42 "attribute vec2 texCoords;\n";
Chet Haase99585ad2011-05-02 15:00:16 -070043const char* gVS_Header_Attributes_AAParameters =
44 "attribute float vtxWidth;\n"
45 "attribute float vtxLength;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -070046const char* gVS_Header_Uniforms_TextureTransform =
47 "uniform mat4 mainTextureTransform;\n";
Romain Guyac670c02010-07-27 17:39:27 -070048const char* gVS_Header_Uniforms =
49 "uniform mat4 transform;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -070050const char* gVS_Header_Uniforms_IsPoint =
Romain Guy80bbfb12011-03-23 16:56:28 -070051 "uniform mediump float pointSize;\n";
Romain Guyee916f12010-09-20 17:53:08 -070052const char* gVS_Header_Uniforms_HasGradient[3] = {
53 // Linear
Romain Guyee916f12010-09-20 17:53:08 -070054 "uniform mat4 screenSpace;\n",
55 // Circular
Romain Guyddb80be2010-09-20 19:04:33 -070056 "uniform mat4 screenSpace;\n",
Romain Guyee916f12010-09-20 17:53:08 -070057 // Sweep
Romain Guyee916f12010-09-20 17:53:08 -070058 "uniform mat4 screenSpace;\n"
59};
Romain Guy889f8d12010-07-29 14:37:42 -070060const char* gVS_Header_Uniforms_HasBitmap =
61 "uniform mat4 textureTransform;\n"
Romain Guy80bbfb12011-03-23 16:56:28 -070062 "uniform mediump vec2 textureDimension;\n";
Romain Guyac670c02010-07-27 17:39:27 -070063const char* gVS_Header_Varyings_HasTexture =
64 "varying vec2 outTexCoords;\n";
Chet Haase99585ad2011-05-02 15:00:16 -070065const char* gVS_Header_Varyings_IsAA =
66 "varying float widthProportion;\n"
67 "varying float lengthProportion;\n";
Romain Guy63553472012-07-18 20:04:14 -070068const char* gVS_Header_Varyings_HasBitmap =
69 "varying highp vec2 outBitmapTexCoords;\n";
70const char* gVS_Header_Varyings_PointHasBitmap =
71 "varying highp vec2 outPointBitmapTexCoords;\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -070072const char* gVS_Header_Varyings_HasGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -070073 // Linear
Romain Guy63553472012-07-18 20:04:14 -070074 "varying highp vec2 linear;\n",
Romain Guy320d46b2012-08-08 16:05:42 -070075 "varying float linear;\n",
Romain Guy211efea2012-07-31 21:16:07 -070076
Romain Guyee916f12010-09-20 17:53:08 -070077 // Circular
Romain Guy63553472012-07-18 20:04:14 -070078 "varying highp vec2 circular;\n",
Romain Guy42e1e0d2012-07-30 14:47:51 -070079 "varying highp vec2 circular;\n",
Romain Guy211efea2012-07-31 21:16:07 -070080
Romain Guyee916f12010-09-20 17:53:08 -070081 // Sweep
Romain Guy42e1e0d2012-07-30 14:47:51 -070082 "varying highp vec2 sweep;\n",
83 "varying highp vec2 sweep;\n",
Romain Guyee916f12010-09-20 17:53:08 -070084};
Romain Guyac670c02010-07-27 17:39:27 -070085const char* gVS_Main =
86 "\nvoid main(void) {\n";
87const char* gVS_Main_OutTexCoords =
88 " outTexCoords = texCoords;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -070089const char* gVS_Main_OutTransformedTexCoords =
90 " outTexCoords = (mainTextureTransform * vec4(texCoords, 0.0, 1.0)).xy;\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -070091const char* gVS_Main_OutGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -070092 // Linear
Romain Guy7537f852010-10-11 14:38:28 -070093 " linear = vec2((screenSpace * position).x, 0.5);\n",
Romain Guy42e1e0d2012-07-30 14:47:51 -070094 " linear = (screenSpace * position).x;\n",
Romain Guy211efea2012-07-31 21:16:07 -070095
Romain Guyee916f12010-09-20 17:53:08 -070096 // Circular
Romain Guy14830942010-10-07 15:07:45 -070097 " circular = (screenSpace * position).xy;\n",
Romain Guy42e1e0d2012-07-30 14:47:51 -070098 " circular = (screenSpace * position).xy;\n",
Romain Guy211efea2012-07-31 21:16:07 -070099
Romain Guyee916f12010-09-20 17:53:08 -0700100 // Sweep
Romain Guy42e1e0d2012-07-30 14:47:51 -0700101 " sweep = (screenSpace * position).xy;\n",
102 " sweep = (screenSpace * position).xy;\n",
Romain Guyee916f12010-09-20 17:53:08 -0700103};
Romain Guy889f8d12010-07-29 14:37:42 -0700104const char* gVS_Main_OutBitmapTexCoords =
Romain Guy707b2f72010-10-11 16:34:59 -0700105 " outBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -0700106const char* gVS_Main_OutPointBitmapTexCoords =
107 " outPointBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700108const char* gVS_Main_Position =
109 " gl_Position = transform * position;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -0700110const char* gVS_Main_PointSize =
111 " gl_PointSize = pointSize;\n";
Chet Haase99585ad2011-05-02 15:00:16 -0700112const char* gVS_Main_AA =
113 " widthProportion = vtxWidth;\n"
114 " lengthProportion = vtxLength;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700115const char* gVS_Footer =
116 "}\n\n";
117
118///////////////////////////////////////////////////////////////////////////////
119// Fragment shaders snippets
120///////////////////////////////////////////////////////////////////////////////
121
Romain Guya5aed0d2010-09-09 14:42:43 -0700122const char* gFS_Header_Extension_FramebufferFetch =
123 "#extension GL_NV_shader_framebuffer_fetch : enable\n\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700124const char* gFS_Header_Extension_ExternalTexture =
125 "#extension GL_OES_EGL_image_external : require\n\n";
Romain Guyac670c02010-07-27 17:39:27 -0700126const char* gFS_Header =
127 "precision mediump float;\n\n";
128const char* gFS_Uniforms_Color =
129 "uniform vec4 color;\n";
Chet Haase99585ad2011-05-02 15:00:16 -0700130const char* gFS_Uniforms_AA =
Chet Haase5b0200b2011-04-13 17:58:08 -0700131 "uniform float boundaryWidth;\n"
Chris Craika798b952012-08-27 17:03:13 -0700132 "uniform float boundaryLength;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -0700133const char* gFS_Header_Uniforms_PointHasBitmap =
134 "uniform vec2 textureDimension;\n"
135 "uniform float pointSize;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700136const char* gFS_Uniforms_TextureSampler =
137 "uniform sampler2D sampler;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700138const char* gFS_Uniforms_ExternalTextureSampler =
139 "uniform samplerExternalOES sampler;\n";
Romain Guy211efea2012-07-31 21:16:07 -0700140#define FS_UNIFORMS_DITHER \
141 "uniform float ditherSize;\n" \
142 "uniform sampler2D ditherSampler;\n"
143#define FS_UNIFORMS_GRADIENT \
144 "uniform vec4 startColor;\n" \
145 "uniform vec4 endColor;\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700146const char* gFS_Uniforms_GradientSampler[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700147 // Linear
Romain Guy211efea2012-07-31 21:16:07 -0700148 FS_UNIFORMS_DITHER "uniform sampler2D gradientSampler;\n",
149 FS_UNIFORMS_DITHER FS_UNIFORMS_GRADIENT,
150
Romain Guyee916f12010-09-20 17:53:08 -0700151 // Circular
Romain Guy211efea2012-07-31 21:16:07 -0700152 FS_UNIFORMS_DITHER "uniform sampler2D gradientSampler;\n",
153 FS_UNIFORMS_DITHER FS_UNIFORMS_GRADIENT,
154
Romain Guyee916f12010-09-20 17:53:08 -0700155 // Sweep
Romain Guy211efea2012-07-31 21:16:07 -0700156 FS_UNIFORMS_DITHER "uniform sampler2D gradientSampler;\n",
157 FS_UNIFORMS_DITHER FS_UNIFORMS_GRADIENT
Romain Guyee916f12010-09-20 17:53:08 -0700158};
Romain Guyac670c02010-07-27 17:39:27 -0700159const char* gFS_Uniforms_BitmapSampler =
160 "uniform sampler2D bitmapSampler;\n";
161const char* gFS_Uniforms_ColorOp[4] = {
162 // None
163 "",
164 // Matrix
165 "uniform mat4 colorMatrix;\n"
166 "uniform vec4 colorMatrixVector;\n",
167 // Lighting
Romain Guydb1938e2010-08-02 18:50:22 -0700168 "uniform vec4 lightingMul;\n"
169 "uniform vec4 lightingAdd;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700170 // PorterDuff
Romain Guydb1938e2010-08-02 18:50:22 -0700171 "uniform vec4 colorBlend;\n"
Romain Guyac670c02010-07-27 17:39:27 -0700172};
Romain Guy41210632012-07-16 17:04:24 -0700173const char* gFS_Uniforms_Gamma =
174 "uniform float gamma;\n";
175
Romain Guyac670c02010-07-27 17:39:27 -0700176const char* gFS_Main =
177 "\nvoid main(void) {\n"
Romain Guy7fbcc042010-08-04 15:40:07 -0700178 " lowp vec4 fragColor;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700179
Romain Guyed6fcb02011-03-21 13:11:28 -0700180const char* gFS_Main_PointBitmapTexCoords =
Romain Guy63553472012-07-18 20:04:14 -0700181 " highp vec2 outBitmapTexCoords = outPointBitmapTexCoords + "
Romain Guyed6fcb02011-03-21 13:11:28 -0700182 "((gl_PointCoord - vec2(0.5, 0.5)) * textureDimension * vec2(pointSize, pointSize));\n";
183
Romain Guy211efea2012-07-31 21:16:07 -0700184#define FS_MAIN_DITHER \
185 "texture2D(ditherSampler, gl_FragCoord.xy * ditherSize).a * ditherSize * ditherSize"
186const char* gFS_Main_AddDitherToGradient =
187 " gradientColor += " FS_MAIN_DITHER ";\n";
188
Romain Guy707b2f72010-10-11 16:34:59 -0700189// Fast cases
190const char* gFS_Fast_SingleColor =
191 "\nvoid main(void) {\n"
192 " gl_FragColor = color;\n"
193 "}\n\n";
194const char* gFS_Fast_SingleTexture =
195 "\nvoid main(void) {\n"
196 " gl_FragColor = texture2D(sampler, outTexCoords);\n"
197 "}\n\n";
198const char* gFS_Fast_SingleModulateTexture =
199 "\nvoid main(void) {\n"
200 " gl_FragColor = color.a * texture2D(sampler, outTexCoords);\n"
201 "}\n\n";
202const char* gFS_Fast_SingleA8Texture =
203 "\nvoid main(void) {\n"
Romain Guy9db91242010-10-12 13:13:09 -0700204 " gl_FragColor = texture2D(sampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700205 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700206const char* gFS_Fast_SingleA8Texture_ApplyGamma =
207 "\nvoid main(void) {\n"
208 " gl_FragColor = vec4(0.0, 0.0, 0.0, pow(texture2D(sampler, outTexCoords).a, gamma));\n"
209 "}\n\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700210const char* gFS_Fast_SingleModulateA8Texture =
211 "\nvoid main(void) {\n"
212 " gl_FragColor = color * texture2D(sampler, outTexCoords).a;\n"
213 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700214const char* gFS_Fast_SingleModulateA8Texture_ApplyGamma =
215 "\nvoid main(void) {\n"
216 " gl_FragColor = color * pow(texture2D(sampler, outTexCoords).a, gamma);\n"
217 "}\n\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -0700218const char* gFS_Fast_SingleGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700219 "\nvoid main(void) {\n"
Romain Guy211efea2012-07-31 21:16:07 -0700220 " gl_FragColor = " FS_MAIN_DITHER " + texture2D(gradientSampler, linear);\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700221 "}\n\n",
222 "\nvoid main(void) {\n"
Romain Guy211efea2012-07-31 21:16:07 -0700223 " gl_FragColor = " FS_MAIN_DITHER " + mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700224 "}\n\n"
225};
226const char* gFS_Fast_SingleModulateGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700227 "\nvoid main(void) {\n"
Romain Guy211efea2012-07-31 21:16:07 -0700228 " gl_FragColor " FS_MAIN_DITHER " + color.a * texture2D(gradientSampler, linear);\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700229 "}\n\n",
230 "\nvoid main(void) {\n"
Romain Guy211efea2012-07-31 21:16:07 -0700231 " gl_FragColor " FS_MAIN_DITHER " + color.a * mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700232 "}\n\n"
233};
Romain Guy707b2f72010-10-11 16:34:59 -0700234
235// General case
Romain Guyac670c02010-07-27 17:39:27 -0700236const char* gFS_Main_FetchColor =
237 " fragColor = color;\n";
Romain Guy740bf2b2011-04-26 15:33:10 -0700238const char* gFS_Main_ModulateColor =
239 " fragColor *= color.a;\n";
Romain Guy41210632012-07-16 17:04:24 -0700240const char* gFS_Main_ModulateColor_ApplyGamma =
241 " fragColor *= pow(color.a, gamma);\n";
Chet Haase99585ad2011-05-02 15:00:16 -0700242const char* gFS_Main_AccountForAA =
Chris Craika798b952012-08-27 17:03:13 -0700243 " fragColor *= (1.0 - smoothstep(boundaryWidth, 0.5, abs(0.5 - widthProportion)))\n"
244 " * (1.0 - smoothstep(boundaryLength, 0.5, abs(0.5 - lengthProportion)));\n";
245
Romain Guy707b2f72010-10-11 16:34:59 -0700246const char* gFS_Main_FetchTexture[2] = {
247 // Don't modulate
248 " fragColor = texture2D(sampler, outTexCoords);\n",
249 // Modulate
250 " fragColor = color * texture2D(sampler, outTexCoords);\n"
251};
252const char* gFS_Main_FetchA8Texture[2] = {
253 // Don't modulate
Romain Guy9db91242010-10-12 13:13:09 -0700254 " fragColor = texture2D(sampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700255 // Modulate
256 " fragColor = color * texture2D(sampler, outTexCoords).a;\n"
257};
Romain Guy42e1e0d2012-07-30 14:47:51 -0700258const char* gFS_Main_FetchGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700259 // Linear
Romain Guy320d46b2012-08-08 16:05:42 -0700260 " vec4 gradientColor = texture2D(gradientSampler, linear);\n",
Romain Guy211efea2012-07-31 21:16:07 -0700261
Romain Guy320d46b2012-08-08 16:05:42 -0700262 " vec4 gradientColor = mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700263
Romain Guyee916f12010-09-20 17:53:08 -0700264 // Circular
Romain Guy320d46b2012-08-08 16:05:42 -0700265 " vec4 gradientColor = texture2D(gradientSampler, vec2(length(circular), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700266
Romain Guy320d46b2012-08-08 16:05:42 -0700267 " vec4 gradientColor = mix(startColor, endColor, clamp(length(circular), 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700268
Romain Guyee916f12010-09-20 17:53:08 -0700269 // Sweep
Romain Guy63553472012-07-18 20:04:14 -0700270 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700271 " vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700272
Romain Guy42e1e0d2012-07-30 14:47:51 -0700273 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700274 " vec4 gradientColor = mix(startColor, endColor, clamp(index - floor(index), 0.0, 1.0));\n"
Romain Guyee916f12010-09-20 17:53:08 -0700275};
Romain Guyac670c02010-07-27 17:39:27 -0700276const char* gFS_Main_FetchBitmap =
277 " vec4 bitmapColor = texture2D(bitmapSampler, outBitmapTexCoords);\n";
Romain Guy889f8d12010-07-29 14:37:42 -0700278const char* gFS_Main_FetchBitmapNpot =
279 " vec4 bitmapColor = texture2D(bitmapSampler, wrap(outBitmapTexCoords));\n";
Romain Guyac670c02010-07-27 17:39:27 -0700280const char* gFS_Main_BlendShadersBG =
Romain Guyac670c02010-07-27 17:39:27 -0700281 " fragColor = blendShaders(gradientColor, bitmapColor)";
Romain Guy06f96e22010-07-30 19:18:16 -0700282const char* gFS_Main_BlendShadersGB =
283 " fragColor = blendShaders(bitmapColor, gradientColor)";
Romain Guy707b2f72010-10-11 16:34:59 -0700284const char* gFS_Main_BlendShaders_Modulate[3] = {
285 // Don't modulate
286 ";\n",
287 // Modulate
288 " * fragColor.a;\n",
289 // Modulate with alpha 8 texture
290 " * texture2D(sampler, outTexCoords).a;\n"
291};
292const char* gFS_Main_GradientShader_Modulate[3] = {
293 // Don't modulate
294 " fragColor = gradientColor;\n",
295 // Modulate
296 " fragColor = gradientColor * fragColor.a;\n",
297 // Modulate with alpha 8 texture
298 " fragColor = gradientColor * texture2D(sampler, outTexCoords).a;\n"
299 };
300const char* gFS_Main_BitmapShader_Modulate[3] = {
301 // Don't modulate
302 " fragColor = bitmapColor;\n",
303 // Modulate
304 " fragColor = bitmapColor * fragColor.a;\n",
305 // Modulate with alpha 8 texture
306 " fragColor = bitmapColor * texture2D(sampler, outTexCoords).a;\n"
307 };
Romain Guyac670c02010-07-27 17:39:27 -0700308const char* gFS_Main_FragColor =
309 " gl_FragColor = fragColor;\n";
Romain Guya5aed0d2010-09-09 14:42:43 -0700310const char* gFS_Main_FragColor_Blend =
311 " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n";
Romain Guyf607bdc2010-09-10 19:20:06 -0700312const char* gFS_Main_FragColor_Blend_Swap =
313 " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n";
Romain Guyac670c02010-07-27 17:39:27 -0700314const char* gFS_Main_ApplyColorOp[4] = {
315 // None
316 "",
317 // Matrix
Romain Guydb1938e2010-08-02 18:50:22 -0700318 // TODO: Fix premultiplied alpha computations for color matrix
Romain Guyac670c02010-07-27 17:39:27 -0700319 " fragColor *= colorMatrix;\n"
Romain Guydb1938e2010-08-02 18:50:22 -0700320 " fragColor += colorMatrixVector;\n"
321 " fragColor.rgb *= fragColor.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700322 // Lighting
Romain Guydb1938e2010-08-02 18:50:22 -0700323 " float lightingAlpha = fragColor.a;\n"
324 " fragColor = min(fragColor * lightingMul + (lightingAdd * lightingAlpha), lightingAlpha);\n"
325 " fragColor.a = lightingAlpha;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700326 // PorterDuff
327 " fragColor = blendColors(colorBlend, fragColor);\n"
328};
329const char* gFS_Footer =
330 "}\n\n";
331
332///////////////////////////////////////////////////////////////////////////////
333// PorterDuff snippets
334///////////////////////////////////////////////////////////////////////////////
335
Romain Guy48daa542010-08-10 19:21:34 -0700336const char* gBlendOps[18] = {
Romain Guyac670c02010-07-27 17:39:27 -0700337 // Clear
338 "return vec4(0.0, 0.0, 0.0, 0.0);\n",
339 // Src
340 "return src;\n",
341 // Dst
342 "return dst;\n",
343 // SrcOver
Romain Guy06f96e22010-07-30 19:18:16 -0700344 "return src + dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700345 // DstOver
Romain Guy06f96e22010-07-30 19:18:16 -0700346 "return dst + src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700347 // SrcIn
Romain Guy06f96e22010-07-30 19:18:16 -0700348 "return src * dst.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700349 // DstIn
Romain Guy06f96e22010-07-30 19:18:16 -0700350 "return dst * src.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700351 // SrcOut
Romain Guy06f96e22010-07-30 19:18:16 -0700352 "return src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700353 // DstOut
Romain Guy06f96e22010-07-30 19:18:16 -0700354 "return dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700355 // SrcAtop
356 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n",
357 // DstAtop
358 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n",
359 // Xor
Romain Guy48daa542010-08-10 19:21:34 -0700360 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, "
Romain Guyac670c02010-07-27 17:39:27 -0700361 "src.a + dst.a - 2.0 * src.a * dst.a);\n",
Romain Guy48daa542010-08-10 19:21:34 -0700362 // Add
363 "return min(src + dst, 1.0);\n",
364 // Multiply
365 "return src * dst;\n",
366 // Screen
367 "return src + dst - src * dst;\n",
368 // Overlay
369 "return clamp(vec4(mix("
370 "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), "
371 "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), "
372 "step(dst.a, 2.0 * dst.rgb)), "
373 "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n",
374 // Darken
375 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
376 "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
377 // Lighten
378 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
379 "max(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700380};
381
382///////////////////////////////////////////////////////////////////////////////
383// Constructors/destructors
384///////////////////////////////////////////////////////////////////////////////
385
386ProgramCache::ProgramCache() {
387}
388
389ProgramCache::~ProgramCache() {
390 clear();
391}
392
393///////////////////////////////////////////////////////////////////////////////
394// Cache management
395///////////////////////////////////////////////////////////////////////////////
396
397void ProgramCache::clear() {
Romain Guy67f27952010-12-07 20:09:23 -0800398 PROGRAM_LOGD("Clearing program cache");
399
Romain Guyac670c02010-07-27 17:39:27 -0700400 size_t count = mCache.size();
401 for (size_t i = 0; i < count; i++) {
402 delete mCache.valueAt(i);
403 }
404 mCache.clear();
405}
406
407Program* ProgramCache::get(const ProgramDescription& description) {
408 programid key = description.key();
409 ssize_t index = mCache.indexOfKey(key);
410 Program* program = NULL;
411 if (index < 0) {
Romain Guyee916f12010-09-20 17:53:08 -0700412 description.log("Could not find program");
Romain Guyac670c02010-07-27 17:39:27 -0700413 program = generateProgram(description, key);
414 mCache.add(key, program);
415 } else {
416 program = mCache.valueAt(index);
417 }
418 return program;
419}
420
421///////////////////////////////////////////////////////////////////////////////
422// Program generation
423///////////////////////////////////////////////////////////////////////////////
424
425Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) {
426 String8 vertexShader = generateVertexShader(description);
427 String8 fragmentShader = generateFragmentShader(description);
428
Romain Guy42e1e0d2012-07-30 14:47:51 -0700429 return new Program(description, vertexShader.string(), fragmentShader.string());
430}
431
432static inline size_t gradientIndex(const ProgramDescription& description) {
433 return description.gradientType * 2 + description.isSimpleGradient;
Romain Guyac670c02010-07-27 17:39:27 -0700434}
435
436String8 ProgramCache::generateVertexShader(const ProgramDescription& description) {
437 // Add attributes
438 String8 shader(gVS_Header_Attributes);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700439 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700440 shader.append(gVS_Header_Attributes_TexCoords);
441 }
Chet Haase99585ad2011-05-02 15:00:16 -0700442 if (description.isAA) {
443 shader.append(gVS_Header_Attributes_AAParameters);
Chet Haase5b0200b2011-04-13 17:58:08 -0700444 }
Romain Guyac670c02010-07-27 17:39:27 -0700445 // Uniforms
446 shader.append(gVS_Header_Uniforms);
Romain Guy8f0095c2011-05-02 17:24:22 -0700447 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700448 shader.append(gVS_Header_Uniforms_TextureTransform);
449 }
Romain Guyac670c02010-07-27 17:39:27 -0700450 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700451 shader.append(gVS_Header_Uniforms_HasGradient[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700452 }
Romain Guy889f8d12010-07-29 14:37:42 -0700453 if (description.hasBitmap) {
454 shader.append(gVS_Header_Uniforms_HasBitmap);
455 }
Romain Guyed6fcb02011-03-21 13:11:28 -0700456 if (description.isPoint) {
457 shader.append(gVS_Header_Uniforms_IsPoint);
458 }
Romain Guyac670c02010-07-27 17:39:27 -0700459 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700460 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700461 shader.append(gVS_Header_Varyings_HasTexture);
462 }
Chet Haase99585ad2011-05-02 15:00:16 -0700463 if (description.isAA) {
464 shader.append(gVS_Header_Varyings_IsAA);
Chet Haase5b0200b2011-04-13 17:58:08 -0700465 }
Romain Guyac670c02010-07-27 17:39:27 -0700466 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700467 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700468 }
469 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700470 shader.append(description.isPoint ?
Romain Guy63553472012-07-18 20:04:14 -0700471 gVS_Header_Varyings_PointHasBitmap :
472 gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700473 }
474
475 // Begin the shader
476 shader.append(gVS_Main); {
Romain Guy8f0095c2011-05-02 17:24:22 -0700477 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700478 shader.append(gVS_Main_OutTransformedTexCoords);
Romain Guy8f0095c2011-05-02 17:24:22 -0700479 } else if (description.hasTexture || description.hasExternalTexture) {
480 shader.append(gVS_Main_OutTexCoords);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700481 }
Chet Haase99585ad2011-05-02 15:00:16 -0700482 if (description.isAA) {
483 shader.append(gVS_Main_AA);
Chet Haase5b0200b2011-04-13 17:58:08 -0700484 }
Romain Guyac670c02010-07-27 17:39:27 -0700485 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700486 shader.append(gVS_Main_OutGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700487 }
Romain Guy889f8d12010-07-29 14:37:42 -0700488 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700489 shader.append(description.isPoint ?
490 gVS_Main_OutPointBitmapTexCoords :
491 gVS_Main_OutBitmapTexCoords);
492 }
493 if (description.isPoint) {
494 shader.append(gVS_Main_PointSize);
Romain Guy889f8d12010-07-29 14:37:42 -0700495 }
Romain Guyac670c02010-07-27 17:39:27 -0700496 // Output transformed position
497 shader.append(gVS_Main_Position);
498 }
499 // End the shader
500 shader.append(gVS_Footer);
501
502 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string());
503
504 return shader;
505}
506
507String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) {
Romain Guya5aed0d2010-09-09 14:42:43 -0700508 String8 shader;
509
Romain Guy707b2f72010-10-11 16:34:59 -0700510 const bool blendFramebuffer = description.framebufferMode >= SkXfermode::kPlus_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700511 if (blendFramebuffer) {
512 shader.append(gFS_Header_Extension_FramebufferFetch);
513 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700514 if (description.hasExternalTexture) {
515 shader.append(gFS_Header_Extension_ExternalTexture);
516 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700517
518 shader.append(gFS_Header);
Romain Guyac670c02010-07-27 17:39:27 -0700519
520 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700521 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700522 shader.append(gVS_Header_Varyings_HasTexture);
523 }
Chet Haase99585ad2011-05-02 15:00:16 -0700524 if (description.isAA) {
525 shader.append(gVS_Header_Varyings_IsAA);
Chet Haase5b0200b2011-04-13 17:58:08 -0700526 }
Romain Guyac670c02010-07-27 17:39:27 -0700527 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700528 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700529 }
530 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700531 shader.append(description.isPoint ?
Romain Guy63553472012-07-18 20:04:14 -0700532 gVS_Header_Varyings_PointHasBitmap :
533 gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700534 }
535
Romain Guyac670c02010-07-27 17:39:27 -0700536 // Uniforms
Romain Guy707b2f72010-10-11 16:34:59 -0700537 int modulateOp = MODULATE_OP_NO_MODULATE;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700538 const bool singleColor = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700539 !description.hasGradient && !description.hasBitmap;
540
541 if (description.modulate || singleColor) {
542 shader.append(gFS_Uniforms_Color);
543 if (!singleColor) modulateOp = MODULATE_OP_MODULATE;
544 }
Romain Guyac670c02010-07-27 17:39:27 -0700545 if (description.hasTexture) {
546 shader.append(gFS_Uniforms_TextureSampler);
Romain Guy8f0095c2011-05-02 17:24:22 -0700547 } else if (description.hasExternalTexture) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700548 shader.append(gFS_Uniforms_ExternalTextureSampler);
549 }
Chet Haase99585ad2011-05-02 15:00:16 -0700550 if (description.isAA) {
551 shader.append(gFS_Uniforms_AA);
Chet Haase5b0200b2011-04-13 17:58:08 -0700552 }
Romain Guyac670c02010-07-27 17:39:27 -0700553 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700554 shader.append(gFS_Uniforms_GradientSampler[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700555 }
Romain Guyed6fcb02011-03-21 13:11:28 -0700556 if (description.hasBitmap && description.isPoint) {
557 shader.append(gFS_Header_Uniforms_PointHasBitmap);
558 }
Romain Guy41210632012-07-16 17:04:24 -0700559 if (description.hasGammaCorrection) {
560 shader.append(gFS_Uniforms_Gamma);
561 }
Romain Guy707b2f72010-10-11 16:34:59 -0700562
563 // Optimization for common cases
Chet Haase99585ad2011-05-02 15:00:16 -0700564 if (!description.isAA && !blendFramebuffer &&
Chet Haase5b0200b2011-04-13 17:58:08 -0700565 description.colorOp == ProgramDescription::kColorNone && !description.isPoint) {
Romain Guy707b2f72010-10-11 16:34:59 -0700566 bool fast = false;
567
568 const bool noShader = !description.hasGradient && !description.hasBitmap;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700569 const bool singleTexture = (description.hasTexture || description.hasExternalTexture) &&
Romain Guy707b2f72010-10-11 16:34:59 -0700570 !description.hasAlpha8Texture && noShader;
571 const bool singleA8Texture = description.hasTexture &&
572 description.hasAlpha8Texture && noShader;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700573 const bool singleGradient = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700574 description.hasGradient && !description.hasBitmap &&
575 description.gradientType == ProgramDescription::kGradientLinear;
576
577 if (singleColor) {
578 shader.append(gFS_Fast_SingleColor);
579 fast = true;
580 } else if (singleTexture) {
581 if (!description.modulate) {
582 shader.append(gFS_Fast_SingleTexture);
583 } else {
584 shader.append(gFS_Fast_SingleModulateTexture);
585 }
586 fast = true;
587 } else if (singleA8Texture) {
588 if (!description.modulate) {
Romain Guy41210632012-07-16 17:04:24 -0700589 if (description.hasGammaCorrection) {
590 shader.append(gFS_Fast_SingleA8Texture_ApplyGamma);
591 } else {
592 shader.append(gFS_Fast_SingleA8Texture);
593 }
Romain Guy707b2f72010-10-11 16:34:59 -0700594 } else {
Romain Guy41210632012-07-16 17:04:24 -0700595 if (description.hasGammaCorrection) {
596 shader.append(gFS_Fast_SingleModulateA8Texture_ApplyGamma);
597 } else {
598 shader.append(gFS_Fast_SingleModulateA8Texture);
599 }
Romain Guy707b2f72010-10-11 16:34:59 -0700600 }
601 fast = true;
602 } else if (singleGradient) {
603 if (!description.modulate) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700604 shader.append(gFS_Fast_SingleGradient[description.isSimpleGradient]);
Romain Guy707b2f72010-10-11 16:34:59 -0700605 } else {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700606 shader.append(gFS_Fast_SingleModulateGradient[description.isSimpleGradient]);
Romain Guy707b2f72010-10-11 16:34:59 -0700607 }
608 fast = true;
609 }
610
611 if (fast) {
Romain Guyc15008e2010-11-10 11:59:15 -0800612#if DEBUG_PROGRAMS
Romain Guy707b2f72010-10-11 16:34:59 -0700613 PROGRAM_LOGD("*** Fast case:\n");
614 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
615 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800616#endif
Romain Guy707b2f72010-10-11 16:34:59 -0700617
618 return shader;
619 }
620 }
621
Romain Guyac670c02010-07-27 17:39:27 -0700622 if (description.hasBitmap) {
623 shader.append(gFS_Uniforms_BitmapSampler);
624 }
625 shader.append(gFS_Uniforms_ColorOp[description.colorOp]);
626
627 // Generate required functions
628 if (description.hasGradient && description.hasBitmap) {
Romain Guy48daa542010-08-10 19:21:34 -0700629 generateBlend(shader, "blendShaders", description.shadersMode);
Romain Guyac670c02010-07-27 17:39:27 -0700630 }
631 if (description.colorOp == ProgramDescription::kColorBlend) {
Romain Guy48daa542010-08-10 19:21:34 -0700632 generateBlend(shader, "blendColors", description.colorMode);
Romain Guyac670c02010-07-27 17:39:27 -0700633 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700634 if (blendFramebuffer) {
635 generateBlend(shader, "blendFramebuffer", description.framebufferMode);
636 }
Romain Guy889f8d12010-07-29 14:37:42 -0700637 if (description.isBitmapNpot) {
638 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
639 }
Romain Guyac670c02010-07-27 17:39:27 -0700640
641 // Begin the shader
642 shader.append(gFS_Main); {
643 // Stores the result in fragColor directly
Romain Guyaa6c24c2011-04-28 18:40:04 -0700644 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700645 if (description.hasAlpha8Texture) {
Romain Guy707b2f72010-10-11 16:34:59 -0700646 if (!description.hasGradient && !description.hasBitmap) {
647 shader.append(gFS_Main_FetchA8Texture[modulateOp]);
648 }
Romain Guyac670c02010-07-27 17:39:27 -0700649 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700650 shader.append(gFS_Main_FetchTexture[modulateOp]);
Romain Guyac670c02010-07-27 17:39:27 -0700651 }
652 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700653 if ((!description.hasGradient && !description.hasBitmap) || description.modulate) {
654 shader.append(gFS_Main_FetchColor);
655 }
Romain Guyac670c02010-07-27 17:39:27 -0700656 }
Chet Haase99585ad2011-05-02 15:00:16 -0700657 if (description.isAA) {
658 shader.append(gFS_Main_AccountForAA);
Chet Haase5b0200b2011-04-13 17:58:08 -0700659 }
Romain Guyac670c02010-07-27 17:39:27 -0700660 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700661 shader.append(gFS_Main_FetchGradient[gradientIndex(description)]);
Romain Guy211efea2012-07-31 21:16:07 -0700662 shader.append(gFS_Main_AddDitherToGradient);
Romain Guyac670c02010-07-27 17:39:27 -0700663 }
664 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700665 if (description.isPoint) {
666 shader.append(gFS_Main_PointBitmapTexCoords);
667 }
Romain Guy889f8d12010-07-29 14:37:42 -0700668 if (!description.isBitmapNpot) {
669 shader.append(gFS_Main_FetchBitmap);
670 } else {
671 shader.append(gFS_Main_FetchBitmapNpot);
672 }
Romain Guyac670c02010-07-27 17:39:27 -0700673 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700674 bool applyModulate = false;
Romain Guyac670c02010-07-27 17:39:27 -0700675 // Case when we have two shaders set
676 if (description.hasGradient && description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700677 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
Romain Guyac670c02010-07-27 17:39:27 -0700678 if (description.isBitmapFirst) {
679 shader.append(gFS_Main_BlendShadersBG);
680 } else {
681 shader.append(gFS_Main_BlendShadersGB);
682 }
Romain Guy707b2f72010-10-11 16:34:59 -0700683 shader.append(gFS_Main_BlendShaders_Modulate[op]);
Romain Guy740bf2b2011-04-26 15:33:10 -0700684 applyModulate = true;
Romain Guy889f8d12010-07-29 14:37:42 -0700685 } else {
686 if (description.hasGradient) {
Romain Guy707b2f72010-10-11 16:34:59 -0700687 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
688 shader.append(gFS_Main_GradientShader_Modulate[op]);
Romain Guy740bf2b2011-04-26 15:33:10 -0700689 applyModulate = true;
Romain Guy889f8d12010-07-29 14:37:42 -0700690 } else if (description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700691 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
692 shader.append(gFS_Main_BitmapShader_Modulate[op]);
Romain Guy740bf2b2011-04-26 15:33:10 -0700693 applyModulate = true;
Romain Guy889f8d12010-07-29 14:37:42 -0700694 }
Romain Guyac670c02010-07-27 17:39:27 -0700695 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700696 if (description.modulate && applyModulate) {
Romain Guy41210632012-07-16 17:04:24 -0700697 if (description.hasGammaCorrection) {
698 shader.append(gFS_Main_ModulateColor_ApplyGamma);
699 } else {
700 shader.append(gFS_Main_ModulateColor);
701 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700702 }
Romain Guyac670c02010-07-27 17:39:27 -0700703 // Apply the color op if needed
704 shader.append(gFS_Main_ApplyColorOp[description.colorOp]);
705 // Output the fragment
Romain Guya5aed0d2010-09-09 14:42:43 -0700706 if (!blendFramebuffer) {
707 shader.append(gFS_Main_FragColor);
708 } else {
Romain Guyf607bdc2010-09-10 19:20:06 -0700709 shader.append(!description.swapSrcDst ?
710 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap);
Romain Guya5aed0d2010-09-09 14:42:43 -0700711 }
Romain Guyac670c02010-07-27 17:39:27 -0700712 }
713 // End the shader
714 shader.append(gFS_Footer);
715
Romain Guyc15008e2010-11-10 11:59:15 -0800716#if DEBUG_PROGRAMS
Romain Guydb1938e2010-08-02 18:50:22 -0700717 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
718 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800719#endif
Romain Guydb1938e2010-08-02 18:50:22 -0700720
Romain Guyac670c02010-07-27 17:39:27 -0700721 return shader;
722}
723
Romain Guy48daa542010-08-10 19:21:34 -0700724void ProgramCache::generateBlend(String8& shader, const char* name, SkXfermode::Mode mode) {
Romain Guyac670c02010-07-27 17:39:27 -0700725 shader.append("\nvec4 ");
726 shader.append(name);
727 shader.append("(vec4 src, vec4 dst) {\n");
728 shader.append(" ");
Romain Guy48daa542010-08-10 19:21:34 -0700729 shader.append(gBlendOps[mode]);
Romain Guyac670c02010-07-27 17:39:27 -0700730 shader.append("}\n");
731}
732
Romain Guy889f8d12010-07-29 14:37:42 -0700733void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) {
Romain Guy63553472012-07-18 20:04:14 -0700734 shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700735 if (wrapS == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700736 shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700737 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n");
738 }
739 if (wrapT == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700740 shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700741 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n");
742 }
743 shader.append(" return vec2(");
744 switch (wrapS) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700745 case GL_CLAMP_TO_EDGE:
746 shader.append("texCoords.x");
747 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700748 case GL_REPEAT:
749 shader.append("mod(texCoords.x, 1.0)");
750 break;
751 case GL_MIRRORED_REPEAT:
752 shader.append("xMod2");
753 break;
754 }
755 shader.append(", ");
756 switch (wrapT) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700757 case GL_CLAMP_TO_EDGE:
758 shader.append("texCoords.y");
759 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700760 case GL_REPEAT:
761 shader.append("mod(texCoords.y, 1.0)");
762 break;
763 case GL_MIRRORED_REPEAT:
764 shader.append("yMod2");
765 break;
766 }
767 shader.append(");\n");
768 shader.append("}\n");
769}
770
Romain Guydb1938e2010-08-02 18:50:22 -0700771void ProgramCache::printLongString(const String8& shader) const {
772 ssize_t index = 0;
773 ssize_t lastIndex = 0;
774 const char* str = shader.string();
775 while ((index = shader.find("\n", index)) > -1) {
776 String8 line(str, index - lastIndex);
777 if (line.length() == 0) line.append("\n");
778 PROGRAM_LOGD("%s", line.string());
779 index++;
780 str += (index - lastIndex);
781 lastIndex = index;
782 }
783}
784
Romain Guyac670c02010-07-27 17:39:27 -0700785}; // namespace uirenderer
786}; // namespace android