blob: 6d504100c0fa6be0c39050d90ee4b1b106c83acb [file] [log] [blame]
Romain Guyac670c02010-07-27 17:39:27 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "OpenGLRenderer"
18
19#include <utils/String8.h>
20
Romain Guya60c3882011-08-01 15:28:16 -070021#include "Caches.h"
Romain Guyb4880042013-04-05 11:17:55 -070022#include "Dither.h"
Romain Guyac670c02010-07-27 17:39:27 -070023#include "ProgramCache.h"
24
25namespace android {
26namespace uirenderer {
27
28///////////////////////////////////////////////////////////////////////////////
Romain Guy707b2f72010-10-11 16:34:59 -070029// Defines
30///////////////////////////////////////////////////////////////////////////////
31
32#define MODULATE_OP_NO_MODULATE 0
33#define MODULATE_OP_MODULATE 1
34#define MODULATE_OP_MODULATE_A8 2
35
Romain Guyb4880042013-04-05 11:17:55 -070036#define STR(x) STR1(x)
37#define STR1(x) #x
38
Romain Guy707b2f72010-10-11 16:34:59 -070039///////////////////////////////////////////////////////////////////////////////
Romain Guyac670c02010-07-27 17:39:27 -070040// Vertex shaders snippets
41///////////////////////////////////////////////////////////////////////////////
42
Romain Guyac670c02010-07-27 17:39:27 -070043const char* gVS_Header_Attributes =
44 "attribute vec4 position;\n";
45const char* gVS_Header_Attributes_TexCoords =
46 "attribute vec2 texCoords;\n";
Romain Guyff316ec2013-02-13 18:39:43 -080047const char* gVS_Header_Attributes_Colors =
48 "attribute vec4 colors;\n";
Chris Craik710f46d2012-09-17 17:25:49 -070049const char* gVS_Header_Attributes_AAVertexShapeParameters =
Chris Craik6ebdc112012-08-31 18:24:33 -070050 "attribute float vtxAlpha;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -070051const char* gVS_Header_Uniforms_TextureTransform =
52 "uniform mat4 mainTextureTransform;\n";
Romain Guyac670c02010-07-27 17:39:27 -070053const char* gVS_Header_Uniforms =
Romain Guy39284b72012-09-26 16:39:40 -070054 "uniform mat4 projection;\n" \
Romain Guyac670c02010-07-27 17:39:27 -070055 "uniform mat4 transform;\n";
Romain Guyb4880042013-04-05 11:17:55 -070056const char* gVS_Header_Uniforms_HasGradient =
57 "uniform mat4 screenSpace;\n";
Romain Guy889f8d12010-07-29 14:37:42 -070058const char* gVS_Header_Uniforms_HasBitmap =
59 "uniform mat4 textureTransform;\n"
Romain Guy80bbfb12011-03-23 16:56:28 -070060 "uniform mediump vec2 textureDimension;\n";
Romain Guyac670c02010-07-27 17:39:27 -070061const char* gVS_Header_Varyings_HasTexture =
62 "varying vec2 outTexCoords;\n";
Romain Guyff316ec2013-02-13 18:39:43 -080063const char* gVS_Header_Varyings_HasColors =
64 "varying vec4 outColors;\n";
Chris Craik710f46d2012-09-17 17:25:49 -070065const char* gVS_Header_Varyings_IsAAVertexShape =
Chris Craik6ebdc112012-08-31 18:24:33 -070066 "varying float alpha;\n";
Romain Guy63553472012-07-18 20:04:14 -070067const char* gVS_Header_Varyings_HasBitmap =
68 "varying highp vec2 outBitmapTexCoords;\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -070069const char* gVS_Header_Varyings_HasGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -070070 // Linear
Chet Haasea1d12dd2012-09-21 14:50:14 -070071 "varying highp vec2 linear;\n"
72 "varying vec2 ditherTexCoords;\n",
73 "varying float linear;\n"
74 "varying vec2 ditherTexCoords;\n",
Romain Guy211efea2012-07-31 21:16:07 -070075
Romain Guyee916f12010-09-20 17:53:08 -070076 // Circular
Chet Haasea1d12dd2012-09-21 14:50:14 -070077 "varying highp vec2 circular;\n"
78 "varying vec2 ditherTexCoords;\n",
79 "varying highp vec2 circular;\n"
80 "varying vec2 ditherTexCoords;\n",
Romain Guy211efea2012-07-31 21:16:07 -070081
Romain Guyee916f12010-09-20 17:53:08 -070082 // Sweep
Chet Haasea1d12dd2012-09-21 14:50:14 -070083 "varying highp vec2 sweep;\n"
84 "varying vec2 ditherTexCoords;\n",
85 "varying highp vec2 sweep;\n"
86 "varying vec2 ditherTexCoords;\n",
Romain Guyee916f12010-09-20 17:53:08 -070087};
Romain Guyac670c02010-07-27 17:39:27 -070088const char* gVS_Main =
89 "\nvoid main(void) {\n";
90const char* gVS_Main_OutTexCoords =
91 " outTexCoords = texCoords;\n";
Romain Guyff316ec2013-02-13 18:39:43 -080092const char* gVS_Main_OutColors =
93 " outColors = colors;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -070094const char* gVS_Main_OutTransformedTexCoords =
95 " outTexCoords = (mainTextureTransform * vec4(texCoords, 0.0, 1.0)).xy;\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -070096const char* gVS_Main_OutGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -070097 // Linear
Chet Haasea1d12dd2012-09-21 14:50:14 -070098 " linear = vec2((screenSpace * position).x, 0.5);\n"
Romain Guyb4880042013-04-05 11:17:55 -070099 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Chet Haasea1d12dd2012-09-21 14:50:14 -0700100 " linear = (screenSpace * position).x;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700101 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Romain Guy211efea2012-07-31 21:16:07 -0700102
Romain Guyee916f12010-09-20 17:53:08 -0700103 // Circular
Chet Haasea1d12dd2012-09-21 14:50:14 -0700104 " circular = (screenSpace * position).xy;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700105 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Chet Haasea1d12dd2012-09-21 14:50:14 -0700106 " circular = (screenSpace * position).xy;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700107 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Romain Guy211efea2012-07-31 21:16:07 -0700108
Romain Guyee916f12010-09-20 17:53:08 -0700109 // Sweep
Chet Haasea1d12dd2012-09-21 14:50:14 -0700110 " sweep = (screenSpace * position).xy;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700111 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Chet Haasea1d12dd2012-09-21 14:50:14 -0700112 " sweep = (screenSpace * position).xy;\n"
Romain Guyb4880042013-04-05 11:17:55 -0700113 " ditherTexCoords = (transform * position).xy * " STR(DITHER_KERNEL_SIZE_INV) ";\n",
Romain Guyee916f12010-09-20 17:53:08 -0700114};
Romain Guy889f8d12010-07-29 14:37:42 -0700115const char* gVS_Main_OutBitmapTexCoords =
Romain Guy707b2f72010-10-11 16:34:59 -0700116 " outBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700117const char* gVS_Main_Position =
Romain Guy39284b72012-09-26 16:39:40 -0700118 " gl_Position = projection * transform * position;\n";
Chris Craik710f46d2012-09-17 17:25:49 -0700119const char* gVS_Main_AAVertexShape =
Chris Craik6ebdc112012-08-31 18:24:33 -0700120 " alpha = vtxAlpha;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700121const char* gVS_Footer =
122 "}\n\n";
123
124///////////////////////////////////////////////////////////////////////////////
125// Fragment shaders snippets
126///////////////////////////////////////////////////////////////////////////////
127
Romain Guya5aed0d2010-09-09 14:42:43 -0700128const char* gFS_Header_Extension_FramebufferFetch =
129 "#extension GL_NV_shader_framebuffer_fetch : enable\n\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700130const char* gFS_Header_Extension_ExternalTexture =
131 "#extension GL_OES_EGL_image_external : require\n\n";
Romain Guyac670c02010-07-27 17:39:27 -0700132const char* gFS_Header =
133 "precision mediump float;\n\n";
134const char* gFS_Uniforms_Color =
135 "uniform vec4 color;\n";
136const char* gFS_Uniforms_TextureSampler =
Romain Guya938f562012-09-13 20:31:08 -0700137 "uniform sampler2D baseSampler;\n";
Romain Guyaa6c24c2011-04-28 18:40:04 -0700138const char* gFS_Uniforms_ExternalTextureSampler =
Romain Guya938f562012-09-13 20:31:08 -0700139 "uniform samplerExternalOES baseSampler;\n";
Romain Guyb4880042013-04-05 11:17:55 -0700140const char* gFS_Uniforms_Dither =
141 "uniform sampler2D ditherSampler;";
142const char* gFS_Uniforms_GradientSampler[2] = {
143 "%s\n"
144 "uniform sampler2D gradientSampler;\n",
145 "%s\n"
146 "uniform vec4 startColor;\n"
Romain Guy211efea2012-07-31 21:16:07 -0700147 "uniform vec4 endColor;\n"
Romain Guyee916f12010-09-20 17:53:08 -0700148};
Romain Guyac670c02010-07-27 17:39:27 -0700149const char* gFS_Uniforms_BitmapSampler =
150 "uniform sampler2D bitmapSampler;\n";
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500151const char* gFS_Uniforms_ColorOp[3] = {
Romain Guyac670c02010-07-27 17:39:27 -0700152 // None
153 "",
154 // Matrix
155 "uniform mat4 colorMatrix;\n"
156 "uniform vec4 colorMatrixVector;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700157 // PorterDuff
Romain Guydb1938e2010-08-02 18:50:22 -0700158 "uniform vec4 colorBlend;\n"
Romain Guyac670c02010-07-27 17:39:27 -0700159};
Romain Guy41210632012-07-16 17:04:24 -0700160const char* gFS_Uniforms_Gamma =
161 "uniform float gamma;\n";
162
Romain Guyac670c02010-07-27 17:39:27 -0700163const char* gFS_Main =
164 "\nvoid main(void) {\n"
Romain Guy7fbcc042010-08-04 15:40:07 -0700165 " lowp vec4 fragColor;\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700166
Romain Guyb4880042013-04-05 11:17:55 -0700167const char* gFS_Main_Dither[2] = {
168 // ES 2.0
169 "texture2D(ditherSampler, ditherTexCoords).a * " STR(DITHER_KERNEL_SIZE_INV_SQUARE),
170 // ES 3.0
Romain Guy032d47a2013-04-08 19:45:40 -0700171 "texture2D(ditherSampler, ditherTexCoords).a"
Romain Guyb4880042013-04-05 11:17:55 -0700172};
Romain Guy211efea2012-07-31 21:16:07 -0700173const char* gFS_Main_AddDitherToGradient =
Romain Guyb4880042013-04-05 11:17:55 -0700174 " gradientColor += %s;\n";
Romain Guy211efea2012-07-31 21:16:07 -0700175
Romain Guy707b2f72010-10-11 16:34:59 -0700176// Fast cases
177const char* gFS_Fast_SingleColor =
178 "\nvoid main(void) {\n"
179 " gl_FragColor = color;\n"
180 "}\n\n";
181const char* gFS_Fast_SingleTexture =
182 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700183 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700184 "}\n\n";
185const char* gFS_Fast_SingleModulateTexture =
186 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700187 " gl_FragColor = color.a * texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700188 "}\n\n";
189const char* gFS_Fast_SingleA8Texture =
190 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700191 " gl_FragColor = texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700192 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700193const char* gFS_Fast_SingleA8Texture_ApplyGamma =
194 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700195 " gl_FragColor = vec4(0.0, 0.0, 0.0, pow(texture2D(baseSampler, outTexCoords).a, gamma));\n"
Romain Guy41210632012-07-16 17:04:24 -0700196 "}\n\n";
Romain Guy707b2f72010-10-11 16:34:59 -0700197const char* gFS_Fast_SingleModulateA8Texture =
198 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700199 " gl_FragColor = color * texture2D(baseSampler, outTexCoords).a;\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700200 "}\n\n";
Romain Guy41210632012-07-16 17:04:24 -0700201const char* gFS_Fast_SingleModulateA8Texture_ApplyGamma =
202 "\nvoid main(void) {\n"
Romain Guya938f562012-09-13 20:31:08 -0700203 " gl_FragColor = color * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy41210632012-07-16 17:04:24 -0700204 "}\n\n";
Romain Guy42e1e0d2012-07-30 14:47:51 -0700205const char* gFS_Fast_SingleGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700206 "\nvoid main(void) {\n"
Romain Guyb4880042013-04-05 11:17:55 -0700207 " gl_FragColor = %s + texture2D(gradientSampler, linear);\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700208 "}\n\n",
209 "\nvoid main(void) {\n"
Romain Guyb4880042013-04-05 11:17:55 -0700210 " gl_FragColor = %s + mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
211 "}\n\n",
Romain Guy42e1e0d2012-07-30 14:47:51 -0700212};
213const char* gFS_Fast_SingleModulateGradient[2] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700214 "\nvoid main(void) {\n"
Romain Guyb4880042013-04-05 11:17:55 -0700215 " gl_FragColor = %s + color.a * texture2D(gradientSampler, linear);\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700216 "}\n\n",
217 "\nvoid main(void) {\n"
Romain Guyb4880042013-04-05 11:17:55 -0700218 " gl_FragColor = %s + color.a * mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n"
Romain Guy42e1e0d2012-07-30 14:47:51 -0700219 "}\n\n"
220};
Romain Guy707b2f72010-10-11 16:34:59 -0700221
222// General case
Romain Guyac670c02010-07-27 17:39:27 -0700223const char* gFS_Main_FetchColor =
224 " fragColor = color;\n";
Romain Guy740bf2b2011-04-26 15:33:10 -0700225const char* gFS_Main_ModulateColor =
226 " fragColor *= color.a;\n";
Chris Craik710f46d2012-09-17 17:25:49 -0700227const char* gFS_Main_AccountForAAVertexShape =
Chris Craik6ebdc112012-08-31 18:24:33 -0700228 " fragColor *= alpha;\n";
Chris Craika798b952012-08-27 17:03:13 -0700229
Romain Guy707b2f72010-10-11 16:34:59 -0700230const char* gFS_Main_FetchTexture[2] = {
231 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700232 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700233 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700234 " fragColor = color * texture2D(baseSampler, outTexCoords);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700235};
Romain Guya938f562012-09-13 20:31:08 -0700236const char* gFS_Main_FetchA8Texture[4] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700237 // Don't modulate
Romain Guya938f562012-09-13 20:31:08 -0700238 " fragColor = texture2D(baseSampler, outTexCoords);\n",
239 " fragColor = texture2D(baseSampler, outTexCoords);\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700240 // Modulate
Romain Guya938f562012-09-13 20:31:08 -0700241 " fragColor = color * texture2D(baseSampler, outTexCoords).a;\n",
242 " fragColor = color * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700243};
Romain Guy42e1e0d2012-07-30 14:47:51 -0700244const char* gFS_Main_FetchGradient[6] = {
Romain Guyee916f12010-09-20 17:53:08 -0700245 // Linear
Romain Guy320d46b2012-08-08 16:05:42 -0700246 " vec4 gradientColor = texture2D(gradientSampler, linear);\n",
Romain Guy211efea2012-07-31 21:16:07 -0700247
Romain Guy320d46b2012-08-08 16:05:42 -0700248 " vec4 gradientColor = mix(startColor, endColor, clamp(linear, 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700249
Romain Guyee916f12010-09-20 17:53:08 -0700250 // Circular
Romain Guy320d46b2012-08-08 16:05:42 -0700251 " vec4 gradientColor = texture2D(gradientSampler, vec2(length(circular), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700252
Romain Guy320d46b2012-08-08 16:05:42 -0700253 " vec4 gradientColor = mix(startColor, endColor, clamp(length(circular), 0.0, 1.0));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700254
Romain Guyee916f12010-09-20 17:53:08 -0700255 // Sweep
Romain Guy63553472012-07-18 20:04:14 -0700256 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700257 " vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n",
Romain Guy211efea2012-07-31 21:16:07 -0700258
Romain Guy42e1e0d2012-07-30 14:47:51 -0700259 " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n"
Romain Guy320d46b2012-08-08 16:05:42 -0700260 " vec4 gradientColor = mix(startColor, endColor, clamp(index - floor(index), 0.0, 1.0));\n"
Romain Guyee916f12010-09-20 17:53:08 -0700261};
Romain Guyac670c02010-07-27 17:39:27 -0700262const char* gFS_Main_FetchBitmap =
263 " vec4 bitmapColor = texture2D(bitmapSampler, outBitmapTexCoords);\n";
Romain Guy889f8d12010-07-29 14:37:42 -0700264const char* gFS_Main_FetchBitmapNpot =
265 " vec4 bitmapColor = texture2D(bitmapSampler, wrap(outBitmapTexCoords));\n";
Romain Guyac670c02010-07-27 17:39:27 -0700266const char* gFS_Main_BlendShadersBG =
Romain Guyac670c02010-07-27 17:39:27 -0700267 " fragColor = blendShaders(gradientColor, bitmapColor)";
Romain Guy06f96e22010-07-30 19:18:16 -0700268const char* gFS_Main_BlendShadersGB =
269 " fragColor = blendShaders(bitmapColor, gradientColor)";
Romain Guya938f562012-09-13 20:31:08 -0700270const char* gFS_Main_BlendShaders_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700271 // Don't modulate
272 ";\n",
Romain Guya938f562012-09-13 20:31:08 -0700273 ";\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700274 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700275 " * color.a;\n",
276 " * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700277 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700278 " * texture2D(baseSampler, outTexCoords).a;\n",
279 " * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700280};
Romain Guya938f562012-09-13 20:31:08 -0700281const char* gFS_Main_GradientShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700282 // Don't modulate
283 " fragColor = gradientColor;\n",
Romain Guya938f562012-09-13 20:31:08 -0700284 " fragColor = gradientColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700285 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700286 " fragColor = gradientColor * color.a;\n",
287 " fragColor = gradientColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700288 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700289 " fragColor = gradientColor * texture2D(baseSampler, outTexCoords).a;\n",
290 " fragColor = gradientColor * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700291 };
Romain Guya938f562012-09-13 20:31:08 -0700292const char* gFS_Main_BitmapShader_Modulate[6] = {
Romain Guy707b2f72010-10-11 16:34:59 -0700293 // Don't modulate
294 " fragColor = bitmapColor;\n",
Romain Guya938f562012-09-13 20:31:08 -0700295 " fragColor = bitmapColor;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700296 // Modulate
Chet Haase0990ffb2012-09-17 17:43:45 -0700297 " fragColor = bitmapColor * color.a;\n",
298 " fragColor = bitmapColor * color.a;\n",
Romain Guy707b2f72010-10-11 16:34:59 -0700299 // Modulate with alpha 8 texture
Romain Guya938f562012-09-13 20:31:08 -0700300 " fragColor = bitmapColor * texture2D(baseSampler, outTexCoords).a;\n",
301 " fragColor = bitmapColor * pow(texture2D(baseSampler, outTexCoords).a, gamma);\n"
Romain Guy707b2f72010-10-11 16:34:59 -0700302 };
Romain Guyac670c02010-07-27 17:39:27 -0700303const char* gFS_Main_FragColor =
304 " gl_FragColor = fragColor;\n";
Romain Guyff316ec2013-02-13 18:39:43 -0800305const char* gFS_Main_FragColor_HasColors =
306 " gl_FragColor *= outColors;\n";
Romain Guya5aed0d2010-09-09 14:42:43 -0700307const char* gFS_Main_FragColor_Blend =
308 " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n";
Romain Guyf607bdc2010-09-10 19:20:06 -0700309const char* gFS_Main_FragColor_Blend_Swap =
310 " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n";
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500311const char* gFS_Main_ApplyColorOp[3] = {
Romain Guyac670c02010-07-27 17:39:27 -0700312 // None
313 "",
314 // Matrix
315 " fragColor *= colorMatrix;\n"
Romain Guydb1938e2010-08-02 18:50:22 -0700316 " fragColor += colorMatrixVector;\n"
317 " fragColor.rgb *= fragColor.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700318 // PorterDuff
319 " fragColor = blendColors(colorBlend, fragColor);\n"
320};
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800321const char* gFS_Main_DebugHighlight =
322 " gl_FragColor.rgb = vec3(0.0, gl_FragColor.a, 0.0);\n";
Romain Guy78dd96d2013-05-03 14:24:16 -0700323const char* gFS_Main_EmulateStencil =
324 " gl_FragColor.rgba = vec4(1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0, 1.0);\n"
325 " return;\n"
326 " /*\n";
327const char* gFS_Footer_EmulateStencil =
328 " */\n";
Romain Guyac670c02010-07-27 17:39:27 -0700329const char* gFS_Footer =
330 "}\n\n";
331
332///////////////////////////////////////////////////////////////////////////////
333// PorterDuff snippets
334///////////////////////////////////////////////////////////////////////////////
335
Romain Guy48daa542010-08-10 19:21:34 -0700336const char* gBlendOps[18] = {
Romain Guyac670c02010-07-27 17:39:27 -0700337 // Clear
338 "return vec4(0.0, 0.0, 0.0, 0.0);\n",
339 // Src
340 "return src;\n",
341 // Dst
342 "return dst;\n",
343 // SrcOver
Romain Guy06f96e22010-07-30 19:18:16 -0700344 "return src + dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700345 // DstOver
Romain Guy06f96e22010-07-30 19:18:16 -0700346 "return dst + src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700347 // SrcIn
Romain Guy06f96e22010-07-30 19:18:16 -0700348 "return src * dst.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700349 // DstIn
Romain Guy06f96e22010-07-30 19:18:16 -0700350 "return dst * src.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700351 // SrcOut
Romain Guy06f96e22010-07-30 19:18:16 -0700352 "return src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700353 // DstOut
Romain Guy06f96e22010-07-30 19:18:16 -0700354 "return dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700355 // SrcAtop
356 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n",
357 // DstAtop
358 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n",
359 // Xor
Romain Guy48daa542010-08-10 19:21:34 -0700360 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, "
Romain Guyac670c02010-07-27 17:39:27 -0700361 "src.a + dst.a - 2.0 * src.a * dst.a);\n",
Romain Guy48daa542010-08-10 19:21:34 -0700362 // Add
363 "return min(src + dst, 1.0);\n",
364 // Multiply
365 "return src * dst;\n",
366 // Screen
367 "return src + dst - src * dst;\n",
368 // Overlay
369 "return clamp(vec4(mix("
370 "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), "
371 "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), "
372 "step(dst.a, 2.0 * dst.rgb)), "
373 "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n",
374 // Darken
375 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
376 "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n",
377 // Lighten
378 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + "
379 "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 -0700380};
381
382///////////////////////////////////////////////////////////////////////////////
383// Constructors/destructors
384///////////////////////////////////////////////////////////////////////////////
385
Romain Guyb4880042013-04-05 11:17:55 -0700386ProgramCache::ProgramCache(): mHasES3(Extensions::getInstance().getMajorGlVersion() >= 3) {
Romain Guyac670c02010-07-27 17:39:27 -0700387}
388
389ProgramCache::~ProgramCache() {
390 clear();
391}
392
393///////////////////////////////////////////////////////////////////////////////
394// Cache management
395///////////////////////////////////////////////////////////////////////////////
396
397void ProgramCache::clear() {
Romain Guy67f27952010-12-07 20:09:23 -0800398 PROGRAM_LOGD("Clearing program cache");
399
Romain Guyac670c02010-07-27 17:39:27 -0700400 size_t count = mCache.size();
401 for (size_t i = 0; i < count; i++) {
402 delete mCache.valueAt(i);
403 }
404 mCache.clear();
405}
406
407Program* ProgramCache::get(const ProgramDescription& description) {
408 programid key = description.key();
Chris Craik096b8d92013-03-01 11:08:11 -0800409 if (key == (PROGRAM_KEY_TEXTURE | PROGRAM_KEY_A8_TEXTURE)) {
410 // program for A8, unmodulated, texture w/o shader (black text/path textures) is equivalent
411 // to standard texture program (bitmaps, patches). Consider them equivalent.
412 key = PROGRAM_KEY_TEXTURE;
413 }
414
Romain Guyac670c02010-07-27 17:39:27 -0700415 ssize_t index = mCache.indexOfKey(key);
416 Program* program = NULL;
417 if (index < 0) {
Romain Guyee916f12010-09-20 17:53:08 -0700418 description.log("Could not find program");
Romain Guyac670c02010-07-27 17:39:27 -0700419 program = generateProgram(description, key);
420 mCache.add(key, program);
421 } else {
422 program = mCache.valueAt(index);
423 }
424 return program;
425}
426
427///////////////////////////////////////////////////////////////////////////////
428// Program generation
429///////////////////////////////////////////////////////////////////////////////
430
431Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) {
432 String8 vertexShader = generateVertexShader(description);
433 String8 fragmentShader = generateFragmentShader(description);
434
Romain Guy42e1e0d2012-07-30 14:47:51 -0700435 return new Program(description, vertexShader.string(), fragmentShader.string());
436}
437
438static inline size_t gradientIndex(const ProgramDescription& description) {
439 return description.gradientType * 2 + description.isSimpleGradient;
Romain Guyac670c02010-07-27 17:39:27 -0700440}
441
442String8 ProgramCache::generateVertexShader(const ProgramDescription& description) {
443 // Add attributes
444 String8 shader(gVS_Header_Attributes);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700445 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700446 shader.append(gVS_Header_Attributes_TexCoords);
447 }
Chris Craik710f46d2012-09-17 17:25:49 -0700448 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800449 shader.append(gVS_Header_Attributes_AAVertexShapeParameters);
Chet Haase5b0200b2011-04-13 17:58:08 -0700450 }
Romain Guyff316ec2013-02-13 18:39:43 -0800451 if (description.hasColors) {
452 shader.append(gVS_Header_Attributes_Colors);
453 }
Romain Guyac670c02010-07-27 17:39:27 -0700454 // Uniforms
455 shader.append(gVS_Header_Uniforms);
Romain Guy8f0095c2011-05-02 17:24:22 -0700456 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700457 shader.append(gVS_Header_Uniforms_TextureTransform);
458 }
Romain Guyac670c02010-07-27 17:39:27 -0700459 if (description.hasGradient) {
Romain Guyb4880042013-04-05 11:17:55 -0700460 shader.append(gVS_Header_Uniforms_HasGradient);
Romain Guyac670c02010-07-27 17:39:27 -0700461 }
Romain Guy889f8d12010-07-29 14:37:42 -0700462 if (description.hasBitmap) {
463 shader.append(gVS_Header_Uniforms_HasBitmap);
464 }
Romain Guyac670c02010-07-27 17:39:27 -0700465 // Varyings
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_Varyings_HasTexture);
468 }
Chris Craik710f46d2012-09-17 17:25:49 -0700469 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800470 shader.append(gVS_Header_Varyings_IsAAVertexShape);
Chet Haase5b0200b2011-04-13 17:58:08 -0700471 }
Romain Guyff316ec2013-02-13 18:39:43 -0800472 if (description.hasColors) {
473 shader.append(gVS_Header_Varyings_HasColors);
474 }
Romain Guyac670c02010-07-27 17:39:27 -0700475 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700476 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700477 }
478 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700479 shader.append(gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700480 }
481
482 // Begin the shader
483 shader.append(gVS_Main); {
Romain Guy8f0095c2011-05-02 17:24:22 -0700484 if (description.hasTextureTransform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700485 shader.append(gVS_Main_OutTransformedTexCoords);
Romain Guy8f0095c2011-05-02 17:24:22 -0700486 } else if (description.hasTexture || description.hasExternalTexture) {
487 shader.append(gVS_Main_OutTexCoords);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700488 }
Chris Craik710f46d2012-09-17 17:25:49 -0700489 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800490 shader.append(gVS_Main_AAVertexShape);
Chet Haase5b0200b2011-04-13 17:58:08 -0700491 }
Romain Guyff316ec2013-02-13 18:39:43 -0800492 if (description.hasColors) {
493 shader.append(gVS_Main_OutColors);
494 }
Romain Guy889f8d12010-07-29 14:37:42 -0700495 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700496 shader.append(gVS_Main_OutBitmapTexCoords);
Romain Guy889f8d12010-07-29 14:37:42 -0700497 }
Romain Guyac670c02010-07-27 17:39:27 -0700498 // Output transformed position
499 shader.append(gVS_Main_Position);
Chet Haasea1d12dd2012-09-21 14:50:14 -0700500 if (description.hasGradient) {
501 shader.append(gVS_Main_OutGradient[gradientIndex(description)]);
502 }
Romain Guyac670c02010-07-27 17:39:27 -0700503 }
504 // End the shader
505 shader.append(gVS_Footer);
506
507 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string());
508
509 return shader;
510}
511
Romain Guya938f562012-09-13 20:31:08 -0700512static bool shaderOp(const ProgramDescription& description, String8& shader,
513 const int modulateOp, const char** snippets) {
514 int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp;
515 op = op * 2 + description.hasGammaCorrection;
516 shader.append(snippets[op]);
517 return description.hasAlpha8Texture;
518}
519
Romain Guyac670c02010-07-27 17:39:27 -0700520String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) {
Romain Guya5aed0d2010-09-09 14:42:43 -0700521 String8 shader;
522
Romain Guy707b2f72010-10-11 16:34:59 -0700523 const bool blendFramebuffer = description.framebufferMode >= SkXfermode::kPlus_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700524 if (blendFramebuffer) {
525 shader.append(gFS_Header_Extension_FramebufferFetch);
526 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700527 if (description.hasExternalTexture) {
528 shader.append(gFS_Header_Extension_ExternalTexture);
529 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700530
531 shader.append(gFS_Header);
Romain Guyac670c02010-07-27 17:39:27 -0700532
533 // Varyings
Romain Guyaa6c24c2011-04-28 18:40:04 -0700534 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700535 shader.append(gVS_Header_Varyings_HasTexture);
536 }
Chris Craik710f46d2012-09-17 17:25:49 -0700537 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800538 shader.append(gVS_Header_Varyings_IsAAVertexShape);
Chet Haase5b0200b2011-04-13 17:58:08 -0700539 }
Romain Guyff316ec2013-02-13 18:39:43 -0800540 if (description.hasColors) {
541 shader.append(gVS_Header_Varyings_HasColors);
542 }
Romain Guyac670c02010-07-27 17:39:27 -0700543 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700544 shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]);
Romain Guyac670c02010-07-27 17:39:27 -0700545 }
546 if (description.hasBitmap) {
Chris Craik6d29c8d2013-05-08 18:35:44 -0700547 shader.append(gVS_Header_Varyings_HasBitmap);
Romain Guyac670c02010-07-27 17:39:27 -0700548 }
549
Romain Guyac670c02010-07-27 17:39:27 -0700550 // Uniforms
Romain Guy707b2f72010-10-11 16:34:59 -0700551 int modulateOp = MODULATE_OP_NO_MODULATE;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700552 const bool singleColor = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700553 !description.hasGradient && !description.hasBitmap;
554
555 if (description.modulate || singleColor) {
556 shader.append(gFS_Uniforms_Color);
557 if (!singleColor) modulateOp = MODULATE_OP_MODULATE;
558 }
Romain Guyac670c02010-07-27 17:39:27 -0700559 if (description.hasTexture) {
560 shader.append(gFS_Uniforms_TextureSampler);
Romain Guy8f0095c2011-05-02 17:24:22 -0700561 } else if (description.hasExternalTexture) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700562 shader.append(gFS_Uniforms_ExternalTextureSampler);
563 }
Romain Guyac670c02010-07-27 17:39:27 -0700564 if (description.hasGradient) {
Romain Guyb4880042013-04-05 11:17:55 -0700565 shader.appendFormat(gFS_Uniforms_GradientSampler[description.isSimpleGradient],
566 gFS_Uniforms_Dither);
Romain Guyac670c02010-07-27 17:39:27 -0700567 }
Romain Guy41210632012-07-16 17:04:24 -0700568 if (description.hasGammaCorrection) {
569 shader.append(gFS_Uniforms_Gamma);
570 }
Romain Guy707b2f72010-10-11 16:34:59 -0700571
572 // Optimization for common cases
Romain Guyff316ec2013-02-13 18:39:43 -0800573 if (!description.isAA && !blendFramebuffer && !description.hasColors &&
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800574 description.colorOp == ProgramDescription::kColorNone &&
Chris Craik6d29c8d2013-05-08 18:35:44 -0700575 !description.hasDebugHighlight && !description.emulateStencil) {
Romain Guy707b2f72010-10-11 16:34:59 -0700576 bool fast = false;
577
578 const bool noShader = !description.hasGradient && !description.hasBitmap;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700579 const bool singleTexture = (description.hasTexture || description.hasExternalTexture) &&
Romain Guy707b2f72010-10-11 16:34:59 -0700580 !description.hasAlpha8Texture && noShader;
581 const bool singleA8Texture = description.hasTexture &&
582 description.hasAlpha8Texture && noShader;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700583 const bool singleGradient = !description.hasTexture && !description.hasExternalTexture &&
Romain Guy707b2f72010-10-11 16:34:59 -0700584 description.hasGradient && !description.hasBitmap &&
585 description.gradientType == ProgramDescription::kGradientLinear;
586
587 if (singleColor) {
588 shader.append(gFS_Fast_SingleColor);
589 fast = true;
590 } else if (singleTexture) {
591 if (!description.modulate) {
592 shader.append(gFS_Fast_SingleTexture);
593 } else {
594 shader.append(gFS_Fast_SingleModulateTexture);
595 }
596 fast = true;
597 } else if (singleA8Texture) {
598 if (!description.modulate) {
Romain Guy41210632012-07-16 17:04:24 -0700599 if (description.hasGammaCorrection) {
600 shader.append(gFS_Fast_SingleA8Texture_ApplyGamma);
601 } else {
602 shader.append(gFS_Fast_SingleA8Texture);
603 }
Romain Guy707b2f72010-10-11 16:34:59 -0700604 } else {
Romain Guy41210632012-07-16 17:04:24 -0700605 if (description.hasGammaCorrection) {
606 shader.append(gFS_Fast_SingleModulateA8Texture_ApplyGamma);
607 } else {
608 shader.append(gFS_Fast_SingleModulateA8Texture);
609 }
Romain Guy707b2f72010-10-11 16:34:59 -0700610 }
611 fast = true;
612 } else if (singleGradient) {
613 if (!description.modulate) {
Romain Guyb4880042013-04-05 11:17:55 -0700614 shader.appendFormat(gFS_Fast_SingleGradient[description.isSimpleGradient],
615 gFS_Main_Dither[mHasES3]);
Romain Guy707b2f72010-10-11 16:34:59 -0700616 } else {
Romain Guyb4880042013-04-05 11:17:55 -0700617 shader.appendFormat(gFS_Fast_SingleModulateGradient[description.isSimpleGradient],
618 gFS_Main_Dither[mHasES3]);
Romain Guy707b2f72010-10-11 16:34:59 -0700619 }
620 fast = true;
621 }
622
623 if (fast) {
Romain Guyc15008e2010-11-10 11:59:15 -0800624#if DEBUG_PROGRAMS
Romain Guy707b2f72010-10-11 16:34:59 -0700625 PROGRAM_LOGD("*** Fast case:\n");
626 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
627 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800628#endif
Romain Guy707b2f72010-10-11 16:34:59 -0700629
630 return shader;
631 }
632 }
633
Romain Guyac670c02010-07-27 17:39:27 -0700634 if (description.hasBitmap) {
635 shader.append(gFS_Uniforms_BitmapSampler);
636 }
637 shader.append(gFS_Uniforms_ColorOp[description.colorOp]);
638
639 // Generate required functions
640 if (description.hasGradient && description.hasBitmap) {
Romain Guy48daa542010-08-10 19:21:34 -0700641 generateBlend(shader, "blendShaders", description.shadersMode);
Romain Guyac670c02010-07-27 17:39:27 -0700642 }
643 if (description.colorOp == ProgramDescription::kColorBlend) {
Romain Guy48daa542010-08-10 19:21:34 -0700644 generateBlend(shader, "blendColors", description.colorMode);
Romain Guyac670c02010-07-27 17:39:27 -0700645 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700646 if (blendFramebuffer) {
647 generateBlend(shader, "blendFramebuffer", description.framebufferMode);
648 }
Romain Guy889f8d12010-07-29 14:37:42 -0700649 if (description.isBitmapNpot) {
650 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
651 }
Romain Guyac670c02010-07-27 17:39:27 -0700652
653 // Begin the shader
654 shader.append(gFS_Main); {
Romain Guy78dd96d2013-05-03 14:24:16 -0700655 if (description.emulateStencil) {
656 shader.append(gFS_Main_EmulateStencil);
657 }
Romain Guyac670c02010-07-27 17:39:27 -0700658 // Stores the result in fragColor directly
Romain Guyaa6c24c2011-04-28 18:40:04 -0700659 if (description.hasTexture || description.hasExternalTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700660 if (description.hasAlpha8Texture) {
Romain Guy707b2f72010-10-11 16:34:59 -0700661 if (!description.hasGradient && !description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700662 shader.append(gFS_Main_FetchA8Texture[modulateOp * 2 +
663 description.hasGammaCorrection]);
Romain Guy707b2f72010-10-11 16:34:59 -0700664 }
Romain Guyac670c02010-07-27 17:39:27 -0700665 } else {
Romain Guy707b2f72010-10-11 16:34:59 -0700666 shader.append(gFS_Main_FetchTexture[modulateOp]);
Romain Guyac670c02010-07-27 17:39:27 -0700667 }
668 } else {
Romain Guya938f562012-09-13 20:31:08 -0700669 if (!description.hasGradient && !description.hasBitmap) {
Romain Guy707b2f72010-10-11 16:34:59 -0700670 shader.append(gFS_Main_FetchColor);
671 }
Romain Guyac670c02010-07-27 17:39:27 -0700672 }
673 if (description.hasGradient) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700674 shader.append(gFS_Main_FetchGradient[gradientIndex(description)]);
Romain Guyb4880042013-04-05 11:17:55 -0700675 shader.appendFormat(gFS_Main_AddDitherToGradient, gFS_Main_Dither[mHasES3]);
Romain Guyac670c02010-07-27 17:39:27 -0700676 }
677 if (description.hasBitmap) {
Romain Guy889f8d12010-07-29 14:37:42 -0700678 if (!description.isBitmapNpot) {
679 shader.append(gFS_Main_FetchBitmap);
680 } else {
681 shader.append(gFS_Main_FetchBitmapNpot);
682 }
Romain Guyac670c02010-07-27 17:39:27 -0700683 }
Romain Guy740bf2b2011-04-26 15:33:10 -0700684 bool applyModulate = false;
Romain Guyac670c02010-07-27 17:39:27 -0700685 // Case when we have two shaders set
686 if (description.hasGradient && description.hasBitmap) {
687 if (description.isBitmapFirst) {
688 shader.append(gFS_Main_BlendShadersBG);
689 } else {
690 shader.append(gFS_Main_BlendShadersGB);
691 }
Romain Guya938f562012-09-13 20:31:08 -0700692 applyModulate = shaderOp(description, shader, modulateOp,
693 gFS_Main_BlendShaders_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700694 } else {
695 if (description.hasGradient) {
Romain Guya938f562012-09-13 20:31:08 -0700696 applyModulate = shaderOp(description, shader, modulateOp,
697 gFS_Main_GradientShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700698 } else if (description.hasBitmap) {
Romain Guya938f562012-09-13 20:31:08 -0700699 applyModulate = shaderOp(description, shader, modulateOp,
700 gFS_Main_BitmapShader_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700701 }
Romain Guyac670c02010-07-27 17:39:27 -0700702 }
Romain Guya938f562012-09-13 20:31:08 -0700703
Romain Guy740bf2b2011-04-26 15:33:10 -0700704 if (description.modulate && applyModulate) {
Romain Guya938f562012-09-13 20:31:08 -0700705 shader.append(gFS_Main_ModulateColor);
Romain Guy740bf2b2011-04-26 15:33:10 -0700706 }
Romain Guya938f562012-09-13 20:31:08 -0700707
Romain Guyac670c02010-07-27 17:39:27 -0700708 // Apply the color op if needed
709 shader.append(gFS_Main_ApplyColorOp[description.colorOp]);
Chris Craik9f44a132012-09-13 18:34:55 -0700710
Chris Craik710f46d2012-09-17 17:25:49 -0700711 if (description.isAA) {
Chris Craik65cd6122012-12-10 17:56:27 -0800712 shader.append(gFS_Main_AccountForAAVertexShape);
Chris Craik9f44a132012-09-13 18:34:55 -0700713 }
714
Romain Guyac670c02010-07-27 17:39:27 -0700715 // Output the fragment
Romain Guya5aed0d2010-09-09 14:42:43 -0700716 if (!blendFramebuffer) {
717 shader.append(gFS_Main_FragColor);
718 } else {
Romain Guyf607bdc2010-09-10 19:20:06 -0700719 shader.append(!description.swapSrcDst ?
720 gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap);
Romain Guya5aed0d2010-09-09 14:42:43 -0700721 }
Romain Guyff316ec2013-02-13 18:39:43 -0800722 if (description.hasColors) {
723 shader.append(gFS_Main_FragColor_HasColors);
724 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800725 if (description.hasDebugHighlight) {
726 shader.append(gFS_Main_DebugHighlight);
727 }
Romain Guyac670c02010-07-27 17:39:27 -0700728 }
Romain Guy78dd96d2013-05-03 14:24:16 -0700729 if (description.emulateStencil) {
730 shader.append(gFS_Footer_EmulateStencil);
731 }
Romain Guyac670c02010-07-27 17:39:27 -0700732 // End the shader
733 shader.append(gFS_Footer);
734
Romain Guyc15008e2010-11-10 11:59:15 -0800735#if DEBUG_PROGRAMS
Romain Guydb1938e2010-08-02 18:50:22 -0700736 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
737 printLongString(shader);
Romain Guyc15008e2010-11-10 11:59:15 -0800738#endif
Romain Guydb1938e2010-08-02 18:50:22 -0700739
Romain Guyac670c02010-07-27 17:39:27 -0700740 return shader;
741}
742
Romain Guy48daa542010-08-10 19:21:34 -0700743void ProgramCache::generateBlend(String8& shader, const char* name, SkXfermode::Mode mode) {
Romain Guyac670c02010-07-27 17:39:27 -0700744 shader.append("\nvec4 ");
745 shader.append(name);
746 shader.append("(vec4 src, vec4 dst) {\n");
747 shader.append(" ");
Romain Guy48daa542010-08-10 19:21:34 -0700748 shader.append(gBlendOps[mode]);
Romain Guyac670c02010-07-27 17:39:27 -0700749 shader.append("}\n");
750}
751
Romain Guy889f8d12010-07-29 14:37:42 -0700752void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) {
Romain Guy63553472012-07-18 20:04:14 -0700753 shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700754 if (wrapS == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700755 shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700756 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n");
757 }
758 if (wrapT == GL_MIRRORED_REPEAT) {
Romain Guy63553472012-07-18 20:04:14 -0700759 shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n");
Romain Guy889f8d12010-07-29 14:37:42 -0700760 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n");
761 }
762 shader.append(" return vec2(");
763 switch (wrapS) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700764 case GL_CLAMP_TO_EDGE:
765 shader.append("texCoords.x");
766 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700767 case GL_REPEAT:
768 shader.append("mod(texCoords.x, 1.0)");
769 break;
770 case GL_MIRRORED_REPEAT:
771 shader.append("xMod2");
772 break;
773 }
774 shader.append(", ");
775 switch (wrapT) {
Romain Guy61c8c9c2010-08-09 20:48:09 -0700776 case GL_CLAMP_TO_EDGE:
777 shader.append("texCoords.y");
778 break;
Romain Guy889f8d12010-07-29 14:37:42 -0700779 case GL_REPEAT:
780 shader.append("mod(texCoords.y, 1.0)");
781 break;
782 case GL_MIRRORED_REPEAT:
783 shader.append("yMod2");
784 break;
785 }
786 shader.append(");\n");
787 shader.append("}\n");
788}
789
Romain Guydb1938e2010-08-02 18:50:22 -0700790void ProgramCache::printLongString(const String8& shader) const {
791 ssize_t index = 0;
792 ssize_t lastIndex = 0;
793 const char* str = shader.string();
794 while ((index = shader.find("\n", index)) > -1) {
795 String8 line(str, index - lastIndex);
796 if (line.length() == 0) line.append("\n");
797 PROGRAM_LOGD("%s", line.string());
798 index++;
799 str += (index - lastIndex);
800 lastIndex = index;
801 }
802}
803
Romain Guyac670c02010-07-27 17:39:27 -0700804}; // namespace uirenderer
805}; // namespace android