~jammy-zhou/+junk/skia

« back to all changes in this revision

Viewing changes to bench/.svn/text-base/BitmapBench.cpp.svn-base

  • Committer: Jammy Zhou
  • Date: 2010-11-05 22:47:35 UTC
  • Revision ID: jammy.zhou@linaro.org-20101105224735-i1miyqbyxwslg7t2
initial version (upstream r622)

http://code.google.com/p/skia/source/list

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "SkBenchmark.h"
 
2
#include "SkBitmap.h"
 
3
#include "SkPaint.h"
 
4
#include "SkCanvas.h"
 
5
#include "SkColorPriv.h"
 
6
#include "SkRandom.h"
 
7
#include "SkString.h"
 
8
 
 
9
static const char* gTileName[] = {
 
10
    "clamp", "repeat", "mirror"
 
11
};
 
12
 
 
13
static const char* gConfigName[] = {
 
14
    "ERROR", "a1", "a8", "index8", "565", "4444", "8888"
 
15
};
 
16
 
 
17
static void drawIntoBitmap(const SkBitmap& bm) {
 
18
    const int w = bm.width();
 
19
    const int h = bm.height();
 
20
 
 
21
    SkCanvas canvas(bm);
 
22
    SkPaint p;
 
23
    p.setAntiAlias(true);
 
24
    p.setColor(SK_ColorRED);
 
25
    canvas.drawCircle(SkIntToScalar(w)/2, SkIntToScalar(h)/2,
 
26
                      SkIntToScalar(SkMin32(w, h))*3/8, p);
 
27
    
 
28
    SkRect r;
 
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);
 
34
}
 
35
 
 
36
static int conv6ToByte(int x) {
 
37
    return x * 0xFF / 5;
 
38
}
 
39
 
 
40
static int convByteTo6(int x) {
 
41
    return x * 5 / 255;
 
42
}
 
43
 
 
44
static uint8_t compute666Index(SkPMColor c) {
 
45
    int r = SkGetPackedR32(c);
 
46
    int g = SkGetPackedG32(c);
 
47
    int b = SkGetPackedB32(c);
 
48
    
 
49
    return convByteTo6(r) * 36 + convByteTo6(g) * 6 + convByteTo6(b);
 
50
}
 
51
 
 
52
static void convertToIndex666(const SkBitmap& src, SkBitmap* dst) {
 
53
    SkColorTable* ctable = new SkColorTable(216);
 
54
    SkPMColor* colors = ctable->lockColors();
 
55
    // rrr ggg bbb
 
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);
 
63
            }
 
64
        }
 
65
    }
 
66
    ctable->unlockColors(true);
 
67
    dst->setConfig(SkBitmap::kIndex8_Config, src.width(), src.height());
 
68
    dst->allocPixels(ctable);
 
69
    ctable->unref();
 
70
    
 
71
    SkAutoLockPixels alps(src);
 
72
    SkAutoLockPixels alpd(*dst);
 
73
 
 
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++);
 
79
        }
 
80
    }
 
81
}
 
82
 
 
83
/*  Variants for bitmaps
 
84
 
 
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
 
89
    
 
90
 */
 
91
 
 
92
class BitmapBench : public SkBenchmark {
 
93
    SkBitmap    fBitmap;
 
94
    SkPaint     fPaint;
 
95
    bool        fIsOpaque;
 
96
    int         fTileX, fTileY; // -1 means don't use shader
 
97
    SkString    fName;
 
98
    enum { N = 300 };
 
99
public:
 
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) {
 
103
        const int w = 128;
 
104
        const int h = 128;
 
105
        SkBitmap bm;
 
106
 
 
107
        if (SkBitmap::kIndex8_Config == c) {
 
108
            bm.setConfig(SkBitmap::kARGB_8888_Config, w, h);
 
109
        } else {
 
110
            bm.setConfig(c, w, h);
 
111
        }
 
112
        bm.allocPixels();
 
113
        bm.eraseColor(isOpaque ? SK_ColorBLACK : 0);
 
114
        
 
115
        drawIntoBitmap(bm);
 
116
 
 
117
        if (SkBitmap::kIndex8_Config == c) {
 
118
            convertToIndex666(bm, &fBitmap);
 
119
        } else {
 
120
            fBitmap = bm;
 
121
        }
 
122
 
 
123
        if (fBitmap.getColorTable()) {
 
124
            fBitmap.getColorTable()->setIsOpaque(isOpaque);
 
125
        }
 
126
        fBitmap.setIsOpaque(isOpaque);
 
127
    }
 
128
 
 
129
protected:
 
130
    virtual const char* onGetName() {
 
131
        fName.set("bitmap");
 
132
        if (fTileX >= 0) {
 
133
            fName.appendf("_%s", gTileName[fTileX]);
 
134
            if (fTileY != fTileX) {
 
135
                fName.appendf("_%s", gTileName[fTileY]);
 
136
            }
 
137
        }
 
138
        fName.appendf("_%s%s", gConfigName[fBitmap.config()],
 
139
                      fIsOpaque ? "" : "_A");
 
140
        return fName.c_str();
 
141
    }
 
142
 
 
143
    virtual void onDraw(SkCanvas* canvas) {
 
144
        SkIPoint dim = this->getSize();
 
145
        SkRandom rand;
 
146
 
 
147
        SkPaint paint(fPaint);
 
148
        this->setupPaint(&paint);
 
149
 
 
150
        const SkBitmap& bitmap = fBitmap;
 
151
        const SkScalar x0 = SkIntToScalar(-bitmap.width() / 2);
 
152
        const SkScalar y0 = SkIntToScalar(-bitmap.height() / 2);
 
153
        
 
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);
 
158
        }
 
159
    }
 
160
 
 
161
private:
 
162
    typedef SkBenchmark INHERITED;
 
163
};
 
164
 
 
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); }
 
172
 
 
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);