Flutter Engine
flutter::AndroidContextGL Class Reference

#include <android_context_gl.h>

Inheritance diagram for flutter::AndroidContextGL:
flutter::AndroidContext

Public Member Functions

 AndroidContextGL (AndroidRenderingAPI rendering_api, fml::RefPtr< AndroidEnvironmentGL > environment)
 
 ~AndroidContextGL ()
 
std::unique_ptr< AndroidEGLSurfaceCreateOnscreenSurface (fml::RefPtr< AndroidNativeWindow > window) const
 Allocates an new EGL window surface that is used for on-screen pixels. More...
 
std::unique_ptr< AndroidEGLSurfaceCreateOffscreenSurface () const
 Allocates an 1x1 pbuffer surface that is used for making the offscreen current for texture uploads. More...
 
fml::RefPtr< AndroidEnvironmentGLEnvironment () const
 
bool IsValid () const
 
bool ClearCurrent ()
 
EGLContext CreateNewContext () const
 Create a new EGLContext using the same EGLConfig. More...
 
- Public Member Functions inherited from flutter::AndroidContext
 AndroidContext (AndroidRenderingAPI rendering_api)
 
 ~AndroidContext ()
 
AndroidRenderingAPI RenderingApi () const
 
bool IsValid () const
 

Detailed Description

The Android context is used by AndroidSurfaceGL to create and manage EGL surfaces.

This context binds EGLContext to the current rendering thread and to the draw and read EGLSurfaces.

Definition at line 72 of file android_context_gl.h.

Constructor & Destructor Documentation

◆ AndroidContextGL()

flutter::AndroidContextGL::AndroidContextGL ( AndroidRenderingAPI  rendering_api,
fml::RefPtr< AndroidEnvironmentGL environment 
)

Definition at line 149 of file android_context_gl.cc.

References flutter::ChooseEGLConfiguration(), flutter::CreateContext(), FML_LOG, and flutter::LogLastEGLError().

153  environment_(environment),
154  config_(nullptr) {
155  if (!environment_->IsValid()) {
156  FML_LOG(ERROR) << "Could not create an Android GL environment.";
157  return;
158  }
159 
160  bool success = false;
161 
162  // Choose a valid configuration.
163  std::tie(success, config_) = ChooseEGLConfiguration(environment_->Display());
164  if (!success) {
165  FML_LOG(ERROR) << "Could not choose an EGL configuration.";
166  LogLastEGLError();
167  return;
168  }
169 
170  // Create a context for the configuration.
171  std::tie(success, context_) =
172  CreateContext(environment_->Display(), config_, EGL_NO_CONTEXT);
173  if (!success) {
174  FML_LOG(ERROR) << "Could not create an EGL context";
175  LogLastEGLError();
176  return;
177  }
178 
179  std::tie(success, resource_context_) =
180  CreateContext(environment_->Display(), config_, context_);
181  if (!success) {
182  FML_LOG(ERROR) << "Could not create an EGL resource context";
183  LogLastEGLError();
184  return;
185  }
186 
187  // All done!
188  valid_ = true;
189 }
static void LogLastEGLError()
#define FML_LOG(severity)
Definition: logging.h:65
static EGLResult< EGLContext > CreateContext(EGLDisplay display, EGLConfig config, EGLContext share=EGL_NO_CONTEXT)
static EGLResult< EGLConfig > ChooseEGLConfiguration(EGLDisplay display)
AndroidContext(AndroidRenderingAPI rendering_api)

◆ ~AndroidContextGL()

flutter::AndroidContextGL::~AndroidContextGL ( )

Definition at line 191 of file android_context_gl.cc.

References FML_LOG, flutter::LogLastEGLError(), and flutter::TeardownContext().

191  {
192  if (!TeardownContext(environment_->Display(), context_)) {
193  FML_LOG(ERROR)
194  << "Could not tear down the EGL context. Possible resource leak.";
195  LogLastEGLError();
196  }
197 
198  if (!TeardownContext(environment_->Display(), resource_context_)) {
199  FML_LOG(ERROR) << "Could not tear down the EGL resource context. Possible "
200  "resource leak.";
201  LogLastEGLError();
202  }
203 }
static void LogLastEGLError()
#define FML_LOG(severity)
Definition: logging.h:65
static bool TeardownContext(EGLDisplay display, EGLContext context)

