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 | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 17 | #include "src/gpu/tessellate/GrStrokeTessellateOp.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 | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 20 | |
| 21 | // This is the number of cubics in desk_chalkboard.skp. (There are no quadratics in the chalkboard.) |
| 22 | constexpr static int kNumCubicsInChalkboard = 47182; |
| 23 | |
Chris Dalton | 90ad0fe | 2020-11-09 14:13:39 -0700 | [diff] [blame] | 24 | static sk_sp<GrDirectContext> make_mock_context() { |
| 25 | GrMockOptions mockOptions; |
| 26 | mockOptions.fDrawInstancedSupport = true; |
| 27 | mockOptions.fMaxTessellationSegments = 64; |
| 28 | mockOptions.fMapBufferFlags = GrCaps::kCanMap_MapFlag; |
| 29 | mockOptions.fConfigOptions[(int)GrColorType::kAlpha_8].fRenderability = |
| 30 | GrMockOptions::ConfigOptions::Renderability::kMSAA; |
| 31 | mockOptions.fConfigOptions[(int)GrColorType::kAlpha_8].fTexturable = true; |
| 32 | mockOptions.fIntegerSupport = true; |
| 33 | |
| 34 | GrContextOptions ctxOptions; |
| 35 | ctxOptions.fGpuPathRenderers = GpuPathRenderers::kTessellation; |
| 36 | |
| 37 | return GrDirectContext::MakeMock(&mockOptions, ctxOptions); |
| 38 | } |
| 39 | |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 40 | static SkPath make_cubic_path() { |
| 41 | SkRandom rand; |
| 42 | SkPath path; |
| 43 | for (int i = 0; i < kNumCubicsInChalkboard/2; ++i) { |
Chris Dalton | 2f2d81c | 2020-05-13 17:57:37 -0600 | [diff] [blame] | 44 | float x = std::ldexp(rand.nextF(), (i % 18)) / 1e3f; |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 45 | path.cubicTo(111.625f*x, 308.188f*x, 764.62f*x, -435.688f*x, 742.63f*x, 85.187f*x); |
| 46 | path.cubicTo(764.62f*x, -435.688f*x, 111.625f*x, 308.188f*x, 0, 0); |
| 47 | } |
| 48 | return path; |
| 49 | } |
| 50 | |
Chris Dalton | 078f875 | 2020-07-30 19:50:46 -0600 | [diff] [blame] | 51 | // This serves as a base class for benchmarking individual methods on GrPathTessellateOp. |
| 52 | class GrPathTessellateOp::TestingOnly_Benchmark : public Benchmark { |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 53 | public: |
| 54 | TestingOnly_Benchmark(const char* subName, SkPath path, const SkMatrix& m) |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 55 | : fOp(m, path, GrPaint(), GrAAType::kMSAA, GrTessellationPathRenderer::OpFlags::kNone) { |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 56 | fName.printf("tessellate_%s", subName); |
| 57 | } |
| 58 | |
| 59 | const char* onGetName() override { return fName.c_str(); } |
| 60 | bool isSuitableFor(Backend backend) final { return backend == kNonRendering_Backend; } |
| 61 | |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 62 | class prepareMiddleOutStencilGeometry; |
| 63 | class prepareMiddleOutStencilGeometry_indirect; |
| 64 | class prepareIndirectOuterCubics; |
| 65 | class prepareTessellatedOuterCubics; |
| 66 | class prepareTessellatedCubicWedges; |
| 67 | class wangs_formula_cubic_log2; |
| 68 | class wangs_formula_cubic_log2_scale; |
| 69 | class wangs_formula_cubic_log2_affine; |
| 70 | class middle_out_triangulation; |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 71 | |
| 72 | private: |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 73 | void onDelayedSetup() override { |
Chris Dalton | 90ad0fe | 2020-11-09 14:13:39 -0700 | [diff] [blame] | 74 | fTarget = std::make_unique<GrMockOpTarget>(make_mock_context()); |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 77 | void onDraw(int loops, SkCanvas*) final { |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 78 | if (!fTarget->mockContext()) { |
Chris Dalton | 1443c9d | 2020-05-27 09:43:34 -0600 | [diff] [blame] | 79 | SkDebugf("ERROR: could not create mock context."); |
| 80 | return; |
| 81 | } |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 82 | for (int i = 0; i < loops; ++i) { |
| 83 | fOp.fTriangleBuffer.reset(); |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 84 | fOp.fTriangleVertexCount = 0; |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 85 | fOp.fPipelineForStencils = nullptr; |
| 86 | fOp.fPipelineForFills = nullptr; |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 87 | fOp.fStencilTrianglesProgram = nullptr; |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 88 | fOp.fFillTrianglesProgram = nullptr; |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 89 | fOp.fCubicBuffer.reset(); |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 90 | fOp.fCubicVertexCount = 0; |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 91 | // Make fStencilCubicsProgram non-null to keep assertions happy. |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 92 | fOp.fStencilCubicsProgram = (GrProgramInfo*)-1; |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 93 | fOp.fFillPathProgram = nullptr; |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 94 | this->runBench(fTarget.get(), &fOp); |
| 95 | fTarget->resetAllocator(); |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
Chris Dalton | 078f875 | 2020-07-30 19:50:46 -0600 | [diff] [blame] | 99 | virtual void runBench(GrMeshDrawOp::Target*, GrPathTessellateOp*) = 0; |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 100 | |
Chris Dalton | 078f875 | 2020-07-30 19:50:46 -0600 | [diff] [blame] | 101 | GrPathTessellateOp fOp; |
Chris Dalton | 90ad0fe | 2020-11-09 14:13:39 -0700 | [diff] [blame] | 102 | std::unique_ptr<GrMockOpTarget> fTarget; |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 103 | SkString fName; |
| 104 | }; |
| 105 | |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 106 | #define DEF_PATH_TESS_BENCH(NAME, PATH, MATRIX, TARGET, OP) \ |
Chris Dalton | 078f875 | 2020-07-30 19:50:46 -0600 | [diff] [blame] | 107 | class GrPathTessellateOp::TestingOnly_Benchmark::NAME \ |
| 108 | : public GrPathTessellateOp::TestingOnly_Benchmark { \ |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 109 | public: \ |
| 110 | NAME() : TestingOnly_Benchmark(#NAME, (PATH), (MATRIX)) {} \ |
Chris Dalton | 078f875 | 2020-07-30 19:50:46 -0600 | [diff] [blame] | 111 | void runBench(GrMeshDrawOp::Target* target, GrPathTessellateOp* op) override; \ |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 112 | }; \ |
Chris Dalton | 078f875 | 2020-07-30 19:50:46 -0600 | [diff] [blame] | 113 | DEF_BENCH( return new GrPathTessellateOp::TestingOnly_Benchmark::NAME(); ); \ |
| 114 | void GrPathTessellateOp::TestingOnly_Benchmark::NAME::runBench( \ |
| 115 | GrMeshDrawOp::Target* TARGET, GrPathTessellateOp* op) |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 116 | |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 117 | DEF_PATH_TESS_BENCH(prepareMiddleOutStencilGeometry, make_cubic_path(), SkMatrix::I(), target, op) { |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 118 | // Make fStencilTrianglesProgram non-null so we benchmark the tessellation path with separate |
| 119 | // triangles. |
| 120 | op->fStencilTrianglesProgram = (GrProgramInfo*)-1; |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 121 | op->prepareMiddleOutTrianglesAndCubics(target); |
| 122 | } |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 123 | |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 124 | DEF_PATH_TESS_BENCH(prepareMiddleOutStencilGeometry_indirect, make_cubic_path(), SkMatrix::I(), |
| 125 | target, op) { |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 126 | GrResolveLevelCounter resolveLevelCounter; |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 127 | op->prepareMiddleOutTrianglesAndCubics(target, &resolveLevelCounter); |
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 | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 130 | DEF_PATH_TESS_BENCH(prepareIndirectOuterCubics, make_cubic_path(), SkMatrix::I(), target, op) { |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 131 | GrResolveLevelCounter resolveLevelCounter; |
| 132 | resolveLevelCounter.reset(op->fPath, SkMatrix::I(), 4); |
| 133 | op->prepareIndirectOuterCubics(target, resolveLevelCounter); |
| 134 | } |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 135 | |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 136 | DEF_PATH_TESS_BENCH(prepareTessellatedOuterCubics, make_cubic_path(), SkMatrix::I(), target, op) { |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 137 | op->prepareTessellatedOuterCubics(target, kNumCubicsInChalkboard); |
| 138 | } |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 139 | |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 140 | DEF_PATH_TESS_BENCH(prepareTessellatedCubicWedges, make_cubic_path(), SkMatrix::I(), target, op) { |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 141 | op->prepareTessellatedCubicWedges(target); |
| 142 | } |
Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 143 | |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 144 | static void benchmark_wangs_formula_cubic_log2(const SkMatrix& matrix, const SkPath& path) { |
| 145 | int sum = 0; |
| 146 | GrVectorXform xform(matrix); |
| 147 | for (auto [verb, pts, w] : SkPathPriv::Iterate(path)) { |
| 148 | if (verb == SkPathVerb::kCubic) { |
| 149 | sum += GrWangsFormula::cubic_log2(4, pts, xform); |
Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 150 | } |
| 151 | } |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 152 | // Don't let the compiler optimize away GrWangsFormula::cubic_log2. |
| 153 | if (sum <= 0) { |
| 154 | SK_ABORT("sum should be > 0."); |
| 155 | } |
| 156 | } |
Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 157 | |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 158 | 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] | 159 | benchmark_wangs_formula_cubic_log2(op->fViewMatrix, op->fPath); |
| 160 | } |
| 161 | |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 162 | DEF_PATH_TESS_BENCH(wangs_formula_cubic_log2_scale, make_cubic_path(), SkMatrix::Scale(1.1f, 0.9f), |
| 163 | target, op) { |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 164 | benchmark_wangs_formula_cubic_log2(op->fViewMatrix, op->fPath); |
| 165 | } |
| 166 | |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 167 | DEF_PATH_TESS_BENCH(wangs_formula_cubic_log2_affine, make_cubic_path(), |
| 168 | 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] | 169 | benchmark_wangs_formula_cubic_log2(op->fViewMatrix, op->fPath); |
| 170 | } |
| 171 | |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 172 | DEF_PATH_TESS_BENCH(middle_out_triangulation, |
| 173 | ToolUtils::make_star(SkRect::MakeWH(500, 500), kNumCubicsInChalkboard), |
| 174 | SkMatrix::I(), target, op) { |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 175 | int baseVertex; |
| 176 | auto vertexData = static_cast<SkPoint*>(target->makeVertexSpace( |
| 177 | sizeof(SkPoint), kNumCubicsInChalkboard, nullptr, &baseVertex)); |
| 178 | GrMiddleOutPolygonTriangulator middleOut(vertexData, 3, kNumCubicsInChalkboard + 2); |
| 179 | for (auto [verb, pts, w] : SkPathPriv::Iterate(op->fPath)) { |
| 180 | switch (verb) { |
| 181 | case SkPathVerb::kMove: |
| 182 | middleOut.closeAndMove(pts[0]); |
| 183 | break; |
| 184 | case SkPathVerb::kLine: |
| 185 | middleOut.pushVertex(pts[1]); |
| 186 | break; |
| 187 | case SkPathVerb::kClose: |
| 188 | middleOut.close(); |
| 189 | break; |
| 190 | case SkPathVerb::kQuad: |
| 191 | case SkPathVerb::kConic: |
| 192 | case SkPathVerb::kCubic: |
| 193 | SkUNREACHABLE; |
| 194 | } |
| 195 | middleOut.closeAndMove(pts[0]); |
| 196 | } |
| 197 | } |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 198 | |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 199 | class GrStrokeTessellateOp::TestingOnly_Benchmark : public Benchmark { |
Chris Dalton | 2882e70 | 2020-11-02 12:43:06 -0700 | [diff] [blame] | 200 | public: |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 201 | TestingOnly_Benchmark(float matrixScale, const char* suffix) : fMatrixScale(matrixScale) { |
| 202 | fName.printf("tessellate_GrStrokeTessellateOp_prepare%s", suffix); |
Chris Dalton | 2882e70 | 2020-11-02 12:43:06 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | private: |
| 206 | const char* onGetName() override { return fName.c_str(); } |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 207 | bool isSuitableFor(Backend backend) final { return backend == kNonRendering_Backend; } |
| 208 | |
| 209 | void onDelayedSetup() override { |
Chris Dalton | 90ad0fe | 2020-11-09 14:13:39 -0700 | [diff] [blame] | 210 | fTarget = std::make_unique<GrMockOpTarget>(make_mock_context()); |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 211 | fPath.reset().moveTo(0, 0); |
| 212 | for (int i = 0; i < kNumCubicsInChalkboard/2; ++i) { |
Chris Dalton | 2882e70 | 2020-11-02 12:43:06 -0700 | [diff] [blame] | 213 | fPath.cubicTo(100, 0, 50, 100, 100, 100); |
| 214 | fPath.cubicTo(0, -100, 200, 100, 0, 0); |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 215 | } |
| 216 | fStrokeRec.setStrokeStyle(8); |
| 217 | fStrokeRec.setStrokeParams(SkPaint::kButt_Cap, SkPaint::kMiter_Join, 4); |
| 218 | } |
| 219 | |
| 220 | void onDraw(int loops, SkCanvas*) final { |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 221 | if (!fTarget->mockContext()) { |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 222 | SkDebugf("ERROR: could not create mock context."); |
| 223 | return; |
| 224 | } |
| 225 | for (int i = 0; i < loops; ++i) { |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 226 | GrStrokeTessellateOp op(GrAAType::kMSAA, SkMatrix::Scale(fMatrixScale, fMatrixScale), |
| 227 | fStrokeRec, fPath, GrPaint()); |
| 228 | op.fTarget = fTarget.get(); |
| 229 | op.prepareBuffers(); |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
Chris Dalton | 2882e70 | 2020-11-02 12:43:06 -0700 | [diff] [blame] | 233 | const float fMatrixScale; |
| 234 | SkString fName; |
Chris Dalton | 90ad0fe | 2020-11-09 14:13:39 -0700 | [diff] [blame] | 235 | std::unique_ptr<GrMockOpTarget> fTarget; |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 236 | SkPath fPath; |
| 237 | SkStrokeRec fStrokeRec = SkStrokeRec(SkStrokeRec::kFill_InitStyle); |
Chris Dalton | e206764 | 2020-09-23 11:07:20 -0600 | [diff] [blame] | 238 | }; |
| 239 | |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 240 | DEF_BENCH( return new GrStrokeTessellateOp::TestingOnly_Benchmark(1, ""); ) |
| 241 | DEF_BENCH( return new GrStrokeTessellateOp::TestingOnly_Benchmark(5, "_one_chop"); ) |