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>
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 )
76 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "Creating Window\n" );
77 Initialize( surface );
80 WindowRenderSurface::~WindowRenderSurface()
82 if( mRotationTrigger )
84 delete mRotationTrigger;
93 void WindowRenderSurface::Initialize( Any surface )
95 // If width or height are zero, go full screen.
96 if ( (mPositionSize.width == 0) || (mPositionSize.height == 0) )
98 // Default window size == screen size
102 WindowSystem::GetScreenSize( mPositionSize.width, mPositionSize.height );
105 // Create a window base
106 auto windowFactory = Dali::Internal::Adaptor::GetWindowFactory();
107 mWindowBase = windowFactory->CreateWindowBase( mPositionSize, surface, ( mColorDepth == COLOR_DEPTH_32 ? true : false ) );
110 mWindowBase->OutputTransformedSignal().Connect( this, &WindowRenderSurface::OutputTransformed );
112 // Check screen rotation
113 mScreenRotationAngle = mWindowBase->GetScreenRotationAngle();
114 if( mScreenRotationAngle != 0 )
116 mScreenRotationFinished = false;
117 mResizeFinished = 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( !mRotationTrigger )
150 TriggerEventFactoryInterface& triggerFactory = Internal::Adaptor::Adaptor::GetImplementation( Adaptor::Get() ).GetTriggerEventFactoryInterface();
151 mRotationTrigger = triggerFactory.CreateTriggerEvent( MakeCallback( this, &WindowRenderSurface::ProcessRotationRequest ), TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER );
154 mPositionSize.width = width;
155 mPositionSize.height = height;
157 mRotationAngle = angle;
158 mRotationFinished = false;
160 mWindowBase->SetWindowRotationAngle( mRotationAngle );
162 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::Rotate: angle = %d screen rotation = %d\n", mRotationAngle, mScreenRotationAngle );
165 WindowBase* WindowRenderSurface::GetWindowBase()
167 return mWindowBase.get();
170 WindowBase::OutputSignalType& WindowRenderSurface::OutputTransformedSignal()
172 return mOutputTransformedSignal;
175 PositionSize WindowRenderSurface::GetPositionSize() const
177 return mPositionSize;
180 void WindowRenderSurface::GetDpi( unsigned int& dpiHorizontal, unsigned int& dpiVertical )
182 const char* environmentDpiX = std::getenv("DALI_ENV_DPI_HORIZONTAL");
183 dpiHorizontal = environmentDpiX ? std::atoi(environmentDpiX) : 0;
185 const char* environmentDpiY = std::getenv("DALI_ENV_DPI_VERTICAL");
186 dpiVertical = environmentDpiY ? std::atoi(environmentDpiY) : 0;
188 if( dpiHorizontal == 0 || dpiVertical == 0 )
190 mWindowBase->GetDpi( dpiHorizontal, dpiVertical );
194 int WindowRenderSurface::GetOrientation() const
196 return mWindowBase->GetOrientation();
199 void WindowRenderSurface::InitializeGraphics()
202 mGraphics = &mAdaptor->GetGraphicsInterface();
204 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
205 mEGL = &eglGraphics->GetEglInterface();
207 if ( mEGLContext == NULL )
209 // Create the OpenGL context for this window
210 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>(*mEGL);
211 eglImpl.ChooseConfig(true, mColorDepth);
212 eglImpl.CreateWindowContext( mEGLContext );
214 // Create the OpenGL surface
219 void WindowRenderSurface::CreateSurface()
221 DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
224 if( mScreenRotationAngle == 0 || mScreenRotationAngle == 180 )
226 width = mPositionSize.width;
227 height = mPositionSize.height;
231 width = mPositionSize.height;
232 height = mPositionSize.width;
235 // Create the EGL window
236 EGLNativeWindowType window = mWindowBase->CreateEglWindow( width, height );
238 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
240 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
241 mEGLSurface = eglImpl.CreateSurfaceWindow( window, mColorDepth );
243 // Check rotation capability
244 mRotationSupported = mWindowBase->IsEglWindowRotationSupported();
246 int screenWidth, screenHeight;
247 WindowSystem::GetScreenSize( screenWidth, screenHeight );
248 DALI_LOG_RELEASE_INFO("WindowRenderSurface::CreateSurface: w = %d h = %d screenWidth = %d screenHeight = %d angle = %d screen rotation = %d\n",
249 mPositionSize.width, mPositionSize.height, screenWidth, screenHeight, mRotationAngle, mScreenRotationAngle );
252 void WindowRenderSurface::DestroySurface()
254 DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
256 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
258 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
259 eglImpl.DestroySurface( mEGLSurface );
261 mWindowBase->DestroyEglWindow();
264 bool WindowRenderSurface::ReplaceGraphicsSurface()
266 DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
268 // Destroy the old one
269 mWindowBase->DestroyEglWindow();
272 if( mScreenRotationAngle == 0 || mScreenRotationAngle == 180 )
274 width = mPositionSize.width;
275 height = mPositionSize.height;
279 width = mPositionSize.height;
280 height = mPositionSize.width;
283 // Create the EGL window
284 EGLNativeWindowType window = mWindowBase->CreateEglWindow( width, height );
286 // Set screen rotation
287 mScreenRotationFinished = false;
289 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
291 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
292 return eglImpl.ReplaceSurfaceWindow( window, mEGLSurface, mEGLContext );
295 void WindowRenderSurface::MoveResize( Dali::PositionSize positionSize )
297 bool needToMove = false;
298 bool needToResize = false;
301 if( (fabs(positionSize.x - mPositionSize.x) > MINIMUM_DIMENSION_CHANGE) ||
302 (fabs(positionSize.y - mPositionSize.y) > MINIMUM_DIMENSION_CHANGE) )
308 if( (fabs(positionSize.width - mPositionSize.width) > MINIMUM_DIMENSION_CHANGE) ||
309 (fabs(positionSize.height - mPositionSize.height) > MINIMUM_DIMENSION_CHANGE) )
318 mWindowBase->MoveResize( positionSize );
322 mWindowBase->Resize( positionSize );
325 mResizeFinished = false;
326 mPositionSize = positionSize;
332 mWindowBase->Move( positionSize );
334 mPositionSize = positionSize;
338 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::MoveResize: %d, %d, %d, %d\n", mPositionSize.x, mPositionSize.y, mPositionSize.width, mPositionSize.height );
341 void WindowRenderSurface::StartRender()
345 bool WindowRenderSurface::PreRender( bool resizingSurface )
347 MakeContextCurrent();
349 if( resizingSurface )
351 int totalAngle = (mRotationAngle + mScreenRotationAngle) % 360;
353 // Window rotate or screen rotate
354 if( !mRotationFinished || !mScreenRotationFinished )
356 mWindowBase->SetEglWindowRotation( totalAngle );
357 mWindowBase->SetEglWindowBufferTransform( totalAngle );
359 // Reset only screen rotation flag
360 mScreenRotationFinished = true;
362 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PreRender: Set rotation [%d] [%d]\n", mRotationAngle, mScreenRotationAngle );
365 // Only window rotate
366 if( !mRotationFinished )
368 mWindowBase->SetEglWindowTransform( mRotationAngle );
372 if ( !mResizeFinished )
374 Dali::PositionSize positionSize;
375 positionSize.x = mPositionSize.x;
376 positionSize.y = mPositionSize.y;
377 if( totalAngle == 0 || totalAngle == 180 )
379 positionSize.width = mPositionSize.width;
380 positionSize.height = mPositionSize.height;
384 positionSize.width = mPositionSize.height;
385 positionSize.height = mPositionSize.width;
388 mWindowBase->ResizeEglWindow( positionSize );
389 mResizeFinished = true;
391 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PreRender: Set resize\n" );
395 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
398 GlImplementation& mGLES = eglGraphics->GetGlesInterface();
405 void WindowRenderSurface::PostRender( bool renderToFbo, bool replacingSurface, bool resizingSurface )
407 // Inform the gl implementation that rendering has finished before informing the surface
408 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
411 GlImplementation& mGLES = eglGraphics->GetGlesInterface();
421 if( resizingSurface )
423 if( !mRotationFinished )
425 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PostRender: Trigger rotation event\n" );
427 mRotationTrigger->Trigger();
429 if( mThreadSynchronization )
431 // Wait until the event-thread complete the rotation event processing
432 mThreadSynchronization->PostRenderWaitForCompletion();
438 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
439 eglImpl.SwapBuffers( mEGLSurface );
441 if( mRenderNotification )
443 mRenderNotification->Trigger();
448 void WindowRenderSurface::StopRender()
452 void WindowRenderSurface::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
454 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::SetThreadSynchronization: called\n" );
456 mThreadSynchronization = &threadSynchronization;
459 void WindowRenderSurface::ReleaseLock()
464 Integration::RenderSurface::Type WindowRenderSurface::GetSurfaceType()
466 return RenderSurface::WINDOW_RENDER_SURFACE;
469 void WindowRenderSurface::MakeContextCurrent()
471 if ( mEGL != nullptr )
473 mEGL->MakeContextCurrent( mEGLSurface, mEGLContext );
477 Integration::DepthBufferAvailable WindowRenderSurface::GetDepthBufferRequired()
479 return mGraphics ? mGraphics->GetDepthBufferRequired() : Integration::DepthBufferAvailable::FALSE;
482 Integration::StencilBufferAvailable WindowRenderSurface::GetStencilBufferRequired()
484 return mGraphics ? mGraphics->GetStencilBufferRequired() : Integration::StencilBufferAvailable::FALSE;
487 void WindowRenderSurface::OutputTransformed()
489 int screenRotationAngle = mWindowBase->GetScreenRotationAngle();
491 if( mScreenRotationAngle != screenRotationAngle )
493 mScreenRotationAngle = screenRotationAngle;
494 mScreenRotationFinished = false;
495 mResizeFinished = 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