@@ -20,14 +20,14 @@ constexpr char kEmulatorRendererPrefix[] =
2020} // anonymous namespace
2121
2222AndroidSurfaceGL::AndroidSurfaceGL (
23- const AndroidContext& android_context,
23+ const std::shared_ptr< AndroidContext> & android_context,
2424 std::shared_ptr<PlatformViewAndroidJNI> jni_facade)
25- : android_context_( static_cast < const AndroidContextGL&>( android_context) ),
25+ : AndroidSurface( android_context),
2626 native_window_ (nullptr ),
2727 onscreen_surface_(nullptr ),
2828 offscreen_surface_(nullptr ) {
2929 // Acquire the offscreen surface.
30- offscreen_surface_ = android_context_. CreateOffscreenSurface ();
30+ offscreen_surface_ = GLContextPtr ()-> CreateOffscreenSurface ();
3131 if (!offscreen_surface_->IsValid ()) {
3232 offscreen_surface_ = nullptr ;
3333 }
@@ -39,20 +39,27 @@ void AndroidSurfaceGL::TeardownOnScreenContext() {
3939 // When the onscreen surface is destroyed, the context and the surface
4040 // instance should be deleted. Issue:
4141 // https://github.com/flutter/flutter/issues/64414
42- android_context_. ClearCurrent ();
42+ GLContextPtr ()-> ClearCurrent ();
4343 onscreen_surface_ = nullptr ;
4444}
4545
4646bool AndroidSurfaceGL::IsValid () const {
47- return offscreen_surface_ && android_context_. IsValid ();
47+ return offscreen_surface_ && GLContextPtr ()-> IsValid ();
4848}
4949
5050std::unique_ptr<Surface> AndroidSurfaceGL::CreateGPUSurface (
5151 GrDirectContext* gr_context) {
5252 if (gr_context) {
5353 return std::make_unique<GPUSurfaceGL>(sk_ref_sp (gr_context), this , true );
54+ } else {
55+ sk_sp<GrDirectContext> main_skia_context =
56+ GLContextPtr ()->GetMainSkiaContext ();
57+ if (!main_skia_context) {
58+ main_skia_context = GPUSurfaceGL::MakeGLContext (this );
59+ GLContextPtr ()->SetMainSkiaContext (main_skia_context);
60+ }
61+ return std::make_unique<GPUSurfaceGL>(main_skia_context, this , true );
5462 }
55- return std::make_unique<GPUSurfaceGL>(this , true );
5663}
5764
5865bool AndroidSurfaceGL::OnScreenSurfaceResize (const SkISize& size) {
@@ -64,12 +71,12 @@ bool AndroidSurfaceGL::OnScreenSurfaceResize(const SkISize& size) {
6471 return true ;
6572 }
6673
67- android_context_. ClearCurrent ();
74+ GLContextPtr ()-> ClearCurrent ();
6875
6976 // Ensure the destructor is called since it destroys the `EGLSurface` before
7077 // creating a new onscreen surface.
7178 onscreen_surface_ = nullptr ;
72- onscreen_surface_ = android_context_. CreateOnscreenSurface (native_window_);
79+ onscreen_surface_ = GLContextPtr ()-> CreateOnscreenSurface (native_window_);
7380 if (!onscreen_surface_->IsValid ()) {
7481 FML_LOG (ERROR) << " Unable to create EGL window surface on resize." ;
7582 return false ;
@@ -85,7 +92,7 @@ bool AndroidSurfaceGL::ResourceContextMakeCurrent() {
8592
8693bool AndroidSurfaceGL::ResourceContextClearCurrent () {
8794 FML_DCHECK (IsValid ());
88- return android_context_. ClearCurrent ();
95+ return GLContextPtr ()-> ClearCurrent ();
8996}
9097
9198bool AndroidSurfaceGL::SetNativeWindow (
@@ -97,7 +104,7 @@ bool AndroidSurfaceGL::SetNativeWindow(
97104 // creating a new onscreen surface.
98105 onscreen_surface_ = nullptr ;
99106 // Create the onscreen surface.
100- onscreen_surface_ = android_context_. CreateOnscreenSurface (window);
107+ onscreen_surface_ = GLContextPtr ()-> CreateOnscreenSurface (window);
101108 if (!onscreen_surface_->IsValid ()) {
102109 return false ;
103110 }
@@ -114,7 +121,7 @@ std::unique_ptr<GLContextResult> AndroidSurfaceGL::GLContextMakeCurrent() {
114121
115122bool AndroidSurfaceGL::GLContextClearCurrent () {
116123 FML_DCHECK (IsValid ());
117- return android_context_. ClearCurrent ();
124+ return GLContextPtr ()-> ClearCurrent ();
118125}
119126
120127bool AndroidSurfaceGL::GLContextPresent (uint32_t fbo_id) {
@@ -143,7 +150,7 @@ sk_sp<const GrGLInterface> AndroidSurfaceGL::GetGLInterface() const {
143150 reinterpret_cast <const char *>(glGetString (GL_RENDERER));
144151 if (gl_renderer && strncmp (gl_renderer, kEmulatorRendererPrefix ,
145152 strlen (kEmulatorRendererPrefix )) == 0 ) {
146- EGLContext new_context = android_context_. CreateNewContext ();
153+ EGLContext new_context = GLContextPtr ()-> CreateNewContext ();
147154 if (new_context != EGL_NO_CONTEXT) {
148155 EGLContext old_context = eglGetCurrentContext ();
149156 EGLDisplay display = eglGetCurrentDisplay ();
@@ -162,4 +169,8 @@ sk_sp<const GrGLInterface> AndroidSurfaceGL::GetGLInterface() const {
162169 return GPUSurfaceGLDelegate::GetGLInterface ();
163170}
164171
172+ AndroidContextGL* AndroidSurfaceGL::GLContextPtr () const {
173+ return reinterpret_cast <AndroidContextGL*>(android_context_.get ());
174+ }
175+
165176} // namespace flutter
0 commit comments