msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 1 | /* |
| 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 Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 9 | |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 10 | #include "Resources.h" |
| 11 | #include "SkCanvas.h" |
| 12 | #include "SkData.h" |
Florin Malita | ab244f0 | 2017-05-03 19:16:58 +0000 | [diff] [blame] | 13 | #include "SkImage.h" |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 14 | #include "SkImageEncoderPriv.h" |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 15 | #include "SkJpegEncoder.h" |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 16 | #include "SkPngEncoder.h" |
Matt Sarett | 04c3731 | 2017-05-05 14:02:13 -0400 | [diff] [blame] | 17 | #include "SkWebpEncoder.h" |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 18 | |
| 19 | namespace skiagm { |
| 20 | |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 21 | #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 22 | static SkEncodedImageFormat kTypes[] { |
| 23 | SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kGIF, |
| 24 | SkEncodedImageFormat::kBMP, SkEncodedImageFormat::kICO, |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 25 | }; |
| 26 | #elif defined(SK_BUILD_FOR_WIN) |
| 27 | // Use PNG multiple times because our WIC encoder does not support GIF, BMP, or ICO. |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 28 | static SkEncodedImageFormat kTypes[] { |
| 29 | SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kPNG, |
| 30 | SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kPNG, |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 31 | }; |
| 32 | #else |
| 33 | // Use WEBP in place of GIF. Use PNG two extra times. We don't support GIF, BMP, or ICO. |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 34 | static SkEncodedImageFormat kTypes[] { |
| 35 | SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kWEBP, |
| 36 | SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kPNG, |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 37 | }; |
| 38 | #endif |
| 39 | |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 40 | static sk_sp<SkData> encode_data(SkEncodedImageFormat type, const SkBitmap& bitmap) { |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 41 | SkPixmap src; |
| 42 | if (!bitmap.peekPixels(&src)) { |
| 43 | return nullptr; |
| 44 | } |
| 45 | SkDynamicMemoryWStream buf; |
| 46 | #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |
| 47 | return SkEncodeImageWithCG(&buf, src, type) ? buf.detachAsData() : nullptr; |
| 48 | #elif defined(SK_BUILD_FOR_WIN) |
| 49 | return SkEncodeImageWithWIC(&buf, src, type, 100) ? buf.detachAsData() : nullptr; |
| 50 | #else |
| 51 | switch (type) { |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 52 | case SkEncodedImageFormat::kPNG: { |
Brian Osman | b62f50c | 2018-07-12 14:44:27 -0400 | [diff] [blame] | 53 | bool success = SkPngEncoder::Encode(&buf, src, SkPngEncoder::Options()); |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 54 | return success ? buf.detachAsData() : nullptr; |
| 55 | } |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 56 | case SkEncodedImageFormat::kJPEG: { |
| 57 | bool success = SkJpegEncoder::Encode(&buf, src, SkJpegEncoder::Options()); |
| 58 | return success ? buf.detachAsData() : nullptr; |
| 59 | } |
Matt Sarett | 04c3731 | 2017-05-05 14:02:13 -0400 | [diff] [blame] | 60 | case SkEncodedImageFormat::kWEBP: { |
Brian Osman | b62f50c | 2018-07-12 14:44:27 -0400 | [diff] [blame] | 61 | bool success = SkWebpEncoder::Encode(&buf, src, SkWebpEncoder::Options()); |
Matt Sarett | 04c3731 | 2017-05-05 14:02:13 -0400 | [diff] [blame] | 62 | return success ? buf.detachAsData() : nullptr; |
| 63 | } |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 64 | default: |
| 65 | SkASSERT(false); |
| 66 | return nullptr; |
| 67 | } |
| 68 | #endif |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 69 | } |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 70 | |
| 71 | class EncodePlatformGM : public GM { |
| 72 | public: |
| 73 | EncodePlatformGM() {} |
| 74 | |
| 75 | protected: |
| 76 | SkString onShortName() override { |
| 77 | return SkString("encode-platform"); |
| 78 | } |
| 79 | |
| 80 | SkISize onISize() override { |
| 81 | return SkISize::Make(256 * SK_ARRAY_COUNT(kTypes), 256 * 3); |
| 82 | } |
| 83 | |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 84 | DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override { |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 85 | SkBitmap opaqueBm, premulBm, unpremulBm; |
Hal Canary | baa2a28 | 2018-11-26 15:34:12 -0500 | [diff] [blame] | 86 | |
| 87 | if (!GetResourceAsBitmap("images/mandrill_256.png", &opaqueBm)) { |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 88 | *errorMsg = "Could not load images/mandrill_256.png.png. " |
| 89 | "Did you forget to set the resourcePath?"; |
| 90 | return DrawResult::kFail; |
Hal Canary | baa2a28 | 2018-11-26 15:34:12 -0500 | [diff] [blame] | 91 | } |
| 92 | SkBitmap tmp; |
| 93 | if (!GetResourceAsBitmap("images/yellow_rose.png", &tmp)) { |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 94 | *errorMsg = "Could not load images/yellow_rose.png. " |
| 95 | "Did you forget to set the resourcePath?"; |
| 96 | return DrawResult::kFail; |
Hal Canary | baa2a28 | 2018-11-26 15:34:12 -0500 | [diff] [blame] | 97 | } |
| 98 | tmp.extractSubset(&premulBm, SkIRect::MakeWH(256, 256)); |
| 99 | tmp.reset(); |
| 100 | unpremulBm.allocPixels(premulBm.info().makeAlphaType(kUnpremul_SkAlphaType)); |
| 101 | SkAssertResult(premulBm.readPixels(unpremulBm.pixmap())); |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 102 | |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 103 | for (SkEncodedImageFormat type : kTypes) { |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 104 | 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)); |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 107 | |
| 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 | } |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 114 | return DrawResult::kOk; |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | private: |
| 118 | typedef GM INHERITED; |
| 119 | }; |
| 120 | |
| 121 | DEF_GM( return new EncodePlatformGM; ) |
| 122 | } |