[dali_2.3.25] Merge branch 'devel/master'
[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-upload-helper.h>
32 #include <dali/graphics-api/graphics-texture.h>
33 #include <dali/graphics-api/graphics-types.h>
34 #include <dali/internal/event/rendering/texture-impl.h>
35 #include <dali/internal/render/renderers/render-sampler.h>
36
37 namespace Dali
38 {
39 namespace Internal
40 {
41 namespace SceneGraph
42 {
43 class RenderManager;
44 } // namespace SceneGraph
45
46 namespace Render
47 {
48 struct Sampler;
49
50 class Texture
51 {
52 public:
53   using Type = Dali::TextureType::Type;
54
55   /**
56    * Factory method to return a new texture accessed by key.
57    */
58   static TextureKey NewKey(Type type, Pixel::Format format, ImageDimensions size);
59
60   /**
61    * Factory method to return a new texture accessed by key.
62    */
63   static TextureKey NewKey(NativeImageInterfacePtr nativeImageInterface);
64
65   /**
66    * Factory method to return a new texture accessed by key.
67    */
68   static TextureKey NewKey(Type type, uint32_t resourceId);
69
70   /**
71    * Constructor
72    * @param[in] type The type of the texture
73    * @param[in] format The format of the pixel data
74    * @param[in] size The size of the texture
75    */
76   Texture(Type type, Pixel::Format format, ImageDimensions size);
77
78   /**
79    * Constructor from native image
80    * @param[in] nativeImageInterface The native image
81    */
82   explicit Texture(NativeImageInterfacePtr nativeImageInterface);
83
84   /**
85    * Constructor from resource id
86    * @param[in] type The type of the texture
87    * @param[in] resourceId The resouce id for texture upload manager using
88    */
89   explicit Texture(Type type, uint32_t resourceId);
90
91   /**
92    * Destructor
93    */
94   ~Texture();
95
96   /**
97    * Deletes the texture from it's global memory pool
98    */
99   void operator delete(void* ptr);
100
101   static Texture* Get(TextureKey::KeyType);
102
103   /**
104    * Get the key of the given renderer in the associated memory pool.
105    * @param[in] renderer the given renderer
106    * @return The key in the associated memory pool.
107    */
108   static TextureKey GetKey(const Render::Texture& renderer);
109
110   /**
111    * Get the key of the given renderer in the associated memory pool.
112    * @param[in] renderer the given renderer
113    * @return The key in the associated memory pool, or -1 if not
114    * found.
115    */
116   static TextureKey GetKey(Render::Texture* renderer);
117
118   /**
119    * Stores the graphics controller for use when required.
120    *
121    * @param[in] graphicsController The graphics controller to use
122    * @param[in] renderManager The render manager to be used.
123    */
124   void Initialize(Graphics::Controller& graphicsController, SceneGraph::RenderManager& renderManager);
125
126   /**
127    * Create the texture without a buffer
128    * @param[in] usage How texture will be used
129    */
130   void Create(Graphics::TextureUsageFlags usage);
131
132   /**
133    * Deletes the texture from the GPU
134    */
135   void Destroy();
136
137   /**
138    * Uploads data to the texture.
139    * @param[in] pixelData A pixel data object
140    * @param[in] params Upload parameters. See UploadParams
141    */
142   void Upload(PixelDataPtr pixelData, const Graphics::UploadParams& params);
143
144   /**
145    * Auto generates mipmaps for the texture
146    */
147   void GenerateMipmaps();
148
149   /**
150    * Retrieve whether the texture has an alpha channel
151    * @return True if the texture has alpha channel, false otherwise
152    */
153   [[nodiscard]] bool HasAlphaChannel() const;
154
155   /**
156    * Get the graphics object associated with this texture
157    */
158   [[nodiscard]] Graphics::Texture* GetGraphicsObject();
159
160   /**
161    * Get the type of the texture
162    * @return Type of the texture
163    */
164   [[nodiscard]] Type GetType() const
165   {
166     return mType;
167   }
168
169   /**
170    * Check if the texture is a native image
171    * @return if the texture is a native image
172    */
173   [[nodiscard]] bool IsNativeImage() const
174   {
175     return static_cast<bool>(mNativeImage);
176   }
177
178   /**
179    * Return the pixel format of the texture
180    * @return The pixel format of the texture data.
181    */
182   Pixel::Format GetPixelFormat() const
183   {
184     return mPixelFormat;
185   }
186
187   /**
188    * Return the width of the texture
189    * @return The width of the texture
190    */
191   uint16_t GetWidth() const
192   {
193     return mWidth;
194   }
195
196   /**
197    * Return the height of the texture
198    * @return The height of the texture
199    */
200   uint16_t GetHeight() const
201   {
202     return mHeight;
203   }
204
205   /**
206    * Set the pixel format of the texture
207    * @param[in] pixelFormat The pixel format of the texture data.
208    */
209   void SetPixelFormat(Pixel::Format pixelFormat)
210   {
211     mPixelFormat = pixelFormat;
212   }
213
214   /**
215    * Set the width of the texture
216    * @param[in] width The width of the texture
217    */
218   void SetWidth(uint16_t width)
219   {
220     mWidth = width;
221   }
222
223   /**
224    * Set the height of the texture
225    * @param[in] height The height of the texture
226    */
227   void SetHeight(uint16_t height)
228   {
229     mHeight = height;
230   }
231
232   /**
233    * Called from RenderManager to notify the texture that current rendering pass has finished.
234    */
235   void OnRenderFinished();
236
237   /**
238    * Set the updated flag.
239    * @param[in] updated The updated flag
240    */
241   void SetUpdated(bool updated)
242   {
243     mUpdated = updated;
244   }
245
246   /**
247    * Check if the texture is updated
248    * @return True if the texture is updated
249    */
250   [[nodiscard]] bool Updated()
251   {
252     if(mUpdated || (mNativeImage && mNativeImage->SourceChanged()) || (mResourceId != 0u && IsGraphicsObjectChanged()))
253     {
254       return true;
255     }
256     return false;
257   }
258
259   /**
260    * Get the updated area
261    * @return The updated area if the texture is updated, otherwise an empty area
262    * @note The origin of the area is the top-left corner of the texture.
263    */
264   Rect<uint16_t> GetUpdatedArea();
265
266 private:
267   /**
268    * Helper method to apply a sampler to the texture
269    * @param[in] sampler The sampler
270    */
271   void ApplySampler(Render::Sampler* sampler);
272
273   /**
274    * Create the texture without a buffer
275    * @param[in] usage How texture will be used
276    * @param[in] allocationPolicy Policy of texture allocation
277    */
278   void Create(Graphics::TextureUsageFlags usage, Graphics::TextureAllocationPolicy allocationPolicy);
279
280   /**
281    * Create a texture with a buffer if non-null
282    * @param[in] usage How texture will be used
283    * @param[in] allocationPolicy Policy of texture allocation
284    * @param[in] buffer Buffer to copy
285    * @param[in] bufferSize Size of buffer to copy
286    */
287   void CreateWithData(Graphics::TextureUsageFlags usage, Graphics::TextureAllocationPolicy allocationPolicy, uint8_t* buffer, uint32_t bufferSize);
288
289   /**
290    * @brief Check whether graphics object pointer were changed what we know before.
291    *
292    * @return True if graphics object created newly. False otherwised.
293    */
294   bool IsGraphicsObjectChanged();
295
296   /**
297    * @brief Notify to RenderManager that this texture has been updated.
298    */
299   void NotifyTextureUpdated();
300
301 private:
302   Graphics::Controller*                  mGraphicsController;
303   SceneGraph::RenderManager*             mRenderManager;
304   Graphics::UniquePtr<Graphics::Texture> mGraphicsTexture;
305
306   NativeImageInterfacePtr mNativeImage; ///< Pointer to native image
307   Render::Sampler         mSampler;     ///< The current sampler state
308
309   Rect<uint16_t> mUpdatedArea{}; ///< Updated area of the texture
310
311   Pixel::Format mPixelFormat; ///< Pixel format of the texture
312   uint16_t      mWidth;       ///< Width of the texture
313   uint16_t      mHeight;      ///< Height of the texture
314
315   uint32_t           mResourceId;
316   Graphics::Texture* mLatestUsedGraphicsTexture{nullptr};
317
318   Type mType : 3;     ///< Type of the texture
319   bool mHasAlpha : 1; ///< Whether the format has an alpha channel
320   bool mUpdated : 1;  ///< Whether the texture is updated
321
322   bool mUseUploadedParameter : 1; ///< Whether ths texture size and format depend on uploaded image or not.
323 };
324
325 } // namespace Render
326
327 } // namespace Internal
328
329 } // namespace Dali
330
331 #endif //  DALI_INTERNAL_RENDER_TEXTURE_H