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