2009-01-05 03:34:50 +00:00
|
|
|
#include "SkCanvas.h"
|
2009-01-26 23:15:37 +00:00
|
|
|
#include "SkColorPriv.h"
|
2009-01-28 00:56:29 +00:00
|
|
|
#include "SkGraphics.h"
|
2009-01-07 11:47:57 +00:00
|
|
|
#include "SkImageEncoder.h"
|
2009-01-05 03:34:50 +00:00
|
|
|
#include "SkString.h"
|
2009-01-19 20:08:35 +00:00
|
|
|
#include "SkTime.h"
|
2009-01-05 03:34:50 +00:00
|
|
|
|
|
|
|
#include "SkBenchmark.h"
|
|
|
|
|
|
|
|
class Iter {
|
|
|
|
public:
|
|
|
|
Iter() {
|
|
|
|
fBench = BenchRegistry::Head();
|
|
|
|
}
|
|
|
|
|
|
|
|
SkBenchmark* next() {
|
|
|
|
if (fBench) {
|
|
|
|
BenchRegistry::Factory f = fBench->factory();
|
|
|
|
fBench = fBench->next();
|
2009-01-22 17:41:57 +00:00
|
|
|
return f(0);
|
2009-01-05 03:34:50 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const BenchRegistry* fBench;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void make_filename(const char name[], SkString* path) {
|
|
|
|
path->set(name);
|
|
|
|
for (int i = 0; name[i]; i++) {
|
|
|
|
switch (name[i]) {
|
|
|
|
case '/':
|
|
|
|
case '\\':
|
|
|
|
case ' ':
|
|
|
|
case ':':
|
|
|
|
path->writable_str()[i] = '-';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-21 03:15:13 +00:00
|
|
|
static void saveFile(const char name[], const char config[], const char dir[],
|
|
|
|
const SkBitmap& bm) {
|
|
|
|
SkBitmap copy;
|
|
|
|
if (!bm.copyTo(©, SkBitmap::kARGB_8888_Config)) {
|
|
|
|
return;
|
|
|
|
}
|
2009-01-26 23:15:37 +00:00
|
|
|
|
|
|
|
if (bm.config() == SkBitmap::kA8_Config) {
|
|
|
|
// turn alpha into gray-scale
|
|
|
|
size_t size = copy.getSize() >> 2;
|
|
|
|
SkPMColor* p = copy.getAddr32(0, 0);
|
|
|
|
for (size_t i = 0; i < size; i++) {
|
|
|
|
int c = (*p >> SK_A32_SHIFT) & 0xFF;
|
|
|
|
c = 255 - c;
|
|
|
|
c |= (c << 24) | (c << 16) | (c << 8);
|
|
|
|
*p++ = c | (SK_A32_MASK << SK_A32_SHIFT);
|
|
|
|
}
|
|
|
|
}
|
2009-01-21 03:15:13 +00:00
|
|
|
|
|
|
|
SkString str;
|
|
|
|
make_filename(name, &str);
|
|
|
|
str.appendf("_%s.png", config);
|
|
|
|
str.prepend(dir);
|
|
|
|
::remove(str.c_str());
|
|
|
|
SkImageEncoder::EncodeFile(str.c_str(), copy, SkImageEncoder::kPNG_Type,
|
|
|
|
100);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void performClip(SkCanvas* canvas, int w, int h) {
|
|
|
|
SkRect r;
|
|
|
|
|
|
|
|
r.set(SkIntToScalar(10), SkIntToScalar(10),
|
|
|
|
SkIntToScalar(w*2/3), SkIntToScalar(h*2/3));
|
|
|
|
canvas->clipRect(r, SkRegion::kIntersect_Op);
|
|
|
|
|
|
|
|
r.set(SkIntToScalar(w/3), SkIntToScalar(h/3),
|
|
|
|
SkIntToScalar(w-10), SkIntToScalar(h-10));
|
|
|
|
canvas->clipRect(r, SkRegion::kXOR_Op);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void performRotate(SkCanvas* canvas, int w, int h) {
|
|
|
|
const SkScalar x = SkIntToScalar(w) / 2;
|
|
|
|
const SkScalar y = SkIntToScalar(h) / 2;
|
|
|
|
|
|
|
|
canvas->translate(x, y);
|
|
|
|
canvas->rotate(SkIntToScalar(35));
|
|
|
|
canvas->translate(-x, -y);
|
|
|
|
}
|
|
|
|
|
2009-01-19 20:08:35 +00:00
|
|
|
static const struct {
|
|
|
|
SkBitmap::Config fConfig;
|
|
|
|
const char* fName;
|
|
|
|
} gConfigs[] = {
|
|
|
|
{ SkBitmap::kARGB_8888_Config, "8888" },
|
|
|
|
{ SkBitmap::kRGB_565_Config, "565", },
|
|
|
|
{ SkBitmap::kARGB_4444_Config, "4444", },
|
|
|
|
{ SkBitmap::kA8_Config, "A8", }
|
|
|
|
};
|
|
|
|
|
2009-01-21 03:15:13 +00:00
|
|
|
static int findConfig(const char config[]) {
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) {
|
|
|
|
if (!strcmp(config, gConfigs[i].fName)) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-01-28 00:56:29 +00:00
|
|
|
class SkAutoGraphics {
|
|
|
|
public:
|
2009-03-07 03:39:23 +00:00
|
|
|
SkAutoGraphics() {
|
|
|
|
SkGraphics::Init();
|
2009-01-28 00:56:29 +00:00
|
|
|
}
|
|
|
|
~SkAutoGraphics() {
|
|
|
|
SkGraphics::Term();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-01-05 03:34:50 +00:00
|
|
|
int main (int argc, char * const argv[]) {
|
2009-01-28 00:56:29 +00:00
|
|
|
SkAutoGraphics ag;
|
|
|
|
|
2009-01-19 20:08:35 +00:00
|
|
|
int repeatDraw = 1;
|
|
|
|
int forceAlpha = 0xFF;
|
|
|
|
bool forceAA = true;
|
2009-01-21 03:15:13 +00:00
|
|
|
bool doRotate = false;
|
|
|
|
bool doClip = false;
|
2009-01-19 20:08:35 +00:00
|
|
|
|
2009-01-07 11:47:57 +00:00
|
|
|
SkString outDir;
|
|
|
|
SkBitmap::Config outConfig = SkBitmap::kARGB_8888_Config;
|
|
|
|
|
|
|
|
char* const* stop = argv + argc;
|
|
|
|
for (++argv; argv < stop; ++argv) {
|
|
|
|
if (strcmp(*argv, "-o") == 0) {
|
|
|
|
argv++;
|
|
|
|
if (argv < stop && **argv) {
|
|
|
|
outDir.set(*argv);
|
|
|
|
if (outDir.c_str()[outDir.size() - 1] != '/') {
|
|
|
|
outDir.append("/");
|
|
|
|
}
|
|
|
|
}
|
2009-01-19 20:08:35 +00:00
|
|
|
} else if (strcmp(*argv, "-repeat") == 0) {
|
|
|
|
argv++;
|
|
|
|
if (argv < stop) {
|
|
|
|
repeatDraw = atoi(*argv);
|
|
|
|
if (repeatDraw < 1) {
|
|
|
|
repeatDraw = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "missing arg for -repeat\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2009-01-21 03:15:13 +00:00
|
|
|
} else if (!strcmp(*argv, "-rotate")) {
|
|
|
|
doRotate = true;
|
|
|
|
} else if (!strcmp(*argv, "-clip")) {
|
|
|
|
doClip = true;
|
2009-01-19 20:08:35 +00:00
|
|
|
} else if (strcmp(*argv, "-forceAA") == 0) {
|
|
|
|
forceAA = true;
|
|
|
|
} else if (strcmp(*argv, "-forceBW") == 0) {
|
|
|
|
forceAA = false;
|
|
|
|
} else if (strcmp(*argv, "-forceBlend") == 0) {
|
|
|
|
forceAlpha = 0x80;
|
|
|
|
} else if (strcmp(*argv, "-forceOpaque") == 0) {
|
|
|
|
forceAlpha = 0xFF;
|
2009-01-21 03:15:13 +00:00
|
|
|
} else {
|
|
|
|
int index = findConfig(*argv);
|
|
|
|
if (index >= 0) {
|
|
|
|
outConfig = gConfigs[index].fConfig;
|
|
|
|
}
|
2009-01-07 11:47:57 +00:00
|
|
|
}
|
|
|
|
}
|
2009-01-19 20:08:35 +00:00
|
|
|
|
|
|
|
const char* configName = "";
|
|
|
|
int configCount = SK_ARRAY_COUNT(gConfigs);
|
|
|
|
|
2009-01-05 03:34:50 +00:00
|
|
|
Iter iter;
|
|
|
|
SkBenchmark* bench;
|
|
|
|
while ((bench = iter.next()) != NULL) {
|
2009-01-07 11:47:57 +00:00
|
|
|
SkIPoint dim = bench->getSize();
|
|
|
|
if (dim.fX <= 0 || dim.fY <= 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-01-19 20:08:35 +00:00
|
|
|
bench->setForceAlpha(forceAlpha);
|
|
|
|
bench->setForceAA(forceAA);
|
|
|
|
|
2009-01-26 23:15:37 +00:00
|
|
|
printf("running bench %16s", bench->getName());
|
2009-01-19 20:08:35 +00:00
|
|
|
|
|
|
|
for (int configIndex = 0; configIndex < configCount; configIndex++) {
|
|
|
|
if (configCount > 1) {
|
|
|
|
outConfig = gConfigs[configIndex].fConfig;
|
|
|
|
configName = gConfigs[configIndex].fName;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkBitmap bm;
|
|
|
|
bm.setConfig(outConfig, dim.fX, dim.fY);
|
|
|
|
bm.allocPixels();
|
|
|
|
|
2009-01-26 23:15:37 +00:00
|
|
|
if (bm.config() == SkBitmap::kA8_Config) {
|
|
|
|
bm.eraseColor(0);
|
|
|
|
} else {
|
|
|
|
bm.eraseColor(SK_ColorWHITE);
|
|
|
|
}
|
|
|
|
|
2009-01-19 20:08:35 +00:00
|
|
|
SkCanvas canvas(bm);
|
2009-01-26 23:15:37 +00:00
|
|
|
|
2009-01-21 03:15:13 +00:00
|
|
|
if (doClip) {
|
|
|
|
performClip(&canvas, dim.fX, dim.fY);
|
|
|
|
}
|
|
|
|
if (doRotate) {
|
|
|
|
performRotate(&canvas, dim.fX, dim.fY);
|
|
|
|
}
|
|
|
|
|
2009-01-19 20:08:35 +00:00
|
|
|
SkMSec now = SkTime::GetMSecs();
|
|
|
|
for (int i = 0; i < repeatDraw; i++) {
|
2009-01-21 03:15:13 +00:00
|
|
|
SkAutoCanvasRestore acr(&canvas, true);
|
2009-01-19 20:08:35 +00:00
|
|
|
bench->draw(&canvas);
|
|
|
|
}
|
|
|
|
if (repeatDraw > 1) {
|
|
|
|
printf(" %4s:%7.2f", configName,
|
|
|
|
(SkTime::GetMSecs() - now) / (double)repeatDraw);
|
|
|
|
}
|
2009-01-21 03:15:13 +00:00
|
|
|
if (outDir.size() > 0) {
|
|
|
|
saveFile(bench->getName(), configName, outDir.c_str(), bm);
|
|
|
|
}
|
2009-01-19 20:08:35 +00:00
|
|
|
}
|
|
|
|
printf("\n");
|
2009-01-05 03:34:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|