blob: 38c23e4babe8d9341994e07b5a4be82ff12d7812 [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 Guy0d86d7e2017-03-15 20:38:11 -0700193// The dithering pattern is generated with a triangle noise generator in the range [-1.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
Romain Guy6183c972017-03-16 12:24:55 -0700205 R"__SHADER__(
206 vec4 dither(const vec4 color) {
207 return color + (triangleNoise(gl_FragCoord.xy * screenSize.xy) / 255.0);
208 }
209 vec4 gradientMix(const vec4 a, const vec4 b, float v) {
210 vec4 c = mix(a, b, v);
211 return vec4(c.rgb * c.a, c.a);
212 }
213 )__SHADER__",
Romain Guy253f2c22016-09-28 17:34:42 -0700214 // sRGB framebuffer
Romain Guy6183c972017-03-16 12:24:55 -0700215 R"__SHADER__(
216 vec4 dither(const vec4 color) {
217 vec3 dithered = sqrt(color.rgb) + (triangleNoise(gl_FragCoord.xy * screenSize.xy) / 255.0);
218 return vec4(dithered * dithered, color.a);
219 }
220 vec4 gradientMixMix(const vec4 a, const vec4 b, float v) {
221 vec4 c = mix(a, b, v);
222 return vec4(c.rgb * c.a, c.a);
223 }
224 )__SHADER__",
Romain Guy253f2c22016-09-28 17:34:42 -0700225};
226
227// Uses luminance coefficients from Rec.709 to choose the appropriate gamma
228// The gamma() function assumes that bright text will be displayed on a dark
229// background and that dark text will be displayed on bright background
230// The gamma coefficient is chosen to thicken or thin the text accordingly
231// The dot product used to compute the luminance could be approximated with
232// a simple max(color.r, color.g, color.b)
Romain Guy9fe7e162017-02-03 16:16:07 -0800233const char* gFS_Gamma_Preamble = R"__SHADER__(
234 #define GAMMA (%.2f)
235 #define GAMMA_INV (%.2f)
236
237 float gamma(float a, const vec3 color) {
238 float luminance = dot(color, vec3(0.2126, 0.7152, 0.0722));
239 return pow(a, luminance < 0.5 ? GAMMA_INV : GAMMA);
240 }
241)__SHADER__";
Romain Guy253f2c22016-09-28 17:34:42 -0700242
Romain Guyac670c02010-07-27 17:39:27 -0700243const char* gFS_Main =
244 "\nvoid main(void) {\n"
Romain Guy253f2c22016-09-28 17:34:42 -0700245 " vec4 fragColor;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700246
Romain Guy253f2c22016-09-28 17:34:42 -0700247const char* gFS_Main_AddDither =
248 " fragColor = dither(fragColor);\n";
Romain Guy211efea2012-07-31 21:16:07 -0700249
Romain Guy707b2f72010-10-11 16:34:59 -0700250// General case
Romain Guyac670c02010-07-27 17:39:27 -0700251const char* gFS_Main_FetchColor =
252 " fragColor = color;\n";
Romain Guy740bf2b2011-04-26 15:33:10 -0700253const char* gFS_Main_ModulateColor =
254 " fragColor *= color.a;\n";
Chris Craik91a8c7c2014-08-12 14:31:35 -0700255const char* gFS_Main_ApplyVertexAlphaLinearInterp =
Chris Craik6ebdc112012-08-31 18:24:33 -0700256 " fragColor *= alpha;\n";
Chris Craik91a8c7c2014-08-12 14:31:35 -0700257const char* gFS_Main_ApplyVertexAlphaShadowInterp =
Chris Craik138c21f2016-04-28 16:59:42 -0700258 // map alpha through shadow alpha sampler
259 " fragColor *= texture2D(baseSampler, vec2(alpha, 0.5)).a;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700260const char* gFS_Main_FetchTexture[2] = {
261 // Don't modulate
Romain Guy636afc12017-02-07 11:21:05 -0800262 " fragColor = OETF(texture2D(baseSampler, outTexCoords));\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700263 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700264 " fragColor = color * texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700265};
Romain Guy253f2c22016-09-28 17:34:42 -0700266const char* gFS_Main_FetchA8Texture[4] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700267 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700268 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700269 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700270 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700271 " fragColor = color * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700272 " fragColor = color * gamma(texture2D(baseSampler, outTexCoords).a, color.rgb);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700273};
Romain Guy42e1e0d2012-07-30 14:47:51 -0700274const char* gFS_Main_FetchGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700275 // Linear
Romain Guy320d46b2012-08-08 16:05:42 -0700276 " vec4 gradientColor = texture2D(gradientSampler, linear);\n",
Romain Guy211efea2012-07-31 21:16:07 -0700277
Romain Guy6183c972017-03-16 12:24:55 -0700278 " vec4 gradientColor = gradientMix(startColor, endColor, clamp(linear, 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700279
Romain Guyee916f12010-09-20 17:53:08 -0700280 // Circular
Romain Guy320d46b2012-08-08 16:05:42 -0700281 " vec4 gradientColor = texture2D(gradientSampler, vec2(length(circular), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700282
Romain Guy6183c972017-03-16 12:24:55 -0700283 " vec4 gradientColor = gradientMix(startColor, endColor, clamp(length(circular), 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700284
Romain Guyee916f12010-09-20 17:53:08 -0700285 // Sweep
Romain Guy63553472012-07-18 20:04:14 -0700286 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700287 " vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700288
Romain Guy42e1e0d2012-07-30 14:47:51 -0700289 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy6183c972017-03-16 12:24:55 -0700290 " vec4 gradientColor = gradientMix(startColor, endColor, clamp(index - floor(index), 0.0, 1.0));\n"
Romain Guyee916f12010-09-20 17:53:08 -0700291};
Romain Guyac670c02010-07-27 17:39:27 -0700292const char* gFS_Main_FetchBitmap =
Romain Guy636afc12017-02-07 11:21:05 -0800293 " vec4 bitmapColor = OETF(texture2D(bitmapSampler, outBitmapTexCoords));\n";
Romain Guy889f8d12010-07-29 14:37:42 -0700294const char* gFS_Main_FetchBitmapNpot =
Romain Guy636afc12017-02-07 11:21:05 -0800295 " vec4 bitmapColor = OETF(texture2D(bitmapSampler, wrap(outBitmapTexCoords)));\n";
Romain Guyac670c02010-07-27 17:39:27 -0700296const char* gFS_Main_BlendShadersBG =
Romain Guyac670c02010-07-27 17:39:27 -0700297 " fragColor = blendShaders(gradientColor, bitmapColor)";
Romain Guy06f96e22010-07-30 19:18:16 -0700298const char* gFS_Main_BlendShadersGB =
299 " fragColor = blendShaders(bitmapColor, gradientColor)";
Romain Guy253f2c22016-09-28 17:34:42 -0700300const char* gFS_Main_BlendShaders_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700301 // Don't modulate
302 ";\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700303 ";\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700304 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700305 " * color.a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700306 " * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700307 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700308 " * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700309 " * gamma(texture2D(baseSampler, outTexCoords).a, color.rgb);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700310};
Romain Guy253f2c22016-09-28 17:34:42 -0700311const char* gFS_Main_GradientShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700312 // Don't modulate
313 " fragColor = gradientColor;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700314 " fragColor = gradientColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700315 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700316 " fragColor = gradientColor * color.a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700317 " fragColor = gradientColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700318 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700319 " fragColor = gradientColor * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700320 " fragColor = gradientColor * gamma(texture2D(baseSampler, outTexCoords).a, gradientColor.rgb);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700321 };
Romain Guy253f2c22016-09-28 17:34:42 -0700322const char* gFS_Main_BitmapShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700323 // Don't modulate
324 " fragColor = bitmapColor;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700325 " fragColor = bitmapColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700326 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700327 " fragColor = bitmapColor * color.a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700328 " fragColor = bitmapColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700329 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700330 " fragColor = bitmapColor * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700331 " fragColor = bitmapColor * gamma(texture2D(baseSampler, outTexCoords).a, bitmapColor.rgb);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700332 };
Romain Guyac670c02010-07-27 17:39:27 -0700333const char* gFS_Main_FragColor =
334 " gl_FragColor = fragColor;\n";
Romain Guyff316ec2013-02-13 18:39:43 -0800335const char* gFS_Main_FragColor_HasColors =
336 " gl_FragColor *= outColors;\n";
Romain Guya5aed0d2010-09-09 14:42:43 -0700337const char* gFS_Main_FragColor_Blend =
338 " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n";
Romain Guyf607bdc2010-09-10 19:20:06 -0700339const char* gFS_Main_FragColor_Blend_Swap =
340 " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n";
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500341const char* gFS_Main_ApplyColorOp[3] = {
Romain Guyac670c02010-07-27 17:39:27 -0700342 // None
343 "",
344 // Matrix
Chris Craik73821c82014-09-16 17:32:13 -0700345 " fragColor.rgb /= (fragColor.a + 0.0019);\n" // un-premultiply
Romain Guyac670c02010-07-27 17:39:27 -0700346 " fragColor *= colorMatrix;\n"
Chris Craik73821c82014-09-16 17:32:13 -0700347 " fragColor += colorMatrixVector;\n"
348 " fragColor.rgb *= (fragColor.a + 0.0019);\n", // re-premultiply
Romain Guyac670c02010-07-27 17:39:27 -0700349 // PorterDuff
350 " fragColor = blendColors(colorBlend, fragColor);\n"
351};
Chris Craikdeeda3d2014-05-05 19:09:33 -0700352
353// Note: LTRB -> xyzw
354const char* gFS_Main_FragColor_HasRoundRectClip =
355 " mediump vec2 fragToLT = roundRectInnerRectLTRB.xy - roundRectPos;\n"
356 " mediump vec2 fragFromRB = roundRectPos - roundRectInnerRectLTRB.zw;\n"
Chris Craikf99f3202014-08-05 15:31:52 -0700357
358 // divide + multiply by 128 to avoid falling out of range in length() function
359 " mediump vec2 dist = max(max(fragToLT, fragFromRB), vec2(0.0, 0.0)) / 128.0;\n"
360 " mediump float linearDist = roundRectRadius - (length(dist) * 128.0);\n"
Chris Craikdeeda3d2014-05-05 19:09:33 -0700361 " gl_FragColor *= clamp(linearDist, 0.0, 1.0);\n";
362
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800363const char* gFS_Main_DebugHighlight =
364 " gl_FragColor.rgb = vec3(0.0, gl_FragColor.a, 0.0);\n";
Romain Guyac670c02010-07-27 17:39:27 -0700365const char* gFS_Footer =
366 "}\n\n";
367
368///////////////////////////////////////////////////////////////////////////////
369// PorterDuff snippets
370///////////////////////////////////////////////////////////////////////////////
371
Romain Guy48daa542010-08-10 19:21:34 -0700372const char* gBlendOps[18] = {
Romain Guyac670c02010-07-27 17:39:27 -0700373 // Clear
374 "return vec4(0.0, 0.0, 0.0, 0.0);\n",
375 // Src
376 "return src;\n",
377 // Dst
378 "return dst;\n",
379 // SrcOver
Romain Guy06f96e22010-07-30 19:18:16 -0700380 "return src + dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700381 // DstOver
Romain Guy06f96e22010-07-30 19:18:16 -0700382 "return dst + src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700383 // SrcIn
Romain Guy06f96e22010-07-30 19:18:16 -0700384 "return src * dst.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700385 // DstIn
Romain Guy06f96e22010-07-30 19:18:16 -0700386 "return dst * src.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700387 // SrcOut
Romain Guy06f96e22010-07-30 19:18:16 -0700388 "return src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700389 // DstOut
Romain Guy06f96e22010-07-30 19:18:16 -0700390 "return dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700391 // SrcAtop
392 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n",
393 // DstAtop
394 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n",
395 // Xor
Romain Guy48daa542010-08-10 19:21:34 -0700396 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, "
Romain Guyac670c02010-07-27 17:39:27 -0700397 "src.a + dst.a - 2.0 * src.a * dst.a);\n",
Derek Sollenbergerc0bf7002015-03-06 13:48:27 -0500398 // Plus
Romain Guy48daa542010-08-10 19:21:34 -0700399 "return min(src + dst, 1.0);\n",
Derek Sollenbergerc0bf7002015-03-06 13:48:27 -0500400 // Modulate
Romain Guy48daa542010-08-10 19:21:34 -0700401 "return src * dst;\n",
402 // Screen
403 "return src + dst - src * dst;\n",
404 // Overlay
405 "return clamp(vec4(mix("
406 "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), "
407 "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), "
408 "step(dst.a, 2.0 * dst.rgb)), "
409 "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n",
410 // Darken
411 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
412 "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
413 // Lighten
414 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
415 "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 -0700416};
417
418///////////////////////////////////////////////////////////////////////////////
419// Constructors/destructors
420///////////////////////////////////////////////////////////////////////////////
421
Chris Craik117bdbc2015-02-05 10:12:38 -0800422ProgramCache::ProgramCache(Extensions& extensions)
Romain Guy253f2c22016-09-28 17:34:42 -0700423 : mHasES3(extensions.getMajorGlVersion() >= 3)
Romain Guyefb4b062017-02-27 11:00:04 -0800424 , mHasLinearBlending(extensions.hasLinearBlending()) {
Romain Guyac670c02010-07-27 17:39:27 -0700425}
426
427ProgramCache::~ProgramCache() {
428 clear();
429}
430
431///////////////////////////////////////////////////////////////////////////////
432// Cache management
433///////////////////////////////////////////////////////////////////////////////
434
435void ProgramCache::clear() {
Romain Guy67f27952010-12-07 20:09:23 -0800436 PROGRAM_LOGD("Clearing program cache");
Romain Guyac670c02010-07-27 17:39:27 -0700437 mCache.clear();
438}
439
440Program* ProgramCache::get(const ProgramDescription& description) {
441 programid key = description.key();
Chris Craik096b8d92013-03-01 11:08:11 -0800442 if (key == (PROGRAM_KEY_TEXTURE | PROGRAM_KEY_A8_TEXTURE)) {
443 // program for A8, unmodulated, texture w/o shader (black text/path textures) is equivalent
444 // to standard texture program (bitmaps, patches). Consider them equivalent.
445 key = PROGRAM_KEY_TEXTURE;
446 }
447
Chris Craik51d6a3d2014-12-22 17:16:56 -0800448 auto iter = mCache.find(key);
Chris Craikd41c4d82015-01-05 15:51:13 -0800449 Program* program = nullptr;
Chris Craik51d6a3d2014-12-22 17:16:56 -0800450 if (iter == mCache.end()) {
Romain Guyee916f12010-09-20 17:53:08 -0700451 description.log("Could not find program");
Romain Guyac670c02010-07-27 17:39:27 -0700452 program = generateProgram(description, key);
Chris Craik51d6a3d2014-12-22 17:16:56 -0800453 mCache[key] = std::unique_ptr<Program>(program);
Romain Guyac670c02010-07-27 17:39:27 -0700454 } else {
Chris Craik51d6a3d2014-12-22 17:16:56 -0800455 program = iter->second.get();
Romain Guyac670c02010-07-27 17:39:27 -0700456 }
457 return program;
458}
459
460///////////////////////////////////////////////////////////////////////////////
461// Program generation
462///////////////////////////////////////////////////////////////////////////////
463
Andreas Gampe64bb4132014-11-22 00:35:09 +0000464Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) {
Romain Guyac670c02010-07-27 17:39:27 -0700465 String8 vertexShader = generateVertexShader(description);
466 String8 fragmentShader = generateFragmentShader(description);
467
Romain Guy42e1e0d2012-07-30 14:47:51 -0700468 return new Program(description, vertexShader.string(), fragmentShader.string());
469}
470
471static inline size_t gradientIndex(const ProgramDescription& description) {
472 return description.gradientType * 2 + description.isSimpleGradient;
Romain Guyac670c02010-07-27 17:39:27 -0700473}
474
475String8 ProgramCache::generateVertexShader(const ProgramDescription& description) {
476 // Add attributes
Chris Craik8bd68c62015-08-19 15:29:05 -0700477 String8 shader(gVS_Header_Start);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700478 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700479 shader.append(gVS_Header_Attributes_TexCoords);
480 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700481 if (description.hasVertexAlpha) {
482 shader.append(gVS_Header_Attributes_VertexAlphaParameters);
Chet Haase5b0200b2011-04-13 17:58:08 -0700483 }
Romain Guyff316ec2013-02-13 18:39:43 -0800484 if (description.hasColors) {
485 shader.append(gVS_Header_Attributes_Colors);
486 }
Romain Guyac670c02010-07-27 17:39:27 -0700487 // Uniforms
488 shader.append(gVS_Header_Uniforms);
Romain Guy8f0095c2011-05-02 17:24:22 -0700489 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700490 shader.append(gVS_Header_Uniforms_TextureTransform);
491 }
Romain Guyac670c02010-07-27 17:39:27 -0700492 if (description.hasGradient) {
Romain Guyb4880042013-04-05 11:17:55 -0700493 shader.append(gVS_Header_Uniforms_HasGradient);
Romain Guyac670c02010-07-27 17:39:27 -0700494 }
Romain Guy889f8d12010-07-29 14:37:42 -0700495 if (description.hasBitmap) {
496 shader.append(gVS_Header_Uniforms_HasBitmap);
497 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700498 if (description.hasRoundRectClip) {
499 shader.append(gVS_Header_Uniforms_HasRoundRectClip);
500 }
Romain Guyac670c02010-07-27 17:39:27 -0700501 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700502 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700503 shader.append(gVS_Header_Varyings_HasTexture);
504 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700505 if (description.hasVertexAlpha) {
506 shader.append(gVS_Header_Varyings_HasVertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700507 }
Romain Guyff316ec2013-02-13 18:39:43 -0800508 if (description.hasColors) {
509 shader.append(gVS_Header_Varyings_HasColors);
510 }
Romain Guyac670c02010-07-27 17:39:27 -0700511 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700512 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700513 }
514 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700515 shader.append(gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700516 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700517 if (description.hasRoundRectClip) {
518 shader.append(gVS_Header_Varyings_HasRoundRectClip);
519 }
Romain Guyac670c02010-07-27 17:39:27 -0700520
521 // Begin the shader
522 shader.append(gVS_Main); {
Romain Guy8f0095c2011-05-02 17:24:22 -0700523 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700524 shader.append(gVS_Main_OutTransformedTexCoords);
Romain Guy8f0095c2011-05-02 17:24:22 -0700525 } else if (description.hasTexture || description.hasExternalTexture) {
526 shader.append(gVS_Main_OutTexCoords);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700527 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700528 if (description.hasVertexAlpha) {
529 shader.append(gVS_Main_VertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700530 }
Romain Guyff316ec2013-02-13 18:39:43 -0800531 if (description.hasColors) {
532 shader.append(gVS_Main_OutColors);
533 }
Romain Guy889f8d12010-07-29 14:37:42 -0700534 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700535 shader.append(gVS_Main_OutBitmapTexCoords);
Romain Guy889f8d12010-07-29 14:37:42 -0700536 }
Romain Guyac670c02010-07-27 17:39:27 -0700537 // Output transformed position
538 shader.append(gVS_Main_Position);
Chet Haasea1d12dd2012-09-21 14:50:14 -0700539 if (description.hasGradient) {
540 shader.append(gVS_Main_OutGradient[gradientIndex(description)]);
541 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700542 if (description.hasRoundRectClip) {
543 shader.append(gVS_Main_HasRoundRectClip);
544 }
Romain Guyac670c02010-07-27 17:39:27 -0700545 }
546 // End the shader
547 shader.append(gVS_Footer);
548
549 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string());
550
551 return shader;
552}
553
Romain Guya938f562012-09-13 20:31:08 -0700554static bool shaderOp(const ProgramDescription& description, String8& shader,
555 const int modulateOp, const char** snippets) {
556 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
Romain Guy253f2c22016-09-28 17:34:42 -0700557 op = op * 2 + description.hasGammaCorrection;
Romain Guya938f562012-09-13 20:31:08 -0700558 shader.append(snippets[op]);
559 return description.hasAlpha8Texture;
560}
561
Romain Guyac670c02010-07-27 17:39:27 -0700562String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) {
Chris Craik8bd68c62015-08-19 15:29:05 -0700563 String8 shader(gFS_Header_Start);
Romain Guya5aed0d2010-09-09 14:42:43 -0700564
Mike Reedc2f31df2016-10-28 17:21:45 -0400565 const bool blendFramebuffer = description.framebufferMode >= SkBlendMode::kPlus;
Romain Guya5aed0d2010-09-09 14:42:43 -0700566 if (blendFramebuffer) {
567 shader.append(gFS_Header_Extension_FramebufferFetch);
568 }
sergeyv9c97e482016-12-12 16:14:11 -0800569 if (description.hasExternalTexture
570 || (description.hasBitmap && description.isShaderBitmapExternal)) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700571 shader.append(gFS_Header_Extension_ExternalTexture);
572 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700573
574 shader.append(gFS_Header);
Romain Guyac670c02010-07-27 17:39:27 -0700575
576 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700577 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700578 shader.append(gVS_Header_Varyings_HasTexture);
579 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700580 if (description.hasVertexAlpha) {
581 shader.append(gVS_Header_Varyings_HasVertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700582 }
Romain Guyff316ec2013-02-13 18:39:43 -0800583 if (description.hasColors) {
584 shader.append(gVS_Header_Varyings_HasColors);
585 }
Romain Guyac670c02010-07-27 17:39:27 -0700586 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700587 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700588 }
589 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700590 shader.append(gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700591 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700592 if (description.hasRoundRectClip) {
593 shader.append(gVS_Header_Varyings_HasRoundRectClip);
594 }
Romain Guyac670c02010-07-27 17:39:27 -0700595
Romain Guyac670c02010-07-27 17:39:27 -0700596 // Uniforms
Romain Guy707b2f72010-10-11 16:34:59 -0700597 int modulateOp = MODULATE_OP_NO_MODULATE;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700598 const bool singleColor = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700599 !description.hasGradient && !description.hasBitmap;
600
601 if (description.modulate || singleColor) {
602 shader.append(gFS_Uniforms_Color);
603 if (!singleColor) modulateOp = MODULATE_OP_MODULATE;
604 }
Chris Craik138c21f2016-04-28 16:59:42 -0700605 if (description.hasTexture || description.useShadowAlphaInterp) {
Romain Guyac670c02010-07-27 17:39:27 -0700606 shader.append(gFS_Uniforms_TextureSampler);
Romain Guy8f0095c2011-05-02 17:24:22 -0700607 } else if (description.hasExternalTexture) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700608 shader.append(gFS_Uniforms_ExternalTextureSampler);
609 }
Romain Guyac670c02010-07-27 17:39:27 -0700610 if (description.hasGradient) {
Romain Guy253f2c22016-09-28 17:34:42 -0700611 shader.append(gFS_Uniforms_GradientSampler[description.isSimpleGradient]);
Romain Guyac670c02010-07-27 17:39:27 -0700612 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700613 if (description.hasRoundRectClip) {
614 shader.append(gFS_Uniforms_HasRoundRectClip);
615 }
Romain Guy707b2f72010-10-11 16:34:59 -0700616
Romain Guy253f2c22016-09-28 17:34:42 -0700617 if (description.hasGammaCorrection) {
618 shader.appendFormat(gFS_Gamma_Preamble, Properties::textGamma, 1.0f / Properties::textGamma);
619 }
620
Romain Guyac670c02010-07-27 17:39:27 -0700621 if (description.hasBitmap) {
sergeyv9c97e482016-12-12 16:14:11 -0800622 if (description.isShaderBitmapExternal) {
623 shader.append(gFS_Uniforms_BitmapExternalSampler);
624 } else {
625 shader.append(gFS_Uniforms_BitmapSampler);
626 }
Romain Guyac670c02010-07-27 17:39:27 -0700627 }
Chris Craikb9ce116d2015-08-20 15:14:06 -0700628 shader.append(gFS_Uniforms_ColorOp[static_cast<int>(description.colorOp)]);
Romain Guyac670c02010-07-27 17:39:27 -0700629
630 // Generate required functions
631 if (description.hasGradient && description.hasBitmap) {
Romain Guy48daa542010-08-10 19:21:34 -0700632 generateBlend(shader, "blendShaders", description.shadersMode);
Romain Guyac670c02010-07-27 17:39:27 -0700633 }
Chris Craikb9ce116d2015-08-20 15:14:06 -0700634 if (description.colorOp == ProgramDescription::ColorFilterMode::Blend) {
Romain Guy48daa542010-08-10 19:21:34 -0700635 generateBlend(shader, "blendColors", description.colorMode);
Romain Guyac670c02010-07-27 17:39:27 -0700636 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700637 if (blendFramebuffer) {
638 generateBlend(shader, "blendFramebuffer", description.framebufferMode);
639 }
sergeyv554ffeb2016-11-15 18:01:21 -0800640 if (description.useShaderBasedWrap) {
Romain Guy889f8d12010-07-29 14:37:42 -0700641 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
642 }
Romain Guy636afc12017-02-07 11:21:05 -0800643 if (description.hasGradient || description.hasLinearTexture) {
644 shader.append(gFS_Transfer_Functions);
645 }
646 if (description.hasBitmap || ((description.hasTexture || description.hasExternalTexture) &&
647 !description.hasAlpha8Texture)) {
Romain Guyefb4b062017-02-27 11:00:04 -0800648 shader.append(gFS_OETF[description.hasLinearTexture && !mHasLinearBlending]);
Romain Guy636afc12017-02-07 11:21:05 -0800649 }
Romain Guy253f2c22016-09-28 17:34:42 -0700650 if (description.hasGradient) {
Romain Guy8762e332016-10-12 12:14:07 -0700651 shader.append(gFS_Gradient_Functions);
Romain Guyefb4b062017-02-27 11:00:04 -0800652 shader.append(gFS_Gradient_Preamble[mHasLinearBlending]);
Romain Guy253f2c22016-09-28 17:34:42 -0700653 }
Romain Guyac670c02010-07-27 17:39:27 -0700654
655 // Begin the shader
656 shader.append(gFS_Main); {
657 // Stores the result in fragColor directly
Romain Guyaa6c24c2011-04-28 18:40:04 -0700658 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700659 if (description.hasAlpha8Texture) {
Romain Guy707b2f72010-10-11 16:34:59 -0700660 if (!description.hasGradient && !description.hasBitmap) {
Romain Guy253f2c22016-09-28 17:34:42 -0700661 shader.append(
662 gFS_Main_FetchA8Texture[modulateOp * 2 + description.hasGammaCorrection]);
Romain Guy707b2f72010-10-11 16:34:59 -0700663 }
Romain Guyac670c02010-07-27 17:39:27 -0700664 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700665 shader.append(gFS_Main_FetchTexture[modulateOp]);
Romain Guyac670c02010-07-27 17:39:27 -0700666 }
667 } else {
Romain Guya938f562012-09-13 20:31:08 -0700668 if (!description.hasGradient && !description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700669 shader.append(gFS_Main_FetchColor);
670 }
Romain Guyac670c02010-07-27 17:39:27 -0700671 }
672 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700673 shader.append(gFS_Main_FetchGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700674 }
675 if (description.hasBitmap) {
sergeyv554ffeb2016-11-15 18:01:21 -0800676 if (!description.useShaderBasedWrap) {
Romain Guy889f8d12010-07-29 14:37:42 -0700677 shader.append(gFS_Main_FetchBitmap);
678 } else {
679 shader.append(gFS_Main_FetchBitmapNpot);
680 }
Romain Guyac670c02010-07-27 17:39:27 -0700681 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700682 bool applyModulate = false;
Romain Guyac670c02010-07-27 17:39:27 -0700683 // Case when we have two shaders set
684 if (description.hasGradient && description.hasBitmap) {
685 if (description.isBitmapFirst) {
686 shader.append(gFS_Main_BlendShadersBG);
687 } else {
688 shader.append(gFS_Main_BlendShadersGB);
689 }
Romain Guya938f562012-09-13 20:31:08 -0700690 applyModulate = shaderOp(description, shader, modulateOp,
691 gFS_Main_BlendShaders_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700692 } else {
693 if (description.hasGradient) {
Romain Guya938f562012-09-13 20:31:08 -0700694 applyModulate = shaderOp(description, shader, modulateOp,
695 gFS_Main_GradientShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700696 } else if (description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700697 applyModulate = shaderOp(description, shader, modulateOp,
698 gFS_Main_BitmapShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700699 }
Romain Guyac670c02010-07-27 17:39:27 -0700700 }
Romain Guya938f562012-09-13 20:31:08 -0700701
Romain Guy740bf2b2011-04-26 15:33:10 -0700702 if (description.modulate && applyModulate) {
Romain Guya938f562012-09-13 20:31:08 -0700703 shader.append(gFS_Main_ModulateColor);
Romain Guy740bf2b2011-04-26 15:33:10 -0700704 }
Romain Guya938f562012-09-13 20:31:08 -0700705
Romain Guyac670c02010-07-27 17:39:27 -0700706 // Apply the color op if needed
Chris Craikb9ce116d2015-08-20 15:14:06 -0700707 shader.append(gFS_Main_ApplyColorOp[static_cast<int>(description.colorOp)]);
Chris Craik9f44a132012-09-13 18:34:55 -0700708
Chris Craik91a8c7c2014-08-12 14:31:35 -0700709 if (description.hasVertexAlpha) {
710 if (description.useShadowAlphaInterp) {
711 shader.append(gFS_Main_ApplyVertexAlphaShadowInterp);
Chris Craikbf759452014-08-11 16:00:44 -0700712 } else {
Chris Craik91a8c7c2014-08-12 14:31:35 -0700713 shader.append(gFS_Main_ApplyVertexAlphaLinearInterp);
Chris Craikbf759452014-08-11 16:00:44 -0700714 }
Chris Craik9f44a132012-09-13 18:34:55 -0700715 }
716
Romain Guy253f2c22016-09-28 17:34:42 -0700717 if (description.hasGradient) {
718 shader.append(gFS_Main_AddDither);
719 }
720
Romain Guyac670c02010-07-27 17:39:27 -0700721 // Output the fragment
Romain Guya5aed0d2010-09-09 14:42:43 -0700722 if (!blendFramebuffer) {
723 shader.append(gFS_Main_FragColor);
724 } else {
Romain Guyf607bdc2010-09-10 19:20:06 -0700725 shader.append(!description.swapSrcDst ?
726 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap);
Romain Guya5aed0d2010-09-09 14:42:43 -0700727 }
Romain Guyff316ec2013-02-13 18:39:43 -0800728 if (description.hasColors) {
729 shader.append(gFS_Main_FragColor_HasColors);
730 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700731 if (description.hasRoundRectClip) {
732 shader.append(gFS_Main_FragColor_HasRoundRectClip);
733 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800734 if (description.hasDebugHighlight) {
735 shader.append(gFS_Main_DebugHighlight);
736 }
Romain Guyac670c02010-07-27 17:39:27 -0700737 }
738 // End the shader
739 shader.append(gFS_Footer);
740
Romain Guy91a8ec02017-02-08 07:45:11 -0800741#if DEBUG_PROGRAMS
Romain Guydb1938e2010-08-02 18:50:22 -0700742 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
743 printLongString(shader);
Romain Guy91a8ec02017-02-08 07:45:11 -0800744#endif
Romain Guydb1938e2010-08-02 18:50:22 -0700745
Romain Guyac670c02010-07-27 17:39:27 -0700746 return shader;
747}
748
Mike Reedc2f31df2016-10-28 17:21:45 -0400749void ProgramCache::generateBlend(String8& shader, const char* name, SkBlendMode mode) {
Romain Guyac670c02010-07-27 17:39:27 -0700750 shader.append("\nvec4 ");
751 shader.append(name);
752 shader.append("(vec4 src, vec4 dst) {\n");
753 shader.append(" ");
Mike Reedc2f31df2016-10-28 17:21:45 -0400754 shader.append(gBlendOps[(int)mode]);
Romain Guyac670c02010-07-27 17:39:27 -0700755 shader.append("}\n");
756}
757
Romain Guy889f8d12010-07-29 14:37:42 -0700758void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) {
Romain Guy63553472012-07-18 20:04:14 -0700759 shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700760 if (wrapS == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700761 shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700762 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n");
763 }
764 if (wrapT == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700765 shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700766 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n");
767 }
768 shader.append(" return vec2(");
769 switch (wrapS) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700770 case GL_CLAMP_TO_EDGE:
771 shader.append("texCoords.x");
772 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700773 case GL_REPEAT:
774 shader.append("mod(texCoords.x, 1.0)");
775 break;
776 case GL_MIRRORED_REPEAT:
777 shader.append("xMod2");
778 break;
779 }
780 shader.append(", ");
781 switch (wrapT) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700782 case GL_CLAMP_TO_EDGE:
783 shader.append("texCoords.y");
784 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700785 case GL_REPEAT:
786 shader.append("mod(texCoords.y, 1.0)");
787 break;
788 case GL_MIRRORED_REPEAT:
789 shader.append("yMod2");
790 break;
791 }
792 shader.append(");\n");
793 shader.append("}\n");
794}
795
Romain Guydb1938e2010-08-02 18:50:22 -0700796void ProgramCache::printLongString(const String8& shader) const {
797 ssize_t index = 0;
798 ssize_t lastIndex = 0;
799 const char* str = shader.string();
800 while ((index = shader.find("\n", index)) > -1) {
801 String8 line(str, index - lastIndex);
802 if (line.length() == 0) line.append("\n");
Chris Craik1c1c3fe2015-03-06 09:40:35 -0800803 ALOGD("%s", line.string());
Romain Guydb1938e2010-08-02 18:50:22 -0700804 index++;
805 str += (index - lastIndex);
806 lastIndex = index;
807 }
808}
809
Romain Guyac670c02010-07-27 17:39:27 -0700810}; // namespace uirenderer
811}; // namespace android