blob: bf534f4820c638e5830ebdf26bf205d137430ac1 [file] [log] [blame]
mtklein00b621c2015-06-17 15:26:15 -07001/*
2 * Copyright 2013 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 */
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +00007
8#include "GrContext.h"
9#include "GrContextFactory.h"
10#include "GrRenderTarget.h"
11#include "SkGpuDevice.h"
12#include "gl/GrGLDefines.h"
13
14#include "SkBitmap.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000015#include "SkCanvas.h"
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +000016#include "SkColor.h"
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +000017#include "SkGraphics.h"
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +000018#include "SkImageEncoder.h"
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +000019#include "SkOSFile.h"
20#include "SkPicture.h"
21#include "SkRTConf.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000022#include "SkStream.h"
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +000023#include "SkString.h"
24#include "SkTArray.h"
25#include "SkTDArray.h"
mtklein406654b2014-09-03 15:34:37 -070026#include "SkTaskGroup.h"
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +000027#include "SkTime.h"
28#include "Test.h"
29
mtklein00b621c2015-06-17 15:26:15 -070030#if !SK_SUPPORT_GPU
31#error "GPU support required"
32#endif
33
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +000034#ifdef SK_BUILD_FOR_WIN
35 #define PATH_SLASH "\\"
36 #define IN_DIR "D:\\9-30-13\\"
37 #define OUT_DIR "D:\\skpSkGr\\11\\"
38 #define LINE_FEED "\r\n"
39#else
40 #define PATH_SLASH "/"
41 #define IN_DIR "/usr/local/google/home/caryclark" PATH_SLASH "9-30-13-skp"
42 #define OUT_DIR "/media/01CD75512A7F9EE0/4" PATH_SLASH
tfarina@chromium.org78e7b4e2014-01-02 21:45:03 +000043 #define LINE_FEED "\n"
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +000044#endif
45
46#define PATH_STR_SIZE 512
47
48static const struct {
49 int directory;
50 const char* filename;
51} skipOverSkGr[] = {
52 {1, "http___accuweather_com_.skp"}, // Couldn't convert bitmap to texture.http___absoku072_com_
53};
54
commit-bot@chromium.org9e344732014-05-22 19:58:22 +000055static const size_t skipOverSkGrCount = SK_ARRAY_COUNT(skipOverSkGr);
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +000056
57/////////////////////////////////////////
58
59class SkpSkGrThreadedRunnable;
60
61enum TestStep {
62 kCompareBits,
63 kEncodeFiles,
64};
65
66enum {
67 kMaxLength = 128,
68 kMaxFiles = 128,
69};
70
71struct TestResult {
72 void init(int dirNo) {
73 fDirNo = dirNo;
74 sk_bzero(fFilename, sizeof(fFilename));
75 fTestStep = kCompareBits;
76 fScaleOversized = true;
77 }
78
79 SkString status() {
80 SkString outStr;
81 outStr.printf("%s %d %d%s", fFilename, fPixelError, fTime, LINE_FEED);
82 return outStr;
83 }
84
85 static void Test(int dirNo, const char* filename, TestStep testStep, bool verbose) {
86 TestResult test;
87 test.init(dirNo);
88 test.fTestStep = testStep;
89 strcpy(test.fFilename, filename);
90 test.testOne();
91 if (verbose) {
92 SkDebugf("%s", test.status().c_str());
93 }
94 }
95
96 void test(int dirNo, const SkString& filename) {
97 init(dirNo);
98 strcpy(fFilename, filename.c_str());
99 testOne();
100 }
101
102 void testOne();
skia.committer@gmail.comf54ad6f2013-11-02 07:02:02 +0000103
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000104 char fFilename[kMaxLength];
105 TestStep fTestStep;
106 int fDirNo;
107 int fPixelError;
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700108 SkMSec fTime;
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000109 bool fScaleOversized;
110};
111
112struct SkpSkGrThreadState {
113 void init(int dirNo) {
114 fResult.init(dirNo);
115 fFoundCount = 0;
116 fSmallestError = 0;
117 sk_bzero(fFilesFound, sizeof(fFilesFound));
118 sk_bzero(fDirsFound, sizeof(fDirsFound));
119 sk_bzero(fError, sizeof(fError));
120 }
skia.committer@gmail.comf54ad6f2013-11-02 07:02:02 +0000121
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000122 char fFilesFound[kMaxFiles][kMaxLength];
123 int fDirsFound[kMaxFiles];
124 int fError[kMaxFiles];
125 int fFoundCount;
126 int fSmallestError;
127 skiatest::Reporter* fReporter;
128 TestResult fResult;
129};
130
131struct SkpSkGrThreadedTestRunner {
mtklein406654b2014-09-03 15:34:37 -0700132 SkpSkGrThreadedTestRunner(skiatest::Reporter* reporter)
133 : fReporter(reporter) {
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000134 }
135
136 ~SkpSkGrThreadedTestRunner();
137 void render();
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000138 SkTDArray<SkpSkGrThreadedRunnable*> fRunnables;
139 skiatest::Reporter* fReporter;
140};
141
mtklein048494c2016-02-16 19:06:15 -0800142class SkpSkGrThreadedRunnable {
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000143public:
144 SkpSkGrThreadedRunnable(void (*testFun)(SkpSkGrThreadState*), int dirNo, const char* str,
145 SkpSkGrThreadedTestRunner* runner) {
146 SkASSERT(strlen(str) < sizeof(fState.fResult.fFilename) - 1);
147 fState.init(dirNo);
148 strcpy(fState.fResult.fFilename, str);
149 fState.fReporter = runner->fReporter;
150 fTestFun = testFun;
151 }
152
mtklein048494c2016-02-16 19:06:15 -0800153 void operator()() {
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000154 SkGraphics::SetTLSFontCacheLimit(1 * 1024 * 1024);
155 (*fTestFun)(&fState);
156 }
157
158 SkpSkGrThreadState fState;
159 void (*fTestFun)(SkpSkGrThreadState*);
160};
161
162SkpSkGrThreadedTestRunner::~SkpSkGrThreadedTestRunner() {
163 for (int index = 0; index < fRunnables.count(); index++) {
halcanary385fe4d2015-08-26 13:07:48 -0700164 delete fRunnables[index];
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000165 }
166}
167
168void SkpSkGrThreadedTestRunner::render() {
mtklein279c7862016-01-04 19:13:19 -0800169 SkTaskGroup().batch(fRunnables.count(), [&](int i) {
mtklein048494c2016-02-16 19:06:15 -0800170 fRunnables[i]();
mtklein00b621c2015-06-17 15:26:15 -0700171 });
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000172}
173
174////////////////////////////////////////////////
175
176static const char outGrDir[] = OUT_DIR "grTest";
177static const char outSkDir[] = OUT_DIR "skTest";
178static const char outSkpDir[] = OUT_DIR "skpTest";
179static const char outDiffDir[] = OUT_DIR "outTest";
180static const char outStatusDir[] = OUT_DIR "statusTest";
181
182static SkString make_filepath(int dirIndex, const char* dir, const char* name) {
183 SkString path(dir);
184 if (dirIndex) {
185 path.appendf("%d", dirIndex);
186 }
187 path.append(PATH_SLASH);
188 path.append(name);
189 return path;
190}
191
192static SkString make_in_dir_name(int dirIndex) {
193 SkString dirName(IN_DIR);
194 dirName.appendf("%d", dirIndex);
195 if (!sk_exists(dirName.c_str())) {
196 SkDebugf("could not read dir %s\n", dirName.c_str());
197 return SkString();
198 }
199 return dirName;
200}
201
202static bool make_out_dirs() {
203 SkString outDir = make_filepath(0, OUT_DIR, "");
204 if (!sk_exists(outDir.c_str())) {
205 if (!sk_mkdir(outDir.c_str())) {
206 SkDebugf("could not create dir %s\n", outDir.c_str());
207 return false;
208 }
209 }
210 SkString grDir = make_filepath(0, outGrDir, "");
211 if (!sk_exists(grDir.c_str())) {
212 if (!sk_mkdir(grDir.c_str())) {
213 SkDebugf("could not create dir %s\n", grDir.c_str());
214 return false;
215 }
216 }
217 SkString skDir = make_filepath(0, outSkDir, "");
218 if (!sk_exists(skDir.c_str())) {
219 if (!sk_mkdir(skDir.c_str())) {
220 SkDebugf("could not create dir %s\n", skDir.c_str());
221 return false;
222 }
223 }
224 SkString skpDir = make_filepath(0, outSkpDir, "");
225 if (!sk_exists(skpDir.c_str())) {
226 if (!sk_mkdir(skpDir.c_str())) {
227 SkDebugf("could not create dir %s\n", skpDir.c_str());
228 return false;
229 }
230 }
231 SkString diffDir = make_filepath(0, outDiffDir, "");
232 if (!sk_exists(diffDir.c_str())) {
233 if (!sk_mkdir(diffDir.c_str())) {
234 SkDebugf("could not create dir %s\n", diffDir.c_str());
235 return false;
236 }
237 }
238 SkString statusDir = make_filepath(0, outStatusDir, "");
239 if (!sk_exists(statusDir.c_str())) {
240 if (!sk_mkdir(statusDir.c_str())) {
241 SkDebugf("could not create dir %s\n", statusDir.c_str());
242 return false;
243 }
244 }
245 return true;
246}
247
248static SkString make_png_name(const char* filename) {
249 SkString pngName = SkString(filename);
250 pngName.remove(pngName.size() - 3, 3);
251 pngName.append("png");
252 return pngName;
253}
254
bsalomon85b4b532016-04-05 11:06:27 -0700255typedef GrContextFactory::ContextType ContextType;
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000256#ifdef SK_BUILD_FOR_WIN
bsalomon85b4b532016-04-05 11:06:27 -0700257static const ContextType kAngle = GrContextFactory::kANGLE_ContextType;
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000258#else
bsalomon85b4b532016-04-05 11:06:27 -0700259static const ContextType kNative = GrContextFactory::kNativeGL_ContextType;
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000260#endif
261
262static int similarBits(const SkBitmap& gr, const SkBitmap& sk) {
263 const int kRowCount = 3;
264 const int kThreshold = 3;
265 int width = SkTMin(gr.width(), sk.width());
266 if (width < kRowCount) {
267 return true;
268 }
269 int height = SkTMin(gr.height(), sk.height());
270 if (height < kRowCount) {
271 return true;
272 }
273 int errorTotal = 0;
274 SkTArray<char, true> errorRows;
275 errorRows.push_back_n(width * kRowCount);
276 SkAutoLockPixels autoGr(gr);
277 SkAutoLockPixels autoSk(sk);
278 char* base = &errorRows[0];
279 for (int y = 0; y < height; ++y) {
280 SkPMColor* grRow = gr.getAddr32(0, y);
281 SkPMColor* skRow = sk.getAddr32(0, y);
282 char* cOut = &errorRows[(y % kRowCount) * width];
283 for (int x = 0; x < width; ++x) {
284 SkPMColor grColor = grRow[x];
285 SkPMColor skColor = skRow[x];
286 int dr = SkGetPackedR32(grColor) - SkGetPackedR32(skColor);
287 int dg = SkGetPackedG32(grColor) - SkGetPackedG32(skColor);
288 int db = SkGetPackedB32(grColor) - SkGetPackedB32(skColor);
289 int error = SkTMax(SkAbs32(dr), SkTMax(SkAbs32(dg), SkAbs32(db)));
290 if ((cOut[x] = error >= kThreshold) && x >= 2
291 && base[x - 2] && base[width + x - 2] && base[width * 2 + x - 2]
292 && base[x - 1] && base[width + x - 1] && base[width * 2 + x - 1]
293 && base[x - 0] && base[width + x - 0] && base[width * 2 + x - 0]) {
294 errorTotal += error;
295 }
296 }
297 }
298 return errorTotal;
299}
300
301static bool addError(SkpSkGrThreadState* data) {
302 bool foundSmaller = false;
303 int dCount = data->fFoundCount;
304 int pixelError = data->fResult.fPixelError;
305 if (data->fFoundCount < kMaxFiles) {
306 data->fError[dCount] = pixelError;
307 strcpy(data->fFilesFound[dCount], data->fResult.fFilename);
308 data->fDirsFound[dCount] = data->fResult.fDirNo;
309 ++data->fFoundCount;
310 } else if (pixelError > data->fSmallestError) {
311 int smallest = SK_MaxS32;
312 int smallestIndex = 0;
313 for (int index = 0; index < kMaxFiles; ++index) {
314 if (smallest > data->fError[index]) {
315 smallest = data->fError[index];
316 smallestIndex = index;
317 }
318 }
319 data->fError[smallestIndex] = pixelError;
320 strcpy(data->fFilesFound[smallestIndex], data->fResult.fFilename);
321 data->fDirsFound[smallestIndex] = data->fResult.fDirNo;
322 data->fSmallestError = SK_MaxS32;
323 for (int index = 0; index < kMaxFiles; ++index) {
324 if (data->fSmallestError > data->fError[index]) {
325 data->fSmallestError = data->fError[index];
326 }
327 }
328 SkDebugf("*%d*", data->fSmallestError);
329 foundSmaller = true;
330 }
331 return foundSmaller;
332}
333
334static SkMSec timePict(SkPicture* pic, SkCanvas* canvas) {
335 canvas->save();
336 int pWidth = pic->width();
337 int pHeight = pic->height();
338 const int maxDimension = 1000;
339 const int slices = 3;
340 int xInterval = SkTMax(pWidth - maxDimension, 0) / (slices - 1);
341 int yInterval = SkTMax(pHeight - maxDimension, 0) / (slices - 1);
skia.committer@gmail.comf54ad6f2013-11-02 07:02:02 +0000342 SkRect rect = {0, 0, SkIntToScalar(SkTMin(maxDimension, pWidth)),
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000343 SkIntToScalar(SkTMin(maxDimension, pHeight))};
344 canvas->clipRect(rect);
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700345 skiatest::Timer timer;
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000346 for (int x = 0; x < slices; ++x) {
347 for (int y = 0; y < slices; ++y) {
348 pic->draw(canvas);
349 canvas->translate(0, SkIntToScalar(yInterval));
350 }
351 canvas->translate(SkIntToScalar(xInterval), SkIntToScalar(-yInterval * slices));
352 }
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700353 SkMSec elapsed = timer.elapsedMsInt();
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000354 canvas->restore();
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700355 return elapsed;
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000356}
357
358static void drawPict(SkPicture* pic, SkCanvas* canvas, int scale) {
359 canvas->clear(SK_ColorWHITE);
360 if (scale != 1) {
361 canvas->save();
362 canvas->scale(1.0f / scale, 1.0f / scale);
363 }
364 pic->draw(canvas);
365 if (scale != 1) {
366 canvas->restore();
367 }
368}
369
370static void writePict(const SkBitmap& bitmap, const char* outDir, const char* pngName) {
371 SkString outFile = make_filepath(0, outDir, pngName);
372 if (!SkImageEncoder::EncodeFile(outFile.c_str(), bitmap,
373 SkImageEncoder::kPNG_Type, 100)) {
374 SkDebugf("unable to encode gr %s (width=%d height=%d)br \n", pngName,
375 bitmap.width(), bitmap.height());
376 }
377}
378
379void TestResult::testOne() {
reedca2622b2016-03-18 07:25:55 -0700380 sk_sp<SkPicture> pic;
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000381 {
382 SkString d;
383 d.printf(" {%d, \"%s\"},", fDirNo, fFilename);
384 SkString path = make_filepath(fDirNo, IN_DIR, fFilename);
385 SkFILEStream stream(path.c_str());
386 if (!stream.isValid()) {
387 SkDebugf("invalid stream %s\n", path.c_str());
388 goto finish;
389 }
390 if (fTestStep == kEncodeFiles) {
391 size_t length = stream.getLength();
392 SkTArray<char, true> bytes;
393 bytes.push_back_n(length);
394 stream.read(&bytes[0], length);
395 stream.rewind();
396 SkString wPath = make_filepath(0, outSkpDir, fFilename);
397 SkFILEWStream wStream(wPath.c_str());
398 wStream.write(&bytes[0], length);
399 wStream.flush();
400 }
reedca2622b2016-03-18 07:25:55 -0700401 pic = SkPicture::MakeFromStream(&stream);
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000402 if (!pic) {
403 SkDebugf("unable to decode %s\n", fFilename);
404 goto finish;
405 }
406 int pWidth = pic->width();
407 int pHeight = pic->height();
408 int pLargerWH = SkTMax(pWidth, pHeight);
409 GrContextFactory contextFactory;
410#ifdef SK_BUILD_FOR_WIN
411 GrContext* context = contextFactory.get(kAngle);
412#else
413 GrContext* context = contextFactory.get(kNative);
414#endif
halcanary96fcdcc2015-08-27 07:41:13 -0700415 if (nullptr == context) {
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000416 SkDebugf("unable to allocate context for %s\n", fFilename);
417 goto finish;
418 }
419 int maxWH = context->getMaxRenderTargetSize();
420 int scale = 1;
421 while (pLargerWH / scale > maxWH) {
422 scale *= 2;
423 }
424 SkBitmap bitmap;
425 SkIPoint dim;
426 do {
427 dim.fX = (pWidth + scale - 1) / scale;
428 dim.fY = (pHeight + scale - 1) / scale;
commit-bot@chromium.org9e344732014-05-22 19:58:22 +0000429 bool success = bitmap.allocN32Pixels(dim.fX, dim.fY);
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000430 if (success) {
431 break;
432 }
433 SkDebugf("-%d-", scale);
434 } while ((scale *= 2) < 256);
435 if (scale >= 256) {
436 SkDebugf("unable to allocate bitmap for %s (w=%d h=%d) (sw=%d sh=%d)\n",
437 fFilename, pWidth, pHeight, dim.fX, dim.fY);
reedca2622b2016-03-18 07:25:55 -0700438 return;
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000439 }
440 SkCanvas skCanvas(bitmap);
441 drawPict(pic, &skCanvas, fScaleOversized ? scale : 1);
442 GrTextureDesc desc;
443 desc.fConfig = kSkia8888_GrPixelConfig;
444 desc.fFlags = kRenderTarget_GrTextureFlagBit;
445 desc.fWidth = dim.fX;
446 desc.fHeight = dim.fY;
447 desc.fSampleCnt = 0;
halcanary96fcdcc2015-08-27 07:41:13 -0700448 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, nullptr, 0));
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000449 if (!texture) {
450 SkDebugf("unable to allocate texture for %s (w=%d h=%d)\n", fFilename,
451 dim.fX, dim.fY);
reedca2622b2016-03-18 07:25:55 -0700452 return;
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000453 }
454 SkGpuDevice grDevice(context, texture.get());
455 SkCanvas grCanvas(&grDevice);
reedca2622b2016-03-18 07:25:55 -0700456 drawPict(pic.get(), &grCanvas, fScaleOversized ? scale : 1);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000457
458 SkBitmap grBitmap;
459 grBitmap.allocPixels(grCanvas.imageInfo());
460 grCanvas.readPixels(&grBitmap, 0, 0);
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +0000461
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000462 if (fTestStep == kCompareBits) {
463 fPixelError = similarBits(grBitmap, bitmap);
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700464 SkMSec skTime = timePict(pic, &skCanvas);
465 SkMSec grTime = timePict(pic, &grCanvas);
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000466 fTime = skTime - grTime;
467 } else if (fTestStep == kEncodeFiles) {
468 SkString pngStr = make_png_name(fFilename);
469 const char* pngName = pngStr.c_str();
470 writePict(grBitmap, outGrDir, pngName);
471 writePict(bitmap, outSkDir, pngName);
472 }
473 }
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000474}
475
476static SkString makeStatusString(int dirNo) {
477 SkString statName;
478 statName.printf("stats%d.txt", dirNo);
479 SkString statusFile = make_filepath(0, outStatusDir, statName.c_str());
480 return statusFile;
481}
482
483class PreParser {
484public:
485 PreParser(int dirNo)
486 : fDirNo(dirNo)
487 , fIndex(0)
488 , fStatusPath(makeStatusString(dirNo)) {
489 if (!sk_exists(fStatusPath.c_str())) {
490 return;
491 }
492 SkFILEStream reader;
493 reader.setPath(fStatusPath.c_str());
494 while (fetch(reader, &fResults.push_back()))
495 ;
496 fResults.pop_back();
497 }
498
499 bool fetch(SkFILEStream& reader, TestResult* result) {
500 char c;
501 int i = 0;
502 result->init(fDirNo);
503 result->fPixelError = 0;
504 result->fTime = 0;
505 do {
506 bool readOne = reader.read(&c, 1) != 0;
507 if (!readOne) {
508 SkASSERT(i == 0);
509 return false;
510 }
511 if (c == ' ') {
512 result->fFilename[i++] = '\0';
513 break;
514 }
515 result->fFilename[i++] = c;
516 SkASSERT(i < kMaxLength);
517 } while (true);
518 do {
519 SkAssertResult(reader.read(&c, 1) != 0);
520 if (c == ' ') {
521 break;
522 }
523 SkASSERT(c >= '0' && c <= '9');
524 result->fPixelError = result->fPixelError * 10 + (c - '0');
525 } while (true);
526 bool minus = false;
527 do {
528 if (reader.read(&c, 1) == 0) {
529 break;
530 }
531 if (c == '\r' && reader.read(&c, 1) == 0) {
532 break;
533 }
534 if (c == '\n') {
535 break;
536 }
537 if (c == '-') {
538 minus = true;
539 continue;
540 }
541 SkASSERT(c >= '0' && c <= '9');
542 result->fTime = result->fTime * 10 + (c - '0');
543 } while (true);
544 if (minus) {
545 result->fTime = -result->fTime;
546 }
547 return true;
548 }
549
550 bool match(const SkString& filename, SkFILEWStream* stream, TestResult* result) {
551 if (fIndex < fResults.count()) {
552 *result = fResults[fIndex++];
553 SkASSERT(filename.equals(result->fFilename));
554 SkString outStr(result->status());
555 stream->write(outStr.c_str(), outStr.size());
556 stream->flush();
557 return true;
558 }
559 return false;
560 }
561
562private:
563 int fDirNo;
564 int fIndex;
565 SkTArray<TestResult, true> fResults;
566 SkString fStatusPath;
567};
568
569static bool initTest() {
570#if !defined SK_BUILD_FOR_WIN && !defined SK_BUILD_FOR_MAC
571 SK_CONF_SET("images.jpeg.suppressDecoderWarnings", true);
572 SK_CONF_SET("images.png.suppressDecoderWarnings", true);
573#endif
574 return make_out_dirs();
575}
576
tfarina@chromium.org78e7b4e2014-01-02 21:45:03 +0000577DEF_TEST(SkpSkGr, reporter) {
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000578 SkTArray<TestResult, true> errors;
579 if (!initTest()) {
580 return;
581 }
582 SkpSkGrThreadState state;
583 state.init(0);
584 int smallCount = 0;
585 for (int dirNo = 1; dirNo <= 100; ++dirNo) {
586 SkString pictDir = make_in_dir_name(dirNo);
587 SkASSERT(pictDir.size());
588 if (reporter->verbose()) {
589 SkDebugf("dirNo=%d\n", dirNo);
590 }
591 SkOSFile::Iter iter(pictDir.c_str(), "skp");
592 SkString filename;
593 int testCount = 0;
594 PreParser preParser(dirNo);
595 SkFILEWStream statusStream(makeStatusString(dirNo).c_str());
596 while (iter.next(&filename)) {
597 for (size_t index = 0; index < skipOverSkGrCount; ++index) {
598 if (skipOverSkGr[index].directory == dirNo
599 && strcmp(filename.c_str(), skipOverSkGr[index].filename) == 0) {
600 goto skipOver;
601 }
602 }
603 if (preParser.match(filename, &statusStream, &state.fResult)) {
604 addError(&state);
605 ++testCount;
606 goto checkEarlyExit;
607 }
608 if (state.fSmallestError > 5000000) {
609 goto breakOut;
610 }
611 {
612 TestResult& result = state.fResult;
613 result.test(dirNo, filename);
614 SkString outStr(result.status());
615 statusStream.write(outStr.c_str(), outStr.size());
616 statusStream.flush();
617 if (1) {
618 SkDebugf("%s", outStr.c_str());
619 }
620 bool noMatch = addError(&state);
621 if (noMatch) {
622 smallCount = 0;
623 } else if (++smallCount > 10000) {
624 goto breakOut;
625 }
626 }
627 ++testCount;
628 if (reporter->verbose()) {
629 if (testCount % 100 == 0) {
630 SkDebugf("#%d\n", testCount);
631 }
632 }
tfarina@chromium.org78e7b4e2014-01-02 21:45:03 +0000633 skipOver:
634 reporter->bumpTestCount();
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000635 checkEarlyExit:
636 if (1 && testCount == 20) {
637 break;
638 }
639 }
640 }
641breakOut:
642 if (reporter->verbose()) {
643 for (int index = 0; index < state.fFoundCount; ++index) {
644 SkDebugf("%d %s %d\n", state.fDirsFound[index], state.fFilesFound[index],
645 state.fError[index]);
646 }
647 }
648 for (int index = 0; index < state.fFoundCount; ++index) {
649 TestResult::Test(state.fDirsFound[index], state.fFilesFound[index], kEncodeFiles,
650 reporter->verbose());
651 if (reporter->verbose()) SkDebugf("+");
652 }
653}
654
655static void bumpCount(skiatest::Reporter* reporter, bool skipping) {
656 if (reporter->verbose()) {
657 static int threadTestCount;
658 sk_atomic_inc(&threadTestCount);
659 if (!skipping && threadTestCount % 100 == 0) {
660 SkDebugf("#%d\n", threadTestCount);
661 }
662 if (skipping && threadTestCount % 10000 == 0) {
663 SkDebugf("#%d\n", threadTestCount);
664 }
665 }
666}
667
668static void testSkGrMain(SkpSkGrThreadState* data) {
669 data->fResult.testOne();
670 bumpCount(data->fReporter, false);
671 data->fReporter->bumpTestCount();
672}
673
tfarina@chromium.org78e7b4e2014-01-02 21:45:03 +0000674DEF_TEST(SkpSkGrThreaded, reporter) {
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000675 if (!initTest()) {
676 return;
677 }
mtklein406654b2014-09-03 15:34:37 -0700678 SkpSkGrThreadedTestRunner testRunner(reporter);
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000679 for (int dirIndex = 1; dirIndex <= 100; ++dirIndex) {
680 SkString pictDir = make_in_dir_name(dirIndex);
681 if (pictDir.size() == 0) {
682 continue;
683 }
684 SkOSFile::Iter iter(pictDir.c_str(), "skp");
685 SkString filename;
686 while (iter.next(&filename)) {
687 SkString pngName = make_png_name(filename.c_str());
688 SkString oldPng = make_filepath(dirIndex, outSkDir, pngName.c_str());
689 SkString newPng = make_filepath(dirIndex, outGrDir, pngName.c_str());
690 if (sk_exists(oldPng.c_str()) && sk_exists(newPng.c_str())) {
691 bumpCount(reporter, true);
692 continue;
693 }
694 for (size_t index = 0; index < skipOverSkGrCount; ++index) {
695 if (skipOverSkGr[index].directory == dirIndex
696 && strcmp(filename.c_str(), skipOverSkGr[index].filename) == 0) {
697 bumpCount(reporter, true);
698 goto skipOver;
699 }
700 }
halcanary385fe4d2015-08-26 13:07:48 -0700701 *testRunner.fRunnables.append() = new SkpSkGrThreadedRunnable(
702 &testSkGrMain, dirIndex, filename.c_str(), &testRunner);
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000703 skipOver:
704 ;
705 }
706 }
707 testRunner.render();
708 SkpSkGrThreadState& max = testRunner.fRunnables[0]->fState;
709 for (int dirIndex = 2; dirIndex <= 100; ++dirIndex) {
710 SkpSkGrThreadState& state = testRunner.fRunnables[dirIndex - 1]->fState;
711 for (int index = 0; index < state.fFoundCount; ++index) {
712 int maxIdx = max.fFoundCount;
713 if (maxIdx < kMaxFiles) {
714 max.fError[maxIdx] = state.fError[index];
715 strcpy(max.fFilesFound[maxIdx], state.fFilesFound[index]);
716 max.fDirsFound[maxIdx] = state.fDirsFound[index];
717 ++max.fFoundCount;
718 continue;
719 }
720 for (maxIdx = 0; maxIdx < max.fFoundCount; ++maxIdx) {
721 if (max.fError[maxIdx] < state.fError[index]) {
722 max.fError[maxIdx] = state.fError[index];
723 strcpy(max.fFilesFound[maxIdx], state.fFilesFound[index]);
724 max.fDirsFound[maxIdx] = state.fDirsFound[index];
725 break;
726 }
727 }
728 }
729 }
730 TestResult encoder;
731 encoder.fTestStep = kEncodeFiles;
732 for (int index = 0; index < max.fFoundCount; ++index) {
733 encoder.fDirNo = max.fDirsFound[index];
734 strcpy(encoder.fFilename, max.fFilesFound[index]);
735 encoder.testOne();
736 SkDebugf("+");
737 }
738}
739
tfarina@chromium.org78e7b4e2014-01-02 21:45:03 +0000740DEF_TEST(SkpSkGrOneOff, reporter) {
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000741 if (!initTest()) {
742 return;
743 }
744 int testIndex = 166;
745 int dirIndex = skipOverSkGr[testIndex - 166].directory;
746 SkString pictDir = make_in_dir_name(dirIndex);
747 if (pictDir.size() == 0) {
748 return;
749 }
750 SkString filename(skipOverSkGr[testIndex - 166].filename);
751 TestResult::Test(dirIndex, filename.c_str(), kCompareBits, reporter->verbose());
752 TestResult::Test(dirIndex, filename.c_str(), kEncodeFiles, reporter->verbose());
753}