1 // Copyright 2013 The Flutter Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "flutter/shell/platform/android/android_surface_gl.h" 6 7 #include <utility> 8 9 #include "flutter/fml/logging.h" 10 #include "flutter/fml/memory/ref_ptr.h" 11 12 namespace flutter { 13 CreateResourceLoadingContext()14static fml::RefPtr<AndroidContextGL> CreateResourceLoadingContext() { 15 auto environment = fml::MakeRefCounted<AndroidEnvironmentGL>(); 16 if (!environment->IsValid()) { 17 return nullptr; 18 } 19 20 auto context = fml::MakeRefCounted<AndroidContextGL>(environment); 21 if (!context->IsValid()) { 22 return nullptr; 23 } 24 25 return context; 26 } 27 AndroidSurfaceGL()28AndroidSurfaceGL::AndroidSurfaceGL() { 29 // Acquire the offscreen context. 30 offscreen_context_ = CreateResourceLoadingContext(); 31 32 if (!offscreen_context_ || !offscreen_context_->IsValid()) { 33 offscreen_context_ = nullptr; 34 } 35 } 36 37 AndroidSurfaceGL::~AndroidSurfaceGL() = default; 38 IsOffscreenContextValid() const39bool AndroidSurfaceGL::IsOffscreenContextValid() const { 40 return offscreen_context_ && offscreen_context_->IsValid(); 41 } 42 TeardownOnScreenContext()43void AndroidSurfaceGL::TeardownOnScreenContext() { 44 if (onscreen_context_) { 45 onscreen_context_->ClearCurrent(); 46 } 47 onscreen_context_ = nullptr; 48 } 49 IsValid() const50bool AndroidSurfaceGL::IsValid() const { 51 if (!onscreen_context_ || !offscreen_context_) { 52 return false; 53 } 54 55 return onscreen_context_->IsValid() && offscreen_context_->IsValid(); 56 } 57 CreateGPUSurface()58std::unique_ptr<Surface> AndroidSurfaceGL::CreateGPUSurface() { 59 auto surface = std::make_unique<GPUSurfaceGL>(this, true); 60 return surface->IsValid() ? std::move(surface) : nullptr; 61 } 62 OnScreenSurfaceResize(const SkISize & size) const63bool AndroidSurfaceGL::OnScreenSurfaceResize(const SkISize& size) const { 64 FML_DCHECK(onscreen_context_ && onscreen_context_->IsValid()); 65 return onscreen_context_->Resize(size); 66 } 67 ResourceContextMakeCurrent()68bool AndroidSurfaceGL::ResourceContextMakeCurrent() { 69 FML_DCHECK(offscreen_context_ && offscreen_context_->IsValid()); 70 return offscreen_context_->MakeCurrent(); 71 } 72 ResourceContextClearCurrent()73bool AndroidSurfaceGL::ResourceContextClearCurrent() { 74 FML_DCHECK(offscreen_context_ && offscreen_context_->IsValid()); 75 return offscreen_context_->ClearCurrent(); 76 } 77 SetNativeWindow(fml::RefPtr<AndroidNativeWindow> window)78bool AndroidSurfaceGL::SetNativeWindow( 79 fml::RefPtr<AndroidNativeWindow> window) { 80 // In any case, we want to get rid of our current onscreen context. 81 onscreen_context_ = nullptr; 82 83 // If the offscreen context has not been setup, we dont have the sharegroup. 84 // So bail. 85 if (!offscreen_context_ || !offscreen_context_->IsValid()) { 86 return false; 87 } 88 89 // Create the onscreen context. 90 onscreen_context_ = fml::MakeRefCounted<AndroidContextGL>( 91 offscreen_context_->Environment(), 92 offscreen_context_.get() /* sharegroup */); 93 94 if (!onscreen_context_->IsValid()) { 95 onscreen_context_ = nullptr; 96 return false; 97 } 98 99 if (!onscreen_context_->CreateWindowSurface(std::move(window))) { 100 onscreen_context_ = nullptr; 101 return false; 102 } 103 104 return true; 105 } 106 GLContextMakeCurrent()107bool AndroidSurfaceGL::GLContextMakeCurrent() { 108 FML_DCHECK(onscreen_context_ && onscreen_context_->IsValid()); 109 return onscreen_context_->MakeCurrent(); 110 } 111 GLContextClearCurrent()112bool AndroidSurfaceGL::GLContextClearCurrent() { 113 FML_DCHECK(onscreen_context_ && onscreen_context_->IsValid()); 114 return onscreen_context_->ClearCurrent(); 115 } 116 GLContextPresent()117bool AndroidSurfaceGL::GLContextPresent() { 118 FML_DCHECK(onscreen_context_ && onscreen_context_->IsValid()); 119 return onscreen_context_->SwapBuffers(); 120 } 121 GLContextFBO() const122intptr_t AndroidSurfaceGL::GLContextFBO() const { 123 FML_DCHECK(onscreen_context_ && onscreen_context_->IsValid()); 124 // The default window bound framebuffer on Android. 125 return 0; 126 } 127 128 // |GPUSurfaceGLDelegate| GetExternalViewEmbedder()129ExternalViewEmbedder* AndroidSurfaceGL::GetExternalViewEmbedder() { 130 return nullptr; 131 } 132 133 } // namespace flutter 134