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 /// Signature at the start of a PNG file.
60 static constexpr std::array<uint8_t, 8> kPngSignature = {137, 80, 78, 71,
61 13, 10, 26, 10};
62
63 private:
64 static constexpr size_t kChunkCrcSize = 4;
65
66 /// The size of the sequence number at the beginning of an fdAT chunk.
67 static constexpr size_t kFrameDataSequenceNumberSize = 4;
68
69 enum ChunkType {
70 kImageHeaderChunkType = 'IHDR',
71 kAnimationControlChunkType = 'acTL',
72 kImageDataChunkType = 'IDAT',
73 kFrameControlChunkType = 'fcTL',
74 kFrameDataChunkType = 'fdAT',
75 kImageTrailerChunkType = 'IEND',
76 };
77
78 class __attribute__((packed, aligned(1))) ChunkHeader {
79 PNG_FIELD(uint32_t, data_length)
80 PNG_FIELD(ChunkType, type)
81
82 public:
83 void UpdateChunkCrc32();
84
85 private:
86 uint32_t ComputeChunkCrc32();
87 };
88
89 class __attribute__((packed, aligned(1))) ImageHeaderChunkData {
90 PNG_FIELD(uint32_t, width)
91 PNG_FIELD(uint32_t, height)
92 PNG_FIELD(uint8_t, bit_depth)
93 PNG_FIELD(uint8_t, color_type)
94 PNG_FIELD(uint8_t, compression_method)
95 PNG_FIELD(uint8_t, filter_method)
96 PNG_FIELD(uint8_t, interlace_method)
97 };
98
99 class __attribute__((packed, aligned(1))) AnimationControlChunkData {
100 PNG_FIELD(uint32_t, num_frames)
101 PNG_FIELD(uint32_t, num_plays)
102 };
103
104 class __attribute__((packed, aligned(1))) FrameControlChunkData {
105 PNG_FIELD(uint32_t, sequence_number)
106 PNG_FIELD(uint32_t, width)
107 PNG_FIELD(uint32_t, height)
108 PNG_FIELD(uint32_t, x_offset)
109 PNG_FIELD(uint32_t, y_offset)
110 PNG_FIELD(uint16_t, delay_num)
111 PNG_FIELD(uint16_t, delay_den)
112 PNG_FIELD(uint8_t, dispose_op)
113 PNG_FIELD(uint8_t, blend_op)
114 };
115
116 /// @brief The first PNG frame is always the "default" PNG frame. Absence of
117 /// `frame_info` is only possible on the "default" PNG frame.
118 /// Each frame goes through two decoding stages:
119 /// 1. Demuxing stage: An individual PNG codec is created for a frame
120 /// while walking through the APNG chunk stream -- this is placed
121 /// in the `codec` field.
122 /// 2. Decoding stage: When a frame is requested for the first time,
123 /// the decoded image is requested from the `SkCodec` and then
124 /// (depending on the `frame_info`) composited with a previous
125 /// frame. The final "canvas" frame is placed in the
126 /// `composited_image` field. At this point, the `codec` is freed
127 /// and the `composited_image` is handed to the caller for drawing.
128 struct APNGImage {
129 std::unique_ptr<SkCodec> codec;
130
131 // The rendered frame pixels.
132 std::vector<uint8_t> pixels;
133
134 // Absence of frame info is possible on the "default" image.
135 std::optional<ImageGenerator::FrameInfo> frame_info;
136
137 // X offset of this image when composited. Only applicable to frames.
138 unsigned int x_offset;
139
140 // Y offset of this image when composited. Only applicable to frames.
141 unsigned int y_offset;
142 };
143
144 APNGImageGenerator(sk_sp<SkData>& data,
145 SkImageInfo& image_info,
146 APNGImage&& default_image,
147 unsigned int frame_count,
148 unsigned int play_count,
149 const void* next_chunk_p,
150 const std::vector<uint8_t>& header);
151
152 static bool IsValidChunkHeader(const void* buffer,
153 size_t size,
154 const ChunkHeader* chunk);
155
156 static const ChunkHeader* GetNextChunk(const void* buffer,
157 size_t size,
158 const ChunkHeader* current_chunk);
159
160 /// @brief This is a utility template for casting a png buffer pointer to a
161 /// chunk header. Its primary purpose is to statically insert runtime
162 /// debug checks that detect invalid decoding behavior.
163 template <typename T>
164 static constexpr const T* CastChunkData(const ChunkHeader* chunk) {
165 if constexpr (std::is_same_v<T, ImageHeaderChunkData>) {
166 FML_DCHECK(chunk->get_type() == kImageHeaderChunkType);
167 } else if constexpr (std::is_same_v<T, AnimationControlChunkData>) {
168 FML_DCHECK(chunk->get_type() == kAnimationControlChunkType);
169 } else if constexpr (std::is_same_v<T, FrameControlChunkData>) {
170 FML_DCHECK(chunk->get_type() == kFrameControlChunkType);
171 } else {
172 static_assert(!sizeof(T), "Invalid chunk struct");
173 }
174
175 return reinterpret_cast<const T*>(reinterpret_cast<const uint8_t*>(chunk) +
176 sizeof(ChunkHeader));
177 }
178
179 static constexpr size_t GetChunkSize(const ChunkHeader* chunk) {
180 return sizeof(ChunkHeader) + chunk->get_data_length() + kChunkCrcSize;
181 }
182
183 static constexpr bool IsChunkCopySafe(const ChunkHeader* chunk) {
184 // The safe-to-copy bit is the 5th bit of the chunk name's 4th byte. This is
185 // the same as checking that the 4th byte is lowercase.
186 return (chunk->get_type() & 0x20) != 0;
187 }
188
189 /// @brief Extract a header that's safe to use for both the "default" image
190 /// and individual PNG frames. Strip the animation control chunk.
191 static std::pair<std::optional<std::vector<uint8_t>>, const void*>
192 ExtractHeader(const void* buffer_p, size_t buffer_size);
193
194 /// @brief Takes a chunk pointer to a chunk and demuxes/interprets the next
195 /// image in the APNG sequence. It also provides the next `chunk_p`
196 /// to use.
197 /// @see `APNGImage`
198 static std::pair<std::optional<APNGImage>, const void*> DemuxNextImage(
199 const void* buffer_p,
200 size_t buffer_size,
201 const std::vector<uint8_t>& header,
202 const void* chunk_p);
203
204 bool DemuxNextImageInternal();
205
206 bool DemuxToImageIndex(unsigned int image_index);
207
208 bool RenderDefaultImage(const SkImageInfo& info,
209 void* pixels,
210 size_t row_bytes);
211
212 FML_DISALLOW_COPY_ASSIGN_AND_MOVE(APNGImageGenerator);
213 sk_sp<SkData> data_;
214 SkImageInfo image_info_;
215 unsigned int frame_count_;
216 unsigned int play_count_;
217
218 // The first image is always the default image, which may or may not be a
219 // frame. All subsequent images are guaranteed to have frame data.
220 std::vector<APNGImage> images_;
221
222 unsigned int first_frame_index_;
223
224 const void* next_chunk_p_;
225 std::vector<uint8_t> header_;
226};
227
228} // namespace flutter
229
230#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...
static constexpr std::array< uint8_t, 8 > kPngSignature
Signature at the start of a PNG file.
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.