2 * Copyright (c) 2020 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/internal/graphics/common/egl-image-extensions.h>
26 #include <dali/internal/graphics/gles/egl-graphics.h>
27 #include <dali/internal/adaptor/common/adaptor-impl.h>
28 #include <dali/internal/window-system/windows/platform-implement-win.h>
29 #include <dali/integration-api/adaptor-framework/render-surface-interface.h>
39 using Dali::Integration::PixelBuffer;
41 NativeImageSourceWin* NativeImageSourceWin::New(unsigned int width, unsigned int height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
43 NativeImageSourceWin* image = new NativeImageSourceWin( width, height, depth, nativeImageSource );
44 DALI_ASSERT_DEBUG( image && "NativeImageSource allocation failed." );
46 // 2nd phase construction
47 if(image) //< Defensive in case we ever compile without exceptions.
55 NativeImageSourceWin::NativeImageSourceWin( unsigned int width, unsigned int height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
60 mBlendingRequired( false ),
63 mEglImageExtensions( NULL )
65 DALI_ASSERT_ALWAYS( Adaptor::IsAvailable() );
67 GraphicsInterface* graphics = &( Adaptor::GetImplementation( Adaptor::Get() ).GetGraphicsInterface() );
68 auto eglGraphics = static_cast<EglGraphics *>(graphics);
70 mEglImageExtensions = eglGraphics->GetImageExtensions();
72 DALI_ASSERT_DEBUG( mEglImageExtensions );
75 mPixmap = GetPixmapFromAny(nativeImageSource);
78 void NativeImageSourceWin::Initialize()
80 // if pixmap has been created outside of Windows Image we can return
83 // we don't own the pixmap
86 // find out the pixmap width / height and color depth
91 // get the pixel depth
92 int depth = GetPixelDepth(mColorDepth);
94 // set whether blending is required according to pixel format based on the depth
95 /* default pixel format is RGB888
96 If depth = 8, Pixel::A8;
97 If depth = 16, Pixel::RGB565;
98 If depth = 32, Pixel::RGBA8888 */
99 mBlendingRequired = ( depth == 32 || depth == 8 );
102 NativeImageSourceWin::~NativeImageSourceWin()
106 Any NativeImageSourceWin::GetNativeImageSource() const
111 bool NativeImageSourceWin::GetPixels(std::vector<uint8_t>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
113 DALI_ASSERT_DEBUG(sizeof(unsigned) == 4);
114 bool success = false;
121 void NativeImageSourceWin::SetSource( Any source )
123 mPixmap = GetPixmapFromAny( source );
127 // we don't own the pixmap
130 // find out the pixmap width / height and color depth
135 bool NativeImageSourceWin::IsColorDepthSupported( Dali::NativeImageSource::ColorDepth colorDepth )
140 bool NativeImageSourceWin::GlExtensionCreate()
142 // if the image existed previously delete it.
143 if (mEglImageKHR != NULL)
145 GlExtensionDestroy();
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::GlExtensionDestroy()
159 mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
164 unsigned int NativeImageSourceWin::TargetTexture()
166 mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
171 void NativeImageSourceWin::PrepareTexture()
175 int NativeImageSourceWin::GetPixelDepth(Dali::NativeImageSource::ColorDepth depth) const
179 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
183 case Dali::NativeImageSource::COLOR_DEPTH_8:
187 case Dali::NativeImageSource::COLOR_DEPTH_16:
191 case Dali::NativeImageSource::COLOR_DEPTH_24:
195 case Dali::NativeImageSource::COLOR_DEPTH_32:
201 DALI_ASSERT_DEBUG(0 && "unknown color enum");
207 unsigned int NativeImageSourceWin::GetPixmapFromAny(Any pixmap) const
214 // see if it is of type Windows pixmap
215 if (pixmap.GetType() == typeid ( unsigned int ))
217 // get the Windows pixmap type
218 unsigned int xpixmap = AnyCast<unsigned int>(pixmap);
220 // cast it to a Windows pixmap type
221 return static_cast<unsigned int>(xpixmap);
225 return AnyCast<unsigned int>(pixmap);
229 void NativeImageSourceWin::GetPixmapDetails()
233 uint8_t* NativeImageSourceWin::AcquireBuffer( uint16_t& width, uint16_t& height, uint16_t& stride )
239 bool NativeImageSourceWin::ReleaseBuffer()
244 } // namespace Adaptor
246 } // namespace internal