Bugfix: Gfx program discarded while DALi ProgramCache still holds to the GL resource
[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   void DestroyGraphicsObjects();
72
73   inline Graphics::SamplerAddressMode GetGraphicsSamplerAddressMode(WrapMode mode) const
74   {
75     switch(mode)
76     {
77       case WrapMode::REPEAT:
78         return Graphics::SamplerAddressMode::REPEAT;
79       case WrapMode::MIRRORED_REPEAT:
80         return Graphics::SamplerAddressMode::MIRRORED_REPEAT;
81       case WrapMode::CLAMP_TO_EDGE:
82         return Graphics::SamplerAddressMode::CLAMP_TO_EDGE;
83       case WrapMode::DEFAULT:
84         return Graphics::SamplerAddressMode::CLAMP_TO_EDGE;
85     }
86     return {};
87   }
88
89   inline Graphics::SamplerMipmapMode GetGraphicsSamplerMipmapMode(FilterMode mode) const
90   {
91     switch(mode)
92     {
93       case FilterMode::LINEAR_MIPMAP_LINEAR:
94         return Graphics::SamplerMipmapMode::LINEAR;
95       case FilterMode::NEAREST_MIPMAP_LINEAR:
96         return Graphics::SamplerMipmapMode::LINEAR;
97       case FilterMode::NEAREST_MIPMAP_NEAREST:
98         return Graphics::SamplerMipmapMode::NEAREST;
99       case FilterMode::LINEAR_MIPMAP_NEAREST:
100         return Graphics::SamplerMipmapMode::NEAREST;
101       default:
102         return Graphics::SamplerMipmapMode::NONE;
103     }
104     return {};
105   }
106
107   inline Graphics::SamplerFilter GetGraphicsFilter(FilterMode mode) const
108   {
109     switch(mode)
110     {
111       case FilterMode::LINEAR:
112       case FilterMode::LINEAR_MIPMAP_LINEAR:
113       case FilterMode::LINEAR_MIPMAP_NEAREST:
114         return Graphics::SamplerFilter::LINEAR;
115       case FilterMode::NEAREST:
116       case FilterMode::NEAREST_MIPMAP_LINEAR:
117       case FilterMode::NEAREST_MIPMAP_NEAREST:
118         return Graphics::SamplerFilter::NEAREST;
119       case FilterMode::DEFAULT:
120         return Graphics::SamplerFilter::LINEAR;
121       case FilterMode::NONE:
122         return Graphics::SamplerFilter::NEAREST;
123       default:
124         return {};
125     }
126     return {};
127   }
128
129   /**
130    * Sets the filter modes for an existing sampler
131    * @param[in] sampler The sampler
132    * @param[in] minFilterMode The filter to use under minification
133    * @param[in] magFilterMode The filter to use under magnification
134    */
135   inline void SetFilterMode(unsigned int minFilterMode, unsigned int magFilterMode)
136   {
137     mMinificationFilter  = static_cast<Dali::FilterMode::Type>(minFilterMode);
138     mMagnificationFilter = static_cast<Dali::FilterMode::Type>(magFilterMode);
139     mIsDirty             = true;
140   }
141
142   /**
143    * Sets the wrap mode for an existing sampler
144    * @param[in] sampler The sampler
145    * @param[in] rWrapMode Wrapping mode in z direction
146    * @param[in] sWrapMode Wrapping mode in x direction
147    * @param[in] tWrapMode Wrapping mode in y direction
148    */
149   inline void SetWrapMode(unsigned int rWrapMode, unsigned int sWrapMode, unsigned int tWrapMode)
150   {
151     mRWrapMode = static_cast<Dali::WrapMode::Type>(rWrapMode);
152     mSWrapMode = static_cast<Dali::WrapMode::Type>(sWrapMode);
153     mTWrapMode = static_cast<Dali::WrapMode::Type>(tWrapMode);
154     mIsDirty   = true;
155   }
156
157   /**
158    * Check if the sampler has default values
159    */
160   inline bool IsDefaultSampler()
161   {
162     return (mMagnificationFilter == FilterMode::DEFAULT &&
163             mMinificationFilter == FilterMode::DEFAULT &&
164             mSWrapMode == WrapMode::DEFAULT &&
165             mTWrapMode == WrapMode::DEFAULT &&
166             mRWrapMode == WrapMode::DEFAULT);
167   }
168
169   void Initialize(Graphics::Controller& graphicsController);
170
171   Graphics::Sampler* CreateGraphicsObject();
172
173   Graphics::Controller*                  mGraphicsController;
174   Graphics::UniquePtr<Graphics::Sampler> mGraphicsSampler;
175
176   FilterMode mMinificationFilter : 4;  ///< The minify filter
177   FilterMode mMagnificationFilter : 4; ///< The magnify filter
178   WrapMode   mSWrapMode : 4;           ///< The horizontal wrap mode
179   WrapMode   mTWrapMode : 4;           ///< The vertical wrap mode
180   WrapMode   mRWrapMode : 4;           ///< The vertical wrap mode
181   bool       mIsDirty : 1;             ///< If parameters have been set thru API
182 };
183
184 } // namespace Render
185
186 } // namespace Internal
187
188 } // namespace Dali
189
190 #endif //  DALI_INTERNAL_RENDER_SAMPLER_H