Skip to content

Commit 23972e1

Browse files
Merge pull request #19661 from Snuffleupagus/thumbnails-inline-finishRenderTask
Inline the `PDFThumbnailView.prototype.#finishRenderTask` helper method
2 parents 4b2683e + 6548c9f commit 23972e1

File tree

1 file changed

+47
-56
lines changed

1 file changed

+47
-56
lines changed

web/pdf_thumbnail_view.js

+47-56
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ const DRAW_UPSCALE_FACTOR = 2; // See comment in `PDFThumbnailView.draw` below.
3131
const MAX_NUM_SCALING_STEPS = 3;
3232
const THUMBNAIL_WIDTH = 98; // px
3333

34+
function zeroCanvas(c) {
35+
// Zeroing the width and height causes Firefox to release graphics
36+
// resources immediately, which can greatly reduce memory consumption.
37+
c.width = 0;
38+
c.height = 0;
39+
}
40+
3441
/**
3542
* @typedef {Object} PDFThumbnailViewOptions
3643
* @property {HTMLDivElement} container - The viewer element.
@@ -74,12 +81,8 @@ class TempImageFactory {
7481
}
7582

7683
static destroyCanvas() {
77-
const tempCanvas = this.#tempCanvas;
78-
if (tempCanvas) {
79-
// Zeroing the width and height causes Firefox to release graphics
80-
// resources immediately, which can greatly reduce memory consumption.
81-
tempCanvas.width = 0;
82-
tempCanvas.height = 0;
84+
if (this.#tempCanvas) {
85+
zeroCanvas(this.#tempCanvas);
8386
}
8487
this.#tempCanvas = null;
8588
}
@@ -255,37 +258,15 @@ class PDFThumbnailView {
255258
this.div.setAttribute("data-loaded", true);
256259
this._placeholderImg.replaceWith(image);
257260

258-
// Zeroing the width and height causes Firefox to release graphics
259-
// resources immediately, which can greatly reduce memory consumption.
260-
reducedCanvas.width = 0;
261-
reducedCanvas.height = 0;
262-
}
263-
264-
async #finishRenderTask(renderTask, canvas, error = null) {
265-
// The renderTask may have been replaced by a new one, so only remove
266-
// the reference to the renderTask if it matches the one that is
267-
// triggering this callback.
268-
if (renderTask === this.renderTask) {
269-
this.renderTask = null;
270-
}
271-
272-
if (error instanceof RenderingCancelledException) {
273-
return;
274-
}
275-
this.renderingState = RenderingStates.FINISHED;
276-
this.#convertCanvasToImage(canvas);
277-
278-
if (error) {
279-
throw error;
280-
}
261+
zeroCanvas(reducedCanvas);
281262
}
282263

283264
async draw() {
284265
if (this.renderingState !== RenderingStates.INITIAL) {
285266
console.error("Must be in new state before drawing");
286-
return undefined;
267+
return;
287268
}
288-
const { pdfPage } = this;
269+
const { pageColors, pdfPage } = this;
289270

290271
if (!pdfPage) {
291272
this.renderingState = RenderingStates.FINISHED;
@@ -321,29 +302,42 @@ class PDFThumbnailView {
321302
transform,
322303
viewport: drawViewport,
323304
optionalContentConfigPromise: this._optionalContentConfigPromise,
324-
pageColors: this.pageColors,
305+
pageColors,
325306
};
326307
const renderTask = (this.renderTask = pdfPage.render(renderContext));
327308
renderTask.onContinue = renderContinueCallback;
328309

329-
const resultPromise = renderTask.promise.then(
330-
() => this.#finishRenderTask(renderTask, canvas),
331-
error => this.#finishRenderTask(renderTask, canvas, error)
332-
);
333-
resultPromise.finally(() => {
334-
// Zeroing the width and height causes Firefox to release graphics
335-
// resources immediately, which can greatly reduce memory consumption.
336-
canvas.width = 0;
337-
canvas.height = 0;
338-
339-
this.eventBus.dispatch("thumbnailrendered", {
340-
source: this,
341-
pageNumber: this.id,
342-
pdfPage: this.pdfPage,
343-
});
310+
let error = null;
311+
try {
312+
await renderTask.promise;
313+
} catch (e) {
314+
if (e instanceof RenderingCancelledException) {
315+
zeroCanvas(canvas);
316+
return;
317+
}
318+
error = e;
319+
} finally {
320+
// The renderTask may have been replaced by a new one, so only remove
321+
// the reference to the renderTask if it matches the one that is
322+
// triggering this callback.
323+
if (renderTask === this.renderTask) {
324+
this.renderTask = null;
325+
}
326+
}
327+
this.renderingState = RenderingStates.FINISHED;
328+
329+
this.#convertCanvasToImage(canvas);
330+
zeroCanvas(canvas);
331+
332+
this.eventBus.dispatch("thumbnailrendered", {
333+
source: this,
334+
pageNumber: this.id,
335+
pdfPage,
344336
});
345337

346-
return resultPromise;
338+
if (error) {
339+
throw error;
340+
}
347341
}
348342

349343
setImage(pageView) {
@@ -366,24 +360,21 @@ class PDFThumbnailView {
366360
}
367361

368362
#getReducedImageDims(canvas) {
369-
let reducedWidth = canvas.width << MAX_NUM_SCALING_STEPS,
370-
reducedHeight = canvas.height << MAX_NUM_SCALING_STEPS;
363+
const width = canvas.width << MAX_NUM_SCALING_STEPS,
364+
height = canvas.height << MAX_NUM_SCALING_STEPS;
371365

372366
const outputScale = new OutputScale();
373367
// Here we're not actually "rendering" to the canvas and the `OutputScale`
374368
// is thus only used to limit the canvas size, hence the identity scale.
375369
outputScale.sx = outputScale.sy = 1;
376370

377371
outputScale.limitCanvas(
378-
reducedWidth,
379-
reducedHeight,
372+
width,
373+
height,
380374
this.maxCanvasPixels,
381375
this.maxCanvasDim
382376
);
383-
reducedWidth = (reducedWidth * outputScale.sx) | 0;
384-
reducedHeight = (reducedHeight * outputScale.sy) | 0;
385-
386-
return [reducedWidth, reducedHeight];
377+
return [(width * outputScale.sx) | 0, (height * outputScale.sy) | 0];
387378
}
388379

389380
#reduceImage(img) {

0 commit comments

Comments
 (0)