5#include "flutter/shell/platform/android/android_context_gl_skia.h"
9#include "flutter/fml/trace_event.h"
10#include "flutter/shell/platform/android/android_egl_surface.h"
19 EGLContext share = EGL_NO_CONTEXT) {
20 EGLint attributes[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
22 EGLContext context = eglCreateContext(display, config, share, attributes);
24 return {context != EGL_NO_CONTEXT, context};
28 EGLint attributes[] = {
30 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
31 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
42 EGLint config_count = 0;
43 EGLConfig egl_config =
nullptr;
45 if (eglChooseConfig(display, attributes, &egl_config, 1, &config_count) !=
47 return {
false,
nullptr};
50 bool success = config_count > 0 && egl_config !=
nullptr;
52 return {success, success ? egl_config :
nullptr};
56 if (context != EGL_NO_CONTEXT) {
57 return eglDestroyContext(display, context) == EGL_TRUE;
69 if (!environment_->IsValid()) {
70 FML_LOG(
ERROR) <<
"Could not create an Android GL environment.";
79 FML_LOG(
ERROR) <<
"Could not choose an EGL configuration.";
85 std::tie(success, context_) =
86 CreateContext(environment_->Display(), config_, EGL_NO_CONTEXT);
93 std::tie(success, resource_context_) =
96 FML_LOG(
ERROR) <<
"Could not create an EGL resource 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();
129 <<
"Could not tear down the EGL context. Possible resource leak.";
134 FML_LOG(
ERROR) <<
"Could not tear down the EGL resource context. Possible "
140std::unique_ptr<AndroidEGLSurface> AndroidContextGLSkia::CreateOnscreenSurface(
142 if (
window->IsFakeWindow()) {
143 return CreatePbufferSurface();
145 EGLDisplay display = environment_->Display();
147 const EGLint attribs[] = {EGL_NONE};
149 EGLSurface
surface = eglCreateWindowSurface(
151 reinterpret_cast<EGLNativeWindowType
>(
window->handle()), attribs);
152 return std::make_unique<AndroidEGLSurface>(
surface, display, context_);
156std::unique_ptr<AndroidEGLSurface>
157AndroidContextGLSkia::CreateOffscreenSurface()
const {
160 EGLDisplay display = environment_->Display();
162 const EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
164 EGLSurface
surface = eglCreatePbufferSurface(display, config_, attribs);
165 return std::make_unique<AndroidEGLSurface>(
surface, display,
169std::unique_ptr<AndroidEGLSurface> AndroidContextGLSkia::CreatePbufferSurface()
171 EGLDisplay display = environment_->Display();
173 const EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
175 EGLSurface
surface = eglCreatePbufferSurface(display, config_, attribs);
176 return std::make_unique<AndroidEGLSurface>(
surface, display, context_);
183bool AndroidContextGLSkia::IsValid()
const {
187bool AndroidContextGLSkia::ClearCurrent()
const {
188 if (eglGetCurrentContext() != context_) {
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";
200EGLContext AndroidContextGLSkia::GetEGLContext()
const {
204EGLDisplay AndroidContextGLSkia::GetEGLDisplay()
const {
205 return environment_->Display();
208EGLContext AndroidContextGLSkia::CreateNewContext()
const {
211 std::tie(success, context) =
212 CreateContext(environment_->Display(), config_, EGL_NO_CONTEXT);
213 return success ? context : EGL_NO_CONTEXT;
AndroidContextGLSkia(fml::RefPtr< AndroidEnvironmentGL > environment, const TaskRunners &taskRunners)
Holds state that is shared across Android surfaces.
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
fml::RefPtr< fml::TaskRunner > GetPlatformTaskRunner() const
static void RunNowOrPostTask(const fml::RefPtr< fml::TaskRunner > &runner, const fml::closure &task)
virtual bool RunsTasksOnCurrentThread()
TaskRunners task_runners_
#define FML_LOG(severity)
#define FML_DCHECK(condition)
static dart::SimpleHashMap * environment
static bool TeardownContext(EGLDisplay display, EGLContext context)
std::pair< bool, T > EGLResult
static EGLResult< EGLConfig > ChooseEGLConfiguration(EGLDisplay display)
static EGLResult< EGLContext > CreateContext(EGLDisplay display, EGLConfig config, EGLContext share=EGL_NO_CONTEXT)