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