blob: a8c393f985702aa29a9b267659cdd7415f23d530 [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. "
commit-bot@chromium.orgf11943f2014-03-12 20:09:46 +000036 "TODO(epoger): Currently, this only works if --writePath is also specified. "
37 "See https://code.google.com/p/skia/issues/detail?id=2043 .");
scroggo@google.com604e0c22013-04-09 21:25:46 +000038DEFINE_string2(writePath, w, "", "Directory to write the rendered images.");
scroggo@google.com161e1ba2013-03-04 16:41:06 +000039DEFINE_bool(writeWholeImage, false, "In tile mode, write the entire rendered image to a "
40 "file, instead of an image for each tile.");
41DEFINE_bool(validate, false, "Verify that the rendered image contains the same pixels as "
junov@chromium.orge286e842013-03-13 17:27:16 +000042 "the picture rendered in simple mode. When used in conjunction with --bbh, results "
43 "are validated against the picture rendered in the same mode, but without the bbh.");
junov@chromium.org777442d2012-06-12 14:56:36 +000044
commit-bot@chromium.orgff36a1d2013-07-24 20:37:30 +000045DEFINE_bool(bench_record, false, "If true, drop into an infinite loop of recording the picture.");
46
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000047DEFINE_bool(preprocess, false, "If true, perform device specific preprocessing before rendering.");
48
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +000049static void make_output_filepath(SkString* path, const SkString& dir,
junov@chromium.org777442d2012-06-12 14:56:36 +000050 const SkString& name) {
twiz@google.coma31b8bb2012-06-22 18:24:56 +000051 sk_tools::make_filepath(path, dir, name);
scroggo@google.com81f9d2e2012-09-20 14:54:21 +000052 // Remove ".skp"
53 path->remove(path->size() - 4, 4);
junov@chromium.org777442d2012-06-12 14:56:36 +000054}
55
scroggo@google.com1125d392013-05-03 20:43:37 +000056////////////////////////////////////////////////////////////////////////////////////////////////////
57
58/**
59 * Table for translating from format of data to a suffix.
60 */
61struct Format {
62 SkImageDecoder::Format fFormat;
63 const char* fSuffix;
64};
65static const Format gFormats[] = {
66 { SkImageDecoder::kBMP_Format, ".bmp" },
67 { SkImageDecoder::kGIF_Format, ".gif" },
68 { SkImageDecoder::kICO_Format, ".ico" },
69 { SkImageDecoder::kJPEG_Format, ".jpg" },
70 { SkImageDecoder::kPNG_Format, ".png" },
71 { SkImageDecoder::kWBMP_Format, ".wbmp" },
72 { SkImageDecoder::kWEBP_Format, ".webp" },
73 { SkImageDecoder::kUnknown_Format, "" },
74};
75
76/**
77 * Get an appropriate suffix for an image format.
78 */
79static const char* get_suffix_from_format(SkImageDecoder::Format format) {
80 for (size_t i = 0; i < SK_ARRAY_COUNT(gFormats); i++) {
81 if (gFormats[i].fFormat == format) {
82 return gFormats[i].fSuffix;
83 }
84 }
85 return "";
86}
87
88/**
89 * Base name for an image file created from the encoded data in an skp.
90 */
91static SkString gInputFileName;
92
93/**
94 * Number to be appended to the image file name so that it is unique.
95 */
96static uint32_t gImageNo;
97
98/**
99 * Set up the name for writing encoded data to a file.
100 * Sets gInputFileName to name, minus any extension ".*"
101 * Sets gImageNo to 0, so images from file "X.skp" will
102 * look like "X_<gImageNo>.<suffix>", beginning with 0
103 * for each new skp.
104 */
105static void reset_image_file_base_name(const SkString& name) {
106 gImageNo = 0;
107 // Remove ".skp"
108 const char* cName = name.c_str();
109 const char* dot = strrchr(cName, '.');
110 if (dot != NULL) {
111 gInputFileName.set(cName, dot - cName);
112 } else {
113 gInputFileName.set(name);
114 }
115}
116
117/**
118 * Write the raw encoded bitmap data to a file.
119 */
120static bool write_image_to_file(const void* buffer, size_t size, SkBitmap* bitmap) {
121 SkASSERT(!FLAGS_writePath.isEmpty());
122 SkMemoryStream memStream(buffer, size);
123 SkString outPath;
124 SkImageDecoder::Format format = SkImageDecoder::GetStreamFormat(&memStream);
125 SkString name = SkStringPrintf("%s_%d%s", gInputFileName.c_str(), gImageNo++,
126 get_suffix_from_format(format));
127 SkString dir(FLAGS_writePath[0]);
128 sk_tools::make_filepath(&outPath, dir, name);
129 SkFILEWStream fileStream(outPath.c_str());
130 if (!(fileStream.isValid() && fileStream.write(buffer, size))) {
131 SkDebugf("Failed to write encoded data to \"%s\"\n", outPath.c_str());
132 }
133 // Put in a dummy bitmap.
134 return SkImageDecoder::DecodeStream(&memStream, bitmap, SkBitmap::kNo_Config,
135 SkImageDecoder::kDecodeBounds_Mode);
136}
137
138////////////////////////////////////////////////////////////////////////////////////////////////////
139
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000140/**
141 * Called only by render_picture().
142 */
143static bool render_picture_internal(const SkString& inputPath, const SkString* outputDir,
144 sk_tools::PictureRenderer& renderer,
145 SkBitmap** out) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000146 SkString inputFilename;
147 sk_tools::get_basename(&inputFilename, inputPath);
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) {
177 const int kRecordFlags = 0;
178 SkPicture other;
179 picture->draw(other.beginRecording(picture->width(), picture->height(), kRecordFlags));
180 other.endRecording();
181 }
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
edisonn@google.com84f548c2012-12-18 22:24:03 +0000192 renderer.init(picture);
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()) {
196 renderer.getCanvas()->EXPERIMENTAL_optimize(picture);
197 }
198 }
199
scroggo@google.comb4773b42012-10-01 20:06:09 +0000200 renderer.setup();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000201
borenet@google.com070d3542012-10-26 13:26:55 +0000202 SkString* outputPath = NULL;
scroggo@google.com1125d392013-05-03 20:43:37 +0000203 if (NULL != outputDir && outputDir->size() > 0 && !FLAGS_writeEncodedImages) {
borenet@google.com070d3542012-10-26 13:26:55 +0000204 outputPath = SkNEW(SkString);
205 make_output_filepath(outputPath, *outputDir, inputFilename);
206 }
edisonn@google.com84f548c2012-12-18 22:24:03 +0000207
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000208 bool success = renderer.render(outputPath, out);
borenet@google.com070d3542012-10-26 13:26:55 +0000209 if (outputPath) {
210 if (!success) {
211 SkDebugf("Could not write to file %s\n", outputPath->c_str());
212 }
213 SkDELETE(outputPath);
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000214 }
scroggo@google.com9a412522012-09-07 15:21:18 +0000215
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000216 renderer.end();
edisonn@google.com84f548c2012-12-18 22:24:03 +0000217
218 SkDELETE(picture);
borenet@google.com66bcbd12012-09-17 18:26:06 +0000219 return success;
junov@chromium.org777442d2012-06-12 14:56:36 +0000220}
221
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000222static inline int getByte(uint32_t value, int index) {
223 SkASSERT(0 <= index && index < 4);
224 return (value >> (index * 8)) & 0xFF;
225}
226
227static int MaxByteDiff(uint32_t v1, uint32_t v2) {
228 return SkMax32(SkMax32(abs(getByte(v1, 0) - getByte(v2, 0)), abs(getByte(v1, 1) - getByte(v2, 1))),
229 SkMax32(abs(getByte(v1, 2) - getByte(v2, 2)), abs(getByte(v1, 3) - getByte(v2, 3))));
230}
231
junov@chromium.orge286e842013-03-13 17:27:16 +0000232class AutoRestoreBbhType {
233public:
234 AutoRestoreBbhType() {
235 fRenderer = NULL;
junov@chromium.orgd34fda12013-03-13 19:03:26 +0000236 fSavedBbhType = sk_tools::PictureRenderer::kNone_BBoxHierarchyType;
junov@chromium.orge286e842013-03-13 17:27:16 +0000237 }
238
239 void set(sk_tools::PictureRenderer* renderer,
240 sk_tools::PictureRenderer::BBoxHierarchyType bbhType) {
241 fRenderer = renderer;
242 fSavedBbhType = renderer->getBBoxHierarchyType();
243 renderer->setBBoxHierarchyType(bbhType);
244 }
245
246 ~AutoRestoreBbhType() {
247 if (NULL != fRenderer) {
248 fRenderer->setBBoxHierarchyType(fSavedBbhType);
249 }
250 }
251
252private:
253 sk_tools::PictureRenderer* fRenderer;
254 sk_tools::PictureRenderer::BBoxHierarchyType fSavedBbhType;
255};
junov@chromium.orge286e842013-03-13 17:27:16 +0000256
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000257/**
258 * Render the SKP file(s) within inputPath, writing their bitmap images into outputDir.
259 *
260 * @param inputPath path to an individual SKP file, or a directory of SKP files
261 * @param outputDir if not NULL, write the image(s) generated into this directory
262 * @param renderer PictureRenderer to use to render the SKPs
263 * @param jsonSummaryPtr if not NULL, add the image(s) generated to this summary
264 */
edisonn@google.com84f548c2012-12-18 22:24:03 +0000265static bool render_picture(const SkString& inputPath, const SkString* outputDir,
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000266 sk_tools::PictureRenderer& renderer,
267 sk_tools::ImageResultsSummary *jsonSummaryPtr) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000268 int diffs[256] = {0};
edisonn@google.com84f548c2012-12-18 22:24:03 +0000269 SkBitmap* bitmap = NULL;
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000270 renderer.setJsonSummaryPtr(jsonSummaryPtr);
271 bool success = render_picture_internal(inputPath,
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000272 FLAGS_writeWholeImage ? NULL : outputDir,
edisonn@google.com84f548c2012-12-18 22:24:03 +0000273 renderer,
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000274 FLAGS_validate || FLAGS_writeWholeImage ? &bitmap : NULL);
edisonn@google.com84f548c2012-12-18 22:24:03 +0000275
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000276 if (!success || ((FLAGS_validate || FLAGS_writeWholeImage) && bitmap == NULL)) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000277 SkDebugf("Failed to draw the picture.\n");
278 SkDELETE(bitmap);
279 return false;
280 }
281
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000282 if (FLAGS_validate) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000283 SkBitmap* referenceBitmap = NULL;
junov@chromium.orge286e842013-03-13 17:27:16 +0000284 sk_tools::PictureRenderer* referenceRenderer;
285 // If the renderer uses a BBoxHierarchy, then the reference renderer
skia.committer@gmail.com03682be2013-03-14 07:02:51 +0000286 // will be the same renderer, without the bbh.
junov@chromium.orge286e842013-03-13 17:27:16 +0000287 AutoRestoreBbhType arbbh;
288 if (sk_tools::PictureRenderer::kNone_BBoxHierarchyType !=
289 renderer.getBBoxHierarchyType()) {
290 referenceRenderer = &renderer;
291 referenceRenderer->ref(); // to match auto unref below
292 arbbh.set(referenceRenderer, sk_tools::PictureRenderer::kNone_BBoxHierarchyType);
293 } else {
294 referenceRenderer = SkNEW(sk_tools::SimplePictureRenderer);
295 }
296 SkAutoTUnref<sk_tools::PictureRenderer> aurReferenceRenderer(referenceRenderer);
297
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000298 success = render_picture_internal(inputPath, NULL, *referenceRenderer,
299 &referenceBitmap);
edisonn@google.com84f548c2012-12-18 22:24:03 +0000300
junov@chromium.orgc19c1912013-03-12 19:56:49 +0000301 if (!success || NULL == referenceBitmap || NULL == referenceBitmap->getPixels()) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000302 SkDebugf("Failed to draw the reference picture.\n");
303 SkDELETE(bitmap);
304 SkDELETE(referenceBitmap);
305 return false;
306 }
307
308 if (success && (bitmap->width() != referenceBitmap->width())) {
309 SkDebugf("Expected image width: %i, actual image width %i.\n",
310 referenceBitmap->width(), bitmap->width());
311 SkDELETE(bitmap);
312 SkDELETE(referenceBitmap);
313 return false;
314 }
315 if (success && (bitmap->height() != referenceBitmap->height())) {
316 SkDebugf("Expected image height: %i, actual image height %i",
317 referenceBitmap->height(), bitmap->height());
318 SkDELETE(bitmap);
319 SkDELETE(referenceBitmap);
320 return false;
321 }
skia.committer@gmail.coma7d8e3e2012-12-19 02:01:38 +0000322
edisonn@google.com84f548c2012-12-18 22:24:03 +0000323 for (int y = 0; success && y < bitmap->height(); y++) {
324 for (int x = 0; success && x < bitmap->width(); x++) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000325 int diff = MaxByteDiff(*referenceBitmap->getAddr32(x, y),
326 *bitmap->getAddr32(x, y));
327 SkASSERT(diff >= 0 && diff <= 255);
328 diffs[diff]++;
skia.committer@gmail.com4d28d982013-01-17 07:06:06 +0000329
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000330 if (diff > FLAGS_maxComponentDiff) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000331 SkDebugf("Expected pixel at (%i %i) exceedds maximum "
332 "component diff of %i: 0x%x, actual 0x%x\n",
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000333 x, y, FLAGS_maxComponentDiff,
edisonn@google.com01754bf2013-01-11 16:08:07 +0000334 *referenceBitmap->getAddr32(x, y),
edisonn@google.com84f548c2012-12-18 22:24:03 +0000335 *bitmap->getAddr32(x, y));
336 SkDELETE(bitmap);
337 SkDELETE(referenceBitmap);
338 return false;
339 }
340 }
341 }
342 SkDELETE(referenceBitmap);
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000343
344 for (int i = 1; i <= 255; ++i) {
345 if(diffs[i] > 0) {
346 SkDebugf("Number of pixels with max diff of %i is %i\n", i, diffs[i]);
347 }
348 }
edisonn@google.com84f548c2012-12-18 22:24:03 +0000349 }
350
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000351 if (FLAGS_writeWholeImage) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000352 sk_tools::force_all_opaque(*bitmap);
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000353
354 if (NULL != jsonSummaryPtr) {
355 // EPOGER: This is a hacky way of constructing the filename associated with the
356 // image checksum; we basically are repeating the logic of make_output_filepath()
357 // and code below here, within here.
358 // It would be better for the filename (without outputDir) to be passed in here,
359 // and used both for the checksum file and writing into outputDir.
360 //
361 // EPOGER: what about including the config type within hashFilename? That way,
362 // we could combine results of different config types without conflicting filenames.
363 SkString hashFilename;
364 sk_tools::get_basename(&hashFilename, inputPath);
365 hashFilename.remove(hashFilename.size() - 4, 4); // Remove ".skp"
366 hashFilename.append(".png");
367 jsonSummaryPtr->add(hashFilename.c_str(), *bitmap);
368 }
369
370 if (NULL != outputDir) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000371 SkString inputFilename;
372 sk_tools::get_basename(&inputFilename, inputPath);
373 SkString outputPath;
374 make_output_filepath(&outputPath, *outputDir, inputFilename);
375 outputPath.append(".png");
376 if (!SkImageEncoder::EncodeFile(outputPath.c_str(), *bitmap,
377 SkImageEncoder::kPNG_Type, 100)) {
378 SkDebugf("Failed to draw the picture.\n");
379 success = false;
380 }
381 }
382 }
383 SkDELETE(bitmap);
384
385 return success;
386}
387
388
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000389static int process_input(const char* input, const SkString* outputDir,
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000390 sk_tools::PictureRenderer& renderer,
391 sk_tools::ImageResultsSummary *jsonSummaryPtr) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000392 SkOSFile::Iter iter(input, "skp");
junov@chromium.org777442d2012-06-12 14:56:36 +0000393 SkString inputFilename;
borenet@google.com66bcbd12012-09-17 18:26:06 +0000394 int failures = 0;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000395 SkDebugf("process_input, %s\n", input);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000396 if (iter.next(&inputFilename)) {
397 do {
398 SkString inputPath;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000399 SkString inputAsSkString(input);
400 sk_tools::make_filepath(&inputPath, inputAsSkString, inputFilename);
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000401 if (!render_picture(inputPath, outputDir, renderer, jsonSummaryPtr)) {
borenet@google.com57837bf2012-09-19 17:28:29 +0000402 ++failures;
403 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000404 } while(iter.next(&inputFilename));
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000405 } else if (SkStrEndsWith(input, ".skp")) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000406 SkString inputPath(input);
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000407 if (!render_picture(inputPath, outputDir, renderer, jsonSummaryPtr)) {
borenet@google.com57837bf2012-09-19 17:28:29 +0000408 ++failures;
409 }
410 } else {
411 SkString warning;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000412 warning.printf("Warning: skipping %s\n", input);
borenet@google.com57837bf2012-09-19 17:28:29 +0000413 SkDebugf(warning.c_str());
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000414 }
borenet@google.com66bcbd12012-09-17 18:26:06 +0000415 return failures;
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000416}
417
caryclark@google.com5987f582012-10-02 18:33:14 +0000418int tool_main(int argc, char** argv);
419int tool_main(int argc, char** argv) {
scroggo@google.comd9ba9a02013-03-21 19:43:15 +0000420 SkCommandLineFlags::SetUsage("Render .skp files.");
421 SkCommandLineFlags::Parse(argc, argv);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000422
scroggo@google.com604e0c22013-04-09 21:25:46 +0000423 if (FLAGS_readPath.isEmpty()) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000424 SkDebugf(".skp files or directories are required.\n");
425 exit(-1);
426 }
427
428 if (FLAGS_maxComponentDiff < 0 || FLAGS_maxComponentDiff > 256) {
429 SkDebugf("--maxComponentDiff must be between 0 and 256\n");
430 exit(-1);
431 }
432
433 if (FLAGS_maxComponentDiff != 256 && !FLAGS_validate) {
434 SkDebugf("--maxComponentDiff requires --validate\n");
435 exit(-1);
436 }
437
438 if (FLAGS_clone < 0) {
439 SkDebugf("--clone must be >= 0. Was %i\n", FLAGS_clone);
440 exit(-1);
441 }
442
scroggo@google.com1125d392013-05-03 20:43:37 +0000443 if (FLAGS_writeEncodedImages) {
444 if (FLAGS_writePath.isEmpty()) {
445 SkDebugf("--writeEncodedImages requires --writePath\n");
446 exit(-1);
447 }
448 if (FLAGS_deferImageDecoding) {
449 SkDebugf("--writeEncodedImages is not compatible with --deferImageDecoding\n");
450 exit(-1);
451 }
452 }
453
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000454 SkString errorString;
455 SkAutoTUnref<sk_tools::PictureRenderer> renderer(parseRenderer(errorString,
456 kRender_PictureTool));
457 if (errorString.size() > 0) {
458 SkDebugf("%s\n", errorString.c_str());
459 }
460
461 if (renderer.get() == NULL) {
462 exit(-1);
463 }
464
borenet@google.com66bcbd12012-09-17 18:26:06 +0000465 SkAutoGraphics ag;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000466
467 SkString outputDir;
scroggo@google.com604e0c22013-04-09 21:25:46 +0000468 if (FLAGS_writePath.count() == 1) {
469 outputDir.set(FLAGS_writePath[0]);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000470 }
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000471 sk_tools::ImageResultsSummary jsonSummary;
472 sk_tools::ImageResultsSummary* jsonSummaryPtr = NULL;
473 if (FLAGS_writeJsonSummaryPath.count() == 1) {
474 jsonSummaryPtr = &jsonSummary;
475 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000476
borenet@google.com66bcbd12012-09-17 18:26:06 +0000477 int failures = 0;
scroggo@google.com604e0c22013-04-09 21:25:46 +0000478 for (int i = 0; i < FLAGS_readPath.count(); i ++) {
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000479 failures += process_input(FLAGS_readPath[i], &outputDir, *renderer.get(), jsonSummaryPtr);
junov@chromium.org777442d2012-06-12 14:56:36 +0000480 }
borenet@google.com66bcbd12012-09-17 18:26:06 +0000481 if (failures != 0) {
482 SkDebugf("Failed to render %i pictures.\n", failures);
483 return 1;
484 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000485#if SK_SUPPORT_GPU
486#if GR_CACHE_STATS
487 if (renderer->isUsingGpuDevice()) {
488 GrContext* ctx = renderer->getGrContext();
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000489 ctx->printCacheStats();
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000490#ifdef SK_DEVELOPER
491 ctx->dumpFontCache();
492#endif
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000493 }
494#endif
495#endif
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000496 if (FLAGS_writeJsonSummaryPath.count() == 1) {
497 jsonSummary.writeToFile(FLAGS_writeJsonSummaryPath[0]);
498 }
caryclark@google.com868e1f62012-10-02 20:00:03 +0000499 return 0;
junov@chromium.org777442d2012-06-12 14:56:36 +0000500}
caryclark@google.com5987f582012-10-02 18:33:14 +0000501
502#if !defined SK_BUILD_FOR_IOS
503int main(int argc, char * const argv[]) {
504 return tool_main(argc, (char**) argv);
505}
506#endif