f1997a7ee47ab9bf84e3abd734772d24ad6cb0c5
[platform/core/uifw/dali-adaptor.git] / dali / internal / imaging / windows / native-image-source-impl-win.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/imaging/windows/native-image-source-impl-win.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23
24 // INTERNAL INCLUDES
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>
30
31 namespace Dali
32 {
33 namespace Internal
34 {
35 namespace Adaptor
36 {
37 using Dali::Integration::PixelBuffer;
38
39 NativeImageSourceWin* NativeImageSourceWin::New(unsigned int width, unsigned int height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource)
40 {
41   NativeImageSourceWin* image = new NativeImageSourceWin(width, height, depth, nativeImageSource);
42   DALI_ASSERT_DEBUG(image && "NativeImageSource allocation failed.");
43
44   // 2nd phase construction
45   if(image) //< Defensive in case we ever compile without exceptions.
46   {
47     image->Initialize();
48   }
49
50   return image;
51 }
52
53 NativeImageSourceWin::NativeImageSourceWin(unsigned int width, unsigned int height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource)
54 : mWidth(width),
55   mHeight(height),
56   mOwnPixmap(true),
57   mPixmap(0),
58   mBlendingRequired(false),
59   mColorDepth(depth),
60   mEglImageKHR(NULL),
61   mEglImageExtensions(NULL)
62 {
63   DALI_ASSERT_ALWAYS(Adaptor::IsAvailable());
64
65   GraphicsInterface* graphics    = &(Adaptor::GetImplementation(Adaptor::Get()).GetGraphicsInterface());
66   auto               eglGraphics = static_cast<EglGraphics*>(graphics);
67
68   mEglImageExtensions = eglGraphics->GetImageExtensions();
69
70   DALI_ASSERT_DEBUG(mEglImageExtensions);
71
72   // assign the pixmap
73   mPixmap = GetPixmapFromAny(nativeImageSource);
74 }
75
76 void NativeImageSourceWin::Initialize()
77 {
78   // if pixmap has been created outside of Windows Image we can return
79   if(mPixmap)
80   {
81     // we don't own the pixmap
82     mOwnPixmap = false;
83
84     // find out the pixmap width / height and color depth
85     GetPixmapDetails();
86     return;
87   }
88
89   // get the pixel depth
90   int depth = GetPixelDepth(mColorDepth);
91
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);
98 }
99
100 NativeImageSourceWin::~NativeImageSourceWin()
101 {
102 }
103
104 Any NativeImageSourceWin::GetNativeImageSource() const
105 {
106   return Any(mPixmap);
107 }
108
109 bool NativeImageSourceWin::GetPixels(std::vector<uint8_t>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
110 {
111   DALI_ASSERT_DEBUG(sizeof(unsigned) == 4);
112   bool success = false;
113   width        = mWidth;
114   height       = mHeight;
115
116   return success;
117 }
118
119 void NativeImageSourceWin::SetSource(Any source)
120 {
121   mPixmap = GetPixmapFromAny(source);
122
123   if(mPixmap)
124   {
125     // we don't own the pixmap
126     mOwnPixmap = false;
127
128     // find out the pixmap width / height and color depth
129     GetPixmapDetails();
130   }
131 }
132
133 bool NativeImageSourceWin::IsColorDepthSupported(Dali::NativeImageSource::ColorDepth colorDepth)
134 {
135   return true;
136 }
137
138 bool NativeImageSourceWin::CreateResource()
139 {
140   // if the image existed previously delete it.
141   if(mEglImageKHR != NULL)
142   {
143     DestroyResource();
144   }
145
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);
149
150   mEglImageKHR = mEglImageExtensions->CreateImageKHR(eglBuffer);
151
152   return mEglImageKHR != NULL;
153 }
154
155 void NativeImageSourceWin::DestroyResource()
156 {
157   mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
158
159   mEglImageKHR = NULL;
160 }
161
162 unsigned int NativeImageSourceWin::TargetTexture()
163 {
164   mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
165
166   return 0;
167 }
168
169 void NativeImageSourceWin::PrepareTexture()
170 {
171 }
172
173 int NativeImageSourceWin::GetPixelDepth(Dali::NativeImageSource::ColorDepth depth) const
174 {
175   switch(depth)
176   {
177     case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
178     {
179       return 32;
180     }
181     case Dali::NativeImageSource::COLOR_DEPTH_8:
182     {
183       return 8;
184     }
185     case Dali::NativeImageSource::COLOR_DEPTH_16:
186     {
187       return 16;
188     }
189     case Dali::NativeImageSource::COLOR_DEPTH_24:
190     {
191       return 24;
192     }
193     case Dali::NativeImageSource::COLOR_DEPTH_32:
194     {
195       return 32;
196     }
197     default:
198     {
199       DALI_ASSERT_DEBUG(0 && "unknown color enum");
200       return 0;
201     }
202   }
203 }
204
205 unsigned int NativeImageSourceWin::GetPixmapFromAny(Any pixmap) const
206 {
207   if(pixmap.Empty())
208   {
209     return 0;
210   }
211
212   // see if it is of type Windows pixmap
213   if(pixmap.GetType() == typeid(unsigned int))
214   {
215     // get the Windows pixmap type
216     unsigned int xpixmap = AnyCast<unsigned int>(pixmap);
217
218     // cast it to a Windows pixmap type
219     return static_cast<unsigned int>(xpixmap);
220   }
221   else
222   {
223     return AnyCast<unsigned int>(pixmap);
224   }
225 }
226
227 void NativeImageSourceWin::GetPixmapDetails()
228 {
229 }
230
231 bool NativeImageSourceWin::ApplyNativeFragmentShader(std::string& shader)
232 {
233   return false;
234 }
235
236 const char* NativeImageSourceWin::GetCustomSamplerTypename() const
237 {
238   return nullptr;
239 }
240
241 int NativeImageSourceWin::GetTextureTarget() const
242 {
243   return GL_TEXTURE_2D;
244 }
245
246 Any NativeImageSourceWin::GetNativeImageHandle() const
247 {
248   return mPixmap;
249 }
250
251 bool NativeImageSourceWin::SourceChanged() const
252 {
253   return false;
254 }
255
256 uint8_t* NativeImageSourceWin::AcquireBuffer(uint16_t& width, uint16_t& height, uint16_t& stride)
257 {
258   return NULL;
259 }
260
261 bool NativeImageSourceWin::ReleaseBuffer()
262 {
263   return false;
264 }
265
266 } // namespace Adaptor
267
268 } // namespace Internal
269
270 } // namespace Dali