2 * Copyright (c) 2024 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/imaging/windows/native-image-source-impl-win.h>
22 #include <dali/devel-api/common/stage.h>
23 #include <dali/integration-api/debug.h>
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>
38 using Dali::Integration::PixelBuffer;
40 NativeImageSourceWin* NativeImageSourceWin::New(uint32_t width, uint32_t height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource)
42 NativeImageSourceWin* image = new NativeImageSourceWin(width, height, depth, nativeImageSource);
43 DALI_ASSERT_DEBUG(image && "NativeImageSource allocation failed.");
45 // 2nd phase construction
46 if(image) //< Defensive in case we ever compile without exceptions.
54 NativeImageSourceWin::NativeImageSourceWin(uint32_t width, uint32_t height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource)
59 mBlendingRequired(false),
63 mEglImageExtensions(NULL),
64 mResourceDestructionCallback()
66 DALI_ASSERT_ALWAYS(Dali::Stage::IsCoreThread() && "Core is not installed. Might call this API from worker thread?");
68 GraphicsInterface* graphics = &(Adaptor::GetImplementation(Adaptor::Get()).GetGraphicsInterface());
69 mEglGraphics = static_cast<EglGraphics*>(graphics);
72 mPixmap = GetPixmapFromAny(nativeImageSource);
75 void NativeImageSourceWin::Initialize()
77 // if pixmap has been created outside of Windows Image we can return
80 // we don't own the pixmap
83 // find out the pixmap width / height and color depth
88 // get the pixel depth
89 int depth = GetPixelDepth(mColorDepth);
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);
99 NativeImageSourceWin::~NativeImageSourceWin()
103 Any NativeImageSourceWin::GetNativeImageSource() const
108 bool NativeImageSourceWin::GetPixels(std::vector<uint8_t>& pixbuf, uint32_t& width, uint32_t& height, Pixel::Format& pixelFormat) const
110 DALI_ASSERT_DEBUG(sizeof(uint32_t) == 4);
111 bool success = false;
118 void NativeImageSourceWin::SetSource(Any source)
120 mPixmap = GetPixmapFromAny(source);
124 // we don't own the pixmap
127 // find out the pixmap width / height and color depth
132 bool NativeImageSourceWin::IsColorDepthSupported(Dali::NativeImageSource::ColorDepth colorDepth)
137 bool NativeImageSourceWin::CreateResource()
139 mEglImageExtensions = mEglGraphics->GetImageExtensions();
140 DALI_ASSERT_DEBUG(mEglImageExtensions);
142 // if the image existed previously delete it.
143 if(mEglImageKHR != NULL)
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);
152 mEglImageKHR = mEglImageExtensions->CreateImageKHR(eglBuffer);
154 return mEglImageKHR != NULL;
157 void NativeImageSourceWin::DestroyResource()
159 mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
163 if(mResourceDestructionCallback)
165 mResourceDestructionCallback->Trigger();
169 uint32_t NativeImageSourceWin::TargetTexture()
171 mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
176 void NativeImageSourceWin::PrepareTexture()
180 int NativeImageSourceWin::GetPixelDepth(Dali::NativeImageSource::ColorDepth depth) const
184 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
188 case Dali::NativeImageSource::COLOR_DEPTH_8:
192 case Dali::NativeImageSource::COLOR_DEPTH_16:
196 case Dali::NativeImageSource::COLOR_DEPTH_24:
200 case Dali::NativeImageSource::COLOR_DEPTH_32:
206 DALI_ASSERT_DEBUG(0 && "unknown color enum");
212 unsigned int NativeImageSourceWin::GetPixmapFromAny(Any pixmap) const
219 // see if it is of type Windows pixmap
220 if(pixmap.GetType() == typeid(unsigned int))
222 // get the Windows pixmap type
223 unsigned int xpixmap = AnyCast<unsigned int>(pixmap);
225 // cast it to a Windows pixmap type
226 return static_cast<unsigned int>(xpixmap);
230 return AnyCast<unsigned int>(pixmap);
234 void NativeImageSourceWin::GetPixmapDetails()
238 bool NativeImageSourceWin::ApplyNativeFragmentShader(std::string& shader)
243 const char* NativeImageSourceWin::GetCustomSamplerTypename() const
248 int NativeImageSourceWin::GetTextureTarget() const
250 return GL_TEXTURE_2D;
253 Any NativeImageSourceWin::GetNativeImageHandle() const
258 bool NativeImageSourceWin::SourceChanged() const
263 uint8_t* NativeImageSourceWin::AcquireBuffer(uint32_t& width, uint32_t& height, uint32_t& stride)
268 bool NativeImageSourceWin::ReleaseBuffer(const Rect<uint32_t>& updatedArea)
273 void NativeImageSourceWin::SetResourceDestructionCallback(EventThreadCallback* callback)
275 mResourceDestructionCallback = std::unique_ptr<EventThreadCallback>(callback);
278 void NativeImageSourceWin::EnableBackBuffer(bool enable)
282 } // namespace Adaptor
284 } // namespace Internal