Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Unified Diff: cc/layers/layer_proto_converter.cc

Issue 1398443008: Add support for (de)serializing cc::Layer hierarchy. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@review-1394353002
Patch Set: Moved functionality outside of Layer to LayerProtoConverter. Updated test-comments to be multiline. Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: cc/layers/layer_proto_converter.cc
diff --git a/cc/layers/layer_proto_converter.cc b/cc/layers/layer_proto_converter.cc
new file mode 100644
index 0000000000000000000000000000000000000000..45fdcb6cca1d053374ccd291e1dbffad3c348278
--- /dev/null
+++ b/cc/layers/layer_proto_converter.cc
@@ -0,0 +1,72 @@
+// Copyright 2015 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/layers/layer_proto_converter.h"
+
+#include "base/stl_util.h"
+#include "cc/layers/layer.h"
+#include "cc/proto/layer.pb.h"
+#include "cc/trees/layer_tree_host_common.h"
+#include "cc/trees/layer_tree_settings.h"
+
+namespace cc {
+
+LayerProtoConverter::LayerProtoConverter() {}
+
+LayerProtoConverter::~LayerProtoConverter() {}
+
+// static
+void LayerProtoConverter::SerializeLayerHierarchy(
+ const scoped_refptr<Layer> root_layer,
+ proto::LayerNode* root_node) {
+ root_layer->ToLayerNodeProto(root_node);
+}
+
+// static
+scoped_refptr<Layer> LayerProtoConverter::DeserializeLayerHierarchy(
+ scoped_refptr<Layer> existing_root,
+ const proto::LayerNode& root_node) {
+ LayerIdMap layer_id_map;
+ RecursivelyFindAllLayers(existing_root, &layer_id_map);
+
+ scoped_refptr<Layer> new_root = existing_root;
+ if (!existing_root ||
+ (root_node.has_id() && root_node.id() != existing_root->id())) {
+ // The root node has changed or there was no root node,
+ // so find or create the new root.
+ new_root = FindOrAllocateAndConstruct(root_node, layer_id_map);
+ }
+ new_root->FromLayerNodeProto(root_node, layer_id_map);
+ return new_root;
+}
+
+// static
+void LayerProtoConverter::RecursivelyFindAllLayers(
+ const scoped_refptr<Layer>& layer,
+ LayerIdMap* layer_id_map) {
+ LayerTreeHostCommon::CallFunctionForSubtree(
+ layer.get(),
+ [layer_id_map](Layer* layer) { (*layer_id_map)[layer->id()] = layer; });
+}
+
+// static
+scoped_refptr<Layer> LayerProtoConverter::FindOrAllocateAndConstruct(
+ const proto::LayerNode& proto,
+ const Layer::LayerIdMap& layer_id_map) {
+ DCHECK(proto.has_id());
+ Layer::LayerIdMap::const_iterator iter = layer_id_map.find(proto.id());
+ if (iter != layer_id_map.end())
+ return iter->second;
+ DCHECK(proto.has_type());
+ switch (proto.type()) {
+ case proto::Base:
vmpstr 2015/10/23 21:26:10 Do other cases exist already? If there's only Base
nyquist 2015/10/26 03:14:29 Done. For now it's only base (until the TODO is i
vmpstr 2015/10/26 17:51:45 Yep, that's perfect.
+ return Layer::Create(LayerSettings()).get();
+ default:
+ // TODO(nyquist): Add the rest of the necessary LayerTypes. This function
+ // should not return null.
+ return nullptr;
+ }
+}
+
+} // namespace cc

Powered by Google App Engine
This is Rietveld 408576698