| Flutter Impeller |

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 | LifecycleEvent { LifecycleEvent::kDidMakeCurrent, LifecycleEvent::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< UniqueID > | 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. 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

using impeller::egl::Context::LifecycleListener = std::function<void(LifecycleEvent)>

Definition at line 75 of file context.h.

## Member Enumeration Documentation

## ◆ LifecycleEvent

enum impeller::egl::Context::LifecycleEvent
| strong

Enumerator
kDidMakeCurrent |
kWillClearCurrent |

Definition at line 71 of file context.h.

71 {
72 kDidMakeCurrent,
73 kWillClearCurrent,
74 };

## Constructor & Destructor Documentation

## ◆ ~Context()

impeller::egl::Context::~Context | ( |  | ) |

Definition at line 15 of file context.cc.

15 {
16 if (context_ != EGL_NO_CONTEXT) {
17 if (::eglDestroyContext(display_, context_) != EGL_TRUE) {
18 IMPELLER_LOG_EGL_ERROR;
19 }
20 }
21 }

References IMPELLER_LOG_EGL_ERROR.

## Member Function Documentation

## ◆ AddLifecycleListener()

std::optional< UniqueID > impeller::egl::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] | listener | The 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 }

## ◆ ClearCurrent()

bool impeller::egl::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) {
69 IMPELLER_LOG_EGL_ERROR;
70 }
71 return result;
72 }

References impeller::egl::EGLMakeCurrentIfNecessary(), and IMPELLER_LOG_EGL_ERROR.

## ◆ GetHandle()

const EGLContext & impeller::egl::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 }

Referenced by impeller::egl::Display::CreateContext().

## ◆ IsValid()

bool impeller::egl::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 impeller::egl::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] | surface | The 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) {
55 IMPELLER_LOG_EGL_ERROR;
56 }
57 DispatchLifecyleEvent(LifecycleEvent::kDidMakeCurrent);
58 return result;
59 }

References impeller::egl::EGLMakeCurrentIfNecessary(), impeller::egl::Surface::GetHandle(), and IMPELLER_LOG_EGL_ERROR.

## ◆ RemoveLifecycleListener()

bool impeller::egl::Context::RemoveLifecycleListener | ( | UniqueID | id | ) |

Remove a previously added context listener.

Parameters

  [in] | id | The 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:

- impeller/toolkit/egl/context.h
- impeller/toolkit/egl/context.cc

impeller::egl::Context::LifecycleEvent::kDidMakeCurrent
@ kDidMakeCurrent
impeller::egl::Context::LifecycleEvent::kWillClearCurrent
@ kWillClearCurrent
impeller::egl::EGLMakeCurrentIfNecessary
static EGLBoolean EGLMakeCurrentIfNecessary(EGLDisplay display, EGLSurface draw, EGLSurface read, EGLContext context)
Definition: context.cc:31
IMPELLER_LOG_EGL_ERROR
#define IMPELLER_LOG_EGL_ERROR
Definition: egl.h:25