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 {createQuerySliceTrack} from '../../components/tracks/query_slice_track'; 16import {PerfettoPlugin} from '../../public/plugin'; 17import {Trace} from '../../public/trace'; 18import {TrackNode} from '../../public/workspace'; 19 20const INCLUDE_DESKTOP_MODULE_QUERY = `INCLUDE PERFETTO MODULE android.desktop_mode`; 21 22const QUERY = ` 23SELECT 24 ROW_NUMBER() OVER (ORDER BY ts) AS id, 25 ts, 26 dur, 27 ifnull(p.package_name, 'uid=' || dw.uid) AS name 28FROM android_desktop_mode_windows dw 29LEFT JOIN package_list p ON CAST (dw.uid AS INT) % 100000 = p.uid AND p.uid != 1000 30`; 31 32const COLUMNS = ['id', 'ts', 'dur', 'name']; 33const TRACK_NAME = 'Desktop Mode Windows'; 34const TRACK_URI = '/desktop_windows'; 35 36export default class implements PerfettoPlugin { 37 static readonly id = 'dev.perfetto.AndroidDesktopMode'; 38 39 async onTraceLoad(ctx: Trace): Promise<void> { 40 await ctx.engine.query(INCLUDE_DESKTOP_MODULE_QUERY); 41 await this.registerTrack(ctx, QUERY); 42 ctx.commands.registerCommand({ 43 id: 'dev.perfetto.DesktopMode#AddTrackDesktopWindowss', 44 name: 'Add Track: ' + TRACK_NAME, 45 callback: () => this.addSimpleTrack(ctx), 46 }); 47 } 48 49 private async registerTrack(ctx: Trace, sql: string) { 50 const track = await createQuerySliceTrack({ 51 trace: ctx, 52 uri: TRACK_URI, 53 data: { 54 sqlSource: sql, 55 columns: COLUMNS, 56 }, 57 }); 58 ctx.tracks.registerTrack({ 59 uri: TRACK_URI, 60 title: TRACK_NAME, 61 track, 62 }); 63 } 64 65 private addSimpleTrack(ctx: Trace) { 66 const trackNode = new TrackNode({uri: TRACK_URI, title: TRACK_NAME}); 67 ctx.workspace.addChildInOrder(trackNode); 68 trackNode.pin(); 69 } 70} 71