Migrated Render::Texture to use memory pool
[platform/core/uifw/dali-core.git] / dali / internal / event / rendering / texture-impl.h
1 #ifndef DALI_INTERNAL_NEW_TEXTURE_H
2 #define DALI_INTERNAL_NEW_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
21 // INTERNAL INCLUDES
22 #include <dali/internal/event/common/event-thread-services.h>
23 #include <dali/internal/event/images/pixel-data-impl.h>
24 #include <dali/internal/render/renderers/render-texture-key.h>
25 #include <dali/public-api/common/dali-common.h>      // DALI_ASSERT_ALWAYS
26 #include <dali/public-api/common/intrusive-ptr.h>    // Dali::IntrusivePtr
27 #include <dali/public-api/images/image-operations.h> // Dali::ImageDimensions
28 #include <dali/public-api/images/pixel.h>
29 #include <dali/public-api/object/base-object.h>
30 #include <dali/public-api/rendering/texture.h> // Dali::Internal::Render::Texture
31
32 namespace Dali
33 {
34 namespace Internal
35 {
36 class Texture;
37 using TexturePtr = IntrusivePtr<Texture>;
38
39 class Texture : public BaseObject
40 {
41 public:
42   /**
43    * @brief Structure used to pass parameters to the Upload method
44    */
45   struct UploadParams
46   {
47     uint32_t dataXOffset; ///< Specifies a pixeldata offset in the x direction within the pixeldata buffer.
48     uint32_t dataYOffset; ///< Specifies a pixeldata offset in the y direction within the pixeldata buffer.
49     uint16_t dataWidth;   ///< Specifies the width of the pixeldata subimage.
50     uint16_t dataHeight;  ///< Specifies the height of the pixeldata subimage.
51     uint16_t layer;       ///< Specifies the layer of a cube map or array texture
52     uint16_t mipmap;      ///< Specifies the level-of-detail number. Level 0 is the base image level. Level n is the nth mipmap reduction image.
53     uint16_t xOffset;     ///< Specifies a texel offset in the x direction within the texture array.
54     uint16_t yOffset;     ///< Specifies a texel offset in the y direction within the texture array.
55     uint16_t width;       ///< Specifies the width of the texture subimage
56     uint16_t height;      ///< Specifies the height of the texture subimage.
57   };
58
59   /**
60    * @brief Create a new Texture.
61    *
62    * @param[in] type The type of the texture
63    * @param[in] format The format of the pixel data
64    * @param[in] width The width of the texture
65    * @param[in] height The height of the texture
66    * @return A smart-pointer to the newly allocated Texture.
67    */
68   static TexturePtr New(TextureType::Type type, Pixel::Format format, uint32_t width, uint32_t height);
69
70   /**
71    * @brief Creates a new Texture from a native image
72    * @param[in] nativeImageInterface The native image
73    * @return A smart-pointer to the newly allocated Texture.
74    */
75   static TexturePtr New(NativeImageInterface& nativeImageInterface);
76
77   /**
78    * @brief Get the texture render object
79    *
80    * @return the texture render object
81    */
82   Render::TextureKey GetRenderTextureKey() const;
83
84   /**
85    * @copydoc Dali::Texture::Upload()
86    */
87   bool Upload(PixelDataPtr pixelData);
88
89   /**
90    * @copydoc Dali::Texture::Upload()
91    */
92   bool Upload(PixelDataPtr pixelData,
93               uint32_t     layer,
94               uint32_t     mipmap,
95               uint32_t     xOffset,
96               uint32_t     yOffset,
97               uint32_t     width,
98               uint32_t     height);
99
100   /**
101    * @copydoc Dali::Texture::UploadSubPixelData()
102    */
103   bool UploadSubPixelData(PixelDataPtr pixelData,
104                           uint32_t     dataXOffset,
105                           uint32_t     dataYOffset,
106                           uint32_t     dataWidth,
107                           uint32_t     dataHeight);
108
109   /**
110    * @copydoc Dali::Texture::UploadSubPixelData()
111    */
112   bool UploadSubPixelData(PixelDataPtr pixelData,
113                           uint32_t     dataXOffset,
114                           uint32_t     dataYOffset,
115                           uint32_t     dataWidth,
116                           uint32_t     dataHeight,
117                           uint32_t     layer,
118                           uint32_t     mipmap,
119                           uint32_t     xOffset,
120                           uint32_t     yOffset,
121                           uint32_t     width,
122                           uint32_t     height);
123
124   /**
125    * @copydoc Dali::Texture::GenerateMipmaps()
126    */
127   void GenerateMipmaps();
128
129   /**
130    * @copydoc Dali::Texture::GetWidth()
131    */
132   uint32_t GetWidth() const;
133
134   /**
135    * @copydoc Dali::Texture::GetHeight()
136    */
137   uint32_t GetHeight() const;
138
139   /**
140    * @copydoc Dali::Texture::GetPixelFormat()
141    */
142   Pixel::Format GetPixelFormat() const;
143
144   /**
145    * @brief Determine if the texture is a native image
146    *
147    * @return true if the texture has been initialized with a native image
148    */
149   bool IsNative() const;
150
151   /**
152    * @brief Apply any native texture code to the given fragment shader
153    *
154    * @param[in,out] shader The fragment shader
155    * @return true if the shader has been modified.
156    */
157   bool ApplyNativeFragmentShader(std::string& shader);
158
159 private: // implementation
160   /**
161    * Constructor
162    * @param[in] type The type of the texture
163    * @param[in] format The format of the pixel data
164    * @param[in] size The size of the texture
165    */
166   Texture(TextureType::Type type, Pixel::Format format, ImageDimensions size);
167
168   /**
169    * Constructor from native image
170    * @param[in] nativeImageInterface The native image
171    */
172   Texture(NativeImageInterfacePtr nativeImageInterface);
173
174   /**
175    * Second stage initialization of the Texture
176    */
177   void Initialize();
178
179 protected:
180   /**
181    * A reference counted object may only be deleted by calling Unreference()
182    */
183   ~Texture() override;
184
185 private: // unimplemented methods
186   Texture(const Texture&);
187   Texture& operator=(const Texture&);
188
189 private:                                               // data
190   Internal::EventThreadServices& mEventThreadServices; ///<Used to send messages to the render thread via update thread
191   Internal::Render::TextureKey   mTextureKey;          ///<The Render::Texture associated to this texture
192
193   NativeImageInterfacePtr mNativeImage; ///< Pointer to native image
194   ImageDimensions         mSize;        ///< Size of the texture
195   Dali::TextureType::Type mType;        ///< Texture type (cached)
196   Pixel::Format           mFormat;      ///< Pixel format
197 };
198
199 } // namespace Internal
200
201 // Helpers for public-api forwarding methods
202 inline Internal::Texture& GetImplementation(Dali::Texture& handle)
203 {
204   DALI_ASSERT_ALWAYS(handle && "Texture handle is empty");
205
206   BaseObject& object = handle.GetBaseObject();
207
208   return static_cast<Internal::Texture&>(object);
209 }
210
211 inline const Internal::Texture& GetImplementation(const Dali::Texture& handle)
212 {
213   DALI_ASSERT_ALWAYS(handle && "Texture handle is empty");
214
215   const BaseObject& object = handle.GetBaseObject();
216
217   return static_cast<const Internal::Texture&>(object);
218 }
219
220 } // namespace Dali
221
222 #endif // DALI_INTERNAL_TEXTURE_H