• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "SkBenchmark.h"
2 #include "SkBitmap.h"
3 #include "SkCanvas.h"
4 #include "SkColorPriv.h"
5 #include "SkPaint.h"
6 #include "SkShader.h"
7 #include "SkString.h"
8 
9 static const char* gConfigName[] = {
10     "ERROR", "a1", "a8", "index8", "565", "4444", "8888"
11 };
12 
drawIntoBitmap(const SkBitmap & bm)13 static void drawIntoBitmap(const SkBitmap& bm) {
14     const int w = bm.width();
15     const int h = bm.height();
16 
17     SkCanvas canvas(bm);
18     SkPaint p;
19     p.setAntiAlias(true);
20     p.setColor(SK_ColorRED);
21     canvas.drawCircle(SkIntToScalar(w)/2, SkIntToScalar(h)/2,
22                       SkIntToScalar(SkMin32(w, h))*3/8, p);
23 
24     SkRect r;
25     r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h));
26     p.setStyle(SkPaint::kStroke_Style);
27     p.setStrokeWidth(SkIntToScalar(4));
28     p.setColor(SK_ColorBLUE);
29     canvas.drawRect(r, p);
30 }
31 
conv6ToByte(int x)32 static int conv6ToByte(int x) {
33     return x * 0xFF / 5;
34 }
35 
convByteTo6(int x)36 static int convByteTo6(int x) {
37     return x * 5 / 255;
38 }
39 
compute666Index(SkPMColor c)40 static uint8_t compute666Index(SkPMColor c) {
41     int r = SkGetPackedR32(c);
42     int g = SkGetPackedG32(c);
43     int b = SkGetPackedB32(c);
44 
45     return convByteTo6(r) * 36 + convByteTo6(g) * 6 + convByteTo6(b);
46 }
47 
convertToIndex666(const SkBitmap & src,SkBitmap * dst)48 static void convertToIndex666(const SkBitmap& src, SkBitmap* dst) {
49     SkColorTable* ctable = new SkColorTable(216);
50     SkPMColor* colors = ctable->lockColors();
51     // rrr ggg bbb
52     for (int r = 0; r < 6; r++) {
53         int rr = conv6ToByte(r);
54         for (int g = 0; g < 6; g++) {
55             int gg = conv6ToByte(g);
56             for (int b = 0; b < 6; b++) {
57                 int bb = conv6ToByte(b);
58                 *colors++ = SkPreMultiplyARGB(0xFF, rr, gg, bb);
59             }
60         }
61     }
62     ctable->unlockColors(true);
63     dst->setConfig(SkBitmap::kIndex8_Config, src.width(), src.height());
64     dst->allocPixels(ctable);
65     ctable->unref();
66 
67     SkAutoLockPixels alps(src);
68     SkAutoLockPixels alpd(*dst);
69 
70     for (int y = 0; y < src.height(); y++) {
71         const SkPMColor* srcP = src.getAddr32(0, y);
72         uint8_t* dstP = dst->getAddr8(0, y);
73         for (int x = src.width() - 1; x >= 0; --x) {
74             *dstP++ = compute666Index(*srcP++);
75         }
76     }
77 }
78 
79 class RepeatTileBench : public SkBenchmark {
80     SkPaint     fPaint;
81     SkString    fName;
82     enum { N = 20 };
83 public:
RepeatTileBench(void * param,SkBitmap::Config c)84     RepeatTileBench(void* param, SkBitmap::Config c) : INHERITED(param) {
85         const int w = 50;
86         const int h = 50;
87         SkBitmap bm;
88 
89         if (SkBitmap::kIndex8_Config == c) {
90             bm.setConfig(SkBitmap::kARGB_8888_Config, w, h);
91         } else {
92             bm.setConfig(c, w, h);
93         }
94         bm.allocPixels();
95         bm.eraseColor(0);
96 
97         drawIntoBitmap(bm);
98 
99         if (SkBitmap::kIndex8_Config == c) {
100             SkBitmap tmp;
101             convertToIndex666(bm, &tmp);
102             bm = tmp;
103         }
104 
105         SkShader* s = SkShader::CreateBitmapShader(bm,
106                                                    SkShader::kRepeat_TileMode,
107                                                    SkShader::kRepeat_TileMode);
108         fPaint.setShader(s)->unref();
109         fName.printf("repeatTile_%s", gConfigName[bm.config()]);
110     }
111 
112 protected:
onGetName()113     virtual const char* onGetName() {
114         return fName.c_str();
115     }
116 
onDraw(SkCanvas * canvas)117     virtual void onDraw(SkCanvas* canvas) {
118         SkPaint paint(fPaint);
119         this->setupPaint(&paint);
120 
121         for (int i = 0; i < N; i++) {
122             canvas->drawPaint(paint);
123         }
124     }
125 
126 private:
127     typedef SkBenchmark INHERITED;
128 };
129 
Fact0(void * p)130 static SkBenchmark* Fact0(void* p) { return new RepeatTileBench(p, SkBitmap::kARGB_8888_Config); }
Fact1(void * p)131 static SkBenchmark* Fact1(void* p) { return new RepeatTileBench(p, SkBitmap::kRGB_565_Config); }
Fact2(void * p)132 static SkBenchmark* Fact2(void* p) { return new RepeatTileBench(p, SkBitmap::kARGB_4444_Config); }
Fact3(void * p)133 static SkBenchmark* Fact3(void* p) { return new RepeatTileBench(p, SkBitmap::kIndex8_Config); }
134 
135 static BenchRegistry gReg0(Fact0);
136 static BenchRegistry gReg1(Fact1);
137 static BenchRegistry gReg2(Fact2);
138 static BenchRegistry gReg3(Fact3);
139