Support multiple window rendering
[platform/core/uifw/dali-core.git] / dali / internal / event / actors / layer-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/event/actors/layer-impl.h>
20
21 // EXTERNAL INCLUDES
22
23 // INTERNAL INCLUDES
24 #include <dali/public-api/actors/layer.h>
25 #include <dali/public-api/common/dali-common.h>
26 #include <dali/public-api/object/type-registry.h>
27 #include <dali/internal/event/actors/layer-list.h>
28 #include <dali/internal/event/common/property-helper.h>
29 #include <dali/internal/event/common/scene-impl.h>
30 #include <dali/internal/event/common/event-thread-services.h>
31
32 using Dali::Internal::SceneGraph::UpdateManager;
33
34 namespace Dali
35 {
36
37 namespace
38 {
39
40 typedef Layer::Behavior Behavior;
41
42 DALI_ENUM_TO_STRING_TABLE_BEGIN( BEHAVIOR )
43 DALI_ENUM_TO_STRING_WITH_SCOPE( Layer, LAYER_UI )
44 DALI_ENUM_TO_STRING_WITH_SCOPE( Layer, LAYER_3D )
45 DALI_ENUM_TO_STRING_TABLE_END( BEHAVIOR )
46
47 } // namespace
48
49 namespace Internal
50 {
51
52 namespace
53 {
54
55 // Properties
56
57 //              Name                Type      writable animatable constraint-input  enum for index-checking
58 DALI_PROPERTY_TABLE_BEGIN
59 DALI_PROPERTY( "clippingEnable",    BOOLEAN,    true,    false,   true,   Dali::Layer::Property::CLIPPING_ENABLE )
60 DALI_PROPERTY( "clippingBox",       RECTANGLE,  true,    false,   true,   Dali::Layer::Property::CLIPPING_BOX    )
61 DALI_PROPERTY( "behavior",          STRING,     true,    false,   false,  Dali::Layer::Property::BEHAVIOR        )
62 DALI_PROPERTY_TABLE_END( DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX, LayerDefaultProperties )
63
64 // Actions
65
66 const char* const ACTION_RAISE =           "raise";
67 const char* const ACTION_LOWER =           "lower";
68 const char* const ACTION_RAISE_TO_TOP =    "raiseToTop";
69 const char* const ACTION_LOWER_TO_BOTTOM = "lowerToBottom";
70
71 BaseHandle Create()
72 {
73   return Dali::Layer::New();
74 }
75
76 TypeRegistration mType( typeid( Dali::Layer ), typeid( Dali::Actor ), Create, LayerDefaultProperties );
77
78 TypeAction a1( mType, ACTION_RAISE, &Layer::DoAction );
79 TypeAction a2( mType, ACTION_LOWER, &Layer::DoAction );
80 TypeAction a3( mType, ACTION_RAISE_TO_TOP, &Layer::DoAction );
81 TypeAction a4( mType, ACTION_LOWER_TO_BOTTOM, &Layer::DoAction );
82
83 } // unnamed namespace
84
85
86 LayerPtr Layer::New()
87 {
88   // create node, nodes are owned by UpdateManager
89   SceneGraph::Layer* layerNode = SceneGraph::Layer::New();
90   OwnerPointer< SceneGraph::Node > transferOwnership( layerNode );
91   AddNodeMessage( EventThreadServices::Get().GetUpdateManager(), transferOwnership );
92   LayerPtr layer( new Layer( Actor::LAYER, *layerNode ) );
93
94   // Second-phase construction
95   layer->Initialize();
96
97   return layer;
98 }
99
100 LayerPtr Layer::NewRoot( LayerList& layerList, UpdateManager& manager )
101 {
102   // create node, nodes are owned by UpdateManager
103   SceneGraph::Layer* rootLayer = SceneGraph::Layer::New();
104   OwnerPointer< SceneGraph::Layer > transferOwnership( rootLayer );
105   InstallRootMessage( manager, transferOwnership );
106
107   LayerPtr root( new Layer( Actor::ROOT_LAYER, *rootLayer ) );
108
109   // root actor is immediately considered to be on-stage
110   root->mIsOnStage = true;
111
112   // The root actor will not emit a stage connection signal so set the signalled flag here as well
113   root->mOnStageSignalled = true;
114
115   // layer-list must be set for the root layer
116   root->mLayerList = &layerList;
117   layerList.SetRootLayer( &(*root) );
118   layerList.RegisterLayer( *root );
119
120   return root;
121 }
122
123 Layer::Layer( Actor::DerivedType type, const SceneGraph::Layer& layer )
124 : Actor( type, layer ),
125   mLayerList( NULL ),
126   mClippingBox( 0, 0, 0, 0 ),
127   mSortFunction( Layer::ZValue ),
128   mBehavior( Dali::Layer::LAYER_UI ),
129   mIsClipping( false ),
130   mDepthTestDisabled( true ),
131   mTouchConsumed( false ),
132   mHoverConsumed( false )
133 {
134 }
135
136 void Layer::OnInitialize()
137 {
138 }
139
140 Layer::~Layer()
141 {
142 }
143
144 unsigned int Layer::GetDepth() const
145 {
146   return mLayerList ? mLayerList->GetDepth( this ) : 0u;
147 }
148
149 void Layer::Raise()
150 {
151   if ( mLayerList )
152   {
153     mLayerList->RaiseLayer(*this);
154   }
155 }
156
157 void Layer::Lower()
158 {
159   if ( mLayerList )
160   {
161     mLayerList->LowerLayer(*this);
162   }
163 }
164
165 void Layer::RaiseAbove( const Internal::Layer& target )
166 {
167   // cannot raise above ourself, both have to be on stage
168   if( ( this != &target ) && OnStage() && target.OnStage() )
169   {
170     // get parameters depth
171     const uint32_t targetDepth = target.GetDepth();
172     if( GetDepth() < targetDepth )
173     {
174       MoveAbove( target );
175     }
176   }
177 }
178
179 void Layer::LowerBelow( const Internal::Layer& target )
180 {
181   // cannot lower below ourself, both have to be on stage
182   if( ( this != &target ) && OnStage() && target.OnStage() )
183   {
184     // get parameters depth
185     const uint32_t targetDepth = target.GetDepth();
186     if( GetDepth() > targetDepth )
187     {
188       MoveBelow( target );
189     }
190   }
191 }
192
193 void Layer::RaiseToTop()
194 {
195   if ( mLayerList )
196   {
197     mLayerList->RaiseLayerToTop(*this);
198   }
199 }
200
201 void Layer::LowerToBottom()
202 {
203   if ( mLayerList )
204   {
205     mLayerList->LowerLayerToBottom(*this);
206   }
207 }
208
209 void Layer::MoveAbove( const Internal::Layer& target )
210 {
211   // cannot raise above ourself, both have to be on stage
212   if( ( this != &target ) && mLayerList && target.OnStage() )
213   {
214     mLayerList->MoveLayerAbove(*this, target );
215   }
216 }
217
218 void Layer::MoveBelow( const Internal::Layer& target )
219 {
220   // cannot lower below ourself, both have to be on stage
221   if( ( this != &target ) && mLayerList && target.OnStage() )
222   {
223     mLayerList->MoveLayerBelow(*this, target );
224   }
225 }
226
227 void Layer::SetBehavior( Dali::Layer::Behavior behavior )
228 {
229   mBehavior = behavior;
230
231   // Notify update side object.
232   SetBehaviorMessage( GetEventThreadServices(), GetSceneLayerOnStage(), behavior );
233   // By default, disable depth test for LAYER_UI, and enable for LAYER_3D.
234   SetDepthTestDisabled( mBehavior == Dali::Layer::LAYER_UI );
235 }
236
237 void Layer::SetClipping(bool enabled)
238 {
239   if (enabled != mIsClipping)
240   {
241     mIsClipping = enabled;
242
243     // layerNode is being used in a separate thread; queue a message to set the value
244     SetClippingMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mIsClipping );
245   }
246 }
247
248 void Layer::SetClippingBox(int x, int y, int width, int height)
249 {
250   if( ( x != mClippingBox.x ) ||
251       ( y != mClippingBox.y ) ||
252       ( width != mClippingBox.width ) ||
253       ( height != mClippingBox.height ) )
254   {
255     // Clipping box is not animatable; this is the most up-to-date value
256     mClippingBox.Set(x, y, width, height);
257
258     // Convert mClippingBox to GL based coordinates (from bottom-left)
259     ClippingBox clippingBox( mClippingBox );
260
261     if( mScene )
262     {
263       clippingBox.y = static_cast<int32_t>( mScene->GetSize().height ) - clippingBox.y - clippingBox.height;
264
265       // layerNode is being used in a separate thread; queue a message to set the value
266       SetClippingBoxMessage( GetEventThreadServices(), GetSceneLayerOnStage(), clippingBox );
267     }
268   }
269 }
270
271 void Layer::SetDepthTestDisabled( bool disable )
272 {
273   if( disable != mDepthTestDisabled )
274   {
275     mDepthTestDisabled = disable;
276
277     // Send message.
278     // layerNode is being used in a separate thread; queue a message to set the value
279     SetDepthTestDisabledMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mDepthTestDisabled );
280   }
281 }
282
283 bool Layer::IsDepthTestDisabled() const
284 {
285   return mDepthTestDisabled;
286 }
287
288 void Layer::SetSortFunction(Dali::Layer::SortFunctionType function)
289 {
290   if( function != mSortFunction )
291   {
292     mSortFunction = function;
293
294     // layerNode is being used in a separate thread; queue a message to set the value
295     SetSortFunctionMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mSortFunction );
296   }
297 }
298
299 void Layer::SetTouchConsumed( bool consume )
300 {
301   mTouchConsumed = consume;
302 }
303
304 bool Layer::IsTouchConsumed() const
305 {
306   return mTouchConsumed;
307 }
308
309 void Layer::SetHoverConsumed( bool consume )
310 {
311   mHoverConsumed = consume;
312 }
313
314 bool Layer::IsHoverConsumed() const
315 {
316   return mHoverConsumed;
317 }
318
319 void Layer::OnStageConnectionInternal()
320 {
321   if ( !mIsRoot )
322   {
323     DALI_ASSERT_DEBUG( NULL == mLayerList );
324
325     // Find the ordered layer-list
326     for ( Actor* parent = mParent; parent != NULL; parent = parent->GetParent() )
327     {
328       if( parent->IsLayer() )
329       {
330         Layer* parentLayer = static_cast< Layer* >( parent ); // cheaper than dynamic_cast
331         mLayerList = parentLayer->mLayerList;
332       }
333     }
334   }
335
336   DALI_ASSERT_DEBUG( NULL != mLayerList );
337   mLayerList->RegisterLayer( *this );
338 }
339
340 void Layer::OnStageDisconnectionInternal()
341 {
342   mLayerList->UnregisterLayer(*this);
343
344   // mLayerList is only valid when on-stage
345   mLayerList = NULL;
346 }
347
348 const SceneGraph::Layer& Layer::GetSceneLayerOnStage() const
349 {
350   return static_cast< const SceneGraph::Layer& >( GetNode() ); // we know our node is a layer node
351 }
352
353 void Layer::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
354 {
355   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
356   {
357     Actor::SetDefaultProperty( index, propertyValue );
358   }
359   else
360   {
361     switch( index )
362     {
363       case Dali::Layer::Property::CLIPPING_ENABLE:
364       {
365         SetClipping( propertyValue.Get<bool>() );
366         break;
367       }
368       case Dali::Layer::Property::CLIPPING_BOX:
369       {
370         Rect<int32_t> clippingBox( propertyValue.Get<Rect<int32_t> >() );
371         SetClippingBox( clippingBox.x, clippingBox.y, clippingBox.width, clippingBox.height );
372         break;
373       }
374       case Dali::Layer::Property::BEHAVIOR:
375       {
376         Behavior behavior(Dali::Layer::LAYER_UI);
377         if( Scripting::GetEnumeration< Behavior >( propertyValue.Get< std::string >().c_str(), BEHAVIOR_TABLE, BEHAVIOR_TABLE_COUNT, behavior ) )
378         {
379           SetBehavior( behavior );
380         }
381         break;
382       }
383       default:
384       {
385         DALI_LOG_WARNING( "Unknown property (%d)\n", index );
386         break;
387       }
388     } // switch(index)
389
390   } // else
391 }
392
393 Property::Value Layer::GetDefaultProperty( Property::Index index ) const
394 {
395   Property::Value ret;
396   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
397   {
398     ret = Actor::GetDefaultProperty( index );
399   }
400   else
401   {
402     switch( index )
403     {
404       case Dali::Layer::Property::CLIPPING_ENABLE:
405       {
406         ret = mIsClipping;
407         break;
408       }
409       case Dali::Layer::Property::CLIPPING_BOX:
410       {
411         ret = mClippingBox;
412         break;
413       }
414       case Dali::Layer::Property::BEHAVIOR:
415       {
416         ret = Scripting::GetLinearEnumerationName< Behavior >( GetBehavior(), BEHAVIOR_TABLE, BEHAVIOR_TABLE_COUNT );
417         break;
418       }
419       default:
420       {
421         DALI_LOG_WARNING( "Unknown property (%d)\n", index );
422         break;
423       }
424     } // switch(index)
425   }
426
427   return ret;
428 }
429
430 Property::Value Layer::GetDefaultPropertyCurrentValue( Property::Index index ) const
431 {
432   Property::Value ret;
433   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
434   {
435     ret = Actor::GetDefaultPropertyCurrentValue( index );
436   }
437   else
438   {
439     ret = GetDefaultProperty( index ); // Layer only has event-side properties
440   }
441
442   return ret;
443 }
444
445 bool Layer::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& /*attributes*/ )
446 {
447   bool done = false;
448   Layer* layer = dynamic_cast<Layer*>( object );
449
450   if( layer )
451   {
452     if( 0 == actionName.compare( ACTION_RAISE ) )
453     {
454       layer->Raise();
455       done = true;
456     }
457     else if( 0 == actionName.compare( ACTION_LOWER ) )
458     {
459       layer->Lower();
460       done = true;
461     }
462     else if( 0 == actionName.compare( ACTION_RAISE_TO_TOP ) )
463     {
464       layer->RaiseToTop();
465       done = true;
466     }
467     else if( 0 == actionName.compare( ACTION_LOWER_TO_BOTTOM ) )
468     {
469       layer->LowerToBottom();
470       done = true;
471     }
472   }
473
474   return done;
475 }
476
477 } // namespace Internal
478
479 } // namespace Dali