blob: 9f220e07a8cb60e35cd21831d564320bc41adbbb [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 memset(fColorMatrix, 0, sizeof(fColorMatrix));
176 fColorMatrixEnabled = false;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000177 }
178
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000179 bool hasTexture() const {
180 return 0 != this->getActiveTextureStageMask();
181 }
182
183 bool hasMask() const {
184 return 0 != this->getActiveMaskStageMask();
185 }
186
187 bool hasTextureOrMask() const {
188 return this->hasTexture() || this->hasMask();
189 }
190
191 // helpers for GrContext, GrTextContext
192 int getActiveTextureStageMask() const {
193 int mask = 0;
194 for (int i = 0; i < kMaxTextures; ++i) {
195 if (NULL != fTextures[i]) {
196 mask |= 1 << (i + kFirstTextureStage);
197 }
198 }
199 return mask;
200 }
201
202 int getActiveMaskStageMask() const {
bsalomon@google.com00e17c52011-05-18 19:02:42 +0000203 int mask = 0;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000204 for (int i = 0; i < kMaxMasks; ++i) {
205 if (NULL != fMaskTextures[i]) {
206 mask |= 1 << (i + kFirstMaskStage);
207 }
208 }
209 return mask;
210 }
211
212 int getActiveStageMask() const {
213 return this->getActiveTextureStageMask() |
214 this->getActiveMaskStageMask();
215 }
216
217 // internal use
218 // GrPaint's textures and masks map to the first N stages
219 // of GrDrawTarget in that order (textures followed by masks)
220 enum {
221 kFirstTextureStage = 0,
222 kFirstMaskStage = kMaxTextures,
223 kTotalStages = kMaxTextures + kMaxMasks,
224 };
225
bsalomon@google.com27847de2011-02-22 20:59:41 +0000226private:
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000227
228 GrSamplerState fTextureSamplers[kMaxTextures];
229 GrSamplerState fMaskSamplers[kMaxMasks];
230
231 GrTexture* fTextures[kMaxTextures];
232 GrTexture* fMaskTextures[kMaxMasks];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000233
234 void resetBlend() {
235 fSrcBlendCoeff = kOne_BlendCoeff;
236 fDstBlendCoeff = kZero_BlendCoeff;
237 }
238
239 void resetOptions() {
240 fAntiAlias = false;
241 fDither = false;
242 }
243
244 void resetColor() {
245 fColor = GrColorPackRGBA(0xff, 0xff, 0xff, 0xff);
246 }
247
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000248 void resetCoverage() {
249 fCoverage = 0xff;
250 }
251
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000252 void resetTextures() {
253 for (int i = 0; i < kMaxTextures; ++i) {
254 this->setTexture(i, NULL);
bsalomon@google.com97912912011-12-06 16:30:36 +0000255 fTextureSamplers[i].reset();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000256 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000257 }
258
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000259 void resetMasks() {
260 for (int i = 0; i < kMaxMasks; ++i) {
261 this->setMask(i, NULL);
bsalomon@google.com97912912011-12-06 16:30:36 +0000262 fMaskSamplers[i].reset();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000263 }
264 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000265};
266
267#endif