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" |
| 9 | #include "include/gpu/GrContext.h" |
Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 10 | #include "src/core/SkPathPriv.h" |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 11 | #include "src/gpu/GrContextPriv.h" |
| 12 | #include "src/gpu/GrOpFlushState.h" |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 13 | #include "src/gpu/tessellate/GrMiddleOutPolygonTriangulator.h" |
| 14 | #include "src/gpu/tessellate/GrResolveLevelCounter.h" |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 15 | #include "src/gpu/tessellate/GrTessellatePathOp.h" |
Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 16 | #include "src/gpu/tessellate/GrWangsFormula.h" |
Chris Dalton | f5132a0 | 2020-04-27 23:40:03 -0600 | [diff] [blame] | 17 | #include "tools/ToolUtils.h" |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 18 | |
| 19 | // This is the number of cubics in desk_chalkboard.skp. (There are no quadratics in the chalkboard.) |
| 20 | constexpr static int kNumCubicsInChalkboard = 47182; |
| 21 | |
| 22 | static SkPath make_cubic_path() { |
| 23 | SkRandom rand; |
| 24 | SkPath path; |
| 25 | for (int i = 0; i < kNumCubicsInChalkboard/2; ++i) { |
Chris Dalton | 2f2d81c | 2020-05-13 17:57:37 -0600 | [diff] [blame] | 26 | float x = std::ldexp(rand.nextF(), (i % 18)) / 1e3f; |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 27 | path.cubicTo(111.625f*x, 308.188f*x, 764.62f*x, -435.688f*x, 742.63f*x, 85.187f*x); |
| 28 | path.cubicTo(764.62f*x, -435.688f*x, 111.625f*x, 308.188f*x, 0, 0); |
| 29 | } |
| 30 | return path; |
| 31 | } |
| 32 | |
Chris Dalton | 2f2d81c | 2020-05-13 17:57:37 -0600 | [diff] [blame] | 33 | // This is a dummy GrMeshDrawOp::Target implementation that just gives back pointers into |
| 34 | // pre-allocated CPU buffers, rather than allocating and mapping GPU buffers. |
| 35 | class BenchmarkTarget : public GrMeshDrawOp::Target { |
| 36 | public: |
Chris Dalton | 1443c9d | 2020-05-27 09:43:34 -0600 | [diff] [blame] | 37 | BenchmarkTarget() { |
| 38 | GrMockOptions mockOptions; |
| 39 | mockOptions.fDrawInstancedSupport = true; |
| 40 | mockOptions.fTessellationSupport = true; |
| 41 | mockOptions.fMapBufferFlags = GrCaps::kCanMap_MapFlag; |
| 42 | mockOptions.fConfigOptions[(int)GrColorType::kAlpha_8].fRenderability = |
| 43 | GrMockOptions::ConfigOptions::Renderability::kMSAA; |
| 44 | mockOptions.fConfigOptions[(int)GrColorType::kAlpha_8].fTexturable = true; |
| 45 | mockOptions.fIntegerSupport = true; |
| 46 | |
| 47 | GrContextOptions ctxOptions; |
| 48 | ctxOptions.fGpuPathRenderers = GpuPathRenderers::kTessellation; |
| 49 | |
| 50 | fMockContext = GrContext::MakeMock(&mockOptions, ctxOptions); |
| 51 | } |
| 52 | const GrContext* mockContext() const { return fMockContext.get(); } |
| 53 | const GrCaps& caps() const override { return *fMockContext->priv().caps(); } |
| 54 | GrResourceProvider* resourceProvider() const override { |
| 55 | return fMockContext->priv().resourceProvider(); |
| 56 | } |
Chris Dalton | 2f2d81c | 2020-05-13 17:57:37 -0600 | [diff] [blame] | 57 | void resetAllocator() { fAllocator.reset(); } |
| 58 | SkArenaAlloc* allocator() override { return &fAllocator; } |
| 59 | void putBackVertices(int vertices, size_t vertexStride) override { /* no-op */ } |
| 60 | |
| 61 | void* makeVertexSpace(size_t vertexSize, int vertexCount, sk_sp<const GrBuffer>*, |
| 62 | int* startVertex) override { |
| 63 | if (vertexSize * vertexCount > sizeof(fStaticVertexData)) { |
| 64 | SK_ABORT(SkStringPrintf( |
| 65 | "FATAL: wanted %zu bytes of static vertex data; only have %zu.\n", |
| 66 | vertexSize * vertexCount, SK_ARRAY_COUNT(fStaticVertexData)).c_str()); |
| 67 | } |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 68 | *startVertex = 0; |
Chris Dalton | 2f2d81c | 2020-05-13 17:57:37 -0600 | [diff] [blame] | 69 | return fStaticVertexData; |
| 70 | } |
| 71 | |
| 72 | GrDrawIndexedIndirectCommand* makeDrawIndexedIndirectSpace( |
| 73 | int drawCount, sk_sp<const GrBuffer>* buffer, size_t* offsetInBytes) override { |
| 74 | int staticBufferCount = (int)SK_ARRAY_COUNT(fStaticDrawIndexedIndirectData); |
| 75 | if (drawCount > staticBufferCount) { |
| 76 | SK_ABORT(SkStringPrintf( |
| 77 | "FATAL: wanted %i static drawIndexedIndirect elements; only have %i.\n", |
| 78 | drawCount, staticBufferCount).c_str()); |
| 79 | } |
| 80 | return fStaticDrawIndexedIndirectData; |
| 81 | } |
| 82 | |
| 83 | #define UNIMPL(...) __VA_ARGS__ override { SK_ABORT("unimplemented."); } |
| 84 | UNIMPL(void recordDraw(const GrGeometryProcessor*, const GrSimpleMesh[], int, |
| 85 | const GrSurfaceProxy* const[], GrPrimitiveType)) |
| 86 | UNIMPL(uint16_t* makeIndexSpace(int, sk_sp<const GrBuffer>*, int*)) |
| 87 | UNIMPL(void* makeVertexSpaceAtLeast(size_t, int, int, sk_sp<const GrBuffer>*, int*, int*)) |
| 88 | UNIMPL(uint16_t* makeIndexSpaceAtLeast(int, int, sk_sp<const GrBuffer>*, int*, int*)) |
| 89 | UNIMPL(GrDrawIndirectCommand* makeDrawIndirectSpace(int, sk_sp<const GrBuffer>*, size_t*)) |
| 90 | UNIMPL(void putBackIndices(int)) |
| 91 | UNIMPL(GrRenderTargetProxy* proxy() const) |
| 92 | UNIMPL(const GrSurfaceProxyView* writeView() const) |
| 93 | UNIMPL(const GrAppliedClip* appliedClip() const) |
| 94 | UNIMPL(GrAppliedClip detachAppliedClip()) |
| 95 | UNIMPL(const GrXferProcessor::DstProxyView& dstProxyView() const) |
Chris Dalton | 2f2d81c | 2020-05-13 17:57:37 -0600 | [diff] [blame] | 96 | UNIMPL(GrStrikeCache* strikeCache() const) |
| 97 | UNIMPL(GrAtlasManager* atlasManager() const) |
| 98 | UNIMPL(SkTArray<GrSurfaceProxy*, true>* sampledProxyArray()) |
Chris Dalton | 2f2d81c | 2020-05-13 17:57:37 -0600 | [diff] [blame] | 99 | UNIMPL(GrDeferredUploadTarget* deferredUploadTarget()) |
| 100 | #undef UNIMPL |
| 101 | |
| 102 | private: |
Chris Dalton | 1443c9d | 2020-05-27 09:43:34 -0600 | [diff] [blame] | 103 | sk_sp<GrContext> fMockContext; |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 104 | SkPoint fStaticVertexData[(kNumCubicsInChalkboard + 2) * 8]; |
Chris Dalton | 2f2d81c | 2020-05-13 17:57:37 -0600 | [diff] [blame] | 105 | GrDrawIndexedIndirectCommand fStaticDrawIndexedIndirectData[32]; |
| 106 | SkSTArenaAlloc<1024 * 1024> fAllocator; |
| 107 | }; |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 108 | |
| 109 | // This serves as a base class for benchmarking individual methods on GrTessellatePathOp. |
| 110 | class GrTessellatePathOp::TestingOnly_Benchmark : public Benchmark { |
| 111 | public: |
| 112 | TestingOnly_Benchmark(const char* subName, SkPath path, const SkMatrix& m) |
Chris Dalton | 2f2d81c | 2020-05-13 17:57:37 -0600 | [diff] [blame] | 113 | : fOp(m, path, GrPaint(), GrAAType::kMSAA) { |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 114 | fName.printf("tessellate_%s", subName); |
| 115 | } |
| 116 | |
| 117 | const char* onGetName() override { return fName.c_str(); } |
| 118 | bool isSuitableFor(Backend backend) final { return backend == kNonRendering_Backend; } |
| 119 | |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 120 | class prepareMiddleOutStencilGeometry; |
| 121 | class prepareMiddleOutStencilGeometry_indirect; |
| 122 | class prepareIndirectOuterCubics; |
| 123 | class prepareTessellatedOuterCubics; |
| 124 | class prepareTessellatedCubicWedges; |
| 125 | class wangs_formula_cubic_log2; |
| 126 | class wangs_formula_cubic_log2_scale; |
| 127 | class wangs_formula_cubic_log2_affine; |
| 128 | class middle_out_triangulation; |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 129 | |
| 130 | private: |
| 131 | void onDraw(int loops, SkCanvas*) final { |
Chris Dalton | 1443c9d | 2020-05-27 09:43:34 -0600 | [diff] [blame] | 132 | if (!fTarget.mockContext()) { |
| 133 | SkDebugf("ERROR: could not create mock context."); |
| 134 | return; |
| 135 | } |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 136 | for (int i = 0; i < loops; ++i) { |
| 137 | fOp.fTriangleBuffer.reset(); |
| 138 | fOp.fDoStencilTriangleBuffer = false; |
| 139 | fOp.fDoFillTriangleBuffer = false; |
| 140 | fOp.fCubicBuffer.reset(); |
| 141 | fOp.fStencilCubicsShader = nullptr; |
Chris Dalton | 2f2d81c | 2020-05-13 17:57:37 -0600 | [diff] [blame] | 142 | this->runBench(&fTarget, &fOp); |
| 143 | fTarget.resetAllocator(); |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
Chris Dalton | 2f2d81c | 2020-05-13 17:57:37 -0600 | [diff] [blame] | 147 | virtual void runBench(GrMeshDrawOp::Target*, GrTessellatePathOp*) = 0; |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 148 | |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 149 | GrTessellatePathOp fOp; |
Chris Dalton | 2f2d81c | 2020-05-13 17:57:37 -0600 | [diff] [blame] | 150 | BenchmarkTarget fTarget; |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 151 | SkString fName; |
| 152 | }; |
| 153 | |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 154 | #define DEF_TESS_BENCH(NAME, PATH, MATRIX, TARGET, OP) \ |
| 155 | class GrTessellatePathOp::TestingOnly_Benchmark::NAME \ |
| 156 | : public GrTessellatePathOp::TestingOnly_Benchmark { \ |
| 157 | public: \ |
| 158 | NAME() : TestingOnly_Benchmark(#NAME, (PATH), (MATRIX)) {} \ |
| 159 | void runBench(GrMeshDrawOp::Target* target, GrTessellatePathOp* op) override; \ |
| 160 | }; \ |
| 161 | DEF_BENCH( return new GrTessellatePathOp::TestingOnly_Benchmark::NAME(); ); \ |
| 162 | void GrTessellatePathOp::TestingOnly_Benchmark::NAME::runBench( \ |
| 163 | GrMeshDrawOp::Target* TARGET, GrTessellatePathOp* op) |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 164 | |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 165 | DEF_TESS_BENCH(prepareMiddleOutStencilGeometry, make_cubic_path(), SkMatrix::I(), target, op) { |
| 166 | op->prepareMiddleOutTrianglesAndCubics(target); |
| 167 | } |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 168 | |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 169 | DEF_TESS_BENCH(prepareMiddleOutStencilGeometry_indirect, make_cubic_path(), SkMatrix::I(), target, |
| 170 | op) { |
| 171 | GrResolveLevelCounter resolveLevelCounter; |
| 172 | op->prepareMiddleOutTrianglesAndCubics(target, &resolveLevelCounter, true); |
| 173 | } |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 174 | |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 175 | DEF_TESS_BENCH(prepareIndirectOuterCubics, make_cubic_path(), SkMatrix::I(), target, op) { |
| 176 | GrResolveLevelCounter resolveLevelCounter; |
| 177 | resolveLevelCounter.reset(op->fPath, SkMatrix::I(), 4); |
| 178 | op->prepareIndirectOuterCubics(target, resolveLevelCounter); |
| 179 | } |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 180 | |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 181 | DEF_TESS_BENCH(prepareTessellatedOuterCubics, make_cubic_path(), SkMatrix::I(), target, op) { |
| 182 | op->prepareTessellatedOuterCubics(target, kNumCubicsInChalkboard); |
| 183 | } |
Chris Dalton | 7f0b897 | 2020-04-23 15:52:24 -0600 | [diff] [blame] | 184 | |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 185 | DEF_TESS_BENCH(prepareTessellatedCubicWedges, make_cubic_path(), SkMatrix::I(), target, op) { |
| 186 | op->prepareTessellatedCubicWedges(target); |
| 187 | } |
Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 188 | |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 189 | static void benchmark_wangs_formula_cubic_log2(const SkMatrix& matrix, const SkPath& path) { |
| 190 | int sum = 0; |
| 191 | GrVectorXform xform(matrix); |
| 192 | for (auto [verb, pts, w] : SkPathPriv::Iterate(path)) { |
| 193 | if (verb == SkPathVerb::kCubic) { |
| 194 | sum += GrWangsFormula::cubic_log2(4, pts, xform); |
Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 195 | } |
| 196 | } |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 197 | // Don't let the compiler optimize away GrWangsFormula::cubic_log2. |
| 198 | if (sum <= 0) { |
| 199 | SK_ABORT("sum should be > 0."); |
| 200 | } |
| 201 | } |
Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 202 | |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 203 | DEF_TESS_BENCH(wangs_formula_cubic_log2, make_cubic_path(), SkMatrix::I(), target, op) { |
| 204 | benchmark_wangs_formula_cubic_log2(op->fViewMatrix, op->fPath); |
| 205 | } |
| 206 | |
| 207 | DEF_TESS_BENCH(wangs_formula_cubic_log2_scale, make_cubic_path(), SkMatrix::Scale(1.1f, 0.9f), |
| 208 | target, op) { |
| 209 | benchmark_wangs_formula_cubic_log2(op->fViewMatrix, op->fPath); |
| 210 | } |
| 211 | |
| 212 | DEF_TESS_BENCH(wangs_formula_cubic_log2_affine, make_cubic_path(), |
| 213 | SkMatrix::MakeAll(.9f,0.9f,0, 1.1f,1.1f,0, 0,0,1), target, op) { |
| 214 | benchmark_wangs_formula_cubic_log2(op->fViewMatrix, op->fPath); |
| 215 | } |
| 216 | |
| 217 | DEF_TESS_BENCH(middle_out_triangulation, |
| 218 | ToolUtils::make_star(SkRect::MakeWH(500, 500), kNumCubicsInChalkboard), |
| 219 | SkMatrix::I(), target, op) { |
| 220 | int baseVertex; |
| 221 | auto vertexData = static_cast<SkPoint*>(target->makeVertexSpace( |
| 222 | sizeof(SkPoint), kNumCubicsInChalkboard, nullptr, &baseVertex)); |
| 223 | GrMiddleOutPolygonTriangulator middleOut(vertexData, 3, kNumCubicsInChalkboard + 2); |
| 224 | for (auto [verb, pts, w] : SkPathPriv::Iterate(op->fPath)) { |
| 225 | switch (verb) { |
| 226 | case SkPathVerb::kMove: |
| 227 | middleOut.closeAndMove(pts[0]); |
| 228 | break; |
| 229 | case SkPathVerb::kLine: |
| 230 | middleOut.pushVertex(pts[1]); |
| 231 | break; |
| 232 | case SkPathVerb::kClose: |
| 233 | middleOut.close(); |
| 234 | break; |
| 235 | case SkPathVerb::kQuad: |
| 236 | case SkPathVerb::kConic: |
| 237 | case SkPathVerb::kCubic: |
| 238 | SkUNREACHABLE; |
| 239 | } |
| 240 | middleOut.closeAndMove(pts[0]); |
| 241 | } |
| 242 | } |