blob: d17ebed147e6d81ee8bd0680c6877d9f551940c1 [file] [log] [blame]
msarettb92dc7a2016-04-18 12:37:46 -07001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "bench/Benchmark.h"
9#include "include/core/SkBitmap.h"
10#include "include/core/SkStream.h"
11#include "include/encode/SkJpegEncoder.h"
12#include "include/encode/SkPngEncoder.h"
13#include "include/encode/SkWebpEncoder.h"
14#include "tools/Resources.h"
Hal Canarydb683012016-11-23 08:55:18 -070015
Nigel Tao9d4f4502018-11-08 09:52:39 +110016// Like other Benchmark subclasses, Encoder benchmarks are run by:
17// nanobench --match ^Encode_
18//
19// There is no corresponding DecodeBench class. Decoder benchmarks are run by:
20// nanobench --benchType skcodec --images your_images_directory
21
msarettb92dc7a2016-04-18 12:37:46 -070022class EncodeBench : public Benchmark {
23public:
Hal Canary05784d92017-06-16 16:52:10 -040024 using Encoder = bool (*)(SkWStream*, const SkPixmap&);
25 EncodeBench(const char* filename, Encoder encoder, const char* encoderName)
26 : fSourceFilename(filename)
27 , fEncoder(encoder)
28 , fName(SkStringPrintf("Encode_%s_%s", filename, encoderName)) {}
msarettb92dc7a2016-04-18 12:37:46 -070029
30 bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; }
Hal Canary05784d92017-06-16 16:52:10 -040031
msarettb92dc7a2016-04-18 12:37:46 -070032 const char* onGetName() override { return fName.c_str(); }
Hal Canary05784d92017-06-16 16:52:10 -040033
Nigel Tao37264982018-10-18 10:42:28 +110034 void onDelayedSetup() override {
Hal Canary05784d92017-06-16 16:52:10 -040035 SkAssertResult(GetResourceAsBitmap(fSourceFilename, &fBitmap));
msarettb92dc7a2016-04-18 12:37:46 -070036 }
37
38 void onDraw(int loops, SkCanvas*) override {
Hal Canary05784d92017-06-16 16:52:10 -040039 while (loops-- > 0) {
40 SkPixmap pixmap;
41 SkAssertResult(fBitmap.peekPixels(&pixmap));
42 SkNullWStream dst;
43 SkAssertResult(fEncoder(&dst, pixmap));
44 SkASSERT(dst.bytesWritten() > 0);
msarettb92dc7a2016-04-18 12:37:46 -070045 }
46 }
47
48private:
Hal Canary05784d92017-06-16 16:52:10 -040049 const char* fSourceFilename;
50 Encoder fEncoder;
51 SkString fName;
52 SkBitmap fBitmap;
msarettb92dc7a2016-04-18 12:37:46 -070053};
54
Hal Canary05784d92017-06-16 16:52:10 -040055static bool encode_jpeg(SkWStream* dst, const SkPixmap& src) {
56 SkJpegEncoder::Options opts;
57 opts.fQuality = 90;
58 return SkJpegEncoder::Encode(dst, src, opts);
59}
60
61static bool encode_webp_lossy(SkWStream* dst, const SkPixmap& src) {
62 SkWebpEncoder::Options opts;
63 opts.fCompression = SkWebpEncoder::Compression::kLossy;
64 opts.fQuality = 90;
Hal Canary05784d92017-06-16 16:52:10 -040065 return SkWebpEncoder::Encode(dst, src, opts);
66}
67
68static bool encode_webp_lossless(SkWStream* dst, const SkPixmap& src) {
69 SkWebpEncoder::Options opts;
70 opts.fCompression = SkWebpEncoder::Compression::kLossless;
71 opts.fQuality = 90;
Hal Canary05784d92017-06-16 16:52:10 -040072 return SkWebpEncoder::Encode(dst, src, opts);
73}
74
75static bool encode_png(SkWStream* dst,
76 const SkPixmap& src,
77 SkPngEncoder::FilterFlag filters,
78 int zlibLevel) {
79 SkPngEncoder::Options opts;
80 opts.fFilterFlags = filters;
Hal Canary05784d92017-06-16 16:52:10 -040081 opts.fZLibLevel = zlibLevel;
82 return SkPngEncoder::Encode(dst, src, opts);
83}
84
85#define PNG(FLAG, ZLIBLEVEL) [](SkWStream* d, const SkPixmap& s) { \
86 return encode_png(d, s, SkPngEncoder::FilterFlag::FLAG, ZLIBLEVEL); }
87
Hal Canaryc465d132017-12-08 10:21:31 -050088static const char* srcs[2] = {"images/mandrill_512.png", "images/color_wheel.jpg"};
msarettb92dc7a2016-04-18 12:37:46 -070089
90// The Android Photos app uses a quality of 90 on JPEG encodes
Hal Canary05784d92017-06-16 16:52:10 -040091DEF_BENCH(return new EncodeBench(srcs[0], &encode_jpeg, "JPEG"));
92DEF_BENCH(return new EncodeBench(srcs[1], &encode_jpeg, "JPEG"));
msarettb92dc7a2016-04-18 12:37:46 -070093
94// TODO: What is the appropriate quality to use to benchmark WEBP encodes?
Hal Canary05784d92017-06-16 16:52:10 -040095DEF_BENCH(return new EncodeBench(srcs[0], encode_webp_lossy, "WEBP"));
96DEF_BENCH(return new EncodeBench(srcs[1], encode_webp_lossy, "WEBP"));
97
98DEF_BENCH(return new EncodeBench(srcs[0], encode_webp_lossless, "WEBP_LL"));
99DEF_BENCH(return new EncodeBench(srcs[1], encode_webp_lossless, "WEBP_LL"));
100
101DEF_BENCH(return new EncodeBench(srcs[0], PNG(kAll, 6), "PNG"));
102DEF_BENCH(return new EncodeBench(srcs[0], PNG(kAll, 3), "PNG_3"));
103DEF_BENCH(return new EncodeBench(srcs[0], PNG(kAll, 1), "PNG_1"));
104
105DEF_BENCH(return new EncodeBench(srcs[0], PNG(kSub, 6), "PNG_6s"));
106DEF_BENCH(return new EncodeBench(srcs[0], PNG(kSub, 3), "PNG_3s"));
107DEF_BENCH(return new EncodeBench(srcs[0], PNG(kSub, 1), "PNG_1s"));
108
109DEF_BENCH(return new EncodeBench(srcs[0], PNG(kNone, 6), "PNG_6n"));
110DEF_BENCH(return new EncodeBench(srcs[0], PNG(kNone, 3), "PNG_3n"));
111DEF_BENCH(return new EncodeBench(srcs[0], PNG(kNone, 1), "PNG_1n"));
112
113DEF_BENCH(return new EncodeBench(srcs[1], PNG(kAll, 6), "PNG"));
114DEF_BENCH(return new EncodeBench(srcs[1], PNG(kAll, 3), "PNG_3"));
115DEF_BENCH(return new EncodeBench(srcs[1], PNG(kAll, 1), "PNG_1"));
116
117DEF_BENCH(return new EncodeBench(srcs[1], PNG(kSub, 6), "PNG_6s"));
118DEF_BENCH(return new EncodeBench(srcs[1], PNG(kSub, 3), "PNG_3s"));
119DEF_BENCH(return new EncodeBench(srcs[1], PNG(kSub, 1), "PNG_1s"));
120
121DEF_BENCH(return new EncodeBench(srcs[1], PNG(kNone, 6), "PNG_6n"));
122DEF_BENCH(return new EncodeBench(srcs[1], PNG(kNone, 3), "PNG_3n"));
123DEF_BENCH(return new EncodeBench(srcs[1], PNG(kNone, 1), "PNG_1n"));
124
125#undef PNG