diff --git a/engine/src/flutter/common/graphics/persistent_cache.h b/engine/src/flutter/common/graphics/persistent_cache.h index 03dc449742..00b6c299b5 100644 --- a/engine/src/flutter/common/graphics/persistent_cache.h +++ b/engine/src/flutter/common/graphics/persistent_cache.h @@ -95,6 +95,9 @@ class PersistentCache : public GrContextOptions::PersistentCache { /// Set the asset manager from which PersistentCache can load SkLSs. A nullptr /// can be provided to clear the asset manager. static void SetAssetManager(std::shared_ptr value); + static std::shared_ptr asset_manager() { + return asset_manager_; + } static bool cache_sksl() { return cache_sksl_; } diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/engine.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/engine.cc index 34f32c3e87..08080bb0ec 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/engine.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/engine.cc @@ -266,8 +266,8 @@ Engine::Engine(Delegate& delegate, // Setup the callback that will instantiate the platform view. flutter::Shell::CreateCallback on_create_platform_view = fml::MakeCopyable( - [debug_label = thread_label_, view_ref = std::move(platform_view_ref), - runner_services, + [this, debug_label = thread_label_, + view_ref = std::move(platform_view_ref), runner_services, parent_environment_service_provider = std::move(parent_environment_service_provider), session_listener_request = std::move(session_listener_request), @@ -293,7 +293,41 @@ Engine::Engine(Delegate& delegate, await_vsync_for_secondary_callback_callback = [this](FireCallbackCallback cb) { session_connection_->AwaitVsyncForSecondaryCallback(cb); - }](flutter::Shell& shell) mutable { + }, + product_config](flutter::Shell& shell) mutable { + OnShaderWarmup on_shader_warmup = nullptr; + if (product_config.enable_shader_warmup()) { + FML_DCHECK(surface_producer_); + if (product_config.enable_shader_warmup_dart_hooks()) { + on_shader_warmup = + [this, &shell]( + const std::vector& skp_names, + std::function completion_callback, + uint64_t width, uint64_t height) { + WarmupSkps( + shell.GetDartVM() + ->GetConcurrentMessageLoop() + ->GetTaskRunner() + .get(), + shell.GetTaskRunners().GetRasterTaskRunner().get(), + surface_producer_.value(), width, height, + flutter::PersistentCache::GetCacheForProcess() + ->asset_manager(), + skp_names, completion_callback); + }; + } else { + WarmupSkps(shell.GetDartVM() + ->GetConcurrentMessageLoop() + ->GetTaskRunner() + .get(), + shell.GetTaskRunners().GetRasterTaskRunner().get(), + surface_producer_.value(), 1024, 600, + flutter::PersistentCache::GetCacheForProcess() + ->asset_manager(), + std::nullopt, std::nullopt); + } + } + return std::make_unique( shell, // delegate debug_label, // debug label @@ -313,7 +347,8 @@ Engine::Engine(Delegate& delegate, std::move(on_destroy_view_callback), std::move(on_create_surface_callback), std::move(on_semantics_node_update_callback), - std::move(on_request_announce_callback), external_view_embedder, + std::move(on_request_announce_callback), + std::move(on_shader_warmup), external_view_embedder, // Callbacks for VsyncWaiter to call into SessionConnection. await_vsync_callback, await_vsync_for_secondary_callback_callback); @@ -322,22 +357,12 @@ Engine::Engine(Delegate& delegate, // Setup the callback that will instantiate the rasterizer. flutter::Shell::CreateCallback on_create_rasterizer; #if defined(LEGACY_FUCHSIA_EMBEDDER) - on_create_rasterizer = [this, &product_config](flutter::Shell& shell) { + on_create_rasterizer = [this](flutter::Shell& shell) { if (use_legacy_renderer_) { FML_DCHECK(session_connection_); FML_DCHECK(surface_producer_); FML_DCHECK(legacy_external_view_embedder_); - if (product_config.enable_shader_warmup()) { - FML_DCHECK(surface_producer_); - WarmupSkps(shell.GetDartVM() - ->GetConcurrentMessageLoop() - ->GetTaskRunner() - .get(), - shell.GetTaskRunners().GetRasterTaskRunner().get(), - surface_producer_.value()); - } - auto compositor_context = std::make_unique( *(session_connection_.get()), surface_producer_.value(), @@ -345,27 +370,11 @@ Engine::Engine(Delegate& delegate, return std::make_unique( shell, std::move(compositor_context)); } else { - if (product_config.enable_shader_warmup()) { - FML_DCHECK(surface_producer_); - WarmupSkps(shell.GetDartVM() - ->GetConcurrentMessageLoop() - ->GetTaskRunner() - .get(), - shell.GetTaskRunners().GetRasterTaskRunner().get(), - surface_producer_.value()); - } return std::make_unique(shell); } }; #else - on_create_rasterizer = [this, &product_config](flutter::Shell& shell) { - if (product_config.enable_shader_warmup()) { - FML_DCHECK(surface_producer_); - WarmupSkps( - shell.GetDartVM()->GetConcurrentMessageLoop()->GetTaskRunner().get(), - shell.GetTaskRunners().GetRasterTaskRunner().get(), - surface_producer_.value()); - } + on_create_rasterizer = [this](flutter::Shell& shell) { return std::make_unique(shell); }; #endif @@ -734,10 +743,17 @@ void Engine::WriteProfileToTrace() const { } #endif // !defined(DART_PRODUCT) -void Engine::WarmupSkps(fml::BasicTaskRunner* concurrent_task_runner, - fml::BasicTaskRunner* raster_task_runner, - VulkanSurfaceProducer& surface_producer) { - SkISize size = SkISize::Make(1024, 600); +void Engine::WarmupSkps( + fml::BasicTaskRunner* concurrent_task_runner, + fml::BasicTaskRunner* raster_task_runner, + VulkanSurfaceProducer& surface_producer, + uint64_t width, + uint64_t height, + std::shared_ptr asset_manager, + std::optional> skp_names, + std::optional> completion_callback) { + SkISize size = SkISize::Make(width, height); + // We use a raw pointer here because we want to keep this alive until all gpu // work is done and the callbacks skia takes for this are function pointers // so we are unable to use a lambda that captures the smart pointer. @@ -751,11 +767,22 @@ void Engine::WarmupSkps(fml::BasicTaskRunner* concurrent_task_runner, // tell concurrent task runner to deserialize all skps available from // the asset manager concurrent_task_runner->PostTask([raster_task_runner, skp_warmup_surface, - &surface_producer]() { + &surface_producer, asset_manager, skp_names, + completion_callback]() { TRACE_DURATION("flutter", "DeserializeSkps"); - std::vector> skp_mappings = - flutter::PersistentCache::GetCacheForProcess() - ->GetSkpsFromAssetManager(); + std::vector> skp_mappings; + if (skp_names) { + for (auto& skp_name : skp_names.value()) { + auto skp_mapping = asset_manager->GetAsMapping(skp_name); + if (skp_mapping) { + skp_mappings.push_back(std::move(skp_mapping)); + } else { + FML_LOG(ERROR) << "Failed to get mapping for " << skp_name; + } + } + } else { + skp_mappings = asset_manager->GetAsMappings(".*\\.skp$", "shaders"); + } size_t total_size = 0; for (auto& mapping : skp_mappings) { @@ -783,7 +810,7 @@ void Engine::WarmupSkps(fml::BasicTaskRunner* concurrent_task_runner, // Tell raster task runner to warmup have the compositor // context warm up the newly deserialized picture raster_task_runner->PostTask([skp_warmup_surface, picture, - &surface_producer, i, + &surface_producer, completion_callback, i, count = skp_mappings.size()] { TRACE_DURATION("flutter", "WarmupSkp"); skp_warmup_surface->GetSkiaSurface()->getCanvas()->drawPicture(picture); @@ -793,7 +820,7 @@ void Engine::WarmupSkps(fml::BasicTaskRunner* concurrent_task_runner, surface_producer.gr_context()->flushAndSubmit(); } else { // For the last skp we provide a callback that frees the warmup - // surface + // surface and calls the completion callback struct GrFlushInfo flush_info; flush_info.fFinishedContext = skp_warmup_surface; flush_info.fFinishedProc = [](void* skp_warmup_surface) { @@ -802,6 +829,14 @@ void Engine::WarmupSkps(fml::BasicTaskRunner* concurrent_task_runner, surface_producer.gr_context()->flush(flush_info); surface_producer.gr_context()->submit(); + + // We call this here instead of inside fFinishedProc above because + // we want to unblock the dart animation code as soon as the raster + // thread is free to enque work, rather than waiting for the GPU work + // itself to finish. + if (completion_callback) { + completion_callback.value()(count); + } } }); i++; diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/engine.h b/engine/src/flutter/shell/platform/fuchsia/flutter/engine.h index 1b6dc7a087..3e2b991940 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/engine.h +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/engine.h @@ -97,9 +97,15 @@ class Engine final { fml::WeakPtrFactory weak_factory_; - static void WarmupSkps(fml::BasicTaskRunner* concurrent_task_runner, - fml::BasicTaskRunner* raster_task_runner, - VulkanSurfaceProducer& surface_producer); + static void WarmupSkps( + fml::BasicTaskRunner* concurrent_task_runner, + fml::BasicTaskRunner* raster_task_runner, + VulkanSurfaceProducer& surface_producer, + uint64_t width, + uint64_t height, + std::shared_ptr asset_manager, + std::optional> skp_names, + std::optional> completion_callback); void OnMainIsolateStart(); diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/flutter_runner_product_configuration.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/flutter_runner_product_configuration.cc index e3e774aec6..dfc50c1c5c 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/flutter_runner_product_configuration.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/flutter_runner_product_configuration.cc @@ -43,6 +43,11 @@ FlutterRunnerProductConfiguration::FlutterRunnerProductConfiguration( if (val.IsBool()) enable_shader_warmup_ = val.GetBool(); } + if (document.HasMember("enable_shader_warmup_dart_hooks")) { + auto& val = document["enable_shader_warmup_dart_hooks"]; + if (val.IsBool()) + enable_shader_warmup_dart_hooks_ = val.GetBool(); + } #if defined(LEGACY_FUCHSIA_EMBEDDER) if (document.HasMember("use_legacy_renderer")) { auto& val = document["use_legacy_renderer"]; diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/flutter_runner_product_configuration.h b/engine/src/flutter/shell/platform/fuchsia/flutter/flutter_runner_product_configuration.h index 9fa36d6ab7..e5cee47fb7 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/flutter_runner_product_configuration.h +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/flutter_runner_product_configuration.h @@ -18,6 +18,9 @@ class FlutterRunnerProductConfiguration { uint64_t get_max_frames_in_flight() { return max_frames_in_flight_; } bool get_intercept_all_input() { return intercept_all_input_; } bool enable_shader_warmup() { return enable_shader_warmup_; } + bool enable_shader_warmup_dart_hooks() { + return enable_shader_warmup_dart_hooks_; + } #if defined(LEGACY_FUCHSIA_EMBEDDER) bool use_legacy_renderer() { return use_legacy_renderer_; } #endif @@ -26,7 +29,8 @@ class FlutterRunnerProductConfiguration { fml::TimeDelta vsync_offset_ = fml::TimeDelta::Zero(); uint64_t max_frames_in_flight_ = 3; bool intercept_all_input_ = false; - bool enable_shader_warmup_ = false; + bool enable_shader_warmup_ = true; + bool enable_shader_warmup_dart_hooks_ = true; #if defined(LEGACY_FUCHSIA_EMBEDDER) bool use_legacy_renderer_ = true; #endif diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view.cc index 0bc4ffd6bd..925e082554 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view.cc @@ -37,6 +37,7 @@ static constexpr char kTextInputChannel[] = "flutter/textinput"; static constexpr char kKeyEventChannel[] = "flutter/keyevent"; static constexpr char kAccessibilityChannel[] = "flutter/accessibility"; static constexpr char kFlutterPlatformViewsChannel[] = "flutter/platform_views"; +static constexpr char kFuchsiaShaderWarmupChannel[] = "fuchsia/shader_warmup"; // FL(77): Terminate engine if Fuchsia system FIDL connections have error. template @@ -74,6 +75,7 @@ PlatformView::PlatformView( OnCreateSurface on_create_surface_callback, OnSemanticsNodeUpdate on_semantics_node_update_callback, OnRequestAnnounce on_request_announce_callback, + OnShaderWarmup on_shader_warmup, std::shared_ptr external_view_embedder, AwaitVsyncCallback await_vsync_callback, AwaitVsyncForSecondaryCallbackCallback @@ -93,6 +95,7 @@ PlatformView::PlatformView( on_semantics_node_update_callback_( std::move(on_semantics_node_update_callback)), on_request_announce_callback_(std::move(on_request_announce_callback)), + on_shader_warmup_(std::move(on_shader_warmup)), external_view_embedder_(external_view_embedder), ime_client_(this), keyboard_listener_binding_(this, std::move(keyboard_listener_request)), @@ -135,6 +138,9 @@ void PlatformView::RegisterPlatformMessageHandlers() { platform_message_handlers_[kFlutterPlatformViewsChannel] = std::bind(&PlatformView::HandleFlutterPlatformViewsChannelPlatformMessage, this, std::placeholders::_1); + platform_message_handlers_[kFuchsiaShaderWarmupChannel] = + std::bind(&HandleFuchsiaShaderWarmupChannelPlatformMessage, + on_shader_warmup_, std::placeholders::_1); } // |fuchsia::ui::input::InputMethodEditorClient| @@ -1133,4 +1139,82 @@ void PlatformView::HandleFlutterPlatformViewsChannelPlatformMessage( } } +void PlatformView::HandleFuchsiaShaderWarmupChannelPlatformMessage( + OnShaderWarmup on_shader_warmup, + std::unique_ptr message) { + FML_DCHECK(message->channel() == kFuchsiaShaderWarmupChannel); + + if (!on_shader_warmup) { + FML_LOG(ERROR) << "No shader warmup callback set!"; + std::string result = "[0]"; + message->response()->Complete( + std::make_unique(std::vector( + (const uint8_t*)result.c_str(), + (const uint8_t*)result.c_str() + result.length()))); + return; + } + + const auto& data = message->data(); + rapidjson::Document document; + document.Parse(reinterpret_cast(data.GetMapping()), + data.GetSize()); + if (document.HasParseError() || !document.IsObject()) { + FML_LOG(ERROR) << "Could not parse document"; + return; + } + auto root = document.GetObject(); + auto method = root.FindMember("method"); + if (method == root.MemberEnd() || !method->value.IsString() || + method->value != "WarmupSkps") { + FML_LOG(ERROR) << "Invalid method name"; + return; + } + + auto args_it = root.FindMember("args"); + if (args_it == root.MemberEnd() || !args_it->value.IsObject()) { + FML_LOG(ERROR) << "No arguments found."; + return; + } + + auto shaders_it = root["args"].FindMember("shaders"); + if (shaders_it == root["args"].MemberEnd() || !shaders_it->value.IsArray()) { + FML_LOG(ERROR) << "No shaders found."; + return; + } + + auto width_it = root["args"].FindMember("width"); + auto height_it = root["args"].FindMember("height"); + if (width_it == root["args"].MemberEnd() || !width_it->value.IsNumber()) { + FML_LOG(ERROR) << "Invalid width"; + return; + } + if (height_it == root["args"].MemberEnd() || !height_it->value.IsNumber()) { + FML_LOG(ERROR) << "Invalid height"; + return; + } + auto width = width_it->value.GetUint64(); + auto height = height_it->value.GetUint64(); + + std::vector skp_paths; + const auto& shaders = shaders_it->value; + for (rapidjson::Value::ConstValueIterator itr = shaders.Begin(); + itr != shaders.End(); ++itr) { + skp_paths.push_back((*itr).GetString()); + } + + auto completion_callback = [response = + message->response()](uint num_successes) { + std::ostringstream result_stream; + result_stream << "[" << num_successes << "]"; + + std::string result(result_stream.str()); + + response->Complete(std::make_unique(std::vector( + (const uint8_t*)result.c_str(), + (const uint8_t*)result.c_str() + result.length()))); + }; + + on_shader_warmup(skp_paths, completion_callback, width, height); +} + } // namespace flutter_runner diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view.h b/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view.h index 607743bb41..2f3787f806 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view.h +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view.h @@ -38,6 +38,13 @@ using OnCreateSurface = fit::function()>; using OnSemanticsNodeUpdate = fit::function; using OnRequestAnnounce = fit::function; +// we use an std::function here because the fit::funtion causes problems with +// std:bind since HandleFuchsiaShaderWarmupChannelPlatformMessage takes one of +// these as its first argument. +using OnShaderWarmup = std::function&, + std::function, + uint64_t, + uint64_t)>; // PlatformView is the per-engine component residing on the platform thread that // is responsible for all platform specific integrations -- particularly @@ -76,6 +83,7 @@ class PlatformView final : public flutter::PlatformView, OnCreateSurface on_create_surface_callback, OnSemanticsNodeUpdate on_semantics_node_update_callback, OnRequestAnnounce on_request_announce_callback, + OnShaderWarmup on_shader_warmup, std::shared_ptr view_embedder, AwaitVsyncCallback await_vsync_callback, AwaitVsyncForSecondaryCallbackCallback @@ -166,6 +174,11 @@ class PlatformView final : public flutter::PlatformView, void HandleFlutterPlatformViewsChannelPlatformMessage( std::unique_ptr message); + // Channel handler for kFuchsiaShaderWarmupChannel. + static void HandleFuchsiaShaderWarmupChannelPlatformMessage( + OnShaderWarmup on_shader_warmup, + std::unique_ptr message); + const std::string debug_label_; // TODO(MI4-2490): remove once ViewRefControl is passed to Scenic and kept // alive there @@ -194,6 +207,7 @@ class PlatformView final : public flutter::PlatformView, OnSemanticsNodeUpdate on_semantics_node_update_callback_; OnRequestAnnounce on_request_announce_callback_; + OnShaderWarmup on_shader_warmup_; std::shared_ptr external_view_embedder_; int current_text_input_client_ = 0; @@ -214,7 +228,7 @@ class PlatformView final : public flutter::PlatformView, std::set down_pointers_; std::map /* message */)> /* handler */> platform_message_handlers_; diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view_unittest.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view_unittest.cc index dc357da835..8f4088073d 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view_unittest.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view_unittest.cc @@ -230,6 +230,11 @@ class PlatformViewBuilder { return *this; } + PlatformViewBuilder& SetShaderWarmupCallback(OnShaderWarmup callback) { + on_shader_warmup_callback_ = std::move(callback); + return *this; + } + PlatformViewBuilder& SetSessionListenerRequest( fidl::InterfaceRequest request) { session_listener_request_ = std::move(request); @@ -270,8 +275,9 @@ class PlatformViewBuilder { std::move(on_destroy_view_callback_), std::move(on_create_surface_callback_), std::move(on_semantics_node_update_callback_), - std::move(on_request_announce_callback_), view_embedder_, - [](auto...) {}, [](auto...) {}); + std::move(on_request_announce_callback_), + std::move(on_shader_warmup_callback_), view_embedder_, [](auto...) {}, + [](auto...) {}); } private: @@ -302,6 +308,7 @@ class PlatformViewBuilder { OnCreateSurface on_create_surface_callback_{nullptr}; OnSemanticsNodeUpdate on_semantics_node_update_callback_{nullptr}; OnRequestAnnounce on_request_announce_callback_{nullptr}; + OnShaderWarmup on_shader_warmup_callback_{nullptr}; std::shared_ptr view_embedder_{nullptr}; fml::TimeDelta vsync_offset_{fml::TimeDelta::Zero()}; }; @@ -1173,4 +1180,88 @@ TEST_F(PlatformViewTests, OnKeyEvent) { } } +// Makes sure that OnKeyEvent is dispatched as a platform message. +TEST_F(PlatformViewTests, OnShaderWarmup) { + sys::testing::ServiceDirectoryProvider services_provider(dispatcher()); + MockPlatformViewDelegate delegate; + flutter::TaskRunners task_runners = + flutter::TaskRunners("test_runners", nullptr, nullptr, nullptr, nullptr); + + uint64_t width = 200; + uint64_t height = 100; + std::vector shaders = {"foo.skp", "bar.skp", "baz.skp"}; + + OnShaderWarmup on_shader_warmup = + [&](const std::vector& shaders_in, + std::function completion_callback, uint64_t width_in, + uint64_t height_in) { + ASSERT_EQ(shaders.size(), shaders_in.size()); + for (size_t i = 0; i < shaders_in.size(); i++) { + ASSERT_EQ(shaders[i], shaders_in[i]); + } + ASSERT_EQ(width, width_in); + ASSERT_EQ(height, height_in); + + completion_callback(shaders_in.size()); + }; + + flutter_runner::PlatformView platform_view = + PlatformViewBuilder(delegate, std::move(task_runners), + services_provider.service_directory()) + .SetShaderWarmupCallback(on_shader_warmup) + .Build(); + + std::ostringstream shaders_array_ostream; + shaders_array_ostream << "[ "; + for (auto it = shaders.begin(); it != shaders.end(); ++it) { + shaders_array_ostream << "\"" << *it << "\""; + if (std::next(it) != shaders.end()) { + shaders_array_ostream << ", "; + } + } + shaders_array_ostream << "]"; + + std::string shaders_array_string = shaders_array_ostream.str(); + + // Create initial view for testing. + std::ostringstream warmup_shaders_ostream; + warmup_shaders_ostream << "{" + << " \"method\":\"WarmupSkps\"," + << " \"args\":{" + << " \"shaders\":" << shaders_array_string << "," + << " \"width\":" << width << "," + << " \"height\":" << height << " }" + << "}\n"; + std::string warmup_shaders_string = warmup_shaders_ostream.str(); + + class TestPlatformMessageResponse : public flutter::PlatformMessageResponse { + public: + TestPlatformMessageResponse() {} + void Complete(std::unique_ptr data) override { + result_string = std::string( + reinterpret_cast(data->GetMapping()), data->GetSize()); + is_complete_ = true; + } + void CompleteEmpty() override { is_complete_ = true; } + std::string result_string; + FML_DISALLOW_COPY_AND_ASSIGN(TestPlatformMessageResponse); + }; + + fml::RefPtr response( + new TestPlatformMessageResponse); + static_cast(&platform_view) + ->HandlePlatformMessage(std::make_unique( + "fuchsia/shader_warmup", + fml::MallocMapping::Copy(warmup_shaders_string.c_str(), + warmup_shaders_string.size()), + response)); + RunLoopUntilIdle(); + ASSERT_TRUE(response->is_complete()); + + std::ostringstream expected_result_ostream; + expected_result_ostream << "[" << shaders.size() << "]"; + std::string expected_result_string = expected_result_ostream.str(); + EXPECT_EQ(expected_result_string, response->result_string); +} + } // namespace flutter_runner::testing diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/tests/engine_unittests.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/tests/engine_unittests.cc index cd2b293fbf..34f9acfc86 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/tests/engine_unittests.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/tests/engine_unittests.cc @@ -54,7 +54,12 @@ class MockTaskRunner : public fml::BasicTaskRunner { class EngineTest : public ::testing::Test { public: - void WarmupSkps() { + void WarmupSkps( + uint64_t width, + uint64_t height, + std::shared_ptr asset_manager, + std::optional> skp_names, + std::optional> completion_callback) { // Have to create a message loop so default async dispatcher gets set, // otherwise we segfault creating the VulkanSurfaceProducer auto loop = fml::MessageLoopImpl::Create(); @@ -65,7 +70,8 @@ class EngineTest : public ::testing::Test { surface_producer_ = std::make_unique(&session); Engine::WarmupSkps(&concurrent_task_runner_, &raster_task_runner_, - *surface_producer_); + *surface_producer_, width, height, asset_manager, + std::nullopt, std::nullopt); } protected: @@ -112,9 +118,55 @@ TEST_F(EngineTest, SkpWarmup) { asset_manager->PushBack( std::make_unique(std::move(asset_dir_fd), false)); - PersistentCache::GetCacheForProcess()->SetAssetManager(asset_manager); + WarmupSkps(draw_size.width(), draw_size.height(), asset_manager, std::nullopt, + std::nullopt); + concurrent_task_runner_.Run(); + raster_task_runner_.Run(); - WarmupSkps(); + EXPECT_EQ(concurrent_task_runner_.GetTaskCount(), 1); + EXPECT_EQ(raster_task_runner_.GetTaskCount(), 1); +} + +TEST_F(EngineTest, SkpWarmupAsync) { + SkISize draw_size = SkISize::Make(100, 100); + SkPictureRecorder recorder; + auto canvas = recorder.beginRecording(draw_size.width(), draw_size.height()); + + // adapted from https://fiddle.skia.org/c/@Canvas_drawLine + SkPaint paint; + paint.setColor(0xFF9a67be); + paint.setStrokeWidth(20); + canvas->drawLine(0, 0, draw_size.width(), draw_size.height(), paint); + canvas->drawLine(0, draw_size.height(), draw_size.width(), 0, paint); + + sk_sp picture = recorder.finishRecordingAsPicture(); + SkSerialProcs procs = {0}; + procs.fImageProc = SerializeImageWithoutData; + procs.fTypefaceProc = SerializeTypefaceWithoutData; + sk_sp data = picture->serialize(&procs); + ASSERT_TRUE(data); + ASSERT_GT(data->size(), 0u); + + fml::NonOwnedMapping mapping(data->bytes(), data->size()); + + fml::ScopedTemporaryDirectory asset_dir; + fml::UniqueFD asset_dir_fd = fml::OpenDirectory( + asset_dir.path().c_str(), false, fml::FilePermission::kRead); + fml::UniqueFD subdir_fd = fml::OpenDirectory(asset_dir_fd, "shaders", true, + fml::FilePermission::kReadWrite); + std::string skp_name = "test.skp"; + + bool success = fml::WriteAtomically(subdir_fd, skp_name.c_str(), mapping); + ASSERT_TRUE(success); + + auto asset_manager = std::make_shared(); + asset_manager->PushBack( + std::make_unique(std::move(asset_dir_fd), false)); + + std::vector skp_names = {skp_name}; + + WarmupSkps(draw_size.width(), draw_size.height(), asset_manager, skp_names, + [](uint32_t count) { EXPECT_EQ(1u, count); }); concurrent_task_runner_.Run(); raster_task_runner_.Run();