layer_owner.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #include "ui/compositor/layer_owner.h"
  5. #include <utility>
  6. #include "base/observer_list.h"
  7. #include "ui/compositor/compositor.h"
  8. #include "ui/compositor/layer.h"
  9. namespace ui {
  10. LayerOwner::LayerOwner(std::unique_ptr<Layer> layer) {
  11. if (layer)
  12. SetLayer(std::move(layer));
  13. }
  14. LayerOwner::~LayerOwner() = default;
  15. void LayerOwner::AddObserver(Observer* observer) {
  16. observers_.AddObserver(observer);
  17. }
  18. void LayerOwner::RemoveObserver(Observer* observer) {
  19. observers_.RemoveObserver(observer);
  20. }
  21. void LayerOwner::SetLayer(std::unique_ptr<Layer> layer) {
  22. DCHECK(!OwnsLayer());
  23. layer_owner_ = std::move(layer);
  24. layer_ = layer_owner_.get();
  25. layer_->owner_ = this;
  26. }
  27. std::unique_ptr<Layer> LayerOwner::AcquireLayer() {
  28. if (layer_owner_)
  29. layer_owner_->owner_ = nullptr;
  30. return std::move(layer_owner_);
  31. }
  32. std::unique_ptr<Layer> LayerOwner::ReleaseLayer() {
  33. layer_ = nullptr;
  34. return AcquireLayer();
  35. }
  36. void LayerOwner::Reset(std::unique_ptr<Layer> layer) {
  37. ReleaseLayer();
  38. SetLayer(std::move(layer));
  39. }
  40. std::unique_ptr<Layer> LayerOwner::RecreateLayer() {
  41. std::unique_ptr<ui::Layer> old_layer(AcquireLayer());
  42. if (!old_layer)
  43. return old_layer;
  44. LayerDelegate* old_delegate = old_layer->delegate();
  45. old_layer->set_delegate(nullptr);
  46. SetLayer(old_layer->Clone());
  47. if (old_layer->parent()) {
  48. // Install new layer as a sibling of the old layer, stacked below it.
  49. old_layer->parent()->Add(layer_);
  50. old_layer->parent()->StackBelow(layer_, old_layer.get());
  51. } else if (old_layer->GetCompositor()) {
  52. // If old_layer was the layer tree root then we need to move the Compositor
  53. // over to the new root.
  54. old_layer->GetCompositor()->SetRootLayer(layer_);
  55. }
  56. // Migrate all the child layers over to the new layer. Copy the list because
  57. // the items are removed during iteration.
  58. std::vector<ui::Layer*> children_copy = old_layer->children();
  59. for (std::vector<ui::Layer*>::const_iterator it = children_copy.begin();
  60. it != children_copy.end();
  61. ++it) {
  62. ui::Layer* child = *it;
  63. layer_->Add(child);
  64. }
  65. // Install the delegate last so that the delegate isn't notified as we copy
  66. // state to the new layer.
  67. layer_->set_delegate(old_delegate);
  68. for (auto& observer : observers_)
  69. observer.OnLayerRecreated(old_layer.get());
  70. return old_layer;
  71. }
  72. void LayerOwner::DestroyLayer() {
  73. layer_ = nullptr;
  74. layer_owner_.reset();
  75. }
  76. bool LayerOwner::OwnsLayer() const {
  77. return !!layer_owner_;
  78. }
  79. } // namespace ui