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 "base/logging.h" | |
varkha
2017/06/19 21:15:04
order alphabetically (git cl format will do that f
gklassen
2017/06/20 16:09:38
Done.
| |
7 #include "components/viz/common/hit_test/display_hit_test_data.h" | |
8 | |
9 namespace viz { | |
10 namespace hit_test { | |
11 | |
12 namespace { | |
13 // TODO(gklassen): Review and select appropriate sizes ( based on | |
14 // telemetry / UMA? ). | |
15 constexpr int kInitialSize = 1024; | |
16 constexpr int kIncrementalSize = 1024; | |
17 | |
18 bool ValidateHitTestRegion( | |
19 const hit_test::mojom::HitTestRegionPtr& hit_test_region) { | |
20 if (hit_test_region->flags == hit_test::mojom::kHitTestChildSurface) { | |
varkha
2017/06/19 21:15:04
nit: Can inverse this and return early.
gklassen
2017/06/20 16:09:38
I anticipate that this method can and will expand
| |
21 if (!hit_test_region->surface_id.is_valid()) { | |
varkha
2017/06/19 21:15:04
nit: no {} for one liner conditional.
gklassen
2017/06/20 16:09:38
Done.
| |
22 return false; | |
23 } | |
24 } | |
25 return true; | |
26 } | |
27 bool ValidateHitTestData(const hit_test::mojom::HitTestDataPtr& hit_test_data) { | |
28 for (auto& region : hit_test_data->regions) { | |
29 if (!ValidateHitTestRegion(region)) { | |
30 return false; | |
31 } | |
32 } | |
33 return true; | |
34 } | |
35 | |
36 } // namespace | |
37 | |
38 HitTestAggregator::HitTestAggregator( | |
39 std::unique_ptr<DisplayHitTestDataFactory> display_hit_test_data_factory) | |
40 : active_region_count_(0), | |
41 display_hit_test_data_factory_(std::move(display_hit_test_data_factory)), | |
42 weak_ptr_factory_(this) { | |
43 AllocateDisplayHitTestData(); | |
44 } | |
45 HitTestAggregator::~HitTestAggregator() {} | |
46 | |
47 void HitTestAggregator::SubmitHitTestData( | |
48 hit_test::mojom::HitTestDataPtr hit_test_data) { | |
49 DCHECK(ValidateHitTestData(hit_test_data)); | |
50 // TODO(gklassen): Runtime validation that hit_test_data is valid. | |
51 // TODO(gklassen): Inform FrameSink that the hit_test_data is invalid. | |
52 // TODO(gklassen): FrameSink needs to inform the host of a difficult renderer. | |
53 pending_[hit_test_data->surface_id] = std::move(hit_test_data); | |
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_data objects for this surface. | |
70 return; | |
71 } | |
72 hit_test::mojom::HitTestData* hit_test_data = pending_search->second.get(); | |
73 | |
74 // Update the region count. | |
75 auto active_search = active_.find(surface_id); | |
76 if (active_search != active_.end()) { | |
77 hit_test::mojom::HitTestData* old_hit_test_data = | |
78 active_search->second.get(); | |
79 active_region_count_ -= old_hit_test_data->regions.size(); | |
80 } | |
81 active_region_count_ += hit_test_data->regions.size(); | |
82 DCHECK_GE(active_region_count_, 0); | |
83 | |
84 active_[surface_id] = std::move(pending_[surface_id]); | |
85 pending_.erase(surface_id); | |
86 } | |
87 | |
88 void HitTestAggregator::AllocateDisplayHitTestData() { | |
89 AllocateDisplayHitTestData(kInitialSize); | |
90 } | |
91 | |
92 void HitTestAggregator::AllocateDisplayHitTestData(int length) { | |
93 size_t byte_count = | |
94 sizeof(DisplayHitTestData) + length * sizeof(DisplayHitTestRegion); | |
95 display_hit_test_data_ = (DisplayHitTestData*)malloc(byte_count); | |
96 | |
97 display_hit_test_data_->length = length; | |
98 display_hit_test_data_->read_offset = 0; | |
99 display_hit_test_data_->regions[0].child_count = kEndOfList; | |
100 display_hit_test_data_->regions[length / 2].child_count = kEndOfList; | |
101 } | |
102 | |
103 void HitTestAggregator::ResizeDisplayHitTestData(int length) { | |
104 DisplayHitTestData* old_display_hit_test_data = display_hit_test_data_; | |
105 | |
106 AllocateDisplayHitTestData(length); | |
107 | |
108 // Copy over the current data and then mark the old structure | |
109 // as invalid so that clients will re-acquire their reference. | |
110 int old_length = old_display_hit_test_data->length; | |
111 int new_length = display_hit_test_data_->length; | |
112 DCHECK(new_length > old_length); | |
113 memcpy(display_hit_test_data_->regions, old_display_hit_test_data->regions, | |
114 old_length * sizeof(DisplayHitTestRegion)); | |
115 | |
116 old_display_hit_test_data->read_offset = kOldPleaseReAcquire; | |
117 } | |
118 | |
119 void HitTestAggregator::PostTaskAggregate(cc::SurfaceId display_surface_id) { | |
120 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
121 FROM_HERE, | |
122 base::BindOnce(&HitTestAggregator::Aggregate, | |
123 weak_ptr_factory_.GetWeakPtr(), display_surface_id)); | |
124 } | |
125 | |
126 void HitTestAggregator::Aggregate(cc::SurfaceId display_surface_id) { | |
127 // Size check. | |
128 int length = display_hit_test_data_->length / 2; | |
129 if (active_region_count_ + 1 > length) { | |
130 while (length < active_region_count_) { | |
131 length += kIncrementalSize; | |
132 } | |
133 ResizeDisplayHitTestData(length); | |
134 } | |
135 | |
136 int index = GetBackIndex(); | |
137 int last_index = Append(display_surface_id, index); | |
138 display_hit_test_data_->regions[last_index].child_count = kEndOfList; | |
139 } | |
140 | |
141 int HitTestAggregator::Append(cc::SurfaceId surface_id, int index) { | |
142 auto search = active_.find(surface_id); | |
143 if (search == active_.end()) { | |
144 // Referenced surface not found ( it may be late ). | |
145 return index; | |
146 } | |
147 hit_test::mojom::HitTestData* hit_test_data = search->second.get(); | |
148 | |
149 for (auto& region : hit_test_data->regions) { | |
150 index = Append(region, index); | |
151 } | |
152 return index; | |
153 } | |
154 | |
155 int HitTestAggregator::Append(const hit_test::mojom::HitTestRegionPtr& region, | |
156 int index) { | |
157 DisplayHitTestRegion* element = &display_hit_test_data_->regions[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 index = Append(region->surface_id, index); | |
168 } | |
169 | |
170 DCHECK_GE(index - parent_index - 1, 0); | |
171 element->child_count = index - parent_index - 1; | |
172 return index; | |
173 } | |
174 | |
175 void HitTestAggregator::Swap() { | |
176 display_hit_test_data_->read_offset = GetBackIndex(); | |
177 } | |
178 | |
179 int HitTestAggregator::GetBackIndex() { | |
180 if (display_hit_test_data_->read_offset == 0) | |
181 return display_hit_test_data_->length / 2; | |
182 return 0; | |
183 } | |
184 | |
185 DisplayHitTestRegion* HitTestAggregator::GetCurrentRegions() { | |
186 return display_hit_test_data_->regions + display_hit_test_data_->read_offset; | |
187 } | |
188 | |
189 } // namespace hit_test | |
190 } // namespace viz | |
OLD | NEW |