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