From 6a90b7f95bd87e74b8d6e6771729644527bbc365 Mon Sep 17 00:00:00 2001 From: LiuJiLong Date: Tue, 27 Aug 2019 17:00:09 +0800 Subject: [PATCH] Reuse texture cache in ios_external_texture_gl In current implementation, external texture data flow is a producer-consumer model. When painting external texture, it always asks registered external texture object to produce new CVPixelBuffer, then transforms it to texture. `MarkNewFrameAvailable` function is ignored. This commit changes the dataflow. Now ios_external_texture_gl caches previous opengl texture, if no new frame are available, it do not `copyPixelBuffer` method, just uses cached opengl texture to draw. This is a re-land of flutter/engine#9806, which was previously reverted in flutter/engine#11522. --- shell/platform/darwin/ios/ios_external_texture_gl.h | 2 ++ .../platform/darwin/ios/ios_external_texture_gl.mm | 13 +++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/shell/platform/darwin/ios/ios_external_texture_gl.h b/shell/platform/darwin/ios/ios_external_texture_gl.h index 923f7410632e8..88be5cc7c466a 100644 --- a/shell/platform/darwin/ios/ios_external_texture_gl.h +++ b/shell/platform/darwin/ios/ios_external_texture_gl.h @@ -30,7 +30,9 @@ class IOSExternalTextureGL : public flutter::Texture { void CreateTextureFromPixelBuffer(); void EnsureTextureCacheExists(); + bool NeedUpdateTexture(bool freeze); + bool new_frame_ready_ = false; NSObject* external_texture_; fml::CFRef cache_ref_; fml::CFRef texture_ref_; diff --git a/shell/platform/darwin/ios/ios_external_texture_gl.mm b/shell/platform/darwin/ios/ios_external_texture_gl.mm index e7e15d5719196..a5e729195ce34 100644 --- a/shell/platform/darwin/ios/ios_external_texture_gl.mm +++ b/shell/platform/darwin/ios/ios_external_texture_gl.mm @@ -53,17 +53,24 @@ } } +bool IOSExternalTextureGL::NeedUpdateTexture(bool freeze) { + // Update texture if `texture_ref_` is reset to `nullptr` when GrContext + // is destroied or new frame is ready. + return (!freeze && new_frame_ready_) || !texture_ref_; +} + void IOSExternalTextureGL::Paint(SkCanvas& canvas, const SkRect& bounds, bool freeze, GrContext* context) { EnsureTextureCacheExists(); - if (!freeze) { + if (NeedUpdateTexture(freeze)) { auto pixelBuffer = [external_texture_ copyPixelBuffer]; if (pixelBuffer) { buffer_ref_.Reset(pixelBuffer); } CreateTextureFromPixelBuffer(); + new_frame_ready_ = false; } if (!texture_ref_) { return; @@ -93,6 +100,8 @@ cache_ref_.Reset(nullptr); } -void IOSExternalTextureGL::MarkNewFrameAvailable() {} +void IOSExternalTextureGL::MarkNewFrameAvailable() { + new_frame_ready_ = true; +} } // namespace flutter