blob: 1bc4d21e33098edc3cb5a0f06f9b3fdce9a99bfc [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.orgee18f2a2014-05-08 14:48:44 +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.");
scroggo@google.com604e0c22013-04-09 21:25:46 +000034DECLARE_string(readPath);
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000035DEFINE_bool(writeChecksumBasedFilenames, false,
36 "When writing out images, use checksum-based filenames.");
scroggo@google.com1125d392013-05-03 20:43:37 +000037DEFINE_bool(writeEncodedImages, false, "Any time the skp contains an encoded image, write it to a "
38 "file rather than decoding it. Requires writePath to be set. Skips drawing the full "
39 "skp to a file. Not compatible with deferImageDecoding.");
commit-bot@chromium.org4610a462014-04-29 19:39:22 +000040DEFINE_string(writeJsonSummaryPath, "", "File to write a JSON summary of image results to.");
41DEFINE_string2(writePath, w, "", "Directory to write the rendered images into.");
scroggo@google.com161e1ba2013-03-04 16:41:06 +000042DEFINE_bool(writeWholeImage, false, "In tile mode, write the entire rendered image to a "
43 "file, instead of an image for each tile.");
44DEFINE_bool(validate, false, "Verify that the rendered image contains the same pixels as "
junov@chromium.orge286e842013-03-13 17:27:16 +000045 "the picture rendered in simple mode. When used in conjunction with --bbh, results "
46 "are validated against the picture rendered in the same mode, but without the bbh.");
junov@chromium.org777442d2012-06-12 14:56:36 +000047
commit-bot@chromium.orgff36a1d2013-07-24 20:37:30 +000048DEFINE_bool(bench_record, false, "If true, drop into an infinite loop of recording the picture.");
49
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000050DEFINE_bool(preprocess, false, "If true, perform device specific preprocessing before rendering.");
51
scroggo@google.com1125d392013-05-03 20:43:37 +000052////////////////////////////////////////////////////////////////////////////////////////////////////
53
54/**
55 * Table for translating from format of data to a suffix.
56 */
57struct Format {
58 SkImageDecoder::Format fFormat;
59 const char* fSuffix;
60};
61static const Format gFormats[] = {
62 { SkImageDecoder::kBMP_Format, ".bmp" },
63 { SkImageDecoder::kGIF_Format, ".gif" },
64 { SkImageDecoder::kICO_Format, ".ico" },
65 { SkImageDecoder::kJPEG_Format, ".jpg" },
66 { SkImageDecoder::kPNG_Format, ".png" },
67 { SkImageDecoder::kWBMP_Format, ".wbmp" },
68 { SkImageDecoder::kWEBP_Format, ".webp" },
69 { SkImageDecoder::kUnknown_Format, "" },
70};
71
72/**
73 * Get an appropriate suffix for an image format.
74 */
75static const char* get_suffix_from_format(SkImageDecoder::Format format) {
76 for (size_t i = 0; i < SK_ARRAY_COUNT(gFormats); i++) {
77 if (gFormats[i].fFormat == format) {
78 return gFormats[i].fSuffix;
79 }
80 }
81 return "";
82}
83
84/**
85 * Base name for an image file created from the encoded data in an skp.
86 */
87static SkString gInputFileName;
88
89/**
90 * Number to be appended to the image file name so that it is unique.
91 */
92static uint32_t gImageNo;
93
94/**
95 * Set up the name for writing encoded data to a file.
96 * Sets gInputFileName to name, minus any extension ".*"
97 * Sets gImageNo to 0, so images from file "X.skp" will
98 * look like "X_<gImageNo>.<suffix>", beginning with 0
99 * for each new skp.
100 */
101static void reset_image_file_base_name(const SkString& name) {
102 gImageNo = 0;
103 // Remove ".skp"
104 const char* cName = name.c_str();
105 const char* dot = strrchr(cName, '.');
106 if (dot != NULL) {
107 gInputFileName.set(cName, dot - cName);
108 } else {
109 gInputFileName.set(name);
110 }
111}
112
113/**
114 * Write the raw encoded bitmap data to a file.
115 */
116static bool write_image_to_file(const void* buffer, size_t size, SkBitmap* bitmap) {
117 SkASSERT(!FLAGS_writePath.isEmpty());
118 SkMemoryStream memStream(buffer, size);
119 SkString outPath;
120 SkImageDecoder::Format format = SkImageDecoder::GetStreamFormat(&memStream);
121 SkString name = SkStringPrintf("%s_%d%s", gInputFileName.c_str(), gImageNo++,
122 get_suffix_from_format(format));
123 SkString dir(FLAGS_writePath[0]);
124 sk_tools::make_filepath(&outPath, dir, name);
125 SkFILEWStream fileStream(outPath.c_str());
126 if (!(fileStream.isValid() && fileStream.write(buffer, size))) {
127 SkDebugf("Failed to write encoded data to \"%s\"\n", outPath.c_str());
128 }
129 // Put in a dummy bitmap.
130 return SkImageDecoder::DecodeStream(&memStream, bitmap, SkBitmap::kNo_Config,
131 SkImageDecoder::kDecodeBounds_Mode);
132}
133
134////////////////////////////////////////////////////////////////////////////////////////////////////
135
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000136/**
137 * Called only by render_picture().
138 */
139static bool render_picture_internal(const SkString& inputPath, const SkString* outputDir,
140 sk_tools::PictureRenderer& renderer,
141 SkBitmap** out) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000142 SkString inputFilename;
143 sk_tools::get_basename(&inputFilename, inputPath);
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000144 SkString outputDirString;
145 if (NULL != outputDir && outputDir->size() > 0 && !FLAGS_writeEncodedImages) {
146 outputDirString.set(*outputDir);
147 }
twiz@google.coma31b8bb2012-06-22 18:24:56 +0000148
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000149 SkFILEStream inputStream;
twiz@google.coma31b8bb2012-06-22 18:24:56 +0000150 inputStream.setPath(inputPath.c_str());
151 if (!inputStream.isValid()) {
152 SkDebugf("Could not open file %s\n", inputPath.c_str());
borenet@google.com66bcbd12012-09-17 18:26:06 +0000153 return false;
twiz@google.coma31b8bb2012-06-22 18:24:56 +0000154 }
155
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000156 SkPicture::InstallPixelRefProc proc;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000157 if (FLAGS_deferImageDecoding) {
commit-bot@chromium.org56799e22013-07-16 18:21:46 +0000158 proc = &sk_tools::LazyDecodeBitmap;
scroggo@google.com1125d392013-05-03 20:43:37 +0000159 } else if (FLAGS_writeEncodedImages) {
160 SkASSERT(!FLAGS_writePath.isEmpty());
161 reset_image_file_base_name(inputFilename);
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000162 proc = &write_image_to_file;
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000163 } else {
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000164 proc = &SkImageDecoder::DecodeMemory;
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000165 }
scroggo@google.com1125d392013-05-03 20:43:37 +0000166
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000167 SkDebugf("deserializing... %s\n", inputPath.c_str());
168
169 SkPicture* picture = SkPicture::CreateFromStream(&inputStream, proc);
170
171 if (NULL == picture) {
borenet@google.com66bcbd12012-09-17 18:26:06 +0000172 SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str());
173 return false;
174 }
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000175
commit-bot@chromium.orgff36a1d2013-07-24 20:37:30 +0000176 while (FLAGS_bench_record) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000177 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000178 picture->draw(recorder.beginRecording(picture->width(), picture->height(), NULL, 0));
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000179 SkAutoTUnref<SkPicture> other(recorder.endRecording());
commit-bot@chromium.orgff36a1d2013-07-24 20:37:30 +0000180 }
181
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000182 for (int i = 0; i < FLAGS_clone; ++i) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000183 SkPicture* clone = picture->clone();
184 SkDELETE(picture);
185 picture = clone;
186 }
187
188 SkDebugf("drawing... [%i %i] %s\n", picture->width(), picture->height(),
borenet@google.com03fcee82012-09-10 18:18:38 +0000189 inputPath.c_str());
skia.committer@gmail.com1d225f22012-09-14 02:01:10 +0000190
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000191 renderer.init(picture, &outputDirString, &inputFilename, FLAGS_writeChecksumBasedFilenames);
skia.committer@gmail.comeb849e52014-03-17 03:02:17 +0000192
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000193 if (FLAGS_preprocess) {
194 if (NULL != renderer.getCanvas()) {
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000195 renderer.getCanvas()->EXPERIMENTAL_optimize(renderer.getPicture());
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000196 }
197 }
198
scroggo@google.comb4773b42012-10-01 20:06:09 +0000199 renderer.setup();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000200
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000201 bool success = renderer.render(out);
202 if (!success) {
203 SkDebugf("Failed to render %s\n", inputFilename.c_str());
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
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000344 SkString inputFilename, outputPath;
345 sk_tools::get_basename(&inputFilename, inputPath);
346 sk_tools::make_filepath(&outputPath, *outputDir, inputFilename);
347 sk_tools::replace_char(&outputPath, '.', '_');
348 outputPath.append(".png");
349
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000350 if (NULL != jsonSummaryPtr) {
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000351 SkString outputFileBasename;
352 sk_tools::get_basename(&outputFileBasename, outputPath);
353 jsonSummaryPtr->add(inputFilename.c_str(), outputFileBasename.c_str(), *bitmap);
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000354 }
355
356 if (NULL != outputDir) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000357 if (!SkImageEncoder::EncodeFile(outputPath.c_str(), *bitmap,
358 SkImageEncoder::kPNG_Type, 100)) {
359 SkDebugf("Failed to draw the picture.\n");
360 success = false;
361 }
362 }
363 }
364 SkDELETE(bitmap);
365
366 return success;
367}
368
369
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000370static int process_input(const char* input, const SkString* outputDir,
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000371 sk_tools::PictureRenderer& renderer,
372 sk_tools::ImageResultsSummary *jsonSummaryPtr) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000373 SkOSFile::Iter iter(input, "skp");
junov@chromium.org777442d2012-06-12 14:56:36 +0000374 SkString inputFilename;
borenet@google.com66bcbd12012-09-17 18:26:06 +0000375 int failures = 0;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000376 SkDebugf("process_input, %s\n", input);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000377 if (iter.next(&inputFilename)) {
378 do {
379 SkString inputPath;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000380 SkString inputAsSkString(input);
381 sk_tools::make_filepath(&inputPath, inputAsSkString, inputFilename);
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000382 if (!render_picture(inputPath, outputDir, renderer, jsonSummaryPtr)) {
borenet@google.com57837bf2012-09-19 17:28:29 +0000383 ++failures;
384 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000385 } while(iter.next(&inputFilename));
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000386 } else if (SkStrEndsWith(input, ".skp")) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000387 SkString inputPath(input);
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000388 if (!render_picture(inputPath, outputDir, renderer, jsonSummaryPtr)) {
borenet@google.com57837bf2012-09-19 17:28:29 +0000389 ++failures;
390 }
391 } else {
392 SkString warning;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000393 warning.printf("Warning: skipping %s\n", input);
borenet@google.com57837bf2012-09-19 17:28:29 +0000394 SkDebugf(warning.c_str());
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000395 }
borenet@google.com66bcbd12012-09-17 18:26:06 +0000396 return failures;
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000397}
398
caryclark@google.com5987f582012-10-02 18:33:14 +0000399int tool_main(int argc, char** argv);
400int tool_main(int argc, char** argv) {
scroggo@google.comd9ba9a02013-03-21 19:43:15 +0000401 SkCommandLineFlags::SetUsage("Render .skp files.");
402 SkCommandLineFlags::Parse(argc, argv);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000403
scroggo@google.com604e0c22013-04-09 21:25:46 +0000404 if (FLAGS_readPath.isEmpty()) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000405 SkDebugf(".skp files or directories are required.\n");
406 exit(-1);
407 }
408
409 if (FLAGS_maxComponentDiff < 0 || FLAGS_maxComponentDiff > 256) {
410 SkDebugf("--maxComponentDiff must be between 0 and 256\n");
411 exit(-1);
412 }
413
414 if (FLAGS_maxComponentDiff != 256 && !FLAGS_validate) {
415 SkDebugf("--maxComponentDiff requires --validate\n");
416 exit(-1);
417 }
418
419 if (FLAGS_clone < 0) {
420 SkDebugf("--clone must be >= 0. Was %i\n", FLAGS_clone);
421 exit(-1);
422 }
423
scroggo@google.com1125d392013-05-03 20:43:37 +0000424 if (FLAGS_writeEncodedImages) {
425 if (FLAGS_writePath.isEmpty()) {
426 SkDebugf("--writeEncodedImages requires --writePath\n");
427 exit(-1);
428 }
429 if (FLAGS_deferImageDecoding) {
430 SkDebugf("--writeEncodedImages is not compatible with --deferImageDecoding\n");
431 exit(-1);
432 }
433 }
434
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000435 SkString errorString;
436 SkAutoTUnref<sk_tools::PictureRenderer> renderer(parseRenderer(errorString,
437 kRender_PictureTool));
438 if (errorString.size() > 0) {
439 SkDebugf("%s\n", errorString.c_str());
440 }
441
442 if (renderer.get() == NULL) {
443 exit(-1);
444 }
445
borenet@google.com66bcbd12012-09-17 18:26:06 +0000446 SkAutoGraphics ag;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000447
448 SkString outputDir;
scroggo@google.com604e0c22013-04-09 21:25:46 +0000449 if (FLAGS_writePath.count() == 1) {
450 outputDir.set(FLAGS_writePath[0]);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000451 }
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000452 sk_tools::ImageResultsSummary jsonSummary;
453 sk_tools::ImageResultsSummary* jsonSummaryPtr = NULL;
454 if (FLAGS_writeJsonSummaryPath.count() == 1) {
455 jsonSummaryPtr = &jsonSummary;
456 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000457
borenet@google.com66bcbd12012-09-17 18:26:06 +0000458 int failures = 0;
scroggo@google.com604e0c22013-04-09 21:25:46 +0000459 for (int i = 0; i < FLAGS_readPath.count(); i ++) {
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000460 failures += process_input(FLAGS_readPath[i], &outputDir, *renderer.get(), jsonSummaryPtr);
junov@chromium.org777442d2012-06-12 14:56:36 +0000461 }
borenet@google.com66bcbd12012-09-17 18:26:06 +0000462 if (failures != 0) {
463 SkDebugf("Failed to render %i pictures.\n", failures);
464 return 1;
465 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000466#if SK_SUPPORT_GPU
467#if GR_CACHE_STATS
468 if (renderer->isUsingGpuDevice()) {
469 GrContext* ctx = renderer->getGrContext();
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000470 ctx->printCacheStats();
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000471#ifdef SK_DEVELOPER
472 ctx->dumpFontCache();
473#endif
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000474 }
475#endif
476#endif
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000477 if (FLAGS_writeJsonSummaryPath.count() == 1) {
478 jsonSummary.writeToFile(FLAGS_writeJsonSummaryPath[0]);
479 }
caryclark@google.com868e1f62012-10-02 20:00:03 +0000480 return 0;
junov@chromium.org777442d2012-06-12 14:56:36 +0000481}
caryclark@google.com5987f582012-10-02 18:33:14 +0000482
483#if !defined SK_BUILD_FOR_IOS
484int main(int argc, char * const argv[]) {
485 return tool_main(argc, (char**) argv);
486}
487#endif