Chromium Code Reviews| Index: skia/ext/image_operations_bench.cc |
| =================================================================== |
| --- skia/ext/image_operations_bench.cc (revision 0) |
| +++ skia/ext/image_operations_bench.cc (revision 0) |
| @@ -0,0 +1,279 @@ |
| +// Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#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.
|
| + |
| +#include "base/basictypes.h" |
| +#include "base/command_line.h" |
| +#include "base/format_macros.h" |
| +#include "base/string_number_conversions.h" |
| +#include "base/string_split.h" |
| +#include "base/string_util.h" |
| +#include "base/time.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "skia/ext/image_operations.h" |
| +#include "third_party/skia/include/core/SkBitmap.h" |
| +#include "third_party/skia/include/core/SkRect.h" |
| + |
| +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.
|
| +struct StringMethodPair { |
| + const char* name; |
| + skia::ImageOperations::ResizeMethod method; |
| +}; |
| +#define ADD_METHOD(x) { #x, skia::ImageOperations::RESIZE_##x } |
| +const StringMethodPair resize_methods[] = { |
| + ADD_METHOD(GOOD), |
| + ADD_METHOD(BETTER), |
| + ADD_METHOD(BEST), |
| + ADD_METHOD(BOX), |
| + ADD_METHOD(HAMMING1), |
| + ADD_METHOD(LANCZOS2), |
| + ADD_METHOD(LANCZOS3), |
| + ADD_METHOD(SUBPIXEL) |
| +}; |
| + |
| +// converts a string into one of the image operation method to resize. |
| +// @return true on success, false otherwise. |
| +bool StringToMethod(const std::string& arg, |
| + skia::ImageOperations::ResizeMethod* method) { |
| + for (size_t i = 0; i < arraysize(resize_methods); ++i) { |
| + if (base::strcasecmp(arg.c_str(), resize_methods[i].name) == 0) { |
| + *method = resize_methods[i].method; |
| + return true; |
| + } |
| + } |
| + return false; |
| +} |
| + |
| +const char* MethodToString(skia::ImageOperations::ResizeMethod method) { |
| + for (size_t i = 0; i < arraysize(resize_methods); ++i) { |
| + if (method == resize_methods[i].method) { |
| + return resize_methods[i].name; |
| + } |
| + } |
| + return "unknown"; |
| +} |
| + |
| +// Prints all supported resize methods |
| +void PrintMethods() { |
| + bool print_comma = false; |
| + for (size_t i = 0; i < arraysize(resize_methods); ++i) { |
| + if (print_comma) { |
| + printf(","); |
| + } else { |
| + print_comma = true; |
| + } |
| + printf(" %s", resize_methods[i].name); |
| + } |
| +} |
| + |
| +// @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.
|
| +// from what SkBitmap::getSize() returns since it does not take into account |
| +// the stride. The difference between the stride and the width can be large |
| +// because of the alignment constraints on bitmaps created for SRB scaling |
| +// (32 pixels). Using this metric instead of the getSize seemed to be a more |
| +// accurate representation of the work done (even though in terms of memory |
| +// bandwidth that might be similar because of the cache line size). |
| +// @return the number of bytes of the image. |
| +int GetBitmapSize(const SkBitmap* bitmap) { |
| + return bitmap->height() * bitmap->bytesPerPixel() * bitmap->width(); |
| +} |
| + |
| +// Simple class to represent dimensions of a bitmap (width, height). |
| +class Dimensions { |
| + public: |
| + 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 ':'
|
| + width_(0), |
| + height_(0) {} |
| + |
| + void set(int w, int h) { |
| + width_ = w; |
| + height_ = h; |
| + } |
| + |
| + int width() const { |
| + return width_; |
| + } |
| + |
| + int height() const { |
| + return height_; |
| + } |
| + |
| + bool IsValid() const { |
| + return (width_ > 0 && height_ > 0); |
| + } |
| + |
| + // On failure, will set its state in such a way that IsValid will return |
| + // false. |
| + void FromString(const std::string& arg) { |
| + std::vector<std::string> strings; |
| + base::SplitString(std::string(arg), 'x', &strings); |
| + if (strings.size() != 2 || |
| + base::StringToInt(strings[0], &width_) == false || |
| + base::StringToInt(strings[1], &height_) == false) { |
| + width_ = -1; // force the dimension object to be invalid. |
| + } |
| + } |
| + private: |
| + int width_; |
| + int height_; |
| +}; |
| + |
| +// main class used for the benchmarking. |
| +class Benchmark { |
| + public: |
| + static const int kDefaultNumberIterations; |
| + static const skia::ImageOperations::ResizeMethod kDefaultResizeMethod; |
| + |
| + Benchmark() : |
|
brettw
2010/12/17 20:07:55
Ditto.
evannier
2010/12/19 00:15:03
Done.
|
| + num_iterations_(kDefaultNumberIterations), |
| + method_(kDefaultResizeMethod) {} |
| + |
| + // @return true if command line parsing was successful, false otherwise. |
| + bool ParseArgs(const CommandLine* command_line); |
| + |
| + // @returns true if successful, false otherwise. |
| + bool Run() const; |
| + |
| + static void Usage(); |
| + private: |
| + int num_iterations_; |
| + skia::ImageOperations::ResizeMethod method_; |
| + Dimensions source_; |
| + Dimensions dest_; |
| +}; |
| + |
| +// static |
| +const int Benchmark::kDefaultNumberIterations = 1024; |
| +const skia::ImageOperations::ResizeMethod Benchmark::kDefaultResizeMethod = |
| + skia::ImageOperations::RESIZE_LANCZOS3; |
| + |
| +// argument management |
| +void Benchmark::Usage() { |
| + printf("image_operations_bench -source wxh -destination wxh " |
| + "[-iterations i] [-method m] [-help]\n" |
| + " -source wxh: specify source width and height\n" |
| + " -destination wxh: specify destination width and height\n" |
| + " -iter i: perform i iterations (default:%d)\n" |
| + " -method m: use method m (default:%s), which can be:", |
| + Benchmark::kDefaultNumberIterations, |
| + MethodToString(Benchmark::kDefaultResizeMethod)); |
| + PrintMethods(); |
| + printf("\n -help: prints this help and exits\n"); |
| +} |
| + |
| +bool Benchmark::ParseArgs(const CommandLine* command_line) { |
| + const CommandLine::SwitchMap& switches = command_line->GetSwitches(); |
| + bool fNeedHelp = false; |
| + |
| + for (CommandLine::SwitchMap::const_iterator iter = switches.begin(); |
| + iter != switches.end(); |
| + ++iter) { |
| + const std::string& s = iter->first; |
| + std::string value; |
| +#if defined(OS_WIN) |
| + value = WideToUTF8(iter->second); |
| +#else |
| + value = iter->second; |
| +#endif |
| + if (s == "source") { |
| + source_.FromString(value); |
| + } else if (s == "destination") { |
| + dest_.FromString(value); |
| + } else if (s == "iterations") { |
| + if (base::StringToInt(value, &num_iterations_) == false) { |
| + fNeedHelp = true; |
| + } |
| + } else if (s == "method") { |
| + if (!StringToMethod(value, &method_)) { |
| + printf("Invalid method '%s' specified\n", value.c_str()); |
| + fNeedHelp = true; |
| + } |
| + } else { |
| + fNeedHelp = true; |
| + } |
| + } |
| + |
| + if (num_iterations_ <= 0) { |
| + printf("Invalid number of iterations: %d\n", num_iterations_); |
| + fNeedHelp = true; |
| + } |
| + if (!source_.IsValid()) { |
| + printf("Invalid source dimensions specified\n"); |
| + fNeedHelp = true; |
| + } |
| + if (!dest_.IsValid()) { |
| + printf("Invalid dest dimensions specified\n"); |
| + fNeedHelp = true; |
| + } |
| + if (fNeedHelp == true) { |
| + return false; |
| + } |
| + return true; |
| +} |
| + |
| +// actual benchmark. |
| +bool Benchmark::Run() const { |
| + SkBitmap source; |
| + source.setConfig(SkBitmap::kARGB_8888_Config, |
| + source_.width(), source_.height()); |
| + source.allocPixels(); |
| + source.eraseARGB(0, 0, 0, 0); |
| + |
| + SkBitmap dest; |
| + |
| + const base::TimeTicks start = base::TimeTicks::Now(); |
| + |
| + for (int i = 0; i < num_iterations_; ++i) { |
| + dest = skia::ImageOperations::Resize(source, |
| + method_, |
| + dest_.width(), dest_.height()); |
| + } |
| + |
| + const int64 elapsed_us = (base::TimeTicks::Now() - start).InMicroseconds(); |
| + |
| + const uint64 num_bytes = static_cast<uint64>(num_iterations_) * |
| + (GetBitmapSize(&source) + GetBitmapSize(&dest)); |
| + |
| + printf("%"PRIu64" MB/s,\telapsed = %"PRIu64" source=%d dest=%d\n", |
| + static_cast<uint64>(elapsed_us == 0 ? 0 : num_bytes / elapsed_us), |
| + static_cast<uint64>(elapsed_us), |
| + GetBitmapSize(&source), GetBitmapSize(&dest)); |
| + |
| + return true; |
| +} |
| + |
| +// A small class to automatically call Reset on the global command line to |
| +// avoid nasty valgrind complaints for the leak of the global command line. |
| +class CommandLineAutoReset { |
| + public: |
| + CommandLineAutoReset(int argc, char** argv) { |
| + CommandLine::Init(argc, argv); |
| + } |
| + ~CommandLineAutoReset() { |
| + CommandLine::Reset(); |
| + } |
| + |
| + const CommandLine* Get() const { |
| + return CommandLine::ForCurrentProcess(); |
| + } |
| +}; |
| +} // anonymous namespace |
|
brettw
2010/12/17 20:07:55
Just "// namespace" is sufficient. Also, be sure
evannier
2010/12/19 00:15:03
Done.
|
| + |
| +int main(int argc, char** argv) { |
| + Benchmark bench; |
| + CommandLineAutoReset command_line(argc, argv); |
| + |
| + if (!bench.ParseArgs(command_line.Get())) { |
| + Benchmark::Usage(); |
| + return 1; |
| + } |
| + |
| + if (!bench.Run()) { |
| + printf("Failed to run benchmark\n"); |
| + return 1; |
| + } |
| + |
| + return 0; |
| +} |