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.
5 #include "ui/compositor/layer_owner.h"
7 #include "ui/compositor/layer_owner_delegate.h"
11 LayerOwner::LayerOwner() : layer_(NULL
), layer_owner_delegate_(NULL
) {
14 LayerOwner::~LayerOwner() {
17 void LayerOwner::SetLayer(Layer
* layer
) {
19 layer_owner_
.reset(layer
);
21 layer_
->owner_
= this;
24 scoped_ptr
<Layer
> LayerOwner::AcquireLayer() {
26 layer_owner_
->owner_
= NULL
;
27 return layer_owner_
.Pass();
30 scoped_ptr
<Layer
> LayerOwner::RecreateLayer() {
31 scoped_ptr
<ui::Layer
> old_layer(AcquireLayer());
33 return old_layer
.Pass();
35 LayerDelegate
* old_delegate
= old_layer
->delegate();
36 old_layer
->set_delegate(NULL
);
38 const gfx::Rect
layer_bounds(old_layer
->bounds());
39 Layer
* new_layer
= new ui::Layer(old_layer
->type());
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 if (old_layer
->type() == LAYER_SOLID_COLOR
)
50 new_layer
->SetColor(old_layer
->GetTargetColor());
51 SkRegion
* alpha_shape
= old_layer
->alpha_shape();
53 new_layer
->SetAlphaShape(make_scoped_ptr(new SkRegion(*alpha_shape
)));
55 // Install new layer as a sibling of the old layer, stacked below it.
56 if (old_layer
->parent()) {
57 old_layer
->parent()->Add(new_layer
);
58 old_layer
->parent()->StackBelow(new_layer
, old_layer
.get());
61 // Migrate all the child layers over to the new layer. Copy the list because
62 // the items are removed during iteration.
63 std::vector
<ui::Layer
*> children_copy
= old_layer
->children();
64 for (std::vector
<ui::Layer
*>::const_iterator it
= children_copy
.begin();
65 it
!= children_copy
.end();
67 ui::Layer
* child
= *it
;
68 new_layer
->Add(child
);
71 // If old_layer was the layer tree root then we need to move the Compositor
72 // over to the new root.
73 const bool is_root
= !old_layer
->parent();
74 if (is_root
&& old_layer
->GetCompositor())
75 old_layer
->GetCompositor()->SetRootLayer(new_layer
);
77 // Install the delegate last so that the delegate isn't notified as we copy
78 // state to the new layer.
79 new_layer
->set_delegate(old_delegate
);
81 if (layer_owner_delegate_
)
82 layer_owner_delegate_
->OnLayerRecreated(old_layer
.get(), new_layer
);
84 return old_layer
.Pass();
87 void LayerOwner::DestroyLayer() {
92 bool LayerOwner::OwnsLayer() const {
93 return !!layer_owner_
;