blob: 11dd6f899ba4288baadcd871483b9895da3a11e3 [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.com4d850592009-08-12 20:30:58 +00008#include "SkBenchmark.h"
9#include "SkBitmap.h"
10#include "SkCanvas.h"
11#include "SkColorPriv.h"
12#include "SkPaint.h"
13#include "SkShader.h"
14#include "SkString.h"
15
16static const char* gConfigName[] = {
17 "ERROR", "a1", "a8", "index8", "565", "4444", "8888"
18};
19
20static void drawIntoBitmap(const SkBitmap& bm) {
21 const int w = bm.width();
22 const int h = bm.height();
23
24 SkCanvas canvas(bm);
25 SkPaint p;
26 p.setAntiAlias(true);
27 p.setColor(SK_ColorRED);
28 canvas.drawCircle(SkIntToScalar(w)/2, SkIntToScalar(h)/2,
29 SkIntToScalar(SkMin32(w, h))*3/8, p);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000030
reed@android.com4d850592009-08-12 20:30:58 +000031 SkRect r;
32 r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h));
33 p.setStyle(SkPaint::kStroke_Style);
34 p.setStrokeWidth(SkIntToScalar(4));
35 p.setColor(SK_ColorBLUE);
36 canvas.drawRect(r, p);
37}
38
39static int conv6ToByte(int x) {
40 return x * 0xFF / 5;
41}
42
43static int convByteTo6(int x) {
44 return x * 5 / 255;
45}
46
47static uint8_t compute666Index(SkPMColor c) {
48 int r = SkGetPackedR32(c);
49 int g = SkGetPackedG32(c);
50 int b = SkGetPackedB32(c);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000051
reed@android.com4d850592009-08-12 20:30:58 +000052 return convByteTo6(r) * 36 + convByteTo6(g) * 6 + convByteTo6(b);
53}
54
55static void convertToIndex666(const SkBitmap& src, SkBitmap* dst) {
56 SkColorTable* ctable = new SkColorTable(216);
57 SkPMColor* colors = ctable->lockColors();
58 // rrr ggg bbb
59 for (int r = 0; r < 6; r++) {
60 int rr = conv6ToByte(r);
61 for (int g = 0; g < 6; g++) {
62 int gg = conv6ToByte(g);
63 for (int b = 0; b < 6; b++) {
64 int bb = conv6ToByte(b);
65 *colors++ = SkPreMultiplyARGB(0xFF, rr, gg, bb);
66 }
67 }
68 }
69 ctable->unlockColors(true);
70 dst->setConfig(SkBitmap::kIndex8_Config, src.width(), src.height());
71 dst->allocPixels(ctable);
72 ctable->unref();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000073
reed@android.com4d850592009-08-12 20:30:58 +000074 SkAutoLockPixels alps(src);
75 SkAutoLockPixels alpd(*dst);
76
77 for (int y = 0; y < src.height(); y++) {
78 const SkPMColor* srcP = src.getAddr32(0, y);
79 uint8_t* dstP = dst->getAddr8(0, y);
80 for (int x = src.width() - 1; x >= 0; --x) {
81 *dstP++ = compute666Index(*srcP++);
82 }
83 }
84}
85
86class RepeatTileBench : public SkBenchmark {
87 SkPaint fPaint;
88 SkString fName;
tomhudson@google.comca529d32011-10-28 15:34:49 +000089 enum { N = SkBENCHLOOP(20) };
reed@android.com4d850592009-08-12 20:30:58 +000090public:
reed@google.com420f9e92012-10-16 19:47:54 +000091 RepeatTileBench(void* param, SkBitmap::Config c, bool isOpaque = false) : INHERITED(param) {
reed@android.com4d850592009-08-12 20:30:58 +000092 const int w = 50;
93 const int h = 50;
94 SkBitmap bm;
95
96 if (SkBitmap::kIndex8_Config == c) {
97 bm.setConfig(SkBitmap::kARGB_8888_Config, w, h);
98 } else {
99 bm.setConfig(c, w, h);
100 }
101 bm.allocPixels();
reed@google.com420f9e92012-10-16 19:47:54 +0000102 bm.eraseColor(isOpaque ? SK_ColorWHITE : 0);
103 bm.setIsOpaque(isOpaque);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000104
reed@android.com4d850592009-08-12 20:30:58 +0000105 drawIntoBitmap(bm);
106
107 if (SkBitmap::kIndex8_Config == c) {
108 SkBitmap tmp;
109 convertToIndex666(bm, &tmp);
110 bm = tmp;
111 }
112
113 SkShader* s = SkShader::CreateBitmapShader(bm,
114 SkShader::kRepeat_TileMode,
115 SkShader::kRepeat_TileMode);
116 fPaint.setShader(s)->unref();
reed@google.com420f9e92012-10-16 19:47:54 +0000117 fName.printf("repeatTile_%s_%c", gConfigName[bm.config()], isOpaque ? 'X' : 'A');
reed@android.com4d850592009-08-12 20:30:58 +0000118 }
119
120protected:
121 virtual const char* onGetName() {
122 return fName.c_str();
123 }
124
125 virtual void onDraw(SkCanvas* canvas) {
126 SkPaint paint(fPaint);
127 this->setupPaint(&paint);
128
129 for (int i = 0; i < N; i++) {
130 canvas->drawPaint(paint);
131 }
132 }
133
134private:
135 typedef SkBenchmark INHERITED;
136};
137
reed@google.com420f9e92012-10-16 19:47:54 +0000138DEF_BENCH(return new RepeatTileBench(p, SkBitmap::kARGB_8888_Config, true))
139DEF_BENCH(return new RepeatTileBench(p, SkBitmap::kARGB_8888_Config, false))
140DEF_BENCH(return new RepeatTileBench(p, SkBitmap::kRGB_565_Config))
reed@google.com420f9e92012-10-16 19:47:54 +0000141DEF_BENCH(return new RepeatTileBench(p, SkBitmap::kIndex8_Config))