From 08a3eedf0de535e010d432234f355f9f0a245ad1 Mon Sep 17 00:00:00 2001 From: Kaushik Iska Date: Tue, 30 Apr 2019 15:25:20 -0700 Subject: [PATCH] Re-create texture from pixel buffer onGrContextCreate (flutter/engine#8792) * Re-create texture from pixel buffer onGrContextCreate OnGrContextDestroy we destroy the texture, this is because we can not access it from the potentially new context that we get on bringing the app back to foreground. To show a valid texture on fg, we need to preserve the pixel buffer, using which we will create the new texture. * ensure texture cache exists * only reset pixel buffer if not null. --- .../darwin/ios/ios_external_texture_gl.h | 5 ++ .../darwin/ios/ios_external_texture_gl.mm | 55 ++++++++++++------- 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/engine/src/flutter/shell/platform/darwin/ios/ios_external_texture_gl.h b/engine/src/flutter/shell/platform/darwin/ios/ios_external_texture_gl.h index 3c15ac9148..923f741063 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/ios_external_texture_gl.h +++ b/engine/src/flutter/shell/platform/darwin/ios/ios_external_texture_gl.h @@ -27,9 +27,14 @@ class IOSExternalTextureGL : public flutter::Texture { void MarkNewFrameAvailable() override; private: + void CreateTextureFromPixelBuffer(); + + void EnsureTextureCacheExists(); + NSObject* external_texture_; fml::CFRef cache_ref_; fml::CFRef texture_ref_; + fml::CFRef buffer_ref_; FML_DISALLOW_COPY_AND_ASSIGN(IOSExternalTextureGL); }; diff --git a/engine/src/flutter/shell/platform/darwin/ios/ios_external_texture_gl.mm b/engine/src/flutter/shell/platform/darwin/ios/ios_external_texture_gl.mm index ff0be31447..e7e15d5719 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/ios_external_texture_gl.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/ios_external_texture_gl.mm @@ -22,10 +22,7 @@ IOSExternalTextureGL::IOSExternalTextureGL(int64_t textureId, IOSExternalTextureGL::~IOSExternalTextureGL() = default; -void IOSExternalTextureGL::Paint(SkCanvas& canvas, - const SkRect& bounds, - bool freeze, - GrContext* context) { +void IOSExternalTextureGL::EnsureTextureCacheExists() { if (!cache_ref_) { CVOpenGLESTextureCacheRef cache; CVReturn err = CVOpenGLESTextureCacheCreate(kCFAllocatorDefault, NULL, @@ -37,22 +34,36 @@ void IOSExternalTextureGL::Paint(SkCanvas& canvas, return; } } - fml::CFRef bufferRef; +} + +void IOSExternalTextureGL::CreateTextureFromPixelBuffer() { + if (buffer_ref_ == nullptr) { + return; + } + CVOpenGLESTextureRef texture; + CVReturn err = CVOpenGLESTextureCacheCreateTextureFromImage( + kCFAllocatorDefault, cache_ref_, buffer_ref_, nullptr, GL_TEXTURE_2D, GL_RGBA, + static_cast(CVPixelBufferGetWidth(buffer_ref_)), + static_cast(CVPixelBufferGetHeight(buffer_ref_)), GL_BGRA, GL_UNSIGNED_BYTE, 0, + &texture); + if (err != noErr) { + FML_LOG(WARNING) << "Could not create texture from pixel buffer: " << err; + } else { + texture_ref_.Reset(texture); + } +} + +void IOSExternalTextureGL::Paint(SkCanvas& canvas, + const SkRect& bounds, + bool freeze, + GrContext* context) { + EnsureTextureCacheExists(); if (!freeze) { - bufferRef.Reset([external_texture_ copyPixelBuffer]); - if (bufferRef != nullptr) { - CVOpenGLESTextureRef texture; - CVReturn err = CVOpenGLESTextureCacheCreateTextureFromImage( - kCFAllocatorDefault, cache_ref_, bufferRef, nullptr, GL_TEXTURE_2D, GL_RGBA, - static_cast(CVPixelBufferGetWidth(bufferRef)), - static_cast(CVPixelBufferGetHeight(bufferRef)), GL_BGRA, GL_UNSIGNED_BYTE, 0, - &texture); - texture_ref_.Reset(texture); - if (err != noErr) { - FML_LOG(WARNING) << "Could not create texture from pixel buffer: " << err; - return; - } + auto pixelBuffer = [external_texture_ copyPixelBuffer]; + if (pixelBuffer) { + buffer_ref_.Reset(pixelBuffer); } + CreateTextureFromPixelBuffer(); } if (!texture_ref_) { return; @@ -69,7 +80,13 @@ void IOSExternalTextureGL::Paint(SkCanvas& canvas, } } -void IOSExternalTextureGL::OnGrContextCreated() {} +void IOSExternalTextureGL::OnGrContextCreated() { + // Re-create texture from pixel buffer that was saved before + // OnGrContextDestroyed gets called. + // https://github.com/flutter/flutter/issues/30491 + EnsureTextureCacheExists(); + CreateTextureFromPixelBuffer(); +} void IOSExternalTextureGL::OnGrContextDestroyed() { texture_ref_.Reset(nullptr);