blob: 0f014cbfa96acff77a7896a62cbfab8347a7f597 [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};
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800354const char* gFS_Main_DebugHighlight =
355 " gl_FragColor.rgb = vec3(0.0, gl_FragColor.a, 0.0);\n";
Romain Guyac670c02010-07-27 17:39:27 -0700356const char* gFS_Footer =
357 "}\n\n";
358
359///////////////////////////////////////////////////////////////////////////////
360// PorterDuff snippets
361///////////////////////////////////////////////////////////////////////////////
362
Romain Guy48daa542010-08-10 19:21:34 -0700363const char* gBlendOps[18] = {
Romain Guyac670c02010-07-27 17:39:27 -0700364 // Clear
365 "return vec4(0.0, 0.0, 0.0, 0.0);\n",
366 // Src
367 "return src;\n",
368 // Dst
369 "return dst;\n",
370 // SrcOver
Romain Guy06f96e22010-07-30 19:18:16 -0700371 "return src + dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700372 // DstOver
Romain Guy06f96e22010-07-30 19:18:16 -0700373 "return dst + src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700374 // SrcIn
Romain Guy06f96e22010-07-30 19:18:16 -0700375 "return src * dst.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700376 // DstIn
Romain Guy06f96e22010-07-30 19:18:16 -0700377 "return dst * src.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700378 // SrcOut
Romain Guy06f96e22010-07-30 19:18:16 -0700379 "return src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700380 // DstOut
Romain Guy06f96e22010-07-30 19:18:16 -0700381 "return dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700382 // SrcAtop
383 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n",
384 // DstAtop
385 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n",
386 // Xor
Romain Guy48daa542010-08-10 19:21:34 -0700387 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, "
Romain Guyac670c02010-07-27 17:39:27 -0700388 "src.a + dst.a - 2.0 * src.a * dst.a);\n",
Romain Guy48daa542010-08-10 19:21:34 -0700389 // Add
390 "return min(src + dst, 1.0);\n",
391 // Multiply
392 "return src * dst;\n",
393 // Screen
394 "return src + dst - src * dst;\n",
395 // Overlay
396 "return clamp(vec4(mix("
397 "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), "
398 "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), "
399 "step(dst.a, 2.0 * dst.rgb)), "
400 "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n",
401 // Darken
402 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
403 "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
404 // Lighten
405 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
406 "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 -0700407};
408
409///////////////////////////////////////////////////////////////////////////////
410// Constructors/destructors
411///////////////////////////////////////////////////////////////////////////////
412
413ProgramCache::ProgramCache() {
414}
415
416ProgramCache::~ProgramCache() {
417 clear();
418}
419
420///////////////////////////////////////////////////////////////////////////////
421// Cache management
422///////////////////////////////////////////////////////////////////////////////
423
424void ProgramCache::clear() {
Romain Guy67f27952010-12-07 20:09:23 -0800425 PROGRAM_LOGD("Clearing program cache");
426
Romain Guyac670c02010-07-27 17:39:27 -0700427 size_t count = mCache.size();
428 for (size_t i = 0; i < count; i++) {
429 delete mCache.valueAt(i);
430 }
431 mCache.clear();
432}
433
434Program* ProgramCache::get(const ProgramDescription& description) {
435 programid key = description.key();
436 ssize_t index = mCache.indexOfKey(key);
437 Program* program = NULL;
438 if (index < 0) {
Romain Guyee916f12010-09-20 17:53:08 -0700439 description.log("Could not find program");
Romain Guyac670c02010-07-27 17:39:27 -0700440 program = generateProgram(description, key);
441 mCache.add(key, program);
442 } else {
443 program = mCache.valueAt(index);
444 }
445 return program;
446}
447
448///////////////////////////////////////////////////////////////////////////////
449// Program generation
450///////////////////////////////////////////////////////////////////////////////
451
452Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) {
453 String8 vertexShader = generateVertexShader(description);
454 String8 fragmentShader = generateFragmentShader(description);
455
Romain Guy42e1e0d2012-07-30 14:47:51 -0700456 return new Program(description, vertexShader.string(), fragmentShader.string());
457}
458
459static inline size_t gradientIndex(const ProgramDescription& description) {
460 return description.gradientType * 2 + description.isSimpleGradient;
Romain Guyac670c02010-07-27 17:39:27 -0700461}
462
463String8 ProgramCache::generateVertexShader(const ProgramDescription& description) {
464 // Add attributes
465 String8 shader(gVS_Header_Attributes);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700466 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700467 shader.append(gVS_Header_Attributes_TexCoords);
468 }
Chris Craik710f46d2012-09-17 17:25:49 -0700469 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800470 shader.append(gVS_Header_Attributes_AAVertexShapeParameters);
Chet Haase5b0200b2011-04-13 17:58:08 -0700471 }
Romain Guyff316ec2013-02-13 18:39:43 -0800472 if (description.hasColors) {
473 shader.append(gVS_Header_Attributes_Colors);
474 }
Romain Guyac670c02010-07-27 17:39:27 -0700475 // Uniforms
476 shader.append(gVS_Header_Uniforms);
Romain Guy8f0095c2011-05-02 17:24:22 -0700477 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700478 shader.append(gVS_Header_Uniforms_TextureTransform);
479 }
Romain Guyac670c02010-07-27 17:39:27 -0700480 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700481 shader.append(gVS_Header_Uniforms_HasGradient[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700482 }
Romain Guy889f8d12010-07-29 14:37:42 -0700483 if (description.hasBitmap) {
484 shader.append(gVS_Header_Uniforms_HasBitmap);
485 }
Romain Guyed6fcb02011-03-21 13:11:28 -0700486 if (description.isPoint) {
487 shader.append(gVS_Header_Uniforms_IsPoint);
488 }
Romain Guyac670c02010-07-27 17:39:27 -0700489 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700490 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700491 shader.append(gVS_Header_Varyings_HasTexture);
492 }
Chris Craik710f46d2012-09-17 17:25:49 -0700493 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800494 shader.append(gVS_Header_Varyings_IsAAVertexShape);
Chet Haase5b0200b2011-04-13 17:58:08 -0700495 }
Romain Guyff316ec2013-02-13 18:39:43 -0800496 if (description.hasColors) {
497 shader.append(gVS_Header_Varyings_HasColors);
498 }
Romain Guyac670c02010-07-27 17:39:27 -0700499 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700500 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700501 }
502 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700503 shader.append(description.isPoint ?
Romain Guy63553472012-07-18 20:04:14 -0700504 gVS_Header_Varyings_PointHasBitmap :
505 gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700506 }
507
508 // Begin the shader
509 shader.append(gVS_Main); {
Romain Guy8f0095c2011-05-02 17:24:22 -0700510 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700511 shader.append(gVS_Main_OutTransformedTexCoords);
Romain Guy8f0095c2011-05-02 17:24:22 -0700512 } else if (description.hasTexture || description.hasExternalTexture) {
513 shader.append(gVS_Main_OutTexCoords);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700514 }
Chris Craik710f46d2012-09-17 17:25:49 -0700515 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800516 shader.append(gVS_Main_AAVertexShape);
Chet Haase5b0200b2011-04-13 17:58:08 -0700517 }
Romain Guyff316ec2013-02-13 18:39:43 -0800518 if (description.hasColors) {
519 shader.append(gVS_Main_OutColors);
520 }
Romain Guy889f8d12010-07-29 14:37:42 -0700521 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700522 shader.append(description.isPoint ?
523 gVS_Main_OutPointBitmapTexCoords :
524 gVS_Main_OutBitmapTexCoords);
525 }
526 if (description.isPoint) {
527 shader.append(gVS_Main_PointSize);
Romain Guy889f8d12010-07-29 14:37:42 -0700528 }
Romain Guyac670c02010-07-27 17:39:27 -0700529 // Output transformed position
530 shader.append(gVS_Main_Position);
Chet Haasea1d12dd2012-09-21 14:50:14 -0700531 if (description.hasGradient) {
532 shader.append(gVS_Main_OutGradient[gradientIndex(description)]);
533 }
Romain Guyac670c02010-07-27 17:39:27 -0700534 }
535 // End the shader
536 shader.append(gVS_Footer);
537
538 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string());
539
540 return shader;
541}
542
Romain Guya938f562012-09-13 20:31:08 -0700543static bool shaderOp(const ProgramDescription& description, String8& shader,
544 const int modulateOp, const char** snippets) {
545 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
546 op = op * 2 + description.hasGammaCorrection;
547 shader.append(snippets[op]);
548 return description.hasAlpha8Texture;
549}
550
Romain Guyac670c02010-07-27 17:39:27 -0700551String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) {
Romain Guya5aed0d2010-09-09 14:42:43 -0700552 String8 shader;
553
Romain Guy707b2f72010-10-11 16:34:59 -0700554 const bool blendFramebuffer = description.framebufferMode >= SkXfermode::kPlus_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700555 if (blendFramebuffer) {
556 shader.append(gFS_Header_Extension_FramebufferFetch);
557 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700558 if (description.hasExternalTexture) {
559 shader.append(gFS_Header_Extension_ExternalTexture);
560 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700561
562 shader.append(gFS_Header);
Romain Guyac670c02010-07-27 17:39:27 -0700563
564 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700565 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700566 shader.append(gVS_Header_Varyings_HasTexture);
567 }
Chris Craik710f46d2012-09-17 17:25:49 -0700568 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800569 shader.append(gVS_Header_Varyings_IsAAVertexShape);
Chet Haase5b0200b2011-04-13 17:58:08 -0700570 }
Romain Guyff316ec2013-02-13 18:39:43 -0800571 if (description.hasColors) {
572 shader.append(gVS_Header_Varyings_HasColors);
573 }
Romain Guyac670c02010-07-27 17:39:27 -0700574 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700575 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700576 }
577 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700578 shader.append(description.isPoint ?
Romain Guy63553472012-07-18 20:04:14 -0700579 gVS_Header_Varyings_PointHasBitmap :
580 gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700581 }
582
Romain Guyac670c02010-07-27 17:39:27 -0700583 // Uniforms
Romain Guy707b2f72010-10-11 16:34:59 -0700584 int modulateOp = MODULATE_OP_NO_MODULATE;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700585 const bool singleColor = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700586 !description.hasGradient && !description.hasBitmap;
587
588 if (description.modulate || singleColor) {
589 shader.append(gFS_Uniforms_Color);
590 if (!singleColor) modulateOp = MODULATE_OP_MODULATE;
591 }
Romain Guyac670c02010-07-27 17:39:27 -0700592 if (description.hasTexture) {
593 shader.append(gFS_Uniforms_TextureSampler);
Romain Guy8f0095c2011-05-02 17:24:22 -0700594 } else if (description.hasExternalTexture) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700595 shader.append(gFS_Uniforms_ExternalTextureSampler);
596 }
Romain Guyac670c02010-07-27 17:39:27 -0700597 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700598 shader.append(gFS_Uniforms_GradientSampler[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700599 }
Romain Guyed6fcb02011-03-21 13:11:28 -0700600 if (description.hasBitmap && description.isPoint) {
601 shader.append(gFS_Header_Uniforms_PointHasBitmap);
602 }
Romain Guy41210632012-07-16 17:04:24 -0700603 if (description.hasGammaCorrection) {
604 shader.append(gFS_Uniforms_Gamma);
605 }
Romain Guy707b2f72010-10-11 16:34:59 -0700606
607 // Optimization for common cases
Romain Guyff316ec2013-02-13 18:39:43 -0800608 if (!description.isAA && !blendFramebuffer && !description.hasColors &&
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800609 description.colorOp == ProgramDescription::kColorNone &&
610 !description.isPoint && !description.hasDebugHighlight) {
Romain Guy707b2f72010-10-11 16:34:59 -0700611 bool fast = false;
612
613 const bool noShader = !description.hasGradient && !description.hasBitmap;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700614 const bool singleTexture = (description.hasTexture || description.hasExternalTexture) &&
Romain Guy707b2f72010-10-11 16:34:59 -0700615 !description.hasAlpha8Texture && noShader;
616 const bool singleA8Texture = description.hasTexture &&
617 description.hasAlpha8Texture && noShader;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700618 const bool singleGradient = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700619 description.hasGradient && !description.hasBitmap &&
620 description.gradientType == ProgramDescription::kGradientLinear;
621
622 if (singleColor) {
623 shader.append(gFS_Fast_SingleColor);
624 fast = true;
625 } else if (singleTexture) {
626 if (!description.modulate) {
627 shader.append(gFS_Fast_SingleTexture);
628 } else {
629 shader.append(gFS_Fast_SingleModulateTexture);
630 }
631 fast = true;
632 } else if (singleA8Texture) {
633 if (!description.modulate) {
Romain Guy41210632012-07-16 17:04:24 -0700634 if (description.hasGammaCorrection) {
635 shader.append(gFS_Fast_SingleA8Texture_ApplyGamma);
636 } else {
637 shader.append(gFS_Fast_SingleA8Texture);
638 }
Romain Guy707b2f72010-10-11 16:34:59 -0700639 } else {
Romain Guy41210632012-07-16 17:04:24 -0700640 if (description.hasGammaCorrection) {
641 shader.append(gFS_Fast_SingleModulateA8Texture_ApplyGamma);
642 } else {
643 shader.append(gFS_Fast_SingleModulateA8Texture);
644 }
Romain Guy707b2f72010-10-11 16:34:59 -0700645 }
646 fast = true;
647 } else if (singleGradient) {
648 if (!description.modulate) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700649 shader.append(gFS_Fast_SingleGradient[description.isSimpleGradient]);
Romain Guy707b2f72010-10-11 16:34:59 -0700650 } else {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700651 shader.append(gFS_Fast_SingleModulateGradient[description.isSimpleGradient]);
Romain Guy707b2f72010-10-11 16:34:59 -0700652 }
653 fast = true;
654 }
655
656 if (fast) {
Romain Guyc15008e2010-11-10 11:59:15 -0800657#if DEBUG_PROGRAMS
Romain Guy707b2f72010-10-11 16:34:59 -0700658 PROGRAM_LOGD("*** Fast case:\n");
659 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
660 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800661#endif
Romain Guy707b2f72010-10-11 16:34:59 -0700662
663 return shader;
664 }
665 }
666
Romain Guyac670c02010-07-27 17:39:27 -0700667 if (description.hasBitmap) {
668 shader.append(gFS_Uniforms_BitmapSampler);
669 }
670 shader.append(gFS_Uniforms_ColorOp[description.colorOp]);
671
672 // Generate required functions
673 if (description.hasGradient && description.hasBitmap) {
Romain Guy48daa542010-08-10 19:21:34 -0700674 generateBlend(shader, "blendShaders", description.shadersMode);
Romain Guyac670c02010-07-27 17:39:27 -0700675 }
676 if (description.colorOp == ProgramDescription::kColorBlend) {
Romain Guy48daa542010-08-10 19:21:34 -0700677 generateBlend(shader, "blendColors", description.colorMode);
Romain Guyac670c02010-07-27 17:39:27 -0700678 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700679 if (blendFramebuffer) {
680 generateBlend(shader, "blendFramebuffer", description.framebufferMode);
681 }
Romain Guy889f8d12010-07-29 14:37:42 -0700682 if (description.isBitmapNpot) {
683 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
684 }
Romain Guyac670c02010-07-27 17:39:27 -0700685
686 // Begin the shader
687 shader.append(gFS_Main); {
688 // Stores the result in fragColor directly
Romain Guyaa6c24c2011-04-28 18:40:04 -0700689 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700690 if (description.hasAlpha8Texture) {
Romain Guy707b2f72010-10-11 16:34:59 -0700691 if (!description.hasGradient && !description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700692 shader.append(gFS_Main_FetchA8Texture[modulateOp * 2 +
693 description.hasGammaCorrection]);
Romain Guy707b2f72010-10-11 16:34:59 -0700694 }
Romain Guyac670c02010-07-27 17:39:27 -0700695 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700696 shader.append(gFS_Main_FetchTexture[modulateOp]);
Romain Guyac670c02010-07-27 17:39:27 -0700697 }
698 } else {
Romain Guya938f562012-09-13 20:31:08 -0700699 if (!description.hasGradient && !description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700700 shader.append(gFS_Main_FetchColor);
701 }
Romain Guyac670c02010-07-27 17:39:27 -0700702 }
703 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700704 shader.append(gFS_Main_FetchGradient[gradientIndex(description)]);
Romain Guy211efea2012-07-31 21:16:07 -0700705 shader.append(gFS_Main_AddDitherToGradient);
Romain Guyac670c02010-07-27 17:39:27 -0700706 }
707 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700708 if (description.isPoint) {
709 shader.append(gFS_Main_PointBitmapTexCoords);
710 }
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 Craik710f46d2012-09-17 17:25:49 -0700744 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800745 shader.append(gFS_Main_AccountForAAVertexShape);
Chris Craik9f44a132012-09-13 18:34:55 -0700746 }
747
Romain Guyac670c02010-07-27 17:39:27 -0700748 // Output the fragment
Romain Guya5aed0d2010-09-09 14:42:43 -0700749 if (!blendFramebuffer) {
750 shader.append(gFS_Main_FragColor);
751 } else {
Romain Guyf607bdc2010-09-10 19:20:06 -0700752 shader.append(!description.swapSrcDst ?
753 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap);
Romain Guya5aed0d2010-09-09 14:42:43 -0700754 }
Romain Guyff316ec2013-02-13 18:39:43 -0800755 if (description.hasColors) {
756 shader.append(gFS_Main_FragColor_HasColors);
757 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800758 if (description.hasDebugHighlight) {
759 shader.append(gFS_Main_DebugHighlight);
760 }
Romain Guyac670c02010-07-27 17:39:27 -0700761 }
762 // End the shader
763 shader.append(gFS_Footer);
764
Romain Guyc15008e2010-11-10 11:59:15 -0800765#if DEBUG_PROGRAMS
Romain Guydb1938e2010-08-02 18:50:22 -0700766 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
767 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800768#endif
Romain Guydb1938e2010-08-02 18:50:22 -0700769
Romain Guyac670c02010-07-27 17:39:27 -0700770 return shader;
771}
772
Romain Guy48daa542010-08-10 19:21:34 -0700773void ProgramCache::generateBlend(String8& shader, const char* name, SkXfermode::Mode mode) {
Romain Guyac670c02010-07-27 17:39:27 -0700774 shader.append("\nvec4 ");
775 shader.append(name);
776 shader.append("(vec4 src, vec4 dst) {\n");
777 shader.append(" ");
Romain Guy48daa542010-08-10 19:21:34 -0700778 shader.append(gBlendOps[mode]);
Romain Guyac670c02010-07-27 17:39:27 -0700779 shader.append("}\n");
780}
781
Romain Guy889f8d12010-07-29 14:37:42 -0700782void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) {
Romain Guy63553472012-07-18 20:04:14 -0700783 shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700784 if (wrapS == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700785 shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700786 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n");
787 }
788 if (wrapT == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700789 shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700790 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n");
791 }
792 shader.append(" return vec2(");
793 switch (wrapS) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700794 case GL_CLAMP_TO_EDGE:
795 shader.append("texCoords.x");
796 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700797 case GL_REPEAT:
798 shader.append("mod(texCoords.x, 1.0)");
799 break;
800 case GL_MIRRORED_REPEAT:
801 shader.append("xMod2");
802 break;
803 }
804 shader.append(", ");
805 switch (wrapT) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700806 case GL_CLAMP_TO_EDGE:
807 shader.append("texCoords.y");
808 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700809 case GL_REPEAT:
810 shader.append("mod(texCoords.y, 1.0)");
811 break;
812 case GL_MIRRORED_REPEAT:
813 shader.append("yMod2");
814 break;
815 }
816 shader.append(");\n");
817 shader.append("}\n");
818}
819
Romain Guydb1938e2010-08-02 18:50:22 -0700820void ProgramCache::printLongString(const String8& shader) const {
821 ssize_t index = 0;
822 ssize_t lastIndex = 0;
823 const char* str = shader.string();
824 while ((index = shader.find("\n", index)) > -1) {
825 String8 line(str, index - lastIndex);
826 if (line.length() == 0) line.append("\n");
827 PROGRAM_LOGD("%s", line.string());
828 index++;
829 str += (index - lastIndex);
830 lastIndex = index;
831 }
832}
833
Romain Guyac670c02010-07-27 17:39:27 -0700834}; // namespace uirenderer
835}; // namespace android