1 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 "Benchmark.h" 10 #include "SkCanvas.h" 11 #include "SkPaint.h" 12 #include "SkRandom.h" 13 #include "SkString.h" 14 #include "SkXfermode.h" 15 16 // Benchmark that draws non-AA rects with an SkXfermode::Mode 17 class XfermodeBench : public Benchmark { 18 public: XfermodeBench(SkXfermode::Mode mode)19 XfermodeBench(SkXfermode::Mode mode) { 20 fXfermode.reset(SkXfermode::Create(mode)); 21 SkASSERT(fXfermode.get() || SkXfermode::kSrcOver_Mode == mode); 22 fName.printf("Xfermode_%s", SkXfermode::ModeName(mode)); 23 } 24 XfermodeBench(SkXfermode * xferMode,const char * name)25 XfermodeBench(SkXfermode* xferMode, const char* name) { 26 SkASSERT(xferMode); 27 fXfermode.reset(xferMode); 28 fName.printf("Xfermode_%s", name); 29 } 30 31 protected: onGetName()32 virtual const char* onGetName() SK_OVERRIDE { return fName.c_str(); } 33 onDraw(const int loops,SkCanvas * canvas)34 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { 35 SkISize size = canvas->getDeviceSize(); 36 SkRandom random; 37 for (int i = 0; i < loops; ++i) { 38 SkPaint paint; 39 paint.setXfermode(fXfermode.get()); 40 paint.setColor(random.nextU()); 41 SkScalar w = random.nextRangeScalar(SkIntToScalar(kMinSize), SkIntToScalar(kMaxSize)); 42 SkScalar h = random.nextRangeScalar(SkIntToScalar(kMinSize), SkIntToScalar(kMaxSize)); 43 SkRect rect = SkRect::MakeXYWH( 44 random.nextUScalar1() * (size.fWidth - w), 45 random.nextUScalar1() * (size.fHeight - h), 46 w, 47 h 48 ); 49 canvas->drawRect(rect, paint); 50 } 51 } 52 53 private: 54 enum { 55 kMinSize = 50, 56 kMaxSize = 100, 57 }; 58 SkAutoTUnref<SkXfermode> fXfermode; 59 SkString fName; 60 61 typedef Benchmark INHERITED; 62 }; 63 64 class XferCreateBench : public Benchmark { 65 public: isSuitableFor(Backend backend)66 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { 67 return backend == kNonRendering_Backend; 68 } 69 70 protected: onGetName()71 virtual const char* onGetName() SK_OVERRIDE { return "xfermode_create"; } 72 onDraw(const int loops,SkCanvas * canvas)73 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { 74 for (int outer = 0; outer < loops * 10; ++outer) { 75 for (int i = 0; i <= SkXfermode::kLastMode; ++i) { 76 SkXfermode* xfer = SkXfermode::Create(SkXfermode::Mode(i)); 77 SkSafeUnref(xfer); 78 } 79 } 80 } 81 82 private: 83 typedef Benchmark INHERITED; 84 }; 85 86 ////////////////////////////////////////////////////////////////////////////// 87 88 #define CONCAT_I(x, y) x ## y 89 #define CONCAT(x, y) CONCAT_I(x, y) // allow for macro expansion 90 #define BENCH(...) \ 91 DEF_BENCH( return new XfermodeBench(__VA_ARGS__); );\ 92 93 94 BENCH(SkXfermode::kClear_Mode) 95 BENCH(SkXfermode::kSrc_Mode) 96 BENCH(SkXfermode::kDst_Mode) 97 BENCH(SkXfermode::kSrcOver_Mode) 98 BENCH(SkXfermode::kDstOver_Mode) 99 BENCH(SkXfermode::kSrcIn_Mode) 100 BENCH(SkXfermode::kDstIn_Mode) 101 BENCH(SkXfermode::kSrcOut_Mode) 102 BENCH(SkXfermode::kDstOut_Mode) 103 BENCH(SkXfermode::kSrcATop_Mode) 104 BENCH(SkXfermode::kDstATop_Mode) 105 BENCH(SkXfermode::kXor_Mode) 106 107 BENCH(SkXfermode::kPlus_Mode) 108 BENCH(SkXfermode::kModulate_Mode) 109 BENCH(SkXfermode::kScreen_Mode) 110 111 BENCH(SkXfermode::kOverlay_Mode) 112 BENCH(SkXfermode::kDarken_Mode) 113 BENCH(SkXfermode::kLighten_Mode) 114 BENCH(SkXfermode::kColorDodge_Mode) 115 BENCH(SkXfermode::kColorBurn_Mode) 116 BENCH(SkXfermode::kHardLight_Mode) 117 BENCH(SkXfermode::kSoftLight_Mode) 118 BENCH(SkXfermode::kDifference_Mode) 119 BENCH(SkXfermode::kExclusion_Mode) 120 BENCH(SkXfermode::kMultiply_Mode) 121 122 BENCH(SkXfermode::kHue_Mode) 123 BENCH(SkXfermode::kSaturation_Mode) 124 BENCH(SkXfermode::kColor_Mode) 125 BENCH(SkXfermode::kLuminosity_Mode) 126 127 DEF_BENCH(return new XferCreateBench;) 128