blob: 9ec136c1e7140e1720066176aaa7e0deed1d4ac1 [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:
epogerb492c6f2014-08-14 07:32:49 -070029DEFINE_bool(bench_record, false, "If true, drop into an infinite loop of recording the picture.");
scroggo@google.com161e1ba2013-03-04 16:41:06 +000030DECLARE_bool(deferImageDecoding);
epogerb492c6f2014-08-14 07:32:49 -070031DEFINE_string(descriptions, "", "one or more key=value pairs to add to the descriptions section "
32 "of the JSON summary.");
rmistry2529f2e2014-08-22 04:46:30 -070033DEFINE_string(imageBaseGSUrl, "", "The Google Storage image base URL the images are stored in.");
scroggo@google.com161e1ba2013-03-04 16:41:06 +000034DEFINE_int32(maxComponentDiff, 256, "Maximum diff on a component, 0 - 256. Components that differ "
35 "by more than this amount are considered errors, though all diffs are reported. "
36 "Requires --validate.");
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000037DEFINE_string(mismatchPath, "", "Write images for tests that failed due to "
38 "pixel mismatches into this directory.");
epogerb492c6f2014-08-14 07:32:49 -070039DEFINE_bool(preprocess, false, "If true, perform device specific preprocessing before rendering.");
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000040DEFINE_string(readJsonSummaryPath, "", "JSON file to read image expectations from.");
scroggo@google.com604e0c22013-04-09 21:25:46 +000041DECLARE_string(readPath);
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000042DEFINE_bool(writeChecksumBasedFilenames, false,
43 "When writing out images, use checksum-based filenames.");
scroggo@google.com1125d392013-05-03 20:43:37 +000044DEFINE_bool(writeEncodedImages, false, "Any time the skp contains an encoded image, write it to a "
45 "file rather than decoding it. Requires writePath to be set. Skips drawing the full "
46 "skp to a file. Not compatible with deferImageDecoding.");
commit-bot@chromium.org4610a462014-04-29 19:39:22 +000047DEFINE_string(writeJsonSummaryPath, "", "File to write a JSON summary of image results to.");
48DEFINE_string2(writePath, w, "", "Directory to write the rendered images into.");
scroggo@google.com161e1ba2013-03-04 16:41:06 +000049DEFINE_bool(writeWholeImage, false, "In tile mode, write the entire rendered image to a "
50 "file, instead of an image for each tile.");
51DEFINE_bool(validate, false, "Verify that the rendered image contains the same pixels as "
junov@chromium.orge286e842013-03-13 17:27:16 +000052 "the picture rendered in simple mode. When used in conjunction with --bbh, results "
53 "are validated against the picture rendered in the same mode, but without the bbh.");
junov@chromium.org777442d2012-06-12 14:56:36 +000054
scroggo@google.com1125d392013-05-03 20:43:37 +000055////////////////////////////////////////////////////////////////////////////////////////////////////
56
57/**
58 * Table for translating from format of data to a suffix.
59 */
60struct Format {
61 SkImageDecoder::Format fFormat;
62 const char* fSuffix;
63};
64static const Format gFormats[] = {
65 { SkImageDecoder::kBMP_Format, ".bmp" },
66 { SkImageDecoder::kGIF_Format, ".gif" },
67 { SkImageDecoder::kICO_Format, ".ico" },
68 { SkImageDecoder::kJPEG_Format, ".jpg" },
69 { SkImageDecoder::kPNG_Format, ".png" },
70 { SkImageDecoder::kWBMP_Format, ".wbmp" },
71 { SkImageDecoder::kWEBP_Format, ".webp" },
72 { SkImageDecoder::kUnknown_Format, "" },
73};
74
75/**
76 * Get an appropriate suffix for an image format.
77 */
78static const char* get_suffix_from_format(SkImageDecoder::Format format) {
79 for (size_t i = 0; i < SK_ARRAY_COUNT(gFormats); i++) {
80 if (gFormats[i].fFormat == format) {
81 return gFormats[i].fSuffix;
82 }
83 }
84 return "";
85}
86
87/**
88 * Base name for an image file created from the encoded data in an skp.
89 */
90static SkString gInputFileName;
91
92/**
93 * Number to be appended to the image file name so that it is unique.
94 */
95static uint32_t gImageNo;
96
97/**
98 * Set up the name for writing encoded data to a file.
99 * Sets gInputFileName to name, minus any extension ".*"
100 * Sets gImageNo to 0, so images from file "X.skp" will
101 * look like "X_<gImageNo>.<suffix>", beginning with 0
102 * for each new skp.
103 */
104static void reset_image_file_base_name(const SkString& name) {
105 gImageNo = 0;
106 // Remove ".skp"
107 const char* cName = name.c_str();
108 const char* dot = strrchr(cName, '.');
109 if (dot != NULL) {
110 gInputFileName.set(cName, dot - cName);
111 } else {
112 gInputFileName.set(name);
113 }
114}
115
116/**
117 * Write the raw encoded bitmap data to a file.
118 */
119static bool write_image_to_file(const void* buffer, size_t size, SkBitmap* bitmap) {
120 SkASSERT(!FLAGS_writePath.isEmpty());
121 SkMemoryStream memStream(buffer, size);
122 SkString outPath;
123 SkImageDecoder::Format format = SkImageDecoder::GetStreamFormat(&memStream);
124 SkString name = SkStringPrintf("%s_%d%s", gInputFileName.c_str(), gImageNo++,
125 get_suffix_from_format(format));
126 SkString dir(FLAGS_writePath[0]);
tfarinaa8e2e152014-07-28 19:26:58 -0700127 outPath = SkOSPath::Join(dir.c_str(), name.c_str());
scroggo@google.com1125d392013-05-03 20:43:37 +0000128 SkFILEWStream fileStream(outPath.c_str());
129 if (!(fileStream.isValid() && fileStream.write(buffer, size))) {
130 SkDebugf("Failed to write encoded data to \"%s\"\n", outPath.c_str());
131 }
132 // Put in a dummy bitmap.
reedbfefc7c2014-06-12 17:40:00 -0700133 return SkImageDecoder::DecodeStream(&memStream, bitmap, kUnknown_SkColorType,
scroggo@google.com1125d392013-05-03 20:43:37 +0000134 SkImageDecoder::kDecodeBounds_Mode);
135}
136
137////////////////////////////////////////////////////////////////////////////////////////////////////
138
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000139/**
140 * Called only by render_picture().
141 */
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000142static bool render_picture_internal(const SkString& inputPath, const SkString* writePath,
143 const SkString* mismatchPath,
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000144 sk_tools::PictureRenderer& renderer,
145 SkBitmap** out) {
tfarinaa8e2e152014-07-28 19:26:58 -0700146 SkString inputFilename = SkOSPath::Basename(inputPath.c_str());
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000147 SkString writePathString;
148 if (NULL != writePath && writePath->size() > 0 && !FLAGS_writeEncodedImages) {
149 writePathString.set(*writePath);
150 }
151 SkString mismatchPathString;
152 if (NULL != mismatchPath && mismatchPath->size() > 0) {
153 mismatchPathString.set(*mismatchPath);
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000154 }
twiz@google.coma31b8bb2012-06-22 18:24:56 +0000155
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000156 SkFILEStream inputStream;
twiz@google.coma31b8bb2012-06-22 18:24:56 +0000157 inputStream.setPath(inputPath.c_str());
158 if (!inputStream.isValid()) {
159 SkDebugf("Could not open file %s\n", inputPath.c_str());
borenet@google.com66bcbd12012-09-17 18:26:06 +0000160 return false;
twiz@google.coma31b8bb2012-06-22 18:24:56 +0000161 }
162
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000163 SkPicture::InstallPixelRefProc proc;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000164 if (FLAGS_deferImageDecoding) {
commit-bot@chromium.org56799e22013-07-16 18:21:46 +0000165 proc = &sk_tools::LazyDecodeBitmap;
scroggo@google.com1125d392013-05-03 20:43:37 +0000166 } else if (FLAGS_writeEncodedImages) {
167 SkASSERT(!FLAGS_writePath.isEmpty());
168 reset_image_file_base_name(inputFilename);
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000169 proc = &write_image_to_file;
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000170 } else {
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000171 proc = &SkImageDecoder::DecodeMemory;
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000172 }
scroggo@google.com1125d392013-05-03 20:43:37 +0000173
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000174 SkDebugf("deserializing... %s\n", inputPath.c_str());
175
robertphillipse9470752014-08-12 10:12:40 -0700176 SkAutoTUnref<SkPicture> picture(SkPicture::CreateFromStream(&inputStream, proc));
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000177
178 if (NULL == picture) {
borenet@google.com66bcbd12012-09-17 18:26:06 +0000179 SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str());
180 return false;
181 }
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000182
robertphillipse9470752014-08-12 10:12:40 -0700183 if (FLAGS_preprocess) {
184 // Because the GPU preprocessing step relies on the in-memory picture
185 // statistics we need to rerecord the picture here
186 SkPictureRecorder recorder;
robertphillipsc5ba71d2014-09-04 08:42:50 -0700187 picture->playback(recorder.beginRecording(picture->cullRect().width(),
188 picture->cullRect().height(),
189 NULL, 0));
robertphillipse9470752014-08-12 10:12:40 -0700190 picture.reset(recorder.endRecording());
191 }
192
commit-bot@chromium.orgff36a1d2013-07-24 20:37:30 +0000193 while (FLAGS_bench_record) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000194 SkPictureRecorder recorder;
robertphillipsc5ba71d2014-09-04 08:42:50 -0700195 picture->playback(recorder.beginRecording(picture->cullRect().width(),
196 picture->cullRect().height(),
197 NULL, 0));
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000198 SkAutoTUnref<SkPicture> other(recorder.endRecording());
commit-bot@chromium.orgff36a1d2013-07-24 20:37:30 +0000199 }
200
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700201 SkDebugf("drawing... [%f %f %f %f] %s\n",
202 picture->cullRect().fLeft, picture->cullRect().fTop,
203 picture->cullRect().fRight, picture->cullRect().fBottom,
borenet@google.com03fcee82012-09-10 18:18:38 +0000204 inputPath.c_str());
skia.committer@gmail.com1d225f22012-09-14 02:01:10 +0000205
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000206 renderer.init(picture, &writePathString, &mismatchPathString, &inputFilename,
207 FLAGS_writeChecksumBasedFilenames);
skia.committer@gmail.comeb849e52014-03-17 03:02:17 +0000208
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000209 if (FLAGS_preprocess) {
210 if (NULL != renderer.getCanvas()) {
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000211 renderer.getCanvas()->EXPERIMENTAL_optimize(renderer.getPicture());
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000212 }
213 }
214
scroggo@google.comb4773b42012-10-01 20:06:09 +0000215 renderer.setup();
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000216 renderer.enableWrites();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000217
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000218 bool success = renderer.render(out);
219 if (!success) {
220 SkDebugf("Failed to render %s\n", inputFilename.c_str());
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000221 }
scroggo@google.com9a412522012-09-07 15:21:18 +0000222
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000223 renderer.end();
edisonn@google.com84f548c2012-12-18 22:24:03 +0000224
borenet@google.com66bcbd12012-09-17 18:26:06 +0000225 return success;
junov@chromium.org777442d2012-06-12 14:56:36 +0000226}
227
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000228static inline int getByte(uint32_t value, int index) {
229 SkASSERT(0 <= index && index < 4);
230 return (value >> (index * 8)) & 0xFF;
231}
232
233static int MaxByteDiff(uint32_t v1, uint32_t v2) {
234 return SkMax32(SkMax32(abs(getByte(v1, 0) - getByte(v2, 0)), abs(getByte(v1, 1) - getByte(v2, 1))),
235 SkMax32(abs(getByte(v1, 2) - getByte(v2, 2)), abs(getByte(v1, 3) - getByte(v2, 3))));
236}
237
junov@chromium.orge286e842013-03-13 17:27:16 +0000238class AutoRestoreBbhType {
239public:
240 AutoRestoreBbhType() {
241 fRenderer = NULL;
junov@chromium.orgd34fda12013-03-13 19:03:26 +0000242 fSavedBbhType = sk_tools::PictureRenderer::kNone_BBoxHierarchyType;
junov@chromium.orge286e842013-03-13 17:27:16 +0000243 }
244
245 void set(sk_tools::PictureRenderer* renderer,
246 sk_tools::PictureRenderer::BBoxHierarchyType bbhType) {
247 fRenderer = renderer;
248 fSavedBbhType = renderer->getBBoxHierarchyType();
249 renderer->setBBoxHierarchyType(bbhType);
250 }
251
252 ~AutoRestoreBbhType() {
253 if (NULL != fRenderer) {
254 fRenderer->setBBoxHierarchyType(fSavedBbhType);
255 }
256 }
257
258private:
259 sk_tools::PictureRenderer* fRenderer;
260 sk_tools::PictureRenderer::BBoxHierarchyType fSavedBbhType;
261};
junov@chromium.orge286e842013-03-13 17:27:16 +0000262
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000263/**
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000264 * Render the SKP file(s) within inputPath.
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000265 *
266 * @param inputPath path to an individual SKP file, or a directory of SKP files
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000267 * @param writePath if not NULL, write all image(s) generated into this directory
268 * @param mismatchPath if not NULL, write any image(s) not matching expectations into this directory
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000269 * @param renderer PictureRenderer to use to render the SKPs
270 * @param jsonSummaryPtr if not NULL, add the image(s) generated to this summary
271 */
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000272static bool render_picture(const SkString& inputPath, const SkString* writePath,
273 const SkString* mismatchPath, sk_tools::PictureRenderer& renderer,
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000274 sk_tools::ImageResultsAndExpectations *jsonSummaryPtr) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000275 int diffs[256] = {0};
edisonn@google.com84f548c2012-12-18 22:24:03 +0000276 SkBitmap* bitmap = NULL;
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000277 renderer.setJsonSummaryPtr(jsonSummaryPtr);
278 bool success = render_picture_internal(inputPath,
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000279 FLAGS_writeWholeImage ? NULL : writePath,
280 FLAGS_writeWholeImage ? NULL : mismatchPath,
edisonn@google.com84f548c2012-12-18 22:24:03 +0000281 renderer,
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000282 FLAGS_validate || FLAGS_writeWholeImage ? &bitmap : NULL);
edisonn@google.com84f548c2012-12-18 22:24:03 +0000283
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000284 if (!success || ((FLAGS_validate || FLAGS_writeWholeImage) && bitmap == NULL)) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000285 SkDebugf("Failed to draw the picture.\n");
286 SkDELETE(bitmap);
287 return false;
288 }
289
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000290 if (FLAGS_validate) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000291 SkBitmap* referenceBitmap = NULL;
junov@chromium.orge286e842013-03-13 17:27:16 +0000292 sk_tools::PictureRenderer* referenceRenderer;
293 // If the renderer uses a BBoxHierarchy, then the reference renderer
skia.committer@gmail.com03682be2013-03-14 07:02:51 +0000294 // will be the same renderer, without the bbh.
junov@chromium.orge286e842013-03-13 17:27:16 +0000295 AutoRestoreBbhType arbbh;
296 if (sk_tools::PictureRenderer::kNone_BBoxHierarchyType !=
297 renderer.getBBoxHierarchyType()) {
298 referenceRenderer = &renderer;
299 referenceRenderer->ref(); // to match auto unref below
300 arbbh.set(referenceRenderer, sk_tools::PictureRenderer::kNone_BBoxHierarchyType);
301 } else {
krajcevskib1aded82014-08-18 07:52:17 -0700302#if SK_SUPPORT_GPU
303 referenceRenderer = SkNEW_ARGS(sk_tools::SimplePictureRenderer,
304 (renderer.getGrContextOptions()));
305#else
junov@chromium.orge286e842013-03-13 17:27:16 +0000306 referenceRenderer = SkNEW(sk_tools::SimplePictureRenderer);
krajcevskib1aded82014-08-18 07:52:17 -0700307#endif
junov@chromium.orge286e842013-03-13 17:27:16 +0000308 }
309 SkAutoTUnref<sk_tools::PictureRenderer> aurReferenceRenderer(referenceRenderer);
310
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000311 success = render_picture_internal(inputPath, NULL, NULL, *referenceRenderer,
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000312 &referenceBitmap);
edisonn@google.com84f548c2012-12-18 22:24:03 +0000313
junov@chromium.orgc19c1912013-03-12 19:56:49 +0000314 if (!success || NULL == referenceBitmap || NULL == referenceBitmap->getPixels()) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000315 SkDebugf("Failed to draw the reference picture.\n");
316 SkDELETE(bitmap);
317 SkDELETE(referenceBitmap);
318 return false;
319 }
320
321 if (success && (bitmap->width() != referenceBitmap->width())) {
322 SkDebugf("Expected image width: %i, actual image width %i.\n",
323 referenceBitmap->width(), bitmap->width());
324 SkDELETE(bitmap);
325 SkDELETE(referenceBitmap);
326 return false;
327 }
328 if (success && (bitmap->height() != referenceBitmap->height())) {
329 SkDebugf("Expected image height: %i, actual image height %i",
330 referenceBitmap->height(), bitmap->height());
331 SkDELETE(bitmap);
332 SkDELETE(referenceBitmap);
333 return false;
334 }
skia.committer@gmail.coma7d8e3e2012-12-19 02:01:38 +0000335
edisonn@google.com84f548c2012-12-18 22:24:03 +0000336 for (int y = 0; success && y < bitmap->height(); y++) {
337 for (int x = 0; success && x < bitmap->width(); x++) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000338 int diff = MaxByteDiff(*referenceBitmap->getAddr32(x, y),
339 *bitmap->getAddr32(x, y));
340 SkASSERT(diff >= 0 && diff <= 255);
341 diffs[diff]++;
skia.committer@gmail.com4d28d982013-01-17 07:06:06 +0000342
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000343 if (diff > FLAGS_maxComponentDiff) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000344 SkDebugf("Expected pixel at (%i %i) exceedds maximum "
345 "component diff of %i: 0x%x, actual 0x%x\n",
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000346 x, y, FLAGS_maxComponentDiff,
edisonn@google.com01754bf2013-01-11 16:08:07 +0000347 *referenceBitmap->getAddr32(x, y),
edisonn@google.com84f548c2012-12-18 22:24:03 +0000348 *bitmap->getAddr32(x, y));
349 SkDELETE(bitmap);
350 SkDELETE(referenceBitmap);
351 return false;
352 }
353 }
354 }
355 SkDELETE(referenceBitmap);
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000356
357 for (int i = 1; i <= 255; ++i) {
358 if(diffs[i] > 0) {
359 SkDebugf("Number of pixels with max diff of %i is %i\n", i, diffs[i]);
360 }
361 }
edisonn@google.com84f548c2012-12-18 22:24:03 +0000362 }
363
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000364 if (FLAGS_writeWholeImage) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000365 sk_tools::force_all_opaque(*bitmap);
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000366
tfarinaa8e2e152014-07-28 19:26:58 -0700367 SkString inputFilename = SkOSPath::Basename(inputPath.c_str());
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000368 SkString outputFilename(inputFilename);
369 sk_tools::replace_char(&outputFilename, '.', '_');
370 outputFilename.append(".png");
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000371
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000372 if (NULL != jsonSummaryPtr) {
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000373 sk_tools::ImageDigest imageDigest(*bitmap);
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000374 jsonSummaryPtr->add(inputFilename.c_str(), outputFilename.c_str(), imageDigest);
375 if ((NULL != mismatchPath) && !mismatchPath->isEmpty() &&
epoger85b438d2014-08-21 23:21:32 -0700376 !jsonSummaryPtr->getExpectation(inputFilename.c_str()).matches(imageDigest)) {
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000377 success &= sk_tools::write_bitmap_to_disk(*bitmap, *mismatchPath, NULL,
378 outputFilename);
379 }
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000380 }
381
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000382 if ((NULL != writePath) && !writePath->isEmpty()) {
383 success &= sk_tools::write_bitmap_to_disk(*bitmap, *writePath, NULL, outputFilename);
edisonn@google.com84f548c2012-12-18 22:24:03 +0000384 }
385 }
386 SkDELETE(bitmap);
387
388 return success;
389}
390
391
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000392static int process_input(const char* input, const SkString* writePath,
393 const SkString* mismatchPath, sk_tools::PictureRenderer& renderer,
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000394 sk_tools::ImageResultsAndExpectations *jsonSummaryPtr) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000395 SkOSFile::Iter iter(input, "skp");
junov@chromium.org777442d2012-06-12 14:56:36 +0000396 SkString inputFilename;
borenet@google.com66bcbd12012-09-17 18:26:06 +0000397 int failures = 0;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000398 SkDebugf("process_input, %s\n", input);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000399 if (iter.next(&inputFilename)) {
400 do {
tfarinaa8e2e152014-07-28 19:26:58 -0700401 SkString inputPath = SkOSPath::Join(input, inputFilename.c_str());
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000402 if (!render_picture(inputPath, writePath, mismatchPath, renderer, jsonSummaryPtr)) {
borenet@google.com57837bf2012-09-19 17:28:29 +0000403 ++failures;
404 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000405 } while(iter.next(&inputFilename));
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000406 } else if (SkStrEndsWith(input, ".skp")) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000407 SkString inputPath(input);
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000408 if (!render_picture(inputPath, writePath, mismatchPath, renderer, jsonSummaryPtr)) {
borenet@google.com57837bf2012-09-19 17:28:29 +0000409 ++failures;
410 }
411 } else {
412 SkString warning;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000413 warning.printf("Warning: skipping %s\n", input);
borenet@google.com57837bf2012-09-19 17:28:29 +0000414 SkDebugf(warning.c_str());
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000415 }
borenet@google.com66bcbd12012-09-17 18:26:06 +0000416 return failures;
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000417}
418
caryclark@google.com5987f582012-10-02 18:33:14 +0000419int tool_main(int argc, char** argv);
420int tool_main(int argc, char** argv) {
scroggo@google.comd9ba9a02013-03-21 19:43:15 +0000421 SkCommandLineFlags::SetUsage("Render .skp files.");
422 SkCommandLineFlags::Parse(argc, argv);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000423
scroggo@google.com604e0c22013-04-09 21:25:46 +0000424 if (FLAGS_readPath.isEmpty()) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000425 SkDebugf(".skp files or directories are required.\n");
426 exit(-1);
427 }
428
429 if (FLAGS_maxComponentDiff < 0 || FLAGS_maxComponentDiff > 256) {
430 SkDebugf("--maxComponentDiff must be between 0 and 256\n");
431 exit(-1);
432 }
433
434 if (FLAGS_maxComponentDiff != 256 && !FLAGS_validate) {
435 SkDebugf("--maxComponentDiff requires --validate\n");
436 exit(-1);
437 }
438
scroggo@google.com1125d392013-05-03 20:43:37 +0000439 if (FLAGS_writeEncodedImages) {
440 if (FLAGS_writePath.isEmpty()) {
441 SkDebugf("--writeEncodedImages requires --writePath\n");
442 exit(-1);
443 }
444 if (FLAGS_deferImageDecoding) {
445 SkDebugf("--writeEncodedImages is not compatible with --deferImageDecoding\n");
446 exit(-1);
447 }
448 }
449
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000450 SkString errorString;
451 SkAutoTUnref<sk_tools::PictureRenderer> renderer(parseRenderer(errorString,
452 kRender_PictureTool));
453 if (errorString.size() > 0) {
454 SkDebugf("%s\n", errorString.c_str());
455 }
456
457 if (renderer.get() == NULL) {
458 exit(-1);
459 }
460
borenet@google.com66bcbd12012-09-17 18:26:06 +0000461 SkAutoGraphics ag;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000462
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000463 SkString writePath;
scroggo@google.com604e0c22013-04-09 21:25:46 +0000464 if (FLAGS_writePath.count() == 1) {
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000465 writePath.set(FLAGS_writePath[0]);
466 }
467 SkString mismatchPath;
468 if (FLAGS_mismatchPath.count() == 1) {
469 mismatchPath.set(FLAGS_mismatchPath[0]);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000470 }
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000471 sk_tools::ImageResultsAndExpectations jsonSummary;
472 sk_tools::ImageResultsAndExpectations* jsonSummaryPtr = NULL;
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000473 if (FLAGS_writeJsonSummaryPath.count() == 1) {
474 jsonSummaryPtr = &jsonSummary;
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000475 if (FLAGS_readJsonSummaryPath.count() == 1) {
476 SkASSERT(jsonSummary.readExpectationsFile(FLAGS_readJsonSummaryPath[0]));
477 }
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000478 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000479
borenet@google.com66bcbd12012-09-17 18:26:06 +0000480 int failures = 0;
scroggo@google.com604e0c22013-04-09 21:25:46 +0000481 for (int i = 0; i < FLAGS_readPath.count(); i ++) {
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000482 failures += process_input(FLAGS_readPath[i], &writePath, &mismatchPath, *renderer.get(),
483 jsonSummaryPtr);
junov@chromium.org777442d2012-06-12 14:56:36 +0000484 }
borenet@google.com66bcbd12012-09-17 18:26:06 +0000485 if (failures != 0) {
486 SkDebugf("Failed to render %i pictures.\n", failures);
487 return 1;
488 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000489#if SK_SUPPORT_GPU
490#if GR_CACHE_STATS
491 if (renderer->isUsingGpuDevice()) {
492 GrContext* ctx = renderer->getGrContext();
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000493 ctx->printCacheStats();
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000494#ifdef SK_DEVELOPER
495 ctx->dumpFontCache();
496#endif
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000497 }
498#endif
499#endif
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000500 if (FLAGS_writeJsonSummaryPath.count() == 1) {
epogerb492c6f2014-08-14 07:32:49 -0700501 // If there were any descriptions on the command line, insert them now.
502 for (int i=0; i<FLAGS_descriptions.count(); i++) {
503 SkTArray<SkString> tokens;
504 SkStrSplit(FLAGS_descriptions[i], "=", &tokens);
505 SkASSERT(tokens.count() == 2);
506 jsonSummary.addDescription(tokens[0].c_str(), tokens[1].c_str());
507 }
rmistry2529f2e2014-08-22 04:46:30 -0700508 if (FLAGS_imageBaseGSUrl.count() == 1) {
509 jsonSummary.setImageBaseGSUrl(FLAGS_imageBaseGSUrl[0]);
510 }
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000511 jsonSummary.writeToFile(FLAGS_writeJsonSummaryPath[0]);
512 }
caryclark@google.com868e1f62012-10-02 20:00:03 +0000513 return 0;
junov@chromium.org777442d2012-06-12 14:56:36 +0000514}
caryclark@google.com5987f582012-10-02 18:33:14 +0000515
516#if !defined SK_BUILD_FOR_IOS
517int main(int argc, char * const argv[]) {
518 return tool_main(argc, (char**) argv);
519}
520#endif