• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Adds compile-time JS functions to augment the CanvasKit interface.
2// Specifically, anything that should only be on the Skottie builds of canvaskit.
3
4
5CanvasKit.MakeManagedAnimation = function(json, assets) {
6  if (!CanvasKit._MakeManagedAnimation) {
7    throw 'Not compiled with MakeManagedAnimation';
8  }
9  if (!assets) {
10    return CanvasKit._MakeManagedAnimation(json, 0, nullptr, nullptr, nullptr);
11  }
12  var assetNamePtrs = [];
13  var assetDataPtrs = [];
14  var assetSizes    = [];
15
16  var assetKeys = Object.keys(assets || {});
17  for (var i = 0; i < assetKeys.length; i++) {
18    var key = assetKeys[i];
19    var buffer = assets[key];
20    var data = new Uint8Array(buffer);
21
22    var iptr = CanvasKit._malloc(data.byteLength);
23    CanvasKit.HEAPU8.set(data, iptr);
24    assetDataPtrs.push(iptr);
25    assetSizes.push(data.byteLength);
26
27    // lengthBytesUTF8 and stringToUTF8Array are defined in the emscripten
28    // JS.  See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#stringToUTF8
29    // Add 1 for null terminator
30    var strLen = lengthBytesUTF8(key) + 1;
31    var strPtr = CanvasKit._malloc(strLen);
32
33    stringToUTF8(key, strPtr, strLen);
34    assetNamePtrs.push(strPtr);
35  }
36
37  // Not entirely sure if it matters, but the uintptr_t are 32 bits
38  // we want to copy our array of uintptr_t into the right size memory.
39  var namesPtr      = copy1dArray(assetNamePtrs, CanvasKit.HEAPU32);
40  var assetsPtr     = copy1dArray(assetDataPtrs, CanvasKit.HEAPU32);
41  var assetSizesPtr = copy1dArray(assetSizes,    CanvasKit.HEAPU32);
42
43  var anim = CanvasKit._MakeManagedAnimation(json, assetKeys.length, namesPtr,
44                                             assetsPtr, assetSizesPtr);
45
46  // We leave the asset data arrays and string data live and assume
47  // it is now owned by the C++ code
48  CanvasKit._free(namesPtr);
49  CanvasKit._free(assetsPtr);
50  CanvasKit._free(assetSizesPtr);
51
52  return anim;
53};
54