Flutter Engine
The Flutter Engine
Public Types | Public Member Functions | Friends | List of all members
impeller::egl::Context Class Reference

An instance of an EGL context. More...

#include <context.h>

Public Types

enum class  LifecycleEvent { kDidMakeCurrent , kWillClearCurrent }
 
using LifecycleListener = std::function< void(LifecycleEvent)>
 

Public Member Functions

 ~Context ()
 
bool IsValid () const
 Determines if a valid context could be created. The context still needs to be made current on the thread for it to be useful. More...
 
const EGLContext & GetHandle () const
 Get the underlying handle to the EGL context. More...
 
bool MakeCurrent (const Surface &surface) const
 Make the context current on the calling thread. It is the caller responsibility to ensure that any context previously current on the thread must be cleared via ClearCurrent. More...
 
bool ClearCurrent () const
 Clear the thread association of this context. More...
 
std::optional< UniqueIDAddLifecycleListener (const LifecycleListener &listener)
 Add a listener that gets invoked when the context is made and cleared current from the thread. Applications typically use this to manage workers that schedule OpenGL API calls that need to be careful about the context being current when called. More...
 
bool RemoveLifecycleListener (UniqueID id)
 Remove a previously added context listener. More...
 

Friends

class Display
 

Detailed Description

An instance of an EGL context.

        An EGL context can only be used on a single thread at a given
        time. A thread can only have a single context current at any
        given time.

        Context cannot be created directly. Only a valid instance of an
        egl::Display can create a context.

Definition at line 30 of file context.h.

Member Typedef Documentation

◆ LifecycleListener

Definition at line 75 of file context.h.

Member Enumeration Documentation

◆ LifecycleEvent

Enumerator
kDidMakeCurrent 
kWillClearCurrent 

Definition at line 71 of file context.h.

71 {
72 kDidMakeCurrent,
73 kWillClearCurrent,
74 };

Constructor & Destructor Documentation

◆ ~Context()

Context::~Context ( )

Definition at line 15 of file context.cc.

15 {
16 if (context_ != EGL_NO_CONTEXT) {
17 if (::eglDestroyContext(display_, context_) != EGL_TRUE) {
19 }
20 }
21}
#define IMPELLER_LOG_EGL_ERROR
Definition: egl.h:25

Member Function Documentation

◆ AddLifecycleListener()

std::optional< UniqueID > Context::AddLifecycleListener ( const LifecycleListener listener)

Add a listener that gets invoked when the context is made and cleared current from the thread. Applications typically use this to manage workers that schedule OpenGL API calls that need to be careful about the context being current when called.

Parameters
[in]listenerThe listener
Returns
A unique ID for the listener that can used used in RemoveLifecycleListener to remove a previously added listener.

Definition at line 74 of file context.cc.

75 {
76 if (!listener) {
77 return std::nullopt;
78 }
79 WriterLock lock(listeners_mutex_);
80 UniqueID id;
81 listeners_[id] = listener;
82 return id;
83}
const uintptr_t id

◆ ClearCurrent()

bool Context::ClearCurrent ( ) const

Clear the thread association of this context.

Returns
If the thread association could be cleared.

Definition at line 61 of file context.cc.

61 {
62 DispatchLifecyleEvent(LifecycleEvent::kWillClearCurrent);
63 const auto result = EGLMakeCurrentIfNecessary(display_, //
64 EGL_NO_SURFACE, //
65 EGL_NO_SURFACE, //
66 EGL_NO_CONTEXT //
67 ) == EGL_TRUE;
68 if (!result) {
70 }
71 return result;
72}
GAsyncResult * result
static EGLBoolean EGLMakeCurrentIfNecessary(EGLDisplay display, EGLSurface draw, EGLSurface read, EGLContext context)
Definition: context.cc:31

◆ GetHandle()

const EGLContext & Context::GetHandle ( ) const

Get the underlying handle to the EGL context.

Returns
The handle.

Definition at line 27 of file context.cc.

27 {
28 return context_;
29}

◆ IsValid()

bool Context::IsValid ( ) const

Determines if a valid context could be created. The context still needs to be made current on the thread for it to be useful.

Returns
True if valid, False otherwise.

Definition at line 23 of file context.cc.

23 {
24 return context_ != EGL_NO_CONTEXT;
25}

◆ MakeCurrent()

bool Context::MakeCurrent ( const Surface surface) const

Make the context current on the calling thread. It is the caller responsibility to ensure that any context previously current on the thread must be cleared via ClearCurrent.

@important The config used to create the surface must match the config used to create this context instance.

Parameters
[in]surfaceThe surface to use to make the context current.
Returns
If the context could be made current on the callers thread.

Definition at line 45 of file context.cc.

45 {
46 if (context_ == EGL_NO_CONTEXT) {
47 return false;
48 }
49 const auto result = EGLMakeCurrentIfNecessary(display_, //
50 surface.GetHandle(), //
51 surface.GetHandle(), //
52 context_ //
53 ) == EGL_TRUE;
54 if (!result) {
56 }
57 DispatchLifecyleEvent(LifecycleEvent::kDidMakeCurrent);
58 return result;
59}
VkSurfaceKHR surface
Definition: main.cc:49

◆ RemoveLifecycleListener()

bool Context::RemoveLifecycleListener ( UniqueID  id)

Remove a previously added context listener.

Parameters
[in]idThe identifier obtained via a previous call to AddLifecycleListener.
Returns
True if the listener could be removed.

Definition at line 85 of file context.cc.

85 {
86 WriterLock lock(listeners_mutex_);
87 auto found = listeners_.find(id);
88 if (found == listeners_.end()) {
89 return false;
90 }
91 listeners_.erase(found);
92 return true;
93}

Friends And Related Function Documentation

◆ Display

friend class Display
friend

Definition at line 103 of file context.h.


The documentation for this class was generated from the following files: