Flutter Engine
The Flutter Engine
offscreen_surface.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#if !SLIMPELLER
6
7#include "flutter/flow/layers/offscreen_surface.h"
8
17
18namespace flutter {
19
21 const SkISize& size) {
22 const auto image_info = SkImageInfo::MakeN32Premul(
23 size.width(), size.height(), SkColorSpace::MakeSRGB());
24 if (surface_context) {
25 // There is a rendering surface that may contain textures that are going to
26 // be referenced in the layer tree about to be drawn.
27 return SkSurfaces::RenderTarget(surface_context, skgpu::Budgeted::kNo,
28 image_info);
29 }
30
31 // There is no rendering surface, assume no GPU textures are present and
32 // create a raster surface.
33 return SkSurfaces::Raster(image_info);
34}
35
36/// Returns a buffer containing a snapshot of the surface.
37///
38/// If compressed is true the data is encoded as PNG.
39static sk_sp<SkData> GetRasterData(const sk_sp<SkSurface>& offscreen_surface,
40 bool compressed) {
41 // Prepare an image from the surface, this image may potentially be on th GPU.
42 auto potentially_gpu_snapshot = offscreen_surface->makeImageSnapshot();
43 if (!potentially_gpu_snapshot) {
44 FML_LOG(ERROR) << "Screenshot: unable to make image screenshot";
45 return nullptr;
46 }
47
48 // Copy the GPU image snapshot into CPU memory.
49 // TODO (https://github.com/flutter/flutter/issues/13498)
50 auto cpu_snapshot = potentially_gpu_snapshot->makeRasterImage();
51 if (!cpu_snapshot) {
52 FML_LOG(ERROR) << "Screenshot: unable to make raster image";
53 return nullptr;
54 }
55
56 // If the caller want the pixels to be compressed, there is a Skia utility to
57 // compress to PNG. Use that.
58 if (compressed) {
59 return SkPngEncoder::Encode(nullptr, cpu_snapshot.get(), {});
60 }
61
62 // Copy it into a bitmap and return the same.
63 SkPixmap pixmap;
64 if (!cpu_snapshot->peekPixels(&pixmap)) {
65 FML_LOG(ERROR) << "Screenshot: unable to obtain bitmap pixels";
66 return nullptr;
67 }
68 return SkData::MakeWithCopy(pixmap.addr32(), pixmap.computeByteSize());
69}
70
72 const SkISize& size) {
73 offscreen_surface_ = CreateSnapshotSurface(surface_context, size);
74 if (offscreen_surface_) {
75 adapter_.set_canvas(offscreen_surface_->getCanvas());
76 }
77}
78
80 return flutter::GetRasterData(offscreen_surface_, compressed);
81}
82
84 return &adapter_;
85}
86
88 return offscreen_surface_ != nullptr;
89}
90
91} // namespace flutter
92
93#endif // !SLIMPELLER
static sk_sp< SkColorSpace > MakeSRGB()
static sk_sp< SkData > MakeWithCopy(const void *data, size_t length)
Definition: SkData.cpp:111
const uint32_t * addr32() const
Definition: SkPixmap.h:352
size_t computeByteSize() const
Definition: SkPixmap.h:231
SkCanvas * getCanvas()
Definition: SkSurface.cpp:82
sk_sp< SkImage > makeImageSnapshot()
Definition: SkSurface.cpp:90
Developer-facing API for rendering anything within the engine.
Definition: dl_canvas.h:38
void set_canvas(SkCanvas *canvas)
Definition: dl_sk_canvas.cc:39
sk_sp< SkData > GetRasterData(bool compressed) const
OffscreenSurface(GrDirectContext *surface_context, const SkISize &size)
#define FML_LOG(severity)
Definition: logging.h:82
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
SK_API sk_sp< SkSurface > RenderTarget(GrRecordingContext *context, skgpu::Budgeted budgeted, const SkImageInfo &imageInfo, int sampleCount, GrSurfaceOrigin surfaceOrigin, const SkSurfaceProps *surfaceProps, bool shouldCreateWithMips=false, bool isProtected=false)
static sk_sp< SkSurface > CreateSnapshotSurface(GrDirectContext *surface_context, const SkISize &size)
static sk_sp< SkData > GetRasterData(const sk_sp< SkSurface > &offscreen_surface, bool compressed)
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
Definition: SkSize.h:16
static SkImageInfo MakeN32Premul(int width, int height)
#define ERROR(message)
Definition: elf_loader.cc:260