Chris Dalton | cb72722 | 2017-06-30 12:00:35 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "bench/Benchmark.h" |
| 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkPaint.h" |
| 11 | #include "include/core/SkPath.h" |
| 12 | #include "include/utils/SkRandom.h" |
| 13 | #include "src/core/SkStrike.h" |
| 14 | #include "src/core/SkStrikeCache.h" |
Herb Derby | baf6478 | 2019-04-17 18:01:04 -0400 | [diff] [blame] | 15 | #include "src/core/SkStrikeSpec.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "tools/ToolUtils.h" |
Chris Dalton | cb72722 | 2017-06-30 12:00:35 -0600 | [diff] [blame] | 17 | |
| 18 | static constexpr int kScreenWidth = 1500; |
| 19 | static constexpr int kScreenHeight = 1500; |
| 20 | |
| 21 | static constexpr int kNumDraws = 2000; |
| 22 | |
| 23 | // I and l are rects on OS X. |
| 24 | static constexpr char kGlyphs[] = "ABCDEFGH7JKLMNOPQRSTUVWXYZabcdefghijk1mnopqrstuvwxyz"; |
| 25 | static constexpr int kNumGlyphs = sizeof(kGlyphs) - 1; |
| 26 | static_assert(52 == kNumGlyphs, "expected 52 glyphs"); |
| 27 | |
| 28 | /* |
| 29 | * This class benchmarks drawing many glyphs at random scales and rotations. |
| 30 | */ |
| 31 | class PathTextBench : public Benchmark { |
| 32 | public: |
Chris Dalton | 7c02cc7 | 2017-11-06 14:10:54 -0700 | [diff] [blame] | 33 | PathTextBench(bool clipped, bool uncached) : fClipped(clipped), fUncached(uncached) {} |
Chris Dalton | cb72722 | 2017-06-30 12:00:35 -0600 | [diff] [blame] | 34 | |
| 35 | private: |
| 36 | const char* onGetName() override { |
Chris Dalton | 7c02cc7 | 2017-11-06 14:10:54 -0700 | [diff] [blame] | 37 | fName = "path_text"; |
| 38 | if (fClipped) { |
| 39 | fName.append("_clipped"); |
| 40 | } |
| 41 | if (fUncached) { |
| 42 | fName.append("_uncached"); |
| 43 | } |
| 44 | return fName.c_str(); |
Chris Dalton | cb72722 | 2017-06-30 12:00:35 -0600 | [diff] [blame] | 45 | } |
| 46 | SkIPoint onGetSize() override { return SkIPoint::Make(kScreenWidth, kScreenHeight); } |
| 47 | |
| 48 | void onDelayedSetup() override { |
Mike Reed | 32c6066 | 2018-11-28 10:28:07 -0500 | [diff] [blame] | 49 | SkFont defaultFont; |
Herb Derby | ac71827 | 2019-06-06 14:33:12 -0400 | [diff] [blame] | 50 | SkStrikeSpec strikeSpec = SkStrikeSpec::MakeWithNoDevice(defaultFont); |
Herb Derby | baf6478 | 2019-04-17 18:01:04 -0400 | [diff] [blame] | 51 | auto cache = strikeSpec.findOrCreateExclusiveStrike(); |
Chris Dalton | cb72722 | 2017-06-30 12:00:35 -0600 | [diff] [blame] | 52 | for (int i = 0; i < kNumGlyphs; ++i) { |
Mike Reed | fdb876d | 2019-02-01 09:55:20 -0500 | [diff] [blame] | 53 | SkPackedGlyphID id(defaultFont.unicharToGlyph(kGlyphs[i])); |
Ben Wagner | 5ddb308 | 2018-03-29 11:18:06 -0400 | [diff] [blame] | 54 | sk_ignore_unused_variable(cache->getScalerContext()->getPath(id, &fGlyphs[i])); |
Chris Dalton | 7c02cc7 | 2017-11-06 14:10:54 -0700 | [diff] [blame] | 55 | fGlyphs[i].setIsVolatile(fUncached); |
Chris Dalton | cb72722 | 2017-06-30 12:00:35 -0600 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | SkRandom rand; |
| 59 | for (int i = 0; i < kNumDraws; ++i) { |
| 60 | const SkPath& glyph = fGlyphs[i % kNumGlyphs]; |
| 61 | const SkRect& bounds = glyph.getBounds(); |
| 62 | float glyphSize = SkTMax(bounds.width(), bounds.height()); |
| 63 | |
| 64 | float t0 = pow(rand.nextF(), 100); |
| 65 | float size = (1 - t0) * SkTMin(kScreenWidth, kScreenHeight) / 50 + |
| 66 | t0 * SkTMin(kScreenWidth, kScreenHeight) / 3; |
| 67 | float scale = size / glyphSize; |
| 68 | float t1 = rand.nextF(), t2 = rand.nextF(); |
| 69 | fXforms[i].setTranslate((1 - t1) * sqrt(2) * scale/2 * glyphSize + |
| 70 | t1 * (kScreenWidth - sqrt(2) * scale/2 * glyphSize), |
| 71 | (1 - t2) * sqrt(2) * scale/2 * glyphSize + |
| 72 | t2 * (kScreenHeight - sqrt(2) * scale/2 * glyphSize)); |
| 73 | fXforms[i].preRotate(rand.nextF() * 360); |
| 74 | fXforms[i].preTranslate(-scale/2 * bounds.width(), -scale/2 * bounds.height()); |
| 75 | fXforms[i].preScale(scale, scale); |
| 76 | fPaints[i].setAntiAlias(true); |
| 77 | fPaints[i].setColor(rand.nextU() | 0x80808080); |
| 78 | } |
Chris Dalton | 7c02cc7 | 2017-11-06 14:10:54 -0700 | [diff] [blame] | 79 | |
| 80 | if (fClipped) { |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 81 | fClipPath = ToolUtils::make_star(SkRect::MakeIWH(kScreenWidth, kScreenHeight), 11, 3); |
Chris Dalton | 7c02cc7 | 2017-11-06 14:10:54 -0700 | [diff] [blame] | 82 | fClipPath.setIsVolatile(fUncached); |
| 83 | } |
Chris Dalton | cb72722 | 2017-06-30 12:00:35 -0600 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void onDraw(int loops, SkCanvas* canvas) override { |
| 87 | SkAutoCanvasRestore acr(canvas, true); |
Chris Dalton | 7c02cc7 | 2017-11-06 14:10:54 -0700 | [diff] [blame] | 88 | if (fClipped) { |
| 89 | canvas->clipPath(fClipPath, SkClipOp::kIntersect, true); |
| 90 | } |
Chris Dalton | cb72722 | 2017-06-30 12:00:35 -0600 | [diff] [blame] | 91 | for (int i = 0; i < kNumDraws; ++i) { |
| 92 | const SkPath& glyph = fGlyphs[i % kNumGlyphs]; |
| 93 | canvas->setMatrix(fXforms[i]); |
| 94 | canvas->drawPath(glyph, fPaints[i]); |
| 95 | } |
| 96 | } |
| 97 | |
Chris Dalton | 7c02cc7 | 2017-11-06 14:10:54 -0700 | [diff] [blame] | 98 | const bool fClipped; |
| 99 | const bool fUncached; |
| 100 | SkString fName; |
Chris Dalton | cb72722 | 2017-06-30 12:00:35 -0600 | [diff] [blame] | 101 | SkPath fGlyphs[kNumGlyphs]; |
| 102 | SkPaint fPaints[kNumDraws]; |
| 103 | SkMatrix fXforms[kNumDraws]; |
Chris Dalton | 7c02cc7 | 2017-11-06 14:10:54 -0700 | [diff] [blame] | 104 | SkPath fClipPath; |
Chris Dalton | cb72722 | 2017-06-30 12:00:35 -0600 | [diff] [blame] | 105 | |
| 106 | typedef Benchmark INHERITED; |
| 107 | }; |
| 108 | |
Chris Dalton | 7c02cc7 | 2017-11-06 14:10:54 -0700 | [diff] [blame] | 109 | DEF_BENCH(return new PathTextBench(false, false);) |
| 110 | DEF_BENCH(return new PathTextBench(false, true);) |
| 111 | DEF_BENCH(return new PathTextBench(true, true);) |