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;
MitchellOHOS::Rosen::Drawing::CubicResampler40     static constexpr CubicResampler Mitchell()
41     {
42         return { 1 / 3.0f, 1 / 3.0f };
43     }
CatmullRomOHOS::Rosen::Drawing::CubicResampler44     static constexpr CubicResampler CatmullRom()
45     {
46         return { 0.0f, 1 / 2.0f };
47     }
48 
DumpOHOS::Rosen::Drawing::CubicResampler49     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 
~SamplingOptions()64     inline ~SamplingOptions() {}
65 
66     inline bool GetUseCubic() const;
67     inline FilterMode GetFilterMode() const;
68     inline MipmapMode GetMipmapMode() const;
69     inline float GetCubicCoffB() const;
70     inline float GetCubicCoffC() const;
71 
72     friend inline bool operator==(const SamplingOptions& a, const SamplingOptions& b);
73     friend inline bool operator!=(const SamplingOptions& a, const SamplingOptions& b);
74 
75     inline void Dump(std::string& out) const;
76 
77 private:
78     bool useCubic = false;
79     CubicResampler cubic = {0, 0};
80     FilterMode filter = FilterMode::NEAREST;
81     MipmapMode mipmap = MipmapMode::NONE;
82 };
83 
84 inline SamplingOptions::SamplingOptions() noexcept
mipmap(MipmapMode::NONE)85     : useCubic(false), filter(FilterMode::NEAREST), mipmap(MipmapMode::NONE)
86 {}
87 
mipmap(MipmapMode::NONE)88 inline SamplingOptions::SamplingOptions(FilterMode fm) noexcept : useCubic(false), filter(fm), mipmap(MipmapMode::NONE)
89 {}
90 
mipmap(mm)91 inline SamplingOptions::SamplingOptions(FilterMode fm, MipmapMode mm) noexcept : useCubic(false), filter(fm), mipmap(mm)
92 {}
93 
cubic(c)94 inline SamplingOptions::SamplingOptions(const CubicResampler& c) noexcept : useCubic(true), cubic(c) {}
95 
GetUseCubic() const96 inline bool SamplingOptions::GetUseCubic() const
97 {
98     return useCubic;
99 }
100 
GetFilterMode() const101 inline FilterMode SamplingOptions::GetFilterMode() const
102 {
103     return filter;
104 }
105 
GetMipmapMode() const106 inline MipmapMode SamplingOptions::GetMipmapMode() const
107 {
108     return mipmap;
109 }
110 
GetCubicCoffB() const111 inline float SamplingOptions::GetCubicCoffB() const
112 {
113     return cubic.cubicCoffB;
114 }
115 
GetCubicCoffC() const116 inline float SamplingOptions::GetCubicCoffC() const
117 {
118     return cubic.cubicCoffC;
119 }
120 
operator ==(const SamplingOptions& a, const SamplingOptions& b)121 inline bool operator==(const SamplingOptions& a, const SamplingOptions& b)
122 {
123     return a.useCubic == b.useCubic && a.cubic.cubicCoffB == b.cubic.cubicCoffB &&
124         a.cubic.cubicCoffC == b.cubic.cubicCoffC && a.filter == b.filter && a.mipmap == b.mipmap;
125 }
126 
operator !=(const SamplingOptions& a, const SamplingOptions& b)127 inline bool operator!=(const SamplingOptions& a, const SamplingOptions& b)
128 {
129     return !(a == b);
130 }
131 
Dump(std::string& out) const132 inline void SamplingOptions::Dump(std::string& out) const
133 {
134     out += "[useCubic:" + std::string(useCubic ? "true" : "false");
135     out += " cubic";
136     cubic.Dump(out);
137     out += " filterMode:" + std::to_string(static_cast<int>(filter));
138     out += " mipmapMode:" + std::to_string(static_cast<int>(mipmap));
139     out += ']';
140 }
141 } // namespace Drawing
142 } // namespace Rosen
143 } // namespace OHOS
144 #endif