Merge "Check NULL pointer dereference of the return value" into devel/master
[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.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
33 // Allow this to be encoded and saved:
34 #include <platform-abstractions/tizen/resource-loader/resource-loader.h>
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 NativeImageSource* NativeImageSource::New(unsigned int width, unsigned int height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource )
70 {
71   NativeImageSource* image = new NativeImageSource( 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 NativeImageSource::NativeImageSource( 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   mEglImageExtensions( NULL ),
92   mSetSource( false )
93 {
94   DALI_ASSERT_ALWAYS( Adaptor::IsAvailable() );
95   EglFactory& eglFactory = Adaptor::GetImplementation( Adaptor::Get() ).GetEGLFactory();
96   mEglImageExtensions = eglFactory.GetImageExtensions();
97   DALI_ASSERT_DEBUG( mEglImageExtensions );
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 NativeImageSource::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 NativeImageSource::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 NativeImageSource::~NativeImageSource()
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   }
203 }
204
205 Any NativeImageSource::GetNativeImageSource() const
206 {
207   return Any( mTbmSurface );
208 }
209
210 bool NativeImageSource::GetPixels(std::vector<unsigned char>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
211 {
212   if( mTbmSurface != NULL )
213   {
214     tbm_surface_info_s surface_info;
215
216     if( tbm_surface_map( mTbmSurface, TBM_SURF_OPTION_READ, &surface_info) != TBM_SURFACE_ERROR_NONE )
217     {
218       DALI_LOG_ERROR( "Fail to map tbm_surface\n" );
219
220       width = 0;
221       height = 0;
222
223       return false;
224     }
225
226     tbm_format format = surface_info.format;
227     uint32_t stride = surface_info.planes[0].stride;
228     unsigned char* ptr = surface_info.planes[0].ptr;
229
230     width = mWidth;
231     height = mHeight;
232     size_t lineSize;
233     size_t offset;
234     size_t cOffset;
235
236     switch( format )
237     {
238       case TBM_FORMAT_RGB888:
239       {
240         lineSize = width*3;
241         pixelFormat = Pixel::RGB888;
242         pixbuf.resize( lineSize*height );
243         unsigned char* bufptr = &pixbuf[0];
244
245         for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
246         {
247           for( unsigned int c = 0; c < width; ++c )
248           {
249             cOffset = c*3;
250             offset = cOffset + r*stride;
251             *(bufptr) = ptr[offset+2];
252             *(bufptr+cOffset+1) = ptr[offset+1];
253             *(bufptr+cOffset+2) = ptr[offset];
254           }
255         }
256         break;
257       }
258       case TBM_FORMAT_RGBA8888:
259       {
260         lineSize = width*4;
261         pixelFormat = Pixel::RGBA8888;
262         pixbuf.resize( lineSize*height );
263         unsigned char* bufptr = &pixbuf[0];
264
265         for( unsigned int r = 0; r < height; ++r, bufptr += lineSize )
266         {
267           for( unsigned int c = 0; c < width; ++c )
268           {
269             cOffset = c*4;
270             offset = cOffset + r*stride;
271             *(bufptr) = ptr[offset+3];
272             *(bufptr+cOffset+1) = ptr[offset+2];
273             *(bufptr+cOffset+2) = ptr[offset+1];
274             *(bufptr+cOffset+3) = ptr[offset];
275           }
276         }
277         break;
278       }
279       default:
280       {
281         DALI_LOG_WARNING( "Tbm surface has unsupported pixel format.\n" );
282
283         pixbuf.resize( 0 );
284         width = 0;
285         height = 0;
286
287         return false;
288       }
289     }
290
291     if( tbm_surface_unmap( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
292     {
293       DALI_LOG_ERROR( "Fail to unmap tbm_surface\n" );
294     }
295
296     return true;
297   }
298
299   DALI_LOG_WARNING( "TBM surface does not exist.\n" );
300
301   width = 0;
302   height = 0;
303
304   return false;
305 }
306
307 bool NativeImageSource::EncodeToFile(const std::string& filename) const
308 {
309   std::vector< unsigned char > pixbuf;
310   unsigned int width(0), height(0);
311   Pixel::Format pixelFormat;
312
313   if(GetPixels(pixbuf, width, height, pixelFormat))
314   {
315     return Dali::EncodeToFile(&pixbuf[0], filename, pixelFormat, width, height);
316   }
317   return false;
318 }
319
320 void NativeImageSource::SetSource( Any source )
321 {
322   if( mOwnTbmSurface )
323   {
324     if( mTbmSurface != NULL && tbm_surface_destroy( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
325     {
326       DALI_LOG_ERROR( "Failed to destroy tbm_surface\n" );
327     }
328
329     mTbmSurface = NULL;
330     mOwnTbmSurface = false;
331   }
332   else
333   {
334     if( mTbmSurface != NULL )
335     {
336       tbm_surface_internal_unref( mTbmSurface );
337       mTbmSurface = NULL;
338     }
339   }
340
341   mTbmSurface = GetSurfaceFromAny( source );
342
343   if( mTbmSurface != NULL )
344   {
345     mSetSource = true;
346     tbm_surface_internal_ref( mTbmSurface );
347     mBlendingRequired = CheckBlending( tbm_surface_get_format( mTbmSurface ) );
348     mWidth = tbm_surface_get_width( mTbmSurface );
349     mHeight = tbm_surface_get_height( mTbmSurface );
350   }
351 }
352
353 bool NativeImageSource::IsColorDepthSupported( Dali::NativeImageSource::ColorDepth colorDepth )
354 {
355   uint32_t* formats;
356   uint32_t formatNum;
357   tbm_format format = TBM_FORMAT_RGB888;
358
359   switch( colorDepth )
360   {
361     case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
362     {
363       format = TBM_FORMAT_RGBA8888;
364       break;
365     }
366     case Dali::NativeImageSource::COLOR_DEPTH_8:
367     {
368       format = TBM_FORMAT_C8;
369       break;
370     }
371     case Dali::NativeImageSource::COLOR_DEPTH_16:
372     {
373       format = TBM_FORMAT_RGB565;
374       break;
375     }
376     case Dali::NativeImageSource::COLOR_DEPTH_24:
377     {
378       format = TBM_FORMAT_RGB888;
379       break;
380     }
381     case Dali::NativeImageSource::COLOR_DEPTH_32:
382     {
383       format = TBM_FORMAT_RGBA8888;
384       break;
385     }
386   }
387
388   if( tbm_surface_query_formats( &formats, &formatNum ) )
389   {
390     for( unsigned int i = 0; i < formatNum; i++ )
391     {
392       if( formats[i] == format )
393       {
394         free( formats );
395         return true;
396       }
397     }
398   }
399
400   free( formats );
401   return false;
402 }
403
404 bool NativeImageSource::GlExtensionCreate()
405 {
406   // casting from an unsigned int to a void *, which should then be cast back
407   // to an unsigned int in the driver.
408   EGLClientBuffer eglBuffer = reinterpret_cast< EGLClientBuffer >(mTbmSurface);
409   if( !eglBuffer )
410   {
411     return false;
412   }
413
414   mEglImageKHR = mEglImageExtensions->CreateImageKHR( eglBuffer );
415
416   return mEglImageKHR != NULL;
417 }
418
419 void NativeImageSource::GlExtensionDestroy()
420 {
421   if( mEglImageKHR )
422   {
423     mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
424
425     mEglImageKHR = NULL;
426   }
427 }
428
429 unsigned int NativeImageSource::TargetTexture()
430 {
431   mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
432
433   return 0;
434 }
435
436 void NativeImageSource::PrepareTexture()
437 {
438   if( mSetSource )
439   {
440     void* eglImage = mEglImageKHR;
441
442     if( GlExtensionCreate() )
443     {
444       TargetTexture();
445     }
446
447     mEglImageExtensions->DestroyImageKHR( eglImage );
448
449     mSetSource = false;
450   }
451 }
452
453 int NativeImageSource::GetPixelDepth(Dali::NativeImageSource::ColorDepth depth) const
454 {
455   switch (depth)
456   {
457     case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
458     {
459       // ToDo: Get the default screen depth
460       return 32;
461     }
462     case Dali::NativeImageSource::COLOR_DEPTH_8:
463     {
464       return 8;
465     }
466     case Dali::NativeImageSource::COLOR_DEPTH_16:
467     {
468       return 16;
469     }
470     case Dali::NativeImageSource::COLOR_DEPTH_24:
471     {
472       return 24;
473     }
474     case Dali::NativeImageSource::COLOR_DEPTH_32:
475     {
476       return 32;
477     }
478     default:
479     {
480       DALI_ASSERT_DEBUG(0 && "unknown color enum");
481       return 0;
482     }
483   }
484 }
485
486 const char* NativeImageSource::GetCustomFragmentPreFix()
487 {
488   return FRAGMENT_PREFIX;
489 }
490
491 const char* NativeImageSource::GetCustomSamplerTypename()
492 {
493   return SAMPLER_TYPE;
494 }
495
496 int NativeImageSource::GetEglImageTextureTarget()
497 {
498   return GL_TEXTURE_EXTERNAL_OES;
499 }
500
501 bool NativeImageSource::CheckBlending( tbm_format format )
502 {
503   if( mTbmFormat != format )
504   {
505     for(int i = 0; i < NUM_FORMATS_BLENDING_REQUIRED; ++i)
506     {
507       if( format == FORMATS_BLENDING_REQUIRED[i] )
508       {
509         mBlendingRequired = true;
510         break;
511       }
512     }
513     mTbmFormat = format;
514   }
515
516   return mBlendingRequired;
517 }
518
519 } // namespace Adaptor
520
521 } // namespace internal
522
523 } // namespace Dali