File: layer_owner.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (103 lines) | stat: -rw-r--r-- 2,738 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/compositor/layer_owner.h"

#include <utility>

#include "base/memory/raw_ptr.h"
#include "base/observer_list.h"
#include "ui/compositor/compositor.h"
#include "ui/compositor/layer.h"

namespace ui {

LayerOwner::LayerOwner(std::unique_ptr<Layer> layer) {
  if (layer)
    SetLayer(std::move(layer));
}

LayerOwner::~LayerOwner() = default;

void LayerOwner::AddObserver(Observer* observer) {
  observers_.AddObserver(observer);
}

void LayerOwner::RemoveObserver(Observer* observer) {
  observers_.RemoveObserver(observer);
}

void LayerOwner::SetLayer(std::unique_ptr<Layer> layer) {
  DCHECK(!OwnsLayer());
  layer_owner_ = std::move(layer);
  layer_ = layer_owner_.get();
  layer_->owner_ = this;
}

std::unique_ptr<Layer> LayerOwner::AcquireLayer() {
  if (layer_owner_)
    layer_owner_->owner_ = nullptr;
  return std::move(layer_owner_);
}

std::unique_ptr<Layer> LayerOwner::ReleaseLayer() {
  layer_ = nullptr;
  return AcquireLayer();
}

void LayerOwner::Reset(std::unique_ptr<Layer> layer) {
  ReleaseLayer();
  SetLayer(std::move(layer));
}

std::unique_ptr<Layer> LayerOwner::RecreateLayer() {
  std::unique_ptr<ui::Layer> old_layer(AcquireLayer());
  if (!old_layer)
    return old_layer;

  LayerDelegate* old_delegate = old_layer->delegate();
  old_layer->set_delegate(nullptr);

  SetLayer(old_layer->Clone());

  if (old_layer->parent()) {
    // Install new layer as a sibling of the old layer, stacked below it.
    old_layer->parent()->Add(layer_);
    old_layer->parent()->StackBelow(layer_, old_layer.get());
  } else if (old_layer->GetCompositor()) {
    // If old_layer was the layer tree root then we need to move the Compositor
    // over to the new root.
    old_layer->GetCompositor()->SetRootLayer(layer_);
  }

  // Migrate all the child layers over to the new layer. Copy the list because
  // the items are removed during iteration.
  std::vector<raw_ptr<ui::Layer, VectorExperimental>> children_copy =
      old_layer->children();
  for (std::vector<raw_ptr<ui::Layer, VectorExperimental>>::const_iterator it =
           children_copy.begin();
       it != children_copy.end(); ++it) {
    ui::Layer* child = *it;
    layer_->Add(child);
  }

  // Install the delegate last so that the delegate isn't notified as we copy
  // state to the new layer.
  layer_->set_delegate(old_delegate);

  observers_.Notify(&Observer::OnLayerRecreated, old_layer.get());

  return old_layer;
}

void LayerOwner::DestroyLayer() {
  layer_ = nullptr;
  layer_owner_.reset();
}

bool LayerOwner::OwnsLayer() const {
  return !!layer_owner_;
}

}  // namespace ui