Skip to content

Commit 2027870

Browse files
committed
Move getContext call to InternalRenderTask
This is a precursor to moving the call into a worker thread to let us use `OffscreenCanvas`. The current position wouldn't work since we make transformations to the canvas object after the getContext call, which isn't allowed for OffscreenCanvas. Also it isn't allowed to clone or `transferControlToOffscreen` the canvas after the `getContext` call.
1 parent 802808c commit 2027870

File tree

6 files changed

+47
-35
lines changed

6 files changed

+47
-35
lines changed

src/display/api.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,8 @@ function getDocument(src = {}) {
501501
task,
502502
networkStream,
503503
transportParams,
504-
transportFactory
504+
transportFactory,
505+
enableHWA
505506
);
506507
task._transport = transport;
507508
messageHandler.send("Ready", null);
@@ -1404,7 +1405,7 @@ class PDFPageProxy {
14041405
* resolved when the page finishes rendering.
14051406
*/
14061407
render({
1407-
canvasContext,
1408+
canvas,
14081409
viewport,
14091410
intent = "display",
14101411
annotationMode = AnnotationMode.ENABLE,
@@ -1496,7 +1497,7 @@ class PDFPageProxy {
14961497
callback: complete,
14971498
// Only include the required properties, and *not* the entire object.
14981499
params: {
1499-
canvasContext,
1500+
canvas,
15001501
viewport,
15011502
transform,
15021503
background,
@@ -1511,6 +1512,7 @@ class PDFPageProxy {
15111512
useRequestAnimationFrame: !intentPrint,
15121513
pdfBug: this._pdfBug,
15131514
pageColors,
1515+
enableHWA: this._transport.enableHWA,
15141516
});
15151517

15161518
(intentState.renderTasks ||= new Set()).add(internalRenderTask);
@@ -2305,7 +2307,14 @@ class WorkerTransport {
23052307

23062308
#passwordCapability = null;
23072309

2308-
constructor(messageHandler, loadingTask, networkStream, params, factory) {
2310+
constructor(
2311+
messageHandler,
2312+
loadingTask,
2313+
networkStream,
2314+
params,
2315+
factory,
2316+
enableHWA
2317+
) {
23092318
this.messageHandler = messageHandler;
23102319
this.loadingTask = loadingTask;
23112320
this.commonObjs = new PDFObjects();
@@ -2329,6 +2338,7 @@ class WorkerTransport {
23292338
this._fullReader = null;
23302339
this._lastProgress = null;
23312340
this.downloadInfoCapability = Promise.withResolvers();
2341+
this.enableHWA = enableHWA;
23322342

23332343
this.setupMessageHandler();
23342344

@@ -3104,6 +3114,7 @@ class InternalRenderTask {
31043114
useRequestAnimationFrame = false,
31053115
pdfBug = false,
31063116
pageColors = null,
3117+
enableHWA,
31073118
}) {
31083119
this.callback = callback;
31093120
this.params = params;
@@ -3131,7 +3142,8 @@ class InternalRenderTask {
31313142
this._continueBound = this._continue.bind(this);
31323143
this._scheduleNextBound = this._scheduleNext.bind(this);
31333144
this._nextBound = this._next.bind(this);
3134-
this._canvas = params.canvasContext.canvas;
3145+
this._canvas = params.canvas;
3146+
this.enableHWA = enableHWA;
31353147
}
31363148

31373149
get completed() {
@@ -3161,7 +3173,12 @@ class InternalRenderTask {
31613173
this.stepper.init(this.operatorList);
31623174
this.stepper.nextBreakPoint = this.stepper.getNextBreakPoint();
31633175
}
3164-
const { canvasContext, viewport, transform, background } = this.params;
3176+
const { viewport, transform, background } = this.params;
3177+
3178+
const canvasContext = this._canvas.getContext("2d", {
3179+
alpha: false,
3180+
willReadFrequently: !this.enableHWA,
3181+
});
31653182

31663183
this.gfx = new CanvasGraphics(
31673184
canvasContext,

test/unit/api_spec.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4410,7 +4410,7 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
44104410
viewport.height
44114411
);
44124412
const renderTask = pdfPage.render({
4413-
canvasContext: canvasAndCtx.context,
4413+
canvas: canvasAndCtx.canvas,
44144414
viewport,
44154415
});
44164416
expect(renderTask instanceof RenderTask).toEqual(true);
@@ -4446,7 +4446,7 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
44464446
viewport.height
44474447
);
44484448
const renderTask = page.render({
4449-
canvasContext: canvasAndCtx.context,
4449+
canvas: canvasAndCtx.canvas,
44504450
viewport,
44514451
});
44524452
expect(renderTask instanceof RenderTask).toEqual(true);
@@ -4477,7 +4477,7 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
44774477
viewport.height
44784478
);
44794479
const renderTask = page.render({
4480-
canvasContext: canvasAndCtx.context,
4480+
canvas: canvasAndCtx.canvas,
44814481
viewport,
44824482
});
44834483
expect(renderTask instanceof RenderTask).toEqual(true);
@@ -4494,7 +4494,7 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
44944494
}
44954495

44964496
const reRenderTask = page.render({
4497-
canvasContext: canvasAndCtx.context,
4497+
canvas: canvasAndCtx.canvas,
44984498
viewport,
44994499
});
45004500
expect(reRenderTask instanceof RenderTask).toEqual(true);
@@ -4518,14 +4518,14 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
45184518
viewport.height
45194519
);
45204520
const renderTask1 = page.render({
4521-
canvasContext: canvasAndCtx.context,
4521+
canvas: canvasAndCtx.canvas,
45224522
viewport,
45234523
optionalContentConfigPromise,
45244524
});
45254525
expect(renderTask1 instanceof RenderTask).toEqual(true);
45264526

45274527
const renderTask2 = page.render({
4528-
canvasContext: canvasAndCtx.context,
4528+
canvas: canvasAndCtx.canvas,
45294529
viewport,
45304530
optionalContentConfigPromise,
45314531
});
@@ -4562,7 +4562,7 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
45624562
viewport.height
45634563
);
45644564
const renderTask = pdfPage.render({
4565-
canvasContext: canvasAndCtx.context,
4565+
canvas: canvasAndCtx.canvas,
45664566
viewport,
45674567
});
45684568
expect(renderTask instanceof RenderTask).toEqual(true);
@@ -4591,7 +4591,7 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
45914591
viewport.height
45924592
);
45934593
const renderTask = pdfPage.render({
4594-
canvasContext: canvasAndCtx.context,
4594+
canvas: canvasAndCtx.canvas,
45954595
viewport,
45964596
background: "#FF0000", // See comment below.
45974597
});
@@ -4651,7 +4651,7 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
46514651
viewport.height
46524652
);
46534653
const renderTask = pdfPage.render({
4654-
canvasContext: canvasAndCtx.context,
4654+
canvas: canvasAndCtx.canvas,
46554655
viewport,
46564656
});
46574657

@@ -4755,7 +4755,7 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
47554755
viewport.height
47564756
);
47574757
const renderTask = pdfPage.render({
4758-
canvasContext: canvasAndCtx.context,
4758+
canvas: canvasAndCtx.canvas,
47594759
viewport,
47604760
});
47614761

@@ -4802,7 +4802,7 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
48024802
viewport.height
48034803
);
48044804
const renderTask = pdfPage.render({
4805-
canvasContext: canvasAndCtx.context,
4805+
canvas: canvasAndCtx.canvas,
48064806
viewport,
48074807
});
48084808

