aboutsummaryrefslogtreecommitdiffhomepage
path: root/bench/PremulAndUnpremulAlphaOpsBench.cpp
blob: eb494f46d966212327927ceea33d0ea02e7f5c57 (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
67
68
69
70
71
72
73

/*
 * 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"

class PremulAndUnpremulAlphaOpsBench : public SkBenchmark {
public:
    PremulAndUnpremulAlphaOpsBench(void* param, SkCanvas::Config8888 config)
        : INHERITED(param) {
        fUnPremulConfig = config;
        fName.printf("premul_and_unpremul_alpha_%s",
                     (config ==  SkCanvas::kRGBA_Unpremul_Config8888) ?
                     "RGBA8888" : "Native8888");
    }

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

    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
        canvas->clear(SK_ColorBLACK);
        SkISize size = canvas->getDeviceSize();

        SkBitmap bmp1;
        bmp1.setConfig(SkBitmap::kARGB_8888_Config, size.width(),
                       size.height());
        bmp1.allocPixels();
        SkAutoLockPixels alp(bmp1);
        uint32_t* pixels = reinterpret_cast<uint32_t*>(bmp1.getPixels());
        for (int h = 0; h < size.height(); ++h) {
            for (int w = 0; w < size.width(); ++w)
                pixels[h * size.width() + w] = SkPackConfig8888(fUnPremulConfig,
                    h & 0xFF, w & 0xFF, w & 0xFF, w & 0xFF);
        }

        SkBitmap bmp2;
        bmp2.setConfig(SkBitmap::kARGB_8888_Config, size.width(),
                       size.height());

        for (int loop = 0; loop < this->getLoops(); ++loop) {
            // Unpremul -> Premul
            canvas->writePixels(bmp1, 0, 0, fUnPremulConfig);
            // Premul -> Unpremul
            canvas->readPixels(&bmp2, 0, 0, fUnPremulConfig);
        }
    }

private:
    SkCanvas::Config8888 fUnPremulConfig;
    SkString fName;
    typedef SkBenchmark INHERITED;
};

static SkBenchmark* fact0(void* p) {
    return new PremulAndUnpremulAlphaOpsBench(p,
        SkCanvas::kRGBA_Unpremul_Config8888);
}
static SkBenchmark* fact1(void* p) {
    return new PremulAndUnpremulAlphaOpsBench(p,
        SkCanvas::kNative_Unpremul_Config8888);
}

static BenchRegistry gReg0(fact0);
static BenchRegistry gReg1(fact1);