blob: 8eb85e5547eb33e504cf7e61e145cc6cb3ed7f91 [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 Guyb4880042013-04-05 11:17:55 -070022#include "Dither.h"
Romain Guyac670c02010-07-27 17:39:27 -070023#include "ProgramCache.h"
24
25namespace android {
26namespace uirenderer {
27
28///////////////////////////////////////////////////////////////////////////////
Romain Guy707b2f72010-10-11 16:34:59 -070029// Defines
30///////////////////////////////////////////////////////////////////////////////
31
32#define MODULATE_OP_NO_MODULATE 0
33#define MODULATE_OP_MODULATE 1
34#define MODULATE_OP_MODULATE_A8 2
35
Romain Guyb4880042013-04-05 11:17:55 -070036#define STR(x) STR1(x)
37#define STR1(x) #x
38
Romain Guy707b2f72010-10-11 16:34:59 -070039///////////////////////////////////////////////////////////////////////////////
Romain Guyac670c02010-07-27 17:39:27 -070040// Vertex shaders snippets
41///////////////////////////////////////////////////////////////////////////////
42
Romain Guyac670c02010-07-27 17:39:27 -070043const char* gVS_Header_Attributes =
44 "attribute vec4 position;\n";
45const char* gVS_Header_Attributes_TexCoords =
46 "attribute vec2 texCoords;\n";
Romain Guyff316ec2013-02-13 18:39:43 -080047const char* gVS_Header_Attributes_Colors =
48 "attribute vec4 colors;\n";
Chris Craik710f46d2012-09-17 17:25:49 -070049const char* gVS_Header_Attributes_AAVertexShapeParameters =
Chris Craik6ebdc112012-08-31 18:24:33 -070050 "attribute float vtxAlpha;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -070051const char* gVS_Header_Uniforms_TextureTransform =
52 "uniform mat4 mainTextureTransform;\n";
Romain Guyac670c02010-07-27 17:39:27 -070053const char* gVS_Header_Uniforms =
Romain Guy39284b72012-09-26 16:39:40 -070054 "uniform mat4 projection;\n" \
Romain Guyac670c02010-07-27 17:39:27 -070055 "uniform mat4 transform;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -070056const char* gVS_Header_Uniforms_IsPoint =
Romain Guy80bbfb12011-03-23 16:56:28 -070057 "uniform mediump float pointSize;\n";
Romain Guyb4880042013-04-05 11:17:55 -070058const char* gVS_Header_Uniforms_HasGradient =
59 "uniform mat4 screenSpace;\n";
Romain Guy889f8d12010-07-29 14:37:42 -070060const char* gVS_Header_Uniforms_HasBitmap =
61 "uniform mat4 textureTransform;\n"
Romain Guy80bbfb12011-03-23 16:56:28 -070062 "uniform mediump vec2 textureDimension;\n";
Romain Guyac670c02010-07-27 17:39:27 -070063const char* gVS_Header_Varyings_HasTexture =
64 "varying vec2 outTexCoords;\n";
Romain Guyff316ec2013-02-13 18:39:43 -080065const char* gVS_Header_Varyings_HasColors =
66 "varying vec4 outColors;\n";
Chris Craik710f46d2012-09-17 17:25:49 -070067const char* gVS_Header_Varyings_IsAAVertexShape =
Chris Craik6ebdc112012-08-31 18:24:33 -070068 "varying float alpha;\n";
Romain Guy63553472012-07-18 20:04:14 -070069const char* gVS_Header_Varyings_HasBitmap =
70 "varying highp vec2 outBitmapTexCoords;\n";
71const char* gVS_Header_Varyings_PointHasBitmap =
72 "varying highp vec2 outPointBitmapTexCoords;\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -070073const char* gVS_Header_Varyings_HasGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -070074 // Linear
Chet Haasea1d12dd2012-09-21 14:50:14 -070075 "varying highp vec2 linear;\n"
76 "varying vec2 ditherTexCoords;\n",
77 "varying float linear;\n"
78 "varying vec2 ditherTexCoords;\n",
Romain Guy211efea2012-07-31 21:16:07 -070079
Romain Guyee916f12010-09-20 17:53:08 -070080 // Circular
Chet Haasea1d12dd2012-09-21 14:50:14 -070081 "varying highp vec2 circular;\n"
82 "varying vec2 ditherTexCoords;\n",
83 "varying highp vec2 circular;\n"
84 "varying vec2 ditherTexCoords;\n",
Romain Guy211efea2012-07-31 21:16:07 -070085
Romain Guyee916f12010-09-20 17:53:08 -070086 // Sweep
Chet Haasea1d12dd2012-09-21 14:50:14 -070087 "varying highp vec2 sweep;\n"
88 "varying vec2 ditherTexCoords;\n",
89 "varying highp vec2 sweep;\n"
90 "varying vec2 ditherTexCoords;\n",
Romain Guyee916f12010-09-20 17:53:08 -070091};
Romain Guyac670c02010-07-27 17:39:27 -070092const char* gVS_Main =
93 "\nvoid main(void) {\n";
94const char* gVS_Main_OutTexCoords =
95 " outTexCoords = texCoords;\n";
Romain Guyff316ec2013-02-13 18:39:43 -080096const char* gVS_Main_OutColors =
97 " outColors = colors;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -070098const char* gVS_Main_OutTransformedTexCoords =
99 " outTexCoords = (mainTextureTransform * vec4(texCoords, 0.0, 1.0)).xy;\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -0700100const char* gVS_Main_OutGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700101 // Linear
Chet Haasea1d12dd2012-09-21 14:50:14 -0700102 " linear = vec2((screenSpace * position).x, 0.5);\n"
Romain Guyb4880042013-04-05 11:17:55 -0700103 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Chet Haasea1d12dd2012-09-21 14:50:14 -0700104 " linear = (screenSpace * position).x;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700105 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Romain Guy211efea2012-07-31 21:16:07 -0700106
Romain Guyee916f12010-09-20 17:53:08 -0700107 // Circular
Chet Haasea1d12dd2012-09-21 14:50:14 -0700108 " circular = (screenSpace * position).xy;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700109 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Chet Haasea1d12dd2012-09-21 14:50:14 -0700110 " circular = (screenSpace * position).xy;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700111 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Romain Guy211efea2012-07-31 21:16:07 -0700112
Romain Guyee916f12010-09-20 17:53:08 -0700113 // Sweep
Chet Haasea1d12dd2012-09-21 14:50:14 -0700114 " sweep = (screenSpace * position).xy;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700115 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Chet Haasea1d12dd2012-09-21 14:50:14 -0700116 " sweep = (screenSpace * position).xy;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700117 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Romain Guyee916f12010-09-20 17:53:08 -0700118};
Romain Guy889f8d12010-07-29 14:37:42 -0700119const char* gVS_Main_OutBitmapTexCoords =
Romain Guy707b2f72010-10-11 16:34:59 -0700120 " outBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -0700121const char* gVS_Main_OutPointBitmapTexCoords =
122 " outPointBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700123const char* gVS_Main_Position =
Romain Guy39284b72012-09-26 16:39:40 -0700124 " gl_Position = projection * transform * position;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -0700125const char* gVS_Main_PointSize =
126 " gl_PointSize = pointSize;\n";
Chris Craik710f46d2012-09-17 17:25:49 -0700127const char* gVS_Main_AAVertexShape =
Chris Craik6ebdc112012-08-31 18:24:33 -0700128 " alpha = vtxAlpha;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700129const char* gVS_Footer =
130 "}\n\n";
131
132///////////////////////////////////////////////////////////////////////////////
133// Fragment shaders snippets
134///////////////////////////////////////////////////////////////////////////////
135
Romain Guya5aed0d2010-09-09 14:42:43 -0700136const char* gFS_Header_Extension_FramebufferFetch =
137 "#extension GL_NV_shader_framebuffer_fetch : enable\n\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700138const char* gFS_Header_Extension_ExternalTexture =
139 "#extension GL_OES_EGL_image_external : require\n\n";
Romain Guyac670c02010-07-27 17:39:27 -0700140const char* gFS_Header =
141 "precision mediump float;\n\n";
142const char* gFS_Uniforms_Color =
143 "uniform vec4 color;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -0700144const char* gFS_Header_Uniforms_PointHasBitmap =
145 "uniform vec2 textureDimension;\n"
146 "uniform float pointSize;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700147const char* gFS_Uniforms_TextureSampler =
Romain Guya938f562012-09-13 20:31:08 -0700148 "uniform sampler2D baseSampler;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700149const char* gFS_Uniforms_ExternalTextureSampler =
Romain Guya938f562012-09-13 20:31:08 -0700150 "uniform samplerExternalOES baseSampler;\n";
Romain Guyb4880042013-04-05 11:17:55 -0700151const char* gFS_Uniforms_Dither =
152 "uniform sampler2D ditherSampler;";
153const char* gFS_Uniforms_GradientSampler[2] = {
154 "%s\n"
155 "uniform sampler2D gradientSampler;\n",
156 "%s\n"
157 "uniform vec4 startColor;\n"
Romain Guy211efea2012-07-31 21:16:07 -0700158 "uniform vec4 endColor;\n"
Romain Guyee916f12010-09-20 17:53:08 -0700159};
Romain Guyac670c02010-07-27 17:39:27 -0700160const char* gFS_Uniforms_BitmapSampler =
161 "uniform sampler2D bitmapSampler;\n";
162const char* gFS_Uniforms_ColorOp[4] = {
163 // None
164 "",
165 // Matrix
166 "uniform mat4 colorMatrix;\n"
167 "uniform vec4 colorMatrixVector;\n",
168 // Lighting
Romain Guydb1938e2010-08-02 18:50:22 -0700169 "uniform vec4 lightingMul;\n"
170 "uniform vec4 lightingAdd;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700171 // PorterDuff
Romain Guydb1938e2010-08-02 18:50:22 -0700172 "uniform vec4 colorBlend;\n"
Romain Guyac670c02010-07-27 17:39:27 -0700173};
Romain Guy41210632012-07-16 17:04:24 -0700174const char* gFS_Uniforms_Gamma =
175 "uniform float gamma;\n";
176
Romain Guyac670c02010-07-27 17:39:27 -0700177const char* gFS_Main =
178 "\nvoid main(void) {\n"
Romain Guy7fbcc042010-08-04 15:40:07 -0700179 " lowp vec4 fragColor;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700180
Romain Guyed6fcb02011-03-21 13:11:28 -0700181const char* gFS_Main_PointBitmapTexCoords =
Romain Guy63553472012-07-18 20:04:14 -0700182 " highp vec2 outBitmapTexCoords = outPointBitmapTexCoords + "
Romain Guyed6fcb02011-03-21 13:11:28 -0700183 "((gl_PointCoord - vec2(0.5, 0.5)) * textureDimension * vec2(pointSize, pointSize));\n";
184
Romain Guyb4880042013-04-05 11:17:55 -0700185const char* gFS_Main_Dither[2] = {
186 // ES 2.0
187 "texture2D(ditherSampler, ditherTexCoords).a * " STR(DITHER_KERNEL_SIZE_INV_SQUARE),
188 // ES 3.0
Romain Guy032d47a2013-04-08 19:45:40 -0700189 "texture2D(ditherSampler, ditherTexCoords).a"
Romain Guyb4880042013-04-05 11:17:55 -0700190};
Romain Guy211efea2012-07-31 21:16:07 -0700191const char* gFS_Main_AddDitherToGradient =
Romain Guyb4880042013-04-05 11:17:55 -0700192 " gradientColor += %s;\n";
Romain Guy211efea2012-07-31 21:16:07 -0700193
Romain Guy707b2f72010-10-11 16:34:59 -0700194// Fast cases
195const char* gFS_Fast_SingleColor =
196 "\nvoid main(void) {\n"
197 " gl_FragColor = color;\n"
198 "}\n\n";
199const char* gFS_Fast_SingleTexture =
200 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700201 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700202 "}\n\n";
203const char* gFS_Fast_SingleModulateTexture =
204 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700205 " gl_FragColor = color.a * texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700206 "}\n\n";
207const char* gFS_Fast_SingleA8Texture =
208 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700209 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700210 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700211const char* gFS_Fast_SingleA8Texture_ApplyGamma =
212 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700213 " gl_FragColor = vec4(0.0, 0.0, 0.0, pow(texture2D(baseSampler, outTexCoords).a, gamma));\n"
Romain Guy41210632012-07-16 17:04:24 -0700214 "}\n\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700215const char* gFS_Fast_SingleModulateA8Texture =
216 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700217 " gl_FragColor = color * texture2D(baseSampler, outTexCoords).a;\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700218 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700219const char* gFS_Fast_SingleModulateA8Texture_ApplyGamma =
220 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700221 " gl_FragColor = color * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy41210632012-07-16 17:04:24 -0700222 "}\n\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -0700223const char* gFS_Fast_SingleGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700224 "\nvoid main(void) {\n"
Romain Guyb4880042013-04-05 11:17:55 -0700225 " gl_FragColor = %s + texture2D(gradientSampler, linear);\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700226 "}\n\n",
227 "\nvoid main(void) {\n"
Romain Guyb4880042013-04-05 11:17:55 -0700228 " gl_FragColor = %s + mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
229 "}\n\n",
Romain Guy42e1e0d2012-07-30 14:47:51 -0700230};
231const char* gFS_Fast_SingleModulateGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700232 "\nvoid main(void) {\n"
Romain Guyb4880042013-04-05 11:17:55 -0700233 " gl_FragColor = %s + color.a * texture2D(gradientSampler, linear);\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700234 "}\n\n",
235 "\nvoid main(void) {\n"
Romain Guyb4880042013-04-05 11:17:55 -0700236 " gl_FragColor = %s + color.a * mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700237 "}\n\n"
238};
Romain Guy707b2f72010-10-11 16:34:59 -0700239
240// General case
Romain Guyac670c02010-07-27 17:39:27 -0700241const char* gFS_Main_FetchColor =
242 " fragColor = color;\n";
Romain Guy740bf2b2011-04-26 15:33:10 -0700243const char* gFS_Main_ModulateColor =
244 " fragColor *= color.a;\n";
Chris Craik710f46d2012-09-17 17:25:49 -0700245const char* gFS_Main_AccountForAAVertexShape =
Chris Craik6ebdc112012-08-31 18:24:33 -0700246 " fragColor *= alpha;\n";
Chris Craika798b952012-08-27 17:03:13 -0700247
Romain Guy707b2f72010-10-11 16:34:59 -0700248const char* gFS_Main_FetchTexture[2] = {
249 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700250 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700251 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700252 " fragColor = color * texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700253};
Romain Guya938f562012-09-13 20:31:08 -0700254const char* gFS_Main_FetchA8Texture[4] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700255 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700256 " fragColor = texture2D(baseSampler, outTexCoords);\n",
257 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700258 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700259 " fragColor = color * texture2D(baseSampler, outTexCoords).a;\n",
260 " fragColor = color * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700261};
Romain Guy42e1e0d2012-07-30 14:47:51 -0700262const char* gFS_Main_FetchGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700263 // Linear
Romain Guy320d46b2012-08-08 16:05:42 -0700264 " vec4 gradientColor = texture2D(gradientSampler, linear);\n",
Romain Guy211efea2012-07-31 21:16:07 -0700265
Romain Guy320d46b2012-08-08 16:05:42 -0700266 " vec4 gradientColor = mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700267
Romain Guyee916f12010-09-20 17:53:08 -0700268 // Circular
Romain Guy320d46b2012-08-08 16:05:42 -0700269 " vec4 gradientColor = texture2D(gradientSampler, vec2(length(circular), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700270
Romain Guy320d46b2012-08-08 16:05:42 -0700271 " vec4 gradientColor = mix(startColor, endColor, clamp(length(circular), 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700272
Romain Guyee916f12010-09-20 17:53:08 -0700273 // Sweep
Romain Guy63553472012-07-18 20:04:14 -0700274 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700275 " vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700276
Romain Guy42e1e0d2012-07-30 14:47:51 -0700277 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700278 " vec4 gradientColor = mix(startColor, endColor, clamp(index - floor(index), 0.0, 1.0));\n"
Romain Guyee916f12010-09-20 17:53:08 -0700279};
Romain Guyac670c02010-07-27 17:39:27 -0700280const char* gFS_Main_FetchBitmap =
281 " vec4 bitmapColor = texture2D(bitmapSampler, outBitmapTexCoords);\n";
Romain Guy889f8d12010-07-29 14:37:42 -0700282const char* gFS_Main_FetchBitmapNpot =
283 " vec4 bitmapColor = texture2D(bitmapSampler, wrap(outBitmapTexCoords));\n";
Romain Guyac670c02010-07-27 17:39:27 -0700284const char* gFS_Main_BlendShadersBG =
Romain Guyac670c02010-07-27 17:39:27 -0700285 " fragColor = blendShaders(gradientColor, bitmapColor)";
Romain Guy06f96e22010-07-30 19:18:16 -0700286const char* gFS_Main_BlendShadersGB =
287 " fragColor = blendShaders(bitmapColor, gradientColor)";
Romain Guya938f562012-09-13 20:31:08 -0700288const char* gFS_Main_BlendShaders_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700289 // Don't modulate
290 ";\n",
Romain Guya938f562012-09-13 20:31:08 -0700291 ";\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700292 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700293 " * color.a;\n",
294 " * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700295 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700296 " * texture2D(baseSampler, outTexCoords).a;\n",
297 " * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700298};
Romain Guya938f562012-09-13 20:31:08 -0700299const char* gFS_Main_GradientShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700300 // Don't modulate
301 " fragColor = gradientColor;\n",
Romain Guya938f562012-09-13 20:31:08 -0700302 " fragColor = gradientColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700303 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700304 " fragColor = gradientColor * color.a;\n",
305 " fragColor = gradientColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700306 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700307 " fragColor = gradientColor * texture2D(baseSampler, outTexCoords).a;\n",
308 " fragColor = gradientColor * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700309 };
Romain Guya938f562012-09-13 20:31:08 -0700310const char* gFS_Main_BitmapShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700311 // Don't modulate
312 " fragColor = bitmapColor;\n",
Romain Guya938f562012-09-13 20:31:08 -0700313 " fragColor = bitmapColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700314 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700315 " fragColor = bitmapColor * color.a;\n",
316 " fragColor = bitmapColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700317 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700318 " fragColor = bitmapColor * texture2D(baseSampler, outTexCoords).a;\n",
319 " fragColor = bitmapColor * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700320 };
Romain Guyac670c02010-07-27 17:39:27 -0700321const char* gFS_Main_FragColor =
322 " gl_FragColor = fragColor;\n";
Romain Guyff316ec2013-02-13 18:39:43 -0800323const char* gFS_Main_FragColor_HasColors =
324 " gl_FragColor *= outColors;\n";
Romain Guya5aed0d2010-09-09 14:42:43 -0700325const char* gFS_Main_FragColor_Blend =
326 " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n";
Romain Guyf607bdc2010-09-10 19:20:06 -0700327const char* gFS_Main_FragColor_Blend_Swap =
328 " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n";
Romain Guyac670c02010-07-27 17:39:27 -0700329const char* gFS_Main_ApplyColorOp[4] = {
330 // None
331 "",
332 // Matrix
333 " fragColor *= colorMatrix;\n"
Romain Guydb1938e2010-08-02 18:50:22 -0700334 " fragColor += colorMatrixVector;\n"
335 " fragColor.rgb *= fragColor.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700336 // Lighting
Romain Guydb1938e2010-08-02 18:50:22 -0700337 " float lightingAlpha = fragColor.a;\n"
338 " fragColor = min(fragColor * lightingMul + (lightingAdd * lightingAlpha), lightingAlpha);\n"
339 " fragColor.a = lightingAlpha;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700340 // PorterDuff
341 " fragColor = blendColors(colorBlend, fragColor);\n"
342};
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800343const char* gFS_Main_DebugHighlight =
344 " gl_FragColor.rgb = vec3(0.0, gl_FragColor.a, 0.0);\n";
Romain Guyac670c02010-07-27 17:39:27 -0700345const char* gFS_Footer =
346 "}\n\n";
347
348///////////////////////////////////////////////////////////////////////////////
349// PorterDuff snippets
350///////////////////////////////////////////////////////////////////////////////
351
Romain Guy48daa542010-08-10 19:21:34 -0700352const char* gBlendOps[18] = {
Romain Guyac670c02010-07-27 17:39:27 -0700353 // Clear
354 "return vec4(0.0, 0.0, 0.0, 0.0);\n",
355 // Src
356 "return src;\n",
357 // Dst
358 "return dst;\n",
359 // SrcOver
Romain Guy06f96e22010-07-30 19:18:16 -0700360 "return src + dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700361 // DstOver
Romain Guy06f96e22010-07-30 19:18:16 -0700362 "return dst + src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700363 // SrcIn
Romain Guy06f96e22010-07-30 19:18:16 -0700364 "return src * dst.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700365 // DstIn
Romain Guy06f96e22010-07-30 19:18:16 -0700366 "return dst * src.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700367 // SrcOut
Romain Guy06f96e22010-07-30 19:18:16 -0700368 "return src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700369 // DstOut
Romain Guy06f96e22010-07-30 19:18:16 -0700370 "return dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700371 // SrcAtop
372 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n",
373 // DstAtop
374 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n",
375 // Xor
Romain Guy48daa542010-08-10 19:21:34 -0700376 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, "
Romain Guyac670c02010-07-27 17:39:27 -0700377 "src.a + dst.a - 2.0 * src.a * dst.a);\n",
Romain Guy48daa542010-08-10 19:21:34 -0700378 // Add
379 "return min(src + dst, 1.0);\n",
380 // Multiply
381 "return src * dst;\n",
382 // Screen
383 "return src + dst - src * dst;\n",
384 // Overlay
385 "return clamp(vec4(mix("
386 "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), "
387 "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), "
388 "step(dst.a, 2.0 * dst.rgb)), "
389 "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n",
390 // Darken
391 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
392 "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
393 // Lighten
394 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
395 "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 -0700396};
397
398///////////////////////////////////////////////////////////////////////////////
399// Constructors/destructors
400///////////////////////////////////////////////////////////////////////////////
401
Romain Guyb4880042013-04-05 11:17:55 -0700402ProgramCache::ProgramCache(): mHasES3(Extensions::getInstance().getMajorGlVersion() >= 3) {
Romain Guyac670c02010-07-27 17:39:27 -0700403}
404
405ProgramCache::~ProgramCache() {
406 clear();
407}
408
409///////////////////////////////////////////////////////////////////////////////
410// Cache management
411///////////////////////////////////////////////////////////////////////////////
412
413void ProgramCache::clear() {
Romain Guy67f27952010-12-07 20:09:23 -0800414 PROGRAM_LOGD("Clearing program cache");
415
Romain Guyac670c02010-07-27 17:39:27 -0700416 size_t count = mCache.size();
417 for (size_t i = 0; i < count; i++) {
418 delete mCache.valueAt(i);
419 }
420 mCache.clear();
421}
422
423Program* ProgramCache::get(const ProgramDescription& description) {
424 programid key = description.key();
Chris Craik096b8d92013-03-01 11:08:11 -0800425 if (key == (PROGRAM_KEY_TEXTURE | PROGRAM_KEY_A8_TEXTURE)) {
426 // program for A8, unmodulated, texture w/o shader (black text/path textures) is equivalent
427 // to standard texture program (bitmaps, patches). Consider them equivalent.
428 key = PROGRAM_KEY_TEXTURE;
429 }
430
Romain Guyac670c02010-07-27 17:39:27 -0700431 ssize_t index = mCache.indexOfKey(key);
432 Program* program = NULL;
433 if (index < 0) {
Romain Guyee916f12010-09-20 17:53:08 -0700434 description.log("Could not find program");
Romain Guyac670c02010-07-27 17:39:27 -0700435 program = generateProgram(description, key);
436 mCache.add(key, program);
437 } else {
438 program = mCache.valueAt(index);
439 }
440 return program;
441}
442
443///////////////////////////////////////////////////////////////////////////////
444// Program generation
445///////////////////////////////////////////////////////////////////////////////
446
447Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) {
448 String8 vertexShader = generateVertexShader(description);
449 String8 fragmentShader = generateFragmentShader(description);
450
Romain Guy42e1e0d2012-07-30 14:47:51 -0700451 return new Program(description, vertexShader.string(), fragmentShader.string());
452}
453
454static inline size_t gradientIndex(const ProgramDescription& description) {
455 return description.gradientType * 2 + description.isSimpleGradient;
Romain Guyac670c02010-07-27 17:39:27 -0700456}
457
458String8 ProgramCache::generateVertexShader(const ProgramDescription& description) {
459 // Add attributes
460 String8 shader(gVS_Header_Attributes);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700461 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700462 shader.append(gVS_Header_Attributes_TexCoords);
463 }
Chris Craik710f46d2012-09-17 17:25:49 -0700464 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800465 shader.append(gVS_Header_Attributes_AAVertexShapeParameters);
Chet Haase5b0200b2011-04-13 17:58:08 -0700466 }
Romain Guyff316ec2013-02-13 18:39:43 -0800467 if (description.hasColors) {
468 shader.append(gVS_Header_Attributes_Colors);
469 }
Romain Guyac670c02010-07-27 17:39:27 -0700470 // Uniforms
471 shader.append(gVS_Header_Uniforms);
Romain Guy8f0095c2011-05-02 17:24:22 -0700472 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700473 shader.append(gVS_Header_Uniforms_TextureTransform);
474 }
Romain Guyac670c02010-07-27 17:39:27 -0700475 if (description.hasGradient) {
Romain Guyb4880042013-04-05 11:17:55 -0700476 shader.append(gVS_Header_Uniforms_HasGradient);
Romain Guyac670c02010-07-27 17:39:27 -0700477 }
Romain Guy889f8d12010-07-29 14:37:42 -0700478 if (description.hasBitmap) {
479 shader.append(gVS_Header_Uniforms_HasBitmap);
480 }
Romain Guyed6fcb02011-03-21 13:11:28 -0700481 if (description.isPoint) {
482 shader.append(gVS_Header_Uniforms_IsPoint);
483 }
Romain Guyac670c02010-07-27 17:39:27 -0700484 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700485 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700486 shader.append(gVS_Header_Varyings_HasTexture);
487 }
Chris Craik710f46d2012-09-17 17:25:49 -0700488 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800489 shader.append(gVS_Header_Varyings_IsAAVertexShape);
Chet Haase5b0200b2011-04-13 17:58:08 -0700490 }
Romain Guyff316ec2013-02-13 18:39:43 -0800491 if (description.hasColors) {
492 shader.append(gVS_Header_Varyings_HasColors);
493 }
Romain Guyac670c02010-07-27 17:39:27 -0700494 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700495 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700496 }
497 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700498 shader.append(description.isPoint ?
Romain Guy63553472012-07-18 20:04:14 -0700499 gVS_Header_Varyings_PointHasBitmap :
500 gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700501 }
502
503 // Begin the shader
504 shader.append(gVS_Main); {
Romain Guy8f0095c2011-05-02 17:24:22 -0700505 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700506 shader.append(gVS_Main_OutTransformedTexCoords);
Romain Guy8f0095c2011-05-02 17:24:22 -0700507 } else if (description.hasTexture || description.hasExternalTexture) {
508 shader.append(gVS_Main_OutTexCoords);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700509 }
Chris Craik710f46d2012-09-17 17:25:49 -0700510 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800511 shader.append(gVS_Main_AAVertexShape);
Chet Haase5b0200b2011-04-13 17:58:08 -0700512 }
Romain Guyff316ec2013-02-13 18:39:43 -0800513 if (description.hasColors) {
514 shader.append(gVS_Main_OutColors);
515 }
Romain Guy889f8d12010-07-29 14:37:42 -0700516 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700517 shader.append(description.isPoint ?
518 gVS_Main_OutPointBitmapTexCoords :
519 gVS_Main_OutBitmapTexCoords);
520 }
521 if (description.isPoint) {
522 shader.append(gVS_Main_PointSize);
Romain Guy889f8d12010-07-29 14:37:42 -0700523 }
Romain Guyac670c02010-07-27 17:39:27 -0700524 // Output transformed position
525 shader.append(gVS_Main_Position);
Chet Haasea1d12dd2012-09-21 14:50:14 -0700526 if (description.hasGradient) {
527 shader.append(gVS_Main_OutGradient[gradientIndex(description)]);
528 }
Romain Guyac670c02010-07-27 17:39:27 -0700529 }
530 // End the shader
531 shader.append(gVS_Footer);
532
533 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string());
534
535 return shader;
536}
537
Romain Guya938f562012-09-13 20:31:08 -0700538static bool shaderOp(const ProgramDescription& description, String8& shader,
539 const int modulateOp, const char** snippets) {
540 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
541 op = op * 2 + description.hasGammaCorrection;
542 shader.append(snippets[op]);
543 return description.hasAlpha8Texture;
544}
545
Romain Guyac670c02010-07-27 17:39:27 -0700546String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) {
Romain Guya5aed0d2010-09-09 14:42:43 -0700547 String8 shader;
548
Romain Guy707b2f72010-10-11 16:34:59 -0700549 const bool blendFramebuffer = description.framebufferMode >= SkXfermode::kPlus_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700550 if (blendFramebuffer) {
551 shader.append(gFS_Header_Extension_FramebufferFetch);
552 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700553 if (description.hasExternalTexture) {
554 shader.append(gFS_Header_Extension_ExternalTexture);
555 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700556
557 shader.append(gFS_Header);
Romain Guyac670c02010-07-27 17:39:27 -0700558
559 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700560 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700561 shader.append(gVS_Header_Varyings_HasTexture);
562 }
Chris Craik710f46d2012-09-17 17:25:49 -0700563 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800564 shader.append(gVS_Header_Varyings_IsAAVertexShape);
Chet Haase5b0200b2011-04-13 17:58:08 -0700565 }
Romain Guyff316ec2013-02-13 18:39:43 -0800566 if (description.hasColors) {
567 shader.append(gVS_Header_Varyings_HasColors);
568 }
Romain Guyac670c02010-07-27 17:39:27 -0700569 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700570 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700571 }
572 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700573 shader.append(description.isPoint ?
Romain Guy63553472012-07-18 20:04:14 -0700574 gVS_Header_Varyings_PointHasBitmap :
575 gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700576 }
577
Romain Guyac670c02010-07-27 17:39:27 -0700578 // Uniforms
Romain Guy707b2f72010-10-11 16:34:59 -0700579 int modulateOp = MODULATE_OP_NO_MODULATE;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700580 const bool singleColor = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700581 !description.hasGradient && !description.hasBitmap;
582
583 if (description.modulate || singleColor) {
584 shader.append(gFS_Uniforms_Color);
585 if (!singleColor) modulateOp = MODULATE_OP_MODULATE;
586 }
Romain Guyac670c02010-07-27 17:39:27 -0700587 if (description.hasTexture) {
588 shader.append(gFS_Uniforms_TextureSampler);
Romain Guy8f0095c2011-05-02 17:24:22 -0700589 } else if (description.hasExternalTexture) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700590 shader.append(gFS_Uniforms_ExternalTextureSampler);
591 }
Romain Guyac670c02010-07-27 17:39:27 -0700592 if (description.hasGradient) {
Romain Guyb4880042013-04-05 11:17:55 -0700593 shader.appendFormat(gFS_Uniforms_GradientSampler[description.isSimpleGradient],
594 gFS_Uniforms_Dither);
Romain Guyac670c02010-07-27 17:39:27 -0700595 }
Romain Guyed6fcb02011-03-21 13:11:28 -0700596 if (description.hasBitmap && description.isPoint) {
597 shader.append(gFS_Header_Uniforms_PointHasBitmap);
598 }
Romain Guy41210632012-07-16 17:04:24 -0700599 if (description.hasGammaCorrection) {
600 shader.append(gFS_Uniforms_Gamma);
601 }
Romain Guy707b2f72010-10-11 16:34:59 -0700602
603 // Optimization for common cases
Romain Guyff316ec2013-02-13 18:39:43 -0800604 if (!description.isAA && !blendFramebuffer && !description.hasColors &&
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800605 description.colorOp == ProgramDescription::kColorNone &&
606 !description.isPoint && !description.hasDebugHighlight) {
Romain Guy707b2f72010-10-11 16:34:59 -0700607 bool fast = false;
608
609 const bool noShader = !description.hasGradient && !description.hasBitmap;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700610 const bool singleTexture = (description.hasTexture || description.hasExternalTexture) &&
Romain Guy707b2f72010-10-11 16:34:59 -0700611 !description.hasAlpha8Texture && noShader;
612 const bool singleA8Texture = description.hasTexture &&
613 description.hasAlpha8Texture && noShader;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700614 const bool singleGradient = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700615 description.hasGradient && !description.hasBitmap &&
616 description.gradientType == ProgramDescription::kGradientLinear;
617
618 if (singleColor) {
619 shader.append(gFS_Fast_SingleColor);
620 fast = true;
621 } else if (singleTexture) {
622 if (!description.modulate) {
623 shader.append(gFS_Fast_SingleTexture);
624 } else {
625 shader.append(gFS_Fast_SingleModulateTexture);
626 }
627 fast = true;
628 } else if (singleA8Texture) {
629 if (!description.modulate) {
Romain Guy41210632012-07-16 17:04:24 -0700630 if (description.hasGammaCorrection) {
631 shader.append(gFS_Fast_SingleA8Texture_ApplyGamma);
632 } else {
633 shader.append(gFS_Fast_SingleA8Texture);
634 }
Romain Guy707b2f72010-10-11 16:34:59 -0700635 } else {
Romain Guy41210632012-07-16 17:04:24 -0700636 if (description.hasGammaCorrection) {
637 shader.append(gFS_Fast_SingleModulateA8Texture_ApplyGamma);
638 } else {
639 shader.append(gFS_Fast_SingleModulateA8Texture);
640 }
Romain Guy707b2f72010-10-11 16:34:59 -0700641 }
642 fast = true;
643 } else if (singleGradient) {
644 if (!description.modulate) {
Romain Guyb4880042013-04-05 11:17:55 -0700645 shader.appendFormat(gFS_Fast_SingleGradient[description.isSimpleGradient],
646 gFS_Main_Dither[mHasES3]);
Romain Guy707b2f72010-10-11 16:34:59 -0700647 } else {
Romain Guyb4880042013-04-05 11:17:55 -0700648 shader.appendFormat(gFS_Fast_SingleModulateGradient[description.isSimpleGradient],
649 gFS_Main_Dither[mHasES3]);
Romain Guy707b2f72010-10-11 16:34:59 -0700650 }
651 fast = true;
652 }
653
654 if (fast) {
Romain Guyc15008e2010-11-10 11:59:15 -0800655#if DEBUG_PROGRAMS
Romain Guy707b2f72010-10-11 16:34:59 -0700656 PROGRAM_LOGD("*** Fast case:\n");
657 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
658 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800659#endif
Romain Guy707b2f72010-10-11 16:34:59 -0700660
661 return shader;
662 }
663 }
664
Romain Guyac670c02010-07-27 17:39:27 -0700665 if (description.hasBitmap) {
666 shader.append(gFS_Uniforms_BitmapSampler);
667 }
668 shader.append(gFS_Uniforms_ColorOp[description.colorOp]);
669
670 // Generate required functions
671 if (description.hasGradient && description.hasBitmap) {
Romain Guy48daa542010-08-10 19:21:34 -0700672 generateBlend(shader, "blendShaders", description.shadersMode);
Romain Guyac670c02010-07-27 17:39:27 -0700673 }
674 if (description.colorOp == ProgramDescription::kColorBlend) {
Romain Guy48daa542010-08-10 19:21:34 -0700675 generateBlend(shader, "blendColors", description.colorMode);
Romain Guyac670c02010-07-27 17:39:27 -0700676 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700677 if (blendFramebuffer) {
678 generateBlend(shader, "blendFramebuffer", description.framebufferMode);
679 }
Romain Guy889f8d12010-07-29 14:37:42 -0700680 if (description.isBitmapNpot) {
681 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
682 }
Romain Guyac670c02010-07-27 17:39:27 -0700683
684 // Begin the shader
685 shader.append(gFS_Main); {
686 // Stores the result in fragColor directly
Romain Guyaa6c24c2011-04-28 18:40:04 -0700687 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700688 if (description.hasAlpha8Texture) {
Romain Guy707b2f72010-10-11 16:34:59 -0700689 if (!description.hasGradient && !description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700690 shader.append(gFS_Main_FetchA8Texture[modulateOp * 2 +
691 description.hasGammaCorrection]);
Romain Guy707b2f72010-10-11 16:34:59 -0700692 }
Romain Guyac670c02010-07-27 17:39:27 -0700693 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700694 shader.append(gFS_Main_FetchTexture[modulateOp]);
Romain Guyac670c02010-07-27 17:39:27 -0700695 }
696 } else {
Romain Guya938f562012-09-13 20:31:08 -0700697 if (!description.hasGradient && !description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700698 shader.append(gFS_Main_FetchColor);
699 }
Romain Guyac670c02010-07-27 17:39:27 -0700700 }
701 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700702 shader.append(gFS_Main_FetchGradient[gradientIndex(description)]);
Romain Guyb4880042013-04-05 11:17:55 -0700703 shader.appendFormat(gFS_Main_AddDitherToGradient, gFS_Main_Dither[mHasES3]);
Romain Guyac670c02010-07-27 17:39:27 -0700704 }
705 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700706 if (description.isPoint) {
707 shader.append(gFS_Main_PointBitmapTexCoords);
708 }
Romain Guy889f8d12010-07-29 14:37:42 -0700709 if (!description.isBitmapNpot) {
710 shader.append(gFS_Main_FetchBitmap);
711 } else {
712 shader.append(gFS_Main_FetchBitmapNpot);
713 }
Romain Guyac670c02010-07-27 17:39:27 -0700714 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700715 bool applyModulate = false;
Romain Guyac670c02010-07-27 17:39:27 -0700716 // Case when we have two shaders set
717 if (description.hasGradient && description.hasBitmap) {
718 if (description.isBitmapFirst) {
719 shader.append(gFS_Main_BlendShadersBG);
720 } else {
721 shader.append(gFS_Main_BlendShadersGB);
722 }
Romain Guya938f562012-09-13 20:31:08 -0700723 applyModulate = shaderOp(description, shader, modulateOp,
724 gFS_Main_BlendShaders_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700725 } else {
726 if (description.hasGradient) {
Romain Guya938f562012-09-13 20:31:08 -0700727 applyModulate = shaderOp(description, shader, modulateOp,
728 gFS_Main_GradientShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700729 } else if (description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700730 applyModulate = shaderOp(description, shader, modulateOp,
731 gFS_Main_BitmapShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700732 }
Romain Guyac670c02010-07-27 17:39:27 -0700733 }
Romain Guya938f562012-09-13 20:31:08 -0700734
Romain Guy740bf2b2011-04-26 15:33:10 -0700735 if (description.modulate && applyModulate) {
Romain Guya938f562012-09-13 20:31:08 -0700736 shader.append(gFS_Main_ModulateColor);
Romain Guy740bf2b2011-04-26 15:33:10 -0700737 }
Romain Guya938f562012-09-13 20:31:08 -0700738
Romain Guyac670c02010-07-27 17:39:27 -0700739 // Apply the color op if needed
740 shader.append(gFS_Main_ApplyColorOp[description.colorOp]);
Chris Craik9f44a132012-09-13 18:34:55 -0700741
Chris Craik710f46d2012-09-17 17:25:49 -0700742 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800743 shader.append(gFS_Main_AccountForAAVertexShape);
Chris Craik9f44a132012-09-13 18:34:55 -0700744 }
745
Romain Guyac670c02010-07-27 17:39:27 -0700746 // Output the fragment
Romain Guya5aed0d2010-09-09 14:42:43 -0700747 if (!blendFramebuffer) {
748 shader.append(gFS_Main_FragColor);
749 } else {
Romain Guyf607bdc2010-09-10 19:20:06 -0700750 shader.append(!description.swapSrcDst ?
751 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap);
Romain Guya5aed0d2010-09-09 14:42:43 -0700752 }
Romain Guyff316ec2013-02-13 18:39:43 -0800753 if (description.hasColors) {
754 shader.append(gFS_Main_FragColor_HasColors);
755 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800756 if (description.hasDebugHighlight) {
757 shader.append(gFS_Main_DebugHighlight);
758 }
Romain Guyac670c02010-07-27 17:39:27 -0700759 }
760 // End the shader
761 shader.append(gFS_Footer);
762
Romain Guyc15008e2010-11-10 11:59:15 -0800763#if DEBUG_PROGRAMS
Romain Guydb1938e2010-08-02 18:50:22 -0700764 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
765 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800766#endif
Romain Guydb1938e2010-08-02 18:50:22 -0700767
Romain Guyac670c02010-07-27 17:39:27 -0700768 return shader;
769}
770
Romain Guy48daa542010-08-10 19:21:34 -0700771void ProgramCache::generateBlend(String8& shader, const char* name, SkXfermode::Mode mode) {
Romain Guyac670c02010-07-27 17:39:27 -0700772 shader.append("\nvec4 ");
773 shader.append(name);
774 shader.append("(vec4 src, vec4 dst) {\n");
775 shader.append(" ");
Romain Guy48daa542010-08-10 19:21:34 -0700776 shader.append(gBlendOps[mode]);
Romain Guyac670c02010-07-27 17:39:27 -0700777 shader.append("}\n");
778}
779
Romain Guy889f8d12010-07-29 14:37:42 -0700780void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) {
Romain Guy63553472012-07-18 20:04:14 -0700781 shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700782 if (wrapS == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700783 shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700784 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n");
785 }
786 if (wrapT == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700787 shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700788 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n");
789 }
790 shader.append(" return vec2(");
791 switch (wrapS) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700792 case GL_CLAMP_TO_EDGE:
793 shader.append("texCoords.x");
794 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700795 case GL_REPEAT:
796 shader.append("mod(texCoords.x, 1.0)");
797 break;
798 case GL_MIRRORED_REPEAT:
799 shader.append("xMod2");
800 break;
801 }
802 shader.append(", ");
803 switch (wrapT) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700804 case GL_CLAMP_TO_EDGE:
805 shader.append("texCoords.y");
806 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700807 case GL_REPEAT:
808 shader.append("mod(texCoords.y, 1.0)");
809 break;
810 case GL_MIRRORED_REPEAT:
811 shader.append("yMod2");
812 break;
813 }
814 shader.append(");\n");
815 shader.append("}\n");
816}
817
Romain Guydb1938e2010-08-02 18:50:22 -0700818void ProgramCache::printLongString(const String8& shader) const {
819 ssize_t index = 0;
820 ssize_t lastIndex = 0;
821 const char* str = shader.string();
822 while ((index = shader.find("\n", index)) > -1) {
823 String8 line(str, index - lastIndex);
824 if (line.length() == 0) line.append("\n");
825 PROGRAM_LOGD("%s", line.string());
826 index++;
827 str += (index - lastIndex);
828 lastIndex = index;
829 }
830}
831
Romain Guyac670c02010-07-27 17:39:27 -0700832}; // namespace uirenderer
833}; // namespace android