Use streams for OperatorList chunking (issue 10023)

*Please note:* The majority of this patch was written by Yury, and it's simply been rebased and slightly extended to prevent issues when dealing with `RenderingCancelledException`.

By leveraging streams this (finally) provides a simple way in which parsing can be aborted on the worker-thread, which will ultimately help save resources.
With this patch worker-thread parsing will *only* be aborted when the document is destroyed, and not when rendering is cancelled. There's a couple of reasons for this:

 - The API currently expects the *entire* OperatorList to be extracted, or an Error to occur, once it's been started. Hence additional re-factoring/re-writing of the API code will be necessary to properly support cancelling and re-starting of OperatorList parsing in cases where the `lastChunk` hasn't yet been seen.
 - Even with the above addressed, immediately cancelling when encountering a `RenderingCancelledException` will lead to worse performance in e.g. the default viewer. When zooming and/or rotation of the document occurs it's very likely that `cancel` will be (almost) immediately followed by a new `render` call. In that case you'd obviously *not* want to abort parsing on the worker-thread, since then you'd risk throwing away a partially parsed Page and thus be forced to re-parse it again which will regress perceived performance.
 - This patch is already *somewhat* risky, given that it touches fundamentally important/critical code, and trying to keep it somewhat small should hopefully reduce the risk of regressions (and simplify reviewing as well).

Time permitting, once this has landed and been in Nightly for awhile, I'll try to work on the remaining points outlined above.

Co-Authored-By: Yury Delendik <ydelendik@mozilla.com>
Co-Authored-By: Jonas Jenwald <jonas.jenwald@gmail.com>
This commit is contained in:
Yury Delendik 2017-11-09 19:58:43 -06:00 committed by Jonas Jenwald
parent ee75fc1298
commit 66e0dd1b06
7 changed files with 141 additions and 94 deletions

View file

