xref: /aosp_15_r20/external/skia/tests/PathOpsOpLoopThreadedTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
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  */
7 #include "include/core/SkPath.h"
8 #include "include/core/SkPoint.h"
9 #include "include/core/SkScalar.h"
10 #include "include/core/SkString.h"
11 #include "include/core/SkTypes.h"
12 #include "include/pathops/SkPathOps.h"
13 #include "include/private/base/SkTDArray.h"
14 #include "tests/PathOpsDebug.h"
15 #include "tests/PathOpsExtendedTest.h"
16 #include "tests/PathOpsThreadedCommon.h"
17 #include "tests/Test.h"
18 
19 #include <atomic>
20 
21 static int loopNo = 17;
22 
add_point(SkString * str,SkScalar x,SkScalar y)23 static void add_point(SkString* str, SkScalar x, SkScalar y) {
24     int asInt = SkScalarRoundToInt(x);
25     if (SkIntToScalar(asInt) == x) {
26         str->appendf("%d", asInt);
27     } else {
28         str->appendf("%1.9gf", x);
29     }
30     str->appendf(",");
31     asInt = SkScalarRoundToInt(y);
32     if (SkIntToScalar(asInt) == y) {
33         str->appendf("%d", asInt);
34     } else {
35         str->appendf("%1.9gf", y);
36     }
37 }
38 
39 static std::atomic<int> gLoopsTestNo{0};
40 
testOpLoopsMain(PathOpsThreadState * data)41 static void testOpLoopsMain(PathOpsThreadState* data) {
42     SkASSERT(data);
43     PathOpsThreadState& state = *data;
44     SkString pathStr;
45     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;
60         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);
67         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");
88             state.outputProgress(pathStr.c_str(), kIntersect_SkPathOp);
89         }
90         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;
94                 }
95             }
96         }
97     }
98 }
99 
DEF_TEST(PathOpsOpLoopsThreaded,reporter)100 DEF_TEST(PathOpsOpLoopsThreaded, reporter) {
101     initializeTests(reporter, "loopOp");
102     PathOpsThreadedTestRunner testRunner(reporter);
103     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) {
107                     *testRunner.fRunnables.append() =
108                             new PathOpsThreadedRunnable(&testOpLoopsMain, a, b, c, d, &testRunner);
109                 }
110             }
111             if (!reporter->allowExtendedTest()) goto finish;
112         }
113     }
114 finish:
115     testRunner.render();
116 }
117