blob: dd0e07ce4b05b8db33f231c990444f311270e7a6 [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 "SkCanvas.h"
Mike Reed89126e42019-01-03 12:59:14 -050010#include "SkFont.h"
fmalita5598b632015-09-15 11:26:13 -070011#include "SkImageSource.h"
sugoi@google.com580a1722013-04-23 14:20:45 +000012#include "SkMergeImageFilter.h"
fmalita5598b632015-09-15 11:26:13 -070013#include "SkSurface.h"
sugoi@google.com580a1722013-04-23 14:20:45 +000014
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
robertphillips2238c9d2016-03-30 13:34:16 -070020static sk_sp<SkImage> make_bitmap() {
21 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(80, 80));
22 surface->getCanvas()->clear(0x00000000);
23 SkPaint paint;
robertphillips2238c9d2016-03-30 13:34:16 -070024 paint.setColor(0xFF884422);
Mike Reed89126e42019-01-03 12:59:14 -050025 SkFont font;
26 font.setSize(SkIntToScalar(96));
27 surface->getCanvas()->drawSimpleText("g", 1, kUTF8_SkTextEncoding, 15, 55, font, paint);
robertphillips2238c9d2016-03-30 13:34:16 -070028 return surface->makeImageSnapshot();
29}
30
31static 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
tfarinaf168b862014-06-19 12:32:29 -070054class MergeBench : public Benchmark {
sugoi@google.com580a1722013-04-23 14:20:45 +000055public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000056 MergeBench(bool small) : fIsSmall(small), fInitialized(false) { }
sugoi@google.com580a1722013-04-23 14:20:45 +000057
58protected:
mtklein36352bf2015-03-25 18:17:31 -070059 const char* onGetName() override {
sugoi@google.com580a1722013-04-23 14:20:45 +000060 return fIsSmall ? "merge_small" : "merge_large";
61 }
62
joshualitt8a6697a2015-09-30 12:11:07 -070063 void onDelayedSetup() override {
sugoi@google.com580a1722013-04-23 14:20:45 +000064 if (!fInitialized) {
robertphillips2238c9d2016-03-30 13:34:16 -070065 fImage = make_bitmap();
66 fCheckerboard = make_checkerboard();
sugoi@google.com580a1722013-04-23 14:20:45 +000067 fInitialized = true;
68 }
69 }
70
mtkleina1ebeb22015-10-01 09:43:39 -070071 void onDraw(int loops, SkCanvas* canvas) override {
sugoi@google.com580a1722013-04-23 14:20:45 +000072 SkRect r = fIsSmall ? SkRect::MakeWH(FILTER_WIDTH_SMALL, FILTER_HEIGHT_SMALL) :
73 SkRect::MakeWH(FILTER_WIDTH_LARGE, FILTER_HEIGHT_LARGE);
74 SkPaint paint;
robertphillips2238c9d2016-03-30 13:34:16 -070075 paint.setImageFilter(this->mergeBitmaps());
commit-bot@chromium.org33614712013-12-03 18:17:16 +000076 for (int i = 0; i < loops; i++) {
mtklein@google.comc2897432013-09-10 19:23:38 +000077 canvas->drawRect(r, paint);
78 }
sugoi@google.com580a1722013-04-23 14:20:45 +000079 }
80
81private:
robertphillips2238c9d2016-03-30 13:34:16 -070082 sk_sp<SkImageFilter> mergeBitmaps() {
robertphillips549c8992016-04-01 09:28:51 -070083 return SkMergeImageFilter::Make(SkImageSource::Make(fCheckerboard),
Mike Reed0bdaf052017-06-18 23:35:57 -040084 SkImageSource::Make(fImage));
sugoi@google.com580a1722013-04-23 14:20:45 +000085 }
86
87 bool fIsSmall;
88 bool fInitialized;
reed9ce9d672016-03-17 10:51:11 -070089 sk_sp<SkImage> fImage, fCheckerboard;
sugoi@google.com580a1722013-04-23 14:20:45 +000090
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); )