blob: 925b6b2bfe869070070f8f0d488b10b5123cc270 [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"
fmalita5598b632015-09-15 11:26:13 -070010#include "SkImageSource.h"
sugoi@google.com580a1722013-04-23 14:20:45 +000011#include "SkMergeImageFilter.h"
fmalita5598b632015-09-15 11:26:13 -070012#include "SkSurface.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
tfarinaf168b862014-06-19 12:32:29 -070019class MergeBench : public Benchmark {
sugoi@google.com580a1722013-04-23 14:20:45 +000020public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000021 MergeBench(bool small) : fIsSmall(small), fInitialized(false) { }
sugoi@google.com580a1722013-04-23 14:20:45 +000022
23protected:
mtklein36352bf2015-03-25 18:17:31 -070024 const char* onGetName() override {
sugoi@google.com580a1722013-04-23 14:20:45 +000025 return fIsSmall ? "merge_small" : "merge_large";
26 }
27
joshualitt8a6697a2015-09-30 12:11:07 -070028 void onDelayedSetup() override {
sugoi@google.com580a1722013-04-23 14:20:45 +000029 if (!fInitialized) {
30 make_bitmap();
31 make_checkerboard();
32 fInitialized = true;
33 }
34 }
35
mtkleina1ebeb22015-10-01 09:43:39 -070036 void onDraw(int loops, SkCanvas* canvas) override {
sugoi@google.com580a1722013-04-23 14:20:45 +000037 SkRect r = fIsSmall ? SkRect::MakeWH(FILTER_WIDTH_SMALL, FILTER_HEIGHT_SMALL) :
38 SkRect::MakeWH(FILTER_WIDTH_LARGE, FILTER_HEIGHT_LARGE);
39 SkPaint paint;
40 paint.setImageFilter(mergeBitmaps())->unref();
commit-bot@chromium.org33614712013-12-03 18:17:16 +000041 for (int i = 0; i < loops; i++) {
mtklein@google.comc2897432013-09-10 19:23:38 +000042 canvas->drawRect(r, paint);
43 }
sugoi@google.com580a1722013-04-23 14:20:45 +000044 }
45
46private:
47 SkImageFilter* mergeBitmaps() {
fmalita5598b632015-09-15 11:26:13 -070048 SkAutoTUnref<SkImageFilter> first(SkImageSource::Create(fCheckerboard));
49 SkAutoTUnref<SkImageFilter> second(SkImageSource::Create(fImage));
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000050 return SkMergeImageFilter::Create(first, second);
sugoi@google.com580a1722013-04-23 14:20:45 +000051 }
52
53 void make_bitmap() {
fmalita5598b632015-09-15 11:26:13 -070054 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(80, 80));
55 surface->getCanvas()->clear(0x00000000);
sugoi@google.com580a1722013-04-23 14:20:45 +000056 SkPaint paint;
57 paint.setAntiAlias(true);
58 paint.setColor(0xFF884422);
59 paint.setTextSize(SkIntToScalar(96));
60 const char* str = "g";
fmalita5598b632015-09-15 11:26:13 -070061 surface->getCanvas()->drawText(str, strlen(str), 15, 55, paint);
62 fImage.reset(surface->newImageSnapshot());
sugoi@google.com580a1722013-04-23 14:20:45 +000063 }
64
65 void make_checkerboard() {
fmalita5598b632015-09-15 11:26:13 -070066 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(80, 80));
67 SkCanvas* canvas = surface->getCanvas();
68 canvas->clear(0x00000000);
sugoi@google.com580a1722013-04-23 14:20:45 +000069 SkPaint darkPaint;
70 darkPaint.setColor(0xFF804020);
71 SkPaint lightPaint;
72 lightPaint.setColor(0xFF244484);
73 for (int y = 0; y < 80; y += 16) {
74 for (int x = 0; x < 80; x += 16) {
fmalita5598b632015-09-15 11:26:13 -070075 canvas->save();
76 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
77 canvas->drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
78 canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
79 canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
80 canvas->drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
81 canvas->restore();
sugoi@google.com580a1722013-04-23 14:20:45 +000082 }
83 }
fmalita5598b632015-09-15 11:26:13 -070084
85 fCheckerboard.reset(surface->newImageSnapshot());
sugoi@google.com580a1722013-04-23 14:20:45 +000086 }
87
88 bool fIsSmall;
89 bool fInitialized;
fmalita5598b632015-09-15 11:26:13 -070090 SkAutoTUnref<SkImage> fImage, fCheckerboard;
sugoi@google.com580a1722013-04-23 14:20:45 +000091
tfarinaf168b862014-06-19 12:32:29 -070092 typedef Benchmark INHERITED;
sugoi@google.com580a1722013-04-23 14:20:45 +000093};
94
95///////////////////////////////////////////////////////////////////////////////
96
mtklein@google.com410e6e82013-09-13 19:52:27 +000097DEF_BENCH( return new MergeBench(true); )
98DEF_BENCH( return new MergeBench(false); )