Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Public Member Functions | Static Public Member Functions | List of all members
flutter::APNGImageGenerator Class Reference

#include <image_generator_apng.h>

Inheritance diagram for flutter::APNGImageGenerator:
flutter::ImageGenerator

Public Member Functions

 ~APNGImageGenerator ()
 
const SkImageInfoGetInfo () override
 Returns basic information about the contents of the encoded image. This information can almost always be collected by just interpreting the header of a decoded image.
 
unsigned int GetFrameCount () const override
 Get the number of frames that the encoded image stores. This method is always expected to be called before GetFrameInfo, as the underlying image decoder may interpret frame information that is then used when calling GetFrameInfo.
 
unsigned int GetPlayCount () const override
 The number of times an animated image should play through before playback stops.
 
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 frame blending. This method should only ever be called after GetFrameCount has been called. This information is nonsensical for single-frame images.
 
SkISize GetScaledDimensions (float desired_scale) override
 Given a scale value, find the closest image size that can be used for efficiently decoding the image. If subpixel image decoding is not supported by the decoder, this method should just return the original image size.
 
bool GetPixels (const SkImageInfo &info, void *pixels, size_t row_bytes, unsigned int frame_index, std::optional< unsigned int > prior_frame) override
 Decode the image into a given buffer. This method is currently always used for sub-pixel image decoding. For full-sized still images, GetImage is always attempted first.
 
- Public Member Functions inherited from flutter::ImageGenerator
virtual ~ImageGenerator ()
 
sk_sp< SkImageGetImage ()
 Creates an SkImage based on the current ImageInfo of this ImageGenerator.
 

Static Public Member Functions

static std::unique_ptr< ImageGeneratorMakeFromData (sk_sp< SkData > data)
 

Additional Inherited Members

- Static Public Attributes inherited from flutter::ImageGenerator
static const unsigned int kInfinitePlayCount
 Frame count value to denote infinite looping.
 

Detailed Description

Definition at line 27 of file image_generator_apng.h.

Constructor & Destructor Documentation

◆ ~APNGImageGenerator()

flutter::APNGImageGenerator::~APNGImageGenerator ( )
default

Member Function Documentation

◆ GetFrameCount()

unsigned int flutter::APNGImageGenerator::GetFrameCount ( ) const
overridevirtual

Get the number of frames that the encoded image stores. This method is always expected to be called before GetFrameInfo, as the underlying image decoder may interpret frame information that is then used when calling GetFrameInfo.

Returns
The number of frames that the encoded image stores. This will always be 1 for single-frame images.

Implements flutter::ImageGenerator.

Definition at line 43 of file image_generator_apng.cc.

43 {
44 return frame_count_;
45}

◆ GetFrameInfo()

const ImageGenerator::FrameInfo flutter::APNGImageGenerator::GetFrameInfo ( unsigned int  frame_index)
overridevirtual

Get information about a single frame in the context of a multi-frame image, useful for animation and frame blending. This method should only ever be called after GetFrameCount has been called. This information is nonsensical for single-frame images.

Parameters
[in]frame_indexThe index of the frame to get information about.
Returns
Information about the given frame. If the image is single-frame, a default result is returned.
See also
GetFrameCount

Implements flutter::ImageGenerator.

Definition at line 51 of file image_generator_apng.cc.

52 {
53 unsigned int image_index = first_frame_index_ + frame_index;
54 if (!DemuxToImageIndex(image_index)) {
55 return {};
56 }
57
58 auto frame_info = images_[image_index].frame_info;
59 if (frame_info.has_value()) {
60 return frame_info.value();
61 }
62 return {};
63}

◆ GetInfo()

const SkImageInfo & flutter::APNGImageGenerator::GetInfo ( )
overridevirtual

Returns basic information about the contents of the encoded image. This information can almost always be collected by just interpreting the header of a decoded image.

Returns
Size and color information describing the image.
Note
This method is executed on the UI thread and used for layout purposes by the framework, and so this method should not perform long synchronous tasks.

