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
5 changes: 1 addition & 4 deletions assets/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ source_set("assets") {
"directory_asset_bundle.h",
]

deps = [
"//flutter/common",
"//flutter/fml",
]
deps = [ "//flutter/fml" ]

public_configs = [ "//flutter:config" ]
}
6 changes: 3 additions & 3 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ FILE: ../../../flutter/flow/matrix_decomposition_unittests.cc
FILE: ../../../flutter/flow/mutators_stack_unittests.cc
FILE: ../../../flutter/flow/paint_utils.cc
FILE: ../../../flutter/flow/paint_utils.h
FILE: ../../../flutter/flow/persistent_cache.cc
FILE: ../../../flutter/flow/persistent_cache.h
FILE: ../../../flutter/flow/persistent_cache_unittests.cc
FILE: ../../../flutter/flow/raster_cache.cc
FILE: ../../../flutter/flow/raster_cache.h
FILE: ../../../flutter/flow/raster_cache_key.cc
Expand Down Expand Up @@ -607,9 +610,6 @@ FILE: ../../../flutter/shell/common/fixtures/shelltest_screenshot.png
FILE: ../../../flutter/shell/common/input_events_unittests.cc
FILE: ../../../flutter/shell/common/isolate_configuration.cc
FILE: ../../../flutter/shell/common/isolate_configuration.h
FILE: ../../../flutter/shell/common/persistent_cache.cc
FILE: ../../../flutter/shell/common/persistent_cache.h
FILE: ../../../flutter/shell/common/persistent_cache_unittests.cc
FILE: ../../../flutter/shell/common/pipeline.cc
FILE: ../../../flutter/shell/common/pipeline.h
FILE: ../../../flutter/shell/common/pipeline_unittests.cc
Expand Down
7 changes: 6 additions & 1 deletion flow/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ source_set_maybe_fuchsia_legacy("flow") {
"matrix_decomposition.h",
"paint_utils.cc",
"paint_utils.h",
"persistent_cache.cc",
"persistent_cache.h",
"raster_cache.cc",
"raster_cache.h",
"raster_cache_key.cc",
Expand All @@ -73,8 +75,9 @@ source_set_maybe_fuchsia_legacy("flow") {
public_configs = [ "//flutter:config" ]

deps = [
"//flutter/common",
"//flutter/assets",
"//flutter/fml",
"//third_party/rapidjson",
"//third_party/skia",
]

Expand Down Expand Up @@ -151,6 +154,7 @@ if (enable_unittests) {
"layers/transform_layer_unittests.cc",
"matrix_decomposition_unittests.cc",
"mutators_stack_unittests.cc",
"persistent_cache_unittests.cc",
"raster_cache_unittests.cc",
"rtree_unittests.cc",
"skia_gpu_object_unittests.cc",
Expand All @@ -161,6 +165,7 @@ if (enable_unittests) {

deps = [
":flow_fixtures",
"//flutter/assets",
"//flutter/fml",
"//flutter/testing:skia",
"//flutter/testing:testing_lib",
Expand Down
79 changes: 45 additions & 34 deletions shell/common/persistent_cache.cc → flow/persistent_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,27 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "flutter/shell/common/persistent_cache.h"
#include "flutter/flow/persistent_cache.h"

#include <future>
#include <memory>
#include <string>
#include <string_view>

#include "rapidjson/document.h"
#include "third_party/skia/include/utils/SkBase64.h"

#include "flutter/fml/base32.h"
#include "flutter/fml/file.h"
#include "flutter/fml/logging.h"
#include "flutter/fml/make_copyable.h"
#include "flutter/fml/mapping.h"
#include "flutter/fml/paths.h"
#include "flutter/fml/trace_event.h"
#include "flutter/shell/version/version.h"
#include "third_party/rapidjson/include/rapidjson/document.h"
#include "third_party/skia/include/utils/SkBase64.h"

namespace flutter {

std::string PersistentCache::cache_base_path_;
std::string PersistentCache::cache_version_;

std::shared_ptr<AssetManager> PersistentCache::asset_manager_;

Expand Down Expand Up @@ -69,16 +68,26 @@ PersistentCache* PersistentCache::GetCacheForProcess() {
return gPersistentCache.get();
}

void PersistentCache::ClearCacheForProcess() {
std::scoped_lock lock(instance_mutex_);
gPersistentCache.reset(nullptr);
strategy_set_ = false;
}

void PersistentCache::ResetCacheForProcess() {
std::scoped_lock lock(instance_mutex_);
gPersistentCache.reset(new PersistentCache(gIsReadOnly));
strategy_set_ = false;
}

void PersistentCache::SetCacheDirectoryPath(std::string path) {
void PersistentCache::SetCacheDirectoryPath(const std::string& path) {
cache_base_path_ = path;
}

void PersistentCache::SetCacheVersion(const std::string& version) {
cache_version_ = version;
}

bool PersistentCache::Purge() {
// Make sure that this is called after the worker task runner setup so all the
// file system modifications would happen on that single thread to avoid
Expand All @@ -100,31 +109,29 @@ bool PersistentCache::Purge() {

namespace {

constexpr char kEngineComponent[] = "flutter_engine";

static void FreeOldCacheDirectory(const fml::UniqueFD& cache_base_dir) {
fml::UniqueFD engine_dir =
fml::OpenDirectoryReadOnly(cache_base_dir, kEngineComponent);
if (!engine_dir.is_valid()) {
return;
}
fml::VisitFiles(engine_dir, [](const fml::UniqueFD& directory,
const std::string& filename) {
if (filename != GetFlutterEngineVersion()) {
auto dir = fml::OpenDirectory(directory, filename.c_str(), false,
fml::FilePermission::kReadWrite);
if (dir.is_valid()) {
fml::RemoveDirectoryRecursively(directory, filename.c_str());
}
}
return true;
});
static void FreeOldCacheDirectory(const fml::UniqueFD& cache_base_dir,
const std::string& cache_version) {
fml::VisitFiles(
cache_base_dir, [cache_version](const fml::UniqueFD& directory,
const std::string& filename) {
if (filename != cache_version) {
auto dir = fml::OpenDirectory(directory, filename.c_str(), false,
fml::FilePermission::kReadWrite);
if (dir.is_valid()) {
fml::RemoveDirectoryRecursively(directory, filename.c_str());
}
}
return true;
});
}

static std::shared_ptr<fml::UniqueFD> MakeCacheDirectory(
const std::string& global_cache_base_path,
const std::string& cache_version,
bool read_only,
bool cache_sksl) {
FML_CHECK(cache_version.length()) << "Cache version unspecified";

fml::UniqueFD cache_base_dir;
if (global_cache_base_path.length()) {
cache_base_dir = fml::OpenDirectory(global_cache_base_path.c_str(), false,
Expand All @@ -134,9 +141,8 @@ static std::shared_ptr<fml::UniqueFD> MakeCacheDirectory(
}

if (cache_base_dir.is_valid()) {
FreeOldCacheDirectory(cache_base_dir);
std::vector<std::string> components = {
kEngineComponent, GetFlutterEngineVersion(), "skia", GetSkiaVersion()};
FreeOldCacheDirectory(cache_base_dir, cache_version);
std::vector<std::string> components = {cache_version};
if (cache_sksl) {
components.push_back(PersistentCache::kSkSLSubdirName);
}
Expand Down Expand Up @@ -198,9 +204,9 @@ std::vector<PersistentCache::SkSLCache> PersistentCache::LoadSkSLs() {
mapping = asset_manager_->GetAsMapping(kAssetFileName);
}
if (mapping == nullptr) {
FML_LOG(INFO) << "No sksl asset found.";
FML_DLOG(INFO) << "No sksl asset found.";
} else {
FML_LOG(INFO) << "Found sksl asset. Loading SkSLs from it...";
FML_DLOG(INFO) << "Found sksl asset. Loading SkSLs from it...";
rapidjson::Document json_doc;
rapidjson::ParseResult parse_result =
json_doc.Parse(reinterpret_cast<const char*>(mapping->GetMapping()),
Expand All @@ -225,9 +231,14 @@ std::vector<PersistentCache::SkSLCache> PersistentCache::LoadSkSLs() {

PersistentCache::PersistentCache(bool read_only)
: is_read_only_(read_only),
cache_directory_(MakeCacheDirectory(cache_base_path_, read_only, false)),
sksl_cache_directory_(
MakeCacheDirectory(cache_base_path_, read_only, true)) {
cache_directory_(MakeCacheDirectory(cache_base_path_,
cache_version_,
read_only,
false)),
sksl_cache_directory_(MakeCacheDirectory(cache_base_path_,
cache_version_,
read_only,
true)) {
if (!IsValid()) {
FML_LOG(WARNING) << "Could not acquire the persistent cache directory. "
"Caching of GPU resources on disk is disabled.";
Expand Down Expand Up @@ -341,7 +352,7 @@ void PersistentCache::DumpSkp(const SkData& data) {
auto ticks = fml::TimePoint::Now().ToEpochDelta().ToNanoseconds();
name_stream << "shader_dump_" << std::to_string(ticks) << ".skp";
std::string file_name = name_stream.str();
FML_LOG(INFO) << "Dumping " << file_name;
FML_DLOG(INFO) << "Dumping " << file_name;
auto mapping = std::make_unique<fml::DataMapping>(
std::vector<uint8_t>{data.bytes(), data.bytes() + data.size()});
PersistentCacheStore(GetWorkerTaskRunner(), cache_directory_,
Expand Down
16 changes: 11 additions & 5 deletions shell/common/persistent_cache.h → flow/persistent_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef FLUTTER_SHELL_COMMON_PERSISTENT_CACHE_H_
#define FLUTTER_SHELL_COMMON_PERSISTENT_CACHE_H_
#ifndef FLUTTER_FLOW_PERSISTENT_CACHE_H_
#define FLUTTER_FLOW_PERSISTENT_CACHE_H_

#include <memory>
#include <mutex>
Expand Down Expand Up @@ -31,11 +31,16 @@ class PersistentCache : public GrContextOptions::PersistentCache {
static bool gIsReadOnly;

static PersistentCache* GetCacheForProcess();
static void ResetCacheForProcess();
static void ClearCacheForProcess();
static void ResetCacheForProcess(); // Rec-creates the cache.

// This must be called before |GetCacheForProcess|. Otherwise, it won't
// affect the cache directory returned by |GetCacheForProcess|.
static void SetCacheDirectoryPath(std::string path);
static void SetCacheDirectoryPath(const std::string& path);

// This must be called before |GetCacheForProcess|. Otherwise, it won't
// affect the cache directory returned by |GetCacheForProcess|.
static void SetCacheVersion(const std::string& version);

// Convert a binary SkData key into a Base32 encoded string.
//
Expand Down Expand Up @@ -83,6 +88,7 @@ class PersistentCache : public GrContextOptions::PersistentCache {

private:
static std::string cache_base_path_;
static std::string cache_version_;

static std::shared_ptr<AssetManager> asset_manager_;

Expand Down Expand Up @@ -125,4 +131,4 @@ class PersistentCache : public GrContextOptions::PersistentCache {

} // namespace flutter

#endif // FLUTTER_SHELL_COMMON_PERSISTENT_CACHE_H_
#endif // FLUTTER_FLOW_PERSISTENT_CACHE_H_
Loading