blob: 13ae6ead3f22f2788cf152ef1963be7cdc02c3a2 [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.org90c0fbd2014-05-09 03:18:41 +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:
scroggo@google.com161e1ba2013-03-04 16:41:06 +000029DECLARE_bool(deferImageDecoding);
30DEFINE_int32(maxComponentDiff, 256, "Maximum diff on a component, 0 - 256. Components that differ "
31 "by more than this amount are considered errors, though all diffs are reported. "
32 "Requires --validate.");
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000033DEFINE_string(mismatchPath, "", "Write images for tests that failed due to "
34 "pixel mismatches into this directory.");
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000035DEFINE_string(readJsonSummaryPath, "", "JSON file to read image expectations from.");
scroggo@google.com604e0c22013-04-09 21:25:46 +000036DECLARE_string(readPath);
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000037DEFINE_bool(writeChecksumBasedFilenames, false,
38 "When writing out images, use checksum-based filenames.");
scroggo@google.com1125d392013-05-03 20:43:37 +000039DEFINE_bool(writeEncodedImages, false, "Any time the skp contains an encoded image, write it to a "
40 "file rather than decoding it. Requires writePath to be set. Skips drawing the full "
41 "skp to a file. Not compatible with deferImageDecoding.");
commit-bot@chromium.org4610a462014-04-29 19:39:22 +000042DEFINE_string(writeJsonSummaryPath, "", "File to write a JSON summary of image results to.");
43DEFINE_string2(writePath, w, "", "Directory to write the rendered images into.");
scroggo@google.com161e1ba2013-03-04 16:41:06 +000044DEFINE_bool(writeWholeImage, false, "In tile mode, write the entire rendered image to a "
45 "file, instead of an image for each tile.");
46DEFINE_bool(validate, false, "Verify that the rendered image contains the same pixels as "
junov@chromium.orge286e842013-03-13 17:27:16 +000047 "the picture rendered in simple mode. When used in conjunction with --bbh, results "
48 "are validated against the picture rendered in the same mode, but without the bbh.");
junov@chromium.org777442d2012-06-12 14:56:36 +000049
commit-bot@chromium.orgff36a1d2013-07-24 20:37:30 +000050DEFINE_bool(bench_record, false, "If true, drop into an infinite loop of recording the picture.");
51
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000052DEFINE_bool(preprocess, false, "If true, perform device specific preprocessing before rendering.");
53
scroggo@google.com1125d392013-05-03 20:43:37 +000054////////////////////////////////////////////////////////////////////////////////////////////////////
55
56/**
57 * Table for translating from format of data to a suffix.
58 */
59struct Format {
60 SkImageDecoder::Format fFormat;
61 const char* fSuffix;
62};
63static const Format gFormats[] = {
64 { SkImageDecoder::kBMP_Format, ".bmp" },
65 { SkImageDecoder::kGIF_Format, ".gif" },
66 { SkImageDecoder::kICO_Format, ".ico" },
67 { SkImageDecoder::kJPEG_Format, ".jpg" },
68 { SkImageDecoder::kPNG_Format, ".png" },
69 { SkImageDecoder::kWBMP_Format, ".wbmp" },
70 { SkImageDecoder::kWEBP_Format, ".webp" },
71 { SkImageDecoder::kUnknown_Format, "" },
72};
73
74/**
75 * Get an appropriate suffix for an image format.
76 */
77static const char* get_suffix_from_format(SkImageDecoder::Format format) {
78 for (size_t i = 0; i < SK_ARRAY_COUNT(gFormats); i++) {
79 if (gFormats[i].fFormat == format) {
80 return gFormats[i].fSuffix;
81 }
82 }
83 return "";
84}
85
86/**
87 * Base name for an image file created from the encoded data in an skp.
88 */
89static SkString gInputFileName;
90
91/**
92 * Number to be appended to the image file name so that it is unique.
93 */
94static uint32_t gImageNo;
95
96/**
97 * Set up the name for writing encoded data to a file.
98 * Sets gInputFileName to name, minus any extension ".*"
99 * Sets gImageNo to 0, so images from file "X.skp" will
100 * look like "X_<gImageNo>.<suffix>", beginning with 0
101 * for each new skp.
102 */
103static void reset_image_file_base_name(const SkString& name) {
104 gImageNo = 0;
105 // Remove ".skp"
106 const char* cName = name.c_str();
107 const char* dot = strrchr(cName, '.');
108 if (dot != NULL) {
109 gInputFileName.set(cName, dot - cName);
110 } else {
111 gInputFileName.set(name);
112 }
113}
114
115/**
116 * Write the raw encoded bitmap data to a file.
117 */
118static bool write_image_to_file(const void* buffer, size_t size, SkBitmap* bitmap) {
119 SkASSERT(!FLAGS_writePath.isEmpty());
120 SkMemoryStream memStream(buffer, size);
121 SkString outPath;
122 SkImageDecoder::Format format = SkImageDecoder::GetStreamFormat(&memStream);
123 SkString name = SkStringPrintf("%s_%d%s", gInputFileName.c_str(), gImageNo++,
124 get_suffix_from_format(format));
125 SkString dir(FLAGS_writePath[0]);
tfarinada4ed322014-06-12 08:50:56 -0700126 outPath = SkOSPath::SkPathJoin(dir.c_str(), name.c_str());
scroggo@google.com1125d392013-05-03 20:43:37 +0000127 SkFILEWStream fileStream(outPath.c_str());
128 if (!(fileStream.isValid() && fileStream.write(buffer, size))) {
129 SkDebugf("Failed to write encoded data to \"%s\"\n", outPath.c_str());
130 }
131 // Put in a dummy bitmap.
reedbfefc7c2014-06-12 17:40:00 -0700132 return SkImageDecoder::DecodeStream(&memStream, bitmap, kUnknown_SkColorType,
scroggo@google.com1125d392013-05-03 20:43:37 +0000133 SkImageDecoder::kDecodeBounds_Mode);
134}
135
136////////////////////////////////////////////////////////////////////////////////////////////////////
137
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000138/**
139 * Called only by render_picture().
140 */
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000141static bool render_picture_internal(const SkString& inputPath, const SkString* writePath,
142 const SkString* mismatchPath,
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000143 sk_tools::PictureRenderer& renderer,
144 SkBitmap** out) {
tfarina1c99ea82014-06-11 08:58:50 -0700145 SkString inputFilename = SkOSPath::SkBasename(inputPath.c_str());
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000146 SkString writePathString;
147 if (NULL != writePath && writePath->size() > 0 && !FLAGS_writeEncodedImages) {
148 writePathString.set(*writePath);
149 }
150 SkString mismatchPathString;
151 if (NULL != mismatchPath && mismatchPath->size() > 0) {
152 mismatchPathString.set(*mismatchPath);
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000153 }
twiz@google.coma31b8bb2012-06-22 18:24:56 +0000154
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000155 SkFILEStream inputStream;
twiz@google.coma31b8bb2012-06-22 18:24:56 +0000156 inputStream.setPath(inputPath.c_str());
157 if (!inputStream.isValid()) {
158 SkDebugf("Could not open file %s\n", inputPath.c_str());
borenet@google.com66bcbd12012-09-17 18:26:06 +0000159 return false;
twiz@google.coma31b8bb2012-06-22 18:24:56 +0000160 }
161
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000162 SkPicture::InstallPixelRefProc proc;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000163 if (FLAGS_deferImageDecoding) {
commit-bot@chromium.org56799e22013-07-16 18:21:46 +0000164 proc = &sk_tools::LazyDecodeBitmap;
scroggo@google.com1125d392013-05-03 20:43:37 +0000165 } else if (FLAGS_writeEncodedImages) {
166 SkASSERT(!FLAGS_writePath.isEmpty());
167 reset_image_file_base_name(inputFilename);
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000168 proc = &write_image_to_file;
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000169 } else {
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000170 proc = &SkImageDecoder::DecodeMemory;
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000171 }
scroggo@google.com1125d392013-05-03 20:43:37 +0000172
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000173 SkDebugf("deserializing... %s\n", inputPath.c_str());
174
175 SkPicture* picture = SkPicture::CreateFromStream(&inputStream, proc);
176
177 if (NULL == picture) {
borenet@google.com66bcbd12012-09-17 18:26:06 +0000178 SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str());
179 return false;
180 }
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000181
commit-bot@chromium.orgff36a1d2013-07-24 20:37:30 +0000182 while (FLAGS_bench_record) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000183 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000184 picture->draw(recorder.beginRecording(picture->width(), picture->height(), NULL, 0));
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000185 SkAutoTUnref<SkPicture> other(recorder.endRecording());
commit-bot@chromium.orgff36a1d2013-07-24 20:37:30 +0000186 }
187
edisonn@google.com84f548c2012-12-18 22:24:03 +0000188 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.org3f045172014-05-15 15:10:48 +0000191 renderer.init(picture, &writePathString, &mismatchPathString, &inputFilename,
192 FLAGS_writeChecksumBasedFilenames);
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()) {
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000196 renderer.getCanvas()->EXPERIMENTAL_optimize(renderer.getPicture());
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000197 }
198 }
199
scroggo@google.comb4773b42012-10-01 20:06:09 +0000200 renderer.setup();
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000201 renderer.enableWrites();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000202
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000203 bool success = renderer.render(out);
204 if (!success) {
205 SkDebugf("Failed to render %s\n", inputFilename.c_str());
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000206 }
scroggo@google.com9a412522012-09-07 15:21:18 +0000207
robertphillips952841b2014-06-30 08:26:50 -0700208 if (FLAGS_preprocess) {
209 if (NULL != renderer.getCanvas()) {
210 renderer.getCanvas()->EXPERIMENTAL_purge(renderer.getPicture());
211 }
212 }
213
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000214 renderer.end();
edisonn@google.com84f548c2012-12-18 22:24:03 +0000215
216 SkDELETE(picture);
borenet@google.com66bcbd12012-09-17 18:26:06 +0000217 return success;
junov@chromium.org777442d2012-06-12 14:56:36 +0000218}
219
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000220static inline int getByte(uint32_t value, int index) {
221 SkASSERT(0 <= index && index < 4);
222 return (value >> (index * 8)) & 0xFF;
223}
224
225static int MaxByteDiff(uint32_t v1, uint32_t v2) {
226 return SkMax32(SkMax32(abs(getByte(v1, 0) - getByte(v2, 0)), abs(getByte(v1, 1) - getByte(v2, 1))),
227 SkMax32(abs(getByte(v1, 2) - getByte(v2, 2)), abs(getByte(v1, 3) - getByte(v2, 3))));
228}
229
junov@chromium.orge286e842013-03-13 17:27:16 +0000230class AutoRestoreBbhType {
231public:
232 AutoRestoreBbhType() {
233 fRenderer = NULL;
junov@chromium.orgd34fda12013-03-13 19:03:26 +0000234 fSavedBbhType = sk_tools::PictureRenderer::kNone_BBoxHierarchyType;
junov@chromium.orge286e842013-03-13 17:27:16 +0000235 }
236
237 void set(sk_tools::PictureRenderer* renderer,
238 sk_tools::PictureRenderer::BBoxHierarchyType bbhType) {
239 fRenderer = renderer;
240 fSavedBbhType = renderer->getBBoxHierarchyType();
241 renderer->setBBoxHierarchyType(bbhType);
242 }
243
244 ~AutoRestoreBbhType() {
245 if (NULL != fRenderer) {
246 fRenderer->setBBoxHierarchyType(fSavedBbhType);
247 }
248 }
249
250private:
251 sk_tools::PictureRenderer* fRenderer;
252 sk_tools::PictureRenderer::BBoxHierarchyType fSavedBbhType;
253};
junov@chromium.orge286e842013-03-13 17:27:16 +0000254
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000255/**
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000256 * Render the SKP file(s) within inputPath.
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000257 *
258 * @param inputPath path to an individual SKP file, or a directory of SKP files
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000259 * @param writePath if not NULL, write all image(s) generated into this directory
260 * @param mismatchPath if not NULL, write any image(s) not matching expectations into this directory
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000261 * @param renderer PictureRenderer to use to render the SKPs
262 * @param jsonSummaryPtr if not NULL, add the image(s) generated to this summary
263 */
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000264static bool render_picture(const SkString& inputPath, const SkString* writePath,
265 const SkString* mismatchPath, sk_tools::PictureRenderer& renderer,
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000266 sk_tools::ImageResultsAndExpectations *jsonSummaryPtr) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000267 int diffs[256] = {0};
edisonn@google.com84f548c2012-12-18 22:24:03 +0000268 SkBitmap* bitmap = NULL;
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000269 renderer.setJsonSummaryPtr(jsonSummaryPtr);
270 bool success = render_picture_internal(inputPath,
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000271 FLAGS_writeWholeImage ? NULL : writePath,
272 FLAGS_writeWholeImage ? NULL : mismatchPath,
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.org3f045172014-05-15 15:10:48 +0000298 success = render_picture_internal(inputPath, NULL, NULL, *referenceRenderer,
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000299 &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
tfarina1c99ea82014-06-11 08:58:50 -0700354 SkString inputFilename = SkOSPath::SkBasename(inputPath.c_str());
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000355 SkString outputFilename(inputFilename);
356 sk_tools::replace_char(&outputFilename, '.', '_');
357 outputFilename.append(".png");
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000358
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000359 if (NULL != jsonSummaryPtr) {
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000360 sk_tools::ImageDigest imageDigest(*bitmap);
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000361 jsonSummaryPtr->add(inputFilename.c_str(), outputFilename.c_str(), imageDigest);
362 if ((NULL != mismatchPath) && !mismatchPath->isEmpty() &&
363 !jsonSummaryPtr->matchesExpectation(inputFilename.c_str(), imageDigest)) {
364 success &= sk_tools::write_bitmap_to_disk(*bitmap, *mismatchPath, NULL,
365 outputFilename);
366 }
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000367 }
368
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000369 if ((NULL != writePath) && !writePath->isEmpty()) {
370 success &= sk_tools::write_bitmap_to_disk(*bitmap, *writePath, NULL, outputFilename);
edisonn@google.com84f548c2012-12-18 22:24:03 +0000371 }
372 }
373 SkDELETE(bitmap);
374
375 return success;
376}
377
378
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000379static int process_input(const char* input, const SkString* writePath,
380 const SkString* mismatchPath, sk_tools::PictureRenderer& renderer,
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000381 sk_tools::ImageResultsAndExpectations *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 {
tfarinada4ed322014-06-12 08:50:56 -0700388 SkString inputPath = SkOSPath::SkPathJoin(input, inputFilename.c_str());
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000389 if (!render_picture(inputPath, writePath, mismatchPath, renderer, jsonSummaryPtr)) {
borenet@google.com57837bf2012-09-19 17:28:29 +0000390 ++failures;
391 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000392 } while(iter.next(&inputFilename));
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000393 } else if (SkStrEndsWith(input, ".skp")) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000394 SkString inputPath(input);
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000395 if (!render_picture(inputPath, writePath, mismatchPath, renderer, jsonSummaryPtr)) {
borenet@google.com57837bf2012-09-19 17:28:29 +0000396 ++failures;
397 }
398 } else {
399 SkString warning;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000400 warning.printf("Warning: skipping %s\n", input);
borenet@google.com57837bf2012-09-19 17:28:29 +0000401 SkDebugf(warning.c_str());
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000402 }
borenet@google.com66bcbd12012-09-17 18:26:06 +0000403 return failures;
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000404}
405
caryclark@google.com5987f582012-10-02 18:33:14 +0000406int tool_main(int argc, char** argv);
407int tool_main(int argc, char** argv) {
scroggo@google.comd9ba9a02013-03-21 19:43:15 +0000408 SkCommandLineFlags::SetUsage("Render .skp files.");
409 SkCommandLineFlags::Parse(argc, argv);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000410
scroggo@google.com604e0c22013-04-09 21:25:46 +0000411 if (FLAGS_readPath.isEmpty()) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000412 SkDebugf(".skp files or directories are required.\n");
413 exit(-1);
414 }
415
416 if (FLAGS_maxComponentDiff < 0 || FLAGS_maxComponentDiff > 256) {
417 SkDebugf("--maxComponentDiff must be between 0 and 256\n");
418 exit(-1);
419 }
420
421 if (FLAGS_maxComponentDiff != 256 && !FLAGS_validate) {
422 SkDebugf("--maxComponentDiff requires --validate\n");
423 exit(-1);
424 }
425
scroggo@google.com1125d392013-05-03 20:43:37 +0000426 if (FLAGS_writeEncodedImages) {
427 if (FLAGS_writePath.isEmpty()) {
428 SkDebugf("--writeEncodedImages requires --writePath\n");
429 exit(-1);
430 }
431 if (FLAGS_deferImageDecoding) {
432 SkDebugf("--writeEncodedImages is not compatible with --deferImageDecoding\n");
433 exit(-1);
434 }
435 }
436
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000437 SkString errorString;
438 SkAutoTUnref<sk_tools::PictureRenderer> renderer(parseRenderer(errorString,
439 kRender_PictureTool));
440 if (errorString.size() > 0) {
441 SkDebugf("%s\n", errorString.c_str());
442 }
443
444 if (renderer.get() == NULL) {
445 exit(-1);
446 }
447
borenet@google.com66bcbd12012-09-17 18:26:06 +0000448 SkAutoGraphics ag;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000449
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000450 SkString writePath;
scroggo@google.com604e0c22013-04-09 21:25:46 +0000451 if (FLAGS_writePath.count() == 1) {
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000452 writePath.set(FLAGS_writePath[0]);
453 }
454 SkString mismatchPath;
455 if (FLAGS_mismatchPath.count() == 1) {
456 mismatchPath.set(FLAGS_mismatchPath[0]);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000457 }
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000458 sk_tools::ImageResultsAndExpectations jsonSummary;
459 sk_tools::ImageResultsAndExpectations* jsonSummaryPtr = NULL;
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000460 if (FLAGS_writeJsonSummaryPath.count() == 1) {
461 jsonSummaryPtr = &jsonSummary;
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000462 if (FLAGS_readJsonSummaryPath.count() == 1) {
463 SkASSERT(jsonSummary.readExpectationsFile(FLAGS_readJsonSummaryPath[0]));
464 }
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000465 }
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.org3f045172014-05-15 15:10:48 +0000469 failures += process_input(FLAGS_readPath[i], &writePath, &mismatchPath, *renderer.get(),
470 jsonSummaryPtr);
junov@chromium.org777442d2012-06-12 14:56:36 +0000471 }
borenet@google.com66bcbd12012-09-17 18:26:06 +0000472 if (failures != 0) {
473 SkDebugf("Failed to render %i pictures.\n", failures);
474 return 1;
475 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000476#if SK_SUPPORT_GPU
477#if GR_CACHE_STATS
478 if (renderer->isUsingGpuDevice()) {
479 GrContext* ctx = renderer->getGrContext();
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000480 ctx->printCacheStats();
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000481#ifdef SK_DEVELOPER
482 ctx->dumpFontCache();
483#endif
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000484 }
485#endif
486#endif
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000487 if (FLAGS_writeJsonSummaryPath.count() == 1) {
488 jsonSummary.writeToFile(FLAGS_writeJsonSummaryPath[0]);
489 }
caryclark@google.com868e1f62012-10-02 20:00:03 +0000490 return 0;
junov@chromium.org777442d2012-06-12 14:56:36 +0000491}
caryclark@google.com5987f582012-10-02 18:33:14 +0000492
493#if !defined SK_BUILD_FOR_IOS
494int main(int argc, char * const argv[]) {
495 return tool_main(argc, (char**) argv);
496}
497#endif