1 /*
2  * Copyright 2013 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "Benchmark.h"
9 #include "SkCanvas.h"
10 #include "SkString.h"
11 #include "sk_tool_utils.h"
12 
13 class PremulAndUnpremulAlphaOpsBench : public Benchmark {
14     enum {
15         W = 256,
16         H = 256,
17     };
18     SkBitmap fBmp1, fBmp2;
19 
20 public:
PremulAndUnpremulAlphaOpsBench(SkColorType ct)21     PremulAndUnpremulAlphaOpsBench(SkColorType ct) {
22         fColorType = ct;
23         fName.printf("premul_and_unpremul_alpha_%s", sk_tool_utils::colortype_name(ct));
24     }
25 
26 protected:
onGetName()27     const char* onGetName() override {
28         return fName.c_str();
29     }
30 
onDelayedSetup()31     void onDelayedSetup() override {
32         SkImageInfo info = SkImageInfo::Make(W, H, fColorType, kUnpremul_SkAlphaType);
33         fBmp1.allocPixels(info);   // used in writePixels
34 
35         for (int h = 0; h < H; ++h) {
36             for (int w = 0; w < W; ++w) {
37                 // SkColor places A in the right slot for either RGBA or BGRA
38                 *fBmp1.getAddr32(w, h) = SkColorSetARGB(h & 0xFF, w & 0xFF, w & 0xFF, w & 0xFF);
39             }
40         }
41 
42         fBmp2.allocPixels(info);    // used in readPixels()
43     }
44 
onDraw(int loops,SkCanvas * canvas)45     void onDraw(int loops, SkCanvas* canvas) override {
46         canvas->clear(SK_ColorBLACK);
47 
48         for (int loop = 0; loop < loops; ++loop) {
49             // Unpremul -> Premul
50             canvas->writePixels(fBmp1.info(), fBmp1.getPixels(), fBmp1.rowBytes(), 0, 0);
51             // Premul -> Unpremul
52             canvas->readPixels(fBmp2.info(), fBmp2.getPixels(), fBmp2.rowBytes(), 0, 0);
53         }
54     }
55 
56 private:
57     SkColorType fColorType;
58     SkString fName;
59 
60     typedef Benchmark INHERITED;
61 };
62 
63 
64 DEF_BENCH(return new PremulAndUnpremulAlphaOpsBench(kRGBA_8888_SkColorType));
65 DEF_BENCH(return new PremulAndUnpremulAlphaOpsBench(kBGRA_8888_SkColorType));
66