blob: 32052585ce7bfa3d287e3e7cc900c400b13b27a8 [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
21#include "ProgramCache.h"
22
23namespace android {
24namespace uirenderer {
25
26///////////////////////////////////////////////////////////////////////////////
27// Vertex shaders snippets
28///////////////////////////////////////////////////////////////////////////////
29
Romain Guyac670c02010-07-27 17:39:27 -070030const char* gVS_Header_Attributes =
31 "attribute vec4 position;\n";
32const char* gVS_Header_Attributes_TexCoords =
33 "attribute vec2 texCoords;\n";
34const char* gVS_Header_Uniforms =
35 "uniform mat4 transform;\n";
36const char* gVS_Header_Uniforms_HasGradient =
37 "uniform float gradientLength;\n"
38 "uniform vec2 gradient;\n"
39 "uniform vec2 gradientStart;\n"
40 "uniform mat4 screenSpace;\n";
Romain Guy889f8d12010-07-29 14:37:42 -070041const char* gVS_Header_Uniforms_HasBitmap =
42 "uniform mat4 textureTransform;\n"
43 "uniform vec2 textureDimension;\n";
Romain Guyac670c02010-07-27 17:39:27 -070044const char* gVS_Header_Varyings_HasTexture =
45 "varying vec2 outTexCoords;\n";
46const char* gVS_Header_Varyings_HasBitmap =
47 "varying vec2 outBitmapTexCoords;\n";
48const char* gVS_Header_Varyings_HasGradient =
49 "varying float index;\n";
50const char* gVS_Main =
51 "\nvoid main(void) {\n";
52const char* gVS_Main_OutTexCoords =
53 " outTexCoords = texCoords;\n";
54const char* gVS_Main_OutGradientIndex =
55 " vec4 location = screenSpace * position;\n"
56 " index = dot(location.xy - gradientStart, gradient) * gradientLength;\n";
Romain Guy889f8d12010-07-29 14:37:42 -070057const char* gVS_Main_OutBitmapTexCoords =
58 " vec4 bitmapCoords = textureTransform * position;\n"
59 " outBitmapTexCoords = bitmapCoords.xy * textureDimension;\n";
Romain Guyac670c02010-07-27 17:39:27 -070060const char* gVS_Main_Position =
61 " gl_Position = transform * position;\n";
62const char* gVS_Footer =
63 "}\n\n";
64
65///////////////////////////////////////////////////////////////////////////////
66// Fragment shaders snippets
67///////////////////////////////////////////////////////////////////////////////
68
69const char* gFS_Header =
70 "precision mediump float;\n\n";
71const char* gFS_Uniforms_Color =
72 "uniform vec4 color;\n";
73const char* gFS_Uniforms_TextureSampler =
74 "uniform sampler2D sampler;\n";
75const char* gFS_Uniforms_GradientSampler =
76 "uniform sampler2D gradientSampler;\n";
77const char* gFS_Uniforms_BitmapSampler =
78 "uniform sampler2D bitmapSampler;\n";
79const char* gFS_Uniforms_ColorOp[4] = {
80 // None
81 "",
82 // Matrix
83 "uniform mat4 colorMatrix;\n"
84 "uniform vec4 colorMatrixVector;\n",
85 // Lighting
Romain Guydb1938e2010-08-02 18:50:22 -070086 "uniform vec4 lightingMul;\n"
87 "uniform vec4 lightingAdd;\n",
Romain Guyac670c02010-07-27 17:39:27 -070088 // PorterDuff
Romain Guydb1938e2010-08-02 18:50:22 -070089 "uniform vec4 colorBlend;\n"
Romain Guyac670c02010-07-27 17:39:27 -070090};
91const char* gFS_Main =
92 "\nvoid main(void) {\n"
93 " vec4 fragColor;\n";
94const char* gFS_Main_FetchColor =
95 " fragColor = color;\n";
96const char* gFS_Main_FetchTexture =
97 " fragColor = color * texture2D(sampler, outTexCoords);\n";
98const char* gFS_Main_FetchA8Texture =
99 " fragColor = color * texture2D(sampler, outTexCoords).a;\n";
100const char* gFS_Main_FetchGradient =
101 " vec4 gradientColor = texture2D(gradientSampler, vec2(index, 0.5));\n";
102const char* gFS_Main_FetchBitmap =
103 " vec4 bitmapColor = texture2D(bitmapSampler, outBitmapTexCoords);\n";
Romain Guy889f8d12010-07-29 14:37:42 -0700104const char* gFS_Main_FetchBitmapNpot =
105 " vec4 bitmapColor = texture2D(bitmapSampler, wrap(outBitmapTexCoords));\n";
Romain Guyac670c02010-07-27 17:39:27 -0700106const char* gFS_Main_BlendShadersBG =
Romain Guyac670c02010-07-27 17:39:27 -0700107 " fragColor = blendShaders(gradientColor, bitmapColor)";
Romain Guy06f96e22010-07-30 19:18:16 -0700108const char* gFS_Main_BlendShadersGB =
109 " fragColor = blendShaders(bitmapColor, gradientColor)";
Romain Guyac670c02010-07-27 17:39:27 -0700110const char* gFS_Main_BlendShaders_Modulate =
111 " * fragColor.a;\n";
Romain Guy889f8d12010-07-29 14:37:42 -0700112const char* gFS_Main_GradientShader_Modulate =
113 " fragColor = gradientColor * fragColor.a;\n";
114const char* gFS_Main_BitmapShader_Modulate =
115 " fragColor = bitmapColor * fragColor.a;\n";
Romain Guyac670c02010-07-27 17:39:27 -0700116const char* gFS_Main_FragColor =
117 " gl_FragColor = fragColor;\n";
118const char* gFS_Main_ApplyColorOp[4] = {
119 // None
120 "",
121 // Matrix
Romain Guydb1938e2010-08-02 18:50:22 -0700122 // TODO: Fix premultiplied alpha computations for color matrix
Romain Guyac670c02010-07-27 17:39:27 -0700123 " fragColor *= colorMatrix;\n"
Romain Guydb1938e2010-08-02 18:50:22 -0700124 " fragColor += colorMatrixVector;\n"
125 " fragColor.rgb *= fragColor.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700126 // Lighting
Romain Guydb1938e2010-08-02 18:50:22 -0700127 " float lightingAlpha = fragColor.a;\n"
128 " fragColor = min(fragColor * lightingMul + (lightingAdd * lightingAlpha), lightingAlpha);\n"
129 " fragColor.a = lightingAlpha;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700130 // PorterDuff
131 " fragColor = blendColors(colorBlend, fragColor);\n"
132};
133const char* gFS_Footer =
134 "}\n\n";
135
136///////////////////////////////////////////////////////////////////////////////
137// PorterDuff snippets
138///////////////////////////////////////////////////////////////////////////////
139
140const char* gPorterDuff[12] = {
141 // Clear
142 "return vec4(0.0, 0.0, 0.0, 0.0);\n",
143 // Src
144 "return src;\n",
145 // Dst
146 "return dst;\n",
147 // SrcOver
Romain Guy06f96e22010-07-30 19:18:16 -0700148 "return src + dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700149 // DstOver
Romain Guy06f96e22010-07-30 19:18:16 -0700150 "return dst + src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700151 // SrcIn
Romain Guy06f96e22010-07-30 19:18:16 -0700152 "return src * dst.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700153 // DstIn
Romain Guy06f96e22010-07-30 19:18:16 -0700154 "return dst * src.a;\n",
Romain Guyac670c02010-07-27 17:39:27 -0700155 // SrcOut
Romain Guy06f96e22010-07-30 19:18:16 -0700156 "return src * (1.0 - dst.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700157 // DstOut
Romain Guy06f96e22010-07-30 19:18:16 -0700158 "return dst * (1.0 - src.a);\n",
Romain Guyac670c02010-07-27 17:39:27 -0700159 // SrcAtop
160 "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n",
161 // DstAtop
162 "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n",
163 // Xor
Romain Guy06f96e22010-07-30 19:18:16 -0700164 "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, 1.0, "
Romain Guyac670c02010-07-27 17:39:27 -0700165 "src.a + dst.a - 2.0 * src.a * dst.a);\n",
166};
167
168///////////////////////////////////////////////////////////////////////////////
169// Constructors/destructors
170///////////////////////////////////////////////////////////////////////////////
171
172ProgramCache::ProgramCache() {
173}
174
175ProgramCache::~ProgramCache() {
176 clear();
177}
178
179///////////////////////////////////////////////////////////////////////////////
180// Cache management
181///////////////////////////////////////////////////////////////////////////////
182
183void ProgramCache::clear() {
184 size_t count = mCache.size();
185 for (size_t i = 0; i < count; i++) {
186 delete mCache.valueAt(i);
187 }
188 mCache.clear();
189}
190
191Program* ProgramCache::get(const ProgramDescription& description) {
192 programid key = description.key();
193 ssize_t index = mCache.indexOfKey(key);
194 Program* program = NULL;
195 if (index < 0) {
196 PROGRAM_LOGD("Could not find program with key 0x%x", key);
197 program = generateProgram(description, key);
198 mCache.add(key, program);
199 } else {
200 program = mCache.valueAt(index);
201 }
202 return program;
203}
204
205///////////////////////////////////////////////////////////////////////////////
206// Program generation
207///////////////////////////////////////////////////////////////////////////////
208
209Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) {
210 String8 vertexShader = generateVertexShader(description);
211 String8 fragmentShader = generateFragmentShader(description);
212
213 Program* program = new Program(vertexShader.string(), fragmentShader.string());
214 return program;
215}
216
217String8 ProgramCache::generateVertexShader(const ProgramDescription& description) {
218 // Add attributes
219 String8 shader(gVS_Header_Attributes);
Romain Guy889f8d12010-07-29 14:37:42 -0700220 if (description.hasTexture) {
Romain Guyac670c02010-07-27 17:39:27 -0700221 shader.append(gVS_Header_Attributes_TexCoords);
222 }
223 // Uniforms
224 shader.append(gVS_Header_Uniforms);
225 if (description.hasGradient) {
226 shader.append(gVS_Header_Uniforms_HasGradient);
227 }
Romain Guy889f8d12010-07-29 14:37:42 -0700228 if (description.hasBitmap) {
229 shader.append(gVS_Header_Uniforms_HasBitmap);
230 }
Romain Guyac670c02010-07-27 17:39:27 -0700231 // Varyings
232 if (description.hasTexture) {
233 shader.append(gVS_Header_Varyings_HasTexture);
234 }
235 if (description.hasGradient) {
236 shader.append(gVS_Header_Varyings_HasGradient);
237 }
238 if (description.hasBitmap) {
239 shader.append(gVS_Header_Varyings_HasBitmap);
240 }
241
242 // Begin the shader
243 shader.append(gVS_Main); {
244 if (description.hasTexture) {
245 shader.append(gVS_Main_OutTexCoords);
246 }
247 if (description.hasGradient) {
248 shader.append(gVS_Main_OutGradientIndex);
249 }
Romain Guy889f8d12010-07-29 14:37:42 -0700250 if (description.hasBitmap) {
251 shader.append(gVS_Main_OutBitmapTexCoords);
252 }
Romain Guyac670c02010-07-27 17:39:27 -0700253 // Output transformed position
254 shader.append(gVS_Main_Position);
255 }
256 // End the shader
257 shader.append(gVS_Footer);
258
259 PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string());
260
261 return shader;
262}
263
264String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) {
265 // Set the default precision
266 String8 shader(gFS_Header);
267
268 // Varyings
269 if (description.hasTexture) {
270 shader.append(gVS_Header_Varyings_HasTexture);
271 }
272 if (description.hasGradient) {
273 shader.append(gVS_Header_Varyings_HasGradient);
274 }
275 if (description.hasBitmap) {
276 shader.append(gVS_Header_Varyings_HasBitmap);
277 }
278
279
280 // Uniforms
281 shader.append(gFS_Uniforms_Color);
282 if (description.hasTexture) {
283 shader.append(gFS_Uniforms_TextureSampler);
284 }
285 if (description.hasGradient) {
286 shader.append(gFS_Uniforms_GradientSampler);
287 }
288 if (description.hasBitmap) {
289 shader.append(gFS_Uniforms_BitmapSampler);
290 }
291 shader.append(gFS_Uniforms_ColorOp[description.colorOp]);
292
293 // Generate required functions
294 if (description.hasGradient && description.hasBitmap) {
295 generatePorterDuffBlend(shader, "blendShaders", description.shadersMode);
296 }
297 if (description.colorOp == ProgramDescription::kColorBlend) {
298 generatePorterDuffBlend(shader, "blendColors", description.colorMode);
299 }
Romain Guy889f8d12010-07-29 14:37:42 -0700300 if (description.isBitmapNpot) {
301 generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT);
302 }
Romain Guyac670c02010-07-27 17:39:27 -0700303
304 // Begin the shader
305 shader.append(gFS_Main); {
306 // Stores the result in fragColor directly
307 if (description.hasTexture) {
308 if (description.hasAlpha8Texture) {
309 shader.append(gFS_Main_FetchA8Texture);
310 } else {
311 shader.append(gFS_Main_FetchTexture);
312 }
313 } else {
314 shader.append(gFS_Main_FetchColor);
315 }
316 if (description.hasGradient) {
317 shader.append(gFS_Main_FetchGradient);
318 }
319 if (description.hasBitmap) {
Romain Guy889f8d12010-07-29 14:37:42 -0700320 if (!description.isBitmapNpot) {
321 shader.append(gFS_Main_FetchBitmap);
322 } else {
323 shader.append(gFS_Main_FetchBitmapNpot);
324 }
Romain Guyac670c02010-07-27 17:39:27 -0700325 }
326 // Case when we have two shaders set
327 if (description.hasGradient && description.hasBitmap) {
328 if (description.isBitmapFirst) {
329 shader.append(gFS_Main_BlendShadersBG);
330 } else {
331 shader.append(gFS_Main_BlendShadersGB);
332 }
333 shader.append(gFS_Main_BlendShaders_Modulate);
Romain Guy889f8d12010-07-29 14:37:42 -0700334 } else {
335 if (description.hasGradient) {
336 shader.append(gFS_Main_GradientShader_Modulate);
337 } else if (description.hasBitmap) {
338 shader.append(gFS_Main_BitmapShader_Modulate);
339 }
Romain Guyac670c02010-07-27 17:39:27 -0700340 }
341 // Apply the color op if needed
342 shader.append(gFS_Main_ApplyColorOp[description.colorOp]);
343 // Output the fragment
344 shader.append(gFS_Main_FragColor);
345 }
346 // End the shader
347 shader.append(gFS_Footer);
348
Romain Guydb1938e2010-08-02 18:50:22 -0700349 if (DEBUG_PROGRAM_CACHE) {
350 PROGRAM_LOGD("*** Generated fragment shader:\n\n");
351 printLongString(shader);
352 }
353
Romain Guyac670c02010-07-27 17:39:27 -0700354 return shader;
355}
356
357void ProgramCache::generatePorterDuffBlend(String8& shader, const char* name,
358 SkXfermode::Mode mode) {
359 shader.append("\nvec4 ");
360 shader.append(name);
361 shader.append("(vec4 src, vec4 dst) {\n");
362 shader.append(" ");
363 shader.append(gPorterDuff[mode]);
364 shader.append("}\n");
365}
366
Romain Guy889f8d12010-07-29 14:37:42 -0700367void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) {
368 shader.append("\nvec2 wrap(vec2 texCoords) {\n");
369 if (wrapS == GL_MIRRORED_REPEAT) {
370 shader.append(" float xMod2 = mod(texCoords.x, 2.0);\n");
371 shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n");
372 }
373 if (wrapT == GL_MIRRORED_REPEAT) {
374 shader.append(" float yMod2 = mod(texCoords.y, 2.0);\n");
375 shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n");
376 }
377 shader.append(" return vec2(");
378 switch (wrapS) {
379 case GL_REPEAT:
380 shader.append("mod(texCoords.x, 1.0)");
381 break;
382 case GL_MIRRORED_REPEAT:
383 shader.append("xMod2");
384 break;
385 }
386 shader.append(", ");
387 switch (wrapT) {
388 case GL_REPEAT:
389 shader.append("mod(texCoords.y, 1.0)");
390 break;
391 case GL_MIRRORED_REPEAT:
392 shader.append("yMod2");
393 break;
394 }
395 shader.append(");\n");
396 shader.append("}\n");
397}
398
Romain Guydb1938e2010-08-02 18:50:22 -0700399void ProgramCache::printLongString(const String8& shader) const {
400 ssize_t index = 0;
401 ssize_t lastIndex = 0;
402 const char* str = shader.string();
403 while ((index = shader.find("\n", index)) > -1) {
404 String8 line(str, index - lastIndex);
405 if (line.length() == 0) line.append("\n");
406 PROGRAM_LOGD("%s", line.string());
407 index++;
408 str += (index - lastIndex);
409 lastIndex = index;
410 }
411}
412
Romain Guyac670c02010-07-27 17:39:27 -0700413}; // namespace uirenderer
414}; // namespace android