OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "Benchmark.h" | 8 #include "Benchmark.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkImageSource.h" | 10 #include "SkImageSource.h" |
11 #include "SkMergeImageFilter.h" | 11 #include "SkMergeImageFilter.h" |
12 #include "SkSurface.h" | 12 #include "SkSurface.h" |
13 | 13 |
14 #define FILTER_WIDTH_SMALL SkIntToScalar(32) | 14 #define FILTER_WIDTH_SMALL SkIntToScalar(32) |
15 #define FILTER_HEIGHT_SMALL SkIntToScalar(32) | 15 #define FILTER_HEIGHT_SMALL SkIntToScalar(32) |
16 #define FILTER_WIDTH_LARGE SkIntToScalar(256) | 16 #define FILTER_WIDTH_LARGE SkIntToScalar(256) |
17 #define FILTER_HEIGHT_LARGE SkIntToScalar(256) | 17 #define FILTER_HEIGHT_LARGE SkIntToScalar(256) |
18 | 18 |
| 19 static sk_sp<SkImage> make_bitmap() { |
| 20 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(80, 80)); |
| 21 surface->getCanvas()->clear(0x00000000); |
| 22 SkPaint paint; |
| 23 paint.setAntiAlias(true); |
| 24 paint.setColor(0xFF884422); |
| 25 paint.setTextSize(SkIntToScalar(96)); |
| 26 const char* str = "g"; |
| 27 surface->getCanvas()->drawText(str, strlen(str), 15, 55, paint); |
| 28 return surface->makeImageSnapshot(); |
| 29 } |
| 30 |
| 31 static sk_sp<SkImage> make_checkerboard() { |
| 32 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(80, 80)); |
| 33 SkCanvas* canvas = surface->getCanvas(); |
| 34 canvas->clear(0x00000000); |
| 35 SkPaint darkPaint; |
| 36 darkPaint.setColor(0xFF804020); |
| 37 SkPaint lightPaint; |
| 38 lightPaint.setColor(0xFF244484); |
| 39 for (int y = 0; y < 80; y += 16) { |
| 40 for (int x = 0; x < 80; x += 16) { |
| 41 canvas->save(); |
| 42 canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 43 canvas->drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint); |
| 44 canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint); |
| 45 canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint); |
| 46 canvas->drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint); |
| 47 canvas->restore(); |
| 48 } |
| 49 } |
| 50 |
| 51 return surface->makeImageSnapshot(); |
| 52 } |
| 53 |
19 class MergeBench : public Benchmark { | 54 class MergeBench : public Benchmark { |
20 public: | 55 public: |
21 MergeBench(bool small) : fIsSmall(small), fInitialized(false) { } | 56 MergeBench(bool small) : fIsSmall(small), fInitialized(false) { } |
22 | 57 |
23 protected: | 58 protected: |
24 const char* onGetName() override { | 59 const char* onGetName() override { |
25 return fIsSmall ? "merge_small" : "merge_large"; | 60 return fIsSmall ? "merge_small" : "merge_large"; |
26 } | 61 } |
27 | 62 |
28 void onDelayedSetup() override { | 63 void onDelayedSetup() override { |
29 if (!fInitialized) { | 64 if (!fInitialized) { |
30 make_bitmap(); | 65 fImage = make_bitmap(); |
31 make_checkerboard(); | 66 fCheckerboard = make_checkerboard(); |
32 fInitialized = true; | 67 fInitialized = true; |
33 } | 68 } |
34 } | 69 } |
35 | 70 |
36 void onDraw(int loops, SkCanvas* canvas) override { | 71 void onDraw(int loops, SkCanvas* canvas) override { |
37 SkRect r = fIsSmall ? SkRect::MakeWH(FILTER_WIDTH_SMALL, FILTER_HEIGHT_S
MALL) : | 72 SkRect r = fIsSmall ? SkRect::MakeWH(FILTER_WIDTH_SMALL, FILTER_HEIGHT_S
MALL) : |
38 SkRect::MakeWH(FILTER_WIDTH_LARGE, FILTER_HEIGHT_L
ARGE); | 73 SkRect::MakeWH(FILTER_WIDTH_LARGE, FILTER_HEIGHT_L
ARGE); |
39 SkPaint paint; | 74 SkPaint paint; |
40 paint.setImageFilter(mergeBitmaps())->unref(); | 75 paint.setImageFilter(this->mergeBitmaps()); |
41 for (int i = 0; i < loops; i++) { | 76 for (int i = 0; i < loops; i++) { |
42 canvas->drawRect(r, paint); | 77 canvas->drawRect(r, paint); |
43 } | 78 } |
44 } | 79 } |
45 | 80 |
46 private: | 81 private: |
47 SkImageFilter* mergeBitmaps() { | 82 sk_sp<SkImageFilter> mergeBitmaps() { |
48 SkAutoTUnref<SkImageFilter> first(SkImageSource::Create(fCheckerboard.ge
t())); | 83 sk_sp<SkImageFilter> first(SkImageSource::Create(fCheckerboard.get())); |
49 SkAutoTUnref<SkImageFilter> second(SkImageSource::Create(fImage.get())); | 84 sk_sp<SkImageFilter> second(SkImageSource::Create(fImage.get())); |
50 return SkMergeImageFilter::Create(first, second); | 85 return SkMergeImageFilter::Make(std::move(first), std::move(second)); |
51 } | |
52 | |
53 void make_bitmap() { | |
54 auto surface(SkSurface::MakeRasterN32Premul(80, 80)); | |
55 surface->getCanvas()->clear(0x00000000); | |
56 SkPaint paint; | |
57 paint.setAntiAlias(true); | |
58 paint.setColor(0xFF884422); | |
59 paint.setTextSize(SkIntToScalar(96)); | |
60 const char* str = "g"; | |
61 surface->getCanvas()->drawText(str, strlen(str), 15, 55, paint); | |
62 fImage = surface->makeImageSnapshot(); | |
63 } | |
64 | |
65 void make_checkerboard() { | |
66 auto surface(SkSurface::MakeRasterN32Premul(80, 80)); | |
67 SkCanvas* canvas = surface->getCanvas(); | |
68 canvas->clear(0x00000000); | |
69 SkPaint darkPaint; | |
70 darkPaint.setColor(0xFF804020); | |
71 SkPaint lightPaint; | |
72 lightPaint.setColor(0xFF244484); | |
73 for (int y = 0; y < 80; y += 16) { | |
74 for (int x = 0; x < 80; x += 16) { | |
75 canvas->save(); | |
76 canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); | |
77 canvas->drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint); | |
78 canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint); | |
79 canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint); | |
80 canvas->drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint); | |
81 canvas->restore(); | |
82 } | |
83 } | |
84 | |
85 fCheckerboard = surface->makeImageSnapshot(); | |
86 } | 86 } |
87 | 87 |
88 bool fIsSmall; | 88 bool fIsSmall; |
89 bool fInitialized; | 89 bool fInitialized; |
90 sk_sp<SkImage> fImage, fCheckerboard; | 90 sk_sp<SkImage> fImage, fCheckerboard; |
91 | 91 |
92 typedef Benchmark INHERITED; | 92 typedef Benchmark INHERITED; |
93 }; | 93 }; |
94 | 94 |
95 /////////////////////////////////////////////////////////////////////////////// | 95 /////////////////////////////////////////////////////////////////////////////// |
96 | 96 |
97 DEF_BENCH( return new MergeBench(true); ) | 97 DEF_BENCH( return new MergeBench(true); ) |
98 DEF_BENCH( return new MergeBench(false); ) | 98 DEF_BENCH( return new MergeBench(false); ) |
OLD | NEW |