blob: 1164ebfdf1e543dc3e6dbe1a7f030e175279497e [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";
John Reck1bcacfd2017-11-03 10:12:19 -070044const char* gVS_Header_Attributes_TexCoords = "attribute vec2 texCoords;\n";
45const char* gVS_Header_Attributes_Colors = "attribute vec4 colors;\n";
46const char* gVS_Header_Attributes_VertexAlphaParameters = "attribute float vtxAlpha;\n";
47const char* gVS_Header_Uniforms_TextureTransform = "uniform mat4 mainTextureTransform;\n";
Romain Guyac670c02010-07-27 17:39:27 -070048const char* gVS_Header_Uniforms =
John Reck1bcacfd2017-11-03 10:12:19 -070049 "uniform mat4 projection;\n"
Romain Guyac670c02010-07-27 17:39:27 -070050 "uniform mat4 transform;\n";
John Reck1bcacfd2017-11-03 10:12:19 -070051const char* gVS_Header_Uniforms_HasGradient = "uniform mat4 screenSpace;\n";
Romain Guy889f8d12010-07-29 14:37:42 -070052const char* gVS_Header_Uniforms_HasBitmap =
53 "uniform mat4 textureTransform;\n"
Romain Guy80bbfb12011-03-23 16:56:28 -070054 "uniform mediump vec2 textureDimension;\n";
Chris Craikdeeda3d2014-05-05 19:09:33 -070055const char* gVS_Header_Uniforms_HasRoundRectClip =
Arun06e9f322017-01-23 11:59:21 +000056 "uniform mat4 roundRectInvTransform;\n"
57 "uniform mediump vec4 roundRectInnerRectLTWH;\n"
58 "uniform mediump float roundRectRadius;\n";
John Reck1bcacfd2017-11-03 10:12:19 -070059const char* gVS_Header_Varyings_HasTexture = "varying vec2 outTexCoords;\n";
60const char* gVS_Header_Varyings_HasColors = "varying vec4 outColors;\n";
61const char* gVS_Header_Varyings_HasVertexAlpha = "varying float alpha;\n";
62const char* gVS_Header_Varyings_HasBitmap = "varying highp vec2 outBitmapTexCoords;\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -070063const char* gVS_Header_Varyings_HasGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -070064 // Linear
John Reck1bcacfd2017-11-03 10:12:19 -070065 "varying highp vec2 linear;\n", "varying float linear;\n",
Romain Guy211efea2012-07-31 21:16:07 -070066
Romain Guyee916f12010-09-20 17:53:08 -070067 // Circular
John Reck1bcacfd2017-11-03 10:12:19 -070068 "varying highp vec2 circular;\n", "varying highp vec2 circular;\n",
Romain Guy211efea2012-07-31 21:16:07 -070069
Romain Guyee916f12010-09-20 17:53:08 -070070 // Sweep
John Reck1bcacfd2017-11-03 10:12:19 -070071 "varying highp vec2 sweep;\n", "varying highp vec2 sweep;\n",
Romain Guyee916f12010-09-20 17:53:08 -070072};
John Reck1bcacfd2017-11-03 10:12:19 -070073const char* gVS_Header_Varyings_HasRoundRectClip = "varying mediump vec2 roundRectPos;\n";
74const char* gVS_Main = "\nvoid main(void) {\n";
75const char* gVS_Main_OutTexCoords = " outTexCoords = texCoords;\n";
76const char* gVS_Main_OutColors = " outColors = colors;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -070077const char* gVS_Main_OutTransformedTexCoords =
78 " outTexCoords = (mainTextureTransform * vec4(texCoords, 0.0, 1.0)).xy;\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -070079const char* gVS_Main_OutGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -070080 // Linear
Romain Guy253f2c22016-09-28 17:34:42 -070081 " linear = vec2((screenSpace * position).x, 0.5);\n",
82 " linear = (screenSpace * position).x;\n",
Romain Guy211efea2012-07-31 21:16:07 -070083
Romain Guyee916f12010-09-20 17:53:08 -070084 // Circular
Romain Guy253f2c22016-09-28 17:34:42 -070085 " circular = (screenSpace * position).xy;\n",
86 " circular = (screenSpace * position).xy;\n",
Romain Guy211efea2012-07-31 21:16:07 -070087
Romain Guyee916f12010-09-20 17:53:08 -070088 // Sweep
John Reck1bcacfd2017-11-03 10:12:19 -070089 " sweep = (screenSpace * position).xy;\n", " sweep = (screenSpace * position).xy;\n"};
Romain Guy889f8d12010-07-29 14:37:42 -070090const char* gVS_Main_OutBitmapTexCoords =
Romain Guy707b2f72010-10-11 16:34:59 -070091 " outBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
Romain Guyac670c02010-07-27 17:39:27 -070092const char* gVS_Main_Position =
Chris Craikdeeda3d2014-05-05 19:09:33 -070093 " vec4 transformedPosition = projection * transform * position;\n"
94 " gl_Position = transformedPosition;\n";
Chris Craikbf759452014-08-11 16:00:44 -070095
John Reck1bcacfd2017-11-03 10:12:19 -070096const char* gVS_Main_VertexAlpha = " alpha = vtxAlpha;\n";
Chris Craikbf759452014-08-11 16:00:44 -070097
Chris Craikdeeda3d2014-05-05 19:09:33 -070098const char* gVS_Main_HasRoundRectClip =
John Reck1bcacfd2017-11-03 10:12:19 -070099 " roundRectPos = ((roundRectInvTransform * transformedPosition).xy / roundRectRadius) - "
100 "roundRectInnerRectLTWH.xy;\n";
101const char* gVS_Footer = "}\n\n";
Romain Guyac670c02010-07-27 17:39:27 -0700102
103///////////////////////////////////////////////////////////////////////////////
104// Fragment shaders snippets
105///////////////////////////////////////////////////////////////////////////////
106
John Reck1bcacfd2017-11-03 10:12:19 -0700107const char* gFS_Header_Start = "#version 100\n";
Romain Guya5aed0d2010-09-09 14:42:43 -0700108const char* gFS_Header_Extension_FramebufferFetch =
109 "#extension GL_NV_shader_framebuffer_fetch : enable\n\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700110const char* gFS_Header_Extension_ExternalTexture =
111 "#extension GL_OES_EGL_image_external : require\n\n";
John Reck1bcacfd2017-11-03 10:12:19 -0700112const char* gFS_Header = "precision mediump float;\n\n";
113const char* gFS_Uniforms_Color = "uniform vec4 color;\n";
114const char* gFS_Uniforms_TextureSampler = "uniform sampler2D baseSampler;\n";
115const char* gFS_Uniforms_ExternalTextureSampler = "uniform samplerExternalOES baseSampler;\n";
Romain Guyb4880042013-04-05 11:17:55 -0700116const char* gFS_Uniforms_GradientSampler[2] = {
Romain Guy253f2c22016-09-28 17:34:42 -0700117 "uniform vec2 screenSize;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700118 "uniform sampler2D gradientSampler;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700119
120 "uniform vec2 screenSize;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700121 "uniform vec4 startColor;\n"
John Reck1bcacfd2017-11-03 10:12:19 -0700122 "uniform vec4 endColor;\n"};
123const char* gFS_Uniforms_BitmapSampler = "uniform sampler2D bitmapSampler;\n";
124const char* gFS_Uniforms_BitmapExternalSampler = "uniform samplerExternalOES bitmapSampler;\n";
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500125const char* gFS_Uniforms_ColorOp[3] = {
Romain Guyac670c02010-07-27 17:39:27 -0700126 // None
127 "",
128 // Matrix
129 "uniform mat4 colorMatrix;\n"
130 "uniform vec4 colorMatrixVector;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700131 // PorterDuff
John Reck1bcacfd2017-11-03 10:12:19 -0700132 "uniform vec4 colorBlend;\n"};
Romain Guy41210632012-07-16 17:04:24 -0700133
Chris Craikdeeda3d2014-05-05 19:09:33 -0700134const char* gFS_Uniforms_HasRoundRectClip =
Arun06e9f322017-01-23 11:59:21 +0000135 "uniform mediump vec4 roundRectInnerRectLTWH;\n"
136 "uniform mediump float roundRectRadius;\n";
Chris Craikdeeda3d2014-05-05 19:09:33 -0700137
Romain Guycaaaa662017-03-27 00:40:21 -0700138const char* gFS_Uniforms_ColorSpaceConversion =
139 // TODO: Should we use a 3D LUT to combine the matrix and transfer functions?
140 // 32x32x32 fp16 LUTs (for scRGB output) are large and heavy to generate...
141 "uniform mat3 colorSpaceMatrix;\n";
142
143const char* gFS_Uniforms_TransferFunction[4] = {
144 // In this order: g, a, b, c, d, e, f
145 // See ColorSpace::TransferParameters
146 // We'll use hardware sRGB conversion as much as possible
John Reck1bcacfd2017-11-03 10:12:19 -0700147 "", "uniform float transferFunction[7];\n", "uniform float transferFunction[5];\n",
148 "uniform float transferFunctionGamma;\n"};
Romain Guy636afc12017-02-07 11:21:05 -0800149
Romain Guycaaaa662017-03-27 00:40:21 -0700150const char* gFS_OETF[2] = {
151 R"__SHADER__(
152 vec4 OETF(const vec4 linear) {
153 return linear;
154 }
155 )__SHADER__",
156 // We expect linear data to be scRGB so we mirror the gamma function
157 R"__SHADER__(
158 vec4 OETF(const vec4 linear) {
159 return vec4(sign(linear.rgb) * OETF_sRGB(abs(linear.rgb)), linear.a);
160 }
John Reck1bcacfd2017-11-03 10:12:19 -0700161 )__SHADER__"};
Romain Guycaaaa662017-03-27 00:40:21 -0700162
163const char* gFS_ColorConvert[3] = {
164 // Just OETF
165 R"__SHADER__(
166 vec4 colorConvert(const vec4 color) {
167 return OETF(color);
168 }
169 )__SHADER__",
170 // Full color conversion for opaque bitmaps
171 R"__SHADER__(
172 vec4 colorConvert(const vec4 color) {
173 return OETF(vec4(colorSpaceMatrix * EOTF_Parametric(color.rgb), color.a));
174 }
175 )__SHADER__",
176 // Full color conversion for translucent bitmaps
177 // Note: 0.5/256=0.0019
178 R"__SHADER__(
179 vec4 colorConvert(in vec4 color) {
180 color.rgb /= color.a + 0.0019;
181 color = OETF(vec4(colorSpaceMatrix * EOTF_Parametric(color.rgb), color.a));
182 color.rgb *= color.a + 0.0019;
183 return color;
184 }
185 )__SHADER__",
186};
187
188const char* gFS_sRGB_TransferFunctions = R"__SHADER__(
Romain Guy636afc12017-02-07 11:21:05 -0800189 float OETF_sRGB(const float linear) {
190 // IEC 61966-2-1:1999
191 return linear <= 0.0031308 ? linear * 12.92 : (pow(linear, 1.0 / 2.4) * 1.055) - 0.055;
192 }
193
194 vec3 OETF_sRGB(const vec3 linear) {
195 return vec3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b));
196 }
197
198 float EOTF_sRGB(float srgb) {
199 // IEC 61966-2-1:1999
200 return srgb <= 0.04045 ? srgb / 12.92 : pow((srgb + 0.055) / 1.055, 2.4);
201 }
202)__SHADER__";
203
Romain Guycaaaa662017-03-27 00:40:21 -0700204const char* gFS_TransferFunction[4] = {
205 // Conversion done by the texture unit (sRGB)
206 R"__SHADER__(
207 vec3 EOTF_Parametric(const vec3 x) {
208 return x;
209 }
210 )__SHADER__",
211 // Full transfer function
212 // TODO: We should probably use a 1D LUT (256x1 with texelFetch() since input is 8 bit)
213 // TODO: That would cause 3 dependent texture fetches. Is it worth it?
214 R"__SHADER__(
215 float EOTF_Parametric(float x) {
216 return x <= transferFunction[4]
217 ? transferFunction[3] * x + transferFunction[6]
218 : pow(transferFunction[1] * x + transferFunction[2], transferFunction[0])
219 + transferFunction[5];
220 }
221
222 vec3 EOTF_Parametric(const vec3 x) {
223 return vec3(EOTF_Parametric(x.r), EOTF_Parametric(x.g), EOTF_Parametric(x.b));
224 }
225 )__SHADER__",
226 // Limited transfer function, e = f = 0.0
227 R"__SHADER__(
228 float EOTF_Parametric(float x) {
229 return x <= transferFunction[4]
230 ? transferFunction[3] * x
231 : pow(transferFunction[1] * x + transferFunction[2], transferFunction[0]);
232 }
233
234 vec3 EOTF_Parametric(const vec3 x) {
235 return vec3(EOTF_Parametric(x.r), EOTF_Parametric(x.g), EOTF_Parametric(x.b));
236 }
237 )__SHADER__",
238 // Gamma transfer function, e = f = 0.0
239 R"__SHADER__(
240 vec3 EOTF_Parametric(const vec3 x) {
241 return vec3(pow(x.r, transferFunctionGamma),
242 pow(x.g, transferFunctionGamma),
243 pow(x.b, transferFunctionGamma));
244 }
John Reck1bcacfd2017-11-03 10:12:19 -0700245 )__SHADER__"};
Romain Guycaaaa662017-03-27 00:40:21 -0700246
Romain Guy253f2c22016-09-28 17:34:42 -0700247// Dithering must be done in the quantization space
248// When we are writing to an sRGB framebuffer, we must do the following:
Romain Guy636afc12017-02-07 11:21:05 -0800249// EOTF(OETF(color) + dither)
Romain Guy0d86d7e2017-03-15 20:38:11 -0700250// The dithering pattern is generated with a triangle noise generator in the range [-1.0,1.0]
Romain Guy253f2c22016-09-28 17:34:42 -0700251// TODO: Handle linear fp16 render targets
Romain Guycaaaa662017-03-27 00:40:21 -0700252const char* gFS_GradientFunctions = R"__SHADER__(
Romain Guy9fe7e162017-02-03 16:16:07 -0800253 float triangleNoise(const highp vec2 n) {
254 highp vec2 p = fract(n * vec2(5.3987, 5.4421));
255 p += dot(p.yx, p.xy + vec2(21.5351, 14.3137));
256 highp float xy = p.x * p.y;
257 return fract(xy * 95.4307) + fract(xy * 75.04961) - 1.0;
258 }
Romain Guy9fe7e162017-02-03 16:16:07 -0800259)__SHADER__";
Romain Guycaaaa662017-03-27 00:40:21 -0700260
261const char* gFS_GradientPreamble[2] = {
Romain Guy253f2c22016-09-28 17:34:42 -0700262 // Linear framebuffer
Romain Guy6183c972017-03-16 12:24:55 -0700263 R"__SHADER__(
264 vec4 dither(const vec4 color) {
265 return color + (triangleNoise(gl_FragCoord.xy * screenSize.xy) / 255.0);
266 }
Romain Guy6183c972017-03-16 12:24:55 -0700267 )__SHADER__",
Romain Guy253f2c22016-09-28 17:34:42 -0700268 // sRGB framebuffer
Romain Guy6183c972017-03-16 12:24:55 -0700269 R"__SHADER__(
270 vec4 dither(const vec4 color) {
271 vec3 dithered = sqrt(color.rgb) + (triangleNoise(gl_FragCoord.xy * screenSize.xy) / 255.0);
272 return vec4(dithered * dithered, color.a);
273 }
Romain Guy6183c972017-03-16 12:24:55 -0700274 )__SHADER__",
Romain Guy253f2c22016-09-28 17:34:42 -0700275};
276
277// Uses luminance coefficients from Rec.709 to choose the appropriate gamma
278// The gamma() function assumes that bright text will be displayed on a dark
279// background and that dark text will be displayed on bright background
280// The gamma coefficient is chosen to thicken or thin the text accordingly
281// The dot product used to compute the luminance could be approximated with
282// a simple max(color.r, color.g, color.b)
Romain Guy9fe7e162017-02-03 16:16:07 -0800283const char* gFS_Gamma_Preamble = R"__SHADER__(
284 #define GAMMA (%.2f)
285 #define GAMMA_INV (%.2f)
286
287 float gamma(float a, const vec3 color) {
288 float luminance = dot(color, vec3(0.2126, 0.7152, 0.0722));
289 return pow(a, luminance < 0.5 ? GAMMA_INV : GAMMA);
290 }
291)__SHADER__";
Romain Guy253f2c22016-09-28 17:34:42 -0700292
Romain Guyac670c02010-07-27 17:39:27 -0700293const char* gFS_Main =
294 "\nvoid main(void) {\n"
Romain Guy253f2c22016-09-28 17:34:42 -0700295 " vec4 fragColor;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700296
John Reck1bcacfd2017-11-03 10:12:19 -0700297const char* gFS_Main_AddDither = " fragColor = dither(fragColor);\n";
Romain Guy211efea2012-07-31 21:16:07 -0700298
Romain Guy707b2f72010-10-11 16:34:59 -0700299// General case
John Reck1bcacfd2017-11-03 10:12:19 -0700300const char* gFS_Main_FetchColor = " fragColor = color;\n";
301const char* gFS_Main_ModulateColor = " fragColor *= color.a;\n";
302const char* gFS_Main_ApplyVertexAlphaLinearInterp = " fragColor *= alpha;\n";
Chris Craik91a8c7c2014-08-12 14:31:35 -0700303const char* gFS_Main_ApplyVertexAlphaShadowInterp =
Chris Craik138c21f2016-04-28 16:59:42 -0700304 // map alpha through shadow alpha sampler
305 " fragColor *= texture2D(baseSampler, vec2(alpha, 0.5)).a;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700306const char* gFS_Main_FetchTexture[2] = {
307 // Don't modulate
Romain Guycaaaa662017-03-27 00:40:21 -0700308 " fragColor = colorConvert(texture2D(baseSampler, outTexCoords));\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700309 // Modulate
John Reck1bcacfd2017-11-03 10:12:19 -0700310 " fragColor = color * colorConvert(texture2D(baseSampler, outTexCoords));\n"};
Romain Guy253f2c22016-09-28 17:34:42 -0700311const char* gFS_Main_FetchA8Texture[4] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700312 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700313 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700314 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700315 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700316 " fragColor = color * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700317 " fragColor = color * gamma(texture2D(baseSampler, outTexCoords).a, color.rgb);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700318};
Romain Guy42e1e0d2012-07-30 14:47:51 -0700319const char* gFS_Main_FetchGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700320 // Linear
Romain Guy320d46b2012-08-08 16:05:42 -0700321 " vec4 gradientColor = texture2D(gradientSampler, linear);\n",
Romain Guy211efea2012-07-31 21:16:07 -0700322
Derek Sollenberger669b15a2017-03-31 12:09:24 -0400323 " vec4 gradientColor = mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700324
Romain Guyee916f12010-09-20 17:53:08 -0700325 // Circular
Romain Guy320d46b2012-08-08 16:05:42 -0700326 " vec4 gradientColor = texture2D(gradientSampler, vec2(length(circular), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700327
Derek Sollenberger669b15a2017-03-31 12:09:24 -0400328 " vec4 gradientColor = mix(startColor, endColor, clamp(length(circular), 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700329
Romain Guyee916f12010-09-20 17:53:08 -0700330 // Sweep
Romain Guy63553472012-07-18 20:04:14 -0700331 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700332 " vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700333
Romain Guy42e1e0d2012-07-30 14:47:51 -0700334 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
John Reck1bcacfd2017-11-03 10:12:19 -0700335 " vec4 gradientColor = mix(startColor, endColor, clamp(index - floor(index), 0.0, "
336 "1.0));\n"};
Romain Guyac670c02010-07-27 17:39:27 -0700337const char* gFS_Main_FetchBitmap =
Romain Guycaaaa662017-03-27 00:40:21 -0700338 " vec4 bitmapColor = colorConvert(texture2D(bitmapSampler, outBitmapTexCoords));\n";
Romain Guy889f8d12010-07-29 14:37:42 -0700339const char* gFS_Main_FetchBitmapNpot =
John Reck1bcacfd2017-11-03 10:12:19 -0700340 " vec4 bitmapColor = colorConvert(texture2D(bitmapSampler, "
341 "wrap(outBitmapTexCoords)));\n";
342const char* gFS_Main_BlendShadersBG = " fragColor = blendShaders(gradientColor, bitmapColor)";
343const char* gFS_Main_BlendShadersGB = " fragColor = blendShaders(bitmapColor, gradientColor)";
Romain Guy253f2c22016-09-28 17:34:42 -0700344const char* gFS_Main_BlendShaders_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700345 // Don't modulate
John Reck1bcacfd2017-11-03 10:12:19 -0700346 ";\n", ";\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700347 // Modulate
John Reck1bcacfd2017-11-03 10:12:19 -0700348 " * color.a;\n", " * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700349 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700350 " * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700351 " * gamma(texture2D(baseSampler, outTexCoords).a, color.rgb);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700352};
Romain Guy253f2c22016-09-28 17:34:42 -0700353const char* gFS_Main_GradientShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700354 // Don't modulate
John Reck1bcacfd2017-11-03 10:12:19 -0700355 " fragColor = gradientColor;\n", " fragColor = gradientColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700356 // Modulate
John Reck1bcacfd2017-11-03 10:12:19 -0700357 " fragColor = gradientColor * color.a;\n", " fragColor = gradientColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700358 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700359 " fragColor = gradientColor * texture2D(baseSampler, outTexCoords).a;\n",
John Reck1bcacfd2017-11-03 10:12:19 -0700360 " fragColor = gradientColor * gamma(texture2D(baseSampler, outTexCoords).a, "
361 "gradientColor.rgb);\n",
362};
Romain Guy253f2c22016-09-28 17:34:42 -0700363const char* gFS_Main_BitmapShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700364 // Don't modulate
John Reck1bcacfd2017-11-03 10:12:19 -0700365 " fragColor = bitmapColor;\n", " fragColor = bitmapColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700366 // Modulate
John Reck1bcacfd2017-11-03 10:12:19 -0700367 " fragColor = bitmapColor * color.a;\n", " fragColor = bitmapColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700368 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700369 " fragColor = bitmapColor * texture2D(baseSampler, outTexCoords).a;\n",
John Reck1bcacfd2017-11-03 10:12:19 -0700370 " fragColor = bitmapColor * gamma(texture2D(baseSampler, outTexCoords).a, "
371 "bitmapColor.rgb);\n",
372};
373const char* gFS_Main_FragColor = " gl_FragColor = fragColor;\n";
374const char* gFS_Main_FragColor_HasColors = " gl_FragColor *= outColors;\n";
Romain Guya5aed0d2010-09-09 14:42:43 -0700375const char* gFS_Main_FragColor_Blend =
376 " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n";
Romain Guyf607bdc2010-09-10 19:20:06 -0700377const char* gFS_Main_FragColor_Blend_Swap =
378 " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n";
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500379const char* gFS_Main_ApplyColorOp[3] = {
Romain Guyac670c02010-07-27 17:39:27 -0700380 // None
381 "",
382 // Matrix
John Reck1bcacfd2017-11-03 10:12:19 -0700383 " fragColor.rgb /= (fragColor.a + 0.0019);\n" // un-premultiply
Romain Guyac670c02010-07-27 17:39:27 -0700384 " fragColor *= colorMatrix;\n"
Chris Craik73821c82014-09-16 17:32:13 -0700385 " fragColor += colorMatrixVector;\n"
John Reck1bcacfd2017-11-03 10:12:19 -0700386 " fragColor.rgb *= (fragColor.a + 0.0019);\n", // re-premultiply
Romain Guyac670c02010-07-27 17:39:27 -0700387 // PorterDuff
John Reck1bcacfd2017-11-03 10:12:19 -0700388 " fragColor = blendColors(colorBlend, fragColor);\n"};
Chris Craikdeeda3d2014-05-05 19:09:33 -0700389
Arun06e9f322017-01-23 11:59:21 +0000390// Note: LTWH (left top width height) -> xyzw
391// roundRectPos is now divided by roundRectRadius in vertex shader
392// after we also subtract roundRectInnerRectLTWH.xy from roundRectPos
Chris Craikdeeda3d2014-05-05 19:09:33 -0700393const char* gFS_Main_FragColor_HasRoundRectClip =
Arun06e9f322017-01-23 11:59:21 +0000394 " mediump vec2 fragToLT = -roundRectPos;\n"
395 " mediump vec2 fragFromRB = roundRectPos - roundRectInnerRectLTWH.zw;\n"
Chris Craikf99f3202014-08-05 15:31:52 -0700396
Arun06e9f322017-01-23 11:59:21 +0000397 // since distance is divided by radius, it's in [0;1] so precision is not an issue
398 // this also lets us clamp(0.0, 1.0) instead of max() which is cheaper on GPUs
399 " mediump vec2 dist = clamp(max(fragToLT, fragFromRB), 0.0, 1.0);\n"
John Reck1bcacfd2017-11-03 10:12:19 -0700400 " mediump float linearDist = clamp(roundRectRadius - (length(dist) * roundRectRadius), "
401 "0.0, 1.0);\n"
Arun06e9f322017-01-23 11:59:21 +0000402 " gl_FragColor *= linearDist;\n";
Chris Craikdeeda3d2014-05-05 19:09:33 -0700403
John Reck1bcacfd2017-11-03 10:12:19 -0700404const char* gFS_Main_DebugHighlight = " gl_FragColor.rgb = vec3(0.0, gl_FragColor.a, 0.0);\n";
405const char* gFS_Footer = "}\n\n";
Romain Guyac670c02010-07-27 17:39:27 -0700406
407///////////////////////////////////////////////////////////////////////////////
408// PorterDuff snippets
409///////////////////////////////////////////////////////////////////////////////
410
Romain Guy48daa542010-08-10 19:21:34 -0700411const char* gBlendOps[18] = {
Romain Guyac670c02010-07-27 17:39:27 -0700412 // Clear
413 "return vec4(0.0, 0.0, 0.0, 0.0);\n",
414 // Src
415 "return src;\n",
416 // Dst
417 "return dst;\n",
418 // SrcOver
Romain Guy06f96e22010-07-30 19:18:16 -0700419 "return src + dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700420 // DstOver
Romain Guy06f96e22010-07-30 19:18:16 -0700421 "return dst + src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700422 // SrcIn
Romain Guy06f96e22010-07-30 19:18:16 -0700423 "return src * dst.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700424 // DstIn
Romain Guy06f96e22010-07-30 19:18:16 -0700425 "return dst * src.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700426 // SrcOut
Romain Guy06f96e22010-07-30 19:18:16 -0700427 "return src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700428 // DstOut
Romain Guy06f96e22010-07-30 19:18:16 -0700429 "return dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700430 // SrcAtop
431 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n",
432 // DstAtop
433 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n",
434 // Xor
Romain Guy48daa542010-08-10 19:21:34 -0700435 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, "
John Reck1bcacfd2017-11-03 10:12:19 -0700436 "src.a + dst.a - 2.0 * src.a * dst.a);\n",
Derek Sollenbergerc0bf7002015-03-06 13:48:27 -0500437 // Plus
Romain Guy48daa542010-08-10 19:21:34 -0700438 "return min(src + dst, 1.0);\n",
Derek Sollenbergerc0bf7002015-03-06 13:48:27 -0500439 // Modulate
Romain Guy48daa542010-08-10 19:21:34 -0700440 "return src * dst;\n",
441 // Screen
442 "return src + dst - src * dst;\n",
443 // Overlay
444 "return clamp(vec4(mix("
John Reck1bcacfd2017-11-03 10:12:19 -0700445 "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), "
446 "src.a * dst.a - 2.0 * (dst.a - dst.rgb) * (src.a - src.rgb) + src.rgb * (1.0 - dst.a) + "
447 "dst.rgb * (1.0 - src.a), "
448 "step(dst.a, 2.0 * dst.rgb)), "
449 "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n",
Romain Guy48daa542010-08-10 19:21:34 -0700450 // Darken
451 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
John Reck1bcacfd2017-11-03 10:12:19 -0700452 "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
Romain Guy48daa542010-08-10 19:21:34 -0700453 // Lighten
454 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
John Reck1bcacfd2017-11-03 10:12:19 -0700455 "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 -0700456};
457
458///////////////////////////////////////////////////////////////////////////////
459// Constructors/destructors
460///////////////////////////////////////////////////////////////////////////////
461
John Reck8dc02f92017-07-17 09:55:02 -0700462ProgramCache::ProgramCache(const Extensions& extensions)
Romain Guy253f2c22016-09-28 17:34:42 -0700463 : mHasES3(extensions.getMajorGlVersion() >= 3)
John Reck1bcacfd2017-11-03 10:12:19 -0700464 , mHasLinearBlending(extensions.hasLinearBlending()) {}
Romain Guyac670c02010-07-27 17:39:27 -0700465
466ProgramCache::~ProgramCache() {
467 clear();
468}
469
470///////////////////////////////////////////////////////////////////////////////
471// Cache management
472///////////////////////////////////////////////////////////////////////////////
473
474void ProgramCache::clear() {
Romain Guy67f27952010-12-07 20:09:23 -0800475 PROGRAM_LOGD("Clearing program cache");
Romain Guyac670c02010-07-27 17:39:27 -0700476 mCache.clear();
477}
478
479Program* ProgramCache::get(const ProgramDescription& description) {
480 programid key = description.key();
Chris Craik096b8d92013-03-01 11:08:11 -0800481 if (key == (PROGRAM_KEY_TEXTURE | PROGRAM_KEY_A8_TEXTURE)) {
482 // program for A8, unmodulated, texture w/o shader (black text/path textures) is equivalent
483 // to standard texture program (bitmaps, patches). Consider them equivalent.
484 key = PROGRAM_KEY_TEXTURE;
485 }
486
Chris Craik51d6a3d2014-12-22 17:16:56 -0800487 auto iter = mCache.find(key);
Chris Craikd41c4d82015-01-05 15:51:13 -0800488 Program* program = nullptr;
Chris Craik51d6a3d2014-12-22 17:16:56 -0800489 if (iter == mCache.end()) {
Romain Guyee916f12010-09-20 17:53:08 -0700490 description.log("Could not find program");
Romain Guyac670c02010-07-27 17:39:27 -0700491 program = generateProgram(description, key);
Chris Craik51d6a3d2014-12-22 17:16:56 -0800492 mCache[key] = std::unique_ptr<Program>(program);
Romain Guyac670c02010-07-27 17:39:27 -0700493 } else {
Chris Craik51d6a3d2014-12-22 17:16:56 -0800494 program = iter->second.get();
Romain Guyac670c02010-07-27 17:39:27 -0700495 }
496 return program;
497}
498
499///////////////////////////////////////////////////////////////////////////////
500// Program generation
501///////////////////////////////////////////////////////////////////////////////
502
Andreas Gampe64bb4132014-11-22 00:35:09 +0000503Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) {
Romain Guyac670c02010-07-27 17:39:27 -0700504 String8 vertexShader = generateVertexShader(description);
505 String8 fragmentShader = generateFragmentShader(description);
506
Romain Guy42e1e0d2012-07-30 14:47:51 -0700507 return new Program(description, vertexShader.string(), fragmentShader.string());
508}
509
510static inline size_t gradientIndex(const ProgramDescription& description) {
511 return description.gradientType * 2 + description.isSimpleGradient;
Romain Guyac670c02010-07-27 17:39:27 -0700512}
513
514String8 ProgramCache::generateVertexShader(const ProgramDescription& description) {
515 // Add attributes
Chris Craik8bd68c62015-08-19 15:29:05 -0700516 String8 shader(gVS_Header_Start);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700517 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700518 shader.append(gVS_Header_Attributes_TexCoords);
519 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700520 if (description.hasVertexAlpha) {
521 shader.append(gVS_Header_Attributes_VertexAlphaParameters);
Chet Haase5b0200b2011-04-13 17:58:08 -0700522 }
Romain Guyff316ec2013-02-13 18:39:43 -0800523 if (description.hasColors) {
524 shader.append(gVS_Header_Attributes_Colors);
525 }
Romain Guyac670c02010-07-27 17:39:27 -0700526 // Uniforms
527 shader.append(gVS_Header_Uniforms);
Romain Guy8f0095c2011-05-02 17:24:22 -0700528 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700529 shader.append(gVS_Header_Uniforms_TextureTransform);
530 }
Romain Guyac670c02010-07-27 17:39:27 -0700531 if (description.hasGradient) {
Romain Guyb4880042013-04-05 11:17:55 -0700532 shader.append(gVS_Header_Uniforms_HasGradient);
Romain Guyac670c02010-07-27 17:39:27 -0700533 }
Romain Guy889f8d12010-07-29 14:37:42 -0700534 if (description.hasBitmap) {
535 shader.append(gVS_Header_Uniforms_HasBitmap);
536 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700537 if (description.hasRoundRectClip) {
538 shader.append(gVS_Header_Uniforms_HasRoundRectClip);
539 }
Romain Guyac670c02010-07-27 17:39:27 -0700540 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700541 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700542 shader.append(gVS_Header_Varyings_HasTexture);
543 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700544 if (description.hasVertexAlpha) {
545 shader.append(gVS_Header_Varyings_HasVertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700546 }
Romain Guyff316ec2013-02-13 18:39:43 -0800547 if (description.hasColors) {
548 shader.append(gVS_Header_Varyings_HasColors);
549 }
Romain Guyac670c02010-07-27 17:39:27 -0700550 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700551 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700552 }
553 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700554 shader.append(gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700555 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700556 if (description.hasRoundRectClip) {
557 shader.append(gVS_Header_Varyings_HasRoundRectClip);
558 }
Romain Guyac670c02010-07-27 17:39:27 -0700559
560 // Begin the shader
John Reck1bcacfd2017-11-03 10:12:19 -0700561 shader.append(gVS_Main);
562 {
Romain Guy8f0095c2011-05-02 17:24:22 -0700563 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700564 shader.append(gVS_Main_OutTransformedTexCoords);
Romain Guy8f0095c2011-05-02 17:24:22 -0700565 } else if (description.hasTexture || description.hasExternalTexture) {
566 shader.append(gVS_Main_OutTexCoords);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700567 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700568 if (description.hasVertexAlpha) {
569 shader.append(gVS_Main_VertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700570 }
Romain Guyff316ec2013-02-13 18:39:43 -0800571 if (description.hasColors) {
572 shader.append(gVS_Main_OutColors);
573 }
Romain Guy889f8d12010-07-29 14:37:42 -0700574 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700575 shader.append(gVS_Main_OutBitmapTexCoords);
Romain Guy889f8d12010-07-29 14:37:42 -0700576 }
Romain Guyac670c02010-07-27 17:39:27 -0700577 // Output transformed position
578 shader.append(gVS_Main_Position);
Chet Haasea1d12dd2012-09-21 14:50:14 -0700579 if (description.hasGradient) {
580 shader.append(gVS_Main_OutGradient[gradientIndex(description)]);
581 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700582 if (description.hasRoundRectClip) {
583 shader.append(gVS_Main_HasRoundRectClip);
584 }
Romain Guyac670c02010-07-27 17:39:27 -0700585 }
586 // End the shader
587 shader.append(gVS_Footer);
588
589 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string());
590
591 return shader;
592}
593
John Reck1bcacfd2017-11-03 10:12:19 -0700594static bool shaderOp(const ProgramDescription& description, String8& shader, const int modulateOp,
595 const char** snippets) {
Romain Guya938f562012-09-13 20:31:08 -0700596 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
Romain Guy253f2c22016-09-28 17:34:42 -0700597 op = op * 2 + description.hasGammaCorrection;
Romain Guya938f562012-09-13 20:31:08 -0700598 shader.append(snippets[op]);
599 return description.hasAlpha8Texture;
600}
601
Romain Guyac670c02010-07-27 17:39:27 -0700602String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) {
Chris Craik8bd68c62015-08-19 15:29:05 -0700603 String8 shader(gFS_Header_Start);
Romain Guya5aed0d2010-09-09 14:42:43 -0700604
Mike Reedc2f31df2016-10-28 17:21:45 -0400605 const bool blendFramebuffer = description.framebufferMode >= SkBlendMode::kPlus;
Romain Guya5aed0d2010-09-09 14:42:43 -0700606 if (blendFramebuffer) {
607 shader.append(gFS_Header_Extension_FramebufferFetch);
608 }
John Reck1bcacfd2017-11-03 10:12:19 -0700609 if (description.hasExternalTexture ||
610 (description.hasBitmap && description.isShaderBitmapExternal)) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700611 shader.append(gFS_Header_Extension_ExternalTexture);
612 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700613
614 shader.append(gFS_Header);
Romain Guyac670c02010-07-27 17:39:27 -0700615
616 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700617 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700618 shader.append(gVS_Header_Varyings_HasTexture);
619 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700620 if (description.hasVertexAlpha) {
621 shader.append(gVS_Header_Varyings_HasVertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700622 }
Romain Guyff316ec2013-02-13 18:39:43 -0800623 if (description.hasColors) {
624 shader.append(gVS_Header_Varyings_HasColors);
625 }
Romain Guyac670c02010-07-27 17:39:27 -0700626 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700627 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700628 }
629 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700630 shader.append(gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700631 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700632 if (description.hasRoundRectClip) {
633 shader.append(gVS_Header_Varyings_HasRoundRectClip);
634 }
Romain Guyac670c02010-07-27 17:39:27 -0700635
Romain Guyac670c02010-07-27 17:39:27 -0700636 // Uniforms
Romain Guy707b2f72010-10-11 16:34:59 -0700637 int modulateOp = MODULATE_OP_NO_MODULATE;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700638 const bool singleColor = !description.hasTexture && !description.hasExternalTexture &&
John Reck1bcacfd2017-11-03 10:12:19 -0700639 !description.hasGradient && !description.hasBitmap;
Romain Guy707b2f72010-10-11 16:34:59 -0700640
641 if (description.modulate || singleColor) {
642 shader.append(gFS_Uniforms_Color);
643 if (!singleColor) modulateOp = MODULATE_OP_MODULATE;
644 }
Chris Craik138c21f2016-04-28 16:59:42 -0700645 if (description.hasTexture || description.useShadowAlphaInterp) {
Romain Guyac670c02010-07-27 17:39:27 -0700646 shader.append(gFS_Uniforms_TextureSampler);
Romain Guy8f0095c2011-05-02 17:24:22 -0700647 } else if (description.hasExternalTexture) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700648 shader.append(gFS_Uniforms_ExternalTextureSampler);
649 }
Romain Guyac670c02010-07-27 17:39:27 -0700650 if (description.hasGradient) {
Romain Guy253f2c22016-09-28 17:34:42 -0700651 shader.append(gFS_Uniforms_GradientSampler[description.isSimpleGradient]);
Romain Guyac670c02010-07-27 17:39:27 -0700652 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700653 if (description.hasRoundRectClip) {
654 shader.append(gFS_Uniforms_HasRoundRectClip);
655 }
Romain Guy707b2f72010-10-11 16:34:59 -0700656
Romain Guy253f2c22016-09-28 17:34:42 -0700657 if (description.hasGammaCorrection) {
John Reck1bcacfd2017-11-03 10:12:19 -0700658 shader.appendFormat(gFS_Gamma_Preamble, Properties::textGamma,
659 1.0f / Properties::textGamma);
Romain Guy253f2c22016-09-28 17:34:42 -0700660 }
661
Romain Guyac670c02010-07-27 17:39:27 -0700662 if (description.hasBitmap) {
sergeyv9c97e482016-12-12 16:14:11 -0800663 if (description.isShaderBitmapExternal) {
664 shader.append(gFS_Uniforms_BitmapExternalSampler);
665 } else {
666 shader.append(gFS_Uniforms_BitmapSampler);
667 }
Romain Guyac670c02010-07-27 17:39:27 -0700668 }
Chris Craikb9ce116d2015-08-20 15:14:06 -0700669 shader.append(gFS_Uniforms_ColorOp[static_cast<int>(description.colorOp)]);
Romain Guyac670c02010-07-27 17:39:27 -0700670
Romain Guycaaaa662017-03-27 00:40:21 -0700671 if (description.hasColorSpaceConversion) {
672 shader.append(gFS_Uniforms_ColorSpaceConversion);
673 }
674 shader.append(gFS_Uniforms_TransferFunction[static_cast<int>(description.transferFunction)]);
675
Romain Guyac670c02010-07-27 17:39:27 -0700676 // Generate required functions
677 if (description.hasGradient && description.hasBitmap) {
Romain Guy48daa542010-08-10 19:21:34 -0700678 generateBlend(shader, "blendShaders", description.shadersMode);
Romain Guyac670c02010-07-27 17:39:27 -0700679 }
Chris Craikb9ce116d2015-08-20 15:14:06 -0700680 if (description.colorOp == ProgramDescription::ColorFilterMode::Blend) {
Romain Guy48daa542010-08-10 19:21:34 -0700681 generateBlend(shader, "blendColors", description.colorMode);
Romain Guyac670c02010-07-27 17:39:27 -0700682 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700683 if (blendFramebuffer) {
684 generateBlend(shader, "blendFramebuffer", description.framebufferMode);
685 }
sergeyv554ffeb2016-11-15 18:01:21 -0800686 if (description.useShaderBasedWrap) {
Romain Guy889f8d12010-07-29 14:37:42 -0700687 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
688 }
John Reck1bcacfd2017-11-03 10:12:19 -0700689 if (description.hasGradient || description.hasLinearTexture ||
690 description.hasColorSpaceConversion) {
Romain Guycaaaa662017-03-27 00:40:21 -0700691 shader.append(gFS_sRGB_TransferFunctions);
Romain Guy636afc12017-02-07 11:21:05 -0800692 }
693 if (description.hasBitmap || ((description.hasTexture || description.hasExternalTexture) &&
John Reck1bcacfd2017-11-03 10:12:19 -0700694 !description.hasAlpha8Texture)) {
Romain Guycaaaa662017-03-27 00:40:21 -0700695 shader.append(gFS_TransferFunction[static_cast<int>(description.transferFunction)]);
John Reck1bcacfd2017-11-03 10:12:19 -0700696 shader.append(
697 gFS_OETF[(description.hasLinearTexture || description.hasColorSpaceConversion) &&
698 !mHasLinearBlending]);
Romain Guycaaaa662017-03-27 00:40:21 -0700699 shader.append(gFS_ColorConvert[description.hasColorSpaceConversion
John Reck1bcacfd2017-11-03 10:12:19 -0700700 ? 1 + description.hasTranslucentConversion
701 : 0]);
Romain Guy636afc12017-02-07 11:21:05 -0800702 }
Romain Guy253f2c22016-09-28 17:34:42 -0700703 if (description.hasGradient) {
Romain Guycaaaa662017-03-27 00:40:21 -0700704 shader.append(gFS_GradientFunctions);
705 shader.append(gFS_GradientPreamble[mHasLinearBlending]);
Romain Guy253f2c22016-09-28 17:34:42 -0700706 }
Romain Guyac670c02010-07-27 17:39:27 -0700707
708 // Begin the shader
John Reck1bcacfd2017-11-03 10:12:19 -0700709 shader.append(gFS_Main);
710 {
Romain Guyac670c02010-07-27 17:39:27 -0700711 // Stores the result in fragColor directly
Romain Guyaa6c24c2011-04-28 18:40:04 -0700712 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700713 if (description.hasAlpha8Texture) {
Romain Guy707b2f72010-10-11 16:34:59 -0700714 if (!description.hasGradient && !description.hasBitmap) {
John Reck1bcacfd2017-11-03 10:12:19 -0700715 shader.append(gFS_Main_FetchA8Texture[modulateOp * 2 +
716 description.hasGammaCorrection]);
Romain Guy707b2f72010-10-11 16:34:59 -0700717 }
Romain Guyac670c02010-07-27 17:39:27 -0700718 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700719 shader.append(gFS_Main_FetchTexture[modulateOp]);
Romain Guyac670c02010-07-27 17:39:27 -0700720 }
721 } else {
Romain Guya938f562012-09-13 20:31:08 -0700722 if (!description.hasGradient && !description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700723 shader.append(gFS_Main_FetchColor);
724 }
Romain Guyac670c02010-07-27 17:39:27 -0700725 }
726 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700727 shader.append(gFS_Main_FetchGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700728 }
729 if (description.hasBitmap) {
sergeyv554ffeb2016-11-15 18:01:21 -0800730 if (!description.useShaderBasedWrap) {
Romain Guy889f8d12010-07-29 14:37:42 -0700731 shader.append(gFS_Main_FetchBitmap);
732 } else {
733 shader.append(gFS_Main_FetchBitmapNpot);
734 }
Romain Guyac670c02010-07-27 17:39:27 -0700735 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700736 bool applyModulate = false;
Romain Guyac670c02010-07-27 17:39:27 -0700737 // Case when we have two shaders set
738 if (description.hasGradient && description.hasBitmap) {
739 if (description.isBitmapFirst) {
740 shader.append(gFS_Main_BlendShadersBG);
741 } else {
742 shader.append(gFS_Main_BlendShadersGB);
743 }
John Reck1bcacfd2017-11-03 10:12:19 -0700744 applyModulate =
745 shaderOp(description, shader, modulateOp, gFS_Main_BlendShaders_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700746 } else {
747 if (description.hasGradient) {
John Reck1bcacfd2017-11-03 10:12:19 -0700748 applyModulate =
749 shaderOp(description, shader, modulateOp, gFS_Main_GradientShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700750 } else if (description.hasBitmap) {
John Reck1bcacfd2017-11-03 10:12:19 -0700751 applyModulate =
752 shaderOp(description, shader, modulateOp, gFS_Main_BitmapShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700753 }
Romain Guyac670c02010-07-27 17:39:27 -0700754 }
Romain Guya938f562012-09-13 20:31:08 -0700755
Romain Guy740bf2b2011-04-26 15:33:10 -0700756 if (description.modulate && applyModulate) {
Romain Guya938f562012-09-13 20:31:08 -0700757 shader.append(gFS_Main_ModulateColor);
Romain Guy740bf2b2011-04-26 15:33:10 -0700758 }
Romain Guya938f562012-09-13 20:31:08 -0700759
Romain Guyac670c02010-07-27 17:39:27 -0700760 // Apply the color op if needed
Chris Craikb9ce116d2015-08-20 15:14:06 -0700761 shader.append(gFS_Main_ApplyColorOp[static_cast<int>(description.colorOp)]);
Chris Craik9f44a132012-09-13 18:34:55 -0700762
Chris Craik91a8c7c2014-08-12 14:31:35 -0700763 if (description.hasVertexAlpha) {
764 if (description.useShadowAlphaInterp) {
765 shader.append(gFS_Main_ApplyVertexAlphaShadowInterp);
Chris Craikbf759452014-08-11 16:00:44 -0700766 } else {
Chris Craik91a8c7c2014-08-12 14:31:35 -0700767 shader.append(gFS_Main_ApplyVertexAlphaLinearInterp);
Chris Craikbf759452014-08-11 16:00:44 -0700768 }
Chris Craik9f44a132012-09-13 18:34:55 -0700769 }
770
Romain Guy253f2c22016-09-28 17:34:42 -0700771 if (description.hasGradient) {
772 shader.append(gFS_Main_AddDither);
773 }
774
Romain Guyac670c02010-07-27 17:39:27 -0700775 // Output the fragment
Romain Guya5aed0d2010-09-09 14:42:43 -0700776 if (!blendFramebuffer) {
777 shader.append(gFS_Main_FragColor);
778 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700779 shader.append(!description.swapSrcDst ? gFS_Main_FragColor_Blend
780 : gFS_Main_FragColor_Blend_Swap);
Romain Guya5aed0d2010-09-09 14:42:43 -0700781 }
Romain Guyff316ec2013-02-13 18:39:43 -0800782 if (description.hasColors) {
783 shader.append(gFS_Main_FragColor_HasColors);
784 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700785 if (description.hasRoundRectClip) {
786 shader.append(gFS_Main_FragColor_HasRoundRectClip);
787 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800788 if (description.hasDebugHighlight) {
789 shader.append(gFS_Main_DebugHighlight);
790 }
Romain Guyac670c02010-07-27 17:39:27 -0700791 }
792 // End the shader
793 shader.append(gFS_Footer);
794
Romain Guy91a8ec02017-02-08 07:45:11 -0800795#if DEBUG_PROGRAMS
John Reck1bcacfd2017-11-03 10:12:19 -0700796 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
797 printLongString(shader);
Romain Guy91a8ec02017-02-08 07:45:11 -0800798#endif
Romain Guydb1938e2010-08-02 18:50:22 -0700799
Romain Guyac670c02010-07-27 17:39:27 -0700800 return shader;
801}
802
Mike Reedc2f31df2016-10-28 17:21:45 -0400803void ProgramCache::generateBlend(String8& shader, const char* name, SkBlendMode mode) {
Romain Guyac670c02010-07-27 17:39:27 -0700804 shader.append("\nvec4 ");
805 shader.append(name);
806 shader.append("(vec4 src, vec4 dst) {\n");
807 shader.append(" ");
Mike Reedc2f31df2016-10-28 17:21:45 -0400808 shader.append(gBlendOps[(int)mode]);
Romain Guyac670c02010-07-27 17:39:27 -0700809 shader.append("}\n");
810}
811
Romain Guy889f8d12010-07-29 14:37:42 -0700812void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) {
Romain Guy63553472012-07-18 20:04:14 -0700813 shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700814 if (wrapS == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700815 shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700816 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n");
817 }
818 if (wrapT == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700819 shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700820 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n");
821 }
822 shader.append(" return vec2(");
823 switch (wrapS) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700824 case GL_CLAMP_TO_EDGE:
825 shader.append("texCoords.x");
826 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700827 case GL_REPEAT:
828 shader.append("mod(texCoords.x, 1.0)");
829 break;
830 case GL_MIRRORED_REPEAT:
831 shader.append("xMod2");
832 break;
833 }
834 shader.append(", ");
835 switch (wrapT) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700836 case GL_CLAMP_TO_EDGE:
837 shader.append("texCoords.y");
838 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700839 case GL_REPEAT:
840 shader.append("mod(texCoords.y, 1.0)");
841 break;
842 case GL_MIRRORED_REPEAT:
843 shader.append("yMod2");
844 break;
845 }
846 shader.append(");\n");
847 shader.append("}\n");
848}
849
Romain Guydb1938e2010-08-02 18:50:22 -0700850void ProgramCache::printLongString(const String8& shader) const {
851 ssize_t index = 0;
852 ssize_t lastIndex = 0;
853 const char* str = shader.string();
854 while ((index = shader.find("\n", index)) > -1) {
855 String8 line(str, index - lastIndex);
856 if (line.length() == 0) line.append("\n");
Chris Craik1c1c3fe2015-03-06 09:40:35 -0800857 ALOGD("%s", line.string());
Romain Guydb1938e2010-08-02 18:50:22 -0700858 index++;
859 str += (index - lastIndex);
860 lastIndex = index;
861 }
862}
863
John Reck1bcacfd2017-11-03 10:12:19 -0700864}; // namespace uirenderer
865}; // namespace android