File: layers_as_json.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 (186 lines) | stat: -rw-r--r-- 6,625 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright 2019 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/compositing/layers_as_json.h"

#include "cc/layers/layer.h"
#include "third_party/blink/renderer/platform/geometry/geometry_as_json.h"
#include "third_party/blink/renderer/platform/graphics/color.h"
#include "third_party/blink/renderer/platform/graphics/compositing/content_layer_client_impl.h"
#include "third_party/blink/renderer/platform/graphics/paint/transform_paint_property_node.h"
#include "ui/gfx/geometry/point_f.h"

namespace blink {

namespace {

String PointerAsString(const void* ptr) {
  return String::Format("%p", ptr);
}

double RoundCloseToZero(double number) {
  return std::abs(number) < 1e-7 ? 0 : number;
}

std::unique_ptr<JSONArray> TransformAsJSONArray(const gfx::Transform& t) {
  auto array = std::make_unique<JSONArray>();
  for (int c = 0; c < 4; c++) {
    auto col = std::make_unique<JSONArray>();
    for (int r = 0; r < 4; r++)
      col->PushDouble(RoundCloseToZero(t.rc(r, c)));
    array->PushArray(std::move(col));
  }
  return array;
}

}  // namespace

// Create a JSON version of the specified |layer|.
std::unique_ptr<JSONObject> CCLayerAsJSON(const cc::Layer& layer,
                                          LayerTreeFlags flags) {
  auto json = std::make_unique<JSONObject>();

  if (flags & kLayerTreeIncludesDebugInfo) {
    json->SetString("this", PointerAsString(&layer));
    json->SetInteger("ccLayerId", layer.id());
  }

  String debug_name(layer.DebugName());
  json->SetString("name", debug_name);

  if (layer.offset_to_transform_parent() != gfx::Vector2dF()) {
    json->SetArray("position",
                   VectorAsJSONArray(layer.offset_to_transform_parent()));
  }

  // This is testing against gfx::Size(), *not* whether the size is empty.
  if (layer.bounds() != gfx::Size())
    json->SetArray("bounds", SizeAsJSONArray(layer.bounds()));

  if (layer.contents_opaque())
    json->SetBoolean("contentsOpaque", true);
  else if (layer.contents_opaque_for_text())
    json->SetBoolean("contentsOpaqueForText", true);

  if (!layer.draws_content())
    json->SetBoolean("drawsContent", false);

  if (layer.should_check_backface_visibility())
    json->SetString("backfaceVisibility", "hidden");

  if (!Color::FromSkColor4f(layer.background_color()).IsFullyTransparent() &&
      ((flags & kLayerTreeIncludesDebugInfo) ||
       // Omit backgroundColor for these layers because it's not interesting
       // and we want to avoid platform differences and changes with CLs
       // affecting backgroundColor in web tests that dump layer trees.
       (debug_name != "Caret" && !debug_name.Contains("Scroll corner of")))) {
    json->SetString("backgroundColor",
                    Color::FromSkColor4f(layer.background_color())
                        .NameForLayoutTreeAsText());
  }

  if (flags &
      (kLayerTreeIncludesDebugInfo | kLayerTreeIncludesCompositingReasons)) {
    if (layer.debug_info()) {
      auto compositing_reasons_json = std::make_unique<JSONArray>();
      for (const char* name : layer.debug_info()->compositing_reasons)
        compositing_reasons_json->PushString(name);
      json->SetArray("compositingReasons", std::move(compositing_reasons_json));
    }
  }

  if ((flags & kLayerTreeIncludesDebugInfo) &&
      layer.hit_test_opaqueness() != cc::HitTestOpaqueness::kOpaque) {
    json->SetString("hitTestOpaqueness",
                    cc::HitTestOpaquenessToString(layer.hit_test_opaqueness()));
  }

  return json;
}

LayersAsJSON::LayersAsJSON(LayerTreeFlags flags)
    : flags_(flags),
      next_transform_id_(1),
      layers_json_(std::make_unique<JSONArray>()),
      transforms_json_(std::make_unique<JSONArray>()) {}

int LayersAsJSON::AddTransformJSON(
    const TransformPaintPropertyNode& transform) {
  auto it = transform_id_map_.find(&transform);
  if (it != transform_id_map_.end())
    return it->value;

  int parent_id = 0;
  if (transform.Parent())
    parent_id = AddTransformJSON(*transform.UnaliasedParent());
  if (transform.IsIdentity() && !transform.RenderingContextId()) {
    transform_id_map_.Set(&transform, parent_id);
    return parent_id;
  }

  auto transform_json = std::make_unique<JSONObject>();
  int transform_id = next_transform_id_++;
  transform_id_map_.Set(&transform, transform_id);
  transform_json->SetInteger("id", transform_id);
  if (parent_id)
    transform_json->SetInteger("parent", parent_id);

  if (!transform.IsIdentity()) {
    transform_json->SetArray("transform",
                             TransformAsJSONArray(transform.Matrix()));
  }

  if (!transform.Matrix().IsIdentityOrTranslation()) {
    transform_json->SetArray("origin", Point3AsJSONArray(transform.Origin()));
  }

  if (!transform.FlattensInheritedTransform())
    transform_json->SetBoolean("flattenInheritedTransform", false);

  if (auto rendering_context = transform.RenderingContextId()) {
    auto context_lookup_result = rendering_context_map_.find(rendering_context);
    int rendering_id = rendering_context_map_.size() + 1;
    if (context_lookup_result == rendering_context_map_.end())
      rendering_context_map_.Set(rendering_context, rendering_id);
    else
      rendering_id = context_lookup_result->value;

    transform_json->SetInteger("renderingContext", rendering_id);
  }

  transforms_json_->PushObject(std::move(transform_json));
  return transform_id;
}

void LayersAsJSON::AddLayer(const cc::Layer& layer,
                            const TransformPaintPropertyNode& transform,
                            const ContentLayerClientImpl* layer_client) {
  if (!(flags_ & kLayerTreeIncludesAllLayers) && !layer.draws_content()) {
    std::string debug_name = layer.DebugName();
    if (debug_name == "LayoutView #document" ||
        debug_name == "Inner Viewport Scroll Layer" ||
        debug_name == "Scrolling Contents Layer") {
      return;
    }
  }

  auto layer_json = CCLayerAsJSON(layer, flags_);
  if (layer_client) {
    layer_client->AppendAdditionalInfoAsJSON(flags_, layer, *layer_json);
  }
  int transform_id = AddTransformJSON(transform);
  if (transform_id)
    layer_json->SetInteger("transform", transform_id);
  layers_json_->PushObject(std::move(layer_json));
}

std::unique_ptr<JSONObject> LayersAsJSON::Finalize() {
  auto json = std::make_unique<JSONObject>();
  json->SetArray("layers", std::move(layers_json_));
  if (transforms_json_->size())
    json->SetArray("transforms", std::move(transforms_json_));
  return json;
}

}  // namespace blink