Add API for setting resource destruction callback
[platform/core/uifw/dali-adaptor.git] / dali / internal / imaging / android / native-image-source-impl-android.h
1 #ifndef DALI_INTERNAL_NATIVE_IMAGE_SOURCE_H
2 #define DALI_INTERNAL_NATIVE_IMAGE_SOURCE_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 // EXTERNAL INCLUDES
22 #include <hardware_buffer.h>
23
24 // INTERNAL INCLUDES
25 #include <dali/internal/graphics/common/graphics-interface.h>
26
27 #include <dali/public-api/adaptor-framework/native-image-source.h>
28
29 #include <dali/internal/imaging/common/native-image-source-impl.h>
30
31 namespace Dali
32 {
33 namespace Internal
34 {
35 namespace Adaptor
36 {
37 class EglImageExtensions;
38
39 /**
40  * Dali internal NativeImageSource.
41  */
42 class NativeImageSourceAndroid : public Internal::Adaptor::NativeImageSource
43 {
44 public:
45   /**
46    * Create a new NativeImageSource internally.
47    * Depending on hardware the width and height may have to be a power of two.
48    * @param[in] width The width of the image.
49    * @param[in] height The height of the image.
50    * @param[in] depth color depth of the image.
51    * @param[in] nativeImageSource contains either: Android hardware buffer or is empty
52    * @return A smart-pointer to a newly allocated image.
53    */
54   static NativeImageSourceAndroid* New(uint32_t                            width,
55                                        uint32_t                            height,
56                                        Dali::NativeImageSource::ColorDepth depth,
57                                        Any                                 nativeImageSource);
58   /**
59    * @copydoc Dali::NativeImageSource::GetNativeImageSource()
60    */
61   Any GetNativeImageSource() const override;
62
63   /**
64    * @copydoc Dali::NativeImageSource::GetPixels()
65    */
66   bool GetPixels(std::vector<unsigned char>& pixbuf, uint32_t& width, uint32_t& height, Pixel::Format& pixelFormat) const override;
67
68   /**
69    * @copydoc Dali::NativeImageSource::SetSource( Any source )
70    */
71   void SetSource(Any source) override;
72
73   /**
74    * @copydoc Dali::NativeImageSource::IsColorDepthSupported( ColorDepth colorDepth )
75    */
76   bool IsColorDepthSupported(Dali::NativeImageSource::ColorDepth colorDepth) override;
77
78   /**
79    * destructor
80    */
81   ~NativeImageSourceAndroid() override;
82
83   /**
84    * @copydoc Dali::NativeImageSource::CreateResource()
85    */
86   bool CreateResource() override;
87
88   /**
89    * @copydoc Dali::NativeImageSource::DestroyResource()
90    */
91   void DestroyResource() override;
92
93   /**
94    * @copydoc Dali::NativeImageSource::TargetTexture()
95    */
96   uint32_t TargetTexture() override;
97
98   /**
99    * @copydoc Dali::NativeImageSource::PrepareTexture()
100    */
101   void PrepareTexture() override;
102
103   /**
104    * @copydoc Dali::NativeImageSource::GetWidth()
105    */
106   uint32_t GetWidth() const override
107   {
108     return mWidth;
109   }
110
111   /**
112    * @copydoc Dali::NativeImageSource::GetHeight()
113    */
114   uint32_t GetHeight() const override
115   {
116     return mHeight;
117   }
118
119   /**
120    * @copydoc Dali::NativeImageSource::RequiresBlending()
121    */
122   bool RequiresBlending() const override
123   {
124     return mBlendingRequired;
125   }
126
127   /**
128    * @copydoc Dali::NativeImageSource::GetTargetTexture()
129    */
130   int GetTextureTarget() const override;
131
132   /**
133    * @copydoc Dali::NativeImageSource::ApplyNativeFragmentShader()
134    */
135   bool ApplyNativeFragmentShader(std::string& shader) override;
136
137   /**
138    * @copydoc Dali::NativeImageSource::GetCustomSamplerTypename()
139    */
140   const char* GetCustomSamplerTypename() const override;
141
142   /**
143    * @copydoc Dali::NativeImageSource::GetNativeImageHandle()
144    */
145   Any GetNativeImageHandle() const override
146   {
147     return GetNativeImageSource();
148   }
149
150   /**
151    * @copydoc Dali::NativeImageSource::SourceChanged()
152    */
153   bool SourceChanged() const override
154   {
155     return false;
156   }
157
158   /**
159    * @copydoc Dali::NativeImageInterface::GetExtension()
160    */
161   NativeImageInterface::Extension* GetNativeImageInterfaceExtension() override
162   {
163     return nullptr;
164   }
165
166   /**
167    * @copydoc Dali::Internal::Adaptor::NativeImageSource::AcquireBuffer()
168    */
169   uint8_t* AcquireBuffer(uint16_t& width, uint16_t& height, uint16_t& stride) override;
170
171   /**
172    * @copydoc Dali::Internal::Adaptor::NativeImageSource::ReleaseBuffer()
173    */
174   bool ReleaseBuffer() override;
175
176   /**
177    * @copydoc Dali::NativeImageSource::SetResourceDestructionCallback()
178    */
179   void SetResourceDestructionCallback(EventThreadCallback* callback) override;
180
181 private:
182   /**
183    * Private constructor; @see NativeImageSource::New()
184    * @param[in] width The width of the image.
185    * @param[in] height The height of the image.
186    * @param[in] colour depth of the image.
187    * @param[in] nativeImageSource contains either: pixmap of type X11 Pixmap , a Ecore_X_Pixmap or is empty
188    */
189   NativeImageSourceAndroid(uint32_t                            width,
190                            uint32_t                            height,
191                            Dali::NativeImageSource::ColorDepth depth,
192                            Any                                 nativeImageSource);
193
194   /**
195    * 2nd phase construction.
196    */
197   void Initialize();
198
199   /**
200    * Gets the pixmap from the Any parameter
201    * @param pixmap contains either: pixmap of type Android hardware buffer or empty
202    * @return pixmap pixmap
203    */
204   void* GetPixmapFromAny(Any pixmap) const;
205
206   /**
207    * Given an existing pixmap, the function uses Android hardware buffer API to find
208    * the width, heigth and depth of that pixmap.
209    */
210   void GetPixmapDetails();
211
212 private:
213   uint32_t                             mWidth;                        ///< image width
214   uint32_t                             mHeight;                       ///< image heights
215   bool                                 mOwnPixmap;                    ///< Whether we created pixmap or not
216   AHardwareBuffer*                     mPixmap;                       ///<
217   bool                                 mBlendingRequired;             ///< Whether blending is required
218   Dali::NativeImageSource::ColorDepth  mColorDepth;                   ///< color depth of image
219   void*                                mEglImageKHR;                  ///< From EGL extension
220   EglImageExtensions*                  mEglImageExtensions;           ///< The EGL Image Extensions
221   std::unique_ptr<EventThreadCallback> mResourceDestructionCallback;  ///< The Resource Destruction Callback
222 };
223
224 } // namespace Adaptor
225
226 } // namespace Internal
227
228 } // namespace Dali
229
230 #endif // DALI_INTERNAL_NATIVE_IMAGE_SOURCE_H