Implements flutter::ImageGenerator.

Definition at line 39 of file image_generator_apng.cc.

39 {
40 return image_info_;
41}

◆ GetPixels()

bool flutter::APNGImageGenerator::GetPixels ( const SkImageInfo info,
void *  pixels,
size_t  row_bytes,
unsigned int  frame_index,
std::optional< unsigned int prior_frame 
)
overridevirtual

Decode the image into a given buffer. This method is currently always used for sub-pixel image decoding. For full-sized still images, GetImage is always attempted first.

Parameters
[in]infoThe desired size and color info of the decoded image to be returned. The implementation of GetScaledDimensions determines which sizes are supported by the image decoder.
[in]pixelsThe location where the raw decoded image data should be written.
[in]row_bytesThe total number of bytes that should make up a single row of decoded image data (i.e. width * bytes_per_pixel).
[in]frame_indexWhich frame to decode. This is only useful for multi-frame images.
[in]prior_frameOptional frame index parameter for multi-frame images which specifies the previous frame that should be use for blending. This hints to the decoder that it should use a previously cached frame instead of decoding dependency frame(s). If an empty value is supplied, the decoder should decode any necessary frames first.
Returns
True if the image was successfully decoded.
Note
This method performs potentially long synchronous work, and so it should never be executed on the UI thread. Image decoders do not require GPU acceleration, and so threads without a GPU context may also be used.
See also
GetScaledDimensions
  1. Demux the frame from the APNG stream.
  1. Decode the frame.
  1. Composite the frame onto the canvas.

Implements flutter::ImageGenerator.

Definition at line 69 of file image_generator_apng.cc.

