@@ -1267,34 +1267,46 @@ Float32List toSkColorStops(List<double>? colorStops) {
1267
1267
return skColorStops;
1268
1268
}
1269
1269
1270
- @JS ('Float32Array' )
1271
- external _NativeFloat32ArrayType get _nativeFloat32ArrayType;
1272
-
1273
1270
@JS ()
1274
1271
@staticInterop
1275
- class _NativeFloat32ArrayType {}
1272
+ abstract class _NativeType {}
1273
+
1274
+ @JS ('Float32Array' )
1275
+ external _NativeType get _nativeFloat32ArrayType;
1276
+
1277
+ @JS ('Uint32Array' )
1278
+ external _NativeType get _nativeUint32ArrayType;
1276
1279
1277
1280
@JS ('window.flutterCanvasKit.Malloc' )
1278
- external SkFloat32List _mallocFloat32List (
1279
- _NativeFloat32ArrayType float32ListType,
1280
- int size,
1281
- );
1281
+ external Object _malloc (_NativeType nativeType, int length);
1282
+
1283
+ /// Allocates a [Float32List] of [length] elements, backed by WASM memory,
1284
+ /// managed by a [SkFloat32List] .
1285
+ ///
1286
+ /// To free the allocated array use [free] .
1287
+ SkFloat32List mallocFloat32List (int length) {
1288
+ return _malloc (_nativeFloat32ArrayType, length) as SkFloat32List ;
1289
+ }
1282
1290
1283
- /// Allocates a [Float32List] backed by WASM memory, managed by
1284
- /// a [SkFloat32List ] .
1291
+ /// Allocates a [Uint32List] of [length] elements, backed by WASM memory,
1292
+ /// managed by a [SkUint32List ] .
1285
1293
///
1286
- /// To free the allocated array use [freeFloat32List ] .
1287
- SkFloat32List mallocFloat32List (int size ) {
1288
- return _mallocFloat32List (_nativeFloat32ArrayType, size) ;
1294
+ /// To free the allocated array use [free ] .
1295
+ SkUint32List mallocUint32List (int length ) {
1296
+ return _malloc (_nativeUint32ArrayType, length) as SkUint32List ;
1289
1297
}
1290
1298
1291
- /// Frees the WASM memory occupied by a [SkFloat32List] .
1299
+ /// Frees the WASM memory occupied by a [SkFloat32List] or [SkUint32List] .
1292
1300
///
1293
1301
/// The [list] is no longer usable after calling this function.
1294
1302
///
1295
1303
/// Use this function to free lists owned by the engine.
1296
1304
@JS ('window.flutterCanvasKit.Free' )
1297
- external void freeFloat32List (SkFloat32List list);
1305
+ external void free (MallocObj list);
1306
+
1307
+ @JS ()
1308
+ @staticInterop
1309
+ abstract class MallocObj {}
1298
1310
1299
1311
/// Wraps a [Float32List] backed by WASM memory.
1300
1312
///
@@ -1303,19 +1315,45 @@ external void freeFloat32List(SkFloat32List list);
1303
1315
/// that's attached to the current WASM memory block.
1304
1316
@JS ()
1305
1317
@staticInterop
1306
- class SkFloat32List {}
1318
+ class SkFloat32List extends MallocObj {}
1307
1319
1308
1320
extension SkFloat32ListExtension on SkFloat32List {
1321
+ /// The number of objects this pointer refers to.
1322
+ external int length;
1323
+
1309
1324
/// Returns the [Float32List] object backed by WASM memory.
1310
1325
///
1311
- /// Do not reuse the returned list across multiple WASM function/method
1326
+ /// Do not reuse the returned array across multiple WASM function/method
1312
1327
/// invocations that may lead to WASM memory to grow. When WASM memory
1313
- /// grows the [Float32List] object becomes "detached" and is no longer
1314
- /// usable. Instead, call this method every time you need to read from
1328
+ /// grows, the returned [Float32List] object becomes "detached" and is no
1329
+ /// longer usable. Instead, call this method every time you need to read from
1315
1330
/// or write to the list.
1316
1331
external Float32List toTypedArray ();
1317
1332
}
1318
1333
1334
+ /// Wraps a [Uint32List] backed by WASM memory.
1335
+ ///
1336
+ /// This wrapper is necessary because the raw [Uint32List] will get detached
1337
+ /// when WASM grows its memory. Call [toTypedArray] to get a new instance
1338
+ /// that's attached to the current WASM memory block.
1339
+ @JS ()
1340
+ @staticInterop
1341
+ class SkUint32List extends MallocObj {}
1342
+
1343
+ extension SkUint32ListExtension on SkUint32List {
1344
+ /// The number of objects this pointer refers to.
1345
+ external int length;
1346
+
1347
+ /// Returns the [Uint32List] object backed by WASM memory.
1348
+ ///
1349
+ /// Do not reuse the returned array across multiple WASM function/method
1350
+ /// invocations that may lead to WASM memory to grow. When WASM memory
1351
+ /// grows, the returned [Uint32List] object becomes "detached" and is no
1352
+ /// longer usable. Instead, call this method every time you need to read from
1353
+ /// or write to the list.
1354
+ external Uint32List toTypedArray ();
1355
+ }
1356
+
1319
1357
/// Writes [color] information into the given [skColor] buffer.
1320
1358
Float32List _populateSkColor (SkFloat32List skColor, ui.Color color) {
1321
1359
final Float32List array = skColor.toTypedArray ();
@@ -1585,7 +1623,7 @@ Float32List toOuterSkRect(ui.RRect rrect) {
1585
1623
/// Uses `CanvasKit.Malloc` to allocate storage for the points in the WASM
1586
1624
/// memory to avoid unnecessary copying. Unless CanvasKit takes ownership of
1587
1625
/// the list the returned list must be explicitly freed using
1588
- /// [freeMallocedFloat32List ] .
1626
+ /// [free ] .
1589
1627
SkFloat32List toMallocedSkPoints (List <ui.Offset > points) {
1590
1628
final int len = points.length;
1591
1629
final SkFloat32List skPoints = mallocFloat32List (len * 2 );
0 commit comments