Refactor drawing to offscreen surface (flutter/engine#32338)
This commit is contained in:
@@ -146,6 +146,9 @@ FILE: ../../../flutter/flow/layers/layer.h
|
||||
FILE: ../../../flutter/flow/layers/layer_tree.cc
|
||||
FILE: ../../../flutter/flow/layers/layer_tree.h
|
||||
FILE: ../../../flutter/flow/layers/layer_tree_unittests.cc
|
||||
FILE: ../../../flutter/flow/layers/offscreen_surface.cc
|
||||
FILE: ../../../flutter/flow/layers/offscreen_surface.h
|
||||
FILE: ../../../flutter/flow/layers/offscreen_surface_unittests.cc
|
||||
FILE: ../../../flutter/flow/layers/opacity_layer.cc
|
||||
FILE: ../../../flutter/flow/layers/opacity_layer.h
|
||||
FILE: ../../../flutter/flow/layers/opacity_layer_unittests.cc
|
||||
|
||||
@@ -38,6 +38,8 @@ source_set("flow") {
|
||||
"layers/layer.h",
|
||||
"layers/layer_tree.cc",
|
||||
"layers/layer_tree.h",
|
||||
"layers/offscreen_surface.cc",
|
||||
"layers/offscreen_surface.h",
|
||||
"layers/opacity_layer.cc",
|
||||
"layers/opacity_layer.h",
|
||||
"layers/performance_overlay_layer.cc",
|
||||
@@ -146,6 +148,7 @@ if (enable_unittests) {
|
||||
"layers/display_list_layer_unittests.cc",
|
||||
"layers/image_filter_layer_unittests.cc",
|
||||
"layers/layer_tree_unittests.cc",
|
||||
"layers/offscreen_surface_unittests.cc",
|
||||
"layers/opacity_layer_unittests.cc",
|
||||
"layers/performance_overlay_layer_unittests.cc",
|
||||
"layers/physical_shape_layer_unittests.cc",
|
||||
|
||||
85
engine/src/flutter/flow/layers/offscreen_surface.cc
Normal file
85
engine/src/flutter/flow/layers/offscreen_surface.cc
Normal file
@@ -0,0 +1,85 @@
|
||||
// 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/flow/layers/offscreen_surface.h"
|
||||
|
||||
#include "third_party/skia/include/core/SkImageEncoder.h"
|
||||
#include "third_party/skia/include/core/SkPictureRecorder.h"
|
||||
#include "third_party/skia/include/core/SkSerialProcs.h"
|
||||
#include "third_party/skia/include/core/SkSurface.h"
|
||||
#include "third_party/skia/include/core/SkSurfaceCharacterization.h"
|
||||
#include "third_party/skia/include/utils/SkBase64.h"
|
||||
|
||||
namespace flutter {
|
||||
|
||||
static sk_sp<SkSurface> CreateSnapshotSurface(GrDirectContext* surface_context,
|
||||
const SkISize& size) {
|
||||
const auto image_info = SkImageInfo::MakeN32Premul(
|
||||
size.width(), size.height(), SkColorSpace::MakeSRGB());
|
||||
if (surface_context) {
|
||||
// There is a rendering surface that may contain textures that are going to
|
||||
// be referenced in the layer tree about to be drawn.
|
||||
return SkSurface::MakeRenderTarget(
|
||||
reinterpret_cast<GrRecordingContext*>(surface_context), SkBudgeted::kNo,
|
||||
image_info);
|
||||
}
|
||||
|
||||
// There is no rendering surface, assume no GPU textures are present and
|
||||
// create a raster surface.
|
||||
return SkSurface::MakeRaster(image_info);
|
||||
}
|
||||
|
||||
/// Returns a buffer containing a snapshot of the surface.
|
||||
///
|
||||
/// If compressed is true the data is encoded as PNG.
|
||||
static sk_sp<SkData> GetRasterData(sk_sp<SkSurface> offscreen_surface,
|
||||
bool compressed) {
|
||||
// Prepare an image from the surface, this image may potentially be on th GPU.
|
||||
auto potentially_gpu_snapshot = offscreen_surface->makeImageSnapshot();
|
||||
if (!potentially_gpu_snapshot) {
|
||||
FML_LOG(ERROR) << "Screenshot: unable to make image screenshot";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Copy the GPU image snapshot into CPU memory.
|
||||
// TODO (https://github.com/flutter/flutter/issues/13498)
|
||||
auto cpu_snapshot = potentially_gpu_snapshot->makeRasterImage();
|
||||
if (!cpu_snapshot) {
|
||||
FML_LOG(ERROR) << "Screenshot: unable to make raster image";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// If the caller want the pixels to be compressed, there is a Skia utility to
|
||||
// compress to PNG. Use that.
|
||||
if (compressed) {
|
||||
return cpu_snapshot->encodeToData();
|
||||
}
|
||||
|
||||
// Copy it into a bitmap and return the same.
|
||||
SkPixmap pixmap;
|
||||
if (!cpu_snapshot->peekPixels(&pixmap)) {
|
||||
FML_LOG(ERROR) << "Screenshot: unable to obtain bitmap pixels";
|
||||
return nullptr;
|
||||
}
|
||||
return SkData::MakeWithCopy(pixmap.addr32(), pixmap.computeByteSize());
|
||||
}
|
||||
|
||||
OffscreenSurface::OffscreenSurface(GrDirectContext* surface_context,
|
||||
const SkISize& size) {
|
||||
offscreen_surface_ = CreateSnapshotSurface(surface_context, size);
|
||||
}
|
||||
|
||||
sk_sp<SkData> OffscreenSurface::GetRasterData(bool compressed) const {
|
||||
return flutter::GetRasterData(offscreen_surface_, compressed);
|
||||
}
|
||||
|
||||
SkCanvas* OffscreenSurface::GetCanvas() const {
|
||||
return offscreen_surface_->getCanvas();
|
||||
}
|
||||
|
||||
bool OffscreenSurface::IsValid() const {
|
||||
return offscreen_surface_ != nullptr;
|
||||
}
|
||||
|
||||
} // namespace flutter
|
||||
36
engine/src/flutter/flow/layers/offscreen_surface.h
Normal file
36
engine/src/flutter/flow/layers/offscreen_surface.h
Normal file
@@ -0,0 +1,36 @@
|
||||
// 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_FLOW_LAYERS_OFFSCREEN_SURFACE_H_
|
||||
#define FLUTTER_FLOW_LAYERS_OFFSCREEN_SURFACE_H_
|
||||
|
||||
#include "flutter/fml/logging.h"
|
||||
#include "flutter/fml/macros.h"
|
||||
|
||||
#include "third_party/skia/include/core/SkSurface.h"
|
||||
|
||||
namespace flutter {
|
||||
|
||||
class OffscreenSurface {
|
||||
public:
|
||||
explicit OffscreenSurface(GrDirectContext* surface_context,
|
||||
const SkISize& size);
|
||||
|
||||
~OffscreenSurface() = default;
|
||||
|
||||
sk_sp<SkData> GetRasterData(bool compressed) const;
|
||||
|
||||
SkCanvas* GetCanvas() const;
|
||||
|
||||
bool IsValid() const;
|
||||
|
||||
private:
|
||||
sk_sp<SkSurface> offscreen_surface_;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(OffscreenSurface);
|
||||
};
|
||||
|
||||
} // namespace flutter
|
||||
|
||||
#endif // FLUTTER_FLOW_LAYERS_OFFSCREEN_SURFACE_H_
|
||||
@@ -0,0 +1,44 @@
|
||||
// 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/flow/layers/offscreen_surface.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "include/core/SkColor.h"
|
||||
#include "third_party/skia/include/core/SkCanvas.h"
|
||||
#include "third_party/skia/include/core/SkData.h"
|
||||
|
||||
namespace flutter::testing {
|
||||
|
||||
TEST(OffscreenSurfaceTest, EmptySurfaceIsInvalid) {
|
||||
auto surface =
|
||||
std::make_unique<OffscreenSurface>(nullptr, SkISize::MakeEmpty());
|
||||
ASSERT_FALSE(surface->IsValid());
|
||||
}
|
||||
|
||||
TEST(OffscreenSurfaceTest, OnexOneSurfaceIsValid) {
|
||||
auto surface =
|
||||
std::make_unique<OffscreenSurface>(nullptr, SkISize::Make(1, 1));
|
||||
ASSERT_TRUE(surface->IsValid());
|
||||
}
|
||||
|
||||
TEST(OffscreenSurfaceTest, PaintSurfaceBlack) {
|
||||
auto surface =
|
||||
std::make_unique<OffscreenSurface>(nullptr, SkISize::Make(1, 1));
|
||||
|
||||
SkCanvas* canvas = surface->GetCanvas();
|
||||
canvas->clear(SK_ColorBLACK);
|
||||
canvas->flush();
|
||||
|
||||
auto raster_data = surface->GetRasterData(false);
|
||||
const uint32_t* actual =
|
||||
reinterpret_cast<const uint32_t*>(raster_data->data());
|
||||
|
||||
// picking black as the color since byte ordering seems to matter.
|
||||
ASSERT_EQ(actual[0], 0xFF000000u);
|
||||
}
|
||||
|
||||
} // namespace flutter::testing
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
#include "flow/frame_timings.h"
|
||||
#include "flutter/common/graphics/persistent_cache.h"
|
||||
#include "flutter/flow/layers/offscreen_surface.h"
|
||||
#include "flutter/fml/time/time_delta.h"
|
||||
#include "flutter/fml/time/time_point.h"
|
||||
#include "flutter/shell/common/serialization_callbacks.h"
|
||||
@@ -658,24 +659,6 @@ static sk_sp<SkData> ScreenshotLayerTreeAsPicture(
|
||||
return recorder.finishRecordingAsPicture()->serialize(&procs);
|
||||
}
|
||||
|
||||
static sk_sp<SkSurface> CreateSnapshotSurface(GrDirectContext* surface_context,
|
||||
const SkISize& size) {
|
||||
const auto image_info = SkImageInfo::MakeN32Premul(
|
||||
size.width(), size.height(), SkColorSpace::MakeSRGB());
|
||||
if (surface_context) {
|
||||
// There is a rendering surface that may contain textures that are going to
|
||||
// be referenced in the layer tree about to be drawn.
|
||||
return SkSurface::MakeRenderTarget(surface_context, //
|
||||
SkBudgeted::kNo, //
|
||||
image_info //
|
||||
);
|
||||
}
|
||||
|
||||
// There is no rendering surface, assume no GPU textures are present and
|
||||
// create a raster surface.
|
||||
return SkSurface::MakeRaster(image_info);
|
||||
}
|
||||
|
||||
sk_sp<SkData> Rasterizer::ScreenshotLayerTreeAsImage(
|
||||
flutter::LayerTree* tree,
|
||||
flutter::CompositorContext& compositor_context,
|
||||
@@ -683,15 +666,16 @@ sk_sp<SkData> Rasterizer::ScreenshotLayerTreeAsImage(
|
||||
bool compressed) {
|
||||
// Attempt to create a snapshot surface depending on whether we have access to
|
||||
// a valid GPU rendering context.
|
||||
auto snapshot_surface =
|
||||
CreateSnapshotSurface(surface_context, tree->frame_size());
|
||||
if (snapshot_surface == nullptr) {
|
||||
std::unique_ptr<OffscreenSurface> snapshot_surface =
|
||||
std::make_unique<OffscreenSurface>(surface_context, tree->frame_size());
|
||||
|
||||
if (!snapshot_surface->IsValid()) {
|
||||
FML_LOG(ERROR) << "Screenshot: unable to create snapshot surface";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Draw the current layer tree into the snapshot surface.
|
||||
auto* canvas = snapshot_surface->getCanvas();
|
||||
auto* canvas = snapshot_surface->GetCanvas();
|
||||
|
||||
// There is no root surface transformation for the screenshot layer. Reset the
|
||||
// matrix to identity.
|
||||
@@ -722,33 +706,7 @@ sk_sp<SkData> Rasterizer::ScreenshotLayerTreeAsImage(
|
||||
frame->Raster(*tree, true, nullptr);
|
||||
canvas->flush();
|
||||
|
||||
// Prepare an image from the surface, this image may potentially be on th GPU.
|
||||
auto potentially_gpu_snapshot = snapshot_surface->makeImageSnapshot();
|
||||
if (!potentially_gpu_snapshot) {
|
||||
FML_LOG(ERROR) << "Screenshot: unable to make image screenshot";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Copy the GPU image snapshot into CPU memory.
|
||||
auto cpu_snapshot = potentially_gpu_snapshot->makeRasterImage();
|
||||
if (!cpu_snapshot) {
|
||||
FML_LOG(ERROR) << "Screenshot: unable to make raster image";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// If the caller want the pixels to be compressed, there is a Skia utility to
|
||||
// compress to PNG. Use that.
|
||||
if (compressed) {
|
||||
return cpu_snapshot->encodeToData();
|
||||
}
|
||||
|
||||
// Copy it into a bitmap and return the same.
|
||||
SkPixmap pixmap;
|
||||
if (!cpu_snapshot->peekPixels(&pixmap)) {
|
||||
FML_LOG(ERROR) << "Screenshot: unable to obtain bitmap pixels";
|
||||
return nullptr;
|
||||
}
|
||||
return SkData::MakeWithCopy(pixmap.addr32(), pixmap.computeByteSize());
|
||||
return snapshot_surface->GetRasterData(compressed);
|
||||
}
|
||||
|
||||
Rasterizer::Screenshot Rasterizer::ScreenshotLastLayerTree(
|
||||
|
||||
Reference in New Issue
Block a user