Refactoring node partial update cache
[platform/core/uifw/dali-core.git] / dali / internal / update / manager / update-proxy-property-modifier.h
1 #ifndef DALI_INTERNAL_UPDATE_PROXY_PROPERTY_MODIFIER_H
2 #define DALI_INTERNAL_UPDATE_PROXY_PROPERTY_MODIFIER_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <dali/internal/common/owner-pointer.h>
23 #include <dali/internal/update/common/property-resetter.h>
24 #include <dali/internal/update/manager/update-manager.h>
25 #include <dali/internal/update/manager/update-proxy-impl.h>
26
27 namespace Dali
28 {
29 namespace Internal
30 {
31 namespace SceneGraph
32 {
33 class Node;
34 class PropertyBase;
35 } // namespace SceneGraph
36
37 /**
38  * Keeps track of any non-transform manager properties that are modified by the UpdateProxy.
39  *
40  * This is required so the Update Manager can then reset the value to the base at the start of every frame.
41  */
42 class UpdateProxy::PropertyModifier final
43 {
44 public:
45   using Resetter = SceneGraph::Resetter<PropertyModifier>;
46
47   /**
48    * Observer to determine when the animator is no longer present
49    */
50   class LifecycleObserver
51   {
52   public:
53     /**
54      * Called shortly before the animator is destroyed.
55      */
56     virtual void ObjectDestroyed() = 0;
57
58   protected:
59     /**
60      * Virtual destructor, no deletion through this interface
61      */
62     virtual ~LifecycleObserver() = default;
63   };
64
65   /**
66    * Default Constructor.
67    * @param[in]  updateManager  A reference to the update-manager
68    */
69   PropertyModifier(SceneGraph::UpdateManager& updateManager)
70   : mProperties(),
71     mLifecycleObservers(),
72     mUpdateManager(&updateManager)
73   {
74   }
75
76   /**
77    * Non-virtual destructor.
78    */
79   ~PropertyModifier()
80   {
81     for(auto& observer : mLifecycleObservers)
82     {
83       observer->ObjectDestroyed();
84     }
85   }
86
87   // Movable but not copyable
88
89   PropertyModifier(const PropertyModifier&) = delete;            ///< Deleted copy constructor.
90   PropertyModifier& operator=(const PropertyModifier&) = delete; ///< Deleted assignment operator.
91
92   /**
93    * Move constructor.
94    */
95   PropertyModifier(PropertyModifier&& other)
96   : mProperties(std::move(other.mProperties)),
97     mLifecycleObservers(std::move(other.mLifecycleObservers)),
98     mUpdateManager(std::move(other.mUpdateManager))
99   {
100     // Clear other so that it does not remove any resetters unintentionally
101     other.mLifecycleObservers.clear();
102   }
103
104   /**
105    * Move assignment operator.
106    */
107   PropertyModifier& operator=(PropertyModifier&& other)
108   {
109     if(this != &other)
110     {
111       mProperties         = std::move(other.mProperties);
112       mLifecycleObservers = std::move(other.mLifecycleObservers);
113       mUpdateManager      = std::move(other.mUpdateManager);
114
115       // Clear other so that it does not remove any resetters unintentionally
116       other.mLifecycleObservers.clear();
117     }
118     return *this;
119   }
120
121   /**
122    * Allows Resetter to track the life-cycle of this object.
123    * @param[in]  observer  The observer to add.
124    */
125   void AddLifecycleObserver(LifecycleObserver& observer)
126   {
127     mLifecycleObservers.push_back(&observer);
128   }
129
130   /**
131    * The Resetter no longer needs to track the life-cycle of this object.
132    * @param[in]  observer  The observer that to remove.
133    */
134   void RemoveLifecycleObserver(LifecycleObserver& observer)
135   {
136     std::remove(mLifecycleObservers.begin(), mLifecycleObservers.end(), &observer);
137   }
138
139   /**
140    * Adds a resetter to the given node and property if it hasn't already been added previously.
141    * @param[in]  node          The associated Node
142    * @param[in]  propertyBase  The associated PropertyBase
143    */
144   void AddResetter(SceneGraph::Node& node, SceneGraph::PropertyBase& propertyBase)
145   {
146     // Check if we've already added a resetter for this node and property to the update-manager
147     NodePropertyPair pair{&node, &propertyBase};
148     if(mUpdateManager &&
149        (mProperties.end() == std::find(mProperties.begin(), mProperties.end(), pair)))
150     {
151       // We haven't, add the pair to our container to ensure we don't add it again
152       // Then create a Resetter which will observe the life of this object
153       // Finally, add the resetter to the Update-Manager
154       // When this object is destroyed, the resetter will be informed and will automatically be removed
155
156       mProperties.emplace_back(std::move(pair));
157       OwnerPointer<SceneGraph::PropertyResetterBase> resetter(Resetter::New(node, propertyBase, *this));
158       mUpdateManager->AddPropertyResetter(resetter);
159     }
160   }
161
162 public:
163   /**
164    * Structure to store the Node & property-base pair
165    */
166   struct NodePropertyPair
167   {
168     SceneGraph::Node*         node;
169     SceneGraph::PropertyBase* propertyBase;
170
171     NodePropertyPair(const NodePropertyPair&) = delete;            ///< Deleted copy constructor.
172     NodePropertyPair(NodePropertyPair&&)      = default;           ///< Default move constructor.
173     NodePropertyPair& operator=(const NodePropertyPair&) = delete; ///< Deleted assignment operator.
174     NodePropertyPair& operator=(NodePropertyPair&&) = default;     ///< Default move assignment operator.
175
176     /**
177      * Comparison operator
178      */
179     bool operator==(const NodePropertyPair& other)
180     {
181       return (other.node == node) &&
182              (other.propertyBase == propertyBase);
183     }
184   };
185
186   std::vector<NodePropertyPair>   mProperties;
187   std::vector<LifecycleObserver*> mLifecycleObservers;
188   SceneGraph::UpdateManager*      mUpdateManager;
189 };
190
191 } // namespace Internal
192
193 } // namespace Dali
194
195 #endif // DALI_INTERNAL_UPDATE_PROXY_PROPERTY_MODIFIER_H