Add support for (de)serializing cc::Layer hierarchy.

This CL adds a protocol buffer for serializing the Layer hierarchy,
including the mask and replica layers. This will be used whenever the
hierarchy has changed and needs to be sent to the client.

Given that the protocol still is in flux, and for good measure, all
fields in the protocol buffer are marked as optional, even the
currently required fields.

When a hierarchy is serialized, the whole hierarchy is serialized from
the root node, instead of just the changed subtree.

Most of the logic lives in the Layer class, but the possibility to
change the root of the hierarchy is extracted out to a
LayerProtoConverter. It also has functionality to build up a map of
the current tree, which will also be used during property
deserialization.

The next CL will deal with serializing the properties:
https://codereview.chromium.org/1423523002/

BUG=538710
CQ_INCLUDE_TRYBOTS=tryserver.blink:linux_blink_rel

Review URL: https://codereview.chromium.org/1398443008

Cr-Commit-Position: refs/heads/master@{#358424}
diff --git a/cc/layers/layer.h b/cc/layers/layer.h
index 2fec3636..5c298f5 100644
--- a/cc/layers/layer.h
+++ b/cc/layers/layer.h
@@ -67,6 +67,10 @@
 class SimpleEnclosedRegion;
 struct AnimationEvent;
 
+namespace proto {
+class LayerNode;
+}  // namespace proto
+
 // Base class for composited layers. Special layer types are derived from
 // this class.
 class CC_EXPORT Layer : public base::RefCounted<Layer>,
@@ -74,6 +78,7 @@
                         public LayerAnimationValueProvider {
  public:
   typedef LayerList LayerListType;
+  typedef base::hash_map<int, scoped_refptr<Layer>> LayerIdMap;
 
   enum LayerIdLabels {
     INVALID_ID = -1,
@@ -358,6 +363,30 @@
 
   virtual void PushPropertiesTo(LayerImpl* layer);
 
+  // Sets the type proto::LayerType that should be used for serialization
+  // of the current layer by calling LayerNode::set_type(proto::LayerType).
+  // TODO(nyquist): Start using a forward declared enum class when
+  // https://github.com/google/protobuf/issues/67 has been fixed and rolled in.
+  // This function would preferably instead return a proto::LayerType, but
+  // since that is an enum (the protobuf library does not generate enum
+  // classes), it can't be forward declared. We don't want to include
+  // //cc/proto/layer.pb.h in this header file, as it requires that all
+  // dependent targets would have to be given the config for how to include it.
+  virtual void SetTypeForProtoSerialization(proto::LayerNode* proto) const;
+
+  // Recursively iterate over this layer and all children and write the
+  // hierarchical structure to the given LayerNode proto. In addition to the
+  // structure itself, the Layer id and type is also written to facilitate
+  // construction of the correct layer on the client.
+  void ToLayerNodeProto(proto::LayerNode* proto) const;
+
+  // Recursively iterate over the given LayerNode proto and read the structure
+  // into this node and its children. The |layer_map| should be used to look
+  // for previously existing Layers, since they should be re-used between each
+  // hierarchy update.
+  void FromLayerNodeProto(const proto::LayerNode& proto,
+                          const LayerIdMap& layer_map);
+
   LayerTreeHost* layer_tree_host() { return layer_tree_host_; }
   const LayerTreeHost* layer_tree_host() const { return layer_tree_host_; }