blob: bc765f7e4096cc4426c2095ffb3acb75ccd5dc00 [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 +0000222class AutoRestoreBbhType {
223public:
224 AutoRestoreBbhType() {
225 fRenderer = NULL;
junov@chromium.orgd34fda12013-03-13 19:03:26 +0000226 fSavedBbhType = sk_tools::PictureRenderer::kNone_BBoxHierarchyType;
junov@chromium.orge286e842013-03-13 17:27:16 +0000227 }
228
229 void set(sk_tools::PictureRenderer* renderer,
230 sk_tools::PictureRenderer::BBoxHierarchyType bbhType) {
231 fRenderer = renderer;
232 fSavedBbhType = renderer->getBBoxHierarchyType();
233 renderer->setBBoxHierarchyType(bbhType);
234 }
235
236 ~AutoRestoreBbhType() {
237 if (NULL != fRenderer) {
238 fRenderer->setBBoxHierarchyType(fSavedBbhType);
239 }
240 }
241
242private:
243 sk_tools::PictureRenderer* fRenderer;
244 sk_tools::PictureRenderer::BBoxHierarchyType fSavedBbhType;
245};
junov@chromium.orge286e842013-03-13 17:27:16 +0000246
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000247/**
248 * Render the SKP file(s) within inputPath, writing their bitmap images into outputDir.
249 *
250 * @param inputPath path to an individual SKP file, or a directory of SKP files
251 * @param outputDir if not NULL, write the image(s) generated into this directory
252 * @param renderer PictureRenderer to use to render the SKPs
253 * @param jsonSummaryPtr if not NULL, add the image(s) generated to this summary
254 */
edisonn@google.com84f548c2012-12-18 22:24:03 +0000255static bool render_picture(const SkString& inputPath, const SkString* outputDir,
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000256 sk_tools::PictureRenderer& renderer,
257 sk_tools::ImageResultsSummary *jsonSummaryPtr) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000258 int diffs[256] = {0};
edisonn@google.com84f548c2012-12-18 22:24:03 +0000259 SkBitmap* bitmap = NULL;
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000260 renderer.setJsonSummaryPtr(jsonSummaryPtr);
261 bool success = render_picture_internal(inputPath,
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000262 FLAGS_writeWholeImage ? NULL : outputDir,
edisonn@google.com84f548c2012-12-18 22:24:03 +0000263 renderer,
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000264 FLAGS_validate || FLAGS_writeWholeImage ? &bitmap : NULL);
edisonn@google.com84f548c2012-12-18 22:24:03 +0000265
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000266 if (!success || ((FLAGS_validate || FLAGS_writeWholeImage) && bitmap == NULL)) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000267 SkDebugf("Failed to draw the picture.\n");
268 SkDELETE(bitmap);
269 return false;
270 }
271
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000272 if (FLAGS_validate) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000273 SkBitmap* referenceBitmap = NULL;
junov@chromium.orge286e842013-03-13 17:27:16 +0000274 sk_tools::PictureRenderer* referenceRenderer;
275 // If the renderer uses a BBoxHierarchy, then the reference renderer
skia.committer@gmail.com03682be2013-03-14 07:02:51 +0000276 // will be the same renderer, without the bbh.
junov@chromium.orge286e842013-03-13 17:27:16 +0000277 AutoRestoreBbhType arbbh;
278 if (sk_tools::PictureRenderer::kNone_BBoxHierarchyType !=
279 renderer.getBBoxHierarchyType()) {
280 referenceRenderer = &renderer;
281 referenceRenderer->ref(); // to match auto unref below
282 arbbh.set(referenceRenderer, sk_tools::PictureRenderer::kNone_BBoxHierarchyType);
283 } else {
284 referenceRenderer = SkNEW(sk_tools::SimplePictureRenderer);
285 }
286 SkAutoTUnref<sk_tools::PictureRenderer> aurReferenceRenderer(referenceRenderer);
287
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000288 success = render_picture_internal(inputPath, NULL, *referenceRenderer,
289 &referenceBitmap);
edisonn@google.com84f548c2012-12-18 22:24:03 +0000290
junov@chromium.orgc19c1912013-03-12 19:56:49 +0000291 if (!success || NULL == referenceBitmap || NULL == referenceBitmap->getPixels()) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000292 SkDebugf("Failed to draw the reference picture.\n");
293 SkDELETE(bitmap);
294 SkDELETE(referenceBitmap);
295 return false;
296 }
297
298 if (success && (bitmap->width() != referenceBitmap->width())) {
299 SkDebugf("Expected image width: %i, actual image width %i.\n",
300 referenceBitmap->width(), bitmap->width());
301 SkDELETE(bitmap);
302 SkDELETE(referenceBitmap);
303 return false;
304 }
305 if (success && (bitmap->height() != referenceBitmap->height())) {
306 SkDebugf("Expected image height: %i, actual image height %i",
307 referenceBitmap->height(), bitmap->height());
308 SkDELETE(bitmap);
309 SkDELETE(referenceBitmap);
310 return false;
311 }
skia.committer@gmail.coma7d8e3e2012-12-19 02:01:38 +0000312
edisonn@google.com84f548c2012-12-18 22:24:03 +0000313 for (int y = 0; success && y < bitmap->height(); y++) {
314 for (int x = 0; success && x < bitmap->width(); x++) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000315 int diff = MaxByteDiff(*referenceBitmap->getAddr32(x, y),
316 *bitmap->getAddr32(x, y));
317 SkASSERT(diff >= 0 && diff <= 255);
318 diffs[diff]++;
skia.committer@gmail.com4d28d982013-01-17 07:06:06 +0000319
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000320 if (diff > FLAGS_maxComponentDiff) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000321 SkDebugf("Expected pixel at (%i %i) exceedds maximum "
322 "component diff of %i: 0x%x, actual 0x%x\n",
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000323 x, y, FLAGS_maxComponentDiff,
edisonn@google.com01754bf2013-01-11 16:08:07 +0000324 *referenceBitmap->getAddr32(x, y),
edisonn@google.com84f548c2012-12-18 22:24:03 +0000325 *bitmap->getAddr32(x, y));
326 SkDELETE(bitmap);
327 SkDELETE(referenceBitmap);
328 return false;
329 }
330 }
331 }
332 SkDELETE(referenceBitmap);
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000333
334 for (int i = 1; i <= 255; ++i) {
335 if(diffs[i] > 0) {
336 SkDebugf("Number of pixels with max diff of %i is %i\n", i, diffs[i]);
337 }
338 }
edisonn@google.com84f548c2012-12-18 22:24:03 +0000339 }
340
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000341 if (FLAGS_writeWholeImage) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000342 sk_tools::force_all_opaque(*bitmap);
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000343
344 if (NULL != jsonSummaryPtr) {
345 // EPOGER: This is a hacky way of constructing the filename associated with the
346 // image checksum; we basically are repeating the logic of make_output_filepath()
347 // and code below here, within here.
348 // It would be better for the filename (without outputDir) to be passed in here,
349 // and used both for the checksum file and writing into outputDir.
350 //
351 // EPOGER: what about including the config type within hashFilename? That way,
352 // we could combine results of different config types without conflicting filenames.
353 SkString hashFilename;
354 sk_tools::get_basename(&hashFilename, inputPath);
355 hashFilename.remove(hashFilename.size() - 4, 4); // Remove ".skp"
356 hashFilename.append(".png");
357 jsonSummaryPtr->add(hashFilename.c_str(), *bitmap);
358 }
359
360 if (NULL != outputDir) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000361 SkString inputFilename;
362 sk_tools::get_basename(&inputFilename, inputPath);
363 SkString outputPath;
364 make_output_filepath(&outputPath, *outputDir, inputFilename);
365 outputPath.append(".png");
366 if (!SkImageEncoder::EncodeFile(outputPath.c_str(), *bitmap,
367 SkImageEncoder::kPNG_Type, 100)) {
368 SkDebugf("Failed to draw the picture.\n");
369 success = false;
370 }
371 }
372 }
373 SkDELETE(bitmap);
374
375 return success;
376}
377
378
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000379static int process_input(const char* input, const SkString* outputDir,
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000380 sk_tools::PictureRenderer& renderer,
381 sk_tools::ImageResultsSummary *jsonSummaryPtr) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000382 SkOSFile::Iter iter(input, "skp");
junov@chromium.org777442d2012-06-12 14:56:36 +0000383 SkString inputFilename;
borenet@google.com66bcbd12012-09-17 18:26:06 +0000384 int failures = 0;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000385 SkDebugf("process_input, %s\n", input);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000386 if (iter.next(&inputFilename)) {
387 do {
388 SkString inputPath;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000389 SkString inputAsSkString(input);
390 sk_tools::make_filepath(&inputPath, inputAsSkString, inputFilename);
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000391 if (!render_picture(inputPath, outputDir, renderer, jsonSummaryPtr)) {
borenet@google.com57837bf2012-09-19 17:28:29 +0000392 ++failures;
393 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000394 } while(iter.next(&inputFilename));
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000395 } else if (SkStrEndsWith(input, ".skp")) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000396 SkString inputPath(input);
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000397 if (!render_picture(inputPath, outputDir, renderer, jsonSummaryPtr)) {
borenet@google.com57837bf2012-09-19 17:28:29 +0000398 ++failures;
399 }
400 } else {
401 SkString warning;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000402 warning.printf("Warning: skipping %s\n", input);
borenet@google.com57837bf2012-09-19 17:28:29 +0000403 SkDebugf(warning.c_str());
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000404 }
borenet@google.com66bcbd12012-09-17 18:26:06 +0000405 return failures;
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000406}
407
caryclark@google.com5987f582012-10-02 18:33:14 +0000408int tool_main(int argc, char** argv);
409int tool_main(int argc, char** argv) {
scroggo@google.comd9ba9a02013-03-21 19:43:15 +0000410 SkCommandLineFlags::SetUsage("Render .skp files.");
411 SkCommandLineFlags::Parse(argc, argv);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000412
scroggo@google.com604e0c22013-04-09 21:25:46 +0000413 if (FLAGS_readPath.isEmpty()) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000414 SkDebugf(".skp files or directories are required.\n");
415 exit(-1);
416 }
417
418 if (FLAGS_maxComponentDiff < 0 || FLAGS_maxComponentDiff > 256) {
419 SkDebugf("--maxComponentDiff must be between 0 and 256\n");
420 exit(-1);
421 }
422
423 if (FLAGS_maxComponentDiff != 256 && !FLAGS_validate) {
424 SkDebugf("--maxComponentDiff requires --validate\n");
425 exit(-1);
426 }
427
428 if (FLAGS_clone < 0) {
429 SkDebugf("--clone must be >= 0. Was %i\n", FLAGS_clone);
430 exit(-1);
431 }
432
scroggo@google.com1125d392013-05-03 20:43:37 +0000433 if (FLAGS_writeEncodedImages) {
434 if (FLAGS_writePath.isEmpty()) {
435 SkDebugf("--writeEncodedImages requires --writePath\n");
436 exit(-1);
437 }
438 if (FLAGS_deferImageDecoding) {
439 SkDebugf("--writeEncodedImages is not compatible with --deferImageDecoding\n");
440 exit(-1);
441 }
442 }
443
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000444 SkString errorString;
445 SkAutoTUnref<sk_tools::PictureRenderer> renderer(parseRenderer(errorString,
446 kRender_PictureTool));
447 if (errorString.size() > 0) {
448 SkDebugf("%s\n", errorString.c_str());
449 }
450
451 if (renderer.get() == NULL) {
452 exit(-1);
453 }
454
borenet@google.com66bcbd12012-09-17 18:26:06 +0000455 SkAutoGraphics ag;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000456
457 SkString outputDir;
scroggo@google.com604e0c22013-04-09 21:25:46 +0000458 if (FLAGS_writePath.count() == 1) {
459 outputDir.set(FLAGS_writePath[0]);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000460 }
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000461 sk_tools::ImageResultsSummary jsonSummary;
462 sk_tools::ImageResultsSummary* jsonSummaryPtr = NULL;
463 if (FLAGS_writeJsonSummaryPath.count() == 1) {
464 jsonSummaryPtr = &jsonSummary;
465 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000466
borenet@google.com66bcbd12012-09-17 18:26:06 +0000467 int failures = 0;
scroggo@google.com604e0c22013-04-09 21:25:46 +0000468 for (int i = 0; i < FLAGS_readPath.count(); i ++) {
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000469 failures += process_input(FLAGS_readPath[i], &outputDir, *renderer.get(), jsonSummaryPtr);
junov@chromium.org777442d2012-06-12 14:56:36 +0000470 }
borenet@google.com66bcbd12012-09-17 18:26:06 +0000471 if (failures != 0) {
472 SkDebugf("Failed to render %i pictures.\n", failures);
473 return 1;
474 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000475#if SK_SUPPORT_GPU
476#if GR_CACHE_STATS
477 if (renderer->isUsingGpuDevice()) {
478 GrContext* ctx = renderer->getGrContext();
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000479 ctx->printCacheStats();
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000480#ifdef SK_DEVELOPER
481 ctx->dumpFontCache();
482#endif
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000483 }
484#endif
485#endif
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000486 if (FLAGS_writeJsonSummaryPath.count() == 1) {
487 jsonSummary.writeToFile(FLAGS_writeJsonSummaryPath[0]);
488 }
caryclark@google.com868e1f62012-10-02 20:00:03 +0000489 return 0;
junov@chromium.org777442d2012-06-12 14:56:36 +0000490}
caryclark@google.com5987f582012-10-02 18:33:14 +0000491
492#if !defined SK_BUILD_FOR_IOS
493int main(int argc, char * const argv[]) {
494 return tool_main(argc, (char**) argv);
495}
496#endif