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 "PathOpsExtendedTest.h"
8#include "PathOpsThreadedCommon.h"
9
10static int add_point(char* str, SkScalar x, SkScalar y) {
11    int result;
12    int asInt = SkScalarRoundToInt(x);
13    if (SkIntToScalar(asInt) == x) {
14        result = sprintf(str, "%d", asInt);
15    } else {
16        result = sprintf(str, "%1.9gf", x);
17    }
18    result += sprintf(str + result, ",");
19    asInt = SkScalarRoundToInt(y);
20    if (SkIntToScalar(asInt) == y) {
21        result += sprintf(str + result, "%d", asInt);
22    } else {
23        result += sprintf(str + result, "%1.9gf", y);
24    }
25    return result;
26}
27
28static void testOpLoopsMain(PathOpsThreadState* data) {
29#if DEBUG_SHOW_TEST_NAME
30    strncpy(DEBUG_FILENAME_STRING, "", DEBUG_FILENAME_STRING_LENGTH);
31#endif
32    SkASSERT(data);
33    PathOpsThreadState& state = *data;
34    char pathStr[1024];  // gdb: set print elements 400
35    bool progress = state.fReporter->verbose(); // FIXME: break out into its own parameter?
36    if (progress) {
37        sk_bzero(pathStr, sizeof(pathStr));
38    }
39    for (int a = 0 ; a < 6; ++a) {
40        for (int b = a + 1 ; b < 7; ++b) {
41            for (int c = 0 ; c < 6; ++c) {
42                for (int d = c + 1 ; d < 7; ++d) {
43        // define 4 points that form two lines that often cross; one line is (a, b) (c, d)
44        SkVector v = {SkIntToScalar(a - c), SkIntToScalar(b - d)};
45        SkPoint midA = { SkIntToScalar(a * state.fA + c * (6 - state.fA)) / 6,
46                         SkIntToScalar(b * state.fA + d * (6 - state.fA)) / 6 };
47        SkPoint midB = { SkIntToScalar(a * state.fB + c * (6 - state.fB)) / 6,
48                         SkIntToScalar(b * state.fB + d * (6 - state.fB)) / 6 };
49        SkPoint endC = { midA.fX + v.fY * state.fC / 3,
50                          midA.fY + v.fX * state.fC / 3 };
51        SkPoint endD = { midB.fX - v.fY * state.fD / 3,
52                          midB.fY + v.fX * state.fD / 3 };
53        SkPath pathA, pathB;
54        if (progress) {
55            char* str = pathStr;
56            const int loopNo = 17;
57            str += sprintf(str, "static void loop%d(skiatest::Reporter* reporter,"
58                    " const char* filename) {\n", loopNo);
59            str += sprintf(str, "    SkPath path, pathB;\n");
60            str += sprintf(str, "    path.moveTo(%d,%d);\n", a, b);
61            str += sprintf(str, "    path.cubicTo(%d,%d, ", c, d);
62            str += add_point(str, endC.fX, endC.fY);
63            str += sprintf(str, ", ");
64            str += add_point(str, endD.fX, endD.fY);
65            str += sprintf(str, ");\n");
66            str += sprintf(str, "    path.close();\n");
67            str += sprintf(str, "    pathB.moveTo(%d,%d);\n", c, d);
68            str += sprintf(str, "    pathB.cubicTo(");
69            str += add_point(str, endC.fX, endC.fY);
70            str += sprintf(str, ", ");
71            str += add_point(str, endD.fX, endD.fY);
72            str += sprintf(str, ", %d,%d);\n", a, b);
73            str += sprintf(str, "    pathB.close();\n");
74            str += sprintf(str, "    testPathOp(reporter, path, pathB, kIntersect_SkPathOp,"
75                    " filename);\n");
76            str += sprintf(str, "}\n");
77        }
78        pathA.moveTo(SkIntToScalar(a), SkIntToScalar(b));
79        pathA.cubicTo(SkIntToScalar(c), SkIntToScalar(d), endC.fX, endC.fY, endD.fX, endD.fY);
80        pathA.close();
81        pathB.moveTo(SkIntToScalar(c), SkIntToScalar(d));
82        pathB.cubicTo(endC.fX, endC.fY, endD.fX, endD.fY, SkIntToScalar(a), SkIntToScalar(b));
83        pathB.close();
84//        SkDebugf("%s\n", pathStr);
85        if (progress) {
86            outputProgress(state.fPathStr, pathStr, kIntersect_SkPathOp);
87        }
88        testPathOp(state.fReporter, pathA, pathB, kIntersect_SkPathOp, "loops");
89                }
90            }
91        }
92    }
93}
94
95DEF_TEST(PathOpsOpLoopsThreaded, reporter) {
96    initializeTests(reporter, "loopOp");
97    PathOpsThreadedTestRunner testRunner(reporter);
98    for (int a = 0; a < 6; ++a) {  // outermost
99        for (int b = a + 1; b < 7; ++b) {
100            for (int c = 0 ; c < 6; ++c) {
101                for (int d = c + 1; d < 7; ++d) {
102                    *testRunner.fRunnables.append() = SkNEW_ARGS(PathOpsThreadedRunnable,
103                            (&testOpLoopsMain, a, b, c, d, &testRunner));
104                }
105            }
106            if (!reporter->allowExtendedTest()) goto finish;
107        }
108    }
109finish:
110    testRunner.render();
111    ShowTestArray("loopOp");
112}
113