blob: 7bc2b376b43b9160b5b9c5631abce98e4004f918 [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";
Chris Craik710f46d2012-09-17 17:25:49 -070043const char* gVS_Header_Attributes_AALineParameters =
Chet Haase99585ad2011-05-02 15:00:16 -070044 "attribute float vtxWidth;\n"
45 "attribute float vtxLength;\n";
Chris Craik710f46d2012-09-17 17:25:49 -070046const char* gVS_Header_Attributes_AAVertexShapeParameters =
Chris Craik6ebdc112012-08-31 18:24:33 -070047 "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";
Chris Craik710f46d2012-09-17 17:25:49 -070070const char* gVS_Header_Varyings_IsAALine =
Chet Haase99585ad2011-05-02 15:00:16 -070071 "varying float widthProportion;\n"
72 "varying float lengthProportion;\n";
Chris Craik710f46d2012-09-17 17:25:49 -070073const char* gVS_Header_Varyings_IsAAVertexShape =
Chris Craik6ebdc112012-08-31 18:24:33 -070074 "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";
Chris Craik710f46d2012-09-17 17:25:49 -0700131const char* gVS_Main_AALine =
Chet Haase99585ad2011-05-02 15:00:16 -0700132 " widthProportion = vtxWidth;\n"
133 " lengthProportion = vtxLength;\n";
Chris Craik710f46d2012-09-17 17:25:49 -0700134const char* gVS_Main_AAVertexShape =
Chris Craik6ebdc112012-08-31 18:24:33 -0700135 " 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";
Chris Craik710f46d2012-09-17 17:25:49 -0700151const char* gFS_Uniforms_AALine =
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";
Chris Craik710f46d2012-09-17 17:25:49 -0700261const char* gFS_Main_AccountForAALine =
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 Craik710f46d2012-09-17 17:25:49 -0700264const char* gFS_Main_AccountForAAVertexShape =
Chris Craik6ebdc112012-08-31 18:24:33 -0700265 " 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
350 " fragColor *= colorMatrix;\n"
Romain Guydb1938e2010-08-02 18:50:22 -0700351 " fragColor += colorMatrixVector;\n"
352 " fragColor.rgb *= fragColor.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700353 // Lighting
Romain Guydb1938e2010-08-02 18:50:22 -0700354 " float lightingAlpha = fragColor.a;\n"
355 " fragColor = min(fragColor * lightingMul + (lightingAdd * lightingAlpha), lightingAlpha);\n"
356 " fragColor.a = lightingAlpha;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700357 // PorterDuff
358 " fragColor = blendColors(colorBlend, fragColor);\n"
359};
360const char* gFS_Footer =
361 "}\n\n";
362
363///////////////////////////////////////////////////////////////////////////////
364// PorterDuff snippets
365///////////////////////////////////////////////////////////////////////////////
366
Romain Guy48daa542010-08-10 19:21:34 -0700367const char* gBlendOps[18] = {
Romain Guyac670c02010-07-27 17:39:27 -0700368 // Clear
369 "return vec4(0.0, 0.0, 0.0, 0.0);\n",
370 // Src
371 "return src;\n",
372 // Dst
373 "return dst;\n",
374 // SrcOver
Romain Guy06f96e22010-07-30 19:18:16 -0700375 "return src + dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700376 // DstOver
Romain Guy06f96e22010-07-30 19:18:16 -0700377 "return dst + src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700378 // SrcIn
Romain Guy06f96e22010-07-30 19:18:16 -0700379 "return src * dst.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700380 // DstIn
Romain Guy06f96e22010-07-30 19:18:16 -0700381 "return dst * src.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700382 // SrcOut
Romain Guy06f96e22010-07-30 19:18:16 -0700383 "return src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700384 // DstOut
Romain Guy06f96e22010-07-30 19:18:16 -0700385 "return dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700386 // SrcAtop
387 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n",
388 // DstAtop
389 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n",
390 // Xor
Romain Guy48daa542010-08-10 19:21:34 -0700391 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, "
Romain Guyac670c02010-07-27 17:39:27 -0700392 "src.a + dst.a - 2.0 * src.a * dst.a);\n",
Romain Guy48daa542010-08-10 19:21:34 -0700393 // Add
394 "return min(src + dst, 1.0);\n",
395 // Multiply
396 "return src * dst;\n",
397 // Screen
398 "return src + dst - src * dst;\n",
399 // Overlay
400 "return clamp(vec4(mix("
401 "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), "
402 "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), "
403 "step(dst.a, 2.0 * dst.rgb)), "
404 "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n",
405 // Darken
406 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
407 "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
408 // Lighten
409 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
410 "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 -0700411};
412
413///////////////////////////////////////////////////////////////////////////////
414// Constructors/destructors
415///////////////////////////////////////////////////////////////////////////////
416
417ProgramCache::ProgramCache() {
418}
419
420ProgramCache::~ProgramCache() {
421 clear();
422}
423
424///////////////////////////////////////////////////////////////////////////////
425// Cache management
426///////////////////////////////////////////////////////////////////////////////
427
428void ProgramCache::clear() {
Romain Guy67f27952010-12-07 20:09:23 -0800429 PROGRAM_LOGD("Clearing program cache");
430
Romain Guyac670c02010-07-27 17:39:27 -0700431 size_t count = mCache.size();
432 for (size_t i = 0; i < count; i++) {
433 delete mCache.valueAt(i);
434 }
435 mCache.clear();
436}
437
438Program* ProgramCache::get(const ProgramDescription& description) {
439 programid key = description.key();
440 ssize_t index = mCache.indexOfKey(key);
441 Program* program = NULL;
442 if (index < 0) {
Romain Guyee916f12010-09-20 17:53:08 -0700443 description.log("Could not find program");
Romain Guyac670c02010-07-27 17:39:27 -0700444 program = generateProgram(description, key);
445 mCache.add(key, program);
446 } else {
447 program = mCache.valueAt(index);
448 }
449 return program;
450}
451
452///////////////////////////////////////////////////////////////////////////////
453// Program generation
454///////////////////////////////////////////////////////////////////////////////
455
456Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) {
457 String8 vertexShader = generateVertexShader(description);
458 String8 fragmentShader = generateFragmentShader(description);
459
Romain Guy42e1e0d2012-07-30 14:47:51 -0700460 return new Program(description, vertexShader.string(), fragmentShader.string());
461}
462
463static inline size_t gradientIndex(const ProgramDescription& description) {
464 return description.gradientType * 2 + description.isSimpleGradient;
Romain Guyac670c02010-07-27 17:39:27 -0700465}
466
467String8 ProgramCache::generateVertexShader(const ProgramDescription& description) {
468 // Add attributes
469 String8 shader(gVS_Header_Attributes);
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_Attributes_TexCoords);
472 }
Chris Craik710f46d2012-09-17 17:25:49 -0700473 if (description.isAA) {
474 if (description.isVertexShape) {
475 shader.append(gVS_Header_Attributes_AAVertexShapeParameters);
476 } else {
477 shader.append(gVS_Header_Attributes_AALineParameters);
478 }
Chet Haase5b0200b2011-04-13 17:58:08 -0700479 }
Romain Guyac670c02010-07-27 17:39:27 -0700480 // Uniforms
481 shader.append(gVS_Header_Uniforms);
Romain Guy8f0095c2011-05-02 17:24:22 -0700482 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700483 shader.append(gVS_Header_Uniforms_TextureTransform);
484 }
Romain Guyac670c02010-07-27 17:39:27 -0700485 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700486 shader.append(gVS_Header_Uniforms_HasGradient[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700487 }
Romain Guy889f8d12010-07-29 14:37:42 -0700488 if (description.hasBitmap) {
489 shader.append(gVS_Header_Uniforms_HasBitmap);
490 }
Romain Guyed6fcb02011-03-21 13:11:28 -0700491 if (description.isPoint) {
492 shader.append(gVS_Header_Uniforms_IsPoint);
493 }
Romain Guyac670c02010-07-27 17:39:27 -0700494 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700495 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700496 shader.append(gVS_Header_Varyings_HasTexture);
497 }
Chris Craik710f46d2012-09-17 17:25:49 -0700498 if (description.isAA) {
499 if (description.isVertexShape) {
500 shader.append(gVS_Header_Varyings_IsAAVertexShape);
501 } else {
502 shader.append(gVS_Header_Varyings_IsAALine);
503 }
Chet Haase5b0200b2011-04-13 17:58:08 -0700504 }
Romain Guyac670c02010-07-27 17:39:27 -0700505 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700506 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700507 }
508 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700509 shader.append(description.isPoint ?
Romain Guy63553472012-07-18 20:04:14 -0700510 gVS_Header_Varyings_PointHasBitmap :
511 gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700512 }
513
514 // Begin the shader
515 shader.append(gVS_Main); {
Romain Guy8f0095c2011-05-02 17:24:22 -0700516 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700517 shader.append(gVS_Main_OutTransformedTexCoords);
Romain Guy8f0095c2011-05-02 17:24:22 -0700518 } else if (description.hasTexture || description.hasExternalTexture) {
519 shader.append(gVS_Main_OutTexCoords);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700520 }
Chris Craik710f46d2012-09-17 17:25:49 -0700521 if (description.isAA) {
522 if (description.isVertexShape) {
523 shader.append(gVS_Main_AAVertexShape);
524 } else {
525 shader.append(gVS_Main_AALine);
526 }
Chet Haase5b0200b2011-04-13 17:58:08 -0700527 }
Romain Guy889f8d12010-07-29 14:37:42 -0700528 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700529 shader.append(description.isPoint ?
530 gVS_Main_OutPointBitmapTexCoords :
531 gVS_Main_OutBitmapTexCoords);
532 }
533 if (description.isPoint) {
534 shader.append(gVS_Main_PointSize);
Romain Guy889f8d12010-07-29 14:37:42 -0700535 }
Romain Guyac670c02010-07-27 17:39:27 -0700536 // Output transformed position
537 shader.append(gVS_Main_Position);
Chet Haasea1d12dd2012-09-21 14:50:14 -0700538 if (description.hasGradient) {
539 shader.append(gVS_Main_OutGradient[gradientIndex(description)]);
540 }
Romain Guyac670c02010-07-27 17:39:27 -0700541 }
542 // End the shader
543 shader.append(gVS_Footer);
544
545 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string());
546
547 return shader;
548}
549
Romain Guya938f562012-09-13 20:31:08 -0700550static bool shaderOp(const ProgramDescription& description, String8& shader,
551 const int modulateOp, const char** snippets) {
552 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
553 op = op * 2 + description.hasGammaCorrection;
554 shader.append(snippets[op]);
555 return description.hasAlpha8Texture;
556}
557
Romain Guyac670c02010-07-27 17:39:27 -0700558String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) {
Romain Guya5aed0d2010-09-09 14:42:43 -0700559 String8 shader;
560
Romain Guy707b2f72010-10-11 16:34:59 -0700561 const bool blendFramebuffer = description.framebufferMode >= SkXfermode::kPlus_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700562 if (blendFramebuffer) {
563 shader.append(gFS_Header_Extension_FramebufferFetch);
564 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700565 if (description.hasExternalTexture) {
566 shader.append(gFS_Header_Extension_ExternalTexture);
567 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700568
569 shader.append(gFS_Header);
Romain Guyac670c02010-07-27 17:39:27 -0700570
571 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700572 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700573 shader.append(gVS_Header_Varyings_HasTexture);
574 }
Chris Craik710f46d2012-09-17 17:25:49 -0700575 if (description.isAA) {
576 if (description.isVertexShape) {
577 shader.append(gVS_Header_Varyings_IsAAVertexShape);
578 } else {
579 shader.append(gVS_Header_Varyings_IsAALine);
580 }
Chet Haase5b0200b2011-04-13 17:58:08 -0700581 }
Romain Guyac670c02010-07-27 17:39:27 -0700582 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700583 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700584 }
585 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700586 shader.append(description.isPoint ?
Romain Guy63553472012-07-18 20:04:14 -0700587 gVS_Header_Varyings_PointHasBitmap :
588 gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700589 }
590
Romain Guyac670c02010-07-27 17:39:27 -0700591 // Uniforms
Romain Guy707b2f72010-10-11 16:34:59 -0700592 int modulateOp = MODULATE_OP_NO_MODULATE;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700593 const bool singleColor = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700594 !description.hasGradient && !description.hasBitmap;
595
596 if (description.modulate || singleColor) {
597 shader.append(gFS_Uniforms_Color);
598 if (!singleColor) modulateOp = MODULATE_OP_MODULATE;
599 }
Romain Guyac670c02010-07-27 17:39:27 -0700600 if (description.hasTexture) {
601 shader.append(gFS_Uniforms_TextureSampler);
Romain Guy8f0095c2011-05-02 17:24:22 -0700602 } else if (description.hasExternalTexture) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700603 shader.append(gFS_Uniforms_ExternalTextureSampler);
604 }
Chris Craik710f46d2012-09-17 17:25:49 -0700605 if (description.isAA && !description.isVertexShape) {
606 shader.append(gFS_Uniforms_AALine);
Chet Haase5b0200b2011-04-13 17:58:08 -0700607 }
Romain Guyac670c02010-07-27 17:39:27 -0700608 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700609 shader.append(gFS_Uniforms_GradientSampler[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700610 }
Romain Guyed6fcb02011-03-21 13:11:28 -0700611 if (description.hasBitmap && description.isPoint) {
612 shader.append(gFS_Header_Uniforms_PointHasBitmap);
613 }
Romain Guy41210632012-07-16 17:04:24 -0700614 if (description.hasGammaCorrection) {
615 shader.append(gFS_Uniforms_Gamma);
616 }
Romain Guy707b2f72010-10-11 16:34:59 -0700617
618 // Optimization for common cases
Chet Haase99585ad2011-05-02 15:00:16 -0700619 if (!description.isAA && !blendFramebuffer &&
Chris Craik6ebdc112012-08-31 18:24:33 -0700620 description.colorOp == ProgramDescription::kColorNone &&
Chris Craik710f46d2012-09-17 17:25:49 -0700621 !description.isPoint && !description.isVertexShape) {
Romain Guy707b2f72010-10-11 16:34:59 -0700622 bool fast = false;
623
624 const bool noShader = !description.hasGradient && !description.hasBitmap;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700625 const bool singleTexture = (description.hasTexture || description.hasExternalTexture) &&
Romain Guy707b2f72010-10-11 16:34:59 -0700626 !description.hasAlpha8Texture && noShader;
627 const bool singleA8Texture = description.hasTexture &&
628 description.hasAlpha8Texture && noShader;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700629 const bool singleGradient = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700630 description.hasGradient && !description.hasBitmap &&
631 description.gradientType == ProgramDescription::kGradientLinear;
632
633 if (singleColor) {
634 shader.append(gFS_Fast_SingleColor);
635 fast = true;
636 } else if (singleTexture) {
637 if (!description.modulate) {
638 shader.append(gFS_Fast_SingleTexture);
639 } else {
640 shader.append(gFS_Fast_SingleModulateTexture);
641 }
642 fast = true;
643 } else if (singleA8Texture) {
644 if (!description.modulate) {
Romain Guy41210632012-07-16 17:04:24 -0700645 if (description.hasGammaCorrection) {
646 shader.append(gFS_Fast_SingleA8Texture_ApplyGamma);
647 } else {
648 shader.append(gFS_Fast_SingleA8Texture);
649 }
Romain Guy707b2f72010-10-11 16:34:59 -0700650 } else {
Romain Guy41210632012-07-16 17:04:24 -0700651 if (description.hasGammaCorrection) {
652 shader.append(gFS_Fast_SingleModulateA8Texture_ApplyGamma);
653 } else {
654 shader.append(gFS_Fast_SingleModulateA8Texture);
655 }
Romain Guy707b2f72010-10-11 16:34:59 -0700656 }
657 fast = true;
658 } else if (singleGradient) {
659 if (!description.modulate) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700660 shader.append(gFS_Fast_SingleGradient[description.isSimpleGradient]);
Romain Guy707b2f72010-10-11 16:34:59 -0700661 } else {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700662 shader.append(gFS_Fast_SingleModulateGradient[description.isSimpleGradient]);
Romain Guy707b2f72010-10-11 16:34:59 -0700663 }
664 fast = true;
665 }
666
667 if (fast) {
Romain Guyc15008e2010-11-10 11:59:15 -0800668#if DEBUG_PROGRAMS
Romain Guy707b2f72010-10-11 16:34:59 -0700669 PROGRAM_LOGD("*** Fast case:\n");
670 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
671 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800672#endif
Romain Guy707b2f72010-10-11 16:34:59 -0700673
674 return shader;
675 }
676 }
677
Romain Guyac670c02010-07-27 17:39:27 -0700678 if (description.hasBitmap) {
679 shader.append(gFS_Uniforms_BitmapSampler);
680 }
681 shader.append(gFS_Uniforms_ColorOp[description.colorOp]);
682
683 // Generate required functions
684 if (description.hasGradient && description.hasBitmap) {
Romain Guy48daa542010-08-10 19:21:34 -0700685 generateBlend(shader, "blendShaders", description.shadersMode);
Romain Guyac670c02010-07-27 17:39:27 -0700686 }
687 if (description.colorOp == ProgramDescription::kColorBlend) {
Romain Guy48daa542010-08-10 19:21:34 -0700688 generateBlend(shader, "blendColors", description.colorMode);
Romain Guyac670c02010-07-27 17:39:27 -0700689 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700690 if (blendFramebuffer) {
691 generateBlend(shader, "blendFramebuffer", description.framebufferMode);
692 }
Romain Guy889f8d12010-07-29 14:37:42 -0700693 if (description.isBitmapNpot) {
694 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
695 }
Romain Guyac670c02010-07-27 17:39:27 -0700696
697 // Begin the shader
698 shader.append(gFS_Main); {
699 // Stores the result in fragColor directly
Romain Guyaa6c24c2011-04-28 18:40:04 -0700700 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700701 if (description.hasAlpha8Texture) {
Romain Guy707b2f72010-10-11 16:34:59 -0700702 if (!description.hasGradient && !description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700703 shader.append(gFS_Main_FetchA8Texture[modulateOp * 2 +
704 description.hasGammaCorrection]);
Romain Guy707b2f72010-10-11 16:34:59 -0700705 }
Romain Guyac670c02010-07-27 17:39:27 -0700706 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700707 shader.append(gFS_Main_FetchTexture[modulateOp]);
Romain Guyac670c02010-07-27 17:39:27 -0700708 }
709 } else {
Romain Guya938f562012-09-13 20:31:08 -0700710 if (!description.hasGradient && !description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700711 shader.append(gFS_Main_FetchColor);
712 }
Romain Guyac670c02010-07-27 17:39:27 -0700713 }
714 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700715 shader.append(gFS_Main_FetchGradient[gradientIndex(description)]);
Romain Guy211efea2012-07-31 21:16:07 -0700716 shader.append(gFS_Main_AddDitherToGradient);
Romain Guyac670c02010-07-27 17:39:27 -0700717 }
718 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700719 if (description.isPoint) {
720 shader.append(gFS_Main_PointBitmapTexCoords);
721 }
Romain Guy889f8d12010-07-29 14:37:42 -0700722 if (!description.isBitmapNpot) {
723 shader.append(gFS_Main_FetchBitmap);
724 } else {
725 shader.append(gFS_Main_FetchBitmapNpot);
726 }
Romain Guyac670c02010-07-27 17:39:27 -0700727 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700728 bool applyModulate = false;
Romain Guyac670c02010-07-27 17:39:27 -0700729 // Case when we have two shaders set
730 if (description.hasGradient && description.hasBitmap) {
731 if (description.isBitmapFirst) {
732 shader.append(gFS_Main_BlendShadersBG);
733 } else {
734 shader.append(gFS_Main_BlendShadersGB);
735 }
Romain Guya938f562012-09-13 20:31:08 -0700736 applyModulate = shaderOp(description, shader, modulateOp,
737 gFS_Main_BlendShaders_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700738 } else {
739 if (description.hasGradient) {
Romain Guya938f562012-09-13 20:31:08 -0700740 applyModulate = shaderOp(description, shader, modulateOp,
741 gFS_Main_GradientShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700742 } else if (description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700743 applyModulate = shaderOp(description, shader, modulateOp,
744 gFS_Main_BitmapShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700745 }
Romain Guyac670c02010-07-27 17:39:27 -0700746 }
Romain Guya938f562012-09-13 20:31:08 -0700747
Romain Guy740bf2b2011-04-26 15:33:10 -0700748 if (description.modulate && applyModulate) {
Romain Guya938f562012-09-13 20:31:08 -0700749 shader.append(gFS_Main_ModulateColor);
Romain Guy740bf2b2011-04-26 15:33:10 -0700750 }
Romain Guya938f562012-09-13 20:31:08 -0700751
Romain Guyac670c02010-07-27 17:39:27 -0700752 // Apply the color op if needed
753 shader.append(gFS_Main_ApplyColorOp[description.colorOp]);
Chris Craik9f44a132012-09-13 18:34:55 -0700754
Chris Craik710f46d2012-09-17 17:25:49 -0700755 if (description.isAA) {
756 if (description.isVertexShape) {
757 shader.append(gFS_Main_AccountForAAVertexShape);
758 } else {
759 shader.append(gFS_Main_AccountForAALine);
760 }
Chris Craik9f44a132012-09-13 18:34:55 -0700761 }
762
Romain Guyac670c02010-07-27 17:39:27 -0700763 // Output the fragment
Romain Guya5aed0d2010-09-09 14:42:43 -0700764 if (!blendFramebuffer) {
765 shader.append(gFS_Main_FragColor);
766 } else {
Romain Guyf607bdc2010-09-10 19:20:06 -0700767 shader.append(!description.swapSrcDst ?
768 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap);
Romain Guya5aed0d2010-09-09 14:42:43 -0700769 }
Romain Guyac670c02010-07-27 17:39:27 -0700770 }
771 // End the shader
772 shader.append(gFS_Footer);
773
Romain Guyc15008e2010-11-10 11:59:15 -0800774#if DEBUG_PROGRAMS
Romain Guydb1938e2010-08-02 18:50:22 -0700775 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
776 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800777#endif
Romain Guydb1938e2010-08-02 18:50:22 -0700778
Romain Guyac670c02010-07-27 17:39:27 -0700779 return shader;
780}
781
Romain Guy48daa542010-08-10 19:21:34 -0700782void ProgramCache::generateBlend(String8& shader, const char* name, SkXfermode::Mode mode) {
Romain Guyac670c02010-07-27 17:39:27 -0700783 shader.append("\nvec4 ");
784 shader.append(name);
785 shader.append("(vec4 src, vec4 dst) {\n");
786 shader.append(" ");
Romain Guy48daa542010-08-10 19:21:34 -0700787 shader.append(gBlendOps[mode]);
Romain Guyac670c02010-07-27 17:39:27 -0700788 shader.append("}\n");
789}
790
Romain Guy889f8d12010-07-29 14:37:42 -0700791void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) {
Romain Guy63553472012-07-18 20:04:14 -0700792 shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700793 if (wrapS == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700794 shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700795 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n");
796 }
797 if (wrapT == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700798 shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700799 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n");
800 }
801 shader.append(" return vec2(");
802 switch (wrapS) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700803 case GL_CLAMP_TO_EDGE:
804 shader.append("texCoords.x");
805 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700806 case GL_REPEAT:
807 shader.append("mod(texCoords.x, 1.0)");
808 break;
809 case GL_MIRRORED_REPEAT:
810 shader.append("xMod2");
811 break;
812 }
813 shader.append(", ");
814 switch (wrapT) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700815 case GL_CLAMP_TO_EDGE:
816 shader.append("texCoords.y");
817 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700818 case GL_REPEAT:
819 shader.append("mod(texCoords.y, 1.0)");
820 break;
821 case GL_MIRRORED_REPEAT:
822 shader.append("yMod2");
823 break;
824 }
825 shader.append(");\n");
826 shader.append("}\n");
827}
828
Romain Guydb1938e2010-08-02 18:50:22 -0700829void ProgramCache::printLongString(const String8& shader) const {
830 ssize_t index = 0;
831 ssize_t lastIndex = 0;
832 const char* str = shader.string();
833 while ((index = shader.find("\n", index)) > -1) {
834 String8 line(str, index - lastIndex);
835 if (line.length() == 0) line.append("\n");
836 PROGRAM_LOGD("%s", line.string());
837 index++;
838 str += (index - lastIndex);
839 lastIndex = index;
840 }
841}
842
Romain Guyac670c02010-07-27 17:39:27 -0700843}; // namespace uirenderer
844}; // namespace android