Prevent app from accessing the GPU in the background in EncodeImage (flutter/engine#28369)

This commit is contained in:
ColdPaleLight
2021-09-10 00:42:02 +08:00
committed by GitHub
parent 668e0a3c8b
commit 9898f7b464
5 changed files with 169 additions and 50 deletions

View File

@@ -375,6 +375,7 @@ FILE: ../../../flutter/lib/ui/painting/image_descriptor.h
FILE: ../../../flutter/lib/ui/painting/image_dispose_unittests.cc
FILE: ../../../flutter/lib/ui/painting/image_encoding.cc
FILE: ../../../flutter/lib/ui/painting/image_encoding.h
FILE: ../../../flutter/lib/ui/painting/image_encoding_impl.h
FILE: ../../../flutter/lib/ui/painting/image_encoding_unittests.cc
FILE: ../../../flutter/lib/ui/painting/image_filter.cc
FILE: ../../../flutter/lib/ui/painting/image_filter.h

View File

@@ -42,6 +42,7 @@ source_set("ui") {
"painting/image_descriptor.h",
"painting/image_encoding.cc",
"painting/image_encoding.h",
"painting/image_encoding_impl.h",
"painting/image_filter.cc",
"painting/image_filter.h",
"painting/image_generator.cc",

View File

@@ -3,6 +3,7 @@
// found in the LICENSE file.
#include "flutter/lib/ui/painting/image_encoding.h"
#include "flutter/lib/ui/painting/image_encoding_impl.h"
#include <memory>
#include <utility>
@@ -12,11 +13,7 @@
#include "flutter/fml/make_copyable.h"
#include "flutter/fml/trace_event.h"
#include "flutter/lib/ui/painting/image.h"
#include "flutter/lib/ui/ui_dart_state.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkEncodedImageFormat.h"
#include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/core/SkSurface.h"
#include "third_party/tonic/dart_persistent_value.h"
#include "third_party/tonic/logging/dart_invoke.h"
#include "third_party/tonic/typed_data/typed_list.h"
@@ -63,42 +60,14 @@ void InvokeDataCallback(std::unique_ptr<DartPersistentValue> callback,
DartInvoke(callback->value(), {dart_data});
}
sk_sp<SkImage> ConvertToRasterUsingResourceContext(
void ConvertImageToRaster(
sk_sp<SkImage> image,
GrDirectContext* resource_context) {
sk_sp<SkSurface> surface;
SkImageInfo surface_info = SkImageInfo::MakeN32Premul(image->dimensions());
if (resource_context) {
surface = SkSurface::MakeRenderTarget(resource_context, SkBudgeted::kNo,
surface_info);
} else {
surface = SkSurface::MakeRaster(surface_info);
}
if (surface == nullptr || surface->getCanvas() == nullptr) {
FML_LOG(ERROR) << "Could not create a surface to copy the texture into.";
return nullptr;
}
surface->getCanvas()->drawImage(image, 0, 0);
surface->getCanvas()->flush();
auto snapshot = surface->makeImageSnapshot();
if (snapshot == nullptr) {
FML_LOG(ERROR) << "Could not snapshot image to encode.";
return nullptr;
}
return snapshot->makeRasterImage();
}
void ConvertImageToRaster(sk_sp<SkImage> image,
std::function<void(sk_sp<SkImage>)> encode_task,
fml::RefPtr<fml::TaskRunner> raster_task_runner,
fml::RefPtr<fml::TaskRunner> io_task_runner,
GrDirectContext* resource_context,
fml::WeakPtr<SnapshotDelegate> snapshot_delegate) {
std::function<void(sk_sp<SkImage>)> encode_task,
fml::RefPtr<fml::TaskRunner> raster_task_runner,
fml::RefPtr<fml::TaskRunner> io_task_runner,
fml::WeakPtr<GrDirectContext> resource_context,
fml::WeakPtr<SnapshotDelegate> snapshot_delegate,
const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch) {
// Check validity of the image.
if (image == nullptr) {
FML_LOG(ERROR) << "Image was null.";
@@ -132,19 +101,20 @@ void ConvertImageToRaster(sk_sp<SkImage> image,
// to prevent concurrent usage of the image on both the IO and raster threads.
raster_task_runner->PostTask([image, encode_task = std::move(encode_task),
resource_context, snapshot_delegate,
io_task_runner]() {
io_task_runner, is_gpu_disabled_sync_switch]() {
sk_sp<SkImage> raster_image =
snapshot_delegate->ConvertToRasterImage(image);
io_task_runner->PostTask([image, encode_task = std::move(encode_task),
raster_image = std::move(raster_image),
resource_context]() mutable {
resource_context,
is_gpu_disabled_sync_switch]() mutable {
if (!raster_image) {
// The rasterizer was unable to render the cross-context image
// (presumably because it does not have a GrContext). In that case,
// convert the image on the IO thread using the resource context.
raster_image =
ConvertToRasterUsingResourceContext(image, resource_context);
raster_image = ConvertToRasterUsingResourceContext(
image, resource_context, is_gpu_disabled_sync_switch);
}
encode_task(raster_image);
});
@@ -231,8 +201,9 @@ void EncodeImageAndInvokeDataCallback(
fml::RefPtr<fml::TaskRunner> ui_task_runner,
fml::RefPtr<fml::TaskRunner> raster_task_runner,
fml::RefPtr<fml::TaskRunner> io_task_runner,
GrDirectContext* resource_context,
fml::WeakPtr<SnapshotDelegate> snapshot_delegate) {
fml::WeakPtr<GrDirectContext> resource_context,
fml::WeakPtr<SnapshotDelegate> snapshot_delegate,
const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch) {
auto callback_task = fml::MakeCopyable(
[callback = std::move(callback)](sk_sp<SkData> encoded) mutable {
InvokeDataCallback(std::move(callback), std::move(encoded));
@@ -248,7 +219,8 @@ void EncodeImageAndInvokeDataCallback(
};
ConvertImageToRaster(std::move(image), encode_task, raster_task_runner,
io_task_runner, resource_context, snapshot_delegate);
io_task_runner, resource_context, snapshot_delegate,
is_gpu_disabled_sync_switch);
}
} // namespace
@@ -282,8 +254,9 @@ Dart_Handle EncodeImage(CanvasImage* canvas_image,
EncodeImageAndInvokeDataCallback(
std::move(image), std::move(callback), image_format,
std::move(ui_task_runner), std::move(raster_task_runner),
std::move(io_task_runner), io_manager->GetResourceContext().get(),
std::move(snapshot_delegate));
std::move(io_task_runner), io_manager->GetResourceContext(),
std::move(snapshot_delegate),
io_manager->GetIsGpuDisabledSyncSwitch());
}));
return Dart_Null();

View File

@@ -0,0 +1,57 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FLUTTER_LIB_UI_PAINTING_IMAGE_ENCODING_IMPL_H_
#define FLUTTER_LIB_UI_PAINTING_IMAGE_ENCODING_IMPL_H_
#include "flutter/lib/ui/ui_dart_state.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/core/SkSurface.h"
namespace flutter {
template <typename SyncSwitch>
sk_sp<SkImage> ConvertToRasterUsingResourceContext(
sk_sp<SkImage> image,
fml::WeakPtr<GrDirectContext> resource_context,
const std::shared_ptr<const SyncSwitch>& is_gpu_disabled_sync_switch) {
sk_sp<SkSurface> surface;
SkImageInfo surface_info = SkImageInfo::MakeN32Premul(image->dimensions());
is_gpu_disabled_sync_switch->Execute(
typename SyncSwitch::Handlers()
.SetIfTrue([&surface, &surface_info] {
surface = SkSurface::MakeRaster(surface_info);
})
.SetIfFalse([&surface, &surface_info, resource_context] {
if (resource_context) {
surface = SkSurface::MakeRenderTarget(
resource_context.get(), SkBudgeted::kNo, surface_info);
} else {
surface = SkSurface::MakeRaster(surface_info);
}
}));
if (surface == nullptr || surface->getCanvas() == nullptr) {
FML_LOG(ERROR) << "Could not create a surface to copy the texture into.";
return nullptr;
}
surface->getCanvas()->drawImage(image, 0, 0);
surface->getCanvas()->flush();
auto snapshot = surface->makeImageSnapshot();
if (snapshot == nullptr) {
FML_LOG(ERROR) << "Could not snapshot image to encode.";
return nullptr;
}
return snapshot->makeRasterImage();
}
} // namespace flutter
#endif // FLUTTER_LIB_UI_PAINTING_IMAGE_ENCODING_IMPL_H_

View File

@@ -3,13 +3,16 @@
// found in the LICENSE file.
#include "flutter/lib/ui/painting/image_encoding.h"
#include "flutter/lib/ui/painting/image_encoding_impl.h"
#include "flutter/common/task_runners.h"
#include "flutter/fml/synchronization/waitable_event.h"
#include "flutter/lib/ui/painting/image.h"
#include "flutter/runtime/dart_vm.h"
#include "flutter/shell/common/shell_test.h"
#include "flutter/shell/common/thread_host.h"
#include "flutter/testing/testing.h"
#include "gmock/gmock.h"
namespace flutter {
namespace testing {
@@ -18,8 +21,27 @@ namespace {
fml::AutoResetWaitableEvent message_latch;
};
class MockSyncSwitch {
public:
struct Handlers {
Handlers& SetIfTrue(const std::function<void()>& handler) {
true_handler = std::move(handler);
return *this;
}
Handlers& SetIfFalse(const std::function<void()>& handler) {
false_handler = std::move(handler);
return *this;
}
std::function<void()> true_handler = [] {};
std::function<void()> false_handler = [] {};
};
MOCK_CONST_METHOD1(Execute, void(const Handlers& handlers));
MOCK_METHOD1(SetSwitch, void(bool value));
};
TEST_F(ShellTest, EncodeImageGivesExternalTypedData) {
auto nativeEncodeImage = [&](Dart_NativeArguments args) {
auto native_encode_image = [&](Dart_NativeArguments args) {
auto image_handle = Dart_GetNativeArgument(args, 0);
image_handle =
Dart_GetField(image_handle, Dart_NewStringFromCString("_image"));
@@ -59,7 +81,7 @@ TEST_F(ShellTest, EncodeImageGivesExternalTypedData) {
CreateNewThread() // io
);
AddNativeCallback("EncodeImage", CREATE_NATIVE_ENTRY(nativeEncodeImage));
AddNativeCallback("EncodeImage", CREATE_NATIVE_ENTRY(native_encode_image));
AddNativeCallback("ValidateExternal",
CREATE_NATIVE_ENTRY(nativeValidateExternal));
@@ -78,5 +100,70 @@ TEST_F(ShellTest, EncodeImageGivesExternalTypedData) {
DestroyShell(std::move(shell), std::move(task_runners));
}
TEST_F(ShellTest, EncodeImageAccessesSyncSwitch) {
Settings settings = CreateSettingsForFixture();
TaskRunners task_runners("test", // label
GetCurrentTaskRunner(), // platform
CreateNewThread(), // raster
CreateNewThread(), // ui
CreateNewThread() // io
);
auto native_encode_image = [&](Dart_NativeArguments args) {
auto image_handle = Dart_GetNativeArgument(args, 0);
image_handle =
Dart_GetField(image_handle, Dart_NewStringFromCString("_image"));
ASSERT_FALSE(Dart_IsError(image_handle)) << Dart_GetError(image_handle);
ASSERT_FALSE(Dart_IsNull(image_handle));
auto format_handle = Dart_GetNativeArgument(args, 1);
intptr_t peer = 0;
Dart_Handle result = Dart_GetNativeInstanceField(
image_handle, tonic::DartWrappable::kPeerIndex, &peer);
ASSERT_FALSE(Dart_IsError(result));
CanvasImage* canvas_image = reinterpret_cast<CanvasImage*>(peer);
int64_t format = -1;
result = Dart_IntegerToInt64(format_handle, &format);
ASSERT_FALSE(Dart_IsError(result));
auto io_manager = UIDartState::Current()->GetIOManager();
fml::AutoResetWaitableEvent latch;
task_runners.GetIOTaskRunner()->PostTask([&]() {
auto is_gpu_disabled_sync_switch =
std::make_shared<const MockSyncSwitch>();
EXPECT_CALL(*is_gpu_disabled_sync_switch, Execute)
.WillOnce([](const MockSyncSwitch::Handlers& handlers) {
handlers.true_handler();
});
ConvertToRasterUsingResourceContext(canvas_image->image(),
io_manager->GetResourceContext(),
is_gpu_disabled_sync_switch);
latch.Signal();
});
latch.Wait();
message_latch.Signal();
};
AddNativeCallback("EncodeImage", CREATE_NATIVE_ENTRY(native_encode_image));
std::unique_ptr<Shell> shell =
CreateShell(std::move(settings), std::move(task_runners));
ASSERT_TRUE(shell->IsSetup());
auto configuration = RunConfiguration::InferFromSettings(settings);
configuration.SetEntrypoint("encodeImageProducesExternalUint8List");
shell->RunEngine(std::move(configuration), [&](auto result) {
ASSERT_EQ(result, Engine::RunStatus::Success);
});
message_latch.Wait();
DestroyShell(std::move(shell), std::move(task_runners));
}
} // namespace testing
} // namespace flutter