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