blob: fcbffb2a10be5b0ca0ab83f0b26a5cde085a5e90 [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
reed@google.com0a6151d2013-10-10 14:44:56 +000055static void convert_to_index666(const SkBitmap& src, SkBitmap* dst,
56 bool isOpaque) {
57 SkPMColor storage[216];
58 SkPMColor* colors = storage;
reed@android.com4d850592009-08-12 20:30:58 +000059 // rrr ggg bbb
60 for (int r = 0; r < 6; r++) {
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000061 int rr = conv_6_to_byte(r);
reed@android.com4d850592009-08-12 20:30:58 +000062 for (int g = 0; g < 6; g++) {
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000063 int gg = conv_6_to_byte(g);
reed@android.com4d850592009-08-12 20:30:58 +000064 for (int b = 0; b < 6; b++) {
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000065 int bb = conv_6_to_byte(b);
reed@android.com4d850592009-08-12 20:30:58 +000066 *colors++ = SkPreMultiplyARGB(0xFF, rr, gg, bb);
67 }
68 }
69 }
reed@google.com0a6151d2013-10-10 14:44:56 +000070 SkAlphaType aType = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
71 SkColorTable* ctable = new SkColorTable(storage, 216, aType);
reed@android.com4d850592009-08-12 20:30:58 +000072 dst->setConfig(SkBitmap::kIndex8_Config, src.width(), src.height());
73 dst->allocPixels(ctable);
74 ctable->unref();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000075
reed@android.com4d850592009-08-12 20:30:58 +000076 SkAutoLockPixels alps(src);
77 SkAutoLockPixels alpd(*dst);
78
79 for (int y = 0; y < src.height(); y++) {
80 const SkPMColor* srcP = src.getAddr32(0, y);
81 uint8_t* dstP = dst->getAddr8(0, y);
82 for (int x = src.width() - 1; x >= 0; --x) {
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000083 *dstP++ = compute_666_index(*srcP++);
reed@android.com4d850592009-08-12 20:30:58 +000084 }
85 }
86}
87
88class RepeatTileBench : public SkBenchmark {
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000089 SkPaint fPaint;
90 SkString fName;
91 SkBitmap fBitmap;
92 bool fIsOpaque;
93 SkBitmap::Config fConfig;
reed@android.com4d850592009-08-12 20:30:58 +000094public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000095 RepeatTileBench(SkBitmap::Config c, bool isOpaque = false) {
reed@android.com4d850592009-08-12 20:30:58 +000096 const int w = 50;
97 const int h = 50;
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000098 fConfig = c;
99 fIsOpaque = isOpaque;
reed@android.com4d850592009-08-12 20:30:58 +0000100
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +0000101 if (SkBitmap::kIndex8_Config == fConfig) {
102 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, w, h);
reed@android.com4d850592009-08-12 20:30:58 +0000103 } else {
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +0000104 fBitmap.setConfig(fConfig, w, h);
reed@android.com4d850592009-08-12 20:30:58 +0000105 }
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +0000106 fName.printf("repeatTile_%s_%c",
107 gConfigName[fBitmap.config()], isOpaque ? 'X' : 'A');
reed@android.com4d850592009-08-12 20:30:58 +0000108 }
109
110protected:
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +0000111 virtual const char* onGetName() SK_OVERRIDE {
reed@android.com4d850592009-08-12 20:30:58 +0000112 return fName.c_str();
113 }
114
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +0000115 virtual void onPreDraw() SK_OVERRIDE {
116 fBitmap.allocPixels();
117 fBitmap.eraseColor(fIsOpaque ? SK_ColorWHITE : 0);
reed@google.com383a6972013-10-21 14:00:07 +0000118 fBitmap.setAlphaType(fIsOpaque ?
119 kOpaque_SkAlphaType : kPremul_SkAlphaType);
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +0000120
121 draw_into_bitmap(fBitmap);
122
123 if (SkBitmap::kIndex8_Config == fConfig) {
124 SkBitmap tmp;
reed@google.com0a6151d2013-10-10 14:44:56 +0000125 convert_to_index666(fBitmap, &tmp, fIsOpaque);
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +0000126 fBitmap = tmp;
127 }
128
129 SkShader* s = SkShader::CreateBitmapShader(fBitmap,
130 SkShader::kRepeat_TileMode,
131 SkShader::kRepeat_TileMode);
132 fPaint.setShader(s)->unref();
133 }
134
135
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000136 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
reed@android.com4d850592009-08-12 20:30:58 +0000137 SkPaint paint(fPaint);
138 this->setupPaint(&paint);
139
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000140 for (int i = 0; i < loops; i++) {
reed@android.com4d850592009-08-12 20:30:58 +0000141 canvas->drawPaint(paint);
142 }
143 }
144
145private:
146 typedef SkBenchmark INHERITED;
147};
148
mtklein@google.com410e6e82013-09-13 19:52:27 +0000149DEF_BENCH(return new RepeatTileBench(SkBitmap::kARGB_8888_Config, true))
150DEF_BENCH(return new RepeatTileBench(SkBitmap::kARGB_8888_Config, false))
151DEF_BENCH(return new RepeatTileBench(SkBitmap::kRGB_565_Config))
152DEF_BENCH(return new RepeatTileBench(SkBitmap::kIndex8_Config))