Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
dl_test_surface_instance_skia.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
10#include "third_party/skia/include/encode/SkPngEncoder.h"
11#include "third_party/skia/include/gpu/ganesh/GrDirectContext.h"
12
13namespace {
14
15class DlSkiaPixelData : public flutter::testing::DlPixelData {
16 private:
17 struct FreeDeleter {
18 void operator()(uint8_t* p) { std::free(p); }
19 };
20
21 public:
22 static std::unique_ptr<DlSkiaPixelData> MakeFromRasterImage(
23 const sk_sp<SkImage>& raster_image) {
24 SkImageInfo info = raster_image->imageInfo();
25 info = SkImageInfo::MakeN32Premul(info.dimensions());
26 FML_CHECK(info.bytesPerPixel() == 4);
27
28 size_t min_row_bytes = info.minRowBytes();
29 size_t byte_count = info.computeByteSize(min_row_bytes);
30 if (byte_count == std::numeric_limits<size_t>::max()) {
31 return nullptr;
32 }
33
34 std::unique_ptr<uint8_t, FreeDeleter> pixels;
35 pixels.reset(static_cast<uint8_t*>(std::malloc(byte_count)));
36 if (!pixels.get()) {
37 return nullptr;
38 }
39
40 SkPixmap pixmap;
41 // Resetting the pixmap does not give ownership of pixels to it.
42 pixmap.reset(info, pixels.get(), info.minRowBytes());
43 if (!raster_image->readPixels(pixmap, 0, 0)) {
44 return nullptr;
45 }
46
47 // We could hand in the already established SkPixmap object, but the
48 // pixel ownership needs to be explicitly transferred via std::move.
49 // Passing the pixmap doesn't transfer that ownership and passing it
50 // while additionally transferring the ownership creates an odd
51 // case of the pixmap having a pointer in it that is undergoing a
52 // transfer. Safer to pass in the raw info and then re-establish the
53 // pixmap field (for ease of the access methods) in the constructor.
54 return std::make_unique<DlSkiaPixelData>(info, min_row_bytes,
55 std::move(pixels));
56 }
57
58 DlSkiaPixelData(const SkImageInfo& info,
59 size_t row_bytes,
60 std::unique_ptr<uint8_t, FreeDeleter> pixels)
61 : pixels_(std::move(pixels)) {
62 // Resetting the pixmap_ does not give ownership of pixels to it.
63 pixmap_.reset(info, pixels_.get(), row_bytes);
64 }
65
66 ~DlSkiaPixelData() override = default;
67
68 const uint32_t* addr32(uint32_t x, uint32_t y) const override {
69 if (x >= width() || y >= height()) {
70 return nullptr;
71 }
72 return static_cast<const uint32_t*>(pixmap_.addr(x, y));
73 }
74
75 size_t width() const override { return pixmap_.info().width(); }
76 size_t height() const override { return pixmap_.info().height(); }
77
78 virtual bool write(const std::string& path) const override {
79 sk_sp<SkData> data = SkPngEncoder::Encode(pixmap_, {});
80 if (!data) {
81 return false;
82 }
83 fml::NonOwnedMapping mapping(static_cast<const uint8_t*>(data->data()),
84 data->size());
86 path.c_str(), mapping);
87 }
88
89 private:
90 std::unique_ptr<uint8_t, FreeDeleter> pixels_;
91 SkPixmap pixmap_;
92};
93
94} // namespace
95
96namespace flutter {
97namespace testing {
98
100
102
105
107
109 GetCanvas()->Clear(color);
110}
111
113 if (adapter_.canvas() == nullptr) {
114 adapter_.set_canvas(GetSurface()->getCanvas());
115 adapter_.canvas()->save();
116 }
117 return &adapter_;
118}
119
121 const sk_sp<DisplayList>& display_list) {
122 GetCanvas()->DrawDisplayList(display_list);
123}
124
126 auto surface = GetSurface();
127 if (!surface) {
128 return;
129 }
130 if (GrDirectContext* dContext =
131 GrAsDirectContext(surface->recordingContext())) {
132 dContext->flushAndSubmit(surface.get(), GrSyncCpu::kYes);
133 }
134 adapter_.canvas()->restoreToCount(0);
135 adapter_.canvas()->save();
136}
137
138bool DlSurfaceInstanceSkiaBase::SnapshotToFile(std::string& filename) const {
139 sk_sp<SkImage> raster = GetRasterImage();
140 if (!raster) {
141 return false;
142 }
143 sk_sp<SkData> data = SkPngEncoder::Encode(nullptr, raster.get(), {});
144 if (!data) {
145 return false;
146 }
147 fml::NonOwnedMapping mapping(static_cast<const uint8_t*>(data->data()),
148 data->size());
149 return WriteAtomically(OpenFixturesDirectory(), filename.c_str(), mapping);
150}
151
153 const {
154 sk_sp<SkImage> raster = GetRasterImage();
155
156 return raster ? DlSkiaPixelData::MakeFromRasterImage(raster) : nullptr;
157}
158
160 return DlImageSkia::Make(GetRasterImage());
161}
162
164 return GetSurface()->width();
165}
166
168 return GetSurface()->height();
169}
170
171sk_sp<SkImage> DlSurfaceInstanceSkiaBase::GetRasterImage() const {
172 auto surface = GetSurface();
173 auto image = surface->makeImageSnapshot();
174 if (!image) {
175 return nullptr;
176 }
177 return image->makeRasterImage(nullptr);
178}
179
180sk_sp<SkSurface> DlSurfaceInstanceSkia::GetSurface() const {
181 return surface_;
182}
183
184} // namespace testing
185} // namespace flutter
Developer-facing API for rendering anything within the engine.
Definition dl_canvas.h:32
virtual void DrawDisplayList(const sk_sp< DisplayList > display_list, DlScalar opacity=SK_Scalar1)=0
void Clear(DlColor color)
Definition dl_canvas.h:104
static sk_sp< DlImage > Make(const SkImage *image)
void set_canvas(SkCanvas *canvas)
virtual size_t height() const =0
virtual const uint32_t * addr32(uint32_t x, uint32_t y) const =0
virtual bool write(const std::string &path) const =0
virtual size_t width() const =0
std::unique_ptr< DlPixelData > SnapshotToPixelData() const override
virtual sk_sp< SkSurface > GetSurface() const =0
void RenderDisplayList(const sk_sp< DisplayList > &display_list) override
bool SnapshotToFile(std::string &filename) const override
Store a snapshot of this Surface to the file indicated by the filename.
int height() const override
The height of the underlying surface.
void Clear(const DlColor &color) override
Clear the entire surface to the indicated color.
int width() const override
The width of the underlying surface.
int32_t x
FlutterVulkanImage * image
VkSurfaceKHR surface
Definition main.cc:65
#define FML_CHECK(condition)
Definition logging.h:104
EGLSurface surface_
double y
fml::UniqueFD OpenFixturesDirectory()
Opens the fixtures directory for the unit-test harness.
Definition testing.cc:22
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition switch_defs.h:52
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition switch_defs.h:36
bool WriteAtomically(const fml::UniqueFD &base_directory, const char *file_name, const Mapping &mapping)
Definition ref_ptr.h:261