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