blob: 3c8c47afe412f7d7baec49e6f55db6ac5aaf12eb [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
tomhudson@google.comf13f5882012-06-25 17:27:28 +000067 bool isTextureStageEnabled(int i) const {
68 GrAssert((unsigned)i < kMaxTextures);
69 return (NULL != fTextures[i]) ||
70 (NULL != fTextureSamplers[i].getCustomStage());
71 }
72
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000073 // The mask can be alpha-only or per channel. It is applied
74 // after the colorfilter
75 void setMask(int i, GrTexture* mask) {
76 GrAssert((unsigned)i < kMaxMasks);
77 GrSafeRef(mask);
78 GrSafeUnref(fMaskTextures[i]);
79 fMaskTextures[i] = mask;
80 }
81
82 GrTexture* getMask(int i) const {
83 GrAssert((unsigned)i < kMaxMasks);
84 return fMaskTextures[i];
85 }
86
87 // mask's sampler matrix is always applied to the positions
88 // (i.e. no explicit texture coordinates)
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +000089 GrSamplerState* maskSampler(int i) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000090 GrAssert((unsigned)i < kMaxMasks);
91 return fMaskSamplers + i;
92 }
93
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +000094 const GrSamplerState& getMaskSampler(int i) const {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000095 GrAssert((unsigned)i < kMaxMasks);
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +000096 return fMaskSamplers[i];
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000097 }
98
tomhudson@google.comf13f5882012-06-25 17:27:28 +000099 bool isMaskStageEnabled(int i) const {
100 GrAssert((unsigned)i < kMaxTextures);
101 return (NULL != fMaskTextures[i]) ||
102 (NULL != fMaskSamplers[i].getCustomStage());
103 }
104
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000105 // pre-concats sampler matrices for non-NULL textures and masks
106 void preConcatActiveSamplerMatrices(const GrMatrix& matrix) {
107 for (int i = 0; i < kMaxTextures; ++i) {
108 fTextureSamplers[i].preConcatMatrix(matrix);
109 }
110 for (int i = 0; i < kMaxMasks; ++i) {
111 fMaskSamplers[i].preConcatMatrix(matrix);
112 }
113 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000114
115 // uninitialized
116 GrPaint() {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000117 for (int i = 0; i < kMaxTextures; ++i) {
118 fTextures[i] = NULL;
119 }
120 for (int i = 0; i < kMaxMasks; ++i) {
121 fMaskTextures[i] = NULL;
122 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000123 }
124
125 GrPaint(const GrPaint& paint) {
bsalomon@google.com27c9b6d2011-09-12 14:30:27 +0000126 for (int i = 0; i < kMaxTextures; ++i) {
127 fTextures[i] = NULL;
128 }
129 for (int i = 0; i < kMaxMasks; ++i) {
130 fMaskTextures[i] = NULL;
131 }
132 *this = paint;
133 }
134
135 GrPaint& operator=(const GrPaint& paint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000136 fSrcBlendCoeff = paint.fSrcBlendCoeff;
137 fDstBlendCoeff = paint.fDstBlendCoeff;
138 fAntiAlias = paint.fAntiAlias;
139 fDither = paint.fDither;
140
141 fColor = paint.fColor;
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000142 fCoverage = paint.fCoverage;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000143
Scroggo97c88c22011-05-11 14:05:25 +0000144 fColorFilterColor = paint.fColorFilterColor;
145 fColorFilterXfermode = paint.fColorFilterXfermode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000146 fColorMatrixEnabled = paint.fColorMatrixEnabled;
bsalomon@google.comdddf6f62012-03-16 17:50:37 +0000147 if (fColorMatrixEnabled) {
148 memcpy(fColorMatrix, paint.fColorMatrix, sizeof(fColorMatrix));
149 }
150
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000151 for (int i = 0; i < kMaxTextures; ++i) {
bsalomon@google.com27c9b6d2011-09-12 14:30:27 +0000152 GrSafeUnref(fTextures[i]);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000153 fTextures[i] = paint.fTextures[i];
bsalomon@google.comdddf6f62012-03-16 17:50:37 +0000154 if (NULL != fTextures[i]) {
155 fTextureSamplers[i] = paint.fTextureSamplers[i];
156 fTextures[i]->ref();
157 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000158 }
159 for (int i = 0; i < kMaxMasks; ++i) {
bsalomon@google.com27c9b6d2011-09-12 14:30:27 +0000160 GrSafeUnref(fMaskTextures[i]);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000161 fMaskTextures[i] = paint.fMaskTextures[i];
bsalomon@google.comdddf6f62012-03-16 17:50:37 +0000162 if (NULL != fMaskTextures[i]) {
163 fMaskSamplers[i] = paint.fMaskSamplers[i];
164 fMaskTextures[i]->ref();
165 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000166 }
bsalomon@google.com27c9b6d2011-09-12 14:30:27 +0000167 return *this;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000168 }
169
170 ~GrPaint() {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000171 for (int i = 0; i < kMaxTextures; ++i) {
172 GrSafeUnref(fTextures[i]);
173 }
174 for (int i = 0; i < kMaxMasks; ++i) {
175 GrSafeUnref(fMaskTextures[i]);
176 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000177 }
178
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000179 // sets paint to src-over, solid white, no texture, no mask
bsalomon@google.com27847de2011-02-22 20:59:41 +0000180 void reset() {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000181 this->resetBlend();
182 this->resetOptions();
183 this->resetColor();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000184 this->resetCoverage();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000185 this->resetTextures();
186 this->resetColorFilter();
187 this->resetMasks();
Scroggo97c88c22011-05-11 14:05:25 +0000188 }
189
190 void resetColorFilter() {
191 fColorFilterXfermode = SkXfermode::kDst_Mode;
192 fColorFilterColor = GrColorPackRGBA(0xff, 0xff, 0xff, 0xff);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000193 fColorMatrixEnabled = false;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000194 }
195
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000196 bool hasTexture() const {
197 return 0 != this->getActiveTextureStageMask();
198 }
199
200 bool hasMask() const {
201 return 0 != this->getActiveMaskStageMask();
202 }
203
204 bool hasTextureOrMask() const {
205 return this->hasTexture() || this->hasMask();
206 }
207
208 // helpers for GrContext, GrTextContext
209 int getActiveTextureStageMask() const {
210 int mask = 0;
211 for (int i = 0; i < kMaxTextures; ++i) {
212 if (NULL != fTextures[i]) {
213 mask |= 1 << (i + kFirstTextureStage);
214 }
215 }
216 return mask;
217 }
218
219 int getActiveMaskStageMask() const {
bsalomon@google.com00e17c52011-05-18 19:02:42 +0000220 int mask = 0;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000221 for (int i = 0; i < kMaxMasks; ++i) {
222 if (NULL != fMaskTextures[i]) {
223 mask |= 1 << (i + kFirstMaskStage);
224 }
225 }
226 return mask;
227 }
228
229 int getActiveStageMask() const {
230 return this->getActiveTextureStageMask() |
231 this->getActiveMaskStageMask();
232 }
233
234 // internal use
235 // GrPaint's textures and masks map to the first N stages
236 // of GrDrawTarget in that order (textures followed by masks)
237 enum {
238 kFirstTextureStage = 0,
239 kFirstMaskStage = kMaxTextures,
240 kTotalStages = kMaxTextures + kMaxMasks,
241 };
242
bsalomon@google.com27847de2011-02-22 20:59:41 +0000243private:
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000244
245 GrSamplerState fTextureSamplers[kMaxTextures];
246 GrSamplerState fMaskSamplers[kMaxMasks];
247
248 GrTexture* fTextures[kMaxTextures];
249 GrTexture* fMaskTextures[kMaxMasks];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000250
251 void resetBlend() {
bsalomon@google.com47059542012-06-06 20:51:20 +0000252 fSrcBlendCoeff = kOne_GrBlendCoeff;
253 fDstBlendCoeff = kZero_GrBlendCoeff;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000254 }
255
256 void resetOptions() {
257 fAntiAlias = false;
258 fDither = false;
259 }
260
261 void resetColor() {
262 fColor = GrColorPackRGBA(0xff, 0xff, 0xff, 0xff);
263 }
264
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000265 void resetCoverage() {
266 fCoverage = 0xff;
267 }
268
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000269 void resetTextures() {
270 for (int i = 0; i < kMaxTextures; ++i) {
271 this->setTexture(i, NULL);
bsalomon@google.com97912912011-12-06 16:30:36 +0000272 fTextureSamplers[i].reset();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000273 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000274 }
275
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000276 void resetMasks() {
277 for (int i = 0; i < kMaxMasks; ++i) {
278 this->setMask(i, NULL);
bsalomon@google.com97912912011-12-06 16:30:36 +0000279 fMaskSamplers[i].reset();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000280 }
281 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000282};
283
284#endif