| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 2 | /* |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 10 | #ifndef GrPaint_DEFINED |
| 11 | #define GrPaint_DEFINED |
| 12 | |
| 13 | #include "GrTexture.h" |
| 14 | #include "GrColor.h" |
| 15 | #include "GrSamplerState.h" |
| 16 | |
| Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 17 | #include "SkXfermode.h" |
| 18 | |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 19 | /** |
| 20 | * The paint describes how pixels are colored when the context draws to |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 21 | * them. TODO: Make this a "real" class with getters and setters, default |
| 22 | * values, and documentation. |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 23 | */ |
| 24 | class GrPaint { |
| 25 | public: |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 26 | enum { |
| 27 | kMaxTextures = 1, |
| 28 | kMaxMasks = 1, |
| 29 | }; |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 30 | |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 31 | // All the paint fields are public except textures/samplers |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 32 | GrBlendCoeff fSrcBlendCoeff; |
| 33 | GrBlendCoeff fDstBlendCoeff; |
| 34 | bool fAntiAlias; |
| 35 | bool fDither; |
| senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame^] | 36 | bool fColorMatrixEnabled; |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 37 | |
| 38 | GrColor fColor; |
| 39 | |
| Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 40 | GrColor fColorFilterColor; |
| 41 | SkXfermode::Mode fColorFilterXfermode; |
| senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame^] | 42 | float fColorMatrix[20]; |
| Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 43 | |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 44 | void setTexture(int i, GrTexture* texture) { |
| 45 | GrAssert((unsigned)i < kMaxTextures); |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 46 | GrSafeRef(texture); |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 47 | GrSafeUnref(fTextures[i]); |
| 48 | fTextures[i] = texture; |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 51 | GrTexture* getTexture(int i) const { |
| 52 | GrAssert((unsigned)i < kMaxTextures); |
| 53 | return fTextures[i]; |
| 54 | } |
| 55 | |
| bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 56 | GrSamplerState* textureSampler(int i) { |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 57 | GrAssert((unsigned)i < kMaxTextures); |
| 58 | return fTextureSamplers + i; |
| 59 | } |
| 60 | |
| bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 61 | const GrSamplerState& getTextureSampler(int i) const { |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 62 | GrAssert((unsigned)i < kMaxTextures); |
| bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 63 | return fTextureSamplers[i]; |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // The mask can be alpha-only or per channel. It is applied |
| 67 | // after the colorfilter |
| 68 | void setMask(int i, GrTexture* mask) { |
| 69 | GrAssert((unsigned)i < kMaxMasks); |
| 70 | GrSafeRef(mask); |
| 71 | GrSafeUnref(fMaskTextures[i]); |
| 72 | fMaskTextures[i] = mask; |
| 73 | } |
| 74 | |
| 75 | GrTexture* getMask(int i) const { |
| 76 | GrAssert((unsigned)i < kMaxMasks); |
| 77 | return fMaskTextures[i]; |
| 78 | } |
| 79 | |
| 80 | // mask's sampler matrix is always applied to the positions |
| 81 | // (i.e. no explicit texture coordinates) |
| bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 82 | GrSamplerState* maskSampler(int i) { |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 83 | GrAssert((unsigned)i < kMaxMasks); |
| 84 | return fMaskSamplers + i; |
| 85 | } |
| 86 | |
| bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 87 | const GrSamplerState& getMaskSampler(int i) const { |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 88 | GrAssert((unsigned)i < kMaxMasks); |
| bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 89 | return fMaskSamplers[i]; |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | // pre-concats sampler matrices for non-NULL textures and masks |
| 93 | void preConcatActiveSamplerMatrices(const GrMatrix& matrix) { |
| 94 | for (int i = 0; i < kMaxTextures; ++i) { |
| 95 | fTextureSamplers[i].preConcatMatrix(matrix); |
| 96 | } |
| 97 | for (int i = 0; i < kMaxMasks; ++i) { |
| 98 | fMaskSamplers[i].preConcatMatrix(matrix); |
| 99 | } |
| 100 | } |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 101 | |
| 102 | // uninitialized |
| 103 | GrPaint() { |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 104 | for (int i = 0; i < kMaxTextures; ++i) { |
| 105 | fTextures[i] = NULL; |
| 106 | } |
| 107 | for (int i = 0; i < kMaxMasks; ++i) { |
| 108 | fMaskTextures[i] = NULL; |
| 109 | } |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | GrPaint(const GrPaint& paint) { |
| bsalomon@google.com | 27c9b6d | 2011-09-12 14:30:27 +0000 | [diff] [blame] | 113 | for (int i = 0; i < kMaxTextures; ++i) { |
| 114 | fTextures[i] = NULL; |
| 115 | } |
| 116 | for (int i = 0; i < kMaxMasks; ++i) { |
| 117 | fMaskTextures[i] = NULL; |
| 118 | } |
| 119 | *this = paint; |
| 120 | } |
| 121 | |
| 122 | GrPaint& operator=(const GrPaint& paint) { |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 123 | fSrcBlendCoeff = paint.fSrcBlendCoeff; |
| 124 | fDstBlendCoeff = paint.fDstBlendCoeff; |
| 125 | fAntiAlias = paint.fAntiAlias; |
| 126 | fDither = paint.fDither; |
| 127 | |
| 128 | fColor = paint.fColor; |
| 129 | |
| Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 130 | fColorFilterColor = paint.fColorFilterColor; |
| 131 | fColorFilterXfermode = paint.fColorFilterXfermode; |
| senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame^] | 132 | memcpy(fColorMatrix, paint.fColorMatrix, sizeof(fColorMatrix)); |
| 133 | fColorMatrixEnabled = paint.fColorMatrixEnabled; |
| Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 134 | |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 135 | for (int i = 0; i < kMaxTextures; ++i) { |
| bsalomon@google.com | 27c9b6d | 2011-09-12 14:30:27 +0000 | [diff] [blame] | 136 | GrSafeUnref(fTextures[i]); |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 137 | fTextureSamplers[i] = paint.fTextureSamplers[i]; |
| 138 | fTextures[i] = paint.fTextures[i]; |
| 139 | GrSafeRef(fTextures[i]); |
| 140 | } |
| 141 | for (int i = 0; i < kMaxMasks; ++i) { |
| bsalomon@google.com | 27c9b6d | 2011-09-12 14:30:27 +0000 | [diff] [blame] | 142 | GrSafeUnref(fMaskTextures[i]); |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 143 | fMaskSamplers[i] = paint.fMaskSamplers[i]; |
| 144 | fMaskTextures[i] = paint.fMaskTextures[i]; |
| 145 | GrSafeRef(fMaskTextures[i]); |
| 146 | } |
| bsalomon@google.com | 27c9b6d | 2011-09-12 14:30:27 +0000 | [diff] [blame] | 147 | return *this; |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | ~GrPaint() { |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 151 | for (int i = 0; i < kMaxTextures; ++i) { |
| 152 | GrSafeUnref(fTextures[i]); |
| 153 | } |
| 154 | for (int i = 0; i < kMaxMasks; ++i) { |
| 155 | GrSafeUnref(fMaskTextures[i]); |
| 156 | } |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 159 | // sets paint to src-over, solid white, no texture, no mask |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 160 | void reset() { |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 161 | this->resetBlend(); |
| 162 | this->resetOptions(); |
| 163 | this->resetColor(); |
| 164 | this->resetTextures(); |
| 165 | this->resetColorFilter(); |
| 166 | this->resetMasks(); |
| Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void resetColorFilter() { |
| 170 | fColorFilterXfermode = SkXfermode::kDst_Mode; |
| 171 | fColorFilterColor = GrColorPackRGBA(0xff, 0xff, 0xff, 0xff); |
| senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame^] | 172 | memset(fColorMatrix, 0, sizeof(fColorMatrix)); |
| 173 | fColorMatrixEnabled = false; |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 176 | bool hasTexture() const { |
| 177 | return 0 != this->getActiveTextureStageMask(); |
| 178 | } |
| 179 | |
| 180 | bool hasMask() const { |
| 181 | return 0 != this->getActiveMaskStageMask(); |
| 182 | } |
| 183 | |
| 184 | bool hasTextureOrMask() const { |
| 185 | return this->hasTexture() || this->hasMask(); |
| 186 | } |
| 187 | |
| 188 | // helpers for GrContext, GrTextContext |
| 189 | int getActiveTextureStageMask() const { |
| 190 | int mask = 0; |
| 191 | for (int i = 0; i < kMaxTextures; ++i) { |
| 192 | if (NULL != fTextures[i]) { |
| 193 | mask |= 1 << (i + kFirstTextureStage); |
| 194 | } |
| 195 | } |
| 196 | return mask; |
| 197 | } |
| 198 | |
| 199 | int getActiveMaskStageMask() const { |
| bsalomon@google.com | 00e17c5 | 2011-05-18 19:02:42 +0000 | [diff] [blame] | 200 | int mask = 0; |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 201 | for (int i = 0; i < kMaxMasks; ++i) { |
| 202 | if (NULL != fMaskTextures[i]) { |
| 203 | mask |= 1 << (i + kFirstMaskStage); |
| 204 | } |
| 205 | } |
| 206 | return mask; |
| 207 | } |
| 208 | |
| 209 | int getActiveStageMask() const { |
| 210 | return this->getActiveTextureStageMask() | |
| 211 | this->getActiveMaskStageMask(); |
| 212 | } |
| 213 | |
| 214 | // internal use |
| 215 | // GrPaint's textures and masks map to the first N stages |
| 216 | // of GrDrawTarget in that order (textures followed by masks) |
| 217 | enum { |
| 218 | kFirstTextureStage = 0, |
| 219 | kFirstMaskStage = kMaxTextures, |
| 220 | kTotalStages = kMaxTextures + kMaxMasks, |
| 221 | }; |
| 222 | |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 223 | private: |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 224 | |
| 225 | GrSamplerState fTextureSamplers[kMaxTextures]; |
| 226 | GrSamplerState fMaskSamplers[kMaxMasks]; |
| 227 | |
| 228 | GrTexture* fTextures[kMaxTextures]; |
| 229 | GrTexture* fMaskTextures[kMaxMasks]; |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 230 | |
| 231 | void resetBlend() { |
| 232 | fSrcBlendCoeff = kOne_BlendCoeff; |
| 233 | fDstBlendCoeff = kZero_BlendCoeff; |
| 234 | } |
| 235 | |
| 236 | void resetOptions() { |
| 237 | fAntiAlias = false; |
| 238 | fDither = false; |
| 239 | } |
| 240 | |
| 241 | void resetColor() { |
| 242 | fColor = GrColorPackRGBA(0xff, 0xff, 0xff, 0xff); |
| 243 | } |
| 244 | |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 245 | void resetTextures() { |
| 246 | for (int i = 0; i < kMaxTextures; ++i) { |
| 247 | this->setTexture(i, NULL); |
| bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 248 | fTextureSamplers[i].reset(); |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 249 | } |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 252 | void resetMasks() { |
| 253 | for (int i = 0; i < kMaxMasks; ++i) { |
| 254 | this->setMask(i, NULL); |
| bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 255 | fMaskSamplers[i].reset(); |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 256 | } |
| 257 | } |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 258 | }; |
| 259 | |
| 260 | #endif |