blob: 591af940f279ea3435ff17212183e8039a237c4c [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
Leon Scroggins III326b9892020-08-05 16:51:10 -040027#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) || defined(SK_BUILD_FOR_WIN) \
28 || defined(SK_ENABLE_NDK_IMAGES)
Ben Wagner7fde8e12019-05-01 17:28:53 -040029#include "src/images/SkImageEncoderPriv.h"
30#endif
31
Leon Scroggins III326b9892020-08-05 16:51:10 -040032namespace {
msarett36931c22016-08-16 15:11:24 -070033
Leon Scroggins III326b9892020-08-05 16:51:10 -040034static const struct {
35 SkEncodedImageFormat format;
36 int quality;
37} gRecs[] = {
msarett36931c22016-08-16 15:11:24 -070038#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
Leon Scroggins III326b9892020-08-05 16:51:10 -040039 { SkEncodedImageFormat::kPNG, 100},
40 { SkEncodedImageFormat::kJPEG, 100},
41 { SkEncodedImageFormat::kGIF, 100},
42 { SkEncodedImageFormat::kBMP, 100},
43 { SkEncodedImageFormat::kICO, 100},
msarett36931c22016-08-16 15:11:24 -070044#elif defined(SK_BUILD_FOR_WIN)
Leon Scroggins III326b9892020-08-05 16:51:10 -040045 // Our WIC encoder does not support GIF, BMP, or ICO.
46 { SkEncodedImageFormat::kPNG, 100},
47 { SkEncodedImageFormat::kJPEG, 100},
48 { SkEncodedImageFormat::kPNG, 100},
49 { SkEncodedImageFormat::kPNG, 100},
50 { SkEncodedImageFormat::kPNG, 100},
msarett36931c22016-08-16 15:11:24 -070051#else
Leon Scroggins III326b9892020-08-05 16:51:10 -040052 // We don't support GIF, BMP, or ICO. This applies to both NDK and SkEncoder.
53 { SkEncodedImageFormat::kPNG, 100},
54 { SkEncodedImageFormat::kJPEG, 100},
55 { SkEncodedImageFormat::kWEBP, 100}, // Lossless
56 { SkEncodedImageFormat::kWEBP, 80}, // Lossy
57 { SkEncodedImageFormat::kPNG, 100},
msarett36931c22016-08-16 15:11:24 -070058#endif
Leon Scroggins III326b9892020-08-05 16:51:10 -040059};
msarett36931c22016-08-16 15:11:24 -070060
Leon Scroggins III326b9892020-08-05 16:51:10 -040061} // anonymous namespace
62
63static sk_sp<SkData> encode_data(SkEncodedImageFormat type, const SkBitmap& bitmap, int quality) {
Hal Canary1fcc4042016-11-30 17:07:59 -050064 SkPixmap src;
65 if (!bitmap.peekPixels(&src)) {
66 return nullptr;
67 }
68 SkDynamicMemoryWStream buf;
69 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
70 return SkEncodeImageWithCG(&buf, src, type) ? buf.detachAsData() : nullptr;
71 #elif defined(SK_BUILD_FOR_WIN)
Leon Scroggins III326b9892020-08-05 16:51:10 -040072 return SkEncodeImageWithWIC(&buf, src, type, quality) ? buf.detachAsData() : nullptr;
73 #elif defined(SK_ENABLE_NDK_IMAGES)
74 return SkEncodeImageWithNDK(&buf, src, type, quality) ? buf.detachAsData() : nullptr;
Hal Canary1fcc4042016-11-30 17:07:59 -050075 #else
76 switch (type) {
Matt Sarettc367d032017-05-05 11:13:26 -040077 case SkEncodedImageFormat::kPNG: {
Brian Osmanb62f50c2018-07-12 14:44:27 -040078 bool success = SkPngEncoder::Encode(&buf, src, SkPngEncoder::Options());
Matt Sarettc367d032017-05-05 11:13:26 -040079 return success ? buf.detachAsData() : nullptr;
80 }
Matt Sarett26b44df2017-05-02 16:04:56 -040081 case SkEncodedImageFormat::kJPEG: {
82 bool success = SkJpegEncoder::Encode(&buf, src, SkJpegEncoder::Options());
83 return success ? buf.detachAsData() : nullptr;
84 }
Matt Sarett04c37312017-05-05 14:02:13 -040085 case SkEncodedImageFormat::kWEBP: {
Brian Osmanb62f50c2018-07-12 14:44:27 -040086 bool success = SkWebpEncoder::Encode(&buf, src, SkWebpEncoder::Options());
Matt Sarett04c37312017-05-05 14:02:13 -040087 return success ? buf.detachAsData() : nullptr;
88 }
Hal Canary1fcc4042016-11-30 17:07:59 -050089 default:
Leon Scroggins III326b9892020-08-05 16:51:10 -040090 SkUNREACHABLE;
Hal Canary1fcc4042016-11-30 17:07:59 -050091 }
92 #endif
Hal Canarydb683012016-11-23 08:55:18 -070093}
msarett36931c22016-08-16 15:11:24 -070094
Leon Scroggins III326b9892020-08-05 16:51:10 -040095namespace skiagm {
96
msarett36931c22016-08-16 15:11:24 -070097class EncodePlatformGM : public GM {
98public:
99 EncodePlatformGM() {}
100
101protected:
102 SkString onShortName() override {
103 return SkString("encode-platform");
104 }
105
106 SkISize onISize() override {
Leon Scroggins III326b9892020-08-05 16:51:10 -0400107 return SkISize::Make(256 * SK_ARRAY_COUNT(gRecs), 256 * 3);
msarett36931c22016-08-16 15:11:24 -0700108 }
109
Chris Dalton50e24d72019-02-07 16:20:09 -0700110 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
msarett36931c22016-08-16 15:11:24 -0700111 SkBitmap opaqueBm, premulBm, unpremulBm;
Hal Canarybaa2a282018-11-26 15:34:12 -0500112
113 if (!GetResourceAsBitmap("images/mandrill_256.png", &opaqueBm)) {
Chris Dalton50e24d72019-02-07 16:20:09 -0700114 *errorMsg = "Could not load images/mandrill_256.png.png. "
115 "Did you forget to set the resourcePath?";
116 return DrawResult::kFail;
Hal Canarybaa2a282018-11-26 15:34:12 -0500117 }
118 SkBitmap tmp;
119 if (!GetResourceAsBitmap("images/yellow_rose.png", &tmp)) {
Chris Dalton50e24d72019-02-07 16:20:09 -0700120 *errorMsg = "Could not load images/yellow_rose.png. "
121 "Did you forget to set the resourcePath?";
122 return DrawResult::kFail;
Hal Canarybaa2a282018-11-26 15:34:12 -0500123 }
124 tmp.extractSubset(&premulBm, SkIRect::MakeWH(256, 256));
125 tmp.reset();
126 unpremulBm.allocPixels(premulBm.info().makeAlphaType(kUnpremul_SkAlphaType));
127 SkAssertResult(premulBm.readPixels(unpremulBm.pixmap()));
msarett36931c22016-08-16 15:11:24 -0700128
Leon Scroggins III326b9892020-08-05 16:51:10 -0400129 for (const auto& rec : gRecs) {
130 auto fmt = rec.format; int q = rec.quality;
131 auto opaqueImage = SkImage::MakeFromEncoded(encode_data(fmt, opaqueBm, q));
132 auto premulImage = SkImage::MakeFromEncoded(encode_data(fmt, premulBm, q));
133 auto unpremulImage = SkImage::MakeFromEncoded(encode_data(fmt, unpremulBm, q));
msarett36931c22016-08-16 15:11:24 -0700134
135 canvas->drawImage(opaqueImage.get(), 0.0f, 0.0f);
136 canvas->drawImage(premulImage.get(), 0.0f, 256.0f);
137 canvas->drawImage(unpremulImage.get(), 0.0f, 512.0f);
138
139 canvas->translate(256.0f, 0.0f);
140 }
Chris Dalton50e24d72019-02-07 16:20:09 -0700141 return DrawResult::kOk;
msarett36931c22016-08-16 15:11:24 -0700142 }
143
144private:
John Stiles7571f9e2020-09-02 22:42:33 -0400145 using INHERITED = GM;
msarett36931c22016-08-16 15:11:24 -0700146};
147
148DEF_GM( return new EncodePlatformGM; )
John Stilesa6841be2020-08-06 14:11:56 -0400149} // namespace skiagm