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;
90 void WindowRenderSurface::Initialize( Any surface )
92 // If width or height are zero, go full screen.
93 if ( (mPositionSize.width == 0) || (mPositionSize.height == 0) )
95 // Default window size == screen size
98 WindowSystem::GetScreenSize( mPositionSize.width, mPositionSize.height );
101 // Create a window base
102 auto windowFactory = Dali::Internal::Adaptor::GetWindowFactory();
103 mWindowBase = windowFactory->CreateWindowBase( mPositionSize, surface, ( mColorDepth == COLOR_DEPTH_32 ? true : false ) );
106 mWindowBase->OutputTransformedSignal().Connect( this, &WindowRenderSurface::OutputTransformed );
109 Any WindowRenderSurface::GetNativeWindow()
111 return mWindowBase->GetNativeWindow();
114 int WindowRenderSurface::GetNativeWindowId()
116 return mWindowBase->GetNativeWindowId();
119 void WindowRenderSurface::Map()
124 void WindowRenderSurface::SetRenderNotification( TriggerEventInterface* renderNotification )
126 mRenderNotification = renderNotification;
129 void WindowRenderSurface::SetTransparency( bool transparent )
131 mWindowBase->SetTransparency( transparent );
134 void WindowRenderSurface::RequestRotation( int angle, int width, int height )
136 if( !mRotationTrigger )
138 mRotationTrigger = TriggerEventFactory::CreateTriggerEvent( MakeCallback( this, &WindowRenderSurface::ProcessRotationRequest ), TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER );
141 mPositionSize.width = width;
142 mPositionSize.height = height;
144 mRotationAngle = angle;
145 mRotationFinished = false;
147 mWindowBase->SetWindowRotationAngle( mRotationAngle );
149 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::Rotate: angle = %d screen rotation = %d\n", mRotationAngle, mScreenRotationAngle );
152 WindowBase* WindowRenderSurface::GetWindowBase()
154 return mWindowBase.get();
157 WindowBase::OutputSignalType& WindowRenderSurface::OutputTransformedSignal()
159 return mOutputTransformedSignal;
162 PositionSize WindowRenderSurface::GetPositionSize() const
164 return mPositionSize;
167 void WindowRenderSurface::GetDpi( unsigned int& dpiHorizontal, unsigned int& dpiVertical )
169 if( mDpiHorizontal == 0 || mDpiVertical == 0 )
171 const char* environmentDpiHorizontal = std::getenv( DALI_ENV_DPI_HORIZONTAL );
172 mDpiHorizontal = environmentDpiHorizontal ? std::atoi( environmentDpiHorizontal ) : 0;
174 const char* environmentDpiVertical = std::getenv( DALI_ENV_DPI_VERTICAL );
175 mDpiVertical = environmentDpiVertical ? std::atoi( environmentDpiVertical ) : 0;
177 if( mDpiHorizontal == 0 || mDpiVertical == 0 )
179 mWindowBase->GetDpi( mDpiHorizontal, mDpiVertical );
183 dpiHorizontal = mDpiHorizontal;
184 dpiVertical = mDpiVertical;
187 int WindowRenderSurface::GetOrientation() const
189 return mWindowBase->GetOrientation();
192 void WindowRenderSurface::InitializeGraphics()
194 mGraphics = &mAdaptor->GetGraphicsInterface();
196 DALI_ASSERT_ALWAYS( mGraphics && "Graphics interface is not created" );
198 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
199 mEGL = &eglGraphics->GetEglInterface();
201 if ( mEGLContext == NULL )
203 // Create the OpenGL context for this window
204 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>(*mEGL);
205 eglImpl.ChooseConfig(true, mColorDepth);
206 eglImpl.CreateWindowContext( mEGLContext );
208 // Create the OpenGL surface
213 void WindowRenderSurface::CreateSurface()
215 DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
217 // Create the EGL window
218 EGLNativeWindowType window = mWindowBase->CreateEglWindow( mPositionSize.width, mPositionSize.height );
220 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
222 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
223 mEGLSurface = eglImpl.CreateSurfaceWindow( window, mColorDepth );
225 // Check rotation capability
226 mRotationSupported = mWindowBase->IsEglWindowRotationSupported();
228 DALI_LOG_RELEASE_INFO("WindowRenderSurface::CreateSurface: WinId (%d), w = %d h = %d angle = %d screen rotation = %d\n",
229 mWindowBase->GetNativeWindowId(), mPositionSize.width, mPositionSize.height, mRotationAngle, mScreenRotationAngle );
232 void WindowRenderSurface::DestroySurface()
234 DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
236 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
239 DALI_LOG_RELEASE_INFO("WindowRenderSurface::DestroySurface: WinId (%d)\n", mWindowBase->GetNativeWindowId() );
241 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
243 eglImpl.DestroySurface( mEGLSurface );
244 mEGLSurface = nullptr;
246 // Destroy context also
247 eglImpl.DestroyContext( mEGLContext );
248 mEGLContext = nullptr;
250 mWindowBase->DestroyEglWindow();
254 bool WindowRenderSurface::ReplaceGraphicsSurface()
256 DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
258 // Destroy the old one
259 mWindowBase->DestroyEglWindow();
262 if( mScreenRotationAngle == 0 || mScreenRotationAngle == 180 )
264 width = mPositionSize.width;
265 height = mPositionSize.height;
269 width = mPositionSize.height;
270 height = mPositionSize.width;
273 // Create the EGL window
274 EGLNativeWindowType window = mWindowBase->CreateEglWindow( width, height );
276 // Set screen rotation
277 mScreenRotationFinished = false;
279 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
281 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
282 return eglImpl.ReplaceSurfaceWindow( window, mEGLSurface, mEGLContext );
285 void WindowRenderSurface::MoveResize( Dali::PositionSize positionSize )
287 bool needToMove = false;
288 bool needToResize = false;
291 if( (fabs(positionSize.x - mPositionSize.x) > MINIMUM_DIMENSION_CHANGE) ||
292 (fabs(positionSize.y - mPositionSize.y) > MINIMUM_DIMENSION_CHANGE) )
298 if( (fabs(positionSize.width - mPositionSize.width) > MINIMUM_DIMENSION_CHANGE) ||
299 (fabs(positionSize.height - mPositionSize.height) > MINIMUM_DIMENSION_CHANGE) )
308 mWindowBase->MoveResize( positionSize );
312 mWindowBase->Resize( positionSize );
315 mResizeFinished = false;
316 mPositionSize = positionSize;
322 mWindowBase->Move( positionSize );
324 mPositionSize = positionSize;
328 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::MoveResize: %d, %d, %d, %d\n", mPositionSize.x, mPositionSize.y, mPositionSize.width, mPositionSize.height );
331 void WindowRenderSurface::StartRender()
335 bool WindowRenderSurface::PreRender( bool resizingSurface )
337 MakeContextCurrent();
339 if( resizingSurface )
341 int totalAngle = (mRotationAngle + mScreenRotationAngle) % 360;
343 // Window rotate or screen rotate
344 if( !mRotationFinished || !mScreenRotationFinished )
346 mWindowBase->SetEglWindowRotation( totalAngle );
347 mWindowBase->SetEglWindowBufferTransform( totalAngle );
349 // Reset only screen rotation flag
350 mScreenRotationFinished = true;
352 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PreRender: Set rotation [%d] [%d]\n", mRotationAngle, mScreenRotationAngle );
355 // Only window rotate
356 if( !mRotationFinished )
358 mWindowBase->SetEglWindowTransform( mRotationAngle );
362 if ( !mResizeFinished )
364 Dali::PositionSize positionSize;
365 positionSize.x = mPositionSize.x;
366 positionSize.y = mPositionSize.y;
367 if( totalAngle == 0 || totalAngle == 180 )
369 positionSize.width = mPositionSize.width;
370 positionSize.height = mPositionSize.height;
374 positionSize.width = mPositionSize.height;
375 positionSize.height = mPositionSize.width;
377 mWindowBase->ResizeEglWindow( positionSize );
378 mResizeFinished = true;
380 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PreRender: Set resize\n" );
384 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
387 GlImplementation& mGLES = eglGraphics->GetGlesInterface();
394 void WindowRenderSurface::PostRender( bool renderToFbo, bool replacingSurface, bool resizingSurface )
396 // Inform the gl implementation that rendering has finished before informing the surface
397 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
400 GlImplementation& mGLES = eglGraphics->GetGlesInterface();
410 if( resizingSurface )
412 if( !mRotationFinished )
414 if( mThreadSynchronization )
416 // Enable PostRender flag
417 mThreadSynchronization->PostRenderStarted();
420 DALI_LOG_RELEASE_INFO("WindowRenderSurface::PostRender: Trigger rotation event\n" );
422 mRotationTrigger->Trigger();
424 if( mThreadSynchronization )
426 // Wait until the event-thread complete the rotation event processing
427 mThreadSynchronization->PostRenderWaitForCompletion();
433 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
434 eglImpl.SwapBuffers( mEGLSurface );
436 if( mRenderNotification )
438 mRenderNotification->Trigger();
443 void WindowRenderSurface::StopRender()
447 void WindowRenderSurface::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
449 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::SetThreadSynchronization: called\n" );
451 mThreadSynchronization = &threadSynchronization;
454 void WindowRenderSurface::ReleaseLock()
459 Dali::RenderSurfaceInterface::Type WindowRenderSurface::GetSurfaceType()
461 return Dali::RenderSurfaceInterface::WINDOW_RENDER_SURFACE;
464 void WindowRenderSurface::MakeContextCurrent()
466 if ( mEGL != nullptr )
468 mEGL->MakeContextCurrent( mEGLSurface, mEGLContext );
472 Integration::DepthBufferAvailable WindowRenderSurface::GetDepthBufferRequired()
474 return mGraphics ? mGraphics->GetDepthBufferRequired() : Integration::DepthBufferAvailable::FALSE;
477 Integration::StencilBufferAvailable WindowRenderSurface::GetStencilBufferRequired()
479 return mGraphics ? mGraphics->GetStencilBufferRequired() : Integration::StencilBufferAvailable::FALSE;
482 void WindowRenderSurface::OutputTransformed()
484 int screenRotationAngle = mWindowBase->GetScreenRotationAngle();
486 if( mScreenRotationAngle != screenRotationAngle )
488 mScreenRotationAngle = screenRotationAngle;
489 mScreenRotationFinished = false;
490 mResizeFinished = false;
492 mOutputTransformedSignal.Emit();
494 DALI_LOG_RELEASE_INFO( "WindowRenderSurface::OutputTransformed: window = %d screen = %d\n", mRotationAngle, mScreenRotationAngle );
498 DALI_LOG_RELEASE_INFO( "WindowRenderSurface::OutputTransformed: Ignore output transform [%d]\n", mScreenRotationAngle );
502 void WindowRenderSurface::ProcessRotationRequest()
504 mRotationFinished = true;
506 mWindowBase->WindowRotationCompleted( mRotationAngle, mPositionSize.width, mPositionSize.height );
508 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::ProcessRotationRequest: Rotation Done\n" );
510 if( mThreadSynchronization )
512 mThreadSynchronization->PostRenderComplete();
516 } // namespace Adaptor
518 } // namespace internal