| 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 SkPngRustEncoderImpl_DEFINED |
| 8 |
#define SkPngRustEncoderImpl_DEFINED |
| 9 |
|
| 10 |
#include <memory> |
| 11 |
|
| 12 |
#include "rust/png/FFI.rs.h" |
| 13 |
#include "src/encode/SkPngEncoderBase.h" |
| 14 |
#include "third_party/rust/cxx/v1/cxx.h" |
| 15 |
|
| 16 |
class SkWStream; |
| 17 |
struct SkEncodedInfo; |
| 18 |
class SkPixmap; |
| 19 |
class SkPngEncoderMgr; |
| 20 |
template <typename T> class SkSpan; |
| 21 |
|
| 22 |
namespace SkPngRustEncoder { |
| 23 |
struct Options; |
| 24 |
} // namespace SkPngRustEncoder |
| 25 |
|
| 26 |
// This class provides the Skia image encoding API (`SkEncoder`) on top of the |
| 27 |
// third-party `png` crate (PNG compression and encoding implemented in Rust). |
| 28 |
class SkPngRustEncoderImpl final : public SkPngEncoderBase { |
| 29 |
public: |
| 30 |
enum ExtraRowTransform { |
| 31 |
// `kNone...` indicates that pixels from `SkPngEncoderBase` can be fed |
| 32 |
// directly into Rust `png`. |
| 33 |
kNone_ExtraRowTransform, |
| 34 |
|
| 35 |
// `kRgba8ToRgb8...` indicates that `SkPngEncoderBase` gives RGBx (8-bit) |
| 36 |
// pixels to `onEncodeRow`. And since Rust `png` can't ignore the |
| 37 |
// alpha channel (`libpng` can do this via `png_set_filler`), we need to |
| 38 |
// do an extra RGBA => RGB transformation before feeding the data into |
| 39 |
// Rust. |
| 40 |
kRgba8ToRgb8_ExtraRowTransform, |
| 41 |
|
| 42 |
// `kRgba16leToRgba16be...` indicates that `SkPngEncoderBase` gives RGBA (16-bit) |
| 43 |
// little endian pixels to `onEncodeRow`. And since Rust `png` can't swap the |
| 44 |
// endianess (`libpng` can do this via `png_set_swap`), we need to |
| 45 |
// do an extra LE => BE transformation before feeding the data into |
| 46 |
// Rust. |
| 47 |
kRgba16leToRgba16be_ExtraRowTransform, |
| 48 |
|
| 49 |
// `kRgba16leToRgb16be...` indicates that `SkPngEncoderBase` gives RGBA (16-bit) |
| 50 |
// little endian pixels to `onEncodeRow`. And we must ignore alpha + swap endianess. |
| 51 |
kRgba16leToRgb16be_ExtraRowTransform, |
| 52 |
}; |
| 53 |
|
| 54 |
static std::unique_ptr<SkEncoder> Make(SkWStream*, |
| 55 |
const SkPixmap&, |
| 56 |
const SkPngRustEncoder::Options& options); |
| 57 |
|
| 58 |
// `public` to support `std::make_unique<SkPngRustEncoderImpl>(...)`. |
| 59 |
SkPngRustEncoderImpl(TargetInfo targetInfo, |
| 60 |
const SkPixmap& src, |
| 61 |
rust::Box<rust_png::StreamWriter> streamWriter, |
| 62 |
ExtraRowTransform extraRowTransform); |
| 63 |
|
| 64 |
~SkPngRustEncoderImpl() override; |
| 65 |
|
| 66 |
protected: |
| 67 |
bool onEncodeRow(SkSpan<const uint8_t> row) override; |
| 68 |
bool onFinishEncoding() override; |
| 69 |
|
| 70 |
private: |
| 71 |
rust::Box<rust_png::StreamWriter> fStreamWriter; |
| 72 |
|
| 73 |
ExtraRowTransform fExtraRowTransform; |
| 74 |
std::vector<uint8_t> fExtraRowBuffer; |
| 75 |
}; |
| 76 |
|
| 77 |
#endif // SkPngRustEncoderImpl_DEFINED |