blob: 19f81f3165d6b16ec140273927be68b4c1824a3e [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "bench/Benchmark.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkFont.h"
11#include "include/core/SkSurface.h"
Michael Ludwig23003182019-08-05 11:25:23 -040012#include "include/effects/SkImageFilters.h"
sugoi@google.com580a1722013-04-23 14:20:45 +000013
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
robertphillips2238c9d2016-03-30 13:34:16 -070019static sk_sp<SkImage> make_bitmap() {
20 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(80, 80));
21 surface->getCanvas()->clear(0x00000000);
22 SkPaint paint;
robertphillips2238c9d2016-03-30 13:34:16 -070023 paint.setColor(0xFF884422);
Mike Reed89126e42019-01-03 12:59:14 -050024 SkFont font;
25 font.setSize(SkIntToScalar(96));
Ben Wagner51e15a62019-05-07 15:38:46 -040026 surface->getCanvas()->drawSimpleText("g", 1, SkTextEncoding::kUTF8, 15, 55, font, paint);
robertphillips2238c9d2016-03-30 13:34:16 -070027 return surface->makeImageSnapshot();
28}
29
30static 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
tfarinaf168b862014-06-19 12:32:29 -070053class MergeBench : public Benchmark {
sugoi@google.com580a1722013-04-23 14:20:45 +000054public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000055 MergeBench(bool small) : fIsSmall(small), fInitialized(false) { }
sugoi@google.com580a1722013-04-23 14:20:45 +000056
57protected:
mtklein36352bf2015-03-25 18:17:31 -070058 const char* onGetName() override {
sugoi@google.com580a1722013-04-23 14:20:45 +000059 return fIsSmall ? "merge_small" : "merge_large";
60 }
61
joshualitt8a6697a2015-09-30 12:11:07 -070062 void onDelayedSetup() override {
sugoi@google.com580a1722013-04-23 14:20:45 +000063 if (!fInitialized) {
robertphillips2238c9d2016-03-30 13:34:16 -070064 fImage = make_bitmap();
65 fCheckerboard = make_checkerboard();
sugoi@google.com580a1722013-04-23 14:20:45 +000066 fInitialized = true;
67 }
68 }
69
mtkleina1ebeb22015-10-01 09:43:39 -070070 void onDraw(int loops, SkCanvas* canvas) override {
sugoi@google.com580a1722013-04-23 14:20:45 +000071 SkRect r = fIsSmall ? SkRect::MakeWH(FILTER_WIDTH_SMALL, FILTER_HEIGHT_SMALL) :
72 SkRect::MakeWH(FILTER_WIDTH_LARGE, FILTER_HEIGHT_LARGE);
73 SkPaint paint;
Michael Ludwig23003182019-08-05 11:25:23 -040074 paint.setImageFilter(SkImageFilters::Merge(SkImageFilters::Image(fCheckerboard),
75 SkImageFilters::Image(fImage)));
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:
sugoi@google.com580a1722013-04-23 14:20:45 +000082 bool fIsSmall;
83 bool fInitialized;
reed9ce9d672016-03-17 10:51:11 -070084 sk_sp<SkImage> fImage, fCheckerboard;
sugoi@google.com580a1722013-04-23 14:20:45 +000085
John Stiles7571f9e2020-09-02 22:42:33 -040086 using INHERITED = Benchmark;
sugoi@google.com580a1722013-04-23 14:20:45 +000087};
88
89///////////////////////////////////////////////////////////////////////////////
90
mtklein@google.com410e6e82013-09-13 19:52:27 +000091DEF_BENCH( return new MergeBench(true); )
92DEF_BENCH( return new MergeBench(false); )