blob: f87b9b4e10111485e865dd0a935ed92563e38723 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 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.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#ifndef GrSamplerState_DEFINED
12#define GrSamplerState_DEFINED
13
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000014#include "GrCustomStage.h"
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000015#include "GrMatrix.h"
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000016#include "GrTypes.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000017
bsalomon@google.comb8670992012-07-25 21:27:09 +000018#include "SkShader.h"
19
20class GrTextureParams {
21public:
22 GrTextureParams() {
23 this->reset();
24 }
25
26 GrTextureParams(const GrTextureParams& params) {
27 *this = params;
28 }
29
30 GrTextureParams& operator =(const GrTextureParams& params) {
31 fTileModes[0] = params.fTileModes[0];
32 fTileModes[1] = params.fTileModes[1];
33 fBilerp = params.fBilerp;
34 return *this;
35 }
36
37 void reset() {
38 this->reset(SkShader::kClamp_TileMode, false);
39 }
40
41 void reset(SkShader::TileMode tileXAndY, bool filter) {
42 fTileModes[0] = fTileModes[1] = tileXAndY;
43 fBilerp = filter;
44 }
45 void reset(SkShader::TileMode tileModes[2], bool filter) {
46 fTileModes[0] = tileModes[0];
47 fTileModes[1] = tileModes[1];
48 fBilerp = filter;
49 }
50
51 void setClampNoFilter() {
52 fTileModes[0] = fTileModes[1] = SkShader::kClamp_TileMode;
53 fBilerp = false;
54 }
55
56 void setClamp() {
57 fTileModes[0] = fTileModes[1] = SkShader::kClamp_TileMode;
58 }
59
60 void setBilerp(bool bilerp) { fBilerp = bilerp; }
61
62 void setTileModeX(const SkShader::TileMode tm) { fTileModes[0] = tm; }
63 void setTileModeY(const SkShader::TileMode tm) { fTileModes[1] = tm; }
64 void setTileModeXAndY(const SkShader::TileMode tm) { fTileModes[0] = fTileModes[1] = tm; }
65
66 SkShader::TileMode getTileModeX() const { return fTileModes[0]; }
67
68 SkShader::TileMode getTileModeY() const { return fTileModes[1]; }
69
70 bool isTiled() const {
71 return SkShader::kClamp_TileMode != fTileModes[0] ||
72 SkShader::kClamp_TileMode != fTileModes[1];
73 }
74
75 bool isBilerp() const { return fBilerp; }
76
77private:
78
79 SkShader::TileMode fTileModes[2];
80 bool fBilerp;
81};
82
reed@google.comac10a2d2010-12-22 21:39:39 +000083class GrSamplerState {
84public:
bsalomon@google.comb8670992012-07-25 21:27:09 +000085 static const bool kBilerpDefault = false;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000086
bsalomon@google.comb8670992012-07-25 21:27:09 +000087 static const SkShader::TileMode kTileModeDefault = SkShader::kClamp_TileMode;
reed@google.comac10a2d2010-12-22 21:39:39 +000088
89 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000090 * Default sampler state is set to clamp, use normal sampling mode, be
91 * unfiltered, and use identity matrix.
reed@google.comac10a2d2010-12-22 21:39:39 +000092 */
bsalomon@google.com6aab8e32011-06-21 20:32:12 +000093 GrSamplerState()
tomhudson@google.com898e7b52012-06-01 20:42:15 +000094 : fCustomStage (NULL) {
tomhudson@google.com194de082012-05-31 20:35:27 +000095 memset(this, 0, sizeof(GrSamplerState));
bsalomon@google.com97912912011-12-06 16:30:36 +000096 this->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +000097 }
98
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000099 ~GrSamplerState() {
100 GrSafeUnref(fCustomStage);
101 }
102
tomhudson@google.com02b1ea22012-04-30 20:19:07 +0000103 bool operator ==(const GrSamplerState& s) const {
tomhudson@google.comb88bbd22012-05-01 12:48:07 +0000104 /* We must be bit-identical as far as the CustomStage;
105 there may be multiple CustomStages that will produce
106 the same shader code and so are equivalent.
107 Can't take the address of fWrapX because it's :8 */
108 int bitwiseRegion = (intptr_t) &fCustomStage - (intptr_t) this;
109 GrAssert(sizeof(GrSamplerState) ==
110 bitwiseRegion + sizeof(fCustomStage));
111 return !memcmp(this, &s, bitwiseRegion) &&
112 ((fCustomStage == s.fCustomStage) ||
113 (fCustomStage && s.fCustomStage &&
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000114 (fCustomStage->getFactory() ==
115 s.fCustomStage->getFactory()) &&
bsalomon@google.comb505a122012-05-31 18:40:36 +0000116 fCustomStage->isEqual(*s.fCustomStage)));
tomhudson@google.com02b1ea22012-04-30 20:19:07 +0000117 }
118 bool operator !=(const GrSamplerState& s) const { return !(*this == s); }
119
tomhudson@google.com0bdbed32012-06-01 19:50:02 +0000120 GrSamplerState& operator =(const GrSamplerState& s) {
tomhudson@google.com11175592012-05-31 14:23:28 +0000121 // memcpy() breaks refcounting
bsalomon@google.comb8670992012-07-25 21:27:09 +0000122 fTextureParams = s.fTextureParams;
tomhudson@google.com11175592012-05-31 14:23:28 +0000123 fMatrix = s.fMatrix;
124 fSwapRAndB = s.fSwapRAndB;
tomhudson@google.com11175592012-05-31 14:23:28 +0000125
tomhudson@google.com50e4ce02012-06-19 15:27:50 +0000126 GrSafeAssign(fCustomStage, s.fCustomStage);
tomhudson@google.com11175592012-05-31 14:23:28 +0000127
tomhudson@google.com02b1ea22012-04-30 20:19:07 +0000128 return *this;
129 }
130
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000131 const GrMatrix& getMatrix() const { return fMatrix; }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000132 bool swapsRAndB() const { return fSwapRAndB; }
bsalomon@google.com0342a852012-08-20 19:22:38 +0000133 bool premultiply() const { return fPremultiply; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000134
bsalomon@google.comb8670992012-07-25 21:27:09 +0000135 GrTextureParams* textureParams() { return &fTextureParams; }
136 const GrTextureParams& getTextureParams() const { return fTextureParams; }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000137 /**
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000138 * Access the sampler's matrix. See SampleMode for explanation of
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000139 * relationship between the matrix and sample mode.
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000140 */
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000141 GrMatrix* matrix() { return &fMatrix; }
142
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000143 /**
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000144 * Swaps the R and B components when reading from the texture. Has no effect
145 * if the texture is alpha only.
146 */
147 void setRAndBSwap(bool swap) { fSwapRAndB = swap; }
148
149 /**
bsalomon@google.com0342a852012-08-20 19:22:38 +0000150 * If the texture is RGBA/BGRA 8888 config then its rgb components will be
151 * multiplied by its a component after the texture read.
152 **/
153 void setPremultiply(bool premul) { fPremultiply = premul; }
154
155 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000156 * Multiplies the current sampler matrix a matrix
157 *
158 * After this call M' = M*m where M is the old matrix, m is the parameter
159 * to this function, and M' is the new matrix. (We consider points to
160 * be column vectors so tex cood vector t is transformed by matrix X as
161 * t' = X*t.)
162 *
163 * @param matrix the matrix used to modify the matrix.
164 */
165 void preConcatMatrix(const GrMatrix& matrix) { fMatrix.preConcat(matrix); }
166
bsalomon@google.comb8670992012-07-25 21:27:09 +0000167 void reset(SkShader::TileMode tileXAndY,
168 bool filter,
bsalomon@google.com97912912011-12-06 16:30:36 +0000169 const GrMatrix& matrix) {
bsalomon@google.comb8670992012-07-25 21:27:09 +0000170 fTextureParams.reset(tileXAndY, filter);
bsalomon@google.com97912912011-12-06 16:30:36 +0000171 fMatrix = matrix;
bsalomon@google.com97912912011-12-06 16:30:36 +0000172 fSwapRAndB = false;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000173 fPremultiply = false;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000174 GrSafeSetNull(fCustomStage);
bsalomon@google.com97912912011-12-06 16:30:36 +0000175 }
bsalomon@google.comb8670992012-07-25 21:27:09 +0000176 void reset(SkShader::TileMode wrapXAndY, bool filter) {
bsalomon@google.comb505a122012-05-31 18:40:36 +0000177 this->reset(wrapXAndY, filter, GrMatrix::I());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000178 }
bsalomon@google.com1e266f82011-12-12 16:11:33 +0000179 void reset(const GrMatrix& matrix) {
bsalomon@google.comb8670992012-07-25 21:27:09 +0000180 this->reset(kTileModeDefault, kBilerpDefault, matrix);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000181 }
182 void reset() {
bsalomon@google.comb8670992012-07-25 21:27:09 +0000183 this->reset(kTileModeDefault, kBilerpDefault, GrMatrix::I());
bsalomon@google.com1e266f82011-12-12 16:11:33 +0000184 }
bsalomon@google.com97912912011-12-06 16:30:36 +0000185
tomhudson@google.com83e5eb82012-06-04 19:58:30 +0000186 GrCustomStage* setCustomStage(GrCustomStage* stage) {
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000187 GrSafeAssign(fCustomStage, stage);
tomhudson@google.com83e5eb82012-06-04 19:58:30 +0000188 return stage;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000189 }
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000190 const GrCustomStage* getCustomStage() const { return fCustomStage; }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000191
reed@google.comac10a2d2010-12-22 21:39:39 +0000192private:
bsalomon@google.comb8670992012-07-25 21:27:09 +0000193 GrTextureParams fTextureParams;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000194 bool fSwapRAndB;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000195 bool fPremultiply; // temporary, will be replaced soon by a custom stage.
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000196 GrMatrix fMatrix;
reed@google.comac10a2d2010-12-22 21:39:39 +0000197
tomhudson@google.com02b1ea22012-04-30 20:19:07 +0000198 GrCustomStage* fCustomStage;
reed@google.comac10a2d2010-12-22 21:39:39 +0000199};
200
201#endif
202