• 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 {colorForTid} from '../../frontend/colorizer';
18import {globals} from '../../frontend/globals';
19import {Track} from '../../frontend/track';
20import {trackRegistry} from '../../frontend/track_registry';
21
22import {
23  Config,
24  Data,
25  PROCESS_SUMMARY_TRACK,
26} from './common';
27
28const MARGIN_TOP = 7;
29const RECT_HEIGHT = 30;
30
31class ProcessSummaryTrack extends Track<Config, Data> {
32  static readonly kind = PROCESS_SUMMARY_TRACK;
33  static create(trackState: TrackState): ProcessSummaryTrack {
34    return new ProcessSummaryTrack(trackState);
35  }
36
37  constructor(trackState: TrackState) {
38    super(trackState);
39  }
40
41  renderCanvas(ctx: CanvasRenderingContext2D): void {
42    const {timeScale, visibleWindowTime} = globals.frontendLocalState;
43    const data = this.data();
44
45    // If there aren't enough cached slices data in |data| request more to
46    // the controller.
47    const inRange = data !== undefined &&
48        (visibleWindowTime.start >= data.start &&
49         visibleWindowTime.end <= data.end);
50    if (!inRange || data === undefined ||
51        data.resolution !== globals.getCurResolution()) {
52      globals.requestTrackData(this.trackState.id);
53    }
54    if (data === undefined) return;  // Can't possibly draw anything.
55
56    checkerboardExcept(
57        ctx,
58        timeScale.timeToPx(visibleWindowTime.start),
59        timeScale.timeToPx(visibleWindowTime.end),
60        timeScale.timeToPx(data.start),
61        timeScale.timeToPx(data.end));
62
63    this.renderSummary(ctx, data);
64  }
65
66  // TODO(dproy): Dedup with CPU slices.
67  renderSummary(ctx: CanvasRenderingContext2D, data: Data): void {
68    const {timeScale, visibleWindowTime} = globals.frontendLocalState;
69    const startPx = Math.floor(timeScale.timeToPx(visibleWindowTime.start));
70    const bottomY = MARGIN_TOP + RECT_HEIGHT;
71
72    let lastX = startPx;
73    let lastY = bottomY;
74
75    // TODO(hjd): Dedupe this math.
76    const color = colorForTid(this.config.pidForColor);
77    color.l = Math.min(color.l + 10, 60);
78    color.s -= 20;
79
80    ctx.fillStyle = `hsl(${color.h}, ${color.s}%, ${color.l}%)`;
81    ctx.beginPath();
82    ctx.moveTo(lastX, lastY);
83    for (let i = 0; i < data.utilizations.length; i++) {
84      // TODO(dproy): Investigate why utilization is > 1 sometimes.
85      const utilization = Math.min(data.utilizations[i], 1);
86      const startTime = i * data.bucketSizeSeconds + data.start;
87
88      lastX = Math.floor(timeScale.timeToPx(startTime));
89
90      ctx.lineTo(lastX, lastY);
91      lastY = MARGIN_TOP + Math.round(RECT_HEIGHT * (1 - utilization));
92      ctx.lineTo(lastX, lastY);
93    }
94    ctx.lineTo(lastX, bottomY);
95    ctx.closePath();
96    ctx.fill();
97  }
98}
99
100trackRegistry.register(ProcessSummaryTrack);
101