Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(571)

Side by Side Diff: components/viz/service/hit_test/hit_test_aggregator.cc

Issue 2938953002: Implement HitTestAggregator (Closed)
Patch Set: correct mojom include directory and improvements based on reviewer comments Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 #include "components/viz/service/hit_test/hit_test_aggregator.h"
6 #include "components/viz/common/hit_test/aggregated_hit_test_region.h"
7
8 namespace viz {
9
10 namespace {
11 // TODO(gklassen): Review and select appropriate sizes based on
12 // telemetry / UMA.
13 constexpr int kInitialSize = 1024;
14 constexpr int kIncrementalSize = 1024;
15 constexpr int kMaxRegionsPerSurface = 1024;
16 constexpr int kMaxSize = 100 * 1024;
17
18 bool ValidateHitTestRegion(const mojom::HitTestRegionPtr& hit_test_region) {
19 if (hit_test_region->flags == mojom::kHitTestChildSurface) {
20 if (!hit_test_region->surface_id.is_valid())
21 return false;
22 }
23
24 return true;
25 }
26
27 bool ValidateHitTestRegionList(
28 const mojom::HitTestRegionListPtr& hit_test_region_list) {
29 if (hit_test_region_list->regions.size() > kMaxRegionsPerSurface)
30 return false;
31 for (auto& region : hit_test_region_list->regions) {
32 if (!ValidateHitTestRegion(region))
33 return false;
34 }
35 return true;
36 }
37
38 } // namespace
39
40 HitTestAggregator::HitTestAggregator() : weak_ptr_factory_(this) {
41 AllocateHitTestRegionArray();
42 }
43
44 HitTestAggregator::~HitTestAggregator() = default;
45
46 void HitTestAggregator::SubmitHitTestRegionList(
47 mojom::HitTestRegionListPtr hit_test_region_list) {
48 DCHECK(ValidateHitTestRegionList(hit_test_region_list));
49 // TODO(gklassen): Runtime validation that hit_test_region_list is valid.
50 // TODO(gklassen): Inform FrameSink that the hit_test_region_list is invalid.
51 // TODO(gklassen): FrameSink needs to inform the host of a difficult renderer.
52 pending_[hit_test_region_list->surface_id] = std::move(hit_test_region_list);
53 }
54
55 bool HitTestAggregator::OnSurfaceDamaged(const SurfaceId& surface_id,
56 const cc::BeginFrameAck& ack) {
57 return false;
58 }
59
60 void HitTestAggregator::OnSurfaceDiscarded(const SurfaceId& surface_id) {
61 // Update the region count.
62 auto active_search = active_.find(surface_id);
63 if (active_search != active_.end()) {
64 mojom::HitTestRegionList* old_hit_test_data = active_search->second.get();
65 active_region_count_ -= old_hit_test_data->regions.size();
66 }
67 DCHECK_GE(active_region_count_, 0);
68
69 pending_.erase(surface_id);
70 active_.erase(surface_id);
71 }
72
73 void HitTestAggregator::OnSurfaceWillDraw(const SurfaceId& surface_id) {
74 auto pending_search = pending_.find(surface_id);
75 if (pending_search == pending_.end()) {
76 // Have already activated pending hit_test_region_list objects for this
77 // surface.
78 return;
79 }
80 mojom::HitTestRegionList* hit_test_region_list = pending_search->second.get();
81
82 // Update the region count.
83 auto active_search = active_.find(surface_id);
84 if (active_search != active_.end()) {
85 mojom::HitTestRegionList* old_hit_test_data = active_search->second.get();
86 active_region_count_ -= old_hit_test_data->regions.size();
87 }
88 active_region_count_ += hit_test_region_list->regions.size();
89 DCHECK_GE(active_region_count_, 0);
90
91 active_[surface_id] = std::move(pending_[surface_id]);
92 pending_.erase(surface_id);
93 }
94
95 void HitTestAggregator::AllocateHitTestRegionArray() {
96 AllocateHitTestRegionArray(kInitialSize);
97 Swap();
98 AllocateHitTestRegionArray(kInitialSize);
99 }
100
101 void HitTestAggregator::AllocateHitTestRegionArray(int size) {
102 size_t num_bytes = size * sizeof(AggregatedHitTestRegion);
103 write_handle_ = mojo::SharedBufferHandle::Create(num_bytes);
104 write_size_ = size;
105 write_buffer_ = write_handle_->Map(num_bytes);
106
107 AggregatedHitTestRegion* region =
108 (AggregatedHitTestRegion*)write_buffer_.get();
109 region[0].child_count = kEndOfList;
110 }
111
112 void HitTestAggregator::PostTaskAggregate(SurfaceId display_surface_id) {
113 base::ThreadTaskRunnerHandle::Get()->PostTask(
114 FROM_HERE,
115 base::BindOnce(&HitTestAggregator::Aggregate,
116 weak_ptr_factory_.GetWeakPtr(), display_surface_id));
117 }
118
119 void HitTestAggregator::Aggregate(SurfaceId display_surface_id) {
120 // Check to ensure that enough memory has been allocated.
121 int size = write_size_;
122 int max_size = active_region_count_ + active_.size() + 1;
123 if (max_size > kMaxSize)
124 max_size = kMaxSize;
125
126 if (max_size > size) {
127 size = (1 + max_size / kIncrementalSize) * kIncrementalSize;
128 AllocateHitTestRegionArray(size);
129 }
130
131 AppendRoot(display_surface_id);
132 }
133
134 void HitTestAggregator::AppendRoot(SurfaceId surface_id) {
135 auto search = active_.find(surface_id);
136 DCHECK(search != active_.end());
137
138 mojom::HitTestRegionList* hit_test_region_list = search->second.get();
139
140 AggregatedHitTestRegion* regions =
141 static_cast<AggregatedHitTestRegion*>(write_buffer_.get());
142
143 regions[0].frame_sink_id = hit_test_region_list->surface_id.frame_sink_id();
144 regions[0].flags = hit_test_region_list->flags;
145 regions[0].rect = hit_test_region_list->bounds;
146 regions[0].transform = hit_test_region_list->transform;
147
148 int region_index = 1;
149 for (const auto& region : hit_test_region_list->regions) {
150 if (region_index >= write_size_ - 1)
151 break;
152 region_index = AppendRegion(regions, region_index, region);
153 }
154
155 DCHECK_GE(region_index, 1);
156 regions[0].child_count = region_index - 1;
157 regions[region_index].child_count = kEndOfList;
158 }
159
160 int HitTestAggregator::AppendRegion(AggregatedHitTestRegion* regions,
161 int region_index,
162 const mojom::HitTestRegionPtr& region) {
163 AggregatedHitTestRegion* element = &regions[region_index];
164
165 element->frame_sink_id = region->surface_id.frame_sink_id();
166 element->flags = region->flags;
167 element->rect = region->rect;
168 element->transform = region->transform;
169
170 int parent_index = region_index++;
171 if (region_index >= write_size_ - 1) {
172 element->child_count = 0;
173 return region_index;
174 }
175
176 if (region->flags == mojom::kHitTestChildSurface) {
177 auto search = active_.find(region->surface_id);
178 if (search == active_.end()) {
179 // Surface HitTestRegionList not found - it may be late.
180 // Don't include this region so that it doesn't receive events.
181 return parent_index;
182 }
183
184 // Rather than add a node in the tree for this hit_test_region_list element
185 // we can simplify the tree by merging the flags and transform into
186 // the kHitTestChildSurface element.
187 mojom::HitTestRegionList* hit_test_region_list = search->second.get();
188 if (!hit_test_region_list->transform.IsIdentity())
189 element->transform.PreconcatTransform(hit_test_region_list->transform);
190
191 element->flags |= hit_test_region_list->flags;
192
193 for (const auto& child_region : hit_test_region_list->regions) {
194 region_index = AppendRegion(regions, region_index, child_region);
195 if (region_index >= write_size_ - 1)
196 break;
197 }
198 }
199 DCHECK_GE(region_index - parent_index - 1, 0);
200 element->child_count = region_index - parent_index - 1;
201 return region_index;
202 }
203
204 void HitTestAggregator::Swap() {
205 using std::swap;
206
207 swap(read_handle_, write_handle_);
208 swap(read_size_, write_size_);
209 swap(read_buffer_, write_buffer_);
210 }
211
212 } // namespace viz
OLDNEW
« no previous file with comments | « components/viz/service/hit_test/hit_test_aggregator.h ('k') | components/viz/service/hit_test/hit_test_aggregator_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698