cc: Add id->LayerImpl map to LayerTreeImpl

This is a prerequisite to two LayerTreeImpls, as PictureLayerImpls in the
active tree may need to ask about their pending tree sibilings with the
same id.  Once this lands, functionality like findScrollingLayerbyId can
be wrapped into this too.

LayerImpl now registers itself with its tree on creation and destruction.
As there are now asserts that a given id is not already in use, a number
of tests have been updated to not duplicate layer ids.

There doesn't technically need to be an unregister step, but as LayerImpl
doesn't necessarily have to be in the LayerImpl tree rooted in the
LayerTreeImpl, this unregister check will blow up if anybody has a layer
outliving their registered tree (which is bad).

As LayerImpl has single ownership, it's impossible to assign the same
non-NULL replica or mask to a layer, so the LayerImpl unit tests are
updated to not test this anymore.  Masks and replicas are also modified
to keep their id in sync with their pointer during tree synchronization
to prevent asserts about duplicate ids.

[email protected]
BUG=155209


Review URL: https://chromiumcodereview.appspot.com/11575018

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@173110 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/cc/layer_tree_impl.cc b/cc/layer_tree_impl.cc
index d434bb0..a113316 100644
--- a/cc/layer_tree_impl.cc
+++ b/cc/layer_tree_impl.cc
@@ -19,6 +19,9 @@
 }
 
 LayerTreeImpl::~LayerTreeImpl() {
+  // Need to explicitly clear the tree prior to destroying this so that
+  // the LayerTreeImpl pointer is still valid in the LayerImpl dtor.
+  root_layer_.reset();
 }
 
 static LayerImpl* findRootScrollLayer(LayerImpl* layer)
@@ -68,6 +71,21 @@
   scrolling_layer_id_from_previous_tree_ = 0;
 }
 
+LayerImpl* LayerTreeImpl::LayerById(int id) {
+  LayerIdMap::iterator iter = layer_id_map_.find(id);
+  return iter != layer_id_map_.end() ? iter->second : NULL;
+}
+
+void LayerTreeImpl::RegisterLayer(LayerImpl* layer) {
+  DCHECK(!LayerById(layer->id()));
+  layer_id_map_[layer->id()] = layer;
+}
+
+void LayerTreeImpl::UnregisterLayer(LayerImpl* layer) {
+  DCHECK(LayerById(layer->id()));
+  layer_id_map_.erase(layer->id());
+}
+
 const LayerTreeSettings& LayerTreeImpl::settings() const {
   return layer_tree_host_impl_->settings();
 }