msarett | b92dc7a | 2016-04-18 12:37:46 -0700 | [diff] [blame] | 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 "Benchmark.h" |
| 9 | #include "Resources.h" |
| 10 | #include "SkBitmap.h" |
| 11 | #include "SkData.h" |
| 12 | #include "SkImageEncoder.h" |
| 13 | |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 14 | #include "sk_tool_utils.h" |
| 15 | |
msarett | b92dc7a | 2016-04-18 12:37:46 -0700 | [diff] [blame] | 16 | class EncodeBench : public Benchmark { |
| 17 | public: |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 18 | EncodeBench(const char* filename, SkEncodedImageFormat type, int quality) |
msarett | b92dc7a | 2016-04-18 12:37:46 -0700 | [diff] [blame] | 19 | : 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 Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 28 | case SkEncodedImageFormat::kJPEG: |
msarett | b92dc7a | 2016-04-18 12:37:46 -0700 | [diff] [blame] | 29 | name.append("JPEG"); |
| 30 | break; |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 31 | case SkEncodedImageFormat::kPNG: |
msarett | b92dc7a | 2016-04-18 12:37:46 -0700 | [diff] [blame] | 32 | name.append("PNG"); |
| 33 | break; |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 34 | case SkEncodedImageFormat::kWEBP: |
msarett | b92dc7a | 2016-04-18 12:37:46 -0700 | [diff] [blame] | 35 | 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 Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 59 | sk_sp<SkData> data(sk_tool_utils::EncodeImageToData(fBitmap, fType, fQuality)); |
msarett | b92dc7a | 2016-04-18 12:37:46 -0700 | [diff] [blame] | 60 | SkASSERT(data); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | const char* fFilename; |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 66 | const SkEncodedImageFormat fType; |
msarett | b92dc7a | 2016-04-18 12:37:46 -0700 | [diff] [blame] | 67 | 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 Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 74 | DEF_BENCH(return new EncodeBench("mandrill_512.png", SkEncodedImageFormat::kJPEG, 90)); |
| 75 | DEF_BENCH(return new EncodeBench("color_wheel.jpg", SkEncodedImageFormat::kJPEG, 90)); |
msarett | b92dc7a | 2016-04-18 12:37:46 -0700 | [diff] [blame] | 76 | |
| 77 | // PNG encodes are lossless so quality should be ignored |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 78 | DEF_BENCH(return new EncodeBench("mandrill_512.png", SkEncodedImageFormat::kPNG, 90)); |
| 79 | DEF_BENCH(return new EncodeBench("color_wheel.jpg", SkEncodedImageFormat::kPNG, 90)); |
msarett | b92dc7a | 2016-04-18 12:37:46 -0700 | [diff] [blame] | 80 | |
| 81 | // TODO: What is the appropriate quality to use to benchmark WEBP encodes? |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 82 | DEF_BENCH(return new EncodeBench("mandrill_512.png", SkEncodedImageFormat::kWEBP, 90)); |
| 83 | DEF_BENCH(return new EncodeBench("color_wheel.jpg", SkEncodedImageFormat::kWEBP, 90)); |