blob: 315c60eccb3e9700db4c28d2a45a4dc7269496a1 [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";
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500148const char* gFS_Uniforms_ColorOp[3] = {
Romain Guyac670c02010-07-27 17:39:27 -0700149 // None
150 "",
151 // Matrix
152 "uniform mat4 colorMatrix;\n"
153 "uniform vec4 colorMatrixVector;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700154 // PorterDuff
Romain Guydb1938e2010-08-02 18:50:22 -0700155 "uniform vec4 colorBlend;\n"
Romain Guyac670c02010-07-27 17:39:27 -0700156};
Romain Guy41210632012-07-16 17:04:24 -0700157
Chris Craikdeeda3d2014-05-05 19:09:33 -0700158const char* gFS_Uniforms_HasRoundRectClip =
159 "uniform vec4 roundRectInnerRectLTRB;\n"
160 "uniform float roundRectRadius;\n";
161
Romain Guy253f2c22016-09-28 17:34:42 -0700162// Dithering must be done in the quantization space
163// When we are writing to an sRGB framebuffer, we must do the following:
164// EOCF(OECF(color) + dither)
165// We approximate the transfer functions with gamma 2.0 to avoid branches and pow()
166// The dithering pattern is generated with a triangle noise generator in the range [-0.5,1.5[
167// TODO: Handle linear fp16 render targets
168const char* gFS_Dither_Functions =
169 "\nmediump float triangleNoise(const highp vec2 n) {\n"
170 " highp vec2 p = fract(n * vec2(5.3987, 5.4421));\n"
171 " p += dot(p.yx, p.xy + vec2(21.5351, 14.3137));\n"
172 " highp float xy = p.x * p.y;\n"
173 " return fract(xy * 95.4307) + fract(xy * 75.04961) - 0.5;\n"
174 "}\n";
175const char* gFS_Dither_Preamble[2] = {
176 // Linear framebuffer
177 "\nvec4 dither(const vec4 color) {\n"
178 " return vec4(color.rgb + (triangleNoise(gl_FragCoord.xy * screenSize.xy) / 255.0), color.a);"
179 "}\n",
180 // sRGB framebuffer
181 "\nvec4 dither(const vec4 color) {\n"
182 " vec3 dithered = sqrt(color.rgb) + (triangleNoise(gl_FragCoord.xy * screenSize.xy) / 255.0);\n"
183 " return vec4(dithered * dithered, color.a);\n"
184 "}\n"
185};
186
187// Uses luminance coefficients from Rec.709 to choose the appropriate gamma
188// The gamma() function assumes that bright text will be displayed on a dark
189// background and that dark text will be displayed on bright background
190// The gamma coefficient is chosen to thicken or thin the text accordingly
191// The dot product used to compute the luminance could be approximated with
192// a simple max(color.r, color.g, color.b)
193const char* gFS_Gamma_Preamble =
194 "\n#define GAMMA (%.2f)\n"
195 "#define GAMMA_INV (%.2f)\n"
196 "\nfloat gamma(float a, const vec3 color) {\n"
197 " float luminance = dot(color, vec3(0.2126, 0.7152, 0.0722));\n"
198 " return pow(a, luminance < 0.5 ? GAMMA_INV : GAMMA);\n"
199 "}\n";
200
Romain Guyac670c02010-07-27 17:39:27 -0700201const char* gFS_Main =
202 "\nvoid main(void) {\n"
Romain Guy253f2c22016-09-28 17:34:42 -0700203 " vec4 fragColor;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700204
Romain Guy253f2c22016-09-28 17:34:42 -0700205const char* gFS_Main_AddDither =
206 " fragColor = dither(fragColor);\n";
Romain Guy211efea2012-07-31 21:16:07 -0700207
Romain Guy707b2f72010-10-11 16:34:59 -0700208// Fast cases
209const char* gFS_Fast_SingleColor =
210 "\nvoid main(void) {\n"
211 " gl_FragColor = color;\n"
212 "}\n\n";
213const char* gFS_Fast_SingleTexture =
214 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700215 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700216 "}\n\n";
217const char* gFS_Fast_SingleModulateTexture =
218 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700219 " gl_FragColor = color.a * texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700220 "}\n\n";
221const char* gFS_Fast_SingleA8Texture =
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";
Romain Guy253f2c22016-09-28 17:34:42 -0700225const char* gFS_Fast_SingleA8Texture_ApplyGamma =
226 "\nvoid main(void) {\n"
227 " gl_FragColor = vec4(0.0, 0.0, 0.0, pow(texture2D(baseSampler, outTexCoords).a, GAMMA));\n"
228 "}\n\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700229const char* gFS_Fast_SingleModulateA8Texture =
230 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700231 " gl_FragColor = color * texture2D(baseSampler, outTexCoords).a;\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700232 "}\n\n";
Romain Guy253f2c22016-09-28 17:34:42 -0700233const char* gFS_Fast_SingleModulateA8Texture_ApplyGamma =
234 "\nvoid main(void) {\n"
235 " gl_FragColor = color * gamma(texture2D(baseSampler, outTexCoords).a, color.rgb);\n"
236 "}\n\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -0700237const char* gFS_Fast_SingleGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700238 "\nvoid main(void) {\n"
Romain Guy253f2c22016-09-28 17:34:42 -0700239 " gl_FragColor = dither(texture2D(gradientSampler, linear));\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700240 "}\n\n",
241 "\nvoid main(void) {\n"
Romain Guy253f2c22016-09-28 17:34:42 -0700242 " gl_FragColor = dither(mix(startColor, endColor, clamp(linear, 0.0, 1.0)));\n"
Romain Guyb4880042013-04-05 11:17:55 -0700243 "}\n\n",
Romain Guy42e1e0d2012-07-30 14:47:51 -0700244};
245const char* gFS_Fast_SingleModulateGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700246 "\nvoid main(void) {\n"
Romain Guy253f2c22016-09-28 17:34:42 -0700247 " gl_FragColor = dither(color.a * texture2D(gradientSampler, linear));\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700248 "}\n\n",
249 "\nvoid main(void) {\n"
Romain Guy253f2c22016-09-28 17:34:42 -0700250 " gl_FragColor = dither(color.a * mix(startColor, endColor, clamp(linear, 0.0, 1.0)));\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700251 "}\n\n"
252};
Romain Guy707b2f72010-10-11 16:34:59 -0700253
254// General case
Romain Guyac670c02010-07-27 17:39:27 -0700255const char* gFS_Main_FetchColor =
256 " fragColor = color;\n";
Romain Guy740bf2b2011-04-26 15:33:10 -0700257const char* gFS_Main_ModulateColor =
258 " fragColor *= color.a;\n";
Chris Craik91a8c7c2014-08-12 14:31:35 -0700259const char* gFS_Main_ApplyVertexAlphaLinearInterp =
Chris Craik6ebdc112012-08-31 18:24:33 -0700260 " fragColor *= alpha;\n";
Chris Craik91a8c7c2014-08-12 14:31:35 -0700261const char* gFS_Main_ApplyVertexAlphaShadowInterp =
Chris Craik138c21f2016-04-28 16:59:42 -0700262 // map alpha through shadow alpha sampler
263 " fragColor *= texture2D(baseSampler, vec2(alpha, 0.5)).a;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700264const char* gFS_Main_FetchTexture[2] = {
265 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700266 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700267 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700268 " fragColor = color * texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700269};
Romain Guy253f2c22016-09-28 17:34:42 -0700270const char* gFS_Main_FetchA8Texture[4] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700271 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700272 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700273 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700274 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700275 " fragColor = color * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700276 " fragColor = color * gamma(texture2D(baseSampler, outTexCoords).a, color.rgb);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700277};
Romain Guy42e1e0d2012-07-30 14:47:51 -0700278const char* gFS_Main_FetchGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700279 // Linear
Romain Guy320d46b2012-08-08 16:05:42 -0700280 " vec4 gradientColor = texture2D(gradientSampler, linear);\n",
Romain Guy211efea2012-07-31 21:16:07 -0700281
Romain Guy320d46b2012-08-08 16:05:42 -0700282 " vec4 gradientColor = mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700283
Romain Guyee916f12010-09-20 17:53:08 -0700284 // Circular
Romain Guy320d46b2012-08-08 16:05:42 -0700285 " vec4 gradientColor = texture2D(gradientSampler, vec2(length(circular), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700286
Romain Guy320d46b2012-08-08 16:05:42 -0700287 " vec4 gradientColor = mix(startColor, endColor, clamp(length(circular), 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700288
Romain Guyee916f12010-09-20 17:53:08 -0700289 // Sweep
Romain Guy63553472012-07-18 20:04:14 -0700290 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700291 " vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700292
Romain Guy42e1e0d2012-07-30 14:47:51 -0700293 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700294 " vec4 gradientColor = mix(startColor, endColor, clamp(index - floor(index), 0.0, 1.0));\n"
Romain Guyee916f12010-09-20 17:53:08 -0700295};
Romain Guyac670c02010-07-27 17:39:27 -0700296const char* gFS_Main_FetchBitmap =
297 " vec4 bitmapColor = texture2D(bitmapSampler, outBitmapTexCoords);\n";
Romain Guy889f8d12010-07-29 14:37:42 -0700298const char* gFS_Main_FetchBitmapNpot =
299 " vec4 bitmapColor = texture2D(bitmapSampler, wrap(outBitmapTexCoords));\n";
Romain Guyac670c02010-07-27 17:39:27 -0700300const char* gFS_Main_BlendShadersBG =
Romain Guyac670c02010-07-27 17:39:27 -0700301 " fragColor = blendShaders(gradientColor, bitmapColor)";
Romain Guy06f96e22010-07-30 19:18:16 -0700302const char* gFS_Main_BlendShadersGB =
303 " fragColor = blendShaders(bitmapColor, gradientColor)";
Romain Guy253f2c22016-09-28 17:34:42 -0700304const char* gFS_Main_BlendShaders_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700305 // Don't modulate
306 ";\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700307 ";\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700308 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700309 " * color.a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700310 " * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700311 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700312 " * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700313 " * gamma(texture2D(baseSampler, outTexCoords).a, color.rgb);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700314};
Romain Guy253f2c22016-09-28 17:34:42 -0700315const char* gFS_Main_GradientShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700316 // Don't modulate
317 " fragColor = gradientColor;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700318 " fragColor = gradientColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700319 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700320 " fragColor = gradientColor * color.a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700321 " fragColor = gradientColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700322 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700323 " fragColor = gradientColor * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700324 " fragColor = gradientColor * gamma(texture2D(baseSampler, outTexCoords).a, gradientColor.rgb);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700325 };
Romain Guy253f2c22016-09-28 17:34:42 -0700326const char* gFS_Main_BitmapShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700327 // Don't modulate
328 " fragColor = bitmapColor;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700329 " fragColor = bitmapColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700330 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700331 " fragColor = bitmapColor * color.a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700332 " fragColor = bitmapColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700333 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700334 " fragColor = bitmapColor * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy253f2c22016-09-28 17:34:42 -0700335 " fragColor = bitmapColor * gamma(texture2D(baseSampler, outTexCoords).a, bitmapColor.rgb);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700336 };
Romain Guyac670c02010-07-27 17:39:27 -0700337const char* gFS_Main_FragColor =
338 " gl_FragColor = fragColor;\n";
Romain Guyff316ec2013-02-13 18:39:43 -0800339const char* gFS_Main_FragColor_HasColors =
340 " gl_FragColor *= outColors;\n";
Romain Guya5aed0d2010-09-09 14:42:43 -0700341const char* gFS_Main_FragColor_Blend =
342 " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n";
Romain Guyf607bdc2010-09-10 19:20:06 -0700343const char* gFS_Main_FragColor_Blend_Swap =
344 " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n";
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500345const char* gFS_Main_ApplyColorOp[3] = {
Romain Guyac670c02010-07-27 17:39:27 -0700346 // None
347 "",
348 // Matrix
Chris Craik73821c82014-09-16 17:32:13 -0700349 " fragColor.rgb /= (fragColor.a + 0.0019);\n" // un-premultiply
Romain Guyac670c02010-07-27 17:39:27 -0700350 " fragColor *= colorMatrix;\n"
Chris Craik73821c82014-09-16 17:32:13 -0700351 " fragColor += colorMatrixVector;\n"
352 " fragColor.rgb *= (fragColor.a + 0.0019);\n", // re-premultiply
Romain Guyac670c02010-07-27 17:39:27 -0700353 // PorterDuff
354 " fragColor = blendColors(colorBlend, fragColor);\n"
355};
Chris Craikdeeda3d2014-05-05 19:09:33 -0700356
357// Note: LTRB -> xyzw
358const char* gFS_Main_FragColor_HasRoundRectClip =
359 " mediump vec2 fragToLT = roundRectInnerRectLTRB.xy - roundRectPos;\n"
360 " mediump vec2 fragFromRB = roundRectPos - roundRectInnerRectLTRB.zw;\n"
Chris Craikf99f3202014-08-05 15:31:52 -0700361
362 // divide + multiply by 128 to avoid falling out of range in length() function
363 " mediump vec2 dist = max(max(fragToLT, fragFromRB), vec2(0.0, 0.0)) / 128.0;\n"
364 " mediump float linearDist = roundRectRadius - (length(dist) * 128.0);\n"
Chris Craikdeeda3d2014-05-05 19:09:33 -0700365 " gl_FragColor *= clamp(linearDist, 0.0, 1.0);\n";
366
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800367const char* gFS_Main_DebugHighlight =
368 " gl_FragColor.rgb = vec3(0.0, gl_FragColor.a, 0.0);\n";
Romain Guyac670c02010-07-27 17:39:27 -0700369const char* gFS_Footer =
370 "}\n\n";
371
372///////////////////////////////////////////////////////////////////////////////
373// PorterDuff snippets
374///////////////////////////////////////////////////////////////////////////////
375
Romain Guy48daa542010-08-10 19:21:34 -0700376const char* gBlendOps[18] = {
Romain Guyac670c02010-07-27 17:39:27 -0700377 // Clear
378 "return vec4(0.0, 0.0, 0.0, 0.0);\n",
379 // Src
380 "return src;\n",
381 // Dst
382 "return dst;\n",
383 // SrcOver
Romain Guy06f96e22010-07-30 19:18:16 -0700384 "return src + dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700385 // DstOver
Romain Guy06f96e22010-07-30 19:18:16 -0700386 "return dst + src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700387 // SrcIn
Romain Guy06f96e22010-07-30 19:18:16 -0700388 "return src * dst.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700389 // DstIn
Romain Guy06f96e22010-07-30 19:18:16 -0700390 "return dst * src.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700391 // SrcOut
Romain Guy06f96e22010-07-30 19:18:16 -0700392 "return src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700393 // DstOut
Romain Guy06f96e22010-07-30 19:18:16 -0700394 "return dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700395 // SrcAtop
396 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n",
397 // DstAtop
398 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n",
399 // Xor
Romain Guy48daa542010-08-10 19:21:34 -0700400 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, "
Romain Guyac670c02010-07-27 17:39:27 -0700401 "src.a + dst.a - 2.0 * src.a * dst.a);\n",
Derek Sollenbergerc0bf7002015-03-06 13:48:27 -0500402 // Plus
Romain Guy48daa542010-08-10 19:21:34 -0700403 "return min(src + dst, 1.0);\n",
Derek Sollenbergerc0bf7002015-03-06 13:48:27 -0500404 // Modulate
Romain Guy48daa542010-08-10 19:21:34 -0700405 "return src * dst;\n",
406 // Screen
407 "return src + dst - src * dst;\n",
408 // Overlay
409 "return clamp(vec4(mix("
410 "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), "
411 "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), "
412 "step(dst.a, 2.0 * dst.rgb)), "
413 "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n",
414 // Darken
415 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
416 "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
417 // Lighten
418 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
419 "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 -0700420};
421
422///////////////////////////////////////////////////////////////////////////////
423// Constructors/destructors
424///////////////////////////////////////////////////////////////////////////////
425
Chris Craik117bdbc2015-02-05 10:12:38 -0800426ProgramCache::ProgramCache(Extensions& extensions)
Romain Guy253f2c22016-09-28 17:34:42 -0700427 : mHasES3(extensions.getMajorGlVersion() >= 3)
428 , mHasSRGB(extensions.hasSRGB()) {
Romain Guyac670c02010-07-27 17:39:27 -0700429}
430
431ProgramCache::~ProgramCache() {
432 clear();
433}
434
435///////////////////////////////////////////////////////////////////////////////
436// Cache management
437///////////////////////////////////////////////////////////////////////////////
438
439void ProgramCache::clear() {
Romain Guy67f27952010-12-07 20:09:23 -0800440 PROGRAM_LOGD("Clearing program cache");
Romain Guyac670c02010-07-27 17:39:27 -0700441 mCache.clear();
442}
443
444Program* ProgramCache::get(const ProgramDescription& description) {
445 programid key = description.key();
Chris Craik096b8d92013-03-01 11:08:11 -0800446 if (key == (PROGRAM_KEY_TEXTURE | PROGRAM_KEY_A8_TEXTURE)) {
447 // program for A8, unmodulated, texture w/o shader (black text/path textures) is equivalent
448 // to standard texture program (bitmaps, patches). Consider them equivalent.
449 key = PROGRAM_KEY_TEXTURE;
450 }
451
Chris Craik51d6a3d2014-12-22 17:16:56 -0800452 auto iter = mCache.find(key);
Chris Craikd41c4d82015-01-05 15:51:13 -0800453 Program* program = nullptr;
Chris Craik51d6a3d2014-12-22 17:16:56 -0800454 if (iter == mCache.end()) {
Romain Guyee916f12010-09-20 17:53:08 -0700455 description.log("Could not find program");
Romain Guyac670c02010-07-27 17:39:27 -0700456 program = generateProgram(description, key);
Chris Craik51d6a3d2014-12-22 17:16:56 -0800457 mCache[key] = std::unique_ptr<Program>(program);
Romain Guyac670c02010-07-27 17:39:27 -0700458 } else {
Chris Craik51d6a3d2014-12-22 17:16:56 -0800459 program = iter->second.get();
Romain Guyac670c02010-07-27 17:39:27 -0700460 }
461 return program;
462}
463
464///////////////////////////////////////////////////////////////////////////////
465// Program generation
466///////////////////////////////////////////////////////////////////////////////
467
Andreas Gampe64bb4132014-11-22 00:35:09 +0000468Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) {
Romain Guyac670c02010-07-27 17:39:27 -0700469 String8 vertexShader = generateVertexShader(description);
470 String8 fragmentShader = generateFragmentShader(description);
471
Romain Guy42e1e0d2012-07-30 14:47:51 -0700472 return new Program(description, vertexShader.string(), fragmentShader.string());
473}
474
475static inline size_t gradientIndex(const ProgramDescription& description) {
476 return description.gradientType * 2 + description.isSimpleGradient;
Romain Guyac670c02010-07-27 17:39:27 -0700477}
478
479String8 ProgramCache::generateVertexShader(const ProgramDescription& description) {
480 // Add attributes
Chris Craik8bd68c62015-08-19 15:29:05 -0700481 String8 shader(gVS_Header_Start);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700482 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700483 shader.append(gVS_Header_Attributes_TexCoords);
484 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700485 if (description.hasVertexAlpha) {
486 shader.append(gVS_Header_Attributes_VertexAlphaParameters);
Chet Haase5b0200b2011-04-13 17:58:08 -0700487 }
Romain Guyff316ec2013-02-13 18:39:43 -0800488 if (description.hasColors) {
489 shader.append(gVS_Header_Attributes_Colors);
490 }
Romain Guyac670c02010-07-27 17:39:27 -0700491 // Uniforms
492 shader.append(gVS_Header_Uniforms);
Romain Guy8f0095c2011-05-02 17:24:22 -0700493 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700494 shader.append(gVS_Header_Uniforms_TextureTransform);
495 }
Romain Guyac670c02010-07-27 17:39:27 -0700496 if (description.hasGradient) {
Romain Guyb4880042013-04-05 11:17:55 -0700497 shader.append(gVS_Header_Uniforms_HasGradient);
Romain Guyac670c02010-07-27 17:39:27 -0700498 }
Romain Guy889f8d12010-07-29 14:37:42 -0700499 if (description.hasBitmap) {
500 shader.append(gVS_Header_Uniforms_HasBitmap);
501 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700502 if (description.hasRoundRectClip) {
503 shader.append(gVS_Header_Uniforms_HasRoundRectClip);
504 }
Romain Guyac670c02010-07-27 17:39:27 -0700505 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700506 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700507 shader.append(gVS_Header_Varyings_HasTexture);
508 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700509 if (description.hasVertexAlpha) {
510 shader.append(gVS_Header_Varyings_HasVertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700511 }
Romain Guyff316ec2013-02-13 18:39:43 -0800512 if (description.hasColors) {
513 shader.append(gVS_Header_Varyings_HasColors);
514 }
Romain Guyac670c02010-07-27 17:39:27 -0700515 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700516 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700517 }
518 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700519 shader.append(gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700520 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700521 if (description.hasRoundRectClip) {
522 shader.append(gVS_Header_Varyings_HasRoundRectClip);
523 }
Romain Guyac670c02010-07-27 17:39:27 -0700524
525 // Begin the shader
526 shader.append(gVS_Main); {
Romain Guy8f0095c2011-05-02 17:24:22 -0700527 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700528 shader.append(gVS_Main_OutTransformedTexCoords);
Romain Guy8f0095c2011-05-02 17:24:22 -0700529 } else if (description.hasTexture || description.hasExternalTexture) {
530 shader.append(gVS_Main_OutTexCoords);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700531 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700532 if (description.hasVertexAlpha) {
533 shader.append(gVS_Main_VertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700534 }
Romain Guyff316ec2013-02-13 18:39:43 -0800535 if (description.hasColors) {
536 shader.append(gVS_Main_OutColors);
537 }
Romain Guy889f8d12010-07-29 14:37:42 -0700538 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700539 shader.append(gVS_Main_OutBitmapTexCoords);
Romain Guy889f8d12010-07-29 14:37:42 -0700540 }
Romain Guyac670c02010-07-27 17:39:27 -0700541 // Output transformed position
542 shader.append(gVS_Main_Position);
Chet Haasea1d12dd2012-09-21 14:50:14 -0700543 if (description.hasGradient) {
544 shader.append(gVS_Main_OutGradient[gradientIndex(description)]);
545 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700546 if (description.hasRoundRectClip) {
547 shader.append(gVS_Main_HasRoundRectClip);
548 }
Romain Guyac670c02010-07-27 17:39:27 -0700549 }
550 // End the shader
551 shader.append(gVS_Footer);
552
553 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string());
554
555 return shader;
556}
557
Romain Guya938f562012-09-13 20:31:08 -0700558static bool shaderOp(const ProgramDescription& description, String8& shader,
559 const int modulateOp, const char** snippets) {
560 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
Romain Guy253f2c22016-09-28 17:34:42 -0700561 op = op * 2 + description.hasGammaCorrection;
Romain Guya938f562012-09-13 20:31:08 -0700562 shader.append(snippets[op]);
563 return description.hasAlpha8Texture;
564}
565
Romain Guyac670c02010-07-27 17:39:27 -0700566String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) {
Chris Craik8bd68c62015-08-19 15:29:05 -0700567 String8 shader(gFS_Header_Start);
Romain Guya5aed0d2010-09-09 14:42:43 -0700568
Romain Guy707b2f72010-10-11 16:34:59 -0700569 const bool blendFramebuffer = description.framebufferMode >= SkXfermode::kPlus_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700570 if (blendFramebuffer) {
571 shader.append(gFS_Header_Extension_FramebufferFetch);
572 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700573 if (description.hasExternalTexture) {
574 shader.append(gFS_Header_Extension_ExternalTexture);
575 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700576
577 shader.append(gFS_Header);
Romain Guyac670c02010-07-27 17:39:27 -0700578
579 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700580 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700581 shader.append(gVS_Header_Varyings_HasTexture);
582 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700583 if (description.hasVertexAlpha) {
584 shader.append(gVS_Header_Varyings_HasVertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700585 }
Romain Guyff316ec2013-02-13 18:39:43 -0800586 if (description.hasColors) {
587 shader.append(gVS_Header_Varyings_HasColors);
588 }
Romain Guyac670c02010-07-27 17:39:27 -0700589 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700590 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700591 }
592 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700593 shader.append(gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700594 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700595 if (description.hasRoundRectClip) {
596 shader.append(gVS_Header_Varyings_HasRoundRectClip);
597 }
Romain Guyac670c02010-07-27 17:39:27 -0700598
Romain Guyac670c02010-07-27 17:39:27 -0700599 // Uniforms
Romain Guy707b2f72010-10-11 16:34:59 -0700600 int modulateOp = MODULATE_OP_NO_MODULATE;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700601 const bool singleColor = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700602 !description.hasGradient && !description.hasBitmap;
603
604 if (description.modulate || singleColor) {
605 shader.append(gFS_Uniforms_Color);
606 if (!singleColor) modulateOp = MODULATE_OP_MODULATE;
607 }
Chris Craik138c21f2016-04-28 16:59:42 -0700608 if (description.hasTexture || description.useShadowAlphaInterp) {
Romain Guyac670c02010-07-27 17:39:27 -0700609 shader.append(gFS_Uniforms_TextureSampler);
Romain Guy8f0095c2011-05-02 17:24:22 -0700610 } else if (description.hasExternalTexture) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700611 shader.append(gFS_Uniforms_ExternalTextureSampler);
612 }
Romain Guyac670c02010-07-27 17:39:27 -0700613 if (description.hasGradient) {
Romain Guy253f2c22016-09-28 17:34:42 -0700614 shader.append(gFS_Uniforms_GradientSampler[description.isSimpleGradient]);
Romain Guyac670c02010-07-27 17:39:27 -0700615 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700616 if (description.hasRoundRectClip) {
617 shader.append(gFS_Uniforms_HasRoundRectClip);
618 }
Romain Guy707b2f72010-10-11 16:34:59 -0700619
Romain Guy253f2c22016-09-28 17:34:42 -0700620 if (description.hasGammaCorrection) {
621 shader.appendFormat(gFS_Gamma_Preamble, Properties::textGamma, 1.0f / Properties::textGamma);
622 }
623
Romain Guy707b2f72010-10-11 16:34:59 -0700624 // Optimization for common cases
Chris Craik91a8c7c2014-08-12 14:31:35 -0700625 if (!description.hasVertexAlpha
Chris Craikdeeda3d2014-05-05 19:09:33 -0700626 && !blendFramebuffer
627 && !description.hasColors
Chris Craikb9ce116d2015-08-20 15:14:06 -0700628 && description.colorOp == ProgramDescription::ColorFilterMode::None
Chris Craikdeeda3d2014-05-05 19:09:33 -0700629 && !description.hasDebugHighlight
Chris Craikdeeda3d2014-05-05 19:09:33 -0700630 && !description.hasRoundRectClip) {
Romain Guy707b2f72010-10-11 16:34:59 -0700631 bool fast = false;
632
633 const bool noShader = !description.hasGradient && !description.hasBitmap;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700634 const bool singleTexture = (description.hasTexture || description.hasExternalTexture) &&
Romain Guy707b2f72010-10-11 16:34:59 -0700635 !description.hasAlpha8Texture && noShader;
636 const bool singleA8Texture = description.hasTexture &&
637 description.hasAlpha8Texture && noShader;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700638 const bool singleGradient = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700639 description.hasGradient && !description.hasBitmap &&
640 description.gradientType == ProgramDescription::kGradientLinear;
641
642 if (singleColor) {
643 shader.append(gFS_Fast_SingleColor);
644 fast = true;
645 } else if (singleTexture) {
646 if (!description.modulate) {
647 shader.append(gFS_Fast_SingleTexture);
648 } else {
649 shader.append(gFS_Fast_SingleModulateTexture);
650 }
651 fast = true;
652 } else if (singleA8Texture) {
653 if (!description.modulate) {
Romain Guy253f2c22016-09-28 17:34:42 -0700654 if (description.hasGammaCorrection) {
655 shader.append(gFS_Fast_SingleA8Texture_ApplyGamma);
656 } else {
657 shader.append(gFS_Fast_SingleA8Texture);
658 }
Romain Guy707b2f72010-10-11 16:34:59 -0700659 } else {
Romain Guy253f2c22016-09-28 17:34:42 -0700660 if (description.hasGammaCorrection) {
661 shader.append(gFS_Fast_SingleModulateA8Texture_ApplyGamma);
662 } else {
663 shader.append(gFS_Fast_SingleModulateA8Texture);
664 }
Romain Guy707b2f72010-10-11 16:34:59 -0700665 }
666 fast = true;
667 } else if (singleGradient) {
Romain Guy253f2c22016-09-28 17:34:42 -0700668 shader.append(gFS_Dither_Functions);
669 shader.append(gFS_Dither_Preamble[mHasSRGB]);
Romain Guy707b2f72010-10-11 16:34:59 -0700670 if (!description.modulate) {
Romain Guy253f2c22016-09-28 17:34:42 -0700671 shader.append(gFS_Fast_SingleGradient[description.isSimpleGradient]);
Romain Guy707b2f72010-10-11 16:34:59 -0700672 } else {
Romain Guy253f2c22016-09-28 17:34:42 -0700673 shader.append(gFS_Fast_SingleModulateGradient[description.isSimpleGradient]);
Romain Guy707b2f72010-10-11 16:34:59 -0700674 }
675 fast = true;
676 }
677
678 if (fast) {
Romain Guyc15008e2010-11-10 11:59:15 -0800679#if DEBUG_PROGRAMS
Romain Guy707b2f72010-10-11 16:34:59 -0700680 PROGRAM_LOGD("*** Fast case:\n");
681 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
682 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800683#endif
Romain Guy707b2f72010-10-11 16:34:59 -0700684
685 return shader;
686 }
687 }
688
Romain Guyac670c02010-07-27 17:39:27 -0700689 if (description.hasBitmap) {
690 shader.append(gFS_Uniforms_BitmapSampler);
691 }
Chris Craikb9ce116d2015-08-20 15:14:06 -0700692 shader.append(gFS_Uniforms_ColorOp[static_cast<int>(description.colorOp)]);
Romain Guyac670c02010-07-27 17:39:27 -0700693
694 // Generate required functions
695 if (description.hasGradient && description.hasBitmap) {
Romain Guy48daa542010-08-10 19:21:34 -0700696 generateBlend(shader, "blendShaders", description.shadersMode);
Romain Guyac670c02010-07-27 17:39:27 -0700697 }
Chris Craikb9ce116d2015-08-20 15:14:06 -0700698 if (description.colorOp == ProgramDescription::ColorFilterMode::Blend) {
Romain Guy48daa542010-08-10 19:21:34 -0700699 generateBlend(shader, "blendColors", description.colorMode);
Romain Guyac670c02010-07-27 17:39:27 -0700700 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700701 if (blendFramebuffer) {
702 generateBlend(shader, "blendFramebuffer", description.framebufferMode);
703 }
Romain Guy889f8d12010-07-29 14:37:42 -0700704 if (description.isBitmapNpot) {
705 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
706 }
Romain Guy253f2c22016-09-28 17:34:42 -0700707 if (description.hasGradient) {
708 shader.append(gFS_Dither_Functions);
709 shader.append(gFS_Dither_Preamble[mHasSRGB]);
710 }
Romain Guyac670c02010-07-27 17:39:27 -0700711
712 // Begin the shader
713 shader.append(gFS_Main); {
714 // Stores the result in fragColor directly
Romain Guyaa6c24c2011-04-28 18:40:04 -0700715 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700716 if (description.hasAlpha8Texture) {
Romain Guy707b2f72010-10-11 16:34:59 -0700717 if (!description.hasGradient && !description.hasBitmap) {
Romain Guy253f2c22016-09-28 17:34:42 -0700718 shader.append(
719 gFS_Main_FetchA8Texture[modulateOp * 2 + description.hasGammaCorrection]);
Romain Guy707b2f72010-10-11 16:34:59 -0700720 }
Romain Guyac670c02010-07-27 17:39:27 -0700721 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700722 shader.append(gFS_Main_FetchTexture[modulateOp]);
Romain Guyac670c02010-07-27 17:39:27 -0700723 }
724 } else {
Romain Guya938f562012-09-13 20:31:08 -0700725 if (!description.hasGradient && !description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700726 shader.append(gFS_Main_FetchColor);
727 }
Romain Guyac670c02010-07-27 17:39:27 -0700728 }
729 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700730 shader.append(gFS_Main_FetchGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700731 }
732 if (description.hasBitmap) {
Romain Guy889f8d12010-07-29 14:37:42 -0700733 if (!description.isBitmapNpot) {
734 shader.append(gFS_Main_FetchBitmap);
735 } else {
736 shader.append(gFS_Main_FetchBitmapNpot);
737 }
Romain Guyac670c02010-07-27 17:39:27 -0700738 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700739 bool applyModulate = false;
Romain Guyac670c02010-07-27 17:39:27 -0700740 // Case when we have two shaders set
741 if (description.hasGradient && description.hasBitmap) {
742 if (description.isBitmapFirst) {
743 shader.append(gFS_Main_BlendShadersBG);
744 } else {
745 shader.append(gFS_Main_BlendShadersGB);
746 }
Romain Guya938f562012-09-13 20:31:08 -0700747 applyModulate = shaderOp(description, shader, modulateOp,
748 gFS_Main_BlendShaders_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700749 } else {
750 if (description.hasGradient) {
Romain Guya938f562012-09-13 20:31:08 -0700751 applyModulate = shaderOp(description, shader, modulateOp,
752 gFS_Main_GradientShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700753 } else if (description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700754 applyModulate = shaderOp(description, shader, modulateOp,
755 gFS_Main_BitmapShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700756 }
Romain Guyac670c02010-07-27 17:39:27 -0700757 }
Romain Guya938f562012-09-13 20:31:08 -0700758
Romain Guy740bf2b2011-04-26 15:33:10 -0700759 if (description.modulate && applyModulate) {
Romain Guya938f562012-09-13 20:31:08 -0700760 shader.append(gFS_Main_ModulateColor);
Romain Guy740bf2b2011-04-26 15:33:10 -0700761 }
Romain Guya938f562012-09-13 20:31:08 -0700762
Romain Guyac670c02010-07-27 17:39:27 -0700763 // Apply the color op if needed
Chris Craikb9ce116d2015-08-20 15:14:06 -0700764 shader.append(gFS_Main_ApplyColorOp[static_cast<int>(description.colorOp)]);
Chris Craik9f44a132012-09-13 18:34:55 -0700765
Chris Craik91a8c7c2014-08-12 14:31:35 -0700766 if (description.hasVertexAlpha) {
767 if (description.useShadowAlphaInterp) {
768 shader.append(gFS_Main_ApplyVertexAlphaShadowInterp);
Chris Craikbf759452014-08-11 16:00:44 -0700769 } else {
Chris Craik91a8c7c2014-08-12 14:31:35 -0700770 shader.append(gFS_Main_ApplyVertexAlphaLinearInterp);
Chris Craikbf759452014-08-11 16:00:44 -0700771 }
Chris Craik9f44a132012-09-13 18:34:55 -0700772 }
773
Romain Guy253f2c22016-09-28 17:34:42 -0700774 if (description.hasGradient) {
775 shader.append(gFS_Main_AddDither);
776 }
777
Romain Guyac670c02010-07-27 17:39:27 -0700778 // Output the fragment
Romain Guya5aed0d2010-09-09 14:42:43 -0700779 if (!blendFramebuffer) {
780 shader.append(gFS_Main_FragColor);
781 } else {
Romain Guyf607bdc2010-09-10 19:20:06 -0700782 shader.append(!description.swapSrcDst ?
783 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap);
Romain Guya5aed0d2010-09-09 14:42:43 -0700784 }
Romain Guyff316ec2013-02-13 18:39:43 -0800785 if (description.hasColors) {
786 shader.append(gFS_Main_FragColor_HasColors);
787 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700788 if (description.hasRoundRectClip) {
789 shader.append(gFS_Main_FragColor_HasRoundRectClip);
790 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800791 if (description.hasDebugHighlight) {
792 shader.append(gFS_Main_DebugHighlight);
793 }
Romain Guyac670c02010-07-27 17:39:27 -0700794 }
795 // End the shader
796 shader.append(gFS_Footer);
797
Romain Guyc15008e2010-11-10 11:59:15 -0800798#if DEBUG_PROGRAMS
Romain Guydb1938e2010-08-02 18:50:22 -0700799 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
800 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800801#endif
Romain Guydb1938e2010-08-02 18:50:22 -0700802
Romain Guyac670c02010-07-27 17:39:27 -0700803 return shader;
804}
805
Romain Guy48daa542010-08-10 19:21:34 -0700806void ProgramCache::generateBlend(String8& shader, const char* name, SkXfermode::Mode mode) {
Romain Guyac670c02010-07-27 17:39:27 -0700807 shader.append("\nvec4 ");
808 shader.append(name);
809 shader.append("(vec4 src, vec4 dst) {\n");
810 shader.append(" ");
Romain Guy48daa542010-08-10 19:21:34 -0700811 shader.append(gBlendOps[mode]);
Romain Guyac670c02010-07-27 17:39:27 -0700812 shader.append("}\n");
813}
814
Romain Guy889f8d12010-07-29 14:37:42 -0700815void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) {
Romain Guy63553472012-07-18 20:04:14 -0700816 shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700817 if (wrapS == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700818 shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700819 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n");
820 }
821 if (wrapT == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700822 shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700823 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n");
824 }
825 shader.append(" return vec2(");
826 switch (wrapS) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700827 case GL_CLAMP_TO_EDGE:
828 shader.append("texCoords.x");
829 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700830 case GL_REPEAT:
831 shader.append("mod(texCoords.x, 1.0)");
832 break;
833 case GL_MIRRORED_REPEAT:
834 shader.append("xMod2");
835 break;
836 }
837 shader.append(", ");
838 switch (wrapT) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700839 case GL_CLAMP_TO_EDGE:
840 shader.append("texCoords.y");
841 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700842 case GL_REPEAT:
843 shader.append("mod(texCoords.y, 1.0)");
844 break;
845 case GL_MIRRORED_REPEAT:
846 shader.append("yMod2");
847 break;
848 }
849 shader.append(");\n");
850 shader.append("}\n");
851}
852
Romain Guydb1938e2010-08-02 18:50:22 -0700853void ProgramCache::printLongString(const String8& shader) const {
854 ssize_t index = 0;
855 ssize_t lastIndex = 0;
856 const char* str = shader.string();
857 while ((index = shader.find("\n", index)) > -1) {
858 String8 line(str, index - lastIndex);
859 if (line.length() == 0) line.append("\n");
Chris Craik1c1c3fe2015-03-06 09:40:35 -0800860 ALOGD("%s", line.string());
Romain Guydb1938e2010-08-02 18:50:22 -0700861 index++;
862 str += (index - lastIndex);
863 lastIndex = index;
864 }
865}
866
Romain Guyac670c02010-07-27 17:39:27 -0700867}; // namespace uirenderer
868}; // namespace android