Skip to content

Commit fb483b4

Browse files
committed
moves async delivery of task to isolates
as Flutter's compute method does not allow for reuse of inner resources
1 parent e26b51e commit fb483b4

File tree

19 files changed

+426
-155
lines changed

19 files changed

+426
-155
lines changed

.vscode/settings.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"files.associations": {
3+
"base_options.h": "c",
4+
"__config": "c",
5+
"text_classifier.h": "c",
6+
"category.h": "c"
7+
}
8+
}

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ test: generate_text test_text generate_core test_core
1111
# Runs `ffigen` for all packages and all tests for all packages
1212
test_only: test_core test_text
1313

14+
# Rebuilds the MediaPipe task for macOS
15+
# Assumes google/mediapipe and google/flutter-mediapipe are siblings on the file system
16+
compile_text_classifier_macos:
17+
cd ../mediapipe && bazel build --linkopt -s --config darwin_arm64 --strip always --define MEDIAPIPE_DISABLE_GPU=1 mediapipe/tasks/c/text/text_classifier:libtext_classifier.dylib
18+
cd ../mediapipe && sudo cp bazel-bin/mediapipe/tasks/c/text/text_classifier/libtext_classifier.dylib ../flutter-mediapipe/packages/mediapipe-task-text/example/assets
19+
1420
# Core ---
1521

1622
# Runs `ffigen` for `mediapipe_core`
@@ -25,7 +31,7 @@ test_core:
2531

2632
# Runs `ffigen` for `mediapipe_text`
2733
generate_text:
28-
cd packages/mediapipe-task-text && dart run ffigen --config=ffigen.yaml
34+
cd packages/mediapipe-task-text && dart --enable-experiment=native-assets run ffigen --config=ffigen.yaml
2935

3036
# Compiles the faked C artifacts for testing
3137
compile_fake_text:

