blob: 9f28bff4b981101d90b0656d3ca01d9a66319604 [file] [log] [blame]
junov@chromium.org777442d2012-06-12 14:56:36 +00001/*
2 * Copyright 2012 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
commit-bot@chromium.org56799e22013-07-16 18:21:46 +00008#include "LazyDecodeBitmap.h"
scroggo@google.com4a26d9d2012-11-07 18:01:46 +00009#include "CopyTilesRenderer.h"
junov@chromium.org777442d2012-06-12 14:56:36 +000010#include "SkBitmap.h"
keyar@chromium.org472b3792012-07-20 22:34:27 +000011#include "SkDevice.h"
scroggo@google.comd9ba9a02013-03-21 19:43:15 +000012#include "SkCommandLineFlags.h"
borenet@google.com10ef79e2012-09-10 17:19:06 +000013#include "SkGraphics.h"
scroggo@google.com5a7c6be2012-10-04 21:46:08 +000014#include "SkImageDecoder.h"
edisonn@google.com84f548c2012-12-18 22:24:03 +000015#include "SkImageEncoder.h"
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +000016#include "SkMath.h"
junov@chromium.org777442d2012-06-12 14:56:36 +000017#include "SkOSFile.h"
18#include "SkPicture.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000019#include "SkPictureRecorder.h"
junov@chromium.org777442d2012-06-12 14:56:36 +000020#include "SkStream.h"
21#include "SkString.h"
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +000022
23#include "image_expectations.h"
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000024#include "PictureRenderer.h"
scroggo@google.com161e1ba2013-03-04 16:41:06 +000025#include "PictureRenderingFlags.h"
twiz@google.coma31b8bb2012-06-22 18:24:56 +000026#include "picture_utils.h"
junov@chromium.org777442d2012-06-12 14:56:36 +000027
scroggo@google.com161e1ba2013-03-04 16:41:06 +000028// Flags used by this file, alphabetically:
29DEFINE_int32(clone, 0, "Clone the picture n times before rendering.");
30DECLARE_bool(deferImageDecoding);
31DEFINE_int32(maxComponentDiff, 256, "Maximum diff on a component, 0 - 256. Components that differ "
32 "by more than this amount are considered errors, though all diffs are reported. "
33 "Requires --validate.");
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000034DEFINE_string(readJsonSummaryPath, "", "JSON file to read image expectations from.");
scroggo@google.com604e0c22013-04-09 21:25:46 +000035DECLARE_string(readPath);
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000036DEFINE_bool(writeChecksumBasedFilenames, false,
37 "When writing out images, use checksum-based filenames.");
scroggo@google.com1125d392013-05-03 20:43:37 +000038DEFINE_bool(writeEncodedImages, false, "Any time the skp contains an encoded image, write it to a "
39 "file rather than decoding it. Requires writePath to be set. Skips drawing the full "
40 "skp to a file. Not compatible with deferImageDecoding.");
commit-bot@chromium.org4610a462014-04-29 19:39:22 +000041DEFINE_string(writeJsonSummaryPath, "", "File to write a JSON summary of image results to.");
42DEFINE_string2(writePath, w, "", "Directory to write the rendered images into.");
scroggo@google.com161e1ba2013-03-04 16:41:06 +000043DEFINE_bool(writeWholeImage, false, "In tile mode, write the entire rendered image to a "
44 "file, instead of an image for each tile.");
45DEFINE_bool(validate, false, "Verify that the rendered image contains the same pixels as "
junov@chromium.orge286e842013-03-13 17:27:16 +000046 "the picture rendered in simple mode. When used in conjunction with --bbh, results "
47 "are validated against the picture rendered in the same mode, but without the bbh.");
junov@chromium.org777442d2012-06-12 14:56:36 +000048
commit-bot@chromium.orgff36a1d2013-07-24 20:37:30 +000049DEFINE_bool(bench_record, false, "If true, drop into an infinite loop of recording the picture.");
50
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000051DEFINE_bool(preprocess, false, "If true, perform device specific preprocessing before rendering.");
52
scroggo@google.com1125d392013-05-03 20:43:37 +000053////////////////////////////////////////////////////////////////////////////////////////////////////
54
55/**
56 * Table for translating from format of data to a suffix.
57 */
58struct Format {
59 SkImageDecoder::Format fFormat;
60 const char* fSuffix;
61};
62static const Format gFormats[] = {
63 { SkImageDecoder::kBMP_Format, ".bmp" },
64 { SkImageDecoder::kGIF_Format, ".gif" },
65 { SkImageDecoder::kICO_Format, ".ico" },
66 { SkImageDecoder::kJPEG_Format, ".jpg" },
67 { SkImageDecoder::kPNG_Format, ".png" },
68 { SkImageDecoder::kWBMP_Format, ".wbmp" },
69 { SkImageDecoder::kWEBP_Format, ".webp" },
70 { SkImageDecoder::kUnknown_Format, "" },
71};
72
73/**
74 * Get an appropriate suffix for an image format.
75 */
76static const char* get_suffix_from_format(SkImageDecoder::Format format) {
77 for (size_t i = 0; i < SK_ARRAY_COUNT(gFormats); i++) {
78 if (gFormats[i].fFormat == format) {
79 return gFormats[i].fSuffix;
80 }
81 }
82 return "";
83}
84
85/**
86 * Base name for an image file created from the encoded data in an skp.
87 */
88static SkString gInputFileName;
89
90/**
91 * Number to be appended to the image file name so that it is unique.
92 */
93static uint32_t gImageNo;
94
95/**
96 * Set up the name for writing encoded data to a file.
97 * Sets gInputFileName to name, minus any extension ".*"
98 * Sets gImageNo to 0, so images from file "X.skp" will
99 * look like "X_<gImageNo>.<suffix>", beginning with 0
100 * for each new skp.
101 */
102static void reset_image_file_base_name(const SkString& name) {
103 gImageNo = 0;
104 // Remove ".skp"
105 const char* cName = name.c_str();
106 const char* dot = strrchr(cName, '.');
107 if (dot != NULL) {
108 gInputFileName.set(cName, dot - cName);
109 } else {
110 gInputFileName.set(name);
111 }
112}
113
114/**
115 * Write the raw encoded bitmap data to a file.
116 */
117static bool write_image_to_file(const void* buffer, size_t size, SkBitmap* bitmap) {
118 SkASSERT(!FLAGS_writePath.isEmpty());
119 SkMemoryStream memStream(buffer, size);
120 SkString outPath;
121 SkImageDecoder::Format format = SkImageDecoder::GetStreamFormat(&memStream);
122 SkString name = SkStringPrintf("%s_%d%s", gInputFileName.c_str(), gImageNo++,
123 get_suffix_from_format(format));
124 SkString dir(FLAGS_writePath[0]);
125 sk_tools::make_filepath(&outPath, dir, name);
126 SkFILEWStream fileStream(outPath.c_str());
127 if (!(fileStream.isValid() && fileStream.write(buffer, size))) {
128 SkDebugf("Failed to write encoded data to \"%s\"\n", outPath.c_str());
129 }
130 // Put in a dummy bitmap.
131 return SkImageDecoder::DecodeStream(&memStream, bitmap, SkBitmap::kNo_Config,
132 SkImageDecoder::kDecodeBounds_Mode);
133}
134
135////////////////////////////////////////////////////////////////////////////////////////////////////
136
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000137/**
138 * Called only by render_picture().
139 */
140static bool render_picture_internal(const SkString& inputPath, const SkString* outputDir,
141 sk_tools::PictureRenderer& renderer,
142 SkBitmap** out) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000143 SkString inputFilename;
144 sk_tools::get_basename(&inputFilename, inputPath);
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000145 SkString outputDirString;
146 if (NULL != outputDir && outputDir->size() > 0 && !FLAGS_writeEncodedImages) {
147 outputDirString.set(*outputDir);
148 }
twiz@google.coma31b8bb2012-06-22 18:24:56 +0000149
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000150 SkFILEStream inputStream;
twiz@google.coma31b8bb2012-06-22 18:24:56 +0000151 inputStream.setPath(inputPath.c_str());
152 if (!inputStream.isValid()) {
153 SkDebugf("Could not open file %s\n", inputPath.c_str());
borenet@google.com66bcbd12012-09-17 18:26:06 +0000154 return false;
twiz@google.coma31b8bb2012-06-22 18:24:56 +0000155 }
156
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000157 SkPicture::InstallPixelRefProc proc;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000158 if (FLAGS_deferImageDecoding) {
commit-bot@chromium.org56799e22013-07-16 18:21:46 +0000159 proc = &sk_tools::LazyDecodeBitmap;
scroggo@google.com1125d392013-05-03 20:43:37 +0000160 } else if (FLAGS_writeEncodedImages) {
161 SkASSERT(!FLAGS_writePath.isEmpty());
162 reset_image_file_base_name(inputFilename);
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000163 proc = &write_image_to_file;
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000164 } else {
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000165 proc = &SkImageDecoder::DecodeMemory;
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000166 }
scroggo@google.com1125d392013-05-03 20:43:37 +0000167
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000168 SkDebugf("deserializing... %s\n", inputPath.c_str());
169
170 SkPicture* picture = SkPicture::CreateFromStream(&inputStream, proc);
171
172 if (NULL == picture) {
borenet@google.com66bcbd12012-09-17 18:26:06 +0000173 SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str());
174 return false;
175 }
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000176
commit-bot@chromium.orgff36a1d2013-07-24 20:37:30 +0000177 while (FLAGS_bench_record) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000178 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000179 picture->draw(recorder.beginRecording(picture->width(), picture->height(), NULL, 0));
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000180 SkAutoTUnref<SkPicture> other(recorder.endRecording());
commit-bot@chromium.orgff36a1d2013-07-24 20:37:30 +0000181 }
182
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000183 for (int i = 0; i < FLAGS_clone; ++i) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000184 SkPicture* clone = picture->clone();
185 SkDELETE(picture);
186 picture = clone;
187 }
188
189 SkDebugf("drawing... [%i %i] %s\n", picture->width(), picture->height(),
borenet@google.com03fcee82012-09-10 18:18:38 +0000190 inputPath.c_str());
skia.committer@gmail.com1d225f22012-09-14 02:01:10 +0000191
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000192 renderer.init(picture, &outputDirString, &inputFilename, FLAGS_writeChecksumBasedFilenames);
skia.committer@gmail.comeb849e52014-03-17 03:02:17 +0000193
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000194 if (FLAGS_preprocess) {
195 if (NULL != renderer.getCanvas()) {
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000196 renderer.getCanvas()->EXPERIMENTAL_optimize(renderer.getPicture());
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000197 }
198 }
199
scroggo@google.comb4773b42012-10-01 20:06:09 +0000200 renderer.setup();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000201
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000202 bool success = renderer.render(out);
203 if (!success) {
204 SkDebugf("Failed to render %s\n", inputFilename.c_str());
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000205 }
scroggo@google.com9a412522012-09-07 15:21:18 +0000206
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000207 renderer.end();
edisonn@google.com84f548c2012-12-18 22:24:03 +0000208
209 SkDELETE(picture);
borenet@google.com66bcbd12012-09-17 18:26:06 +0000210 return success;
junov@chromium.org777442d2012-06-12 14:56:36 +0000211}
212
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000213static inline int getByte(uint32_t value, int index) {
214 SkASSERT(0 <= index && index < 4);
215 return (value >> (index * 8)) & 0xFF;
216}
217
218static int MaxByteDiff(uint32_t v1, uint32_t v2) {
219 return SkMax32(SkMax32(abs(getByte(v1, 0) - getByte(v2, 0)), abs(getByte(v1, 1) - getByte(v2, 1))),
220 SkMax32(abs(getByte(v1, 2) - getByte(v2, 2)), abs(getByte(v1, 3) - getByte(v2, 3))));
221}
222
junov@chromium.orge286e842013-03-13 17:27:16 +0000223class AutoRestoreBbhType {
224public:
225 AutoRestoreBbhType() {
226 fRenderer = NULL;
junov@chromium.orgd34fda12013-03-13 19:03:26 +0000227 fSavedBbhType = sk_tools::PictureRenderer::kNone_BBoxHierarchyType;
junov@chromium.orge286e842013-03-13 17:27:16 +0000228 }
229
230 void set(sk_tools::PictureRenderer* renderer,
231 sk_tools::PictureRenderer::BBoxHierarchyType bbhType) {
232 fRenderer = renderer;
233 fSavedBbhType = renderer->getBBoxHierarchyType();
234 renderer->setBBoxHierarchyType(bbhType);
235 }
236
237 ~AutoRestoreBbhType() {
238 if (NULL != fRenderer) {
239 fRenderer->setBBoxHierarchyType(fSavedBbhType);
240 }
241 }
242
243private:
244 sk_tools::PictureRenderer* fRenderer;
245 sk_tools::PictureRenderer::BBoxHierarchyType fSavedBbhType;
246};
junov@chromium.orge286e842013-03-13 17:27:16 +0000247
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000248/**
249 * Render the SKP file(s) within inputPath, writing their bitmap images into outputDir.
250 *
251 * @param inputPath path to an individual SKP file, or a directory of SKP files
252 * @param outputDir if not NULL, write the image(s) generated into this directory
253 * @param renderer PictureRenderer to use to render the SKPs
254 * @param jsonSummaryPtr if not NULL, add the image(s) generated to this summary
255 */
edisonn@google.com84f548c2012-12-18 22:24:03 +0000256static bool render_picture(const SkString& inputPath, const SkString* outputDir,
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000257 sk_tools::PictureRenderer& renderer,
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000258 sk_tools::ImageResultsAndExpectations *jsonSummaryPtr) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000259 int diffs[256] = {0};
edisonn@google.com84f548c2012-12-18 22:24:03 +0000260 SkBitmap* bitmap = NULL;
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000261 renderer.setJsonSummaryPtr(jsonSummaryPtr);
262 bool success = render_picture_internal(inputPath,
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000263 FLAGS_writeWholeImage ? NULL : outputDir,
edisonn@google.com84f548c2012-12-18 22:24:03 +0000264 renderer,
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000265 FLAGS_validate || FLAGS_writeWholeImage ? &bitmap : NULL);
edisonn@google.com84f548c2012-12-18 22:24:03 +0000266
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000267 if (!success || ((FLAGS_validate || FLAGS_writeWholeImage) && bitmap == NULL)) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000268 SkDebugf("Failed to draw the picture.\n");
269 SkDELETE(bitmap);
270 return false;
271 }
272
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000273 if (FLAGS_validate) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000274 SkBitmap* referenceBitmap = NULL;
junov@chromium.orge286e842013-03-13 17:27:16 +0000275 sk_tools::PictureRenderer* referenceRenderer;
276 // If the renderer uses a BBoxHierarchy, then the reference renderer
skia.committer@gmail.com03682be2013-03-14 07:02:51 +0000277 // will be the same renderer, without the bbh.
junov@chromium.orge286e842013-03-13 17:27:16 +0000278 AutoRestoreBbhType arbbh;
279 if (sk_tools::PictureRenderer::kNone_BBoxHierarchyType !=
280 renderer.getBBoxHierarchyType()) {
281 referenceRenderer = &renderer;
282 referenceRenderer->ref(); // to match auto unref below
283 arbbh.set(referenceRenderer, sk_tools::PictureRenderer::kNone_BBoxHierarchyType);
284 } else {
285 referenceRenderer = SkNEW(sk_tools::SimplePictureRenderer);
286 }
287 SkAutoTUnref<sk_tools::PictureRenderer> aurReferenceRenderer(referenceRenderer);
288
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000289 success = render_picture_internal(inputPath, NULL, *referenceRenderer,
290 &referenceBitmap);
edisonn@google.com84f548c2012-12-18 22:24:03 +0000291
junov@chromium.orgc19c1912013-03-12 19:56:49 +0000292 if (!success || NULL == referenceBitmap || NULL == referenceBitmap->getPixels()) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000293 SkDebugf("Failed to draw the reference picture.\n");
294 SkDELETE(bitmap);
295 SkDELETE(referenceBitmap);
296 return false;
297 }
298
299 if (success && (bitmap->width() != referenceBitmap->width())) {
300 SkDebugf("Expected image width: %i, actual image width %i.\n",
301 referenceBitmap->width(), bitmap->width());
302 SkDELETE(bitmap);
303 SkDELETE(referenceBitmap);
304 return false;
305 }
306 if (success && (bitmap->height() != referenceBitmap->height())) {
307 SkDebugf("Expected image height: %i, actual image height %i",
308 referenceBitmap->height(), bitmap->height());
309 SkDELETE(bitmap);
310 SkDELETE(referenceBitmap);
311 return false;
312 }
skia.committer@gmail.coma7d8e3e2012-12-19 02:01:38 +0000313
edisonn@google.com84f548c2012-12-18 22:24:03 +0000314 for (int y = 0; success && y < bitmap->height(); y++) {
315 for (int x = 0; success && x < bitmap->width(); x++) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000316 int diff = MaxByteDiff(*referenceBitmap->getAddr32(x, y),
317 *bitmap->getAddr32(x, y));
318 SkASSERT(diff >= 0 && diff <= 255);
319 diffs[diff]++;
skia.committer@gmail.com4d28d982013-01-17 07:06:06 +0000320
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000321 if (diff > FLAGS_maxComponentDiff) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000322 SkDebugf("Expected pixel at (%i %i) exceedds maximum "
323 "component diff of %i: 0x%x, actual 0x%x\n",
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000324 x, y, FLAGS_maxComponentDiff,
edisonn@google.com01754bf2013-01-11 16:08:07 +0000325 *referenceBitmap->getAddr32(x, y),
edisonn@google.com84f548c2012-12-18 22:24:03 +0000326 *bitmap->getAddr32(x, y));
327 SkDELETE(bitmap);
328 SkDELETE(referenceBitmap);
329 return false;
330 }
331 }
332 }
333 SkDELETE(referenceBitmap);
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000334
335 for (int i = 1; i <= 255; ++i) {
336 if(diffs[i] > 0) {
337 SkDebugf("Number of pixels with max diff of %i is %i\n", i, diffs[i]);
338 }
339 }
edisonn@google.com84f548c2012-12-18 22:24:03 +0000340 }
341
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000342 if (FLAGS_writeWholeImage) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000343 sk_tools::force_all_opaque(*bitmap);
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000344
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000345 SkString inputFilename, outputPath;
346 sk_tools::get_basename(&inputFilename, inputPath);
347 sk_tools::make_filepath(&outputPath, *outputDir, inputFilename);
348 sk_tools::replace_char(&outputPath, '.', '_');
349 outputPath.append(".png");
350
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000351 if (NULL != jsonSummaryPtr) {
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000352 sk_tools::ImageDigest imageDigest(*bitmap);
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000353 SkString outputFileBasename;
354 sk_tools::get_basename(&outputFileBasename, outputPath);
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000355 jsonSummaryPtr->add(inputFilename.c_str(), outputFileBasename.c_str(), imageDigest);
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000356 }
357
358 if (NULL != outputDir) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000359 if (!SkImageEncoder::EncodeFile(outputPath.c_str(), *bitmap,
360 SkImageEncoder::kPNG_Type, 100)) {
361 SkDebugf("Failed to draw the picture.\n");
362 success = false;
363 }
364 }
365 }
366 SkDELETE(bitmap);
367
368 return success;
369}
370
371
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000372static int process_input(const char* input, const SkString* outputDir,
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000373 sk_tools::PictureRenderer& renderer,
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000374 sk_tools::ImageResultsAndExpectations *jsonSummaryPtr) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000375 SkOSFile::Iter iter(input, "skp");
junov@chromium.org777442d2012-06-12 14:56:36 +0000376 SkString inputFilename;
borenet@google.com66bcbd12012-09-17 18:26:06 +0000377 int failures = 0;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000378 SkDebugf("process_input, %s\n", input);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000379 if (iter.next(&inputFilename)) {
380 do {
381 SkString inputPath;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000382 SkString inputAsSkString(input);
383 sk_tools::make_filepath(&inputPath, inputAsSkString, inputFilename);
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000384 if (!render_picture(inputPath, outputDir, renderer, jsonSummaryPtr)) {
borenet@google.com57837bf2012-09-19 17:28:29 +0000385 ++failures;
386 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000387 } while(iter.next(&inputFilename));
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000388 } else if (SkStrEndsWith(input, ".skp")) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000389 SkString inputPath(input);
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000390 if (!render_picture(inputPath, outputDir, renderer, jsonSummaryPtr)) {
borenet@google.com57837bf2012-09-19 17:28:29 +0000391 ++failures;
392 }
393 } else {
394 SkString warning;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000395 warning.printf("Warning: skipping %s\n", input);
borenet@google.com57837bf2012-09-19 17:28:29 +0000396 SkDebugf(warning.c_str());
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000397 }
borenet@google.com66bcbd12012-09-17 18:26:06 +0000398 return failures;
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000399}
400
caryclark@google.com5987f582012-10-02 18:33:14 +0000401int tool_main(int argc, char** argv);
402int tool_main(int argc, char** argv) {
scroggo@google.comd9ba9a02013-03-21 19:43:15 +0000403 SkCommandLineFlags::SetUsage("Render .skp files.");
404 SkCommandLineFlags::Parse(argc, argv);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000405
scroggo@google.com604e0c22013-04-09 21:25:46 +0000406 if (FLAGS_readPath.isEmpty()) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000407 SkDebugf(".skp files or directories are required.\n");
408 exit(-1);
409 }
410
411 if (FLAGS_maxComponentDiff < 0 || FLAGS_maxComponentDiff > 256) {
412 SkDebugf("--maxComponentDiff must be between 0 and 256\n");
413 exit(-1);
414 }
415
416 if (FLAGS_maxComponentDiff != 256 && !FLAGS_validate) {
417 SkDebugf("--maxComponentDiff requires --validate\n");
418 exit(-1);
419 }
420
421 if (FLAGS_clone < 0) {
422 SkDebugf("--clone must be >= 0. Was %i\n", FLAGS_clone);
423 exit(-1);
424 }
425
scroggo@google.com1125d392013-05-03 20:43:37 +0000426 if (FLAGS_writeEncodedImages) {
427 if (FLAGS_writePath.isEmpty()) {
428 SkDebugf("--writeEncodedImages requires --writePath\n");
429 exit(-1);
430 }
431 if (FLAGS_deferImageDecoding) {
432 SkDebugf("--writeEncodedImages is not compatible with --deferImageDecoding\n");
433 exit(-1);
434 }
435 }
436
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000437 SkString errorString;
438 SkAutoTUnref<sk_tools::PictureRenderer> renderer(parseRenderer(errorString,
439 kRender_PictureTool));
440 if (errorString.size() > 0) {
441 SkDebugf("%s\n", errorString.c_str());
442 }
443
444 if (renderer.get() == NULL) {
445 exit(-1);
446 }
447
borenet@google.com66bcbd12012-09-17 18:26:06 +0000448 SkAutoGraphics ag;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000449
450 SkString outputDir;
scroggo@google.com604e0c22013-04-09 21:25:46 +0000451 if (FLAGS_writePath.count() == 1) {
452 outputDir.set(FLAGS_writePath[0]);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000453 }
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000454 sk_tools::ImageResultsAndExpectations jsonSummary;
455 sk_tools::ImageResultsAndExpectations* jsonSummaryPtr = NULL;
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000456 if (FLAGS_writeJsonSummaryPath.count() == 1) {
457 jsonSummaryPtr = &jsonSummary;
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000458 if (FLAGS_readJsonSummaryPath.count() == 1) {
459 SkASSERT(jsonSummary.readExpectationsFile(FLAGS_readJsonSummaryPath[0]));
460 }
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000461 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000462
borenet@google.com66bcbd12012-09-17 18:26:06 +0000463 int failures = 0;
scroggo@google.com604e0c22013-04-09 21:25:46 +0000464 for (int i = 0; i < FLAGS_readPath.count(); i ++) {
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000465 failures += process_input(FLAGS_readPath[i], &outputDir, *renderer.get(), jsonSummaryPtr);
junov@chromium.org777442d2012-06-12 14:56:36 +0000466 }
borenet@google.com66bcbd12012-09-17 18:26:06 +0000467 if (failures != 0) {
468 SkDebugf("Failed to render %i pictures.\n", failures);
469 return 1;
470 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000471#if SK_SUPPORT_GPU
472#if GR_CACHE_STATS
473 if (renderer->isUsingGpuDevice()) {
474 GrContext* ctx = renderer->getGrContext();
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000475 ctx->printCacheStats();
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000476#ifdef SK_DEVELOPER
477 ctx->dumpFontCache();
478#endif
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000479 }
480#endif
481#endif
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000482 if (FLAGS_writeJsonSummaryPath.count() == 1) {
483 jsonSummary.writeToFile(FLAGS_writeJsonSummaryPath[0]);
484 }
caryclark@google.com868e1f62012-10-02 20:00:03 +0000485 return 0;
junov@chromium.org777442d2012-06-12 14:56:36 +0000486}
caryclark@google.com5987f582012-10-02 18:33:14 +0000487
488#if !defined SK_BUILD_FOR_IOS
489int main(int argc, char * const argv[]) {
490 return tool_main(argc, (char**) argv);
491}
492#endif