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