| 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; |
| 36 | |
| 37 | GrColor fColor; |
| 38 | |
| Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 39 | GrColor fColorFilterColor; |
| 40 | SkXfermode::Mode fColorFilterXfermode; |
| 41 | |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 42 | void setTexture(int i, GrTexture* texture) { |
| 43 | GrAssert((unsigned)i < kMaxTextures); |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 44 | GrSafeRef(texture); |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 45 | GrSafeUnref(fTextures[i]); |
| 46 | fTextures[i] = texture; |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 49 | GrTexture* getTexture(int i) const { |
| 50 | GrAssert((unsigned)i < kMaxTextures); |
| 51 | return fTextures[i]; |
| 52 | } |
| 53 | |
| 54 | GrSamplerState* getTextureSampler(int i) { |
| 55 | GrAssert((unsigned)i < kMaxTextures); |
| 56 | return fTextureSamplers + i; |
| 57 | } |
| 58 | |
| 59 | const GrSamplerState* getTextureSampler(int i) const { |
| 60 | GrAssert((unsigned)i < kMaxTextures); |
| 61 | return fTextureSamplers + i; |
| 62 | } |
| 63 | |
| 64 | // The mask can be alpha-only or per channel. It is applied |
| 65 | // after the colorfilter |
| 66 | void setMask(int i, GrTexture* mask) { |
| 67 | GrAssert((unsigned)i < kMaxMasks); |
| 68 | GrSafeRef(mask); |
| 69 | GrSafeUnref(fMaskTextures[i]); |
| 70 | fMaskTextures[i] = mask; |
| 71 | } |
| 72 | |
| 73 | GrTexture* getMask(int i) const { |
| 74 | GrAssert((unsigned)i < kMaxMasks); |
| 75 | return fMaskTextures[i]; |
| 76 | } |
| 77 | |
| 78 | // mask's sampler matrix is always applied to the positions |
| 79 | // (i.e. no explicit texture coordinates) |
| 80 | GrSamplerState* getMaskSampler(int i) { |
| 81 | GrAssert((unsigned)i < kMaxMasks); |
| 82 | return fMaskSamplers + i; |
| 83 | } |
| 84 | |
| 85 | const GrSamplerState* getMaskSampler(int i) const { |
| 86 | GrAssert((unsigned)i < kMaxMasks); |
| 87 | return fMaskSamplers + i; |
| 88 | } |
| 89 | |
| 90 | // pre-concats sampler matrices for non-NULL textures and masks |
| 91 | void preConcatActiveSamplerMatrices(const GrMatrix& matrix) { |
| 92 | for (int i = 0; i < kMaxTextures; ++i) { |
| 93 | fTextureSamplers[i].preConcatMatrix(matrix); |
| 94 | } |
| 95 | for (int i = 0; i < kMaxMasks; ++i) { |
| 96 | fMaskSamplers[i].preConcatMatrix(matrix); |
| 97 | } |
| 98 | } |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 99 | |
| 100 | // uninitialized |
| 101 | GrPaint() { |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 102 | for (int i = 0; i < kMaxTextures; ++i) { |
| 103 | fTextures[i] = NULL; |
| 104 | } |
| 105 | for (int i = 0; i < kMaxMasks; ++i) { |
| 106 | fMaskTextures[i] = NULL; |
| 107 | } |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | GrPaint(const GrPaint& paint) { |
| 111 | fSrcBlendCoeff = paint.fSrcBlendCoeff; |
| 112 | fDstBlendCoeff = paint.fDstBlendCoeff; |
| 113 | fAntiAlias = paint.fAntiAlias; |
| 114 | fDither = paint.fDither; |
| 115 | |
| 116 | fColor = paint.fColor; |
| 117 | |
| Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 118 | fColorFilterColor = paint.fColorFilterColor; |
| 119 | fColorFilterXfermode = paint.fColorFilterXfermode; |
| 120 | |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 121 | for (int i = 0; i < kMaxTextures; ++i) { |
| 122 | fTextureSamplers[i] = paint.fTextureSamplers[i]; |
| 123 | fTextures[i] = paint.fTextures[i]; |
| 124 | GrSafeRef(fTextures[i]); |
| 125 | } |
| 126 | for (int i = 0; i < kMaxMasks; ++i) { |
| 127 | fMaskSamplers[i] = paint.fMaskSamplers[i]; |
| 128 | fMaskTextures[i] = paint.fMaskTextures[i]; |
| 129 | GrSafeRef(fMaskTextures[i]); |
| 130 | } |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | ~GrPaint() { |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 134 | for (int i = 0; i < kMaxTextures; ++i) { |
| 135 | GrSafeUnref(fTextures[i]); |
| 136 | } |
| 137 | for (int i = 0; i < kMaxMasks; ++i) { |
| 138 | GrSafeUnref(fMaskTextures[i]); |
| 139 | } |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 142 | // sets paint to src-over, solid white, no texture, no mask |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 143 | void reset() { |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 144 | this->resetBlend(); |
| 145 | this->resetOptions(); |
| 146 | this->resetColor(); |
| 147 | this->resetTextures(); |
| 148 | this->resetColorFilter(); |
| 149 | this->resetMasks(); |
| Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void resetColorFilter() { |
| 153 | fColorFilterXfermode = SkXfermode::kDst_Mode; |
| 154 | fColorFilterColor = GrColorPackRGBA(0xff, 0xff, 0xff, 0xff); |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 157 | bool hasTexture() const { |
| 158 | return 0 != this->getActiveTextureStageMask(); |
| 159 | } |
| 160 | |
| 161 | bool hasMask() const { |
| 162 | return 0 != this->getActiveMaskStageMask(); |
| 163 | } |
| 164 | |
| 165 | bool hasTextureOrMask() const { |
| 166 | return this->hasTexture() || this->hasMask(); |
| 167 | } |
| 168 | |
| 169 | // helpers for GrContext, GrTextContext |
| 170 | int getActiveTextureStageMask() const { |
| 171 | int mask = 0; |
| 172 | for (int i = 0; i < kMaxTextures; ++i) { |
| 173 | if (NULL != fTextures[i]) { |
| 174 | mask |= 1 << (i + kFirstTextureStage); |
| 175 | } |
| 176 | } |
| 177 | return mask; |
| 178 | } |
| 179 | |
| 180 | int getActiveMaskStageMask() const { |
| bsalomon@google.com | 00e17c5 | 2011-05-18 19:02:42 +0000 | [diff] [blame] | 181 | int mask = 0; |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 182 | for (int i = 0; i < kMaxMasks; ++i) { |
| 183 | if (NULL != fMaskTextures[i]) { |
| 184 | mask |= 1 << (i + kFirstMaskStage); |
| 185 | } |
| 186 | } |
| 187 | return mask; |
| 188 | } |
| 189 | |
| 190 | int getActiveStageMask() const { |
| 191 | return this->getActiveTextureStageMask() | |
| 192 | this->getActiveMaskStageMask(); |
| 193 | } |
| 194 | |
| 195 | // internal use |
| 196 | // GrPaint's textures and masks map to the first N stages |
| 197 | // of GrDrawTarget in that order (textures followed by masks) |
| 198 | enum { |
| 199 | kFirstTextureStage = 0, |
| 200 | kFirstMaskStage = kMaxTextures, |
| 201 | kTotalStages = kMaxTextures + kMaxMasks, |
| 202 | }; |
| 203 | |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 204 | private: |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 205 | |
| 206 | GrSamplerState fTextureSamplers[kMaxTextures]; |
| 207 | GrSamplerState fMaskSamplers[kMaxMasks]; |
| 208 | |
| 209 | GrTexture* fTextures[kMaxTextures]; |
| 210 | GrTexture* fMaskTextures[kMaxMasks]; |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 211 | |
| 212 | void resetBlend() { |
| 213 | fSrcBlendCoeff = kOne_BlendCoeff; |
| 214 | fDstBlendCoeff = kZero_BlendCoeff; |
| 215 | } |
| 216 | |
| 217 | void resetOptions() { |
| 218 | fAntiAlias = false; |
| 219 | fDither = false; |
| 220 | } |
| 221 | |
| 222 | void resetColor() { |
| 223 | fColor = GrColorPackRGBA(0xff, 0xff, 0xff, 0xff); |
| 224 | } |
| 225 | |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 226 | void resetTextures() { |
| 227 | for (int i = 0; i < kMaxTextures; ++i) { |
| 228 | this->setTexture(i, NULL); |
| 229 | fTextureSamplers[i].setClampNoFilter(); |
| 230 | } |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 233 | void resetMasks() { |
| 234 | for (int i = 0; i < kMaxMasks; ++i) { |
| 235 | this->setMask(i, NULL); |
| 236 | fMaskSamplers[i].setClampNoFilter(); |
| 237 | } |
| 238 | } |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 239 | }; |
| 240 | |
| 241 | #endif |