Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImageBitmap and OffscreenCanvas for DEM tiles #8845

Merged
merged 18 commits into from
Dec 10, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/source/raster_dem_tile_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ class RasterDEMTileSource extends RasterTileSource implements Source {
if (this.map._refreshExpiredTiles) tile.setExpiryData(img);
delete (img: any).cacheControl;
delete (img: any).expires;
const rawImageData = browser.getImageData(img, 1);
const transfer = window.ImageBitmap && img instanceof window.ImageBitmap && browser.supportsOffscreenCanvas();
const rawImageData = transfer ? img : browser.getImageData(img, 1);
const params = {
uid: tile.uid,
coord: tile.tileID,
Expand Down
13 changes: 12 additions & 1 deletion src/source/raster_dem_tile_worker_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,25 @@ import type {
class RasterDEMTileWorkerSource {
actor: Actor;
loaded: {[string]: DEMData};
offcreenCanvas: ?OffscreenCanvas;

constructor() {
this.loaded = {};
this.offcreenCanvas = new OffscreenCanvas(512, 512);
this.offcreenCanvasContext = this.offcreenCanvas.getContext('2d');
}

loadTile(params: WorkerDEMTileParameters, callback: WorkerDEMTileCallback) {
const {uid, encoding, rawImageData} = params;
const dem = new DEMData(uid, rawImageData, encoding);
let imagePixels = rawImageData;
if (rawImageData instanceof ImageBitmap) {
this.offcreenCanvas.width = rawImageData.width;
this.offcreenCanvas.height = rawImageData.height;
this.offcreenCanvasContext.drawImage(rawImageData, 0, 0, rawImageData.width, rawImageData.height);
imagePixels = this.offcreenCanvasContext.getImageData(-1, -1, rawImageData.width + 2, rawImageData.height + 2);
}

const dem = new DEMData(uid, imagePixels, encoding);

this.loaded = this.loaded || {};
this.loaded[uid] = dem;
Expand Down
43 changes: 32 additions & 11 deletions src/util/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const ResourceType = {
};
export {ResourceType};

const supportsImageBitmap = typeof window.createImageBitmap == 'function';

if (typeof Object.freeze == 'function') {
Object.freeze(ResourceType);
}
Expand Down Expand Up @@ -257,14 +259,38 @@ function sameOrigin(url) {

const transparentPngUrl = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVQYV2NgAAIAAAUAAarVyFEAAAAASUVORK5CYII=';

function arrayBufferToImage(data: ArrayBuffer, callback: (err: ?Error, image: ?HTMLImageElement) => void, cacheControl: ?string, expires: ?string) {
const img: HTMLImageElement = new window.Image();
const URL = window.URL || window.webkitURL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the || window.webkitURL here?

img.onload = () => {
callback(null, img);
URL.revokeObjectURL(img.src);
};
img.onerror = () => callback(new Error('Could not load image. Please make sure to use a supported image type such as PNG or JPEG. Note that SVGs are not supported.'));
const blob: Blob = new window.Blob([new Uint8Array(data)], {type: 'image/png'});
(img: any).cacheControl = cacheControl;
(img: any).expires = expires;
img.src = data.byteLength ? URL.createObjectURL(blob) : transparentPngUrl;
}

function arrayBufferToImageBitmap(data: ArrayBuffer, callback: (err: ?Error, image: ?ImageBitmap) => void, cacheControl: ?string, expires: ?string) {
const blob: Blob = new window.Blob([new Uint8Array(data)], {type: 'image/png'});
window.createImageBitmap(blob).then((imgBitmap) => {
callback(null, imgBitmap);
}).catch((e) => {
console.log(e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to leave this in?

callback(new Error('Could not load image. Please make sure to use a supported image type such as PNG or JPEG. Note that SVGs are not supported.'));
});
}

let imageQueue, numImageRequests;
export const resetImageRequestQueue = () => {
imageQueue = [];
numImageRequests = 0;
};
resetImageRequestQueue();

export const getImage = function(requestParameters: RequestParameters, callback: Callback<HTMLImageElement>): Cancelable {
export const getImage = function(requestParameters: RequestParameters, callback: Callback<HTMLImageElement | ImageBitmap>): Cancelable {
if (webpSupported.supported) {
if (!requestParameters.headers) {
requestParameters.headers = {};
Expand Down Expand Up @@ -309,16 +335,11 @@ export const getImage = function(requestParameters: RequestParameters, callback:
if (err) {
callback(err);
} else if (data) {
const img: HTMLImageElement = new window.Image();
img.onload = () => {
callback(null, img);
window.URL.revokeObjectURL(img.src);
};
img.onerror = () => callback(new Error('Could not load image. Please make sure to use a supported image type such as PNG or JPEG. Note that SVGs are not supported.'));
const blob: Blob = new window.Blob([new Uint8Array(data)], {type: 'image/png'});
(img: any).cacheControl = cacheControl;
(img: any).expires = expires;
img.src = data.byteLength ? window.URL.createObjectURL(blob) : transparentPngUrl;
if (supportsImageBitmap) {
arrayBufferToImageBitmap(data, callback, cacheControl, expires);
} else {
arrayBufferToImage(data, callback, cacheControl, expires);
}
}
});

Expand Down
4 changes: 4 additions & 0 deletions src/util/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ const exported = {

hardwareConcurrency: window.navigator.hardwareConcurrency || 4,

supportsOffscreenCanvas(): boolean {
return !!window.OffscreenCanvas;
},

get devicePixelRatio() { return window.devicePixelRatio; },
get prefersReducedMotion(): boolean {
if (!window.matchMedia) return false;
Expand Down
7 changes: 6 additions & 1 deletion src/util/web_worker_transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export function serialize(input: mixed, transferables: ?Array<Transferable>): Se
return input;
}

if (isArrayBuffer(input)) {
if (isArrayBuffer(input) || window.ImageBitmap && input instanceof window.ImageBitmap) {
if (transferables) {
transferables.push(((input: any): ArrayBuffer));
}
Expand Down Expand Up @@ -223,7 +223,12 @@ export function deserialize(input: Serialized): mixed {
input instanceof String ||
input instanceof Date ||
input instanceof RegExp ||
<<<<<<< HEAD
input instanceof ArrayBuffer ||
input instanceof ImageBitmap ||
=======
isArrayBuffer(input) ||
>>>>>>> 8e8a28025dca26e258ad6a26c064d07596f11527
arindam1993 marked this conversation as resolved.
Show resolved Hide resolved
ArrayBuffer.isView(input) ||
input instanceof ImageData) {
return input;
Expand Down