Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions shell/common/platform_view_service_protocol.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ void PlatformViewServiceProtocol::RegisterHook(bool running_precompiled_code) {
}
Dart_RegisterRootServiceRequestCallback(kRunInViewExtensionName, &RunInView,
nullptr);
// [benchmark helper] Wait for the UI Thread to idle.
Dart_RegisterRootServiceRequestCallback(kWaitUIThreadIdleExtensionName,
&WaitUIThreadIdle, nullptr);
}

const char* PlatformViewServiceProtocol::kRunInViewExtensionName =
Expand Down Expand Up @@ -202,11 +205,10 @@ bool PlatformViewServiceProtocol::ListViews(const char* method,
intptr_t num_params,
void* user_data,
const char** json_object) {
// Ask the Shell for the list of platform views. This will run a task on
// the UI thread before returning.
// Ask the Shell for the list of platform views.
Shell& shell = Shell::Shared();
std::vector<Shell::PlatformViewInfo> platform_views;
shell.WaitForPlatformViewIds(&platform_views);
shell.GetPlatformViewIds(&platform_views);

std::stringstream response;

Expand Down Expand Up @@ -314,4 +316,29 @@ void PlatformViewServiceProtocol::ScreenshotGpuTask(SkBitmap* bitmap) {
canvas->flush();
}

const char* PlatformViewServiceProtocol::kWaitUIThreadIdleExtensionName =
"_flutter.waitUIThreadIdle";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking this is not waiting for the UI thread to idle it is more wait for UI thread to finish processing the first event, I don't have a better name for it though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about FlushUIThreadTasks?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not actually flushing all the tasks, but just the first one.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure? The implementation calls blink::Threads::UI()->PostTask, which will queue up a task. Given that the message queue is FIFO, all tasks that were scheduled prior to this task will execute first. No?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I misunderstood the meaning of Tasks, you are right. It will wait for all the previous tasks to be flushed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


// This API should not be invoked by production code.
// It can potentially starve the service isolate if the main isolate pauses
// at a breakpoint or is in an infinite loop.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth mentioning that this method must be invoked from the VM service thread and blocks it until UI thread tasks are processed.

Copy link
Contributor Author

@B3rn475 B3rn475 Jul 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add this description even to the other services? some of them are blocking too.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be great. We have comments like that in shell.h.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

bool PlatformViewServiceProtocol::WaitUIThreadIdle(const char* method,
const char** param_keys,
const char** param_values,
intptr_t num_params,
void* user_data,
const char** json_object) {
ftl::AutoResetWaitableEvent latch;
blink::Threads::UI()->PostTask([&latch]() {
// This task is empty because we just need to synchronize this RPC with the
// UI Thread
latch.Signal();
});

latch.Wait();

*json_object = strdup("{\"type\":\"Success\"}");
return true;
}

} // namespace shell
11 changes: 11 additions & 0 deletions shell/common/platform_view_service_protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ class PlatformViewServiceProtocol {
void* user_data,
const char** json_object);
static void ScreenshotGpuTask(SkBitmap* bitmap);

// This API should not be invoked by production code.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto re calling from VM isolate thread

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

// It can potentially starve the service isolate if the main isolate pauses
// at a breakpoint or is in an infinite loop.
static const char* kWaitUIThreadIdleExtensionName;
static bool WaitUIThreadIdle(const char* method,
const char** param_keys,
const char** param_values,
intptr_t num_params,
void* user_data,
const char** json_object);
};

} // namespace shell
Expand Down
2 changes: 1 addition & 1 deletion shell/common/shell.cc
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ void Shell::GetPlatformViews(
*platform_views = platform_views_;
}

void Shell::WaitForPlatformViewIds(
void Shell::GetPlatformViewIds(
std::vector<PlatformViewInfo>* platform_view_ids) {
std::lock_guard<std::mutex> lk(platform_views_mutex_);
for (auto it = platform_views_.begin(); it != platform_views_.end(); it++) {
Expand Down
4 changes: 2 additions & 2 deletions shell/common/shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Shell {

// List of PlatformViews.

// These APIs must only be accessed on UI thread.
// These APIs can be called from any thread.
void AddPlatformView(const std::shared_ptr<PlatformView>& platform_view);
void PurgePlatformViews();
void GetPlatformViews(
Expand All @@ -58,7 +58,7 @@ class Shell {

// These APIs can be called from any thread.
// Return the list of platform view ids at the time of this call.
void WaitForPlatformViewIds(std::vector<PlatformViewInfo>* platform_view_ids);
void GetPlatformViewIds(std::vector<PlatformViewInfo>* platform_view_ids);

// Attempt to run a script inside a flutter view indicated by |view_id|.
// Will set |view_existed| to true if the view was found and false otherwise.
Expand Down