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