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