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 "SkBlendImageFilter.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkDevice.h" |
| 12 | |
| 13 | #define FILTER_WIDTH 256 |
| 14 | #define FILTER_HEIGHT 256 |
| 15 | |
| 16 | class BlendBaseBench : public SkBenchmark { |
| 17 | public: |
| 18 | BlendBaseBench(void* param) : INHERITED(param), fInitialized(false) { |
| 19 | } |
| 20 | |
| 21 | protected: |
| 22 | virtual void onPreDraw() SK_OVERRIDE { |
| 23 | if (!fInitialized) { |
| 24 | make_bitmap(); |
| 25 | make_checkerboard(); |
| 26 | fInitialized = true; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x) { |
| 31 | canvas->save(); |
| 32 | canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), 0, |
| 33 | SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height()))); |
| 34 | canvas->drawBitmap(fBitmap, SkIntToScalar(x), 0, &paint); |
| 35 | canvas->restore(); |
| 36 | } |
| 37 | |
| 38 | void make_bitmap() { |
| 39 | fBitmap.setConfig(SkBitmap::kARGB_8888_Config, FILTER_WIDTH, FILTER_HEIGHT); |
| 40 | fBitmap.allocPixels(); |
| 41 | SkDevice device(fBitmap); |
| 42 | SkCanvas canvas(&device); |
| 43 | canvas.clear(0x00000000); |
| 44 | SkPaint paint; |
| 45 | paint.setAntiAlias(true); |
| 46 | paint.setColor(0xD000D000); |
| 47 | paint.setTextSize(SkIntToScalar(96)); |
| 48 | const char* str = "e"; |
| 49 | canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(65), paint); |
| 50 | } |
| 51 | |
| 52 | void make_checkerboard() { |
| 53 | fCheckerboard.setConfig(SkBitmap::kARGB_8888_Config, FILTER_WIDTH, FILTER_HEIGHT); |
| 54 | fCheckerboard.allocPixels(); |
| 55 | SkDevice device(fCheckerboard); |
| 56 | SkCanvas canvas(&device); |
| 57 | canvas.clear(0x00000000); |
| 58 | SkPaint darkPaint; |
| 59 | darkPaint.setColor(0xFF404040); |
| 60 | SkPaint lightPaint; |
| 61 | lightPaint.setColor(0xFFA0A0A0); |
| 62 | for (int y = 0; y < FILTER_HEIGHT; y += 16) { |
| 63 | for (int x = 0; x < FILTER_WIDTH; x += 16) { |
| 64 | canvas.save(); |
| 65 | canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 66 | canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint); |
| 67 | canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint); |
| 68 | canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint); |
| 69 | canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint); |
| 70 | canvas.restore(); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | typedef SkBenchmark INHERITED; |
| 76 | SkBitmap fBitmap, fCheckerboard; |
| 77 | bool fInitialized; |
| 78 | }; |
| 79 | |
| 80 | class BlendNormalBench : public BlendBaseBench { |
| 81 | public: |
| 82 | BlendNormalBench(void* param) : INHERITED(param) { |
| 83 | } |
| 84 | |
| 85 | protected: |
| 86 | virtual const char* onGetName() SK_OVERRIDE { |
| 87 | return "blend_normal"; |
| 88 | } |
| 89 | |
| 90 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 91 | SkPaint paint; |
| 92 | SkAutoTUnref<SkImageFilter> background(SkNEW_ARGS(SkBitmapSource, (fCheckerboard))); |
| 93 | paint.setImageFilter(SkNEW_ARGS(SkBlendImageFilter, |
| 94 | (SkBlendImageFilter::kNormal_Mode, background)))->unref(); |
| 95 | drawClippedBitmap(canvas, paint, 0); |
| 96 | } |
| 97 | |
| 98 | private: |
| 99 | typedef BlendBaseBench INHERITED; |
| 100 | }; |
| 101 | |
| 102 | class BlendMultiplyBench : public BlendBaseBench { |
| 103 | public: |
| 104 | BlendMultiplyBench(void* param) : INHERITED(param) { |
| 105 | } |
| 106 | |
| 107 | protected: |
| 108 | virtual const char* onGetName() SK_OVERRIDE { |
| 109 | return "blend_multiply"; |
| 110 | } |
| 111 | |
| 112 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 113 | SkPaint paint; |
| 114 | SkAutoTUnref<SkImageFilter> background(SkNEW_ARGS(SkBitmapSource, (fCheckerboard))); |
| 115 | paint.setImageFilter(SkNEW_ARGS(SkBlendImageFilter, |
| 116 | (SkBlendImageFilter::kMultiply_Mode, background)))->unref(); |
| 117 | drawClippedBitmap(canvas, paint, 100); |
| 118 | } |
| 119 | |
| 120 | private: |
| 121 | typedef BlendBaseBench INHERITED; |
| 122 | }; |
| 123 | |
| 124 | class BlendScreenBench : public BlendBaseBench { |
| 125 | public: |
| 126 | BlendScreenBench(void* param) : INHERITED(param) { |
| 127 | } |
| 128 | |
| 129 | protected: |
| 130 | virtual const char* onGetName() SK_OVERRIDE { |
| 131 | return "blend_screen"; |
| 132 | } |
| 133 | |
| 134 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 135 | SkPaint paint; |
| 136 | SkAutoTUnref<SkImageFilter> background(SkNEW_ARGS(SkBitmapSource, (fCheckerboard))); |
| 137 | paint.setImageFilter(SkNEW_ARGS(SkBlendImageFilter, |
| 138 | (SkBlendImageFilter::kScreen_Mode, background)))->unref(); |
| 139 | drawClippedBitmap(canvas, paint, 200); |
| 140 | } |
| 141 | |
| 142 | private: |
| 143 | typedef BlendBaseBench INHERITED; |
| 144 | }; |
| 145 | |
| 146 | class BlendDarkenBench : public BlendBaseBench { |
| 147 | public: |
| 148 | BlendDarkenBench(void* param) : INHERITED(param) { |
| 149 | } |
| 150 | |
| 151 | protected: |
| 152 | virtual const char* onGetName() SK_OVERRIDE { |
| 153 | return "blend_darken"; |
| 154 | } |
| 155 | |
| 156 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 157 | SkPaint paint; |
| 158 | SkAutoTUnref<SkImageFilter> background(SkNEW_ARGS(SkBitmapSource, (fCheckerboard))); |
| 159 | paint.setImageFilter(SkNEW_ARGS(SkBlendImageFilter, |
| 160 | (SkBlendImageFilter::kDarken_Mode, background)))->unref(); |
| 161 | drawClippedBitmap(canvas, paint, 300); |
| 162 | } |
| 163 | |
| 164 | private: |
| 165 | typedef BlendBaseBench INHERITED; |
| 166 | }; |
| 167 | |
| 168 | class BlendLightenBench : public BlendBaseBench { |
| 169 | public: |
| 170 | BlendLightenBench(void* param) : INHERITED(param) { |
| 171 | } |
| 172 | |
| 173 | protected: |
| 174 | virtual const char* onGetName() SK_OVERRIDE { |
| 175 | return "blend_lighten"; |
| 176 | } |
| 177 | |
| 178 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 179 | SkPaint paint; |
| 180 | SkAutoTUnref<SkImageFilter> background(SkNEW_ARGS(SkBitmapSource, (fCheckerboard))); |
| 181 | paint.setImageFilter(SkNEW_ARGS(SkBlendImageFilter, |
| 182 | (SkBlendImageFilter::kLighten_Mode, background)))->unref(); |
| 183 | drawClippedBitmap(canvas, paint, 400); |
| 184 | } |
| 185 | |
| 186 | private: |
| 187 | typedef BlendBaseBench INHERITED; |
| 188 | }; |
| 189 | |
| 190 | /////////////////////////////////////////////////////////////////////////////// |
| 191 | |
| 192 | DEF_BENCH( return new BlendNormalBench(p); ) |
| 193 | DEF_BENCH( return new BlendMultiplyBench(p); ) |
| 194 | DEF_BENCH( return new BlendScreenBench(p); ) |
| 195 | DEF_BENCH( return new BlendDarkenBench(p); ) |
| 196 | DEF_BENCH( return new BlendLightenBench(p); ) |