1/*
2 * Copyright 2016 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 "SkPM4f.h"
10#include "SkString.h"
11#include "SkXfermode.h"
12
13#define USE_AA      (1 << 31)   // merge with Xfermode::PMFlags w/o conflict
14
15#define INNER_LOOPS 1000
16
17// Benchmark that draws non-AA rects or AA text with an SkXfermode::Mode.
18class XferD32Bench : public Benchmark {
19public:
20    XferD32Bench(SkXfermode::Mode mode, const char name[], bool doN, uint32_t flags)
21        : fDoN(doN)
22        , fFlags(flags & ~USE_AA)
23    {
24        fXfer.reset(SkXfermode::Create(mode));
25
26        fProc1 = SkXfermode::GetD32Proc(fXfer, fFlags | SkXfermode::kSrcIsSingle_D32Flag);
27        fProcN = SkXfermode::GetD32Proc(fXfer, fFlags);
28        fName.printf("xfer4f_%s_%s_%c_%s_%s",
29                     name,
30                     (flags & USE_AA) ? "aa" : "bw",
31                     fDoN ? 'N' : '1',
32                     (flags & SkXfermode::kSrcIsOpaque_D32Flag) ? "opaque" : "alpha",
33                     (flags & SkXfermode::kDstIsSRGB_D32Flag) ? "srgb" : "linear");
34
35        for (int i = 0; i < N; ++i) {
36            fSrc[i] = {{ 1, 1, 1, 1 }};
37            fDst[i] = 0;
38            fAAStorage[i] = i * 255 / (N - 1);
39        }
40
41        if (flags & USE_AA) {
42            fAA = fAAStorage;
43        } else {
44            fAA = nullptr;
45        }
46    }
47
48protected:
49    bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; }
50
51    const char* onGetName() override { return fName.c_str(); }
52
53    void onDraw(int loops, SkCanvas*) override {
54        for (int i = 0; i < loops * INNER_LOOPS; ++i) {
55            if (fDoN) {
56                fProcN(fXfer, fDst, fSrc, N, fAA);
57            } else {
58                fProc1(fXfer, fDst, fSrc, N, fAA);
59            }
60        }
61    }
62
63private:
64    SkAutoTUnref<SkXfermode> fXfer;
65    SkString             fName;
66    SkXfermode::D32Proc  fProc1;
67    SkXfermode::D32Proc  fProcN;
68    const SkAlpha*       fAA;
69    bool                 fDoN;
70    uint32_t             fFlags;
71
72    enum {
73        N = 1000,
74    };
75    SkPM4f      fSrc[N];
76    SkPMColor   fDst[N];
77    uint8_t     fAAStorage[N];
78
79    typedef Benchmark INHERITED;
80};
81
82#define F00 0
83#define F01 (SkXfermode::kSrcIsOpaque_D32Flag)
84#define F10 (SkXfermode::kDstIsSRGB_D32Flag)
85#define F11 (SkXfermode::kSrcIsOpaque_D32Flag | SkXfermode::kDstIsSRGB_D32Flag)
86
87DEF_BENCH( return new XferD32Bench(SkXfermode::kSrcOver_Mode, "srcover", false, F10); )
88DEF_BENCH( return new XferD32Bench(SkXfermode::kSrcOver_Mode, "srcover", false, F00); )
89DEF_BENCH( return new XferD32Bench(SkXfermode::kSrcOver_Mode, "srcover", false, F11); )
90DEF_BENCH( return new XferD32Bench(SkXfermode::kSrcOver_Mode, "srcover", false, F01); )
91
92DEF_BENCH( return new XferD32Bench(SkXfermode::kSrcOver_Mode, "srcover", true,  F10); )
93DEF_BENCH( return new XferD32Bench(SkXfermode::kSrcOver_Mode, "srcover", true,  F00); )
94DEF_BENCH( return new XferD32Bench(SkXfermode::kSrcOver_Mode, "srcover", true,  F11); )
95DEF_BENCH( return new XferD32Bench(SkXfermode::kSrcOver_Mode, "srcover", true,  F01); )
96
97DEF_BENCH( return new XferD32Bench(SkXfermode::kSrcOver_Mode, "srcover", false, F10 | USE_AA); )
98DEF_BENCH( return new XferD32Bench(SkXfermode::kSrcOver_Mode, "srcover", false, F00 | USE_AA); )
99DEF_BENCH( return new XferD32Bench(SkXfermode::kSrcOver_Mode, "srcover", false, F11 | USE_AA); )
100DEF_BENCH( return new XferD32Bench(SkXfermode::kSrcOver_Mode, "srcover", false, F01 | USE_AA); )
101
102DEF_BENCH( return new XferD32Bench(SkXfermode::kSrcOver_Mode, "srcover", true,  F10 | USE_AA); )
103DEF_BENCH( return new XferD32Bench(SkXfermode::kSrcOver_Mode, "srcover", true,  F00 | USE_AA); )
104DEF_BENCH( return new XferD32Bench(SkXfermode::kSrcOver_Mode, "srcover", true,  F11 | USE_AA); )
105DEF_BENCH( return new XferD32Bench(SkXfermode::kSrcOver_Mode, "srcover", true,  F01 | USE_AA); )
106