1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-19 22:58:07 +02:00

Add an option to enable/disable hardware acceleration (bug 1902012)

This commit is contained in:
Calixte Denizet 2024-06-12 12:51:51 +02:00
parent 341ff40e74
commit ff6180a4c9
13 changed files with 62 additions and 12 deletions

View file

@ -439,6 +439,7 @@ const PDFViewerApplication = {
)
: null;
const enableHWA = AppOptions.get("enableHWA");
const pdfViewer = new PDFViewer({
container,
viewer,
@ -465,6 +466,7 @@ const PDFViewerApplication = {
pageColors,
mlManager: this.mlManager,
abortSignal: this._globalAbortController.signal,
enableHWA,
});
this.pdfViewer = pdfViewer;
@ -480,6 +482,7 @@ const PDFViewerApplication = {
linkService: pdfLinkService,
pageColors,
abortSignal: this._globalAbortController.signal,
enableHWA,
});
pdfRenderingQueue.setThumbnailViewer(this.pdfThumbnailViewer);
}

View file

@ -310,6 +310,11 @@ const defaultOptions = {
value: "",
kind: OptionKind.API,
},
enableHWA: {
/** @type {boolean} */
value: false,
kind: OptionKind.API + OptionKind.VIEWER + OptionKind.PREFERENCE,
},
enableXfa: {
/** @type {boolean} */
value: true,

View file

@ -81,6 +81,8 @@ import { XfaLayerBuilder } from "./xfa_layer_builder.js";
* @property {IL10n} [l10n] - Localization service.
* @property {Object} [layerProperties] - The object that is used to lookup
* the necessary layer-properties.
* @property {boolean} [enableHWA] - Enables hardware acceleration for
* rendering. The default value is `false`.
*/
const DEFAULT_LAYER_PROPERTIES =
@ -113,6 +115,8 @@ const LAYERS_ORDER = new Map([
class PDFPageView {
#annotationMode = AnnotationMode.ENABLE_FORMS;
#enableHWA = false;
#hasRestrictedScaling = false;
#layerProperties = null;
@ -163,6 +167,7 @@ class PDFPageView {
this.maxCanvasPixels =
options.maxCanvasPixels ?? AppOptions.get("maxCanvasPixels");
this.pageColors = options.pageColors || null;
this.#enableHWA = options.enableHWA || false;
this.eventBus = options.eventBus;
this.renderingQueue = options.renderingQueue;
@ -981,7 +986,10 @@ class PDFPageView {
canvasWrapper.append(canvas);
this.canvas = canvas;
const ctx = canvas.getContext("2d", { alpha: false });
const ctx = canvas.getContext("2d", {
alpha: false,
willReadFrequently: !this.#enableHWA,
});
const outputScale = (this.outputScale = new OutputScale());
if (

View file

@ -44,6 +44,8 @@ const THUMBNAIL_WIDTH = 98; // px
* @property {Object} [pageColors] - Overwrites background and foreground colors
* with user defined ones in order to improve readability in high contrast
* mode.
* @property {boolean} [enableHWA] - Enables hardware acceleration for
* rendering. The default value is `false`.
*/
class TempImageFactory {
@ -92,6 +94,7 @@ class PDFThumbnailView {
linkService,
renderingQueue,
pageColors,
enableHWA,
}) {
this.id = id;
this.renderingId = "thumbnail" + id;
@ -103,6 +106,7 @@ class PDFThumbnailView {
this.pdfPageRotate = defaultViewport.rotation;
this._optionalContentConfigPromise = optionalContentConfigPromise || null;
this.pageColors = pageColors || null;
this.enableHWA = enableHWA || false;
this.eventBus = eventBus;
this.linkService = linkService;
@ -196,11 +200,14 @@ class PDFThumbnailView {
this.resume = null;
}
#getPageDrawContext(upscaleFactor = 1) {
#getPageDrawContext(upscaleFactor = 1, enableHWA = this.enableHWA) {
// Keep the no-thumbnail outline visible, i.e. `data-loaded === false`,
// until rendering/image conversion is complete, to avoid display issues.
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d", { alpha: false });
const ctx = canvas.getContext("2d", {
alpha: false,
willReadFrequently: !enableHWA,
});
const outputScale = new OutputScale();
canvas.width = (upscaleFactor * this.canvasWidth * outputScale.sx) | 0;
@ -340,7 +347,7 @@ class PDFThumbnailView {
}
#reduceImage(img) {
const { ctx, canvas } = this.#getPageDrawContext();
const { ctx, canvas } = this.#getPageDrawContext(1, true);
if (img.width <= 2 * canvas.width) {
ctx.drawImage(

View file

@ -44,6 +44,8 @@ const THUMBNAIL_SELECTED_CLASS = "selected";
* mode.
* @property {AbortSignal} [abortSignal] - The AbortSignal for the window
* events.
* @property {boolean} [enableHWA] - Enables hardware acceleration for
* rendering. The default value is `false`.
*/
/**
@ -60,12 +62,14 @@ class PDFThumbnailViewer {
renderingQueue,
pageColors,
abortSignal,
enableHWA,
}) {
this.container = container;
this.eventBus = eventBus;
this.linkService = linkService;
this.renderingQueue = renderingQueue;
this.pageColors = pageColors || null;
this.enableHWA = enableHWA || false;
this.scroll = watchScroll(
this.container,
@ -206,6 +210,7 @@ class PDFThumbnailViewer {
linkService: this.linkService,
renderingQueue: this.renderingQueue,
pageColors: this.pageColors,
enableHWA: this.enableHWA,
});
this._thumbnails.push(thumbnail);
}

View file

@ -123,6 +123,8 @@ function isValidAnnotationEditorMode(mode) {
* @property {Object} [pageColors] - Overwrites background and foreground colors
* with user defined ones in order to improve readability in high contrast
* mode.
* @property {boolean} [enableHWA] - Enables hardware acceleration for
* rendering. The default value is `false`.
*/
class PDFPageViewBuffer {
@ -211,6 +213,8 @@ class PDFViewer {
#containerTopLeft = null;
#enableHWA = false;
#enableHighlightFloatingButton = false;
#enablePermissions = false;
@ -296,6 +300,7 @@ class PDFViewer {
this.#enablePermissions = options.enablePermissions || false;
this.pageColors = options.pageColors || null;
this.#mlManager = options.mlManager || null;
this.#enableHWA = options.enableHWA || false;
this.defaultRenderingQueue = !options.renderingQueue;
if (
@ -943,6 +948,7 @@ class PDFViewer {
pageColors,
l10n: this.l10n,
layerProperties: this._layerProperties,
enableHWA: this.#enableHWA,
});
this._pages.push(pageView);
}