blob: 4b848b73f47cf6e655242c8a8fe0483e1dbeb0bc [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"
Robert Phillipsf0288102020-07-06 13:45:34 -04009#include "include/gpu/GrDirectContext.h"
Chris Daltonf6bf5162020-05-13 19:18:46 -060010#include "src/core/SkPathPriv.h"
Adlai Hollera0693042020-10-14 11:23:11 -040011#include "src/gpu/GrDirectContextPriv.h"
Chris Dalton7f0b8972020-04-23 15:52:24 -060012#include "src/gpu/GrOpFlushState.h"
Chris Dalton90ad0fe2020-11-09 14:13:39 -070013#include "src/gpu/mock/GrMockOpTarget.h"
Chris Daltonb5391d92020-05-24 14:55:54 -060014#include "src/gpu/tessellate/GrMiddleOutPolygonTriangulator.h"
Chris Dalton078f8752020-07-30 19:50:46 -060015#include "src/gpu/tessellate/GrPathTessellateOp.h"
Chris Daltonb5391d92020-05-24 14:55:54 -060016#include "src/gpu/tessellate/GrResolveLevelCounter.h"
Chris Daltonc2a17462020-12-09 16:46:22 -070017#include "src/gpu/tessellate/GrStrokeIndirectOp.h"
Chris Dalton0e543092020-11-03 14:09:16 -070018#include "src/gpu/tessellate/GrStrokeTessellateOp.h"
Chris Daltonf6bf5162020-05-13 19:18:46 -060019#include "src/gpu/tessellate/GrWangsFormula.h"
Chris Daltonf5132a02020-04-27 23:40:03 -060020#include "tools/ToolUtils.h"
Chris Daltonc2a17462020-12-09 16:46:22 -070021#include <vector>
Chris Dalton7f0b8972020-04-23 15:52:24 -060022
23// This is the number of cubics in desk_chalkboard.skp. (There are no quadratics in the chalkboard.)
24constexpr static int kNumCubicsInChalkboard = 47182;
25
Chris Dalton90ad0fe2020-11-09 14:13:39 -070026static 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;
38
39 return GrDirectContext::MakeMock(&mockOptions, ctxOptions);
40}
41
Chris Dalton7f0b8972020-04-23 15:52:24 -060042static SkPath make_cubic_path() {
43 SkRandom rand;
44 SkPath path;
45 for (int i = 0; i < kNumCubicsInChalkboard/2; ++i) {
Chris Dalton2f2d81c2020-05-13 17:57:37 -060046 float x = std::ldexp(rand.nextF(), (i % 18)) / 1e3f;
Chris Dalton7f0b8972020-04-23 15:52:24 -060047 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
Chris Dalton078f8752020-07-30 19:50:46 -060053// This serves as a base class for benchmarking individual methods on GrPathTessellateOp.
54class GrPathTessellateOp::TestingOnly_Benchmark : public Benchmark {
Chris Dalton7f0b8972020-04-23 15:52:24 -060055public:
56 TestingOnly_Benchmark(const char* subName, SkPath path, const SkMatrix& m)
Chris Daltonb96995d2020-06-04 16:44:29 -060057 : fOp(m, path, GrPaint(), GrAAType::kMSAA, GrTessellationPathRenderer::OpFlags::kNone) {
Chris Dalton7f0b8972020-04-23 15:52:24 -060058 fName.printf("tessellate_%s", subName);
59 }
60
61 const char* onGetName() override { return fName.c_str(); }
62 bool isSuitableFor(Backend backend) final { return backend == kNonRendering_Backend; }
63
Chris Daltonb5391d92020-05-24 14:55:54 -060064 class prepareMiddleOutStencilGeometry;
65 class prepareMiddleOutStencilGeometry_indirect;
66 class prepareIndirectOuterCubics;
67 class prepareTessellatedOuterCubics;
68 class prepareTessellatedCubicWedges;
69 class wangs_formula_cubic_log2;
70 class wangs_formula_cubic_log2_scale;
71 class wangs_formula_cubic_log2_affine;
72 class middle_out_triangulation;
Chris Dalton7f0b8972020-04-23 15:52:24 -060073
74private:
Chris Dalton0e543092020-11-03 14:09:16 -070075 void onDelayedSetup() override {
Chris Dalton90ad0fe2020-11-09 14:13:39 -070076 fTarget = std::make_unique<GrMockOpTarget>(make_mock_context());
Chris Dalton0e543092020-11-03 14:09:16 -070077 }
78
Chris Dalton7f0b8972020-04-23 15:52:24 -060079 void onDraw(int loops, SkCanvas*) final {
Chris Dalton0e543092020-11-03 14:09:16 -070080 if (!fTarget->mockContext()) {
Chris Dalton1443c9d2020-05-27 09:43:34 -060081 SkDebugf("ERROR: could not create mock context.");
82 return;
83 }
Chris Dalton7f0b8972020-04-23 15:52:24 -060084 for (int i = 0; i < loops; ++i) {
85 fOp.fTriangleBuffer.reset();
Chris Daltone74cebe2020-09-24 09:32:54 -060086 fOp.fTriangleVertexCount = 0;
Chris Dalton5e1545f2020-09-25 16:24:03 -060087 fOp.fPipelineForStencils = nullptr;
88 fOp.fPipelineForFills = nullptr;
Chris Daltone74cebe2020-09-24 09:32:54 -060089 fOp.fStencilTrianglesProgram = nullptr;
Chris Dalton5e1545f2020-09-25 16:24:03 -060090 fOp.fFillTrianglesProgram = nullptr;
Chris Dalton7f0b8972020-04-23 15:52:24 -060091 fOp.fCubicBuffer.reset();
Chris Daltone74cebe2020-09-24 09:32:54 -060092 fOp.fCubicVertexCount = 0;
Chris Dalton5e1545f2020-09-25 16:24:03 -060093 // Make fStencilCubicsProgram non-null to keep assertions happy.
Chris Daltone74cebe2020-09-24 09:32:54 -060094 fOp.fStencilCubicsProgram = (GrProgramInfo*)-1;
Chris Dalton5e1545f2020-09-25 16:24:03 -060095 fOp.fFillPathProgram = nullptr;
Chris Dalton0e543092020-11-03 14:09:16 -070096 this->runBench(fTarget.get(), &fOp);
97 fTarget->resetAllocator();
Chris Dalton7f0b8972020-04-23 15:52:24 -060098 }
99 }
100
Chris Dalton078f8752020-07-30 19:50:46 -0600101 virtual void runBench(GrMeshDrawOp::Target*, GrPathTessellateOp*) = 0;
Chris Dalton7f0b8972020-04-23 15:52:24 -0600102
Chris Dalton078f8752020-07-30 19:50:46 -0600103 GrPathTessellateOp fOp;
Chris Dalton90ad0fe2020-11-09 14:13:39 -0700104 std::unique_ptr<GrMockOpTarget> fTarget;
Chris Dalton7f0b8972020-04-23 15:52:24 -0600105 SkString fName;
106};
107
Chris Daltone2067642020-09-23 11:07:20 -0600108#define DEF_PATH_TESS_BENCH(NAME, PATH, MATRIX, TARGET, OP) \
Chris Dalton078f8752020-07-30 19:50:46 -0600109 class GrPathTessellateOp::TestingOnly_Benchmark::NAME \
110 : public GrPathTessellateOp::TestingOnly_Benchmark { \
Chris Daltonb5391d92020-05-24 14:55:54 -0600111 public: \
112 NAME() : TestingOnly_Benchmark(#NAME, (PATH), (MATRIX)) {} \
Chris Dalton078f8752020-07-30 19:50:46 -0600113 void runBench(GrMeshDrawOp::Target* target, GrPathTessellateOp* op) override; \
Chris Daltonb5391d92020-05-24 14:55:54 -0600114 }; \
Chris Dalton078f8752020-07-30 19:50:46 -0600115 DEF_BENCH( return new GrPathTessellateOp::TestingOnly_Benchmark::NAME(); ); \
116 void GrPathTessellateOp::TestingOnly_Benchmark::NAME::runBench( \
117 GrMeshDrawOp::Target* TARGET, GrPathTessellateOp* op)
Chris Dalton7f0b8972020-04-23 15:52:24 -0600118
Chris Daltone2067642020-09-23 11:07:20 -0600119DEF_PATH_TESS_BENCH(prepareMiddleOutStencilGeometry, make_cubic_path(), SkMatrix::I(), target, op) {
Chris Daltone74cebe2020-09-24 09:32:54 -0600120 // Make fStencilTrianglesProgram non-null so we benchmark the tessellation path with separate
121 // triangles.
122 op->fStencilTrianglesProgram = (GrProgramInfo*)-1;
Chris Daltonb5391d92020-05-24 14:55:54 -0600123 op->prepareMiddleOutTrianglesAndCubics(target);
124}
Chris Dalton7f0b8972020-04-23 15:52:24 -0600125
Chris Daltone2067642020-09-23 11:07:20 -0600126DEF_PATH_TESS_BENCH(prepareMiddleOutStencilGeometry_indirect, make_cubic_path(), SkMatrix::I(),
127 target, op) {
Chris Daltonb5391d92020-05-24 14:55:54 -0600128 GrResolveLevelCounter resolveLevelCounter;
Chris Daltone74cebe2020-09-24 09:32:54 -0600129 op->prepareMiddleOutTrianglesAndCubics(target, &resolveLevelCounter);
Chris Daltonb5391d92020-05-24 14:55:54 -0600130}
Chris Dalton7f0b8972020-04-23 15:52:24 -0600131
Chris Daltone2067642020-09-23 11:07:20 -0600132DEF_PATH_TESS_BENCH(prepareIndirectOuterCubics, make_cubic_path(), SkMatrix::I(), target, op) {
Chris Daltonb5391d92020-05-24 14:55:54 -0600133 GrResolveLevelCounter resolveLevelCounter;
134 resolveLevelCounter.reset(op->fPath, SkMatrix::I(), 4);
135 op->prepareIndirectOuterCubics(target, resolveLevelCounter);
136}
Chris Dalton7f0b8972020-04-23 15:52:24 -0600137
Chris Daltone2067642020-09-23 11:07:20 -0600138DEF_PATH_TESS_BENCH(prepareTessellatedOuterCubics, make_cubic_path(), SkMatrix::I(), target, op) {
Chris Daltonb5391d92020-05-24 14:55:54 -0600139 op->prepareTessellatedOuterCubics(target, kNumCubicsInChalkboard);
140}
Chris Dalton7f0b8972020-04-23 15:52:24 -0600141
Chris Daltone2067642020-09-23 11:07:20 -0600142DEF_PATH_TESS_BENCH(prepareTessellatedCubicWedges, make_cubic_path(), SkMatrix::I(), target, op) {
Chris Daltonb5391d92020-05-24 14:55:54 -0600143 op->prepareTessellatedCubicWedges(target);
144}
Chris Daltonf6bf5162020-05-13 19:18:46 -0600145
Chris Daltonb5391d92020-05-24 14:55:54 -0600146static void benchmark_wangs_formula_cubic_log2(const SkMatrix& matrix, const SkPath& path) {
147 int sum = 0;
148 GrVectorXform xform(matrix);
149 for (auto [verb, pts, w] : SkPathPriv::Iterate(path)) {
150 if (verb == SkPathVerb::kCubic) {
151 sum += GrWangsFormula::cubic_log2(4, pts, xform);
Chris Daltonf6bf5162020-05-13 19:18:46 -0600152 }
153 }
Chris Daltonb5391d92020-05-24 14:55:54 -0600154 // Don't let the compiler optimize away GrWangsFormula::cubic_log2.
155 if (sum <= 0) {
156 SK_ABORT("sum should be > 0.");
157 }
158}
Chris Daltonf6bf5162020-05-13 19:18:46 -0600159
Chris Daltone2067642020-09-23 11:07:20 -0600160DEF_PATH_TESS_BENCH(wangs_formula_cubic_log2, make_cubic_path(), SkMatrix::I(), target, op) {
Chris Daltonb5391d92020-05-24 14:55:54 -0600161 benchmark_wangs_formula_cubic_log2(op->fViewMatrix, op->fPath);
162}
163
Chris Daltone2067642020-09-23 11:07:20 -0600164DEF_PATH_TESS_BENCH(wangs_formula_cubic_log2_scale, make_cubic_path(), SkMatrix::Scale(1.1f, 0.9f),
165 target, op) {
Chris Daltonb5391d92020-05-24 14:55:54 -0600166 benchmark_wangs_formula_cubic_log2(op->fViewMatrix, op->fPath);
167}
168
Chris Daltone2067642020-09-23 11:07:20 -0600169DEF_PATH_TESS_BENCH(wangs_formula_cubic_log2_affine, make_cubic_path(),
170 SkMatrix::MakeAll(.9f,0.9f,0, 1.1f,1.1f,0, 0,0,1), target, op) {
Chris Daltonb5391d92020-05-24 14:55:54 -0600171 benchmark_wangs_formula_cubic_log2(op->fViewMatrix, op->fPath);
172}
173
Chris Daltone2067642020-09-23 11:07:20 -0600174DEF_PATH_TESS_BENCH(middle_out_triangulation,
175 ToolUtils::make_star(SkRect::MakeWH(500, 500), kNumCubicsInChalkboard),
176 SkMatrix::I(), target, op) {
Chris Daltonb5391d92020-05-24 14:55:54 -0600177 int baseVertex;
178 auto vertexData = static_cast<SkPoint*>(target->makeVertexSpace(
179 sizeof(SkPoint), kNumCubicsInChalkboard, nullptr, &baseVertex));
180 GrMiddleOutPolygonTriangulator middleOut(vertexData, 3, kNumCubicsInChalkboard + 2);
181 for (auto [verb, pts, w] : SkPathPriv::Iterate(op->fPath)) {
182 switch (verb) {
183 case SkPathVerb::kMove:
184 middleOut.closeAndMove(pts[0]);
185 break;
186 case SkPathVerb::kLine:
187 middleOut.pushVertex(pts[1]);
188 break;
189 case SkPathVerb::kClose:
190 middleOut.close();
191 break;
192 case SkPathVerb::kQuad:
193 case SkPathVerb::kConic:
194 case SkPathVerb::kCubic:
195 SkUNREACHABLE;
196 }
197 middleOut.closeAndMove(pts[0]);
198 }
199}
Chris Daltone2067642020-09-23 11:07:20 -0600200
Chris Dalton0e543092020-11-03 14:09:16 -0700201class GrStrokeTessellateOp::TestingOnly_Benchmark : public Benchmark {
Chris Dalton2882e702020-11-02 12:43:06 -0700202public:
Chris Dalton0e543092020-11-03 14:09:16 -0700203 TestingOnly_Benchmark(float matrixScale, const char* suffix) : fMatrixScale(matrixScale) {
204 fName.printf("tessellate_GrStrokeTessellateOp_prepare%s", suffix);
Chris Dalton2882e702020-11-02 12:43:06 -0700205 }
206
207private:
208 const char* onGetName() override { return fName.c_str(); }
Chris Daltone2067642020-09-23 11:07:20 -0600209 bool isSuitableFor(Backend backend) final { return backend == kNonRendering_Backend; }
210
211 void onDelayedSetup() override {
Chris Dalton90ad0fe2020-11-09 14:13:39 -0700212 fTarget = std::make_unique<GrMockOpTarget>(make_mock_context());
Chris Daltone2067642020-09-23 11:07:20 -0600213 fPath.reset().moveTo(0, 0);
214 for (int i = 0; i < kNumCubicsInChalkboard/2; ++i) {
Chris Dalton2882e702020-11-02 12:43:06 -0700215 fPath.cubicTo(100, 0, 50, 100, 100, 100);
216 fPath.cubicTo(0, -100, 200, 100, 0, 0);
Chris Daltone2067642020-09-23 11:07:20 -0600217 }
218 fStrokeRec.setStrokeStyle(8);
219 fStrokeRec.setStrokeParams(SkPaint::kButt_Cap, SkPaint::kMiter_Join, 4);
220 }
221
222 void onDraw(int loops, SkCanvas*) final {
Chris Dalton0e543092020-11-03 14:09:16 -0700223 if (!fTarget->mockContext()) {
Chris Daltone2067642020-09-23 11:07:20 -0600224 SkDebugf("ERROR: could not create mock context.");
225 return;
226 }
227 for (int i = 0; i < loops; ++i) {
Chris Dalton0e543092020-11-03 14:09:16 -0700228 GrStrokeTessellateOp op(GrAAType::kMSAA, SkMatrix::Scale(fMatrixScale, fMatrixScale),
229 fStrokeRec, fPath, GrPaint());
230 op.fTarget = fTarget.get();
231 op.prepareBuffers();
Chris Daltone2067642020-09-23 11:07:20 -0600232 }
233 }
234
Chris Dalton2882e702020-11-02 12:43:06 -0700235 const float fMatrixScale;
236 SkString fName;
Chris Dalton90ad0fe2020-11-09 14:13:39 -0700237 std::unique_ptr<GrMockOpTarget> fTarget;
Chris Daltone2067642020-09-23 11:07:20 -0600238 SkPath fPath;
239 SkStrokeRec fStrokeRec = SkStrokeRec(SkStrokeRec::kFill_InitStyle);
Chris Daltone2067642020-09-23 11:07:20 -0600240};
241
Chris Dalton0e543092020-11-03 14:09:16 -0700242DEF_BENCH( return new GrStrokeTessellateOp::TestingOnly_Benchmark(1, ""); )
243DEF_BENCH( return new GrStrokeTessellateOp::TestingOnly_Benchmark(5, "_one_chop"); )
Chris Daltonc2a17462020-12-09 16:46:22 -0700244
245class GrStrokeIndirectOp::Benchmark : public ::Benchmark {
246public:
247 Benchmark(const char* nameSuffix, SkPaint::Join join, std::vector<SkPoint> pts)
248 : fJoin(join), fPts(std::move(pts)) {
249 fName.printf("tessellate_GrStrokeIndirectOpBench%s", nameSuffix);
250 }
251
252private:
253 const char* onGetName() override { return fName.c_str(); }
254 bool isSuitableFor(Backend backend) final { return backend == kNonRendering_Backend; }
255
256 void onDelayedSetup() override {
257 fTarget = std::make_unique<GrMockOpTarget>(make_mock_context());
258 if (fJoin == SkPaint::kRound_Join) {
259 fPath.reset().moveTo(fPts.back());
260 for (size_t i = 0; i < kNumCubicsInChalkboard/fPts.size(); ++i) {
261 for (size_t j = 0; j < fPts.size(); ++j) {
262 fPath.lineTo(fPts[j]);
263 }
264 }
265 } else {
266 fPath.reset().moveTo(fPts[0]);
267 for (int i = 0; i < kNumCubicsInChalkboard/2; ++i) {
268 if (fPts.size() == 4) {
269 fPath.cubicTo(fPts[1], fPts[2], fPts[3]);
270 fPath.cubicTo(fPts[2], fPts[1], fPts[0]);
271 } else {
272 SkASSERT(fPts.size() == 3);
273 fPath.quadTo(fPts[1], fPts[2]);
274 fPath.quadTo(fPts[2], fPts[1]);
275 }
276 }
277 }
278 fStrokeRec.setStrokeStyle(8);
279 fStrokeRec.setStrokeParams(SkPaint::kButt_Cap, fJoin, 4);
280 }
281
282 void onDraw(int loops, SkCanvas*) final {
283 if (!fTarget->mockContext()) {
284 SkDebugf("ERROR: could not create mock context.");
285 return;
286 }
287 for (int i = 0; i < loops; ++i) {
288 GrStrokeIndirectOp op(GrAAType::kMSAA, SkMatrix::I(), fPath, fStrokeRec, GrPaint());
289 op.prePrepareResolveLevels(fTarget->allocator());
290 op.prepareBuffers(fTarget.get());
291 }
292 }
293
294 SkString fName;
295 SkPaint::Join fJoin;
296 std::vector<SkPoint> fPts;
297 std::unique_ptr<GrMockOpTarget> fTarget;
298 SkPath fPath;
299 SkStrokeRec fStrokeRec = SkStrokeRec(SkStrokeRec::kFill_InitStyle);
300};
301
302DEF_BENCH( return new GrStrokeIndirectOp::Benchmark(
303 "_inflect1", SkPaint::kBevel_Join, {{0,0}, {100,0}, {0,100}, {100,100}}); )
304
305DEF_BENCH( return new GrStrokeIndirectOp::Benchmark(
306 "_inflect2", SkPaint::kBevel_Join, {{37,162}, {412,160}, {249,65}, {112,360}}); )
307
308DEF_BENCH( return new GrStrokeIndirectOp::Benchmark(
309 "_loop", SkPaint::kBevel_Join, {{0,0}, {100,0}, {0,100}, {0,0}}); )
310
311DEF_BENCH( return new GrStrokeIndirectOp::Benchmark(
312 "_nochop", SkPaint::kBevel_Join, {{0,0}, {50,0}, {100,50}, {100,100}}); )
313
314DEF_BENCH( return new GrStrokeIndirectOp::Benchmark(
315 "_quad", SkPaint::kBevel_Join, {{0,0}, {50,100}, {100,0}}); )
316
317DEF_BENCH( return new GrStrokeIndirectOp::Benchmark(
318 "_roundjoin", SkPaint::kRound_Join, {{0,0}, {50,100}, {100,0}}); )