commit-bot@chromium.org | 5f87975 | 2013-06-13 10:18:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "bench/Benchmark.h" |
Mike Reed | ac9f0c9 | 2020-12-23 10:11:33 -0500 | [diff] [blame] | 9 | #include "include/core/SkBitmap.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkString.h" |
| 12 | #include "tools/ToolUtils.h" |
commit-bot@chromium.org | 5f87975 | 2013-06-13 10:18:02 +0000 | [diff] [blame] | 13 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 14 | class PremulAndUnpremulAlphaOpsBench : public Benchmark { |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 15 | enum { |
| 16 | W = 256, |
| 17 | H = 256, |
| 18 | }; |
| 19 | SkBitmap fBmp1, fBmp2; |
skia.committer@gmail.com | db0c875 | 2014-03-18 03:02:11 +0000 | [diff] [blame] | 20 | |
commit-bot@chromium.org | 5f87975 | 2013-06-13 10:18:02 +0000 | [diff] [blame] | 21 | public: |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 22 | PremulAndUnpremulAlphaOpsBench(SkColorType ct) { |
| 23 | fColorType = ct; |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 24 | fName.printf("premul_and_unpremul_alpha_%s", ToolUtils::colortype_name(ct)); |
commit-bot@chromium.org | 5f87975 | 2013-06-13 10:18:02 +0000 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 28 | const char* onGetName() override { |
commit-bot@chromium.org | 5f87975 | 2013-06-13 10:18:02 +0000 | [diff] [blame] | 29 | return fName.c_str(); |
| 30 | } |
| 31 | |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 32 | void onDelayedSetup() override { |
scroggo | fa04776 | 2014-07-11 10:45:11 -0700 | [diff] [blame] | 33 | SkImageInfo info = SkImageInfo::Make(W, H, fColorType, kUnpremul_SkAlphaType); |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 34 | fBmp1.allocPixels(info); // used in writePixels |
| 35 | |
| 36 | for (int h = 0; h < H; ++h) { |
| 37 | for (int w = 0; w < W; ++w) { |
| 38 | // SkColor places A in the right slot for either RGBA or BGRA |
| 39 | *fBmp1.getAddr32(w, h) = SkColorSetARGB(h & 0xFF, w & 0xFF, w & 0xFF, w & 0xFF); |
| 40 | } |
| 41 | } |
skia.committer@gmail.com | db0c875 | 2014-03-18 03:02:11 +0000 | [diff] [blame] | 42 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 43 | fBmp2.allocPixels(info); // used in readPixels() |
| 44 | } |
| 45 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 46 | void onDraw(int loops, SkCanvas* canvas) override { |
commit-bot@chromium.org | 5f87975 | 2013-06-13 10:18:02 +0000 | [diff] [blame] | 47 | canvas->clear(SK_ColorBLACK); |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 48 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 49 | for (int loop = 0; loop < loops; ++loop) { |
commit-bot@chromium.org | 5f87975 | 2013-06-13 10:18:02 +0000 | [diff] [blame] | 50 | // Unpremul -> Premul |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 51 | canvas->writePixels(fBmp1.info(), fBmp1.getPixels(), fBmp1.rowBytes(), 0, 0); |
commit-bot@chromium.org | 5f87975 | 2013-06-13 10:18:02 +0000 | [diff] [blame] | 52 | // Premul -> Unpremul |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 53 | canvas->readPixels(fBmp2.info(), fBmp2.getPixels(), fBmp2.rowBytes(), 0, 0); |
commit-bot@chromium.org | 5f87975 | 2013-06-13 10:18:02 +0000 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
| 57 | private: |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 58 | SkColorType fColorType; |
commit-bot@chromium.org | 5f87975 | 2013-06-13 10:18:02 +0000 | [diff] [blame] | 59 | SkString fName; |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 60 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 61 | using INHERITED = Benchmark; |
commit-bot@chromium.org | 5f87975 | 2013-06-13 10:18:02 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
commit-bot@chromium.org | 5f87975 | 2013-06-13 10:18:02 +0000 | [diff] [blame] | 64 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 65 | DEF_BENCH(return new PremulAndUnpremulAlphaOpsBench(kRGBA_8888_SkColorType)); |
| 66 | DEF_BENCH(return new PremulAndUnpremulAlphaOpsBench(kBGRA_8888_SkColorType)); |