Flutter Engine
 
Loading...
Searching...
No Matches
embedder_test_backingstore_producer_software.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
6
9#include "third_party/skia/include/core/SkColorSpace.h"
10#include "third_party/skia/include/gpu/ganesh/GrBackendSurface.h"
11#include "third_party/skia/include/gpu/ganesh/SkSurfaceGanesh.h"
12
13namespace flutter::testing {
14
15namespace {
16struct UserData {
17 sk_sp<SkSurface> surface;
18};
19} // namespace
20
23 sk_sp<GrDirectContext> context,
25 FlutterSoftwarePixelFormat software_pixfmt)
26 : EmbedderTestBackingStoreProducer(std::move(context), type),
27 software_pixfmt_(software_pixfmt) {
29 software_pixfmt_ != kFlutterSoftwarePixelFormatNative32) {
30 FML_LOG(ERROR) << "Expected pixel format to be the default "
31 "(kFlutterSoftwarePixelFormatNative32) when"
32 "backing store producer should produce deprecated v1 "
33 "software backing "
34 "stores.";
35 std::abort();
36 };
37}
38
41
43 const FlutterBackingStoreConfig* config,
44 FlutterBackingStore* backing_store_out) {
45 switch (type_) {
47 return CreateSoftware(config, backing_store_out);
49 return CreateSoftware2(config, backing_store_out);
50 default:
51 return false;
52 }
53}
54
56 const FlutterBackingStore* backing_store) const {
57 UserData* user_data = reinterpret_cast<UserData*>(backing_store->user_data);
58 return user_data->surface;
59}
60
62 const FlutterBackingStore* backing_store) const {
63 auto user_data = reinterpret_cast<UserData*>(backing_store->user_data);
64 return user_data->surface->makeImageSnapshot();
65}
66
67bool EmbedderTestBackingStoreProducerSoftware::CreateSoftware(
68 const FlutterBackingStoreConfig* config,
69 FlutterBackingStore* backing_store_out) {
70 auto surface = SkSurfaces::Raster(
71 SkImageInfo::MakeN32Premul(config->size.width, config->size.height));
72
73 if (!surface) {
74 FML_LOG(ERROR)
75 << "Could not create the render target for compositor layer.";
76 return false;
77 }
78
79 SkPixmap pixmap;
80 if (!surface->peekPixels(&pixmap)) {
81 FML_LOG(ERROR) << "Could not peek pixels of pixmap.";
82 return false;
83 }
84
85 auto user_data = new UserData{.surface = surface};
86 backing_store_out->type = kFlutterBackingStoreTypeSoftware;
87 backing_store_out->user_data = user_data;
88 backing_store_out->software.allocation = pixmap.addr();
89 backing_store_out->software.row_bytes = pixmap.rowBytes();
90 backing_store_out->software.height = pixmap.height();
91 backing_store_out->software.user_data = user_data;
92 backing_store_out->software.destruction_callback = [](void* user_data) {
93 delete reinterpret_cast<UserData*>(user_data);
94 };
95
96 return true;
97}
98
99bool EmbedderTestBackingStoreProducerSoftware::CreateSoftware2(
100 const FlutterBackingStoreConfig* config,
101 FlutterBackingStore* backing_store_out) {
102 const auto color_info = getSkColorInfo(software_pixfmt_);
103 if (!color_info) {
104 return false;
105 }
106
107 auto surface = SkSurfaces::Raster(SkImageInfo::Make(
108 SkISize::Make(config->size.width, config->size.height), *color_info));
109 if (!surface) {
110 FML_LOG(ERROR)
111 << "Could not create the render target for compositor layer.";
112 return false;
113 }
114
115 SkPixmap pixmap;
116 if (!surface->peekPixels(&pixmap)) {
117 FML_LOG(ERROR) << "Could not peek pixels of pixmap.";
118 return false;
119 }
120
121 auto user_data = new UserData{.surface = surface};
122 backing_store_out->type = kFlutterBackingStoreTypeSoftware2;
123 backing_store_out->user_data = user_data;
124 backing_store_out->software2.struct_size =
126 backing_store_out->software2.allocation = pixmap.writable_addr();
127 backing_store_out->software2.row_bytes = pixmap.rowBytes();
128 backing_store_out->software2.height = pixmap.height();
129 backing_store_out->software2.user_data = user_data;
130 backing_store_out->software2.destruction_callback = [](void* user_data) {
131 delete reinterpret_cast<UserData*>(user_data);
132 };
133 backing_store_out->software2.pixel_format = software_pixfmt_;
134
135 return true;
136}
137
138} // namespace flutter::testing
GLenum type
bool Create(const FlutterBackingStoreConfig *config, FlutterBackingStore *backing_store_out) override
EmbedderTestBackingStoreProducerSoftware(sk_sp< GrDirectContext > context, RenderTargetType type, FlutterSoftwarePixelFormat software_pixfmt=kFlutterSoftwarePixelFormatNative32)
sk_sp< SkSurface > GetSurface(const FlutterBackingStore *backing_store) const override
sk_sp< SkImage > MakeImageSnapshot(const FlutterBackingStore *backing_store) const override
FlutterSoftwarePixelFormat
Definition embedder.h:450
@ kFlutterSoftwarePixelFormatNative32
Definition embedder.h:506
@ kFlutterBackingStoreTypeSoftware2
Definition embedder.h:2060
@ kFlutterBackingStoreTypeSoftware
Specified an software allocation for Flutter to render into using the CPU.
Definition embedder.h:2053
VkSurfaceKHR surface
Definition main.cc:65
#define FML_LOG(severity)
Definition logging.h:101
Definition ref_ptr.h:261
std::optional< SkColorInfo > getSkColorInfo(FlutterSoftwarePixelFormat pixfmt)
FlutterSize size
The size of the render target the engine expects to render into.
Definition embedder.h:2093
FlutterBackingStoreType type
Specifies the type of backing store.
Definition embedder.h:2071
FlutterSoftwareBackingStore software
The description of the software backing store.
Definition embedder.h:2079
FlutterSoftwareBackingStore2 software2
The description of the software backing store.
Definition embedder.h:2081
double height
Definition embedder.h:629
double width
Definition embedder.h:628
size_t struct_size
The size of this struct. Must be sizeof(FlutterSoftwareBackingStore2).
Definition embedder.h:1946
VoidCallback destruction_callback
Definition embedder.h:1960
size_t row_bytes
The number of bytes in a single row of the allocation.
Definition embedder.h:1951
size_t height
The number of rows in the allocation.
Definition embedder.h:1953
FlutterSoftwarePixelFormat pixel_format
Definition embedder.h:1964
VoidCallback destruction_callback
Definition embedder.h:1941
size_t row_bytes
The number of bytes in a single row of the allocation.
Definition embedder.h:1932
size_t height
The number of rows in the allocation.
Definition embedder.h:1934