-
Notifications
You must be signed in to change notification settings - Fork 6k
[web] Make Canvaskit's malloc more useful #38130
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1267,34 +1267,46 @@ Float32List toSkColorStops(List<double>? colorStops) { | |
| return skColorStops; | ||
| } | ||
|
|
||
| @JS('Float32Array') | ||
| external _NativeFloat32ArrayType get _nativeFloat32ArrayType; | ||
|
|
||
| @JS() | ||
| @staticInterop | ||
| class _NativeFloat32ArrayType {} | ||
| abstract class _NativeArrayType {} | ||
|
|
||
| @JS('Float32Array') | ||
| external _NativeArrayType get _nativeFloat32ArrayType; | ||
|
|
||
| @JS('Uint32Array') | ||
| external _NativeArrayType get _nativeUint32ArrayType; | ||
|
|
||
| @JS('window.flutterCanvasKit.Malloc') | ||
| external SkFloat32List _mallocFloat32List( | ||
| _NativeFloat32ArrayType float32ListType, | ||
| int size, | ||
| ); | ||
| external Object _mallocList(_NativeArrayType nativeArrayType, int size); | ||
|
|
||
| /// Allocates a [Float32List] backed by WASM memory, managed by | ||
| /// a [SkFloat32List]. | ||
| /// | ||
| /// To free the allocated array use [freeFloat32List]. | ||
| /// To free the allocated array use [freeList]. | ||
| SkFloat32List mallocFloat32List(int size) { | ||
| return _mallocFloat32List(_nativeFloat32ArrayType, size); | ||
| return _mallocList(_nativeFloat32ArrayType, size) as SkFloat32List; | ||
| } | ||
|
|
||
| /// Allocates a [Uint32List] backed by WASM memory, managed by | ||
| /// a [SkUint32List]. | ||
| /// | ||
| /// To free the allocated array use [freeList]. | ||
| SkUint32List mallocUint32List(int size) { | ||
| return _mallocList(_nativeUint32ArrayType, size) as SkUint32List; | ||
| } | ||
|
|
||
| /// Frees the WASM memory occupied by a [SkFloat32List]. | ||
| /// Frees the WASM memory occupied by a [SkFloat32List] or [SkUint32List]. | ||
| /// | ||
| /// The [list] is no longer usable after calling this function. | ||
| /// | ||
| /// Use this function to free lists owned by the engine. | ||
| @JS('window.flutterCanvasKit.Free') | ||
| external void freeFloat32List(SkFloat32List list); | ||
| external void freeList(_MallocObj list); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's strange for a public function to take a private type. Can Also, nit: avoid abbreviations -
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I'll make it public. As for the abbreviation, I used whatever name CanvasKit uses: https://github.com/google/skia/blob/0e95fd2482f8a456b2bcabf350efd51a042c3b97/modules/canvaskit/npm_build/types/index.d.ts#L786
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: do we need
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need |
||
|
|
||
| @JS() | ||
| @staticInterop | ||
| abstract class _MallocObj {} | ||
|
|
||
| /// Wraps a [Float32List] backed by WASM memory. | ||
| /// | ||
|
|
@@ -1303,19 +1315,45 @@ external void freeFloat32List(SkFloat32List list); | |
| /// that's attached to the current WASM memory block. | ||
| @JS() | ||
| @staticInterop | ||
| class SkFloat32List {} | ||
| class SkFloat32List extends _MallocObj {} | ||
|
|
||
| extension SkFloat32ListExtension on SkFloat32List { | ||
| /// The number of objects this pointer refers to. | ||
| external int length; | ||
|
|
||
| /// Returns the [Float32List] object backed by WASM memory. | ||
| /// | ||
| /// Do not reuse the returned list across multiple WASM function/method | ||
| /// Do not reuse the returned array across multiple WASM function/method | ||
| /// invocations that may lead to WASM memory to grow. When WASM memory | ||
| /// grows the [Float32List] object becomes "detached" and is no longer | ||
| /// usable. Instead, call this method every time you need to read from | ||
| /// grows, the returned [Float32List] object becomes "detached" and is no | ||
| /// longer usable. Instead, call this method every time you need to read from | ||
| /// or write to the list. | ||
| external Float32List toTypedArray(); | ||
| } | ||
|
|
||
| /// Wraps a [Uint32List] backed by WASM memory. | ||
| /// | ||
| /// This wrapper is necessary because the raw [Uint32List] will get detached | ||
| /// when WASM grows its memory. Call [toTypedArray] to get a new instance | ||
| /// that's attached to the current WASM memory block. | ||
| @JS() | ||
| @staticInterop | ||
| class SkUint32List extends _MallocObj {} | ||
|
|
||
| extension SkUint32ListExtension on SkUint32List { | ||
| /// The number of objects this pointer refers to. | ||
| external int length; | ||
|
|
||
| /// Returns the [Uint32List] object backed by WASM memory. | ||
| /// | ||
| /// Do not reuse the returned array across multiple WASM function/method | ||
| /// invocations that may lead to WASM memory to grow. When WASM memory | ||
| /// grows, the returned [Uint32List] object becomes "detached" and is no | ||
| /// longer usable. Instead, call this method every time you need to read from | ||
| /// or write to the list. | ||
| external Uint32List toTypedArray(); | ||
| } | ||
|
|
||
| /// Writes [color] information into the given [skColor] buffer. | ||
| Float32List _populateSkColor(SkFloat32List skColor, ui.Color color) { | ||
| final Float32List array = skColor.toTypedArray(); | ||
|
|
@@ -1584,7 +1622,7 @@ Float32List toOuterSkRect(ui.RRect rrect) { | |
| /// Uses `CanvasKit.Malloc` to allocate storage for the points in the WASM | ||
| /// memory to avoid unnecessary copying. Unless CanvasKit takes ownership of | ||
| /// the list the returned list must be explicitly freed using | ||
| /// [freeMallocedFloat32List]. | ||
| /// [freeList]. | ||
| SkFloat32List toMallocedSkPoints(List<ui.Offset> points) { | ||
| final int len = points.length; | ||
| final SkFloat32List skPoints = mallocFloat32List(len * 2); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's document if
sizeis in bytes or element count. Also, if it's the latter, then perhapslengthis a more idiomatic name for the parameter (and I thinksizeInBytesis more common for the other one).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll check what CanvasKit calls the parameter.