Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an option to enable/disable hardware acceleration (bug 1902012)
Browse files Browse the repository at this point in the history
calixteman committed Jun 12, 2024

Verified

This commit was signed with the committer’s verified signature.
narbs91 Narb
1 parent 341ff40 commit ba1d68d
Showing 12 changed files with 54 additions and 9 deletions.
5 changes: 5 additions & 0 deletions extensions/chromium/preferences_schema.json
Original file line number Diff line number Diff line change
@@ -45,6 +45,11 @@
"type": "boolean",
"default": false
},
"enableHardwareAcceleration": {
"description": "Whether to enable hardware acceleration.",
"type": "boolean",
"default": false
},
"enableML": {
"type": "boolean",
"default": false
2 changes: 1 addition & 1 deletion src/core/default_appearance.js
Original file line number Diff line number Diff line change
@@ -242,7 +242,7 @@ class FakeUnicodeFont {
this.fontFamily = fontFamily;

const canvas = new OffscreenCanvas(1, 1);
this.ctxMeasure = canvas.getContext("2d");
this.ctxMeasure = canvas.getContext("2d", { willReadFrequently: true });

if (!FakeUnicodeFont._fontNameId) {
FakeUnicodeFont._fontNameId = 1;
6 changes: 5 additions & 1 deletion src/display/api.js
Original file line number Diff line number Diff line change
@@ -213,6 +213,8 @@ const DefaultStandardFontDataFactory =
* when creating canvases. The default value is {new DOMCanvasFactory()}.
* @property {Object} [filterFactory] - A factory instance that will be used
* to create SVG filters when rendering some images on the main canvas.
* @property {boolean} [enableHardwareAcceleration] - Enables hardware
* acceleration for rendering. The default value is `false`.
*/

/**
@@ -314,8 +316,10 @@ function getDocument(src) {
standardFontDataUrl &&
isValidFetchUrl(cMapUrl, document.baseURI) &&
isValidFetchUrl(standardFontDataUrl, document.baseURI));
const enableHardwareAcceleration = src.enableHardwareAcceleration === true;
const canvasFactory =
src.canvasFactory || new DefaultCanvasFactory({ ownerDocument });
src.canvasFactory ||
new DefaultCanvasFactory({ ownerDocument, enableHardwareAcceleration });
const filterFactory =
src.filterFactory || new DefaultFilterFactory({ docId, ownerDocument });

9 changes: 7 additions & 2 deletions src/display/base_factory.js
Original file line number Diff line number Diff line change
@@ -46,10 +46,13 @@ class BaseFilterFactory {
}

class BaseCanvasFactory {
constructor() {
#enableHardwareAcceleration = false;

constructor(enableHardwareAcceleration = false) {
if (this.constructor === BaseCanvasFactory) {
unreachable("Cannot initialize BaseCanvasFactory.");
}
this.#enableHardwareAcceleration = enableHardwareAcceleration;
}

create(width, height) {
@@ -59,7 +62,9 @@ class BaseCanvasFactory {
const canvas = this._createCanvas(width, height);
return {
canvas,
context: canvas.getContext("2d"),
context: canvas.getContext("2d", {
willReadFrequently: !this.#enableHardwareAcceleration,
}),
};
}

7 changes: 5 additions & 2 deletions src/display/display_utils.js
Original file line number Diff line number Diff line change
@@ -477,8 +477,11 @@ class DOMFilterFactory extends BaseFilterFactory {
}

class DOMCanvasFactory extends BaseCanvasFactory {
constructor({ ownerDocument = globalThis.document } = {}) {
super();
constructor({
ownerDocument = globalThis.document,
enableHardwareAcceleration = false,
} = {}) {
super(enableHardwareAcceleration);
this._document = ownerDocument;
}

2 changes: 1 addition & 1 deletion src/display/editor/tools.js
Original file line number Diff line number Diff line change
@@ -98,7 +98,7 @@ class ImageManager {
// behavior in Safari.
const svg = `data:image/svg+xml;charset=UTF-8,<svg viewBox="0 0 1 1" width="1" height="1" xmlns="http://www.w3.org/2000/svg"><rect width="1" height="1" style="fill:red;"/></svg>`;
const canvas = new OffscreenCanvas(1, 3);
const ctx = canvas.getContext("2d");
const ctx = canvas.getContext("2d", { willReadFrequently: true });
const image = new Image();
image.src = svg;
const promise = image.decode().then(() => {
4 changes: 4 additions & 0 deletions src/display/node_utils.js
Original file line number Diff line number Diff line change
@@ -117,6 +117,10 @@ const fetchData = function (url) {
class NodeFilterFactory extends BaseFilterFactory {}

class NodeCanvasFactory extends BaseCanvasFactory {
constructor({ enableHardwareAcceleration = false } = {}) {
super(enableHardwareAcceleration);
}

/**
* @ignore
*/
5 changes: 4 additions & 1 deletion src/display/text_layer.js
Original file line number Diff line number Diff line change
@@ -447,7 +447,10 @@ class TextLayer {
canvas.className = "hiddenCanvasElement";
canvas.lang = lang;
document.body.append(canvas);
canvasContext = canvas.getContext("2d", { alpha: false });
canvasContext = canvas.getContext("2d", {
alpha: false,
willReadFrequently: true,
});
this.#canvasContexts.set(lang, canvasContext);
}
return canvasContext;
1 change: 1 addition & 0 deletions web/app.js
Original file line number Diff line number Diff line change
@@ -465,6 +465,7 @@ const PDFViewerApplication = {
pageColors,
mlManager: this.mlManager,
abortSignal: this._globalAbortController.signal,
enableHardwareAcceleration: AppOptions.get("enableHardwareAcceleration"),
});
this.pdfViewer = pdfViewer;

5 changes: 5 additions & 0 deletions web/app_options.js
Original file line number Diff line number Diff line change
@@ -310,6 +310,11 @@ const defaultOptions = {
value: "",
kind: OptionKind.API,
},
enableHardwareAcceleration: {
/** @type {boolean} */
value: false,
kind: OptionKind.API + OptionKind.PREFERENCE + OptionKind.VIEWER,
},
enableXfa: {
/** @type {boolean} */
value: true,
10 changes: 9 additions & 1 deletion web/pdf_page_view.js
Original file line number Diff line number Diff line change
@@ -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} [enableHardwareAcceleration] - 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;

#enableHardwareAcceleration = false;

#hasRestrictedScaling = false;

#layerProperties = null;
@@ -163,6 +167,7 @@ class PDFPageView {
this.maxCanvasPixels =
options.maxCanvasPixels ?? AppOptions.get("maxCanvasPixels");
this.pageColors = options.pageColors || null;
this.#enableHardwareAcceleration = !!options.enableHardwareAcceleration;

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.#enableHardwareAcceleration,
});
const outputScale = (this.outputScale = new OutputScale());

if (
7 changes: 7 additions & 0 deletions web/pdf_viewer.js
Original file line number Diff line number Diff line change
@@ -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} [enableHardwareAcceleration] - Enables hardware
* acceleration for rendering. The default value is `false`.
*/

class PDFPageViewBuffer {
@@ -211,6 +213,8 @@ class PDFViewer {

#containerTopLeft = null;

#enableHardwareAcceleration = false;

#enableHighlightFloatingButton = false;

#enablePermissions = false;
@@ -296,6 +300,8 @@ class PDFViewer {
this.#enablePermissions = options.enablePermissions || false;
this.pageColors = options.pageColors || null;
this.#mlManager = options.mlManager || null;
this.#enableHardwareAcceleration =
options.enableHardwareAcceleration || false;

this.defaultRenderingQueue = !options.renderingQueue;
if (
@@ -943,6 +949,7 @@ class PDFViewer {
pageColors,
l10n: this.l10n,
layerProperties: this._layerProperties,
enableHardwareAcceleration: this.#enableHardwareAcceleration,
});
this._pages.push(pageView);
}

0 comments on commit ba1d68d

Please sign in to comment.