1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef SAMPLING_OPTIONS_H
17 #define SAMPLING_OPTIONS_H
18
19 #include <string>
20
21 #include "utils/drawing_macros.h"
22
23 namespace OHOS {
24 namespace Rosen {
25 namespace Drawing {
26 enum class FilterMode {
27 NEAREST,
28 LINEAR,
29 };
30
31 enum class MipmapMode {
32 NONE,
33 NEAREST,
34 LINEAR,
35 };
36
37 struct CubicResampler {
38 float cubicCoffB = 0;
39 float cubicCoffC = 0;
MitchellCubicResampler40 static constexpr CubicResampler Mitchell()
41 {
42 return { 1 / 3.0f, 1 / 3.0f };
43 }
CatmullRomCubicResampler44 static constexpr CubicResampler CatmullRom()
45 {
46 return { 0.0f, 1 / 2.0f };
47 }
48
DumpCubicResampler49 inline void Dump(std::string& out) const
50 {
51 out += "[cubicCoffB:" + std::to_string(cubicCoffB);
52 out += " cubicCoffC:" + std::to_string(cubicCoffB);
53 out += ']';
54 }
55 };
56
57 class DRAWING_API SamplingOptions {
58 public:
59 inline SamplingOptions() noexcept;
60 inline explicit SamplingOptions(FilterMode fm) noexcept;
61 inline SamplingOptions(FilterMode fm, MipmapMode mm) noexcept;
62 inline explicit SamplingOptions(const CubicResampler& c) noexcept;
63 inline SamplingOptions(const SamplingOptions& other) noexcept = default;
64
~SamplingOptions()65 inline ~SamplingOptions() {}
66
67 inline bool GetUseCubic() const;
68 inline FilterMode GetFilterMode() const;
69 inline MipmapMode GetMipmapMode() const;
70 inline float GetCubicCoffB() const;
71 inline float GetCubicCoffC() const;
72
73 friend inline bool operator==(const SamplingOptions& a, const SamplingOptions& b);
74 friend inline bool operator!=(const SamplingOptions& a, const SamplingOptions& b);
75
76 inline void Dump(std::string& out) const;
77
78 private:
79 bool useCubic = false;
80 CubicResampler cubic = {0, 0};
81 FilterMode filter = FilterMode::NEAREST;
82 MipmapMode mipmap = MipmapMode::NONE;
83 };
84
SamplingOptions()85 inline SamplingOptions::SamplingOptions() noexcept
86 : useCubic(false), filter(FilterMode::NEAREST), mipmap(MipmapMode::NONE)
87 {}
88
SamplingOptions(FilterMode fm)89 inline SamplingOptions::SamplingOptions(FilterMode fm) noexcept : useCubic(false), filter(fm), mipmap(MipmapMode::NONE)
90 {}
91
SamplingOptions(FilterMode fm,MipmapMode mm)92 inline SamplingOptions::SamplingOptions(FilterMode fm, MipmapMode mm) noexcept : useCubic(false), filter(fm), mipmap(mm)
93 {}
94
SamplingOptions(const CubicResampler & c)95 inline SamplingOptions::SamplingOptions(const CubicResampler& c) noexcept : useCubic(true), cubic(c) {}
96
GetUseCubic()97 inline bool SamplingOptions::GetUseCubic() const
98 {
99 return useCubic;
100 }
101
GetFilterMode()102 inline FilterMode SamplingOptions::GetFilterMode() const
103 {
104 return filter;
105 }
106
GetMipmapMode()107 inline MipmapMode SamplingOptions::GetMipmapMode() const
108 {
109 return mipmap;
110 }
111
GetCubicCoffB()112 inline float SamplingOptions::GetCubicCoffB() const
113 {
114 return cubic.cubicCoffB;
115 }
116
GetCubicCoffC()117 inline float SamplingOptions::GetCubicCoffC() const
118 {
119 return cubic.cubicCoffC;
120 }
121
122 inline bool operator==(const SamplingOptions& a, const SamplingOptions& b)
123 {
124 return a.useCubic == b.useCubic && a.cubic.cubicCoffB == b.cubic.cubicCoffB &&
125 a.cubic.cubicCoffC == b.cubic.cubicCoffC && a.filter == b.filter && a.mipmap == b.mipmap;
126 }
127
128 inline bool operator!=(const SamplingOptions& a, const SamplingOptions& b)
129 {
130 return !(a == b);
131 }
132
Dump(std::string & out)133 inline void SamplingOptions::Dump(std::string& out) const
134 {
135 out += "[useCubic:" + std::string(useCubic ? "true" : "false");
136 out += " cubic";
137 cubic.Dump(out);
138 out += " filterMode:" + std::to_string(static_cast<int>(filter));
139 out += " mipmapMode:" + std::to_string(static_cast<int>(mipmap));
140 out += ']';
141 }
142 } // namespace Drawing
143 } // namespace Rosen
144 } // namespace OHOS
145 #endif