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