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