From ab5d7be17952d50ce082a20c3d48d28f4042f41e Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Mon, 27 Feb 2023 11:15:32 -0800 Subject: [PATCH 01/12] [engine] move asset mapping decompression to IO thread --- lib/ui/painting/immutable_buffer.cc | 64 ++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 15 deletions(-) diff --git a/lib/ui/painting/immutable_buffer.cc b/lib/ui/painting/immutable_buffer.cc index 0d86b07c24e4a..993f0879f1e0a 100644 --- a/lib/ui/painting/immutable_buffer.cc +++ b/lib/ui/painting/immutable_buffer.cc @@ -43,7 +43,7 @@ Dart_Handle ImmutableBuffer::init(Dart_Handle buffer_handle, return Dart_Null(); } -Dart_Handle ImmutableBuffer::initFromAsset(Dart_Handle buffer_handle, +Dart_Handle ImmutableBuffer::initFromAsset(Dart_Handle raw_buffer_handle, Dart_Handle asset_name_handle, Dart_Handle callback_handle) { UIDartState::ThrowIfUIOperationsProhibited(); @@ -62,21 +62,55 @@ Dart_Handle ImmutableBuffer::initFromAsset(Dart_Handle buffer_handle, std::string asset_name = std::string{reinterpret_cast(chars), static_cast(asset_length)}; - std::shared_ptr asset_manager = UIDartState::Current() - ->platform_configuration() - ->client() - ->GetAssetManager(); - std::unique_ptr data = asset_manager->GetAsMapping(asset_name); - if (data == nullptr) { - return tonic::ToDart("Asset not found"); - } + auto* dart_state = UIDartState::Current(); + auto ui_task_runner = dart_state->GetTaskRunners().GetUITaskRunner(); + auto buffer_callback = + std::make_unique(dart_state, callback_handle); + auto buffer_handle = std::make_unique( + dart_state, raw_buffer_handle); - auto size = data->GetSize(); - const void* bytes = static_cast(data->GetMapping()); - auto sk_data = MakeSkDataWithCopy(bytes, size); - auto buffer = fml::MakeRefCounted(sk_data); - buffer->AssociateWithDartWrapper(buffer_handle); - tonic::DartInvoke(callback_handle, {tonic::ToDart(size)}); + auto ui_task = fml::MakeCopyable( + [buffer_callback = std::move(buffer_callback), + buffer_handle = std::move(buffer_handle)](const sk_sp& sk_data, + size_t buffer_size) mutable { + auto dart_state = buffer_callback->dart_state().lock(); + if (!dart_state) { + return; + } + tonic::DartState::Scope scope(dart_state); + if (!sk_data) { + // -1 is used as a sentinel that the file could not be opened. + tonic::DartInvoke(buffer_callback->Get(), {tonic::ToDart(-1)}); + return; + } + auto buffer = fml::MakeRefCounted(sk_data); + buffer->AssociateWithDartWrapper(buffer_handle->Get()); + tonic::DartInvoke(buffer_callback->Get(), {tonic::ToDart(buffer_size)}); + }); + + dart_state->GetConcurrentTaskRunner()->PostTask( + [asset_name = std::move(asset_name), + ui_task_runner = std::move(ui_task_runner), ui_task] { + std::shared_ptr asset_manager = + UIDartState::Current() + ->platform_configuration() + ->client() + ->GetAssetManager(); + std::unique_ptr mapping = + asset_manager->GetAsMapping(asset_name); + + sk_sp sk_data; + size_t buffer_size = 0; + if (mapping != nullptr) { + buffer_size = mapping->GetSize(); + const void* bytes = static_cast(mapping->GetMapping()); + sk_data = MakeSkDataWithCopy(bytes, buffer_size); + } + ui_task_runner->PostTask( + [sk_data = std::move(sk_data), ui_task = ui_task, buffer_size]() { + ui_task(sk_data, buffer_size); + }); + }); return Dart_Null(); } From 4bb19990dbe5aa47a6b34f8c7d429e74bfd1aa3c Mon Sep 17 00:00:00 2001 From: jonahwilliams Date: Mon, 27 Feb 2023 11:34:12 -0800 Subject: [PATCH 02/12] acquire on UI thread --- lib/ui/painting/immutable_buffer.cc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/ui/painting/immutable_buffer.cc b/lib/ui/painting/immutable_buffer.cc index 993f0879f1e0a..0595eb8dc93e3 100644 --- a/lib/ui/painting/immutable_buffer.cc +++ b/lib/ui/painting/immutable_buffer.cc @@ -68,6 +68,10 @@ Dart_Handle ImmutableBuffer::initFromAsset(Dart_Handle raw_buffer_handle, std::make_unique(dart_state, callback_handle); auto buffer_handle = std::make_unique( dart_state, raw_buffer_handle); + auto asset_manager = UIDartState::Current() + ->platform_configuration() + ->client() + ->GetAssetManager(); auto ui_task = fml::MakeCopyable( [buffer_callback = std::move(buffer_callback), @@ -90,12 +94,8 @@ Dart_Handle ImmutableBuffer::initFromAsset(Dart_Handle raw_buffer_handle, dart_state->GetConcurrentTaskRunner()->PostTask( [asset_name = std::move(asset_name), + asset_manager = std::move(asset_manager), ui_task_runner = std::move(ui_task_runner), ui_task] { - std::shared_ptr asset_manager = - UIDartState::Current() - ->platform_configuration() - ->client() - ->GetAssetManager(); std::unique_ptr mapping = asset_manager->GetAsMapping(asset_name); From 3fcc0bdde03d6916194bcc0f3dd5ac171be29d2a Mon Sep 17 00:00:00 2001 From: jonahwilliams Date: Mon, 27 Feb 2023 11:55:11 -0800 Subject: [PATCH 03/12] fix error handling --- lib/ui/painting.dart | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/ui/painting.dart b/lib/ui/painting.dart index a147acec52606..1d935076a889f 100644 --- a/lib/ui/painting.dart +++ b/lib/ui/painting.dart @@ -6293,7 +6293,12 @@ class ImmutableBuffer extends NativeFieldWrapperClass1 { final ImmutableBuffer instance = ImmutableBuffer._(0); return _futurize((_Callback callback) { return instance._initFromAsset(encodedKey, callback); - }).then((int length) => instance.._length = length); + }).then((int length) { + if (length == -1) { + throw Exception('Asset not found'); + } + return instance.._length = length; + }); } /// Create a buffer from the file with [path]. From 6505195fffd1de44a5d4952d392910bbb4572099 Mon Sep 17 00:00:00 2001 From: jonahwilliams Date: Tue, 7 Mar 2023 10:29:23 -0800 Subject: [PATCH 04/12] try a shell test --- fml/concurrent_message_loop.cc | 10 +++++ fml/concurrent_message_loop.h | 2 + shell/common/fixtures/shell_test.dart | 8 ++++ shell/common/shell_unittests.cc | 61 +++++++++++++++++++++++++++ 4 files changed, 81 insertions(+) diff --git a/fml/concurrent_message_loop.cc b/fml/concurrent_message_loop.cc index 681b983fca3ed..f553654a1195f 100644 --- a/fml/concurrent_message_loop.cc +++ b/fml/concurrent_message_loop.cc @@ -170,4 +170,14 @@ void ConcurrentTaskRunner::PostTask(const fml::closure& task) { task(); } +bool ConcurrentMessageLoop::RunsTasksOnCurrentThread() { + std::scoped_lock lock(tasks_mutex_); + for (const auto& worker_thread_id : worker_thread_ids_) { + if (worker_thread_id == std::this_thread::get_id()) { + return true; + } + } + return false; +} + } // namespace fml diff --git a/fml/concurrent_message_loop.h b/fml/concurrent_message_loop.h index c043705935e57..ab8534b6c946a 100644 --- a/fml/concurrent_message_loop.h +++ b/fml/concurrent_message_loop.h @@ -34,6 +34,8 @@ class ConcurrentMessageLoop void PostTaskToAllWorkers(const fml::closure& task); + bool RunsTasksOnCurrentThread(); + private: friend ConcurrentTaskRunner; diff --git a/shell/common/fixtures/shell_test.dart b/shell/common/fixtures/shell_test.dart index 605e3d951d230..b88289a093f74 100644 --- a/shell/common/fixtures/shell_test.dart +++ b/shell/common/fixtures/shell_test.dart @@ -470,3 +470,11 @@ Future testPluginUtilitiesCallbackHandle() async { } notifyNativeBool(true); } + +@pragma('vm:entry-point') +Future testThatAssetLoadingHappensOnWorkerThread() async { + try { + await ImmutableBuffer.fromAsset('DoesNotExist'); + } catch (err) { /* Do nothing */ } + notifyNative(); +} diff --git a/shell/common/shell_unittests.cc b/shell/common/shell_unittests.cc index 449822cd91c32..740ff1ae9e0c0 100644 --- a/shell/common/shell_unittests.cc +++ b/shell/common/shell_unittests.cc @@ -194,6 +194,37 @@ class TestAssetResolver : public AssetResolver { AssetResolver::AssetResolverType type_; }; +class ThreadCheckingAssetResolver : public AssetResolver { + public: + ThreadCheckingAssetResolver( + std::shared_ptr concurrent_loop) + : concurrent_loop_(std::move(concurrent_loop)) {} + + // |AssetResolver| + bool IsValid() const override { return true; } + + // |AssetResolver| + bool IsValidAfterAssetManagerChange() const override { return true; } + + // |AssetResolver| + AssetResolverType GetType() const { + return AssetResolverType::kApkAssetProvider; + } + + // |AssetResolver| + std::unique_ptr GetAsMapping( + const std::string& asset_name) const override { + mapping_requests.push_back(asset_name); + EXPECT_TRUE(concurrent_loop_->RunsTasksOnCurrentThread()); + return nullptr; + } + + mutable std::vector mapping_requests; + + private: + std::shared_ptr concurrent_loop_; +}; + static bool ValidateShell(Shell* shell) { if (!shell) { return false; @@ -3805,6 +3836,36 @@ TEST_F(ShellTest, SpawnWorksWithOnError) { ASSERT_FALSE(DartVMRef::IsInstanceRunning()); } +TEST_F(ShellTest, ImmutableBufferLoadsAssetOnBackgroundThread) { + Settings settings = CreateSettingsForFixture(); + auto task_runner = CreateNewThread(); + TaskRunners task_runners("test", task_runner, task_runner, task_runner, + task_runner); + std::unique_ptr shell = CreateShell(settings, task_runners); + + fml::CountDownLatch latch(1); + AddNativeCallback("NotifyNative", + CREATE_NATIVE_ENTRY([&](auto args) { latch.CountDown(); })); + + auto asset_manager = shell->GetEngine()->GetAssetManager(); + + auto test_resolver = std::make_unique( + shell->GetDartVM()->GetConcurrentMessageLoop()); + asset_manager->PushBack(std::move(test_resolver)); + + // Create the surface needed by rasterizer + PlatformViewNotifyCreated(shell.get()); + + auto configuration = RunConfiguration::InferFromSettings(settings); + configuration.SetEntrypoint("testThatAssetLoadingHappensOnWorkerThread"); + + RunEngine(shell.get(), std::move(configuration)); + PumpOneFrame(shell.get()); + + latch.Wait(); + DestroyShell(std::move(shell), task_runners); +} + TEST_F(ShellTest, PictureToImageSync) { #if !SHELL_ENABLE_GL // This test uses the GL backend. From 33a7117897abe7d3bae0175bd368498c0251440d Mon Sep 17 00:00:00 2001 From: jonahwilliams Date: Tue, 7 Mar 2023 10:57:19 -0800 Subject: [PATCH 05/12] fix test --- shell/common/shell_unittests.cc | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/shell/common/shell_unittests.cc b/shell/common/shell_unittests.cc index 740ff1ae9e0c0..4a3ac7069a046 100644 --- a/shell/common/shell_unittests.cc +++ b/shell/common/shell_unittests.cc @@ -19,6 +19,7 @@ #include "flutter/flow/layers/layer_raster_cache_item.h" #include "flutter/flow/layers/platform_view_layer.h" #include "flutter/flow/layers/transform_layer.h" +#include "flutter/fml/backtrace.h" #include "flutter/fml/command_line.h" #include "flutter/fml/dart/dart_converter.h" #include "flutter/fml/make_copyable.h" @@ -214,8 +215,13 @@ class ThreadCheckingAssetResolver : public AssetResolver { // |AssetResolver| std::unique_ptr GetAsMapping( const std::string& asset_name) const override { + if (asset_name == "FontManifest.json") { + // This file is loaded directly by the engine. + return nullptr; + } mapping_requests.push_back(asset_name); - EXPECT_TRUE(concurrent_loop_->RunsTasksOnCurrentThread()); + EXPECT_TRUE(concurrent_loop_->RunsTasksOnCurrentThread()) + << fml::BacktraceHere(); return nullptr; } @@ -3847,23 +3853,26 @@ TEST_F(ShellTest, ImmutableBufferLoadsAssetOnBackgroundThread) { AddNativeCallback("NotifyNative", CREATE_NATIVE_ENTRY([&](auto args) { latch.CountDown(); })); - auto asset_manager = shell->GetEngine()->GetAssetManager(); - - auto test_resolver = std::make_unique( - shell->GetDartVM()->GetConcurrentMessageLoop()); - asset_manager->PushBack(std::move(test_resolver)); - // Create the surface needed by rasterizer PlatformViewNotifyCreated(shell.get()); auto configuration = RunConfiguration::InferFromSettings(settings); configuration.SetEntrypoint("testThatAssetLoadingHappensOnWorkerThread"); + auto asset_manager = configuration.GetAssetManager(); + auto test_resolver = std::make_unique( + shell->GetDartVM()->GetConcurrentMessageLoop()); + auto leaked_resolver = test_resolver.get(); + asset_manager->PushBack(std::move(test_resolver)); RunEngine(shell.get(), std::move(configuration)); PumpOneFrame(shell.get()); latch.Wait(); - DestroyShell(std::move(shell), task_runners); + + EXPECT_EQ(leaked_resolver->mapping_requests[0], "DoesNotExist"); + + PlatformViewNotifyDestroyed(shell.get()); + DestroyShell(std::move(shell)); } TEST_F(ShellTest, PictureToImageSync) { From efe5617a014e672cfea2d5b4c44c967907636fcf Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Tue, 7 Mar 2023 11:32:01 -0800 Subject: [PATCH 06/12] Update shell_unittests.cc --- shell/common/shell_unittests.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/common/shell_unittests.cc b/shell/common/shell_unittests.cc index 4a3ac7069a046..f8780b5c72011 100644 --- a/shell/common/shell_unittests.cc +++ b/shell/common/shell_unittests.cc @@ -197,7 +197,7 @@ class TestAssetResolver : public AssetResolver { class ThreadCheckingAssetResolver : public AssetResolver { public: - ThreadCheckingAssetResolver( + explicit ThreadCheckingAssetResolver( std::shared_ptr concurrent_loop) : concurrent_loop_(std::move(concurrent_loop)) {} From ba10197a821a2d81d059320e411b0ef1a415153c Mon Sep 17 00:00:00 2001 From: jonahwilliams Date: Tue, 7 Mar 2023 15:44:13 -0800 Subject: [PATCH 07/12] fix shell destruction thread --- shell/common/shell_unittests.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/common/shell_unittests.cc b/shell/common/shell_unittests.cc index 4a3ac7069a046..fc4ef02236228 100644 --- a/shell/common/shell_unittests.cc +++ b/shell/common/shell_unittests.cc @@ -3872,7 +3872,7 @@ TEST_F(ShellTest, ImmutableBufferLoadsAssetOnBackgroundThread) { EXPECT_EQ(leaked_resolver->mapping_requests[0], "DoesNotExist"); PlatformViewNotifyDestroyed(shell.get()); - DestroyShell(std::move(shell)); + DestroyShell(std::move(shell), task_runners); } TEST_F(ShellTest, PictureToImageSync) { From 63ef819c2165da76cf5bbd73b86da60942994035 Mon Sep 17 00:00:00 2001 From: jonahwilliams Date: Thu, 9 Mar 2023 10:56:59 -0800 Subject: [PATCH 08/12] fixes from Jason suggestion --- lib/ui/painting/immutable_buffer.cc | 36 ++++++++++++++++++----------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/lib/ui/painting/immutable_buffer.cc b/lib/ui/painting/immutable_buffer.cc index 0595eb8dc93e3..4d1c457d7c1df 100644 --- a/lib/ui/painting/immutable_buffer.cc +++ b/lib/ui/painting/immutable_buffer.cc @@ -64,19 +64,23 @@ Dart_Handle ImmutableBuffer::initFromAsset(Dart_Handle raw_buffer_handle, auto* dart_state = UIDartState::Current(); auto ui_task_runner = dart_state->GetTaskRunners().GetUITaskRunner(); - auto buffer_callback = - std::make_unique(dart_state, callback_handle); - auto buffer_handle = std::make_unique( - dart_state, raw_buffer_handle); + auto* buffer_callback_ptr = + new tonic::DartPersistentValue(dart_state, callback_handle); + auto* buffer_handle_ptr = + new tonic::DartPersistentValue(dart_state, raw_buffer_handle); auto asset_manager = UIDartState::Current() ->platform_configuration() ->client() ->GetAssetManager(); auto ui_task = fml::MakeCopyable( - [buffer_callback = std::move(buffer_callback), - buffer_handle = std::move(buffer_handle)](const sk_sp& sk_data, - size_t buffer_size) mutable { + [buffer_callback_ptr, buffer_handle_ptr](const sk_sp& sk_data, + size_t buffer_size) mutable { + std::unique_ptr buffer_handle( + buffer_handle_ptr); + std::unique_ptr buffer_callback( + buffer_callback_ptr); + auto dart_state = buffer_callback->dart_state().lock(); if (!dart_state) { return; @@ -135,15 +139,19 @@ Dart_Handle ImmutableBuffer::initFromFile(Dart_Handle raw_buffer_handle, auto* dart_state = UIDartState::Current(); auto ui_task_runner = dart_state->GetTaskRunners().GetUITaskRunner(); - auto buffer_callback = - std::make_unique(dart_state, callback_handle); - auto buffer_handle = std::make_unique( - dart_state, raw_buffer_handle); + auto* buffer_callback_ptr = + new tonic::DartPersistentValue(dart_state, callback_handle); + auto* buffer_handle_ptr = + new tonic::DartPersistentValue(dart_state, raw_buffer_handle); auto ui_task = fml::MakeCopyable( - [buffer_callback = std::move(buffer_callback), - buffer_handle = std::move(buffer_handle)](const sk_sp& sk_data, - size_t buffer_size) mutable { + [buffer_callback_ptr, buffer_handle_ptr](const sk_sp& sk_data, + size_t buffer_size) mutable { + std::unique_ptr buffer_handle( + buffer_handle_ptr); + std::unique_ptr buffer_callback( + buffer_callback_ptr); + auto dart_state = buffer_callback->dart_state().lock(); if (!dart_state) { return; From 8a105efb73e3c6bf218135c5d93b8e39ec039f90 Mon Sep 17 00:00:00 2001 From: jonahwilliams Date: Thu, 9 Mar 2023 13:19:03 -0800 Subject: [PATCH 09/12] move creation after dart state scope lock --- lib/ui/painting/immutable_buffer.cc | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/ui/painting/immutable_buffer.cc b/lib/ui/painting/immutable_buffer.cc index 4d1c457d7c1df..8407559631ba3 100644 --- a/lib/ui/painting/immutable_buffer.cc +++ b/lib/ui/painting/immutable_buffer.cc @@ -76,16 +76,17 @@ Dart_Handle ImmutableBuffer::initFromAsset(Dart_Handle raw_buffer_handle, auto ui_task = fml::MakeCopyable( [buffer_callback_ptr, buffer_handle_ptr](const sk_sp& sk_data, size_t buffer_size) mutable { - std::unique_ptr buffer_handle( - buffer_handle_ptr); - std::unique_ptr buffer_callback( - buffer_callback_ptr); - auto dart_state = buffer_callback->dart_state().lock(); if (!dart_state) { return; } tonic::DartState::Scope scope(dart_state); + + std::unique_ptr buffer_handle( + buffer_handle_ptr); + std::unique_ptr buffer_callback( + buffer_callback_ptr); + if (!sk_data) { // -1 is used as a sentinel that the file could not be opened. tonic::DartInvoke(buffer_callback->Get(), {tonic::ToDart(-1)}); @@ -147,16 +148,17 @@ Dart_Handle ImmutableBuffer::initFromFile(Dart_Handle raw_buffer_handle, auto ui_task = fml::MakeCopyable( [buffer_callback_ptr, buffer_handle_ptr](const sk_sp& sk_data, size_t buffer_size) mutable { - std::unique_ptr buffer_handle( - buffer_handle_ptr); - std::unique_ptr buffer_callback( - buffer_callback_ptr); - auto dart_state = buffer_callback->dart_state().lock(); if (!dart_state) { return; } tonic::DartState::Scope scope(dart_state); + + std::unique_ptr buffer_handle( + buffer_handle_ptr); + std::unique_ptr buffer_callback( + buffer_callback_ptr); + if (!sk_data) { // -1 is used as a sentinel that the file could not be opened. tonic::DartInvoke(buffer_callback->Get(), {tonic::ToDart(-1)}); From 9111571df7543ba3c2a30a4351a2c6840e045355 Mon Sep 17 00:00:00 2001 From: jonahwilliams Date: Thu, 9 Mar 2023 13:31:20 -0800 Subject: [PATCH 10/12] ++ --- lib/ui/painting/immutable_buffer.cc | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/ui/painting/immutable_buffer.cc b/lib/ui/painting/immutable_buffer.cc index 8407559631ba3..5d3e88b4c19a9 100644 --- a/lib/ui/painting/immutable_buffer.cc +++ b/lib/ui/painting/immutable_buffer.cc @@ -76,7 +76,10 @@ Dart_Handle ImmutableBuffer::initFromAsset(Dart_Handle raw_buffer_handle, auto ui_task = fml::MakeCopyable( [buffer_callback_ptr, buffer_handle_ptr](const sk_sp& sk_data, size_t buffer_size) mutable { - auto dart_state = buffer_callback->dart_state().lock(); + if (!buffer_callback_ptr) { + return; + } + auto dart_state = buffer_callback_ptr->dart_state().lock(); if (!dart_state) { return; } @@ -148,7 +151,10 @@ Dart_Handle ImmutableBuffer::initFromFile(Dart_Handle raw_buffer_handle, auto ui_task = fml::MakeCopyable( [buffer_callback_ptr, buffer_handle_ptr](const sk_sp& sk_data, size_t buffer_size) mutable { - auto dart_state = buffer_callback->dart_state().lock(); + if (!buffer_callback_ptr) { + return; + } + auto dart_state = buffer_callback_ptr->dart_state().lock(); if (!dart_state) { return; } From 2af02dfd4228064d7ee0604f6bb8eaf351d2fb78 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Thu, 9 Mar 2023 13:49:03 -0800 Subject: [PATCH 11/12] Update immutable_buffer.cc --- lib/ui/painting/immutable_buffer.cc | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/lib/ui/painting/immutable_buffer.cc b/lib/ui/painting/immutable_buffer.cc index 5d3e88b4c19a9..f6700c888121e 100644 --- a/lib/ui/painting/immutable_buffer.cc +++ b/lib/ui/painting/immutable_buffer.cc @@ -76,20 +76,17 @@ Dart_Handle ImmutableBuffer::initFromAsset(Dart_Handle raw_buffer_handle, auto ui_task = fml::MakeCopyable( [buffer_callback_ptr, buffer_handle_ptr](const sk_sp& sk_data, size_t buffer_size) mutable { - if (!buffer_callback_ptr) { - return; - } + std::unique_ptr buffer_handle( + buffer_handle_ptr); + std::unique_ptr buffer_callback( + buffer_callback_ptr); + auto dart_state = buffer_callback_ptr->dart_state().lock(); if (!dart_state) { return; } tonic::DartState::Scope scope(dart_state); - std::unique_ptr buffer_handle( - buffer_handle_ptr); - std::unique_ptr buffer_callback( - buffer_callback_ptr); - if (!sk_data) { // -1 is used as a sentinel that the file could not be opened. tonic::DartInvoke(buffer_callback->Get(), {tonic::ToDart(-1)}); @@ -151,20 +148,16 @@ Dart_Handle ImmutableBuffer::initFromFile(Dart_Handle raw_buffer_handle, auto ui_task = fml::MakeCopyable( [buffer_callback_ptr, buffer_handle_ptr](const sk_sp& sk_data, size_t buffer_size) mutable { - if (!buffer_callback_ptr) { - return; - } + std::unique_ptr buffer_handle( + buffer_handle_ptr); + std::unique_ptr buffer_callback( + buffer_callback_ptr); auto dart_state = buffer_callback_ptr->dart_state().lock(); if (!dart_state) { return; } tonic::DartState::Scope scope(dart_state); - std::unique_ptr buffer_handle( - buffer_handle_ptr); - std::unique_ptr buffer_callback( - buffer_callback_ptr); - if (!sk_data) { // -1 is used as a sentinel that the file could not be opened. tonic::DartInvoke(buffer_callback->Get(), {tonic::ToDart(-1)}); From 5db53e4abde6232381883cf93f5a5ba92a30cd3f Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Thu, 9 Mar 2023 13:50:53 -0800 Subject: [PATCH 12/12] Update immutable_buffer.cc --- lib/ui/painting/immutable_buffer.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ui/painting/immutable_buffer.cc b/lib/ui/painting/immutable_buffer.cc index f6700c888121e..0f859f3420140 100644 --- a/lib/ui/painting/immutable_buffer.cc +++ b/lib/ui/painting/immutable_buffer.cc @@ -81,7 +81,7 @@ Dart_Handle ImmutableBuffer::initFromAsset(Dart_Handle raw_buffer_handle, std::unique_ptr buffer_callback( buffer_callback_ptr); - auto dart_state = buffer_callback_ptr->dart_state().lock(); + auto dart_state = buffer_callback->dart_state().lock(); if (!dart_state) { return; } @@ -152,7 +152,7 @@ Dart_Handle ImmutableBuffer::initFromFile(Dart_Handle raw_buffer_handle, buffer_handle_ptr); std::unique_ptr buffer_callback( buffer_callback_ptr); - auto dart_state = buffer_callback_ptr->dart_state().lock(); + auto dart_state = buffer_callback->dart_state().lock(); if (!dart_state) { return; }