xref: /aosp_15_r20/external/perfetto/ui/src/frontend/tickmark_panel.ts (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1// Copyright (C) 2019 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use size 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 m from 'mithril';
16import {TRACK_SHELL_WIDTH} from './css_constants';
17import {getMaxMajorTicks, generateTicks, TickType} from './gridline_helper';
18import {Size2D} from '../base/geom';
19import {Panel} from './panel_container';
20import {TimeScale} from '../base/time_scale';
21import {canvasClip} from '../base/canvas_utils';
22import {SearchOverviewTrack} from './search_overview_track';
23import {TraceImpl} from '../core/trace_impl';
24import {getOrCreate} from '../base/utils';
25
26// We want to create the overview track only once per trace, but this
27// class can be delete and re-instantiated when switching between pages via
28// the sidebar. So we cache the overview track and bind it to the lifetime of
29// the TraceImpl object.
30const trackTraceMap = new WeakMap<TraceImpl, SearchOverviewTrack>();
31
32// This is used to display the summary of search results.
33export class TickmarkPanel implements Panel {
34  readonly kind = 'panel';
35  readonly selectable = false;
36  private searchOverviewTrack: SearchOverviewTrack;
37
38  constructor(private readonly trace: TraceImpl) {
39    this.searchOverviewTrack = getOrCreate(
40      trackTraceMap,
41      trace,
42      () => new SearchOverviewTrack(trace),
43    );
44  }
45
46  render(): m.Children {
47    return m('.tickbar');
48  }
49
50  renderCanvas(ctx: CanvasRenderingContext2D, size: Size2D): void {
51    ctx.fillStyle = '#999';
52    ctx.fillRect(TRACK_SHELL_WIDTH - 2, 0, 2, size.height);
53
54    const trackSize = {...size, width: size.width - TRACK_SHELL_WIDTH};
55    ctx.save();
56    ctx.translate(TRACK_SHELL_WIDTH, 0);
57    canvasClip(ctx, 0, 0, trackSize.width, trackSize.height);
58    this.renderTrack(ctx, trackSize);
59    ctx.restore();
60  }
61
62  private renderTrack(ctx: CanvasRenderingContext2D, size: Size2D): void {
63    const visibleWindow = this.trace.timeline.visibleWindow;
64    const timescale = new TimeScale(visibleWindow, {
65      left: 0,
66      right: size.width,
67    });
68    const timespan = visibleWindow.toTimeSpan();
69
70    if (size.width > 0 && timespan.duration > 0n) {
71      const maxMajorTicks = getMaxMajorTicks(size.width);
72
73      const offset = this.trace.timeline.timestampOffset();
74      const tickGen = generateTicks(timespan, maxMajorTicks, offset);
75      for (const {type, time} of tickGen) {
76        const px = Math.floor(timescale.timeToPx(time));
77        if (type === TickType.MAJOR) {
78          ctx.fillRect(px, 0, 1, size.height);
79        }
80      }
81    }
82
83    this.searchOverviewTrack.render(ctx, size);
84  }
85}
86