Skip to content

[native_assets_cli] Improve documentation on asset paths #188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dcharkes opened this issue Nov 6, 2023 · 9 comments · Fixed by #946
Closed

[native_assets_cli] Improve documentation on asset paths #188

dcharkes opened this issue Nov 6, 2023 · 9 comments · Fixed by #946
Labels
P2 A bug or feature request we're likely to work on package:hooks

Comments

@dcharkes
Copy link
Collaborator

dcharkes commented Nov 6, 2023

The documentation on asset paths is not clear on what context these asset paths refer to.

  • Relative and Absolute: the location of the file after build.dart has been run. These are modified by flutter_tools and dart_dev upon copying assets to the app bundle.
  • System: the location of a dylib on the target device. These paths are copied as is to the kernel mapping.

We need some vocabulary to talk about these different phases.

Thanks @mkustermann for reporting!

@dcharkes dcharkes added P2 A bug or feature request we're likely to work on package:hooks labels Nov 6, 2023
@andzsinszan
Copy link

andzsinszan commented May 23, 2024

Quote from #946 :

Asset is renamed to NativeCodeAsset.

The term asset in Flutter and Android lingo often means fonts, images, audio, text files, etc. Most probably the word asset in the title does not cover these cases? To be honest, I don't know.

Also, I ran out of ideas how to search for non-code asset paths - especially in the context of native (C/C++) code - as Google strongly biased for results with shared libraries and their likes. My problem is that I cannot figure out the relative path to load a DNN model in C/ffi. Abs path works :-/ during dev.

@dcharkes
Copy link
Collaborator Author

Hi @andzsinszan, nice to meet you! Are you talking about assets from build hooks? Or assets from the pubspec in Flutter?

@andzsinszan
Copy link

andzsinszan commented May 24, 2024

Nice to meet you @dcharkes ,
Thanks for your great work, fascinating project, tons of contributions from you.

assets from build hooks? Or assets from the pubspec in Flutter?

I use pubspec. But probably that is the problem?
Resource is declared in pubspec of the FFI plugin.

flutter:
  assets:
    - res/model.onnx

It lands in the build dir. So far so good.

./build/linux/x64/debug/bundle/data/flutter_assets/packages/ffi_mic/res/model.onnx
./build/flutter_assets/packages/ffi_mic/res/model.onnx

During dev (linux target) I can only load the model if I use an abs path. The model loading takes place in C, path is a const char*. For the worse - wrt to asset bundles - the C code runs in an isolate. Currently I am out of ideas. I couldn't even google if an image can be loaded from C/FFI.

@dcharkes
Copy link
Collaborator Author

Resource is declared in pubspec of the FFI plugin.

Yes, the assets reported by build hooks are not connected at all to the assets from the pubspec at this point.
Also, the build hooks cannot yet report data assets, only code assets.

The assets from a Flutter pubspec are only available to Dart code as bytes or strings, they are not available as a file path.

Most likely the data assets from the build hooks, once they are implemented, will also not be available as a file path. (They might be bundled inside a zip file in an app bundle and not actually be on disk.)

@dcharkes
Copy link
Collaborator Author

Most likely the data assets from the build hooks, once they are implemented, will also not be available as a file path. (They might be bundled inside a zip file in an app bundle and not actually be on disk.)

I've written some ideas on if we could make the file paths for data assets from build hooks available in the future:

For your current setup, you probably need to read your asset bytes (https://docs.flutter.dev/ui/assets/assets-and-images#loading-assets) and then write them to a File manually (https://api.dart.dev/stable/3.4.1/dart-io/File/writeAsBytes.html) and pass the path of that file to your C function.

P.S. Feel free to open new issues instead of reviving closed ones.

@andzsinszan
Copy link

Hi @dcharkes ,

Many thank, you just saved me from hours of frustration.

The assets from a Flutter pubspec are only available to Dart code as bytes or strings, they are not available as a file path.

Great clarification. Explicitly stating what cannot be done is sometimes just as useful as the valid use cases.

read your asset bytes && write them to a File && pass the path of that file to your C

Very clear, thank you, will try.

P.S. Feel free to open new issues instead of reviving closed ones.

Thanks, will do - though trying not to spam the issue tracker.

@andzsinszan
Copy link

Just a quick feedback - hopefully not seen as spam.
Data asset loading works as advertised.
Loaded model in main isolate, sent over worker isolate, passed to C as const char*. Skipped the file writing - not much of a disk IO fan. The char-wise copy is ugly, but works.

final lstView = req.model.buffer.asUint8List();
final Pointer<Uint8> charStar = malloc<Uint8>(lstView.length);
for(var i =0; i < lstView.length; i++) {
  charStar[i] = lstView[i];
}
// void* char -> charStar.cast<Void>()

Thanks @dcharkes

@dcharkes
Copy link
Collaborator Author

The char-wise copy is ugly, but works.

charStart.asTypedList().setRange(0, lstView.length, lstView);

@andzsinszan
Copy link

thanks, all set.
I used this line below (adding length as arg):

charStar.asTypedList(lstView.length).setRange(0, lstView.length, lstView);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P2 A bug or feature request we're likely to work on package:hooks
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants