blob: f0adaa1071d0e9d4d161e389d23efc1863f65dcd [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
Hal Canarydb683012016-11-23 08:55:18 -070014#include "sk_tool_utils.h"
15
msarettb92dc7a2016-04-18 12:37:46 -070016class EncodeBench : public Benchmark {
17public:
Hal Canarydb683012016-11-23 08:55:18 -070018 EncodeBench(const char* filename, SkEncodedImageFormat type, int quality)
msarettb92dc7a2016-04-18 12:37:46 -070019 : fFilename(filename)
20 , fType(type)
21 , fQuality(quality)
22 {
23 // Set the name of the bench
24 SkString name("Encode_");
25 name.append(filename);
26 name.append("_");
27 switch (type) {
Hal Canarydb683012016-11-23 08:55:18 -070028 case SkEncodedImageFormat::kJPEG:
msarettb92dc7a2016-04-18 12:37:46 -070029 name.append("JPEG");
30 break;
Hal Canarydb683012016-11-23 08:55:18 -070031 case SkEncodedImageFormat::kPNG:
msarettb92dc7a2016-04-18 12:37:46 -070032 name.append("PNG");
33 break;
Hal Canarydb683012016-11-23 08:55:18 -070034 case SkEncodedImageFormat::kWEBP:
msarettb92dc7a2016-04-18 12:37:46 -070035 name.append("WEBP");
36 break;
37 default:
38 name.append("Unknown");
39 break;
40 }
41
42 fName = name;
43 }
44
45 bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; }
46
47 const char* onGetName() override { return fName.c_str(); }
48
49 void onPreDraw(SkCanvas*) override {
50#ifdef SK_DEBUG
51 bool result =
52#endif
53 GetResourceAsBitmap(fFilename, &fBitmap);
54 SkASSERT(result);
55 }
56
57 void onDraw(int loops, SkCanvas*) override {
58 for (int i = 0; i < loops; i++) {
Hal Canarydb683012016-11-23 08:55:18 -070059 sk_sp<SkData> data(sk_tool_utils::EncodeImageToData(fBitmap, fType, fQuality));
msarettb92dc7a2016-04-18 12:37:46 -070060 SkASSERT(data);
61 }
62 }
63
64private:
65 const char* fFilename;
Hal Canarydb683012016-11-23 08:55:18 -070066 const SkEncodedImageFormat fType;
msarettb92dc7a2016-04-18 12:37:46 -070067 const int fQuality;
68 SkString fName;
69 SkBitmap fBitmap;
70};
71
72
73// The Android Photos app uses a quality of 90 on JPEG encodes
Hal Canarydb683012016-11-23 08:55:18 -070074DEF_BENCH(return new EncodeBench("mandrill_512.png", SkEncodedImageFormat::kJPEG, 90));
75DEF_BENCH(return new EncodeBench("color_wheel.jpg", SkEncodedImageFormat::kJPEG, 90));
msarettb92dc7a2016-04-18 12:37:46 -070076
77// PNG encodes are lossless so quality should be ignored
Hal Canarydb683012016-11-23 08:55:18 -070078DEF_BENCH(return new EncodeBench("mandrill_512.png", SkEncodedImageFormat::kPNG, 90));
79DEF_BENCH(return new EncodeBench("color_wheel.jpg", SkEncodedImageFormat::kPNG, 90));
msarettb92dc7a2016-04-18 12:37:46 -070080
81// TODO: What is the appropriate quality to use to benchmark WEBP encodes?
Hal Canarydb683012016-11-23 08:55:18 -070082DEF_BENCH(return new EncodeBench("mandrill_512.png", SkEncodedImageFormat::kWEBP, 90));
83DEF_BENCH(return new EncodeBench("color_wheel.jpg", SkEncodedImageFormat::kWEBP, 90));