• 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// assets is a dictionary of named blobs: { key: ArrayBuffer, ... }
5// The keys should be well-behaved strings - they're turned into null-terminated
6// strings for the native side.
7CanvasKit.MakeManagedAnimation = function(json, assets) {
8  if (!CanvasKit._MakeManagedAnimation) {
9    throw 'Not compiled with MakeManagedAnimation';
10  }
11  if (!assets) {
12    return CanvasKit._MakeManagedAnimation(json, 0, nullptr, nullptr, nullptr);
13  }
14  var assetNamePtrs = [];
15  var assetDataPtrs = [];
16  var assetSizes    = [];
17
18  var assetKeys = Object.keys(assets || {});
19  for (var i = 0; i < assetKeys.length; i++) {
20    var key = assetKeys[i];
21    var buffer = assets[key];
22    var data = new Uint8Array(buffer);
23
24    var iptr = CanvasKit._malloc(data.byteLength);
25    CanvasKit.HEAPU8.set(data, iptr);
26    assetDataPtrs.push(iptr);
27    assetSizes.push(data.byteLength);
28
29    // lengthBytesUTF8 and stringToUTF8Array are defined in the emscripten
30    // JS.  See https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#stringToUTF8
31    // Add 1 for null terminator
32    var strLen = lengthBytesUTF8(key) + 1;
33    var strPtr = CanvasKit._malloc(strLen);
34
35    stringToUTF8(key, strPtr, strLen);
36    assetNamePtrs.push(strPtr);
37  }
38
39  // Not entirely sure if it matters, but the uintptr_t are 32 bits
40  // we want to copy our array of uintptr_t into the right size memory.
41  var namesPtr      = copy1dArray(assetNamePtrs, CanvasKit.HEAPU32);
42  var assetsPtr     = copy1dArray(assetDataPtrs, CanvasKit.HEAPU32);
43  var assetSizesPtr = copy1dArray(assetSizes,    CanvasKit.HEAPU32);
44
45  var anim = CanvasKit._MakeManagedAnimation(json, assetKeys.length, namesPtr,
46                                             assetsPtr, assetSizesPtr);
47
48  // The C++ code has made copies of the asset and string data, so free our copies.
49  CanvasKit._free(namesPtr);
50  CanvasKit._free(assetsPtr);
51  CanvasKit._free(assetSizesPtr);
52
53  return anim;
54};
55