humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 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: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 23 | BitmapScaleBench( int is, int os) { |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 24 | fInputSize = is; |
| 25 | fOutputSize = os; |
| 26 | |
humper@google.com | 523521c | 2013-07-11 20:28:30 +0000 | [diff] [blame] | 27 | fLoopCount = 20; |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | protected: |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 31 | |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 32 | SkBitmap fInputBitmap, fOutputBitmap; |
| 33 | SkMatrix fMatrix; |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 34 | |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 35 | virtual const char* onGetName() { |
| 36 | return fName.c_str(); |
| 37 | } |
| 38 | |
| 39 | int inputSize() const { |
| 40 | return fInputSize; |
| 41 | } |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 42 | |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 43 | int outputSize() const { |
| 44 | return fOutputSize; |
| 45 | } |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 46 | |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 47 | float scale() const { |
| 48 | return float(outputSize())/inputSize(); |
| 49 | } |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 50 | |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 51 | SkIPoint onGetSize() SK_OVERRIDE { |
| 52 | return SkIPoint::Make( fOutputSize, fOutputSize ); |
| 53 | } |
| 54 | |
| 55 | void setName(const char * name) { |
| 56 | fName.printf( "bitmap_scale_%s_%d_%d", name, fInputSize, fOutputSize ); |
| 57 | } |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 58 | |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 59 | virtual void onPreDraw() { |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 60 | fInputBitmap.setConfig(SkBitmap::kARGB_8888_Config, |
| 61 | fInputSize, fInputSize, 0, kOpaque_SkAlphaType); |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 62 | fInputBitmap.allocPixels(); |
| 63 | fInputBitmap.eraseColor(SK_ColorWHITE); |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 64 | |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 65 | fOutputBitmap.setConfig(SkBitmap::kARGB_8888_Config, |
| 66 | fOutputSize, fOutputSize, 0, kOpaque_SkAlphaType); |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 67 | fOutputBitmap.allocPixels(); |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 68 | |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 69 | fMatrix.setScale( scale(), scale() ); |
| 70 | } |
| 71 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 72 | virtual void onDraw(const int loops, SkCanvas*) { |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 73 | SkPaint paint; |
| 74 | this->setupPaint(&paint); |
| 75 | |
| 76 | preBenchSetup(); |
| 77 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 78 | for (int i = 0; i < loops; i++) { |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 79 | doScaleImage(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | virtual void doScaleImage() = 0; |
| 84 | virtual void preBenchSetup() {} |
| 85 | private: |
| 86 | typedef SkBenchmark INHERITED; |
| 87 | }; |
| 88 | |
| 89 | class BitmapFilterScaleBench: public BitmapScaleBench { |
| 90 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 91 | BitmapFilterScaleBench( int is, int os) : INHERITED(is, os) { |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 92 | setName( "filter" ); |
| 93 | } |
| 94 | protected: |
| 95 | virtual void doScaleImage() SK_OVERRIDE { |
| 96 | SkCanvas canvas( fOutputBitmap ); |
| 97 | SkPaint paint; |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 98 | |
reed@google.com | 9cfc83c | 2013-07-22 17:18:18 +0000 | [diff] [blame] | 99 | paint.setFilterLevel(SkPaint::kHigh_FilterLevel); |
commit-bot@chromium.org | 1b28f6f | 2014-03-06 17:16:37 +0000 | [diff] [blame] | 100 | fInputBitmap.notifyPixelsChanged(); |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 101 | canvas.drawBitmapMatrix( fInputBitmap, fMatrix, &paint ); |
| 102 | } |
| 103 | private: |
| 104 | typedef BitmapScaleBench INHERITED; |
| 105 | }; |
| 106 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 107 | DEF_BENCH(return new BitmapFilterScaleBench(10, 90);) |
| 108 | DEF_BENCH(return new BitmapFilterScaleBench(30, 90);) |
| 109 | DEF_BENCH(return new BitmapFilterScaleBench(80, 90);) |
| 110 | DEF_BENCH(return new BitmapFilterScaleBench(90, 90);) |
| 111 | DEF_BENCH(return new BitmapFilterScaleBench(90, 80);) |
| 112 | DEF_BENCH(return new BitmapFilterScaleBench(90, 30);) |
| 113 | DEF_BENCH(return new BitmapFilterScaleBench(90, 10);) |
| 114 | DEF_BENCH(return new BitmapFilterScaleBench(256, 64);) |
| 115 | DEF_BENCH(return new BitmapFilterScaleBench(64, 256);) |