Skip to content

Commit

Permalink
AssetResolver updating in AssetManager for Dynamic features (flutter#…
Browse files Browse the repository at this point in the history
  • Loading branch information
GaryQian authored Dec 24, 2020
1 parent 596bae2 commit 1c975f1
Show file tree
Hide file tree
Showing 22 changed files with 387 additions and 56 deletions.
29 changes: 29 additions & 0 deletions assets/asset_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ void AssetManager::PushBack(std::unique_ptr<AssetResolver> resolver) {
resolvers_.push_back(std::move(resolver));
}

void AssetManager::UpdateResolverByType(
std::unique_ptr<AssetResolver> updated_asset_resolver,
AssetResolver::AssetResolverType type) {
if (updated_asset_resolver == nullptr) {
return;
}
bool updated = false;
std::deque<std::unique_ptr<AssetResolver>> new_resolvers;
for (auto& old_resolver : resolvers_) {
if (!updated && old_resolver->GetType() == type) {
// Push the replacement updated resolver in place of the old_resolver.
new_resolvers.push_back(std::move(updated_asset_resolver));
updated = true;
} else {
new_resolvers.push_back(std::move(old_resolver));
}
}
// Append resolver to the end if not used as a replacement.
if (!updated) {
new_resolvers.push_back(std::move(updated_asset_resolver));
}
resolvers_.swap(new_resolvers);
}

std::deque<std::unique_ptr<AssetResolver>> AssetManager::TakeResolvers() {
return std::move(resolvers_);
}
Expand Down Expand Up @@ -79,4 +103,9 @@ bool AssetManager::IsValidAfterAssetManagerChange() const {
return false;
}

// |AssetResolver|
AssetResolver::AssetResolverType AssetManager::GetType() const {
return AssetResolverType::kAssetManager;
}

} // namespace flutter
31 changes: 31 additions & 0 deletions assets/asset_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,34 @@ class AssetManager final : public AssetResolver {

void PushBack(std::unique_ptr<AssetResolver> resolver);

//--------------------------------------------------------------------------
/// @brief Replaces an asset resolver of the specified `type` with
/// `updated_asset_resolver`. The matching AssetResolver is
/// removed and replaced with `updated_asset_resolvers`.
///
/// AssetResolvers should be updated when the existing resolver
/// becomes obsolete and a newer one becomes available that
/// provides updated access to the same type of assets as the
/// existing one. This update process is meant to be performed
/// at runtime.
///
/// If a null resolver is provided, nothing will be done. If no
/// matching resolver is found, the provided resolver will be
/// added to the end of the AssetManager resolvers queue. The
/// replacement only occurs with the first matching resolver.
/// Any additional matching resolvers are untouched.
///
/// @param[in] updated_asset_resolver The asset resolver to replace the
/// resolver of matching type with.
///
/// @param[in] type The type of AssetResolver to update. Only resolvers of
/// the specified type will be replaced by the updated
/// resolver.
///
void UpdateResolverByType(
std::unique_ptr<AssetResolver> updated_asset_resolver,
AssetResolver::AssetResolverType type);

std::deque<std::unique_ptr<AssetResolver>> TakeResolvers();

// |AssetResolver|
Expand All @@ -33,6 +61,9 @@ class AssetManager final : public AssetResolver {
// |AssetResolver|
bool IsValidAfterAssetManagerChange() const override;

// |AssetResolver|
AssetResolver::AssetResolverType GetType() const override;

// |AssetResolver|
std::unique_ptr<fml::Mapping> GetAsMapping(
const std::string& asset_name) const override;
Expand Down
17 changes: 17 additions & 0 deletions assets/asset_resolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ class AssetResolver {

virtual ~AssetResolver() = default;

//----------------------------------------------------------------------------
/// @brief Identifies the type of AssetResolver an instance is.
///
enum AssetResolverType {
kAssetManager,
kApkAssetProvider,
kDirectoryAssetBundle
};

virtual bool IsValid() const = 0;

//----------------------------------------------------------------------------
Expand All @@ -39,6 +48,14 @@ class AssetResolver {
///
virtual bool IsValidAfterAssetManagerChange() const = 0;

//----------------------------------------------------------------------------
/// @brief Gets the type of AssetResolver this is. Types are defined in
/// AssetResolverType.
///
/// @return Returns the AssetResolverType that this resolver is.
///
virtual AssetResolverType GetType() const = 0;

[[nodiscard]] virtual std::unique_ptr<fml::Mapping> GetAsMapping(
const std::string& asset_name) const = 0;

Expand Down
5 changes: 5 additions & 0 deletions assets/directory_asset_bundle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ bool DirectoryAssetBundle::IsValidAfterAssetManagerChange() const {
return is_valid_after_asset_manager_change_;
}

// |AssetResolver|
AssetResolver::AssetResolverType DirectoryAssetBundle::GetType() const {
return AssetResolver::AssetResolverType::kDirectoryAssetBundle;
}

// |AssetResolver|
std::unique_ptr<fml::Mapping> DirectoryAssetBundle::GetAsMapping(
const std::string& asset_name) const {
Expand Down
3 changes: 3 additions & 0 deletions assets/directory_asset_bundle.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class DirectoryAssetBundle : public AssetResolver {
// |AssetResolver|
bool IsValidAfterAssetManagerChange() const override;

// |AssetResolver|
AssetResolver::AssetResolverType GetType() const override;

// |AssetResolver|
std::unique_ptr<fml::Mapping> GetAsMapping(
const std::string& asset_name) const override;
Expand Down
7 changes: 5 additions & 2 deletions shell/common/platform_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ void PlatformView::LoadDartDeferredLibraryError(intptr_t loading_unit_id,
const std::string error_message,
bool transient) {}

void PlatformView::UpdateAssetManager(
std::shared_ptr<AssetManager> asset_manager) {}
void PlatformView::UpdateAssetResolverByType(
std::unique_ptr<AssetResolver> updated_asset_resolver,
AssetResolver::AssetResolverType type) {
delegate_.UpdateAssetResolverByType(std::move(updated_asset_resolver), type);
}

} // namespace flutter
69 changes: 54 additions & 15 deletions shell/common/platform_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,34 @@ class PlatformView {
const std::string error_message,
bool transient) = 0;

// TODO(garyq): Implement a proper asset_resolver replacement instead of
// overwriting the entire asset manager.
//--------------------------------------------------------------------------
/// @brief Sets the asset manager of the engine to asset_manager
///
/// @param[in] asset_manager The asset manager to use.
///
virtual void UpdateAssetManager(
std::shared_ptr<AssetManager> asset_manager) = 0;
/// @brief Replaces the asset resolver handled by the engine's
/// AssetManager of the specified `type` with
/// `updated_asset_resolver`. The matching AssetResolver is
/// removed and replaced with `updated_asset_resolvers`.
///
/// AssetResolvers should be updated when the existing resolver
/// becomes obsolete and a newer one becomes available that
/// provides updated access to the same type of assets as the
/// existing one. This update process is meant to be performed
/// at runtime.
///
/// If a null resolver is provided, nothing will be done. If no
/// matching resolver is found, the provided resolver will be
/// added to the end of the AssetManager resolvers queue. The
/// replacement only occurs with the first matching resolver.
/// Any additional matching resolvers are untouched.
///
/// @param[in] updated_asset_resolver The asset resolver to replace the
/// resolver of matching type with.
///
/// @param[in] type The type of AssetResolver to update. Only resolvers of
/// the specified type will be replaced by the updated
/// resolver.
///
virtual void UpdateAssetResolverByType(
std::unique_ptr<AssetResolver> updated_asset_resolver,
AssetResolver::AssetResolverType type) = 0;
};

//----------------------------------------------------------------------------
Expand Down Expand Up @@ -720,14 +739,34 @@ class PlatformView {
const std::string error_message,
bool transient);

// TODO(garyq): Implement a proper asset_resolver replacement instead of
// overwriting the entire asset manager.
//--------------------------------------------------------------------------
/// @brief Sets the asset manager of the engine to asset_manager
///
/// @param[in] asset_manager The asset manager to use.
///
virtual void UpdateAssetManager(std::shared_ptr<AssetManager> asset_manager);
/// @brief Replaces the asset resolver handled by the engine's
/// AssetManager of the specified `type` with
/// `updated_asset_resolver`. The matching AssetResolver is
/// removed and replaced with `updated_asset_resolvers`.
///
/// AssetResolvers should be updated when the existing resolver
/// becomes obsolete and a newer one becomes available that
/// provides updated access to the same type of assets as the
/// existing one. This update process is meant to be performed
/// at runtime.
///
/// If a null resolver is provided, nothing will be done. If no
/// matching resolver is found, the provided resolver will be
/// added to the end of the AssetManager resolvers queue. The
/// replacement only occurs with the first matching resolver.
/// Any additional matching resolvers are untouched.
///
/// @param[in] updated_asset_resolver The asset resolver to replace the
/// resolver of matching type with.
///
/// @param[in] type The type of AssetResolver to update. Only resolvers of
/// the specified type will be replaced by the updated
/// resolver.
///
virtual void UpdateAssetResolverByType(
std::unique_ptr<AssetResolver> updated_asset_resolver,
AssetResolver::AssetResolverType type);

protected:
PlatformView::Delegate& delegate_;
Expand Down
7 changes: 5 additions & 2 deletions shell/common/shell.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1225,8 +1225,11 @@ void Shell::LoadDartDeferredLibraryError(intptr_t loading_unit_id,
transient);
}

void Shell::UpdateAssetManager(std::shared_ptr<AssetManager> asset_manager) {
engine_->UpdateAssetManager(std::move(asset_manager));
void Shell::UpdateAssetResolverByType(
std::unique_ptr<AssetResolver> updated_asset_resolver,
AssetResolver::AssetResolverType type) {
engine_->GetAssetManager()->UpdateResolverByType(
std::move(updated_asset_resolver), type);
}

// |Engine::Delegate|
Expand Down
4 changes: 3 additions & 1 deletion shell/common/shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,9 @@ class Shell final : public PlatformView::Delegate,
bool transient) override;

// |PlatformView::Delegate|
void UpdateAssetManager(std::shared_ptr<AssetManager> asset_manager) override;
void UpdateAssetResolverByType(
std::unique_ptr<AssetResolver> updated_asset_resolver,
AssetResolver::AssetResolverType type) override;

// |Animator::Delegate|
void OnAnimatorBeginFrame(fml::TimePoint frame_target_time) override;
Expand Down
Loading

0 comments on commit 1c975f1

Please sign in to comment.