msarett | b92dc7a | 2016-04-18 12:37:46 -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 "Benchmark.h" |
| 9 | #include "Resources.h" |
| 10 | #include "SkBitmap.h" |
Hal Canary | 05784d9 | 2017-06-16 16:52:10 -0400 | [diff] [blame] | 11 | #include "SkJpegEncoder.h" |
| 12 | #include "SkPngEncoder.h" |
| 13 | #include "SkWebpEncoder.h" |
| 14 | #include "SkStream.h" |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 15 | |
msarett | b92dc7a | 2016-04-18 12:37:46 -0700 | [diff] [blame] | 16 | class EncodeBench : public Benchmark { |
| 17 | public: |
Hal Canary | 05784d9 | 2017-06-16 16:52:10 -0400 | [diff] [blame] | 18 | using Encoder = bool (*)(SkWStream*, const SkPixmap&); |
| 19 | EncodeBench(const char* filename, Encoder encoder, const char* encoderName) |
| 20 | : fSourceFilename(filename) |
| 21 | , fEncoder(encoder) |
| 22 | , fName(SkStringPrintf("Encode_%s_%s", filename, encoderName)) {} |
msarett | b92dc7a | 2016-04-18 12:37:46 -0700 | [diff] [blame] | 23 | |
| 24 | bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; } |
Hal Canary | 05784d9 | 2017-06-16 16:52:10 -0400 | [diff] [blame] | 25 | |
msarett | b92dc7a | 2016-04-18 12:37:46 -0700 | [diff] [blame] | 26 | const char* onGetName() override { return fName.c_str(); } |
Hal Canary | 05784d9 | 2017-06-16 16:52:10 -0400 | [diff] [blame] | 27 | |
msarett | b92dc7a | 2016-04-18 12:37:46 -0700 | [diff] [blame] | 28 | void onPreDraw(SkCanvas*) override { |
Hal Canary | 05784d9 | 2017-06-16 16:52:10 -0400 | [diff] [blame] | 29 | SkAssertResult(GetResourceAsBitmap(fSourceFilename, &fBitmap)); |
msarett | b92dc7a | 2016-04-18 12:37:46 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | void onDraw(int loops, SkCanvas*) override { |
Hal Canary | 05784d9 | 2017-06-16 16:52:10 -0400 | [diff] [blame] | 33 | while (loops-- > 0) { |
| 34 | SkPixmap pixmap; |
| 35 | SkAssertResult(fBitmap.peekPixels(&pixmap)); |
| 36 | SkNullWStream dst; |
| 37 | SkAssertResult(fEncoder(&dst, pixmap)); |
| 38 | SkASSERT(dst.bytesWritten() > 0); |
msarett | b92dc7a | 2016-04-18 12:37:46 -0700 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
| 42 | private: |
Hal Canary | 05784d9 | 2017-06-16 16:52:10 -0400 | [diff] [blame] | 43 | const char* fSourceFilename; |
| 44 | Encoder fEncoder; |
| 45 | SkString fName; |
| 46 | SkBitmap fBitmap; |
msarett | b92dc7a | 2016-04-18 12:37:46 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
Hal Canary | 05784d9 | 2017-06-16 16:52:10 -0400 | [diff] [blame] | 49 | static bool encode_jpeg(SkWStream* dst, const SkPixmap& src) { |
| 50 | SkJpegEncoder::Options opts; |
| 51 | opts.fQuality = 90; |
| 52 | return SkJpegEncoder::Encode(dst, src, opts); |
| 53 | } |
| 54 | |
| 55 | static bool encode_webp_lossy(SkWStream* dst, const SkPixmap& src) { |
| 56 | SkWebpEncoder::Options opts; |
| 57 | opts.fCompression = SkWebpEncoder::Compression::kLossy; |
| 58 | opts.fQuality = 90; |
| 59 | opts.fUnpremulBehavior = SkTransferFunctionBehavior::kIgnore; |
| 60 | return SkWebpEncoder::Encode(dst, src, opts); |
| 61 | } |
| 62 | |
| 63 | static bool encode_webp_lossless(SkWStream* dst, const SkPixmap& src) { |
| 64 | SkWebpEncoder::Options opts; |
| 65 | opts.fCompression = SkWebpEncoder::Compression::kLossless; |
| 66 | opts.fQuality = 90; |
| 67 | opts.fUnpremulBehavior = SkTransferFunctionBehavior::kIgnore; |
| 68 | return SkWebpEncoder::Encode(dst, src, opts); |
| 69 | } |
| 70 | |
| 71 | static bool encode_png(SkWStream* dst, |
| 72 | const SkPixmap& src, |
| 73 | SkPngEncoder::FilterFlag filters, |
| 74 | int zlibLevel) { |
| 75 | SkPngEncoder::Options opts; |
| 76 | opts.fFilterFlags = filters; |
| 77 | opts.fUnpremulBehavior = SkTransferFunctionBehavior::kIgnore; |
| 78 | opts.fZLibLevel = zlibLevel; |
| 79 | return SkPngEncoder::Encode(dst, src, opts); |
| 80 | } |
| 81 | |
| 82 | #define PNG(FLAG, ZLIBLEVEL) [](SkWStream* d, const SkPixmap& s) { \ |
| 83 | return encode_png(d, s, SkPngEncoder::FilterFlag::FLAG, ZLIBLEVEL); } |
| 84 | |
| 85 | static const char* srcs[2] = {"mandrill_512.png", "color_wheel.jpg"}; |
msarett | b92dc7a | 2016-04-18 12:37:46 -0700 | [diff] [blame] | 86 | |
| 87 | // The Android Photos app uses a quality of 90 on JPEG encodes |
Hal Canary | 05784d9 | 2017-06-16 16:52:10 -0400 | [diff] [blame] | 88 | DEF_BENCH(return new EncodeBench(srcs[0], &encode_jpeg, "JPEG")); |
| 89 | DEF_BENCH(return new EncodeBench(srcs[1], &encode_jpeg, "JPEG")); |
msarett | b92dc7a | 2016-04-18 12:37:46 -0700 | [diff] [blame] | 90 | |
| 91 | // TODO: What is the appropriate quality to use to benchmark WEBP encodes? |
Hal Canary | 05784d9 | 2017-06-16 16:52:10 -0400 | [diff] [blame] | 92 | DEF_BENCH(return new EncodeBench(srcs[0], encode_webp_lossy, "WEBP")); |
| 93 | DEF_BENCH(return new EncodeBench(srcs[1], encode_webp_lossy, "WEBP")); |
| 94 | |
| 95 | DEF_BENCH(return new EncodeBench(srcs[0], encode_webp_lossless, "WEBP_LL")); |
| 96 | DEF_BENCH(return new EncodeBench(srcs[1], encode_webp_lossless, "WEBP_LL")); |
| 97 | |
| 98 | DEF_BENCH(return new EncodeBench(srcs[0], PNG(kAll, 6), "PNG")); |
| 99 | DEF_BENCH(return new EncodeBench(srcs[0], PNG(kAll, 3), "PNG_3")); |
| 100 | DEF_BENCH(return new EncodeBench(srcs[0], PNG(kAll, 1), "PNG_1")); |
| 101 | |
| 102 | DEF_BENCH(return new EncodeBench(srcs[0], PNG(kSub, 6), "PNG_6s")); |
| 103 | DEF_BENCH(return new EncodeBench(srcs[0], PNG(kSub, 3), "PNG_3s")); |
| 104 | DEF_BENCH(return new EncodeBench(srcs[0], PNG(kSub, 1), "PNG_1s")); |
| 105 | |
| 106 | DEF_BENCH(return new EncodeBench(srcs[0], PNG(kNone, 6), "PNG_6n")); |
| 107 | DEF_BENCH(return new EncodeBench(srcs[0], PNG(kNone, 3), "PNG_3n")); |
| 108 | DEF_BENCH(return new EncodeBench(srcs[0], PNG(kNone, 1), "PNG_1n")); |
| 109 | |
| 110 | DEF_BENCH(return new EncodeBench(srcs[1], PNG(kAll, 6), "PNG")); |
| 111 | DEF_BENCH(return new EncodeBench(srcs[1], PNG(kAll, 3), "PNG_3")); |
| 112 | DEF_BENCH(return new EncodeBench(srcs[1], PNG(kAll, 1), "PNG_1")); |
| 113 | |
| 114 | DEF_BENCH(return new EncodeBench(srcs[1], PNG(kSub, 6), "PNG_6s")); |
| 115 | DEF_BENCH(return new EncodeBench(srcs[1], PNG(kSub, 3), "PNG_3s")); |
| 116 | DEF_BENCH(return new EncodeBench(srcs[1], PNG(kSub, 1), "PNG_1s")); |
| 117 | |
| 118 | DEF_BENCH(return new EncodeBench(srcs[1], PNG(kNone, 6), "PNG_6n")); |
| 119 | DEF_BENCH(return new EncodeBench(srcs[1], PNG(kNone, 3), "PNG_3n")); |
| 120 | DEF_BENCH(return new EncodeBench(srcs[1], PNG(kNone, 1), "PNG_1n")); |
| 121 | |
| 122 | #undef PNG |