aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/PathOpsSkpClipTest.cpp
blob: 146c42ade793d562811d9b31ea4fdbe6ab9119da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "PathOpsExtendedTest.h"
#include "PathOpsThreadedCommon.h"
#include "SkBitmap.h"
#include "SkDevice.h"
#include "SkCanvas.h"
#include "SkImageDecoder.h"
#include "SkImageEncoder.h"
#include "SkStream.h"
#include "SkOSFile.h"
#include "SkPicture.h"
#include "SkString.h"

#ifdef SK_BUILD_FOR_WIN
#define PATH_SLASH "\\"
#define IN_DIR "D:" PATH_SLASH "skp"
#define OUT_DIR "D:" PATH_SLASH
#else
#define PATH_SLASH "/"
#define IN_DIR "/Volumes/Untitled" PATH_SLASH
#define OUT_DIR PATH_SLASH
#endif

static const char pictDir[] = IN_DIR ;
static const char outSkpClipDir[] = OUT_DIR "skpClip";
static const char outOldClipDir[] = OUT_DIR "oldClip";

static void make_filepath(SkString* path, const char* dir, const SkString& name) {
    size_t len = strlen(dir);
    path->set(dir);
    if (len > 0 && dir[len - 1] != PATH_SLASH[0]) {
        path->append(PATH_SLASH);
    }
    path->append(name);
}

static void testOne(const SkString& filename) {
#if DEBUG_SHOW_TEST_NAME
    SkString testName(filename);
    const char http[] = "http";
    if (testName.startsWith(http)) {
        testName.remove(0, sizeof(http) - 1);
    }
    while (testName.startsWith("_")) {
        testName.remove(0, 1);
    }
    const char dotSkp[] = ".skp";
    if (testName.endsWith(dotSkp)) {
        size_t len = testName.size();
        testName.remove(len - (sizeof(dotSkp) - 1), sizeof(dotSkp) - 1);
    }
    testName.prepend("skp");
    testName.append("1");
    strncpy(DEBUG_FILENAME_STRING, testName.c_str(), DEBUG_FILENAME_STRING_LENGTH);
#endif
    SkString path;
    make_filepath(&path, pictDir, filename);
    SkFILEStream stream(path.c_str());
    if (!stream.isValid()) {
        return;
    }
    SkPicture* pic = SkPicture::CreateFromStream(&stream, &SkImageDecoder::DecodeMemory);
    if (!pic) {
        SkDebugf("unable to decode %s\n", filename.c_str());
        return;
    }
    int width = pic->width();
    int height = pic->height();
    SkBitmap bitmap;
    bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
    bool success = bitmap.allocPixels();
    if (!success) {
        SkDebugf("unable to allocate bitmap for %s\n", filename.c_str());
        return;
    }
    SkCanvas canvas(bitmap);
    SkString pngName(filename);
    pngName.remove(pngName.size() - 3, 3);
    pngName.append("png");
    for (int i = 0; i < 2; ++i) {
        bool useOp = i ? true : false;
        canvas.setAllowSimplifyClip(useOp);
        pic->draw(&canvas);
        SkString outFile;
        make_filepath(&outFile, useOp ? outSkpClipDir : outOldClipDir, pngName);
        SkImageEncoder::EncodeFile(outFile.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100);
    }
    SkDELETE(pic);
}

const char skipBefore[] = "http___kkiste_to.skp";

static void PathOpsSkpClipTest(skiatest::Reporter* reporter) {
    SkOSFile::Iter iter(pictDir, "skp");
    SkString filename;
    int testCount = 0;
    while (iter.next(&filename)) {
        if (strcmp(filename.c_str(), skipBefore) < 0) {
            continue;
        }
        testOne(filename);
        if (reporter->verbose()) {
            SkDebugf(".");
            if (++testCount % 100 == 0) {
                SkDebugf("\n");
            }
        }
        reporter->bumpTestCount();
    }
}

static void testSkpClipMain(PathOpsThreadState* data) {
        SkString str(data->fSerialNo);
        testOne(str);
        if (data->fReporter->verbose()) {
            SkDebugf(".");
            static int threadTestCount;
            sk_atomic_inc(&threadTestCount);
            if (threadTestCount % 100 == 0) {
                SkDebugf("\n");
            }
        }
}

static void PathOpsSkpClipThreadedTest(skiatest::Reporter* reporter) {
    int threadCount = initializeTests(reporter, "skpClipThreadedTest");
    PathOpsThreadedTestRunner testRunner(reporter, threadCount);
    SkOSFile::Iter iter(pictDir, "skp");
    SkString filename;
    while (iter.next(&filename)) {
        if (strcmp(filename.c_str(), skipBefore) < 0) {
            continue;
        }
        *testRunner.fRunnables.append() = SkNEW_ARGS(PathOpsThreadedRunnable,
                (&testSkpClipMain, filename.c_str(), &testRunner));
        reporter->bumpTestCount();
    }
    testRunner.render();
}

static void PathOpsSkpClipOneOffTest(skiatest::Reporter* reporter) {
    SkString filename(skipBefore);
    testOne(filename);
}

#include "TestClassDef.h"
DEFINE_TESTCLASS_SHORT(PathOpsSkpClipTest)

DEFINE_TESTCLASS_SHORT(PathOpsSkpClipOneOffTest)

DEFINE_TESTCLASS_SHORT(PathOpsSkpClipThreadedTest)