packages/mediapipe-core/ffigen.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ output:
77
import-path: "package:mediapipe_core/src/third_party/mediapipe/generated/mediapipe_common_bindings.dart"
88
headers:
99
entry-points:
10-
- "third_party/mediapipe/tasks/c/**"
10+
- "third_party/mediapipe/tasks/c/**.h"
1111
preamble: |
1212
/* Copyright 2023 The MediaPipe Authors.
1313

packages/mediapipe-core/lib/src/ffi_utils.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,21 @@ int _length(Pointer<Uint8> codeUnits) {
7676
}
7777
return length;
7878
}
79+
80+
/// Offers convenience and readability extensions for detecting null pointers.
81+
extension NullAwarePtr on Pointer {
82+
/// Returns true if this is a null pointer.
83+
bool get isNullPointer => address == 0;
84+
85+
/// Returns true if this is not a null pointer.
86+
bool get isNotNullPointer => address != 0;
87+
}
88+
89+
/// Returns true if this nullable pointer is both not-null and not a `NullPtr`,
90+
/// which is to say, its address is 0x00000000.
91+
bool isNotNullOrNullPointer(Pointer? ptr) =>
92+
ptr != null && ptr.isNotNullPointer;
93+
94+
/// Returns true if this nullable pointer is either null OR is a `NullPtr`,
95+
/// which is to say, its address is 0x00000000.
96+
bool isNullOrNullPointer(Pointer? ptr) => ptr == null || ptr.isNullPointer;

packages/mediapipe-core/lib/src/task_options.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class BaseOptions extends Equatable {
4747
}
4848
if (modelAssetBuffer != null) {
4949
struct.ref.model_asset_buffer = prepareUint8List(modelAssetBuffer!);
50+
struct.ref.model_asset_buffer_count = modelAssetBuffer!.lengthInBytes;
5051
}
5152
return struct;
5253
}

packages/mediapipe-core/lib/src/third_party/mediapipe/generated/mediapipe_common_bindings.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ final class BaseOptions extends ffi.Struct {
2323
external ffi.Pointer<ffi.Char> model_asset_buffer;
2424

2525
external ffi.Pointer<ffi.Char> model_asset_path;
26+
27+
@ffi.UnsignedInt()
28+
external int model_asset_buffer_count;
2629
}
2730

2831
final class __mbstate_t extends ffi.Union {

packages/mediapipe-core/third_party/mediapipe/tasks/c/core/base_options.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@ limitations under the License.
2020
extern "C" {
2121
#endif
2222

23-
// Base options for MediaPipe C Tasks.
24-
struct BaseOptions {
25-
// The model asset file contents as a string.
26-
char* model_asset_buffer;
27-
28-
// The path to the model asset to open and mmap in memory.
29-
char* model_asset_path;
30-
};
23+
// Base options for MediaPipe C Tasks.
24+
struct BaseOptions {
25+
// The model asset file contents as a string.
26+
char* model_asset_buffer;
27+
28+
// The path to the model asset to open and mmap in memory.
29+
char* model_asset_path;
30+
31+
// The size of the model assets buffer (or `0` if not set).
32+
unsigned int model_asset_buffer_count;
33+
};
3134

3235
#ifdef __cplusplus
3336
} // extern C

packages/mediapipe-task-text/ffigen.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ output:
44
bindings: "lib/src/mediapipe_text_bindings.dart"
55
headers:
66
entry-points:
7-
- "third_party/mediapipe/tasks/c/**"
7+
- "third_party/mediapipe/tasks/c/**.h"
88
import:
99
symbol-files:
1010
- "package:mediapipe_core/generated/core_symbols.yaml"

packages/mediapipe-task-text/lib/src/mediapipe_text_bindings.dart

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,39 @@ import 'dart:ffi' as ffi;
1818
import 'package:mediapipe_core/src/third_party/mediapipe/generated/mediapipe_common_bindings.dart'
1919
as imp1;
2020

21-
@ffi.Native<ffi.Pointer<ffi.Void> Function(ffi.Pointer<TextClassifierOptions>)>(
22-
symbol: 'text_classifier_create')
21+
@ffi.Native<
22+
ffi.Pointer<ffi.Void> Function(ffi.Pointer<TextClassifierOptions>,
23+
ffi.Pointer<ffi.Pointer<ffi.Char>>)>(symbol: 'text_classifier_create')
2324
external ffi.Pointer<ffi.Void> text_classifier_create(
2425
ffi.Pointer<TextClassifierOptions> options,
26+
ffi.Pointer<ffi.Pointer<ffi.Char>> error_msg,
2527
);
2628

2729
@ffi.Native<
28-
ffi.Int Function(ffi.Pointer<ffi.Void>, ffi.Pointer<ffi.Char>,
29-
ffi.Pointer<TextClassifierResult>)>(symbol: 'text_classifier_classify')
30+
ffi.Int Function(
31+
ffi.Pointer<ffi.Void>,
32+
ffi.Pointer<ffi.Char>,
33+
ffi.Pointer<TextClassifierResult>,
34+
ffi.Pointer<ffi.Pointer<ffi.Char>>)>(symbol: 'text_classifier_classify')
3035
external int text_classifier_classify(
3136
ffi.Pointer<ffi.Void> classifier,
3237
ffi.Pointer<ffi.Char> utf8_str,
3338
ffi.Pointer<TextClassifierResult> result,
39+
ffi.Pointer<ffi.Pointer<ffi.Char>> error_msg,
3440
);
3541

36-
@ffi.Native<ffi.Void Function(ffi.Pointer<ffi.Void>)>(
37-
symbol: 'text_classifier_close')
38-
external void text_classifier_close(
42+
@ffi.Native<ffi.Void Function(ffi.Pointer<TextClassifierResult>)>(
43+
symbol: 'text_classifier_close_result')
44+
external void text_classifier_close_result(
45+
ffi.Pointer<TextClassifierResult> result,
46+
);
47+
48+
@ffi.Native<
49+
ffi.Int Function(ffi.Pointer<ffi.Void>,
50+
ffi.Pointer<ffi.Pointer<ffi.Char>>)>(symbol: 'text_classifier_close')
51+
external int text_classifier_close(
3952
ffi.Pointer<ffi.Void> classifier,
53+
ffi.Pointer<ffi.Pointer<ffi.Char>> error_msg,
4054
);
4155

4256
final class TextClassifierOptions extends ffi.Struct {

packages/mediapipe-task-text/lib/src/tasks/text_classification/containers/text_classifier_result.dart

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'dart:ffi';
2-
import 'package:ffi/ffi.dart';
31
import 'package:mediapipe_core/mediapipe_core.dart';
42
import 'package:mediapipe_text/src/mediapipe_text_bindings.dart' as bindings;
53

@@ -41,16 +39,6 @@ class TextClassifierResult {
4139
);
4240
}
4341

44-
/// Deallocates all memory associated with an [bindings.ImageClassifierResult]
45-
/// in C memory.
46-
static void freeStruct(Pointer<bindings.TextClassifierResult> struct) {
47-
Classifications.freeStructs(
48-
struct.ref.classifications,
49-
struct.ref.classifications_count,
50-
);
51-
calloc.free(struct);
52-
}
53-
5442
/// Convenience helper for the first [Classifications] object.
5543
Classifications? get firstClassification =>
5644
classifications.isNotEmpty ? classifications.first : null;

0 commit comments

Comments
 (0)