| 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 "bench/Benchmark.h" |
| 9 |
#include "include/core/SkCanvas.h" |
| 10 |
#include "include/core/SkFont.h" |
| 11 |
#include "include/core/SkSurface.h" |
| 12 |
#include "include/effects/SkImageFilters.h" |
| 13 |
#include "tools/fonts/FontToolUtils.h" |
| 14 |
|
| 15 |
#define FILTER_WIDTH_SMALL SkIntToScalar(32) |
| 16 |
#define FILTER_HEIGHT_SMALL SkIntToScalar(32) |
| 17 |
#define FILTER_WIDTH_LARGE SkIntToScalar(256) |
| 18 |
#define FILTER_HEIGHT_LARGE SkIntToScalar(256) |
| 19 |
|
| 20 |
static sk_sp<SkImage> make_bitmap() { |
| 21 |
sk_sp<SkSurface> surface(SkSurfaces::Raster(SkImageInfo::MakeN32Premul(80, 80))); |
| 22 |
surface->getCanvas()->clear(0x00000000); |
| 23 |
SkPaint paint; |
| 24 |
paint.setColor(0xFF884422); |
| 25 |
SkFont font = ToolUtils::DefaultFont(); |
| 26 |
font.setSize(SkIntToScalar(96)); |
| 27 |
surface->getCanvas()->drawSimpleText("g", 1, SkTextEncoding::kUTF8, 15, 55, font, paint); |
| 28 |
return surface->makeImageSnapshot(); |
| 29 |
} |
| 30 |
|
| 31 |
static sk_sp<SkImage> make_checkerboard() { |
| 32 |
sk_sp<SkSurface> surface(SkSurfaces::Raster(SkImageInfo::MakeN32Premul(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 |
|
| 54 |
class MergeBench : public Benchmark { |
| 55 |
public: |
| 56 |
MergeBench(bool small) : fIsSmall(small), fInitialized(false) { } |
| 57 |
|
| 58 |
protected: |
| 59 |
const char* onGetName() override { |
| 60 |
return fIsSmall ? "merge_small" : "merge_large"; |
| 61 |
} |
| 62 |
|
| 63 |
void onDelayedSetup() override { |
| 64 |
if (!fInitialized) { |
| 65 |
fImage = make_bitmap(); |
| 66 |
fCheckerboard = make_checkerboard(); |
| 67 |
fInitialized = true; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
void onDraw(int loops, SkCanvas* canvas) override { |
| 72 |
SkRect r = fIsSmall ? SkRect::MakeWH(FILTER_WIDTH_SMALL, FILTER_HEIGHT_SMALL) : |
| 73 |
SkRect::MakeWH(FILTER_WIDTH_LARGE, FILTER_HEIGHT_LARGE); |
| 74 |
SkPaint paint; |
| 75 |
paint.setImageFilter(SkImageFilters::Merge( |
| 76 |
SkImageFilters::Image(fCheckerboard, SkFilterMode::kNearest), |
| 77 |
SkImageFilters::Image(fImage, SkFilterMode::kNearest))); |
| 78 |
for (int i = 0; i < loops; i++) { |
| 79 |
canvas->drawRect(r, paint); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
private: |
| 84 |
bool fIsSmall; |
| 85 |
bool fInitialized; |
| 86 |
sk_sp<SkImage> fImage, fCheckerboard; |
| 87 |
|
| 88 |
using INHERITED = Benchmark; |
| 89 |
}; |
| 90 |
|
| 91 |
/////////////////////////////////////////////////////////////////////////////// |
| 92 |
|
| 93 |
DEF_BENCH( return new MergeBench(true); ) |
| 94 |
DEF_BENCH( return new MergeBench(false); ) |