[Impeller] Implement ui.Picture.toImage() (flutter/engine#35633)
This commit is contained in:
@@ -1422,6 +1422,12 @@ FILE: ../../../flutter/shell/common/shell_test_platform_view_vulkan.h
|
||||
FILE: ../../../flutter/shell/common/shell_unittests.cc
|
||||
FILE: ../../../flutter/shell/common/skia_event_tracer_impl.cc
|
||||
FILE: ../../../flutter/shell/common/skia_event_tracer_impl.h
|
||||
FILE: ../../../flutter/shell/common/snapshot_controller.cc
|
||||
FILE: ../../../flutter/shell/common/snapshot_controller.h
|
||||
FILE: ../../../flutter/shell/common/snapshot_controller_impeller.cc
|
||||
FILE: ../../../flutter/shell/common/snapshot_controller_impeller.h
|
||||
FILE: ../../../flutter/shell/common/snapshot_controller_skia.cc
|
||||
FILE: ../../../flutter/shell/common/snapshot_controller_skia.h
|
||||
FILE: ../../../flutter/shell/common/snapshot_surface_producer.h
|
||||
FILE: ../../../flutter/shell/common/switches.cc
|
||||
FILE: ../../../flutter/shell/common/switches.h
|
||||
|
||||
@@ -26,4 +26,8 @@ bool Surface::EnableRasterCache() const {
|
||||
return true;
|
||||
}
|
||||
|
||||
impeller::AiksContext* Surface::GetAiksContext() const {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace flutter
|
||||
|
||||
@@ -12,6 +12,10 @@
|
||||
#include "flutter/flow/surface_frame.h"
|
||||
#include "flutter/fml/macros.h"
|
||||
|
||||
namespace impeller {
|
||||
class AiksContext;
|
||||
} // namespace impeller
|
||||
|
||||
namespace flutter {
|
||||
|
||||
/// Abstract Base Class that represents where we will be rendering content.
|
||||
@@ -37,6 +41,8 @@ class Surface {
|
||||
|
||||
virtual bool EnableRasterCache() const;
|
||||
|
||||
virtual impeller::AiksContext* GetAiksContext() const;
|
||||
|
||||
private:
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(Surface);
|
||||
};
|
||||
|
||||
@@ -93,9 +93,8 @@ Dart_Handle Picture::RasterizeToImage(sk_sp<DisplayList> display_list,
|
||||
uint32_t width,
|
||||
uint32_t height,
|
||||
Dart_Handle raw_image_callback) {
|
||||
return RasterizeToImage(
|
||||
[display_list](SkCanvas* canvas) { display_list->RenderTo(canvas); },
|
||||
nullptr, width, height, raw_image_callback);
|
||||
return RasterizeToImage(display_list, nullptr, width, height,
|
||||
raw_image_callback);
|
||||
}
|
||||
|
||||
Dart_Handle Picture::RasterizeLayerTreeToImage(
|
||||
@@ -107,12 +106,11 @@ Dart_Handle Picture::RasterizeLayerTreeToImage(
|
||||
raw_image_callback);
|
||||
}
|
||||
|
||||
Dart_Handle Picture::RasterizeToImage(
|
||||
std::function<void(SkCanvas*)> draw_callback,
|
||||
std::shared_ptr<LayerTree> layer_tree,
|
||||
uint32_t width,
|
||||
uint32_t height,
|
||||
Dart_Handle raw_image_callback) {
|
||||
Dart_Handle Picture::RasterizeToImage(sk_sp<DisplayList> display_list,
|
||||
std::shared_ptr<LayerTree> layer_tree,
|
||||
uint32_t width,
|
||||
uint32_t height,
|
||||
Dart_Handle raw_image_callback) {
|
||||
if (Dart_IsNull(raw_image_callback) || !Dart_IsClosure(raw_image_callback)) {
|
||||
return tonic::ToDart("Image callback was invalid");
|
||||
}
|
||||
@@ -140,7 +138,7 @@ Dart_Handle Picture::RasterizeToImage(
|
||||
// The static leak checker gets confused by the use of fml::MakeCopyable.
|
||||
// NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
|
||||
fml::MakeCopyable([image_callback = std::move(image_callback),
|
||||
unref_queue](sk_sp<SkImage> raster_image) mutable {
|
||||
unref_queue](sk_sp<DlImage> image) mutable {
|
||||
auto dart_state = image_callback->dart_state().lock();
|
||||
if (!dart_state) {
|
||||
// The root isolate could have died in the meantime.
|
||||
@@ -148,14 +146,18 @@ Dart_Handle Picture::RasterizeToImage(
|
||||
}
|
||||
tonic::DartState::Scope scope(dart_state);
|
||||
|
||||
if (!raster_image) {
|
||||
if (!image) {
|
||||
tonic::DartInvoke(image_callback->Get(), {Dart_Null()});
|
||||
return;
|
||||
}
|
||||
|
||||
if (image->skia_image()) {
|
||||
image =
|
||||
DlImageGPU::Make({image->skia_image(), std::move(unref_queue)});
|
||||
}
|
||||
|
||||
auto dart_image = CanvasImage::Create();
|
||||
dart_image->set_image(DlImageGPU::Make(
|
||||
{std::move(raster_image), std::move(unref_queue)}));
|
||||
dart_image->set_image(image);
|
||||
auto* raw_dart_image = tonic::ToDart(std::move(dart_image));
|
||||
|
||||
// All done!
|
||||
@@ -169,28 +171,24 @@ Dart_Handle Picture::RasterizeToImage(
|
||||
// Kick things off on the raster rask runner.
|
||||
fml::TaskRunner::RunNowOrPostTask(
|
||||
raster_task_runner,
|
||||
[ui_task_runner, snapshot_delegate, draw_callback, picture_bounds,
|
||||
ui_task, layer_tree = std::move(layer_tree)] {
|
||||
sk_sp<SkImage> raster_image;
|
||||
[ui_task_runner, snapshot_delegate, display_list, picture_bounds, ui_task,
|
||||
layer_tree = std::move(layer_tree)] {
|
||||
sk_sp<DlImage> image;
|
||||
if (layer_tree) {
|
||||
auto display_list = layer_tree->Flatten(
|
||||
SkRect::MakeWH(picture_bounds.width(), picture_bounds.height()),
|
||||
snapshot_delegate->GetTextureRegistry(),
|
||||
snapshot_delegate->GetGrContext());
|
||||
|
||||
raster_image = snapshot_delegate->MakeRasterSnapshot(
|
||||
[display_list](SkCanvas* canvas) {
|
||||
display_list->RenderTo(canvas);
|
||||
},
|
||||
picture_bounds);
|
||||
image = snapshot_delegate->MakeRasterSnapshot(display_list,
|
||||
picture_bounds);
|
||||
} else {
|
||||
raster_image = snapshot_delegate->MakeRasterSnapshot(draw_callback,
|
||||
picture_bounds);
|
||||
image = snapshot_delegate->MakeRasterSnapshot(display_list,
|
||||
picture_bounds);
|
||||
}
|
||||
|
||||
fml::TaskRunner::RunNowOrPostTask(
|
||||
ui_task_runner,
|
||||
[ui_task, raster_image]() { ui_task(raster_image); });
|
||||
ui_task_runner, [ui_task, image]() { ui_task(image); });
|
||||
});
|
||||
|
||||
return Dart_Null();
|
||||
|
||||
@@ -58,16 +58,14 @@ class Picture : public RefCountedDartWrappable<Picture> {
|
||||
uint32_t height,
|
||||
Dart_Handle raw_image_callback);
|
||||
|
||||
// Callers may provide either a draw_callback (which should reference a
|
||||
// display list) or a layer tree. If a layer tree is provided, it will be
|
||||
// flattened on the raster thread. In this case the draw callback will be
|
||||
// ignored.
|
||||
static Dart_Handle RasterizeToImage(
|
||||
std::function<void(SkCanvas*)> draw_callback,
|
||||
std::shared_ptr<LayerTree> layer_tree,
|
||||
uint32_t width,
|
||||
uint32_t height,
|
||||
Dart_Handle raw_image_callback);
|
||||
// Callers may provide either a display list or a layer tree. If a layer tree
|
||||
// is provided, it will be flattened on the raster thread. In this case the
|
||||
// display list will be ignored.
|
||||
static Dart_Handle RasterizeToImage(sk_sp<DisplayList> display_list,
|
||||
std::shared_ptr<LayerTree> layer_tree,
|
||||
uint32_t width,
|
||||
uint32_t height,
|
||||
Dart_Handle raw_image_callback);
|
||||
|
||||
private:
|
||||
explicit Picture(flutter::SkiaGPUObject<DisplayList> display_list);
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
|
||||
namespace flutter {
|
||||
|
||||
class DlImage;
|
||||
|
||||
class SnapshotDelegate {
|
||||
public:
|
||||
struct GpuImageResult {
|
||||
@@ -59,11 +61,7 @@ class SnapshotDelegate {
|
||||
sk_sp<DisplayList> display_list,
|
||||
const SkImageInfo& image_info) = 0;
|
||||
|
||||
virtual sk_sp<SkImage> MakeRasterSnapshot(
|
||||
std::function<void(SkCanvas*)> draw_callback,
|
||||
SkISize picture_size) = 0;
|
||||
|
||||
virtual sk_sp<SkImage> MakeRasterSnapshot(sk_sp<SkPicture> picture,
|
||||
virtual sk_sp<DlImage> MakeRasterSnapshot(sk_sp<DisplayList> display_list,
|
||||
SkISize picture_size) = 0;
|
||||
|
||||
virtual sk_sp<SkImage> ConvertToRasterImage(sk_sp<SkImage> image) = 0;
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
import("//build/fuchsia/sdk.gni")
|
||||
import("//flutter/common/config.gni")
|
||||
import("//flutter/impeller/tools/impeller.gni")
|
||||
import("//flutter/shell/gpu/gpu.gni")
|
||||
import("//flutter/testing/testing.gni")
|
||||
|
||||
@@ -93,6 +94,10 @@ source_set("common") {
|
||||
"shell_io_manager.h",
|
||||
"skia_event_tracer_impl.cc",
|
||||
"skia_event_tracer_impl.h",
|
||||
"snapshot_controller.cc",
|
||||
"snapshot_controller.h",
|
||||
"snapshot_controller_skia.cc",
|
||||
"snapshot_controller_skia.h",
|
||||
"snapshot_surface_producer.h",
|
||||
"switches.cc",
|
||||
"switches.h",
|
||||
@@ -128,6 +133,15 @@ source_set("common") {
|
||||
"//third_party/dart/runtime:dart_api",
|
||||
"//third_party/skia",
|
||||
]
|
||||
|
||||
if (impeller_supports_rendering) {
|
||||
sources += [
|
||||
"snapshot_controller_impeller.cc",
|
||||
"snapshot_controller_impeller.h",
|
||||
]
|
||||
|
||||
deps += [ "//flutter/impeller" ]
|
||||
}
|
||||
}
|
||||
|
||||
template("shell_host_executable") {
|
||||
|
||||
@@ -34,6 +34,8 @@ Rasterizer::Rasterizer(Delegate& delegate,
|
||||
gpu_image_behavior_(gpu_image_behavior),
|
||||
compositor_context_(std::make_unique<flutter::CompositorContext>(*this)),
|
||||
user_override_resource_cache_bytes_(false),
|
||||
snapshot_controller_(
|
||||
SnapshotController::Make(*this, delegate.GetSettings())),
|
||||
weak_factory_(this) {
|
||||
FML_DCHECK(compositor_context_);
|
||||
}
|
||||
@@ -343,147 +345,14 @@ std::unique_ptr<SnapshotDelegate::GpuImageResult> Rasterizer::MakeGpuImage(
|
||||
return result;
|
||||
}
|
||||
|
||||
namespace {
|
||||
sk_sp<SkImage> DrawSnapshot(
|
||||
sk_sp<SkSurface> surface,
|
||||
const std::function<void(SkCanvas*)>& draw_callback) {
|
||||
if (surface == nullptr || surface->getCanvas() == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
draw_callback(surface->getCanvas());
|
||||
surface->getCanvas()->flush();
|
||||
|
||||
sk_sp<SkImage> device_snapshot;
|
||||
{
|
||||
TRACE_EVENT0("flutter", "MakeDeviceSnpashot");
|
||||
device_snapshot = surface->makeImageSnapshot();
|
||||
}
|
||||
|
||||
if (device_snapshot == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
{
|
||||
TRACE_EVENT0("flutter", "DeviceHostTransfer");
|
||||
if (auto raster_image = device_snapshot->makeRasterImage()) {
|
||||
return raster_image;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
sk_sp<SkImage> Rasterizer::DoMakeRasterSnapshot(
|
||||
SkISize size,
|
||||
std::function<void(SkCanvas*)> draw_callback) {
|
||||
TRACE_EVENT0("flutter", __FUNCTION__);
|
||||
sk_sp<SkImage> result;
|
||||
SkImageInfo image_info = SkImageInfo::MakeN32Premul(
|
||||
size.width(), size.height(), SkColorSpace::MakeSRGB());
|
||||
|
||||
std::unique_ptr<Surface> pbuffer_surface;
|
||||
Surface* snapshot_surface = nullptr;
|
||||
if (surface_ && surface_->GetContext()) {
|
||||
snapshot_surface = surface_.get();
|
||||
} else if (snapshot_surface_producer_) {
|
||||
pbuffer_surface = snapshot_surface_producer_->CreateSnapshotSurface();
|
||||
if (pbuffer_surface && pbuffer_surface->GetContext()) {
|
||||
snapshot_surface = pbuffer_surface.get();
|
||||
}
|
||||
}
|
||||
|
||||
if (!snapshot_surface) {
|
||||
// Raster surface is fine if there is no on screen surface. This might
|
||||
// happen in case of software rendering.
|
||||
sk_sp<SkSurface> sk_surface = SkSurface::MakeRaster(image_info);
|
||||
result = DrawSnapshot(sk_surface, draw_callback);
|
||||
} else {
|
||||
delegate_.GetIsGpuDisabledSyncSwitch()->Execute(
|
||||
fml::SyncSwitch::Handlers()
|
||||
.SetIfTrue([&] {
|
||||
sk_sp<SkSurface> surface = SkSurface::MakeRaster(image_info);
|
||||
result = DrawSnapshot(surface, draw_callback);
|
||||
})
|
||||
.SetIfFalse([&] {
|
||||
FML_DCHECK(snapshot_surface);
|
||||
auto context_switch =
|
||||
snapshot_surface->MakeRenderContextCurrent();
|
||||
if (!context_switch->GetResult()) {
|
||||
return;
|
||||
}
|
||||
|
||||
GrRecordingContext* context = snapshot_surface->GetContext();
|
||||
auto max_size = context->maxRenderTargetSize();
|
||||
double scale_factor = std::min(
|
||||
1.0, static_cast<double>(max_size) /
|
||||
static_cast<double>(std::max(image_info.width(),
|
||||
image_info.height())));
|
||||
|
||||
// Scale down the render target size to the max supported by the
|
||||
// GPU if necessary. Exceeding the max would otherwise cause a
|
||||
// null result.
|
||||
if (scale_factor < 1.0) {
|
||||
image_info = image_info.makeWH(
|
||||
static_cast<double>(image_info.width()) * scale_factor,
|
||||
static_cast<double>(image_info.height()) * scale_factor);
|
||||
}
|
||||
|
||||
// When there is an on screen surface, we need a render target
|
||||
// SkSurface because we want to access texture backed images.
|
||||
sk_sp<SkSurface> sk_surface =
|
||||
SkSurface::MakeRenderTarget(context, // context
|
||||
SkBudgeted::kNo, // budgeted
|
||||
image_info // image info
|
||||
);
|
||||
if (!sk_surface) {
|
||||
FML_LOG(ERROR)
|
||||
<< "DoMakeRasterSnapshot can not create GPU render target";
|
||||
return;
|
||||
}
|
||||
|
||||
sk_surface->getCanvas()->scale(scale_factor, scale_factor);
|
||||
result = DrawSnapshot(sk_surface, draw_callback);
|
||||
}));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
sk_sp<SkImage> Rasterizer::MakeRasterSnapshot(
|
||||
std::function<void(SkCanvas*)> draw_callback,
|
||||
SkISize picture_size) {
|
||||
return DoMakeRasterSnapshot(picture_size, draw_callback);
|
||||
}
|
||||
|
||||
sk_sp<SkImage> Rasterizer::MakeRasterSnapshot(sk_sp<SkPicture> picture,
|
||||
sk_sp<DlImage> Rasterizer::MakeRasterSnapshot(sk_sp<DisplayList> display_list,
|
||||
SkISize picture_size) {
|
||||
return DoMakeRasterSnapshot(picture_size,
|
||||
[picture = std::move(picture)](SkCanvas* canvas) {
|
||||
canvas->drawPicture(picture);
|
||||
});
|
||||
return snapshot_controller_->MakeRasterSnapshot(display_list, picture_size);
|
||||
}
|
||||
|
||||
sk_sp<SkImage> Rasterizer::ConvertToRasterImage(sk_sp<SkImage> image) {
|
||||
TRACE_EVENT0("flutter", __FUNCTION__);
|
||||
|
||||
// If the rasterizer does not have a surface with a GrContext, then it will
|
||||
// be unable to render a cross-context SkImage. The caller will need to
|
||||
// create the raster image on the IO thread.
|
||||
if (surface_ == nullptr || surface_->GetContext() == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (image == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
SkISize image_size = image->dimensions();
|
||||
return DoMakeRasterSnapshot(image_size,
|
||||
[image = std::move(image)](SkCanvas* canvas) {
|
||||
canvas->drawImage(image, 0, 0);
|
||||
});
|
||||
return snapshot_controller_->ConvertToRasterImage(image);
|
||||
}
|
||||
|
||||
fml::Milliseconds Rasterizer::GetFrameBudget() const {
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
#include "flutter/common/settings.h"
|
||||
#include "flutter/common/task_runners.h"
|
||||
#include "flutter/display_list/display_list_image.h"
|
||||
#include "flutter/flow/compositor_context.h"
|
||||
#include "flutter/flow/embedded_views.h"
|
||||
#include "flutter/flow/frame_timings.h"
|
||||
@@ -24,6 +25,7 @@
|
||||
#include "flutter/fml/time/time_point.h"
|
||||
#include "flutter/lib/ui/snapshot_delegate.h"
|
||||
#include "flutter/shell/common/pipeline.h"
|
||||
#include "flutter/shell/common/snapshot_controller.h"
|
||||
#include "flutter/shell/common/snapshot_surface_producer.h"
|
||||
#include "third_party/skia/include/core/SkImage.h"
|
||||
#include "third_party/skia/include/gpu/GrDirectContext.h"
|
||||
@@ -45,7 +47,8 @@ namespace flutter {
|
||||
/// state necessary to render frames to the render surface.
|
||||
///
|
||||
class Rasterizer final : public SnapshotDelegate,
|
||||
public Stopwatch::RefreshRateUpdater {
|
||||
public Stopwatch::RefreshRateUpdater,
|
||||
public SnapshotController::Delegate {
|
||||
public:
|
||||
//----------------------------------------------------------------------------
|
||||
/// @brief Used to forward events from the rasterizer to interested
|
||||
@@ -98,6 +101,8 @@ class Rasterizer final : public SnapshotDelegate,
|
||||
/// is critical that GPU operations are not processed.
|
||||
virtual std::shared_ptr<const fml::SyncSwitch> GetIsGpuDisabledSyncSwitch()
|
||||
const = 0;
|
||||
|
||||
virtual const Settings& GetSettings() const = 0;
|
||||
};
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
@@ -470,12 +475,7 @@ class Rasterizer final : public SnapshotDelegate,
|
||||
const SkImageInfo& image_info) override;
|
||||
|
||||
// |SnapshotDelegate|
|
||||
sk_sp<SkImage> MakeRasterSnapshot(
|
||||
std::function<void(SkCanvas*)> draw_callback,
|
||||
SkISize picture_size) override;
|
||||
|
||||
// |SnapshotDelegate|
|
||||
sk_sp<SkImage> MakeRasterSnapshot(sk_sp<SkPicture> picture,
|
||||
sk_sp<DlImage> MakeRasterSnapshot(sk_sp<DisplayList> display_list,
|
||||
SkISize picture_size) override;
|
||||
|
||||
// |SnapshotDelegate|
|
||||
@@ -487,16 +487,29 @@ class Rasterizer final : public SnapshotDelegate,
|
||||
/// See: `DisplayManager::GetMainDisplayRefreshRate`.
|
||||
fml::Milliseconds GetFrameBudget() const override;
|
||||
|
||||
// |SnapshotController::Delegate|
|
||||
const std::unique_ptr<Surface>& GetSurface() const override {
|
||||
return surface_;
|
||||
}
|
||||
|
||||
// |SnapshotController::Delegate|
|
||||
const std::unique_ptr<SnapshotSurfaceProducer>& GetSnapshotSurfaceProducer()
|
||||
const override {
|
||||
return snapshot_surface_producer_;
|
||||
}
|
||||
|
||||
// |SnapshotController::Delegate|
|
||||
std::shared_ptr<const fml::SyncSwitch> GetIsGpuDisabledSyncSwitch()
|
||||
const override {
|
||||
return delegate_.GetIsGpuDisabledSyncSwitch();
|
||||
}
|
||||
|
||||
sk_sp<SkData> ScreenshotLayerTreeAsImage(
|
||||
flutter::LayerTree* tree,
|
||||
flutter::CompositorContext& compositor_context,
|
||||
GrDirectContext* surface_context,
|
||||
bool compressed);
|
||||
|
||||
sk_sp<SkImage> DoMakeRasterSnapshot(
|
||||
SkISize size,
|
||||
std::function<void(SkCanvas*)> draw_callback);
|
||||
|
||||
RasterStatus DoDraw(
|
||||
std::unique_ptr<FrameTimingsRecorder> frame_timings_recorder,
|
||||
std::shared_ptr<flutter::LayerTree> layer_tree);
|
||||
@@ -529,6 +542,7 @@ class Rasterizer final : public SnapshotDelegate,
|
||||
std::optional<size_t> max_cache_bytes_;
|
||||
fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger_;
|
||||
std::shared_ptr<ExternalViewEmbedder> external_view_embedder_;
|
||||
std::unique_ptr<SnapshotController> snapshot_controller_;
|
||||
|
||||
// WeakPtrFactory must be the last member.
|
||||
fml::TaskRunnerAffineWeakPtrFactory<Rasterizer> weak_factory_;
|
||||
|
||||
@@ -36,6 +36,7 @@ class MockDelegate : public Rasterizer::Delegate {
|
||||
MOCK_CONST_METHOD0(GetIsGpuDisabledSyncSwitch,
|
||||
std::shared_ptr<const fml::SyncSwitch>());
|
||||
MOCK_METHOD0(CreateSnapshotSurface, std::unique_ptr<Surface>());
|
||||
MOCK_CONST_METHOD0(GetSettings, const Settings&());
|
||||
};
|
||||
|
||||
class MockSurface : public Surface {
|
||||
@@ -80,6 +81,8 @@ class MockExternalViewEmbedder : public ExternalViewEmbedder {
|
||||
|
||||
TEST(RasterizerTest, create) {
|
||||
NiceMock<MockDelegate> delegate;
|
||||
Settings settings;
|
||||
ON_CALL(delegate, GetSettings()).WillByDefault(ReturnRef(settings));
|
||||
auto rasterizer = std::make_unique<Rasterizer>(delegate);
|
||||
EXPECT_TRUE(rasterizer != nullptr);
|
||||
}
|
||||
@@ -109,6 +112,8 @@ TEST(RasterizerTest, drawEmptyPipeline) {
|
||||
thread_host.ui_thread->GetTaskRunner(),
|
||||
thread_host.io_thread->GetTaskRunner());
|
||||
NiceMock<MockDelegate> delegate;
|
||||
Settings settings;
|
||||
ON_CALL(delegate, GetSettings()).WillByDefault(ReturnRef(settings));
|
||||
ON_CALL(delegate, GetTaskRunners()).WillByDefault(ReturnRef(task_runners));
|
||||
auto rasterizer = std::make_unique<Rasterizer>(delegate);
|
||||
auto surface = std::make_unique<NiceMock<MockSurface>>();
|
||||
@@ -136,6 +141,8 @@ TEST(RasterizerTest,
|
||||
thread_host.ui_thread->GetTaskRunner(),
|
||||
thread_host.io_thread->GetTaskRunner());
|
||||
NiceMock<MockDelegate> delegate;
|
||||
Settings settings;
|
||||
ON_CALL(delegate, GetSettings()).WillByDefault(ReturnRef(settings));
|
||||
EXPECT_CALL(delegate, GetTaskRunners())
|
||||
.WillRepeatedly(ReturnRef(task_runners));
|
||||
EXPECT_CALL(delegate, OnFrameRasterized(_));
|
||||
@@ -203,6 +210,8 @@ TEST(
|
||||
thread_host.ui_thread->GetTaskRunner(),
|
||||
thread_host.io_thread->GetTaskRunner());
|
||||
NiceMock<MockDelegate> delegate;
|
||||
Settings settings;
|
||||
ON_CALL(delegate, GetSettings()).WillByDefault(ReturnRef(settings));
|
||||
EXPECT_CALL(delegate, GetTaskRunners())
|
||||
.WillRepeatedly(ReturnRef(task_runners));
|
||||
EXPECT_CALL(delegate, OnFrameRasterized(_));
|
||||
@@ -268,6 +277,8 @@ TEST(
|
||||
thread_host.io_thread->GetTaskRunner());
|
||||
|
||||
NiceMock<MockDelegate> delegate;
|
||||
Settings settings;
|
||||
ON_CALL(delegate, GetSettings()).WillByDefault(ReturnRef(settings));
|
||||
EXPECT_CALL(delegate, GetTaskRunners())
|
||||
.WillRepeatedly(ReturnRef(task_runners));
|
||||
EXPECT_CALL(delegate, OnFrameRasterized(_));
|
||||
@@ -332,6 +343,8 @@ TEST(RasterizerTest,
|
||||
thread_host.io_thread->GetTaskRunner());
|
||||
|
||||
NiceMock<MockDelegate> delegate;
|
||||
Settings settings;
|
||||
ON_CALL(delegate, GetSettings()).WillByDefault(ReturnRef(settings));
|
||||
EXPECT_CALL(delegate, GetTaskRunners())
|
||||
.WillRepeatedly(ReturnRef(task_runners));
|
||||
EXPECT_CALL(delegate, OnFrameRasterized(_));
|
||||
@@ -405,6 +418,8 @@ TEST(RasterizerTest, externalViewEmbedderDoesntEndFrameWhenNoSurfaceIsSet) {
|
||||
thread_host.ui_thread->GetTaskRunner(),
|
||||
thread_host.io_thread->GetTaskRunner());
|
||||
NiceMock<MockDelegate> delegate;
|
||||
Settings settings;
|
||||
ON_CALL(delegate, GetSettings()).WillByDefault(ReturnRef(settings));
|
||||
EXPECT_CALL(delegate, GetTaskRunners())
|
||||
.WillRepeatedly(ReturnRef(task_runners));
|
||||
auto rasterizer = std::make_unique<Rasterizer>(delegate);
|
||||
@@ -448,6 +463,8 @@ TEST(RasterizerTest, externalViewEmbedderDoesntEndFrameWhenNotUsedThisFrame) {
|
||||
thread_host.ui_thread->GetTaskRunner(),
|
||||
thread_host.io_thread->GetTaskRunner());
|
||||
NiceMock<MockDelegate> delegate;
|
||||
Settings settings;
|
||||
ON_CALL(delegate, GetSettings()).WillByDefault(ReturnRef(settings));
|
||||
EXPECT_CALL(delegate, GetTaskRunners())
|
||||
.WillRepeatedly(ReturnRef(task_runners));
|
||||
|
||||
@@ -503,6 +520,8 @@ TEST(RasterizerTest, externalViewEmbedderDoesntEndFrameWhenPipelineIsEmpty) {
|
||||
thread_host.ui_thread->GetTaskRunner(),
|
||||
thread_host.io_thread->GetTaskRunner());
|
||||
NiceMock<MockDelegate> delegate;
|
||||
Settings settings;
|
||||
ON_CALL(delegate, GetSettings()).WillByDefault(ReturnRef(settings));
|
||||
EXPECT_CALL(delegate, GetTaskRunners())
|
||||
.WillRepeatedly(ReturnRef(task_runners));
|
||||
|
||||
@@ -546,6 +565,8 @@ TEST(RasterizerTest,
|
||||
thread_host.ui_thread->GetTaskRunner(),
|
||||
thread_host.io_thread->GetTaskRunner());
|
||||
NiceMock<MockDelegate> delegate;
|
||||
Settings settings;
|
||||
ON_CALL(delegate, GetSettings()).WillByDefault(ReturnRef(settings));
|
||||
EXPECT_CALL(delegate, GetTaskRunners())
|
||||
.WillRepeatedly(ReturnRef(task_runners));
|
||||
EXPECT_CALL(delegate, OnFrameRasterized(_));
|
||||
@@ -600,6 +621,8 @@ TEST(
|
||||
thread_host.ui_thread->GetTaskRunner(),
|
||||
thread_host.io_thread->GetTaskRunner());
|
||||
NiceMock<MockDelegate> delegate;
|
||||
Settings settings;
|
||||
ON_CALL(delegate, GetSettings()).WillByDefault(ReturnRef(settings));
|
||||
EXPECT_CALL(delegate, GetTaskRunners())
|
||||
.WillRepeatedly(ReturnRef(task_runners));
|
||||
EXPECT_CALL(delegate, OnFrameRasterized(_));
|
||||
@@ -655,6 +678,8 @@ TEST(
|
||||
thread_host.ui_thread->GetTaskRunner(),
|
||||
thread_host.io_thread->GetTaskRunner());
|
||||
NiceMock<MockDelegate> delegate;
|
||||
Settings settings;
|
||||
ON_CALL(delegate, GetSettings()).WillByDefault(ReturnRef(settings));
|
||||
EXPECT_CALL(delegate, GetTaskRunners())
|
||||
.WillRepeatedly(ReturnRef(task_runners));
|
||||
EXPECT_CALL(delegate, OnFrameRasterized(_));
|
||||
@@ -709,6 +734,8 @@ TEST(
|
||||
thread_host.ui_thread->GetTaskRunner(),
|
||||
thread_host.io_thread->GetTaskRunner());
|
||||
NiceMock<MockDelegate> delegate;
|
||||
Settings settings;
|
||||
ON_CALL(delegate, GetSettings()).WillByDefault(ReturnRef(settings));
|
||||
EXPECT_CALL(delegate, GetTaskRunners())
|
||||
.WillRepeatedly(ReturnRef(task_runners));
|
||||
EXPECT_CALL(delegate, OnFrameRasterized(_)).Times(0);
|
||||
@@ -761,6 +788,8 @@ TEST(RasterizerTest,
|
||||
thread_host.ui_thread->GetTaskRunner(),
|
||||
thread_host.io_thread->GetTaskRunner());
|
||||
NiceMock<MockDelegate> delegate;
|
||||
Settings settings;
|
||||
ON_CALL(delegate, GetSettings()).WillByDefault(ReturnRef(settings));
|
||||
ON_CALL(delegate, GetTaskRunners()).WillByDefault(ReturnRef(task_runners));
|
||||
|
||||
fml::AutoResetWaitableEvent latch;
|
||||
@@ -845,6 +874,8 @@ TEST(RasterizerTest, TeardownFreesResourceCache) {
|
||||
thread_host.io_thread->GetTaskRunner());
|
||||
|
||||
NiceMock<MockDelegate> delegate;
|
||||
Settings settings;
|
||||
ON_CALL(delegate, GetSettings()).WillByDefault(ReturnRef(settings));
|
||||
EXPECT_CALL(delegate, GetTaskRunners())
|
||||
.WillRepeatedly(ReturnRef(task_runners));
|
||||
|
||||
@@ -906,6 +937,8 @@ TEST(RasterizerTest, TeardownNoSurface) {
|
||||
thread_host.io_thread->GetTaskRunner());
|
||||
|
||||
NiceMock<MockDelegate> delegate;
|
||||
Settings settings;
|
||||
ON_CALL(delegate, GetSettings()).WillByDefault(ReturnRef(settings));
|
||||
EXPECT_CALL(delegate, GetTaskRunners())
|
||||
.WillRepeatedly(ReturnRef(task_runners));
|
||||
|
||||
@@ -927,6 +960,8 @@ TEST(RasterizerTest, presentationTimeSetWhenVsyncTargetInFuture) {
|
||||
thread_host.io_thread->GetTaskRunner());
|
||||
|
||||
NiceMock<MockDelegate> delegate;
|
||||
Settings settings;
|
||||
ON_CALL(delegate, GetSettings()).WillByDefault(ReturnRef(settings));
|
||||
ON_CALL(delegate, GetTaskRunners()).WillByDefault(ReturnRef(task_runners));
|
||||
|
||||
fml::AutoResetWaitableEvent latch;
|
||||
@@ -1007,6 +1042,8 @@ TEST(RasterizerTest, presentationTimeNotSetWhenVsyncTargetInPast) {
|
||||
thread_host.io_thread->GetTaskRunner());
|
||||
|
||||
NiceMock<MockDelegate> delegate;
|
||||
Settings settings;
|
||||
ON_CALL(delegate, GetSettings()).WillByDefault(ReturnRef(settings));
|
||||
ON_CALL(delegate, GetTaskRunners()).WillByDefault(ReturnRef(task_runners));
|
||||
|
||||
fml::AutoResetWaitableEvent latch;
|
||||
|
||||
@@ -216,7 +216,7 @@ class Shell final : public PlatformView::Delegate,
|
||||
//------------------------------------------------------------------------------
|
||||
/// @return The settings used to launch this shell.
|
||||
///
|
||||
const Settings& GetSettings() const;
|
||||
const Settings& GetSettings() const override;
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/// @brief If callers wish to interact directly with any shell
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "include/core/SkMatrix.h"
|
||||
#define FML_USED_ON_EMBEDDER
|
||||
|
||||
#include <algorithm>
|
||||
@@ -2345,8 +2344,8 @@ TEST_F(ShellTest, RasterizerMakeRasterSnapshot) {
|
||||
shell->GetTaskRunners().GetRasterTaskRunner(), [&shell, &latch]() {
|
||||
SnapshotDelegate* delegate =
|
||||
reinterpret_cast<Rasterizer*>(shell->GetRasterizer().get());
|
||||
sk_sp<SkImage> image = delegate->MakeRasterSnapshot(
|
||||
SkPicture::MakePlaceholder({0, 0, 50, 50}), SkISize::Make(50, 50));
|
||||
sk_sp<DlImage> image = delegate->MakeRasterSnapshot(
|
||||
MakeSizedDisplayList(50, 50), SkISize::Make(50, 50));
|
||||
EXPECT_NE(image, nullptr);
|
||||
|
||||
latch->Signal();
|
||||
|
||||
29
engine/src/flutter/shell/common/snapshot_controller.cc
Normal file
29
engine/src/flutter/shell/common/snapshot_controller.cc
Normal file
@@ -0,0 +1,29 @@
|
||||
// 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.
|
||||
|
||||
#include "flutter/shell/common/snapshot_controller.h"
|
||||
|
||||
#include "flutter/shell/common/snapshot_controller_skia.h"
|
||||
|
||||
#if IMPELLER_SUPPORTS_RENDERING
|
||||
#include "flutter/shell/common/snapshot_controller_impeller.h"
|
||||
#endif // IMPELLER_SUPPORTS_RENDERING
|
||||
|
||||
namespace flutter {
|
||||
|
||||
std::unique_ptr<SnapshotController> SnapshotController::Make(
|
||||
const Delegate& delegate,
|
||||
const Settings& settings) {
|
||||
#if IMPELLER_SUPPORTS_RENDERING
|
||||
if (settings.enable_impeller) {
|
||||
return std::make_unique<SnapshotControllerImpeller>(delegate);
|
||||
}
|
||||
#endif // IMPELLER_SUPPORTS_RENDERING
|
||||
return std::make_unique<SnapshotControllerSkia>(delegate);
|
||||
}
|
||||
|
||||
SnapshotController::SnapshotController(const Delegate& delegate)
|
||||
: delegate_(delegate) {}
|
||||
|
||||
} // namespace flutter
|
||||
51
engine/src/flutter/shell/common/snapshot_controller.h
Normal file
51
engine/src/flutter/shell/common/snapshot_controller.h
Normal file
@@ -0,0 +1,51 @@
|
||||
// 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_SHELL_COMMON_SNAPSHOT_CONTROLLER_H_
|
||||
#define FLUTTER_SHELL_COMMON_SNAPSHOT_CONTROLLER_H_
|
||||
|
||||
#include "flutter/common/settings.h"
|
||||
#include "flutter/display_list/display_list_image.h"
|
||||
#include "flutter/flow/surface.h"
|
||||
#include "flutter/fml/synchronization/sync_switch.h"
|
||||
#include "flutter/lib/ui/snapshot_delegate.h"
|
||||
#include "flutter/shell/common/snapshot_surface_producer.h"
|
||||
|
||||
namespace flutter {
|
||||
|
||||
class SnapshotController {
|
||||
public:
|
||||
class Delegate {
|
||||
public:
|
||||
virtual ~Delegate() = default;
|
||||
virtual const std::unique_ptr<Surface>& GetSurface() const = 0;
|
||||
virtual const std::unique_ptr<SnapshotSurfaceProducer>&
|
||||
GetSnapshotSurfaceProducer() const = 0;
|
||||
virtual std::shared_ptr<const fml::SyncSwitch> GetIsGpuDisabledSyncSwitch()
|
||||
const = 0;
|
||||
};
|
||||
|
||||
static std::unique_ptr<SnapshotController> Make(const Delegate& delegate,
|
||||
const Settings& settings);
|
||||
|
||||
virtual ~SnapshotController() = default;
|
||||
|
||||
virtual sk_sp<DlImage> MakeRasterSnapshot(sk_sp<DisplayList> display_list,
|
||||
SkISize size) = 0;
|
||||
|
||||
virtual sk_sp<SkImage> ConvertToRasterImage(sk_sp<SkImage> image) = 0;
|
||||
|
||||
protected:
|
||||
explicit SnapshotController(const Delegate& delegate);
|
||||
const Delegate& GetDelegate() { return delegate_; }
|
||||
|
||||
private:
|
||||
const Delegate& delegate_;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(SnapshotController);
|
||||
};
|
||||
|
||||
} // namespace flutter
|
||||
|
||||
#endif // FLUTTER_SHELL_COMMON_SNAPSHOT_CONTROLLER_H_
|
||||
@@ -0,0 +1,79 @@
|
||||
// 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.
|
||||
|
||||
#include "flutter/shell/common/snapshot_controller_impeller.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "flutter/flow/surface.h"
|
||||
#include "flutter/fml/trace_event.h"
|
||||
#include "flutter/impeller/display_list/display_list_dispatcher.h"
|
||||
#include "flutter/impeller/display_list/display_list_image_impeller.h"
|
||||
#include "flutter/impeller/geometry/size.h"
|
||||
#include "flutter/shell/common/snapshot_controller.h"
|
||||
|
||||
namespace flutter {
|
||||
|
||||
sk_sp<DlImage> SnapshotControllerImpeller::MakeRasterSnapshot(
|
||||
sk_sp<DisplayList> display_list,
|
||||
SkISize size) {
|
||||
sk_sp<DlImage> result;
|
||||
GetDelegate().GetIsGpuDisabledSyncSwitch()->Execute(
|
||||
fml::SyncSwitch::Handlers()
|
||||
.SetIfTrue([&] {
|
||||
// Do nothing.
|
||||
})
|
||||
.SetIfFalse(
|
||||
[&] { result = DoMakeRasterSnapshot(display_list, size); }));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
sk_sp<DlImage> SnapshotControllerImpeller::DoMakeRasterSnapshot(
|
||||
sk_sp<DisplayList> display_list,
|
||||
SkISize size) {
|
||||
impeller::DisplayListDispatcher dispatcher;
|
||||
display_list->Dispatch(dispatcher);
|
||||
impeller::Picture picture = dispatcher.EndRecordingAsPicture();
|
||||
if (GetDelegate().GetSurface() &&
|
||||
GetDelegate().GetSurface()->GetAiksContext()) {
|
||||
impeller::AiksContext* context =
|
||||
GetDelegate().GetSurface()->GetAiksContext();
|
||||
|
||||
auto max_size = context->GetContext()
|
||||
->GetResourceAllocator()
|
||||
->GetMaxTextureSizeSupported();
|
||||
double scale_factor_x =
|
||||
static_cast<double>(max_size.width) / static_cast<double>(size.width());
|
||||
double scale_factor_y = static_cast<double>(max_size.height) /
|
||||
static_cast<double>(size.height());
|
||||
double scale_factor =
|
||||
std::min(1.0, std::min(scale_factor_x, scale_factor_y));
|
||||
|
||||
auto render_target_size = impeller::ISize(size.width(), size.height());
|
||||
|
||||
// Scale down the render target size to the max supported by the
|
||||
// GPU if necessary. Exceeding the max would otherwise cause a
|
||||
// null result.
|
||||
if (scale_factor < 1.0) {
|
||||
render_target_size.width *= scale_factor;
|
||||
render_target_size.height *= scale_factor;
|
||||
}
|
||||
|
||||
std::shared_ptr<impeller::Image> image =
|
||||
picture.ToImage(*context, render_target_size);
|
||||
if (image) {
|
||||
return impeller::DlImageImpeller::Make(image->GetTexture());
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
sk_sp<SkImage> SnapshotControllerImpeller::ConvertToRasterImage(
|
||||
sk_sp<SkImage> image) {
|
||||
FML_UNREACHABLE();
|
||||
}
|
||||
|
||||
} // namespace flutter
|
||||
@@ -0,0 +1,32 @@
|
||||
// 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_SHELL_COMMON_SNAPSHOT_CONTROLLER_IMPELLER_H_
|
||||
#define FLUTTER_SHELL_COMMON_SNAPSHOT_CONTROLLER_IMPELLER_H_
|
||||
|
||||
#include "flutter/shell/common/snapshot_controller.h"
|
||||
|
||||
namespace flutter {
|
||||
|
||||
class SnapshotControllerImpeller : public SnapshotController {
|
||||
public:
|
||||
explicit SnapshotControllerImpeller(
|
||||
const SnapshotController::Delegate& delegate)
|
||||
: SnapshotController(delegate) {}
|
||||
|
||||
sk_sp<DlImage> MakeRasterSnapshot(sk_sp<DisplayList> display_list,
|
||||
SkISize size) override;
|
||||
|
||||
sk_sp<SkImage> ConvertToRasterImage(sk_sp<SkImage> image) override;
|
||||
|
||||
private:
|
||||
sk_sp<DlImage> DoMakeRasterSnapshot(sk_sp<DisplayList> display_list,
|
||||
SkISize size);
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(SnapshotControllerImpeller);
|
||||
};
|
||||
|
||||
} // namespace flutter
|
||||
|
||||
#endif // FLUTTER_SHELL_COMMON_SNAPSHOT_CONTROLLER_IMPELLER_H_
|
||||
156
engine/src/flutter/shell/common/snapshot_controller_skia.cc
Normal file
156
engine/src/flutter/shell/common/snapshot_controller_skia.cc
Normal file
@@ -0,0 +1,156 @@
|
||||
// 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.
|
||||
|
||||
#include "flutter/shell/common/snapshot_controller_skia.h"
|
||||
|
||||
#include "display_list/display_list_image.h"
|
||||
#include "flutter/flow/surface.h"
|
||||
#include "flutter/fml/trace_event.h"
|
||||
#include "flutter/shell/common/snapshot_controller.h"
|
||||
#include "third_party/skia/include/core/SkSurface.h"
|
||||
|
||||
namespace flutter {
|
||||
|
||||
namespace {
|
||||
sk_sp<SkImage> DrawSnapshot(
|
||||
sk_sp<SkSurface> surface,
|
||||
const std::function<void(SkCanvas*)>& draw_callback) {
|
||||
if (surface == nullptr || surface->getCanvas() == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
draw_callback(surface->getCanvas());
|
||||
surface->getCanvas()->flush();
|
||||
|
||||
sk_sp<SkImage> device_snapshot;
|
||||
{
|
||||
TRACE_EVENT0("flutter", "MakeDeviceSnpashot");
|
||||
device_snapshot = surface->makeImageSnapshot();
|
||||
}
|
||||
|
||||
if (device_snapshot == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
{
|
||||
TRACE_EVENT0("flutter", "DeviceHostTransfer");
|
||||
if (auto raster_image = device_snapshot->makeRasterImage()) {
|
||||
return raster_image;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
sk_sp<DlImage> SnapshotControllerSkia::DoMakeRasterSnapshot(
|
||||
SkISize size,
|
||||
std::function<void(SkCanvas*)> draw_callback) {
|
||||
TRACE_EVENT0("flutter", __FUNCTION__);
|
||||
sk_sp<SkImage> result;
|
||||
SkImageInfo image_info = SkImageInfo::MakeN32Premul(
|
||||
size.width(), size.height(), SkColorSpace::MakeSRGB());
|
||||
|
||||
std::unique_ptr<Surface> pbuffer_surface;
|
||||
Surface* snapshot_surface = nullptr;
|
||||
auto& delegate = GetDelegate();
|
||||
if (delegate.GetSurface() && delegate.GetSurface()->GetContext()) {
|
||||
snapshot_surface = delegate.GetSurface().get();
|
||||
} else if (delegate.GetSnapshotSurfaceProducer()) {
|
||||
pbuffer_surface =
|
||||
delegate.GetSnapshotSurfaceProducer()->CreateSnapshotSurface();
|
||||
if (pbuffer_surface && pbuffer_surface->GetContext()) {
|
||||
snapshot_surface = pbuffer_surface.get();
|
||||
}
|
||||
}
|
||||
|
||||
if (!snapshot_surface) {
|
||||
// Raster surface is fine if there is no on screen surface. This might
|
||||
// happen in case of software rendering.
|
||||
sk_sp<SkSurface> sk_surface = SkSurface::MakeRaster(image_info);
|
||||
result = DrawSnapshot(sk_surface, draw_callback);
|
||||
} else {
|
||||
delegate.GetIsGpuDisabledSyncSwitch()->Execute(
|
||||
fml::SyncSwitch::Handlers()
|
||||
.SetIfTrue([&] {
|
||||
sk_sp<SkSurface> surface = SkSurface::MakeRaster(image_info);
|
||||
result = DrawSnapshot(surface, draw_callback);
|
||||
})
|
||||
.SetIfFalse([&] {
|
||||
FML_DCHECK(snapshot_surface);
|
||||
auto context_switch =
|
||||
snapshot_surface->MakeRenderContextCurrent();
|
||||
if (!context_switch->GetResult()) {
|
||||
return;
|
||||
}
|
||||
|
||||
GrRecordingContext* context = snapshot_surface->GetContext();
|
||||
auto max_size = context->maxRenderTargetSize();
|
||||
double scale_factor = std::min(
|
||||
1.0, static_cast<double>(max_size) /
|
||||
static_cast<double>(std::max(image_info.width(),
|
||||
image_info.height())));
|
||||
|
||||
// Scale down the render target size to the max supported by the
|
||||
// GPU if necessary. Exceeding the max would otherwise cause a
|
||||
// null result.
|
||||
if (scale_factor < 1.0) {
|
||||
image_info = image_info.makeWH(
|
||||
static_cast<double>(image_info.width()) * scale_factor,
|
||||
static_cast<double>(image_info.height()) * scale_factor);
|
||||
}
|
||||
|
||||
// When there is an on screen surface, we need a render target
|
||||
// SkSurface because we want to access texture backed images.
|
||||
sk_sp<SkSurface> sk_surface =
|
||||
SkSurface::MakeRenderTarget(context, // context
|
||||
SkBudgeted::kNo, // budgeted
|
||||
image_info // image info
|
||||
);
|
||||
if (!sk_surface) {
|
||||
FML_LOG(ERROR)
|
||||
<< "DoMakeRasterSnapshot can not create GPU render target";
|
||||
return;
|
||||
}
|
||||
|
||||
sk_surface->getCanvas()->scale(scale_factor, scale_factor);
|
||||
result = DrawSnapshot(sk_surface, draw_callback);
|
||||
}));
|
||||
}
|
||||
|
||||
return DlImage::Make(result);
|
||||
}
|
||||
|
||||
sk_sp<DlImage> SnapshotControllerSkia::MakeRasterSnapshot(
|
||||
sk_sp<DisplayList> display_list,
|
||||
SkISize size) {
|
||||
return DoMakeRasterSnapshot(size, [display_list](SkCanvas* canvas) {
|
||||
display_list->RenderTo(canvas);
|
||||
});
|
||||
}
|
||||
|
||||
sk_sp<SkImage> SnapshotControllerSkia::ConvertToRasterImage(
|
||||
sk_sp<SkImage> image) {
|
||||
// If the rasterizer does not have a surface with a GrContext, then it will
|
||||
// be unable to render a cross-context SkImage. The caller will need to
|
||||
// create the raster image on the IO thread.
|
||||
if (GetDelegate().GetSurface() == nullptr ||
|
||||
GetDelegate().GetSurface()->GetContext() == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (image == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
SkISize image_size = image->dimensions();
|
||||
|
||||
auto result = DoMakeRasterSnapshot(
|
||||
image_size, [image = std::move(image)](SkCanvas* canvas) {
|
||||
canvas->drawImage(image, 0, 0);
|
||||
});
|
||||
return result->skia_image();
|
||||
}
|
||||
|
||||
} // namespace flutter
|
||||
33
engine/src/flutter/shell/common/snapshot_controller_skia.h
Normal file
33
engine/src/flutter/shell/common/snapshot_controller_skia.h
Normal file
@@ -0,0 +1,33 @@
|
||||
// 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_SHELL_COMMON_SNAPSHOT_CONTROLLER_SKIA_H_
|
||||
#define FLUTTER_SHELL_COMMON_SNAPSHOT_CONTROLLER_SKIA_H_
|
||||
|
||||
#include "flutter/shell/common/snapshot_controller.h"
|
||||
#include "third_party/skia/include/core/SkSurface.h"
|
||||
|
||||
namespace flutter {
|
||||
|
||||
class SnapshotControllerSkia : public SnapshotController {
|
||||
public:
|
||||
explicit SnapshotControllerSkia(const SnapshotController::Delegate& delegate)
|
||||
: SnapshotController(delegate) {}
|
||||
|
||||
sk_sp<DlImage> MakeRasterSnapshot(sk_sp<DisplayList> display_list,
|
||||
SkISize size) override;
|
||||
|
||||
virtual sk_sp<SkImage> ConvertToRasterImage(sk_sp<SkImage> image) override;
|
||||
|
||||
private:
|
||||
sk_sp<DlImage> DoMakeRasterSnapshot(
|
||||
SkISize size,
|
||||
std::function<void(SkCanvas*)> draw_callback);
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(SnapshotControllerSkia);
|
||||
};
|
||||
|
||||
} // namespace flutter
|
||||
|
||||
#endif // FLUTTER_SHELL_COMMON_SNAPSHOT_CONTROLLER_SKIA_H_
|
||||
@@ -158,4 +158,9 @@ bool GPUSurfaceGLImpeller::EnableRasterCache() const {
|
||||
return false;
|
||||
}
|
||||
|
||||
// |Surface|
|
||||
impeller::AiksContext* GPUSurfaceGLImpeller::GetAiksContext() const {
|
||||
return aiks_context_.get();
|
||||
}
|
||||
|
||||
} // namespace flutter
|
||||
|
||||
@@ -55,6 +55,9 @@ class GPUSurfaceGLImpeller final : public Surface {
|
||||
// |Surface|
|
||||
bool EnableRasterCache() const override;
|
||||
|
||||
// |Surface|
|
||||
impeller::AiksContext* GetAiksContext() const override;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(GPUSurfaceGLImpeller);
|
||||
};
|
||||
|
||||
|
||||
@@ -47,6 +47,9 @@ class SK_API_AVAILABLE_CA_METAL_LAYER GPUSurfaceMetalImpeller : public Surface {
|
||||
// |Surface|
|
||||
bool EnableRasterCache() const override;
|
||||
|
||||
// |Surface|
|
||||
impeller::AiksContext* GetAiksContext() const override;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(GPUSurfaceMetalImpeller);
|
||||
};
|
||||
|
||||
|
||||
@@ -120,4 +120,9 @@ bool GPUSurfaceMetalImpeller::EnableRasterCache() const {
|
||||
return false;
|
||||
}
|
||||
|
||||
// |Surface|
|
||||
impeller::AiksContext* GPUSurfaceMetalImpeller::GetAiksContext() const {
|
||||
return aiks_context_.get();
|
||||
}
|
||||
|
||||
} // namespace flutter
|
||||
|
||||
Reference in New Issue
Block a user