Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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" |
Robert Phillips | f028810 | 2020-07-06 13:45:34 -0400 | [diff] [blame] | 9 | #include "include/gpu/GrDirectContext.h" |
Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 10 | #include "src/core/SkPathPriv.h" |
Chris Dalton | 8447f13 | 2021-05-21 15:54:23 -0600 | [diff] [blame] | 11 | #include "src/core/SkRectPriv.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 12 | #include "src/gpu/GrDirectContextPriv.h" |
Michael Ludwig | 4e9d5e2 | 2021-05-11 10:00:12 -0400 | [diff] [blame] | 13 | #include "src/gpu/geometry/GrWangsFormula.h" |
Chris Dalton | 90ad0fe | 2020-11-09 14:13:39 -0700 | [diff] [blame] | 14 | #include "src/gpu/mock/GrMockOpTarget.h" |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 15 | #include "src/gpu/tessellate/GrMiddleOutPolygonTriangulator.h" |
Chris Dalton | d9bdc32 | 2021-06-01 19:22:05 -0600 | [diff] [blame] | 16 | #include "src/gpu/tessellate/GrPathCurveTessellator.h" |
Chris Dalton | d9bdc32 | 2021-06-01 19:22:05 -0600 | [diff] [blame] | 17 | #include "src/gpu/tessellate/GrPathWedgeTessellator.h" |
Chris Dalton | 82007f5 | 2021-04-20 00:45:50 -0600 | [diff] [blame] | 18 | #include "src/gpu/tessellate/GrStrokeFixedCountTessellator.h" |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 19 | #include "src/gpu/tessellate/GrStrokeHardwareTessellator.h" |
Chris Dalton | f5132a0 | 2020-04-27 23:40:03 -0600 | [diff] [blame] | 20 | #include "tools/ToolUtils.h" |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 21 | #include <vector> |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 22 | |
Chris Dalton | 3b41278 | 2021-06-01 13:40:03 -0600 | [diff] [blame] | 23 | using ShaderFlags = GrStrokeTessellationShader::ShaderFlags; |
Chris Dalton | 42582fc | 2021-02-18 11:29:49 -0700 | [diff] [blame] | 24 | |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 25 | // This is the number of cubics in desk_chalkboard.skp. (There are no quadratics in the chalkboard.) |
| 26 | constexpr static int kNumCubicsInChalkboard = 47182; |
| 27 | |
Chris Dalton | 90ad0fe | 2020-11-09 14:13:39 -0700 | [diff] [blame] | 28 | static sk_sp<GrDirectContext> make_mock_context() { |
| 29 | GrMockOptions mockOptions; |
| 30 | mockOptions.fDrawInstancedSupport = true; |
| 31 | mockOptions.fMaxTessellationSegments = 64; |
| 32 | mockOptions.fMapBufferFlags = GrCaps::kCanMap_MapFlag; |
| 33 | mockOptions.fConfigOptions[(int)GrColorType::kAlpha_8].fRenderability = |
| 34 | GrMockOptions::ConfigOptions::Renderability::kMSAA; |
| 35 | mockOptions.fConfigOptions[(int)GrColorType::kAlpha_8].fTexturable = true; |
| 36 | mockOptions.fIntegerSupport = true; |
| 37 | |
| 38 | GrContextOptions ctxOptions; |
| 39 | ctxOptions.fGpuPathRenderers = GpuPathRenderers::kTessellation; |
Chris Dalton | 4ac9aad | 2021-02-24 17:41:44 -0700 | [diff] [blame] | 40 | ctxOptions.fEnableExperimentalHardwareTessellation = true; |
Chris Dalton | 90ad0fe | 2020-11-09 14:13:39 -0700 | [diff] [blame] | 41 | |
| 42 | return GrDirectContext::MakeMock(&mockOptions, ctxOptions); |
| 43 | } |
| 44 | |
Chris Dalton | 8447f13 | 2021-05-21 15:54:23 -0600 | [diff] [blame] | 45 | static SkPath make_cubic_path(int maxPow2) { |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 46 | SkRandom rand; |
| 47 | SkPath path; |
| 48 | for (int i = 0; i < kNumCubicsInChalkboard/2; ++i) { |
Chris Dalton | 8447f13 | 2021-05-21 15:54:23 -0600 | [diff] [blame] | 49 | float x = std::ldexp(rand.nextF(), (i % maxPow2)) / 1e3f; |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 50 | path.cubicTo(111.625f*x, 308.188f*x, 764.62f*x, -435.688f*x, 742.63f*x, 85.187f*x); |
| 51 | path.cubicTo(764.62f*x, -435.688f*x, 111.625f*x, 308.188f*x, 0, 0); |
| 52 | } |
| 53 | return path; |
| 54 | } |
| 55 | |
Tyler Denniston | 04f471a | 2021-02-04 13:07:03 -0500 | [diff] [blame] | 56 | static SkPath make_conic_path() { |
| 57 | SkRandom rand; |
| 58 | SkPath path; |
| 59 | for (int i = 0; i < kNumCubicsInChalkboard / 40; ++i) { |
| 60 | for (int j = -10; j <= 10; j++) { |
| 61 | const float x = std::ldexp(rand.nextF(), (i % 18)) / 1e3f; |
| 62 | const float w = std::ldexp(1 + rand.nextF(), j); |
| 63 | path.conicTo(111.625f * x, 308.188f * x, 764.62f * x, -435.688f * x, w); |
| 64 | } |
| 65 | } |
| 66 | return path; |
| 67 | } |
| 68 | |
Chris Dalton | 078f875 | 2020-07-30 19:50:46 -0600 | [diff] [blame] | 69 | // This serves as a base class for benchmarking individual methods on GrPathTessellateOp. |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 70 | class PathTessellateBenchmark : public Benchmark { |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 71 | public: |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 72 | PathTessellateBenchmark(const char* subName, const SkPath& p, const SkMatrix& m) |
| 73 | : fPath(p), fMatrix(m) { |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 74 | fName.printf("tessellate_%s", subName); |
| 75 | } |
| 76 | |
| 77 | const char* onGetName() override { return fName.c_str(); } |
| 78 | bool isSuitableFor(Backend backend) final { return backend == kNonRendering_Backend; } |
| 79 | |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 80 | protected: |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 81 | void onDelayedSetup() override { |
Chris Dalton | 90ad0fe | 2020-11-09 14:13:39 -0700 | [diff] [blame] | 82 | fTarget = std::make_unique<GrMockOpTarget>(make_mock_context()); |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 85 | void onDraw(int loops, SkCanvas*) final { |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 86 | if (!fTarget->mockContext()) { |
Chris Dalton | 1443c9d | 2020-05-27 09:43:34 -0600 | [diff] [blame] | 87 | SkDebugf("ERROR: could not create mock context."); |
| 88 | return; |
| 89 | } |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 90 | for (int i = 0; i < loops; ++i) { |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 91 | this->runBench(); |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 92 | fTarget->resetAllocator(); |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 96 | virtual void runBench() = 0; |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 97 | |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 98 | SkString fName; |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 99 | std::unique_ptr<GrMockOpTarget> fTarget; |
| 100 | const SkPath fPath; |
Chris Dalton | e1314a3 | 2021-01-29 13:22:33 -0700 | [diff] [blame] | 101 | const SkMatrix fMatrix; |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 102 | }; |
| 103 | |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 104 | #define DEF_PATH_TESS_BENCH(NAME, PATH, MATRIX) \ |
| 105 | class PathTessellateBenchmark_##NAME : public PathTessellateBenchmark { \ |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 106 | public: \ |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 107 | PathTessellateBenchmark_##NAME() : PathTessellateBenchmark(#NAME, (PATH), (MATRIX)) {} \ |
| 108 | void runBench() override; \ |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 109 | }; \ |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 110 | DEF_BENCH( return new PathTessellateBenchmark_##NAME(); ); \ |
| 111 | void PathTessellateBenchmark_##NAME::runBench() |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 112 | |
Chris Dalton | 6966981 | 2021-07-27 10:00:12 -0600 | [diff] [blame] | 113 | static const SkMatrix gAlmostIdentity = SkMatrix::MakeAll( |
| 114 | 1.0001f, 0.0001f, 0.0001f, |
| 115 | -.0001f, 0.9999f, -.0001f, |
| 116 | 0, 0, 1); |
| 117 | |
| 118 | DEF_PATH_TESS_BENCH(GrPathCurveTessellator, make_cubic_path(8), SkMatrix::I()) { |
Chris Dalton | 569c01b | 2021-05-25 10:11:46 -0600 | [diff] [blame] | 119 | SkArenaAlloc arena(1024); |
Chris Dalton | 198ac15 | 2021-06-09 13:49:43 -0600 | [diff] [blame] | 120 | GrPipeline noVaryingsPipeline(GrScissorTest::kDisabled, SkBlendMode::kSrcOver, |
| 121 | GrSwizzle::RGBA()); |
Chris Dalton | d9bdc32 | 2021-06-01 19:22:05 -0600 | [diff] [blame] | 122 | auto tess = GrPathCurveTessellator::Make(&arena, fMatrix, SK_PMColor4fTRANSPARENT, |
Chris Dalton | 7391511 | 2021-06-09 16:28:11 -0600 | [diff] [blame] | 123 | GrPathCurveTessellator::DrawInnerFan::kNo, |
Chris Dalton | 26666bd | 2021-06-08 16:25:46 -0600 | [diff] [blame] | 124 | fTarget->caps().minPathVerbsForHwTessellation(), |
Chris Dalton | 198ac15 | 2021-06-09 13:49:43 -0600 | [diff] [blame] | 125 | noVaryingsPipeline, fTarget->caps()); |
Chris Dalton | 17eaf62 | 2021-07-28 09:43:52 -0600 | [diff] [blame] | 126 | tess->prepare(fTarget.get(), SkRectPriv::MakeLargest(), {gAlmostIdentity, fPath}, |
| 127 | fPath.countVerbs()); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 128 | } |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 129 | |
Chris Dalton | 8447f13 | 2021-05-21 15:54:23 -0600 | [diff] [blame] | 130 | DEF_PATH_TESS_BENCH(GrPathWedgeTessellator, make_cubic_path(8), SkMatrix::I()) { |
Chris Dalton | 569c01b | 2021-05-25 10:11:46 -0600 | [diff] [blame] | 131 | SkArenaAlloc arena(1024); |
Chris Dalton | 198ac15 | 2021-06-09 13:49:43 -0600 | [diff] [blame] | 132 | GrPipeline noVaryingsPipeline(GrScissorTest::kDisabled, SkBlendMode::kSrcOver, |
| 133 | GrSwizzle::RGBA()); |
Chris Dalton | d2b8ba3 | 2021-06-09 00:12:59 -0600 | [diff] [blame] | 134 | auto tess = GrPathWedgeTessellator::Make(&arena, fMatrix, SK_PMColor4fTRANSPARENT, |
| 135 | fTarget->caps().minPathVerbsForHwTessellation(), |
Chris Dalton | 198ac15 | 2021-06-09 13:49:43 -0600 | [diff] [blame] | 136 | noVaryingsPipeline, fTarget->caps()); |
Chris Dalton | 17eaf62 | 2021-07-28 09:43:52 -0600 | [diff] [blame] | 137 | tess->prepare(fTarget.get(), SkRectPriv::MakeLargest(), {gAlmostIdentity, fPath}, |
| 138 | fPath.countVerbs()); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 139 | } |
Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 140 | |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 141 | static void benchmark_wangs_formula_cubic_log2(const SkMatrix& matrix, const SkPath& path) { |
| 142 | int sum = 0; |
| 143 | GrVectorXform xform(matrix); |
| 144 | for (auto [verb, pts, w] : SkPathPriv::Iterate(path)) { |
| 145 | if (verb == SkPathVerb::kCubic) { |
| 146 | sum += GrWangsFormula::cubic_log2(4, pts, xform); |
Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 147 | } |
| 148 | } |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 149 | // Don't let the compiler optimize away GrWangsFormula::cubic_log2. |
| 150 | if (sum <= 0) { |
| 151 | SK_ABORT("sum should be > 0."); |
| 152 | } |
| 153 | } |
Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 154 | |
Chris Dalton | 8447f13 | 2021-05-21 15:54:23 -0600 | [diff] [blame] | 155 | DEF_PATH_TESS_BENCH(wangs_formula_cubic_log2, make_cubic_path(18), SkMatrix::I()) { |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 156 | benchmark_wangs_formula_cubic_log2(fMatrix, fPath); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 157 | } |
| 158 | |
Chris Dalton | 8447f13 | 2021-05-21 15:54:23 -0600 | [diff] [blame] | 159 | DEF_PATH_TESS_BENCH(wangs_formula_cubic_log2_scale, make_cubic_path(18), |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 160 | SkMatrix::Scale(1.1f, 0.9f)) { |
| 161 | benchmark_wangs_formula_cubic_log2(fMatrix, fPath); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 162 | } |
| 163 | |
Chris Dalton | 8447f13 | 2021-05-21 15:54:23 -0600 | [diff] [blame] | 164 | DEF_PATH_TESS_BENCH(wangs_formula_cubic_log2_affine, make_cubic_path(18), |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 165 | SkMatrix::MakeAll(.9f,0.9f,0, 1.1f,1.1f,0, 0,0,1)) { |
| 166 | benchmark_wangs_formula_cubic_log2(fMatrix, fPath); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 167 | } |
| 168 | |
Tyler Denniston | 04f471a | 2021-02-04 13:07:03 -0500 | [diff] [blame] | 169 | static void benchmark_wangs_formula_conic(const SkMatrix& matrix, const SkPath& path) { |
Tyler Denniston | 04f471a | 2021-02-04 13:07:03 -0500 | [diff] [blame] | 170 | int sum = 0; |
| 171 | GrVectorXform xform(matrix); |
| 172 | for (auto [verb, pts, w] : SkPathPriv::Iterate(path)) { |
| 173 | if (verb == SkPathVerb::kConic) { |
Chris Dalton | e6f4531 | 2021-06-02 12:00:01 -0600 | [diff] [blame] | 174 | sum += GrWangsFormula::conic(4, pts, *w, xform); |
Tyler Denniston | 04f471a | 2021-02-04 13:07:03 -0500 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | // Don't let the compiler optimize away GrWangsFormula::conic. |
| 178 | if (sum <= 0) { |
| 179 | SK_ABORT("sum should be > 0."); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | static void benchmark_wangs_formula_conic_log2(const SkMatrix& matrix, const SkPath& path) { |
Tyler Denniston | 04f471a | 2021-02-04 13:07:03 -0500 | [diff] [blame] | 184 | int sum = 0; |
| 185 | GrVectorXform xform(matrix); |
| 186 | for (auto [verb, pts, w] : SkPathPriv::Iterate(path)) { |
| 187 | if (verb == SkPathVerb::kConic) { |
Chris Dalton | e6f4531 | 2021-06-02 12:00:01 -0600 | [diff] [blame] | 188 | sum += GrWangsFormula::conic_log2(4, pts, *w, xform); |
Tyler Denniston | 04f471a | 2021-02-04 13:07:03 -0500 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | // Don't let the compiler optimize away GrWangsFormula::conic. |
| 192 | if (sum <= 0) { |
| 193 | SK_ABORT("sum should be > 0."); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | DEF_PATH_TESS_BENCH(wangs_formula_conic, make_conic_path(), SkMatrix::I()) { |
| 198 | benchmark_wangs_formula_conic(fMatrix, fPath); |
| 199 | } |
| 200 | |
| 201 | DEF_PATH_TESS_BENCH(wangs_formula_conic_log2, make_conic_path(), SkMatrix::I()) { |
| 202 | benchmark_wangs_formula_conic_log2(fMatrix, fPath); |
| 203 | } |
| 204 | |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 205 | DEF_PATH_TESS_BENCH(middle_out_triangulation, |
| 206 | ToolUtils::make_star(SkRect::MakeWH(500, 500), kNumCubicsInChalkboard), |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 207 | SkMatrix::I()) { |
Chris Dalton | 8ed7a8d | 2021-03-31 10:40:29 -0600 | [diff] [blame] | 208 | sk_sp<const GrBuffer> buffer; |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 209 | int baseVertex; |
Chris Dalton | 8731a71 | 2021-05-14 14:48:54 -0600 | [diff] [blame] | 210 | GrVertexWriter vertexWriter = static_cast<SkPoint*>(fTarget->makeVertexSpace( |
Chris Dalton | 8ed7a8d | 2021-03-31 10:40:29 -0600 | [diff] [blame] | 211 | sizeof(SkPoint), kNumCubicsInChalkboard, &buffer, &baseVertex)); |
Chris Dalton | 40c906f | 2021-07-26 11:27:05 -0600 | [diff] [blame] | 212 | int numTrianglesWritten; |
Chris Dalton | 6966981 | 2021-07-27 10:00:12 -0600 | [diff] [blame] | 213 | GrMiddleOutPolygonTriangulator::WritePathInnerFan(std::move(vertexWriter), 0, 0, |
| 214 | gAlmostIdentity, fPath, &numTrianglesWritten); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 215 | } |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 216 | |
Chris Dalton | 981e4a7 | 2021-02-22 12:13:49 -0700 | [diff] [blame] | 217 | using PathStrokeList = GrStrokeTessellator::PathStrokeList; |
Chris Dalton | 13adb4a | 2021-05-26 10:21:56 -0600 | [diff] [blame] | 218 | using MakeTessellatorFn = std::unique_ptr<GrStrokeTessellator>(*)(ShaderFlags, const GrShaderCaps&, |
| 219 | const SkMatrix&, PathStrokeList*, |
Chris Dalton | 0638df1 | 2021-05-14 15:57:39 -0600 | [diff] [blame] | 220 | std::array<float, 2>, const |
| 221 | SkRect&); |
Chris Dalton | 82007f5 | 2021-04-20 00:45:50 -0600 | [diff] [blame] | 222 | |
Chris Dalton | 0638df1 | 2021-05-14 15:57:39 -0600 | [diff] [blame] | 223 | static std::unique_ptr<GrStrokeTessellator> make_hw_tessellator( |
Chris Dalton | 13adb4a | 2021-05-26 10:21:56 -0600 | [diff] [blame] | 224 | ShaderFlags shaderFlags, const GrShaderCaps& shaderCaps, const SkMatrix& viewMatrix, |
| 225 | PathStrokeList* pathStrokeList, std::array<float, 2> matrixMinMaxScales, |
| 226 | const SkRect& strokeCullBounds) { |
Chris Dalton | 6904303 | 2021-07-01 11:17:53 -0600 | [diff] [blame] | 227 | return std::make_unique<GrStrokeHardwareTessellator>(shaderCaps, shaderFlags, viewMatrix, |
Chris Dalton | 13adb4a | 2021-05-26 10:21:56 -0600 | [diff] [blame] | 228 | pathStrokeList, matrixMinMaxScales, |
| 229 | strokeCullBounds); |
Chris Dalton | 82007f5 | 2021-04-20 00:45:50 -0600 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | static std::unique_ptr<GrStrokeTessellator> make_fixed_count_tessellator( |
Chris Dalton | bb995e6 | 2021-07-01 10:58:55 -0600 | [diff] [blame] | 233 | ShaderFlags shaderFlags, const GrShaderCaps& shaderCaps, const SkMatrix& viewMatrix, |
Chris Dalton | 13adb4a | 2021-05-26 10:21:56 -0600 | [diff] [blame] | 234 | PathStrokeList* pathStrokeList, std::array<float, 2> matrixMinMaxScales, |
| 235 | const SkRect& strokeCullBounds) { |
Chris Dalton | 6904303 | 2021-07-01 11:17:53 -0600 | [diff] [blame] | 236 | return std::make_unique<GrStrokeFixedCountTessellator>(shaderCaps, shaderFlags, viewMatrix, |
| 237 | pathStrokeList, matrixMinMaxScales, |
| 238 | strokeCullBounds); |
Chris Dalton | 82007f5 | 2021-04-20 00:45:50 -0600 | [diff] [blame] | 239 | } |
| 240 | |
Chris Dalton | 981e4a7 | 2021-02-22 12:13:49 -0700 | [diff] [blame] | 241 | using MakePathStrokesFn = std::vector<PathStrokeList>(*)(); |
| 242 | |
| 243 | static std::vector<PathStrokeList> make_simple_cubic_path() { |
| 244 | auto path = SkPath().moveTo(0, 0); |
| 245 | for (int i = 0; i < kNumCubicsInChalkboard/2; ++i) { |
| 246 | path.cubicTo(100, 0, 50, 100, 100, 100); |
| 247 | path.cubicTo(0, -100, 200, 100, 0, 0); |
| 248 | } |
| 249 | SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle); |
| 250 | stroke.setStrokeStyle(8); |
| 251 | stroke.setStrokeParams(SkPaint::kButt_Cap, SkPaint::kMiter_Join, 4); |
| 252 | return {{path, stroke, SK_PMColor4fWHITE}}; |
| 253 | } |
| 254 | |
| 255 | // Generates a list of paths that resemble the MotionMark benchmark. |
| 256 | static std::vector<PathStrokeList> make_motionmark_paths() { |
| 257 | std::vector<PathStrokeList> pathStrokes; |
| 258 | SkRandom rand; |
| 259 | for (int i = 0; i < 8702; ++i) { |
| 260 | // The number of paths with a given number of verbs in the MotionMark bench gets cut in half |
| 261 | // every time the number of verbs increases by 1. |
| 262 | int numVerbs = 28 - SkNextLog2(rand.nextRangeU(0, (1 << 27) - 1)); |
| 263 | SkPath path; |
| 264 | for (int j = 0; j < numVerbs; ++j) { |
| 265 | switch (rand.nextU() & 3) { |
| 266 | case 0: |
| 267 | case 1: |
| 268 | path.lineTo(rand.nextRangeF(0, 150), rand.nextRangeF(0, 150)); |
| 269 | break; |
| 270 | case 2: |
| 271 | if (rand.nextULessThan(10) == 0) { |
| 272 | // Cusp. |
| 273 | auto [x, y] = (path.isEmpty()) |
| 274 | ? SkPoint{0,0} |
| 275 | : SkPathPriv::PointData(path)[path.countPoints() - 1]; |
| 276 | path.quadTo(x + rand.nextRangeF(0, 150), y, x - rand.nextRangeF(0, 150), y); |
| 277 | } else { |
| 278 | path.quadTo(rand.nextRangeF(0, 150), rand.nextRangeF(0, 150), |
| 279 | rand.nextRangeF(0, 150), rand.nextRangeF(0, 150)); |
| 280 | } |
| 281 | break; |
| 282 | case 3: |
| 283 | if (rand.nextULessThan(10) == 0) { |
| 284 | // Cusp. |
| 285 | float y = (path.isEmpty()) |
| 286 | ? 0 : SkPathPriv::PointData(path)[path.countPoints() - 1].fY; |
| 287 | path.cubicTo(rand.nextRangeF(0, 150), y, rand.nextRangeF(0, 150), y, |
| 288 | rand.nextRangeF(0, 150), y); |
| 289 | } else { |
| 290 | path.cubicTo(rand.nextRangeF(0, 150), rand.nextRangeF(0, 150), |
| 291 | rand.nextRangeF(0, 150), rand.nextRangeF(0, 150), |
| 292 | rand.nextRangeF(0, 150), rand.nextRangeF(0, 150)); |
| 293 | } |
| 294 | break; |
| 295 | } |
| 296 | } |
| 297 | SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle); |
| 298 | // The number of paths with a given stroke width in the MotionMark bench gets cut in half |
| 299 | // every time the stroke width increases by 1. |
| 300 | float strokeWidth = 21 - log2f(rand.nextRangeF(0, 1 << 20)); |
| 301 | stroke.setStrokeStyle(strokeWidth); |
| 302 | stroke.setStrokeParams(SkPaint::kButt_Cap, SkPaint::kBevel_Join, 0); |
| 303 | pathStrokes.emplace_back(path, stroke, SK_PMColor4fWHITE); |
| 304 | } |
| 305 | return pathStrokes; |
| 306 | } |
| 307 | |
Chris Dalton | 82007f5 | 2021-04-20 00:45:50 -0600 | [diff] [blame] | 308 | class TessPrepareBench : public Benchmark { |
Chris Dalton | 2882e70 | 2020-11-02 12:43:06 -0700 | [diff] [blame] | 309 | public: |
Chris Dalton | 82007f5 | 2021-04-20 00:45:50 -0600 | [diff] [blame] | 310 | TessPrepareBench(MakePathStrokesFn makePathStrokesFn, MakeTessellatorFn makeTessellatorFn, |
| 311 | ShaderFlags shaderFlags, float matrixScale, const char* suffix) |
| 312 | : fMakePathStrokesFn(makePathStrokesFn) |
| 313 | , fMakeTessellatorFn(makeTessellatorFn) |
Chris Dalton | bb33be2 | 2021-02-24 16:30:34 -0700 | [diff] [blame] | 314 | , fShaderFlags(shaderFlags) |
Chris Dalton | 981e4a7 | 2021-02-22 12:13:49 -0700 | [diff] [blame] | 315 | , fMatrixScale(matrixScale) { |
Chris Dalton | 82007f5 | 2021-04-20 00:45:50 -0600 | [diff] [blame] | 316 | fName.printf("tessellate_%s", suffix); |
Chris Dalton | 2882e70 | 2020-11-02 12:43:06 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | private: |
| 320 | const char* onGetName() override { return fName.c_str(); } |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 321 | bool isSuitableFor(Backend backend) final { return backend == kNonRendering_Backend; } |
| 322 | |
| 323 | void onDelayedSetup() override { |
Chris Dalton | 90ad0fe | 2020-11-09 14:13:39 -0700 | [diff] [blame] | 324 | fTarget = std::make_unique<GrMockOpTarget>(make_mock_context()); |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 325 | if (!fTarget->mockContext()) { |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 326 | SkDebugf("ERROR: could not create mock context."); |
| 327 | return; |
| 328 | } |
Chris Dalton | 981e4a7 | 2021-02-22 12:13:49 -0700 | [diff] [blame] | 329 | |
| 330 | fPathStrokes = fMakePathStrokesFn(); |
| 331 | for (size_t i = 0; i < fPathStrokes.size(); ++i) { |
| 332 | if (i + 1 < fPathStrokes.size()) { |
| 333 | fPathStrokes[i].fNext = &fPathStrokes[i + 1]; |
| 334 | } |
| 335 | fTotalVerbCount += fPathStrokes[i].fPath.countVerbs(); |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 336 | } |
Chris Dalton | 82007f5 | 2021-04-20 00:45:50 -0600 | [diff] [blame] | 337 | |
Chris Dalton | 13adb4a | 2021-05-26 10:21:56 -0600 | [diff] [blame] | 338 | fTessellator = fMakeTessellatorFn(fShaderFlags, *fTarget->caps().shaderCaps(), |
| 339 | SkMatrix::Scale(fMatrixScale, fMatrixScale), |
Chris Dalton | 0638df1 | 2021-05-14 15:57:39 -0600 | [diff] [blame] | 340 | fPathStrokes.data(), {fMatrixScale, fMatrixScale}, |
Chris Dalton | 8447f13 | 2021-05-21 15:54:23 -0600 | [diff] [blame] | 341 | SkRectPriv::MakeLargest()); |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 342 | } |
| 343 | |
Chris Dalton | 981e4a7 | 2021-02-22 12:13:49 -0700 | [diff] [blame] | 344 | void onDraw(int loops, SkCanvas*) final { |
Chris Dalton | 981e4a7 | 2021-02-22 12:13:49 -0700 | [diff] [blame] | 345 | for (int i = 0; i < loops; ++i) { |
Chris Dalton | 82007f5 | 2021-04-20 00:45:50 -0600 | [diff] [blame] | 346 | fTessellator->prepare(fTarget.get(), fTotalVerbCount); |
Chris Dalton | 981e4a7 | 2021-02-22 12:13:49 -0700 | [diff] [blame] | 347 | fTarget->resetAllocator(); |
| 348 | } |
| 349 | } |
| 350 | |
Chris Dalton | 2882e70 | 2020-11-02 12:43:06 -0700 | [diff] [blame] | 351 | SkString fName; |
Chris Dalton | 981e4a7 | 2021-02-22 12:13:49 -0700 | [diff] [blame] | 352 | MakePathStrokesFn fMakePathStrokesFn; |
Chris Dalton | 82007f5 | 2021-04-20 00:45:50 -0600 | [diff] [blame] | 353 | MakeTessellatorFn fMakeTessellatorFn; |
Chris Dalton | bb33be2 | 2021-02-24 16:30:34 -0700 | [diff] [blame] | 354 | const ShaderFlags fShaderFlags; |
Chris Dalton | 981e4a7 | 2021-02-22 12:13:49 -0700 | [diff] [blame] | 355 | float fMatrixScale; |
Chris Dalton | 90ad0fe | 2020-11-09 14:13:39 -0700 | [diff] [blame] | 356 | std::unique_ptr<GrMockOpTarget> fTarget; |
Chris Dalton | 981e4a7 | 2021-02-22 12:13:49 -0700 | [diff] [blame] | 357 | std::vector<PathStrokeList> fPathStrokes; |
Chris Dalton | 82007f5 | 2021-04-20 00:45:50 -0600 | [diff] [blame] | 358 | std::unique_ptr<GrStrokeTessellator> fTessellator; |
Chris Dalton | 981e4a7 | 2021-02-22 12:13:49 -0700 | [diff] [blame] | 359 | SkArenaAlloc fPersistentArena{1024}; |
| 360 | int fTotalVerbCount = 0; |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 361 | }; |
| 362 | |
Chris Dalton | 82007f5 | 2021-04-20 00:45:50 -0600 | [diff] [blame] | 363 | DEF_BENCH(return new TessPrepareBench( |
| 364 | make_simple_cubic_path, make_hw_tessellator, ShaderFlags::kNone, 1, |
| 365 | "GrStrokeHardwareTessellator"); |
Chris Dalton | bb33be2 | 2021-02-24 16:30:34 -0700 | [diff] [blame] | 366 | ) |
| 367 | |
Chris Dalton | 82007f5 | 2021-04-20 00:45:50 -0600 | [diff] [blame] | 368 | DEF_BENCH(return new TessPrepareBench( |
| 369 | make_simple_cubic_path, make_hw_tessellator, ShaderFlags::kNone, 5, |
| 370 | "GrStrokeHardwareTessellator_one_chop"); |
Chris Dalton | bb33be2 | 2021-02-24 16:30:34 -0700 | [diff] [blame] | 371 | ) |
| 372 | |
Chris Dalton | 82007f5 | 2021-04-20 00:45:50 -0600 | [diff] [blame] | 373 | DEF_BENCH(return new TessPrepareBench( |
| 374 | make_motionmark_paths, make_hw_tessellator, ShaderFlags::kDynamicStroke, 1, |
| 375 | "GrStrokeHardwareTessellator_motionmark"); |
| 376 | ) |
| 377 | |
| 378 | DEF_BENCH(return new TessPrepareBench( |
| 379 | make_simple_cubic_path, make_fixed_count_tessellator, ShaderFlags::kNone, 1, |
| 380 | "GrStrokeFixedCountTessellator"); |
| 381 | ) |
| 382 | |
| 383 | DEF_BENCH(return new TessPrepareBench( |
| 384 | make_simple_cubic_path, make_fixed_count_tessellator, ShaderFlags::kNone, 5, |
| 385 | "GrStrokeFixedCountTessellator_one_chop"); |
| 386 | ) |
| 387 | |
| 388 | DEF_BENCH(return new TessPrepareBench( |
| 389 | make_motionmark_paths, make_fixed_count_tessellator, ShaderFlags::kDynamicStroke, 1, |
| 390 | "GrStrokeFixedCountTessellator_motionmark"); |
Chris Dalton | bb33be2 | 2021-02-24 16:30:34 -0700 | [diff] [blame] | 391 | ) |