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