• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "Benchmark.h"
9 #include "SkCanvas.h"
10 #include "SkImageSource.h"
11 #include "SkMergeImageFilter.h"
12 #include "SkSurface.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.setAntiAlias(true);
24     paint.setColor(0xFF884422);
25     paint.setTextSize(SkIntToScalar(96));
26     const char* str = "g";
27     surface->getCanvas()->drawString(str, 15, 55, paint);
28     return surface->makeImageSnapshot();
29 }
30 
make_checkerboard()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 
54 class MergeBench : public Benchmark {
55 public:
MergeBench(bool small)56     MergeBench(bool small) : fIsSmall(small), fInitialized(false) { }
57 
58 protected:
onGetName()59     const char* onGetName() override {
60         return fIsSmall ? "merge_small" : "merge_large";
61     }
62 
onDelayedSetup()63     void onDelayedSetup() override {
64         if (!fInitialized) {
65             fImage = make_bitmap();
66             fCheckerboard = make_checkerboard();
67             fInitialized = true;
68         }
69     }
70 
onDraw(int loops,SkCanvas * canvas)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(this->mergeBitmaps());
76         for (int i = 0; i < loops; i++) {
77             canvas->drawRect(r, paint);
78         }
79     }
80 
81 private:
mergeBitmaps()82     sk_sp<SkImageFilter> mergeBitmaps() {
83         return SkMergeImageFilter::Make(SkImageSource::Make(fCheckerboard),
84                                         SkImageSource::Make(fImage));
85     }
86 
87     bool fIsSmall;
88     bool fInitialized;
89     sk_sp<SkImage> fImage, fCheckerboard;
90 
91     typedef Benchmark INHERITED;
92 };
93 
94 ///////////////////////////////////////////////////////////////////////////////
95 
96 DEF_BENCH( return new MergeBench(true); )
97 DEF_BENCH( return new MergeBench(false); )
98