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 | #ifndef ANDROID_UI_PROGRAM_CACHE_H |
| 18 | #define ANDROID_UI_PROGRAM_CACHE_H |
| 19 | |
| 20 | #include <utils/KeyedVector.h> |
| 21 | #include <utils/Log.h> |
| 22 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 23 | #include <GLES2/gl2.h> |
| 24 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 25 | #include <SkXfermode.h> |
| 26 | |
| 27 | #include "Program.h" |
| 28 | |
| 29 | namespace android { |
| 30 | namespace uirenderer { |
| 31 | |
| 32 | /////////////////////////////////////////////////////////////////////////////// |
| 33 | // Defines |
| 34 | /////////////////////////////////////////////////////////////////////////////// |
| 35 | |
| 36 | // Debug |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 37 | #define DEBUG_PROGRAM_CACHE 1 |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 38 | |
| 39 | // Debug |
| 40 | #if DEBUG_PROGRAM_CACHE |
| 41 | #define PROGRAM_LOGD(...) LOGD(__VA_ARGS__) |
| 42 | #else |
| 43 | #define PROGRAM_LOGD(...) |
| 44 | #endif |
| 45 | |
| 46 | #define PROGRAM_KEY_TEXTURE 0x1 |
| 47 | #define PROGRAM_KEY_A8_TEXTURE 0x2 |
| 48 | #define PROGRAM_KEY_BITMAP 0x4 |
| 49 | #define PROGRAM_KEY_GRADIENT 0x8 |
| 50 | #define PROGRAM_KEY_BITMAP_FIRST 0x10 |
| 51 | #define PROGRAM_KEY_COLOR_MATRIX 0x20 |
| 52 | #define PROGRAM_KEY_COLOR_LIGHTING 0x40 |
| 53 | #define PROGRAM_KEY_COLOR_BLEND 0x80 |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 54 | #define PROGRAM_KEY_BITMAP_NPOT 0x100 |
| 55 | |
| 56 | #define PROGRAM_KEY_BITMAP_WRAPS_MASK 0x600 |
| 57 | #define PROGRAM_KEY_BITMAP_WRAPT_MASK 0x1800 |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 58 | |
| 59 | // Support only the 12 Porter-Duff modes for now |
| 60 | #define PROGRAM_MAX_XFERMODE 0xC |
| 61 | #define PROGRAM_XFERMODE_SHADER_SHIFT 24 |
| 62 | #define PROGRAM_XFERMODE_COLOR_OP_SHIFT 20 |
| 63 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 64 | #define PROGRAM_BITMAP_WRAPS_SHIFT 9 |
| 65 | #define PROGRAM_BITMAP_WRAPT_SHIFT 11 |
| 66 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 67 | /////////////////////////////////////////////////////////////////////////////// |
| 68 | // Types |
| 69 | /////////////////////////////////////////////////////////////////////////////// |
| 70 | |
| 71 | typedef uint32_t programid; |
| 72 | |
| 73 | /////////////////////////////////////////////////////////////////////////////// |
| 74 | // Cache |
| 75 | /////////////////////////////////////////////////////////////////////////////// |
| 76 | |
| 77 | /** |
| 78 | * Describe the features required for a given program. The features |
| 79 | * determine the generation of both the vertex and fragment shaders. |
| 80 | * A ProgramDescription must be used in conjunction with a ProgramCache. |
| 81 | */ |
| 82 | struct ProgramDescription { |
| 83 | enum ColorModifier { |
| 84 | kColorNone, |
| 85 | kColorMatrix, |
| 86 | kColorLighting, |
| 87 | kColorBlend |
| 88 | }; |
| 89 | |
| 90 | ProgramDescription(): |
| 91 | hasTexture(false), hasAlpha8Texture(false), |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 92 | hasBitmap(false), isBitmapNpot(false), hasGradient(false), |
| 93 | shadersMode(SkXfermode::kClear_Mode), isBitmapFirst(false), |
| 94 | bitmapWrapS(GL_CLAMP_TO_EDGE), bitmapWrapT(GL_CLAMP_TO_EDGE), |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 95 | colorOp(kColorNone), colorMode(SkXfermode::kClear_Mode) { |
| 96 | } |
| 97 | |
| 98 | // Texturing |
| 99 | bool hasTexture; |
| 100 | bool hasAlpha8Texture; |
| 101 | |
| 102 | // Shaders |
| 103 | bool hasBitmap; |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 104 | bool isBitmapNpot; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 105 | bool hasGradient; |
| 106 | SkXfermode::Mode shadersMode; |
| 107 | bool isBitmapFirst; |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 108 | GLenum bitmapWrapS; |
| 109 | GLenum bitmapWrapT; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 110 | |
| 111 | // Color operations |
| 112 | int colorOp; |
| 113 | SkXfermode::Mode colorMode; |
| 114 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 115 | inline uint32_t getEnumForWrap(GLenum wrap) const { |
| 116 | switch (wrap) { |
| 117 | case GL_CLAMP_TO_EDGE: |
| 118 | return 0; |
| 119 | case GL_REPEAT: |
| 120 | return 1; |
| 121 | case GL_MIRRORED_REPEAT: |
| 122 | return 2; |
| 123 | } |
| 124 | return 0; |
| 125 | } |
| 126 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 127 | programid key() const { |
| 128 | programid key = 0; |
| 129 | if (hasTexture) key |= PROGRAM_KEY_TEXTURE; |
| 130 | if (hasAlpha8Texture) key |= PROGRAM_KEY_A8_TEXTURE; |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 131 | if (hasBitmap) { |
| 132 | key |= PROGRAM_KEY_BITMAP; |
| 133 | if (isBitmapNpot) { |
| 134 | key |= PROGRAM_KEY_BITMAP_NPOT; |
| 135 | key |= getEnumForWrap(bitmapWrapS) << PROGRAM_BITMAP_WRAPS_SHIFT; |
| 136 | key |= getEnumForWrap(bitmapWrapT) << PROGRAM_BITMAP_WRAPT_SHIFT; |
| 137 | } |
| 138 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 139 | if (hasGradient) key |= PROGRAM_KEY_GRADIENT; |
| 140 | if (isBitmapFirst) key |= PROGRAM_KEY_BITMAP_FIRST; |
| 141 | if (hasBitmap && hasGradient) { |
| 142 | key |= (shadersMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_SHADER_SHIFT; |
| 143 | } |
| 144 | switch (colorOp) { |
| 145 | case kColorMatrix: |
| 146 | key |= PROGRAM_KEY_COLOR_MATRIX; |
| 147 | break; |
| 148 | case kColorLighting: |
| 149 | key |= PROGRAM_KEY_COLOR_LIGHTING; |
| 150 | break; |
| 151 | case kColorBlend: |
| 152 | key |= PROGRAM_KEY_COLOR_BLEND; |
| 153 | key |= (colorMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_COLOR_OP_SHIFT; |
| 154 | break; |
| 155 | case kColorNone: |
| 156 | break; |
| 157 | } |
| 158 | return key; |
| 159 | } |
| 160 | }; // struct ProgramDescription |
| 161 | |
| 162 | /** |
| 163 | * Generates and caches program. Programs are generated based on |
| 164 | * ProgramDescriptions. |
| 165 | */ |
| 166 | class ProgramCache { |
| 167 | public: |
| 168 | ProgramCache(); |
| 169 | ~ProgramCache(); |
| 170 | |
| 171 | Program* get(const ProgramDescription& description); |
| 172 | |
| 173 | void clear(); |
| 174 | |
| 175 | private: |
| 176 | Program* generateProgram(const ProgramDescription& description, programid key); |
| 177 | String8 generateVertexShader(const ProgramDescription& description); |
| 178 | String8 generateFragmentShader(const ProgramDescription& description); |
| 179 | void generatePorterDuffBlend(String8& shader, const char* name, SkXfermode::Mode mode); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 180 | void generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 181 | |
| 182 | KeyedVector<programid, Program*> mCache; |
| 183 | |
| 184 | }; // class ProgramCache |
| 185 | |
| 186 | }; // namespace uirenderer |
| 187 | }; // namespace android |
| 188 | |
| 189 | #endif // ANDROID_UI_PROGRAM_CACHE_H |