Merge pull request #19856 from calixteman/bug1936605

Don't update the visible canvas at 60 fps (bug 1936605)
This commit is contained in:
calixteman 2025-04-29 22:31:02 +02:00 committed by GitHub
commit b8de9a372f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 70 additions and 3 deletions

View File

@ -326,8 +326,6 @@ const PDFViewerApplication = {
} }
} }
if (params.has("pdfbug")) { if (params.has("pdfbug")) {
AppOptions.setAll({ pdfBug: true, fontExtraProperties: true });
const enabled = params.get("pdfbug").split(","); const enabled = params.get("pdfbug").split(",");
try { try {
await loadPDFBug(); await loadPDFBug();
@ -335,6 +333,12 @@ const PDFViewerApplication = {
} catch (ex) { } catch (ex) {
console.error("_parseHashParams:", ex); console.error("_parseHashParams:", ex);
} }
const debugOpts = { pdfBug: true, fontExtraProperties: true };
if (globalThis.StepperManager?.enabled) {
debugOpts.minDurationToUpdateCanvas = 0;
}
AppOptions.setAll(debugOpts);
} }
// It is not possible to change locale for the (various) extension builds. // It is not possible to change locale for the (various) extension builds.
if ( if (
@ -519,6 +523,7 @@ const PDFViewerApplication = {
enableHWA, enableHWA,
supportsPinchToZoom: this.supportsPinchToZoom, supportsPinchToZoom: this.supportsPinchToZoom,
enableAutoLinking: AppOptions.get("enableAutoLinking"), enableAutoLinking: AppOptions.get("enableAutoLinking"),
minDurationToUpdateCanvas: AppOptions.get("minDurationToUpdateCanvas"),
})); }));
renderingQueue.setViewer(pdfViewer); renderingQueue.setViewer(pdfViewer);

View File

