• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {canvasClip} from '../../base/canvas_utils';
17import {Size2D} from '../../base/geom';
18import {TimeScale} from '../../base/time_scale';
19import {getOrCreate} from '../../base/utils';
20import {TraceImpl} from '../../core/trace_impl';
21import {TRACK_SHELL_WIDTH} from '../css_constants';
22import {generateTicks, getMaxMajorTicks, TickType} from './gridline_helper';
23import {SearchOverviewTrack} from './search_overview_track';
24
25// We want to create the overview track only once per trace, but this
26// class can be delete and re-instantiated when switching between pages via
27// the sidebar. So we cache the overview track and bind it to the lifetime of
28// the TraceImpl object.
29const trackTraceMap = new WeakMap<TraceImpl, SearchOverviewTrack>();
30
31// This is used to display the summary of search results.
32export class TickmarkPanel {
33  private searchOverviewTrack: SearchOverviewTrack;
34  readonly height = 5;
35
36  constructor(private readonly trace: TraceImpl) {
37    this.searchOverviewTrack = getOrCreate(
38      trackTraceMap,
39      trace,
40      () => new SearchOverviewTrack(trace),
41    );
42  }
43
44  render(): m.Children {
45    return m('', {style: {height: `${this.height}px`}});
46  }
47
48  renderCanvas(ctx: CanvasRenderingContext2D, size: Size2D): void {
49    ctx.fillStyle = '#999';
50    ctx.fillRect(TRACK_SHELL_WIDTH - 1, 0, 1, size.height);
51
52    const trackSize = {...size, width: size.width - TRACK_SHELL_WIDTH};
53    ctx.save();
54    ctx.translate(TRACK_SHELL_WIDTH, 0);
55    canvasClip(ctx, 0, 0, trackSize.width, trackSize.height);
56    this.renderTrack(ctx, trackSize);
57    ctx.restore();
58  }
59
60  private renderTrack(ctx: CanvasRenderingContext2D, size: Size2D): void {
61    const visibleWindow = this.trace.timeline.visibleWindow;
62    const timescale = new TimeScale(visibleWindow, {
63      left: 0,
64      right: size.width,
65    });
66    const timespan = visibleWindow.toTimeSpan();
67
68    if (size.width > 0 && timespan.duration > 0n) {
69      const maxMajorTicks = getMaxMajorTicks(size.width);
70
71      const offset = this.trace.timeline.timestampOffset();
72      const tickGen = generateTicks(timespan, maxMajorTicks, offset);
73      for (const {type, time} of tickGen) {
74        const px = Math.floor(timescale.timeToPx(time));
75        if (type === TickType.MAJOR) {
76          ctx.fillRect(px, 0, 1, size.height);
77        }
78      }
79    }
80
81    this.searchOverviewTrack.render(ctx, size);
82  }
83}
84