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 "ecore-wl-render-surface.h"
22 #include <dali/integration-api/gl-abstraction.h>
23 #include <dali/integration-api/debug.h>
26 #include <ecore-wl-types.h>
27 #include <trigger-event.h>
38 #if defined(DEBUG_ENABLED)
39 Debug::Filter* gRenderSurfaceLogFilter = Debug::Filter::New(Debug::Verbose, false, "LOG_ECORE_X_RENDER_SURFACE");
48 const float MINIMUM_DIMENSION_CHANGE = 1.0f; ///< Minimum change for window to be considered to have moved
49 static bool gWlInitThreadsCalled = false; ///< global to say whether WlInitThreads has been called in this process
50 const unsigned int MICROSECONDS_PER_SECOND = 1000000;
51 const unsigned int MILLISECONDS_PER_SECOND = 1000;
53 } // unnamed namespace
55 RenderSurface::RenderSurface( SurfaceType type,
56 Dali::PositionSize positionSize,
59 const std::string& name,
63 mPosition(positionSize),
65 mColorDepth(isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24),
66 mRenderMode((type == PIXMAP) ? RENDER_SYNC : RENDER_DEFAULT),
67 mRenderNotification( NULL ),
68 mSyncReceived( false ),
73 // see if there is a display in Any display
74 SetDisplay( display );
77 void RenderSurface::Init( Any surface )
79 // see if there is a surface in Any surface
80 unsigned int surfaceId = GetSurfaceId( surface );
82 // if the surface is empty, create a new one.
85 if ( !gWlInitThreadsCalled )
88 mMainDisplay = ecore_wl_display_get();
90 gWlInitThreadsCalled = true;
93 // we own the surface about to created
99 // XLib should already be initialized so no point in calling XInitThreads
100 UseExistingRenderable( surfaceId );
104 // prints out 'INFO: DALI: new RenderSurface, created display xx, used existing surface xx
105 // we can not use LOG_INFO because the surface can be created before Dali Core is created.
106 printf( "INFO: DALI: new RenderSurface, %s display %p, %s %s surface %X \n",
107 mOwnDisplay?"created":"used existing",mMainDisplay,
108 mOwnSurface?"created":"used existing",
109 Dali::RenderSurface::PIXMAP==mType?" pixmap" :"window",
114 RenderSurface::~RenderSurface()
116 // release the display connection if we use our own
121 // NOTE, on 64bit desktop with some NVidia driver versions this crashes
126 Ecore_Wl_Window* RenderSurface::GetWlWindow()
131 WlDisplay* RenderSurface::GetMainDisplay()
136 void RenderSurface::SetRenderNotification( TriggerEvent* renderNotification )
138 mRenderNotification = renderNotification;
141 Ecore_Wl_Window* RenderSurface::GetDrawable()
146 Any RenderSurface::GetDisplay()
148 // this getter is used by main thread so we need to return the main thread version of the display
149 return Any( ecore_wl_display_get() );
152 PositionSize RenderSurface::GetPositionSize() const
157 void RenderSurface::SetRenderMode(RenderMode mode)
162 Dali::RenderSurface::RenderMode RenderSurface::GetRenderMode() const
167 void RenderSurface::MoveResize( Dali::PositionSize positionSize )
169 // nothing to do in base class
172 void RenderSurface::GetDpi( unsigned int& dpiHorizontal, unsigned int& dpiVertical ) const
177 // 1 inch = 25.4 millimeters
178 xres = ecore_wl_dpi_get();
179 yres = ecore_wl_dpi_get();
181 dpiHorizontal = int(xres + 0.5f); // rounding
182 dpiVertical = int(yres + 0.5f);
185 void RenderSurface::Map()
189 void RenderSurface::TransferDisplayOwner( Internal::Adaptor::RenderSurface& newSurface )
191 // if we don't own the display return
192 if( mOwnDisplay == false )
197 RenderSurface* other = dynamic_cast< RenderSurface* >( &newSurface );
200 // if both surfaces share the same display, and this surface owns it,
201 // then transfer the ownership to the new surface
202 if( other->mMainDisplay == mMainDisplay )
205 other->mOwnDisplay = true;
210 void RenderSurface::ConsumeEvents()
214 void RenderSurface::StopRender()
216 // Stop blocking waiting for sync
217 SetSyncMode( RenderSurface::SYNC_MODE_NONE );
218 // Simulate a RenderSync in case render-thread is currently blocked
224 void RenderSurface::SetViewMode( ViewMode )
228 void RenderSurface::SetDisplay( Any display )
230 // the render surface can be passed either EFL e-core types, or x11 types
231 // we use boost any to determine at run time which type
233 if ( display.Empty() == false )
235 // check we have a valid type
236 DALI_ASSERT_ALWAYS( ( ( display.GetType() == typeid (Ecore_Wl_Display *)) ||
237 ( display.GetType() == typeid (WlDisplay *) ) )
239 "Display type is invalid" );
243 // display may point to EcoreXDisplay so may need to cast
244 if( display.GetType() == typeid (Ecore_Wl_Display*) )
246 mMainDisplay = static_cast< WlDisplay* >( ecore_wl_display_get() );
250 mMainDisplay = AnyCast< WlDisplay* >( display );
255 unsigned int RenderSurface::GetSurfaceId( Any surface ) const
257 unsigned int surfaceId = 0;
259 if ( surface.Empty() == false )
261 // check we have a valid type
262 DALI_ASSERT_ALWAYS( ( (surface.GetType() == typeid (Ecore_Wl_Window *) ) )
263 && "Surface type is invalid" );
265 surfaceId = AnyCast<unsigned int>( surface );
270 void RenderSurface::RenderSync()
275 void RenderSurface::DoRenderSync( unsigned int timeDelta, SyncMode syncMode )
277 // Should block waiting for RenderSync?
278 if( mRenderMode == Dali::RenderSurface::RENDER_SYNC )
280 boost::unique_lock< boost::mutex > lock( mSyncMutex );
283 if( syncMode != SYNC_MODE_NONE &&
284 mSyncMode != SYNC_MODE_NONE &&
287 mSyncNotify.wait( lock );
289 mSyncReceived = false;
291 // Software sync based on a timed delay?
292 else if( mRenderMode > Dali::RenderSurface::RENDER_SYNC )
294 unsigned int syncPeriod( MICROSECONDS_PER_SECOND / static_cast< unsigned int >( mRenderMode ) - MILLISECONDS_PER_SECOND );
295 if( timeDelta < syncPeriod )
297 usleep( syncPeriod - timeDelta );
304 } // namespace Adaptor
306 } // namespace Internal