blob: aac5934b4ce4a7dcdfd6a7bd0a395cf407be7a68 [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:
mtklein@google.com410e6e82013-09-13 19:52:27 +000016 PremulAndUnpremulAlphaOpsBench(SkCanvas::Config8888 config) {
commit-bot@chromium.org5f879752013-06-13 10:18:02 +000017 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
mtklein@google.comc2897432013-09-10 19:23:38 +000048 for (int loop = 0; loop < this->getLoops(); ++loop) {
commit-bot@chromium.org5f879752013-06-13 10:18:02 +000049 // 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
commit-bot@chromium.org5f879752013-06-13 10:18:02 +000062
mtklein@google.com410e6e82013-09-13 19:52:27 +000063DEF_BENCH(return new PremulAndUnpremulAlphaOpsBench(SkCanvas::kRGBA_Unpremul_Config8888));
64DEF_BENCH(return new PremulAndUnpremulAlphaOpsBench(SkCanvas::kNative_Unpremul_Config8888));