aboutsummaryrefslogtreecommitdiffhomepage
path: root/bench/PathTextBench.cpp
diff options
context:
space:
mode:
authorGravatar Chris Dalton <csmartdalton@google.com>2017-06-30 12:00:35 -0600
committerGravatar Skia Commit-Bot <skia-commit-bot@chromium.org>2017-07-05 15:38:22 +0000
commitcb72722cb929b8230d337238d748c080df184a46 (patch)
tree231e63e79d3928ea60490ab6f7068d61c54c207b /bench/PathTextBench.cpp
parent466c7d65974a055ad361430e57f92584b09af0bd (diff)
Add a nanobench for drawing glyphs as paths
Bug: skia: Change-Id: I6e620befd2fe8e84f744e79e9287d9d2d19ffa57 Reviewed-on: https://skia-review.googlesource.com/21389 Reviewed-by: Greg Daniel <egdaniel@google.com> Commit-Queue: Chris Dalton <csmartdalton@google.com>
Diffstat (limited to 'bench/PathTextBench.cpp')
-rw-r--r--bench/PathTextBench.cpp86
1 files changed, 86 insertions, 0 deletions
diff --git a/bench/PathTextBench.cpp b/bench/PathTextBench.cpp
new file mode 100644
index 0000000000..6ee8f8879c
--- /dev/null
+++ b/bench/PathTextBench.cpp
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2017 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 "SkGlyphCache.h"
+#include "SkPaint.h"
+#include "SkPath.h"
+#include "SkRandom.h"
+
+static constexpr int kScreenWidth = 1500;
+static constexpr int kScreenHeight = 1500;
+
+static constexpr int kNumDraws = 2000;
+
+// I and l are rects on OS X.
+static constexpr char kGlyphs[] = "ABCDEFGH7JKLMNOPQRSTUVWXYZabcdefghijk1mnopqrstuvwxyz";
+static constexpr int kNumGlyphs = sizeof(kGlyphs) - 1;
+static_assert(52 == kNumGlyphs, "expected 52 glyphs");
+
+/*
+ * This class benchmarks drawing many glyphs at random scales and rotations.
+ */
+class PathTextBench : public Benchmark {
+public:
+ bool isVisual() override { return true; }
+
+private:
+ const char* onGetName() override {
+ return "path_text";
+ }
+ SkIPoint onGetSize() override { return SkIPoint::Make(kScreenWidth, kScreenHeight); }
+
+ void onDelayedSetup() override {
+ SkPaint defaultPaint;
+ SkAutoGlyphCache agc(defaultPaint, nullptr, &SkMatrix::I());
+ SkGlyphCache* cache = agc.getCache();
+ for (int i = 0; i < kNumGlyphs; ++i) {
+ SkGlyphID id = cache->unicharToGlyph(kGlyphs[i]);
+ cache->getScalerContext()->getPath(SkPackedGlyphID(id), &fGlyphs[i]);
+ }
+
+ SkRandom rand;
+ for (int i = 0; i < kNumDraws; ++i) {
+ const SkPath& glyph = fGlyphs[i % kNumGlyphs];
+ const SkRect& bounds = glyph.getBounds();
+ float glyphSize = SkTMax(bounds.width(), bounds.height());
+
+ float t0 = pow(rand.nextF(), 100);
+ float size = (1 - t0) * SkTMin(kScreenWidth, kScreenHeight) / 50 +
+ t0 * SkTMin(kScreenWidth, kScreenHeight) / 3;
+ float scale = size / glyphSize;
+ float t1 = rand.nextF(), t2 = rand.nextF();
+ fXforms[i].setTranslate((1 - t1) * sqrt(2) * scale/2 * glyphSize +
+ t1 * (kScreenWidth - sqrt(2) * scale/2 * glyphSize),
+ (1 - t2) * sqrt(2) * scale/2 * glyphSize +
+ t2 * (kScreenHeight - sqrt(2) * scale/2 * glyphSize));
+ fXforms[i].preRotate(rand.nextF() * 360);
+ fXforms[i].preTranslate(-scale/2 * bounds.width(), -scale/2 * bounds.height());
+ fXforms[i].preScale(scale, scale);
+ fPaints[i].setAntiAlias(true);
+ fPaints[i].setColor(rand.nextU() | 0x80808080);
+ }
+ }
+
+ void onDraw(int loops, SkCanvas* canvas) override {
+ SkAutoCanvasRestore acr(canvas, true);
+ for (int i = 0; i < kNumDraws; ++i) {
+ const SkPath& glyph = fGlyphs[i % kNumGlyphs];
+ canvas->setMatrix(fXforms[i]);
+ canvas->drawPath(glyph, fPaints[i]);
+ }
+ }
+
+ SkPath fGlyphs[kNumGlyphs];
+ SkPaint fPaints[kNumDraws];
+ SkMatrix fXforms[kNumDraws];
+
+ typedef Benchmark INHERITED;
+};
+
+DEF_BENCH(return new PathTextBench;)