Flutter Engine
The Flutter Engine
Public Member Functions | List of all members
flutter::AndroidContextGLSkia Class Reference

#include <android_context_gl_skia.h>

Inheritance diagram for flutter::AndroidContextGLSkia:
flutter::AndroidContext

Public Member Functions

 AndroidContextGLSkia (fml::RefPtr< AndroidEnvironmentGL > environment, const TaskRunners &taskRunners)
 
 ~AndroidContextGLSkia ()
 
std::unique_ptr< AndroidEGLSurfaceCreateOnscreenSurface (const 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 GetEGLContext () const
 Returns the EGLContext. More...
 
EGLDisplay GetEGLDisplay () const
 Returns the EGLDisplay. More...
 
EGLContext CreateNewContext () const
 Create a new EGLContext using the same EGLConfig. More...
 
EGLConfig Config () const
 The EGLConfig for this context. More...
 
- Public Member Functions inherited from flutter::AndroidContext
 AndroidContext (AndroidRenderingAPI rendering_api)
 
virtual ~AndroidContext ()
 
AndroidRenderingAPI RenderingApi () const
 
virtual bool IsValid () const
 
void SetMainSkiaContext (const sk_sp< GrDirectContext > &main_context)
 Setter for the Skia context to be used by subsequent AndroidSurfaces. More...
 
sk_sp< GrDirectContextGetMainSkiaContext () const
 Accessor for the Skia context associated with AndroidSurfaces and the raster thread. More...
 
std::shared_ptr< impeller::ContextGetImpellerContext () const
 Accessor for the Impeller context associated with AndroidSurfaces and the raster thread. More...
 

Additional Inherited Members

- Protected Member Functions inherited from flutter::AndroidContext
void SetImpellerContext (const std::shared_ptr< impeller::Context > &context)
 

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 28 of file android_context_gl_skia.h.

Constructor & Destructor Documentation

◆ AndroidContextGLSkia()

flutter::AndroidContextGLSkia::AndroidContextGLSkia ( fml::RefPtr< AndroidEnvironmentGL environment,
const TaskRunners taskRunners 
)

Definition at line 63 of file android_context_gl_skia.cc.

67 environment_(std::move(environment)),
68 task_runners_(task_runners) {
69 if (!environment_->IsValid()) {
70 FML_LOG(ERROR) << "Could not create an Android GL environment.";
71 return;
72 }
73
74 bool success = false;
75
76 // Choose a valid configuration.
77 std::tie(success, config_) = ChooseEGLConfiguration(environment_->Display());
78 if (!success) {
79 FML_LOG(ERROR) << "Could not choose an EGL configuration.";
81 return;
82 }
83
84 // Create a context for the configuration.
85 std::tie(success, context_) =
86 CreateContext(environment_->Display(), config_, EGL_NO_CONTEXT);
87 if (!success) {
88 FML_LOG(ERROR) << "Could not create an EGL context";
90 return;
91 }
92
93 std::tie(success, resource_context_) =
94 CreateContext(environment_->Display(), config_, context_);
95 if (!success) {
96 FML_LOG(ERROR) << "Could not create an EGL resource context";
98 return;
99 }
100
101 // All done!
102 valid_ = true;
103}
AndroidContext(AndroidRenderingAPI rendering_api)
#define FML_LOG(severity)
Definition: logging.h:82
static dart::SimpleHashMap * environment
Definition: gen_snapshot.cc:59
void LogLastEGLError()
static EGLResult< EGLConfig > ChooseEGLConfiguration(EGLDisplay display)
static EGLResult< EGLContext > CreateContext(EGLDisplay display, EGLConfig config, EGLContext share=EGL_NO_CONTEXT)
#define ERROR(message)
Definition: elf_loader.cc:260

◆ ~AndroidContextGLSkia()

flutter::AndroidContextGLSkia::~AndroidContextGLSkia ( )

Definition at line 105 of file android_context_gl_skia.cc.

105 {
108 SetMainSkiaContext(nullptr);
110 // This context needs to be deallocated from the raster thread in order to
111 // keep a coherent usage of egl from a single thread.
113 if (main_context) {
114 std::unique_ptr<AndroidEGLSurface> pbuffer_surface =
115 CreatePbufferSurface();
116 auto status = pbuffer_surface->MakeCurrent();
117 if (status != AndroidEGLSurfaceMakeCurrentStatus::kFailure) {
118 main_context->releaseResourcesAndAbandonContext();
119 main_context.reset();
120 ClearCurrent();
121 }
122 }
123 latch.Signal();
124 });
125 latch.Wait();
126
127 if (!TeardownContext(environment_->Display(), context_)) {
129 << "Could not tear down the EGL context. Possible resource leak.";
131 }
132
133 if (!TeardownContext(environment_->Display(), resource_context_)) {
134 FML_LOG(ERROR) << "Could not tear down the EGL resource context. Possible "
135 "resource leak.";
137 }
138}
void SetMainSkiaContext(const sk_sp< GrDirectContext > &main_context)
Setter for the Skia context to be used by subsequent AndroidSurfaces.
sk_sp< GrDirectContext > GetMainSkiaContext() const
Accessor for the Skia context associated with AndroidSurfaces and the raster thread.
fml::RefPtr< fml::TaskRunner > GetRasterTaskRunner() const
Definition: task_runners.cc:42
fml::RefPtr< fml::TaskRunner > GetPlatformTaskRunner() const
Definition: task_runners.cc:30
static void RunNowOrPostTask(const fml::RefPtr< fml::TaskRunner > &runner, const fml::closure &task)
Definition: task_runner.cc:55
virtual bool RunsTasksOnCurrentThread()
Definition: task_runner.cc:43
#define FML_DCHECK(condition)
Definition: logging.h:103
static bool TeardownContext(EGLDisplay display, EGLContext context)

