diff --git a/engine/src/flutter/shell/gpu/gpu_surface_gl.cc b/engine/src/flutter/shell/gpu/gpu_surface_gl.cc index 36c2b80976..36551f1c08 100644 --- a/engine/src/flutter/shell/gpu/gpu_surface_gl.cc +++ b/engine/src/flutter/shell/gpu/gpu_surface_gl.cc @@ -113,6 +113,19 @@ static sk_sp WrapOnscreenSurface(GrContext* context, ); } +static sk_sp CreateOffscreenSurface(GrContext* context, + const SkISize& size) { + const SkImageInfo image_info = + SkImageInfo::MakeN32(size.fWidth, size.fHeight, kOpaque_SkAlphaType); + + const SkSurfaceProps surface_props( + SkSurfaceProps::InitType::kLegacyFontHost_InitType); + + return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, image_info, 0, + kBottomLeft_GrSurfaceOrigin, + &surface_props); +} + bool GPUSurfaceGL::CreateOrUpdateSurfaces(const SkISize& size) { if (onscreen_surface_ != nullptr && size == SkISize::Make(onscreen_surface_->width(), @@ -126,13 +139,14 @@ bool GPUSurfaceGL::CreateOrUpdateSurfaces(const SkISize& size) { // Either way, we need to get rid of previous surface. onscreen_surface_ = nullptr; + offscreen_surface_ = nullptr; if (size.isEmpty()) { FXL_LOG(ERROR) << "Cannot create surfaces of empty size."; return false; } - sk_sp onscreen_surface; + sk_sp onscreen_surface, offscreen_surface; onscreen_surface = WrapOnscreenSurface(context_.get(), size, delegate_->GLContextFBO()); @@ -144,7 +158,16 @@ bool GPUSurfaceGL::CreateOrUpdateSurfaces(const SkISize& size) { return false; } + if (delegate_->UseOffscreenSurface()) { + offscreen_surface = CreateOffscreenSurface(context_.get(), size); + if (offscreen_surface == nullptr) { + FXL_LOG(ERROR) << "Could not create offscreen surface."; + return false; + } + } + onscreen_surface_ = std::move(onscreen_surface); + offscreen_surface_ = std::move(offscreen_surface); return true; } @@ -181,6 +204,13 @@ bool GPUSurfaceGL::PresentSurface(SkCanvas* canvas) { return false; } + if (offscreen_surface_ != nullptr) { + TRACE_EVENT0("flutter", "CopyTextureOnscreen"); + SkPaint paint; + onscreen_surface_->getCanvas()->drawImage( + offscreen_surface_->makeImageSnapshot(), 0, 0, &paint); + } + { TRACE_EVENT0("flutter", "SkCanvas::Flush"); onscreen_surface_->getCanvas()->flush(); @@ -196,7 +226,7 @@ sk_sp GPUSurfaceGL::AcquireRenderSurface(const SkISize& size) { return nullptr; } - return onscreen_surface_; + return offscreen_surface_ != nullptr ? offscreen_surface_ : onscreen_surface_; } GrContext* GPUSurfaceGL::GetContext() { diff --git a/engine/src/flutter/shell/gpu/gpu_surface_gl.h b/engine/src/flutter/shell/gpu/gpu_surface_gl.h index 8a29f09cfc..3f951a4481 100644 --- a/engine/src/flutter/shell/gpu/gpu_surface_gl.h +++ b/engine/src/flutter/shell/gpu/gpu_surface_gl.h @@ -22,6 +22,8 @@ class GPUSurfaceGLDelegate { virtual bool GLContextPresent() = 0; virtual intptr_t GLContextFBO() const = 0; + + virtual bool UseOffscreenSurface() const { return false; } }; class GPUSurfaceGL : public Surface { @@ -40,6 +42,7 @@ class GPUSurfaceGL : public Surface { GPUSurfaceGLDelegate* delegate_; sk_sp context_; sk_sp onscreen_surface_; + sk_sp offscreen_surface_; bool valid_ = false; fml::WeakPtrFactory weak_factory_; diff --git a/engine/src/flutter/shell/platform/darwin/ios/ios_surface_gl.h b/engine/src/flutter/shell/platform/darwin/ios/ios_surface_gl.h index 48b8a67018..f7e43e2588 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/ios_surface_gl.h +++ b/engine/src/flutter/shell/platform/darwin/ios/ios_surface_gl.h @@ -36,6 +36,8 @@ class IOSSurfaceGL : public IOSSurface, public GPUSurfaceGLDelegate { intptr_t GLContextFBO() const override; + bool UseOffscreenSurface() const override; + private: IOSGLContext context_; diff --git a/engine/src/flutter/shell/platform/darwin/ios/ios_surface_gl.mm b/engine/src/flutter/shell/platform/darwin/ios/ios_surface_gl.mm index d152521c3f..60756d6094 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/ios_surface_gl.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/ios_surface_gl.mm @@ -36,6 +36,13 @@ intptr_t IOSSurfaceGL::GLContextFBO() const { return IsValid() ? context_.framebuffer() : GL_NONE; } +bool IOSSurfaceGL::UseOffscreenSurface() const { + // The onscreen surface wraps a GL renderbuffer, which is extremely slow to read. + // Certain filter effects require making a copy of the current destination, so we + // always render to an offscreen surface, which will be much quicker to read/copy. + return true; +} + bool IOSSurfaceGL::GLContextMakeCurrent() { return IsValid() ? context_.MakeCurrent() : false; }