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 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 15 16import {search, searchEq} from '../../base/binary_search'; 17import {Actions} from '../../common/actions'; 18import {cropText} from '../../common/canvas_utils'; 19import {TrackState} from '../../common/state'; 20import {translateState} from '../../common/thread_state'; 21import {colorForState} from '../../frontend/colorizer'; 22import {globals} from '../../frontend/globals'; 23import {Track} from '../../frontend/track'; 24import {trackRegistry} from '../../frontend/track_registry'; 25 26import { 27 Config, 28 Data, 29 groupBusyStates, 30 THREAD_STATE_TRACK_KIND, 31} from './common'; 32 33const MARGIN_TOP = 5; 34const RECT_HEIGHT = 12; 35 36class ThreadStateTrack extends Track<Config, Data> { 37 static readonly kind = THREAD_STATE_TRACK_KIND; 38 static create(trackState: TrackState): ThreadStateTrack { 39 return new ThreadStateTrack(trackState); 40 } 41 42 constructor(trackState: TrackState) { 43 super(trackState); 44 } 45 46 getHeight(): number { 47 return 22; 48 } 49 50 renderCanvas(ctx: CanvasRenderingContext2D): void { 51 const {timeScale, visibleWindowTime} = globals.frontendLocalState; 52 const data = this.data(); 53 const charWidth = ctx.measureText('dbpqaouk').width / 8; 54 55 // If there aren't enough cached slices data in |data| request more to 56 // the controller. 57 const inRange = data !== undefined && 58 (visibleWindowTime.start >= data.start && 59 visibleWindowTime.end <= data.end); 60 if (!inRange || data === undefined || 61 data.resolution !== globals.getCurResolution()) { 62 globals.requestTrackData(this.trackState.id); 63 } 64 if (data === undefined) return; // Can't possibly draw anything. 65 66 for (let i = 0; i < data.starts.length; i++) { 67 const tStart = data.starts[i]; 68 const tEnd = data.ends[i]; 69 const state = data.strings[data.state[i]]; 70 if (tEnd <= visibleWindowTime.start || tStart >= visibleWindowTime.end) { 71 continue; 72 } 73 if (tStart && tEnd) { 74 // Don't display a slice for Task Dead. 75 if (state === 'x') continue; 76 const rectStart = timeScale.timeToPx(tStart); 77 const rectEnd = timeScale.timeToPx(tEnd); 78 const color = colorForState(state); 79 ctx.fillStyle = `hsl(${color.h},${color.s}%,${color.l}%)`; 80 let rectWidth = rectEnd - rectStart; 81 if (groupBusyStates(data.resolution) && rectWidth < 1) { 82 rectWidth = 1; 83 } 84 ctx.fillRect(rectStart, MARGIN_TOP, rectWidth, RECT_HEIGHT); 85 86 // Don't render text when we have less than 5px to play with. 87 if (rectWidth < 5) continue; 88 ctx.textAlign = 'center'; 89 const title = cropText(translateState(state), charWidth, rectWidth); 90 const rectXCenter = rectStart + rectWidth / 2; 91 ctx.fillStyle = color.l < 80 ? '#fff' : '#404040'; 92 ctx.font = '10px Google Sans'; 93 ctx.fillText(title, rectXCenter, MARGIN_TOP + RECT_HEIGHT / 2 + 3); 94 } 95 } 96 97 const selection = globals.state.currentSelection; 98 if (selection !== null && selection.kind === 'THREAD_STATE' && 99 selection.utid === this.config.utid) { 100 const [startIndex, endIndex] = searchEq(data.starts, selection.ts); 101 if (startIndex !== endIndex) { 102 const tStart = data.starts[startIndex]; 103 const tEnd = data.ends[startIndex]; 104 const state = data.strings[data.state[startIndex]]; 105 const rectStart = timeScale.timeToPx(tStart); 106 const rectEnd = timeScale.timeToPx(tEnd); 107 const color = colorForState(state); 108 ctx.strokeStyle = `hsl(${color.h},${color.s}%,${color.l * 0.7}%)`; 109 ctx.beginPath(); 110 ctx.lineWidth = 3; 111 ctx.strokeRect( 112 rectStart, MARGIN_TOP - 1.5, rectEnd - rectStart, RECT_HEIGHT + 3); 113 ctx.closePath(); 114 } 115 } 116 } 117 118 onMouseClick({x}: {x: number}) { 119 const data = this.data(); 120 if (data === undefined) return false; 121 const {timeScale} = globals.frontendLocalState; 122 const time = timeScale.pxToTime(x); 123 const index = search(data.starts, time); 124 const ts = index === -1 ? undefined : data.starts[index]; 125 const tsEnd = index === -1 ? undefined : data.ends[index]; 126 const state = index === -1 ? undefined : data.strings[data.state[index]]; 127 const utid = this.config.utid; 128 if (ts && state && tsEnd) { 129 globals.dispatch( 130 Actions.selectThreadState({utid, ts, dur: tsEnd - ts, state})); 131 return true; 132 } 133 return false; 134 } 135} 136 137trackRegistry.register(ThreadStateTrack); 138