From 248ff02331d7f73ee4b6c5a7eabeae1080c16cd4 Mon Sep 17 00:00:00 2001 From: Hal Canary Date: Tue, 22 Nov 2016 09:03:03 -0700 Subject: SkImageEncoder: simplify API - Hide SkImageEncoder class in private header. - SkImageEncoder::Type becomes SkEncodedImageFormat - SkEncodedFormat becomes SkEncodedImageFormat - SkImageEncoder static functions replaced with single function EncodeImage() - utility wrappers for EncodeImage() are in sk_tool_utils.h TODO: remove link-time registration mechanism. TODO: clean up clients use of API and flip the flag. TODO: implement EncodeImage() in chromeium/skia/ext GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=4909 Change-Id: Ib48b31fdc05cf23cda7f56ebfd67c841c149ce70 Reviewed-on: https://skia-review.googlesource.com/4909 Reviewed-by: Mike Reed Reviewed-by: Leon Scroggins Commit-Queue: Hal Canary --- src/images/SkForceLinking.cpp | 6 +-- src/images/SkImageEncoder.cpp | 81 +++-------------------------------- src/images/SkImageEncoderPriv.h | 68 +++++++++++++++++++++++++++++ src/images/SkImageEncoder_Factory.cpp | 4 +- src/images/SkJPEGImageEncoder.cpp | 12 +++--- src/images/SkKTXImageEncoder.cpp | 4 +- src/images/SkPNGImageEncoder.cpp | 4 +- src/images/SkWEBPImageEncoder.cpp | 4 +- 8 files changed, 91 insertions(+), 92 deletions(-) create mode 100644 src/images/SkImageEncoderPriv.h (limited to 'src/images') diff --git a/src/images/SkForceLinking.cpp b/src/images/SkForceLinking.cpp index 81d485c882..dfed6b4b57 100644 --- a/src/images/SkForceLinking.cpp +++ b/src/images/SkForceLinking.cpp @@ -5,7 +5,7 @@ * found in the LICENSE file. */ -#include "SkImageEncoder.h" +#include "SkImageEncoderPriv.h" #include "SkForceLinking.h" // This method is required to fool the linker into not discarding the pre-main @@ -30,10 +30,10 @@ int SkForceLinking(bool doNotPassTrue) { #endif #if defined (SK_USE_CG_ENCODER) - CreateImageEncoder_CG(SkImageEncoder::kPNG_Type); + CreateImageEncoder_CG(SkEncodedImageFormat::kPNG); #endif #if defined (SK_USE_WIC_ENCODER) - CreateImageEncoder_WIC(SkImageEncoder::kPNG_Type); + CreateImageEncoder_WIC(SkEncodedImageFormat::kPNG); #endif return -1; } diff --git a/src/images/SkImageEncoder.cpp b/src/images/SkImageEncoder.cpp index 787ff8ef62..fc5f1267a5 100644 --- a/src/images/SkImageEncoder.cpp +++ b/src/images/SkImageEncoder.cpp @@ -5,84 +5,15 @@ * found in the LICENSE file. */ -#include "SkImageEncoder.h" -#include "SkBitmap.h" -#include "SkPixelSerializer.h" -#include "SkPixmap.h" -#include "SkStream.h" -#include "SkTemplates.h" +#include "SkImageEncoderPriv.h" -SkImageEncoder::~SkImageEncoder() {} - -bool SkImageEncoder::encodeStream(SkWStream* stream, const SkBitmap& bm, - int quality) { - quality = SkMin32(100, SkMax32(0, quality)); - return this->onEncode(stream, bm, quality); -} - -bool SkImageEncoder::encodeFile(const char file[], const SkBitmap& bm, - int quality) { - quality = SkMin32(100, SkMax32(0, quality)); - SkFILEWStream stream(file); - return this->onEncode(&stream, bm, quality); -} - -SkData* SkImageEncoder::encodeData(const SkBitmap& bm, int quality) { - SkDynamicMemoryWStream stream; - quality = SkMin32(100, SkMax32(0, quality)); - if (this->onEncode(&stream, bm, quality)) { - return stream.detachAsData().release(); - } - return nullptr; -} - -bool SkImageEncoder::EncodeFile(const char file[], const SkBitmap& bm, Type t, - int quality) { - std::unique_ptr enc(SkImageEncoder::Create(t)); - return enc.get() && enc.get()->encodeFile(file, bm, quality); -} - -bool SkImageEncoder::EncodeStream(SkWStream* stream, const SkBitmap& bm, Type t, - int quality) { - std::unique_ptr enc(SkImageEncoder::Create(t)); - return enc.get() && enc.get()->encodeStream(stream, bm, quality); -} - -SkData* SkImageEncoder::EncodeData(const SkBitmap& bm, Type t, int quality) { - std::unique_ptr enc(SkImageEncoder::Create(t)); - return enc.get() ? enc.get()->encodeData(bm, quality) : nullptr; -} - -SkData* SkImageEncoder::EncodeData(const SkImageInfo& info, const void* pixels, size_t rowBytes, - Type t, int quality) { - SkBitmap bm; - if (!bm.installPixels(info, const_cast(pixels), rowBytes)) { - return nullptr; - } - bm.setImmutable(); - return SkImageEncoder::EncodeData(bm, t, quality); -} - -SkData* SkImageEncoder::EncodeData(const SkPixmap& pixmap, - Type t, int quality) { +bool SkEncodeImage(SkWStream* dst, const SkPixmap& src, + SkEncodedImageFormat format, int quality) { SkBitmap bm; - if (!bm.installPixels(pixmap)) { + if (!bm.installPixels(src)) { return nullptr; } bm.setImmutable(); - return SkImageEncoder::EncodeData(bm, t, quality); -} - -namespace { -class ImageEncoderPixelSerializer final : public SkPixelSerializer { -protected: - bool onUseEncodedData(const void*, size_t) override { return true; } - SkData* onEncode(const SkPixmap& pmap) override { - return SkImageEncoder::EncodeData(pmap, SkImageEncoder::kPNG_Type, 100); - } -}; -} // namespace - -SkPixelSerializer* SkImageEncoder::CreatePixelSerializer() { - return new ImageEncoderPixelSerializer; + std::unique_ptr enc(SkImageEncoder::Create((SkImageEncoder::Type)format)); + return enc && enc->encodeStream(dst, bm, quality); } diff --git a/src/images/SkImageEncoderPriv.h b/src/images/SkImageEncoderPriv.h new file mode 100644 index 0000000000..9b632f5a5b --- /dev/null +++ b/src/images/SkImageEncoderPriv.h @@ -0,0 +1,68 @@ +/* + * Copyright 2016 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef SkImageEncoderPriv_DEFINED +#define SkImageEncoderPriv_DEFINED + +#include "SkImageEncoder.h" +#include "SkTRegistry.h" + +#ifndef SK_SUPPORT_LEGACY_IMAGE_ENCODER_CLASS + + // TODO(halcanary): replace this class and registry system with something simpler. + class SkImageEncoder { + public: + typedef SkEncodedImageFormat Type; + static SkImageEncoder* Create(SkEncodedImageFormat); + + virtual ~SkImageEncoder() {} + + bool encodeStream(SkWStream* dst, const SkBitmap& src, int quality) { + return this->onEncode(dst, src, SkMin32(100, SkMax32(0, quality))); + } + + protected: + /** + * Encode bitmap 'bm' in the desired format, writing results to + * stream 'stream', at quality level 'quality' (which can be in + * range 0-100). + */ + virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) = 0; + }; + +#endif // SK_SUPPORT_LEGACY_IMAGE_ENCODER_CLASS + +// This macro declares a global (i.e., non-class owned) creation entry point +// for each encoder (e.g., CreateJPEGImageEncoder) +#define DECLARE_ENCODER_CREATOR(codec) \ + SK_API SkImageEncoder *Create ## codec (); + +// This macro defines the global creation entry point for each encoder. Each +// encoder implementation that registers with the encoder factory must call it. +#define DEFINE_ENCODER_CREATOR(codec) \ + SkImageEncoder* Create##codec() { return new Sk##codec; } + +// All the encoders known by Skia. Note that, depending on the compiler settings, +// not all of these will be available +DECLARE_ENCODER_CREATOR(JPEGImageEncoder); +DECLARE_ENCODER_CREATOR(PNGImageEncoder); +DECLARE_ENCODER_CREATOR(KTXImageEncoder); +DECLARE_ENCODER_CREATOR(WEBPImageEncoder); + +#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) +SkImageEncoder* CreateImageEncoder_CG(SkImageEncoder::Type type); +#endif + +#if defined(SK_BUILD_FOR_WIN) +SkImageEncoder* CreateImageEncoder_WIC(SkImageEncoder::Type type); +#endif + +// Typedef to make registering encoder callback easier +// This has to be defined outside SkImageEncoder. :( +typedef SkTRegistry SkImageEncoder_EncodeReg; + +#endif // SkImageEncoderPriv_DEFINED diff --git a/src/images/SkImageEncoder_Factory.cpp b/src/images/SkImageEncoder_Factory.cpp index 887ce55dda..a5ae8d0560 100644 --- a/src/images/SkImageEncoder_Factory.cpp +++ b/src/images/SkImageEncoder_Factory.cpp @@ -5,11 +5,11 @@ * found in the LICENSE file. */ -#include "SkImageEncoder.h" +#include "SkImageEncoderPriv.h" template SkImageEncoder_EncodeReg* SkImageEncoder_EncodeReg::gHead; -SkImageEncoder* SkImageEncoder::Create(Type t) { +SkImageEncoder* SkImageEncoder::Create(SkImageEncoder::Type t) { SkImageEncoder* codec = nullptr; const SkImageEncoder_EncodeReg* curr = SkImageEncoder_EncodeReg::Head(); while (curr) { diff --git a/src/images/SkJPEGImageEncoder.cpp b/src/images/SkJPEGImageEncoder.cpp index 66b2440c20..97b8bbc30d 100644 --- a/src/images/SkJPEGImageEncoder.cpp +++ b/src/images/SkJPEGImageEncoder.cpp @@ -5,20 +5,20 @@ * found in the LICENSE file. */ +#include "SkImageEncoderPriv.h" -#include "SkImageEncoder.h" +#include "SkCanvas.h" #include "SkColorPriv.h" #include "SkDither.h" +#include "SkJPEGWriteUtility.h" +#include "SkRect.h" #include "SkStream.h" #include "SkTemplates.h" #include "SkTime.h" #include "SkUtils.h" -#include "SkRect.h" -#include "SkCanvas.h" - #include -#include "SkJPEGWriteUtility.h" + extern "C" { #include "jpeglib.h" #include "jerror.h" @@ -178,7 +178,7 @@ DEFINE_ENCODER_CREATOR(JPEGImageEncoder); /////////////////////////////////////////////////////////////////////////////// static SkImageEncoder* sk_libjpeg_efactory(SkImageEncoder::Type t) { - return (SkImageEncoder::kJPEG_Type == t) ? new SkJPEGImageEncoder : nullptr; + return (SkEncodedImageFormat::kJPEG == (SkEncodedImageFormat)t) ? new SkJPEGImageEncoder : nullptr; } static SkImageEncoder_EncodeReg gEReg(sk_libjpeg_efactory); diff --git a/src/images/SkKTXImageEncoder.cpp b/src/images/SkKTXImageEncoder.cpp index 078cec6b4d..7a0ccb3a85 100644 --- a/src/images/SkKTXImageEncoder.cpp +++ b/src/images/SkKTXImageEncoder.cpp @@ -6,7 +6,7 @@ */ #include "SkColorPriv.h" -#include "SkImageEncoder.h" +#include "SkImageEncoderPriv.h" #include "SkImageGenerator.h" #include "SkPixelRef.h" #include "SkStream.h" @@ -91,7 +91,7 @@ DEFINE_ENCODER_CREATOR(KTXImageEncoder); ///////////////////////////////////////////////////////////////////////////////////////// SkImageEncoder* sk_libktx_efactory(SkImageEncoder::Type t) { - return (SkImageEncoder::kKTX_Type == t) ? new SkKTXImageEncoder : nullptr; + return (SkEncodedImageFormat::kKTX == (SkEncodedImageFormat)t) ? new SkKTXImageEncoder : nullptr; } static SkImageEncoder_EncodeReg gEReg(sk_libktx_efactory); diff --git a/src/images/SkPNGImageEncoder.cpp b/src/images/SkPNGImageEncoder.cpp index 69a53fb2a4..4fc3a10272 100644 --- a/src/images/SkPNGImageEncoder.cpp +++ b/src/images/SkPNGImageEncoder.cpp @@ -5,7 +5,7 @@ * found in the LICENSE file. */ -#include "SkImageEncoder.h" +#include "SkImageEncoderPriv.h" #include "SkColor.h" #include "SkColorPriv.h" #include "SkDither.h" @@ -356,7 +356,7 @@ DEFINE_ENCODER_CREATOR(PNGImageEncoder); /////////////////////////////////////////////////////////////////////////////// SkImageEncoder* sk_libpng_efactory(SkImageEncoder::Type t) { - return (SkImageEncoder::kPNG_Type == t) ? new SkPNGImageEncoder : nullptr; + return (SkEncodedImageFormat::kPNG == (SkEncodedImageFormat)t) ? new SkPNGImageEncoder : nullptr; } static SkImageEncoder_EncodeReg gEReg(sk_libpng_efactory); diff --git a/src/images/SkWEBPImageEncoder.cpp b/src/images/SkWEBPImageEncoder.cpp index 116608a253..27c984ac1b 100644 --- a/src/images/SkWEBPImageEncoder.cpp +++ b/src/images/SkWEBPImageEncoder.cpp @@ -15,7 +15,7 @@ */ #include "SkBitmap.h" -#include "SkImageEncoder.h" +#include "SkImageEncoderPriv.h" #include "SkColorPriv.h" #include "SkStream.h" #include "SkTemplates.h" @@ -241,7 +241,7 @@ DEFINE_ENCODER_CREATOR(WEBPImageEncoder); /////////////////////////////////////////////////////////////////////////////// static SkImageEncoder* sk_libwebp_efactory(SkImageEncoder::Type t) { - return (SkImageEncoder::kWEBP_Type == t) ? new SkWEBPImageEncoder : nullptr; + return (SkEncodedImageFormat::kWEBP == (SkEncodedImageFormat)t) ? new SkWEBPImageEncoder : nullptr; } static SkImageEncoder_EncodeReg gEReg(sk_libwebp_efactory); -- cgit v1.2.3