blob: f636cb90b70bd7fff5ed1fd8e376a9fda47c75a8 [file] [log] [blame]
msarett36931c22016-08-16 15:11:24 -07001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBitmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
11#include "include/core/SkData.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040012#include "include/core/SkEncodedImageFormat.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkImage.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040014#include "include/core/SkImageInfo.h"
15#include "include/core/SkPixmap.h"
16#include "include/core/SkRect.h"
17#include "include/core/SkRefCnt.h"
18#include "include/core/SkSize.h"
19#include "include/core/SkStream.h"
20#include "include/core/SkString.h"
21#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/encode/SkJpegEncoder.h"
23#include "include/encode/SkPngEncoder.h"
24#include "include/encode/SkWebpEncoder.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "tools/Resources.h"
msarett36931c22016-08-16 15:11:24 -070026
Ben Wagner7fde8e12019-05-01 17:28:53 -040027#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) || defined(SK_BUILD_FOR_WIN)
28#include "src/images/SkImageEncoderPriv.h"
29#endif
30
msarett36931c22016-08-16 15:11:24 -070031namespace skiagm {
32
msarett36931c22016-08-16 15:11:24 -070033#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
Hal Canarydb683012016-11-23 08:55:18 -070034static SkEncodedImageFormat kTypes[] {
35 SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kGIF,
36 SkEncodedImageFormat::kBMP, SkEncodedImageFormat::kICO,
msarett36931c22016-08-16 15:11:24 -070037};
38#elif defined(SK_BUILD_FOR_WIN)
39// Use PNG multiple times because our WIC encoder does not support GIF, BMP, or ICO.
Hal Canarydb683012016-11-23 08:55:18 -070040static SkEncodedImageFormat kTypes[] {
41 SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kPNG,
42 SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kPNG,
msarett36931c22016-08-16 15:11:24 -070043};
44#else
45// Use WEBP in place of GIF. Use PNG two extra times. We don't support GIF, BMP, or ICO.
Hal Canarydb683012016-11-23 08:55:18 -070046static SkEncodedImageFormat kTypes[] {
47 SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kWEBP,
48 SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kPNG,
msarett36931c22016-08-16 15:11:24 -070049};
50#endif
51
Hal Canary1fcc4042016-11-30 17:07:59 -050052static sk_sp<SkData> encode_data(SkEncodedImageFormat type, const SkBitmap& bitmap) {
Hal Canary1fcc4042016-11-30 17:07:59 -050053 SkPixmap src;
54 if (!bitmap.peekPixels(&src)) {
55 return nullptr;
56 }
57 SkDynamicMemoryWStream buf;
58 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
59 return SkEncodeImageWithCG(&buf, src, type) ? buf.detachAsData() : nullptr;
60 #elif defined(SK_BUILD_FOR_WIN)
61 return SkEncodeImageWithWIC(&buf, src, type, 100) ? buf.detachAsData() : nullptr;
62 #else
63 switch (type) {
Matt Sarettc367d032017-05-05 11:13:26 -040064 case SkEncodedImageFormat::kPNG: {
Brian Osmanb62f50c2018-07-12 14:44:27 -040065 bool success = SkPngEncoder::Encode(&buf, src, SkPngEncoder::Options());
Matt Sarettc367d032017-05-05 11:13:26 -040066 return success ? buf.detachAsData() : nullptr;
67 }
Matt Sarett26b44df2017-05-02 16:04:56 -040068 case SkEncodedImageFormat::kJPEG: {
69 bool success = SkJpegEncoder::Encode(&buf, src, SkJpegEncoder::Options());
70 return success ? buf.detachAsData() : nullptr;
71 }
Matt Sarett04c37312017-05-05 14:02:13 -040072 case SkEncodedImageFormat::kWEBP: {
Brian Osmanb62f50c2018-07-12 14:44:27 -040073 bool success = SkWebpEncoder::Encode(&buf, src, SkWebpEncoder::Options());
Matt Sarett04c37312017-05-05 14:02:13 -040074 return success ? buf.detachAsData() : nullptr;
75 }
Hal Canary1fcc4042016-11-30 17:07:59 -050076 default:
77 SkASSERT(false);
78 return nullptr;
79 }
80 #endif
Hal Canarydb683012016-11-23 08:55:18 -070081}
msarett36931c22016-08-16 15:11:24 -070082
83class EncodePlatformGM : public GM {
84public:
85 EncodePlatformGM() {}
86
87protected:
88 SkString onShortName() override {
89 return SkString("encode-platform");
90 }
91
92 SkISize onISize() override {
93 return SkISize::Make(256 * SK_ARRAY_COUNT(kTypes), 256 * 3);
94 }
95
Chris Dalton50e24d72019-02-07 16:20:09 -070096 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
msarett36931c22016-08-16 15:11:24 -070097 SkBitmap opaqueBm, premulBm, unpremulBm;
Hal Canarybaa2a282018-11-26 15:34:12 -050098
99 if (!GetResourceAsBitmap("images/mandrill_256.png", &opaqueBm)) {
Chris Dalton50e24d72019-02-07 16:20:09 -0700100 *errorMsg = "Could not load images/mandrill_256.png.png. "
101 "Did you forget to set the resourcePath?";
102 return DrawResult::kFail;
Hal Canarybaa2a282018-11-26 15:34:12 -0500103 }
104 SkBitmap tmp;
105 if (!GetResourceAsBitmap("images/yellow_rose.png", &tmp)) {
Chris Dalton50e24d72019-02-07 16:20:09 -0700106 *errorMsg = "Could not load images/yellow_rose.png. "
107 "Did you forget to set the resourcePath?";
108 return DrawResult::kFail;
Hal Canarybaa2a282018-11-26 15:34:12 -0500109 }
110 tmp.extractSubset(&premulBm, SkIRect::MakeWH(256, 256));
111 tmp.reset();
112 unpremulBm.allocPixels(premulBm.info().makeAlphaType(kUnpremul_SkAlphaType));
113 SkAssertResult(premulBm.readPixels(unpremulBm.pixmap()));
msarett36931c22016-08-16 15:11:24 -0700114
Hal Canarydb683012016-11-23 08:55:18 -0700115 for (SkEncodedImageFormat type : kTypes) {
Hal Canary1fcc4042016-11-30 17:07:59 -0500116 auto opaqueImage = SkImage::MakeFromEncoded(encode_data(type, opaqueBm));
117 auto premulImage = SkImage::MakeFromEncoded(encode_data(type, premulBm));
118 auto unpremulImage = SkImage::MakeFromEncoded(encode_data(type, unpremulBm));
msarett36931c22016-08-16 15:11:24 -0700119
120 canvas->drawImage(opaqueImage.get(), 0.0f, 0.0f);
121 canvas->drawImage(premulImage.get(), 0.0f, 256.0f);
122 canvas->drawImage(unpremulImage.get(), 0.0f, 512.0f);
123
124 canvas->translate(256.0f, 0.0f);
125 }
Chris Dalton50e24d72019-02-07 16:20:09 -0700126 return DrawResult::kOk;
msarett36931c22016-08-16 15:11:24 -0700127 }
128
129private:
130 typedef GM INHERITED;
131};
132
133DEF_GM( return new EncodePlatformGM; )
134}