0

[rust png] Use SkPngRustEncoder (if enabled) from Blink.

Bug: chromium:379312510
Change-Id: Ie315a49c9fd83290839095608252aa85013af835
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6085623
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Commit-Queue: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: Florin Malita <fmalita@chromium.org>
Auto-Submit: Łukasz Anforowicz <lukasza@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1395770}
This commit is contained in:
Lukasz Anforowicz
2024-12-12 22:05:27 -08:00
committed by Chromium LUCI CQ
parent 5d6ee3e7c7
commit 339ce944ed
7 changed files with 96 additions and 37 deletions
skia
third_party/blink/renderer/platform
ui/gfx/codec

@ -42,6 +42,7 @@ component("rusty_png_feature_detection") {
"rusty_png_feature.cc",
"rusty_png_feature.h",
]
deps = [ ":skia" ]
public_deps = [
":buildflags",
"//base",

@ -5,9 +5,72 @@
#include "skia/rusty_png_feature.h"
#include "base/feature_list.h"
#include "base/notreached.h"
#include "skia/buildflags.h"
#include "third_party/skia/include/encode/SkPngEncoder.h"
#if BUILDFLAG(SKIA_BUILD_RUST_PNG)
#include "third_party/skia/experimental/rust_png/encoder/SkPngRustEncoder.h"
#endif
namespace skia {
#if BUILDFLAG(SKIA_BUILD_RUST_PNG)
namespace {
SkPngRustEncoder::Options ConvertToRustOptions(
const SkPngEncoder::Options& options) {
SkPngRustEncoder::Options rust_options;
if (options.fZLibLevel < 4) {
rust_options.fCompressionLevel = SkPngRustEncoder::CompressionLevel::kLow;
} else if (options.fZLibLevel < 7) {
rust_options.fCompressionLevel =
SkPngRustEncoder::CompressionLevel::kMedium;
} else {
rust_options.fCompressionLevel = SkPngRustEncoder::CompressionLevel::kHigh;
}
rust_options.fComments = options.fComments;
// TODO(https://crbug.com/379312510): Translate other `options` (e.g.
// comments and/or ICC profile).
return rust_options;
}
} // namespace
#endif
BASE_FEATURE(kRustyPngFeature, "RustyPng", base::FEATURE_DISABLED_BY_DEFAULT);
bool EncodePng(SkWStream* dst,
const SkPixmap& src,
const SkPngEncoder::Options& options) {
if (IsRustyPngEnabled()) {
#if BUILDFLAG(SKIA_BUILD_RUST_PNG)
return SkPngRustEncoder::Encode(dst, src, ConvertToRustOptions(options));
#else
// The `if` condition guarantees `SKIA_BUILD_RUST_PNG`.
NOTREACHED();
#endif
}
return SkPngEncoder::Encode(dst, src, options);
}
std::unique_ptr<SkEncoder> MakePngEncoder(
SkWStream* dst,
const SkPixmap& src,
const SkPngEncoder::Options& options) {
if (IsRustyPngEnabled()) {
#if BUILDFLAG(SKIA_BUILD_RUST_PNG)
return SkPngRustEncoder::Make(dst, src, ConvertToRustOptions(options));
#else
// The `if` condition guarantees `SKIA_BUILD_RUST_PNG`.
NOTREACHED();
#endif
}
return SkPngEncoder::Make(dst, src, options);
}
} // namespace skia

@ -5,9 +5,19 @@
#ifndef SKIA_RUSTY_PNG_FEATURE_H_
#define SKIA_RUSTY_PNG_FEATURE_H_
#include <memory>
#include "base/component_export.h"
#include "base/feature_list.h"
#include "skia/buildflags.h"
#include "third_party/skia/include/encode/SkEncoder.h"
class SkPixmap;
class SkWStream;
namespace SkPngEncoder {
struct Options;
} // namespace SkPngEncoder
namespace skia {
@ -31,6 +41,21 @@ inline bool IsRustyPngEnabled() {
#endif
}
// A helper that will encode a PNG image using either the `libpng`-based
// `SkPngEncoder::Encode` API, or (if `kRustyPngFeature` is built and enabled)
// the Rust-based `SkPngRustEncoder::Encode` API.
COMPONENT_EXPORT(SKIA_RUSTY_PNG_FEATURE_DETECTION)
bool EncodePng(SkWStream* dst,
const SkPixmap& src,
const SkPngEncoder::Options& options);
// A helper that will create either a `libpng`-based, or a Rust-based PNG
// encoder (depending on whether the `kRustyPngFeature` is built and enabled).
COMPONENT_EXPORT(SKIA_RUSTY_PNG_FEATURE_DETECTION)
std::unique_ptr<SkEncoder> MakePngEncoder(SkWStream* dst,
const SkPixmap& src,
const SkPngEncoder::Options& options);
} // namespace skia
#endif // SKIA_RUSTY_PNG_FEATURE_H_

@ -1790,6 +1790,7 @@ component("platform") {
"//services/service_manager/public/cpp",
"//services/viz/public/cpp/gpu",
"//skia",
"//skia:rusty_png_feature_detection",
"//skia:skcms",
"//third_party:freetype_buildflags",
"//third_party:freetype_harfbuzz",

@ -6,6 +6,7 @@ include_rules = [
"+third_party/blink/renderer/platform/image-encoders",
# Dependencies.
"+skia/rusty_png_feature.h",
"+third_party/blink/renderer/platform/graphics/graphics_types.h",
"+third_party/blink/renderer/platform/instrumentation/histogram.h",
"+third_party/blink/renderer/platform/network/mime/mime_type_registry.h",

@ -6,14 +6,14 @@
#include "base/notreached.h"
#include "build/build_config.h"
#include "skia/rusty_png_feature.h"
#if BUILDFLAG(IS_WIN)
#include <basetsd.h> // Included before jpeglib.h because of INT32 clash
#endif
#include <stdio.h> // Needed by jpeglib.h
#include <stdio.h> // Needed by jpeglib.h
#include "jpeglib.h" // for JPEG_MAX_DIMENSION
#include "third_party/libwebp/src/src/webp/encode.h" // for WEBP_MAX_DIMENSION
namespace blink {
@ -29,7 +29,7 @@ bool ImageEncoder::Encode(Vector<unsigned char>* dst,
const SkPixmap& src,
const SkPngEncoder::Options& options) {
VectorWStream dst_stream(dst);
return SkPngEncoder::Encode(&dst_stream, src, options);
return skia::EncodePng(&dst_stream, src, options);
}
bool ImageEncoder::Encode(Vector<unsigned char>* dst,
@ -59,7 +59,7 @@ std::unique_ptr<ImageEncoder> ImageEncoder::Create(
const SkPngEncoder::Options& options) {
std::unique_ptr<ImageEncoder> image_encoder(new ImageEncoder(dst));
image_encoder->encoder_ =
SkPngEncoder::Make(&image_encoder->dst_, src, options);
skia::MakePngEncoder(&image_encoder->dst_, src, options);
if (!image_encoder->encoder_) {
return nullptr;
}

@ -25,7 +25,6 @@
#if BUILDFLAG(SKIA_BUILD_RUST_PNG)
#include "third_party/skia/experimental/rust_png/decoder/SkPngRustDecoder.h"
#include "third_party/skia/experimental/rust_png/encoder/SkPngRustEncoder.h"
#endif
namespace gfx {
@ -196,37 +195,6 @@ void AddComments(SkPngEncoder::Options& options,
static_cast<int>(comment_pointers.size()));
}
// A helper that will encode a PNG image using either the `libpng`-based
// `SkPngEncoder::Encode` API, or (if `kRustyPngFeature` is built and enabled)
// the Rust-based `SkPngRustEncoder::Encode` API.
bool EncodePng(SkWStream* dst,
const SkPixmap& src,
const SkPngEncoder::Options& options) {
if (skia::IsRustyPngEnabled()) {
#if BUILDFLAG(SKIA_BUILD_RUST_PNG)
SkPngRustEncoder::Options rust_options;
if (options.fZLibLevel < 4) {
rust_options.fCompressionLevel = SkPngRustEncoder::CompressionLevel::kLow;
} else if (options.fZLibLevel < 7) {
rust_options.fCompressionLevel =
SkPngRustEncoder::CompressionLevel::kMedium;
} else {
rust_options.fCompressionLevel =
SkPngRustEncoder::CompressionLevel::kHigh;
}
rust_options.fComments = options.fComments;
// TODO(https://crbug.com/381139785): Translate the ICC profile as well.
return SkPngRustEncoder::Encode(dst, src, rust_options);
#else
// The `if` condition guarantees `SKIA_BUILD_RUST_PNG`.
NOTREACHED();
#endif
}
return SkPngEncoder::Encode(dst, src, options);
}
std::optional<std::vector<uint8_t>> EncodeSkPixmap(
const SkPixmap& src,
const std::vector<PNGCodec::Comment>& comments,
@ -242,7 +210,7 @@ std::optional<std::vector<uint8_t>> EncodeSkPixmap(
options.fFilterFlags = SkPngEncoder::FilterFlag::kNone;
}
if (!EncodePng(&dst, src, options)) {
if (!skia::EncodePng(&dst, src, options)) {
return std::nullopt;
}