1/*
2 * Copyright 2019, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import {transform, nanos_to_string, get_visible_chip} from './transform.js'
18
19function transform_wl_layer(layer) {
20  function is_visible(layer) {
21    return layer.parent == 0 || (layer.visibleInParent && layer.visible && (layer.hidden != 1));
22  }
23
24  var chips = [];
25  var rect = layer.displayFrame;
26  var visible = is_visible(layer);
27  if (visible && layer.parent != 0) {
28    chips.push(get_visible_chip());
29  }
30  if (!visible) {
31    rect = undefined;
32  }
33
34  return transform({
35    obj: layer,
36    kind: 'layer',
37    name: layer.name,
38    children: [
39      [layer.resolvedChildren, transform_wl_layer],
40    ],
41    rect,
42    highlight: rect,
43    chips,
44    visible,
45    stableId: layer.id,
46  });
47}
48
49function transform_wl_container(cntnr) {
50  var rect = cntnr.geometry;
51  var layersList = cntnr.layers || [];
52
53  return transform({
54    obj: cntnr,
55    kind: 'container',
56    name: cntnr.name,
57    children: [
58      [layersList, transform_wl_layer],
59    ],
60    rect,
61    highlight: rect,
62    stableId: cntnr.id,
63  });
64}
65
66function transform_wl_outputstate(layers) {
67  var containerList = layers.containers || [];
68  var fullBounds = layers.fullBounds;
69
70  return transform({
71    obj: {name: "Output State", fullBounds: fullBounds},
72    kind: 'outputstate',
73    name: 'Output State',
74    rect: fullBounds,
75    highlight: fullBounds,
76    children: [
77      [containerList, transform_wl_container],
78    ],
79  });
80}
81
82function transform_wl_entry(entry) {
83  return transform({
84    obj: entry,
85    kind: 'entry',
86    name: nanos_to_string(entry.elapsedRealtimeNanos) + " - " + entry.where,
87    children: [
88      [[entry.state], transform_wl_outputstate],
89    ],
90    timestamp: entry.elapsedRealtimeNanos,
91    stableId: 'entry',
92  });
93}
94
95function transform_wayland_trace(entries) {
96  var r = transform({
97    obj: entries,
98    kind: 'wltrace',
99    name: 'wltrace',
100    children: [
101      [entries.entry, transform_wl_entry],
102    ],
103  });
104
105  return r;
106}
107
108export {transform_wl_outputstate, transform_wayland_trace};
109