blob: 1ac368f423b8299b75830ebebe96b418d0a115ec [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
Romain Guyac670c02010-07-27 17:39:27 -070041const char* gVS_Header_Attributes =
42 "attribute vec4 position;\n";
43const char* gVS_Header_Attributes_TexCoords =
44 "attribute vec2 texCoords;\n";
Romain Guyff316ec2013-02-13 18:39:43 -080045const char* gVS_Header_Attributes_Colors =
46 "attribute vec4 colors;\n";
Chris Craik91a8c7c2014-08-12 14:31:35 -070047const char* gVS_Header_Attributes_VertexAlphaParameters =
Chris Craik6ebdc112012-08-31 18:24:33 -070048 "attribute float vtxAlpha;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -070049const char* gVS_Header_Uniforms_TextureTransform =
50 "uniform mat4 mainTextureTransform;\n";
Romain Guyac670c02010-07-27 17:39:27 -070051const char* gVS_Header_Uniforms =
Romain Guy39284b72012-09-26 16:39:40 -070052 "uniform mat4 projection;\n" \
Romain Guyac670c02010-07-27 17:39:27 -070053 "uniform mat4 transform;\n";
Romain Guyb4880042013-04-05 11:17:55 -070054const char* gVS_Header_Uniforms_HasGradient =
55 "uniform mat4 screenSpace;\n";
Romain Guy889f8d12010-07-29 14:37:42 -070056const char* gVS_Header_Uniforms_HasBitmap =
57 "uniform mat4 textureTransform;\n"
Romain Guy80bbfb12011-03-23 16:56:28 -070058 "uniform mediump vec2 textureDimension;\n";
Chris Craikdeeda3d2014-05-05 19:09:33 -070059const char* gVS_Header_Uniforms_HasRoundRectClip =
60 "uniform mat4 roundRectInvTransform;\n";
Romain Guyac670c02010-07-27 17:39:27 -070061const char* gVS_Header_Varyings_HasTexture =
62 "varying vec2 outTexCoords;\n";
Romain Guyff316ec2013-02-13 18:39:43 -080063const char* gVS_Header_Varyings_HasColors =
64 "varying vec4 outColors;\n";
Chris Craik91a8c7c2014-08-12 14:31:35 -070065const char* gVS_Header_Varyings_HasVertexAlpha =
Chris Craik6ebdc112012-08-31 18:24:33 -070066 "varying float alpha;\n";
Romain Guy63553472012-07-18 20:04:14 -070067const char* gVS_Header_Varyings_HasBitmap =
68 "varying highp vec2 outBitmapTexCoords;\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -070069const char* gVS_Header_Varyings_HasGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -070070 // Linear
Chet Haasea1d12dd2012-09-21 14:50:14 -070071 "varying highp vec2 linear;\n"
72 "varying vec2 ditherTexCoords;\n",
73 "varying float linear;\n"
74 "varying vec2 ditherTexCoords;\n",
Romain Guy211efea2012-07-31 21:16:07 -070075
Romain Guyee916f12010-09-20 17:53:08 -070076 // Circular
Chet Haasea1d12dd2012-09-21 14:50:14 -070077 "varying highp vec2 circular;\n"
78 "varying vec2 ditherTexCoords;\n",
79 "varying highp vec2 circular;\n"
80 "varying vec2 ditherTexCoords;\n",
Romain Guy211efea2012-07-31 21:16:07 -070081
Romain Guyee916f12010-09-20 17:53:08 -070082 // Sweep
Chet Haasea1d12dd2012-09-21 14:50:14 -070083 "varying highp vec2 sweep;\n"
84 "varying vec2 ditherTexCoords;\n",
85 "varying highp vec2 sweep;\n"
86 "varying vec2 ditherTexCoords;\n",
Romain Guyee916f12010-09-20 17:53:08 -070087};
Chris Craikdeeda3d2014-05-05 19:09:33 -070088const char* gVS_Header_Varyings_HasRoundRectClip =
Chris Craik68a73e82014-08-29 17:06:27 -070089 "varying highp vec2 roundRectPos;\n";
Romain Guyac670c02010-07-27 17:39:27 -070090const char* gVS_Main =
91 "\nvoid main(void) {\n";
92const char* gVS_Main_OutTexCoords =
93 " outTexCoords = texCoords;\n";
Romain Guyff316ec2013-02-13 18:39:43 -080094const char* gVS_Main_OutColors =
95 " outColors = colors;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -070096const char* gVS_Main_OutTransformedTexCoords =
97 " outTexCoords = (mainTextureTransform * vec4(texCoords, 0.0, 1.0)).xy;\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -070098const char* gVS_Main_OutGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -070099 // Linear
Chet Haasea1d12dd2012-09-21 14:50:14 -0700100 " linear = vec2((screenSpace * position).x, 0.5);\n"
Romain Guyb4880042013-04-05 11:17:55 -0700101 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Chet Haasea1d12dd2012-09-21 14:50:14 -0700102 " linear = (screenSpace * position).x;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700103 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Romain Guy211efea2012-07-31 21:16:07 -0700104
Romain Guyee916f12010-09-20 17:53:08 -0700105 // Circular
Chet Haasea1d12dd2012-09-21 14:50:14 -0700106 " circular = (screenSpace * position).xy;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700107 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Chet Haasea1d12dd2012-09-21 14:50:14 -0700108 " circular = (screenSpace * position).xy;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700109 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Romain Guy211efea2012-07-31 21:16:07 -0700110
Romain Guyee916f12010-09-20 17:53:08 -0700111 // Sweep
Chet Haasea1d12dd2012-09-21 14:50:14 -0700112 " sweep = (screenSpace * position).xy;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700113 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Chet Haasea1d12dd2012-09-21 14:50:14 -0700114 " sweep = (screenSpace * position).xy;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700115 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Romain Guyee916f12010-09-20 17:53:08 -0700116};
Romain Guy889f8d12010-07-29 14:37:42 -0700117const char* gVS_Main_OutBitmapTexCoords =
Romain Guy707b2f72010-10-11 16:34:59 -0700118 " outBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700119const char* gVS_Main_Position =
Chris Craikdeeda3d2014-05-05 19:09:33 -0700120 " vec4 transformedPosition = projection * transform * position;\n"
121 " gl_Position = transformedPosition;\n";
Chris Craikbf759452014-08-11 16:00:44 -0700122
Chris Craik91a8c7c2014-08-12 14:31:35 -0700123const char* gVS_Main_VertexAlpha =
Chris Craik6ebdc112012-08-31 18:24:33 -0700124 " alpha = vtxAlpha;\n";
Chris Craikbf759452014-08-11 16:00:44 -0700125
Chris Craikdeeda3d2014-05-05 19:09:33 -0700126const char* gVS_Main_HasRoundRectClip =
127 " roundRectPos = (roundRectInvTransform * transformedPosition).xy;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700128const char* gVS_Footer =
129 "}\n\n";
130
131///////////////////////////////////////////////////////////////////////////////
132// Fragment shaders snippets
133///////////////////////////////////////////////////////////////////////////////
134
Romain Guya5aed0d2010-09-09 14:42:43 -0700135const char* gFS_Header_Extension_FramebufferFetch =
136 "#extension GL_NV_shader_framebuffer_fetch : enable\n\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700137const char* gFS_Header_Extension_ExternalTexture =
138 "#extension GL_OES_EGL_image_external : require\n\n";
Romain Guyac670c02010-07-27 17:39:27 -0700139const char* gFS_Header =
140 "precision mediump float;\n\n";
141const char* gFS_Uniforms_Color =
142 "uniform vec4 color;\n";
143const char* gFS_Uniforms_TextureSampler =
Romain Guya938f562012-09-13 20:31:08 -0700144 "uniform sampler2D baseSampler;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700145const char* gFS_Uniforms_ExternalTextureSampler =
Romain Guya938f562012-09-13 20:31:08 -0700146 "uniform samplerExternalOES baseSampler;\n";
Romain Guyb4880042013-04-05 11:17:55 -0700147const char* gFS_Uniforms_Dither =
148 "uniform sampler2D ditherSampler;";
149const char* gFS_Uniforms_GradientSampler[2] = {
150 "%s\n"
151 "uniform sampler2D gradientSampler;\n",
152 "%s\n"
153 "uniform vec4 startColor;\n"
Romain Guy211efea2012-07-31 21:16:07 -0700154 "uniform vec4 endColor;\n"
Romain Guyee916f12010-09-20 17:53:08 -0700155};
Romain Guyac670c02010-07-27 17:39:27 -0700156const char* gFS_Uniforms_BitmapSampler =
157 "uniform sampler2D bitmapSampler;\n";
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500158const char* gFS_Uniforms_ColorOp[3] = {
Romain Guyac670c02010-07-27 17:39:27 -0700159 // None
160 "",
161 // Matrix
162 "uniform mat4 colorMatrix;\n"
163 "uniform vec4 colorMatrixVector;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700164 // PorterDuff
Romain Guydb1938e2010-08-02 18:50:22 -0700165 "uniform vec4 colorBlend;\n"
Romain Guyac670c02010-07-27 17:39:27 -0700166};
Romain Guy41210632012-07-16 17:04:24 -0700167const char* gFS_Uniforms_Gamma =
168 "uniform float gamma;\n";
169
Chris Craikdeeda3d2014-05-05 19:09:33 -0700170const char* gFS_Uniforms_HasRoundRectClip =
171 "uniform vec4 roundRectInnerRectLTRB;\n"
172 "uniform float roundRectRadius;\n";
173
Romain Guyac670c02010-07-27 17:39:27 -0700174const char* gFS_Main =
175 "\nvoid main(void) {\n"
Romain Guy7fbcc042010-08-04 15:40:07 -0700176 " lowp vec4 fragColor;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700177
Romain Guyb4880042013-04-05 11:17:55 -0700178const char* gFS_Main_Dither[2] = {
179 // ES 2.0
180 "texture2D(ditherSampler, ditherTexCoords).a * " STR(DITHER_KERNEL_SIZE_INV_SQUARE),
181 // ES 3.0
Romain Guy032d47a2013-04-08 19:45:40 -0700182 "texture2D(ditherSampler, ditherTexCoords).a"
Romain Guyb4880042013-04-05 11:17:55 -0700183};
Romain Guy211efea2012-07-31 21:16:07 -0700184const char* gFS_Main_AddDitherToGradient =
Romain Guyb4880042013-04-05 11:17:55 -0700185 " gradientColor += %s;\n";
Romain Guy211efea2012-07-31 21:16:07 -0700186
Romain Guy707b2f72010-10-11 16:34:59 -0700187// Fast cases
188const char* gFS_Fast_SingleColor =
189 "\nvoid main(void) {\n"
190 " gl_FragColor = color;\n"
191 "}\n\n";
192const char* gFS_Fast_SingleTexture =
193 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700194 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700195 "}\n\n";
196const char* gFS_Fast_SingleModulateTexture =
197 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700198 " gl_FragColor = color.a * texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700199 "}\n\n";
200const char* gFS_Fast_SingleA8Texture =
201 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700202 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700203 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700204const char* gFS_Fast_SingleA8Texture_ApplyGamma =
205 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700206 " gl_FragColor = vec4(0.0, 0.0, 0.0, pow(texture2D(baseSampler, outTexCoords).a, gamma));\n"
Romain Guy41210632012-07-16 17:04:24 -0700207 "}\n\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700208const char* gFS_Fast_SingleModulateA8Texture =
209 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700210 " gl_FragColor = color * texture2D(baseSampler, outTexCoords).a;\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700211 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700212const char* gFS_Fast_SingleModulateA8Texture_ApplyGamma =
213 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700214 " gl_FragColor = color * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy41210632012-07-16 17:04:24 -0700215 "}\n\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -0700216const char* gFS_Fast_SingleGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700217 "\nvoid main(void) {\n"
Romain Guyb4880042013-04-05 11:17:55 -0700218 " gl_FragColor = %s + texture2D(gradientSampler, linear);\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700219 "}\n\n",
220 "\nvoid main(void) {\n"
Romain Guyb4880042013-04-05 11:17:55 -0700221 " gl_FragColor = %s + mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
222 "}\n\n",
Romain Guy42e1e0d2012-07-30 14:47:51 -0700223};
224const char* gFS_Fast_SingleModulateGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700225 "\nvoid main(void) {\n"
Romain Guyb4880042013-04-05 11:17:55 -0700226 " gl_FragColor = %s + color.a * texture2D(gradientSampler, linear);\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700227 "}\n\n",
228 "\nvoid main(void) {\n"
Romain Guyb4880042013-04-05 11:17:55 -0700229 " gl_FragColor = %s + color.a * mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700230 "}\n\n"
231};
Romain Guy707b2f72010-10-11 16:34:59 -0700232
233// General case
Romain Guyac670c02010-07-27 17:39:27 -0700234const char* gFS_Main_FetchColor =
235 " fragColor = color;\n";
Romain Guy740bf2b2011-04-26 15:33:10 -0700236const char* gFS_Main_ModulateColor =
237 " fragColor *= color.a;\n";
Chris Craik91a8c7c2014-08-12 14:31:35 -0700238const char* gFS_Main_ApplyVertexAlphaLinearInterp =
Chris Craik6ebdc112012-08-31 18:24:33 -0700239 " fragColor *= alpha;\n";
Chris Craik91a8c7c2014-08-12 14:31:35 -0700240const char* gFS_Main_ApplyVertexAlphaShadowInterp =
ztenghuiecf091e2015-02-17 13:26:10 -0800241 // Use a gaussian function for the shadow fall off. Note that alpha here
242 // is actually (1.0 - alpha) for saving computation.
243 " fragColor *= exp(- alpha * alpha * 4.0) - 0.018;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700244const char* gFS_Main_FetchTexture[2] = {
245 // 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);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700249};
Romain Guya938f562012-09-13 20:31:08 -0700250const char* gFS_Main_FetchA8Texture[4] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700251 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700252 " fragColor = texture2D(baseSampler, outTexCoords);\n",
253 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700254 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700255 " fragColor = color * texture2D(baseSampler, outTexCoords).a;\n",
256 " fragColor = color * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700257};
Romain Guy42e1e0d2012-07-30 14:47:51 -0700258const char* gFS_Main_FetchGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700259 // Linear
Romain Guy320d46b2012-08-08 16:05:42 -0700260 " vec4 gradientColor = texture2D(gradientSampler, linear);\n",
Romain Guy211efea2012-07-31 21:16:07 -0700261
Romain Guy320d46b2012-08-08 16:05:42 -0700262 " vec4 gradientColor = mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700263
Romain Guyee916f12010-09-20 17:53:08 -0700264 // Circular
Romain Guy320d46b2012-08-08 16:05:42 -0700265 " vec4 gradientColor = texture2D(gradientSampler, vec2(length(circular), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700266
Romain Guy320d46b2012-08-08 16:05:42 -0700267 " vec4 gradientColor = mix(startColor, endColor, clamp(length(circular), 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700268
Romain Guyee916f12010-09-20 17:53:08 -0700269 // Sweep
Romain Guy63553472012-07-18 20:04:14 -0700270 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700271 " vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700272
Romain Guy42e1e0d2012-07-30 14:47:51 -0700273 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700274 " vec4 gradientColor = mix(startColor, endColor, clamp(index - floor(index), 0.0, 1.0));\n"
Romain Guyee916f12010-09-20 17:53:08 -0700275};
Romain Guyac670c02010-07-27 17:39:27 -0700276const char* gFS_Main_FetchBitmap =
277 " vec4 bitmapColor = texture2D(bitmapSampler, outBitmapTexCoords);\n";
Romain Guy889f8d12010-07-29 14:37:42 -0700278const char* gFS_Main_FetchBitmapNpot =
279 " vec4 bitmapColor = texture2D(bitmapSampler, wrap(outBitmapTexCoords));\n";
Romain Guyac670c02010-07-27 17:39:27 -0700280const char* gFS_Main_BlendShadersBG =
Romain Guyac670c02010-07-27 17:39:27 -0700281 " fragColor = blendShaders(gradientColor, bitmapColor)";
Romain Guy06f96e22010-07-30 19:18:16 -0700282const char* gFS_Main_BlendShadersGB =
283 " fragColor = blendShaders(bitmapColor, gradientColor)";
Romain Guya938f562012-09-13 20:31:08 -0700284const char* gFS_Main_BlendShaders_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700285 // Don't modulate
286 ";\n",
Romain Guya938f562012-09-13 20:31:08 -0700287 ";\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700288 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700289 " * color.a;\n",
290 " * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700291 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700292 " * texture2D(baseSampler, outTexCoords).a;\n",
293 " * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700294};
Romain Guya938f562012-09-13 20:31:08 -0700295const char* gFS_Main_GradientShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700296 // Don't modulate
297 " fragColor = gradientColor;\n",
Romain Guya938f562012-09-13 20:31:08 -0700298 " fragColor = gradientColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700299 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700300 " fragColor = gradientColor * color.a;\n",
301 " fragColor = gradientColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700302 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700303 " fragColor = gradientColor * texture2D(baseSampler, outTexCoords).a;\n",
304 " fragColor = gradientColor * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700305 };
Romain Guya938f562012-09-13 20:31:08 -0700306const char* gFS_Main_BitmapShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700307 // Don't modulate
308 " fragColor = bitmapColor;\n",
Romain Guya938f562012-09-13 20:31:08 -0700309 " fragColor = bitmapColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700310 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700311 " fragColor = bitmapColor * color.a;\n",
312 " fragColor = bitmapColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700313 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700314 " fragColor = bitmapColor * texture2D(baseSampler, outTexCoords).a;\n",
315 " fragColor = bitmapColor * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700316 };
Romain Guyac670c02010-07-27 17:39:27 -0700317const char* gFS_Main_FragColor =
318 " gl_FragColor = fragColor;\n";
Romain Guyff316ec2013-02-13 18:39:43 -0800319const char* gFS_Main_FragColor_HasColors =
320 " gl_FragColor *= outColors;\n";
Romain Guya5aed0d2010-09-09 14:42:43 -0700321const char* gFS_Main_FragColor_Blend =
322 " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n";
Romain Guyf607bdc2010-09-10 19:20:06 -0700323const char* gFS_Main_FragColor_Blend_Swap =
324 " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n";
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500325const char* gFS_Main_ApplyColorOp[3] = {
Romain Guyac670c02010-07-27 17:39:27 -0700326 // None
327 "",
328 // Matrix
Chris Craik73821c82014-09-16 17:32:13 -0700329 " fragColor.rgb /= (fragColor.a + 0.0019);\n" // un-premultiply
Romain Guyac670c02010-07-27 17:39:27 -0700330 " fragColor *= colorMatrix;\n"
Chris Craik73821c82014-09-16 17:32:13 -0700331 " fragColor += colorMatrixVector;\n"
332 " fragColor.rgb *= (fragColor.a + 0.0019);\n", // re-premultiply
Romain Guyac670c02010-07-27 17:39:27 -0700333 // PorterDuff
334 " fragColor = blendColors(colorBlend, fragColor);\n"
335};
Chris Craikdeeda3d2014-05-05 19:09:33 -0700336
337// Note: LTRB -> xyzw
338const char* gFS_Main_FragColor_HasRoundRectClip =
339 " mediump vec2 fragToLT = roundRectInnerRectLTRB.xy - roundRectPos;\n"
340 " mediump vec2 fragFromRB = roundRectPos - roundRectInnerRectLTRB.zw;\n"
Chris Craikf99f3202014-08-05 15:31:52 -0700341
342 // divide + multiply by 128 to avoid falling out of range in length() function
343 " mediump vec2 dist = max(max(fragToLT, fragFromRB), vec2(0.0, 0.0)) / 128.0;\n"
344 " mediump float linearDist = roundRectRadius - (length(dist) * 128.0);\n"
Chris Craikdeeda3d2014-05-05 19:09:33 -0700345 " gl_FragColor *= clamp(linearDist, 0.0, 1.0);\n";
346
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800347const char* gFS_Main_DebugHighlight =
348 " gl_FragColor.rgb = vec3(0.0, gl_FragColor.a, 0.0);\n";
Romain Guyac670c02010-07-27 17:39:27 -0700349const char* gFS_Footer =
350 "}\n\n";
351
352///////////////////////////////////////////////////////////////////////////////
353// PorterDuff snippets
354///////////////////////////////////////////////////////////////////////////////
355
Romain Guy48daa542010-08-10 19:21:34 -0700356const char* gBlendOps[18] = {
Romain Guyac670c02010-07-27 17:39:27 -0700357 // Clear
358 "return vec4(0.0, 0.0, 0.0, 0.0);\n",
359 // Src
360 "return src;\n",
361 // Dst
362 "return dst;\n",
363 // SrcOver
Romain Guy06f96e22010-07-30 19:18:16 -0700364 "return src + dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700365 // DstOver
Romain Guy06f96e22010-07-30 19:18:16 -0700366 "return dst + src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700367 // SrcIn
Romain Guy06f96e22010-07-30 19:18:16 -0700368 "return src * dst.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700369 // DstIn
Romain Guy06f96e22010-07-30 19:18:16 -0700370 "return dst * src.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700371 // SrcOut
Romain Guy06f96e22010-07-30 19:18:16 -0700372 "return src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700373 // DstOut
Romain Guy06f96e22010-07-30 19:18:16 -0700374 "return dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700375 // SrcAtop
376 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n",
377 // DstAtop
378 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n",
379 // Xor
Romain Guy48daa542010-08-10 19:21:34 -0700380 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, "
Romain Guyac670c02010-07-27 17:39:27 -0700381 "src.a + dst.a - 2.0 * src.a * dst.a);\n",
Derek Sollenbergerc0bf7002015-03-06 13:48:27 -0500382 // Plus
Romain Guy48daa542010-08-10 19:21:34 -0700383 "return min(src + dst, 1.0);\n",
Derek Sollenbergerc0bf7002015-03-06 13:48:27 -0500384 // Modulate
Romain Guy48daa542010-08-10 19:21:34 -0700385 "return src * dst;\n",
386 // Screen
387 "return src + dst - src * dst;\n",
388 // Overlay
389 "return clamp(vec4(mix("
390 "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), "
391 "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), "
392 "step(dst.a, 2.0 * dst.rgb)), "
393 "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n",
394 // Darken
395 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
396 "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
397 // Lighten
398 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
399 "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 -0700400};
401
402///////////////////////////////////////////////////////////////////////////////
403// Constructors/destructors
404///////////////////////////////////////////////////////////////////////////////
405
Chris Craik117bdbc2015-02-05 10:12:38 -0800406ProgramCache::ProgramCache(Extensions& extensions)
407 : mHasES3(extensions.getMajorGlVersion() >= 3) {
Romain Guyac670c02010-07-27 17:39:27 -0700408}
409
410ProgramCache::~ProgramCache() {
411 clear();
412}
413
414///////////////////////////////////////////////////////////////////////////////
415// Cache management
416///////////////////////////////////////////////////////////////////////////////
417
418void ProgramCache::clear() {
Romain Guy67f27952010-12-07 20:09:23 -0800419 PROGRAM_LOGD("Clearing program cache");
Romain Guyac670c02010-07-27 17:39:27 -0700420 mCache.clear();
421}
422
423Program* ProgramCache::get(const ProgramDescription& description) {
424 programid key = description.key();
Chris Craik096b8d92013-03-01 11:08:11 -0800425 if (key == (PROGRAM_KEY_TEXTURE | PROGRAM_KEY_A8_TEXTURE)) {
426 // program for A8, unmodulated, texture w/o shader (black text/path textures) is equivalent
427 // to standard texture program (bitmaps, patches). Consider them equivalent.
428 key = PROGRAM_KEY_TEXTURE;
429 }
430
Chris Craik51d6a3d2014-12-22 17:16:56 -0800431 auto iter = mCache.find(key);
Chris Craikd41c4d82015-01-05 15:51:13 -0800432 Program* program = nullptr;
Chris Craik51d6a3d2014-12-22 17:16:56 -0800433 if (iter == mCache.end()) {
Romain Guyee916f12010-09-20 17:53:08 -0700434 description.log("Could not find program");
Romain Guyac670c02010-07-27 17:39:27 -0700435 program = generateProgram(description, key);
Chris Craik51d6a3d2014-12-22 17:16:56 -0800436 mCache[key] = std::unique_ptr<Program>(program);
Romain Guyac670c02010-07-27 17:39:27 -0700437 } else {
Chris Craik51d6a3d2014-12-22 17:16:56 -0800438 program = iter->second.get();
Romain Guyac670c02010-07-27 17:39:27 -0700439 }
440 return program;
441}
442
443///////////////////////////////////////////////////////////////////////////////
444// Program generation
445///////////////////////////////////////////////////////////////////////////////
446
Andreas Gampe64bb4132014-11-22 00:35:09 +0000447Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) {
Romain Guyac670c02010-07-27 17:39:27 -0700448 String8 vertexShader = generateVertexShader(description);
449 String8 fragmentShader = generateFragmentShader(description);
450
Romain Guy42e1e0d2012-07-30 14:47:51 -0700451 return new Program(description, vertexShader.string(), fragmentShader.string());
452}
453
454static inline size_t gradientIndex(const ProgramDescription& description) {
455 return description.gradientType * 2 + description.isSimpleGradient;
Romain Guyac670c02010-07-27 17:39:27 -0700456}
457
458String8 ProgramCache::generateVertexShader(const ProgramDescription& description) {
459 // Add attributes
460 String8 shader(gVS_Header_Attributes);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700461 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700462 shader.append(gVS_Header_Attributes_TexCoords);
463 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700464 if (description.hasVertexAlpha) {
465 shader.append(gVS_Header_Attributes_VertexAlphaParameters);
Chet Haase5b0200b2011-04-13 17:58:08 -0700466 }
Romain Guyff316ec2013-02-13 18:39:43 -0800467 if (description.hasColors) {
468 shader.append(gVS_Header_Attributes_Colors);
469 }
Romain Guyac670c02010-07-27 17:39:27 -0700470 // Uniforms
471 shader.append(gVS_Header_Uniforms);
Romain Guy8f0095c2011-05-02 17:24:22 -0700472 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700473 shader.append(gVS_Header_Uniforms_TextureTransform);
474 }
Romain Guyac670c02010-07-27 17:39:27 -0700475 if (description.hasGradient) {
Romain Guyb4880042013-04-05 11:17:55 -0700476 shader.append(gVS_Header_Uniforms_HasGradient);
Romain Guyac670c02010-07-27 17:39:27 -0700477 }
Romain Guy889f8d12010-07-29 14:37:42 -0700478 if (description.hasBitmap) {
479 shader.append(gVS_Header_Uniforms_HasBitmap);
480 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700481 if (description.hasRoundRectClip) {
482 shader.append(gVS_Header_Uniforms_HasRoundRectClip);
483 }
Romain Guyac670c02010-07-27 17:39:27 -0700484 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700485 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700486 shader.append(gVS_Header_Varyings_HasTexture);
487 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700488 if (description.hasVertexAlpha) {
489 shader.append(gVS_Header_Varyings_HasVertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700490 }
Romain Guyff316ec2013-02-13 18:39:43 -0800491 if (description.hasColors) {
492 shader.append(gVS_Header_Varyings_HasColors);
493 }
Romain Guyac670c02010-07-27 17:39:27 -0700494 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700495 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700496 }
497 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700498 shader.append(gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700499 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700500 if (description.hasRoundRectClip) {
501 shader.append(gVS_Header_Varyings_HasRoundRectClip);
502 }
Romain Guyac670c02010-07-27 17:39:27 -0700503
504 // Begin the shader
505 shader.append(gVS_Main); {
Romain Guy8f0095c2011-05-02 17:24:22 -0700506 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700507 shader.append(gVS_Main_OutTransformedTexCoords);
Romain Guy8f0095c2011-05-02 17:24:22 -0700508 } else if (description.hasTexture || description.hasExternalTexture) {
509 shader.append(gVS_Main_OutTexCoords);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700510 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700511 if (description.hasVertexAlpha) {
512 shader.append(gVS_Main_VertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700513 }
Romain Guyff316ec2013-02-13 18:39:43 -0800514 if (description.hasColors) {
515 shader.append(gVS_Main_OutColors);
516 }
Romain Guy889f8d12010-07-29 14:37:42 -0700517 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700518 shader.append(gVS_Main_OutBitmapTexCoords);
Romain Guy889f8d12010-07-29 14:37:42 -0700519 }
Romain Guyac670c02010-07-27 17:39:27 -0700520 // Output transformed position
521 shader.append(gVS_Main_Position);
Chet Haasea1d12dd2012-09-21 14:50:14 -0700522 if (description.hasGradient) {
523 shader.append(gVS_Main_OutGradient[gradientIndex(description)]);
524 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700525 if (description.hasRoundRectClip) {
526 shader.append(gVS_Main_HasRoundRectClip);
527 }
Romain Guyac670c02010-07-27 17:39:27 -0700528 }
529 // End the shader
530 shader.append(gVS_Footer);
531
532 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string());
533
534 return shader;
535}
536
Romain Guya938f562012-09-13 20:31:08 -0700537static bool shaderOp(const ProgramDescription& description, String8& shader,
538 const int modulateOp, const char** snippets) {
539 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
540 op = op * 2 + description.hasGammaCorrection;
541 shader.append(snippets[op]);
542 return description.hasAlpha8Texture;
543}
544
Romain Guyac670c02010-07-27 17:39:27 -0700545String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) {
Romain Guya5aed0d2010-09-09 14:42:43 -0700546 String8 shader;
547
Romain Guy707b2f72010-10-11 16:34:59 -0700548 const bool blendFramebuffer = description.framebufferMode >= SkXfermode::kPlus_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700549 if (blendFramebuffer) {
550 shader.append(gFS_Header_Extension_FramebufferFetch);
551 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700552 if (description.hasExternalTexture) {
553 shader.append(gFS_Header_Extension_ExternalTexture);
554 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700555
556 shader.append(gFS_Header);
Romain Guyac670c02010-07-27 17:39:27 -0700557
558 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700559 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700560 shader.append(gVS_Header_Varyings_HasTexture);
561 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700562 if (description.hasVertexAlpha) {
563 shader.append(gVS_Header_Varyings_HasVertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700564 }
Romain Guyff316ec2013-02-13 18:39:43 -0800565 if (description.hasColors) {
566 shader.append(gVS_Header_Varyings_HasColors);
567 }
Romain Guyac670c02010-07-27 17:39:27 -0700568 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700569 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700570 }
571 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700572 shader.append(gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700573 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700574 if (description.hasRoundRectClip) {
575 shader.append(gVS_Header_Varyings_HasRoundRectClip);
576 }
Romain Guyac670c02010-07-27 17:39:27 -0700577
Romain Guyac670c02010-07-27 17:39:27 -0700578 // Uniforms
Romain Guy707b2f72010-10-11 16:34:59 -0700579 int modulateOp = MODULATE_OP_NO_MODULATE;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700580 const bool singleColor = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700581 !description.hasGradient && !description.hasBitmap;
582
583 if (description.modulate || singleColor) {
584 shader.append(gFS_Uniforms_Color);
585 if (!singleColor) modulateOp = MODULATE_OP_MODULATE;
586 }
Romain Guyac670c02010-07-27 17:39:27 -0700587 if (description.hasTexture) {
588 shader.append(gFS_Uniforms_TextureSampler);
Romain Guy8f0095c2011-05-02 17:24:22 -0700589 } else if (description.hasExternalTexture) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700590 shader.append(gFS_Uniforms_ExternalTextureSampler);
591 }
Romain Guyac670c02010-07-27 17:39:27 -0700592 if (description.hasGradient) {
Romain Guyb4880042013-04-05 11:17:55 -0700593 shader.appendFormat(gFS_Uniforms_GradientSampler[description.isSimpleGradient],
594 gFS_Uniforms_Dither);
Romain Guyac670c02010-07-27 17:39:27 -0700595 }
Romain Guy41210632012-07-16 17:04:24 -0700596 if (description.hasGammaCorrection) {
597 shader.append(gFS_Uniforms_Gamma);
598 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700599 if (description.hasRoundRectClip) {
600 shader.append(gFS_Uniforms_HasRoundRectClip);
601 }
Romain Guy707b2f72010-10-11 16:34:59 -0700602
603 // Optimization for common cases
Chris Craik91a8c7c2014-08-12 14:31:35 -0700604 if (!description.hasVertexAlpha
Chris Craikdeeda3d2014-05-05 19:09:33 -0700605 && !blendFramebuffer
606 && !description.hasColors
Chris Craikb9ce116d2015-08-20 15:14:06 -0700607 && description.colorOp == ProgramDescription::ColorFilterMode::None
Chris Craikdeeda3d2014-05-05 19:09:33 -0700608 && !description.hasDebugHighlight
Chris Craikdeeda3d2014-05-05 19:09:33 -0700609 && !description.hasRoundRectClip) {
Romain Guy707b2f72010-10-11 16:34:59 -0700610 bool fast = false;
611
612 const bool noShader = !description.hasGradient && !description.hasBitmap;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700613 const bool singleTexture = (description.hasTexture || description.hasExternalTexture) &&
Romain Guy707b2f72010-10-11 16:34:59 -0700614 !description.hasAlpha8Texture && noShader;
615 const bool singleA8Texture = description.hasTexture &&
616 description.hasAlpha8Texture && noShader;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700617 const bool singleGradient = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700618 description.hasGradient && !description.hasBitmap &&
619 description.gradientType == ProgramDescription::kGradientLinear;
620
621 if (singleColor) {
622 shader.append(gFS_Fast_SingleColor);
623 fast = true;
624 } else if (singleTexture) {
625 if (!description.modulate) {
626 shader.append(gFS_Fast_SingleTexture);
627 } else {
628 shader.append(gFS_Fast_SingleModulateTexture);
629 }
630 fast = true;
631 } else if (singleA8Texture) {
632 if (!description.modulate) {
Romain Guy41210632012-07-16 17:04:24 -0700633 if (description.hasGammaCorrection) {
634 shader.append(gFS_Fast_SingleA8Texture_ApplyGamma);
635 } else {
636 shader.append(gFS_Fast_SingleA8Texture);
637 }
Romain Guy707b2f72010-10-11 16:34:59 -0700638 } else {
Romain Guy41210632012-07-16 17:04:24 -0700639 if (description.hasGammaCorrection) {
640 shader.append(gFS_Fast_SingleModulateA8Texture_ApplyGamma);
641 } else {
642 shader.append(gFS_Fast_SingleModulateA8Texture);
643 }
Romain Guy707b2f72010-10-11 16:34:59 -0700644 }
645 fast = true;
646 } else if (singleGradient) {
647 if (!description.modulate) {
Romain Guyb4880042013-04-05 11:17:55 -0700648 shader.appendFormat(gFS_Fast_SingleGradient[description.isSimpleGradient],
649 gFS_Main_Dither[mHasES3]);
Romain Guy707b2f72010-10-11 16:34:59 -0700650 } else {
Romain Guyb4880042013-04-05 11:17:55 -0700651 shader.appendFormat(gFS_Fast_SingleModulateGradient[description.isSimpleGradient],
652 gFS_Main_Dither[mHasES3]);
Romain Guy707b2f72010-10-11 16:34:59 -0700653 }
654 fast = true;
655 }
656
657 if (fast) {
Romain Guyc15008e2010-11-10 11:59:15 -0800658#if DEBUG_PROGRAMS
Romain Guy707b2f72010-10-11 16:34:59 -0700659 PROGRAM_LOGD("*** Fast case:\n");
660 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
661 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800662#endif
Romain Guy707b2f72010-10-11 16:34:59 -0700663
664 return shader;
665 }
666 }
667
Romain Guyac670c02010-07-27 17:39:27 -0700668 if (description.hasBitmap) {
669 shader.append(gFS_Uniforms_BitmapSampler);
670 }
Chris Craikb9ce116d2015-08-20 15:14:06 -0700671 shader.append(gFS_Uniforms_ColorOp[static_cast<int>(description.colorOp)]);
Romain Guyac670c02010-07-27 17:39:27 -0700672
673 // Generate required functions
674 if (description.hasGradient && description.hasBitmap) {
Romain Guy48daa542010-08-10 19:21:34 -0700675 generateBlend(shader, "blendShaders", description.shadersMode);
Romain Guyac670c02010-07-27 17:39:27 -0700676 }
Chris Craikb9ce116d2015-08-20 15:14:06 -0700677 if (description.colorOp == ProgramDescription::ColorFilterMode::Blend) {
Romain Guy48daa542010-08-10 19:21:34 -0700678 generateBlend(shader, "blendColors", description.colorMode);
Romain Guyac670c02010-07-27 17:39:27 -0700679 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700680 if (blendFramebuffer) {
681 generateBlend(shader, "blendFramebuffer", description.framebufferMode);
682 }
Romain Guy889f8d12010-07-29 14:37:42 -0700683 if (description.isBitmapNpot) {
684 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
685 }
Romain Guyac670c02010-07-27 17:39:27 -0700686
687 // Begin the shader
688 shader.append(gFS_Main); {
689 // Stores the result in fragColor directly
Romain Guyaa6c24c2011-04-28 18:40:04 -0700690 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700691 if (description.hasAlpha8Texture) {
Romain Guy707b2f72010-10-11 16:34:59 -0700692 if (!description.hasGradient && !description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700693 shader.append(gFS_Main_FetchA8Texture[modulateOp * 2 +
694 description.hasGammaCorrection]);
Romain Guy707b2f72010-10-11 16:34:59 -0700695 }
Romain Guyac670c02010-07-27 17:39:27 -0700696 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700697 shader.append(gFS_Main_FetchTexture[modulateOp]);
Romain Guyac670c02010-07-27 17:39:27 -0700698 }
699 } else {
Romain Guya938f562012-09-13 20:31:08 -0700700 if (!description.hasGradient && !description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700701 shader.append(gFS_Main_FetchColor);
702 }
Romain Guyac670c02010-07-27 17:39:27 -0700703 }
704 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700705 shader.append(gFS_Main_FetchGradient[gradientIndex(description)]);
Romain Guyb4880042013-04-05 11:17:55 -0700706 shader.appendFormat(gFS_Main_AddDitherToGradient, gFS_Main_Dither[mHasES3]);
Romain Guyac670c02010-07-27 17:39:27 -0700707 }
708 if (description.hasBitmap) {
Romain Guy889f8d12010-07-29 14:37:42 -0700709 if (!description.isBitmapNpot) {
710 shader.append(gFS_Main_FetchBitmap);
711 } else {
712 shader.append(gFS_Main_FetchBitmapNpot);
713 }
Romain Guyac670c02010-07-27 17:39:27 -0700714 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700715 bool applyModulate = false;
Romain Guyac670c02010-07-27 17:39:27 -0700716 // Case when we have two shaders set
717 if (description.hasGradient && description.hasBitmap) {
718 if (description.isBitmapFirst) {
719 shader.append(gFS_Main_BlendShadersBG);
720 } else {
721 shader.append(gFS_Main_BlendShadersGB);
722 }
Romain Guya938f562012-09-13 20:31:08 -0700723 applyModulate = shaderOp(description, shader, modulateOp,
724 gFS_Main_BlendShaders_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700725 } else {
726 if (description.hasGradient) {
Romain Guya938f562012-09-13 20:31:08 -0700727 applyModulate = shaderOp(description, shader, modulateOp,
728 gFS_Main_GradientShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700729 } else if (description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700730 applyModulate = shaderOp(description, shader, modulateOp,
731 gFS_Main_BitmapShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700732 }
Romain Guyac670c02010-07-27 17:39:27 -0700733 }
Romain Guya938f562012-09-13 20:31:08 -0700734
Romain Guy740bf2b2011-04-26 15:33:10 -0700735 if (description.modulate && applyModulate) {
Romain Guya938f562012-09-13 20:31:08 -0700736 shader.append(gFS_Main_ModulateColor);
Romain Guy740bf2b2011-04-26 15:33:10 -0700737 }
Romain Guya938f562012-09-13 20:31:08 -0700738
Romain Guyac670c02010-07-27 17:39:27 -0700739 // Apply the color op if needed
Chris Craikb9ce116d2015-08-20 15:14:06 -0700740 shader.append(gFS_Main_ApplyColorOp[static_cast<int>(description.colorOp)]);
Chris Craik9f44a132012-09-13 18:34:55 -0700741
Chris Craik91a8c7c2014-08-12 14:31:35 -0700742 if (description.hasVertexAlpha) {
743 if (description.useShadowAlphaInterp) {
744 shader.append(gFS_Main_ApplyVertexAlphaShadowInterp);
Chris Craikbf759452014-08-11 16:00:44 -0700745 } else {
Chris Craik91a8c7c2014-08-12 14:31:35 -0700746 shader.append(gFS_Main_ApplyVertexAlphaLinearInterp);
Chris Craikbf759452014-08-11 16:00:44 -0700747 }
Chris Craik9f44a132012-09-13 18:34:55 -0700748 }
749
Romain Guyac670c02010-07-27 17:39:27 -0700750 // Output the fragment
Romain Guya5aed0d2010-09-09 14:42:43 -0700751 if (!blendFramebuffer) {
752 shader.append(gFS_Main_FragColor);
753 } else {
Romain Guyf607bdc2010-09-10 19:20:06 -0700754 shader.append(!description.swapSrcDst ?
755 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap);
Romain Guya5aed0d2010-09-09 14:42:43 -0700756 }
Romain Guyff316ec2013-02-13 18:39:43 -0800757 if (description.hasColors) {
758 shader.append(gFS_Main_FragColor_HasColors);
759 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700760 if (description.hasRoundRectClip) {
761 shader.append(gFS_Main_FragColor_HasRoundRectClip);
762 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800763 if (description.hasDebugHighlight) {
764 shader.append(gFS_Main_DebugHighlight);
765 }
Romain Guyac670c02010-07-27 17:39:27 -0700766 }
767 // End the shader
768 shader.append(gFS_Footer);
769
Romain Guyc15008e2010-11-10 11:59:15 -0800770#if DEBUG_PROGRAMS
Romain Guydb1938e2010-08-02 18:50:22 -0700771 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
772 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800773#endif
Romain Guydb1938e2010-08-02 18:50:22 -0700774
Romain Guyac670c02010-07-27 17:39:27 -0700775 return shader;
776}
777
Romain Guy48daa542010-08-10 19:21:34 -0700778void ProgramCache::generateBlend(String8& shader, const char* name, SkXfermode::Mode mode) {
Romain Guyac670c02010-07-27 17:39:27 -0700779 shader.append("\nvec4 ");
780 shader.append(name);
781 shader.append("(vec4 src, vec4 dst) {\n");
782 shader.append(" ");
Romain Guy48daa542010-08-10 19:21:34 -0700783 shader.append(gBlendOps[mode]);
Romain Guyac670c02010-07-27 17:39:27 -0700784 shader.append("}\n");
785}
786
Romain Guy889f8d12010-07-29 14:37:42 -0700787void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) {
Romain Guy63553472012-07-18 20:04:14 -0700788 shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700789 if (wrapS == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700790 shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700791 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n");
792 }
793 if (wrapT == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700794 shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700795 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n");
796 }
797 shader.append(" return vec2(");
798 switch (wrapS) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700799 case GL_CLAMP_TO_EDGE:
800 shader.append("texCoords.x");
801 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700802 case GL_REPEAT:
803 shader.append("mod(texCoords.x, 1.0)");
804 break;
805 case GL_MIRRORED_REPEAT:
806 shader.append("xMod2");
807 break;
808 }
809 shader.append(", ");
810 switch (wrapT) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700811 case GL_CLAMP_TO_EDGE:
812 shader.append("texCoords.y");
813 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700814 case GL_REPEAT:
815 shader.append("mod(texCoords.y, 1.0)");
816 break;
817 case GL_MIRRORED_REPEAT:
818 shader.append("yMod2");
819 break;
820 }
821 shader.append(");\n");
822 shader.append("}\n");
823}
824
Romain Guydb1938e2010-08-02 18:50:22 -0700825void ProgramCache::printLongString(const String8& shader) const {
826 ssize_t index = 0;
827 ssize_t lastIndex = 0;
828 const char* str = shader.string();
829 while ((index = shader.find("\n", index)) > -1) {
830 String8 line(str, index - lastIndex);
831 if (line.length() == 0) line.append("\n");
Chris Craik1c1c3fe2015-03-06 09:40:35 -0800832 ALOGD("%s", line.string());
Romain Guydb1938e2010-08-02 18:50:22 -0700833 index++;
834 str += (index - lastIndex);
835 lastIndex = index;
836 }
837}
838
Romain Guyac670c02010-07-27 17:39:27 -0700839}; // namespace uirenderer
840}; // namespace android