This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
/
Copy pathplatform_view.cc
208 lines (168 loc) · 6.48 KB
/
platform_view.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/shell/common/platform_view.h"
#include <utility>
#include "flutter/common/threads.h"
#include "flutter/lib/ui/painting/resource_context.h"
#include "flutter/shell/common/rasterizer.h"
#include "flutter/shell/common/vsync_waiter_fallback.h"
#include "lib/fxl/functional/make_copyable.h"
#include "third_party/skia/include/gpu/GrContextOptions.h"
#include "third_party/skia/include/gpu/gl/GrGLInterface.h"
namespace shell {
PlatformView::PlatformView(std::unique_ptr<Rasterizer> rasterizer)
: rasterizer_(std::move(rasterizer)), size_(SkISize::Make(0, 0)) {
rasterizer_->SetTextureRegistry(&texture_registry_);
Shell::Shared().AddPlatformView(this);
}
PlatformView::~PlatformView() {
Shell::Shared().RemovePlatformView(this);
Rasterizer* rasterizer = rasterizer_.release();
blink::Threads::Gpu()->PostTask([rasterizer]() { delete rasterizer; });
Engine* engine = engine_.release();
blink::Threads::UI()->PostTask([engine]() { delete engine; });
}
void PlatformView::SetRasterizer(std::unique_ptr<Rasterizer> rasterizer) {
Rasterizer* r = rasterizer_.release();
blink::Threads::Gpu()->PostTask([r]() { delete r; });
rasterizer_ = std::move(rasterizer);
rasterizer_->SetTextureRegistry(&texture_registry_);
engine_->set_rasterizer(rasterizer_->GetWeakRasterizerPtr());
}
void PlatformView::CreateEngine() {
engine_.reset(new Engine(this));
}
void PlatformView::DispatchPlatformMessage(
fxl::RefPtr<blink::PlatformMessage> message) {
blink::Threads::UI()->PostTask(
[ engine = engine_->GetWeakPtr(), message = std::move(message) ] {
if (engine) {
engine->DispatchPlatformMessage(message);
}
});
}
void PlatformView::DispatchSemanticsAction(int32_t id,
blink::SemanticsAction action) {
blink::Threads::UI()->PostTask(
[ engine = engine_->GetWeakPtr(), id, action ] {
if (engine) {
engine->DispatchSemanticsAction(
id, static_cast<blink::SemanticsAction>(action));
}
});
}
void PlatformView::SetSemanticsEnabled(bool enabled) {
blink::Threads::UI()->PostTask([ engine = engine_->GetWeakPtr(), enabled ] {
if (engine)
engine->SetSemanticsEnabled(enabled);
});
}
void PlatformView::NotifyCreated(std::unique_ptr<Surface> surface) {
NotifyCreated(std::move(surface), []() {});
}
void PlatformView::NotifyCreated(std::unique_ptr<Surface> surface,
fxl::Closure caller_continuation) {
fxl::AutoResetWaitableEvent latch;
auto ui_continuation = fxl::MakeCopyable([
this, //
surface = std::move(surface), //
caller_continuation, //
&latch
]() mutable {
auto gpu_continuation = fxl::MakeCopyable([
this, //
surface = std::move(surface), //
caller_continuation, //
&latch
]() mutable {
// Runs on the GPU Thread. So does the Caller Continuation.
rasterizer_->Setup(std::move(surface), caller_continuation, &latch);
});
// Runs on the UI Thread.
engine_->OnOutputSurfaceCreated(std::move(gpu_continuation));
});
// Runs on the Platform Thread.
blink::Threads::UI()->PostTask(std::move(ui_continuation));
latch.Wait();
}
void PlatformView::NotifyDestroyed() {
fxl::AutoResetWaitableEvent latch;
auto engine_continuation = [this, &latch]() {
rasterizer_->Teardown(&latch);
};
blink::Threads::UI()->PostTask([this, engine_continuation]() {
engine_->OnOutputSurfaceDestroyed(engine_continuation);
});
latch.Wait();
}
std::weak_ptr<PlatformView> PlatformView::GetWeakPtr() {
return shared_from_this();
}
VsyncWaiter* PlatformView::GetVsyncWaiter() {
if (!vsync_waiter_)
vsync_waiter_ = std::make_unique<VsyncWaiterFallback>();
return vsync_waiter_.get();
}
void PlatformView::UpdateSemantics(std::vector<blink::SemanticsNode> update) {}
void PlatformView::HandlePlatformMessage(
fxl::RefPtr<blink::PlatformMessage> message) {
if (auto response = message->response())
response->CompleteEmpty();
}
void PlatformView::RegisterTexture(std::shared_ptr<flow::Texture> texture) {
ASSERT_IS_PLATFORM_THREAD
blink::Threads::Gpu()->PostTask([this, texture]() {
rasterizer_->GetTextureRegistry().RegisterTexture(texture);
});
}
void PlatformView::UnregisterTexture(int64_t texture_id) {
ASSERT_IS_PLATFORM_THREAD
blink::Threads::Gpu()->PostTask([this, texture_id]() {
rasterizer_->GetTextureRegistry().UnregisterTexture(texture_id);
});
}
void PlatformView::MarkTextureFrameAvailable(int64_t texture_id) {
ASSERT_IS_PLATFORM_THREAD
blink::Threads::UI()->PostTask([this]() { engine_->ScheduleFrame(false); });
}
void PlatformView::SetupResourceContextOnIOThread() {
fxl::AutoResetWaitableEvent latch;
blink::Threads::IO()->PostTask(
[this, &latch]() { SetupResourceContextOnIOThreadPerform(&latch); });
latch.Wait();
}
void PlatformView::SetupResourceContextOnIOThreadPerform(
fxl::AutoResetWaitableEvent* latch) {
if (blink::ResourceContext::Get() != nullptr) {
// The resource context was already setup. This could happen if platforms
// try to setup a context multiple times, or, if there are multiple platform
// views. In any case, there is nothing else to do. So just signal the
// latch.
latch->Signal();
return;
}
bool current = ResourceContextMakeCurrent();
if (!current) {
FXL_DLOG(WARNING)
<< "WARNING: Could not setup a context on the resource loader.";
latch->Signal();
return;
}
GrContextOptions options;
// There is currently a bug with doing GPU YUV to RGB conversions on the IO
// thread. The necessary work isn't being flushed or synchronized with the
// other threads correctly, so the textures end up blank. For now, suppress
// that feature, which will cause texture uploads to do CPU YUV conversion.
options.fDisableGpuYUVConversion = true;
blink::ResourceContext::Set(GrContext::Create(
GrBackend::kOpenGL_GrBackend,
reinterpret_cast<GrBackendContext>(GrGLCreateNativeInterface()),
options));
// Do not cache textures created by the image decoder. These textures should
// be deleted when they are no longer referenced by an SkImage.
if (blink::ResourceContext::Get())
blink::ResourceContext::Get()->setResourceCacheLimits(0, 0);
latch->Signal();
}
} // namespace shell