blob: b067a652bd430c004be3169ba87ead887c9cf3b3 [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"
19#include "SkStream.h"
20#include "SkString.h"
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000021#include "PictureRenderer.h"
scroggo@google.com161e1ba2013-03-04 16:41:06 +000022#include "PictureRenderingFlags.h"
twiz@google.coma31b8bb2012-06-22 18:24:56 +000023#include "picture_utils.h"
junov@chromium.org777442d2012-06-12 14:56:36 +000024
scroggo@google.com161e1ba2013-03-04 16:41:06 +000025// Flags used by this file, alphabetically:
26DEFINE_int32(clone, 0, "Clone the picture n times before rendering.");
27DECLARE_bool(deferImageDecoding);
28DEFINE_int32(maxComponentDiff, 256, "Maximum diff on a component, 0 - 256. Components that differ "
29 "by more than this amount are considered errors, though all diffs are reported. "
30 "Requires --validate.");
scroggo@google.com604e0c22013-04-09 21:25:46 +000031DECLARE_string(readPath);
scroggo@google.com1125d392013-05-03 20:43:37 +000032DEFINE_bool(writeEncodedImages, false, "Any time the skp contains an encoded image, write it to a "
33 "file rather than decoding it. Requires writePath to be set. Skips drawing the full "
34 "skp to a file. Not compatible with deferImageDecoding.");
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +000035DEFINE_string(writeJsonSummaryPath, "", "File to write a JSON summary of image results to. "
36 "TODO(epoger): Currently, this only works if --writePath is also specified.");
scroggo@google.com604e0c22013-04-09 21:25:46 +000037DEFINE_string2(writePath, w, "", "Directory to write the rendered images.");
scroggo@google.com161e1ba2013-03-04 16:41:06 +000038DEFINE_bool(writeWholeImage, false, "In tile mode, write the entire rendered image to a "
39 "file, instead of an image for each tile.");
40DEFINE_bool(validate, false, "Verify that the rendered image contains the same pixels as "
junov@chromium.orge286e842013-03-13 17:27:16 +000041 "the picture rendered in simple mode. When used in conjunction with --bbh, results "
42 "are validated against the picture rendered in the same mode, but without the bbh.");
junov@chromium.org777442d2012-06-12 14:56:36 +000043
commit-bot@chromium.orgff36a1d2013-07-24 20:37:30 +000044DEFINE_bool(bench_record, false, "If true, drop into an infinite loop of recording the picture.");
45
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +000046static void make_output_filepath(SkString* path, const SkString& dir,
junov@chromium.org777442d2012-06-12 14:56:36 +000047 const SkString& name) {
twiz@google.coma31b8bb2012-06-22 18:24:56 +000048 sk_tools::make_filepath(path, dir, name);
scroggo@google.com81f9d2e2012-09-20 14:54:21 +000049 // Remove ".skp"
50 path->remove(path->size() - 4, 4);
junov@chromium.org777442d2012-06-12 14:56:36 +000051}
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);
twiz@google.coma31b8bb2012-06-22 18:24:56 +0000145
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000146 SkFILEStream inputStream;
twiz@google.coma31b8bb2012-06-22 18:24:56 +0000147 inputStream.setPath(inputPath.c_str());
148 if (!inputStream.isValid()) {
149 SkDebugf("Could not open file %s\n", inputPath.c_str());
borenet@google.com66bcbd12012-09-17 18:26:06 +0000150 return false;
twiz@google.coma31b8bb2012-06-22 18:24:56 +0000151 }
152
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000153 SkPicture::InstallPixelRefProc proc;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000154 if (FLAGS_deferImageDecoding) {
commit-bot@chromium.org56799e22013-07-16 18:21:46 +0000155 proc = &sk_tools::LazyDecodeBitmap;
scroggo@google.com1125d392013-05-03 20:43:37 +0000156 } else if (FLAGS_writeEncodedImages) {
157 SkASSERT(!FLAGS_writePath.isEmpty());
158 reset_image_file_base_name(inputFilename);
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000159 proc = &write_image_to_file;
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000160 } else {
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000161 proc = &SkImageDecoder::DecodeMemory;
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000162 }
scroggo@google.com1125d392013-05-03 20:43:37 +0000163
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000164 SkDebugf("deserializing... %s\n", inputPath.c_str());
165
166 SkPicture* picture = SkPicture::CreateFromStream(&inputStream, proc);
167
168 if (NULL == picture) {
borenet@google.com66bcbd12012-09-17 18:26:06 +0000169 SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str());
170 return false;
171 }
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000172
commit-bot@chromium.orgff36a1d2013-07-24 20:37:30 +0000173 while (FLAGS_bench_record) {
174 const int kRecordFlags = 0;
175 SkPicture other;
176 picture->draw(other.beginRecording(picture->width(), picture->height(), kRecordFlags));
177 other.endRecording();
178 }
179
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000180 for (int i = 0; i < FLAGS_clone; ++i) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000181 SkPicture* clone = picture->clone();
182 SkDELETE(picture);
183 picture = clone;
184 }
185
186 SkDebugf("drawing... [%i %i] %s\n", picture->width(), picture->height(),
borenet@google.com03fcee82012-09-10 18:18:38 +0000187 inputPath.c_str());
skia.committer@gmail.com1d225f22012-09-14 02:01:10 +0000188
edisonn@google.com84f548c2012-12-18 22:24:03 +0000189 renderer.init(picture);
scroggo@google.comb4773b42012-10-01 20:06:09 +0000190 renderer.setup();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000191
borenet@google.com070d3542012-10-26 13:26:55 +0000192 SkString* outputPath = NULL;
scroggo@google.com1125d392013-05-03 20:43:37 +0000193 if (NULL != outputDir && outputDir->size() > 0 && !FLAGS_writeEncodedImages) {
borenet@google.com070d3542012-10-26 13:26:55 +0000194 outputPath = SkNEW(SkString);
195 make_output_filepath(outputPath, *outputDir, inputFilename);
196 }
edisonn@google.com84f548c2012-12-18 22:24:03 +0000197
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000198 bool success = renderer.render(outputPath, out);
borenet@google.com070d3542012-10-26 13:26:55 +0000199 if (outputPath) {
200 if (!success) {
201 SkDebugf("Could not write to file %s\n", outputPath->c_str());
202 }
203 SkDELETE(outputPath);
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000204 }
scroggo@google.com9a412522012-09-07 15:21:18 +0000205
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000206 renderer.end();
edisonn@google.com84f548c2012-12-18 22:24:03 +0000207
208 SkDELETE(picture);
borenet@google.com66bcbd12012-09-17 18:26:06 +0000209 return success;
junov@chromium.org777442d2012-06-12 14:56:36 +0000210}
211
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000212static inline int getByte(uint32_t value, int index) {
213 SkASSERT(0 <= index && index < 4);
214 return (value >> (index * 8)) & 0xFF;
215}
216
217static int MaxByteDiff(uint32_t v1, uint32_t v2) {
218 return SkMax32(SkMax32(abs(getByte(v1, 0) - getByte(v2, 0)), abs(getByte(v1, 1) - getByte(v2, 1))),
219 SkMax32(abs(getByte(v1, 2) - getByte(v2, 2)), abs(getByte(v1, 3) - getByte(v2, 3))));
220}
221
junov@chromium.orge286e842013-03-13 17:27:16 +0000222namespace {
223class 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};
247}
248
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000249/**
250 * Render the SKP file(s) within inputPath, writing their bitmap images into outputDir.
251 *
252 * @param inputPath path to an individual SKP file, or a directory of SKP files
253 * @param outputDir if not NULL, write the image(s) generated into this directory
254 * @param renderer PictureRenderer to use to render the SKPs
255 * @param jsonSummaryPtr if not NULL, add the image(s) generated to this summary
256 */
edisonn@google.com84f548c2012-12-18 22:24:03 +0000257static bool render_picture(const SkString& inputPath, const SkString* outputDir,
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000258 sk_tools::PictureRenderer& renderer,
259 sk_tools::ImageResultsSummary *jsonSummaryPtr) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000260 int diffs[256] = {0};
edisonn@google.com84f548c2012-12-18 22:24:03 +0000261 SkBitmap* bitmap = NULL;
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000262 renderer.setJsonSummaryPtr(jsonSummaryPtr);
263 bool success = render_picture_internal(inputPath,
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000264 FLAGS_writeWholeImage ? NULL : outputDir,
edisonn@google.com84f548c2012-12-18 22:24:03 +0000265 renderer,
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000266 FLAGS_validate || FLAGS_writeWholeImage ? &bitmap : NULL);
edisonn@google.com84f548c2012-12-18 22:24:03 +0000267
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000268 if (!success || ((FLAGS_validate || FLAGS_writeWholeImage) && bitmap == NULL)) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000269 SkDebugf("Failed to draw the picture.\n");
270 SkDELETE(bitmap);
271 return false;
272 }
273
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000274 if (FLAGS_validate) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000275 SkBitmap* referenceBitmap = NULL;
junov@chromium.orge286e842013-03-13 17:27:16 +0000276 sk_tools::PictureRenderer* referenceRenderer;
277 // If the renderer uses a BBoxHierarchy, then the reference renderer
skia.committer@gmail.com03682be2013-03-14 07:02:51 +0000278 // will be the same renderer, without the bbh.
junov@chromium.orge286e842013-03-13 17:27:16 +0000279 AutoRestoreBbhType arbbh;
280 if (sk_tools::PictureRenderer::kNone_BBoxHierarchyType !=
281 renderer.getBBoxHierarchyType()) {
282 referenceRenderer = &renderer;
283 referenceRenderer->ref(); // to match auto unref below
284 arbbh.set(referenceRenderer, sk_tools::PictureRenderer::kNone_BBoxHierarchyType);
285 } else {
286 referenceRenderer = SkNEW(sk_tools::SimplePictureRenderer);
287 }
288 SkAutoTUnref<sk_tools::PictureRenderer> aurReferenceRenderer(referenceRenderer);
289
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000290 success = render_picture_internal(inputPath, NULL, *referenceRenderer,
291 &referenceBitmap);
edisonn@google.com84f548c2012-12-18 22:24:03 +0000292
junov@chromium.orgc19c1912013-03-12 19:56:49 +0000293 if (!success || NULL == referenceBitmap || NULL == referenceBitmap->getPixels()) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000294 SkDebugf("Failed to draw the reference picture.\n");
295 SkDELETE(bitmap);
296 SkDELETE(referenceBitmap);
297 return false;
298 }
299
300 if (success && (bitmap->width() != referenceBitmap->width())) {
301 SkDebugf("Expected image width: %i, actual image width %i.\n",
302 referenceBitmap->width(), bitmap->width());
303 SkDELETE(bitmap);
304 SkDELETE(referenceBitmap);
305 return false;
306 }
307 if (success && (bitmap->height() != referenceBitmap->height())) {
308 SkDebugf("Expected image height: %i, actual image height %i",
309 referenceBitmap->height(), bitmap->height());
310 SkDELETE(bitmap);
311 SkDELETE(referenceBitmap);
312 return false;
313 }
skia.committer@gmail.coma7d8e3e2012-12-19 02:01:38 +0000314
edisonn@google.com84f548c2012-12-18 22:24:03 +0000315 for (int y = 0; success && y < bitmap->height(); y++) {
316 for (int x = 0; success && x < bitmap->width(); x++) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000317 int diff = MaxByteDiff(*referenceBitmap->getAddr32(x, y),
318 *bitmap->getAddr32(x, y));
319 SkASSERT(diff >= 0 && diff <= 255);
320 diffs[diff]++;
skia.committer@gmail.com4d28d982013-01-17 07:06:06 +0000321
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000322 if (diff > FLAGS_maxComponentDiff) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000323 SkDebugf("Expected pixel at (%i %i) exceedds maximum "
324 "component diff of %i: 0x%x, actual 0x%x\n",
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000325 x, y, FLAGS_maxComponentDiff,
edisonn@google.com01754bf2013-01-11 16:08:07 +0000326 *referenceBitmap->getAddr32(x, y),
edisonn@google.com84f548c2012-12-18 22:24:03 +0000327 *bitmap->getAddr32(x, y));
328 SkDELETE(bitmap);
329 SkDELETE(referenceBitmap);
330 return false;
331 }
332 }
333 }
334 SkDELETE(referenceBitmap);
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000335
336 for (int i = 1; i <= 255; ++i) {
337 if(diffs[i] > 0) {
338 SkDebugf("Number of pixels with max diff of %i is %i\n", i, diffs[i]);
339 }
340 }
edisonn@google.com84f548c2012-12-18 22:24:03 +0000341 }
342
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000343 if (FLAGS_writeWholeImage) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000344 sk_tools::force_all_opaque(*bitmap);
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000345
346 if (NULL != jsonSummaryPtr) {
347 // EPOGER: This is a hacky way of constructing the filename associated with the
348 // image checksum; we basically are repeating the logic of make_output_filepath()
349 // and code below here, within here.
350 // It would be better for the filename (without outputDir) to be passed in here,
351 // and used both for the checksum file and writing into outputDir.
352 //
353 // EPOGER: what about including the config type within hashFilename? That way,
354 // we could combine results of different config types without conflicting filenames.
355 SkString hashFilename;
356 sk_tools::get_basename(&hashFilename, inputPath);
357 hashFilename.remove(hashFilename.size() - 4, 4); // Remove ".skp"
358 hashFilename.append(".png");
359 jsonSummaryPtr->add(hashFilename.c_str(), *bitmap);
360 }
361
362 if (NULL != outputDir) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000363 SkString inputFilename;
364 sk_tools::get_basename(&inputFilename, inputPath);
365 SkString outputPath;
366 make_output_filepath(&outputPath, *outputDir, inputFilename);
367 outputPath.append(".png");
368 if (!SkImageEncoder::EncodeFile(outputPath.c_str(), *bitmap,
369 SkImageEncoder::kPNG_Type, 100)) {
370 SkDebugf("Failed to draw the picture.\n");
371 success = false;
372 }
373 }
374 }
375 SkDELETE(bitmap);
376
377 return success;
378}
379
380
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000381static int process_input(const char* input, const SkString* outputDir,
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000382 sk_tools::PictureRenderer& renderer,
383 sk_tools::ImageResultsSummary *jsonSummaryPtr) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000384 SkOSFile::Iter iter(input, "skp");
junov@chromium.org777442d2012-06-12 14:56:36 +0000385 SkString inputFilename;
borenet@google.com66bcbd12012-09-17 18:26:06 +0000386 int failures = 0;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000387 SkDebugf("process_input, %s\n", input);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000388 if (iter.next(&inputFilename)) {
389 do {
390 SkString inputPath;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000391 SkString inputAsSkString(input);
392 sk_tools::make_filepath(&inputPath, inputAsSkString, inputFilename);
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000393 if (!render_picture(inputPath, outputDir, renderer, jsonSummaryPtr)) {
borenet@google.com57837bf2012-09-19 17:28:29 +0000394 ++failures;
395 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000396 } while(iter.next(&inputFilename));
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000397 } else if (SkStrEndsWith(input, ".skp")) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000398 SkString inputPath(input);
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000399 if (!render_picture(inputPath, outputDir, renderer, jsonSummaryPtr)) {
borenet@google.com57837bf2012-09-19 17:28:29 +0000400 ++failures;
401 }
402 } else {
403 SkString warning;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000404 warning.printf("Warning: skipping %s\n", input);
borenet@google.com57837bf2012-09-19 17:28:29 +0000405 SkDebugf(warning.c_str());
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000406 }
borenet@google.com66bcbd12012-09-17 18:26:06 +0000407 return failures;
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000408}
409
caryclark@google.com5987f582012-10-02 18:33:14 +0000410int tool_main(int argc, char** argv);
411int tool_main(int argc, char** argv) {
scroggo@google.comd9ba9a02013-03-21 19:43:15 +0000412 SkCommandLineFlags::SetUsage("Render .skp files.");
413 SkCommandLineFlags::Parse(argc, argv);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000414
scroggo@google.com604e0c22013-04-09 21:25:46 +0000415 if (FLAGS_readPath.isEmpty()) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000416 SkDebugf(".skp files or directories are required.\n");
417 exit(-1);
418 }
419
420 if (FLAGS_maxComponentDiff < 0 || FLAGS_maxComponentDiff > 256) {
421 SkDebugf("--maxComponentDiff must be between 0 and 256\n");
422 exit(-1);
423 }
424
425 if (FLAGS_maxComponentDiff != 256 && !FLAGS_validate) {
426 SkDebugf("--maxComponentDiff requires --validate\n");
427 exit(-1);
428 }
429
430 if (FLAGS_clone < 0) {
431 SkDebugf("--clone must be >= 0. Was %i\n", FLAGS_clone);
432 exit(-1);
433 }
434
scroggo@google.com1125d392013-05-03 20:43:37 +0000435 if (FLAGS_writeEncodedImages) {
436 if (FLAGS_writePath.isEmpty()) {
437 SkDebugf("--writeEncodedImages requires --writePath\n");
438 exit(-1);
439 }
440 if (FLAGS_deferImageDecoding) {
441 SkDebugf("--writeEncodedImages is not compatible with --deferImageDecoding\n");
442 exit(-1);
443 }
444 }
445
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000446 SkString errorString;
447 SkAutoTUnref<sk_tools::PictureRenderer> renderer(parseRenderer(errorString,
448 kRender_PictureTool));
449 if (errorString.size() > 0) {
450 SkDebugf("%s\n", errorString.c_str());
451 }
452
453 if (renderer.get() == NULL) {
454 exit(-1);
455 }
456
borenet@google.com66bcbd12012-09-17 18:26:06 +0000457 SkAutoGraphics ag;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000458
459 SkString outputDir;
scroggo@google.com604e0c22013-04-09 21:25:46 +0000460 if (FLAGS_writePath.count() == 1) {
461 outputDir.set(FLAGS_writePath[0]);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000462 }
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000463 sk_tools::ImageResultsSummary jsonSummary;
464 sk_tools::ImageResultsSummary* jsonSummaryPtr = NULL;
465 if (FLAGS_writeJsonSummaryPath.count() == 1) {
466 jsonSummaryPtr = &jsonSummary;
467 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000468
borenet@google.com66bcbd12012-09-17 18:26:06 +0000469 int failures = 0;
scroggo@google.com604e0c22013-04-09 21:25:46 +0000470 for (int i = 0; i < FLAGS_readPath.count(); i ++) {
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000471 failures += process_input(FLAGS_readPath[i], &outputDir, *renderer.get(), jsonSummaryPtr);
junov@chromium.org777442d2012-06-12 14:56:36 +0000472 }
borenet@google.com66bcbd12012-09-17 18:26:06 +0000473 if (failures != 0) {
474 SkDebugf("Failed to render %i pictures.\n", failures);
475 return 1;
476 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000477#if SK_SUPPORT_GPU
478#if GR_CACHE_STATS
479 if (renderer->isUsingGpuDevice()) {
480 GrContext* ctx = renderer->getGrContext();
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000481 ctx->printCacheStats();
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000482#ifdef SK_DEVELOPER
483 ctx->dumpFontCache();
484#endif
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000485 }
486#endif
487#endif
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000488 if (FLAGS_writeJsonSummaryPath.count() == 1) {
489 jsonSummary.writeToFile(FLAGS_writeJsonSummaryPath[0]);
490 }
caryclark@google.com868e1f62012-10-02 20:00:03 +0000491 return 0;
junov@chromium.org777442d2012-06-12 14:56:36 +0000492}
caryclark@google.com5987f582012-10-02 18:33:14 +0000493
494#if !defined SK_BUILD_FOR_IOS
495int main(int argc, char * const argv[]) {
496 return tool_main(argc, (char**) argv);
497}
498#endif