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