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 "utils/drawing_macros.h"
20 
21 namespace OHOS {
22 namespace Rosen {
23 namespace Drawing {
24 enum class FilterMode {
25     NEAREST,
26     LINEAR,
27 };
28 
29 enum class MipmapMode {
30     NONE,
31     NEAREST,
32     LINEAR,
33 };
34 
35 struct CubicResampler {
36     float cubicCoffB = 0;
37     float cubicCoffC = 0;
MitchellCubicResampler38     static constexpr CubicResampler Mitchell()
39     {
40         return { 1 / 3.0f, 1 / 3.0f };
41     }
CatmullRomCubicResampler42     static constexpr CubicResampler CatmullRom()
43     {
44         return { 0.0f, 1 / 2.0f };
45     }
46 };
47 
48 class DRAWING_API SamplingOptions {
49 public:
50     inline SamplingOptions() noexcept;
51     inline explicit SamplingOptions(FilterMode fm) noexcept;
52     inline SamplingOptions(FilterMode fm, MipmapMode mm) noexcept;
53     inline explicit SamplingOptions(const CubicResampler& c) noexcept;
54 
~SamplingOptions()55     inline ~SamplingOptions() {}
56 
57     inline bool GetUseCubic() const;
58     inline FilterMode GetFilterMode() const;
59     inline MipmapMode GetMipmapMode() const;
60     inline float GetCubicCoffB() const;
61     inline float GetCubicCoffC() const;
62 
63     friend inline bool operator==(const SamplingOptions& a, const SamplingOptions& b);
64     friend inline bool operator!=(const SamplingOptions& a, const SamplingOptions& b);
65 
66 private:
67     bool useCubic = false;
68     CubicResampler cubic = {0, 0};
69     FilterMode filter = FilterMode::NEAREST;
70     MipmapMode mipmap = MipmapMode::NONE;
71 };
72 
SamplingOptions()73 inline SamplingOptions::SamplingOptions() noexcept
74     : useCubic(false), filter(FilterMode::NEAREST), mipmap(MipmapMode::NONE)
75 {}
76 
SamplingOptions(FilterMode fm)77 inline SamplingOptions::SamplingOptions(FilterMode fm) noexcept : useCubic(false), filter(fm), mipmap(MipmapMode::NONE)
78 {}
79 
SamplingOptions(FilterMode fm,MipmapMode mm)80 inline SamplingOptions::SamplingOptions(FilterMode fm, MipmapMode mm) noexcept : useCubic(false), filter(fm), mipmap(mm)
81 {}
82 
SamplingOptions(const CubicResampler & c)83 inline SamplingOptions::SamplingOptions(const CubicResampler& c) noexcept : useCubic(true), cubic(c) {}
84 
GetUseCubic()85 inline bool SamplingOptions::GetUseCubic() const
86 {
87     return useCubic;
88 }
89 
GetFilterMode()90 inline FilterMode SamplingOptions::GetFilterMode() const
91 {
92     return filter;
93 }
94 
GetMipmapMode()95 inline MipmapMode SamplingOptions::GetMipmapMode() const
96 {
97     return mipmap;
98 }
99 
GetCubicCoffB()100 inline float SamplingOptions::GetCubicCoffB() const
101 {
102     return cubic.cubicCoffB;
103 }
104 
GetCubicCoffC()105 inline float SamplingOptions::GetCubicCoffC() const
106 {
107     return cubic.cubicCoffC;
108 }
109 
110 inline bool operator==(const SamplingOptions& a, const SamplingOptions& b)
111 {
112     return a.useCubic == b.useCubic && a.cubic.cubicCoffB == b.cubic.cubicCoffB &&
113         a.cubic.cubicCoffC == b.cubic.cubicCoffC && a.filter == b.filter && a.mipmap == b.mipmap;
114 }
115 
116 inline bool operator!=(const SamplingOptions& a, const SamplingOptions& b)
117 {
118     return !(a == b);
119 }
120 } // namespace Drawing
121 } // namespace Rosen
122 } // namespace OHOS
123 #endif