1 #ifndef DALI_INTERNAL_SCENE_GRAPH_CONSTRAINT_BASE_H
2 #define DALI_INTERNAL_SCENE_GRAPH_CONSTRAINT_BASE_H
5 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <dali/internal/common/message.h>
23 #include <dali/internal/event/common/event-thread-services.h>
24 #include <dali/internal/update/animation/scene-graph-constraint-declarations.h>
25 #include <dali/internal/update/common/animatable-property.h>
26 #include <dali/internal/update/common/property-owner.h>
27 #include <dali/internal/update/common/scene-graph-buffers.h>
28 #include <dali/public-api/animation/constraint.h>
29 #include <dali/public-api/common/dali-common.h>
35 // value types used by messages
37 struct ParameterType<Dali::Constraint::RemoveAction>
38 : public BasicType<Dali::Constraint::RemoveAction>
44 using PropertyOwnerContainer = Dali::Vector<PropertyOwner*>;
45 using PropertyOwnerIter = PropertyOwnerContainer::Iterator;
48 * An abstract base class for Constraints.
49 * This can be used to constrain a property of a scene-object, after animations have been applied.
51 class ConstraintBase : public PropertyOwner::Observer
55 * Observer to determine when the constraint is no longer present
57 class LifecycleObserver
61 * Called shortly before the constraint is destroyed.
63 virtual void ObjectDestroyed() = 0;
67 * Virtual destructor, no deletion through this interface
69 virtual ~LifecycleObserver() = default;
73 using RemoveAction = Dali::Constraint::RemoveAction;
77 * @param ownerContainer the properties to constraint
78 * @oparam removeAction perform when removed
80 ConstraintBase(PropertyOwnerContainer& ownerContainer, RemoveAction removeAction);
85 ~ConstraintBase() override;
88 * Property resetter observes the lifecycle of this object
90 void AddLifecycleObserver(LifecycleObserver& observer)
92 mLifecycleObserver = &observer;
96 * Property resetter observers the lifecycle of this object
98 void RemoveLifecycleObserver(LifecycleObserver& observer)
100 mLifecycleObserver = nullptr;
104 * Initialize the constraint.
105 * This should by called by a scene-object, when the constraint is connected.
111 mDisconnected = false;
115 * @copydoc Dali::Constraint::SetRemoveAction()
117 void SetRemoveAction(RemoveAction action)
119 mRemoveAction = action;
123 * @copydoc Dali::Constraint::GetRemoveAction()
125 RemoveAction GetRemoveAction() const
127 return mRemoveAction;
131 * Constrain the associated scene object.
132 * @param[in] updateBufferIndex The current update buffer index.
134 virtual void Apply(BufferIndex updateBufferIndex) = 0;
137 * Helper for internal test cases; only available for debug builds.
138 * @return The current number of Constraint instances in existence.
140 static uint32_t GetCurrentInstanceCount();
143 * Helper for internal test cases; only available for debug builds.
144 * @return The total number of Constraint instances created during the Dali core lifetime.
146 static uint32_t GetTotalInstanceCount();
150 * Helper to start observing property owners
152 void StartObservation()
154 const PropertyOwnerIter end = mObservedOwners.End();
155 for(PropertyOwnerIter iter = mObservedOwners.Begin(); end != iter; ++iter)
157 (*iter)->AddObserver(*this);
162 * Helper to stop observing property owners
164 void StopObservation()
166 const PropertyOwnerIter end = mObservedOwners.End();
167 for(PropertyOwnerIter iter = mObservedOwners.Begin(); end != iter; ++iter)
169 (*iter)->RemoveObserver(*this);
172 mObservedOwners.Clear();
176 * @copydoc PropertyOwner::Observer::PropertyOwnerConnected()
178 void PropertyOwnerConnected(PropertyOwner& owner) override
183 * @copydoc PropertyOwner::Observer::PropertyOwnerDisconnected()
185 void PropertyOwnerDisconnected(BufferIndex bufferIndex, PropertyOwner& owner) override
189 // Stop observing property owners
192 // Notification for derived class
195 mDisconnected = true;
200 * @copydoc PropertyOwner::Observer::PropertyOwnerDestroyed()
202 void PropertyOwnerDestroyed(PropertyOwner& owner) override
206 // Discard pointer to destroyed property owner. Otherwise StopObservation() would crash when trying to remove
207 //the constraint from the destroyed PropertyOwner's observers list
208 PropertyOwnerIter iter = std::find(mObservedOwners.Begin(), mObservedOwners.End(), &owner);
209 if(mObservedOwners.End() != iter)
211 mObservedOwners.Erase(iter);
214 // Stop observing the rest of property owners
217 // Notification for derived class
220 mDisconnected = true;
225 * Notify the derived class to disconnect from property owners
227 virtual void OnDisconnect() = 0;
230 RemoveAction mRemoveAction;
232 bool mFirstApply : 1;
233 bool mDisconnected : 1;
236 PropertyOwnerContainer mObservedOwners; ///< A set of pointers to each observed object. Not owned.
237 LifecycleObserver* mLifecycleObserver; ///< Resetter observers this object
240 static uint32_t mCurrentInstanceCount; ///< The current number of Constraint instances in existence.
241 static uint32_t mTotalInstanceCount; ///< The total number of Constraint instances created during the Dali core lifetime.
245 // Messages for ConstraintBase
247 inline void SetRemoveActionMessage(EventThreadServices& eventThreadServices, const ConstraintBase& constraint, Dali::Constraint::RemoveAction removeAction)
249 using LocalType = MessageValue1<ConstraintBase, Dali::Constraint::RemoveAction>;
251 // Reserve some memory inside the message queue
252 uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
254 // Construct message in the message queue memory; note that delete should not be called on the return value
255 new(slot) LocalType(&constraint, &ConstraintBase::SetRemoveAction, removeAction);
258 } // namespace SceneGraph
260 } // namespace Internal
264 #endif // DALI_INTERNAL_SCENE_GRAPH_CONSTRAINT_BASE_H