@ -302,6 +302,11 @@ const defaultOptions = {
value: 2 ** 25, value: 2 ** 25,
kind: OptionKind.VIEWER, kind: OptionKind.VIEWER,
}, },
minDurationToUpdateCanvas: {
/** @type {number} */
value: 500, // ms
kind: OptionKind.VIEWER,
},
forcePageColors: { forcePageColors: {
/** @type {boolean} */ /** @type {boolean} */
value: false, value: false,

View File

@ -21,12 +21,18 @@ class BasePDFPageView {
#loadingId = null; #loadingId = null;
#minDurationToUpdateCanvas = 0;
#renderError = null; #renderError = null;
#renderingState = RenderingStates.INITIAL; #renderingState = RenderingStates.INITIAL;
#showCanvas = null; #showCanvas = null;
#startTime = 0;
#tempCanvas = null;
canvas = null; canvas = null;
/** @type {null | HTMLDivElement} */ /** @type {null | HTMLDivElement} */
@ -51,6 +57,7 @@ class BasePDFPageView {
this.id = options.id; this.id = options.id;
this.pageColors = options.pageColors || null; this.pageColors = options.pageColors || null;
this.renderingQueue = options.renderingQueue; this.renderingQueue = options.renderingQueue;
this.#minDurationToUpdateCanvas = options.minDurationToUpdateCanvas ?? 500;
} }
get renderingState() { get renderingState() {
@ -71,6 +78,9 @@ class BasePDFPageView {
switch (state) { switch (state) {
case RenderingStates.PAUSED: case RenderingStates.PAUSED:
this.div.classList.remove("loading"); this.div.classList.remove("loading");
// Display the canvas as it has been drawn.
this.#startTime = 0;
this.#showCanvas?.(false);
break; break;
case RenderingStates.RUNNING: case RenderingStates.RUNNING:
this.div.classList.add("loadingIcon"); this.div.classList.add("loadingIcon");
@ -82,10 +92,12 @@ class BasePDFPageView {
this.div.classList.add("loading"); this.div.classList.add("loading");
this.#loadingId = null; this.#loadingId = null;
}, 0); }, 0);
this.#startTime = Date.now();
break; break;
case RenderingStates.INITIAL: case RenderingStates.INITIAL:
case RenderingStates.FINISHED: case RenderingStates.FINISHED:
this.div.classList.remove("loadingIcon", "loading"); this.div.classList.remove("loadingIcon", "loading");
this.#startTime = 0;
break; break;
} }
} }
@ -100,10 +112,41 @@ class BasePDFPageView {
// have a final flash we just display it once all the drawing is done. // have a final flash we just display it once all the drawing is done.
const updateOnFirstShow = !prevCanvas && !hasHCM && !hideUntilComplete; const updateOnFirstShow = !prevCanvas && !hasHCM && !hideUntilComplete;
const canvas = (this.canvas = document.createElement("canvas")); let canvas = (this.canvas = document.createElement("canvas"));
this.#showCanvas = isLastShow => { this.#showCanvas = isLastShow => {
if (updateOnFirstShow) { if (updateOnFirstShow) {
let tempCanvas = this.#tempCanvas;
if (!isLastShow && this.#minDurationToUpdateCanvas > 0) {
// We draw on the canvas at 60fps (in using `requestAnimationFrame`),
// so if the canvas is large, updating it at 60fps can be a way too
// much and can cause some serious performance issues.
// To avoid that we only update the canvas every
// `this.#minDurationToUpdateCanvas` ms.
if (Date.now() - this.#startTime < this.#minDurationToUpdateCanvas) {
return;
}
if (!tempCanvas) {
tempCanvas = this.#tempCanvas = canvas;
canvas = this.canvas = canvas.cloneNode(false);
onShow(canvas);
}
}
if (tempCanvas) {
const ctx = canvas.getContext("2d", {
alpha: false,
});
ctx.drawImage(tempCanvas, 0, 0);
if (isLastShow) {
this.#resetTempCanvas();
} else {
this.#startTime = Date.now();
}
return;
}
// Don't add the canvas until the first draw callback, or until // Don't add the canvas until the first draw callback, or until
// drawing is complete when `!this.renderingQueue`, to prevent black // drawing is complete when `!this.renderingQueue`, to prevent black
// flickering. // flickering.
@ -152,6 +195,14 @@ class BasePDFPageView {
canvas.remove(); canvas.remove();
canvas.width = canvas.height = 0; canvas.width = canvas.height = 0;
this.canvas = null; this.canvas = null;
this.#resetTempCanvas();
}
#resetTempCanvas() {
if (this.#tempCanvas) {
this.#tempCanvas.width = this.#tempCanvas.height = 0;
this.#tempCanvas = null;
}
} }
async _drawCanvas(options, onCancel, onFinish) { async _drawCanvas(options, onCancel, onFinish) {

View File

@ -139,6 +139,8 @@ function isValidAnnotationEditorMode(mode) {
* The default value is `true`. * The default value is `true`.
* @property {boolean} [enableAutoLinking] - Enable creation of hyperlinks from * @property {boolean} [enableAutoLinking] - Enable creation of hyperlinks from
* text that look like URLs. The default value is `true`. * text that look like URLs. The default value is `true`.
* @property {number} [minDurationToUpdateCanvas] - Minimum duration to wait
* before updating the canvas. The default value is `500`.
*/ */
class PDFPageViewBuffer { class PDFPageViewBuffer {
@ -243,6 +245,8 @@ class PDFViewer {
#eventAbortController = null; #eventAbortController = null;
#minDurationToUpdateCanvas = 0;
#mlManager = null; #mlManager = null;
#scrollTimeoutId = null; #scrollTimeoutId = null;
@ -342,6 +346,7 @@ class PDFViewer {
this.#enableHWA = options.enableHWA || false; this.#enableHWA = options.enableHWA || false;
this.#supportsPinchToZoom = options.supportsPinchToZoom !== false; this.#supportsPinchToZoom = options.supportsPinchToZoom !== false;
this.#enableAutoLinking = options.enableAutoLinking !== false; this.#enableAutoLinking = options.enableAutoLinking !== false;
this.#minDurationToUpdateCanvas = options.minDurationToUpdateCanvas ?? 500;
this.defaultRenderingQueue = !options.renderingQueue; this.defaultRenderingQueue = !options.renderingQueue;
if ( if (
@ -1003,6 +1008,7 @@ class PDFViewer {
layerProperties: this._layerProperties, layerProperties: this._layerProperties,
enableHWA: this.#enableHWA, enableHWA: this.#enableHWA,
enableAutoLinking: this.#enableAutoLinking, enableAutoLinking: this.#enableAutoLinking,
minDurationToUpdateCanvas: this.#minDurationToUpdateCanvas,
}); });
this._pages.push(pageView); this._pages.push(pageView);
} }