blob: 29cca36c9ce4a1299a92b1eddfa9e0c3f7a4b36d [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
Romain Guyac670c02010-07-27 17:39:27 -070017#include <utils/String8.h>
18
Romain Guya60c3882011-08-01 15:28:16 -070019#include "Caches.h"
Romain Guyb4880042013-04-05 11:17:55 -070020#include "Dither.h"
Romain Guyac670c02010-07-27 17:39:27 -070021#include "ProgramCache.h"
22
23namespace android {
24namespace uirenderer {
25
26///////////////////////////////////////////////////////////////////////////////
Romain Guy707b2f72010-10-11 16:34:59 -070027// Defines
28///////////////////////////////////////////////////////////////////////////////
29
30#define MODULATE_OP_NO_MODULATE 0
31#define MODULATE_OP_MODULATE 1
32#define MODULATE_OP_MODULATE_A8 2
33
Romain Guyb4880042013-04-05 11:17:55 -070034#define STR(x) STR1(x)
35#define STR1(x) #x
36
Romain Guy707b2f72010-10-11 16:34:59 -070037///////////////////////////////////////////////////////////////////////////////
Romain Guyac670c02010-07-27 17:39:27 -070038// Vertex shaders snippets
39///////////////////////////////////////////////////////////////////////////////
40
Chris Craik8bd68c62015-08-19 15:29:05 -070041const char* gVS_Header_Start =
42 "#version 100\n"
Romain Guyac670c02010-07-27 17:39:27 -070043 "attribute vec4 position;\n";
44const char* gVS_Header_Attributes_TexCoords =
45 "attribute vec2 texCoords;\n";
Romain Guyff316ec2013-02-13 18:39:43 -080046const char* gVS_Header_Attributes_Colors =
47 "attribute vec4 colors;\n";
Chris Craik91a8c7c2014-08-12 14:31:35 -070048const char* gVS_Header_Attributes_VertexAlphaParameters =
Chris Craik6ebdc112012-08-31 18:24:33 -070049 "attribute float vtxAlpha;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -070050const char* gVS_Header_Uniforms_TextureTransform =
51 "uniform mat4 mainTextureTransform;\n";
Romain Guyac670c02010-07-27 17:39:27 -070052const char* gVS_Header_Uniforms =
Romain Guy39284b72012-09-26 16:39:40 -070053 "uniform mat4 projection;\n" \
Romain Guyac670c02010-07-27 17:39:27 -070054 "uniform mat4 transform;\n";
Romain Guyb4880042013-04-05 11:17:55 -070055const char* gVS_Header_Uniforms_HasGradient =
56 "uniform mat4 screenSpace;\n";
Romain Guy889f8d12010-07-29 14:37:42 -070057const char* gVS_Header_Uniforms_HasBitmap =
58 "uniform mat4 textureTransform;\n"
Romain Guy80bbfb12011-03-23 16:56:28 -070059 "uniform mediump vec2 textureDimension;\n";
Chris Craikdeeda3d2014-05-05 19:09:33 -070060const char* gVS_Header_Uniforms_HasRoundRectClip =
61 "uniform mat4 roundRectInvTransform;\n";
Romain Guyac670c02010-07-27 17:39:27 -070062const char* gVS_Header_Varyings_HasTexture =
63 "varying vec2 outTexCoords;\n";
Romain Guyff316ec2013-02-13 18:39:43 -080064const char* gVS_Header_Varyings_HasColors =
65 "varying vec4 outColors;\n";
Chris Craik91a8c7c2014-08-12 14:31:35 -070066const char* gVS_Header_Varyings_HasVertexAlpha =
Chris Craik6ebdc112012-08-31 18:24:33 -070067 "varying float alpha;\n";
Romain Guy63553472012-07-18 20:04:14 -070068const char* gVS_Header_Varyings_HasBitmap =
69 "varying highp vec2 outBitmapTexCoords;\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -070070const char* gVS_Header_Varyings_HasGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -070071 // Linear
Chet Haasea1d12dd2012-09-21 14:50:14 -070072 "varying highp vec2 linear;\n"
73 "varying vec2 ditherTexCoords;\n",
74 "varying float linear;\n"
75 "varying vec2 ditherTexCoords;\n",
Romain Guy211efea2012-07-31 21:16:07 -070076
Romain Guyee916f12010-09-20 17:53:08 -070077 // Circular
Chet Haasea1d12dd2012-09-21 14:50:14 -070078 "varying highp vec2 circular;\n"
79 "varying vec2 ditherTexCoords;\n",
80 "varying highp vec2 circular;\n"
81 "varying vec2 ditherTexCoords;\n",
Romain Guy211efea2012-07-31 21:16:07 -070082
Romain Guyee916f12010-09-20 17:53:08 -070083 // Sweep
Chet Haasea1d12dd2012-09-21 14:50:14 -070084 "varying highp vec2 sweep;\n"
85 "varying vec2 ditherTexCoords;\n",
86 "varying highp vec2 sweep;\n"
87 "varying vec2 ditherTexCoords;\n",
Romain Guyee916f12010-09-20 17:53:08 -070088};
Chris Craikdeeda3d2014-05-05 19:09:33 -070089const char* gVS_Header_Varyings_HasRoundRectClip =
Chris Craik68a73e82014-08-29 17:06:27 -070090 "varying highp vec2 roundRectPos;\n";
Romain Guyac670c02010-07-27 17:39:27 -070091const char* gVS_Main =
92 "\nvoid main(void) {\n";
93const char* gVS_Main_OutTexCoords =
94 " outTexCoords = texCoords;\n";
Romain Guyff316ec2013-02-13 18:39:43 -080095const char* gVS_Main_OutColors =
96 " outColors = colors;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -070097const char* gVS_Main_OutTransformedTexCoords =
98 " outTexCoords = (mainTextureTransform * vec4(texCoords, 0.0, 1.0)).xy;\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -070099const char* gVS_Main_OutGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700100 // Linear
Chet Haasea1d12dd2012-09-21 14:50:14 -0700101 " linear = vec2((screenSpace * position).x, 0.5);\n"
Romain Guyb4880042013-04-05 11:17:55 -0700102 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Chet Haasea1d12dd2012-09-21 14:50:14 -0700103 " linear = (screenSpace * position).x;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700104 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Romain Guy211efea2012-07-31 21:16:07 -0700105
Romain Guyee916f12010-09-20 17:53:08 -0700106 // Circular
Chet Haasea1d12dd2012-09-21 14:50:14 -0700107 " circular = (screenSpace * position).xy;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700108 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Chet Haasea1d12dd2012-09-21 14:50:14 -0700109 " circular = (screenSpace * position).xy;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700110 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Romain Guy211efea2012-07-31 21:16:07 -0700111
Romain Guyee916f12010-09-20 17:53:08 -0700112 // Sweep
Chet Haasea1d12dd2012-09-21 14:50:14 -0700113 " sweep = (screenSpace * position).xy;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700114 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Chet Haasea1d12dd2012-09-21 14:50:14 -0700115 " sweep = (screenSpace * position).xy;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700116 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Romain Guyee916f12010-09-20 17:53:08 -0700117};
Romain Guy889f8d12010-07-29 14:37:42 -0700118const char* gVS_Main_OutBitmapTexCoords =
Romain Guy707b2f72010-10-11 16:34:59 -0700119 " outBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700120const char* gVS_Main_Position =
Chris Craikdeeda3d2014-05-05 19:09:33 -0700121 " vec4 transformedPosition = projection * transform * position;\n"
122 " gl_Position = transformedPosition;\n";
Chris Craikbf759452014-08-11 16:00:44 -0700123
Chris Craik91a8c7c2014-08-12 14:31:35 -0700124const char* gVS_Main_VertexAlpha =
Chris Craik6ebdc112012-08-31 18:24:33 -0700125 " alpha = vtxAlpha;\n";
Chris Craikbf759452014-08-11 16:00:44 -0700126
Chris Craikdeeda3d2014-05-05 19:09:33 -0700127const char* gVS_Main_HasRoundRectClip =
128 " roundRectPos = (roundRectInvTransform * transformedPosition).xy;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700129const char* gVS_Footer =
130 "}\n\n";
131
132///////////////////////////////////////////////////////////////////////////////
133// Fragment shaders snippets
134///////////////////////////////////////////////////////////////////////////////
135
Chris Craik8bd68c62015-08-19 15:29:05 -0700136const char* gFS_Header_Start =
137 "#version 100\n";
Romain Guya5aed0d2010-09-09 14:42:43 -0700138const char* gFS_Header_Extension_FramebufferFetch =
139 "#extension GL_NV_shader_framebuffer_fetch : enable\n\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700140const char* gFS_Header_Extension_ExternalTexture =
141 "#extension GL_OES_EGL_image_external : require\n\n";
Romain Guyac670c02010-07-27 17:39:27 -0700142const char* gFS_Header =
143 "precision mediump float;\n\n";
144const char* gFS_Uniforms_Color =
145 "uniform vec4 color;\n";
146const char* gFS_Uniforms_TextureSampler =
Romain Guya938f562012-09-13 20:31:08 -0700147 "uniform sampler2D baseSampler;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700148const char* gFS_Uniforms_ExternalTextureSampler =
Romain Guya938f562012-09-13 20:31:08 -0700149 "uniform samplerExternalOES baseSampler;\n";
Romain Guyb4880042013-04-05 11:17:55 -0700150const char* gFS_Uniforms_Dither =
151 "uniform sampler2D ditherSampler;";
152const char* gFS_Uniforms_GradientSampler[2] = {
153 "%s\n"
154 "uniform sampler2D gradientSampler;\n",
155 "%s\n"
156 "uniform vec4 startColor;\n"
Romain Guy211efea2012-07-31 21:16:07 -0700157 "uniform vec4 endColor;\n"
Romain Guyee916f12010-09-20 17:53:08 -0700158};
Romain Guyac670c02010-07-27 17:39:27 -0700159const char* gFS_Uniforms_BitmapSampler =
160 "uniform sampler2D bitmapSampler;\n";
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500161const char* gFS_Uniforms_ColorOp[3] = {
Romain Guyac670c02010-07-27 17:39:27 -0700162 // None
163 "",
164 // Matrix
165 "uniform mat4 colorMatrix;\n"
166 "uniform vec4 colorMatrixVector;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700167 // PorterDuff
Romain Guydb1938e2010-08-02 18:50:22 -0700168 "uniform vec4 colorBlend;\n"
Romain Guyac670c02010-07-27 17:39:27 -0700169};
Romain Guy41210632012-07-16 17:04:24 -0700170const char* gFS_Uniforms_Gamma =
171 "uniform float gamma;\n";
172
Chris Craikdeeda3d2014-05-05 19:09:33 -0700173const char* gFS_Uniforms_HasRoundRectClip =
174 "uniform vec4 roundRectInnerRectLTRB;\n"
175 "uniform float roundRectRadius;\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 Guyb4880042013-04-05 11:17:55 -0700181const char* gFS_Main_Dither[2] = {
182 // ES 2.0
183 "texture2D(ditherSampler, ditherTexCoords).a * " STR(DITHER_KERNEL_SIZE_INV_SQUARE),
184 // ES 3.0
Romain Guy032d47a2013-04-08 19:45:40 -0700185 "texture2D(ditherSampler, ditherTexCoords).a"
Romain Guyb4880042013-04-05 11:17:55 -0700186};
Romain Guy211efea2012-07-31 21:16:07 -0700187const char* gFS_Main_AddDitherToGradient =
Romain Guyb4880042013-04-05 11:17:55 -0700188 " gradientColor += %s;\n";
Romain Guy211efea2012-07-31 21:16:07 -0700189
Romain Guy707b2f72010-10-11 16:34:59 -0700190// Fast cases
191const char* gFS_Fast_SingleColor =
192 "\nvoid main(void) {\n"
193 " gl_FragColor = color;\n"
194 "}\n\n";
195const char* gFS_Fast_SingleTexture =
196 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700197 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700198 "}\n\n";
199const char* gFS_Fast_SingleModulateTexture =
200 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700201 " gl_FragColor = color.a * texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700202 "}\n\n";
203const char* gFS_Fast_SingleA8Texture =
204 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700205 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700206 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700207const char* gFS_Fast_SingleA8Texture_ApplyGamma =
208 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700209 " gl_FragColor = vec4(0.0, 0.0, 0.0, pow(texture2D(baseSampler, outTexCoords).a, gamma));\n"
Romain Guy41210632012-07-16 17:04:24 -0700210 "}\n\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700211const char* gFS_Fast_SingleModulateA8Texture =
212 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700213 " gl_FragColor = color * texture2D(baseSampler, outTexCoords).a;\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700214 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700215const char* gFS_Fast_SingleModulateA8Texture_ApplyGamma =
216 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700217 " gl_FragColor = color * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy41210632012-07-16 17:04:24 -0700218 "}\n\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -0700219const char* gFS_Fast_SingleGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700220 "\nvoid main(void) {\n"
Romain Guyb4880042013-04-05 11:17:55 -0700221 " gl_FragColor = %s + texture2D(gradientSampler, linear);\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700222 "}\n\n",
223 "\nvoid main(void) {\n"
Romain Guyb4880042013-04-05 11:17:55 -0700224 " gl_FragColor = %s + mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
225 "}\n\n",
Romain Guy42e1e0d2012-07-30 14:47:51 -0700226};
227const char* gFS_Fast_SingleModulateGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700228 "\nvoid main(void) {\n"
Romain Guyb4880042013-04-05 11:17:55 -0700229 " gl_FragColor = %s + color.a * texture2D(gradientSampler, linear);\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700230 "}\n\n",
231 "\nvoid main(void) {\n"
Romain Guyb4880042013-04-05 11:17:55 -0700232 " gl_FragColor = %s + color.a * mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700233 "}\n\n"
234};
Romain Guy707b2f72010-10-11 16:34:59 -0700235
236// General case
Romain Guyac670c02010-07-27 17:39:27 -0700237const char* gFS_Main_FetchColor =
238 " fragColor = color;\n";
Romain Guy740bf2b2011-04-26 15:33:10 -0700239const char* gFS_Main_ModulateColor =
240 " fragColor *= color.a;\n";
Chris Craik91a8c7c2014-08-12 14:31:35 -0700241const char* gFS_Main_ApplyVertexAlphaLinearInterp =
Chris Craik6ebdc112012-08-31 18:24:33 -0700242 " fragColor *= alpha;\n";
Chris Craik91a8c7c2014-08-12 14:31:35 -0700243const char* gFS_Main_ApplyVertexAlphaShadowInterp =
ztenghuiecf091e2015-02-17 13:26:10 -0800244 // Use a gaussian function for the shadow fall off. Note that alpha here
245 // is actually (1.0 - alpha) for saving computation.
246 " fragColor *= exp(- alpha * alpha * 4.0) - 0.018;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700247const char* gFS_Main_FetchTexture[2] = {
248 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700249 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700250 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700251 " fragColor = color * texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700252};
Romain Guya938f562012-09-13 20:31:08 -0700253const char* gFS_Main_FetchA8Texture[4] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700254 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700255 " fragColor = texture2D(baseSampler, outTexCoords);\n",
256 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700257 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700258 " fragColor = color * texture2D(baseSampler, outTexCoords).a;\n",
259 " fragColor = color * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700260};
Romain Guy42e1e0d2012-07-30 14:47:51 -0700261const char* gFS_Main_FetchGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700262 // Linear
Romain Guy320d46b2012-08-08 16:05:42 -0700263 " vec4 gradientColor = texture2D(gradientSampler, linear);\n",
Romain Guy211efea2012-07-31 21:16:07 -0700264
Romain Guy320d46b2012-08-08 16:05:42 -0700265 " vec4 gradientColor = mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700266
Romain Guyee916f12010-09-20 17:53:08 -0700267 // Circular
Romain Guy320d46b2012-08-08 16:05:42 -0700268 " vec4 gradientColor = texture2D(gradientSampler, vec2(length(circular), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700269
Romain Guy320d46b2012-08-08 16:05:42 -0700270 " vec4 gradientColor = mix(startColor, endColor, clamp(length(circular), 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700271
Romain Guyee916f12010-09-20 17:53:08 -0700272 // Sweep
Romain Guy63553472012-07-18 20:04:14 -0700273 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700274 " vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700275
Romain Guy42e1e0d2012-07-30 14:47:51 -0700276 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700277 " vec4 gradientColor = mix(startColor, endColor, clamp(index - floor(index), 0.0, 1.0));\n"
Romain Guyee916f12010-09-20 17:53:08 -0700278};
Romain Guyac670c02010-07-27 17:39:27 -0700279const char* gFS_Main_FetchBitmap =
280 " vec4 bitmapColor = texture2D(bitmapSampler, outBitmapTexCoords);\n";
Romain Guy889f8d12010-07-29 14:37:42 -0700281const char* gFS_Main_FetchBitmapNpot =
282 " vec4 bitmapColor = texture2D(bitmapSampler, wrap(outBitmapTexCoords));\n";
Romain Guyac670c02010-07-27 17:39:27 -0700283const char* gFS_Main_BlendShadersBG =
Romain Guyac670c02010-07-27 17:39:27 -0700284 " fragColor = blendShaders(gradientColor, bitmapColor)";
Romain Guy06f96e22010-07-30 19:18:16 -0700285const char* gFS_Main_BlendShadersGB =
286 " fragColor = blendShaders(bitmapColor, gradientColor)";
Romain Guya938f562012-09-13 20:31:08 -0700287const char* gFS_Main_BlendShaders_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700288 // Don't modulate
289 ";\n",
Romain Guya938f562012-09-13 20:31:08 -0700290 ";\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700291 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700292 " * color.a;\n",
293 " * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700294 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700295 " * texture2D(baseSampler, outTexCoords).a;\n",
296 " * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700297};
Romain Guya938f562012-09-13 20:31:08 -0700298const char* gFS_Main_GradientShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700299 // Don't modulate
300 " fragColor = gradientColor;\n",
Romain Guya938f562012-09-13 20:31:08 -0700301 " fragColor = gradientColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700302 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700303 " fragColor = gradientColor * color.a;\n",
304 " fragColor = gradientColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700305 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700306 " fragColor = gradientColor * texture2D(baseSampler, outTexCoords).a;\n",
307 " fragColor = gradientColor * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700308 };
Romain Guya938f562012-09-13 20:31:08 -0700309const char* gFS_Main_BitmapShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700310 // Don't modulate
311 " fragColor = bitmapColor;\n",
Romain Guya938f562012-09-13 20:31:08 -0700312 " fragColor = bitmapColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700313 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700314 " fragColor = bitmapColor * color.a;\n",
315 " fragColor = bitmapColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700316 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700317 " fragColor = bitmapColor * texture2D(baseSampler, outTexCoords).a;\n",
318 " fragColor = bitmapColor * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700319 };
Romain Guyac670c02010-07-27 17:39:27 -0700320const char* gFS_Main_FragColor =
321 " gl_FragColor = fragColor;\n";
Romain Guyff316ec2013-02-13 18:39:43 -0800322const char* gFS_Main_FragColor_HasColors =
323 " gl_FragColor *= outColors;\n";
Romain Guya5aed0d2010-09-09 14:42:43 -0700324const char* gFS_Main_FragColor_Blend =
325 " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n";
Romain Guyf607bdc2010-09-10 19:20:06 -0700326const char* gFS_Main_FragColor_Blend_Swap =
327 " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n";
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500328const char* gFS_Main_ApplyColorOp[3] = {
Romain Guyac670c02010-07-27 17:39:27 -0700329 // None
330 "",
331 // Matrix
Chris Craik73821c82014-09-16 17:32:13 -0700332 " fragColor.rgb /= (fragColor.a + 0.0019);\n" // un-premultiply
Romain Guyac670c02010-07-27 17:39:27 -0700333 " fragColor *= colorMatrix;\n"
Chris Craik73821c82014-09-16 17:32:13 -0700334 " fragColor += colorMatrixVector;\n"
335 " fragColor.rgb *= (fragColor.a + 0.0019);\n", // re-premultiply
Romain Guyac670c02010-07-27 17:39:27 -0700336 // PorterDuff
337 " fragColor = blendColors(colorBlend, fragColor);\n"
338};
Chris Craikdeeda3d2014-05-05 19:09:33 -0700339
340// Note: LTRB -> xyzw
341const char* gFS_Main_FragColor_HasRoundRectClip =
342 " mediump vec2 fragToLT = roundRectInnerRectLTRB.xy - roundRectPos;\n"
343 " mediump vec2 fragFromRB = roundRectPos - roundRectInnerRectLTRB.zw;\n"
Chris Craikf99f3202014-08-05 15:31:52 -0700344
345 // divide + multiply by 128 to avoid falling out of range in length() function
346 " mediump vec2 dist = max(max(fragToLT, fragFromRB), vec2(0.0, 0.0)) / 128.0;\n"
347 " mediump float linearDist = roundRectRadius - (length(dist) * 128.0);\n"
Chris Craikdeeda3d2014-05-05 19:09:33 -0700348 " gl_FragColor *= clamp(linearDist, 0.0, 1.0);\n";
349
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800350const char* gFS_Main_DebugHighlight =
351 " gl_FragColor.rgb = vec3(0.0, gl_FragColor.a, 0.0);\n";
Romain Guyac670c02010-07-27 17:39:27 -0700352const char* gFS_Footer =
353 "}\n\n";
354
355///////////////////////////////////////////////////////////////////////////////
356// PorterDuff snippets
357///////////////////////////////////////////////////////////////////////////////
358
Romain Guy48daa542010-08-10 19:21:34 -0700359const char* gBlendOps[18] = {
Romain Guyac670c02010-07-27 17:39:27 -0700360 // Clear
361 "return vec4(0.0, 0.0, 0.0, 0.0);\n",
362 // Src
363 "return src;\n",
364 // Dst
365 "return dst;\n",
366 // SrcOver
Romain Guy06f96e22010-07-30 19:18:16 -0700367 "return src + dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700368 // DstOver
Romain Guy06f96e22010-07-30 19:18:16 -0700369 "return dst + src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700370 // SrcIn
Romain Guy06f96e22010-07-30 19:18:16 -0700371 "return src * dst.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700372 // DstIn
Romain Guy06f96e22010-07-30 19:18:16 -0700373 "return dst * src.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700374 // SrcOut
Romain Guy06f96e22010-07-30 19:18:16 -0700375 "return src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700376 // DstOut
Romain Guy06f96e22010-07-30 19:18:16 -0700377 "return dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700378 // SrcAtop
379 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n",
380 // DstAtop
381 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n",
382 // Xor
Romain Guy48daa542010-08-10 19:21:34 -0700383 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, "
Romain Guyac670c02010-07-27 17:39:27 -0700384 "src.a + dst.a - 2.0 * src.a * dst.a);\n",
Derek Sollenbergerc0bf7002015-03-06 13:48:27 -0500385 // Plus
Romain Guy48daa542010-08-10 19:21:34 -0700386 "return min(src + dst, 1.0);\n",
Derek Sollenbergerc0bf7002015-03-06 13:48:27 -0500387 // Modulate
Romain Guy48daa542010-08-10 19:21:34 -0700388 "return src * dst;\n",
389 // Screen
390 "return src + dst - src * dst;\n",
391 // Overlay
392 "return clamp(vec4(mix("
393 "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), "
394 "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), "
395 "step(dst.a, 2.0 * dst.rgb)), "
396 "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n",
397 // Darken
398 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
399 "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
400 // Lighten
401 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
402 "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 -0700403};
404
405///////////////////////////////////////////////////////////////////////////////
406// Constructors/destructors
407///////////////////////////////////////////////////////////////////////////////
408
Chris Craik117bdbc2015-02-05 10:12:38 -0800409ProgramCache::ProgramCache(Extensions& extensions)
410 : mHasES3(extensions.getMajorGlVersion() >= 3) {
Romain Guyac670c02010-07-27 17:39:27 -0700411}
412
413ProgramCache::~ProgramCache() {
414 clear();
415}
416
417///////////////////////////////////////////////////////////////////////////////
418// Cache management
419///////////////////////////////////////////////////////////////////////////////
420
421void ProgramCache::clear() {
Romain Guy67f27952010-12-07 20:09:23 -0800422 PROGRAM_LOGD("Clearing program cache");
Romain Guyac670c02010-07-27 17:39:27 -0700423 mCache.clear();
424}
425
426Program* ProgramCache::get(const ProgramDescription& description) {
427 programid key = description.key();
Chris Craik096b8d92013-03-01 11:08:11 -0800428 if (key == (PROGRAM_KEY_TEXTURE | PROGRAM_KEY_A8_TEXTURE)) {
429 // program for A8, unmodulated, texture w/o shader (black text/path textures) is equivalent
430 // to standard texture program (bitmaps, patches). Consider them equivalent.
431 key = PROGRAM_KEY_TEXTURE;
432 }
433
Chris Craik51d6a3d2014-12-22 17:16:56 -0800434 auto iter = mCache.find(key);
Chris Craikd41c4d82015-01-05 15:51:13 -0800435 Program* program = nullptr;
Chris Craik51d6a3d2014-12-22 17:16:56 -0800436 if (iter == mCache.end()) {
Romain Guyee916f12010-09-20 17:53:08 -0700437 description.log("Could not find program");
Romain Guyac670c02010-07-27 17:39:27 -0700438 program = generateProgram(description, key);
Chris Craik51d6a3d2014-12-22 17:16:56 -0800439 mCache[key] = std::unique_ptr<Program>(program);
Romain Guyac670c02010-07-27 17:39:27 -0700440 } else {
Chris Craik51d6a3d2014-12-22 17:16:56 -0800441 program = iter->second.get();
Romain Guyac670c02010-07-27 17:39:27 -0700442 }
443 return program;
444}
445
446///////////////////////////////////////////////////////////////////////////////
447// Program generation
448///////////////////////////////////////////////////////////////////////////////
449
Andreas Gampe64bb4132014-11-22 00:35:09 +0000450Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) {
Romain Guyac670c02010-07-27 17:39:27 -0700451 String8 vertexShader = generateVertexShader(description);
452 String8 fragmentShader = generateFragmentShader(description);
453
Romain Guy42e1e0d2012-07-30 14:47:51 -0700454 return new Program(description, vertexShader.string(), fragmentShader.string());
455}
456
457static inline size_t gradientIndex(const ProgramDescription& description) {
458 return description.gradientType * 2 + description.isSimpleGradient;
Romain Guyac670c02010-07-27 17:39:27 -0700459}
460
461String8 ProgramCache::generateVertexShader(const ProgramDescription& description) {
462 // Add attributes
Chris Craik8bd68c62015-08-19 15:29:05 -0700463 String8 shader(gVS_Header_Start);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700464 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700465 shader.append(gVS_Header_Attributes_TexCoords);
466 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700467 if (description.hasVertexAlpha) {
468 shader.append(gVS_Header_Attributes_VertexAlphaParameters);
Chet Haase5b0200b2011-04-13 17:58:08 -0700469 }
Romain Guyff316ec2013-02-13 18:39:43 -0800470 if (description.hasColors) {
471 shader.append(gVS_Header_Attributes_Colors);
472 }
Romain Guyac670c02010-07-27 17:39:27 -0700473 // Uniforms
474 shader.append(gVS_Header_Uniforms);
Romain Guy8f0095c2011-05-02 17:24:22 -0700475 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700476 shader.append(gVS_Header_Uniforms_TextureTransform);
477 }
Romain Guyac670c02010-07-27 17:39:27 -0700478 if (description.hasGradient) {
Romain Guyb4880042013-04-05 11:17:55 -0700479 shader.append(gVS_Header_Uniforms_HasGradient);
Romain Guyac670c02010-07-27 17:39:27 -0700480 }
Romain Guy889f8d12010-07-29 14:37:42 -0700481 if (description.hasBitmap) {
482 shader.append(gVS_Header_Uniforms_HasBitmap);
483 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700484 if (description.hasRoundRectClip) {
485 shader.append(gVS_Header_Uniforms_HasRoundRectClip);
486 }
Romain Guyac670c02010-07-27 17:39:27 -0700487 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700488 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700489 shader.append(gVS_Header_Varyings_HasTexture);
490 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700491 if (description.hasVertexAlpha) {
492 shader.append(gVS_Header_Varyings_HasVertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700493 }
Romain Guyff316ec2013-02-13 18:39:43 -0800494 if (description.hasColors) {
495 shader.append(gVS_Header_Varyings_HasColors);
496 }
Romain Guyac670c02010-07-27 17:39:27 -0700497 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700498 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700499 }
500 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700501 shader.append(gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700502 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700503 if (description.hasRoundRectClip) {
504 shader.append(gVS_Header_Varyings_HasRoundRectClip);
505 }
Romain Guyac670c02010-07-27 17:39:27 -0700506
507 // Begin the shader
508 shader.append(gVS_Main); {
Romain Guy8f0095c2011-05-02 17:24:22 -0700509 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700510 shader.append(gVS_Main_OutTransformedTexCoords);
Romain Guy8f0095c2011-05-02 17:24:22 -0700511 } else if (description.hasTexture || description.hasExternalTexture) {
512 shader.append(gVS_Main_OutTexCoords);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700513 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700514 if (description.hasVertexAlpha) {
515 shader.append(gVS_Main_VertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700516 }
Romain Guyff316ec2013-02-13 18:39:43 -0800517 if (description.hasColors) {
518 shader.append(gVS_Main_OutColors);
519 }
Romain Guy889f8d12010-07-29 14:37:42 -0700520 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700521 shader.append(gVS_Main_OutBitmapTexCoords);
Romain Guy889f8d12010-07-29 14:37:42 -0700522 }
Romain Guyac670c02010-07-27 17:39:27 -0700523 // Output transformed position
524 shader.append(gVS_Main_Position);
Chet Haasea1d12dd2012-09-21 14:50:14 -0700525 if (description.hasGradient) {
526 shader.append(gVS_Main_OutGradient[gradientIndex(description)]);
527 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700528 if (description.hasRoundRectClip) {
529 shader.append(gVS_Main_HasRoundRectClip);
530 }
Romain Guyac670c02010-07-27 17:39:27 -0700531 }
532 // End the shader
533 shader.append(gVS_Footer);
534
535 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string());
536
537 return shader;
538}
539
Romain Guya938f562012-09-13 20:31:08 -0700540static bool shaderOp(const ProgramDescription& description, String8& shader,
541 const int modulateOp, const char** snippets) {
542 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
543 op = op * 2 + description.hasGammaCorrection;
544 shader.append(snippets[op]);
545 return description.hasAlpha8Texture;
546}
547
Romain Guyac670c02010-07-27 17:39:27 -0700548String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) {
Chris Craik8bd68c62015-08-19 15:29:05 -0700549 String8 shader(gFS_Header_Start);
Romain Guya5aed0d2010-09-09 14:42:43 -0700550
Romain Guy707b2f72010-10-11 16:34:59 -0700551 const bool blendFramebuffer = description.framebufferMode >= SkXfermode::kPlus_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700552 if (blendFramebuffer) {
553 shader.append(gFS_Header_Extension_FramebufferFetch);
554 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700555 if (description.hasExternalTexture) {
556 shader.append(gFS_Header_Extension_ExternalTexture);
557 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700558
559 shader.append(gFS_Header);
Romain Guyac670c02010-07-27 17:39:27 -0700560
561 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700562 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700563 shader.append(gVS_Header_Varyings_HasTexture);
564 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700565 if (description.hasVertexAlpha) {
566 shader.append(gVS_Header_Varyings_HasVertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700567 }
Romain Guyff316ec2013-02-13 18:39:43 -0800568 if (description.hasColors) {
569 shader.append(gVS_Header_Varyings_HasColors);
570 }
Romain Guyac670c02010-07-27 17:39:27 -0700571 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700572 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700573 }
574 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700575 shader.append(gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700576 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700577 if (description.hasRoundRectClip) {
578 shader.append(gVS_Header_Varyings_HasRoundRectClip);
579 }
Romain Guyac670c02010-07-27 17:39:27 -0700580
Romain Guyac670c02010-07-27 17:39:27 -0700581 // Uniforms
Romain Guy707b2f72010-10-11 16:34:59 -0700582 int modulateOp = MODULATE_OP_NO_MODULATE;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700583 const bool singleColor = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700584 !description.hasGradient && !description.hasBitmap;
585
586 if (description.modulate || singleColor) {
587 shader.append(gFS_Uniforms_Color);
588 if (!singleColor) modulateOp = MODULATE_OP_MODULATE;
589 }
Romain Guyac670c02010-07-27 17:39:27 -0700590 if (description.hasTexture) {
591 shader.append(gFS_Uniforms_TextureSampler);
Romain Guy8f0095c2011-05-02 17:24:22 -0700592 } else if (description.hasExternalTexture) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700593 shader.append(gFS_Uniforms_ExternalTextureSampler);
594 }
Romain Guyac670c02010-07-27 17:39:27 -0700595 if (description.hasGradient) {
Romain Guyb4880042013-04-05 11:17:55 -0700596 shader.appendFormat(gFS_Uniforms_GradientSampler[description.isSimpleGradient],
597 gFS_Uniforms_Dither);
Romain Guyac670c02010-07-27 17:39:27 -0700598 }
Romain Guy41210632012-07-16 17:04:24 -0700599 if (description.hasGammaCorrection) {
600 shader.append(gFS_Uniforms_Gamma);
601 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700602 if (description.hasRoundRectClip) {
603 shader.append(gFS_Uniforms_HasRoundRectClip);
604 }
Romain Guy707b2f72010-10-11 16:34:59 -0700605
606 // Optimization for common cases
Chris Craik91a8c7c2014-08-12 14:31:35 -0700607 if (!description.hasVertexAlpha
Chris Craikdeeda3d2014-05-05 19:09:33 -0700608 && !blendFramebuffer
609 && !description.hasColors
610 && description.colorOp == ProgramDescription::kColorNone
611 && !description.hasDebugHighlight
Chris Craikdeeda3d2014-05-05 19:09:33 -0700612 && !description.hasRoundRectClip) {
Romain Guy707b2f72010-10-11 16:34:59 -0700613 bool fast = false;
614
615 const bool noShader = !description.hasGradient && !description.hasBitmap;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700616 const bool singleTexture = (description.hasTexture || description.hasExternalTexture) &&
Romain Guy707b2f72010-10-11 16:34:59 -0700617 !description.hasAlpha8Texture && noShader;
618 const bool singleA8Texture = description.hasTexture &&
619 description.hasAlpha8Texture && noShader;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700620 const bool singleGradient = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700621 description.hasGradient && !description.hasBitmap &&
622 description.gradientType == ProgramDescription::kGradientLinear;
623
624 if (singleColor) {
625 shader.append(gFS_Fast_SingleColor);
626 fast = true;
627 } else if (singleTexture) {
628 if (!description.modulate) {
629 shader.append(gFS_Fast_SingleTexture);
630 } else {
631 shader.append(gFS_Fast_SingleModulateTexture);
632 }
633 fast = true;
634 } else if (singleA8Texture) {
635 if (!description.modulate) {
Romain Guy41210632012-07-16 17:04:24 -0700636 if (description.hasGammaCorrection) {
637 shader.append(gFS_Fast_SingleA8Texture_ApplyGamma);
638 } else {
639 shader.append(gFS_Fast_SingleA8Texture);
640 }
Romain Guy707b2f72010-10-11 16:34:59 -0700641 } else {
Romain Guy41210632012-07-16 17:04:24 -0700642 if (description.hasGammaCorrection) {
643 shader.append(gFS_Fast_SingleModulateA8Texture_ApplyGamma);
644 } else {
645 shader.append(gFS_Fast_SingleModulateA8Texture);
646 }
Romain Guy707b2f72010-10-11 16:34:59 -0700647 }
648 fast = true;
649 } else if (singleGradient) {
650 if (!description.modulate) {
Romain Guyb4880042013-04-05 11:17:55 -0700651 shader.appendFormat(gFS_Fast_SingleGradient[description.isSimpleGradient],
652 gFS_Main_Dither[mHasES3]);
Romain Guy707b2f72010-10-11 16:34:59 -0700653 } else {
Romain Guyb4880042013-04-05 11:17:55 -0700654 shader.appendFormat(gFS_Fast_SingleModulateGradient[description.isSimpleGradient],
655 gFS_Main_Dither[mHasES3]);
Romain Guy707b2f72010-10-11 16:34:59 -0700656 }
657 fast = true;
658 }
659
660 if (fast) {
Romain Guyc15008e2010-11-10 11:59:15 -0800661#if DEBUG_PROGRAMS
Romain Guy707b2f72010-10-11 16:34:59 -0700662 PROGRAM_LOGD("*** Fast case:\n");
663 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
664 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800665#endif
Romain Guy707b2f72010-10-11 16:34:59 -0700666
667 return shader;
668 }
669 }
670
Romain Guyac670c02010-07-27 17:39:27 -0700671 if (description.hasBitmap) {
672 shader.append(gFS_Uniforms_BitmapSampler);
673 }
674 shader.append(gFS_Uniforms_ColorOp[description.colorOp]);
675
676 // Generate required functions
677 if (description.hasGradient && description.hasBitmap) {
Romain Guy48daa542010-08-10 19:21:34 -0700678 generateBlend(shader, "blendShaders", description.shadersMode);
Romain Guyac670c02010-07-27 17:39:27 -0700679 }
680 if (description.colorOp == ProgramDescription::kColorBlend) {
Romain Guy48daa542010-08-10 19:21:34 -0700681 generateBlend(shader, "blendColors", description.colorMode);
Romain Guyac670c02010-07-27 17:39:27 -0700682 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700683 if (blendFramebuffer) {
684 generateBlend(shader, "blendFramebuffer", description.framebufferMode);
685 }
Romain Guy889f8d12010-07-29 14:37:42 -0700686 if (description.isBitmapNpot) {
687 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
688 }
Romain Guyac670c02010-07-27 17:39:27 -0700689
690 // Begin the shader
691 shader.append(gFS_Main); {
692 // Stores the result in fragColor directly
Romain Guyaa6c24c2011-04-28 18:40:04 -0700693 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700694 if (description.hasAlpha8Texture) {
Romain Guy707b2f72010-10-11 16:34:59 -0700695 if (!description.hasGradient && !description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700696 shader.append(gFS_Main_FetchA8Texture[modulateOp * 2 +
697 description.hasGammaCorrection]);
Romain Guy707b2f72010-10-11 16:34:59 -0700698 }
Romain Guyac670c02010-07-27 17:39:27 -0700699 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700700 shader.append(gFS_Main_FetchTexture[modulateOp]);
Romain Guyac670c02010-07-27 17:39:27 -0700701 }
702 } else {
Romain Guya938f562012-09-13 20:31:08 -0700703 if (!description.hasGradient && !description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700704 shader.append(gFS_Main_FetchColor);
705 }
Romain Guyac670c02010-07-27 17:39:27 -0700706 }
707 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700708 shader.append(gFS_Main_FetchGradient[gradientIndex(description)]);
Romain Guyb4880042013-04-05 11:17:55 -0700709 shader.appendFormat(gFS_Main_AddDitherToGradient, gFS_Main_Dither[mHasES3]);
Romain Guyac670c02010-07-27 17:39:27 -0700710 }
711 if (description.hasBitmap) {
Romain Guy889f8d12010-07-29 14:37:42 -0700712 if (!description.isBitmapNpot) {
713 shader.append(gFS_Main_FetchBitmap);
714 } else {
715 shader.append(gFS_Main_FetchBitmapNpot);
716 }
Romain Guyac670c02010-07-27 17:39:27 -0700717 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700718 bool applyModulate = false;
Romain Guyac670c02010-07-27 17:39:27 -0700719 // Case when we have two shaders set
720 if (description.hasGradient && description.hasBitmap) {
721 if (description.isBitmapFirst) {
722 shader.append(gFS_Main_BlendShadersBG);
723 } else {
724 shader.append(gFS_Main_BlendShadersGB);
725 }
Romain Guya938f562012-09-13 20:31:08 -0700726 applyModulate = shaderOp(description, shader, modulateOp,
727 gFS_Main_BlendShaders_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700728 } else {
729 if (description.hasGradient) {
Romain Guya938f562012-09-13 20:31:08 -0700730 applyModulate = shaderOp(description, shader, modulateOp,
731 gFS_Main_GradientShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700732 } else if (description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700733 applyModulate = shaderOp(description, shader, modulateOp,
734 gFS_Main_BitmapShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700735 }
Romain Guyac670c02010-07-27 17:39:27 -0700736 }
Romain Guya938f562012-09-13 20:31:08 -0700737
Romain Guy740bf2b2011-04-26 15:33:10 -0700738 if (description.modulate && applyModulate) {
Romain Guya938f562012-09-13 20:31:08 -0700739 shader.append(gFS_Main_ModulateColor);
Romain Guy740bf2b2011-04-26 15:33:10 -0700740 }
Romain Guya938f562012-09-13 20:31:08 -0700741
Romain Guyac670c02010-07-27 17:39:27 -0700742 // Apply the color op if needed
743 shader.append(gFS_Main_ApplyColorOp[description.colorOp]);
Chris Craik9f44a132012-09-13 18:34:55 -0700744
Chris Craik91a8c7c2014-08-12 14:31:35 -0700745 if (description.hasVertexAlpha) {
746 if (description.useShadowAlphaInterp) {
747 shader.append(gFS_Main_ApplyVertexAlphaShadowInterp);
Chris Craikbf759452014-08-11 16:00:44 -0700748 } else {
Chris Craik91a8c7c2014-08-12 14:31:35 -0700749 shader.append(gFS_Main_ApplyVertexAlphaLinearInterp);
Chris Craikbf759452014-08-11 16:00:44 -0700750 }
Chris Craik9f44a132012-09-13 18:34:55 -0700751 }
752
Romain Guyac670c02010-07-27 17:39:27 -0700753 // Output the fragment
Romain Guya5aed0d2010-09-09 14:42:43 -0700754 if (!blendFramebuffer) {
755 shader.append(gFS_Main_FragColor);
756 } else {
Romain Guyf607bdc2010-09-10 19:20:06 -0700757 shader.append(!description.swapSrcDst ?
758 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap);
Romain Guya5aed0d2010-09-09 14:42:43 -0700759 }
Romain Guyff316ec2013-02-13 18:39:43 -0800760 if (description.hasColors) {
761 shader.append(gFS_Main_FragColor_HasColors);
762 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700763 if (description.hasRoundRectClip) {
764 shader.append(gFS_Main_FragColor_HasRoundRectClip);
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 }
770 // End the shader
771 shader.append(gFS_Footer);
772
Romain Guyc15008e2010-11-10 11:59:15 -0800773#if DEBUG_PROGRAMS
Romain Guydb1938e2010-08-02 18:50:22 -0700774 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
775 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800776#endif
Romain Guydb1938e2010-08-02 18:50:22 -0700777
Romain Guyac670c02010-07-27 17:39:27 -0700778 return shader;
779}
780
Romain Guy48daa542010-08-10 19:21:34 -0700781void ProgramCache::generateBlend(String8& shader, const char* name, SkXfermode::Mode mode) {
Romain Guyac670c02010-07-27 17:39:27 -0700782 shader.append("\nvec4 ");
783 shader.append(name);
784 shader.append("(vec4 src, vec4 dst) {\n");
785 shader.append(" ");
Romain Guy48daa542010-08-10 19:21:34 -0700786 shader.append(gBlendOps[mode]);
Romain Guyac670c02010-07-27 17:39:27 -0700787 shader.append("}\n");
788}
789
Romain Guy889f8d12010-07-29 14:37:42 -0700790void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) {
Romain Guy63553472012-07-18 20:04:14 -0700791 shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700792 if (wrapS == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700793 shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700794 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n");
795 }
796 if (wrapT == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700797 shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700798 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n");
799 }
800 shader.append(" return vec2(");
801 switch (wrapS) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700802 case GL_CLAMP_TO_EDGE:
803 shader.append("texCoords.x");
804 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700805 case GL_REPEAT:
806 shader.append("mod(texCoords.x, 1.0)");
807 break;
808 case GL_MIRRORED_REPEAT:
809 shader.append("xMod2");
810 break;
811 }
812 shader.append(", ");
813 switch (wrapT) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700814 case GL_CLAMP_TO_EDGE:
815 shader.append("texCoords.y");
816 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700817 case GL_REPEAT:
818 shader.append("mod(texCoords.y, 1.0)");
819 break;
820 case GL_MIRRORED_REPEAT:
821 shader.append("yMod2");
822 break;
823 }
824 shader.append(");\n");
825 shader.append("}\n");
826}
827
Romain Guydb1938e2010-08-02 18:50:22 -0700828void ProgramCache::printLongString(const String8& shader) const {
829 ssize_t index = 0;
830 ssize_t lastIndex = 0;
831 const char* str = shader.string();
832 while ((index = shader.find("\n", index)) > -1) {
833 String8 line(str, index - lastIndex);
834 if (line.length() == 0) line.append("\n");
Chris Craik1c1c3fe2015-03-06 09:40:35 -0800835 ALOGD("%s", line.string());
Romain Guydb1938e2010-08-02 18:50:22 -0700836 index++;
837 str += (index - lastIndex);
838 lastIndex = index;
839 }
840}
841
Romain Guyac670c02010-07-27 17:39:27 -0700842}; // namespace uirenderer
843}; // namespace android