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

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

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

Powered by Google App Engine
This is Rietveld 408576698