File: paint_property_node.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 (167 lines) | stat: -rw-r--r-- 4,699 bytes parent folder | download | duplicates (4)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/platform/graphics/paint/paint_property_node.h"

#include "third_party/blink/renderer/platform/graphics/paint/clip_paint_property_node.h"
#include "third_party/blink/renderer/platform/graphics/paint/effect_paint_property_node.h"
#include "third_party/blink/renderer/platform/graphics/paint/scroll_paint_property_node.h"
#include "third_party/blink/renderer/platform/graphics/paint/transform_paint_property_node.h"

namespace blink {

int PaintPropertyNode::NodeDepthOrFoundAncestor(
    const PaintPropertyNode& maybe_ancestor) const {
  int depth = 0;
  for (const auto* n = this; n; n = n->Parent()) {
    if (n == &maybe_ancestor) {
      return -1;
    }
    depth++;
  }
  return depth;
}

const PaintPropertyNode& PaintPropertyNode::LowestCommonAncestorInternal(
    const PaintPropertyNode& other) const {
  // Measure both depths.
  auto depth_a = NodeDepthOrFoundAncestor(other);
  if (depth_a == -1) {
    return other;
  }
  auto depth_b = other.NodeDepthOrFoundAncestor(*this);
  if (depth_b == -1) {
    return *this;
  }

  const auto* a_ptr = this;
  const auto* b_ptr = &other;

  // Make it so depth_a >= depth_b.
  if (depth_a < depth_b) {
    std::swap(a_ptr, b_ptr);
    std::swap(depth_a, depth_b);
  }

  // Make it so depth_a == depth_b.
  while (depth_a > depth_b) {
    a_ptr = a_ptr->Parent();
    depth_a--;
  }

  // Walk up until we find the ancestor.
  while (a_ptr != b_ptr) {
    a_ptr = a_ptr->Parent();
    b_ptr = b_ptr->Parent();
  }

  DCHECK(a_ptr) << "Malformed property tree. All nodes must be descendant of "
                   "the same root.";
  return *a_ptr;
}

String PaintPropertyNode::ToString() const {
  String s = ToJSON()->ToJSONString();
#if DCHECK_IS_ON()
  return debug_name_ + String::Format(" %p ", this) + s;
#else
  return s;
#endif
}

std::unique_ptr<JSONObject> PaintPropertyNode::ToJSON() const {
  auto json = std::make_unique<JSONObject>();
  if (Parent()) {
    json->SetString("parent", String::Format("%p", Parent()));
  }
  if (IsParentAlias()) {
    json->SetBoolean("is_alias", true);
  }
  if (NodeChanged() != PaintPropertyChangeType::kUnchanged) {
    json->SetString("changed", PaintPropertyChangeTypeToString(NodeChanged()));
  }
  return json;
}

const char* PaintPropertyChangeTypeToString(PaintPropertyChangeType change) {
  switch (change) {
    case PaintPropertyChangeType::kUnchanged:
      return "unchanged";
    case PaintPropertyChangeType::kChangedOnlyCompositedValues:
      return "composited-values";
    case PaintPropertyChangeType::kChangedOnlyNonRerasterValues:
      return "non-reraster";
    case PaintPropertyChangeType::kChangedOnlySimpleValues:
      return "simple-values";
    case PaintPropertyChangeType::kChangedOnlyValues:
      return "values";
    case PaintPropertyChangeType::kNodeAddedOrRemoved:
      return "node-add-remove";
  }
}

#if DCHECK_IS_ON()

String PaintPropertyNode::ToTreeString() const {
  return PropertyTreePrinter().PathAsString(*this);
}

void PropertyTreePrinter::AddNode(const PaintPropertyNode* node) {
  if (node) {
    nodes_.insert(node);
  }
}

String PropertyTreePrinter::NodesAsTreeString() {
  if (nodes_.empty()) {
    return "";
  }
  StringBuilder string_builder;
  BuildTreeString(string_builder, RootNode(), 0);
  return string_builder.ToString();
}

String PropertyTreePrinter::PathAsString(const PaintPropertyNode& last_node) {
  for (const auto* n = &last_node; n; n = n->Parent()) {
    AddNode(n);
  }
  return NodesAsTreeString();
}

void PropertyTreePrinter::BuildTreeString(StringBuilder& string_builder,
                                          const PaintPropertyNode& node,
                                          unsigned indent) {
  for (unsigned i = 0; i < indent; i++) {
    string_builder.Append(' ');
  }

  string_builder.Append(node.DebugName());
  string_builder.Append(String::Format(" %p ", &node));
  auto json = node.ToJSON();
  json->Remove("parent");
  string_builder.Append(json->ToJSONString());
  string_builder.Append("\n");

  for (const auto& child_node : nodes_) {
    if (child_node->Parent() == &node) {
      BuildTreeString(string_builder, *child_node, indent + 2);
    }
  }
}

const PaintPropertyNode& PropertyTreePrinter::RootNode() {
  const auto* node = nodes_.back().Get();
  while (!node->IsRoot()) {
    node = node->Parent();
  }
  if (node->DebugName().empty()) {
    const_cast<PaintPropertyNode*>(node)->SetDebugName("root");
  }
  nodes_.insert(node);
  return *node;
}

#endif  // DCHECK_IS_ON()

}  // namespace blink