| 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 "bench/Benchmark.h" |
| 9 |
#include "include/core/SkBitmap.h" |
| 10 |
#include "include/core/SkColorType.h" |
| 11 |
#include "include/core/SkStream.h" |
| 12 |
#include "include/encode/SkJpegEncoder.h" |
| 13 |
#include "include/encode/SkPngEncoder.h" |
| 14 |
#include "include/encode/SkWebpEncoder.h" |
| 15 |
#include "tools/DecodeUtils.h" |
| 16 |
|
| 17 |
// Like other Benchmark subclasses, Encoder benchmarks are run by: |
| 18 |
// nanobench --match ^Encode_ |
| 19 |
// |
| 20 |
// There is no corresponding DecodeBench class. Decoder benchmarks are run by: |
| 21 |
// nanobench --benchType skcodec --images your_images_directory |
| 22 |
|
| 23 |
class EncodeBench : public Benchmark { |
| 24 |
public: |
| 25 |
using Encoder = bool (*)(SkWStream*, const SkPixmap&); |
| 26 |
EncodeBench(const char* filename, Encoder encoder, const char* encoderName, SkColorType colorType) |
| 27 |
: fSourceFilename(filename) |
| 28 |
, fEncoder(encoder) |
| 29 |
, fName(SkStringPrintf("Encode_%s_%s_%d", filename, encoderName, static_cast<int>(colorType))) |
| 30 |
, fColorType(colorType) {} |
| 31 |
|
| 32 |
bool isSuitableFor(Backend backend) override { return backend == Backend::kNonRendering; } |
| 33 |
|
| 34 |
const char* onGetName() override { return fName.c_str(); } |
| 35 |
|
| 36 |
void onDelayedSetup() override { |
| 37 |
SkAssertResult( |
| 38 |
ToolUtils::GetResourceAsBitmapWithColortype(fSourceFilename, &fBitmap, fColorType) |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
void onDraw(int loops, SkCanvas*) override { |
| 43 |
while (loops-- > 0) { |
| 44 |
SkPixmap pixmap; |
| 45 |
SkAssertResult(fBitmap.peekPixels(&pixmap)); |
| 46 |
SkNullWStream dst; |
| 47 |
SkAssertResult(fEncoder(&dst, pixmap)); |
| 48 |
SkASSERT(dst.bytesWritten() > 0); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
private: |
| 53 |
const char* fSourceFilename; |
| 54 |
Encoder fEncoder; |
| 55 |
SkString fName; |
| 56 |
SkBitmap fBitmap; |
| 57 |
SkColorType fColorType; |
| 58 |
}; |
| 59 |
|
| 60 |
static bool encode_jpeg(SkWStream* dst, const SkPixmap& src) { |
| 61 |
SkJpegEncoder::Options opts; |
| 62 |
opts.fQuality = 90; |
| 63 |
return SkJpegEncoder::Encode(dst, src, opts); |
| 64 |
} |
| 65 |
|
| 66 |
static bool encode_webp_lossy(SkWStream* dst, const SkPixmap& src) { |
| 67 |
SkWebpEncoder::Options opts; |
| 68 |
opts.fCompression = SkWebpEncoder::Compression::kLossy; |
| 69 |
opts.fQuality = 90; |
| 70 |
return SkWebpEncoder::Encode(dst, src, opts); |
| 71 |
} |
| 72 |
|
| 73 |
static bool encode_webp_lossless(SkWStream* dst, const SkPixmap& src) { |
| 74 |
SkWebpEncoder::Options opts; |
| 75 |
opts.fCompression = SkWebpEncoder::Compression::kLossless; |
| 76 |
opts.fQuality = 90; |
| 77 |
return SkWebpEncoder::Encode(dst, src, opts); |
| 78 |
} |
| 79 |
|
| 80 |
static bool encode_png(SkWStream* dst, |
| 81 |
const SkPixmap& src, |
| 82 |
SkPngEncoder::FilterFlag filters, |
| 83 |
int zlibLevel) { |
| 84 |
SkPngEncoder::Options opts; |
| 85 |
opts.fFilterFlags = filters; |
| 86 |
opts.fZLibLevel = zlibLevel; |
| 87 |
return SkPngEncoder::Encode(dst, src, opts); |
| 88 |
} |
| 89 |
|
| 90 |
#define PNG(FLAG, ZLIBLEVEL) [](SkWStream* d, const SkPixmap& s) { \ |
| 91 |
return encode_png(d, s, SkPngEncoder::FilterFlag::FLAG, ZLIBLEVEL); } |
| 92 |
|
| 93 |
static const char* srcs[2] = {"images/mandrill_512.png", "images/color_wheel.jpg"}; |
| 94 |
|
| 95 |
// The Android Photos app uses a quality of 90 on JPEG encodes |
| 96 |
DEF_BENCH(return new EncodeBench(srcs[0], &encode_jpeg, "JPEG", kRGBA_8888_SkColorType)) |
| 97 |
DEF_BENCH(return new EncodeBench(srcs[1], &encode_jpeg, "JPEG", kRGBA_8888_SkColorType)) |
| 98 |
|
| 99 |
// TODO: What is the appropriate quality to use to benchmark WEBP encodes? |
| 100 |
DEF_BENCH(return new EncodeBench(srcs[0], encode_webp_lossy, "WEBP", kRGBA_8888_SkColorType)) |
| 101 |
DEF_BENCH(return new EncodeBench(srcs[1], encode_webp_lossy, "WEBP", kRGBA_8888_SkColorType)) |
| 102 |
|
| 103 |
DEF_BENCH(return new EncodeBench(srcs[0], encode_webp_lossless, "WEBP_LL", kRGBA_8888_SkColorType)) |
| 104 |
DEF_BENCH(return new EncodeBench(srcs[1], encode_webp_lossless, "WEBP_LL", kRGBA_8888_SkColorType)) |
| 105 |
|
| 106 |
DEF_BENCH(return new EncodeBench(srcs[0], PNG(kAll, 6), "PNG", kRGBA_8888_SkColorType)) |
| 107 |
DEF_BENCH(return new EncodeBench(srcs[0], PNG(kAll, 3), "PNG_3", kRGBA_8888_SkColorType)) |
| 108 |
DEF_BENCH(return new EncodeBench(srcs[0], PNG(kAll, 1), "PNG_1", kRGBA_8888_SkColorType)) |
| 109 |
|
| 110 |
DEF_BENCH(return new EncodeBench(srcs[0], PNG(kSub, 6), "PNG_6s", kRGBA_8888_SkColorType)) |
| 111 |
DEF_BENCH(return new EncodeBench(srcs[0], PNG(kSub, 3), "PNG_3s", kRGBA_8888_SkColorType)) |
| 112 |
DEF_BENCH(return new EncodeBench(srcs[0], PNG(kSub, 1), "PNG_1s", kRGBA_8888_SkColorType)) |
| 113 |
|
| 114 |
DEF_BENCH(return new EncodeBench(srcs[0], PNG(kNone, 6), "PNG_6n", kRGBA_8888_SkColorType)) |
| 115 |
DEF_BENCH(return new EncodeBench(srcs[0], PNG(kNone, 3), "PNG_3n", kRGBA_8888_SkColorType)) |
| 116 |
DEF_BENCH(return new EncodeBench(srcs[0], PNG(kNone, 1), "PNG_1n", kRGBA_8888_SkColorType)) |
| 117 |
|
| 118 |
DEF_BENCH(return new EncodeBench(srcs[1], PNG(kAll, 6), "PNG", kRGBA_8888_SkColorType)) |
| 119 |
DEF_BENCH(return new EncodeBench(srcs[1], PNG(kAll, 3), "PNG_3", kRGBA_8888_SkColorType)) |
| 120 |
DEF_BENCH(return new EncodeBench(srcs[1], PNG(kAll, 1), "PNG_1", kRGBA_8888_SkColorType)) |
| 121 |
|
| 122 |
DEF_BENCH(return new EncodeBench(srcs[1], PNG(kSub, 6), "PNG_6s", kRGBA_8888_SkColorType)) |
| 123 |
DEF_BENCH(return new EncodeBench(srcs[1], PNG(kSub, 3), "PNG_3s", kRGBA_8888_SkColorType)) |
| 124 |
DEF_BENCH(return new EncodeBench(srcs[1], PNG(kSub, 1), "PNG_1s", kRGBA_8888_SkColorType)) |
| 125 |
|
| 126 |
DEF_BENCH(return new EncodeBench(srcs[1], PNG(kNone, 6), "PNG_6n", kRGBA_8888_SkColorType)) |
| 127 |
DEF_BENCH(return new EncodeBench(srcs[1], PNG(kNone, 3), "PNG_3n", kRGBA_8888_SkColorType)) |
| 128 |
DEF_BENCH(return new EncodeBench(srcs[1], PNG(kNone, 1), "PNG_1n", kRGBA_8888_SkColorType)) |
| 129 |
|
| 130 |
DEF_BENCH(return new EncodeBench(srcs[0], PNG(kAll, 6), "PNG", kRGBA_F16_SkColorType)) |
| 131 |
DEF_BENCH(return new EncodeBench(srcs[1], PNG(kAll, 6), "PNG", kRGBA_F16_SkColorType)) |
| 132 |
|
| 133 |
DEF_BENCH(return new EncodeBench(srcs[0], PNG(kAll, 6), "PNG", kRGB_565_SkColorType)) |
| 134 |
DEF_BENCH(return new EncodeBench(srcs[1], PNG(kAll, 6), "PNG", kRGB_565_SkColorType)) |
| 135 |
|
| 136 |
#undef PNG |