Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 23 | namespace android { |
| 24 | namespace uirenderer { |
| 25 | |
| 26 | /////////////////////////////////////////////////////////////////////////////// |
| 27 | // Vertex shaders snippets |
| 28 | /////////////////////////////////////////////////////////////////////////////// |
| 29 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 30 | const char* gVS_Header_Attributes = |
| 31 | "attribute vec4 position;\n"; |
| 32 | const char* gVS_Header_Attributes_TexCoords = |
| 33 | "attribute vec2 texCoords;\n"; |
| 34 | const char* gVS_Header_Uniforms = |
| 35 | "uniform mat4 transform;\n"; |
| 36 | const 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 Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 41 | const char* gVS_Header_Uniforms_HasBitmap = |
| 42 | "uniform mat4 textureTransform;\n" |
| 43 | "uniform vec2 textureDimension;\n"; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 44 | const char* gVS_Header_Varyings_HasTexture = |
| 45 | "varying vec2 outTexCoords;\n"; |
| 46 | const char* gVS_Header_Varyings_HasBitmap = |
| 47 | "varying vec2 outBitmapTexCoords;\n"; |
| 48 | const char* gVS_Header_Varyings_HasGradient = |
| 49 | "varying float index;\n"; |
| 50 | const char* gVS_Main = |
| 51 | "\nvoid main(void) {\n"; |
| 52 | const char* gVS_Main_OutTexCoords = |
| 53 | " outTexCoords = texCoords;\n"; |
| 54 | const char* gVS_Main_OutGradientIndex = |
| 55 | " vec4 location = screenSpace * position;\n" |
| 56 | " index = dot(location.xy - gradientStart, gradient) * gradientLength;\n"; |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 57 | const char* gVS_Main_OutBitmapTexCoords = |
| 58 | " vec4 bitmapCoords = textureTransform * position;\n" |
| 59 | " outBitmapTexCoords = bitmapCoords.xy * textureDimension;\n"; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 60 | const char* gVS_Main_Position = |
| 61 | " gl_Position = transform * position;\n"; |
| 62 | const char* gVS_Footer = |
| 63 | "}\n\n"; |
| 64 | |
| 65 | /////////////////////////////////////////////////////////////////////////////// |
| 66 | // Fragment shaders snippets |
| 67 | /////////////////////////////////////////////////////////////////////////////// |
| 68 | |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 69 | const char* gFS_Header_Extension_FramebufferFetch = |
| 70 | "#extension GL_NV_shader_framebuffer_fetch : enable\n\n"; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 71 | const char* gFS_Header = |
| 72 | "precision mediump float;\n\n"; |
| 73 | const char* gFS_Uniforms_Color = |
| 74 | "uniform vec4 color;\n"; |
| 75 | const char* gFS_Uniforms_TextureSampler = |
| 76 | "uniform sampler2D sampler;\n"; |
| 77 | const char* gFS_Uniforms_GradientSampler = |
| 78 | "uniform sampler2D gradientSampler;\n"; |
| 79 | const char* gFS_Uniforms_BitmapSampler = |
| 80 | "uniform sampler2D bitmapSampler;\n"; |
| 81 | const char* gFS_Uniforms_ColorOp[4] = { |
| 82 | // None |
| 83 | "", |
| 84 | // Matrix |
| 85 | "uniform mat4 colorMatrix;\n" |
| 86 | "uniform vec4 colorMatrixVector;\n", |
| 87 | // Lighting |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 88 | "uniform vec4 lightingMul;\n" |
| 89 | "uniform vec4 lightingAdd;\n", |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 90 | // PorterDuff |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 91 | "uniform vec4 colorBlend;\n" |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 92 | }; |
| 93 | const char* gFS_Main = |
| 94 | "\nvoid main(void) {\n" |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 95 | " lowp vec4 fragColor;\n"; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 96 | const char* gFS_Main_FetchColor = |
| 97 | " fragColor = color;\n"; |
| 98 | const char* gFS_Main_FetchTexture = |
| 99 | " fragColor = color * texture2D(sampler, outTexCoords);\n"; |
| 100 | const char* gFS_Main_FetchA8Texture = |
| 101 | " fragColor = color * texture2D(sampler, outTexCoords).a;\n"; |
| 102 | const char* gFS_Main_FetchGradient = |
| 103 | " vec4 gradientColor = texture2D(gradientSampler, vec2(index, 0.5));\n"; |
| 104 | const char* gFS_Main_FetchBitmap = |
| 105 | " vec4 bitmapColor = texture2D(bitmapSampler, outBitmapTexCoords);\n"; |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 106 | const char* gFS_Main_FetchBitmapNpot = |
| 107 | " vec4 bitmapColor = texture2D(bitmapSampler, wrap(outBitmapTexCoords));\n"; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 108 | const char* gFS_Main_BlendShadersBG = |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 109 | " fragColor = blendShaders(gradientColor, bitmapColor)"; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 110 | const char* gFS_Main_BlendShadersGB = |
| 111 | " fragColor = blendShaders(bitmapColor, gradientColor)"; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 112 | const char* gFS_Main_BlendShaders_Modulate = |
| 113 | " * fragColor.a;\n"; |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 114 | const char* gFS_Main_GradientShader_Modulate = |
| 115 | " fragColor = gradientColor * fragColor.a;\n"; |
| 116 | const char* gFS_Main_BitmapShader_Modulate = |
| 117 | " fragColor = bitmapColor * fragColor.a;\n"; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 118 | const char* gFS_Main_FragColor = |
| 119 | " gl_FragColor = fragColor;\n"; |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 120 | const char* gFS_Main_FragColor_Blend = |
| 121 | " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n"; |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 122 | const char* gFS_Main_FragColor_Blend_Swap = |
| 123 | " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n"; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 124 | const char* gFS_Main_ApplyColorOp[4] = { |
| 125 | // None |
| 126 | "", |
| 127 | // Matrix |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 128 | // TODO: Fix premultiplied alpha computations for color matrix |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 129 | " fragColor *= colorMatrix;\n" |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 130 | " fragColor += colorMatrixVector;\n" |
| 131 | " fragColor.rgb *= fragColor.a;\n", |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 132 | // Lighting |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 133 | " float lightingAlpha = fragColor.a;\n" |
| 134 | " fragColor = min(fragColor * lightingMul + (lightingAdd * lightingAlpha), lightingAlpha);\n" |
| 135 | " fragColor.a = lightingAlpha;\n", |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 136 | // PorterDuff |
| 137 | " fragColor = blendColors(colorBlend, fragColor);\n" |
| 138 | }; |
| 139 | const char* gFS_Footer = |
| 140 | "}\n\n"; |
| 141 | |
| 142 | /////////////////////////////////////////////////////////////////////////////// |
| 143 | // PorterDuff snippets |
| 144 | /////////////////////////////////////////////////////////////////////////////// |
| 145 | |
Romain Guy | 48daa54 | 2010-08-10 19:21:34 -0700 | [diff] [blame] | 146 | const char* gBlendOps[18] = { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 147 | // Clear |
| 148 | "return vec4(0.0, 0.0, 0.0, 0.0);\n", |
| 149 | // Src |
| 150 | "return src;\n", |
| 151 | // Dst |
| 152 | "return dst;\n", |
| 153 | // SrcOver |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 154 | "return src + dst * (1.0 - src.a);\n", |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 155 | // DstOver |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 156 | "return dst + src * (1.0 - dst.a);\n", |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 157 | // SrcIn |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 158 | "return src * dst.a;\n", |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 159 | // DstIn |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 160 | "return dst * src.a;\n", |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 161 | // SrcOut |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 162 | "return src * (1.0 - dst.a);\n", |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 163 | // DstOut |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 164 | "return dst * (1.0 - src.a);\n", |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 165 | // SrcAtop |
| 166 | "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n", |
| 167 | // DstAtop |
| 168 | "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n", |
| 169 | // Xor |
Romain Guy | 48daa54 | 2010-08-10 19:21:34 -0700 | [diff] [blame] | 170 | "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, " |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 171 | "src.a + dst.a - 2.0 * src.a * dst.a);\n", |
Romain Guy | 48daa54 | 2010-08-10 19:21:34 -0700 | [diff] [blame] | 172 | // Add |
| 173 | "return min(src + dst, 1.0);\n", |
| 174 | // Multiply |
| 175 | "return src * dst;\n", |
| 176 | // Screen |
| 177 | "return src + dst - src * dst;\n", |
| 178 | // Overlay |
| 179 | "return clamp(vec4(mix(" |
| 180 | "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), " |
| 181 | "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), " |
| 182 | "step(dst.a, 2.0 * dst.rgb)), " |
| 183 | "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n", |
| 184 | // Darken |
| 185 | "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + " |
| 186 | "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n", |
| 187 | // Lighten |
| 188 | "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + " |
| 189 | "max(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n", |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | /////////////////////////////////////////////////////////////////////////////// |
| 193 | // Constructors/destructors |
| 194 | /////////////////////////////////////////////////////////////////////////////// |
| 195 | |
| 196 | ProgramCache::ProgramCache() { |
| 197 | } |
| 198 | |
| 199 | ProgramCache::~ProgramCache() { |
| 200 | clear(); |
| 201 | } |
| 202 | |
| 203 | /////////////////////////////////////////////////////////////////////////////// |
| 204 | // Cache management |
| 205 | /////////////////////////////////////////////////////////////////////////////// |
| 206 | |
| 207 | void ProgramCache::clear() { |
| 208 | size_t count = mCache.size(); |
| 209 | for (size_t i = 0; i < count; i++) { |
| 210 | delete mCache.valueAt(i); |
| 211 | } |
| 212 | mCache.clear(); |
| 213 | } |
| 214 | |
| 215 | Program* ProgramCache::get(const ProgramDescription& description) { |
| 216 | programid key = description.key(); |
| 217 | ssize_t index = mCache.indexOfKey(key); |
| 218 | Program* program = NULL; |
| 219 | if (index < 0) { |
| 220 | PROGRAM_LOGD("Could not find program with key 0x%x", key); |
| 221 | program = generateProgram(description, key); |
| 222 | mCache.add(key, program); |
| 223 | } else { |
| 224 | program = mCache.valueAt(index); |
| 225 | } |
| 226 | return program; |
| 227 | } |
| 228 | |
| 229 | /////////////////////////////////////////////////////////////////////////////// |
| 230 | // Program generation |
| 231 | /////////////////////////////////////////////////////////////////////////////// |
| 232 | |
| 233 | Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) { |
| 234 | String8 vertexShader = generateVertexShader(description); |
| 235 | String8 fragmentShader = generateFragmentShader(description); |
| 236 | |
| 237 | Program* program = new Program(vertexShader.string(), fragmentShader.string()); |
| 238 | return program; |
| 239 | } |
| 240 | |
| 241 | String8 ProgramCache::generateVertexShader(const ProgramDescription& description) { |
| 242 | // Add attributes |
| 243 | String8 shader(gVS_Header_Attributes); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 244 | if (description.hasTexture) { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 245 | shader.append(gVS_Header_Attributes_TexCoords); |
| 246 | } |
| 247 | // Uniforms |
| 248 | shader.append(gVS_Header_Uniforms); |
| 249 | if (description.hasGradient) { |
| 250 | shader.append(gVS_Header_Uniforms_HasGradient); |
| 251 | } |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 252 | if (description.hasBitmap) { |
| 253 | shader.append(gVS_Header_Uniforms_HasBitmap); |
| 254 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 255 | // Varyings |
| 256 | if (description.hasTexture) { |
| 257 | shader.append(gVS_Header_Varyings_HasTexture); |
| 258 | } |
| 259 | if (description.hasGradient) { |
| 260 | shader.append(gVS_Header_Varyings_HasGradient); |
| 261 | } |
| 262 | if (description.hasBitmap) { |
| 263 | shader.append(gVS_Header_Varyings_HasBitmap); |
| 264 | } |
| 265 | |
| 266 | // Begin the shader |
| 267 | shader.append(gVS_Main); { |
| 268 | if (description.hasTexture) { |
| 269 | shader.append(gVS_Main_OutTexCoords); |
| 270 | } |
| 271 | if (description.hasGradient) { |
| 272 | shader.append(gVS_Main_OutGradientIndex); |
| 273 | } |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 274 | if (description.hasBitmap) { |
| 275 | shader.append(gVS_Main_OutBitmapTexCoords); |
| 276 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 277 | // Output transformed position |
| 278 | shader.append(gVS_Main_Position); |
| 279 | } |
| 280 | // End the shader |
| 281 | shader.append(gVS_Footer); |
| 282 | |
| 283 | PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string()); |
| 284 | |
| 285 | return shader; |
| 286 | } |
| 287 | |
| 288 | String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) { |
| 289 | // Set the default precision |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 290 | String8 shader; |
| 291 | |
| 292 | bool blendFramebuffer = description.framebufferMode >= SkXfermode::kPlus_Mode; |
| 293 | if (blendFramebuffer) { |
| 294 | shader.append(gFS_Header_Extension_FramebufferFetch); |
| 295 | } |
| 296 | |
| 297 | shader.append(gFS_Header); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 298 | |
| 299 | // Varyings |
| 300 | if (description.hasTexture) { |
| 301 | shader.append(gVS_Header_Varyings_HasTexture); |
| 302 | } |
| 303 | if (description.hasGradient) { |
| 304 | shader.append(gVS_Header_Varyings_HasGradient); |
| 305 | } |
| 306 | if (description.hasBitmap) { |
| 307 | shader.append(gVS_Header_Varyings_HasBitmap); |
| 308 | } |
| 309 | |
| 310 | |
| 311 | // Uniforms |
| 312 | shader.append(gFS_Uniforms_Color); |
| 313 | if (description.hasTexture) { |
| 314 | shader.append(gFS_Uniforms_TextureSampler); |
| 315 | } |
| 316 | if (description.hasGradient) { |
| 317 | shader.append(gFS_Uniforms_GradientSampler); |
| 318 | } |
| 319 | if (description.hasBitmap) { |
| 320 | shader.append(gFS_Uniforms_BitmapSampler); |
| 321 | } |
| 322 | shader.append(gFS_Uniforms_ColorOp[description.colorOp]); |
| 323 | |
| 324 | // Generate required functions |
| 325 | if (description.hasGradient && description.hasBitmap) { |
Romain Guy | 48daa54 | 2010-08-10 19:21:34 -0700 | [diff] [blame] | 326 | generateBlend(shader, "blendShaders", description.shadersMode); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 327 | } |
| 328 | if (description.colorOp == ProgramDescription::kColorBlend) { |
Romain Guy | 48daa54 | 2010-08-10 19:21:34 -0700 | [diff] [blame] | 329 | generateBlend(shader, "blendColors", description.colorMode); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 330 | } |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 331 | if (blendFramebuffer) { |
| 332 | generateBlend(shader, "blendFramebuffer", description.framebufferMode); |
| 333 | } |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 334 | if (description.isBitmapNpot) { |
| 335 | generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT); |
| 336 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 337 | |
| 338 | // Begin the shader |
| 339 | shader.append(gFS_Main); { |
| 340 | // Stores the result in fragColor directly |
| 341 | if (description.hasTexture) { |
| 342 | if (description.hasAlpha8Texture) { |
| 343 | shader.append(gFS_Main_FetchA8Texture); |
| 344 | } else { |
| 345 | shader.append(gFS_Main_FetchTexture); |
| 346 | } |
| 347 | } else { |
| 348 | shader.append(gFS_Main_FetchColor); |
| 349 | } |
| 350 | if (description.hasGradient) { |
| 351 | shader.append(gFS_Main_FetchGradient); |
| 352 | } |
| 353 | if (description.hasBitmap) { |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 354 | if (!description.isBitmapNpot) { |
| 355 | shader.append(gFS_Main_FetchBitmap); |
| 356 | } else { |
| 357 | shader.append(gFS_Main_FetchBitmapNpot); |
| 358 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 359 | } |
| 360 | // Case when we have two shaders set |
| 361 | if (description.hasGradient && description.hasBitmap) { |
| 362 | if (description.isBitmapFirst) { |
| 363 | shader.append(gFS_Main_BlendShadersBG); |
| 364 | } else { |
| 365 | shader.append(gFS_Main_BlendShadersGB); |
| 366 | } |
| 367 | shader.append(gFS_Main_BlendShaders_Modulate); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 368 | } else { |
| 369 | if (description.hasGradient) { |
| 370 | shader.append(gFS_Main_GradientShader_Modulate); |
| 371 | } else if (description.hasBitmap) { |
| 372 | shader.append(gFS_Main_BitmapShader_Modulate); |
| 373 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 374 | } |
| 375 | // Apply the color op if needed |
| 376 | shader.append(gFS_Main_ApplyColorOp[description.colorOp]); |
| 377 | // Output the fragment |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 378 | if (!blendFramebuffer) { |
| 379 | shader.append(gFS_Main_FragColor); |
| 380 | } else { |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 381 | shader.append(!description.swapSrcDst ? |
| 382 | gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap); |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 383 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 384 | } |
| 385 | // End the shader |
| 386 | shader.append(gFS_Footer); |
| 387 | |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 388 | if (DEBUG_PROGRAM_CACHE) { |
| 389 | PROGRAM_LOGD("*** Generated fragment shader:\n\n"); |
| 390 | printLongString(shader); |
| 391 | } |
| 392 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 393 | return shader; |
| 394 | } |
| 395 | |
Romain Guy | 48daa54 | 2010-08-10 19:21:34 -0700 | [diff] [blame] | 396 | void ProgramCache::generateBlend(String8& shader, const char* name, SkXfermode::Mode mode) { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 397 | shader.append("\nvec4 "); |
| 398 | shader.append(name); |
| 399 | shader.append("(vec4 src, vec4 dst) {\n"); |
| 400 | shader.append(" "); |
Romain Guy | 48daa54 | 2010-08-10 19:21:34 -0700 | [diff] [blame] | 401 | shader.append(gBlendOps[mode]); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 402 | shader.append("}\n"); |
| 403 | } |
| 404 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 405 | void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) { |
| 406 | shader.append("\nvec2 wrap(vec2 texCoords) {\n"); |
| 407 | if (wrapS == GL_MIRRORED_REPEAT) { |
| 408 | shader.append(" float xMod2 = mod(texCoords.x, 2.0);\n"); |
| 409 | shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n"); |
| 410 | } |
| 411 | if (wrapT == GL_MIRRORED_REPEAT) { |
| 412 | shader.append(" float yMod2 = mod(texCoords.y, 2.0);\n"); |
| 413 | shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n"); |
| 414 | } |
| 415 | shader.append(" return vec2("); |
| 416 | switch (wrapS) { |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 417 | case GL_CLAMP_TO_EDGE: |
| 418 | shader.append("texCoords.x"); |
| 419 | break; |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 420 | case GL_REPEAT: |
| 421 | shader.append("mod(texCoords.x, 1.0)"); |
| 422 | break; |
| 423 | case GL_MIRRORED_REPEAT: |
| 424 | shader.append("xMod2"); |
| 425 | break; |
| 426 | } |
| 427 | shader.append(", "); |
| 428 | switch (wrapT) { |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 429 | case GL_CLAMP_TO_EDGE: |
| 430 | shader.append("texCoords.y"); |
| 431 | break; |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 432 | case GL_REPEAT: |
| 433 | shader.append("mod(texCoords.y, 1.0)"); |
| 434 | break; |
| 435 | case GL_MIRRORED_REPEAT: |
| 436 | shader.append("yMod2"); |
| 437 | break; |
| 438 | } |
| 439 | shader.append(");\n"); |
| 440 | shader.append("}\n"); |
| 441 | } |
| 442 | |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 443 | void ProgramCache::printLongString(const String8& shader) const { |
| 444 | ssize_t index = 0; |
| 445 | ssize_t lastIndex = 0; |
| 446 | const char* str = shader.string(); |
| 447 | while ((index = shader.find("\n", index)) > -1) { |
| 448 | String8 line(str, index - lastIndex); |
| 449 | if (line.length() == 0) line.append("\n"); |
| 450 | PROGRAM_LOGD("%s", line.string()); |
| 451 | index++; |
| 452 | str += (index - lastIndex); |
| 453 | lastIndex = index; |
| 454 | } |
| 455 | } |
| 456 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 457 | }; // namespace uirenderer |
| 458 | }; // namespace android |