blob: 2dd89b857107650bb1cf7096db7c1bce08443736 [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
17#define LOG_TAG "OpenGLRenderer"
18
19#include <utils/String8.h>
20
Romain Guya60c3882011-08-01 15:28:16 -070021#include "Caches.h"
Romain Guyb4880042013-04-05 11:17:55 -070022#include "Dither.h"
Romain Guyac670c02010-07-27 17:39:27 -070023#include "ProgramCache.h"
24
25namespace android {
26namespace uirenderer {
27
28///////////////////////////////////////////////////////////////////////////////
Romain Guy707b2f72010-10-11 16:34:59 -070029// Defines
30///////////////////////////////////////////////////////////////////////////////
31
32#define MODULATE_OP_NO_MODULATE 0
33#define MODULATE_OP_MODULATE 1
34#define MODULATE_OP_MODULATE_A8 2
35
Romain Guyb4880042013-04-05 11:17:55 -070036#define STR(x) STR1(x)
37#define STR1(x) #x
38
Romain Guy707b2f72010-10-11 16:34:59 -070039///////////////////////////////////////////////////////////////////////////////
Romain Guyac670c02010-07-27 17:39:27 -070040// Vertex shaders snippets
41///////////////////////////////////////////////////////////////////////////////
42
Romain Guyac670c02010-07-27 17:39:27 -070043const char* gVS_Header_Attributes =
44 "attribute vec4 position;\n";
45const char* gVS_Header_Attributes_TexCoords =
46 "attribute vec2 texCoords;\n";
Romain Guyff316ec2013-02-13 18:39:43 -080047const char* gVS_Header_Attributes_Colors =
48 "attribute vec4 colors;\n";
Chris Craik710f46d2012-09-17 17:25:49 -070049const char* gVS_Header_Attributes_AAVertexShapeParameters =
Chris Craik6ebdc112012-08-31 18:24:33 -070050 "attribute float vtxAlpha;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -070051const char* gVS_Header_Uniforms_TextureTransform =
52 "uniform mat4 mainTextureTransform;\n";
Romain Guyac670c02010-07-27 17:39:27 -070053const char* gVS_Header_Uniforms =
Romain Guy39284b72012-09-26 16:39:40 -070054 "uniform mat4 projection;\n" \
Romain Guyac670c02010-07-27 17:39:27 -070055 "uniform mat4 transform;\n";
Romain Guyb4880042013-04-05 11:17:55 -070056const char* gVS_Header_Uniforms_HasGradient =
57 "uniform mat4 screenSpace;\n";
Romain Guy889f8d12010-07-29 14:37:42 -070058const char* gVS_Header_Uniforms_HasBitmap =
59 "uniform mat4 textureTransform;\n"
Romain Guy80bbfb12011-03-23 16:56:28 -070060 "uniform mediump vec2 textureDimension;\n";
Chris Craikdeeda3d2014-05-05 19:09:33 -070061const char* gVS_Header_Uniforms_HasRoundRectClip =
62 "uniform mat4 roundRectInvTransform;\n";
Romain Guyac670c02010-07-27 17:39:27 -070063const char* gVS_Header_Varyings_HasTexture =
64 "varying vec2 outTexCoords;\n";
Romain Guyff316ec2013-02-13 18:39:43 -080065const char* gVS_Header_Varyings_HasColors =
66 "varying vec4 outColors;\n";
Chris Craik710f46d2012-09-17 17:25:49 -070067const char* gVS_Header_Varyings_IsAAVertexShape =
Chris Craik6ebdc112012-08-31 18:24:33 -070068 "varying float alpha;\n";
Romain Guy63553472012-07-18 20:04:14 -070069const char* gVS_Header_Varyings_HasBitmap =
70 "varying highp vec2 outBitmapTexCoords;\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -070071const char* gVS_Header_Varyings_HasGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -070072 // Linear
Chet Haasea1d12dd2012-09-21 14:50:14 -070073 "varying highp vec2 linear;\n"
74 "varying vec2 ditherTexCoords;\n",
75 "varying float linear;\n"
76 "varying vec2 ditherTexCoords;\n",
Romain Guy211efea2012-07-31 21:16:07 -070077
Romain Guyee916f12010-09-20 17:53:08 -070078 // Circular
Chet Haasea1d12dd2012-09-21 14:50:14 -070079 "varying highp vec2 circular;\n"
80 "varying vec2 ditherTexCoords;\n",
81 "varying highp vec2 circular;\n"
82 "varying vec2 ditherTexCoords;\n",
Romain Guy211efea2012-07-31 21:16:07 -070083
Romain Guyee916f12010-09-20 17:53:08 -070084 // Sweep
Chet Haasea1d12dd2012-09-21 14:50:14 -070085 "varying highp vec2 sweep;\n"
86 "varying vec2 ditherTexCoords;\n",
87 "varying highp vec2 sweep;\n"
88 "varying vec2 ditherTexCoords;\n",
Romain Guyee916f12010-09-20 17:53:08 -070089};
Chris Craikdeeda3d2014-05-05 19:09:33 -070090const char* gVS_Header_Varyings_HasRoundRectClip =
91 "varying vec2 roundRectPos;\n";
Romain Guyac670c02010-07-27 17:39:27 -070092const char* gVS_Main =
93 "\nvoid main(void) {\n";
94const char* gVS_Main_OutTexCoords =
95 " outTexCoords = texCoords;\n";
Romain Guyff316ec2013-02-13 18:39:43 -080096const char* gVS_Main_OutColors =
97 " outColors = colors;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -070098const char* gVS_Main_OutTransformedTexCoords =
99 " outTexCoords = (mainTextureTransform * vec4(texCoords, 0.0, 1.0)).xy;\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -0700100const char* gVS_Main_OutGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700101 // Linear
Chet Haasea1d12dd2012-09-21 14:50:14 -0700102 " linear = vec2((screenSpace * position).x, 0.5);\n"
Romain Guyb4880042013-04-05 11:17:55 -0700103 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Chet Haasea1d12dd2012-09-21 14:50:14 -0700104 " linear = (screenSpace * position).x;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700105 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Romain Guy211efea2012-07-31 21:16:07 -0700106
Romain Guyee916f12010-09-20 17:53:08 -0700107 // Circular
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",
Chet Haasea1d12dd2012-09-21 14:50:14 -0700110 " circular = (screenSpace * position).xy;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700111 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Romain Guy211efea2012-07-31 21:16:07 -0700112
Romain Guyee916f12010-09-20 17:53:08 -0700113 // Sweep
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",
Chet Haasea1d12dd2012-09-21 14:50:14 -0700116 " sweep = (screenSpace * position).xy;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700117 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Romain Guyee916f12010-09-20 17:53:08 -0700118};
Romain Guy889f8d12010-07-29 14:37:42 -0700119const char* gVS_Main_OutBitmapTexCoords =
Romain Guy707b2f72010-10-11 16:34:59 -0700120 " outBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700121const char* gVS_Main_Position =
Chris Craikdeeda3d2014-05-05 19:09:33 -0700122 " vec4 transformedPosition = projection * transform * position;\n"
123 " gl_Position = transformedPosition;\n";
Chris Craikbf759452014-08-11 16:00:44 -0700124
125const char* gVS_Main_ShadowAAVertexShape =
126 " alpha = pow(vtxAlpha, 0.667);\n";
Chris Craik710f46d2012-09-17 17:25:49 -0700127const char* gVS_Main_AAVertexShape =
Chris Craik6ebdc112012-08-31 18:24:33 -0700128 " alpha = vtxAlpha;\n";
Chris Craikbf759452014-08-11 16:00:44 -0700129
Chris Craikdeeda3d2014-05-05 19:09:33 -0700130const char* gVS_Main_HasRoundRectClip =
131 " roundRectPos = (roundRectInvTransform * transformedPosition).xy;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700132const char* gVS_Footer =
133 "}\n\n";
134
135///////////////////////////////////////////////////////////////////////////////
136// Fragment shaders snippets
137///////////////////////////////////////////////////////////////////////////////
138
Romain Guya5aed0d2010-09-09 14:42:43 -0700139const char* gFS_Header_Extension_FramebufferFetch =
140 "#extension GL_NV_shader_framebuffer_fetch : enable\n\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700141const char* gFS_Header_Extension_ExternalTexture =
142 "#extension GL_OES_EGL_image_external : require\n\n";
Romain Guyac670c02010-07-27 17:39:27 -0700143const char* gFS_Header =
144 "precision mediump float;\n\n";
145const char* gFS_Uniforms_Color =
146 "uniform vec4 color;\n";
147const char* gFS_Uniforms_TextureSampler =
Romain Guya938f562012-09-13 20:31:08 -0700148 "uniform sampler2D baseSampler;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700149const char* gFS_Uniforms_ExternalTextureSampler =
Romain Guya938f562012-09-13 20:31:08 -0700150 "uniform samplerExternalOES baseSampler;\n";
Romain Guyb4880042013-04-05 11:17:55 -0700151const char* gFS_Uniforms_Dither =
152 "uniform sampler2D ditherSampler;";
153const char* gFS_Uniforms_GradientSampler[2] = {
154 "%s\n"
155 "uniform sampler2D gradientSampler;\n",
156 "%s\n"
157 "uniform vec4 startColor;\n"
Romain Guy211efea2012-07-31 21:16:07 -0700158 "uniform vec4 endColor;\n"
Romain Guyee916f12010-09-20 17:53:08 -0700159};
Romain Guyac670c02010-07-27 17:39:27 -0700160const char* gFS_Uniforms_BitmapSampler =
161 "uniform sampler2D bitmapSampler;\n";
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500162const char* gFS_Uniforms_ColorOp[3] = {
Romain Guyac670c02010-07-27 17:39:27 -0700163 // None
164 "",
165 // Matrix
166 "uniform mat4 colorMatrix;\n"
167 "uniform vec4 colorMatrixVector;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700168 // PorterDuff
Romain Guydb1938e2010-08-02 18:50:22 -0700169 "uniform vec4 colorBlend;\n"
Romain Guyac670c02010-07-27 17:39:27 -0700170};
Romain Guy41210632012-07-16 17:04:24 -0700171const char* gFS_Uniforms_Gamma =
172 "uniform float gamma;\n";
173
Chris Craikdeeda3d2014-05-05 19:09:33 -0700174const char* gFS_Uniforms_HasRoundRectClip =
175 "uniform vec4 roundRectInnerRectLTRB;\n"
176 "uniform float roundRectRadius;\n";
177
Romain Guyac670c02010-07-27 17:39:27 -0700178const char* gFS_Main =
179 "\nvoid main(void) {\n"
Romain Guy7fbcc042010-08-04 15:40:07 -0700180 " lowp vec4 fragColor;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700181
Romain Guyb4880042013-04-05 11:17:55 -0700182const char* gFS_Main_Dither[2] = {
183 // ES 2.0
184 "texture2D(ditherSampler, ditherTexCoords).a * " STR(DITHER_KERNEL_SIZE_INV_SQUARE),
185 // ES 3.0
Romain Guy032d47a2013-04-08 19:45:40 -0700186 "texture2D(ditherSampler, ditherTexCoords).a"
Romain Guyb4880042013-04-05 11:17:55 -0700187};
Romain Guy211efea2012-07-31 21:16:07 -0700188const char* gFS_Main_AddDitherToGradient =
Romain Guyb4880042013-04-05 11:17:55 -0700189 " gradientColor += %s;\n";
Romain Guy211efea2012-07-31 21:16:07 -0700190
Romain Guy707b2f72010-10-11 16:34:59 -0700191// Fast cases
192const char* gFS_Fast_SingleColor =
193 "\nvoid main(void) {\n"
194 " gl_FragColor = color;\n"
195 "}\n\n";
196const char* gFS_Fast_SingleTexture =
197 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700198 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700199 "}\n\n";
200const char* gFS_Fast_SingleModulateTexture =
201 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700202 " gl_FragColor = color.a * texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700203 "}\n\n";
204const char* gFS_Fast_SingleA8Texture =
205 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700206 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700207 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700208const char* gFS_Fast_SingleA8Texture_ApplyGamma =
209 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700210 " gl_FragColor = vec4(0.0, 0.0, 0.0, pow(texture2D(baseSampler, outTexCoords).a, gamma));\n"
Romain Guy41210632012-07-16 17:04:24 -0700211 "}\n\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700212const char* gFS_Fast_SingleModulateA8Texture =
213 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700214 " gl_FragColor = color * texture2D(baseSampler, outTexCoords).a;\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700215 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700216const char* gFS_Fast_SingleModulateA8Texture_ApplyGamma =
217 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700218 " gl_FragColor = color * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy41210632012-07-16 17:04:24 -0700219 "}\n\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -0700220const char* gFS_Fast_SingleGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700221 "\nvoid main(void) {\n"
Romain Guyb4880042013-04-05 11:17:55 -0700222 " gl_FragColor = %s + texture2D(gradientSampler, linear);\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700223 "}\n\n",
224 "\nvoid main(void) {\n"
Romain Guyb4880042013-04-05 11:17:55 -0700225 " gl_FragColor = %s + mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
226 "}\n\n",
Romain Guy42e1e0d2012-07-30 14:47:51 -0700227};
228const char* gFS_Fast_SingleModulateGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700229 "\nvoid main(void) {\n"
Romain Guyb4880042013-04-05 11:17:55 -0700230 " gl_FragColor = %s + color.a * texture2D(gradientSampler, linear);\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700231 "}\n\n",
232 "\nvoid main(void) {\n"
Romain Guyb4880042013-04-05 11:17:55 -0700233 " gl_FragColor = %s + color.a * mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700234 "}\n\n"
235};
Romain Guy707b2f72010-10-11 16:34:59 -0700236
237// General case
Romain Guyac670c02010-07-27 17:39:27 -0700238const char* gFS_Main_FetchColor =
239 " fragColor = color;\n";
Romain Guy740bf2b2011-04-26 15:33:10 -0700240const char* gFS_Main_ModulateColor =
241 " fragColor *= color.a;\n";
Chris Craik710f46d2012-09-17 17:25:49 -0700242const char* gFS_Main_AccountForAAVertexShape =
Chris Craik6ebdc112012-08-31 18:24:33 -0700243 " fragColor *= alpha;\n";
Chris Craikbf759452014-08-11 16:00:44 -0700244const char* gFS_Main_AccountForShadowAAVertexShape =
245 " fragColor *= pow(alpha, 1.5);\n";
Chris Craika798b952012-08-27 17:03:13 -0700246
Romain Guy707b2f72010-10-11 16:34:59 -0700247const char* gFS_Main_FetchTexture[2] = {
248 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700249 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700250 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700251 " fragColor = color * texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700252};
Romain Guya938f562012-09-13 20:31:08 -0700253const char* gFS_Main_FetchA8Texture[4] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700254 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700255 " fragColor = texture2D(baseSampler, outTexCoords);\n",
256 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700257 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700258 " fragColor = color * texture2D(baseSampler, outTexCoords).a;\n",
259 " fragColor = color * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700260};
Romain Guy42e1e0d2012-07-30 14:47:51 -0700261const char* gFS_Main_FetchGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700262 // Linear
Romain Guy320d46b2012-08-08 16:05:42 -0700263 " vec4 gradientColor = texture2D(gradientSampler, linear);\n",
Romain Guy211efea2012-07-31 21:16:07 -0700264
Romain Guy320d46b2012-08-08 16:05:42 -0700265 " vec4 gradientColor = mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700266
Romain Guyee916f12010-09-20 17:53:08 -0700267 // Circular
Romain Guy320d46b2012-08-08 16:05:42 -0700268 " vec4 gradientColor = texture2D(gradientSampler, vec2(length(circular), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700269
Romain Guy320d46b2012-08-08 16:05:42 -0700270 " vec4 gradientColor = mix(startColor, endColor, clamp(length(circular), 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700271
Romain Guyee916f12010-09-20 17:53:08 -0700272 // Sweep
Romain Guy63553472012-07-18 20:04:14 -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 = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700275
Romain Guy42e1e0d2012-07-30 14:47:51 -0700276 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700277 " vec4 gradientColor = mix(startColor, endColor, clamp(index - floor(index), 0.0, 1.0));\n"
Romain Guyee916f12010-09-20 17:53:08 -0700278};
Romain Guyac670c02010-07-27 17:39:27 -0700279const char* gFS_Main_FetchBitmap =
280 " vec4 bitmapColor = texture2D(bitmapSampler, outBitmapTexCoords);\n";
Romain Guy889f8d12010-07-29 14:37:42 -0700281const char* gFS_Main_FetchBitmapNpot =
282 " vec4 bitmapColor = texture2D(bitmapSampler, wrap(outBitmapTexCoords));\n";
Romain Guyac670c02010-07-27 17:39:27 -0700283const char* gFS_Main_BlendShadersBG =
Romain Guyac670c02010-07-27 17:39:27 -0700284 " fragColor = blendShaders(gradientColor, bitmapColor)";
Romain Guy06f96e22010-07-30 19:18:16 -0700285const char* gFS_Main_BlendShadersGB =
286 " fragColor = blendShaders(bitmapColor, gradientColor)";
Romain Guya938f562012-09-13 20:31:08 -0700287const char* gFS_Main_BlendShaders_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700288 // Don't modulate
289 ";\n",
Romain Guya938f562012-09-13 20:31:08 -0700290 ";\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700291 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700292 " * color.a;\n",
293 " * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700294 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700295 " * texture2D(baseSampler, outTexCoords).a;\n",
296 " * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700297};
Romain Guya938f562012-09-13 20:31:08 -0700298const char* gFS_Main_GradientShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700299 // Don't modulate
300 " fragColor = gradientColor;\n",
Romain Guya938f562012-09-13 20:31:08 -0700301 " fragColor = gradientColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700302 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700303 " fragColor = gradientColor * color.a;\n",
304 " fragColor = gradientColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700305 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700306 " fragColor = gradientColor * texture2D(baseSampler, outTexCoords).a;\n",
307 " fragColor = gradientColor * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700308 };
Romain Guya938f562012-09-13 20:31:08 -0700309const char* gFS_Main_BitmapShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700310 // Don't modulate
311 " fragColor = bitmapColor;\n",
Romain Guya938f562012-09-13 20:31:08 -0700312 " fragColor = bitmapColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700313 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700314 " fragColor = bitmapColor * color.a;\n",
315 " fragColor = bitmapColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700316 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700317 " fragColor = bitmapColor * texture2D(baseSampler, outTexCoords).a;\n",
318 " fragColor = bitmapColor * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700319 };
Romain Guyac670c02010-07-27 17:39:27 -0700320const char* gFS_Main_FragColor =
321 " gl_FragColor = fragColor;\n";
Romain Guyff316ec2013-02-13 18:39:43 -0800322const char* gFS_Main_FragColor_HasColors =
323 " gl_FragColor *= outColors;\n";
Romain Guya5aed0d2010-09-09 14:42:43 -0700324const char* gFS_Main_FragColor_Blend =
325 " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n";
Romain Guyf607bdc2010-09-10 19:20:06 -0700326const char* gFS_Main_FragColor_Blend_Swap =
327 " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n";
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500328const char* gFS_Main_ApplyColorOp[3] = {
Romain Guyac670c02010-07-27 17:39:27 -0700329 // None
330 "",
331 // Matrix
332 " fragColor *= colorMatrix;\n"
Romain Guydb1938e2010-08-02 18:50:22 -0700333 " fragColor += colorMatrixVector;\n"
334 " fragColor.rgb *= fragColor.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700335 // PorterDuff
336 " fragColor = blendColors(colorBlend, fragColor);\n"
337};
Chris Craikdeeda3d2014-05-05 19:09:33 -0700338
339// Note: LTRB -> xyzw
340const char* gFS_Main_FragColor_HasRoundRectClip =
341 " mediump vec2 fragToLT = roundRectInnerRectLTRB.xy - roundRectPos;\n"
342 " mediump vec2 fragFromRB = roundRectPos - roundRectInnerRectLTRB.zw;\n"
Chris Craikf99f3202014-08-05 15:31:52 -0700343
344 // divide + multiply by 128 to avoid falling out of range in length() function
345 " mediump vec2 dist = max(max(fragToLT, fragFromRB), vec2(0.0, 0.0)) / 128.0;\n"
346 " mediump float linearDist = roundRectRadius - (length(dist) * 128.0);\n"
Chris Craikdeeda3d2014-05-05 19:09:33 -0700347 " gl_FragColor *= clamp(linearDist, 0.0, 1.0);\n";
348
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800349const char* gFS_Main_DebugHighlight =
350 " gl_FragColor.rgb = vec3(0.0, gl_FragColor.a, 0.0);\n";
Romain Guy78dd96d2013-05-03 14:24:16 -0700351const char* gFS_Main_EmulateStencil =
352 " gl_FragColor.rgba = vec4(1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0, 1.0);\n"
353 " return;\n"
354 " /*\n";
355const char* gFS_Footer_EmulateStencil =
356 " */\n";
Romain Guyac670c02010-07-27 17:39:27 -0700357const char* gFS_Footer =
358 "}\n\n";
359
360///////////////////////////////////////////////////////////////////////////////
361// PorterDuff snippets
362///////////////////////////////////////////////////////////////////////////////
363
Romain Guy48daa542010-08-10 19:21:34 -0700364const char* gBlendOps[18] = {
Romain Guyac670c02010-07-27 17:39:27 -0700365 // Clear
366 "return vec4(0.0, 0.0, 0.0, 0.0);\n",
367 // Src
368 "return src;\n",
369 // Dst
370 "return dst;\n",
371 // SrcOver
Romain Guy06f96e22010-07-30 19:18:16 -0700372 "return src + dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700373 // DstOver
Romain Guy06f96e22010-07-30 19:18:16 -0700374 "return dst + src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700375 // SrcIn
Romain Guy06f96e22010-07-30 19:18:16 -0700376 "return src * dst.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700377 // DstIn
Romain Guy06f96e22010-07-30 19:18:16 -0700378 "return dst * src.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700379 // SrcOut
Romain Guy06f96e22010-07-30 19:18:16 -0700380 "return src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700381 // DstOut
Romain Guy06f96e22010-07-30 19:18:16 -0700382 "return dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700383 // SrcAtop
384 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n",
385 // DstAtop
386 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n",
387 // Xor
Romain Guy48daa542010-08-10 19:21:34 -0700388 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, "
Romain Guyac670c02010-07-27 17:39:27 -0700389 "src.a + dst.a - 2.0 * src.a * dst.a);\n",
Romain Guy48daa542010-08-10 19:21:34 -0700390 // Add
391 "return min(src + dst, 1.0);\n",
392 // Multiply
393 "return src * dst;\n",
394 // Screen
395 "return src + dst - src * dst;\n",
396 // Overlay
397 "return clamp(vec4(mix("
398 "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), "
399 "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), "
400 "step(dst.a, 2.0 * dst.rgb)), "
401 "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n",
402 // Darken
403 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
404 "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
405 // Lighten
406 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
407 "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 -0700408};
409
410///////////////////////////////////////////////////////////////////////////////
411// Constructors/destructors
412///////////////////////////////////////////////////////////////////////////////
413
Romain Guyb4880042013-04-05 11:17:55 -0700414ProgramCache::ProgramCache(): mHasES3(Extensions::getInstance().getMajorGlVersion() >= 3) {
Romain Guyac670c02010-07-27 17:39:27 -0700415}
416
417ProgramCache::~ProgramCache() {
418 clear();
419}
420
421///////////////////////////////////////////////////////////////////////////////
422// Cache management
423///////////////////////////////////////////////////////////////////////////////
424
425void ProgramCache::clear() {
Romain Guy67f27952010-12-07 20:09:23 -0800426 PROGRAM_LOGD("Clearing program cache");
427
Romain Guyac670c02010-07-27 17:39:27 -0700428 size_t count = mCache.size();
429 for (size_t i = 0; i < count; i++) {
430 delete mCache.valueAt(i);
431 }
432 mCache.clear();
433}
434
435Program* ProgramCache::get(const ProgramDescription& description) {
436 programid key = description.key();
Chris Craik096b8d92013-03-01 11:08:11 -0800437 if (key == (PROGRAM_KEY_TEXTURE | PROGRAM_KEY_A8_TEXTURE)) {
438 // program for A8, unmodulated, texture w/o shader (black text/path textures) is equivalent
439 // to standard texture program (bitmaps, patches). Consider them equivalent.
440 key = PROGRAM_KEY_TEXTURE;
441 }
442
Romain Guyac670c02010-07-27 17:39:27 -0700443 ssize_t index = mCache.indexOfKey(key);
444 Program* program = NULL;
445 if (index < 0) {
Romain Guyee916f12010-09-20 17:53:08 -0700446 description.log("Could not find program");
Romain Guyac670c02010-07-27 17:39:27 -0700447 program = generateProgram(description, key);
448 mCache.add(key, program);
449 } else {
450 program = mCache.valueAt(index);
451 }
452 return program;
453}
454
455///////////////////////////////////////////////////////////////////////////////
456// Program generation
457///////////////////////////////////////////////////////////////////////////////
458
459Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) {
460 String8 vertexShader = generateVertexShader(description);
461 String8 fragmentShader = generateFragmentShader(description);
462
Romain Guy42e1e0d2012-07-30 14:47:51 -0700463 return new Program(description, vertexShader.string(), fragmentShader.string());
464}
465
466static inline size_t gradientIndex(const ProgramDescription& description) {
467 return description.gradientType * 2 + description.isSimpleGradient;
Romain Guyac670c02010-07-27 17:39:27 -0700468}
469
470String8 ProgramCache::generateVertexShader(const ProgramDescription& description) {
471 // Add attributes
472 String8 shader(gVS_Header_Attributes);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700473 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700474 shader.append(gVS_Header_Attributes_TexCoords);
475 }
Chris Craik710f46d2012-09-17 17:25:49 -0700476 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800477 shader.append(gVS_Header_Attributes_AAVertexShapeParameters);
Chet Haase5b0200b2011-04-13 17:58:08 -0700478 }
Romain Guyff316ec2013-02-13 18:39:43 -0800479 if (description.hasColors) {
480 shader.append(gVS_Header_Attributes_Colors);
481 }
Romain Guyac670c02010-07-27 17:39:27 -0700482 // Uniforms
483 shader.append(gVS_Header_Uniforms);
Romain Guy8f0095c2011-05-02 17:24:22 -0700484 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700485 shader.append(gVS_Header_Uniforms_TextureTransform);
486 }
Romain Guyac670c02010-07-27 17:39:27 -0700487 if (description.hasGradient) {
Romain Guyb4880042013-04-05 11:17:55 -0700488 shader.append(gVS_Header_Uniforms_HasGradient);
Romain Guyac670c02010-07-27 17:39:27 -0700489 }
Romain Guy889f8d12010-07-29 14:37:42 -0700490 if (description.hasBitmap) {
491 shader.append(gVS_Header_Uniforms_HasBitmap);
492 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700493 if (description.hasRoundRectClip) {
494 shader.append(gVS_Header_Uniforms_HasRoundRectClip);
495 }
Romain Guyac670c02010-07-27 17:39:27 -0700496 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700497 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700498 shader.append(gVS_Header_Varyings_HasTexture);
499 }
Chris Craik710f46d2012-09-17 17:25:49 -0700500 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800501 shader.append(gVS_Header_Varyings_IsAAVertexShape);
Chet Haase5b0200b2011-04-13 17:58:08 -0700502 }
Romain Guyff316ec2013-02-13 18:39:43 -0800503 if (description.hasColors) {
504 shader.append(gVS_Header_Varyings_HasColors);
505 }
Romain Guyac670c02010-07-27 17:39:27 -0700506 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700507 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700508 }
509 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700510 shader.append(gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700511 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700512 if (description.hasRoundRectClip) {
513 shader.append(gVS_Header_Varyings_HasRoundRectClip);
514 }
Romain Guyac670c02010-07-27 17:39:27 -0700515
516 // Begin the shader
517 shader.append(gVS_Main); {
Romain Guy8f0095c2011-05-02 17:24:22 -0700518 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700519 shader.append(gVS_Main_OutTransformedTexCoords);
Romain Guy8f0095c2011-05-02 17:24:22 -0700520 } else if (description.hasTexture || description.hasExternalTexture) {
521 shader.append(gVS_Main_OutTexCoords);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700522 }
Chris Craik710f46d2012-09-17 17:25:49 -0700523 if (description.isAA) {
Chris Craikbf759452014-08-11 16:00:44 -0700524 if (description.isShadowAA) {
525 shader.append(gVS_Main_ShadowAAVertexShape);
526 } else {
527 shader.append(gVS_Main_AAVertexShape);
528 }
Chet Haase5b0200b2011-04-13 17:58:08 -0700529 }
Romain Guyff316ec2013-02-13 18:39:43 -0800530 if (description.hasColors) {
531 shader.append(gVS_Main_OutColors);
532 }
Romain Guy889f8d12010-07-29 14:37:42 -0700533 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700534 shader.append(gVS_Main_OutBitmapTexCoords);
Romain Guy889f8d12010-07-29 14:37:42 -0700535 }
Romain Guyac670c02010-07-27 17:39:27 -0700536 // Output transformed position
537 shader.append(gVS_Main_Position);
Chet Haasea1d12dd2012-09-21 14:50:14 -0700538 if (description.hasGradient) {
539 shader.append(gVS_Main_OutGradient[gradientIndex(description)]);
540 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700541 if (description.hasRoundRectClip) {
542 shader.append(gVS_Main_HasRoundRectClip);
543 }
Romain Guyac670c02010-07-27 17:39:27 -0700544 }
545 // End the shader
546 shader.append(gVS_Footer);
547
548 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string());
549
550 return shader;
551}
552
Romain Guya938f562012-09-13 20:31:08 -0700553static bool shaderOp(const ProgramDescription& description, String8& shader,
554 const int modulateOp, const char** snippets) {
555 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
556 op = op * 2 + description.hasGammaCorrection;
557 shader.append(snippets[op]);
558 return description.hasAlpha8Texture;
559}
560
Romain Guyac670c02010-07-27 17:39:27 -0700561String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) {
Romain Guya5aed0d2010-09-09 14:42:43 -0700562 String8 shader;
563
Romain Guy707b2f72010-10-11 16:34:59 -0700564 const bool blendFramebuffer = description.framebufferMode >= SkXfermode::kPlus_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700565 if (blendFramebuffer) {
566 shader.append(gFS_Header_Extension_FramebufferFetch);
567 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700568 if (description.hasExternalTexture) {
569 shader.append(gFS_Header_Extension_ExternalTexture);
570 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700571
572 shader.append(gFS_Header);
Romain Guyac670c02010-07-27 17:39:27 -0700573
574 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700575 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700576 shader.append(gVS_Header_Varyings_HasTexture);
577 }
Chris Craik710f46d2012-09-17 17:25:49 -0700578 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800579 shader.append(gVS_Header_Varyings_IsAAVertexShape);
Chet Haase5b0200b2011-04-13 17:58:08 -0700580 }
Romain Guyff316ec2013-02-13 18:39:43 -0800581 if (description.hasColors) {
582 shader.append(gVS_Header_Varyings_HasColors);
583 }
Romain Guyac670c02010-07-27 17:39:27 -0700584 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700585 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700586 }
587 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700588 shader.append(gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700589 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700590 if (description.hasRoundRectClip) {
591 shader.append(gVS_Header_Varyings_HasRoundRectClip);
592 }
Romain Guyac670c02010-07-27 17:39:27 -0700593
Romain Guyac670c02010-07-27 17:39:27 -0700594 // Uniforms
Romain Guy707b2f72010-10-11 16:34:59 -0700595 int modulateOp = MODULATE_OP_NO_MODULATE;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700596 const bool singleColor = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700597 !description.hasGradient && !description.hasBitmap;
598
599 if (description.modulate || singleColor) {
600 shader.append(gFS_Uniforms_Color);
601 if (!singleColor) modulateOp = MODULATE_OP_MODULATE;
602 }
Romain Guyac670c02010-07-27 17:39:27 -0700603 if (description.hasTexture) {
604 shader.append(gFS_Uniforms_TextureSampler);
Romain Guy8f0095c2011-05-02 17:24:22 -0700605 } else if (description.hasExternalTexture) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700606 shader.append(gFS_Uniforms_ExternalTextureSampler);
607 }
Romain Guyac670c02010-07-27 17:39:27 -0700608 if (description.hasGradient) {
Romain Guyb4880042013-04-05 11:17:55 -0700609 shader.appendFormat(gFS_Uniforms_GradientSampler[description.isSimpleGradient],
610 gFS_Uniforms_Dither);
Romain Guyac670c02010-07-27 17:39:27 -0700611 }
Romain Guy41210632012-07-16 17:04:24 -0700612 if (description.hasGammaCorrection) {
613 shader.append(gFS_Uniforms_Gamma);
614 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700615 if (description.hasRoundRectClip) {
616 shader.append(gFS_Uniforms_HasRoundRectClip);
617 }
Romain Guy707b2f72010-10-11 16:34:59 -0700618
619 // Optimization for common cases
Chris Craikdeeda3d2014-05-05 19:09:33 -0700620 if (!description.isAA
621 && !blendFramebuffer
622 && !description.hasColors
623 && description.colorOp == ProgramDescription::kColorNone
624 && !description.hasDebugHighlight
625 && !description.emulateStencil
626 && !description.hasRoundRectClip) {
Romain Guy707b2f72010-10-11 16:34:59 -0700627 bool fast = false;
628
629 const bool noShader = !description.hasGradient && !description.hasBitmap;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700630 const bool singleTexture = (description.hasTexture || description.hasExternalTexture) &&
Romain Guy707b2f72010-10-11 16:34:59 -0700631 !description.hasAlpha8Texture && noShader;
632 const bool singleA8Texture = description.hasTexture &&
633 description.hasAlpha8Texture && noShader;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700634 const bool singleGradient = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700635 description.hasGradient && !description.hasBitmap &&
636 description.gradientType == ProgramDescription::kGradientLinear;
637
638 if (singleColor) {
639 shader.append(gFS_Fast_SingleColor);
640 fast = true;
641 } else if (singleTexture) {
642 if (!description.modulate) {
643 shader.append(gFS_Fast_SingleTexture);
644 } else {
645 shader.append(gFS_Fast_SingleModulateTexture);
646 }
647 fast = true;
648 } else if (singleA8Texture) {
649 if (!description.modulate) {
Romain Guy41210632012-07-16 17:04:24 -0700650 if (description.hasGammaCorrection) {
651 shader.append(gFS_Fast_SingleA8Texture_ApplyGamma);
652 } else {
653 shader.append(gFS_Fast_SingleA8Texture);
654 }
Romain Guy707b2f72010-10-11 16:34:59 -0700655 } else {
Romain Guy41210632012-07-16 17:04:24 -0700656 if (description.hasGammaCorrection) {
657 shader.append(gFS_Fast_SingleModulateA8Texture_ApplyGamma);
658 } else {
659 shader.append(gFS_Fast_SingleModulateA8Texture);
660 }
Romain Guy707b2f72010-10-11 16:34:59 -0700661 }
662 fast = true;
663 } else if (singleGradient) {
664 if (!description.modulate) {
Romain Guyb4880042013-04-05 11:17:55 -0700665 shader.appendFormat(gFS_Fast_SingleGradient[description.isSimpleGradient],
666 gFS_Main_Dither[mHasES3]);
Romain Guy707b2f72010-10-11 16:34:59 -0700667 } else {
Romain Guyb4880042013-04-05 11:17:55 -0700668 shader.appendFormat(gFS_Fast_SingleModulateGradient[description.isSimpleGradient],
669 gFS_Main_Dither[mHasES3]);
Romain Guy707b2f72010-10-11 16:34:59 -0700670 }
671 fast = true;
672 }
673
674 if (fast) {
Romain Guyc15008e2010-11-10 11:59:15 -0800675#if DEBUG_PROGRAMS
Romain Guy707b2f72010-10-11 16:34:59 -0700676 PROGRAM_LOGD("*** Fast case:\n");
677 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
678 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800679#endif
Romain Guy707b2f72010-10-11 16:34:59 -0700680
681 return shader;
682 }
683 }
684
Romain Guyac670c02010-07-27 17:39:27 -0700685 if (description.hasBitmap) {
686 shader.append(gFS_Uniforms_BitmapSampler);
687 }
688 shader.append(gFS_Uniforms_ColorOp[description.colorOp]);
689
690 // Generate required functions
691 if (description.hasGradient && description.hasBitmap) {
Romain Guy48daa542010-08-10 19:21:34 -0700692 generateBlend(shader, "blendShaders", description.shadersMode);
Romain Guyac670c02010-07-27 17:39:27 -0700693 }
694 if (description.colorOp == ProgramDescription::kColorBlend) {
Romain Guy48daa542010-08-10 19:21:34 -0700695 generateBlend(shader, "blendColors", description.colorMode);
Romain Guyac670c02010-07-27 17:39:27 -0700696 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700697 if (blendFramebuffer) {
698 generateBlend(shader, "blendFramebuffer", description.framebufferMode);
699 }
Romain Guy889f8d12010-07-29 14:37:42 -0700700 if (description.isBitmapNpot) {
701 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
702 }
Romain Guyac670c02010-07-27 17:39:27 -0700703
704 // Begin the shader
705 shader.append(gFS_Main); {
Romain Guy78dd96d2013-05-03 14:24:16 -0700706 if (description.emulateStencil) {
707 shader.append(gFS_Main_EmulateStencil);
708 }
Romain Guyac670c02010-07-27 17:39:27 -0700709 // Stores the result in fragColor directly
Romain Guyaa6c24c2011-04-28 18:40:04 -0700710 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700711 if (description.hasAlpha8Texture) {
Romain Guy707b2f72010-10-11 16:34:59 -0700712 if (!description.hasGradient && !description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700713 shader.append(gFS_Main_FetchA8Texture[modulateOp * 2 +
714 description.hasGammaCorrection]);
Romain Guy707b2f72010-10-11 16:34:59 -0700715 }
Romain Guyac670c02010-07-27 17:39:27 -0700716 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700717 shader.append(gFS_Main_FetchTexture[modulateOp]);
Romain Guyac670c02010-07-27 17:39:27 -0700718 }
719 } else {
Romain Guya938f562012-09-13 20:31:08 -0700720 if (!description.hasGradient && !description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700721 shader.append(gFS_Main_FetchColor);
722 }
Romain Guyac670c02010-07-27 17:39:27 -0700723 }
724 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700725 shader.append(gFS_Main_FetchGradient[gradientIndex(description)]);
Romain Guyb4880042013-04-05 11:17:55 -0700726 shader.appendFormat(gFS_Main_AddDitherToGradient, gFS_Main_Dither[mHasES3]);
Romain Guyac670c02010-07-27 17:39:27 -0700727 }
728 if (description.hasBitmap) {
Romain Guy889f8d12010-07-29 14:37:42 -0700729 if (!description.isBitmapNpot) {
730 shader.append(gFS_Main_FetchBitmap);
731 } else {
732 shader.append(gFS_Main_FetchBitmapNpot);
733 }
Romain Guyac670c02010-07-27 17:39:27 -0700734 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700735 bool applyModulate = false;
Romain Guyac670c02010-07-27 17:39:27 -0700736 // Case when we have two shaders set
737 if (description.hasGradient && description.hasBitmap) {
738 if (description.isBitmapFirst) {
739 shader.append(gFS_Main_BlendShadersBG);
740 } else {
741 shader.append(gFS_Main_BlendShadersGB);
742 }
Romain Guya938f562012-09-13 20:31:08 -0700743 applyModulate = shaderOp(description, shader, modulateOp,
744 gFS_Main_BlendShaders_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700745 } else {
746 if (description.hasGradient) {
Romain Guya938f562012-09-13 20:31:08 -0700747 applyModulate = shaderOp(description, shader, modulateOp,
748 gFS_Main_GradientShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700749 } else if (description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700750 applyModulate = shaderOp(description, shader, modulateOp,
751 gFS_Main_BitmapShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700752 }
Romain Guyac670c02010-07-27 17:39:27 -0700753 }
Romain Guya938f562012-09-13 20:31:08 -0700754
Romain Guy740bf2b2011-04-26 15:33:10 -0700755 if (description.modulate && applyModulate) {
Romain Guya938f562012-09-13 20:31:08 -0700756 shader.append(gFS_Main_ModulateColor);
Romain Guy740bf2b2011-04-26 15:33:10 -0700757 }
Romain Guya938f562012-09-13 20:31:08 -0700758
Romain Guyac670c02010-07-27 17:39:27 -0700759 // Apply the color op if needed
760 shader.append(gFS_Main_ApplyColorOp[description.colorOp]);
Chris Craik9f44a132012-09-13 18:34:55 -0700761
Chris Craik710f46d2012-09-17 17:25:49 -0700762 if (description.isAA) {
Chris Craikbf759452014-08-11 16:00:44 -0700763 if (description.isShadowAA) {
764 shader.append(gFS_Main_AccountForShadowAAVertexShape);
765 } else {
766 shader.append(gFS_Main_AccountForAAVertexShape);
767 }
Chris Craik9f44a132012-09-13 18:34:55 -0700768 }
769
Romain Guyac670c02010-07-27 17:39:27 -0700770 // Output the fragment
Romain Guya5aed0d2010-09-09 14:42:43 -0700771 if (!blendFramebuffer) {
772 shader.append(gFS_Main_FragColor);
773 } else {
Romain Guyf607bdc2010-09-10 19:20:06 -0700774 shader.append(!description.swapSrcDst ?
775 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap);
Romain Guya5aed0d2010-09-09 14:42:43 -0700776 }
Romain Guyff316ec2013-02-13 18:39:43 -0800777 if (description.hasColors) {
778 shader.append(gFS_Main_FragColor_HasColors);
779 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700780 if (description.hasRoundRectClip) {
781 shader.append(gFS_Main_FragColor_HasRoundRectClip);
782 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800783 if (description.hasDebugHighlight) {
784 shader.append(gFS_Main_DebugHighlight);
785 }
Romain Guyac670c02010-07-27 17:39:27 -0700786 }
Romain Guy78dd96d2013-05-03 14:24:16 -0700787 if (description.emulateStencil) {
788 shader.append(gFS_Footer_EmulateStencil);
789 }
Romain Guyac670c02010-07-27 17:39:27 -0700790 // End the shader
791 shader.append(gFS_Footer);
792
Romain Guyc15008e2010-11-10 11:59:15 -0800793#if DEBUG_PROGRAMS
Romain Guydb1938e2010-08-02 18:50:22 -0700794 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
795 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800796#endif
Romain Guydb1938e2010-08-02 18:50:22 -0700797
Romain Guyac670c02010-07-27 17:39:27 -0700798 return shader;
799}
800
Romain Guy48daa542010-08-10 19:21:34 -0700801void ProgramCache::generateBlend(String8& shader, const char* name, SkXfermode::Mode mode) {
Romain Guyac670c02010-07-27 17:39:27 -0700802 shader.append("\nvec4 ");
803 shader.append(name);
804 shader.append("(vec4 src, vec4 dst) {\n");
805 shader.append(" ");
Romain Guy48daa542010-08-10 19:21:34 -0700806 shader.append(gBlendOps[mode]);
Romain Guyac670c02010-07-27 17:39:27 -0700807 shader.append("}\n");
808}
809
Romain Guy889f8d12010-07-29 14:37:42 -0700810void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) {
Romain Guy63553472012-07-18 20:04:14 -0700811 shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700812 if (wrapS == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700813 shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700814 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n");
815 }
816 if (wrapT == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700817 shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700818 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n");
819 }
820 shader.append(" return vec2(");
821 switch (wrapS) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700822 case GL_CLAMP_TO_EDGE:
823 shader.append("texCoords.x");
824 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700825 case GL_REPEAT:
826 shader.append("mod(texCoords.x, 1.0)");
827 break;
828 case GL_MIRRORED_REPEAT:
829 shader.append("xMod2");
830 break;
831 }
832 shader.append(", ");
833 switch (wrapT) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700834 case GL_CLAMP_TO_EDGE:
835 shader.append("texCoords.y");
836 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700837 case GL_REPEAT:
838 shader.append("mod(texCoords.y, 1.0)");
839 break;
840 case GL_MIRRORED_REPEAT:
841 shader.append("yMod2");
842 break;
843 }
844 shader.append(");\n");
845 shader.append("}\n");
846}
847
Romain Guydb1938e2010-08-02 18:50:22 -0700848void ProgramCache::printLongString(const String8& shader) const {
849 ssize_t index = 0;
850 ssize_t lastIndex = 0;
851 const char* str = shader.string();
852 while ((index = shader.find("\n", index)) > -1) {
853 String8 line(str, index - lastIndex);
854 if (line.length() == 0) line.append("\n");
855 PROGRAM_LOGD("%s", line.string());
856 index++;
857 str += (index - lastIndex);
858 lastIndex = index;
859 }
860}
861
Romain Guyac670c02010-07-27 17:39:27 -0700862}; // namespace uirenderer
863}; // namespace android