Flutter Engine
The Flutter Engine
image_generator.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/lib/ui/painting/image_generator.h"
6
7#include <utility>
8
9#include "flutter/fml/logging.h"
14
15namespace flutter {
16
18
21
23 if (!bitmap.tryAllocPixels(info)) {
24 FML_DLOG(ERROR) << "Failed to allocate memory for bitmap of size "
25 << info.computeMinByteSize() << "B";
26 return nullptr;
27 }
28
29 const auto& pixmap = bitmap.pixmap();
30 if (!GetPixels(pixmap.info(), pixmap.writable_addr(), pixmap.rowBytes())) {
31 FML_DLOG(ERROR) << "Failed to get pixels for image.";
32 return nullptr;
33 }
34 bitmap.setImmutable();
36}
37
39
41 std::unique_ptr<SkImageGenerator> generator)
42 : generator_(std::move(generator)) {}
43
45 return generator_->getInfo();
46}
47
49 return 1;
50}
51
53 return 1;
54}
55
57 unsigned int frame_index) {
58 return {.required_frame = std::nullopt,
59 .duration = 0,
61}
62
64 return generator_->getInfo().dimensions();
65}
66
68 const SkImageInfo& info,
69 void* pixels,
70 size_t row_bytes,
71 unsigned int frame_index,
72 std::optional<unsigned int> prior_frame) {
73 return generator_->getPixels(info, pixels, row_bytes);
74}
75
76std::unique_ptr<ImageGenerator> BuiltinSkiaImageGenerator::MakeFromGenerator(
77 std::unique_ptr<SkImageGenerator> generator) {
78 if (!generator) {
79 return nullptr;
80 }
81 return std::make_unique<BuiltinSkiaImageGenerator>(std::move(generator));
82}
83
85
87 SkImageInfo info = codec->getInfo();
90 }
91 if (kUnpremul_SkAlphaType == info.alphaType()) {
92 // Prefer premul over unpremul (this produces better filtering in general)
93 info = info.makeAlphaType(kPremul_SkAlphaType);
94 }
95 return info;
96}
97
99 std::unique_ptr<SkCodec> codec)
100 : codec_(std::move(codec)) {
101 image_info_ = getInfoIncludingExif(codec_.get());
102}
103
106 : codec_(SkCodec::MakeFromData(std::move(buffer)).release()) {
107 image_info_ = getInfoIncludingExif(codec_.get());
108}
109
111 return image_info_;
112}
113
115 return codec_->getFrameCount();
116}
117
119 auto repetition_count = codec_->getRepetitionCount();
120 return repetition_count < 0 ? kInfinitePlayCount : repetition_count + 1;
121}
122
124 unsigned int frame_index) {
126 codec_->getFrameInfo(frame_index, &info);
127 return {
128 .required_frame = info.fRequiredFrame == SkCodec::kNoFrame
129 ? std::nullopt
130 : std::optional<unsigned int>(info.fRequiredFrame),
131 .duration = static_cast<unsigned int>(info.fDuration),
132 .disposal_method = info.fDisposalMethod};
133}
134
136 float desired_scale) {
137 SkISize size = codec_->getScaledDimensions(desired_scale);
138 if (SkEncodedOriginSwapsWidthHeight(codec_->getOrigin())) {
139 std::swap(size.fWidth, size.fHeight);
140 }
141 return size;
142}
143
145 const SkImageInfo& info,
146 void* pixels,
147 size_t row_bytes,
148 unsigned int frame_index,
149 std::optional<unsigned int> prior_frame) {
151 options.fFrameIndex = frame_index;
152 if (prior_frame.has_value()) {
153 options.fPriorFrame = prior_frame.value();
154 }
155 SkEncodedOrigin origin = codec_->getOrigin();
156
157 SkPixmap output_pixmap(info, pixels, row_bytes);
158 SkPixmap temp_pixmap;
159 SkBitmap temp_bitmap;
160 if (origin == kTopLeft_SkEncodedOrigin) {
161 // We can decode directly into the output buffer.
162 temp_pixmap = output_pixmap;
163 } else {
164 // We need to decode into a different buffer so we can re-orient
165 // the pixels later.
166 SkImageInfo temp_info = output_pixmap.info();
168 // We'll be decoding into a buffer that has height and width swapped.
169 temp_info = SkPixmapUtils::SwapWidthHeight(temp_info);
170 }
171 if (!temp_bitmap.tryAllocPixels(temp_info)) {
172 FML_DLOG(ERROR) << "Failed to allocate memory for bitmap of size "
173 << temp_info.computeMinByteSize() << "B";
174 return false;
175 }
176 temp_pixmap = temp_bitmap.pixmap();
177 }
178
179 SkCodec::Result result = codec_->getPixels(temp_pixmap, &options);
180 if (result != SkCodec::kSuccess) {
181 FML_DLOG(WARNING) << "codec could not get pixels. "
183 return false;
184 }
185 if (origin == kTopLeft_SkEncodedOrigin) {
186 return true;
187 }
188 return SkPixmapUtils::Orient(output_pixmap, temp_pixmap, origin);
189}
190
191std::unique_ptr<ImageGenerator> BuiltinSkiaCodecImageGenerator::MakeFromData(
193 auto codec = SkCodec::MakeFromData(std::move(data));
194 if (!codec) {
195 return nullptr;
196 }
197 return std::make_unique<BuiltinSkiaCodecImageGenerator>(std::move(codec));
198}
199
200} // namespace flutter
const char * options
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
kUnpremul_SkAlphaType
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
SkEncodedOrigin
@ kTopLeft_SkEncodedOrigin
static bool SkEncodedOriginSwapsWidthHeight(SkEncodedOrigin origin)
void swap(sk_sp< T > &a, sk_sp< T > &b)
Definition: SkRefCnt.h:341
const SkPixmap & pixmap() const
Definition: SkBitmap.h:133
bool tryAllocPixels(const SkImageInfo &info, size_t rowBytes)
Definition: SkBitmap.cpp:271
static std::unique_ptr< SkCodec > MakeFromData(sk_sp< SkData >, SkSpan< const SkCodecs::Decoder > decoders, SkPngChunkReader *=nullptr)
Definition: SkCodec.cpp:241
static const char * ResultToString(Result)
Definition: SkCodec.cpp:880
Result
Definition: SkCodec.h:76
@ kSuccess
Definition: SkCodec.h:80
SkEncodedOrigin getOrigin() const
Definition: SkCodec.h:246
static constexpr int kNoFrame
Definition: SkCodec.h:650
SkImageInfo getInfo() const
Definition: SkCodec.h:228
const SkImageInfo & info() const
Definition: SkPixmap.h:135
unsigned int GetPlayCount() const override
The number of times an animated image should play through before playback stops.
unsigned int GetFrameCount() const override
Get the number of frames that the encoded image stores. This method is always expected to be called b...
bool GetPixels(const SkImageInfo &info, void *pixels, size_t row_bytes, unsigned int frame_index=0, std::optional< unsigned int > prior_frame=std::nullopt) override
Decode the image into a given buffer. This method is currently always used for sub-pixel image decodi...
const ImageGenerator::FrameInfo GetFrameInfo(unsigned int frame_index) override
Get information about a single frame in the context of a multi-frame image, useful for animation and ...
BuiltinSkiaCodecImageGenerator(std::unique_ptr< SkCodec > codec)
const SkImageInfo & GetInfo() override
Returns basic information about the contents of the encoded image. This information can almost always...
static std::unique_ptr< ImageGenerator > MakeFromData(sk_sp< SkData > data)
SkISize GetScaledDimensions(float desired_scale) override
Given a scale value, find the closest image size that can be used for efficiently decoding the image....
SkISize GetScaledDimensions(float desired_scale) override
Given a scale value, find the closest image size that can be used for efficiently decoding the image....
unsigned int GetPlayCount() const override
The number of times an animated image should play through before playback stops.
const SkImageInfo & GetInfo() override
Returns basic information about the contents of the encoded image. This information can almost always...
const ImageGenerator::FrameInfo GetFrameInfo(unsigned int frame_index) override
Get information about a single frame in the context of a multi-frame image, useful for animation and ...
bool GetPixels(const SkImageInfo &info, void *pixels, size_t row_bytes, unsigned int frame_index=0, std::optional< unsigned int > prior_frame=std::nullopt) override
Decode the image into a given buffer. This method is currently always used for sub-pixel image decodi...
unsigned int GetFrameCount() const override
Get the number of frames that the encoded image stores. This method is always expected to be called b...
BuiltinSkiaImageGenerator(std::unique_ptr< SkImageGenerator > generator)
static std::unique_ptr< ImageGenerator > MakeFromGenerator(std::unique_ptr< SkImageGenerator > generator)
static const unsigned int kInfinitePlayCount
Frame count value to denote infinite looping.
sk_sp< SkImage > GetImage()
Creates an SkImage based on the current ImageInfo of this ImageGenerator.
virtual const SkImageInfo & GetInfo()=0
Returns basic information about the contents of the encoded image. This information can almost always...
virtual bool GetPixels(const SkImageInfo &info, void *pixels, size_t row_bytes, unsigned int frame_index=0, std::optional< unsigned int > prior_frame=std::nullopt)=0
Decode the image into a given buffer. This method is currently always used for sub-pixel image decodi...
GAsyncResult * result
#define FML_DLOG(severity)
Definition: logging.h:102
SK_API sk_sp< SkImage > RasterFromBitmap(const SkBitmap &bitmap)
SK_API bool Orient(const SkPixmap &dst, const SkPixmap &src, SkEncodedOrigin origin)
SK_API SkImageInfo SwapWidthHeight(const SkImageInfo &info)
Definition: bitmap.py:1
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: switches.h:41
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 to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
static SkImageInfo getInfoIncludingExif(SkCodec *codec)
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: ref_ptr.h:256
Definition: SkSize.h:16
size_t computeMinByteSize() const
Definition: SkImageInfo.h:578
Info about a single frame in the context of a multi-frame image, useful for animation and blending.
std::optional< unsigned int > required_frame
#define ERROR(message)
Definition: elf_loader.cc:260