blob: f1849c38e0098ff290fe2621acc6fa3fa88b7cee [file] [log] [blame]
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +00001/*
2 * Copyright 2014 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 */
Kevin Lubick8c73a592022-10-17 15:25:35 -04007#include "include/core/SkPath.h"
8#include "include/core/SkPoint.h"
9#include "include/core/SkScalar.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkString.h"
Kevin Lubick8c73a592022-10-17 15:25:35 -040011#include "include/core/SkTypes.h"
12#include "include/pathops/SkPathOps.h"
Kevin Lubickdc6cc022023-01-13 11:24:27 -050013#include "include/private/base/SkTDArray.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "tests/PathOpsDebug.h"
15#include "tests/PathOpsExtendedTest.h"
16#include "tests/PathOpsThreadedCommon.h"
Kevin Lubick8c73a592022-10-17 15:25:35 -040017#include "tests/Test.h"
Hal Canary8a001442018-09-19 11:31:27 -040018
Cary Clark8af4c402018-08-08 23:22:37 -040019#include <atomic>
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +000020
caryclark8f186432016-10-06 11:46:25 -070021static int loopNo = 17;
22
23static void add_point(SkString* str, SkScalar x, SkScalar y) {
caryclark54359292015-03-26 07:52:43 -070024 int asInt = SkScalarRoundToInt(x);
25 if (SkIntToScalar(asInt) == x) {
caryclark8f186432016-10-06 11:46:25 -070026 str->appendf("%d", asInt);
caryclark54359292015-03-26 07:52:43 -070027 } else {
caryclark8f186432016-10-06 11:46:25 -070028 str->appendf("%1.9gf", x);
caryclark54359292015-03-26 07:52:43 -070029 }
caryclark8f186432016-10-06 11:46:25 -070030 str->appendf(",");
caryclark54359292015-03-26 07:52:43 -070031 asInt = SkScalarRoundToInt(y);
32 if (SkIntToScalar(asInt) == y) {
caryclark8f186432016-10-06 11:46:25 -070033 str->appendf("%d", asInt);
caryclark54359292015-03-26 07:52:43 -070034 } else {
caryclark8f186432016-10-06 11:46:25 -070035 str->appendf("%1.9gf", y);
caryclark54359292015-03-26 07:52:43 -070036 }
caryclark54359292015-03-26 07:52:43 -070037}
38
Cary Clark8af4c402018-08-08 23:22:37 -040039static std::atomic<int> gLoopsTestNo{0};
Cary Clark4533f3d2018-08-08 09:48:09 -040040
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +000041static void testOpLoopsMain(PathOpsThreadState* data) {
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +000042 SkASSERT(data);
43 PathOpsThreadState& state = *data;
caryclark8f186432016-10-06 11:46:25 -070044 SkString pathStr;
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +000045 for (int a = 0 ; a < 6; ++a) {
46 for (int b = a + 1 ; b < 7; ++b) {
47 for (int c = 0 ; c < 6; ++c) {
48 for (int d = c + 1 ; d < 7; ++d) {
49 // define 4 points that form two lines that often cross; one line is (a, b) (c, d)
50 SkVector v = {SkIntToScalar(a - c), SkIntToScalar(b - d)};
51 SkPoint midA = { SkIntToScalar(a * state.fA + c * (6 - state.fA)) / 6,
52 SkIntToScalar(b * state.fA + d * (6 - state.fA)) / 6 };
53 SkPoint midB = { SkIntToScalar(a * state.fB + c * (6 - state.fB)) / 6,
54 SkIntToScalar(b * state.fB + d * (6 - state.fB)) / 6 };
55 SkPoint endC = { midA.fX + v.fY * state.fC / 3,
56 midA.fY + v.fX * state.fC / 3 };
57 SkPoint endD = { midB.fX - v.fY * state.fD / 3,
58 midB.fY + v.fX * state.fD / 3 };
59 SkPath pathA, pathB;
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +000060 pathA.moveTo(SkIntToScalar(a), SkIntToScalar(b));
61 pathA.cubicTo(SkIntToScalar(c), SkIntToScalar(d), endC.fX, endC.fY, endD.fX, endD.fY);
62 pathA.close();
63 pathB.moveTo(SkIntToScalar(c), SkIntToScalar(d));
64 pathB.cubicTo(endC.fX, endC.fY, endD.fX, endD.fY, SkIntToScalar(a), SkIntToScalar(b));
65 pathB.close();
66// SkDebugf("%s\n", pathStr);
caryclark8f186432016-10-06 11:46:25 -070067 if (state.fReporter->verbose()) {
68 pathStr.printf("static void loop%d(skiatest::Reporter* reporter,"
69 " const char* filename) {\n", loopNo);
70 pathStr.appendf(" SkPath path, pathB;\n");
71 pathStr.appendf(" path.moveTo(%d,%d);\n", a, b);
72 pathStr.appendf(" path.cubicTo(%d,%d, ", c, d);
73 add_point(&pathStr, endC.fX, endC.fY);
74 pathStr.appendf(", ");
75 add_point(&pathStr, endD.fX, endD.fY);
76 pathStr.appendf(");\n");
77 pathStr.appendf(" path.close();\n");
78 pathStr.appendf(" pathB.moveTo(%d,%d);\n", c, d);
79 pathStr.appendf(" pathB.cubicTo(");
80 add_point(&pathStr, endC.fX, endC.fY);
81 pathStr.appendf(", ");
82 add_point(&pathStr, endD.fX, endD.fY);
83 pathStr.appendf(", %d,%d);\n", a, b);
84 pathStr.appendf(" pathB.close();\n");
85 pathStr.appendf(" testPathOp(reporter, path, pathB, kIntersect_SkPathOp,"
86 " filename);\n");
87 pathStr.appendf("}\n");
Mike Reedff80c2a2017-01-07 11:16:28 -050088 state.outputProgress(pathStr.c_str(), kIntersect_SkPathOp);
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +000089 }
Cary Clark4533f3d2018-08-08 09:48:09 -040090 SkString testName;
91 testName.printf("thread_loops%d", ++gLoopsTestNo);
92 testPathOp(state.fReporter, pathA, pathB, kIntersect_SkPathOp, testName.c_str());
93 if (PathOpsDebug::gCheckForDuplicateNames) return;
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +000094 }
95 }
96 }
97 }
98}
99
100DEF_TEST(PathOpsOpLoopsThreaded, reporter) {
caryclark1049f122015-04-20 08:31:59 -0700101 initializeTests(reporter, "loopOp");
mtklein406654b2014-09-03 15:34:37 -0700102 PathOpsThreadedTestRunner testRunner(reporter);
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000103 for (int a = 0; a < 6; ++a) { // outermost
104 for (int b = a + 1; b < 7; ++b) {
105 for (int c = 0 ; c < 6; ++c) {
106 for (int d = c + 1; d < 7; ++d) {
halcanary385fe4d2015-08-26 13:07:48 -0700107 *testRunner.fRunnables.append() =
108 new PathOpsThreadedRunnable(&testOpLoopsMain, a, b, c, d, &testRunner);
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000109 }
110 }
111 if (!reporter->allowExtendedTest()) goto finish;
112 }
113 }
114finish:
115 testRunner.render();
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000116}