blob: a3819f76296d293d1831ded5f4e4d3e057d5b838 [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 Guy253f2c22016-09-28 17:34:42 -0700164// Dithering must be done in the quantization space
165// When we are writing to an sRGB framebuffer, we must do the following:
166// EOCF(OECF(color) + dither)
167// We approximate the transfer functions with gamma 2.0 to avoid branches and pow()
Romain Guy8762e332016-10-12 12:14:07 -0700168// The dithering pattern is generated with a triangle noise generator in the range [-0.0,1.0]
Romain Guy253f2c22016-09-28 17:34:42 -0700169// TODO: Handle linear fp16 render targets
Romain Guy8762e332016-10-12 12:14:07 -0700170const char* gFS_Gradient_Functions =
171 "\nfloat triangleNoise(const highp vec2 n) {\n"
Romain Guy253f2c22016-09-28 17:34:42 -0700172 " highp vec2 p = fract(n * vec2(5.3987, 5.4421));\n"
173 " p += dot(p.yx, p.xy + vec2(21.5351, 14.3137));\n"
174 " highp float xy = p.x * p.y;\n"
Romain Guy8762e332016-10-12 12:14:07 -0700175 " return fract(xy * 95.4307) + fract(xy * 75.04961) - 1.0;\n"
Romain Guy253f2c22016-09-28 17:34:42 -0700176 "}\n";
Romain Guy8762e332016-10-12 12:14:07 -0700177const char* gFS_Gradient_Preamble[2] = {
Romain Guy253f2c22016-09-28 17:34:42 -0700178 // Linear framebuffer
179 "\nvec4 dither(const vec4 color) {\n"
180 " return vec4(color.rgb + (triangleNoise(gl_FragCoord.xy * screenSize.xy) / 255.0), color.a);"
Romain Guy8762e332016-10-12 12:14:07 -0700181 "}\n"
182 "\nvec4 gammaMix(const vec4 a, const vec4 b, float v) {\n"
183 " return pow(mix(a, b, v), vec4(vec3(1.0 / 2.2), 1.0));"
Romain Guy253f2c22016-09-28 17:34:42 -0700184 "}\n",
185 // sRGB framebuffer
186 "\nvec4 dither(const vec4 color) {\n"
187 " vec3 dithered = sqrt(color.rgb) + (triangleNoise(gl_FragCoord.xy * screenSize.xy) / 255.0);\n"
188 " return vec4(dithered * dithered, color.a);\n"
189 "}\n"
Romain Guy8762e332016-10-12 12:14:07 -0700190 "\nvec4 gammaMix(const vec4 a, const vec4 b, float v) {\n"
191 " return mix(a, b, v);"
192 "}\n"
Romain Guy253f2c22016-09-28 17:34:42 -0700193};
194
195// Uses luminance coefficients from Rec.709 to choose the appropriate gamma
196// The gamma() function assumes that bright text will be displayed on a dark
197// background and that dark text will be displayed on bright background
198// The gamma coefficient is chosen to thicken or thin the text accordingly
199// The dot product used to compute the luminance could be approximated with
200// a simple max(color.r, color.g, color.b)
201const char* gFS_Gamma_Preamble =
202 "\n#define GAMMA (%.2f)\n"
203 "#define GAMMA_INV (%.2f)\n"
204 "\nfloat gamma(float a, const vec3 color) {\n"
205 " float luminance = dot(color, vec3(0.2126, 0.7152, 0.0722));\n"
206 " return pow(a, luminance < 0.5 ? GAMMA_INV : GAMMA);\n"
207 "}\n";
208
Romain Guyac670c02010-07-27 17:39:27 -0700209const char* gFS_Main =
210 "\nvoid main(void) {\n"
Romain Guy253f2c22016-09-28 17:34:42 -0700211 " vec4 fragColor;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700212
Romain Guy253f2c22016-09-28 17:34:42 -0700213const char* gFS_Main_AddDither =
214 " fragColor = dither(fragColor);\n";
Romain Guy211efea2012-07-31 21:16:07 -0700215
Romain Guy707b2f72010-10-11 16:34:59 -0700216// Fast cases
217const char* gFS_Fast_SingleColor =
218 "\nvoid main(void) {\n"
219 " gl_FragColor = color;\n"
220 "}\n\n";
221const char* gFS_Fast_SingleTexture =
222 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700223 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700224 "}\n\n";
225const char* gFS_Fast_SingleModulateTexture =
226 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700227 " gl_FragColor = color.a * texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700228 "}\n\n";
229const char* gFS_Fast_SingleA8Texture =
230 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700231 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700232 "}\n\n";
Romain Guy253f2c22016-09-28 17:34:42 -0700233const char* gFS_Fast_SingleA8Texture_ApplyGamma =
234 "\nvoid main(void) {\n"
235 " gl_FragColor = vec4(0.0, 0.0, 0.0, pow(texture2D(baseSampler, outTexCoords).a, GAMMA));\n"
236 "}\n\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700237const char* gFS_Fast_SingleModulateA8Texture =
238 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700239 " gl_FragColor = color * texture2D(baseSampler, outTexCoords).a;\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700240 "}\n\n";
Romain Guy253f2c22016-09-28 17:34:42 -0700241const char* gFS_Fast_SingleModulateA8Texture_ApplyGamma =
242 "\nvoid main(void) {\n"
243 " gl_FragColor = color * gamma(texture2D(baseSampler, outTexCoords).a, color.rgb);\n"
244 "}\n\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -0700245const char* gFS_Fast_SingleGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700246 "\nvoid main(void) {\n"
Romain Guy253f2c22016-09-28 17:34:42 -0700247 " gl_FragColor = dither(texture2D(gradientSampler, linear));\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700248 "}\n\n",
249 "\nvoid main(void) {\n"
Romain Guy8762e332016-10-12 12:14:07 -0700250 " gl_FragColor = dither(gammaMix(startColor, endColor, clamp(linear, 0.0, 1.0)));\n"
Romain Guyb4880042013-04-05 11:17:55 -0700251 "}\n\n",
Romain Guy42e1e0d2012-07-30 14:47:51 -0700252};
253const char* gFS_Fast_SingleModulateGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700254 "\nvoid main(void) {\n"
Romain Guy253f2c22016-09-28 17:34:42 -0700255 " gl_FragColor = dither(color.a * texture2D(gradientSampler, linear));\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700256 "}\n\n",
257 "\nvoid main(void) {\n"
Romain Guy8762e332016-10-12 12:14:07 -0700258 " gl_FragColor = dither(color.a * gammaMix(startColor, endColor, clamp(linear, 0.0, 1.0)));\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700259 "}\n\n"
260};
Romain Guy707b2f72010-10-11 16:34:59 -0700261
262// General case
Romain Guyac670c02010-07-27 17:39:27 -0700263const char* gFS_Main_FetchColor =
264 " fragColor = color;\n";
Romain Guy740bf2b2011-04-26 15:33:10 -0700265const char* gFS_Main_ModulateColor =
266 " fragColor *= color.a;\n";
Chris Craik91a8c7c2014-08-12 14:31:35 -0700267const char* gFS_Main_ApplyVertexAlphaLinearInterp =
Chris Craik6ebdc112012-08-31 18:24:33 -0700268 " fragColor *= alpha;\n";
Chris Craik91a8c7c2014-08-12 14:31:35 -0700269const char* gFS_Main_ApplyVertexAlphaShadowInterp =
Chris Craik138c21f2016-04-28 16:59:42 -0700270 // map alpha through shadow alpha sampler
271 " fragColor *= texture2D(baseSampler, vec2(alpha, 0.5)).a;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700272const char* gFS_Main_FetchTexture[2] = {
273 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700274 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700275 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700276 " fragColor = color * texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700277};
Romain Guy253f2c22016-09-28 17:34:42 -0700278const char* gFS_Main_FetchA8Texture[4] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700279 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700280 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700281 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700282 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700283 " fragColor = color * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700284 " fragColor = color * gamma(texture2D(baseSampler, outTexCoords).a, color.rgb);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700285};
Romain Guy42e1e0d2012-07-30 14:47:51 -0700286const char* gFS_Main_FetchGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700287 // Linear
Romain Guy320d46b2012-08-08 16:05:42 -0700288 " vec4 gradientColor = texture2D(gradientSampler, linear);\n",
Romain Guy211efea2012-07-31 21:16:07 -0700289
Romain Guy8762e332016-10-12 12:14:07 -0700290 " vec4 gradientColor = gammaMix(startColor, endColor, clamp(linear, 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700291
Romain Guyee916f12010-09-20 17:53:08 -0700292 // Circular
Romain Guy320d46b2012-08-08 16:05:42 -0700293 " vec4 gradientColor = texture2D(gradientSampler, vec2(length(circular), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700294
Romain Guy8762e332016-10-12 12:14:07 -0700295 " vec4 gradientColor = gammaMix(startColor, endColor, clamp(length(circular), 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700296
Romain Guyee916f12010-09-20 17:53:08 -0700297 // Sweep
Romain Guy63553472012-07-18 20:04:14 -0700298 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700299 " vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700300
Romain Guy42e1e0d2012-07-30 14:47:51 -0700301 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy8762e332016-10-12 12:14:07 -0700302 " vec4 gradientColor = gammaMix(startColor, endColor, clamp(index - floor(index), 0.0, 1.0));\n"
Romain Guyee916f12010-09-20 17:53:08 -0700303};
Romain Guyac670c02010-07-27 17:39:27 -0700304const char* gFS_Main_FetchBitmap =
305 " vec4 bitmapColor = texture2D(bitmapSampler, outBitmapTexCoords);\n";
Romain Guy889f8d12010-07-29 14:37:42 -0700306const char* gFS_Main_FetchBitmapNpot =
307 " vec4 bitmapColor = texture2D(bitmapSampler, wrap(outBitmapTexCoords));\n";
Romain Guyac670c02010-07-27 17:39:27 -0700308const char* gFS_Main_BlendShadersBG =
Romain Guyac670c02010-07-27 17:39:27 -0700309 " fragColor = blendShaders(gradientColor, bitmapColor)";
Romain Guy06f96e22010-07-30 19:18:16 -0700310const char* gFS_Main_BlendShadersGB =
311 " fragColor = blendShaders(bitmapColor, gradientColor)";
Romain Guy253f2c22016-09-28 17:34:42 -0700312const char* gFS_Main_BlendShaders_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700313 // Don't modulate
314 ";\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700315 ";\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700316 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700317 " * color.a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700318 " * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700319 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700320 " * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700321 " * gamma(texture2D(baseSampler, outTexCoords).a, color.rgb);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700322};
Romain Guy253f2c22016-09-28 17:34:42 -0700323const char* gFS_Main_GradientShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700324 // Don't modulate
325 " fragColor = gradientColor;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700326 " fragColor = gradientColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700327 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700328 " fragColor = gradientColor * color.a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700329 " fragColor = gradientColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700330 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700331 " fragColor = gradientColor * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700332 " fragColor = gradientColor * gamma(texture2D(baseSampler, outTexCoords).a, gradientColor.rgb);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700333 };
Romain Guy253f2c22016-09-28 17:34:42 -0700334const char* gFS_Main_BitmapShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700335 // Don't modulate
336 " fragColor = bitmapColor;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700337 " fragColor = bitmapColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700338 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700339 " fragColor = bitmapColor * color.a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700340 " fragColor = bitmapColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700341 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700342 " fragColor = bitmapColor * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700343 " fragColor = bitmapColor * gamma(texture2D(baseSampler, outTexCoords).a, bitmapColor.rgb);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700344 };
Romain Guyac670c02010-07-27 17:39:27 -0700345const char* gFS_Main_FragColor =
346 " gl_FragColor = fragColor;\n";
Romain Guyff316ec2013-02-13 18:39:43 -0800347const char* gFS_Main_FragColor_HasColors =
348 " gl_FragColor *= outColors;\n";
Romain Guya5aed0d2010-09-09 14:42:43 -0700349const char* gFS_Main_FragColor_Blend =
350 " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n";
Romain Guyf607bdc2010-09-10 19:20:06 -0700351const char* gFS_Main_FragColor_Blend_Swap =
352 " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n";
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500353const char* gFS_Main_ApplyColorOp[3] = {
Romain Guyac670c02010-07-27 17:39:27 -0700354 // None
355 "",
356 // Matrix
Chris Craik73821c82014-09-16 17:32:13 -0700357 " fragColor.rgb /= (fragColor.a + 0.0019);\n" // un-premultiply
Romain Guyac670c02010-07-27 17:39:27 -0700358 " fragColor *= colorMatrix;\n"
Chris Craik73821c82014-09-16 17:32:13 -0700359 " fragColor += colorMatrixVector;\n"
360 " fragColor.rgb *= (fragColor.a + 0.0019);\n", // re-premultiply
Romain Guyac670c02010-07-27 17:39:27 -0700361 // PorterDuff
362 " fragColor = blendColors(colorBlend, fragColor);\n"
363};
Chris Craikdeeda3d2014-05-05 19:09:33 -0700364
365// Note: LTRB -> xyzw
366const char* gFS_Main_FragColor_HasRoundRectClip =
367 " mediump vec2 fragToLT = roundRectInnerRectLTRB.xy - roundRectPos;\n"
368 " mediump vec2 fragFromRB = roundRectPos - roundRectInnerRectLTRB.zw;\n"
Chris Craikf99f3202014-08-05 15:31:52 -0700369
370 // divide + multiply by 128 to avoid falling out of range in length() function
371 " mediump vec2 dist = max(max(fragToLT, fragFromRB), vec2(0.0, 0.0)) / 128.0;\n"
372 " mediump float linearDist = roundRectRadius - (length(dist) * 128.0);\n"
Chris Craikdeeda3d2014-05-05 19:09:33 -0700373 " gl_FragColor *= clamp(linearDist, 0.0, 1.0);\n";
374
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800375const char* gFS_Main_DebugHighlight =
376 " gl_FragColor.rgb = vec3(0.0, gl_FragColor.a, 0.0);\n";
Romain Guyac670c02010-07-27 17:39:27 -0700377const char* gFS_Footer =
378 "}\n\n";
379
380///////////////////////////////////////////////////////////////////////////////
381// PorterDuff snippets
382///////////////////////////////////////////////////////////////////////////////
383
Romain Guy48daa542010-08-10 19:21:34 -0700384const char* gBlendOps[18] = {
Romain Guyac670c02010-07-27 17:39:27 -0700385 // Clear
386 "return vec4(0.0, 0.0, 0.0, 0.0);\n",
387 // Src
388 "return src;\n",
389 // Dst
390 "return dst;\n",
391 // SrcOver
Romain Guy06f96e22010-07-30 19:18:16 -0700392 "return src + dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700393 // DstOver
Romain Guy06f96e22010-07-30 19:18:16 -0700394 "return dst + src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700395 // SrcIn
Romain Guy06f96e22010-07-30 19:18:16 -0700396 "return src * dst.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700397 // DstIn
Romain Guy06f96e22010-07-30 19:18:16 -0700398 "return dst * src.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700399 // SrcOut
Romain Guy06f96e22010-07-30 19:18:16 -0700400 "return src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700401 // DstOut
Romain Guy06f96e22010-07-30 19:18:16 -0700402 "return dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700403 // SrcAtop
404 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n",
405 // DstAtop
406 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n",
407 // Xor
Romain Guy48daa542010-08-10 19:21:34 -0700408 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, "
Romain Guyac670c02010-07-27 17:39:27 -0700409 "src.a + dst.a - 2.0 * src.a * dst.a);\n",
Derek Sollenbergerc0bf7002015-03-06 13:48:27 -0500410 // Plus
Romain Guy48daa542010-08-10 19:21:34 -0700411 "return min(src + dst, 1.0);\n",
Derek Sollenbergerc0bf7002015-03-06 13:48:27 -0500412 // Modulate
Romain Guy48daa542010-08-10 19:21:34 -0700413 "return src * dst;\n",
414 // Screen
415 "return src + dst - src * dst;\n",
416 // Overlay
417 "return clamp(vec4(mix("
418 "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), "
419 "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), "
420 "step(dst.a, 2.0 * dst.rgb)), "
421 "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n",
422 // Darken
423 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
424 "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
425 // Lighten
426 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
427 "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 -0700428};
429
430///////////////////////////////////////////////////////////////////////////////
431// Constructors/destructors
432///////////////////////////////////////////////////////////////////////////////
433
Chris Craik117bdbc2015-02-05 10:12:38 -0800434ProgramCache::ProgramCache(Extensions& extensions)
Romain Guy253f2c22016-09-28 17:34:42 -0700435 : mHasES3(extensions.getMajorGlVersion() >= 3)
436 , mHasSRGB(extensions.hasSRGB()) {
Romain Guyac670c02010-07-27 17:39:27 -0700437}
438
439ProgramCache::~ProgramCache() {
440 clear();
441}
442
443///////////////////////////////////////////////////////////////////////////////
444// Cache management
445///////////////////////////////////////////////////////////////////////////////
446
447void ProgramCache::clear() {
Romain Guy67f27952010-12-07 20:09:23 -0800448 PROGRAM_LOGD("Clearing program cache");
Romain Guyac670c02010-07-27 17:39:27 -0700449 mCache.clear();
450}
451
452Program* ProgramCache::get(const ProgramDescription& description) {
453 programid key = description.key();
Chris Craik096b8d92013-03-01 11:08:11 -0800454 if (key == (PROGRAM_KEY_TEXTURE | PROGRAM_KEY_A8_TEXTURE)) {
455 // program for A8, unmodulated, texture w/o shader (black text/path textures) is equivalent
456 // to standard texture program (bitmaps, patches). Consider them equivalent.
457 key = PROGRAM_KEY_TEXTURE;
458 }
459
Chris Craik51d6a3d2014-12-22 17:16:56 -0800460 auto iter = mCache.find(key);
Chris Craikd41c4d82015-01-05 15:51:13 -0800461 Program* program = nullptr;
Chris Craik51d6a3d2014-12-22 17:16:56 -0800462 if (iter == mCache.end()) {
Romain Guyee916f12010-09-20 17:53:08 -0700463 description.log("Could not find program");
Romain Guyac670c02010-07-27 17:39:27 -0700464 program = generateProgram(description, key);
Chris Craik51d6a3d2014-12-22 17:16:56 -0800465 mCache[key] = std::unique_ptr<Program>(program);
Romain Guyac670c02010-07-27 17:39:27 -0700466 } else {
Chris Craik51d6a3d2014-12-22 17:16:56 -0800467 program = iter->second.get();
Romain Guyac670c02010-07-27 17:39:27 -0700468 }
469 return program;
470}
471
472///////////////////////////////////////////////////////////////////////////////
473// Program generation
474///////////////////////////////////////////////////////////////////////////////
475
Andreas Gampe64bb4132014-11-22 00:35:09 +0000476Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) {
Romain Guyac670c02010-07-27 17:39:27 -0700477 String8 vertexShader = generateVertexShader(description);
478 String8 fragmentShader = generateFragmentShader(description);
479
Romain Guy42e1e0d2012-07-30 14:47:51 -0700480 return new Program(description, vertexShader.string(), fragmentShader.string());
481}
482
483static inline size_t gradientIndex(const ProgramDescription& description) {
484 return description.gradientType * 2 + description.isSimpleGradient;
Romain Guyac670c02010-07-27 17:39:27 -0700485}
486
487String8 ProgramCache::generateVertexShader(const ProgramDescription& description) {
488 // Add attributes
Chris Craik8bd68c62015-08-19 15:29:05 -0700489 String8 shader(gVS_Header_Start);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700490 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700491 shader.append(gVS_Header_Attributes_TexCoords);
492 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700493 if (description.hasVertexAlpha) {
494 shader.append(gVS_Header_Attributes_VertexAlphaParameters);
Chet Haase5b0200b2011-04-13 17:58:08 -0700495 }
Romain Guyff316ec2013-02-13 18:39:43 -0800496 if (description.hasColors) {
497 shader.append(gVS_Header_Attributes_Colors);
498 }
Romain Guyac670c02010-07-27 17:39:27 -0700499 // Uniforms
500 shader.append(gVS_Header_Uniforms);
Romain Guy8f0095c2011-05-02 17:24:22 -0700501 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700502 shader.append(gVS_Header_Uniforms_TextureTransform);
503 }
Romain Guyac670c02010-07-27 17:39:27 -0700504 if (description.hasGradient) {
Romain Guyb4880042013-04-05 11:17:55 -0700505 shader.append(gVS_Header_Uniforms_HasGradient);
Romain Guyac670c02010-07-27 17:39:27 -0700506 }
Romain Guy889f8d12010-07-29 14:37:42 -0700507 if (description.hasBitmap) {
508 shader.append(gVS_Header_Uniforms_HasBitmap);
509 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700510 if (description.hasRoundRectClip) {
511 shader.append(gVS_Header_Uniforms_HasRoundRectClip);
512 }
Romain Guyac670c02010-07-27 17:39:27 -0700513 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700514 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700515 shader.append(gVS_Header_Varyings_HasTexture);
516 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700517 if (description.hasVertexAlpha) {
518 shader.append(gVS_Header_Varyings_HasVertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700519 }
Romain Guyff316ec2013-02-13 18:39:43 -0800520 if (description.hasColors) {
521 shader.append(gVS_Header_Varyings_HasColors);
522 }
Romain Guyac670c02010-07-27 17:39:27 -0700523 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700524 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700525 }
526 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700527 shader.append(gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700528 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700529 if (description.hasRoundRectClip) {
530 shader.append(gVS_Header_Varyings_HasRoundRectClip);
531 }
Romain Guyac670c02010-07-27 17:39:27 -0700532
533 // Begin the shader
534 shader.append(gVS_Main); {
Romain Guy8f0095c2011-05-02 17:24:22 -0700535 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700536 shader.append(gVS_Main_OutTransformedTexCoords);
Romain Guy8f0095c2011-05-02 17:24:22 -0700537 } else if (description.hasTexture || description.hasExternalTexture) {
538 shader.append(gVS_Main_OutTexCoords);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700539 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700540 if (description.hasVertexAlpha) {
541 shader.append(gVS_Main_VertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700542 }
Romain Guyff316ec2013-02-13 18:39:43 -0800543 if (description.hasColors) {
544 shader.append(gVS_Main_OutColors);
545 }
Romain Guy889f8d12010-07-29 14:37:42 -0700546 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700547 shader.append(gVS_Main_OutBitmapTexCoords);
Romain Guy889f8d12010-07-29 14:37:42 -0700548 }
Romain Guyac670c02010-07-27 17:39:27 -0700549 // Output transformed position
550 shader.append(gVS_Main_Position);
Chet Haasea1d12dd2012-09-21 14:50:14 -0700551 if (description.hasGradient) {
552 shader.append(gVS_Main_OutGradient[gradientIndex(description)]);
553 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700554 if (description.hasRoundRectClip) {
555 shader.append(gVS_Main_HasRoundRectClip);
556 }
Romain Guyac670c02010-07-27 17:39:27 -0700557 }
558 // End the shader
559 shader.append(gVS_Footer);
560
561 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string());
562
563 return shader;
564}
565
Romain Guya938f562012-09-13 20:31:08 -0700566static bool shaderOp(const ProgramDescription& description, String8& shader,
567 const int modulateOp, const char** snippets) {
568 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
Romain Guy253f2c22016-09-28 17:34:42 -0700569 op = op * 2 + description.hasGammaCorrection;
Romain Guya938f562012-09-13 20:31:08 -0700570 shader.append(snippets[op]);
571 return description.hasAlpha8Texture;
572}
573
Romain Guyac670c02010-07-27 17:39:27 -0700574String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) {
Chris Craik8bd68c62015-08-19 15:29:05 -0700575 String8 shader(gFS_Header_Start);
Romain Guya5aed0d2010-09-09 14:42:43 -0700576
Mike Reedc2f31df2016-10-28 17:21:45 -0400577 const bool blendFramebuffer = description.framebufferMode >= SkBlendMode::kPlus;
Romain Guya5aed0d2010-09-09 14:42:43 -0700578 if (blendFramebuffer) {
579 shader.append(gFS_Header_Extension_FramebufferFetch);
580 }
sergeyv9c97e482016-12-12 16:14:11 -0800581 if (description.hasExternalTexture
582 || (description.hasBitmap && description.isShaderBitmapExternal)) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700583 shader.append(gFS_Header_Extension_ExternalTexture);
584 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700585
586 shader.append(gFS_Header);
Romain Guyac670c02010-07-27 17:39:27 -0700587
588 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700589 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700590 shader.append(gVS_Header_Varyings_HasTexture);
591 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700592 if (description.hasVertexAlpha) {
593 shader.append(gVS_Header_Varyings_HasVertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700594 }
Romain Guyff316ec2013-02-13 18:39:43 -0800595 if (description.hasColors) {
596 shader.append(gVS_Header_Varyings_HasColors);
597 }
Romain Guyac670c02010-07-27 17:39:27 -0700598 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700599 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700600 }
601 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700602 shader.append(gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700603 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700604 if (description.hasRoundRectClip) {
605 shader.append(gVS_Header_Varyings_HasRoundRectClip);
606 }
Romain Guyac670c02010-07-27 17:39:27 -0700607
Romain Guyac670c02010-07-27 17:39:27 -0700608 // Uniforms
Romain Guy707b2f72010-10-11 16:34:59 -0700609 int modulateOp = MODULATE_OP_NO_MODULATE;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700610 const bool singleColor = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700611 !description.hasGradient && !description.hasBitmap;
612
613 if (description.modulate || singleColor) {
614 shader.append(gFS_Uniforms_Color);
615 if (!singleColor) modulateOp = MODULATE_OP_MODULATE;
616 }
Chris Craik138c21f2016-04-28 16:59:42 -0700617 if (description.hasTexture || description.useShadowAlphaInterp) {
Romain Guyac670c02010-07-27 17:39:27 -0700618 shader.append(gFS_Uniforms_TextureSampler);
Romain Guy8f0095c2011-05-02 17:24:22 -0700619 } else if (description.hasExternalTexture) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700620 shader.append(gFS_Uniforms_ExternalTextureSampler);
621 }
Romain Guyac670c02010-07-27 17:39:27 -0700622 if (description.hasGradient) {
Romain Guy253f2c22016-09-28 17:34:42 -0700623 shader.append(gFS_Uniforms_GradientSampler[description.isSimpleGradient]);
Romain Guyac670c02010-07-27 17:39:27 -0700624 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700625 if (description.hasRoundRectClip) {
626 shader.append(gFS_Uniforms_HasRoundRectClip);
627 }
Romain Guy707b2f72010-10-11 16:34:59 -0700628
Romain Guy253f2c22016-09-28 17:34:42 -0700629 if (description.hasGammaCorrection) {
630 shader.appendFormat(gFS_Gamma_Preamble, Properties::textGamma, 1.0f / Properties::textGamma);
631 }
632
Romain Guy707b2f72010-10-11 16:34:59 -0700633 // Optimization for common cases
Chris Craik91a8c7c2014-08-12 14:31:35 -0700634 if (!description.hasVertexAlpha
Chris Craikdeeda3d2014-05-05 19:09:33 -0700635 && !blendFramebuffer
636 && !description.hasColors
Chris Craikb9ce116d2015-08-20 15:14:06 -0700637 && description.colorOp == ProgramDescription::ColorFilterMode::None
Chris Craikdeeda3d2014-05-05 19:09:33 -0700638 && !description.hasDebugHighlight
Chris Craikdeeda3d2014-05-05 19:09:33 -0700639 && !description.hasRoundRectClip) {
Romain Guy707b2f72010-10-11 16:34:59 -0700640 bool fast = false;
641
642 const bool noShader = !description.hasGradient && !description.hasBitmap;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700643 const bool singleTexture = (description.hasTexture || description.hasExternalTexture) &&
Romain Guy707b2f72010-10-11 16:34:59 -0700644 !description.hasAlpha8Texture && noShader;
645 const bool singleA8Texture = description.hasTexture &&
646 description.hasAlpha8Texture && noShader;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700647 const bool singleGradient = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700648 description.hasGradient && !description.hasBitmap &&
649 description.gradientType == ProgramDescription::kGradientLinear;
650
651 if (singleColor) {
652 shader.append(gFS_Fast_SingleColor);
653 fast = true;
654 } else if (singleTexture) {
655 if (!description.modulate) {
656 shader.append(gFS_Fast_SingleTexture);
657 } else {
658 shader.append(gFS_Fast_SingleModulateTexture);
659 }
660 fast = true;
661 } else if (singleA8Texture) {
662 if (!description.modulate) {
Romain Guy253f2c22016-09-28 17:34:42 -0700663 if (description.hasGammaCorrection) {
664 shader.append(gFS_Fast_SingleA8Texture_ApplyGamma);
665 } else {
666 shader.append(gFS_Fast_SingleA8Texture);
667 }
Romain Guy707b2f72010-10-11 16:34:59 -0700668 } else {
Romain Guy253f2c22016-09-28 17:34:42 -0700669 if (description.hasGammaCorrection) {
670 shader.append(gFS_Fast_SingleModulateA8Texture_ApplyGamma);
671 } else {
672 shader.append(gFS_Fast_SingleModulateA8Texture);
673 }
Romain Guy707b2f72010-10-11 16:34:59 -0700674 }
675 fast = true;
676 } else if (singleGradient) {
Romain Guy8762e332016-10-12 12:14:07 -0700677 shader.append(gFS_Gradient_Functions);
678 shader.append(gFS_Gradient_Preamble[mHasSRGB]);
Romain Guy707b2f72010-10-11 16:34:59 -0700679 if (!description.modulate) {
Romain Guy253f2c22016-09-28 17:34:42 -0700680 shader.append(gFS_Fast_SingleGradient[description.isSimpleGradient]);
Romain Guy707b2f72010-10-11 16:34:59 -0700681 } else {
Romain Guy253f2c22016-09-28 17:34:42 -0700682 shader.append(gFS_Fast_SingleModulateGradient[description.isSimpleGradient]);
Romain Guy707b2f72010-10-11 16:34:59 -0700683 }
684 fast = true;
685 }
686
687 if (fast) {
Romain Guyc15008e2010-11-10 11:59:15 -0800688#if DEBUG_PROGRAMS
Romain Guy707b2f72010-10-11 16:34:59 -0700689 PROGRAM_LOGD("*** Fast case:\n");
690 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
691 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800692#endif
Romain Guy707b2f72010-10-11 16:34:59 -0700693
694 return shader;
695 }
696 }
697
Romain Guyac670c02010-07-27 17:39:27 -0700698 if (description.hasBitmap) {
sergeyv9c97e482016-12-12 16:14:11 -0800699 if (description.isShaderBitmapExternal) {
700 shader.append(gFS_Uniforms_BitmapExternalSampler);
701 } else {
702 shader.append(gFS_Uniforms_BitmapSampler);
703 }
Romain Guyac670c02010-07-27 17:39:27 -0700704 }
Chris Craikb9ce116d2015-08-20 15:14:06 -0700705 shader.append(gFS_Uniforms_ColorOp[static_cast<int>(description.colorOp)]);
Romain Guyac670c02010-07-27 17:39:27 -0700706
707 // Generate required functions
708 if (description.hasGradient && description.hasBitmap) {
Romain Guy48daa542010-08-10 19:21:34 -0700709 generateBlend(shader, "blendShaders", description.shadersMode);
Romain Guyac670c02010-07-27 17:39:27 -0700710 }
Chris Craikb9ce116d2015-08-20 15:14:06 -0700711 if (description.colorOp == ProgramDescription::ColorFilterMode::Blend) {
Romain Guy48daa542010-08-10 19:21:34 -0700712 generateBlend(shader, "blendColors", description.colorMode);
Romain Guyac670c02010-07-27 17:39:27 -0700713 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700714 if (blendFramebuffer) {
715 generateBlend(shader, "blendFramebuffer", description.framebufferMode);
716 }
sergeyv554ffeb2016-11-15 18:01:21 -0800717 if (description.useShaderBasedWrap) {
Romain Guy889f8d12010-07-29 14:37:42 -0700718 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
719 }
Romain Guy253f2c22016-09-28 17:34:42 -0700720 if (description.hasGradient) {
Romain Guy8762e332016-10-12 12:14:07 -0700721 shader.append(gFS_Gradient_Functions);
722 shader.append(gFS_Gradient_Preamble[mHasSRGB]);
Romain Guy253f2c22016-09-28 17:34:42 -0700723 }
Romain Guyac670c02010-07-27 17:39:27 -0700724
725 // Begin the shader
726 shader.append(gFS_Main); {
727 // Stores the result in fragColor directly
Romain Guyaa6c24c2011-04-28 18:40:04 -0700728 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700729 if (description.hasAlpha8Texture) {
Romain Guy707b2f72010-10-11 16:34:59 -0700730 if (!description.hasGradient && !description.hasBitmap) {
Romain Guy253f2c22016-09-28 17:34:42 -0700731 shader.append(
732 gFS_Main_FetchA8Texture[modulateOp * 2 + description.hasGammaCorrection]);
Romain Guy707b2f72010-10-11 16:34:59 -0700733 }
Romain Guyac670c02010-07-27 17:39:27 -0700734 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700735 shader.append(gFS_Main_FetchTexture[modulateOp]);
Romain Guyac670c02010-07-27 17:39:27 -0700736 }
737 } else {
Romain Guya938f562012-09-13 20:31:08 -0700738 if (!description.hasGradient && !description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700739 shader.append(gFS_Main_FetchColor);
740 }
Romain Guyac670c02010-07-27 17:39:27 -0700741 }
742 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700743 shader.append(gFS_Main_FetchGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700744 }
745 if (description.hasBitmap) {
sergeyv554ffeb2016-11-15 18:01:21 -0800746 if (!description.useShaderBasedWrap) {
Romain Guy889f8d12010-07-29 14:37:42 -0700747 shader.append(gFS_Main_FetchBitmap);
748 } else {
749 shader.append(gFS_Main_FetchBitmapNpot);
750 }
Romain Guyac670c02010-07-27 17:39:27 -0700751 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700752 bool applyModulate = false;
Romain Guyac670c02010-07-27 17:39:27 -0700753 // Case when we have two shaders set
754 if (description.hasGradient && description.hasBitmap) {
755 if (description.isBitmapFirst) {
756 shader.append(gFS_Main_BlendShadersBG);
757 } else {
758 shader.append(gFS_Main_BlendShadersGB);
759 }
Romain Guya938f562012-09-13 20:31:08 -0700760 applyModulate = shaderOp(description, shader, modulateOp,
761 gFS_Main_BlendShaders_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700762 } else {
763 if (description.hasGradient) {
Romain Guya938f562012-09-13 20:31:08 -0700764 applyModulate = shaderOp(description, shader, modulateOp,
765 gFS_Main_GradientShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700766 } else if (description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700767 applyModulate = shaderOp(description, shader, modulateOp,
768 gFS_Main_BitmapShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700769 }
Romain Guyac670c02010-07-27 17:39:27 -0700770 }
Romain Guya938f562012-09-13 20:31:08 -0700771
Romain Guy740bf2b2011-04-26 15:33:10 -0700772 if (description.modulate && applyModulate) {
Romain Guya938f562012-09-13 20:31:08 -0700773 shader.append(gFS_Main_ModulateColor);
Romain Guy740bf2b2011-04-26 15:33:10 -0700774 }
Romain Guya938f562012-09-13 20:31:08 -0700775
Romain Guyac670c02010-07-27 17:39:27 -0700776 // Apply the color op if needed
Chris Craikb9ce116d2015-08-20 15:14:06 -0700777 shader.append(gFS_Main_ApplyColorOp[static_cast<int>(description.colorOp)]);
Chris Craik9f44a132012-09-13 18:34:55 -0700778
Chris Craik91a8c7c2014-08-12 14:31:35 -0700779 if (description.hasVertexAlpha) {
780 if (description.useShadowAlphaInterp) {
781 shader.append(gFS_Main_ApplyVertexAlphaShadowInterp);
Chris Craikbf759452014-08-11 16:00:44 -0700782 } else {
Chris Craik91a8c7c2014-08-12 14:31:35 -0700783 shader.append(gFS_Main_ApplyVertexAlphaLinearInterp);
Chris Craikbf759452014-08-11 16:00:44 -0700784 }
Chris Craik9f44a132012-09-13 18:34:55 -0700785 }
786
Romain Guy253f2c22016-09-28 17:34:42 -0700787 if (description.hasGradient) {
788 shader.append(gFS_Main_AddDither);
789 }
790
Romain Guyac670c02010-07-27 17:39:27 -0700791 // Output the fragment
Romain Guya5aed0d2010-09-09 14:42:43 -0700792 if (!blendFramebuffer) {
793 shader.append(gFS_Main_FragColor);
794 } else {
Romain Guyf607bdc2010-09-10 19:20:06 -0700795 shader.append(!description.swapSrcDst ?
796 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap);
Romain Guya5aed0d2010-09-09 14:42:43 -0700797 }
Romain Guyff316ec2013-02-13 18:39:43 -0800798 if (description.hasColors) {
799 shader.append(gFS_Main_FragColor_HasColors);
800 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700801 if (description.hasRoundRectClip) {
802 shader.append(gFS_Main_FragColor_HasRoundRectClip);
803 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800804 if (description.hasDebugHighlight) {
805 shader.append(gFS_Main_DebugHighlight);
806 }
Romain Guyac670c02010-07-27 17:39:27 -0700807 }
808 // End the shader
809 shader.append(gFS_Footer);
810
Romain Guyc15008e2010-11-10 11:59:15 -0800811#if DEBUG_PROGRAMS
Romain Guydb1938e2010-08-02 18:50:22 -0700812 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
813 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800814#endif
Romain Guydb1938e2010-08-02 18:50:22 -0700815
Romain Guyac670c02010-07-27 17:39:27 -0700816 return shader;
817}
818
Mike Reedc2f31df2016-10-28 17:21:45 -0400819void ProgramCache::generateBlend(String8& shader, const char* name, SkBlendMode mode) {
Romain Guyac670c02010-07-27 17:39:27 -0700820 shader.append("\nvec4 ");
821 shader.append(name);
822 shader.append("(vec4 src, vec4 dst) {\n");
823 shader.append(" ");
Mike Reedc2f31df2016-10-28 17:21:45 -0400824 shader.append(gBlendOps[(int)mode]);
Romain Guyac670c02010-07-27 17:39:27 -0700825 shader.append("}\n");
826}
827
Romain Guy889f8d12010-07-29 14:37:42 -0700828void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) {
Romain Guy63553472012-07-18 20:04:14 -0700829 shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700830 if (wrapS == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700831 shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700832 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n");
833 }
834 if (wrapT == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700835 shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700836 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n");
837 }
838 shader.append(" return vec2(");
839 switch (wrapS) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700840 case GL_CLAMP_TO_EDGE:
841 shader.append("texCoords.x");
842 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700843 case GL_REPEAT:
844 shader.append("mod(texCoords.x, 1.0)");
845 break;
846 case GL_MIRRORED_REPEAT:
847 shader.append("xMod2");
848 break;
849 }
850 shader.append(", ");
851 switch (wrapT) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700852 case GL_CLAMP_TO_EDGE:
853 shader.append("texCoords.y");
854 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700855 case GL_REPEAT:
856 shader.append("mod(texCoords.y, 1.0)");
857 break;
858 case GL_MIRRORED_REPEAT:
859 shader.append("yMod2");
860 break;
861 }
862 shader.append(");\n");
863 shader.append("}\n");
864}
865
Romain Guydb1938e2010-08-02 18:50:22 -0700866void ProgramCache::printLongString(const String8& shader) const {
867 ssize_t index = 0;
868 ssize_t lastIndex = 0;
869 const char* str = shader.string();
870 while ((index = shader.find("\n", index)) > -1) {
871 String8 line(str, index - lastIndex);
872 if (line.length() == 0) line.append("\n");
Chris Craik1c1c3fe2015-03-06 09:40:35 -0800873 ALOGD("%s", line.string());
Romain Guydb1938e2010-08-02 18:50:22 -0700874 index++;
875 str += (index - lastIndex);
876 lastIndex = index;
877 }
878}
879
Romain Guyac670c02010-07-27 17:39:27 -0700880}; // namespace uirenderer
881}; // namespace android