Merge "Implemented the Handle assignment operators properly" into tizen
[platform/core/uifw/dali-adaptor.git] / adaptors / wayland / ecore-wl-render-surface.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 "ecore-wl-render-surface.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/gl-abstraction.h>
23 #include <dali/integration-api/debug.h>
24
25 // INTERNAL INCLUDES
26 #include <ecore-wl-types.h>
27 #include <trigger-event.h>
28
29 namespace Dali
30 {
31
32 namespace Internal
33 {
34
35 namespace Adaptor
36 {
37
38 #if defined(DEBUG_ENABLED)
39 Debug::Filter* gRenderSurfaceLogFilter = Debug::Filter::New(Debug::Verbose, false, "LOG_ECORE_X_RENDER_SURFACE");
40 #endif
41
42 namespace ECore
43 {
44
45 namespace
46 {
47
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;
52
53 } // unnamed namespace
54
55 RenderSurface::RenderSurface( SurfaceType type,
56                               Dali::PositionSize positionSize,
57                               Any surface,
58                               Any display,
59                               const std::string& name,
60                               bool isTransparent)
61 : mMainDisplay(NULL),
62   mType(type),
63   mPosition(positionSize),
64   mTitle(name),
65   mColorDepth(isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24),
66   mRenderMode((type == PIXMAP) ? RENDER_SYNC : RENDER_DEFAULT),
67   mRenderNotification( NULL ),
68   mSyncReceived( false ),
69   mOwnSurface(false),
70   mOwnDisplay(false),
71   mIsStopped( false )
72 {
73   // see if there is a display in Any display
74   SetDisplay( display );
75 }
76
77 void RenderSurface::Init( Any surface )
78 {
79   // see if there is a surface in Any surface
80   unsigned int surfaceId  = GetSurfaceId( surface );
81
82   // if the surface is empty, create a new one.
83   if ( surfaceId == 0 )
84   {
85     if ( !gWlInitThreadsCalled )
86     {
87       ecore_wl_init(NULL);
88       mMainDisplay = ecore_wl_display_get();
89       mOwnDisplay = true;
90       gWlInitThreadsCalled = true;
91     }
92
93     // we own the surface about to created
94     mOwnSurface = true;
95     CreateWlRenderable();
96   }
97   else
98   {
99     // XLib should already be initialized so no point in calling XInitThreads
100     UseExistingRenderable( surfaceId );
101   }
102
103 #ifdef DEBUG_ENABLED
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",
110              GetDrawable() );
111 #endif
112 }
113
114 RenderSurface::~RenderSurface()
115 {
116   // release the display connection if we use our own
117   if( mOwnDisplay )
118   {
119     if( mMainDisplay )
120     {
121       // NOTE, on 64bit desktop with some NVidia driver versions this crashes
122     }
123   }
124 }
125
126 Ecore_Wl_Window* RenderSurface::GetWlWindow()
127 {
128   return 0;
129 }
130
131 WlDisplay* RenderSurface::GetMainDisplay()
132 {
133   return mMainDisplay;
134 }
135
136 void RenderSurface::SetRenderNotification( TriggerEvent* renderNotification )
137 {
138   mRenderNotification = renderNotification;
139 }
140
141 Ecore_Wl_Window* RenderSurface::GetDrawable()
142 {
143   return 0;
144 }
145
146 Any RenderSurface::GetDisplay()
147 {
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() );
150 }
151
152 PositionSize RenderSurface::GetPositionSize() const
153 {
154   return mPosition;
155 }
156
157 void RenderSurface::SetRenderMode(RenderMode mode)
158 {
159   mRenderMode = mode;
160 }
161
162 Dali::RenderSurface::RenderMode RenderSurface::GetRenderMode() const
163 {
164   return mRenderMode;
165 }
166
167 void RenderSurface::MoveResize( Dali::PositionSize positionSize )
168 {
169   // nothing to do in base class
170 }
171
172 void RenderSurface::GetDpi( unsigned int& dpiHorizontal, unsigned int& dpiVertical ) const
173 {
174   // calculate DPI
175   float xres, yres;
176
177   // 1 inch = 25.4 millimeters
178   xres = ecore_wl_dpi_get();
179   yres = ecore_wl_dpi_get();
180
181   dpiHorizontal = int(xres + 0.5f);  // rounding
182   dpiVertical   = int(yres + 0.5f);
183 }
184
185 void RenderSurface::Map()
186 {
187 }
188
189 void RenderSurface::TransferDisplayOwner( Internal::Adaptor::RenderSurface& newSurface )
190 {
191   // if we don't own the display return
192   if( mOwnDisplay == false )
193   {
194     return;
195   }
196
197   RenderSurface* other = dynamic_cast< RenderSurface* >( &newSurface );
198   if( other )
199   {
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 )
203     {
204       mOwnDisplay = false;
205       other->mOwnDisplay = true;
206     }
207   }
208 }
209
210 void RenderSurface::ConsumeEvents()
211 {
212 }
213
214 void RenderSurface::StopRender()
215 {
216   // Stop blocking waiting for sync
217   SetSyncMode( RenderSurface::SYNC_MODE_NONE );
218   // Simulate a RenderSync in case render-thread is currently blocked
219   RenderSync();
220
221   mIsStopped = true;
222 }
223
224 void RenderSurface::SetViewMode( ViewMode )
225 {
226 }
227
228 void RenderSurface::SetDisplay( Any display )
229 {
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
232
233   if ( display.Empty() == false )
234   {
235     // check we have a valid type
236     DALI_ASSERT_ALWAYS( ( ( display.GetType() == typeid (Ecore_Wl_Display *)) ||
237                           ( display.GetType() == typeid (WlDisplay *) ) )
238                         &&
239                         "Display type is invalid" );
240
241     mOwnDisplay = false;
242
243     // display may point to EcoreXDisplay so may need to cast
244     if( display.GetType() == typeid (Ecore_Wl_Display*) )
245     {
246       mMainDisplay = static_cast< WlDisplay* >( ecore_wl_display_get() );
247     }
248     else
249     {
250       mMainDisplay = AnyCast< WlDisplay* >( display );
251     }
252   }
253 }
254
255 unsigned int RenderSurface::GetSurfaceId( Any surface ) const
256 {
257   unsigned int surfaceId = 0;
258
259   if ( surface.Empty() == false )
260   {
261     // check we have a valid type
262     DALI_ASSERT_ALWAYS( ( (surface.GetType() == typeid (Ecore_Wl_Window *) ) )
263                         && "Surface type is invalid" );
264
265     surfaceId = AnyCast<unsigned int>( surface );
266   }
267   return surfaceId;
268 }
269
270 void RenderSurface::RenderSync()
271 {
272   // nothing to do
273 }
274
275 void RenderSurface::DoRenderSync( unsigned int timeDelta, SyncMode syncMode )
276 {
277   // Should block waiting for RenderSync?
278   if( mRenderMode == Dali::RenderSurface::RENDER_SYNC )
279   {
280     boost::unique_lock< boost::mutex > lock( mSyncMutex );
281
282     // wait for sync
283     if( syncMode != SYNC_MODE_NONE &&
284         mSyncMode != SYNC_MODE_NONE &&
285         !mSyncReceived )
286     {
287       mSyncNotify.wait( lock );
288     }
289     mSyncReceived = false;
290   }
291   // Software sync based on a timed delay?
292   else if( mRenderMode > Dali::RenderSurface::RENDER_SYNC )
293   {
294     unsigned int syncPeriod( MICROSECONDS_PER_SECOND / static_cast< unsigned int >( mRenderMode ) - MILLISECONDS_PER_SECOND );
295     if( timeDelta < syncPeriod )
296     {
297       usleep( syncPeriod - timeDelta );
298     }
299   }
300 }
301
302 } // namespace ECore
303
304 } // namespace Adaptor
305
306 } // namespace Internal
307
308 } // namespace Dali