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 | 078f875 | 2020-07-30 19:50:46 -0600 | [diff] [blame] | 15 | #include "src/gpu/tessellate/GrPathTessellateOp.h" |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 16 | #include "src/gpu/tessellate/GrResolveLevelCounter.h" |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 17 | #include "src/gpu/tessellate/GrStrokeIndirectOp.h" |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 18 | #include "src/gpu/tessellate/GrStrokeTessellateOp.h" |
Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 19 | #include "src/gpu/tessellate/GrWangsFormula.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 | |
| 23 | // This is the number of cubics in desk_chalkboard.skp. (There are no quadratics in the chalkboard.) |
| 24 | constexpr static int kNumCubicsInChalkboard = 47182; |
| 25 | |
Chris Dalton | 90ad0fe | 2020-11-09 14:13:39 -0700 | [diff] [blame] | 26 | static sk_sp<GrDirectContext> make_mock_context() { |
| 27 | GrMockOptions mockOptions; |
| 28 | mockOptions.fDrawInstancedSupport = true; |
| 29 | mockOptions.fMaxTessellationSegments = 64; |
| 30 | mockOptions.fMapBufferFlags = GrCaps::kCanMap_MapFlag; |
| 31 | mockOptions.fConfigOptions[(int)GrColorType::kAlpha_8].fRenderability = |
| 32 | GrMockOptions::ConfigOptions::Renderability::kMSAA; |
| 33 | mockOptions.fConfigOptions[(int)GrColorType::kAlpha_8].fTexturable = true; |
| 34 | mockOptions.fIntegerSupport = true; |
| 35 | |
| 36 | GrContextOptions ctxOptions; |
| 37 | ctxOptions.fGpuPathRenderers = GpuPathRenderers::kTessellation; |
Chris Dalton | 15f5184 | 2020-12-15 19:55:10 -0700 | [diff] [blame] | 38 | ctxOptions.fSuppressTessellationShaders = false; |
Chris Dalton | 90ad0fe | 2020-11-09 14:13:39 -0700 | [diff] [blame] | 39 | |
| 40 | return GrDirectContext::MakeMock(&mockOptions, ctxOptions); |
| 41 | } |
| 42 | |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 43 | static SkPath make_cubic_path() { |
| 44 | SkRandom rand; |
| 45 | SkPath path; |
| 46 | for (int i = 0; i < kNumCubicsInChalkboard/2; ++i) { |
Chris Dalton | 2f2d81c | 2020-05-13 17:57:37 -0600 | [diff] [blame] | 47 | float x = std::ldexp(rand.nextF(), (i % 18)) / 1e3f; |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 48 | path.cubicTo(111.625f*x, 308.188f*x, 764.62f*x, -435.688f*x, 742.63f*x, 85.187f*x); |
| 49 | path.cubicTo(764.62f*x, -435.688f*x, 111.625f*x, 308.188f*x, 0, 0); |
| 50 | } |
| 51 | return path; |
| 52 | } |
| 53 | |
Chris Dalton | 078f875 | 2020-07-30 19:50:46 -0600 | [diff] [blame] | 54 | // This serves as a base class for benchmarking individual methods on GrPathTessellateOp. |
| 55 | class GrPathTessellateOp::TestingOnly_Benchmark : public Benchmark { |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 56 | public: |
| 57 | TestingOnly_Benchmark(const char* subName, SkPath path, const SkMatrix& m) |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 58 | : fOp(m, path, GrPaint(), GrAAType::kMSAA, GrTessellationPathRenderer::OpFlags::kNone) { |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 59 | fName.printf("tessellate_%s", subName); |
| 60 | } |
| 61 | |
| 62 | const char* onGetName() override { return fName.c_str(); } |
| 63 | bool isSuitableFor(Backend backend) final { return backend == kNonRendering_Backend; } |
| 64 | |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 65 | class prepareMiddleOutStencilGeometry; |
| 66 | class prepareMiddleOutStencilGeometry_indirect; |
| 67 | class prepareIndirectOuterCubics; |
| 68 | class prepareTessellatedOuterCubics; |
| 69 | class prepareTessellatedCubicWedges; |
| 70 | class wangs_formula_cubic_log2; |
| 71 | class wangs_formula_cubic_log2_scale; |
| 72 | class wangs_formula_cubic_log2_affine; |
| 73 | class middle_out_triangulation; |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 74 | |
| 75 | private: |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 76 | void onDelayedSetup() override { |
Chris Dalton | 90ad0fe | 2020-11-09 14:13:39 -0700 | [diff] [blame] | 77 | fTarget = std::make_unique<GrMockOpTarget>(make_mock_context()); |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 80 | void onDraw(int loops, SkCanvas*) final { |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 81 | if (!fTarget->mockContext()) { |
Chris Dalton | 1443c9d | 2020-05-27 09:43:34 -0600 | [diff] [blame] | 82 | SkDebugf("ERROR: could not create mock context."); |
| 83 | return; |
| 84 | } |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 85 | for (int i = 0; i < loops; ++i) { |
| 86 | fOp.fTriangleBuffer.reset(); |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 87 | fOp.fTriangleVertexCount = 0; |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 88 | fOp.fPipelineForStencils = nullptr; |
| 89 | fOp.fPipelineForFills = nullptr; |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 90 | fOp.fStencilTrianglesProgram = nullptr; |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 91 | fOp.fFillTrianglesProgram = nullptr; |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 92 | fOp.fCubicBuffer.reset(); |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 93 | fOp.fCubicVertexCount = 0; |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 94 | // Make fStencilCubicsProgram non-null to keep assertions happy. |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 95 | fOp.fStencilCubicsProgram = (GrProgramInfo*)-1; |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 96 | fOp.fFillPathProgram = nullptr; |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 97 | this->runBench(fTarget.get(), &fOp); |
| 98 | fTarget->resetAllocator(); |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
Chris Dalton | 078f875 | 2020-07-30 19:50:46 -0600 | [diff] [blame] | 102 | virtual void runBench(GrMeshDrawOp::Target*, GrPathTessellateOp*) = 0; |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 103 | |
Chris Dalton | 078f875 | 2020-07-30 19:50:46 -0600 | [diff] [blame] | 104 | GrPathTessellateOp fOp; |
Chris Dalton | 90ad0fe | 2020-11-09 14:13:39 -0700 | [diff] [blame] | 105 | std::unique_ptr<GrMockOpTarget> fTarget; |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 106 | SkString fName; |
| 107 | }; |
| 108 | |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 109 | #define DEF_PATH_TESS_BENCH(NAME, PATH, MATRIX, TARGET, OP) \ |
Chris Dalton | 078f875 | 2020-07-30 19:50:46 -0600 | [diff] [blame] | 110 | class GrPathTessellateOp::TestingOnly_Benchmark::NAME \ |
| 111 | : public GrPathTessellateOp::TestingOnly_Benchmark { \ |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 112 | public: \ |
| 113 | NAME() : TestingOnly_Benchmark(#NAME, (PATH), (MATRIX)) {} \ |
Chris Dalton | 078f875 | 2020-07-30 19:50:46 -0600 | [diff] [blame] | 114 | void runBench(GrMeshDrawOp::Target* target, GrPathTessellateOp* op) override; \ |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 115 | }; \ |
Chris Dalton | 078f875 | 2020-07-30 19:50:46 -0600 | [diff] [blame] | 116 | DEF_BENCH( return new GrPathTessellateOp::TestingOnly_Benchmark::NAME(); ); \ |
| 117 | void GrPathTessellateOp::TestingOnly_Benchmark::NAME::runBench( \ |
| 118 | GrMeshDrawOp::Target* TARGET, GrPathTessellateOp* op) |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 119 | |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 120 | DEF_PATH_TESS_BENCH(prepareMiddleOutStencilGeometry, make_cubic_path(), SkMatrix::I(), target, op) { |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 121 | // Make fStencilTrianglesProgram non-null so we benchmark the tessellation path with separate |
| 122 | // triangles. |
| 123 | op->fStencilTrianglesProgram = (GrProgramInfo*)-1; |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 124 | op->prepareMiddleOutTrianglesAndCubics(target); |
| 125 | } |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 126 | |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 127 | DEF_PATH_TESS_BENCH(prepareMiddleOutStencilGeometry_indirect, make_cubic_path(), SkMatrix::I(), |
| 128 | target, op) { |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 129 | GrResolveLevelCounter resolveLevelCounter; |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 130 | op->prepareMiddleOutTrianglesAndCubics(target, &resolveLevelCounter); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 131 | } |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 132 | |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 133 | DEF_PATH_TESS_BENCH(prepareIndirectOuterCubics, make_cubic_path(), SkMatrix::I(), target, op) { |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 134 | GrResolveLevelCounter resolveLevelCounter; |
| 135 | resolveLevelCounter.reset(op->fPath, SkMatrix::I(), 4); |
| 136 | op->prepareIndirectOuterCubics(target, resolveLevelCounter); |
| 137 | } |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 138 | |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 139 | DEF_PATH_TESS_BENCH(prepareTessellatedOuterCubics, make_cubic_path(), SkMatrix::I(), target, op) { |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 140 | op->prepareTessellatedOuterCubics(target, kNumCubicsInChalkboard); |
| 141 | } |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 142 | |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 143 | DEF_PATH_TESS_BENCH(prepareTessellatedCubicWedges, make_cubic_path(), SkMatrix::I(), target, op) { |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 144 | op->prepareTessellatedCubicWedges(target); |
| 145 | } |
Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 146 | |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 147 | static void benchmark_wangs_formula_cubic_log2(const SkMatrix& matrix, const SkPath& path) { |
| 148 | int sum = 0; |
| 149 | GrVectorXform xform(matrix); |
| 150 | for (auto [verb, pts, w] : SkPathPriv::Iterate(path)) { |
| 151 | if (verb == SkPathVerb::kCubic) { |
| 152 | sum += GrWangsFormula::cubic_log2(4, pts, xform); |
Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 153 | } |
| 154 | } |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 155 | // Don't let the compiler optimize away GrWangsFormula::cubic_log2. |
| 156 | if (sum <= 0) { |
| 157 | SK_ABORT("sum should be > 0."); |
| 158 | } |
| 159 | } |
Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 160 | |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 161 | DEF_PATH_TESS_BENCH(wangs_formula_cubic_log2, make_cubic_path(), SkMatrix::I(), target, op) { |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 162 | benchmark_wangs_formula_cubic_log2(op->fViewMatrix, op->fPath); |
| 163 | } |
| 164 | |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 165 | DEF_PATH_TESS_BENCH(wangs_formula_cubic_log2_scale, make_cubic_path(), SkMatrix::Scale(1.1f, 0.9f), |
| 166 | target, op) { |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 167 | benchmark_wangs_formula_cubic_log2(op->fViewMatrix, op->fPath); |
| 168 | } |
| 169 | |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 170 | DEF_PATH_TESS_BENCH(wangs_formula_cubic_log2_affine, make_cubic_path(), |
| 171 | SkMatrix::MakeAll(.9f,0.9f,0, 1.1f,1.1f,0, 0,0,1), target, op) { |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 172 | benchmark_wangs_formula_cubic_log2(op->fViewMatrix, op->fPath); |
| 173 | } |
| 174 | |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 175 | DEF_PATH_TESS_BENCH(middle_out_triangulation, |
| 176 | ToolUtils::make_star(SkRect::MakeWH(500, 500), kNumCubicsInChalkboard), |
| 177 | SkMatrix::I(), target, op) { |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 178 | int baseVertex; |
| 179 | auto vertexData = static_cast<SkPoint*>(target->makeVertexSpace( |
| 180 | sizeof(SkPoint), kNumCubicsInChalkboard, nullptr, &baseVertex)); |
| 181 | GrMiddleOutPolygonTriangulator middleOut(vertexData, 3, kNumCubicsInChalkboard + 2); |
| 182 | for (auto [verb, pts, w] : SkPathPriv::Iterate(op->fPath)) { |
| 183 | switch (verb) { |
| 184 | case SkPathVerb::kMove: |
| 185 | middleOut.closeAndMove(pts[0]); |
| 186 | break; |
| 187 | case SkPathVerb::kLine: |
| 188 | middleOut.pushVertex(pts[1]); |
| 189 | break; |
| 190 | case SkPathVerb::kClose: |
| 191 | middleOut.close(); |
| 192 | break; |
| 193 | case SkPathVerb::kQuad: |
| 194 | case SkPathVerb::kConic: |
| 195 | case SkPathVerb::kCubic: |
| 196 | SkUNREACHABLE; |
| 197 | } |
| 198 | middleOut.closeAndMove(pts[0]); |
| 199 | } |
| 200 | } |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 201 | |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 202 | class GrStrokeTessellateOp::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) { |
| 205 | fName.printf("tessellate_GrStrokeTessellateOp_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 | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 229 | GrStrokeTessellateOp op(GrAAType::kMSAA, SkMatrix::Scale(fMatrixScale, fMatrixScale), |
| 230 | fStrokeRec, fPath, GrPaint()); |
| 231 | op.fTarget = fTarget.get(); |
| 232 | op.prepareBuffers(); |
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 | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 243 | DEF_BENCH( return new GrStrokeTessellateOp::TestingOnly_Benchmark(1, ""); ) |
| 244 | DEF_BENCH( return new GrStrokeTessellateOp::TestingOnly_Benchmark(5, "_one_chop"); ) |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 245 | |
| 246 | class GrStrokeIndirectOp::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 | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 249 | fName.printf("tessellate_GrStrokeIndirectOpBench%s", nameSuffix); |
| 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) { |
| 270 | GrStrokeIndirectOp op(GrAAType::kMSAA, SkMatrix::I(), path, fStrokeRec, GrPaint()); |
| 271 | op.prePrepareResolveLevels(fTarget->allocator()); |
| 272 | op.prepareBuffers(fTarget.get()); |
| 273 | } |
| 274 | fTarget->resetAllocator(); |
| 275 | } |
| 276 | } |
| 277 | virtual void setupPaths(SkTArray<SkPath>*) = 0; |
| 278 | |
| 279 | SkString fName; |
| 280 | std::unique_ptr<GrMockOpTarget> fTarget; |
| 281 | SkTArray<SkPath> fPaths; |
| 282 | SkStrokeRec fStrokeRec{SkStrokeRec::kHairline_InitStyle}; |
| 283 | }; |
| 284 | |
| 285 | class StrokeIndirectBenchmark : public GrStrokeIndirectOp::Benchmark { |
| 286 | public: |
| 287 | StrokeIndirectBenchmark(const char* nameSuffix, SkPaint::Join join, std::vector<SkPoint> pts) |
| 288 | : Benchmark(nameSuffix, join), fPts(std::move(pts)) {} |
| 289 | |
| 290 | private: |
| 291 | void setupPaths(SkTArray<SkPath>* paths) final { |
| 292 | SkPath& path = paths->push_back(); |
| 293 | if (fJoin == SkPaint::kRound_Join) { |
| 294 | path.reset().moveTo(fPts.back()); |
| 295 | for (size_t i = 0; i < kNumCubicsInChalkboard/fPts.size(); ++i) { |
| 296 | for (size_t j = 0; j < fPts.size(); ++j) { |
| 297 | path.lineTo(fPts[j]); |
| 298 | } |
| 299 | } |
| 300 | } else { |
| 301 | path.reset().moveTo(fPts[0]); |
| 302 | for (int i = 0; i < kNumCubicsInChalkboard/2; ++i) { |
| 303 | if (fPts.size() == 4) { |
| 304 | path.cubicTo(fPts[1], fPts[2], fPts[3]); |
| 305 | path.cubicTo(fPts[2], fPts[1], fPts[0]); |
| 306 | } else { |
| 307 | SkASSERT(fPts.size() == 3); |
| 308 | path.quadTo(fPts[1], fPts[2]); |
| 309 | path.quadTo(fPts[2], fPts[1]); |
| 310 | } |
| 311 | } |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
Chris Dalton | ad48280 | 2020-12-28 09:02:47 -0700 | [diff] [blame] | 315 | const std::vector<SkPoint> fPts; |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 316 | }; |
| 317 | |
Chris Dalton | ad48280 | 2020-12-28 09:02:47 -0700 | [diff] [blame] | 318 | DEF_BENCH( return new StrokeIndirectBenchmark( |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 319 | "_inflect1", SkPaint::kBevel_Join, {{0,0}, {100,0}, {0,100}, {100,100}}); ) |
| 320 | |
Chris Dalton | ad48280 | 2020-12-28 09:02:47 -0700 | [diff] [blame] | 321 | DEF_BENCH( return new StrokeIndirectBenchmark( |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 322 | "_inflect2", SkPaint::kBevel_Join, {{37,162}, {412,160}, {249,65}, {112,360}}); ) |
| 323 | |
Chris Dalton | ad48280 | 2020-12-28 09:02:47 -0700 | [diff] [blame] | 324 | DEF_BENCH( return new StrokeIndirectBenchmark( |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 325 | "_loop", SkPaint::kBevel_Join, {{0,0}, {100,0}, {0,100}, {0,0}}); ) |
| 326 | |
Chris Dalton | ad48280 | 2020-12-28 09:02:47 -0700 | [diff] [blame] | 327 | DEF_BENCH( return new StrokeIndirectBenchmark( |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 328 | "_nochop", SkPaint::kBevel_Join, {{0,0}, {50,0}, {100,50}, {100,100}}); ) |
| 329 | |
Chris Dalton | ad48280 | 2020-12-28 09:02:47 -0700 | [diff] [blame] | 330 | DEF_BENCH( return new StrokeIndirectBenchmark( |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 331 | "_quad", SkPaint::kBevel_Join, {{0,0}, {50,100}, {100,0}}); ) |
| 332 | |
Chris Dalton | ad48280 | 2020-12-28 09:02:47 -0700 | [diff] [blame] | 333 | DEF_BENCH( return new StrokeIndirectBenchmark( |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 334 | "_roundjoin", SkPaint::kRound_Join, {{0,0}, {50,100}, {100,0}}); ) |
Chris Dalton | ad48280 | 2020-12-28 09:02:47 -0700 | [diff] [blame] | 335 | |
| 336 | class SingleVerbStrokeIndirectBenchmark : public GrStrokeIndirectOp::Benchmark { |
| 337 | public: |
| 338 | SingleVerbStrokeIndirectBenchmark(const char* nameSuffix, SkPathVerb verb) |
| 339 | : Benchmark(nameSuffix, SkPaint::kBevel_Join), fVerb(verb) {} |
| 340 | |
| 341 | private: |
| 342 | void setupPaths(SkTArray<SkPath>* paths) override { |
| 343 | SkRandom rand; |
| 344 | for (int i = 0; i < kNumCubicsInChalkboard; ++i) { |
| 345 | switch (fVerb) { |
| 346 | case SkPathVerb::kQuad: |
| 347 | paths->push_back().quadTo(rand.nextF(), rand.nextF(), rand.nextF(), |
| 348 | rand.nextF()); |
| 349 | break; |
| 350 | case SkPathVerb::kCubic: |
| 351 | switch (i % 3) { |
| 352 | case 0: |
| 353 | paths->push_back().cubicTo(100, 0, 0, 100, 100, 100); // 1 inflection. |
| 354 | break; |
| 355 | case 1: |
| 356 | paths->push_back().cubicTo(100, 0, 0, 100, 0, 0); // loop. |
| 357 | break; |
| 358 | case 2: |
| 359 | paths->push_back().cubicTo(50, 0, 100, 50, 100, 100); // no chop. |
| 360 | break; |
| 361 | } |
| 362 | break; |
| 363 | default: |
| 364 | SkUNREACHABLE; |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | const SkPathVerb fVerb; |
| 370 | }; |
| 371 | |
| 372 | DEF_BENCH( return new SingleVerbStrokeIndirectBenchmark("_singlequads", SkPathVerb::kQuad); ) |
| 373 | DEF_BENCH( return new SingleVerbStrokeIndirectBenchmark("_singlecubics", SkPathVerb::kCubic); ) |