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 #include "SkBenchmark.h" 8 #include "SkBitmapDevice.h" 9 #include "SkBitmapSource.h" 10 #include "SkCanvas.h" 11 #include "SkDisplacementMapEffect.h" 12 13 #define FILTER_WIDTH_SMALL 32 14 #define FILTER_HEIGHT_SMALL 32 15 #define FILTER_WIDTH_LARGE 256 16 #define FILTER_HEIGHT_LARGE 256 17 18 class DisplacementBaseBench : public SkBenchmark { 19 public: DisplacementBaseBench(bool small)20 DisplacementBaseBench(bool small) : 21 fInitialized(false), fIsSmall(small) { 22 } 23 24 protected: onPreDraw()25 virtual void onPreDraw() SK_OVERRIDE { 26 if (!fInitialized) { 27 this->makeBitmap(); 28 this->makeCheckerboard(); 29 fInitialized = true; 30 } 31 } 32 makeBitmap()33 void makeBitmap() { 34 const int w = this->isSmall() ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE; 35 const int h = this->isSmall() ? FILTER_HEIGHT_LARGE : FILTER_HEIGHT_LARGE; 36 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, w, h); 37 fBitmap.allocPixels(); 38 SkBitmapDevice device(fBitmap); 39 SkCanvas canvas(&device); 40 canvas.clear(0x00000000); 41 SkPaint paint; 42 paint.setAntiAlias(true); 43 paint.setColor(0xFF884422); 44 paint.setTextSize(SkIntToScalar(96)); 45 const char* str = "g"; 46 canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(55), paint); 47 } 48 makeCheckerboard()49 void makeCheckerboard() { 50 const int w = this->isSmall() ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE; 51 const int h = this->isSmall() ? FILTER_HEIGHT_LARGE : FILTER_HEIGHT_LARGE; 52 fCheckerboard.setConfig(SkBitmap::kARGB_8888_Config, w, h); 53 fCheckerboard.allocPixels(); 54 SkBitmapDevice device(fCheckerboard); 55 SkCanvas canvas(&device); 56 canvas.clear(0x00000000); 57 SkPaint darkPaint; 58 darkPaint.setColor(0xFF804020); 59 SkPaint lightPaint; 60 lightPaint.setColor(0xFF244484); 61 for (int y = 0; y < h; y += 16) { 62 for (int x = 0; x < w; x += 16) { 63 canvas.save(); 64 canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); 65 canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint); 66 canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint); 67 canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint); 68 canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint); 69 canvas.restore(); 70 } 71 } 72 } 73 drawClippedBitmap(SkCanvas * canvas,int x,int y,const SkPaint & paint)74 void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) { 75 canvas->save(); 76 canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y), 77 SkIntToScalar(fBitmap.width()), 78 SkIntToScalar(fBitmap.height()))); 79 canvas->drawBitmap(fBitmap, SkIntToScalar(x), SkIntToScalar(y), &paint); 80 canvas->restore(); 81 } 82 isSmall() const83 inline bool isSmall() const { return fIsSmall; } 84 85 SkBitmap fBitmap, fCheckerboard; 86 private: 87 bool fInitialized; 88 bool fIsSmall; 89 typedef SkBenchmark INHERITED; 90 }; 91 92 class DisplacementZeroBench : public DisplacementBaseBench { 93 public: DisplacementZeroBench(bool small)94 DisplacementZeroBench(bool small) : INHERITED(small) { 95 } 96 97 protected: onGetName()98 virtual const char* onGetName() SK_OVERRIDE { 99 return this->isSmall() ? "displacement_zero_small" : "displacement_zero_large"; 100 } 101 onDraw(const int loops,SkCanvas * canvas)102 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { 103 SkPaint paint; 104 SkAutoTUnref<SkImageFilter> displ(SkNEW_ARGS(SkBitmapSource, (fCheckerboard))); 105 // No displacement effect 106 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, 107 (SkDisplacementMapEffect::kR_ChannelSelectorType, 108 SkDisplacementMapEffect::kG_ChannelSelectorType, 0.0f, displ)))->unref(); 109 110 for (int i = 0; i < loops; i++) { 111 this->drawClippedBitmap(canvas, 0, 0, paint); 112 } 113 } 114 115 private: 116 typedef DisplacementBaseBench INHERITED; 117 }; 118 119 class DisplacementAlphaBench : public DisplacementBaseBench { 120 public: DisplacementAlphaBench(bool small)121 DisplacementAlphaBench(bool small) : INHERITED(small) { 122 } 123 124 protected: onGetName()125 virtual const char* onGetName() SK_OVERRIDE { 126 return isSmall() ? "displacement_alpha_small" : "displacement_alpha_large"; 127 } 128 onDraw(const int loops,SkCanvas * canvas)129 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { 130 SkPaint paint; 131 SkAutoTUnref<SkImageFilter> displ(SkNEW_ARGS(SkBitmapSource, (fCheckerboard))); 132 // Displacement, with 1 alpha component (which isn't pre-multiplied) 133 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, 134 (SkDisplacementMapEffect::kB_ChannelSelectorType, 135 SkDisplacementMapEffect::kA_ChannelSelectorType, 16.0f, displ)))->unref(); 136 for (int i = 0; i < loops; i++) { 137 drawClippedBitmap(canvas, 100, 0, paint); 138 } 139 } 140 141 private: 142 typedef DisplacementBaseBench INHERITED; 143 }; 144 145 class DisplacementFullBench : public DisplacementBaseBench { 146 public: DisplacementFullBench(bool small)147 DisplacementFullBench(bool small) : INHERITED(small) { 148 } 149 150 protected: onGetName()151 virtual const char* onGetName() SK_OVERRIDE { 152 return isSmall() ? "displacement_full_small" : "displacement_full_large"; 153 } 154 onDraw(const int loops,SkCanvas * canvas)155 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { 156 SkPaint paint; 157 SkAutoTUnref<SkImageFilter> displ(SkNEW_ARGS(SkBitmapSource, (fCheckerboard))); 158 // Displacement, with 2 non-alpha components 159 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, 160 (SkDisplacementMapEffect::kR_ChannelSelectorType, 161 SkDisplacementMapEffect::kB_ChannelSelectorType, 32.0f, displ)))->unref(); 162 for (int i = 0; i < loops; ++i) { 163 this->drawClippedBitmap(canvas, 200, 0, paint); 164 } 165 } 166 167 private: 168 typedef DisplacementBaseBench INHERITED; 169 }; 170 171 /////////////////////////////////////////////////////////////////////////////// 172 173 DEF_BENCH( return new DisplacementZeroBench(true); ) 174 DEF_BENCH( return new DisplacementAlphaBench(true); ) 175 DEF_BENCH( return new DisplacementFullBench(true); ) 176 DEF_BENCH( return new DisplacementZeroBench(false); ) 177 DEF_BENCH( return new DisplacementAlphaBench(false); ) 178 DEF_BENCH( return new DisplacementFullBench(false); ) 179