33ff187720d5f321e61a464f9f8b01f24e8010b7
[platform/core/uifw/dali-adaptor.git] / adaptors / tizen / native-image-source-impl-tizen.cpp
1 /*
2  * Copyright (c) 2014 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 <native-image-source-impl-tizen.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <dali/integration-api/gl-defines.h>
24 #include <cstring>
25 #include <tbm_surface_internal.h>
26
27 // INTERNAL INCLUDES
28 #include <gl/egl-image-extensions.h>
29 #include <gl/egl-factory.h>
30 #include <adaptor-impl.h>
31 #include <render-surface.h>
32 #include <trigger-event-factory.h>
33
34 // Allow this to be encoded and saved:
35 #include <bitmap-saver.h>
36
37 namespace Dali
38 {
39
40 namespace Internal
41 {
42
43 namespace Adaptor
44 {
45
46 namespace
47 {
48 const char* FRAGMENT_PREFIX = "#extension GL_OES_EGL_image_external:require\n";
49 const char* SAMPLER_TYPE = "samplerExternalOES";
50
51 tbm_format FORMATS_BLENDING_REQUIRED[] = {
52   TBM_FORMAT_ARGB4444, TBM_FORMAT_ABGR4444,
53   TBM_FORMAT_RGBA4444, TBM_FORMAT_BGRA4444,
54   TBM_FORMAT_RGBX5551, TBM_FORMAT_BGRX5551,
55   TBM_FORMAT_ARGB1555, TBM_FORMAT_ABGR1555,
56   TBM_FORMAT_RGBA5551, TBM_FORMAT_BGRA5551,
57   TBM_FORMAT_ARGB8888, TBM_FORMAT_ABGR8888,
58   TBM_FORMAT_RGBA8888, TBM_FORMAT_BGRA8888,
59   TBM_FORMAT_ARGB2101010, TBM_FORMAT_ABGR2101010,
60   TBM_FORMAT_RGBA1010102, TBM_FORMAT_BGRA1010102
61 };
62
63 const int NUM_FORMATS_BLENDING_REQUIRED = 18;
64
65 }
66
67 using Dali::Integration::PixelBuffer;
68
69 NativeImageSourceTizen* NativeImageSourceTizen::New(unsigned int width, unsigned int height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
70 {
71   NativeImageSourceTizen* image = new NativeImageSourceTizen( width, height, depth, nativeImageSource );
72   DALI_ASSERT_DEBUG( image && "NativeImageSource allocation failed." );
73
74   if( image )
75   {
76     image->Initialize();
77   }
78
79   return image;
80 }
81
82 NativeImageSourceTizen::NativeImageSourceTizen( unsigned int width, unsigned int height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
83 : mWidth( width ),
84   mHeight( height ),
85   mOwnTbmSurface( false ),
86   mTbmSurface( NULL ),
87   mTbmFormat( 0 ),
88   mBlendingRequired( false ),
89   mColorDepth( depth ),
90   mEglImageKHR( NULL ),
91   mEglFactory( NULL ),
92   mEglImageExtensions( NULL ),
93   mSetSource( false ),
94   mNotification( NULL )
95 {
96   DALI_ASSERT_ALWAYS( Adaptor::IsAvailable() );
97   mEglFactory = &( Adaptor::GetImplementation( Adaptor::Get() ).GetEGLFactory() );
98
99   mTbmSurface = GetSurfaceFromAny( nativeImageSource );
100
101   if( mTbmSurface != NULL )
102   {
103     tbm_surface_internal_ref( mTbmSurface );
104     mBlendingRequired = CheckBlending( tbm_surface_get_format( mTbmSurface ) );
105     mWidth = tbm_surface_get_width( mTbmSurface );
106     mHeight = tbm_surface_get_height( mTbmSurface );
107   }
108 }
109
110 void NativeImageSourceTizen::Initialize()
111 {
112   if( mTbmSurface != NULL || mWidth == 0 || mHeight == 0 )
113   {
114     return;
115   }
116
117   tbm_format format = TBM_FORMAT_RGB888;
118   int depth = 0;
119
120   switch( mColorDepth )
121   {
122     case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
123     {
124       format = TBM_FORMAT_RGBA8888;
125       depth = 32;
126       break;
127     }
128     case Dali::NativeImageSource::COLOR_DEPTH_8:
129     {
130       format = TBM_FORMAT_C8;
131       depth = 8;
132       break;
133     }
134     case Dali::NativeImageSource::COLOR_DEPTH_16:
135     {
136       format = TBM_FORMAT_RGB565;
137       depth = 16;
138       break;
139     }
140     case Dali::NativeImageSource::COLOR_DEPTH_24:
141     {
142       format = TBM_FORMAT_RGB888;
143       depth = 24;
144       break;
145     }
146     case Dali::NativeImageSource::COLOR_DEPTH_32:
147     {
148       format = TBM_FORMAT_RGBA8888;
149       depth = 32;
150       break;
151     }
152     default:
153     {
154       DALI_LOG_WARNING( "Wrong color depth.\n" );
155       return;
156     }
157   }
158
159   // set whether blending is required according to pixel format based on the depth
160   /* default pixel format is RGB888
161      If depth = 8, Pixel::A8;
162      If depth = 16, Pixel::RGB565;
163      If depth = 32, Pixel::RGBA8888 */
164   mBlendingRequired = ( depth == 32 || depth == 8 );
165
166   mTbmSurface = tbm_surface_create( mWidth, mHeight, format );
167   mOwnTbmSurface = true;
168 }
169
170 tbm_surface_h NativeImageSourceTizen::GetSurfaceFromAny( Any source ) const
171 {
172   if( source.Empty() )
173   {
174     return NULL;
175   }
176
177   if( source.GetType() == typeid( tbm_surface_h ) )
178   {
179     return AnyCast< tbm_surface_h >( source );
180   }
181   else
182   {
183     return NULL;
184   }
185 }
186
187 NativeImageSourceTizen::~NativeImageSourceTizen()
188 {
189   if( mOwnTbmSurface )
190   {
191     if( mTbmSurface != NULL && tbm_surface_destroy( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
192     {
193       DALI_LOG_ERROR( "Failed to destroy tbm_surface\n" );
194     }
195   }
196   else
197   {
198     if( mTbmSurface != NULL )
199     {
200       tbm_surface_internal_unref( mTbmSurface );
201
202       if (mNotification != NULL) {
203           TriggerEventInterface* triggerEvent = static_cast<TriggerEventInterface* >(mNotification);
204           triggerEvent->Trigger();
205       }
206     }
207   }
208 }
209
210 Any NativeImageSourceTizen::GetNativeImageSource() const
211 {
212   return Any( mTbmSurface );
213 }
214
215
216 void NativeImageSourceTizen::SetDestructorNotification(void* notification)
217 {
218   mNotification = notification;
219 }
220
221 bool NativeImageSourceTizen::GetPixels(std::vector<unsigned char>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
222 {
223   if( mTbmSurface != NULL )
224   {
225     tbm_surface_info_s surface_info;
226
227     if( tbm_surface_map( mTbmSurface, TBM_SURF_OPTION_READ, &surface_info) != TBM_SURFACE_ERROR_NONE )
228     {
229       DALI_LOG_ERROR( "Fail to map tbm_surface\n" );
230
231       width = 0;
232       height = 0;
233
234       return false;
235     }
236
237     tbm_format format = surface_info.format;
238     uint32_t stride = surface_info.planes[0].stride;
239     unsigned char* ptr = surface_info.planes[0].ptr;
240
241     width = mWidth;
242     height = mHeight;
243     size_t lineSize;
244     size_t offset;
245     size_t cOffset;
246
247     switch( format )
248     {
249       case TBM_FORMAT_RGB888:
250       {
251         lineSize = width*3;
252         pixelFormat = Pixel::RGB888;
253         pixbuf.resize( lineSize*height );
254         unsigned char* bufptr = &pixbuf[0];
255
256         for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
257         {
258           for( unsigned int c = 0; c < width; ++c )
259           {
260             cOffset = c*3;
261             offset = cOffset + r*stride;
262             *(bufptr+cOffset) = ptr[offset+2];
263             *(bufptr+cOffset+1) = ptr[offset+1];
264             *(bufptr+cOffset+2) = ptr[offset];
265           }
266         }
267         break;
268       }
269       case TBM_FORMAT_RGBA8888:
270       {
271         lineSize = width*4;
272         pixelFormat = Pixel::RGBA8888;
273         pixbuf.resize( lineSize*height );
274         unsigned char* bufptr = &pixbuf[0];
275
276         for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
277         {
278           for( unsigned int c = 0; c < width; ++c )
279           {
280             cOffset = c*4;
281             offset = cOffset + r*stride;
282             *(bufptr+cOffset) = ptr[offset+3];
283             *(bufptr+cOffset+1) = ptr[offset+2];
284             *(bufptr+cOffset+2) = ptr[offset+1];
285             *(bufptr+cOffset+3) = ptr[offset];
286           }
287         }
288         break;
289       }
290       default:
291       {
292         DALI_ASSERT_ALWAYS( 0 && "Tbm surface has unsupported pixel format.\n" );
293
294         return false;
295       }
296     }
297
298     if( tbm_surface_unmap( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
299     {
300       DALI_LOG_ERROR( "Fail to unmap tbm_surface\n" );
301     }
302
303     return true;
304   }
305
306   DALI_LOG_WARNING( "TBM surface does not exist.\n" );
307
308   width = 0;
309   height = 0;
310
311   return false;
312 }
313
314 bool NativeImageSourceTizen::EncodeToFile(const std::string& filename) const
315 {
316   std::vector< unsigned char > pixbuf;
317   unsigned int width(0), height(0);
318   Pixel::Format pixelFormat;
319
320   if(GetPixels(pixbuf, width, height, pixelFormat))
321   {
322     return Dali::EncodeToFile(&pixbuf[0], filename, pixelFormat, width, height);
323   }
324   return false;
325 }
326
327 void NativeImageSourceTizen::SetSource( Any source )
328 {
329   if( mOwnTbmSurface )
330   {
331     if( mTbmSurface != NULL && tbm_surface_destroy( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
332     {
333       DALI_LOG_ERROR( "Failed to destroy tbm_surface\n" );
334     }
335
336     mTbmSurface = NULL;
337     mOwnTbmSurface = false;
338   }
339   else
340   {
341     if( mTbmSurface != NULL )
342     {
343       tbm_surface_internal_unref( mTbmSurface );
344       mTbmSurface = NULL;
345     }
346   }
347
348   mTbmSurface = GetSurfaceFromAny( source );
349
350   if( mTbmSurface != NULL )
351   {
352     mSetSource = true;
353     tbm_surface_internal_ref( mTbmSurface );
354     mBlendingRequired = CheckBlending( tbm_surface_get_format( mTbmSurface ) );
355     mWidth = tbm_surface_get_width( mTbmSurface );
356     mHeight = tbm_surface_get_height( mTbmSurface );
357   }
358 }
359
360 bool NativeImageSourceTizen::IsColorDepthSupported( Dali::NativeImageSource::ColorDepth colorDepth )
361 {
362   uint32_t* formats;
363   uint32_t formatNum;
364   tbm_format format = TBM_FORMAT_RGB888;
365
366   switch( colorDepth )
367   {
368     case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
369     {
370       format = TBM_FORMAT_RGBA8888;
371       break;
372     }
373     case Dali::NativeImageSource::COLOR_DEPTH_8:
374     {
375       format = TBM_FORMAT_C8;
376       break;
377     }
378     case Dali::NativeImageSource::COLOR_DEPTH_16:
379     {
380       format = TBM_FORMAT_RGB565;
381       break;
382     }
383     case Dali::NativeImageSource::COLOR_DEPTH_24:
384     {
385       format = TBM_FORMAT_RGB888;
386       break;
387     }
388     case Dali::NativeImageSource::COLOR_DEPTH_32:
389     {
390       format = TBM_FORMAT_RGBA8888;
391       break;
392     }
393   }
394
395   if( tbm_surface_query_formats( &formats, &formatNum ) )
396   {
397     for( unsigned int i = 0; i < formatNum; i++ )
398     {
399       if( formats[i] == format )
400       {
401         free( formats );
402         return true;
403       }
404     }
405   }
406
407   free( formats );
408   return false;
409 }
410
411 bool NativeImageSourceTizen::GlExtensionCreate()
412 {
413   // casting from an unsigned int to a void *, which should then be cast back
414   // to an unsigned int in the driver.
415   EGLClientBuffer eglBuffer = reinterpret_cast< EGLClientBuffer >(mTbmSurface);
416   if( !eglBuffer )
417   {
418     return false;
419   }
420
421   mEglImageExtensions = mEglFactory->GetImageExtensions();
422   DALI_ASSERT_DEBUG( mEglImageExtensions );
423
424   mEglImageKHR = mEglImageExtensions->CreateImageKHR( eglBuffer );
425
426   return mEglImageKHR != NULL;
427 }
428
429 void NativeImageSourceTizen::GlExtensionDestroy()
430 {
431   if( mEglImageKHR )
432   {
433     mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
434
435     mEglImageKHR = NULL;
436   }
437 }
438
439 unsigned int NativeImageSourceTizen::TargetTexture()
440 {
441   mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
442
443   return 0;
444 }
445
446 void NativeImageSourceTizen::PrepareTexture()
447 {
448   if( mSetSource )
449   {
450     void* eglImage = mEglImageKHR;
451
452     if( GlExtensionCreate() )
453     {
454       TargetTexture();
455     }
456
457     mEglImageExtensions->DestroyImageKHR( eglImage );
458
459     mSetSource = false;
460   }
461 }
462
463 const char* NativeImageSourceTizen::GetCustomFragmentPreFix()
464 {
465   return FRAGMENT_PREFIX;
466 }
467
468 const char* NativeImageSourceTizen::GetCustomSamplerTypename()
469 {
470   return SAMPLER_TYPE;
471 }
472
473 int NativeImageSourceTizen::GetEglImageTextureTarget()
474 {
475   return GL_TEXTURE_EXTERNAL_OES;
476 }
477
478 bool NativeImageSourceTizen::CheckBlending( tbm_format format )
479 {
480   if( mTbmFormat != format )
481   {
482     for(int i = 0; i < NUM_FORMATS_BLENDING_REQUIRED; ++i)
483     {
484       if( format == FORMATS_BLENDING_REQUIRED[i] )
485       {
486         mBlendingRequired = true;
487         break;
488       }
489     }
490     mTbmFormat = format;
491   }
492
493   return mBlendingRequired;
494 }
495
496 } // namespace Adaptor
497
498 } // namespace internal
499
500 } // namespace Dali