Merge "Added UIThreadLoader to GLIB framework" into devel/master
[platform/core/uifw/dali-adaptor.git] / dali / public-api / adaptor-framework / native-image-source.h
1 #ifndef DALI_NATIVE_IMAGE_SOURCE_H
2 #define DALI_NATIVE_IMAGE_SOURCE_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 // EXTERNAL INCLUDES
22 #include <string>
23
24 #include <dali/public-api/common/vector-wrapper.h>
25 #include <dali/public-api/images/native-image-interface.h>
26 #include <dali/public-api/images/pixel.h>
27 #include <dali/public-api/object/any.h>
28
29 // INTERNAL INCLUDES
30 #include <dali/public-api/dali-adaptor-common.h>
31
32 namespace Dali
33 {
34 /**
35  * @addtogroup dali_adaptor_framework
36  * @{
37  */
38
39 namespace Internal DALI_INTERNAL
40 {
41 namespace Adaptor
42 {
43 class NativeImageSource;
44 }
45 } // namespace DALI_INTERNAL
46
47 class NativeImageSource;
48 /**
49  * @brief Pointer to Dali::NativeImageSource.
50  * @SINCE_1_0.0
51  */
52 typedef Dali::IntrusivePtr<Dali::NativeImageSource> NativeImageSourcePtr;
53
54 /**
55  * @brief Used for displaying native images.
56  *
57  * NativeImageSource can be created internally or
58  * externally by native image source.
59  * NativeImage is a platform specific way of providing pixel data to the GPU for rendering,
60  * for example via an EGL image.
61  *
62  * @SINCE_1_1.4
63  * @see NativeImage
64  */
65 class DALI_ADAPTOR_API NativeImageSource : public NativeImageInterface
66 {
67 public:
68   /**
69     * @brief Enumeration for the instance when creating a native image, the color depth has to be specified.
70     * @SINCE_1_0.0
71     */
72   enum ColorDepth
73   {
74     COLOR_DEPTH_DEFAULT, ///< Uses the current screen default depth (recommended) @SINCE_1_0.0
75     COLOR_DEPTH_8,       ///< 8 bits per pixel @SINCE_1_0.0
76     COLOR_DEPTH_16,      ///< 16 bits per pixel @SINCE_1_0.0
77     COLOR_DEPTH_24,      ///< 24 bits per pixel @SINCE_1_0.0
78     COLOR_DEPTH_32       ///< 32 bits per pixel @SINCE_1_0.0
79   };
80
81   /**
82    * @brief Creates a new NativeImageSource.
83    *
84    * Depending on hardware, the width and height may have to be a power of two.
85    * @SINCE_1_0.0
86    * @param[in] width The width of the image
87    * @param[in] height The height of the image
88    * @param[in] depth color depth of the image
89    * @return A smart-pointer to a newly allocated image
90    */
91   static NativeImageSourcePtr New(uint32_t width, uint32_t height, ColorDepth depth);
92
93   /**
94    * @brief Creates a new NativeImageSource from an existing native image source.
95    *
96    * @SINCE_1_0.0
97    * @param[in] nativeImageSource NativeImageSource must be a any handle with native image source
98    * @return A smart-pointer to a newly allocated image
99    * @see NativeImageInterface
100    */
101   static NativeImageSourcePtr New(Any nativeImageSource);
102
103   /**
104    * @brief Retrieves the internal native image.
105    *
106    * @SINCE_1_0.0
107    * @return Any object containing the internal native image source
108    */
109   Any GetNativeImageSource();
110
111   /**
112    * @brief Gets a copy of the pixels used by NativeImageSource.
113    *
114    * This is only supported for 24 bit RGB and 32 bit RGBA internal formats
115    * (COLOR_DEPTH_24 and COLOR_DEPTH_32).
116    * @SINCE_1_0.0
117    * @param[out] pixbuf A vector to store the pixels in
118    * @param[out] width The width of image
119    * @param[out] height The height of image
120    * @param[out] pixelFormat pixel format used by image
121    * @return     @c true if the pixels were gotten, and @c false otherwise
122    */
123   bool GetPixels(std::vector<uint8_t>& pixbuf, uint32_t& width, uint32_t& height, Pixel::Format& pixelFormat) const;
124
125   /**
126    * @brief Converts the current pixel contents to either a JPEG or PNG format
127    * and write that to the filesystem.
128    *
129    * @SINCE_1_0.0
130    * @param[in] filename Identify the filesystem location at which to write the encoded image.
131    *                     The extension determines the encoding used.
132    *                     The two valid encoding are (".jpeg"|".jpg") and ".png".
133    * @return    @c true if the pixels were written, and @c false otherwise
134    */
135   bool EncodeToFile(const std::string& filename) const;
136
137   /**
138    * @brief Sets an existing source.
139    *
140    * @SINCE_1_1.19
141    * @param[in] source Any handle with the source
142    */
143   void SetSource(Any source);
144
145   /**
146    * @brief Checks if the specified color depth is supported.
147    *
148    * @SINCE_1_1.34
149    * @param[in] colorDepth The color depth to check
150    * @return @c true if colorDepth is supported, @c false otherwise
151    */
152   bool IsColorDepthSupported(ColorDepth colorDepth);
153
154 public: // native image
155   /**
156    * @copydoc Dali::NativeImageInterface::GetWidth()
157    */
158   uint32_t GetWidth() const override;
159
160   /**
161    * @copydoc Dali::NativeImageInterface::GetHeight()
162    */
163   uint32_t GetHeight() const override;
164
165   /**
166    * @copydoc Dali::NativeImageInterface::GetTextureTarget()
167    */
168   int GetTextureTarget() const override;
169
170   /**
171    * @copydoc Dali::NativeImageInterface::ApplyNativeFragmentShader()
172    */
173   bool ApplyNativeFragmentShader(std::string& shader) override;
174
175   /**
176    * @copydoc Dali::NativeImageInterface::GetCustomSamplerTypename()
177    */
178   const char* GetCustomSamplerTypename() const override;
179
180 private: // native image
181   /**
182    * @copydoc Dali::NativeImageInterface::CreateResource()
183    */
184   bool CreateResource() override;
185
186   /**
187    * @copydoc Dali::NativeImageInterface::DestroyResource()
188    */
189   void DestroyResource() override;
190
191   /**
192    * @copydoc Dali::NativeImageInterface::TargetTexture()
193    */
194   uint32_t TargetTexture() override;
195
196   /**
197    * @copydoc Dali::NativeImageInterface::PrepareTexture()
198    */
199   void PrepareTexture() override;
200
201   /**
202    * @copydoc Dali::NativeImageInterface::RequiresBlending()
203    */
204   bool RequiresBlending() const override;
205
206   /**
207    * @copydoc Dali::NativeImageInterface::GetNativeImageHandle()
208    */
209   Any GetNativeImageHandle() const override;
210
211   /**
212    * @copydoc Dali::NativeImageInterface::SourceChanged()
213    */
214   bool SourceChanged() const override;
215
216   /**
217    * @copydoc Dali::NativeImageInterface::GetUpdatedArea()
218    */
219   Rect<uint32_t> GetUpdatedArea() override;
220
221   /**
222    * @copydoc Dali::NativeImageInterface::GetExtension()
223    */
224   NativeImageInterface::Extension* GetExtension() override;
225
226 private:
227   /// @cond internal
228   /**
229    * @brief Private constructor.
230    * @SINCE_1_0.0
231    * @param[in] width The width of the image
232    * @param[in] height The height of the image
233    * @param[in] depth color depth of the image
234    * @param[in] nativeImageSource contains either: native image source or is empty
235    */
236   DALI_INTERNAL NativeImageSource(uint32_t width, uint32_t height, ColorDepth depth, Any nativeImageSource);
237
238   /**
239    * @brief A reference counted object may only be deleted by calling Unreference().
240    *
241    * The implementation should destroy the NativeImage resources.
242    * @SINCE_1_0.0
243    */
244   DALI_INTERNAL ~NativeImageSource() override;
245
246   /**
247    * @brief Undefined copy constructor.
248    *
249    * This avoids accidental calls to a default copy constructor.
250    * @SINCE_1_0.0
251    * @param[in] nativeImageSource A reference to the object to copy
252    */
253   DALI_INTERNAL NativeImageSource(const NativeImageSource& nativeImageSource);
254
255   /**
256    * @brief Undefined assignment operator.
257    *
258    * This avoids accidental calls to a default assignment operator.
259    * @SINCE_1_0.0
260    * @param[in] rhs A reference to the object to copy
261    */
262   DALI_INTERNAL NativeImageSource& operator=(const NativeImageSource& rhs);
263   /// @endcond
264
265 private:
266   /// @cond internal
267   Internal::Adaptor::NativeImageSource* mImpl; ///< Implementation pointer
268   friend class Internal::Adaptor::NativeImageSource;
269
270   /// @endcond
271 };
272
273 /**
274  * @}
275  */
276 } // namespace Dali
277
278 #endif // DALI_NATIVE_IMAGE_SOURCE_H