This source file includes following definitions.
- StringToMethod
- MethodToString
- PrintMethods
- GetBitmapSize
- height_
- set
- width
- height
- IsValid
- FromString
- method_
- Usage
- ParseArgs
- Run
- Get
- main
#include <stdio.h>
#include "base/basictypes.h"
#include "base/command_line.h"
#include "base/format_macros.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "skia/ext/image_operations.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkRect.h"
namespace {
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)
};
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";
}
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);
}
}
int GetBitmapSize(const SkBitmap* bitmap) {
return bitmap->height() * bitmap->bytesPerPixel() * bitmap->width();
}
class Dimensions {
public:
Dimensions()
: 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);
}
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;
}
}
private:
int width_;
int height_;
};
class Benchmark {
public:
static const int kDefaultNumberIterations;
static const skia::ImageOperations::ResizeMethod kDefaultResizeMethod;
Benchmark()
: num_iterations_(kDefaultNumberIterations),
method_(kDefaultResizeMethod) {}
bool ParseArgs(const base::CommandLine* command_line);
bool Run() const;
static void Usage();
private:
int num_iterations_;
skia::ImageOperations::ResizeMethod method_;
Dimensions source_;
Dimensions dest_;
};
const int Benchmark::kDefaultNumberIterations = 1024;
const skia::ImageOperations::ResizeMethod Benchmark::kDefaultResizeMethod =
skia::ImageOperations::RESIZE_LANCZOS3;
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 base::CommandLine* command_line) {
const base::CommandLine::SwitchMap& switches = command_line->GetSwitches();
bool fNeedHelp = false;
for (base::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 = base::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;
}
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;
}
class CommandLineAutoReset {
public:
CommandLineAutoReset(int argc, char** argv) {
base::CommandLine::Init(argc, argv);
}
~CommandLineAutoReset() {
base::CommandLine::Reset();
}
const base::CommandLine* Get() const {
return base::CommandLine::ForCurrentProcess();
}
};
}
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;
}