blob: d7ddfbc602b00cbd8059a012ba2d3b03710fff98 [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"
11#include "SkData.h"
12#include "SkImageEncoder.h"
13
14class EncodeBench : public Benchmark {
15public:
16 EncodeBench(const char* filename, SkImageEncoder::Type type, int quality)
17 : fFilename(filename)
18 , fType(type)
19 , fQuality(quality)
20 {
21 // Set the name of the bench
22 SkString name("Encode_");
23 name.append(filename);
24 name.append("_");
25 switch (type) {
26 case SkImageEncoder::kJPEG_Type:
27 name.append("JPEG");
28 break;
29 case SkImageEncoder::kPNG_Type:
30 name.append("PNG");
31 break;
32 case SkImageEncoder::kWEBP_Type:
33 name.append("WEBP");
34 break;
35 default:
36 name.append("Unknown");
37 break;
38 }
39
40 fName = name;
41 }
42
43 bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; }
44
45 const char* onGetName() override { return fName.c_str(); }
46
47 void onPreDraw(SkCanvas*) override {
48#ifdef SK_DEBUG
49 bool result =
50#endif
51 GetResourceAsBitmap(fFilename, &fBitmap);
52 SkASSERT(result);
53 }
54
55 void onDraw(int loops, SkCanvas*) override {
56 for (int i = 0; i < loops; i++) {
bungemanffae30d2016-08-03 13:32:32 -070057 sk_sp<SkData> data(SkImageEncoder::EncodeData(fBitmap, fType, fQuality));
msarettb92dc7a2016-04-18 12:37:46 -070058 SkASSERT(data);
59 }
60 }
61
62private:
63 const char* fFilename;
64 const SkImageEncoder::Type fType;
65 const int fQuality;
66 SkString fName;
67 SkBitmap fBitmap;
68};
69
70
71// The Android Photos app uses a quality of 90 on JPEG encodes
72DEF_BENCH(return new EncodeBench("mandrill_512.png", SkImageEncoder::kJPEG_Type, 90));
73DEF_BENCH(return new EncodeBench("color_wheel.jpg", SkImageEncoder::kJPEG_Type, 90));
74
75// PNG encodes are lossless so quality should be ignored
76DEF_BENCH(return new EncodeBench("mandrill_512.png", SkImageEncoder::kPNG_Type, 90));
77DEF_BENCH(return new EncodeBench("color_wheel.jpg", SkImageEncoder::kPNG_Type, 90));
78
79// TODO: What is the appropriate quality to use to benchmark WEBP encodes?
80DEF_BENCH(return new EncodeBench("mandrill_512.png", SkImageEncoder::kWEBP_Type, 90));
81DEF_BENCH(return new EncodeBench("color_wheel.jpg", SkImageEncoder::kWEBP_Type, 90));