-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
829 additions
and
362 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// This file contains a collection of classes which make it easier to perform | ||
// batch rendering of Canvas2D primitives. The advantage of this over just doing | ||
// ctx.beginPath() ... ctx.rect(...) ... ctx.endPath() is that you can construct | ||
// several different batch renderers are the same time, then decide on their | ||
// paint order at the end. | ||
// | ||
// See FlamechartPanZoomView.renderOverlays for an example of how this is used. | ||
|
||
export interface TextArgs { | ||
text: string | ||
x: number | ||
y: number | ||
} | ||
|
||
export class BatchCanvasTextRenderer { | ||
private argsBatch: TextArgs[] = [] | ||
|
||
text(args: TextArgs) { | ||
this.argsBatch.push(args) | ||
} | ||
|
||
fill(ctx: CanvasRenderingContext2D, color: string) { | ||
if (this.argsBatch.length === 0) return | ||
ctx.fillStyle = color | ||
for (let args of this.argsBatch) { | ||
ctx.fillText(args.text, args.x, args.y) | ||
} | ||
this.argsBatch = [] | ||
} | ||
} | ||
|
||
export interface RectArgs { | ||
x: number | ||
y: number | ||
w: number | ||
h: number | ||
} | ||
|
||
export class BatchCanvasRectRenderer { | ||
private argsBatch: RectArgs[] = [] | ||
|
||
rect(args: RectArgs) { | ||
this.argsBatch.push(args) | ||
} | ||
|
||
private drawPath(ctx: CanvasRenderingContext2D) { | ||
ctx.beginPath() | ||
for (let args of this.argsBatch) { | ||
ctx.rect(args.x, args.y, args.w, args.h) | ||
} | ||
ctx.closePath() | ||
this.argsBatch = [] | ||
} | ||
|
||
fill(ctx: CanvasRenderingContext2D, color: string) { | ||
if (this.argsBatch.length === 0) return | ||
ctx.fillStyle = color | ||
this.drawPath(ctx) | ||
ctx.fill() | ||
} | ||
|
||
stroke(ctx: CanvasRenderingContext2D, color: string, lineWidth: number) { | ||
if (this.argsBatch.length === 0) return | ||
ctx.strokeStyle = color | ||
ctx.lineWidth = lineWidth | ||
this.drawPath(ctx) | ||
ctx.stroke() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import {Profile, Frame, CallTreeNode} from './profile' | ||
import {FuzzyMatch, fuzzyMatchStrings} from './fuzzy-find' | ||
import {Flamechart, FlamechartFrame} from './flamechart' | ||
import {Rect, Vec2} from './math' | ||
|
||
export enum FlamechartType { | ||
CHRONO_FLAME_CHART, | ||
LEFT_HEAVY_FLAME_GRAPH, | ||
} | ||
|
||
// A utility class for storing cached search results to avoid recomputation when | ||
// the search results & profile did not change. | ||
export class ProfileSearchResults { | ||
constructor(readonly profile: Profile, readonly searchQuery: string) {} | ||
|
||
private matches: Map<Frame, FuzzyMatch> | null = null | ||
getMatchForFrame(frame: Frame): FuzzyMatch | null { | ||
if (!this.matches) { | ||
this.matches = new Map() | ||
this.profile.forEachFrame(frame => { | ||
const match = fuzzyMatchStrings(frame.name, this.searchQuery) | ||
if (match == null) return | ||
this.matches!.set(frame, match) | ||
}) | ||
} | ||
return this.matches.get(frame) || null | ||
} | ||
} | ||
|
||
export interface FlamechartSearchMatch { | ||
configSpaceBounds: Rect | ||
node: CallTreeNode | ||
} | ||
|
||
interface CachedFlamechartResult { | ||
matches: FlamechartSearchMatch[] | ||
indexForNode: Map<CallTreeNode, number> | ||
} | ||
|
||
export class FlamechartSearchResults { | ||
constructor(readonly flamechart: Flamechart, readonly profileResults: ProfileSearchResults) {} | ||
|
||
private matches: CachedFlamechartResult | null = null | ||
private getResults(): CachedFlamechartResult { | ||
if (this.matches == null) { | ||
const matches: FlamechartSearchMatch[] = [] | ||
const indexForNode = new Map<CallTreeNode, number>() | ||
const visit = (frame: FlamechartFrame, depth: number) => { | ||
const {node} = frame | ||
if (this.profileResults.getMatchForFrame(node.frame)) { | ||
const configSpaceBounds = new Rect( | ||
new Vec2(frame.start, depth), | ||
new Vec2(frame.end - frame.start, 1), | ||
) | ||
indexForNode.set(node, matches.length) | ||
matches.push({configSpaceBounds, node}) | ||
} | ||
|
||
frame.children.forEach(child => { | ||
visit(child, depth + 1) | ||
}) | ||
} | ||
|
||
const layers = this.flamechart.getLayers() | ||
if (layers.length > 0) { | ||
layers[0].forEach(frame => visit(frame, 0)) | ||
} | ||
|
||
this.matches = {matches, indexForNode} | ||
} | ||
return this.matches | ||
} | ||
|
||
count(): number { | ||
return this.getResults().matches.length | ||
} | ||
|
||
indexOf(node: CallTreeNode): number | null { | ||
const result = this.getResults().indexForNode.get(node) | ||
return result === undefined ? null : result | ||
} | ||
|
||
at(index: number): FlamechartSearchMatch { | ||
const matches = this.getResults().matches | ||
if (index < 0 || index >= matches.length) { | ||
throw new Error(`Index ${index} out of bounds in list of ${matches.length} matches.`) | ||
} | ||
return matches[index] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.