b0e78fbae53e976bc66ea35a7ad9dd7eab919f6e
[platform/core/uifw/dali-adaptor.git] / dali / internal / imaging / windows / native-image-source-impl-win.cpp
1 /*
2  * Copyright (c) 2017 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/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/devel-api/adaptor-framework/bitmap-saver.h>
30 #include <dali/integration-api/render-surface.h>
31
32 namespace Dali
33 {
34
35 namespace Internal
36 {
37
38 namespace Adaptor
39 {
40 using Dali::Integration::PixelBuffer;
41
42 NativeImageSourceWin* NativeImageSourceWin::New(unsigned int width, unsigned int height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
43 {
44   NativeImageSourceWin* image = new NativeImageSourceWin( width, height, depth, nativeImageSource );
45   DALI_ASSERT_DEBUG( image && "NativeImageSource allocation failed." );
46
47   // 2nd phase construction
48   if(image) //< Defensive in case we ever compile without exceptions.
49   {
50     image->Initialize();
51   }
52
53   return image;
54 }
55
56 NativeImageSourceWin::NativeImageSourceWin( unsigned int width, unsigned int height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
57 : mWidth( width ),
58   mHeight( height ),
59   mOwnPixmap( true ),
60   mPixmap( 0 ),
61   mBlendingRequired( false ),
62   mColorDepth( depth ),
63   mEglImageKHR( NULL ),
64   mEglImageExtensions( NULL )
65 {
66   DALI_ASSERT_ALWAYS( Adaptor::IsAvailable() );
67
68   GraphicsInterface* graphics = &( Adaptor::GetImplementation( Adaptor::Get() ).GetGraphicsInterface() );
69   auto eglGraphics = static_cast<EglGraphics *>(graphics);
70
71   mEglImageExtensions = eglGraphics->GetImageExtensions();
72
73   DALI_ASSERT_DEBUG( mEglImageExtensions );
74
75   // assign the pixmap
76   mPixmap = GetPixmapFromAny(nativeImageSource);
77 }
78
79 void NativeImageSourceWin::Initialize()
80 {
81   // if pixmap has been created outside of Windows Image we can return
82   if (mPixmap)
83   {
84     // we don't own the pixmap
85     mOwnPixmap = false;
86
87     // find out the pixmap width / height and color depth
88     GetPixmapDetails();
89     return;
90   }
91
92   // get the pixel depth
93   int depth = GetPixelDepth(mColorDepth);
94
95   // set whether blending is required according to pixel format based on the depth
96   /* default pixel format is RGB888
97      If depth = 8, Pixel::A8;
98      If depth = 16, Pixel::RGB565;
99      If depth = 32, Pixel::RGBA8888 */
100   mBlendingRequired = ( depth == 32 || depth == 8 );
101 }
102
103 NativeImageSourceWin::~NativeImageSourceWin()
104 {
105 }
106
107 Any NativeImageSourceWin::GetNativeImageSource() const
108 {
109   return Any(mPixmap);
110 }
111
112 bool NativeImageSourceWin::GetPixels(std::vector<uint8_t>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
113 {
114   DALI_ASSERT_DEBUG(sizeof(unsigned) == 4);
115   bool success = false;
116   width  = mWidth;
117   height = mHeight;
118
119   return success;
120 }
121
122 bool NativeImageSourceWin::EncodeToFile(const std::string& filename) const
123 {
124   std::vector< uint8_t > pixbuf;
125   uint32_t width(0), height(0);
126   Pixel::Format pixelFormat;
127
128   if(GetPixels(pixbuf, width, height, pixelFormat))
129   {
130     return Dali::EncodeToFile(&pixbuf[0], filename, pixelFormat, width, height);
131   }
132   return false;
133 }
134
135 void NativeImageSourceWin::SetSource( Any source )
136 {
137   mPixmap = GetPixmapFromAny( source );
138
139   if (mPixmap)
140   {
141     // we don't own the pixmap
142     mOwnPixmap = false;
143
144     // find out the pixmap width / height and color depth
145     GetPixmapDetails();
146   }
147 }
148
149 bool NativeImageSourceWin::IsColorDepthSupported( Dali::NativeImageSource::ColorDepth colorDepth )
150 {
151   return true;
152 }
153
154 bool NativeImageSourceWin::GlExtensionCreate()
155 {
156   // if the image existed previously delete it.
157   if (mEglImageKHR != NULL)
158   {
159     GlExtensionDestroy();
160   }
161
162   // casting from an unsigned int to a void *, which should then be cast back
163   // to an unsigned int in the driver.
164   EGLClientBuffer eglBuffer = reinterpret_cast< EGLClientBuffer > (mPixmap);
165
166   mEglImageKHR = mEglImageExtensions->CreateImageKHR( eglBuffer );
167
168   return mEglImageKHR != NULL;
169 }
170
171 void NativeImageSourceWin::GlExtensionDestroy()
172 {
173   mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
174
175   mEglImageKHR = NULL;
176 }
177
178 unsigned int NativeImageSourceWin::TargetTexture()
179 {
180   mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
181
182   return 0;
183 }
184
185 void NativeImageSourceWin::PrepareTexture()
186 {
187 }
188
189 int NativeImageSourceWin::GetPixelDepth(Dali::NativeImageSource::ColorDepth depth) const
190 {
191   switch (depth)
192   {
193     case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
194     {
195       return 32;
196     }
197     case Dali::NativeImageSource::COLOR_DEPTH_8:
198     {
199       return 8;
200     }
201     case Dali::NativeImageSource::COLOR_DEPTH_16:
202     {
203       return 16;
204     }
205     case Dali::NativeImageSource::COLOR_DEPTH_24:
206     {
207       return 24;
208     }
209     case Dali::NativeImageSource::COLOR_DEPTH_32:
210     {
211       return 32;
212     }
213     default:
214     {
215       DALI_ASSERT_DEBUG(0 && "unknown color enum");
216       return 0;
217     }
218   }
219 }
220
221 unsigned int NativeImageSourceWin::GetPixmapFromAny(Any pixmap) const
222 {
223   if (pixmap.Empty())
224   {
225     return 0;
226   }
227
228   // see if it is of type Windows pixmap
229   if (pixmap.GetType() == typeid ( unsigned int ))
230   {
231     // get the Windows pixmap type
232     unsigned int xpixmap = AnyCast<unsigned int>(pixmap);
233
234     // cast it to a Windows pixmap type
235     return static_cast<unsigned int>(xpixmap);
236   }
237   else
238   {
239     return AnyCast<unsigned int>(pixmap);
240   }
241 }
242
243 void NativeImageSourceWin::GetPixmapDetails()
244 {
245 }
246
247 } // namespace Adaptor
248
249 } // namespace internal
250
251 } // namespace Dali