Assert when proprety notification is used in a thread other than the main thread
[platform/core/uifw/dali-core.git] / dali / internal / event / common / property-notification-impl.cpp
1 /*
2  * Copyright (c) 2023 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/common/property-notification-impl.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/internal/event/actors/actor-impl.h>
23 #include <dali/internal/event/common/object-impl.h>
24 #include <dali/internal/event/common/property-notification-manager.h>
25 #include <dali/internal/event/common/thread-local-storage.h>
26 #include <dali/internal/update/common/scene-graph-property-notification.h>
27 #include <dali/internal/update/manager/update-manager.h>
28 #include <dali/public-api/actors/actor.h>
29 #include <dali/public-api/common/dali-common.h>
30 #include <dali/public-api/math/radian.h>
31 #include <dali/public-api/math/vector2.h>
32
33 using Dali::Internal::SceneGraph::UpdateManager;
34
35 namespace Dali
36 {
37 namespace Internal
38 {
39 PropertyNotificationPtr PropertyNotification::New(Property&                      target,
40                                                   int                            componentIndex,
41                                                   const Dali::PropertyCondition& condition)
42 {
43   ThreadLocalStorage& tls = ThreadLocalStorage::Get();
44
45   UpdateManager& updateManager = tls.GetUpdateManager();
46
47   PropertyNotificationManager& propertyNotificationManager = tls.GetPropertyNotificationManager();
48   PropertyNotificationPtr      propertyNotification        = new PropertyNotification(updateManager,
49                                                                           propertyNotificationManager,
50                                                                           target,
51                                                                           componentIndex,
52                                                                           condition);
53   return propertyNotification;
54 }
55
56 PropertyNotification::PropertyNotification(UpdateManager&                 updateManager,
57                                            PropertyNotificationManager&   propertyNotificationManager,
58                                            Property&                      target,
59                                            int                            componentIndex,
60                                            const Dali::PropertyCondition& condition)
61 : mUpdateManager(updateManager),
62   mPropertyNotification(nullptr),
63   mPropertyNotificationManager(propertyNotificationManager),
64   mObjectPropertyIndex(target.propertyIndex),
65   mPropertyType(Property::NONE),
66   mComponentIndex(componentIndex),
67   mCondition(condition),
68   mNotifyMode(Dali::PropertyNotification::NOTIFY_ON_TRUE),
69   mNotifyResult(false),
70   mCompare(false)
71 {
72   const Internal::PropertyCondition& conditionImpl = GetImplementation(condition);
73
74   Dali::Vector<float>::SizeType count = conditionImpl.arguments.Count();
75   for(Dali::Vector<float>::SizeType index = 0; index < count; ++index)
76   {
77     mRawConditionArgs.PushBack(conditionImpl.arguments[index]);
78   }
79
80   // Observe target object and create/destroy notification scene object accordingly.
81   mObject = dynamic_cast<Object*>(&GetImplementation(target.object));
82   if(mObject)
83   {
84     mPropertyType = mObject->GetPropertyType(mObjectPropertyIndex);
85
86     int internalComponentIndex = mObject->GetPropertyComponentIndex(mObjectPropertyIndex);
87     if(internalComponentIndex != Property::INVALID_COMPONENT_INDEX)
88     {
89       // override the one passed in
90       mComponentIndex = internalComponentIndex;
91     }
92     if(mComponentIndex != Property::INVALID_COMPONENT_INDEX)
93     {
94       Property::Type type = mObject->GetPropertyType(mObjectPropertyIndex);
95       if(type == Property::VECTOR2 || type == Property::VECTOR3 || type == Property::VECTOR4)
96       {
97         mPropertyType = Property::FLOAT;
98       }
99     }
100
101     // To cover swapping components, previous and current components should be compared.
102     if(mObject->GetPropertyType(mObjectPropertyIndex) == Property::VECTOR3)
103     {
104       mCompare = true;
105       for(int i = 0; i < 3; ++i)
106       {
107         mRawConditionArgs.PushBack(0.0f);
108       }
109     }
110
111     // all objects always have scene object
112     CreateSceneObject();
113   }
114
115   // Connect to the property notification manager
116   mPropertyNotificationManager.PropertyNotificationCreated(*this);
117 }
118
119 PropertyNotification::~PropertyNotification()
120 {
121   Disable();
122
123   // Guard to disallow use of PropertyNotificationManager after Core has been destroyed
124   if(!EventThreadServices::IsShuttingDown())
125   {
126     // Disconnect from the property notification manager
127     mPropertyNotificationManager.PropertyNotificationDestroyed(*this);
128   }
129 }
130
131 Dali::PropertyNotifySignalType& PropertyNotification::NotifySignal()
132 {
133   return mNotifySignal;
134 }
135
136 void PropertyNotification::EmitSignalNotify()
137 {
138   Dali::PropertyNotification source(this);
139
140   mNotifySignal.Emit(source);
141 }
142
143 void PropertyNotification::Enable()
144 {
145   CreateSceneObject();
146 }
147
148 void PropertyNotification::Disable()
149 {
150   // Guard to allow handle destruction after Core has been destroyed
151   if(!EventThreadServices::IsShuttingDown())
152   {
153     // Stop scene-graph from monitoring the target's properties.
154     DestroySceneObject();
155   }
156 }
157
158 void PropertyNotification::SetNotifyResult(bool result)
159 {
160   mNotifyResult = result;
161 }
162
163 const Dali::PropertyCondition& PropertyNotification::GetCondition() const
164 {
165   return mCondition;
166 }
167
168 Dali::Handle PropertyNotification::GetTarget() const
169 {
170   Dali::Handle handle(mObject);
171
172   return handle;
173 }
174
175 Property::Index PropertyNotification::GetTargetProperty() const
176 {
177   return mObjectPropertyIndex;
178 }
179
180 void PropertyNotification::SetNotifyMode(NotifyMode mode)
181 {
182   mNotifyMode = mode;
183   if(mPropertyNotification)
184   {
185     PropertyNotificationSetNotifyModeMessage(mUpdateManager, mPropertyNotification, mode);
186   }
187 }
188
189 PropertyNotification::NotifyMode PropertyNotification::GetNotifyMode()
190 {
191   return mNotifyMode;
192 }
193
194 bool PropertyNotification::GetNotifyResult() const
195 {
196   return mNotifyResult;
197 }
198
199 bool PropertyNotification::CompareSceneObject(const SceneGraph::PropertyNotification* sceneObject)
200 {
201   return sceneObject && sceneObject == mPropertyNotification;
202 }
203
204 void PropertyNotification::CreateSceneObject()
205 {
206   // this method can be called from constructor and on stage connection
207   if(!mPropertyNotification)
208   {
209     const PropertyInputImpl* property = mObject->GetSceneObjectInputProperty(mObjectPropertyIndex);
210
211     // Create a new PropertyNotification, keep a const pointer to it
212     mPropertyNotification = SceneGraph::PropertyNotification::New(property,
213                                                                   mObjectPropertyIndex,
214                                                                   mPropertyType,
215                                                                   mComponentIndex,
216                                                                   GetImplementation(mCondition).type,
217                                                                   mRawConditionArgs,
218                                                                   mNotifyMode,
219                                                                   mCompare);
220     OwnerPointer<SceneGraph::PropertyNotification> transferOwnership(const_cast<SceneGraph::PropertyNotification*>(mPropertyNotification));
221     AddPropertyNotificationMessage(mUpdateManager, transferOwnership);
222   }
223 }
224
225 void PropertyNotification::DestroySceneObject()
226 {
227   if(mPropertyNotification != nullptr)
228   {
229     DALI_ASSERT_ALWAYS(EventThreadServices::IsCoreRunning());
230
231     // Remove PropertyNotification using a message to the update manager
232     RemovePropertyNotificationMessage(mUpdateManager, *mPropertyNotification);
233     mPropertyNotification = nullptr;
234   }
235 }
236
237 } // namespace Internal
238
239 } // namespace Dali