2012-06-11 19:22:01 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2013-09-26 19:23:03 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "bench/Benchmark.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/effects/SkMorphologyImageFilter.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
2012-06-11 19:22:01 +00:00
|
|
|
|
|
|
|
#define SMALL SkIntToScalar(2)
|
2013-11-25 19:44:07 +00:00
|
|
|
#define REAL 1.5f
|
2012-06-11 19:22:01 +00:00
|
|
|
#define BIG SkIntToScalar(10)
|
|
|
|
|
|
|
|
enum MorphologyType {
|
|
|
|
kErode_MT,
|
|
|
|
kDilate_MT
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char* gStyleName[] = {
|
|
|
|
"erode",
|
|
|
|
"dilate"
|
|
|
|
};
|
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
class MorphologyBench : public Benchmark {
|
2012-06-11 19:22:01 +00:00
|
|
|
SkScalar fRadius;
|
|
|
|
MorphologyType fStyle;
|
|
|
|
SkString fName;
|
|
|
|
|
|
|
|
public:
|
2013-09-13 19:52:27 +00:00
|
|
|
MorphologyBench(SkScalar rad, MorphologyType style)
|
|
|
|
{
|
2012-06-11 19:22:01 +00:00
|
|
|
fRadius = rad;
|
|
|
|
fStyle = style;
|
|
|
|
const char* name = rad > 0 ? gStyleName[style] : "none";
|
|
|
|
if (SkScalarFraction(rad) != 0) {
|
|
|
|
fName.printf("morph_%.2f_%s", SkScalarToFloat(rad), name);
|
|
|
|
} else {
|
2013-12-17 19:22:07 +00:00
|
|
|
fName.printf("morph_%d_%s", SkScalarRoundToInt(rad), name);
|
2012-06-11 19:22:01 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2012-06-11 19:22:01 +00:00
|
|
|
protected:
|
2016-04-05 16:09:36 +00:00
|
|
|
const char* onGetName() override {
|
2012-06-11 19:22:01 +00:00
|
|
|
return fName.c_str();
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2016-04-05 16:09:36 +00:00
|
|
|
void onDraw(int loops, SkCanvas* canvas) override {
|
2012-06-11 19:22:01 +00:00
|
|
|
SkPaint paint;
|
|
|
|
this->setupPaint(&paint);
|
|
|
|
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2013-12-03 18:17:16 +00:00
|
|
|
for (int i = 0; i < loops; i++) {
|
2012-06-11 19:22:01 +00:00
|
|
|
SkRect r = SkRect::MakeWH(rand.nextUScalar1() * 400,
|
|
|
|
rand.nextUScalar1() * 400);
|
|
|
|
r.offset(fRadius, fRadius);
|
|
|
|
|
|
|
|
if (fRadius > 0) {
|
2016-04-05 16:09:36 +00:00
|
|
|
sk_sp<SkImageFilter> mf;
|
2012-06-11 19:22:01 +00:00
|
|
|
switch (fStyle) {
|
|
|
|
case kDilate_MT:
|
2016-04-05 16:09:36 +00:00
|
|
|
mf = SkDilateImageFilter::Make(SkScalarFloorToInt(fRadius),
|
|
|
|
SkScalarFloorToInt(fRadius),
|
|
|
|
nullptr);
|
2012-06-11 19:22:01 +00:00
|
|
|
break;
|
|
|
|
case kErode_MT:
|
2016-04-05 16:09:36 +00:00
|
|
|
mf = SkErodeImageFilter::Make(SkScalarFloorToInt(fRadius),
|
|
|
|
SkScalarFloorToInt(fRadius),
|
|
|
|
nullptr);
|
2012-06-11 19:22:01 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-04-05 16:09:36 +00:00
|
|
|
paint.setImageFilter(std::move(mf));
|
2012-06-11 19:22:01 +00:00
|
|
|
}
|
|
|
|
canvas->drawOval(r, paint);
|
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2012-06-11 19:22:01 +00:00
|
|
|
private:
|
2014-06-19 19:32:29 +00:00
|
|
|
typedef Benchmark INHERITED;
|
2012-06-11 19:22:01 +00:00
|
|
|
};
|
|
|
|
|
2013-09-13 19:52:27 +00:00
|
|
|
DEF_BENCH( return new MorphologyBench(SMALL, kErode_MT); )
|
|
|
|
DEF_BENCH( return new MorphologyBench(SMALL, kDilate_MT); )
|
2012-06-11 20:48:18 +00:00
|
|
|
|
2013-09-13 19:52:27 +00:00
|
|
|
DEF_BENCH( return new MorphologyBench(BIG, kErode_MT); )
|
|
|
|
DEF_BENCH( return new MorphologyBench(BIG, kDilate_MT); )
|
2012-06-11 19:22:01 +00:00
|
|
|
|
2013-09-13 19:52:27 +00:00
|
|
|
DEF_BENCH( return new MorphologyBench(REAL, kErode_MT); )
|
|
|
|
DEF_BENCH( return new MorphologyBench(REAL, kDilate_MT); )
|
2012-06-11 19:22:01 +00:00
|
|
|
|
2013-09-13 19:52:27 +00:00
|
|
|
DEF_BENCH( return new MorphologyBench(0, kErode_MT); )
|