Flutter Engine
The Flutter Engine
|
The reactor attempts to make thread-safe usage of OpenGL ES easier to reason about. More...
#include <reactor_gles.h>
Classes | |
class | Worker |
A delegate implemented by a thread on which an OpenGL context is current. There may be multiple workers for the reactor to perform reactions on. In that case, it is the workers responsibility to ensure that all of them use either the same OpenGL context or multiple OpenGL contexts in the same sharegroup. More... | |
Public Types | |
using | WorkerID = UniqueID |
using | Ref = std::shared_ptr< ReactorGLES > |
using | Operation = std::function< void(const ReactorGLES &reactor)> |
Public Member Functions | |
ReactorGLES (std::unique_ptr< ProcTableGLES > gl) | |
Create a new reactor. There are expensive and only one per application instance is necessary. More... | |
~ReactorGLES () | |
Destroy a reactor. More... | |
bool | IsValid () const |
If this is a valid reactor. Invalid reactors must be discarded immediately. More... | |
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 the other workers in the reactor or in the same sharegroup. More... | |
bool | RemoveWorker (WorkerID id) |
Remove a previously added worker from the reactor. If the reactor has no workers, pending added operations will never run. More... | |
const ProcTableGLES & | GetProcTable () const |
Get the OpenGL proc. table the reactor uses to manage handles. More... | |
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 call within a reaction. That is, within a ReactorGLES::Operation . More... | |
HandleGLES | CreateHandle (HandleType type) |
Create a reactor handle. More... | |
void | CollectHandle (HandleGLES handle) |
Collect a reactor handle. More... | |
void | SetDebugLabel (const HandleGLES &handle, std::string label) |
Set the debug label on a reactor handle. More... | |
bool | AddOperation (Operation operation) |
Adds an operation that the reactor runs on a worker that ensures that an OpenGL context is current. More... | |
bool | React () |
Perform a reaction on the current thread if able. More... | |
The reactor attempts to make thread-safe usage of OpenGL ES easier to reason about.
In the other Impeller backends (like Metal and Vulkan), resources can be created, used, and deleted on any thread with relatively few restrictions. However, OpenGL resources can only be created, used, and deleted on a thread on which an OpenGL context (or one in the same sharegroup) is current.
There aren't too many OpenGL contexts to go around and making the caller reason about the timing and threading requirement only when the OpenGL backend is in use is tedious. To work around this tedium, there is an abstraction between the resources and their handles in OpenGL. The reactor is this abstraction.
The reactor is thread-safe and can created, used, and collected on any thread.
Reactor handles HandleGLES
can be created, used, and collected on any thread. These handles can be to textures, buffers, etc..
Operations added to the reactor are guaranteed to run on a worker within a finite amount of time unless the reactor itself is torn down or there are no workers. These operations may run on the calling thread immediately if a worker is active on the current thread and can perform reactions. The operations are guaranteed to run with an OpenGL context current and all reactor handles having live OpenGL handle counterparts.
Creating a handle in the reactor doesn't mean an OpenGL handle is created immediately. OpenGL handles become live before the next reaction. Similarly, dropping the last reference to a reactor handle means that the OpenGL handle will be deleted at some point in the near future.
Definition at line 55 of file reactor_gles.h.
using impeller::ReactorGLES::Operation = std::function<void(const ReactorGLES& reactor)> |
Definition at line 194 of file reactor_gles.h.
using impeller::ReactorGLES::Ref = std::shared_ptr<ReactorGLES> |
Definition at line 86 of file reactor_gles.h.
Definition at line 57 of file reactor_gles.h.
|
explicit |
Create a new reactor. There are expensive and only one per application instance is necessary.
[in] | gl | The proc table for GL access. This is necessary for the reactor to be able to create and collect OpenGL handles. |
Definition at line 15 of file reactor_gles.cc.
|
default |
Destroy a reactor.
bool impeller::ReactorGLES::AddOperation | ( | Operation | operation | ) |
Adds an operation that the reactor runs on a worker that ensures that an OpenGL context is current.
This operation is not guaranteed to run immediately. It will complete in a finite amount of time on any thread as long as there is a reactor worker and the reactor itself is not being torn down.
[in] | operation | The operation |
Definition at line 71 of file reactor_gles.cc.
ReactorGLES::WorkerID impeller::ReactorGLES::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 the other workers in the reactor or in the same sharegroup.
[in] | worker | The worker |
Definition at line 31 of file reactor_gles.cc.
void impeller::ReactorGLES::CollectHandle | ( | HandleGLES | handle | ) |
Collect a reactor handle.
This can be called on any thread. Even one that doesn't have an OpenGL context.
[in] | handle | The reactor handle handle |
Definition at line 149 of file reactor_gles.cc.
HandleGLES impeller::ReactorGLES::CreateHandle | ( | HandleType | type | ) |
Create a reactor handle.
This can be called on any thread. Even one that doesn't have an OpenGL context.
[in] | type | The type of handle to create. |
Definition at line 133 of file reactor_gles.cc.
std::optional< GLuint > impeller::ReactorGLES::GetGLHandle | ( | const HandleGLES & | handle | ) | const |
Returns the OpenGL handle for a reactor handle if one is available. This is typically only safe to call within a reaction. That is, within a ReactorGLES::Operation
.
Asking for the OpenGL handle before the reactor has a chance to reactor will return std::nullopt
.
This can be called on any thread but is typically useless outside of a reaction since the handle is useless outside of a reactor operation.
[in] | handle | The reactor handle. |
std::nullopt
otherwise. Definition at line 53 of file reactor_gles.cc.
const ProcTableGLES & impeller::ReactorGLES::GetProcTable | ( | ) | const |
Get the OpenGL proc. table the reactor uses to manage handles.
Definition at line 48 of file reactor_gles.cc.
bool impeller::ReactorGLES::IsValid | ( | ) | const |
If this is a valid reactor. Invalid reactors must be discarded immediately.
Definition at line 27 of file reactor_gles.cc.
bool impeller::ReactorGLES::React | ( | ) |
Perform a reaction on the current thread if able.
It is safe to call this simultaneously from multiple threads at the same time.
Definition at line 156 of file reactor_gles.cc.
bool impeller::ReactorGLES::RemoveWorker | ( | WorkerID | id | ) |
Remove a previously added worker from the reactor. If the reactor has no workers, pending added operations will never run.
[in] | id | The worker identifier previously returned by AddWorker . |
Definition at line 38 of file reactor_gles.cc.
void impeller::ReactorGLES::SetDebugLabel | ( | const HandleGLES & | handle, |
std::string | label | ||
) |
Set the debug label on a reactor handle.
This call ensures that the OpenGL debug label is propagated to even the OpenGL handle hasn't been created at the time the caller sets the label.
[in] | handle | The handle |
[in] | label | The label |
Definition at line 274 of file reactor_gles.cc.