blob: 00de9924ace1dbe22331a24347131086d18cc10c [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 Guy636afc12017-02-07 11:21:05 -0800164const char* gFS_OETF[2] = {
165 "\nvec4 OETF(const vec4 linear) {\n"
166 " return linear;\n"
167 "}\n",
168 // We expect linear data to be scRGB so we mirror the gamma function
169 "\nvec4 OETF(const vec4 linear) {"
170 " return vec4(sign(linear.rgb) * OETF_sRGB(abs(linear.rgb)), linear.a);\n"
171 "}\n",
172};
173
174const char* gFS_Transfer_Functions = R"__SHADER__(
175 float OETF_sRGB(const float linear) {
176 // IEC 61966-2-1:1999
177 return linear <= 0.0031308 ? linear * 12.92 : (pow(linear, 1.0 / 2.4) * 1.055) - 0.055;
178 }
179
180 vec3 OETF_sRGB(const vec3 linear) {
181 return vec3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b));
182 }
183
184 float EOTF_sRGB(float srgb) {
185 // IEC 61966-2-1:1999
186 return srgb <= 0.04045 ? srgb / 12.92 : pow((srgb + 0.055) / 1.055, 2.4);
187 }
188)__SHADER__";
189
Romain Guy253f2c22016-09-28 17:34:42 -0700190// Dithering must be done in the quantization space
191// When we are writing to an sRGB framebuffer, we must do the following:
Romain Guy636afc12017-02-07 11:21:05 -0800192// EOTF(OETF(color) + dither)
Romain Guy8762e332016-10-12 12:14:07 -0700193// The dithering pattern is generated with a triangle noise generator in the range [-0.0,1.0]
Romain Guy253f2c22016-09-28 17:34:42 -0700194// TODO: Handle linear fp16 render targets
Romain Guy9fe7e162017-02-03 16:16:07 -0800195const char* gFS_Gradient_Functions = R"__SHADER__(
196 float triangleNoise(const highp vec2 n) {
197 highp vec2 p = fract(n * vec2(5.3987, 5.4421));
198 p += dot(p.yx, p.xy + vec2(21.5351, 14.3137));
199 highp float xy = p.x * p.y;
200 return fract(xy * 95.4307) + fract(xy * 75.04961) - 1.0;
201 }
Romain Guy9fe7e162017-02-03 16:16:07 -0800202)__SHADER__";
Romain Guy8762e332016-10-12 12:14:07 -0700203const char* gFS_Gradient_Preamble[2] = {
Romain Guy253f2c22016-09-28 17:34:42 -0700204 // Linear framebuffer
205 "\nvec4 dither(const vec4 color) {\n"
Romain Guya0ed6f02016-12-12 18:21:32 -0800206 " return vec4(color.rgb + (triangleNoise(gl_FragCoord.xy * screenSize.xy) / 255.0), color.a);\n"
Romain Guy8762e332016-10-12 12:14:07 -0700207 "}\n"
208 "\nvec4 gammaMix(const vec4 a, const vec4 b, float v) {\n"
Romain Guy9fe7e162017-02-03 16:16:07 -0800209 " vec4 c = mix(a, b, v);\n"
Romain Guy636afc12017-02-07 11:21:05 -0800210 " c.a = EOTF_sRGB(c.a);\n" // This is technically incorrect but preserves compatibility
211 " return vec4(OETF_sRGB(c.rgb) * c.a, c.a);\n"
Romain Guy253f2c22016-09-28 17:34:42 -0700212 "}\n",
213 // sRGB framebuffer
214 "\nvec4 dither(const vec4 color) {\n"
215 " vec3 dithered = sqrt(color.rgb) + (triangleNoise(gl_FragCoord.xy * screenSize.xy) / 255.0);\n"
216 " return vec4(dithered * dithered, color.a);\n"
217 "}\n"
Romain Guy8762e332016-10-12 12:14:07 -0700218 "\nvec4 gammaMix(const vec4 a, const vec4 b, float v) {\n"
Romain Guya0ed6f02016-12-12 18:21:32 -0800219 " vec4 c = mix(a, b, v);\n"
220 " return vec4(c.rgb * c.a, c.a);\n"
Romain Guy8762e332016-10-12 12:14:07 -0700221 "}\n"
Romain Guy253f2c22016-09-28 17:34:42 -0700222};
223
224// Uses luminance coefficients from Rec.709 to choose the appropriate gamma
225// The gamma() function assumes that bright text will be displayed on a dark
226// background and that dark text will be displayed on bright background
227// The gamma coefficient is chosen to thicken or thin the text accordingly
228// The dot product used to compute the luminance could be approximated with
229// a simple max(color.r, color.g, color.b)
Romain Guy9fe7e162017-02-03 16:16:07 -0800230const char* gFS_Gamma_Preamble = R"__SHADER__(
231 #define GAMMA (%.2f)
232 #define GAMMA_INV (%.2f)
233
234 float gamma(float a, const vec3 color) {
235 float luminance = dot(color, vec3(0.2126, 0.7152, 0.0722));
236 return pow(a, luminance < 0.5 ? GAMMA_INV : GAMMA);
237 }
238)__SHADER__";
Romain Guy253f2c22016-09-28 17:34:42 -0700239
Romain Guyac670c02010-07-27 17:39:27 -0700240const char* gFS_Main =
241 "\nvoid main(void) {\n"
Romain Guy253f2c22016-09-28 17:34:42 -0700242 " vec4 fragColor;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700243
Romain Guy253f2c22016-09-28 17:34:42 -0700244const char* gFS_Main_AddDither =
245 " fragColor = dither(fragColor);\n";
Romain Guy211efea2012-07-31 21:16:07 -0700246
Romain Guy707b2f72010-10-11 16:34:59 -0700247// General case
Romain Guyac670c02010-07-27 17:39:27 -0700248const char* gFS_Main_FetchColor =
249 " fragColor = color;\n";
Romain Guy740bf2b2011-04-26 15:33:10 -0700250const char* gFS_Main_ModulateColor =
251 " fragColor *= color.a;\n";
Chris Craik91a8c7c2014-08-12 14:31:35 -0700252const char* gFS_Main_ApplyVertexAlphaLinearInterp =
Chris Craik6ebdc112012-08-31 18:24:33 -0700253 " fragColor *= alpha;\n";
Chris Craik91a8c7c2014-08-12 14:31:35 -0700254const char* gFS_Main_ApplyVertexAlphaShadowInterp =
Chris Craik138c21f2016-04-28 16:59:42 -0700255 // map alpha through shadow alpha sampler
256 " fragColor *= texture2D(baseSampler, vec2(alpha, 0.5)).a;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700257const char* gFS_Main_FetchTexture[2] = {
258 // Don't modulate
Romain Guy636afc12017-02-07 11:21:05 -0800259 " fragColor = OETF(texture2D(baseSampler, outTexCoords));\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700260 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700261 " fragColor = color * texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700262};
Romain Guy253f2c22016-09-28 17:34:42 -0700263const char* gFS_Main_FetchA8Texture[4] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700264 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700265 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700266 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700267 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700268 " fragColor = color * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700269 " fragColor = color * gamma(texture2D(baseSampler, outTexCoords).a, color.rgb);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700270};
Romain Guy42e1e0d2012-07-30 14:47:51 -0700271const char* gFS_Main_FetchGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700272 // Linear
Romain Guy320d46b2012-08-08 16:05:42 -0700273 " vec4 gradientColor = texture2D(gradientSampler, linear);\n",
Romain Guy211efea2012-07-31 21:16:07 -0700274
Romain Guy8762e332016-10-12 12:14:07 -0700275 " vec4 gradientColor = gammaMix(startColor, endColor, clamp(linear, 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700276
Romain Guyee916f12010-09-20 17:53:08 -0700277 // Circular
Romain Guy320d46b2012-08-08 16:05:42 -0700278 " vec4 gradientColor = texture2D(gradientSampler, vec2(length(circular), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700279
Romain Guy8762e332016-10-12 12:14:07 -0700280 " vec4 gradientColor = gammaMix(startColor, endColor, clamp(length(circular), 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700281
Romain Guyee916f12010-09-20 17:53:08 -0700282 // Sweep
Romain Guy63553472012-07-18 20:04:14 -0700283 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700284 " vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700285
Romain Guy42e1e0d2012-07-30 14:47:51 -0700286 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy8762e332016-10-12 12:14:07 -0700287 " vec4 gradientColor = gammaMix(startColor, endColor, clamp(index - floor(index), 0.0, 1.0));\n"
Romain Guyee916f12010-09-20 17:53:08 -0700288};
Romain Guyac670c02010-07-27 17:39:27 -0700289const char* gFS_Main_FetchBitmap =
Romain Guy636afc12017-02-07 11:21:05 -0800290 " vec4 bitmapColor = OETF(texture2D(bitmapSampler, outBitmapTexCoords));\n";
Romain Guy889f8d12010-07-29 14:37:42 -0700291const char* gFS_Main_FetchBitmapNpot =
Romain Guy636afc12017-02-07 11:21:05 -0800292 " vec4 bitmapColor = OETF(texture2D(bitmapSampler, wrap(outBitmapTexCoords)));\n";
Romain Guyac670c02010-07-27 17:39:27 -0700293const char* gFS_Main_BlendShadersBG =
Romain Guyac670c02010-07-27 17:39:27 -0700294 " fragColor = blendShaders(gradientColor, bitmapColor)";
Romain Guy06f96e22010-07-30 19:18:16 -0700295const char* gFS_Main_BlendShadersGB =
296 " fragColor = blendShaders(bitmapColor, gradientColor)";
Romain Guy253f2c22016-09-28 17:34:42 -0700297const char* gFS_Main_BlendShaders_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700298 // Don't modulate
299 ";\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700300 ";\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700301 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700302 " * color.a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700303 " * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700304 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700305 " * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700306 " * gamma(texture2D(baseSampler, outTexCoords).a, color.rgb);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700307};
Romain Guy253f2c22016-09-28 17:34:42 -0700308const char* gFS_Main_GradientShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700309 // Don't modulate
310 " fragColor = gradientColor;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700311 " fragColor = gradientColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700312 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700313 " fragColor = gradientColor * color.a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700314 " fragColor = gradientColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700315 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700316 " fragColor = gradientColor * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700317 " fragColor = gradientColor * gamma(texture2D(baseSampler, outTexCoords).a, gradientColor.rgb);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700318 };
Romain Guy253f2c22016-09-28 17:34:42 -0700319const char* gFS_Main_BitmapShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700320 // Don't modulate
321 " fragColor = bitmapColor;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700322 " fragColor = bitmapColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700323 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700324 " fragColor = bitmapColor * color.a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700325 " fragColor = bitmapColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700326 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700327 " fragColor = bitmapColor * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700328 " fragColor = bitmapColor * gamma(texture2D(baseSampler, outTexCoords).a, bitmapColor.rgb);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700329 };
Romain Guyac670c02010-07-27 17:39:27 -0700330const char* gFS_Main_FragColor =
331 " gl_FragColor = fragColor;\n";
Romain Guyff316ec2013-02-13 18:39:43 -0800332const char* gFS_Main_FragColor_HasColors =
333 " gl_FragColor *= outColors;\n";
Romain Guya5aed0d2010-09-09 14:42:43 -0700334const char* gFS_Main_FragColor_Blend =
335 " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n";
Romain Guyf607bdc2010-09-10 19:20:06 -0700336const char* gFS_Main_FragColor_Blend_Swap =
337 " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n";
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500338const char* gFS_Main_ApplyColorOp[3] = {
Romain Guyac670c02010-07-27 17:39:27 -0700339 // None
340 "",
341 // Matrix
Chris Craik73821c82014-09-16 17:32:13 -0700342 " fragColor.rgb /= (fragColor.a + 0.0019);\n" // un-premultiply
Romain Guyac670c02010-07-27 17:39:27 -0700343 " fragColor *= colorMatrix;\n"
Chris Craik73821c82014-09-16 17:32:13 -0700344 " fragColor += colorMatrixVector;\n"
345 " fragColor.rgb *= (fragColor.a + 0.0019);\n", // re-premultiply
Romain Guyac670c02010-07-27 17:39:27 -0700346 // PorterDuff
347 " fragColor = blendColors(colorBlend, fragColor);\n"
348};
Chris Craikdeeda3d2014-05-05 19:09:33 -0700349
350// Note: LTRB -> xyzw
351const char* gFS_Main_FragColor_HasRoundRectClip =
352 " mediump vec2 fragToLT = roundRectInnerRectLTRB.xy - roundRectPos;\n"
353 " mediump vec2 fragFromRB = roundRectPos - roundRectInnerRectLTRB.zw;\n"
Chris Craikf99f3202014-08-05 15:31:52 -0700354
355 // divide + multiply by 128 to avoid falling out of range in length() function
356 " mediump vec2 dist = max(max(fragToLT, fragFromRB), vec2(0.0, 0.0)) / 128.0;\n"
357 " mediump float linearDist = roundRectRadius - (length(dist) * 128.0);\n"
Chris Craikdeeda3d2014-05-05 19:09:33 -0700358 " gl_FragColor *= clamp(linearDist, 0.0, 1.0);\n";
359
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800360const char* gFS_Main_DebugHighlight =
361 " gl_FragColor.rgb = vec3(0.0, gl_FragColor.a, 0.0);\n";
Romain Guyac670c02010-07-27 17:39:27 -0700362const char* gFS_Footer =
363 "}\n\n";
364
365///////////////////////////////////////////////////////////////////////////////
366// PorterDuff snippets
367///////////////////////////////////////////////////////////////////////////////
368
Romain Guy48daa542010-08-10 19:21:34 -0700369const char* gBlendOps[18] = {
Romain Guyac670c02010-07-27 17:39:27 -0700370 // Clear
371 "return vec4(0.0, 0.0, 0.0, 0.0);\n",
372 // Src
373 "return src;\n",
374 // Dst
375 "return dst;\n",
376 // SrcOver
Romain Guy06f96e22010-07-30 19:18:16 -0700377 "return src + dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700378 // DstOver
Romain Guy06f96e22010-07-30 19:18:16 -0700379 "return dst + src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700380 // SrcIn
Romain Guy06f96e22010-07-30 19:18:16 -0700381 "return src * dst.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700382 // DstIn
Romain Guy06f96e22010-07-30 19:18:16 -0700383 "return dst * src.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700384 // SrcOut
Romain Guy06f96e22010-07-30 19:18:16 -0700385 "return src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700386 // DstOut
Romain Guy06f96e22010-07-30 19:18:16 -0700387 "return dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700388 // SrcAtop
389 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n",
390 // DstAtop
391 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n",
392 // Xor
Romain Guy48daa542010-08-10 19:21:34 -0700393 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, "
Romain Guyac670c02010-07-27 17:39:27 -0700394 "src.a + dst.a - 2.0 * src.a * dst.a);\n",
Derek Sollenbergerc0bf7002015-03-06 13:48:27 -0500395 // Plus
Romain Guy48daa542010-08-10 19:21:34 -0700396 "return min(src + dst, 1.0);\n",
Derek Sollenbergerc0bf7002015-03-06 13:48:27 -0500397 // Modulate
Romain Guy48daa542010-08-10 19:21:34 -0700398 "return src * dst;\n",
399 // Screen
400 "return src + dst - src * dst;\n",
401 // Overlay
402 "return clamp(vec4(mix("
403 "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), "
404 "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), "
405 "step(dst.a, 2.0 * dst.rgb)), "
406 "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n",
407 // Darken
408 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
409 "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
410 // Lighten
411 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
412 "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 -0700413};
414
415///////////////////////////////////////////////////////////////////////////////
416// Constructors/destructors
417///////////////////////////////////////////////////////////////////////////////
418
Chris Craik117bdbc2015-02-05 10:12:38 -0800419ProgramCache::ProgramCache(Extensions& extensions)
Romain Guy253f2c22016-09-28 17:34:42 -0700420 : mHasES3(extensions.getMajorGlVersion() >= 3)
421 , mHasSRGB(extensions.hasSRGB()) {
Romain Guyac670c02010-07-27 17:39:27 -0700422}
423
424ProgramCache::~ProgramCache() {
425 clear();
426}
427
428///////////////////////////////////////////////////////////////////////////////
429// Cache management
430///////////////////////////////////////////////////////////////////////////////
431
432void ProgramCache::clear() {
Romain Guy67f27952010-12-07 20:09:23 -0800433 PROGRAM_LOGD("Clearing program cache");
Romain Guyac670c02010-07-27 17:39:27 -0700434 mCache.clear();
435}
436
437Program* ProgramCache::get(const ProgramDescription& description) {
438 programid key = description.key();
Chris Craik096b8d92013-03-01 11:08:11 -0800439 if (key == (PROGRAM_KEY_TEXTURE | PROGRAM_KEY_A8_TEXTURE)) {
440 // program for A8, unmodulated, texture w/o shader (black text/path textures) is equivalent
441 // to standard texture program (bitmaps, patches). Consider them equivalent.
442 key = PROGRAM_KEY_TEXTURE;
443 }
444
Chris Craik51d6a3d2014-12-22 17:16:56 -0800445 auto iter = mCache.find(key);
Chris Craikd41c4d82015-01-05 15:51:13 -0800446 Program* program = nullptr;
Chris Craik51d6a3d2014-12-22 17:16:56 -0800447 if (iter == mCache.end()) {
Romain Guyee916f12010-09-20 17:53:08 -0700448 description.log("Could not find program");
Romain Guyac670c02010-07-27 17:39:27 -0700449 program = generateProgram(description, key);
Chris Craik51d6a3d2014-12-22 17:16:56 -0800450 mCache[key] = std::unique_ptr<Program>(program);
Romain Guyac670c02010-07-27 17:39:27 -0700451 } else {
Chris Craik51d6a3d2014-12-22 17:16:56 -0800452 program = iter->second.get();
Romain Guyac670c02010-07-27 17:39:27 -0700453 }
454 return program;
455}
456
457///////////////////////////////////////////////////////////////////////////////
458// Program generation
459///////////////////////////////////////////////////////////////////////////////
460
Andreas Gampe64bb4132014-11-22 00:35:09 +0000461Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) {
Romain Guyac670c02010-07-27 17:39:27 -0700462 String8 vertexShader = generateVertexShader(description);
463 String8 fragmentShader = generateFragmentShader(description);
464
Romain Guy42e1e0d2012-07-30 14:47:51 -0700465 return new Program(description, vertexShader.string(), fragmentShader.string());
466}
467
468static inline size_t gradientIndex(const ProgramDescription& description) {
469 return description.gradientType * 2 + description.isSimpleGradient;
Romain Guyac670c02010-07-27 17:39:27 -0700470}
471
472String8 ProgramCache::generateVertexShader(const ProgramDescription& description) {
473 // Add attributes
Chris Craik8bd68c62015-08-19 15:29:05 -0700474 String8 shader(gVS_Header_Start);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700475 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700476 shader.append(gVS_Header_Attributes_TexCoords);
477 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700478 if (description.hasVertexAlpha) {
479 shader.append(gVS_Header_Attributes_VertexAlphaParameters);
Chet Haase5b0200b2011-04-13 17:58:08 -0700480 }
Romain Guyff316ec2013-02-13 18:39:43 -0800481 if (description.hasColors) {
482 shader.append(gVS_Header_Attributes_Colors);
483 }
Romain Guyac670c02010-07-27 17:39:27 -0700484 // Uniforms
485 shader.append(gVS_Header_Uniforms);
Romain Guy8f0095c2011-05-02 17:24:22 -0700486 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700487 shader.append(gVS_Header_Uniforms_TextureTransform);
488 }
Romain Guyac670c02010-07-27 17:39:27 -0700489 if (description.hasGradient) {
Romain Guyb4880042013-04-05 11:17:55 -0700490 shader.append(gVS_Header_Uniforms_HasGradient);
Romain Guyac670c02010-07-27 17:39:27 -0700491 }
Romain Guy889f8d12010-07-29 14:37:42 -0700492 if (description.hasBitmap) {
493 shader.append(gVS_Header_Uniforms_HasBitmap);
494 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700495 if (description.hasRoundRectClip) {
496 shader.append(gVS_Header_Uniforms_HasRoundRectClip);
497 }
Romain Guyac670c02010-07-27 17:39:27 -0700498 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700499 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700500 shader.append(gVS_Header_Varyings_HasTexture);
501 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700502 if (description.hasVertexAlpha) {
503 shader.append(gVS_Header_Varyings_HasVertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700504 }
Romain Guyff316ec2013-02-13 18:39:43 -0800505 if (description.hasColors) {
506 shader.append(gVS_Header_Varyings_HasColors);
507 }
Romain Guyac670c02010-07-27 17:39:27 -0700508 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700509 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700510 }
511 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700512 shader.append(gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700513 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700514 if (description.hasRoundRectClip) {
515 shader.append(gVS_Header_Varyings_HasRoundRectClip);
516 }
Romain Guyac670c02010-07-27 17:39:27 -0700517
518 // Begin the shader
519 shader.append(gVS_Main); {
Romain Guy8f0095c2011-05-02 17:24:22 -0700520 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700521 shader.append(gVS_Main_OutTransformedTexCoords);
Romain Guy8f0095c2011-05-02 17:24:22 -0700522 } else if (description.hasTexture || description.hasExternalTexture) {
523 shader.append(gVS_Main_OutTexCoords);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700524 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700525 if (description.hasVertexAlpha) {
526 shader.append(gVS_Main_VertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700527 }
Romain Guyff316ec2013-02-13 18:39:43 -0800528 if (description.hasColors) {
529 shader.append(gVS_Main_OutColors);
530 }
Romain Guy889f8d12010-07-29 14:37:42 -0700531 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700532 shader.append(gVS_Main_OutBitmapTexCoords);
Romain Guy889f8d12010-07-29 14:37:42 -0700533 }
Romain Guyac670c02010-07-27 17:39:27 -0700534 // Output transformed position
535 shader.append(gVS_Main_Position);
Chet Haasea1d12dd2012-09-21 14:50:14 -0700536 if (description.hasGradient) {
537 shader.append(gVS_Main_OutGradient[gradientIndex(description)]);
538 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700539 if (description.hasRoundRectClip) {
540 shader.append(gVS_Main_HasRoundRectClip);
541 }
Romain Guyac670c02010-07-27 17:39:27 -0700542 }
543 // End the shader
544 shader.append(gVS_Footer);
545
546 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string());
547
548 return shader;
549}
550
Romain Guya938f562012-09-13 20:31:08 -0700551static bool shaderOp(const ProgramDescription& description, String8& shader,
552 const int modulateOp, const char** snippets) {
553 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
Romain Guy253f2c22016-09-28 17:34:42 -0700554 op = op * 2 + description.hasGammaCorrection;
Romain Guya938f562012-09-13 20:31:08 -0700555 shader.append(snippets[op]);
556 return description.hasAlpha8Texture;
557}
558
Romain Guyac670c02010-07-27 17:39:27 -0700559String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) {
Chris Craik8bd68c62015-08-19 15:29:05 -0700560 String8 shader(gFS_Header_Start);
Romain Guya5aed0d2010-09-09 14:42:43 -0700561
Mike Reedc2f31df2016-10-28 17:21:45 -0400562 const bool blendFramebuffer = description.framebufferMode >= SkBlendMode::kPlus;
Romain Guya5aed0d2010-09-09 14:42:43 -0700563 if (blendFramebuffer) {
564 shader.append(gFS_Header_Extension_FramebufferFetch);
565 }
sergeyv9c97e482016-12-12 16:14:11 -0800566 if (description.hasExternalTexture
567 || (description.hasBitmap && description.isShaderBitmapExternal)) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700568 shader.append(gFS_Header_Extension_ExternalTexture);
569 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700570
571 shader.append(gFS_Header);
Romain Guyac670c02010-07-27 17:39:27 -0700572
573 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700574 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700575 shader.append(gVS_Header_Varyings_HasTexture);
576 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700577 if (description.hasVertexAlpha) {
578 shader.append(gVS_Header_Varyings_HasVertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700579 }
Romain Guyff316ec2013-02-13 18:39:43 -0800580 if (description.hasColors) {
581 shader.append(gVS_Header_Varyings_HasColors);
582 }
Romain Guyac670c02010-07-27 17:39:27 -0700583 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700584 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700585 }
586 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700587 shader.append(gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700588 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700589 if (description.hasRoundRectClip) {
590 shader.append(gVS_Header_Varyings_HasRoundRectClip);
591 }
Romain Guyac670c02010-07-27 17:39:27 -0700592
Romain Guyac670c02010-07-27 17:39:27 -0700593 // Uniforms
Romain Guy707b2f72010-10-11 16:34:59 -0700594 int modulateOp = MODULATE_OP_NO_MODULATE;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700595 const bool singleColor = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700596 !description.hasGradient && !description.hasBitmap;
597
598 if (description.modulate || singleColor) {
599 shader.append(gFS_Uniforms_Color);
600 if (!singleColor) modulateOp = MODULATE_OP_MODULATE;
601 }
Chris Craik138c21f2016-04-28 16:59:42 -0700602 if (description.hasTexture || description.useShadowAlphaInterp) {
Romain Guyac670c02010-07-27 17:39:27 -0700603 shader.append(gFS_Uniforms_TextureSampler);
Romain Guy8f0095c2011-05-02 17:24:22 -0700604 } else if (description.hasExternalTexture) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700605 shader.append(gFS_Uniforms_ExternalTextureSampler);
606 }
Romain Guyac670c02010-07-27 17:39:27 -0700607 if (description.hasGradient) {
Romain Guy253f2c22016-09-28 17:34:42 -0700608 shader.append(gFS_Uniforms_GradientSampler[description.isSimpleGradient]);
Romain Guyac670c02010-07-27 17:39:27 -0700609 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700610 if (description.hasRoundRectClip) {
611 shader.append(gFS_Uniforms_HasRoundRectClip);
612 }
Romain Guy707b2f72010-10-11 16:34:59 -0700613
Romain Guy253f2c22016-09-28 17:34:42 -0700614 if (description.hasGammaCorrection) {
615 shader.appendFormat(gFS_Gamma_Preamble, Properties::textGamma, 1.0f / Properties::textGamma);
616 }
617
Romain Guyac670c02010-07-27 17:39:27 -0700618 if (description.hasBitmap) {
sergeyv9c97e482016-12-12 16:14:11 -0800619 if (description.isShaderBitmapExternal) {
620 shader.append(gFS_Uniforms_BitmapExternalSampler);
621 } else {
622 shader.append(gFS_Uniforms_BitmapSampler);
623 }
Romain Guyac670c02010-07-27 17:39:27 -0700624 }
Chris Craikb9ce116d2015-08-20 15:14:06 -0700625 shader.append(gFS_Uniforms_ColorOp[static_cast<int>(description.colorOp)]);
Romain Guyac670c02010-07-27 17:39:27 -0700626
627 // Generate required functions
628 if (description.hasGradient && description.hasBitmap) {
Romain Guy48daa542010-08-10 19:21:34 -0700629 generateBlend(shader, "blendShaders", description.shadersMode);
Romain Guyac670c02010-07-27 17:39:27 -0700630 }
Chris Craikb9ce116d2015-08-20 15:14:06 -0700631 if (description.colorOp == ProgramDescription::ColorFilterMode::Blend) {
Romain Guy48daa542010-08-10 19:21:34 -0700632 generateBlend(shader, "blendColors", description.colorMode);
Romain Guyac670c02010-07-27 17:39:27 -0700633 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700634 if (blendFramebuffer) {
635 generateBlend(shader, "blendFramebuffer", description.framebufferMode);
636 }
sergeyv554ffeb2016-11-15 18:01:21 -0800637 if (description.useShaderBasedWrap) {
Romain Guy889f8d12010-07-29 14:37:42 -0700638 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
639 }
Romain Guy636afc12017-02-07 11:21:05 -0800640 if (description.hasGradient || description.hasLinearTexture) {
641 shader.append(gFS_Transfer_Functions);
642 }
643 if (description.hasBitmap || ((description.hasTexture || description.hasExternalTexture) &&
644 !description.hasAlpha8Texture)) {
645 shader.append(gFS_OETF[description.hasLinearTexture && !mHasSRGB]);
646 }
Romain Guy253f2c22016-09-28 17:34:42 -0700647 if (description.hasGradient) {
Romain Guy8762e332016-10-12 12:14:07 -0700648 shader.append(gFS_Gradient_Functions);
649 shader.append(gFS_Gradient_Preamble[mHasSRGB]);
Romain Guy253f2c22016-09-28 17:34:42 -0700650 }
Romain Guyac670c02010-07-27 17:39:27 -0700651
652 // Begin the shader
653 shader.append(gFS_Main); {
654 // Stores the result in fragColor directly
Romain Guyaa6c24c2011-04-28 18:40:04 -0700655 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700656 if (description.hasAlpha8Texture) {
Romain Guy707b2f72010-10-11 16:34:59 -0700657 if (!description.hasGradient && !description.hasBitmap) {
Romain Guy253f2c22016-09-28 17:34:42 -0700658 shader.append(
659 gFS_Main_FetchA8Texture[modulateOp * 2 + description.hasGammaCorrection]);
Romain Guy707b2f72010-10-11 16:34:59 -0700660 }
Romain Guyac670c02010-07-27 17:39:27 -0700661 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700662 shader.append(gFS_Main_FetchTexture[modulateOp]);
Romain Guyac670c02010-07-27 17:39:27 -0700663 }
664 } else {
Romain Guya938f562012-09-13 20:31:08 -0700665 if (!description.hasGradient && !description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700666 shader.append(gFS_Main_FetchColor);
667 }
Romain Guyac670c02010-07-27 17:39:27 -0700668 }
669 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700670 shader.append(gFS_Main_FetchGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700671 }
672 if (description.hasBitmap) {
sergeyv554ffeb2016-11-15 18:01:21 -0800673 if (!description.useShaderBasedWrap) {
Romain Guy889f8d12010-07-29 14:37:42 -0700674 shader.append(gFS_Main_FetchBitmap);
675 } else {
676 shader.append(gFS_Main_FetchBitmapNpot);
677 }
Romain Guyac670c02010-07-27 17:39:27 -0700678 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700679 bool applyModulate = false;
Romain Guyac670c02010-07-27 17:39:27 -0700680 // Case when we have two shaders set
681 if (description.hasGradient && description.hasBitmap) {
682 if (description.isBitmapFirst) {
683 shader.append(gFS_Main_BlendShadersBG);
684 } else {
685 shader.append(gFS_Main_BlendShadersGB);
686 }
Romain Guya938f562012-09-13 20:31:08 -0700687 applyModulate = shaderOp(description, shader, modulateOp,
688 gFS_Main_BlendShaders_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700689 } else {
690 if (description.hasGradient) {
Romain Guya938f562012-09-13 20:31:08 -0700691 applyModulate = shaderOp(description, shader, modulateOp,
692 gFS_Main_GradientShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700693 } else if (description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700694 applyModulate = shaderOp(description, shader, modulateOp,
695 gFS_Main_BitmapShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700696 }
Romain Guyac670c02010-07-27 17:39:27 -0700697 }
Romain Guya938f562012-09-13 20:31:08 -0700698
Romain Guy740bf2b2011-04-26 15:33:10 -0700699 if (description.modulate && applyModulate) {
Romain Guya938f562012-09-13 20:31:08 -0700700 shader.append(gFS_Main_ModulateColor);
Romain Guy740bf2b2011-04-26 15:33:10 -0700701 }
Romain Guya938f562012-09-13 20:31:08 -0700702
Romain Guyac670c02010-07-27 17:39:27 -0700703 // Apply the color op if needed
Chris Craikb9ce116d2015-08-20 15:14:06 -0700704 shader.append(gFS_Main_ApplyColorOp[static_cast<int>(description.colorOp)]);
Chris Craik9f44a132012-09-13 18:34:55 -0700705
Chris Craik91a8c7c2014-08-12 14:31:35 -0700706 if (description.hasVertexAlpha) {
707 if (description.useShadowAlphaInterp) {
708 shader.append(gFS_Main_ApplyVertexAlphaShadowInterp);
Chris Craikbf759452014-08-11 16:00:44 -0700709 } else {
Chris Craik91a8c7c2014-08-12 14:31:35 -0700710 shader.append(gFS_Main_ApplyVertexAlphaLinearInterp);
Chris Craikbf759452014-08-11 16:00:44 -0700711 }
Chris Craik9f44a132012-09-13 18:34:55 -0700712 }
713
Romain Guy253f2c22016-09-28 17:34:42 -0700714 if (description.hasGradient) {
715 shader.append(gFS_Main_AddDither);
716 }
717
Romain Guyac670c02010-07-27 17:39:27 -0700718 // Output the fragment
Romain Guya5aed0d2010-09-09 14:42:43 -0700719 if (!blendFramebuffer) {
720 shader.append(gFS_Main_FragColor);
721 } else {
Romain Guyf607bdc2010-09-10 19:20:06 -0700722 shader.append(!description.swapSrcDst ?
723 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap);
Romain Guya5aed0d2010-09-09 14:42:43 -0700724 }
Romain Guyff316ec2013-02-13 18:39:43 -0800725 if (description.hasColors) {
726 shader.append(gFS_Main_FragColor_HasColors);
727 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700728 if (description.hasRoundRectClip) {
729 shader.append(gFS_Main_FragColor_HasRoundRectClip);
730 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800731 if (description.hasDebugHighlight) {
732 shader.append(gFS_Main_DebugHighlight);
733 }
Romain Guyac670c02010-07-27 17:39:27 -0700734 }
735 // End the shader
736 shader.append(gFS_Footer);
737
Romain Guy636afc12017-02-07 11:21:05 -0800738//#if DEBUG_PROGRAMS
Romain Guydb1938e2010-08-02 18:50:22 -0700739 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
740 printLongString(shader);
Romain Guy636afc12017-02-07 11:21:05 -0800741//#endif
Romain Guydb1938e2010-08-02 18:50:22 -0700742
Romain Guyac670c02010-07-27 17:39:27 -0700743 return shader;
744}
745
Mike Reedc2f31df2016-10-28 17:21:45 -0400746void ProgramCache::generateBlend(String8& shader, const char* name, SkBlendMode mode) {
Romain Guyac670c02010-07-27 17:39:27 -0700747 shader.append("\nvec4 ");
748 shader.append(name);
749 shader.append("(vec4 src, vec4 dst) {\n");
750 shader.append(" ");
Mike Reedc2f31df2016-10-28 17:21:45 -0400751 shader.append(gBlendOps[(int)mode]);
Romain Guyac670c02010-07-27 17:39:27 -0700752 shader.append("}\n");
753}
754
Romain Guy889f8d12010-07-29 14:37:42 -0700755void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) {
Romain Guy63553472012-07-18 20:04:14 -0700756 shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700757 if (wrapS == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700758 shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700759 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n");
760 }
761 if (wrapT == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700762 shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700763 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n");
764 }
765 shader.append(" return vec2(");
766 switch (wrapS) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700767 case GL_CLAMP_TO_EDGE:
768 shader.append("texCoords.x");
769 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700770 case GL_REPEAT:
771 shader.append("mod(texCoords.x, 1.0)");
772 break;
773 case GL_MIRRORED_REPEAT:
774 shader.append("xMod2");
775 break;
776 }
777 shader.append(", ");
778 switch (wrapT) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700779 case GL_CLAMP_TO_EDGE:
780 shader.append("texCoords.y");
781 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700782 case GL_REPEAT:
783 shader.append("mod(texCoords.y, 1.0)");
784 break;
785 case GL_MIRRORED_REPEAT:
786 shader.append("yMod2");
787 break;
788 }
789 shader.append(");\n");
790 shader.append("}\n");
791}
792
Romain Guydb1938e2010-08-02 18:50:22 -0700793void ProgramCache::printLongString(const String8& shader) const {
794 ssize_t index = 0;
795 ssize_t lastIndex = 0;
796 const char* str = shader.string();
797 while ((index = shader.find("\n", index)) > -1) {
798 String8 line(str, index - lastIndex);
799 if (line.length() == 0) line.append("\n");
Chris Craik1c1c3fe2015-03-06 09:40:35 -0800800 ALOGD("%s", line.string());
Romain Guydb1938e2010-08-02 18:50:22 -0700801 index++;
802 str += (index - lastIndex);
803 lastIndex = index;
804 }
805}
806
Romain Guyac670c02010-07-27 17:39:27 -0700807}; // namespace uirenderer
808}; // namespace android