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
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 ),
72 mRotationSupported( false ),
73 mRotationFinished( true ),
74 mScreenRotationFinished( true ),
75 mResizeFinished( true )
77 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "Creating Window\n" );
78 Initialize( surface );
81 WindowRenderSurface::~WindowRenderSurface()
83 if( mRotationTrigger )
85 delete mRotationTrigger;
94 void WindowRenderSurface::Initialize( Any surface )
96 // If width or height are zero, go full screen.
97 if ( (mPositionSize.width == 0) || (mPositionSize.height == 0) )
99 // Default window size == screen size
103 WindowSystem::GetScreenSize( mPositionSize.width, mPositionSize.height );
106 // Create a window base
107 auto windowFactory = Dali::Internal::Adaptor::GetWindowFactory();
108 mWindowBase = windowFactory->CreateWindowBase( mPositionSize, surface, ( mColorDepth == COLOR_DEPTH_32 ? true : false ) );
111 mWindowBase->OutputTransformedSignal().Connect( this, &WindowRenderSurface::OutputTransformed );
113 // Check screen rotation
114 mScreenRotationAngle = mWindowBase->GetScreenRotationAngle();
115 if( mScreenRotationAngle != 0 )
117 mScreenRotationFinished = false;
118 mResizeFinished = 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( !mRotationTrigger )
151 TriggerEventFactoryInterface& triggerFactory = Internal::Adaptor::Adaptor::GetImplementation( Adaptor::Get() ).GetTriggerEventFactoryInterface();
152 mRotationTrigger = triggerFactory.CreateTriggerEvent( MakeCallback( this, &WindowRenderSurface::ProcessRotationRequest ), TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER );
155 mPositionSize.width = width;
156 mPositionSize.height = height;
158 mRotationAngle = angle;
159 mRotationFinished = false;
161 mWindowBase->SetWindowRotationAngle( mRotationAngle );
163 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::Rotate: angle = %d screen rotation = %d\n", mRotationAngle, mScreenRotationAngle );
166 WindowBase* WindowRenderSurface::GetWindowBase()
168 return mWindowBase.get();
171 WindowBase::OutputSignalType& WindowRenderSurface::OutputTransformedSignal()
173 return mOutputTransformedSignal;
176 PositionSize WindowRenderSurface::GetPositionSize() const
178 return mPositionSize;
181 void WindowRenderSurface::GetDpi( unsigned int& dpiHorizontal, unsigned int& dpiVertical )
183 const char* environmentDpiHorizontal = std::getenv( DALI_ENV_DPI_HORIZONTAL );
184 dpiHorizontal = environmentDpiHorizontal ? std::atoi( environmentDpiHorizontal ) : 0;
186 const char* environmentDpiVertical = std::getenv( DALI_ENV_DPI_VERTICAL );
187 dpiVertical = environmentDpiVertical ? std::atoi( environmentDpiVertical ) : 0;
189 if( dpiHorizontal == 0 || dpiVertical == 0 )
191 mWindowBase->GetDpi( dpiHorizontal, dpiVertical );
195 int WindowRenderSurface::GetOrientation() const
197 return mWindowBase->GetOrientation();
200 void WindowRenderSurface::InitializeGraphics()
203 mGraphics = &mAdaptor->GetGraphicsInterface();
205 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
206 mEGL = &eglGraphics->GetEglInterface();
208 if ( mEGLContext == NULL )
210 // Create the OpenGL context for this window
211 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>(*mEGL);
212 eglImpl.ChooseConfig(true, mColorDepth);
213 eglImpl.CreateWindowContext( mEGLContext );
215 // Create the OpenGL surface
220 void WindowRenderSurface::CreateSurface()
222 DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
225 if( mScreenRotationAngle == 0 || mScreenRotationAngle == 180 )
227 width = mPositionSize.width;
228 height = mPositionSize.height;
232 width = mPositionSize.height;
233 height = mPositionSize.width;
236 // Create the EGL window
237 EGLNativeWindowType window = mWindowBase->CreateEglWindow( width, height );
239 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
241 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
242 mEGLSurface = eglImpl.CreateSurfaceWindow( window, mColorDepth );
244 // Check rotation capability
245 mRotationSupported = mWindowBase->IsEglWindowRotationSupported();
247 int screenWidth, screenHeight;
248 WindowSystem::GetScreenSize( screenWidth, screenHeight );
249 DALI_LOG_RELEASE_INFO("WindowRenderSurface::CreateSurface: w = %d h = %d screenWidth = %d screenHeight = %d angle = %d screen rotation = %d\n",
250 mPositionSize.width, mPositionSize.height, screenWidth, screenHeight, mRotationAngle, mScreenRotationAngle );
253 void WindowRenderSurface::DestroySurface()
255 DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
257 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
259 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
260 eglImpl.DestroySurface( mEGLSurface );
262 mWindowBase->DestroyEglWindow();
265 bool WindowRenderSurface::ReplaceGraphicsSurface()
267 DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
269 // Destroy the old one
270 mWindowBase->DestroyEglWindow();
273 if( mScreenRotationAngle == 0 || mScreenRotationAngle == 180 )
275 width = mPositionSize.width;
276 height = mPositionSize.height;
280 width = mPositionSize.height;
281 height = mPositionSize.width;
284 // Create the EGL window
285 EGLNativeWindowType window = mWindowBase->CreateEglWindow( width, height );
287 // Set screen rotation
288 mScreenRotationFinished = false;
290 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
292 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
293 return eglImpl.ReplaceSurfaceWindow( window, mEGLSurface, mEGLContext );
296 void WindowRenderSurface::MoveResize( Dali::PositionSize positionSize )
298 bool needToMove = false;
299 bool needToResize = false;
302 if( (fabs(positionSize.x - mPositionSize.x) > MINIMUM_DIMENSION_CHANGE) ||
303 (fabs(positionSize.y - mPositionSize.y) > MINIMUM_DIMENSION_CHANGE) )
309 if( (fabs(positionSize.width - mPositionSize.width) > MINIMUM_DIMENSION_CHANGE) ||
310 (fabs(positionSize.height - mPositionSize.height) > MINIMUM_DIMENSION_CHANGE) )
319 mWindowBase->MoveResize( positionSize );
323 mWindowBase->Resize( positionSize );
326 mResizeFinished = false;
327 mPositionSize = positionSize;
333 mWindowBase->Move( positionSize );
335 mPositionSize = positionSize;
339 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::MoveResize: %d, %d, %d, %d\n", mPositionSize.x, mPositionSize.y, mPositionSize.width, mPositionSize.height );
342 void WindowRenderSurface::StartRender()
346 bool WindowRenderSurface::PreRender( bool resizingSurface )
348 MakeContextCurrent();
350 if( resizingSurface )
352 int totalAngle = (mRotationAngle + mScreenRotationAngle) % 360;
354 // Window rotate or screen rotate
355 if( !mRotationFinished || !mScreenRotationFinished )
357 mWindowBase->SetEglWindowRotation( totalAngle );
358 mWindowBase->SetEglWindowBufferTransform( totalAngle );
360 // Reset only screen rotation flag
361 mScreenRotationFinished = true;
363 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PreRender: Set rotation [%d] [%d]\n", mRotationAngle, mScreenRotationAngle );
366 // Only window rotate
367 if( !mRotationFinished )
369 mWindowBase->SetEglWindowTransform( mRotationAngle );
373 if ( !mResizeFinished )
375 Dali::PositionSize positionSize;
376 positionSize.x = mPositionSize.x;
377 positionSize.y = mPositionSize.y;
378 if( totalAngle == 0 || totalAngle == 180 )
380 positionSize.width = mPositionSize.width;
381 positionSize.height = mPositionSize.height;
385 positionSize.width = mPositionSize.height;
386 positionSize.height = mPositionSize.width;
389 mWindowBase->ResizeEglWindow( positionSize );
390 mResizeFinished = true;
392 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PreRender: Set resize\n" );
396 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
399 GlImplementation& mGLES = eglGraphics->GetGlesInterface();
406 void WindowRenderSurface::PostRender( bool renderToFbo, bool replacingSurface, bool resizingSurface )
408 // Inform the gl implementation that rendering has finished before informing the surface
409 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
412 GlImplementation& mGLES = eglGraphics->GetGlesInterface();
422 if( resizingSurface )
424 if( !mRotationFinished )
426 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PostRender: Trigger rotation event\n" );
428 mRotationTrigger->Trigger();
430 if( mThreadSynchronization )
432 // Wait until the event-thread complete the rotation event processing
433 mThreadSynchronization->PostRenderWaitForCompletion();
439 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
440 eglImpl.SwapBuffers( mEGLSurface );
442 if( mRenderNotification )
444 mRenderNotification->Trigger();
449 void WindowRenderSurface::StopRender()
453 void WindowRenderSurface::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
455 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::SetThreadSynchronization: called\n" );
457 mThreadSynchronization = &threadSynchronization;
460 void WindowRenderSurface::ReleaseLock()
465 Integration::RenderSurface::Type WindowRenderSurface::GetSurfaceType()
467 return RenderSurface::WINDOW_RENDER_SURFACE;
470 void WindowRenderSurface::MakeContextCurrent()
472 if ( mEGL != nullptr )
474 mEGL->MakeContextCurrent( mEGLSurface, mEGLContext );
478 Integration::DepthBufferAvailable WindowRenderSurface::GetDepthBufferRequired()
480 return mGraphics ? mGraphics->GetDepthBufferRequired() : Integration::DepthBufferAvailable::FALSE;
483 Integration::StencilBufferAvailable WindowRenderSurface::GetStencilBufferRequired()
485 return mGraphics ? mGraphics->GetStencilBufferRequired() : Integration::StencilBufferAvailable::FALSE;
488 void WindowRenderSurface::OutputTransformed()
490 int screenRotationAngle = mWindowBase->GetScreenRotationAngle();
492 if( mScreenRotationAngle != screenRotationAngle )
494 mScreenRotationAngle = screenRotationAngle;
495 mScreenRotationFinished = false;
496 mResizeFinished = false;
498 mOutputTransformedSignal.Emit();
500 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::OutputTransformed: angle = %d screen rotation = %d\n", mRotationAngle, mScreenRotationAngle );
504 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::OutputTransformed: Ignore output transform [%d]\n", mScreenRotationAngle );
508 void WindowRenderSurface::ProcessRotationRequest()
510 mRotationFinished = true;
512 mWindowBase->WindowRotationCompleted( mRotationAngle, mPositionSize.width, mPositionSize.height );
514 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::ProcessRotationRequest: Rotation Done\n" );
516 if( mThreadSynchronization )
518 mThreadSynchronization->PostRenderComplete();
522 } // namespace Adaptor
524 } // namespace internal