blob: d149b484a8776f69ee30f7ed0ca24cfce58024b3 [file] [log] [blame]
mtklein@google.comd36522d2013-10-16 13:02:15 +00001// Main binary for DM.
2// For a high-level overview, please see dm/README.
3
4#include "GrContext.h"
5#include "GrContextFactory.h"
6#include "SkCommandLineFlags.h"
7#include "SkForceLinking.h"
8#include "SkGraphics.h"
9#include "gm.h"
10
11#include "DMReporter.h"
12#include "DMTask.h"
13#include "DMTaskRunner.h"
14#include "DMCpuTask.h"
15#include "DMGpuTask.h"
16
17#include <string.h>
18
19using skiagm::GM;
20using skiagm::GMRegistry;
21using skiagm::Expectations;
22using skiagm::ExpectationsSource;
23using skiagm::JsonExpectationsSource;
24
25DEFINE_int32(cpuThreads, -1, "Threads for CPU work. Default NUM_CPUS.");
26DEFINE_int32(gpuThreads, 1, "Threads for GPU work.");
27DEFINE_string(expectations, "", "Compare generated images against JSON expectations at this path.");
28DEFINE_string(resources, "resources", "Path to resources directory.");
29DEFINE_string(match, "", "[~][^]substring[$] [...] of GM name to run.\n"
30 "Multiple matches may be separated by spaces.\n"
31 "~ causes a matching GM to always be skipped\n"
32 "^ requires the start of the GM to match\n"
33 "$ requires the end of the GM to match\n"
34 "^ and $ requires an exact match\n"
35 "If a GM does not match any list entry,\n"
36 "it is skipped unless some list entry starts with ~");
37DEFINE_string(config, "8888 gpu",
38 "Options: 565 8888 gpu msaa4 msaa16 gpunull gpudebug angle mesa"); // TODO(mtklein): pdf
39
40__SK_FORCE_IMAGE_DECODER_LINKING;
41
42// Split str on any characters in delimiters into out. (Think, strtok with a sane API.)
43static void split(const char* str, const char* delimiters, SkTArray<SkString>* out) {
44 const char* end = str + strlen(str);
45 while (str != end) {
46 // Find a token.
47 const size_t len = strcspn(str, delimiters);
48 out->push_back().set(str, len);
49 str += len;
50 // Skip any delimiters.
51 str += strspn(str, delimiters);
52 }
53}
54
55// "FooBar" -> "foobar". Obviously, ASCII only.
56static SkString lowercase(SkString s) {
57 for (size_t i = 0; i < s.size(); i++) {
58 s[i] = tolower(s[i]);
59 }
60 return s;
61}
62
63static void kick_off_tasks(const SkTDArray<GMRegistry::Factory>& gms,
64 const SkTArray<SkString>& configs,
65 const ExpectationsSource& expectations,
66 DM::Reporter* reporter,
67 DM::TaskRunner* tasks) {
68 const SkBitmap::Config _565 = SkBitmap::kRGB_565_Config;
69 const SkBitmap::Config _8888 = SkBitmap::kARGB_8888_Config;
70 const GrContextFactory::GLContextType native = GrContextFactory::kNative_GLContextType;
71 const GrContextFactory::GLContextType null = GrContextFactory::kNull_GLContextType;
72 const GrContextFactory::GLContextType debug = GrContextFactory::kDebug_GLContextType;
73 const GrContextFactory::GLContextType angle =
74 #if SK_ANGLE
75 GrContextFactory::kANGLE_GLContextType;
76 #else
77 native;
78 #endif
79 const GrContextFactory::GLContextType mesa =
80 #if SK_MESA
81 GLContextFactory::kMESA_GLContextType;
82 #else
83 native;
84 #endif
85
86 for (int i = 0; i < gms.count(); i++) {
87 SkAutoTDelete<GM> gmForName(gms[i](NULL));
88 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, gmForName->shortName())) continue;
89
90#define START(name, type, ...) \
91 if (lowercase(configs[j]).equals(name)) { \
92 tasks->add(SkNEW_ARGS(DM::type, \
93 (name, reporter, tasks, expectations, gms[i], __VA_ARGS__))); \
94 }
95 for (int j = 0; j < configs.count(); j++) {
96 START("565", CpuTask, _565);
97 START("8888", CpuTask, _8888);
98 START("gpu", GpuTask, _8888, native, 0);
99 START("msaa4", GpuTask, _8888, native, 4);
100 START("msaa16", GpuTask, _8888, native, 16);
101 START("gpunull", GpuTask, _8888, null, 0);
102 START("gpudebug", GpuTask, _8888, debug, 0);
103 START("angle", GpuTask, _8888, angle, 0);
104 START("mesa", GpuTask, _8888, mesa, 0);
105 //START("pdf", PdfTask, _8888);
106 }
107 }
108#undef START
109}
110
111static void report_failures(const DM::Reporter& reporter) {
112 SkTArray<SkString> failures;
113 reporter.getFailures(&failures);
114
115 if (failures.count() == 0) {
116 return;
117 }
118
119 SkDebugf("Failures:\n");
120 for (int i = 0; i < failures.count(); i++) {
121 SkDebugf(" %s\n", failures[i].c_str());
122 }
123}
124
125class NoExpectations : public ExpectationsSource {
126public:
127 Expectations get(const char* /*testName*/) const SK_OVERRIDE {
128 return Expectations();
129 }
130};
131
132
133int main(int argc, char** argv) {
134 SkGraphics::Init();
135
136 SkCommandLineFlags::Parse(argc, argv);
137 GM::SetResourcePath(FLAGS_resources[0]);
138 SkTArray<SkString> configs;
139 for (int i = 0; i < FLAGS_config.count(); i++) {
140 split(FLAGS_config[i], ", ", &configs);
141 }
142
143 SkTDArray<GMRegistry::Factory> gms;
144 for (const GMRegistry* reg = GMRegistry::Head(); reg != NULL; reg = reg->next()) {
145 *gms.append() = reg->factory();
146 }
147 SkDebugf("%d GMs x %d configs\n", gms.count(), configs.count());
148
149 SkAutoTUnref<ExpectationsSource> expectations(SkNEW(NoExpectations));
150 if (FLAGS_expectations.count() > 0) {
151 expectations.reset(SkNEW_ARGS(JsonExpectationsSource, (FLAGS_expectations[0])));
152 }
153
154 DM::Reporter reporter;
155 DM::TaskRunner tasks(FLAGS_cpuThreads, FLAGS_gpuThreads);
156 kick_off_tasks(gms, configs, *expectations, &reporter, &tasks);
157 tasks.wait();
158
159 reporter.updateStatusLine();
160 SkDebugf("\n");
161 report_failures(reporter);
162
163 SkGraphics::Term();
164
165 return reporter.failed() > 0;
166}