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 {nanos_to_string} from './transform.js';
18
19function transform_transaction(transaction, layerIdToName) {
20  const transactions = [];
21
22  for (const surfaceChange of transaction.surfaceChange) {
23    transactions.push(Object.freeze({
24      type: 'surfaceChange',
25      obj: surfaceChange,
26      layerName: layerIdToName[surfaceChange.id],
27    }));
28  }
29
30  for (const displayChange of transaction.displayChange) {
31    transactions.push(Object.freeze({
32      type: 'displayChange',
33      obj: displayChange,
34      layerName: layerIdToName[displayChange.id],
35    }));
36  }
37
38  return transactions;
39}
40
41function transform_entry(entry, layerIdToName) {
42  const type = entry.increment;
43  const timestamp = entry.timeStamp;
44  const time = nanos_to_string(timestamp);
45
46  switch (type) {
47    case 'transaction':
48
49      return Object.freeze({
50        type,
51        // TODO: Rename to changes
52        transactions: transform_transaction(entry.transaction, layerIdToName),
53        synchronous: entry.transaction.synchronous,
54        animation: entry.transaction.animation,
55        identifier: entry.transaction.id,
56        time,
57        origin: entry.transaction.origin,
58        timestamp,
59      });
60
61    case 'surfaceCreation':
62      // NOTE: There is no break on purpose — we want to fall through to default
63      layerIdToName[entry[type].id] = entry[type].name;
64
65    default:
66      return Object.freeze({
67        type,
68        obj: entry[type],
69        layerName: entry[type].name ?? layerIdToName[entry[type].id],
70        time,
71        timestamp,
72      });
73  }
74}
75
76function transform_transaction_trace(entries) {
77  const layerIdToName = {};
78  const data = entries.increment.map((entry) => transform_entry(entry, layerIdToName));
79
80  return {children: data};
81}
82
83export {transform_transaction_trace};
84