From ff2f423d90147dba710186ec4b63de75da7be8ed Mon Sep 17 00:00:00 2001 From: reed Date: Fri, 29 May 2015 07:17:16 -0700 Subject: add bench for building strokes BUG=skia: Review URL: https://codereview.chromium.org/1160093004 --- bench/StrokeBench.cpp | 161 +++++++++++++++++++++++++------------------------- 1 file changed, 81 insertions(+), 80 deletions(-) (limited to 'bench/StrokeBench.cpp') diff --git a/bench/StrokeBench.cpp b/bench/StrokeBench.cpp index 24e0cabbb0..7844035bcc 100644 --- a/bench/StrokeBench.cpp +++ b/bench/StrokeBench.cpp @@ -1,104 +1,105 @@ /* - * Copyright 2013 Google Inc. + * Copyright 2015 Google Inc. * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include "Benchmark.h" -#include "SkCanvas.h" #include "SkPaint.h" -#include "SkRRect.h" +#include "SkPath.h" +#include "SkRandom.h" #include "SkString.h" -struct RRectRec { - SkCanvas* fCanvas; - SkRRect fRRect; - SkPaint fPaint; -}; +class StrokeBench : public Benchmark { +public: + StrokeBench(const SkPath& path, const SkPaint& paint, const char pathType[]) + : fPath(path), fPaint(paint) + { + fName.printf("build_stroke_%s_%g_%d_%d", + pathType, paint.getStrokeWidth(), paint.getStrokeJoin(), paint.getStrokeCap()); + } -typedef const char* (*DrawProc)(const RRectRec*, int); +protected: + const char* onGetName() override { return fName.c_str(); } -static const char* draw_rect(const RRectRec* rec, int count) { - if (rec) { - const SkRect& r = rec->fRRect.getBounds(); - for (int i = 0; i < count; ++i) { - rec->fCanvas->drawRect(r, rec->fPaint); - } - } - return "rect"; -} + void onDraw(const int loops, SkCanvas* canvas) override { + SkPaint paint(fPaint); + this->setupPaint(&paint); -static const char* draw_rrect(const RRectRec* rec, int count) { - if (rec) { - for (int i = 0; i < count; ++i) { - rec->fCanvas->drawRRect(rec->fRRect, rec->fPaint); + for (int outer = 0; outer < 10; ++outer) { + for (int i = 0; i < loops; ++i) { + SkPath result; + paint.getFillPath(fPath, &result); + } } } - return "rrect"; -} -static const char* draw_oval(const RRectRec* rec, int count) { - if (rec) { - const SkRect& r = rec->fRRect.getBounds(); - for (int i = 0; i < count; ++i) { - rec->fCanvas->drawOval(r, rec->fPaint); - } - } - return "oval"; -} +private: + SkPath fPath; + SkPaint fPaint; + SkString fName; -// Handles rect, rrect, and oval -// -// Test drawing a small stroked version to see the effect of special-casing -// our stroke code for these convex single-contour shapes. -// -class StrokeRRectBench : public Benchmark { - SkString fName; - SkPaint::Join fJoin; - RRectRec fRec; - DrawProc fProc; -public: - StrokeRRectBench(SkPaint::Join j, DrawProc proc) { - static const char* gJoinName[] = { - "miter", "round", "bevel" - }; + typedef Benchmark INHERITED; +}; - fJoin = j; - fProc = proc; - fName.printf("draw_stroke_%s_%s", proc(NULL, 0), gJoinName[j]); +/////////////////////////////////////////////////////////////////////////////// - SkRect r = { 20, 20, 40, 40 }; - SkScalar rad = 4; - fRec.fRRect.setRectXY(r, rad, rad); - } +static const int N = 100; +static const SkScalar X = 100; +static const SkScalar Y = 100; -protected: - virtual const char* onGetName() { - return fName.c_str(); - } +static SkPoint rand_pt(SkRandom& rand) { + return SkPoint::Make(rand.nextSScalar1() * X, rand.nextSScalar1() * Y); +} - virtual void onDraw(const int loops, SkCanvas* canvas) { - fRec.fCanvas = canvas; - this->setupPaint(&fRec.fPaint); - fRec.fPaint.setStyle(SkPaint::kStroke_Style); - fRec.fPaint.setStrokeJoin(fJoin); - fRec.fPaint.setStrokeWidth(5); - fProc(&fRec, loops); +static SkPath line_path_maker() { + SkPath path; + SkRandom rand; + path.moveTo(rand_pt(rand)); + for (int i = 0; i < N; ++i) { + path.lineTo(rand_pt(rand)); } + return path; +} +static SkPath quad_path_maker() { + SkPath path; + SkRandom rand; + path.moveTo(rand_pt(rand)); + for (int i = 0; i < N; ++i) { + path.quadTo(rand_pt(rand), rand_pt(rand)); + } + return path; +} +static SkPath conic_path_maker() { + SkPath path; + SkRandom rand; + path.moveTo(rand_pt(rand)); + for (int i = 0; i < N; ++i) { + path.conicTo(rand_pt(rand), rand_pt(rand), rand.nextUScalar1()); + } + return path; +} +static SkPath cubic_path_maker() { + SkPath path; + SkRandom rand; + path.moveTo(rand_pt(rand)); + for (int i = 0; i < N; ++i) { + path.cubicTo(rand_pt(rand), rand_pt(rand), rand_pt(rand)); + } + return path; +} -private: - typedef Benchmark INHERITED; -}; - -DEF_BENCH( return new StrokeRRectBench(SkPaint::kRound_Join, draw_rect); ) -DEF_BENCH( return new StrokeRRectBench(SkPaint::kBevel_Join, draw_rect); ) -DEF_BENCH( return new StrokeRRectBench(SkPaint::kMiter_Join, draw_rect); ) - -DEF_BENCH( return new StrokeRRectBench(SkPaint::kRound_Join, draw_rrect); ) -DEF_BENCH( return new StrokeRRectBench(SkPaint::kBevel_Join, draw_rrect); ) -DEF_BENCH( return new StrokeRRectBench(SkPaint::kMiter_Join, draw_rrect); ) +static SkPaint paint_maker() { + SkPaint paint; + paint.setStyle(SkPaint::kStroke_Style); + paint.setStrokeWidth(X / 10); + paint.setStrokeJoin(SkPaint::kMiter_Join); + paint.setStrokeCap(SkPaint::kSquare_Cap); + return paint; +} -DEF_BENCH( return new StrokeRRectBench(SkPaint::kRound_Join, draw_oval); ) -DEF_BENCH( return new StrokeRRectBench(SkPaint::kBevel_Join, draw_oval); ) -DEF_BENCH( return new StrokeRRectBench(SkPaint::kMiter_Join, draw_oval); ) +DEF_BENCH( return SkNEW_ARGS(StrokeBench, (line_path_maker(), paint_maker(), "line")); ) +DEF_BENCH( return SkNEW_ARGS(StrokeBench, (quad_path_maker(), paint_maker(), "quad")); ) +DEF_BENCH( return SkNEW_ARGS(StrokeBench, (conic_path_maker(), paint_maker(), "conic")); ) +DEF_BENCH( return SkNEW_ARGS(StrokeBench, (cubic_path_maker(), paint_maker(), "cubic")); ) -- cgit v1.2.3