1// Copyright (c) 2012 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 "ui/compositor/layer_owner.h" 6 7#include "ui/compositor/layer_owner_delegate.h" 8 9namespace ui { 10 11LayerOwner::LayerOwner() : layer_(NULL), layer_owner_delegate_(NULL) { 12} 13 14LayerOwner::~LayerOwner() { 15} 16 17void LayerOwner::SetLayer(Layer* layer) { 18 DCHECK(!OwnsLayer()); 19 layer_owner_.reset(layer); 20 layer_ = layer; 21 layer_->owner_ = this; 22} 23 24scoped_ptr<Layer> LayerOwner::AcquireLayer() { 25 if (layer_owner_) 26 layer_owner_->owner_ = NULL; 27 return layer_owner_.Pass(); 28} 29 30scoped_ptr<Layer> LayerOwner::RecreateLayer() { 31 scoped_ptr<ui::Layer> old_layer(AcquireLayer()); 32 if (!old_layer) 33 return old_layer.Pass(); 34 35 LayerDelegate* old_delegate = old_layer->delegate(); 36 old_layer->set_delegate(NULL); 37 38 const gfx::Rect layer_bounds(old_layer->bounds()); 39 Layer* new_layer = new ui::Layer(old_layer->type()); 40 SetLayer(new_layer); 41 new_layer->SetVisible(old_layer->GetTargetVisibility()); 42 new_layer->SetOpacity(old_layer->GetTargetOpacity()); 43 new_layer->SetBounds(layer_bounds); 44 new_layer->SetMasksToBounds(old_layer->GetMasksToBounds()); 45 new_layer->set_name(old_layer->name()); 46 new_layer->SetFillsBoundsOpaquely(old_layer->fills_bounds_opaquely()); 47 new_layer->SetFillsBoundsCompletely(old_layer->FillsBoundsCompletely()); 48 new_layer->SetSubpixelPositionOffset(old_layer->subpixel_position_offset()); 49 SkRegion* alpha_shape = old_layer->alpha_shape(); 50 if (alpha_shape) 51 new_layer->SetAlphaShape(make_scoped_ptr(new SkRegion(*alpha_shape))); 52 53 // Install new layer as a sibling of the old layer, stacked below it. 54 if (old_layer->parent()) { 55 old_layer->parent()->Add(new_layer); 56 old_layer->parent()->StackBelow(new_layer, old_layer.get()); 57 } 58 59 // Migrate all the child layers over to the new layer. Copy the list because 60 // the items are removed during iteration. 61 std::vector<ui::Layer*> children_copy = old_layer->children(); 62 for (std::vector<ui::Layer*>::const_iterator it = children_copy.begin(); 63 it != children_copy.end(); 64 ++it) { 65 ui::Layer* child = *it; 66 new_layer->Add(child); 67 } 68 69 // Install the delegate last so that the delegate isn't notified as we copy 70 // state to the new layer. 71 new_layer->set_delegate(old_delegate); 72 73 if (layer_owner_delegate_) 74 layer_owner_delegate_->OnLayerRecreated(old_layer.get(), new_layer); 75 76 return old_layer.Pass(); 77} 78 79void LayerOwner::DestroyLayer() { 80 layer_ = NULL; 81 layer_owner_.reset(); 82} 83 84bool LayerOwner::OwnsLayer() const { 85 return !!layer_owner_; 86} 87 88} // namespace ui 89