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

Commit e37bd27

Browse files
authored
Fix weak pointer use violations in shell and platform view. (#8046)
1 parent dd80fc9 commit e37bd27

File tree

4 files changed

+30
-16
lines changed

4 files changed

+30
-16
lines changed

shell/common/platform_view.cc

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,29 @@ void PlatformView::SetViewportMetrics(const blink::ViewportMetrics& metrics) {
6060
}
6161

6262
void PlatformView::NotifyCreated() {
63-
delegate_.OnPlatformViewCreated(CreateRenderingSurface());
63+
std::unique_ptr<Surface> surface;
64+
65+
// Threading: We want to use the platform view on the non-platform thread.
66+
// Using the weak pointer is illegal. But, we are going to introduce a latch
67+
// so that the platform view is not collected till the surface is obtained.
68+
auto* platform_view = this;
69+
fml::ManualResetWaitableEvent latch;
70+
fml::TaskRunner::RunNowOrPostTask(
71+
task_runners_.GetGPUTaskRunner(), [platform_view, &surface, &latch]() {
72+
surface = platform_view->CreateRenderingSurface();
73+
latch.Signal();
74+
});
75+
latch.Wait();
76+
delegate_.OnPlatformViewCreated(std::move(surface));
6477
}
6578

6679
void PlatformView::NotifyDestroyed() {
6780
delegate_.OnPlatformViewDestroyed();
6881
}
6982

7083
sk_sp<GrContext> PlatformView::CreateResourceContext() const {
71-
#ifndef OS_FUCHSIA
7284
FML_DLOG(WARNING) << "This platform does not setup the resource "
7385
"context on the IO thread for async texture uploads.";
74-
#endif // OS_FUCHSIA
7586
return nullptr;
7687
}
7788

shell/common/platform_view.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ class PlatformView {
123123
SkISize size_;
124124
fml::WeakPtrFactory<PlatformView> weak_factory_;
125125

126+
// Unlike all other methods on the platform view, this is called on the GPU
127+
// task runner.
126128
virtual std::unique_ptr<Surface> CreateRenderingSurface();
127129

128130
private:

shell/common/shell.cc

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -446,26 +446,27 @@ void Shell::OnPlatformViewCreated(std::unique_ptr<Surface> surface) {
446446
fml::TaskRunner::RunNowOrPostTask(gpu_task_runner, gpu_task);
447447
};
448448

449-
auto io_task = [io_manager = io_manager_->GetWeakPtr(),
450-
platform_view = platform_view_->GetWeakPtr(),
449+
// Threading: Capture platform view by raw pointer and not the weak pointer.
450+
// We are going to use the pointer on the IO thread which is not safe with a
451+
// weak pointer. However, we are preventing the platform view from being
452+
// collected by using a latch.
453+
auto* platform_view = platform_view_.get();
454+
455+
FML_DCHECK(platform_view);
456+
457+
auto io_task = [io_manager = io_manager_->GetWeakPtr(), platform_view,
451458
ui_task_runner = task_runners_.GetUITaskRunner(), ui_task] {
452-
if (io_manager) {
459+
if (io_manager && !io_manager->GetResourceContext()) {
453460
io_manager->NotifyResourceContextAvailable(
454-
platform_view ? platform_view->CreateResourceContext() : nullptr);
461+
platform_view->CreateResourceContext());
455462
}
456463
// Step 1: Next, post a task on the UI thread to tell the engine that it has
457464
// an output surface.
458465
fml::TaskRunner::RunNowOrPostTask(ui_task_runner, ui_task);
459466
};
460467

461-
// Step 0: If the IOManager doesn't already have a ResourceContext, tell the
462-
// IO thread that the PlatformView can make one for it now.
463-
// Otherwise, jump right to step 1 on the UI thread.
464-
if (!io_manager_->GetResourceContext()) {
465-
fml::TaskRunner::RunNowOrPostTask(task_runners_.GetIOTaskRunner(), io_task);
466-
} else {
467-
fml::TaskRunner::RunNowOrPostTask(task_runners_.GetUITaskRunner(), ui_task);
468-
}
468+
fml::TaskRunner::RunNowOrPostTask(task_runners_.GetIOTaskRunner(), io_task);
469+
469470
latch.Wait();
470471
}
471472

shell/common/surface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
namespace shell {
1616

1717
/// Represents a Frame that has been fully configured for the underlying client
18-
/// rendering API. A frame may only be sumitted once.
18+
/// rendering API. A frame may only be submitted once.
1919
class SurfaceFrame {
2020
public:
2121
using SubmitCallback =

0 commit comments

Comments
 (0)