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 "SkBlurMask.h" 10 #include "SkCanvas.h" 11 #include "SkPaint.h" 12 #include "SkRandom.h" 13 #include "SkShader.h" 14 #include "SkString.h" 15 16 class BitmapScaleBench: public Benchmark { 17 int fLoopCount; 18 int fInputSize; 19 int fOutputSize; 20 SkString fName; 21 22 public: BitmapScaleBench(int is,int os)23 BitmapScaleBench( int is, int os) { 24 fInputSize = is; 25 fOutputSize = os; 26 27 fLoopCount = 20; 28 } 29 30 protected: 31 32 SkBitmap fInputBitmap, fOutputBitmap; 33 SkMatrix fMatrix; 34 onGetName()35 const char* onGetName() override { 36 return fName.c_str(); 37 } 38 inputSize() const39 int inputSize() const { 40 return fInputSize; 41 } 42 outputSize() const43 int outputSize() const { 44 return fOutputSize; 45 } 46 scale() const47 float scale() const { 48 return float(outputSize())/inputSize(); 49 } 50 onGetSize()51 SkIPoint onGetSize() override { 52 return SkIPoint::Make( fOutputSize, fOutputSize ); 53 } 54 setName(const char * name)55 void setName(const char * name) { 56 fName.printf( "bitmap_scale_%s_%d_%d", name, fInputSize, fOutputSize ); 57 } 58 onDelayedSetup()59 void onDelayedSetup() override { 60 fInputBitmap.allocN32Pixels(fInputSize, fInputSize, true); 61 fInputBitmap.eraseColor(SK_ColorWHITE); 62 63 fOutputBitmap.allocN32Pixels(fOutputSize, fOutputSize, true); 64 65 fMatrix.setScale( scale(), scale() ); 66 } 67 onDraw(int loops,SkCanvas *)68 void onDraw(int loops, SkCanvas*) override { 69 SkPaint paint; 70 this->setupPaint(&paint); 71 72 preBenchSetup(); 73 74 for (int i = 0; i < loops; i++) { 75 doScaleImage(); 76 } 77 } 78 79 virtual void doScaleImage() = 0; preBenchSetup()80 virtual void preBenchSetup() {} 81 private: 82 typedef Benchmark INHERITED; 83 }; 84 85 class BitmapFilterScaleBench: public BitmapScaleBench { 86 public: BitmapFilterScaleBench(int is,int os)87 BitmapFilterScaleBench( int is, int os) : INHERITED(is, os) { 88 setName( "filter" ); 89 } 90 protected: doScaleImage()91 void doScaleImage() override { 92 SkCanvas canvas( fOutputBitmap ); 93 SkPaint paint; 94 95 paint.setFilterQuality(kHigh_SkFilterQuality); 96 fInputBitmap.notifyPixelsChanged(); 97 canvas.concat(fMatrix); 98 canvas.drawBitmap(fInputBitmap, 0, 0, &paint ); 99 } 100 private: 101 typedef BitmapScaleBench INHERITED; 102 }; 103 104 DEF_BENCH(return new BitmapFilterScaleBench(10, 90);) 105 DEF_BENCH(return new BitmapFilterScaleBench(30, 90);) 106 DEF_BENCH(return new BitmapFilterScaleBench(80, 90);) 107 DEF_BENCH(return new BitmapFilterScaleBench(90, 90);) 108 DEF_BENCH(return new BitmapFilterScaleBench(90, 80);) 109 DEF_BENCH(return new BitmapFilterScaleBench(90, 30);) 110 DEF_BENCH(return new BitmapFilterScaleBench(90, 10);) 111 DEF_BENCH(return new BitmapFilterScaleBench(256, 64);) 112 DEF_BENCH(return new BitmapFilterScaleBench(64, 256);) 113 114 /////////////////////////////////////////////////////////////////////////////////////////////// 115 116 #include "SkBitmapScaler.h" 117 118 class PixmapScalerBench: public Benchmark { 119 SkBitmapScaler::ResizeMethod fMethod; 120 SkString fName; 121 SkBitmap fSrc, fDst; 122 123 public: PixmapScalerBench(SkBitmapScaler::ResizeMethod method,const char suffix[])124 PixmapScalerBench(SkBitmapScaler::ResizeMethod method, const char suffix[]) : fMethod(method) { 125 fName.printf("pixmapscaler_%s", suffix); 126 } 127 128 protected: onGetName()129 const char* onGetName() override { 130 return fName.c_str(); 131 } 132 onGetSize()133 SkIPoint onGetSize() override { return{ 100, 100 }; } 134 isSuitableFor(Backend backend)135 bool isSuitableFor(Backend backend) override { 136 return backend == kNonRendering_Backend; 137 } 138 onDelayedSetup()139 void onDelayedSetup() override { 140 fSrc.allocN32Pixels(640, 480); 141 fSrc.eraseColor(SK_ColorWHITE); 142 fDst.allocN32Pixels(300, 250); 143 } 144 onDraw(int loops,SkCanvas *)145 void onDraw(int loops, SkCanvas*) override { 146 SkPixmap src, dst; 147 fSrc.peekPixels(&src); 148 fDst.peekPixels(&dst); 149 for (int i = 0; i < loops * 16; i++) { 150 SkBitmapScaler::Resize(dst, src, fMethod); 151 } 152 } 153 154 private: 155 typedef Benchmark INHERITED; 156 }; 157 DEF_BENCH( return new PixmapScalerBench(SkBitmapScaler::RESIZE_LANCZOS3, "lanczos"); ) 158 DEF_BENCH( return new PixmapScalerBench(SkBitmapScaler::RESIZE_MITCHELL, "mitchell"); ) 159 DEF_BENCH( return new PixmapScalerBench(SkBitmapScaler::RESIZE_HAMMING, "hamming"); ) 160 DEF_BENCH( return new PixmapScalerBench(SkBitmapScaler::RESIZE_TRIANGLE, "triangle"); ) 161 DEF_BENCH( return new PixmapScalerBench(SkBitmapScaler::RESIZE_BOX, "box"); ) 162