Debian Codegraph @

src/codec/SkPngRustCodec.h

raw
1 /*
2 * Copyright 2024 Google LLC.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7 #ifndef SkPngRustCodec_DEFINED
8 #define SkPngRustCodec_DEFINED
9
10 #include <memory>
11 #include <vector>
12
13 #include "rust/png/FFI.rs.h"
14 #include "src/codec/SkFrameHolder.h"
15 #include "src/codec/SkPngCodecBase.h"
16 #include "third_party/rust/cxx/v1/cxx.h"
17
18 struct SkEncodedInfo;
19 class SkFrame;
20 class SkStream;
21 template <typename T> class SkSpan;
22
23 // This class provides the Skia image decoding API (`SkCodec`) on top of:
24 // * The third-party `png` crate (PNG decompression and decoding implemented in
25 // Rust)
26 // * Skia's `SkSwizzler` and `skcms_Transform` (pixel format and color space
27 // transformations implemented in C++).
28 class SkPngRustCodec final : public SkPngCodecBase {
29 public:
30 static std::unique_ptr<SkPngRustCodec> MakeFromStream(std::unique_ptr<SkStream>, Result*);
31
32 // `public` to support `std::make_unique<SkPngRustCodec>(...)`.
33 SkPngRustCodec(SkEncodedInfo&&, std::unique_ptr<SkStream>, rust::Box<rust_png::Reader>);
34
35 ~SkPngRustCodec() override;
36
37 private:
38 struct DecodingDstInfo {
39 // `fDst` is based on `pixels` passed to `onGetPixels` or
40 // `onStartIncrementalDecode`. For interlaced and non-interlaced
41 // images, `startDecoding` initializes `fDst` to start at the (0,0)
42 // (top-left) pixel of the current frame (which may be offset from
43 // `pixels` if the current frame is a sub-rect of the full image).
44 // After decoding a non-interlaced row this moves (by `fDstRowStride`)
45 // to the next row.
46 SkSpan<uint8_t> fDst;
47
48 // Size of a row (in bytes) in the full image. Based on `rowBytes`
49 // passed to `onGetPixels` or `onStartIncrementalDecode`.
50 size_t fDstRowStride = 0;
51
52 // Size of a row (in bytes) in the current frame.
53 size_t fDstRowSize = 0;
54
55 // Bytes per pixel of fDst.
56 uint8_t fDstBytesPerPixel = 0;
57 };
58
59 struct DecodingState {
60 // The info and pixels we will be decoding into.
61 DecodingDstInfo fDecodingDstInfo;
62
63 // Intermediate buffer that holds color-transformed pixels that are
64 // ready to be blended with the destination. Used only when this frame
65 // uses `SkCodecAnimation::Blend::kSrcOver`. For interlaced images this
66 // buffer holds the whole frame; otherwise it holds only a single row.
67
68 // This is also used in the case of subsets for interlaced images. We use
69 // this buffer as a full sized encoded image, which we then take the subset
70 // from.
71 // TODO: Subsets of APNG not supported, but if we need to, we would need
72 // a separate fInterlacedBuffer along with fPreblendBuffer.
73 std::vector<uint8_t> fPreblendBuffer;
74
75 int fFirstRow = 0;
76 int fLastRow = 0;
77 // The y offset for a subset in the encoded color type, not the dst color type.
78 // Only used for interlaced images.
79 size_t fYByteOffset = 0;
80 };
81
82 // Helper for validating parameters of `onGetPixels` and/or
83 // `onStartIncrementalDecode`. If `kSuccess` is returned then
84 // `decodingState` output parameter got populated.
85 Result startDecoding(const SkImageInfo& dstInfo,
86 void* pixels,
87 size_t rowBytes,
88 const Options& options,
89 DecodingState* decodingState);
90
91 // Helper for taking a decoded interlaced `srcRow`, applying color
92 // transformations, and then expanding it into the `frame`.
93 void expandDecodedInterlacedRow(SkSpan<uint8_t> dstFrame,
94 SkSpan<const uint8_t> srcRow,
95 const DecodingDstInfo& decodingState,
96 bool xFormNeeded);
97
98 // Helper for row-by-row decoding which is used from `onGetPixels` and/or
99 // `onIncrementalDecode`.
100 Result incrementalDecode(DecodingState& decodingState, int* rowsDecoded);
101 // The same as incrementalDecode but uses `applyXFormRow()`. Should only
102 // be used if this->canReadRows() is false.
103 Result incrementalDecodeXForm(DecodingState& decodingState, int* rowsDecoded);
104
105 // Helper for reading until the start of the next `fdAT` sequence.
106 Result readToStartOfNextFrame();
107
108 // Helper for seeking to the start of image data for the given frame.
109 Result seekToStartOfFrame(int index);
110
111 // The number of frames calculated based on 1) the presence, and 2) the
112 // contents of an `acTL` chunk. "raw" in the sense that it reports all the
113 // frames, while `SkCodec::getFrameCount` and
114 // `SkPngRustCodec::onGetFrameCount` only report frames for which we have
115 // successfully populated `fFrameHolder` with frame info parsed from `IHDR`
116 // and/or `fcTL` chunks.
117 int getRawFrameCount() const;
118
119 // Attempts to read through the input stream to parse the additional `fcTL`
120 // chunks.
121 Result parseAdditionalFrameInfos();
122
123 // SkCodec overrides:
124 Result onGetPixels(const SkImageInfo& dstInfo,
125 void* pixels,
126 size_t rowBytes,
127 const Options&,
128 int* rowsDecoded) override;
129 bool onSupportsIncrementalDecode(const SkImageInfo&) override { return true; }
130 Result onStartIncrementalDecode(const SkImageInfo& dstInfo,
131 void* pixels,
132 size_t rowBytes,
133 const Options&) override;
134 Result onIncrementalDecode(int* rowsDecoded) override;
135 int onGetFrameCount() override;
136 bool onGetFrameInfo(int, FrameInfo*) const override;
137 int onGetRepetitionCount() override;
138 IsAnimated onIsAnimated() override;
139 const SkFrameHolder* getFrameHolder() const override;
140 std::unique_ptr<SkStream> getEncodedData() const override;
141 // Determines whether or not we can read from the rust decoder directly into dst.
142 bool canReadRow();
143
144 // SkPngCodecBase overrides:
145 std::optional<SkSpan<const PaletteColorEntry>> onTryGetPlteChunk() override;
146 std::optional<SkSpan<const uint8_t>> onTryGetTrnsChunk() override;
147
148 rust::Box<rust_png::Reader> fReader;
149
150 // `-1` means that `IDAT` is not part of animation and wasn't skipped yet.
151 int fFrameAtCurrentStreamPosition = -1;
152 bool fStreamIsPositionedAtStartOfFrameData = false;
153 const std::unique_ptr<SkStream> fPrivStream;
154 // TODO(https://crbug.com/371060427): Once fast seeking is available, we can
155 // remove the field that tracks the stream length.
156 std::optional<size_t> fMaxStreamLengthSeenWhenParsingAdditionalFrameInfos;
157
158 std::optional<DecodingState> fIncrementalDecodingState;
159
160 class FrameHolder final : public SkFrameHolder {
161 public:
162 FrameHolder(int width, int height);
163 ~FrameHolder() override;
164
165 FrameHolder(const FrameHolder&) = delete;
166 FrameHolder(FrameHolder&&) = delete;
167 FrameHolder& operator=(const FrameHolder&) = delete;
168 FrameHolder& operator=(FrameHolder&&) = delete;
169
170 // Returning an `int` (rather than `size_t`) for easier interop with
171 // other parts of the SkCodec API.
172 int size() const;
173
174 Result appendNewFrame(const rust_png::Reader& reader, const SkEncodedInfo& info);
175 void markFrameAsFullyReceived(size_t index);
176 bool getFrameInfo(int index, FrameInfo* info) const;
177
178 private:
179 class PngFrame;
180
181 const SkFrame* onGetFrame(int unverifiedIndex) const override;
182 Result setFrameInfoFromCurrentFctlChunk(const rust_png::Reader& reader,
183 PngFrame* out_frame);
184
185 std::vector<PngFrame> fFrames;
186 };
187 FrameHolder fFrameHolder;
188
189 // Whether there may still be additional `fcTL` chunks to discover and parse.
190 //
191 // `true` if the stream hasn't been fully received (i.e. only
192 // `kIncompleteInput` errors so far, no hard errors) and `fFrameHolder`
193 // doesn't yet contain frame info for all `num_frames` declared in an `acTL`
194 // chunk.
195 bool fCanParseAdditionalFrameInfos = true;
196
197 // When decoding interlaced subsets, decode the full image into a buffer in
198 // the encoded colortype and extract a rect subset for the final dst using
199 // this function. This function will use applyXFormRow() for each row.
200 void getSubsetFromFullImage(SkSpan<const uint8_t> fullImageBuffer,
201 SkSpan<uint8_t> dst,
202 size_t dstRowStride,
203 size_t offset);
204 };
205
206 #endif // SkPngRustCodec_DEFINED