Skip to content

Commit 3f807f5

Browse files
committed
fixup! Implement offscreen rendering with workers
1 parent a22cfd7 commit 3f807f5

File tree

5 files changed

+43
-30
lines changed

5 files changed

+43
-30
lines changed

src/display/api.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1952,9 +1952,9 @@ class PDFPageProxy {
19521952
return this._stats;
19531953
}
19541954

1955-
resetCanvas() {
1955+
resetCanvas(taskID) {
19561956
this._transport.rendererHandler.send("resetCanvas", {
1957-
pageIndex: this._pageIndex,
1957+
taskID,
19581958
});
19591959
}
19601960
}
@@ -3073,6 +3073,10 @@ class RenderTask {
30733073
(separateAnnots.canvas && annotationCanvasMap?.size > 0)
30743074
);
30753075
}
3076+
3077+
get taskID() {
3078+
return this.#internalRenderTask.taskID;
3079+
}
30763080
}
30773081

30783082
/**
@@ -3163,17 +3167,16 @@ class InternalRenderTask {
31633167
this.rendererHandler.send(
31643168
"init",
31653169
{
3166-
canvas: offscreen,
3167-
drawingParams: {
3168-
viewport,
3169-
transform,
3170-
transparency,
3171-
background,
3172-
},
31733170
pageIndex: this._pageIndex,
3171+
canvas: offscreen,
31743172
map: this.annotationCanvasMap,
31753173
colors: this.pageColors,
31763174
taskID: this.taskID,
3175+
transform,
3176+
viewport,
3177+
transparency,
3178+
background,
3179+
optionalContentConfig,
31773180
},
31783181
[offscreen]
31793182
);

src/display/canvas.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ class CanvasGraphics {
648648
objs,
649649
canvasFactory,
650650
filterFactory,
651-
{ isVisible, markedContentStack = null },
651+
{ optionalContentConfig, markedContentStack = null },
652652
annotationCanvasMap,
653653
pageColors
654654
) {
@@ -678,8 +678,7 @@ class CanvasGraphics {
678678
this.suspendedCtx = null;
679679
this.contentVisible = true;
680680
this.markedContentStack = markedContentStack || [];
681-
// this.optionalContentConfig = optionalContentConfig;
682-
this.isVisible = isVisible;
681+
this.optionalContentConfig = optionalContentConfig;
683682
this.cachedCanvases = new CachedCanvases(this.canvasFactory);
684683
this.cachedPatterns = new Map();
685684
this.annotationCanvasMap = annotationCanvasMap;
@@ -2166,8 +2165,7 @@ class CanvasGraphics {
21662165
this.canvasFactory,
21672166
this.filterFactory,
21682167
{
2169-
// optionalContentConfig: this.optionalContentConfig,
2170-
isVisible: this.isVisible,
2168+
optionalContentConfig: this.optionalContentConfig,
21712169
markedContentStack: this.markedContentStack,
21722170
}
21732171
),
@@ -2876,7 +2874,7 @@ class CanvasGraphics {
28762874
beginMarkedContentProps(tag, properties) {
28772875
if (tag === "OC") {
28782876
this.markedContentStack.push({
2879-
visible: this.isVisible(properties),
2877+
visible: this.optionalContentConfig.isVisible(properties),
28802878
});
28812879
} else {
28822880
this.markedContentStack.push({

src/display/renderer_worker.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ class RendererMessageHandler {
1313

1414
static #tasks = new Map();
1515

16-
static #canvases = new Map();
17-
1816
static #fontLoader = new FontLoader({
1917
ownerDocument: self,
2018
});
@@ -62,7 +60,7 @@ class RendererMessageHandler {
6260
if (terminated) {
6361
throw new Error("Renderer worker has been terminated.");
6462
}
65-
this.handleCommonObj(id, type, data, workerHandler, this.#commonObjs);
63+
this.handleCommonObj(id, type, data, workerHandler);
6664
});
6765

6866
workerHandler.on("obj", ([id, pageIndex, type, data]) => {
@@ -75,7 +73,18 @@ class RendererMessageHandler {
7573

7674
mainHandler.on(
7775
"init",
78-
({ pageIndex, canvas, drawingParams, map, colors, taskID }) => {
76+
({
77+
pageIndex,
78+
canvas,
79+
map,
80+
colors,
81+
taskID,
82+
transform,
83+
viewport,
84+
transparency,
85+
background,
86+
optionalContentConfig,
87+
}) => {
7988
assert(!this.#tasks.has(taskID), "Task already initialized");
8089
const ctx = canvas.getContext("2d", {
8190
alpha: false,
@@ -88,13 +97,12 @@ class RendererMessageHandler {
8897
objs,
8998
this.#canvasFactory,
9099
this.#filterFactory,
91-
{},
100+
{ optionalContentConfig },
92101
map,
93102
colors
94103
);
95-
gfx.beginDrawing(drawingParams);
104+
gfx.beginDrawing({ transform, viewport, transparency, background });
96105
this.#tasks.set(taskID, { canvas, gfx });
97-
this.#canvases.set(pageIndex, canvas);
98106
}
99107
);
100108

@@ -123,12 +131,13 @@ class RendererMessageHandler {
123131
task.gfx.endDrawing();
124132
});
125133

126-
mainHandler.on("resetCanvas", ({ pageIndex }) => {
134+
mainHandler.on("resetCanvas", ({ taskID }) => {
127135
if (terminated) {
128136
throw new Error("Renderer worker has been terminated.");
129137
}
130-
const canvas = this.#canvases.get(pageIndex);
131-
assert(canvas !== undefined, "Page not initialized");
138+
const task = this.#tasks.get(taskID);
139+
assert(task !== undefined, "Task not initialized");
140+
const canvas = task.canvas;
132141
canvas.width = canvas.height = 0;
133142
});
134143

@@ -141,7 +150,6 @@ class RendererMessageHandler {
141150
this.#objsMap.clear();
142151
this.#tasks.clear();
143152
this.#fontLoader.clear();
144-
this.#canvases.clear();
145153
mainHandler.destroy();
146154
mainHandler = null;
147155
});

web/base_pdf_page_view.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class BasePDFPageView {
4646

4747
renderTask = null;
4848

49+
renderTaskID = null;
50+
4951
resume = null;
5052

5153
constructor(options) {
@@ -157,7 +159,7 @@ class BasePDFPageView {
157159

158160
if (prevCanvas) {
159161
prevCanvas.replaceWith(canvas);
160-
this.pdfPage.resetCanvas();
162+
this.pdfPage.resetCanvas(this.renderTaskID);
161163
} else {
162164
onShow(canvas);
163165
}
@@ -185,7 +187,7 @@ class BasePDFPageView {
185187
return;
186188
}
187189
canvas.remove();
188-
this.pdfPage.resetCanvas();
190+
this.pdfPage.resetCanvas(this.renderTaskID);
189191
this.canvas = null;
190192
this.#resetTempCanvas();
191193
}
@@ -207,6 +209,8 @@ class BasePDFPageView {
207209
}
208210
};
209211

212+
this.renderTaskID = renderTask.taskID;
213+
210214
let error = null;
211215
try {
212216
await renderTask.promise;

web/pdf_thumbnail_view.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ class PDFThumbnailView {
303303
await renderTask.promise;
304304
} catch (e) {
305305
if (e instanceof RenderingCancelledException) {
306-
zeroCanvas(canvas);
306+
pdfPage.resetCanvas(renderTask.taskID);
307307
return;
308308
}
309309
error = e;
@@ -318,7 +318,7 @@ class PDFThumbnailView {
318318
this.renderingState = RenderingStates.FINISHED;
319319

320320
this.#convertCanvasToImage(canvas);
321-
zeroCanvas(canvas);
321+
pdfPage.resetCanvas(renderTask.taskID);
322322

323323
this.eventBus.dispatch("thumbnailrendered", {
324324
source: this,

0 commit comments

Comments
 (0)