2 * Copyright (c) 2014 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 "mobile-native-buffer-render-surface.h"
22 #include <dali/integration-api/debug.h>
23 #include <dali/integration-api/gl-abstraction.h>
24 #include <native-buffer-pool.h>
27 #include <gl/egl-implementation.h>
28 #include <trigger-event.h>
39 #if defined(DEBUG_ENABLED)
40 extern Debug::Filter* gRenderSurfaceLogFilter;
46 NativeBufferRenderSurface::NativeBufferRenderSurface( native_buffer_provider* provider,
47 native_buffer_pool* pool,
48 unsigned int maxBufferCount,
49 Dali::PositionSize positionSize,
52 const std::string& name,
54 : RenderSurface( Dali::RenderSurface::NATIVE_BUFFER, positionSize, surface, display, "native_buffer", isTransparent ),
55 mProvider( provider ),
57 mMaxBufferCount( maxBufferCount ),
60 DALI_ASSERT_ALWAYS(maxBufferCount > 0);
64 NativeBufferRenderSurface::~NativeBufferRenderSurface()
66 DALI_LOG_WARNING("%d native buffer will be destroyed\n", mBuffers.size());
69 NativeBufferContainer::iterator bufferIter;
70 for (bufferIter = mBuffers.begin(); bufferIter != mBuffers.end(); ++bufferIter)
72 native_buffer_destroy((*bufferIter));
77 Ecore_X_Drawable NativeBufferRenderSurface::GetDrawable()
79 return (Ecore_X_Drawable)0;
82 Dali::RenderSurface::SurfaceType NativeBufferRenderSurface::GetType()
84 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
85 return Dali::RenderSurface::NATIVE_BUFFER;
88 Any NativeBufferRenderSurface::GetSurface()
90 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
94 void NativeBufferRenderSurface::InitializeEgl( EglInterface& egl )
96 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
98 EglImplementation& eglImpl = static_cast<EglImplementation&>( egl );
99 eglImpl.InitializeGles( reinterpret_cast< EGLNativeDisplayType >( mMainDisplay ), false /* external surface */);
101 eglImpl.ChooseConfig(false, mColorDepth);
104 native_buffer* NativeBufferRenderSurface::CreateNativeBuffer()
106 native_buffer* buffer;
108 buffer = native_buffer_create(mProvider, mPosition.width, mPosition.height,
109 mColorDepth == COLOR_DEPTH_32? mPosition.width * 4 : mPosition.width * 3, /* stride will be deprecated */
110 mColorDepth == COLOR_DEPTH_32? NATIVE_BUFFER_FORMAT_BGRA_8888 : NATIVE_BUFFER_FORMAT_RGB_888,
111 NATIVE_BUFFER_USAGE_3D_RENDER);
115 // insert buffer to list
116 mBuffers.push_back(buffer);
122 void NativeBufferRenderSurface::CreateEglSurface( EglInterface& egl )
124 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
126 EglImplementation& eglImpl = static_cast<EglImplementation&>( egl );
129 native_buffer_pool_add_buffer(mPool, CreateNativeBuffer());
131 DALI_ASSERT_ALWAYS( native_buffer_pool_acquire_surface( mPool, eglImpl.GetDisplay(), eglImpl.GetContext() ) == STATUS_SUCCESS );
135 void NativeBufferRenderSurface::DestroyEglSurface( EglInterface& egl )
137 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
139 // Remove buffers from pool
140 native_buffer_pool_reset(mPool);
143 bool NativeBufferRenderSurface::ReplaceEGLSurface( EglInterface& egl )
145 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
150 bool NativeBufferRenderSurface::PreRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction )
152 EglImplementation& eglImpl = static_cast<EglImplementation&>( egl );
160 // Attempt to acquire a surface for rendering
161 while( !mIsStopped && native_buffer_pool_get_input_buffer_count(mPool) < 1 )
163 if(mBuffers.size() <= mMaxBufferCount)
166 native_buffer_pool_add_buffer(mPool, CreateNativeBuffer());
170 usleep( 5 * 1000 ); // polling per 5 msec
174 if( !mIsStopped && native_buffer_pool_acquire_surface( mPool, eglImpl.GetDisplay(), eglImpl.GetContext() ) != STATUS_SUCCESS )
176 DALI_LOG_ERROR("Failed to acquire native buffer surface (# queue : %d)\n", native_buffer_pool_get_input_buffer_count(mPool));
179 return !mIsStopped; // fail if it is stopped
182 void NativeBufferRenderSurface::PostRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, unsigned int timeDelta, SyncMode syncMode )
184 glAbstraction.Flush();
186 // release the surface to allow consumer usage
187 if(native_buffer_pool_release_surface( mPool ) != STATUS_SUCCESS)
189 DALI_LOG_ERROR("Failed to release native buffer surface (# queue : %d)\n", native_buffer_pool_get_input_buffer_count(mPool));
192 // create damage for client applications which wish to know the update timing
193 if( mRenderNotification )
195 // use notification trigger
196 // Tell the event-thread to render the pixmap
197 mRenderNotification->Trigger();
200 // Do render synchronisation
201 DoRenderSync( timeDelta, syncMode );
204 void NativeBufferRenderSurface::CreateXRenderable()
206 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
211 void NativeBufferRenderSurface::UseExistingRenderable( unsigned int surfaceId )
213 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
220 } // namespace Adaptor
222 } // namespace Internal