diff --git a/engine/src/flutter/lib/ui/painting/codec.cc b/engine/src/flutter/lib/ui/painting/codec.cc index e218f342ca..3e47ba86d5 100644 --- a/engine/src/flutter/lib/ui/painting/codec.cc +++ b/engine/src/flutter/lib/ui/painting/codec.cc @@ -53,13 +53,17 @@ sk_sp DecodeImage(sk_sp buffer, size_t trace_id) { return nullptr; } - GrContext* context = ResourceContext::Get(); + std::unique_ptr resourceContext = ResourceContext::Acquire(); + GrContext* context = resourceContext->Get(); if (context) { // This indicates that we do not want a "linear blending" decode. sk_sp dstColorSpace = nullptr; return SkImage::MakeCrossContextFromEncoded(context, std::move(buffer), false, dstColorSpace.get()); } else { + // Defer decoding until time of draw later on the GPU thread. Can happen + // when GL operations are currently forbidden such as in the background + // on iOS. return SkImage::MakeFromEncoded(std::move(buffer)); } } @@ -241,7 +245,8 @@ sk_sp MultiFrameCodec::GetNextFrameImage() { } } - GrContext* context = ResourceContext::Get(); + std::unique_ptr resourceContext = ResourceContext::Acquire(); + GrContext* context = resourceContext->Get(); if (context) { SkPixmap pixmap(bitmap.info(), bitmap.pixelRef()->pixels(), bitmap.pixelRef()->rowBytes()); @@ -250,6 +255,9 @@ sk_sp MultiFrameCodec::GetNextFrameImage() { return SkImage::MakeCrossContextFromPixmap(context, pixmap, false, dstColorSpace.get()); } else { + // Defer decoding until time of draw later on the GPU thread. Can happen + // when GL operations are currently forbidden such as in the background + // on iOS. return SkImage::MakeFromBitmap(bitmap); } } diff --git a/engine/src/flutter/lib/ui/painting/resource_context.cc b/engine/src/flutter/lib/ui/painting/resource_context.cc index fecbf356b9..bc8118a546 100644 --- a/engine/src/flutter/lib/ui/painting/resource_context.cc +++ b/engine/src/flutter/lib/ui/painting/resource_context.cc @@ -5,21 +5,46 @@ #include "flutter/lib/ui/painting/resource_context.h" #include "lib/fxl/logging.h" +#include "lib/fxl/synchronization/mutex.h" namespace blink { namespace { static GrContext* g_context = nullptr; +static fxl::Mutex g_mutex; +static volatile bool g_freeze = false; } // namespace +ResourceContext::ResourceContext() { + g_mutex.Lock(); +} + +ResourceContext::~ResourceContext() { + g_mutex.Unlock(); +} + void ResourceContext::Set(GrContext* context) { FXL_DCHECK(!g_context); g_context = context; } GrContext* ResourceContext::Get() { - return g_context; + return g_freeze ? nullptr : g_context; +} + +std::unique_ptr ResourceContext::Acquire() { + return std::make_unique(); +} + +void ResourceContext::Freeze() { + fxl::MutexLocker lock(&g_mutex); + g_freeze = true; +} + +void ResourceContext::Unfreeze() { + fxl::MutexLocker lock(&g_mutex); + g_freeze = false; } } // namespace blink diff --git a/engine/src/flutter/lib/ui/painting/resource_context.h b/engine/src/flutter/lib/ui/painting/resource_context.h index 0d800f4e37..047c34ce42 100644 --- a/engine/src/flutter/lib/ui/painting/resource_context.h +++ b/engine/src/flutter/lib/ui/painting/resource_context.h @@ -5,14 +5,54 @@ #ifndef FLUTTER_LIB_UI_PAINTING_RESOURCE_CONTEXT_H_ #define FLUTTER_LIB_UI_PAINTING_RESOURCE_CONTEXT_H_ +#include "lib/fxl/macros.h" #include "third_party/skia/include/gpu/GrContext.h" namespace blink { class ResourceContext { public: + /** + * Globally set the GrContext singleton instance. + */ static void Set(GrContext* context); - static GrContext* Get(); + + /** + * Acquire a GrContext wrapping ResourceContext that's also an exclusive mutex + * on GrContext operations. + * + * Destructing the ResourceContext frees the mutex. + */ + static std::unique_ptr Acquire(); + + /** + * Synchronously signal a freeze on GrContext operations. + * + * ResourceContext instances will return nullptr on GrContext Get until + * unfrozen. + */ + static void Freeze(); + + /** + * Synchronously unfreeze GrContext operations. + * + * ResourceContext instances will continue to return the global GrContext + * instance on Get. + */ + static void Unfreeze(); + + ResourceContext(); + ~ResourceContext(); + + /** + * Returns global GrContext instance. May return null when operations are + * frozen. + * + * Happens on iOS when background operations on GrContext are forbidden. + */ + GrContext* Get(); + + FXL_DISALLOW_COPY_AND_ASSIGN(ResourceContext); }; } // namespace blink diff --git a/engine/src/flutter/shell/common/platform_view.cc b/engine/src/flutter/shell/common/platform_view.cc index 5479a96b2b..eccd26916d 100644 --- a/engine/src/flutter/shell/common/platform_view.cc +++ b/engine/src/flutter/shell/common/platform_view.cc @@ -167,7 +167,9 @@ void PlatformView::SetupResourceContextOnIOThread() { void PlatformView::SetupResourceContextOnIOThreadPerform( fxl::AutoResetWaitableEvent* latch) { - if (blink::ResourceContext::Get() != nullptr) { + std::unique_ptr resourceContext = + blink::ResourceContext::Acquire(); + if (resourceContext->Get() != nullptr) { // The resource context was already setup. This could happen if platforms // try to setup a context multiple times, or, if there are multiple platform // views. In any case, there is nothing else to do. So just signal the @@ -199,8 +201,8 @@ void PlatformView::SetupResourceContextOnIOThreadPerform( // Do not cache textures created by the image decoder. These textures should // be deleted when they are no longer referenced by an SkImage. - if (blink::ResourceContext::Get()) - blink::ResourceContext::Get()->setResourceCacheLimits(0, 0); + if (resourceContext->Get()) + resourceContext->Get()->setResourceCacheLimits(0, 0); latch->Signal(); } diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm index 8874ffff0a..1f19abaf8e 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm @@ -11,6 +11,7 @@ #include "flutter/fml/platform/darwin/platform_version.h" #include "flutter/fml/platform/darwin/scoped_block.h" #include "flutter/fml/platform/darwin/scoped_nsobject.h" +#include "flutter/lib/ui/painting/resource_context.h" #include "flutter/shell/platform/darwin/common/buffer_conversions.h" #include "flutter/shell/platform/darwin/common/platform_mac.h" #include "flutter/shell/platform/darwin/ios/framework/Headers/FlutterCodecs.h" @@ -398,6 +399,8 @@ class PlatformMessageResponseDarwin : public blink::PlatformMessageResponse { - (void)applicationDidEnterBackground:(NSNotification*)notification { TRACE_EVENT0("flutter", "applicationDidEnterBackground"); [self surfaceUpdated:NO]; + // GrContext operations are blocked when the app is in the background. + blink::ResourceContext::Freeze(); [_lifecycleChannel.get() sendMessage:@"AppLifecycleState.paused"]; } @@ -405,6 +408,7 @@ class PlatformMessageResponseDarwin : public blink::PlatformMessageResponse { TRACE_EVENT0("flutter", "applicationWillEnterForeground"); if (_viewportMetrics.physical_width) [self surfaceUpdated:YES]; + blink::ResourceContext::Unfreeze(); [_lifecycleChannel.get() sendMessage:@"AppLifecycleState.inactive"]; }