blob: 3b169253ed1a02f5fb9342b220786a7e52903e89 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.comf523e252009-01-26 23:15:37 +00008#include "SkBenchmark.h"
9#include "SkBitmap.h"
10#include "SkPaint.h"
11#include "SkCanvas.h"
12#include "SkColorPriv.h"
13#include "SkRandom.h"
14#include "SkString.h"
15
16static const char* gTileName[] = {
17 "clamp", "repeat", "mirror"
18};
19
20static const char* gConfigName[] = {
21 "ERROR", "a1", "a8", "index8", "565", "4444", "8888"
22};
23
24static void drawIntoBitmap(const SkBitmap& bm) {
25 const int w = bm.width();
26 const int h = bm.height();
27
28 SkCanvas canvas(bm);
29 SkPaint p;
30 p.setAntiAlias(true);
31 p.setColor(SK_ColorRED);
32 canvas.drawCircle(SkIntToScalar(w)/2, SkIntToScalar(h)/2,
33 SkIntToScalar(SkMin32(w, h))*3/8, p);
34
35 SkRect r;
36 r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h));
37 p.setStyle(SkPaint::kStroke_Style);
38 p.setStrokeWidth(SkIntToScalar(4));
39 p.setColor(SK_ColorBLUE);
40 canvas.drawRect(r, p);
41}
42
43static int conv6ToByte(int x) {
44 return x * 0xFF / 5;
45}
46
47static int convByteTo6(int x) {
48 return x * 5 / 255;
49}
50
51static uint8_t compute666Index(SkPMColor c) {
52 int r = SkGetPackedR32(c);
53 int g = SkGetPackedG32(c);
54 int b = SkGetPackedB32(c);
55
56 return convByteTo6(r) * 36 + convByteTo6(g) * 6 + convByteTo6(b);
57}
58
59static void convertToIndex666(const SkBitmap& src, SkBitmap* dst) {
60 SkColorTable* ctable = new SkColorTable(216);
61 SkPMColor* colors = ctable->lockColors();
62 // rrr ggg bbb
63 for (int r = 0; r < 6; r++) {
64 int rr = conv6ToByte(r);
65 for (int g = 0; g < 6; g++) {
66 int gg = conv6ToByte(g);
67 for (int b = 0; b < 6; b++) {
68 int bb = conv6ToByte(b);
69 *colors++ = SkPreMultiplyARGB(0xFF, rr, gg, bb);
70 }
71 }
72 }
73 ctable->unlockColors(true);
74 dst->setConfig(SkBitmap::kIndex8_Config, src.width(), src.height());
75 dst->allocPixels(ctable);
76 ctable->unref();
77
78 SkAutoLockPixels alps(src);
79 SkAutoLockPixels alpd(*dst);
80
81 for (int y = 0; y < src.height(); y++) {
82 const SkPMColor* srcP = src.getAddr32(0, y);
83 uint8_t* dstP = dst->getAddr8(0, y);
84 for (int x = src.width() - 1; x >= 0; --x) {
85 *dstP++ = compute666Index(*srcP++);
86 }
87 }
88}
89
90/* Variants for bitmaps
91
92 - src depth (32 w+w/o alpha), 565, 4444, index, a8
93 - paint options: filtering, dither, alpha
94 - matrix options: translate, scale, rotate, persp
95 - tiling: none, repeat, mirror, clamp
96
97 */
98
99class BitmapBench : public SkBenchmark {
100 SkBitmap fBitmap;
101 SkPaint fPaint;
reed@android.com11ec1862009-10-19 19:01:45 +0000102 bool fIsOpaque;
junov@google.com4ee7ae52011-06-30 17:30:49 +0000103 bool fForceUpdate; //bitmap marked as dirty before each draw. forces bitmap to be updated on device cache
reed@android.comf523e252009-01-26 23:15:37 +0000104 int fTileX, fTileY; // -1 means don't use shader
105 SkString fName;
tomhudson@google.comca529d32011-10-28 15:34:49 +0000106 enum { N = SkBENCHLOOP(300) };
reed@android.comf523e252009-01-26 23:15:37 +0000107public:
reed@android.com11ec1862009-10-19 19:01:45 +0000108 BitmapBench(void* param, bool isOpaque, SkBitmap::Config c,
junov@google.com4ee7ae52011-06-30 17:30:49 +0000109 bool forceUpdate = false, bool bitmapVolatile = false,
reed@android.com11ec1862009-10-19 19:01:45 +0000110 int tx = -1, int ty = -1)
junov@google.com4ee7ae52011-06-30 17:30:49 +0000111 : INHERITED(param), fIsOpaque(isOpaque), fForceUpdate(forceUpdate), fTileX(tx), fTileY(ty) {
reed@android.comf523e252009-01-26 23:15:37 +0000112 const int w = 128;
113 const int h = 128;
114 SkBitmap bm;
115
116 if (SkBitmap::kIndex8_Config == c) {
117 bm.setConfig(SkBitmap::kARGB_8888_Config, w, h);
118 } else {
119 bm.setConfig(c, w, h);
120 }
121 bm.allocPixels();
reed@android.com11ec1862009-10-19 19:01:45 +0000122 bm.eraseColor(isOpaque ? SK_ColorBLACK : 0);
reed@android.comf523e252009-01-26 23:15:37 +0000123
124 drawIntoBitmap(bm);
125
126 if (SkBitmap::kIndex8_Config == c) {
127 convertToIndex666(bm, &fBitmap);
128 } else {
129 fBitmap = bm;
130 }
reed@android.com11ec1862009-10-19 19:01:45 +0000131
132 if (fBitmap.getColorTable()) {
133 fBitmap.getColorTable()->setIsOpaque(isOpaque);
134 }
135 fBitmap.setIsOpaque(isOpaque);
junov@google.com4ee7ae52011-06-30 17:30:49 +0000136 fBitmap.setIsVolatile(bitmapVolatile);
reed@android.comf523e252009-01-26 23:15:37 +0000137 }
138
139protected:
140 virtual const char* onGetName() {
141 fName.set("bitmap");
142 if (fTileX >= 0) {
143 fName.appendf("_%s", gTileName[fTileX]);
144 if (fTileY != fTileX) {
145 fName.appendf("_%s", gTileName[fTileY]);
146 }
147 }
reed@android.com11ec1862009-10-19 19:01:45 +0000148 fName.appendf("_%s%s", gConfigName[fBitmap.config()],
149 fIsOpaque ? "" : "_A");
junov@google.com4ee7ae52011-06-30 17:30:49 +0000150 if (fForceUpdate)
151 fName.append("_update");
152 if (fBitmap.isVolatile())
153 fName.append("_volatile");
154
reed@android.comf523e252009-01-26 23:15:37 +0000155 return fName.c_str();
156 }
157
158 virtual void onDraw(SkCanvas* canvas) {
159 SkIPoint dim = this->getSize();
160 SkRandom rand;
161
162 SkPaint paint(fPaint);
163 this->setupPaint(&paint);
164
165 const SkBitmap& bitmap = fBitmap;
166 const SkScalar x0 = SkIntToScalar(-bitmap.width() / 2);
167 const SkScalar y0 = SkIntToScalar(-bitmap.height() / 2);
168
169 for (int i = 0; i < N; i++) {
170 SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
171 SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
junov@google.com4ee7ae52011-06-30 17:30:49 +0000172
173 if (fForceUpdate)
174 bitmap.notifyPixelsChanged();
175
reed@android.comf523e252009-01-26 23:15:37 +0000176 canvas->drawBitmap(bitmap, x, y, &paint);
177 }
178 }
179
180private:
181 typedef SkBenchmark INHERITED;
182};
183
reed@android.com11ec1862009-10-19 19:01:45 +0000184static SkBenchmark* Fact0(void* p) { return new BitmapBench(p, false, SkBitmap::kARGB_8888_Config); }
185static SkBenchmark* Fact1(void* p) { return new BitmapBench(p, true, SkBitmap::kARGB_8888_Config); }
186static SkBenchmark* Fact2(void* p) { return new BitmapBench(p, true, SkBitmap::kRGB_565_Config); }
187static SkBenchmark* Fact3(void* p) { return new BitmapBench(p, false, SkBitmap::kARGB_4444_Config); }
188static SkBenchmark* Fact4(void* p) { return new BitmapBench(p, true, SkBitmap::kARGB_4444_Config); }
189static SkBenchmark* Fact5(void* p) { return new BitmapBench(p, false, SkBitmap::kIndex8_Config); }
190static SkBenchmark* Fact6(void* p) { return new BitmapBench(p, true, SkBitmap::kIndex8_Config); }
junov@google.com4ee7ae52011-06-30 17:30:49 +0000191static SkBenchmark* Fact7(void* p) { return new BitmapBench(p, true, SkBitmap::kARGB_8888_Config, true, true); }
192static SkBenchmark* Fact8(void* p) { return new BitmapBench(p, true, SkBitmap::kARGB_8888_Config, true, false); }
reed@android.comf523e252009-01-26 23:15:37 +0000193
194static BenchRegistry gReg0(Fact0);
195static BenchRegistry gReg1(Fact1);
196static BenchRegistry gReg2(Fact2);
197static BenchRegistry gReg3(Fact3);
reed@android.com11ec1862009-10-19 19:01:45 +0000198static BenchRegistry gReg4(Fact4);
199static BenchRegistry gReg5(Fact5);
200static BenchRegistry gReg6(Fact6);
junov@google.com4ee7ae52011-06-30 17:30:49 +0000201static BenchRegistry gReg7(Fact7);
202static BenchRegistry gReg8(Fact8);