blob: 827b89024fb5aa9f11bcba72a5169112bfb202e5 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
bsalomon@google.com27847de2011-02-22 20:59:41 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.com27847de2011-02-22 20:59:41 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
bsalomon@google.com27847de2011-02-22 20:59:41 +000010#ifndef GrPaint_DEFINED
11#define GrPaint_DEFINED
12
13#include "GrTexture.h"
14#include "GrColor.h"
15#include "GrSamplerState.h"
16
Scroggo97c88c22011-05-11 14:05:25 +000017#include "SkXfermode.h"
18
bsalomon@google.com27847de2011-02-22 20:59:41 +000019/**
20 * The paint describes how pixels are colored when the context draws to
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000021 * them. TODO: Make this a "real" class with getters and setters, default
22 * values, and documentation.
bsalomon@google.com27847de2011-02-22 20:59:41 +000023 */
24class GrPaint {
25public:
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000026 enum {
27 kMaxTextures = 1,
28 kMaxMasks = 1,
29 };
bsalomon@google.com27847de2011-02-22 20:59:41 +000030
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000031 // All the paint fields are public except textures/samplers
bsalomon@google.com27847de2011-02-22 20:59:41 +000032 GrBlendCoeff fSrcBlendCoeff;
33 GrBlendCoeff fDstBlendCoeff;
34 bool fAntiAlias;
35 bool fDither;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +000036 bool fColorMatrixEnabled;
bsalomon@google.com27847de2011-02-22 20:59:41 +000037
38 GrColor fColor;
bsalomon@google.comdd1be602012-01-18 20:34:00 +000039 uint8_t fCoverage;
bsalomon@google.com27847de2011-02-22 20:59:41 +000040
Scroggo97c88c22011-05-11 14:05:25 +000041 GrColor fColorFilterColor;
42 SkXfermode::Mode fColorFilterXfermode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +000043 float fColorMatrix[20];
Scroggo97c88c22011-05-11 14:05:25 +000044
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000045 void setTexture(int i, GrTexture* texture) {
46 GrAssert((unsigned)i < kMaxTextures);
bsalomon@google.com27847de2011-02-22 20:59:41 +000047 GrSafeRef(texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000048 GrSafeUnref(fTextures[i]);
49 fTextures[i] = texture;
bsalomon@google.com27847de2011-02-22 20:59:41 +000050 }
51
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000052 GrTexture* getTexture(int i) const {
53 GrAssert((unsigned)i < kMaxTextures);
54 return fTextures[i];
55 }
56
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +000057 GrSamplerState* textureSampler(int i) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000058 GrAssert((unsigned)i < kMaxTextures);
59 return fTextureSamplers + i;
60 }
61
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +000062 const GrSamplerState& getTextureSampler(int i) const {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000063 GrAssert((unsigned)i < kMaxTextures);
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +000064 return fTextureSamplers[i];
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000065 }
66
67 // The mask can be alpha-only or per channel. It is applied
68 // after the colorfilter
69 void setMask(int i, GrTexture* mask) {
70 GrAssert((unsigned)i < kMaxMasks);
71 GrSafeRef(mask);
72 GrSafeUnref(fMaskTextures[i]);
73 fMaskTextures[i] = mask;
74 }
75
76 GrTexture* getMask(int i) const {
77 GrAssert((unsigned)i < kMaxMasks);
78 return fMaskTextures[i];
79 }
80
81 // mask's sampler matrix is always applied to the positions
82 // (i.e. no explicit texture coordinates)
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +000083 GrSamplerState* maskSampler(int i) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000084 GrAssert((unsigned)i < kMaxMasks);
85 return fMaskSamplers + i;
86 }
87
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +000088 const GrSamplerState& getMaskSampler(int i) const {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000089 GrAssert((unsigned)i < kMaxMasks);
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +000090 return fMaskSamplers[i];
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000091 }
92
93 // pre-concats sampler matrices for non-NULL textures and masks
94 void preConcatActiveSamplerMatrices(const GrMatrix& matrix) {
95 for (int i = 0; i < kMaxTextures; ++i) {
96 fTextureSamplers[i].preConcatMatrix(matrix);
97 }
98 for (int i = 0; i < kMaxMasks; ++i) {
99 fMaskSamplers[i].preConcatMatrix(matrix);
100 }
101 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000102
103 // uninitialized
104 GrPaint() {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000105 for (int i = 0; i < kMaxTextures; ++i) {
106 fTextures[i] = NULL;
107 }
108 for (int i = 0; i < kMaxMasks; ++i) {
109 fMaskTextures[i] = NULL;
110 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000111 }
112
113 GrPaint(const GrPaint& paint) {
bsalomon@google.com27c9b6d2011-09-12 14:30:27 +0000114 for (int i = 0; i < kMaxTextures; ++i) {
115 fTextures[i] = NULL;
116 }
117 for (int i = 0; i < kMaxMasks; ++i) {
118 fMaskTextures[i] = NULL;
119 }
120 *this = paint;
121 }
122
123 GrPaint& operator=(const GrPaint& paint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000124 fSrcBlendCoeff = paint.fSrcBlendCoeff;
125 fDstBlendCoeff = paint.fDstBlendCoeff;
126 fAntiAlias = paint.fAntiAlias;
127 fDither = paint.fDither;
128
129 fColor = paint.fColor;
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000130 fCoverage = paint.fCoverage;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000131
Scroggo97c88c22011-05-11 14:05:25 +0000132 fColorFilterColor = paint.fColorFilterColor;
133 fColorFilterXfermode = paint.fColorFilterXfermode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000134 memcpy(fColorMatrix, paint.fColorMatrix, sizeof(fColorMatrix));
135 fColorMatrixEnabled = paint.fColorMatrixEnabled;
Scroggo97c88c22011-05-11 14:05:25 +0000136
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000137 for (int i = 0; i < kMaxTextures; ++i) {
bsalomon@google.com27c9b6d2011-09-12 14:30:27 +0000138 GrSafeUnref(fTextures[i]);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000139 fTextureSamplers[i] = paint.fTextureSamplers[i];
140 fTextures[i] = paint.fTextures[i];
141 GrSafeRef(fTextures[i]);
142 }
143 for (int i = 0; i < kMaxMasks; ++i) {
bsalomon@google.com27c9b6d2011-09-12 14:30:27 +0000144 GrSafeUnref(fMaskTextures[i]);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000145 fMaskSamplers[i] = paint.fMaskSamplers[i];
146 fMaskTextures[i] = paint.fMaskTextures[i];
147 GrSafeRef(fMaskTextures[i]);
148 }
bsalomon@google.com27c9b6d2011-09-12 14:30:27 +0000149 return *this;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000150 }
151
152 ~GrPaint() {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000153 for (int i = 0; i < kMaxTextures; ++i) {
154 GrSafeUnref(fTextures[i]);
155 }
156 for (int i = 0; i < kMaxMasks; ++i) {
157 GrSafeUnref(fMaskTextures[i]);
158 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000159 }
160
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000161 // sets paint to src-over, solid white, no texture, no mask
bsalomon@google.com27847de2011-02-22 20:59:41 +0000162 void reset() {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000163 this->resetBlend();
164 this->resetOptions();
165 this->resetColor();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000166 this->resetCoverage();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000167 this->resetTextures();
168 this->resetColorFilter();
169 this->resetMasks();
Scroggo97c88c22011-05-11 14:05:25 +0000170 }
171
172 void resetColorFilter() {
173 fColorFilterXfermode = SkXfermode::kDst_Mode;
174 fColorFilterColor = GrColorPackRGBA(0xff, 0xff, 0xff, 0xff);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000175 fColorMatrixEnabled = false;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000176 }
177
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000178 bool hasTexture() const {
179 return 0 != this->getActiveTextureStageMask();
180 }
181
182 bool hasMask() const {
183 return 0 != this->getActiveMaskStageMask();
184 }
185
186 bool hasTextureOrMask() const {
187 return this->hasTexture() || this->hasMask();
188 }
189
190 // helpers for GrContext, GrTextContext
191 int getActiveTextureStageMask() const {
192 int mask = 0;
193 for (int i = 0; i < kMaxTextures; ++i) {
194 if (NULL != fTextures[i]) {
195 mask |= 1 << (i + kFirstTextureStage);
196 }
197 }
198 return mask;
199 }
200
201 int getActiveMaskStageMask() const {
bsalomon@google.com00e17c52011-05-18 19:02:42 +0000202 int mask = 0;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000203 for (int i = 0; i < kMaxMasks; ++i) {
204 if (NULL != fMaskTextures[i]) {
205 mask |= 1 << (i + kFirstMaskStage);
206 }
207 }
208 return mask;
209 }
210
211 int getActiveStageMask() const {
212 return this->getActiveTextureStageMask() |
213 this->getActiveMaskStageMask();
214 }
215
216 // internal use
217 // GrPaint's textures and masks map to the first N stages
218 // of GrDrawTarget in that order (textures followed by masks)
219 enum {
220 kFirstTextureStage = 0,
221 kFirstMaskStage = kMaxTextures,
222 kTotalStages = kMaxTextures + kMaxMasks,
223 };
224
bsalomon@google.com27847de2011-02-22 20:59:41 +0000225private:
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000226
227 GrSamplerState fTextureSamplers[kMaxTextures];
228 GrSamplerState fMaskSamplers[kMaxMasks];
229
230 GrTexture* fTextures[kMaxTextures];
231 GrTexture* fMaskTextures[kMaxMasks];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000232
233 void resetBlend() {
234 fSrcBlendCoeff = kOne_BlendCoeff;
235 fDstBlendCoeff = kZero_BlendCoeff;
236 }
237
238 void resetOptions() {
239 fAntiAlias = false;
240 fDither = false;
241 }
242
243 void resetColor() {
244 fColor = GrColorPackRGBA(0xff, 0xff, 0xff, 0xff);
245 }
246
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000247 void resetCoverage() {
248 fCoverage = 0xff;
249 }
250
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000251 void resetTextures() {
252 for (int i = 0; i < kMaxTextures; ++i) {
253 this->setTexture(i, NULL);
bsalomon@google.com97912912011-12-06 16:30:36 +0000254 fTextureSamplers[i].reset();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000255 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000256 }
257
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000258 void resetMasks() {
259 for (int i = 0; i < kMaxMasks; ++i) {
260 this->setMask(i, NULL);
bsalomon@google.com97912912011-12-06 16:30:36 +0000261 fMaskSamplers[i].reset();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000262 }
263 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000264};
265
266#endif