| Index: cc/surfaces/framesink_manager.cc
|
| diff --git a/cc/surfaces/framesink_manager.cc b/cc/surfaces/framesink_manager.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..cc08b5a18cd8e61c655254bc47c7b40a9d84171e
|
| --- /dev/null
|
| +++ b/cc/surfaces/framesink_manager.cc
|
| @@ -0,0 +1,242 @@
|
| +// Copyright 2014 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 "cc/surfaces/framesink_manager.h"
|
| +
|
| +#include <stddef.h>
|
| +#include <stdint.h>
|
| +
|
| +#include <queue>
|
| +#include <utility>
|
| +
|
| +#include "base/logging.h"
|
| +
|
| +#if DCHECK_IS_ON()
|
| +#include <sstream>
|
| +#endif
|
| +
|
| +namespace cc {
|
| +
|
| +FrameSinkManager::FrameSinkSourceMapping::FrameSinkSourceMapping()
|
| + : client(nullptr), source(nullptr) {}
|
| +
|
| +FrameSinkManager::FrameSinkSourceMapping::FrameSinkSourceMapping(
|
| + const FrameSinkSourceMapping& other) = default;
|
| +
|
| +FrameSinkManager::FrameSinkSourceMapping::~FrameSinkSourceMapping() {
|
| + DCHECK(is_empty()) << "client: " << client
|
| + << ", children: " << children.size();
|
| +}
|
| +
|
| +FrameSinkManager::FrameSinkManager() {
|
| + thread_checker_.DetachFromThread();
|
| +}
|
| +
|
| +FrameSinkManager::~FrameSinkManager() {
|
| + DCHECK(thread_checker_.CalledOnValidThread());
|
| +
|
| + // All hierarchies, sources, and surface factory clients should be
|
| + // unregistered prior to SurfaceManager destruction.
|
| + DCHECK_EQ(frame_sink_source_map_.size(), 0u);
|
| + DCHECK_EQ(registered_sources_.size(), 0u);
|
| +}
|
| +
|
| +void FrameSinkManager::RegisterFrameSinkId(const FrameSinkId& frame_sink_id) {
|
| + bool inserted = valid_frame_sink_ids_.insert(frame_sink_id).second;
|
| + DCHECK(inserted);
|
| +}
|
| +
|
| +void FrameSinkManager::InvalidateFrameSinkId(const FrameSinkId& frame_sink_id) {
|
| + valid_frame_sink_ids_.erase(frame_sink_id);
|
| +}
|
| +
|
| +void FrameSinkManager::RegisterSurfaceFactoryClient(
|
| + const FrameSinkId& frame_sink_id,
|
| + SurfaceFactoryClient* client) {
|
| + DCHECK(client);
|
| + DCHECK_EQ(valid_frame_sink_ids_.count(frame_sink_id), 1u);
|
| +
|
| + // Will create a new FrameSinkSourceMapping for |frame_sink_id| if necessary.
|
| + FrameSinkSourceMapping& frame_sink_source =
|
| + frame_sink_source_map_[frame_sink_id];
|
| + DCHECK(!frame_sink_source.client);
|
| + frame_sink_source.client = client;
|
| +
|
| + // Propagate any previously set sources to the new client.
|
| + if (frame_sink_source.source)
|
| + client->SetBeginFrameSource(frame_sink_source.source);
|
| +}
|
| +
|
| +void FrameSinkManager::UnregisterSurfaceFactoryClient(
|
| + const FrameSinkId& frame_sink_id) {
|
| + DCHECK_EQ(valid_frame_sink_ids_.count(frame_sink_id), 1u);
|
| + DCHECK_EQ(frame_sink_source_map_.count(frame_sink_id), 1u);
|
| +
|
| + auto iter = frame_sink_source_map_.find(frame_sink_id);
|
| + if (iter->second.source)
|
| + iter->second.client->SetBeginFrameSource(nullptr);
|
| + iter->second.client = nullptr;
|
| +
|
| + // The SurfaceFactoryClient and hierarchy can be registered/unregistered
|
| + // in either order, so empty namespace_client_map entries need to be
|
| + // checked when removing either clients or relationships.
|
| + if (iter->second.is_empty())
|
| + frame_sink_source_map_.erase(iter);
|
| +}
|
| +
|
| +void FrameSinkManager::RegisterBeginFrameSource(
|
| + BeginFrameSource* source,
|
| + const FrameSinkId& frame_sink_id) {
|
| + DCHECK(source);
|
| + DCHECK_EQ(registered_sources_.count(source), 0u);
|
| + DCHECK_EQ(valid_frame_sink_ids_.count(frame_sink_id), 1u);
|
| +
|
| + registered_sources_[source] = frame_sink_id;
|
| + RecursivelyAttachBeginFrameSource(frame_sink_id, source);
|
| +}
|
| +
|
| +void FrameSinkManager::UnregisterBeginFrameSource(BeginFrameSource* source) {
|
| + DCHECK(source);
|
| + DCHECK_EQ(registered_sources_.count(source), 1u);
|
| +
|
| + FrameSinkId frame_sink_id = registered_sources_[source];
|
| + registered_sources_.erase(source);
|
| +
|
| + if (frame_sink_source_map_.count(frame_sink_id) == 0u)
|
| + return;
|
| +
|
| + // TODO(enne): these walks could be done in one step.
|
| + // Remove this begin frame source from its subtree.
|
| + RecursivelyDetachBeginFrameSource(frame_sink_id, source);
|
| + // Then flush every remaining registered source to fix any sources that
|
| + // became null because of the previous step but that have an alternative.
|
| + for (auto source_iter : registered_sources_)
|
| + RecursivelyAttachBeginFrameSource(source_iter.second, source_iter.first);
|
| +}
|
| +
|
| +void FrameSinkManager::RecursivelyAttachBeginFrameSource(
|
| + const FrameSinkId& frame_sink_id,
|
| + BeginFrameSource* source) {
|
| + FrameSinkSourceMapping& mapping = frame_sink_source_map_[frame_sink_id];
|
| + if (!mapping.source) {
|
| + mapping.source = source;
|
| + if (mapping.client)
|
| + mapping.client->SetBeginFrameSource(source);
|
| + }
|
| + for (size_t i = 0; i < mapping.children.size(); ++i)
|
| + RecursivelyAttachBeginFrameSource(mapping.children[i], source);
|
| +}
|
| +
|
| +void FrameSinkManager::RecursivelyDetachBeginFrameSource(
|
| + const FrameSinkId& frame_sink_id,
|
| + BeginFrameSource* source) {
|
| + auto iter = frame_sink_source_map_.find(frame_sink_id);
|
| + if (iter == frame_sink_source_map_.end())
|
| + return;
|
| + if (iter->second.source == source) {
|
| + iter->second.source = nullptr;
|
| + if (iter->second.client)
|
| + iter->second.client->SetBeginFrameSource(nullptr);
|
| + }
|
| +
|
| + if (iter->second.is_empty()) {
|
| + frame_sink_source_map_.erase(iter);
|
| + return;
|
| + }
|
| +
|
| + std::vector<FrameSinkId>& children = iter->second.children;
|
| + for (size_t i = 0; i < children.size(); ++i) {
|
| + RecursivelyDetachBeginFrameSource(children[i], source);
|
| + }
|
| +}
|
| +
|
| +bool FrameSinkManager::ChildContains(
|
| + const FrameSinkId& child_frame_sink_id,
|
| + const FrameSinkId& search_frame_sink_id) const {
|
| + auto iter = frame_sink_source_map_.find(child_frame_sink_id);
|
| + if (iter == frame_sink_source_map_.end())
|
| + return false;
|
| +
|
| + const std::vector<FrameSinkId>& children = iter->second.children;
|
| + for (size_t i = 0; i < children.size(); ++i) {
|
| + if (children[i] == search_frame_sink_id)
|
| + return true;
|
| + if (ChildContains(children[i], search_frame_sink_id))
|
| + return true;
|
| + }
|
| + return false;
|
| +}
|
| +
|
| +void FrameSinkManager::RegisterFrameSinkHierarchy(
|
| + const FrameSinkId& parent_frame_sink_id,
|
| + const FrameSinkId& child_frame_sink_id) {
|
| + DCHECK_EQ(valid_frame_sink_ids_.count(parent_frame_sink_id), 1u);
|
| + DCHECK_EQ(valid_frame_sink_ids_.count(child_frame_sink_id), 1u);
|
| +
|
| + // If it's possible to reach the parent through the child's descendant chain,
|
| + // then this will create an infinite loop. Might as well just crash here.
|
| + CHECK(!ChildContains(child_frame_sink_id, parent_frame_sink_id));
|
| +
|
| + std::vector<FrameSinkId>& children =
|
| + frame_sink_source_map_[parent_frame_sink_id].children;
|
| + for (size_t i = 0; i < children.size(); ++i)
|
| + DCHECK(children[i] != child_frame_sink_id);
|
| + children.push_back(child_frame_sink_id);
|
| +
|
| + // If the parent has no source, then attaching it to this child will
|
| + // not change any downstream sources.
|
| + BeginFrameSource* parent_source =
|
| + frame_sink_source_map_[parent_frame_sink_id].source;
|
| + if (!parent_source)
|
| + return;
|
| +
|
| + DCHECK_EQ(registered_sources_.count(parent_source), 1u);
|
| + RecursivelyAttachBeginFrameSource(child_frame_sink_id, parent_source);
|
| +}
|
| +
|
| +void FrameSinkManager::UnregisterFrameSinkHierarchy(
|
| + const FrameSinkId& parent_frame_sink_id,
|
| + const FrameSinkId& child_frame_sink_id) {
|
| + // Deliberately do not check validity of either parent or child namespace
|
| + // here. They were valid during the registration, so were valid at some
|
| + // point in time. This makes it possible to invalidate parent and child
|
| + // namespaces independently of each other and not have an ordering dependency
|
| + // of unregistering the hierarchy first before either of them.
|
| + DCHECK_EQ(frame_sink_source_map_.count(parent_frame_sink_id), 1u);
|
| +
|
| + auto iter = frame_sink_source_map_.find(parent_frame_sink_id);
|
| +
|
| + std::vector<FrameSinkId>& children = iter->second.children;
|
| + bool found_child = false;
|
| + for (size_t i = 0; i < children.size(); ++i) {
|
| + if (children[i] == child_frame_sink_id) {
|
| + found_child = true;
|
| + children[i] = children.back();
|
| + children.resize(children.size() - 1);
|
| + break;
|
| + }
|
| + }
|
| + DCHECK(found_child);
|
| +
|
| + // The SurfaceFactoryClient and hierarchy can be registered/unregistered
|
| + // in either order, so empty namespace_client_map entries need to be
|
| + // checked when removing either clients or relationships.
|
| + if (iter->second.is_empty()) {
|
| + frame_sink_source_map_.erase(iter);
|
| + return;
|
| + }
|
| +
|
| + // If the parent does not have a begin frame source, then disconnecting it
|
| + // will not change any of its children.
|
| + BeginFrameSource* parent_source = iter->second.source;
|
| + if (!parent_source)
|
| + return;
|
| +
|
| + // TODO(enne): these walks could be done in one step.
|
| + RecursivelyDetachBeginFrameSource(child_frame_sink_id, parent_source);
|
| + for (auto source_iter : registered_sources_)
|
| + RecursivelyAttachBeginFrameSource(source_iter.second, source_iter.first);
|
| +}
|
| +
|
| +} // namespace cc
|
|
|