@@ -4852,7 +4852,7 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
48524852
viewport.height
48534853
);
48544854
const renderTask = pdfPage.render({
4855-
canvasContext: canvasAndCtx.context,
4855+
canvas: canvasAndCtx.canvas,
48564856
viewport,
48574857
intent: "print",
48584858
annotationMode: AnnotationMode.ENABLE_STORAGE,
@@ -4939,7 +4939,7 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
49394939
viewport.height
49404940
);
49414941
const renderTask = page.render({
4942-
canvasContext: canvasAndCtx.context,
4942+
canvas: canvasAndCtx.canvas,
49434943
viewport,
49444944
});
49454945
await renderTask.promise;

test/unit/custom_spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe("custom canvas rendering", function () {
5050
const canvasAndCtx = canvasFactory.create(viewport.width, viewport.height);
5151

5252
const renderTask = page.render({
53-
canvasContext: canvasAndCtx.context,
53+
canvas: canvasAndCtx.canvas,
5454
viewport,
5555
});
5656
await renderTask.promise;
@@ -70,7 +70,7 @@ describe("custom canvas rendering", function () {
7070
const canvasAndCtx = canvasFactory.create(viewport.width, viewport.height);
7171

7272
const renderTask = page.render({
73-
canvasContext: canvasAndCtx.context,
73+
canvas: canvasAndCtx.canvas,
7474
viewport,
7575
background: "rgba(255,0,0,1.0)",
7676
});
@@ -160,7 +160,7 @@ describe("custom ownerDocument", function () {
160160
const canvasAndCtx = canvasFactory.create(viewport.width, viewport.height);
161161

162162
await page.render({
163-
canvasContext: canvasAndCtx.context,
163+
canvas: canvasAndCtx.canvas,
164164
viewport,
165165
}).promise;
166166

@@ -194,7 +194,7 @@ describe("custom ownerDocument", function () {
194194
const canvasAndCtx = canvasFactory.create(viewport.width, viewport.height);
195195

196196
await page.render({
197-
canvasContext: canvasAndCtx.context,
197+
canvas: canvasAndCtx.canvas,
198198
viewport,
199199
}).promise;
200200

web/base_pdf_page_view.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,7 @@ class BasePDFPageView {
166166
}
167167
};
168168

169-
const ctx = canvas.getContext("2d", {
170-
alpha: false,
171-
willReadFrequently: !this.#enableHWA,
172-
});
173-
174-
return { canvas, prevCanvas, ctx };
169+
return { canvas, prevCanvas };
175170
}
176171

177172
#renderContinueCallback = cont => {

web/pdf_page_detail_view.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ class PDFPageDetailView extends BasePDFPageView {
214214

215215
const canvasWrapper = this.pageView._ensureCanvasWrapper();
216216

217-
const { canvas, prevCanvas, ctx } = this._createCanvas(newCanvas => {
217+
const { canvas, prevCanvas } = this._createCanvas(newCanvas => {
218218
// If there is already the background canvas, inject this new canvas
219219
// after it. We cannot simply use .append because all canvases must
220220
// be before the SVG elements used for drawings.
@@ -249,7 +249,7 @@ class PDFPageDetailView extends BasePDFPageView {
249249
style.left = `${(area.minX * 100) / width}%`;
250250

251251
const renderingPromise = this._drawCanvas(
252-
this.pageView._getRenderingContext(ctx, transform),
252+
this.pageView._getRenderingContext(canvas, transform),
253253
() => {
254254
// If the rendering is cancelled, keep the old canvas visible.
255255
this.canvas?.remove();

web/pdf_page_view.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -912,9 +912,9 @@ class PDFPageView extends BasePDFPageView {
912912
return canvasWrapper;
913913
}
914914

915-
_getRenderingContext(canvasContext, transform) {
915+
_getRenderingContext(canvas, transform) {
916916
return {
917-
canvasContext,
917+
canvas,
918918
transform,
919919
viewport: this.viewport,
920920
annotationMode: this.#annotationMode,
@@ -1000,7 +1000,7 @@ class PDFPageView extends BasePDFPageView {
10001000
const { width, height } = viewport;
10011001
this.#originalViewport = viewport;
10021002

1003-
const { canvas, prevCanvas, ctx } = this._createCanvas(newCanvas => {
1003+
const { canvas, prevCanvas } = this._createCanvas(newCanvas => {
10041004
// Always inject the canvas as the first element in the wrapper.
10051005
canvasWrapper.prepend(newCanvas);
10061006
});
@@ -1042,7 +1042,7 @@ class PDFPageView extends BasePDFPageView {
10421042
? [outputScale.sx, 0, 0, outputScale.sy, 0, 0]
10431043
: null;
10441044
const resultPromise = this._drawCanvas(
1045-
this._getRenderingContext(ctx, transform),
1045+
this._getRenderingContext(canvas, transform),
10461046
() => {
10471047
prevCanvas?.remove();
10481048
this._resetCanvas();

0 commit comments

Comments
 (0)