Hit-test can hit more than 32 depth clipping actor.
[platform/core/uifw/dali-core.git] / dali / internal / event / events / hit-test-algorithm-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/events/hit-test-algorithm-impl.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <dali/internal/event/actors/actor-impl.h>
24 #include <dali/internal/event/actors/camera-actor-impl.h>
25 #include <dali/internal/event/actors/layer-impl.h>
26 #include <dali/internal/event/actors/layer-list.h>
27 #include <dali/internal/event/common/projection.h>
28 #include <dali/internal/event/events/ray-test.h>
29 #include <dali/internal/event/render-tasks/render-task-impl.h>
30 #include <dali/internal/event/render-tasks/render-task-list-impl.h>
31 #include <dali/internal/event/rendering/renderer-impl.h>
32 #include <dali/public-api/actors/layer.h>
33 #include <dali/public-api/math/vector2.h>
34 #include <dali/public-api/math/vector4.h>
35
36 namespace Dali
37 {
38 namespace Internal
39 {
40 namespace HitTestAlgorithm
41 {
42 namespace
43 {
44 struct HitActor
45 {
46   HitActor()
47   : actor(nullptr),
48     distance(std::numeric_limits<float>::max()),
49     depth(std::numeric_limits<int>::min())
50   {
51   }
52
53   Actor*  actor;       ///< The actor hit (if actor is hit, then this is initialised).
54   Vector2 hitPosition; ///< Position of hit (only valid if actor valid).
55   float   distance;    ///< Distance from ray origin to hit actor.
56   int32_t depth;       ///< Depth index of this actor.
57 };
58
59 /**
60  * Creates an Actor handle so that a HitTestFunction provided via the public API can be called.
61  */
62 struct HitTestFunctionWrapper : public HitTestInterface
63 {
64   /**
65    * Constructor
66    *
67    * @param[in] func HitTestFunction to call with an Actor handle.
68    */
69   HitTestFunctionWrapper(Dali::HitTestAlgorithm::HitTestFunction func)
70   : mFunc(func)
71   {
72   }
73
74   bool IsActorHittable(Actor* actor) override
75   {
76     return mFunc(Dali::Actor(actor), Dali::HitTestAlgorithm::CHECK_ACTOR);
77   }
78
79   bool DescendActorHierarchy(Actor* actor) override
80   {
81     return mFunc(Dali::Actor(actor), Dali::HitTestAlgorithm::DESCEND_ACTOR_TREE);
82   }
83
84   bool DoesLayerConsumeHit(Layer* layer) override
85   {
86     // Layer::IsTouchConsumed() focuses on touch only. Here we are a wrapper for the public-api
87     // where the caller may want to check for something completely different.
88     // TODO: Should provide a means to let caller decide. For now do not allow layers to consume
89     return false;
90   }
91
92   bool ActorRequiresHitResultCheck(Actor* actor, Integration::Point point, Vector2 hitPointLocal, uint32_t timeStamp) override
93   {
94     return actor->EmitHitTestResultSignal(point, hitPointLocal, timeStamp);
95   }
96
97   Dali::HitTestAlgorithm::HitTestFunction mFunc;
98 };
99
100 /**
101  * Used in the hit-test algorithm to check whether the actor is touchable.
102  * It is used by the touch event processor.
103  */
104 struct ActorTouchableCheck : public HitTestInterface
105 {
106   bool IsActorHittable(Actor* actor) override
107   {
108     return (actor->GetTouchRequired() || actor->GetInterceptTouchRequired() || actor->IsTouchFocusable()) && // Does the Application or derived actor type require a touch event or a intercept touch event? or focusable by touch?
109            actor->IsHittable();                                                                              // Is actor sensitive, visible and on the scene?
110   }
111
112   bool DescendActorHierarchy(Actor* actor) override
113   {
114     return actor->IsVisible() && // Actor is visible, if not visible then none of its children are visible.
115            actor->IsSensitive(); // Actor is sensitive, if insensitive none of its children should be hittable either.
116   }
117
118   bool DoesLayerConsumeHit(Layer* layer) override
119   {
120     return layer->IsTouchConsumed();
121   }
122
123   bool ActorRequiresHitResultCheck(Actor* actor, Integration::Point point, Vector2 hitPointLocal, uint32_t timeStamp) override
124   {
125     return actor->EmitHitTestResultSignal(point, hitPointLocal, timeStamp);
126   }
127 };
128
129 /**
130  * Check to see if the actor we're about to hit test is exclusively owned by another rendertask?
131  */
132 bool IsActorExclusiveToAnotherRenderTask(const Actor&                               actor,
133                                          const RenderTask&                          renderTask,
134                                          const RenderTaskList::ExclusivesContainer& exclusives)
135
136 {
137   if(exclusives.size())
138   {
139     for(const auto& exclusive : exclusives)
140     {
141       if((exclusive.renderTaskPtr != &renderTask) && (exclusive.actor.GetActor() == &actor))
142       {
143         return true;
144       }
145     }
146   }
147   return false;
148 }
149
150 /**
151  * Recursively hit test all the actors, without crossing into other layers.
152  * This algorithm performs a Depth-First-Search (DFS) on all Actors within Layer.
153  * Hit-Testing each Actor, noting the distance from the Ray-Origin (3D origin
154  * of touch vector). The closest Hit-Tested Actor is that which is returned.
155  * Exceptions to this rule are:
156  * - When comparing against renderable parents, if Actor is the same distance
157  * or closer than it's renderable parent, then it takes priority.
158  */
159 HitActor HitTestWithinLayer(Actor&                                     actor,
160                             const RenderTask&                          renderTask,
161                             const RenderTaskList::ExclusivesContainer& exclusives,
162                             const Vector4&                             rayOrigin,
163                             const Vector4&                             rayDir,
164                             const float&                               nearClippingPlane,
165                             const float&                               farClippingPlane,
166                             HitTestInterface&                          hitCheck,
167                             const bool&                                overlayed,
168                             bool&                                      overlayHit,
169                             bool                                       layerIs3d,
170                             const RayTest&                             rayTest,
171                             const Integration::Point&                  point,
172                             const uint32_t                             eventTime)
173 {
174   HitActor hit;
175
176   if(IsActorExclusiveToAnotherRenderTask(actor, renderTask, exclusives))
177   {
178     return hit;
179   }
180
181   // For clipping, regardless of whether we have hit this actor or not.
182   // This is used later to ensure all nested clipped children have hit
183   // all clipping actors also for them to be counted as hit.
184   bool clippingActor  = actor.GetClippingMode() != ClippingMode::DISABLED;
185   bool overlayedActor = overlayed || actor.IsOverlay();
186
187   // If we are a clipping actor or hittable...
188   if(clippingActor || hitCheck.IsActorHittable(&actor))
189   {
190     Vector3 size(actor.GetCurrentSize());
191
192     // Ensure the actor has a valid size.
193     // If so, perform a quick ray sphere test to see if our ray is close to the actor.
194     if(size.x > 0.0f && size.y > 0.0f && rayTest.SphereTest(actor, rayOrigin, rayDir))
195     {
196       Vector2 hitPointLocal;
197       float   distance;
198
199       // Finally, perform a more accurate ray test to see if our ray actually hits the actor.
200       if(rayTest.ActorTest(actor, rayOrigin, rayDir, hitPointLocal, distance))
201       {
202         // Calculate z coordinate value in Camera Space.
203         const Matrix&  viewMatrix          = renderTask.GetCameraActor()->GetViewMatrix();
204         const Vector4& hitDir              = Vector4(rayDir.x * distance, rayDir.y * distance, rayDir.z * distance, 0.0f);
205         const float    cameraDepthDistance = (viewMatrix * hitDir).z;
206
207         // Check if cameraDepthDistance is between clipping plane
208         if(cameraDepthDistance >= nearClippingPlane && cameraDepthDistance <= farClippingPlane)
209         {
210           if(overlayHit && !overlayedActor)
211           {
212             // If we have already hit an overlay and current actor is not an overlay ignore current actor.
213           }
214           else
215           {
216             if(overlayedActor)
217             {
218               overlayHit = true;
219             }
220
221             // If the hit actor does not want to hit, the hit-test continues.
222             if(hitCheck.ActorRequiresHitResultCheck(&actor, point, hitPointLocal, eventTime))
223             {
224               hit.actor       = &actor;
225               hit.hitPosition = hitPointLocal;
226               hit.distance    = distance;
227               hit.depth       = actor.GetSortingDepth();
228
229               if(actor.GetRendererCount() > 0)
230               {
231                 //Get renderer with maximum depth
232                 int rendererMaxDepth(actor.GetRendererAt(0).Get()->GetDepthIndex());
233                 for(uint32_t i(1); i < actor.GetRendererCount(); ++i)
234                 {
235                   int depth = actor.GetRendererAt(i).Get()->GetDepthIndex();
236                   if(depth > rendererMaxDepth)
237                   {
238                     rendererMaxDepth = depth;
239                   }
240                 }
241                 hit.depth += rendererMaxDepth;
242               }
243             }
244           }
245         }
246       }
247     }
248   }
249
250   // If current actor is clipping, and hit failed, We should not checkup child actors. Fast return
251   if(clippingActor && !(hit.actor))
252   {
253     return hit;
254   }
255
256   // Find a child hit, until we run out of actors in the current layer.
257   HitActor childHit;
258   if(actor.GetChildCount() > 0)
259   {
260     childHit.distance        = std::numeric_limits<float>::max();
261     childHit.depth           = std::numeric_limits<int32_t>::min();
262     ActorContainer& children = actor.GetChildrenInternal();
263
264     // Hit test ALL children and calculate their distance.
265     bool parentIsRenderable = actor.IsRenderable();
266
267     for(ActorIter iter = children.begin(), endIter = children.end(); iter != endIter; ++iter)
268     {
269       // Descend tree only if...
270       if(!(*iter)->IsLayer() &&                           // Child is NOT a layer, hit testing current layer only
271          (hitCheck.DescendActorHierarchy((*iter).Get()))) // We can descend into child hierarchy
272       {
273         HitActor currentHit(HitTestWithinLayer((*iter->Get()),
274                                                renderTask,
275                                                exclusives,
276                                                rayOrigin,
277                                                rayDir,
278                                                nearClippingPlane,
279                                                farClippingPlane,
280                                                hitCheck,
281                                                overlayedActor,
282                                                overlayHit,
283                                                layerIs3d,
284                                                rayTest,
285                                                point,
286                                                eventTime));
287
288         // Make sure the set hit actor is actually hittable. This is usually required when we have some
289         // clipping as we need to hit-test all actors as we descend the tree regardless of whether they
290         // are hittable or not.
291         if(currentHit.actor && (!hitCheck.IsActorHittable(currentHit.actor)))
292         {
293           continue;
294         }
295
296         bool updateChildHit = false;
297         if(currentHit.distance >= 0.0f)
298         {
299           if(layerIs3d)
300           {
301             updateChildHit = ((currentHit.depth > childHit.depth) ||
302                               ((currentHit.depth == childHit.depth) && (currentHit.distance < childHit.distance)));
303           }
304           else
305           {
306             updateChildHit = currentHit.depth >= childHit.depth;
307           }
308         }
309
310         if(updateChildHit)
311         {
312           if(!parentIsRenderable || currentHit.depth > hit.depth ||
313              (layerIs3d && (currentHit.depth == hit.depth && currentHit.distance < hit.distance)))
314           {
315             childHit = currentHit;
316           }
317         }
318       }
319     }
320   }
321
322   return (childHit.actor) ? childHit : hit;
323 }
324
325 /**
326  * Return true if actor is sourceActor or a descendent of sourceActor
327  */
328 bool IsWithinSourceActors(const Actor& sourceActor, const Actor& actor)
329 {
330   if(&sourceActor == &actor)
331   {
332     return true;
333   }
334
335   Actor* parent = actor.GetParent();
336   if(parent)
337   {
338     return IsWithinSourceActors(sourceActor, *parent);
339   }
340
341   // Not within source actors
342   return false;
343 }
344
345 /**
346  * Returns true if the layer and all of the layer's parents are visible and sensitive.
347  */
348 inline bool IsActuallyHittable(Layer& layer, const Vector2& screenCoordinates, const Vector2& stageSize, HitTestInterface& hitCheck)
349 {
350   bool hittable(true);
351
352   if(layer.IsClipping())
353   {
354     ClippingBox box = layer.GetClippingBox();
355
356     if(screenCoordinates.x < static_cast<float>(box.x) ||
357        screenCoordinates.x > static_cast<float>(box.x + box.width) ||
358        screenCoordinates.y < stageSize.y - static_cast<float>(box.y + box.height) ||
359        screenCoordinates.y > stageSize.y - static_cast<float>(box.y))
360     {
361       // Not touchable if clipping is enabled in the layer and the screen coordinate is outside the clip region.
362       hittable = false;
363     }
364   }
365
366   if(hittable)
367   {
368     Actor* actor(&layer);
369
370     // Ensure that we can descend into the layer's (or any of its parent's) hierarchy.
371     while(actor && hittable)
372     {
373       if(!hitCheck.DescendActorHierarchy(actor))
374       {
375         hittable = false;
376         break;
377       }
378       actor = actor->GetParent();
379     }
380   }
381
382   return hittable;
383 }
384
385 /**
386  * Gets the near and far clipping planes of the camera from which the scene is viewed in the render task.
387  */
388 void GetCameraClippingPlane(RenderTask& renderTask, float& nearClippingPlane, float& farClippingPlane)
389 {
390   CameraActor* cameraActor = renderTask.GetCameraActor();
391   nearClippingPlane        = cameraActor->GetNearClippingPlane();
392   farClippingPlane         = cameraActor->GetFarClippingPlane();
393 }
394
395 /**
396  * Hit test a RenderTask
397  */
398 bool HitTestRenderTask(const RenderTaskList::ExclusivesContainer& exclusives,
399                        const Vector2&                             sceneSize,
400                        LayerList&                                 layers,
401                        RenderTask&                                renderTask,
402                        Vector2                                    screenCoordinates,
403                        Results&                                   results,
404                        HitTestInterface&                          hitCheck,
405                        const RayTest&                             rayTest)
406 {
407   if(renderTask.IsHittable(screenCoordinates))
408   {
409     Viewport viewport;
410     renderTask.GetViewport(viewport);
411     if(screenCoordinates.x < static_cast<float>(viewport.x) ||
412        screenCoordinates.x > static_cast<float>(viewport.x + viewport.width) ||
413        screenCoordinates.y < static_cast<float>(viewport.y) ||
414        screenCoordinates.y > static_cast<float>(viewport.y + viewport.height))
415     {
416       // The screen coordinate is outside the viewport of render task. The viewport clips all layers.
417       return false;
418     }
419
420     float nearClippingPlane, farClippingPlane;
421     GetCameraClippingPlane(renderTask, nearClippingPlane, farClippingPlane);
422
423     // Determine the layer depth of the source actor
424     Actor* sourceActor(renderTask.GetSourceActor());
425     if(sourceActor)
426     {
427       Dali::Layer layer(sourceActor->GetLayer());
428       if(layer)
429       {
430         const uint32_t sourceActorDepth(layer.GetProperty<bool>(Dali::Layer::Property::DEPTH));
431
432         CameraActor* cameraActor     = renderTask.GetCameraActor();
433         bool         pickingPossible = cameraActor->BuildPickingRay(
434           screenCoordinates,
435           viewport,
436           results.rayOrigin,
437           results.rayDirection);
438         if(!pickingPossible)
439         {
440           return false;
441         }
442
443         // Hit test starting with the top layer, working towards the bottom layer.
444         HitActor hit;
445         bool     overlayHit       = false;
446         bool     layerConsumesHit = false;
447
448         for(int32_t i = layers.GetLayerCount() - 1; i >= 0 && !(hit.actor); --i)
449         {
450           Layer* layer(layers.GetLayer(i));
451           overlayHit = false;
452
453           // Ensure layer is touchable (also checks whether ancestors are also touchable)
454           if(IsActuallyHittable(*layer, screenCoordinates, sceneSize, hitCheck))
455           {
456             // Always hit-test the source actor; otherwise test whether the layer is below the source actor in the hierarchy
457             if(sourceActorDepth == static_cast<uint32_t>(i))
458             {
459               // Recursively hit test the source actor & children, without crossing into other layers.
460               hit = HitTestWithinLayer(*sourceActor,
461                                        renderTask,
462                                        exclusives,
463                                        results.rayOrigin,
464                                        results.rayDirection,
465                                        nearClippingPlane,
466                                        farClippingPlane,
467                                        hitCheck,
468                                        overlayHit,
469                                        overlayHit,
470                                        layer->GetBehavior() == Dali::Layer::LAYER_3D,
471                                        rayTest,
472                                        results.point,
473                                        results.eventTime);
474             }
475             else if(IsWithinSourceActors(*sourceActor, *layer))
476             {
477               // Recursively hit test all the actors, without crossing into other layers.
478               hit = HitTestWithinLayer(*layer,
479                                        renderTask,
480                                        exclusives,
481                                        results.rayOrigin,
482                                        results.rayDirection,
483                                        nearClippingPlane,
484                                        farClippingPlane,
485                                        hitCheck,
486                                        overlayHit,
487                                        overlayHit,
488                                        layer->GetBehavior() == Dali::Layer::LAYER_3D,
489                                        rayTest,
490                                        results.point,
491                                        results.eventTime);
492             }
493
494             // If this layer is set to consume the hit, then do not check any layers behind it
495             if(hitCheck.DoesLayerConsumeHit(layer))
496             {
497               layerConsumesHit = true;
498               break;
499             }
500           }
501         }
502
503         if(hit.actor)
504         {
505           results.renderTask       = RenderTaskPtr(&renderTask);
506           results.actor            = Dali::Actor(hit.actor);
507           results.actorCoordinates = hit.hitPosition;
508
509           return true; // Success
510         }
511
512         if(layerConsumesHit)
513         {
514           return true; // Also success if layer is consuming the hit
515         }
516       }
517     }
518   }
519   return false;
520 }
521
522 /**
523  * Iterate through the RenderTaskList and perform hit testing.
524  *
525  * @param[in] sceneSize The scene size the tests will be performed in
526  * @param[in] layers The list of layers to test
527  * @param[in] taskList The list of render tasks
528  * @param[out] results Ray information calculated by the camera
529  * @param[in] hitCheck The hit testing interface object to use
530  * @param[in] onScreen True to test on-screen, false to test off-screen
531  * @return True if we have a hit, false otherwise
532  */
533 bool HitTestRenderTaskList(const Vector2&    sceneSize,
534                            LayerList&        layers,
535                            RenderTaskList&   taskList,
536                            const Vector2&    screenCoordinates,
537                            Results&          results,
538                            HitTestInterface& hitCheck,
539                            bool              onScreen)
540 {
541   RenderTaskList::RenderTaskContainer&                  tasks      = taskList.GetTasks();
542   RenderTaskList::RenderTaskContainer::reverse_iterator endIter    = tasks.rend();
543   const auto&                                           exclusives = taskList.GetExclusivesList();
544   RayTest                                               rayTest;
545
546   for(RenderTaskList::RenderTaskContainer::reverse_iterator iter = tasks.rbegin(); endIter != iter; ++iter)
547   {
548     RenderTask& renderTask            = *iter->Get();
549     const bool  isOffscreenRenderTask = renderTask.GetFrameBuffer();
550     if((onScreen && isOffscreenRenderTask) || (!onScreen && !isOffscreenRenderTask))
551     {
552       // Skip to next task
553       continue;
554     }
555     if(HitTestRenderTask(exclusives, sceneSize, layers, renderTask, screenCoordinates, results, hitCheck, rayTest))
556     {
557       // Return true when an actor is hit (or layer in our render-task consumes the hit)
558       return true; // don't bother checking off screen tasks
559     }
560   }
561
562   return false;
563 }
564
565 /**
566  * Iterate through the RenderTaskList and perform hit testing for both on-screen and off-screen.
567  *
568  * @param[in] sceneSize The scene size the tests will be performed in
569  * @param[in] layers The list of layers to test
570  * @param[in] taskList The list of render tasks
571  * @param[out] results Ray information calculated by the camera
572  * @param[in] hitCheck The hit testing interface object to use
573  * @param[in] onScreen True to test on-screen, false to test off-screen
574  * @return True if we have a hit, false otherwise
575  */
576 bool HitTestForEachRenderTask(const Vector2&    sceneSize,
577                               LayerList&        layers,
578                               RenderTaskList&   taskList,
579                               const Vector2&    screenCoordinates,
580                               Results&          results,
581                               HitTestInterface& hitCheck)
582 {
583   bool result = false;
584
585   // Check on-screen tasks before off-screen ones.
586   // Hit test order should be reverse of draw order (see ProcessRenderTasks() where off-screen tasks are drawn first).
587   if(HitTestRenderTaskList(sceneSize, layers, taskList, screenCoordinates, results, hitCheck, true) ||
588      HitTestRenderTaskList(sceneSize, layers, taskList, screenCoordinates, results, hitCheck, false))
589   {
590     // Found hit.
591     result = true;
592   }
593
594   return result;
595 }
596
597 } // unnamed namespace
598
599 HitTestInterface::~HitTestInterface() = default;
600
601 bool HitTest(const Vector2& sceneSize, RenderTaskList& taskList, LayerList& layerList, const Vector2& screenCoordinates, Dali::HitTestAlgorithm::Results& results, Dali::HitTestAlgorithm::HitTestFunction func)
602 {
603   bool wasHit(false);
604   // Hit-test the regular on-scene actors
605   Results                hitTestResults;
606   HitTestFunctionWrapper hitTestFunctionWrapper(func);
607   if(HitTestForEachRenderTask(sceneSize, layerList, taskList, screenCoordinates, hitTestResults, hitTestFunctionWrapper))
608   {
609     results.actor            = hitTestResults.actor;
610     results.actorCoordinates = hitTestResults.actorCoordinates;
611     wasHit                   = true;
612   }
613   return wasHit;
614 }
615
616 bool HitTest(const Vector2& sceneSize, RenderTaskList& renderTaskList, LayerList& layerList, const Vector2& screenCoordinates, Results& results, HitTestInterface& hitTestInterface)
617 {
618   bool wasHit(false);
619
620   // Hit-test the regular on-scene actors
621   if(!wasHit)
622   {
623     wasHit = HitTestForEachRenderTask(sceneSize, layerList, renderTaskList, screenCoordinates, results, hitTestInterface);
624   }
625   return wasHit;
626 }
627
628 bool HitTest(const Vector2& sceneSize, RenderTaskList& renderTaskList, LayerList& layerList, const Vector2& screenCoordinates, Results& results)
629 {
630   ActorTouchableCheck actorTouchableCheck;
631   return HitTest(sceneSize, renderTaskList, layerList, screenCoordinates, results, actorTouchableCheck);
632 }
633
634 } // namespace HitTestAlgorithm
635
636 } // namespace Internal
637
638 } // namespace Dali