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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkBitmap.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkData.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 12 | #include "include/core/SkEncodedImageFormat.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkImage.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 14 | #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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "include/encode/SkJpegEncoder.h" |
| 23 | #include "include/encode/SkPngEncoder.h" |
| 24 | #include "include/encode/SkWebpEncoder.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "tools/Resources.h" |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 26 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 27 | #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 | |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 31 | namespace skiagm { |
| 32 | |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 33 | #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 34 | static SkEncodedImageFormat kTypes[] { |
| 35 | SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kGIF, |
| 36 | SkEncodedImageFormat::kBMP, SkEncodedImageFormat::kICO, |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 37 | }; |
| 38 | #elif defined(SK_BUILD_FOR_WIN) |
| 39 | // 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] | 40 | static SkEncodedImageFormat kTypes[] { |
| 41 | SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kPNG, |
| 42 | SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kPNG, |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 43 | }; |
| 44 | #else |
| 45 | // 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] | 46 | static SkEncodedImageFormat kTypes[] { |
| 47 | SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kWEBP, |
| 48 | SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kPNG, |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 49 | }; |
| 50 | #endif |
| 51 | |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 52 | static sk_sp<SkData> encode_data(SkEncodedImageFormat type, const SkBitmap& bitmap) { |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 53 | 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 Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 64 | case SkEncodedImageFormat::kPNG: { |
Brian Osman | b62f50c | 2018-07-12 14:44:27 -0400 | [diff] [blame] | 65 | bool success = SkPngEncoder::Encode(&buf, src, SkPngEncoder::Options()); |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 66 | return success ? buf.detachAsData() : nullptr; |
| 67 | } |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 68 | case SkEncodedImageFormat::kJPEG: { |
| 69 | bool success = SkJpegEncoder::Encode(&buf, src, SkJpegEncoder::Options()); |
| 70 | return success ? buf.detachAsData() : nullptr; |
| 71 | } |
Matt Sarett | 04c3731 | 2017-05-05 14:02:13 -0400 | [diff] [blame] | 72 | case SkEncodedImageFormat::kWEBP: { |
Brian Osman | b62f50c | 2018-07-12 14:44:27 -0400 | [diff] [blame] | 73 | bool success = SkWebpEncoder::Encode(&buf, src, SkWebpEncoder::Options()); |
Matt Sarett | 04c3731 | 2017-05-05 14:02:13 -0400 | [diff] [blame] | 74 | return success ? buf.detachAsData() : nullptr; |
| 75 | } |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 76 | default: |
| 77 | SkASSERT(false); |
| 78 | return nullptr; |
| 79 | } |
| 80 | #endif |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 81 | } |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 82 | |
| 83 | class EncodePlatformGM : public GM { |
| 84 | public: |
| 85 | EncodePlatformGM() {} |
| 86 | |
| 87 | protected: |
| 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 Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 96 | DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override { |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 97 | SkBitmap opaqueBm, premulBm, unpremulBm; |
Hal Canary | baa2a28 | 2018-11-26 15:34:12 -0500 | [diff] [blame] | 98 | |
| 99 | if (!GetResourceAsBitmap("images/mandrill_256.png", &opaqueBm)) { |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 100 | *errorMsg = "Could not load images/mandrill_256.png.png. " |
| 101 | "Did you forget to set the resourcePath?"; |
| 102 | return DrawResult::kFail; |
Hal Canary | baa2a28 | 2018-11-26 15:34:12 -0500 | [diff] [blame] | 103 | } |
| 104 | SkBitmap tmp; |
| 105 | if (!GetResourceAsBitmap("images/yellow_rose.png", &tmp)) { |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 106 | *errorMsg = "Could not load images/yellow_rose.png. " |
| 107 | "Did you forget to set the resourcePath?"; |
| 108 | return DrawResult::kFail; |
Hal Canary | baa2a28 | 2018-11-26 15:34:12 -0500 | [diff] [blame] | 109 | } |
| 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())); |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 114 | |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 115 | for (SkEncodedImageFormat type : kTypes) { |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 116 | 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)); |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 119 | |
| 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 Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 126 | return DrawResult::kOk; |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | private: |
| 130 | typedef GM INHERITED; |
| 131 | }; |
| 132 | |
| 133 | DEF_GM( return new EncodePlatformGM; ) |
| 134 | } |