blob: 04fbe007386c37a0723fd5e48fbb97c90e1f3238 [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; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000133
bsalomon@google.comb8670992012-07-25 21:27:09 +0000134 GrTextureParams* textureParams() { return &fTextureParams; }
135 const GrTextureParams& getTextureParams() const { return fTextureParams; }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000136 /**
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000137 * Access the sampler's matrix. See SampleMode for explanation of
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000138 * relationship between the matrix and sample mode.
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000139 */
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000140 GrMatrix* matrix() { return &fMatrix; }
141
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000142 /**
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000143 * Swaps the R and B components when reading from the texture. Has no effect
144 * if the texture is alpha only.
145 */
146 void setRAndBSwap(bool swap) { fSwapRAndB = swap; }
147
148 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000149 * Multiplies the current sampler matrix a matrix
150 *
151 * After this call M' = M*m where M is the old matrix, m is the parameter
152 * to this function, and M' is the new matrix. (We consider points to
153 * be column vectors so tex cood vector t is transformed by matrix X as
154 * t' = X*t.)
155 *
156 * @param matrix the matrix used to modify the matrix.
157 */
158 void preConcatMatrix(const GrMatrix& matrix) { fMatrix.preConcat(matrix); }
159
bsalomon@google.comb8670992012-07-25 21:27:09 +0000160 void reset(SkShader::TileMode tileXAndY,
161 bool filter,
bsalomon@google.com97912912011-12-06 16:30:36 +0000162 const GrMatrix& matrix) {
bsalomon@google.comb8670992012-07-25 21:27:09 +0000163 fTextureParams.reset(tileXAndY, filter);
bsalomon@google.com97912912011-12-06 16:30:36 +0000164 fMatrix = matrix;
bsalomon@google.com97912912011-12-06 16:30:36 +0000165 fSwapRAndB = false;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000166 GrSafeSetNull(fCustomStage);
bsalomon@google.com97912912011-12-06 16:30:36 +0000167 }
bsalomon@google.comb8670992012-07-25 21:27:09 +0000168 void reset(SkShader::TileMode wrapXAndY, bool filter) {
bsalomon@google.comb505a122012-05-31 18:40:36 +0000169 this->reset(wrapXAndY, filter, GrMatrix::I());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000170 }
bsalomon@google.com1e266f82011-12-12 16:11:33 +0000171 void reset(const GrMatrix& matrix) {
bsalomon@google.comb8670992012-07-25 21:27:09 +0000172 this->reset(kTileModeDefault, kBilerpDefault, matrix);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000173 }
174 void reset() {
bsalomon@google.comb8670992012-07-25 21:27:09 +0000175 this->reset(kTileModeDefault, kBilerpDefault, GrMatrix::I());
bsalomon@google.com1e266f82011-12-12 16:11:33 +0000176 }
bsalomon@google.com97912912011-12-06 16:30:36 +0000177
tomhudson@google.com83e5eb82012-06-04 19:58:30 +0000178 GrCustomStage* setCustomStage(GrCustomStage* stage) {
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000179 GrSafeAssign(fCustomStage, stage);
tomhudson@google.com83e5eb82012-06-04 19:58:30 +0000180 return stage;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000181 }
182 GrCustomStage* getCustomStage() const { return fCustomStage; }
183
reed@google.comac10a2d2010-12-22 21:39:39 +0000184private:
bsalomon@google.comb8670992012-07-25 21:27:09 +0000185 GrTextureParams fTextureParams;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000186 bool fSwapRAndB;
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000187 GrMatrix fMatrix;
reed@google.comac10a2d2010-12-22 21:39:39 +0000188
tomhudson@google.com02b1ea22012-04-30 20:19:07 +0000189 GrCustomStage* fCustomStage;
reed@google.comac10a2d2010-12-22 21:39:39 +0000190};
191
192#endif
193