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/window-system/common/window-base.h>
31 #include <dali/internal/window-system/common/window-factory.h>
32 #include <dali/internal/window-system/common/window-system.h>
33 #include <dali/internal/graphics/gles/egl-graphics.h>
46 const int MINIMUM_DIMENSION_CHANGE( 1 ); ///< Minimum change for window to be considered to have moved
48 #if defined(DEBUG_ENABLED)
49 Debug::Filter* gWindowRenderSurfaceLogFilter = Debug::Filter::New(Debug::Verbose, false, "LOG_WINDOW_RENDER_SURFACE");
52 } // unnamed namespace
54 WindowRenderSurface::WindowRenderSurface( Dali::PositionSize positionSize, Any surface, bool isTransparent )
55 : mPositionSize( positionSize ),
57 mThreadSynchronization( NULL ),
58 mRenderNotification( NULL ),
59 mRotationTrigger( NULL ),
60 mColorDepth( isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24 ),
61 mOutputTransformedSignal(),
63 mScreenRotationAngle( 0 ),
65 mRotationSupported( false ),
66 mRotationFinished( true ),
67 mScreenRotationFinished( true ),
68 mResizeFinished( true )
70 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "Creating Window\n" );
71 Initialize( surface );
74 WindowRenderSurface::~WindowRenderSurface()
76 if( mRotationTrigger )
78 delete mRotationTrigger;
82 void WindowRenderSurface::Initialize( Any surface )
84 // If width or height are zero, go full screen.
85 if ( (mPositionSize.width == 0) || (mPositionSize.height == 0) )
87 // Default window size == screen size
91 WindowSystem::GetScreenSize( mPositionSize.width, mPositionSize.height );
94 // Create a window base
95 auto windowFactory = Dali::Internal::Adaptor::GetWindowFactory();
96 mWindowBase = windowFactory->CreateWindowBase( mPositionSize, surface, ( mColorDepth == COLOR_DEPTH_32 ? true : false ) );
99 mWindowBase->OutputTransformedSignal().Connect( this, &WindowRenderSurface::OutputTransformed );
101 // Check screen rotation
102 mScreenRotationAngle = mWindowBase->GetScreenRotationAngle();
103 if( mScreenRotationAngle != 0 )
105 mScreenRotationFinished = false;
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( !mRotationSupported )
138 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::Rotate: Rotation is not supported!\n" );
142 if( !mRotationTrigger )
144 TriggerEventFactoryInterface& triggerFactory = Internal::Adaptor::Adaptor::GetImplementation( Adaptor::Get() ).GetTriggerEventFactoryInterface();
145 mRotationTrigger = triggerFactory.CreateTriggerEvent( MakeCallback( this, &WindowRenderSurface::ProcessRotationRequest ), TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER );
148 mPositionSize.width = width;
149 mPositionSize.height = height;
151 mRotationAngle = angle;
152 mRotationFinished = false;
154 mWindowBase->SetWindowRotationAngle( mRotationAngle );
156 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::Rotate: angle = %d screen rotation = %d\n", mRotationAngle, mScreenRotationAngle );
159 WindowBase* WindowRenderSurface::GetWindowBase()
161 return mWindowBase.get();
164 WindowBase::OutputSignalType& WindowRenderSurface::OutputTransformedSignal()
166 return mOutputTransformedSignal;
169 PositionSize WindowRenderSurface::GetPositionSize() const
171 return mPositionSize;
174 void WindowRenderSurface::GetDpi( unsigned int& dpiHorizontal, unsigned int& dpiVertical )
176 mWindowBase->GetDpi( dpiHorizontal, dpiVertical );
179 void WindowRenderSurface::InitializeGraphics( GraphicsInterface& graphics, Dali::DisplayConnection& displayConnection )
181 mGraphics = &graphics;
183 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
185 EglInterface* mEGL = eglGraphics->Create();
187 // Initialize EGL & OpenGL
188 displayConnection.Initialize();
190 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>(*mEGL);
191 eglImpl.ChooseConfig(true, mColorDepth);
193 // Create the OpenGL context
194 mEGL->CreateContext();
196 // Create the OpenGL surface
200 mEGL->MakeContextCurrent();
203 void WindowRenderSurface::CreateSurface()
205 DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
208 if( mScreenRotationAngle == 0 || mScreenRotationAngle == 180 )
210 width = mPositionSize.width;
211 height = mPositionSize.height;
215 width = mPositionSize.height;
216 height = mPositionSize.width;
219 // Create the EGL window
220 EGLNativeWindowType window = mWindowBase->CreateEglWindow( width, height );
222 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
224 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
225 eglImpl.CreateSurfaceWindow( window, mColorDepth );
227 // Check rotation capability
228 mRotationSupported = mWindowBase->IsEglWindowRotationSupported();
230 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::CreateSurface: w = %d h = %d angle = %d screen rotation = %d\n", mPositionSize.width, mPositionSize.height, mRotationAngle, mScreenRotationAngle );
233 void WindowRenderSurface::DestroySurface()
235 DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
237 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
239 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
240 eglImpl.DestroySurface();
242 mWindowBase->DestroyEglWindow();
245 bool WindowRenderSurface::ReplaceGraphicsSurface()
247 DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
249 // Destroy the old one
250 mWindowBase->DestroyEglWindow();
253 if( mScreenRotationAngle == 0 || mScreenRotationAngle == 180 )
255 width = mPositionSize.width;
256 height = mPositionSize.height;
260 width = mPositionSize.height;
261 height = mPositionSize.width;
264 // Create the EGL window
265 EGLNativeWindowType window = mWindowBase->CreateEglWindow( width, height );
267 // Set screen rotation
268 mScreenRotationFinished = false;
270 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
272 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
273 return eglImpl.ReplaceSurfaceWindow( window );
276 void WindowRenderSurface::MoveResize( Dali::PositionSize positionSize )
278 bool needToMove = false;
279 bool needToResize = false;
282 if( (fabs(positionSize.x - mPositionSize.x) > MINIMUM_DIMENSION_CHANGE) ||
283 (fabs(positionSize.y - mPositionSize.y) > MINIMUM_DIMENSION_CHANGE) )
289 if( (fabs(positionSize.width - mPositionSize.width) > MINIMUM_DIMENSION_CHANGE) ||
290 (fabs(positionSize.height - mPositionSize.height) > MINIMUM_DIMENSION_CHANGE) )
299 mWindowBase->MoveResize( positionSize );
303 mWindowBase->Resize( positionSize );
306 mResizeFinished = false;
307 mPositionSize = positionSize;
313 mWindowBase->Move( positionSize );
315 mPositionSize = positionSize;
319 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::MoveResize: %d, %d, %d, %d\n", mPositionSize.x, mPositionSize.y, mPositionSize.width, mPositionSize.height );
322 void WindowRenderSurface::SetViewMode( ViewMode viewMode )
324 mWindowBase->SetViewMode( viewMode );
327 void WindowRenderSurface::StartRender()
331 bool WindowRenderSurface::PreRender( bool resizingSurface )
333 if( resizingSurface )
335 #ifdef OVER_TIZEN_VERSION_4
336 // Window rotate or screen rotate
337 if( !mRotationFinished || !mScreenRotationFinished )
339 int totalAngle = (mRotationAngle + mScreenRotationAngle) % 360;
341 mWindowBase->SetEglWindowRotation( totalAngle );
342 mWindowBase->SetEglWindowBufferTransform( totalAngle );
344 // Reset only screen rotation flag
345 mScreenRotationFinished = true;
347 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PreRender: Set rotation [%d] [%d]\n", mRotationAngle, mScreenRotationAngle );
350 // Only window rotate
351 if( !mRotationFinished )
353 mWindowBase->SetEglWindowTransform( mRotationAngle );
358 if( !mResizeFinished )
360 mWindowBase->ResizeEglWindow( mPositionSize );
361 mResizeFinished = true;
363 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PreRender: Set resize\n" );
367 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
368 auto mGLES = eglGraphics->GetGlesInterface();
374 void WindowRenderSurface::PostRender( bool renderToFbo, bool replacingSurface, bool resizingSurface )
376 // Inform the gl implementation that rendering has finished before informing the surface
377 auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
378 auto mGLES = eglGraphics->GetGlesInterface();
388 if( resizingSurface )
390 if( !mRotationFinished )
392 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PostRender: Trigger rotation event\n" );
394 mRotationTrigger->Trigger();
396 if( mThreadSynchronization )
398 // Wait until the event-thread complete the rotation event processing
399 mThreadSynchronization->PostRenderWaitForCompletion();
405 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
406 eglImpl.SwapBuffers();
408 if( mRenderNotification )
410 mRenderNotification->Trigger();
414 void WindowRenderSurface::StopRender()
418 void WindowRenderSurface::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
420 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::SetThreadSynchronization: called\n" );
422 mThreadSynchronization = &threadSynchronization;
425 void WindowRenderSurface::ReleaseLock()
430 RenderSurface::Type WindowRenderSurface::GetSurfaceType()
432 return RenderSurface::WINDOW_RENDER_SURFACE;
435 void WindowRenderSurface::OutputTransformed()
437 int screenRotationAngle = mWindowBase->GetScreenRotationAngle();
439 if( mScreenRotationAngle != screenRotationAngle )
441 mScreenRotationAngle = screenRotationAngle;
442 mScreenRotationFinished = false;
444 mOutputTransformedSignal.Emit();
446 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::OutputTransformed: angle = %d screen rotation = %d\n", mRotationAngle, mScreenRotationAngle );
450 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::OutputTransformed: Ignore output transform [%d]\n", mScreenRotationAngle );
454 void WindowRenderSurface::ProcessRotationRequest()
456 mRotationFinished = true;
458 mWindowBase->WindowRotationCompleted( mRotationAngle, mPositionSize.width, mPositionSize.height );
460 DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::ProcessRotationRequest: Rotation Done\n" );
462 if( mThreadSynchronization )
464 mThreadSynchronization->PostRenderComplete();
468 } // namespace Adaptor
470 } // namespace internal