From 967f12d80993b575b11c01ce2438b99f872d1b58 Mon Sep 17 00:00:00 2001 From: Dan Field Date: Thu, 6 Apr 2023 11:40:27 -0700 Subject: [PATCH] [Impeller] Make multi-frame image decodes work even if the GPU switch is disabled (flutter/engine#40963) Fixes https://github.com/flutter/flutter/issues/124269 Manually verified this is safe on an iPhone 11. We're not creating/encoding command buffers in this path so it's ok. AFAICT the test I added would fail if we started doing that because it doesn't provide any real command buffer interfaces. Most of the code here is related to tests. --- .../src/flutter/lib/ui/fixtures/ui_test.dart | 7 +- .../lib/ui/painting/image_decoder_impeller.cc | 2 + .../ui/painting/image_decoder_unittests.cc | 165 ++++++++++++++++++ .../lib/ui/painting/multi_frame_codec.cc | 21 +-- .../lib/ui/painting/multi_frame_codec.h | 4 +- .../shell/common/fixtures/shell_test.dart | 8 +- .../flutter/testing/dart_isolate_runner.cc | 1 + 7 files changed, 187 insertions(+), 21 deletions(-) diff --git a/engine/src/flutter/lib/ui/fixtures/ui_test.dart b/engine/src/flutter/lib/ui/fixtures/ui_test.dart index 2393ec4431..36f09bf397 100644 --- a/engine/src/flutter/lib/ui/fixtures/ui_test.dart +++ b/engine/src/flutter/lib/ui/fixtures/ui_test.dart @@ -248,10 +248,13 @@ void createPath() { external void _validatePath(Path path); @pragma('vm:entry-point') -void frameCallback(_Image, int) { - print('called back'); +void frameCallback(Object? image, int durationMilliseconds) { + validateFrameCallback(image, durationMilliseconds); } +@pragma('vm:external-name', 'ValidateFrameCallback') +external void validateFrameCallback(Object? image, int durationMilliseconds); + @pragma('vm:entry-point') void platformMessagePortResponseTest() async { ReceivePort receivePort = ReceivePort(); diff --git a/engine/src/flutter/lib/ui/painting/image_decoder_impeller.cc b/engine/src/flutter/lib/ui/painting/image_decoder_impeller.cc index 4d70397958..4db68ac2fe 100644 --- a/engine/src/flutter/lib/ui/painting/image_decoder_impeller.cc +++ b/engine/src/flutter/lib/ui/painting/image_decoder_impeller.cc @@ -110,6 +110,8 @@ static std::optional ToPixelFormat(SkColorType type) { switch (type) { case kRGBA_8888_SkColorType: return impeller::PixelFormat::kR8G8B8A8UNormInt; + case kBGRA_8888_SkColorType: + return impeller::PixelFormat::kB8G8R8A8UNormInt; case kRGBA_F16_SkColorType: return impeller::PixelFormat::kR16G16B16A16Float; case kBGR_101010x_XR_SkColorType: diff --git a/engine/src/flutter/lib/ui/painting/image_decoder_unittests.cc b/engine/src/flutter/lib/ui/painting/image_decoder_unittests.cc index 1a2da19e97..0cc5165d53 100644 --- a/engine/src/flutter/lib/ui/painting/image_decoder_unittests.cc +++ b/engine/src/flutter/lib/ui/painting/image_decoder_unittests.cc @@ -8,6 +8,7 @@ #include "flutter/impeller/core/allocator.h" #include "flutter/impeller/core/device_buffer.h" #include "flutter/impeller/geometry/size.h" +#include "flutter/impeller/renderer/context.h" #include "flutter/lib/ui/painting/image_decoder.h" #include "flutter/lib/ui/painting/image_decoder_impeller.h" #include "flutter/lib/ui/painting/image_decoder_skia.h" @@ -28,8 +29,28 @@ #include "third_party/skia/include/core/SkImageInfo.h" #include "third_party/skia/include/core/SkSize.h" +// CREATE_NATIVE_ENTRY is leaky by design +// NOLINTBEGIN(clang-analyzer-core.StackAddressEscape) + namespace impeller { +class TestImpellerTexture : public Texture { + public: + explicit TestImpellerTexture(TextureDescriptor desc) : Texture(desc) {} + + void SetLabel(std::string_view label) override {} + bool IsValid() const override { return true; } + ISize GetSize() const { return GetTextureDescriptor().size; } + + bool OnSetContents(const uint8_t* contents, size_t length, size_t slice) { + return true; + } + bool OnSetContents(std::shared_ptr mapping, + size_t slice) { + return true; + } +}; + class TestImpellerDeviceBuffer : public DeviceBuffer { public: explicit TestImpellerDeviceBuffer(DeviceBufferDescriptor desc) @@ -84,8 +105,42 @@ class TestImpellerAllocator : public impeller::Allocator { std::shared_ptr OnCreateTexture( const TextureDescriptor& desc) override { + return std::make_shared(desc); + } +}; + +class TestImpellerContext : public impeller::Context { + public: + TestImpellerContext() = default; + + bool IsValid() const override { return true; } + + const std::shared_ptr& GetCapabilities() const override { + return capabilities_; + } + + std::shared_ptr GetResourceAllocator() const override { + return std::make_shared(); + } + + std::shared_ptr GetShaderLibrary() const override { return nullptr; } + + std::shared_ptr GetSamplerLibrary() const override { + return nullptr; + } + + std::shared_ptr GetPipelineLibrary() const override { + return nullptr; + } + + std::shared_ptr CreateCommandBuffer() const override { + return nullptr; + } + + private: + std::shared_ptr capabilities_; }; } // namespace impeller @@ -98,6 +153,7 @@ class TestIOManager final : public IOManager { explicit TestIOManager(const fml::RefPtr& task_runner, bool has_gpu_context = true) : gl_surface_(SkISize::Make(1, 1)), + impeller_context_(std::make_shared()), gl_context_(has_gpu_context ? gl_surface_.CreateGrContext() : nullptr), weak_gl_context_factory_( has_gpu_context @@ -149,10 +205,20 @@ class TestIOManager final : public IOManager { return is_gpu_disabled_sync_switch_; } + // |IOManager| + std::shared_ptr GetImpellerContext() const override { + return impeller_context_; + } + + void SetGpuDisabled(bool disabled) { + is_gpu_disabled_sync_switch_->SetSwitch(disabled); + } + bool did_access_is_gpu_disabled_sync_switch_ = false; private: TestGLSurface gl_surface_; + std::shared_ptr impeller_context_; sk_sp gl_context_; std::unique_ptr> weak_gl_context_factory_; @@ -939,7 +1005,15 @@ TEST_F(ImageDecoderFixtureTest, MultiFrameCodecDidAccessGpuDisabledSyncSwitch) { std::unique_ptr io_manager; fml::RefPtr codec; + fml::AutoResetWaitableEvent latch; + auto validate_frame_callback = [&latch](Dart_NativeArguments args) { + EXPECT_FALSE(Dart_IsNull(Dart_GetNativeArgument(args, 0))); + latch.Signal(); + }; + + AddNativeCallback("ValidateFrameCallback", + CREATE_NATIVE_ENTRY(validate_frame_callback)); // Setup the IO manager. PostTaskSync(runners.GetIOTaskRunner(), [&]() { io_manager = std::make_unique(runners.GetIOTaskRunner()); @@ -978,6 +1052,95 @@ TEST_F(ImageDecoderFixtureTest, MultiFrameCodecDidAccessGpuDisabledSyncSwitch) { EXPECT_TRUE(io_manager->did_access_is_gpu_disabled_sync_switch_); }); + latch.Wait(); + + // Destroy the Isolate + isolate = nullptr; + + // Destroy the MultiFrameCodec + PostTaskSync(runners.GetUITaskRunner(), [&]() { codec = nullptr; }); + + // Destroy the IO manager + PostTaskSync(runners.GetIOTaskRunner(), [&]() { io_manager.reset(); }); +} + +TEST_F(ImageDecoderFixtureTest, + MultiFrameCodecProducesATextureEvenIfGPUIsDisabledOnImpeller) { + auto settings = CreateSettingsForFixture(); + settings.enable_impeller = true; + auto vm_ref = DartVMRef::Create(settings); + auto vm_data = vm_ref.GetVMData(); + + auto gif_mapping = OpenFixtureAsSkData("hello_loop_2.gif"); + + ASSERT_TRUE(gif_mapping); + + ImageGeneratorRegistry registry; + std::shared_ptr gif_generator = + registry.CreateCompatibleGenerator(gif_mapping); + ASSERT_TRUE(gif_generator); + + TaskRunners runners(GetCurrentTestName(), // label + CreateNewThread("platform"), // platform + CreateNewThread("raster"), // raster + CreateNewThread("ui"), // ui + CreateNewThread("io") // io + ); + + std::unique_ptr io_manager; + fml::RefPtr codec; + fml::AutoResetWaitableEvent latch; + + auto validate_frame_callback = [&latch](Dart_NativeArguments args) { + EXPECT_FALSE(Dart_IsNull(Dart_GetNativeArgument(args, 0))); + latch.Signal(); + }; + + AddNativeCallback("ValidateFrameCallback", + CREATE_NATIVE_ENTRY(validate_frame_callback)); + + // Setup the IO manager. + PostTaskSync(runners.GetIOTaskRunner(), [&]() { + io_manager = std::make_unique(runners.GetIOTaskRunner()); + // Mark GPU disabled. + io_manager->SetGpuDisabled(true); + }); + + auto isolate = RunDartCodeInIsolate(vm_ref, settings, runners, "main", {}, + GetDefaultKernelFilePath(), + io_manager->GetWeakIOManager()); + + PostTaskSync(runners.GetUITaskRunner(), [&]() { + fml::AutoResetWaitableEvent isolate_latch; + + EXPECT_TRUE(isolate->RunInIsolateScope([&]() -> bool { + Dart_Handle library = Dart_RootLibrary(); + if (Dart_IsError(library)) { + isolate_latch.Signal(); + return false; + } + Dart_Handle closure = + Dart_GetField(library, Dart_NewStringFromCString("frameCallback")); + if (Dart_IsError(closure) || !Dart_IsClosure(closure)) { + isolate_latch.Signal(); + return false; + } + + EXPECT_FALSE(io_manager->did_access_is_gpu_disabled_sync_switch_); + codec = fml::MakeRefCounted(std::move(gif_generator)); + codec->getNextFrame(closure); + isolate_latch.Signal(); + return true; + })); + isolate_latch.Wait(); + }); + + PostTaskSync(runners.GetIOTaskRunner(), [&]() { + EXPECT_TRUE(io_manager->did_access_is_gpu_disabled_sync_switch_); + }); + + latch.Wait(); + // Destroy the Isolate isolate = nullptr; @@ -990,3 +1153,5 @@ TEST_F(ImageDecoderFixtureTest, MultiFrameCodecDidAccessGpuDisabledSyncSwitch) { } // namespace testing } // namespace flutter + +// NOLINTEND(clang-analyzer-core.StackAddressEscape) diff --git a/engine/src/flutter/lib/ui/painting/multi_frame_codec.cc b/engine/src/flutter/lib/ui/painting/multi_frame_codec.cc index 23d181188a..796ea4645f 100644 --- a/engine/src/flutter/lib/ui/painting/multi_frame_codec.cc +++ b/engine/src/flutter/lib/ui/painting/multi_frame_codec.cc @@ -87,7 +87,7 @@ static bool CopyToBitmap(SkBitmap* dst, sk_sp MultiFrameCodec::State::GetNextFrameImage( fml::WeakPtr resourceContext, const std::shared_ptr& gpu_disable_sync_switch, - std::shared_ptr impeller_context_, + const std::shared_ptr& impeller_context, fml::RefPtr unref_queue) { SkBitmap bitmap = SkBitmap(); SkImageInfo info = generator_->GetInfo().makeColorType(kN32_SkColorType); @@ -145,16 +145,11 @@ sk_sp MultiFrameCodec::State::GetNextFrameImage( #if IMPELLER_SUPPORTS_RENDERING if (is_impeller_enabled_) { - sk_sp result; - // impeller, transfer to DlImageImpeller - gpu_disable_sync_switch->Execute(fml::SyncSwitch::Handlers().SetIfFalse( - [&result, &bitmap, &impeller_context_] { - result = ImageDecoderImpeller::UploadTextureToShared( - impeller_context_, std::make_shared(bitmap), - /*create_mips=*/false); - })); - - return result; + // This is safe regardless of whether the GPU is available or not because + // without mipmap creation there is no command buffer encoding done. + return ImageDecoderImpeller::UploadTextureToShared( + impeller_context, std::make_shared(bitmap), + /*create_mips=*/false); } #endif // IMPELLER_SUPPORTS_RENDERING @@ -191,12 +186,12 @@ void MultiFrameCodec::State::GetNextFrameAndInvokeCallback( fml::RefPtr unref_queue, const std::shared_ptr& gpu_disable_sync_switch, size_t trace_id, - std::shared_ptr impeller_context) { + const std::shared_ptr& impeller_context) { fml::RefPtr image = nullptr; int duration = 0; sk_sp dlImage = GetNextFrameImage(std::move(resourceContext), gpu_disable_sync_switch, - std::move(impeller_context), std::move(unref_queue)); + impeller_context, std::move(unref_queue)); if (dlImage) { image = CanvasImage::Create(); image->set_image(dlImage); diff --git a/engine/src/flutter/lib/ui/painting/multi_frame_codec.h b/engine/src/flutter/lib/ui/painting/multi_frame_codec.h index 3cb42b0706..f569bbd7eb 100644 --- a/engine/src/flutter/lib/ui/painting/multi_frame_codec.h +++ b/engine/src/flutter/lib/ui/painting/multi_frame_codec.h @@ -59,7 +59,7 @@ class MultiFrameCodec : public Codec { sk_sp GetNextFrameImage( fml::WeakPtr resourceContext, const std::shared_ptr& gpu_disable_sync_switch, - std::shared_ptr impeller_context_, + const std::shared_ptr& impeller_context, fml::RefPtr unref_queue); void GetNextFrameAndInvokeCallback( @@ -69,7 +69,7 @@ class MultiFrameCodec : public Codec { fml::RefPtr unref_queue, const std::shared_ptr& gpu_disable_sync_switch, size_t trace_id, - std::shared_ptr impeller_context_); + const std::shared_ptr& impeller_context); }; // Shared across the UI and IO task runners. diff --git a/engine/src/flutter/shell/common/fixtures/shell_test.dart b/engine/src/flutter/shell/common/fixtures/shell_test.dart index 5f953fd648..0a4e5a603b 100644 --- a/engine/src/flutter/shell/common/fixtures/shell_test.dart +++ b/engine/src/flutter/shell/common/fixtures/shell_test.dart @@ -326,10 +326,10 @@ void onBeginFrameWithNotifyNativeMain() { } @pragma('vm:entry-point') -void frameCallback(_Image, int) { - // It is used as the frame callback of 'MultiFrameCodec' in the test - // 'ItDoesNotCrashThatSkiaUnrefQueueDrainAfterIOManagerReset'. - // The test is a regression test and doesn't care about images, so it is empty. +void frameCallback(Object? image, int durationMilliseconds) { + if (image == null) { + throw Exception('Expeccted image in frame callback to be non-null'); + } } Picture CreateRedBox(Size size) { diff --git a/engine/src/flutter/testing/dart_isolate_runner.cc b/engine/src/flutter/testing/dart_isolate_runner.cc index ff034a9e72..1db5228d9d 100644 --- a/engine/src/flutter/testing/dart_isolate_runner.cc +++ b/engine/src/flutter/testing/dart_isolate_runner.cc @@ -123,6 +123,7 @@ std::unique_ptr RunDartCodeInIsolateOnUITaskRunner( context.io_manager = std::move(io_manager); context.advisory_script_uri = "main.dart"; context.advisory_script_entrypoint = entrypoint.c_str(); + context.enable_impeller = p_settings.enable_impeller; auto isolate = DartIsolate::CreateRunningRootIsolate(