2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "bench/Benchmark.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkString.h"
12 #include "tools/ToolUtils.h"
14 class PremulAndUnpremulAlphaOpsBench : public Benchmark {
19 SkBitmap fBmp1, fBmp2;
22 PremulAndUnpremulAlphaOpsBench(SkColorType ct) {
24 fName.printf("premul_and_unpremul_alpha_%s", ToolUtils::colortype_name(ct));
28 const char* onGetName() override {
32 void onDelayedSetup() override {
33 SkImageInfo info = SkImageInfo::Make(W, H, fColorType, kUnpremul_SkAlphaType);
34 fBmp1.allocPixels(info); // used in writePixels
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);
43 fBmp2.allocPixels(info); // used in readPixels()
46 void onDraw(int loops, SkCanvas* canvas) override {
47 canvas->clear(SK_ColorBLACK);
49 for (int loop = 0; loop < loops; ++loop) {
51 canvas->writePixels(fBmp1.info(), fBmp1.getPixels(), fBmp1.rowBytes(), 0, 0);
53 canvas->readPixels(fBmp2.info(), fBmp2.getPixels(), fBmp2.rowBytes(), 0, 0);
58 SkColorType fColorType;
61 using INHERITED = Benchmark;
65 DEF_BENCH(return new PremulAndUnpremulAlphaOpsBench(kRGBA_8888_SkColorType));
66 DEF_BENCH(return new PremulAndUnpremulAlphaOpsBench(kBGRA_8888_SkColorType));