From 36931c2b47f81db7ccf441937567c6fda72ad3e6 Mon Sep 17 00:00:00 2001 From: msarett Date: Tue, 16 Aug 2016 15:11:24 -0700 Subject: [PATCH] Add test for platform encoders, turn off platform encoders by default Clients that like WIC and CG can still use them. And we can be confident about that, since we now test WIC and CG. Let Skia always use our own encoders by default, so we can do cool, custom things on all platforms. BUG=skia: GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2250683003 Review-Url: https://codereview.chromium.org/2250683003 --- gm/encode-platform.cpp | 121 +++++++++++++++++++++++++++++++++++++++ gyp/images.gyp | 9 +-- include/core/SkImageEncoder.h | 4 +- src/images/SkForceLinking.cpp | 20 +++---- src/ports/SkImageEncoder_CG.cpp | 13 ++--- src/ports/SkImageEncoder_WIC.cpp | 18 ++---- 6 files changed, 143 insertions(+), 42 deletions(-) create mode 100644 gm/encode-platform.cpp diff --git a/gm/encode-platform.cpp b/gm/encode-platform.cpp new file mode 100644 index 0000000..90f6aa9 --- /dev/null +++ b/gm/encode-platform.cpp @@ -0,0 +1,121 @@ +/* + * Copyright 2016 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "gm.h" +#include "Resources.h" +#include "SkCanvas.h" +#include "SkData.h" +#include "SkImageEncoder.h" +#include "SkUnPreMultiply.h" + +namespace skiagm { + +static void make_opaque_256(SkBitmap* bitmap) { + GetResourceAsBitmap("mandrill_256.png", bitmap); +} + +static void make_premul_256(SkBitmap* bitmap) { + SkBitmap tmp; + GetResourceAsBitmap("yellow_rose.png", &tmp); + tmp.extractSubset(bitmap, SkIRect::MakeWH(256, 256)); + bitmap->lockPixels(); +} + +static void make_unpremul_256(SkBitmap* bitmap) { + make_premul_256(bitmap); + for (int y = 0; y < bitmap->height(); y++) { + for (int x = 0; x < bitmap->width(); x++) { + SkPMColor* pixel = bitmap->getAddr32(x, y); + *pixel = SkUnPreMultiply::UnPreMultiplyPreservingByteOrder(*pixel); + } + } + bitmap->setAlphaType(kUnpremul_SkAlphaType); +} + +static SkImageEncoder* make_encoder(SkImageEncoder::Type type) { +#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) + return CreateImageEncoder_CG(type); +#elif defined(SK_BUILD_FOR_WIN) + return CreateImageEncoder_WIC(type); +#else + switch (type) { + case SkImageEncoder::kPNG_Type: + return CreatePNGImageEncoder(); + case SkImageEncoder::kJPEG_Type: + return CreateJPEGImageEncoder(); + case SkImageEncoder::kWEBP_Type: + return CreateWEBPImageEncoder(); + default: + SkASSERT(false); + return nullptr; + } +#endif +} + +#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, +}; +#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, +}; +#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, +}; +#endif + + +class EncodePlatformGM : public GM { +public: + EncodePlatformGM() {} + +protected: + SkString onShortName() override { + return SkString("encode-platform"); + } + + SkISize onISize() override { + return SkISize::Make(256 * SK_ARRAY_COUNT(kTypes), 256 * 3); + } + + void onDraw(SkCanvas* canvas) override { + SkBitmap opaqueBm, premulBm, unpremulBm; + make_opaque_256(&opaqueBm); + make_premul_256(&premulBm); + make_unpremul_256(&unpremulBm); + + for (SkImageEncoder::Type type : kTypes) { + SkAutoTDelete encoder(make_encoder(type)); + sk_sp opaqueData(encoder->encodeData(opaqueBm, 100)); + sk_sp premulData(encoder->encodeData(premulBm, 100)); + sk_sp unpremulData(encoder->encodeData(unpremulBm, 100)); + + sk_sp opaqueImage = SkImage::MakeFromEncoded(opaqueData); + sk_sp premulImage = SkImage::MakeFromEncoded(premulData); + sk_sp unpremulImage = SkImage::MakeFromEncoded(unpremulData); + + canvas->drawImage(opaqueImage.get(), 0.0f, 0.0f); + canvas->drawImage(premulImage.get(), 0.0f, 256.0f); + canvas->drawImage(unpremulImage.get(), 0.0f, 512.0f); + + canvas->translate(256.0f, 0.0f); + } + } + +private: + typedef GM INHERITED; +}; + +DEF_GM( return new EncodePlatformGM; ) +} diff --git a/gyp/images.gyp b/gyp/images.gyp index e4ab9ed..7b1636e 100644 --- a/gyp/images.gyp +++ b/gyp/images.gyp @@ -17,6 +17,7 @@ 'libjpeg-turbo-selector.gyp:libjpeg-turbo-selector', 'etc1.gyp:libetc1', 'ktx.gyp:libSkKTX', + 'libpng.gyp:libpng', 'libwebp.gyp:libwebp', 'utils.gyp:utils', ], @@ -57,7 +58,6 @@ 'conditions': [ [ 'skia_os == "win"', { 'sources!': [ - '../src/images/SkPNGImageEncoder.cpp', '../src/images/SkGIFMovie.cpp', ], 'dependencies!': [ @@ -75,7 +75,6 @@ }], [ 'skia_os in ["mac", "ios"]', { 'sources!': [ - '../src/images/SkPNGImageEncoder.cpp', '../src/images/SkGIFMovie.cpp', ], },{ #else if skia_os != mac @@ -83,12 +82,6 @@ '../src/ports/SkImageEncoder_CG.cpp', ], }], - [ 'skia_os in ["linux", "freebsd", "openbsd", "solaris"]', { - 'dependencies': [ - 'libpng.gyp:libpng', - ], - # end libpng stuff - }], [ 'skia_os == "android"', { 'include_dirs': [ '../src/utils', diff --git a/include/core/SkImageEncoder.h b/include/core/SkImageEncoder.h index 706d0c7..8049089 100644 --- a/include/core/SkImageEncoder.h +++ b/include/core/SkImageEncoder.h @@ -106,11 +106,11 @@ DECLARE_ENCODER_CREATOR(KTXImageEncoder); DECLARE_ENCODER_CREATOR(WEBPImageEncoder); #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) -DECLARE_ENCODER_CREATOR(PNGImageEncoder_CG); +SkImageEncoder* CreateImageEncoder_CG(SkImageEncoder::Type type); #endif #if defined(SK_BUILD_FOR_WIN) -DECLARE_ENCODER_CREATOR(ImageEncoder_WIC); +SkImageEncoder* CreateImageEncoder_WIC(SkImageEncoder::Type type); #endif // Typedef to make registering encoder callback easier diff --git a/src/images/SkForceLinking.cpp b/src/images/SkForceLinking.cpp index 2afe719..81d485c 100644 --- a/src/images/SkForceLinking.cpp +++ b/src/images/SkForceLinking.cpp @@ -14,28 +14,26 @@ int SkForceLinking(bool doNotPassTrue) { if (doNotPassTrue) { SkASSERT(false); -#if !defined(SK_BUILD_FOR_MAC) && !defined(SK_BUILD_FOR_WIN) && !defined(SK_BUILD_FOR_IOS) && \ - defined(SK_HAS_JPEG_LIBRARY) +#if defined(SK_HAS_JPEG_LIBRARY) && !defined(SK_USE_CG_ENCODER) && !defined(SK_USE_WIC_ENCODER) CreateJPEGImageEncoder(); #endif -#if defined(SK_HAS_WEBP_LIBRARY) +#if defined(SK_HAS_WEBP_LIBRARY) && !defined(SK_USE_CG_ENCODER) && !defined(SK_USE_WIC_ENCODER) CreateWEBPImageEncoder(); #endif +#if defined(SK_HAS_PNG_LIBRARY) && !defined(SK_USE_CG_ENCODER) && !defined(SK_USE_WIC_ENCODER) + CreatePNGImageEncoder(); +#endif // Only link hardware texture codecs on platforms that build them. See images.gyp #ifndef SK_BUILD_FOR_ANDROID_FRAMEWORK CreateKTXImageEncoder(); #endif -#if !defined(SK_BUILD_FOR_MAC) && !defined(SK_BUILD_FOR_WIN) && !defined(SK_BUILD_FOR_IOS) && \ - defined(SK_HAS_PNG_LIBRARY) - CreatePNGImageEncoder(); -#endif -#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) - CreatePNGImageEncoder_CG(); +#if defined (SK_USE_CG_ENCODER) + CreateImageEncoder_CG(SkImageEncoder::kPNG_Type); #endif -#if defined(SK_BUILD_FOR_WIN) - CreateImageEncoder_WIC(); +#if defined (SK_USE_WIC_ENCODER) + CreateImageEncoder_WIC(SkImageEncoder::kPNG_Type); #endif return -1; } diff --git a/src/ports/SkImageEncoder_CG.cpp b/src/ports/SkImageEncoder_CG.cpp index 5b217e6..7892856 100644 --- a/src/ports/SkImageEncoder_CG.cpp +++ b/src/ports/SkImageEncoder_CG.cpp @@ -126,6 +126,7 @@ bool SkImageEncoder_CG::onEncode(SkWStream* stream, const SkBitmap& bm, /////////////////////////////////////////////////////////////////////////////// +#ifdef SK_USE_CG_ENCODER static SkImageEncoder* sk_imageencoder_cg_factory(SkImageEncoder::Type t) { switch (t) { case SkImageEncoder::kICO_Type: @@ -141,14 +142,10 @@ static SkImageEncoder* sk_imageencoder_cg_factory(SkImageEncoder::Type t) { } static SkImageEncoder_EncodeReg gEReg(sk_imageencoder_cg_factory); +#endif -class SkPNGImageEncoder_CG : public SkImageEncoder_CG { -public: - SkPNGImageEncoder_CG() - : SkImageEncoder_CG(kPNG_Type) { - } -}; - -DEFINE_ENCODER_CREATOR(PNGImageEncoder_CG); +SkImageEncoder* CreateImageEncoder_CG(SkImageEncoder::Type type) { + return new SkImageEncoder_CG(type); +} #endif//defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) diff --git a/src/ports/SkImageEncoder_WIC.cpp b/src/ports/SkImageEncoder_WIC.cpp index 9be9572..6524526 100644 --- a/src/ports/SkImageEncoder_WIC.cpp +++ b/src/ports/SkImageEncoder_WIC.cpp @@ -50,10 +50,6 @@ class SkImageEncoder_WIC : public SkImageEncoder { public: SkImageEncoder_WIC(Type t) : fType(t) {} - // DO NOT USE this constructor. This exists only so SkForceLinking can - // link the WIC image encoder. - SkImageEncoder_WIC() {} - protected: virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality); @@ -67,12 +63,6 @@ bool SkImageEncoder_WIC::onEncode(SkWStream* stream { GUID type; switch (fType) { - case kBMP_Type: - type = GUID_ContainerFormatBmp; - break; - case kICO_Type: - type = GUID_ContainerFormatIco; - break; case kJPEG_Type: type = GUID_ContainerFormatJpeg; break; @@ -228,10 +218,9 @@ bool SkImageEncoder_WIC::onEncode(SkWStream* stream /////////////////////////////////////////////////////////////////////////////// +#ifdef SK_USE_WIC_ENCODER static SkImageEncoder* sk_imageencoder_wic_factory(SkImageEncoder::Type t) { switch (t) { - case SkImageEncoder::kBMP_Type: - case SkImageEncoder::kICO_Type: case SkImageEncoder::kPNG_Type: case SkImageEncoder::kJPEG_Type: break; @@ -242,7 +231,10 @@ static SkImageEncoder* sk_imageencoder_wic_factory(SkImageEncoder::Type t) { } static SkImageEncoder_EncodeReg gEReg(sk_imageencoder_wic_factory); +#endif -DEFINE_ENCODER_CREATOR(ImageEncoder_WIC); +SkImageEncoder* CreateImageEncoder_WIC(SkImageEncoder::Type type) { + return new SkImageEncoder_WIC(type); +} #endif // defined(SK_BUILD_FOR_WIN32) -- 2.7.4