[Tizen] Implement partial update
[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 )
101 {
102   // create node, nodes are owned by UpdateManager
103   SceneGraph::Layer* rootLayer = SceneGraph::Layer::New();
104   OwnerPointer< SceneGraph::Layer > transferOwnership( rootLayer );
105   InstallRootMessage( EventThreadServices::Get().GetUpdateManager(), 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   if ( mIsRoot )
143   {
144     // Guard to allow handle destruction after Core has been destroyed
145     if( EventThreadServices::IsCoreRunning() )
146     {
147       UninstallRootMessage( GetEventThreadServices().GetUpdateManager(), &GetSceneLayerOnStage() );
148
149       GetEventThreadServices().UnregisterObject( this );
150     }
151   }
152 }
153
154 unsigned int Layer::GetDepth() const
155 {
156   return mLayerList ? mLayerList->GetDepth( this ) : 0u;
157 }
158
159 void Layer::Raise()
160 {
161   if ( mLayerList )
162   {
163     mLayerList->RaiseLayer(*this);
164   }
165 }
166
167 void Layer::Lower()
168 {
169   if ( mLayerList )
170   {
171     mLayerList->LowerLayer(*this);
172   }
173 }
174
175 void Layer::RaiseAbove( const Internal::Layer& target )
176 {
177   // cannot raise above ourself, both have to be on stage
178   if( ( this != &target ) && OnStage() && target.OnStage() )
179   {
180     // get parameters depth
181     const uint32_t targetDepth = target.GetDepth();
182     if( GetDepth() < targetDepth )
183     {
184       MoveAbove( target );
185     }
186   }
187 }
188
189 void Layer::LowerBelow( const Internal::Layer& target )
190 {
191   // cannot lower below ourself, both have to be on stage
192   if( ( this != &target ) && OnStage() && target.OnStage() )
193   {
194     // get parameters depth
195     const uint32_t targetDepth = target.GetDepth();
196     if( GetDepth() > targetDepth )
197     {
198       MoveBelow( target );
199     }
200   }
201 }
202
203 void Layer::RaiseToTop()
204 {
205   if ( mLayerList )
206   {
207     mLayerList->RaiseLayerToTop(*this);
208   }
209 }
210
211 void Layer::LowerToBottom()
212 {
213   if ( mLayerList )
214   {
215     mLayerList->LowerLayerToBottom(*this);
216   }
217 }
218
219 void Layer::MoveAbove( const Internal::Layer& target )
220 {
221   // cannot raise above ourself, both have to be on stage
222   if( ( this != &target ) && mLayerList && target.OnStage() )
223   {
224     mLayerList->MoveLayerAbove(*this, target );
225   }
226 }
227
228 void Layer::MoveBelow( const Internal::Layer& target )
229 {
230   // cannot lower below ourself, both have to be on stage
231   if( ( this != &target ) && mLayerList && target.OnStage() )
232   {
233     mLayerList->MoveLayerBelow(*this, target );
234   }
235 }
236
237 void Layer::SetBehavior( Dali::Layer::Behavior behavior )
238 {
239   mBehavior = behavior;
240
241   // Notify update side object.
242   SetBehaviorMessage( GetEventThreadServices(), GetSceneLayerOnStage(), behavior );
243   // By default, disable depth test for LAYER_UI, and enable for LAYER_3D.
244   SetDepthTestDisabled( mBehavior == Dali::Layer::LAYER_UI );
245 }
246
247 void Layer::SetClipping(bool enabled)
248 {
249   if (enabled != mIsClipping)
250   {
251     mIsClipping = enabled;
252
253     // layerNode is being used in a separate thread; queue a message to set the value
254     SetClippingMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mIsClipping );
255   }
256 }
257
258 void Layer::SetClippingBox(int x, int y, int width, int height)
259 {
260   if( ( x != mClippingBox.x ) ||
261       ( y != mClippingBox.y ) ||
262       ( width != mClippingBox.width ) ||
263       ( height != mClippingBox.height ) )
264   {
265     // Clipping box is not animatable; this is the most up-to-date value
266     mClippingBox.Set(x, y, width, height);
267
268     // Convert mClippingBox to GL based coordinates (from bottom-left)
269     ClippingBox clippingBox( mClippingBox );
270
271     if( mScene )
272     {
273       clippingBox.y = static_cast<int32_t>( mScene->GetSize().height ) - clippingBox.y - clippingBox.height;
274
275       // layerNode is being used in a separate thread; queue a message to set the value
276       SetClippingBoxMessage( GetEventThreadServices(), GetSceneLayerOnStage(), clippingBox );
277     }
278   }
279 }
280
281 void Layer::SetDepthTestDisabled( bool disable )
282 {
283   if( disable != mDepthTestDisabled )
284   {
285     mDepthTestDisabled = disable;
286
287     // Send message.
288     // layerNode is being used in a separate thread; queue a message to set the value
289     SetDepthTestDisabledMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mDepthTestDisabled );
290   }
291 }
292
293 bool Layer::IsDepthTestDisabled() const
294 {
295   return mDepthTestDisabled;
296 }
297
298 void Layer::SetSortFunction(Dali::Layer::SortFunctionType function)
299 {
300   if( function != mSortFunction )
301   {
302     mSortFunction = function;
303
304     // layerNode is being used in a separate thread; queue a message to set the value
305     SetSortFunctionMessage( GetEventThreadServices(), GetSceneLayerOnStage(), mSortFunction );
306   }
307 }
308
309 void Layer::SetTouchConsumed( bool consume )
310 {
311   mTouchConsumed = consume;
312 }
313
314 bool Layer::IsTouchConsumed() const
315 {
316   return mTouchConsumed;
317 }
318
319 void Layer::SetHoverConsumed( bool consume )
320 {
321   mHoverConsumed = consume;
322 }
323
324 bool Layer::IsHoverConsumed() const
325 {
326   return mHoverConsumed;
327 }
328
329 void Layer::OnStageConnectionInternal()
330 {
331   if ( !mIsRoot )
332   {
333     DALI_ASSERT_DEBUG( NULL == mLayerList );
334
335     // Find the ordered layer-list
336     for ( Actor* parent = mParent; parent != NULL; parent = parent->GetParent() )
337     {
338       if( parent->IsLayer() )
339       {
340         Layer* parentLayer = static_cast< Layer* >( parent ); // cheaper than dynamic_cast
341         mLayerList = parentLayer->mLayerList;
342       }
343     }
344   }
345
346   DALI_ASSERT_DEBUG( NULL != mLayerList );
347   mLayerList->RegisterLayer( *this );
348 }
349
350 void Layer::OnStageDisconnectionInternal()
351 {
352   mLayerList->UnregisterLayer(*this);
353
354   // mLayerList is only valid when on-stage
355   mLayerList = NULL;
356 }
357
358 const SceneGraph::Layer& Layer::GetSceneLayerOnStage() const
359 {
360   return static_cast< const SceneGraph::Layer& >( GetNode() ); // we know our node is a layer node
361 }
362
363 void Layer::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
364 {
365   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
366   {
367     Actor::SetDefaultProperty( index, propertyValue );
368   }
369   else
370   {
371     switch( index )
372     {
373       case Dali::Layer::Property::CLIPPING_ENABLE:
374       {
375         SetClipping( propertyValue.Get<bool>() );
376         break;
377       }
378       case Dali::Layer::Property::CLIPPING_BOX:
379       {
380         Rect<int32_t> clippingBox( propertyValue.Get<Rect<int32_t> >() );
381         SetClippingBox( clippingBox.x, clippingBox.y, clippingBox.width, clippingBox.height );
382         break;
383       }
384       case Dali::Layer::Property::BEHAVIOR:
385       {
386         Behavior behavior(Dali::Layer::LAYER_UI);
387         if( Scripting::GetEnumeration< Behavior >( propertyValue.Get< std::string >().c_str(), BEHAVIOR_TABLE, BEHAVIOR_TABLE_COUNT, behavior ) )
388         {
389           SetBehavior( behavior );
390         }
391         break;
392       }
393       default:
394       {
395         DALI_LOG_WARNING( "Unknown property (%d)\n", index );
396         break;
397       }
398     } // switch(index)
399
400   } // else
401 }
402
403 Property::Value Layer::GetDefaultProperty( Property::Index index ) const
404 {
405   Property::Value ret;
406   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
407   {
408     ret = Actor::GetDefaultProperty( index );
409   }
410   else
411   {
412     switch( index )
413     {
414       case Dali::Layer::Property::CLIPPING_ENABLE:
415       {
416         ret = mIsClipping;
417         break;
418       }
419       case Dali::Layer::Property::CLIPPING_BOX:
420       {
421         ret = mClippingBox;
422         break;
423       }
424       case Dali::Layer::Property::BEHAVIOR:
425       {
426         ret = Scripting::GetLinearEnumerationName< Behavior >( GetBehavior(), BEHAVIOR_TABLE, BEHAVIOR_TABLE_COUNT );
427         break;
428       }
429       default:
430       {
431         DALI_LOG_WARNING( "Unknown property (%d)\n", index );
432         break;
433       }
434     } // switch(index)
435   }
436
437   return ret;
438 }
439
440 Property::Value Layer::GetDefaultPropertyCurrentValue( Property::Index index ) const
441 {
442   Property::Value ret;
443   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
444   {
445     ret = Actor::GetDefaultPropertyCurrentValue( index );
446   }
447   else
448   {
449     ret = GetDefaultProperty( index ); // Layer only has event-side properties
450   }
451
452   return ret;
453 }
454
455 bool Layer::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& /*attributes*/ )
456 {
457   bool done = false;
458   Layer* layer = dynamic_cast<Layer*>( object );
459
460   if( layer )
461   {
462     if( 0 == actionName.compare( ACTION_RAISE ) )
463     {
464       layer->Raise();
465       done = true;
466     }
467     else if( 0 == actionName.compare( ACTION_LOWER ) )
468     {
469       layer->Lower();
470       done = true;
471     }
472     else if( 0 == actionName.compare( ACTION_RAISE_TO_TOP ) )
473     {
474       layer->RaiseToTop();
475       done = true;
476     }
477     else if( 0 == actionName.compare( ACTION_LOWER_TO_BOTTOM ) )
478     {
479       layer->LowerToBottom();
480       done = true;
481     }
482   }
483
484   return done;
485 }
486
487 } // namespace Internal
488
489 } // namespace Dali