1 /*
2  * Copyright 2015 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef GrTextureParams_DEFINED
9 #define GrTextureParams_DEFINED
10 
11 #include "GrTypes.h"
12 #include "SkShader.h"
13 
14 /**
15  * Represents the filtering and tile modes used to access a texture.
16  */
17 class GrTextureParams {
18 public:
ClampNoFilter()19     static const GrTextureParams& ClampNoFilter() {
20         static const GrTextureParams gParams;
21         return gParams;
22     }
ClampBilerp()23     static const GrTextureParams& ClampBilerp() {
24         static const GrTextureParams gParams(SkShader::kClamp_TileMode, kBilerp_FilterMode);
25         return gParams;
26     }
27 
GrTextureParams()28     GrTextureParams() {
29         this->reset();
30     }
31 
32     enum FilterMode {
33         kNone_FilterMode,
34         kBilerp_FilterMode,
35         kMipMap_FilterMode
36     };
37 
GrTextureParams(SkShader::TileMode tileXAndY,FilterMode filterMode)38     GrTextureParams(SkShader::TileMode tileXAndY, FilterMode filterMode) {
39         this->reset(tileXAndY, filterMode);
40     }
41 
GrTextureParams(const SkShader::TileMode tileModes[2],FilterMode filterMode)42     GrTextureParams(const SkShader::TileMode tileModes[2], FilterMode filterMode) {
43         this->reset(tileModes, filterMode);
44     }
45 
GrTextureParams(const GrTextureParams & params)46     GrTextureParams(const GrTextureParams& params) {
47         *this = params;
48     }
49 
50     GrTextureParams& operator= (const GrTextureParams& params) {
51         fTileModes[0] = params.fTileModes[0];
52         fTileModes[1] = params.fTileModes[1];
53         fFilterMode = params.fFilterMode;
54         return *this;
55     }
56 
reset()57     void reset() {
58         this->reset(SkShader::kClamp_TileMode, kNone_FilterMode);
59     }
60 
reset(SkShader::TileMode tileXAndY,FilterMode filterMode)61     void reset(SkShader::TileMode tileXAndY, FilterMode filterMode) {
62         fTileModes[0] = fTileModes[1] = tileXAndY;
63         fFilterMode = filterMode;
64     }
65 
reset(const SkShader::TileMode tileModes[2],FilterMode filterMode)66     void reset(const SkShader::TileMode tileModes[2], FilterMode filterMode) {
67         fTileModes[0] = tileModes[0];
68         fTileModes[1] = tileModes[1];
69         fFilterMode = filterMode;
70     }
71 
setClampNoFilter()72     void setClampNoFilter() {
73         fTileModes[0] = fTileModes[1] = SkShader::kClamp_TileMode;
74         fFilterMode = kNone_FilterMode;
75     }
76 
setClamp()77     void setClamp() {
78         fTileModes[0] = fTileModes[1] = SkShader::kClamp_TileMode;
79     }
80 
setFilterMode(FilterMode filterMode)81     void setFilterMode(FilterMode filterMode) { fFilterMode = filterMode; }
82 
setTileModeX(const SkShader::TileMode tm)83     void setTileModeX(const SkShader::TileMode tm) { fTileModes[0] = tm; }
setTileModeY(const SkShader::TileMode tm)84     void setTileModeY(const SkShader::TileMode tm) { fTileModes[1] = tm; }
setTileModeXAndY(const SkShader::TileMode tm)85     void setTileModeXAndY(const SkShader::TileMode tm) { fTileModes[0] = fTileModes[1] = tm; }
86 
getTileModeX()87     SkShader::TileMode getTileModeX() const { return fTileModes[0]; }
88 
getTileModeY()89     SkShader::TileMode getTileModeY() const { return fTileModes[1]; }
90 
isTiled()91     bool isTiled() const {
92         return SkShader::kClamp_TileMode != fTileModes[0] ||
93                SkShader::kClamp_TileMode != fTileModes[1];
94     }
95 
filterMode()96     FilterMode filterMode() const { return fFilterMode; }
97 
98     bool operator== (const GrTextureParams& other) const {
99         return fTileModes[0] == other.fTileModes[0] &&
100                fTileModes[1] == other.fTileModes[1] &&
101                fFilterMode == other.fFilterMode;
102     }
103 
104     bool operator!= (const GrTextureParams& other) const { return !(*this == other); }
105 
106 private:
107     SkShader::TileMode fTileModes[2];
108     FilterMode         fFilterMode;
109 };
110 #endif
111