OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2010 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 #include <stdio.h> | |
brettw
2010/12/17 20:07:55
Can you write a comment at the top of the file giv
evannier
2010/12/19 00:15:03
Done.
| |
6 | |
7 #include "base/basictypes.h" | |
8 #include "base/command_line.h" | |
9 #include "base/format_macros.h" | |
10 #include "base/string_number_conversions.h" | |
11 #include "base/string_split.h" | |
12 #include "base/string_util.h" | |
13 #include "base/time.h" | |
14 #include "base/utf_string_conversions.h" | |
15 #include "skia/ext/image_operations.h" | |
16 #include "third_party/skia/include/core/SkBitmap.h" | |
17 #include "third_party/skia/include/core/SkRect.h" | |
18 | |
19 namespace { | |
brettw
2010/12/17 20:07:55
Please put a blank line on both sides of namespace
evannier
2010/12/19 00:15:03
Done.
| |
20 struct StringMethodPair { | |
21 const char* name; | |
22 skia::ImageOperations::ResizeMethod method; | |
23 }; | |
24 #define ADD_METHOD(x) { #x, skia::ImageOperations::RESIZE_##x } | |
25 const StringMethodPair resize_methods[] = { | |
26 ADD_METHOD(GOOD), | |
27 ADD_METHOD(BETTER), | |
28 ADD_METHOD(BEST), | |
29 ADD_METHOD(BOX), | |
30 ADD_METHOD(HAMMING1), | |
31 ADD_METHOD(LANCZOS2), | |
32 ADD_METHOD(LANCZOS3), | |
33 ADD_METHOD(SUBPIXEL) | |
34 }; | |
35 | |
36 // converts a string into one of the image operation method to resize. | |
37 // @return true on success, false otherwise. | |
38 bool StringToMethod(const std::string& arg, | |
39 skia::ImageOperations::ResizeMethod* method) { | |
40 for (size_t i = 0; i < arraysize(resize_methods); ++i) { | |
41 if (base::strcasecmp(arg.c_str(), resize_methods[i].name) == 0) { | |
42 *method = resize_methods[i].method; | |
43 return true; | |
44 } | |
45 } | |
46 return false; | |
47 } | |
48 | |
49 const char* MethodToString(skia::ImageOperations::ResizeMethod method) { | |
50 for (size_t i = 0; i < arraysize(resize_methods); ++i) { | |
51 if (method == resize_methods[i].method) { | |
52 return resize_methods[i].name; | |
53 } | |
54 } | |
55 return "unknown"; | |
56 } | |
57 | |
58 // Prints all supported resize methods | |
59 void PrintMethods() { | |
60 bool print_comma = false; | |
61 for (size_t i = 0; i < arraysize(resize_methods); ++i) { | |
62 if (print_comma) { | |
63 printf(","); | |
64 } else { | |
65 print_comma = true; | |
66 } | |
67 printf(" %s", resize_methods[i].name); | |
68 } | |
69 } | |
70 | |
71 // @return the number of bytes that the bitmap has. This number is different | |
brettw
2010/12/17 20:07:55
We don't use the doxygen-style comments. Can you c
evannier
2010/12/19 00:15:03
Done.
| |
72 // from what SkBitmap::getSize() returns since it does not take into account | |
73 // the stride. The difference between the stride and the width can be large | |
74 // because of the alignment constraints on bitmaps created for SRB scaling | |
75 // (32 pixels). Using this metric instead of the getSize seemed to be a more | |
76 // accurate representation of the work done (even though in terms of memory | |
77 // bandwidth that might be similar because of the cache line size). | |
78 // @return the number of bytes of the image. | |
79 int GetBitmapSize(const SkBitmap* bitmap) { | |
80 return bitmap->height() * bitmap->bytesPerPixel() * bitmap->width(); | |
81 } | |
82 | |
83 // Simple class to represent dimensions of a bitmap (width, height). | |
84 class Dimensions { | |
85 public: | |
86 Dimensions() : | |
brettw
2010/12/17 20:07:55
Style: this can all go on one line (I would probab
evannier
2010/12/19 00:15:03
Done. i left on separate lines, but moved the ':'
| |
87 width_(0), | |
88 height_(0) {} | |
89 | |
90 void set(int w, int h) { | |
91 width_ = w; | |
92 height_ = h; | |
93 } | |
94 | |
95 int width() const { | |
96 return width_; | |
97 } | |
98 | |
99 int height() const { | |
100 return height_; | |
101 } | |
102 | |
103 bool IsValid() const { | |
104 return (width_ > 0 && height_ > 0); | |
105 } | |
106 | |
107 // On failure, will set its state in such a way that IsValid will return | |
108 // false. | |
109 void FromString(const std::string& arg) { | |
110 std::vector<std::string> strings; | |
111 base::SplitString(std::string(arg), 'x', &strings); | |
112 if (strings.size() != 2 || | |
113 base::StringToInt(strings[0], &width_) == false || | |
114 base::StringToInt(strings[1], &height_) == false) { | |
115 width_ = -1; // force the dimension object to be invalid. | |
116 } | |
117 } | |
118 private: | |
119 int width_; | |
120 int height_; | |
121 }; | |
122 | |
123 // main class used for the benchmarking. | |
124 class Benchmark { | |
125 public: | |
126 static const int kDefaultNumberIterations; | |
127 static const skia::ImageOperations::ResizeMethod kDefaultResizeMethod; | |
128 | |
129 Benchmark() : | |
brettw
2010/12/17 20:07:55
Ditto.
evannier
2010/12/19 00:15:03
Done.
| |
130 num_iterations_(kDefaultNumberIterations), | |
131 method_(kDefaultResizeMethod) {} | |
132 | |
133 // @return true if command line parsing was successful, false otherwise. | |
134 bool ParseArgs(const CommandLine* command_line); | |
135 | |
136 // @returns true if successful, false otherwise. | |
137 bool Run() const; | |
138 | |
139 static void Usage(); | |
140 private: | |
141 int num_iterations_; | |
142 skia::ImageOperations::ResizeMethod method_; | |
143 Dimensions source_; | |
144 Dimensions dest_; | |
145 }; | |
146 | |
147 // static | |
148 const int Benchmark::kDefaultNumberIterations = 1024; | |
149 const skia::ImageOperations::ResizeMethod Benchmark::kDefaultResizeMethod = | |
150 skia::ImageOperations::RESIZE_LANCZOS3; | |
151 | |
152 // argument management | |
153 void Benchmark::Usage() { | |
154 printf("image_operations_bench -source wxh -destination wxh " | |
155 "[-iterations i] [-method m] [-help]\n" | |
156 " -source wxh: specify source width and height\n" | |
157 " -destination wxh: specify destination width and height\n" | |
158 " -iter i: perform i iterations (default:%d)\n" | |
159 " -method m: use method m (default:%s), which can be:", | |
160 Benchmark::kDefaultNumberIterations, | |
161 MethodToString(Benchmark::kDefaultResizeMethod)); | |
162 PrintMethods(); | |
163 printf("\n -help: prints this help and exits\n"); | |
164 } | |
165 | |
166 bool Benchmark::ParseArgs(const CommandLine* command_line) { | |
167 const CommandLine::SwitchMap& switches = command_line->GetSwitches(); | |
168 bool fNeedHelp = false; | |
169 | |
170 for (CommandLine::SwitchMap::const_iterator iter = switches.begin(); | |
171 iter != switches.end(); | |
172 ++iter) { | |
173 const std::string& s = iter->first; | |
174 std::string value; | |
175 #if defined(OS_WIN) | |
176 value = WideToUTF8(iter->second); | |
177 #else | |
178 value = iter->second; | |
179 #endif | |
180 if (s == "source") { | |
181 source_.FromString(value); | |
182 } else if (s == "destination") { | |
183 dest_.FromString(value); | |
184 } else if (s == "iterations") { | |
185 if (base::StringToInt(value, &num_iterations_) == false) { | |
186 fNeedHelp = true; | |
187 } | |
188 } else if (s == "method") { | |
189 if (!StringToMethod(value, &method_)) { | |
190 printf("Invalid method '%s' specified\n", value.c_str()); | |
191 fNeedHelp = true; | |
192 } | |
193 } else { | |
194 fNeedHelp = true; | |
195 } | |
196 } | |
197 | |
198 if (num_iterations_ <= 0) { | |
199 printf("Invalid number of iterations: %d\n", num_iterations_); | |
200 fNeedHelp = true; | |
201 } | |
202 if (!source_.IsValid()) { | |
203 printf("Invalid source dimensions specified\n"); | |
204 fNeedHelp = true; | |
205 } | |
206 if (!dest_.IsValid()) { | |
207 printf("Invalid dest dimensions specified\n"); | |
208 fNeedHelp = true; | |
209 } | |
210 if (fNeedHelp == true) { | |
211 return false; | |
212 } | |
213 return true; | |
214 } | |
215 | |
216 // actual benchmark. | |
217 bool Benchmark::Run() const { | |
218 SkBitmap source; | |
219 source.setConfig(SkBitmap::kARGB_8888_Config, | |
220 source_.width(), source_.height()); | |
221 source.allocPixels(); | |
222 source.eraseARGB(0, 0, 0, 0); | |
223 | |
224 SkBitmap dest; | |
225 | |
226 const base::TimeTicks start = base::TimeTicks::Now(); | |
227 | |
228 for (int i = 0; i < num_iterations_; ++i) { | |
229 dest = skia::ImageOperations::Resize(source, | |
230 method_, | |
231 dest_.width(), dest_.height()); | |
232 } | |
233 | |
234 const int64 elapsed_us = (base::TimeTicks::Now() - start).InMicroseconds(); | |
235 | |
236 const uint64 num_bytes = static_cast<uint64>(num_iterations_) * | |
237 (GetBitmapSize(&source) + GetBitmapSize(&dest)); | |
238 | |
239 printf("%"PRIu64" MB/s,\telapsed = %"PRIu64" source=%d dest=%d\n", | |
240 static_cast<uint64>(elapsed_us == 0 ? 0 : num_bytes / elapsed_us), | |
241 static_cast<uint64>(elapsed_us), | |
242 GetBitmapSize(&source), GetBitmapSize(&dest)); | |
243 | |
244 return true; | |
245 } | |
246 | |
247 // A small class to automatically call Reset on the global command line to | |
248 // avoid nasty valgrind complaints for the leak of the global command line. | |
249 class CommandLineAutoReset { | |
250 public: | |
251 CommandLineAutoReset(int argc, char** argv) { | |
252 CommandLine::Init(argc, argv); | |
253 } | |
254 ~CommandLineAutoReset() { | |
255 CommandLine::Reset(); | |
256 } | |
257 | |
258 const CommandLine* Get() const { | |
259 return CommandLine::ForCurrentProcess(); | |
260 } | |
261 }; | |
262 } // anonymous namespace | |
brettw
2010/12/17 20:07:55
Just "// namespace" is sufficient. Also, be sure
evannier
2010/12/19 00:15:03
Done.
| |
263 | |
264 int main(int argc, char** argv) { | |
265 Benchmark bench; | |
266 CommandLineAutoReset command_line(argc, argv); | |
267 | |
268 if (!bench.ParseArgs(command_line.Get())) { | |
269 Benchmark::Usage(); | |
270 return 1; | |
271 } | |
272 | |
273 if (!bench.Run()) { | |
274 printf("Failed to run benchmark\n"); | |
275 return 1; | |
276 } | |
277 | |
278 return 0; | |
279 } | |
OLD | NEW |