@ -1032,7 +1032,7 @@ class PDFPageProxy {
};
stats.time('Page Request');
this._transport.messageHandler.send('RenderPageRequest', {
this._pumpOperatorList({
pageIndex: this.pageNumber - 1,
intent: renderingIntent,
renderInteractiveForms: renderInteractiveForms === true,
@ -1054,6 +1054,11 @@ class PDFPageProxy {
if (error) {
internalRenderTask.capability.reject(error);
this._abortOperatorList({
intentState,
reason: error,
});
} else {
internalRenderTask.capability.resolve();
}
@ -1135,7 +1140,7 @@ class PDFPageProxy {
};
this._stats.time('Page Request');
this._transport.messageHandler.send('RenderPageRequest', {
this._pumpOperatorList({
pageIndex: this.pageIndex,
intent: renderingIntent,
});
@ -1201,19 +1206,25 @@ class PDFPageProxy {
this._transport.pageCache[this.pageIndex] = null;
const waitOn = [];
Object.keys(this.intentStates).forEach(function(intent) {
Object.keys(this.intentStates).forEach((intent) => {
const intentState = this.intentStates[intent];
this._abortOperatorList({
intentState,
reason: new Error('Page was destroyed.'),
force: true,
});
if (intent === 'oplist') {
// Avoid errors below, since the renderTasks are just stubs.
return;
}
const intentState = this.intentStates[intent];
intentState.renderTasks.forEach(function(renderTask) {
const renderCompleted = renderTask.capability.promise.
catch(function() {}); // ignoring failures
waitOn.push(renderCompleted);
renderTask.cancel();
});
}, this);
});
this.objs.clear();
this.annotationsPromise = null;
this.pendingCleanup = false;
@ -1273,8 +1284,7 @@ class PDFPageProxy {
* For internal use only.
* @ignore
*/
_renderPageChunk(operatorListChunk, intent) {
const intentState = this.intentStates[intent];
_renderPageChunk(operatorListChunk, intentState) {
// Add the new chunk to the current operator list.
for (let i = 0, ii = operatorListChunk.length; i < ii; i++) {
intentState.operatorList.fnArray.push(operatorListChunk.fnArray[i]);
@ -1293,6 +1303,86 @@ class PDFPageProxy {
}
}
/**
* For internal use only.
* @ignore
*/
_pumpOperatorList(args) {
assert(args.intent,
'PDFPageProxy._pumpOperatorList: Expected "intent" argument.');
const readableStream =
this._transport.messageHandler.sendWithStream('GetOperatorList', args);
const reader = readableStream.getReader();
const intentState = this.intentStates[args.intent];
intentState.streamReader = reader;
const pump = () => {
reader.read().then(({ value, done, }) => {
if (done) {
intentState.streamReader = null;
return;
}
if (this._transport.destroyed) {
return; // Ignore any pending requests if the worker was terminated.
}
this._renderPageChunk(value.operatorList, intentState);
pump();
}, (reason) => {
intentState.streamReader = null;
if (this._transport.destroyed) {
return; // Ignore any pending requests if the worker was terminated.
}
if (intentState.operatorList) {
// Mark operator list as complete.
intentState.operatorList.lastChunk = true;
for (let i = 0; i < intentState.renderTasks.length; i++) {
intentState.renderTasks[i].operatorListChanged();
}
this._tryCleanup();
}
if (intentState.displayReadyCapability) {
intentState.displayReadyCapability.reject(reason);
} else if (intentState.opListReadCapability) {
intentState.opListReadCapability.reject(reason);
} else {
throw reason;
}
});
};
pump();
}
/**
* For internal use only.
* @ignore
*/
_abortOperatorList({ intentState, reason, force = false, }) {
assert(reason instanceof Error,
'PDFPageProxy._abortOperatorList: Expected "reason" argument.');
if (!intentState.streamReader) {
return;
}
if (!force && intentState.renderTasks.length !== 0) {
// Ensure that an Error occuring in *only* one `InternalRenderTask`, e.g.
// multiple render() calls on the same canvas, won't break all rendering.
return;
}
if (reason instanceof RenderingCancelledException) {
// Aborting parsing on the worker-thread when rendering is cancelled will
// break subsequent rendering operations. TODO: Remove this restriction.
return;
}
intentState.streamReader.cancel(
new AbortException(reason && reason.message));
intentState.streamReader = null;
}
/**
* @return {Object} Returns page stats, if enabled.
*/
@ -1955,15 +2045,6 @@ class WorkerTransport {
page._startRenderPage(data.transparency, data.intent);
}, this);
messageHandler.on('RenderPageChunk', function(data) {
if (this.destroyed) {
return; // Ignore any pending requests if the worker was terminated.
}
const page = this.pageCache[data.pageIndex];
page._renderPageChunk(data.operatorList, data.intent);
}, this);
messageHandler.on('commonobj', function(data) {
if (this.destroyed) {
return; // Ignore any pending requests if the worker was terminated.
@ -2083,33 +2164,6 @@ class WorkerTransport {
}
}, this);
messageHandler.on('PageError', function(data) {
if (this.destroyed) {
return; // Ignore any pending requests if the worker was terminated.
}
const page = this.pageCache[data.pageIndex];
const intentState = page.intentStates[data.intent];
if (intentState.operatorList) {
// Mark operator list as complete.
intentState.operatorList.lastChunk = true;
for (let i = 0; i < intentState.renderTasks.length; i++) {
intentState.renderTasks[i].operatorListChanged();
}
page._tryCleanup();
}
if (intentState.displayReadyCapability) {
intentState.displayReadyCapability.reject(new Error(data.error));
} else if (intentState.opListReadCapability) {
intentState.opListReadCapability.reject(new Error(data.error));
} else {
throw new Error(data.error);
}
}, this);
messageHandler.on('UnsupportedFeature', this._onUnsupportedFeature, this);
messageHandler.on('JpegDecode', function(data) {