2 * Copyright (c) 2019 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/window-system/common/window-render-surface.h>
22 #include <dali/integration-api/gl-abstraction.h>
23 #include <dali/integration-api/debug.h>
26 #include <dali/integration-api/adaptor-framework/thread-synchronization-interface.h>
27 #include <dali/integration-api/adaptor-framework/trigger-event-factory-interface.h>
28 #include <dali/internal/adaptor/common/adaptor-impl.h>
29 #include <dali/internal/adaptor/common/adaptor-internal-services.h>
30 #include <dali/internal/graphics/gles/egl-graphics.h>
31 #include <dali/internal/graphics/gles/egl-implementation.h>
32 #include <dali/internal/window-system/common/window-base.h>
33 #include <dali/internal/window-system/common/window-factory.h>
34 #include <dali/internal/window-system/common/window-system.h>
35 #include <dali/internal/system/common/environment-variables.h>
47 const int MINIMUM_DIMENSION_CHANGE( 1 ); ///< Minimum change for window to be considered to have moved
49 #if defined(DEBUG_ENABLED)
50 Debug::Filter* gWindowRenderSurfaceLogFilter = Debug::Filter::New(Debug::Verbose, false, "LOG_WINDOW_RENDER_SURFACE");
53 } // unnamed namespace
55 WindowRenderSurface::WindowRenderSurface( Dali::PositionSize positionSize, Any surface, bool isTransparent )
57 mDisplayConnection( nullptr ),
58 mPositionSize( positionSize ),
60 mThreadSynchronization( NULL ),
61 mRenderNotification( NULL ),
62 mRotationTrigger( NULL ),
64 mEGLSurface( nullptr ),
65 mEGLContext( nullptr ),
66 mColorDepth( isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24 ),
67 mOutputTransformedSignal(),
69 mScreenRotationAngle( 0 ),
71 mRotationSupported( false ),
72 mRotationFinished( true ),
73 mScreenRotationFinished( true ),
74 mResizeFinished( true ),
78 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "Creating Window\n" );
79 Initialize( surface );
82 WindowRenderSurface::~WindowRenderSurface()
84 if( mRotationTrigger )
86 delete mRotationTrigger;
95 void WindowRenderSurface::Initialize( Any surface )
97 // If width or height are zero, go full screen.
98 if ( (mPositionSize.width == 0) || (mPositionSize.height == 0) )
100 // Default window size == screen size
104 WindowSystem::GetScreenSize( mPositionSize.width, mPositionSize.height );
107 // Create a window base
108 auto windowFactory = Dali::Internal::Adaptor::GetWindowFactory();
109 mWindowBase = windowFactory->CreateWindowBase( mPositionSize, surface, ( mColorDepth == COLOR_DEPTH_32 ? true : false ) );
112 mWindowBase->OutputTransformedSignal().Connect( this, &WindowRenderSurface::OutputTransformed );
114 // Check screen rotation
115 mScreenRotationAngle = mWindowBase->GetScreenRotationAngle();
116 if( mScreenRotationAngle != 0 )
118 mScreenRotationFinished = false;
122 Any WindowRenderSurface::GetNativeWindow()
124 return mWindowBase->GetNativeWindow();
127 int WindowRenderSurface::GetNativeWindowId()
129 return mWindowBase->GetNativeWindowId();
132 void WindowRenderSurface::Map()
137 void WindowRenderSurface::SetRenderNotification( TriggerEventInterface* renderNotification )
139 mRenderNotification = renderNotification;
142 void WindowRenderSurface::SetTransparency( bool transparent )
144 mWindowBase->SetTransparency( transparent );
147 void WindowRenderSurface::RequestRotation( int angle, int width, int height )
149 if( !mRotationSupported )
151 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::Rotate: Rotation is not supported!\n" );
155 if( !mRotationTrigger )
157 TriggerEventFactoryInterface& triggerFactory = Internal::Adaptor::Adaptor::GetImplementation( Adaptor::Get() ).GetTriggerEventFactoryInterface();
158 mRotationTrigger = triggerFactory.CreateTriggerEvent( MakeCallback( this, &WindowRenderSurface::ProcessRotationRequest ), TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER );
161 mPositionSize.width = width;
162 mPositionSize.height = height;
164 mRotationAngle = angle;
165 mRotationFinished = false;
167 mWindowBase->SetWindowRotationAngle( mRotationAngle );
169 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::Rotate: angle = %d screen rotation = %d\n", mRotationAngle, mScreenRotationAngle );
172 WindowBase* WindowRenderSurface::GetWindowBase()
174 return mWindowBase.get();
177 WindowBase::OutputSignalType& WindowRenderSurface::OutputTransformedSignal()
179 return mOutputTransformedSignal;
182 PositionSize WindowRenderSurface::GetPositionSize() const
184 return mPositionSize;
187 void WindowRenderSurface::GetDpi( unsigned int& dpiHorizontal, unsigned int& dpiVertical )
189 if( mDpiHorizontal == 0 || mDpiVertical == 0 )
191 const char* environmentDpiHorizontal = std::getenv( DALI_ENV_DPI_HORIZONTAL );
192 mDpiHorizontal = environmentDpiHorizontal ? std::atoi( environmentDpiHorizontal ) : 0;
194 const char* environmentDpiVertical = std::getenv( DALI_ENV_DPI_VERTICAL );
195 mDpiVertical = environmentDpiVertical ? std::atoi( environmentDpiVertical ) : 0;
197 if( mDpiHorizontal == 0 || mDpiVertical == 0 )
199 mWindowBase->GetDpi( mDpiHorizontal, mDpiVertical );
203 dpiHorizontal = mDpiHorizontal;
204 dpiVertical = mDpiVertical;
207 void WindowRenderSurface::InitializeGraphics()
210 mGraphics = &mAdaptor->GetGraphicsInterface();
212 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
213 mEGL = &eglGraphics->GetEglInterface();
215 if ( mEGLContext == NULL )
217 // Create the OpenGL context for this window
218 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>(*mEGL);
219 eglImpl.ChooseConfig(true, mColorDepth);
220 eglImpl.CreateWindowContext( mEGLContext );
222 // Create the OpenGL surface
227 void WindowRenderSurface::CreateSurface()
229 DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
232 if( mScreenRotationAngle == 0 || mScreenRotationAngle == 180 )
234 width = mPositionSize.width;
235 height = mPositionSize.height;
239 width = mPositionSize.height;
240 height = mPositionSize.width;
243 // Create the EGL window
244 EGLNativeWindowType window = mWindowBase->CreateEglWindow( width, height );
246 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
248 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
249 mEGLSurface = eglImpl.CreateSurfaceWindow( window, mColorDepth );
251 // Check rotation capability
252 mRotationSupported = mWindowBase->IsEglWindowRotationSupported();
254 DALI_LOG_RELEASE_INFO("WindowRenderSurface::CreateSurface: w = %d h = %d angle = %d screen rotation = %d\n",
255 mPositionSize.width, mPositionSize.height, mRotationAngle, mScreenRotationAngle );
258 void WindowRenderSurface::DestroySurface()
260 DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
262 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
264 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
265 eglImpl.DestroySurface( mEGLSurface );
267 mWindowBase->DestroyEglWindow();
270 bool WindowRenderSurface::ReplaceGraphicsSurface()
272 DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
274 // Destroy the old one
275 mWindowBase->DestroyEglWindow();
278 if( mScreenRotationAngle == 0 || mScreenRotationAngle == 180 )
280 width = mPositionSize.width;
281 height = mPositionSize.height;
285 width = mPositionSize.height;
286 height = mPositionSize.width;
289 // Create the EGL window
290 EGLNativeWindowType window = mWindowBase->CreateEglWindow( width, height );
292 // Set screen rotation
293 mScreenRotationFinished = false;
295 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
297 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
298 return eglImpl.ReplaceSurfaceWindow( window, mEGLSurface, mEGLContext );
301 void WindowRenderSurface::MoveResize( Dali::PositionSize positionSize )
303 bool needToMove = false;
304 bool needToResize = false;
307 if( (fabs(positionSize.x - mPositionSize.x) > MINIMUM_DIMENSION_CHANGE) ||
308 (fabs(positionSize.y - mPositionSize.y) > MINIMUM_DIMENSION_CHANGE) )
314 if( (fabs(positionSize.width - mPositionSize.width) > MINIMUM_DIMENSION_CHANGE) ||
315 (fabs(positionSize.height - mPositionSize.height) > MINIMUM_DIMENSION_CHANGE) )
324 mWindowBase->MoveResize( positionSize );
328 mWindowBase->Resize( positionSize );
331 mResizeFinished = false;
332 mPositionSize = positionSize;
338 mWindowBase->Move( positionSize );
340 mPositionSize = positionSize;
344 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::MoveResize: %d, %d, %d, %d\n", mPositionSize.x, mPositionSize.y, mPositionSize.width, mPositionSize.height );
347 void WindowRenderSurface::StartRender()
351 bool WindowRenderSurface::PreRender( bool resizingSurface )
353 MakeContextCurrent();
355 if( resizingSurface )
357 // Window rotate or screen rotate
358 if( !mRotationFinished || !mScreenRotationFinished )
360 int totalAngle = (mRotationAngle + mScreenRotationAngle) % 360;
362 mWindowBase->SetEglWindowRotation( totalAngle );
363 mWindowBase->SetEglWindowBufferTransform( totalAngle );
365 // Reset only screen rotation flag
366 mScreenRotationFinished = true;
368 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PreRender: Set rotation [%d] [%d]\n", mRotationAngle, mScreenRotationAngle );
371 // Only window rotate
372 if( !mRotationFinished )
374 mWindowBase->SetEglWindowTransform( mRotationAngle );
378 if( !mResizeFinished )
380 mWindowBase->ResizeEglWindow( mPositionSize );
381 mResizeFinished = true;
383 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PreRender: Set resize\n" );
387 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
390 GlImplementation& mGLES = eglGraphics->GetGlesInterface();
397 void WindowRenderSurface::PostRender( bool renderToFbo, bool replacingSurface, bool resizingSurface )
399 // Inform the gl implementation that rendering has finished before informing the surface
400 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
403 GlImplementation& mGLES = eglGraphics->GetGlesInterface();
413 if( resizingSurface )
415 if( !mRotationFinished )
417 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PostRender: Trigger rotation event\n" );
419 mRotationTrigger->Trigger();
421 if( mThreadSynchronization )
423 // Wait until the event-thread complete the rotation event processing
424 mThreadSynchronization->PostRenderWaitForCompletion();
430 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
431 eglImpl.SwapBuffers( mEGLSurface );
433 if( mRenderNotification )
435 mRenderNotification->Trigger();
440 void WindowRenderSurface::StopRender()
444 void WindowRenderSurface::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
446 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::SetThreadSynchronization: called\n" );
448 mThreadSynchronization = &threadSynchronization;
451 void WindowRenderSurface::ReleaseLock()
456 Integration::RenderSurface::Type WindowRenderSurface::GetSurfaceType()
458 return RenderSurface::WINDOW_RENDER_SURFACE;
461 void WindowRenderSurface::MakeContextCurrent()
463 if ( mEGL != nullptr )
465 mEGL->MakeContextCurrent( mEGLSurface, mEGLContext );
469 Integration::DepthBufferAvailable WindowRenderSurface::GetDepthBufferRequired()
471 return mGraphics ? mGraphics->GetDepthBufferRequired() : Integration::DepthBufferAvailable::FALSE;
474 Integration::StencilBufferAvailable WindowRenderSurface::GetStencilBufferRequired()
476 return mGraphics ? mGraphics->GetStencilBufferRequired() : Integration::StencilBufferAvailable::FALSE;
479 void WindowRenderSurface::OutputTransformed()
481 int screenRotationAngle = mWindowBase->GetScreenRotationAngle();
483 if( mScreenRotationAngle != screenRotationAngle )
485 mScreenRotationAngle = screenRotationAngle;
486 mScreenRotationFinished = false;
488 mOutputTransformedSignal.Emit();
490 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::OutputTransformed: angle = %d screen rotation = %d\n", mRotationAngle, mScreenRotationAngle );
494 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::OutputTransformed: Ignore output transform [%d]\n", mScreenRotationAngle );
498 void WindowRenderSurface::ProcessRotationRequest()
500 mRotationFinished = true;
502 mWindowBase->WindowRotationCompleted( mRotationAngle, mPositionSize.width, mPositionSize.height );
504 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::ProcessRotationRequest: Rotation Done\n" );
506 if( mThreadSynchronization )
508 mThreadSynchronization->PostRenderComplete();
512 } // namespace Adaptor
514 } // namespace internal