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

Commit 8ba522e

Browse files
B3rn475jason-simmons
authored andcommitted
Avoid to freeze the system if hot reloading during debug (#3833)
1 parent 431a251 commit 8ba522e

18 files changed

+76
-67
lines changed

shell/common/engine.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ void Engine::Render(std::unique_ptr<flow::LayerTree> layer_tree) {
474474

475475
void Engine::UpdateSemantics(std::vector<blink::SemanticsNode> update) {
476476
blink::Threads::Platform()->PostTask(ftl::MakeCopyable(
477-
[ platform_view = platform_view_, update = std::move(update) ]() mutable {
477+
[ platform_view = std::shared_ptr<PlatformView>{platform_view_}, update = std::move(update) ]() mutable {
478478
if (platform_view)
479479
platform_view->UpdateSemantics(std::move(update));
480480
}));
@@ -487,7 +487,7 @@ void Engine::HandlePlatformMessage(
487487
return;
488488
}
489489
blink::Threads::Platform()->PostTask([
490-
platform_view = platform_view_, message = std::move(message)
490+
platform_view = std::shared_ptr<PlatformView>{platform_view_}, message = std::move(message)
491491
]() mutable {
492492
if (platform_view)
493493
platform_view->HandlePlatformMessage(std::move(message));

shell/common/engine.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class Engine : public blink::RuntimeDelegate {
9595
void HandleAssetPlatformMessage(ftl::RefPtr<blink::PlatformMessage> message);
9696
bool GetAssetAsBuffer(const std::string& name, std::vector<uint8_t>* data);
9797

98-
ftl::WeakPtr<PlatformView> platform_view_;
98+
std::weak_ptr<PlatformView> platform_view_;
9999
std::unique_ptr<Animator> animator_;
100100
std::unique_ptr<blink::RuntimeController> runtime_;
101101
tonic::DartErrorHandleType load_script_error_;

shell/common/platform_view.cc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ namespace shell {
1818

1919
PlatformView::PlatformView(std::unique_ptr<Rasterizer> rasterizer)
2020
: rasterizer_(std::move(rasterizer)),
21-
size_(SkISize::Make(0, 0)),
22-
weak_factory_(this) {}
21+
size_(SkISize::Make(0, 0)){}
2322

2423
PlatformView::~PlatformView() {
2524
blink::Threads::UI()->PostTask([] { Shell::Shared().PurgePlatformViews(); });
@@ -39,7 +38,7 @@ void PlatformView::CreateEngine() {
3938
// Subclasses should call this after the object is fully constructed.
4039
void PlatformView::PostAddToShellTask() {
4140
blink::Threads::UI()->PostTask(
42-
[self = GetWeakPtr()] { Shell::Shared().AddPlatformView(self); });
41+
[self = shared_from_this()] { Shell::Shared().AddPlatformView(self); });
4342
}
4443

4544
void PlatformView::DispatchPlatformMessage(
@@ -118,8 +117,8 @@ void PlatformView::NotifyDestroyed() {
118117
latch.Wait();
119118
}
120119

121-
ftl::WeakPtr<PlatformView> PlatformView::GetWeakPtr() {
122-
return weak_factory_.GetWeakPtr();
120+
std::weak_ptr<PlatformView> PlatformView::GetWeakPtr() {
121+
return shared_from_this();
123122
}
124123

125124
VsyncWaiter* PlatformView::GetVsyncWaiter() {

shell/common/platform_view.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace shell {
2222

2323
class Rasterizer;
2424

25-
class PlatformView {
25+
class PlatformView : public std::enable_shared_from_this<PlatformView> {
2626
public:
2727
struct SurfaceConfig {
2828
uint8_t red_bits = 8;
@@ -48,7 +48,7 @@ class PlatformView {
4848

4949
void NotifyDestroyed();
5050

51-
ftl::WeakPtr<PlatformView> GetWeakPtr();
51+
std::weak_ptr<PlatformView> GetWeakPtr();
5252

5353
// The VsyncWaiter will live at least as long as the PlatformView.
5454
virtual VsyncWaiter* GetVsyncWaiter();
@@ -81,8 +81,7 @@ class PlatformView {
8181
std::unique_ptr<VsyncWaiter> vsync_waiter_;
8282
SkISize size_;
8383

84-
private:
85-
ftl::WeakPtrFactory<PlatformView> weak_factory_;
84+
private:
8685

8786
FTL_DISALLOW_COPY_AND_ASSIGN(PlatformView);
8887
};

shell/common/shell.cc

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ bool IsInvalid(const ftl::WeakPtr<Rasterizer>& rasterizer) {
3333
return !rasterizer;
3434
}
3535

36-
bool IsViewInvalid(const ftl::WeakPtr<PlatformView>& platform_view) {
37-
return !platform_view;
36+
bool IsViewInvalid(const std::weak_ptr<PlatformView>& platform_view) {
37+
return !(platform_view.expired());
3838
}
3939

4040
template <typename T>
@@ -242,58 +242,41 @@ void Shell::GetRasterizers(std::vector<ftl::WeakPtr<Rasterizer>>* rasterizers) {
242242
*rasterizers = rasterizers_;
243243
}
244244

245-
void Shell::AddPlatformView(const ftl::WeakPtr<PlatformView>& platform_view) {
246-
FTL_DCHECK(ui_thread_checker_ &&
247-
ui_thread_checker_->IsCreationThreadCurrent());
245+
void Shell::AddPlatformView(const std::shared_ptr<PlatformView>& platform_view) {
246+
std::lock_guard<std::mutex> lk(platform_views_mutex_);
248247
if (platform_view) {
249248
platform_views_.push_back(platform_view);
250249
}
251250
}
252251

253252
void Shell::PurgePlatformViews() {
254-
FTL_DCHECK(ui_thread_checker_ &&
255-
ui_thread_checker_->IsCreationThreadCurrent());
253+
std::lock_guard<std::mutex> lk(platform_views_mutex_);
256254
platform_views_.erase(std::remove_if(platform_views_.begin(),
257255
platform_views_.end(), IsViewInvalid),
258256
platform_views_.end());
259257
}
260258

261259
void Shell::GetPlatformViews(
262-
std::vector<ftl::WeakPtr<PlatformView>>* platform_views) {
263-
FTL_DCHECK(ui_thread_checker_ &&
264-
ui_thread_checker_->IsCreationThreadCurrent());
260+
std::vector<std::weak_ptr<PlatformView>>* platform_views) {
261+
std::lock_guard<std::mutex> lk(platform_views_mutex_);
265262
*platform_views = platform_views_;
266263
}
267264

268265
void Shell::WaitForPlatformViewIds(
269266
std::vector<PlatformViewInfo>* platform_view_ids) {
270-
ftl::AutoResetWaitableEvent latch;
271-
272-
blink::Threads::UI()->PostTask([this, platform_view_ids, &latch]() {
273-
WaitForPlatformViewsIdsUIThread(platform_view_ids, &latch);
274-
});
275-
276-
latch.Wait();
277-
}
278-
279-
void Shell::WaitForPlatformViewsIdsUIThread(
280-
std::vector<PlatformViewInfo>* platform_view_ids,
281-
ftl::AutoResetWaitableEvent* latch) {
282-
std::vector<ftl::WeakPtr<PlatformView>> platform_views;
283-
GetPlatformViews(&platform_views);
284-
for (auto it = platform_views.begin(); it != platform_views.end(); it++) {
285-
PlatformView* view = it->get();
267+
std::lock_guard<std::mutex> lk(platform_views_mutex_);
268+
for (auto it = platform_views_.begin(); it != platform_views_.end(); it++) {
269+
std::shared_ptr <PlatformView> view{*it};
286270
if (!view) {
287271
// Skip dead views.
288272
continue;
289273
}
290274
PlatformViewInfo info;
291-
info.view_id = reinterpret_cast<uintptr_t>(view);
275+
info.view_id = reinterpret_cast<uintptr_t>(view.get());
292276
info.isolate_id = view->engine().GetUIIsolateMainPort();
293277
info.isolate_name = view->engine().GetUIIsolateName();
294278
platform_view_ids->push_back(info);
295279
}
296-
latch->Signal();
297280
}
298281

299282
void Shell::RunInPlatformView(uintptr_t view_id,
@@ -334,8 +317,9 @@ void Shell::RunInPlatformViewUIThread(uintptr_t view_id,
334317
*view_existed = false;
335318

336319
for (auto it = platform_views_.begin(); it != platform_views_.end(); it++) {
337-
PlatformView* view = it->get();
338-
if (reinterpret_cast<uintptr_t>(view) == view_id) {
320+
std::shared_ptr<PlatformView> view{*it};
321+
if (!view) continue;
322+
if (reinterpret_cast<uintptr_t>(view.get()) == view_id) {
339323
*view_existed = true;
340324
view->RunFromSource(assets_directory, main, packages);
341325
*dart_isolate_id = view->engine().GetUIIsolateMainPort();

shell/common/shell.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include "lib/ftl/synchronization/waitable_event.h"
1616
#include "lib/ftl/tasks/task_runner.h"
1717

18+
#include <mutex>
19+
1820
namespace shell {
1921

2022
class PlatformView;
@@ -43,10 +45,10 @@ class Shell {
4345
// List of PlatformViews.
4446

4547
// These APIs must only be accessed on UI thread.
46-
void AddPlatformView(const ftl::WeakPtr<PlatformView>& platform_view);
48+
void AddPlatformView(const std::shared_ptr<PlatformView>& platform_view);
4749
void PurgePlatformViews();
4850
void GetPlatformViews(
49-
std::vector<ftl::WeakPtr<PlatformView>>* platform_views);
51+
std::vector<std::weak_ptr<PlatformView>>* platform_views);
5052

5153
struct PlatformViewInfo {
5254
uintptr_t view_id;
@@ -76,10 +78,6 @@ class Shell {
7678
void InitGpuThread();
7779
void InitUIThread();
7880

79-
void WaitForPlatformViewsIdsUIThread(
80-
std::vector<PlatformViewInfo>* platform_views,
81-
ftl::AutoResetWaitableEvent* latch);
82-
8381
void RunInPlatformViewUIThread(uintptr_t view_id,
8482
const std::string& main,
8583
const std::string& packages,
@@ -101,7 +99,9 @@ class Shell {
10199
TracingController tracing_controller_;
102100

103101
std::vector<ftl::WeakPtr<Rasterizer>> rasterizers_;
104-
std::vector<ftl::WeakPtr<PlatformView>> platform_views_;
102+
std::vector<std::weak_ptr<PlatformView>> platform_views_;
103+
104+
std::mutex platform_views_mutex_;
105105

106106
FTL_DISALLOW_COPY_AND_ASSIGN(Shell);
107107
};

shell/platform/android/platform_view_android.cc

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ class PlatformMessageResponseAndroid : public blink::PlatformMessageResponse {
3737
ftl::RefPtr<PlatformMessageResponseAndroid> self(this);
3838
blink::Threads::Platform()->PostTask(
3939
ftl::MakeCopyable([ self, data = std::move(data) ]() mutable {
40-
if (!self->view_)
40+
std::shared_ptr<PlatformView> view{self->view_};
41+
if (!view)
4142
return;
42-
static_cast<PlatformViewAndroid*>(self->view_.get())
43+
static_cast<PlatformViewAndroid*>(view.get())
4344
->HandlePlatformMessageResponse(self->response_id_,
4445
std::move(data));
4546
}));
@@ -48,20 +49,21 @@ class PlatformMessageResponseAndroid : public blink::PlatformMessageResponse {
4849
void CompleteEmpty() override {
4950
ftl::RefPtr<PlatformMessageResponseAndroid> self(this);
5051
blink::Threads::Platform()->PostTask(ftl::MakeCopyable([self]() mutable {
51-
if (!self->view_)
52+
std::shared_ptr<PlatformView> view{self->view_};
53+
if (!view)
5254
return;
53-
static_cast<PlatformViewAndroid*>(self->view_.get())
55+
static_cast<PlatformViewAndroid*>(view.get())
5456
->HandlePlatformMessageEmptyResponse(self->response_id_);
5557
}));
5658
}
5759

5860
private:
5961
PlatformMessageResponseAndroid(int response_id,
60-
ftl::WeakPtr<PlatformView> view)
62+
std::weak_ptr<PlatformView> view)
6163
: response_id_(response_id), view_(view) {}
6264

6365
int response_id_;
64-
ftl::WeakPtr<PlatformView> view_;
66+
std::weak_ptr<PlatformView> view_;
6567
};
6668

6769
static std::unique_ptr<AndroidSurface> InitializePlatformSurfaceGL() {
@@ -125,6 +127,11 @@ static std::unique_ptr<AndroidSurface> InitializePlatformSurface() {
125127
PlatformViewAndroid::PlatformViewAndroid()
126128
: PlatformView(std::make_unique<GPURasterizer>(nullptr)),
127129
android_surface_(InitializePlatformSurface()) {
130+
}
131+
132+
PlatformViewAndroid::~PlatformViewAndroid() = default;
133+
134+
void PlatformViewAndroid::Attach() {
128135
CreateEngine();
129136

130137
// Eagerly setup the IO thread context. We have already setup the surface.
@@ -135,11 +142,8 @@ PlatformViewAndroid::PlatformViewAndroid()
135142
PostAddToShellTask();
136143
}
137144

138-
PlatformViewAndroid::~PlatformViewAndroid() = default;
139-
140145
void PlatformViewAndroid::Detach() {
141146
ReleaseSurface();
142-
delete this;
143147
}
144148

145149
void PlatformViewAndroid::SurfaceCreated(JNIEnv* env,

shell/platform/android/platform_view_android.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class PlatformViewAndroid : public PlatformView {
2828

2929
~PlatformViewAndroid() override;
3030

31+
void Attach();
32+
3133
void Detach();
3234

3335
void SurfaceCreated(JNIEnv* env, jobject jsurface, jint backgroundColor);

shell/platform/android/platform_view_android_jni.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "lib/ftl/arraysize.h"
1212
#include "lib/ftl/logging.h"
1313

14-
#define PLATFORM_VIEW reinterpret_cast<PlatformViewAndroid*>(platform_view)
14+
#define PLATFORM_VIEW (*reinterpret_cast<std::shared_ptr<PlatformViewAndroid>*>(platform_view))
1515

1616
namespace shell {
1717

@@ -52,15 +52,18 @@ void FlutterViewUpdateSemantics(JNIEnv* env,
5252
// Called By Java
5353

5454
static jlong Attach(JNIEnv* env, jclass clazz, jobject flutterView) {
55-
PlatformViewAndroid* view = new PlatformViewAndroid();
55+
auto view = new PlatformViewAndroid();
56+
auto storage = new std::shared_ptr<PlatformViewAndroid>(view);
5657
// Create a weak reference to the flutterView Java object so that we can make
5758
// calls into it later.
59+
view->Attach();
5860
view->set_flutter_view(fml::jni::JavaObjectWeakGlobalRef(env, flutterView));
59-
return reinterpret_cast<jlong>(view);
61+
return reinterpret_cast<jlong>(storage);
6062
}
6163

6264
static void Detach(JNIEnv* env, jobject jcaller, jlong platform_view) {
63-
return PLATFORM_VIEW->Detach();
65+
PLATFORM_VIEW->Detach();
66+
delete &PLATFORM_VIEW;
6467
}
6568

6669
static jstring GetObservatoryUri(JNIEnv* env, jclass clazz) {

shell/platform/darwin/desktop/flutter_window.mm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ - (void)setupPlatformView {
5656
FTL_DCHECK(_platformView == nullptr) << "The platform view must not already be set.";
5757

5858
_platformView.reset(new shell::PlatformViewMac(self.renderSurface));
59+
_platformView->Attach();
5960
_platformView->SetupResourceContextOnIOThread();
6061
_platformView->NotifyCreated(std::make_unique<shell::GPUSurfaceGL>(_platformView.get()));
6162
}

0 commit comments

Comments
 (0)