Updates for NativeImageInterface
[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 {
65   DALI_ASSERT_ALWAYS( Adaptor::IsAvailable() );
66
67   GraphicsInterface* graphics = &( Adaptor::GetImplementation( Adaptor::Get() ).GetGraphicsInterface() );
68   auto eglGraphics = static_cast<EglGraphics *>(graphics);
69
70   mEglImageExtensions = eglGraphics->GetImageExtensions();
71
72   DALI_ASSERT_DEBUG( mEglImageExtensions );
73
74   // assign the pixmap
75   mPixmap = GetPixmapFromAny(nativeImageSource);
76 }
77
78 void NativeImageSourceWin::Initialize()
79 {
80   // if pixmap has been created outside of Windows Image we can return
81   if (mPixmap)
82   {
83     // we don't own the pixmap
84     mOwnPixmap = false;
85
86     // find out the pixmap width / height and color depth
87     GetPixmapDetails();
88     return;
89   }
90
91   // get the pixel depth
92   int depth = GetPixelDepth(mColorDepth);
93
94   // set whether blending is required according to pixel format based on the depth
95   /* default pixel format is RGB888
96      If depth = 8, Pixel::A8;
97      If depth = 16, Pixel::RGB565;
98      If depth = 32, Pixel::RGBA8888 */
99   mBlendingRequired = ( depth == 32 || depth == 8 );
100 }
101
102 NativeImageSourceWin::~NativeImageSourceWin()
103 {
104 }
105
106 Any NativeImageSourceWin::GetNativeImageSource() const
107 {
108   return Any(mPixmap);
109 }
110
111 bool NativeImageSourceWin::GetPixels(std::vector<uint8_t>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
112 {
113   DALI_ASSERT_DEBUG(sizeof(unsigned) == 4);
114   bool success = false;
115   width  = mWidth;
116   height = mHeight;
117
118   return success;
119 }
120
121 void NativeImageSourceWin::SetSource( Any source )
122 {
123   mPixmap = GetPixmapFromAny( source );
124
125   if (mPixmap)
126   {
127     // we don't own the pixmap
128     mOwnPixmap = false;
129
130     // find out the pixmap width / height and color depth
131     GetPixmapDetails();
132   }
133 }
134
135 bool NativeImageSourceWin::IsColorDepthSupported( Dali::NativeImageSource::ColorDepth colorDepth )
136 {
137   return true;
138 }
139
140 bool NativeImageSourceWin::CreateResource()
141 {
142   // if the image existed previously delete it.
143   if (mEglImageKHR != NULL)
144   {
145     DestroyResource();
146   }
147
148   // casting from an unsigned int to a void *, which should then be cast back
149   // to an unsigned int in the driver.
150   EGLClientBuffer eglBuffer = reinterpret_cast< EGLClientBuffer > (mPixmap);
151
152   mEglImageKHR = mEglImageExtensions->CreateImageKHR( eglBuffer );
153
154   return mEglImageKHR != NULL;
155 }
156
157 void NativeImageSourceWin::DestroyResource()
158 {
159   mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
160
161   mEglImageKHR = NULL;
162 }
163
164 unsigned int NativeImageSourceWin::TargetTexture()
165 {
166   mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
167
168   return 0;
169 }
170
171 void NativeImageSourceWin::PrepareTexture()
172 {
173 }
174
175 int NativeImageSourceWin::GetPixelDepth(Dali::NativeImageSource::ColorDepth depth) const
176 {
177   switch (depth)
178   {
179     case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
180     {
181       return 32;
182     }
183     case Dali::NativeImageSource::COLOR_DEPTH_8:
184     {
185       return 8;
186     }
187     case Dali::NativeImageSource::COLOR_DEPTH_16:
188     {
189       return 16;
190     }
191     case Dali::NativeImageSource::COLOR_DEPTH_24:
192     {
193       return 24;
194     }
195     case Dali::NativeImageSource::COLOR_DEPTH_32:
196     {
197       return 32;
198     }
199     default:
200     {
201       DALI_ASSERT_DEBUG(0 && "unknown color enum");
202       return 0;
203     }
204   }
205 }
206
207 unsigned int NativeImageSourceWin::GetPixmapFromAny(Any pixmap) const
208 {
209   if (pixmap.Empty())
210   {
211     return 0;
212   }
213
214   // see if it is of type Windows pixmap
215   if (pixmap.GetType() == typeid ( unsigned int ))
216   {
217     // get the Windows pixmap type
218     unsigned int xpixmap = AnyCast<unsigned int>(pixmap);
219
220     // cast it to a Windows pixmap type
221     return static_cast<unsigned int>(xpixmap);
222   }
223   else
224   {
225     return AnyCast<unsigned int>(pixmap);
226   }
227 }
228
229 void NativeImageSourceWin::GetPixmapDetails()
230 {
231 }
232
233 const char* NativeImageSourceWin::GetCustomFragmentPrefix() const
234 {
235   return nullptr;
236 }
237
238 const char* NativeImageSourceWin::GetCustomSamplerTypename() const
239 {
240   return nullptr;
241 }
242
243 int NativeImageSourceWin::GetTextureTarget() const
244 {
245   return GL_TEXTURE_2D;
246 }
247
248 Any NativeImageSourceWin::GetNativeImageHandle() const
249 {
250   return mPixmap;
251 }
252
253 bool NativeImageSourceWin::SourceChanged() const
254 {
255   return false;
256 }
257
258 uint8_t* NativeImageSourceWin::AcquireBuffer( uint16_t& width, uint16_t& height, uint16_t& stride )
259 {
260   return NULL;
261 }
262
263
264 bool NativeImageSourceWin::ReleaseBuffer()
265 {
266   return false;
267 }
268
269 } // namespace Adaptor
270
271 } // namespace internal
272
273 } // namespace Dali