Realize kernel asset mappings on a worker thread if one is available. (flutter/engine#6648)

This commit is contained in:
Chinmay Garde
2018-10-24 11:33:46 -07:00
committed by GitHub
parent 4f3e232de7
commit 68887916e2
14 changed files with 128 additions and 68 deletions

View File

@@ -15,9 +15,12 @@
namespace blink {
class AssetManager final : public AssetResolver,
public fml::RefCountedThreadSafe<AssetManager> {
class AssetManager final : public AssetResolver {
public:
AssetManager();
~AssetManager();
void PushFront(std::unique_ptr<AssetResolver> resolver);
void PushBack(std::unique_ptr<AssetResolver> resolver);
@@ -32,13 +35,7 @@ class AssetManager final : public AssetResolver,
private:
std::deque<std::unique_ptr<AssetResolver>> resolvers_;
AssetManager();
~AssetManager();
FML_DISALLOW_COPY_AND_ASSIGN(AssetManager);
FML_FRIEND_MAKE_REF_COUNTED(AssetManager);
FML_FRIEND_REF_COUNTED_THREAD_SAFE(AssetManager);
};
} // namespace blink

View File

@@ -21,7 +21,7 @@ void MappingReleaseProc(const void* ptr, void* context) {
} // anonymous namespace
AssetManagerFontProvider::AssetManagerFontProvider(
fml::RefPtr<blink::AssetManager> asset_manager)
std::shared_ptr<blink::AssetManager> asset_manager)
: asset_manager_(asset_manager) {}
AssetManagerFontProvider::~AssetManagerFontProvider() = default;
@@ -65,7 +65,7 @@ void AssetManagerFontProvider::RegisterAsset(std::string family_name,
}
AssetManagerFontStyleSet::AssetManagerFontStyleSet(
fml::RefPtr<blink::AssetManager> asset_manager)
std::shared_ptr<blink::AssetManager> asset_manager)
: asset_manager_(asset_manager) {}
AssetManagerFontStyleSet::~AssetManagerFontStyleSet() = default;

View File

