diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index b64efc428d..0bd541e91f 100755 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -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 diff --git a/engine/src/flutter/flow/BUILD.gn b/engine/src/flutter/flow/BUILD.gn index 9654e891d5..32e2cc2538 100644 --- a/engine/src/flutter/flow/BUILD.gn +++ b/engine/src/flutter/flow/BUILD.gn @@ -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", diff --git a/engine/src/flutter/flow/layers/offscreen_surface.cc b/engine/src/flutter/flow/layers/offscreen_surface.cc new file mode 100644 index 0000000000..dc50bd8bc1 --- /dev/null +++ b/engine/src/flutter/flow/layers/offscreen_surface.cc @@ -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 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(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 GetRasterData(sk_sp 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 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 diff --git a/engine/src/flutter/flow/layers/offscreen_surface.h b/engine/src/flutter/flow/layers/offscreen_surface.h new file mode 100644 index 0000000000..bfa192fb85 --- /dev/null +++ b/engine/src/flutter/flow/layers/offscreen_surface.h @@ -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 GetRasterData(bool compressed) const; + + SkCanvas* GetCanvas() const; + + bool IsValid() const; + + private: + sk_sp offscreen_surface_; + + FML_DISALLOW_COPY_AND_ASSIGN(OffscreenSurface); +}; + +} // namespace flutter + +#endif // FLUTTER_FLOW_LAYERS_OFFSCREEN_SURFACE_H_ diff --git a/engine/src/flutter/flow/layers/offscreen_surface_unittests.cc b/engine/src/flutter/flow/layers/offscreen_surface_unittests.cc new file mode 100644 index 0000000000..64063b0001 --- /dev/null +++ b/engine/src/flutter/flow/layers/offscreen_surface_unittests.cc @@ -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 + +#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(nullptr, SkISize::MakeEmpty()); + ASSERT_FALSE(surface->IsValid()); +} + +TEST(OffscreenSurfaceTest, OnexOneSurfaceIsValid) { + auto surface = + std::make_unique(nullptr, SkISize::Make(1, 1)); + ASSERT_TRUE(surface->IsValid()); +} + +TEST(OffscreenSurfaceTest, PaintSurfaceBlack) { + auto surface = + std::make_unique(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(raster_data->data()); + + // picking black as the color since byte ordering seems to matter. + ASSERT_EQ(actual[0], 0xFF000000u); +} + +} // namespace flutter::testing diff --git a/engine/src/flutter/shell/common/rasterizer.cc b/engine/src/flutter/shell/common/rasterizer.cc index eb47b37562..5941ca68ec 100644 --- a/engine/src/flutter/shell/common/rasterizer.cc +++ b/engine/src/flutter/shell/common/rasterizer.cc @@ -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 ScreenshotLayerTreeAsPicture( return recorder.finishRecordingAsPicture()->serialize(&procs); } -static sk_sp 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 Rasterizer::ScreenshotLayerTreeAsImage( flutter::LayerTree* tree, flutter::CompositorContext& compositor_context, @@ -683,15 +666,16 @@ sk_sp 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 snapshot_surface = + std::make_unique(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 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(