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
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
108 WindowSystem::GetScreenSize( mPositionSize.width, mPositionSize.height );
111 // Create a window base
112 auto windowFactory = Dali::Internal::Adaptor::GetWindowFactory();
113 mWindowBase = windowFactory->CreateWindowBase( mPositionSize, surface, ( mColorDepth == COLOR_DEPTH_32 ? true : false ) );
116 mWindowBase->OutputTransformedSignal().Connect( this, &WindowRenderSurface::OutputTransformed );
118 // Check screen rotation
119 mScreenRotationAngle = mWindowBase->GetScreenRotationAngle();
120 if( mScreenRotationAngle != 0 )
122 mScreenRotationFinished = false;
123 mResizeFinished = false;
127 Any WindowRenderSurface::GetNativeWindow()
129 return mWindowBase->GetNativeWindow();
132 int WindowRenderSurface::GetNativeWindowId()
134 return mWindowBase->GetNativeWindowId();
137 void WindowRenderSurface::Map()
142 void WindowRenderSurface::SetRenderNotification( TriggerEventInterface* renderNotification )
144 mRenderNotification = renderNotification;
147 void WindowRenderSurface::SetTransparency( bool transparent )
149 mWindowBase->SetTransparency( transparent );
152 void WindowRenderSurface::RequestRotation( int angle, int width, int height )
154 if( !mRotationTrigger )
156 TriggerEventFactoryInterface& triggerFactory = Internal::Adaptor::Adaptor::GetImplementation( Adaptor::Get() ).GetTriggerEventFactoryInterface();
157 mRotationTrigger = triggerFactory.CreateTriggerEvent( MakeCallback( this, &WindowRenderSurface::ProcessRotationRequest ), TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER );
160 mPositionSize.width = width;
161 mPositionSize.height = height;
163 mRotationAngle = angle;
164 mRotationFinished = false;
166 mWindowBase->SetWindowRotationAngle( mRotationAngle );
168 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::Rotate: angle = %d screen rotation = %d\n", mRotationAngle, mScreenRotationAngle );
171 WindowBase* WindowRenderSurface::GetWindowBase()
173 return mWindowBase.get();
176 WindowBase::OutputSignalType& WindowRenderSurface::OutputTransformedSignal()
178 return mOutputTransformedSignal;
181 PositionSize WindowRenderSurface::GetPositionSize() const
183 return mPositionSize;
186 void WindowRenderSurface::GetDpi( unsigned int& dpiHorizontal, unsigned int& dpiVertical )
188 if( mDpiHorizontal == 0 || mDpiVertical == 0 )
190 const char* environmentDpiHorizontal = std::getenv( DALI_ENV_DPI_HORIZONTAL );
191 mDpiHorizontal = environmentDpiHorizontal ? std::atoi( environmentDpiHorizontal ) : 0;
193 const char* environmentDpiVertical = std::getenv( DALI_ENV_DPI_VERTICAL );
194 mDpiVertical = environmentDpiVertical ? std::atoi( environmentDpiVertical ) : 0;
196 if( mDpiHorizontal == 0 || mDpiVertical == 0 )
198 mWindowBase->GetDpi( mDpiHorizontal, mDpiVertical );
202 dpiHorizontal = mDpiHorizontal;
203 dpiVertical = mDpiVertical;
206 int WindowRenderSurface::GetOrientation() const
208 return mWindowBase->GetOrientation();
211 void WindowRenderSurface::InitializeGraphics()
214 mGraphics = &mAdaptor->GetGraphicsInterface();
216 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
217 mEGL = &eglGraphics->GetEglInterface();
219 if ( mEGLContext == NULL )
221 // Create the OpenGL context for this window
222 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>(*mEGL);
223 eglImpl.ChooseConfig(true, mColorDepth);
224 eglImpl.CreateWindowContext( mEGLContext );
226 // Create the OpenGL surface
231 void WindowRenderSurface::CreateSurface()
233 DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
236 if( mScreenRotationAngle == 0 || mScreenRotationAngle == 180 )
238 width = mPositionSize.width;
239 height = mPositionSize.height;
243 width = mPositionSize.height;
244 height = mPositionSize.width;
247 // Create the EGL window
248 EGLNativeWindowType window = mWindowBase->CreateEglWindow( width, height );
250 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
252 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
253 mEGLSurface = eglImpl.CreateSurfaceWindow( window, mColorDepth );
255 // Check rotation capability
256 mRotationSupported = mWindowBase->IsEglWindowRotationSupported();
258 DALI_LOG_RELEASE_INFO("WindowRenderSurface::CreateSurface: w = %d h = %d angle = %d screen rotation = %d\n",
259 mPositionSize.width, mPositionSize.height, mRotationAngle, mScreenRotationAngle );
262 void WindowRenderSurface::DestroySurface()
264 DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
266 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
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 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PostRender: Trigger rotation event\n" );
523 mRotationTrigger->Trigger();
525 if( mThreadSynchronization )
527 // Wait until the event-thread complete the rotation event processing
528 mThreadSynchronization->PostRenderWaitForCompletion();
534 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
536 eglImpl.SwapBuffers( mEGLSurface );
538 if( mRenderNotification )
540 mRenderNotification->Trigger();
545 void WindowRenderSurface::StopRender()
549 void WindowRenderSurface::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
551 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::SetThreadSynchronization: called\n" );
553 mThreadSynchronization = &threadSynchronization;
556 void WindowRenderSurface::ReleaseLock()
561 Integration::RenderSurface::Type WindowRenderSurface::GetSurfaceType()
563 return RenderSurface::WINDOW_RENDER_SURFACE;
566 void WindowRenderSurface::MakeContextCurrent()
568 if ( mEGL != nullptr )
570 mEGL->MakeContextCurrent( mEGLSurface, mEGLContext );
574 Integration::DepthBufferAvailable WindowRenderSurface::GetDepthBufferRequired()
576 return mGraphics ? mGraphics->GetDepthBufferRequired() : Integration::DepthBufferAvailable::FALSE;
579 Integration::StencilBufferAvailable WindowRenderSurface::GetStencilBufferRequired()
581 return mGraphics ? mGraphics->GetStencilBufferRequired() : Integration::StencilBufferAvailable::FALSE;
584 void WindowRenderSurface::OutputTransformed()
586 int screenRotationAngle = mWindowBase->GetScreenRotationAngle();
588 if( mScreenRotationAngle != screenRotationAngle )
590 mScreenRotationAngle = screenRotationAngle;
591 mScreenRotationFinished = false;
592 mResizeFinished = false;
594 mOutputTransformedSignal.Emit();
596 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::OutputTransformed: angle = %d screen rotation = %d\n", mRotationAngle, mScreenRotationAngle );
600 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::OutputTransformed: Ignore output transform [%d]\n", mScreenRotationAngle );
604 void WindowRenderSurface::ProcessRotationRequest()
606 mRotationFinished = true;
608 mWindowBase->WindowRotationCompleted( mRotationAngle, mPositionSize.width, mPositionSize.height );
610 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::ProcessRotationRequest: Rotation Done\n" );
612 if( mThreadSynchronization )
614 mThreadSynchronization->PostRenderComplete();
618 } // namespace Adaptor
620 } // namespace internal