Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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#include "flutter/flow/layers/offscreen_surface.h"
6
15
16namespace flutter {
17
19 const SkISize& size) {
20 const auto image_info = SkImageInfo::MakeN32Premul(
21 size.width(), size.height(), SkColorSpace::MakeSRGB());
22 if (surface_context) {
23 // There is a rendering surface that may contain textures that are going to
24 // be referenced in the layer tree about to be drawn.
25 return SkSurfaces::RenderTarget(surface_context, skgpu::Budgeted::kNo,
26 image_info);
27 }
28
29 // There is no rendering surface, assume no GPU textures are present and
30 // create a raster surface.
31 return SkSurfaces::Raster(image_info);
32}
33
34/// Returns a buffer containing a snapshot of the surface.
35///
36/// If compressed is true the data is encoded as PNG.
37static sk_sp<SkData> GetRasterData(const sk_sp<SkSurface>& offscreen_surface,
38 bool compressed) {
39 // Prepare an image from the surface, this image may potentially be on th GPU.
40 auto potentially_gpu_snapshot = offscreen_surface->makeImageSnapshot();
41 if (!potentially_gpu_snapshot) {
42 FML_LOG(ERROR) << "Screenshot: unable to make image screenshot";
43 return nullptr;
44 }
45
46 // Copy the GPU image snapshot into CPU memory.
47 // TODO (https://github.com/flutter/flutter/issues/13498)
48 auto cpu_snapshot = potentially_gpu_snapshot->makeRasterImage();
49 if (!cpu_snapshot) {
50 FML_LOG(ERROR) << "Screenshot: unable to make raster image";
51 return nullptr;
52 }
53
54 // If the caller want the pixels to be compressed, there is a Skia utility to
55 // compress to PNG. Use that.
56 if (compressed) {
57 return SkPngEncoder::Encode(nullptr, cpu_snapshot.get(), {});
58 }
59
60 // Copy it into a bitmap and return the same.
61 SkPixmap pixmap;
62 if (!cpu_snapshot->peekPixels(&pixmap)) {
63 FML_LOG(ERROR) << "Screenshot: unable to obtain bitmap pixels";
64 return nullptr;
65 }
66 return SkData::MakeWithCopy(pixmap.addr32(), pixmap.computeByteSize());
67}
68
70 const SkISize& size) {
71 offscreen_surface_ = CreateSnapshotSurface(surface_context, size);
72 if (offscreen_surface_) {
73 adapter_.set_canvas(offscreen_surface_->getCanvas());
74 }
75}
76
78 return flutter::GetRasterData(offscreen_surface_, compressed);
79}
80
82 return &adapter_;
83}
84
86 return offscreen_surface_ != nullptr;
87}
88
89} // namespace flutter
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
Developer-facing API for rendering anything within the engine.
Definition dl_canvas.h:37
void set_canvas(SkCanvas *canvas)
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
static SkImageInfo MakeN32Premul(int width, int height)
#define ERROR(message)