2 * Copyright (c) 2020 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
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
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;
121 Any WindowRenderSurface::GetNativeWindow()
123 return mWindowBase->GetNativeWindow();
126 int WindowRenderSurface::GetNativeWindowId()
128 return mWindowBase->GetNativeWindowId();
131 void WindowRenderSurface::Map()
136 void WindowRenderSurface::SetRenderNotification( TriggerEventInterface* renderNotification )
138 mRenderNotification = renderNotification;
141 void WindowRenderSurface::SetTransparency( bool transparent )
143 mWindowBase->SetTransparency( transparent );
146 void WindowRenderSurface::RequestRotation( int angle, int width, int height )
148 if( !mRotationSupported )
150 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::Rotate: Rotation is not supported!\n" );
154 if( !mRotationTrigger )
156 mRotationTrigger = TriggerEventFactory::CreateTriggerEvent( MakeCallback( this, &WindowRenderSurface::ProcessRotationRequest ), TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER );
159 mPositionSize.width = width;
160 mPositionSize.height = height;
162 mRotationAngle = angle;
163 mRotationFinished = false;
165 mWindowBase->SetWindowRotationAngle( mRotationAngle );
167 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::Rotate: angle = %d screen rotation = %d\n", mRotationAngle, mScreenRotationAngle );
170 WindowBase* WindowRenderSurface::GetWindowBase()
172 return mWindowBase.get();
175 WindowBase::OutputSignalType& WindowRenderSurface::OutputTransformedSignal()
177 return mOutputTransformedSignal;
180 PositionSize WindowRenderSurface::GetPositionSize() const
182 return mPositionSize;
185 void WindowRenderSurface::GetDpi( unsigned int& dpiHorizontal, unsigned int& dpiVertical )
187 if( mDpiHorizontal == 0 || mDpiVertical == 0 )
189 const char* environmentDpiHorizontal = std::getenv( DALI_ENV_DPI_HORIZONTAL );
190 mDpiHorizontal = environmentDpiHorizontal ? std::atoi( environmentDpiHorizontal ) : 0;
192 const char* environmentDpiVertical = std::getenv( DALI_ENV_DPI_VERTICAL );
193 mDpiVertical = environmentDpiVertical ? std::atoi( environmentDpiVertical ) : 0;
195 if( mDpiHorizontal == 0 || mDpiVertical == 0 )
197 mWindowBase->GetDpi( mDpiHorizontal, mDpiVertical );
201 dpiHorizontal = mDpiHorizontal;
202 dpiVertical = mDpiVertical;
205 void WindowRenderSurface::InitializeGraphics()
207 mGraphics = &mAdaptor->GetGraphicsInterface();
209 DALI_ASSERT_ALWAYS( mGraphics && "Graphics interface is not created" );
211 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
212 mEGL = &eglGraphics->GetEglInterface();
214 if ( mEGLContext == NULL )
216 // Create the OpenGL context for this window
217 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>(*mEGL);
218 eglImpl.ChooseConfig(true, mColorDepth);
219 eglImpl.CreateWindowContext( mEGLContext );
221 // Create the OpenGL surface
226 void WindowRenderSurface::CreateSurface()
228 DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
231 if( mScreenRotationAngle == 0 || mScreenRotationAngle == 180 )
233 width = mPositionSize.width;
234 height = mPositionSize.height;
238 width = mPositionSize.height;
239 height = mPositionSize.width;
242 // Create the EGL window
243 EGLNativeWindowType window = mWindowBase->CreateEglWindow( width, height );
245 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
247 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
248 mEGLSurface = eglImpl.CreateSurfaceWindow( window, mColorDepth );
250 // Check rotation capability
251 mRotationSupported = mWindowBase->IsEglWindowRotationSupported();
253 DALI_LOG_RELEASE_INFO("WindowRenderSurface::CreateSurface: WinId (%d), w = %d h = %d angle = %d screen rotation = %d\n",
254 mWindowBase->GetNativeWindowId(), mPositionSize.width, mPositionSize.height, mRotationAngle, mScreenRotationAngle );
257 void WindowRenderSurface::DestroySurface()
259 DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
261 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
264 DALI_LOG_RELEASE_INFO("WindowRenderSurface::DestroySurface: WinId (%d)\n", mWindowBase->GetNativeWindowId() );
266 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
267 eglImpl.DestroySurface( mEGLSurface );
269 mWindowBase->DestroyEglWindow();
273 bool WindowRenderSurface::ReplaceGraphicsSurface()
275 DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
277 // Destroy the old one
278 mWindowBase->DestroyEglWindow();
281 if( mScreenRotationAngle == 0 || mScreenRotationAngle == 180 )
283 width = mPositionSize.width;
284 height = mPositionSize.height;
288 width = mPositionSize.height;
289 height = mPositionSize.width;
292 // Create the EGL window
293 EGLNativeWindowType window = mWindowBase->CreateEglWindow( width, height );
295 // Set screen rotation
296 mScreenRotationFinished = false;
298 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
300 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
301 return eglImpl.ReplaceSurfaceWindow( window, mEGLSurface, mEGLContext );
304 void WindowRenderSurface::MoveResize( Dali::PositionSize positionSize )
306 bool needToMove = false;
307 bool needToResize = false;
310 if( (fabs(positionSize.x - mPositionSize.x) > MINIMUM_DIMENSION_CHANGE) ||
311 (fabs(positionSize.y - mPositionSize.y) > MINIMUM_DIMENSION_CHANGE) )
317 if( (fabs(positionSize.width - mPositionSize.width) > MINIMUM_DIMENSION_CHANGE) ||
318 (fabs(positionSize.height - mPositionSize.height) > MINIMUM_DIMENSION_CHANGE) )
327 mWindowBase->MoveResize( positionSize );
331 mWindowBase->Resize( positionSize );
334 mResizeFinished = false;
335 mPositionSize = positionSize;
341 mWindowBase->Move( positionSize );
343 mPositionSize = positionSize;
347 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::MoveResize: %d, %d, %d, %d\n", mPositionSize.x, mPositionSize.y, mPositionSize.width, mPositionSize.height );
350 void WindowRenderSurface::StartRender()
354 bool WindowRenderSurface::PreRender( bool resizingSurface )
356 MakeContextCurrent();
358 if( resizingSurface )
360 // Window rotate or screen rotate
361 if( !mRotationFinished || !mScreenRotationFinished )
363 int totalAngle = (mRotationAngle + mScreenRotationAngle) % 360;
365 mWindowBase->SetEglWindowRotation( totalAngle );
366 mWindowBase->SetEglWindowBufferTransform( totalAngle );
368 // Reset only screen rotation flag
369 mScreenRotationFinished = true;
371 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PreRender: Set rotation [%d] [%d]\n", mRotationAngle, mScreenRotationAngle );
374 // Only window rotate
375 if( !mRotationFinished )
377 mWindowBase->SetEglWindowTransform( mRotationAngle );
381 if( !mResizeFinished )
383 mWindowBase->ResizeEglWindow( mPositionSize );
384 mResizeFinished = true;
386 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PreRender: Set resize\n" );
390 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
393 GlImplementation& mGLES = eglGraphics->GetGlesInterface();
400 void WindowRenderSurface::PostRender( bool renderToFbo, bool replacingSurface, bool resizingSurface )
402 // Inform the gl implementation that rendering has finished before informing the surface
403 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
406 GlImplementation& mGLES = eglGraphics->GetGlesInterface();
416 if( resizingSurface )
418 if( !mRotationFinished )
420 if( mThreadSynchronization )
422 // Enable PostRender flag
423 mThreadSynchronization->PostRenderStarted();
426 DALI_LOG_RELEASE_INFO("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 Dali::RenderSurfaceInterface::Type WindowRenderSurface::GetSurfaceType()
467 return Dali::RenderSurfaceInterface::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;
497 mOutputTransformedSignal.Emit();
499 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::OutputTransformed: angle = %d screen rotation = %d\n", mRotationAngle, mScreenRotationAngle );
503 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::OutputTransformed: Ignore output transform [%d]\n", mScreenRotationAngle );
507 void WindowRenderSurface::ProcessRotationRequest()
509 mRotationFinished = true;
511 mWindowBase->WindowRotationCompleted( mRotationAngle, mPositionSize.width, mPositionSize.height );
513 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::ProcessRotationRequest: Rotation Done\n" );
515 if( mThreadSynchronization )
517 mThreadSynchronization->PostRenderComplete();
521 } // namespace Adaptor
523 } // namespace internal