Index: components/viz/hit_test/hit_test_aggregator.cc |
diff --git a/components/viz/hit_test/hit_test_aggregator.cc b/components/viz/hit_test/hit_test_aggregator.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..eedae4c593e5144d65bf3ab5023c9dcce19f22f9 |
--- /dev/null |
+++ b/components/viz/hit_test/hit_test_aggregator.cc |
@@ -0,0 +1,185 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/viz/hit_test/hit_test_aggregator.h" |
+#include "base/logging.h" |
+#include "display_hit_test_data.h" |
+ |
+namespace viz { |
+namespace hit_test { |
+ |
+namespace { |
+// TODO(gklassen): Review and select appropriate sizes ( based on |
+// telemetry? ). |
+constexpr int kInitialSize = 1024; |
+constexpr int kIncrementalSize = 1024; |
+ |
+bool ValidateHitTestRegion( |
+ const hit_test::mojom::HitTestRegionPtr& hit_test_region) { |
+ if (hit_test_region->flags == hit_test::mojom::kHitTestChildSurface) { |
+ if (!hit_test_region->surface_id.is_valid()) { |
+ return false; |
+ } |
+ } |
+ return true; |
+} |
+bool ValidateHitTestData(const hit_test::mojom::HitTestDataPtr& hit_test_data) { |
+ for (auto& region : hit_test_data->regions) { |
+ if (!ValidateHitTestRegion(region)) { |
+ return false; |
+ } |
+ } |
+ return true; |
+} |
+ |
+} // namespace |
+ |
+HitTestAggregator::HitTestAggregator( |
+ std::unique_ptr<DisplayHitTestDataFactory> display_hit_test_data_factory) |
+ : active_region_count_(0), |
+ display_hit_test_data_factory_(std::move(display_hit_test_data_factory)), |
+ weak_ptr_factory_(this) { |
+ AllocateDisplayHitTestData(); |
+} |
+HitTestAggregator::~HitTestAggregator() {} |
+ |
+void HitTestAggregator::SubmitHitTestData( |
+ hit_test::mojom::HitTestDataPtr hit_test_data) { |
+ DCHECK(ValidateHitTestData(hit_test_data)); |
+ // TODO(gklassen): We need comprehensive run-time validation of hit test |
+ // data and a mechanism to handle renderers that may be submitting invalid |
+ // hit test data. |
+ pending_[hit_test_data->surface_id] = std::move(hit_test_data); |
+} |
+ |
+void HitTestAggregator::OnSurfaceDiscarded(const cc::SurfaceId& surface_id) { |
+ pending_.erase(surface_id); |
+ active_.erase(surface_id); |
+} |
+ |
+void HitTestAggregator::OnSurfaceWillDraw(const cc::SurfaceId& surface_id) { |
+ auto pending_search = pending_.find(surface_id); |
+ if (pending_search == pending_.end()) { |
+ // Have already activated pending hit_test_data objects for this surface. |
+ return; |
+ } |
+ hit_test::mojom::HitTestData* hit_test_data = pending_search->second.get(); |
+ |
+ // Update the region count. |
+ auto active_search = active_.find(surface_id); |
+ if (active_search != active_.end()) { |
+ hit_test::mojom::HitTestData* old_hit_test_data = |
+ active_search->second.get(); |
+ active_region_count_ -= old_hit_test_data->regions.size(); |
+ } |
+ active_region_count_ += hit_test_data->regions.size(); |
+ DCHECK(active_region_count_ >= 0); |
+ |
+ active_[surface_id] = std::move(pending_[surface_id]); |
+ pending_.erase(surface_id); |
+} |
+ |
+void HitTestAggregator::AllocateDisplayHitTestData() { |
+ AllocateDisplayHitTestData(kInitialSize); |
+} |
+ |
+void HitTestAggregator::AllocateDisplayHitTestData(int length) { |
+ size_t byte_count = |
+ sizeof(DisplayHitTestData) + length * sizeof(DisplayHitTestRegion); |
+ display_hit_test_data_ = (DisplayHitTestData*)malloc(byte_count); |
+ |
+ display_hit_test_data_->length = length; |
+ display_hit_test_data_->read_offset = 0; |
+ display_hit_test_data_->regions[0].child_count = kEndOfList; |
+ display_hit_test_data_->regions[length / 2].child_count = kEndOfList; |
+} |
+ |
+void HitTestAggregator::ResizeDisplayHitTestData(int length) { |
+ DisplayHitTestData* old_display_hit_test_data = display_hit_test_data_; |
+ |
+ AllocateDisplayHitTestData(length); |
+ |
+ // Copy over the current data and then mark the old structure |
+ // as invalid so that clients will re-acquire their reference. |
+ int old_length = old_display_hit_test_data->length; |
+ int new_length = display_hit_test_data_->length; |
+ DCHECK(new_length > old_length); |
+ memcpy(display_hit_test_data_->regions, old_display_hit_test_data->regions, |
+ old_length * sizeof(DisplayHitTestRegion)); |
+ |
+ old_display_hit_test_data->read_offset = kOldPleaseReAcquire; |
+} |
+ |
+void HitTestAggregator::PostTaskAggregate(cc::SurfaceId display_surface_id) { |
+ base::ThreadTaskRunnerHandle::Get()->PostTask( |
+ FROM_HERE, |
+ base::BindOnce(&HitTestAggregator::Aggregate, |
+ weak_ptr_factory_.GetWeakPtr(), display_surface_id)); |
+} |
+ |
+void HitTestAggregator::Aggregate(cc::SurfaceId display_surface_id) { |
+ // Size check. |
+ int length = display_hit_test_data_->length / 2; |
+ if (active_region_count_ + 1 > length) { |
+ while (length < active_region_count_) { |
+ length += kIncrementalSize; |
+ } |
+ ResizeDisplayHitTestData(length); |
+ } |
+ |
+ int index = GetBackIndex(); |
+ int last_index = Append(display_surface_id, index); |
+ display_hit_test_data_->regions[last_index].child_count = kEndOfList; |
+} |
+ |
+int HitTestAggregator::Append(cc::SurfaceId surface_id, int index) { |
+ auto search = active_.find(surface_id); |
+ if (search == active_.end()) { |
+ // Referenced surface not found ( it may be late ). |
+ return index; |
+ } |
+ hit_test::mojom::HitTestData* hit_test_data = search->second.get(); |
+ |
+ for (auto& region : hit_test_data->regions) { |
+ index = Append(region, index); |
+ } |
+ return index; |
+} |
+ |
+int HitTestAggregator::Append(const hit_test::mojom::HitTestRegionPtr& region, |
+ int index) { |
+ DisplayHitTestRegion* element = &display_hit_test_data_->regions[index]; |
+ |
+ element->frame_sink_id = region->surface_id.frame_sink_id(); |
+ element->flags = region->flags; |
+ element->rect = region->rect; |
+ element->transform = region->transform; |
+ |
+ int parent_index = index++; |
+ |
+ if (region->flags == hit_test::mojom::kHitTestChildSurface) { |
+ index = Append(region->surface_id, index); |
+ } |
+ |
+ DCHECK((index - parent_index - 1) >= 0); |
+ element->child_count = index - parent_index - 1; |
+ return index; |
+} |
+ |
+void HitTestAggregator::Swap() { |
+ display_hit_test_data_->read_offset = GetBackIndex(); |
+} |
+ |
+int HitTestAggregator::GetBackIndex() { |
+ if (display_hit_test_data_->read_offset == 0) |
+ return display_hit_test_data_->length / 2; |
+ return 0; |
+} |
+ |
+DisplayHitTestRegion* HitTestAggregator::GetCurrentRegions() { |
+ return display_hit_test_data_->regions + display_hit_test_data_->read_offset; |
+} |
+ |
+} // namespace hit_test |
+} // namespace viz |