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