blob: c60061b61045df2bfe4c2de3df084b0f04ec6e78 [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
8#include "Benchmark.h"
9#include "Resources.h"
10#include "SkBitmap.h"
Hal Canary05784d92017-06-16 16:52:10 -040011#include "SkJpegEncoder.h"
12#include "SkPngEncoder.h"
13#include "SkWebpEncoder.h"
14#include "SkStream.h"
Hal Canarydb683012016-11-23 08:55:18 -070015
msarettb92dc7a2016-04-18 12:37:46 -070016class EncodeBench : public Benchmark {
17public:
Hal Canary05784d92017-06-16 16:52:10 -040018 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)) {}
msarettb92dc7a2016-04-18 12:37:46 -070023
24 bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; }
Hal Canary05784d92017-06-16 16:52:10 -040025
msarettb92dc7a2016-04-18 12:37:46 -070026 const char* onGetName() override { return fName.c_str(); }
Hal Canary05784d92017-06-16 16:52:10 -040027
msarettb92dc7a2016-04-18 12:37:46 -070028 void onPreDraw(SkCanvas*) override {
Hal Canary05784d92017-06-16 16:52:10 -040029 SkAssertResult(GetResourceAsBitmap(fSourceFilename, &fBitmap));
msarettb92dc7a2016-04-18 12:37:46 -070030 }
31
32 void onDraw(int loops, SkCanvas*) override {
Hal Canary05784d92017-06-16 16:52:10 -040033 while (loops-- > 0) {
34 SkPixmap pixmap;
35 SkAssertResult(fBitmap.peekPixels(&pixmap));
36 SkNullWStream dst;
37 SkAssertResult(fEncoder(&dst, pixmap));
38 SkASSERT(dst.bytesWritten() > 0);
msarettb92dc7a2016-04-18 12:37:46 -070039 }
40 }
41
42private:
Hal Canary05784d92017-06-16 16:52:10 -040043 const char* fSourceFilename;
44 Encoder fEncoder;
45 SkString fName;
46 SkBitmap fBitmap;
msarettb92dc7a2016-04-18 12:37:46 -070047};
48
Hal Canary05784d92017-06-16 16:52:10 -040049static 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
55static 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
63static 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
71static 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
Hal Canaryc465d132017-12-08 10:21:31 -050085static const char* srcs[2] = {"images/mandrill_512.png", "images/color_wheel.jpg"};
msarettb92dc7a2016-04-18 12:37:46 -070086
87// The Android Photos app uses a quality of 90 on JPEG encodes
Hal Canary05784d92017-06-16 16:52:10 -040088DEF_BENCH(return new EncodeBench(srcs[0], &encode_jpeg, "JPEG"));
89DEF_BENCH(return new EncodeBench(srcs[1], &encode_jpeg, "JPEG"));
msarettb92dc7a2016-04-18 12:37:46 -070090
91// TODO: What is the appropriate quality to use to benchmark WEBP encodes?
Hal Canary05784d92017-06-16 16:52:10 -040092DEF_BENCH(return new EncodeBench(srcs[0], encode_webp_lossy, "WEBP"));
93DEF_BENCH(return new EncodeBench(srcs[1], encode_webp_lossy, "WEBP"));
94
95DEF_BENCH(return new EncodeBench(srcs[0], encode_webp_lossless, "WEBP_LL"));
96DEF_BENCH(return new EncodeBench(srcs[1], encode_webp_lossless, "WEBP_LL"));
97
98DEF_BENCH(return new EncodeBench(srcs[0], PNG(kAll, 6), "PNG"));
99DEF_BENCH(return new EncodeBench(srcs[0], PNG(kAll, 3), "PNG_3"));
100DEF_BENCH(return new EncodeBench(srcs[0], PNG(kAll, 1), "PNG_1"));
101
102DEF_BENCH(return new EncodeBench(srcs[0], PNG(kSub, 6), "PNG_6s"));
103DEF_BENCH(return new EncodeBench(srcs[0], PNG(kSub, 3), "PNG_3s"));
104DEF_BENCH(return new EncodeBench(srcs[0], PNG(kSub, 1), "PNG_1s"));
105
106DEF_BENCH(return new EncodeBench(srcs[0], PNG(kNone, 6), "PNG_6n"));
107DEF_BENCH(return new EncodeBench(srcs[0], PNG(kNone, 3), "PNG_3n"));
108DEF_BENCH(return new EncodeBench(srcs[0], PNG(kNone, 1), "PNG_1n"));
109
110DEF_BENCH(return new EncodeBench(srcs[1], PNG(kAll, 6), "PNG"));
111DEF_BENCH(return new EncodeBench(srcs[1], PNG(kAll, 3), "PNG_3"));
112DEF_BENCH(return new EncodeBench(srcs[1], PNG(kAll, 1), "PNG_1"));
113
114DEF_BENCH(return new EncodeBench(srcs[1], PNG(kSub, 6), "PNG_6s"));
115DEF_BENCH(return new EncodeBench(srcs[1], PNG(kSub, 3), "PNG_3s"));
116DEF_BENCH(return new EncodeBench(srcs[1], PNG(kSub, 1), "PNG_1s"));
117
118DEF_BENCH(return new EncodeBench(srcs[1], PNG(kNone, 6), "PNG_6n"));
119DEF_BENCH(return new EncodeBench(srcs[1], PNG(kNone, 3), "PNG_3n"));
120DEF_BENCH(return new EncodeBench(srcs[1], PNG(kNone, 1), "PNG_1n"));
121
122#undef PNG