1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef COMPONENTS_VIZ_SERVICE_HIT_TEST_HIT_TEST_MANAGER_H_
6 #define COMPONENTS_VIZ_SERVICE_HIT_TEST_HIT_TEST_MANAGER_H_
7 
8 #include "base/optional.h"
9 #include "base/timer/elapsed_timer.h"
10 #include "components/viz/common/hit_test/aggregated_hit_test_region.h"
11 #include "components/viz/common/surfaces/surface_id.h"
12 #include "components/viz/service/surfaces/surface_manager.h"
13 #include "components/viz/service/surfaces/surface_observer.h"
14 #include "components/viz/service/viz_service_export.h"
15 #include "services/viz/public/mojom/hit_test/hit_test_region_list.mojom.h"
16 
17 namespace viz {
18 
19 class LatestLocalSurfaceIdLookupDelegate;
20 
21 // HitTestManager manages the collection of HitTestRegionList objects
22 // submitted in calls to SubmitCompositorFrame.  This collection is
23 // used by HitTestAggregator.
24 class VIZ_SERVICE_EXPORT HitTestManager : public SurfaceObserver {
25  public:
26   explicit HitTestManager(SurfaceManager* surface_manager);
27   ~HitTestManager() override;
28 
29   // SurfaceObserver:
OnFirstSurfaceActivation(const SurfaceInfo & surface_info)30   void OnFirstSurfaceActivation(const SurfaceInfo& surface_info) override {}
31   void OnSurfaceActivated(const SurfaceId& surface_id) override;
OnSurfaceMarkedForDestruction(const SurfaceId & surface_id)32   void OnSurfaceMarkedForDestruction(const SurfaceId& surface_id) override {}
33   bool OnSurfaceDamaged(const SurfaceId& surface_id,
34                         const BeginFrameAck& ack) override;
35   void OnSurfaceDestroyed(const SurfaceId& surface_id) override;
OnSurfaceDamageExpected(const SurfaceId & surface_id,const BeginFrameArgs & args)36   void OnSurfaceDamageExpected(const SurfaceId& surface_id,
37                                const BeginFrameArgs& args) override {}
38 
39   // Called when HitTestRegionList is submitted along with every call
40   // to SubmitCompositorFrame.
41   void SubmitHitTestRegionList(
42       const SurfaceId& surface_id,
43       const uint64_t frame_index,
44       base::Optional<HitTestRegionList> hit_test_region_list);
45 
46   // Returns the HitTestRegionList corresponding to the given
47   // |frame_sink_id| and the active CompositorFrame matched by frame_index.
48   // The returned pointer is not stable and should not be stored or used after
49   // calling any non-const methods on this class. ActiveFrameIndex is stored
50   // if |store_active_frame_index| is given, which is used to detect updates.
51   const HitTestRegionList* GetActiveHitTestRegionList(
52       LatestLocalSurfaceIdLookupDelegate* delegate,
53       const FrameSinkId& frame_sink_id,
54       uint64_t* store_active_frame_index = nullptr) const;
55 
56   int64_t GetTraceId(const SurfaceId& id) const;
57 
58   const base::flat_set<FrameSinkId>* GetHitTestAsyncQueriedDebugRegions(
59       const FrameSinkId& root_frame_sink_id) const;
60   void SetHitTestAsyncQueriedDebugRegions(
61       const FrameSinkId& root_frame_sink_id,
62       const std::vector<FrameSinkId>& hit_test_async_queried_debug_queue);
63 
submit_hit_test_region_list_index()64   uint64_t submit_hit_test_region_list_index() const {
65     return submit_hit_test_region_list_index_;
66   }
SetNeedsSubmit()67   void SetNeedsSubmit() { submit_hit_test_region_list_index_++; }
68 
69  private:
70   bool ValidateHitTestRegionList(const SurfaceId& surface_id,
71                                  HitTestRegionList* hit_test_region_list);
72 
73   SurfaceManager* const surface_manager_;
74 
75   std::map<SurfaceId, base::flat_map<uint64_t, HitTestRegionList>>
76       hit_test_region_lists_;
77 
78   struct HitTestAsyncQueriedDebugRegion {
79     HitTestAsyncQueriedDebugRegion();
80     explicit HitTestAsyncQueriedDebugRegion(
81         base::flat_set<FrameSinkId> regions);
82     ~HitTestAsyncQueriedDebugRegion();
83 
84     HitTestAsyncQueriedDebugRegion(HitTestAsyncQueriedDebugRegion&&);
85     HitTestAsyncQueriedDebugRegion& operator=(HitTestAsyncQueriedDebugRegion&&);
86 
87     base::flat_set<FrameSinkId> regions;
88     base::ElapsedTimer timer;
89   };
90 
91   // We store the async queried regions for each |root_frame_sink_id|. If viz
92   // hit-test debug is enabled, We will highlight the regions red in
93   // HitTestAggregator for 2 seconds, or until the next async queried event.
94   base::flat_map<FrameSinkId, HitTestAsyncQueriedDebugRegion>
95       hit_test_async_queried_debug_regions_;
96 
97   // Keeps track of the number of submitted HitTestRegionLists. This allows the
98   // HitTestAggregators to stay in sync with the HitTestManager and only
99   // aggregate when there is new hit-test data.
100   uint64_t submit_hit_test_region_list_index_ = 0;
101 
102   DISALLOW_COPY_AND_ASSIGN(HitTestManager);
103 };
104 
105 }  // namespace viz
106 
107 #endif  // COMPONENTS_VIZ_SERVICE_HIT_TEST_HIT_TEST_MANAGER_H_
108