• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (C) 2023 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 {uuidv4} from '../../base/uuid';
16import {AddTrackArgs} from '../../common/actions';
17import {GenericSliceDetailsTabConfig} from '../../frontend/generic_slice_details_tab';
18import {
19  BottomTabToSCSAdapter,
20  NUM,
21  Plugin,
22  PluginContextTrace,
23  PluginDescriptor,
24  PrimaryTrackSortKey,
25} from '../../public';
26import {Engine} from '../../trace_processor/engine';
27
28import {ScreenshotTab} from './screenshot_panel';
29import {ScreenshotsTrack} from './screenshots_track';
30
31export type DecideTracksResult = {
32  tracksToAdd: AddTrackArgs[];
33};
34
35// TODO(stevegolton): Use suggestTrack().
36export async function decideTracks(
37  engine: Engine,
38): Promise<DecideTracksResult> {
39  const result: DecideTracksResult = {
40    tracksToAdd: [],
41  };
42
43  const res = await engine.query(`
44    INCLUDE PERFETTO MODULE android.screenshots;
45    select
46      count() as count
47    from android_screenshots
48  `);
49  const {count} = res.firstRow({count: NUM});
50
51  if (count > 0) {
52    result.tracksToAdd.push({
53      uri: 'perfetto.Screenshots',
54      name: 'Screenshots',
55      trackSortKey: PrimaryTrackSortKey.ASYNC_SLICE_TRACK,
56    });
57  }
58
59  return result;
60}
61
62class ScreenshotsPlugin implements Plugin {
63  async onTraceLoad(ctx: PluginContextTrace): Promise<void> {
64    const res = await ctx.engine.query(`
65      INCLUDE PERFETTO MODULE android.screenshots;
66      select
67        count() as count
68      from android_screenshots
69    `);
70    const {count} = res.firstRow({count: NUM});
71
72    if (count > 0) {
73      const displayName = 'Screenshots';
74      const uri = 'perfetto.Screenshots';
75      ctx.registerTrack({
76        uri,
77        displayName,
78        kind: ScreenshotsTrack.kind,
79        trackFactory: ({trackKey}) => {
80          return new ScreenshotsTrack({
81            engine: ctx.engine,
82            trackKey,
83          });
84        },
85      });
86
87      ctx.registerDetailsPanel(
88        new BottomTabToSCSAdapter({
89          tabFactory: (selection) => {
90            if (
91              selection.kind === 'GENERIC_SLICE' &&
92              selection.detailsPanelConfig.kind === ScreenshotTab.kind
93            ) {
94              const config = selection.detailsPanelConfig.config;
95              return new ScreenshotTab({
96                config: config as GenericSliceDetailsTabConfig,
97                engine: ctx.engine,
98                uuid: uuidv4(),
99              });
100            }
101            return undefined;
102          },
103        }),
104      );
105    }
106  }
107}
108
109export const plugin: PluginDescriptor = {
110  pluginId: 'perfetto.Screenshots',
111  plugin: ScreenshotsPlugin,
112};
113