blob: 6e795417a60850f259021926fb75c605a77e9a1a [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
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
commit-bot@chromium.org5f879752013-06-13 10:18:02 +00009#include "SkCanvas.h"
10#include "SkConfig8888.h"
11#include "SkString.h"
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000012#include "sk_tool_utils.h"
commit-bot@chromium.org5f879752013-06-13 10:18:02 +000013
tfarinaf168b862014-06-19 12:32:29 -070014class PremulAndUnpremulAlphaOpsBench : public Benchmark {
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000015 enum {
16 W = 256,
17 H = 256,
18 };
19 SkBitmap fBmp1, fBmp2;
skia.committer@gmail.comdb0c8752014-03-18 03:02:11 +000020
commit-bot@chromium.org5f879752013-06-13 10:18:02 +000021public:
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000022 PremulAndUnpremulAlphaOpsBench(SkColorType ct) {
23 fColorType = ct;
24 fName.printf("premul_and_unpremul_alpha_%s", sk_tool_utils::colortype_name(ct));
commit-bot@chromium.org5f879752013-06-13 10:18:02 +000025 }
26
27protected:
mtklein36352bf2015-03-25 18:17:31 -070028 const char* onGetName() override {
commit-bot@chromium.org5f879752013-06-13 10:18:02 +000029 return fName.c_str();
30 }
31
mtklein36352bf2015-03-25 18:17:31 -070032 void onPreDraw() override {
scroggofa047762014-07-11 10:45:11 -070033 SkImageInfo info = SkImageInfo::Make(W, H, fColorType, kUnpremul_SkAlphaType);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000034 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.comdb0c8752014-03-18 03:02:11 +000042
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000043 fBmp2.allocPixels(info); // used in readPixels()
44 }
45
mtklein36352bf2015-03-25 18:17:31 -070046 void onDraw(const int loops, SkCanvas* canvas) override {
commit-bot@chromium.org5f879752013-06-13 10:18:02 +000047 canvas->clear(SK_ColorBLACK);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000048
commit-bot@chromium.org33614712013-12-03 18:17:16 +000049 for (int loop = 0; loop < loops; ++loop) {
commit-bot@chromium.org5f879752013-06-13 10:18:02 +000050 // Unpremul -> Premul
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000051 canvas->writePixels(fBmp1.info(), fBmp1.getPixels(), fBmp1.rowBytes(), 0, 0);
commit-bot@chromium.org5f879752013-06-13 10:18:02 +000052 // Premul -> Unpremul
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000053 canvas->readPixels(fBmp2.info(), fBmp2.getPixels(), fBmp2.rowBytes(), 0, 0);
commit-bot@chromium.org5f879752013-06-13 10:18:02 +000054 }
55 }
56
57private:
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000058 SkColorType fColorType;
commit-bot@chromium.org5f879752013-06-13 10:18:02 +000059 SkString fName;
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000060
tfarinaf168b862014-06-19 12:32:29 -070061 typedef Benchmark INHERITED;
commit-bot@chromium.org5f879752013-06-13 10:18:02 +000062};
63
commit-bot@chromium.org5f879752013-06-13 10:18:02 +000064
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +000065DEF_BENCH(return new PremulAndUnpremulAlphaOpsBench(kRGBA_8888_SkColorType));
66DEF_BENCH(return new PremulAndUnpremulAlphaOpsBench(kBGRA_8888_SkColorType));