• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "SkBlendModePriv.h"
10 #include "SkCanvas.h"
11 #include "SkPaint.h"
12 #include "SkRandom.h"
13 #include "SkString.h"
14 
15 // Benchmark that draws non-AA rects or AA text with an SkXfermode::Mode.
16 class XfermodeBench : public Benchmark {
17 public:
XfermodeBench(SkBlendMode mode,bool aa)18     XfermodeBench(SkBlendMode mode, bool aa) : fBlendMode(mode) {
19         fAA = aa;
20         fName.printf("blendmode_%s_%s", aa ? "mask" : "rect", SkBlendMode_Name(mode));
21     }
22 
23 protected:
onGetName()24     const char* onGetName() override { return fName.c_str(); }
25 
onDraw(int loops,SkCanvas * canvas)26     void onDraw(int loops, SkCanvas* canvas) override {
27         const char* text = "Hamburgefons";
28         size_t len = strlen(text);
29         SkISize size = canvas->getBaseLayerSize();
30         SkRandom random;
31         for (int i = 0; i < loops; ++i) {
32             SkPaint paint;
33             paint.setBlendMode(fBlendMode);
34             paint.setColor(random.nextU());
35             if (fAA) {
36                 // Draw text to exercise AA code paths.
37                 paint.setAntiAlias(true);
38                 paint.setTextSize(random.nextRangeScalar(12, 96));
39                 SkScalar x = random.nextRangeScalar(0, (SkScalar)size.fWidth),
40                          y = random.nextRangeScalar(0, (SkScalar)size.fHeight);
41                 for (int j = 0; j < 1000; ++j) {
42                     canvas->drawText(text, len, x, y, paint);
43                 }
44             } else {
45                 // Draw rects to exercise non-AA code paths.
46                 SkScalar w = random.nextRangeScalar(50, 100);
47                 SkScalar h = random.nextRangeScalar(50, 100);
48                 SkRect rect = SkRect::MakeXYWH(
49                     random.nextUScalar1() * (size.fWidth - w),
50                     random.nextUScalar1() * (size.fHeight - h),
51                     w,
52                     h
53                 );
54                 for (int j = 0; j < 1000; ++j) {
55                     canvas->drawRect(rect, paint);
56                 }
57             }
58         }
59     }
60 
61 private:
62     SkBlendMode fBlendMode;
63     SkString    fName;
64     bool        fAA;
65 
66     typedef Benchmark INHERITED;
67 };
68 
69 //////////////////////////////////////////////////////////////////////////////
70 
71 #define BENCH(...)                                             \
72     DEF_BENCH( return new XfermodeBench(__VA_ARGS__, true); )  \
73     DEF_BENCH( return new XfermodeBench(__VA_ARGS__, false); )
74 
75 BENCH(SkBlendMode::kClear)
76 BENCH(SkBlendMode::kSrc)
77 BENCH(SkBlendMode::kDst)
78 BENCH(SkBlendMode::kSrcOver)
79 BENCH(SkBlendMode::kDstOver)
80 BENCH(SkBlendMode::kSrcIn)
81 BENCH(SkBlendMode::kDstIn)
82 BENCH(SkBlendMode::kSrcOut)
83 BENCH(SkBlendMode::kDstOut)
84 BENCH(SkBlendMode::kSrcATop)
85 BENCH(SkBlendMode::kDstATop)
86 BENCH(SkBlendMode::kXor)
87 
88 BENCH(SkBlendMode::kPlus)
89 BENCH(SkBlendMode::kModulate)
90 BENCH(SkBlendMode::kScreen)
91 
92 BENCH(SkBlendMode::kOverlay)
93 BENCH(SkBlendMode::kDarken)
94 BENCH(SkBlendMode::kLighten)
95 BENCH(SkBlendMode::kColorDodge)
96 BENCH(SkBlendMode::kColorBurn)
97 BENCH(SkBlendMode::kHardLight)
98 BENCH(SkBlendMode::kSoftLight)
99 BENCH(SkBlendMode::kDifference)
100 BENCH(SkBlendMode::kExclusion)
101 BENCH(SkBlendMode::kMultiply)
102 
103 BENCH(SkBlendMode::kHue)
104 BENCH(SkBlendMode::kSaturation)
105 BENCH(SkBlendMode::kColor)
106 BENCH(SkBlendMode::kLuminosity)
107