blob: b67098c5967fd6ccc218a3ce6c55a084e219f51b [file] [log] [blame]
commit-bot@chromium.org5f879752013-06-13 10:18:02 +00001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "bench/Benchmark.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkString.h"
11#include "tools/ToolUtils.h"
commit-bot@chromium.org5f879752013-06-13 10:18:02 +000012
tfarinaf168b862014-06-19 12:32:29 -070013class PremulAndUnpremulAlphaOpsBench : public Benchmark {
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000014 enum {
15 W = 256,
16 H = 256,
17 };
18 SkBitmap fBmp1, fBmp2;
skia.committer@gmail.comdb0c8752014-03-18 03:02:11 +000019
commit-bot@chromium.org5f879752013-06-13 10:18:02 +000020public:
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000021 PremulAndUnpremulAlphaOpsBench(SkColorType ct) {
22 fColorType = ct;
Mike Kleinea3f0142019-03-20 11:12:10 -050023 fName.printf("premul_and_unpremul_alpha_%s", ToolUtils::colortype_name(ct));
commit-bot@chromium.org5f879752013-06-13 10:18:02 +000024 }
25
26protected:
mtklein36352bf2015-03-25 18:17:31 -070027 const char* onGetName() override {
commit-bot@chromium.org5f879752013-06-13 10:18:02 +000028 return fName.c_str();
29 }
30
joshualitt8a6697a2015-09-30 12:11:07 -070031 void onDelayedSetup() override {
scroggofa047762014-07-11 10:45:11 -070032 SkImageInfo info = SkImageInfo::Make(W, H, fColorType, kUnpremul_SkAlphaType);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000033 fBmp1.allocPixels(info); // used in writePixels
34
35 for (int h = 0; h < H; ++h) {
36 for (int w = 0; w < W; ++w) {
37 // SkColor places A in the right slot for either RGBA or BGRA
38 *fBmp1.getAddr32(w, h) = SkColorSetARGB(h & 0xFF, w & 0xFF, w & 0xFF, w & 0xFF);
39 }
40 }
skia.committer@gmail.comdb0c8752014-03-18 03:02:11 +000041
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000042 fBmp2.allocPixels(info); // used in readPixels()
43 }
44
mtkleina1ebeb22015-10-01 09:43:39 -070045 void onDraw(int loops, SkCanvas* canvas) override {
commit-bot@chromium.org5f879752013-06-13 10:18:02 +000046 canvas->clear(SK_ColorBLACK);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000047
commit-bot@chromium.org33614712013-12-03 18:17:16 +000048 for (int loop = 0; loop < loops; ++loop) {
commit-bot@chromium.org5f879752013-06-13 10:18:02 +000049 // Unpremul -> Premul
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000050 canvas->writePixels(fBmp1.info(), fBmp1.getPixels(), fBmp1.rowBytes(), 0, 0);
commit-bot@chromium.org5f879752013-06-13 10:18:02 +000051 // Premul -> Unpremul
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000052 canvas->readPixels(fBmp2.info(), fBmp2.getPixels(), fBmp2.rowBytes(), 0, 0);
commit-bot@chromium.org5f879752013-06-13 10:18:02 +000053 }
54 }
55
56private:
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000057 SkColorType fColorType;
commit-bot@chromium.org5f879752013-06-13 10:18:02 +000058 SkString fName;
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000059
tfarinaf168b862014-06-19 12:32:29 -070060 typedef Benchmark INHERITED;
commit-bot@chromium.org5f879752013-06-13 10:18:02 +000061};
62
commit-bot@chromium.org5f879752013-06-13 10:18:02 +000063
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000064DEF_BENCH(return new PremulAndUnpremulAlphaOpsBench(kRGBA_8888_SkColorType));
65DEF_BENCH(return new PremulAndUnpremulAlphaOpsBench(kBGRA_8888_SkColorType));