/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#include "PathOpsExtendedTest.h"
#include "PathOpsThreadedCommon.h"

static void testSimplifyTrianglesMain(PathOpsThreadState* data) {
    SkASSERT(data);
    PathOpsThreadState& state = *data;
    char pathStr[1024];
    sk_bzero(pathStr, sizeof(pathStr));
    int ax = state.fA & 0x03;
    int ay = state.fA >> 2;
    int bx = state.fB & 0x03;
    int by = state.fB >> 2;
    int cx = state.fC & 0x03;
    int cy = state.fC >> 2;
    for (int d = 0; d < 15; ++d) {
        int dx = d & 0x03;
        int dy = d >> 2;
        for (int e = d + 1; e < 16; ++e) {
            int ex = e & 0x03;
            int ey = e >> 2;
            for (int f = d + 1; f < 16; ++f) {
                if (e == f) {
                    continue;
                }
                int fx = f & 0x03;
                int fy = f >> 2;
                if ((ex - dx) * (fy - dy) == (ey - dy) * (fx - dx)) {
                    continue;
                }
                SkPath path, out;
                path.setFillType(SkPath::kWinding_FillType);
                path.moveTo(SkIntToScalar(ax), SkIntToScalar(ay));
                path.lineTo(SkIntToScalar(bx), SkIntToScalar(by));
                path.lineTo(SkIntToScalar(cx), SkIntToScalar(cy));
                path.close();
                path.moveTo(SkIntToScalar(dx), SkIntToScalar(dy));
                path.lineTo(SkIntToScalar(ex), SkIntToScalar(ey));
                path.lineTo(SkIntToScalar(fx), SkIntToScalar(fy));
                path.close();
                char* str = pathStr;
                str += sprintf(str, "    path.moveTo(%d, %d);\n", ax, ay);
                str += sprintf(str, "    path.lineTo(%d, %d);\n", bx, by);
                str += sprintf(str, "    path.lineTo(%d, %d);\n", cx, cy);
                str += sprintf(str, "    path.close();\n");
                str += sprintf(str, "    path.moveTo(%d, %d);\n", dx, dy);
                str += sprintf(str, "    path.lineTo(%d, %d);\n", ex, ey);
                str += sprintf(str, "    path.lineTo(%d, %d);\n", fx, fy);
                str += sprintf(str, "    path.close();\n");
                outputProgress(state.fPathStr, pathStr, SkPath::kWinding_FillType);
                testSimplify(path, false, out, state, pathStr);
                path.setFillType(SkPath::kEvenOdd_FillType);
                outputProgress(state.fPathStr, pathStr, SkPath::kEvenOdd_FillType);
                testSimplify(path, true, out, state, pathStr);
            }
        }
    }
}

static void TestSimplifyTrianglesThreaded(skiatest::Reporter* reporter) {
    int threadCount = initializeTests("testTriangles");
    PathOpsThreadedTestRunner testRunner(reporter, threadCount);
    for (int a = 0; a < 15; ++a) {
        int ax = a & 0x03;
        int ay = a >> 2;
        for (int b = a + 1; b < 16; ++b) {
            int bx = b & 0x03;
            int by = b >> 2;
            for (int c = a + 1; c < 16; ++c) {
                if (b == c) {
                    continue;
                }
                int cx = c & 0x03;
                int cy = c >> 2;
                if ((bx - ax) * (cy - ay) == (by - ay) * (cx - ax)) {
                    continue;
                }
                *testRunner.fRunnables.append() = SkNEW_ARGS(PathOpsThreadedRunnable,
                        (&testSimplifyTrianglesMain, a, b, c, 0, &testRunner));
            }
            if (!reporter->allowExtendedTest()) goto finish;
        }
    }
finish:
    testRunner.render();
}

#include "TestClassDef.h"
DEFINE_TESTCLASS("SimplifyTrianglesThreaded", SimplifyTrianglesThreadedTestClass, \
        TestSimplifyTrianglesThreaded)
