Reland Skia Caching improvements (flutter/engine#10434)
This commit is contained in:
@@ -164,6 +164,7 @@ if (current_toolchain == host_toolchain) {
|
||||
":shell_unittests_gpu_configuration",
|
||||
"$flutter_root/common",
|
||||
"$flutter_root/flow",
|
||||
"$flutter_root/lib/ui:ui",
|
||||
"$flutter_root/shell",
|
||||
"$flutter_root/testing:dart",
|
||||
"$flutter_root/testing:opengl",
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:convert' show utf8;
|
||||
import 'dart:isolate';
|
||||
import 'dart:typed_data';
|
||||
import 'dart:ui';
|
||||
|
||||
void main() {}
|
||||
@@ -57,3 +59,19 @@ void testCanLaunchSecondaryIsolate() {
|
||||
Isolate.spawn(secondaryIsolateMain, 'Hello from root isolate.');
|
||||
notifyNative();
|
||||
}
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
void testSkiaResourceCacheSendsResponse() {
|
||||
final PlatformMessageResponseCallback callback = (ByteData data) {
|
||||
notifyNative();
|
||||
};
|
||||
const String json = '''{
|
||||
"method": "Skia.setResourceCacheMaxBytes",
|
||||
"args": 10000
|
||||
}''';
|
||||
window.sendPlatformMessage(
|
||||
'flutter/skia',
|
||||
Uint8List.fromList(utf8.encode(json)).buffer.asByteData(),
|
||||
callback,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -43,6 +43,7 @@ Rasterizer::Rasterizer(
|
||||
: delegate_(delegate),
|
||||
task_runners_(std::move(task_runners)),
|
||||
compositor_context_(std::move(compositor_context)),
|
||||
user_override_resource_cache_bytes_(false),
|
||||
weak_factory_(this) {
|
||||
FML_DCHECK(compositor_context_);
|
||||
}
|
||||
@@ -55,6 +56,10 @@ fml::WeakPtr<Rasterizer> Rasterizer::GetWeakPtr() const {
|
||||
|
||||
void Rasterizer::Setup(std::unique_ptr<Surface> surface) {
|
||||
surface_ = std::move(surface);
|
||||
if (max_cache_bytes_.has_value()) {
|
||||
SetResourceCacheMaxBytes(max_cache_bytes_.value(),
|
||||
user_override_resource_cache_bytes_);
|
||||
}
|
||||
compositor_context_->OnGrContextCreated();
|
||||
}
|
||||
|
||||
@@ -355,7 +360,20 @@ void Rasterizer::FireNextFrameCallbackIfPresent() {
|
||||
callback();
|
||||
}
|
||||
|
||||
void Rasterizer::SetResourceCacheMaxBytes(int max_bytes) {
|
||||
void Rasterizer::SetResourceCacheMaxBytes(size_t max_bytes, bool from_user) {
|
||||
user_override_resource_cache_bytes_ |= from_user;
|
||||
|
||||
if (!from_user && user_override_resource_cache_bytes_) {
|
||||
// We should not update the setting here if a user has explicitly set a
|
||||
// value for this over the flutter/skia channel.
|
||||
return;
|
||||
}
|
||||
|
||||
max_cache_bytes_ = max_bytes;
|
||||
if (!surface_) {
|
||||
return;
|
||||
}
|
||||
|
||||
GrContext* context = surface_->GetContext();
|
||||
if (context) {
|
||||
int max_resources;
|
||||
@@ -364,6 +382,19 @@ void Rasterizer::SetResourceCacheMaxBytes(int max_bytes) {
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<size_t> Rasterizer::GetResourceCacheMaxBytes() const {
|
||||
if (!surface_) {
|
||||
return std::nullopt;
|
||||
}
|
||||
GrContext* context = surface_->GetContext();
|
||||
if (context) {
|
||||
size_t max_bytes;
|
||||
context->getResourceCacheLimits(nullptr, &max_bytes);
|
||||
return max_bytes;
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
Rasterizer::Screenshot::Screenshot() {}
|
||||
|
||||
Rasterizer::Screenshot::Screenshot(sk_sp<SkData> p_data, SkISize p_size)
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#define SHELL_COMMON_RASTERIZER_H_
|
||||
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
#include "flutter/common/settings.h"
|
||||
#include "flutter/common/task_runners.h"
|
||||
@@ -374,8 +375,25 @@ class Rasterizer final {
|
||||
///
|
||||
/// @param[in] max_bytes The maximum byte size of resource that may be
|
||||
/// cached for GPU rendering.
|
||||
/// @param[in] from_user Whether this request was from user code, e.g. via
|
||||
/// the flutter/skia message channel, in which case
|
||||
/// it should not be overridden by the platform.
|
||||
///
|
||||
void SetResourceCacheMaxBytes(int max_bytes);
|
||||
void SetResourceCacheMaxBytes(size_t max_bytes, bool from_user);
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
/// @brief The current value of Skia's resource cache size, if a surface
|
||||
/// is present.
|
||||
///
|
||||
/// @attention This cache does not describe the entirety of GPU resources
|
||||
/// that may be cached. The `RasterCache` also holds very large
|
||||
/// GPU resources.
|
||||
///
|
||||
/// @see `RasterCache`
|
||||
///
|
||||
/// @return The size of Skia's resource cache, if available.
|
||||
///
|
||||
std::optional<size_t> GetResourceCacheMaxBytes() const;
|
||||
|
||||
private:
|
||||
Delegate& delegate_;
|
||||
@@ -384,6 +402,8 @@ class Rasterizer final {
|
||||
std::unique_ptr<flutter::CompositorContext> compositor_context_;
|
||||
std::unique_ptr<flutter::LayerTree> last_layer_tree_;
|
||||
fml::closure next_frame_callback_;
|
||||
bool user_override_resource_cache_bytes_;
|
||||
std::optional<size_t> max_cache_bytes_;
|
||||
fml::WeakPtrFactory<Rasterizer> weak_factory_;
|
||||
|
||||
void DoDraw(std::unique_ptr<flutter::LayerTree> layer_tree);
|
||||
|
||||
@@ -682,6 +682,16 @@ void Shell::OnPlatformViewSetViewportMetrics(const ViewportMetrics& metrics) {
|
||||
FML_DCHECK(is_setup_);
|
||||
FML_DCHECK(task_runners_.GetPlatformTaskRunner()->RunsTasksOnCurrentThread());
|
||||
|
||||
// This is the formula Android uses.
|
||||
// https://android.googlesource.com/platform/frameworks/base/+/master/libs/hwui/renderthread/CacheManager.cpp#41
|
||||
size_t max_bytes = metrics.physical_width * metrics.physical_height * 12 * 4;
|
||||
task_runners_.GetGPUTaskRunner()->PostTask(
|
||||
[rasterizer = rasterizer_->GetWeakPtr(), max_bytes] {
|
||||
if (rasterizer) {
|
||||
rasterizer->SetResourceCacheMaxBytes(max_bytes, false);
|
||||
}
|
||||
});
|
||||
|
||||
task_runners_.GetUITaskRunner()->PostTask(
|
||||
[engine = engine_->GetWeakPtr(), metrics]() {
|
||||
if (engine) {
|
||||
@@ -939,10 +949,14 @@ void Shell::HandleEngineSkiaMessage(fml::RefPtr<PlatformMessage> message) {
|
||||
return;
|
||||
|
||||
task_runners_.GetGPUTaskRunner()->PostTask(
|
||||
[rasterizer = rasterizer_->GetWeakPtr(),
|
||||
max_bytes = args->value.GetInt()] {
|
||||
[rasterizer = rasterizer_->GetWeakPtr(), max_bytes = args->value.GetInt(),
|
||||
response = std::move(message->response())] {
|
||||
if (rasterizer) {
|
||||
rasterizer->SetResourceCacheMaxBytes(max_bytes);
|
||||
rasterizer->SetResourceCacheMaxBytes(static_cast<size_t>(max_bytes),
|
||||
true);
|
||||
}
|
||||
if (response) {
|
||||
response->CompleteEmpty();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -22,6 +22,21 @@ ShellTest::ShellTest()
|
||||
|
||||
ShellTest::~ShellTest() = default;
|
||||
|
||||
void ShellTest::SendEnginePlatformMessage(
|
||||
Shell* shell,
|
||||
fml::RefPtr<PlatformMessage> message) {
|
||||
fml::AutoResetWaitableEvent latch;
|
||||
fml::TaskRunner::RunNowOrPostTask(
|
||||
shell->GetTaskRunners().GetPlatformTaskRunner(),
|
||||
[shell, &latch, message = std::move(message)]() {
|
||||
if (auto engine = shell->weak_engine_) {
|
||||
engine->HandlePlatformMessage(std::move(message));
|
||||
}
|
||||
latch.Signal();
|
||||
});
|
||||
latch.Wait();
|
||||
}
|
||||
|
||||
void ShellTest::SetSnapshotsAndAssets(Settings& settings) {
|
||||
if (!assets_dir_.is_valid()) {
|
||||
return;
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
#include "flutter/common/settings.h"
|
||||
#include "flutter/fml/macros.h"
|
||||
#include "flutter/lib/ui/window/platform_message.h"
|
||||
#include "flutter/shell/common/run_configuration.h"
|
||||
#include "flutter/shell/common/shell.h"
|
||||
#include "flutter/shell/common/thread_host.h"
|
||||
@@ -32,6 +33,9 @@ class ShellTest : public ThreadTest {
|
||||
TaskRunners task_runners);
|
||||
TaskRunners GetTaskRunnersForFixture();
|
||||
|
||||
void SendEnginePlatformMessage(Shell* shell,
|
||||
fml::RefPtr<PlatformMessage> message);
|
||||
|
||||
void AddNativeCallback(std::string name, Dart_NativeFunction callback);
|
||||
|
||||
static void PlatformViewNotifyCreated(
|
||||
|
||||
@@ -264,11 +264,11 @@ TEST_F(ShellTest, NoNeedToReportTimingsByDefault) {
|
||||
ASSERT_FALSE(GetNeedsReportTimings(shell.get()));
|
||||
|
||||
// This assertion may or may not be the direct result of needs_report_timings_
|
||||
// being false. The count could be 0 simply because we just cleared unreported
|
||||
// timings by reporting them. Hence this can't replace the
|
||||
// ASSERT_FALSE(GetNeedsReportTimings(shell.get())) check. We added this
|
||||
// assertion for an additional confidence that we're not pushing back to
|
||||
// unreported timings unnecessarily.
|
||||
// being false. The count could be 0 simply because we just cleared
|
||||
// unreported timings by reporting them. Hence this can't replace the
|
||||
// ASSERT_FALSE(GetNeedsReportTimings(shell.get())) check. We added
|
||||
// this assertion for an additional confidence that we're not pushing
|
||||
// back to unreported timings unnecessarily.
|
||||
//
|
||||
// Conversely, do not assert UnreportedTimingsCount(shell.get()) to be
|
||||
// positive in any tests. Otherwise those tests will be flaky as the clearing
|
||||
@@ -600,5 +600,124 @@ TEST_F(ShellTest, WaitForFirstFrameInlined) {
|
||||
ASSERT_FALSE(event.WaitWithTimeout(fml::TimeDelta::FromMilliseconds(1000)));
|
||||
}
|
||||
|
||||
TEST_F(ShellTest, SetResourceCacheSize) {
|
||||
Settings settings = CreateSettingsForFixture();
|
||||
auto task_runner = GetThreadTaskRunner();
|
||||
TaskRunners task_runners("test", task_runner, task_runner, task_runner,
|
||||
task_runner);
|
||||
std::unique_ptr<Shell> shell =
|
||||
CreateShell(std::move(settings), std::move(task_runners));
|
||||
|
||||
// Create the surface needed by rasterizer
|
||||
PlatformViewNotifyCreated(shell.get());
|
||||
|
||||
auto configuration = RunConfiguration::InferFromSettings(settings);
|
||||
configuration.SetEntrypoint("emptyMain");
|
||||
|
||||
RunEngine(shell.get(), std::move(configuration));
|
||||
PumpOneFrame(shell.get());
|
||||
|
||||
EXPECT_EQ(shell->GetRasterizer()->GetResourceCacheMaxBytes().value_or(0U),
|
||||
static_cast<size_t>(24 * (1 << 20)));
|
||||
|
||||
fml::TaskRunner::RunNowOrPostTask(
|
||||
shell->GetTaskRunners().GetPlatformTaskRunner(), [&shell]() {
|
||||
shell->GetPlatformView()->SetViewportMetrics(
|
||||
{1.0, 400, 200, 0, 0, 0, 0, 0, 0, 0, 0});
|
||||
});
|
||||
PumpOneFrame(shell.get());
|
||||
|
||||
EXPECT_EQ(shell->GetRasterizer()->GetResourceCacheMaxBytes().value_or(0U),
|
||||
3840000U);
|
||||
|
||||
std::string request_json = R"json({
|
||||
"method": "Skia.setResourceCacheMaxBytes",
|
||||
"args": 10000
|
||||
})json";
|
||||
std::vector<uint8_t> data(request_json.begin(), request_json.end());
|
||||
auto platform_message = fml::MakeRefCounted<PlatformMessage>(
|
||||
"flutter/skia", std::move(data), nullptr);
|
||||
SendEnginePlatformMessage(shell.get(), std::move(platform_message));
|
||||
PumpOneFrame(shell.get());
|
||||
EXPECT_EQ(shell->GetRasterizer()->GetResourceCacheMaxBytes().value_or(0U),
|
||||
10000U);
|
||||
|
||||
fml::TaskRunner::RunNowOrPostTask(
|
||||
shell->GetTaskRunners().GetPlatformTaskRunner(), [&shell]() {
|
||||
shell->GetPlatformView()->SetViewportMetrics(
|
||||
{1.0, 800, 400, 0, 0, 0, 0, 0, 0, 0, 0});
|
||||
});
|
||||
PumpOneFrame(shell.get());
|
||||
|
||||
EXPECT_EQ(shell->GetRasterizer()->GetResourceCacheMaxBytes().value_or(0U),
|
||||
10000U);
|
||||
}
|
||||
|
||||
TEST_F(ShellTest, SetResourceCacheSizeEarly) {
|
||||
Settings settings = CreateSettingsForFixture();
|
||||
auto task_runner = GetThreadTaskRunner();
|
||||
TaskRunners task_runners("test", task_runner, task_runner, task_runner,
|
||||
task_runner);
|
||||
std::unique_ptr<Shell> shell =
|
||||
CreateShell(std::move(settings), std::move(task_runners));
|
||||
|
||||
fml::TaskRunner::RunNowOrPostTask(
|
||||
shell->GetTaskRunners().GetPlatformTaskRunner(), [&shell]() {
|
||||
shell->GetPlatformView()->SetViewportMetrics(
|
||||
{1.0, 400, 200, 0, 0, 0, 0, 0, 0, 0, 0});
|
||||
});
|
||||
PumpOneFrame(shell.get());
|
||||
|
||||
// Create the surface needed by rasterizer
|
||||
PlatformViewNotifyCreated(shell.get());
|
||||
|
||||
auto configuration = RunConfiguration::InferFromSettings(settings);
|
||||
configuration.SetEntrypoint("emptyMain");
|
||||
|
||||
RunEngine(shell.get(), std::move(configuration));
|
||||
PumpOneFrame(shell.get());
|
||||
|
||||
EXPECT_EQ(shell->GetRasterizer()->GetResourceCacheMaxBytes().value_or(0),
|
||||
static_cast<size_t>(3840000U));
|
||||
}
|
||||
|
||||
TEST_F(ShellTest, SetResourceCacheSizeNotifiesDart) {
|
||||
Settings settings = CreateSettingsForFixture();
|
||||
auto task_runner = GetThreadTaskRunner();
|
||||
TaskRunners task_runners("test", task_runner, task_runner, task_runner,
|
||||
task_runner);
|
||||
std::unique_ptr<Shell> shell =
|
||||
CreateShell(std::move(settings), std::move(task_runners));
|
||||
|
||||
fml::TaskRunner::RunNowOrPostTask(
|
||||
shell->GetTaskRunners().GetPlatformTaskRunner(), [&shell]() {
|
||||
shell->GetPlatformView()->SetViewportMetrics(
|
||||
{1.0, 400, 200, 0, 0, 0, 0, 0, 0, 0, 0});
|
||||
});
|
||||
PumpOneFrame(shell.get());
|
||||
|
||||
// Create the surface needed by rasterizer
|
||||
PlatformViewNotifyCreated(shell.get());
|
||||
|
||||
auto configuration = RunConfiguration::InferFromSettings(settings);
|
||||
configuration.SetEntrypoint("testSkiaResourceCacheSendsResponse");
|
||||
|
||||
EXPECT_EQ(shell->GetRasterizer()->GetResourceCacheMaxBytes().value_or(0),
|
||||
static_cast<size_t>(3840000U));
|
||||
|
||||
fml::AutoResetWaitableEvent latch;
|
||||
AddNativeCallback("NotifyNative", CREATE_NATIVE_ENTRY([&latch](auto args) {
|
||||
latch.Signal();
|
||||
}));
|
||||
|
||||
RunEngine(shell.get(), std::move(configuration));
|
||||
PumpOneFrame(shell.get());
|
||||
|
||||
latch.Wait();
|
||||
|
||||
EXPECT_EQ(shell->GetRasterizer()->GetResourceCacheMaxBytes().value_or(0),
|
||||
static_cast<size_t>(10000U));
|
||||
}
|
||||
|
||||
} // namespace testing
|
||||
} // namespace flutter
|
||||
|
||||
@@ -28,7 +28,10 @@ static const int kGrCacheMaxCount = 8192;
|
||||
|
||||
// Default maximum number of bytes of GPU memory of budgeted resources in the
|
||||
// cache.
|
||||
static const size_t kGrCacheMaxByteSize = 512 * (1 << 20);
|
||||
// The shell will dynamically increase or decrease this cache based on the
|
||||
// viewport size, unless a user has specifically requested a size on the Skia
|
||||
// system channel.
|
||||
static const size_t kGrCacheMaxByteSize = 24 * (1 << 20);
|
||||
|
||||
GPUSurfaceGL::GPUSurfaceGL(GPUSurfaceGLDelegate* delegate)
|
||||
: delegate_(delegate), weak_factory_(this) {
|
||||
|
||||
Reference in New Issue
Block a user