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" |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
| 10 | #include "SkDevice.h" |
| 11 | #include "SkMergeImageFilter.h" |
| 12 | |
| 13 | #define FILTER_WIDTH_SMALL SkIntToScalar(32) |
| 14 | #define FILTER_HEIGHT_SMALL SkIntToScalar(32) |
| 15 | #define FILTER_WIDTH_LARGE SkIntToScalar(256) |
| 16 | #define FILTER_HEIGHT_LARGE SkIntToScalar(256) |
| 17 | |
| 18 | class MergeBench : public SkBenchmark { |
| 19 | public: |
| 20 | MergeBench(void* param, bool small) : INHERITED(param), fIsSmall(small), fInitialized(false) { |
| 21 | } |
| 22 | |
| 23 | protected: |
| 24 | virtual const char* onGetName() SK_OVERRIDE { |
| 25 | return fIsSmall ? "merge_small" : "merge_large"; |
| 26 | } |
| 27 | |
| 28 | virtual void onPreDraw() SK_OVERRIDE { |
| 29 | if (!fInitialized) { |
| 30 | make_bitmap(); |
| 31 | make_checkerboard(); |
| 32 | fInitialized = true; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 37 | SkRect r = fIsSmall ? SkRect::MakeWH(FILTER_WIDTH_SMALL, FILTER_HEIGHT_SMALL) : |
| 38 | SkRect::MakeWH(FILTER_WIDTH_LARGE, FILTER_HEIGHT_LARGE); |
| 39 | SkPaint paint; |
| 40 | paint.setImageFilter(mergeBitmaps())->unref(); |
| 41 | canvas->drawRect(r, paint); |
| 42 | } |
| 43 | |
| 44 | private: |
| 45 | SkImageFilter* mergeBitmaps() { |
| 46 | SkImageFilter* first = new SkBitmapSource(fCheckerboard); |
| 47 | SkImageFilter* second = new SkBitmapSource(fBitmap); |
| 48 | SkAutoUnref aur0(first); |
| 49 | SkAutoUnref aur1(second); |
| 50 | return new SkMergeImageFilter(first, second); |
| 51 | } |
| 52 | |
| 53 | void make_bitmap() { |
| 54 | fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 80, 80); |
| 55 | fBitmap.allocPixels(); |
robertphillips@google.com | 9b051a3 | 2013-08-20 20:06:40 +0000 | [diff] [blame] | 56 | SkDevice device(fBitmap); |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 57 | SkCanvas canvas(&device); |
| 58 | canvas.clear(0x00000000); |
| 59 | SkPaint paint; |
| 60 | paint.setAntiAlias(true); |
| 61 | paint.setColor(0xFF884422); |
| 62 | paint.setTextSize(SkIntToScalar(96)); |
| 63 | const char* str = "g"; |
| 64 | canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(55), paint); |
| 65 | } |
| 66 | |
| 67 | void make_checkerboard() { |
| 68 | fCheckerboard.setConfig(SkBitmap::kARGB_8888_Config, 80, 80); |
| 69 | fCheckerboard.allocPixels(); |
robertphillips@google.com | 9b051a3 | 2013-08-20 20:06:40 +0000 | [diff] [blame] | 70 | SkDevice device(fCheckerboard); |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 71 | SkCanvas canvas(&device); |
| 72 | canvas.clear(0x00000000); |
| 73 | SkPaint darkPaint; |
| 74 | darkPaint.setColor(0xFF804020); |
| 75 | SkPaint lightPaint; |
| 76 | lightPaint.setColor(0xFF244484); |
| 77 | for (int y = 0; y < 80; y += 16) { |
| 78 | for (int x = 0; x < 80; x += 16) { |
| 79 | canvas.save(); |
| 80 | canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 81 | canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint); |
| 82 | canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint); |
| 83 | canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint); |
| 84 | canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint); |
| 85 | canvas.restore(); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | bool fIsSmall; |
| 91 | bool fInitialized; |
| 92 | SkBitmap fBitmap, fCheckerboard; |
| 93 | |
| 94 | typedef SkBenchmark INHERITED; |
| 95 | }; |
| 96 | |
| 97 | /////////////////////////////////////////////////////////////////////////////// |
| 98 | |
| 99 | DEF_BENCH( return new MergeBench(p, true); ) |
| 100 | DEF_BENCH( return new MergeBench(p, false); ) |