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/trigger-event-factory-interface.h>
27 #include <dali/integration-api/thread-synchronization-interface.h>
28 #include <dali/internal/graphics/gles/egl-implementation.h>
29 #include <dali/internal/adaptor/common/adaptor-impl.h>
30 #include <dali/internal/adaptor/common/adaptor-internal-services.h>
31 #include <dali/internal/window-system/common/window-base.h>
32 #include <dali/internal/window-system/common/window-factory.h>
33 #include <dali/internal/window-system/common/window-system.h>
34 #include <dali/internal/graphics/gles/egl-graphics.h>
35 #include <dali/internal/system/common/environment-variables.h>
48 const int MINIMUM_DIMENSION_CHANGE( 1 ); ///< Minimum change for window to be considered to have moved
49 const int TILE_SIZE = 16u; ///< Unit of tile size at GPU driver
51 #if defined(DEBUG_ENABLED)
52 Debug::Filter* gWindowRenderSurfaceLogFilter = Debug::Filter::New(Debug::Verbose, false, "LOG_WINDOW_RENDER_SURFACE");
55 } // unnamed namespace
57 WindowRenderSurface::WindowRenderSurface( Dali::PositionSize positionSize, Any surface, bool isTransparent )
59 mDisplayConnection( nullptr ),
60 mPositionSize( positionSize ),
62 mThreadSynchronization( NULL ),
63 mRenderNotification( NULL ),
64 mRotationTrigger( NULL ),
66 mEGLSurface( nullptr ),
67 mEGLContext( nullptr ),
68 mColorDepth( isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24 ),
69 mOutputTransformedSignal(),
71 mScreenRotationAngle( 0 ),
75 mRotationSupported( false ),
76 mRotationFinished( true ),
77 mScreenRotationFinished( true ),
78 mResizeFinished( true ),
83 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "Creating Window\n" );
84 Initialize( surface );
87 WindowRenderSurface::~WindowRenderSurface()
89 if( mRotationTrigger )
91 delete mRotationTrigger;
100 void WindowRenderSurface::Initialize( Any surface )
102 // If width or height are zero, go full screen.
103 if ( (mPositionSize.width == 0) || (mPositionSize.height == 0) )
105 // Default window size == screen size
109 WindowSystem::GetScreenSize( mPositionSize.width, mPositionSize.height );
112 // Create a window base
113 auto windowFactory = Dali::Internal::Adaptor::GetWindowFactory();
114 mWindowBase = windowFactory->CreateWindowBase( mPositionSize, surface, ( mColorDepth == COLOR_DEPTH_32 ? true : false ) );
117 mWindowBase->OutputTransformedSignal().Connect( this, &WindowRenderSurface::OutputTransformed );
119 // Check screen rotation
120 mScreenRotationAngle = mWindowBase->GetScreenRotationAngle();
121 if( mScreenRotationAngle != 0 )
123 mScreenRotationFinished = false;
124 mResizeFinished = false;
128 Any WindowRenderSurface::GetNativeWindow()
130 return mWindowBase->GetNativeWindow();
133 int WindowRenderSurface::GetNativeWindowId()
135 return mWindowBase->GetNativeWindowId();
138 void WindowRenderSurface::Map()
143 void WindowRenderSurface::SetRenderNotification( TriggerEventInterface* renderNotification )
145 mRenderNotification = renderNotification;
148 void WindowRenderSurface::SetTransparency( bool transparent )
150 mWindowBase->SetTransparency( transparent );
153 void WindowRenderSurface::RequestRotation( int angle, int width, int height )
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 int WindowRenderSurface::GetOrientation() const
209 return mWindowBase->GetOrientation();
212 void WindowRenderSurface::InitializeGraphics()
215 mGraphics = &mAdaptor->GetGraphicsInterface();
217 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
218 mEGL = &eglGraphics->GetEglInterface();
220 if ( mEGLContext == NULL )
222 // Create the OpenGL context for this window
223 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>(*mEGL);
224 eglImpl.ChooseConfig(true, mColorDepth);
225 eglImpl.CreateWindowContext( mEGLContext );
227 // Create the OpenGL surface
232 void WindowRenderSurface::CreateSurface()
234 DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
237 if( mScreenRotationAngle == 0 || mScreenRotationAngle == 180 )
239 width = mPositionSize.width;
240 height = mPositionSize.height;
244 width = mPositionSize.height;
245 height = mPositionSize.width;
248 // Create the EGL window
249 EGLNativeWindowType window = mWindowBase->CreateEglWindow( width, height );
251 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
253 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
254 mEGLSurface = eglImpl.CreateSurfaceWindow( window, mColorDepth );
256 // Check rotation capability
257 mRotationSupported = mWindowBase->IsEglWindowRotationSupported();
259 DALI_LOG_RELEASE_INFO("WindowRenderSurface::CreateSurface: w = %d h = %d angle = %d screen rotation = %d\n",
260 mPositionSize.width, mPositionSize.height, mRotationAngle, mScreenRotationAngle );
263 void WindowRenderSurface::DestroySurface()
265 DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
267 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
269 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
270 eglImpl.DestroySurface( mEGLSurface );
272 mWindowBase->DestroyEglWindow();
275 bool WindowRenderSurface::ReplaceGraphicsSurface()
277 DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
279 // Destroy the old one
280 mWindowBase->DestroyEglWindow();
283 if( mScreenRotationAngle == 0 || mScreenRotationAngle == 180 )
285 width = mPositionSize.width;
286 height = mPositionSize.height;
290 width = mPositionSize.height;
291 height = mPositionSize.width;
294 // Create the EGL window
295 EGLNativeWindowType window = mWindowBase->CreateEglWindow( width, height );
297 // Set screen rotation
298 mScreenRotationFinished = false;
300 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
302 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
303 return eglImpl.ReplaceSurfaceWindow( window, mEGLSurface, mEGLContext );
306 void WindowRenderSurface::MoveResize( Dali::PositionSize positionSize )
308 bool needToMove = false;
309 bool needToResize = false;
312 if( (fabs(positionSize.x - mPositionSize.x) > MINIMUM_DIMENSION_CHANGE) ||
313 (fabs(positionSize.y - mPositionSize.y) > MINIMUM_DIMENSION_CHANGE) )
319 if( (fabs(positionSize.width - mPositionSize.width) > MINIMUM_DIMENSION_CHANGE) ||
320 (fabs(positionSize.height - mPositionSize.height) > MINIMUM_DIMENSION_CHANGE) )
329 mWindowBase->MoveResize( positionSize );
333 mWindowBase->Resize( positionSize );
336 mResizeFinished = false;
337 mPositionSize = positionSize;
343 mWindowBase->Move( positionSize );
345 mPositionSize = positionSize;
349 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::MoveResize: %d, %d, %d, %d\n", mPositionSize.x, mPositionSize.y, mPositionSize.width, mPositionSize.height );
352 void WindowRenderSurface::StartRender()
356 bool WindowRenderSurface::PreRender( bool resizingSurface )
358 MakeContextCurrent();
360 if( resizingSurface )
362 int totalAngle = (mRotationAngle + mScreenRotationAngle) % 360;
364 // Window rotate or screen rotate
365 if( !mRotationFinished || !mScreenRotationFinished )
367 mWindowBase->SetEglWindowRotation( totalAngle );
368 mWindowBase->SetEglWindowBufferTransform( totalAngle );
370 // Reset only screen rotation flag
371 mScreenRotationFinished = true;
373 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PreRender: Set rotation [%d] [%d]\n", mRotationAngle, mScreenRotationAngle );
376 // Only window rotate
377 if( !mRotationFinished )
379 mWindowBase->SetEglWindowTransform( mRotationAngle );
383 if ( !mResizeFinished )
385 Dali::PositionSize positionSize;
386 positionSize.x = mPositionSize.x;
387 positionSize.y = mPositionSize.y;
388 if( totalAngle == 0 || totalAngle == 180 )
390 positionSize.width = mPositionSize.width;
391 positionSize.height = mPositionSize.height;
395 positionSize.width = mPositionSize.height;
396 positionSize.height = mPositionSize.width;
399 mWindowBase->ResizeEglWindow( positionSize );
400 mResizeFinished = true;
402 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PreRender: Set resize\n" );
406 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
409 GlImplementation& mGLES = eglGraphics->GetGlesInterface();
416 std::vector<int32_t> WindowRenderSurface::MergeRect( const Rect<int32_t>& damagedRect, int bufferAge )
418 std::vector<int32_t> mergedRectArray;
420 int dx1 = mPositionSize.width, dx2 = 0, dy1 = mPositionSize.height, dy2 = 0;
421 int checkWidth = mPositionSize.width - TILE_SIZE;
422 int checkHeight = mPositionSize.height - TILE_SIZE;
424 dx1 = std::min( damagedRect.x, dx1 );
425 dx2 = std::max( damagedRect.x + damagedRect.width, dx2);
426 dy1 = std::min( damagedRect.y, dy1 );
427 dy2 = std::max( damagedRect.y + damagedRect.height, dy2 );
429 for( int j = 0; j <= bufferAge; j++ )
431 if( !mPreDamagedRect[j].IsEmpty() )
433 dx1 = std::min( mPreDamagedRect[j].x, dx1 );
434 dx2 = std::max( mPreDamagedRect[j].x + mPreDamagedRect[j].width, dx2);
435 dy1 = std::min( mPreDamagedRect[j].y, dy1 );
436 dy2 = std::max( mPreDamagedRect[j].y + mPreDamagedRect[j].height, dy2 );
438 if( dx1 < TILE_SIZE && dx2 > checkWidth && dy1 < TILE_SIZE && dy2 > checkHeight )
440 dx1 = 0, dx2 = mPositionSize.width, dy1 = 0, dy2 = mPositionSize.height;
446 dx1 = TILE_SIZE * (dx1 / TILE_SIZE);
447 dy1 = TILE_SIZE * (dy1 / TILE_SIZE);
448 dx2 = TILE_SIZE * ((dx2 + TILE_SIZE - 1) / TILE_SIZE);
449 dy2 = TILE_SIZE * ((dy2 + TILE_SIZE - 1) / TILE_SIZE);
451 mergedRectArray.push_back( dx1 );
452 mergedRectArray.push_back( dy1 );
453 mergedRectArray.push_back( dx2 - dx1 );
454 mergedRectArray.push_back( dy2 - dy1 );
456 return mergedRectArray;
460 void WindowRenderSurface::SetDamagedRect( const Dali::DamagedRect& damagedRect, Dali::DamagedRect& mergedRect )
462 auto eglGraphics = static_cast<EglGraphics *>( mGraphics );
463 std::vector<int32_t> rectArray;
466 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
468 rectArray = MergeRect( damagedRect, mBufferAge );
470 mPreDamagedRect[4] = std::move( mPreDamagedRect[3] );
471 mPreDamagedRect[3] = std::move( mPreDamagedRect[2] );
472 mPreDamagedRect[2] = std::move( mPreDamagedRect[1] );
473 mPreDamagedRect[1] = std::move( mPreDamagedRect[0] );
474 mPreDamagedRect[0] = std::move( damagedRect );
476 eglImpl.SetDamagedRect( rectArray, mEGLSurface );
479 if( !rectArray.empty() )
481 mergedRect.x = rectArray[0];
482 mergedRect.y = rectArray[1];
483 mergedRect.width = rectArray[2];
484 mergedRect.height = rectArray[3];
488 int32_t WindowRenderSurface::GetBufferAge()
490 int result = mBufferAge = 0;
491 auto eglGraphics = static_cast<EglGraphics *>( mGraphics );
494 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
495 mBufferAge = eglImpl.GetBufferAge( mEGLSurface );;
496 result = ( mBufferAge != mPreBufferAge ) ? 0 : mBufferAge;
497 mPreBufferAge = mBufferAge;
502 void WindowRenderSurface::PostRender( bool renderToFbo, bool replacingSurface, bool resizingSurface )
504 // Inform the gl implementation that rendering has finished before informing the surface
505 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
508 GlImplementation& mGLES = eglGraphics->GetGlesInterface();
518 if( resizingSurface )
520 if( !mRotationFinished )
522 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PostRender: Trigger rotation event\n" );
524 mRotationTrigger->Trigger();
526 if( mThreadSynchronization )
528 // Wait until the event-thread complete the rotation event processing
529 mThreadSynchronization->PostRenderWaitForCompletion();
535 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
537 eglImpl.SwapBuffers( mEGLSurface );
539 if( mRenderNotification )
541 mRenderNotification->Trigger();
546 void WindowRenderSurface::StopRender()
550 void WindowRenderSurface::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
552 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::SetThreadSynchronization: called\n" );
554 mThreadSynchronization = &threadSynchronization;
557 void WindowRenderSurface::ReleaseLock()
562 Integration::RenderSurface::Type WindowRenderSurface::GetSurfaceType()
564 return RenderSurface::WINDOW_RENDER_SURFACE;
567 void WindowRenderSurface::MakeContextCurrent()
569 if ( mEGL != nullptr )
571 mEGL->MakeContextCurrent( mEGLSurface, mEGLContext );
575 Integration::DepthBufferAvailable WindowRenderSurface::GetDepthBufferRequired()
577 return mGraphics ? mGraphics->GetDepthBufferRequired() : Integration::DepthBufferAvailable::FALSE;
580 Integration::StencilBufferAvailable WindowRenderSurface::GetStencilBufferRequired()
582 return mGraphics ? mGraphics->GetStencilBufferRequired() : Integration::StencilBufferAvailable::FALSE;
585 void WindowRenderSurface::OutputTransformed()
587 int screenRotationAngle = mWindowBase->GetScreenRotationAngle();
589 if( mScreenRotationAngle != screenRotationAngle )
591 mScreenRotationAngle = screenRotationAngle;
592 mScreenRotationFinished = false;
593 mResizeFinished = false;
595 mOutputTransformedSignal.Emit();
597 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::OutputTransformed: angle = %d screen rotation = %d\n", mRotationAngle, mScreenRotationAngle );
601 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::OutputTransformed: Ignore output transform [%d]\n", mScreenRotationAngle );
605 void WindowRenderSurface::ProcessRotationRequest()
607 mRotationFinished = true;
609 mWindowBase->WindowRotationCompleted( mRotationAngle, mPositionSize.width, mPositionSize.height );
611 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::ProcessRotationRequest: Rotation Done\n" );
613 if( mThreadSynchronization )
615 mThreadSynchronization->PostRenderComplete();
619 } // namespace Adaptor
621 } // namespace internal