Skip to content

Commit

Permalink
Add feature detection for createImageBitmap
Browse files Browse the repository at this point in the history
  • Loading branch information
Shehata committed Feb 18, 2019
1 parent 9219277 commit 96cbc18
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 2 deletions.
71 changes: 69 additions & 2 deletions Source/Core/FeatureDetection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ define([
'./defaultValue',
'./defined',
'./Fullscreen',
'./RuntimeError',
'../ThirdParty/when'
], function(
defaultValue,
defined,
Fullscreen,
RuntimeError,
when) {
'use strict';
/*global CanvasPixelArray*/
Expand Down Expand Up @@ -255,6 +253,71 @@ define([
}
}

var supportsCreateImageBitmapResult;
function supportsCreateImageBitmap() {
if (!defined(supportsCreateImageBitmapResult)) {
supportsCreateImageBitmapResult = defined(window.createImageBitmap);
}

return supportsCreateImageBitmapResult;
}

var supportsImageBitmapOptionsResult;
var supportsImageBitmapOptionsPromise;
function supportsImageBitmapOptions() {
// Until the HTML folks figure out what to do about this, we need to actually try loading an image to
// know if this browser supports passing options to the createImageBitmap function.
// https://github.com/whatwg/html/pull/4248
if (defined(supportsImageBitmapOptionsPromise)) {
return supportsImageBitmapOptionsPromise.promise;
}

supportsImageBitmapOptionsPromise = when.defer();

if (!supportsCreateImageBitmap()) {
supportsImageBitmapOptionsResult = false;
supportsImageBitmapOptionsPromise.resolve(supportsImageBitmapOptionsResult);
return supportsImageBitmapOptionsPromise.promise;
}

var imageDataUri = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWP4////fwAJ+wP9CNHoHgAAAABJRU5ErkJggg==';
fetch(imageDataUri)
.then(function(response) {
return response.blob();
})
.then(function(blob) {
return createImageBitmap(blob, {
imageOrientation: 'flipY'
});
})
.then(function(imageBitmap) {
supportsImageBitmapOptionsResult = true;
supportsImageBitmapOptionsPromise.resolve(supportsImageBitmapOptionsResult);
})
.catch(function() {
supportsImageBitmapOptionsResult = false;
supportsImageBitmapOptionsPromise.resolve(supportsImageBitmapOptionsResult);
});

return supportsImageBitmapOptionsPromise.promise;
}

function supportsImageBitmapOptionsSync() {
if (!defined(supportsImageBitmapOptionsPromise)) {
supportsImageBitmapOptions();
}

return supportsImageBitmapOptionsResult;
}

var supportsFetchApiResult;
function supportsFetchApi() {
if (!defined(supportsFetchApiResult)) {
supportsFetchApiResult = defined(window.fetch);
}
return supportsFetchApiResult;
}

/**
* A set of functions to detect whether the current browser supports
* various features.
Expand All @@ -280,6 +343,10 @@ define([
supportsImageRenderingPixelated: supportsImageRenderingPixelated,
supportsWebP: supportsWebP,
supportsWebPSync: supportsWebPSync,
supportsCreateImageBitmap: supportsCreateImageBitmap,
supportsImageBitmapOptions: supportsImageBitmapOptions,
supportsImageBitmapOptionsSync: supportsImageBitmapOptionsSync,
supportsFetchApi : supportsFetchApi,
imageRenderingValue: imageRenderingValue,
typedArrayTypes: typedArrayTypes
};
Expand Down
22 changes: 22 additions & 0 deletions Specs/Core/FeatureDetectionSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,26 @@ defineSuite([
expect(FeatureDetection.supportsWebPSync()).toEqual(supportsWebP);
});
});

it('detects fetch API support', function() {
var supportsFetchApi = FeatureDetection.supportsFetchApi();
expect(typeof supportsFetchApi).toEqual('boolean');
});

it('detects createImageBitmap support', function() {
var supportsCreateImageBitmap = FeatureDetection.supportsCreateImageBitmap();
expect(typeof supportsCreateImageBitmap).toEqual('boolean');
});

it('detects ImageBitmapOptions support', function() {
if (FeatureDetection.supportsCreateImageBitmap()) {
expect(FeatureDetection.supportsImageBitmapOptionsSync()).not.toBeDefined();
}

return FeatureDetection.supportsImageBitmapOptions()
.then(function(supportsImageBitmapOptions) {
expect(typeof supportsImageBitmapOptions).toEqual('boolean');
expect(FeatureDetection.supportsImageBitmapOptionsSync()).toEqual(supportsImageBitmapOptions);
});
});
});

0 comments on commit 96cbc18

Please sign in to comment.