Reset node animatable properties for two frames after removing the frame callback
[platform/core/uifw/dali-core.git] / dali / internal / update / manager / scene-graph-frame-callback.cpp
1 /*
2  * Copyright (c) 2021 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/update/manager/scene-graph-frame-callback.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/devel-api/threading/mutex.h>
23 #include <dali/devel-api/update/frame-callback-interface.h>
24 #include <dali/devel-api/update/update-proxy.h>
25 #include <dali/internal/event/update/frame-callback-interface-impl.h>
26
27 namespace Dali
28 {
29 namespace Internal
30 {
31 namespace SceneGraph
32 {
33 FrameCallback* FrameCallback::New(FrameCallbackInterface& frameCallbackInterface)
34 {
35   return new FrameCallback(&frameCallbackInterface);
36 }
37
38 FrameCallback::~FrameCallback()
39 {
40   if(mUpdateProxy)
41   {
42     mUpdateProxy->GetRootNode().RemoveObserver(*this);
43     mUpdateProxy->AddNodeResetters();
44   }
45
46   Invalidate();
47 }
48
49 void FrameCallback::ConnectToSceneGraph(UpdateManager& updateManager, TransformManager& transformManager, Node& rootNode)
50 {
51   mUpdateProxy = std::unique_ptr<UpdateProxy>(new UpdateProxy(updateManager, transformManager, rootNode));
52   rootNode.AddObserver(*this);
53 }
54
55 bool FrameCallback::Update(BufferIndex bufferIndex, float elapsedSeconds, bool nodeHierarchyChanged)
56 {
57   bool continueCalling = false;
58   if(mUpdateProxy)
59   {
60     mUpdateProxy->SetCurrentBufferIndex(bufferIndex);
61
62     if(nodeHierarchyChanged)
63     {
64       mUpdateProxy->NodeHierarchyChanged();
65     }
66
67     Mutex::ScopedLock lock(mMutex);
68     if(mFrameCallbackInterface && mValid)
69     {
70       Dali::UpdateProxy updateProxy(*mUpdateProxy);
71       mFrameCallbackInterface->Update(updateProxy, elapsedSeconds);
72       continueCalling = true;
73     }
74   }
75   return continueCalling;
76 }
77
78 void FrameCallback::Invalidate()
79 {
80   Mutex::ScopedLock lock(mMutex);
81   if(mFrameCallbackInterface && mValid)
82   {
83     FrameCallbackInterface::Impl::Get(*mFrameCallbackInterface).DisconnectFromSceneGraphObject();
84     mValid = false;
85     // Do not set mFrameCallbackInterface to nullptr as it is used for comparison checks by the comparison operator
86   }
87 }
88
89 void FrameCallback::PropertyOwnerDestroyed(PropertyOwner& owner)
90 {
91   mUpdateProxy.reset(); // Root node is being destroyed so no point keeping the update-proxy either
92
93   Invalidate();
94 }
95
96 FrameCallback::FrameCallback(FrameCallbackInterface* frameCallbackInterface)
97 : mMutex(),
98   mFrameCallbackInterface(frameCallbackInterface)
99 {
100   if(frameCallbackInterface)
101   {
102     FrameCallbackInterface::Impl::Get(*mFrameCallbackInterface).ConnectToSceneGraphObject(*this);
103   }
104 }
105
106 } // namespace SceneGraph
107
108 } // namespace Internal
109
110 } // namespace Dali