We need this as part of the XR image tracking work. Specifically, image tracking has a list of reference images:
https://github.com/BabylonJS/Babylon.js/blob/master/src/XR/features/WebXRImageTracking.ts#L22
These can either be strings (e.g. a url) or an ImageBitmap that has already been loaded. In the case of a string, the XR image tracking feature will use it as the source of an Image, load and decode that image, then pass the Image to Engine.createImageBitmap to create an ImageBitmap:
https://github.com/BabylonJS/Babylon.js/blob/master/src/XR/features/WebXRImageTracking.ts#L186-L197
Babylon Native already has a native polyfill for Image:
https://github.com/BabylonJS/BabylonNative/blob/master/Polyfills/Canvas/Source/Image.cpp
And this already supports loading from a string by setting the src property, and supports the onload callback, but it doesn't have the decode function today. That said, I believe the image decoding is done implicitly during load, so I think the first thing we need is:
Once that is done, then the XR image tracking code can pass the resulting Image into the call to createImageBitmap. Babylon Native already has a native implementation of createImageBitmap:
https://github.com/BabylonJS/BabylonNative/blob/master/Plugins/NativeEngine/Source/NativeEngine.cpp#L1511-L1542
However as is it only knows how to deal with the input arg being an ArrayBuffer. It needs to also be able to handle an Image being passed in. From a WebAPI standpoint, Image is basically an opaque object (you can't get direct access to the underlying image data buffer), which means whoever is implementing WebAPI needs to understand the internals of that opaque type. In the case of Babylon Native, it defines the Image type (through the native polyfill) and therefore understands the internal type. Given this, I think the next thing we need is:
This will allow NativeEngine::CreateImageBitmap accept an Image as input and return the corresponding ImageBitmap. Like Image, ImageBitmap is an opaque object from the WebAPI standpoint. I think this means that when that ImageBitmaps are passed from the JS code (as the XR session init arg, as the trackedImages), Babylon Native's NativeXR layer needs knowledge of the opaque ImageBitmap's internals. That is to say, it needs to read the data, width, and height properties off of the JS object to be passed down into the XR abstraction, and eventually passed on to ARKit/ARCore. This part is being handled though by another body of work that is more centrally about plumbing through image tracking support in that XR layer.
We need this as part of the XR image tracking work. Specifically, image tracking has a list of reference images:
https://github.com/BabylonJS/Babylon.js/blob/master/src/XR/features/WebXRImageTracking.ts#L22
These can either be strings (e.g. a url) or an
ImageBitmapthat has already been loaded. In the case of a string, the XR image tracking feature will use it as the source of anImage, load and decode that image, then pass theImagetoEngine.createImageBitmapto create anImageBitmap:https://github.com/BabylonJS/Babylon.js/blob/master/src/XR/features/WebXRImageTracking.ts#L186-L197
Babylon Native already has a native polyfill for
Image:https://github.com/BabylonJS/BabylonNative/blob/master/Polyfills/Canvas/Source/Image.cpp
And this already supports loading from a string by setting the
srcproperty, and supports theonloadcallback, but it doesn't have thedecodefunction today. That said, I believe the image decoding is done implicitly during load, so I think the first thing we need is:decodefunction to the native Image polyfill that returns aPromisethat is completed afteronloadis called.Once that is done, then the XR image tracking code can pass the resulting
Imageinto the call tocreateImageBitmap. Babylon Native already has a native implementation ofcreateImageBitmap:https://github.com/BabylonJS/BabylonNative/blob/master/Plugins/NativeEngine/Source/NativeEngine.cpp#L1511-L1542
However as is it only knows how to deal with the input arg being an
ArrayBuffer. It needs to also be able to handle anImagebeing passed in. From a WebAPI standpoint,Imageis basically an opaque object (you can't get direct access to the underlying image data buffer), which means whoever is implementing WebAPI needs to understand the internals of that opaque type. In the case of Babylon Native, it defines theImagetype (through the native polyfill) and therefore understands the internal type. Given this, I think the next thing we need is:Imagestore thebimg::ImageContainerpointer in a property (perhaps_imageContaineror something) on theImageobject, perhaps using theNapi::Pointer<bimg::ImageContainer>::Create(...)as seen elsewhere inNativeEngine.cpp.NativeEngine::CreateImageBitmapaccept anImageobject in addition to a rawArrayBuffer. I think this really means just accept a generic JS object that has an_imageContainerproperty defined. OnceCreateImageBitmaphas thebimg::ImageContainer, the rest of the existing code that creates aBitmapImagefrom abimg::ImageContainershould basically be the same.This will allow
NativeEngine::CreateImageBitmapaccept anImageas input and return the correspondingImageBitmap. LikeImage,ImageBitmapis an opaque object from the WebAPI standpoint. I think this means that when thatImageBitmaps are passed from the JS code (as the XR session init arg, as thetrackedImages), Babylon Native'sNativeXRlayer needs knowledge of the opaqueImageBitmap's internals. That is to say, it needs to read thedata,width, andheightproperties off of the JS object to be passed down into the XR abstraction, and eventually passed on to ARKit/ARCore. This part is being handled though by another body of work that is more centrally about plumbing through image tracking support in that XR layer.