blob: f11eb0b80ee05fed4c4c17f9037e0a9b438b5f1a [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"
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000022#include "PictureRenderer.h"
scroggo@google.com161e1ba2013-03-04 16:41:06 +000023#include "PictureRenderingFlags.h"
twiz@google.coma31b8bb2012-06-22 18:24:56 +000024#include "picture_utils.h"
junov@chromium.org777442d2012-06-12 14:56:36 +000025
scroggo@google.com161e1ba2013-03-04 16:41:06 +000026// Flags used by this file, alphabetically:
27DEFINE_int32(clone, 0, "Clone the picture n times before rendering.");
28DECLARE_bool(deferImageDecoding);
29DEFINE_int32(maxComponentDiff, 256, "Maximum diff on a component, 0 - 256. Components that differ "
30 "by more than this amount are considered errors, though all diffs are reported. "
31 "Requires --validate.");
scroggo@google.com604e0c22013-04-09 21:25:46 +000032DECLARE_string(readPath);
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000033DEFINE_bool(writeChecksumBasedFilenames, false,
34 "When writing out images, use checksum-based filenames.");
scroggo@google.com1125d392013-05-03 20:43:37 +000035DEFINE_bool(writeEncodedImages, false, "Any time the skp contains an encoded image, write it to a "
36 "file rather than decoding it. Requires writePath to be set. Skips drawing the full "
37 "skp to a file. Not compatible with deferImageDecoding.");
commit-bot@chromium.org4610a462014-04-29 19:39:22 +000038DEFINE_string(writeJsonSummaryPath, "", "File to write a JSON summary of image results to.");
39DEFINE_string2(writePath, w, "", "Directory to write the rendered images into.");
scroggo@google.com161e1ba2013-03-04 16:41:06 +000040DEFINE_bool(writeWholeImage, false, "In tile mode, write the entire rendered image to a "
41 "file, instead of an image for each tile.");
42DEFINE_bool(validate, false, "Verify that the rendered image contains the same pixels as "
junov@chromium.orge286e842013-03-13 17:27:16 +000043 "the picture rendered in simple mode. When used in conjunction with --bbh, results "
44 "are validated against the picture rendered in the same mode, but without the bbh.");
junov@chromium.org777442d2012-06-12 14:56:36 +000045
commit-bot@chromium.orgff36a1d2013-07-24 20:37:30 +000046DEFINE_bool(bench_record, false, "If true, drop into an infinite loop of recording the picture.");
47
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000048DEFINE_bool(preprocess, false, "If true, perform device specific preprocessing before rendering.");
49
scroggo@google.com1125d392013-05-03 20:43:37 +000050////////////////////////////////////////////////////////////////////////////////////////////////////
51
52/**
53 * Table for translating from format of data to a suffix.
54 */
55struct Format {
56 SkImageDecoder::Format fFormat;
57 const char* fSuffix;
58};
59static const Format gFormats[] = {
60 { SkImageDecoder::kBMP_Format, ".bmp" },
61 { SkImageDecoder::kGIF_Format, ".gif" },
62 { SkImageDecoder::kICO_Format, ".ico" },
63 { SkImageDecoder::kJPEG_Format, ".jpg" },
64 { SkImageDecoder::kPNG_Format, ".png" },
65 { SkImageDecoder::kWBMP_Format, ".wbmp" },
66 { SkImageDecoder::kWEBP_Format, ".webp" },
67 { SkImageDecoder::kUnknown_Format, "" },
68};
69
70/**
71 * Get an appropriate suffix for an image format.
72 */
73static const char* get_suffix_from_format(SkImageDecoder::Format format) {
74 for (size_t i = 0; i < SK_ARRAY_COUNT(gFormats); i++) {
75 if (gFormats[i].fFormat == format) {
76 return gFormats[i].fSuffix;
77 }
78 }
79 return "";
80}
81
82/**
83 * Base name for an image file created from the encoded data in an skp.
84 */
85static SkString gInputFileName;
86
87/**
88 * Number to be appended to the image file name so that it is unique.
89 */
90static uint32_t gImageNo;
91
92/**
93 * Set up the name for writing encoded data to a file.
94 * Sets gInputFileName to name, minus any extension ".*"
95 * Sets gImageNo to 0, so images from file "X.skp" will
96 * look like "X_<gImageNo>.<suffix>", beginning with 0
97 * for each new skp.
98 */
99static void reset_image_file_base_name(const SkString& name) {
100 gImageNo = 0;
101 // Remove ".skp"
102 const char* cName = name.c_str();
103 const char* dot = strrchr(cName, '.');
104 if (dot != NULL) {
105 gInputFileName.set(cName, dot - cName);
106 } else {
107 gInputFileName.set(name);
108 }
109}
110
111/**
112 * Write the raw encoded bitmap data to a file.
113 */
114static bool write_image_to_file(const void* buffer, size_t size, SkBitmap* bitmap) {
115 SkASSERT(!FLAGS_writePath.isEmpty());
116 SkMemoryStream memStream(buffer, size);
117 SkString outPath;
118 SkImageDecoder::Format format = SkImageDecoder::GetStreamFormat(&memStream);
119 SkString name = SkStringPrintf("%s_%d%s", gInputFileName.c_str(), gImageNo++,
120 get_suffix_from_format(format));
121 SkString dir(FLAGS_writePath[0]);
122 sk_tools::make_filepath(&outPath, dir, name);
123 SkFILEWStream fileStream(outPath.c_str());
124 if (!(fileStream.isValid() && fileStream.write(buffer, size))) {
125 SkDebugf("Failed to write encoded data to \"%s\"\n", outPath.c_str());
126 }
127 // Put in a dummy bitmap.
128 return SkImageDecoder::DecodeStream(&memStream, bitmap, SkBitmap::kNo_Config,
129 SkImageDecoder::kDecodeBounds_Mode);
130}
131
132////////////////////////////////////////////////////////////////////////////////////////////////////
133
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000134/**
135 * Called only by render_picture().
136 */
137static bool render_picture_internal(const SkString& inputPath, const SkString* outputDir,
138 sk_tools::PictureRenderer& renderer,
139 SkBitmap** out) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000140 SkString inputFilename;
141 sk_tools::get_basename(&inputFilename, inputPath);
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000142 SkString outputDirString;
143 if (NULL != outputDir && outputDir->size() > 0 && !FLAGS_writeEncodedImages) {
144 outputDirString.set(*outputDir);
145 }
twiz@google.coma31b8bb2012-06-22 18:24:56 +0000146
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000147 SkFILEStream inputStream;
twiz@google.coma31b8bb2012-06-22 18:24:56 +0000148 inputStream.setPath(inputPath.c_str());
149 if (!inputStream.isValid()) {
150 SkDebugf("Could not open file %s\n", inputPath.c_str());
borenet@google.com66bcbd12012-09-17 18:26:06 +0000151 return false;
twiz@google.coma31b8bb2012-06-22 18:24:56 +0000152 }
153
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000154 SkPicture::InstallPixelRefProc proc;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000155 if (FLAGS_deferImageDecoding) {
commit-bot@chromium.org56799e22013-07-16 18:21:46 +0000156 proc = &sk_tools::LazyDecodeBitmap;
scroggo@google.com1125d392013-05-03 20:43:37 +0000157 } else if (FLAGS_writeEncodedImages) {
158 SkASSERT(!FLAGS_writePath.isEmpty());
159 reset_image_file_base_name(inputFilename);
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000160 proc = &write_image_to_file;
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000161 } else {
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000162 proc = &SkImageDecoder::DecodeMemory;
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000163 }
scroggo@google.com1125d392013-05-03 20:43:37 +0000164
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000165 SkDebugf("deserializing... %s\n", inputPath.c_str());
166
167 SkPicture* picture = SkPicture::CreateFromStream(&inputStream, proc);
168
169 if (NULL == picture) {
borenet@google.com66bcbd12012-09-17 18:26:06 +0000170 SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str());
171 return false;
172 }
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000173
commit-bot@chromium.orgff36a1d2013-07-24 20:37:30 +0000174 while (FLAGS_bench_record) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000175 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000176 picture->draw(recorder.beginRecording(picture->width(), picture->height(), NULL, 0));
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000177 SkAutoTUnref<SkPicture> other(recorder.endRecording());
commit-bot@chromium.orgff36a1d2013-07-24 20:37:30 +0000178 }
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
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000189 renderer.init(picture, &outputDirString, &inputFilename, FLAGS_writeChecksumBasedFilenames);
skia.committer@gmail.comeb849e52014-03-17 03:02:17 +0000190
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000191 if (FLAGS_preprocess) {
192 if (NULL != renderer.getCanvas()) {
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000193 renderer.getCanvas()->EXPERIMENTAL_optimize(renderer.getPicture());
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000194 }
195 }
196
scroggo@google.comb4773b42012-10-01 20:06:09 +0000197 renderer.setup();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000198
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000199 bool success = renderer.render(out);
200 if (!success) {
201 SkDebugf("Failed to render %s\n", inputFilename.c_str());
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000202 }
scroggo@google.com9a412522012-09-07 15:21:18 +0000203
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000204 renderer.end();
edisonn@google.com84f548c2012-12-18 22:24:03 +0000205
206 SkDELETE(picture);
borenet@google.com66bcbd12012-09-17 18:26:06 +0000207 return success;
junov@chromium.org777442d2012-06-12 14:56:36 +0000208}
209
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000210static inline int getByte(uint32_t value, int index) {
211 SkASSERT(0 <= index && index < 4);
212 return (value >> (index * 8)) & 0xFF;
213}
214
215static int MaxByteDiff(uint32_t v1, uint32_t v2) {
216 return SkMax32(SkMax32(abs(getByte(v1, 0) - getByte(v2, 0)), abs(getByte(v1, 1) - getByte(v2, 1))),
217 SkMax32(abs(getByte(v1, 2) - getByte(v2, 2)), abs(getByte(v1, 3) - getByte(v2, 3))));
218}
219
junov@chromium.orge286e842013-03-13 17:27:16 +0000220class AutoRestoreBbhType {
221public:
222 AutoRestoreBbhType() {
223 fRenderer = NULL;
junov@chromium.orgd34fda12013-03-13 19:03:26 +0000224 fSavedBbhType = sk_tools::PictureRenderer::kNone_BBoxHierarchyType;
junov@chromium.orge286e842013-03-13 17:27:16 +0000225 }
226
227 void set(sk_tools::PictureRenderer* renderer,
228 sk_tools::PictureRenderer::BBoxHierarchyType bbhType) {
229 fRenderer = renderer;
230 fSavedBbhType = renderer->getBBoxHierarchyType();
231 renderer->setBBoxHierarchyType(bbhType);
232 }
233
234 ~AutoRestoreBbhType() {
235 if (NULL != fRenderer) {
236 fRenderer->setBBoxHierarchyType(fSavedBbhType);
237 }
238 }
239
240private:
241 sk_tools::PictureRenderer* fRenderer;
242 sk_tools::PictureRenderer::BBoxHierarchyType fSavedBbhType;
243};
junov@chromium.orge286e842013-03-13 17:27:16 +0000244
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000245/**
246 * Render the SKP file(s) within inputPath, writing their bitmap images into outputDir.
247 *
248 * @param inputPath path to an individual SKP file, or a directory of SKP files
249 * @param outputDir if not NULL, write the image(s) generated into this directory
250 * @param renderer PictureRenderer to use to render the SKPs
251 * @param jsonSummaryPtr if not NULL, add the image(s) generated to this summary
252 */
edisonn@google.com84f548c2012-12-18 22:24:03 +0000253static bool render_picture(const SkString& inputPath, const SkString* outputDir,
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000254 sk_tools::PictureRenderer& renderer,
255 sk_tools::ImageResultsSummary *jsonSummaryPtr) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000256 int diffs[256] = {0};
edisonn@google.com84f548c2012-12-18 22:24:03 +0000257 SkBitmap* bitmap = NULL;
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000258 renderer.setJsonSummaryPtr(jsonSummaryPtr);
259 bool success = render_picture_internal(inputPath,
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000260 FLAGS_writeWholeImage ? NULL : outputDir,
edisonn@google.com84f548c2012-12-18 22:24:03 +0000261 renderer,
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000262 FLAGS_validate || FLAGS_writeWholeImage ? &bitmap : NULL);
edisonn@google.com84f548c2012-12-18 22:24:03 +0000263
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000264 if (!success || ((FLAGS_validate || FLAGS_writeWholeImage) && bitmap == NULL)) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000265 SkDebugf("Failed to draw the picture.\n");
266 SkDELETE(bitmap);
267 return false;
268 }
269
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000270 if (FLAGS_validate) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000271 SkBitmap* referenceBitmap = NULL;
junov@chromium.orge286e842013-03-13 17:27:16 +0000272 sk_tools::PictureRenderer* referenceRenderer;
273 // If the renderer uses a BBoxHierarchy, then the reference renderer
skia.committer@gmail.com03682be2013-03-14 07:02:51 +0000274 // will be the same renderer, without the bbh.
junov@chromium.orge286e842013-03-13 17:27:16 +0000275 AutoRestoreBbhType arbbh;
276 if (sk_tools::PictureRenderer::kNone_BBoxHierarchyType !=
277 renderer.getBBoxHierarchyType()) {
278 referenceRenderer = &renderer;
279 referenceRenderer->ref(); // to match auto unref below
280 arbbh.set(referenceRenderer, sk_tools::PictureRenderer::kNone_BBoxHierarchyType);
281 } else {
282 referenceRenderer = SkNEW(sk_tools::SimplePictureRenderer);
283 }
284 SkAutoTUnref<sk_tools::PictureRenderer> aurReferenceRenderer(referenceRenderer);
285
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000286 success = render_picture_internal(inputPath, NULL, *referenceRenderer,
287 &referenceBitmap);
edisonn@google.com84f548c2012-12-18 22:24:03 +0000288
junov@chromium.orgc19c1912013-03-12 19:56:49 +0000289 if (!success || NULL == referenceBitmap || NULL == referenceBitmap->getPixels()) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000290 SkDebugf("Failed to draw the reference picture.\n");
291 SkDELETE(bitmap);
292 SkDELETE(referenceBitmap);
293 return false;
294 }
295
296 if (success && (bitmap->width() != referenceBitmap->width())) {
297 SkDebugf("Expected image width: %i, actual image width %i.\n",
298 referenceBitmap->width(), bitmap->width());
299 SkDELETE(bitmap);
300 SkDELETE(referenceBitmap);
301 return false;
302 }
303 if (success && (bitmap->height() != referenceBitmap->height())) {
304 SkDebugf("Expected image height: %i, actual image height %i",
305 referenceBitmap->height(), bitmap->height());
306 SkDELETE(bitmap);
307 SkDELETE(referenceBitmap);
308 return false;
309 }
skia.committer@gmail.coma7d8e3e2012-12-19 02:01:38 +0000310
edisonn@google.com84f548c2012-12-18 22:24:03 +0000311 for (int y = 0; success && y < bitmap->height(); y++) {
312 for (int x = 0; success && x < bitmap->width(); x++) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000313 int diff = MaxByteDiff(*referenceBitmap->getAddr32(x, y),
314 *bitmap->getAddr32(x, y));
315 SkASSERT(diff >= 0 && diff <= 255);
316 diffs[diff]++;
skia.committer@gmail.com4d28d982013-01-17 07:06:06 +0000317
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000318 if (diff > FLAGS_maxComponentDiff) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000319 SkDebugf("Expected pixel at (%i %i) exceedds maximum "
320 "component diff of %i: 0x%x, actual 0x%x\n",
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000321 x, y, FLAGS_maxComponentDiff,
edisonn@google.com01754bf2013-01-11 16:08:07 +0000322 *referenceBitmap->getAddr32(x, y),
edisonn@google.com84f548c2012-12-18 22:24:03 +0000323 *bitmap->getAddr32(x, y));
324 SkDELETE(bitmap);
325 SkDELETE(referenceBitmap);
326 return false;
327 }
328 }
329 }
330 SkDELETE(referenceBitmap);
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000331
332 for (int i = 1; i <= 255; ++i) {
333 if(diffs[i] > 0) {
334 SkDebugf("Number of pixels with max diff of %i is %i\n", i, diffs[i]);
335 }
336 }
edisonn@google.com84f548c2012-12-18 22:24:03 +0000337 }
338
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000339 if (FLAGS_writeWholeImage) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000340 sk_tools::force_all_opaque(*bitmap);
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000341
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000342 SkString inputFilename, outputPath;
343 sk_tools::get_basename(&inputFilename, inputPath);
344 sk_tools::make_filepath(&outputPath, *outputDir, inputFilename);
345 sk_tools::replace_char(&outputPath, '.', '_');
346 outputPath.append(".png");
347
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000348 if (NULL != jsonSummaryPtr) {
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000349 SkString outputFileBasename;
350 sk_tools::get_basename(&outputFileBasename, outputPath);
351 jsonSummaryPtr->add(inputFilename.c_str(), outputFileBasename.c_str(), *bitmap);
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000352 }
353
354 if (NULL != outputDir) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000355 if (!SkImageEncoder::EncodeFile(outputPath.c_str(), *bitmap,
356 SkImageEncoder::kPNG_Type, 100)) {
357 SkDebugf("Failed to draw the picture.\n");
358 success = false;
359 }
360 }
361 }
362 SkDELETE(bitmap);
363
364 return success;
365}
366
367
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000368static int process_input(const char* input, const SkString* outputDir,
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000369 sk_tools::PictureRenderer& renderer,
370 sk_tools::ImageResultsSummary *jsonSummaryPtr) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000371 SkOSFile::Iter iter(input, "skp");
junov@chromium.org777442d2012-06-12 14:56:36 +0000372 SkString inputFilename;
borenet@google.com66bcbd12012-09-17 18:26:06 +0000373 int failures = 0;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000374 SkDebugf("process_input, %s\n", input);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000375 if (iter.next(&inputFilename)) {
376 do {
377 SkString inputPath;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000378 SkString inputAsSkString(input);
379 sk_tools::make_filepath(&inputPath, inputAsSkString, inputFilename);
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000380 if (!render_picture(inputPath, outputDir, renderer, jsonSummaryPtr)) {
borenet@google.com57837bf2012-09-19 17:28:29 +0000381 ++failures;
382 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000383 } while(iter.next(&inputFilename));
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000384 } else if (SkStrEndsWith(input, ".skp")) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000385 SkString inputPath(input);
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000386 if (!render_picture(inputPath, outputDir, renderer, jsonSummaryPtr)) {
borenet@google.com57837bf2012-09-19 17:28:29 +0000387 ++failures;
388 }
389 } else {
390 SkString warning;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000391 warning.printf("Warning: skipping %s\n", input);
borenet@google.com57837bf2012-09-19 17:28:29 +0000392 SkDebugf(warning.c_str());
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000393 }
borenet@google.com66bcbd12012-09-17 18:26:06 +0000394 return failures;
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000395}
396
caryclark@google.com5987f582012-10-02 18:33:14 +0000397int tool_main(int argc, char** argv);
398int tool_main(int argc, char** argv) {
scroggo@google.comd9ba9a02013-03-21 19:43:15 +0000399 SkCommandLineFlags::SetUsage("Render .skp files.");
400 SkCommandLineFlags::Parse(argc, argv);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000401
scroggo@google.com604e0c22013-04-09 21:25:46 +0000402 if (FLAGS_readPath.isEmpty()) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000403 SkDebugf(".skp files or directories are required.\n");
404 exit(-1);
405 }
406
407 if (FLAGS_maxComponentDiff < 0 || FLAGS_maxComponentDiff > 256) {
408 SkDebugf("--maxComponentDiff must be between 0 and 256\n");
409 exit(-1);
410 }
411
412 if (FLAGS_maxComponentDiff != 256 && !FLAGS_validate) {
413 SkDebugf("--maxComponentDiff requires --validate\n");
414 exit(-1);
415 }
416
417 if (FLAGS_clone < 0) {
418 SkDebugf("--clone must be >= 0. Was %i\n", FLAGS_clone);
419 exit(-1);
420 }
421
scroggo@google.com1125d392013-05-03 20:43:37 +0000422 if (FLAGS_writeEncodedImages) {
423 if (FLAGS_writePath.isEmpty()) {
424 SkDebugf("--writeEncodedImages requires --writePath\n");
425 exit(-1);
426 }
427 if (FLAGS_deferImageDecoding) {
428 SkDebugf("--writeEncodedImages is not compatible with --deferImageDecoding\n");
429 exit(-1);
430 }
431 }
432
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000433 SkString errorString;
434 SkAutoTUnref<sk_tools::PictureRenderer> renderer(parseRenderer(errorString,
435 kRender_PictureTool));
436 if (errorString.size() > 0) {
437 SkDebugf("%s\n", errorString.c_str());
438 }
439
440 if (renderer.get() == NULL) {
441 exit(-1);
442 }
443
borenet@google.com66bcbd12012-09-17 18:26:06 +0000444 SkAutoGraphics ag;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000445
446 SkString outputDir;
scroggo@google.com604e0c22013-04-09 21:25:46 +0000447 if (FLAGS_writePath.count() == 1) {
448 outputDir.set(FLAGS_writePath[0]);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000449 }
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000450 sk_tools::ImageResultsSummary jsonSummary;
451 sk_tools::ImageResultsSummary* jsonSummaryPtr = NULL;
452 if (FLAGS_writeJsonSummaryPath.count() == 1) {
453 jsonSummaryPtr = &jsonSummary;
454 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000455
borenet@google.com66bcbd12012-09-17 18:26:06 +0000456 int failures = 0;
scroggo@google.com604e0c22013-04-09 21:25:46 +0000457 for (int i = 0; i < FLAGS_readPath.count(); i ++) {
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000458 failures += process_input(FLAGS_readPath[i], &outputDir, *renderer.get(), jsonSummaryPtr);
junov@chromium.org777442d2012-06-12 14:56:36 +0000459 }
borenet@google.com66bcbd12012-09-17 18:26:06 +0000460 if (failures != 0) {
461 SkDebugf("Failed to render %i pictures.\n", failures);
462 return 1;
463 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000464#if SK_SUPPORT_GPU
465#if GR_CACHE_STATS
466 if (renderer->isUsingGpuDevice()) {
467 GrContext* ctx = renderer->getGrContext();
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000468 ctx->printCacheStats();
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000469#ifdef SK_DEVELOPER
470 ctx->dumpFontCache();
471#endif
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000472 }
473#endif
474#endif
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000475 if (FLAGS_writeJsonSummaryPath.count() == 1) {
476 jsonSummary.writeToFile(FLAGS_writeJsonSummaryPath[0]);
477 }
caryclark@google.com868e1f62012-10-02 20:00:03 +0000478 return 0;
junov@chromium.org777442d2012-06-12 14:56:36 +0000479}
caryclark@google.com5987f582012-10-02 18:33:14 +0000480
481#if !defined SK_BUILD_FOR_IOS
482int main(int argc, char * const argv[]) {
483 return tool_main(argc, (char**) argv);
484}
485#endif