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