blob: d67bfbe3d4ea5a72f3b140d3ef4a4c6a815a8244 [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"
Chet Haase99585ad2011-05-02 15:00:16 -0700132 "uniform float inverseBoundaryWidth;\n"
133 "uniform float boundaryLength;\n"
134 "uniform float inverseBoundaryLength;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -0700135const char* gFS_Header_Uniforms_PointHasBitmap =
136 "uniform vec2 textureDimension;\n"
137 "uniform float pointSize;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700138const char* gFS_Uniforms_TextureSampler =
139 "uniform sampler2D sampler;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700140const char* gFS_Uniforms_ExternalTextureSampler =
141 "uniform samplerExternalOES sampler;\n";
Romain Guy211efea2012-07-31 21:16:07 -0700142#define FS_UNIFORMS_DITHER \
143 "uniform float ditherSize;\n" \
144 "uniform sampler2D ditherSampler;\n"
145#define FS_UNIFORMS_GRADIENT \
146 "uniform vec4 startColor;\n" \
147 "uniform vec4 endColor;\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700148const char* gFS_Uniforms_GradientSampler[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700149 // Linear
Romain Guy211efea2012-07-31 21:16:07 -0700150 FS_UNIFORMS_DITHER "uniform sampler2D gradientSampler;\n",
151 FS_UNIFORMS_DITHER FS_UNIFORMS_GRADIENT,
152
Romain Guyee916f12010-09-20 17:53:08 -0700153 // Circular
Romain Guy211efea2012-07-31 21:16:07 -0700154 FS_UNIFORMS_DITHER "uniform sampler2D gradientSampler;\n",
155 FS_UNIFORMS_DITHER FS_UNIFORMS_GRADIENT,
156
Romain Guyee916f12010-09-20 17:53:08 -0700157 // Sweep
Romain Guy211efea2012-07-31 21:16:07 -0700158 FS_UNIFORMS_DITHER "uniform sampler2D gradientSampler;\n",
159 FS_UNIFORMS_DITHER FS_UNIFORMS_GRADIENT
Romain Guyee916f12010-09-20 17:53:08 -0700160};
Romain Guyac670c02010-07-27 17:39:27 -0700161const char* gFS_Uniforms_BitmapSampler =
162 "uniform sampler2D bitmapSampler;\n";
163const char* gFS_Uniforms_ColorOp[4] = {
164 // None
165 "",
166 // Matrix
167 "uniform mat4 colorMatrix;\n"
168 "uniform vec4 colorMatrixVector;\n",
169 // Lighting
Romain Guydb1938e2010-08-02 18:50:22 -0700170 "uniform vec4 lightingMul;\n"
171 "uniform vec4 lightingAdd;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700172 // PorterDuff
Romain Guydb1938e2010-08-02 18:50:22 -0700173 "uniform vec4 colorBlend;\n"
Romain Guyac670c02010-07-27 17:39:27 -0700174};
Romain Guy41210632012-07-16 17:04:24 -0700175const char* gFS_Uniforms_Gamma =
176 "uniform float gamma;\n";
177
Romain Guyac670c02010-07-27 17:39:27 -0700178const char* gFS_Main =
179 "\nvoid main(void) {\n"
Romain Guy7fbcc042010-08-04 15:40:07 -0700180 " lowp vec4 fragColor;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700181
Romain Guyed6fcb02011-03-21 13:11:28 -0700182const char* gFS_Main_PointBitmapTexCoords =
Romain Guy63553472012-07-18 20:04:14 -0700183 " highp vec2 outBitmapTexCoords = outPointBitmapTexCoords + "
Romain Guyed6fcb02011-03-21 13:11:28 -0700184 "((gl_PointCoord - vec2(0.5, 0.5)) * textureDimension * vec2(pointSize, pointSize));\n";
185
Romain Guy211efea2012-07-31 21:16:07 -0700186#define FS_MAIN_DITHER \
187 "texture2D(ditherSampler, gl_FragCoord.xy * ditherSize).a * ditherSize * ditherSize"
188const char* gFS_Main_AddDitherToGradient =
189 " gradientColor += " FS_MAIN_DITHER ";\n";
190
Romain Guy707b2f72010-10-11 16:34:59 -0700191// Fast cases
192const char* gFS_Fast_SingleColor =
193 "\nvoid main(void) {\n"
194 " gl_FragColor = color;\n"
195 "}\n\n";
196const char* gFS_Fast_SingleTexture =
197 "\nvoid main(void) {\n"
198 " gl_FragColor = texture2D(sampler, outTexCoords);\n"
199 "}\n\n";
200const char* gFS_Fast_SingleModulateTexture =
201 "\nvoid main(void) {\n"
202 " gl_FragColor = color.a * texture2D(sampler, outTexCoords);\n"
203 "}\n\n";
204const char* gFS_Fast_SingleA8Texture =
205 "\nvoid main(void) {\n"
Romain Guy9db91242010-10-12 13:13:09 -0700206 " gl_FragColor = texture2D(sampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700207 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700208const char* gFS_Fast_SingleA8Texture_ApplyGamma =
209 "\nvoid main(void) {\n"
210 " gl_FragColor = vec4(0.0, 0.0, 0.0, pow(texture2D(sampler, outTexCoords).a, gamma));\n"
211 "}\n\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700212const char* gFS_Fast_SingleModulateA8Texture =
213 "\nvoid main(void) {\n"
214 " gl_FragColor = color * texture2D(sampler, outTexCoords).a;\n"
215 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700216const char* gFS_Fast_SingleModulateA8Texture_ApplyGamma =
217 "\nvoid main(void) {\n"
218 " gl_FragColor = color * pow(texture2D(sampler, outTexCoords).a, gamma);\n"
219 "}\n\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -0700220const char* gFS_Fast_SingleGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700221 "\nvoid main(void) {\n"
Romain Guy211efea2012-07-31 21:16:07 -0700222 " gl_FragColor = " FS_MAIN_DITHER " + texture2D(gradientSampler, linear);\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700223 "}\n\n",
224 "\nvoid main(void) {\n"
Romain Guy211efea2012-07-31 21:16:07 -0700225 " gl_FragColor = " FS_MAIN_DITHER " + mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700226 "}\n\n"
227};
228const char* gFS_Fast_SingleModulateGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700229 "\nvoid main(void) {\n"
Romain Guy211efea2012-07-31 21:16:07 -0700230 " gl_FragColor " FS_MAIN_DITHER " + color.a * texture2D(gradientSampler, linear);\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700231 "}\n\n",
232 "\nvoid main(void) {\n"
Romain Guy211efea2012-07-31 21:16:07 -0700233 " gl_FragColor " FS_MAIN_DITHER " + color.a * mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700234 "}\n\n"
235};
Romain Guy707b2f72010-10-11 16:34:59 -0700236
237// General case
Romain Guyac670c02010-07-27 17:39:27 -0700238const char* gFS_Main_FetchColor =
239 " fragColor = color;\n";
Romain Guy740bf2b2011-04-26 15:33:10 -0700240const char* gFS_Main_ModulateColor =
241 " fragColor *= color.a;\n";
Romain Guy41210632012-07-16 17:04:24 -0700242const char* gFS_Main_ModulateColor_ApplyGamma =
243 " fragColor *= pow(color.a, gamma);\n";
Chet Haase99585ad2011-05-02 15:00:16 -0700244const char* gFS_Main_AccountForAA =
245 " if (widthProportion < boundaryWidth) {\n"
246 " fragColor *= (widthProportion * inverseBoundaryWidth);\n"
247 " } else if (widthProportion > (1.0 - boundaryWidth)) {\n"
248 " fragColor *= ((1.0 - widthProportion) * inverseBoundaryWidth);\n"
249 " }\n"
250 " if (lengthProportion < boundaryLength) {\n"
251 " fragColor *= (lengthProportion * inverseBoundaryLength);\n"
252 " } else if (lengthProportion > (1.0 - boundaryLength)) {\n"
253 " fragColor *= ((1.0 - lengthProportion) * inverseBoundaryLength);\n"
Chet Haase5b0200b2011-04-13 17:58:08 -0700254 " }\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700255const char* gFS_Main_FetchTexture[2] = {
256 // Don't modulate
257 " fragColor = texture2D(sampler, outTexCoords);\n",
258 // Modulate
259 " fragColor = color * texture2D(sampler, outTexCoords);\n"
260};
261const char* gFS_Main_FetchA8Texture[2] = {
262 // Don't modulate
Romain Guy9db91242010-10-12 13:13:09 -0700263 " fragColor = texture2D(sampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700264 // Modulate
265 " fragColor = color * texture2D(sampler, outTexCoords).a;\n"
266};
Romain Guy42e1e0d2012-07-30 14:47:51 -0700267const char* gFS_Main_FetchGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700268 // Linear
Romain Guy320d46b2012-08-08 16:05:42 -0700269 " vec4 gradientColor = texture2D(gradientSampler, linear);\n",
Romain Guy211efea2012-07-31 21:16:07 -0700270
Romain Guy320d46b2012-08-08 16:05:42 -0700271 " vec4 gradientColor = mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700272
Romain Guyee916f12010-09-20 17:53:08 -0700273 // Circular
Romain Guy320d46b2012-08-08 16:05:42 -0700274 " vec4 gradientColor = texture2D(gradientSampler, vec2(length(circular), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700275
Romain Guy320d46b2012-08-08 16:05:42 -0700276 " vec4 gradientColor = mix(startColor, endColor, clamp(length(circular), 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700277
Romain Guyee916f12010-09-20 17:53:08 -0700278 // Sweep
Romain Guy63553472012-07-18 20:04:14 -0700279 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700280 " vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700281
Romain Guy42e1e0d2012-07-30 14:47:51 -0700282 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700283 " vec4 gradientColor = mix(startColor, endColor, clamp(index - floor(index), 0.0, 1.0));\n"
Romain Guyee916f12010-09-20 17:53:08 -0700284};
Romain Guyac670c02010-07-27 17:39:27 -0700285const char* gFS_Main_FetchBitmap =
286 " vec4 bitmapColor = texture2D(bitmapSampler, outBitmapTexCoords);\n";
Romain Guy889f8d12010-07-29 14:37:42 -0700287const char* gFS_Main_FetchBitmapNpot =
288 " vec4 bitmapColor = texture2D(bitmapSampler, wrap(outBitmapTexCoords));\n";
Romain Guyac670c02010-07-27 17:39:27 -0700289const char* gFS_Main_BlendShadersBG =
Romain Guyac670c02010-07-27 17:39:27 -0700290 " fragColor = blendShaders(gradientColor, bitmapColor)";
Romain Guy06f96e22010-07-30 19:18:16 -0700291const char* gFS_Main_BlendShadersGB =
292 " fragColor = blendShaders(bitmapColor, gradientColor)";
Romain Guy707b2f72010-10-11 16:34:59 -0700293const char* gFS_Main_BlendShaders_Modulate[3] = {
294 // Don't modulate
295 ";\n",
296 // Modulate
297 " * fragColor.a;\n",
298 // Modulate with alpha 8 texture
299 " * texture2D(sampler, outTexCoords).a;\n"
300};
301const char* gFS_Main_GradientShader_Modulate[3] = {
302 // Don't modulate
303 " fragColor = gradientColor;\n",
304 // Modulate
305 " fragColor = gradientColor * fragColor.a;\n",
306 // Modulate with alpha 8 texture
307 " fragColor = gradientColor * texture2D(sampler, outTexCoords).a;\n"
308 };
309const char* gFS_Main_BitmapShader_Modulate[3] = {
310 // Don't modulate
311 " fragColor = bitmapColor;\n",
312 // Modulate
313 " fragColor = bitmapColor * fragColor.a;\n",
314 // Modulate with alpha 8 texture
315 " fragColor = bitmapColor * texture2D(sampler, outTexCoords).a;\n"
316 };
Romain Guyac670c02010-07-27 17:39:27 -0700317const char* gFS_Main_FragColor =
318 " gl_FragColor = fragColor;\n";
Romain Guya5aed0d2010-09-09 14:42:43 -0700319const char* gFS_Main_FragColor_Blend =
320 " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n";
Romain Guyf607bdc2010-09-10 19:20:06 -0700321const char* gFS_Main_FragColor_Blend_Swap =
322 " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n";
Romain Guyac670c02010-07-27 17:39:27 -0700323const char* gFS_Main_ApplyColorOp[4] = {
324 // None
325 "",
326 // Matrix
Romain Guydb1938e2010-08-02 18:50:22 -0700327 // TODO: Fix premultiplied alpha computations for color matrix
Romain Guyac670c02010-07-27 17:39:27 -0700328 " fragColor *= colorMatrix;\n"
Romain Guydb1938e2010-08-02 18:50:22 -0700329 " fragColor += colorMatrixVector;\n"
330 " fragColor.rgb *= fragColor.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700331 // Lighting
Romain Guydb1938e2010-08-02 18:50:22 -0700332 " float lightingAlpha = fragColor.a;\n"
333 " fragColor = min(fragColor * lightingMul + (lightingAdd * lightingAlpha), lightingAlpha);\n"
334 " fragColor.a = lightingAlpha;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700335 // PorterDuff
336 " fragColor = blendColors(colorBlend, fragColor);\n"
337};
338const char* gFS_Footer =
339 "}\n\n";
340
341///////////////////////////////////////////////////////////////////////////////
342// PorterDuff snippets
343///////////////////////////////////////////////////////////////////////////////
344
Romain Guy48daa542010-08-10 19:21:34 -0700345const char* gBlendOps[18] = {
Romain Guyac670c02010-07-27 17:39:27 -0700346 // Clear
347 "return vec4(0.0, 0.0, 0.0, 0.0);\n",
348 // Src
349 "return src;\n",
350 // Dst
351 "return dst;\n",
352 // SrcOver
Romain Guy06f96e22010-07-30 19:18:16 -0700353 "return src + dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700354 // DstOver
Romain Guy06f96e22010-07-30 19:18:16 -0700355 "return dst + src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700356 // SrcIn
Romain Guy06f96e22010-07-30 19:18:16 -0700357 "return src * dst.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700358 // DstIn
Romain Guy06f96e22010-07-30 19:18:16 -0700359 "return dst * src.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700360 // SrcOut
Romain Guy06f96e22010-07-30 19:18:16 -0700361 "return src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700362 // DstOut
Romain Guy06f96e22010-07-30 19:18:16 -0700363 "return dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700364 // SrcAtop
365 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n",
366 // DstAtop
367 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n",
368 // Xor
Romain Guy48daa542010-08-10 19:21:34 -0700369 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, "
Romain Guyac670c02010-07-27 17:39:27 -0700370 "src.a + dst.a - 2.0 * src.a * dst.a);\n",
Romain Guy48daa542010-08-10 19:21:34 -0700371 // Add
372 "return min(src + dst, 1.0);\n",
373 // Multiply
374 "return src * dst;\n",
375 // Screen
376 "return src + dst - src * dst;\n",
377 // Overlay
378 "return clamp(vec4(mix("
379 "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), "
380 "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), "
381 "step(dst.a, 2.0 * dst.rgb)), "
382 "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n",
383 // Darken
384 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
385 "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
386 // Lighten
387 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
388 "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 -0700389};
390
391///////////////////////////////////////////////////////////////////////////////
392// Constructors/destructors
393///////////////////////////////////////////////////////////////////////////////
394
395ProgramCache::ProgramCache() {
396}
397
398ProgramCache::~ProgramCache() {
399 clear();
400}
401
402///////////////////////////////////////////////////////////////////////////////
403// Cache management
404///////////////////////////////////////////////////////////////////////////////
405
406void ProgramCache::clear() {
Romain Guy67f27952010-12-07 20:09:23 -0800407 PROGRAM_LOGD("Clearing program cache");
408
Romain Guyac670c02010-07-27 17:39:27 -0700409 size_t count = mCache.size();
410 for (size_t i = 0; i < count; i++) {
411 delete mCache.valueAt(i);
412 }
413 mCache.clear();
414}
415
416Program* ProgramCache::get(const ProgramDescription& description) {
417 programid key = description.key();
418 ssize_t index = mCache.indexOfKey(key);
419 Program* program = NULL;
420 if (index < 0) {
Romain Guyee916f12010-09-20 17:53:08 -0700421 description.log("Could not find program");
Romain Guyac670c02010-07-27 17:39:27 -0700422 program = generateProgram(description, key);
423 mCache.add(key, program);
424 } else {
425 program = mCache.valueAt(index);
426 }
427 return program;
428}
429
430///////////////////////////////////////////////////////////////////////////////
431// Program generation
432///////////////////////////////////////////////////////////////////////////////
433
434Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) {
435 String8 vertexShader = generateVertexShader(description);
436 String8 fragmentShader = generateFragmentShader(description);
437
Romain Guy42e1e0d2012-07-30 14:47:51 -0700438 return new Program(description, vertexShader.string(), fragmentShader.string());
439}
440
441static inline size_t gradientIndex(const ProgramDescription& description) {
442 return description.gradientType * 2 + description.isSimpleGradient;
Romain Guyac670c02010-07-27 17:39:27 -0700443}
444
445String8 ProgramCache::generateVertexShader(const ProgramDescription& description) {
446 // Add attributes
447 String8 shader(gVS_Header_Attributes);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700448 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700449 shader.append(gVS_Header_Attributes_TexCoords);
450 }
Chet Haase99585ad2011-05-02 15:00:16 -0700451 if (description.isAA) {
452 shader.append(gVS_Header_Attributes_AAParameters);
Chet Haase5b0200b2011-04-13 17:58:08 -0700453 }
Romain Guyac670c02010-07-27 17:39:27 -0700454 // Uniforms
455 shader.append(gVS_Header_Uniforms);
Romain Guy8f0095c2011-05-02 17:24:22 -0700456 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700457 shader.append(gVS_Header_Uniforms_TextureTransform);
458 }
Romain Guyac670c02010-07-27 17:39:27 -0700459 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700460 shader.append(gVS_Header_Uniforms_HasGradient[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700461 }
Romain Guy889f8d12010-07-29 14:37:42 -0700462 if (description.hasBitmap) {
463 shader.append(gVS_Header_Uniforms_HasBitmap);
464 }
Romain Guyed6fcb02011-03-21 13:11:28 -0700465 if (description.isPoint) {
466 shader.append(gVS_Header_Uniforms_IsPoint);
467 }
Romain Guyac670c02010-07-27 17:39:27 -0700468 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700469 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700470 shader.append(gVS_Header_Varyings_HasTexture);
471 }
Chet Haase99585ad2011-05-02 15:00:16 -0700472 if (description.isAA) {
473 shader.append(gVS_Header_Varyings_IsAA);
Chet Haase5b0200b2011-04-13 17:58:08 -0700474 }
Romain Guyac670c02010-07-27 17:39:27 -0700475 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700476 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700477 }
478 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700479 shader.append(description.isPoint ?
Romain Guy63553472012-07-18 20:04:14 -0700480 gVS_Header_Varyings_PointHasBitmap :
481 gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700482 }
483
484 // Begin the shader
485 shader.append(gVS_Main); {
Romain Guy8f0095c2011-05-02 17:24:22 -0700486 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700487 shader.append(gVS_Main_OutTransformedTexCoords);
Romain Guy8f0095c2011-05-02 17:24:22 -0700488 } else if (description.hasTexture || description.hasExternalTexture) {
489 shader.append(gVS_Main_OutTexCoords);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700490 }
Chet Haase99585ad2011-05-02 15:00:16 -0700491 if (description.isAA) {
492 shader.append(gVS_Main_AA);
Chet Haase5b0200b2011-04-13 17:58:08 -0700493 }
Romain Guyac670c02010-07-27 17:39:27 -0700494 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700495 shader.append(gVS_Main_OutGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700496 }
Romain Guy889f8d12010-07-29 14:37:42 -0700497 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700498 shader.append(description.isPoint ?
499 gVS_Main_OutPointBitmapTexCoords :
500 gVS_Main_OutBitmapTexCoords);
501 }
502 if (description.isPoint) {
503 shader.append(gVS_Main_PointSize);
Romain Guy889f8d12010-07-29 14:37:42 -0700504 }
Romain Guyac670c02010-07-27 17:39:27 -0700505 // Output transformed position
506 shader.append(gVS_Main_Position);
507 }
508 // End the shader
509 shader.append(gVS_Footer);
510
511 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string());
512
513 return shader;
514}
515
516String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) {
Romain Guya5aed0d2010-09-09 14:42:43 -0700517 String8 shader;
518
Romain Guy707b2f72010-10-11 16:34:59 -0700519 const bool blendFramebuffer = description.framebufferMode >= SkXfermode::kPlus_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700520 if (blendFramebuffer) {
521 shader.append(gFS_Header_Extension_FramebufferFetch);
522 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700523 if (description.hasExternalTexture) {
524 shader.append(gFS_Header_Extension_ExternalTexture);
525 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700526
527 shader.append(gFS_Header);
Romain Guyac670c02010-07-27 17:39:27 -0700528
529 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700530 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700531 shader.append(gVS_Header_Varyings_HasTexture);
532 }
Chet Haase99585ad2011-05-02 15:00:16 -0700533 if (description.isAA) {
534 shader.append(gVS_Header_Varyings_IsAA);
Chet Haase5b0200b2011-04-13 17:58:08 -0700535 }
Romain Guyac670c02010-07-27 17:39:27 -0700536 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700537 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700538 }
539 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700540 shader.append(description.isPoint ?
Romain Guy63553472012-07-18 20:04:14 -0700541 gVS_Header_Varyings_PointHasBitmap :
542 gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700543 }
544
Romain Guyac670c02010-07-27 17:39:27 -0700545 // Uniforms
Romain Guy707b2f72010-10-11 16:34:59 -0700546 int modulateOp = MODULATE_OP_NO_MODULATE;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700547 const bool singleColor = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700548 !description.hasGradient && !description.hasBitmap;
549
550 if (description.modulate || singleColor) {
551 shader.append(gFS_Uniforms_Color);
552 if (!singleColor) modulateOp = MODULATE_OP_MODULATE;
553 }
Romain Guyac670c02010-07-27 17:39:27 -0700554 if (description.hasTexture) {
555 shader.append(gFS_Uniforms_TextureSampler);
Romain Guy8f0095c2011-05-02 17:24:22 -0700556 } else if (description.hasExternalTexture) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700557 shader.append(gFS_Uniforms_ExternalTextureSampler);
558 }
Chet Haase99585ad2011-05-02 15:00:16 -0700559 if (description.isAA) {
560 shader.append(gFS_Uniforms_AA);
Chet Haase5b0200b2011-04-13 17:58:08 -0700561 }
Romain Guyac670c02010-07-27 17:39:27 -0700562 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700563 shader.append(gFS_Uniforms_GradientSampler[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700564 }
Romain Guyed6fcb02011-03-21 13:11:28 -0700565 if (description.hasBitmap && description.isPoint) {
566 shader.append(gFS_Header_Uniforms_PointHasBitmap);
567 }
Romain Guy41210632012-07-16 17:04:24 -0700568 if (description.hasGammaCorrection) {
569 shader.append(gFS_Uniforms_Gamma);
570 }
Romain Guy707b2f72010-10-11 16:34:59 -0700571
572 // Optimization for common cases
Chet Haase99585ad2011-05-02 15:00:16 -0700573 if (!description.isAA && !blendFramebuffer &&
Chet Haase5b0200b2011-04-13 17:58:08 -0700574 description.colorOp == ProgramDescription::kColorNone && !description.isPoint) {
Romain Guy707b2f72010-10-11 16:34:59 -0700575 bool fast = false;
576
577 const bool noShader = !description.hasGradient && !description.hasBitmap;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700578 const bool singleTexture = (description.hasTexture || description.hasExternalTexture) &&
Romain Guy707b2f72010-10-11 16:34:59 -0700579 !description.hasAlpha8Texture && noShader;
580 const bool singleA8Texture = description.hasTexture &&
581 description.hasAlpha8Texture && noShader;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700582 const bool singleGradient = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700583 description.hasGradient && !description.hasBitmap &&
584 description.gradientType == ProgramDescription::kGradientLinear;
585
586 if (singleColor) {
587 shader.append(gFS_Fast_SingleColor);
588 fast = true;
589 } else if (singleTexture) {
590 if (!description.modulate) {
591 shader.append(gFS_Fast_SingleTexture);
592 } else {
593 shader.append(gFS_Fast_SingleModulateTexture);
594 }
595 fast = true;
596 } else if (singleA8Texture) {
597 if (!description.modulate) {
Romain Guy41210632012-07-16 17:04:24 -0700598 if (description.hasGammaCorrection) {
599 shader.append(gFS_Fast_SingleA8Texture_ApplyGamma);
600 } else {
601 shader.append(gFS_Fast_SingleA8Texture);
602 }
Romain Guy707b2f72010-10-11 16:34:59 -0700603 } else {
Romain Guy41210632012-07-16 17:04:24 -0700604 if (description.hasGammaCorrection) {
605 shader.append(gFS_Fast_SingleModulateA8Texture_ApplyGamma);
606 } else {
607 shader.append(gFS_Fast_SingleModulateA8Texture);
608 }
Romain Guy707b2f72010-10-11 16:34:59 -0700609 }
610 fast = true;
611 } else if (singleGradient) {
612 if (!description.modulate) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700613 shader.append(gFS_Fast_SingleGradient[description.isSimpleGradient]);
Romain Guy707b2f72010-10-11 16:34:59 -0700614 } else {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700615 shader.append(gFS_Fast_SingleModulateGradient[description.isSimpleGradient]);
Romain Guy707b2f72010-10-11 16:34:59 -0700616 }
617 fast = true;
618 }
619
620 if (fast) {
Romain Guyc15008e2010-11-10 11:59:15 -0800621#if DEBUG_PROGRAMS
Romain Guy707b2f72010-10-11 16:34:59 -0700622 PROGRAM_LOGD("*** Fast case:\n");
623 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
624 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800625#endif
Romain Guy707b2f72010-10-11 16:34:59 -0700626
627 return shader;
628 }
629 }
630
Romain Guyac670c02010-07-27 17:39:27 -0700631 if (description.hasBitmap) {
632 shader.append(gFS_Uniforms_BitmapSampler);
633 }
634 shader.append(gFS_Uniforms_ColorOp[description.colorOp]);
635
636 // Generate required functions
637 if (description.hasGradient && description.hasBitmap) {
Romain Guy48daa542010-08-10 19:21:34 -0700638 generateBlend(shader, "blendShaders", description.shadersMode);
Romain Guyac670c02010-07-27 17:39:27 -0700639 }
640 if (description.colorOp == ProgramDescription::kColorBlend) {
Romain Guy48daa542010-08-10 19:21:34 -0700641 generateBlend(shader, "blendColors", description.colorMode);
Romain Guyac670c02010-07-27 17:39:27 -0700642 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700643 if (blendFramebuffer) {
644 generateBlend(shader, "blendFramebuffer", description.framebufferMode);
645 }
Romain Guy889f8d12010-07-29 14:37:42 -0700646 if (description.isBitmapNpot) {
647 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
648 }
Romain Guyac670c02010-07-27 17:39:27 -0700649
650 // Begin the shader
651 shader.append(gFS_Main); {
652 // Stores the result in fragColor directly
Romain Guyaa6c24c2011-04-28 18:40:04 -0700653 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700654 if (description.hasAlpha8Texture) {
Romain Guy707b2f72010-10-11 16:34:59 -0700655 if (!description.hasGradient && !description.hasBitmap) {
656 shader.append(gFS_Main_FetchA8Texture[modulateOp]);
657 }
Romain Guyac670c02010-07-27 17:39:27 -0700658 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700659 shader.append(gFS_Main_FetchTexture[modulateOp]);
Romain Guyac670c02010-07-27 17:39:27 -0700660 }
661 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700662 if ((!description.hasGradient && !description.hasBitmap) || description.modulate) {
663 shader.append(gFS_Main_FetchColor);
664 }
Romain Guyac670c02010-07-27 17:39:27 -0700665 }
Chet Haase99585ad2011-05-02 15:00:16 -0700666 if (description.isAA) {
667 shader.append(gFS_Main_AccountForAA);
Chet Haase5b0200b2011-04-13 17:58:08 -0700668 }
Romain Guyac670c02010-07-27 17:39:27 -0700669 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700670 shader.append(gFS_Main_FetchGradient[gradientIndex(description)]);
Romain Guy211efea2012-07-31 21:16:07 -0700671 shader.append(gFS_Main_AddDitherToGradient);
Romain Guyac670c02010-07-27 17:39:27 -0700672 }
673 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700674 if (description.isPoint) {
675 shader.append(gFS_Main_PointBitmapTexCoords);
676 }
Romain Guy889f8d12010-07-29 14:37:42 -0700677 if (!description.isBitmapNpot) {
678 shader.append(gFS_Main_FetchBitmap);
679 } else {
680 shader.append(gFS_Main_FetchBitmapNpot);
681 }
Romain Guyac670c02010-07-27 17:39:27 -0700682 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700683 bool applyModulate = false;
Romain Guyac670c02010-07-27 17:39:27 -0700684 // Case when we have two shaders set
685 if (description.hasGradient && description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700686 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
Romain Guyac670c02010-07-27 17:39:27 -0700687 if (description.isBitmapFirst) {
688 shader.append(gFS_Main_BlendShadersBG);
689 } else {
690 shader.append(gFS_Main_BlendShadersGB);
691 }
Romain Guy707b2f72010-10-11 16:34:59 -0700692 shader.append(gFS_Main_BlendShaders_Modulate[op]);
Romain Guy740bf2b2011-04-26 15:33:10 -0700693 applyModulate = true;
Romain Guy889f8d12010-07-29 14:37:42 -0700694 } else {
695 if (description.hasGradient) {
Romain Guy707b2f72010-10-11 16:34:59 -0700696 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
697 shader.append(gFS_Main_GradientShader_Modulate[op]);
Romain Guy740bf2b2011-04-26 15:33:10 -0700698 applyModulate = true;
Romain Guy889f8d12010-07-29 14:37:42 -0700699 } else if (description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700700 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
701 shader.append(gFS_Main_BitmapShader_Modulate[op]);
Romain Guy740bf2b2011-04-26 15:33:10 -0700702 applyModulate = true;
Romain Guy889f8d12010-07-29 14:37:42 -0700703 }
Romain Guyac670c02010-07-27 17:39:27 -0700704 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700705 if (description.modulate && applyModulate) {
Romain Guy41210632012-07-16 17:04:24 -0700706 if (description.hasGammaCorrection) {
707 shader.append(gFS_Main_ModulateColor_ApplyGamma);
708 } else {
709 shader.append(gFS_Main_ModulateColor);
710 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700711 }
Romain Guyac670c02010-07-27 17:39:27 -0700712 // Apply the color op if needed
713 shader.append(gFS_Main_ApplyColorOp[description.colorOp]);
714 // Output the fragment
Romain Guya5aed0d2010-09-09 14:42:43 -0700715 if (!blendFramebuffer) {
716 shader.append(gFS_Main_FragColor);
717 } else {
Romain Guyf607bdc2010-09-10 19:20:06 -0700718 shader.append(!description.swapSrcDst ?
719 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap);
Romain Guya5aed0d2010-09-09 14:42:43 -0700720 }
Romain Guyac670c02010-07-27 17:39:27 -0700721 }
722 // End the shader
723 shader.append(gFS_Footer);
724
Romain Guyc15008e2010-11-10 11:59:15 -0800725#if DEBUG_PROGRAMS
Romain Guydb1938e2010-08-02 18:50:22 -0700726 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
727 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800728#endif
Romain Guydb1938e2010-08-02 18:50:22 -0700729
Romain Guyac670c02010-07-27 17:39:27 -0700730 return shader;
731}
732
Romain Guy48daa542010-08-10 19:21:34 -0700733void ProgramCache::generateBlend(String8& shader, const char* name, SkXfermode::Mode mode) {
Romain Guyac670c02010-07-27 17:39:27 -0700734 shader.append("\nvec4 ");
735 shader.append(name);
736 shader.append("(vec4 src, vec4 dst) {\n");
737 shader.append(" ");
Romain Guy48daa542010-08-10 19:21:34 -0700738 shader.append(gBlendOps[mode]);
Romain Guyac670c02010-07-27 17:39:27 -0700739 shader.append("}\n");
740}
741
Romain Guy889f8d12010-07-29 14:37:42 -0700742void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) {
Romain Guy63553472012-07-18 20:04:14 -0700743 shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700744 if (wrapS == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700745 shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700746 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n");
747 }
748 if (wrapT == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700749 shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700750 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n");
751 }
752 shader.append(" return vec2(");
753 switch (wrapS) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700754 case GL_CLAMP_TO_EDGE:
755 shader.append("texCoords.x");
756 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700757 case GL_REPEAT:
758 shader.append("mod(texCoords.x, 1.0)");
759 break;
760 case GL_MIRRORED_REPEAT:
761 shader.append("xMod2");
762 break;
763 }
764 shader.append(", ");
765 switch (wrapT) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700766 case GL_CLAMP_TO_EDGE:
767 shader.append("texCoords.y");
768 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700769 case GL_REPEAT:
770 shader.append("mod(texCoords.y, 1.0)");
771 break;
772 case GL_MIRRORED_REPEAT:
773 shader.append("yMod2");
774 break;
775 }
776 shader.append(");\n");
777 shader.append("}\n");
778}
779
Romain Guydb1938e2010-08-02 18:50:22 -0700780void ProgramCache::printLongString(const String8& shader) const {
781 ssize_t index = 0;
782 ssize_t lastIndex = 0;
783 const char* str = shader.string();
784 while ((index = shader.find("\n", index)) > -1) {
785 String8 line(str, index - lastIndex);
786 if (line.length() == 0) line.append("\n");
787 PROGRAM_LOGD("%s", line.string());
788 index++;
789 str += (index - lastIndex);
790 lastIndex = index;
791 }
792}
793
Romain Guyac670c02010-07-27 17:39:27 -0700794}; // namespace uirenderer
795}; // namespace android