blob: 5edae6e299525e4078b3a41087b1bb2f55f8ba17 [file] [log] [blame]
tomhudson@google.comf2e91a32012-06-11 19:22:01 +00001/*
2 * Copyright 2012 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 */
commit-bot@chromium.org97b4b672013-09-26 19:23:03 +00007
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
tomhudson@google.comf2e91a32012-06-11 19:22:01 +00009#include "SkCanvas.h"
tfarinaf168b862014-06-19 12:32:29 -070010#include "SkMorphologyImageFilter.h"
tomhudson@google.comf2e91a32012-06-11 19:22:01 +000011#include "SkPaint.h"
12#include "SkRandom.h"
13#include "SkShader.h"
14#include "SkString.h"
tomhudson@google.comf2e91a32012-06-11 19:22:01 +000015
16#define SMALL SkIntToScalar(2)
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000017#define REAL 1.5f
tomhudson@google.comf2e91a32012-06-11 19:22:01 +000018#define BIG SkIntToScalar(10)
19
tomhudson@google.comf2e91a32012-06-11 19:22:01 +000020enum MorphologyType {
21 kErode_MT,
22 kDilate_MT
23};
24
tomhudson@google.comf2e91a32012-06-11 19:22:01 +000025static const char* gStyleName[] = {
26 "erode",
27 "dilate"
28};
29
tfarinaf168b862014-06-19 12:32:29 -070030class MorphologyBench : public Benchmark {
tomhudson@google.comf2e91a32012-06-11 19:22:01 +000031 SkScalar fRadius;
32 MorphologyType fStyle;
33 SkString fName;
34
35public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000036 MorphologyBench(SkScalar rad, MorphologyType style)
37 {
tomhudson@google.comf2e91a32012-06-11 19:22:01 +000038 fRadius = rad;
39 fStyle = style;
40 const char* name = rad > 0 ? gStyleName[style] : "none";
41 if (SkScalarFraction(rad) != 0) {
42 fName.printf("morph_%.2f_%s", SkScalarToFloat(rad), name);
43 } else {
reed@google.come1ca7052013-12-17 19:22:07 +000044 fName.printf("morph_%d_%s", SkScalarRoundToInt(rad), name);
tomhudson@google.comf2e91a32012-06-11 19:22:01 +000045 }
46 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000047
tomhudson@google.comf2e91a32012-06-11 19:22:01 +000048protected:
robertphillipsfc11b0a2016-04-05 09:09:36 -070049 const char* onGetName() override {
tomhudson@google.comf2e91a32012-06-11 19:22:01 +000050 return fName.c_str();
51 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000052
robertphillipsfc11b0a2016-04-05 09:09:36 -070053 void onDraw(int loops, SkCanvas* canvas) override {
tomhudson@google.comf2e91a32012-06-11 19:22:01 +000054 SkPaint paint;
55 this->setupPaint(&paint);
56
57 paint.setAntiAlias(true);
58
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000059 SkRandom rand;
commit-bot@chromium.org33614712013-12-03 18:17:16 +000060 for (int i = 0; i < loops; i++) {
tomhudson@google.comf2e91a32012-06-11 19:22:01 +000061 SkRect r = SkRect::MakeWH(rand.nextUScalar1() * 400,
62 rand.nextUScalar1() * 400);
63 r.offset(fRadius, fRadius);
64
65 if (fRadius > 0) {
robertphillipsfc11b0a2016-04-05 09:09:36 -070066 sk_sp<SkImageFilter> mf;
tomhudson@google.comf2e91a32012-06-11 19:22:01 +000067 switch (fStyle) {
68 case kDilate_MT:
robertphillipsfc11b0a2016-04-05 09:09:36 -070069 mf = SkDilateImageFilter::Make(SkScalarFloorToInt(fRadius),
70 SkScalarFloorToInt(fRadius),
71 nullptr);
tomhudson@google.comf2e91a32012-06-11 19:22:01 +000072 break;
73 case kErode_MT:
robertphillipsfc11b0a2016-04-05 09:09:36 -070074 mf = SkErodeImageFilter::Make(SkScalarFloorToInt(fRadius),
75 SkScalarFloorToInt(fRadius),
76 nullptr);
tomhudson@google.comf2e91a32012-06-11 19:22:01 +000077 break;
78 }
robertphillipsfc11b0a2016-04-05 09:09:36 -070079 paint.setImageFilter(std::move(mf));
tomhudson@google.comf2e91a32012-06-11 19:22:01 +000080 }
81 canvas->drawOval(r, paint);
82 }
83 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000084
tomhudson@google.comf2e91a32012-06-11 19:22:01 +000085private:
tfarinaf168b862014-06-19 12:32:29 -070086 typedef Benchmark INHERITED;
tomhudson@google.comf2e91a32012-06-11 19:22:01 +000087};
88
mtklein@google.com410e6e82013-09-13 19:52:27 +000089DEF_BENCH( return new MorphologyBench(SMALL, kErode_MT); )
90DEF_BENCH( return new MorphologyBench(SMALL, kDilate_MT); )
tomhudson@google.coma0116d52012-06-11 20:48:18 +000091
mtklein@google.com410e6e82013-09-13 19:52:27 +000092DEF_BENCH( return new MorphologyBench(BIG, kErode_MT); )
93DEF_BENCH( return new MorphologyBench(BIG, kDilate_MT); )
tomhudson@google.comf2e91a32012-06-11 19:22:01 +000094
mtklein@google.com410e6e82013-09-13 19:52:27 +000095DEF_BENCH( return new MorphologyBench(REAL, kErode_MT); )
96DEF_BENCH( return new MorphologyBench(REAL, kDilate_MT); )
tomhudson@google.comf2e91a32012-06-11 19:22:01 +000097
mtklein@google.com410e6e82013-09-13 19:52:27 +000098DEF_BENCH( return new MorphologyBench(0, kErode_MT); )