blob: 1afc97839b444165be2d9ceb0c04075d0353d09a [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 Guyac670c02010-07-27 17:39:27 -070020#include "ProgramCache.h"
Romain Guy253f2c22016-09-28 17:34:42 -070021#include "Properties.h"
Romain Guyac670c02010-07-27 17:39:27 -070022
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
Romain Guy253f2c22016-09-28 17:34:42 -070072 "varying highp vec2 linear;\n",
73 "varying float linear;\n",
Romain Guy211efea2012-07-31 21:16:07 -070074
Romain Guyee916f12010-09-20 17:53:08 -070075 // Circular
Romain Guy253f2c22016-09-28 17:34:42 -070076 "varying highp vec2 circular;\n",
77 "varying highp vec2 circular;\n",
Romain Guy211efea2012-07-31 21:16:07 -070078
Romain Guyee916f12010-09-20 17:53:08 -070079 // Sweep
Romain Guy253f2c22016-09-28 17:34:42 -070080 "varying highp vec2 sweep;\n",
81 "varying highp vec2 sweep;\n",
Romain Guyee916f12010-09-20 17:53:08 -070082};
Chris Craikdeeda3d2014-05-05 19:09:33 -070083const char* gVS_Header_Varyings_HasRoundRectClip =
Chris Craik68a73e82014-08-29 17:06:27 -070084 "varying highp vec2 roundRectPos;\n";
Romain Guyac670c02010-07-27 17:39:27 -070085const char* gVS_Main =
86 "\nvoid main(void) {\n";
87const char* gVS_Main_OutTexCoords =
88 " outTexCoords = texCoords;\n";
Romain Guyff316ec2013-02-13 18:39:43 -080089const char* gVS_Main_OutColors =
90 " outColors = colors;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -070091const char* gVS_Main_OutTransformedTexCoords =
92 " outTexCoords = (mainTextureTransform * vec4(texCoords, 0.0, 1.0)).xy;\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -070093const char* gVS_Main_OutGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -070094 // Linear
Romain Guy253f2c22016-09-28 17:34:42 -070095 " linear = vec2((screenSpace * position).x, 0.5);\n",
96 " linear = (screenSpace * position).x;\n",
Romain Guy211efea2012-07-31 21:16:07 -070097
Romain Guyee916f12010-09-20 17:53:08 -070098 // Circular
Romain Guy253f2c22016-09-28 17:34:42 -070099 " circular = (screenSpace * position).xy;\n",
100 " circular = (screenSpace * position).xy;\n",
Romain Guy211efea2012-07-31 21:16:07 -0700101
Romain Guyee916f12010-09-20 17:53:08 -0700102 // Sweep
Romain Guy253f2c22016-09-28 17:34:42 -0700103 " sweep = (screenSpace * position).xy;\n",
Chet Haasea1d12dd2012-09-21 14:50:14 -0700104 " sweep = (screenSpace * position).xy;\n"
Romain Guyee916f12010-09-20 17:53:08 -0700105};
Romain Guy889f8d12010-07-29 14:37:42 -0700106const char* gVS_Main_OutBitmapTexCoords =
Romain Guy707b2f72010-10-11 16:34:59 -0700107 " outBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700108const char* gVS_Main_Position =
Chris Craikdeeda3d2014-05-05 19:09:33 -0700109 " vec4 transformedPosition = projection * transform * position;\n"
110 " gl_Position = transformedPosition;\n";
Chris Craikbf759452014-08-11 16:00:44 -0700111
Chris Craik91a8c7c2014-08-12 14:31:35 -0700112const char* gVS_Main_VertexAlpha =
Chris Craik6ebdc112012-08-31 18:24:33 -0700113 " alpha = vtxAlpha;\n";
Chris Craikbf759452014-08-11 16:00:44 -0700114
Chris Craikdeeda3d2014-05-05 19:09:33 -0700115const char* gVS_Main_HasRoundRectClip =
116 " roundRectPos = (roundRectInvTransform * transformedPosition).xy;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700117const char* gVS_Footer =
118 "}\n\n";
119
120///////////////////////////////////////////////////////////////////////////////
121// Fragment shaders snippets
122///////////////////////////////////////////////////////////////////////////////
123
Chris Craik8bd68c62015-08-19 15:29:05 -0700124const char* gFS_Header_Start =
125 "#version 100\n";
Romain Guya5aed0d2010-09-09 14:42:43 -0700126const char* gFS_Header_Extension_FramebufferFetch =
127 "#extension GL_NV_shader_framebuffer_fetch : enable\n\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700128const char* gFS_Header_Extension_ExternalTexture =
129 "#extension GL_OES_EGL_image_external : require\n\n";
Romain Guyac670c02010-07-27 17:39:27 -0700130const char* gFS_Header =
131 "precision mediump float;\n\n";
132const char* gFS_Uniforms_Color =
133 "uniform vec4 color;\n";
134const char* gFS_Uniforms_TextureSampler =
Romain Guya938f562012-09-13 20:31:08 -0700135 "uniform sampler2D baseSampler;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700136const char* gFS_Uniforms_ExternalTextureSampler =
Romain Guya938f562012-09-13 20:31:08 -0700137 "uniform samplerExternalOES baseSampler;\n";
Romain Guyb4880042013-04-05 11:17:55 -0700138const char* gFS_Uniforms_GradientSampler[2] = {
Romain Guy253f2c22016-09-28 17:34:42 -0700139 "uniform vec2 screenSize;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700140 "uniform sampler2D gradientSampler;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700141
142 "uniform vec2 screenSize;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700143 "uniform vec4 startColor;\n"
Romain Guy211efea2012-07-31 21:16:07 -0700144 "uniform vec4 endColor;\n"
Romain Guyee916f12010-09-20 17:53:08 -0700145};
Romain Guyac670c02010-07-27 17:39:27 -0700146const char* gFS_Uniforms_BitmapSampler =
147 "uniform sampler2D bitmapSampler;\n";
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500148const char* gFS_Uniforms_ColorOp[3] = {
Romain Guyac670c02010-07-27 17:39:27 -0700149 // None
150 "",
151 // Matrix
152 "uniform mat4 colorMatrix;\n"
153 "uniform vec4 colorMatrixVector;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700154 // PorterDuff
Romain Guydb1938e2010-08-02 18:50:22 -0700155 "uniform vec4 colorBlend;\n"
Romain Guyac670c02010-07-27 17:39:27 -0700156};
Romain Guy41210632012-07-16 17:04:24 -0700157
Chris Craikdeeda3d2014-05-05 19:09:33 -0700158const char* gFS_Uniforms_HasRoundRectClip =
159 "uniform vec4 roundRectInnerRectLTRB;\n"
160 "uniform float roundRectRadius;\n";
161
Romain Guy253f2c22016-09-28 17:34:42 -0700162// Dithering must be done in the quantization space
163// When we are writing to an sRGB framebuffer, we must do the following:
164// EOCF(OECF(color) + dither)
165// We approximate the transfer functions with gamma 2.0 to avoid branches and pow()
Romain Guy8762e332016-10-12 12:14:07 -0700166// The dithering pattern is generated with a triangle noise generator in the range [-0.0,1.0]
Romain Guy253f2c22016-09-28 17:34:42 -0700167// TODO: Handle linear fp16 render targets
Romain Guy8762e332016-10-12 12:14:07 -0700168const char* gFS_Gradient_Functions =
169 "\nfloat triangleNoise(const highp vec2 n) {\n"
Romain Guy253f2c22016-09-28 17:34:42 -0700170 " highp vec2 p = fract(n * vec2(5.3987, 5.4421));\n"
171 " p += dot(p.yx, p.xy + vec2(21.5351, 14.3137));\n"
172 " highp float xy = p.x * p.y;\n"
Romain Guy8762e332016-10-12 12:14:07 -0700173 " return fract(xy * 95.4307) + fract(xy * 75.04961) - 1.0;\n"
Romain Guy253f2c22016-09-28 17:34:42 -0700174 "}\n";
Romain Guy8762e332016-10-12 12:14:07 -0700175const char* gFS_Gradient_Preamble[2] = {
Romain Guy253f2c22016-09-28 17:34:42 -0700176 // Linear framebuffer
177 "\nvec4 dither(const vec4 color) {\n"
178 " return vec4(color.rgb + (triangleNoise(gl_FragCoord.xy * screenSize.xy) / 255.0), color.a);"
Romain Guy8762e332016-10-12 12:14:07 -0700179 "}\n"
180 "\nvec4 gammaMix(const vec4 a, const vec4 b, float v) {\n"
181 " return pow(mix(a, b, v), vec4(vec3(1.0 / 2.2), 1.0));"
Romain Guy253f2c22016-09-28 17:34:42 -0700182 "}\n",
183 // sRGB framebuffer
184 "\nvec4 dither(const vec4 color) {\n"
185 " vec3 dithered = sqrt(color.rgb) + (triangleNoise(gl_FragCoord.xy * screenSize.xy) / 255.0);\n"
186 " return vec4(dithered * dithered, color.a);\n"
187 "}\n"
Romain Guy8762e332016-10-12 12:14:07 -0700188 "\nvec4 gammaMix(const vec4 a, const vec4 b, float v) {\n"
189 " return mix(a, b, v);"
190 "}\n"
Romain Guy253f2c22016-09-28 17:34:42 -0700191};
192
193// Uses luminance coefficients from Rec.709 to choose the appropriate gamma
194// The gamma() function assumes that bright text will be displayed on a dark
195// background and that dark text will be displayed on bright background
196// The gamma coefficient is chosen to thicken or thin the text accordingly
197// The dot product used to compute the luminance could be approximated with
198// a simple max(color.r, color.g, color.b)
199const char* gFS_Gamma_Preamble =
200 "\n#define GAMMA (%.2f)\n"
201 "#define GAMMA_INV (%.2f)\n"
202 "\nfloat gamma(float a, const vec3 color) {\n"
203 " float luminance = dot(color, vec3(0.2126, 0.7152, 0.0722));\n"
204 " return pow(a, luminance < 0.5 ? GAMMA_INV : GAMMA);\n"
205 "}\n";
206
Romain Guyac670c02010-07-27 17:39:27 -0700207const char* gFS_Main =
208 "\nvoid main(void) {\n"
Romain Guy253f2c22016-09-28 17:34:42 -0700209 " vec4 fragColor;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700210
Romain Guy253f2c22016-09-28 17:34:42 -0700211const char* gFS_Main_AddDither =
212 " fragColor = dither(fragColor);\n";
Romain Guy211efea2012-07-31 21:16:07 -0700213
Romain Guy707b2f72010-10-11 16:34:59 -0700214// Fast cases
215const char* gFS_Fast_SingleColor =
216 "\nvoid main(void) {\n"
217 " gl_FragColor = color;\n"
218 "}\n\n";
219const char* gFS_Fast_SingleTexture =
220 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700221 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700222 "}\n\n";
223const char* gFS_Fast_SingleModulateTexture =
224 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700225 " gl_FragColor = color.a * texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700226 "}\n\n";
227const char* gFS_Fast_SingleA8Texture =
228 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700229 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700230 "}\n\n";
Romain Guy253f2c22016-09-28 17:34:42 -0700231const char* gFS_Fast_SingleA8Texture_ApplyGamma =
232 "\nvoid main(void) {\n"
233 " gl_FragColor = vec4(0.0, 0.0, 0.0, pow(texture2D(baseSampler, outTexCoords).a, GAMMA));\n"
234 "}\n\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700235const char* gFS_Fast_SingleModulateA8Texture =
236 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700237 " gl_FragColor = color * texture2D(baseSampler, outTexCoords).a;\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700238 "}\n\n";
Romain Guy253f2c22016-09-28 17:34:42 -0700239const char* gFS_Fast_SingleModulateA8Texture_ApplyGamma =
240 "\nvoid main(void) {\n"
241 " gl_FragColor = color * gamma(texture2D(baseSampler, outTexCoords).a, color.rgb);\n"
242 "}\n\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -0700243const char* gFS_Fast_SingleGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700244 "\nvoid main(void) {\n"
Romain Guy253f2c22016-09-28 17:34:42 -0700245 " gl_FragColor = dither(texture2D(gradientSampler, linear));\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700246 "}\n\n",
247 "\nvoid main(void) {\n"
Romain Guy8762e332016-10-12 12:14:07 -0700248 " gl_FragColor = dither(gammaMix(startColor, endColor, clamp(linear, 0.0, 1.0)));\n"
Romain Guyb4880042013-04-05 11:17:55 -0700249 "}\n\n",
Romain Guy42e1e0d2012-07-30 14:47:51 -0700250};
251const char* gFS_Fast_SingleModulateGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700252 "\nvoid main(void) {\n"
Romain Guy253f2c22016-09-28 17:34:42 -0700253 " gl_FragColor = dither(color.a * texture2D(gradientSampler, linear));\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700254 "}\n\n",
255 "\nvoid main(void) {\n"
Romain Guy8762e332016-10-12 12:14:07 -0700256 " gl_FragColor = dither(color.a * gammaMix(startColor, endColor, clamp(linear, 0.0, 1.0)));\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700257 "}\n\n"
258};
Romain Guy707b2f72010-10-11 16:34:59 -0700259
260// General case
Romain Guyac670c02010-07-27 17:39:27 -0700261const char* gFS_Main_FetchColor =
262 " fragColor = color;\n";
Romain Guy740bf2b2011-04-26 15:33:10 -0700263const char* gFS_Main_ModulateColor =
264 " fragColor *= color.a;\n";
Chris Craik91a8c7c2014-08-12 14:31:35 -0700265const char* gFS_Main_ApplyVertexAlphaLinearInterp =
Chris Craik6ebdc112012-08-31 18:24:33 -0700266 " fragColor *= alpha;\n";
Chris Craik91a8c7c2014-08-12 14:31:35 -0700267const char* gFS_Main_ApplyVertexAlphaShadowInterp =
Chris Craik138c21f2016-04-28 16:59:42 -0700268 // map alpha through shadow alpha sampler
269 " fragColor *= texture2D(baseSampler, vec2(alpha, 0.5)).a;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700270const char* gFS_Main_FetchTexture[2] = {
271 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700272 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700273 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700274 " fragColor = color * texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700275};
Romain Guy253f2c22016-09-28 17:34:42 -0700276const char* gFS_Main_FetchA8Texture[4] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700277 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700278 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700279 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700280 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700281 " fragColor = color * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700282 " fragColor = color * gamma(texture2D(baseSampler, outTexCoords).a, color.rgb);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700283};
Romain Guy42e1e0d2012-07-30 14:47:51 -0700284const char* gFS_Main_FetchGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700285 // Linear
Romain Guy320d46b2012-08-08 16:05:42 -0700286 " vec4 gradientColor = texture2D(gradientSampler, linear);\n",
Romain Guy211efea2012-07-31 21:16:07 -0700287
Romain Guy8762e332016-10-12 12:14:07 -0700288 " vec4 gradientColor = gammaMix(startColor, endColor, clamp(linear, 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700289
Romain Guyee916f12010-09-20 17:53:08 -0700290 // Circular
Romain Guy320d46b2012-08-08 16:05:42 -0700291 " vec4 gradientColor = texture2D(gradientSampler, vec2(length(circular), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700292
Romain Guy8762e332016-10-12 12:14:07 -0700293 " vec4 gradientColor = gammaMix(startColor, endColor, clamp(length(circular), 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700294
Romain Guyee916f12010-09-20 17:53:08 -0700295 // Sweep
Romain Guy63553472012-07-18 20:04:14 -0700296 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700297 " vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700298
Romain Guy42e1e0d2012-07-30 14:47:51 -0700299 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy8762e332016-10-12 12:14:07 -0700300 " vec4 gradientColor = gammaMix(startColor, endColor, clamp(index - floor(index), 0.0, 1.0));\n"
Romain Guyee916f12010-09-20 17:53:08 -0700301};
Romain Guyac670c02010-07-27 17:39:27 -0700302const char* gFS_Main_FetchBitmap =
303 " vec4 bitmapColor = texture2D(bitmapSampler, outBitmapTexCoords);\n";
Romain Guy889f8d12010-07-29 14:37:42 -0700304const char* gFS_Main_FetchBitmapNpot =
305 " vec4 bitmapColor = texture2D(bitmapSampler, wrap(outBitmapTexCoords));\n";
Romain Guyac670c02010-07-27 17:39:27 -0700306const char* gFS_Main_BlendShadersBG =
Romain Guyac670c02010-07-27 17:39:27 -0700307 " fragColor = blendShaders(gradientColor, bitmapColor)";
Romain Guy06f96e22010-07-30 19:18:16 -0700308const char* gFS_Main_BlendShadersGB =
309 " fragColor = blendShaders(bitmapColor, gradientColor)";
Romain Guy253f2c22016-09-28 17:34:42 -0700310const char* gFS_Main_BlendShaders_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700311 // Don't modulate
312 ";\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700313 ";\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700314 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700315 " * color.a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700316 " * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700317 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700318 " * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700319 " * gamma(texture2D(baseSampler, outTexCoords).a, color.rgb);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700320};
Romain Guy253f2c22016-09-28 17:34:42 -0700321const char* gFS_Main_GradientShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700322 // Don't modulate
323 " fragColor = gradientColor;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700324 " fragColor = gradientColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700325 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700326 " fragColor = gradientColor * color.a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700327 " fragColor = gradientColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700328 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700329 " fragColor = gradientColor * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700330 " fragColor = gradientColor * gamma(texture2D(baseSampler, outTexCoords).a, gradientColor.rgb);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700331 };
Romain Guy253f2c22016-09-28 17:34:42 -0700332const char* gFS_Main_BitmapShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700333 // Don't modulate
334 " fragColor = bitmapColor;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700335 " fragColor = bitmapColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700336 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700337 " fragColor = bitmapColor * color.a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700338 " fragColor = bitmapColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700339 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700340 " fragColor = bitmapColor * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700341 " fragColor = bitmapColor * gamma(texture2D(baseSampler, outTexCoords).a, bitmapColor.rgb);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700342 };
Romain Guyac670c02010-07-27 17:39:27 -0700343const char* gFS_Main_FragColor =
344 " gl_FragColor = fragColor;\n";
Romain Guyff316ec2013-02-13 18:39:43 -0800345const char* gFS_Main_FragColor_HasColors =
346 " gl_FragColor *= outColors;\n";
Romain Guya5aed0d2010-09-09 14:42:43 -0700347const char* gFS_Main_FragColor_Blend =
348 " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n";
Romain Guyf607bdc2010-09-10 19:20:06 -0700349const char* gFS_Main_FragColor_Blend_Swap =
350 " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n";
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500351const char* gFS_Main_ApplyColorOp[3] = {
Romain Guyac670c02010-07-27 17:39:27 -0700352 // None
353 "",
354 // Matrix
Chris Craik73821c82014-09-16 17:32:13 -0700355 " fragColor.rgb /= (fragColor.a + 0.0019);\n" // un-premultiply
Romain Guyac670c02010-07-27 17:39:27 -0700356 " fragColor *= colorMatrix;\n"
Chris Craik73821c82014-09-16 17:32:13 -0700357 " fragColor += colorMatrixVector;\n"
358 " fragColor.rgb *= (fragColor.a + 0.0019);\n", // re-premultiply
Romain Guyac670c02010-07-27 17:39:27 -0700359 // PorterDuff
360 " fragColor = blendColors(colorBlend, fragColor);\n"
361};
Chris Craikdeeda3d2014-05-05 19:09:33 -0700362
363// Note: LTRB -> xyzw
364const char* gFS_Main_FragColor_HasRoundRectClip =
365 " mediump vec2 fragToLT = roundRectInnerRectLTRB.xy - roundRectPos;\n"
366 " mediump vec2 fragFromRB = roundRectPos - roundRectInnerRectLTRB.zw;\n"
Chris Craikf99f3202014-08-05 15:31:52 -0700367
368 // divide + multiply by 128 to avoid falling out of range in length() function
369 " mediump vec2 dist = max(max(fragToLT, fragFromRB), vec2(0.0, 0.0)) / 128.0;\n"
370 " mediump float linearDist = roundRectRadius - (length(dist) * 128.0);\n"
Chris Craikdeeda3d2014-05-05 19:09:33 -0700371 " gl_FragColor *= clamp(linearDist, 0.0, 1.0);\n";
372
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800373const char* gFS_Main_DebugHighlight =
374 " gl_FragColor.rgb = vec3(0.0, gl_FragColor.a, 0.0);\n";
Romain Guyac670c02010-07-27 17:39:27 -0700375const char* gFS_Footer =
376 "}\n\n";
377
378///////////////////////////////////////////////////////////////////////////////
379// PorterDuff snippets
380///////////////////////////////////////////////////////////////////////////////
381
Romain Guy48daa542010-08-10 19:21:34 -0700382const char* gBlendOps[18] = {
Romain Guyac670c02010-07-27 17:39:27 -0700383 // Clear
384 "return vec4(0.0, 0.0, 0.0, 0.0);\n",
385 // Src
386 "return src;\n",
387 // Dst
388 "return dst;\n",
389 // SrcOver
Romain Guy06f96e22010-07-30 19:18:16 -0700390 "return src + dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700391 // DstOver
Romain Guy06f96e22010-07-30 19:18:16 -0700392 "return dst + src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700393 // SrcIn
Romain Guy06f96e22010-07-30 19:18:16 -0700394 "return src * dst.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700395 // DstIn
Romain Guy06f96e22010-07-30 19:18:16 -0700396 "return dst * src.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700397 // SrcOut
Romain Guy06f96e22010-07-30 19:18:16 -0700398 "return src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700399 // DstOut
Romain Guy06f96e22010-07-30 19:18:16 -0700400 "return dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700401 // SrcAtop
402 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n",
403 // DstAtop
404 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n",
405 // Xor
Romain Guy48daa542010-08-10 19:21:34 -0700406 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, "
Romain Guyac670c02010-07-27 17:39:27 -0700407 "src.a + dst.a - 2.0 * src.a * dst.a);\n",
Derek Sollenbergerc0bf7002015-03-06 13:48:27 -0500408 // Plus
Romain Guy48daa542010-08-10 19:21:34 -0700409 "return min(src + dst, 1.0);\n",
Derek Sollenbergerc0bf7002015-03-06 13:48:27 -0500410 // Modulate
Romain Guy48daa542010-08-10 19:21:34 -0700411 "return src * dst;\n",
412 // Screen
413 "return src + dst - src * dst;\n",
414 // Overlay
415 "return clamp(vec4(mix("
416 "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), "
417 "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), "
418 "step(dst.a, 2.0 * dst.rgb)), "
419 "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n",
420 // Darken
421 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
422 "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
423 // Lighten
424 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
425 "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 -0700426};
427
428///////////////////////////////////////////////////////////////////////////////
429// Constructors/destructors
430///////////////////////////////////////////////////////////////////////////////
431
Chris Craik117bdbc2015-02-05 10:12:38 -0800432ProgramCache::ProgramCache(Extensions& extensions)
Romain Guy253f2c22016-09-28 17:34:42 -0700433 : mHasES3(extensions.getMajorGlVersion() >= 3)
434 , mHasSRGB(extensions.hasSRGB()) {
Romain Guyac670c02010-07-27 17:39:27 -0700435}
436
437ProgramCache::~ProgramCache() {
438 clear();
439}
440
441///////////////////////////////////////////////////////////////////////////////
442// Cache management
443///////////////////////////////////////////////////////////////////////////////
444
445void ProgramCache::clear() {
Romain Guy67f27952010-12-07 20:09:23 -0800446 PROGRAM_LOGD("Clearing program cache");
Romain Guyac670c02010-07-27 17:39:27 -0700447 mCache.clear();
448}
449
450Program* ProgramCache::get(const ProgramDescription& description) {
451 programid key = description.key();
Chris Craik096b8d92013-03-01 11:08:11 -0800452 if (key == (PROGRAM_KEY_TEXTURE | PROGRAM_KEY_A8_TEXTURE)) {
453 // program for A8, unmodulated, texture w/o shader (black text/path textures) is equivalent
454 // to standard texture program (bitmaps, patches). Consider them equivalent.
455 key = PROGRAM_KEY_TEXTURE;
456 }
457
Chris Craik51d6a3d2014-12-22 17:16:56 -0800458 auto iter = mCache.find(key);
Chris Craikd41c4d82015-01-05 15:51:13 -0800459 Program* program = nullptr;
Chris Craik51d6a3d2014-12-22 17:16:56 -0800460 if (iter == mCache.end()) {
Romain Guyee916f12010-09-20 17:53:08 -0700461 description.log("Could not find program");
Romain Guyac670c02010-07-27 17:39:27 -0700462 program = generateProgram(description, key);
Chris Craik51d6a3d2014-12-22 17:16:56 -0800463 mCache[key] = std::unique_ptr<Program>(program);
Romain Guyac670c02010-07-27 17:39:27 -0700464 } else {
Chris Craik51d6a3d2014-12-22 17:16:56 -0800465 program = iter->second.get();
Romain Guyac670c02010-07-27 17:39:27 -0700466 }
467 return program;
468}
469
470///////////////////////////////////////////////////////////////////////////////
471// Program generation
472///////////////////////////////////////////////////////////////////////////////
473
Andreas Gampe64bb4132014-11-22 00:35:09 +0000474Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) {
Romain Guyac670c02010-07-27 17:39:27 -0700475 String8 vertexShader = generateVertexShader(description);
476 String8 fragmentShader = generateFragmentShader(description);
477
Romain Guy42e1e0d2012-07-30 14:47:51 -0700478 return new Program(description, vertexShader.string(), fragmentShader.string());
479}
480
481static inline size_t gradientIndex(const ProgramDescription& description) {
482 return description.gradientType * 2 + description.isSimpleGradient;
Romain Guyac670c02010-07-27 17:39:27 -0700483}
484
485String8 ProgramCache::generateVertexShader(const ProgramDescription& description) {
486 // Add attributes
Chris Craik8bd68c62015-08-19 15:29:05 -0700487 String8 shader(gVS_Header_Start);
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_Attributes_TexCoords);
490 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700491 if (description.hasVertexAlpha) {
492 shader.append(gVS_Header_Attributes_VertexAlphaParameters);
Chet Haase5b0200b2011-04-13 17:58:08 -0700493 }
Romain Guyff316ec2013-02-13 18:39:43 -0800494 if (description.hasColors) {
495 shader.append(gVS_Header_Attributes_Colors);
496 }
Romain Guyac670c02010-07-27 17:39:27 -0700497 // Uniforms
498 shader.append(gVS_Header_Uniforms);
Romain Guy8f0095c2011-05-02 17:24:22 -0700499 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700500 shader.append(gVS_Header_Uniforms_TextureTransform);
501 }
Romain Guyac670c02010-07-27 17:39:27 -0700502 if (description.hasGradient) {
Romain Guyb4880042013-04-05 11:17:55 -0700503 shader.append(gVS_Header_Uniforms_HasGradient);
Romain Guyac670c02010-07-27 17:39:27 -0700504 }
Romain Guy889f8d12010-07-29 14:37:42 -0700505 if (description.hasBitmap) {
506 shader.append(gVS_Header_Uniforms_HasBitmap);
507 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700508 if (description.hasRoundRectClip) {
509 shader.append(gVS_Header_Uniforms_HasRoundRectClip);
510 }
Romain Guyac670c02010-07-27 17:39:27 -0700511 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700512 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700513 shader.append(gVS_Header_Varyings_HasTexture);
514 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700515 if (description.hasVertexAlpha) {
516 shader.append(gVS_Header_Varyings_HasVertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700517 }
Romain Guyff316ec2013-02-13 18:39:43 -0800518 if (description.hasColors) {
519 shader.append(gVS_Header_Varyings_HasColors);
520 }
Romain Guyac670c02010-07-27 17:39:27 -0700521 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700522 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700523 }
524 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700525 shader.append(gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700526 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700527 if (description.hasRoundRectClip) {
528 shader.append(gVS_Header_Varyings_HasRoundRectClip);
529 }
Romain Guyac670c02010-07-27 17:39:27 -0700530
531 // Begin the shader
532 shader.append(gVS_Main); {
Romain Guy8f0095c2011-05-02 17:24:22 -0700533 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700534 shader.append(gVS_Main_OutTransformedTexCoords);
Romain Guy8f0095c2011-05-02 17:24:22 -0700535 } else if (description.hasTexture || description.hasExternalTexture) {
536 shader.append(gVS_Main_OutTexCoords);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700537 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700538 if (description.hasVertexAlpha) {
539 shader.append(gVS_Main_VertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700540 }
Romain Guyff316ec2013-02-13 18:39:43 -0800541 if (description.hasColors) {
542 shader.append(gVS_Main_OutColors);
543 }
Romain Guy889f8d12010-07-29 14:37:42 -0700544 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700545 shader.append(gVS_Main_OutBitmapTexCoords);
Romain Guy889f8d12010-07-29 14:37:42 -0700546 }
Romain Guyac670c02010-07-27 17:39:27 -0700547 // Output transformed position
548 shader.append(gVS_Main_Position);
Chet Haasea1d12dd2012-09-21 14:50:14 -0700549 if (description.hasGradient) {
550 shader.append(gVS_Main_OutGradient[gradientIndex(description)]);
551 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700552 if (description.hasRoundRectClip) {
553 shader.append(gVS_Main_HasRoundRectClip);
554 }
Romain Guyac670c02010-07-27 17:39:27 -0700555 }
556 // End the shader
557 shader.append(gVS_Footer);
558
559 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string());
560
561 return shader;
562}
563
Romain Guya938f562012-09-13 20:31:08 -0700564static bool shaderOp(const ProgramDescription& description, String8& shader,
565 const int modulateOp, const char** snippets) {
566 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
Romain Guy253f2c22016-09-28 17:34:42 -0700567 op = op * 2 + description.hasGammaCorrection;
Romain Guya938f562012-09-13 20:31:08 -0700568 shader.append(snippets[op]);
569 return description.hasAlpha8Texture;
570}
571
Romain Guyac670c02010-07-27 17:39:27 -0700572String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) {
Chris Craik8bd68c62015-08-19 15:29:05 -0700573 String8 shader(gFS_Header_Start);
Romain Guya5aed0d2010-09-09 14:42:43 -0700574
Mike Reedc2f31df2016-10-28 17:21:45 -0400575 const bool blendFramebuffer = description.framebufferMode >= SkBlendMode::kPlus;
Romain Guya5aed0d2010-09-09 14:42:43 -0700576 if (blendFramebuffer) {
577 shader.append(gFS_Header_Extension_FramebufferFetch);
578 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700579 if (description.hasExternalTexture) {
580 shader.append(gFS_Header_Extension_ExternalTexture);
581 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700582
583 shader.append(gFS_Header);
Romain Guyac670c02010-07-27 17:39:27 -0700584
585 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700586 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700587 shader.append(gVS_Header_Varyings_HasTexture);
588 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700589 if (description.hasVertexAlpha) {
590 shader.append(gVS_Header_Varyings_HasVertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700591 }
Romain Guyff316ec2013-02-13 18:39:43 -0800592 if (description.hasColors) {
593 shader.append(gVS_Header_Varyings_HasColors);
594 }
Romain Guyac670c02010-07-27 17:39:27 -0700595 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700596 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700597 }
598 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700599 shader.append(gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700600 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700601 if (description.hasRoundRectClip) {
602 shader.append(gVS_Header_Varyings_HasRoundRectClip);
603 }
Romain Guyac670c02010-07-27 17:39:27 -0700604
Romain Guyac670c02010-07-27 17:39:27 -0700605 // Uniforms
Romain Guy707b2f72010-10-11 16:34:59 -0700606 int modulateOp = MODULATE_OP_NO_MODULATE;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700607 const bool singleColor = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700608 !description.hasGradient && !description.hasBitmap;
609
610 if (description.modulate || singleColor) {
611 shader.append(gFS_Uniforms_Color);
612 if (!singleColor) modulateOp = MODULATE_OP_MODULATE;
613 }
Chris Craik138c21f2016-04-28 16:59:42 -0700614 if (description.hasTexture || description.useShadowAlphaInterp) {
Romain Guyac670c02010-07-27 17:39:27 -0700615 shader.append(gFS_Uniforms_TextureSampler);
Romain Guy8f0095c2011-05-02 17:24:22 -0700616 } else if (description.hasExternalTexture) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700617 shader.append(gFS_Uniforms_ExternalTextureSampler);
618 }
Romain Guyac670c02010-07-27 17:39:27 -0700619 if (description.hasGradient) {
Romain Guy253f2c22016-09-28 17:34:42 -0700620 shader.append(gFS_Uniforms_GradientSampler[description.isSimpleGradient]);
Romain Guyac670c02010-07-27 17:39:27 -0700621 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700622 if (description.hasRoundRectClip) {
623 shader.append(gFS_Uniforms_HasRoundRectClip);
624 }
Romain Guy707b2f72010-10-11 16:34:59 -0700625
Romain Guy253f2c22016-09-28 17:34:42 -0700626 if (description.hasGammaCorrection) {
627 shader.appendFormat(gFS_Gamma_Preamble, Properties::textGamma, 1.0f / Properties::textGamma);
628 }
629
Romain Guy707b2f72010-10-11 16:34:59 -0700630 // Optimization for common cases
Chris Craik91a8c7c2014-08-12 14:31:35 -0700631 if (!description.hasVertexAlpha
Chris Craikdeeda3d2014-05-05 19:09:33 -0700632 && !blendFramebuffer
633 && !description.hasColors
Chris Craikb9ce116d2015-08-20 15:14:06 -0700634 && description.colorOp == ProgramDescription::ColorFilterMode::None
Chris Craikdeeda3d2014-05-05 19:09:33 -0700635 && !description.hasDebugHighlight
Chris Craikdeeda3d2014-05-05 19:09:33 -0700636 && !description.hasRoundRectClip) {
Romain Guy707b2f72010-10-11 16:34:59 -0700637 bool fast = false;
638
639 const bool noShader = !description.hasGradient && !description.hasBitmap;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700640 const bool singleTexture = (description.hasTexture || description.hasExternalTexture) &&
Romain Guy707b2f72010-10-11 16:34:59 -0700641 !description.hasAlpha8Texture && noShader;
642 const bool singleA8Texture = description.hasTexture &&
643 description.hasAlpha8Texture && noShader;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700644 const bool singleGradient = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700645 description.hasGradient && !description.hasBitmap &&
646 description.gradientType == ProgramDescription::kGradientLinear;
647
648 if (singleColor) {
649 shader.append(gFS_Fast_SingleColor);
650 fast = true;
651 } else if (singleTexture) {
652 if (!description.modulate) {
653 shader.append(gFS_Fast_SingleTexture);
654 } else {
655 shader.append(gFS_Fast_SingleModulateTexture);
656 }
657 fast = true;
658 } else if (singleA8Texture) {
659 if (!description.modulate) {
Romain Guy253f2c22016-09-28 17:34:42 -0700660 if (description.hasGammaCorrection) {
661 shader.append(gFS_Fast_SingleA8Texture_ApplyGamma);
662 } else {
663 shader.append(gFS_Fast_SingleA8Texture);
664 }
Romain Guy707b2f72010-10-11 16:34:59 -0700665 } else {
Romain Guy253f2c22016-09-28 17:34:42 -0700666 if (description.hasGammaCorrection) {
667 shader.append(gFS_Fast_SingleModulateA8Texture_ApplyGamma);
668 } else {
669 shader.append(gFS_Fast_SingleModulateA8Texture);
670 }
Romain Guy707b2f72010-10-11 16:34:59 -0700671 }
672 fast = true;
673 } else if (singleGradient) {
Romain Guy8762e332016-10-12 12:14:07 -0700674 shader.append(gFS_Gradient_Functions);
675 shader.append(gFS_Gradient_Preamble[mHasSRGB]);
Romain Guy707b2f72010-10-11 16:34:59 -0700676 if (!description.modulate) {
Romain Guy253f2c22016-09-28 17:34:42 -0700677 shader.append(gFS_Fast_SingleGradient[description.isSimpleGradient]);
Romain Guy707b2f72010-10-11 16:34:59 -0700678 } else {
Romain Guy253f2c22016-09-28 17:34:42 -0700679 shader.append(gFS_Fast_SingleModulateGradient[description.isSimpleGradient]);
Romain Guy707b2f72010-10-11 16:34:59 -0700680 }
681 fast = true;
682 }
683
684 if (fast) {
Romain Guyc15008e2010-11-10 11:59:15 -0800685#if DEBUG_PROGRAMS
Romain Guy707b2f72010-10-11 16:34:59 -0700686 PROGRAM_LOGD("*** Fast case:\n");
687 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
688 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800689#endif
Romain Guy707b2f72010-10-11 16:34:59 -0700690
691 return shader;
692 }
693 }
694
Romain Guyac670c02010-07-27 17:39:27 -0700695 if (description.hasBitmap) {
696 shader.append(gFS_Uniforms_BitmapSampler);
697 }
Chris Craikb9ce116d2015-08-20 15:14:06 -0700698 shader.append(gFS_Uniforms_ColorOp[static_cast<int>(description.colorOp)]);
Romain Guyac670c02010-07-27 17:39:27 -0700699
700 // Generate required functions
701 if (description.hasGradient && description.hasBitmap) {
Romain Guy48daa542010-08-10 19:21:34 -0700702 generateBlend(shader, "blendShaders", description.shadersMode);
Romain Guyac670c02010-07-27 17:39:27 -0700703 }
Chris Craikb9ce116d2015-08-20 15:14:06 -0700704 if (description.colorOp == ProgramDescription::ColorFilterMode::Blend) {
Romain Guy48daa542010-08-10 19:21:34 -0700705 generateBlend(shader, "blendColors", description.colorMode);
Romain Guyac670c02010-07-27 17:39:27 -0700706 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700707 if (blendFramebuffer) {
708 generateBlend(shader, "blendFramebuffer", description.framebufferMode);
709 }
Romain Guy889f8d12010-07-29 14:37:42 -0700710 if (description.isBitmapNpot) {
711 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
712 }
Romain Guy253f2c22016-09-28 17:34:42 -0700713 if (description.hasGradient) {
Romain Guy8762e332016-10-12 12:14:07 -0700714 shader.append(gFS_Gradient_Functions);
715 shader.append(gFS_Gradient_Preamble[mHasSRGB]);
Romain Guy253f2c22016-09-28 17:34:42 -0700716 }
Romain Guyac670c02010-07-27 17:39:27 -0700717
718 // Begin the shader
719 shader.append(gFS_Main); {
720 // Stores the result in fragColor directly
Romain Guyaa6c24c2011-04-28 18:40:04 -0700721 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700722 if (description.hasAlpha8Texture) {
Romain Guy707b2f72010-10-11 16:34:59 -0700723 if (!description.hasGradient && !description.hasBitmap) {
Romain Guy253f2c22016-09-28 17:34:42 -0700724 shader.append(
725 gFS_Main_FetchA8Texture[modulateOp * 2 + description.hasGammaCorrection]);
Romain Guy707b2f72010-10-11 16:34:59 -0700726 }
Romain Guyac670c02010-07-27 17:39:27 -0700727 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700728 shader.append(gFS_Main_FetchTexture[modulateOp]);
Romain Guyac670c02010-07-27 17:39:27 -0700729 }
730 } else {
Romain Guya938f562012-09-13 20:31:08 -0700731 if (!description.hasGradient && !description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700732 shader.append(gFS_Main_FetchColor);
733 }
Romain Guyac670c02010-07-27 17:39:27 -0700734 }
735 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700736 shader.append(gFS_Main_FetchGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700737 }
738 if (description.hasBitmap) {
Romain Guy889f8d12010-07-29 14:37:42 -0700739 if (!description.isBitmapNpot) {
740 shader.append(gFS_Main_FetchBitmap);
741 } else {
742 shader.append(gFS_Main_FetchBitmapNpot);
743 }
Romain Guyac670c02010-07-27 17:39:27 -0700744 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700745 bool applyModulate = false;
Romain Guyac670c02010-07-27 17:39:27 -0700746 // Case when we have two shaders set
747 if (description.hasGradient && description.hasBitmap) {
748 if (description.isBitmapFirst) {
749 shader.append(gFS_Main_BlendShadersBG);
750 } else {
751 shader.append(gFS_Main_BlendShadersGB);
752 }
Romain Guya938f562012-09-13 20:31:08 -0700753 applyModulate = shaderOp(description, shader, modulateOp,
754 gFS_Main_BlendShaders_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700755 } else {
756 if (description.hasGradient) {
Romain Guya938f562012-09-13 20:31:08 -0700757 applyModulate = shaderOp(description, shader, modulateOp,
758 gFS_Main_GradientShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700759 } else if (description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700760 applyModulate = shaderOp(description, shader, modulateOp,
761 gFS_Main_BitmapShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700762 }
Romain Guyac670c02010-07-27 17:39:27 -0700763 }
Romain Guya938f562012-09-13 20:31:08 -0700764
Romain Guy740bf2b2011-04-26 15:33:10 -0700765 if (description.modulate && applyModulate) {
Romain Guya938f562012-09-13 20:31:08 -0700766 shader.append(gFS_Main_ModulateColor);
Romain Guy740bf2b2011-04-26 15:33:10 -0700767 }
Romain Guya938f562012-09-13 20:31:08 -0700768
Romain Guyac670c02010-07-27 17:39:27 -0700769 // Apply the color op if needed
Chris Craikb9ce116d2015-08-20 15:14:06 -0700770 shader.append(gFS_Main_ApplyColorOp[static_cast<int>(description.colorOp)]);
Chris Craik9f44a132012-09-13 18:34:55 -0700771
Chris Craik91a8c7c2014-08-12 14:31:35 -0700772 if (description.hasVertexAlpha) {
773 if (description.useShadowAlphaInterp) {
774 shader.append(gFS_Main_ApplyVertexAlphaShadowInterp);
Chris Craikbf759452014-08-11 16:00:44 -0700775 } else {
Chris Craik91a8c7c2014-08-12 14:31:35 -0700776 shader.append(gFS_Main_ApplyVertexAlphaLinearInterp);
Chris Craikbf759452014-08-11 16:00:44 -0700777 }
Chris Craik9f44a132012-09-13 18:34:55 -0700778 }
779
Romain Guy253f2c22016-09-28 17:34:42 -0700780 if (description.hasGradient) {
781 shader.append(gFS_Main_AddDither);
782 }
783
Romain Guyac670c02010-07-27 17:39:27 -0700784 // Output the fragment
Romain Guya5aed0d2010-09-09 14:42:43 -0700785 if (!blendFramebuffer) {
786 shader.append(gFS_Main_FragColor);
787 } else {
Romain Guyf607bdc2010-09-10 19:20:06 -0700788 shader.append(!description.swapSrcDst ?
789 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap);
Romain Guya5aed0d2010-09-09 14:42:43 -0700790 }
Romain Guyff316ec2013-02-13 18:39:43 -0800791 if (description.hasColors) {
792 shader.append(gFS_Main_FragColor_HasColors);
793 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700794 if (description.hasRoundRectClip) {
795 shader.append(gFS_Main_FragColor_HasRoundRectClip);
796 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800797 if (description.hasDebugHighlight) {
798 shader.append(gFS_Main_DebugHighlight);
799 }
Romain Guyac670c02010-07-27 17:39:27 -0700800 }
801 // End the shader
802 shader.append(gFS_Footer);
803
Romain Guyc15008e2010-11-10 11:59:15 -0800804#if DEBUG_PROGRAMS
Romain Guydb1938e2010-08-02 18:50:22 -0700805 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
806 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800807#endif
Romain Guydb1938e2010-08-02 18:50:22 -0700808
Romain Guyac670c02010-07-27 17:39:27 -0700809 return shader;
810}
811
Mike Reedc2f31df2016-10-28 17:21:45 -0400812void ProgramCache::generateBlend(String8& shader, const char* name, SkBlendMode mode) {
Romain Guyac670c02010-07-27 17:39:27 -0700813 shader.append("\nvec4 ");
814 shader.append(name);
815 shader.append("(vec4 src, vec4 dst) {\n");
816 shader.append(" ");
Mike Reedc2f31df2016-10-28 17:21:45 -0400817 shader.append(gBlendOps[(int)mode]);
Romain Guyac670c02010-07-27 17:39:27 -0700818 shader.append("}\n");
819}
820
Romain Guy889f8d12010-07-29 14:37:42 -0700821void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) {
Romain Guy63553472012-07-18 20:04:14 -0700822 shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700823 if (wrapS == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700824 shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700825 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n");
826 }
827 if (wrapT == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700828 shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700829 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n");
830 }
831 shader.append(" return vec2(");
832 switch (wrapS) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700833 case GL_CLAMP_TO_EDGE:
834 shader.append("texCoords.x");
835 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700836 case GL_REPEAT:
837 shader.append("mod(texCoords.x, 1.0)");
838 break;
839 case GL_MIRRORED_REPEAT:
840 shader.append("xMod2");
841 break;
842 }
843 shader.append(", ");
844 switch (wrapT) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700845 case GL_CLAMP_TO_EDGE:
846 shader.append("texCoords.y");
847 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700848 case GL_REPEAT:
849 shader.append("mod(texCoords.y, 1.0)");
850 break;
851 case GL_MIRRORED_REPEAT:
852 shader.append("yMod2");
853 break;
854 }
855 shader.append(");\n");
856 shader.append("}\n");
857}
858
Romain Guydb1938e2010-08-02 18:50:22 -0700859void ProgramCache::printLongString(const String8& shader) const {
860 ssize_t index = 0;
861 ssize_t lastIndex = 0;
862 const char* str = shader.string();
863 while ((index = shader.find("\n", index)) > -1) {
864 String8 line(str, index - lastIndex);
865 if (line.length() == 0) line.append("\n");
Chris Craik1c1c3fe2015-03-06 09:40:35 -0800866 ALOGD("%s", line.string());
Romain Guydb1938e2010-08-02 18:50:22 -0700867 index++;
868 str += (index - lastIndex);
869 lastIndex = index;
870 }
871}
872
Romain Guyac670c02010-07-27 17:39:27 -0700873}; // namespace uirenderer
874}; // namespace android