2 * Copyright (c) 2023 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/integration-api/debug.h>
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>
37 using Dali::Integration::PixelBuffer;
39 NativeImageSourceWin* NativeImageSourceWin::New(uint32_t width, uint32_t height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource)
41 NativeImageSourceWin* image = new NativeImageSourceWin(width, height, depth, nativeImageSource);
42 DALI_ASSERT_DEBUG(image && "NativeImageSource allocation failed.");
44 // 2nd phase construction
45 if(image) //< Defensive in case we ever compile without exceptions.
53 NativeImageSourceWin::NativeImageSourceWin(uint32_t width, uint32_t height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource)
58 mBlendingRequired(false),
62 mEglImageExtensions(NULL),
63 mResourceDestructionCallback()
65 DALI_ASSERT_ALWAYS(Adaptor::IsAvailable());
67 GraphicsInterface* graphics = &(Adaptor::GetImplementation(Adaptor::Get()).GetGraphicsInterface());
68 mEglGraphics = static_cast<EglGraphics*>(graphics);
71 mPixmap = GetPixmapFromAny(nativeImageSource);
74 void NativeImageSourceWin::Initialize()
76 // if pixmap has been created outside of Windows Image we can return
79 // we don't own the pixmap
82 // find out the pixmap width / height and color depth
87 // get the pixel depth
88 int depth = GetPixelDepth(mColorDepth);
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);
98 NativeImageSourceWin::~NativeImageSourceWin()
102 Any NativeImageSourceWin::GetNativeImageSource() const
107 bool NativeImageSourceWin::GetPixels(std::vector<uint8_t>& pixbuf, uint32_t& width, uint32_t& height, Pixel::Format& pixelFormat) const
109 DALI_ASSERT_DEBUG(sizeof(uint32_t) == 4);
110 bool success = false;
117 void NativeImageSourceWin::SetSource(Any source)
119 mPixmap = GetPixmapFromAny(source);
123 // we don't own the pixmap
126 // find out the pixmap width / height and color depth
131 bool NativeImageSourceWin::IsColorDepthSupported(Dali::NativeImageSource::ColorDepth colorDepth)
136 bool NativeImageSourceWin::CreateResource()
138 mEglImageExtensions = mEglGraphics->GetImageExtensions();
139 DALI_ASSERT_DEBUG(mEglImageExtensions);
141 // if the image existed previously delete it.
142 if(mEglImageKHR != NULL)
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);
151 mEglImageKHR = mEglImageExtensions->CreateImageKHR(eglBuffer);
153 return mEglImageKHR != NULL;
156 void NativeImageSourceWin::DestroyResource()
158 mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
162 if(mResourceDestructionCallback)
164 mResourceDestructionCallback->Trigger();
168 uint32_t NativeImageSourceWin::TargetTexture()
170 mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
175 void NativeImageSourceWin::PrepareTexture()
179 int NativeImageSourceWin::GetPixelDepth(Dali::NativeImageSource::ColorDepth depth) const
183 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
187 case Dali::NativeImageSource::COLOR_DEPTH_8:
191 case Dali::NativeImageSource::COLOR_DEPTH_16:
195 case Dali::NativeImageSource::COLOR_DEPTH_24:
199 case Dali::NativeImageSource::COLOR_DEPTH_32:
205 DALI_ASSERT_DEBUG(0 && "unknown color enum");
211 unsigned int NativeImageSourceWin::GetPixmapFromAny(Any pixmap) const
218 // see if it is of type Windows pixmap
219 if(pixmap.GetType() == typeid(unsigned int))
221 // get the Windows pixmap type
222 unsigned int xpixmap = AnyCast<unsigned int>(pixmap);
224 // cast it to a Windows pixmap type
225 return static_cast<unsigned int>(xpixmap);
229 return AnyCast<unsigned int>(pixmap);
233 void NativeImageSourceWin::GetPixmapDetails()
237 bool NativeImageSourceWin::ApplyNativeFragmentShader(std::string& shader)
242 const char* NativeImageSourceWin::GetCustomSamplerTypename() const
247 int NativeImageSourceWin::GetTextureTarget() const
249 return GL_TEXTURE_2D;
252 Any NativeImageSourceWin::GetNativeImageHandle() const
257 bool NativeImageSourceWin::SourceChanged() const
262 uint8_t* NativeImageSourceWin::AcquireBuffer(uint32_t& width, uint32_t& height, uint32_t& stride)
267 bool NativeImageSourceWin::ReleaseBuffer(const Rect<uint32_t>& updatedArea)
272 void NativeImageSourceWin::SetResourceDestructionCallback(EventThreadCallback* callback)
274 mResourceDestructionCallback = std::unique_ptr<EventThreadCallback>(callback);
277 void NativeImageSourceWin::EnableBackBuffer(bool enable)
281 } // namespace Adaptor
283 } // namespace Internal