blob: b767046f1a4f1e2b0112092e3eb66481d820e377 [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 =
Arun06e9f322017-01-23 11:59:21 +000061 "uniform mat4 roundRectInvTransform;\n"
62 "uniform mediump vec4 roundRectInnerRectLTWH;\n"
63 "uniform mediump float roundRectRadius;\n";
Romain Guyac670c02010-07-27 17:39:27 -070064const char* gVS_Header_Varyings_HasTexture =
65 "varying vec2 outTexCoords;\n";
Romain Guyff316ec2013-02-13 18:39:43 -080066const char* gVS_Header_Varyings_HasColors =
67 "varying vec4 outColors;\n";
Chris Craik91a8c7c2014-08-12 14:31:35 -070068const char* gVS_Header_Varyings_HasVertexAlpha =
Chris Craik6ebdc112012-08-31 18:24:33 -070069 "varying float alpha;\n";
Romain Guy63553472012-07-18 20:04:14 -070070const char* gVS_Header_Varyings_HasBitmap =
71 "varying highp vec2 outBitmapTexCoords;\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -070072const char* gVS_Header_Varyings_HasGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -070073 // Linear
Romain Guy253f2c22016-09-28 17:34:42 -070074 "varying highp vec2 linear;\n",
75 "varying float linear;\n",
Romain Guy211efea2012-07-31 21:16:07 -070076
Romain Guyee916f12010-09-20 17:53:08 -070077 // Circular
Romain Guy253f2c22016-09-28 17:34:42 -070078 "varying highp vec2 circular;\n",
79 "varying highp vec2 circular;\n",
Romain Guy211efea2012-07-31 21:16:07 -070080
Romain Guyee916f12010-09-20 17:53:08 -070081 // Sweep
Romain Guy253f2c22016-09-28 17:34:42 -070082 "varying highp vec2 sweep;\n",
83 "varying highp vec2 sweep;\n",
Romain Guyee916f12010-09-20 17:53:08 -070084};
Chris Craikdeeda3d2014-05-05 19:09:33 -070085const char* gVS_Header_Varyings_HasRoundRectClip =
Arun06e9f322017-01-23 11:59:21 +000086 "varying mediump vec2 roundRectPos;\n";
Romain Guyac670c02010-07-27 17:39:27 -070087const char* gVS_Main =
88 "\nvoid main(void) {\n";
89const char* gVS_Main_OutTexCoords =
90 " outTexCoords = texCoords;\n";
Romain Guyff316ec2013-02-13 18:39:43 -080091const char* gVS_Main_OutColors =
92 " outColors = colors;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -070093const char* gVS_Main_OutTransformedTexCoords =
94 " outTexCoords = (mainTextureTransform * vec4(texCoords, 0.0, 1.0)).xy;\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -070095const char* gVS_Main_OutGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -070096 // Linear
Romain Guy253f2c22016-09-28 17:34:42 -070097 " linear = vec2((screenSpace * position).x, 0.5);\n",
98 " linear = (screenSpace * position).x;\n",
Romain Guy211efea2012-07-31 21:16:07 -070099
Romain Guyee916f12010-09-20 17:53:08 -0700100 // Circular
Romain Guy253f2c22016-09-28 17:34:42 -0700101 " circular = (screenSpace * position).xy;\n",
102 " circular = (screenSpace * position).xy;\n",
Romain Guy211efea2012-07-31 21:16:07 -0700103
Romain Guyee916f12010-09-20 17:53:08 -0700104 // Sweep
Romain Guy253f2c22016-09-28 17:34:42 -0700105 " sweep = (screenSpace * position).xy;\n",
Chet Haasea1d12dd2012-09-21 14:50:14 -0700106 " sweep = (screenSpace * position).xy;\n"
Romain Guyee916f12010-09-20 17:53:08 -0700107};
Romain Guy889f8d12010-07-29 14:37:42 -0700108const char* gVS_Main_OutBitmapTexCoords =
Romain Guy707b2f72010-10-11 16:34:59 -0700109 " outBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700110const char* gVS_Main_Position =
Chris Craikdeeda3d2014-05-05 19:09:33 -0700111 " vec4 transformedPosition = projection * transform * position;\n"
112 " gl_Position = transformedPosition;\n";
Chris Craikbf759452014-08-11 16:00:44 -0700113
Chris Craik91a8c7c2014-08-12 14:31:35 -0700114const char* gVS_Main_VertexAlpha =
Chris Craik6ebdc112012-08-31 18:24:33 -0700115 " alpha = vtxAlpha;\n";
Chris Craikbf759452014-08-11 16:00:44 -0700116
Chris Craikdeeda3d2014-05-05 19:09:33 -0700117const char* gVS_Main_HasRoundRectClip =
Arun06e9f322017-01-23 11:59:21 +0000118 " roundRectPos = ((roundRectInvTransform * transformedPosition).xy / roundRectRadius) - roundRectInnerRectLTWH.xy;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700119const char* gVS_Footer =
120 "}\n\n";
121
122///////////////////////////////////////////////////////////////////////////////
123// Fragment shaders snippets
124///////////////////////////////////////////////////////////////////////////////
125
Chris Craik8bd68c62015-08-19 15:29:05 -0700126const char* gFS_Header_Start =
127 "#version 100\n";
Romain Guya5aed0d2010-09-09 14:42:43 -0700128const char* gFS_Header_Extension_FramebufferFetch =
129 "#extension GL_NV_shader_framebuffer_fetch : enable\n\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700130const char* gFS_Header_Extension_ExternalTexture =
131 "#extension GL_OES_EGL_image_external : require\n\n";
Romain Guyac670c02010-07-27 17:39:27 -0700132const char* gFS_Header =
133 "precision mediump float;\n\n";
134const char* gFS_Uniforms_Color =
135 "uniform vec4 color;\n";
136const char* gFS_Uniforms_TextureSampler =
Romain Guya938f562012-09-13 20:31:08 -0700137 "uniform sampler2D baseSampler;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700138const char* gFS_Uniforms_ExternalTextureSampler =
Romain Guya938f562012-09-13 20:31:08 -0700139 "uniform samplerExternalOES baseSampler;\n";
Romain Guyb4880042013-04-05 11:17:55 -0700140const char* gFS_Uniforms_GradientSampler[2] = {
Romain Guy253f2c22016-09-28 17:34:42 -0700141 "uniform vec2 screenSize;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700142 "uniform sampler2D gradientSampler;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700143
144 "uniform vec2 screenSize;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700145 "uniform vec4 startColor;\n"
Romain Guy211efea2012-07-31 21:16:07 -0700146 "uniform vec4 endColor;\n"
Romain Guyee916f12010-09-20 17:53:08 -0700147};
Romain Guyac670c02010-07-27 17:39:27 -0700148const char* gFS_Uniforms_BitmapSampler =
149 "uniform sampler2D bitmapSampler;\n";
sergeyv9c97e482016-12-12 16:14:11 -0800150const char* gFS_Uniforms_BitmapExternalSampler =
151 "uniform samplerExternalOES bitmapSampler;\n";
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500152const char* gFS_Uniforms_ColorOp[3] = {
Romain Guyac670c02010-07-27 17:39:27 -0700153 // None
154 "",
155 // Matrix
156 "uniform mat4 colorMatrix;\n"
157 "uniform vec4 colorMatrixVector;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700158 // PorterDuff
Romain Guydb1938e2010-08-02 18:50:22 -0700159 "uniform vec4 colorBlend;\n"
Romain Guyac670c02010-07-27 17:39:27 -0700160};
Romain Guy41210632012-07-16 17:04:24 -0700161
Chris Craikdeeda3d2014-05-05 19:09:33 -0700162const char* gFS_Uniforms_HasRoundRectClip =
Arun06e9f322017-01-23 11:59:21 +0000163 "uniform mediump vec4 roundRectInnerRectLTWH;\n"
164 "uniform mediump float roundRectRadius;\n";
Chris Craikdeeda3d2014-05-05 19:09:33 -0700165
Romain Guycaaaa662017-03-27 00:40:21 -0700166const char* gFS_Uniforms_ColorSpaceConversion =
167 // TODO: Should we use a 3D LUT to combine the matrix and transfer functions?
168 // 32x32x32 fp16 LUTs (for scRGB output) are large and heavy to generate...
169 "uniform mat3 colorSpaceMatrix;\n";
170
171const char* gFS_Uniforms_TransferFunction[4] = {
172 // In this order: g, a, b, c, d, e, f
173 // See ColorSpace::TransferParameters
174 // We'll use hardware sRGB conversion as much as possible
175 "",
176 "uniform float transferFunction[7];\n",
177 "uniform float transferFunction[5];\n",
178 "uniform float transferFunctionGamma;\n"
Romain Guy636afc12017-02-07 11:21:05 -0800179};
180
Romain Guycaaaa662017-03-27 00:40:21 -0700181const char* gFS_OETF[2] = {
182 R"__SHADER__(
183 vec4 OETF(const vec4 linear) {
184 return linear;
185 }
186 )__SHADER__",
187 // We expect linear data to be scRGB so we mirror the gamma function
188 R"__SHADER__(
189 vec4 OETF(const vec4 linear) {
190 return vec4(sign(linear.rgb) * OETF_sRGB(abs(linear.rgb)), linear.a);
191 }
192 )__SHADER__"
193};
194
195const char* gFS_ColorConvert[3] = {
196 // Just OETF
197 R"__SHADER__(
198 vec4 colorConvert(const vec4 color) {
199 return OETF(color);
200 }
201 )__SHADER__",
202 // Full color conversion for opaque bitmaps
203 R"__SHADER__(
204 vec4 colorConvert(const vec4 color) {
205 return OETF(vec4(colorSpaceMatrix * EOTF_Parametric(color.rgb), color.a));
206 }
207 )__SHADER__",
208 // Full color conversion for translucent bitmaps
209 // Note: 0.5/256=0.0019
210 R"__SHADER__(
211 vec4 colorConvert(in vec4 color) {
212 color.rgb /= color.a + 0.0019;
213 color = OETF(vec4(colorSpaceMatrix * EOTF_Parametric(color.rgb), color.a));
214 color.rgb *= color.a + 0.0019;
215 return color;
216 }
217 )__SHADER__",
218};
219
220const char* gFS_sRGB_TransferFunctions = R"__SHADER__(
Romain Guy636afc12017-02-07 11:21:05 -0800221 float OETF_sRGB(const float linear) {
222 // IEC 61966-2-1:1999
223 return linear <= 0.0031308 ? linear * 12.92 : (pow(linear, 1.0 / 2.4) * 1.055) - 0.055;
224 }
225
226 vec3 OETF_sRGB(const vec3 linear) {
227 return vec3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b));
228 }
229
230 float EOTF_sRGB(float srgb) {
231 // IEC 61966-2-1:1999
232 return srgb <= 0.04045 ? srgb / 12.92 : pow((srgb + 0.055) / 1.055, 2.4);
233 }
234)__SHADER__";
235
Romain Guycaaaa662017-03-27 00:40:21 -0700236const char* gFS_TransferFunction[4] = {
237 // Conversion done by the texture unit (sRGB)
238 R"__SHADER__(
239 vec3 EOTF_Parametric(const vec3 x) {
240 return x;
241 }
242 )__SHADER__",
243 // Full transfer function
244 // TODO: We should probably use a 1D LUT (256x1 with texelFetch() since input is 8 bit)
245 // TODO: That would cause 3 dependent texture fetches. Is it worth it?
246 R"__SHADER__(
247 float EOTF_Parametric(float x) {
248 return x <= transferFunction[4]
249 ? transferFunction[3] * x + transferFunction[6]
250 : pow(transferFunction[1] * x + transferFunction[2], transferFunction[0])
251 + transferFunction[5];
252 }
253
254 vec3 EOTF_Parametric(const vec3 x) {
255 return vec3(EOTF_Parametric(x.r), EOTF_Parametric(x.g), EOTF_Parametric(x.b));
256 }
257 )__SHADER__",
258 // Limited transfer function, e = f = 0.0
259 R"__SHADER__(
260 float EOTF_Parametric(float x) {
261 return x <= transferFunction[4]
262 ? transferFunction[3] * x
263 : pow(transferFunction[1] * x + transferFunction[2], transferFunction[0]);
264 }
265
266 vec3 EOTF_Parametric(const vec3 x) {
267 return vec3(EOTF_Parametric(x.r), EOTF_Parametric(x.g), EOTF_Parametric(x.b));
268 }
269 )__SHADER__",
270 // Gamma transfer function, e = f = 0.0
271 R"__SHADER__(
272 vec3 EOTF_Parametric(const vec3 x) {
273 return vec3(pow(x.r, transferFunctionGamma),
274 pow(x.g, transferFunctionGamma),
275 pow(x.b, transferFunctionGamma));
276 }
277 )__SHADER__"
278};
279
Romain Guy253f2c22016-09-28 17:34:42 -0700280// Dithering must be done in the quantization space
281// When we are writing to an sRGB framebuffer, we must do the following:
Romain Guy636afc12017-02-07 11:21:05 -0800282// EOTF(OETF(color) + dither)
Romain Guy0d86d7e2017-03-15 20:38:11 -0700283// The dithering pattern is generated with a triangle noise generator in the range [-1.0,1.0]
Romain Guy253f2c22016-09-28 17:34:42 -0700284// TODO: Handle linear fp16 render targets
Romain Guycaaaa662017-03-27 00:40:21 -0700285const char* gFS_GradientFunctions = R"__SHADER__(
Romain Guy9fe7e162017-02-03 16:16:07 -0800286 float triangleNoise(const highp vec2 n) {
287 highp vec2 p = fract(n * vec2(5.3987, 5.4421));
288 p += dot(p.yx, p.xy + vec2(21.5351, 14.3137));
289 highp float xy = p.x * p.y;
290 return fract(xy * 95.4307) + fract(xy * 75.04961) - 1.0;
291 }
Romain Guy9fe7e162017-02-03 16:16:07 -0800292)__SHADER__";
Romain Guycaaaa662017-03-27 00:40:21 -0700293
294const char* gFS_GradientPreamble[2] = {
Romain Guy253f2c22016-09-28 17:34:42 -0700295 // Linear framebuffer
Romain Guy6183c972017-03-16 12:24:55 -0700296 R"__SHADER__(
297 vec4 dither(const vec4 color) {
298 return color + (triangleNoise(gl_FragCoord.xy * screenSize.xy) / 255.0);
299 }
Romain Guy6183c972017-03-16 12:24:55 -0700300 )__SHADER__",
Romain Guy253f2c22016-09-28 17:34:42 -0700301 // sRGB framebuffer
Romain Guy6183c972017-03-16 12:24:55 -0700302 R"__SHADER__(
303 vec4 dither(const vec4 color) {
304 vec3 dithered = sqrt(color.rgb) + (triangleNoise(gl_FragCoord.xy * screenSize.xy) / 255.0);
305 return vec4(dithered * dithered, color.a);
306 }
Romain Guy6183c972017-03-16 12:24:55 -0700307 )__SHADER__",
Romain Guy253f2c22016-09-28 17:34:42 -0700308};
309
310// Uses luminance coefficients from Rec.709 to choose the appropriate gamma
311// The gamma() function assumes that bright text will be displayed on a dark
312// background and that dark text will be displayed on bright background
313// The gamma coefficient is chosen to thicken or thin the text accordingly
314// The dot product used to compute the luminance could be approximated with
315// a simple max(color.r, color.g, color.b)
Romain Guy9fe7e162017-02-03 16:16:07 -0800316const char* gFS_Gamma_Preamble = R"__SHADER__(
317 #define GAMMA (%.2f)
318 #define GAMMA_INV (%.2f)
319
320 float gamma(float a, const vec3 color) {
321 float luminance = dot(color, vec3(0.2126, 0.7152, 0.0722));
322 return pow(a, luminance < 0.5 ? GAMMA_INV : GAMMA);
323 }
324)__SHADER__";
Romain Guy253f2c22016-09-28 17:34:42 -0700325
Romain Guyac670c02010-07-27 17:39:27 -0700326const char* gFS_Main =
327 "\nvoid main(void) {\n"
Romain Guy253f2c22016-09-28 17:34:42 -0700328 " vec4 fragColor;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700329
Romain Guy253f2c22016-09-28 17:34:42 -0700330const char* gFS_Main_AddDither =
331 " fragColor = dither(fragColor);\n";
Romain Guy211efea2012-07-31 21:16:07 -0700332
Romain Guy707b2f72010-10-11 16:34:59 -0700333// General case
Romain Guyac670c02010-07-27 17:39:27 -0700334const char* gFS_Main_FetchColor =
335 " fragColor = color;\n";
Romain Guy740bf2b2011-04-26 15:33:10 -0700336const char* gFS_Main_ModulateColor =
337 " fragColor *= color.a;\n";
Chris Craik91a8c7c2014-08-12 14:31:35 -0700338const char* gFS_Main_ApplyVertexAlphaLinearInterp =
Chris Craik6ebdc112012-08-31 18:24:33 -0700339 " fragColor *= alpha;\n";
Chris Craik91a8c7c2014-08-12 14:31:35 -0700340const char* gFS_Main_ApplyVertexAlphaShadowInterp =
Chris Craik138c21f2016-04-28 16:59:42 -0700341 // map alpha through shadow alpha sampler
342 " fragColor *= texture2D(baseSampler, vec2(alpha, 0.5)).a;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700343const char* gFS_Main_FetchTexture[2] = {
344 // Don't modulate
Romain Guycaaaa662017-03-27 00:40:21 -0700345 " fragColor = colorConvert(texture2D(baseSampler, outTexCoords));\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700346 // Modulate
Romain Guycaaaa662017-03-27 00:40:21 -0700347 " fragColor = color * colorConvert(texture2D(baseSampler, outTexCoords));\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700348};
Romain Guy253f2c22016-09-28 17:34:42 -0700349const char* gFS_Main_FetchA8Texture[4] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700350 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700351 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700352 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700353 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700354 " fragColor = color * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700355 " fragColor = color * gamma(texture2D(baseSampler, outTexCoords).a, color.rgb);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700356};
Romain Guy42e1e0d2012-07-30 14:47:51 -0700357const char* gFS_Main_FetchGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700358 // Linear
Romain Guy320d46b2012-08-08 16:05:42 -0700359 " vec4 gradientColor = texture2D(gradientSampler, linear);\n",
Romain Guy211efea2012-07-31 21:16:07 -0700360
Derek Sollenberger669b15a2017-03-31 12:09:24 -0400361 " vec4 gradientColor = mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700362
Romain Guyee916f12010-09-20 17:53:08 -0700363 // Circular
Romain Guy320d46b2012-08-08 16:05:42 -0700364 " vec4 gradientColor = texture2D(gradientSampler, vec2(length(circular), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700365
Derek Sollenberger669b15a2017-03-31 12:09:24 -0400366 " vec4 gradientColor = mix(startColor, endColor, clamp(length(circular), 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700367
Romain Guyee916f12010-09-20 17:53:08 -0700368 // Sweep
Romain Guy63553472012-07-18 20:04:14 -0700369 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700370 " vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700371
Romain Guy42e1e0d2012-07-30 14:47:51 -0700372 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Derek Sollenberger669b15a2017-03-31 12:09:24 -0400373 " vec4 gradientColor = mix(startColor, endColor, clamp(index - floor(index), 0.0, 1.0));\n"
Romain Guyee916f12010-09-20 17:53:08 -0700374};
Romain Guyac670c02010-07-27 17:39:27 -0700375const char* gFS_Main_FetchBitmap =
Romain Guycaaaa662017-03-27 00:40:21 -0700376 " vec4 bitmapColor = colorConvert(texture2D(bitmapSampler, outBitmapTexCoords));\n";
Romain Guy889f8d12010-07-29 14:37:42 -0700377const char* gFS_Main_FetchBitmapNpot =
Romain Guycaaaa662017-03-27 00:40:21 -0700378 " vec4 bitmapColor = colorConvert(texture2D(bitmapSampler, wrap(outBitmapTexCoords)));\n";
Romain Guyac670c02010-07-27 17:39:27 -0700379const char* gFS_Main_BlendShadersBG =
Romain Guyac670c02010-07-27 17:39:27 -0700380 " fragColor = blendShaders(gradientColor, bitmapColor)";
Romain Guy06f96e22010-07-30 19:18:16 -0700381const char* gFS_Main_BlendShadersGB =
382 " fragColor = blendShaders(bitmapColor, gradientColor)";
Romain Guy253f2c22016-09-28 17:34:42 -0700383const char* gFS_Main_BlendShaders_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700384 // Don't modulate
385 ";\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700386 ";\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700387 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700388 " * color.a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700389 " * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700390 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700391 " * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700392 " * gamma(texture2D(baseSampler, outTexCoords).a, color.rgb);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700393};
Romain Guy253f2c22016-09-28 17:34:42 -0700394const char* gFS_Main_GradientShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700395 // Don't modulate
396 " fragColor = gradientColor;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700397 " fragColor = gradientColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700398 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700399 " fragColor = gradientColor * color.a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700400 " fragColor = gradientColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700401 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700402 " fragColor = gradientColor * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700403 " fragColor = gradientColor * gamma(texture2D(baseSampler, outTexCoords).a, gradientColor.rgb);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700404 };
Romain Guy253f2c22016-09-28 17:34:42 -0700405const char* gFS_Main_BitmapShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700406 // Don't modulate
407 " fragColor = bitmapColor;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700408 " fragColor = bitmapColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700409 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700410 " fragColor = bitmapColor * color.a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700411 " fragColor = bitmapColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700412 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700413 " fragColor = bitmapColor * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700414 " fragColor = bitmapColor * gamma(texture2D(baseSampler, outTexCoords).a, bitmapColor.rgb);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700415 };
Romain Guyac670c02010-07-27 17:39:27 -0700416const char* gFS_Main_FragColor =
417 " gl_FragColor = fragColor;\n";
Romain Guyff316ec2013-02-13 18:39:43 -0800418const char* gFS_Main_FragColor_HasColors =
419 " gl_FragColor *= outColors;\n";
Romain Guya5aed0d2010-09-09 14:42:43 -0700420const char* gFS_Main_FragColor_Blend =
421 " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n";
Romain Guyf607bdc2010-09-10 19:20:06 -0700422const char* gFS_Main_FragColor_Blend_Swap =
423 " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n";
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500424const char* gFS_Main_ApplyColorOp[3] = {
Romain Guyac670c02010-07-27 17:39:27 -0700425 // None
426 "",
427 // Matrix
Chris Craik73821c82014-09-16 17:32:13 -0700428 " fragColor.rgb /= (fragColor.a + 0.0019);\n" // un-premultiply
Romain Guyac670c02010-07-27 17:39:27 -0700429 " fragColor *= colorMatrix;\n"
Chris Craik73821c82014-09-16 17:32:13 -0700430 " fragColor += colorMatrixVector;\n"
431 " fragColor.rgb *= (fragColor.a + 0.0019);\n", // re-premultiply
Romain Guyac670c02010-07-27 17:39:27 -0700432 // PorterDuff
433 " fragColor = blendColors(colorBlend, fragColor);\n"
434};
Chris Craikdeeda3d2014-05-05 19:09:33 -0700435
Arun06e9f322017-01-23 11:59:21 +0000436// Note: LTWH (left top width height) -> xyzw
437// roundRectPos is now divided by roundRectRadius in vertex shader
438// after we also subtract roundRectInnerRectLTWH.xy from roundRectPos
Chris Craikdeeda3d2014-05-05 19:09:33 -0700439const char* gFS_Main_FragColor_HasRoundRectClip =
Arun06e9f322017-01-23 11:59:21 +0000440 " mediump vec2 fragToLT = -roundRectPos;\n"
441 " mediump vec2 fragFromRB = roundRectPos - roundRectInnerRectLTWH.zw;\n"
Chris Craikf99f3202014-08-05 15:31:52 -0700442
Arun06e9f322017-01-23 11:59:21 +0000443 // since distance is divided by radius, it's in [0;1] so precision is not an issue
444 // this also lets us clamp(0.0, 1.0) instead of max() which is cheaper on GPUs
445 " mediump vec2 dist = clamp(max(fragToLT, fragFromRB), 0.0, 1.0);\n"
446 " mediump float linearDist = clamp(roundRectRadius - (length(dist) * roundRectRadius), 0.0, 1.0);\n"
447 " gl_FragColor *= linearDist;\n";
Chris Craikdeeda3d2014-05-05 19:09:33 -0700448
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800449const char* gFS_Main_DebugHighlight =
450 " gl_FragColor.rgb = vec3(0.0, gl_FragColor.a, 0.0);\n";
Romain Guyac670c02010-07-27 17:39:27 -0700451const char* gFS_Footer =
452 "}\n\n";
453
454///////////////////////////////////////////////////////////////////////////////
455// PorterDuff snippets
456///////////////////////////////////////////////////////////////////////////////
457
Romain Guy48daa542010-08-10 19:21:34 -0700458const char* gBlendOps[18] = {
Romain Guyac670c02010-07-27 17:39:27 -0700459 // Clear
460 "return vec4(0.0, 0.0, 0.0, 0.0);\n",
461 // Src
462 "return src;\n",
463 // Dst
464 "return dst;\n",
465 // SrcOver
Romain Guy06f96e22010-07-30 19:18:16 -0700466 "return src + dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700467 // DstOver
Romain Guy06f96e22010-07-30 19:18:16 -0700468 "return dst + src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700469 // SrcIn
Romain Guy06f96e22010-07-30 19:18:16 -0700470 "return src * dst.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700471 // DstIn
Romain Guy06f96e22010-07-30 19:18:16 -0700472 "return dst * src.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700473 // SrcOut
Romain Guy06f96e22010-07-30 19:18:16 -0700474 "return src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700475 // DstOut
Romain Guy06f96e22010-07-30 19:18:16 -0700476 "return dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700477 // SrcAtop
478 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n",
479 // DstAtop
480 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n",
481 // Xor
Romain Guy48daa542010-08-10 19:21:34 -0700482 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, "
Romain Guyac670c02010-07-27 17:39:27 -0700483 "src.a + dst.a - 2.0 * src.a * dst.a);\n",
Derek Sollenbergerc0bf7002015-03-06 13:48:27 -0500484 // Plus
Romain Guy48daa542010-08-10 19:21:34 -0700485 "return min(src + dst, 1.0);\n",
Derek Sollenbergerc0bf7002015-03-06 13:48:27 -0500486 // Modulate
Romain Guy48daa542010-08-10 19:21:34 -0700487 "return src * dst;\n",
488 // Screen
489 "return src + dst - src * dst;\n",
490 // Overlay
491 "return clamp(vec4(mix("
492 "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), "
493 "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), "
494 "step(dst.a, 2.0 * dst.rgb)), "
495 "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n",
496 // Darken
497 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
498 "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
499 // Lighten
500 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
501 "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 -0700502};
503
504///////////////////////////////////////////////////////////////////////////////
505// Constructors/destructors
506///////////////////////////////////////////////////////////////////////////////
507
John Reck8dc02f92017-07-17 09:55:02 -0700508ProgramCache::ProgramCache(const Extensions& extensions)
Romain Guy253f2c22016-09-28 17:34:42 -0700509 : mHasES3(extensions.getMajorGlVersion() >= 3)
Romain Guyefb4b062017-02-27 11:00:04 -0800510 , mHasLinearBlending(extensions.hasLinearBlending()) {
Romain Guyac670c02010-07-27 17:39:27 -0700511}
512
513ProgramCache::~ProgramCache() {
514 clear();
515}
516
517///////////////////////////////////////////////////////////////////////////////
518// Cache management
519///////////////////////////////////////////////////////////////////////////////
520
521void ProgramCache::clear() {
Romain Guy67f27952010-12-07 20:09:23 -0800522 PROGRAM_LOGD("Clearing program cache");
Romain Guyac670c02010-07-27 17:39:27 -0700523 mCache.clear();
524}
525
526Program* ProgramCache::get(const ProgramDescription& description) {
527 programid key = description.key();
Chris Craik096b8d92013-03-01 11:08:11 -0800528 if (key == (PROGRAM_KEY_TEXTURE | PROGRAM_KEY_A8_TEXTURE)) {
529 // program for A8, unmodulated, texture w/o shader (black text/path textures) is equivalent
530 // to standard texture program (bitmaps, patches). Consider them equivalent.
531 key = PROGRAM_KEY_TEXTURE;
532 }
533
Chris Craik51d6a3d2014-12-22 17:16:56 -0800534 auto iter = mCache.find(key);
Chris Craikd41c4d82015-01-05 15:51:13 -0800535 Program* program = nullptr;
Chris Craik51d6a3d2014-12-22 17:16:56 -0800536 if (iter == mCache.end()) {
Romain Guyee916f12010-09-20 17:53:08 -0700537 description.log("Could not find program");
Romain Guyac670c02010-07-27 17:39:27 -0700538 program = generateProgram(description, key);
Chris Craik51d6a3d2014-12-22 17:16:56 -0800539 mCache[key] = std::unique_ptr<Program>(program);
Romain Guyac670c02010-07-27 17:39:27 -0700540 } else {
Chris Craik51d6a3d2014-12-22 17:16:56 -0800541 program = iter->second.get();
Romain Guyac670c02010-07-27 17:39:27 -0700542 }
543 return program;
544}
545
546///////////////////////////////////////////////////////////////////////////////
547// Program generation
548///////////////////////////////////////////////////////////////////////////////
549
Andreas Gampe64bb4132014-11-22 00:35:09 +0000550Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) {
Romain Guyac670c02010-07-27 17:39:27 -0700551 String8 vertexShader = generateVertexShader(description);
552 String8 fragmentShader = generateFragmentShader(description);
553
Romain Guy42e1e0d2012-07-30 14:47:51 -0700554 return new Program(description, vertexShader.string(), fragmentShader.string());
555}
556
557static inline size_t gradientIndex(const ProgramDescription& description) {
558 return description.gradientType * 2 + description.isSimpleGradient;
Romain Guyac670c02010-07-27 17:39:27 -0700559}
560
561String8 ProgramCache::generateVertexShader(const ProgramDescription& description) {
562 // Add attributes
Chris Craik8bd68c62015-08-19 15:29:05 -0700563 String8 shader(gVS_Header_Start);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700564 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700565 shader.append(gVS_Header_Attributes_TexCoords);
566 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700567 if (description.hasVertexAlpha) {
568 shader.append(gVS_Header_Attributes_VertexAlphaParameters);
Chet Haase5b0200b2011-04-13 17:58:08 -0700569 }
Romain Guyff316ec2013-02-13 18:39:43 -0800570 if (description.hasColors) {
571 shader.append(gVS_Header_Attributes_Colors);
572 }
Romain Guyac670c02010-07-27 17:39:27 -0700573 // Uniforms
574 shader.append(gVS_Header_Uniforms);
Romain Guy8f0095c2011-05-02 17:24:22 -0700575 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700576 shader.append(gVS_Header_Uniforms_TextureTransform);
577 }
Romain Guyac670c02010-07-27 17:39:27 -0700578 if (description.hasGradient) {
Romain Guyb4880042013-04-05 11:17:55 -0700579 shader.append(gVS_Header_Uniforms_HasGradient);
Romain Guyac670c02010-07-27 17:39:27 -0700580 }
Romain Guy889f8d12010-07-29 14:37:42 -0700581 if (description.hasBitmap) {
582 shader.append(gVS_Header_Uniforms_HasBitmap);
583 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700584 if (description.hasRoundRectClip) {
585 shader.append(gVS_Header_Uniforms_HasRoundRectClip);
586 }
Romain Guyac670c02010-07-27 17:39:27 -0700587 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700588 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700589 shader.append(gVS_Header_Varyings_HasTexture);
590 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700591 if (description.hasVertexAlpha) {
592 shader.append(gVS_Header_Varyings_HasVertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700593 }
Romain Guyff316ec2013-02-13 18:39:43 -0800594 if (description.hasColors) {
595 shader.append(gVS_Header_Varyings_HasColors);
596 }
Romain Guyac670c02010-07-27 17:39:27 -0700597 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700598 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700599 }
600 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700601 shader.append(gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700602 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700603 if (description.hasRoundRectClip) {
604 shader.append(gVS_Header_Varyings_HasRoundRectClip);
605 }
Romain Guyac670c02010-07-27 17:39:27 -0700606
607 // Begin the shader
608 shader.append(gVS_Main); {
Romain Guy8f0095c2011-05-02 17:24:22 -0700609 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700610 shader.append(gVS_Main_OutTransformedTexCoords);
Romain Guy8f0095c2011-05-02 17:24:22 -0700611 } else if (description.hasTexture || description.hasExternalTexture) {
612 shader.append(gVS_Main_OutTexCoords);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700613 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700614 if (description.hasVertexAlpha) {
615 shader.append(gVS_Main_VertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700616 }
Romain Guyff316ec2013-02-13 18:39:43 -0800617 if (description.hasColors) {
618 shader.append(gVS_Main_OutColors);
619 }
Romain Guy889f8d12010-07-29 14:37:42 -0700620 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700621 shader.append(gVS_Main_OutBitmapTexCoords);
Romain Guy889f8d12010-07-29 14:37:42 -0700622 }
Romain Guyac670c02010-07-27 17:39:27 -0700623 // Output transformed position
624 shader.append(gVS_Main_Position);
Chet Haasea1d12dd2012-09-21 14:50:14 -0700625 if (description.hasGradient) {
626 shader.append(gVS_Main_OutGradient[gradientIndex(description)]);
627 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700628 if (description.hasRoundRectClip) {
629 shader.append(gVS_Main_HasRoundRectClip);
630 }
Romain Guyac670c02010-07-27 17:39:27 -0700631 }
632 // End the shader
633 shader.append(gVS_Footer);
634
635 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string());
636
637 return shader;
638}
639
Romain Guya938f562012-09-13 20:31:08 -0700640static bool shaderOp(const ProgramDescription& description, String8& shader,
641 const int modulateOp, const char** snippets) {
642 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
Romain Guy253f2c22016-09-28 17:34:42 -0700643 op = op * 2 + description.hasGammaCorrection;
Romain Guya938f562012-09-13 20:31:08 -0700644 shader.append(snippets[op]);
645 return description.hasAlpha8Texture;
646}
647
Romain Guyac670c02010-07-27 17:39:27 -0700648String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) {
Chris Craik8bd68c62015-08-19 15:29:05 -0700649 String8 shader(gFS_Header_Start);
Romain Guya5aed0d2010-09-09 14:42:43 -0700650
Mike Reedc2f31df2016-10-28 17:21:45 -0400651 const bool blendFramebuffer = description.framebufferMode >= SkBlendMode::kPlus;
Romain Guya5aed0d2010-09-09 14:42:43 -0700652 if (blendFramebuffer) {
653 shader.append(gFS_Header_Extension_FramebufferFetch);
654 }
sergeyv9c97e482016-12-12 16:14:11 -0800655 if (description.hasExternalTexture
656 || (description.hasBitmap && description.isShaderBitmapExternal)) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700657 shader.append(gFS_Header_Extension_ExternalTexture);
658 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700659
660 shader.append(gFS_Header);
Romain Guyac670c02010-07-27 17:39:27 -0700661
662 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700663 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700664 shader.append(gVS_Header_Varyings_HasTexture);
665 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700666 if (description.hasVertexAlpha) {
667 shader.append(gVS_Header_Varyings_HasVertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700668 }
Romain Guyff316ec2013-02-13 18:39:43 -0800669 if (description.hasColors) {
670 shader.append(gVS_Header_Varyings_HasColors);
671 }
Romain Guyac670c02010-07-27 17:39:27 -0700672 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700673 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700674 }
675 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700676 shader.append(gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700677 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700678 if (description.hasRoundRectClip) {
679 shader.append(gVS_Header_Varyings_HasRoundRectClip);
680 }
Romain Guyac670c02010-07-27 17:39:27 -0700681
Romain Guyac670c02010-07-27 17:39:27 -0700682 // Uniforms
Romain Guy707b2f72010-10-11 16:34:59 -0700683 int modulateOp = MODULATE_OP_NO_MODULATE;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700684 const bool singleColor = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700685 !description.hasGradient && !description.hasBitmap;
686
687 if (description.modulate || singleColor) {
688 shader.append(gFS_Uniforms_Color);
689 if (!singleColor) modulateOp = MODULATE_OP_MODULATE;
690 }
Chris Craik138c21f2016-04-28 16:59:42 -0700691 if (description.hasTexture || description.useShadowAlphaInterp) {
Romain Guyac670c02010-07-27 17:39:27 -0700692 shader.append(gFS_Uniforms_TextureSampler);
Romain Guy8f0095c2011-05-02 17:24:22 -0700693 } else if (description.hasExternalTexture) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700694 shader.append(gFS_Uniforms_ExternalTextureSampler);
695 }
Romain Guyac670c02010-07-27 17:39:27 -0700696 if (description.hasGradient) {
Romain Guy253f2c22016-09-28 17:34:42 -0700697 shader.append(gFS_Uniforms_GradientSampler[description.isSimpleGradient]);
Romain Guyac670c02010-07-27 17:39:27 -0700698 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700699 if (description.hasRoundRectClip) {
700 shader.append(gFS_Uniforms_HasRoundRectClip);
701 }
Romain Guy707b2f72010-10-11 16:34:59 -0700702
Romain Guy253f2c22016-09-28 17:34:42 -0700703 if (description.hasGammaCorrection) {
704 shader.appendFormat(gFS_Gamma_Preamble, Properties::textGamma, 1.0f / Properties::textGamma);
705 }
706
Romain Guyac670c02010-07-27 17:39:27 -0700707 if (description.hasBitmap) {
sergeyv9c97e482016-12-12 16:14:11 -0800708 if (description.isShaderBitmapExternal) {
709 shader.append(gFS_Uniforms_BitmapExternalSampler);
710 } else {
711 shader.append(gFS_Uniforms_BitmapSampler);
712 }
Romain Guyac670c02010-07-27 17:39:27 -0700713 }
Chris Craikb9ce116d2015-08-20 15:14:06 -0700714 shader.append(gFS_Uniforms_ColorOp[static_cast<int>(description.colorOp)]);
Romain Guyac670c02010-07-27 17:39:27 -0700715
Romain Guycaaaa662017-03-27 00:40:21 -0700716 if (description.hasColorSpaceConversion) {
717 shader.append(gFS_Uniforms_ColorSpaceConversion);
718 }
719 shader.append(gFS_Uniforms_TransferFunction[static_cast<int>(description.transferFunction)]);
720
Romain Guyac670c02010-07-27 17:39:27 -0700721 // Generate required functions
722 if (description.hasGradient && description.hasBitmap) {
Romain Guy48daa542010-08-10 19:21:34 -0700723 generateBlend(shader, "blendShaders", description.shadersMode);
Romain Guyac670c02010-07-27 17:39:27 -0700724 }
Chris Craikb9ce116d2015-08-20 15:14:06 -0700725 if (description.colorOp == ProgramDescription::ColorFilterMode::Blend) {
Romain Guy48daa542010-08-10 19:21:34 -0700726 generateBlend(shader, "blendColors", description.colorMode);
Romain Guyac670c02010-07-27 17:39:27 -0700727 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700728 if (blendFramebuffer) {
729 generateBlend(shader, "blendFramebuffer", description.framebufferMode);
730 }
sergeyv554ffeb2016-11-15 18:01:21 -0800731 if (description.useShaderBasedWrap) {
Romain Guy889f8d12010-07-29 14:37:42 -0700732 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
733 }
Romain Guycaaaa662017-03-27 00:40:21 -0700734 if (description.hasGradient || description.hasLinearTexture
735 || description.hasColorSpaceConversion) {
736 shader.append(gFS_sRGB_TransferFunctions);
Romain Guy636afc12017-02-07 11:21:05 -0800737 }
738 if (description.hasBitmap || ((description.hasTexture || description.hasExternalTexture) &&
739 !description.hasAlpha8Texture)) {
Romain Guycaaaa662017-03-27 00:40:21 -0700740 shader.append(gFS_TransferFunction[static_cast<int>(description.transferFunction)]);
741 shader.append(gFS_OETF[(description.hasLinearTexture || description.hasColorSpaceConversion)
742 && !mHasLinearBlending]);
743 shader.append(gFS_ColorConvert[description.hasColorSpaceConversion
744 ? 1 + description.hasTranslucentConversion : 0]);
Romain Guy636afc12017-02-07 11:21:05 -0800745 }
Romain Guy253f2c22016-09-28 17:34:42 -0700746 if (description.hasGradient) {
Romain Guycaaaa662017-03-27 00:40:21 -0700747 shader.append(gFS_GradientFunctions);
748 shader.append(gFS_GradientPreamble[mHasLinearBlending]);
Romain Guy253f2c22016-09-28 17:34:42 -0700749 }
Romain Guyac670c02010-07-27 17:39:27 -0700750
751 // Begin the shader
752 shader.append(gFS_Main); {
753 // Stores the result in fragColor directly
Romain Guyaa6c24c2011-04-28 18:40:04 -0700754 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700755 if (description.hasAlpha8Texture) {
Romain Guy707b2f72010-10-11 16:34:59 -0700756 if (!description.hasGradient && !description.hasBitmap) {
Romain Guy253f2c22016-09-28 17:34:42 -0700757 shader.append(
758 gFS_Main_FetchA8Texture[modulateOp * 2 + description.hasGammaCorrection]);
Romain Guy707b2f72010-10-11 16:34:59 -0700759 }
Romain Guyac670c02010-07-27 17:39:27 -0700760 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700761 shader.append(gFS_Main_FetchTexture[modulateOp]);
Romain Guyac670c02010-07-27 17:39:27 -0700762 }
763 } else {
Romain Guya938f562012-09-13 20:31:08 -0700764 if (!description.hasGradient && !description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700765 shader.append(gFS_Main_FetchColor);
766 }
Romain Guyac670c02010-07-27 17:39:27 -0700767 }
768 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700769 shader.append(gFS_Main_FetchGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700770 }
771 if (description.hasBitmap) {
sergeyv554ffeb2016-11-15 18:01:21 -0800772 if (!description.useShaderBasedWrap) {
Romain Guy889f8d12010-07-29 14:37:42 -0700773 shader.append(gFS_Main_FetchBitmap);
774 } else {
775 shader.append(gFS_Main_FetchBitmapNpot);
776 }
Romain Guyac670c02010-07-27 17:39:27 -0700777 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700778 bool applyModulate = false;
Romain Guyac670c02010-07-27 17:39:27 -0700779 // Case when we have two shaders set
780 if (description.hasGradient && description.hasBitmap) {
781 if (description.isBitmapFirst) {
782 shader.append(gFS_Main_BlendShadersBG);
783 } else {
784 shader.append(gFS_Main_BlendShadersGB);
785 }
Romain Guya938f562012-09-13 20:31:08 -0700786 applyModulate = shaderOp(description, shader, modulateOp,
787 gFS_Main_BlendShaders_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700788 } else {
789 if (description.hasGradient) {
Romain Guya938f562012-09-13 20:31:08 -0700790 applyModulate = shaderOp(description, shader, modulateOp,
791 gFS_Main_GradientShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700792 } else if (description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700793 applyModulate = shaderOp(description, shader, modulateOp,
794 gFS_Main_BitmapShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700795 }
Romain Guyac670c02010-07-27 17:39:27 -0700796 }
Romain Guya938f562012-09-13 20:31:08 -0700797
Romain Guy740bf2b2011-04-26 15:33:10 -0700798 if (description.modulate && applyModulate) {
Romain Guya938f562012-09-13 20:31:08 -0700799 shader.append(gFS_Main_ModulateColor);
Romain Guy740bf2b2011-04-26 15:33:10 -0700800 }
Romain Guya938f562012-09-13 20:31:08 -0700801
Romain Guyac670c02010-07-27 17:39:27 -0700802 // Apply the color op if needed
Chris Craikb9ce116d2015-08-20 15:14:06 -0700803 shader.append(gFS_Main_ApplyColorOp[static_cast<int>(description.colorOp)]);
Chris Craik9f44a132012-09-13 18:34:55 -0700804
Chris Craik91a8c7c2014-08-12 14:31:35 -0700805 if (description.hasVertexAlpha) {
806 if (description.useShadowAlphaInterp) {
807 shader.append(gFS_Main_ApplyVertexAlphaShadowInterp);
Chris Craikbf759452014-08-11 16:00:44 -0700808 } else {
Chris Craik91a8c7c2014-08-12 14:31:35 -0700809 shader.append(gFS_Main_ApplyVertexAlphaLinearInterp);
Chris Craikbf759452014-08-11 16:00:44 -0700810 }
Chris Craik9f44a132012-09-13 18:34:55 -0700811 }
812
Romain Guy253f2c22016-09-28 17:34:42 -0700813 if (description.hasGradient) {
814 shader.append(gFS_Main_AddDither);
815 }
816
Romain Guyac670c02010-07-27 17:39:27 -0700817 // Output the fragment
Romain Guya5aed0d2010-09-09 14:42:43 -0700818 if (!blendFramebuffer) {
819 shader.append(gFS_Main_FragColor);
820 } else {
Romain Guyf607bdc2010-09-10 19:20:06 -0700821 shader.append(!description.swapSrcDst ?
822 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap);
Romain Guya5aed0d2010-09-09 14:42:43 -0700823 }
Romain Guyff316ec2013-02-13 18:39:43 -0800824 if (description.hasColors) {
825 shader.append(gFS_Main_FragColor_HasColors);
826 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700827 if (description.hasRoundRectClip) {
828 shader.append(gFS_Main_FragColor_HasRoundRectClip);
829 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800830 if (description.hasDebugHighlight) {
831 shader.append(gFS_Main_DebugHighlight);
832 }
Romain Guyac670c02010-07-27 17:39:27 -0700833 }
834 // End the shader
835 shader.append(gFS_Footer);
836
Romain Guy91a8ec02017-02-08 07:45:11 -0800837#if DEBUG_PROGRAMS
Romain Guydb1938e2010-08-02 18:50:22 -0700838 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
839 printLongString(shader);
Romain Guy91a8ec02017-02-08 07:45:11 -0800840#endif
Romain Guydb1938e2010-08-02 18:50:22 -0700841
Romain Guyac670c02010-07-27 17:39:27 -0700842 return shader;
843}
844
Mike Reedc2f31df2016-10-28 17:21:45 -0400845void ProgramCache::generateBlend(String8& shader, const char* name, SkBlendMode mode) {
Romain Guyac670c02010-07-27 17:39:27 -0700846 shader.append("\nvec4 ");
847 shader.append(name);
848 shader.append("(vec4 src, vec4 dst) {\n");
849 shader.append(" ");
Mike Reedc2f31df2016-10-28 17:21:45 -0400850 shader.append(gBlendOps[(int)mode]);
Romain Guyac670c02010-07-27 17:39:27 -0700851 shader.append("}\n");
852}
853
Romain Guy889f8d12010-07-29 14:37:42 -0700854void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) {
Romain Guy63553472012-07-18 20:04:14 -0700855 shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700856 if (wrapS == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700857 shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700858 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n");
859 }
860 if (wrapT == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700861 shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700862 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n");
863 }
864 shader.append(" return vec2(");
865 switch (wrapS) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700866 case GL_CLAMP_TO_EDGE:
867 shader.append("texCoords.x");
868 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700869 case GL_REPEAT:
870 shader.append("mod(texCoords.x, 1.0)");
871 break;
872 case GL_MIRRORED_REPEAT:
873 shader.append("xMod2");
874 break;
875 }
876 shader.append(", ");
877 switch (wrapT) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700878 case GL_CLAMP_TO_EDGE:
879 shader.append("texCoords.y");
880 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700881 case GL_REPEAT:
882 shader.append("mod(texCoords.y, 1.0)");
883 break;
884 case GL_MIRRORED_REPEAT:
885 shader.append("yMod2");
886 break;
887 }
888 shader.append(");\n");
889 shader.append("}\n");
890}
891
Romain Guydb1938e2010-08-02 18:50:22 -0700892void ProgramCache::printLongString(const String8& shader) const {
893 ssize_t index = 0;
894 ssize_t lastIndex = 0;
895 const char* str = shader.string();
896 while ((index = shader.find("\n", index)) > -1) {
897 String8 line(str, index - lastIndex);
898 if (line.length() == 0) line.append("\n");
Chris Craik1c1c3fe2015-03-06 09:40:35 -0800899 ALOGD("%s", line.string());
Romain Guydb1938e2010-08-02 18:50:22 -0700900 index++;
901 str += (index - lastIndex);
902 lastIndex = index;
903 }
904}
905
Romain Guyac670c02010-07-27 17:39:27 -0700906}; // namespace uirenderer
907}; // namespace android