2 * Copyright (c) 2018 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/tizen/native-image-source-queue-impl-tizen.h>
22 #include <dali/integration-api/debug.h>
23 #include <dali/integration-api/gl-defines.h>
24 #include <tbm_surface_internal.h>
27 #include <dali/internal/graphics/common/egl-image-extensions.h>
28 #include <dali/internal/graphics/gles20/egl-graphics.h>
29 #include <dali/internal/adaptor/common/adaptor-impl.h>
42 #define TBM_SURFACE_QUEUE_SIZE 3
44 const char* FRAGMENT_PREFIX = "#extension GL_OES_EGL_image_external:require\n";
45 const char* SAMPLER_TYPE = "samplerExternalOES";
47 int FORMATS_BLENDING_REQUIRED[] = {
48 TBM_FORMAT_ARGB4444, TBM_FORMAT_ABGR4444,
49 TBM_FORMAT_RGBA4444, TBM_FORMAT_BGRA4444,
50 TBM_FORMAT_RGBX5551, TBM_FORMAT_BGRX5551,
51 TBM_FORMAT_ARGB1555, TBM_FORMAT_ABGR1555,
52 TBM_FORMAT_RGBA5551, TBM_FORMAT_BGRA5551,
53 TBM_FORMAT_ARGB8888, TBM_FORMAT_ABGR8888,
54 TBM_FORMAT_RGBA8888, TBM_FORMAT_BGRA8888,
55 TBM_FORMAT_ARGB2101010, TBM_FORMAT_ABGR2101010,
56 TBM_FORMAT_RGBA1010102, TBM_FORMAT_BGRA1010102
59 const int NUM_FORMATS_BLENDING_REQUIRED = 18;
63 NativeImageSourceQueueTizen* NativeImageSourceQueueTizen::New( unsigned int width, unsigned int height, Dali::NativeImageSourceQueue::ColorDepth depth, Any nativeImageSourceQueue )
65 NativeImageSourceQueueTizen* image = new NativeImageSourceQueueTizen( width, height, depth, nativeImageSourceQueue );
66 DALI_ASSERT_DEBUG( image && "NativeImageSourceQueueTizen allocation failed." );
70 image->Initialize( depth );
76 NativeImageSourceQueueTizen::NativeImageSourceQueueTizen( unsigned int width, unsigned int height, Dali::NativeImageSourceQueue::ColorDepth depth, Any nativeImageSourceQueue )
80 mConsumeSurface( NULL ),
82 mEglImageExtensions( NULL ),
83 mOwnTbmQueue( false ),
84 mBlendingRequired( false )
86 DALI_ASSERT_ALWAYS( Adaptor::IsAvailable() );
88 GraphicsInterface* graphics = &( Adaptor::GetImplementation( Adaptor::Get() ).GetGraphicsInterface() );
89 auto eglGraphics = static_cast<EglGraphics *>(graphics);
91 mEglImageExtensions = eglGraphics->GetImageExtensions();
93 DALI_ASSERT_DEBUG( mEglImageExtensions );
95 mTbmQueue = GetSurfaceFromAny( nativeImageSourceQueue );
97 if( mTbmQueue != NULL )
99 mBlendingRequired = CheckBlending( tbm_surface_queue_get_format( mTbmQueue ) );
100 mWidth = tbm_surface_queue_get_width( mTbmQueue );
101 mHeight = tbm_surface_queue_get_height( mTbmQueue );
105 NativeImageSourceQueueTizen::~NativeImageSourceQueueTizen()
113 void NativeImageSourceQueueTizen::Initialize( Dali::NativeImageSourceQueue::ColorDepth depth )
115 if( mTbmQueue != NULL || mWidth == 0 || mHeight == 0 )
120 int format = TBM_FORMAT_ARGB8888;
124 case Dali::NativeImageSourceQueue::COLOR_DEPTH_DEFAULT:
125 case Dali::NativeImageSourceQueue::COLOR_DEPTH_32:
127 format = TBM_FORMAT_ARGB8888;
128 mBlendingRequired = true;
131 case Dali::NativeImageSourceQueue::COLOR_DEPTH_24:
133 format = TBM_FORMAT_RGB888;
134 mBlendingRequired = false;
139 DALI_LOG_WARNING( "Wrong color depth.\n" );
144 mTbmQueue = tbm_surface_queue_create( TBM_SURFACE_QUEUE_SIZE, mWidth, mHeight, format, 0 );
149 tbm_surface_queue_h NativeImageSourceQueueTizen::GetSurfaceFromAny( Any source ) const
156 if( source.GetType() == typeid( tbm_surface_queue_h ) )
158 return AnyCast< tbm_surface_queue_h >( source );
166 Any NativeImageSourceQueueTizen::GetNativeImageSourceQueue() const
168 return Any( mTbmQueue );
171 void NativeImageSourceQueueTizen::SetSource( Any source )
178 mTbmQueue = GetSurfaceFromAny( source );
180 if( mTbmQueue != NULL )
182 mBlendingRequired = CheckBlending( tbm_surface_queue_get_format( mTbmQueue ) );
183 mWidth = tbm_surface_queue_get_width( mTbmQueue );
184 mHeight = tbm_surface_queue_get_height( mTbmQueue );
188 bool NativeImageSourceQueueTizen::GlExtensionCreate()
193 void NativeImageSourceQueueTizen::GlExtensionDestroy()
195 for( auto&& iter : mEglImages )
197 mEglImageExtensions->DestroyImageKHR( iter.second );
199 tbm_surface_internal_unref( iter.first );
204 unsigned int NativeImageSourceQueueTizen::TargetTexture()
209 void NativeImageSourceQueueTizen::PrepareTexture()
211 tbm_surface_h oldSurface = mConsumeSurface;
213 bool needToWait = ( mConsumeSurface == NULL ) ? true : false;
215 if( tbm_surface_queue_can_acquire( mTbmQueue, needToWait ) )
217 if( tbm_surface_queue_acquire( mTbmQueue, &mConsumeSurface ) != TBM_SURFACE_QUEUE_ERROR_NONE )
219 DALI_LOG_ERROR( "Failed to aquire a tbm_surface\n" );
224 if( oldSurface && oldSurface != mConsumeSurface )
226 if( tbm_surface_internal_is_valid( oldSurface ) )
228 tbm_surface_queue_release( mTbmQueue, oldSurface );
232 if( mConsumeSurface )
234 bool existing = false;
235 for( auto&& iter : mEglImages )
237 if( iter.first == mConsumeSurface )
239 // Find the surface in the existing list
241 mEglImageExtensions->TargetTextureKHR( iter.second );
249 tbm_surface_internal_ref( mConsumeSurface );
251 void* eglImageKHR = mEglImageExtensions->CreateImageKHR( reinterpret_cast< EGLClientBuffer >( mConsumeSurface ) );
252 mEglImageExtensions->TargetTextureKHR( eglImageKHR );
254 mEglImages.push_back( EglImagePair( mConsumeSurface, eglImageKHR) );
259 const char* NativeImageSourceQueueTizen::GetCustomFragmentPreFix()
261 return FRAGMENT_PREFIX;
264 const char* NativeImageSourceQueueTizen::GetCustomSamplerTypename()
269 int NativeImageSourceQueueTizen::GetEglImageTextureTarget()
271 return GL_TEXTURE_EXTERNAL_OES;
274 void NativeImageSourceQueueTizen::DestroyQueue()
276 if( mConsumeSurface )
278 tbm_surface_internal_unref( mConsumeSurface );
280 if( tbm_surface_internal_is_valid( mConsumeSurface ) )
282 tbm_surface_queue_release( mTbmQueue, mConsumeSurface );
286 if( mTbmQueue != NULL )
288 tbm_surface_queue_destroy( mTbmQueue );
292 mOwnTbmQueue = false;
295 bool NativeImageSourceQueueTizen::CheckBlending( int format )
297 for( int i = 0; i < NUM_FORMATS_BLENDING_REQUIRED; ++i )
299 if( format == FORMATS_BLENDING_REQUIRED[i] )
308 } // namespace Adaptor
310 } // namespace internal