Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 61c9ac7

Browse files
committed
wph migration
1 parent 1e302a1 commit 61c9ac7

File tree

13 files changed

+189
-40
lines changed

13 files changed

+189
-40
lines changed

lib/ui/painting/image_decoder.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,13 @@ void ImageDecoder::Decode(fml::RefPtr<ImageDescriptor> descriptor,
223223
FML_DCHECK(callback);
224224
FML_DCHECK(runners_.GetUITaskRunner()->RunsTasksOnCurrentThread());
225225

226-
// Always service the callback on the UI thread.
227-
auto result = [callback, ui_runner = runners_.GetUITaskRunner()](
226+
// Always service the callback (and cleanup the descriptor) on the UI thread.
227+
auto result = [callback, descriptor, ui_runner = runners_.GetUITaskRunner()](
228228
SkiaGPUObject<SkImage> image,
229229
fml::tracing::TraceFlow flow) {
230-
ui_runner->PostTask(fml::MakeCopyable(
231-
[callback, image = std::move(image), flow = std::move(flow)]() mutable {
230+
ui_runner->PostTask(
231+
fml::MakeCopyable([callback, descriptor, image = std::move(image),
232+
flow = std::move(flow)]() mutable {
232233
// We are going to terminate the trace flow here. Flows cannot
233234
// terminate without a base trace. Add one explicitly.
234235
TRACE_EVENT0("flutter", "ImageDecodeCallback");

lib/ui/painting/image_encoding.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ enum ImageByteFormat {
3535
kPNG,
3636
};
3737

38-
void FinalizeSkData(void* isolate_callback_data,
39-
Dart_WeakPersistentHandle handle,
40-
void* peer) {
38+
void FinalizeSkData(void* isolate_callback_data, void* peer) {
4139
SkData* buffer = reinterpret_cast<SkData*>(peer);
4240
buffer->unref();
4341
}

runtime/dart_isolate.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,11 @@ void DartIsolate::AddIsolateShutdownCallback(const fml::closure& closure) {
868868
}
869869

870870
void DartIsolate::OnShutdownCallback() {
871+
tonic::DartState* state = tonic::DartState::Current();
872+
if (state != nullptr) {
873+
state->SetIsShuttingDown();
874+
}
875+
871876
{
872877
tonic::DartApiScope api_scope;
873878
Dart_Handle sticky_error = Dart_GetStickyError();

shell/platform/embedder/embedder.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,8 +1883,7 @@ FlutterEngineResult FlutterEnginePostDartObject(
18831883
dart_object.value.as_external_typed_data.data = buffer;
18841884
dart_object.value.as_external_typed_data.peer = peer;
18851885
dart_object.value.as_external_typed_data.callback =
1886-
+[](void* unused_isolate_callback_data,
1887-
Dart_WeakPersistentHandle unused_handle, void* peer) {
1886+
+[](void* unused_isolate_callback_data, void* peer) {
18881887
auto typed_peer = reinterpret_cast<ExternalTypedDataPeer*>(peer);
18891888
typed_peer->trampoline(typed_peer->user_data);
18901889
delete typed_peer;

shell/platform/fuchsia/dart_runner/dart_runner.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ void IsolateShutdownCallback(void* isolate_group_data, void* isolate_data) {
9292
tonic::DartMicrotaskQueue::GetForCurrentThread()->Destroy();
9393
async_loop_quit(loop);
9494
}
95+
96+
auto state =
97+
static_cast<std::shared_ptr<tonic::DartState>*>(isolate_group_data);
98+
state->get()->SetIsShuttingDown();
9599
}
96100

97101
void IsolateGroupCleanupCallback(void* isolate_group_data) {

third_party/tonic/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ source_set("tonic") {
2929
"dart_persistent_value.h",
3030
"dart_state.cc",
3131
"dart_state.h",
32+
"dart_weak_persistent_value.cc",
33+
"dart_weak_persistent_value.h",
3234
"dart_wrappable.cc",
3335
"dart_wrappable.h",
3436
"dart_wrapper_info.h",

third_party/tonic/dart_state.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ DartState::DartState(int dirfd,
2727
message_handler_(new DartMessageHandler()),
2828
file_loader_(new FileLoader(dirfd)),
2929
message_epilogue_(message_epilogue),
30-
has_set_return_code_(false) {}
30+
has_set_return_code_(false),
31+
is_shutting_down_(false) {}
3132

3233
DartState::~DartState() {}
3334

third_party/tonic/dart_state.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ class DartState : public std::enable_shared_from_this<DartState> {
6868
void SetReturnCodeCallback(std::function<void(uint32_t)> callback);
6969
bool has_set_return_code() const { return has_set_return_code_; }
7070

71+
void SetIsShuttingDown() { is_shutting_down_ = true; }
72+
bool IsShuttingDown() { return is_shutting_down_; }
73+
7174
virtual void DidSetIsolate();
7275

7376
static Dart_Handle HandleLibraryTag(Dart_LibraryTag tag,
@@ -83,6 +86,7 @@ class DartState : public std::enable_shared_from_this<DartState> {
8386
std::function<void(Dart_Handle)> message_epilogue_;
8487
std::function<void(uint32_t)> set_return_code_callback_;
8588
bool has_set_return_code_;
89+
std::atomic<bool> is_shutting_down_;
8690

8791
protected:
8892
TONIC_DISALLOW_COPY_AND_ASSIGN(DartState);
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "tonic/dart_weak_persistent_value.h"
6+
7+
#include "tonic/dart_state.h"
8+
#include "tonic/scopes/dart_isolate_scope.h"
9+
10+
namespace tonic {
11+
12+
DartWeakPersistentValue::DartWeakPersistentValue() : handle_(nullptr) {}
13+
14+
DartWeakPersistentValue::~DartWeakPersistentValue() {
15+
Clear();
16+
}
17+
18+
void DartWeakPersistentValue::Set(DartState* dart_state,
19+
Dart_Handle object,
20+
void* peer,
21+
intptr_t external_allocation_size,
22+
Dart_HandleFinalizer callback) {
23+
TONIC_DCHECK(is_empty());
24+
dart_state_ = dart_state->GetWeakPtr();
25+
handle_ = Dart_NewWeakPersistentHandle(object, peer, external_allocation_size,
26+
callback);
27+
}
28+
29+
void DartWeakPersistentValue::Clear() {
30+
if (!handle_) {
31+
return;
32+
}
33+
34+
auto dart_state = dart_state_.lock();
35+
if (!dart_state) {
36+
return;
37+
}
38+
39+
if (!dart_state->IsShuttingDown()) {
40+
if (Dart_CurrentIsolateGroup()) {
41+
Dart_DeleteWeakPersistentHandle(handle_);
42+
} else {
43+
// If we are not on the mutator thread, this will fail. The caller must
44+
// ensure to be on the mutator thread.
45+
DartIsolateScope scope(dart_state->isolate());
46+
Dart_DeleteWeakPersistentHandle(handle_);
47+
}
48+
}
49+
// If it's shutting down, the handle will be deleted already.
50+
51+
dart_state_.reset();
52+
handle_ = nullptr;
53+
}
54+
55+
Dart_Handle DartWeakPersistentValue::Get() {
56+
auto dart_state = dart_state_.lock();
57+
TONIC_DCHECK(dart_state);
58+
TONIC_DCHECK(!dart_state->IsShuttingDown());
59+
if (!handle_) {
60+
return nullptr;
61+
}
62+
return Dart_HandleFromWeakPersistent(handle_);
63+
}
64+
65+
} // namespace tonic
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef LIB_TONIC_DART_WEAK_PERSISTENT_VALUE_H_
6+
#define LIB_TONIC_DART_WEAK_PERSISTENT_VALUE_H_
7+
8+
#include <memory>
9+
10+
#include "third_party/dart/runtime/include/dart_api.h"
11+
#include "tonic/common/macros.h"
12+
13+
namespace tonic {
14+
class DartState;
15+
16+
// DartWeakPersistentValue is a bookkeeping class to help pair calls to
17+
// Dart_NewWeakPersistentHandle with Dart_DeleteWeakPersistentHandle even in
18+
// the case if IsolateGroup shutdown. Consider using this class instead of
19+
// holding a Dart_PersistentHandle directly so that you don't leak the
20+
// Dart_WeakPersistentHandle.
21+
class DartWeakPersistentValue {
22+
public:
23+
DartWeakPersistentValue();
24+
~DartWeakPersistentValue();
25+
26+
Dart_WeakPersistentHandle value() const { return handle_; }
27+
bool is_empty() const { return handle_ == nullptr; }
28+
29+
void Set(DartState* dart_state,
30+
Dart_Handle object,
31+
void* peer,
32+
intptr_t external_allocation_size,
33+
Dart_HandleFinalizer callback);
34+
void Clear();
35+
Dart_Handle Get();
36+
37+
const std::weak_ptr<DartState>& dart_state() const { return dart_state_; }
38+
39+
private:
40+
std::weak_ptr<DartState> dart_state_;
41+
Dart_WeakPersistentHandle handle_;
42+
43+
TONIC_DISALLOW_COPY_AND_ASSIGN(DartWeakPersistentValue);
44+
};
45+
46+
} // namespace tonic
47+
48+
#endif // LIB_TONIC_DART_WEAK_PERSISTENT_VALUE_H_

0 commit comments

Comments
 (0)