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...
 
std::unique_ptr< AndroidEGLSurfaceCreatePbufferSurface () const
 Allocates an 1x1 pbuffer surface that is used for making the onscreen context current for snapshotting. More...
 
fml::RefPtr< AndroidEnvironmentGLEnvironment () const
 
bool IsValid () const override
 
bool ClearCurrent () const
 
EGLContext CreateNewContext () const
 Create a new EGLContext using the same EGLConfig. More...
 
- Public Member Functions inherited from flutter::AndroidContext
 AndroidContext (AndroidRenderingAPI rendering_api)
 
virtual ~AndroidContext ()
 
AndroidRenderingAPI RenderingApi () 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 ( ) const
Returns
Whether the current context was successfully clear.

Definition at line 252 of file android_context_gl.cc.

References FML_LOG, and flutter::LogLastEGLError().

Referenced by flutter::AndroidSurfaceGL::GLContextClearCurrent(), flutter::AndroidSurfaceGL::OnScreenSurfaceResize(), and flutter::AndroidSurfaceGL::TeardownOnScreenContext().

252  {
253  if (eglGetCurrentContext() != context_) {
254  return true;
255  }
256  if (eglMakeCurrent(environment_->Display(), EGL_NO_SURFACE, EGL_NO_SURFACE,
257  EGL_NO_CONTEXT) != EGL_TRUE) {
258  FML_LOG(ERROR) << "Could not clear the current context";
259  LogLastEGLError();
260  return false;
261  }
262  return true;
263 }
static void LogLastEGLError()
#define FML_LOG(severity)
Definition: logging.h:65

◆ CreateNewContext()

EGLContext flutter::AndroidContextGL::CreateNewContext ( ) const

Create a new EGLContext using the same EGLConfig.

Returns
The EGLContext.

Definition at line 265 of file android_context_gl.cc.

References flutter::CreateContext().

Referenced by flutter::AndroidSurfaceGL::GetGLInterface().

265  {
266  bool success;
267  EGLContext context;
268  std::tie(success, context) =
269  CreateContext(environment_->Display(), config_, EGL_NO_CONTEXT);
270  return success ? context : EGL_NO_CONTEXT;
271 }
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 221 of file android_context_gl.cc.

Referenced by flutter::AndroidSurfaceGL::AndroidSurfaceGL().

222  {
223  // We only ever create pbuffer surfaces for background resource loading
224  // contexts. We never bind the pbuffer to anything.
225  EGLDisplay display = environment_->Display();
226 
227  const EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
228 
229  EGLSurface surface = eglCreatePbufferSurface(display, config_, attribs);
230  return std::make_unique<AndroidEGLSurface>(surface, display,
231  resource_context_);
232 }

◆ 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 CreatePbufferSurface().

Referenced by flutter::AndroidSurfaceGL::OnScreenSurfaceResize(), and flutter::AndroidSurfaceGL::SetNativeWindow().

206  {
207  if (window->IsFakeWindow()) {
208  return CreatePbufferSurface();
209  } else {
210  EGLDisplay display = environment_->Display();
211 
212  const EGLint attribs[] = {EGL_NONE};
213 
214  EGLSurface surface = eglCreateWindowSurface(
215  display, config_,
216  reinterpret_cast<EGLNativeWindowType>(window->handle()), attribs);
217  return std::make_unique<AndroidEGLSurface>(surface, display, context_);
218  }
219 }
std::unique_ptr< AndroidEGLSurface > CreatePbufferSurface() const
Allocates an 1x1 pbuffer surface that is used for making the onscreen context current for snapshottin...

◆ CreatePbufferSurface()

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

Allocates an 1x1 pbuffer surface that is used for making the onscreen context current for snapshotting.

Returns
The pbuffer surface.

Definition at line 234 of file android_context_gl.cc.

Referenced by CreateOnscreenSurface(), and flutter::AndroidSurfaceGL::CreatePbufferSurface().

235  {
236  EGLDisplay display = environment_->Display();
237 
238  const EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
239 
240  EGLSurface surface = eglCreatePbufferSurface(display, config_, attribs);
241  return std::make_unique<AndroidEGLSurface>(surface, display, context_);
242 }

◆ Environment()

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

Definition at line 244 of file android_context_gl.cc.

244  {
245  return environment_;
246 }

◆ IsValid()

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

Reimplemented from flutter::AndroidContext.

Definition at line 248 of file android_context_gl.cc.

Referenced by flutter::AndroidSurfaceGL::IsValid().

248  {
249  return valid_;
250 }

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