blob: fb00335ebd499ac1292520167da67031c3ff6eaa [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";
Chris Craik710f46d2012-09-17 17:25:49 -070043const char* gVS_Header_Attributes_AAVertexShapeParameters =
Chris Craik6ebdc112012-08-31 18:24:33 -070044 "attribute float vtxAlpha;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -070045const char* gVS_Header_Uniforms_TextureTransform =
46 "uniform mat4 mainTextureTransform;\n";
Romain Guyac670c02010-07-27 17:39:27 -070047const char* gVS_Header_Uniforms =
Romain Guy39284b72012-09-26 16:39:40 -070048 "uniform mat4 projection;\n" \
Romain Guyac670c02010-07-27 17:39:27 -070049 "uniform mat4 transform;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -070050const char* gVS_Header_Uniforms_IsPoint =
Romain Guy80bbfb12011-03-23 16:56:28 -070051 "uniform mediump float pointSize;\n";
Romain Guyee916f12010-09-20 17:53:08 -070052const char* gVS_Header_Uniforms_HasGradient[3] = {
53 // Linear
Chet Haasea1d12dd2012-09-21 14:50:14 -070054 "uniform mat4 screenSpace;\n"
55 "uniform float ditherSize;\n",
Romain Guyee916f12010-09-20 17:53:08 -070056 // Circular
Chet Haasea1d12dd2012-09-21 14:50:14 -070057 "uniform mat4 screenSpace;\n"
58 "uniform float ditherSize;\n",
Romain Guyee916f12010-09-20 17:53:08 -070059 // Sweep
Romain Guyee916f12010-09-20 17:53:08 -070060 "uniform mat4 screenSpace;\n"
Chet Haasea1d12dd2012-09-21 14:50:14 -070061 "uniform float ditherSize;\n"
Romain Guyee916f12010-09-20 17:53:08 -070062};
Romain Guy889f8d12010-07-29 14:37:42 -070063const char* gVS_Header_Uniforms_HasBitmap =
64 "uniform mat4 textureTransform;\n"
Romain Guy80bbfb12011-03-23 16:56:28 -070065 "uniform mediump vec2 textureDimension;\n";
Romain Guyac670c02010-07-27 17:39:27 -070066const char* gVS_Header_Varyings_HasTexture =
67 "varying vec2 outTexCoords;\n";
Chris Craik710f46d2012-09-17 17:25:49 -070068const char* gVS_Header_Varyings_IsAAVertexShape =
Chris Craik6ebdc112012-08-31 18:24:33 -070069 "varying float alpha;\n";
Romain Guy63553472012-07-18 20:04:14 -070070const char* gVS_Header_Varyings_HasBitmap =
71 "varying highp vec2 outBitmapTexCoords;\n";
72const char* gVS_Header_Varyings_PointHasBitmap =
73 "varying highp vec2 outPointBitmapTexCoords;\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -070074const char* gVS_Header_Varyings_HasGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -070075 // Linear
Chet Haasea1d12dd2012-09-21 14:50:14 -070076 "varying highp vec2 linear;\n"
77 "varying vec2 ditherTexCoords;\n",
78 "varying float linear;\n"
79 "varying vec2 ditherTexCoords;\n",
Romain Guy211efea2012-07-31 21:16:07 -070080
Romain Guyee916f12010-09-20 17:53:08 -070081 // Circular
Chet Haasea1d12dd2012-09-21 14:50:14 -070082 "varying highp vec2 circular;\n"
83 "varying vec2 ditherTexCoords;\n",
84 "varying highp vec2 circular;\n"
85 "varying vec2 ditherTexCoords;\n",
Romain Guy211efea2012-07-31 21:16:07 -070086
Romain Guyee916f12010-09-20 17:53:08 -070087 // Sweep
Chet Haasea1d12dd2012-09-21 14:50:14 -070088 "varying highp vec2 sweep;\n"
89 "varying vec2 ditherTexCoords;\n",
90 "varying highp vec2 sweep;\n"
91 "varying vec2 ditherTexCoords;\n",
Romain Guyee916f12010-09-20 17:53:08 -070092};
Romain Guyac670c02010-07-27 17:39:27 -070093const char* gVS_Main =
94 "\nvoid main(void) {\n";
95const char* gVS_Main_OutTexCoords =
96 " outTexCoords = texCoords;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -070097const char* gVS_Main_OutTransformedTexCoords =
98 " outTexCoords = (mainTextureTransform * vec4(texCoords, 0.0, 1.0)).xy;\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -070099const char* gVS_Main_OutGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700100 // Linear
Chet Haasea1d12dd2012-09-21 14:50:14 -0700101 " linear = vec2((screenSpace * position).x, 0.5);\n"
Romain Guy39284b72012-09-26 16:39:40 -0700102 " ditherTexCoords = (transform * position).xy * ditherSize;\n",
Chet Haasea1d12dd2012-09-21 14:50:14 -0700103 " linear = (screenSpace * position).x;\n"
Romain Guy39284b72012-09-26 16:39:40 -0700104 " ditherTexCoords = (transform * position).xy * ditherSize;\n",
Romain Guy211efea2012-07-31 21:16:07 -0700105
Romain Guyee916f12010-09-20 17:53:08 -0700106 // Circular
Chet Haasea1d12dd2012-09-21 14:50:14 -0700107 " circular = (screenSpace * position).xy;\n"
Romain Guy39284b72012-09-26 16:39:40 -0700108 " ditherTexCoords = (transform * position).xy * ditherSize;\n",
Chet Haasea1d12dd2012-09-21 14:50:14 -0700109 " circular = (screenSpace * position).xy;\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 // Sweep
Chet Haasea1d12dd2012-09-21 14:50:14 -0700113 " sweep = (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 " sweep = (screenSpace * position).xy;\n"
Romain Guy39284b72012-09-26 16:39:40 -0700116 " ditherTexCoords = (transform * position).xy * ditherSize;\n",
Romain Guyee916f12010-09-20 17:53:08 -0700117};
Romain Guy889f8d12010-07-29 14:37:42 -0700118const char* gVS_Main_OutBitmapTexCoords =
Romain Guy707b2f72010-10-11 16:34:59 -0700119 " outBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -0700120const char* gVS_Main_OutPointBitmapTexCoords =
121 " outPointBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700122const char* gVS_Main_Position =
Romain Guy39284b72012-09-26 16:39:40 -0700123 " gl_Position = projection * transform * position;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -0700124const char* gVS_Main_PointSize =
125 " gl_PointSize = pointSize;\n";
Chris Craik710f46d2012-09-17 17:25:49 -0700126const char* gVS_Main_AAVertexShape =
Chris Craik6ebdc112012-08-31 18:24:33 -0700127 " alpha = vtxAlpha;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700128const char* gVS_Footer =
129 "}\n\n";
130
131///////////////////////////////////////////////////////////////////////////////
132// Fragment shaders snippets
133///////////////////////////////////////////////////////////////////////////////
134
Romain Guya5aed0d2010-09-09 14:42:43 -0700135const char* gFS_Header_Extension_FramebufferFetch =
136 "#extension GL_NV_shader_framebuffer_fetch : enable\n\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700137const char* gFS_Header_Extension_ExternalTexture =
138 "#extension GL_OES_EGL_image_external : require\n\n";
Romain Guyac670c02010-07-27 17:39:27 -0700139const char* gFS_Header =
140 "precision mediump float;\n\n";
141const char* gFS_Uniforms_Color =
142 "uniform vec4 color;\n";
Romain Guyed6fcb02011-03-21 13:11:28 -0700143const char* gFS_Header_Uniforms_PointHasBitmap =
144 "uniform vec2 textureDimension;\n"
145 "uniform float pointSize;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700146const char* gFS_Uniforms_TextureSampler =
Romain Guya938f562012-09-13 20:31:08 -0700147 "uniform sampler2D baseSampler;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700148const char* gFS_Uniforms_ExternalTextureSampler =
Romain Guya938f562012-09-13 20:31:08 -0700149 "uniform samplerExternalOES baseSampler;\n";
Romain Guy211efea2012-07-31 21:16:07 -0700150#define FS_UNIFORMS_DITHER \
Chet Haasea1d12dd2012-09-21 14:50:14 -0700151 "uniform float ditherSizeSquared;\n" \
Romain Guy211efea2012-07-31 21:16:07 -0700152 "uniform sampler2D ditherSampler;\n"
153#define FS_UNIFORMS_GRADIENT \
154 "uniform vec4 startColor;\n" \
155 "uniform vec4 endColor;\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700156const char* gFS_Uniforms_GradientSampler[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700157 // Linear
Romain Guy211efea2012-07-31 21:16:07 -0700158 FS_UNIFORMS_DITHER "uniform sampler2D gradientSampler;\n",
159 FS_UNIFORMS_DITHER FS_UNIFORMS_GRADIENT,
160
Romain Guyee916f12010-09-20 17:53:08 -0700161 // Circular
Romain Guy211efea2012-07-31 21:16:07 -0700162 FS_UNIFORMS_DITHER "uniform sampler2D gradientSampler;\n",
163 FS_UNIFORMS_DITHER FS_UNIFORMS_GRADIENT,
164
Romain Guyee916f12010-09-20 17:53:08 -0700165 // Sweep
Romain Guy211efea2012-07-31 21:16:07 -0700166 FS_UNIFORMS_DITHER "uniform sampler2D gradientSampler;\n",
167 FS_UNIFORMS_DITHER FS_UNIFORMS_GRADIENT
Romain Guyee916f12010-09-20 17:53:08 -0700168};
Romain Guyac670c02010-07-27 17:39:27 -0700169const char* gFS_Uniforms_BitmapSampler =
170 "uniform sampler2D bitmapSampler;\n";
171const char* gFS_Uniforms_ColorOp[4] = {
172 // None
173 "",
174 // Matrix
175 "uniform mat4 colorMatrix;\n"
176 "uniform vec4 colorMatrixVector;\n",
177 // Lighting
Romain Guydb1938e2010-08-02 18:50:22 -0700178 "uniform vec4 lightingMul;\n"
179 "uniform vec4 lightingAdd;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700180 // PorterDuff
Romain Guydb1938e2010-08-02 18:50:22 -0700181 "uniform vec4 colorBlend;\n"
Romain Guyac670c02010-07-27 17:39:27 -0700182};
Romain Guy41210632012-07-16 17:04:24 -0700183const char* gFS_Uniforms_Gamma =
184 "uniform float gamma;\n";
185
Romain Guyac670c02010-07-27 17:39:27 -0700186const char* gFS_Main =
187 "\nvoid main(void) {\n"
Romain Guy7fbcc042010-08-04 15:40:07 -0700188 " lowp vec4 fragColor;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700189
Romain Guyed6fcb02011-03-21 13:11:28 -0700190const char* gFS_Main_PointBitmapTexCoords =
Romain Guy63553472012-07-18 20:04:14 -0700191 " highp vec2 outBitmapTexCoords = outPointBitmapTexCoords + "
Romain Guyed6fcb02011-03-21 13:11:28 -0700192 "((gl_PointCoord - vec2(0.5, 0.5)) * textureDimension * vec2(pointSize, pointSize));\n";
193
Romain Guy211efea2012-07-31 21:16:07 -0700194#define FS_MAIN_DITHER \
Chet Haasea1d12dd2012-09-21 14:50:14 -0700195 "texture2D(ditherSampler, ditherTexCoords).a * ditherSizeSquared"
Romain Guy211efea2012-07-31 21:16:07 -0700196const char* gFS_Main_AddDitherToGradient =
197 " gradientColor += " FS_MAIN_DITHER ";\n";
198
Romain Guy707b2f72010-10-11 16:34:59 -0700199// Fast cases
200const char* gFS_Fast_SingleColor =
201 "\nvoid main(void) {\n"
202 " gl_FragColor = color;\n"
203 "}\n\n";
204const char* gFS_Fast_SingleTexture =
205 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700206 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700207 "}\n\n";
208const char* gFS_Fast_SingleModulateTexture =
209 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700210 " gl_FragColor = color.a * texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700211 "}\n\n";
212const char* gFS_Fast_SingleA8Texture =
213 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700214 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700215 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700216const char* gFS_Fast_SingleA8Texture_ApplyGamma =
217 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700218 " gl_FragColor = vec4(0.0, 0.0, 0.0, pow(texture2D(baseSampler, outTexCoords).a, gamma));\n"
Romain Guy41210632012-07-16 17:04:24 -0700219 "}\n\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700220const char* gFS_Fast_SingleModulateA8Texture =
221 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700222 " gl_FragColor = color * texture2D(baseSampler, outTexCoords).a;\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700223 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700224const char* gFS_Fast_SingleModulateA8Texture_ApplyGamma =
225 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700226 " gl_FragColor = color * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy41210632012-07-16 17:04:24 -0700227 "}\n\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -0700228const char* gFS_Fast_SingleGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700229 "\nvoid main(void) {\n"
Romain Guy211efea2012-07-31 21:16:07 -0700230 " gl_FragColor = " FS_MAIN_DITHER " + texture2D(gradientSampler, linear);\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700231 "}\n\n",
232 "\nvoid main(void) {\n"
Romain Guy211efea2012-07-31 21:16:07 -0700233 " gl_FragColor = " FS_MAIN_DITHER " + mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700234 "}\n\n"
235};
236const char* gFS_Fast_SingleModulateGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700237 "\nvoid main(void) {\n"
Chet Haase1c5c2062012-09-17 17:09:21 -0700238 " gl_FragColor = " FS_MAIN_DITHER " + color.a * texture2D(gradientSampler, linear);\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700239 "}\n\n",
240 "\nvoid main(void) {\n"
Chet Haase1c5c2062012-09-17 17:09:21 -0700241 " gl_FragColor = " FS_MAIN_DITHER " + color.a * mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700242 "}\n\n"
243};
Romain Guy707b2f72010-10-11 16:34:59 -0700244
245// General case
Romain Guyac670c02010-07-27 17:39:27 -0700246const char* gFS_Main_FetchColor =
247 " fragColor = color;\n";
Romain Guy740bf2b2011-04-26 15:33:10 -0700248const char* gFS_Main_ModulateColor =
249 " fragColor *= color.a;\n";
Chris Craik710f46d2012-09-17 17:25:49 -0700250const char* gFS_Main_AccountForAAVertexShape =
Chris Craik6ebdc112012-08-31 18:24:33 -0700251 " fragColor *= alpha;\n";
Chris Craika798b952012-08-27 17:03:13 -0700252
Romain Guy707b2f72010-10-11 16:34:59 -0700253const char* gFS_Main_FetchTexture[2] = {
254 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700255 " 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);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700258};
Romain Guya938f562012-09-13 20:31:08 -0700259const char* gFS_Main_FetchA8Texture[4] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700260 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700261 " fragColor = texture2D(baseSampler, outTexCoords);\n",
262 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700263 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700264 " fragColor = color * texture2D(baseSampler, outTexCoords).a;\n",
265 " fragColor = color * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700266};
Romain Guy42e1e0d2012-07-30 14:47:51 -0700267const char* gFS_Main_FetchGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700268 // Linear
Romain Guy320d46b2012-08-08 16:05:42 -0700269 " vec4 gradientColor = texture2D(gradientSampler, linear);\n",
Romain Guy211efea2012-07-31 21:16:07 -0700270
Romain Guy320d46b2012-08-08 16:05:42 -0700271 " vec4 gradientColor = mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700272
Romain Guyee916f12010-09-20 17:53:08 -0700273 // Circular
Romain Guy320d46b2012-08-08 16:05:42 -0700274 " vec4 gradientColor = texture2D(gradientSampler, vec2(length(circular), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700275
Romain Guy320d46b2012-08-08 16:05:42 -0700276 " vec4 gradientColor = mix(startColor, endColor, clamp(length(circular), 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700277
Romain Guyee916f12010-09-20 17:53:08 -0700278 // Sweep
Romain Guy63553472012-07-18 20:04:14 -0700279 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700280 " vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700281
Romain Guy42e1e0d2012-07-30 14:47:51 -0700282 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700283 " vec4 gradientColor = mix(startColor, endColor, clamp(index - floor(index), 0.0, 1.0));\n"
Romain Guyee916f12010-09-20 17:53:08 -0700284};
Romain Guyac670c02010-07-27 17:39:27 -0700285const char* gFS_Main_FetchBitmap =
286 " vec4 bitmapColor = texture2D(bitmapSampler, outBitmapTexCoords);\n";
Romain Guy889f8d12010-07-29 14:37:42 -0700287const char* gFS_Main_FetchBitmapNpot =
288 " vec4 bitmapColor = texture2D(bitmapSampler, wrap(outBitmapTexCoords));\n";
Romain Guyac670c02010-07-27 17:39:27 -0700289const char* gFS_Main_BlendShadersBG =
Romain Guyac670c02010-07-27 17:39:27 -0700290 " fragColor = blendShaders(gradientColor, bitmapColor)";
Romain Guy06f96e22010-07-30 19:18:16 -0700291const char* gFS_Main_BlendShadersGB =
292 " fragColor = blendShaders(bitmapColor, gradientColor)";
Romain Guya938f562012-09-13 20:31:08 -0700293const char* gFS_Main_BlendShaders_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700294 // Don't modulate
295 ";\n",
Romain Guya938f562012-09-13 20:31:08 -0700296 ";\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700297 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700298 " * color.a;\n",
299 " * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700300 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700301 " * texture2D(baseSampler, outTexCoords).a;\n",
302 " * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700303};
Romain Guya938f562012-09-13 20:31:08 -0700304const char* gFS_Main_GradientShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700305 // Don't modulate
306 " fragColor = gradientColor;\n",
Romain Guya938f562012-09-13 20:31:08 -0700307 " fragColor = gradientColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700308 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700309 " fragColor = gradientColor * color.a;\n",
310 " fragColor = gradientColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700311 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700312 " fragColor = gradientColor * texture2D(baseSampler, outTexCoords).a;\n",
313 " fragColor = gradientColor * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700314 };
Romain Guya938f562012-09-13 20:31:08 -0700315const char* gFS_Main_BitmapShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700316 // Don't modulate
317 " fragColor = bitmapColor;\n",
Romain Guya938f562012-09-13 20:31:08 -0700318 " fragColor = bitmapColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700319 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700320 " fragColor = bitmapColor * color.a;\n",
321 " fragColor = bitmapColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700322 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700323 " fragColor = bitmapColor * texture2D(baseSampler, outTexCoords).a;\n",
324 " fragColor = bitmapColor * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700325 };
Romain Guyac670c02010-07-27 17:39:27 -0700326const char* gFS_Main_FragColor =
327 " gl_FragColor = fragColor;\n";
Romain Guya5aed0d2010-09-09 14:42:43 -0700328const char* gFS_Main_FragColor_Blend =
329 " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n";
Romain Guyf607bdc2010-09-10 19:20:06 -0700330const char* gFS_Main_FragColor_Blend_Swap =
331 " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n";
Romain Guyac670c02010-07-27 17:39:27 -0700332const char* gFS_Main_ApplyColorOp[4] = {
333 // None
334 "",
335 // Matrix
336 " fragColor *= colorMatrix;\n"
Romain Guydb1938e2010-08-02 18:50:22 -0700337 " fragColor += colorMatrixVector;\n"
338 " fragColor.rgb *= fragColor.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700339 // Lighting
Romain Guydb1938e2010-08-02 18:50:22 -0700340 " float lightingAlpha = fragColor.a;\n"
341 " fragColor = min(fragColor * lightingMul + (lightingAdd * lightingAlpha), lightingAlpha);\n"
342 " fragColor.a = lightingAlpha;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700343 // PorterDuff
344 " fragColor = blendColors(colorBlend, fragColor);\n"
345};
346const char* gFS_Footer =
347 "}\n\n";
348
349///////////////////////////////////////////////////////////////////////////////
350// PorterDuff snippets
351///////////////////////////////////////////////////////////////////////////////
352
Romain Guy48daa542010-08-10 19:21:34 -0700353const char* gBlendOps[18] = {
Romain Guyac670c02010-07-27 17:39:27 -0700354 // Clear
355 "return vec4(0.0, 0.0, 0.0, 0.0);\n",
356 // Src
357 "return src;\n",
358 // Dst
359 "return dst;\n",
360 // SrcOver
Romain Guy06f96e22010-07-30 19:18:16 -0700361 "return src + dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700362 // DstOver
Romain Guy06f96e22010-07-30 19:18:16 -0700363 "return dst + src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700364 // SrcIn
Romain Guy06f96e22010-07-30 19:18:16 -0700365 "return src * dst.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700366 // DstIn
Romain Guy06f96e22010-07-30 19:18:16 -0700367 "return dst * src.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700368 // SrcOut
Romain Guy06f96e22010-07-30 19:18:16 -0700369 "return src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700370 // DstOut
Romain Guy06f96e22010-07-30 19:18:16 -0700371 "return dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700372 // SrcAtop
373 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n",
374 // DstAtop
375 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n",
376 // Xor
Romain Guy48daa542010-08-10 19:21:34 -0700377 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, "
Romain Guyac670c02010-07-27 17:39:27 -0700378 "src.a + dst.a - 2.0 * src.a * dst.a);\n",
Romain Guy48daa542010-08-10 19:21:34 -0700379 // Add
380 "return min(src + dst, 1.0);\n",
381 // Multiply
382 "return src * dst;\n",
383 // Screen
384 "return src + dst - src * dst;\n",
385 // Overlay
386 "return clamp(vec4(mix("
387 "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), "
388 "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), "
389 "step(dst.a, 2.0 * dst.rgb)), "
390 "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n",
391 // Darken
392 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
393 "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
394 // Lighten
395 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
396 "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 -0700397};
398
399///////////////////////////////////////////////////////////////////////////////
400// Constructors/destructors
401///////////////////////////////////////////////////////////////////////////////
402
403ProgramCache::ProgramCache() {
404}
405
406ProgramCache::~ProgramCache() {
407 clear();
408}
409
410///////////////////////////////////////////////////////////////////////////////
411// Cache management
412///////////////////////////////////////////////////////////////////////////////
413
414void ProgramCache::clear() {
Romain Guy67f27952010-12-07 20:09:23 -0800415 PROGRAM_LOGD("Clearing program cache");
416
Romain Guyac670c02010-07-27 17:39:27 -0700417 size_t count = mCache.size();
418 for (size_t i = 0; i < count; i++) {
419 delete mCache.valueAt(i);
420 }
421 mCache.clear();
422}
423
424Program* ProgramCache::get(const ProgramDescription& description) {
425 programid key = description.key();
426 ssize_t index = mCache.indexOfKey(key);
427 Program* program = NULL;
428 if (index < 0) {
Romain Guyee916f12010-09-20 17:53:08 -0700429 description.log("Could not find program");
Romain Guyac670c02010-07-27 17:39:27 -0700430 program = generateProgram(description, key);
431 mCache.add(key, program);
432 } else {
433 program = mCache.valueAt(index);
434 }
435 return program;
436}
437
438///////////////////////////////////////////////////////////////////////////////
439// Program generation
440///////////////////////////////////////////////////////////////////////////////
441
442Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) {
443 String8 vertexShader = generateVertexShader(description);
444 String8 fragmentShader = generateFragmentShader(description);
445
Romain Guy42e1e0d2012-07-30 14:47:51 -0700446 return new Program(description, vertexShader.string(), fragmentShader.string());
447}
448
449static inline size_t gradientIndex(const ProgramDescription& description) {
450 return description.gradientType * 2 + description.isSimpleGradient;
Romain Guyac670c02010-07-27 17:39:27 -0700451}
452
453String8 ProgramCache::generateVertexShader(const ProgramDescription& description) {
454 // Add attributes
455 String8 shader(gVS_Header_Attributes);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700456 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700457 shader.append(gVS_Header_Attributes_TexCoords);
458 }
Chris Craik710f46d2012-09-17 17:25:49 -0700459 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800460 shader.append(gVS_Header_Attributes_AAVertexShapeParameters);
Chet Haase5b0200b2011-04-13 17:58:08 -0700461 }
Romain Guyac670c02010-07-27 17:39:27 -0700462 // Uniforms
463 shader.append(gVS_Header_Uniforms);
Romain Guy8f0095c2011-05-02 17:24:22 -0700464 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700465 shader.append(gVS_Header_Uniforms_TextureTransform);
466 }
Romain Guyac670c02010-07-27 17:39:27 -0700467 if (description.hasGradient) {
Romain Guyee916f12010-09-20 17:53:08 -0700468 shader.append(gVS_Header_Uniforms_HasGradient[description.gradientType]);
Romain Guyac670c02010-07-27 17:39:27 -0700469 }
Romain Guy889f8d12010-07-29 14:37:42 -0700470 if (description.hasBitmap) {
471 shader.append(gVS_Header_Uniforms_HasBitmap);
472 }
Romain Guyed6fcb02011-03-21 13:11:28 -0700473 if (description.isPoint) {
474 shader.append(gVS_Header_Uniforms_IsPoint);
475 }
Romain Guyac670c02010-07-27 17:39:27 -0700476 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700477 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700478 shader.append(gVS_Header_Varyings_HasTexture);
479 }
Chris Craik710f46d2012-09-17 17:25:49 -0700480 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800481 shader.append(gVS_Header_Varyings_IsAAVertexShape);
Chet Haase5b0200b2011-04-13 17:58:08 -0700482 }
Romain Guyac670c02010-07-27 17:39:27 -0700483 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700484 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700485 }
486 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700487 shader.append(description.isPoint ?
Romain Guy63553472012-07-18 20:04:14 -0700488 gVS_Header_Varyings_PointHasBitmap :
489 gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700490 }
491
492 // Begin the shader
493 shader.append(gVS_Main); {
Romain Guy8f0095c2011-05-02 17:24:22 -0700494 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700495 shader.append(gVS_Main_OutTransformedTexCoords);
Romain Guy8f0095c2011-05-02 17:24:22 -0700496 } else if (description.hasTexture || description.hasExternalTexture) {
497 shader.append(gVS_Main_OutTexCoords);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700498 }
Chris Craik710f46d2012-09-17 17:25:49 -0700499 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800500 shader.append(gVS_Main_AAVertexShape);
Chet Haase5b0200b2011-04-13 17:58:08 -0700501 }
Romain Guy889f8d12010-07-29 14:37:42 -0700502 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700503 shader.append(description.isPoint ?
504 gVS_Main_OutPointBitmapTexCoords :
505 gVS_Main_OutBitmapTexCoords);
506 }
507 if (description.isPoint) {
508 shader.append(gVS_Main_PointSize);
Romain Guy889f8d12010-07-29 14:37:42 -0700509 }
Romain Guyac670c02010-07-27 17:39:27 -0700510 // Output transformed position
511 shader.append(gVS_Main_Position);
Chet Haasea1d12dd2012-09-21 14:50:14 -0700512 if (description.hasGradient) {
513 shader.append(gVS_Main_OutGradient[gradientIndex(description)]);
514 }
Romain Guyac670c02010-07-27 17:39:27 -0700515 }
516 // End the shader
517 shader.append(gVS_Footer);
518
519 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string());
520
521 return shader;
522}
523
Romain Guya938f562012-09-13 20:31:08 -0700524static bool shaderOp(const ProgramDescription& description, String8& shader,
525 const int modulateOp, const char** snippets) {
526 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
527 op = op * 2 + description.hasGammaCorrection;
528 shader.append(snippets[op]);
529 return description.hasAlpha8Texture;
530}
531
Romain Guyac670c02010-07-27 17:39:27 -0700532String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) {
Romain Guya5aed0d2010-09-09 14:42:43 -0700533 String8 shader;
534
Romain Guy707b2f72010-10-11 16:34:59 -0700535 const bool blendFramebuffer = description.framebufferMode >= SkXfermode::kPlus_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700536 if (blendFramebuffer) {
537 shader.append(gFS_Header_Extension_FramebufferFetch);
538 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700539 if (description.hasExternalTexture) {
540 shader.append(gFS_Header_Extension_ExternalTexture);
541 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700542
543 shader.append(gFS_Header);
Romain Guyac670c02010-07-27 17:39:27 -0700544
545 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700546 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700547 shader.append(gVS_Header_Varyings_HasTexture);
548 }
Chris Craik710f46d2012-09-17 17:25:49 -0700549 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800550 shader.append(gVS_Header_Varyings_IsAAVertexShape);
Chet Haase5b0200b2011-04-13 17:58:08 -0700551 }
Romain Guyac670c02010-07-27 17:39:27 -0700552 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700553 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700554 }
555 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700556 shader.append(description.isPoint ?
Romain Guy63553472012-07-18 20:04:14 -0700557 gVS_Header_Varyings_PointHasBitmap :
558 gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700559 }
560
Romain Guyac670c02010-07-27 17:39:27 -0700561 // Uniforms
Romain Guy707b2f72010-10-11 16:34:59 -0700562 int modulateOp = MODULATE_OP_NO_MODULATE;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700563 const bool singleColor = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700564 !description.hasGradient && !description.hasBitmap;
565
566 if (description.modulate || singleColor) {
567 shader.append(gFS_Uniforms_Color);
568 if (!singleColor) modulateOp = MODULATE_OP_MODULATE;
569 }
Romain Guyac670c02010-07-27 17:39:27 -0700570 if (description.hasTexture) {
571 shader.append(gFS_Uniforms_TextureSampler);
Romain Guy8f0095c2011-05-02 17:24:22 -0700572 } else if (description.hasExternalTexture) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700573 shader.append(gFS_Uniforms_ExternalTextureSampler);
574 }
Romain Guyac670c02010-07-27 17:39:27 -0700575 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700576 shader.append(gFS_Uniforms_GradientSampler[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700577 }
Romain Guyed6fcb02011-03-21 13:11:28 -0700578 if (description.hasBitmap && description.isPoint) {
579 shader.append(gFS_Header_Uniforms_PointHasBitmap);
580 }
Romain Guy41210632012-07-16 17:04:24 -0700581 if (description.hasGammaCorrection) {
582 shader.append(gFS_Uniforms_Gamma);
583 }
Romain Guy707b2f72010-10-11 16:34:59 -0700584
585 // Optimization for common cases
Chet Haase99585ad2011-05-02 15:00:16 -0700586 if (!description.isAA && !blendFramebuffer &&
Chris Craik65cd6122012-12-10 17:56:27 -0800587 description.colorOp == ProgramDescription::kColorNone && !description.isPoint) {
Romain Guy707b2f72010-10-11 16:34:59 -0700588 bool fast = false;
589
590 const bool noShader = !description.hasGradient && !description.hasBitmap;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700591 const bool singleTexture = (description.hasTexture || description.hasExternalTexture) &&
Romain Guy707b2f72010-10-11 16:34:59 -0700592 !description.hasAlpha8Texture && noShader;
593 const bool singleA8Texture = description.hasTexture &&
594 description.hasAlpha8Texture && noShader;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700595 const bool singleGradient = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700596 description.hasGradient && !description.hasBitmap &&
597 description.gradientType == ProgramDescription::kGradientLinear;
598
599 if (singleColor) {
600 shader.append(gFS_Fast_SingleColor);
601 fast = true;
602 } else if (singleTexture) {
603 if (!description.modulate) {
604 shader.append(gFS_Fast_SingleTexture);
605 } else {
606 shader.append(gFS_Fast_SingleModulateTexture);
607 }
608 fast = true;
609 } else if (singleA8Texture) {
610 if (!description.modulate) {
Romain Guy41210632012-07-16 17:04:24 -0700611 if (description.hasGammaCorrection) {
612 shader.append(gFS_Fast_SingleA8Texture_ApplyGamma);
613 } else {
614 shader.append(gFS_Fast_SingleA8Texture);
615 }
Romain Guy707b2f72010-10-11 16:34:59 -0700616 } else {
Romain Guy41210632012-07-16 17:04:24 -0700617 if (description.hasGammaCorrection) {
618 shader.append(gFS_Fast_SingleModulateA8Texture_ApplyGamma);
619 } else {
620 shader.append(gFS_Fast_SingleModulateA8Texture);
621 }
Romain Guy707b2f72010-10-11 16:34:59 -0700622 }
623 fast = true;
624 } else if (singleGradient) {
625 if (!description.modulate) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700626 shader.append(gFS_Fast_SingleGradient[description.isSimpleGradient]);
Romain Guy707b2f72010-10-11 16:34:59 -0700627 } else {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700628 shader.append(gFS_Fast_SingleModulateGradient[description.isSimpleGradient]);
Romain Guy707b2f72010-10-11 16:34:59 -0700629 }
630 fast = true;
631 }
632
633 if (fast) {
Romain Guyc15008e2010-11-10 11:59:15 -0800634#if DEBUG_PROGRAMS
Romain Guy707b2f72010-10-11 16:34:59 -0700635 PROGRAM_LOGD("*** Fast case:\n");
636 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
637 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800638#endif
Romain Guy707b2f72010-10-11 16:34:59 -0700639
640 return shader;
641 }
642 }
643
Romain Guyac670c02010-07-27 17:39:27 -0700644 if (description.hasBitmap) {
645 shader.append(gFS_Uniforms_BitmapSampler);
646 }
647 shader.append(gFS_Uniforms_ColorOp[description.colorOp]);
648
649 // Generate required functions
650 if (description.hasGradient && description.hasBitmap) {
Romain Guy48daa542010-08-10 19:21:34 -0700651 generateBlend(shader, "blendShaders", description.shadersMode);
Romain Guyac670c02010-07-27 17:39:27 -0700652 }
653 if (description.colorOp == ProgramDescription::kColorBlend) {
Romain Guy48daa542010-08-10 19:21:34 -0700654 generateBlend(shader, "blendColors", description.colorMode);
Romain Guyac670c02010-07-27 17:39:27 -0700655 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700656 if (blendFramebuffer) {
657 generateBlend(shader, "blendFramebuffer", description.framebufferMode);
658 }
Romain Guy889f8d12010-07-29 14:37:42 -0700659 if (description.isBitmapNpot) {
660 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
661 }
Romain Guyac670c02010-07-27 17:39:27 -0700662
663 // Begin the shader
664 shader.append(gFS_Main); {
665 // Stores the result in fragColor directly
Romain Guyaa6c24c2011-04-28 18:40:04 -0700666 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700667 if (description.hasAlpha8Texture) {
Romain Guy707b2f72010-10-11 16:34:59 -0700668 if (!description.hasGradient && !description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700669 shader.append(gFS_Main_FetchA8Texture[modulateOp * 2 +
670 description.hasGammaCorrection]);
Romain Guy707b2f72010-10-11 16:34:59 -0700671 }
Romain Guyac670c02010-07-27 17:39:27 -0700672 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700673 shader.append(gFS_Main_FetchTexture[modulateOp]);
Romain Guyac670c02010-07-27 17:39:27 -0700674 }
675 } else {
Romain Guya938f562012-09-13 20:31:08 -0700676 if (!description.hasGradient && !description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700677 shader.append(gFS_Main_FetchColor);
678 }
Romain Guyac670c02010-07-27 17:39:27 -0700679 }
680 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700681 shader.append(gFS_Main_FetchGradient[gradientIndex(description)]);
Romain Guy211efea2012-07-31 21:16:07 -0700682 shader.append(gFS_Main_AddDitherToGradient);
Romain Guyac670c02010-07-27 17:39:27 -0700683 }
684 if (description.hasBitmap) {
Romain Guyed6fcb02011-03-21 13:11:28 -0700685 if (description.isPoint) {
686 shader.append(gFS_Main_PointBitmapTexCoords);
687 }
Romain Guy889f8d12010-07-29 14:37:42 -0700688 if (!description.isBitmapNpot) {
689 shader.append(gFS_Main_FetchBitmap);
690 } else {
691 shader.append(gFS_Main_FetchBitmapNpot);
692 }
Romain Guyac670c02010-07-27 17:39:27 -0700693 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700694 bool applyModulate = false;
Romain Guyac670c02010-07-27 17:39:27 -0700695 // Case when we have two shaders set
696 if (description.hasGradient && description.hasBitmap) {
697 if (description.isBitmapFirst) {
698 shader.append(gFS_Main_BlendShadersBG);
699 } else {
700 shader.append(gFS_Main_BlendShadersGB);
701 }
Romain Guya938f562012-09-13 20:31:08 -0700702 applyModulate = shaderOp(description, shader, modulateOp,
703 gFS_Main_BlendShaders_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700704 } else {
705 if (description.hasGradient) {
Romain Guya938f562012-09-13 20:31:08 -0700706 applyModulate = shaderOp(description, shader, modulateOp,
707 gFS_Main_GradientShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700708 } else if (description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700709 applyModulate = shaderOp(description, shader, modulateOp,
710 gFS_Main_BitmapShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700711 }
Romain Guyac670c02010-07-27 17:39:27 -0700712 }
Romain Guya938f562012-09-13 20:31:08 -0700713
Romain Guy740bf2b2011-04-26 15:33:10 -0700714 if (description.modulate && applyModulate) {
Romain Guya938f562012-09-13 20:31:08 -0700715 shader.append(gFS_Main_ModulateColor);
Romain Guy740bf2b2011-04-26 15:33:10 -0700716 }
Romain Guya938f562012-09-13 20:31:08 -0700717
Romain Guyac670c02010-07-27 17:39:27 -0700718 // Apply the color op if needed
719 shader.append(gFS_Main_ApplyColorOp[description.colorOp]);
Chris Craik9f44a132012-09-13 18:34:55 -0700720
Chris Craik710f46d2012-09-17 17:25:49 -0700721 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800722 shader.append(gFS_Main_AccountForAAVertexShape);
Chris Craik9f44a132012-09-13 18:34:55 -0700723 }
724
Romain Guyac670c02010-07-27 17:39:27 -0700725 // Output the fragment
Romain Guya5aed0d2010-09-09 14:42:43 -0700726 if (!blendFramebuffer) {
727 shader.append(gFS_Main_FragColor);
728 } else {
Romain Guyf607bdc2010-09-10 19:20:06 -0700729 shader.append(!description.swapSrcDst ?
730 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap);
Romain Guya5aed0d2010-09-09 14:42:43 -0700731 }
Romain Guyac670c02010-07-27 17:39:27 -0700732 }
733 // End the shader
734 shader.append(gFS_Footer);
735
Romain Guyc15008e2010-11-10 11:59:15 -0800736#if DEBUG_PROGRAMS
Romain Guydb1938e2010-08-02 18:50:22 -0700737 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
738 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800739#endif
Romain Guydb1938e2010-08-02 18:50:22 -0700740
Romain Guyac670c02010-07-27 17:39:27 -0700741 return shader;
742}
743
Romain Guy48daa542010-08-10 19:21:34 -0700744void ProgramCache::generateBlend(String8& shader, const char* name, SkXfermode::Mode mode) {
Romain Guyac670c02010-07-27 17:39:27 -0700745 shader.append("\nvec4 ");
746 shader.append(name);
747 shader.append("(vec4 src, vec4 dst) {\n");
748 shader.append(" ");
Romain Guy48daa542010-08-10 19:21:34 -0700749 shader.append(gBlendOps[mode]);
Romain Guyac670c02010-07-27 17:39:27 -0700750 shader.append("}\n");
751}
752
Romain Guy889f8d12010-07-29 14:37:42 -0700753void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) {
Romain Guy63553472012-07-18 20:04:14 -0700754 shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700755 if (wrapS == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700756 shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700757 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n");
758 }
759 if (wrapT == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700760 shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700761 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n");
762 }
763 shader.append(" return vec2(");
764 switch (wrapS) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700765 case GL_CLAMP_TO_EDGE:
766 shader.append("texCoords.x");
767 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700768 case GL_REPEAT:
769 shader.append("mod(texCoords.x, 1.0)");
770 break;
771 case GL_MIRRORED_REPEAT:
772 shader.append("xMod2");
773 break;
774 }
775 shader.append(", ");
776 switch (wrapT) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700777 case GL_CLAMP_TO_EDGE:
778 shader.append("texCoords.y");
779 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700780 case GL_REPEAT:
781 shader.append("mod(texCoords.y, 1.0)");
782 break;
783 case GL_MIRRORED_REPEAT:
784 shader.append("yMod2");
785 break;
786 }
787 shader.append(");\n");
788 shader.append("}\n");
789}
790
Romain Guydb1938e2010-08-02 18:50:22 -0700791void ProgramCache::printLongString(const String8& shader) const {
792 ssize_t index = 0;
793 ssize_t lastIndex = 0;
794 const char* str = shader.string();
795 while ((index = shader.find("\n", index)) > -1) {
796 String8 line(str, index - lastIndex);
797 if (line.length() == 0) line.append("\n");
798 PROGRAM_LOGD("%s", line.string());
799 index++;
800 str += (index - lastIndex);
801 lastIndex = index;
802 }
803}
804
Romain Guyac670c02010-07-27 17:39:27 -0700805}; // namespace uirenderer
806}; // namespace android