blob: 74d598d5311e9bbbf862d370cae9e67a502d8a62 [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 Guyac670c02010-07-27 17:39:27 -070022#include "ProgramCache.h"
23
24namespace android {
25namespace uirenderer {
26
27///////////////////////////////////////////////////////////////////////////////
Romain Guy707b2f72010-10-11 16:34:59 -070028// Defines
29///////////////////////////////////////////////////////////////////////////////
30
31#define MODULATE_OP_NO_MODULATE 0
32#define MODULATE_OP_MODULATE 1
33#define MODULATE_OP_MODULATE_A8 2
34
35///////////////////////////////////////////////////////////////////////////////
Romain Guyac670c02010-07-27 17:39:27 -070036// Vertex shaders snippets
37///////////////////////////////////////////////////////////////////////////////
38
Romain Guyac670c02010-07-27 17:39:27 -070039const char* gVS_Header_Attributes =
40 "attribute vec4 position;\n";
41const char* gVS_Header_Attributes_TexCoords =
42 "attribute vec2 texCoords;\n";
Romain Guyff316ec2013-02-13 18:39:43 -080043const char* gVS_Header_Attributes_Colors =
44 "attribute vec4 colors;\n";
Chris Craik710f46d2012-09-17 17:25:49 -070045const char* gVS_Header_Attributes_AAVertexShapeParameters =
Chris Craik6ebdc112012-08-31 18:24:33 -070046 "attribute float vtxAlpha;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -070047const char* gVS_Header_Uniforms_TextureTransform =
48 "uniform mat4 mainTextureTransform;\n";
Romain Guyac670c02010-07-27 17:39:27 -070049const char* gVS_Header_Uniforms =
Romain Guy39284b72012-09-26 16:39:40 -070050 "uniform mat4 projection;\n" \
Romain Guyac670c02010-07-27 17:39:27 -070051 "uniform mat4 transform;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -070052const char* gVS_Header_Uniforms_IsPoint =
Romain Guy80bbfb12011-03-23 16:56:28 -070053 "uniform mediump float pointSize;\n";
Romain Guyee916f12010-09-20 17:53:08 -070054const char* gVS_Header_Uniforms_HasGradient[3] = {
55 // Linear
Chet Haasea1d12dd2012-09-21 14:50:14 -070056 "uniform mat4 screenSpace;\n"
57 "uniform float ditherSize;\n",
Romain Guyee916f12010-09-20 17:53:08 -070058 // Circular
Chet Haasea1d12dd2012-09-21 14:50:14 -070059 "uniform mat4 screenSpace;\n"
60 "uniform float ditherSize;\n",
Romain Guyee916f12010-09-20 17:53:08 -070061 // Sweep
Romain Guyee916f12010-09-20 17:53:08 -070062 "uniform mat4 screenSpace;\n"
Chet Haasea1d12dd2012-09-21 14:50:14 -070063 "uniform float ditherSize;\n"
Romain Guyee916f12010-09-20 17:53:08 -070064};
Romain Guy889f8d12010-07-29 14:37:42 -070065const char* gVS_Header_Uniforms_HasBitmap =
66 "uniform mat4 textureTransform;\n"
Romain Guy80bbfb12011-03-23 16:56:28 -070067 "uniform mediump vec2 textureDimension;\n";
Romain Guyac670c02010-07-27 17:39:27 -070068const char* gVS_Header_Varyings_HasTexture =
69 "varying vec2 outTexCoords;\n";
Romain Guyff316ec2013-02-13 18:39:43 -080070const char* gVS_Header_Varyings_HasColors =
71 "varying vec4 outColors;\n";
Chris Craik710f46d2012-09-17 17:25:49 -070072const char* gVS_Header_Varyings_IsAAVertexShape =
Chris Craik6ebdc112012-08-31 18:24:33 -070073 "varying float alpha;\n";
Romain Guy63553472012-07-18 20:04:14 -070074const char* gVS_Header_Varyings_HasBitmap =
75 "varying highp vec2 outBitmapTexCoords;\n";
76const char* gVS_Header_Varyings_PointHasBitmap =
77 "varying highp vec2 outPointBitmapTexCoords;\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -070078const char* gVS_Header_Varyings_HasGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -070079 // Linear
Chet Haasea1d12dd2012-09-21 14:50:14 -070080 "varying highp vec2 linear;\n"
81 "varying vec2 ditherTexCoords;\n",
82 "varying float linear;\n"
83 "varying vec2 ditherTexCoords;\n",
Romain Guy211efea2012-07-31 21:16:07 -070084
Romain Guyee916f12010-09-20 17:53:08 -070085 // Circular
Chet Haasea1d12dd2012-09-21 14:50:14 -070086 "varying highp vec2 circular;\n"
87 "varying vec2 ditherTexCoords;\n",
88 "varying highp vec2 circular;\n"
89 "varying vec2 ditherTexCoords;\n",
Romain Guy211efea2012-07-31 21:16:07 -070090
Romain Guyee916f12010-09-20 17:53:08 -070091 // Sweep
Chet Haasea1d12dd2012-09-21 14:50:14 -070092 "varying highp vec2 sweep;\n"
93 "varying vec2 ditherTexCoords;\n",
94 "varying highp vec2 sweep;\n"
95 "varying vec2 ditherTexCoords;\n",
Romain Guyee916f12010-09-20 17:53:08 -070096};
Romain Guyac670c02010-07-27 17:39:27 -070097const char* gVS_Main =
98 "\nvoid main(void) {\n";
99const char* gVS_Main_OutTexCoords =
100 " outTexCoords = texCoords;\n";
Romain Guyff316ec2013-02-13 18:39:43 -0800101const char* gVS_Main_OutColors =
102 " outColors = colors;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700103const char* gVS_Main_OutTransformedTexCoords =
104 " outTexCoords = (mainTextureTransform * vec4(texCoords, 0.0, 1.0)).xy;\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -0700105const char* gVS_Main_OutGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700106 // Linear
Chet Haasea1d12dd2012-09-21 14:50:14 -0700107 " linear = vec2((screenSpace * position).x, 0.5);\n"
Romain Guy39284b72012-09-26 16:39:40 -0700108 " ditherTexCoords = (transform * position).xy * ditherSize;\n",
Chet Haasea1d12dd2012-09-21 14:50:14 -0700109 " linear = (screenSpace * position).x;\n"
Romain Guy39284b72012-09-26 16:39:40 -0700110 " ditherTexCoords = (transform * position).xy * ditherSize;\n",
Romain Guy211efea2012-07-31 21:16:07 -0700111
Romain Guyee916f12010-09-20 17:53:08 -0700112 // Circular
Chet Haasea1d12dd2012-09-21 14:50:14 -0700113 " circular = (screenSpace * position).xy;\n"
Romain Guy39284b72012-09-26 16:39:40 -0700114 " ditherTexCoords = (transform * position).xy * ditherSize;\n",
Chet Haasea1d12dd2012-09-21 14:50:14 -0700115 " circular = (screenSpace * position).xy;\n"
Romain Guy39284b72012-09-26 16:39:40 -0700116 " ditherTexCoords = (transform * position).xy * ditherSize;\n",
Romain Guy211efea2012-07-31 21:16:07 -0700117
Romain Guyee916f12010-09-20 17:53:08 -0700118 // Sweep
Chet Haasea1d12dd2012-09-21 14:50:14 -0700119 " sweep = (screenSpace * position).xy;\n"
Romain Guy39284b72012-09-26 16:39:40 -0700120 " ditherTexCoords = (transform * position).xy * ditherSize;\n",
Chet Haasea1d12dd2012-09-21 14:50:14 -0700121 " sweep = (screenSpace * position).xy;\n"
Romain Guy39284b72012-09-26 16:39:40 -0700122 " ditherTexCoords = (transform * position).xy * ditherSize;\n",
Romain Guyee916f12010-09-20 17:53:08 -0700123};
Romain Guy889f8d12010-07-29 14:37:42 -0700124const char* gVS_Main_OutBitmapTexCoords =
Romain Guy707b2f72010-10-11 16:34:59 -0700125 " outBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -0700126const char* gVS_Main_OutPointBitmapTexCoords =
127 " outPointBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700128const char* gVS_Main_Position =
Romain Guy39284b72012-09-26 16:39:40 -0700129 " gl_Position = projection * transform * position;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -0700130const char* gVS_Main_PointSize =
131 " gl_PointSize = pointSize;\n";
Chris Craik710f46d2012-09-17 17:25:49 -0700132const char* gVS_Main_AAVertexShape =
Chris Craik6ebdc112012-08-31 18:24:33 -0700133 " alpha = vtxAlpha;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700134const char* gVS_Footer =
135 "}\n\n";
136
137///////////////////////////////////////////////////////////////////////////////
138// Fragment shaders snippets
139///////////////////////////////////////////////////////////////////////////////
140
Romain Guya5aed0d2010-09-09 14:42:43 -0700141const char* gFS_Header_Extension_FramebufferFetch =
142 "#extension GL_NV_shader_framebuffer_fetch : enable\n\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700143const char* gFS_Header_Extension_ExternalTexture =
144 "#extension GL_OES_EGL_image_external : require\n\n";
Romain Guyac670c02010-07-27 17:39:27 -0700145const char* gFS_Header =
146 "precision mediump float;\n\n";
147const char* gFS_Uniforms_Color =
148 "uniform vec4 color;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -0700149const char* gFS_Header_Uniforms_PointHasBitmap =
150 "uniform vec2 textureDimension;\n"
151 "uniform float pointSize;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700152const char* gFS_Uniforms_TextureSampler =
Romain Guya938f562012-09-13 20:31:08 -0700153 "uniform sampler2D baseSampler;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700154const char* gFS_Uniforms_ExternalTextureSampler =
Romain Guya938f562012-09-13 20:31:08 -0700155 "uniform samplerExternalOES baseSampler;\n";
Romain Guy211efea2012-07-31 21:16:07 -0700156#define FS_UNIFORMS_DITHER \
Chet Haasea1d12dd2012-09-21 14:50:14 -0700157 "uniform float ditherSizeSquared;\n" \
Romain Guy211efea2012-07-31 21:16:07 -0700158 "uniform sampler2D ditherSampler;\n"
159#define FS_UNIFORMS_GRADIENT \
160 "uniform vec4 startColor;\n" \
161 "uniform vec4 endColor;\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700162const char* gFS_Uniforms_GradientSampler[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700163 // Linear
Romain Guy211efea2012-07-31 21:16:07 -0700164 FS_UNIFORMS_DITHER "uniform sampler2D gradientSampler;\n",
165 FS_UNIFORMS_DITHER FS_UNIFORMS_GRADIENT,
166
Romain Guyee916f12010-09-20 17:53:08 -0700167 // Circular
Romain Guy211efea2012-07-31 21:16:07 -0700168 FS_UNIFORMS_DITHER "uniform sampler2D gradientSampler;\n",
169 FS_UNIFORMS_DITHER FS_UNIFORMS_GRADIENT,
170
Romain Guyee916f12010-09-20 17:53:08 -0700171 // Sweep
Romain Guy211efea2012-07-31 21:16:07 -0700172 FS_UNIFORMS_DITHER "uniform sampler2D gradientSampler;\n",
173 FS_UNIFORMS_DITHER FS_UNIFORMS_GRADIENT
Romain Guyee916f12010-09-20 17:53:08 -0700174};
Romain Guyac670c02010-07-27 17:39:27 -0700175const char* gFS_Uniforms_BitmapSampler =
176 "uniform sampler2D bitmapSampler;\n";
177const char* gFS_Uniforms_ColorOp[4] = {
178 // None
179 "",
180 // Matrix
181 "uniform mat4 colorMatrix;\n"
182 "uniform vec4 colorMatrixVector;\n",
183 // Lighting
Romain Guydb1938e2010-08-02 18:50:22 -0700184 "uniform vec4 lightingMul;\n"
185 "uniform vec4 lightingAdd;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700186 // PorterDuff
Romain Guydb1938e2010-08-02 18:50:22 -0700187 "uniform vec4 colorBlend;\n"
Romain Guyac670c02010-07-27 17:39:27 -0700188};
Romain Guy41210632012-07-16 17:04:24 -0700189const char* gFS_Uniforms_Gamma =
190 "uniform float gamma;\n";
191
Romain Guyac670c02010-07-27 17:39:27 -0700192const char* gFS_Main =
193 "\nvoid main(void) {\n"
Romain Guy7fbcc042010-08-04 15:40:07 -0700194 " lowp vec4 fragColor;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700195
Romain Guyed6fcb02011-03-21 13:11:28 -0700196const char* gFS_Main_PointBitmapTexCoords =
Romain Guy63553472012-07-18 20:04:14 -0700197 " highp vec2 outBitmapTexCoords = outPointBitmapTexCoords + "
Romain Guyed6fcb02011-03-21 13:11:28 -0700198 "((gl_PointCoord - vec2(0.5, 0.5)) * textureDimension * vec2(pointSize, pointSize));\n";
199
Romain Guy211efea2012-07-31 21:16:07 -0700200#define FS_MAIN_DITHER \
Chet Haasea1d12dd2012-09-21 14:50:14 -0700201 "texture2D(ditherSampler, ditherTexCoords).a * ditherSizeSquared"
Romain Guy211efea2012-07-31 21:16:07 -0700202const char* gFS_Main_AddDitherToGradient =
203 " gradientColor += " FS_MAIN_DITHER ";\n";
204
Romain Guy707b2f72010-10-11 16:34:59 -0700205// Fast cases
206const char* gFS_Fast_SingleColor =
207 "\nvoid main(void) {\n"
208 " gl_FragColor = color;\n"
209 "}\n\n";
210const char* gFS_Fast_SingleTexture =
211 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700212 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700213 "}\n\n";
214const char* gFS_Fast_SingleModulateTexture =
215 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700216 " gl_FragColor = color.a * texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700217 "}\n\n";
218const char* gFS_Fast_SingleA8Texture =
219 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700220 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700221 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700222const char* gFS_Fast_SingleA8Texture_ApplyGamma =
223 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700224 " gl_FragColor = vec4(0.0, 0.0, 0.0, pow(texture2D(baseSampler, outTexCoords).a, gamma));\n"
Romain Guy41210632012-07-16 17:04:24 -0700225 "}\n\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700226const char* gFS_Fast_SingleModulateA8Texture =
227 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700228 " gl_FragColor = color * texture2D(baseSampler, outTexCoords).a;\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700229 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700230const char* gFS_Fast_SingleModulateA8Texture_ApplyGamma =
231 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700232 " gl_FragColor = color * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy41210632012-07-16 17:04:24 -0700233 "}\n\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -0700234const char* gFS_Fast_SingleGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700235 "\nvoid main(void) {\n"
Romain Guy211efea2012-07-31 21:16:07 -0700236 " gl_FragColor = " FS_MAIN_DITHER " + texture2D(gradientSampler, linear);\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700237 "}\n\n",
238 "\nvoid main(void) {\n"
Romain Guy211efea2012-07-31 21:16:07 -0700239 " gl_FragColor = " FS_MAIN_DITHER " + mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700240 "}\n\n"
241};
242const char* gFS_Fast_SingleModulateGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700243 "\nvoid main(void) {\n"
Chet Haase1c5c2062012-09-17 17:09:21 -0700244 " gl_FragColor = " FS_MAIN_DITHER " + color.a * texture2D(gradientSampler, linear);\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700245 "}\n\n",
246 "\nvoid main(void) {\n"
Chet Haase1c5c2062012-09-17 17:09:21 -0700247 " gl_FragColor = " FS_MAIN_DITHER " + color.a * mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700248 "}\n\n"
249};
Romain Guy707b2f72010-10-11 16:34:59 -0700250
251// General case
Romain Guyac670c02010-07-27 17:39:27 -0700252const char* gFS_Main_FetchColor =
253 " fragColor = color;\n";
Romain Guy740bf2b2011-04-26 15:33:10 -0700254const char* gFS_Main_ModulateColor =
255 " fragColor *= color.a;\n";
Chris Craik710f46d2012-09-17 17:25:49 -0700256const char* gFS_Main_AccountForAAVertexShape =
Chris Craik6ebdc112012-08-31 18:24:33 -0700257 " fragColor *= alpha;\n";
Chris Craika798b952012-08-27 17:03:13 -0700258
Romain Guy707b2f72010-10-11 16:34:59 -0700259const char* gFS_Main_FetchTexture[2] = {
260 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700261 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700262 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700263 " fragColor = color * texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700264};
Romain Guya938f562012-09-13 20:31:08 -0700265const char* gFS_Main_FetchA8Texture[4] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700266 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700267 " fragColor = texture2D(baseSampler, outTexCoords);\n",
268 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700269 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700270 " fragColor = color * texture2D(baseSampler, outTexCoords).a;\n",
271 " fragColor = color * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700272};
Romain Guy42e1e0d2012-07-30 14:47:51 -0700273const char* gFS_Main_FetchGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700274 // Linear
Romain Guy320d46b2012-08-08 16:05:42 -0700275 " vec4 gradientColor = texture2D(gradientSampler, linear);\n",
Romain Guy211efea2012-07-31 21:16:07 -0700276
Romain Guy320d46b2012-08-08 16:05:42 -0700277 " vec4 gradientColor = mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700278
Romain Guyee916f12010-09-20 17:53:08 -0700279 // Circular
Romain Guy320d46b2012-08-08 16:05:42 -0700280 " vec4 gradientColor = texture2D(gradientSampler, vec2(length(circular), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700281
Romain Guy320d46b2012-08-08 16:05:42 -0700282 " vec4 gradientColor = mix(startColor, endColor, clamp(length(circular), 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700283
Romain Guyee916f12010-09-20 17:53:08 -0700284 // Sweep
Romain Guy63553472012-07-18 20:04:14 -0700285 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700286 " vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700287
Romain Guy42e1e0d2012-07-30 14:47:51 -0700288 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700289 " vec4 gradientColor = mix(startColor, endColor, clamp(index - floor(index), 0.0, 1.0));\n"
Romain Guyee916f12010-09-20 17:53:08 -0700290};
Romain Guyac670c02010-07-27 17:39:27 -0700291const char* gFS_Main_FetchBitmap =
292 " vec4 bitmapColor = texture2D(bitmapSampler, outBitmapTexCoords);\n";
Romain Guy889f8d12010-07-29 14:37:42 -0700293const char* gFS_Main_FetchBitmapNpot =
294 " vec4 bitmapColor = texture2D(bitmapSampler, wrap(outBitmapTexCoords));\n";
Romain Guyac670c02010-07-27 17:39:27 -0700295const char* gFS_Main_BlendShadersBG =
Romain Guyac670c02010-07-27 17:39:27 -0700296 " fragColor = blendShaders(gradientColor, bitmapColor)";
Romain Guy06f96e22010-07-30 19:18:16 -0700297const char* gFS_Main_BlendShadersGB =
298 " fragColor = blendShaders(bitmapColor, gradientColor)";
Romain Guya938f562012-09-13 20:31:08 -0700299const char* gFS_Main_BlendShaders_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700300 // Don't modulate
301 ";\n",
Romain Guya938f562012-09-13 20:31:08 -0700302 ";\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700303 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700304 " * color.a;\n",
305 " * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700306 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700307 " * texture2D(baseSampler, outTexCoords).a;\n",
308 " * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700309};
Romain Guya938f562012-09-13 20:31:08 -0700310const char* gFS_Main_GradientShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700311 // Don't modulate
312 " fragColor = gradientColor;\n",
Romain Guya938f562012-09-13 20:31:08 -0700313 " fragColor = gradientColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700314 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700315 " fragColor = gradientColor * color.a;\n",
316 " fragColor = gradientColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700317 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700318 " fragColor = gradientColor * texture2D(baseSampler, outTexCoords).a;\n",
319 " fragColor = gradientColor * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700320 };
Romain Guya938f562012-09-13 20:31:08 -0700321const char* gFS_Main_BitmapShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700322 // Don't modulate
323 " fragColor = bitmapColor;\n",
Romain Guya938f562012-09-13 20:31:08 -0700324 " fragColor = bitmapColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700325 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700326 " fragColor = bitmapColor * color.a;\n",
327 " fragColor = bitmapColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700328 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700329 " fragColor = bitmapColor * texture2D(baseSampler, outTexCoords).a;\n",
330 " fragColor = bitmapColor * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700331 };
Romain Guyac670c02010-07-27 17:39:27 -0700332const char* gFS_Main_FragColor =
333 " gl_FragColor = fragColor;\n";
Romain Guyff316ec2013-02-13 18:39:43 -0800334const char* gFS_Main_FragColor_HasColors =
335 " gl_FragColor *= outColors;\n";
Romain Guya5aed0d2010-09-09 14:42:43 -0700336const char* gFS_Main_FragColor_Blend =
337 " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n";
Romain Guyf607bdc2010-09-10 19:20:06 -0700338const char* gFS_Main_FragColor_Blend_Swap =
339 " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n";
Romain Guyac670c02010-07-27 17:39:27 -0700340const char* gFS_Main_ApplyColorOp[4] = {
341 // None
342 "",
343 // Matrix
344 " fragColor *= colorMatrix;\n"
Romain Guydb1938e2010-08-02 18:50:22 -0700345 " fragColor += colorMatrixVector;\n"
346 " fragColor.rgb *= fragColor.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700347 // Lighting
Romain Guydb1938e2010-08-02 18:50:22 -0700348 " float lightingAlpha = fragColor.a;\n"
349 " fragColor = min(fragColor * lightingMul + (lightingAdd * lightingAlpha), lightingAlpha);\n"
350 " fragColor.a = lightingAlpha;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700351 // PorterDuff
352 " fragColor = blendColors(colorBlend, fragColor);\n"
353};
354const char* gFS_Footer =
355 "}\n\n";
356
357///////////////////////////////////////////////////////////////////////////////
358// PorterDuff snippets
359///////////////////////////////////////////////////////////////////////////////
360
Romain Guy48daa542010-08-10 19:21:34 -0700361const char* gBlendOps[18] = {
Romain Guyac670c02010-07-27 17:39:27 -0700362 // Clear
363 "return vec4(0.0, 0.0, 0.0, 0.0);\n",
364 // Src
365 "return src;\n",
366 // Dst
367 "return dst;\n",
368 // SrcOver
Romain Guy06f96e22010-07-30 19:18:16 -0700369 "return src + dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700370 // DstOver
Romain Guy06f96e22010-07-30 19:18:16 -0700371 "return dst + src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700372 // SrcIn
Romain Guy06f96e22010-07-30 19:18:16 -0700373 "return src * dst.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700374 // DstIn
Romain Guy06f96e22010-07-30 19:18:16 -0700375 "return dst * src.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700376 // SrcOut
Romain Guy06f96e22010-07-30 19:18:16 -0700377 "return src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700378 // DstOut
Romain Guy06f96e22010-07-30 19:18:16 -0700379 "return dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700380 // SrcAtop
381 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n",
382 // DstAtop
383 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n",
384 // Xor
Romain Guy48daa542010-08-10 19:21:34 -0700385 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, "
Romain Guyac670c02010-07-27 17:39:27 -0700386 "src.a + dst.a - 2.0 * src.a * dst.a);\n",
Romain Guy48daa542010-08-10 19:21:34 -0700387 // Add
388 "return min(src + dst, 1.0);\n",
389 // Multiply
390 "return src * dst;\n",
391 // Screen
392 "return src + dst - src * dst;\n",
393 // Overlay
394 "return clamp(vec4(mix("
395 "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), "
396 "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), "
397 "step(dst.a, 2.0 * dst.rgb)), "
398 "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n",
399 // Darken
400 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
401 "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
402 // Lighten
403 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
404 "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 -0700405};
406
407///////////////////////////////////////////////////////////////////////////////
408// Constructors/destructors
409///////////////////////////////////////////////////////////////////////////////
410
411ProgramCache::ProgramCache() {
412}
413
414ProgramCache::~ProgramCache() {
415 clear();
416}
417
418///////////////////////////////////////////////////////////////////////////////
419// Cache management
420///////////////////////////////////////////////////////////////////////////////
421
422void ProgramCache::clear() {
Romain Guy67f27952010-12-07 20:09:23 -0800423 PROGRAM_LOGD("Clearing program cache");
424
Romain Guyac670c02010-07-27 17:39:27 -0700425 size_t count = mCache.size();
426 for (size_t i = 0; i < count; i++) {
427 delete mCache.valueAt(i);
428 }
429 mCache.clear();
430}
431
432Program* ProgramCache::get(const ProgramDescription& description) {
433 programid key = description.key();
434 ssize_t index = mCache.indexOfKey(key);
435 Program* program = NULL;
436 if (index < 0) {
Romain Guyee916f12010-09-20 17:53:08 -0700437 description.log("Could not find program");
Romain Guyac670c02010-07-27 17:39:27 -0700438 program = generateProgram(description, key);
439 mCache.add(key, program);
440 } else {
441 program = mCache.valueAt(index);
442 }
443 return program;
444}
445
446///////////////////////////////////////////////////////////////////////////////
447// Program generation
448///////////////////////////////////////////////////////////////////////////////
449
450Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) {
451 String8 vertexShader = generateVertexShader(description);
452 String8 fragmentShader = generateFragmentShader(description);
453
Romain Guy42e1e0d2012-07-30 14:47:51 -0700454 return new Program(description, vertexShader.string(), fragmentShader.string());
455}
456
457static inline size_t gradientIndex(const ProgramDescription& description) {
458 return description.gradientType * 2 + description.isSimpleGradient;
Romain Guyac670c02010-07-27 17:39:27 -0700459}
460
461String8 ProgramCache::generateVertexShader(const ProgramDescription& description) {
462 // Add attributes
463 String8 shader(gVS_Header_Attributes);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700464 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700465 shader.append(gVS_Header_Attributes_TexCoords);
466 }
Chris Craik710f46d2012-09-17 17:25:49 -0700467 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800468 shader.append(gVS_Header_Attributes_AAVertexShapeParameters);
Chet Haase5b0200b2011-04-13 17:58:08 -0700469 }
Romain Guyff316ec2013-02-13 18:39:43 -0800470 if (description.hasColors) {
471 shader.append(gVS_Header_Attributes_Colors);
472 }
Romain Guyac670c02010-07-27 17:39:27 -0700473 // Uniforms
474 shader.append(gVS_Header_Uniforms);
Romain Guy8f0095c2011-05-02 17:24:22 -0700475 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700476 shader.append(gVS_Header_Uniforms_TextureTransform);
477 }
Romain Guyac670c02010-07-27 17:39:27 -0700478 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700479 shader.append(gVS_Header_Uniforms_HasGradient[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700480 }
Romain Guy889f8d12010-07-29 14:37:42 -0700481 if (description.hasBitmap) {
482 shader.append(gVS_Header_Uniforms_HasBitmap);
483 }
Romain Guyed6fcb02011-03-21 13:11:28 -0700484 if (description.isPoint) {
485 shader.append(gVS_Header_Uniforms_IsPoint);
486 }
Romain Guyac670c02010-07-27 17:39:27 -0700487 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700488 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700489 shader.append(gVS_Header_Varyings_HasTexture);
490 }
Chris Craik710f46d2012-09-17 17:25:49 -0700491 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800492 shader.append(gVS_Header_Varyings_IsAAVertexShape);
Chet Haase5b0200b2011-04-13 17:58:08 -0700493 }
Romain Guyff316ec2013-02-13 18:39:43 -0800494 if (description.hasColors) {
495 shader.append(gVS_Header_Varyings_HasColors);
496 }
Romain Guyac670c02010-07-27 17:39:27 -0700497 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700498 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700499 }
500 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700501 shader.append(description.isPoint ?
Romain Guy63553472012-07-18 20:04:14 -0700502 gVS_Header_Varyings_PointHasBitmap :
503 gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700504 }
505
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 Craik710f46d2012-09-17 17:25:49 -0700513 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800514 shader.append(gVS_Main_AAVertexShape);
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) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700520 shader.append(description.isPoint ?
521 gVS_Main_OutPointBitmapTexCoords :
522 gVS_Main_OutBitmapTexCoords);
523 }
524 if (description.isPoint) {
525 shader.append(gVS_Main_PointSize);
Romain Guy889f8d12010-07-29 14:37:42 -0700526 }
Romain Guyac670c02010-07-27 17:39:27 -0700527 // Output transformed position
528 shader.append(gVS_Main_Position);
Chet Haasea1d12dd2012-09-21 14:50:14 -0700529 if (description.hasGradient) {
530 shader.append(gVS_Main_OutGradient[gradientIndex(description)]);
531 }
Romain Guyac670c02010-07-27 17:39:27 -0700532 }
533 // End the shader
534 shader.append(gVS_Footer);
535
536 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string());
537
538 return shader;
539}
540
Romain Guya938f562012-09-13 20:31:08 -0700541static bool shaderOp(const ProgramDescription& description, String8& shader,
542 const int modulateOp, const char** snippets) {
543 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
544 op = op * 2 + description.hasGammaCorrection;
545 shader.append(snippets[op]);
546 return description.hasAlpha8Texture;
547}
548
Romain Guyac670c02010-07-27 17:39:27 -0700549String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) {
Romain Guya5aed0d2010-09-09 14:42:43 -0700550 String8 shader;
551
Romain Guy707b2f72010-10-11 16:34:59 -0700552 const bool blendFramebuffer = description.framebufferMode >= SkXfermode::kPlus_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700553 if (blendFramebuffer) {
554 shader.append(gFS_Header_Extension_FramebufferFetch);
555 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700556 if (description.hasExternalTexture) {
557 shader.append(gFS_Header_Extension_ExternalTexture);
558 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700559
560 shader.append(gFS_Header);
Romain Guyac670c02010-07-27 17:39:27 -0700561
562 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700563 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700564 shader.append(gVS_Header_Varyings_HasTexture);
565 }
Chris Craik710f46d2012-09-17 17:25:49 -0700566 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800567 shader.append(gVS_Header_Varyings_IsAAVertexShape);
Chet Haase5b0200b2011-04-13 17:58:08 -0700568 }
Romain Guyff316ec2013-02-13 18:39:43 -0800569 if (description.hasColors) {
570 shader.append(gVS_Header_Varyings_HasColors);
571 }
Romain Guyac670c02010-07-27 17:39:27 -0700572 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700573 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700574 }
575 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700576 shader.append(description.isPoint ?
Romain Guy63553472012-07-18 20:04:14 -0700577 gVS_Header_Varyings_PointHasBitmap :
578 gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700579 }
580
Romain Guyac670c02010-07-27 17:39:27 -0700581 // Uniforms
Romain Guy707b2f72010-10-11 16:34:59 -0700582 int modulateOp = MODULATE_OP_NO_MODULATE;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700583 const bool singleColor = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700584 !description.hasGradient && !description.hasBitmap;
585
586 if (description.modulate || singleColor) {
587 shader.append(gFS_Uniforms_Color);
588 if (!singleColor) modulateOp = MODULATE_OP_MODULATE;
589 }
Romain Guyac670c02010-07-27 17:39:27 -0700590 if (description.hasTexture) {
591 shader.append(gFS_Uniforms_TextureSampler);
Romain Guy8f0095c2011-05-02 17:24:22 -0700592 } else if (description.hasExternalTexture) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700593 shader.append(gFS_Uniforms_ExternalTextureSampler);
594 }
Romain Guyac670c02010-07-27 17:39:27 -0700595 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700596 shader.append(gFS_Uniforms_GradientSampler[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700597 }
Romain Guyed6fcb02011-03-21 13:11:28 -0700598 if (description.hasBitmap && description.isPoint) {
599 shader.append(gFS_Header_Uniforms_PointHasBitmap);
600 }
Romain Guy41210632012-07-16 17:04:24 -0700601 if (description.hasGammaCorrection) {
602 shader.append(gFS_Uniforms_Gamma);
603 }
Romain Guy707b2f72010-10-11 16:34:59 -0700604
605 // Optimization for common cases
Romain Guyff316ec2013-02-13 18:39:43 -0800606 if (!description.isAA && !blendFramebuffer && !description.hasColors &&
Chris Craik65cd6122012-12-10 17:56:27 -0800607 description.colorOp == ProgramDescription::kColorNone && !description.isPoint) {
Romain Guy707b2f72010-10-11 16:34:59 -0700608 bool fast = false;
609
610 const bool noShader = !description.hasGradient && !description.hasBitmap;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700611 const bool singleTexture = (description.hasTexture || description.hasExternalTexture) &&
Romain Guy707b2f72010-10-11 16:34:59 -0700612 !description.hasAlpha8Texture && noShader;
613 const bool singleA8Texture = description.hasTexture &&
614 description.hasAlpha8Texture && noShader;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700615 const bool singleGradient = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700616 description.hasGradient && !description.hasBitmap &&
617 description.gradientType == ProgramDescription::kGradientLinear;
618
619 if (singleColor) {
620 shader.append(gFS_Fast_SingleColor);
621 fast = true;
622 } else if (singleTexture) {
623 if (!description.modulate) {
624 shader.append(gFS_Fast_SingleTexture);
625 } else {
626 shader.append(gFS_Fast_SingleModulateTexture);
627 }
628 fast = true;
629 } else if (singleA8Texture) {
630 if (!description.modulate) {
Romain Guy41210632012-07-16 17:04:24 -0700631 if (description.hasGammaCorrection) {
632 shader.append(gFS_Fast_SingleA8Texture_ApplyGamma);
633 } else {
634 shader.append(gFS_Fast_SingleA8Texture);
635 }
Romain Guy707b2f72010-10-11 16:34:59 -0700636 } else {
Romain Guy41210632012-07-16 17:04:24 -0700637 if (description.hasGammaCorrection) {
638 shader.append(gFS_Fast_SingleModulateA8Texture_ApplyGamma);
639 } else {
640 shader.append(gFS_Fast_SingleModulateA8Texture);
641 }
Romain Guy707b2f72010-10-11 16:34:59 -0700642 }
643 fast = true;
644 } else if (singleGradient) {
645 if (!description.modulate) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700646 shader.append(gFS_Fast_SingleGradient[description.isSimpleGradient]);
Romain Guy707b2f72010-10-11 16:34:59 -0700647 } else {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700648 shader.append(gFS_Fast_SingleModulateGradient[description.isSimpleGradient]);
Romain Guy707b2f72010-10-11 16:34:59 -0700649 }
650 fast = true;
651 }
652
653 if (fast) {
Romain Guyc15008e2010-11-10 11:59:15 -0800654#if DEBUG_PROGRAMS
Romain Guy707b2f72010-10-11 16:34:59 -0700655 PROGRAM_LOGD("*** Fast case:\n");
656 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
657 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800658#endif
Romain Guy707b2f72010-10-11 16:34:59 -0700659
660 return shader;
661 }
662 }
663
Romain Guyac670c02010-07-27 17:39:27 -0700664 if (description.hasBitmap) {
665 shader.append(gFS_Uniforms_BitmapSampler);
666 }
667 shader.append(gFS_Uniforms_ColorOp[description.colorOp]);
668
669 // Generate required functions
670 if (description.hasGradient && description.hasBitmap) {
Romain Guy48daa542010-08-10 19:21:34 -0700671 generateBlend(shader, "blendShaders", description.shadersMode);
Romain Guyac670c02010-07-27 17:39:27 -0700672 }
673 if (description.colorOp == ProgramDescription::kColorBlend) {
Romain Guy48daa542010-08-10 19:21:34 -0700674 generateBlend(shader, "blendColors", description.colorMode);
Romain Guyac670c02010-07-27 17:39:27 -0700675 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700676 if (blendFramebuffer) {
677 generateBlend(shader, "blendFramebuffer", description.framebufferMode);
678 }
Romain Guy889f8d12010-07-29 14:37:42 -0700679 if (description.isBitmapNpot) {
680 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
681 }
Romain Guyac670c02010-07-27 17:39:27 -0700682
683 // Begin the shader
684 shader.append(gFS_Main); {
685 // Stores the result in fragColor directly
Romain Guyaa6c24c2011-04-28 18:40:04 -0700686 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700687 if (description.hasAlpha8Texture) {
Romain Guy707b2f72010-10-11 16:34:59 -0700688 if (!description.hasGradient && !description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700689 shader.append(gFS_Main_FetchA8Texture[modulateOp * 2 +
690 description.hasGammaCorrection]);
Romain Guy707b2f72010-10-11 16:34:59 -0700691 }
Romain Guyac670c02010-07-27 17:39:27 -0700692 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700693 shader.append(gFS_Main_FetchTexture[modulateOp]);
Romain Guyac670c02010-07-27 17:39:27 -0700694 }
695 } else {
Romain Guya938f562012-09-13 20:31:08 -0700696 if (!description.hasGradient && !description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700697 shader.append(gFS_Main_FetchColor);
698 }
Romain Guyac670c02010-07-27 17:39:27 -0700699 }
700 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700701 shader.append(gFS_Main_FetchGradient[gradientIndex(description)]);
Romain Guy211efea2012-07-31 21:16:07 -0700702 shader.append(gFS_Main_AddDitherToGradient);
Romain Guyac670c02010-07-27 17:39:27 -0700703 }
704 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700705 if (description.isPoint) {
706 shader.append(gFS_Main_PointBitmapTexCoords);
707 }
Romain Guy889f8d12010-07-29 14:37:42 -0700708 if (!description.isBitmapNpot) {
709 shader.append(gFS_Main_FetchBitmap);
710 } else {
711 shader.append(gFS_Main_FetchBitmapNpot);
712 }
Romain Guyac670c02010-07-27 17:39:27 -0700713 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700714 bool applyModulate = false;
Romain Guyac670c02010-07-27 17:39:27 -0700715 // Case when we have two shaders set
716 if (description.hasGradient && description.hasBitmap) {
717 if (description.isBitmapFirst) {
718 shader.append(gFS_Main_BlendShadersBG);
719 } else {
720 shader.append(gFS_Main_BlendShadersGB);
721 }
Romain Guya938f562012-09-13 20:31:08 -0700722 applyModulate = shaderOp(description, shader, modulateOp,
723 gFS_Main_BlendShaders_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700724 } else {
725 if (description.hasGradient) {
Romain Guya938f562012-09-13 20:31:08 -0700726 applyModulate = shaderOp(description, shader, modulateOp,
727 gFS_Main_GradientShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700728 } else if (description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700729 applyModulate = shaderOp(description, shader, modulateOp,
730 gFS_Main_BitmapShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700731 }
Romain Guyac670c02010-07-27 17:39:27 -0700732 }
Romain Guya938f562012-09-13 20:31:08 -0700733
Romain Guy740bf2b2011-04-26 15:33:10 -0700734 if (description.modulate && applyModulate) {
Romain Guya938f562012-09-13 20:31:08 -0700735 shader.append(gFS_Main_ModulateColor);
Romain Guy740bf2b2011-04-26 15:33:10 -0700736 }
Romain Guya938f562012-09-13 20:31:08 -0700737
Romain Guyac670c02010-07-27 17:39:27 -0700738 // Apply the color op if needed
739 shader.append(gFS_Main_ApplyColorOp[description.colorOp]);
Chris Craik9f44a132012-09-13 18:34:55 -0700740
Chris Craik710f46d2012-09-17 17:25:49 -0700741 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800742 shader.append(gFS_Main_AccountForAAVertexShape);
Chris Craik9f44a132012-09-13 18:34:55 -0700743 }
744
Romain Guyac670c02010-07-27 17:39:27 -0700745 // Output the fragment
Romain Guya5aed0d2010-09-09 14:42:43 -0700746 if (!blendFramebuffer) {
747 shader.append(gFS_Main_FragColor);
748 } else {
Romain Guyf607bdc2010-09-10 19:20:06 -0700749 shader.append(!description.swapSrcDst ?
750 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap);
Romain Guya5aed0d2010-09-09 14:42:43 -0700751 }
Romain Guyff316ec2013-02-13 18:39:43 -0800752 if (description.hasColors) {
753 shader.append(gFS_Main_FragColor_HasColors);
754 }
Romain Guyac670c02010-07-27 17:39:27 -0700755 }
756 // End the shader
757 shader.append(gFS_Footer);
758
Romain Guyc15008e2010-11-10 11:59:15 -0800759#if DEBUG_PROGRAMS
Romain Guydb1938e2010-08-02 18:50:22 -0700760 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
761 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800762#endif
Romain Guydb1938e2010-08-02 18:50:22 -0700763
Romain Guyac670c02010-07-27 17:39:27 -0700764 return shader;
765}
766
Romain Guy48daa542010-08-10 19:21:34 -0700767void ProgramCache::generateBlend(String8& shader, const char* name, SkXfermode::Mode mode) {
Romain Guyac670c02010-07-27 17:39:27 -0700768 shader.append("\nvec4 ");
769 shader.append(name);
770 shader.append("(vec4 src, vec4 dst) {\n");
771 shader.append(" ");
Romain Guy48daa542010-08-10 19:21:34 -0700772 shader.append(gBlendOps[mode]);
Romain Guyac670c02010-07-27 17:39:27 -0700773 shader.append("}\n");
774}
775
Romain Guy889f8d12010-07-29 14:37:42 -0700776void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) {
Romain Guy63553472012-07-18 20:04:14 -0700777 shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700778 if (wrapS == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700779 shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700780 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n");
781 }
782 if (wrapT == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700783 shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700784 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n");
785 }
786 shader.append(" return vec2(");
787 switch (wrapS) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700788 case GL_CLAMP_TO_EDGE:
789 shader.append("texCoords.x");
790 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700791 case GL_REPEAT:
792 shader.append("mod(texCoords.x, 1.0)");
793 break;
794 case GL_MIRRORED_REPEAT:
795 shader.append("xMod2");
796 break;
797 }
798 shader.append(", ");
799 switch (wrapT) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700800 case GL_CLAMP_TO_EDGE:
801 shader.append("texCoords.y");
802 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700803 case GL_REPEAT:
804 shader.append("mod(texCoords.y, 1.0)");
805 break;
806 case GL_MIRRORED_REPEAT:
807 shader.append("yMod2");
808 break;
809 }
810 shader.append(");\n");
811 shader.append("}\n");
812}
813
Romain Guydb1938e2010-08-02 18:50:22 -0700814void ProgramCache::printLongString(const String8& shader) const {
815 ssize_t index = 0;
816 ssize_t lastIndex = 0;
817 const char* str = shader.string();
818 while ((index = shader.find("\n", index)) > -1) {
819 String8 line(str, index - lastIndex);
820 if (line.length() == 0) line.append("\n");
821 PROGRAM_LOGD("%s", line.string());
822 index++;
823 str += (index - lastIndex);
824 lastIndex = index;
825 }
826}
827
Romain Guyac670c02010-07-27 17:39:27 -0700828}; // namespace uirenderer
829}; // namespace android