blob: 367294cb677d67e916802a63efc240fdfead9ed1 [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 Guy78dd96d2013-05-03 14:24:16 -0700345const char* gFS_Main_EmulateStencil =
346 " gl_FragColor.rgba = vec4(1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0, 1.0);\n"
347 " return;\n"
348 " /*\n";
349const char* gFS_Footer_EmulateStencil =
350 " */\n";
Romain Guyac670c02010-07-27 17:39:27 -0700351const char* gFS_Footer =
352 "}\n\n";
353
354///////////////////////////////////////////////////////////////////////////////
355// PorterDuff snippets
356///////////////////////////////////////////////////////////////////////////////
357
Romain Guy48daa542010-08-10 19:21:34 -0700358const char* gBlendOps[18] = {
Romain Guyac670c02010-07-27 17:39:27 -0700359 // Clear
360 "return vec4(0.0, 0.0, 0.0, 0.0);\n",
361 // Src
362 "return src;\n",
363 // Dst
364 "return dst;\n",
365 // SrcOver
Romain Guy06f96e22010-07-30 19:18:16 -0700366 "return src + dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700367 // DstOver
Romain Guy06f96e22010-07-30 19:18:16 -0700368 "return dst + src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700369 // SrcIn
Romain Guy06f96e22010-07-30 19:18:16 -0700370 "return src * dst.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700371 // DstIn
Romain Guy06f96e22010-07-30 19:18:16 -0700372 "return dst * src.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700373 // SrcOut
Romain Guy06f96e22010-07-30 19:18:16 -0700374 "return src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700375 // DstOut
Romain Guy06f96e22010-07-30 19:18:16 -0700376 "return dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700377 // SrcAtop
378 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n",
379 // DstAtop
380 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n",
381 // Xor
Romain Guy48daa542010-08-10 19:21:34 -0700382 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, "
Romain Guyac670c02010-07-27 17:39:27 -0700383 "src.a + dst.a - 2.0 * src.a * dst.a);\n",
Romain Guy48daa542010-08-10 19:21:34 -0700384 // Add
385 "return min(src + dst, 1.0);\n",
386 // Multiply
387 "return src * dst;\n",
388 // Screen
389 "return src + dst - src * dst;\n",
390 // Overlay
391 "return clamp(vec4(mix("
392 "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), "
393 "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), "
394 "step(dst.a, 2.0 * dst.rgb)), "
395 "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n",
396 // Darken
397 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
398 "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
399 // Lighten
400 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
401 "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 -0700402};
403
404///////////////////////////////////////////////////////////////////////////////
405// Constructors/destructors
406///////////////////////////////////////////////////////////////////////////////
407
Romain Guyb4880042013-04-05 11:17:55 -0700408ProgramCache::ProgramCache(): mHasES3(Extensions::getInstance().getMajorGlVersion() >= 3) {
Romain Guyac670c02010-07-27 17:39:27 -0700409}
410
411ProgramCache::~ProgramCache() {
412 clear();
413}
414
415///////////////////////////////////////////////////////////////////////////////
416// Cache management
417///////////////////////////////////////////////////////////////////////////////
418
419void ProgramCache::clear() {
Romain Guy67f27952010-12-07 20:09:23 -0800420 PROGRAM_LOGD("Clearing program cache");
421
Romain Guyac670c02010-07-27 17:39:27 -0700422 size_t count = mCache.size();
423 for (size_t i = 0; i < count; i++) {
424 delete mCache.valueAt(i);
425 }
426 mCache.clear();
427}
428
429Program* ProgramCache::get(const ProgramDescription& description) {
430 programid key = description.key();
Chris Craik096b8d92013-03-01 11:08:11 -0800431 if (key == (PROGRAM_KEY_TEXTURE | PROGRAM_KEY_A8_TEXTURE)) {
432 // program for A8, unmodulated, texture w/o shader (black text/path textures) is equivalent
433 // to standard texture program (bitmaps, patches). Consider them equivalent.
434 key = PROGRAM_KEY_TEXTURE;
435 }
436
Romain Guyac670c02010-07-27 17:39:27 -0700437 ssize_t index = mCache.indexOfKey(key);
438 Program* program = NULL;
439 if (index < 0) {
Romain Guyee916f12010-09-20 17:53:08 -0700440 description.log("Could not find program");
Romain Guyac670c02010-07-27 17:39:27 -0700441 program = generateProgram(description, key);
442 mCache.add(key, program);
443 } else {
444 program = mCache.valueAt(index);
445 }
446 return program;
447}
448
449///////////////////////////////////////////////////////////////////////////////
450// Program generation
451///////////////////////////////////////////////////////////////////////////////
452
453Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) {
454 String8 vertexShader = generateVertexShader(description);
455 String8 fragmentShader = generateFragmentShader(description);
456
Romain Guy42e1e0d2012-07-30 14:47:51 -0700457 return new Program(description, vertexShader.string(), fragmentShader.string());
458}
459
460static inline size_t gradientIndex(const ProgramDescription& description) {
461 return description.gradientType * 2 + description.isSimpleGradient;
Romain Guyac670c02010-07-27 17:39:27 -0700462}
463
464String8 ProgramCache::generateVertexShader(const ProgramDescription& description) {
465 // Add attributes
466 String8 shader(gVS_Header_Attributes);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700467 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700468 shader.append(gVS_Header_Attributes_TexCoords);
469 }
Chris Craik710f46d2012-09-17 17:25:49 -0700470 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800471 shader.append(gVS_Header_Attributes_AAVertexShapeParameters);
Chet Haase5b0200b2011-04-13 17:58:08 -0700472 }
Romain Guyff316ec2013-02-13 18:39:43 -0800473 if (description.hasColors) {
474 shader.append(gVS_Header_Attributes_Colors);
475 }
Romain Guyac670c02010-07-27 17:39:27 -0700476 // Uniforms
477 shader.append(gVS_Header_Uniforms);
Romain Guy8f0095c2011-05-02 17:24:22 -0700478 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700479 shader.append(gVS_Header_Uniforms_TextureTransform);
480 }
Romain Guyac670c02010-07-27 17:39:27 -0700481 if (description.hasGradient) {
Romain Guyb4880042013-04-05 11:17:55 -0700482 shader.append(gVS_Header_Uniforms_HasGradient);
Romain Guyac670c02010-07-27 17:39:27 -0700483 }
Romain Guy889f8d12010-07-29 14:37:42 -0700484 if (description.hasBitmap) {
485 shader.append(gVS_Header_Uniforms_HasBitmap);
486 }
Romain Guyed6fcb02011-03-21 13:11:28 -0700487 if (description.isPoint) {
488 shader.append(gVS_Header_Uniforms_IsPoint);
489 }
Romain Guyac670c02010-07-27 17:39:27 -0700490 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700491 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700492 shader.append(gVS_Header_Varyings_HasTexture);
493 }
Chris Craik710f46d2012-09-17 17:25:49 -0700494 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800495 shader.append(gVS_Header_Varyings_IsAAVertexShape);
Chet Haase5b0200b2011-04-13 17:58:08 -0700496 }
Romain Guyff316ec2013-02-13 18:39:43 -0800497 if (description.hasColors) {
498 shader.append(gVS_Header_Varyings_HasColors);
499 }
Romain Guyac670c02010-07-27 17:39:27 -0700500 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700501 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700502 }
503 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700504 shader.append(description.isPoint ?
Romain Guy63553472012-07-18 20:04:14 -0700505 gVS_Header_Varyings_PointHasBitmap :
506 gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700507 }
508
509 // Begin the shader
510 shader.append(gVS_Main); {
Romain Guy8f0095c2011-05-02 17:24:22 -0700511 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700512 shader.append(gVS_Main_OutTransformedTexCoords);
Romain Guy8f0095c2011-05-02 17:24:22 -0700513 } else if (description.hasTexture || description.hasExternalTexture) {
514 shader.append(gVS_Main_OutTexCoords);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700515 }
Chris Craik710f46d2012-09-17 17:25:49 -0700516 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800517 shader.append(gVS_Main_AAVertexShape);
Chet Haase5b0200b2011-04-13 17:58:08 -0700518 }
Romain Guyff316ec2013-02-13 18:39:43 -0800519 if (description.hasColors) {
520 shader.append(gVS_Main_OutColors);
521 }
Romain Guy889f8d12010-07-29 14:37:42 -0700522 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700523 shader.append(description.isPoint ?
524 gVS_Main_OutPointBitmapTexCoords :
525 gVS_Main_OutBitmapTexCoords);
526 }
527 if (description.isPoint) {
528 shader.append(gVS_Main_PointSize);
Romain Guy889f8d12010-07-29 14:37:42 -0700529 }
Romain Guyac670c02010-07-27 17:39:27 -0700530 // Output transformed position
531 shader.append(gVS_Main_Position);
Chet Haasea1d12dd2012-09-21 14:50:14 -0700532 if (description.hasGradient) {
533 shader.append(gVS_Main_OutGradient[gradientIndex(description)]);
534 }
Romain Guyac670c02010-07-27 17:39:27 -0700535 }
536 // End the shader
537 shader.append(gVS_Footer);
538
539 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string());
540
541 return shader;
542}
543
Romain Guya938f562012-09-13 20:31:08 -0700544static bool shaderOp(const ProgramDescription& description, String8& shader,
545 const int modulateOp, const char** snippets) {
546 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
547 op = op * 2 + description.hasGammaCorrection;
548 shader.append(snippets[op]);
549 return description.hasAlpha8Texture;
550}
551
Romain Guyac670c02010-07-27 17:39:27 -0700552String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) {
Romain Guya5aed0d2010-09-09 14:42:43 -0700553 String8 shader;
554
Romain Guy707b2f72010-10-11 16:34:59 -0700555 const bool blendFramebuffer = description.framebufferMode >= SkXfermode::kPlus_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700556 if (blendFramebuffer) {
557 shader.append(gFS_Header_Extension_FramebufferFetch);
558 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700559 if (description.hasExternalTexture) {
560 shader.append(gFS_Header_Extension_ExternalTexture);
561 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700562
563 shader.append(gFS_Header);
Romain Guyac670c02010-07-27 17:39:27 -0700564
565 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700566 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700567 shader.append(gVS_Header_Varyings_HasTexture);
568 }
Chris Craik710f46d2012-09-17 17:25:49 -0700569 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800570 shader.append(gVS_Header_Varyings_IsAAVertexShape);
Chet Haase5b0200b2011-04-13 17:58:08 -0700571 }
Romain Guyff316ec2013-02-13 18:39:43 -0800572 if (description.hasColors) {
573 shader.append(gVS_Header_Varyings_HasColors);
574 }
Romain Guyac670c02010-07-27 17:39:27 -0700575 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700576 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700577 }
578 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700579 shader.append(description.isPoint ?
Romain Guy63553472012-07-18 20:04:14 -0700580 gVS_Header_Varyings_PointHasBitmap :
581 gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700582 }
583
Romain Guyac670c02010-07-27 17:39:27 -0700584 // Uniforms
Romain Guy707b2f72010-10-11 16:34:59 -0700585 int modulateOp = MODULATE_OP_NO_MODULATE;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700586 const bool singleColor = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700587 !description.hasGradient && !description.hasBitmap;
588
589 if (description.modulate || singleColor) {
590 shader.append(gFS_Uniforms_Color);
591 if (!singleColor) modulateOp = MODULATE_OP_MODULATE;
592 }
Romain Guyac670c02010-07-27 17:39:27 -0700593 if (description.hasTexture) {
594 shader.append(gFS_Uniforms_TextureSampler);
Romain Guy8f0095c2011-05-02 17:24:22 -0700595 } else if (description.hasExternalTexture) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700596 shader.append(gFS_Uniforms_ExternalTextureSampler);
597 }
Romain Guyac670c02010-07-27 17:39:27 -0700598 if (description.hasGradient) {
Romain Guyb4880042013-04-05 11:17:55 -0700599 shader.appendFormat(gFS_Uniforms_GradientSampler[description.isSimpleGradient],
600 gFS_Uniforms_Dither);
Romain Guyac670c02010-07-27 17:39:27 -0700601 }
Romain Guyed6fcb02011-03-21 13:11:28 -0700602 if (description.hasBitmap && description.isPoint) {
603 shader.append(gFS_Header_Uniforms_PointHasBitmap);
604 }
Romain Guy41210632012-07-16 17:04:24 -0700605 if (description.hasGammaCorrection) {
606 shader.append(gFS_Uniforms_Gamma);
607 }
Romain Guy707b2f72010-10-11 16:34:59 -0700608
609 // Optimization for common cases
Romain Guyff316ec2013-02-13 18:39:43 -0800610 if (!description.isAA && !blendFramebuffer && !description.hasColors &&
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800611 description.colorOp == ProgramDescription::kColorNone &&
Romain Guy78dd96d2013-05-03 14:24:16 -0700612 !description.isPoint && !description.hasDebugHighlight &&
613 !description.emulateStencil) {
Romain Guy707b2f72010-10-11 16:34:59 -0700614 bool fast = false;
615
616 const bool noShader = !description.hasGradient && !description.hasBitmap;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700617 const bool singleTexture = (description.hasTexture || description.hasExternalTexture) &&
Romain Guy707b2f72010-10-11 16:34:59 -0700618 !description.hasAlpha8Texture && noShader;
619 const bool singleA8Texture = description.hasTexture &&
620 description.hasAlpha8Texture && noShader;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700621 const bool singleGradient = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700622 description.hasGradient && !description.hasBitmap &&
623 description.gradientType == ProgramDescription::kGradientLinear;
624
625 if (singleColor) {
626 shader.append(gFS_Fast_SingleColor);
627 fast = true;
628 } else if (singleTexture) {
629 if (!description.modulate) {
630 shader.append(gFS_Fast_SingleTexture);
631 } else {
632 shader.append(gFS_Fast_SingleModulateTexture);
633 }
634 fast = true;
635 } else if (singleA8Texture) {
636 if (!description.modulate) {
Romain Guy41210632012-07-16 17:04:24 -0700637 if (description.hasGammaCorrection) {
638 shader.append(gFS_Fast_SingleA8Texture_ApplyGamma);
639 } else {
640 shader.append(gFS_Fast_SingleA8Texture);
641 }
Romain Guy707b2f72010-10-11 16:34:59 -0700642 } else {
Romain Guy41210632012-07-16 17:04:24 -0700643 if (description.hasGammaCorrection) {
644 shader.append(gFS_Fast_SingleModulateA8Texture_ApplyGamma);
645 } else {
646 shader.append(gFS_Fast_SingleModulateA8Texture);
647 }
Romain Guy707b2f72010-10-11 16:34:59 -0700648 }
649 fast = true;
650 } else if (singleGradient) {
651 if (!description.modulate) {
Romain Guyb4880042013-04-05 11:17:55 -0700652 shader.appendFormat(gFS_Fast_SingleGradient[description.isSimpleGradient],
653 gFS_Main_Dither[mHasES3]);
Romain Guy707b2f72010-10-11 16:34:59 -0700654 } else {
Romain Guyb4880042013-04-05 11:17:55 -0700655 shader.appendFormat(gFS_Fast_SingleModulateGradient[description.isSimpleGradient],
656 gFS_Main_Dither[mHasES3]);
Romain Guy707b2f72010-10-11 16:34:59 -0700657 }
658 fast = true;
659 }
660
661 if (fast) {
Romain Guyc15008e2010-11-10 11:59:15 -0800662#if DEBUG_PROGRAMS
Romain Guy707b2f72010-10-11 16:34:59 -0700663 PROGRAM_LOGD("*** Fast case:\n");
664 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
665 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800666#endif
Romain Guy707b2f72010-10-11 16:34:59 -0700667
668 return shader;
669 }
670 }
671
Romain Guyac670c02010-07-27 17:39:27 -0700672 if (description.hasBitmap) {
673 shader.append(gFS_Uniforms_BitmapSampler);
674 }
675 shader.append(gFS_Uniforms_ColorOp[description.colorOp]);
676
677 // Generate required functions
678 if (description.hasGradient && description.hasBitmap) {
Romain Guy48daa542010-08-10 19:21:34 -0700679 generateBlend(shader, "blendShaders", description.shadersMode);
Romain Guyac670c02010-07-27 17:39:27 -0700680 }
681 if (description.colorOp == ProgramDescription::kColorBlend) {
Romain Guy48daa542010-08-10 19:21:34 -0700682 generateBlend(shader, "blendColors", description.colorMode);
Romain Guyac670c02010-07-27 17:39:27 -0700683 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700684 if (blendFramebuffer) {
685 generateBlend(shader, "blendFramebuffer", description.framebufferMode);
686 }
Romain Guy889f8d12010-07-29 14:37:42 -0700687 if (description.isBitmapNpot) {
688 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
689 }
Romain Guyac670c02010-07-27 17:39:27 -0700690
691 // Begin the shader
692 shader.append(gFS_Main); {
Romain Guy78dd96d2013-05-03 14:24:16 -0700693 if (description.emulateStencil) {
694 shader.append(gFS_Main_EmulateStencil);
695 }
Romain Guyac670c02010-07-27 17:39:27 -0700696 // Stores the result in fragColor directly
Romain Guyaa6c24c2011-04-28 18:40:04 -0700697 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700698 if (description.hasAlpha8Texture) {
Romain Guy707b2f72010-10-11 16:34:59 -0700699 if (!description.hasGradient && !description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700700 shader.append(gFS_Main_FetchA8Texture[modulateOp * 2 +
701 description.hasGammaCorrection]);
Romain Guy707b2f72010-10-11 16:34:59 -0700702 }
Romain Guyac670c02010-07-27 17:39:27 -0700703 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700704 shader.append(gFS_Main_FetchTexture[modulateOp]);
Romain Guyac670c02010-07-27 17:39:27 -0700705 }
706 } else {
Romain Guya938f562012-09-13 20:31:08 -0700707 if (!description.hasGradient && !description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700708 shader.append(gFS_Main_FetchColor);
709 }
Romain Guyac670c02010-07-27 17:39:27 -0700710 }
711 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700712 shader.append(gFS_Main_FetchGradient[gradientIndex(description)]);
Romain Guyb4880042013-04-05 11:17:55 -0700713 shader.appendFormat(gFS_Main_AddDitherToGradient, gFS_Main_Dither[mHasES3]);
Romain Guyac670c02010-07-27 17:39:27 -0700714 }
715 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700716 if (description.isPoint) {
717 shader.append(gFS_Main_PointBitmapTexCoords);
718 }
Romain Guy889f8d12010-07-29 14:37:42 -0700719 if (!description.isBitmapNpot) {
720 shader.append(gFS_Main_FetchBitmap);
721 } else {
722 shader.append(gFS_Main_FetchBitmapNpot);
723 }
Romain Guyac670c02010-07-27 17:39:27 -0700724 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700725 bool applyModulate = false;
Romain Guyac670c02010-07-27 17:39:27 -0700726 // Case when we have two shaders set
727 if (description.hasGradient && description.hasBitmap) {
728 if (description.isBitmapFirst) {
729 shader.append(gFS_Main_BlendShadersBG);
730 } else {
731 shader.append(gFS_Main_BlendShadersGB);
732 }
Romain Guya938f562012-09-13 20:31:08 -0700733 applyModulate = shaderOp(description, shader, modulateOp,
734 gFS_Main_BlendShaders_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700735 } else {
736 if (description.hasGradient) {
Romain Guya938f562012-09-13 20:31:08 -0700737 applyModulate = shaderOp(description, shader, modulateOp,
738 gFS_Main_GradientShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700739 } else if (description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700740 applyModulate = shaderOp(description, shader, modulateOp,
741 gFS_Main_BitmapShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700742 }
Romain Guyac670c02010-07-27 17:39:27 -0700743 }
Romain Guya938f562012-09-13 20:31:08 -0700744
Romain Guy740bf2b2011-04-26 15:33:10 -0700745 if (description.modulate && applyModulate) {
Romain Guya938f562012-09-13 20:31:08 -0700746 shader.append(gFS_Main_ModulateColor);
Romain Guy740bf2b2011-04-26 15:33:10 -0700747 }
Romain Guya938f562012-09-13 20:31:08 -0700748
Romain Guyac670c02010-07-27 17:39:27 -0700749 // Apply the color op if needed
750 shader.append(gFS_Main_ApplyColorOp[description.colorOp]);
Chris Craik9f44a132012-09-13 18:34:55 -0700751
Chris Craik710f46d2012-09-17 17:25:49 -0700752 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800753 shader.append(gFS_Main_AccountForAAVertexShape);
Chris Craik9f44a132012-09-13 18:34:55 -0700754 }
755
Romain Guyac670c02010-07-27 17:39:27 -0700756 // Output the fragment
Romain Guya5aed0d2010-09-09 14:42:43 -0700757 if (!blendFramebuffer) {
758 shader.append(gFS_Main_FragColor);
759 } else {
Romain Guyf607bdc2010-09-10 19:20:06 -0700760 shader.append(!description.swapSrcDst ?
761 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap);
Romain Guya5aed0d2010-09-09 14:42:43 -0700762 }
Romain Guyff316ec2013-02-13 18:39:43 -0800763 if (description.hasColors) {
764 shader.append(gFS_Main_FragColor_HasColors);
765 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800766 if (description.hasDebugHighlight) {
767 shader.append(gFS_Main_DebugHighlight);
768 }
Romain Guyac670c02010-07-27 17:39:27 -0700769 }
Romain Guy78dd96d2013-05-03 14:24:16 -0700770 if (description.emulateStencil) {
771 shader.append(gFS_Footer_EmulateStencil);
772 }
Romain Guyac670c02010-07-27 17:39:27 -0700773 // End the shader
774 shader.append(gFS_Footer);
775
Romain Guyc15008e2010-11-10 11:59:15 -0800776#if DEBUG_PROGRAMS
Romain Guydb1938e2010-08-02 18:50:22 -0700777 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
778 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800779#endif
Romain Guydb1938e2010-08-02 18:50:22 -0700780
Romain Guyac670c02010-07-27 17:39:27 -0700781 return shader;
782}
783
Romain Guy48daa542010-08-10 19:21:34 -0700784void ProgramCache::generateBlend(String8& shader, const char* name, SkXfermode::Mode mode) {
Romain Guyac670c02010-07-27 17:39:27 -0700785 shader.append("\nvec4 ");
786 shader.append(name);
787 shader.append("(vec4 src, vec4 dst) {\n");
788 shader.append(" ");
Romain Guy48daa542010-08-10 19:21:34 -0700789 shader.append(gBlendOps[mode]);
Romain Guyac670c02010-07-27 17:39:27 -0700790 shader.append("}\n");
791}
792
Romain Guy889f8d12010-07-29 14:37:42 -0700793void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) {
Romain Guy63553472012-07-18 20:04:14 -0700794 shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700795 if (wrapS == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700796 shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700797 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n");
798 }
799 if (wrapT == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700800 shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700801 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n");
802 }
803 shader.append(" return vec2(");
804 switch (wrapS) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700805 case GL_CLAMP_TO_EDGE:
806 shader.append("texCoords.x");
807 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700808 case GL_REPEAT:
809 shader.append("mod(texCoords.x, 1.0)");
810 break;
811 case GL_MIRRORED_REPEAT:
812 shader.append("xMod2");
813 break;
814 }
815 shader.append(", ");
816 switch (wrapT) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700817 case GL_CLAMP_TO_EDGE:
818 shader.append("texCoords.y");
819 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700820 case GL_REPEAT:
821 shader.append("mod(texCoords.y, 1.0)");
822 break;
823 case GL_MIRRORED_REPEAT:
824 shader.append("yMod2");
825 break;
826 }
827 shader.append(");\n");
828 shader.append("}\n");
829}
830
Romain Guydb1938e2010-08-02 18:50:22 -0700831void ProgramCache::printLongString(const String8& shader) const {
832 ssize_t index = 0;
833 ssize_t lastIndex = 0;
834 const char* str = shader.string();
835 while ((index = shader.find("\n", index)) > -1) {
836 String8 line(str, index - lastIndex);
837 if (line.length() == 0) line.append("\n");
838 PROGRAM_LOGD("%s", line.string());
839 index++;
840 str += (index - lastIndex);
841 lastIndex = index;
842 }
843}
844
Romain Guyac670c02010-07-27 17:39:27 -0700845}; // namespace uirenderer
846}; // namespace android