blob: a5ce6f603663c4b141b4f7975050d32d2c078679 [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 Guyb4880042013-04-05 11:17:55 -070056const char* gVS_Header_Uniforms_HasGradient =
57 "uniform mat4 screenSpace;\n";
Romain Guy889f8d12010-07-29 14:37:42 -070058const char* gVS_Header_Uniforms_HasBitmap =
59 "uniform mat4 textureTransform;\n"
Romain Guy80bbfb12011-03-23 16:56:28 -070060 "uniform mediump vec2 textureDimension;\n";
Romain Guyac670c02010-07-27 17:39:27 -070061const char* gVS_Header_Varyings_HasTexture =
62 "varying vec2 outTexCoords;\n";
Romain Guyff316ec2013-02-13 18:39:43 -080063const char* gVS_Header_Varyings_HasColors =
64 "varying vec4 outColors;\n";
Chris Craik710f46d2012-09-17 17:25:49 -070065const char* gVS_Header_Varyings_IsAAVertexShape =
Chris Craik6ebdc112012-08-31 18:24:33 -070066 "varying float alpha;\n";
Romain Guy63553472012-07-18 20:04:14 -070067const char* gVS_Header_Varyings_HasBitmap =
68 "varying highp vec2 outBitmapTexCoords;\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -070069const char* gVS_Header_Varyings_HasGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -070070 // Linear
Chet Haasea1d12dd2012-09-21 14:50:14 -070071 "varying highp vec2 linear;\n"
72 "varying vec2 ditherTexCoords;\n",
73 "varying float linear;\n"
74 "varying vec2 ditherTexCoords;\n",
Romain Guy211efea2012-07-31 21:16:07 -070075
Romain Guyee916f12010-09-20 17:53:08 -070076 // Circular
Chet Haasea1d12dd2012-09-21 14:50:14 -070077 "varying highp vec2 circular;\n"
78 "varying vec2 ditherTexCoords;\n",
79 "varying highp vec2 circular;\n"
80 "varying vec2 ditherTexCoords;\n",
Romain Guy211efea2012-07-31 21:16:07 -070081
Romain Guyee916f12010-09-20 17:53:08 -070082 // Sweep
Chet Haasea1d12dd2012-09-21 14:50:14 -070083 "varying highp vec2 sweep;\n"
84 "varying vec2 ditherTexCoords;\n",
85 "varying highp vec2 sweep;\n"
86 "varying vec2 ditherTexCoords;\n",
Romain Guyee916f12010-09-20 17:53:08 -070087};
Romain Guyac670c02010-07-27 17:39:27 -070088const char* gVS_Main =
89 "\nvoid main(void) {\n";
90const char* gVS_Main_OutTexCoords =
91 " outTexCoords = texCoords;\n";
Romain Guyff316ec2013-02-13 18:39:43 -080092const char* gVS_Main_OutColors =
93 " outColors = colors;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -070094const char* gVS_Main_OutTransformedTexCoords =
95 " outTexCoords = (mainTextureTransform * vec4(texCoords, 0.0, 1.0)).xy;\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -070096const char* gVS_Main_OutGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -070097 // Linear
Chet Haasea1d12dd2012-09-21 14:50:14 -070098 " linear = vec2((screenSpace * position).x, 0.5);\n"
Romain Guyb4880042013-04-05 11:17:55 -070099 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Chet Haasea1d12dd2012-09-21 14:50:14 -0700100 " linear = (screenSpace * position).x;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700101 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Romain Guy211efea2012-07-31 21:16:07 -0700102
Romain Guyee916f12010-09-20 17:53:08 -0700103 // Circular
Chet Haasea1d12dd2012-09-21 14:50:14 -0700104 " circular = (screenSpace * position).xy;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700105 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Chet Haasea1d12dd2012-09-21 14:50:14 -0700106 " circular = (screenSpace * position).xy;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700107 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Romain Guy211efea2012-07-31 21:16:07 -0700108
Romain Guyee916f12010-09-20 17:53:08 -0700109 // Sweep
Chet Haasea1d12dd2012-09-21 14:50:14 -0700110 " sweep = (screenSpace * position).xy;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700111 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Chet Haasea1d12dd2012-09-21 14:50:14 -0700112 " sweep = (screenSpace * position).xy;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700113 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Romain Guyee916f12010-09-20 17:53:08 -0700114};
Romain Guy889f8d12010-07-29 14:37:42 -0700115const char* gVS_Main_OutBitmapTexCoords =
Romain Guy707b2f72010-10-11 16:34:59 -0700116 " outBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700117const char* gVS_Main_Position =
Romain Guy39284b72012-09-26 16:39:40 -0700118 " gl_Position = projection * transform * position;\n";
Chris Craik710f46d2012-09-17 17:25:49 -0700119const char* gVS_Main_AAVertexShape =
Chris Craik6ebdc112012-08-31 18:24:33 -0700120 " alpha = vtxAlpha;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700121const char* gVS_Footer =
122 "}\n\n";
123
124///////////////////////////////////////////////////////////////////////////////
125// Fragment shaders snippets
126///////////////////////////////////////////////////////////////////////////////
127
Romain Guya5aed0d2010-09-09 14:42:43 -0700128const char* gFS_Header_Extension_FramebufferFetch =
129 "#extension GL_NV_shader_framebuffer_fetch : enable\n\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700130const char* gFS_Header_Extension_ExternalTexture =
131 "#extension GL_OES_EGL_image_external : require\n\n";
Romain Guyac670c02010-07-27 17:39:27 -0700132const char* gFS_Header =
133 "precision mediump float;\n\n";
134const char* gFS_Uniforms_Color =
135 "uniform vec4 color;\n";
136const char* gFS_Uniforms_TextureSampler =
Romain Guya938f562012-09-13 20:31:08 -0700137 "uniform sampler2D baseSampler;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700138const char* gFS_Uniforms_ExternalTextureSampler =
Romain Guya938f562012-09-13 20:31:08 -0700139 "uniform samplerExternalOES baseSampler;\n";
Romain Guyb4880042013-04-05 11:17:55 -0700140const char* gFS_Uniforms_Dither =
141 "uniform sampler2D ditherSampler;";
142const char* gFS_Uniforms_GradientSampler[2] = {
143 "%s\n"
144 "uniform sampler2D gradientSampler;\n",
145 "%s\n"
146 "uniform vec4 startColor;\n"
Romain Guy211efea2012-07-31 21:16:07 -0700147 "uniform vec4 endColor;\n"
Romain Guyee916f12010-09-20 17:53:08 -0700148};
Romain Guyac670c02010-07-27 17:39:27 -0700149const char* gFS_Uniforms_BitmapSampler =
150 "uniform sampler2D bitmapSampler;\n";
151const char* gFS_Uniforms_ColorOp[4] = {
152 // None
153 "",
154 // Matrix
155 "uniform mat4 colorMatrix;\n"
156 "uniform vec4 colorMatrixVector;\n",
157 // Lighting
Romain Guydb1938e2010-08-02 18:50:22 -0700158 "uniform vec4 lightingMul;\n"
159 "uniform vec4 lightingAdd;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700160 // PorterDuff
Romain Guydb1938e2010-08-02 18:50:22 -0700161 "uniform vec4 colorBlend;\n"
Romain Guyac670c02010-07-27 17:39:27 -0700162};
Romain Guy41210632012-07-16 17:04:24 -0700163const char* gFS_Uniforms_Gamma =
164 "uniform float gamma;\n";
165
Romain Guyac670c02010-07-27 17:39:27 -0700166const char* gFS_Main =
167 "\nvoid main(void) {\n"
Romain Guy7fbcc042010-08-04 15:40:07 -0700168 " lowp vec4 fragColor;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700169
Romain Guyb4880042013-04-05 11:17:55 -0700170const char* gFS_Main_Dither[2] = {
171 // ES 2.0
172 "texture2D(ditherSampler, ditherTexCoords).a * " STR(DITHER_KERNEL_SIZE_INV_SQUARE),
173 // ES 3.0
Romain Guy032d47a2013-04-08 19:45:40 -0700174 "texture2D(ditherSampler, ditherTexCoords).a"
Romain Guyb4880042013-04-05 11:17:55 -0700175};
Romain Guy211efea2012-07-31 21:16:07 -0700176const char* gFS_Main_AddDitherToGradient =
Romain Guyb4880042013-04-05 11:17:55 -0700177 " gradientColor += %s;\n";
Romain Guy211efea2012-07-31 21:16:07 -0700178
Romain Guy707b2f72010-10-11 16:34:59 -0700179// Fast cases
180const char* gFS_Fast_SingleColor =
181 "\nvoid main(void) {\n"
182 " gl_FragColor = color;\n"
183 "}\n\n";
184const char* gFS_Fast_SingleTexture =
185 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700186 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700187 "}\n\n";
188const char* gFS_Fast_SingleModulateTexture =
189 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700190 " gl_FragColor = color.a * texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700191 "}\n\n";
192const char* gFS_Fast_SingleA8Texture =
193 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700194 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700195 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700196const char* gFS_Fast_SingleA8Texture_ApplyGamma =
197 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700198 " gl_FragColor = vec4(0.0, 0.0, 0.0, pow(texture2D(baseSampler, outTexCoords).a, gamma));\n"
Romain Guy41210632012-07-16 17:04:24 -0700199 "}\n\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700200const char* gFS_Fast_SingleModulateA8Texture =
201 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700202 " gl_FragColor = color * texture2D(baseSampler, outTexCoords).a;\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700203 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700204const char* gFS_Fast_SingleModulateA8Texture_ApplyGamma =
205 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700206 " gl_FragColor = color * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy41210632012-07-16 17:04:24 -0700207 "}\n\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -0700208const char* gFS_Fast_SingleGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700209 "\nvoid main(void) {\n"
Romain Guyb4880042013-04-05 11:17:55 -0700210 " gl_FragColor = %s + texture2D(gradientSampler, linear);\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700211 "}\n\n",
212 "\nvoid main(void) {\n"
Romain Guyb4880042013-04-05 11:17:55 -0700213 " gl_FragColor = %s + mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
214 "}\n\n",
Romain Guy42e1e0d2012-07-30 14:47:51 -0700215};
216const char* gFS_Fast_SingleModulateGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700217 "\nvoid main(void) {\n"
Romain Guyb4880042013-04-05 11:17:55 -0700218 " gl_FragColor = %s + color.a * texture2D(gradientSampler, linear);\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700219 "}\n\n",
220 "\nvoid main(void) {\n"
Romain Guyb4880042013-04-05 11:17:55 -0700221 " gl_FragColor = %s + color.a * mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700222 "}\n\n"
223};
Romain Guy707b2f72010-10-11 16:34:59 -0700224
225// General case
Romain Guyac670c02010-07-27 17:39:27 -0700226const char* gFS_Main_FetchColor =
227 " fragColor = color;\n";
Romain Guy740bf2b2011-04-26 15:33:10 -0700228const char* gFS_Main_ModulateColor =
229 " fragColor *= color.a;\n";
Chris Craik710f46d2012-09-17 17:25:49 -0700230const char* gFS_Main_AccountForAAVertexShape =
Chris Craik6ebdc112012-08-31 18:24:33 -0700231 " fragColor *= alpha;\n";
Chris Craika798b952012-08-27 17:03:13 -0700232
Romain Guy707b2f72010-10-11 16:34:59 -0700233const char* gFS_Main_FetchTexture[2] = {
234 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700235 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700236 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700237 " fragColor = color * texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700238};
Romain Guya938f562012-09-13 20:31:08 -0700239const char* gFS_Main_FetchA8Texture[4] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700240 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700241 " fragColor = texture2D(baseSampler, outTexCoords);\n",
242 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700243 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700244 " fragColor = color * texture2D(baseSampler, outTexCoords).a;\n",
245 " fragColor = color * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700246};
Romain Guy42e1e0d2012-07-30 14:47:51 -0700247const char* gFS_Main_FetchGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700248 // Linear
Romain Guy320d46b2012-08-08 16:05:42 -0700249 " vec4 gradientColor = texture2D(gradientSampler, linear);\n",
Romain Guy211efea2012-07-31 21:16:07 -0700250
Romain Guy320d46b2012-08-08 16:05:42 -0700251 " vec4 gradientColor = mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700252
Romain Guyee916f12010-09-20 17:53:08 -0700253 // Circular
Romain Guy320d46b2012-08-08 16:05:42 -0700254 " vec4 gradientColor = texture2D(gradientSampler, vec2(length(circular), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700255
Romain Guy320d46b2012-08-08 16:05:42 -0700256 " vec4 gradientColor = mix(startColor, endColor, clamp(length(circular), 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700257
Romain Guyee916f12010-09-20 17:53:08 -0700258 // Sweep
Romain Guy63553472012-07-18 20:04:14 -0700259 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700260 " vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700261
Romain Guy42e1e0d2012-07-30 14:47:51 -0700262 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700263 " vec4 gradientColor = mix(startColor, endColor, clamp(index - floor(index), 0.0, 1.0));\n"
Romain Guyee916f12010-09-20 17:53:08 -0700264};
Romain Guyac670c02010-07-27 17:39:27 -0700265const char* gFS_Main_FetchBitmap =
266 " vec4 bitmapColor = texture2D(bitmapSampler, outBitmapTexCoords);\n";
Romain Guy889f8d12010-07-29 14:37:42 -0700267const char* gFS_Main_FetchBitmapNpot =
268 " vec4 bitmapColor = texture2D(bitmapSampler, wrap(outBitmapTexCoords));\n";
Romain Guyac670c02010-07-27 17:39:27 -0700269const char* gFS_Main_BlendShadersBG =
Romain Guyac670c02010-07-27 17:39:27 -0700270 " fragColor = blendShaders(gradientColor, bitmapColor)";
Romain Guy06f96e22010-07-30 19:18:16 -0700271const char* gFS_Main_BlendShadersGB =
272 " fragColor = blendShaders(bitmapColor, gradientColor)";
Romain Guya938f562012-09-13 20:31:08 -0700273const char* gFS_Main_BlendShaders_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700274 // Don't modulate
275 ";\n",
Romain Guya938f562012-09-13 20:31:08 -0700276 ";\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700277 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700278 " * color.a;\n",
279 " * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700280 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700281 " * texture2D(baseSampler, outTexCoords).a;\n",
282 " * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700283};
Romain Guya938f562012-09-13 20:31:08 -0700284const char* gFS_Main_GradientShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700285 // Don't modulate
286 " fragColor = gradientColor;\n",
Romain Guya938f562012-09-13 20:31:08 -0700287 " fragColor = gradientColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700288 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700289 " fragColor = gradientColor * color.a;\n",
290 " fragColor = gradientColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700291 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700292 " fragColor = gradientColor * texture2D(baseSampler, outTexCoords).a;\n",
293 " fragColor = gradientColor * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700294 };
Romain Guya938f562012-09-13 20:31:08 -0700295const char* gFS_Main_BitmapShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700296 // Don't modulate
297 " fragColor = bitmapColor;\n",
Romain Guya938f562012-09-13 20:31:08 -0700298 " fragColor = bitmapColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700299 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700300 " fragColor = bitmapColor * color.a;\n",
301 " fragColor = bitmapColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700302 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700303 " fragColor = bitmapColor * texture2D(baseSampler, outTexCoords).a;\n",
304 " fragColor = bitmapColor * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700305 };
Romain Guyac670c02010-07-27 17:39:27 -0700306const char* gFS_Main_FragColor =
307 " gl_FragColor = fragColor;\n";
Romain Guyff316ec2013-02-13 18:39:43 -0800308const char* gFS_Main_FragColor_HasColors =
309 " gl_FragColor *= outColors;\n";
Romain Guya5aed0d2010-09-09 14:42:43 -0700310const char* gFS_Main_FragColor_Blend =
311 " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n";
Romain Guyf607bdc2010-09-10 19:20:06 -0700312const char* gFS_Main_FragColor_Blend_Swap =
313 " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n";
Romain Guyac670c02010-07-27 17:39:27 -0700314const char* gFS_Main_ApplyColorOp[4] = {
315 // None
316 "",
317 // Matrix
318 " fragColor *= colorMatrix;\n"
Romain Guydb1938e2010-08-02 18:50:22 -0700319 " fragColor += colorMatrixVector;\n"
320 " fragColor.rgb *= fragColor.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700321 // Lighting
Romain Guydb1938e2010-08-02 18:50:22 -0700322 " float lightingAlpha = fragColor.a;\n"
323 " fragColor = min(fragColor * lightingMul + (lightingAdd * lightingAlpha), lightingAlpha);\n"
324 " fragColor.a = lightingAlpha;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700325 // PorterDuff
326 " fragColor = blendColors(colorBlend, fragColor);\n"
327};
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800328const char* gFS_Main_DebugHighlight =
329 " gl_FragColor.rgb = vec3(0.0, gl_FragColor.a, 0.0);\n";
Romain Guy78dd96d2013-05-03 14:24:16 -0700330const char* gFS_Main_EmulateStencil =
331 " gl_FragColor.rgba = vec4(1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0, 1.0);\n"
332 " return;\n"
333 " /*\n";
334const char* gFS_Footer_EmulateStencil =
335 " */\n";
Romain Guyac670c02010-07-27 17:39:27 -0700336const char* gFS_Footer =
337 "}\n\n";
338
339///////////////////////////////////////////////////////////////////////////////
340// PorterDuff snippets
341///////////////////////////////////////////////////////////////////////////////
342
Romain Guy48daa542010-08-10 19:21:34 -0700343const char* gBlendOps[18] = {
Romain Guyac670c02010-07-27 17:39:27 -0700344 // Clear
345 "return vec4(0.0, 0.0, 0.0, 0.0);\n",
346 // Src
347 "return src;\n",
348 // Dst
349 "return dst;\n",
350 // SrcOver
Romain Guy06f96e22010-07-30 19:18:16 -0700351 "return src + dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700352 // DstOver
Romain Guy06f96e22010-07-30 19:18:16 -0700353 "return dst + src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700354 // SrcIn
Romain Guy06f96e22010-07-30 19:18:16 -0700355 "return src * dst.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700356 // DstIn
Romain Guy06f96e22010-07-30 19:18:16 -0700357 "return dst * src.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700358 // SrcOut
Romain Guy06f96e22010-07-30 19:18:16 -0700359 "return src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700360 // DstOut
Romain Guy06f96e22010-07-30 19:18:16 -0700361 "return dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700362 // SrcAtop
363 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n",
364 // DstAtop
365 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n",
366 // Xor
Romain Guy48daa542010-08-10 19:21:34 -0700367 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, "
Romain Guyac670c02010-07-27 17:39:27 -0700368 "src.a + dst.a - 2.0 * src.a * dst.a);\n",
Romain Guy48daa542010-08-10 19:21:34 -0700369 // Add
370 "return min(src + dst, 1.0);\n",
371 // Multiply
372 "return src * dst;\n",
373 // Screen
374 "return src + dst - src * dst;\n",
375 // Overlay
376 "return clamp(vec4(mix("
377 "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), "
378 "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), "
379 "step(dst.a, 2.0 * dst.rgb)), "
380 "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n",
381 // Darken
382 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
383 "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
384 // Lighten
385 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
386 "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 -0700387};
388
389///////////////////////////////////////////////////////////////////////////////
390// Constructors/destructors
391///////////////////////////////////////////////////////////////////////////////
392
Romain Guyb4880042013-04-05 11:17:55 -0700393ProgramCache::ProgramCache(): mHasES3(Extensions::getInstance().getMajorGlVersion() >= 3) {
Romain Guyac670c02010-07-27 17:39:27 -0700394}
395
396ProgramCache::~ProgramCache() {
397 clear();
398}
399
400///////////////////////////////////////////////////////////////////////////////
401// Cache management
402///////////////////////////////////////////////////////////////////////////////
403
404void ProgramCache::clear() {
Romain Guy67f27952010-12-07 20:09:23 -0800405 PROGRAM_LOGD("Clearing program cache");
406
Romain Guyac670c02010-07-27 17:39:27 -0700407 size_t count = mCache.size();
408 for (size_t i = 0; i < count; i++) {
409 delete mCache.valueAt(i);
410 }
411 mCache.clear();
412}
413
414Program* ProgramCache::get(const ProgramDescription& description) {
415 programid key = description.key();
Chris Craik096b8d92013-03-01 11:08:11 -0800416 if (key == (PROGRAM_KEY_TEXTURE | PROGRAM_KEY_A8_TEXTURE)) {
417 // program for A8, unmodulated, texture w/o shader (black text/path textures) is equivalent
418 // to standard texture program (bitmaps, patches). Consider them equivalent.
419 key = PROGRAM_KEY_TEXTURE;
420 }
421
Romain Guyac670c02010-07-27 17:39:27 -0700422 ssize_t index = mCache.indexOfKey(key);
423 Program* program = NULL;
424 if (index < 0) {
Romain Guyee916f12010-09-20 17:53:08 -0700425 description.log("Could not find program");
Romain Guyac670c02010-07-27 17:39:27 -0700426 program = generateProgram(description, key);
427 mCache.add(key, program);
428 } else {
429 program = mCache.valueAt(index);
430 }
431 return program;
432}
433
434///////////////////////////////////////////////////////////////////////////////
435// Program generation
436///////////////////////////////////////////////////////////////////////////////
437
438Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) {
439 String8 vertexShader = generateVertexShader(description);
440 String8 fragmentShader = generateFragmentShader(description);
441
Romain Guy42e1e0d2012-07-30 14:47:51 -0700442 return new Program(description, vertexShader.string(), fragmentShader.string());
443}
444
445static inline size_t gradientIndex(const ProgramDescription& description) {
446 return description.gradientType * 2 + description.isSimpleGradient;
Romain Guyac670c02010-07-27 17:39:27 -0700447}
448
449String8 ProgramCache::generateVertexShader(const ProgramDescription& description) {
450 // Add attributes
451 String8 shader(gVS_Header_Attributes);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700452 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700453 shader.append(gVS_Header_Attributes_TexCoords);
454 }
Chris Craik710f46d2012-09-17 17:25:49 -0700455 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800456 shader.append(gVS_Header_Attributes_AAVertexShapeParameters);
Chet Haase5b0200b2011-04-13 17:58:08 -0700457 }
Romain Guyff316ec2013-02-13 18:39:43 -0800458 if (description.hasColors) {
459 shader.append(gVS_Header_Attributes_Colors);
460 }
Romain Guyac670c02010-07-27 17:39:27 -0700461 // Uniforms
462 shader.append(gVS_Header_Uniforms);
Romain Guy8f0095c2011-05-02 17:24:22 -0700463 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700464 shader.append(gVS_Header_Uniforms_TextureTransform);
465 }
Romain Guyac670c02010-07-27 17:39:27 -0700466 if (description.hasGradient) {
Romain Guyb4880042013-04-05 11:17:55 -0700467 shader.append(gVS_Header_Uniforms_HasGradient);
Romain Guyac670c02010-07-27 17:39:27 -0700468 }
Romain Guy889f8d12010-07-29 14:37:42 -0700469 if (description.hasBitmap) {
470 shader.append(gVS_Header_Uniforms_HasBitmap);
471 }
Romain Guyac670c02010-07-27 17:39:27 -0700472 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700473 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700474 shader.append(gVS_Header_Varyings_HasTexture);
475 }
Chris Craik710f46d2012-09-17 17:25:49 -0700476 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800477 shader.append(gVS_Header_Varyings_IsAAVertexShape);
Chet Haase5b0200b2011-04-13 17:58:08 -0700478 }
Romain Guyff316ec2013-02-13 18:39:43 -0800479 if (description.hasColors) {
480 shader.append(gVS_Header_Varyings_HasColors);
481 }
Romain Guyac670c02010-07-27 17:39:27 -0700482 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700483 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700484 }
485 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700486 shader.append(gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700487 }
488
489 // Begin the shader
490 shader.append(gVS_Main); {
Romain Guy8f0095c2011-05-02 17:24:22 -0700491 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700492 shader.append(gVS_Main_OutTransformedTexCoords);
Romain Guy8f0095c2011-05-02 17:24:22 -0700493 } else if (description.hasTexture || description.hasExternalTexture) {
494 shader.append(gVS_Main_OutTexCoords);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700495 }
Chris Craik710f46d2012-09-17 17:25:49 -0700496 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800497 shader.append(gVS_Main_AAVertexShape);
Chet Haase5b0200b2011-04-13 17:58:08 -0700498 }
Romain Guyff316ec2013-02-13 18:39:43 -0800499 if (description.hasColors) {
500 shader.append(gVS_Main_OutColors);
501 }
Romain Guy889f8d12010-07-29 14:37:42 -0700502 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700503 shader.append(gVS_Main_OutBitmapTexCoords);
Romain Guy889f8d12010-07-29 14:37:42 -0700504 }
Romain Guyac670c02010-07-27 17:39:27 -0700505 // Output transformed position
506 shader.append(gVS_Main_Position);
Chet Haasea1d12dd2012-09-21 14:50:14 -0700507 if (description.hasGradient) {
508 shader.append(gVS_Main_OutGradient[gradientIndex(description)]);
509 }
Romain Guyac670c02010-07-27 17:39:27 -0700510 }
511 // End the shader
512 shader.append(gVS_Footer);
513
514 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string());
515
516 return shader;
517}
518
Romain Guya938f562012-09-13 20:31:08 -0700519static bool shaderOp(const ProgramDescription& description, String8& shader,
520 const int modulateOp, const char** snippets) {
521 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
522 op = op * 2 + description.hasGammaCorrection;
523 shader.append(snippets[op]);
524 return description.hasAlpha8Texture;
525}
526
Romain Guyac670c02010-07-27 17:39:27 -0700527String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) {
Romain Guya5aed0d2010-09-09 14:42:43 -0700528 String8 shader;
529
Romain Guy707b2f72010-10-11 16:34:59 -0700530 const bool blendFramebuffer = description.framebufferMode >= SkXfermode::kPlus_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700531 if (blendFramebuffer) {
532 shader.append(gFS_Header_Extension_FramebufferFetch);
533 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700534 if (description.hasExternalTexture) {
535 shader.append(gFS_Header_Extension_ExternalTexture);
536 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700537
538 shader.append(gFS_Header);
Romain Guyac670c02010-07-27 17:39:27 -0700539
540 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700541 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700542 shader.append(gVS_Header_Varyings_HasTexture);
543 }
Chris Craik710f46d2012-09-17 17:25:49 -0700544 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800545 shader.append(gVS_Header_Varyings_IsAAVertexShape);
Chet Haase5b0200b2011-04-13 17:58:08 -0700546 }
Romain Guyff316ec2013-02-13 18:39:43 -0800547 if (description.hasColors) {
548 shader.append(gVS_Header_Varyings_HasColors);
549 }
Romain Guyac670c02010-07-27 17:39:27 -0700550 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700551 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700552 }
553 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700554 shader.append(gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700555 }
556
Romain Guyac670c02010-07-27 17:39:27 -0700557 // Uniforms
Romain Guy707b2f72010-10-11 16:34:59 -0700558 int modulateOp = MODULATE_OP_NO_MODULATE;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700559 const bool singleColor = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700560 !description.hasGradient && !description.hasBitmap;
561
562 if (description.modulate || singleColor) {
563 shader.append(gFS_Uniforms_Color);
564 if (!singleColor) modulateOp = MODULATE_OP_MODULATE;
565 }
Romain Guyac670c02010-07-27 17:39:27 -0700566 if (description.hasTexture) {
567 shader.append(gFS_Uniforms_TextureSampler);
Romain Guy8f0095c2011-05-02 17:24:22 -0700568 } else if (description.hasExternalTexture) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700569 shader.append(gFS_Uniforms_ExternalTextureSampler);
570 }
Romain Guyac670c02010-07-27 17:39:27 -0700571 if (description.hasGradient) {
Romain Guyb4880042013-04-05 11:17:55 -0700572 shader.appendFormat(gFS_Uniforms_GradientSampler[description.isSimpleGradient],
573 gFS_Uniforms_Dither);
Romain Guyac670c02010-07-27 17:39:27 -0700574 }
Romain Guy41210632012-07-16 17:04:24 -0700575 if (description.hasGammaCorrection) {
576 shader.append(gFS_Uniforms_Gamma);
577 }
Romain Guy707b2f72010-10-11 16:34:59 -0700578
579 // Optimization for common cases
Romain Guyff316ec2013-02-13 18:39:43 -0800580 if (!description.isAA && !blendFramebuffer && !description.hasColors &&
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800581 description.colorOp == ProgramDescription::kColorNone &&
Chris Craik6d29c8d2013-05-08 18:35:44 -0700582 !description.hasDebugHighlight && !description.emulateStencil) {
Romain Guy707b2f72010-10-11 16:34:59 -0700583 bool fast = false;
584
585 const bool noShader = !description.hasGradient && !description.hasBitmap;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700586 const bool singleTexture = (description.hasTexture || description.hasExternalTexture) &&
Romain Guy707b2f72010-10-11 16:34:59 -0700587 !description.hasAlpha8Texture && noShader;
588 const bool singleA8Texture = description.hasTexture &&
589 description.hasAlpha8Texture && noShader;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700590 const bool singleGradient = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700591 description.hasGradient && !description.hasBitmap &&
592 description.gradientType == ProgramDescription::kGradientLinear;
593
594 if (singleColor) {
595 shader.append(gFS_Fast_SingleColor);
596 fast = true;
597 } else if (singleTexture) {
598 if (!description.modulate) {
599 shader.append(gFS_Fast_SingleTexture);
600 } else {
601 shader.append(gFS_Fast_SingleModulateTexture);
602 }
603 fast = true;
604 } else if (singleA8Texture) {
605 if (!description.modulate) {
Romain Guy41210632012-07-16 17:04:24 -0700606 if (description.hasGammaCorrection) {
607 shader.append(gFS_Fast_SingleA8Texture_ApplyGamma);
608 } else {
609 shader.append(gFS_Fast_SingleA8Texture);
610 }
Romain Guy707b2f72010-10-11 16:34:59 -0700611 } else {
Romain Guy41210632012-07-16 17:04:24 -0700612 if (description.hasGammaCorrection) {
613 shader.append(gFS_Fast_SingleModulateA8Texture_ApplyGamma);
614 } else {
615 shader.append(gFS_Fast_SingleModulateA8Texture);
616 }
Romain Guy707b2f72010-10-11 16:34:59 -0700617 }
618 fast = true;
619 } else if (singleGradient) {
620 if (!description.modulate) {
Romain Guyb4880042013-04-05 11:17:55 -0700621 shader.appendFormat(gFS_Fast_SingleGradient[description.isSimpleGradient],
622 gFS_Main_Dither[mHasES3]);
Romain Guy707b2f72010-10-11 16:34:59 -0700623 } else {
Romain Guyb4880042013-04-05 11:17:55 -0700624 shader.appendFormat(gFS_Fast_SingleModulateGradient[description.isSimpleGradient],
625 gFS_Main_Dither[mHasES3]);
Romain Guy707b2f72010-10-11 16:34:59 -0700626 }
627 fast = true;
628 }
629
630 if (fast) {
Romain Guyc15008e2010-11-10 11:59:15 -0800631#if DEBUG_PROGRAMS
Romain Guy707b2f72010-10-11 16:34:59 -0700632 PROGRAM_LOGD("*** Fast case:\n");
633 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
634 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800635#endif
Romain Guy707b2f72010-10-11 16:34:59 -0700636
637 return shader;
638 }
639 }
640
Romain Guyac670c02010-07-27 17:39:27 -0700641 if (description.hasBitmap) {
642 shader.append(gFS_Uniforms_BitmapSampler);
643 }
644 shader.append(gFS_Uniforms_ColorOp[description.colorOp]);
645
646 // Generate required functions
647 if (description.hasGradient && description.hasBitmap) {
Romain Guy48daa542010-08-10 19:21:34 -0700648 generateBlend(shader, "blendShaders", description.shadersMode);
Romain Guyac670c02010-07-27 17:39:27 -0700649 }
650 if (description.colorOp == ProgramDescription::kColorBlend) {
Romain Guy48daa542010-08-10 19:21:34 -0700651 generateBlend(shader, "blendColors", description.colorMode);
Romain Guyac670c02010-07-27 17:39:27 -0700652 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700653 if (blendFramebuffer) {
654 generateBlend(shader, "blendFramebuffer", description.framebufferMode);
655 }
Romain Guy889f8d12010-07-29 14:37:42 -0700656 if (description.isBitmapNpot) {
657 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
658 }
Romain Guyac670c02010-07-27 17:39:27 -0700659
660 // Begin the shader
661 shader.append(gFS_Main); {
Romain Guy78dd96d2013-05-03 14:24:16 -0700662 if (description.emulateStencil) {
663 shader.append(gFS_Main_EmulateStencil);
664 }
Romain Guyac670c02010-07-27 17:39:27 -0700665 // Stores the result in fragColor directly
Romain Guyaa6c24c2011-04-28 18:40:04 -0700666 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700667 if (description.hasAlpha8Texture) {
Romain Guy707b2f72010-10-11 16:34:59 -0700668 if (!description.hasGradient && !description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700669 shader.append(gFS_Main_FetchA8Texture[modulateOp * 2 +
670 description.hasGammaCorrection]);
Romain Guy707b2f72010-10-11 16:34:59 -0700671 }
Romain Guyac670c02010-07-27 17:39:27 -0700672 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700673 shader.append(gFS_Main_FetchTexture[modulateOp]);
Romain Guyac670c02010-07-27 17:39:27 -0700674 }
675 } else {
Romain Guya938f562012-09-13 20:31:08 -0700676 if (!description.hasGradient && !description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700677 shader.append(gFS_Main_FetchColor);
678 }
Romain Guyac670c02010-07-27 17:39:27 -0700679 }
680 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700681 shader.append(gFS_Main_FetchGradient[gradientIndex(description)]);
Romain Guyb4880042013-04-05 11:17:55 -0700682 shader.appendFormat(gFS_Main_AddDitherToGradient, gFS_Main_Dither[mHasES3]);
Romain Guyac670c02010-07-27 17:39:27 -0700683 }
684 if (description.hasBitmap) {
Romain Guy889f8d12010-07-29 14:37:42 -0700685 if (!description.isBitmapNpot) {
686 shader.append(gFS_Main_FetchBitmap);
687 } else {
688 shader.append(gFS_Main_FetchBitmapNpot);
689 }
Romain Guyac670c02010-07-27 17:39:27 -0700690 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700691 bool applyModulate = false;
Romain Guyac670c02010-07-27 17:39:27 -0700692 // Case when we have two shaders set
693 if (description.hasGradient && description.hasBitmap) {
694 if (description.isBitmapFirst) {
695 shader.append(gFS_Main_BlendShadersBG);
696 } else {
697 shader.append(gFS_Main_BlendShadersGB);
698 }
Romain Guya938f562012-09-13 20:31:08 -0700699 applyModulate = shaderOp(description, shader, modulateOp,
700 gFS_Main_BlendShaders_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700701 } else {
702 if (description.hasGradient) {
Romain Guya938f562012-09-13 20:31:08 -0700703 applyModulate = shaderOp(description, shader, modulateOp,
704 gFS_Main_GradientShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700705 } else if (description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700706 applyModulate = shaderOp(description, shader, modulateOp,
707 gFS_Main_BitmapShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700708 }
Romain Guyac670c02010-07-27 17:39:27 -0700709 }
Romain Guya938f562012-09-13 20:31:08 -0700710
Romain Guy740bf2b2011-04-26 15:33:10 -0700711 if (description.modulate && applyModulate) {
Romain Guya938f562012-09-13 20:31:08 -0700712 shader.append(gFS_Main_ModulateColor);
Romain Guy740bf2b2011-04-26 15:33:10 -0700713 }
Romain Guya938f562012-09-13 20:31:08 -0700714
Romain Guyac670c02010-07-27 17:39:27 -0700715 // Apply the color op if needed
716 shader.append(gFS_Main_ApplyColorOp[description.colorOp]);
Chris Craik9f44a132012-09-13 18:34:55 -0700717
Chris Craik710f46d2012-09-17 17:25:49 -0700718 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800719 shader.append(gFS_Main_AccountForAAVertexShape);
Chris Craik9f44a132012-09-13 18:34:55 -0700720 }
721
Romain Guyac670c02010-07-27 17:39:27 -0700722 // Output the fragment
Romain Guya5aed0d2010-09-09 14:42:43 -0700723 if (!blendFramebuffer) {
724 shader.append(gFS_Main_FragColor);
725 } else {
Romain Guyf607bdc2010-09-10 19:20:06 -0700726 shader.append(!description.swapSrcDst ?
727 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap);
Romain Guya5aed0d2010-09-09 14:42:43 -0700728 }
Romain Guyff316ec2013-02-13 18:39:43 -0800729 if (description.hasColors) {
730 shader.append(gFS_Main_FragColor_HasColors);
731 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800732 if (description.hasDebugHighlight) {
733 shader.append(gFS_Main_DebugHighlight);
734 }
Romain Guyac670c02010-07-27 17:39:27 -0700735 }
Romain Guy78dd96d2013-05-03 14:24:16 -0700736 if (description.emulateStencil) {
737 shader.append(gFS_Footer_EmulateStencil);
738 }
Romain Guyac670c02010-07-27 17:39:27 -0700739 // End the shader
740 shader.append(gFS_Footer);
741
Romain Guyc15008e2010-11-10 11:59:15 -0800742#if DEBUG_PROGRAMS
Romain Guydb1938e2010-08-02 18:50:22 -0700743 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
744 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800745#endif
Romain Guydb1938e2010-08-02 18:50:22 -0700746
Romain Guyac670c02010-07-27 17:39:27 -0700747 return shader;
748}
749
Romain Guy48daa542010-08-10 19:21:34 -0700750void ProgramCache::generateBlend(String8& shader, const char* name, SkXfermode::Mode mode) {
Romain Guyac670c02010-07-27 17:39:27 -0700751 shader.append("\nvec4 ");
752 shader.append(name);
753 shader.append("(vec4 src, vec4 dst) {\n");
754 shader.append(" ");
Romain Guy48daa542010-08-10 19:21:34 -0700755 shader.append(gBlendOps[mode]);
Romain Guyac670c02010-07-27 17:39:27 -0700756 shader.append("}\n");
757}
758
Romain Guy889f8d12010-07-29 14:37:42 -0700759void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) {
Romain Guy63553472012-07-18 20:04:14 -0700760 shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700761 if (wrapS == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700762 shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700763 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n");
764 }
765 if (wrapT == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700766 shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700767 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n");
768 }
769 shader.append(" return vec2(");
770 switch (wrapS) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700771 case GL_CLAMP_TO_EDGE:
772 shader.append("texCoords.x");
773 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700774 case GL_REPEAT:
775 shader.append("mod(texCoords.x, 1.0)");
776 break;
777 case GL_MIRRORED_REPEAT:
778 shader.append("xMod2");
779 break;
780 }
781 shader.append(", ");
782 switch (wrapT) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700783 case GL_CLAMP_TO_EDGE:
784 shader.append("texCoords.y");
785 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700786 case GL_REPEAT:
787 shader.append("mod(texCoords.y, 1.0)");
788 break;
789 case GL_MIRRORED_REPEAT:
790 shader.append("yMod2");
791 break;
792 }
793 shader.append(");\n");
794 shader.append("}\n");
795}
796
Romain Guydb1938e2010-08-02 18:50:22 -0700797void ProgramCache::printLongString(const String8& shader) const {
798 ssize_t index = 0;
799 ssize_t lastIndex = 0;
800 const char* str = shader.string();
801 while ((index = shader.find("\n", index)) > -1) {
802 String8 line(str, index - lastIndex);
803 if (line.length() == 0) line.append("\n");
804 PROGRAM_LOGD("%s", line.string());
805 index++;
806 str += (index - lastIndex);
807 lastIndex = index;
808 }
809}
810
Romain Guyac670c02010-07-27 17:39:27 -0700811}; // namespace uirenderer
812}; // namespace android