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.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
48 const int TILE_SIZE = 16u; ///< Unit of tile size at GPU driver
50 #if defined(DEBUG_ENABLED)
51 Debug::Filter* gWindowRenderSurfaceLogFilter = Debug::Filter::New(Debug::Verbose, false, "LOG_WINDOW_RENDER_SURFACE");
54 } // unnamed namespace
56 WindowRenderSurface::WindowRenderSurface( Dali::PositionSize positionSize, Any surface, bool isTransparent )
58 mDisplayConnection( nullptr ),
59 mPositionSize( positionSize ),
61 mThreadSynchronization( NULL ),
62 mRenderNotification( NULL ),
63 mRotationTrigger( NULL ),
65 mEGLSurface( nullptr ),
66 mEGLContext( nullptr ),
67 mColorDepth( isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24 ),
68 mOutputTransformedSignal(),
70 mScreenRotationAngle( 0 ),
74 mRotationSupported( false ),
75 mRotationFinished( true ),
76 mScreenRotationFinished( true ),
77 mResizeFinished( true ),
82 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "Creating Window\n" );
83 Initialize( surface );
86 WindowRenderSurface::~WindowRenderSurface()
88 if( mRotationTrigger )
90 delete mRotationTrigger;
99 void WindowRenderSurface::Initialize( Any surface )
101 // If width or height are zero, go full screen.
102 if ( (mPositionSize.width == 0) || (mPositionSize.height == 0) )
104 // Default window size == screen size
107 WindowSystem::GetScreenSize( mPositionSize.width, mPositionSize.height );
110 // Create a window base
111 auto windowFactory = Dali::Internal::Adaptor::GetWindowFactory();
112 mWindowBase = windowFactory->CreateWindowBase( mPositionSize, surface, ( mColorDepth == COLOR_DEPTH_32 ? true : false ) );
115 mWindowBase->OutputTransformedSignal().Connect( this, &WindowRenderSurface::OutputTransformed );
117 // Check screen rotation
118 mScreenRotationAngle = mWindowBase->GetScreenRotationAngle();
119 if( mScreenRotationAngle != 0 )
121 mScreenRotationFinished = false;
122 mResizeFinished = false;
126 Any WindowRenderSurface::GetNativeWindow()
128 return mWindowBase->GetNativeWindow();
131 int WindowRenderSurface::GetNativeWindowId()
133 return mWindowBase->GetNativeWindowId();
136 void WindowRenderSurface::Map()
141 void WindowRenderSurface::SetRenderNotification( TriggerEventInterface* renderNotification )
143 mRenderNotification = renderNotification;
146 void WindowRenderSurface::SetTransparency( bool transparent )
148 mWindowBase->SetTransparency( transparent );
151 void WindowRenderSurface::RequestRotation( int angle, int width, int height )
153 if( !mRotationTrigger )
155 mRotationTrigger = TriggerEventFactory::CreateTriggerEvent( MakeCallback( this, &WindowRenderSurface::ProcessRotationRequest ), TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER );
158 mPositionSize.width = width;
159 mPositionSize.height = height;
161 mRotationAngle = angle;
162 mRotationFinished = false;
164 mWindowBase->SetWindowRotationAngle( mRotationAngle );
166 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::Rotate: angle = %d screen rotation = %d\n", mRotationAngle, mScreenRotationAngle );
169 WindowBase* WindowRenderSurface::GetWindowBase()
171 return mWindowBase.get();
174 WindowBase::OutputSignalType& WindowRenderSurface::OutputTransformedSignal()
176 return mOutputTransformedSignal;
179 PositionSize WindowRenderSurface::GetPositionSize() const
181 return mPositionSize;
184 void WindowRenderSurface::GetDpi( unsigned int& dpiHorizontal, unsigned int& dpiVertical )
186 if( mDpiHorizontal == 0 || mDpiVertical == 0 )
188 const char* environmentDpiHorizontal = std::getenv( DALI_ENV_DPI_HORIZONTAL );
189 mDpiHorizontal = environmentDpiHorizontal ? std::atoi( environmentDpiHorizontal ) : 0;
191 const char* environmentDpiVertical = std::getenv( DALI_ENV_DPI_VERTICAL );
192 mDpiVertical = environmentDpiVertical ? std::atoi( environmentDpiVertical ) : 0;
194 if( mDpiHorizontal == 0 || mDpiVertical == 0 )
196 mWindowBase->GetDpi( mDpiHorizontal, mDpiVertical );
200 dpiHorizontal = mDpiHorizontal;
201 dpiVertical = mDpiVertical;
204 int WindowRenderSurface::GetOrientation() const
206 return mWindowBase->GetOrientation();
209 void WindowRenderSurface::InitializeGraphics()
212 mGraphics = &mAdaptor->GetGraphicsInterface();
214 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
215 mEGL = &eglGraphics->GetEglInterface();
217 if ( mEGLContext == NULL )
219 // Create the OpenGL context for this window
220 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>(*mEGL);
221 eglImpl.ChooseConfig(true, mColorDepth);
222 eglImpl.CreateWindowContext( mEGLContext );
224 // Create the OpenGL surface
229 void WindowRenderSurface::CreateSurface()
231 DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
234 if( mScreenRotationAngle == 0 || mScreenRotationAngle == 180 )
236 width = mPositionSize.width;
237 height = mPositionSize.height;
241 width = mPositionSize.height;
242 height = mPositionSize.width;
245 // Create the EGL window
246 EGLNativeWindowType window = mWindowBase->CreateEglWindow( width, height );
248 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
250 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
251 mEGLSurface = eglImpl.CreateSurfaceWindow( window, mColorDepth );
253 // Check rotation capability
254 mRotationSupported = mWindowBase->IsEglWindowRotationSupported();
256 DALI_LOG_RELEASE_INFO("WindowRenderSurface::CreateSurface: WinId (%d), w = %d h = %d angle = %d screen rotation = %d\n",
257 mWindowBase->GetNativeWindowId(), mPositionSize.width, mPositionSize.height, mRotationAngle, mScreenRotationAngle );
260 void WindowRenderSurface::DestroySurface()
262 DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
264 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
266 DALI_LOG_RELEASE_INFO("WindowRenderSurface::DestroySurface: WinId (%d)\n", mWindowBase->GetNativeWindowId() );
268 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
269 eglImpl.DestroySurface( mEGLSurface );
271 mWindowBase->DestroyEglWindow();
274 bool WindowRenderSurface::ReplaceGraphicsSurface()
276 DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
278 // Destroy the old one
279 mWindowBase->DestroyEglWindow();
282 if( mScreenRotationAngle == 0 || mScreenRotationAngle == 180 )
284 width = mPositionSize.width;
285 height = mPositionSize.height;
289 width = mPositionSize.height;
290 height = mPositionSize.width;
293 // Create the EGL window
294 EGLNativeWindowType window = mWindowBase->CreateEglWindow( width, height );
296 // Set screen rotation
297 mScreenRotationFinished = false;
299 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
301 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
302 return eglImpl.ReplaceSurfaceWindow( window, mEGLSurface, mEGLContext );
305 void WindowRenderSurface::MoveResize( Dali::PositionSize positionSize )
307 bool needToMove = false;
308 bool needToResize = false;
311 if( (fabs(positionSize.x - mPositionSize.x) > MINIMUM_DIMENSION_CHANGE) ||
312 (fabs(positionSize.y - mPositionSize.y) > MINIMUM_DIMENSION_CHANGE) )
318 if( (fabs(positionSize.width - mPositionSize.width) > MINIMUM_DIMENSION_CHANGE) ||
319 (fabs(positionSize.height - mPositionSize.height) > MINIMUM_DIMENSION_CHANGE) )
328 mWindowBase->MoveResize( positionSize );
332 mWindowBase->Resize( positionSize );
335 mResizeFinished = false;
336 mPositionSize = positionSize;
342 mWindowBase->Move( positionSize );
344 mPositionSize = positionSize;
348 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::MoveResize: %d, %d, %d, %d\n", mPositionSize.x, mPositionSize.y, mPositionSize.width, mPositionSize.height );
351 void WindowRenderSurface::StartRender()
355 bool WindowRenderSurface::PreRender( bool resizingSurface )
357 MakeContextCurrent();
359 if( resizingSurface )
361 int totalAngle = (mRotationAngle + mScreenRotationAngle) % 360;
363 // Window rotate or screen rotate
364 if( !mRotationFinished || !mScreenRotationFinished )
366 mWindowBase->SetEglWindowRotation( totalAngle );
367 mWindowBase->SetEglWindowBufferTransform( totalAngle );
369 // Reset only screen rotation flag
370 mScreenRotationFinished = true;
372 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PreRender: Set rotation [%d] [%d]\n", mRotationAngle, mScreenRotationAngle );
375 // Only window rotate
376 if( !mRotationFinished )
378 mWindowBase->SetEglWindowTransform( mRotationAngle );
382 if ( !mResizeFinished )
384 Dali::PositionSize positionSize;
385 positionSize.x = mPositionSize.x;
386 positionSize.y = mPositionSize.y;
387 if( totalAngle == 0 || totalAngle == 180 )
389 positionSize.width = mPositionSize.width;
390 positionSize.height = mPositionSize.height;
394 positionSize.width = mPositionSize.height;
395 positionSize.height = mPositionSize.width;
398 mWindowBase->ResizeEglWindow( positionSize );
399 mResizeFinished = true;
401 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PreRender: Set resize\n" );
405 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
408 GlImplementation& mGLES = eglGraphics->GetGlesInterface();
415 std::vector<int32_t> WindowRenderSurface::MergeRect( const Rect<int32_t>& damagedRect, int bufferAge )
417 std::vector<int32_t> mergedRectArray;
419 int dx1 = mPositionSize.width, dx2 = 0, dy1 = mPositionSize.height, dy2 = 0;
420 int checkWidth = mPositionSize.width - TILE_SIZE;
421 int checkHeight = mPositionSize.height - TILE_SIZE;
423 dx1 = std::min( damagedRect.x, dx1 );
424 dx2 = std::max( damagedRect.x + damagedRect.width, dx2);
425 dy1 = std::min( damagedRect.y, dy1 );
426 dy2 = std::max( damagedRect.y + damagedRect.height, dy2 );
428 for( int j = 0; j <= bufferAge; j++ )
430 if( !mPreDamagedRect[j].IsEmpty() )
432 dx1 = std::min( mPreDamagedRect[j].x, dx1 );
433 dx2 = std::max( mPreDamagedRect[j].x + mPreDamagedRect[j].width, dx2);
434 dy1 = std::min( mPreDamagedRect[j].y, dy1 );
435 dy2 = std::max( mPreDamagedRect[j].y + mPreDamagedRect[j].height, dy2 );
437 if( dx1 < TILE_SIZE && dx2 > checkWidth && dy1 < TILE_SIZE && dy2 > checkHeight )
439 dx1 = 0, dx2 = mPositionSize.width, dy1 = 0, dy2 = mPositionSize.height;
445 dx1 = TILE_SIZE * (dx1 / TILE_SIZE);
446 dy1 = TILE_SIZE * (dy1 / TILE_SIZE);
447 dx2 = TILE_SIZE * ((dx2 + TILE_SIZE - 1) / TILE_SIZE);
448 dy2 = TILE_SIZE * ((dy2 + TILE_SIZE - 1) / TILE_SIZE);
450 mergedRectArray.push_back( dx1 );
451 mergedRectArray.push_back( dy1 );
452 mergedRectArray.push_back( dx2 - dx1 );
453 mergedRectArray.push_back( dy2 - dy1 );
455 return mergedRectArray;
459 void WindowRenderSurface::SetDamagedRect( const Dali::DamagedRect& damagedRect, Dali::DamagedRect& mergedRect )
461 auto eglGraphics = static_cast<EglGraphics *>( mGraphics );
462 std::vector<int32_t> rectArray;
465 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
467 rectArray = MergeRect( damagedRect, mBufferAge );
469 mPreDamagedRect[4] = std::move( mPreDamagedRect[3] );
470 mPreDamagedRect[3] = std::move( mPreDamagedRect[2] );
471 mPreDamagedRect[2] = std::move( mPreDamagedRect[1] );
472 mPreDamagedRect[1] = std::move( mPreDamagedRect[0] );
473 mPreDamagedRect[0] = std::move( damagedRect );
475 eglImpl.SetDamagedRect( rectArray, mEGLSurface );
478 if( !rectArray.empty() )
480 mergedRect.x = rectArray[0];
481 mergedRect.y = rectArray[1];
482 mergedRect.width = rectArray[2];
483 mergedRect.height = rectArray[3];
487 int32_t WindowRenderSurface::GetBufferAge()
489 int result = mBufferAge = 0;
490 auto eglGraphics = static_cast<EglGraphics *>( mGraphics );
493 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
494 mBufferAge = eglImpl.GetBufferAge( mEGLSurface );;
495 result = ( mBufferAge != mPreBufferAge ) ? 0 : mBufferAge;
496 mPreBufferAge = mBufferAge;
501 void WindowRenderSurface::PostRender( bool renderToFbo, bool replacingSurface, bool resizingSurface )
503 // Inform the gl implementation that rendering has finished before informing the surface
504 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
507 GlImplementation& mGLES = eglGraphics->GetGlesInterface();
517 if( resizingSurface )
519 if( !mRotationFinished )
521 if( mThreadSynchronization )
523 // Enable PostRender flag
524 mThreadSynchronization->PostRenderStarted();
527 DALI_LOG_RELEASE_INFO("WindowRenderSurface::PostRender: Trigger rotation event\n" );
529 mRotationTrigger->Trigger();
531 if( mThreadSynchronization )
533 // Wait until the event-thread complete the rotation event processing
534 mThreadSynchronization->PostRenderWaitForCompletion();
540 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
542 eglImpl.SwapBuffers( mEGLSurface );
544 if( mRenderNotification )
546 mRenderNotification->Trigger();
551 void WindowRenderSurface::StopRender()
555 void WindowRenderSurface::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
557 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::SetThreadSynchronization: called\n" );
559 mThreadSynchronization = &threadSynchronization;
562 void WindowRenderSurface::ReleaseLock()
567 Integration::RenderSurface::Type WindowRenderSurface::GetSurfaceType()
569 return RenderSurface::WINDOW_RENDER_SURFACE;
572 void WindowRenderSurface::MakeContextCurrent()
574 if ( mEGL != nullptr )
576 mEGL->MakeContextCurrent( mEGLSurface, mEGLContext );
580 Integration::DepthBufferAvailable WindowRenderSurface::GetDepthBufferRequired()
582 return mGraphics ? mGraphics->GetDepthBufferRequired() : Integration::DepthBufferAvailable::FALSE;
585 Integration::StencilBufferAvailable WindowRenderSurface::GetStencilBufferRequired()
587 return mGraphics ? mGraphics->GetStencilBufferRequired() : Integration::StencilBufferAvailable::FALSE;
590 void WindowRenderSurface::OutputTransformed()
592 int screenRotationAngle = mWindowBase->GetScreenRotationAngle();
594 if( mScreenRotationAngle != screenRotationAngle )
596 mScreenRotationAngle = screenRotationAngle;
597 mScreenRotationFinished = false;
598 mResizeFinished = false;
600 mOutputTransformedSignal.Emit();
602 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::OutputTransformed: angle = %d screen rotation = %d\n", mRotationAngle, mScreenRotationAngle );
606 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::OutputTransformed: Ignore output transform [%d]\n", mScreenRotationAngle );
610 void WindowRenderSurface::ProcessRotationRequest()
612 mRotationFinished = true;
614 mWindowBase->WindowRotationCompleted( mRotationAngle, mPositionSize.width, mPositionSize.height );
616 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::ProcessRotationRequest: Rotation Done\n" );
618 if( mThreadSynchronization )
620 mThreadSynchronization->PostRenderComplete();
624 } // namespace Adaptor
626 } // namespace internal