xref: /aosp_15_r20/external/perfetto/ui/src/public/plugin.ts (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1// Copyright (C) 2024 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import {Trace} from './trace';
16import {App} from './app';
17
18/**
19 * This interface defines the shape of the plugins's class constructor (i.e. the
20 * the constructor and all static members of the plugin's class.
21 *
22 * This class constructor is registered with the core.
23 *
24 * On trace load, the core will create a new class instance by calling new on
25 * this constructor and then call its onTraceLoad() function.
26 */
27export interface PerfettoPluginStatic<T extends PerfettoPlugin> {
28  readonly id: string;
29  readonly dependencies?: ReadonlyArray<PerfettoPluginStatic<PerfettoPlugin>>;
30  onActivate?(app: App): void;
31  metricVisualisations?(): MetricVisualisation[];
32  new (trace: Trace): T;
33}
34
35/**
36 * This interface defines the shape of a plugin's trace-scoped instance, which
37 * is created from the class constructor above at trace load time.
38 */
39export interface PerfettoPlugin {
40  onTraceLoad?(ctx: Trace): Promise<void>;
41}
42
43export interface MetricVisualisation {
44  // The name of the metric e.g. 'android_camera'
45  metric: string;
46
47  // A vega or vega-lite visualisation spec.
48  // The data from the metric under path will be exposed as a
49  // datasource named "metric" in Vega(-Lite)
50  spec: string;
51
52  // A path index into the metric.
53  // For example if the metric returns the folowing protobuf:
54  // {
55  //   foo {
56  //     bar {
57  //       baz: { name: "a" }
58  //       baz: { name: "b" }
59  //       baz: { name: "c" }
60  //     }
61  //   }
62  // }
63  // That becomes the following json:
64  // { "foo": { "bar": { "baz": [
65  //  {"name": "a"},
66  //  {"name": "b"},
67  //  {"name": "c"},
68  // ]}}}
69  // And given path = ["foo", "bar", "baz"]
70  // We extract:
71  // [ {"name": "a"}, {"name": "b"}, {"name": "c"} ]
72  // And pass that to the vega(-lite) visualisation.
73  path: string[];
74}
75
76export interface PluginManager {
77  getPlugin<T extends PerfettoPlugin>(plugin: PerfettoPluginStatic<T>): T;
78  metricVisualisations(): MetricVisualisation[];
79}
80