2 * Copyright (c) 2018 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;
88 void WindowRenderSurface::Initialize( Any surface )
90 // If width or height are zero, go full screen.
91 if ( (mPositionSize.width == 0) || (mPositionSize.height == 0) )
93 // Default window size == screen size
97 WindowSystem::GetScreenSize( mPositionSize.width, mPositionSize.height );
100 // Create a window base
101 auto windowFactory = Dali::Internal::Adaptor::GetWindowFactory();
102 mWindowBase = windowFactory->CreateWindowBase( mPositionSize, surface, ( mColorDepth == COLOR_DEPTH_32 ? true : false ) );
105 mWindowBase->OutputTransformedSignal().Connect( this, &WindowRenderSurface::OutputTransformed );
107 // Check screen rotation
108 mScreenRotationAngle = mWindowBase->GetScreenRotationAngle();
109 if( mScreenRotationAngle != 0 )
111 mScreenRotationFinished = false;
115 Any WindowRenderSurface::GetNativeWindow()
117 return mWindowBase->GetNativeWindow();
120 int WindowRenderSurface::GetNativeWindowId()
122 return mWindowBase->GetNativeWindowId();
125 void WindowRenderSurface::Map()
130 void WindowRenderSurface::SetRenderNotification( TriggerEventInterface* renderNotification )
132 mRenderNotification = renderNotification;
135 void WindowRenderSurface::SetTransparency( bool transparent )
137 mWindowBase->SetTransparency( transparent );
140 void WindowRenderSurface::RequestRotation( int angle, int width, int height )
142 if( !mRotationSupported )
144 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::Rotate: Rotation is not supported!\n" );
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 mWindowBase->GetDpi( dpiHorizontal, dpiVertical );
185 void WindowRenderSurface::InitializeGraphics()
188 mGraphics = &mAdaptor->GetGraphicsInterface();
190 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
191 mEGL = &eglGraphics->GetEglInterface();
193 if ( mEGLContext == NULL )
195 // Create the OpenGL context for this window
196 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>(*mEGL);
197 eglImpl.ChooseConfig(true, mColorDepth);
198 eglImpl.CreateWindowContext( mEGLContext );
200 // Create the OpenGL surface
205 void WindowRenderSurface::CreateSurface()
207 DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
210 if( mScreenRotationAngle == 0 || mScreenRotationAngle == 180 )
212 width = mPositionSize.width;
213 height = mPositionSize.height;
217 width = mPositionSize.height;
218 height = mPositionSize.width;
221 // Create the EGL window
222 EGLNativeWindowType window = mWindowBase->CreateEglWindow( width, height );
224 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
226 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
227 mEGLSurface = eglImpl.CreateSurfaceWindow( window, mColorDepth );
229 // Check rotation capability
230 mRotationSupported = mWindowBase->IsEglWindowRotationSupported();
232 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::CreateSurface: w = %d h = %d angle = %d screen rotation = %d\n", mPositionSize.width, mPositionSize.height, mRotationAngle, mScreenRotationAngle );
235 void WindowRenderSurface::DestroySurface()
237 DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
239 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
241 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
242 eglImpl.DestroySurface( mEGLSurface );
244 mWindowBase->DestroyEglWindow();
247 bool WindowRenderSurface::ReplaceGraphicsSurface()
249 DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
251 // Destroy the old one
252 mWindowBase->DestroyEglWindow();
255 if( mScreenRotationAngle == 0 || mScreenRotationAngle == 180 )
257 width = mPositionSize.width;
258 height = mPositionSize.height;
262 width = mPositionSize.height;
263 height = mPositionSize.width;
266 // Create the EGL window
267 EGLNativeWindowType window = mWindowBase->CreateEglWindow( width, height );
269 // Set screen rotation
270 mScreenRotationFinished = false;
272 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
274 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
275 return eglImpl.ReplaceSurfaceWindow( window, mEGLSurface, mEGLContext );
278 void WindowRenderSurface::MoveResize( Dali::PositionSize positionSize )
280 bool needToMove = false;
281 bool needToResize = false;
284 if( (fabs(positionSize.x - mPositionSize.x) > MINIMUM_DIMENSION_CHANGE) ||
285 (fabs(positionSize.y - mPositionSize.y) > MINIMUM_DIMENSION_CHANGE) )
291 if( (fabs(positionSize.width - mPositionSize.width) > MINIMUM_DIMENSION_CHANGE) ||
292 (fabs(positionSize.height - mPositionSize.height) > MINIMUM_DIMENSION_CHANGE) )
301 mWindowBase->MoveResize( positionSize );
305 mWindowBase->Resize( positionSize );
308 mResizeFinished = false;
309 mPositionSize = positionSize;
315 mWindowBase->Move( positionSize );
317 mPositionSize = positionSize;
321 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::MoveResize: %d, %d, %d, %d\n", mPositionSize.x, mPositionSize.y, mPositionSize.width, mPositionSize.height );
324 void WindowRenderSurface::StartRender()
328 bool WindowRenderSurface::PreRender( bool resizingSurface )
330 MakeContextCurrent();
332 if( resizingSurface )
334 #ifdef OVER_TIZEN_VERSION_4
335 // Window rotate or screen rotate
336 if( !mRotationFinished || !mScreenRotationFinished )
338 int totalAngle = (mRotationAngle + mScreenRotationAngle) % 360;
340 mWindowBase->SetEglWindowRotation( totalAngle );
341 mWindowBase->SetEglWindowBufferTransform( totalAngle );
343 // Reset only screen rotation flag
344 mScreenRotationFinished = true;
346 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PreRender: Set rotation [%d] [%d]\n", mRotationAngle, mScreenRotationAngle );
349 // Only window rotate
350 if( !mRotationFinished )
352 mWindowBase->SetEglWindowTransform( mRotationAngle );
357 if( !mResizeFinished )
359 mWindowBase->ResizeEglWindow( mPositionSize );
360 mResizeFinished = true;
362 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PreRender: Set resize\n" );
366 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
369 GlImplementation& mGLES = eglGraphics->GetGlesInterface();
376 void WindowRenderSurface::PostRender( bool renderToFbo, bool replacingSurface, bool resizingSurface )
378 // Inform the gl implementation that rendering has finished before informing the surface
379 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
382 GlImplementation& mGLES = eglGraphics->GetGlesInterface();
392 if( resizingSurface )
394 if( !mRotationFinished )
396 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PostRender: Trigger rotation event\n" );
398 mRotationTrigger->Trigger();
400 if( mThreadSynchronization )
402 // Wait until the event-thread complete the rotation event processing
403 mThreadSynchronization->PostRenderWaitForCompletion();
409 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
410 eglImpl.SwapBuffers( mEGLSurface );
412 if( mRenderNotification )
414 mRenderNotification->Trigger();
419 void WindowRenderSurface::StopRender()
423 void WindowRenderSurface::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
425 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::SetThreadSynchronization: called\n" );
427 mThreadSynchronization = &threadSynchronization;
430 void WindowRenderSurface::ReleaseLock()
435 Integration::RenderSurface::Type WindowRenderSurface::GetSurfaceType()
437 return RenderSurface::WINDOW_RENDER_SURFACE;
440 void WindowRenderSurface::MakeContextCurrent()
442 if ( mEGL != nullptr )
444 mEGL->MakeContextCurrent( mEGLSurface, mEGLContext );
448 Integration::DepthBufferAvailable WindowRenderSurface::GetDepthBufferRequired()
450 return mGraphics ? mGraphics->GetDepthBufferRequired() : Integration::DepthBufferAvailable::FALSE;
453 Integration::StencilBufferAvailable WindowRenderSurface::GetStencilBufferRequired()
455 return mGraphics ? mGraphics->GetStencilBufferRequired() : Integration::StencilBufferAvailable::FALSE;
458 void WindowRenderSurface::OutputTransformed()
460 int screenRotationAngle = mWindowBase->GetScreenRotationAngle();
462 if( mScreenRotationAngle != screenRotationAngle )
464 mScreenRotationAngle = screenRotationAngle;
465 mScreenRotationFinished = false;
467 mOutputTransformedSignal.Emit();
469 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::OutputTransformed: angle = %d screen rotation = %d\n", mRotationAngle, mScreenRotationAngle );
473 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::OutputTransformed: Ignore output transform [%d]\n", mScreenRotationAngle );
477 void WindowRenderSurface::ProcessRotationRequest()
479 mRotationFinished = true;
481 mWindowBase->WindowRotationCompleted( mRotationAngle, mPositionSize.width, mPositionSize.height );
483 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::ProcessRotationRequest: Rotation Done\n" );
485 if( mThreadSynchronization )
487 mThreadSynchronization->PostRenderComplete();
491 } // namespace Adaptor
493 } // namespace internal