Add some performace trace logs
[platform/core/uifw/dali-core.git] / dali / internal / event / events / long-press-gesture / long-press-gesture-processor.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/events/long-press-gesture/long-press-gesture-processor.h>
20
21 // EXTERNAL INCLUDES
22 #include <algorithm>
23
24 // INTERNAL INCLUDES
25 #include <dali/integration-api/debug.h>
26 #include <dali/integration-api/trace.h>
27 #include <dali/internal/event/actors/actor-impl.h>
28 #include <dali/internal/event/common/scene-impl.h>
29 #include <dali/internal/event/events/gesture-requests.h>
30 #include <dali/internal/event/events/long-press-gesture/long-press-gesture-event.h>
31 #include <dali/internal/event/events/long-press-gesture/long-press-gesture-impl.h>
32 #include <dali/internal/event/events/long-press-gesture/long-press-gesture-recognizer.h>
33 #include <dali/internal/event/render-tasks/render-task-impl.h>
34 #include <dali/public-api/actors/actor.h>
35 #include <dali/public-api/common/dali-common.h>
36
37 namespace Dali
38 {
39 namespace Internal
40 {
41 namespace
42 {
43 DALI_INIT_TRACE_FILTER(gTraceFilter, DALI_TRACE_PERFORMANCE_MARKER, false);
44 const unsigned long DEFAULT_MINIMUM_HOLDING_TIME = 500u;
45
46 /**
47  * Creates a LongPressGesture and asks the specified detector to emit its detected signal.
48  * @param[in]  actor             The actor on which the long press gesture has occurred.
49  * @param[in]  gestureDetectors  A reference to gesture detectors that should emit the signal.
50  * @param[in]  longPressEvent    The longPressEvent received from the adaptor.
51  * @param[in]  localPoint        Relative to the actor attached to the detector.
52  */
53 void EmitLongPressSignal(
54   Actor*                          actor,
55   const GestureDetectorContainer& gestureDetectors,
56   const LongPressGestureEvent&    longPressEvent,
57   Vector2                         localPoint)
58 {
59   Internal::LongPressGesturePtr longPress(new Internal::LongPressGesture(longPressEvent.state));
60   longPress->SetTime(longPressEvent.time);
61   longPress->SetNumberOfTouches(longPressEvent.numberOfTouches);
62   longPress->SetScreenPoint(longPressEvent.point);
63   longPress->SetLocalPoint(localPoint);
64   longPress->SetSourceType(longPressEvent.sourceType);
65   longPress->SetSourceData(longPressEvent.sourceData);
66
67   DALI_TRACE_SCOPE(gTraceFilter, "DALI_EMIT_LONG_PRESS_GESTURE_SIGNAL");
68
69   Dali::Actor                                    actorHandle(actor);
70   const GestureDetectorContainer::const_iterator endIter = gestureDetectors.end();
71   for(GestureDetectorContainer::const_iterator iter = gestureDetectors.begin(); iter != endIter; ++iter)
72   {
73     static_cast<LongPressGestureDetector*>(*iter)->EmitLongPressGestureSignal(actorHandle, Dali::LongPressGesture(longPress.Get()));
74   }
75 }
76
77 /**
78  * Functor which checks whether the specified actor is attached to the gesture detector.
79  * It returns true if it is no longer attached.  This can be used in remove_if functions.
80  */
81 struct IsNotAttachedFunctor
82 {
83   /**
84    * Constructor
85    * @param[in]  actor  The actor to check whether it is attached.
86    */
87   IsNotAttachedFunctor(Actor* actor)
88   : actorToCheck(actor)
89   {
90   }
91
92   /**
93    * Returns true if not attached, false if it is still attached.
94    * @param[in]  detector  The detector to check.
95    * @return true, if not attached, false otherwise.
96    */
97   bool operator()(const GestureDetector* detector) const
98   {
99     return !detector->IsAttached(*actorToCheck);
100   }
101
102   Actor* actorToCheck; ///< The actor to check whether it is attached or not.
103 };
104
105 } // unnamed namespace
106
107 LongPressGestureProcessor::LongPressGestureProcessor()
108 : GestureProcessor(GestureType::LONG_PRESS),
109   mLongPressGestureDetectors(),
110   mCurrentEmitters(),
111   mCurrentRenderTask(),
112   mMinTouchesRequired(1),
113   mMaxTouchesRequired(1),
114   mCurrentLongPressEvent(nullptr),
115   mMinimumHoldingTime(DEFAULT_MINIMUM_HOLDING_TIME)
116 {
117 }
118
119 LongPressGestureProcessor::~LongPressGestureProcessor() = default;
120
121 void LongPressGestureProcessor::Process(Scene& scene, const LongPressGestureEvent& longPressEvent)
122 {
123   DALI_TRACE_SCOPE(gTraceFilter, "DALI_PROCESS_LONG_PRESS_GESTURE");
124   switch(longPressEvent.state)
125   {
126     case GestureState::POSSIBLE:
127     {
128       mCurrentEmitters.clear();
129       ResetActor();
130
131       HitTestAlgorithm::Results hitTestResults;
132       if(HitTest(scene, longPressEvent.point, hitTestResults))
133       {
134         SetActor(&GetImplementation(hitTestResults.actor));
135       }
136       break;
137     }
138
139     case GestureState::STARTED:
140     {
141       Actor* currentGesturedActor = GetCurrentGesturedActor();
142       if(currentGesturedActor)
143       {
144         HitTestAlgorithm::Results hitTestResults;
145         HitTest(scene, longPressEvent.point, hitTestResults);
146
147         if(hitTestResults.actor && (currentGesturedActor == &GetImplementation(hitTestResults.actor)))
148         {
149           // Record the current render-task for Screen->Actor coordinate conversions
150           mCurrentRenderTask = hitTestResults.renderTask;
151
152           // Set mCurrentLongPressEvent to use inside overridden methods called from ProcessAndEmit()
153           mCurrentLongPressEvent = &longPressEvent;
154           ProcessAndEmit(hitTestResults);
155           mCurrentLongPressEvent = nullptr;
156         }
157         else
158         {
159           mCurrentEmitters.clear();
160           ResetActor();
161         }
162       }
163       break;
164     }
165
166     case GestureState::FINISHED:
167     {
168       // The gesture should only be sent to the gesture detector which first received it so that it
169       // can be told when the gesture ends as well.
170
171       // Only send subsequent long press gesture signals if we processed the gesture when it started.
172       // Check if actor is still touchable.
173
174       Actor* currentGesturedActor = GetCurrentGesturedActor();
175       if(currentGesturedActor)
176       {
177         if(currentGesturedActor->IsHittable() && !mCurrentEmitters.empty() && mCurrentRenderTask)
178         {
179           // Ensure actor is still attached to the emitters, if it is not then remove the emitter.
180           GestureDetectorContainer::iterator endIter = std::remove_if(mCurrentEmitters.begin(), mCurrentEmitters.end(), IsNotAttachedFunctor(currentGesturedActor));
181           mCurrentEmitters.erase(endIter, mCurrentEmitters.end());
182
183           if(!mCurrentEmitters.empty())
184           {
185             Vector2     actorCoords;
186             RenderTask& renderTaskImpl = *mCurrentRenderTask.Get();
187             currentGesturedActor->ScreenToLocal(renderTaskImpl, actorCoords.x, actorCoords.y, longPressEvent.point.x, longPressEvent.point.y);
188
189             EmitLongPressSignal(currentGesturedActor, mCurrentEmitters, longPressEvent, actorCoords);
190           }
191         }
192
193         // Clear current emitters and emitted actor
194         mCurrentEmitters.clear();
195         ResetActor();
196       }
197       break;
198     }
199
200     case GestureState::CANCELLED:
201     {
202       mCurrentEmitters.clear();
203       ResetActor();
204       break;
205     }
206
207     case GestureState::CONTINUING:
208     {
209       DALI_ABORT("Incorrect state received from Integration layer: CONTINUING\n");
210       break;
211     }
212
213     case GestureState::CLEAR:
214     {
215       DALI_ABORT("Incorrect state received from Integration layer: CLEAR\n");
216       break;
217     }
218   }
219 }
220
221 void LongPressGestureProcessor::AddGestureDetector(LongPressGestureDetector* gestureDetector, Scene& scene)
222 {
223   bool firstRegistration(mLongPressGestureDetectors.empty());
224
225   mLongPressGestureDetectors.push_back(gestureDetector);
226
227   if(firstRegistration)
228   {
229     mMinTouchesRequired = gestureDetector->GetMinimumTouchesRequired();
230     mMaxTouchesRequired = gestureDetector->GetMaximumTouchesRequired();
231
232     LongPressGestureRequest request;
233     request.minTouches = mMinTouchesRequired;
234     request.maxTouches = mMaxTouchesRequired;
235
236     Size size = scene.GetSize();
237
238     mGestureRecognizer = new LongPressGestureRecognizer(*this, Vector2(size.width, size.height), static_cast<const LongPressGestureRequest&>(request), mMinimumHoldingTime);
239   }
240   else
241   {
242     UpdateDetection();
243   }
244 }
245
246 void LongPressGestureProcessor::RemoveGestureDetector(LongPressGestureDetector* gestureDetector)
247 {
248   // Find detector ...
249   LongPressGestureDetectorContainer::iterator endIter = std::remove(mLongPressGestureDetectors.begin(), mLongPressGestureDetectors.end(), gestureDetector);
250   DALI_ASSERT_DEBUG(endIter != mLongPressGestureDetectors.end());
251
252   // ... and remove it
253   mLongPressGestureDetectors.erase(endIter, mLongPressGestureDetectors.end());
254
255   if(mLongPressGestureDetectors.empty())
256   {
257     mGestureRecognizer = nullptr;
258   }
259   else
260   {
261     UpdateDetection();
262   }
263 }
264
265 void LongPressGestureProcessor::GestureDetectorUpdated(LongPressGestureDetector* gestureDetector)
266 {
267   DALI_ASSERT_DEBUG(find(mLongPressGestureDetectors.begin(), mLongPressGestureDetectors.end(), gestureDetector) != mLongPressGestureDetectors.end());
268
269   UpdateDetection();
270 }
271
272 void LongPressGestureProcessor::SetMinimumHoldingTime(uint32_t time)
273 {
274   if(time > 0u && mMinimumHoldingTime != time)
275   {
276     mMinimumHoldingTime = time;
277
278     if(mGestureRecognizer)
279     {
280       LongPressGestureRecognizer* longPressRecognizer = dynamic_cast<LongPressGestureRecognizer*>(mGestureRecognizer.Get());
281       if(longPressRecognizer)
282       {
283         longPressRecognizer->SetMinimumHoldingTime(time);
284       }
285     }
286   }
287 }
288
289 uint32_t LongPressGestureProcessor::GetMinimumHoldingTime() const
290 {
291   return mMinimumHoldingTime;
292 }
293
294 void LongPressGestureProcessor::UpdateDetection()
295 {
296   DALI_ASSERT_DEBUG(!mLongPressGestureDetectors.empty());
297
298   unsigned int minimumRequired = UINT_MAX;
299   unsigned int maximumRequired = 0;
300
301   for(LongPressGestureDetectorContainer::iterator iter = mLongPressGestureDetectors.begin(), endIter = mLongPressGestureDetectors.end(); iter != endIter; ++iter)
302   {
303     LongPressGestureDetector* current(*iter);
304
305     if(current)
306     {
307       unsigned int minimum = current->GetMinimumTouchesRequired();
308       if(minimum < minimumRequired)
309       {
310         minimumRequired = minimum;
311       }
312
313       unsigned int maximum = current->GetMaximumTouchesRequired();
314       if(maximum > maximumRequired)
315       {
316         maximumRequired = maximum;
317       }
318     }
319   }
320
321   if((minimumRequired != mMinTouchesRequired) || (maximumRequired != mMaxTouchesRequired))
322   {
323     mMinTouchesRequired = minimumRequired;
324     mMaxTouchesRequired = maximumRequired;
325
326     LongPressGestureRequest request;
327     request.minTouches = mMinTouchesRequired;
328     request.maxTouches = mMaxTouchesRequired;
329     mGestureRecognizer->Update(request);
330   }
331 }
332
333 void LongPressGestureProcessor::OnGesturedActorStageDisconnection()
334 {
335   mCurrentEmitters.clear();
336 }
337
338 bool LongPressGestureProcessor::CheckGestureDetector(GestureDetector* detector, Actor* actor)
339 {
340   DALI_ASSERT_DEBUG(mCurrentLongPressEvent);
341
342   LongPressGestureDetector* longPressDetector(static_cast<LongPressGestureDetector*>(detector));
343
344   return (longPressDetector->GetMinimumTouchesRequired() <= mCurrentLongPressEvent->numberOfTouches) &&
345          (longPressDetector->GetMaximumTouchesRequired() >= mCurrentLongPressEvent->numberOfTouches);
346 }
347
348 void LongPressGestureProcessor::EmitGestureSignal(Actor* actor, const GestureDetectorContainer& gestureDetectors, Vector2 actorCoordinates)
349 {
350   DALI_ASSERT_DEBUG(mCurrentLongPressEvent);
351
352   mCurrentEmitters.clear();
353   ResetActor();
354
355   EmitLongPressSignal(actor, gestureDetectors, *mCurrentLongPressEvent, actorCoordinates);
356
357   if(actor->OnScene())
358   {
359     mCurrentEmitters = gestureDetectors;
360     SetActor(actor);
361   }
362 }
363
364 } // namespace Internal
365
366 } // namespace Dali