SkImageEncoder: simplify API
(re-land 248ff02 & 2cb6cb7, with changes)
- 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
Change-Id: I47d451e50be4d5c6c130869c7fa7c2857243d9f0
Reviewed-on: https://skia-review.googlesource.com/4909
Reviewed-by: Mike Reed <reed@google.com>
Reviewed-by: Leon Scroggins <scroggo@google.com>
Reviewed-on: https://skia-review.googlesource.com/5186
Commit-Queue: Hal Canary <halcanary@google.com>
Reviewed-by: Hal Canary <halcanary@google.com>
diff --git a/gm/encode-platform.cpp b/gm/encode-platform.cpp
index 09b64cc..0357298 100644
--- a/gm/encode-platform.cpp
+++ b/gm/encode-platform.cpp
@@ -6,10 +6,11 @@
*/
#include "gm.h"
+
#include "Resources.h"
#include "SkCanvas.h"
#include "SkData.h"
-#include "SkImageEncoder.h"
+#include "SkImageEncoderPriv.h"
#include "SkUnPreMultiply.h"
namespace skiagm {
@@ -36,18 +37,18 @@
bitmap->setAlphaType(kUnpremul_SkAlphaType);
}
-static SkImageEncoder* make_encoder(SkImageEncoder::Type type) {
+static SkImageEncoder* make_encoder(SkEncodedImageFormat type) {
#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
- return CreateImageEncoder_CG(type);
+ return CreateImageEncoder_CG((SkImageEncoder::Type)type);
#elif defined(SK_BUILD_FOR_WIN)
- return CreateImageEncoder_WIC(type);
+ return CreateImageEncoder_WIC((SkImageEncoder::Type)type);
#else
switch (type) {
- case SkImageEncoder::kPNG_Type:
+ case SkEncodedImageFormat::kPNG:
return CreatePNGImageEncoder();
- case SkImageEncoder::kJPEG_Type:
+ case SkEncodedImageFormat::kJPEG:
return CreateJPEGImageEncoder();
- case SkImageEncoder::kWEBP_Type:
+ case SkEncodedImageFormat::kWEBP:
return CreateWEBPImageEncoder();
default:
SkASSERT(false);
@@ -57,24 +58,28 @@
}
#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
-static SkImageEncoder::Type kTypes[] {
- SkImageEncoder::kPNG_Type, SkImageEncoder::kJPEG_Type, SkImageEncoder::kGIF_Type,
- SkImageEncoder::kBMP_Type, SkImageEncoder::kICO_Type,
+static SkEncodedImageFormat kTypes[] {
+ SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kGIF,
+ SkEncodedImageFormat::kBMP, SkEncodedImageFormat::kICO,
};
#elif defined(SK_BUILD_FOR_WIN)
// Use PNG multiple times because our WIC encoder does not support GIF, BMP, or ICO.
-static SkImageEncoder::Type kTypes[] {
- SkImageEncoder::kPNG_Type, SkImageEncoder::kJPEG_Type, SkImageEncoder::kPNG_Type,
- SkImageEncoder::kPNG_Type, SkImageEncoder::kPNG_Type,
+static SkEncodedImageFormat kTypes[] {
+ SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kPNG,
+ SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kPNG,
};
#else
// Use WEBP in place of GIF. Use PNG two extra times. We don't support GIF, BMP, or ICO.
-static SkImageEncoder::Type kTypes[] {
- SkImageEncoder::kPNG_Type, SkImageEncoder::kJPEG_Type, SkImageEncoder::kWEBP_Type,
- SkImageEncoder::kPNG_Type, SkImageEncoder::kPNG_Type,
+static SkEncodedImageFormat kTypes[] {
+ SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kWEBP,
+ SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kPNG,
};
#endif
+static sk_sp<SkData> encode_data(std::unique_ptr<SkImageEncoder>& encoder, const SkBitmap& src) {
+ SkDynamicMemoryWStream buffer;
+ return encoder->encodeStream(&buffer, src, 100) ? buffer.detachAsData() : nullptr;
+}
class EncodePlatformGM : public GM {
public:
@@ -95,15 +100,12 @@
make_premul_256(&premulBm);
make_unpremul_256(&unpremulBm);
- for (SkImageEncoder::Type type : kTypes) {
+ for (SkEncodedImageFormat type : kTypes) {
std::unique_ptr<SkImageEncoder> encoder(make_encoder(type));
- sk_sp<SkData> opaqueData(encoder->encodeData(opaqueBm, 100));
- sk_sp<SkData> premulData(encoder->encodeData(premulBm, 100));
- sk_sp<SkData> unpremulData(encoder->encodeData(unpremulBm, 100));
- sk_sp<SkImage> opaqueImage = SkImage::MakeFromEncoded(opaqueData);
- sk_sp<SkImage> premulImage = SkImage::MakeFromEncoded(premulData);
- sk_sp<SkImage> unpremulImage = SkImage::MakeFromEncoded(unpremulData);
+ auto opaqueImage = SkImage::MakeFromEncoded(encode_data(encoder, opaqueBm));
+ auto premulImage = SkImage::MakeFromEncoded(encode_data(encoder, premulBm));
+ auto unpremulImage = SkImage::MakeFromEncoded(encode_data(encoder, unpremulBm));
canvas->drawImage(opaqueImage.get(), 0.0f, 0.0f);
canvas->drawImage(premulImage.get(), 0.0f, 256.0f);