blob: 0e77cb23df81759e4d1d5d9c55787f33599c0733 [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 Guy8e025de2012-07-18 17:44:07 -070072// TODO: These values are used to sample from textures,
73// they may need to be highp
Romain Guy42e1e0d2012-07-30 14:47:51 -070074const char* gVS_Header_Varyings_HasGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -070075 // Linear
Romain Guy63553472012-07-18 20:04:14 -070076 "varying highp vec2 linear;\n",
Romain Guy42e1e0d2012-07-30 14:47:51 -070077 "varying highp float linear;\n",
Romain Guy211efea2012-07-31 21:16:07 -070078
Romain Guyee916f12010-09-20 17:53:08 -070079 // Circular
Romain Guy63553472012-07-18 20:04:14 -070080 "varying highp vec2 circular;\n",
Romain Guy42e1e0d2012-07-30 14:47:51 -070081 "varying highp vec2 circular;\n",
Romain Guy211efea2012-07-31 21:16:07 -070082
Romain Guyee916f12010-09-20 17:53:08 -070083 // Sweep
Romain Guy42e1e0d2012-07-30 14:47:51 -070084 "varying highp vec2 sweep;\n",
85 "varying highp vec2 sweep;\n",
Romain Guyee916f12010-09-20 17:53:08 -070086};
Romain Guyac670c02010-07-27 17:39:27 -070087const char* gVS_Main =
88 "\nvoid main(void) {\n";
89const char* gVS_Main_OutTexCoords =
90 " outTexCoords = texCoords;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -070091const char* gVS_Main_OutTransformedTexCoords =
92 " outTexCoords = (mainTextureTransform * vec4(texCoords, 0.0, 1.0)).xy;\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -070093const char* gVS_Main_OutGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -070094 // Linear
Romain Guy7537f852010-10-11 14:38:28 -070095 " linear = vec2((screenSpace * position).x, 0.5);\n",
Romain Guy42e1e0d2012-07-30 14:47:51 -070096 " linear = (screenSpace * position).x;\n",
Romain Guy211efea2012-07-31 21:16:07 -070097
Romain Guyee916f12010-09-20 17:53:08 -070098 // Circular
Romain Guy14830942010-10-07 15:07:45 -070099 " circular = (screenSpace * position).xy;\n",
Romain Guy42e1e0d2012-07-30 14:47:51 -0700100 " circular = (screenSpace * position).xy;\n",
Romain Guy211efea2012-07-31 21:16:07 -0700101
Romain Guyee916f12010-09-20 17:53:08 -0700102 // Sweep
Romain Guy42e1e0d2012-07-30 14:47:51 -0700103 " sweep = (screenSpace * position).xy;\n",
104 " sweep = (screenSpace * position).xy;\n",
Romain Guyee916f12010-09-20 17:53:08 -0700105};
Romain Guy889f8d12010-07-29 14:37:42 -0700106const char* gVS_Main_OutBitmapTexCoords =
Romain Guy707b2f72010-10-11 16:34:59 -0700107 " outBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -0700108const char* gVS_Main_OutPointBitmapTexCoords =
109 " outPointBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700110const char* gVS_Main_Position =
111 " gl_Position = transform * position;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -0700112const char* gVS_Main_PointSize =
113 " gl_PointSize = pointSize;\n";
Chet Haase99585ad2011-05-02 15:00:16 -0700114const char* gVS_Main_AA =
115 " widthProportion = vtxWidth;\n"
116 " lengthProportion = vtxLength;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700117const char* gVS_Footer =
118 "}\n\n";
119
120///////////////////////////////////////////////////////////////////////////////
121// Fragment shaders snippets
122///////////////////////////////////////////////////////////////////////////////
123
Romain Guya5aed0d2010-09-09 14:42:43 -0700124const char* gFS_Header_Extension_FramebufferFetch =
125 "#extension GL_NV_shader_framebuffer_fetch : enable\n\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700126const char* gFS_Header_Extension_ExternalTexture =
127 "#extension GL_OES_EGL_image_external : require\n\n";
Romain Guyac670c02010-07-27 17:39:27 -0700128const char* gFS_Header =
129 "precision mediump float;\n\n";
130const char* gFS_Uniforms_Color =
131 "uniform vec4 color;\n";
Chet Haase99585ad2011-05-02 15:00:16 -0700132const char* gFS_Uniforms_AA =
Chet Haase5b0200b2011-04-13 17:58:08 -0700133 "uniform float boundaryWidth;\n"
Chet Haase99585ad2011-05-02 15:00:16 -0700134 "uniform float inverseBoundaryWidth;\n"
135 "uniform float boundaryLength;\n"
136 "uniform float inverseBoundaryLength;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -0700137const char* gFS_Header_Uniforms_PointHasBitmap =
138 "uniform vec2 textureDimension;\n"
139 "uniform float pointSize;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700140const char* gFS_Uniforms_TextureSampler =
141 "uniform sampler2D sampler;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700142const char* gFS_Uniforms_ExternalTextureSampler =
143 "uniform samplerExternalOES sampler;\n";
Romain Guy211efea2012-07-31 21:16:07 -0700144#define FS_UNIFORMS_DITHER \
145 "uniform float ditherSize;\n" \
146 "uniform sampler2D ditherSampler;\n"
147#define FS_UNIFORMS_GRADIENT \
148 "uniform vec4 startColor;\n" \
149 "uniform vec4 endColor;\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700150const char* gFS_Uniforms_GradientSampler[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700151 // Linear
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 // Circular
Romain Guy211efea2012-07-31 21:16:07 -0700156 FS_UNIFORMS_DITHER "uniform sampler2D gradientSampler;\n",
157 FS_UNIFORMS_DITHER FS_UNIFORMS_GRADIENT,
158
Romain Guyee916f12010-09-20 17:53:08 -0700159 // Sweep
Romain Guy211efea2012-07-31 21:16:07 -0700160 FS_UNIFORMS_DITHER "uniform sampler2D gradientSampler;\n",
161 FS_UNIFORMS_DITHER FS_UNIFORMS_GRADIENT
Romain Guyee916f12010-09-20 17:53:08 -0700162};
Romain Guyac670c02010-07-27 17:39:27 -0700163const char* gFS_Uniforms_BitmapSampler =
164 "uniform sampler2D bitmapSampler;\n";
165const char* gFS_Uniforms_ColorOp[4] = {
166 // None
167 "",
168 // Matrix
169 "uniform mat4 colorMatrix;\n"
170 "uniform vec4 colorMatrixVector;\n",
171 // Lighting
Romain Guydb1938e2010-08-02 18:50:22 -0700172 "uniform vec4 lightingMul;\n"
173 "uniform vec4 lightingAdd;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700174 // PorterDuff
Romain Guydb1938e2010-08-02 18:50:22 -0700175 "uniform vec4 colorBlend;\n"
Romain Guyac670c02010-07-27 17:39:27 -0700176};
Romain Guy41210632012-07-16 17:04:24 -0700177const char* gFS_Uniforms_Gamma =
178 "uniform float gamma;\n";
179
Romain Guyac670c02010-07-27 17:39:27 -0700180const char* gFS_Main =
181 "\nvoid main(void) {\n"
Romain Guy7fbcc042010-08-04 15:40:07 -0700182 " lowp vec4 fragColor;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700183
Romain Guyed6fcb02011-03-21 13:11:28 -0700184const char* gFS_Main_PointBitmapTexCoords =
Romain Guy63553472012-07-18 20:04:14 -0700185 " highp vec2 outBitmapTexCoords = outPointBitmapTexCoords + "
Romain Guyed6fcb02011-03-21 13:11:28 -0700186 "((gl_PointCoord - vec2(0.5, 0.5)) * textureDimension * vec2(pointSize, pointSize));\n";
187
Romain Guy211efea2012-07-31 21:16:07 -0700188#define FS_MAIN_DITHER \
189 "texture2D(ditherSampler, gl_FragCoord.xy * ditherSize).a * ditherSize * ditherSize"
190const char* gFS_Main_AddDitherToGradient =
191 " gradientColor += " FS_MAIN_DITHER ";\n";
192
Romain Guy707b2f72010-10-11 16:34:59 -0700193// Fast cases
194const char* gFS_Fast_SingleColor =
195 "\nvoid main(void) {\n"
196 " gl_FragColor = color;\n"
197 "}\n\n";
198const char* gFS_Fast_SingleTexture =
199 "\nvoid main(void) {\n"
200 " gl_FragColor = texture2D(sampler, outTexCoords);\n"
201 "}\n\n";
202const char* gFS_Fast_SingleModulateTexture =
203 "\nvoid main(void) {\n"
204 " gl_FragColor = color.a * texture2D(sampler, outTexCoords);\n"
205 "}\n\n";
206const char* gFS_Fast_SingleA8Texture =
207 "\nvoid main(void) {\n"
Romain Guy9db91242010-10-12 13:13:09 -0700208 " gl_FragColor = texture2D(sampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700209 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700210const char* gFS_Fast_SingleA8Texture_ApplyGamma =
211 "\nvoid main(void) {\n"
212 " gl_FragColor = vec4(0.0, 0.0, 0.0, pow(texture2D(sampler, outTexCoords).a, gamma));\n"
213 "}\n\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700214const char* gFS_Fast_SingleModulateA8Texture =
215 "\nvoid main(void) {\n"
216 " gl_FragColor = color * texture2D(sampler, outTexCoords).a;\n"
217 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700218const char* gFS_Fast_SingleModulateA8Texture_ApplyGamma =
219 "\nvoid main(void) {\n"
220 " gl_FragColor = color * pow(texture2D(sampler, outTexCoords).a, gamma);\n"
221 "}\n\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -0700222const char* gFS_Fast_SingleGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700223 "\nvoid main(void) {\n"
Romain Guy211efea2012-07-31 21:16:07 -0700224 " gl_FragColor = " FS_MAIN_DITHER " + texture2D(gradientSampler, linear);\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700225 "}\n\n",
226 "\nvoid main(void) {\n"
Romain Guy211efea2012-07-31 21:16:07 -0700227 " gl_FragColor = " FS_MAIN_DITHER " + mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700228 "}\n\n"
229};
230const char* gFS_Fast_SingleModulateGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700231 "\nvoid main(void) {\n"
Romain Guy211efea2012-07-31 21:16:07 -0700232 " gl_FragColor " FS_MAIN_DITHER " + color.a * texture2D(gradientSampler, linear);\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700233 "}\n\n",
234 "\nvoid main(void) {\n"
Romain Guy211efea2012-07-31 21:16:07 -0700235 " gl_FragColor " FS_MAIN_DITHER " + color.a * mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700236 "}\n\n"
237};
Romain Guy707b2f72010-10-11 16:34:59 -0700238
239// General case
Romain Guyac670c02010-07-27 17:39:27 -0700240const char* gFS_Main_FetchColor =
241 " fragColor = color;\n";
Romain Guy740bf2b2011-04-26 15:33:10 -0700242const char* gFS_Main_ModulateColor =
243 " fragColor *= color.a;\n";
Romain Guy41210632012-07-16 17:04:24 -0700244const char* gFS_Main_ModulateColor_ApplyGamma =
245 " fragColor *= pow(color.a, gamma);\n";
Chet Haase99585ad2011-05-02 15:00:16 -0700246const char* gFS_Main_AccountForAA =
247 " if (widthProportion < boundaryWidth) {\n"
248 " fragColor *= (widthProportion * inverseBoundaryWidth);\n"
249 " } else if (widthProportion > (1.0 - boundaryWidth)) {\n"
250 " fragColor *= ((1.0 - widthProportion) * inverseBoundaryWidth);\n"
251 " }\n"
252 " if (lengthProportion < boundaryLength) {\n"
253 " fragColor *= (lengthProportion * inverseBoundaryLength);\n"
254 " } else if (lengthProportion > (1.0 - boundaryLength)) {\n"
255 " fragColor *= ((1.0 - lengthProportion) * inverseBoundaryLength);\n"
Chet Haase5b0200b2011-04-13 17:58:08 -0700256 " }\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700257const char* gFS_Main_FetchTexture[2] = {
258 // Don't modulate
259 " fragColor = texture2D(sampler, outTexCoords);\n",
260 // Modulate
261 " fragColor = color * texture2D(sampler, outTexCoords);\n"
262};
263const char* gFS_Main_FetchA8Texture[2] = {
264 // Don't modulate
Romain Guy9db91242010-10-12 13:13:09 -0700265 " fragColor = texture2D(sampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700266 // Modulate
267 " fragColor = color * texture2D(sampler, outTexCoords).a;\n"
268};
Romain Guy42e1e0d2012-07-30 14:47:51 -0700269const char* gFS_Main_FetchGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700270 // Linear
Romain Guy211efea2012-07-31 21:16:07 -0700271 " highp vec4 gradientColor = texture2D(gradientSampler, linear);\n",
272
273 " highp vec4 gradientColor = mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n",
274
Romain Guyee916f12010-09-20 17:53:08 -0700275 // Circular
Romain Guy211efea2012-07-31 21:16:07 -0700276 " highp vec4 gradientColor = texture2D(gradientSampler, vec2(length(circular), 0.5));\n",
277
278 " highp vec4 gradientColor = mix(startColor, endColor, clamp(length(circular), 0.0, 1.0));\n",
279
Romain Guyee916f12010-09-20 17:53:08 -0700280 // Sweep
Romain Guy63553472012-07-18 20:04:14 -0700281 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy211efea2012-07-31 21:16:07 -0700282 " highp vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n",
283
Romain Guy42e1e0d2012-07-30 14:47:51 -0700284 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy211efea2012-07-31 21:16:07 -0700285 " highp vec4 gradientColor = mix(startColor, endColor, clamp(index - floor(index), 0.0, 1.0));\n"
Romain Guyee916f12010-09-20 17:53:08 -0700286};
Romain Guyac670c02010-07-27 17:39:27 -0700287const char* gFS_Main_FetchBitmap =
288 " vec4 bitmapColor = texture2D(bitmapSampler, outBitmapTexCoords);\n";
Romain Guy889f8d12010-07-29 14:37:42 -0700289const char* gFS_Main_FetchBitmapNpot =
290 " vec4 bitmapColor = texture2D(bitmapSampler, wrap(outBitmapTexCoords));\n";
Romain Guyac670c02010-07-27 17:39:27 -0700291const char* gFS_Main_BlendShadersBG =
Romain Guyac670c02010-07-27 17:39:27 -0700292 " fragColor = blendShaders(gradientColor, bitmapColor)";
Romain Guy06f96e22010-07-30 19:18:16 -0700293const char* gFS_Main_BlendShadersGB =
294 " fragColor = blendShaders(bitmapColor, gradientColor)";
Romain Guy707b2f72010-10-11 16:34:59 -0700295const char* gFS_Main_BlendShaders_Modulate[3] = {
296 // Don't modulate
297 ";\n",
298 // Modulate
299 " * fragColor.a;\n",
300 // Modulate with alpha 8 texture
301 " * texture2D(sampler, outTexCoords).a;\n"
302};
303const char* gFS_Main_GradientShader_Modulate[3] = {
304 // Don't modulate
305 " fragColor = gradientColor;\n",
306 // Modulate
307 " fragColor = gradientColor * fragColor.a;\n",
308 // Modulate with alpha 8 texture
309 " fragColor = gradientColor * texture2D(sampler, outTexCoords).a;\n"
310 };
311const char* gFS_Main_BitmapShader_Modulate[3] = {
312 // Don't modulate
313 " fragColor = bitmapColor;\n",
314 // Modulate
315 " fragColor = bitmapColor * fragColor.a;\n",
316 // Modulate with alpha 8 texture
317 " fragColor = bitmapColor * texture2D(sampler, outTexCoords).a;\n"
318 };
Romain Guyac670c02010-07-27 17:39:27 -0700319const char* gFS_Main_FragColor =
320 " gl_FragColor = fragColor;\n";
Romain Guya5aed0d2010-09-09 14:42:43 -0700321const char* gFS_Main_FragColor_Blend =
322 " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n";
Romain Guyf607bdc2010-09-10 19:20:06 -0700323const char* gFS_Main_FragColor_Blend_Swap =
324 " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n";
Romain Guyac670c02010-07-27 17:39:27 -0700325const char* gFS_Main_ApplyColorOp[4] = {
326 // None
327 "",
328 // Matrix
Romain Guydb1938e2010-08-02 18:50:22 -0700329 // TODO: Fix premultiplied alpha computations for color matrix
Romain Guyac670c02010-07-27 17:39:27 -0700330 " fragColor *= colorMatrix;\n"
Romain Guydb1938e2010-08-02 18:50:22 -0700331 " fragColor += colorMatrixVector;\n"
332 " fragColor.rgb *= fragColor.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700333 // Lighting
Romain Guydb1938e2010-08-02 18:50:22 -0700334 " float lightingAlpha = fragColor.a;\n"
335 " fragColor = min(fragColor * lightingMul + (lightingAdd * lightingAlpha), lightingAlpha);\n"
336 " fragColor.a = lightingAlpha;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700337 // PorterDuff
338 " fragColor = blendColors(colorBlend, fragColor);\n"
339};
340const char* gFS_Footer =
341 "}\n\n";
342
343///////////////////////////////////////////////////////////////////////////////
344// PorterDuff snippets
345///////////////////////////////////////////////////////////////////////////////
346
Romain Guy48daa542010-08-10 19:21:34 -0700347const char* gBlendOps[18] = {
Romain Guyac670c02010-07-27 17:39:27 -0700348 // Clear
349 "return vec4(0.0, 0.0, 0.0, 0.0);\n",
350 // Src
351 "return src;\n",
352 // Dst
353 "return dst;\n",
354 // SrcOver
Romain Guy06f96e22010-07-30 19:18:16 -0700355 "return src + dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700356 // DstOver
Romain Guy06f96e22010-07-30 19:18:16 -0700357 "return dst + src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700358 // SrcIn
Romain Guy06f96e22010-07-30 19:18:16 -0700359 "return src * dst.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700360 // DstIn
Romain Guy06f96e22010-07-30 19:18:16 -0700361 "return dst * src.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700362 // SrcOut
Romain Guy06f96e22010-07-30 19:18:16 -0700363 "return src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700364 // DstOut
Romain Guy06f96e22010-07-30 19:18:16 -0700365 "return dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700366 // SrcAtop
367 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n",
368 // DstAtop
369 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n",
370 // Xor
Romain Guy48daa542010-08-10 19:21:34 -0700371 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, "
Romain Guyac670c02010-07-27 17:39:27 -0700372 "src.a + dst.a - 2.0 * src.a * dst.a);\n",
Romain Guy48daa542010-08-10 19:21:34 -0700373 // Add
374 "return min(src + dst, 1.0);\n",
375 // Multiply
376 "return src * dst;\n",
377 // Screen
378 "return src + dst - src * dst;\n",
379 // Overlay
380 "return clamp(vec4(mix("
381 "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), "
382 "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), "
383 "step(dst.a, 2.0 * dst.rgb)), "
384 "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n",
385 // Darken
386 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
387 "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
388 // Lighten
389 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
390 "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 -0700391};
392
393///////////////////////////////////////////////////////////////////////////////
394// Constructors/destructors
395///////////////////////////////////////////////////////////////////////////////
396
397ProgramCache::ProgramCache() {
398}
399
400ProgramCache::~ProgramCache() {
401 clear();
402}
403
404///////////////////////////////////////////////////////////////////////////////
405// Cache management
406///////////////////////////////////////////////////////////////////////////////
407
408void ProgramCache::clear() {
Romain Guy67f27952010-12-07 20:09:23 -0800409 PROGRAM_LOGD("Clearing program cache");
410
Romain Guyac670c02010-07-27 17:39:27 -0700411 size_t count = mCache.size();
412 for (size_t i = 0; i < count; i++) {
413 delete mCache.valueAt(i);
414 }
415 mCache.clear();
416}
417
418Program* ProgramCache::get(const ProgramDescription& description) {
419 programid key = description.key();
420 ssize_t index = mCache.indexOfKey(key);
421 Program* program = NULL;
422 if (index < 0) {
Romain Guyee916f12010-09-20 17:53:08 -0700423 description.log("Could not find program");
Romain Guyac670c02010-07-27 17:39:27 -0700424 program = generateProgram(description, key);
425 mCache.add(key, program);
426 } else {
427 program = mCache.valueAt(index);
428 }
429 return program;
430}
431
432///////////////////////////////////////////////////////////////////////////////
433// Program generation
434///////////////////////////////////////////////////////////////////////////////
435
436Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) {
437 String8 vertexShader = generateVertexShader(description);
438 String8 fragmentShader = generateFragmentShader(description);
439
Romain Guy42e1e0d2012-07-30 14:47:51 -0700440 return new Program(description, vertexShader.string(), fragmentShader.string());
441}
442
443static inline size_t gradientIndex(const ProgramDescription& description) {
444 return description.gradientType * 2 + description.isSimpleGradient;
Romain Guyac670c02010-07-27 17:39:27 -0700445}
446
447String8 ProgramCache::generateVertexShader(const ProgramDescription& description) {
448 // Add attributes
449 String8 shader(gVS_Header_Attributes);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700450 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700451 shader.append(gVS_Header_Attributes_TexCoords);
452 }
Chet Haase99585ad2011-05-02 15:00:16 -0700453 if (description.isAA) {
454 shader.append(gVS_Header_Attributes_AAParameters);
Chet Haase5b0200b2011-04-13 17:58:08 -0700455 }
Romain Guyac670c02010-07-27 17:39:27 -0700456 // Uniforms
457 shader.append(gVS_Header_Uniforms);
Romain Guy8f0095c2011-05-02 17:24:22 -0700458 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700459 shader.append(gVS_Header_Uniforms_TextureTransform);
460 }
Romain Guyac670c02010-07-27 17:39:27 -0700461 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700462 shader.append(gVS_Header_Uniforms_HasGradient[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700463 }
Romain Guy889f8d12010-07-29 14:37:42 -0700464 if (description.hasBitmap) {
465 shader.append(gVS_Header_Uniforms_HasBitmap);
466 }
Romain Guyed6fcb02011-03-21 13:11:28 -0700467 if (description.isPoint) {
468 shader.append(gVS_Header_Uniforms_IsPoint);
469 }
Romain Guyac670c02010-07-27 17:39:27 -0700470 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700471 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700472 shader.append(gVS_Header_Varyings_HasTexture);
473 }
Chet Haase99585ad2011-05-02 15:00:16 -0700474 if (description.isAA) {
475 shader.append(gVS_Header_Varyings_IsAA);
Chet Haase5b0200b2011-04-13 17:58:08 -0700476 }
Romain Guyac670c02010-07-27 17:39:27 -0700477 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700478 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700479 }
480 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700481 shader.append(description.isPoint ?
Romain Guy63553472012-07-18 20:04:14 -0700482 gVS_Header_Varyings_PointHasBitmap :
483 gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700484 }
485
486 // Begin the shader
487 shader.append(gVS_Main); {
Romain Guy8f0095c2011-05-02 17:24:22 -0700488 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700489 shader.append(gVS_Main_OutTransformedTexCoords);
Romain Guy8f0095c2011-05-02 17:24:22 -0700490 } else if (description.hasTexture || description.hasExternalTexture) {
491 shader.append(gVS_Main_OutTexCoords);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700492 }
Chet Haase99585ad2011-05-02 15:00:16 -0700493 if (description.isAA) {
494 shader.append(gVS_Main_AA);
Chet Haase5b0200b2011-04-13 17:58:08 -0700495 }
Romain Guyac670c02010-07-27 17:39:27 -0700496 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700497 shader.append(gVS_Main_OutGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700498 }
Romain Guy889f8d12010-07-29 14:37:42 -0700499 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700500 shader.append(description.isPoint ?
501 gVS_Main_OutPointBitmapTexCoords :
502 gVS_Main_OutBitmapTexCoords);
503 }
504 if (description.isPoint) {
505 shader.append(gVS_Main_PointSize);
Romain Guy889f8d12010-07-29 14:37:42 -0700506 }
Romain Guyac670c02010-07-27 17:39:27 -0700507 // Output transformed position
508 shader.append(gVS_Main_Position);
509 }
510 // End the shader
511 shader.append(gVS_Footer);
512
513 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string());
514
515 return shader;
516}
517
518String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) {
Romain Guya5aed0d2010-09-09 14:42:43 -0700519 String8 shader;
520
Romain Guy707b2f72010-10-11 16:34:59 -0700521 const bool blendFramebuffer = description.framebufferMode >= SkXfermode::kPlus_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700522 if (blendFramebuffer) {
523 shader.append(gFS_Header_Extension_FramebufferFetch);
524 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700525 if (description.hasExternalTexture) {
526 shader.append(gFS_Header_Extension_ExternalTexture);
527 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700528
529 shader.append(gFS_Header);
Romain Guyac670c02010-07-27 17:39:27 -0700530
531 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700532 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700533 shader.append(gVS_Header_Varyings_HasTexture);
534 }
Chet Haase99585ad2011-05-02 15:00:16 -0700535 if (description.isAA) {
536 shader.append(gVS_Header_Varyings_IsAA);
Chet Haase5b0200b2011-04-13 17:58:08 -0700537 }
Romain Guyac670c02010-07-27 17:39:27 -0700538 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700539 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700540 }
541 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700542 shader.append(description.isPoint ?
Romain Guy63553472012-07-18 20:04:14 -0700543 gVS_Header_Varyings_PointHasBitmap :
544 gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700545 }
546
Romain Guyac670c02010-07-27 17:39:27 -0700547 // Uniforms
Romain Guy707b2f72010-10-11 16:34:59 -0700548 int modulateOp = MODULATE_OP_NO_MODULATE;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700549 const bool singleColor = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700550 !description.hasGradient && !description.hasBitmap;
551
552 if (description.modulate || singleColor) {
553 shader.append(gFS_Uniforms_Color);
554 if (!singleColor) modulateOp = MODULATE_OP_MODULATE;
555 }
Romain Guyac670c02010-07-27 17:39:27 -0700556 if (description.hasTexture) {
557 shader.append(gFS_Uniforms_TextureSampler);
Romain Guy8f0095c2011-05-02 17:24:22 -0700558 } else if (description.hasExternalTexture) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700559 shader.append(gFS_Uniforms_ExternalTextureSampler);
560 }
Chet Haase99585ad2011-05-02 15:00:16 -0700561 if (description.isAA) {
562 shader.append(gFS_Uniforms_AA);
Chet Haase5b0200b2011-04-13 17:58:08 -0700563 }
Romain Guyac670c02010-07-27 17:39:27 -0700564 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700565 shader.append(gFS_Uniforms_GradientSampler[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700566 }
Romain Guyed6fcb02011-03-21 13:11:28 -0700567 if (description.hasBitmap && description.isPoint) {
568 shader.append(gFS_Header_Uniforms_PointHasBitmap);
569 }
Romain Guy41210632012-07-16 17:04:24 -0700570 if (description.hasGammaCorrection) {
571 shader.append(gFS_Uniforms_Gamma);
572 }
Romain Guy707b2f72010-10-11 16:34:59 -0700573
574 // Optimization for common cases
Chet Haase99585ad2011-05-02 15:00:16 -0700575 if (!description.isAA && !blendFramebuffer &&
Chet Haase5b0200b2011-04-13 17:58:08 -0700576 description.colorOp == ProgramDescription::kColorNone && !description.isPoint) {
Romain Guy707b2f72010-10-11 16:34:59 -0700577 bool fast = false;
578
579 const bool noShader = !description.hasGradient && !description.hasBitmap;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700580 const bool singleTexture = (description.hasTexture || description.hasExternalTexture) &&
Romain Guy707b2f72010-10-11 16:34:59 -0700581 !description.hasAlpha8Texture && noShader;
582 const bool singleA8Texture = description.hasTexture &&
583 description.hasAlpha8Texture && noShader;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700584 const bool singleGradient = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700585 description.hasGradient && !description.hasBitmap &&
586 description.gradientType == ProgramDescription::kGradientLinear;
587
588 if (singleColor) {
589 shader.append(gFS_Fast_SingleColor);
590 fast = true;
591 } else if (singleTexture) {
592 if (!description.modulate) {
593 shader.append(gFS_Fast_SingleTexture);
594 } else {
595 shader.append(gFS_Fast_SingleModulateTexture);
596 }
597 fast = true;
598 } else if (singleA8Texture) {
599 if (!description.modulate) {
Romain Guy41210632012-07-16 17:04:24 -0700600 if (description.hasGammaCorrection) {
601 shader.append(gFS_Fast_SingleA8Texture_ApplyGamma);
602 } else {
603 shader.append(gFS_Fast_SingleA8Texture);
604 }
Romain Guy707b2f72010-10-11 16:34:59 -0700605 } else {
Romain Guy41210632012-07-16 17:04:24 -0700606 if (description.hasGammaCorrection) {
607 shader.append(gFS_Fast_SingleModulateA8Texture_ApplyGamma);
608 } else {
609 shader.append(gFS_Fast_SingleModulateA8Texture);
610 }
Romain Guy707b2f72010-10-11 16:34:59 -0700611 }
612 fast = true;
613 } else if (singleGradient) {
614 if (!description.modulate) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700615 shader.append(gFS_Fast_SingleGradient[description.isSimpleGradient]);
Romain Guy707b2f72010-10-11 16:34:59 -0700616 } else {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700617 shader.append(gFS_Fast_SingleModulateGradient[description.isSimpleGradient]);
Romain Guy707b2f72010-10-11 16:34:59 -0700618 }
619 fast = true;
620 }
621
622 if (fast) {
Romain Guyc15008e2010-11-10 11:59:15 -0800623#if DEBUG_PROGRAMS
Romain Guy707b2f72010-10-11 16:34:59 -0700624 PROGRAM_LOGD("*** Fast case:\n");
625 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
626 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800627#endif
Romain Guy707b2f72010-10-11 16:34:59 -0700628
629 return shader;
630 }
631 }
632
Romain Guyac670c02010-07-27 17:39:27 -0700633 if (description.hasBitmap) {
634 shader.append(gFS_Uniforms_BitmapSampler);
635 }
636 shader.append(gFS_Uniforms_ColorOp[description.colorOp]);
637
638 // Generate required functions
639 if (description.hasGradient && description.hasBitmap) {
Romain Guy48daa542010-08-10 19:21:34 -0700640 generateBlend(shader, "blendShaders", description.shadersMode);
Romain Guyac670c02010-07-27 17:39:27 -0700641 }
642 if (description.colorOp == ProgramDescription::kColorBlend) {
Romain Guy48daa542010-08-10 19:21:34 -0700643 generateBlend(shader, "blendColors", description.colorMode);
Romain Guyac670c02010-07-27 17:39:27 -0700644 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700645 if (blendFramebuffer) {
646 generateBlend(shader, "blendFramebuffer", description.framebufferMode);
647 }
Romain Guy889f8d12010-07-29 14:37:42 -0700648 if (description.isBitmapNpot) {
649 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
650 }
Romain Guyac670c02010-07-27 17:39:27 -0700651
652 // Begin the shader
653 shader.append(gFS_Main); {
654 // Stores the result in fragColor directly
Romain Guyaa6c24c2011-04-28 18:40:04 -0700655 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700656 if (description.hasAlpha8Texture) {
Romain Guy707b2f72010-10-11 16:34:59 -0700657 if (!description.hasGradient && !description.hasBitmap) {
658 shader.append(gFS_Main_FetchA8Texture[modulateOp]);
659 }
Romain Guyac670c02010-07-27 17:39:27 -0700660 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700661 shader.append(gFS_Main_FetchTexture[modulateOp]);
Romain Guyac670c02010-07-27 17:39:27 -0700662 }
663 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700664 if ((!description.hasGradient && !description.hasBitmap) || description.modulate) {
665 shader.append(gFS_Main_FetchColor);
666 }
Romain Guyac670c02010-07-27 17:39:27 -0700667 }
Chet Haase99585ad2011-05-02 15:00:16 -0700668 if (description.isAA) {
669 shader.append(gFS_Main_AccountForAA);
Chet Haase5b0200b2011-04-13 17:58:08 -0700670 }
Romain Guyac670c02010-07-27 17:39:27 -0700671 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700672 shader.append(gFS_Main_FetchGradient[gradientIndex(description)]);
Romain Guy211efea2012-07-31 21:16:07 -0700673 shader.append(gFS_Main_AddDitherToGradient);
Romain Guyac670c02010-07-27 17:39:27 -0700674 }
675 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700676 if (description.isPoint) {
677 shader.append(gFS_Main_PointBitmapTexCoords);
678 }
Romain Guy889f8d12010-07-29 14:37:42 -0700679 if (!description.isBitmapNpot) {
680 shader.append(gFS_Main_FetchBitmap);
681 } else {
682 shader.append(gFS_Main_FetchBitmapNpot);
683 }
Romain Guyac670c02010-07-27 17:39:27 -0700684 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700685 bool applyModulate = false;
Romain Guyac670c02010-07-27 17:39:27 -0700686 // Case when we have two shaders set
687 if (description.hasGradient && description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700688 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
Romain Guyac670c02010-07-27 17:39:27 -0700689 if (description.isBitmapFirst) {
690 shader.append(gFS_Main_BlendShadersBG);
691 } else {
692 shader.append(gFS_Main_BlendShadersGB);
693 }
Romain Guy707b2f72010-10-11 16:34:59 -0700694 shader.append(gFS_Main_BlendShaders_Modulate[op]);
Romain Guy740bf2b2011-04-26 15:33:10 -0700695 applyModulate = true;
Romain Guy889f8d12010-07-29 14:37:42 -0700696 } else {
697 if (description.hasGradient) {
Romain Guy707b2f72010-10-11 16:34:59 -0700698 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
699 shader.append(gFS_Main_GradientShader_Modulate[op]);
Romain Guy740bf2b2011-04-26 15:33:10 -0700700 applyModulate = true;
Romain Guy889f8d12010-07-29 14:37:42 -0700701 } else if (description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700702 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
703 shader.append(gFS_Main_BitmapShader_Modulate[op]);
Romain Guy740bf2b2011-04-26 15:33:10 -0700704 applyModulate = true;
Romain Guy889f8d12010-07-29 14:37:42 -0700705 }
Romain Guyac670c02010-07-27 17:39:27 -0700706 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700707 if (description.modulate && applyModulate) {
Romain Guy41210632012-07-16 17:04:24 -0700708 if (description.hasGammaCorrection) {
709 shader.append(gFS_Main_ModulateColor_ApplyGamma);
710 } else {
711 shader.append(gFS_Main_ModulateColor);
712 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700713 }
Romain Guyac670c02010-07-27 17:39:27 -0700714 // Apply the color op if needed
715 shader.append(gFS_Main_ApplyColorOp[description.colorOp]);
716 // Output the fragment
Romain Guya5aed0d2010-09-09 14:42:43 -0700717 if (!blendFramebuffer) {
718 shader.append(gFS_Main_FragColor);
719 } else {
Romain Guyf607bdc2010-09-10 19:20:06 -0700720 shader.append(!description.swapSrcDst ?
721 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap);
Romain Guya5aed0d2010-09-09 14:42:43 -0700722 }
Romain Guyac670c02010-07-27 17:39:27 -0700723 }
724 // End the shader
725 shader.append(gFS_Footer);
726
Romain Guyc15008e2010-11-10 11:59:15 -0800727#if DEBUG_PROGRAMS
Romain Guydb1938e2010-08-02 18:50:22 -0700728 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
729 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800730#endif
Romain Guydb1938e2010-08-02 18:50:22 -0700731
Romain Guyac670c02010-07-27 17:39:27 -0700732 return shader;
733}
734
Romain Guy48daa542010-08-10 19:21:34 -0700735void ProgramCache::generateBlend(String8& shader, const char* name, SkXfermode::Mode mode) {
Romain Guyac670c02010-07-27 17:39:27 -0700736 shader.append("\nvec4 ");
737 shader.append(name);
738 shader.append("(vec4 src, vec4 dst) {\n");
739 shader.append(" ");
Romain Guy48daa542010-08-10 19:21:34 -0700740 shader.append(gBlendOps[mode]);
Romain Guyac670c02010-07-27 17:39:27 -0700741 shader.append("}\n");
742}
743
Romain Guy889f8d12010-07-29 14:37:42 -0700744void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) {
Romain Guy63553472012-07-18 20:04:14 -0700745 shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700746 if (wrapS == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700747 shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700748 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n");
749 }
750 if (wrapT == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700751 shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700752 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n");
753 }
754 shader.append(" return vec2(");
755 switch (wrapS) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700756 case GL_CLAMP_TO_EDGE:
757 shader.append("texCoords.x");
758 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700759 case GL_REPEAT:
760 shader.append("mod(texCoords.x, 1.0)");
761 break;
762 case GL_MIRRORED_REPEAT:
763 shader.append("xMod2");
764 break;
765 }
766 shader.append(", ");
767 switch (wrapT) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700768 case GL_CLAMP_TO_EDGE:
769 shader.append("texCoords.y");
770 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700771 case GL_REPEAT:
772 shader.append("mod(texCoords.y, 1.0)");
773 break;
774 case GL_MIRRORED_REPEAT:
775 shader.append("yMod2");
776 break;
777 }
778 shader.append(");\n");
779 shader.append("}\n");
780}
781
Romain Guydb1938e2010-08-02 18:50:22 -0700782void ProgramCache::printLongString(const String8& shader) const {
783 ssize_t index = 0;
784 ssize_t lastIndex = 0;
785 const char* str = shader.string();
786 while ((index = shader.find("\n", index)) > -1) {
787 String8 line(str, index - lastIndex);
788 if (line.length() == 0) line.append("\n");
789 PROGRAM_LOGD("%s", line.string());
790 index++;
791 str += (index - lastIndex);
792 lastIndex = index;
793 }
794}
795
Romain Guyac670c02010-07-27 17:39:27 -0700796}; // namespace uirenderer
797}; // namespace android