Chromium Code Reviews| 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); | |
| 66 | |
| 67 // Update the region count. | |
| 68 auto active_search = active_.find(surface_id); | |
|
sadrul
2017/06/28 23:36:37
This will not work since you have already erased()
gklassen
2017/06/29 11:56:26
Thank you for the catch. Done.
| |
| 69 if (active_search != active_.end()) { | |
| 70 hit_test::mojom::HitTestRegionList* old_hit_test_data = | |
| 71 active_search->second.get(); | |
| 72 active_region_count_ -= old_hit_test_data->regions.size(); | |
| 73 } | |
| 74 DCHECK_GE(active_region_count_, 0); | |
| 75 } | |
| 76 | |
| 77 void HitTestAggregator::OnSurfaceWillDraw(const cc::SurfaceId& surface_id) { | |
| 78 auto pending_search = pending_.find(surface_id); | |
| 79 if (pending_search == pending_.end()) { | |
| 80 // Have already activated pending hit_test_region_list objects for this | |
| 81 // surface. | |
| 82 return; | |
| 83 } | |
| 84 hit_test::mojom::HitTestRegionList* hit_test_region_list = | |
| 85 pending_search->second.get(); | |
| 86 | |
| 87 // Update the region count. | |
| 88 auto active_search = active_.find(surface_id); | |
| 89 if (active_search != active_.end()) { | |
| 90 hit_test::mojom::HitTestRegionList* old_hit_test_data = | |
| 91 active_search->second.get(); | |
| 92 active_region_count_ -= old_hit_test_data->regions.size(); | |
| 93 } | |
| 94 active_region_count_ += hit_test_region_list->regions.size(); | |
| 95 DCHECK_GE(active_region_count_, 0); | |
| 96 | |
| 97 active_[surface_id] = std::move(pending_[surface_id]); | |
| 98 pending_.erase(surface_id); | |
| 99 } | |
| 100 | |
| 101 void HitTestAggregator::AllocateDisplayHitTestRegionList() { | |
| 102 AllocateDisplayHitTestRegionList(kInitialSize); | |
| 103 Swap(); | |
| 104 AllocateDisplayHitTestRegionList(kInitialSize); | |
| 105 } | |
| 106 | |
| 107 void HitTestAggregator::AllocateDisplayHitTestRegionList(int size) { | |
| 108 size_t num_bytes = size * sizeof(DisplayHitTestRegion); | |
| 109 write_handle_ = mojo::SharedBufferHandle::Create(num_bytes); | |
| 110 write_size_ = size; | |
| 111 write_buffer_ = write_handle_->Map(num_bytes); | |
| 112 | |
| 113 DisplayHitTestRegion* region = (DisplayHitTestRegion*)write_buffer_.get(); | |
| 114 region[0].child_count = kEndOfList; | |
| 115 } | |
| 116 | |
| 117 void HitTestAggregator::PostTaskAggregate(cc::SurfaceId display_surface_id) { | |
| 118 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 119 FROM_HERE, | |
| 120 base::BindOnce(&HitTestAggregator::Aggregate, | |
| 121 weak_ptr_factory_.GetWeakPtr(), display_surface_id)); | |
| 122 } | |
| 123 | |
| 124 void HitTestAggregator::Aggregate(cc::SurfaceId display_surface_id) { | |
| 125 // Check to ensure that enough memory has been allocated. | |
| 126 int size = write_size_; | |
| 127 int max_size = active_region_count_ + active_.size() + 1; | |
| 128 if (max_size > kMaxSize) | |
| 129 max_size = kMaxSize; | |
| 130 | |
| 131 if (max_size > size) { | |
| 132 size = (1 + max_size / kIncrementalSize) * kIncrementalSize; | |
| 133 AllocateDisplayHitTestRegionList(size); | |
| 134 } | |
| 135 | |
| 136 AppendRoot(display_surface_id); | |
| 137 } | |
| 138 | |
| 139 void HitTestAggregator::AppendRoot(cc::SurfaceId surface_id) { | |
| 140 auto search = active_.find(surface_id); | |
| 141 CHECK(search != active_.end()); | |
| 142 | |
| 143 hit_test::mojom::HitTestRegionList* hit_test_region_list = | |
| 144 search->second.get(); | |
| 145 | |
| 146 DisplayHitTestRegion* regions = (DisplayHitTestRegion*)write_buffer_.get(); | |
| 147 | |
| 148 regions[0].frame_sink_id = hit_test_region_list->surface_id.frame_sink_id(); | |
| 149 regions[0].flags = hit_test_region_list->flags; | |
| 150 regions[0].rect = hit_test_region_list->bounds; | |
| 151 regions[0].transform = hit_test_region_list->transform; | |
| 152 | |
| 153 int index = 1; | |
| 154 for (const auto& region : hit_test_region_list->regions) { | |
| 155 if (index >= write_size_ - 1) | |
| 156 break; | |
| 157 index = AppendRegion(regions, region, index); | |
| 158 } | |
| 159 | |
| 160 DCHECK_GE(index, 1); | |
| 161 regions[0].child_count = index - 1; | |
| 162 regions[index].child_count = kEndOfList; | |
| 163 } | |
| 164 | |
| 165 int HitTestAggregator::AppendRegion( | |
| 166 DisplayHitTestRegion* regions, | |
| 167 const hit_test::mojom::HitTestRegionPtr& region, | |
| 168 int index) { | |
| 169 DisplayHitTestRegion* element = ®ions[index]; | |
| 170 | |
| 171 element->frame_sink_id = region->surface_id.frame_sink_id(); | |
| 172 element->flags = region->flags; | |
| 173 element->rect = region->rect; | |
| 174 element->transform = region->transform; | |
| 175 | |
| 176 int parent_index = index++; | |
| 177 if (index >= write_size_ - 1) { | |
| 178 element->child_count = 0; | |
| 179 return index; | |
| 180 } | |
| 181 | |
| 182 if (region->flags == hit_test::mojom::kHitTestChildSurface) { | |
| 183 auto search = active_.find(region->surface_id); | |
| 184 if (search == active_.end()) { | |
| 185 // Surface HitTestRegionList not found - it may be late. | |
| 186 // Don't include this region so that it doesn't receive events. | |
| 187 return parent_index; | |
| 188 } | |
| 189 | |
| 190 // Rather than add a node in the tree for this hit_test_region_list element | |
| 191 // we can simplify the tree by merging the flags and transform into | |
| 192 // the kHitTestChildSurface element. | |
| 193 hit_test::mojom::HitTestRegionList* hit_test_region_list = | |
| 194 search->second.get(); | |
| 195 if (!hit_test_region_list->transform.IsIdentity()) | |
| 196 element->transform.PreconcatTransform(hit_test_region_list->transform); | |
| 197 | |
| 198 element->flags |= hit_test_region_list->flags; | |
| 199 | |
| 200 for (const auto& child_region : hit_test_region_list->regions) { | |
| 201 index = AppendRegion(regions, child_region, index); | |
| 202 if (index >= write_size_ - 1) | |
| 203 break; | |
| 204 } | |
| 205 } | |
| 206 DCHECK_GE(index - parent_index - 1, 0); | |
| 207 element->child_count = index - parent_index - 1; | |
| 208 return index; | |
| 209 } | |
| 210 | |
| 211 void HitTestAggregator::Swap() { | |
| 212 std::swap(read_handle_, write_handle_); | |
| 213 std::swap(read_size_, write_size_); | |
| 214 std::swap(read_buffer_, write_buffer_); | |
| 215 } | |
| 216 | |
| 217 } // namespace hit_test | |
| 218 } // namespace viz | |
| OLD | NEW |