This repository has been archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
[dart:ui] add an explicit API for loading assets #21153
Closed
Closed
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
78941c9
[dart:ui] add an explicit API for loading assets
jonahwilliams 31c26c9
nullcheck mapping
jonahwilliams 226be4a
apply formatting
jonahwilliams f284e47
more clang format fix
jonahwilliams 79833b7
unsafe use of dart API
jonahwilliams 87dd5e1
Merge branch 'master' of github.com:flutter/engine into add_asset_loa…
jonahwilliams bcf4c84
Merge branch 'master' of github.com:flutter/engine into add_asset_loa…
jonahwilliams b5892d3
more draft work on immut buffer
jonahwilliams File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "flutter/lib/ui/assets.h" | ||
|
||
#include "flutter/assets/asset_manager.h" | ||
#include "flutter/lib/ui/ui_dart_state.h" | ||
#include "flutter/lib/ui/window/platform_configuration.h" | ||
#include "third_party/tonic/dart_binding_macros.h" | ||
#include "third_party/tonic/dart_library_natives.h" | ||
#include "third_party/tonic/logging/dart_invoke.h" | ||
#include "third_party/tonic/typed_data/dart_byte_data.h" | ||
#include "third_party/tonic/typed_data/typed_list.h" | ||
|
||
using tonic::DartInvoke; | ||
using tonic::DartPersistentValue; | ||
using tonic::ToDart; | ||
|
||
namespace flutter { | ||
|
||
void FinalizeSkData(void* isolate_callback_data, | ||
Dart_WeakPersistentHandle handle, | ||
void* peer) { | ||
|
||
} | ||
|
||
void Assets::loadAssetBytes(Dart_NativeArguments args) { | ||
UIDartState::ThrowIfUIOperationsProhibited(); | ||
Dart_Handle callback = Dart_GetNativeArgument(args, 1); | ||
if (!Dart_IsClosure(callback)) { | ||
Dart_SetReturnValue(args, tonic::ToDart("Callback must be a function")); | ||
return; | ||
} | ||
Dart_Handle asset_name_handle = Dart_GetNativeArgument(args, 0); | ||
uint8_t* chars = nullptr; | ||
intptr_t asset_length = 0; | ||
Dart_Handle result = | ||
Dart_StringToUTF8(asset_name_handle, &chars, &asset_length); | ||
if (Dart_IsError(result)) { | ||
Dart_PropagateError(result); | ||
return; | ||
} | ||
std::string asset_name = std::string{reinterpret_cast<const char*>(chars), | ||
static_cast<size_t>(asset_length)}; | ||
|
||
std::shared_ptr<AssetManager> asset_manager = UIDartState::Current() | ||
->platform_configuration() | ||
->client() | ||
->GetAssetManager(); | ||
std::unique_ptr<fml::Mapping> data = asset_manager->GetAsMapping(asset_name); | ||
|
||
if (data == nullptr) { | ||
return; | ||
} | ||
const void* bytes_ = static_cast<const void*>(data->GetMapping()); | ||
void* bytes = const_cast<void*>(bytes_); | ||
const intptr_t length = data->GetSize(); | ||
void* peer = reinterpret_cast<void*>(data.release()); | ||
Dart_Handle byte_buffer = Dart_NewExternalTypedDataWithFinalizer( | ||
Dart_TypedData_kUint8, bytes, length, peer, length, FinalizeSkData); | ||
|
||
// Dart_Handle byte_buffer = | ||
// tonic::DartByteData::Create(data->GetMapping(), data->GetSize()); | ||
tonic::DartInvoke(callback, {ToDart(byte_buffer)}); | ||
} | ||
|
||
void Assets::RegisterNatives(tonic::DartLibraryNatives* natives) { | ||
natives->Register({ | ||
{"loadAssetBytes", loadAssetBytes, 2, true}, | ||
}); | ||
} | ||
|
||
} // namespace flutter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
// @dart = 2.10 | ||
|
||
part of dart.ui; | ||
|
||
/// Load the asset bytes specified by [assetKey]. | ||
/// | ||
/// The [assetKey] is generally the filepath of the asset which is bundled | ||
/// into the flutter application. This API is not supported on the Web. | ||
/// | ||
/// If the [assetKey] does not correspond to a real asset, returns `null`. | ||
ByteData? loadAsset(String assetKey) { | ||
ByteData? result; | ||
_loadAsset(assetKey, (Uint8List bytes) { | ||
result = bytes.buffer.asByteData(); | ||
}); | ||
return result; | ||
} | ||
|
||
void _loadAsset(String asseyKey, void Function(Uint8List) onData) native 'loadAssetBytes'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef FLUTTER_LIB_UI_ASSETS_H_ | ||
#define FLUTTER_LIB_UI_ASSETS_H_ | ||
|
||
#include "flutter/assets/asset_manager.h" | ||
#include "flutter/lib/ui/ui_dart_state.h" | ||
#include "flutter/lib/ui/window/platform_configuration.h" | ||
#include "third_party/tonic/dart_binding_macros.h" | ||
#include "third_party/tonic/dart_library_natives.h" | ||
#include "third_party/tonic/logging/dart_invoke.h" | ||
#include "third_party/tonic/typed_data/dart_byte_data.h" | ||
#include "third_party/tonic/typed_data/typed_list.h" | ||
|
||
namespace flutter { | ||
|
||
class Assets { | ||
public: | ||
static void loadAssetBytes(Dart_NativeArguments args); | ||
|
||
static void RegisterNatives(tonic::DartLibraryNatives* natives); | ||
}; | ||
} // namespace flutter | ||
#endif // FLUTTER_LIB_UI_ASSETS_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
// @dart = 2.10 | ||
part of ui; | ||
|
||
/// Load the asset bytes specified by [assetKey]. | ||
/// | ||
/// The [assetKey] is generally the filepath of the asset which is bundled | ||
/// into the flutter application. This API is not supported on the | ||
/// Web. | ||
/// | ||
/// If the [assetKey] does not correspond to a real asset, returns `null`. | ||
ByteData? loadAsset(String assetKey) { | ||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
// @dart = 2.6 | ||
import 'dart:ui' as ui; | ||
|
||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
test('Loading an asset that does not exist returns null', () { | ||
expect(ui.loadAsset('ThisDoesNotExist'), null); | ||
}); | ||
|
||
test('returns the bytes of a bundled asset', () { | ||
expect(ui.loadAsset('FontManifest.json'), isNotNull); | ||
}); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If we use
ImmutableBuffer
here, we could have a nice fast path for loading these without relying on Dart.But right now, that class is only useful when creating images. We'd want to consider extending it in some way so you could get a
ByteData
from it, or a Uint8List or something.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.
Today, we ahve
ImmutableBuffer.fromUint8List
, but this could allow for creatingImmutableBuffer
s directly, which would definitely be useful for e.g.Image.asset
.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.
Sorry I'm not remembering all of the context around
ImmutableBuffer
. If we had aReadonlyUint8List
from the core Dart SDK, wouldImmutableBuffer
still be needed?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.
Ultimately we need a way to somewhat compatible with the existing API and expose the bytes to users applications. Assets are used for more than just images today, we have APIs to load strings/bytedata/json
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.
that's not to say we couldn't add a special API for images though, they are one of the most common cases
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.
Agreed.
ImmutableBuffer was added to avoid having to do multiple copies of image data from a Uint8List to an SkData buffer if it ends up being reused.
If the Dart SDK has an appropriate type that's useful outside of dart:ui, that's probably better. But if not we could expand ImmutableBuffer to have access as ByteData (which, when Dart added some type appropriate for it, could just become a pass through to that type).