@@ -5,6 +5,7 @@
#ifndef FLUTTER_LIB_UI_TEXT_ASSET_MANAGER_FONT_PROVIDER_H_
#define FLUTTER_LIB_UI_TEXT_ASSET_MANAGER_FONT_PROVIDER_H_
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
@@ -18,7 +19,7 @@ namespace blink {
class AssetManagerFontStyleSet : public SkFontStyleSet {
public:
AssetManagerFontStyleSet(fml::RefPtr<blink::AssetManager> asset_manager);
AssetManagerFontStyleSet(std::shared_ptr<blink::AssetManager> asset_manager);
~AssetManagerFontStyleSet() override;
@@ -37,7 +38,7 @@ class AssetManagerFontStyleSet : public SkFontStyleSet {
SkTypeface* matchStyle(const SkFontStyle& pattern) override;
private:
fml::RefPtr<blink::AssetManager> asset_manager_;
std::shared_ptr<blink::AssetManager> asset_manager_;
struct TypefaceAsset {
TypefaceAsset(std::string a) : asset(std::move(a)) {}
@@ -51,7 +52,8 @@ class AssetManagerFontStyleSet : public SkFontStyleSet {
class AssetManagerFontProvider : public txt::FontAssetProvider {
public:
AssetManagerFontProvider(fml::RefPtr<blink::AssetManager> asset_manager);
AssetManagerFontProvider(std::shared_ptr<blink::AssetManager> asset_manager);
~AssetManagerFontProvider() override;
void RegisterAsset(std::string family_name, std::string asset);
@@ -66,7 +68,7 @@ class AssetManagerFontProvider : public txt::FontAssetProvider {
SkFontStyleSet* MatchFamily(const std::string& family_name) override;
private:
fml::RefPtr<AssetManager> asset_manager_;
std::shared_ptr<AssetManager> asset_manager_;
std::unordered_map<std::string, AssetManagerFontStyleSet>
registered_families_;
std::vector<std::string> family_names_;

View File

@@ -67,7 +67,8 @@ std::shared_ptr<txt::FontCollection> FontCollection::GetFontCollection() const {
return collection_;
}
void FontCollection::RegisterFonts(fml::RefPtr<AssetManager> asset_manager) {
void FontCollection::RegisterFonts(
std::shared_ptr<AssetManager> asset_manager) {
std::unique_ptr<fml::Mapping> manifest_mapping =
asset_manager->GetAsMapping("FontManifest.json");
if (manifest_mapping == nullptr) {

View File

@@ -29,7 +29,7 @@ class FontCollection {
std::shared_ptr<txt::FontCollection> GetFontCollection() const;
void RegisterFonts(fml::RefPtr<AssetManager> asset_manager);
void RegisterFonts(std::shared_ptr<AssetManager> asset_manager);
void RegisterTestFonts();

View File

@@ -78,7 +78,7 @@ fml::WeakPtr<Engine> Engine::GetWeakPtr() const {
}
bool Engine::UpdateAssetManager(
fml::RefPtr<blink::AssetManager> new_asset_manager) {
std::shared_ptr<blink::AssetManager> new_asset_manager) {
if (asset_manager_ == new_asset_manager) {
return false;
}

View File

@@ -75,7 +75,7 @@ class Engine final : public blink::RuntimeDelegate {
FML_WARN_UNUSED_RESULT
bool Restart(RunConfiguration configuration);
bool UpdateAssetManager(fml::RefPtr<blink::AssetManager> asset_manager);
bool UpdateAssetManager(std::shared_ptr<blink::AssetManager> asset_manager);
void BeginFrame(fml::TimePoint frame_time);
@@ -121,7 +121,7 @@ class Engine final : public blink::RuntimeDelegate {
std::unique_ptr<blink::RuntimeController> runtime_controller_;
std::string initial_route_;
blink::ViewportMetrics viewport_metrics_;
fml::RefPtr<blink::AssetManager> asset_manager_;
std::shared_ptr<blink::AssetManager> asset_manager_;
bool activity_running_;
bool have_surface_;
blink::FontCollection font_collection_;

View File

@@ -4,6 +4,7 @@
#include "flutter/shell/common/isolate_configuration.h"
#include "flutter/fml/make_copyable.h"
#include "flutter/runtime/dart_vm.h"
#ifdef ERROR
@@ -61,7 +62,7 @@ class KernelIsolateConfiguration : public IsolateConfiguration {
class KernelListIsolateConfiguration final : public IsolateConfiguration {
public:
KernelListIsolateConfiguration(
std::vector<std::unique_ptr<fml::Mapping>> kernel_pieces)
std::vector<std::future<std::unique_ptr<fml::Mapping>>> kernel_pieces)
: kernel_pieces_(std::move(kernel_pieces)) {}
// |shell::IsolateConfiguration|
@@ -72,7 +73,8 @@ class KernelListIsolateConfiguration final : public IsolateConfiguration {
for (size_t i = 0; i < kernel_pieces_.size(); i++) {
bool last_piece = i + 1 == kernel_pieces_.size();
if (!isolate.PrepareForRunningFromKernel(std::move(kernel_pieces_[i]),
if (!isolate.PrepareForRunningFromKernel(kernel_pieces_[i].get(),
last_piece)) {
return false;
}
@@ -82,21 +84,81 @@ class KernelListIsolateConfiguration final : public IsolateConfiguration {
}
private:
std::vector<std::unique_ptr<fml::Mapping>> kernel_pieces_;
std::vector<std::future<std::unique_ptr<fml::Mapping>>> kernel_pieces_;
FML_DISALLOW_COPY_AND_ASSIGN(KernelListIsolateConfiguration);
};
static std::vector<std::string> ParseKernelListPaths(
std::unique_ptr<fml::Mapping> kernel_list) {
FML_DCHECK(kernel_list);
std::vector<std::string> kernel_pieces_paths;
const char* kernel_list_str =
reinterpret_cast<const char*>(kernel_list->GetMapping());
size_t kernel_list_size = kernel_list->GetSize();
size_t piece_path_start = 0;
while (piece_path_start < kernel_list_size) {
size_t piece_path_end = piece_path_start;
while ((piece_path_end < kernel_list_size) &&
(kernel_list_str[piece_path_end] != '\n')) {
piece_path_end++;
}
std::string piece_path(&kernel_list_str[piece_path_start],
piece_path_end - piece_path_start);
kernel_pieces_paths.emplace_back(std::move(piece_path));
piece_path_start = piece_path_end + 1;
}
return kernel_pieces_paths;
}
static std::vector<std::future<std::unique_ptr<fml::Mapping>>>
PrepareKernelMappings(std::vector<std::string> kernel_pieces_paths,
std::shared_ptr<blink::AssetManager> asset_manager,
fml::RefPtr<fml::TaskRunner> io_worker) {
FML_DCHECK(asset_manager);
std::vector<std::future<std::unique_ptr<fml::Mapping>>> fetch_futures;
for (const auto& kernel_pieces_path : kernel_pieces_paths) {
std::promise<std::unique_ptr<fml::Mapping>> fetch_promise;
fetch_futures.push_back(fetch_promise.get_future());
auto fetch_task =
fml::MakeCopyable([asset_manager, kernel_pieces_path,
fetch_promise = std::move(fetch_promise)]() mutable {
fetch_promise.set_value(
asset_manager->GetAsMapping(kernel_pieces_path));
});
// Fulfill the promise on the worker if one is available or the current
// thread if one is not.
if (io_worker) {
io_worker->PostTask(fetch_task);
} else {
fetch_task();
}
}
return fetch_futures;
}
std::unique_ptr<IsolateConfiguration> IsolateConfiguration::InferFromSettings(
const blink::Settings& settings,
fml::RefPtr<blink::AssetManager> asset_manager) {
std::shared_ptr<blink::AssetManager> asset_manager,
fml::RefPtr<fml::TaskRunner> io_worker) {
// Running in AOT mode.
if (blink::DartVM::IsRunningPrecompiledCode()) {
return CreateForAppSnapshot();
}
if (!asset_manager) {
return nullptr;
}
// Running from kernel snapshot.
if (asset_manager) {
{
std::unique_ptr<fml::Mapping> kernel =
asset_manager->GetAsMapping(settings.application_kernel_asset);
if (kernel) {
@@ -105,39 +167,14 @@ std::unique_ptr<IsolateConfiguration> IsolateConfiguration::InferFromSettings(
}
// Running from kernel divided into several pieces (for sharing).
// TODO(fuchsia): Use async blobfs API once it becomes available.
if (asset_manager) {
{
std::unique_ptr<fml::Mapping> kernel_list =
asset_manager->GetAsMapping(settings.application_kernel_list_asset);
if (kernel_list) {
const char* kernel_list_str =
reinterpret_cast<const char*>(kernel_list->GetMapping());
size_t kernel_list_size = kernel_list->GetSize();
std::vector<std::unique_ptr<fml::Mapping>> kernel_pieces;
size_t piece_path_start = 0;
while (piece_path_start < kernel_list_size) {
size_t piece_path_end = piece_path_start;
while ((piece_path_end < kernel_list_size) &&
(kernel_list_str[piece_path_end] != '\n')) {
piece_path_end++;
}
std::string piece_path(&kernel_list_str[piece_path_start],
piece_path_end - piece_path_start);
std::unique_ptr<fml::Mapping> piece =
asset_manager->GetAsMapping(piece_path);
if (piece == nullptr) {
FML_LOG(ERROR) << "Failed to load: " << piece_path;
return nullptr;
}
kernel_pieces.emplace_back(std::move(piece));
piece_path_start = piece_path_end + 1;
}
return CreateForKernelList(std::move(kernel_pieces));
auto kernel_pieces_paths = ParseKernelListPaths(std::move(kernel_list));
auto kernel_mappings = PrepareKernelMappings(
std::move(kernel_pieces_paths), asset_manager, io_worker);
return CreateForKernelList(std::move(kernel_mappings));
}
}
@@ -156,6 +193,17 @@ std::unique_ptr<IsolateConfiguration> IsolateConfiguration::CreateForKernel(
std::unique_ptr<IsolateConfiguration> IsolateConfiguration::CreateForKernelList(
std::vector<std::unique_ptr<fml::Mapping>> kernel_pieces) {
std::vector<std::future<std::unique_ptr<fml::Mapping>>> pieces;
for (auto& piece : kernel_pieces) {
std::promise<std::unique_ptr<fml::Mapping>> promise;
pieces.push_back(promise.get_future());
promise.set_value(std::move(piece));
}
return CreateForKernelList(std::move(pieces));
}
std::unique_ptr<IsolateConfiguration> IsolateConfiguration::CreateForKernelList(
std::vector<std::future<std::unique_ptr<fml::Mapping>>> kernel_pieces) {
return std::make_unique<KernelListIsolateConfiguration>(
std::move(kernel_pieces));
}

View File

@@ -5,6 +5,7 @@
#ifndef FLUTTER_SHELL_COMMON_ISOLATE_CONFIGURATION_H_
#define FLUTTER_SHELL_COMMON_ISOLATE_CONFIGURATION_H_
#include <future>
#include <memory>
#include <string>
@@ -22,13 +23,20 @@ class IsolateConfiguration {
public:
static std::unique_ptr<IsolateConfiguration> InferFromSettings(
const blink::Settings& settings,
fml::RefPtr<blink::AssetManager> asset_manager);
std::shared_ptr<blink::AssetManager> asset_manager,
fml::RefPtr<fml::TaskRunner> io_worker);
static std::unique_ptr<IsolateConfiguration> CreateForAppSnapshot();
static std::unique_ptr<IsolateConfiguration> CreateForKernel(
std::unique_ptr<fml::Mapping> kernel);
static std::unique_ptr<IsolateConfiguration> CreateForKernelList(
std::vector<std::future<std::unique_ptr<fml::Mapping>>> kernel_pieces);
// TODO(chinmaygarde): Remove this variant in favor of the one using futures
// for parallelizing asset loads. This one is in place for API compatibility
// till Android is updated.
static std::unique_ptr<IsolateConfiguration> CreateForKernelList(
std::vector<std::unique_ptr<fml::Mapping>> kernel_pieces);

View File

@@ -13,8 +13,9 @@
namespace shell {
RunConfiguration RunConfiguration::InferFromSettings(
const blink::Settings& settings) {
auto asset_manager = fml::MakeRefCounted<blink::AssetManager>();
const blink::Settings& settings,
fml::RefPtr<fml::TaskRunner> io_worker) {
auto asset_manager = std::make_shared<blink::AssetManager>();
asset_manager->PushBack(std::make_unique<blink::DirectoryAssetBundle>(
fml::Duplicate(settings.assets_dir)));
@@ -23,18 +24,19 @@ RunConfiguration RunConfiguration::InferFromSettings(
std::make_unique<blink::DirectoryAssetBundle>(fml::OpenDirectory(
settings.assets_path.c_str(), false, fml::FilePermission::kRead)));
return {IsolateConfiguration::InferFromSettings(settings, asset_manager),
return {IsolateConfiguration::InferFromSettings(settings, asset_manager,
io_worker),
asset_manager};
}
RunConfiguration::RunConfiguration(
std::unique_ptr<IsolateConfiguration> configuration)
: RunConfiguration(std::move(configuration),
fml::MakeRefCounted<blink::AssetManager>()) {}
std::make_shared<blink::AssetManager>()) {}
RunConfiguration::RunConfiguration(
std::unique_ptr<IsolateConfiguration> configuration,
fml::RefPtr<blink::AssetManager> asset_manager)
std::shared_ptr<blink::AssetManager> asset_manager)
: isolate_configuration_(std::move(configuration)),
asset_manager_(std::move(asset_manager)) {}
@@ -66,7 +68,7 @@ void RunConfiguration::SetEntrypointAndLibrary(std::string entrypoint,
entrypoint_library_ = std::move(library);
}
fml::RefPtr<blink::AssetManager> RunConfiguration::GetAssetManager() const {
std::shared_ptr<blink::AssetManager> RunConfiguration::GetAssetManager() const {
return asset_manager_;
}

View File

@@ -20,12 +20,14 @@ namespace shell {
class RunConfiguration {
public:
static RunConfiguration InferFromSettings(const blink::Settings& settings);
static RunConfiguration InferFromSettings(
const blink::Settings& settings,
fml::RefPtr<fml::TaskRunner> io_worker = nullptr);
RunConfiguration(std::unique_ptr<IsolateConfiguration> configuration);
RunConfiguration(std::unique_ptr<IsolateConfiguration> configuration,
fml::RefPtr<blink::AssetManager> asset_manager);
std::shared_ptr<blink::AssetManager> asset_manager);
RunConfiguration(RunConfiguration&&);
@@ -39,7 +41,7 @@ class RunConfiguration {
void SetEntrypointAndLibrary(std::string entrypoint, std::string library);
fml::RefPtr<blink::AssetManager> GetAssetManager() const;
std::shared_ptr<blink::AssetManager> GetAssetManager() const;
const std::string& GetEntrypoint() const;
@@ -49,7 +51,7 @@ class RunConfiguration {
private:
std::unique_ptr<IsolateConfiguration> isolate_configuration_;
fml::RefPtr<blink::AssetManager> asset_manager_;
std::shared_ptr<blink::AssetManager> asset_manager_;
std::string entrypoint_ = "main";
std::string entrypoint_library_ = "";

View File

@@ -949,7 +949,7 @@ bool Shell::OnServiceProtocolSetAssetBundlePath(
auto& allocator = response.GetAllocator();
response.SetObject();
auto asset_manager = fml::MakeRefCounted<blink::AssetManager>();
auto asset_manager = std::make_shared<blink::AssetManager>();
asset_manager->PushFront(std::make_unique<blink::DirectoryAssetBundle>(
fml::OpenDirectory(params.at("assetDirectory").ToString().c_str(), false,

View File

@@ -241,7 +241,7 @@ static void RunBundleAndSnapshotFromLibrary(JNIEnv* env,
jstring jEntrypoint,
jstring jLibraryUrl,
jobject jAssetManager) {
auto asset_manager = fml::MakeRefCounted<blink::AssetManager>();
auto asset_manager = std::make_shared<blink::AssetManager>();
const auto bundlepath = fml::jni::JavaStringToString(env, jbundlepath);
if (bundlepath.size() > 0) {

View File

@@ -145,7 +145,7 @@ int RunTester(const blink::Settings& settings, bool run_forever) {
return EXIT_FAILURE;
}
auto asset_manager = fml::MakeRefCounted<blink::AssetManager>();
auto asset_manager = std::make_shared<blink::AssetManager>();
asset_manager->PushBack(std::make_unique<blink::DirectoryAssetBundle>(
fml::Duplicate(settings.assets_dir)));
asset_manager->PushBack(