Skip to content

Commit

Permalink
Move the ImageBitmap clean-up into the PDFObjects class
Browse files Browse the repository at this point in the history
With upcoming changes we'll potentially start to cache `ImageBitmap` data at the document-level, in addition to just at the page-level.
Hence we need to ensure that such data is actually released on clean-up, and rather than duplicating the existing *manual* handling this code is instead moved into the `PDFObjects.clear` method. (In my opinion, this is an overall improvement even without globally cached `ImageBitmap` data.)

*Please note:* This patch is written using the GitHub UI, since I'm currently without a dev machine, so hopefully it's correct and makes sense.
  • Loading branch information
Snuffleupagus committed Feb 21, 2023
1 parent 534b22a commit 1b076b7
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions src/display/api.js
Expand Up @@ -1282,8 +1282,6 @@ class PDFPageProxy {
this.commonObjs = transport.commonObjs;
this.objs = new PDFObjects();

this._bitmaps = new Set();

this.cleanupAfterRender = false;
this.pendingCleanup = false;
this._intentStates = new Map();
Expand Down Expand Up @@ -1679,10 +1677,6 @@ class PDFPageProxy {
}
}
this.objs.clear();
for (const bitmap of this._bitmaps) {
bitmap.close();
}
this._bitmaps.clear();
this.pendingCleanup = false;
return Promise.all(waitOn);
}
Expand Down Expand Up @@ -1718,10 +1712,6 @@ class PDFPageProxy {
if (resetStats && this._stats) {
this._stats = new StatTimer();
}
for (const bitmap of this._bitmaps) {
bitmap.close();
}
this._bitmaps.clear();
this.pendingCleanup = false;
return true;
}
Expand Down Expand Up @@ -2774,9 +2764,8 @@ class WorkerTransport {
if (imageData) {
let length;
if (imageData.bitmap) {
const { bitmap, width, height } = imageData;
const { width, height } = imageData;
length = width * height * 4;
pageProxy._bitmaps.add(bitmap);
} else {
length = imageData.data?.length || 0;
}
Expand Down Expand Up @@ -3150,6 +3139,10 @@ class PDFObjects {
}

clear() {
for (const objId in this.#objs) {
const { data } = this.#objs[objId];
data?.bitmap?.close(); // Release any `ImageBitmap` data.
}
this.#objs = Object.create(null);
}
}
Expand Down

0 comments on commit 1b076b7

Please sign in to comment.