blob: eb494f46d966212327927ceea33d0ea02e7f5c57 [file] [log] [blame]
commit-bot@chromium.org5f879752013-06-13 10:18:02 +00001
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(void* param, SkCanvas::Config8888 config)
17 : INHERITED(param) {
18 fUnPremulConfig = config;
19 fName.printf("premul_and_unpremul_alpha_%s",
20 (config == SkCanvas::kRGBA_Unpremul_Config8888) ?
21 "RGBA8888" : "Native8888");
22 }
23
24protected:
25 virtual const char* onGetName() SK_OVERRIDE {
26 return fName.c_str();
27 }
28
29 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
30 canvas->clear(SK_ColorBLACK);
31 SkISize size = canvas->getDeviceSize();
32
33 SkBitmap bmp1;
34 bmp1.setConfig(SkBitmap::kARGB_8888_Config, size.width(),
35 size.height());
36 bmp1.allocPixels();
37 SkAutoLockPixels alp(bmp1);
38 uint32_t* pixels = reinterpret_cast<uint32_t*>(bmp1.getPixels());
39 for (int h = 0; h < size.height(); ++h) {
40 for (int w = 0; w < size.width(); ++w)
41 pixels[h * size.width() + w] = SkPackConfig8888(fUnPremulConfig,
42 h & 0xFF, w & 0xFF, w & 0xFF, w & 0xFF);
43 }
44
45 SkBitmap bmp2;
46 bmp2.setConfig(SkBitmap::kARGB_8888_Config, size.width(),
47 size.height());
48
mtklein@google.comc2897432013-09-10 19:23:38 +000049 for (int loop = 0; loop < this->getLoops(); ++loop) {
commit-bot@chromium.org5f879752013-06-13 10:18:02 +000050 // Unpremul -> Premul
51 canvas->writePixels(bmp1, 0, 0, fUnPremulConfig);
52 // Premul -> Unpremul
53 canvas->readPixels(&bmp2, 0, 0, fUnPremulConfig);
54 }
55 }
56
57private:
58 SkCanvas::Config8888 fUnPremulConfig;
59 SkString fName;
60 typedef SkBenchmark INHERITED;
61};
62
63static SkBenchmark* fact0(void* p) {
64 return new PremulAndUnpremulAlphaOpsBench(p,
65 SkCanvas::kRGBA_Unpremul_Config8888);
66}
67static SkBenchmark* fact1(void* p) {
68 return new PremulAndUnpremulAlphaOpsBench(p,
69 SkCanvas::kNative_Unpremul_Config8888);
70}
71
72static BenchRegistry gReg0(fact0);
73static BenchRegistry gReg1(fact1);