blob: f08da73ce8d1d5f171de8ad26bdac8a93efa007c [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
8#include "gm.h"
Hal Canarydb683012016-11-23 08:55:18 -07009
msarett36931c22016-08-16 15:11:24 -070010#include "Resources.h"
11#include "SkCanvas.h"
12#include "SkData.h"
Hal Canarydb683012016-11-23 08:55:18 -070013#include "SkImageEncoderPriv.h"
msarett36931c22016-08-16 15:11:24 -070014#include "SkUnPreMultiply.h"
15
16namespace skiagm {
17
18static void make_opaque_256(SkBitmap* bitmap) {
19 GetResourceAsBitmap("mandrill_256.png", bitmap);
20}
21
22static void make_premul_256(SkBitmap* bitmap) {
23 SkBitmap tmp;
24 GetResourceAsBitmap("yellow_rose.png", &tmp);
25 tmp.extractSubset(bitmap, SkIRect::MakeWH(256, 256));
msarett36931c22016-08-16 15:11:24 -070026}
27
28static void make_unpremul_256(SkBitmap* bitmap) {
29 make_premul_256(bitmap);
30 for (int y = 0; y < bitmap->height(); y++) {
31 for (int x = 0; x < bitmap->width(); x++) {
32 SkPMColor* pixel = bitmap->getAddr32(x, y);
33 *pixel = SkUnPreMultiply::UnPreMultiplyPreservingByteOrder(*pixel);
34 }
35 }
36 bitmap->setAlphaType(kUnpremul_SkAlphaType);
37}
38
msarett36931c22016-08-16 15:11:24 -070039#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
Hal Canarydb683012016-11-23 08:55:18 -070040static SkEncodedImageFormat kTypes[] {
41 SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kGIF,
42 SkEncodedImageFormat::kBMP, SkEncodedImageFormat::kICO,
msarett36931c22016-08-16 15:11:24 -070043};
44#elif defined(SK_BUILD_FOR_WIN)
45// Use PNG multiple times because our WIC encoder does not support GIF, BMP, or ICO.
Hal Canarydb683012016-11-23 08:55:18 -070046static SkEncodedImageFormat kTypes[] {
47 SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kPNG,
48 SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kPNG,
msarett36931c22016-08-16 15:11:24 -070049};
50#else
51// 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 -070052static SkEncodedImageFormat kTypes[] {
53 SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kWEBP,
54 SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kPNG,
msarett36931c22016-08-16 15:11:24 -070055};
56#endif
57
Hal Canary1fcc4042016-11-30 17:07:59 -050058static sk_sp<SkData> encode_data(SkEncodedImageFormat type, const SkBitmap& bitmap) {
Hal Canary1fcc4042016-11-30 17:07:59 -050059 SkPixmap src;
60 if (!bitmap.peekPixels(&src)) {
61 return nullptr;
62 }
63 SkDynamicMemoryWStream buf;
64 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
65 return SkEncodeImageWithCG(&buf, src, type) ? buf.detachAsData() : nullptr;
66 #elif defined(SK_BUILD_FOR_WIN)
67 return SkEncodeImageWithWIC(&buf, src, type, 100) ? buf.detachAsData() : nullptr;
68 #else
69 switch (type) {
70 case SkEncodedImageFormat::kPNG:
Matt Sarett84014f02017-01-10 11:28:54 -050071 return SkEncodeImageAsPNG(&buf, src, SkEncodeOptions()) ? buf.detachAsData()
72 : nullptr;
Hal Canary1fcc4042016-11-30 17:07:59 -050073 case SkEncodedImageFormat::kJPEG:
74 return SkEncodeImageAsJPEG(&buf, src, 100) ? buf.detachAsData() : nullptr;
75 case SkEncodedImageFormat::kWEBP:
76 return SkEncodeImageAsWEBP(&buf, src, 100) ? buf.detachAsData() : nullptr;
77 default:
78 SkASSERT(false);
79 return nullptr;
80 }
81 #endif
Hal Canarydb683012016-11-23 08:55:18 -070082}
msarett36931c22016-08-16 15:11:24 -070083
84class EncodePlatformGM : public GM {
85public:
86 EncodePlatformGM() {}
87
88protected:
89 SkString onShortName() override {
90 return SkString("encode-platform");
91 }
92
93 SkISize onISize() override {
94 return SkISize::Make(256 * SK_ARRAY_COUNT(kTypes), 256 * 3);
95 }
96
97 void onDraw(SkCanvas* canvas) override {
98 SkBitmap opaqueBm, premulBm, unpremulBm;
99 make_opaque_256(&opaqueBm);
100 make_premul_256(&premulBm);
101 make_unpremul_256(&unpremulBm);
102
Hal Canarydb683012016-11-23 08:55:18 -0700103 for (SkEncodedImageFormat type : kTypes) {
Hal Canary1fcc4042016-11-30 17:07:59 -0500104 auto opaqueImage = SkImage::MakeFromEncoded(encode_data(type, opaqueBm));
105 auto premulImage = SkImage::MakeFromEncoded(encode_data(type, premulBm));
106 auto unpremulImage = SkImage::MakeFromEncoded(encode_data(type, unpremulBm));
msarett36931c22016-08-16 15:11:24 -0700107
108 canvas->drawImage(opaqueImage.get(), 0.0f, 0.0f);
109 canvas->drawImage(premulImage.get(), 0.0f, 256.0f);
110 canvas->drawImage(unpremulImage.get(), 0.0f, 512.0f);
111
112 canvas->translate(256.0f, 0.0f);
113 }
114 }
115
116private:
117 typedef GM INHERITED;
118};
119
120DEF_GM( return new EncodePlatformGM; )
121}