Make property resetter age down correctly
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-texture.h
1 #ifndef DALI_INTERNAL_RENDER_TEXTURE_H
2 #define DALI_INTERNAL_RENDER_TEXTURE_H
3
4 /*
5  * Copyright (c) 2023 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 // EXTERNAL INCLUDES
21 #include <cstdint> // uint16_t, uint32_t
22 #include <string>
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/images/image-operations.h> // Dali::ImageDimensions
26 #include <dali/public-api/rendering/sampler.h>
27 #include <dali/public-api/rendering/texture.h>
28
29 #include <dali/graphics-api/graphics-controller.h>
30 #include <dali/graphics-api/graphics-texture-create-info.h>
31 #include <dali/graphics-api/graphics-texture.h>
32 #include <dali/graphics-api/graphics-types.h>
33 #include <dali/internal/event/rendering/texture-impl.h>
34 #include <dali/internal/render/renderers/render-sampler.h>
35
36 namespace Dali
37 {
38 namespace Internal
39 {
40 namespace Render
41 {
42 struct Sampler;
43
44 class Texture
45 {
46 public:
47   using Type = Dali::TextureType::Type;
48
49   /**
50    * Factory method to return a new texture accessed by key.
51    */
52   static TextureKey NewKey(Type type, Pixel::Format format, ImageDimensions size);
53
54   /**
55    * Factory method to return a new texture accessed by key.
56    */
57   static TextureKey NewKey(NativeImageInterfacePtr nativeImageInterface);
58
59   /**
60    * Constructor
61    * @param[in] type The type of the texture
62    * @param[in] format The format of the pixel data
63    * @param[in] size The size of the texture
64    */
65   Texture(Type type, Pixel::Format format, ImageDimensions size);
66
67   /**
68    * Constructor from native image
69    * @param[in] nativeImageInterface The native image
70    */
71   explicit Texture(NativeImageInterfacePtr nativeImageInterface);
72
73   /**
74    * Destructor
75    */
76   ~Texture();
77
78   /**
79    * Deletes the texture from it's global memory pool
80    */
81   void operator delete(void* ptr);
82
83   static Texture* Get(TextureKey::KeyType);
84
85   /**
86    * Get the key of the given renderer in the associated memory pool.
87    * @param[in] renderer the given renderer
88    * @return The key in the associated memory pool.
89    */
90   static TextureKey GetKey(const Render::Texture& renderer);
91
92   /**
93    * Get the key of the given renderer in the associated memory pool.
94    * @param[in] renderer the given renderer
95    * @return The key in the associated memory pool, or -1 if not
96    * found.
97    */
98   static TextureKey GetKey(Render::Texture* renderer);
99
100   /**
101    * Stores the graphics controller for use when required.
102    *
103    * @param[in] graphicsController The graphics controller to use
104    */
105   void Initialize(Graphics::Controller& graphicsController);
106
107   /**
108    * Create the texture without a buffer
109    * @param[in] usage How texture will be used
110    */
111   void Create(Graphics::TextureUsageFlags usage);
112
113   /**
114    * Deletes the texture from the GPU
115    */
116   void Destroy();
117
118   /**
119    * Uploads data to the texture.
120    * @param[in] pixelData A pixel data object
121    * @param[in] params Upload parameters. See UploadParams
122    */
123   void Upload(PixelDataPtr pixelData, const Internal::Texture::UploadParams& params);
124
125   /**
126    * Auto generates mipmaps for the texture
127    */
128   void GenerateMipmaps();
129
130   /**
131    * Retrieve whether the texture has an alpha channel
132    * @return True if the texture has alpha channel, false otherwise
133    */
134   [[nodiscard]] bool HasAlphaChannel() const;
135
136   /**
137    * Get the graphics object associated with this texture
138    */
139   [[nodiscard]] Graphics::Texture* GetGraphicsObject() const;
140
141   /**
142    * Get the type of the texture
143    * @return Type of the texture
144    */
145   [[nodiscard]] Type GetType() const
146   {
147     return mType;
148   }
149
150   /**
151    * Check if the texture is a native image
152    * @return if the texture is a native image
153    */
154   [[nodiscard]] bool IsNativeImage() const
155   {
156     return static_cast<bool>(mNativeImage);
157   }
158
159   /**
160    * Return the pixel format of the texture
161    * @return The pixel format of the texture data.
162    */
163   Pixel::Format GetPixelFormat() const
164   {
165     return mPixelFormat;
166   }
167
168   /**
169    * Return the width of the texture
170    * @return The width of the texture
171    */
172   uint16_t GetWidth() const
173   {
174     return mWidth;
175   }
176
177   /**
178    * Return the height of the texture
179    * @return The height of the texture
180    */
181   uint32_t GetHeight() const
182   {
183     return mHeight;
184   }
185
186   /**
187    * Called from RenderManager to notify the texture that current rendering pass has finished.
188    */
189   void OnRenderFinished();
190
191   /**
192    * Set the updated flag.
193    * @param[in] updated The updated flag
194    */
195   void SetUpdated(bool updated)
196   {
197     mUpdated = updated;
198   }
199
200   /**
201    * Check if the texture is updated
202    * @return True if the texture is updated
203    */
204   [[nodiscard]] bool Updated()
205   {
206     if(mUpdated || (mNativeImage && mNativeImage->SourceChanged()))
207     {
208       return true;
209     }
210     return false;
211   }
212
213   /**
214    * Get the updated area
215    * @return The updated area if the texture is updated, otherwise an empty area
216    * @note The origin of the area is the top-left corner of the texture.
217    */
218   Rect<uint16_t> GetUpdatedArea();
219
220 private:
221   /**
222    * Helper method to apply a sampler to the texture
223    * @param[in] sampler The sampler
224    */
225   void ApplySampler(Render::Sampler* sampler);
226
227   /**
228    * Create a texture with a buffer if non-null
229    * @param[in] usage How texture will be used
230    * @param[in] buffer Buffer to copy
231    * @param[in] bufferSize Size of buffer to copy
232    */
233   void CreateWithData(Graphics::TextureUsageFlags usage, uint8_t* buffer, uint32_t bufferSize);
234
235 private:
236   Graphics::Controller*                  mGraphicsController;
237   Graphics::UniquePtr<Graphics::Texture> mGraphicsTexture;
238
239   NativeImageInterfacePtr mNativeImage; ///< Pointer to native image
240   Render::Sampler         mSampler;     ///< The current sampler state
241
242   Rect<uint16_t> mUpdatedArea{}; ///< Updated area of the texture
243
244   Pixel::Format mPixelFormat; ///< Pixel format of the texture
245   uint16_t      mWidth;       ///< Width of the texture
246   uint16_t      mHeight;      ///< Height of the texture
247
248   Type mType : 3;     ///< Type of the texture
249   bool mHasAlpha : 1; ///< Whether the format has an alpha channel
250   bool mUpdated : 1;  ///< Whether the texture is updated
251
252   bool mUseUploadedParameter : 1; ///< Whether ths texture size and format depend on uploaded image or not.
253 };
254
255 } // namespace Render
256
257 } // namespace Internal
258
259 } // namespace Dali
260
261 #endif //  DALI_INTERNAL_RENDER_TEXTURE_H