[Tizen] Add API for setting resource destruction callback
[platform/core/uifw/dali-adaptor.git] / dali / internal / imaging / windows / native-image-source-impl-win.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/imaging/windows/native-image-source-impl-win.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23
24 // INTERNAL INCLUDES
25 #include <dali/internal/graphics/common/egl-image-extensions.h>
26 #include <dali/internal/graphics/gles/egl-graphics.h>
27 #include <dali/internal/adaptor/common/adaptor-impl.h>
28 #include <dali/internal/window-system/windows/platform-implement-win.h>
29 #include <dali/integration-api/adaptor-framework/render-surface-interface.h>
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 namespace Adaptor
38 {
39 using Dali::Integration::PixelBuffer;
40
41 NativeImageSourceWin* NativeImageSourceWin::New(unsigned int width, unsigned int height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
42 {
43   NativeImageSourceWin* image = new NativeImageSourceWin( width, height, depth, nativeImageSource );
44   DALI_ASSERT_DEBUG( image && "NativeImageSource allocation failed." );
45
46   // 2nd phase construction
47   if(image) //< Defensive in case we ever compile without exceptions.
48   {
49     image->Initialize();
50   }
51
52   return image;
53 }
54
55 NativeImageSourceWin::NativeImageSourceWin( unsigned int width, unsigned int height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
56 : mWidth( width ),
57   mHeight( height ),
58   mOwnPixmap( true ),
59   mPixmap( 0 ),
60   mBlendingRequired( false ),
61   mColorDepth( depth ),
62   mEglImageKHR( NULL ),
63   mEglImageExtensions( NULL ),
64   mResourceDestructionCallback()
65 {
66   DALI_ASSERT_ALWAYS( Adaptor::IsAvailable() );
67
68   GraphicsInterface* graphics = &( Adaptor::GetImplementation( Adaptor::Get() ).GetGraphicsInterface() );
69   auto eglGraphics = static_cast<EglGraphics *>(graphics);
70
71   mEglImageExtensions = eglGraphics->GetImageExtensions();
72
73   DALI_ASSERT_DEBUG( mEglImageExtensions );
74
75   // assign the pixmap
76   mPixmap = GetPixmapFromAny(nativeImageSource);
77 }
78
79 void NativeImageSourceWin::Initialize()
80 {
81   // if pixmap has been created outside of Windows Image we can return
82   if (mPixmap)
83   {
84     // we don't own the pixmap
85     mOwnPixmap = false;
86
87     // find out the pixmap width / height and color depth
88     GetPixmapDetails();
89     return;
90   }
91
92   // get the pixel depth
93   int depth = GetPixelDepth(mColorDepth);
94
95   // set whether blending is required according to pixel format based on the depth
96   /* default pixel format is RGB888
97      If depth = 8, Pixel::A8;
98      If depth = 16, Pixel::RGB565;
99      If depth = 32, Pixel::RGBA8888 */
100   mBlendingRequired = ( depth == 32 || depth == 8 );
101 }
102
103 NativeImageSourceWin::~NativeImageSourceWin()
104 {
105 }
106
107 Any NativeImageSourceWin::GetNativeImageSource() const
108 {
109   return Any(mPixmap);
110 }
111
112 bool NativeImageSourceWin::GetPixels(std::vector<uint8_t>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
113 {
114   DALI_ASSERT_DEBUG(sizeof(unsigned) == 4);
115   bool success = false;
116   width  = mWidth;
117   height = mHeight;
118
119   return success;
120 }
121
122 void NativeImageSourceWin::SetSource( Any source )
123 {
124   mPixmap = GetPixmapFromAny( source );
125
126   if (mPixmap)
127   {
128     // we don't own the pixmap
129     mOwnPixmap = false;
130
131     // find out the pixmap width / height and color depth
132     GetPixmapDetails();
133   }
134 }
135
136 bool NativeImageSourceWin::IsColorDepthSupported( Dali::NativeImageSource::ColorDepth colorDepth )
137 {
138   return true;
139 }
140
141 bool NativeImageSourceWin::CreateResource()
142 {
143   // if the image existed previously delete it.
144   if (mEglImageKHR != NULL)
145   {
146     DestroyResource();
147   }
148
149   // casting from an unsigned int to a void *, which should then be cast back
150   // to an unsigned int in the driver.
151   EGLClientBuffer eglBuffer = reinterpret_cast< EGLClientBuffer > (mPixmap);
152
153   mEglImageKHR = mEglImageExtensions->CreateImageKHR( eglBuffer );
154
155   return mEglImageKHR != NULL;
156 }
157
158 void NativeImageSourceWin::DestroyResource()
159 {
160   mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
161
162   mEglImageKHR = NULL;
163 }
164
165 unsigned int NativeImageSourceWin::TargetTexture()
166 {
167   mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
168
169   return 0;
170 }
171
172 void NativeImageSourceWin::PrepareTexture()
173 {
174 }
175
176 int NativeImageSourceWin::GetPixelDepth(Dali::NativeImageSource::ColorDepth depth) const
177 {
178   switch (depth)
179   {
180     case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
181     {
182       return 32;
183     }
184     case Dali::NativeImageSource::COLOR_DEPTH_8:
185     {
186       return 8;
187     }
188     case Dali::NativeImageSource::COLOR_DEPTH_16:
189     {
190       return 16;
191     }
192     case Dali::NativeImageSource::COLOR_DEPTH_24:
193     {
194       return 24;
195     }
196     case Dali::NativeImageSource::COLOR_DEPTH_32:
197     {
198       return 32;
199     }
200     default:
201     {
202       DALI_ASSERT_DEBUG(0 && "unknown color enum");
203       return 0;
204     }
205   }
206 }
207
208 unsigned int NativeImageSourceWin::GetPixmapFromAny(Any pixmap) const
209 {
210   if (pixmap.Empty())
211   {
212     return 0;
213   }
214
215   // see if it is of type Windows pixmap
216   if (pixmap.GetType() == typeid ( unsigned int ))
217   {
218     // get the Windows pixmap type
219     unsigned int xpixmap = AnyCast<unsigned int>(pixmap);
220
221     // cast it to a Windows pixmap type
222     return static_cast<unsigned int>(xpixmap);
223   }
224   else
225   {
226     return AnyCast<unsigned int>(pixmap);
227   }
228 }
229
230 void NativeImageSourceWin::GetPixmapDetails()
231 {
232 }
233
234 const char* NativeImageSourceWin::GetCustomFragmentPrefix() const
235 {
236   return nullptr;
237 }
238
239 const char* NativeImageSourceWin::GetCustomSamplerTypename() const
240 {
241   return nullptr;
242 }
243
244 int NativeImageSourceWin::GetTextureTarget() const
245 {
246   return GL_TEXTURE_2D;
247 }
248
249 Any NativeImageSourceWin::GetNativeImageHandle() const
250 {
251   return mPixmap;
252 }
253
254 bool NativeImageSourceWin::SourceChanged() const
255 {
256   return false;
257 }
258
259 uint8_t* NativeImageSourceWin::AcquireBuffer( uint16_t& width, uint16_t& height, uint16_t& stride )
260 {
261   return NULL;
262 }
263
264
265 bool NativeImageSourceWin::ReleaseBuffer()
266 {
267   return false;
268 }
269
270 void NativeImageSourceWin::SetResourceDestructionCallback(EventThreadCallback* callback)
271 {
272   mResourceDestructionCallback = std::unique_ptr<EventThreadCallback>(callback);
273 }
274
275 } // namespace Adaptor
276
277 } // namespace internal
278
279 } // namespace Dali