• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (C) 2021 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 {Actions} from '../common/actions';
16import {
17  Color,
18  hslForSlice,
19} from '../common/colorizer';
20import {STR_NULL} from '../common/query_result';
21
22import {
23  BASE_SLICE_ROW,
24  BaseSliceTrack,
25  BaseSliceTrackTypes,
26  OnSliceClickArgs,
27  OnSliceOverArgs,
28} from './base_slice_track';
29import {globals} from './globals';
30import {NewTrackArgs} from './track';
31
32export const NAMED_SLICE_ROW = {
33  // Base columns (tsq, ts, dur, id, depth).
34  ...BASE_SLICE_ROW,
35
36  // Impl-specific columns.
37  name: STR_NULL,
38};
39export type NamedSliceRow = typeof NAMED_SLICE_ROW;
40
41export interface NamedSliceTrackTypes extends BaseSliceTrackTypes {
42  row: NamedSliceRow;
43}
44
45export abstract class NamedSliceTrack<
46    T extends NamedSliceTrackTypes = NamedSliceTrackTypes> extends
47    BaseSliceTrack<T> {
48  constructor(args: NewTrackArgs) {
49    super(args);
50  }
51
52  // This is used by the base class to call iter().
53  getRowSpec(): T['row'] {
54    return NAMED_SLICE_ROW;
55  }
56
57  // Converts a SQL result row to an "Impl" Slice.
58  rowToSlice(row: T['row']): T['slice'] {
59    const baseSlice = super.rowToSlice(row);
60    // Ignore PIDs or numeric arguments when hashing.
61    const name = row.name || '';
62    const nameForHashing = name.replace(/\s?\d+/g, '');
63    const hsl = hslForSlice(nameForHashing, /*isSelected=*/ false);
64    // We cache the color so we hash only once per query.
65    const baseColor: Color = {c: '', h: hsl[0], s: hsl[1], l: hsl[2]};
66    return {...baseSlice, title: name, baseColor};
67  }
68
69  onSliceOver(args: OnSliceOverArgs<T['slice']>) {
70    const name = args.slice.title;
71    args.tooltip = [name];
72  }
73
74  onSliceClick(args: OnSliceClickArgs<T['slice']>) {
75    globals.makeSelection(Actions.selectChromeSlice({
76      id: args.slice.id,
77      trackId: this.trackId,
78
79      // |table| here can be either 'slice' or 'annotation'. The
80      // AnnotationSliceTrack overrides the onSliceClick and sets this to
81      // 'annotation'
82      table: 'slice',
83    }));
84  }
85}
86