blob: 8c81ce2f078f6f11c48d5e430f5a4a6ef13421ac [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
Chet Haasea1d12dd2012-09-21 14:50:14 -070056 "uniform mat4 screenSpace;\n"
57 "uniform float ditherSize;\n",
Romain Guyee916f12010-09-20 17:53:08 -070058 // Circular
Chet Haasea1d12dd2012-09-21 14:50:14 -070059 "uniform mat4 screenSpace;\n"
60 "uniform float ditherSize;\n",
Romain Guyee916f12010-09-20 17:53:08 -070061 // Sweep
Romain Guyee916f12010-09-20 17:53:08 -070062 "uniform mat4 screenSpace;\n"
Chet Haasea1d12dd2012-09-21 14:50:14 -070063 "uniform float ditherSize;\n"
Romain Guyee916f12010-09-20 17:53:08 -070064};
Romain Guy889f8d12010-07-29 14:37:42 -070065const char* gVS_Header_Uniforms_HasBitmap =
66 "uniform mat4 textureTransform;\n"
Romain Guy80bbfb12011-03-23 16:56:28 -070067 "uniform mediump vec2 textureDimension;\n";
Romain Guyac670c02010-07-27 17:39:27 -070068const char* gVS_Header_Varyings_HasTexture =
69 "varying vec2 outTexCoords;\n";
Chet Haase99585ad2011-05-02 15:00:16 -070070const char* gVS_Header_Varyings_IsAA =
71 "varying float widthProportion;\n"
72 "varying float lengthProportion;\n";
Chris Craik6ebdc112012-08-31 18:24:33 -070073const char* gVS_Header_Varyings_IsAARect =
74 "varying float alpha;\n";
Romain Guy63553472012-07-18 20:04:14 -070075const char* gVS_Header_Varyings_HasBitmap =
76 "varying highp vec2 outBitmapTexCoords;\n";
77const char* gVS_Header_Varyings_PointHasBitmap =
78 "varying highp vec2 outPointBitmapTexCoords;\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -070079const char* gVS_Header_Varyings_HasGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -070080 // Linear
Chet Haasea1d12dd2012-09-21 14:50:14 -070081 "varying highp vec2 linear;\n"
82 "varying vec2 ditherTexCoords;\n",
83 "varying float linear;\n"
84 "varying vec2 ditherTexCoords;\n",
Romain Guy211efea2012-07-31 21:16:07 -070085
Romain Guyee916f12010-09-20 17:53:08 -070086 // Circular
Chet Haasea1d12dd2012-09-21 14:50:14 -070087 "varying highp vec2 circular;\n"
88 "varying vec2 ditherTexCoords;\n",
89 "varying highp vec2 circular;\n"
90 "varying vec2 ditherTexCoords;\n",
Romain Guy211efea2012-07-31 21:16:07 -070091
Romain Guyee916f12010-09-20 17:53:08 -070092 // Sweep
Chet Haasea1d12dd2012-09-21 14:50:14 -070093 "varying highp vec2 sweep;\n"
94 "varying vec2 ditherTexCoords;\n",
95 "varying highp vec2 sweep;\n"
96 "varying vec2 ditherTexCoords;\n",
Romain Guyee916f12010-09-20 17:53:08 -070097};
Romain Guyac670c02010-07-27 17:39:27 -070098const char* gVS_Main =
99 "\nvoid main(void) {\n";
100const char* gVS_Main_OutTexCoords =
101 " outTexCoords = texCoords;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700102const char* gVS_Main_OutTransformedTexCoords =
103 " outTexCoords = (mainTextureTransform * vec4(texCoords, 0.0, 1.0)).xy;\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -0700104const char* gVS_Main_OutGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700105 // Linear
Chet Haasea1d12dd2012-09-21 14:50:14 -0700106 " linear = vec2((screenSpace * position).x, 0.5);\n"
107 " ditherTexCoords = (gl_Position * ditherSize).xy;\n",
108 " linear = (screenSpace * position).x;\n"
109 " ditherTexCoords = (gl_Position * ditherSize).xy;\n",
Romain Guy211efea2012-07-31 21:16:07 -0700110
Romain Guyee916f12010-09-20 17:53:08 -0700111 // Circular
Chet Haasea1d12dd2012-09-21 14:50:14 -0700112 " circular = (screenSpace * position).xy;\n"
113 " ditherTexCoords = (gl_Position * ditherSize).xy;\n",
114 " circular = (screenSpace * position).xy;\n"
115 " ditherTexCoords = (gl_Position * ditherSize).xy;\n",
Romain Guy211efea2012-07-31 21:16:07 -0700116
Romain Guyee916f12010-09-20 17:53:08 -0700117 // Sweep
Chet Haasea1d12dd2012-09-21 14:50:14 -0700118 " sweep = (screenSpace * position).xy;\n"
119 " ditherTexCoords = (gl_Position * ditherSize).xy;\n",
120 " sweep = (screenSpace * position).xy;\n"
121 " ditherTexCoords = (gl_Position * ditherSize).xy;\n",
Romain Guyee916f12010-09-20 17:53:08 -0700122};
Romain Guy889f8d12010-07-29 14:37:42 -0700123const char* gVS_Main_OutBitmapTexCoords =
Romain Guy707b2f72010-10-11 16:34:59 -0700124 " outBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -0700125const char* gVS_Main_OutPointBitmapTexCoords =
126 " outPointBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700127const char* gVS_Main_Position =
128 " gl_Position = transform * position;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -0700129const char* gVS_Main_PointSize =
130 " gl_PointSize = pointSize;\n";
Chet Haase99585ad2011-05-02 15:00:16 -0700131const char* gVS_Main_AA =
132 " widthProportion = vtxWidth;\n"
133 " lengthProportion = vtxLength;\n";
Chris Craik6ebdc112012-08-31 18:24:33 -0700134const char* gVS_Main_AARect =
135 " alpha = vtxAlpha;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700136const char* gVS_Footer =
137 "}\n\n";
138
139///////////////////////////////////////////////////////////////////////////////
140// Fragment shaders snippets
141///////////////////////////////////////////////////////////////////////////////
142
Romain Guya5aed0d2010-09-09 14:42:43 -0700143const char* gFS_Header_Extension_FramebufferFetch =
144 "#extension GL_NV_shader_framebuffer_fetch : enable\n\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700145const char* gFS_Header_Extension_ExternalTexture =
146 "#extension GL_OES_EGL_image_external : require\n\n";
Romain Guyac670c02010-07-27 17:39:27 -0700147const char* gFS_Header =
148 "precision mediump float;\n\n";
149const char* gFS_Uniforms_Color =
150 "uniform vec4 color;\n";
Chet Haase99585ad2011-05-02 15:00:16 -0700151const char* gFS_Uniforms_AA =
Chet Haase5b0200b2011-04-13 17:58:08 -0700152 "uniform float boundaryWidth;\n"
Chris Craika798b952012-08-27 17:03:13 -0700153 "uniform float boundaryLength;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -0700154const char* gFS_Header_Uniforms_PointHasBitmap =
155 "uniform vec2 textureDimension;\n"
156 "uniform float pointSize;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700157const char* gFS_Uniforms_TextureSampler =
Romain Guya938f562012-09-13 20:31:08 -0700158 "uniform sampler2D baseSampler;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700159const char* gFS_Uniforms_ExternalTextureSampler =
Romain Guya938f562012-09-13 20:31:08 -0700160 "uniform samplerExternalOES baseSampler;\n";
Romain Guy211efea2012-07-31 21:16:07 -0700161#define FS_UNIFORMS_DITHER \
Chet Haasea1d12dd2012-09-21 14:50:14 -0700162 "uniform float ditherSizeSquared;\n" \
Romain Guy211efea2012-07-31 21:16:07 -0700163 "uniform sampler2D ditherSampler;\n"
164#define FS_UNIFORMS_GRADIENT \
165 "uniform vec4 startColor;\n" \
166 "uniform vec4 endColor;\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700167const char* gFS_Uniforms_GradientSampler[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700168 // Linear
Romain Guy211efea2012-07-31 21:16:07 -0700169 FS_UNIFORMS_DITHER "uniform sampler2D gradientSampler;\n",
170 FS_UNIFORMS_DITHER FS_UNIFORMS_GRADIENT,
171
Romain Guyee916f12010-09-20 17:53:08 -0700172 // Circular
Romain Guy211efea2012-07-31 21:16:07 -0700173 FS_UNIFORMS_DITHER "uniform sampler2D gradientSampler;\n",
174 FS_UNIFORMS_DITHER FS_UNIFORMS_GRADIENT,
175
Romain Guyee916f12010-09-20 17:53:08 -0700176 // Sweep
Romain Guy211efea2012-07-31 21:16:07 -0700177 FS_UNIFORMS_DITHER "uniform sampler2D gradientSampler;\n",
178 FS_UNIFORMS_DITHER FS_UNIFORMS_GRADIENT
Romain Guyee916f12010-09-20 17:53:08 -0700179};
Romain Guyac670c02010-07-27 17:39:27 -0700180const char* gFS_Uniforms_BitmapSampler =
181 "uniform sampler2D bitmapSampler;\n";
182const char* gFS_Uniforms_ColorOp[4] = {
183 // None
184 "",
185 // Matrix
186 "uniform mat4 colorMatrix;\n"
187 "uniform vec4 colorMatrixVector;\n",
188 // Lighting
Romain Guydb1938e2010-08-02 18:50:22 -0700189 "uniform vec4 lightingMul;\n"
190 "uniform vec4 lightingAdd;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700191 // PorterDuff
Romain Guydb1938e2010-08-02 18:50:22 -0700192 "uniform vec4 colorBlend;\n"
Romain Guyac670c02010-07-27 17:39:27 -0700193};
Romain Guy41210632012-07-16 17:04:24 -0700194const char* gFS_Uniforms_Gamma =
195 "uniform float gamma;\n";
196
Romain Guyac670c02010-07-27 17:39:27 -0700197const char* gFS_Main =
198 "\nvoid main(void) {\n"
Romain Guy7fbcc042010-08-04 15:40:07 -0700199 " lowp vec4 fragColor;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700200
Romain Guyed6fcb02011-03-21 13:11:28 -0700201const char* gFS_Main_PointBitmapTexCoords =
Romain Guy63553472012-07-18 20:04:14 -0700202 " highp vec2 outBitmapTexCoords = outPointBitmapTexCoords + "
Romain Guyed6fcb02011-03-21 13:11:28 -0700203 "((gl_PointCoord - vec2(0.5, 0.5)) * textureDimension * vec2(pointSize, pointSize));\n";
204
Romain Guy211efea2012-07-31 21:16:07 -0700205#define FS_MAIN_DITHER \
Chet Haasea1d12dd2012-09-21 14:50:14 -0700206 "texture2D(ditherSampler, ditherTexCoords).a * ditherSizeSquared"
Romain Guy211efea2012-07-31 21:16:07 -0700207const char* gFS_Main_AddDitherToGradient =
208 " gradientColor += " FS_MAIN_DITHER ";\n";
209
Romain Guy707b2f72010-10-11 16:34:59 -0700210// Fast cases
211const char* gFS_Fast_SingleColor =
212 "\nvoid main(void) {\n"
213 " gl_FragColor = color;\n"
214 "}\n\n";
215const char* gFS_Fast_SingleTexture =
216 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700217 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700218 "}\n\n";
219const char* gFS_Fast_SingleModulateTexture =
220 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700221 " gl_FragColor = color.a * texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700222 "}\n\n";
223const char* gFS_Fast_SingleA8Texture =
224 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700225 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700226 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700227const char* gFS_Fast_SingleA8Texture_ApplyGamma =
228 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700229 " gl_FragColor = vec4(0.0, 0.0, 0.0, pow(texture2D(baseSampler, outTexCoords).a, gamma));\n"
Romain Guy41210632012-07-16 17:04:24 -0700230 "}\n\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700231const char* gFS_Fast_SingleModulateA8Texture =
232 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700233 " gl_FragColor = color * texture2D(baseSampler, outTexCoords).a;\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700234 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700235const char* gFS_Fast_SingleModulateA8Texture_ApplyGamma =
236 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700237 " gl_FragColor = color * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy41210632012-07-16 17:04:24 -0700238 "}\n\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -0700239const char* gFS_Fast_SingleGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700240 "\nvoid main(void) {\n"
Romain Guy211efea2012-07-31 21:16:07 -0700241 " gl_FragColor = " FS_MAIN_DITHER " + texture2D(gradientSampler, linear);\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700242 "}\n\n",
243 "\nvoid main(void) {\n"
Romain Guy211efea2012-07-31 21:16:07 -0700244 " gl_FragColor = " FS_MAIN_DITHER " + mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700245 "}\n\n"
246};
247const char* gFS_Fast_SingleModulateGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700248 "\nvoid main(void) {\n"
Chet Haase1c5c2062012-09-17 17:09:21 -0700249 " gl_FragColor = " FS_MAIN_DITHER " + color.a * texture2D(gradientSampler, linear);\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700250 "}\n\n",
251 "\nvoid main(void) {\n"
Chet Haase1c5c2062012-09-17 17:09:21 -0700252 " gl_FragColor = " FS_MAIN_DITHER " + color.a * mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700253 "}\n\n"
254};
Romain Guy707b2f72010-10-11 16:34:59 -0700255
256// General case
Romain Guyac670c02010-07-27 17:39:27 -0700257const char* gFS_Main_FetchColor =
258 " fragColor = color;\n";
Romain Guy740bf2b2011-04-26 15:33:10 -0700259const char* gFS_Main_ModulateColor =
260 " fragColor *= color.a;\n";
Chet Haase99585ad2011-05-02 15:00:16 -0700261const char* gFS_Main_AccountForAA =
Chris Craika798b952012-08-27 17:03:13 -0700262 " fragColor *= (1.0 - smoothstep(boundaryWidth, 0.5, abs(0.5 - widthProportion)))\n"
263 " * (1.0 - smoothstep(boundaryLength, 0.5, abs(0.5 - lengthProportion)));\n";
Chris Craik6ebdc112012-08-31 18:24:33 -0700264const char* gFS_Main_AccountForAARect =
265 " fragColor *= alpha;\n";
Chris Craika798b952012-08-27 17:03:13 -0700266
Romain Guy707b2f72010-10-11 16:34:59 -0700267const char* gFS_Main_FetchTexture[2] = {
268 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700269 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700270 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700271 " fragColor = color * texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700272};
Romain Guya938f562012-09-13 20:31:08 -0700273const char* gFS_Main_FetchA8Texture[4] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700274 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700275 " fragColor = texture2D(baseSampler, outTexCoords);\n",
276 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700277 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700278 " fragColor = color * texture2D(baseSampler, outTexCoords).a;\n",
279 " fragColor = color * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700280};
Romain Guy42e1e0d2012-07-30 14:47:51 -0700281const char* gFS_Main_FetchGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700282 // Linear
Romain Guy320d46b2012-08-08 16:05:42 -0700283 " vec4 gradientColor = texture2D(gradientSampler, linear);\n",
Romain Guy211efea2012-07-31 21:16:07 -0700284
Romain Guy320d46b2012-08-08 16:05:42 -0700285 " vec4 gradientColor = mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700286
Romain Guyee916f12010-09-20 17:53:08 -0700287 // Circular
Romain Guy320d46b2012-08-08 16:05:42 -0700288 " vec4 gradientColor = texture2D(gradientSampler, vec2(length(circular), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700289
Romain Guy320d46b2012-08-08 16:05:42 -0700290 " vec4 gradientColor = mix(startColor, endColor, clamp(length(circular), 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700291
Romain Guyee916f12010-09-20 17:53:08 -0700292 // Sweep
Romain Guy63553472012-07-18 20:04:14 -0700293 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700294 " vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700295
Romain Guy42e1e0d2012-07-30 14:47:51 -0700296 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700297 " vec4 gradientColor = mix(startColor, endColor, clamp(index - floor(index), 0.0, 1.0));\n"
Romain Guyee916f12010-09-20 17:53:08 -0700298};
Romain Guyac670c02010-07-27 17:39:27 -0700299const char* gFS_Main_FetchBitmap =
300 " vec4 bitmapColor = texture2D(bitmapSampler, outBitmapTexCoords);\n";
Romain Guy889f8d12010-07-29 14:37:42 -0700301const char* gFS_Main_FetchBitmapNpot =
302 " vec4 bitmapColor = texture2D(bitmapSampler, wrap(outBitmapTexCoords));\n";
Romain Guyac670c02010-07-27 17:39:27 -0700303const char* gFS_Main_BlendShadersBG =
Romain Guyac670c02010-07-27 17:39:27 -0700304 " fragColor = blendShaders(gradientColor, bitmapColor)";
Romain Guy06f96e22010-07-30 19:18:16 -0700305const char* gFS_Main_BlendShadersGB =
306 " fragColor = blendShaders(bitmapColor, gradientColor)";
Romain Guya938f562012-09-13 20:31:08 -0700307const char* gFS_Main_BlendShaders_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700308 // Don't modulate
309 ";\n",
Romain Guya938f562012-09-13 20:31:08 -0700310 ";\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700311 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700312 " * color.a;\n",
313 " * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700314 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700315 " * texture2D(baseSampler, outTexCoords).a;\n",
316 " * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700317};
Romain Guya938f562012-09-13 20:31:08 -0700318const char* gFS_Main_GradientShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700319 // Don't modulate
320 " fragColor = gradientColor;\n",
Romain Guya938f562012-09-13 20:31:08 -0700321 " fragColor = gradientColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700322 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700323 " fragColor = gradientColor * color.a;\n",
324 " fragColor = gradientColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700325 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700326 " fragColor = gradientColor * texture2D(baseSampler, outTexCoords).a;\n",
327 " fragColor = gradientColor * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700328 };
Romain Guya938f562012-09-13 20:31:08 -0700329const char* gFS_Main_BitmapShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700330 // Don't modulate
331 " fragColor = bitmapColor;\n",
Romain Guya938f562012-09-13 20:31:08 -0700332 " fragColor = bitmapColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700333 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700334 " fragColor = bitmapColor * color.a;\n",
335 " fragColor = bitmapColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700336 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700337 " fragColor = bitmapColor * texture2D(baseSampler, outTexCoords).a;\n",
338 " fragColor = bitmapColor * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700339 };
Romain Guyac670c02010-07-27 17:39:27 -0700340const char* gFS_Main_FragColor =
341 " gl_FragColor = fragColor;\n";
Romain Guya5aed0d2010-09-09 14:42:43 -0700342const char* gFS_Main_FragColor_Blend =
343 " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n";
Romain Guyf607bdc2010-09-10 19:20:06 -0700344const char* gFS_Main_FragColor_Blend_Swap =
345 " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n";
Romain Guyac670c02010-07-27 17:39:27 -0700346const char* gFS_Main_ApplyColorOp[4] = {
347 // None
348 "",
349 // Matrix
Romain Guydb1938e2010-08-02 18:50:22 -0700350 // TODO: Fix premultiplied alpha computations for color matrix
Romain Guyac670c02010-07-27 17:39:27 -0700351 " fragColor *= colorMatrix;\n"
Romain Guydb1938e2010-08-02 18:50:22 -0700352 " fragColor += colorMatrixVector;\n"
353 " fragColor.rgb *= fragColor.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700354 // Lighting
Romain Guydb1938e2010-08-02 18:50:22 -0700355 " float lightingAlpha = fragColor.a;\n"
356 " fragColor = min(fragColor * lightingMul + (lightingAdd * lightingAlpha), lightingAlpha);\n"
357 " fragColor.a = lightingAlpha;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700358 // PorterDuff
359 " fragColor = blendColors(colorBlend, fragColor);\n"
360};
361const char* gFS_Footer =
362 "}\n\n";
363
364///////////////////////////////////////////////////////////////////////////////
365// PorterDuff snippets
366///////////////////////////////////////////////////////////////////////////////
367
Romain Guy48daa542010-08-10 19:21:34 -0700368const char* gBlendOps[18] = {
Romain Guyac670c02010-07-27 17:39:27 -0700369 // Clear
370 "return vec4(0.0, 0.0, 0.0, 0.0);\n",
371 // Src
372 "return src;\n",
373 // Dst
374 "return dst;\n",
375 // SrcOver
Romain Guy06f96e22010-07-30 19:18:16 -0700376 "return src + dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700377 // DstOver
Romain Guy06f96e22010-07-30 19:18:16 -0700378 "return dst + src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700379 // SrcIn
Romain Guy06f96e22010-07-30 19:18:16 -0700380 "return src * dst.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700381 // DstIn
Romain Guy06f96e22010-07-30 19:18:16 -0700382 "return dst * src.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700383 // SrcOut
Romain Guy06f96e22010-07-30 19:18:16 -0700384 "return src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700385 // DstOut
Romain Guy06f96e22010-07-30 19:18:16 -0700386 "return dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700387 // SrcAtop
388 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n",
389 // DstAtop
390 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n",
391 // Xor
Romain Guy48daa542010-08-10 19:21:34 -0700392 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, "
Romain Guyac670c02010-07-27 17:39:27 -0700393 "src.a + dst.a - 2.0 * src.a * dst.a);\n",
Romain Guy48daa542010-08-10 19:21:34 -0700394 // Add
395 "return min(src + dst, 1.0);\n",
396 // Multiply
397 "return src * dst;\n",
398 // Screen
399 "return src + dst - src * dst;\n",
400 // Overlay
401 "return clamp(vec4(mix("
402 "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), "
403 "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), "
404 "step(dst.a, 2.0 * dst.rgb)), "
405 "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n",
406 // Darken
407 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
408 "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
409 // Lighten
410 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
411 "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 -0700412};
413
414///////////////////////////////////////////////////////////////////////////////
415// Constructors/destructors
416///////////////////////////////////////////////////////////////////////////////
417
418ProgramCache::ProgramCache() {
419}
420
421ProgramCache::~ProgramCache() {
422 clear();
423}
424
425///////////////////////////////////////////////////////////////////////////////
426// Cache management
427///////////////////////////////////////////////////////////////////////////////
428
429void ProgramCache::clear() {
Romain Guy67f27952010-12-07 20:09:23 -0800430 PROGRAM_LOGD("Clearing program cache");
431
Romain Guyac670c02010-07-27 17:39:27 -0700432 size_t count = mCache.size();
433 for (size_t i = 0; i < count; i++) {
434 delete mCache.valueAt(i);
435 }
436 mCache.clear();
437}
438
439Program* ProgramCache::get(const ProgramDescription& description) {
440 programid key = description.key();
441 ssize_t index = mCache.indexOfKey(key);
442 Program* program = NULL;
443 if (index < 0) {
Romain Guyee916f12010-09-20 17:53:08 -0700444 description.log("Could not find program");
Romain Guyac670c02010-07-27 17:39:27 -0700445 program = generateProgram(description, key);
446 mCache.add(key, program);
447 } else {
448 program = mCache.valueAt(index);
449 }
450 return program;
451}
452
453///////////////////////////////////////////////////////////////////////////////
454// Program generation
455///////////////////////////////////////////////////////////////////////////////
456
457Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) {
458 String8 vertexShader = generateVertexShader(description);
459 String8 fragmentShader = generateFragmentShader(description);
460
Romain Guy42e1e0d2012-07-30 14:47:51 -0700461 return new Program(description, vertexShader.string(), fragmentShader.string());
462}
463
464static inline size_t gradientIndex(const ProgramDescription& description) {
465 return description.gradientType * 2 + description.isSimpleGradient;
Romain Guyac670c02010-07-27 17:39:27 -0700466}
467
468String8 ProgramCache::generateVertexShader(const ProgramDescription& description) {
469 // Add attributes
470 String8 shader(gVS_Header_Attributes);
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_Attributes_TexCoords);
473 }
Chris Craik6ebdc112012-08-31 18:24:33 -0700474 if (description.isAARect) {
475 shader.append(gVS_Header_Attributes_AARectParameters);
476 } else if (description.isAA) {
Chet Haase99585ad2011-05-02 15:00:16 -0700477 shader.append(gVS_Header_Attributes_AAParameters);
Chet Haase5b0200b2011-04-13 17:58:08 -0700478 }
Romain Guyac670c02010-07-27 17:39:27 -0700479 // Uniforms
480 shader.append(gVS_Header_Uniforms);
Romain Guy8f0095c2011-05-02 17:24:22 -0700481 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700482 shader.append(gVS_Header_Uniforms_TextureTransform);
483 }
Romain Guyac670c02010-07-27 17:39:27 -0700484 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700485 shader.append(gVS_Header_Uniforms_HasGradient[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700486 }
Romain Guy889f8d12010-07-29 14:37:42 -0700487 if (description.hasBitmap) {
488 shader.append(gVS_Header_Uniforms_HasBitmap);
489 }
Romain Guyed6fcb02011-03-21 13:11:28 -0700490 if (description.isPoint) {
491 shader.append(gVS_Header_Uniforms_IsPoint);
492 }
Romain Guyac670c02010-07-27 17:39:27 -0700493 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700494 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700495 shader.append(gVS_Header_Varyings_HasTexture);
496 }
Chris Craik6ebdc112012-08-31 18:24:33 -0700497 if (description.isAARect) {
498 shader.append(gVS_Header_Varyings_IsAARect);
499 } else if (description.isAA) {
Chet Haase99585ad2011-05-02 15:00:16 -0700500 shader.append(gVS_Header_Varyings_IsAA);
Chet Haase5b0200b2011-04-13 17:58:08 -0700501 }
Romain Guyac670c02010-07-27 17:39:27 -0700502 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700503 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700504 }
505 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700506 shader.append(description.isPoint ?
Romain Guy63553472012-07-18 20:04:14 -0700507 gVS_Header_Varyings_PointHasBitmap :
508 gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700509 }
510
511 // Begin the shader
512 shader.append(gVS_Main); {
Romain Guy8f0095c2011-05-02 17:24:22 -0700513 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700514 shader.append(gVS_Main_OutTransformedTexCoords);
Romain Guy8f0095c2011-05-02 17:24:22 -0700515 } else if (description.hasTexture || description.hasExternalTexture) {
516 shader.append(gVS_Main_OutTexCoords);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700517 }
Chris Craik6ebdc112012-08-31 18:24:33 -0700518 if (description.isAARect) {
519 shader.append(gVS_Main_AARect);
520 } else if (description.isAA) {
Chet Haase99585ad2011-05-02 15:00:16 -0700521 shader.append(gVS_Main_AA);
Chet Haase5b0200b2011-04-13 17:58:08 -0700522 }
Romain Guy889f8d12010-07-29 14:37:42 -0700523 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700524 shader.append(description.isPoint ?
525 gVS_Main_OutPointBitmapTexCoords :
526 gVS_Main_OutBitmapTexCoords);
527 }
528 if (description.isPoint) {
529 shader.append(gVS_Main_PointSize);
Romain Guy889f8d12010-07-29 14:37:42 -0700530 }
Romain Guyac670c02010-07-27 17:39:27 -0700531 // Output transformed position
532 shader.append(gVS_Main_Position);
Chet Haasea1d12dd2012-09-21 14:50:14 -0700533 if (description.hasGradient) {
534 shader.append(gVS_Main_OutGradient[gradientIndex(description)]);
535 }
Romain Guyac670c02010-07-27 17:39:27 -0700536 }
537 // End the shader
538 shader.append(gVS_Footer);
539
540 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string());
541
542 return shader;
543}
544
Romain Guya938f562012-09-13 20:31:08 -0700545static bool shaderOp(const ProgramDescription& description, String8& shader,
546 const int modulateOp, const char** snippets) {
547 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
548 op = op * 2 + description.hasGammaCorrection;
549 shader.append(snippets[op]);
550 return description.hasAlpha8Texture;
551}
552
Romain Guyac670c02010-07-27 17:39:27 -0700553String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) {
Romain Guya5aed0d2010-09-09 14:42:43 -0700554 String8 shader;
555
Romain Guy707b2f72010-10-11 16:34:59 -0700556 const bool blendFramebuffer = description.framebufferMode >= SkXfermode::kPlus_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700557 if (blendFramebuffer) {
558 shader.append(gFS_Header_Extension_FramebufferFetch);
559 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700560 if (description.hasExternalTexture) {
561 shader.append(gFS_Header_Extension_ExternalTexture);
562 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700563
564 shader.append(gFS_Header);
Romain Guyac670c02010-07-27 17:39:27 -0700565
566 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700567 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700568 shader.append(gVS_Header_Varyings_HasTexture);
569 }
Chris Craik6ebdc112012-08-31 18:24:33 -0700570 if (description.isAARect) {
571 shader.append(gVS_Header_Varyings_IsAARect);
572 } else if (description.isAA) {
Chet Haase99585ad2011-05-02 15:00:16 -0700573 shader.append(gVS_Header_Varyings_IsAA);
Chet Haase5b0200b2011-04-13 17:58:08 -0700574 }
Romain Guyac670c02010-07-27 17:39:27 -0700575 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700576 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700577 }
578 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700579 shader.append(description.isPoint ?
Romain Guy63553472012-07-18 20:04:14 -0700580 gVS_Header_Varyings_PointHasBitmap :
581 gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700582 }
583
Romain Guyac670c02010-07-27 17:39:27 -0700584 // Uniforms
Romain Guy707b2f72010-10-11 16:34:59 -0700585 int modulateOp = MODULATE_OP_NO_MODULATE;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700586 const bool singleColor = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700587 !description.hasGradient && !description.hasBitmap;
588
589 if (description.modulate || singleColor) {
590 shader.append(gFS_Uniforms_Color);
591 if (!singleColor) modulateOp = MODULATE_OP_MODULATE;
592 }
Romain Guyac670c02010-07-27 17:39:27 -0700593 if (description.hasTexture) {
594 shader.append(gFS_Uniforms_TextureSampler);
Romain Guy8f0095c2011-05-02 17:24:22 -0700595 } else if (description.hasExternalTexture) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700596 shader.append(gFS_Uniforms_ExternalTextureSampler);
597 }
Chet Haase99585ad2011-05-02 15:00:16 -0700598 if (description.isAA) {
599 shader.append(gFS_Uniforms_AA);
Chet Haase5b0200b2011-04-13 17:58:08 -0700600 }
Romain Guyac670c02010-07-27 17:39:27 -0700601 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700602 shader.append(gFS_Uniforms_GradientSampler[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700603 }
Romain Guyed6fcb02011-03-21 13:11:28 -0700604 if (description.hasBitmap && description.isPoint) {
605 shader.append(gFS_Header_Uniforms_PointHasBitmap);
606 }
Romain Guy41210632012-07-16 17:04:24 -0700607 if (description.hasGammaCorrection) {
608 shader.append(gFS_Uniforms_Gamma);
609 }
Romain Guy707b2f72010-10-11 16:34:59 -0700610
611 // Optimization for common cases
Chet Haase99585ad2011-05-02 15:00:16 -0700612 if (!description.isAA && !blendFramebuffer &&
Chris Craik6ebdc112012-08-31 18:24:33 -0700613 description.colorOp == ProgramDescription::kColorNone &&
614 !description.isPoint && !description.isAARect) {
Romain Guy707b2f72010-10-11 16:34:59 -0700615 bool fast = false;
616
617 const bool noShader = !description.hasGradient && !description.hasBitmap;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700618 const bool singleTexture = (description.hasTexture || description.hasExternalTexture) &&
Romain Guy707b2f72010-10-11 16:34:59 -0700619 !description.hasAlpha8Texture && noShader;
620 const bool singleA8Texture = description.hasTexture &&
621 description.hasAlpha8Texture && noShader;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700622 const bool singleGradient = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700623 description.hasGradient && !description.hasBitmap &&
624 description.gradientType == ProgramDescription::kGradientLinear;
625
626 if (singleColor) {
627 shader.append(gFS_Fast_SingleColor);
628 fast = true;
629 } else if (singleTexture) {
630 if (!description.modulate) {
631 shader.append(gFS_Fast_SingleTexture);
632 } else {
633 shader.append(gFS_Fast_SingleModulateTexture);
634 }
635 fast = true;
636 } else if (singleA8Texture) {
637 if (!description.modulate) {
Romain Guy41210632012-07-16 17:04:24 -0700638 if (description.hasGammaCorrection) {
639 shader.append(gFS_Fast_SingleA8Texture_ApplyGamma);
640 } else {
641 shader.append(gFS_Fast_SingleA8Texture);
642 }
Romain Guy707b2f72010-10-11 16:34:59 -0700643 } else {
Romain Guy41210632012-07-16 17:04:24 -0700644 if (description.hasGammaCorrection) {
645 shader.append(gFS_Fast_SingleModulateA8Texture_ApplyGamma);
646 } else {
647 shader.append(gFS_Fast_SingleModulateA8Texture);
648 }
Romain Guy707b2f72010-10-11 16:34:59 -0700649 }
650 fast = true;
651 } else if (singleGradient) {
652 if (!description.modulate) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700653 shader.append(gFS_Fast_SingleGradient[description.isSimpleGradient]);
Romain Guy707b2f72010-10-11 16:34:59 -0700654 } else {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700655 shader.append(gFS_Fast_SingleModulateGradient[description.isSimpleGradient]);
Romain Guy707b2f72010-10-11 16:34:59 -0700656 }
657 fast = true;
658 }
659
660 if (fast) {
Romain Guyc15008e2010-11-10 11:59:15 -0800661#if DEBUG_PROGRAMS
Romain Guy707b2f72010-10-11 16:34:59 -0700662 PROGRAM_LOGD("*** Fast case:\n");
663 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
664 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800665#endif
Romain Guy707b2f72010-10-11 16:34:59 -0700666
667 return shader;
668 }
669 }
670
Romain Guyac670c02010-07-27 17:39:27 -0700671 if (description.hasBitmap) {
672 shader.append(gFS_Uniforms_BitmapSampler);
673 }
674 shader.append(gFS_Uniforms_ColorOp[description.colorOp]);
675
676 // Generate required functions
677 if (description.hasGradient && description.hasBitmap) {
Romain Guy48daa542010-08-10 19:21:34 -0700678 generateBlend(shader, "blendShaders", description.shadersMode);
Romain Guyac670c02010-07-27 17:39:27 -0700679 }
680 if (description.colorOp == ProgramDescription::kColorBlend) {
Romain Guy48daa542010-08-10 19:21:34 -0700681 generateBlend(shader, "blendColors", description.colorMode);
Romain Guyac670c02010-07-27 17:39:27 -0700682 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700683 if (blendFramebuffer) {
684 generateBlend(shader, "blendFramebuffer", description.framebufferMode);
685 }
Romain Guy889f8d12010-07-29 14:37:42 -0700686 if (description.isBitmapNpot) {
687 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
688 }
Romain Guyac670c02010-07-27 17:39:27 -0700689
690 // Begin the shader
691 shader.append(gFS_Main); {
692 // Stores the result in fragColor directly
Romain Guyaa6c24c2011-04-28 18:40:04 -0700693 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700694 if (description.hasAlpha8Texture) {
Romain Guy707b2f72010-10-11 16:34:59 -0700695 if (!description.hasGradient && !description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700696 shader.append(gFS_Main_FetchA8Texture[modulateOp * 2 +
697 description.hasGammaCorrection]);
Romain Guy707b2f72010-10-11 16:34:59 -0700698 }
Romain Guyac670c02010-07-27 17:39:27 -0700699 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700700 shader.append(gFS_Main_FetchTexture[modulateOp]);
Romain Guyac670c02010-07-27 17:39:27 -0700701 }
702 } else {
Romain Guya938f562012-09-13 20:31:08 -0700703 if (!description.hasGradient && !description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700704 shader.append(gFS_Main_FetchColor);
705 }
Romain Guyac670c02010-07-27 17:39:27 -0700706 }
707 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700708 shader.append(gFS_Main_FetchGradient[gradientIndex(description)]);
Romain Guy211efea2012-07-31 21:16:07 -0700709 shader.append(gFS_Main_AddDitherToGradient);
Romain Guyac670c02010-07-27 17:39:27 -0700710 }
711 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700712 if (description.isPoint) {
713 shader.append(gFS_Main_PointBitmapTexCoords);
714 }
Romain Guy889f8d12010-07-29 14:37:42 -0700715 if (!description.isBitmapNpot) {
716 shader.append(gFS_Main_FetchBitmap);
717 } else {
718 shader.append(gFS_Main_FetchBitmapNpot);
719 }
Romain Guyac670c02010-07-27 17:39:27 -0700720 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700721 bool applyModulate = false;
Romain Guyac670c02010-07-27 17:39:27 -0700722 // Case when we have two shaders set
723 if (description.hasGradient && description.hasBitmap) {
724 if (description.isBitmapFirst) {
725 shader.append(gFS_Main_BlendShadersBG);
726 } else {
727 shader.append(gFS_Main_BlendShadersGB);
728 }
Romain Guya938f562012-09-13 20:31:08 -0700729 applyModulate = shaderOp(description, shader, modulateOp,
730 gFS_Main_BlendShaders_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700731 } else {
732 if (description.hasGradient) {
Romain Guya938f562012-09-13 20:31:08 -0700733 applyModulate = shaderOp(description, shader, modulateOp,
734 gFS_Main_GradientShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700735 } else if (description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700736 applyModulate = shaderOp(description, shader, modulateOp,
737 gFS_Main_BitmapShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700738 }
Romain Guyac670c02010-07-27 17:39:27 -0700739 }
Romain Guya938f562012-09-13 20:31:08 -0700740
Romain Guy740bf2b2011-04-26 15:33:10 -0700741 if (description.modulate && applyModulate) {
Romain Guya938f562012-09-13 20:31:08 -0700742 shader.append(gFS_Main_ModulateColor);
Romain Guy740bf2b2011-04-26 15:33:10 -0700743 }
Romain Guya938f562012-09-13 20:31:08 -0700744
Romain Guyac670c02010-07-27 17:39:27 -0700745 // Apply the color op if needed
746 shader.append(gFS_Main_ApplyColorOp[description.colorOp]);
Chris Craik9f44a132012-09-13 18:34:55 -0700747
748 if (description.isAARect) {
749 shader.append(gFS_Main_AccountForAARect);
750 } else if (description.isAA) {
751 shader.append(gFS_Main_AccountForAA);
752 }
753
Romain Guyac670c02010-07-27 17:39:27 -0700754 // Output the fragment
Romain Guya5aed0d2010-09-09 14:42:43 -0700755 if (!blendFramebuffer) {
756 shader.append(gFS_Main_FragColor);
757 } else {
Romain Guyf607bdc2010-09-10 19:20:06 -0700758 shader.append(!description.swapSrcDst ?
759 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap);
Romain Guya5aed0d2010-09-09 14:42:43 -0700760 }
Romain Guyac670c02010-07-27 17:39:27 -0700761 }
762 // End the shader
763 shader.append(gFS_Footer);
764
Romain Guyc15008e2010-11-10 11:59:15 -0800765#if DEBUG_PROGRAMS
Romain Guydb1938e2010-08-02 18:50:22 -0700766 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
767 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800768#endif
Romain Guydb1938e2010-08-02 18:50:22 -0700769
Romain Guyac670c02010-07-27 17:39:27 -0700770 return shader;
771}
772
Romain Guy48daa542010-08-10 19:21:34 -0700773void ProgramCache::generateBlend(String8& shader, const char* name, SkXfermode::Mode mode) {
Romain Guyac670c02010-07-27 17:39:27 -0700774 shader.append("\nvec4 ");
775 shader.append(name);
776 shader.append("(vec4 src, vec4 dst) {\n");
777 shader.append(" ");
Romain Guy48daa542010-08-10 19:21:34 -0700778 shader.append(gBlendOps[mode]);
Romain Guyac670c02010-07-27 17:39:27 -0700779 shader.append("}\n");
780}
781
Romain Guy889f8d12010-07-29 14:37:42 -0700782void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) {
Romain Guy63553472012-07-18 20:04:14 -0700783 shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700784 if (wrapS == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700785 shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700786 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n");
787 }
788 if (wrapT == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700789 shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700790 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n");
791 }
792 shader.append(" return vec2(");
793 switch (wrapS) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700794 case GL_CLAMP_TO_EDGE:
795 shader.append("texCoords.x");
796 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700797 case GL_REPEAT:
798 shader.append("mod(texCoords.x, 1.0)");
799 break;
800 case GL_MIRRORED_REPEAT:
801 shader.append("xMod2");
802 break;
803 }
804 shader.append(", ");
805 switch (wrapT) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700806 case GL_CLAMP_TO_EDGE:
807 shader.append("texCoords.y");
808 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700809 case GL_REPEAT:
810 shader.append("mod(texCoords.y, 1.0)");
811 break;
812 case GL_MIRRORED_REPEAT:
813 shader.append("yMod2");
814 break;
815 }
816 shader.append(");\n");
817 shader.append("}\n");
818}
819
Romain Guydb1938e2010-08-02 18:50:22 -0700820void ProgramCache::printLongString(const String8& shader) const {
821 ssize_t index = 0;
822 ssize_t lastIndex = 0;
823 const char* str = shader.string();
824 while ((index = shader.find("\n", index)) > -1) {
825 String8 line(str, index - lastIndex);
826 if (line.length() == 0) line.append("\n");
827 PROGRAM_LOGD("%s", line.string());
828 index++;
829 str += (index - lastIndex);
830 lastIndex = index;
831 }
832}
833
Romain Guyac670c02010-07-27 17:39:27 -0700834}; // namespace uirenderer
835}; // namespace android