blob: da52e952771ab396ec636652d6ccd71a54bb7b59 [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
reed@google.comac10a2d2010-12-22 21:39:39 +000020class GrSamplerState {
21public:
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000022
bsalomon@google.com6aab8e32011-06-21 20:32:12 +000023 GrSamplerState()
tomhudson@google.com898e7b52012-06-01 20:42:15 +000024 : fCustomStage (NULL) {
tomhudson@google.com194de082012-05-31 20:35:27 +000025 memset(this, 0, sizeof(GrSamplerState));
bsalomon@google.com97912912011-12-06 16:30:36 +000026 this->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +000027 }
28
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000029 ~GrSamplerState() {
30 GrSafeUnref(fCustomStage);
31 }
32
tomhudson@google.com02b1ea22012-04-30 20:19:07 +000033 bool operator ==(const GrSamplerState& s) const {
tomhudson@google.comb88bbd22012-05-01 12:48:07 +000034 /* We must be bit-identical as far as the CustomStage;
35 there may be multiple CustomStages that will produce
rmistry@google.comfbfcd562012-08-23 18:09:54 +000036 the same shader code and so are equivalent.
tomhudson@google.comb88bbd22012-05-01 12:48:07 +000037 Can't take the address of fWrapX because it's :8 */
38 int bitwiseRegion = (intptr_t) &fCustomStage - (intptr_t) this;
39 GrAssert(sizeof(GrSamplerState) ==
40 bitwiseRegion + sizeof(fCustomStage));
41 return !memcmp(this, &s, bitwiseRegion) &&
42 ((fCustomStage == s.fCustomStage) ||
43 (fCustomStage && s.fCustomStage &&
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000044 (fCustomStage->getFactory() ==
45 s.fCustomStage->getFactory()) &&
bsalomon@google.comb505a122012-05-31 18:40:36 +000046 fCustomStage->isEqual(*s.fCustomStage)));
tomhudson@google.com02b1ea22012-04-30 20:19:07 +000047 }
48 bool operator !=(const GrSamplerState& s) const { return !(*this == s); }
49
tomhudson@google.com0bdbed32012-06-01 19:50:02 +000050 GrSamplerState& operator =(const GrSamplerState& s) {
tomhudson@google.com11175592012-05-31 14:23:28 +000051 fMatrix = s.fMatrix;
tomhudson@google.com50e4ce02012-06-19 15:27:50 +000052 GrSafeAssign(fCustomStage, s.fCustomStage);
tomhudson@google.com02b1ea22012-04-30 20:19:07 +000053 return *this;
54 }
55
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000056 const GrMatrix& getMatrix() const { return fMatrix; }
reed@google.comac10a2d2010-12-22 21:39:39 +000057
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000058 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000059 * Multiplies the current sampler matrix a matrix
60 *
61 * After this call M' = M*m where M is the old matrix, m is the parameter
62 * to this function, and M' is the new matrix. (We consider points to
rmistry@google.comfbfcd562012-08-23 18:09:54 +000063 * be column vectors so tex cood vector t is transformed by matrix X as
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000064 * t' = X*t.)
65 *
66 * @param matrix the matrix used to modify the matrix.
67 */
68 void preConcatMatrix(const GrMatrix& matrix) { fMatrix.preConcat(matrix); }
69
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +000070 /**
71 * Do not call this function. It will be removed soon.
72 */
73 void setMatrixDeprecated(const GrMatrix& matrix) { fMatrix = matrix; }
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000074
bsalomon@google.comaa814fe2011-12-12 18:45:07 +000075 void reset() {
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000076 fMatrix.reset();
77 GrSafeSetNull(fCustomStage);
bsalomon@google.com1e266f82011-12-12 16:11:33 +000078 }
bsalomon@google.com97912912011-12-06 16:30:36 +000079
tomhudson@google.com83e5eb82012-06-04 19:58:30 +000080 GrCustomStage* setCustomStage(GrCustomStage* stage) {
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000081 GrSafeAssign(fCustomStage, stage);
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +000082 fMatrix.reset();
tomhudson@google.com83e5eb82012-06-04 19:58:30 +000083 return stage;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000084 }
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +000085
86 GrCustomStage* setCustomStage(GrCustomStage* stage, const GrMatrix& matrix) {
87 GrSafeAssign(fCustomStage, stage);
88 fMatrix = matrix;
89 return stage;
90 }
91
bsalomon@google.comcddaf342012-07-30 13:09:05 +000092 const GrCustomStage* getCustomStage() const { return fCustomStage; }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000093
reed@google.comac10a2d2010-12-22 21:39:39 +000094private:
tomhudson@google.com898e7b52012-06-01 20:42:15 +000095 GrMatrix fMatrix;
reed@google.comac10a2d2010-12-22 21:39:39 +000096
tomhudson@google.com02b1ea22012-04-30 20:19:07 +000097 GrCustomStage* fCustomStage;
reed@google.comac10a2d2010-12-22 21:39:39 +000098};
99
100#endif
101