1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This small program is used to measure the performance of the various
6 // resize algorithms offered by the ImageOperations::Resize function.
7 // It will generate an empty source bitmap, and rescale it to specified
8 // dimensions. It will repeat this operation multiple time to get more accurate
9 // average throughput. Because it uses elapsed time to do its math, it is only
10 // accurate on an idle system (but that approach was deemed more accurate
11 // than the use of the times() call.
12 // To present a single number in MB/s, it calculates the 'speed' by taking
13 // source surface + destination surface and dividing by the elapsed time.
14 // This number is somewhat reasonable way to measure this, given our current
15 // implementation which somewhat scales this way.
19 #include "base/basictypes.h"
20 #include "base/command_line.h"
21 #include "base/format_macros.h"
22 #include "base/strings/string_number_conversions.h"
23 #include "base/strings/string_split.h"
24 #include "base/strings/string_util.h"
25 #include "base/strings/utf_string_conversions.h"
26 #include "base/time/time.h"
27 #include "skia/ext/image_operations.h"
28 #include "third_party/skia/include/core/SkBitmap.h"
29 #include "third_party/skia/include/core/SkRect.h"
33 struct StringMethodPair
{
35 skia::ImageOperations::ResizeMethod method
;
37 #define ADD_METHOD(x) { #x, skia::ImageOperations::RESIZE_##x }
38 const StringMethodPair resize_methods
[] = {
48 // converts a string into one of the image operation method to resize.
49 // Returns true on success, false otherwise.
50 bool StringToMethod(const std::string
& arg
,
51 skia::ImageOperations::ResizeMethod
* method
) {
52 for (size_t i
= 0; i
< arraysize(resize_methods
); ++i
) {
53 if (base::EqualsCaseInsensitiveASCII(arg
, resize_methods
[i
].name
)) {
54 *method
= resize_methods
[i
].method
;
61 const char* MethodToString(skia::ImageOperations::ResizeMethod method
) {
62 for (size_t i
= 0; i
< arraysize(resize_methods
); ++i
) {
63 if (method
== resize_methods
[i
].method
) {
64 return resize_methods
[i
].name
;
70 // Prints all supported resize methods
72 bool print_comma
= false;
73 for (size_t i
= 0; i
< arraysize(resize_methods
); ++i
) {
79 printf(" %s", resize_methods
[i
].name
);
83 // Returns the number of bytes that the bitmap has. This number is different
84 // from what SkBitmap::getSize() returns since it does not take into account
85 // the stride. The difference between the stride and the width can be large
86 // because of the alignment constraints on bitmaps created for SRB scaling
87 // (32 pixels) as seen on GTV platforms. Using this metric instead of the
88 // getSize seemed to be a more accurate representation of the work done (even
89 // though in terms of memory bandwidth that might be similar because of the
91 int GetBitmapSize(const SkBitmap
* bitmap
) {
92 return bitmap
->height() * bitmap
->bytesPerPixel() * bitmap
->width();
95 // Simple class to represent dimensions of a bitmap (width, height).
102 void set(int w
, int h
) {
115 bool IsValid() const {
116 return (width_
> 0 && height_
> 0);
119 // On failure, will set its state in such a way that IsValid will return
121 void FromString(const std::string
& arg
) {
122 std::vector
<base::StringPiece
> strings
= base::SplitStringPiece(
123 arg
, "x", base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
124 if (strings
.size() != 2 ||
125 base::StringToInt(strings
[0], &width_
) == false ||
126 base::StringToInt(strings
[1], &height_
) == false) {
127 width_
= -1; // force the dimension object to be invalid.
135 // main class used for the benchmarking.
138 static const int kDefaultNumberIterations
;
139 static const skia::ImageOperations::ResizeMethod kDefaultResizeMethod
;
142 : num_iterations_(kDefaultNumberIterations
),
143 method_(kDefaultResizeMethod
) {}
145 // Returns true if command line parsing was successful, false otherwise.
146 bool ParseArgs(const base::CommandLine
* command_line
);
148 // Returns true if successful, false otherwise.
154 skia::ImageOperations::ResizeMethod method_
;
160 const int Benchmark::kDefaultNumberIterations
= 1024;
161 const skia::ImageOperations::ResizeMethod
Benchmark::kDefaultResizeMethod
=
162 skia::ImageOperations::RESIZE_LANCZOS3
;
164 // argument management
165 void Benchmark::Usage() {
166 printf("image_operations_bench -source wxh -destination wxh "
167 "[-iterations i] [-method m] [-help]\n"
168 " -source wxh: specify source width and height\n"
169 " -destination wxh: specify destination width and height\n"
170 " -iter i: perform i iterations (default:%d)\n"
171 " -method m: use method m (default:%s), which can be:",
172 Benchmark::kDefaultNumberIterations
,
173 MethodToString(Benchmark::kDefaultResizeMethod
));
175 printf("\n -help: prints this help and exits\n");
178 bool Benchmark::ParseArgs(const base::CommandLine
* command_line
) {
179 const base::CommandLine::SwitchMap
& switches
= command_line
->GetSwitches();
180 bool fNeedHelp
= false;
182 for (base::CommandLine::SwitchMap::const_iterator iter
= switches
.begin();
183 iter
!= switches
.end();
185 const std::string
& s
= iter
->first
;
188 value
= base::WideToUTF8(iter
->second
);
190 value
= iter
->second
;
193 source_
.FromString(value
);
194 } else if (s
== "destination") {
195 dest_
.FromString(value
);
196 } else if (s
== "iterations") {
197 if (base::StringToInt(value
, &num_iterations_
) == false) {
200 } else if (s
== "method") {
201 if (!StringToMethod(value
, &method_
)) {
202 printf("Invalid method '%s' specified\n", value
.c_str());
210 if (num_iterations_
<= 0) {
211 printf("Invalid number of iterations: %d\n", num_iterations_
);
214 if (!source_
.IsValid()) {
215 printf("Invalid source dimensions specified\n");
218 if (!dest_
.IsValid()) {
219 printf("Invalid dest dimensions specified\n");
222 if (fNeedHelp
== true) {
229 bool Benchmark::Run() const {
231 source
.allocN32Pixels(source_
.width(), source_
.height());
232 source
.eraseARGB(0, 0, 0, 0);
236 const base::TimeTicks start
= base::TimeTicks::Now();
238 for (int i
= 0; i
< num_iterations_
; ++i
) {
239 dest
= skia::ImageOperations::Resize(source
,
241 dest_
.width(), dest_
.height());
244 const int64 elapsed_us
= (base::TimeTicks::Now() - start
).InMicroseconds();
246 const uint64 num_bytes
= static_cast<uint64
>(num_iterations_
) *
247 (GetBitmapSize(&source
) + GetBitmapSize(&dest
));
249 printf("%" PRIu64
" MB/s,\telapsed = %" PRIu64
" source=%d dest=%d\n",
250 static_cast<uint64
>(elapsed_us
== 0 ? 0 : num_bytes
/ elapsed_us
),
251 static_cast<uint64
>(elapsed_us
),
252 GetBitmapSize(&source
), GetBitmapSize(&dest
));
257 // A small class to automatically call Reset on the global command line to
258 // avoid nasty valgrind complaints for the leak of the global command line.
259 class CommandLineAutoReset
{
261 CommandLineAutoReset(int argc
, char** argv
) {
262 base::CommandLine::Init(argc
, argv
);
264 ~CommandLineAutoReset() {
265 base::CommandLine::Reset();
268 const base::CommandLine
* Get() const {
269 return base::CommandLine::ForCurrentProcess();
275 int main(int argc
, char** argv
) {
277 CommandLineAutoReset
command_line(argc
, argv
);
279 if (!bench
.ParseArgs(command_line
.Get())) {
285 printf("Failed to run benchmark\n");