blob: 39830bf90f8a5c290284f08d2e2bd90e6da311ce [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 */
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
reed@android.com4d850592009-08-12 20:30:58 +00009#include "SkBitmap.h"
10#include "SkCanvas.h"
11#include "SkColorPriv.h"
12#include "SkPaint.h"
13#include "SkShader.h"
14#include "SkString.h"
reed6c225732014-06-09 19:52:07 -070015#include "sk_tool_utils.h"
reed@android.com4d850592009-08-12 20:30:58 +000016
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000017static void draw_into_bitmap(const SkBitmap& bm) {
reed@android.com4d850592009-08-12 20:30:58 +000018 const int w = bm.width();
19 const int h = bm.height();
20
21 SkCanvas canvas(bm);
22 SkPaint p;
23 p.setAntiAlias(true);
24 p.setColor(SK_ColorRED);
25 canvas.drawCircle(SkIntToScalar(w)/2, SkIntToScalar(h)/2,
26 SkIntToScalar(SkMin32(w, h))*3/8, p);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000027
reed@android.com4d850592009-08-12 20:30:58 +000028 SkRect r;
29 r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h));
30 p.setStyle(SkPaint::kStroke_Style);
31 p.setStrokeWidth(SkIntToScalar(4));
32 p.setColor(SK_ColorBLUE);
33 canvas.drawRect(r, p);
34}
35
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000036static int conv_6_to_byte(int x) {
reed@android.com4d850592009-08-12 20:30:58 +000037 return x * 0xFF / 5;
38}
39
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000040static int conv_byte_to_6(int x) {
reed@android.com4d850592009-08-12 20:30:58 +000041 return x * 5 / 255;
42}
43
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000044static uint8_t compute_666_index(SkPMColor c) {
reed@android.com4d850592009-08-12 20:30:58 +000045 int r = SkGetPackedR32(c);
46 int g = SkGetPackedG32(c);
47 int b = SkGetPackedB32(c);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000048
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000049 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 +000050}
51
reed6c225732014-06-09 19:52:07 -070052static void convert_to_index666(const SkBitmap& src, SkBitmap* dst) {
reed@google.com0a6151d2013-10-10 14:44:56 +000053 SkPMColor storage[216];
54 SkPMColor* colors = storage;
reed@android.com4d850592009-08-12 20:30:58 +000055 // rrr ggg bbb
56 for (int r = 0; r < 6; r++) {
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000057 int rr = conv_6_to_byte(r);
reed@android.com4d850592009-08-12 20:30:58 +000058 for (int g = 0; g < 6; g++) {
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000059 int gg = conv_6_to_byte(g);
reed@android.com4d850592009-08-12 20:30:58 +000060 for (int b = 0; b < 6; b++) {
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000061 int bb = conv_6_to_byte(b);
reed@android.com4d850592009-08-12 20:30:58 +000062 *colors++ = SkPreMultiplyARGB(0xFF, rr, gg, bb);
63 }
64 }
65 }
reedc5e15a12014-09-29 12:10:27 -070066 SkColorTable* ctable = new SkColorTable(storage, 216);
reed6c225732014-06-09 19:52:07 -070067 dst->allocPixels(SkImageInfo::Make(src.width(), src.height(),
68 kIndex_8_SkColorType, kOpaque_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -070069 nullptr, ctable);
reed@android.com4d850592009-08-12 20:30:58 +000070 ctable->unref();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000071
reed@android.com4d850592009-08-12 20:30:58 +000072 SkAutoLockPixels alps(src);
73 SkAutoLockPixels alpd(*dst);
74
75 for (int y = 0; y < src.height(); y++) {
76 const SkPMColor* srcP = src.getAddr32(0, y);
77 uint8_t* dstP = dst->getAddr8(0, y);
78 for (int x = src.width() - 1; x >= 0; --x) {
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000079 *dstP++ = compute_666_index(*srcP++);
reed@android.com4d850592009-08-12 20:30:58 +000080 }
81 }
82}
83
tfarinaf168b862014-06-19 12:32:29 -070084class RepeatTileBench : public Benchmark {
reed6c225732014-06-09 19:52:07 -070085 const SkColorType fColorType;
86 const SkAlphaType fAlphaType;
87 SkPaint fPaint;
88 SkString fName;
89 SkBitmap fBitmap;
reed@android.com4d850592009-08-12 20:30:58 +000090public:
reed6c225732014-06-09 19:52:07 -070091 RepeatTileBench(SkColorType ct, SkAlphaType at = kPremul_SkAlphaType)
92 : fColorType(ct), fAlphaType(at)
93 {
reed@android.com4d850592009-08-12 20:30:58 +000094 const int w = 50;
95 const int h = 50;
reed@android.com4d850592009-08-12 20:30:58 +000096
reed6c225732014-06-09 19:52:07 -070097 if (kIndex_8_SkColorType == ct) {
98 fBitmap.setInfo(SkImageInfo::MakeN32(w, h, at));
reed@android.com4d850592009-08-12 20:30:58 +000099 } else {
reed6c225732014-06-09 19:52:07 -0700100 fBitmap.setInfo(SkImageInfo::Make(w, h, ct, at));
reed@android.com4d850592009-08-12 20:30:58 +0000101 }
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +0000102 fName.printf("repeatTile_%s_%c",
reed6c225732014-06-09 19:52:07 -0700103 sk_tool_utils::colortype_name(ct), kOpaque_SkAlphaType == at ? 'X' : 'A');
reed@android.com4d850592009-08-12 20:30:58 +0000104 }
105
106protected:
mtklein36352bf2015-03-25 18:17:31 -0700107 const char* onGetName() override {
reed@android.com4d850592009-08-12 20:30:58 +0000108 return fName.c_str();
109 }
110
joshualitt8a6697a2015-09-30 12:11:07 -0700111 void onDelayedSetup() override {
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +0000112 fBitmap.allocPixels();
reed6c225732014-06-09 19:52:07 -0700113 fBitmap.eraseColor(kOpaque_SkAlphaType == fAlphaType ? SK_ColorWHITE : 0);
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +0000114
115 draw_into_bitmap(fBitmap);
116
reed6c225732014-06-09 19:52:07 -0700117 if (kIndex_8_SkColorType == fColorType) {
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +0000118 SkBitmap tmp;
reed6c225732014-06-09 19:52:07 -0700119 convert_to_index666(fBitmap, &tmp);
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +0000120 fBitmap = tmp;
121 }
122
123 SkShader* s = SkShader::CreateBitmapShader(fBitmap,
124 SkShader::kRepeat_TileMode,
125 SkShader::kRepeat_TileMode);
126 fPaint.setShader(s)->unref();
127 }
128
129
mtkleina1ebeb22015-10-01 09:43:39 -0700130 void onDraw(int loops, SkCanvas* canvas) override {
reed@android.com4d850592009-08-12 20:30:58 +0000131 SkPaint paint(fPaint);
132 this->setupPaint(&paint);
133
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000134 for (int i = 0; i < loops; i++) {
reed@android.com4d850592009-08-12 20:30:58 +0000135 canvas->drawPaint(paint);
136 }
137 }
138
139private:
tfarinaf168b862014-06-19 12:32:29 -0700140 typedef Benchmark INHERITED;
reed@android.com4d850592009-08-12 20:30:58 +0000141};
142
reed6c225732014-06-09 19:52:07 -0700143DEF_BENCH(return new RepeatTileBench(kN32_SkColorType, kOpaque_SkAlphaType))
144DEF_BENCH(return new RepeatTileBench(kN32_SkColorType, kPremul_SkAlphaType))
145DEF_BENCH(return new RepeatTileBench(kRGB_565_SkColorType, kOpaque_SkAlphaType))
146DEF_BENCH(return new RepeatTileBench(kIndex_8_SkColorType, kPremul_SkAlphaType))