epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 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 | */ |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 8 | #include "Benchmark.h" |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 9 | #include "SkBitmap.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkColorPriv.h" |
| 12 | #include "SkPaint.h" |
| 13 | #include "SkShader.h" |
| 14 | #include "SkString.h" |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 15 | #include "sk_tool_utils.h" |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 16 | |
commit-bot@chromium.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 17 | static void draw_into_bitmap(const SkBitmap& bm) { |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 18 | 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.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 27 | |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 28 | 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.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 36 | static int conv_6_to_byte(int x) { |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 37 | return x * 0xFF / 5; |
| 38 | } |
| 39 | |
commit-bot@chromium.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 40 | static int conv_byte_to_6(int x) { |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 41 | return x * 5 / 255; |
| 42 | } |
| 43 | |
commit-bot@chromium.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 44 | static uint8_t compute_666_index(SkPMColor c) { |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 45 | int r = SkGetPackedR32(c); |
| 46 | int g = SkGetPackedG32(c); |
| 47 | int b = SkGetPackedB32(c); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 48 | |
commit-bot@chromium.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 49 | return conv_byte_to_6(r) * 36 + conv_byte_to_6(g) * 6 + conv_byte_to_6(b); |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 50 | } |
| 51 | |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 52 | static void convert_to_index666(const SkBitmap& src, SkBitmap* dst) { |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 53 | SkPMColor storage[216]; |
| 54 | SkPMColor* colors = storage; |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 55 | // rrr ggg bbb |
| 56 | for (int r = 0; r < 6; r++) { |
commit-bot@chromium.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 57 | int rr = conv_6_to_byte(r); |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 58 | for (int g = 0; g < 6; g++) { |
commit-bot@chromium.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 59 | int gg = conv_6_to_byte(g); |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 60 | for (int b = 0; b < 6; b++) { |
commit-bot@chromium.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 61 | int bb = conv_6_to_byte(b); |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 62 | *colors++ = SkPreMultiplyARGB(0xFF, rr, gg, bb); |
| 63 | } |
| 64 | } |
| 65 | } |
reed | c5e15a1 | 2014-09-29 12:10:27 -0700 | [diff] [blame] | 66 | SkColorTable* ctable = new SkColorTable(storage, 216); |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 67 | dst->allocPixels(SkImageInfo::Make(src.width(), src.height(), |
| 68 | kIndex_8_SkColorType, kOpaque_SkAlphaType), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 69 | nullptr, ctable); |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 70 | ctable->unref(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 71 | |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 72 | 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.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 79 | *dstP++ = compute_666_index(*srcP++); |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 84 | class RepeatTileBench : public Benchmark { |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 85 | const SkColorType fColorType; |
| 86 | const SkAlphaType fAlphaType; |
| 87 | SkPaint fPaint; |
| 88 | SkString fName; |
| 89 | SkBitmap fBitmap; |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 90 | public: |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 91 | RepeatTileBench(SkColorType ct, SkAlphaType at = kPremul_SkAlphaType) |
| 92 | : fColorType(ct), fAlphaType(at) |
| 93 | { |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 94 | const int w = 50; |
| 95 | const int h = 50; |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 96 | |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 97 | if (kIndex_8_SkColorType == ct) { |
| 98 | fBitmap.setInfo(SkImageInfo::MakeN32(w, h, at)); |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 99 | } else { |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 100 | fBitmap.setInfo(SkImageInfo::Make(w, h, ct, at)); |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 101 | } |
commit-bot@chromium.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 102 | fName.printf("repeatTile_%s_%c", |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 103 | sk_tool_utils::colortype_name(ct), kOpaque_SkAlphaType == at ? 'X' : 'A'); |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 107 | const char* onGetName() override { |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 108 | return fName.c_str(); |
| 109 | } |
| 110 | |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 111 | void onDelayedSetup() override { |
commit-bot@chromium.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 112 | fBitmap.allocPixels(); |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 113 | fBitmap.eraseColor(kOpaque_SkAlphaType == fAlphaType ? SK_ColorWHITE : 0); |
commit-bot@chromium.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 114 | |
| 115 | draw_into_bitmap(fBitmap); |
| 116 | |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 117 | if (kIndex_8_SkColorType == fColorType) { |
commit-bot@chromium.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 118 | SkBitmap tmp; |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 119 | convert_to_index666(fBitmap, &tmp); |
commit-bot@chromium.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 120 | 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 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 130 | void onDraw(int loops, SkCanvas* canvas) override { |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 131 | SkPaint paint(fPaint); |
| 132 | this->setupPaint(&paint); |
| 133 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 134 | for (int i = 0; i < loops; i++) { |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 135 | canvas->drawPaint(paint); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 140 | typedef Benchmark INHERITED; |
reed@android.com | 4d85059 | 2009-08-12 20:30:58 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 143 | DEF_BENCH(return new RepeatTileBench(kN32_SkColorType, kOpaque_SkAlphaType)) |
| 144 | DEF_BENCH(return new RepeatTileBench(kN32_SkColorType, kPremul_SkAlphaType)) |
| 145 | DEF_BENCH(return new RepeatTileBench(kRGB_565_SkColorType, kOpaque_SkAlphaType)) |
| 146 | DEF_BENCH(return new RepeatTileBench(kIndex_8_SkColorType, kPremul_SkAlphaType)) |