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 "SkBenchmark.h" 9 #include "SkCanvas.h" 10 #include "SkPaint.h" 11 #include "SkRandom.h" 12 #include "SkShader.h" 13 #include "SkString.h" 14 #include "SkBlurMask.h" 15 16 class BitmapScaleBench: public SkBenchmark { 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 virtual const char* onGetName() { 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() SK_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 onPreDraw()59 virtual void onPreDraw() { 60 fInputBitmap.setConfig(SkBitmap::kARGB_8888_Config, 61 fInputSize, fInputSize, 0, kOpaque_SkAlphaType); 62 fInputBitmap.allocPixels(); 63 fInputBitmap.eraseColor(SK_ColorWHITE); 64 65 fOutputBitmap.setConfig(SkBitmap::kARGB_8888_Config, 66 fOutputSize, fOutputSize, 0, kOpaque_SkAlphaType); 67 fOutputBitmap.allocPixels(); 68 69 fMatrix.setScale( scale(), scale() ); 70 } 71 onDraw(const int loops,SkCanvas *)72 virtual void onDraw(const int loops, SkCanvas*) { 73 SkPaint paint; 74 this->setupPaint(&paint); 75 76 preBenchSetup(); 77 78 for (int i = 0; i < loops; i++) { 79 doScaleImage(); 80 } 81 } 82 83 virtual void doScaleImage() = 0; preBenchSetup()84 virtual void preBenchSetup() {} 85 private: 86 typedef SkBenchmark INHERITED; 87 }; 88 89 class BitmapFilterScaleBench: public BitmapScaleBench { 90 public: BitmapFilterScaleBench(int is,int os)91 BitmapFilterScaleBench( int is, int os) : INHERITED(is, os) { 92 setName( "filter" ); 93 } 94 protected: doScaleImage()95 virtual void doScaleImage() SK_OVERRIDE { 96 SkCanvas canvas( fOutputBitmap ); 97 SkPaint paint; 98 99 paint.setFilterLevel(SkPaint::kHigh_FilterLevel); 100 canvas.drawBitmapMatrix( fInputBitmap, fMatrix, &paint ); 101 } 102 private: 103 typedef BitmapScaleBench INHERITED; 104 }; 105 106 DEF_BENCH(return new BitmapFilterScaleBench(10, 90);) 107 DEF_BENCH(return new BitmapFilterScaleBench(30, 90);) 108 DEF_BENCH(return new BitmapFilterScaleBench(80, 90);) 109 DEF_BENCH(return new BitmapFilterScaleBench(90, 90);) 110 DEF_BENCH(return new BitmapFilterScaleBench(90, 80);) 111 DEF_BENCH(return new BitmapFilterScaleBench(90, 30);) 112 DEF_BENCH(return new BitmapFilterScaleBench(90, 10);) 113 DEF_BENCH(return new BitmapFilterScaleBench(256, 64);) 114 DEF_BENCH(return new BitmapFilterScaleBench(64, 256);) 115