skia2/experimental/canvaskit/skottie.js
Florin Malita 71a23d4195 [canvaskit] Consolidate Skottie assets
- use a single name -> SkData map for both images and fonts

 - consolidate images/fonts as a generic "assets" API

 - pass pre-constructed asset maps to the SkottieAssetManager factory
   and remove setter (immutable class at this point)

Bug: skia:
Change-Id: Ief6d3bcbba339ccd1d81fd9950871eaa691ee947
Reviewed-on: https://skia-review.googlesource.com/c/196503
Reviewed-by: Kevin Lubick <kjlubick@google.com>
2019-03-01 13:14:12 +00:00

54 lines
1.9 KiB
JavaScript

// Adds compile-time JS functions to augment the CanvasKit interface.
// Specifically, anything that should only be on the Skottie builds of canvaskit.
CanvasKit.MakeManagedAnimation = function(json, assets) {
if (!CanvasKit._MakeManagedAnimation) {
throw 'Not compiled with MakeManagedAnimation';
}
if (!assets) {
return CanvasKit._MakeManagedAnimation(json, 0, nullptr, nullptr, nullptr);
}
var assetNamePtrs = [];
var assetDataPtrs = [];
var assetSizes = [];
var assetKeys = Object.keys(assets || {});
for (var i = 0; i < assetKeys.length; i++) {
var key = assetKeys[i];
var buffer = assets[key];
var data = new Uint8Array(buffer);
var iptr = CanvasKit._malloc(data.byteLength);
CanvasKit.HEAPU8.set(data, iptr);
assetDataPtrs.push(iptr);
assetSizes.push(data.byteLength);
// lengthBytesUTF8 and stringToUTF8Array are defined in the emscripten
// JS. See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#stringToUTF8
// Add 1 for null terminator
var strLen = lengthBytesUTF8(key) + 1;
var strPtr = CanvasKit._malloc(strLen);
stringToUTF8(key, strPtr, strLen);
assetNamePtrs.push(strPtr);
}
// Not entirely sure if it matters, but the uintptr_t are 32 bits
// we want to copy our array of uintptr_t into the right size memory.
var namesPtr = copy1dArray(assetNamePtrs, CanvasKit.HEAPU32);
var assetsPtr = copy1dArray(assetDataPtrs, CanvasKit.HEAPU32);
var assetSizesPtr = copy1dArray(assetSizes, CanvasKit.HEAPU32);
var anim = CanvasKit._MakeManagedAnimation(json, assetKeys.length, namesPtr,
assetsPtr, assetSizesPtr);
// We leave the asset data arrays and string data live and assume
// it is now owned by the C++ code
CanvasKit._free(namesPtr);
CanvasKit._free(assetsPtr);
CanvasKit._free(assetSizesPtr);
return anim;
};