[dali_2.3.27] Merge branch 'devel/master'
[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 Create a new Texture.
44    *
45    * @param[in] type The type of the texture
46    * @param[in] format The format of the pixel data
47    * @param[in] width The width of the texture
48    * @param[in] height The height of the texture
49    * @return A smart-pointer to the newly allocated Texture.
50    */
51   static TexturePtr New(TextureType::Type type, Pixel::Format format, uint32_t width, uint32_t height);
52
53   /**
54    * @brief Creates a new Texture from a native image
55    * @param[in] nativeImageInterface The native image
56    * @return A smart-pointer to the newly allocated Texture.
57    */
58   static TexturePtr New(NativeImageInterface& nativeImageInterface);
59
60   /**
61    * @brief Create a new Texture with resourceId.
62    *
63    * @param[in] type The type of the texture
64    * @param[in] resourceId The unique id of this texture combind with TextureUploadManager
65    * @return A smart-pointer to the newly allocated Texture.
66    */
67   static TexturePtr New(TextureType::Type type, uint32_t resourceId);
68
69   /**
70    * @brief Get the texture render object
71    *
72    * @return the texture render object
73    */
74   Render::TextureKey GetRenderTextureKey() const;
75
76   /**
77    * @copydoc Dali::Texture::Upload()
78    */
79   bool Upload(PixelDataPtr pixelData);
80
81   /**
82    * @copydoc Dali::Texture::Upload()
83    */
84   bool Upload(PixelDataPtr pixelData,
85               uint32_t     layer,
86               uint32_t     mipmap,
87               uint32_t     xOffset,
88               uint32_t     yOffset,
89               uint32_t     width,
90               uint32_t     height);
91
92   /**
93    * @copydoc Dali::Texture::UploadSubPixelData()
94    */
95   bool UploadSubPixelData(PixelDataPtr pixelData,
96                           uint32_t     dataXOffset,
97                           uint32_t     dataYOffset,
98                           uint32_t     dataWidth,
99                           uint32_t     dataHeight);
100
101   /**
102    * @copydoc Dali::Texture::UploadSubPixelData()
103    */
104   bool UploadSubPixelData(PixelDataPtr pixelData,
105                           uint32_t     dataXOffset,
106                           uint32_t     dataYOffset,
107                           uint32_t     dataWidth,
108                           uint32_t     dataHeight,
109                           uint32_t     layer,
110                           uint32_t     mipmap,
111                           uint32_t     xOffset,
112                           uint32_t     yOffset,
113                           uint32_t     width,
114                           uint32_t     height);
115
116   /**
117    * @copydoc Dali::Texture::GenerateMipmaps()
118    */
119   void GenerateMipmaps();
120
121   /**
122    * @copydoc Dali::Texture::GetWidth()
123    */
124   uint32_t GetWidth() const;
125
126   /**
127    * @copydoc Dali::Texture::GetHeight()
128    */
129   uint32_t GetHeight() const;
130
131   /**
132    * @copydoc Dali::Texture::GetPixelFormat()
133    */
134   Pixel::Format GetPixelFormat() const;
135
136   /**
137    * @copydoc Dali::Integration::GetTextureResourceId()
138    */
139   uint32_t GetResourceId() const;
140
141   /**
142    * @copydoc Dali::Integration::GetTextureType()
143    */
144   Dali::TextureType::Type GetTextureType() const;
145
146   /**
147    * @copydoc Dali::Integration::SetTextureSize()
148    */
149   void SetSize(const ImageDimensions& size);
150
151   /**
152    * @copydoc Dali::Integration::SetTexturePixelFormat()
153    */
154   void SetPixelFormat(Pixel::Format format);
155
156   /**
157    * @brief Determine if the texture is a native image
158    *
159    * @return true if the texture has been initialized with a native image
160    */
161   bool IsNative() const;
162
163   /**
164    * @brief Apply any native texture code to the given fragment shader
165    *
166    * @param[in,out] shader The fragment shader
167    * @return true if the shader has been modified.
168    */
169   bool ApplyNativeFragmentShader(std::string& shader);
170
171 private: // implementation
172   /**
173    * Constructor
174    * @param[in] type The type of the texture
175    * @param[in] format The format of the pixel data
176    * @param[in] size The size of the texture
177    */
178   Texture(TextureType::Type type, Pixel::Format format, ImageDimensions size);
179
180   /**
181    * Constructor from native image
182    * @param[in] nativeImageInterface The native image
183    */
184   Texture(NativeImageInterfacePtr nativeImageInterface);
185
186   /**
187    * Constructor from resource id
188    * @param[in] type The type of the texture
189    * @param[in] resourceId The resouce id for texture upload manager using
190    */
191   Texture(TextureType::Type type, uint32_t resourceId);
192
193   /**
194    * Second stage initialization of the Texture
195    */
196   void Initialize();
197
198 protected:
199   /**
200    * A reference counted object may only be deleted by calling Unreference()
201    */
202   ~Texture() override;
203
204 private: // unimplemented methods
205   Texture(const Texture&);
206   Texture& operator=(const Texture&);
207
208 private:                                               // data
209   Internal::EventThreadServices& mEventThreadServices; ///<Used to send messages to the render thread via update thread
210   Internal::Render::TextureKey   mTextureKey;          ///<The Render::Texture associated to this texture
211
212   NativeImageInterfacePtr mNativeImage; ///< Pointer to native image
213   ImageDimensions         mSize;        ///< Size of the texture
214   Dali::TextureType::Type mType;        ///< Texture type (cached)
215   Pixel::Format           mFormat;      ///< Pixel format
216
217   uint32_t mResourceId;
218
219   bool mUseUploadedParameter : 1; ///< Whether ths texture size and format depend on uploaded image or not.
220 };
221
222 } // namespace Internal
223
224 // Helpers for public-api forwarding methods
225 inline Internal::Texture& GetImplementation(Dali::Texture& handle)
226 {
227   DALI_ASSERT_ALWAYS(handle && "Texture handle is empty");
228
229   BaseObject& object = handle.GetBaseObject();
230
231   return static_cast<Internal::Texture&>(object);
232 }
233
234 inline const Internal::Texture& GetImplementation(const Dali::Texture& handle)
235 {
236   DALI_ASSERT_ALWAYS(handle && "Texture handle is empty");
237
238   const BaseObject& object = handle.GetBaseObject();
239
240   return static_cast<const Internal::Texture&>(object);
241 }
242
243 } // namespace Dali
244
245 #endif // DALI_INTERNAL_TEXTURE_H