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/hit_test/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 | |
rjkroege
2017/06/27 00:00:07
same nit about spaces and (
gklassen
2017/06/27 21:46:32
Done.
| |
13 // telemetry / UMA? ). | |
14 constexpr int kInitialSize = 1024; | |
15 constexpr int kIncrementalSize = 1024; | |
16 | |
17 bool ValidateHitTestRegion( | |
18 const hit_test::mojom::HitTestRegionPtr& hit_test_region) { | |
19 if (hit_test_region->flags == hit_test::mojom::kHitTestChildSurface) { | |
20 if (!hit_test_region->surface_id.is_valid()) | |
21 return false; | |
22 } | |
23 return true; | |
24 } | |
25 bool ValidateHitTestData(const hit_test::mojom::HitTestDataPtr& hit_test_data) { | |
26 for (auto& region : hit_test_data->regions) { | |
27 if (!ValidateHitTestRegion(region)) { | |
28 return false; | |
29 } | |
30 } | |
31 return true; | |
32 } | |
33 | |
34 } // namespace | |
35 | |
36 HitTestAggregator::HitTestAggregator( | |
37 std::unique_ptr<DisplayHitTestDataFactory> display_hit_test_data_factory) | |
38 : active_region_count_(0), | |
39 display_hit_test_data_factory_(std::move(display_hit_test_data_factory)), | |
40 weak_ptr_factory_(this) { | |
41 AllocateDisplayHitTestData(); | |
42 } | |
43 HitTestAggregator::~HitTestAggregator() {} | |
44 | |
45 void HitTestAggregator::SubmitHitTestData( | |
46 hit_test::mojom::HitTestDataPtr hit_test_data) { | |
47 DCHECK(ValidateHitTestData(hit_test_data)); | |
48 // TODO(gklassen): Runtime validation that hit_test_data is valid. | |
49 // TODO(gklassen): Inform FrameSink that the hit_test_data is invalid. | |
50 // TODO(gklassen): FrameSink needs to inform the host of a difficult renderer. | |
51 pending_[hit_test_data->surface_id] = std::move(hit_test_data); | |
52 } | |
53 | |
54 bool HitTestAggregator::OnSurfaceDamaged(const cc::SurfaceId& surface_id, | |
55 const cc::BeginFrameAck& ack) { | |
56 return false; | |
57 } | |
58 | |
59 void HitTestAggregator::OnSurfaceDiscarded(const cc::SurfaceId& surface_id) { | |
60 pending_.erase(surface_id); | |
61 active_.erase(surface_id); | |
62 } | |
63 | |
64 void HitTestAggregator::OnSurfaceWillDraw(const cc::SurfaceId& surface_id) { | |
65 auto pending_search = pending_.find(surface_id); | |
66 if (pending_search == pending_.end()) { | |
67 // Have already activated pending hit_test_data objects for this surface. | |
68 return; | |
69 } | |
70 hit_test::mojom::HitTestData* hit_test_data = pending_search->second.get(); | |
71 | |
72 // Update the region count. | |
73 auto active_search = active_.find(surface_id); | |
74 if (active_search != active_.end()) { | |
75 hit_test::mojom::HitTestData* old_hit_test_data = | |
76 active_search->second.get(); | |
77 active_region_count_ -= old_hit_test_data->regions.size(); | |
78 } | |
79 active_region_count_ += hit_test_data->regions.size(); | |
80 DCHECK_GE(active_region_count_, 0); | |
81 | |
82 active_[surface_id] = std::move(pending_[surface_id]); | |
83 pending_.erase(surface_id); | |
84 } | |
85 | |
86 void HitTestAggregator::AllocateDisplayHitTestData() { | |
87 AllocateDisplayHitTestData(kInitialSize); | |
88 Swap(); | |
89 AllocateDisplayHitTestData(kInitialSize); | |
90 } | |
91 | |
92 void HitTestAggregator::AllocateDisplayHitTestData(int size) { | |
93 // TODO(gklassen): free old memory | |
94 | |
95 size_t byte_count = | |
96 sizeof(DisplayHitTestData) + size * sizeof(DisplayHitTestRegion); | |
97 write_display_hit_test_data_ = (DisplayHitTestData*)malloc(byte_count); | |
98 | |
99 write_display_hit_test_data_->size = size; | |
100 write_display_hit_test_data_->regions[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_display_hit_test_data_->size; | |
113 int max_size = active_region_count_ + active_.size() + 1; | |
114 if (max_size > size) { | |
115 while (size < max_size) | |
rjkroege
2017/06/27 00:00:07
it is conceivable that a client could try to DoS u
gklassen
2017/06/27 21:46:32
Done.
| |
116 size += kIncrementalSize; | |
117 AllocateDisplayHitTestData(size); | |
118 } | |
119 | |
120 AppendRoot(display_surface_id); | |
121 } | |
122 | |
123 void HitTestAggregator::AppendRoot(cc::SurfaceId surface_id) { | |
124 auto search = active_.find(surface_id); | |
125 if (search == active_.end()) { | |
rjkroege
2017/06/27 00:00:07
how this can happen?
gklassen
2017/06/27 21:46:32
Done.
| |
126 // Referenced surface not found ( it may be late ). | |
127 return; | |
128 } | |
129 hit_test::mojom::HitTestData* hit_test_data = search->second.get(); | |
130 | |
131 DisplayHitTestRegion* element = write_display_hit_test_data_->regions; | |
132 | |
133 element->frame_sink_id = hit_test_data->surface_id.frame_sink_id(); | |
134 element->flags = hit_test_data->flags; | |
135 element->rect = hit_test_data->bounds; | |
136 element->transform = hit_test_data->transform; | |
137 | |
138 int index = 1; | |
139 for (auto& region : hit_test_data->regions) { | |
140 index = AppendRegion(region, index); | |
141 } | |
142 DCHECK_GE(index, 1); | |
143 element->child_count = index - 1; | |
144 write_display_hit_test_data_->regions[index].child_count = kEndOfList; | |
145 } | |
146 | |
147 int HitTestAggregator::AppendRegion( | |
148 const hit_test::mojom::HitTestRegionPtr& region, | |
149 int index) { | |
150 DisplayHitTestRegion* element = &write_display_hit_test_data_->regions[index]; | |
151 | |
152 element->frame_sink_id = region->surface_id.frame_sink_id(); | |
153 element->flags = region->flags; | |
154 element->rect = region->rect; | |
155 element->transform = region->transform; | |
156 | |
157 int parent_index = index++; | |
158 | |
159 if (region->flags == hit_test::mojom::kHitTestChildSurface) { | |
rjkroege
2017/06/27 00:00:07
why is this possible? You are only activating surf
gklassen
2017/06/27 21:46:32
Yes, we are only activating surfaces that are in t
| |
160 auto search = active_.find(region->surface_id); | |
161 if (search == active_.end()) { | |
162 // Surface HitTestData not found - it may be late. | |
163 // Don't include this region so that it doesn't receive events. | |
164 // TODO(gklassen): create test case for this case. | |
165 return parent_index; | |
166 } | |
167 | |
168 // Rather than add a node in the tree for this hit_test_data element | |
169 // we can simplify the tree by merging the flags and transform into | |
170 // the kHitTestChildSurface element. | |
171 hit_test::mojom::HitTestData* hit_test_data = search->second.get(); | |
172 if (!hit_test_data->transform.IsIdentity()) { | |
173 // TODO(gklassen): Add test case to validate this. | |
174 element->transform.PreconcatTransform(hit_test_data->transform); | |
175 } | |
176 element->flags |= hit_test_data->flags; | |
177 | |
178 for (auto& child_region : hit_test_data->regions) { | |
179 index = AppendRegion(child_region, index); | |
180 } | |
181 } | |
182 DCHECK_GE(index - parent_index - 1, 0); | |
183 element->child_count = index - parent_index - 1; | |
184 return index; | |
185 } | |
186 | |
187 void HitTestAggregator::Swap() { | |
188 DisplayHitTestData* temp = read_display_hit_test_data_; | |
189 read_display_hit_test_data_ = write_display_hit_test_data_; | |
190 write_display_hit_test_data_ = temp; | |
191 } | |
192 | |
193 DisplayHitTestRegion* HitTestAggregator::GetCurrentRegions() { | |
194 return read_display_hit_test_data_->regions; | |
195 } | |
196 | |
197 } // namespace hit_test | |
198 } // namespace viz | |
OLD | NEW |