blob: f536adeff3742ab92e6ee933125e44aa17c527c8 [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 =
Romain Guy39284b72012-09-26 16:39:40 -070051 "uniform mat4 projection;\n" \
Romain Guyac670c02010-07-27 17:39:27 -070052 "uniform mat4 transform;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -070053const char* gVS_Header_Uniforms_IsPoint =
Romain Guy80bbfb12011-03-23 16:56:28 -070054 "uniform mediump float pointSize;\n";
Romain Guyee916f12010-09-20 17:53:08 -070055const char* gVS_Header_Uniforms_HasGradient[3] = {
56 // Linear
Chet Haasea1d12dd2012-09-21 14:50:14 -070057 "uniform mat4 screenSpace;\n"
58 "uniform float ditherSize;\n",
Romain Guyee916f12010-09-20 17:53:08 -070059 // Circular
Chet Haasea1d12dd2012-09-21 14:50:14 -070060 "uniform mat4 screenSpace;\n"
61 "uniform float ditherSize;\n",
Romain Guyee916f12010-09-20 17:53:08 -070062 // Sweep
Romain Guyee916f12010-09-20 17:53:08 -070063 "uniform mat4 screenSpace;\n"
Chet Haasea1d12dd2012-09-21 14:50:14 -070064 "uniform float ditherSize;\n"
Romain Guyee916f12010-09-20 17:53:08 -070065};
Romain Guy889f8d12010-07-29 14:37:42 -070066const char* gVS_Header_Uniforms_HasBitmap =
67 "uniform mat4 textureTransform;\n"
Romain Guy80bbfb12011-03-23 16:56:28 -070068 "uniform mediump vec2 textureDimension;\n";
Romain Guyac670c02010-07-27 17:39:27 -070069const char* gVS_Header_Varyings_HasTexture =
70 "varying vec2 outTexCoords;\n";
Chris Craik710f46d2012-09-17 17:25:49 -070071const char* gVS_Header_Varyings_IsAALine =
Chet Haase99585ad2011-05-02 15:00:16 -070072 "varying float widthProportion;\n"
73 "varying float lengthProportion;\n";
Chris Craik710f46d2012-09-17 17:25:49 -070074const char* gVS_Header_Varyings_IsAAVertexShape =
Chris Craik6ebdc112012-08-31 18:24:33 -070075 "varying float alpha;\n";
Romain Guy63553472012-07-18 20:04:14 -070076const char* gVS_Header_Varyings_HasBitmap =
77 "varying highp vec2 outBitmapTexCoords;\n";
78const char* gVS_Header_Varyings_PointHasBitmap =
79 "varying highp vec2 outPointBitmapTexCoords;\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -070080const char* gVS_Header_Varyings_HasGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -070081 // Linear
Chet Haasea1d12dd2012-09-21 14:50:14 -070082 "varying highp vec2 linear;\n"
83 "varying vec2 ditherTexCoords;\n",
84 "varying float linear;\n"
85 "varying vec2 ditherTexCoords;\n",
Romain Guy211efea2012-07-31 21:16:07 -070086
Romain Guyee916f12010-09-20 17:53:08 -070087 // Circular
Chet Haasea1d12dd2012-09-21 14:50:14 -070088 "varying highp vec2 circular;\n"
89 "varying vec2 ditherTexCoords;\n",
90 "varying highp vec2 circular;\n"
91 "varying vec2 ditherTexCoords;\n",
Romain Guy211efea2012-07-31 21:16:07 -070092
Romain Guyee916f12010-09-20 17:53:08 -070093 // Sweep
Chet Haasea1d12dd2012-09-21 14:50:14 -070094 "varying highp vec2 sweep;\n"
95 "varying vec2 ditherTexCoords;\n",
96 "varying highp vec2 sweep;\n"
97 "varying vec2 ditherTexCoords;\n",
Romain Guyee916f12010-09-20 17:53:08 -070098};
Romain Guyac670c02010-07-27 17:39:27 -070099const char* gVS_Main =
100 "\nvoid main(void) {\n";
101const char* gVS_Main_OutTexCoords =
102 " outTexCoords = texCoords;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700103const char* gVS_Main_OutTransformedTexCoords =
104 " outTexCoords = (mainTextureTransform * vec4(texCoords, 0.0, 1.0)).xy;\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -0700105const char* gVS_Main_OutGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700106 // Linear
Chet Haasea1d12dd2012-09-21 14:50:14 -0700107 " linear = vec2((screenSpace * position).x, 0.5);\n"
Romain Guy39284b72012-09-26 16:39:40 -0700108 " ditherTexCoords = (transform * position).xy * ditherSize;\n",
Chet Haasea1d12dd2012-09-21 14:50:14 -0700109 " linear = (screenSpace * position).x;\n"
Romain Guy39284b72012-09-26 16:39:40 -0700110 " ditherTexCoords = (transform * position).xy * ditherSize;\n",
Romain Guy211efea2012-07-31 21:16:07 -0700111
Romain Guyee916f12010-09-20 17:53:08 -0700112 // Circular
Chet Haasea1d12dd2012-09-21 14:50:14 -0700113 " circular = (screenSpace * position).xy;\n"
Romain Guy39284b72012-09-26 16:39:40 -0700114 " ditherTexCoords = (transform * position).xy * ditherSize;\n",
Chet Haasea1d12dd2012-09-21 14:50:14 -0700115 " circular = (screenSpace * position).xy;\n"
Romain Guy39284b72012-09-26 16:39:40 -0700116 " ditherTexCoords = (transform * position).xy * ditherSize;\n",
Romain Guy211efea2012-07-31 21:16:07 -0700117
Romain Guyee916f12010-09-20 17:53:08 -0700118 // Sweep
Chet Haasea1d12dd2012-09-21 14:50:14 -0700119 " sweep = (screenSpace * position).xy;\n"
Romain Guy39284b72012-09-26 16:39:40 -0700120 " ditherTexCoords = (transform * position).xy * ditherSize;\n",
Chet Haasea1d12dd2012-09-21 14:50:14 -0700121 " sweep = (screenSpace * position).xy;\n"
Romain Guy39284b72012-09-26 16:39:40 -0700122 " ditherTexCoords = (transform * position).xy * ditherSize;\n",
Romain Guyee916f12010-09-20 17:53:08 -0700123};
Romain Guy889f8d12010-07-29 14:37:42 -0700124const char* gVS_Main_OutBitmapTexCoords =
Romain Guy707b2f72010-10-11 16:34:59 -0700125 " outBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -0700126const char* gVS_Main_OutPointBitmapTexCoords =
127 " outPointBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700128const char* gVS_Main_Position =
Romain Guy39284b72012-09-26 16:39:40 -0700129 " gl_Position = projection * transform * position;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -0700130const char* gVS_Main_PointSize =
131 " gl_PointSize = pointSize;\n";
Chris Craik710f46d2012-09-17 17:25:49 -0700132const char* gVS_Main_AALine =
Chet Haase99585ad2011-05-02 15:00:16 -0700133 " widthProportion = vtxWidth;\n"
134 " lengthProportion = vtxLength;\n";
Chris Craik710f46d2012-09-17 17:25:49 -0700135const char* gVS_Main_AAVertexShape =
Chris Craik6ebdc112012-08-31 18:24:33 -0700136 " alpha = vtxAlpha;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700137const char* gVS_Footer =
138 "}\n\n";
139
140///////////////////////////////////////////////////////////////////////////////
141// Fragment shaders snippets
142///////////////////////////////////////////////////////////////////////////////
143
Romain Guya5aed0d2010-09-09 14:42:43 -0700144const char* gFS_Header_Extension_FramebufferFetch =
145 "#extension GL_NV_shader_framebuffer_fetch : enable\n\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700146const char* gFS_Header_Extension_ExternalTexture =
147 "#extension GL_OES_EGL_image_external : require\n\n";
Romain Guyac670c02010-07-27 17:39:27 -0700148const char* gFS_Header =
149 "precision mediump float;\n\n";
150const char* gFS_Uniforms_Color =
151 "uniform vec4 color;\n";
Chris Craik710f46d2012-09-17 17:25:49 -0700152const char* gFS_Uniforms_AALine =
Chet Haase5b0200b2011-04-13 17:58:08 -0700153 "uniform float boundaryWidth;\n"
Chris Craika798b952012-08-27 17:03:13 -0700154 "uniform float boundaryLength;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -0700155const char* gFS_Header_Uniforms_PointHasBitmap =
156 "uniform vec2 textureDimension;\n"
157 "uniform float pointSize;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700158const char* gFS_Uniforms_TextureSampler =
Romain Guya938f562012-09-13 20:31:08 -0700159 "uniform sampler2D baseSampler;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700160const char* gFS_Uniforms_ExternalTextureSampler =
Romain Guya938f562012-09-13 20:31:08 -0700161 "uniform samplerExternalOES baseSampler;\n";
Romain Guy211efea2012-07-31 21:16:07 -0700162#define FS_UNIFORMS_DITHER \
Chet Haasea1d12dd2012-09-21 14:50:14 -0700163 "uniform float ditherSizeSquared;\n" \
Romain Guy211efea2012-07-31 21:16:07 -0700164 "uniform sampler2D ditherSampler;\n"
165#define FS_UNIFORMS_GRADIENT \
166 "uniform vec4 startColor;\n" \
167 "uniform vec4 endColor;\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700168const char* gFS_Uniforms_GradientSampler[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700169 // Linear
Romain Guy211efea2012-07-31 21:16:07 -0700170 FS_UNIFORMS_DITHER "uniform sampler2D gradientSampler;\n",
171 FS_UNIFORMS_DITHER FS_UNIFORMS_GRADIENT,
172
Romain Guyee916f12010-09-20 17:53:08 -0700173 // Circular
Romain Guy211efea2012-07-31 21:16:07 -0700174 FS_UNIFORMS_DITHER "uniform sampler2D gradientSampler;\n",
175 FS_UNIFORMS_DITHER FS_UNIFORMS_GRADIENT,
176
Romain Guyee916f12010-09-20 17:53:08 -0700177 // Sweep
Romain Guy211efea2012-07-31 21:16:07 -0700178 FS_UNIFORMS_DITHER "uniform sampler2D gradientSampler;\n",
179 FS_UNIFORMS_DITHER FS_UNIFORMS_GRADIENT
Romain Guyee916f12010-09-20 17:53:08 -0700180};
Romain Guyac670c02010-07-27 17:39:27 -0700181const char* gFS_Uniforms_BitmapSampler =
182 "uniform sampler2D bitmapSampler;\n";
183const char* gFS_Uniforms_ColorOp[4] = {
184 // None
185 "",
186 // Matrix
187 "uniform mat4 colorMatrix;\n"
188 "uniform vec4 colorMatrixVector;\n",
189 // Lighting
Romain Guydb1938e2010-08-02 18:50:22 -0700190 "uniform vec4 lightingMul;\n"
191 "uniform vec4 lightingAdd;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700192 // PorterDuff
Romain Guydb1938e2010-08-02 18:50:22 -0700193 "uniform vec4 colorBlend;\n"
Romain Guyac670c02010-07-27 17:39:27 -0700194};
Romain Guy41210632012-07-16 17:04:24 -0700195const char* gFS_Uniforms_Gamma =
196 "uniform float gamma;\n";
197
Romain Guyac670c02010-07-27 17:39:27 -0700198const char* gFS_Main =
199 "\nvoid main(void) {\n"
Romain Guy7fbcc042010-08-04 15:40:07 -0700200 " lowp vec4 fragColor;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700201
Romain Guyed6fcb02011-03-21 13:11:28 -0700202const char* gFS_Main_PointBitmapTexCoords =
Romain Guy63553472012-07-18 20:04:14 -0700203 " highp vec2 outBitmapTexCoords = outPointBitmapTexCoords + "
Romain Guyed6fcb02011-03-21 13:11:28 -0700204 "((gl_PointCoord - vec2(0.5, 0.5)) * textureDimension * vec2(pointSize, pointSize));\n";
205
Romain Guy211efea2012-07-31 21:16:07 -0700206#define FS_MAIN_DITHER \
Chet Haasea1d12dd2012-09-21 14:50:14 -0700207 "texture2D(ditherSampler, ditherTexCoords).a * ditherSizeSquared"
Romain Guy211efea2012-07-31 21:16:07 -0700208const char* gFS_Main_AddDitherToGradient =
209 " gradientColor += " FS_MAIN_DITHER ";\n";
210
Romain Guy707b2f72010-10-11 16:34:59 -0700211// Fast cases
212const char* gFS_Fast_SingleColor =
213 "\nvoid main(void) {\n"
214 " gl_FragColor = color;\n"
215 "}\n\n";
216const char* gFS_Fast_SingleTexture =
217 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700218 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700219 "}\n\n";
220const char* gFS_Fast_SingleModulateTexture =
221 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700222 " gl_FragColor = color.a * texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700223 "}\n\n";
224const char* gFS_Fast_SingleA8Texture =
225 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700226 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700227 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700228const char* gFS_Fast_SingleA8Texture_ApplyGamma =
229 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700230 " gl_FragColor = vec4(0.0, 0.0, 0.0, pow(texture2D(baseSampler, outTexCoords).a, gamma));\n"
Romain Guy41210632012-07-16 17:04:24 -0700231 "}\n\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700232const char* gFS_Fast_SingleModulateA8Texture =
233 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700234 " gl_FragColor = color * texture2D(baseSampler, outTexCoords).a;\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700235 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700236const char* gFS_Fast_SingleModulateA8Texture_ApplyGamma =
237 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700238 " gl_FragColor = color * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy41210632012-07-16 17:04:24 -0700239 "}\n\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -0700240const char* gFS_Fast_SingleGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700241 "\nvoid main(void) {\n"
Romain Guy211efea2012-07-31 21:16:07 -0700242 " gl_FragColor = " FS_MAIN_DITHER " + texture2D(gradientSampler, linear);\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700243 "}\n\n",
244 "\nvoid main(void) {\n"
Romain Guy211efea2012-07-31 21:16:07 -0700245 " gl_FragColor = " FS_MAIN_DITHER " + mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700246 "}\n\n"
247};
248const char* gFS_Fast_SingleModulateGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700249 "\nvoid main(void) {\n"
Chet Haase1c5c2062012-09-17 17:09:21 -0700250 " gl_FragColor = " FS_MAIN_DITHER " + color.a * texture2D(gradientSampler, linear);\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700251 "}\n\n",
252 "\nvoid main(void) {\n"
Chet Haase1c5c2062012-09-17 17:09:21 -0700253 " gl_FragColor = " FS_MAIN_DITHER " + color.a * mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700254 "}\n\n"
255};
Romain Guy707b2f72010-10-11 16:34:59 -0700256
257// General case
Romain Guyac670c02010-07-27 17:39:27 -0700258const char* gFS_Main_FetchColor =
259 " fragColor = color;\n";
Romain Guy740bf2b2011-04-26 15:33:10 -0700260const char* gFS_Main_ModulateColor =
261 " fragColor *= color.a;\n";
Chris Craik710f46d2012-09-17 17:25:49 -0700262const char* gFS_Main_AccountForAALine =
Chris Craika798b952012-08-27 17:03:13 -0700263 " fragColor *= (1.0 - smoothstep(boundaryWidth, 0.5, abs(0.5 - widthProportion)))\n"
264 " * (1.0 - smoothstep(boundaryLength, 0.5, abs(0.5 - lengthProportion)));\n";
Chris Craik710f46d2012-09-17 17:25:49 -0700265const char* gFS_Main_AccountForAAVertexShape =
Chris Craik6ebdc112012-08-31 18:24:33 -0700266 " fragColor *= alpha;\n";
Chris Craika798b952012-08-27 17:03:13 -0700267
Romain Guy707b2f72010-10-11 16:34:59 -0700268const char* gFS_Main_FetchTexture[2] = {
269 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700270 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700271 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700272 " fragColor = color * texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700273};
Romain Guya938f562012-09-13 20:31:08 -0700274const char* gFS_Main_FetchA8Texture[4] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700275 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700276 " fragColor = texture2D(baseSampler, outTexCoords);\n",
277 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700278 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700279 " fragColor = color * texture2D(baseSampler, outTexCoords).a;\n",
280 " fragColor = color * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700281};
Romain Guy42e1e0d2012-07-30 14:47:51 -0700282const char* gFS_Main_FetchGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700283 // Linear
Romain Guy320d46b2012-08-08 16:05:42 -0700284 " vec4 gradientColor = texture2D(gradientSampler, linear);\n",
Romain Guy211efea2012-07-31 21:16:07 -0700285
Romain Guy320d46b2012-08-08 16:05:42 -0700286 " vec4 gradientColor = mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700287
Romain Guyee916f12010-09-20 17:53:08 -0700288 // Circular
Romain Guy320d46b2012-08-08 16:05:42 -0700289 " vec4 gradientColor = texture2D(gradientSampler, vec2(length(circular), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700290
Romain Guy320d46b2012-08-08 16:05:42 -0700291 " vec4 gradientColor = mix(startColor, endColor, clamp(length(circular), 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700292
Romain Guyee916f12010-09-20 17:53:08 -0700293 // Sweep
Romain Guy63553472012-07-18 20:04:14 -0700294 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700295 " vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700296
Romain Guy42e1e0d2012-07-30 14:47:51 -0700297 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700298 " vec4 gradientColor = mix(startColor, endColor, clamp(index - floor(index), 0.0, 1.0));\n"
Romain Guyee916f12010-09-20 17:53:08 -0700299};
Romain Guyac670c02010-07-27 17:39:27 -0700300const char* gFS_Main_FetchBitmap =
301 " vec4 bitmapColor = texture2D(bitmapSampler, outBitmapTexCoords);\n";
Romain Guy889f8d12010-07-29 14:37:42 -0700302const char* gFS_Main_FetchBitmapNpot =
303 " vec4 bitmapColor = texture2D(bitmapSampler, wrap(outBitmapTexCoords));\n";
Romain Guyac670c02010-07-27 17:39:27 -0700304const char* gFS_Main_BlendShadersBG =
Romain Guyac670c02010-07-27 17:39:27 -0700305 " fragColor = blendShaders(gradientColor, bitmapColor)";
Romain Guy06f96e22010-07-30 19:18:16 -0700306const char* gFS_Main_BlendShadersGB =
307 " fragColor = blendShaders(bitmapColor, gradientColor)";
Romain Guya938f562012-09-13 20:31:08 -0700308const char* gFS_Main_BlendShaders_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700309 // Don't modulate
310 ";\n",
Romain Guya938f562012-09-13 20:31:08 -0700311 ";\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700312 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700313 " * color.a;\n",
314 " * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700315 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700316 " * texture2D(baseSampler, outTexCoords).a;\n",
317 " * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700318};
Romain Guya938f562012-09-13 20:31:08 -0700319const char* gFS_Main_GradientShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700320 // Don't modulate
321 " fragColor = gradientColor;\n",
Romain Guya938f562012-09-13 20:31:08 -0700322 " fragColor = gradientColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700323 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700324 " fragColor = gradientColor * color.a;\n",
325 " fragColor = gradientColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700326 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700327 " fragColor = gradientColor * texture2D(baseSampler, outTexCoords).a;\n",
328 " fragColor = gradientColor * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700329 };
Romain Guya938f562012-09-13 20:31:08 -0700330const char* gFS_Main_BitmapShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700331 // Don't modulate
332 " fragColor = bitmapColor;\n",
Romain Guya938f562012-09-13 20:31:08 -0700333 " fragColor = bitmapColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700334 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700335 " fragColor = bitmapColor * color.a;\n",
336 " fragColor = bitmapColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700337 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700338 " fragColor = bitmapColor * texture2D(baseSampler, outTexCoords).a;\n",
339 " fragColor = bitmapColor * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700340 };
Romain Guyac670c02010-07-27 17:39:27 -0700341const char* gFS_Main_FragColor =
342 " gl_FragColor = fragColor;\n";
Romain Guya5aed0d2010-09-09 14:42:43 -0700343const char* gFS_Main_FragColor_Blend =
344 " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n";
Romain Guyf607bdc2010-09-10 19:20:06 -0700345const char* gFS_Main_FragColor_Blend_Swap =
346 " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n";
Romain Guyac670c02010-07-27 17:39:27 -0700347const char* gFS_Main_ApplyColorOp[4] = {
348 // None
349 "",
350 // Matrix
351 " 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 Craik710f46d2012-09-17 17:25:49 -0700474 if (description.isAA) {
475 if (description.isVertexShape) {
476 shader.append(gVS_Header_Attributes_AAVertexShapeParameters);
477 } else {
478 shader.append(gVS_Header_Attributes_AALineParameters);
479 }
Chet Haase5b0200b2011-04-13 17:58:08 -0700480 }
Romain Guyac670c02010-07-27 17:39:27 -0700481 // Uniforms
482 shader.append(gVS_Header_Uniforms);
Romain Guy8f0095c2011-05-02 17:24:22 -0700483 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700484 shader.append(gVS_Header_Uniforms_TextureTransform);
485 }
Romain Guyac670c02010-07-27 17:39:27 -0700486 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700487 shader.append(gVS_Header_Uniforms_HasGradient[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700488 }
Romain Guy889f8d12010-07-29 14:37:42 -0700489 if (description.hasBitmap) {
490 shader.append(gVS_Header_Uniforms_HasBitmap);
491 }
Romain Guyed6fcb02011-03-21 13:11:28 -0700492 if (description.isPoint) {
493 shader.append(gVS_Header_Uniforms_IsPoint);
494 }
Romain Guyac670c02010-07-27 17:39:27 -0700495 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700496 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700497 shader.append(gVS_Header_Varyings_HasTexture);
498 }
Chris Craik710f46d2012-09-17 17:25:49 -0700499 if (description.isAA) {
500 if (description.isVertexShape) {
501 shader.append(gVS_Header_Varyings_IsAAVertexShape);
502 } else {
503 shader.append(gVS_Header_Varyings_IsAALine);
504 }
Chet Haase5b0200b2011-04-13 17:58:08 -0700505 }
Romain Guyac670c02010-07-27 17:39:27 -0700506 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700507 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700508 }
509 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700510 shader.append(description.isPoint ?
Romain Guy63553472012-07-18 20:04:14 -0700511 gVS_Header_Varyings_PointHasBitmap :
512 gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700513 }
514
515 // Begin the shader
516 shader.append(gVS_Main); {
Romain Guy8f0095c2011-05-02 17:24:22 -0700517 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700518 shader.append(gVS_Main_OutTransformedTexCoords);
Romain Guy8f0095c2011-05-02 17:24:22 -0700519 } else if (description.hasTexture || description.hasExternalTexture) {
520 shader.append(gVS_Main_OutTexCoords);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700521 }
Chris Craik710f46d2012-09-17 17:25:49 -0700522 if (description.isAA) {
523 if (description.isVertexShape) {
524 shader.append(gVS_Main_AAVertexShape);
525 } else {
526 shader.append(gVS_Main_AALine);
527 }
Chet Haase5b0200b2011-04-13 17:58:08 -0700528 }
Romain Guy889f8d12010-07-29 14:37:42 -0700529 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700530 shader.append(description.isPoint ?
531 gVS_Main_OutPointBitmapTexCoords :
532 gVS_Main_OutBitmapTexCoords);
533 }
534 if (description.isPoint) {
535 shader.append(gVS_Main_PointSize);
Romain Guy889f8d12010-07-29 14:37:42 -0700536 }
Romain Guyac670c02010-07-27 17:39:27 -0700537 // Output transformed position
538 shader.append(gVS_Main_Position);
Chet Haasea1d12dd2012-09-21 14:50:14 -0700539 if (description.hasGradient) {
540 shader.append(gVS_Main_OutGradient[gradientIndex(description)]);
541 }
Romain Guyac670c02010-07-27 17:39:27 -0700542 }
543 // End the shader
544 shader.append(gVS_Footer);
545
546 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string());
547
548 return shader;
549}
550
Romain Guya938f562012-09-13 20:31:08 -0700551static bool shaderOp(const ProgramDescription& description, String8& shader,
552 const int modulateOp, const char** snippets) {
553 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
554 op = op * 2 + description.hasGammaCorrection;
555 shader.append(snippets[op]);
556 return description.hasAlpha8Texture;
557}
558
Romain Guyac670c02010-07-27 17:39:27 -0700559String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) {
Romain Guya5aed0d2010-09-09 14:42:43 -0700560 String8 shader;
561
Romain Guy707b2f72010-10-11 16:34:59 -0700562 const bool blendFramebuffer = description.framebufferMode >= SkXfermode::kPlus_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700563 if (blendFramebuffer) {
564 shader.append(gFS_Header_Extension_FramebufferFetch);
565 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700566 if (description.hasExternalTexture) {
567 shader.append(gFS_Header_Extension_ExternalTexture);
568 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700569
570 shader.append(gFS_Header);
Romain Guyac670c02010-07-27 17:39:27 -0700571
572 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700573 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700574 shader.append(gVS_Header_Varyings_HasTexture);
575 }
Chris Craik710f46d2012-09-17 17:25:49 -0700576 if (description.isAA) {
577 if (description.isVertexShape) {
578 shader.append(gVS_Header_Varyings_IsAAVertexShape);
579 } else {
580 shader.append(gVS_Header_Varyings_IsAALine);
581 }
Chet Haase5b0200b2011-04-13 17:58:08 -0700582 }
Romain Guyac670c02010-07-27 17:39:27 -0700583 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700584 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700585 }
586 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700587 shader.append(description.isPoint ?
Romain Guy63553472012-07-18 20:04:14 -0700588 gVS_Header_Varyings_PointHasBitmap :
589 gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700590 }
591
Romain Guyac670c02010-07-27 17:39:27 -0700592 // Uniforms
Romain Guy707b2f72010-10-11 16:34:59 -0700593 int modulateOp = MODULATE_OP_NO_MODULATE;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700594 const bool singleColor = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700595 !description.hasGradient && !description.hasBitmap;
596
597 if (description.modulate || singleColor) {
598 shader.append(gFS_Uniforms_Color);
599 if (!singleColor) modulateOp = MODULATE_OP_MODULATE;
600 }
Romain Guyac670c02010-07-27 17:39:27 -0700601 if (description.hasTexture) {
602 shader.append(gFS_Uniforms_TextureSampler);
Romain Guy8f0095c2011-05-02 17:24:22 -0700603 } else if (description.hasExternalTexture) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700604 shader.append(gFS_Uniforms_ExternalTextureSampler);
605 }
Chris Craik710f46d2012-09-17 17:25:49 -0700606 if (description.isAA && !description.isVertexShape) {
607 shader.append(gFS_Uniforms_AALine);
Chet Haase5b0200b2011-04-13 17:58:08 -0700608 }
Romain Guyac670c02010-07-27 17:39:27 -0700609 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700610 shader.append(gFS_Uniforms_GradientSampler[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700611 }
Romain Guyed6fcb02011-03-21 13:11:28 -0700612 if (description.hasBitmap && description.isPoint) {
613 shader.append(gFS_Header_Uniforms_PointHasBitmap);
614 }
Romain Guy41210632012-07-16 17:04:24 -0700615 if (description.hasGammaCorrection) {
616 shader.append(gFS_Uniforms_Gamma);
617 }
Romain Guy707b2f72010-10-11 16:34:59 -0700618
619 // Optimization for common cases
Chet Haase99585ad2011-05-02 15:00:16 -0700620 if (!description.isAA && !blendFramebuffer &&
Chris Craik6ebdc112012-08-31 18:24:33 -0700621 description.colorOp == ProgramDescription::kColorNone &&
Chris Craik710f46d2012-09-17 17:25:49 -0700622 !description.isPoint && !description.isVertexShape) {
Romain Guy707b2f72010-10-11 16:34:59 -0700623 bool fast = false;
624
625 const bool noShader = !description.hasGradient && !description.hasBitmap;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700626 const bool singleTexture = (description.hasTexture || description.hasExternalTexture) &&
Romain Guy707b2f72010-10-11 16:34:59 -0700627 !description.hasAlpha8Texture && noShader;
628 const bool singleA8Texture = description.hasTexture &&
629 description.hasAlpha8Texture && noShader;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700630 const bool singleGradient = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700631 description.hasGradient && !description.hasBitmap &&
632 description.gradientType == ProgramDescription::kGradientLinear;
633
634 if (singleColor) {
635 shader.append(gFS_Fast_SingleColor);
636 fast = true;
637 } else if (singleTexture) {
638 if (!description.modulate) {
639 shader.append(gFS_Fast_SingleTexture);
640 } else {
641 shader.append(gFS_Fast_SingleModulateTexture);
642 }
643 fast = true;
644 } else if (singleA8Texture) {
645 if (!description.modulate) {
Romain Guy41210632012-07-16 17:04:24 -0700646 if (description.hasGammaCorrection) {
647 shader.append(gFS_Fast_SingleA8Texture_ApplyGamma);
648 } else {
649 shader.append(gFS_Fast_SingleA8Texture);
650 }
Romain Guy707b2f72010-10-11 16:34:59 -0700651 } else {
Romain Guy41210632012-07-16 17:04:24 -0700652 if (description.hasGammaCorrection) {
653 shader.append(gFS_Fast_SingleModulateA8Texture_ApplyGamma);
654 } else {
655 shader.append(gFS_Fast_SingleModulateA8Texture);
656 }
Romain Guy707b2f72010-10-11 16:34:59 -0700657 }
658 fast = true;
659 } else if (singleGradient) {
660 if (!description.modulate) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700661 shader.append(gFS_Fast_SingleGradient[description.isSimpleGradient]);
Romain Guy707b2f72010-10-11 16:34:59 -0700662 } else {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700663 shader.append(gFS_Fast_SingleModulateGradient[description.isSimpleGradient]);
Romain Guy707b2f72010-10-11 16:34:59 -0700664 }
665 fast = true;
666 }
667
668 if (fast) {
Romain Guyc15008e2010-11-10 11:59:15 -0800669#if DEBUG_PROGRAMS
Romain Guy707b2f72010-10-11 16:34:59 -0700670 PROGRAM_LOGD("*** Fast case:\n");
671 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
672 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800673#endif
Romain Guy707b2f72010-10-11 16:34:59 -0700674
675 return shader;
676 }
677 }
678
Romain Guyac670c02010-07-27 17:39:27 -0700679 if (description.hasBitmap) {
680 shader.append(gFS_Uniforms_BitmapSampler);
681 }
682 shader.append(gFS_Uniforms_ColorOp[description.colorOp]);
683
684 // Generate required functions
685 if (description.hasGradient && description.hasBitmap) {
Romain Guy48daa542010-08-10 19:21:34 -0700686 generateBlend(shader, "blendShaders", description.shadersMode);
Romain Guyac670c02010-07-27 17:39:27 -0700687 }
688 if (description.colorOp == ProgramDescription::kColorBlend) {
Romain Guy48daa542010-08-10 19:21:34 -0700689 generateBlend(shader, "blendColors", description.colorMode);
Romain Guyac670c02010-07-27 17:39:27 -0700690 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700691 if (blendFramebuffer) {
692 generateBlend(shader, "blendFramebuffer", description.framebufferMode);
693 }
Romain Guy889f8d12010-07-29 14:37:42 -0700694 if (description.isBitmapNpot) {
695 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
696 }
Romain Guyac670c02010-07-27 17:39:27 -0700697
698 // Begin the shader
699 shader.append(gFS_Main); {
700 // Stores the result in fragColor directly
Romain Guyaa6c24c2011-04-28 18:40:04 -0700701 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700702 if (description.hasAlpha8Texture) {
Romain Guy707b2f72010-10-11 16:34:59 -0700703 if (!description.hasGradient && !description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700704 shader.append(gFS_Main_FetchA8Texture[modulateOp * 2 +
705 description.hasGammaCorrection]);
Romain Guy707b2f72010-10-11 16:34:59 -0700706 }
Romain Guyac670c02010-07-27 17:39:27 -0700707 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700708 shader.append(gFS_Main_FetchTexture[modulateOp]);
Romain Guyac670c02010-07-27 17:39:27 -0700709 }
710 } else {
Romain Guya938f562012-09-13 20:31:08 -0700711 if (!description.hasGradient && !description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700712 shader.append(gFS_Main_FetchColor);
713 }
Romain Guyac670c02010-07-27 17:39:27 -0700714 }
715 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700716 shader.append(gFS_Main_FetchGradient[gradientIndex(description)]);
Romain Guy211efea2012-07-31 21:16:07 -0700717 shader.append(gFS_Main_AddDitherToGradient);
Romain Guyac670c02010-07-27 17:39:27 -0700718 }
719 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700720 if (description.isPoint) {
721 shader.append(gFS_Main_PointBitmapTexCoords);
722 }
Romain Guy889f8d12010-07-29 14:37:42 -0700723 if (!description.isBitmapNpot) {
724 shader.append(gFS_Main_FetchBitmap);
725 } else {
726 shader.append(gFS_Main_FetchBitmapNpot);
727 }
Romain Guyac670c02010-07-27 17:39:27 -0700728 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700729 bool applyModulate = false;
Romain Guyac670c02010-07-27 17:39:27 -0700730 // Case when we have two shaders set
731 if (description.hasGradient && description.hasBitmap) {
732 if (description.isBitmapFirst) {
733 shader.append(gFS_Main_BlendShadersBG);
734 } else {
735 shader.append(gFS_Main_BlendShadersGB);
736 }
Romain Guya938f562012-09-13 20:31:08 -0700737 applyModulate = shaderOp(description, shader, modulateOp,
738 gFS_Main_BlendShaders_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700739 } else {
740 if (description.hasGradient) {
Romain Guya938f562012-09-13 20:31:08 -0700741 applyModulate = shaderOp(description, shader, modulateOp,
742 gFS_Main_GradientShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700743 } else if (description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700744 applyModulate = shaderOp(description, shader, modulateOp,
745 gFS_Main_BitmapShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700746 }
Romain Guyac670c02010-07-27 17:39:27 -0700747 }
Romain Guya938f562012-09-13 20:31:08 -0700748
Romain Guy740bf2b2011-04-26 15:33:10 -0700749 if (description.modulate && applyModulate) {
Romain Guya938f562012-09-13 20:31:08 -0700750 shader.append(gFS_Main_ModulateColor);
Romain Guy740bf2b2011-04-26 15:33:10 -0700751 }
Romain Guya938f562012-09-13 20:31:08 -0700752
Romain Guyac670c02010-07-27 17:39:27 -0700753 // Apply the color op if needed
754 shader.append(gFS_Main_ApplyColorOp[description.colorOp]);
Chris Craik9f44a132012-09-13 18:34:55 -0700755
Chris Craik710f46d2012-09-17 17:25:49 -0700756 if (description.isAA) {
757 if (description.isVertexShape) {
758 shader.append(gFS_Main_AccountForAAVertexShape);
759 } else {
760 shader.append(gFS_Main_AccountForAALine);
761 }
Chris Craik9f44a132012-09-13 18:34:55 -0700762 }
763
Romain Guyac670c02010-07-27 17:39:27 -0700764 // Output the fragment
Romain Guya5aed0d2010-09-09 14:42:43 -0700765 if (!blendFramebuffer) {
766 shader.append(gFS_Main_FragColor);
767 } else {
Romain Guyf607bdc2010-09-10 19:20:06 -0700768 shader.append(!description.swapSrcDst ?
769 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap);
Romain Guya5aed0d2010-09-09 14:42:43 -0700770 }
Romain Guyac670c02010-07-27 17:39:27 -0700771 }
772 // End the shader
773 shader.append(gFS_Footer);
774
Romain Guyc15008e2010-11-10 11:59:15 -0800775#if DEBUG_PROGRAMS
Romain Guydb1938e2010-08-02 18:50:22 -0700776 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
777 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800778#endif
Romain Guydb1938e2010-08-02 18:50:22 -0700779
Romain Guyac670c02010-07-27 17:39:27 -0700780 return shader;
781}
782
Romain Guy48daa542010-08-10 19:21:34 -0700783void ProgramCache::generateBlend(String8& shader, const char* name, SkXfermode::Mode mode) {
Romain Guyac670c02010-07-27 17:39:27 -0700784 shader.append("\nvec4 ");
785 shader.append(name);
786 shader.append("(vec4 src, vec4 dst) {\n");
787 shader.append(" ");
Romain Guy48daa542010-08-10 19:21:34 -0700788 shader.append(gBlendOps[mode]);
Romain Guyac670c02010-07-27 17:39:27 -0700789 shader.append("}\n");
790}
791
Romain Guy889f8d12010-07-29 14:37:42 -0700792void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) {
Romain Guy63553472012-07-18 20:04:14 -0700793 shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700794 if (wrapS == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700795 shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700796 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n");
797 }
798 if (wrapT == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700799 shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700800 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n");
801 }
802 shader.append(" return vec2(");
803 switch (wrapS) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700804 case GL_CLAMP_TO_EDGE:
805 shader.append("texCoords.x");
806 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700807 case GL_REPEAT:
808 shader.append("mod(texCoords.x, 1.0)");
809 break;
810 case GL_MIRRORED_REPEAT:
811 shader.append("xMod2");
812 break;
813 }
814 shader.append(", ");
815 switch (wrapT) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700816 case GL_CLAMP_TO_EDGE:
817 shader.append("texCoords.y");
818 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700819 case GL_REPEAT:
820 shader.append("mod(texCoords.y, 1.0)");
821 break;
822 case GL_MIRRORED_REPEAT:
823 shader.append("yMod2");
824 break;
825 }
826 shader.append(");\n");
827 shader.append("}\n");
828}
829
Romain Guydb1938e2010-08-02 18:50:22 -0700830void ProgramCache::printLongString(const String8& shader) const {
831 ssize_t index = 0;
832 ssize_t lastIndex = 0;
833 const char* str = shader.string();
834 while ((index = shader.find("\n", index)) > -1) {
835 String8 line(str, index - lastIndex);
836 if (line.length() == 0) line.append("\n");
837 PROGRAM_LOGD("%s", line.string());
838 index++;
839 str += (index - lastIndex);
840 lastIndex = index;
841 }
842}
843
Romain Guyac670c02010-07-27 17:39:27 -0700844}; // namespace uirenderer
845}; // namespace android