blob: d0f0949d5e78fc9a51c5d123b2854c02d75891b3 [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";
sergeyv9c97e482016-12-12 16:14:11 -0800148const char* gFS_Uniforms_BitmapExternalSampler =
149 "uniform samplerExternalOES bitmapSampler;\n";
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500150const char* gFS_Uniforms_ColorOp[3] = {
Romain Guyac670c02010-07-27 17:39:27 -0700151 // None
152 "",
153 // Matrix
154 "uniform mat4 colorMatrix;\n"
155 "uniform vec4 colorMatrixVector;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700156 // PorterDuff
Romain Guydb1938e2010-08-02 18:50:22 -0700157 "uniform vec4 colorBlend;\n"
Romain Guyac670c02010-07-27 17:39:27 -0700158};
Romain Guy41210632012-07-16 17:04:24 -0700159
Chris Craikdeeda3d2014-05-05 19:09:33 -0700160const char* gFS_Uniforms_HasRoundRectClip =
161 "uniform vec4 roundRectInnerRectLTRB;\n"
162 "uniform float roundRectRadius;\n";
163
Romain Guycaaaa662017-03-27 00:40:21 -0700164const char* gFS_Uniforms_ColorSpaceConversion =
165 // TODO: Should we use a 3D LUT to combine the matrix and transfer functions?
166 // 32x32x32 fp16 LUTs (for scRGB output) are large and heavy to generate...
167 "uniform mat3 colorSpaceMatrix;\n";
168
169const char* gFS_Uniforms_TransferFunction[4] = {
170 // In this order: g, a, b, c, d, e, f
171 // See ColorSpace::TransferParameters
172 // We'll use hardware sRGB conversion as much as possible
173 "",
174 "uniform float transferFunction[7];\n",
175 "uniform float transferFunction[5];\n",
176 "uniform float transferFunctionGamma;\n"
Romain Guy636afc12017-02-07 11:21:05 -0800177};
178
Romain Guycaaaa662017-03-27 00:40:21 -0700179const char* gFS_OETF[2] = {
180 R"__SHADER__(
181 vec4 OETF(const vec4 linear) {
182 return linear;
183 }
184 )__SHADER__",
185 // We expect linear data to be scRGB so we mirror the gamma function
186 R"__SHADER__(
187 vec4 OETF(const vec4 linear) {
188 return vec4(sign(linear.rgb) * OETF_sRGB(abs(linear.rgb)), linear.a);
189 }
190 )__SHADER__"
191};
192
193const char* gFS_ColorConvert[3] = {
194 // Just OETF
195 R"__SHADER__(
196 vec4 colorConvert(const vec4 color) {
197 return OETF(color);
198 }
199 )__SHADER__",
200 // Full color conversion for opaque bitmaps
201 R"__SHADER__(
202 vec4 colorConvert(const vec4 color) {
203 return OETF(vec4(colorSpaceMatrix * EOTF_Parametric(color.rgb), color.a));
204 }
205 )__SHADER__",
206 // Full color conversion for translucent bitmaps
207 // Note: 0.5/256=0.0019
208 R"__SHADER__(
209 vec4 colorConvert(in vec4 color) {
210 color.rgb /= color.a + 0.0019;
211 color = OETF(vec4(colorSpaceMatrix * EOTF_Parametric(color.rgb), color.a));
212 color.rgb *= color.a + 0.0019;
213 return color;
214 }
215 )__SHADER__",
216};
217
218const char* gFS_sRGB_TransferFunctions = R"__SHADER__(
Romain Guy636afc12017-02-07 11:21:05 -0800219 float OETF_sRGB(const float linear) {
220 // IEC 61966-2-1:1999
221 return linear <= 0.0031308 ? linear * 12.92 : (pow(linear, 1.0 / 2.4) * 1.055) - 0.055;
222 }
223
224 vec3 OETF_sRGB(const vec3 linear) {
225 return vec3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b));
226 }
227
228 float EOTF_sRGB(float srgb) {
229 // IEC 61966-2-1:1999
230 return srgb <= 0.04045 ? srgb / 12.92 : pow((srgb + 0.055) / 1.055, 2.4);
231 }
232)__SHADER__";
233
Romain Guycaaaa662017-03-27 00:40:21 -0700234const char* gFS_TransferFunction[4] = {
235 // Conversion done by the texture unit (sRGB)
236 R"__SHADER__(
237 vec3 EOTF_Parametric(const vec3 x) {
238 return x;
239 }
240 )__SHADER__",
241 // Full transfer function
242 // TODO: We should probably use a 1D LUT (256x1 with texelFetch() since input is 8 bit)
243 // TODO: That would cause 3 dependent texture fetches. Is it worth it?
244 R"__SHADER__(
245 float EOTF_Parametric(float x) {
246 return x <= transferFunction[4]
247 ? transferFunction[3] * x + transferFunction[6]
248 : pow(transferFunction[1] * x + transferFunction[2], transferFunction[0])
249 + transferFunction[5];
250 }
251
252 vec3 EOTF_Parametric(const vec3 x) {
253 return vec3(EOTF_Parametric(x.r), EOTF_Parametric(x.g), EOTF_Parametric(x.b));
254 }
255 )__SHADER__",
256 // Limited transfer function, e = f = 0.0
257 R"__SHADER__(
258 float EOTF_Parametric(float x) {
259 return x <= transferFunction[4]
260 ? transferFunction[3] * x
261 : pow(transferFunction[1] * x + transferFunction[2], transferFunction[0]);
262 }
263
264 vec3 EOTF_Parametric(const vec3 x) {
265 return vec3(EOTF_Parametric(x.r), EOTF_Parametric(x.g), EOTF_Parametric(x.b));
266 }
267 )__SHADER__",
268 // Gamma transfer function, e = f = 0.0
269 R"__SHADER__(
270 vec3 EOTF_Parametric(const vec3 x) {
271 return vec3(pow(x.r, transferFunctionGamma),
272 pow(x.g, transferFunctionGamma),
273 pow(x.b, transferFunctionGamma));
274 }
275 )__SHADER__"
276};
277
Romain Guy253f2c22016-09-28 17:34:42 -0700278// Dithering must be done in the quantization space
279// When we are writing to an sRGB framebuffer, we must do the following:
Romain Guy636afc12017-02-07 11:21:05 -0800280// EOTF(OETF(color) + dither)
Romain Guy0d86d7e2017-03-15 20:38:11 -0700281// The dithering pattern is generated with a triangle noise generator in the range [-1.0,1.0]
Romain Guy253f2c22016-09-28 17:34:42 -0700282// TODO: Handle linear fp16 render targets
Romain Guycaaaa662017-03-27 00:40:21 -0700283const char* gFS_GradientFunctions = R"__SHADER__(
Romain Guy9fe7e162017-02-03 16:16:07 -0800284 float triangleNoise(const highp vec2 n) {
285 highp vec2 p = fract(n * vec2(5.3987, 5.4421));
286 p += dot(p.yx, p.xy + vec2(21.5351, 14.3137));
287 highp float xy = p.x * p.y;
288 return fract(xy * 95.4307) + fract(xy * 75.04961) - 1.0;
289 }
Romain Guy9fe7e162017-02-03 16:16:07 -0800290)__SHADER__";
Romain Guycaaaa662017-03-27 00:40:21 -0700291
292const char* gFS_GradientPreamble[2] = {
Romain Guy253f2c22016-09-28 17:34:42 -0700293 // Linear framebuffer
Romain Guy6183c972017-03-16 12:24:55 -0700294 R"__SHADER__(
295 vec4 dither(const vec4 color) {
296 return color + (triangleNoise(gl_FragCoord.xy * screenSize.xy) / 255.0);
297 }
Romain Guy6183c972017-03-16 12:24:55 -0700298 )__SHADER__",
Romain Guy253f2c22016-09-28 17:34:42 -0700299 // sRGB framebuffer
Romain Guy6183c972017-03-16 12:24:55 -0700300 R"__SHADER__(
301 vec4 dither(const vec4 color) {
302 vec3 dithered = sqrt(color.rgb) + (triangleNoise(gl_FragCoord.xy * screenSize.xy) / 255.0);
303 return vec4(dithered * dithered, color.a);
304 }
Romain Guy6183c972017-03-16 12:24:55 -0700305 )__SHADER__",
Romain Guy253f2c22016-09-28 17:34:42 -0700306};
307
308// Uses luminance coefficients from Rec.709 to choose the appropriate gamma
309// The gamma() function assumes that bright text will be displayed on a dark
310// background and that dark text will be displayed on bright background
311// The gamma coefficient is chosen to thicken or thin the text accordingly
312// The dot product used to compute the luminance could be approximated with
313// a simple max(color.r, color.g, color.b)
Romain Guy9fe7e162017-02-03 16:16:07 -0800314const char* gFS_Gamma_Preamble = R"__SHADER__(
315 #define GAMMA (%.2f)
316 #define GAMMA_INV (%.2f)
317
318 float gamma(float a, const vec3 color) {
319 float luminance = dot(color, vec3(0.2126, 0.7152, 0.0722));
320 return pow(a, luminance < 0.5 ? GAMMA_INV : GAMMA);
321 }
322)__SHADER__";
Romain Guy253f2c22016-09-28 17:34:42 -0700323
Romain Guyac670c02010-07-27 17:39:27 -0700324const char* gFS_Main =
325 "\nvoid main(void) {\n"
Romain Guy253f2c22016-09-28 17:34:42 -0700326 " vec4 fragColor;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700327
Romain Guy253f2c22016-09-28 17:34:42 -0700328const char* gFS_Main_AddDither =
329 " fragColor = dither(fragColor);\n";
Romain Guy211efea2012-07-31 21:16:07 -0700330
Romain Guy707b2f72010-10-11 16:34:59 -0700331// General case
Romain Guyac670c02010-07-27 17:39:27 -0700332const char* gFS_Main_FetchColor =
333 " fragColor = color;\n";
Romain Guy740bf2b2011-04-26 15:33:10 -0700334const char* gFS_Main_ModulateColor =
335 " fragColor *= color.a;\n";
Chris Craik91a8c7c2014-08-12 14:31:35 -0700336const char* gFS_Main_ApplyVertexAlphaLinearInterp =
Chris Craik6ebdc112012-08-31 18:24:33 -0700337 " fragColor *= alpha;\n";
Chris Craik91a8c7c2014-08-12 14:31:35 -0700338const char* gFS_Main_ApplyVertexAlphaShadowInterp =
Chris Craik138c21f2016-04-28 16:59:42 -0700339 // map alpha through shadow alpha sampler
340 " fragColor *= texture2D(baseSampler, vec2(alpha, 0.5)).a;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700341const char* gFS_Main_FetchTexture[2] = {
342 // Don't modulate
Romain Guycaaaa662017-03-27 00:40:21 -0700343 " fragColor = colorConvert(texture2D(baseSampler, outTexCoords));\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700344 // Modulate
Romain Guycaaaa662017-03-27 00:40:21 -0700345 " fragColor = color * colorConvert(texture2D(baseSampler, outTexCoords));\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700346};
Romain Guy253f2c22016-09-28 17:34:42 -0700347const char* gFS_Main_FetchA8Texture[4] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700348 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700349 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700350 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700351 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700352 " fragColor = color * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700353 " fragColor = color * gamma(texture2D(baseSampler, outTexCoords).a, color.rgb);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700354};
Romain Guy42e1e0d2012-07-30 14:47:51 -0700355const char* gFS_Main_FetchGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700356 // Linear
Romain Guy320d46b2012-08-08 16:05:42 -0700357 " vec4 gradientColor = texture2D(gradientSampler, linear);\n",
Romain Guy211efea2012-07-31 21:16:07 -0700358
Derek Sollenberger669b15a2017-03-31 12:09:24 -0400359 " vec4 gradientColor = mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700360
Romain Guyee916f12010-09-20 17:53:08 -0700361 // Circular
Romain Guy320d46b2012-08-08 16:05:42 -0700362 " vec4 gradientColor = texture2D(gradientSampler, vec2(length(circular), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700363
Derek Sollenberger669b15a2017-03-31 12:09:24 -0400364 " vec4 gradientColor = mix(startColor, endColor, clamp(length(circular), 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700365
Romain Guyee916f12010-09-20 17:53:08 -0700366 // Sweep
Romain Guy63553472012-07-18 20:04:14 -0700367 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700368 " vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700369
Romain Guy42e1e0d2012-07-30 14:47:51 -0700370 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Derek Sollenberger669b15a2017-03-31 12:09:24 -0400371 " vec4 gradientColor = mix(startColor, endColor, clamp(index - floor(index), 0.0, 1.0));\n"
Romain Guyee916f12010-09-20 17:53:08 -0700372};
Romain Guyac670c02010-07-27 17:39:27 -0700373const char* gFS_Main_FetchBitmap =
Romain Guycaaaa662017-03-27 00:40:21 -0700374 " vec4 bitmapColor = colorConvert(texture2D(bitmapSampler, outBitmapTexCoords));\n";
Romain Guy889f8d12010-07-29 14:37:42 -0700375const char* gFS_Main_FetchBitmapNpot =
Romain Guycaaaa662017-03-27 00:40:21 -0700376 " vec4 bitmapColor = colorConvert(texture2D(bitmapSampler, wrap(outBitmapTexCoords)));\n";
Romain Guyac670c02010-07-27 17:39:27 -0700377const char* gFS_Main_BlendShadersBG =
Romain Guyac670c02010-07-27 17:39:27 -0700378 " fragColor = blendShaders(gradientColor, bitmapColor)";
Romain Guy06f96e22010-07-30 19:18:16 -0700379const char* gFS_Main_BlendShadersGB =
380 " fragColor = blendShaders(bitmapColor, gradientColor)";
Romain Guy253f2c22016-09-28 17:34:42 -0700381const char* gFS_Main_BlendShaders_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700382 // Don't modulate
383 ";\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700384 ";\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700385 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700386 " * color.a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700387 " * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700388 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700389 " * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700390 " * gamma(texture2D(baseSampler, outTexCoords).a, color.rgb);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700391};
Romain Guy253f2c22016-09-28 17:34:42 -0700392const char* gFS_Main_GradientShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700393 // Don't modulate
394 " fragColor = gradientColor;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700395 " fragColor = gradientColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700396 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700397 " fragColor = gradientColor * color.a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700398 " fragColor = gradientColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700399 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700400 " fragColor = gradientColor * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700401 " fragColor = gradientColor * gamma(texture2D(baseSampler, outTexCoords).a, gradientColor.rgb);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700402 };
Romain Guy253f2c22016-09-28 17:34:42 -0700403const char* gFS_Main_BitmapShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700404 // Don't modulate
405 " fragColor = bitmapColor;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700406 " fragColor = bitmapColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700407 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700408 " fragColor = bitmapColor * color.a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700409 " fragColor = bitmapColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700410 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700411 " fragColor = bitmapColor * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700412 " fragColor = bitmapColor * gamma(texture2D(baseSampler, outTexCoords).a, bitmapColor.rgb);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700413 };
Romain Guyac670c02010-07-27 17:39:27 -0700414const char* gFS_Main_FragColor =
415 " gl_FragColor = fragColor;\n";
Romain Guyff316ec2013-02-13 18:39:43 -0800416const char* gFS_Main_FragColor_HasColors =
417 " gl_FragColor *= outColors;\n";
Romain Guya5aed0d2010-09-09 14:42:43 -0700418const char* gFS_Main_FragColor_Blend =
419 " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n";
Romain Guyf607bdc2010-09-10 19:20:06 -0700420const char* gFS_Main_FragColor_Blend_Swap =
421 " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n";
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500422const char* gFS_Main_ApplyColorOp[3] = {
Romain Guyac670c02010-07-27 17:39:27 -0700423 // None
424 "",
425 // Matrix
Chris Craik73821c82014-09-16 17:32:13 -0700426 " fragColor.rgb /= (fragColor.a + 0.0019);\n" // un-premultiply
Romain Guyac670c02010-07-27 17:39:27 -0700427 " fragColor *= colorMatrix;\n"
Chris Craik73821c82014-09-16 17:32:13 -0700428 " fragColor += colorMatrixVector;\n"
429 " fragColor.rgb *= (fragColor.a + 0.0019);\n", // re-premultiply
Romain Guyac670c02010-07-27 17:39:27 -0700430 // PorterDuff
431 " fragColor = blendColors(colorBlend, fragColor);\n"
432};
Chris Craikdeeda3d2014-05-05 19:09:33 -0700433
434// Note: LTRB -> xyzw
435const char* gFS_Main_FragColor_HasRoundRectClip =
436 " mediump vec2 fragToLT = roundRectInnerRectLTRB.xy - roundRectPos;\n"
437 " mediump vec2 fragFromRB = roundRectPos - roundRectInnerRectLTRB.zw;\n"
Chris Craikf99f3202014-08-05 15:31:52 -0700438
439 // divide + multiply by 128 to avoid falling out of range in length() function
440 " mediump vec2 dist = max(max(fragToLT, fragFromRB), vec2(0.0, 0.0)) / 128.0;\n"
441 " mediump float linearDist = roundRectRadius - (length(dist) * 128.0);\n"
Chris Craikdeeda3d2014-05-05 19:09:33 -0700442 " gl_FragColor *= clamp(linearDist, 0.0, 1.0);\n";
443
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800444const char* gFS_Main_DebugHighlight =
445 " gl_FragColor.rgb = vec3(0.0, gl_FragColor.a, 0.0);\n";
Romain Guyac670c02010-07-27 17:39:27 -0700446const char* gFS_Footer =
447 "}\n\n";
448
449///////////////////////////////////////////////////////////////////////////////
450// PorterDuff snippets
451///////////////////////////////////////////////////////////////////////////////
452
Romain Guy48daa542010-08-10 19:21:34 -0700453const char* gBlendOps[18] = {
Romain Guyac670c02010-07-27 17:39:27 -0700454 // Clear
455 "return vec4(0.0, 0.0, 0.0, 0.0);\n",
456 // Src
457 "return src;\n",
458 // Dst
459 "return dst;\n",
460 // SrcOver
Romain Guy06f96e22010-07-30 19:18:16 -0700461 "return src + dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700462 // DstOver
Romain Guy06f96e22010-07-30 19:18:16 -0700463 "return dst + src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700464 // SrcIn
Romain Guy06f96e22010-07-30 19:18:16 -0700465 "return src * dst.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700466 // DstIn
Romain Guy06f96e22010-07-30 19:18:16 -0700467 "return dst * src.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700468 // SrcOut
Romain Guy06f96e22010-07-30 19:18:16 -0700469 "return src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700470 // DstOut
Romain Guy06f96e22010-07-30 19:18:16 -0700471 "return dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700472 // SrcAtop
473 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n",
474 // DstAtop
475 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n",
476 // Xor
Romain Guy48daa542010-08-10 19:21:34 -0700477 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, "
Romain Guyac670c02010-07-27 17:39:27 -0700478 "src.a + dst.a - 2.0 * src.a * dst.a);\n",
Derek Sollenbergerc0bf7002015-03-06 13:48:27 -0500479 // Plus
Romain Guy48daa542010-08-10 19:21:34 -0700480 "return min(src + dst, 1.0);\n",
Derek Sollenbergerc0bf7002015-03-06 13:48:27 -0500481 // Modulate
Romain Guy48daa542010-08-10 19:21:34 -0700482 "return src * dst;\n",
483 // Screen
484 "return src + dst - src * dst;\n",
485 // Overlay
486 "return clamp(vec4(mix("
487 "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), "
488 "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), "
489 "step(dst.a, 2.0 * dst.rgb)), "
490 "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n",
491 // Darken
492 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
493 "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
494 // Lighten
495 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
496 "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 -0700497};
498
499///////////////////////////////////////////////////////////////////////////////
500// Constructors/destructors
501///////////////////////////////////////////////////////////////////////////////
502
Chris Craik117bdbc2015-02-05 10:12:38 -0800503ProgramCache::ProgramCache(Extensions& extensions)
Romain Guy253f2c22016-09-28 17:34:42 -0700504 : mHasES3(extensions.getMajorGlVersion() >= 3)
Romain Guyefb4b062017-02-27 11:00:04 -0800505 , mHasLinearBlending(extensions.hasLinearBlending()) {
Romain Guyac670c02010-07-27 17:39:27 -0700506}
507
508ProgramCache::~ProgramCache() {
509 clear();
510}
511
512///////////////////////////////////////////////////////////////////////////////
513// Cache management
514///////////////////////////////////////////////////////////////////////////////
515
516void ProgramCache::clear() {
Romain Guy67f27952010-12-07 20:09:23 -0800517 PROGRAM_LOGD("Clearing program cache");
Romain Guyac670c02010-07-27 17:39:27 -0700518 mCache.clear();
519}
520
521Program* ProgramCache::get(const ProgramDescription& description) {
522 programid key = description.key();
Chris Craik096b8d92013-03-01 11:08:11 -0800523 if (key == (PROGRAM_KEY_TEXTURE | PROGRAM_KEY_A8_TEXTURE)) {
524 // program for A8, unmodulated, texture w/o shader (black text/path textures) is equivalent
525 // to standard texture program (bitmaps, patches). Consider them equivalent.
526 key = PROGRAM_KEY_TEXTURE;
527 }
528
Chris Craik51d6a3d2014-12-22 17:16:56 -0800529 auto iter = mCache.find(key);
Chris Craikd41c4d82015-01-05 15:51:13 -0800530 Program* program = nullptr;
Chris Craik51d6a3d2014-12-22 17:16:56 -0800531 if (iter == mCache.end()) {
Romain Guyee916f12010-09-20 17:53:08 -0700532 description.log("Could not find program");
Romain Guyac670c02010-07-27 17:39:27 -0700533 program = generateProgram(description, key);
Chris Craik51d6a3d2014-12-22 17:16:56 -0800534 mCache[key] = std::unique_ptr<Program>(program);
Romain Guyac670c02010-07-27 17:39:27 -0700535 } else {
Chris Craik51d6a3d2014-12-22 17:16:56 -0800536 program = iter->second.get();
Romain Guyac670c02010-07-27 17:39:27 -0700537 }
538 return program;
539}
540
541///////////////////////////////////////////////////////////////////////////////
542// Program generation
543///////////////////////////////////////////////////////////////////////////////
544
Andreas Gampe64bb4132014-11-22 00:35:09 +0000545Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) {
Romain Guyac670c02010-07-27 17:39:27 -0700546 String8 vertexShader = generateVertexShader(description);
547 String8 fragmentShader = generateFragmentShader(description);
548
Romain Guy42e1e0d2012-07-30 14:47:51 -0700549 return new Program(description, vertexShader.string(), fragmentShader.string());
550}
551
552static inline size_t gradientIndex(const ProgramDescription& description) {
553 return description.gradientType * 2 + description.isSimpleGradient;
Romain Guyac670c02010-07-27 17:39:27 -0700554}
555
556String8 ProgramCache::generateVertexShader(const ProgramDescription& description) {
557 // Add attributes
Chris Craik8bd68c62015-08-19 15:29:05 -0700558 String8 shader(gVS_Header_Start);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700559 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700560 shader.append(gVS_Header_Attributes_TexCoords);
561 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700562 if (description.hasVertexAlpha) {
563 shader.append(gVS_Header_Attributes_VertexAlphaParameters);
Chet Haase5b0200b2011-04-13 17:58:08 -0700564 }
Romain Guyff316ec2013-02-13 18:39:43 -0800565 if (description.hasColors) {
566 shader.append(gVS_Header_Attributes_Colors);
567 }
Romain Guyac670c02010-07-27 17:39:27 -0700568 // Uniforms
569 shader.append(gVS_Header_Uniforms);
Romain Guy8f0095c2011-05-02 17:24:22 -0700570 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700571 shader.append(gVS_Header_Uniforms_TextureTransform);
572 }
Romain Guyac670c02010-07-27 17:39:27 -0700573 if (description.hasGradient) {
Romain Guyb4880042013-04-05 11:17:55 -0700574 shader.append(gVS_Header_Uniforms_HasGradient);
Romain Guyac670c02010-07-27 17:39:27 -0700575 }
Romain Guy889f8d12010-07-29 14:37:42 -0700576 if (description.hasBitmap) {
577 shader.append(gVS_Header_Uniforms_HasBitmap);
578 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700579 if (description.hasRoundRectClip) {
580 shader.append(gVS_Header_Uniforms_HasRoundRectClip);
581 }
Romain Guyac670c02010-07-27 17:39:27 -0700582 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700583 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700584 shader.append(gVS_Header_Varyings_HasTexture);
585 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700586 if (description.hasVertexAlpha) {
587 shader.append(gVS_Header_Varyings_HasVertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700588 }
Romain Guyff316ec2013-02-13 18:39:43 -0800589 if (description.hasColors) {
590 shader.append(gVS_Header_Varyings_HasColors);
591 }
Romain Guyac670c02010-07-27 17:39:27 -0700592 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700593 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700594 }
595 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700596 shader.append(gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700597 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700598 if (description.hasRoundRectClip) {
599 shader.append(gVS_Header_Varyings_HasRoundRectClip);
600 }
Romain Guyac670c02010-07-27 17:39:27 -0700601
602 // Begin the shader
603 shader.append(gVS_Main); {
Romain Guy8f0095c2011-05-02 17:24:22 -0700604 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700605 shader.append(gVS_Main_OutTransformedTexCoords);
Romain Guy8f0095c2011-05-02 17:24:22 -0700606 } else if (description.hasTexture || description.hasExternalTexture) {
607 shader.append(gVS_Main_OutTexCoords);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700608 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700609 if (description.hasVertexAlpha) {
610 shader.append(gVS_Main_VertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700611 }
Romain Guyff316ec2013-02-13 18:39:43 -0800612 if (description.hasColors) {
613 shader.append(gVS_Main_OutColors);
614 }
Romain Guy889f8d12010-07-29 14:37:42 -0700615 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700616 shader.append(gVS_Main_OutBitmapTexCoords);
Romain Guy889f8d12010-07-29 14:37:42 -0700617 }
Romain Guyac670c02010-07-27 17:39:27 -0700618 // Output transformed position
619 shader.append(gVS_Main_Position);
Chet Haasea1d12dd2012-09-21 14:50:14 -0700620 if (description.hasGradient) {
621 shader.append(gVS_Main_OutGradient[gradientIndex(description)]);
622 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700623 if (description.hasRoundRectClip) {
624 shader.append(gVS_Main_HasRoundRectClip);
625 }
Romain Guyac670c02010-07-27 17:39:27 -0700626 }
627 // End the shader
628 shader.append(gVS_Footer);
629
630 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string());
631
632 return shader;
633}
634
Romain Guya938f562012-09-13 20:31:08 -0700635static bool shaderOp(const ProgramDescription& description, String8& shader,
636 const int modulateOp, const char** snippets) {
637 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
Romain Guy253f2c22016-09-28 17:34:42 -0700638 op = op * 2 + description.hasGammaCorrection;
Romain Guya938f562012-09-13 20:31:08 -0700639 shader.append(snippets[op]);
640 return description.hasAlpha8Texture;
641}
642
Romain Guyac670c02010-07-27 17:39:27 -0700643String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) {
Chris Craik8bd68c62015-08-19 15:29:05 -0700644 String8 shader(gFS_Header_Start);
Romain Guya5aed0d2010-09-09 14:42:43 -0700645
Mike Reedc2f31df2016-10-28 17:21:45 -0400646 const bool blendFramebuffer = description.framebufferMode >= SkBlendMode::kPlus;
Romain Guya5aed0d2010-09-09 14:42:43 -0700647 if (blendFramebuffer) {
648 shader.append(gFS_Header_Extension_FramebufferFetch);
649 }
sergeyv9c97e482016-12-12 16:14:11 -0800650 if (description.hasExternalTexture
651 || (description.hasBitmap && description.isShaderBitmapExternal)) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700652 shader.append(gFS_Header_Extension_ExternalTexture);
653 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700654
655 shader.append(gFS_Header);
Romain Guyac670c02010-07-27 17:39:27 -0700656
657 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700658 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700659 shader.append(gVS_Header_Varyings_HasTexture);
660 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700661 if (description.hasVertexAlpha) {
662 shader.append(gVS_Header_Varyings_HasVertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700663 }
Romain Guyff316ec2013-02-13 18:39:43 -0800664 if (description.hasColors) {
665 shader.append(gVS_Header_Varyings_HasColors);
666 }
Romain Guyac670c02010-07-27 17:39:27 -0700667 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700668 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700669 }
670 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700671 shader.append(gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700672 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700673 if (description.hasRoundRectClip) {
674 shader.append(gVS_Header_Varyings_HasRoundRectClip);
675 }
Romain Guyac670c02010-07-27 17:39:27 -0700676
Romain Guyac670c02010-07-27 17:39:27 -0700677 // Uniforms
Romain Guy707b2f72010-10-11 16:34:59 -0700678 int modulateOp = MODULATE_OP_NO_MODULATE;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700679 const bool singleColor = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700680 !description.hasGradient && !description.hasBitmap;
681
682 if (description.modulate || singleColor) {
683 shader.append(gFS_Uniforms_Color);
684 if (!singleColor) modulateOp = MODULATE_OP_MODULATE;
685 }
Chris Craik138c21f2016-04-28 16:59:42 -0700686 if (description.hasTexture || description.useShadowAlphaInterp) {
Romain Guyac670c02010-07-27 17:39:27 -0700687 shader.append(gFS_Uniforms_TextureSampler);
Romain Guy8f0095c2011-05-02 17:24:22 -0700688 } else if (description.hasExternalTexture) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700689 shader.append(gFS_Uniforms_ExternalTextureSampler);
690 }
Romain Guyac670c02010-07-27 17:39:27 -0700691 if (description.hasGradient) {
Romain Guy253f2c22016-09-28 17:34:42 -0700692 shader.append(gFS_Uniforms_GradientSampler[description.isSimpleGradient]);
Romain Guyac670c02010-07-27 17:39:27 -0700693 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700694 if (description.hasRoundRectClip) {
695 shader.append(gFS_Uniforms_HasRoundRectClip);
696 }
Romain Guy707b2f72010-10-11 16:34:59 -0700697
Romain Guy253f2c22016-09-28 17:34:42 -0700698 if (description.hasGammaCorrection) {
699 shader.appendFormat(gFS_Gamma_Preamble, Properties::textGamma, 1.0f / Properties::textGamma);
700 }
701
Romain Guyac670c02010-07-27 17:39:27 -0700702 if (description.hasBitmap) {
sergeyv9c97e482016-12-12 16:14:11 -0800703 if (description.isShaderBitmapExternal) {
704 shader.append(gFS_Uniforms_BitmapExternalSampler);
705 } else {
706 shader.append(gFS_Uniforms_BitmapSampler);
707 }
Romain Guyac670c02010-07-27 17:39:27 -0700708 }
Chris Craikb9ce116d2015-08-20 15:14:06 -0700709 shader.append(gFS_Uniforms_ColorOp[static_cast<int>(description.colorOp)]);
Romain Guyac670c02010-07-27 17:39:27 -0700710
Romain Guycaaaa662017-03-27 00:40:21 -0700711 if (description.hasColorSpaceConversion) {
712 shader.append(gFS_Uniforms_ColorSpaceConversion);
713 }
714 shader.append(gFS_Uniforms_TransferFunction[static_cast<int>(description.transferFunction)]);
715
Romain Guyac670c02010-07-27 17:39:27 -0700716 // Generate required functions
717 if (description.hasGradient && description.hasBitmap) {
Romain Guy48daa542010-08-10 19:21:34 -0700718 generateBlend(shader, "blendShaders", description.shadersMode);
Romain Guyac670c02010-07-27 17:39:27 -0700719 }
Chris Craikb9ce116d2015-08-20 15:14:06 -0700720 if (description.colorOp == ProgramDescription::ColorFilterMode::Blend) {
Romain Guy48daa542010-08-10 19:21:34 -0700721 generateBlend(shader, "blendColors", description.colorMode);
Romain Guyac670c02010-07-27 17:39:27 -0700722 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700723 if (blendFramebuffer) {
724 generateBlend(shader, "blendFramebuffer", description.framebufferMode);
725 }
sergeyv554ffeb2016-11-15 18:01:21 -0800726 if (description.useShaderBasedWrap) {
Romain Guy889f8d12010-07-29 14:37:42 -0700727 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
728 }
Romain Guycaaaa662017-03-27 00:40:21 -0700729 if (description.hasGradient || description.hasLinearTexture
730 || description.hasColorSpaceConversion) {
731 shader.append(gFS_sRGB_TransferFunctions);
Romain Guy636afc12017-02-07 11:21:05 -0800732 }
733 if (description.hasBitmap || ((description.hasTexture || description.hasExternalTexture) &&
734 !description.hasAlpha8Texture)) {
Romain Guycaaaa662017-03-27 00:40:21 -0700735 shader.append(gFS_TransferFunction[static_cast<int>(description.transferFunction)]);
736 shader.append(gFS_OETF[(description.hasLinearTexture || description.hasColorSpaceConversion)
737 && !mHasLinearBlending]);
738 shader.append(gFS_ColorConvert[description.hasColorSpaceConversion
739 ? 1 + description.hasTranslucentConversion : 0]);
Romain Guy636afc12017-02-07 11:21:05 -0800740 }
Romain Guy253f2c22016-09-28 17:34:42 -0700741 if (description.hasGradient) {
Romain Guycaaaa662017-03-27 00:40:21 -0700742 shader.append(gFS_GradientFunctions);
743 shader.append(gFS_GradientPreamble[mHasLinearBlending]);
Romain Guy253f2c22016-09-28 17:34:42 -0700744 }
Romain Guyac670c02010-07-27 17:39:27 -0700745
746 // Begin the shader
747 shader.append(gFS_Main); {
748 // Stores the result in fragColor directly
Romain Guyaa6c24c2011-04-28 18:40:04 -0700749 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700750 if (description.hasAlpha8Texture) {
Romain Guy707b2f72010-10-11 16:34:59 -0700751 if (!description.hasGradient && !description.hasBitmap) {
Romain Guy253f2c22016-09-28 17:34:42 -0700752 shader.append(
753 gFS_Main_FetchA8Texture[modulateOp * 2 + description.hasGammaCorrection]);
Romain Guy707b2f72010-10-11 16:34:59 -0700754 }
Romain Guyac670c02010-07-27 17:39:27 -0700755 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700756 shader.append(gFS_Main_FetchTexture[modulateOp]);
Romain Guyac670c02010-07-27 17:39:27 -0700757 }
758 } else {
Romain Guya938f562012-09-13 20:31:08 -0700759 if (!description.hasGradient && !description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700760 shader.append(gFS_Main_FetchColor);
761 }
Romain Guyac670c02010-07-27 17:39:27 -0700762 }
763 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700764 shader.append(gFS_Main_FetchGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700765 }
766 if (description.hasBitmap) {
sergeyv554ffeb2016-11-15 18:01:21 -0800767 if (!description.useShaderBasedWrap) {
Romain Guy889f8d12010-07-29 14:37:42 -0700768 shader.append(gFS_Main_FetchBitmap);
769 } else {
770 shader.append(gFS_Main_FetchBitmapNpot);
771 }
Romain Guyac670c02010-07-27 17:39:27 -0700772 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700773 bool applyModulate = false;
Romain Guyac670c02010-07-27 17:39:27 -0700774 // Case when we have two shaders set
775 if (description.hasGradient && description.hasBitmap) {
776 if (description.isBitmapFirst) {
777 shader.append(gFS_Main_BlendShadersBG);
778 } else {
779 shader.append(gFS_Main_BlendShadersGB);
780 }
Romain Guya938f562012-09-13 20:31:08 -0700781 applyModulate = shaderOp(description, shader, modulateOp,
782 gFS_Main_BlendShaders_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700783 } else {
784 if (description.hasGradient) {
Romain Guya938f562012-09-13 20:31:08 -0700785 applyModulate = shaderOp(description, shader, modulateOp,
786 gFS_Main_GradientShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700787 } else if (description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700788 applyModulate = shaderOp(description, shader, modulateOp,
789 gFS_Main_BitmapShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700790 }
Romain Guyac670c02010-07-27 17:39:27 -0700791 }
Romain Guya938f562012-09-13 20:31:08 -0700792
Romain Guy740bf2b2011-04-26 15:33:10 -0700793 if (description.modulate && applyModulate) {
Romain Guya938f562012-09-13 20:31:08 -0700794 shader.append(gFS_Main_ModulateColor);
Romain Guy740bf2b2011-04-26 15:33:10 -0700795 }
Romain Guya938f562012-09-13 20:31:08 -0700796
Romain Guyac670c02010-07-27 17:39:27 -0700797 // Apply the color op if needed
Chris Craikb9ce116d2015-08-20 15:14:06 -0700798 shader.append(gFS_Main_ApplyColorOp[static_cast<int>(description.colorOp)]);
Chris Craik9f44a132012-09-13 18:34:55 -0700799
Chris Craik91a8c7c2014-08-12 14:31:35 -0700800 if (description.hasVertexAlpha) {
801 if (description.useShadowAlphaInterp) {
802 shader.append(gFS_Main_ApplyVertexAlphaShadowInterp);
Chris Craikbf759452014-08-11 16:00:44 -0700803 } else {
Chris Craik91a8c7c2014-08-12 14:31:35 -0700804 shader.append(gFS_Main_ApplyVertexAlphaLinearInterp);
Chris Craikbf759452014-08-11 16:00:44 -0700805 }
Chris Craik9f44a132012-09-13 18:34:55 -0700806 }
807
Romain Guy253f2c22016-09-28 17:34:42 -0700808 if (description.hasGradient) {
809 shader.append(gFS_Main_AddDither);
810 }
811
Romain Guyac670c02010-07-27 17:39:27 -0700812 // Output the fragment
Romain Guya5aed0d2010-09-09 14:42:43 -0700813 if (!blendFramebuffer) {
814 shader.append(gFS_Main_FragColor);
815 } else {
Romain Guyf607bdc2010-09-10 19:20:06 -0700816 shader.append(!description.swapSrcDst ?
817 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap);
Romain Guya5aed0d2010-09-09 14:42:43 -0700818 }
Romain Guyff316ec2013-02-13 18:39:43 -0800819 if (description.hasColors) {
820 shader.append(gFS_Main_FragColor_HasColors);
821 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700822 if (description.hasRoundRectClip) {
823 shader.append(gFS_Main_FragColor_HasRoundRectClip);
824 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800825 if (description.hasDebugHighlight) {
826 shader.append(gFS_Main_DebugHighlight);
827 }
Romain Guyac670c02010-07-27 17:39:27 -0700828 }
829 // End the shader
830 shader.append(gFS_Footer);
831
Romain Guy91a8ec02017-02-08 07:45:11 -0800832#if DEBUG_PROGRAMS
Romain Guydb1938e2010-08-02 18:50:22 -0700833 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
834 printLongString(shader);
Romain Guy91a8ec02017-02-08 07:45:11 -0800835#endif
Romain Guydb1938e2010-08-02 18:50:22 -0700836
Romain Guyac670c02010-07-27 17:39:27 -0700837 return shader;
838}
839
Mike Reedc2f31df2016-10-28 17:21:45 -0400840void ProgramCache::generateBlend(String8& shader, const char* name, SkBlendMode mode) {
Romain Guyac670c02010-07-27 17:39:27 -0700841 shader.append("\nvec4 ");
842 shader.append(name);
843 shader.append("(vec4 src, vec4 dst) {\n");
844 shader.append(" ");
Mike Reedc2f31df2016-10-28 17:21:45 -0400845 shader.append(gBlendOps[(int)mode]);
Romain Guyac670c02010-07-27 17:39:27 -0700846 shader.append("}\n");
847}
848
Romain Guy889f8d12010-07-29 14:37:42 -0700849void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) {
Romain Guy63553472012-07-18 20:04:14 -0700850 shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700851 if (wrapS == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700852 shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700853 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n");
854 }
855 if (wrapT == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700856 shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700857 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n");
858 }
859 shader.append(" return vec2(");
860 switch (wrapS) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700861 case GL_CLAMP_TO_EDGE:
862 shader.append("texCoords.x");
863 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700864 case GL_REPEAT:
865 shader.append("mod(texCoords.x, 1.0)");
866 break;
867 case GL_MIRRORED_REPEAT:
868 shader.append("xMod2");
869 break;
870 }
871 shader.append(", ");
872 switch (wrapT) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700873 case GL_CLAMP_TO_EDGE:
874 shader.append("texCoords.y");
875 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700876 case GL_REPEAT:
877 shader.append("mod(texCoords.y, 1.0)");
878 break;
879 case GL_MIRRORED_REPEAT:
880 shader.append("yMod2");
881 break;
882 }
883 shader.append(");\n");
884 shader.append("}\n");
885}
886
Romain Guydb1938e2010-08-02 18:50:22 -0700887void ProgramCache::printLongString(const String8& shader) const {
888 ssize_t index = 0;
889 ssize_t lastIndex = 0;
890 const char* str = shader.string();
891 while ((index = shader.find("\n", index)) > -1) {
892 String8 line(str, index - lastIndex);
893 if (line.length() == 0) line.append("\n");
Chris Craik1c1c3fe2015-03-06 09:40:35 -0800894 ALOGD("%s", line.string());
Romain Guydb1938e2010-08-02 18:50:22 -0700895 index++;
896 str += (index - lastIndex);
897 lastIndex = index;
898 }
899}
900
Romain Guyac670c02010-07-27 17:39:27 -0700901}; // namespace uirenderer
902}; // namespace android