2 * Copyright (c) 2021 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(unsigned int width, unsigned int 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(unsigned int width, unsigned int height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource)
58 mBlendingRequired(false),
61 mEglImageExtensions(NULL)
63 DALI_ASSERT_ALWAYS(Adaptor::IsAvailable());
65 GraphicsInterface* graphics = &(Adaptor::GetImplementation(Adaptor::Get()).GetGraphicsInterface());
66 auto eglGraphics = static_cast<EglGraphics*>(graphics);
68 mEglImageExtensions = eglGraphics->GetImageExtensions();
70 DALI_ASSERT_DEBUG(mEglImageExtensions);
73 mPixmap = GetPixmapFromAny(nativeImageSource);
76 void NativeImageSourceWin::Initialize()
78 // if pixmap has been created outside of Windows Image we can return
81 // we don't own the pixmap
84 // find out the pixmap width / height and color depth
89 // get the pixel depth
90 int depth = GetPixelDepth(mColorDepth);
92 // set whether blending is required according to pixel format based on the depth
93 /* default pixel format is RGB888
94 If depth = 8, Pixel::A8;
95 If depth = 16, Pixel::RGB565;
96 If depth = 32, Pixel::RGBA8888 */
97 mBlendingRequired = (depth == 32 || depth == 8);
100 NativeImageSourceWin::~NativeImageSourceWin()
104 Any NativeImageSourceWin::GetNativeImageSource() const
109 bool NativeImageSourceWin::GetPixels(std::vector<uint8_t>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
111 DALI_ASSERT_DEBUG(sizeof(unsigned) == 4);
112 bool success = false;
119 void NativeImageSourceWin::SetSource(Any source)
121 mPixmap = GetPixmapFromAny(source);
125 // we don't own the pixmap
128 // find out the pixmap width / height and color depth
133 bool NativeImageSourceWin::IsColorDepthSupported(Dali::NativeImageSource::ColorDepth colorDepth)
138 bool NativeImageSourceWin::CreateResource()
140 // if the image existed previously delete it.
141 if(mEglImageKHR != NULL)
146 // casting from an unsigned int to a void *, which should then be cast back
147 // to an unsigned int in the driver.
148 EGLClientBuffer eglBuffer = reinterpret_cast<EGLClientBuffer>(mPixmap);
150 mEglImageKHR = mEglImageExtensions->CreateImageKHR(eglBuffer);
152 return mEglImageKHR != NULL;
155 void NativeImageSourceWin::DestroyResource()
157 mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
162 unsigned int NativeImageSourceWin::TargetTexture()
164 mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
169 void NativeImageSourceWin::PrepareTexture()
173 int NativeImageSourceWin::GetPixelDepth(Dali::NativeImageSource::ColorDepth depth) const
177 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
181 case Dali::NativeImageSource::COLOR_DEPTH_8:
185 case Dali::NativeImageSource::COLOR_DEPTH_16:
189 case Dali::NativeImageSource::COLOR_DEPTH_24:
193 case Dali::NativeImageSource::COLOR_DEPTH_32:
199 DALI_ASSERT_DEBUG(0 && "unknown color enum");
205 unsigned int NativeImageSourceWin::GetPixmapFromAny(Any pixmap) const
212 // see if it is of type Windows pixmap
213 if(pixmap.GetType() == typeid(unsigned int))
215 // get the Windows pixmap type
216 unsigned int xpixmap = AnyCast<unsigned int>(pixmap);
218 // cast it to a Windows pixmap type
219 return static_cast<unsigned int>(xpixmap);
223 return AnyCast<unsigned int>(pixmap);
227 void NativeImageSourceWin::GetPixmapDetails()
231 bool NativeImageSourceWin::ApplyNativeFragmentShader(std::string& shader)
236 const char* NativeImageSourceWin::GetCustomSamplerTypename() const
241 int NativeImageSourceWin::GetTextureTarget() const
243 return GL_TEXTURE_2D;
246 Any NativeImageSourceWin::GetNativeImageHandle() const
251 bool NativeImageSourceWin::SourceChanged() const
256 uint8_t* NativeImageSourceWin::AcquireBuffer(uint16_t& width, uint16_t& height, uint16_t& stride)
261 bool NativeImageSourceWin::ReleaseBuffer()
266 } // namespace Adaptor
268 } // namespace Internal