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
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 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 void WindowRenderSurface::InitializeGraphics()
209 mGraphics = &mAdaptor->GetGraphicsInterface();
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);
263 DALI_LOG_RELEASE_INFO("WindowRenderSurface::DestroySurface: WinId (%d)\n", mWindowBase->GetNativeWindowId() );
265 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
266 eglImpl.DestroySurface( mEGLSurface );
268 mWindowBase->DestroyEglWindow();
271 bool WindowRenderSurface::ReplaceGraphicsSurface()
273 DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
275 // Destroy the old one
276 mWindowBase->DestroyEglWindow();
279 if( mScreenRotationAngle == 0 || mScreenRotationAngle == 180 )
281 width = mPositionSize.width;
282 height = mPositionSize.height;
286 width = mPositionSize.height;
287 height = mPositionSize.width;
290 // Create the EGL window
291 EGLNativeWindowType window = mWindowBase->CreateEglWindow( width, height );
293 // Set screen rotation
294 mScreenRotationFinished = false;
296 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
298 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
299 return eglImpl.ReplaceSurfaceWindow( window, mEGLSurface, mEGLContext );
302 void WindowRenderSurface::MoveResize( Dali::PositionSize positionSize )
304 bool needToMove = false;
305 bool needToResize = false;
308 if( (fabs(positionSize.x - mPositionSize.x) > MINIMUM_DIMENSION_CHANGE) ||
309 (fabs(positionSize.y - mPositionSize.y) > MINIMUM_DIMENSION_CHANGE) )
315 if( (fabs(positionSize.width - mPositionSize.width) > MINIMUM_DIMENSION_CHANGE) ||
316 (fabs(positionSize.height - mPositionSize.height) > MINIMUM_DIMENSION_CHANGE) )
325 mWindowBase->MoveResize( positionSize );
329 mWindowBase->Resize( positionSize );
332 mResizeFinished = false;
333 mPositionSize = positionSize;
339 mWindowBase->Move( positionSize );
341 mPositionSize = positionSize;
345 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::MoveResize: %d, %d, %d, %d\n", mPositionSize.x, mPositionSize.y, mPositionSize.width, mPositionSize.height );
348 void WindowRenderSurface::StartRender()
352 bool WindowRenderSurface::PreRender( bool resizingSurface )
354 MakeContextCurrent();
356 if( resizingSurface )
358 // Window rotate or screen rotate
359 if( !mRotationFinished || !mScreenRotationFinished )
361 int totalAngle = (mRotationAngle + mScreenRotationAngle) % 360;
363 mWindowBase->SetEglWindowRotation( totalAngle );
364 mWindowBase->SetEglWindowBufferTransform( totalAngle );
366 // Reset only screen rotation flag
367 mScreenRotationFinished = true;
369 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PreRender: Set rotation [%d] [%d]\n", mRotationAngle, mScreenRotationAngle );
372 // Only window rotate
373 if( !mRotationFinished )
375 mWindowBase->SetEglWindowTransform( mRotationAngle );
379 if( !mResizeFinished )
381 mWindowBase->ResizeEglWindow( mPositionSize );
382 mResizeFinished = true;
384 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PreRender: Set resize\n" );
388 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
391 GlImplementation& mGLES = eglGraphics->GetGlesInterface();
398 void WindowRenderSurface::PostRender( bool renderToFbo, bool replacingSurface, bool resizingSurface )
400 // Inform the gl implementation that rendering has finished before informing the surface
401 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
404 GlImplementation& mGLES = eglGraphics->GetGlesInterface();
414 if( resizingSurface )
416 if( !mRotationFinished )
418 if( mThreadSynchronization )
420 // Enable PostRender flag
421 mThreadSynchronization->PostRenderStarted();
424 DALI_LOG_RELEASE_INFO("WindowRenderSurface::PostRender: Trigger rotation event\n" );
426 mRotationTrigger->Trigger();
428 if( mThreadSynchronization )
430 // Wait until the event-thread complete the rotation event processing
431 mThreadSynchronization->PostRenderWaitForCompletion();
437 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
438 eglImpl.SwapBuffers( mEGLSurface );
440 if( mRenderNotification )
442 mRenderNotification->Trigger();
447 void WindowRenderSurface::StopRender()
451 void WindowRenderSurface::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
453 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::SetThreadSynchronization: called\n" );
455 mThreadSynchronization = &threadSynchronization;
458 void WindowRenderSurface::ReleaseLock()
463 Integration::RenderSurface::Type WindowRenderSurface::GetSurfaceType()
465 return RenderSurface::WINDOW_RENDER_SURFACE;
468 void WindowRenderSurface::MakeContextCurrent()
470 if ( mEGL != nullptr )
472 mEGL->MakeContextCurrent( mEGLSurface, mEGLContext );
476 Integration::DepthBufferAvailable WindowRenderSurface::GetDepthBufferRequired()
478 return mGraphics ? mGraphics->GetDepthBufferRequired() : Integration::DepthBufferAvailable::FALSE;
481 Integration::StencilBufferAvailable WindowRenderSurface::GetStencilBufferRequired()
483 return mGraphics ? mGraphics->GetStencilBufferRequired() : Integration::StencilBufferAvailable::FALSE;
486 void WindowRenderSurface::OutputTransformed()
488 int screenRotationAngle = mWindowBase->GetScreenRotationAngle();
490 if( mScreenRotationAngle != screenRotationAngle )
492 mScreenRotationAngle = screenRotationAngle;
493 mScreenRotationFinished = false;
495 mOutputTransformedSignal.Emit();
497 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::OutputTransformed: angle = %d screen rotation = %d\n", mRotationAngle, mScreenRotationAngle );
501 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::OutputTransformed: Ignore output transform [%d]\n", mScreenRotationAngle );
505 void WindowRenderSurface::ProcessRotationRequest()
507 mRotationFinished = true;
509 mWindowBase->WindowRotationCompleted( mRotationAngle, mPositionSize.width, mPositionSize.height );
511 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::ProcessRotationRequest: Rotation Done\n" );
513 if( mThreadSynchronization )
515 mThreadSynchronization->PostRenderComplete();
519 } // namespace Adaptor
521 } // namespace internal