Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
reactor_gles.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
7#include <algorithm>
8
10#include "fml/closure.h"
11#include "fml/logging.h"
13
14namespace impeller {
15
16// static
17std::optional<ReactorGLES::GLStorage> ReactorGLES::CreateGLHandle(
18 const ProcTableGLES& gl,
20 GLStorage handle = GLStorage{.handle = GL_NONE};
21 switch (type) {
23 return std::nullopt;
25 gl.GenTextures(1u, &handle.handle);
26 return handle;
28 gl.GenBuffers(1u, &handle.handle);
29 return handle;
31 return GLStorage{.handle = gl.CreateProgram()};
33 gl.GenRenderbuffers(1u, &handle.handle);
34 return handle;
36 gl.GenFramebuffers(1u, &handle.handle);
37 return handle;
39 return GLStorage{.sync = gl.FenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0)};
40 }
41 return std::nullopt;
42}
43
44// static
45bool ReactorGLES::CollectGLHandle(const ProcTableGLES& gl,
47 ReactorGLES::GLStorage handle) {
48 switch (type) {
50 return false;
52 gl.DeleteTextures(1u, &handle.handle);
53 return true;
55 gl.DeleteBuffers(1u, &handle.handle);
56 return true;
58 gl.DeleteProgram(handle.handle);
59 return true;
61 gl.DeleteRenderbuffers(1u, &handle.handle);
62 return true;
64 gl.DeleteFramebuffers(1u, &handle.handle);
65 return true;
67 gl.DeleteSync(handle.sync);
68 break;
69 }
70 return false;
71}
72
73ReactorGLES::ReactorGLES(std::unique_ptr<ProcTableGLES> gl)
74 : proc_table_(std::move(gl)) {
75 if (!proc_table_ || !proc_table_->IsValid()) {
76 VALIDATION_LOG << "Proc table was invalid.";
77 return;
78 }
79 can_set_debug_labels_ = proc_table_->GetDescription()->HasDebugExtension();
80 is_valid_ = true;
81}
82
84 if (CanReactOnCurrentThread()) {
85 for (auto& handle : handles_) {
86 if (handle.second.name.has_value()) {
87 CollectGLHandle(*proc_table_, handle.first.GetType(),
88 handle.second.name.value());
89 }
90 }
91 proc_table_->Flush();
92 }
93}
94
96 return is_valid_;
97}
98
100 return can_set_debug_labels_;
101}
102
103ReactorGLES::WorkerID ReactorGLES::AddWorker(std::weak_ptr<Worker> worker) {
104 Lock lock(workers_mutex_);
105 auto id = WorkerID{};
106 workers_[id] = std::move(worker);
107 return id;
108}
109
111 Lock lock(workers_mutex_);
112 return workers_.erase(worker) == 1;
113}
114
115bool ReactorGLES::HasPendingOperations() const {
116 auto thread_id = std::this_thread::get_id();
117 Lock ops_lock(ops_mutex_);
118 auto it = ops_.find(thread_id);
119 return it != ops_.end() ? !it->second.empty() : false;
120}
121
124 return *proc_table_;
125}
126
127std::optional<ReactorGLES::GLStorage> ReactorGLES::GetHandle(
128 const HandleGLES& handle) const {
129 if (handle.untracked_id_.has_value()) {
130 return ReactorGLES::GLStorage{.integer = handle.untracked_id_.value()};
131 }
132
133 ReaderLock handles_lock(handles_mutex_);
134 if (auto found = handles_.find(handle); found != handles_.end()) {
135 if (found->second.pending_collection) {
137 << "Attempted to acquire a handle that was pending collection.";
138 return std::nullopt;
139 }
140 std::optional<ReactorGLES::GLStorage> name = found->second.name;
141 if (!name.has_value()) {
142 VALIDATION_LOG << "Attempt to acquire a handle outside of an operation.";
143 return std::nullopt;
144 }
145 return name;
146 }
147 VALIDATION_LOG << "Attempted to acquire an invalid GL handle.";
148 return std::nullopt;
149}
150
151std::optional<GLuint> ReactorGLES::GetGLHandle(const HandleGLES& handle) const {
152 if (handle.GetType() == HandleType::kFence) {
153 return std::nullopt;
154 }
155 std::optional<ReactorGLES::GLStorage> gl_handle = GetHandle(handle);
156 if (gl_handle.has_value()) {
157 return gl_handle->handle;
158 }
159 return std::nullopt;
160}
161
162std::optional<GLsync> ReactorGLES::GetGLFence(const HandleGLES& handle) const {
163 if (handle.GetType() != HandleType::kFence) {
164 return std::nullopt;
165 }
166 std::optional<ReactorGLES::GLStorage> gl_handle = GetHandle(handle);
167 if (gl_handle.has_value()) {
168 return gl_handle->sync;
169 }
170 return std::nullopt;
171}
172
173bool ReactorGLES::AddOperation(Operation operation, bool defer) {
174 if (!operation) {
175 return false;
176 }
177 auto thread_id = std::this_thread::get_id();
178 {
179 Lock ops_lock(ops_mutex_);
180 ops_[thread_id].emplace_back(std::move(operation));
181 }
182 // Attempt a reaction if able but it is not an error if this isn't possible.
183 if (!defer) {
184 [[maybe_unused]] auto result = React();
185 }
186 return true;
187}
188
190 const fml::closure& callback) {
191 if (handle.IsDead()) {
192 return false;
193 }
194 FML_DCHECK(!handle.untracked_id_.has_value());
195 WriterLock handles_lock(handles_mutex_);
196 if (auto found = handles_.find(handle); found != handles_.end()) {
197 found->second.callback = fml::ScopedCleanupClosure(callback);
198 return true;
199 }
200 return false;
201}
202
204 FML_DCHECK(CanReactOnCurrentThread());
205 auto new_handle = HandleGLES::Create(type);
206 std::optional<ReactorGLES::GLStorage> gl_handle =
207 CreateGLHandle(GetProcTable(), type);
208 if (gl_handle.has_value()) {
209 new_handle.untracked_id_ = gl_handle.value().integer;
210 }
211 return new_handle;
212}
213
215 if (type == HandleType::kUnknown) {
216 return HandleGLES::DeadHandle();
217 }
218 auto new_handle = HandleGLES::Create(type);
219 if (new_handle.IsDead()) {
220 return HandleGLES::DeadHandle();
221 }
222
223 std::optional<ReactorGLES::GLStorage> gl_handle;
224 if (external_handle != GL_NONE) {
225 gl_handle = ReactorGLES::GLStorage{.handle = external_handle};
226 } else if (CanReactOnCurrentThread()) {
227 gl_handle = CreateGLHandle(GetProcTable(), type);
228 }
229
230 WriterLock handles_lock(handles_mutex_);
231 handles_[new_handle] = LiveHandle{gl_handle};
232 return new_handle;
233}
234
236 if (handle.IsDead()) {
237 return;
238 }
239 if (handle.untracked_id_.has_value()) {
240 LiveHandle live_handle(GLStorage{.integer = handle.untracked_id_.value()});
241 live_handle.pending_collection = true;
242 WriterLock handles_lock(handles_mutex_);
243 handles_[handle] = std::move(live_handle);
244 } else {
245 WriterLock handles_lock(handles_mutex_);
246 if (auto found = handles_.find(handle); found != handles_.end()) {
247 if (!found->second.pending_collection) {
248 handles_to_collect_count_ += 1;
249 }
250 found->second.pending_collection = true;
251 }
252 }
253}
254
256 if (!CanReactOnCurrentThread()) {
257 return false;
258 }
259 TRACE_EVENT0("impeller", "ReactorGLES::React");
260 while (HasPendingOperations()) {
261 if (!ReactOnce()) {
262 return false;
263 }
264 }
265 return true;
266}
267
287
288bool ReactorGLES::ReactOnce() {
289 if (!IsValid()) {
290 return false;
291 }
292 TRACE_EVENT0("impeller", __FUNCTION__);
293 return ConsolidateHandles() && FlushOps();
294}
295
296bool ReactorGLES::ConsolidateHandles() {
297 TRACE_EVENT0("impeller", __FUNCTION__);
298 const auto& gl = GetProcTable();
299 std::thread::id current_thread = std::this_thread::get_id();
300 std::vector<std::tuple<HandleGLES, std::optional<GLStorage>>>
301 handles_to_delete;
302 std::vector<std::tuple<DebugResourceType, GLint, std::string>>
303 handles_to_name;
304 {
305 WriterLock handles_lock(handles_mutex_);
306 handles_to_delete.reserve(handles_to_collect_count_);
307 handles_to_collect_count_ = 0;
308 for (auto& handle : handles_) {
309 // Collect dead handles.
310 if (handle.second.pending_collection) {
311 // Some objects can only be deleted on a specific thread. Collection
312 // of these objects will be deferred until the owner thead executes
313 // a reaction and triggers collection of handles.
314 const auto& owner_thread = handle.first.owner_thread_;
315 if (owner_thread.has_value() && *owner_thread != current_thread) {
316 continue;
317 }
318 handles_to_delete.emplace_back(
319 std::make_tuple(handle.first, handle.second.name));
320 continue;
321 }
322 // Create live handles.
323 if (!handle.second.name.has_value()) {
324 auto gl_handle = CreateGLHandle(gl, handle.first.GetType());
325 if (!gl_handle) {
326 VALIDATION_LOG << "Could not create GL handle.";
327 return false;
328 }
329 handle.second.name = gl_handle;
330 }
331 // Set pending debug labels.
332 if (handle.second.pending_debug_label.has_value() &&
333 handle.first.GetType() != HandleType::kFence) {
334 handles_to_name.emplace_back(std::make_tuple(
335 ToDebugResourceType(handle.first.GetType()),
336 handle.second.name.value().handle,
337 std::move(handle.second.pending_debug_label.value())));
338 handle.second.pending_debug_label = std::nullopt;
339 }
340 }
341 for (const auto& handle_to_delete : handles_to_delete) {
342 handles_.erase(std::get<0>(handle_to_delete));
343 }
344 }
345
346 for (const auto& handle : handles_to_name) {
347 gl.SetDebugLabel(std::get<0>(handle), std::get<1>(handle),
348 std::get<2>(handle));
349 }
350 for (const auto& handle : handles_to_delete) {
351 const std::optional<GLStorage>& storage = std::get<1>(handle);
352 // This could be false if the handle was created and collected without
353 // use. We still need to get rid of map entry.
354 if (storage.has_value()) {
355 CollectGLHandle(gl, std::get<0>(handle).GetType(), storage.value());
356 }
357 }
358
359 return true;
360}
361
362bool ReactorGLES::FlushOps() {
363 TRACE_EVENT0("impeller", __FUNCTION__);
364
365#ifdef IMPELLER_DEBUG
366 // glDebugMessageControl sometimes must be called before glPushDebugGroup:
367 // https://github.com/flutter/flutter/issues/135715#issuecomment-1740153506
368 SetupDebugGroups();
369#endif
370
371 // Do NOT hold the ops or handles locks while performing operations in case
372 // the ops enqueue more ops.
373 decltype(ops_)::mapped_type ops;
374 auto thread_id = std::this_thread::get_id();
375 {
376 Lock ops_lock(ops_mutex_);
377 std::swap(ops_[thread_id], ops);
378 }
379 for (const auto& op : ops) {
380 TRACE_EVENT0("impeller", "ReactorGLES::Operation");
381 op(*this);
382 }
383 return true;
384}
385
386void ReactorGLES::SetupDebugGroups() {
387 // Setup of a default active debug group: Filter everything in.
388 if (can_set_debug_labels_) {
389 proc_table_->DebugMessageControlKHR(GL_DONT_CARE, // source
390 GL_DONT_CARE, // type
391 GL_DONT_CARE, // severity
392 0, // count
393 nullptr, // ids
394 GL_TRUE); // enabled
395 }
396}
397
399 std::string_view label) {
401 if (!can_set_debug_labels_) {
402 return;
403 }
404 if (handle.IsDead()) {
405 return;
406 }
407 if (handle.untracked_id_.has_value()) {
408 FML_DCHECK(CanReactOnCurrentThread());
409 const auto& gl = GetProcTable();
410 gl.SetDebugLabel(ToDebugResourceType(handle.GetType()),
411 handle.untracked_id_.value(), label);
412 } else {
413 WriterLock handles_lock(handles_mutex_);
414 if (auto found = handles_.find(handle); found != handles_.end()) {
415 found->second.pending_debug_label = label;
416 }
417 }
418}
419
420bool ReactorGLES::CanReactOnCurrentThread() const {
421 std::vector<WorkerID> dead_workers;
422 Lock lock(workers_mutex_);
423 for (const auto& worker : workers_) {
424 auto worker_ptr = worker.second.lock();
425 if (!worker_ptr) {
426 dead_workers.push_back(worker.first);
427 continue;
428 }
429 if (worker_ptr->CanReactorReactOnCurrentThreadNow(*this)) {
430 return true;
431 }
432 }
433 for (const auto& worker_id : dead_workers) {
434 workers_.erase(worker_id);
435 }
436 return false;
437}
438
439} // namespace impeller
GLenum type
Wraps a closure that is invoked in the destructor unless released by the caller.
Definition closure.h:32
Represents a handle to an underlying OpenGL object. Unlike OpenGL object handles, these handles can b...
Definition handle_gles.h:42
constexpr bool IsDead() const
Determines if the handle is dead.
Definition handle_gles.h:58
HandleType GetType() const
Definition handle_gles.h:79
static HandleGLES DeadHandle()
Creates a dead handle.
Definition handle_gles.h:49
bool RegisterCleanupCallback(const HandleGLES &handle, const fml::closure &callback)
Register a cleanup callback that will be invokved with the provided user data when the handle is dest...
bool CanSetDebugLabels() const
Whether the device is capable of writing debug labels.
std::optional< GLuint > GetGLHandle(const HandleGLES &handle) const
Returns the OpenGL handle for a reactor handle if one is available. This is typically only safe to ca...
ReactorGLES(std::unique_ptr< ProcTableGLES > gl)
Create a new reactor. There are expensive and only one per application instance is necessary.
HandleGLES CreateUntrackedHandle(HandleType type) const
Create a handle that is not managed by ReactorGLES.
std::optional< GLsync > GetGLFence(const HandleGLES &handle) const
void CollectHandle(HandleGLES handle)
Collect a reactor handle.
HandleGLES CreateHandle(HandleType type, GLuint external_handle=GL_NONE)
Create a reactor handle.
std::function< void(const ReactorGLES &reactor)> Operation
bool React()
Perform a reaction on the current thread if able.
~ReactorGLES()
Destroy a reactor.
const ProcTableGLES & GetProcTable() const
Get the OpenGL proc. table the reactor uses to manage handles.
bool AddOperation(Operation operation, bool defer=false)
Adds an operation that the reactor runs on a worker that ensures that an OpenGL context is current.
bool RemoveWorker(WorkerID id)
Remove a previously added worker from the reactor. If the reactor has no workers, pending added opera...
WorkerID AddWorker(std::weak_ptr< Worker > worker)
Adds a worker to the reactor. Each new worker must ensure that the context it manages is the same as ...
void SetDebugLabel(const HandleGLES &handle, std::string_view label)
Set the debug label on a reactor handle.
bool IsValid() const
If this is a valid reactor. Invalid reactors must be discarded immediately.
FlutterDesktopBinaryReply callback
#define FML_UNREACHABLE()
Definition logging.h:128
#define FML_DCHECK(condition)
Definition logging.h:122
const char * name
Definition fuchsia.cc:49
std::function< void()> closure
Definition closure.h:14
static DebugResourceType ToDebugResourceType(HandleType type)
Definition ref_ptr.h:261
const uintptr_t id
#define TRACE_EVENT0(category_group, name)
#define VALIDATION_LOG
Definition validation.h:91