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),
62 mResourceDestructionCallback()
64 DALI_ASSERT_ALWAYS(Adaptor::IsAvailable());
66 GraphicsInterface* graphics = &(Adaptor::GetImplementation(Adaptor::Get()).GetGraphicsInterface());
67 auto eglGraphics = static_cast<EglGraphics*>(graphics);
69 mEglImageExtensions = eglGraphics->GetImageExtensions();
71 DALI_ASSERT_DEBUG(mEglImageExtensions);
74 mPixmap = GetPixmapFromAny(nativeImageSource);
77 void NativeImageSourceWin::Initialize()
79 // if pixmap has been created outside of Windows Image we can return
82 // we don't own the pixmap
85 // find out the pixmap width / height and color depth
90 // get the pixel depth
91 int depth = GetPixelDepth(mColorDepth);
93 // set whether blending is required according to pixel format based on the depth
94 /* default pixel format is RGB888
95 If depth = 8, Pixel::A8;
96 If depth = 16, Pixel::RGB565;
97 If depth = 32, Pixel::RGBA8888 */
98 mBlendingRequired = (depth == 32 || depth == 8);
101 NativeImageSourceWin::~NativeImageSourceWin()
105 Any NativeImageSourceWin::GetNativeImageSource() const
110 bool NativeImageSourceWin::GetPixels(std::vector<uint8_t>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
112 DALI_ASSERT_DEBUG(sizeof(unsigned) == 4);
113 bool success = false;
120 void NativeImageSourceWin::SetSource(Any source)
122 mPixmap = GetPixmapFromAny(source);
126 // we don't own the pixmap
129 // find out the pixmap width / height and color depth
134 bool NativeImageSourceWin::IsColorDepthSupported(Dali::NativeImageSource::ColorDepth colorDepth)
139 bool NativeImageSourceWin::CreateResource()
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 unsigned int 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(uint16_t& width, uint16_t& height, uint16_t& stride)
267 bool NativeImageSourceWin::ReleaseBuffer()
272 void NativeImageSourceWin::SetResourceDestructionCallback(EventThreadCallback* callback)
274 mResourceDestructionCallback = std::unique_ptr<EventThreadCallback>(callback);
277 } // namespace Adaptor
279 } // namespace Internal