commit-bot@chromium.org | 5f87975 | 2013-06-13 10:18:02 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2013 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 | */ |
| 8 | |
| 9 | #include "SkBenchmark.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkConfig8888.h" |
| 12 | #include "SkString.h" |
| 13 | |
| 14 | class PremulAndUnpremulAlphaOpsBench : public SkBenchmark { |
| 15 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 16 | PremulAndUnpremulAlphaOpsBench(SkCanvas::Config8888 config) { |
commit-bot@chromium.org | 5f87975 | 2013-06-13 10:18:02 +0000 | [diff] [blame] | 17 | fUnPremulConfig = config; |
| 18 | fName.printf("premul_and_unpremul_alpha_%s", |
| 19 | (config == SkCanvas::kRGBA_Unpremul_Config8888) ? |
| 20 | "RGBA8888" : "Native8888"); |
| 21 | } |
| 22 | |
| 23 | protected: |
| 24 | virtual const char* onGetName() SK_OVERRIDE { |
| 25 | return fName.c_str(); |
| 26 | } |
| 27 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 28 | virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
commit-bot@chromium.org | 5f87975 | 2013-06-13 10:18:02 +0000 | [diff] [blame] | 29 | canvas->clear(SK_ColorBLACK); |
| 30 | SkISize size = canvas->getDeviceSize(); |
| 31 | |
| 32 | SkBitmap bmp1; |
| 33 | bmp1.setConfig(SkBitmap::kARGB_8888_Config, size.width(), |
| 34 | size.height()); |
| 35 | bmp1.allocPixels(); |
| 36 | SkAutoLockPixels alp(bmp1); |
| 37 | uint32_t* pixels = reinterpret_cast<uint32_t*>(bmp1.getPixels()); |
| 38 | for (int h = 0; h < size.height(); ++h) { |
| 39 | for (int w = 0; w < size.width(); ++w) |
| 40 | pixels[h * size.width() + w] = SkPackConfig8888(fUnPremulConfig, |
| 41 | h & 0xFF, w & 0xFF, w & 0xFF, w & 0xFF); |
| 42 | } |
| 43 | |
| 44 | SkBitmap bmp2; |
| 45 | bmp2.setConfig(SkBitmap::kARGB_8888_Config, size.width(), |
| 46 | size.height()); |
| 47 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 48 | for (int loop = 0; loop < loops; ++loop) { |
commit-bot@chromium.org | 5f87975 | 2013-06-13 10:18:02 +0000 | [diff] [blame] | 49 | // Unpremul -> Premul |
| 50 | canvas->writePixels(bmp1, 0, 0, fUnPremulConfig); |
| 51 | // Premul -> Unpremul |
| 52 | canvas->readPixels(&bmp2, 0, 0, fUnPremulConfig); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | private: |
| 57 | SkCanvas::Config8888 fUnPremulConfig; |
| 58 | SkString fName; |
| 59 | typedef SkBenchmark INHERITED; |
| 60 | }; |
| 61 | |
commit-bot@chromium.org | 5f87975 | 2013-06-13 10:18:02 +0000 | [diff] [blame] | 62 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 63 | DEF_BENCH(return new PremulAndUnpremulAlphaOpsBench(SkCanvas::kRGBA_Unpremul_Config8888)); |
| 64 | DEF_BENCH(return new PremulAndUnpremulAlphaOpsBench(SkCanvas::kNative_Unpremul_Config8888)); |