| 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 |
|
| 8 |
#include "src/encode/SkPngRustEncoderImpl.h" |
| 9 |
|
| 10 |
#include <limits> |
| 11 |
#include <memory> |
| 12 |
#include <optional> |
| 13 |
#include <utility> |
| 14 |
|
| 15 |
#include "include/core/SkSpan.h" |
| 16 |
#include "include/core/SkStream.h" |
| 17 |
#include "include/encode/SkPngRustEncoder.h" |
| 18 |
#include "include/private/SkEncodedInfo.h" |
| 19 |
#include "include/private/base/SkAssert.h" |
| 20 |
#include "rust/png/FFI.rs.h" |
| 21 |
#include "rust/common/SpanUtils.h" |
| 22 |
#include "src/base/SkSafeMath.h" |
| 23 |
#include "src/encode/SkImageEncoderFns.h" |
| 24 |
#include "src/encode/SkImageEncoderPriv.h" |
| 25 |
#include "third_party/rust/cxx/v1/cxx.h" |
| 26 |
|
| 27 |
#ifdef __clang__ |
| 28 |
#pragma clang diagnostic error "-Wconversion" |
| 29 |
#endif |
| 30 |
|
| 31 |
namespace { |
| 32 |
|
| 33 |
rust_png::Compression ToCompression(SkPngRustEncoder::CompressionLevel level) { |
| 34 |
switch (level) { |
| 35 |
case SkPngRustEncoder::CompressionLevel::kLow: |
| 36 |
return rust_png::Compression::Level1WithUpFilter; |
| 37 |
case SkPngRustEncoder::CompressionLevel::kMedium: |
| 38 |
#ifdef SK_RUST_PNG_MAP_MEDIUM_COMPRESSION_LEVEL_TO_FDEFLATE_FAST |
| 39 |
// TODO(https://crbug.com/406072770): Consider using `Fast` instead |
| 40 |
// of `Balanced` compression here. See the bug for details. |
| 41 |
return rust_png::Compression::Fast; |
| 42 |
#else |
| 43 |
return rust_png::Compression::Balanced; |
| 44 |
#endif |
| 45 |
case SkPngRustEncoder::CompressionLevel::kHigh: |
| 46 |
return rust_png::Compression::High; |
| 47 |
} |
| 48 |
SkUNREACHABLE; |
| 49 |
} |
| 50 |
|
| 51 |
rust::Slice<const uint8_t> getDataTableEntry(const SkDataTable& table, int index) { |
| 52 |
SkASSERT_RELEASE((0 <= index) && (index < table.count())); |
| 53 |
|
| 54 |
size_t size = 0; |
| 55 |
const uint8_t* entry = table.atT<uint8_t>(index, &size); |
| 56 |
while (size > 0 && entry[size - 1] == 0) { |
| 57 |
// Ignore trailing NUL characters - these are *not* part of Rust `&str`. |
| 58 |
size--; |
| 59 |
} |
| 60 |
|
| 61 |
return rust::Slice<const uint8_t>(entry, size); |
| 62 |
} |
| 63 |
|
| 64 |
rust_png::EncodingResult EncodeComments(rust_png::Writer& writer, |
| 65 |
const sk_sp<SkDataTable>& comments) { |
| 66 |
if (comments != nullptr) { |
| 67 |
if (comments->count() % 2 != 0) { |
| 68 |
return rust_png::EncodingResult::ParameterError; |
| 69 |
} |
| 70 |
|
| 71 |
for (int i = 0; i < comments->count() / 2; ++i) { |
| 72 |
rust::Slice<const uint8_t> keyword = getDataTableEntry(*comments, 2 * i); |
| 73 |
rust::Slice<const uint8_t> text = getDataTableEntry(*comments, 2 * i + 1); |
| 74 |
rust_png::EncodingResult result = writer.write_text_chunk(keyword, text); |
| 75 |
if (result != rust_png::EncodingResult::Success) { |
| 76 |
return result; |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
return rust_png::EncodingResult::Success; |
| 82 |
} |
| 83 |
|
| 84 |
// This helper class adapts `SkWStream` to expose the API required by Rust FFI |
| 85 |
// (i.e. the `WriteTrait` API). |
| 86 |
class WriteTraitAdapterForSkWStream final : public rust_png::WriteTrait { |
| 87 |
public: |
| 88 |
// SAFETY: The caller needs to guarantee that `stream` will be alive for |
| 89 |
// as long as `WriteTraitAdapterForSkWStream`. |
| 90 |
explicit WriteTraitAdapterForSkWStream(SkWStream* stream) : fStream(stream) { |
| 91 |
SkASSERT_RELEASE(fStream); |
| 92 |
} |
| 93 |
|
| 94 |
~WriteTraitAdapterForSkWStream() override = default; |
| 95 |
|
| 96 |
// Non-copyable and non-movable. |
| 97 |
WriteTraitAdapterForSkWStream(const WriteTraitAdapterForSkWStream&) = delete; |
| 98 |
WriteTraitAdapterForSkWStream& operator=(const WriteTraitAdapterForSkWStream&) = delete; |
| 99 |
WriteTraitAdapterForSkWStream(WriteTraitAdapterForSkWStream&&) = delete; |
| 100 |
WriteTraitAdapterForSkWStream& operator=(WriteTraitAdapterForSkWStream&&) = delete; |
| 101 |
|
| 102 |
// Implementation of the `std::io::Read::read` method. See `RustTrait`'s |
| 103 |
// doc comments and |
| 104 |
// https://doc.rust-lang.org/nightly/std/io/trait.Read.html#tymethod.read |
| 105 |
// for guidance on the desired implementation and behavior of this method. |
| 106 |
bool write(rust::Slice<const uint8_t> buffer) override { |
| 107 |
SkSpan<const uint8_t> span = ToSkSpan(buffer); |
| 108 |
return fStream->write(span.data(), span.size()); |
| 109 |
} |
| 110 |
|
| 111 |
void flush() override { fStream->flush(); } |
| 112 |
|
| 113 |
private: |
| 114 |
SkWStream* fStream = nullptr; // Non-owning pointer. |
| 115 |
}; |
| 116 |
|
| 117 |
} // namespace |
| 118 |
|
| 119 |
// static |
| 120 |
std::unique_ptr<SkEncoder> SkPngRustEncoderImpl::Make(SkWStream* dst, |
| 121 |
const SkPixmap& src, |
| 122 |
const SkPngRustEncoder::Options& options) { |
| 123 |
if (!SkPixmapIsValid(src)) { |
| 124 |
return nullptr; |
| 125 |
} |
| 126 |
|
| 127 |
std::optional<TargetInfo> maybeTargetInfo = SkPngEncoderBase::getTargetInfo(src.info()); |
| 128 |
if (!maybeTargetInfo.has_value()) { |
| 129 |
return nullptr; |
| 130 |
} |
| 131 |
const SkEncodedInfo& dstInfo = maybeTargetInfo->fDstInfo; |
| 132 |
const std::optional<SkImageInfo>& maybeDstRowInfo = maybeTargetInfo->fDstRowInfo; |
| 133 |
|
| 134 |
SkSafeMath safe; |
| 135 |
uint32_t width = safe.castTo<uint32_t>(dstInfo.width()); |
| 136 |
uint32_t height = safe.castTo<uint32_t>(dstInfo.height()); |
| 137 |
if (!safe.ok()) { |
| 138 |
return nullptr; |
| 139 |
} |
| 140 |
|
| 141 |
sk_sp<SkData> encodedProfile; |
| 142 |
rust::Slice<const uint8_t> encodedProfileSlice; |
| 143 |
if (const SkColorSpace* colorSpace = src.colorSpace(); colorSpace && !colorSpace->isSRGB()) { |
| 144 |
encodedProfile = icc_from_color_space(colorSpace); |
| 145 |
if (encodedProfile) { |
| 146 |
encodedProfileSlice = |
| 147 |
rust::Slice<const uint8_t>(encodedProfile->bytes(), encodedProfile->size()); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
rust_png::ColorType rustEncoderColorType; |
| 152 |
ExtraRowTransform extraRowTransform = kNone_ExtraRowTransform; |
| 153 |
switch (dstInfo.color()) { |
| 154 |
case SkEncodedInfo::kRGB_Color: |
| 155 |
rustEncoderColorType = rust_png::ColorType::Rgb; |
| 156 |
break; |
| 157 |
case SkEncodedInfo::kRGBA_Color: |
| 158 |
rustEncoderColorType = rust_png::ColorType::Rgba; |
| 159 |
if (maybeDstRowInfo) { |
| 160 |
if (maybeDstRowInfo->isOpaque()) { |
| 161 |
rustEncoderColorType = rust_png::ColorType::Rgb; |
| 162 |
if (maybeDstRowInfo->colorType() == kR16G16B16A16_unorm_SkColorType) { |
| 163 |
extraRowTransform = kRgba16leToRgb16be_ExtraRowTransform; |
| 164 |
} else { |
| 165 |
SkASSERT_RELEASE(maybeDstRowInfo->colorType() == kRGB_888x_SkColorType); |
| 166 |
extraRowTransform = kRgba8ToRgb8_ExtraRowTransform; |
| 167 |
} |
| 168 |
} else if (maybeDstRowInfo->colorType() == kR16G16B16A16_unorm_SkColorType) { |
| 169 |
extraRowTransform = kRgba16leToRgba16be_ExtraRowTransform; |
| 170 |
} |
| 171 |
} |
| 172 |
break; |
| 173 |
case SkEncodedInfo::kGray_Color: |
| 174 |
rustEncoderColorType = rust_png::ColorType::Grayscale; |
| 175 |
break; |
| 176 |
case SkEncodedInfo::kGrayAlpha_Color: |
| 177 |
rustEncoderColorType = rust_png::ColorType::GrayscaleAlpha; |
| 178 |
break; |
| 179 |
default: |
| 180 |
SkUNREACHABLE; |
| 181 |
} |
| 182 |
|
| 183 |
auto writeTraitAdapter = std::make_unique<WriteTraitAdapterForSkWStream>(dst); |
| 184 |
rust::Box<rust_png::ResultOfWriter> resultOfWriter = |
| 185 |
rust_png::new_writer(std::move(writeTraitAdapter), |
| 186 |
width, |
| 187 |
height, |
| 188 |
rustEncoderColorType, |
| 189 |
dstInfo.bitsPerComponent(), |
| 190 |
ToCompression(options.fCompressionLevel), |
| 191 |
encodedProfileSlice); |
| 192 |
if (resultOfWriter->err() != rust_png::EncodingResult::Success) { |
| 193 |
return nullptr; |
| 194 |
} |
| 195 |
rust::Box<rust_png::Writer> writer = resultOfWriter->unwrap(); |
| 196 |
|
| 197 |
if (EncodeComments(*writer, options.fComments) != rust_png::EncodingResult::Success) { |
| 198 |
return nullptr; |
| 199 |
} |
| 200 |
|
| 201 |
rust::Box<rust_png::ResultOfStreamWriter> resultOfStreamWriter = |
| 202 |
rust_png::convert_writer_into_stream_writer(std::move(writer)); |
| 203 |
if (resultOfStreamWriter->err() != rust_png::EncodingResult::Success) { |
| 204 |
return nullptr; |
| 205 |
} |
| 206 |
rust::Box<rust_png::StreamWriter> stream_writer = resultOfStreamWriter->unwrap(); |
| 207 |
|
| 208 |
return std::make_unique<SkPngRustEncoderImpl>( |
| 209 |
std::move(*maybeTargetInfo), src, std::move(stream_writer), extraRowTransform); |
| 210 |
} |
| 211 |
|
| 212 |
SkPngRustEncoderImpl::SkPngRustEncoderImpl(TargetInfo targetInfo, |
| 213 |
const SkPixmap& src, |
| 214 |
rust::Box<rust_png::StreamWriter> stream_writer, |
| 215 |
ExtraRowTransform extraRowTransform) |
| 216 |
: SkPngEncoderBase(std::move(targetInfo), src) |
| 217 |
, fStreamWriter(std::move(stream_writer)) |
| 218 |
, fExtraRowTransform(extraRowTransform) {} |
| 219 |
|
| 220 |
SkPngRustEncoderImpl::~SkPngRustEncoderImpl() = default; |
| 221 |
|
| 222 |
bool SkPngRustEncoderImpl::onEncodeRow(SkSpan<const uint8_t> row) { |
| 223 |
rust::Slice<const uint8_t> rustRow; |
| 224 |
if (this->fExtraRowTransform != kNone_ExtraRowTransform) { |
| 225 |
skcms_PixelFormat srcFmt, dstFmt; |
| 226 |
size_t srcRowBytes = this->targetInfo().fDstRowInfo->minRowBytes(); |
| 227 |
size_t dstRowBytes; |
| 228 |
switch (this->fExtraRowTransform) { |
| 229 |
case kRgba8ToRgb8_ExtraRowTransform: |
| 230 |
srcFmt = skcms_PixelFormat_RGBA_8888; |
| 231 |
dstFmt = skcms_PixelFormat_RGB_888; |
| 232 |
dstRowBytes = srcRowBytes - (srcRowBytes / 4); |
| 233 |
break; |
| 234 |
case kRgba16leToRgba16be_ExtraRowTransform: |
| 235 |
srcFmt = skcms_PixelFormat_RGBA_16161616LE; |
| 236 |
dstFmt = skcms_PixelFormat_RGBA_16161616BE; |
| 237 |
dstRowBytes = srcRowBytes; |
| 238 |
break; |
| 239 |
case kRgba16leToRgb16be_ExtraRowTransform: |
| 240 |
srcFmt = skcms_PixelFormat_RGBA_16161616LE; |
| 241 |
dstFmt = skcms_PixelFormat_RGB_161616BE; |
| 242 |
dstRowBytes = srcRowBytes - (srcRowBytes / 4); |
| 243 |
break; |
| 244 |
default: |
| 245 |
SkUNREACHABLE; |
| 246 |
} |
| 247 |
|
| 248 |
fExtraRowBuffer.resize(dstRowBytes, 0x00); |
| 249 |
SkSafeMath safe; |
| 250 |
size_t width = safe.castTo<size_t>(this->targetInfo().fDstRowInfo->width()); |
| 251 |
if (!safe.ok()) { |
| 252 |
return false; |
| 253 |
} |
| 254 |
|
| 255 |
bool success = skcms_Transform( |
| 256 |
row.data(), srcFmt, skcms_AlphaFormat_Unpremul, nullptr, |
| 257 |
fExtraRowBuffer.data(), dstFmt, skcms_AlphaFormat_Unpremul, nullptr, |
| 258 |
width); |
| 259 |
if (!success) { |
| 260 |
return false; |
| 261 |
} |
| 262 |
|
| 263 |
rustRow = rust::Slice<const uint8_t>(fExtraRowBuffer); |
| 264 |
} else { |
| 265 |
SkASSERT(this->fExtraRowTransform == kNone_ExtraRowTransform); |
| 266 |
rustRow = rust::Slice<const uint8_t>(row); |
| 267 |
} |
| 268 |
return fStreamWriter->write(rustRow) == rust_png::EncodingResult::Success; |
| 269 |
} |
| 270 |
|
| 271 |
bool SkPngRustEncoderImpl::onFinishEncoding() { |
| 272 |
return rust_png::finish_encoding(std::move(fStreamWriter)) == rust_png::EncodingResult::Success; |
| 273 |
} |