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#if !SLIMPELLER
6
8
9#include "third_party/skia/include/core/SkColorSpace.h"
10#include "third_party/skia/include/core/SkData.h"
11#include "third_party/skia/include/core/SkImage.h"
12#include "third_party/skia/include/core/SkImageInfo.h"
13#include "third_party/skia/include/core/SkPixmap.h"
14#include "third_party/skia/include/encode/SkPngEncoder.h"
15#include "third_party/skia/include/gpu/ganesh/GrDirectContext.h"
16#include "third_party/skia/include/gpu/ganesh/SkSurfaceGanesh.h"
17
18namespace flutter {
19
20static sk_sp<SkSurface> CreateSnapshotSurface(GrDirectContext* surface_context,
21 const DlISize& size) {
22 const auto image_info = SkImageInfo::MakeN32Premul(size.width, size.height,
23 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(nullptr);
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
71OffscreenSurface::OffscreenSurface(GrDirectContext* surface_context,
72 const DlISize& size) {
73 offscreen_surface_ = CreateSnapshotSurface(surface_context, size);
74 if (offscreen_surface_) {
75 adapter_.set_canvas(offscreen_surface_->getCanvas());
76 }
77}
78
79sk_sp<SkData> OffscreenSurface::GetRasterData(bool compressed) const {
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
Developer-facing API for rendering anything within the engine.
Definition dl_canvas.h:32
void set_canvas(SkCanvas *canvas)
OffscreenSurface(GrDirectContext *surface_context, const DlISize &size)
sk_sp< SkData > GetRasterData(bool compressed) const
#define FML_LOG(severity)
Definition logging.h:101
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 use test Running tests that layout and measure text will not yield consistent results across various platforms Enabling this option will make font resolution default to the Ahem test font on all 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
static sk_sp< SkSurface > CreateSnapshotSurface(GrDirectContext *surface_context, const DlISize &size)