blob: f70723c35e8347743456dead2e51077d1ebe82d2 [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 Guyb4880042013-04-05 11:17:55 -070020#include "Dither.h"
Romain Guyac670c02010-07-27 17:39:27 -070021#include "ProgramCache.h"
22
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 =
Arun06e9f322017-01-23 11:59:21 +000061 "uniform mat4 roundRectInvTransform;\n"
62 "uniform mediump vec4 roundRectInnerRectLTWH;\n"
63 "uniform mediump float roundRectRadius;\n";
Romain Guyac670c02010-07-27 17:39:27 -070064const char* gVS_Header_Varyings_HasTexture =
65 "varying vec2 outTexCoords;\n";
Romain Guyff316ec2013-02-13 18:39:43 -080066const char* gVS_Header_Varyings_HasColors =
67 "varying vec4 outColors;\n";
Chris Craik91a8c7c2014-08-12 14:31:35 -070068const char* gVS_Header_Varyings_HasVertexAlpha =
Chris Craik6ebdc112012-08-31 18:24:33 -070069 "varying float alpha;\n";
Romain Guy63553472012-07-18 20:04:14 -070070const char* gVS_Header_Varyings_HasBitmap =
71 "varying highp vec2 outBitmapTexCoords;\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -070072const char* gVS_Header_Varyings_HasGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -070073 // Linear
Chet Haasea1d12dd2012-09-21 14:50:14 -070074 "varying highp vec2 linear;\n"
75 "varying vec2 ditherTexCoords;\n",
76 "varying float linear;\n"
77 "varying vec2 ditherTexCoords;\n",
Romain Guy211efea2012-07-31 21:16:07 -070078
Romain Guyee916f12010-09-20 17:53:08 -070079 // Circular
Chet Haasea1d12dd2012-09-21 14:50:14 -070080 "varying highp vec2 circular;\n"
81 "varying vec2 ditherTexCoords;\n",
82 "varying highp vec2 circular;\n"
83 "varying vec2 ditherTexCoords;\n",
Romain Guy211efea2012-07-31 21:16:07 -070084
Romain Guyee916f12010-09-20 17:53:08 -070085 // Sweep
Chet Haasea1d12dd2012-09-21 14:50:14 -070086 "varying highp vec2 sweep;\n"
87 "varying vec2 ditherTexCoords;\n",
88 "varying highp vec2 sweep;\n"
89 "varying vec2 ditherTexCoords;\n",
Romain Guyee916f12010-09-20 17:53:08 -070090};
Chris Craikdeeda3d2014-05-05 19:09:33 -070091const char* gVS_Header_Varyings_HasRoundRectClip =
Arun06e9f322017-01-23 11:59:21 +000092 "varying mediump vec2 roundRectPos;\n";
Romain Guyac670c02010-07-27 17:39:27 -070093const char* gVS_Main =
94 "\nvoid main(void) {\n";
95const char* gVS_Main_OutTexCoords =
96 " outTexCoords = texCoords;\n";
Romain Guyff316ec2013-02-13 18:39:43 -080097const char* gVS_Main_OutColors =
98 " outColors = colors;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -070099const char* gVS_Main_OutTransformedTexCoords =
100 " outTexCoords = (mainTextureTransform * vec4(texCoords, 0.0, 1.0)).xy;\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -0700101const char* gVS_Main_OutGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700102 // Linear
Chet Haasea1d12dd2012-09-21 14:50:14 -0700103 " linear = vec2((screenSpace * position).x, 0.5);\n"
Romain Guyb4880042013-04-05 11:17:55 -0700104 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Chet Haasea1d12dd2012-09-21 14:50:14 -0700105 " linear = (screenSpace * position).x;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700106 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Romain Guy211efea2012-07-31 21:16:07 -0700107
Romain Guyee916f12010-09-20 17:53:08 -0700108 // Circular
Chet Haasea1d12dd2012-09-21 14:50:14 -0700109 " circular = (screenSpace * position).xy;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700110 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Chet Haasea1d12dd2012-09-21 14:50:14 -0700111 " circular = (screenSpace * position).xy;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700112 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Romain Guy211efea2012-07-31 21:16:07 -0700113
Romain Guyee916f12010-09-20 17:53:08 -0700114 // Sweep
Chet Haasea1d12dd2012-09-21 14:50:14 -0700115 " sweep = (screenSpace * position).xy;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700116 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Chet Haasea1d12dd2012-09-21 14:50:14 -0700117 " sweep = (screenSpace * position).xy;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700118 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Romain Guyee916f12010-09-20 17:53:08 -0700119};
Romain Guy889f8d12010-07-29 14:37:42 -0700120const char* gVS_Main_OutBitmapTexCoords =
Romain Guy707b2f72010-10-11 16:34:59 -0700121 " outBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700122const char* gVS_Main_Position =
Chris Craikdeeda3d2014-05-05 19:09:33 -0700123 " vec4 transformedPosition = projection * transform * position;\n"
124 " gl_Position = transformedPosition;\n";
Chris Craikbf759452014-08-11 16:00:44 -0700125
Chris Craik91a8c7c2014-08-12 14:31:35 -0700126const char* gVS_Main_VertexAlpha =
Chris Craik6ebdc112012-08-31 18:24:33 -0700127 " alpha = vtxAlpha;\n";
Chris Craikbf759452014-08-11 16:00:44 -0700128
Chris Craikdeeda3d2014-05-05 19:09:33 -0700129const char* gVS_Main_HasRoundRectClip =
Arun06e9f322017-01-23 11:59:21 +0000130 " roundRectPos = ((roundRectInvTransform * transformedPosition).xy / roundRectRadius) - roundRectInnerRectLTWH.xy;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700131const char* gVS_Footer =
132 "}\n\n";
133
134///////////////////////////////////////////////////////////////////////////////
135// Fragment shaders snippets
136///////////////////////////////////////////////////////////////////////////////
137
Chris Craik8bd68c62015-08-19 15:29:05 -0700138const char* gFS_Header_Start =
139 "#version 100\n";
Romain Guya5aed0d2010-09-09 14:42:43 -0700140const char* gFS_Header_Extension_FramebufferFetch =
141 "#extension GL_NV_shader_framebuffer_fetch : enable\n\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700142const char* gFS_Header_Extension_ExternalTexture =
143 "#extension GL_OES_EGL_image_external : require\n\n";
Romain Guyac670c02010-07-27 17:39:27 -0700144const char* gFS_Header =
145 "precision mediump float;\n\n";
146const char* gFS_Uniforms_Color =
147 "uniform vec4 color;\n";
148const char* gFS_Uniforms_TextureSampler =
Romain Guya938f562012-09-13 20:31:08 -0700149 "uniform sampler2D baseSampler;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700150const char* gFS_Uniforms_ExternalTextureSampler =
Romain Guya938f562012-09-13 20:31:08 -0700151 "uniform samplerExternalOES baseSampler;\n";
Romain Guyb4880042013-04-05 11:17:55 -0700152const char* gFS_Uniforms_Dither =
153 "uniform sampler2D ditherSampler;";
154const char* gFS_Uniforms_GradientSampler[2] = {
155 "%s\n"
156 "uniform sampler2D gradientSampler;\n",
157 "%s\n"
158 "uniform vec4 startColor;\n"
Romain Guy211efea2012-07-31 21:16:07 -0700159 "uniform vec4 endColor;\n"
Romain Guyee916f12010-09-20 17:53:08 -0700160};
Romain Guyac670c02010-07-27 17:39:27 -0700161const char* gFS_Uniforms_BitmapSampler =
162 "uniform sampler2D bitmapSampler;\n";
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500163const char* gFS_Uniforms_ColorOp[3] = {
Romain Guyac670c02010-07-27 17:39:27 -0700164 // None
165 "",
166 // Matrix
167 "uniform mat4 colorMatrix;\n"
168 "uniform vec4 colorMatrixVector;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700169 // PorterDuff
Romain Guydb1938e2010-08-02 18:50:22 -0700170 "uniform vec4 colorBlend;\n"
Romain Guyac670c02010-07-27 17:39:27 -0700171};
Romain Guy41210632012-07-16 17:04:24 -0700172
Chris Craikdeeda3d2014-05-05 19:09:33 -0700173const char* gFS_Uniforms_HasRoundRectClip =
Arun06e9f322017-01-23 11:59:21 +0000174 "uniform mediump vec4 roundRectInnerRectLTWH;\n"
175 "uniform mediump float roundRectRadius;\n";
Chris Craikdeeda3d2014-05-05 19:09:33 -0700176
Romain Guyac670c02010-07-27 17:39:27 -0700177const char* gFS_Main =
178 "\nvoid main(void) {\n"
Romain Guy7fbcc042010-08-04 15:40:07 -0700179 " lowp vec4 fragColor;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700180
Romain Guyb4880042013-04-05 11:17:55 -0700181const char* gFS_Main_Dither[2] = {
182 // ES 2.0
183 "texture2D(ditherSampler, ditherTexCoords).a * " STR(DITHER_KERNEL_SIZE_INV_SQUARE),
184 // ES 3.0
Romain Guy032d47a2013-04-08 19:45:40 -0700185 "texture2D(ditherSampler, ditherTexCoords).a"
Romain Guyb4880042013-04-05 11:17:55 -0700186};
Romain Guy211efea2012-07-31 21:16:07 -0700187const char* gFS_Main_AddDitherToGradient =
Romain Guyb4880042013-04-05 11:17:55 -0700188 " gradientColor += %s;\n";
Romain Guy211efea2012-07-31 21:16:07 -0700189
Romain Guy707b2f72010-10-11 16:34:59 -0700190// Fast cases
191const char* gFS_Fast_SingleColor =
192 "\nvoid main(void) {\n"
193 " gl_FragColor = color;\n"
194 "}\n\n";
195const char* gFS_Fast_SingleTexture =
196 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700197 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700198 "}\n\n";
199const char* gFS_Fast_SingleModulateTexture =
200 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700201 " gl_FragColor = color.a * texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700202 "}\n\n";
203const char* gFS_Fast_SingleA8Texture =
204 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700205 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700206 "}\n\n";
207const char* gFS_Fast_SingleModulateA8Texture =
208 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700209 " gl_FragColor = color * texture2D(baseSampler, outTexCoords).a;\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700210 "}\n\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -0700211const char* gFS_Fast_SingleGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700212 "\nvoid main(void) {\n"
Romain Guyb4880042013-04-05 11:17:55 -0700213 " gl_FragColor = %s + texture2D(gradientSampler, linear);\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700214 "}\n\n",
215 "\nvoid main(void) {\n"
Romain Guyb4880042013-04-05 11:17:55 -0700216 " gl_FragColor = %s + mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
217 "}\n\n",
Romain Guy42e1e0d2012-07-30 14:47:51 -0700218};
219const char* gFS_Fast_SingleModulateGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700220 "\nvoid main(void) {\n"
Romain Guyb4880042013-04-05 11:17:55 -0700221 " gl_FragColor = %s + color.a * texture2D(gradientSampler, linear);\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700222 "}\n\n",
223 "\nvoid main(void) {\n"
Romain Guyb4880042013-04-05 11:17:55 -0700224 " gl_FragColor = %s + color.a * mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700225 "}\n\n"
226};
Romain Guy707b2f72010-10-11 16:34:59 -0700227
228// General case
Romain Guyac670c02010-07-27 17:39:27 -0700229const char* gFS_Main_FetchColor =
230 " fragColor = color;\n";
Romain Guy740bf2b2011-04-26 15:33:10 -0700231const char* gFS_Main_ModulateColor =
232 " fragColor *= color.a;\n";
Chris Craik91a8c7c2014-08-12 14:31:35 -0700233const char* gFS_Main_ApplyVertexAlphaLinearInterp =
Chris Craik6ebdc112012-08-31 18:24:33 -0700234 " fragColor *= alpha;\n";
Chris Craik91a8c7c2014-08-12 14:31:35 -0700235const char* gFS_Main_ApplyVertexAlphaShadowInterp =
Chris Craik138c21f2016-04-28 16:59:42 -0700236 // map alpha through shadow alpha sampler
237 " fragColor *= texture2D(baseSampler, vec2(alpha, 0.5)).a;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700238const char* gFS_Main_FetchTexture[2] = {
239 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700240 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700241 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700242 " fragColor = color * texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700243};
Chris Craik11718bc2015-09-22 11:50:13 -0700244const char* gFS_Main_FetchA8Texture[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700245 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700246 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700247 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700248 " fragColor = color * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700249};
Romain Guy42e1e0d2012-07-30 14:47:51 -0700250const char* gFS_Main_FetchGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700251 // Linear
Romain Guy320d46b2012-08-08 16:05:42 -0700252 " vec4 gradientColor = texture2D(gradientSampler, linear);\n",
Romain Guy211efea2012-07-31 21:16:07 -0700253
Romain Guy320d46b2012-08-08 16:05:42 -0700254 " vec4 gradientColor = mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700255
Romain Guyee916f12010-09-20 17:53:08 -0700256 // Circular
Romain Guy320d46b2012-08-08 16:05:42 -0700257 " vec4 gradientColor = texture2D(gradientSampler, vec2(length(circular), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700258
Romain Guy320d46b2012-08-08 16:05:42 -0700259 " vec4 gradientColor = mix(startColor, endColor, clamp(length(circular), 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700260
Romain Guyee916f12010-09-20 17:53:08 -0700261 // Sweep
Romain Guy63553472012-07-18 20:04:14 -0700262 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700263 " vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700264
Romain Guy42e1e0d2012-07-30 14:47:51 -0700265 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700266 " vec4 gradientColor = mix(startColor, endColor, clamp(index - floor(index), 0.0, 1.0));\n"
Romain Guyee916f12010-09-20 17:53:08 -0700267};
Romain Guyac670c02010-07-27 17:39:27 -0700268const char* gFS_Main_FetchBitmap =
269 " vec4 bitmapColor = texture2D(bitmapSampler, outBitmapTexCoords);\n";
Romain Guy889f8d12010-07-29 14:37:42 -0700270const char* gFS_Main_FetchBitmapNpot =
271 " vec4 bitmapColor = texture2D(bitmapSampler, wrap(outBitmapTexCoords));\n";
Romain Guyac670c02010-07-27 17:39:27 -0700272const char* gFS_Main_BlendShadersBG =
Romain Guyac670c02010-07-27 17:39:27 -0700273 " fragColor = blendShaders(gradientColor, bitmapColor)";
Romain Guy06f96e22010-07-30 19:18:16 -0700274const char* gFS_Main_BlendShadersGB =
275 " fragColor = blendShaders(bitmapColor, gradientColor)";
Chris Craik11718bc2015-09-22 11:50:13 -0700276const char* gFS_Main_BlendShaders_Modulate[3] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700277 // Don't modulate
278 ";\n",
279 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700280 " * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700281 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700282 " * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700283};
Chris Craik11718bc2015-09-22 11:50:13 -0700284const char* gFS_Main_GradientShader_Modulate[3] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700285 // Don't modulate
286 " fragColor = gradientColor;\n",
287 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700288 " fragColor = gradientColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700289 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700290 " fragColor = gradientColor * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700291 };
Chris Craik11718bc2015-09-22 11:50:13 -0700292const char* gFS_Main_BitmapShader_Modulate[3] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700293 // Don't modulate
294 " fragColor = bitmapColor;\n",
295 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700296 " fragColor = bitmapColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700297 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700298 " fragColor = bitmapColor * texture2D(baseSampler, outTexCoords).a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700299 };
Romain Guyac670c02010-07-27 17:39:27 -0700300const char* gFS_Main_FragColor =
301 " gl_FragColor = fragColor;\n";
Romain Guyff316ec2013-02-13 18:39:43 -0800302const char* gFS_Main_FragColor_HasColors =
303 " gl_FragColor *= outColors;\n";
Romain Guya5aed0d2010-09-09 14:42:43 -0700304const char* gFS_Main_FragColor_Blend =
305 " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n";
Romain Guyf607bdc2010-09-10 19:20:06 -0700306const char* gFS_Main_FragColor_Blend_Swap =
307 " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n";
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500308const char* gFS_Main_ApplyColorOp[3] = {
Romain Guyac670c02010-07-27 17:39:27 -0700309 // None
310 "",
311 // Matrix
Chris Craik73821c82014-09-16 17:32:13 -0700312 " fragColor.rgb /= (fragColor.a + 0.0019);\n" // un-premultiply
Romain Guyac670c02010-07-27 17:39:27 -0700313 " fragColor *= colorMatrix;\n"
Chris Craik73821c82014-09-16 17:32:13 -0700314 " fragColor += colorMatrixVector;\n"
315 " fragColor.rgb *= (fragColor.a + 0.0019);\n", // re-premultiply
Romain Guyac670c02010-07-27 17:39:27 -0700316 // PorterDuff
317 " fragColor = blendColors(colorBlend, fragColor);\n"
318};
Chris Craikdeeda3d2014-05-05 19:09:33 -0700319
Arun06e9f322017-01-23 11:59:21 +0000320// Note: LTWH (left top width height) -> xyzw
321// roundRectPos is now divided by roundRectRadius in vertex shader
322// after we also subtract roundRectInnerRectLTWH.xy from roundRectPos
Chris Craikdeeda3d2014-05-05 19:09:33 -0700323const char* gFS_Main_FragColor_HasRoundRectClip =
Arun06e9f322017-01-23 11:59:21 +0000324 " mediump vec2 fragToLT = -roundRectPos;\n"
325 " mediump vec2 fragFromRB = roundRectPos - roundRectInnerRectLTWH.zw;\n"
Chris Craikf99f3202014-08-05 15:31:52 -0700326
Arun06e9f322017-01-23 11:59:21 +0000327 // since distance is divided by radius, it's in [0;1] so precision is not an issue
328 // this also lets us clamp(0.0, 1.0) instead of max() which is cheaper on GPUs
329 " mediump vec2 dist = clamp(max(fragToLT, fragFromRB), 0.0, 1.0);\n"
330 " mediump float linearDist = clamp(roundRectRadius - (length(dist) * roundRectRadius), 0.0, 1.0);\n"
331 " gl_FragColor *= linearDist;\n";
Chris Craikdeeda3d2014-05-05 19:09:33 -0700332
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800333const char* gFS_Main_DebugHighlight =
334 " gl_FragColor.rgb = vec3(0.0, gl_FragColor.a, 0.0);\n";
Romain Guyac670c02010-07-27 17:39:27 -0700335const char* gFS_Footer =
336 "}\n\n";
337
338///////////////////////////////////////////////////////////////////////////////
339// PorterDuff snippets
340///////////////////////////////////////////////////////////////////////////////
341
Romain Guy48daa542010-08-10 19:21:34 -0700342const char* gBlendOps[18] = {
Romain Guyac670c02010-07-27 17:39:27 -0700343 // Clear
344 "return vec4(0.0, 0.0, 0.0, 0.0);\n",
345 // Src
346 "return src;\n",
347 // Dst
348 "return dst;\n",
349 // SrcOver
Romain Guy06f96e22010-07-30 19:18:16 -0700350 "return src + dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700351 // DstOver
Romain Guy06f96e22010-07-30 19:18:16 -0700352 "return dst + src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700353 // SrcIn
Romain Guy06f96e22010-07-30 19:18:16 -0700354 "return src * dst.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700355 // DstIn
Romain Guy06f96e22010-07-30 19:18:16 -0700356 "return dst * src.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700357 // SrcOut
Romain Guy06f96e22010-07-30 19:18:16 -0700358 "return src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700359 // DstOut
Romain Guy06f96e22010-07-30 19:18:16 -0700360 "return dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700361 // SrcAtop
362 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n",
363 // DstAtop
364 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n",
365 // Xor
Romain Guy48daa542010-08-10 19:21:34 -0700366 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, "
Romain Guyac670c02010-07-27 17:39:27 -0700367 "src.a + dst.a - 2.0 * src.a * dst.a);\n",
Derek Sollenbergerc0bf7002015-03-06 13:48:27 -0500368 // Plus
Romain Guy48daa542010-08-10 19:21:34 -0700369 "return min(src + dst, 1.0);\n",
Derek Sollenbergerc0bf7002015-03-06 13:48:27 -0500370 // Modulate
Romain Guy48daa542010-08-10 19:21:34 -0700371 "return src * dst;\n",
372 // Screen
373 "return src + dst - src * dst;\n",
374 // Overlay
375 "return clamp(vec4(mix("
376 "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), "
377 "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), "
378 "step(dst.a, 2.0 * dst.rgb)), "
379 "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n",
380 // Darken
381 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
382 "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
383 // Lighten
384 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
385 "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 -0700386};
387
388///////////////////////////////////////////////////////////////////////////////
389// Constructors/destructors
390///////////////////////////////////////////////////////////////////////////////
391
Chris Craik117bdbc2015-02-05 10:12:38 -0800392ProgramCache::ProgramCache(Extensions& extensions)
393 : mHasES3(extensions.getMajorGlVersion() >= 3) {
Romain Guyac670c02010-07-27 17:39:27 -0700394}
395
396ProgramCache::~ProgramCache() {
397 clear();
398}
399
400///////////////////////////////////////////////////////////////////////////////
401// Cache management
402///////////////////////////////////////////////////////////////////////////////
403
404void ProgramCache::clear() {
Romain Guy67f27952010-12-07 20:09:23 -0800405 PROGRAM_LOGD("Clearing program cache");
Romain Guyac670c02010-07-27 17:39:27 -0700406 mCache.clear();
407}
408
409Program* ProgramCache::get(const ProgramDescription& description) {
410 programid key = description.key();
Chris Craik096b8d92013-03-01 11:08:11 -0800411 if (key == (PROGRAM_KEY_TEXTURE | PROGRAM_KEY_A8_TEXTURE)) {
412 // program for A8, unmodulated, texture w/o shader (black text/path textures) is equivalent
413 // to standard texture program (bitmaps, patches). Consider them equivalent.
414 key = PROGRAM_KEY_TEXTURE;
415 }
416
Chris Craik51d6a3d2014-12-22 17:16:56 -0800417 auto iter = mCache.find(key);
Chris Craikd41c4d82015-01-05 15:51:13 -0800418 Program* program = nullptr;
Chris Craik51d6a3d2014-12-22 17:16:56 -0800419 if (iter == mCache.end()) {
Romain Guyee916f12010-09-20 17:53:08 -0700420 description.log("Could not find program");
Romain Guyac670c02010-07-27 17:39:27 -0700421 program = generateProgram(description, key);
Chris Craik51d6a3d2014-12-22 17:16:56 -0800422 mCache[key] = std::unique_ptr<Program>(program);
Romain Guyac670c02010-07-27 17:39:27 -0700423 } else {
Chris Craik51d6a3d2014-12-22 17:16:56 -0800424 program = iter->second.get();
Romain Guyac670c02010-07-27 17:39:27 -0700425 }
426 return program;
427}
428
429///////////////////////////////////////////////////////////////////////////////
430// Program generation
431///////////////////////////////////////////////////////////////////////////////
432
Andreas Gampe64bb4132014-11-22 00:35:09 +0000433Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) {
Romain Guyac670c02010-07-27 17:39:27 -0700434 String8 vertexShader = generateVertexShader(description);
435 String8 fragmentShader = generateFragmentShader(description);
436
Romain Guy42e1e0d2012-07-30 14:47:51 -0700437 return new Program(description, vertexShader.string(), fragmentShader.string());
438}
439
440static inline size_t gradientIndex(const ProgramDescription& description) {
441 return description.gradientType * 2 + description.isSimpleGradient;
Romain Guyac670c02010-07-27 17:39:27 -0700442}
443
444String8 ProgramCache::generateVertexShader(const ProgramDescription& description) {
445 // Add attributes
Chris Craik8bd68c62015-08-19 15:29:05 -0700446 String8 shader(gVS_Header_Start);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700447 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700448 shader.append(gVS_Header_Attributes_TexCoords);
449 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700450 if (description.hasVertexAlpha) {
451 shader.append(gVS_Header_Attributes_VertexAlphaParameters);
Chet Haase5b0200b2011-04-13 17:58:08 -0700452 }
Romain Guyff316ec2013-02-13 18:39:43 -0800453 if (description.hasColors) {
454 shader.append(gVS_Header_Attributes_Colors);
455 }
Romain Guyac670c02010-07-27 17:39:27 -0700456 // Uniforms
457 shader.append(gVS_Header_Uniforms);
Romain Guy8f0095c2011-05-02 17:24:22 -0700458 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700459 shader.append(gVS_Header_Uniforms_TextureTransform);
460 }
Romain Guyac670c02010-07-27 17:39:27 -0700461 if (description.hasGradient) {
Romain Guyb4880042013-04-05 11:17:55 -0700462 shader.append(gVS_Header_Uniforms_HasGradient);
Romain Guyac670c02010-07-27 17:39:27 -0700463 }
Romain Guy889f8d12010-07-29 14:37:42 -0700464 if (description.hasBitmap) {
465 shader.append(gVS_Header_Uniforms_HasBitmap);
466 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700467 if (description.hasRoundRectClip) {
468 shader.append(gVS_Header_Uniforms_HasRoundRectClip);
469 }
Romain Guyac670c02010-07-27 17:39:27 -0700470 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700471 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700472 shader.append(gVS_Header_Varyings_HasTexture);
473 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700474 if (description.hasVertexAlpha) {
475 shader.append(gVS_Header_Varyings_HasVertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700476 }
Romain Guyff316ec2013-02-13 18:39:43 -0800477 if (description.hasColors) {
478 shader.append(gVS_Header_Varyings_HasColors);
479 }
Romain Guyac670c02010-07-27 17:39:27 -0700480 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700481 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700482 }
483 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700484 shader.append(gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700485 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700486 if (description.hasRoundRectClip) {
487 shader.append(gVS_Header_Varyings_HasRoundRectClip);
488 }
Romain Guyac670c02010-07-27 17:39:27 -0700489
490 // Begin the shader
491 shader.append(gVS_Main); {
Romain Guy8f0095c2011-05-02 17:24:22 -0700492 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700493 shader.append(gVS_Main_OutTransformedTexCoords);
Romain Guy8f0095c2011-05-02 17:24:22 -0700494 } else if (description.hasTexture || description.hasExternalTexture) {
495 shader.append(gVS_Main_OutTexCoords);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700496 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700497 if (description.hasVertexAlpha) {
498 shader.append(gVS_Main_VertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700499 }
Romain Guyff316ec2013-02-13 18:39:43 -0800500 if (description.hasColors) {
501 shader.append(gVS_Main_OutColors);
502 }
Romain Guy889f8d12010-07-29 14:37:42 -0700503 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700504 shader.append(gVS_Main_OutBitmapTexCoords);
Romain Guy889f8d12010-07-29 14:37:42 -0700505 }
Romain Guyac670c02010-07-27 17:39:27 -0700506 // Output transformed position
507 shader.append(gVS_Main_Position);
Chet Haasea1d12dd2012-09-21 14:50:14 -0700508 if (description.hasGradient) {
509 shader.append(gVS_Main_OutGradient[gradientIndex(description)]);
510 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700511 if (description.hasRoundRectClip) {
512 shader.append(gVS_Main_HasRoundRectClip);
513 }
Romain Guyac670c02010-07-27 17:39:27 -0700514 }
515 // End the shader
516 shader.append(gVS_Footer);
517
518 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string());
519
520 return shader;
521}
522
Romain Guya938f562012-09-13 20:31:08 -0700523static bool shaderOp(const ProgramDescription& description, String8& shader,
524 const int modulateOp, const char** snippets) {
525 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
Romain Guya938f562012-09-13 20:31:08 -0700526 shader.append(snippets[op]);
527 return description.hasAlpha8Texture;
528}
529
Romain Guyac670c02010-07-27 17:39:27 -0700530String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) {
Chris Craik8bd68c62015-08-19 15:29:05 -0700531 String8 shader(gFS_Header_Start);
Romain Guya5aed0d2010-09-09 14:42:43 -0700532
Romain Guy707b2f72010-10-11 16:34:59 -0700533 const bool blendFramebuffer = description.framebufferMode >= SkXfermode::kPlus_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700534 if (blendFramebuffer) {
535 shader.append(gFS_Header_Extension_FramebufferFetch);
536 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700537 if (description.hasExternalTexture) {
538 shader.append(gFS_Header_Extension_ExternalTexture);
539 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700540
541 shader.append(gFS_Header);
Romain Guyac670c02010-07-27 17:39:27 -0700542
543 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700544 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700545 shader.append(gVS_Header_Varyings_HasTexture);
546 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700547 if (description.hasVertexAlpha) {
548 shader.append(gVS_Header_Varyings_HasVertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700549 }
Romain Guyff316ec2013-02-13 18:39:43 -0800550 if (description.hasColors) {
551 shader.append(gVS_Header_Varyings_HasColors);
552 }
Romain Guyac670c02010-07-27 17:39:27 -0700553 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700554 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700555 }
556 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700557 shader.append(gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700558 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700559 if (description.hasRoundRectClip) {
560 shader.append(gVS_Header_Varyings_HasRoundRectClip);
561 }
Romain Guyac670c02010-07-27 17:39:27 -0700562
Romain Guyac670c02010-07-27 17:39:27 -0700563 // Uniforms
Romain Guy707b2f72010-10-11 16:34:59 -0700564 int modulateOp = MODULATE_OP_NO_MODULATE;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700565 const bool singleColor = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700566 !description.hasGradient && !description.hasBitmap;
567
568 if (description.modulate || singleColor) {
569 shader.append(gFS_Uniforms_Color);
570 if (!singleColor) modulateOp = MODULATE_OP_MODULATE;
571 }
Chris Craik138c21f2016-04-28 16:59:42 -0700572 if (description.hasTexture || description.useShadowAlphaInterp) {
Romain Guyac670c02010-07-27 17:39:27 -0700573 shader.append(gFS_Uniforms_TextureSampler);
Romain Guy8f0095c2011-05-02 17:24:22 -0700574 } else if (description.hasExternalTexture) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700575 shader.append(gFS_Uniforms_ExternalTextureSampler);
576 }
Romain Guyac670c02010-07-27 17:39:27 -0700577 if (description.hasGradient) {
Romain Guyb4880042013-04-05 11:17:55 -0700578 shader.appendFormat(gFS_Uniforms_GradientSampler[description.isSimpleGradient],
579 gFS_Uniforms_Dither);
Romain Guyac670c02010-07-27 17:39:27 -0700580 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700581 if (description.hasRoundRectClip) {
582 shader.append(gFS_Uniforms_HasRoundRectClip);
583 }
Romain Guy707b2f72010-10-11 16:34:59 -0700584
585 // Optimization for common cases
Chris Craik91a8c7c2014-08-12 14:31:35 -0700586 if (!description.hasVertexAlpha
Chris Craikdeeda3d2014-05-05 19:09:33 -0700587 && !blendFramebuffer
588 && !description.hasColors
Chris Craikb9ce116d2015-08-20 15:14:06 -0700589 && description.colorOp == ProgramDescription::ColorFilterMode::None
Chris Craikdeeda3d2014-05-05 19:09:33 -0700590 && !description.hasDebugHighlight
Chris Craikdeeda3d2014-05-05 19:09:33 -0700591 && !description.hasRoundRectClip) {
Romain Guy707b2f72010-10-11 16:34:59 -0700592 bool fast = false;
593
594 const bool noShader = !description.hasGradient && !description.hasBitmap;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700595 const bool singleTexture = (description.hasTexture || description.hasExternalTexture) &&
Romain Guy707b2f72010-10-11 16:34:59 -0700596 !description.hasAlpha8Texture && noShader;
597 const bool singleA8Texture = description.hasTexture &&
598 description.hasAlpha8Texture && noShader;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700599 const bool singleGradient = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700600 description.hasGradient && !description.hasBitmap &&
601 description.gradientType == ProgramDescription::kGradientLinear;
602
603 if (singleColor) {
604 shader.append(gFS_Fast_SingleColor);
605 fast = true;
606 } else if (singleTexture) {
607 if (!description.modulate) {
608 shader.append(gFS_Fast_SingleTexture);
609 } else {
610 shader.append(gFS_Fast_SingleModulateTexture);
611 }
612 fast = true;
613 } else if (singleA8Texture) {
614 if (!description.modulate) {
Chris Craik11718bc2015-09-22 11:50:13 -0700615 shader.append(gFS_Fast_SingleA8Texture);
Romain Guy707b2f72010-10-11 16:34:59 -0700616 } else {
Chris Craik11718bc2015-09-22 11:50:13 -0700617 shader.append(gFS_Fast_SingleModulateA8Texture);
Romain Guy707b2f72010-10-11 16:34:59 -0700618 }
619 fast = true;
620 } else if (singleGradient) {
621 if (!description.modulate) {
Romain Guyb4880042013-04-05 11:17:55 -0700622 shader.appendFormat(gFS_Fast_SingleGradient[description.isSimpleGradient],
623 gFS_Main_Dither[mHasES3]);
Romain Guy707b2f72010-10-11 16:34:59 -0700624 } else {
Romain Guyb4880042013-04-05 11:17:55 -0700625 shader.appendFormat(gFS_Fast_SingleModulateGradient[description.isSimpleGradient],
626 gFS_Main_Dither[mHasES3]);
Romain Guy707b2f72010-10-11 16:34:59 -0700627 }
628 fast = true;
629 }
630
631 if (fast) {
Romain Guyc15008e2010-11-10 11:59:15 -0800632#if DEBUG_PROGRAMS
Romain Guy707b2f72010-10-11 16:34:59 -0700633 PROGRAM_LOGD("*** Fast case:\n");
634 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
635 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800636#endif
Romain Guy707b2f72010-10-11 16:34:59 -0700637
638 return shader;
639 }
640 }
641
Romain Guyac670c02010-07-27 17:39:27 -0700642 if (description.hasBitmap) {
643 shader.append(gFS_Uniforms_BitmapSampler);
644 }
Chris Craikb9ce116d2015-08-20 15:14:06 -0700645 shader.append(gFS_Uniforms_ColorOp[static_cast<int>(description.colorOp)]);
Romain Guyac670c02010-07-27 17:39:27 -0700646
647 // Generate required functions
648 if (description.hasGradient && description.hasBitmap) {
Romain Guy48daa542010-08-10 19:21:34 -0700649 generateBlend(shader, "blendShaders", description.shadersMode);
Romain Guyac670c02010-07-27 17:39:27 -0700650 }
Chris Craikb9ce116d2015-08-20 15:14:06 -0700651 if (description.colorOp == ProgramDescription::ColorFilterMode::Blend) {
Romain Guy48daa542010-08-10 19:21:34 -0700652 generateBlend(shader, "blendColors", description.colorMode);
Romain Guyac670c02010-07-27 17:39:27 -0700653 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700654 if (blendFramebuffer) {
655 generateBlend(shader, "blendFramebuffer", description.framebufferMode);
656 }
Romain Guy889f8d12010-07-29 14:37:42 -0700657 if (description.isBitmapNpot) {
658 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
659 }
Romain Guyac670c02010-07-27 17:39:27 -0700660
661 // Begin the shader
662 shader.append(gFS_Main); {
663 // Stores the result in fragColor directly
Romain Guyaa6c24c2011-04-28 18:40:04 -0700664 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700665 if (description.hasAlpha8Texture) {
Romain Guy707b2f72010-10-11 16:34:59 -0700666 if (!description.hasGradient && !description.hasBitmap) {
Chris Craik11718bc2015-09-22 11:50:13 -0700667 shader.append(gFS_Main_FetchA8Texture[modulateOp]);
Romain Guy707b2f72010-10-11 16:34:59 -0700668 }
Romain Guyac670c02010-07-27 17:39:27 -0700669 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700670 shader.append(gFS_Main_FetchTexture[modulateOp]);
Romain Guyac670c02010-07-27 17:39:27 -0700671 }
672 } else {
Romain Guya938f562012-09-13 20:31:08 -0700673 if (!description.hasGradient && !description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700674 shader.append(gFS_Main_FetchColor);
675 }
Romain Guyac670c02010-07-27 17:39:27 -0700676 }
677 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700678 shader.append(gFS_Main_FetchGradient[gradientIndex(description)]);
Romain Guyb4880042013-04-05 11:17:55 -0700679 shader.appendFormat(gFS_Main_AddDitherToGradient, gFS_Main_Dither[mHasES3]);
Romain Guyac670c02010-07-27 17:39:27 -0700680 }
681 if (description.hasBitmap) {
Romain Guy889f8d12010-07-29 14:37:42 -0700682 if (!description.isBitmapNpot) {
683 shader.append(gFS_Main_FetchBitmap);
684 } else {
685 shader.append(gFS_Main_FetchBitmapNpot);
686 }
Romain Guyac670c02010-07-27 17:39:27 -0700687 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700688 bool applyModulate = false;
Romain Guyac670c02010-07-27 17:39:27 -0700689 // Case when we have two shaders set
690 if (description.hasGradient && description.hasBitmap) {
691 if (description.isBitmapFirst) {
692 shader.append(gFS_Main_BlendShadersBG);
693 } else {
694 shader.append(gFS_Main_BlendShadersGB);
695 }
Romain Guya938f562012-09-13 20:31:08 -0700696 applyModulate = shaderOp(description, shader, modulateOp,
697 gFS_Main_BlendShaders_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700698 } else {
699 if (description.hasGradient) {
Romain Guya938f562012-09-13 20:31:08 -0700700 applyModulate = shaderOp(description, shader, modulateOp,
701 gFS_Main_GradientShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700702 } else if (description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700703 applyModulate = shaderOp(description, shader, modulateOp,
704 gFS_Main_BitmapShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700705 }
Romain Guyac670c02010-07-27 17:39:27 -0700706 }
Romain Guya938f562012-09-13 20:31:08 -0700707
Romain Guy740bf2b2011-04-26 15:33:10 -0700708 if (description.modulate && applyModulate) {
Romain Guya938f562012-09-13 20:31:08 -0700709 shader.append(gFS_Main_ModulateColor);
Romain Guy740bf2b2011-04-26 15:33:10 -0700710 }
Romain Guya938f562012-09-13 20:31:08 -0700711
Romain Guyac670c02010-07-27 17:39:27 -0700712 // Apply the color op if needed
Chris Craikb9ce116d2015-08-20 15:14:06 -0700713 shader.append(gFS_Main_ApplyColorOp[static_cast<int>(description.colorOp)]);
Chris Craik9f44a132012-09-13 18:34:55 -0700714
Chris Craik91a8c7c2014-08-12 14:31:35 -0700715 if (description.hasVertexAlpha) {
716 if (description.useShadowAlphaInterp) {
717 shader.append(gFS_Main_ApplyVertexAlphaShadowInterp);
Chris Craikbf759452014-08-11 16:00:44 -0700718 } else {
Chris Craik91a8c7c2014-08-12 14:31:35 -0700719 shader.append(gFS_Main_ApplyVertexAlphaLinearInterp);
Chris Craikbf759452014-08-11 16:00:44 -0700720 }
Chris Craik9f44a132012-09-13 18:34:55 -0700721 }
722
Romain Guyac670c02010-07-27 17:39:27 -0700723 // Output the fragment
Romain Guya5aed0d2010-09-09 14:42:43 -0700724 if (!blendFramebuffer) {
725 shader.append(gFS_Main_FragColor);
726 } else {
Romain Guyf607bdc2010-09-10 19:20:06 -0700727 shader.append(!description.swapSrcDst ?
728 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap);
Romain Guya5aed0d2010-09-09 14:42:43 -0700729 }
Romain Guyff316ec2013-02-13 18:39:43 -0800730 if (description.hasColors) {
731 shader.append(gFS_Main_FragColor_HasColors);
732 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700733 if (description.hasRoundRectClip) {
734 shader.append(gFS_Main_FragColor_HasRoundRectClip);
735 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800736 if (description.hasDebugHighlight) {
737 shader.append(gFS_Main_DebugHighlight);
738 }
Romain Guyac670c02010-07-27 17:39:27 -0700739 }
740 // End the shader
741 shader.append(gFS_Footer);
742
Romain Guyc15008e2010-11-10 11:59:15 -0800743#if DEBUG_PROGRAMS
Romain Guydb1938e2010-08-02 18:50:22 -0700744 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
745 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800746#endif
Romain Guydb1938e2010-08-02 18:50:22 -0700747
Romain Guyac670c02010-07-27 17:39:27 -0700748 return shader;
749}
750
Romain Guy48daa542010-08-10 19:21:34 -0700751void ProgramCache::generateBlend(String8& shader, const char* name, SkXfermode::Mode mode) {
Romain Guyac670c02010-07-27 17:39:27 -0700752 shader.append("\nvec4 ");
753 shader.append(name);
754 shader.append("(vec4 src, vec4 dst) {\n");
755 shader.append(" ");
Romain Guy48daa542010-08-10 19:21:34 -0700756 shader.append(gBlendOps[mode]);
Romain Guyac670c02010-07-27 17:39:27 -0700757 shader.append("}\n");
758}
759
Romain Guy889f8d12010-07-29 14:37:42 -0700760void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) {
Romain Guy63553472012-07-18 20:04:14 -0700761 shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700762 if (wrapS == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700763 shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700764 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n");
765 }
766 if (wrapT == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700767 shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700768 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n");
769 }
770 shader.append(" return vec2(");
771 switch (wrapS) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700772 case GL_CLAMP_TO_EDGE:
773 shader.append("texCoords.x");
774 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700775 case GL_REPEAT:
776 shader.append("mod(texCoords.x, 1.0)");
777 break;
778 case GL_MIRRORED_REPEAT:
779 shader.append("xMod2");
780 break;
781 }
782 shader.append(", ");
783 switch (wrapT) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700784 case GL_CLAMP_TO_EDGE:
785 shader.append("texCoords.y");
786 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700787 case GL_REPEAT:
788 shader.append("mod(texCoords.y, 1.0)");
789 break;
790 case GL_MIRRORED_REPEAT:
791 shader.append("yMod2");
792 break;
793 }
794 shader.append(");\n");
795 shader.append("}\n");
796}
797
Romain Guydb1938e2010-08-02 18:50:22 -0700798void ProgramCache::printLongString(const String8& shader) const {
799 ssize_t index = 0;
800 ssize_t lastIndex = 0;
801 const char* str = shader.string();
802 while ((index = shader.find("\n", index)) > -1) {
803 String8 line(str, index - lastIndex);
804 if (line.length() == 0) line.append("\n");
Chris Craik1c1c3fe2015-03-06 09:40:35 -0800805 ALOGD("%s", line.string());
Romain Guydb1938e2010-08-02 18:50:22 -0700806 index++;
807 str += (index - lastIndex);
808 lastIndex = index;
809 }
810}
811
Romain Guyac670c02010-07-27 17:39:27 -0700812}; // namespace uirenderer
813}; // namespace android