Flutter Engine
The Flutter Engine
EncodeBench.cpp
Go to the documentation of this file.
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"
14#include "tools/DecodeUtils.h"
15
16// 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
22class EncodeBench : public Benchmark {
23public:
24 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)) {}
29
31
32 const char* onGetName() override { return fName.c_str(); }
33
34 void onDelayedSetup() override {
35 SkAssertResult(ToolUtils::GetResourceAsBitmap(fSourceFilename, &fBitmap));
36 }
37
38 void onDraw(int loops, SkCanvas*) override {
39 while (loops-- > 0) {
40 SkPixmap pixmap;
41 SkAssertResult(fBitmap.peekPixels(&pixmap));
43 SkAssertResult(fEncoder(&dst, pixmap));
44 SkASSERT(dst.bytesWritten() > 0);
45 }
46 }
47
48private:
49 const char* fSourceFilename;
50 Encoder fEncoder;
51 SkString fName;
52 SkBitmap fBitmap;
53};
54
55static bool encode_jpeg(SkWStream* dst, const SkPixmap& src) {
57 opts.fQuality = 90;
58 return SkJpegEncoder::Encode(dst, src, opts);
59}
60
64 opts.fQuality = 90;
65 return SkWebpEncoder::Encode(dst, src, opts);
66}
67
71 opts.fQuality = 90;
72 return SkWebpEncoder::Encode(dst, src, opts);
73}
74
76 const SkPixmap& src,
78 int zlibLevel) {
80 opts.fFilterFlags = filters;
81 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
88static const char* srcs[2] = {"images/mandrill_512.png", "images/color_wheel.jpg"};
89
90// The Android Photos app uses a quality of 90 on JPEG encodes
91DEF_BENCH(return new EncodeBench(srcs[0], &encode_jpeg, "JPEG"));
92DEF_BENCH(return new EncodeBench(srcs[1], &encode_jpeg, "JPEG"));
93
94// TODO: What is the appropriate quality to use to benchmark WEBP encodes?
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
const char * backend
static const char * srcs[2]
Definition: EncodeBench.cpp:88
static bool encode_webp_lossy(SkWStream *dst, const SkPixmap &src)
Definition: EncodeBench.cpp:61
DEF_BENCH(return new EncodeBench(srcs[0], &encode_jpeg, "JPEG"))
static bool encode_jpeg(SkWStream *dst, const SkPixmap &src)
Definition: EncodeBench.cpp:55
#define PNG(FLAG, ZLIBLEVEL)
Definition: EncodeBench.cpp:85
static bool encode_png(SkWStream *dst, const SkPixmap &src, SkPngEncoder::FilterFlag filters, int zlibLevel)
Definition: EncodeBench.cpp:75
static bool encode_webp_lossless(SkWStream *dst, const SkPixmap &src)
Definition: EncodeBench.cpp:68
SkAssertResult(font.textToGlyphs("Hello", 5, SkTextEncoding::kUTF8, glyphs, std::size(glyphs))==count)
#define SkASSERT(cond)
Definition: SkAssert.h:116
SK_API SkString SkStringPrintf(const char *format,...) SK_PRINTF_LIKE(1
Creates a new string and writes into it using a printf()-style format.
void onDraw(int loops, SkCanvas *) override
Definition: EncodeBench.cpp:38
bool(*)(SkWStream *, const SkPixmap &) Encoder
Definition: EncodeBench.cpp:24
const char * onGetName() override
Definition: EncodeBench.cpp:32
bool isSuitableFor(Backend backend) override
Definition: EncodeBench.cpp:30
void onDelayedSetup() override
Definition: EncodeBench.cpp:34
EncodeBench(const char *filename, Encoder encoder, const char *encoderName)
Definition: EncodeBench.cpp:25
bool peekPixels(SkPixmap *pixmap) const
Definition: SkBitmap.cpp:635
const char * c_str() const
Definition: SkString.h:133
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
bool GetResourceAsBitmap(const char *resource, SkBitmap *dst)
Definition: DecodeUtils.h:21
@ kNone
Definition: layer.h:53
dst
Definition: cp.py:12
FilterFlag fFilterFlags
Definition: SkPngEncoder.h:56
Compression fCompression
Definition: SkWebpEncoder.h:43