Moved SetUpdate propagation to UpdateNodes
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-sampler.h
1 #ifndef DALI_INTERNAL_RENDER_SAMPLER_H
2 #define DALI_INTERNAL_RENDER_SAMPLER_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 #include <dali/graphics-api/graphics-controller.h>
21 #include <dali/public-api/actors/sampling.h>
22 #include <dali/public-api/rendering/sampler.h>
23
24 namespace Dali
25 {
26 namespace Internal
27 {
28 namespace Render
29 {
30 /**
31  * The sampler class holds the min/mag filter and texture wrap modes.
32  * It's graphics counterpart is only created when needed, and also
33  * only created when the filters and wrap modes are not default
34  * values.
35  */
36 struct Sampler
37 {
38   using FilterMode = Dali::FilterMode::Type;
39   using WrapMode   = Dali::WrapMode::Type;
40
41   /**
42    * Constructor
43    */
44   Sampler();
45
46   /**
47    * Destructor
48    */
49   ~Sampler() = default;
50
51   bool operator==(const Sampler& rhs) const
52   {
53     return ((mMinificationFilter == rhs.mMinificationFilter) &&
54             (mMagnificationFilter == rhs.mMagnificationFilter) &&
55             (mSWrapMode == rhs.mSWrapMode) &&
56             (mTWrapMode == rhs.mTWrapMode) &&
57             (mRWrapMode == rhs.mRWrapMode));
58   }
59
60   bool operator!=(const Sampler& rhs) const
61   {
62     return !(*this == rhs);
63   }
64
65   /**
66    * Returns Graphics API sampler object
67    * @return Pointer to API sampler or nullptr
68    */
69   const Dali::Graphics::Sampler* GetGraphicsObject();
70
71   [[nodiscard]] static inline Graphics::SamplerAddressMode GetGraphicsSamplerAddressMode(WrapMode mode)
72   {
73     switch(mode)
74     {
75       case WrapMode::REPEAT:
76         return Graphics::SamplerAddressMode::REPEAT;
77       case WrapMode::MIRRORED_REPEAT:
78         return Graphics::SamplerAddressMode::MIRRORED_REPEAT;
79       case WrapMode::CLAMP_TO_EDGE:
80       case WrapMode::DEFAULT:
81         return Graphics::SamplerAddressMode::CLAMP_TO_EDGE;
82     }
83     return {};
84   }
85
86   [[nodiscard]] static inline Graphics::SamplerMipmapMode GetGraphicsSamplerMipmapMode(FilterMode mode)
87   {
88     switch(mode)
89     {
90       case FilterMode::LINEAR_MIPMAP_LINEAR:
91       case FilterMode::NEAREST_MIPMAP_LINEAR:
92         return Graphics::SamplerMipmapMode::LINEAR;
93       case FilterMode::NEAREST_MIPMAP_NEAREST:
94       case FilterMode::LINEAR_MIPMAP_NEAREST:
95         return Graphics::SamplerMipmapMode::NEAREST;
96       default:
97         return Graphics::SamplerMipmapMode::NONE;
98     }
99     return {};
100   }
101
102   [[nodiscard]] static inline Graphics::SamplerFilter GetGraphicsFilter(FilterMode mode)
103   {
104     switch(mode)
105     {
106       case FilterMode::LINEAR:
107       case FilterMode::LINEAR_MIPMAP_LINEAR:
108       case FilterMode::LINEAR_MIPMAP_NEAREST:
109         return Graphics::SamplerFilter::LINEAR;
110       case FilterMode::NEAREST:
111       case FilterMode::NEAREST_MIPMAP_LINEAR:
112       case FilterMode::NEAREST_MIPMAP_NEAREST:
113         return Graphics::SamplerFilter::NEAREST;
114       case FilterMode::DEFAULT:
115         return Graphics::SamplerFilter::LINEAR;
116       case FilterMode::NONE:
117         return Graphics::SamplerFilter::NEAREST;
118       default:
119         return {};
120     }
121     return {};
122   }
123
124   /**
125    * Sets the filter modes for an existing sampler
126    * @param[in] sampler The sampler
127    * @param[in] minFilterMode The filter to use under minification
128    * @param[in] magFilterMode The filter to use under magnification
129    */
130   inline void SetFilterMode(unsigned int minFilterMode, unsigned int magFilterMode)
131   {
132     mMinificationFilter  = static_cast<Dali::FilterMode::Type>(minFilterMode);
133     mMagnificationFilter = static_cast<Dali::FilterMode::Type>(magFilterMode);
134     mIsDirty             = true;
135   }
136
137   /**
138    * Sets the wrap mode for an existing sampler
139    * @param[in] sampler The sampler
140    * @param[in] rWrapMode Wrapping mode in z direction
141    * @param[in] sWrapMode Wrapping mode in x direction
142    * @param[in] tWrapMode Wrapping mode in y direction
143    */
144   inline void SetWrapMode(unsigned int rWrapMode, unsigned int sWrapMode, unsigned int tWrapMode)
145   {
146     mRWrapMode = static_cast<Dali::WrapMode::Type>(rWrapMode);
147     mSWrapMode = static_cast<Dali::WrapMode::Type>(sWrapMode);
148     mTWrapMode = static_cast<Dali::WrapMode::Type>(tWrapMode);
149     mIsDirty   = true;
150   }
151
152   /**
153    * Check if the sampler has default values
154    */
155   [[nodiscard]] inline bool IsDefaultSampler() const
156   {
157     return (mMagnificationFilter == FilterMode::DEFAULT &&
158             mMinificationFilter == FilterMode::DEFAULT &&
159             mSWrapMode == WrapMode::DEFAULT &&
160             mTWrapMode == WrapMode::DEFAULT &&
161             mRWrapMode == WrapMode::DEFAULT);
162   }
163
164   void Initialize(Graphics::Controller& graphicsController);
165
166   Graphics::Sampler* CreateGraphicsObject();
167
168   Graphics::Controller*                  mGraphicsController;
169   Graphics::UniquePtr<Graphics::Sampler> mGraphicsSampler;
170
171   FilterMode mMinificationFilter : 4;  ///< The minify filter
172   FilterMode mMagnificationFilter : 4; ///< The magnify filter
173   WrapMode   mSWrapMode : 4;           ///< The horizontal wrap mode
174   WrapMode   mTWrapMode : 4;           ///< The vertical wrap mode
175   WrapMode   mRWrapMode : 4;           ///< The vertical wrap mode
176   bool       mIsDirty : 1;             ///< If parameters have been set thru API
177 };
178
179 } // namespace Render
180
181 } // namespace Internal
182
183 } // namespace Dali
184
185 #endif //  DALI_INTERNAL_RENDER_SAMPLER_H