blob: 385dba7bddf6bdadc96521ea612f27ddadc97ed2 [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.comb8670992012-07-25 21:27:09 +000022 static const bool kBilerpDefault = false;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000023
bsalomon@google.comb8670992012-07-25 21:27:09 +000024 static const SkShader::TileMode kTileModeDefault = SkShader::kClamp_TileMode;
reed@google.comac10a2d2010-12-22 21:39:39 +000025
26 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000027 * Default sampler state is set to clamp, use normal sampling mode, be
28 * unfiltered, and use identity matrix.
reed@google.comac10a2d2010-12-22 21:39:39 +000029 */
bsalomon@google.com6aab8e32011-06-21 20:32:12 +000030 GrSamplerState()
tomhudson@google.com898e7b52012-06-01 20:42:15 +000031 : fCustomStage (NULL) {
tomhudson@google.com194de082012-05-31 20:35:27 +000032 memset(this, 0, sizeof(GrSamplerState));
bsalomon@google.com97912912011-12-06 16:30:36 +000033 this->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +000034 }
35
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000036 ~GrSamplerState() {
37 GrSafeUnref(fCustomStage);
38 }
39
tomhudson@google.com02b1ea22012-04-30 20:19:07 +000040 bool operator ==(const GrSamplerState& s) const {
tomhudson@google.comb88bbd22012-05-01 12:48:07 +000041 /* We must be bit-identical as far as the CustomStage;
42 there may be multiple CustomStages that will produce
rmistry@google.comfbfcd562012-08-23 18:09:54 +000043 the same shader code and so are equivalent.
tomhudson@google.comb88bbd22012-05-01 12:48:07 +000044 Can't take the address of fWrapX because it's :8 */
45 int bitwiseRegion = (intptr_t) &fCustomStage - (intptr_t) this;
46 GrAssert(sizeof(GrSamplerState) ==
47 bitwiseRegion + sizeof(fCustomStage));
48 return !memcmp(this, &s, bitwiseRegion) &&
49 ((fCustomStage == s.fCustomStage) ||
50 (fCustomStage && s.fCustomStage &&
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000051 (fCustomStage->getFactory() ==
52 s.fCustomStage->getFactory()) &&
bsalomon@google.comb505a122012-05-31 18:40:36 +000053 fCustomStage->isEqual(*s.fCustomStage)));
tomhudson@google.com02b1ea22012-04-30 20:19:07 +000054 }
55 bool operator !=(const GrSamplerState& s) const { return !(*this == s); }
56
tomhudson@google.com0bdbed32012-06-01 19:50:02 +000057 GrSamplerState& operator =(const GrSamplerState& s) {
tomhudson@google.com11175592012-05-31 14:23:28 +000058 fMatrix = s.fMatrix;
tomhudson@google.com50e4ce02012-06-19 15:27:50 +000059 GrSafeAssign(fCustomStage, s.fCustomStage);
tomhudson@google.com02b1ea22012-04-30 20:19:07 +000060 return *this;
61 }
62
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000063 const GrMatrix& getMatrix() const { return fMatrix; }
reed@google.comac10a2d2010-12-22 21:39:39 +000064
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000065 /**
bsalomon@google.comaa814fe2011-12-12 18:45:07 +000066 * Access the sampler's matrix. See SampleMode for explanation of
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000067 * relationship between the matrix and sample mode.
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000068 */
bsalomon@google.comaa814fe2011-12-12 18:45:07 +000069 GrMatrix* matrix() { return &fMatrix; }
70
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000071 /**
72 * Multiplies the current sampler matrix a matrix
73 *
74 * After this call M' = M*m where M is the old matrix, m is the parameter
75 * to this function, and M' is the new matrix. (We consider points to
rmistry@google.comfbfcd562012-08-23 18:09:54 +000076 * be column vectors so tex cood vector t is transformed by matrix X as
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000077 * t' = X*t.)
78 *
79 * @param matrix the matrix used to modify the matrix.
80 */
81 void preConcatMatrix(const GrMatrix& matrix) { fMatrix.preConcat(matrix); }
82
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000083 void reset(const GrMatrix& matrix) {
bsalomon@google.com97912912011-12-06 16:30:36 +000084 fMatrix = matrix;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000085 GrSafeSetNull(fCustomStage);
bsalomon@google.com97912912011-12-06 16:30:36 +000086 }
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000087
bsalomon@google.comaa814fe2011-12-12 18:45:07 +000088 void reset() {
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000089 fMatrix.reset();
90 GrSafeSetNull(fCustomStage);
bsalomon@google.com1e266f82011-12-12 16:11:33 +000091 }
bsalomon@google.com97912912011-12-06 16:30:36 +000092
tomhudson@google.com83e5eb82012-06-04 19:58:30 +000093 GrCustomStage* setCustomStage(GrCustomStage* stage) {
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000094 GrSafeAssign(fCustomStage, stage);
tomhudson@google.com83e5eb82012-06-04 19:58:30 +000095 return stage;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000096 }
bsalomon@google.comcddaf342012-07-30 13:09:05 +000097 const GrCustomStage* getCustomStage() const { return fCustomStage; }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000098
reed@google.comac10a2d2010-12-22 21:39:39 +000099private:
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000100 GrMatrix fMatrix;
reed@google.comac10a2d2010-12-22 21:39:39 +0000101
tomhudson@google.com02b1ea22012-04-30 20:19:07 +0000102 GrCustomStage* fCustomStage;
reed@google.comac10a2d2010-12-22 21:39:39 +0000103};
104
105#endif
106