• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
14 #include "sk_tool_utils.h"
15 
16 class EncodeBench : public Benchmark {
17 public:
EncodeBench(const char * filename,SkEncodedImageFormat type,int quality)18     EncodeBench(const char* filename, SkEncodedImageFormat type, int quality)
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) {
28             case SkEncodedImageFormat::kJPEG:
29                 name.append("JPEG");
30                 break;
31             case SkEncodedImageFormat::kPNG:
32                 name.append("PNG");
33                 break;
34             case SkEncodedImageFormat::kWEBP:
35                 name.append("WEBP");
36                 break;
37             default:
38                 name.append("Unknown");
39                 break;
40         }
41 
42         fName = name;
43     }
44 
isSuitableFor(Backend backend)45     bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; }
46 
onGetName()47     const char* onGetName() override { return fName.c_str(); }
48 
onPreDraw(SkCanvas *)49     void onPreDraw(SkCanvas*) override {
50 #ifdef SK_DEBUG
51         bool result =
52 #endif
53         GetResourceAsBitmap(fFilename, &fBitmap);
54         SkASSERT(result);
55     }
56 
onDraw(int loops,SkCanvas *)57     void onDraw(int loops, SkCanvas*) override {
58         for (int i = 0; i < loops; i++) {
59             sk_sp<SkData> data(sk_tool_utils::EncodeImageToData(fBitmap, fType, fQuality));
60             SkASSERT(data);
61         }
62     }
63 
64 private:
65     const char*                fFilename;
66     const SkEncodedImageFormat fType;
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
74 DEF_BENCH(return new EncodeBench("mandrill_512.png", SkEncodedImageFormat::kJPEG, 90));
75 DEF_BENCH(return new EncodeBench("color_wheel.jpg", SkEncodedImageFormat::kJPEG, 90));
76 
77 // PNG encodes are lossless so quality should be ignored
78 DEF_BENCH(return new EncodeBench("mandrill_512.png", SkEncodedImageFormat::kPNG, 90));
79 DEF_BENCH(return new EncodeBench("color_wheel.jpg", SkEncodedImageFormat::kPNG, 90));
80 
81 // TODO: What is the appropriate quality to use to benchmark WEBP encodes?
82 DEF_BENCH(return new EncodeBench("mandrill_512.png", SkEncodedImageFormat::kWEBP, 90));
83 DEF_BENCH(return new EncodeBench("color_wheel.jpg", SkEncodedImageFormat::kWEBP, 90));
84