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 | |
Leon Scroggins III | 326b989 | 2020-08-05 16:51:10 -0400 | [diff] [blame] | 27 | #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) || defined(SK_BUILD_FOR_WIN) \ |
| 28 | || defined(SK_ENABLE_NDK_IMAGES) |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 29 | #include "src/images/SkImageEncoderPriv.h" |
| 30 | #endif |
| 31 | |
Leon Scroggins III | 326b989 | 2020-08-05 16:51:10 -0400 | [diff] [blame] | 32 | namespace { |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 33 | |
Leon Scroggins III | 326b989 | 2020-08-05 16:51:10 -0400 | [diff] [blame] | 34 | static const struct { |
| 35 | SkEncodedImageFormat format; |
| 36 | int quality; |
| 37 | } gRecs[] = { |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 38 | #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |
Leon Scroggins III | 326b989 | 2020-08-05 16:51:10 -0400 | [diff] [blame] | 39 | { SkEncodedImageFormat::kPNG, 100}, |
| 40 | { SkEncodedImageFormat::kJPEG, 100}, |
| 41 | { SkEncodedImageFormat::kGIF, 100}, |
| 42 | { SkEncodedImageFormat::kBMP, 100}, |
| 43 | { SkEncodedImageFormat::kICO, 100}, |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 44 | #elif defined(SK_BUILD_FOR_WIN) |
Leon Scroggins III | 326b989 | 2020-08-05 16:51:10 -0400 | [diff] [blame] | 45 | // 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}, |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 51 | #else |
Leon Scroggins III | 326b989 | 2020-08-05 16:51:10 -0400 | [diff] [blame] | 52 | // 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}, |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 58 | #endif |
Leon Scroggins III | 326b989 | 2020-08-05 16:51:10 -0400 | [diff] [blame] | 59 | }; |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 60 | |
Leon Scroggins III | 326b989 | 2020-08-05 16:51:10 -0400 | [diff] [blame] | 61 | } // anonymous namespace |
| 62 | |
| 63 | static sk_sp<SkData> encode_data(SkEncodedImageFormat type, const SkBitmap& bitmap, int quality) { |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 64 | 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 III | 326b989 | 2020-08-05 16:51:10 -0400 | [diff] [blame] | 72 | 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 Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 75 | #else |
| 76 | switch (type) { |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 77 | case SkEncodedImageFormat::kPNG: { |
Brian Osman | b62f50c | 2018-07-12 14:44:27 -0400 | [diff] [blame] | 78 | bool success = SkPngEncoder::Encode(&buf, src, SkPngEncoder::Options()); |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 79 | return success ? buf.detachAsData() : nullptr; |
| 80 | } |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 81 | case SkEncodedImageFormat::kJPEG: { |
| 82 | bool success = SkJpegEncoder::Encode(&buf, src, SkJpegEncoder::Options()); |
| 83 | return success ? buf.detachAsData() : nullptr; |
| 84 | } |
Matt Sarett | 04c3731 | 2017-05-05 14:02:13 -0400 | [diff] [blame] | 85 | case SkEncodedImageFormat::kWEBP: { |
Brian Osman | b62f50c | 2018-07-12 14:44:27 -0400 | [diff] [blame] | 86 | bool success = SkWebpEncoder::Encode(&buf, src, SkWebpEncoder::Options()); |
Matt Sarett | 04c3731 | 2017-05-05 14:02:13 -0400 | [diff] [blame] | 87 | return success ? buf.detachAsData() : nullptr; |
| 88 | } |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 89 | default: |
Leon Scroggins III | 326b989 | 2020-08-05 16:51:10 -0400 | [diff] [blame] | 90 | SkUNREACHABLE; |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 91 | } |
| 92 | #endif |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 93 | } |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 94 | |
Leon Scroggins III | 326b989 | 2020-08-05 16:51:10 -0400 | [diff] [blame] | 95 | namespace skiagm { |
| 96 | |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 97 | class EncodePlatformGM : public GM { |
| 98 | public: |
| 99 | EncodePlatformGM() {} |
| 100 | |
| 101 | protected: |
| 102 | SkString onShortName() override { |
| 103 | return SkString("encode-platform"); |
| 104 | } |
| 105 | |
| 106 | SkISize onISize() override { |
Leon Scroggins III | 326b989 | 2020-08-05 16:51:10 -0400 | [diff] [blame] | 107 | return SkISize::Make(256 * SK_ARRAY_COUNT(gRecs), 256 * 3); |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 110 | DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override { |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 111 | SkBitmap opaqueBm, premulBm, unpremulBm; |
Hal Canary | baa2a28 | 2018-11-26 15:34:12 -0500 | [diff] [blame] | 112 | |
| 113 | if (!GetResourceAsBitmap("images/mandrill_256.png", &opaqueBm)) { |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 114 | *errorMsg = "Could not load images/mandrill_256.png.png. " |
| 115 | "Did you forget to set the resourcePath?"; |
| 116 | return DrawResult::kFail; |
Hal Canary | baa2a28 | 2018-11-26 15:34:12 -0500 | [diff] [blame] | 117 | } |
| 118 | SkBitmap tmp; |
| 119 | if (!GetResourceAsBitmap("images/yellow_rose.png", &tmp)) { |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 120 | *errorMsg = "Could not load images/yellow_rose.png. " |
| 121 | "Did you forget to set the resourcePath?"; |
| 122 | return DrawResult::kFail; |
Hal Canary | baa2a28 | 2018-11-26 15:34:12 -0500 | [diff] [blame] | 123 | } |
| 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())); |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 128 | |
Leon Scroggins III | 326b989 | 2020-08-05 16:51:10 -0400 | [diff] [blame] | 129 | 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)); |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 134 | |
| 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 Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 141 | return DrawResult::kOk; |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 145 | using INHERITED = GM; |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | DEF_GM( return new EncodePlatformGM; ) |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 149 | } // namespace skiagm |