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
14 #define FILTER_WIDTH_SMALL SkIntToScalar(32)
15 #define FILTER_HEIGHT_SMALL SkIntToScalar(32)
16 #define FILTER_WIDTH_LARGE SkIntToScalar(256)
17 #define FILTER_HEIGHT_LARGE SkIntToScalar(256)
18
make_bitmap()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.setColor(0xFF884422);
24 SkFont font;
25 font.setSize(SkIntToScalar(96));
26 surface->getCanvas()->drawSimpleText("g", 1, SkTextEncoding::kUTF8, 15, 55, font, paint);
27 return surface->makeImageSnapshot();
28 }
29
make_checkerboard()30 static sk_sp<SkImage> make_checkerboard() {
31 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(80, 80));
32 SkCanvas* canvas = surface->getCanvas();
33 canvas->clear(0x00000000);
34 SkPaint darkPaint;
35 darkPaint.setColor(0xFF804020);
36 SkPaint lightPaint;
37 lightPaint.setColor(0xFF244484);
38 for (int y = 0; y < 80; y += 16) {
39 for (int x = 0; x < 80; x += 16) {
40 canvas->save();
41 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
42 canvas->drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
43 canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
44 canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
45 canvas->drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
46 canvas->restore();
47 }
48 }
49
50 return surface->makeImageSnapshot();
51 }
52
53 class MergeBench : public Benchmark {
54 public:
MergeBench(bool small)55 MergeBench(bool small) : fIsSmall(small), fInitialized(false) { }
56
57 protected:
onGetName()58 const char* onGetName() override {
59 return fIsSmall ? "merge_small" : "merge_large";
60 }
61
onDelayedSetup()62 void onDelayedSetup() override {
63 if (!fInitialized) {
64 fImage = make_bitmap();
65 fCheckerboard = make_checkerboard();
66 fInitialized = true;
67 }
68 }
69
onDraw(int loops,SkCanvas * canvas)70 void onDraw(int loops, SkCanvas* canvas) override {
71 SkRect r = fIsSmall ? SkRect::MakeWH(FILTER_WIDTH_SMALL, FILTER_HEIGHT_SMALL) :
72 SkRect::MakeWH(FILTER_WIDTH_LARGE, FILTER_HEIGHT_LARGE);
73 SkPaint paint;
74 paint.setImageFilter(SkImageFilters::Merge(SkImageFilters::Image(fCheckerboard),
75 SkImageFilters::Image(fImage)));
76 for (int i = 0; i < loops; i++) {
77 canvas->drawRect(r, paint);
78 }
79 }
80
81 private:
82 bool fIsSmall;
83 bool fInitialized;
84 sk_sp<SkImage> fImage, fCheckerboard;
85
86 using INHERITED = Benchmark;
87 };
88
89 ///////////////////////////////////////////////////////////////////////////////
90
91 DEF_BENCH( return new MergeBench(true); )
92 DEF_BENCH( return new MergeBench(false); )
93