blob: 8b1bf2c51eb5899e74a3e1f3517883da1e169131 [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) {
tomhudson@google.come742bf02012-07-13 19:54:19 +0000152 GrSafeAssign(fTextures[i], paint.fTextures[i]);
153 if (paint.isTextureStageEnabled(i)) {
bsalomon@google.comdddf6f62012-03-16 17:50:37 +0000154 fTextureSamplers[i] = paint.fTextureSamplers[i];
bsalomon@google.comdddf6f62012-03-16 17:50:37 +0000155 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000156 }
157 for (int i = 0; i < kMaxMasks; ++i) {
tomhudson@google.come742bf02012-07-13 19:54:19 +0000158 GrSafeAssign(fMaskTextures[i], paint.fMaskTextures[i]);
159 if (paint.isMaskStageEnabled(i)) {
bsalomon@google.comdddf6f62012-03-16 17:50:37 +0000160 fMaskSamplers[i] = paint.fMaskSamplers[i];
bsalomon@google.comdddf6f62012-03-16 17:50:37 +0000161 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000162 }
bsalomon@google.com27c9b6d2011-09-12 14:30:27 +0000163 return *this;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000164 }
165
166 ~GrPaint() {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000167 for (int i = 0; i < kMaxTextures; ++i) {
168 GrSafeUnref(fTextures[i]);
169 }
170 for (int i = 0; i < kMaxMasks; ++i) {
171 GrSafeUnref(fMaskTextures[i]);
172 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000173 }
174
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000175 // sets paint to src-over, solid white, no texture, no mask
bsalomon@google.com27847de2011-02-22 20:59:41 +0000176 void reset() {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000177 this->resetBlend();
178 this->resetOptions();
179 this->resetColor();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000180 this->resetCoverage();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000181 this->resetTextures();
182 this->resetColorFilter();
183 this->resetMasks();
Scroggo97c88c22011-05-11 14:05:25 +0000184 }
185
186 void resetColorFilter() {
187 fColorFilterXfermode = SkXfermode::kDst_Mode;
188 fColorFilterColor = GrColorPackRGBA(0xff, 0xff, 0xff, 0xff);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000189 fColorMatrixEnabled = false;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000190 }
191
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000192 bool hasTexture() const {
193 return 0 != this->getActiveTextureStageMask();
194 }
195
196 bool hasMask() const {
197 return 0 != this->getActiveMaskStageMask();
198 }
199
200 bool hasTextureOrMask() const {
201 return this->hasTexture() || this->hasMask();
202 }
203
204 // helpers for GrContext, GrTextContext
205 int getActiveTextureStageMask() const {
206 int mask = 0;
207 for (int i = 0; i < kMaxTextures; ++i) {
tomhudson@google.come742bf02012-07-13 19:54:19 +0000208 if ((NULL != fTextures[i]) ||
209 (NULL != fTextureSamplers[i].getCustomStage())) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000210 mask |= 1 << (i + kFirstTextureStage);
211 }
212 }
213 return mask;
214 }
215
216 int getActiveMaskStageMask() const {
bsalomon@google.com00e17c52011-05-18 19:02:42 +0000217 int mask = 0;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000218 for (int i = 0; i < kMaxMasks; ++i) {
tomhudson@google.come742bf02012-07-13 19:54:19 +0000219 if ((NULL != fMaskTextures[i]) ||
220 (NULL != fMaskSamplers[i].getCustomStage())) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000221 mask |= 1 << (i + kFirstMaskStage);
222 }
223 }
224 return mask;
225 }
226
227 int getActiveStageMask() const {
228 return this->getActiveTextureStageMask() |
229 this->getActiveMaskStageMask();
230 }
231
232 // internal use
233 // GrPaint's textures and masks map to the first N stages
234 // of GrDrawTarget in that order (textures followed by masks)
235 enum {
236 kFirstTextureStage = 0,
237 kFirstMaskStage = kMaxTextures,
238 kTotalStages = kMaxTextures + kMaxMasks,
239 };
240
bsalomon@google.com27847de2011-02-22 20:59:41 +0000241private:
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000242
243 GrSamplerState fTextureSamplers[kMaxTextures];
244 GrSamplerState fMaskSamplers[kMaxMasks];
245
246 GrTexture* fTextures[kMaxTextures];
247 GrTexture* fMaskTextures[kMaxMasks];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000248
249 void resetBlend() {
bsalomon@google.com47059542012-06-06 20:51:20 +0000250 fSrcBlendCoeff = kOne_GrBlendCoeff;
251 fDstBlendCoeff = kZero_GrBlendCoeff;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000252 }
253
254 void resetOptions() {
255 fAntiAlias = false;
256 fDither = false;
257 }
258
259 void resetColor() {
260 fColor = GrColorPackRGBA(0xff, 0xff, 0xff, 0xff);
261 }
262
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000263 void resetCoverage() {
264 fCoverage = 0xff;
265 }
266
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000267 void resetTextures() {
268 for (int i = 0; i < kMaxTextures; ++i) {
269 this->setTexture(i, NULL);
bsalomon@google.com97912912011-12-06 16:30:36 +0000270 fTextureSamplers[i].reset();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000271 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000272 }
273
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000274 void resetMasks() {
275 for (int i = 0; i < kMaxMasks; ++i) {
276 this->setMask(i, NULL);
bsalomon@google.com97912912011-12-06 16:30:36 +0000277 fMaskSamplers[i].reset();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000278 }
279 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000280};
281
282#endif