blob: 41adda15f36773c4d57152141fddd6d44e7f62a3 [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 Craik91a8c7c2014-08-12 14:31:35 -070049const char* gVS_Header_Attributes_VertexAlphaParameters =
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 Craik91a8c7c2014-08-12 14:31:35 -070067const char* gVS_Header_Varyings_HasVertexAlpha =
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 =
Chris Craik68a73e82014-08-29 17:06:27 -070091 "varying highp 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
Chris Craik91a8c7c2014-08-12 14:31:35 -0700125const char* gVS_Main_VertexAlpha =
Chris Craik6ebdc112012-08-31 18:24:33 -0700126 " alpha = vtxAlpha;\n";
Chris Craikbf759452014-08-11 16:00:44 -0700127
Chris Craikdeeda3d2014-05-05 19:09:33 -0700128const char* gVS_Main_HasRoundRectClip =
129 " roundRectPos = (roundRectInvTransform * transformedPosition).xy;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700130const char* gVS_Footer =
131 "}\n\n";
132
133///////////////////////////////////////////////////////////////////////////////
134// Fragment shaders snippets
135///////////////////////////////////////////////////////////////////////////////
136
Romain Guya5aed0d2010-09-09 14:42:43 -0700137const char* gFS_Header_Extension_FramebufferFetch =
138 "#extension GL_NV_shader_framebuffer_fetch : enable\n\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700139const char* gFS_Header_Extension_ExternalTexture =
140 "#extension GL_OES_EGL_image_external : require\n\n";
Romain Guyac670c02010-07-27 17:39:27 -0700141const char* gFS_Header =
142 "precision mediump float;\n\n";
143const char* gFS_Uniforms_Color =
144 "uniform vec4 color;\n";
145const char* gFS_Uniforms_TextureSampler =
Romain Guya938f562012-09-13 20:31:08 -0700146 "uniform sampler2D baseSampler;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700147const char* gFS_Uniforms_ExternalTextureSampler =
Romain Guya938f562012-09-13 20:31:08 -0700148 "uniform samplerExternalOES baseSampler;\n";
Romain Guyb4880042013-04-05 11:17:55 -0700149const char* gFS_Uniforms_Dither =
150 "uniform sampler2D ditherSampler;";
151const char* gFS_Uniforms_GradientSampler[2] = {
152 "%s\n"
153 "uniform sampler2D gradientSampler;\n",
154 "%s\n"
155 "uniform vec4 startColor;\n"
Romain Guy211efea2012-07-31 21:16:07 -0700156 "uniform vec4 endColor;\n"
Romain Guyee916f12010-09-20 17:53:08 -0700157};
Romain Guyac670c02010-07-27 17:39:27 -0700158const char* gFS_Uniforms_BitmapSampler =
159 "uniform sampler2D bitmapSampler;\n";
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500160const char* gFS_Uniforms_ColorOp[3] = {
Romain Guyac670c02010-07-27 17:39:27 -0700161 // None
162 "",
163 // Matrix
164 "uniform mat4 colorMatrix;\n"
165 "uniform vec4 colorMatrixVector;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700166 // PorterDuff
Romain Guydb1938e2010-08-02 18:50:22 -0700167 "uniform vec4 colorBlend;\n"
Romain Guyac670c02010-07-27 17:39:27 -0700168};
Romain Guy41210632012-07-16 17:04:24 -0700169const char* gFS_Uniforms_Gamma =
170 "uniform float gamma;\n";
171
Chris Craikdeeda3d2014-05-05 19:09:33 -0700172const char* gFS_Uniforms_HasRoundRectClip =
173 "uniform vec4 roundRectInnerRectLTRB;\n"
174 "uniform float roundRectRadius;\n";
175
Romain Guyac670c02010-07-27 17:39:27 -0700176const char* gFS_Main =
177 "\nvoid main(void) {\n"
Romain Guy7fbcc042010-08-04 15:40:07 -0700178 " lowp vec4 fragColor;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700179
Romain Guyb4880042013-04-05 11:17:55 -0700180const char* gFS_Main_Dither[2] = {
181 // ES 2.0
182 "texture2D(ditherSampler, ditherTexCoords).a * " STR(DITHER_KERNEL_SIZE_INV_SQUARE),
183 // ES 3.0
Romain Guy032d47a2013-04-08 19:45:40 -0700184 "texture2D(ditherSampler, ditherTexCoords).a"
Romain Guyb4880042013-04-05 11:17:55 -0700185};
Romain Guy211efea2012-07-31 21:16:07 -0700186const char* gFS_Main_AddDitherToGradient =
Romain Guyb4880042013-04-05 11:17:55 -0700187 " gradientColor += %s;\n";
Romain Guy211efea2012-07-31 21:16:07 -0700188
Romain Guy707b2f72010-10-11 16:34:59 -0700189// Fast cases
190const char* gFS_Fast_SingleColor =
191 "\nvoid main(void) {\n"
192 " gl_FragColor = color;\n"
193 "}\n\n";
194const char* gFS_Fast_SingleTexture =
195 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700196 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700197 "}\n\n";
198const char* gFS_Fast_SingleModulateTexture =
199 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700200 " gl_FragColor = color.a * texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700201 "}\n\n";
202const char* gFS_Fast_SingleA8Texture =
203 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700204 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700205 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700206const char* gFS_Fast_SingleA8Texture_ApplyGamma =
207 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700208 " gl_FragColor = vec4(0.0, 0.0, 0.0, pow(texture2D(baseSampler, outTexCoords).a, gamma));\n"
Romain Guy41210632012-07-16 17:04:24 -0700209 "}\n\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700210const char* gFS_Fast_SingleModulateA8Texture =
211 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700212 " gl_FragColor = color * texture2D(baseSampler, outTexCoords).a;\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700213 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700214const char* gFS_Fast_SingleModulateA8Texture_ApplyGamma =
215 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700216 " gl_FragColor = color * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy41210632012-07-16 17:04:24 -0700217 "}\n\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -0700218const char* gFS_Fast_SingleGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700219 "\nvoid main(void) {\n"
Romain Guyb4880042013-04-05 11:17:55 -0700220 " gl_FragColor = %s + texture2D(gradientSampler, linear);\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700221 "}\n\n",
222 "\nvoid main(void) {\n"
Romain Guyb4880042013-04-05 11:17:55 -0700223 " gl_FragColor = %s + mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
224 "}\n\n",
Romain Guy42e1e0d2012-07-30 14:47:51 -0700225};
226const char* gFS_Fast_SingleModulateGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700227 "\nvoid main(void) {\n"
Romain Guyb4880042013-04-05 11:17:55 -0700228 " gl_FragColor = %s + color.a * texture2D(gradientSampler, linear);\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700229 "}\n\n",
230 "\nvoid main(void) {\n"
Romain Guyb4880042013-04-05 11:17:55 -0700231 " gl_FragColor = %s + color.a * mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700232 "}\n\n"
233};
Romain Guy707b2f72010-10-11 16:34:59 -0700234
235// General case
Romain Guyac670c02010-07-27 17:39:27 -0700236const char* gFS_Main_FetchColor =
237 " fragColor = color;\n";
Romain Guy740bf2b2011-04-26 15:33:10 -0700238const char* gFS_Main_ModulateColor =
239 " fragColor *= color.a;\n";
Chris Craik91a8c7c2014-08-12 14:31:35 -0700240const char* gFS_Main_ApplyVertexAlphaLinearInterp =
Chris Craik6ebdc112012-08-31 18:24:33 -0700241 " fragColor *= alpha;\n";
Chris Craik91a8c7c2014-08-12 14:31:35 -0700242const char* gFS_Main_ApplyVertexAlphaShadowInterp =
ztenghuiecf091e2015-02-17 13:26:10 -0800243 // Use a gaussian function for the shadow fall off. Note that alpha here
244 // is actually (1.0 - alpha) for saving computation.
245 " fragColor *= exp(- alpha * alpha * 4.0) - 0.018;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700246const char* gFS_Main_FetchTexture[2] = {
247 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700248 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700249 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700250 " fragColor = color * texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700251};
Romain Guya938f562012-09-13 20:31:08 -0700252const char* gFS_Main_FetchA8Texture[4] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700253 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700254 " fragColor = texture2D(baseSampler, outTexCoords);\n",
255 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700256 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700257 " fragColor = color * texture2D(baseSampler, outTexCoords).a;\n",
258 " fragColor = color * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700259};
Romain Guy42e1e0d2012-07-30 14:47:51 -0700260const char* gFS_Main_FetchGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700261 // Linear
Romain Guy320d46b2012-08-08 16:05:42 -0700262 " vec4 gradientColor = texture2D(gradientSampler, linear);\n",
Romain Guy211efea2012-07-31 21:16:07 -0700263
Romain Guy320d46b2012-08-08 16:05:42 -0700264 " vec4 gradientColor = mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700265
Romain Guyee916f12010-09-20 17:53:08 -0700266 // Circular
Romain Guy320d46b2012-08-08 16:05:42 -0700267 " vec4 gradientColor = texture2D(gradientSampler, vec2(length(circular), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700268
Romain Guy320d46b2012-08-08 16:05:42 -0700269 " vec4 gradientColor = mix(startColor, endColor, clamp(length(circular), 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700270
Romain Guyee916f12010-09-20 17:53:08 -0700271 // Sweep
Romain Guy63553472012-07-18 20:04:14 -0700272 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700273 " vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700274
Romain Guy42e1e0d2012-07-30 14:47:51 -0700275 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700276 " vec4 gradientColor = mix(startColor, endColor, clamp(index - floor(index), 0.0, 1.0));\n"
Romain Guyee916f12010-09-20 17:53:08 -0700277};
Romain Guyac670c02010-07-27 17:39:27 -0700278const char* gFS_Main_FetchBitmap =
279 " vec4 bitmapColor = texture2D(bitmapSampler, outBitmapTexCoords);\n";
Romain Guy889f8d12010-07-29 14:37:42 -0700280const char* gFS_Main_FetchBitmapNpot =
281 " vec4 bitmapColor = texture2D(bitmapSampler, wrap(outBitmapTexCoords));\n";
Romain Guyac670c02010-07-27 17:39:27 -0700282const char* gFS_Main_BlendShadersBG =
Romain Guyac670c02010-07-27 17:39:27 -0700283 " fragColor = blendShaders(gradientColor, bitmapColor)";
Romain Guy06f96e22010-07-30 19:18:16 -0700284const char* gFS_Main_BlendShadersGB =
285 " fragColor = blendShaders(bitmapColor, gradientColor)";
Romain Guya938f562012-09-13 20:31:08 -0700286const char* gFS_Main_BlendShaders_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700287 // Don't modulate
288 ";\n",
Romain Guya938f562012-09-13 20:31:08 -0700289 ";\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700290 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700291 " * color.a;\n",
292 " * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700293 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700294 " * texture2D(baseSampler, outTexCoords).a;\n",
295 " * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700296};
Romain Guya938f562012-09-13 20:31:08 -0700297const char* gFS_Main_GradientShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700298 // Don't modulate
299 " fragColor = gradientColor;\n",
Romain Guya938f562012-09-13 20:31:08 -0700300 " fragColor = gradientColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700301 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700302 " fragColor = gradientColor * color.a;\n",
303 " fragColor = gradientColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700304 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700305 " fragColor = gradientColor * texture2D(baseSampler, outTexCoords).a;\n",
306 " fragColor = gradientColor * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700307 };
Romain Guya938f562012-09-13 20:31:08 -0700308const char* gFS_Main_BitmapShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700309 // Don't modulate
310 " fragColor = bitmapColor;\n",
Romain Guya938f562012-09-13 20:31:08 -0700311 " fragColor = bitmapColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700312 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700313 " fragColor = bitmapColor * color.a;\n",
314 " fragColor = bitmapColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700315 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700316 " fragColor = bitmapColor * texture2D(baseSampler, outTexCoords).a;\n",
317 " fragColor = bitmapColor * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700318 };
Romain Guyac670c02010-07-27 17:39:27 -0700319const char* gFS_Main_FragColor =
320 " gl_FragColor = fragColor;\n";
Romain Guyff316ec2013-02-13 18:39:43 -0800321const char* gFS_Main_FragColor_HasColors =
322 " gl_FragColor *= outColors;\n";
Romain Guya5aed0d2010-09-09 14:42:43 -0700323const char* gFS_Main_FragColor_Blend =
324 " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n";
Romain Guyf607bdc2010-09-10 19:20:06 -0700325const char* gFS_Main_FragColor_Blend_Swap =
326 " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n";
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500327const char* gFS_Main_ApplyColorOp[3] = {
Romain Guyac670c02010-07-27 17:39:27 -0700328 // None
329 "",
330 // Matrix
Chris Craik73821c82014-09-16 17:32:13 -0700331 " fragColor.rgb /= (fragColor.a + 0.0019);\n" // un-premultiply
Romain Guyac670c02010-07-27 17:39:27 -0700332 " fragColor *= colorMatrix;\n"
Chris Craik73821c82014-09-16 17:32:13 -0700333 " fragColor += colorMatrixVector;\n"
334 " fragColor.rgb *= (fragColor.a + 0.0019);\n", // re-premultiply
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 Guyac670c02010-07-27 17:39:27 -0700351const char* gFS_Footer =
352 "}\n\n";
353
354///////////////////////////////////////////////////////////////////////////////
355// PorterDuff snippets
356///////////////////////////////////////////////////////////////////////////////
357
Romain Guy48daa542010-08-10 19:21:34 -0700358const char* gBlendOps[18] = {
Romain Guyac670c02010-07-27 17:39:27 -0700359 // Clear
360 "return vec4(0.0, 0.0, 0.0, 0.0);\n",
361 // Src
362 "return src;\n",
363 // Dst
364 "return dst;\n",
365 // SrcOver
Romain Guy06f96e22010-07-30 19:18:16 -0700366 "return src + dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700367 // DstOver
Romain Guy06f96e22010-07-30 19:18:16 -0700368 "return dst + src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700369 // SrcIn
Romain Guy06f96e22010-07-30 19:18:16 -0700370 "return src * dst.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700371 // DstIn
Romain Guy06f96e22010-07-30 19:18:16 -0700372 "return dst * src.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700373 // SrcOut
Romain Guy06f96e22010-07-30 19:18:16 -0700374 "return src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700375 // DstOut
Romain Guy06f96e22010-07-30 19:18:16 -0700376 "return dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700377 // SrcAtop
378 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n",
379 // DstAtop
380 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n",
381 // Xor
Romain Guy48daa542010-08-10 19:21:34 -0700382 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, "
Romain Guyac670c02010-07-27 17:39:27 -0700383 "src.a + dst.a - 2.0 * src.a * dst.a);\n",
Derek Sollenbergerc0bf7002015-03-06 13:48:27 -0500384 // Plus
Romain Guy48daa542010-08-10 19:21:34 -0700385 "return min(src + dst, 1.0);\n",
Derek Sollenbergerc0bf7002015-03-06 13:48:27 -0500386 // Modulate
Romain Guy48daa542010-08-10 19:21:34 -0700387 "return src * dst;\n",
388 // Screen
389 "return src + dst - src * dst;\n",
390 // Overlay
391 "return clamp(vec4(mix("
392 "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), "
393 "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), "
394 "step(dst.a, 2.0 * dst.rgb)), "
395 "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n",
396 // Darken
397 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
398 "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
399 // Lighten
400 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
401 "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 -0700402};
403
404///////////////////////////////////////////////////////////////////////////////
405// Constructors/destructors
406///////////////////////////////////////////////////////////////////////////////
407
Chris Craik117bdbc2015-02-05 10:12:38 -0800408ProgramCache::ProgramCache(Extensions& extensions)
409 : mHasES3(extensions.getMajorGlVersion() >= 3) {
Romain Guyac670c02010-07-27 17:39:27 -0700410}
411
412ProgramCache::~ProgramCache() {
413 clear();
414}
415
416///////////////////////////////////////////////////////////////////////////////
417// Cache management
418///////////////////////////////////////////////////////////////////////////////
419
420void ProgramCache::clear() {
Romain Guy67f27952010-12-07 20:09:23 -0800421 PROGRAM_LOGD("Clearing program cache");
Romain Guyac670c02010-07-27 17:39:27 -0700422 mCache.clear();
423}
424
425Program* ProgramCache::get(const ProgramDescription& description) {
426 programid key = description.key();
Chris Craik096b8d92013-03-01 11:08:11 -0800427 if (key == (PROGRAM_KEY_TEXTURE | PROGRAM_KEY_A8_TEXTURE)) {
428 // program for A8, unmodulated, texture w/o shader (black text/path textures) is equivalent
429 // to standard texture program (bitmaps, patches). Consider them equivalent.
430 key = PROGRAM_KEY_TEXTURE;
431 }
432
Chris Craik51d6a3d2014-12-22 17:16:56 -0800433 auto iter = mCache.find(key);
Chris Craikd41c4d82015-01-05 15:51:13 -0800434 Program* program = nullptr;
Chris Craik51d6a3d2014-12-22 17:16:56 -0800435 if (iter == mCache.end()) {
Romain Guyee916f12010-09-20 17:53:08 -0700436 description.log("Could not find program");
Romain Guyac670c02010-07-27 17:39:27 -0700437 program = generateProgram(description, key);
Chris Craik51d6a3d2014-12-22 17:16:56 -0800438 mCache[key] = std::unique_ptr<Program>(program);
Romain Guyac670c02010-07-27 17:39:27 -0700439 } else {
Chris Craik51d6a3d2014-12-22 17:16:56 -0800440 program = iter->second.get();
Romain Guyac670c02010-07-27 17:39:27 -0700441 }
442 return program;
443}
444
445///////////////////////////////////////////////////////////////////////////////
446// Program generation
447///////////////////////////////////////////////////////////////////////////////
448
Andreas Gampe64bb4132014-11-22 00:35:09 +0000449Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) {
Romain Guyac670c02010-07-27 17:39:27 -0700450 String8 vertexShader = generateVertexShader(description);
451 String8 fragmentShader = generateFragmentShader(description);
452
Romain Guy42e1e0d2012-07-30 14:47:51 -0700453 return new Program(description, vertexShader.string(), fragmentShader.string());
454}
455
456static inline size_t gradientIndex(const ProgramDescription& description) {
457 return description.gradientType * 2 + description.isSimpleGradient;
Romain Guyac670c02010-07-27 17:39:27 -0700458}
459
460String8 ProgramCache::generateVertexShader(const ProgramDescription& description) {
461 // Add attributes
462 String8 shader(gVS_Header_Attributes);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700463 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700464 shader.append(gVS_Header_Attributes_TexCoords);
465 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700466 if (description.hasVertexAlpha) {
467 shader.append(gVS_Header_Attributes_VertexAlphaParameters);
Chet Haase5b0200b2011-04-13 17:58:08 -0700468 }
Romain Guyff316ec2013-02-13 18:39:43 -0800469 if (description.hasColors) {
470 shader.append(gVS_Header_Attributes_Colors);
471 }
Romain Guyac670c02010-07-27 17:39:27 -0700472 // Uniforms
473 shader.append(gVS_Header_Uniforms);
Romain Guy8f0095c2011-05-02 17:24:22 -0700474 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700475 shader.append(gVS_Header_Uniforms_TextureTransform);
476 }
Romain Guyac670c02010-07-27 17:39:27 -0700477 if (description.hasGradient) {
Romain Guyb4880042013-04-05 11:17:55 -0700478 shader.append(gVS_Header_Uniforms_HasGradient);
Romain Guyac670c02010-07-27 17:39:27 -0700479 }
Romain Guy889f8d12010-07-29 14:37:42 -0700480 if (description.hasBitmap) {
481 shader.append(gVS_Header_Uniforms_HasBitmap);
482 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700483 if (description.hasRoundRectClip) {
484 shader.append(gVS_Header_Uniforms_HasRoundRectClip);
485 }
Romain Guyac670c02010-07-27 17:39:27 -0700486 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700487 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700488 shader.append(gVS_Header_Varyings_HasTexture);
489 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700490 if (description.hasVertexAlpha) {
491 shader.append(gVS_Header_Varyings_HasVertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700492 }
Romain Guyff316ec2013-02-13 18:39:43 -0800493 if (description.hasColors) {
494 shader.append(gVS_Header_Varyings_HasColors);
495 }
Romain Guyac670c02010-07-27 17:39:27 -0700496 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700497 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700498 }
499 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700500 shader.append(gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700501 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700502 if (description.hasRoundRectClip) {
503 shader.append(gVS_Header_Varyings_HasRoundRectClip);
504 }
Romain Guyac670c02010-07-27 17:39:27 -0700505
506 // Begin the shader
507 shader.append(gVS_Main); {
Romain Guy8f0095c2011-05-02 17:24:22 -0700508 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700509 shader.append(gVS_Main_OutTransformedTexCoords);
Romain Guy8f0095c2011-05-02 17:24:22 -0700510 } else if (description.hasTexture || description.hasExternalTexture) {
511 shader.append(gVS_Main_OutTexCoords);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700512 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700513 if (description.hasVertexAlpha) {
514 shader.append(gVS_Main_VertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700515 }
Romain Guyff316ec2013-02-13 18:39:43 -0800516 if (description.hasColors) {
517 shader.append(gVS_Main_OutColors);
518 }
Romain Guy889f8d12010-07-29 14:37:42 -0700519 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700520 shader.append(gVS_Main_OutBitmapTexCoords);
Romain Guy889f8d12010-07-29 14:37:42 -0700521 }
Romain Guyac670c02010-07-27 17:39:27 -0700522 // Output transformed position
523 shader.append(gVS_Main_Position);
Chet Haasea1d12dd2012-09-21 14:50:14 -0700524 if (description.hasGradient) {
525 shader.append(gVS_Main_OutGradient[gradientIndex(description)]);
526 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700527 if (description.hasRoundRectClip) {
528 shader.append(gVS_Main_HasRoundRectClip);
529 }
Romain Guyac670c02010-07-27 17:39:27 -0700530 }
531 // End the shader
532 shader.append(gVS_Footer);
533
534 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string());
535
536 return shader;
537}
538
Romain Guya938f562012-09-13 20:31:08 -0700539static bool shaderOp(const ProgramDescription& description, String8& shader,
540 const int modulateOp, const char** snippets) {
541 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
542 op = op * 2 + description.hasGammaCorrection;
543 shader.append(snippets[op]);
544 return description.hasAlpha8Texture;
545}
546
Romain Guyac670c02010-07-27 17:39:27 -0700547String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) {
Romain Guya5aed0d2010-09-09 14:42:43 -0700548 String8 shader;
549
Romain Guy707b2f72010-10-11 16:34:59 -0700550 const bool blendFramebuffer = description.framebufferMode >= SkXfermode::kPlus_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700551 if (blendFramebuffer) {
552 shader.append(gFS_Header_Extension_FramebufferFetch);
553 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700554 if (description.hasExternalTexture) {
555 shader.append(gFS_Header_Extension_ExternalTexture);
556 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700557
558 shader.append(gFS_Header);
Romain Guyac670c02010-07-27 17:39:27 -0700559
560 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700561 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700562 shader.append(gVS_Header_Varyings_HasTexture);
563 }
Chris Craik91a8c7c2014-08-12 14:31:35 -0700564 if (description.hasVertexAlpha) {
565 shader.append(gVS_Header_Varyings_HasVertexAlpha);
Chet Haase5b0200b2011-04-13 17:58:08 -0700566 }
Romain Guyff316ec2013-02-13 18:39:43 -0800567 if (description.hasColors) {
568 shader.append(gVS_Header_Varyings_HasColors);
569 }
Romain Guyac670c02010-07-27 17:39:27 -0700570 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700571 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700572 }
573 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700574 shader.append(gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700575 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700576 if (description.hasRoundRectClip) {
577 shader.append(gVS_Header_Varyings_HasRoundRectClip);
578 }
Romain Guyac670c02010-07-27 17:39:27 -0700579
Romain Guyac670c02010-07-27 17:39:27 -0700580 // Uniforms
Romain Guy707b2f72010-10-11 16:34:59 -0700581 int modulateOp = MODULATE_OP_NO_MODULATE;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700582 const bool singleColor = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700583 !description.hasGradient && !description.hasBitmap;
584
585 if (description.modulate || singleColor) {
586 shader.append(gFS_Uniforms_Color);
587 if (!singleColor) modulateOp = MODULATE_OP_MODULATE;
588 }
Romain Guyac670c02010-07-27 17:39:27 -0700589 if (description.hasTexture) {
590 shader.append(gFS_Uniforms_TextureSampler);
Romain Guy8f0095c2011-05-02 17:24:22 -0700591 } else if (description.hasExternalTexture) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700592 shader.append(gFS_Uniforms_ExternalTextureSampler);
593 }
Romain Guyac670c02010-07-27 17:39:27 -0700594 if (description.hasGradient) {
Romain Guyb4880042013-04-05 11:17:55 -0700595 shader.appendFormat(gFS_Uniforms_GradientSampler[description.isSimpleGradient],
596 gFS_Uniforms_Dither);
Romain Guyac670c02010-07-27 17:39:27 -0700597 }
Romain Guy41210632012-07-16 17:04:24 -0700598 if (description.hasGammaCorrection) {
599 shader.append(gFS_Uniforms_Gamma);
600 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700601 if (description.hasRoundRectClip) {
602 shader.append(gFS_Uniforms_HasRoundRectClip);
603 }
Romain Guy707b2f72010-10-11 16:34:59 -0700604
605 // Optimization for common cases
Chris Craik91a8c7c2014-08-12 14:31:35 -0700606 if (!description.hasVertexAlpha
Chris Craikdeeda3d2014-05-05 19:09:33 -0700607 && !blendFramebuffer
608 && !description.hasColors
609 && description.colorOp == ProgramDescription::kColorNone
610 && !description.hasDebugHighlight
Chris Craikdeeda3d2014-05-05 19:09:33 -0700611 && !description.hasRoundRectClip) {
Romain Guy707b2f72010-10-11 16:34:59 -0700612 bool fast = false;
613
614 const bool noShader = !description.hasGradient && !description.hasBitmap;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700615 const bool singleTexture = (description.hasTexture || description.hasExternalTexture) &&
Romain Guy707b2f72010-10-11 16:34:59 -0700616 !description.hasAlpha8Texture && noShader;
617 const bool singleA8Texture = description.hasTexture &&
618 description.hasAlpha8Texture && noShader;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700619 const bool singleGradient = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700620 description.hasGradient && !description.hasBitmap &&
621 description.gradientType == ProgramDescription::kGradientLinear;
622
623 if (singleColor) {
624 shader.append(gFS_Fast_SingleColor);
625 fast = true;
626 } else if (singleTexture) {
627 if (!description.modulate) {
628 shader.append(gFS_Fast_SingleTexture);
629 } else {
630 shader.append(gFS_Fast_SingleModulateTexture);
631 }
632 fast = true;
633 } else if (singleA8Texture) {
634 if (!description.modulate) {
Romain Guy41210632012-07-16 17:04:24 -0700635 if (description.hasGammaCorrection) {
636 shader.append(gFS_Fast_SingleA8Texture_ApplyGamma);
637 } else {
638 shader.append(gFS_Fast_SingleA8Texture);
639 }
Romain Guy707b2f72010-10-11 16:34:59 -0700640 } else {
Romain Guy41210632012-07-16 17:04:24 -0700641 if (description.hasGammaCorrection) {
642 shader.append(gFS_Fast_SingleModulateA8Texture_ApplyGamma);
643 } else {
644 shader.append(gFS_Fast_SingleModulateA8Texture);
645 }
Romain Guy707b2f72010-10-11 16:34:59 -0700646 }
647 fast = true;
648 } else if (singleGradient) {
649 if (!description.modulate) {
Romain Guyb4880042013-04-05 11:17:55 -0700650 shader.appendFormat(gFS_Fast_SingleGradient[description.isSimpleGradient],
651 gFS_Main_Dither[mHasES3]);
Romain Guy707b2f72010-10-11 16:34:59 -0700652 } else {
Romain Guyb4880042013-04-05 11:17:55 -0700653 shader.appendFormat(gFS_Fast_SingleModulateGradient[description.isSimpleGradient],
654 gFS_Main_Dither[mHasES3]);
Romain Guy707b2f72010-10-11 16:34:59 -0700655 }
656 fast = true;
657 }
658
659 if (fast) {
Romain Guyc15008e2010-11-10 11:59:15 -0800660#if DEBUG_PROGRAMS
Romain Guy707b2f72010-10-11 16:34:59 -0700661 PROGRAM_LOGD("*** Fast case:\n");
662 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
663 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800664#endif
Romain Guy707b2f72010-10-11 16:34:59 -0700665
666 return shader;
667 }
668 }
669
Romain Guyac670c02010-07-27 17:39:27 -0700670 if (description.hasBitmap) {
671 shader.append(gFS_Uniforms_BitmapSampler);
672 }
673 shader.append(gFS_Uniforms_ColorOp[description.colorOp]);
674
675 // Generate required functions
676 if (description.hasGradient && description.hasBitmap) {
Romain Guy48daa542010-08-10 19:21:34 -0700677 generateBlend(shader, "blendShaders", description.shadersMode);
Romain Guyac670c02010-07-27 17:39:27 -0700678 }
679 if (description.colorOp == ProgramDescription::kColorBlend) {
Romain Guy48daa542010-08-10 19:21:34 -0700680 generateBlend(shader, "blendColors", description.colorMode);
Romain Guyac670c02010-07-27 17:39:27 -0700681 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700682 if (blendFramebuffer) {
683 generateBlend(shader, "blendFramebuffer", description.framebufferMode);
684 }
Romain Guy889f8d12010-07-29 14:37:42 -0700685 if (description.isBitmapNpot) {
686 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
687 }
Romain Guyac670c02010-07-27 17:39:27 -0700688
689 // Begin the shader
690 shader.append(gFS_Main); {
691 // Stores the result in fragColor directly
Romain Guyaa6c24c2011-04-28 18:40:04 -0700692 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700693 if (description.hasAlpha8Texture) {
Romain Guy707b2f72010-10-11 16:34:59 -0700694 if (!description.hasGradient && !description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700695 shader.append(gFS_Main_FetchA8Texture[modulateOp * 2 +
696 description.hasGammaCorrection]);
Romain Guy707b2f72010-10-11 16:34:59 -0700697 }
Romain Guyac670c02010-07-27 17:39:27 -0700698 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700699 shader.append(gFS_Main_FetchTexture[modulateOp]);
Romain Guyac670c02010-07-27 17:39:27 -0700700 }
701 } else {
Romain Guya938f562012-09-13 20:31:08 -0700702 if (!description.hasGradient && !description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700703 shader.append(gFS_Main_FetchColor);
704 }
Romain Guyac670c02010-07-27 17:39:27 -0700705 }
706 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700707 shader.append(gFS_Main_FetchGradient[gradientIndex(description)]);
Romain Guyb4880042013-04-05 11:17:55 -0700708 shader.appendFormat(gFS_Main_AddDitherToGradient, gFS_Main_Dither[mHasES3]);
Romain Guyac670c02010-07-27 17:39:27 -0700709 }
710 if (description.hasBitmap) {
Romain Guy889f8d12010-07-29 14:37:42 -0700711 if (!description.isBitmapNpot) {
712 shader.append(gFS_Main_FetchBitmap);
713 } else {
714 shader.append(gFS_Main_FetchBitmapNpot);
715 }
Romain Guyac670c02010-07-27 17:39:27 -0700716 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700717 bool applyModulate = false;
Romain Guyac670c02010-07-27 17:39:27 -0700718 // Case when we have two shaders set
719 if (description.hasGradient && description.hasBitmap) {
720 if (description.isBitmapFirst) {
721 shader.append(gFS_Main_BlendShadersBG);
722 } else {
723 shader.append(gFS_Main_BlendShadersGB);
724 }
Romain Guya938f562012-09-13 20:31:08 -0700725 applyModulate = shaderOp(description, shader, modulateOp,
726 gFS_Main_BlendShaders_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700727 } else {
728 if (description.hasGradient) {
Romain Guya938f562012-09-13 20:31:08 -0700729 applyModulate = shaderOp(description, shader, modulateOp,
730 gFS_Main_GradientShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700731 } else if (description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700732 applyModulate = shaderOp(description, shader, modulateOp,
733 gFS_Main_BitmapShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700734 }
Romain Guyac670c02010-07-27 17:39:27 -0700735 }
Romain Guya938f562012-09-13 20:31:08 -0700736
Romain Guy740bf2b2011-04-26 15:33:10 -0700737 if (description.modulate && applyModulate) {
Romain Guya938f562012-09-13 20:31:08 -0700738 shader.append(gFS_Main_ModulateColor);
Romain Guy740bf2b2011-04-26 15:33:10 -0700739 }
Romain Guya938f562012-09-13 20:31:08 -0700740
Romain Guyac670c02010-07-27 17:39:27 -0700741 // Apply the color op if needed
742 shader.append(gFS_Main_ApplyColorOp[description.colorOp]);
Chris Craik9f44a132012-09-13 18:34:55 -0700743
Chris Craik91a8c7c2014-08-12 14:31:35 -0700744 if (description.hasVertexAlpha) {
745 if (description.useShadowAlphaInterp) {
746 shader.append(gFS_Main_ApplyVertexAlphaShadowInterp);
Chris Craikbf759452014-08-11 16:00:44 -0700747 } else {
Chris Craik91a8c7c2014-08-12 14:31:35 -0700748 shader.append(gFS_Main_ApplyVertexAlphaLinearInterp);
Chris Craikbf759452014-08-11 16:00:44 -0700749 }
Chris Craik9f44a132012-09-13 18:34:55 -0700750 }
751
Romain Guyac670c02010-07-27 17:39:27 -0700752 // Output the fragment
Romain Guya5aed0d2010-09-09 14:42:43 -0700753 if (!blendFramebuffer) {
754 shader.append(gFS_Main_FragColor);
755 } else {
Romain Guyf607bdc2010-09-10 19:20:06 -0700756 shader.append(!description.swapSrcDst ?
757 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap);
Romain Guya5aed0d2010-09-09 14:42:43 -0700758 }
Romain Guyff316ec2013-02-13 18:39:43 -0800759 if (description.hasColors) {
760 shader.append(gFS_Main_FragColor_HasColors);
761 }
Chris Craikdeeda3d2014-05-05 19:09:33 -0700762 if (description.hasRoundRectClip) {
763 shader.append(gFS_Main_FragColor_HasRoundRectClip);
764 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800765 if (description.hasDebugHighlight) {
766 shader.append(gFS_Main_DebugHighlight);
767 }
Romain Guyac670c02010-07-27 17:39:27 -0700768 }
769 // End the shader
770 shader.append(gFS_Footer);
771
Romain Guyc15008e2010-11-10 11:59:15 -0800772#if DEBUG_PROGRAMS
Romain Guydb1938e2010-08-02 18:50:22 -0700773 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
774 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800775#endif
Romain Guydb1938e2010-08-02 18:50:22 -0700776
Romain Guyac670c02010-07-27 17:39:27 -0700777 return shader;
778}
779
Romain Guy48daa542010-08-10 19:21:34 -0700780void ProgramCache::generateBlend(String8& shader, const char* name, SkXfermode::Mode mode) {
Romain Guyac670c02010-07-27 17:39:27 -0700781 shader.append("\nvec4 ");
782 shader.append(name);
783 shader.append("(vec4 src, vec4 dst) {\n");
784 shader.append(" ");
Romain Guy48daa542010-08-10 19:21:34 -0700785 shader.append(gBlendOps[mode]);
Romain Guyac670c02010-07-27 17:39:27 -0700786 shader.append("}\n");
787}
788
Romain Guy889f8d12010-07-29 14:37:42 -0700789void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) {
Romain Guy63553472012-07-18 20:04:14 -0700790 shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700791 if (wrapS == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700792 shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700793 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n");
794 }
795 if (wrapT == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700796 shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700797 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n");
798 }
799 shader.append(" return vec2(");
800 switch (wrapS) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700801 case GL_CLAMP_TO_EDGE:
802 shader.append("texCoords.x");
803 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700804 case GL_REPEAT:
805 shader.append("mod(texCoords.x, 1.0)");
806 break;
807 case GL_MIRRORED_REPEAT:
808 shader.append("xMod2");
809 break;
810 }
811 shader.append(", ");
812 switch (wrapT) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700813 case GL_CLAMP_TO_EDGE:
814 shader.append("texCoords.y");
815 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700816 case GL_REPEAT:
817 shader.append("mod(texCoords.y, 1.0)");
818 break;
819 case GL_MIRRORED_REPEAT:
820 shader.append("yMod2");
821 break;
822 }
823 shader.append(");\n");
824 shader.append("}\n");
825}
826
Romain Guydb1938e2010-08-02 18:50:22 -0700827void ProgramCache::printLongString(const String8& shader) const {
828 ssize_t index = 0;
829 ssize_t lastIndex = 0;
830 const char* str = shader.string();
831 while ((index = shader.find("\n", index)) > -1) {
832 String8 line(str, index - lastIndex);
833 if (line.length() == 0) line.append("\n");
Chris Craik1c1c3fe2015-03-06 09:40:35 -0800834 ALOGD("%s", line.string());
Romain Guydb1938e2010-08-02 18:50:22 -0700835 index++;
836 str += (index - lastIndex);
837 lastIndex = index;
838 }
839}
840
Romain Guyac670c02010-07-27 17:39:27 -0700841}; // namespace uirenderer
842}; // namespace android