blob: 550603143d28a0f8229a25e770bf5a8d4e2c59ab [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
tfarinaf168b862014-06-19 12:32:29 -07007#include "Benchmark.h"
reed@android.com4d850592009-08-12 20:30:58 +00008#include "SkBitmap.h"
9#include "SkCanvas.h"
10#include "SkColorPriv.h"
11#include "SkPaint.h"
12#include "SkShader.h"
13#include "SkString.h"
reed6c225732014-06-09 19:52:07 -070014#include "sk_tool_utils.h"
reed@android.com4d850592009-08-12 20:30:58 +000015
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000016static void draw_into_bitmap(const SkBitmap& bm) {
reed@android.com4d850592009-08-12 20:30:58 +000017 const int w = bm.width();
18 const int h = bm.height();
19
20 SkCanvas canvas(bm);
21 SkPaint p;
22 p.setAntiAlias(true);
23 p.setColor(SK_ColorRED);
24 canvas.drawCircle(SkIntToScalar(w)/2, SkIntToScalar(h)/2,
25 SkIntToScalar(SkMin32(w, h))*3/8, p);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000026
reed@android.com4d850592009-08-12 20:30:58 +000027 SkRect r;
28 r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h));
29 p.setStyle(SkPaint::kStroke_Style);
30 p.setStrokeWidth(SkIntToScalar(4));
31 p.setColor(SK_ColorBLUE);
32 canvas.drawRect(r, p);
33}
34
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000035static int conv_6_to_byte(int x) {
reed@android.com4d850592009-08-12 20:30:58 +000036 return x * 0xFF / 5;
37}
38
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000039static int conv_byte_to_6(int x) {
reed@android.com4d850592009-08-12 20:30:58 +000040 return x * 5 / 255;
41}
42
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000043static uint8_t compute_666_index(SkPMColor c) {
reed@android.com4d850592009-08-12 20:30:58 +000044 int r = SkGetPackedR32(c);
45 int g = SkGetPackedG32(c);
46 int b = SkGetPackedB32(c);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000047
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000048 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 +000049}
50
reed6c225732014-06-09 19:52:07 -070051static void convert_to_index666(const SkBitmap& src, SkBitmap* dst) {
reed@google.com0a6151d2013-10-10 14:44:56 +000052 SkPMColor storage[216];
53 SkPMColor* colors = storage;
reed@android.com4d850592009-08-12 20:30:58 +000054 // rrr ggg bbb
55 for (int r = 0; r < 6; r++) {
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000056 int rr = conv_6_to_byte(r);
reed@android.com4d850592009-08-12 20:30:58 +000057 for (int g = 0; g < 6; g++) {
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000058 int gg = conv_6_to_byte(g);
reed@android.com4d850592009-08-12 20:30:58 +000059 for (int b = 0; b < 6; b++) {
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000060 int bb = conv_6_to_byte(b);
reed@android.com4d850592009-08-12 20:30:58 +000061 *colors++ = SkPreMultiplyARGB(0xFF, rr, gg, bb);
62 }
63 }
64 }
reed6c225732014-06-09 19:52:07 -070065 dst->allocPixels(SkImageInfo::Make(src.width(), src.height(),
66 kIndex_8_SkColorType, kOpaque_SkAlphaType),
Mike Reed6b3155c2017-04-03 14:41:44 -040067 SkColorTable::Make(storage, 216));
rmistry@google.comfbfcd562012-08-23 18:09:54 +000068
reed@android.com4d850592009-08-12 20:30:58 +000069 for (int y = 0; y < src.height(); y++) {
70 const SkPMColor* srcP = src.getAddr32(0, y);
71 uint8_t* dstP = dst->getAddr8(0, y);
72 for (int x = src.width() - 1; x >= 0; --x) {
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000073 *dstP++ = compute_666_index(*srcP++);
reed@android.com4d850592009-08-12 20:30:58 +000074 }
75 }
76}
77
tfarinaf168b862014-06-19 12:32:29 -070078class RepeatTileBench : public Benchmark {
reed6c225732014-06-09 19:52:07 -070079 const SkColorType fColorType;
80 const SkAlphaType fAlphaType;
81 SkPaint fPaint;
82 SkString fName;
83 SkBitmap fBitmap;
reed@android.com4d850592009-08-12 20:30:58 +000084public:
reed6c225732014-06-09 19:52:07 -070085 RepeatTileBench(SkColorType ct, SkAlphaType at = kPremul_SkAlphaType)
86 : fColorType(ct), fAlphaType(at)
87 {
reed@android.com4d850592009-08-12 20:30:58 +000088 const int w = 50;
89 const int h = 50;
reed@android.com4d850592009-08-12 20:30:58 +000090
reed6c225732014-06-09 19:52:07 -070091 if (kIndex_8_SkColorType == ct) {
92 fBitmap.setInfo(SkImageInfo::MakeN32(w, h, at));
reed@android.com4d850592009-08-12 20:30:58 +000093 } else {
reed6c225732014-06-09 19:52:07 -070094 fBitmap.setInfo(SkImageInfo::Make(w, h, ct, at));
reed@android.com4d850592009-08-12 20:30:58 +000095 }
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +000096 fName.printf("repeatTile_%s_%c",
reed6c225732014-06-09 19:52:07 -070097 sk_tool_utils::colortype_name(ct), kOpaque_SkAlphaType == at ? 'X' : 'A');
reed@android.com4d850592009-08-12 20:30:58 +000098 }
99
100protected:
mtklein36352bf2015-03-25 18:17:31 -0700101 const char* onGetName() override {
reed@android.com4d850592009-08-12 20:30:58 +0000102 return fName.c_str();
103 }
104
joshualitt8a6697a2015-09-30 12:11:07 -0700105 void onDelayedSetup() override {
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +0000106 fBitmap.allocPixels();
reed6c225732014-06-09 19:52:07 -0700107 fBitmap.eraseColor(kOpaque_SkAlphaType == fAlphaType ? SK_ColorWHITE : 0);
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +0000108
109 draw_into_bitmap(fBitmap);
110
reed6c225732014-06-09 19:52:07 -0700111 if (kIndex_8_SkColorType == fColorType) {
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +0000112 SkBitmap tmp;
reed6c225732014-06-09 19:52:07 -0700113 convert_to_index666(fBitmap, &tmp);
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +0000114 fBitmap = tmp;
115 }
116
reedc6f28f72016-03-14 12:22:10 -0700117 fPaint.setShader(SkShader::MakeBitmapShader(fBitmap,
118 SkShader::kRepeat_TileMode,
119 SkShader::kRepeat_TileMode));
commit-bot@chromium.org7fb83c82013-08-01 15:58:07 +0000120 }
121
122
mtkleina1ebeb22015-10-01 09:43:39 -0700123 void onDraw(int loops, SkCanvas* canvas) override {
reed@android.com4d850592009-08-12 20:30:58 +0000124 SkPaint paint(fPaint);
125 this->setupPaint(&paint);
126
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000127 for (int i = 0; i < loops; i++) {
reed@android.com4d850592009-08-12 20:30:58 +0000128 canvas->drawPaint(paint);
129 }
130 }
131
132private:
tfarinaf168b862014-06-19 12:32:29 -0700133 typedef Benchmark INHERITED;
reed@android.com4d850592009-08-12 20:30:58 +0000134};
135
reed6c225732014-06-09 19:52:07 -0700136DEF_BENCH(return new RepeatTileBench(kN32_SkColorType, kOpaque_SkAlphaType))
137DEF_BENCH(return new RepeatTileBench(kN32_SkColorType, kPremul_SkAlphaType))
138DEF_BENCH(return new RepeatTileBench(kRGB_565_SkColorType, kOpaque_SkAlphaType))
139DEF_BENCH(return new RepeatTileBench(kIndex_8_SkColorType, kPremul_SkAlphaType))