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 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_HWUI_PROGRAM_CACHE_H |
| 18 | #define ANDROID_HWUI_PROGRAM_CACHE_H |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 19 | |
| 20 | #include <utils/KeyedVector.h> |
| 21 | #include <utils/Log.h> |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 22 | #include <utils/String8.h> |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 23 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 24 | #include <GLES2/gl2.h> |
| 25 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 26 | #include <SkXfermode.h> |
| 27 | |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 28 | #include "Debug.h" |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 29 | #include "Program.h" |
| 30 | |
| 31 | namespace android { |
| 32 | namespace uirenderer { |
| 33 | |
| 34 | /////////////////////////////////////////////////////////////////////////////// |
| 35 | // Defines |
| 36 | /////////////////////////////////////////////////////////////////////////////// |
| 37 | |
| 38 | // Debug |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 39 | #if DEBUG_PROGRAMS |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 40 | #define PROGRAM_LOGD(...) LOGD(__VA_ARGS__) |
| 41 | #else |
| 42 | #define PROGRAM_LOGD(...) |
| 43 | #endif |
| 44 | |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 45 | // TODO: This should be set in properties |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 46 | #define PANEL_BIT_DEPTH 20 |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 47 | #define COLOR_COMPONENT_THRESHOLD (1.0f - (0.5f / PANEL_BIT_DEPTH)) |
| 48 | #define COLOR_COMPONENT_INV_THRESHOLD (0.5f / PANEL_BIT_DEPTH) |
| 49 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 50 | #define PROGRAM_KEY_TEXTURE 0x1 |
| 51 | #define PROGRAM_KEY_A8_TEXTURE 0x2 |
| 52 | #define PROGRAM_KEY_BITMAP 0x4 |
| 53 | #define PROGRAM_KEY_GRADIENT 0x8 |
| 54 | #define PROGRAM_KEY_BITMAP_FIRST 0x10 |
| 55 | #define PROGRAM_KEY_COLOR_MATRIX 0x20 |
| 56 | #define PROGRAM_KEY_COLOR_LIGHTING 0x40 |
| 57 | #define PROGRAM_KEY_COLOR_BLEND 0x80 |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 58 | #define PROGRAM_KEY_BITMAP_NPOT 0x100 |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 59 | #define PROGRAM_KEY_SWAP_SRC_DST 0x2000 |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 60 | |
| 61 | #define PROGRAM_KEY_BITMAP_WRAPS_MASK 0x600 |
| 62 | #define PROGRAM_KEY_BITMAP_WRAPT_MASK 0x1800 |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 63 | |
Romain Guy | 48daa54 | 2010-08-10 19:21:34 -0700 | [diff] [blame] | 64 | // Encode the xfermodes on 6 bits |
| 65 | #define PROGRAM_MAX_XFERMODE 0x1f |
| 66 | #define PROGRAM_XFERMODE_SHADER_SHIFT 26 |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 67 | #define PROGRAM_XFERMODE_COLOR_OP_SHIFT 20 |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 68 | #define PROGRAM_XFERMODE_FRAMEBUFFER_SHIFT 14 |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 69 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 70 | #define PROGRAM_BITMAP_WRAPS_SHIFT 9 |
| 71 | #define PROGRAM_BITMAP_WRAPT_SHIFT 11 |
| 72 | |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 73 | #define PROGRAM_GRADIENT_TYPE_SHIFT 33 |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 74 | #define PROGRAM_MODULATE 35 |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 75 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 76 | /////////////////////////////////////////////////////////////////////////////// |
| 77 | // Types |
| 78 | /////////////////////////////////////////////////////////////////////////////// |
| 79 | |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 80 | typedef uint64_t programid; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 81 | |
| 82 | /////////////////////////////////////////////////////////////////////////////// |
| 83 | // Cache |
| 84 | /////////////////////////////////////////////////////////////////////////////// |
| 85 | |
| 86 | /** |
| 87 | * Describe the features required for a given program. The features |
| 88 | * determine the generation of both the vertex and fragment shaders. |
| 89 | * A ProgramDescription must be used in conjunction with a ProgramCache. |
| 90 | */ |
| 91 | struct ProgramDescription { |
| 92 | enum ColorModifier { |
| 93 | kColorNone, |
| 94 | kColorMatrix, |
| 95 | kColorLighting, |
| 96 | kColorBlend |
| 97 | }; |
| 98 | |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 99 | enum Gradient { |
| 100 | kGradientLinear, |
| 101 | kGradientCircular, |
| 102 | kGradientSweep |
| 103 | }; |
| 104 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 105 | ProgramDescription() { |
| 106 | reset(); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // Texturing |
| 110 | bool hasTexture; |
| 111 | bool hasAlpha8Texture; |
| 112 | |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 113 | // Modulate, this should only be set when setColor() return true |
| 114 | bool modulate; |
| 115 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 116 | // Shaders |
| 117 | bool hasBitmap; |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 118 | bool isBitmapNpot; |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 119 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 120 | bool hasGradient; |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 121 | Gradient gradientType; |
| 122 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 123 | SkXfermode::Mode shadersMode; |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 124 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 125 | bool isBitmapFirst; |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 126 | GLenum bitmapWrapS; |
| 127 | GLenum bitmapWrapT; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 128 | |
| 129 | // Color operations |
Romain Guy | 4afdf66 | 2010-10-13 21:31:28 -0700 | [diff] [blame] | 130 | ColorModifier colorOp; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 131 | SkXfermode::Mode colorMode; |
| 132 | |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 133 | // Framebuffer blending (requires Extensions.hasFramebufferFetch()) |
| 134 | // Ignored for all values < SkXfermode::kPlus_Mode |
| 135 | SkXfermode::Mode framebufferMode; |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 136 | bool swapSrcDst; |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 137 | |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 138 | /** |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 139 | * Resets this description. All fields are reset back to the default |
| 140 | * values they hold after building a new instance. |
| 141 | */ |
| 142 | void reset() { |
| 143 | hasTexture = false; |
| 144 | hasAlpha8Texture = false; |
| 145 | |
| 146 | modulate = false; |
| 147 | |
| 148 | hasBitmap = false; |
| 149 | isBitmapNpot = false; |
| 150 | |
| 151 | hasGradient = false; |
| 152 | gradientType = kGradientLinear; |
| 153 | |
| 154 | shadersMode = SkXfermode::kClear_Mode; |
| 155 | |
| 156 | isBitmapFirst = false; |
| 157 | bitmapWrapS = GL_CLAMP_TO_EDGE; |
| 158 | bitmapWrapT = GL_CLAMP_TO_EDGE; |
| 159 | |
| 160 | colorOp = kColorNone; |
| 161 | colorMode = SkXfermode::kClear_Mode; |
| 162 | |
| 163 | framebufferMode = SkXfermode::kClear_Mode; |
| 164 | swapSrcDst = false; |
| 165 | } |
| 166 | |
| 167 | /** |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 168 | * Indicates, for a given color, whether color modulation is required in |
| 169 | * the fragment shader. When this method returns true, the program should |
| 170 | * be provided with a modulation color. |
| 171 | */ |
| 172 | bool setColor(const float r, const float g, const float b, const float a) { |
| 173 | modulate = a < COLOR_COMPONENT_THRESHOLD || r < COLOR_COMPONENT_THRESHOLD || |
| 174 | g < COLOR_COMPONENT_THRESHOLD || b < COLOR_COMPONENT_THRESHOLD; |
| 175 | return modulate; |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 178 | /** |
| 179 | * Indicates, for a given color, whether color modulation is required in |
| 180 | * the fragment shader. When this method returns true, the program should |
| 181 | * be provided with a modulation color. |
| 182 | */ |
| 183 | bool setAlpha8Color(const float r, const float g, const float b, const float a) { |
| 184 | modulate = a < COLOR_COMPONENT_THRESHOLD || r > COLOR_COMPONENT_INV_THRESHOLD || |
| 185 | g > COLOR_COMPONENT_INV_THRESHOLD || b > COLOR_COMPONENT_INV_THRESHOLD; |
| 186 | return modulate; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Computes the unique key identifying this program. |
| 191 | */ |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 192 | programid key() const { |
| 193 | programid key = 0; |
| 194 | if (hasTexture) key |= PROGRAM_KEY_TEXTURE; |
| 195 | if (hasAlpha8Texture) key |= PROGRAM_KEY_A8_TEXTURE; |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 196 | if (hasBitmap) { |
| 197 | key |= PROGRAM_KEY_BITMAP; |
| 198 | if (isBitmapNpot) { |
| 199 | key |= PROGRAM_KEY_BITMAP_NPOT; |
| 200 | key |= getEnumForWrap(bitmapWrapS) << PROGRAM_BITMAP_WRAPS_SHIFT; |
| 201 | key |= getEnumForWrap(bitmapWrapT) << PROGRAM_BITMAP_WRAPT_SHIFT; |
| 202 | } |
| 203 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 204 | if (hasGradient) key |= PROGRAM_KEY_GRADIENT; |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 205 | key |= programid(gradientType) << PROGRAM_GRADIENT_TYPE_SHIFT; |
| 206 | if (isBitmapFirst) key |= PROGRAM_KEY_BITMAP_FIRST; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 207 | if (hasBitmap && hasGradient) { |
| 208 | key |= (shadersMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_SHADER_SHIFT; |
| 209 | } |
| 210 | switch (colorOp) { |
| 211 | case kColorMatrix: |
| 212 | key |= PROGRAM_KEY_COLOR_MATRIX; |
| 213 | break; |
| 214 | case kColorLighting: |
| 215 | key |= PROGRAM_KEY_COLOR_LIGHTING; |
| 216 | break; |
| 217 | case kColorBlend: |
| 218 | key |= PROGRAM_KEY_COLOR_BLEND; |
| 219 | key |= (colorMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_COLOR_OP_SHIFT; |
| 220 | break; |
| 221 | case kColorNone: |
| 222 | break; |
| 223 | } |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 224 | key |= (framebufferMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_FRAMEBUFFER_SHIFT; |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 225 | if (swapSrcDst) key |= PROGRAM_KEY_SWAP_SRC_DST; |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 226 | if (modulate) key |= programid(0x1) << PROGRAM_MODULATE; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 227 | return key; |
| 228 | } |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 229 | |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 230 | /** |
| 231 | * Logs the specified message followed by the key identifying this program. |
| 232 | */ |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 233 | void log(const char* message) const { |
| 234 | programid k = key(); |
| 235 | PROGRAM_LOGD("%s (key = 0x%.8x%.8x)", message, uint32_t(k >> 32), |
| 236 | uint32_t(k & 0xffffffff)); |
| 237 | } |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 238 | |
| 239 | private: |
| 240 | inline uint32_t getEnumForWrap(GLenum wrap) const { |
| 241 | switch (wrap) { |
| 242 | case GL_CLAMP_TO_EDGE: |
| 243 | return 0; |
| 244 | case GL_REPEAT: |
| 245 | return 1; |
| 246 | case GL_MIRRORED_REPEAT: |
| 247 | return 2; |
| 248 | } |
| 249 | return 0; |
| 250 | } |
| 251 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 252 | }; // struct ProgramDescription |
| 253 | |
| 254 | /** |
| 255 | * Generates and caches program. Programs are generated based on |
| 256 | * ProgramDescriptions. |
| 257 | */ |
| 258 | class ProgramCache { |
| 259 | public: |
| 260 | ProgramCache(); |
| 261 | ~ProgramCache(); |
| 262 | |
| 263 | Program* get(const ProgramDescription& description); |
| 264 | |
| 265 | void clear(); |
| 266 | |
| 267 | private: |
| 268 | Program* generateProgram(const ProgramDescription& description, programid key); |
| 269 | String8 generateVertexShader(const ProgramDescription& description); |
| 270 | String8 generateFragmentShader(const ProgramDescription& description); |
Romain Guy | 48daa54 | 2010-08-10 19:21:34 -0700 | [diff] [blame] | 271 | void generateBlend(String8& shader, const char* name, SkXfermode::Mode mode); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 272 | void generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 273 | |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 274 | void printLongString(const String8& shader) const; |
| 275 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 276 | KeyedVector<programid, Program*> mCache; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 277 | }; // class ProgramCache |
| 278 | |
| 279 | }; // namespace uirenderer |
| 280 | }; // namespace android |
| 281 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 282 | #endif // ANDROID_HWUI_PROGRAM_CACHE_H |