Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
image_generator_apng.h
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#ifndef FLUTTER_LIB_UI_PAINTING_IMAGE_GENERATOR_APNG_H_
6#define FLUTTER_LIB_UI_PAINTING_IMAGE_GENERATOR_APNG_H_
7
8#include "image_generator.h"
9
11#include "flutter/fml/logging.h"
12
13#define PNG_FIELD(T, name) \
14 private: \
15 T name; \
16 \
17 public: \
18 T get_##name() const { \
19 return fml::BigEndianToArch<T>(name); \
20 } \
21 void set_##name(T n) { \
22 name = fml::BigEndianToArch<T>(n); \
23 }
24
25namespace flutter {
26
28 public:
30
31 // |ImageGenerator|
32 const SkImageInfo& GetInfo() override;
33
34 // |ImageGenerator|
35 unsigned int GetFrameCount() const override;
36
37 // |ImageGenerator|
38 unsigned int GetPlayCount() const override;
39
40 // |ImageGenerator|
42 unsigned int frame_index) override;
43
44 // |ImageGenerator|
45 SkISize GetScaledDimensions(float desired_scale) override;
46
47 // |ImageGenerator|
48 bool GetPixels(const SkImageInfo& info,
49 void* pixels,
50 size_t row_bytes,
51 unsigned int frame_index,
52 std::optional<unsigned int> prior_frame) override;
53
54 static std::unique_ptr<ImageGenerator> MakeFromData(sk_sp<SkData> data);
55
56 /// Computes the CRC of the data in a PNG chunk.
57 static uint32_t ComputeCrc32(const uint8_t* data, size_t length);
58
59 private:
60 static constexpr uint8_t kPngSignature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
61 static constexpr size_t kChunkCrcSize = 4;
62
63 /// The size of the sequence number at the beginning of an fdAT chunk.
64 static constexpr size_t kFrameDataSequenceNumberSize = 4;
65
66 enum ChunkType {
67 kImageHeaderChunkType = 'IHDR',
68 kAnimationControlChunkType = 'acTL',
69 kImageDataChunkType = 'IDAT',
70 kFrameControlChunkType = 'fcTL',
71 kFrameDataChunkType = 'fdAT',
72 kImageTrailerChunkType = 'IEND',
73 };
74
75 class __attribute__((packed, aligned(1))) ChunkHeader {
76 PNG_FIELD(uint32_t, data_length)
77 PNG_FIELD(ChunkType, type)
78
79 public:
80 void UpdateChunkCrc32();
81
82 private:
83 uint32_t ComputeChunkCrc32();
84 };
85
86 class __attribute__((packed, aligned(1))) ImageHeaderChunkData {
87 PNG_FIELD(uint32_t, width)
88 PNG_FIELD(uint32_t, height)
89 PNG_FIELD(uint8_t, bit_depth)
90 PNG_FIELD(uint8_t, color_type)
91 PNG_FIELD(uint8_t, compression_method)
92 PNG_FIELD(uint8_t, filter_method)
93 PNG_FIELD(uint8_t, interlace_method)
94 };
95
96 class __attribute__((packed, aligned(1))) AnimationControlChunkData {
97 PNG_FIELD(uint32_t, num_frames)
98 PNG_FIELD(uint32_t, num_plays)
99 };
100
101 class __attribute__((packed, aligned(1))) FrameControlChunkData {
102 PNG_FIELD(uint32_t, sequence_number)
103 PNG_FIELD(uint32_t, width)
104 PNG_FIELD(uint32_t, height)
105 PNG_FIELD(uint32_t, x_offset)
106 PNG_FIELD(uint32_t, y_offset)
107 PNG_FIELD(uint16_t, delay_num)
108 PNG_FIELD(uint16_t, delay_den)
109 PNG_FIELD(uint8_t, dispose_op)
110 PNG_FIELD(uint8_t, blend_op)
111 };
112
113 /// @brief The first PNG frame is always the "default" PNG frame. Absence of
114 /// `frame_info` is only possible on the "default" PNG frame.
115 /// Each frame goes through two decoding stages:
116 /// 1. Demuxing stage: An individual PNG codec is created for a frame
117 /// while walking through the APNG chunk stream -- this is placed
118 /// in the `codec` field.
119 /// 2. Decoding stage: When a frame is requested for the first time,
120 /// the decoded image is requested from the `SkCodec` and then
121 /// (depending on the `frame_info`) composited with a previous
122 /// frame. The final "canvas" frame is placed in the
123 /// `composited_image` field. At this point, the `codec` is freed
124 /// and the `composited_image` is handed to the caller for drawing.
125 struct APNGImage {
126 std::unique_ptr<SkCodec> codec;
127
128 // The rendered frame pixels.
129 std::vector<uint8_t> pixels;
130
131 // Absence of frame info is possible on the "default" image.
132 std::optional<ImageGenerator::FrameInfo> frame_info;
133
134 // X offset of this image when composited. Only applicable to frames.
135 unsigned int x_offset;
136
137 // Y offset of this image when composited. Only applicable to frames.
138 unsigned int y_offset;
139 };
140
141 APNGImageGenerator(sk_sp<SkData>& data,
142 SkImageInfo& image_info,
143 APNGImage&& default_image,
144 unsigned int frame_count,
145 unsigned int play_count,
146 const void* next_chunk_p,
147 const std::vector<uint8_t>& header);
148
149 static bool IsValidChunkHeader(const void* buffer,
150 size_t size,
151 const ChunkHeader* chunk);
152
153 static const ChunkHeader* GetNextChunk(const void* buffer,
154 size_t size,
155 const ChunkHeader* current_chunk);
156
157 /// @brief This is a utility template for casting a png buffer pointer to a
158 /// chunk header. Its primary purpose is to statically insert runtime
159 /// debug checks that detect invalid decoding behavior.
160 template <typename T>
161 static constexpr const T* CastChunkData(const ChunkHeader* chunk) {
162 if constexpr (std::is_same_v<T, ImageHeaderChunkData>) {
163 FML_DCHECK(chunk->get_type() == kImageHeaderChunkType);
164 } else if constexpr (std::is_same_v<T, AnimationControlChunkData>) {
165 FML_DCHECK(chunk->get_type() == kAnimationControlChunkType);
166 } else if constexpr (std::is_same_v<T, FrameControlChunkData>) {
167 FML_DCHECK(chunk->get_type() == kFrameControlChunkType);
168 } else {
169 static_assert(!sizeof(T), "Invalid chunk struct");
170 }
171
172 return reinterpret_cast<const T*>(reinterpret_cast<const uint8_t*>(chunk) +
173 sizeof(ChunkHeader));
174 }
175
176 static constexpr size_t GetChunkSize(const ChunkHeader* chunk) {
177 return sizeof(ChunkHeader) + chunk->get_data_length() + kChunkCrcSize;
178 }
179
180 static constexpr bool IsChunkCopySafe(const ChunkHeader* chunk) {
181 // The safe-to-copy bit is the 5th bit of the chunk name's 4th byte. This is
182 // the same as checking that the 4th byte is lowercase.
183 return (chunk->get_type() & 0x20) != 0;
184 }
185
186 /// @brief Extract a header that's safe to use for both the "default" image
187 /// and individual PNG frames. Strip the animation control chunk.
188 static std::pair<std::optional<std::vector<uint8_t>>, const void*>
189 ExtractHeader(const void* buffer_p, size_t buffer_size);
190
191 /// @brief Takes a chunk pointer to a chunk and demuxes/interprets the next
192 /// image in the APNG sequence. It also provides the next `chunk_p`
193 /// to use.
194 /// @see `APNGImage`
195 static std::pair<std::optional<APNGImage>, const void*> DemuxNextImage(
196 const void* buffer_p,
197 size_t buffer_size,
198 const std::vector<uint8_t>& header,
199 const void* chunk_p);
200
201 bool DemuxNextImageInternal();
202
203 bool DemuxToImageIndex(unsigned int image_index);
204
205 bool RenderDefaultImage(const SkImageInfo& info,
206 void* pixels,
207 size_t row_bytes);
208
209 FML_DISALLOW_COPY_ASSIGN_AND_MOVE(APNGImageGenerator);
210 sk_sp<SkData> data_;
211 SkImageInfo image_info_;
212 unsigned int frame_count_;
213 unsigned int play_count_;
214
215 // The first image is always the default image, which may or may not be a
216 // frame. All subsequent images are guaranteed to have frame data.
217 std::vector<APNGImage> images_;
218
219 unsigned int first_frame_index_;
220
221 const void* next_chunk_p_;
222 std::vector<uint8_t> header_;
223};
224
225} // namespace flutter
226
227#endif // FLUTTER_LIB_UI_PAINTING_IMAGE_GENERATOR_APNG_H_
unsigned int GetPlayCount() const override
The number of times an animated image should play through before playback stops.
static uint32_t ComputeCrc32(const uint8_t *data, size_t length)
Computes the CRC of the data in a PNG chunk.
unsigned int GetFrameCount() const override
Get the number of frames that the encoded image stores. This method is always expected to be called b...
const SkImageInfo & GetInfo() override
Returns basic information about the contents of the encoded image. This information can almost always...
SkISize GetScaledDimensions(float desired_scale) override
Given a scale value, find the closest image size that can be used for efficiently decoding the image....
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 ...
static std::unique_ptr< ImageGenerator > MakeFromData(sk_sp< SkData > data)
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 decodi...
The minimal interface necessary for defining a decoder that can be used for both single and multi-fra...
#define FML_DCHECK(condition)
Definition logging.h:122
#define FML_DISALLOW_COPY_ASSIGN_AND_MOVE(TypeName)
Definition macros.h:31
#define PNG_FIELD(T, name)
size_t length
__attribute__((visibility("default"))) int RunBenchmarks(int argc
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
Definition switch_defs.h:36
impeller::ShaderType type
uint32_t color_type
int32_t height
int32_t width
Info about a single frame in the context of a multi-frame image, useful for animation and blending.