sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +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 | #include "SkBenchmark.h" |
| 8 | #include "SkBitmapSource.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkDevice.h" |
| 11 | #include "SkDisplacementMapEffect.h" |
| 12 | |
sugoi@google.com | ca425d9 | 2013-04-23 14:37:38 +0000 | [diff] [blame] | 13 | #define FILTER_WIDTH_SMALL 32 |
| 14 | #define FILTER_HEIGHT_SMALL 32 |
| 15 | #define FILTER_WIDTH_LARGE 256 |
| 16 | #define FILTER_HEIGHT_LARGE 256 |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 17 | |
| 18 | class DisplacementBaseBench : public SkBenchmark { |
| 19 | public: |
| 20 | DisplacementBaseBench(void* param, bool small) : |
| 21 | INHERITED(param), fInitialized(false), fIsSmall(small) { |
| 22 | } |
| 23 | |
| 24 | protected: |
| 25 | virtual void onPreDraw() SK_OVERRIDE { |
| 26 | if (!fInitialized) { |
| 27 | make_bitmap(); |
| 28 | make_checkerboard(); |
| 29 | fInitialized = true; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | void make_bitmap() { |
| 34 | const int w = isSmall() ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE; |
| 35 | const int h = isSmall() ? FILTER_HEIGHT_LARGE : FILTER_HEIGHT_LARGE; |
| 36 | fBitmap.setConfig(SkBitmap::kARGB_8888_Config, w, h); |
| 37 | fBitmap.allocPixels(); |
| 38 | SkDevice 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 | |
| 49 | void make_checkerboard() { |
| 50 | const int w = isSmall() ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE; |
| 51 | const int h = isSmall() ? FILTER_HEIGHT_LARGE : FILTER_HEIGHT_LARGE; |
| 52 | fCheckerboard.setConfig(SkBitmap::kARGB_8888_Config, w, h); |
| 53 | fCheckerboard.allocPixels(); |
| 54 | SkDevice 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 | |
| 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()), SkIntToScalar(fBitmap.height()))); |
| 78 | canvas->drawBitmap(fBitmap, SkIntToScalar(x), SkIntToScalar(y), &paint); |
| 79 | canvas->restore(); |
| 80 | } |
| 81 | |
| 82 | inline bool isSmall() const { return fIsSmall; } |
| 83 | |
| 84 | SkBitmap fBitmap, fCheckerboard; |
| 85 | private: |
| 86 | bool fInitialized; |
| 87 | bool fIsSmall; |
| 88 | typedef SkBenchmark INHERITED; |
| 89 | }; |
| 90 | |
| 91 | class DisplacementZeroBench : public DisplacementBaseBench { |
| 92 | public: |
| 93 | DisplacementZeroBench(void* param, bool small) : INHERITED(param, small) { |
| 94 | } |
| 95 | |
| 96 | protected: |
| 97 | virtual const char* onGetName() SK_OVERRIDE { |
| 98 | return isSmall() ? "displacement_zero_small" : "displacement_zero_large"; |
| 99 | } |
| 100 | |
| 101 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 102 | SkPaint paint; |
| 103 | SkAutoTUnref<SkImageFilter> displ(SkNEW_ARGS(SkBitmapSource, (fCheckerboard))); |
| 104 | // No displacement effect |
| 105 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 106 | (SkDisplacementMapEffect::kR_ChannelSelectorType, |
| 107 | SkDisplacementMapEffect::kG_ChannelSelectorType, 0.0f, displ)))->unref(); |
| 108 | drawClippedBitmap(canvas, 0, 0, paint); |
| 109 | } |
| 110 | |
| 111 | private: |
| 112 | typedef DisplacementBaseBench INHERITED; |
| 113 | }; |
| 114 | |
| 115 | class DisplacementAlphaBench : public DisplacementBaseBench { |
| 116 | public: |
| 117 | DisplacementAlphaBench(void* param, bool small) : INHERITED(param, small) { |
| 118 | } |
| 119 | |
| 120 | protected: |
| 121 | virtual const char* onGetName() SK_OVERRIDE { |
| 122 | return isSmall() ? "displacement_alpha_small" : "displacement_alpha_large"; |
| 123 | } |
| 124 | |
| 125 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 126 | SkPaint paint; |
| 127 | SkAutoTUnref<SkImageFilter> displ(SkNEW_ARGS(SkBitmapSource, (fCheckerboard))); |
| 128 | // Displacement, with 1 alpha component (which isn't pre-multiplied) |
| 129 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 130 | (SkDisplacementMapEffect::kB_ChannelSelectorType, |
| 131 | SkDisplacementMapEffect::kA_ChannelSelectorType, 16.0f, displ)))->unref(); |
| 132 | drawClippedBitmap(canvas, 100, 0, paint); |
| 133 | } |
| 134 | |
| 135 | private: |
| 136 | typedef DisplacementBaseBench INHERITED; |
| 137 | }; |
| 138 | |
| 139 | class DisplacementFullBench : public DisplacementBaseBench { |
| 140 | public: |
| 141 | DisplacementFullBench(void* param, bool small) : INHERITED(param, small) { |
| 142 | } |
| 143 | |
| 144 | protected: |
| 145 | virtual const char* onGetName() SK_OVERRIDE { |
| 146 | return isSmall() ? "displacement_full_small" : "displacement_full_large"; |
| 147 | } |
| 148 | |
| 149 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 150 | SkPaint paint; |
| 151 | SkAutoTUnref<SkImageFilter> displ(SkNEW_ARGS(SkBitmapSource, (fCheckerboard))); |
| 152 | // Displacement, with 2 non-alpha components |
| 153 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 154 | (SkDisplacementMapEffect::kR_ChannelSelectorType, |
| 155 | SkDisplacementMapEffect::kB_ChannelSelectorType, 32.0f, displ)))->unref(); |
| 156 | drawClippedBitmap(canvas, 200, 0, paint); |
| 157 | } |
| 158 | |
| 159 | private: |
| 160 | typedef DisplacementBaseBench INHERITED; |
| 161 | }; |
| 162 | |
| 163 | /////////////////////////////////////////////////////////////////////////////// |
| 164 | |
| 165 | DEF_BENCH( return new DisplacementZeroBench(p, true); ) |
| 166 | DEF_BENCH( return new DisplacementAlphaBench(p, true); ) |
| 167 | DEF_BENCH( return new DisplacementFullBench(p, true); ) |
| 168 | DEF_BENCH( return new DisplacementZeroBench(p, false); ) |
| 169 | DEF_BENCH( return new DisplacementAlphaBench(p, false); ) |
| 170 | DEF_BENCH( return new DisplacementFullBench(p, false); ) |