Block image decoding on iOS in the background (flutter/engine#4384)
* Add synchronization to ResourceContext usage * debug * Use unique ptr factory * formatting * left more notes for future readers
This commit is contained in:
@@ -53,13 +53,17 @@ sk_sp<SkImage> DecodeImage(sk_sp<SkData> buffer, size_t trace_id) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
GrContext* context = ResourceContext::Get();
|
||||
std::unique_ptr<ResourceContext> resourceContext = ResourceContext::Acquire();
|
||||
GrContext* context = resourceContext->Get();
|
||||
if (context) {
|
||||
// This indicates that we do not want a "linear blending" decode.
|
||||
sk_sp<SkColorSpace> 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<SkImage> MultiFrameCodec::GetNextFrameImage() {
|
||||
}
|
||||
}
|
||||
|
||||
GrContext* context = ResourceContext::Get();
|
||||
std::unique_ptr<ResourceContext> 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<SkImage> 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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> ResourceContext::Acquire() {
|
||||
return std::make_unique<ResourceContext>();
|
||||
}
|
||||
|
||||
void ResourceContext::Freeze() {
|
||||
fxl::MutexLocker lock(&g_mutex);
|
||||
g_freeze = true;
|
||||
}
|
||||
|
||||
void ResourceContext::Unfreeze() {
|
||||
fxl::MutexLocker lock(&g_mutex);
|
||||
g_freeze = false;
|
||||
}
|
||||
|
||||
} // namespace blink
|
||||
|
||||
@@ -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<ResourceContext> 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
|
||||
|
||||
@@ -167,7 +167,9 @@ void PlatformView::SetupResourceContextOnIOThread() {
|
||||
|
||||
void PlatformView::SetupResourceContextOnIOThreadPerform(
|
||||
fxl::AutoResetWaitableEvent* latch) {
|
||||
if (blink::ResourceContext::Get() != nullptr) {
|
||||
std::unique_ptr<blink::ResourceContext> 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();
|
||||
}
|
||||
|
||||
@@ -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"];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user