OLD | NEW |
| (Empty) |
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. | |
4 | |
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. | |
16 | |
17 #include <stdio.h> | |
18 | |
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" | |
30 | |
31 namespace { | |
32 | |
33 struct StringMethodPair { | |
34 const char* name; | |
35 skia::ImageOperations::ResizeMethod method; | |
36 }; | |
37 #define ADD_METHOD(x) { #x, skia::ImageOperations::RESIZE_##x } | |
38 const StringMethodPair resize_methods[] = { | |
39 ADD_METHOD(GOOD), | |
40 ADD_METHOD(BETTER), | |
41 ADD_METHOD(BEST), | |
42 ADD_METHOD(BOX), | |
43 ADD_METHOD(HAMMING1), | |
44 ADD_METHOD(LANCZOS2), | |
45 ADD_METHOD(LANCZOS3), | |
46 }; | |
47 | |
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::strcasecmp(arg.c_str(), resize_methods[i].name) == 0) { | |
54 *method = resize_methods[i].method; | |
55 return true; | |
56 } | |
57 } | |
58 return false; | |
59 } | |
60 | |
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; | |
65 } | |
66 } | |
67 return "unknown"; | |
68 } | |
69 | |
70 // Prints all supported resize methods | |
71 void PrintMethods() { | |
72 bool print_comma = false; | |
73 for (size_t i = 0; i < arraysize(resize_methods); ++i) { | |
74 if (print_comma) { | |
75 printf(","); | |
76 } else { | |
77 print_comma = true; | |
78 } | |
79 printf(" %s", resize_methods[i].name); | |
80 } | |
81 } | |
82 | |
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 | |
90 // cache line size). | |
91 int GetBitmapSize(const SkBitmap* bitmap) { | |
92 return bitmap->height() * bitmap->bytesPerPixel() * bitmap->width(); | |
93 } | |
94 | |
95 // Simple class to represent dimensions of a bitmap (width, height). | |
96 class Dimensions { | |
97 public: | |
98 Dimensions() | |
99 : width_(0), | |
100 height_(0) {} | |
101 | |
102 void set(int w, int h) { | |
103 width_ = w; | |
104 height_ = h; | |
105 } | |
106 | |
107 int width() const { | |
108 return width_; | |
109 } | |
110 | |
111 int height() const { | |
112 return height_; | |
113 } | |
114 | |
115 bool IsValid() const { | |
116 return (width_ > 0 && height_ > 0); | |
117 } | |
118 | |
119 // On failure, will set its state in such a way that IsValid will return | |
120 // false. | |
121 void FromString(const std::string& arg) { | |
122 std::vector<std::string> strings; | |
123 base::SplitString(std::string(arg), 'x', &strings); | |
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. | |
128 } | |
129 } | |
130 private: | |
131 int width_; | |
132 int height_; | |
133 }; | |
134 | |
135 // main class used for the benchmarking. | |
136 class Benchmark { | |
137 public: | |
138 static const int kDefaultNumberIterations; | |
139 static const skia::ImageOperations::ResizeMethod kDefaultResizeMethod; | |
140 | |
141 Benchmark() | |
142 : num_iterations_(kDefaultNumberIterations), | |
143 method_(kDefaultResizeMethod) {} | |
144 | |
145 // Returns true if command line parsing was successful, false otherwise. | |
146 bool ParseArgs(const base::CommandLine* command_line); | |
147 | |
148 // Returns true if successful, false otherwise. | |
149 bool Run() const; | |
150 | |
151 static void Usage(); | |
152 private: | |
153 int num_iterations_; | |
154 skia::ImageOperations::ResizeMethod method_; | |
155 Dimensions source_; | |
156 Dimensions dest_; | |
157 }; | |
158 | |
159 // static | |
160 const int Benchmark::kDefaultNumberIterations = 1024; | |
161 const skia::ImageOperations::ResizeMethod Benchmark::kDefaultResizeMethod = | |
162 skia::ImageOperations::RESIZE_LANCZOS3; | |
163 | |
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)); | |
174 PrintMethods(); | |
175 printf("\n -help: prints this help and exits\n"); | |
176 } | |
177 | |
178 bool Benchmark::ParseArgs(const base::CommandLine* command_line) { | |
179 const base::CommandLine::SwitchMap& switches = command_line->GetSwitches(); | |
180 bool fNeedHelp = false; | |
181 | |
182 for (base::CommandLine::SwitchMap::const_iterator iter = switches.begin(); | |
183 iter != switches.end(); | |
184 ++iter) { | |
185 const std::string& s = iter->first; | |
186 std::string value; | |
187 #if defined(OS_WIN) | |
188 value = base::WideToUTF8(iter->second); | |
189 #else | |
190 value = iter->second; | |
191 #endif | |
192 if (s == "source") { | |
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) { | |
198 fNeedHelp = true; | |
199 } | |
200 } else if (s == "method") { | |
201 if (!StringToMethod(value, &method_)) { | |
202 printf("Invalid method '%s' specified\n", value.c_str()); | |
203 fNeedHelp = true; | |
204 } | |
205 } else { | |
206 fNeedHelp = true; | |
207 } | |
208 } | |
209 | |
210 if (num_iterations_ <= 0) { | |
211 printf("Invalid number of iterations: %d\n", num_iterations_); | |
212 fNeedHelp = true; | |
213 } | |
214 if (!source_.IsValid()) { | |
215 printf("Invalid source dimensions specified\n"); | |
216 fNeedHelp = true; | |
217 } | |
218 if (!dest_.IsValid()) { | |
219 printf("Invalid dest dimensions specified\n"); | |
220 fNeedHelp = true; | |
221 } | |
222 if (fNeedHelp == true) { | |
223 return false; | |
224 } | |
225 return true; | |
226 } | |
227 | |
228 // actual benchmark. | |
229 bool Benchmark::Run() const { | |
230 SkBitmap source; | |
231 source.allocN32Pixels(source_.width(), source_.height()); | |
232 source.eraseARGB(0, 0, 0, 0); | |
233 | |
234 SkBitmap dest; | |
235 | |
236 const base::TimeTicks start = base::TimeTicks::Now(); | |
237 | |
238 for (int i = 0; i < num_iterations_; ++i) { | |
239 dest = skia::ImageOperations::Resize(source, | |
240 method_, | |
241 dest_.width(), dest_.height()); | |
242 } | |
243 | |
244 const int64 elapsed_us = (base::TimeTicks::Now() - start).InMicroseconds(); | |
245 | |
246 const uint64 num_bytes = static_cast<uint64>(num_iterations_) * | |
247 (GetBitmapSize(&source) + GetBitmapSize(&dest)); | |
248 | |
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)); | |
253 | |
254 return true; | |
255 } | |
256 | |
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 { | |
260 public: | |
261 CommandLineAutoReset(int argc, char** argv) { | |
262 base::CommandLine::Init(argc, argv); | |
263 } | |
264 ~CommandLineAutoReset() { | |
265 base::CommandLine::Reset(); | |
266 } | |
267 | |
268 const base::CommandLine* Get() const { | |
269 return base::CommandLine::ForCurrentProcess(); | |
270 } | |
271 }; | |
272 | |
273 } // namespace | |
274 | |
275 int main(int argc, char** argv) { | |
276 Benchmark bench; | |
277 CommandLineAutoReset command_line(argc, argv); | |
278 | |
279 if (!bench.ParseArgs(command_line.Get())) { | |
280 Benchmark::Usage(); | |
281 return 1; | |
282 } | |
283 | |
284 if (!bench.Run()) { | |
285 printf("Failed to run benchmark\n"); | |
286 return 1; | |
287 } | |
288 | |
289 return 0; | |
290 } | |
OLD | NEW |