blob: 59f622b359d3987842c6eb31320f93771fe86a6f [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;
36
37 GrColor fColor;
38
Scroggo97c88c22011-05-11 14:05:25 +000039 GrColor fColorFilterColor;
40 SkXfermode::Mode fColorFilterXfermode;
41
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000042 void setTexture(int i, GrTexture* texture) {
43 GrAssert((unsigned)i < kMaxTextures);
bsalomon@google.com27847de2011-02-22 20:59:41 +000044 GrSafeRef(texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000045 GrSafeUnref(fTextures[i]);
46 fTextures[i] = texture;
bsalomon@google.com27847de2011-02-22 20:59:41 +000047 }
48
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000049 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.com27847de2011-02-22 20:59:41 +000099
100 // uninitialized
101 GrPaint() {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000102 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.com27847de2011-02-22 20:59:41 +0000108 }
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
Scroggo97c88c22011-05-11 14:05:25 +0000118 fColorFilterColor = paint.fColorFilterColor;
119 fColorFilterXfermode = paint.fColorFilterXfermode;
120
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000121 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.com27847de2011-02-22 20:59:41 +0000131 }
132
133 ~GrPaint() {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000134 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.com27847de2011-02-22 20:59:41 +0000140 }
141
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000142 // sets paint to src-over, solid white, no texture, no mask
bsalomon@google.com27847de2011-02-22 20:59:41 +0000143 void reset() {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000144 this->resetBlend();
145 this->resetOptions();
146 this->resetColor();
147 this->resetTextures();
148 this->resetColorFilter();
149 this->resetMasks();
Scroggo97c88c22011-05-11 14:05:25 +0000150 }
151
152 void resetColorFilter() {
153 fColorFilterXfermode = SkXfermode::kDst_Mode;
154 fColorFilterColor = GrColorPackRGBA(0xff, 0xff, 0xff, 0xff);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000155 }
156
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000157 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.com00e17c52011-05-18 19:02:42 +0000181 int mask = 0;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000182 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.com27847de2011-02-22 20:59:41 +0000204private:
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000205
206 GrSamplerState fTextureSamplers[kMaxTextures];
207 GrSamplerState fMaskSamplers[kMaxMasks];
208
209 GrTexture* fTextures[kMaxTextures];
210 GrTexture* fMaskTextures[kMaxMasks];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000211
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.com26c2d0a2011-05-17 20:15:30 +0000226 void resetTextures() {
227 for (int i = 0; i < kMaxTextures; ++i) {
228 this->setTexture(i, NULL);
229 fTextureSamplers[i].setClampNoFilter();
230 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000231 }
232
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000233 void resetMasks() {
234 for (int i = 0; i < kMaxMasks; ++i) {
235 this->setMask(i, NULL);
236 fMaskSamplers[i].setClampNoFilter();
237 }
238 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000239};
240
241#endif