| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "cc/layers/layer_proto_converter.h" | |
| 6 | |
| 7 #include "base/stl_util.h" | |
| 8 #include "base/trace_event/trace_event.h" | |
| 9 #include "cc/layers/empty_content_layer_client.h" | |
| 10 #include "cc/layers/heads_up_display_layer.h" | |
| 11 #include "cc/layers/layer.h" | |
| 12 #include "cc/layers/picture_layer.h" | |
| 13 #include "cc/layers/solid_color_scrollbar_layer.h" | |
| 14 #include "cc/proto/layer.pb.h" | |
| 15 #include "cc/trees/layer_tree_host.h" | |
| 16 #include "cc/trees/layer_tree_host_common.h" | |
| 17 #include "cc/trees/layer_tree_settings.h" | |
| 18 | |
| 19 namespace cc { | |
| 20 | |
| 21 LayerProtoConverter::LayerProtoConverter() {} | |
| 22 | |
| 23 LayerProtoConverter::~LayerProtoConverter() {} | |
| 24 | |
| 25 // static | |
| 26 scoped_refptr<Layer> LayerProtoConverter::DeserializeLayerHierarchy( | |
| 27 scoped_refptr<Layer> existing_root, | |
| 28 const proto::LayerNode& root_node, | |
| 29 LayerTreeHost* layer_tree_host) { | |
| 30 LayerIdMap layer_id_map; | |
| 31 if (existing_root) { | |
| 32 existing_root->ClearLayerTreePropertiesForDeserializationAndAddToMap( | |
| 33 &layer_id_map); | |
| 34 } | |
| 35 | |
| 36 scoped_refptr<Layer> new_root = existing_root; | |
| 37 if (!existing_root || | |
| 38 (root_node.has_id() && root_node.id() != existing_root->id())) { | |
| 39 // The root node has changed or there was no root node, | |
| 40 // so find or create the new root. | |
| 41 new_root = FindOrAllocateAndConstruct(root_node, layer_id_map); | |
| 42 } | |
| 43 new_root->FromLayerNodeProto(root_node, layer_id_map, layer_tree_host); | |
| 44 return new_root; | |
| 45 } | |
| 46 | |
| 47 // static | |
| 48 void LayerProtoConverter::SerializeLayerProperties( | |
| 49 LayerTreeHost* host, | |
| 50 proto::LayerUpdate* layer_update) { | |
| 51 TRACE_EVENT0("cc.remote", "LayerProtoConverter::SerializeLayerProperties"); | |
| 52 const bool inputs_only = false; | |
| 53 for (auto* layer : host->GetLayerTree()->LayersThatShouldPushProperties()) | |
| 54 layer->ToLayerPropertiesProto(layer_update, inputs_only); | |
| 55 host->GetLayerTree()->LayersThatShouldPushProperties().clear(); | |
| 56 } | |
| 57 | |
| 58 // static | |
| 59 void LayerProtoConverter::DeserializeLayerProperties( | |
| 60 Layer* existing_root, | |
| 61 const proto::LayerUpdate& layer_update) { | |
| 62 DCHECK(existing_root); | |
| 63 LayerIdMap layer_id_map; | |
| 64 RecursivelyFindAllLayers(existing_root, &layer_id_map); | |
| 65 | |
| 66 for (int i = 0; i < layer_update.layers_size(); ++i) { | |
| 67 const proto::LayerProperties& layer_properties = layer_update.layers(i); | |
| 68 | |
| 69 Layer::LayerIdMap::const_iterator iter = | |
| 70 layer_id_map.find(layer_properties.id()); | |
| 71 DCHECK(iter != layer_id_map.end()); | |
| 72 | |
| 73 iter->second->FromLayerPropertiesProto(layer_properties); | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 // static | |
| 78 void LayerProtoConverter::RecursivelyFindAllLayers(Layer* root_layer, | |
| 79 LayerIdMap* layer_id_map) { | |
| 80 LayerTreeHostCommon::CallFunctionForEveryLayer( | |
| 81 root_layer->GetLayerTree(), | |
| 82 [layer_id_map](Layer* layer) { (*layer_id_map)[layer->id()] = layer; }); | |
| 83 } | |
| 84 | |
| 85 // static | |
| 86 scoped_refptr<Layer> LayerProtoConverter::FindOrAllocateAndConstruct( | |
| 87 const proto::LayerNode& proto, | |
| 88 const Layer::LayerIdMap& layer_id_map) { | |
| 89 DCHECK(proto.has_id()); | |
| 90 Layer::LayerIdMap::const_iterator iter = layer_id_map.find(proto.id()); | |
| 91 if (iter != layer_id_map.end()) | |
| 92 return iter->second; | |
| 93 DCHECK(proto.has_type()); | |
| 94 switch (proto.type()) { | |
| 95 // Fall through and build a base layer. This won't have any special layer | |
| 96 // properties but still maintains the layer hierarchy if we run into a | |
| 97 // layer type we don't support. | |
| 98 case proto::LayerNode::UNKNOWN: | |
| 99 case proto::LayerNode::LAYER: | |
| 100 case proto::LayerNode::PUSH_PROPERTIES_COUNTING_LAYER: | |
| 101 return Layer::Create().get(); | |
| 102 case proto::LayerNode::PICTURE_LAYER: | |
| 103 case proto::LayerNode::FAKE_PICTURE_LAYER: | |
| 104 return PictureLayer::Create(EmptyContentLayerClient::GetInstance()); | |
| 105 case proto::LayerNode::HEADS_UP_DISPLAY_LAYER: | |
| 106 return HeadsUpDisplayLayer::Create(); | |
| 107 case proto::LayerNode::SOLID_COLOR_SCROLLBAR_LAYER: | |
| 108 // Create and return a SolidColorScrollbarLayer with invalid properties | |
| 109 // (orientation, thumb thickness, starting track, left_side_scroll, layer | |
| 110 // id etc.). | |
| 111 // These properties will be set correctly in the later step when we run | |
| 112 // through LayerTreeHost and deserialize them for each layer. | |
| 113 return SolidColorScrollbarLayer::Create(ScrollbarOrientation::HORIZONTAL, | |
| 114 -1, -1, false, Layer::INVALID_ID); | |
| 115 } | |
| 116 // TODO(nyquist): Add the rest of the necessary LayerTypes. This function | |
| 117 // should not return null. | |
| 118 NOTREACHED(); | |
| 119 return nullptr; | |
| 120 } | |
| 121 | |
| 122 } // namespace cc | |
| OLD | NEW |