blob: d22fe2b00ea2e0ace78ba9c588b01f12d2f8b457 [file] [log] [blame]
Chris Dalton7f0b8972020-04-23 15:52:24 -06001/*
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 Daltonf6bf5162020-05-13 19:18:46 -060010#include "src/core/SkPathPriv.h"
Chris Dalton7f0b8972020-04-23 15:52:24 -060011#include "src/gpu/GrContextPriv.h"
12#include "src/gpu/GrOpFlushState.h"
Chris Daltonb5391d92020-05-24 14:55:54 -060013#include "src/gpu/tessellate/GrMiddleOutPolygonTriangulator.h"
14#include "src/gpu/tessellate/GrResolveLevelCounter.h"
Chris Dalton7f0b8972020-04-23 15:52:24 -060015#include "src/gpu/tessellate/GrTessellatePathOp.h"
Chris Daltonf6bf5162020-05-13 19:18:46 -060016#include "src/gpu/tessellate/GrWangsFormula.h"
Chris Daltonf5132a02020-04-27 23:40:03 -060017#include "tools/ToolUtils.h"
Chris Dalton7f0b8972020-04-23 15:52:24 -060018
19// This is the number of cubics in desk_chalkboard.skp. (There are no quadratics in the chalkboard.)
20constexpr static int kNumCubicsInChalkboard = 47182;
21
22static SkPath make_cubic_path() {
23 SkRandom rand;
24 SkPath path;
25 for (int i = 0; i < kNumCubicsInChalkboard/2; ++i) {
Chris Dalton2f2d81c2020-05-13 17:57:37 -060026 float x = std::ldexp(rand.nextF(), (i % 18)) / 1e3f;
Chris Dalton7f0b8972020-04-23 15:52:24 -060027 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 Dalton2f2d81c2020-05-13 17:57:37 -060033// 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.
35class BenchmarkTarget : public GrMeshDrawOp::Target {
36public:
Chris Dalton1443c9d2020-05-27 09:43:34 -060037 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 Dalton2f2d81c2020-05-13 17:57:37 -060057 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 Daltonb5391d92020-05-24 14:55:54 -060068 *startVertex = 0;
Chris Dalton2f2d81c2020-05-13 17:57:37 -060069 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 Dalton2f2d81c2020-05-13 17:57:37 -060096 UNIMPL(GrStrikeCache* strikeCache() const)
97 UNIMPL(GrAtlasManager* atlasManager() const)
98 UNIMPL(SkTArray<GrSurfaceProxy*, true>* sampledProxyArray())
Chris Dalton2f2d81c2020-05-13 17:57:37 -060099 UNIMPL(GrDeferredUploadTarget* deferredUploadTarget())
100#undef UNIMPL
101
102private:
Chris Dalton1443c9d2020-05-27 09:43:34 -0600103 sk_sp<GrContext> fMockContext;
Chris Daltonb5391d92020-05-24 14:55:54 -0600104 SkPoint fStaticVertexData[(kNumCubicsInChalkboard + 2) * 8];
Chris Dalton2f2d81c2020-05-13 17:57:37 -0600105 GrDrawIndexedIndirectCommand fStaticDrawIndexedIndirectData[32];
106 SkSTArenaAlloc<1024 * 1024> fAllocator;
107};
Chris Dalton7f0b8972020-04-23 15:52:24 -0600108
109// This serves as a base class for benchmarking individual methods on GrTessellatePathOp.
110class GrTessellatePathOp::TestingOnly_Benchmark : public Benchmark {
111public:
112 TestingOnly_Benchmark(const char* subName, SkPath path, const SkMatrix& m)
Chris Dalton2f2d81c2020-05-13 17:57:37 -0600113 : fOp(m, path, GrPaint(), GrAAType::kMSAA) {
Chris Dalton7f0b8972020-04-23 15:52:24 -0600114 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 Daltonb5391d92020-05-24 14:55:54 -0600120 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 Dalton7f0b8972020-04-23 15:52:24 -0600129
130private:
131 void onDraw(int loops, SkCanvas*) final {
Chris Dalton1443c9d2020-05-27 09:43:34 -0600132 if (!fTarget.mockContext()) {
133 SkDebugf("ERROR: could not create mock context.");
134 return;
135 }
Chris Dalton7f0b8972020-04-23 15:52:24 -0600136 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 Dalton2f2d81c2020-05-13 17:57:37 -0600142 this->runBench(&fTarget, &fOp);
143 fTarget.resetAllocator();
Chris Dalton7f0b8972020-04-23 15:52:24 -0600144 }
145 }
146
Chris Dalton2f2d81c2020-05-13 17:57:37 -0600147 virtual void runBench(GrMeshDrawOp::Target*, GrTessellatePathOp*) = 0;
Chris Dalton7f0b8972020-04-23 15:52:24 -0600148
Chris Dalton7f0b8972020-04-23 15:52:24 -0600149 GrTessellatePathOp fOp;
Chris Dalton2f2d81c2020-05-13 17:57:37 -0600150 BenchmarkTarget fTarget;
Chris Dalton7f0b8972020-04-23 15:52:24 -0600151 SkString fName;
152};
153
Chris Daltonb5391d92020-05-24 14:55:54 -0600154#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 Dalton7f0b8972020-04-23 15:52:24 -0600164
Chris Daltonb5391d92020-05-24 14:55:54 -0600165DEF_TESS_BENCH(prepareMiddleOutStencilGeometry, make_cubic_path(), SkMatrix::I(), target, op) {
166 op->prepareMiddleOutTrianglesAndCubics(target);
167}
Chris Dalton7f0b8972020-04-23 15:52:24 -0600168
Chris Daltonb5391d92020-05-24 14:55:54 -0600169DEF_TESS_BENCH(prepareMiddleOutStencilGeometry_indirect, make_cubic_path(), SkMatrix::I(), target,
170 op) {
171 GrResolveLevelCounter resolveLevelCounter;
172 op->prepareMiddleOutTrianglesAndCubics(target, &resolveLevelCounter, true);
173}
Chris Dalton7f0b8972020-04-23 15:52:24 -0600174
Chris Daltonb5391d92020-05-24 14:55:54 -0600175DEF_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 Dalton7f0b8972020-04-23 15:52:24 -0600180
Chris Daltonb5391d92020-05-24 14:55:54 -0600181DEF_TESS_BENCH(prepareTessellatedOuterCubics, make_cubic_path(), SkMatrix::I(), target, op) {
182 op->prepareTessellatedOuterCubics(target, kNumCubicsInChalkboard);
183}
Chris Dalton7f0b8972020-04-23 15:52:24 -0600184
Chris Daltonb5391d92020-05-24 14:55:54 -0600185DEF_TESS_BENCH(prepareTessellatedCubicWedges, make_cubic_path(), SkMatrix::I(), target, op) {
186 op->prepareTessellatedCubicWedges(target);
187}
Chris Daltonf6bf5162020-05-13 19:18:46 -0600188
Chris Daltonb5391d92020-05-24 14:55:54 -0600189static 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 Daltonf6bf5162020-05-13 19:18:46 -0600195 }
196 }
Chris Daltonb5391d92020-05-24 14:55:54 -0600197 // Don't let the compiler optimize away GrWangsFormula::cubic_log2.
198 if (sum <= 0) {
199 SK_ABORT("sum should be > 0.");
200 }
201}
Chris Daltonf6bf5162020-05-13 19:18:46 -0600202
Chris Daltonb5391d92020-05-24 14:55:54 -0600203DEF_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
207DEF_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
212DEF_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
217DEF_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}