[dali_2.3.25] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / update / manager / scene-graph-frame-callback.cpp
1 /*
2  * Copyright (c) 2024 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     if(mRootNode)
43     {
44       mRootNode->RemoveObserver(*this);
45     }
46     mUpdateProxy->AddNodeResetters();
47   }
48
49   Invalidate();
50 }
51
52 void FrameCallback::ConnectToSceneGraph(UpdateManager& updateManager, TransformManager& transformManager, Node& rootNode, SceneGraphTravelerInterfacePtr traveler)
53 {
54   mRootNode = &rootNode;
55   rootNode.AddObserver(*this);
56
57   ConnectToSceneGraph(updateManager, transformManager, traveler);
58 }
59
60 void FrameCallback::ConnectToSceneGraph(UpdateManager& updateManager, TransformManager& transformManager, SceneGraphTravelerInterfacePtr traveler)
61 {
62   mUpdateProxy = std::unique_ptr<UpdateProxy>(new UpdateProxy(updateManager, transformManager, traveler));
63 }
64
65 FrameCallback::RequestFlags FrameCallback::Update(BufferIndex bufferIndex, float elapsedSeconds, bool nodeHierarchyChanged)
66 {
67   bool continueCalling = false;
68   bool keepRendering   = false;
69
70   if(mUpdateProxy)
71   {
72     mUpdateProxy->SetCurrentBufferIndex(bufferIndex);
73
74     while(!mSyncPoints.empty())
75     {
76       mUpdateProxy->Notify(mSyncPoints.front());
77       mSyncPoints.pop_front();
78     }
79
80     if(nodeHierarchyChanged)
81     {
82       mUpdateProxy->NodeHierarchyChanged();
83     }
84
85     Mutex::ScopedLock lock(mMutex);
86     if(mFrameCallbackInterface && mValid)
87     {
88       Dali::UpdateProxy updateProxy(*mUpdateProxy);
89       keepRendering   = mFrameCallbackInterface->Update(updateProxy, elapsedSeconds);
90       continueCalling = true;
91     }
92   }
93
94   return static_cast<FrameCallback::RequestFlags>(continueCalling | (keepRendering << 1));
95 }
96
97 void FrameCallback::Notify(Dali::UpdateProxy::NotifySyncPoint syncPoint)
98 {
99   mSyncPoints.push_back(syncPoint);
100 }
101
102 void FrameCallback::Invalidate()
103 {
104   Mutex::ScopedLock lock(mMutex);
105   if(mFrameCallbackInterface && mValid)
106   {
107     FrameCallbackInterface::Impl::Get(*mFrameCallbackInterface).DisconnectFromSceneGraphObject();
108     mValid = false;
109     // Do not set mFrameCallbackInterface to nullptr as it is used for comparison checks by the comparison operator
110   }
111 }
112
113 void FrameCallback::PropertyOwnerDestroyed(PropertyOwner& owner)
114 {
115   mUpdateProxy.reset(); // Root node is being destroyed so no point keeping the update-proxy either
116   mRootNode = nullptr;
117
118   Invalidate();
119 }
120
121 FrameCallback::FrameCallback(FrameCallbackInterface* frameCallbackInterface)
122 : mMutex(),
123   mFrameCallbackInterface(frameCallbackInterface)
124 {
125   if(frameCallbackInterface)
126   {
127     FrameCallbackInterface::Impl::Get(*mFrameCallbackInterface).ConnectToSceneGraphObject(*this);
128   }
129 }
130
131 } // namespace SceneGraph
132
133 } // namespace Internal
134
135 } // namespace Dali