Skip to content

Commit 4d80dcc

Browse files
debug: log rendering time
1 parent f68dd33 commit 4d80dcc

File tree

1 file changed

+106
-1
lines changed

1 file changed

+106
-1
lines changed

web/pdf_page_view.js

+106-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,81 @@ import { XfaLayerBuilder } from "./xfa_layer_builder.js";
5656
// enable the feature?
5757
const ENABLE_ZOOM_DETAIL = true;
5858

59+
class Recorder {
60+
constructor(kind) {
61+
this.kind = kind;
62+
this.accumulatedTime = 0;
63+
this.startTime = 0;
64+
this.currentStartTime = 0;
65+
this.running = false;
66+
}
67+
68+
start() {
69+
if (this.running) {
70+
console.warn(`Start ${this.kind} recorder while running`);
71+
return;
72+
}
73+
74+
console.log(`Start rendering ${this.kind}`);
75+
this.currentStartTime = this.startTime = performance.now();
76+
this.accumulatedTime = 0;
77+
this.running = true;
78+
}
79+
80+
pause() {
81+
if (!this.running) {
82+
console.warn(`Pause ${this.kind} recorder while not running`);
83+
return;
84+
}
85+
86+
console.log(`Pause rendering ${this.kind}`);
87+
this.accumulatedTime += performance.now() - this.currentStartTime;
88+
this.running = false;
89+
}
90+
91+
resume() {
92+
if (this.running) {
93+
console.warn(`Resume ${this.kind} recorder while running`);
94+
return;
95+
}
96+
97+
console.log(`Resume rendering ${this.kind}`);
98+
this.currentStartTime = performance.now();
99+
this.running = true;
100+
}
101+
102+
finish() {
103+
if (!this.running) {
104+
console.warn(`Finish ${this.kind} recorder while not running`);
105+
return;
106+
}
107+
108+
this.accumulatedTime += performance.now() - this.currentStartTime;
109+
const totalTime = performance.now() - this.startTime;
110+
this.running = false;
111+
112+
console.log(
113+
`Finish renderig ${this.kind} (self: ${this.accumulatedTime}ms, total: ${totalTime}ms)`
114+
);
115+
}
116+
117+
cancel() {
118+
if (this.running) {
119+
this.accumulatedTime += performance.now() - this.currentStartTime;
120+
this.running = false;
121+
}
122+
123+
const totalTime = performance.now() - this.startTime;
124+
125+
console.log(
126+
`Cancel renderig ${this.kind} (self: ${this.accumulatedTime}ms, total: ${totalTime}ms)`
127+
);
128+
}
129+
}
130+
131+
const detailRecorder = new Recorder("foreground");
132+
const backgroundRecorder = new Recorder("background");
133+
59134
/**
60135
* @typedef {Object} PDFPageViewOptions
61136
* @property {HTMLDivElement} [container] - The viewer element.
@@ -253,8 +328,10 @@ class PDFPageViewBase {
253328
#renderContinueCallback = cont => {
254329
this.#showCanvas?.(false);
255330
if (this.renderingQueue && !this.renderingQueue.isHighestPriority(this)) {
331+
this.recorder.pause();
256332
this.renderingState = RenderingStates.PAUSED;
257333
this.resume = () => {
334+
this.recorder.resume();
258335
this.renderingState = RenderingStates.RUNNING;
259336
cont();
260337
};
@@ -327,6 +404,12 @@ class PDFPageViewBase {
327404
}
328405

329406
cancelRendering({ cancelExtraDelay = 0 } = {}) {
407+
if (
408+
this.renderingState !== RenderingStates.INITIAL &&
409+
this.renderingState !== RenderingStates.FINISHED
410+
) {
411+
this.recorder.cancel();
412+
}
330413
if (this.renderTask) {
331414
this.renderTask.cancel(cancelExtraDelay);
332415
this.renderTask = null;
@@ -349,6 +432,8 @@ class PDFPageViewBase {
349432
* @implements {IRenderableView}
350433
*/
351434
class PDFPageView extends PDFPageViewBase {
435+
recorder = backgroundRecorder;
436+
352437
#annotationMode = AnnotationMode.ENABLE_FORMS;
353438

354439
#hasRestrictedScaling = false;
@@ -1038,6 +1123,8 @@ class PDFPageView extends PDFPageViewBase {
10381123
}
10391124

10401125
async draw() {
1126+
this.recorder.start();
1127+
10411128
if (this.renderingState !== RenderingStates.INITIAL) {
10421129
console.error("Must be in new state before drawing");
10431130
this.reset(); // Ensure that we reset all state to prevent issues.
@@ -1138,6 +1225,15 @@ class PDFPageView extends PDFPageViewBase {
11381225
} else {
11391226
this.#hasRestrictedScaling = false;
11401227
}
1228+
console.log({
1229+
hasRestrictedScaling: this.#hasRestrictedScaling,
1230+
maxCanvasPixels: this.maxCanvasPixels.toLocaleString(),
1231+
pixelsInViewport: Math.round(pixelsInViewport).toLocaleString(),
1232+
outputScaleSx: outputScale.sx,
1233+
outputScaleSy: outputScale.sy,
1234+
maxScale: maxScale,
1235+
ratio: window.devicePixelRatio
1236+
});
11411237
}
11421238
const sfx = approximateFraction(outputScale.sx);
11431239
const sfy = approximateFraction(outputScale.sy);
@@ -1182,6 +1278,8 @@ class PDFPageView extends PDFPageViewBase {
11821278
renderContext,
11831279
prevCanvas,
11841280
renderTask => {
1281+
this.recorder.finish();
1282+
11851283
// Ensure that the thumbnails won't become partially (or fully) blank,
11861284
// for documents that contain interactive form elements.
11871285
this.#useThumbnailCanvas.regularAnnotations =
@@ -1289,6 +1387,8 @@ class PDFPageView extends PDFPageViewBase {
12891387
* @implements {IRenderableView}
12901388
*/
12911389
class PDFPageDetailView extends PDFPageViewBase {
1390+
recorder = detailRecorder;
1391+
12921392
constructor({ pageView }) {
12931393
super(pageView);
12941394

@@ -1390,6 +1490,8 @@ class PDFPageDetailView extends PDFPageViewBase {
13901490
}
13911491

13921492
async draw() {
1493+
this.recorder.start();
1494+
13931495
const initialRenderingState = this.renderingState;
13941496
if (initialRenderingState !== RenderingStates.INITIAL) {
13951497
console.error("Must be in new state before drawing");
@@ -1448,7 +1550,10 @@ class PDFPageDetailView extends PDFPageViewBase {
14481550
viewport,
14491551
pageColors: this.pageColors,
14501552
},
1451-
prevCanvas
1553+
prevCanvas,
1554+
() => {
1555+
this.recorder.finish();
1556+
}
14521557
);
14531558

14541559
div.setAttribute("data-loaded", true);

0 commit comments

Comments
 (0)