Flutter Engine
The Flutter Engine
MorphologyBench.cpp
Go to the documentation of this file.
1/*
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 */
7
8#include "bench/Benchmark.h"
14#include "src/base/SkRandom.h"
15
16#define SMALL SkIntToScalar(2)
17#define REAL 1.5f
18#define BIG SkIntToScalar(10)
19
23};
24
25static const char* gStyleName[] = {
26 "erode",
27 "dilate"
28};
29
30class MorphologyBench : public Benchmark {
31 SkScalar fRadius;
32 MorphologyType fStyle;
33 SkString fName;
34
35public:
37 fRadius = rad;
38 fStyle = style;
39 const char* name = rad > 0 ? gStyleName[style] : "none";
40 if (SkScalarFraction(rad) != 0) {
41 fName.printf("morph_%.2f_%s", rad, name);
42 } else {
43 fName.printf("morph_%d_%s", SkScalarRoundToInt(rad), name);
44 }
45 }
46
47protected:
48 const char* onGetName() override {
49 return fName.c_str();
50 }
51
52 void onDraw(int loops, SkCanvas* canvas) override {
54 this->setupPaint(&paint);
55
56 paint.setAntiAlias(true);
57
58 SkRandom rand;
59 for (int i = 0; i < loops; i++) {
60 SkRect r = SkRect::MakeWH(rand.nextUScalar1() * 400,
61 rand.nextUScalar1() * 400);
62 r.offset(fRadius, fRadius);
63
64 if (fRadius > 0) {
66 switch (fStyle) {
67 case kDilate_MT:
69 SkScalarFloorToInt(fRadius), SkScalarFloorToInt(fRadius), nullptr);
70 break;
71 case kErode_MT:
73 SkScalarFloorToInt(fRadius), SkScalarFloorToInt(fRadius), nullptr);
74 break;
75 }
76 paint.setImageFilter(std::move(mf));
77 }
78 canvas->drawOval(r, paint);
79 }
80 }
81
82private:
83 using INHERITED = Benchmark;
84};
85
88
91
94
95DEF_BENCH( return new MorphologyBench(0, kErode_MT); )
SkStrokeRec::Style fStyle
#define DEF_BENCH(code)
Definition: Benchmark.h:20
const char * fName
#define SMALL
#define BIG
#define REAL
static const char * gStyleName[]
MorphologyType
@ kErode_MT
@ kDilate_MT
#define SkScalarRoundToInt(x)
Definition: SkScalar.h:37
#define SkScalarFloorToInt(x)
Definition: SkScalar.h:35
static SkScalar SkScalarFraction(SkScalar x)
Definition: SkScalar.h:67
virtual void setupPaint(SkPaint *paint)
Definition: Benchmark.cpp:55
MorphologyBench(SkScalar rad, MorphologyType style)
void onDraw(int loops, SkCanvas *canvas) override
const char * onGetName() override
void drawOval(const SkRect &oval, const SkPaint &paint)
Definition: SkCanvas.cpp:1698
static sk_sp< SkImageFilter > Erode(SkScalar radiusX, SkScalar radiusY, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Dilate(SkScalar radiusX, SkScalar radiusY, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
SkScalar nextUScalar1()
Definition: SkRandom.h:101
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
void offset(float dx, float dy)
Definition: SkRect.h:1016
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609