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/hit_test/hit_test_aggregator.h" |
| 6 #include "base/logging.h" |
| 7 #include "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? ). |
| 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) { |
| 21 if (!hit_test_region->surface_id.is_valid()) { |
| 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): We need comprehensive run-time validation of hit test |
| 51 // data and a mechanism to handle renderers that may be submitting invalid |
| 52 // hit test data. |
| 53 pending_[hit_test_data->surface_id] = std::move(hit_test_data); |
| 54 } |
| 55 |
| 56 void HitTestAggregator::OnSurfaceDiscarded(const cc::SurfaceId& surface_id) { |
| 57 pending_.erase(surface_id); |
| 58 active_.erase(surface_id); |
| 59 } |
| 60 |
| 61 void HitTestAggregator::OnSurfaceWillDraw(const cc::SurfaceId& surface_id) { |
| 62 auto pending_search = pending_.find(surface_id); |
| 63 if (pending_search == pending_.end()) { |
| 64 // Have already activated pending hit_test_data objects for this surface. |
| 65 return; |
| 66 } |
| 67 hit_test::mojom::HitTestData* hit_test_data = pending_search->second.get(); |
| 68 |
| 69 // Update the region count. |
| 70 auto active_search = active_.find(surface_id); |
| 71 if (active_search != active_.end()) { |
| 72 hit_test::mojom::HitTestData* old_hit_test_data = |
| 73 active_search->second.get(); |
| 74 active_region_count_ -= old_hit_test_data->regions.size(); |
| 75 } |
| 76 active_region_count_ += hit_test_data->regions.size(); |
| 77 DCHECK(active_region_count_ >= 0); |
| 78 |
| 79 active_[surface_id] = std::move(pending_[surface_id]); |
| 80 pending_.erase(surface_id); |
| 81 } |
| 82 |
| 83 void HitTestAggregator::AllocateDisplayHitTestData() { |
| 84 AllocateDisplayHitTestData(kInitialSize); |
| 85 } |
| 86 |
| 87 void HitTestAggregator::AllocateDisplayHitTestData(int length) { |
| 88 size_t byte_count = |
| 89 sizeof(DisplayHitTestData) + length * sizeof(DisplayHitTestRegion); |
| 90 display_hit_test_data_ = (DisplayHitTestData*)malloc(byte_count); |
| 91 |
| 92 display_hit_test_data_->length = length; |
| 93 display_hit_test_data_->read_offset = 0; |
| 94 display_hit_test_data_->regions[0].child_count = kEndOfList; |
| 95 display_hit_test_data_->regions[length / 2].child_count = kEndOfList; |
| 96 } |
| 97 |
| 98 void HitTestAggregator::ResizeDisplayHitTestData(int length) { |
| 99 DisplayHitTestData* old_display_hit_test_data = display_hit_test_data_; |
| 100 |
| 101 AllocateDisplayHitTestData(length); |
| 102 |
| 103 // Copy over the current data and then mark the old structure |
| 104 // as invalid so that clients will re-acquire their reference. |
| 105 int old_length = old_display_hit_test_data->length; |
| 106 int new_length = display_hit_test_data_->length; |
| 107 DCHECK(new_length > old_length); |
| 108 memcpy(display_hit_test_data_->regions, old_display_hit_test_data->regions, |
| 109 old_length * sizeof(DisplayHitTestRegion)); |
| 110 |
| 111 old_display_hit_test_data->read_offset = kOldPleaseReAcquire; |
| 112 } |
| 113 |
| 114 void HitTestAggregator::PostTaskAggregate(cc::SurfaceId display_surface_id) { |
| 115 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 116 FROM_HERE, |
| 117 base::BindOnce(&HitTestAggregator::Aggregate, |
| 118 weak_ptr_factory_.GetWeakPtr(), display_surface_id)); |
| 119 } |
| 120 |
| 121 void HitTestAggregator::Aggregate(cc::SurfaceId display_surface_id) { |
| 122 // Size check. |
| 123 int length = display_hit_test_data_->length / 2; |
| 124 if (active_region_count_ + 1 > length) { |
| 125 while (length < active_region_count_) { |
| 126 length += kIncrementalSize; |
| 127 } |
| 128 ResizeDisplayHitTestData(length); |
| 129 } |
| 130 |
| 131 int index = GetBackIndex(); |
| 132 int last_index = Append(display_surface_id, index); |
| 133 display_hit_test_data_->regions[last_index].child_count = kEndOfList; |
| 134 } |
| 135 |
| 136 int HitTestAggregator::Append(cc::SurfaceId surface_id, int index) { |
| 137 auto search = active_.find(surface_id); |
| 138 if (search == active_.end()) { |
| 139 // Referenced surface not found ( it may be late ). |
| 140 return index; |
| 141 } |
| 142 hit_test::mojom::HitTestData* hit_test_data = search->second.get(); |
| 143 |
| 144 for (auto& region : hit_test_data->regions) { |
| 145 index = Append(region, index); |
| 146 } |
| 147 return index; |
| 148 } |
| 149 |
| 150 int HitTestAggregator::Append(const hit_test::mojom::HitTestRegionPtr& region, |
| 151 int index) { |
| 152 DisplayHitTestRegion* element = &display_hit_test_data_->regions[index]; |
| 153 |
| 154 element->frame_sink_id = region->surface_id.frame_sink_id(); |
| 155 element->flags = region->flags; |
| 156 element->rect = region->rect; |
| 157 element->transform = region->transform; |
| 158 |
| 159 int parent_index = index++; |
| 160 |
| 161 if (region->flags == hit_test::mojom::kHitTestChildSurface) { |
| 162 index = Append(region->surface_id, index); |
| 163 } |
| 164 |
| 165 DCHECK((index - parent_index - 1) >= 0); |
| 166 element->child_count = index - parent_index - 1; |
| 167 return index; |
| 168 } |
| 169 |
| 170 void HitTestAggregator::Swap() { |
| 171 display_hit_test_data_->read_offset = GetBackIndex(); |
| 172 } |
| 173 |
| 174 int HitTestAggregator::GetBackIndex() { |
| 175 if (display_hit_test_data_->read_offset == 0) |
| 176 return display_hit_test_data_->length / 2; |
| 177 return 0; |
| 178 } |
| 179 |
| 180 DisplayHitTestRegion* HitTestAggregator::GetCurrentRegions() { |
| 181 return display_hit_test_data_->regions + display_hit_test_data_->read_offset; |
| 182 } |
| 183 |
| 184 } // namespace hit_test |
| 185 } // namespace viz |
OLD | NEW |