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" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 11 | #include "src/gpu/GrDirectContextPriv.h" |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 12 | #include "src/gpu/GrOpFlushState.h" |
Chris Dalton | 90ad0fe | 2020-11-09 14:13:39 -0700 | [diff] [blame] | 13 | #include "src/gpu/mock/GrMockOpTarget.h" |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 14 | #include "src/gpu/tessellate/GrMiddleOutPolygonTriangulator.h" |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 15 | #include "src/gpu/tessellate/GrPathTessellator.h" |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 16 | #include "src/gpu/tessellate/GrStrokeHardwareTessellator.h" |
| 17 | #include "src/gpu/tessellate/GrStrokeIndirectTessellator.h" |
Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 18 | #include "src/gpu/tessellate/GrWangsFormula.h" |
Chris Dalton | f5132a0 | 2020-04-27 23:40:03 -0600 | [diff] [blame] | 19 | #include "tools/ToolUtils.h" |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 20 | #include <vector> |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 21 | |
| 22 | // This is the number of cubics in desk_chalkboard.skp. (There are no quadratics in the chalkboard.) |
| 23 | constexpr static int kNumCubicsInChalkboard = 47182; |
| 24 | |
Chris Dalton | 90ad0fe | 2020-11-09 14:13:39 -0700 | [diff] [blame] | 25 | static sk_sp<GrDirectContext> make_mock_context() { |
| 26 | GrMockOptions mockOptions; |
| 27 | mockOptions.fDrawInstancedSupport = true; |
| 28 | mockOptions.fMaxTessellationSegments = 64; |
| 29 | mockOptions.fMapBufferFlags = GrCaps::kCanMap_MapFlag; |
| 30 | mockOptions.fConfigOptions[(int)GrColorType::kAlpha_8].fRenderability = |
| 31 | GrMockOptions::ConfigOptions::Renderability::kMSAA; |
| 32 | mockOptions.fConfigOptions[(int)GrColorType::kAlpha_8].fTexturable = true; |
| 33 | mockOptions.fIntegerSupport = true; |
| 34 | |
| 35 | GrContextOptions ctxOptions; |
| 36 | ctxOptions.fGpuPathRenderers = GpuPathRenderers::kTessellation; |
Chris Dalton | 15f5184 | 2020-12-15 19:55:10 -0700 | [diff] [blame] | 37 | ctxOptions.fSuppressTessellationShaders = false; |
Chris Dalton | 90ad0fe | 2020-11-09 14:13:39 -0700 | [diff] [blame] | 38 | |
| 39 | return GrDirectContext::MakeMock(&mockOptions, ctxOptions); |
| 40 | } |
| 41 | |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 42 | static SkPath make_cubic_path() { |
| 43 | SkRandom rand; |
| 44 | SkPath path; |
| 45 | for (int i = 0; i < kNumCubicsInChalkboard/2; ++i) { |
Chris Dalton | 2f2d81c | 2020-05-13 17:57:37 -0600 | [diff] [blame] | 46 | float x = std::ldexp(rand.nextF(), (i % 18)) / 1e3f; |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 47 | path.cubicTo(111.625f*x, 308.188f*x, 764.62f*x, -435.688f*x, 742.63f*x, 85.187f*x); |
| 48 | path.cubicTo(764.62f*x, -435.688f*x, 111.625f*x, 308.188f*x, 0, 0); |
| 49 | } |
| 50 | return path; |
| 51 | } |
| 52 | |
Tyler Denniston | 04f471a | 2021-02-04 13:07:03 -0500 | [diff] [blame] | 53 | static SkPath make_conic_path() { |
| 54 | SkRandom rand; |
| 55 | SkPath path; |
| 56 | for (int i = 0; i < kNumCubicsInChalkboard / 40; ++i) { |
| 57 | for (int j = -10; j <= 10; j++) { |
| 58 | const float x = std::ldexp(rand.nextF(), (i % 18)) / 1e3f; |
| 59 | const float w = std::ldexp(1 + rand.nextF(), j); |
| 60 | path.conicTo(111.625f * x, 308.188f * x, 764.62f * x, -435.688f * x, w); |
| 61 | } |
| 62 | } |
| 63 | return path; |
| 64 | } |
| 65 | |
Chris Dalton | 078f875 | 2020-07-30 19:50:46 -0600 | [diff] [blame] | 66 | // This serves as a base class for benchmarking individual methods on GrPathTessellateOp. |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 67 | class PathTessellateBenchmark : public Benchmark { |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 68 | public: |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 69 | PathTessellateBenchmark(const char* subName, const SkPath& p, const SkMatrix& m) |
| 70 | : fPath(p), fMatrix(m) { |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 71 | fName.printf("tessellate_%s", subName); |
| 72 | } |
| 73 | |
| 74 | const char* onGetName() override { return fName.c_str(); } |
| 75 | bool isSuitableFor(Backend backend) final { return backend == kNonRendering_Backend; } |
| 76 | |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 77 | protected: |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 78 | void onDelayedSetup() override { |
Chris Dalton | 90ad0fe | 2020-11-09 14:13:39 -0700 | [diff] [blame] | 79 | fTarget = std::make_unique<GrMockOpTarget>(make_mock_context()); |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 82 | void onDraw(int loops, SkCanvas*) final { |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 83 | if (!fTarget->mockContext()) { |
Chris Dalton | 1443c9d | 2020-05-27 09:43:34 -0600 | [diff] [blame] | 84 | SkDebugf("ERROR: could not create mock context."); |
| 85 | return; |
| 86 | } |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 87 | for (int i = 0; i < loops; ++i) { |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 88 | this->runBench(); |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 89 | fTarget->resetAllocator(); |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 93 | virtual void runBench() = 0; |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 94 | |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 95 | SkString fName; |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 96 | std::unique_ptr<GrMockOpTarget> fTarget; |
| 97 | const SkPath fPath; |
Chris Dalton | e1314a3 | 2021-01-29 13:22:33 -0700 | [diff] [blame] | 98 | const SkMatrix fMatrix; |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 99 | }; |
| 100 | |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 101 | #define DEF_PATH_TESS_BENCH(NAME, PATH, MATRIX) \ |
| 102 | class PathTessellateBenchmark_##NAME : public PathTessellateBenchmark { \ |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 103 | public: \ |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 104 | PathTessellateBenchmark_##NAME() : PathTessellateBenchmark(#NAME, (PATH), (MATRIX)) {} \ |
| 105 | void runBench() override; \ |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 106 | }; \ |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 107 | DEF_BENCH( return new PathTessellateBenchmark_##NAME(); ); \ |
| 108 | void PathTessellateBenchmark_##NAME::runBench() |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 109 | |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 110 | DEF_PATH_TESS_BENCH(GrPathIndirectTessellator, make_cubic_path(), SkMatrix::I()) { |
| 111 | GrPathIndirectTessellator tess(fMatrix, fPath, GrPathIndirectTessellator::DrawInnerFan::kNo); |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 112 | tess.prepare(fTarget.get(), fMatrix, fPath, nullptr); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 113 | } |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 114 | |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 115 | DEF_PATH_TESS_BENCH(GrPathOuterCurveTessellator, make_cubic_path(), SkMatrix::I()) { |
| 116 | GrPathOuterCurveTessellator tess; |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 117 | tess.prepare(fTarget.get(), fMatrix, fPath, nullptr); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 118 | } |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 119 | |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 120 | DEF_PATH_TESS_BENCH(GrPathWedgeTessellator, make_cubic_path(), SkMatrix::I()) { |
| 121 | GrPathWedgeTessellator tess; |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 122 | tess.prepare(fTarget.get(), fMatrix, fPath, nullptr); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 123 | } |
Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 124 | |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 125 | static void benchmark_wangs_formula_cubic_log2(const SkMatrix& matrix, const SkPath& path) { |
| 126 | int sum = 0; |
| 127 | GrVectorXform xform(matrix); |
| 128 | for (auto [verb, pts, w] : SkPathPriv::Iterate(path)) { |
| 129 | if (verb == SkPathVerb::kCubic) { |
| 130 | sum += GrWangsFormula::cubic_log2(4, pts, xform); |
Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 131 | } |
| 132 | } |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 133 | // Don't let the compiler optimize away GrWangsFormula::cubic_log2. |
| 134 | if (sum <= 0) { |
| 135 | SK_ABORT("sum should be > 0."); |
| 136 | } |
| 137 | } |
Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 138 | |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 139 | DEF_PATH_TESS_BENCH(wangs_formula_cubic_log2, make_cubic_path(), SkMatrix::I()) { |
| 140 | benchmark_wangs_formula_cubic_log2(fMatrix, fPath); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 141 | } |
| 142 | |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 143 | DEF_PATH_TESS_BENCH(wangs_formula_cubic_log2_scale, make_cubic_path(), |
| 144 | SkMatrix::Scale(1.1f, 0.9f)) { |
| 145 | benchmark_wangs_formula_cubic_log2(fMatrix, fPath); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 146 | } |
| 147 | |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 148 | DEF_PATH_TESS_BENCH(wangs_formula_cubic_log2_affine, make_cubic_path(), |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 149 | SkMatrix::MakeAll(.9f,0.9f,0, 1.1f,1.1f,0, 0,0,1)) { |
| 150 | benchmark_wangs_formula_cubic_log2(fMatrix, fPath); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 151 | } |
| 152 | |
Tyler Denniston | 04f471a | 2021-02-04 13:07:03 -0500 | [diff] [blame] | 153 | static void benchmark_wangs_formula_conic(const SkMatrix& matrix, const SkPath& path) { |
| 154 | // Conic version expects tolerance, not intolerance |
| 155 | constexpr float kTolerance = 4; |
| 156 | int sum = 0; |
| 157 | GrVectorXform xform(matrix); |
| 158 | for (auto [verb, pts, w] : SkPathPriv::Iterate(path)) { |
| 159 | if (verb == SkPathVerb::kConic) { |
| 160 | sum += GrWangsFormula::conic(kTolerance, pts, *w, xform); |
| 161 | } |
| 162 | } |
| 163 | // Don't let the compiler optimize away GrWangsFormula::conic. |
| 164 | if (sum <= 0) { |
| 165 | SK_ABORT("sum should be > 0."); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | static void benchmark_wangs_formula_conic_log2(const SkMatrix& matrix, const SkPath& path) { |
| 170 | // Conic version expects tolerance, not intolerance |
| 171 | constexpr float kTolerance = 4; |
| 172 | int sum = 0; |
| 173 | GrVectorXform xform(matrix); |
| 174 | for (auto [verb, pts, w] : SkPathPriv::Iterate(path)) { |
| 175 | if (verb == SkPathVerb::kConic) { |
| 176 | sum += GrWangsFormula::conic_log2(kTolerance, pts, *w, xform); |
| 177 | } |
| 178 | } |
| 179 | // Don't let the compiler optimize away GrWangsFormula::conic. |
| 180 | if (sum <= 0) { |
| 181 | SK_ABORT("sum should be > 0."); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | DEF_PATH_TESS_BENCH(wangs_formula_conic, make_conic_path(), SkMatrix::I()) { |
| 186 | benchmark_wangs_formula_conic(fMatrix, fPath); |
| 187 | } |
| 188 | |
| 189 | DEF_PATH_TESS_BENCH(wangs_formula_conic_log2, make_conic_path(), SkMatrix::I()) { |
| 190 | benchmark_wangs_formula_conic_log2(fMatrix, fPath); |
| 191 | } |
| 192 | |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 193 | DEF_PATH_TESS_BENCH(middle_out_triangulation, |
| 194 | ToolUtils::make_star(SkRect::MakeWH(500, 500), kNumCubicsInChalkboard), |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 195 | SkMatrix::I()) { |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 196 | int baseVertex; |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 197 | auto vertexData = static_cast<SkPoint*>(fTarget->makeVertexSpace( |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 198 | sizeof(SkPoint), kNumCubicsInChalkboard, nullptr, &baseVertex)); |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 199 | GrMiddleOutPolygonTriangulator::WritePathInnerFan(vertexData, 3, fPath); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 200 | } |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 201 | |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 202 | class GrStrokeHardwareTessellator::TestingOnly_Benchmark : public Benchmark { |
Chris Dalton | 2882e70 | 2020-11-02 12:43:06 -0700 | [diff] [blame] | 203 | public: |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 204 | TestingOnly_Benchmark(float matrixScale, const char* suffix) : fMatrixScale(matrixScale) { |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 205 | fName.printf("tessellate_GrStrokeHardwareTessellator_prepare%s", suffix); |
Chris Dalton | 2882e70 | 2020-11-02 12:43:06 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | private: |
| 209 | const char* onGetName() override { return fName.c_str(); } |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 210 | bool isSuitableFor(Backend backend) final { return backend == kNonRendering_Backend; } |
| 211 | |
| 212 | void onDelayedSetup() override { |
Chris Dalton | 90ad0fe | 2020-11-09 14:13:39 -0700 | [diff] [blame] | 213 | fTarget = std::make_unique<GrMockOpTarget>(make_mock_context()); |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 214 | fPath.reset().moveTo(0, 0); |
| 215 | for (int i = 0; i < kNumCubicsInChalkboard/2; ++i) { |
Chris Dalton | 2882e70 | 2020-11-02 12:43:06 -0700 | [diff] [blame] | 216 | fPath.cubicTo(100, 0, 50, 100, 100, 100); |
| 217 | fPath.cubicTo(0, -100, 200, 100, 0, 0); |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 218 | } |
| 219 | fStrokeRec.setStrokeStyle(8); |
| 220 | fStrokeRec.setStrokeParams(SkPaint::kButt_Cap, SkPaint::kMiter_Join, 4); |
| 221 | } |
| 222 | |
| 223 | void onDraw(int loops, SkCanvas*) final { |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 224 | if (!fTarget->mockContext()) { |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 225 | SkDebugf("ERROR: could not create mock context."); |
| 226 | return; |
| 227 | } |
| 228 | for (int i = 0; i < loops; ++i) { |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 229 | SkMatrix matrix = SkMatrix::Scale(fMatrixScale, fMatrixScale); |
| 230 | GrStrokeHardwareTessellator tessellator(*fTarget->caps().shaderCaps(), matrix, |
| 231 | fStrokeRec); |
| 232 | tessellator.prepare(fTarget.get(), matrix, fPath, fStrokeRec, fPath.countVerbs()); |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
Chris Dalton | 2882e70 | 2020-11-02 12:43:06 -0700 | [diff] [blame] | 236 | const float fMatrixScale; |
| 237 | SkString fName; |
Chris Dalton | 90ad0fe | 2020-11-09 14:13:39 -0700 | [diff] [blame] | 238 | std::unique_ptr<GrMockOpTarget> fTarget; |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 239 | SkPath fPath; |
| 240 | SkStrokeRec fStrokeRec = SkStrokeRec(SkStrokeRec::kFill_InitStyle); |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 241 | }; |
| 242 | |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 243 | DEF_BENCH( return new GrStrokeHardwareTessellator::TestingOnly_Benchmark(1, ""); ) |
| 244 | DEF_BENCH( return new GrStrokeHardwareTessellator::TestingOnly_Benchmark(5, "_one_chop"); ) |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 245 | |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 246 | class GrStrokeIndirectTessellator::Benchmark : public ::Benchmark { |
Chris Dalton | ad48280 | 2020-12-28 09:02:47 -0700 | [diff] [blame] | 247 | protected: |
| 248 | Benchmark(const char* nameSuffix, SkPaint::Join join) : fJoin(join) { |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 249 | fName.printf("tessellate_GrStrokeIndirectTessellator%s", nameSuffix); |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Chris Dalton | ad48280 | 2020-12-28 09:02:47 -0700 | [diff] [blame] | 252 | const SkPaint::Join fJoin; |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 253 | |
Chris Dalton | ad48280 | 2020-12-28 09:02:47 -0700 | [diff] [blame] | 254 | private: |
| 255 | const char* onGetName() final { return fName.c_str(); } |
| 256 | bool isSuitableFor(Backend backend) final { return backend == kNonRendering_Backend; } |
| 257 | void onDelayedSetup() final { |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 258 | fTarget = std::make_unique<GrMockOpTarget>(make_mock_context()); |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 259 | fStrokeRec.setStrokeStyle(8); |
| 260 | fStrokeRec.setStrokeParams(SkPaint::kButt_Cap, fJoin, 4); |
Chris Dalton | ad48280 | 2020-12-28 09:02:47 -0700 | [diff] [blame] | 261 | this->setupPaths(&fPaths); |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 262 | } |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 263 | void onDraw(int loops, SkCanvas*) final { |
| 264 | if (!fTarget->mockContext()) { |
| 265 | SkDebugf("ERROR: could not create mock context."); |
| 266 | return; |
| 267 | } |
| 268 | for (int i = 0; i < loops; ++i) { |
Chris Dalton | ad48280 | 2020-12-28 09:02:47 -0700 | [diff] [blame] | 269 | for (const SkPath& path : fPaths) { |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 270 | GrStrokeIndirectTessellator tessellator(SkMatrix::I(), path, fStrokeRec, |
| 271 | path.countVerbs(), fTarget->allocator()); |
| 272 | tessellator.prepare(fTarget.get(), SkMatrix::I(), path, fStrokeRec, |
| 273 | path.countVerbs()); |
Chris Dalton | ad48280 | 2020-12-28 09:02:47 -0700 | [diff] [blame] | 274 | } |
| 275 | fTarget->resetAllocator(); |
| 276 | } |
| 277 | } |
| 278 | virtual void setupPaths(SkTArray<SkPath>*) = 0; |
| 279 | |
| 280 | SkString fName; |
| 281 | std::unique_ptr<GrMockOpTarget> fTarget; |
| 282 | SkTArray<SkPath> fPaths; |
| 283 | SkStrokeRec fStrokeRec{SkStrokeRec::kHairline_InitStyle}; |
| 284 | }; |
| 285 | |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 286 | class StrokeIndirectBenchmark : public GrStrokeIndirectTessellator::Benchmark { |
Chris Dalton | ad48280 | 2020-12-28 09:02:47 -0700 | [diff] [blame] | 287 | public: |
| 288 | StrokeIndirectBenchmark(const char* nameSuffix, SkPaint::Join join, std::vector<SkPoint> pts) |
| 289 | : Benchmark(nameSuffix, join), fPts(std::move(pts)) {} |
| 290 | |
| 291 | private: |
| 292 | void setupPaths(SkTArray<SkPath>* paths) final { |
| 293 | SkPath& path = paths->push_back(); |
| 294 | if (fJoin == SkPaint::kRound_Join) { |
| 295 | path.reset().moveTo(fPts.back()); |
| 296 | for (size_t i = 0; i < kNumCubicsInChalkboard/fPts.size(); ++i) { |
| 297 | for (size_t j = 0; j < fPts.size(); ++j) { |
| 298 | path.lineTo(fPts[j]); |
| 299 | } |
| 300 | } |
| 301 | } else { |
| 302 | path.reset().moveTo(fPts[0]); |
| 303 | for (int i = 0; i < kNumCubicsInChalkboard/2; ++i) { |
| 304 | if (fPts.size() == 4) { |
| 305 | path.cubicTo(fPts[1], fPts[2], fPts[3]); |
| 306 | path.cubicTo(fPts[2], fPts[1], fPts[0]); |
| 307 | } else { |
| 308 | SkASSERT(fPts.size() == 3); |
| 309 | path.quadTo(fPts[1], fPts[2]); |
| 310 | path.quadTo(fPts[2], fPts[1]); |
| 311 | } |
| 312 | } |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
Chris Dalton | ad48280 | 2020-12-28 09:02:47 -0700 | [diff] [blame] | 316 | const std::vector<SkPoint> fPts; |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 317 | }; |
| 318 | |
Chris Dalton | ad48280 | 2020-12-28 09:02:47 -0700 | [diff] [blame] | 319 | DEF_BENCH( return new StrokeIndirectBenchmark( |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 320 | "_inflect1", SkPaint::kBevel_Join, {{0,0}, {100,0}, {0,100}, {100,100}}); ) |
| 321 | |
Chris Dalton | ad48280 | 2020-12-28 09:02:47 -0700 | [diff] [blame] | 322 | DEF_BENCH( return new StrokeIndirectBenchmark( |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 323 | "_inflect2", SkPaint::kBevel_Join, {{37,162}, {412,160}, {249,65}, {112,360}}); ) |
| 324 | |
Chris Dalton | ad48280 | 2020-12-28 09:02:47 -0700 | [diff] [blame] | 325 | DEF_BENCH( return new StrokeIndirectBenchmark( |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 326 | "_loop", SkPaint::kBevel_Join, {{0,0}, {100,0}, {0,100}, {0,0}}); ) |
| 327 | |
Chris Dalton | ad48280 | 2020-12-28 09:02:47 -0700 | [diff] [blame] | 328 | DEF_BENCH( return new StrokeIndirectBenchmark( |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 329 | "_nochop", SkPaint::kBevel_Join, {{0,0}, {50,0}, {100,50}, {100,100}}); ) |
| 330 | |
Chris Dalton | ad48280 | 2020-12-28 09:02:47 -0700 | [diff] [blame] | 331 | DEF_BENCH( return new StrokeIndirectBenchmark( |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 332 | "_quad", SkPaint::kBevel_Join, {{0,0}, {50,100}, {100,0}}); ) |
| 333 | |
Chris Dalton | ad48280 | 2020-12-28 09:02:47 -0700 | [diff] [blame] | 334 | DEF_BENCH( return new StrokeIndirectBenchmark( |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 335 | "_roundjoin", SkPaint::kRound_Join, {{0,0}, {50,100}, {100,0}}); ) |
Chris Dalton | ad48280 | 2020-12-28 09:02:47 -0700 | [diff] [blame] | 336 | |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 337 | class SingleVerbStrokeIndirectBenchmark : public GrStrokeIndirectTessellator::Benchmark { |
Chris Dalton | ad48280 | 2020-12-28 09:02:47 -0700 | [diff] [blame] | 338 | public: |
| 339 | SingleVerbStrokeIndirectBenchmark(const char* nameSuffix, SkPathVerb verb) |
| 340 | : Benchmark(nameSuffix, SkPaint::kBevel_Join), fVerb(verb) {} |
| 341 | |
| 342 | private: |
| 343 | void setupPaths(SkTArray<SkPath>* paths) override { |
| 344 | SkRandom rand; |
| 345 | for (int i = 0; i < kNumCubicsInChalkboard; ++i) { |
| 346 | switch (fVerb) { |
| 347 | case SkPathVerb::kQuad: |
| 348 | paths->push_back().quadTo(rand.nextF(), rand.nextF(), rand.nextF(), |
| 349 | rand.nextF()); |
| 350 | break; |
| 351 | case SkPathVerb::kCubic: |
| 352 | switch (i % 3) { |
| 353 | case 0: |
| 354 | paths->push_back().cubicTo(100, 0, 0, 100, 100, 100); // 1 inflection. |
| 355 | break; |
| 356 | case 1: |
| 357 | paths->push_back().cubicTo(100, 0, 0, 100, 0, 0); // loop. |
| 358 | break; |
| 359 | case 2: |
| 360 | paths->push_back().cubicTo(50, 0, 100, 50, 100, 100); // no chop. |
| 361 | break; |
| 362 | } |
| 363 | break; |
| 364 | default: |
| 365 | SkUNREACHABLE; |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | const SkPathVerb fVerb; |
| 371 | }; |
| 372 | |
| 373 | DEF_BENCH( return new SingleVerbStrokeIndirectBenchmark("_singlequads", SkPathVerb::kQuad); ) |
| 374 | DEF_BENCH( return new SingleVerbStrokeIndirectBenchmark("_singlecubics", SkPathVerb::kCubic); ) |