e76cfcaf3791f4f1346d14f574b3796531aa9a26
[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) 2019 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 }
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    /**
70     * @brief Enumeration for the instance when creating a native image, the color depth has to be specified.
71     * @SINCE_1_0.0
72     */
73    enum ColorDepth
74    {
75      COLOR_DEPTH_DEFAULT,     ///< Uses the current screen default depth (recommended) @SINCE_1_0.0
76      COLOR_DEPTH_8,           ///< 8 bits per pixel @SINCE_1_0.0
77      COLOR_DEPTH_16,          ///< 16 bits per pixel @SINCE_1_0.0
78      COLOR_DEPTH_24,          ///< 24 bits per pixel @SINCE_1_0.0
79      COLOR_DEPTH_32           ///< 32 bits per pixel @SINCE_1_0.0
80    };
81
82   /**
83    * @brief Creates a new NativeImageSource.
84    *
85    * Depending on hardware, the width and height may have to be a power of two.
86    * @SINCE_1_0.0
87    * @param[in] width The width of the image
88    * @param[in] height The height of the image
89    * @param[in] depth color depth of the image
90    * @return A smart-pointer to a newly allocated image
91    */
92   static NativeImageSourcePtr New( unsigned int width, unsigned int height, ColorDepth depth );
93
94   /**
95    * @brief Creates a new NativeImageSource from an existing native image source.
96    *
97    * @SINCE_1_0.0
98    * @param[in] nativeImageSource NativeImageSource must be a any handle with native image source
99    * @return A smart-pointer to a newly allocated image
100    * @see NativeImageInterface
101    */
102   static NativeImageSourcePtr New( Any nativeImageSource );
103
104   /**
105    * @brief Retrieves the internal native image.
106    *
107    * @SINCE_1_0.0
108    * @return Any object containing the internal native image source
109    */
110   Any GetNativeImageSource();
111
112   /**
113    * @brief Gets a copy of the pixels used by NativeImageSource.
114    *
115    * This is only supported for 24 bit RGB and 32 bit RGBA internal formats
116    * (COLOR_DEPTH_24 and COLOR_DEPTH_32).
117    * @SINCE_1_0.0
118    * @param[out] pixbuf A vector to store the pixels in
119    * @param[out] width The width of image
120    * @param[out] height The height of image
121    * @param[out] pixelFormat pixel format used by image
122    * @return     @c true if the pixels were gotten, and @c false otherwise
123    */
124   bool GetPixels( std::vector<unsigned char>& pixbuf, unsigned int& width, unsigned int& height, Pixel::Format& pixelFormat ) const;
125
126   /**
127    * @brief Converts the current pixel contents to either a JPEG or PNG format
128    * and write that to the filesystem.
129    *
130    * @SINCE_1_0.0
131    * @param[in] filename Identify the filesystem location at which to write the encoded image.
132    *                     The extension determines the encoding used.
133    *                     The two valid encoding are (".jpeg"|".jpg") and ".png".
134    * @return    @c true if the pixels were written, and @c false otherwise
135    */
136   bool EncodeToFile(const std::string& filename) const;
137
138   /**
139    * @brief Sets an existing source.
140    *
141    * @SINCE_1_1.19
142    * @param[in] source Any handle with the source
143    */
144   void SetSource( Any source );
145
146   /**
147    * @brief Checks if the specified color depth is supported.
148    *
149    * @SINCE_1_1.34
150    * @param[in] colorDepth The color depth to check
151    * @return @c true if colorDepth is supported, @c false otherwise
152    */
153   bool IsColorDepthSupported( ColorDepth colorDepth );
154
155 private:   // native image
156
157   /**
158    * @copydoc Dali::NativeImageInterface::GlExtensionCreate()
159    */
160   virtual bool GlExtensionCreate();
161
162   /**
163    * @copydoc Dali::NativeImageInterface::GlExtensionDestroy()
164    */
165   virtual void GlExtensionDestroy();
166
167   /**
168    * @copydoc Dali::NativeImageInterface::TargetTexture()
169    */
170   virtual unsigned int TargetTexture();
171
172   /**
173    * @copydoc Dali::NativeImageInterface::PrepareTexture()
174    */
175   virtual void PrepareTexture();
176
177   /**
178    * @copydoc Dali::NativeImageInterface::GetWidth()
179    */
180   virtual unsigned int GetWidth() const;
181
182   /**
183    * @copydoc Dali::NativeImageInterface::GetHeight()
184    */
185   virtual unsigned int GetHeight() const;
186
187   /**
188    * @copydoc Dali::NativeImageInterface::RequiresBlending()
189    */
190   virtual bool RequiresBlending() const;
191
192   /**
193    * @copydoc Dali::NativeImageInterface::GetExtension()
194    */
195   NativeImageInterface::Extension* GetExtension();
196
197 private:
198
199   /// @cond internal
200   /**
201    * @brief Private constructor.
202    * @SINCE_1_0.0
203    * @param[in] width The width of the image
204    * @param[in] height The height of the image
205    * @param[in] depth color depth of the image
206    * @param[in] nativeImageSource contains either: native image source or is empty
207    */
208   DALI_INTERNAL NativeImageSource( unsigned int width, unsigned int height, ColorDepth depth, Any nativeImageSource );
209
210   /**
211    * @brief A reference counted object may only be deleted by calling Unreference().
212    *
213    * The implementation should destroy the NativeImage resources.
214    * @SINCE_1_0.0
215    */
216   DALI_INTERNAL virtual ~NativeImageSource();
217
218   /**
219    * @brief Undefined copy constructor.
220    *
221    * This avoids accidental calls to a default copy constructor.
222    * @SINCE_1_0.0
223    * @param[in] nativeImageSource A reference to the object to copy
224    */
225   DALI_INTERNAL NativeImageSource( const NativeImageSource& nativeImageSource );
226
227   /**
228    * @brief Undefined assignment operator.
229    *
230    * This avoids accidental calls to a default assignment operator.
231    * @SINCE_1_0.0
232    * @param[in] rhs A reference to the object to copy
233    */
234   DALI_INTERNAL NativeImageSource& operator=(const NativeImageSource& rhs);
235   /// @endcond
236
237 private:
238
239   /// @cond internal
240   Internal::Adaptor::NativeImageSource* mImpl; ///< Implementation pointer
241   /// @endcond
242 };
243
244 /**
245  * @}
246  */
247 } // namespace Dali
248
249 #endif // DALI_NATIVE_IMAGE_SOURCE_H