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

Commit da35807

Browse files
committed
Add synchronous |PostTask| function.
1 parent 7973865 commit da35807

File tree

4 files changed

+31
-34
lines changed

4 files changed

+31
-34
lines changed

fml/task_runner.cc

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "flutter/fml/message_loop.h"
1414
#include "flutter/fml/message_loop_impl.h"
1515
#include "flutter/fml/message_loop_task_queues.h"
16+
#include "flutter/fml/synchronization/waitable_event.h"
1617

1718
namespace fml {
1819

@@ -64,9 +65,19 @@ void TaskRunner::RunNowOrPostTask(fml::RefPtr<fml::TaskRunner> runner,
6465
}
6566
}
6667

67-
void TaskRunner::RunNowOrPostSyncTask(fml::RefPtr<fml::TaskRunner> runner,
68+
void TaskRunner::RunNowOrPostTaskSync(fml::RefPtr<fml::TaskRunner> runner,
6869
const fml::closure& task) {
70+
FML_DCHECK(runner);
6971
std::scoped_lock lock(g_thread_merging_lock);
70-
RunNowOrPostTask(runner, task);
72+
if (runner->RunsTasksOnCurrentThread()) {
73+
task();
74+
} else {
75+
fml::AutoResetWaitableEvent latch;
76+
runner->PostTask([&] {
77+
task();
78+
latch.Signal();
79+
});
80+
latch.Wait();
81+
}
7182
}
7283
} // namespace fml

fml/task_runner.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ class TaskRunner : public fml::RefCountedThreadSafe<TaskRunner>,
6666
static void RunNowOrPostTask(fml::RefPtr<fml::TaskRunner> runner,
6767
const fml::closure& task);
6868

69-
/// Similar to |RunNowOrPostTask|, but it needs to synchronize with thread
70-
/// merging operation.
71-
static void RunNowOrPostSyncTask(fml::RefPtr<fml::TaskRunner> runner,
69+
/// Similar to |RunNowOrPostTask|, but it needs to mutex with thread
70+
/// merging operation and post the \p tast synchronously.
71+
static void RunNowOrPostTaskSync(fml::RefPtr<fml::TaskRunner> runner,
7272
const fml::closure& task);
7373

7474
protected:

shell/common/platform_view.cc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,14 @@ void PlatformView::NotifyCreated() {
6262
// Using the weak pointer is illegal. But, we are going to introduce a latch
6363
// so that the platform view is not collected till the surface is obtained.
6464
auto* platform_view = this;
65-
fml::ManualResetWaitableEvent latch;
66-
fml::TaskRunner::RunNowOrPostSyncTask(
67-
task_runners_.GetRasterTaskRunner(), [platform_view, &surface, &latch]() {
65+
fml::TaskRunner::RunNowOrPostTaskSync(
66+
task_runners_.GetRasterTaskRunner(), [platform_view, &surface]() {
6867
surface = platform_view->CreateRenderingSurface();
6968
if (surface && !surface->IsValid()) {
7069
surface.reset();
7170
}
72-
latch.Signal();
7371
});
74-
latch.Wait();
72+
7573
if (!surface) {
7674
FML_LOG(ERROR) << "Failed to create platform view rendering surface";
7775
return;

shell/platform/android/platform_view_android.cc

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,12 @@ void PlatformViewAndroid::NotifyCreated(
9797
if (android_surface_) {
9898
InstallFirstFrameCallback();
9999

100-
fml::AutoResetWaitableEvent latch;
101-
fml::TaskRunner::RunNowOrPostSyncTask(
100+
fml::TaskRunner::RunNowOrPostTaskSync(
102101
task_runners_.GetRasterTaskRunner(),
103-
[&latch, surface = android_surface_.get(),
102+
[surface = android_surface_.get(),
104103
native_window = std::move(native_window)]() {
105104
surface->SetNativeWindow(native_window);
106-
latch.Signal();
107105
});
108-
latch.Wait();
109106
}
110107

111108
PlatformView::NotifyCreated();
@@ -114,46 +111,37 @@ void PlatformViewAndroid::NotifyCreated(
114111
void PlatformViewAndroid::NotifySurfaceWindowChanged(
115112
fml::RefPtr<AndroidNativeWindow> native_window) {
116113
if (android_surface_) {
117-
fml::AutoResetWaitableEvent latch;
118-
fml::TaskRunner::RunNowOrPostSyncTask(
114+
fml::TaskRunner::RunNowOrPostTaskSync(
119115
task_runners_.GetRasterTaskRunner(),
120-
[&latch, surface = android_surface_.get(),
116+
[surface = android_surface_.get(),
121117
native_window = std::move(native_window)]() {
122118
surface->TeardownOnScreenContext();
123119
surface->SetNativeWindow(native_window);
124-
latch.Signal();
125120
});
126-
latch.Wait();
127121
}
128122
}
129123

130124
void PlatformViewAndroid::NotifyDestroyed() {
131125
PlatformView::NotifyDestroyed();
132126

133127
if (android_surface_) {
134-
fml::AutoResetWaitableEvent latch;
135-
fml::TaskRunner::RunNowOrPostSyncTask(
136-
task_runners_.GetRasterTaskRunner(),
137-
[&latch, surface = android_surface_.get()]() {
138-
surface->TeardownOnScreenContext();
139-
latch.Signal();
140-
});
141-
latch.Wait();
128+
fml::TaskRunner::RunNowOrPostTaskSync(task_runners_.GetRasterTaskRunner(),
129+
[surface = android_surface_.get()]() {
130+
surface->TeardownOnScreenContext();
131+
});
142132
}
143133
}
144134

145135
void PlatformViewAndroid::NotifyChanged(const SkISize& size) {
146136
if (!android_surface_) {
147137
return;
148138
}
149-
fml::AutoResetWaitableEvent latch;
150-
fml::TaskRunner::RunNowOrPostSyncTask(
151-
task_runners_.GetRasterTaskRunner(), //
152-
[&latch, surface = android_surface_.get(), size]() {
139+
140+
fml::TaskRunner::RunNowOrPostTaskSync(
141+
task_runners_.GetRasterTaskRunner(),
142+
[surface = android_surface_.get(), size]() {
153143
surface->OnScreenSurfaceResize(size);
154-
latch.Signal();
155144
});
156-
latch.Wait();
157145
}
158146

159147
void PlatformViewAndroid::DispatchPlatformMessage(JNIEnv* env,

0 commit comments

Comments
 (0)