blob: 2e1ca323503bf95e4f8ac47ba83b3c4e5b4e12d9 [file] [log] [blame]
sugoi@google.com580a1722013-04-23 14:20:45 +00001/*
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 */
reed8c0c7b02014-06-27 05:49:53 -07007
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
sugoi@google.com580a1722013-04-23 14:20:45 +00009#include "SkBitmapSource.h"
sugoi@google.com580a1722013-04-23 14:20:45 +000010#include "SkCanvas.h"
sugoi@google.com580a1722013-04-23 14:20:45 +000011#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
tfarinaf168b862014-06-19 12:32:29 -070018class MergeBench : public Benchmark {
sugoi@google.com580a1722013-04-23 14:20:45 +000019public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000020 MergeBench(bool small) : fIsSmall(small), fInitialized(false) { }
sugoi@google.com580a1722013-04-23 14:20:45 +000021
22protected:
23 virtual const char* onGetName() SK_OVERRIDE {
24 return fIsSmall ? "merge_small" : "merge_large";
25 }
26
27 virtual void onPreDraw() SK_OVERRIDE {
28 if (!fInitialized) {
29 make_bitmap();
30 make_checkerboard();
31 fInitialized = true;
32 }
33 }
34
commit-bot@chromium.org33614712013-12-03 18:17:16 +000035 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
sugoi@google.com580a1722013-04-23 14:20:45 +000036 SkRect r = fIsSmall ? SkRect::MakeWH(FILTER_WIDTH_SMALL, FILTER_HEIGHT_SMALL) :
37 SkRect::MakeWH(FILTER_WIDTH_LARGE, FILTER_HEIGHT_LARGE);
38 SkPaint paint;
39 paint.setImageFilter(mergeBitmaps())->unref();
commit-bot@chromium.org33614712013-12-03 18:17:16 +000040 for (int i = 0; i < loops; i++) {
mtklein@google.comc2897432013-09-10 19:23:38 +000041 canvas->drawRect(r, paint);
42 }
sugoi@google.com580a1722013-04-23 14:20:45 +000043 }
44
45private:
46 SkImageFilter* mergeBitmaps() {
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000047 SkImageFilter* first = SkBitmapSource::Create(fCheckerboard);
48 SkImageFilter* second = SkBitmapSource::Create(fBitmap);
sugoi@google.com580a1722013-04-23 14:20:45 +000049 SkAutoUnref aur0(first);
50 SkAutoUnref aur1(second);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000051 return SkMergeImageFilter::Create(first, second);
sugoi@google.com580a1722013-04-23 14:20:45 +000052 }
53
54 void make_bitmap() {
reed6c225732014-06-09 19:52:07 -070055 fBitmap.allocN32Pixels(80, 80);
56 SkCanvas canvas(fBitmap);
sugoi@google.com580a1722013-04-23 14:20:45 +000057 canvas.clear(0x00000000);
58 SkPaint paint;
59 paint.setAntiAlias(true);
60 paint.setColor(0xFF884422);
61 paint.setTextSize(SkIntToScalar(96));
62 const char* str = "g";
63 canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(55), paint);
64 }
65
66 void make_checkerboard() {
reed6c225732014-06-09 19:52:07 -070067 fCheckerboard.allocN32Pixels(80, 80);
68 SkCanvas canvas(fCheckerboard);
sugoi@google.com580a1722013-04-23 14:20:45 +000069 canvas.clear(0x00000000);
70 SkPaint darkPaint;
71 darkPaint.setColor(0xFF804020);
72 SkPaint lightPaint;
73 lightPaint.setColor(0xFF244484);
74 for (int y = 0; y < 80; y += 16) {
75 for (int x = 0; x < 80; x += 16) {
76 canvas.save();
77 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
78 canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
79 canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
80 canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
81 canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
82 canvas.restore();
83 }
84 }
85 }
86
87 bool fIsSmall;
88 bool fInitialized;
89 SkBitmap fBitmap, fCheckerboard;
90
tfarinaf168b862014-06-19 12:32:29 -070091 typedef Benchmark INHERITED;
sugoi@google.com580a1722013-04-23 14:20:45 +000092};
93
94///////////////////////////////////////////////////////////////////////////////
95
mtklein@google.com410e6e82013-09-13 19:52:27 +000096DEF_BENCH( return new MergeBench(true); )
97DEF_BENCH( return new MergeBench(false); )