73 {
74 FML_DCHECK(images_.size() > 0);
75 unsigned int image_index = first_frame_index_ + frame_index;
76
77 //----------------------------------------------------------------------------
78 /// 1. Demux the frame from the APNG stream.
79 ///
80
81 if (!DemuxToImageIndex(image_index)) {
82 FML_DLOG(ERROR) << "Couldn't demux image at index " << image_index
83 << " (frame index: " << frame_index
84 << ") from APNG stream.";
85 return RenderDefaultImage(info, pixels, row_bytes);
86 }
87
88 //----------------------------------------------------------------------------
89 /// 2. Decode the frame.
90 ///
91
92 APNGImage& frame = images_[image_index];
93 SkImageInfo frame_info = frame.codec->getInfo();
94 auto frame_row_bytes = frame_info.bytesPerPixel() * frame_info.width();
95
96 if (frame.pixels.empty()) {
97 frame.pixels.resize(frame_row_bytes * frame_info.height());
98 SkCodec::Result result = frame.codec->getPixels(
99 frame.codec->getInfo(), frame.pixels.data(), frame_row_bytes);
100 if (result != SkCodec::kSuccess) {
101 FML_DLOG(ERROR) << "Failed to decode image at index " << image_index
102 << " (frame index: " << frame_index
103 << ") of APNG. SkCodec::Result: " << result;
104 return RenderDefaultImage(info, pixels, row_bytes);
105 }
106 }
107 if (!frame.frame_info.has_value()) {
108 FML_DLOG(ERROR) << "Failed to decode image at index " << image_index
109 << " (frame index: " << frame_index
110 << ") of APNG due to the frame missing data (frame_info).";
111 return false;
112 }
113
114 //----------------------------------------------------------------------------
115 /// 3. Composite the frame onto the canvas.
116 ///
117
118 if (info.colorType() != kN32_SkColorType) {
119 FML_DLOG(ERROR) << "Failed to composite image at index " << image_index
120 << " (frame index: " << frame_index
121 << ") of APNG due to the destination surface having an "
122 "unsupported color type.";
123 return false;
124 }
125 if (frame_info.colorType() != kN32_SkColorType) {
127 << "Failed to composite image at index " << image_index
128 << " (frame index: " << frame_index
129 << ") of APNG due to the frame having an unsupported color type.";
130 return false;
131 }
132
133 // Regardless of the byte order (RGBA vs BGRA), the blending operations are
134 // the same.
135 struct Pixel {
136 uint8_t channel[4];
137
138 uint8_t GetAlpha() { return channel[3]; }
139
140 void Premultiply() {
141 for (int i = 0; i < 3; i++) {
142 channel[i] = channel[i] * GetAlpha() / 0xFF;
143 }
144 }
145
146 void Unpremultiply() {
147 if (GetAlpha() == 0) {
148 channel[0] = channel[1] = channel[2] = 0;
149 return;
150 }
151 for (int i = 0; i < 3; i++) {
152 channel[i] = channel[i] * 0xFF / GetAlpha();
153 }
154 }
155 };
156
157 FML_DCHECK(frame_info.bytesPerPixel() == sizeof(Pixel));
158
159 bool result = true;
160
161 if (frame.frame_info->blend_mode == SkCodecAnimation::Blend::kSrc) {
162 SkPixmap src_pixmap(frame_info, frame.pixels.data(), frame_row_bytes);
163 uint8_t* dst_pixels = static_cast<uint8_t*>(pixels) +
164 frame.y_offset * row_bytes +
165 frame.x_offset * frame_info.bytesPerPixel();
166 result = src_pixmap.readPixels(info, dst_pixels, row_bytes);
167 if (!result) {
168 FML_DLOG(ERROR) << "Failed to copy pixels at index " << image_index
169 << " (frame index: " << frame_index << ") of APNG.";
170 }
171 } else if (frame.frame_info->blend_mode ==
173 for (int y = 0; y < frame_info.height(); y++) {
174 auto src_row = frame.pixels.data() + y * frame_row_bytes;
175 auto dst_row = static_cast<uint8_t*>(pixels) +
176 (y + frame.y_offset) * row_bytes +
177 frame.x_offset * frame_info.bytesPerPixel();
178
179 for (int x = 0; x < frame_info.width(); x++) {
180 auto x_offset_bytes = x * frame_info.bytesPerPixel();
181
182 Pixel src = *reinterpret_cast<Pixel*>(src_row + x_offset_bytes);
183 Pixel* dst_p = reinterpret_cast<Pixel*>(dst_row + x_offset_bytes);
184 Pixel dst = *dst_p;
185
186 // Ensure both colors are premultiplied for the blending operation.
187 if (info.alphaType() == kUnpremul_SkAlphaType) {
188 dst.Premultiply();
189 }
190 if (frame_info.alphaType() == kUnpremul_SkAlphaType) {
191 src.Premultiply();
192 }
193
194 for (int i = 0; i < 4; i++) {
195 dst.channel[i] =
196 src.channel[i] + dst.channel[i] * (0xFF - src.GetAlpha()) / 0xFF;
197 }
198
199 // The final color is premultiplied. Unpremultiply to match the
200 // backdrop surface if necessary.
201 if (info.alphaType() == kUnpremul_SkAlphaType) {
202 dst.Unpremultiply();
203 }
204
205 *dst_p = dst;
206 }
207 }
208 }
209
210 return result;
211}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
kUnpremul_SkAlphaType
@ kSuccess
Definition SkCodec.h:80
double frame
Definition examples.cpp:31
GAsyncResult * result
#define FML_DLOG(severity)
Definition logging.h:102
#define FML_DCHECK(condition)
Definition logging.h:103
double y
double x
dst
Definition cp.py:12
int bytesPerPixel() const
int width() const
SkAlphaType alphaType() const
SkColorType colorType() const
int height() const
#define ERROR(message)

◆ GetPlayCount()

unsigned int flutter::APNGImageGenerator::GetPlayCount ( ) const
overridevirtual

The number of times an animated image should play through before playback stops.

Returns
If this image is animated, the number of times the animation should play through is returned, otherwise it'll just return 1. If the animation should loop forever, kInfinitePlayCount is returned.

