Prevent app from accessing the GPU in the background in MultiFrameCodec (flutter/engine#28159)
This commit is contained in:
@@ -197,7 +197,7 @@ void createPath() {
|
||||
void _validatePath(Path path) native 'ValidatePath';
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
void frameCallback(FrameInfo info) {
|
||||
void frameCallback(_Image, int) {
|
||||
print('called back');
|
||||
}
|
||||
|
||||
|
||||
@@ -731,5 +731,94 @@ TEST_F(ImageDecoderFixtureTest,
|
||||
latch.Wait();
|
||||
}
|
||||
|
||||
TEST_F(ImageDecoderFixtureTest, MultiFrameCodecDidAccessGpuDisabledSyncSwitch) {
|
||||
auto settings = CreateSettingsForFixture();
|
||||
auto vm_ref = DartVMRef::Create(settings);
|
||||
auto vm_data = vm_ref.GetVMData();
|
||||
|
||||
auto gif_mapping = OpenFixtureAsSkData("hello_loop_2.gif");
|
||||
|
||||
ASSERT_TRUE(gif_mapping);
|
||||
|
||||
ImageGeneratorRegistry registry;
|
||||
std::shared_ptr<ImageGenerator> gif_generator =
|
||||
registry.CreateCompatibleGenerator(gif_mapping);
|
||||
ASSERT_TRUE(gif_generator);
|
||||
|
||||
TaskRunners runners(GetCurrentTestName(), // label
|
||||
CreateNewThread("platform"), // platform
|
||||
CreateNewThread("raster"), // raster
|
||||
CreateNewThread("ui"), // ui
|
||||
CreateNewThread("io") // io
|
||||
);
|
||||
|
||||
fml::AutoResetWaitableEvent latch;
|
||||
std::unique_ptr<TestIOManager> io_manager;
|
||||
fml::RefPtr<MultiFrameCodec> codec;
|
||||
|
||||
// Setup the IO manager.
|
||||
runners.GetIOTaskRunner()->PostTask([&]() {
|
||||
io_manager = std::make_unique<TestIOManager>(runners.GetIOTaskRunner());
|
||||
latch.Signal();
|
||||
});
|
||||
latch.Wait();
|
||||
|
||||
auto isolate = RunDartCodeInIsolate(vm_ref, settings, runners, "main", {},
|
||||
GetDefaultKernelFilePath(),
|
||||
io_manager->GetWeakIOManager());
|
||||
|
||||
runners.GetUITaskRunner()->PostTask([&]() {
|
||||
fml::AutoResetWaitableEvent isolate_latch;
|
||||
|
||||
EXPECT_TRUE(isolate->RunInIsolateScope([&]() -> bool {
|
||||
Dart_Handle library = Dart_RootLibrary();
|
||||
if (Dart_IsError(library)) {
|
||||
isolate_latch.Signal();
|
||||
return false;
|
||||
}
|
||||
Dart_Handle closure =
|
||||
Dart_GetField(library, Dart_NewStringFromCString("frameCallback"));
|
||||
if (Dart_IsError(closure) || !Dart_IsClosure(closure)) {
|
||||
isolate_latch.Signal();
|
||||
return false;
|
||||
}
|
||||
|
||||
EXPECT_FALSE(io_manager->did_access_is_gpu_disabled_sync_switch_);
|
||||
codec = fml::MakeRefCounted<MultiFrameCodec>(std::move(gif_generator));
|
||||
codec->getNextFrame(closure);
|
||||
isolate_latch.Signal();
|
||||
return true;
|
||||
}));
|
||||
isolate_latch.Wait();
|
||||
|
||||
latch.Signal();
|
||||
});
|
||||
latch.Wait();
|
||||
|
||||
runners.GetIOTaskRunner()->PostTask([&]() {
|
||||
EXPECT_TRUE(io_manager->did_access_is_gpu_disabled_sync_switch_);
|
||||
latch.Signal();
|
||||
});
|
||||
latch.Wait();
|
||||
|
||||
// Destroy the Isolate
|
||||
isolate = nullptr;
|
||||
|
||||
// Destroy the MultiFrameCodec
|
||||
runners.GetUITaskRunner()->PostTask([&]() {
|
||||
codec = nullptr;
|
||||
latch.Signal();
|
||||
});
|
||||
latch.Wait();
|
||||
|
||||
// Destroy the IO manager
|
||||
runners.GetIOTaskRunner()->PostTask([&]() {
|
||||
io_manager.reset();
|
||||
latch.Signal();
|
||||
});
|
||||
|
||||
latch.Wait();
|
||||
}
|
||||
|
||||
} // namespace testing
|
||||
} // namespace flutter
|
||||
|
||||
@@ -76,7 +76,8 @@ static bool CopyToBitmap(SkBitmap* dst,
|
||||
}
|
||||
|
||||
sk_sp<SkImage> MultiFrameCodec::State::GetNextFrameImage(
|
||||
fml::WeakPtr<GrDirectContext> resourceContext) {
|
||||
fml::WeakPtr<GrDirectContext> resourceContext,
|
||||
const std::shared_ptr<const fml::SyncSwitch>& gpu_disable_sync_switch) {
|
||||
SkBitmap bitmap = SkBitmap();
|
||||
SkImageInfo info = generator_->GetInfo().makeColorType(kN32_SkColorType);
|
||||
if (info.alphaType() == kUnpremul_SkAlphaType) {
|
||||
@@ -122,18 +123,30 @@ sk_sp<SkImage> MultiFrameCodec::State::GetNextFrameImage(
|
||||
lastRequiredFrame_ = std::make_unique<SkBitmap>(bitmap);
|
||||
lastRequiredFrameIndex_ = nextFrameIndex_;
|
||||
}
|
||||
sk_sp<SkImage> result;
|
||||
|
||||
if (resourceContext) {
|
||||
SkPixmap pixmap(bitmap.info(), bitmap.pixelRef()->pixels(),
|
||||
bitmap.pixelRef()->rowBytes());
|
||||
return SkImage::MakeCrossContextFromPixmap(resourceContext.get(), pixmap,
|
||||
true);
|
||||
} else {
|
||||
// Defer decoding until time of draw later on the raster thread. Can happen
|
||||
// when GL operations are currently forbidden such as in the background
|
||||
// on iOS.
|
||||
return SkImage::MakeFromBitmap(bitmap);
|
||||
}
|
||||
gpu_disable_sync_switch->Execute(
|
||||
fml::SyncSwitch::Handlers()
|
||||
.SetIfTrue([&result, &bitmap] {
|
||||
// Defer decoding until time of draw later on the raster thread. Can
|
||||
// happen when GL operations are currently forbidden such as in the
|
||||
// background on iOS.
|
||||
result = SkImage::MakeFromBitmap(bitmap);
|
||||
})
|
||||
.SetIfFalse([&result, &resourceContext, &bitmap] {
|
||||
if (resourceContext) {
|
||||
SkPixmap pixmap(bitmap.info(), bitmap.pixelRef()->pixels(),
|
||||
bitmap.pixelRef()->rowBytes());
|
||||
result = SkImage::MakeCrossContextFromPixmap(
|
||||
resourceContext.get(), pixmap, true);
|
||||
} else {
|
||||
// Defer decoding until time of draw later on the raster thread.
|
||||
// Can happen when GL operations are currently forbidden such as
|
||||
// in the background on iOS.
|
||||
result = SkImage::MakeFromBitmap(bitmap);
|
||||
}
|
||||
}));
|
||||
return result;
|
||||
}
|
||||
|
||||
void MultiFrameCodec::State::GetNextFrameAndInvokeCallback(
|
||||
@@ -141,10 +154,12 @@ void MultiFrameCodec::State::GetNextFrameAndInvokeCallback(
|
||||
fml::RefPtr<fml::TaskRunner> ui_task_runner,
|
||||
fml::WeakPtr<GrDirectContext> resourceContext,
|
||||
fml::RefPtr<flutter::SkiaUnrefQueue> unref_queue,
|
||||
const std::shared_ptr<const fml::SyncSwitch>& gpu_disable_sync_switch,
|
||||
size_t trace_id) {
|
||||
fml::RefPtr<CanvasImage> image = nullptr;
|
||||
int duration = 0;
|
||||
sk_sp<SkImage> skImage = GetNextFrameImage(resourceContext);
|
||||
sk_sp<SkImage> skImage =
|
||||
GetNextFrameImage(resourceContext, gpu_disable_sync_switch);
|
||||
if (skImage) {
|
||||
image = CanvasImage::Create();
|
||||
image->set_image({skImage, std::move(unref_queue)});
|
||||
@@ -200,7 +215,7 @@ Dart_Handle MultiFrameCodec::getNextFrame(Dart_Handle callback_handle) {
|
||||
state->GetNextFrameAndInvokeCallback(
|
||||
std::move(callback), std::move(ui_task_runner),
|
||||
io_manager->GetResourceContext(), io_manager->GetSkiaUnrefQueue(),
|
||||
trace_id);
|
||||
io_manager->GetIsGpuDisabledSyncSwitch(), trace_id);
|
||||
}));
|
||||
|
||||
return Dart_Null();
|
||||
|
||||
@@ -54,13 +54,15 @@ class MultiFrameCodec : public Codec {
|
||||
int lastRequiredFrameIndex_ = -1;
|
||||
|
||||
sk_sp<SkImage> GetNextFrameImage(
|
||||
fml::WeakPtr<GrDirectContext> resourceContext);
|
||||
fml::WeakPtr<GrDirectContext> resourceContext,
|
||||
const std::shared_ptr<const fml::SyncSwitch>& gpu_disable_sync_switch);
|
||||
|
||||
void GetNextFrameAndInvokeCallback(
|
||||
std::unique_ptr<DartPersistentValue> callback,
|
||||
fml::RefPtr<fml::TaskRunner> ui_task_runner,
|
||||
fml::WeakPtr<GrDirectContext> resourceContext,
|
||||
fml::RefPtr<flutter::SkiaUnrefQueue> unref_queue,
|
||||
const std::shared_ptr<const fml::SyncSwitch>& gpu_disable_sync_switch,
|
||||
size_t trace_id);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user