Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -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_H |
| 18 | #define ANDROID_HWUI_PROGRAM_H |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 19 | |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 20 | #include <utils/KeyedVector.h> |
| 21 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 22 | #include <GLES2/gl2.h> |
| 23 | #include <GLES2/gl2ext.h> |
| 24 | |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 25 | #include <SkXfermode.h> |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 26 | |
Chris Craik | 096b8d9 | 2013-03-01 11:08:11 -0800 | [diff] [blame] | 27 | #include "Debug.h" |
Romain Guy | 0b9db91 | 2010-07-09 18:53:25 -0700 | [diff] [blame] | 28 | #include "Matrix.h" |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 29 | #include "Properties.h" |
Romain Guy | 0b9db91 | 2010-07-09 18:53:25 -0700 | [diff] [blame] | 30 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 31 | namespace android { |
| 32 | namespace uirenderer { |
| 33 | |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 34 | /////////////////////////////////////////////////////////////////////////////// |
| 35 | // Defines |
| 36 | /////////////////////////////////////////////////////////////////////////////// |
| 37 | |
| 38 | // Debug |
| 39 | #if DEBUG_PROGRAMS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 40 | #define PROGRAM_LOGD(...) ALOGD(__VA_ARGS__) |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 41 | #else |
| 42 | #define PROGRAM_LOGD(...) |
| 43 | #endif |
| 44 | |
Romain Guy | f877308 | 2012-07-12 18:01:00 -0700 | [diff] [blame] | 45 | #define COLOR_COMPONENT_THRESHOLD 1.0f |
| 46 | #define COLOR_COMPONENT_INV_THRESHOLD 0.0f |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 47 | |
| 48 | #define PROGRAM_KEY_TEXTURE 0x1 |
| 49 | #define PROGRAM_KEY_A8_TEXTURE 0x2 |
| 50 | #define PROGRAM_KEY_BITMAP 0x4 |
| 51 | #define PROGRAM_KEY_GRADIENT 0x8 |
| 52 | #define PROGRAM_KEY_BITMAP_FIRST 0x10 |
| 53 | #define PROGRAM_KEY_COLOR_MATRIX 0x20 |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 54 | #define PROGRAM_KEY_COLOR_BLEND 0x40 |
| 55 | #define PROGRAM_KEY_BITMAP_NPOT 0x80 |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 56 | #define PROGRAM_KEY_SWAP_SRC_DST 0x2000 |
| 57 | |
| 58 | #define PROGRAM_KEY_BITMAP_WRAPS_MASK 0x600 |
| 59 | #define PROGRAM_KEY_BITMAP_WRAPT_MASK 0x1800 |
| 60 | |
| 61 | // Encode the xfermodes on 6 bits |
| 62 | #define PROGRAM_MAX_XFERMODE 0x1f |
| 63 | #define PROGRAM_XFERMODE_SHADER_SHIFT 26 |
| 64 | #define PROGRAM_XFERMODE_COLOR_OP_SHIFT 20 |
| 65 | #define PROGRAM_XFERMODE_FRAMEBUFFER_SHIFT 14 |
| 66 | |
| 67 | #define PROGRAM_BITMAP_WRAPS_SHIFT 9 |
| 68 | #define PROGRAM_BITMAP_WRAPT_SHIFT 11 |
| 69 | |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 70 | #define PROGRAM_GRADIENT_TYPE_SHIFT 33 // 2 bits for gradient type |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 71 | #define PROGRAM_MODULATE_SHIFT 35 |
| 72 | |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 73 | #define PROGRAM_HAS_AA_SHIFT 36 |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 74 | |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 75 | #define PROGRAM_HAS_EXTERNAL_TEXTURE_SHIFT 37 |
| 76 | #define PROGRAM_HAS_TEXTURE_TRANSFORM_SHIFT 38 |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 77 | |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 78 | #define PROGRAM_HAS_GAMMA_CORRECTION 39 |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 79 | |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 80 | #define PROGRAM_IS_SIMPLE_GRADIENT 40 |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 81 | |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 82 | #define PROGRAM_HAS_COLORS 41 |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 83 | |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 84 | #define PROGRAM_HAS_DEBUG_HIGHLIGHT 42 |
| 85 | #define PROGRAM_EMULATE_STENCIL 43 |
Romain Guy | 3ff0bfd | 2013-02-25 14:15:37 -0800 | [diff] [blame] | 86 | |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 87 | /////////////////////////////////////////////////////////////////////////////// |
| 88 | // Types |
| 89 | /////////////////////////////////////////////////////////////////////////////// |
| 90 | |
| 91 | typedef uint64_t programid; |
| 92 | |
| 93 | /////////////////////////////////////////////////////////////////////////////// |
| 94 | // Program description |
| 95 | /////////////////////////////////////////////////////////////////////////////// |
| 96 | |
| 97 | /** |
| 98 | * Describe the features required for a given program. The features |
| 99 | * determine the generation of both the vertex and fragment shaders. |
| 100 | * A ProgramDescription must be used in conjunction with a ProgramCache. |
| 101 | */ |
| 102 | struct ProgramDescription { |
| 103 | enum ColorModifier { |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 104 | kColorNone = 0, |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 105 | kColorMatrix, |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 106 | kColorBlend |
| 107 | }; |
| 108 | |
| 109 | enum Gradient { |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 110 | kGradientLinear = 0, |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 111 | kGradientCircular, |
| 112 | kGradientSweep |
| 113 | }; |
| 114 | |
| 115 | ProgramDescription() { |
| 116 | reset(); |
| 117 | } |
| 118 | |
| 119 | // Texturing |
| 120 | bool hasTexture; |
| 121 | bool hasAlpha8Texture; |
| 122 | bool hasExternalTexture; |
| 123 | bool hasTextureTransform; |
| 124 | |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 125 | // Color attribute |
| 126 | bool hasColors; |
| 127 | |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 128 | // Modulate, this should only be set when setColor() return true |
| 129 | bool modulate; |
| 130 | |
| 131 | // Shaders |
| 132 | bool hasBitmap; |
| 133 | bool isBitmapNpot; |
| 134 | |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 135 | bool isAA; // drawing with a per-vertex alpha |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 136 | |
| 137 | bool hasGradient; |
| 138 | Gradient gradientType; |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 139 | bool isSimpleGradient; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 140 | |
| 141 | SkXfermode::Mode shadersMode; |
| 142 | |
| 143 | bool isBitmapFirst; |
| 144 | GLenum bitmapWrapS; |
| 145 | GLenum bitmapWrapT; |
| 146 | |
| 147 | // Color operations |
| 148 | ColorModifier colorOp; |
| 149 | SkXfermode::Mode colorMode; |
| 150 | |
| 151 | // Framebuffer blending (requires Extensions.hasFramebufferFetch()) |
| 152 | // Ignored for all values < SkXfermode::kPlus_Mode |
| 153 | SkXfermode::Mode framebufferMode; |
| 154 | bool swapSrcDst; |
| 155 | |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 156 | bool hasGammaCorrection; |
| 157 | float gamma; |
| 158 | |
Romain Guy | 3ff0bfd | 2013-02-25 14:15:37 -0800 | [diff] [blame] | 159 | bool hasDebugHighlight; |
Romain Guy | 78dd96d | 2013-05-03 14:24:16 -0700 | [diff] [blame] | 160 | bool emulateStencil; |
Romain Guy | 3ff0bfd | 2013-02-25 14:15:37 -0800 | [diff] [blame] | 161 | |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 162 | /** |
| 163 | * Resets this description. All fields are reset back to the default |
| 164 | * values they hold after building a new instance. |
| 165 | */ |
| 166 | void reset() { |
| 167 | hasTexture = false; |
| 168 | hasAlpha8Texture = false; |
| 169 | hasExternalTexture = false; |
| 170 | hasTextureTransform = false; |
| 171 | |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 172 | hasColors = false; |
| 173 | |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 174 | isAA = false; |
| 175 | |
| 176 | modulate = false; |
| 177 | |
| 178 | hasBitmap = false; |
| 179 | isBitmapNpot = false; |
| 180 | |
| 181 | hasGradient = false; |
| 182 | gradientType = kGradientLinear; |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 183 | isSimpleGradient = false; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 184 | |
| 185 | shadersMode = SkXfermode::kClear_Mode; |
| 186 | |
| 187 | isBitmapFirst = false; |
| 188 | bitmapWrapS = GL_CLAMP_TO_EDGE; |
| 189 | bitmapWrapT = GL_CLAMP_TO_EDGE; |
| 190 | |
| 191 | colorOp = kColorNone; |
| 192 | colorMode = SkXfermode::kClear_Mode; |
| 193 | |
| 194 | framebufferMode = SkXfermode::kClear_Mode; |
| 195 | swapSrcDst = false; |
| 196 | |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 197 | hasGammaCorrection = false; |
| 198 | gamma = 2.2f; |
Romain Guy | 3ff0bfd | 2013-02-25 14:15:37 -0800 | [diff] [blame] | 199 | |
| 200 | hasDebugHighlight = false; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Indicates, for a given color, whether color modulation is required in |
| 205 | * the fragment shader. When this method returns true, the program should |
| 206 | * be provided with a modulation color. |
| 207 | */ |
Chris Craik | e63f7c62 | 2013-10-17 10:30:55 -0700 | [diff] [blame] | 208 | bool setColorModulate(const float a) { |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 209 | modulate = a < COLOR_COMPONENT_THRESHOLD; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 210 | return modulate; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Indicates, for a given color, whether color modulation is required in |
| 215 | * the fragment shader. When this method returns true, the program should |
| 216 | * be provided with a modulation color. |
| 217 | */ |
Chris Craik | e63f7c62 | 2013-10-17 10:30:55 -0700 | [diff] [blame] | 218 | bool setAlpha8ColorModulate(const float r, const float g, const float b, const float a) { |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 219 | modulate = a < COLOR_COMPONENT_THRESHOLD || r > COLOR_COMPONENT_INV_THRESHOLD || |
| 220 | g > COLOR_COMPONENT_INV_THRESHOLD || b > COLOR_COMPONENT_INV_THRESHOLD; |
| 221 | return modulate; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Computes the unique key identifying this program. |
| 226 | */ |
| 227 | programid key() const { |
| 228 | programid key = 0; |
| 229 | if (hasTexture) key |= PROGRAM_KEY_TEXTURE; |
| 230 | if (hasAlpha8Texture) key |= PROGRAM_KEY_A8_TEXTURE; |
| 231 | if (hasBitmap) { |
| 232 | key |= PROGRAM_KEY_BITMAP; |
| 233 | if (isBitmapNpot) { |
| 234 | key |= PROGRAM_KEY_BITMAP_NPOT; |
| 235 | key |= getEnumForWrap(bitmapWrapS) << PROGRAM_BITMAP_WRAPS_SHIFT; |
| 236 | key |= getEnumForWrap(bitmapWrapT) << PROGRAM_BITMAP_WRAPT_SHIFT; |
| 237 | } |
| 238 | } |
| 239 | if (hasGradient) key |= PROGRAM_KEY_GRADIENT; |
| 240 | key |= programid(gradientType) << PROGRAM_GRADIENT_TYPE_SHIFT; |
| 241 | if (isBitmapFirst) key |= PROGRAM_KEY_BITMAP_FIRST; |
| 242 | if (hasBitmap && hasGradient) { |
| 243 | key |= (shadersMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_SHADER_SHIFT; |
| 244 | } |
| 245 | switch (colorOp) { |
| 246 | case kColorMatrix: |
| 247 | key |= PROGRAM_KEY_COLOR_MATRIX; |
| 248 | break; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 249 | case kColorBlend: |
| 250 | key |= PROGRAM_KEY_COLOR_BLEND; |
| 251 | key |= (colorMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_COLOR_OP_SHIFT; |
| 252 | break; |
| 253 | case kColorNone: |
| 254 | break; |
| 255 | } |
| 256 | key |= (framebufferMode & PROGRAM_MAX_XFERMODE) << PROGRAM_XFERMODE_FRAMEBUFFER_SHIFT; |
| 257 | if (swapSrcDst) key |= PROGRAM_KEY_SWAP_SRC_DST; |
| 258 | if (modulate) key |= programid(0x1) << PROGRAM_MODULATE_SHIFT; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 259 | if (isAA) key |= programid(0x1) << PROGRAM_HAS_AA_SHIFT; |
| 260 | if (hasExternalTexture) key |= programid(0x1) << PROGRAM_HAS_EXTERNAL_TEXTURE_SHIFT; |
| 261 | if (hasTextureTransform) key |= programid(0x1) << PROGRAM_HAS_TEXTURE_TRANSFORM_SHIFT; |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 262 | if (hasGammaCorrection) key |= programid(0x1) << PROGRAM_HAS_GAMMA_CORRECTION; |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 263 | if (isSimpleGradient) key |= programid(0x1) << PROGRAM_IS_SIMPLE_GRADIENT; |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 264 | if (hasColors) key |= programid(0x1) << PROGRAM_HAS_COLORS; |
Romain Guy | 3ff0bfd | 2013-02-25 14:15:37 -0800 | [diff] [blame] | 265 | if (hasDebugHighlight) key |= programid(0x1) << PROGRAM_HAS_DEBUG_HIGHLIGHT; |
Romain Guy | 78dd96d | 2013-05-03 14:24:16 -0700 | [diff] [blame] | 266 | if (emulateStencil) key |= programid(0x1) << PROGRAM_EMULATE_STENCIL; |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 267 | return key; |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Logs the specified message followed by the key identifying this program. |
| 272 | */ |
| 273 | void log(const char* message) const { |
| 274 | #if DEBUG_PROGRAMS |
| 275 | programid k = key(); |
| 276 | PROGRAM_LOGD("%s (key = 0x%.8x%.8x)", message, uint32_t(k >> 32), |
| 277 | uint32_t(k & 0xffffffff)); |
| 278 | #endif |
| 279 | } |
| 280 | |
| 281 | private: |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 282 | static inline uint32_t getEnumForWrap(GLenum wrap) { |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 283 | switch (wrap) { |
| 284 | case GL_CLAMP_TO_EDGE: |
| 285 | return 0; |
| 286 | case GL_REPEAT: |
| 287 | return 1; |
| 288 | case GL_MIRRORED_REPEAT: |
| 289 | return 2; |
| 290 | } |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | }; // struct ProgramDescription |
| 295 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 296 | /** |
| 297 | * A program holds a vertex and a fragment shader. It offers several utility |
| 298 | * methods to query attributes and uniforms. |
| 299 | */ |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 300 | class Program { |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 301 | public: |
Romain Guy | 3e263fa | 2011-12-12 16:47:48 -0800 | [diff] [blame] | 302 | enum ShaderBindings { |
| 303 | kBindingPosition, |
| 304 | kBindingTexCoords |
| 305 | }; |
| 306 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 307 | /** |
| 308 | * Creates a new program with the specified vertex and fragment |
| 309 | * shaders sources. |
| 310 | */ |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 311 | Program(const ProgramDescription& description, const char* vertex, const char* fragment); |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 312 | virtual ~Program(); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 313 | |
| 314 | /** |
| 315 | * Binds this program to the GL context. |
| 316 | */ |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 317 | virtual void use(); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 318 | |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 319 | /** |
| 320 | * Marks this program as unused. This will not unbind |
| 321 | * the program from the GL context. |
| 322 | */ |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 323 | virtual void remove(); |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 324 | |
| 325 | /** |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 326 | * Returns the OpenGL name of the specified attribute. |
| 327 | */ |
| 328 | int getAttrib(const char* name); |
| 329 | |
| 330 | /** |
| 331 | * Returns the OpenGL name of the specified uniform. |
| 332 | */ |
| 333 | int getUniform(const char* name); |
| 334 | |
| 335 | /** |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 336 | * Indicates whether this program is currently in use with |
| 337 | * the GL context. |
| 338 | */ |
| 339 | inline bool isInUse() const { |
| 340 | return mUse; |
| 341 | } |
| 342 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 343 | /** |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 344 | * Indicates whether this program was correctly compiled and linked. |
| 345 | */ |
| 346 | inline bool isInitialized() const { |
| 347 | return mInitialized; |
| 348 | } |
| 349 | |
| 350 | /** |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 351 | * Binds the program with the specified projection, modelView and |
| 352 | * transform matrices. |
| 353 | */ |
| 354 | void set(const mat4& projectionMatrix, const mat4& modelViewMatrix, |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 355 | const mat4& transformMatrix, bool offset = false); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 356 | |
| 357 | /** |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 358 | * Sets the color associated with this shader. |
| 359 | */ |
| 360 | void setColor(const float r, const float g, const float b, const float a); |
| 361 | |
| 362 | /** |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 363 | * Name of the position attribute. |
| 364 | */ |
| 365 | int position; |
| 366 | |
| 367 | /** |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 368 | * Name of the texCoords attribute if it exists, -1 otherwise. |
| 369 | */ |
| 370 | int texCoords; |
| 371 | |
| 372 | /** |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 373 | * Name of the transform uniform. |
| 374 | */ |
| 375 | int transform; |
| 376 | |
Romain Guy | 39284b7 | 2012-09-26 16:39:40 -0700 | [diff] [blame] | 377 | /** |
| 378 | * Name of the projection uniform. |
| 379 | */ |
| 380 | int projection; |
| 381 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 382 | protected: |
| 383 | /** |
| 384 | * Adds an attribute with the specified name. |
| 385 | * |
| 386 | * @return The OpenGL name of the attribute. |
| 387 | */ |
| 388 | int addAttrib(const char* name); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 389 | |
| 390 | /** |
Romain Guy | 3e263fa | 2011-12-12 16:47:48 -0800 | [diff] [blame] | 391 | * Binds the specified attribute name to the specified slot. |
| 392 | */ |
| 393 | int bindAttrib(const char* name, ShaderBindings bindingSlot); |
| 394 | |
| 395 | /** |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 396 | * Adds a uniform with the specified name. |
| 397 | * |
| 398 | * @return The OpenGL name of the uniform. |
| 399 | */ |
| 400 | int addUniform(const char* name); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 401 | |
| 402 | private: |
| 403 | /** |
| 404 | * Compiles the specified shader of the specified type. |
| 405 | * |
| 406 | * @return The name of the compiled shader. |
| 407 | */ |
| 408 | GLuint buildShader(const char* source, GLenum type); |
| 409 | |
Romain Guy | 3e263fa | 2011-12-12 16:47:48 -0800 | [diff] [blame] | 410 | // Name of the OpenGL program and shaders |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 411 | GLuint mProgramId; |
Romain Guy | 3e263fa | 2011-12-12 16:47:48 -0800 | [diff] [blame] | 412 | GLuint mVertexShader; |
| 413 | GLuint mFragmentShader; |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 414 | |
| 415 | // Keeps track of attributes and uniforms slots |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 416 | KeyedVector<const char*, int> mAttributes; |
| 417 | KeyedVector<const char*, int> mUniforms; |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 418 | |
| 419 | bool mUse; |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 420 | bool mInitialized; |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 421 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 422 | // Uniforms caching |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 423 | bool mHasColorUniform; |
| 424 | int mColorUniform; |
Romain Guy | 2d4fd36 | 2011-12-13 22:00:19 -0800 | [diff] [blame] | 425 | |
| 426 | bool mHasSampler; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 427 | |
| 428 | mat4 mProjection; |
Chris Craik | d04a6b1 | 2014-01-29 13:00:33 -0800 | [diff] [blame] | 429 | bool mOffset; |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 430 | }; // class Program |
| 431 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 432 | }; // namespace uirenderer |
| 433 | }; // namespace android |
| 434 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 435 | #endif // ANDROID_HWUI_PROGRAM_H |