• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (C) 2018 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 {TrackState} from '../../common/state';
16import {checkerboardExcept} from '../../frontend/checkerboard';
17import {globals} from '../../frontend/globals';
18import {Track} from '../../frontend/track';
19import {trackRegistry} from '../../frontend/track_registry';
20
21import {Config, Data, KIND} from './common';
22
23// TODO(hjd): De-dupe this from ChromeSliceTrack, CpuSliceTrack and VsyncTrack.
24const MARGIN_TOP = 5;
25const RECT_HEIGHT = 30;
26
27class VsyncTrack extends Track<Config, Data> {
28  static readonly kind = KIND;
29  static create(trackState: TrackState): VsyncTrack {
30    return new VsyncTrack(trackState);
31  }
32
33  constructor(trackState: TrackState) {
34    super(trackState);
35  }
36
37  renderCanvas(ctx: CanvasRenderingContext2D): void {
38    const {timeScale, visibleWindowTime} = globals.frontendLocalState;
39
40    const data = this.data();
41    if (data === undefined) return;  // Can't possibly draw anything.
42
43    const dataStartPx = timeScale.timeToPx(data.start);
44    const dataEndPx = timeScale.timeToPx(data.end);
45    const visibleStartPx = timeScale.timeToPx(visibleWindowTime.start);
46    const visibleEndPx = timeScale.timeToPx(visibleWindowTime.end);
47
48    checkerboardExcept(
49        ctx,
50        this.getHeight(),
51        visibleStartPx,
52        visibleEndPx,
53        dataStartPx,
54        dataEndPx);
55
56    const bgColor = '#5E909B';
57    const fgColor = '#323D48';
58
59    const startPx = Math.floor(Math.max(dataStartPx, visibleStartPx));
60    const endPx = Math.floor(Math.min(dataEndPx, visibleEndPx));
61
62    ctx.fillStyle = bgColor;
63    ctx.fillRect(startPx, MARGIN_TOP, endPx - startPx, RECT_HEIGHT);
64
65    ctx.fillStyle = fgColor;
66    for (let i = 0; i < data.vsyncs.length; i += 2) {
67      const leftPx = Math.floor(timeScale.timeToPx(data.vsyncs[i]));
68      const rightPx = Math.floor(timeScale.timeToPx(data.vsyncs[i + 1]));
69      if (rightPx < startPx) continue;
70      // TODO(hjd): Do some thing better when very zoomed out.
71      if ((rightPx - leftPx) <= 1) continue;
72      if (leftPx > endPx) break;
73      ctx.fillRect(leftPx, MARGIN_TOP, rightPx - leftPx, RECT_HEIGHT);
74    }
75  }
76}
77
78trackRegistry.register(VsyncTrack);
79