10#include "third_party/skia/include/codec/SkEncodedOrigin.h"
11#include "third_party/skia/include/codec/SkPixmapUtils.h"
12#include "third_party/skia/include/core/SkBitmap.h"
13#include "third_party/skia/include/core/SkImage.h"
23 if (!bitmap.tryAllocPixels(info)) {
24 FML_DLOG(ERROR) <<
"Failed to allocate memory for bitmap of size "
25 << info.computeMinByteSize() <<
"B";
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.";
34 bitmap.setImmutable();
35 return SkImages::RasterFromBitmap(bitmap);
41 std::unique_ptr<SkImageGenerator> generator)
42 : generator_(
std::move(generator)) {}
45 return generator_->getInfo();
57 unsigned int frame_index) {
60 .disposal_method = SkCodecAnimation::DisposalMethod::kKeep};
64 return generator_->getInfo().dimensions();
68 const SkImageInfo& info,
71 unsigned int frame_index,
72 std::optional<unsigned int> prior_frame) {
73 return generator_->getPixels(info, pixels, row_bytes);
77 std::unique_ptr<SkImageGenerator> generator) {
81 return std::make_unique<BuiltinSkiaImageGenerator>(std::move(generator));
87 SkImageInfo info = codec->getInfo();
88 if (SkEncodedOriginSwapsWidthHeight(codec->getOrigin())) {
89 info = SkPixmapUtils::SwapWidthHeight(info);
91 if (kUnpremul_SkAlphaType == info.alphaType()) {
93 info = info.makeAlphaType(kPremul_SkAlphaType);
99 std::unique_ptr<SkCodec> codec)
100 : codec_(
std::move(codec)) {
105 sk_sp<SkData> buffer)
106 : codec_(SkCodec::MakeFromData(
std::move(
buffer)).release()) {
115 return codec_->getFrameCount();
119 auto repetition_count = codec_->getRepetitionCount();
124 unsigned int frame_index) {
125 SkCodec::FrameInfo info = {};
126 codec_->getFrameInfo(frame_index, &info);
128 .required_frame = info.fRequiredFrame == SkCodec::kNoFrame
130 : std::optional<unsigned int>(info.fRequiredFrame),
131 .duration =
static_cast<unsigned int>(info.fDuration),
132 .disposal_method = info.fDisposalMethod};
136 float desired_scale) {
137 SkISize
size = codec_->getScaledDimensions(desired_scale);
138 if (SkEncodedOriginSwapsWidthHeight(codec_->getOrigin())) {
139 std::swap(
size.fWidth,
size.fHeight);
145 const SkImageInfo& info,
148 unsigned int frame_index,
149 std::optional<unsigned int> prior_frame) {
150 SkCodec::Options options;
151 options.fFrameIndex = frame_index;
152 if (prior_frame.has_value()) {
153 options.fPriorFrame = prior_frame.value();
155 SkEncodedOrigin origin = codec_->getOrigin();
157 SkPixmap output_pixmap(info, pixels, row_bytes);
158 SkPixmap temp_pixmap;
159 SkBitmap temp_bitmap;
160 if (origin == kTopLeft_SkEncodedOrigin) {
162 temp_pixmap = output_pixmap;
166 SkImageInfo temp_info = output_pixmap.info();
167 if (SkEncodedOriginSwapsWidthHeight(origin)) {
169 temp_info = SkPixmapUtils::SwapWidthHeight(temp_info);
171 if (!temp_bitmap.tryAllocPixels(temp_info)) {
172 FML_DLOG(ERROR) <<
"Failed to allocate memory for bitmap of size "
173 << temp_info.computeMinByteSize() <<
"B";
176 temp_pixmap = temp_bitmap.pixmap();
179 SkCodec::Result result = codec_->getPixels(temp_pixmap, &options);
180 if (result != SkCodec::kSuccess) {
181 FML_DLOG(WARNING) <<
"codec could not get pixels. "
182 << SkCodec::ResultToString(result);
185 if (origin == kTopLeft_SkEncodedOrigin) {
188 return SkPixmapUtils::Orient(output_pixmap, temp_pixmap, origin);
192 sk_sp<SkData>
data) {
193 auto codec = SkCodec::MakeFromData(std::move(
data));
197 return std::make_unique<BuiltinSkiaCodecImageGenerator>(std::move(codec));
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()
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)
~BuiltinSkiaImageGenerator()
virtual ~ImageGenerator()
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...
#define FML_DLOG(severity)
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
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
static SkImageInfo getInfoIncludingExif(SkCodec *codec)
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 disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set profile Make the profiler discard new samples once the profiler sample buffer is full When this flag is not the profiler sample buffer is used as a ring buffer
Info about a single frame in the context of a multi-frame image, useful for animation and blending.
std::optional< unsigned int > required_frame