chrome-devtools-frontend
Version:
Chrome DevTools UI
108 lines (95 loc) • 4.21 kB
text/typescript
// Copyright 2023 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import * as i18n from '../../core/i18n/i18n.js';
import * as TraceEngine from '../../models/trace/trace.js';
import {
type TrackAppender,
type TrackAppenderName,
type CompatibilityTracksAppender,
type HighlightedEntryInfo,
} from './CompatibilityTracksAppender.js';
import {buildGroupStyle, buildTrackHeader, getFormattedTime} from './AppenderUtils.js';
const UIStrings = {
/**
*@description Text in Timeline Flame Chart Data Provider of the Performance panel
*/
gpu: 'GPU',
};
const str_ = i18n.i18n.registerUIStrings('panels/timeline/GPUTrackAppender.ts', UIStrings);
const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_);
export class GPUTrackAppender implements TrackAppender {
readonly appenderName: TrackAppenderName = 'GPU';
#compatibilityBuilder: CompatibilityTracksAppender;
#traceParsedData: Readonly<TraceEngine.TraceModel.PartialTraceParseDataDuringMigration>;
constructor(
compatibilityBuilder: CompatibilityTracksAppender,
traceParsedData: TraceEngine.TraceModel.PartialTraceParseDataDuringMigration) {
this.#compatibilityBuilder = compatibilityBuilder;
this.#traceParsedData = traceParsedData;
}
/**
* Appends into the flame chart data the data corresponding to the
* GPU track.
* @param trackStartLevel the horizontal level of the flame chart events where
* the track's events will start being appended.
* @param expanded wether the track should be rendered expanded.
* @returns the first available level to append more data after having
* appended the track's events.
*/
appendTrackAtLevel(trackStartLevel: number, expanded?: boolean|undefined): number {
const gpuEvents = this.#traceParsedData.GPU.mainGPUThreadTasks;
if (gpuEvents.length === 0) {
return trackStartLevel;
}
this.#appendTrackHeaderAtLevel(trackStartLevel, expanded);
return this.#compatibilityBuilder.appendEventsAtLevel(gpuEvents, trackStartLevel, this);
}
/**
* Adds into the flame chart data the header corresponding to the
* GPU track. A header is added in the shape of a group in the
* flame chart data. A group has a predefined style and a reference
* to the definition of the legacy track (which should be removed
* in the future).
* @param currentLevel the flame chart level at which the header is
* appended.
* @param expanded wether the track should be rendered expanded.
*/
#appendTrackHeaderAtLevel(currentLevel: number, expanded?: boolean): void {
const style = buildGroupStyle({shareHeaderLine: false});
const group = buildTrackHeader(currentLevel, i18nString(UIStrings.gpu), style, /* selectable= */ true, expanded);
this.#compatibilityBuilder.registerTrackForGroup(group, this);
}
/*
------------------------------------------------------------------------------------
The following methods are invoked by the flame chart renderer to query features about
events on rendering.
------------------------------------------------------------------------------------
*/
/**
* Gets the color an event added by this appender should be rendered with.
*/
colorForEvent(event: TraceEngine.Types.TraceEvents.TraceEventData): string {
if (!TraceEngine.Types.TraceEvents.isTraceEventGPUTask(event)) {
throw new Error(`Unexpected GPU Task: The event's type is '${event.name}'`);
}
return 'hsl(109, 33%, 55%)';
}
/**
* Gets the title an event added by this appender should be rendered with.
*/
titleForEvent(event: TraceEngine.Types.TraceEvents.TraceEventData): string {
if (TraceEngine.Types.TraceEvents.isTraceEventGPUTask(event)) {
return 'GPU';
}
return event.name;
}
/**
* Returns the info shown when an event added by this appender
* is hovered in the timeline.
*/
highlightedEntryInfo(event: TraceEngine.Types.TraceEvents.TraceEventData): HighlightedEntryInfo {
const title = this.titleForEvent(event);
return {title, formattedTime: getFormattedTime(event.dur)};
}
}