2016-08-16 22:11:24 +00:00
|
|
|
/*
|
|
|
|
* 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"
|
2016-11-23 15:55:18 +00:00
|
|
|
|
2016-08-16 22:11:24 +00:00
|
|
|
#include "Resources.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkData.h"
|
2017-05-03 19:16:58 +00:00
|
|
|
#include "SkImage.h"
|
2016-11-23 15:55:18 +00:00
|
|
|
#include "SkImageEncoderPriv.h"
|
2017-05-02 20:04:56 +00:00
|
|
|
#include "SkJpegEncoder.h"
|
2017-05-05 15:13:26 +00:00
|
|
|
#include "SkPngEncoder.h"
|
2017-05-05 18:02:13 +00:00
|
|
|
#include "SkWebpEncoder.h"
|
2016-08-16 22:11:24 +00:00
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
|
2016-11-23 15:55:18 +00:00
|
|
|
static SkEncodedImageFormat kTypes[] {
|
|
|
|
SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kGIF,
|
|
|
|
SkEncodedImageFormat::kBMP, SkEncodedImageFormat::kICO,
|
2016-08-16 22:11:24 +00:00
|
|
|
};
|
|
|
|
#elif defined(SK_BUILD_FOR_WIN)
|
|
|
|
// Use PNG multiple times because our WIC encoder does not support GIF, BMP, or ICO.
|
2016-11-23 15:55:18 +00:00
|
|
|
static SkEncodedImageFormat kTypes[] {
|
|
|
|
SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kPNG,
|
|
|
|
SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kPNG,
|
2016-08-16 22:11:24 +00:00
|
|
|
};
|
|
|
|
#else
|
|
|
|
// Use WEBP in place of GIF. Use PNG two extra times. We don't support GIF, BMP, or ICO.
|
2016-11-23 15:55:18 +00:00
|
|
|
static SkEncodedImageFormat kTypes[] {
|
|
|
|
SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kWEBP,
|
|
|
|
SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kPNG,
|
2016-08-16 22:11:24 +00:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2016-11-30 22:07:59 +00:00
|
|
|
static sk_sp<SkData> encode_data(SkEncodedImageFormat type, const SkBitmap& bitmap) {
|
|
|
|
SkPixmap src;
|
|
|
|
if (!bitmap.peekPixels(&src)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
SkDynamicMemoryWStream buf;
|
|
|
|
#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
|
|
|
|
return SkEncodeImageWithCG(&buf, src, type) ? buf.detachAsData() : nullptr;
|
|
|
|
#elif defined(SK_BUILD_FOR_WIN)
|
|
|
|
return SkEncodeImageWithWIC(&buf, src, type, 100) ? buf.detachAsData() : nullptr;
|
|
|
|
#else
|
|
|
|
switch (type) {
|
2017-05-05 15:13:26 +00:00
|
|
|
case SkEncodedImageFormat::kPNG: {
|
2018-07-12 18:44:27 +00:00
|
|
|
bool success = SkPngEncoder::Encode(&buf, src, SkPngEncoder::Options());
|
2017-05-05 15:13:26 +00:00
|
|
|
return success ? buf.detachAsData() : nullptr;
|
|
|
|
}
|
2017-05-02 20:04:56 +00:00
|
|
|
case SkEncodedImageFormat::kJPEG: {
|
|
|
|
bool success = SkJpegEncoder::Encode(&buf, src, SkJpegEncoder::Options());
|
|
|
|
return success ? buf.detachAsData() : nullptr;
|
|
|
|
}
|
2017-05-05 18:02:13 +00:00
|
|
|
case SkEncodedImageFormat::kWEBP: {
|
2018-07-12 18:44:27 +00:00
|
|
|
bool success = SkWebpEncoder::Encode(&buf, src, SkWebpEncoder::Options());
|
2017-05-05 18:02:13 +00:00
|
|
|
return success ? buf.detachAsData() : nullptr;
|
|
|
|
}
|
2016-11-30 22:07:59 +00:00
|
|
|
default:
|
|
|
|
SkASSERT(false);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
#endif
|
2016-11-23 15:55:18 +00:00
|
|
|
}
|
2016-08-16 22:11:24 +00:00
|
|
|
|
|
|
|
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;
|
2018-11-26 20:34:12 +00:00
|
|
|
|
|
|
|
if (!GetResourceAsBitmap("images/mandrill_256.png", &opaqueBm)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SkBitmap tmp;
|
|
|
|
if (!GetResourceAsBitmap("images/yellow_rose.png", &tmp)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
tmp.extractSubset(&premulBm, SkIRect::MakeWH(256, 256));
|
|
|
|
tmp.reset();
|
|
|
|
unpremulBm.allocPixels(premulBm.info().makeAlphaType(kUnpremul_SkAlphaType));
|
|
|
|
SkAssertResult(premulBm.readPixels(unpremulBm.pixmap()));
|
2016-08-16 22:11:24 +00:00
|
|
|
|
2016-11-23 15:55:18 +00:00
|
|
|
for (SkEncodedImageFormat type : kTypes) {
|
2016-11-30 22:07:59 +00:00
|
|
|
auto opaqueImage = SkImage::MakeFromEncoded(encode_data(type, opaqueBm));
|
|
|
|
auto premulImage = SkImage::MakeFromEncoded(encode_data(type, premulBm));
|
|
|
|
auto unpremulImage = SkImage::MakeFromEncoded(encode_data(type, unpremulBm));
|
2016-08-16 22:11:24 +00:00
|
|
|
|
|
|
|
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; )
|
|
|
|
}
|