blob: be8f80af55c3551a0d24dac83d23b13f78b9078b [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";
Chris Craik6ebdc112012-08-31 18:24:33 -070046const char* gVS_Header_Attributes_AARectParameters =
47 "attribute float vtxAlpha;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -070048const char* gVS_Header_Uniforms_TextureTransform =
49 "uniform mat4 mainTextureTransform;\n";
Romain Guyac670c02010-07-27 17:39:27 -070050const char* gVS_Header_Uniforms =
51 "uniform mat4 transform;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -070052const char* gVS_Header_Uniforms_IsPoint =
Romain Guy80bbfb12011-03-23 16:56:28 -070053 "uniform mediump float pointSize;\n";
Romain Guyee916f12010-09-20 17:53:08 -070054const char* gVS_Header_Uniforms_HasGradient[3] = {
55 // Linear
Romain Guyee916f12010-09-20 17:53:08 -070056 "uniform mat4 screenSpace;\n",
57 // Circular
Romain Guyddb80be2010-09-20 19:04:33 -070058 "uniform mat4 screenSpace;\n",
Romain Guyee916f12010-09-20 17:53:08 -070059 // Sweep
Romain Guyee916f12010-09-20 17:53:08 -070060 "uniform mat4 screenSpace;\n"
61};
Romain Guy889f8d12010-07-29 14:37:42 -070062const char* gVS_Header_Uniforms_HasBitmap =
63 "uniform mat4 textureTransform;\n"
Romain Guy80bbfb12011-03-23 16:56:28 -070064 "uniform mediump vec2 textureDimension;\n";
Romain Guyac670c02010-07-27 17:39:27 -070065const char* gVS_Header_Varyings_HasTexture =
66 "varying vec2 outTexCoords;\n";
Chet Haase99585ad2011-05-02 15:00:16 -070067const char* gVS_Header_Varyings_IsAA =
68 "varying float widthProportion;\n"
69 "varying float lengthProportion;\n";
Chris Craik6ebdc112012-08-31 18:24:33 -070070const char* gVS_Header_Varyings_IsAARect =
71 "varying float alpha;\n";
Romain Guy63553472012-07-18 20:04:14 -070072const char* gVS_Header_Varyings_HasBitmap =
73 "varying highp vec2 outBitmapTexCoords;\n";
74const char* gVS_Header_Varyings_PointHasBitmap =
75 "varying highp vec2 outPointBitmapTexCoords;\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -070076const char* gVS_Header_Varyings_HasGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -070077 // Linear
Romain Guy63553472012-07-18 20:04:14 -070078 "varying highp vec2 linear;\n",
Romain Guy320d46b2012-08-08 16:05:42 -070079 "varying float linear;\n",
Romain Guy211efea2012-07-31 21:16:07 -070080
Romain Guyee916f12010-09-20 17:53:08 -070081 // Circular
Romain Guy63553472012-07-18 20:04:14 -070082 "varying highp vec2 circular;\n",
Romain Guy42e1e0d2012-07-30 14:47:51 -070083 "varying highp vec2 circular;\n",
Romain Guy211efea2012-07-31 21:16:07 -070084
Romain Guyee916f12010-09-20 17:53:08 -070085 // Sweep
Romain Guy42e1e0d2012-07-30 14:47:51 -070086 "varying highp vec2 sweep;\n",
87 "varying highp vec2 sweep;\n",
Romain Guyee916f12010-09-20 17:53:08 -070088};
Romain Guyac670c02010-07-27 17:39:27 -070089const char* gVS_Main =
90 "\nvoid main(void) {\n";
91const char* gVS_Main_OutTexCoords =
92 " outTexCoords = texCoords;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -070093const char* gVS_Main_OutTransformedTexCoords =
94 " outTexCoords = (mainTextureTransform * vec4(texCoords, 0.0, 1.0)).xy;\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -070095const char* gVS_Main_OutGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -070096 // Linear
Romain Guy7537f852010-10-11 14:38:28 -070097 " linear = vec2((screenSpace * position).x, 0.5);\n",
Romain Guy42e1e0d2012-07-30 14:47:51 -070098 " linear = (screenSpace * position).x;\n",
Romain Guy211efea2012-07-31 21:16:07 -070099
Romain Guyee916f12010-09-20 17:53:08 -0700100 // Circular
Romain Guy14830942010-10-07 15:07:45 -0700101 " circular = (screenSpace * position).xy;\n",
Romain Guy42e1e0d2012-07-30 14:47:51 -0700102 " circular = (screenSpace * position).xy;\n",
Romain Guy211efea2012-07-31 21:16:07 -0700103
Romain Guyee916f12010-09-20 17:53:08 -0700104 // Sweep
Romain Guy42e1e0d2012-07-30 14:47:51 -0700105 " sweep = (screenSpace * position).xy;\n",
106 " sweep = (screenSpace * position).xy;\n",
Romain Guyee916f12010-09-20 17:53:08 -0700107};
Romain Guy889f8d12010-07-29 14:37:42 -0700108const char* gVS_Main_OutBitmapTexCoords =
Romain Guy707b2f72010-10-11 16:34:59 -0700109 " outBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -0700110const char* gVS_Main_OutPointBitmapTexCoords =
111 " outPointBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700112const char* gVS_Main_Position =
113 " gl_Position = transform * position;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -0700114const char* gVS_Main_PointSize =
115 " gl_PointSize = pointSize;\n";
Chet Haase99585ad2011-05-02 15:00:16 -0700116const char* gVS_Main_AA =
117 " widthProportion = vtxWidth;\n"
118 " lengthProportion = vtxLength;\n";
Chris Craik6ebdc112012-08-31 18:24:33 -0700119const char* gVS_Main_AARect =
120 " alpha = vtxAlpha;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700121const char* gVS_Footer =
122 "}\n\n";
123
124///////////////////////////////////////////////////////////////////////////////
125// Fragment shaders snippets
126///////////////////////////////////////////////////////////////////////////////
127
Romain Guya5aed0d2010-09-09 14:42:43 -0700128const char* gFS_Header_Extension_FramebufferFetch =
129 "#extension GL_NV_shader_framebuffer_fetch : enable\n\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700130const char* gFS_Header_Extension_ExternalTexture =
131 "#extension GL_OES_EGL_image_external : require\n\n";
Romain Guyac670c02010-07-27 17:39:27 -0700132const char* gFS_Header =
133 "precision mediump float;\n\n";
134const char* gFS_Uniforms_Color =
135 "uniform vec4 color;\n";
Chet Haase99585ad2011-05-02 15:00:16 -0700136const char* gFS_Uniforms_AA =
Chet Haase5b0200b2011-04-13 17:58:08 -0700137 "uniform float boundaryWidth;\n"
Chris Craika798b952012-08-27 17:03:13 -0700138 "uniform float boundaryLength;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -0700139const char* gFS_Header_Uniforms_PointHasBitmap =
140 "uniform vec2 textureDimension;\n"
141 "uniform float pointSize;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700142const char* gFS_Uniforms_TextureSampler =
143 "uniform sampler2D sampler;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700144const char* gFS_Uniforms_ExternalTextureSampler =
145 "uniform samplerExternalOES sampler;\n";
Romain Guy211efea2012-07-31 21:16:07 -0700146#define FS_UNIFORMS_DITHER \
147 "uniform float ditherSize;\n" \
148 "uniform sampler2D ditherSampler;\n"
149#define FS_UNIFORMS_GRADIENT \
150 "uniform vec4 startColor;\n" \
151 "uniform vec4 endColor;\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700152const char* gFS_Uniforms_GradientSampler[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700153 // Linear
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 // Circular
Romain Guy211efea2012-07-31 21:16:07 -0700158 FS_UNIFORMS_DITHER "uniform sampler2D gradientSampler;\n",
159 FS_UNIFORMS_DITHER FS_UNIFORMS_GRADIENT,
160
Romain Guyee916f12010-09-20 17:53:08 -0700161 // Sweep
Romain Guy211efea2012-07-31 21:16:07 -0700162 FS_UNIFORMS_DITHER "uniform sampler2D gradientSampler;\n",
163 FS_UNIFORMS_DITHER FS_UNIFORMS_GRADIENT
Romain Guyee916f12010-09-20 17:53:08 -0700164};
Romain Guyac670c02010-07-27 17:39:27 -0700165const char* gFS_Uniforms_BitmapSampler =
166 "uniform sampler2D bitmapSampler;\n";
167const char* gFS_Uniforms_ColorOp[4] = {
168 // None
169 "",
170 // Matrix
171 "uniform mat4 colorMatrix;\n"
172 "uniform vec4 colorMatrixVector;\n",
173 // Lighting
Romain Guydb1938e2010-08-02 18:50:22 -0700174 "uniform vec4 lightingMul;\n"
175 "uniform vec4 lightingAdd;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700176 // PorterDuff
Romain Guydb1938e2010-08-02 18:50:22 -0700177 "uniform vec4 colorBlend;\n"
Romain Guyac670c02010-07-27 17:39:27 -0700178};
Romain Guy41210632012-07-16 17:04:24 -0700179const char* gFS_Uniforms_Gamma =
180 "uniform float gamma;\n";
181
Romain Guyac670c02010-07-27 17:39:27 -0700182const char* gFS_Main =
183 "\nvoid main(void) {\n"
Romain Guy7fbcc042010-08-04 15:40:07 -0700184 " lowp vec4 fragColor;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700185
Romain Guyed6fcb02011-03-21 13:11:28 -0700186const char* gFS_Main_PointBitmapTexCoords =
Romain Guy63553472012-07-18 20:04:14 -0700187 " highp vec2 outBitmapTexCoords = outPointBitmapTexCoords + "
Romain Guyed6fcb02011-03-21 13:11:28 -0700188 "((gl_PointCoord - vec2(0.5, 0.5)) * textureDimension * vec2(pointSize, pointSize));\n";
189
Romain Guy211efea2012-07-31 21:16:07 -0700190#define FS_MAIN_DITHER \
191 "texture2D(ditherSampler, gl_FragCoord.xy * ditherSize).a * ditherSize * ditherSize"
192const char* gFS_Main_AddDitherToGradient =
193 " gradientColor += " FS_MAIN_DITHER ";\n";
194
Romain Guy707b2f72010-10-11 16:34:59 -0700195// Fast cases
196const char* gFS_Fast_SingleColor =
197 "\nvoid main(void) {\n"
198 " gl_FragColor = color;\n"
199 "}\n\n";
200const char* gFS_Fast_SingleTexture =
201 "\nvoid main(void) {\n"
202 " gl_FragColor = texture2D(sampler, outTexCoords);\n"
203 "}\n\n";
204const char* gFS_Fast_SingleModulateTexture =
205 "\nvoid main(void) {\n"
206 " gl_FragColor = color.a * texture2D(sampler, outTexCoords);\n"
207 "}\n\n";
208const char* gFS_Fast_SingleA8Texture =
209 "\nvoid main(void) {\n"
Romain Guy9db91242010-10-12 13:13:09 -0700210 " gl_FragColor = texture2D(sampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700211 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700212const char* gFS_Fast_SingleA8Texture_ApplyGamma =
213 "\nvoid main(void) {\n"
214 " gl_FragColor = vec4(0.0, 0.0, 0.0, pow(texture2D(sampler, outTexCoords).a, gamma));\n"
215 "}\n\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700216const char* gFS_Fast_SingleModulateA8Texture =
217 "\nvoid main(void) {\n"
218 " gl_FragColor = color * texture2D(sampler, outTexCoords).a;\n"
219 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700220const char* gFS_Fast_SingleModulateA8Texture_ApplyGamma =
221 "\nvoid main(void) {\n"
222 " gl_FragColor = color * pow(texture2D(sampler, outTexCoords).a, gamma);\n"
223 "}\n\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -0700224const char* gFS_Fast_SingleGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700225 "\nvoid main(void) {\n"
Romain Guy211efea2012-07-31 21:16:07 -0700226 " gl_FragColor = " FS_MAIN_DITHER " + texture2D(gradientSampler, linear);\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700227 "}\n\n",
228 "\nvoid main(void) {\n"
Romain Guy211efea2012-07-31 21:16:07 -0700229 " gl_FragColor = " FS_MAIN_DITHER " + mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700230 "}\n\n"
231};
232const char* gFS_Fast_SingleModulateGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700233 "\nvoid main(void) {\n"
Romain Guy211efea2012-07-31 21:16:07 -0700234 " gl_FragColor " FS_MAIN_DITHER " + color.a * texture2D(gradientSampler, linear);\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700235 "}\n\n",
236 "\nvoid main(void) {\n"
Romain Guy211efea2012-07-31 21:16:07 -0700237 " gl_FragColor " FS_MAIN_DITHER " + color.a * mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700238 "}\n\n"
239};
Romain Guy707b2f72010-10-11 16:34:59 -0700240
241// General case
Romain Guyac670c02010-07-27 17:39:27 -0700242const char* gFS_Main_FetchColor =
243 " fragColor = color;\n";
Romain Guy740bf2b2011-04-26 15:33:10 -0700244const char* gFS_Main_ModulateColor =
245 " fragColor *= color.a;\n";
Romain Guy41210632012-07-16 17:04:24 -0700246const char* gFS_Main_ModulateColor_ApplyGamma =
247 " fragColor *= pow(color.a, gamma);\n";
Chet Haase99585ad2011-05-02 15:00:16 -0700248const char* gFS_Main_AccountForAA =
Chris Craika798b952012-08-27 17:03:13 -0700249 " fragColor *= (1.0 - smoothstep(boundaryWidth, 0.5, abs(0.5 - widthProportion)))\n"
250 " * (1.0 - smoothstep(boundaryLength, 0.5, abs(0.5 - lengthProportion)));\n";
Chris Craik6ebdc112012-08-31 18:24:33 -0700251const char* gFS_Main_AccountForAARect =
252 " fragColor *= alpha;\n";
Chris Craika798b952012-08-27 17:03:13 -0700253
Romain Guy707b2f72010-10-11 16:34:59 -0700254const char* gFS_Main_FetchTexture[2] = {
255 // Don't modulate
256 " fragColor = texture2D(sampler, outTexCoords);\n",
257 // Modulate
258 " fragColor = color * texture2D(sampler, outTexCoords);\n"
259};
260const char* gFS_Main_FetchA8Texture[2] = {
261 // Don't modulate
Romain Guy9db91242010-10-12 13:13:09 -0700262 " fragColor = texture2D(sampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700263 // Modulate
264 " fragColor = color * texture2D(sampler, outTexCoords).a;\n"
265};
Romain Guy42e1e0d2012-07-30 14:47:51 -0700266const char* gFS_Main_FetchGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700267 // Linear
Romain Guy320d46b2012-08-08 16:05:42 -0700268 " vec4 gradientColor = texture2D(gradientSampler, linear);\n",
Romain Guy211efea2012-07-31 21:16:07 -0700269
Romain Guy320d46b2012-08-08 16:05:42 -0700270 " vec4 gradientColor = mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700271
Romain Guyee916f12010-09-20 17:53:08 -0700272 // Circular
Romain Guy320d46b2012-08-08 16:05:42 -0700273 " vec4 gradientColor = texture2D(gradientSampler, vec2(length(circular), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700274
Romain Guy320d46b2012-08-08 16:05:42 -0700275 " vec4 gradientColor = mix(startColor, endColor, clamp(length(circular), 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700276
Romain Guyee916f12010-09-20 17:53:08 -0700277 // Sweep
Romain Guy63553472012-07-18 20:04:14 -0700278 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700279 " vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700280
Romain Guy42e1e0d2012-07-30 14:47:51 -0700281 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700282 " vec4 gradientColor = mix(startColor, endColor, clamp(index - floor(index), 0.0, 1.0));\n"
Romain Guyee916f12010-09-20 17:53:08 -0700283};
Romain Guyac670c02010-07-27 17:39:27 -0700284const char* gFS_Main_FetchBitmap =
285 " vec4 bitmapColor = texture2D(bitmapSampler, outBitmapTexCoords);\n";
Romain Guy889f8d12010-07-29 14:37:42 -0700286const char* gFS_Main_FetchBitmapNpot =
287 " vec4 bitmapColor = texture2D(bitmapSampler, wrap(outBitmapTexCoords));\n";
Romain Guyac670c02010-07-27 17:39:27 -0700288const char* gFS_Main_BlendShadersBG =
Romain Guyac670c02010-07-27 17:39:27 -0700289 " fragColor = blendShaders(gradientColor, bitmapColor)";
Romain Guy06f96e22010-07-30 19:18:16 -0700290const char* gFS_Main_BlendShadersGB =
291 " fragColor = blendShaders(bitmapColor, gradientColor)";
Romain Guy707b2f72010-10-11 16:34:59 -0700292const char* gFS_Main_BlendShaders_Modulate[3] = {
293 // Don't modulate
294 ";\n",
295 // Modulate
296 " * fragColor.a;\n",
297 // Modulate with alpha 8 texture
298 " * texture2D(sampler, outTexCoords).a;\n"
299};
300const char* gFS_Main_GradientShader_Modulate[3] = {
301 // Don't modulate
302 " fragColor = gradientColor;\n",
303 // Modulate
304 " fragColor = gradientColor * fragColor.a;\n",
305 // Modulate with alpha 8 texture
306 " fragColor = gradientColor * texture2D(sampler, outTexCoords).a;\n"
307 };
308const char* gFS_Main_BitmapShader_Modulate[3] = {
309 // Don't modulate
310 " fragColor = bitmapColor;\n",
311 // Modulate
312 " fragColor = bitmapColor * fragColor.a;\n",
313 // Modulate with alpha 8 texture
314 " fragColor = bitmapColor * texture2D(sampler, outTexCoords).a;\n"
315 };
Romain Guyac670c02010-07-27 17:39:27 -0700316const char* gFS_Main_FragColor =
317 " gl_FragColor = fragColor;\n";
Romain Guya5aed0d2010-09-09 14:42:43 -0700318const char* gFS_Main_FragColor_Blend =
319 " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n";
Romain Guyf607bdc2010-09-10 19:20:06 -0700320const char* gFS_Main_FragColor_Blend_Swap =
321 " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n";
Romain Guyac670c02010-07-27 17:39:27 -0700322const char* gFS_Main_ApplyColorOp[4] = {
323 // None
324 "",
325 // Matrix
Romain Guydb1938e2010-08-02 18:50:22 -0700326 // TODO: Fix premultiplied alpha computations for color matrix
Romain Guyac670c02010-07-27 17:39:27 -0700327 " fragColor *= colorMatrix;\n"
Romain Guydb1938e2010-08-02 18:50:22 -0700328 " fragColor += colorMatrixVector;\n"
329 " fragColor.rgb *= fragColor.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700330 // Lighting
Romain Guydb1938e2010-08-02 18:50:22 -0700331 " float lightingAlpha = fragColor.a;\n"
332 " fragColor = min(fragColor * lightingMul + (lightingAdd * lightingAlpha), lightingAlpha);\n"
333 " fragColor.a = lightingAlpha;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700334 // PorterDuff
335 " fragColor = blendColors(colorBlend, fragColor);\n"
336};
337const char* gFS_Footer =
338 "}\n\n";
339
340///////////////////////////////////////////////////////////////////////////////
341// PorterDuff snippets
342///////////////////////////////////////////////////////////////////////////////
343
Romain Guy48daa542010-08-10 19:21:34 -0700344const char* gBlendOps[18] = {
Romain Guyac670c02010-07-27 17:39:27 -0700345 // Clear
346 "return vec4(0.0, 0.0, 0.0, 0.0);\n",
347 // Src
348 "return src;\n",
349 // Dst
350 "return dst;\n",
351 // SrcOver
Romain Guy06f96e22010-07-30 19:18:16 -0700352 "return src + dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700353 // DstOver
Romain Guy06f96e22010-07-30 19:18:16 -0700354 "return dst + src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700355 // SrcIn
Romain Guy06f96e22010-07-30 19:18:16 -0700356 "return src * dst.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700357 // DstIn
Romain Guy06f96e22010-07-30 19:18:16 -0700358 "return dst * src.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700359 // SrcOut
Romain Guy06f96e22010-07-30 19:18:16 -0700360 "return src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700361 // DstOut
Romain Guy06f96e22010-07-30 19:18:16 -0700362 "return dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700363 // SrcAtop
364 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n",
365 // DstAtop
366 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n",
367 // Xor
Romain Guy48daa542010-08-10 19:21:34 -0700368 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, "
Romain Guyac670c02010-07-27 17:39:27 -0700369 "src.a + dst.a - 2.0 * src.a * dst.a);\n",
Romain Guy48daa542010-08-10 19:21:34 -0700370 // Add
371 "return min(src + dst, 1.0);\n",
372 // Multiply
373 "return src * dst;\n",
374 // Screen
375 "return src + dst - src * dst;\n",
376 // Overlay
377 "return clamp(vec4(mix("
378 "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), "
379 "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), "
380 "step(dst.a, 2.0 * dst.rgb)), "
381 "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n",
382 // Darken
383 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
384 "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
385 // Lighten
386 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
387 "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 -0700388};
389
390///////////////////////////////////////////////////////////////////////////////
391// Constructors/destructors
392///////////////////////////////////////////////////////////////////////////////
393
394ProgramCache::ProgramCache() {
395}
396
397ProgramCache::~ProgramCache() {
398 clear();
399}
400
401///////////////////////////////////////////////////////////////////////////////
402// Cache management
403///////////////////////////////////////////////////////////////////////////////
404
405void ProgramCache::clear() {
Romain Guy67f27952010-12-07 20:09:23 -0800406 PROGRAM_LOGD("Clearing program cache");
407
Romain Guyac670c02010-07-27 17:39:27 -0700408 size_t count = mCache.size();
409 for (size_t i = 0; i < count; i++) {
410 delete mCache.valueAt(i);
411 }
412 mCache.clear();
413}
414
415Program* ProgramCache::get(const ProgramDescription& description) {
416 programid key = description.key();
417 ssize_t index = mCache.indexOfKey(key);
418 Program* program = NULL;
419 if (index < 0) {
Romain Guyee916f12010-09-20 17:53:08 -0700420 description.log("Could not find program");
Romain Guyac670c02010-07-27 17:39:27 -0700421 program = generateProgram(description, key);
422 mCache.add(key, program);
423 } else {
424 program = mCache.valueAt(index);
425 }
426 return program;
427}
428
429///////////////////////////////////////////////////////////////////////////////
430// Program generation
431///////////////////////////////////////////////////////////////////////////////
432
433Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) {
434 String8 vertexShader = generateVertexShader(description);
435 String8 fragmentShader = generateFragmentShader(description);
436
Romain Guy42e1e0d2012-07-30 14:47:51 -0700437 return new Program(description, vertexShader.string(), fragmentShader.string());
438}
439
440static inline size_t gradientIndex(const ProgramDescription& description) {
441 return description.gradientType * 2 + description.isSimpleGradient;
Romain Guyac670c02010-07-27 17:39:27 -0700442}
443
444String8 ProgramCache::generateVertexShader(const ProgramDescription& description) {
445 // Add attributes
446 String8 shader(gVS_Header_Attributes);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700447 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700448 shader.append(gVS_Header_Attributes_TexCoords);
449 }
Chris Craik6ebdc112012-08-31 18:24:33 -0700450 if (description.isAARect) {
451 shader.append(gVS_Header_Attributes_AARectParameters);
452 } else if (description.isAA) {
Chet Haase99585ad2011-05-02 15:00:16 -0700453 shader.append(gVS_Header_Attributes_AAParameters);
Chet Haase5b0200b2011-04-13 17:58:08 -0700454 }
Romain Guyac670c02010-07-27 17:39:27 -0700455 // Uniforms
456 shader.append(gVS_Header_Uniforms);
Romain Guy8f0095c2011-05-02 17:24:22 -0700457 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700458 shader.append(gVS_Header_Uniforms_TextureTransform);
459 }
Romain Guyac670c02010-07-27 17:39:27 -0700460 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700461 shader.append(gVS_Header_Uniforms_HasGradient[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700462 }
Romain Guy889f8d12010-07-29 14:37:42 -0700463 if (description.hasBitmap) {
464 shader.append(gVS_Header_Uniforms_HasBitmap);
465 }
Romain Guyed6fcb02011-03-21 13:11:28 -0700466 if (description.isPoint) {
467 shader.append(gVS_Header_Uniforms_IsPoint);
468 }
Romain Guyac670c02010-07-27 17:39:27 -0700469 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700470 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700471 shader.append(gVS_Header_Varyings_HasTexture);
472 }
Chris Craik6ebdc112012-08-31 18:24:33 -0700473 if (description.isAARect) {
474 shader.append(gVS_Header_Varyings_IsAARect);
475 } else if (description.isAA) {
Chet Haase99585ad2011-05-02 15:00:16 -0700476 shader.append(gVS_Header_Varyings_IsAA);
Chet Haase5b0200b2011-04-13 17:58:08 -0700477 }
Romain Guyac670c02010-07-27 17:39:27 -0700478 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700479 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700480 }
481 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700482 shader.append(description.isPoint ?
Romain Guy63553472012-07-18 20:04:14 -0700483 gVS_Header_Varyings_PointHasBitmap :
484 gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700485 }
486
487 // Begin the shader
488 shader.append(gVS_Main); {
Romain Guy8f0095c2011-05-02 17:24:22 -0700489 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700490 shader.append(gVS_Main_OutTransformedTexCoords);
Romain Guy8f0095c2011-05-02 17:24:22 -0700491 } else if (description.hasTexture || description.hasExternalTexture) {
492 shader.append(gVS_Main_OutTexCoords);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700493 }
Chris Craik6ebdc112012-08-31 18:24:33 -0700494 if (description.isAARect) {
495 shader.append(gVS_Main_AARect);
496 } else if (description.isAA) {
Chet Haase99585ad2011-05-02 15:00:16 -0700497 shader.append(gVS_Main_AA);
Chet Haase5b0200b2011-04-13 17:58:08 -0700498 }
Romain Guyac670c02010-07-27 17:39:27 -0700499 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700500 shader.append(gVS_Main_OutGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700501 }
Romain Guy889f8d12010-07-29 14:37:42 -0700502 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700503 shader.append(description.isPoint ?
504 gVS_Main_OutPointBitmapTexCoords :
505 gVS_Main_OutBitmapTexCoords);
506 }
507 if (description.isPoint) {
508 shader.append(gVS_Main_PointSize);
Romain Guy889f8d12010-07-29 14:37:42 -0700509 }
Romain Guyac670c02010-07-27 17:39:27 -0700510 // Output transformed position
511 shader.append(gVS_Main_Position);
512 }
513 // End the shader
514 shader.append(gVS_Footer);
515
516 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string());
517
518 return shader;
519}
520
521String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) {
Romain Guya5aed0d2010-09-09 14:42:43 -0700522 String8 shader;
523
Romain Guy707b2f72010-10-11 16:34:59 -0700524 const bool blendFramebuffer = description.framebufferMode >= SkXfermode::kPlus_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700525 if (blendFramebuffer) {
526 shader.append(gFS_Header_Extension_FramebufferFetch);
527 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700528 if (description.hasExternalTexture) {
529 shader.append(gFS_Header_Extension_ExternalTexture);
530 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700531
532 shader.append(gFS_Header);
Romain Guyac670c02010-07-27 17:39:27 -0700533
534 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700535 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700536 shader.append(gVS_Header_Varyings_HasTexture);
537 }
Chris Craik6ebdc112012-08-31 18:24:33 -0700538 if (description.isAARect) {
539 shader.append(gVS_Header_Varyings_IsAARect);
540 } else if (description.isAA) {
Chet Haase99585ad2011-05-02 15:00:16 -0700541 shader.append(gVS_Header_Varyings_IsAA);
Chet Haase5b0200b2011-04-13 17:58:08 -0700542 }
Romain Guyac670c02010-07-27 17:39:27 -0700543 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700544 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700545 }
546 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700547 shader.append(description.isPoint ?
Romain Guy63553472012-07-18 20:04:14 -0700548 gVS_Header_Varyings_PointHasBitmap :
549 gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700550 }
551
Romain Guyac670c02010-07-27 17:39:27 -0700552 // Uniforms
Romain Guy707b2f72010-10-11 16:34:59 -0700553 int modulateOp = MODULATE_OP_NO_MODULATE;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700554 const bool singleColor = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700555 !description.hasGradient && !description.hasBitmap;
556
557 if (description.modulate || singleColor) {
558 shader.append(gFS_Uniforms_Color);
559 if (!singleColor) modulateOp = MODULATE_OP_MODULATE;
560 }
Romain Guyac670c02010-07-27 17:39:27 -0700561 if (description.hasTexture) {
562 shader.append(gFS_Uniforms_TextureSampler);
Romain Guy8f0095c2011-05-02 17:24:22 -0700563 } else if (description.hasExternalTexture) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700564 shader.append(gFS_Uniforms_ExternalTextureSampler);
565 }
Chet Haase99585ad2011-05-02 15:00:16 -0700566 if (description.isAA) {
567 shader.append(gFS_Uniforms_AA);
Chet Haase5b0200b2011-04-13 17:58:08 -0700568 }
Romain Guyac670c02010-07-27 17:39:27 -0700569 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700570 shader.append(gFS_Uniforms_GradientSampler[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700571 }
Romain Guyed6fcb02011-03-21 13:11:28 -0700572 if (description.hasBitmap && description.isPoint) {
573 shader.append(gFS_Header_Uniforms_PointHasBitmap);
574 }
Romain Guy41210632012-07-16 17:04:24 -0700575 if (description.hasGammaCorrection) {
576 shader.append(gFS_Uniforms_Gamma);
577 }
Romain Guy707b2f72010-10-11 16:34:59 -0700578
579 // Optimization for common cases
Chet Haase99585ad2011-05-02 15:00:16 -0700580 if (!description.isAA && !blendFramebuffer &&
Chris Craik6ebdc112012-08-31 18:24:33 -0700581 description.colorOp == ProgramDescription::kColorNone &&
582 !description.isPoint && !description.isAARect) {
Romain Guy707b2f72010-10-11 16:34:59 -0700583 bool fast = false;
584
585 const bool noShader = !description.hasGradient && !description.hasBitmap;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700586 const bool singleTexture = (description.hasTexture || description.hasExternalTexture) &&
Romain Guy707b2f72010-10-11 16:34:59 -0700587 !description.hasAlpha8Texture && noShader;
588 const bool singleA8Texture = description.hasTexture &&
589 description.hasAlpha8Texture && noShader;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700590 const bool singleGradient = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700591 description.hasGradient && !description.hasBitmap &&
592 description.gradientType == ProgramDescription::kGradientLinear;
593
594 if (singleColor) {
595 shader.append(gFS_Fast_SingleColor);
596 fast = true;
597 } else if (singleTexture) {
598 if (!description.modulate) {
599 shader.append(gFS_Fast_SingleTexture);
600 } else {
601 shader.append(gFS_Fast_SingleModulateTexture);
602 }
603 fast = true;
604 } else if (singleA8Texture) {
605 if (!description.modulate) {
Romain Guy41210632012-07-16 17:04:24 -0700606 if (description.hasGammaCorrection) {
607 shader.append(gFS_Fast_SingleA8Texture_ApplyGamma);
608 } else {
609 shader.append(gFS_Fast_SingleA8Texture);
610 }
Romain Guy707b2f72010-10-11 16:34:59 -0700611 } else {
Romain Guy41210632012-07-16 17:04:24 -0700612 if (description.hasGammaCorrection) {
613 shader.append(gFS_Fast_SingleModulateA8Texture_ApplyGamma);
614 } else {
615 shader.append(gFS_Fast_SingleModulateA8Texture);
616 }
Romain Guy707b2f72010-10-11 16:34:59 -0700617 }
618 fast = true;
619 } else if (singleGradient) {
620 if (!description.modulate) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700621 shader.append(gFS_Fast_SingleGradient[description.isSimpleGradient]);
Romain Guy707b2f72010-10-11 16:34:59 -0700622 } else {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700623 shader.append(gFS_Fast_SingleModulateGradient[description.isSimpleGradient]);
Romain Guy707b2f72010-10-11 16:34:59 -0700624 }
625 fast = true;
626 }
627
628 if (fast) {
Romain Guyc15008e2010-11-10 11:59:15 -0800629#if DEBUG_PROGRAMS
Romain Guy707b2f72010-10-11 16:34:59 -0700630 PROGRAM_LOGD("*** Fast case:\n");
631 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
632 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800633#endif
Romain Guy707b2f72010-10-11 16:34:59 -0700634
635 return shader;
636 }
637 }
638
Romain Guyac670c02010-07-27 17:39:27 -0700639 if (description.hasBitmap) {
640 shader.append(gFS_Uniforms_BitmapSampler);
641 }
642 shader.append(gFS_Uniforms_ColorOp[description.colorOp]);
643
644 // Generate required functions
645 if (description.hasGradient && description.hasBitmap) {
Romain Guy48daa542010-08-10 19:21:34 -0700646 generateBlend(shader, "blendShaders", description.shadersMode);
Romain Guyac670c02010-07-27 17:39:27 -0700647 }
648 if (description.colorOp == ProgramDescription::kColorBlend) {
Romain Guy48daa542010-08-10 19:21:34 -0700649 generateBlend(shader, "blendColors", description.colorMode);
Romain Guyac670c02010-07-27 17:39:27 -0700650 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700651 if (blendFramebuffer) {
652 generateBlend(shader, "blendFramebuffer", description.framebufferMode);
653 }
Romain Guy889f8d12010-07-29 14:37:42 -0700654 if (description.isBitmapNpot) {
655 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
656 }
Romain Guyac670c02010-07-27 17:39:27 -0700657
658 // Begin the shader
659 shader.append(gFS_Main); {
660 // Stores the result in fragColor directly
Romain Guyaa6c24c2011-04-28 18:40:04 -0700661 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700662 if (description.hasAlpha8Texture) {
Romain Guy707b2f72010-10-11 16:34:59 -0700663 if (!description.hasGradient && !description.hasBitmap) {
664 shader.append(gFS_Main_FetchA8Texture[modulateOp]);
665 }
Romain Guyac670c02010-07-27 17:39:27 -0700666 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700667 shader.append(gFS_Main_FetchTexture[modulateOp]);
Romain Guyac670c02010-07-27 17:39:27 -0700668 }
669 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700670 if ((!description.hasGradient && !description.hasBitmap) || description.modulate) {
671 shader.append(gFS_Main_FetchColor);
672 }
Romain Guyac670c02010-07-27 17:39:27 -0700673 }
674 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700675 shader.append(gFS_Main_FetchGradient[gradientIndex(description)]);
Romain Guy211efea2012-07-31 21:16:07 -0700676 shader.append(gFS_Main_AddDitherToGradient);
Romain Guyac670c02010-07-27 17:39:27 -0700677 }
678 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700679 if (description.isPoint) {
680 shader.append(gFS_Main_PointBitmapTexCoords);
681 }
Romain Guy889f8d12010-07-29 14:37:42 -0700682 if (!description.isBitmapNpot) {
683 shader.append(gFS_Main_FetchBitmap);
684 } else {
685 shader.append(gFS_Main_FetchBitmapNpot);
686 }
Romain Guyac670c02010-07-27 17:39:27 -0700687 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700688 bool applyModulate = false;
Romain Guyac670c02010-07-27 17:39:27 -0700689 // Case when we have two shaders set
690 if (description.hasGradient && description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700691 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
Romain Guyac670c02010-07-27 17:39:27 -0700692 if (description.isBitmapFirst) {
693 shader.append(gFS_Main_BlendShadersBG);
694 } else {
695 shader.append(gFS_Main_BlendShadersGB);
696 }
Romain Guy707b2f72010-10-11 16:34:59 -0700697 shader.append(gFS_Main_BlendShaders_Modulate[op]);
Romain Guy740bf2b2011-04-26 15:33:10 -0700698 applyModulate = true;
Romain Guy889f8d12010-07-29 14:37:42 -0700699 } else {
700 if (description.hasGradient) {
Romain Guy707b2f72010-10-11 16:34:59 -0700701 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
702 shader.append(gFS_Main_GradientShader_Modulate[op]);
Romain Guy740bf2b2011-04-26 15:33:10 -0700703 applyModulate = true;
Romain Guy889f8d12010-07-29 14:37:42 -0700704 } else if (description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700705 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
706 shader.append(gFS_Main_BitmapShader_Modulate[op]);
Romain Guy740bf2b2011-04-26 15:33:10 -0700707 applyModulate = true;
Romain Guy889f8d12010-07-29 14:37:42 -0700708 }
Romain Guyac670c02010-07-27 17:39:27 -0700709 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700710 if (description.modulate && applyModulate) {
Romain Guy41210632012-07-16 17:04:24 -0700711 if (description.hasGammaCorrection) {
712 shader.append(gFS_Main_ModulateColor_ApplyGamma);
713 } else {
714 shader.append(gFS_Main_ModulateColor);
715 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700716 }
Romain Guyac670c02010-07-27 17:39:27 -0700717 // Apply the color op if needed
718 shader.append(gFS_Main_ApplyColorOp[description.colorOp]);
Chris Craik9f44a132012-09-13 18:34:55 -0700719
720 if (description.isAARect) {
721 shader.append(gFS_Main_AccountForAARect);
722 } else if (description.isAA) {
723 shader.append(gFS_Main_AccountForAA);
724 }
725
Romain Guyac670c02010-07-27 17:39:27 -0700726 // Output the fragment
Romain Guya5aed0d2010-09-09 14:42:43 -0700727 if (!blendFramebuffer) {
728 shader.append(gFS_Main_FragColor);
729 } else {
Romain Guyf607bdc2010-09-10 19:20:06 -0700730 shader.append(!description.swapSrcDst ?
731 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap);
Romain Guya5aed0d2010-09-09 14:42:43 -0700732 }
Romain Guyac670c02010-07-27 17:39:27 -0700733 }
734 // End the shader
735 shader.append(gFS_Footer);
736
Romain Guyc15008e2010-11-10 11:59:15 -0800737#if DEBUG_PROGRAMS
Romain Guydb1938e2010-08-02 18:50:22 -0700738 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
739 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800740#endif
Romain Guydb1938e2010-08-02 18:50:22 -0700741
Romain Guyac670c02010-07-27 17:39:27 -0700742 return shader;
743}
744
Romain Guy48daa542010-08-10 19:21:34 -0700745void ProgramCache::generateBlend(String8& shader, const char* name, SkXfermode::Mode mode) {
Romain Guyac670c02010-07-27 17:39:27 -0700746 shader.append("\nvec4 ");
747 shader.append(name);
748 shader.append("(vec4 src, vec4 dst) {\n");
749 shader.append(" ");
Romain Guy48daa542010-08-10 19:21:34 -0700750 shader.append(gBlendOps[mode]);
Romain Guyac670c02010-07-27 17:39:27 -0700751 shader.append("}\n");
752}
753
Romain Guy889f8d12010-07-29 14:37:42 -0700754void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) {
Romain Guy63553472012-07-18 20:04:14 -0700755 shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700756 if (wrapS == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700757 shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700758 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n");
759 }
760 if (wrapT == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700761 shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700762 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n");
763 }
764 shader.append(" return vec2(");
765 switch (wrapS) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700766 case GL_CLAMP_TO_EDGE:
767 shader.append("texCoords.x");
768 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700769 case GL_REPEAT:
770 shader.append("mod(texCoords.x, 1.0)");
771 break;
772 case GL_MIRRORED_REPEAT:
773 shader.append("xMod2");
774 break;
775 }
776 shader.append(", ");
777 switch (wrapT) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700778 case GL_CLAMP_TO_EDGE:
779 shader.append("texCoords.y");
780 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700781 case GL_REPEAT:
782 shader.append("mod(texCoords.y, 1.0)");
783 break;
784 case GL_MIRRORED_REPEAT:
785 shader.append("yMod2");
786 break;
787 }
788 shader.append(");\n");
789 shader.append("}\n");
790}
791
Romain Guydb1938e2010-08-02 18:50:22 -0700792void ProgramCache::printLongString(const String8& shader) const {
793 ssize_t index = 0;
794 ssize_t lastIndex = 0;
795 const char* str = shader.string();
796 while ((index = shader.find("\n", index)) > -1) {
797 String8 line(str, index - lastIndex);
798 if (line.length() == 0) line.append("\n");
799 PROGRAM_LOGD("%s", line.string());
800 index++;
801 str += (index - lastIndex);
802 lastIndex = index;
803 }
804}
805
Romain Guyac670c02010-07-27 17:39:27 -0700806}; // namespace uirenderer
807}; // namespace android