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 GrSamplerState_DEFINED
9#define GrSamplerState_DEFINED
10
11#include "include/gpu/GrTypes.h"
12
13/**
14 * Represents the filtering and tile modes used to access a texture.
15 */
16class GrSamplerState {
17public:
18 enum class Filter : uint8_t { kNearest, kBilerp, kMipMap, kLast = kMipMap };
19 enum class WrapMode : uint8_t { kClamp, kRepeat, kMirrorRepeat, kClampToBorder,
20 kLast = kClampToBorder };
21
22 static constexpr int kFilterCount = static_cast<int>(Filter::kLast) + 1;
23 static constexpr int kWrapModeCount = static_cast<int>(WrapMode::kLast) + 1;
24
25 constexpr GrSamplerState() = default;
26
27 constexpr GrSamplerState(WrapMode wrapXAndY, Filter filter)
28 : fWrapModes{wrapXAndY, wrapXAndY}, fFilter(filter) {}
29
30 constexpr GrSamplerState(WrapMode wrapX, WrapMode wrapY, Filter filter)
31 : fWrapModes{wrapX, wrapY}, fFilter(filter) {}
32
33 constexpr GrSamplerState(const WrapMode wrapModes[2], Filter filter)
34 : fWrapModes{wrapModes[0], wrapModes[1]}, fFilter(filter) {}
35
36 constexpr /*explicit*/ GrSamplerState(Filter filter) : fFilter(filter) {}
37
38 constexpr GrSamplerState(const GrSamplerState&) = default;
39
40 constexpr GrSamplerState& operator=(const GrSamplerState&) = default;
41
42 constexpr Filter filter() const { return fFilter; }
43
44 constexpr void setFilterMode(Filter filterMode) { fFilter = filterMode; }
45
46 constexpr void setWrapModeX(const WrapMode wrap) { fWrapModes[0] = wrap; }
47 constexpr void setWrapModeY(const WrapMode wrap) { fWrapModes[1] = wrap; }
48
49 constexpr WrapMode wrapModeX() const { return fWrapModes[0]; }
50 constexpr WrapMode wrapModeY() const { return fWrapModes[1]; }
51
52 constexpr bool isRepeated() const {
53 return (WrapMode::kClamp != fWrapModes[0] && WrapMode::kClampToBorder != fWrapModes[0]) ||
54 (WrapMode::kClamp != fWrapModes[1] && WrapMode::kClampToBorder != fWrapModes[1]);
55 }
56
57 constexpr bool operator==(GrSamplerState that) const {
58 return fWrapModes[0] == that.fWrapModes[0] && fWrapModes[1] == that.fWrapModes[1] &&
59 fFilter == that.fFilter;
60 }
61
62 constexpr bool operator!=(const GrSamplerState& that) const { return !(*this == that); }
63
64 constexpr static uint8_t GenerateKey(GrSamplerState samplerState) {
65 const int kTileModeXShift = 2;
66 const int kTileModeYShift = 4;
67
68 SkASSERT(static_cast<int>(samplerState.filter()) <= 3);
69 uint8_t key = static_cast<uint8_t>(samplerState.filter());
70
71 SkASSERT(static_cast<int>(samplerState.wrapModeX()) <= 3);
72 key |= (static_cast<uint8_t>(samplerState.wrapModeX()) << kTileModeXShift);
73
74 SkASSERT(static_cast<int>(samplerState.wrapModeY()) <= 3);
75 key |= (static_cast<uint8_t>(samplerState.wrapModeY()) << kTileModeYShift);
76
77 return key;
78 }
79
80private:
81 WrapMode fWrapModes[2] = {WrapMode::kClamp, WrapMode::kClamp};
82 Filter fFilter = GrSamplerState::Filter::kNearest;
83};
84
85#endif
86