OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/common/wm/forwarding_layer_delegate.h" | 5 #include "ash/common/wm/forwarding_layer_delegate.h" |
6 | 6 |
7 #include "ash/common/wm_window.h" | 7 #include "ash/common/wm_window.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "ui/compositor/layer.h" | 9 #include "ui/compositor/layer.h" |
| 10 #include "ui/compositor/layer_owner.h" |
10 | 11 |
11 namespace ash { | 12 namespace ash { |
12 namespace wm { | 13 namespace wm { |
13 | 14 |
14 ForwardingLayerDelegate::ForwardingLayerDelegate(WmWindow* original_window, | 15 ForwardingLayerDelegate::ForwardingLayerDelegate(ui::Layer* new_layer, |
15 ui::LayerDelegate* delegate) | 16 ui::Layer* original_layer) |
16 : original_window_(original_window), original_delegate_(delegate) {} | 17 : client_layer_(new_layer), |
| 18 original_layer_(original_layer), |
| 19 scoped_observer_(this) { |
| 20 scoped_observer_.Add(original_layer); |
| 21 } |
17 | 22 |
18 ForwardingLayerDelegate::~ForwardingLayerDelegate() {} | 23 ForwardingLayerDelegate::~ForwardingLayerDelegate() {} |
19 | 24 |
20 void ForwardingLayerDelegate::OnPaintLayer(const ui::PaintContext& context) { | 25 void ForwardingLayerDelegate::OnPaintLayer(const ui::PaintContext& context) { |
21 if (!original_delegate_) | 26 if (original_layer_ && original_layer_->delegate()) |
22 return; | 27 original_layer_->delegate()->OnPaintLayer(context); |
23 // |original_delegate_| may have already been deleted or | |
24 // disconnected by this time. Check if |original_delegate_| is still | |
25 // used by the original_window tree, or skip otherwise. | |
26 if (IsDelegateValid(original_window_->GetLayer())) | |
27 original_delegate_->OnPaintLayer(context); | |
28 else | |
29 original_delegate_ = nullptr; | |
30 } | 28 } |
31 | 29 |
32 void ForwardingLayerDelegate::OnDelegatedFrameDamage( | 30 void ForwardingLayerDelegate::OnDelegatedFrameDamage( |
33 const gfx::Rect& damage_rect_in_dip) {} | 31 const gfx::Rect& damage_rect_in_dip) {} |
34 | 32 |
35 void ForwardingLayerDelegate::OnDeviceScaleFactorChanged( | 33 void ForwardingLayerDelegate::OnDeviceScaleFactorChanged( |
36 float device_scale_factor) { | 34 float device_scale_factor) { |
37 // Don't tell the original delegate about device scale factor change | 35 // Don't tell the original delegate about device scale factor change |
38 // on cloned layer because the original layer is still on the same display. | 36 // on cloned layer because the original layer is still on the same display. |
39 } | 37 } |
40 | 38 |
41 base::Closure ForwardingLayerDelegate::PrepareForLayerBoundsChange() { | 39 base::Closure ForwardingLayerDelegate::PrepareForLayerBoundsChange() { |
42 return base::Closure(); | 40 return base::Closure(); |
43 } | 41 } |
44 | 42 |
45 bool ForwardingLayerDelegate::IsDelegateValid(ui::Layer* layer) const { | 43 void ForwardingLayerDelegate::DidPaintLayer(ui::Layer* layer, |
46 if (layer->delegate() == original_delegate_) | 44 const gfx::Rect& rect) { |
47 return true; | 45 client_layer_->SchedulePaint(rect); |
48 for (auto* child : layer->children()) { | 46 } |
49 if (IsDelegateValid(child)) | 47 |
50 return true; | 48 void ForwardingLayerDelegate::SurfaceChanged(ui::Layer* layer) { |
51 } | 49 // This will delete the old layer and any descendants. |
52 return false; | 50 ui::LayerOwner old_client; |
| 51 old_client.SetLayer(client_layer_); |
| 52 |
| 53 ui::LayerOwner* owner = layer->owner(); |
| 54 // The layer recreation step isn't recursive, but layers with surfaces don't |
| 55 // tend to have children anyway. We may end up missing some children, but we |
| 56 // can also reach that state if layers are ever added or removed. |
| 57 // TODO(estade): address this if it ever becomes a practical issue. |
| 58 std::unique_ptr<ui::Layer> recreated = owner->RecreateLayer(); |
| 59 client_layer_ = recreated.get(); |
| 60 old_client.layer()->parent()->Add(recreated.release()); |
| 61 old_client.layer()->parent()->Remove(old_client.layer()); |
| 62 |
| 63 scoped_observer_.Remove(original_layer_); |
| 64 original_layer_ = owner->layer(); |
| 65 scoped_observer_.Add(original_layer_); |
| 66 } |
| 67 |
| 68 void ForwardingLayerDelegate::LayerDestroyed(ui::Layer* layer) { |
| 69 original_layer_ = nullptr; |
| 70 scoped_observer_.Remove(layer); |
53 } | 71 } |
54 | 72 |
55 } // namespace wm | 73 } // namespace wm |
56 } // namespace ash | 74 } // namespace ash |
OLD | NEW |