Member Function Documentation

◆ ClearCurrent()

bool flutter::AndroidContextGL::ClearCurrent ( )
Returns
Whether the current context was successfully clear.

Definition at line 238 of file android_context_gl.cc.

References eglMakeCurrent(), FML_LOG, and flutter::LogLastEGLError().

238  {
239  if (eglGetCurrentContext() != context_) {
240  return true;
241  }
242  if (eglMakeCurrent(environment_->Display(), EGL_NO_SURFACE, EGL_NO_SURFACE,
243  EGL_NO_CONTEXT) != EGL_TRUE) {
244  FML_LOG(ERROR) << "Could not clear the current context";
245  LogLastEGLError();
246  return false;
247  }
248  return true;
249 }
static void LogLastEGLError()
#define FML_LOG(severity)
Definition: logging.h:65
EGLBoolean eglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx)
Definition: mock_egl.cc:306

◆ CreateNewContext()

EGLContext flutter::AndroidContextGL::CreateNewContext ( ) const

Create a new EGLContext using the same EGLConfig.

Returns
The EGLContext.

Definition at line 251 of file android_context_gl.cc.

References flutter::CreateContext().

251  {
252  bool success;
253  EGLContext context;
254  std::tie(success, context) =
255  CreateContext(environment_->Display(), config_, EGL_NO_CONTEXT);
256  return success ? context : EGL_NO_CONTEXT;
257 }
static EGLResult< EGLContext > CreateContext(EGLDisplay display, EGLConfig config, EGLContext share=EGL_NO_CONTEXT)

◆ CreateOffscreenSurface()

std::unique_ptr< AndroidEGLSurface > flutter::AndroidContextGL::CreateOffscreenSurface ( ) const

Allocates an 1x1 pbuffer surface that is used for making the offscreen current for texture uploads.

Returns
The pbuffer surface.

Definition at line 217 of file android_context_gl.cc.

References eglCreatePbufferSurface().

218  {
219  // We only ever create pbuffer surfaces for background resource loading
220  // contexts. We never bind the pbuffer to anything.
221  EGLDisplay display = environment_->Display();
222 
223  const EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
224 
225  EGLSurface surface = eglCreatePbufferSurface(display, config_, attribs);
226  return std::make_unique<AndroidEGLSurface>(surface, display,
227  resource_context_);
228 }
EGLSurface eglCreatePbufferSurface(EGLDisplay dpy, EGLConfig config, const EGLint *attrib_list)
Definition: mock_egl.cc:132

◆ CreateOnscreenSurface()

std::unique_ptr< AndroidEGLSurface > flutter::AndroidContextGL::CreateOnscreenSurface ( fml::RefPtr< AndroidNativeWindow window) const

Allocates an new EGL window surface that is used for on-screen pixels.

Returns
The window surface.

Definition at line 205 of file android_context_gl.cc.

References eglCreateWindowSurface().

206  {
207  EGLDisplay display = environment_->Display();
208 
209  const EGLint attribs[] = {EGL_NONE};
210 
211  EGLSurface surface = eglCreateWindowSurface(
212  display, config_, reinterpret_cast<EGLNativeWindowType>(window->handle()),
213  attribs);
214  return std::make_unique<AndroidEGLSurface>(surface, display, context_);
215 }
EGLSurface eglCreateWindowSurface(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, const EGLint *attrib_list)
Definition: mock_egl.cc:142

◆ Environment()

fml::RefPtr< AndroidEnvironmentGL > flutter::AndroidContextGL::Environment ( ) const
Returns
The Android environment that contains a reference to the display.

Definition at line 230 of file android_context_gl.cc.

230  {
231  return environment_;
232 }

◆ IsValid()

bool flutter::AndroidContextGL::IsValid ( ) const
Returns
Whether the current context is valid. That is, if the EGL contexts were successfully created.

Definition at line 234 of file android_context_gl.cc.

234  {
235  return valid_;
236 }

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