aboutsummaryrefslogtreecommitdiffhomepage
path: root/bench/PremulAndUnpremulAlphaOpsBench.cpp
blob: e937e14f1749f1daf733e05b68b7ec474c21a3a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
 * Copyright 2013 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkBenchmark.h"
#include "SkCanvas.h"
#include "SkConfig8888.h"
#include "SkString.h"
#include "sk_tool_utils.h"

class PremulAndUnpremulAlphaOpsBench : public SkBenchmark {
    enum {
        W = 256,
        H = 256,
    };
    SkBitmap fBmp1, fBmp2;

public:
    PremulAndUnpremulAlphaOpsBench(SkColorType ct) {
        fColorType = ct;
        fName.printf("premul_and_unpremul_alpha_%s", sk_tool_utils::colortype_name(ct));
    }

protected:
    virtual const char* onGetName() SK_OVERRIDE {
        return fName.c_str();
    }

    virtual void onPreDraw() {
        SkImageInfo info = SkImageInfo::Make(W, H, fColorType, kPremul_SkAlphaType);
        fBmp1.allocPixels(info);   // used in writePixels

        for (int h = 0; h < H; ++h) {
            for (int w = 0; w < W; ++w) {
                // SkColor places A in the right slot for either RGBA or BGRA
                *fBmp1.getAddr32(w, h) = SkColorSetARGB(h & 0xFF, w & 0xFF, w & 0xFF, w & 0xFF);
            }
        }

        fBmp2.allocPixels(info);    // used in readPixels()
    }

    virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
        canvas->clear(SK_ColorBLACK);

        for (int loop = 0; loop < loops; ++loop) {
            // Unpremul -> Premul
            canvas->writePixels(fBmp1.info(), fBmp1.getPixels(), fBmp1.rowBytes(), 0, 0);
            // Premul -> Unpremul
            canvas->readPixels(fBmp2.info(), fBmp2.getPixels(), fBmp2.rowBytes(), 0, 0);
        }
    }

private:
    SkColorType fColorType;
    SkString fName;

    typedef SkBenchmark INHERITED;
};


DEF_BENCH(return new PremulAndUnpremulAlphaOpsBench(kRGBA_8888_SkColorType));
DEF_BENCH(return new PremulAndUnpremulAlphaOpsBench(kBGRA_8888_SkColorType));