blob: d7d939c4097e0b126e4233c12f41f4dc6a766600 [file]
/*
* Copyright (C) 2026 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <SkImage.h>
#include <SkPixmap.h>
#include <SkPngEncoder.h>
#include <SkPngRustEncoder.h>
#include <SkStream.h>
#include <com_android_graphics_hwui_flags.h>
namespace android {
namespace uirenderer {
inline SkPngRustEncoder::Options MapOptions(const SkPngEncoder::Options& options) {
SkPngRustEncoder::Options rustOptions;
rustOptions.fComments = options.fComments;
rustOptions.fGainmap = options.fGainmap;
rustOptions.fGainmapInfo = options.fGainmapInfo;
// Map zlib compression level (0-9) to Rust compression level (Low, Medium, High)
if (options.fZLibLevel <= 3) {
rustOptions.fCompressionLevel = SkPngRustEncoder::CompressionLevel::kLow;
} else if (options.fZLibLevel <= 6) {
rustOptions.fCompressionLevel = SkPngRustEncoder::CompressionLevel::kMedium;
} else {
rustOptions.fCompressionLevel = SkPngRustEncoder::CompressionLevel::kHigh;
}
// Note: fFilterFlags and fHdrMetadata are not supported by SkPngRustEncoder and are ignored.
return rustOptions;
}
inline bool EncodePng(SkWStream* dst, const SkPixmap& src, const SkPngEncoder::Options& options) {
if (com::android::graphics::hwui::flags::enable_rust_png()) {
return SkPngRustEncoder::Encode(dst, src, MapOptions(options));
}
return SkPngEncoder::Encode(dst, src, options);
}
inline sk_sp<SkData> EncodePng(GrDirectContext* ctx, const SkImage* img,
const SkPngEncoder::Options& options) {
if (com::android::graphics::hwui::flags::enable_rust_png()) {
return SkPngRustEncoder::Encode(ctx, img, MapOptions(options));
}
return SkPngEncoder::Encode(ctx, img, options);
}
} // namespace uirenderer
} // namespace android