Member Function Documentation

◆ ClearCurrent()

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

Definition at line 187 of file android_context_gl_skia.cc.

187 {
188 if (eglGetCurrentContext() != context_) {
189 return true;
190 }
191 if (eglMakeCurrent(environment_->Display(), EGL_NO_SURFACE, EGL_NO_SURFACE,
192 EGL_NO_CONTEXT) != EGL_TRUE) {
193 FML_LOG(ERROR) << "Could not clear the current context";
195 return false;
196 }
197 return true;
198}

◆ Config()

EGLConfig flutter::AndroidContextGLSkia::Config ( ) const
inline

The EGLConfig for this context.

Definition at line 101 of file android_context_gl_skia.h.

101{ return config_; }

◆ CreateNewContext()

EGLContext flutter::AndroidContextGLSkia::CreateNewContext ( ) const

Create a new EGLContext using the same EGLConfig.

Returns
The EGLContext.

Definition at line 208 of file android_context_gl_skia.cc.

208 {
209 bool success;
210 EGLContext context;
211 std::tie(success, context) =
212 CreateContext(environment_->Display(), config_, EGL_NO_CONTEXT);
213 return success ? context : EGL_NO_CONTEXT;
214}

◆ CreateOffscreenSurface()

std::unique_ptr< AndroidEGLSurface > flutter::AndroidContextGLSkia::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 157 of file android_context_gl_skia.cc.

157 {
158 // We only ever create pbuffer surfaces for background resource loading
159 // contexts. We never bind the pbuffer to anything.
160 EGLDisplay display = environment_->Display();
161
162 const EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
163
164 EGLSurface surface = eglCreatePbufferSurface(display, config_, attribs);
165 return std::make_unique<AndroidEGLSurface>(surface, display,
166 resource_context_);
167}
VkSurfaceKHR surface
Definition: main.cc:49

◆ CreateOnscreenSurface()

std::unique_ptr< AndroidEGLSurface > flutter::AndroidContextGLSkia::CreateOnscreenSurface ( const 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 140 of file android_context_gl_skia.cc.

141 {
142 if (window->IsFakeWindow()) {
143 return CreatePbufferSurface();
144 } else {
145 EGLDisplay display = environment_->Display();
146
147 const EGLint attribs[] = {EGL_NONE};
148
149 EGLSurface surface = eglCreateWindowSurface(
150 display, config_,
151 reinterpret_cast<EGLNativeWindowType>(window->handle()), attribs);
152 return std::make_unique<AndroidEGLSurface>(surface, display, context_);
153 }
154}
std::unique_ptr< AndroidEGLSurface > CreatePbufferSurface() const
Allocates an 1x1 pbuffer surface that is used for making the onscreen context current for snapshottin...
GLFWwindow * window
Definition: main.cc:45

◆ CreatePbufferSurface()

std::unique_ptr< AndroidEGLSurface > flutter::AndroidContextGLSkia::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 169 of file android_context_gl_skia.cc.

170 {
171 EGLDisplay display = environment_->Display();
172
173 const EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
174
175 EGLSurface surface = eglCreatePbufferSurface(display, config_, attribs);
176 return std::make_unique<AndroidEGLSurface>(surface, display, context_);
177}

◆ Environment()

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

Definition at line 179 of file android_context_gl_skia.cc.

179 {
180 return environment_;
181}

◆ GetEGLContext()

EGLContext flutter::AndroidContextGLSkia::GetEGLContext ( ) const

Returns the EGLContext.

Returns
EGLContext.

Definition at line 200 of file android_context_gl_skia.cc.

200 {
201 return context_;
202}

◆ GetEGLDisplay()

EGLDisplay flutter::AndroidContextGLSkia::GetEGLDisplay ( ) const

Returns the EGLDisplay.

Returns
EGLDisplay.

Definition at line 204 of file android_context_gl_skia.cc.

204 {
205 return environment_->Display();
206}

◆ IsValid()

bool flutter::AndroidContextGLSkia::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 183 of file android_context_gl_skia.cc.

183 {
184 return valid_;
185}

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