PremulAndUnpremulAlphaOpsBench.cpp revision 0a657bbc2c6fc9daf699942e023050536d5ec95f
1
2/*
3 * Copyright 2013 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "SkBenchmark.h"
10#include "SkCanvas.h"
11#include "SkConfig8888.h"
12#include "SkString.h"
13
14class PremulAndUnpremulAlphaOpsBench : public SkBenchmark {
15public:
16    PremulAndUnpremulAlphaOpsBench(SkCanvas::Config8888 config) {
17        fUnPremulConfig = config;
18        fName.printf("premul_and_unpremul_alpha_%s",
19                     (config ==  SkCanvas::kRGBA_Unpremul_Config8888) ?
20                     "RGBA8888" : "Native8888");
21    }
22
23protected:
24    virtual const char* onGetName() SK_OVERRIDE {
25        return fName.c_str();
26    }
27
28    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
29        canvas->clear(SK_ColorBLACK);
30        SkISize size = canvas->getDeviceSize();
31
32        SkBitmap bmp1;
33        bmp1.setConfig(SkBitmap::kARGB_8888_Config, size.width(),
34                       size.height());
35        bmp1.allocPixels();
36        SkAutoLockPixels alp(bmp1);
37        uint32_t* pixels = reinterpret_cast<uint32_t*>(bmp1.getPixels());
38        for (int h = 0; h < size.height(); ++h) {
39            for (int w = 0; w < size.width(); ++w)
40                pixels[h * size.width() + w] = SkPackConfig8888(fUnPremulConfig,
41                    h & 0xFF, w & 0xFF, w & 0xFF, w & 0xFF);
42        }
43
44        SkBitmap bmp2;
45        bmp2.setConfig(SkBitmap::kARGB_8888_Config, size.width(),
46                       size.height());
47
48        for (int loop = 0; loop < this->getLoops(); ++loop) {
49            // Unpremul -> Premul
50            canvas->writePixels(bmp1, 0, 0, fUnPremulConfig);
51            // Premul -> Unpremul
52            canvas->readPixels(&bmp2, 0, 0, fUnPremulConfig);
53        }
54    }
55
56private:
57    SkCanvas::Config8888 fUnPremulConfig;
58    SkString fName;
59    typedef SkBenchmark INHERITED;
60};
61
62
63DEF_BENCH(return new PremulAndUnpremulAlphaOpsBench(SkCanvas::kRGBA_Unpremul_Config8888));
64DEF_BENCH(return new PremulAndUnpremulAlphaOpsBench(SkCanvas::kNative_Unpremul_Config8888));
65