blob: ab7ccf4ccde1868aa217099ddec28ef92204ec82 [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
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000020static void draw_into_bitmap(const SkBitmap& bm) {
reed@android.com4d850592009-08-12 20:30:58 +000021 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
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000039static int conv_6_to_byte(int x) {
reed@android.com4d850592009-08-12 20:30:58 +000040 return x * 0xFF / 5;
41}
42
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000043static int conv_byte_to_6(int x) {
reed@android.com4d850592009-08-12 20:30:58 +000044 return x * 5 / 255;
45}
46
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000047static uint8_t compute_666_index(SkPMColor c) {
reed@android.com4d850592009-08-12 20:30:58 +000048 int r = SkGetPackedR32(c);
49 int g = SkGetPackedG32(c);
50 int b = SkGetPackedB32(c);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000051
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000052 return conv_byte_to_6(r) * 36 + conv_byte_to_6(g) * 6 + conv_byte_to_6(b);
reed@android.com4d850592009-08-12 20:30:58 +000053}
54
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000055static void convert_to_index666(const SkBitmap& src, SkBitmap* dst) {
reed@android.com4d850592009-08-12 20:30:58 +000056 SkColorTable* ctable = new SkColorTable(216);
57 SkPMColor* colors = ctable->lockColors();
58 // rrr ggg bbb
59 for (int r = 0; r < 6; r++) {
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000060 int rr = conv_6_to_byte(r);
reed@android.com4d850592009-08-12 20:30:58 +000061 for (int g = 0; g < 6; g++) {
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000062 int gg = conv_6_to_byte(g);
reed@android.com4d850592009-08-12 20:30:58 +000063 for (int b = 0; b < 6; b++) {
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000064 int bb = conv_6_to_byte(b);
reed@android.com4d850592009-08-12 20:30:58 +000065 *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) {
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000081 *dstP++ = compute_666_index(*srcP++);
reed@android.com4d850592009-08-12 20:30:58 +000082 }
83 }
84}
85
86class RepeatTileBench : public SkBenchmark {
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000087 SkPaint fPaint;
88 SkString fName;
89 SkBitmap fBitmap;
90 bool fIsOpaque;
91 SkBitmap::Config fConfig;
reed@android.com4d850592009-08-12 20:30:58 +000092public:
reed@google.com420f9e92012-10-16 19:47:54 +000093 RepeatTileBench(void* param, SkBitmap::Config c, bool isOpaque = false) : INHERITED(param) {
reed@android.com4d850592009-08-12 20:30:58 +000094 const int w = 50;
95 const int h = 50;
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000096 fConfig = c;
97 fIsOpaque = isOpaque;
reed@android.com4d850592009-08-12 20:30:58 +000098
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000099 if (SkBitmap::kIndex8_Config == fConfig) {
100 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, w, h);
reed@android.com4d850592009-08-12 20:30:58 +0000101 } else {
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +0000102 fBitmap.setConfig(fConfig, w, h);
reed@android.com4d850592009-08-12 20:30:58 +0000103 }
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +0000104 fName.printf("repeatTile_%s_%c",
105 gConfigName[fBitmap.config()], isOpaque ? 'X' : 'A');
reed@android.com4d850592009-08-12 20:30:58 +0000106 }
107
108protected:
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +0000109 virtual const char* onGetName() SK_OVERRIDE {
reed@android.com4d850592009-08-12 20:30:58 +0000110 return fName.c_str();
111 }
112
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +0000113 virtual void onPreDraw() SK_OVERRIDE {
114 fBitmap.allocPixels();
115 fBitmap.eraseColor(fIsOpaque ? SK_ColorWHITE : 0);
116 fBitmap.setIsOpaque(fIsOpaque);
117
118 draw_into_bitmap(fBitmap);
119
120 if (SkBitmap::kIndex8_Config == fConfig) {
121 SkBitmap tmp;
122 convert_to_index666(fBitmap, &tmp);
123 fBitmap = tmp;
124 }
125
126 SkShader* s = SkShader::CreateBitmapShader(fBitmap,
127 SkShader::kRepeat_TileMode,
128 SkShader::kRepeat_TileMode);
129 fPaint.setShader(s)->unref();
130 }
131
132
133 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
reed@android.com4d850592009-08-12 20:30:58 +0000134 SkPaint paint(fPaint);
135 this->setupPaint(&paint);
136
mtklein@google.comc2897432013-09-10 19:23:38 +0000137 for (int i = 0; i < this->getLoops(); i++) {
reed@android.com4d850592009-08-12 20:30:58 +0000138 canvas->drawPaint(paint);
139 }
140 }
141
142private:
143 typedef SkBenchmark INHERITED;
144};
145
reed@google.com420f9e92012-10-16 19:47:54 +0000146DEF_BENCH(return new RepeatTileBench(p, SkBitmap::kARGB_8888_Config, true))
147DEF_BENCH(return new RepeatTileBench(p, SkBitmap::kARGB_8888_Config, false))
148DEF_BENCH(return new RepeatTileBench(p, SkBitmap::kRGB_565_Config))
reed@google.com420f9e92012-10-16 19:47:54 +0000149DEF_BENCH(return new RepeatTileBench(p, SkBitmap::kIndex8_Config))