1f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com/*
2f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com * Copyright 2012 Google Inc.
3f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com *
4f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com * Use of this source code is governed by a BSD-style license that can be
5f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com * found in the LICENSE file.
6f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com */
797b4b67ee79b094ea6ec84071d4a233177f9c7bccommit-bot@chromium.org
8f168b86d7fafc5c20c87bebc6fd393cb17e120catfarina#include "Benchmark.h"
9f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com#include "SkCanvas.h"
10f168b86d7fafc5c20c87bebc6fd393cb17e120catfarina#include "SkMorphologyImageFilter.h"
11f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com#include "SkPaint.h"
12f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com#include "SkRandom.h"
13f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com#include "SkShader.h"
14f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com#include "SkString.h"
15f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com
16f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com#define SMALL   SkIntToScalar(2)
174b413c8bb123e42ca4b9c7bfa6bc2167283cb84ccommit-bot@chromium.org#define REAL    1.5f
18f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com#define BIG     SkIntToScalar(10)
19f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com
20f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.comenum MorphologyType {
21f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com    kErode_MT,
22f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com    kDilate_MT
23f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com};
24f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com
25f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.comstatic const char* gStyleName[] = {
26f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com    "erode",
27f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com    "dilate"
28f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com};
29f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com
30f168b86d7fafc5c20c87bebc6fd393cb17e120catfarinaclass MorphologyBench : public Benchmark {
31f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com    SkScalar       fRadius;
32f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com    MorphologyType fStyle;
33f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com    SkString       fName;
34f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com
35f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.compublic:
36410e6e80f00a6c660675c80904807a041c7b7d2amtklein@google.com    MorphologyBench(SkScalar rad, MorphologyType style)
37410e6e80f00a6c660675c80904807a041c7b7d2amtklein@google.com         {
38f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com        fRadius = rad;
39f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com        fStyle = style;
40f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com        const char* name = rad > 0 ? gStyleName[style] : "none";
41f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com        if (SkScalarFraction(rad) != 0) {
42f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com            fName.printf("morph_%.2f_%s", SkScalarToFloat(rad), name);
43f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com        } else {
44e1ca705cac4b946993f6cbf798e2a0ba27e739f3reed@google.com            fName.printf("morph_%d_%s", SkScalarRoundToInt(rad), name);
45f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com        }
46f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com    }
47fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
48f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.comprotected:
49f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com    virtual const char* onGetName() {
50f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com        return fName.c_str();
51f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com    }
52fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
533361471a3504ecd0351ff70f4c42d8d6fee963d4commit-bot@chromium.org    virtual void onDraw(const int loops, SkCanvas* canvas) {
54f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com        SkPaint paint;
55f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com        this->setupPaint(&paint);
56f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com
57f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com        paint.setAntiAlias(true);
58f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com
59e0e7cfe44bb9d66d76120a79e5275c294bacaa22commit-bot@chromium.org        SkRandom rand;
603361471a3504ecd0351ff70f4c42d8d6fee963d4commit-bot@chromium.org        for (int i = 0; i < loops; i++) {
61f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com            SkRect r = SkRect::MakeWH(rand.nextUScalar1() * 400,
62f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com                                      rand.nextUScalar1() * 400);
63f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com            r.offset(fRadius, fRadius);
64f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com
65f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com            if (fRadius > 0) {
66f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com                SkMorphologyImageFilter* mf = NULL;
67f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com                switch (fStyle) {
68f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com                case kDilate_MT:
69cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                    mf = SkDilateImageFilter::Create(SkScalarFloorToInt(fRadius),
70cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                                                    SkScalarFloorToInt(fRadius));
71f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com                    break;
72f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com                case kErode_MT:
73cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                    mf = SkErodeImageFilter::Create(SkScalarFloorToInt(fRadius),
74cac5fd597f6e2495f50aaa6bcbe3dadc56f0b977commit-bot@chromium.org                                                    SkScalarFloorToInt(fRadius));
75f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com                    break;
76f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com                }
77f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com                paint.setImageFilter(mf)->unref();
78f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com            }
79f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com            canvas->drawOval(r, paint);
80f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com        }
81f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com    }
82fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
83f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.comprivate:
84f168b86d7fafc5c20c87bebc6fd393cb17e120catfarina    typedef Benchmark INHERITED;
85f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com};
86f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com
87410e6e80f00a6c660675c80904807a041c7b7d2amtklein@google.comDEF_BENCH( return new MorphologyBench(SMALL, kErode_MT); )
88410e6e80f00a6c660675c80904807a041c7b7d2amtklein@google.comDEF_BENCH( return new MorphologyBench(SMALL, kDilate_MT); )
89a0116d541d6fee722cc159b99de6867c9ee379a5tomhudson@google.com
90410e6e80f00a6c660675c80904807a041c7b7d2amtklein@google.comDEF_BENCH( return new MorphologyBench(BIG, kErode_MT); )
91410e6e80f00a6c660675c80904807a041c7b7d2amtklein@google.comDEF_BENCH( return new MorphologyBench(BIG, kDilate_MT); )
92f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com
93410e6e80f00a6c660675c80904807a041c7b7d2amtklein@google.comDEF_BENCH( return new MorphologyBench(REAL, kErode_MT); )
94410e6e80f00a6c660675c80904807a041c7b7d2amtklein@google.comDEF_BENCH( return new MorphologyBench(REAL, kDilate_MT); )
95f2e91a3907a544b838d7ad214d020a3ab15a9889tomhudson@google.com
96410e6e80f00a6c660675c80904807a041c7b7d2amtklein@google.comDEF_BENCH( return new MorphologyBench(0, kErode_MT); )
97