Merge "To get MaxTextureSize by using glGetIntegerv not by using environment variable...
[platform/core/uifw/dali-adaptor.git] / dali / internal / window-system / common / window-impl.cpp
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/window-system/common/window-impl.h>
20
21 // EXTERNAL HEADERS
22 #include <dali/integration-api/core.h>
23 #include <dali/public-api/actors/actor.h>
24 #include <dali/public-api/actors/layer.h>
25 #include <dali/public-api/actors/camera-actor.h>
26 #include <dali/public-api/render-tasks/render-task.h>
27 #include <dali/public-api/render-tasks/render-task-list.h>
28 #include <dali/public-api/rendering/frame-buffer.h>
29 #include <dali/devel-api/adaptor-framework/orientation.h>
30 #include <dali/integration-api/events/touch-event-integ.h>
31
32 #ifdef DALI_ADAPTOR_COMPILATION
33 #include <dali/integration-api/render-surface-interface.h>
34 #else
35 #include <dali/integration-api/adaptors/render-surface-interface.h>
36 #endif
37
38 // INTERNAL HEADERS
39 #include <dali/internal/window-system/common/event-handler.h>
40 #include <dali/internal/window-system/common/orientation-impl.h>
41 #include <dali/internal/window-system/common/render-surface-factory.h>
42 #include <dali/internal/window-system/common/window-factory.h>
43 #include <dali/internal/window-system/common/window-base.h>
44 #include <dali/internal/window-system/common/window-render-surface.h>
45 #include <dali/internal/window-system/common/window-visibility-observer.h>
46
47 namespace Dali
48 {
49 namespace Internal
50 {
51 namespace Adaptor
52 {
53
54 namespace
55 {
56
57 #if defined(DEBUG_ENABLED)
58 Debug::Filter* gWindowLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_WINDOW" );
59 #endif
60
61 } // unnamed namespace
62
63 Window* Window::New( const PositionSize& positionSize, const std::string& name, const std::string& className, bool isTransparent )
64 {
65   Window* window = new Window();
66   window->mIsTransparent = isTransparent;
67   window->Initialize( positionSize, name, className );
68   return window;
69 }
70
71 Window::Window()
72 : mWindowSurface( nullptr ),
73   mWindowBase(),
74   mIsTransparent( false ),
75   mIsFocusAcceptable( true ),
76   mIconified( false ),
77   mOpaqueState( false ),
78   mResizeEnabled( false ),
79   mType( Dali::Window::NORMAL ),
80   mParentWindow( NULL ),
81   mPreferredOrientation( Dali::Window::PORTRAIT ),
82   mRotationAngle( 0 ),
83   mWindowWidth( 0 ),
84   mWindowHeight( 0 ),
85   mFocusChangedSignal(),
86   mResizedSignal(),
87   mDeleteRequestSignal()
88 {
89 }
90
91 Window::~Window()
92 {
93   if ( mEventHandler )
94   {
95     mEventHandler->RemoveObserver( *this );
96   }
97 }
98
99 void Window::Initialize(const PositionSize& positionSize, const std::string& name, const std::string& className)
100 {
101   // Create a window render surface
102   Any surface;
103   auto renderSurfaceFactory = Dali::Internal::Adaptor::GetRenderSurfaceFactory();
104   mSurface = renderSurfaceFactory->CreateWindowRenderSurface( positionSize, surface, mIsTransparent );
105   mWindowSurface = static_cast<WindowRenderSurface*>( mSurface.get() );
106
107   // Get a window base
108   mWindowBase = mWindowSurface->GetWindowBase();
109
110   // Connect signals
111   mWindowBase->IconifyChangedSignal().Connect( this, &Window::OnIconifyChanged );
112   mWindowBase->FocusChangedSignal().Connect( this, &Window::OnFocusChanged );
113   mWindowBase->DeleteRequestSignal().Connect( this, &Window::OnDeleteRequest );
114
115   mWindowSurface->OutputTransformedSignal().Connect( this, &Window::OnOutputTransformed );
116
117   if( !positionSize.IsEmpty() )
118   {
119     AddAuxiliaryHint( "wm.policy.win.user.geometry", "1" );
120     mResizeEnabled = true;
121   }
122
123   SetClass( name, className );
124
125   mWindowSurface->Map();
126
127   mOrientation = Orientation::New( this );
128 }
129
130 void Window::OnAdaptorSet(Dali::Adaptor& adaptor)
131 {
132   mEventHandler = EventHandlerPtr(new EventHandler( mWindowSurface, *mAdaptor ) );
133   mEventHandler->AddObserver( *this );
134 }
135
136 void Window::OnSurfaceSet( Dali::RenderSurfaceInterface* surface )
137 {
138   mWindowSurface = static_cast<WindowRenderSurface*>( surface );
139 }
140
141 void Window::ShowIndicator( Dali::Window::IndicatorVisibleMode visibleMode )
142 {
143 }
144
145 void Window::SetIndicatorBgOpacity( Dali::Window::IndicatorBgOpacity opacityMode )
146 {
147 }
148
149 void Window::RotateIndicator( Dali::Window::WindowOrientation orientation )
150 {
151 }
152
153 void Window::SetClass( std::string name, std::string className )
154 {
155   mName = name;
156   mClassName = className;
157   mWindowBase->SetClass( name, className );
158 }
159
160 std::string Window::GetClassName() const
161 {
162   return mClassName;
163 }
164
165 void Window::Raise()
166 {
167   mWindowBase->Raise();
168   DALI_LOG_RELEASE_INFO( "Window (%p) Raise() \n", this );
169 }
170
171 void Window::Lower()
172 {
173   mWindowBase->Lower();
174   DALI_LOG_RELEASE_INFO( "Window (%p) Lower() \n", this );
175 }
176
177 void Window::Activate()
178 {
179   mWindowBase->Activate();
180   DALI_LOG_RELEASE_INFO( "Window (%p) Activate() \n", this );
181 }
182
183 uint32_t Window::GetLayerCount() const
184 {
185   return mScene.GetLayerCount();
186 }
187
188 Dali::Layer Window::GetLayer( uint32_t depth ) const
189 {
190   return mScene.GetLayer( depth );
191 }
192
193 Dali::RenderTaskList Window::GetRenderTaskList() const
194 {
195   return mScene.GetRenderTaskList();
196 }
197
198 void Window::AddAvailableOrientation( Dali::Window::WindowOrientation orientation )
199 {
200   bool found = false;
201
202   if( orientation <= Dali::Window::LANDSCAPE_INVERSE )
203   {
204     for( std::size_t i = 0; i < mAvailableOrientations.size(); i++ )
205     {
206       if( mAvailableOrientations[i] == orientation )
207       {
208         found = true;
209         break;
210       }
211     }
212
213     if( !found )
214     {
215       mAvailableOrientations.push_back( orientation );
216       SetAvailableOrientations( mAvailableOrientations );
217     }
218   }
219 }
220
221 void Window::RemoveAvailableOrientation( Dali::Window::WindowOrientation orientation )
222 {
223   for( std::vector<Dali::Window::WindowOrientation>::iterator iter = mAvailableOrientations.begin();
224        iter != mAvailableOrientations.end(); ++iter )
225   {
226     if( *iter == orientation )
227     {
228       mAvailableOrientations.erase( iter );
229       break;
230     }
231   }
232   SetAvailableOrientations( mAvailableOrientations );
233 }
234
235 void Window::SetAvailableOrientations( const std::vector< Dali::Window::WindowOrientation >& orientations )
236 {
237   if( orientations.size() > 4 )
238   {
239     DALI_LOG_INFO( gWindowLogFilter, Debug::Verbose, "Window::SetAvailableOrientations: Invalid vector size! [%d]\n", orientations.size() );
240     return;
241   }
242
243   mAvailableOrientations = orientations;
244
245   mWindowBase->SetAvailableOrientations( mAvailableOrientations );
246 }
247
248 const std::vector< Dali::Window::WindowOrientation >& Window::GetAvailableOrientations()
249 {
250   return mAvailableOrientations;
251 }
252
253 void Window::SetPreferredOrientation( Dali::Window::WindowOrientation orientation )
254 {
255   mPreferredOrientation = orientation;
256
257   mWindowBase->SetPreferredOrientation( mPreferredOrientation );
258 }
259
260 Dali::Window::WindowOrientation Window::GetPreferredOrientation()
261 {
262   return mPreferredOrientation;
263 }
264
265 Dali::Any Window::GetNativeHandle() const
266 {
267   return mWindowSurface->GetNativeWindow();
268 }
269
270 void Window::SetAcceptFocus( bool accept )
271 {
272   mIsFocusAcceptable = accept;
273
274   mWindowBase->SetAcceptFocus( accept );
275 }
276
277 bool Window::IsFocusAcceptable() const
278 {
279   return mIsFocusAcceptable;
280 }
281
282 void Window::Show()
283 {
284   mVisible = true;
285
286   mWindowBase->Show();
287
288   if( !mIconified )
289   {
290     WindowVisibilityObserver* observer( mAdaptor );
291     observer->OnWindowShown();
292   }
293
294   DALI_LOG_RELEASE_INFO( "Window (%p) Show(): iconified = %d\n", this, mIconified );
295 }
296
297 void Window::Hide()
298 {
299   mVisible = false;
300
301   mWindowBase->Hide();
302
303   if( !mIconified )
304   {
305     WindowVisibilityObserver* observer( mAdaptor );
306     observer->OnWindowHidden();
307   }
308
309   DALI_LOG_RELEASE_INFO( "Window (%p) Hide(): iconified = %d\n", this, mIconified );
310 }
311
312 bool Window::IsVisible() const
313 {
314   return mVisible && !mIconified;
315 }
316
317 unsigned int Window::GetSupportedAuxiliaryHintCount() const
318 {
319   return mWindowBase->GetSupportedAuxiliaryHintCount();
320 }
321
322 std::string Window::GetSupportedAuxiliaryHint( unsigned int index ) const
323 {
324   return mWindowBase->GetSupportedAuxiliaryHint( index );
325 }
326
327 unsigned int Window::AddAuxiliaryHint( const std::string& hint, const std::string& value )
328 {
329   return mWindowBase->AddAuxiliaryHint( hint, value );
330 }
331
332 bool Window::RemoveAuxiliaryHint( unsigned int id )
333 {
334   return mWindowBase->RemoveAuxiliaryHint( id );
335 }
336
337 bool Window::SetAuxiliaryHintValue( unsigned int id, const std::string& value )
338 {
339   return mWindowBase->SetAuxiliaryHintValue( id, value );
340 }
341
342 std::string Window::GetAuxiliaryHintValue( unsigned int id ) const
343 {
344   return mWindowBase->GetAuxiliaryHintValue( id );
345 }
346
347 unsigned int Window::GetAuxiliaryHintId( const std::string& hint ) const
348 {
349   return mWindowBase->GetAuxiliaryHintId( hint );
350 }
351
352 void Window::SetInputRegion( const Rect< int >& inputRegion )
353 {
354   mWindowBase->SetInputRegion( inputRegion );
355
356   DALI_LOG_INFO( gWindowLogFilter, Debug::Verbose, "Window::SetInputRegion: x = %d, y = %d, w = %d, h = %d\n", inputRegion.x, inputRegion.y, inputRegion.width, inputRegion.height );
357 }
358
359 void Window::SetType( Dali::Window::Type type )
360 {
361   if( type != mType )
362   {
363     mWindowBase->SetType( type );
364
365     mType = type;
366   }
367 }
368
369 Dali::Window::Type Window::GetType() const
370 {
371   return mType;
372 }
373
374 bool Window::SetNotificationLevel( Dali::Window::NotificationLevel::Type level )
375 {
376   if( mType != Dali::Window::NOTIFICATION )
377   {
378     DALI_LOG_INFO( gWindowLogFilter, Debug::Verbose, "Window::SetNotificationLevel: Not supported window type [%d]\n", mType );
379     return false;
380   }
381
382   return mWindowBase->SetNotificationLevel( level );
383 }
384
385 Dali::Window::NotificationLevel::Type Window::GetNotificationLevel() const
386 {
387   if( mType != Dali::Window::NOTIFICATION )
388   {
389     DALI_LOG_INFO( gWindowLogFilter, Debug::Verbose, "Window::GetNotificationLevel: Not supported window type [%d]\n", mType );
390     return Dali::Window::NotificationLevel::NONE;
391   }
392
393   return mWindowBase->GetNotificationLevel();
394 }
395
396 void Window::SetOpaqueState( bool opaque )
397 {
398   mOpaqueState = opaque;
399
400   mWindowBase->SetOpaqueState( opaque );
401
402   DALI_LOG_INFO( gWindowLogFilter, Debug::Verbose, "Window::SetOpaqueState: opaque = %d\n", opaque );
403 }
404
405 bool Window::IsOpaqueState() const
406 {
407   return mOpaqueState;
408 }
409
410 bool Window::SetScreenOffMode(Dali::Window::ScreenOffMode::Type screenOffMode)
411 {
412   return mWindowBase->SetScreenOffMode( screenOffMode );
413 }
414
415 Dali::Window::ScreenOffMode::Type Window::GetScreenOffMode() const
416 {
417   return mWindowBase->GetScreenOffMode();
418 }
419
420 bool Window::SetBrightness( int brightness )
421 {
422   if( brightness < 0 || brightness > 100 )
423   {
424     DALI_LOG_INFO( gWindowLogFilter, Debug::Verbose, "Window::SetBrightness: Invalid brightness value [%d]\n", brightness );
425     return false;
426   }
427
428   return mWindowBase->SetBrightness( brightness );
429 }
430
431 int Window::GetBrightness() const
432 {
433   return mWindowBase->GetBrightness();
434 }
435
436 void Window::SetSize( Dali::Window::WindowSize size )
437 {
438   if( !mResizeEnabled )
439   {
440     AddAuxiliaryHint( "wm.policy.win.user.geometry", "1" );
441     mResizeEnabled = true;
442   }
443
444   PositionSize oldRect = mSurface->GetPositionSize();
445
446   mWindowSurface->MoveResize( PositionSize( oldRect.x, oldRect.y, size.GetWidth(), size.GetHeight() ) );
447
448   PositionSize newRect = mSurface->GetPositionSize();
449
450   // When surface size is updated, inform adaptor of resizing and emit ResizeSignal
451   if( ( oldRect.width != newRect.width ) || ( oldRect.height != newRect.height ) )
452   {
453     Uint16Pair newSize( newRect.width, newRect.height );
454
455     SurfaceResized();
456
457     mAdaptor->SurfaceResizePrepare( mSurface.get(), newSize );
458
459     mResizedSignal.Emit( newSize );
460
461     mAdaptor->SurfaceResizeComplete( mSurface.get(), newSize );
462   }
463 }
464
465 Dali::Window::WindowSize Window::GetSize() const
466 {
467   PositionSize positionSize = mSurface->GetPositionSize();
468
469   return Dali::Window::WindowSize( positionSize.width, positionSize.height );
470 }
471
472 void Window::SetPosition( Dali::Window::WindowPosition position )
473 {
474   if( !mResizeEnabled )
475   {
476     AddAuxiliaryHint( "wm.policy.win.user.geometry", "1" );
477     mResizeEnabled = true;
478   }
479
480   PositionSize oldRect = mSurface->GetPositionSize();
481
482   mWindowSurface->MoveResize( PositionSize( position.GetX(), position.GetY(), oldRect.width, oldRect.height ) );
483 }
484
485 Dali::Window::WindowPosition Window::GetPosition() const
486 {
487   PositionSize positionSize = mSurface->GetPositionSize();
488
489   return Dali::Window::WindowPosition( positionSize.x, positionSize.y );
490 }
491
492 void Window::SetPositionSize( PositionSize positionSize )
493 {
494   if( !mResizeEnabled )
495   {
496     AddAuxiliaryHint( "wm.policy.win.user.geometry", "1" );
497     mResizeEnabled = true;
498   }
499
500   PositionSize oldRect = mSurface->GetPositionSize();
501
502   mWindowSurface->MoveResize( positionSize );
503
504   PositionSize newRect = mSurface->GetPositionSize();
505
506   // When surface size is updated, inform adaptor of resizing and emit ResizeSignal
507   if( ( oldRect.width != newRect.width ) || ( oldRect.height != newRect.height ) )
508   {
509     Uint16Pair newSize( newRect.width, newRect.height );
510
511     SurfaceResized();
512
513     mAdaptor->SurfaceResizePrepare( mSurface.get(), newSize );
514
515     mResizedSignal.Emit( newSize );
516
517     mAdaptor->SurfaceResizeComplete( mSurface.get(), newSize );
518   }
519 }
520
521 Dali::Layer Window::GetRootLayer() const
522 {
523   return mScene.GetRootLayer();
524 }
525
526 void Window::SetTransparency( bool transparent )
527 {
528   mWindowSurface->SetTransparency( transparent );
529 }
530
531 bool Window::GrabKey( Dali::KEY key, KeyGrab::KeyGrabMode grabMode )
532 {
533   return mWindowBase->GrabKey( key, grabMode );
534 }
535
536 bool Window::UngrabKey( Dali::KEY key )
537 {
538   return mWindowBase->UngrabKey( key );
539 }
540
541 bool Window::GrabKeyList( const Dali::Vector< Dali::KEY >& key, const Dali::Vector< KeyGrab::KeyGrabMode >& grabMode, Dali::Vector< bool >& result )
542 {
543   return mWindowBase->GrabKeyList( key, grabMode, result );
544 }
545
546 bool Window::UngrabKeyList( const Dali::Vector< Dali::KEY >& key, Dali::Vector< bool >& result )
547 {
548   return mWindowBase->UngrabKeyList( key, result );
549 }
550
551 void Window::OnIconifyChanged( bool iconified )
552 {
553   if( iconified )
554   {
555     mIconified = true;
556
557     if( mVisible )
558     {
559       WindowVisibilityObserver* observer( mAdaptor );
560       observer->OnWindowHidden();
561     }
562
563     DALI_LOG_RELEASE_INFO( "Window (%p) Iconified: visible = %d\n", this, mVisible );
564   }
565   else
566   {
567     mIconified = false;
568
569     if( mVisible )
570     {
571       WindowVisibilityObserver* observer( mAdaptor );
572       observer->OnWindowShown();
573     }
574
575     DALI_LOG_RELEASE_INFO( "Window (%p) Deiconified: visible = %d\n", this, mVisible );
576   }
577 }
578
579 void Window::OnFocusChanged( bool focusIn )
580 {
581   mFocusChangedSignal.Emit( focusIn );
582 }
583
584 void Window::OnOutputTransformed()
585 {
586   PositionSize positionSize = mSurface->GetPositionSize();
587   SurfaceResized();
588   mAdaptor->SurfaceResizePrepare( mSurface.get(), Adaptor::SurfaceSize( positionSize.width, positionSize.height ) );
589   mAdaptor->SurfaceResizeComplete( mSurface.get(), Adaptor::SurfaceSize( positionSize.width, positionSize.height ) );
590 }
591
592 void Window::OnDeleteRequest()
593 {
594   mDeleteRequestSignal.Emit();
595 }
596
597 void Window::OnTouchPoint( Dali::Integration::Point& point, int timeStamp )
598 {
599   FeedTouchPoint( point, timeStamp );
600 }
601
602 void Window::OnWheelEvent( Dali::Integration::WheelEvent& wheelEvent )
603 {
604   FeedWheelEvent( wheelEvent );
605 }
606
607 void Window::OnKeyEvent( Dali::Integration::KeyEvent& keyEvent )
608 {
609   FeedKeyEvent( keyEvent );
610 }
611
612 void Window::OnRotation( const RotationEvent& rotation )
613 {
614   mRotationAngle = rotation.angle;
615   mWindowWidth = rotation.width;
616   mWindowHeight = rotation.height;
617
618   // Notify that the orientation is changed
619   mOrientation->OnOrientationChange( rotation );
620
621   mWindowSurface->RequestRotation( mRotationAngle, mWindowWidth, mWindowHeight );
622
623   SurfaceResized();
624
625   mAdaptor->SurfaceResizePrepare( mSurface.get(), Adaptor::SurfaceSize( mRotationAngle, mWindowHeight ) );
626
627   // Emit signal
628   mResizedSignal.Emit( Dali::Window::WindowSize( mRotationAngle, mWindowHeight ) );
629
630   mAdaptor->SurfaceResizeComplete( mSurface.get(), Adaptor::SurfaceSize( mRotationAngle, mWindowHeight ) );
631 }
632
633 void Window::OnPause()
634 {
635   if( mEventHandler )
636   {
637     mEventHandler->Pause();
638   }
639 }
640
641 void Window::OnResume()
642 {
643   if( mEventHandler )
644   {
645     mEventHandler->Resume();
646   }
647 }
648
649 void Window::RecalculateTouchPosition( Integration::Point& point )
650 {
651   Vector2 position = point.GetScreenPosition();
652   Vector2 convertedPosition;
653
654   switch( mRotationAngle )
655   {
656     case 90:
657     {
658       convertedPosition.x = static_cast<float>( mWindowWidth ) - position.y;
659       convertedPosition.y = position.x;
660       break;
661     }
662     case 180:
663     {
664       convertedPosition.x = static_cast<float>( mWindowWidth ) - position.x;
665       convertedPosition.y = static_cast<float>( mWindowHeight ) - position.y;
666       break;
667     }
668     case 270:
669     {
670       convertedPosition.x = position.y;
671       convertedPosition.y = static_cast<float>( mWindowHeight ) - position.x;
672       break;
673     }
674     default:
675     {
676       convertedPosition = position;
677       break;
678     }
679   }
680
681   point.SetScreenPosition( convertedPosition );
682 }
683
684 Dali::Window Window::Get( Dali::Actor actor )
685 {
686   Internal::Adaptor::Window* windowImpl = nullptr;
687
688   if ( Internal::Adaptor::Adaptor::IsAvailable() )
689   {
690     Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation( Internal::Adaptor::Adaptor::Get() );
691     windowImpl = static_cast<Internal::Adaptor::Window*>( adaptor.GetWindow( actor ) );
692   }
693
694   return Dali::Window( windowImpl );
695 }
696
697
698 void Window::SetParent( Dali::Window& parent )
699 {
700   if ( DALI_UNLIKELY( parent ) )
701   {
702     mParentWindow = parent;
703     Dali::Window grandParent = Dali::DevelWindow::GetParent( parent );
704     // check circular parent window setting
705     if ( DALI_UNLIKELY( grandParent ) && mWindowBase->IsMatchedWindow( grandParent.GetNativeHandle() ) )
706     {
707       Dali::DevelWindow::Unparent( parent );
708     }
709     mWindowBase->SetParent( parent.GetNativeHandle() );
710   }
711 }
712
713 void Window::Unparent()
714 {
715   Any parent;
716   mWindowBase->SetParent( parent );
717 }
718
719 Dali::Window Window::GetParent()
720 {
721   return mParentWindow;
722 }
723
724 } // Adaptor
725
726 } // Internal
727
728 } // Dali