Chromium Code Reviews| Index: components/viz/service/hit_test/hit_test_aggregator.cc |
| diff --git a/components/viz/service/hit_test/hit_test_aggregator.cc b/components/viz/service/hit_test/hit_test_aggregator.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..eb6026de76849922e9ec2f6ef38ecbedb9917045 |
| --- /dev/null |
| +++ b/components/viz/service/hit_test/hit_test_aggregator.cc |
| @@ -0,0 +1,198 @@ |
| +// 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/service/hit_test/hit_test_aggregator.h" |
| +#include "components/viz/common/hit_test/display_hit_test_data.h" |
| + |
| +namespace viz { |
| +namespace hit_test { |
| + |
| +namespace { |
| +// 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.
|
| +// telemetry / UMA? ). |
| +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): Runtime validation that hit_test_data is valid. |
| + // TODO(gklassen): Inform FrameSink that the hit_test_data is invalid. |
| + // TODO(gklassen): FrameSink needs to inform the host of a difficult renderer. |
| + pending_[hit_test_data->surface_id] = std::move(hit_test_data); |
| +} |
| + |
| +bool HitTestAggregator::OnSurfaceDamaged(const cc::SurfaceId& surface_id, |
| + const cc::BeginFrameAck& ack) { |
| + return false; |
| +} |
| + |
| +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_GE(active_region_count_, 0); |
| + |
| + active_[surface_id] = std::move(pending_[surface_id]); |
| + pending_.erase(surface_id); |
| +} |
| + |
| +void HitTestAggregator::AllocateDisplayHitTestData() { |
| + AllocateDisplayHitTestData(kInitialSize); |
| + Swap(); |
| + AllocateDisplayHitTestData(kInitialSize); |
| +} |
| + |
| +void HitTestAggregator::AllocateDisplayHitTestData(int size) { |
| + // TODO(gklassen): free old memory |
| + |
| + size_t byte_count = |
| + sizeof(DisplayHitTestData) + size * sizeof(DisplayHitTestRegion); |
| + write_display_hit_test_data_ = (DisplayHitTestData*)malloc(byte_count); |
| + |
| + write_display_hit_test_data_->size = size; |
| + write_display_hit_test_data_->regions[0].child_count = kEndOfList; |
| +} |
| + |
| +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 size = write_display_hit_test_data_->size; |
| + int max_size = active_region_count_ + active_.size() + 1; |
| + if (max_size > size) { |
| + 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.
|
| + size += kIncrementalSize; |
| + AllocateDisplayHitTestData(size); |
| + } |
| + |
| + AppendRoot(display_surface_id); |
| +} |
| + |
| +void HitTestAggregator::AppendRoot(cc::SurfaceId surface_id) { |
| + auto search = active_.find(surface_id); |
| + if (search == active_.end()) { |
|
rjkroege
2017/06/27 00:00:07
how this can happen?
gklassen
2017/06/27 21:46:32
Done.
|
| + // Referenced surface not found ( it may be late ). |
| + return; |
| + } |
| + hit_test::mojom::HitTestData* hit_test_data = search->second.get(); |
| + |
| + DisplayHitTestRegion* element = write_display_hit_test_data_->regions; |
| + |
| + element->frame_sink_id = hit_test_data->surface_id.frame_sink_id(); |
| + element->flags = hit_test_data->flags; |
| + element->rect = hit_test_data->bounds; |
| + element->transform = hit_test_data->transform; |
| + |
| + int index = 1; |
| + for (auto& region : hit_test_data->regions) { |
| + index = AppendRegion(region, index); |
| + } |
| + DCHECK_GE(index, 1); |
| + element->child_count = index - 1; |
| + write_display_hit_test_data_->regions[index].child_count = kEndOfList; |
| +} |
| + |
| +int HitTestAggregator::AppendRegion( |
| + const hit_test::mojom::HitTestRegionPtr& region, |
| + int index) { |
| + DisplayHitTestRegion* element = &write_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) { |
|
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
|
| + auto search = active_.find(region->surface_id); |
| + if (search == active_.end()) { |
| + // Surface HitTestData not found - it may be late. |
| + // Don't include this region so that it doesn't receive events. |
| + // TODO(gklassen): create test case for this case. |
| + return parent_index; |
| + } |
| + |
| + // Rather than add a node in the tree for this hit_test_data element |
| + // we can simplify the tree by merging the flags and transform into |
| + // the kHitTestChildSurface element. |
| + hit_test::mojom::HitTestData* hit_test_data = search->second.get(); |
| + if (!hit_test_data->transform.IsIdentity()) { |
| + // TODO(gklassen): Add test case to validate this. |
| + element->transform.PreconcatTransform(hit_test_data->transform); |
| + } |
| + element->flags |= hit_test_data->flags; |
| + |
| + for (auto& child_region : hit_test_data->regions) { |
| + index = AppendRegion(child_region, index); |
| + } |
| + } |
| + DCHECK_GE(index - parent_index - 1, 0); |
| + element->child_count = index - parent_index - 1; |
| + return index; |
| +} |
| + |
| +void HitTestAggregator::Swap() { |
| + DisplayHitTestData* temp = read_display_hit_test_data_; |
| + read_display_hit_test_data_ = write_display_hit_test_data_; |
| + write_display_hit_test_data_ = temp; |
| +} |
| + |
| +DisplayHitTestRegion* HitTestAggregator::GetCurrentRegions() { |
| + return read_display_hit_test_data_->regions; |
| +} |
| + |
| +} // namespace hit_test |
| +} // namespace viz |