Implements flutter::ImageGenerator.

Definition at line 47 of file image_generator_apng.cc.

47 {
48 return frame_count_ > 1 ? play_count_ : 1;
49}

◆ GetScaledDimensions()

SkISize flutter::APNGImageGenerator::GetScaledDimensions ( float  scale)
overridevirtual

Given a scale value, find the closest image size that can be used for efficiently decoding the image. If subpixel image decoding is not supported by the decoder, this method should just return the original image size.

Parameters
[in]scaleThe desired scale factor of the image for decoding.
Returns
The closest image size that can be used for efficiently decoding the image.
Note
This method is called prior to GetPixels in order to query for supported sizes.
See also
GetPixels

Implements flutter::ImageGenerator.

Definition at line 65 of file image_generator_apng.cc.

65 {
66 return image_info_.dimensions();
67}
SkISize dimensions() const

◆ MakeFromData()

std::unique_ptr< ImageGenerator > flutter::APNGImageGenerator::MakeFromData ( sk_sp< SkData data)
static

Definition at line 213 of file image_generator_apng.cc.

214 {
215 // Ensure the buffer is large enough to at least contain the PNG signature
216 // and a chunk header.
217 if (data->size() < sizeof(kPngSignature) + sizeof(ChunkHeader)) {
218 return nullptr;
219 }
220 // Validate the full PNG signature.
221 const uint8_t* data_p = static_cast<const uint8_t*>(data.get()->data());
222 if (memcmp(data_p, kPngSignature, sizeof(kPngSignature))) {
223 return nullptr;
224 }
225
226 // Validate the header chunk.
227 const ChunkHeader* chunk = reinterpret_cast<const ChunkHeader*>(data_p + 8);
228 if (!IsValidChunkHeader(data_p, data->size(), chunk) ||
229 chunk->get_data_length() != sizeof(ImageHeaderChunkData) ||
230 chunk->get_type() != kImageHeaderChunkType) {
231 return nullptr;
232 }
233
234 // Walk the chunks to find the "animation control" chunk. If an "image data"
235 // chunk is found first, this PNG is not animated.
236 while (true) {
237 chunk = GetNextChunk(data_p, data->size(), chunk);
238
239 if (chunk == nullptr) {
240 return nullptr;
241 }
242 if (chunk->get_type() == kImageDataChunkType) {
243 return nullptr;
244 }
245 if (chunk->get_type() == kAnimationControlChunkType) {
246 break;
247 }
248 }
249
250 const AnimationControlChunkData* animation_data =
251 CastChunkData<AnimationControlChunkData>(chunk);
252
253 // Extract the header signature and chunks to prepend when demuxing images.
254 std::optional<std::vector<uint8_t>> header;
255 const void* first_chunk_p;
256 std::tie(header, first_chunk_p) = ExtractHeader(data_p, data->size());
257 if (!header.has_value()) {
258 return nullptr;
259 }
260
261 // Demux the first image in the APNG chunk stream in order to interpret
262 // extent and blending info immediately.
263 std::optional<APNGImage> default_image;
264 const void* next_chunk_p;
265 std::tie(default_image, next_chunk_p) =
266 DemuxNextImage(data_p, data->size(), header.value(), first_chunk_p);
267 if (!default_image.has_value()) {
268 return nullptr;
269 }
270
271 unsigned int play_count = animation_data->get_num_plays();
272 if (play_count == 0) {
273 play_count = kInfinitePlayCount;
274 }
275
276 SkImageInfo image_info = default_image.value().codec->getInfo();
277 return std::unique_ptr<APNGImageGenerator>(
278 new APNGImageGenerator(data, image_info, std::move(default_image.value()),
279 animation_data->get_num_frames(), play_count,
280 next_chunk_p, header.value()));
281}
static const unsigned int kInfinitePlayCount
Frame count value to denote infinite looping.
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
static const char header[]
Definition skpbench.cpp:88

The documentation for this class was generated from the following files: