Updates for NativeImageInterface
[platform/core/uifw/dali-adaptor.git] / dali / internal / imaging / common / native-image-source-impl.h
1 #ifndef DALI_INTERNAL_NATIVE_IMAGE_SOURCE_IMPL_H
2 #define DALI_INTERNAL_NATIVE_IMAGE_SOURCE_IMPL_H
3
4 /*
5  * Copyright (c) 2020 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/public-api/adaptor-framework/native-image-source.h>
23 #include <dali/devel-api/adaptor-framework/bitmap-saver.h>
24
25 namespace Dali
26 {
27
28 namespace Internal
29 {
30
31 namespace Adaptor
32 {
33
34 /**
35  * Dali internal NativeImageSource.
36  */
37 class NativeImageSource
38 {
39 public:
40
41   static constexpr uint32_t DEFAULT_QUALITY = 100;
42
43   /**
44    * Create a new NativeImageSource internally.
45    * Depending on hardware the width and height may have to be a power of two.
46    * @param[in] width The width of the image.
47    * @param[in] height The height of the image.
48    * @param[in] depth color depth of the image.
49    * @param[in] nativeImageSource contains either: pixmap of type X11 Pixmap , a Ecore_X_Pixmap or is empty
50    * @return A smart-pointer to a newly allocated image.
51    */
52   static NativeImageSource* New(uint32_t width,
53                                 uint32_t height,
54                                 Dali::NativeImageSource::ColorDepth depth,
55                                 Any nativeImageSource);
56   /**
57    * @copydoc Dali::NativeImageSource::GetNativeImageSource()
58    */
59   virtual Any GetNativeImageSource() const = 0;
60
61   /**
62    * @copydoc Dali::NativeImageSource::GetPixels()
63    */
64   virtual bool GetPixels(std::vector<unsigned char> &pixbuf, uint32_t &width, uint32_t &height, Pixel::Format& pixelFormat ) const = 0;
65
66   /**
67    * @copydoc Dali::NativeImageSource::SetSource( Any source )
68    */
69   virtual void SetSource( Any source ) = 0;
70
71   /**
72    * @copydoc Dali::NativeImageSource::IsColorDepthSupported( ColorDepth colorDepth )
73    */
74   virtual bool IsColorDepthSupported( Dali::NativeImageSource::ColorDepth colorDepth ) = 0;
75
76   /**
77    * destructor
78    */
79   virtual ~NativeImageSource() = default;
80
81   /**
82    * @copydoc Dali::NativeImageSource::CreateResource()
83    */
84   virtual bool CreateResource() = 0;
85
86   /**
87    * @copydoc Dali::NativeImageSource::DestroyResource()
88    */
89   virtual void DestroyResource() = 0;
90
91   /**
92    * @copydoc Dali::NativeImageSource::TargetTexture()
93    */
94   virtual uint32_t TargetTexture() = 0;
95
96   /**
97    * @copydoc Dali::NativeImageSource::PrepareTexture()
98    */
99   virtual void PrepareTexture() = 0;
100
101   /**
102    * @copydoc Dali::NativeImageSource::GetWidth()
103    */
104   virtual uint32_t GetWidth() const = 0;
105
106   /**
107    * @copydoc Dali::NativeImageSource::GetHeight()
108    */
109   virtual uint32_t GetHeight() const = 0;
110
111   /**
112    * @copydoc Dali::NativeImageSource::RequiresBlending()
113    */
114   virtual bool RequiresBlending() const = 0;
115
116   /**
117    * @copydoc Dali::NativeImageSource::GetTextureTarget()
118    */
119   virtual int GetTextureTarget() const = 0;
120
121   /**
122    * @copydoc Dali::NativeImageSource::GetCustomFragmentPrefix()
123    */
124   virtual const char* GetCustomFragmentPrefix() const = 0;
125
126   /**
127    * @copydoc Dali::NativeImageSource::GetCustomSamplerTypename()
128    */
129   virtual const char* GetCustomSamplerTypename() const = 0;
130
131   /**
132    * @copydoc Dali::NativeImageSource::GetNativeImageHandle()
133    */
134   virtual Any GetNativeImageHandle() const = 0;
135
136   /**
137    * @copydoc Dali::NativeImageSource::SourceChanged()
138    */
139   virtual bool SourceChanged() const = 0;
140
141   /**
142    * @copydoc Dali::NativeImageInterface::GetExtension()
143    */
144   virtual NativeImageInterface::Extension* GetNativeImageInterfaceExtension() = 0;
145
146   /**
147    * @brief Dali::DevelNativeImageSource::AcquireBuffer()
148    */
149   virtual uint8_t* AcquireBuffer( uint16_t& width, uint16_t& height, uint16_t& stride ) = 0;
150
151   /**
152    * @brief Dali::DevelNativeImageSource::ReleaseBuffer()
153    */
154   virtual bool ReleaseBuffer() = 0;
155
156   /**
157    * @copydoc Dali::NativeImageSource::EncodeToFile(const std::string& )
158    */
159   inline bool EncodeToFile( const std::string& filename ) const
160   {
161     return EncodeToFile( filename, DEFAULT_QUALITY );
162   }
163
164   /**
165    * @brief Converts the current pixel contents to either a JPEG or PNG format
166    * and write that to the filesystem.
167    *
168    * @param[in] filename Identify the filesystem location at which to write the encoded image.
169    *                     The extension determines the encoding used.
170    *                     The two valid encoding are (".jpeg"|".jpg") and ".png".
171    * @param[in] quality The quality of encoded jpeg image
172    * @return    @c true if the pixels were written, and @c false otherwise
173    */
174   inline bool EncodeToFile( const std::string& filename, const uint32_t quality ) const
175   {
176     std::vector< uint8_t > pixbuf;
177     uint32_t width( 0 ), height( 0 );
178     Pixel::Format pixelFormat;
179
180     if( GetPixels( pixbuf, width, height, pixelFormat ) )
181     {
182       return Dali::EncodeToFile( &pixbuf[0], filename, pixelFormat, width, height, quality );
183     }
184     return false;
185   }
186
187 public:
188   inline static Internal::Adaptor::NativeImageSource& GetImplementation( Dali::NativeImageSource& image ) { return *image.mImpl; }
189 };
190
191 } // namespace Adaptor
192
193 } // namespace Internal
194
195 } // namespace Dali
196
197 #endif // DALI_INTERNAL_NATIVE_IMAGE_SOURCE_IMPL_H