1
#include "SkBenchmark.h"
5
#include "SkColorPriv.h"
9
static const char* gTileName[] = {
10
"clamp", "repeat", "mirror"
13
static const char* gConfigName[] = {
14
"ERROR", "a1", "a8", "index8", "565", "4444", "8888"
17
static void drawIntoBitmap(const SkBitmap& bm) {
18
const int w = bm.width();
19
const int h = bm.height();
24
p.setColor(SK_ColorRED);
25
canvas.drawCircle(SkIntToScalar(w)/2, SkIntToScalar(h)/2,
26
SkIntToScalar(SkMin32(w, h))*3/8, p);
29
r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h));
30
p.setStyle(SkPaint::kStroke_Style);
31
p.setStrokeWidth(SkIntToScalar(4));
32
p.setColor(SK_ColorBLUE);
33
canvas.drawRect(r, p);
36
static int conv6ToByte(int x) {
40
static int convByteTo6(int x) {
44
static uint8_t compute666Index(SkPMColor c) {
45
int r = SkGetPackedR32(c);
46
int g = SkGetPackedG32(c);
47
int b = SkGetPackedB32(c);
49
return convByteTo6(r) * 36 + convByteTo6(g) * 6 + convByteTo6(b);
52
static void convertToIndex666(const SkBitmap& src, SkBitmap* dst) {
53
SkColorTable* ctable = new SkColorTable(216);
54
SkPMColor* colors = ctable->lockColors();
56
for (int r = 0; r < 6; r++) {
57
int rr = conv6ToByte(r);
58
for (int g = 0; g < 6; g++) {
59
int gg = conv6ToByte(g);
60
for (int b = 0; b < 6; b++) {
61
int bb = conv6ToByte(b);
62
*colors++ = SkPreMultiplyARGB(0xFF, rr, gg, bb);
66
ctable->unlockColors(true);
67
dst->setConfig(SkBitmap::kIndex8_Config, src.width(), src.height());
68
dst->allocPixels(ctable);
71
SkAutoLockPixels alps(src);
72
SkAutoLockPixels alpd(*dst);
74
for (int y = 0; y < src.height(); y++) {
75
const SkPMColor* srcP = src.getAddr32(0, y);
76
uint8_t* dstP = dst->getAddr8(0, y);
77
for (int x = src.width() - 1; x >= 0; --x) {
78
*dstP++ = compute666Index(*srcP++);
83
/* Variants for bitmaps
85
- src depth (32 w+w/o alpha), 565, 4444, index, a8
86
- paint options: filtering, dither, alpha
87
- matrix options: translate, scale, rotate, persp
88
- tiling: none, repeat, mirror, clamp
92
class BitmapBench : public SkBenchmark {
96
int fTileX, fTileY; // -1 means don't use shader
100
BitmapBench(void* param, bool isOpaque, SkBitmap::Config c,
101
int tx = -1, int ty = -1)
102
: INHERITED(param), fIsOpaque(isOpaque), fTileX(tx), fTileY(ty) {
107
if (SkBitmap::kIndex8_Config == c) {
108
bm.setConfig(SkBitmap::kARGB_8888_Config, w, h);
110
bm.setConfig(c, w, h);
113
bm.eraseColor(isOpaque ? SK_ColorBLACK : 0);
117
if (SkBitmap::kIndex8_Config == c) {
118
convertToIndex666(bm, &fBitmap);
123
if (fBitmap.getColorTable()) {
124
fBitmap.getColorTable()->setIsOpaque(isOpaque);
126
fBitmap.setIsOpaque(isOpaque);
130
virtual const char* onGetName() {
133
fName.appendf("_%s", gTileName[fTileX]);
134
if (fTileY != fTileX) {
135
fName.appendf("_%s", gTileName[fTileY]);
138
fName.appendf("_%s%s", gConfigName[fBitmap.config()],
139
fIsOpaque ? "" : "_A");
140
return fName.c_str();
143
virtual void onDraw(SkCanvas* canvas) {
144
SkIPoint dim = this->getSize();
147
SkPaint paint(fPaint);
148
this->setupPaint(&paint);
150
const SkBitmap& bitmap = fBitmap;
151
const SkScalar x0 = SkIntToScalar(-bitmap.width() / 2);
152
const SkScalar y0 = SkIntToScalar(-bitmap.height() / 2);
154
for (int i = 0; i < N; i++) {
155
SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
156
SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
157
canvas->drawBitmap(bitmap, x, y, &paint);
162
typedef SkBenchmark INHERITED;
165
static SkBenchmark* Fact0(void* p) { return new BitmapBench(p, false, SkBitmap::kARGB_8888_Config); }
166
static SkBenchmark* Fact1(void* p) { return new BitmapBench(p, true, SkBitmap::kARGB_8888_Config); }
167
static SkBenchmark* Fact2(void* p) { return new BitmapBench(p, true, SkBitmap::kRGB_565_Config); }
168
static SkBenchmark* Fact3(void* p) { return new BitmapBench(p, false, SkBitmap::kARGB_4444_Config); }
169
static SkBenchmark* Fact4(void* p) { return new BitmapBench(p, true, SkBitmap::kARGB_4444_Config); }
170
static SkBenchmark* Fact5(void* p) { return new BitmapBench(p, false, SkBitmap::kIndex8_Config); }
171
static SkBenchmark* Fact6(void* p) { return new BitmapBench(p, true, SkBitmap::kIndex8_Config); }
173
static BenchRegistry gReg0(Fact0);
174
static BenchRegistry gReg1(Fact1);
175
static BenchRegistry gReg2(Fact2);
176
static BenchRegistry gReg3(Fact3);
177
static BenchRegistry gReg4(Fact4);
178
static BenchRegistry gReg5(Fact5);
179
static BenchRegistry gReg6(Fact6);