image_operations_bench.cc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. // This small program is used to measure the performance of the various
  5. // resize algorithms offered by the ImageOperations::Resize function.
  6. // It will generate an empty source bitmap, and rescale it to specified
  7. // dimensions. It will repeat this operation multiple time to get more accurate
  8. // average throughput. Because it uses elapsed time to do its math, it is only
  9. // accurate on an idle system (but that approach was deemed more accurate
  10. // than the use of the times() call.
  11. // To present a single number in MB/s, it calculates the 'speed' by taking
  12. // source surface + destination surface and dividing by the elapsed time.
  13. // This number is somewhat reasonable way to measure this, given our current
  14. // implementation which somewhat scales this way.
  15. #include <stddef.h>
  16. #include <stdint.h>
  17. #include <stdio.h>
  18. #include "base/command_line.h"
  19. #include "base/format_macros.h"
  20. #include "base/strings/string_number_conversions.h"
  21. #include "base/strings/string_split.h"
  22. #include "base/strings/string_util.h"
  23. #include "base/strings/utf_string_conversions.h"
  24. #include "base/time/time.h"
  25. #include "build/build_config.h"
  26. #include "skia/ext/image_operations.h"
  27. #include "third_party/skia/include/core/SkBitmap.h"
  28. #include "third_party/skia/include/core/SkRect.h"
  29. namespace {
  30. struct StringMethodPair {
  31. const char* name;
  32. skia::ImageOperations::ResizeMethod method;
  33. };
  34. #define ADD_METHOD(x) { #x, skia::ImageOperations::RESIZE_##x }
  35. const StringMethodPair resize_methods[] = {
  36. ADD_METHOD(GOOD),
  37. ADD_METHOD(BETTER),
  38. ADD_METHOD(BEST),
  39. ADD_METHOD(BOX),
  40. ADD_METHOD(HAMMING1),
  41. ADD_METHOD(LANCZOS3),
  42. };
  43. // converts a string into one of the image operation method to resize.
  44. // Returns true on success, false otherwise.
  45. bool StringToMethod(const std::string& arg,
  46. skia::ImageOperations::ResizeMethod* method) {
  47. for (size_t i = 0; i < std::size(resize_methods); ++i) {
  48. if (base::EqualsCaseInsensitiveASCII(arg, resize_methods[i].name)) {
  49. *method = resize_methods[i].method;
  50. return true;
  51. }
  52. }
  53. return false;
  54. }
  55. const char* MethodToString(skia::ImageOperations::ResizeMethod method) {
  56. for (size_t i = 0; i < std::size(resize_methods); ++i) {
  57. if (method == resize_methods[i].method) {
  58. return resize_methods[i].name;
  59. }
  60. }
  61. return "unknown";
  62. }
  63. // Prints all supported resize methods
  64. void PrintMethods() {
  65. bool print_comma = false;
  66. for (size_t i = 0; i < std::size(resize_methods); ++i) {
  67. if (print_comma) {
  68. printf(",");
  69. } else {
  70. print_comma = true;
  71. }
  72. printf(" %s", resize_methods[i].name);
  73. }
  74. }
  75. // Returns the number of bytes that the bitmap has. This number is different
  76. // from what SkBitmap::getSize() returns since it does not take into account
  77. // the stride. The difference between the stride and the width can be large
  78. // because of the alignment constraints on bitmaps created for SRB scaling
  79. // (32 pixels) as seen on GTV platforms. Using this metric instead of the
  80. // getSize seemed to be a more accurate representation of the work done (even
  81. // though in terms of memory bandwidth that might be similar because of the
  82. // cache line size).
  83. int GetBitmapSize(const SkBitmap* bitmap) {
  84. return bitmap->height() * bitmap->bytesPerPixel() * bitmap->width();
  85. }
  86. // Simple class to represent dimensions of a bitmap (width, height).
  87. class Dimensions {
  88. public:
  89. Dimensions()
  90. : width_(0),
  91. height_(0) {}
  92. void set(int w, int h) {
  93. width_ = w;
  94. height_ = h;
  95. }
  96. int width() const {
  97. return width_;
  98. }
  99. int height() const {
  100. return height_;
  101. }
  102. bool IsValid() const {
  103. return (width_ > 0 && height_ > 0);
  104. }
  105. // On failure, will set its state in such a way that IsValid will return
  106. // false.
  107. void FromString(const std::string& arg) {
  108. std::vector<base::StringPiece> strings = base::SplitStringPiece(
  109. arg, "x", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
  110. if (strings.size() != 2 ||
  111. base::StringToInt(strings[0], &width_) == false ||
  112. base::StringToInt(strings[1], &height_) == false) {
  113. width_ = -1; // force the dimension object to be invalid.
  114. }
  115. }
  116. private:
  117. int width_;
  118. int height_;
  119. };
  120. // main class used for the benchmarking.
  121. class Benchmark {
  122. public:
  123. static const int kDefaultNumberIterations;
  124. static const skia::ImageOperations::ResizeMethod kDefaultResizeMethod;
  125. Benchmark()
  126. : num_iterations_(kDefaultNumberIterations),
  127. method_(kDefaultResizeMethod) {}
  128. // Returns true if command line parsing was successful, false otherwise.
  129. bool ParseArgs(const base::CommandLine* command_line);
  130. // Returns true if successful, false otherwise.
  131. bool Run() const;
  132. static void Usage();
  133. private:
  134. int num_iterations_;
  135. skia::ImageOperations::ResizeMethod method_;
  136. Dimensions source_;
  137. Dimensions dest_;
  138. };
  139. // static
  140. const int Benchmark::kDefaultNumberIterations = 1024;
  141. const skia::ImageOperations::ResizeMethod Benchmark::kDefaultResizeMethod =
  142. skia::ImageOperations::RESIZE_LANCZOS3;
  143. // argument management
  144. void Benchmark::Usage() {
  145. printf("image_operations_bench -source wxh -destination wxh "
  146. "[-iterations i] [-method m] [-help]\n"
  147. " -source wxh: specify source width and height\n"
  148. " -destination wxh: specify destination width and height\n"
  149. " -iter i: perform i iterations (default:%d)\n"
  150. " -method m: use method m (default:%s), which can be:",
  151. Benchmark::kDefaultNumberIterations,
  152. MethodToString(Benchmark::kDefaultResizeMethod));
  153. PrintMethods();
  154. printf("\n -help: prints this help and exits\n");
  155. }
  156. bool Benchmark::ParseArgs(const base::CommandLine* command_line) {
  157. const base::CommandLine::SwitchMap& switches = command_line->GetSwitches();
  158. bool fNeedHelp = false;
  159. for (base::CommandLine::SwitchMap::const_iterator iter = switches.begin();
  160. iter != switches.end();
  161. ++iter) {
  162. const std::string& s = iter->first;
  163. std::string value;
  164. #if BUILDFLAG(IS_WIN)
  165. value = base::WideToUTF8(iter->second);
  166. #else
  167. value = iter->second;
  168. #endif
  169. if (s == "source") {
  170. source_.FromString(value);
  171. } else if (s == "destination") {
  172. dest_.FromString(value);
  173. } else if (s == "iterations") {
  174. if (base::StringToInt(value, &num_iterations_) == false) {
  175. fNeedHelp = true;
  176. }
  177. } else if (s == "method") {
  178. if (!StringToMethod(value, &method_)) {
  179. printf("Invalid method '%s' specified\n", value.c_str());
  180. fNeedHelp = true;
  181. }
  182. } else {
  183. fNeedHelp = true;
  184. }
  185. }
  186. if (num_iterations_ <= 0) {
  187. printf("Invalid number of iterations: %d\n", num_iterations_);
  188. fNeedHelp = true;
  189. }
  190. if (!source_.IsValid()) {
  191. printf("Invalid source dimensions specified\n");
  192. fNeedHelp = true;
  193. }
  194. if (!dest_.IsValid()) {
  195. printf("Invalid dest dimensions specified\n");
  196. fNeedHelp = true;
  197. }
  198. if (fNeedHelp == true) {
  199. return false;
  200. }
  201. return true;
  202. }
  203. // actual benchmark.
  204. bool Benchmark::Run() const {
  205. SkBitmap source;
  206. source.allocN32Pixels(source_.width(), source_.height());
  207. source.eraseARGB(0, 0, 0, 0);
  208. SkBitmap dest;
  209. const base::TimeTicks start = base::TimeTicks::Now();
  210. for (int i = 0; i < num_iterations_; ++i) {
  211. dest = skia::ImageOperations::Resize(source,
  212. method_,
  213. dest_.width(), dest_.height());
  214. }
  215. const int64_t elapsed_us = (base::TimeTicks::Now() - start).InMicroseconds();
  216. const uint64_t num_bytes = static_cast<uint64_t>(num_iterations_) *
  217. (GetBitmapSize(&source) + GetBitmapSize(&dest));
  218. printf("%" PRIu64 " MB/s,\telapsed = %" PRIu64 " source=%d dest=%d\n",
  219. static_cast<uint64_t>(elapsed_us == 0 ? 0 : num_bytes / elapsed_us),
  220. static_cast<uint64_t>(elapsed_us), GetBitmapSize(&source),
  221. GetBitmapSize(&dest));
  222. return true;
  223. }
  224. // A small class to automatically call Reset on the global command line to
  225. // avoid nasty valgrind complaints for the leak of the global command line.
  226. class CommandLineAutoReset {
  227. public:
  228. CommandLineAutoReset(int argc, char** argv) {
  229. base::CommandLine::Init(argc, argv);
  230. }
  231. ~CommandLineAutoReset() {
  232. base::CommandLine::Reset();
  233. }
  234. const base::CommandLine* Get() const {
  235. return base::CommandLine::ForCurrentProcess();
  236. }
  237. };
  238. } // namespace
  239. int main(int argc, char** argv) {
  240. Benchmark bench;
  241. CommandLineAutoReset command_line(argc, argv);
  242. if (!bench.ParseArgs(command_line.Get())) {
  243. Benchmark::Usage();
  244. return 1;
  245. }
  246. if (!bench.Run()) {
  247. printf("Failed to run benchmark\n");
  248. return 1;
  249. }
  250. return 0;
  251. }