Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions assets/directory_asset_bundle.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@
#include <vector>

#include "lib/fxl/macros.h"
#include "lib/fxl/memory/ref_counted.h"

namespace blink {

class DirectoryAssetBundle {
class DirectoryAssetBundle
: public fxl::RefCountedThreadSafe<DirectoryAssetBundle> {
public:
explicit DirectoryAssetBundle(std::string directory);
~DirectoryAssetBundle();

bool GetAsBuffer(const std::string& asset_name, std::vector<uint8_t>* data);

private:
std::string GetPathForAsset(const std::string& asset_name);

private:
const std::string directory_;

FXL_DISALLOW_COPY_AND_ASSIGN(DirectoryAssetBundle);
Expand Down
12 changes: 7 additions & 5 deletions lib/ui/text/font_collection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ std::shared_ptr<txt::FontCollection> FontCollection::GetFontCollection() const {
return collection_;
}

void FontCollection::RegisterFontsFromAssetStore(
fxl::RefPtr<blink::ZipAssetStore> asset_store) {
if (!asset_store) {
void FontCollection::RegisterFontsFromDirectoryAssetBundle(
fxl::RefPtr<blink::DirectoryAssetBundle> directory_asset_bundle) {
if (!directory_asset_bundle) {
return;
}

std::vector<uint8_t> manifest_data;
if (!asset_store->GetAsBuffer("FontManifest.json", &manifest_data)) {
if (!directory_asset_bundle->GetAsBuffer("FontManifest.json",
&manifest_data)) {
FXL_DLOG(WARNING) << "Could not find the font manifest in the asset store.";
return;
}
Expand Down Expand Up @@ -91,7 +92,8 @@ void FontCollection::RegisterFontsFromAssetStore(

// TODO: Handle weights and styles.
std::vector<uint8_t> font_data;
if (asset_store->GetAsBuffer(font_asset->value.GetString(), &font_data)) {
if (directory_asset_bundle->GetAsBuffer(font_asset->value.GetString(),
&font_data)) {
// The data must be copied because it needs to be moved into the
// typeface as a stream.
auto data =
Expand Down
6 changes: 3 additions & 3 deletions lib/ui/text/font_collection.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include <memory>
#include <vector>
#include "flutter/assets/zip_asset_store.h"
#include "flutter/assets/directory_asset_bundle.h"
#include "lib/fxl/macros.h"
#include "lib/fxl/memory/ref_ptr.h"
#include "txt/asset_data_provider.h"
Expand All @@ -21,8 +21,8 @@ class FontCollection {

std::shared_ptr<txt::FontCollection> GetFontCollection() const;

void RegisterFontsFromAssetStore(
fxl::RefPtr<blink::ZipAssetStore> asset_store);
void RegisterFontsFromDirectoryAssetBundle(
fxl::RefPtr<blink::DirectoryAssetBundle> directory_asset_bundle);

void RegisterTestFonts();

Expand Down
18 changes: 11 additions & 7 deletions runtime/asset_font_selector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "flutter/runtime/asset_font_selector.h"

#include "flutter/assets/zip_asset_store.h"
#include "flutter/assets/directory_asset_bundle.h"
#include "flutter/lib/ui/ui_dart_state.h"
#include "flutter/sky/engine/platform/fonts/FontData.h"
#include "flutter/sky/engine/platform/fonts/FontFaceCreationParams.h"
Expand Down Expand Up @@ -80,15 +80,17 @@ struct FontMatcher {

} // namespace

void AssetFontSelector::Install(fxl::RefPtr<ZipAssetStore> asset_store) {
void AssetFontSelector::Install(
fxl::RefPtr<DirectoryAssetBundle> directory_asset_bundle) {
RefPtr<AssetFontSelector> font_selector =
adoptRef(new AssetFontSelector(std::move(asset_store)));
adoptRef(new AssetFontSelector(std::move(directory_asset_bundle)));
font_selector->parseFontManifest();
UIDartState::Current()->set_font_selector(font_selector);
}

AssetFontSelector::AssetFontSelector(fxl::RefPtr<ZipAssetStore> asset_store)
: asset_store_(std::move(asset_store)) {}
AssetFontSelector::AssetFontSelector(
fxl::RefPtr<DirectoryAssetBundle> directory_asset_bundle)
: directory_asset_bundle_(std::move(directory_asset_bundle)) {}

AssetFontSelector::~AssetFontSelector() {}

Expand All @@ -106,7 +108,8 @@ AssetFontSelector::FlutterFontAttributes::~FlutterFontAttributes() {}

void AssetFontSelector::parseFontManifest() {
std::vector<uint8_t> font_manifest_data;
if (!asset_store_->GetAsBuffer(kFontManifestAssetPath, &font_manifest_data))
if (!directory_asset_bundle_->GetAsBuffer(kFontManifestAssetPath,
&font_manifest_data))
return;

rapidjson::Document document;
Expand Down Expand Up @@ -222,7 +225,8 @@ sk_sp<SkTypeface> AssetFontSelector::getTypefaceAsset(
}

std::unique_ptr<TypefaceAsset> typeface_asset(new TypefaceAsset);
if (!asset_store_->GetAsBuffer(asset_path, &typeface_asset->data)) {
if (!directory_asset_bundle_->GetAsBuffer(asset_path,
&typeface_asset->data)) {
typeface_cache_.insert(std::make_pair(asset_path, nullptr));
return nullptr;
}
Expand Down
9 changes: 5 additions & 4 deletions runtime/asset_font_selector.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <unordered_map>
#include <vector>

#include "flutter/assets/zip_asset_store.h"
#include "flutter/assets/directory_asset_bundle.h"
#include "flutter/sky/engine/platform/fonts/FontCacheKey.h"
#include "flutter/sky/engine/platform/fonts/FontSelector.h"
#include "flutter/sky/engine/platform/fonts/SimpleFontData.h"
Expand All @@ -23,7 +23,7 @@ class AssetFontSelector : public FontSelector {

~AssetFontSelector() override;

static void Install(fxl::RefPtr<ZipAssetStore> asset_store);
static void Install(fxl::RefPtr<DirectoryAssetBundle> directory_asset_bundle);

PassRefPtr<FontData> getFontData(const FontDescription& font_description,
const AtomicString& family_name) override;
Expand All @@ -39,14 +39,15 @@ class AssetFontSelector : public FontSelector {
private:
struct TypefaceAsset;

explicit AssetFontSelector(fxl::RefPtr<ZipAssetStore> asset_store);
explicit AssetFontSelector(
fxl::RefPtr<DirectoryAssetBundle> directory_asset_bundle);

void parseFontManifest();

sk_sp<SkTypeface> getTypefaceAsset(const FontDescription& font_description,
const AtomicString& family_name);

fxl::RefPtr<ZipAssetStore> asset_store_;
fxl::RefPtr<DirectoryAssetBundle> directory_asset_bundle_;

HashMap<AtomicString, std::vector<FlutterFontAttributes>> font_family_map_;

Expand Down
41 changes: 36 additions & 5 deletions runtime/dart_init.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <utility>
#include <vector>

#include "flutter/assets/directory_asset_bundle.h"
#include "flutter/assets/unzipper_provider.h"
#include "flutter/assets/zip_asset_store.h"
#include "flutter/common/settings.h"
Expand All @@ -27,6 +28,7 @@
#include "flutter/runtime/start_up.h"
#include "lib/fxl/arraysize.h"
#include "lib/fxl/build_config.h"
#include "lib/fxl/files/path.h"
#include "lib/fxl/logging.h"
#include "lib/fxl/time/time_delta.h"
#include "lib/tonic/converter/dart_converter.h"
Expand Down Expand Up @@ -265,6 +267,16 @@ Dart_Isolate ServiceIsolateCreateCallback(const char* script_uri,
#endif // FLUTTER_RUNTIME_MODE
}

static bool GetAssetAsBuffer(
const std::string& name,
std::vector<uint8_t>* data,
std::unique_ptr<DirectoryAssetBundle>& directory_asset_bundle,
fxl::RefPtr<ZipAssetStore>& asset_store) {
return (directory_asset_bundle &&
directory_asset_bundle->GetAsBuffer(name, data)) ||
(asset_store && asset_store->GetAsBuffer(name, data));
}

Dart_Isolate IsolateCreateCallback(const char* script_uri,
const char* main,
const char* package_root,
Expand Down Expand Up @@ -296,11 +308,30 @@ Dart_Isolate IsolateCreateCallback(const char* script_uri,
if (!running_from_source) {
// Attempt to copy the snapshot from the asset bundle.
const std::string& bundle_path = entry_path;
fxl::RefPtr<ZipAssetStore> zip_asset_store =
fxl::MakeRefCounted<ZipAssetStore>(
GetUnzipperProviderForPath(std::move(bundle_path)));
zip_asset_store->GetAsBuffer(kKernelAssetKey, &kernel_data);
zip_asset_store->GetAsBuffer(kSnapshotAssetKey, &snapshot_data);

struct stat stat_result = {};
if (::stat(bundle_path.c_str(), &stat_result) == 0) {
std::unique_ptr<DirectoryAssetBundle> directory_asset_bundle;
// TODO(zarah): Remove usage of zip_asset_store once app.flx is removed.
fxl::RefPtr<ZipAssetStore> zip_asset_store;
// bundle_path is either the path to app.flx or the flutter assets
// directory.
std::string flx_path = bundle_path;
if (S_ISDIR(stat_result.st_mode)) {
directory_asset_bundle =
std::make_unique<DirectoryAssetBundle>(bundle_path);
flx_path = files::GetDirectoryName(bundle_path) + "/app.flx";
}

if (access(flx_path.c_str(), R_OK) == 0) {
zip_asset_store = fxl::MakeRefCounted<ZipAssetStore>(
GetUnzipperProviderForPath(flx_path));
}
GetAssetAsBuffer(kKernelAssetKey, &kernel_data, directory_asset_bundle,
zip_asset_store);
GetAssetAsBuffer(kSnapshotAssetKey, &snapshot_data,
directory_asset_bundle, zip_asset_store);
}
}
}

Expand Down
44 changes: 24 additions & 20 deletions shell/common/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,17 @@ void Engine::Init(const std::string& bundle_path) {
#error Unknown OS
#endif

std::string flx_path = bundle_path;
struct stat stat_result = {};
if (::stat(flx_path.c_str(), &stat_result) == 0) {
if (S_ISDIR(stat_result.st_mode)) {
flx_path = files::GetDirectoryName(bundle_path) + "/app.flx";
}
}

blink::InitRuntime(vm_snapshot_data, vm_snapshot_instr,
default_isolate_snapshot_data,
default_isolate_snapshot_instr, bundle_path);
default_isolate_snapshot_instr, flx_path);
}

const std::string Engine::main_entrypoint_ = "main";
Expand Down Expand Up @@ -476,27 +484,26 @@ void Engine::SetSemanticsEnabled(bool enabled) {
void Engine::ConfigureAssetBundle(const std::string& path) {
struct stat stat_result = {};

directory_asset_bundle_.reset();
// TODO(abarth): We should reset asset_store_ as well, but that might break
// TODO(abarth): We should reset directory_asset_bundle_, but that might break
// custom font loading in hot reload.

if (::stat(path.c_str(), &stat_result) != 0) {
FXL_LOG(INFO) << "Could not configure asset bundle at path: " << path;
return;
}

std::string flx_path;
if (S_ISDIR(stat_result.st_mode)) {
directory_asset_bundle_ =
std::make_unique<blink::DirectoryAssetBundle>(path);
return;
fxl::MakeRefCounted<blink::DirectoryAssetBundle>(path);
flx_path = files::GetDirectoryName(path) + "/app.flx";
} else if (S_ISREG(stat_result.st_mode)) {
flx_path = path;
}

if (S_ISREG(stat_result.st_mode)) {
if (PathExists(flx_path)) {
asset_store_ = fxl::MakeRefCounted<blink::ZipAssetStore>(
blink::GetUnzipperProviderForPath(path));
directory_asset_bundle_ = std::make_unique<blink::DirectoryAssetBundle>(
files::GetDirectoryName(path));
return;
blink::GetUnzipperProviderForPath(flx_path));
}
}

Expand All @@ -520,11 +527,11 @@ void Engine::DidCreateMainIsolate(Dart_Isolate isolate) {
blink::TestFontSelector::Install();
if (!blink::Settings::Get().using_blink)
blink::FontCollection::ForProcess().RegisterTestFonts();
} else if (asset_store_) {
blink::AssetFontSelector::Install(asset_store_);
} else if (directory_asset_bundle_) {
blink::AssetFontSelector::Install(directory_asset_bundle_);
if (!blink::Settings::Get().using_blink) {
blink::FontCollection::ForProcess().RegisterFontsFromAssetStore(
asset_store_);
blink::FontCollection::ForProcess().RegisterFontsFromDirectoryAssetBundle(
directory_asset_bundle_);
}
}
}
Expand Down Expand Up @@ -595,12 +602,9 @@ void Engine::HandleAssetPlatformMessage(
const auto& data = message->data();
std::string asset_name(reinterpret_cast<const char*>(data.data()),
data.size());
std::vector<uint8_t> asset_data;
if (GetAssetAsBuffer(asset_name, &asset_data)) {
response->Complete(std::move(asset_data));
} else {
response->CompleteEmpty();
}
std::string asset_path = directory_asset_bundle_->GetPathForAsset(asset_name);
response->Complete(
std::vector<uint8_t>(asset_path.begin(), asset_path.end()));
}

bool Engine::GetAssetAsBuffer(const std::string& name,
Expand Down
4 changes: 2 additions & 2 deletions shell/common/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ class Engine : public blink::RuntimeDelegate {
std::string country_code_;
std::string user_settings_data_;
bool semantics_enabled_ = false;
// TODO(abarth): Unify these two behind a common interface.
// TODO(zarah): Remove usage of asset_store_ once app.flx is removed.
fxl::RefPtr<blink::ZipAssetStore> asset_store_;
std::unique_ptr<blink::DirectoryAssetBundle> directory_asset_bundle_;
fxl::RefPtr<blink::DirectoryAssetBundle> directory_asset_bundle_;
// TODO(eseidel): This should move into an AnimatorStateMachine.
bool activity_running_;
bool have_surface_;
Expand Down
3 changes: 3 additions & 0 deletions shell/common/switches.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ DEF_SWITCH(EnableTxt,
"enable-txt",
"Enable libtxt as the text shaping library instead of Blink.")
DEF_SWITCH(FLX, "flx", "Specify the FLX path.")
DEF_SWITCH(FlutterAssetsDir,
"flutter-assets-dir",
"Path to the Flutter assets directory.")
DEF_SWITCH(Help, "help", "Display this help text.")
DEF_SWITCH(LogTag, "log-tag", "Tag associated with log messages.")
DEF_SWITCH(MainDartFile, "dart-main", "The path to the main Dart file.")
Expand Down
8 changes: 7 additions & 1 deletion shell/platform/android/io/flutter/view/FlutterMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class FlutterMain {
private static final String AOT_ISOLATE_SNAPSHOT_INSTR_KEY = "isolate-snapshot-instr";
private static final String FLX_KEY = "flx";
private static final String SNAPSHOT_BLOB_KEY = "snapshot-blob";
private static final String FLUTTER_ASSETS_DIR_KEY = "flutter-assets-dir";

// XML Attribute keys supported in AndroidManifest.xml
public static final String PUBLIC_AOT_AOT_SHARED_LIBRARY_PATH =
Expand All @@ -54,6 +55,8 @@ public class FlutterMain {
FlutterMain.class.getName() + '.' + FLX_KEY;
public static final String PUBLIC_SNAPSHOT_BLOB_KEY =
FlutterMain.class.getName() + '.' + SNAPSHOT_BLOB_KEY;
public static final String PUBLIC_FLUTTER_ASSETS_DIR_KEY =
FlutterMain.class.getName() + '.' + FLUTTER_ASSETS_DIR_KEY;

// Resource names used for components of the precompiled snapshot.
private static final String DEFAULT_AOT_SHARED_LIBRARY_PATH= "app.so";
Expand All @@ -63,6 +66,7 @@ public class FlutterMain {
private static final String DEFAULT_AOT_ISOLATE_SNAPSHOT_INSTR = "isolate_snapshot_instr";
private static final String DEFAULT_FLX = "app.flx";
private static final String DEFAULT_SNAPSHOT_BLOB = "snapshot_blob.bin";
private static final String DEFAULT_FLUTTER_ASSETS_DIR = "flutter_assets";

private static final String MANIFEST = "flutter.yaml";

Expand All @@ -79,6 +83,7 @@ public class FlutterMain {
private static String sAotIsolateSnapshotInstr = DEFAULT_AOT_ISOLATE_SNAPSHOT_INSTR;
private static String sFlx = DEFAULT_FLX;
private static String sSnapshotBlob = DEFAULT_SNAPSHOT_BLOB;
private static String sFlutterAssetsDir = DEFAULT_FLUTTER_ASSETS_DIR;

private static boolean sInitialized = false;
private static ResourceExtractor sResourceExtractor;
Expand Down Expand Up @@ -239,6 +244,7 @@ private static void initResources(Context applicationContext) {
sResourceExtractor = new ResourceExtractor(context)
.addResources(SKY_RESOURCES)
.addResource(sFlx);
.addResource(sFlutterAssetsDir)
if (sIsPrecompiledAsSharedLibrary) {
sResourceExtractor
.addResource(sAotSharedLibraryPath);
Expand Down Expand Up @@ -290,7 +296,7 @@ public static boolean isRunningPrecompiledCode() {

public static String findAppBundlePath(Context applicationContext) {
String dataDirectory = PathUtils.getDataDirectory(applicationContext);
File appBundle = new File(dataDirectory, sFlx);
File appBundle = new File(dataDirectory, sFlutterAssetsDir);
return appBundle.exists() ? appBundle.getPath() : null;
}
}
Loading