blob: 392083197eed8706bada036ccdc4a5d91dc1560f [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
bungeman60e0fee2015-08-26 05:15:46 -070028#include <stdlib.h>
29
scroggo@google.com161e1ba2013-03-04 16:41:06 +000030// Flags used by this file, alphabetically:
epogerb492c6f2014-08-14 07:32:49 -070031DEFINE_bool(bench_record, false, "If true, drop into an infinite loop of recording the picture.");
scroggo@google.com161e1ba2013-03-04 16:41:06 +000032DECLARE_bool(deferImageDecoding);
epogerb492c6f2014-08-14 07:32:49 -070033DEFINE_string(descriptions, "", "one or more key=value pairs to add to the descriptions section "
34 "of the JSON summary.");
rmistry2529f2e2014-08-22 04:46:30 -070035DEFINE_string(imageBaseGSUrl, "", "The Google Storage image base URL the images are stored in.");
scroggo@google.com161e1ba2013-03-04 16:41:06 +000036DEFINE_int32(maxComponentDiff, 256, "Maximum diff on a component, 0 - 256. Components that differ "
37 "by more than this amount are considered errors, though all diffs are reported. "
38 "Requires --validate.");
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000039DEFINE_string(mismatchPath, "", "Write images for tests that failed due to "
40 "pixel mismatches into this directory.");
robertphillips754f4e92014-09-18 13:52:08 -070041#if GR_GPU_STATS
42DEFINE_bool(gpuStats, false, "Only meaningful with gpu configurations. "
43 "Report some GPU call statistics.");
44#endif
robertphillips78c71272014-10-09 04:59:19 -070045DEFINE_bool(mpd, false, "If true, use MultiPictureDraw for rendering.");
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000046DEFINE_string(readJsonSummaryPath, "", "JSON file to read image expectations from.");
scroggo@google.com604e0c22013-04-09 21:25:46 +000047DECLARE_string(readPath);
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000048DEFINE_bool(writeChecksumBasedFilenames, false,
49 "When writing out images, use checksum-based filenames.");
scroggo@google.com1125d392013-05-03 20:43:37 +000050DEFINE_bool(writeEncodedImages, false, "Any time the skp contains an encoded image, write it to a "
51 "file rather than decoding it. Requires writePath to be set. Skips drawing the full "
52 "skp to a file. Not compatible with deferImageDecoding.");
commit-bot@chromium.org4610a462014-04-29 19:39:22 +000053DEFINE_string(writeJsonSummaryPath, "", "File to write a JSON summary of image results to.");
54DEFINE_string2(writePath, w, "", "Directory to write the rendered images into.");
scroggo@google.com161e1ba2013-03-04 16:41:06 +000055DEFINE_bool(writeWholeImage, false, "In tile mode, write the entire rendered image to a "
56 "file, instead of an image for each tile.");
57DEFINE_bool(validate, false, "Verify that the rendered image contains the same pixels as "
junov@chromium.orge286e842013-03-13 17:27:16 +000058 "the picture rendered in simple mode. When used in conjunction with --bbh, results "
59 "are validated against the picture rendered in the same mode, but without the bbh.");
junov@chromium.org777442d2012-06-12 14:56:36 +000060
scroggo@google.com1125d392013-05-03 20:43:37 +000061////////////////////////////////////////////////////////////////////////////////////////////////////
62
63/**
64 * Table for translating from format of data to a suffix.
65 */
66struct Format {
67 SkImageDecoder::Format fFormat;
68 const char* fSuffix;
69};
70static const Format gFormats[] = {
71 { SkImageDecoder::kBMP_Format, ".bmp" },
72 { SkImageDecoder::kGIF_Format, ".gif" },
73 { SkImageDecoder::kICO_Format, ".ico" },
74 { SkImageDecoder::kJPEG_Format, ".jpg" },
75 { SkImageDecoder::kPNG_Format, ".png" },
76 { SkImageDecoder::kWBMP_Format, ".wbmp" },
77 { SkImageDecoder::kWEBP_Format, ".webp" },
78 { SkImageDecoder::kUnknown_Format, "" },
79};
80
81/**
82 * Get an appropriate suffix for an image format.
83 */
84static const char* get_suffix_from_format(SkImageDecoder::Format format) {
85 for (size_t i = 0; i < SK_ARRAY_COUNT(gFormats); i++) {
86 if (gFormats[i].fFormat == format) {
87 return gFormats[i].fSuffix;
88 }
89 }
90 return "";
91}
92
93/**
94 * Base name for an image file created from the encoded data in an skp.
95 */
96static SkString gInputFileName;
97
98/**
99 * Number to be appended to the image file name so that it is unique.
100 */
101static uint32_t gImageNo;
102
103/**
104 * Set up the name for writing encoded data to a file.
105 * Sets gInputFileName to name, minus any extension ".*"
106 * Sets gImageNo to 0, so images from file "X.skp" will
107 * look like "X_<gImageNo>.<suffix>", beginning with 0
108 * for each new skp.
109 */
110static void reset_image_file_base_name(const SkString& name) {
111 gImageNo = 0;
112 // Remove ".skp"
113 const char* cName = name.c_str();
114 const char* dot = strrchr(cName, '.');
115 if (dot != NULL) {
116 gInputFileName.set(cName, dot - cName);
117 } else {
118 gInputFileName.set(name);
119 }
120}
121
122/**
123 * Write the raw encoded bitmap data to a file.
124 */
125static bool write_image_to_file(const void* buffer, size_t size, SkBitmap* bitmap) {
126 SkASSERT(!FLAGS_writePath.isEmpty());
127 SkMemoryStream memStream(buffer, size);
128 SkString outPath;
129 SkImageDecoder::Format format = SkImageDecoder::GetStreamFormat(&memStream);
130 SkString name = SkStringPrintf("%s_%d%s", gInputFileName.c_str(), gImageNo++,
131 get_suffix_from_format(format));
132 SkString dir(FLAGS_writePath[0]);
tfarinaa8e2e152014-07-28 19:26:58 -0700133 outPath = SkOSPath::Join(dir.c_str(), name.c_str());
scroggo@google.com1125d392013-05-03 20:43:37 +0000134 SkFILEWStream fileStream(outPath.c_str());
135 if (!(fileStream.isValid() && fileStream.write(buffer, size))) {
136 SkDebugf("Failed to write encoded data to \"%s\"\n", outPath.c_str());
137 }
138 // Put in a dummy bitmap.
reedbfefc7c2014-06-12 17:40:00 -0700139 return SkImageDecoder::DecodeStream(&memStream, bitmap, kUnknown_SkColorType,
scroggo@google.com1125d392013-05-03 20:43:37 +0000140 SkImageDecoder::kDecodeBounds_Mode);
141}
142
143////////////////////////////////////////////////////////////////////////////////////////////////////
144
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000145/**
146 * Called only by render_picture().
147 */
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000148static bool render_picture_internal(const SkString& inputPath, const SkString* writePath,
149 const SkString* mismatchPath,
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000150 sk_tools::PictureRenderer& renderer,
151 SkBitmap** out) {
tfarinaa8e2e152014-07-28 19:26:58 -0700152 SkString inputFilename = SkOSPath::Basename(inputPath.c_str());
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000153 SkString writePathString;
bsalomon49f085d2014-09-05 13:34:00 -0700154 if (writePath && writePath->size() > 0 && !FLAGS_writeEncodedImages) {
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000155 writePathString.set(*writePath);
156 }
157 SkString mismatchPathString;
bsalomon49f085d2014-09-05 13:34:00 -0700158 if (mismatchPath && mismatchPath->size() > 0) {
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000159 mismatchPathString.set(*mismatchPath);
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000160 }
twiz@google.coma31b8bb2012-06-22 18:24:56 +0000161
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000162 SkFILEStream inputStream;
twiz@google.coma31b8bb2012-06-22 18:24:56 +0000163 inputStream.setPath(inputPath.c_str());
164 if (!inputStream.isValid()) {
165 SkDebugf("Could not open file %s\n", inputPath.c_str());
borenet@google.com66bcbd12012-09-17 18:26:06 +0000166 return false;
twiz@google.coma31b8bb2012-06-22 18:24:56 +0000167 }
168
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000169 SkPicture::InstallPixelRefProc proc;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000170 if (FLAGS_deferImageDecoding) {
commit-bot@chromium.org56799e22013-07-16 18:21:46 +0000171 proc = &sk_tools::LazyDecodeBitmap;
scroggo@google.com1125d392013-05-03 20:43:37 +0000172 } else if (FLAGS_writeEncodedImages) {
173 SkASSERT(!FLAGS_writePath.isEmpty());
174 reset_image_file_base_name(inputFilename);
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000175 proc = &write_image_to_file;
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000176 } else {
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000177 proc = &SkImageDecoder::DecodeMemory;
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000178 }
scroggo@google.com1125d392013-05-03 20:43:37 +0000179
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000180 SkDebugf("deserializing... %s\n", inputPath.c_str());
181
robertphillipse9470752014-08-12 10:12:40 -0700182 SkAutoTUnref<SkPicture> picture(SkPicture::CreateFromStream(&inputStream, proc));
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000183
184 if (NULL == picture) {
borenet@google.com66bcbd12012-09-17 18:26:06 +0000185 SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str());
186 return false;
187 }
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000188
commit-bot@chromium.orgff36a1d2013-07-24 20:37:30 +0000189 while (FLAGS_bench_record) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000190 SkPictureRecorder recorder;
robertphillipsc5ba71d2014-09-04 08:42:50 -0700191 picture->playback(recorder.beginRecording(picture->cullRect().width(),
192 picture->cullRect().height(),
193 NULL, 0));
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000194 SkAutoTUnref<SkPicture> other(recorder.endRecording());
commit-bot@chromium.orgff36a1d2013-07-24 20:37:30 +0000195 }
196
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700197 SkDebugf("drawing... [%f %f %f %f] %s\n",
198 picture->cullRect().fLeft, picture->cullRect().fTop,
199 picture->cullRect().fRight, picture->cullRect().fBottom,
borenet@google.com03fcee82012-09-10 18:18:38 +0000200 inputPath.c_str());
skia.committer@gmail.com1d225f22012-09-14 02:01:10 +0000201
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000202 renderer.init(picture, &writePathString, &mismatchPathString, &inputFilename,
robertphillips78c71272014-10-09 04:59:19 -0700203 FLAGS_writeChecksumBasedFilenames, FLAGS_mpd);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000204
scroggo@google.comb4773b42012-10-01 20:06:09 +0000205 renderer.setup();
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000206 renderer.enableWrites();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000207
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000208 bool success = renderer.render(out);
209 if (!success) {
210 SkDebugf("Failed to render %s\n", inputFilename.c_str());
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000211 }
scroggo@google.com9a412522012-09-07 15:21:18 +0000212
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000213 renderer.end();
edisonn@google.com84f548c2012-12-18 22:24:03 +0000214
borenet@google.com66bcbd12012-09-17 18:26:06 +0000215 return success;
junov@chromium.org777442d2012-06-12 14:56:36 +0000216}
217
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000218static inline int getByte(uint32_t value, int index) {
219 SkASSERT(0 <= index && index < 4);
220 return (value >> (index * 8)) & 0xFF;
221}
222
223static int MaxByteDiff(uint32_t v1, uint32_t v2) {
bungeman60e0fee2015-08-26 05:15:46 -0700224 return SkMax32(SkMax32(SkTAbs(getByte(v1, 0) - getByte(v2, 0)), SkTAbs(getByte(v1, 1) - getByte(v2, 1))),
225 SkMax32(SkTAbs(getByte(v1, 2) - getByte(v2, 2)), SkTAbs(getByte(v1, 3) - getByte(v2, 3))));
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000226}
227
junov@chromium.orge286e842013-03-13 17:27:16 +0000228class AutoRestoreBbhType {
229public:
230 AutoRestoreBbhType() {
231 fRenderer = NULL;
junov@chromium.orgd34fda12013-03-13 19:03:26 +0000232 fSavedBbhType = sk_tools::PictureRenderer::kNone_BBoxHierarchyType;
junov@chromium.orge286e842013-03-13 17:27:16 +0000233 }
234
235 void set(sk_tools::PictureRenderer* renderer,
236 sk_tools::PictureRenderer::BBoxHierarchyType bbhType) {
237 fRenderer = renderer;
238 fSavedBbhType = renderer->getBBoxHierarchyType();
239 renderer->setBBoxHierarchyType(bbhType);
240 }
241
242 ~AutoRestoreBbhType() {
bsalomon49f085d2014-09-05 13:34:00 -0700243 if (fRenderer) {
junov@chromium.orge286e842013-03-13 17:27:16 +0000244 fRenderer->setBBoxHierarchyType(fSavedBbhType);
245 }
246 }
247
248private:
249 sk_tools::PictureRenderer* fRenderer;
250 sk_tools::PictureRenderer::BBoxHierarchyType fSavedBbhType;
251};
junov@chromium.orge286e842013-03-13 17:27:16 +0000252
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000253/**
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000254 * Render the SKP file(s) within inputPath.
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000255 *
256 * @param inputPath path to an individual SKP file, or a directory of SKP files
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000257 * @param writePath if not NULL, write all image(s) generated into this directory
258 * @param mismatchPath if not NULL, write any image(s) not matching expectations into this directory
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000259 * @param renderer PictureRenderer to use to render the SKPs
260 * @param jsonSummaryPtr if not NULL, add the image(s) generated to this summary
261 */
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000262static bool render_picture(const SkString& inputPath, const SkString* writePath,
263 const SkString* mismatchPath, sk_tools::PictureRenderer& renderer,
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000264 sk_tools::ImageResultsAndExpectations *jsonSummaryPtr) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000265 int diffs[256] = {0};
edisonn@google.com84f548c2012-12-18 22:24:03 +0000266 SkBitmap* bitmap = NULL;
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000267 renderer.setJsonSummaryPtr(jsonSummaryPtr);
268 bool success = render_picture_internal(inputPath,
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000269 FLAGS_writeWholeImage ? NULL : writePath,
270 FLAGS_writeWholeImage ? NULL : mismatchPath,
edisonn@google.com84f548c2012-12-18 22:24:03 +0000271 renderer,
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000272 FLAGS_validate || FLAGS_writeWholeImage ? &bitmap : NULL);
edisonn@google.com84f548c2012-12-18 22:24:03 +0000273
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000274 if (!success || ((FLAGS_validate || FLAGS_writeWholeImage) && bitmap == NULL)) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000275 SkDebugf("Failed to draw the picture.\n");
276 SkDELETE(bitmap);
277 return false;
278 }
279
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000280 if (FLAGS_validate) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000281 SkBitmap* referenceBitmap = NULL;
junov@chromium.orge286e842013-03-13 17:27:16 +0000282 sk_tools::PictureRenderer* referenceRenderer;
283 // If the renderer uses a BBoxHierarchy, then the reference renderer
skia.committer@gmail.com03682be2013-03-14 07:02:51 +0000284 // will be the same renderer, without the bbh.
junov@chromium.orge286e842013-03-13 17:27:16 +0000285 AutoRestoreBbhType arbbh;
286 if (sk_tools::PictureRenderer::kNone_BBoxHierarchyType !=
287 renderer.getBBoxHierarchyType()) {
288 referenceRenderer = &renderer;
289 referenceRenderer->ref(); // to match auto unref below
290 arbbh.set(referenceRenderer, sk_tools::PictureRenderer::kNone_BBoxHierarchyType);
291 } else {
krajcevskib1aded82014-08-18 07:52:17 -0700292#if SK_SUPPORT_GPU
293 referenceRenderer = SkNEW_ARGS(sk_tools::SimplePictureRenderer,
294 (renderer.getGrContextOptions()));
295#else
junov@chromium.orge286e842013-03-13 17:27:16 +0000296 referenceRenderer = SkNEW(sk_tools::SimplePictureRenderer);
krajcevskib1aded82014-08-18 07:52:17 -0700297#endif
junov@chromium.orge286e842013-03-13 17:27:16 +0000298 }
299 SkAutoTUnref<sk_tools::PictureRenderer> aurReferenceRenderer(referenceRenderer);
300
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000301 success = render_picture_internal(inputPath, NULL, NULL, *referenceRenderer,
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000302 &referenceBitmap);
edisonn@google.com84f548c2012-12-18 22:24:03 +0000303
junov@chromium.orgc19c1912013-03-12 19:56:49 +0000304 if (!success || NULL == referenceBitmap || NULL == referenceBitmap->getPixels()) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000305 SkDebugf("Failed to draw the reference picture.\n");
306 SkDELETE(bitmap);
307 SkDELETE(referenceBitmap);
308 return false;
309 }
310
311 if (success && (bitmap->width() != referenceBitmap->width())) {
312 SkDebugf("Expected image width: %i, actual image width %i.\n",
313 referenceBitmap->width(), bitmap->width());
314 SkDELETE(bitmap);
315 SkDELETE(referenceBitmap);
316 return false;
317 }
318 if (success && (bitmap->height() != referenceBitmap->height())) {
319 SkDebugf("Expected image height: %i, actual image height %i",
320 referenceBitmap->height(), bitmap->height());
321 SkDELETE(bitmap);
322 SkDELETE(referenceBitmap);
323 return false;
324 }
skia.committer@gmail.coma7d8e3e2012-12-19 02:01:38 +0000325
edisonn@google.com84f548c2012-12-18 22:24:03 +0000326 for (int y = 0; success && y < bitmap->height(); y++) {
327 for (int x = 0; success && x < bitmap->width(); x++) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000328 int diff = MaxByteDiff(*referenceBitmap->getAddr32(x, y),
329 *bitmap->getAddr32(x, y));
330 SkASSERT(diff >= 0 && diff <= 255);
331 diffs[diff]++;
skia.committer@gmail.com4d28d982013-01-17 07:06:06 +0000332
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000333 if (diff > FLAGS_maxComponentDiff) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000334 SkDebugf("Expected pixel at (%i %i) exceedds maximum "
335 "component diff of %i: 0x%x, actual 0x%x\n",
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000336 x, y, FLAGS_maxComponentDiff,
edisonn@google.com01754bf2013-01-11 16:08:07 +0000337 *referenceBitmap->getAddr32(x, y),
edisonn@google.com84f548c2012-12-18 22:24:03 +0000338 *bitmap->getAddr32(x, y));
339 SkDELETE(bitmap);
340 SkDELETE(referenceBitmap);
341 return false;
342 }
343 }
344 }
345 SkDELETE(referenceBitmap);
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000346
347 for (int i = 1; i <= 255; ++i) {
348 if(diffs[i] > 0) {
349 SkDebugf("Number of pixels with max diff of %i is %i\n", i, diffs[i]);
350 }
351 }
edisonn@google.com84f548c2012-12-18 22:24:03 +0000352 }
353
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000354 if (FLAGS_writeWholeImage) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000355 sk_tools::force_all_opaque(*bitmap);
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000356
tfarinaa8e2e152014-07-28 19:26:58 -0700357 SkString inputFilename = SkOSPath::Basename(inputPath.c_str());
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000358 SkString outputFilename(inputFilename);
359 sk_tools::replace_char(&outputFilename, '.', '_');
360 outputFilename.append(".png");
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000361
bsalomon49f085d2014-09-05 13:34:00 -0700362 if (jsonSummaryPtr) {
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000363 sk_tools::ImageDigest imageDigest(*bitmap);
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000364 jsonSummaryPtr->add(inputFilename.c_str(), outputFilename.c_str(), imageDigest);
bsalomon49f085d2014-09-05 13:34:00 -0700365 if ((mismatchPath) && !mismatchPath->isEmpty() &&
epoger85b438d2014-08-21 23:21:32 -0700366 !jsonSummaryPtr->getExpectation(inputFilename.c_str()).matches(imageDigest)) {
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000367 success &= sk_tools::write_bitmap_to_disk(*bitmap, *mismatchPath, NULL,
368 outputFilename);
369 }
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000370 }
371
bsalomon49f085d2014-09-05 13:34:00 -0700372 if ((writePath) && !writePath->isEmpty()) {
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000373 success &= sk_tools::write_bitmap_to_disk(*bitmap, *writePath, NULL, outputFilename);
edisonn@google.com84f548c2012-12-18 22:24:03 +0000374 }
375 }
376 SkDELETE(bitmap);
377
378 return success;
379}
380
381
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000382static int process_input(const char* input, const SkString* writePath,
383 const SkString* mismatchPath, sk_tools::PictureRenderer& renderer,
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000384 sk_tools::ImageResultsAndExpectations *jsonSummaryPtr) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000385 SkOSFile::Iter iter(input, "skp");
junov@chromium.org777442d2012-06-12 14:56:36 +0000386 SkString inputFilename;
borenet@google.com66bcbd12012-09-17 18:26:06 +0000387 int failures = 0;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000388 SkDebugf("process_input, %s\n", input);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000389 if (iter.next(&inputFilename)) {
390 do {
tfarinaa8e2e152014-07-28 19:26:58 -0700391 SkString inputPath = SkOSPath::Join(input, inputFilename.c_str());
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000392 if (!render_picture(inputPath, writePath, mismatchPath, renderer, jsonSummaryPtr)) {
borenet@google.com57837bf2012-09-19 17:28:29 +0000393 ++failures;
394 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000395 } while(iter.next(&inputFilename));
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000396 } else if (SkStrEndsWith(input, ".skp")) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000397 SkString inputPath(input);
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000398 if (!render_picture(inputPath, writePath, mismatchPath, renderer, jsonSummaryPtr)) {
borenet@google.com57837bf2012-09-19 17:28:29 +0000399 ++failures;
400 }
401 } else {
402 SkString warning;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000403 warning.printf("Warning: skipping %s\n", input);
kkinnunen297aaf92015-02-19 06:32:12 -0800404 SkDebugf("%s", warning.c_str());
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000405 }
borenet@google.com66bcbd12012-09-17 18:26:06 +0000406 return failures;
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000407}
408
caryclark@google.com5987f582012-10-02 18:33:14 +0000409int tool_main(int argc, char** argv);
410int tool_main(int argc, char** argv) {
scroggo@google.comd9ba9a02013-03-21 19:43:15 +0000411 SkCommandLineFlags::SetUsage("Render .skp files.");
412 SkCommandLineFlags::Parse(argc, argv);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000413
scroggo@google.com604e0c22013-04-09 21:25:46 +0000414 if (FLAGS_readPath.isEmpty()) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000415 SkDebugf(".skp files or directories are required.\n");
416 exit(-1);
417 }
418
419 if (FLAGS_maxComponentDiff < 0 || FLAGS_maxComponentDiff > 256) {
420 SkDebugf("--maxComponentDiff must be between 0 and 256\n");
421 exit(-1);
422 }
423
424 if (FLAGS_maxComponentDiff != 256 && !FLAGS_validate) {
425 SkDebugf("--maxComponentDiff requires --validate\n");
426 exit(-1);
427 }
428
scroggo@google.com1125d392013-05-03 20:43:37 +0000429 if (FLAGS_writeEncodedImages) {
430 if (FLAGS_writePath.isEmpty()) {
431 SkDebugf("--writeEncodedImages requires --writePath\n");
432 exit(-1);
433 }
434 if (FLAGS_deferImageDecoding) {
435 SkDebugf("--writeEncodedImages is not compatible with --deferImageDecoding\n");
436 exit(-1);
437 }
438 }
439
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000440 SkString errorString;
441 SkAutoTUnref<sk_tools::PictureRenderer> renderer(parseRenderer(errorString,
442 kRender_PictureTool));
443 if (errorString.size() > 0) {
444 SkDebugf("%s\n", errorString.c_str());
445 }
446
447 if (renderer.get() == NULL) {
448 exit(-1);
449 }
450
borenet@google.com66bcbd12012-09-17 18:26:06 +0000451 SkAutoGraphics ag;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000452
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000453 SkString writePath;
scroggo@google.com604e0c22013-04-09 21:25:46 +0000454 if (FLAGS_writePath.count() == 1) {
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000455 writePath.set(FLAGS_writePath[0]);
456 }
457 SkString mismatchPath;
458 if (FLAGS_mismatchPath.count() == 1) {
459 mismatchPath.set(FLAGS_mismatchPath[0]);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000460 }
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000461 sk_tools::ImageResultsAndExpectations jsonSummary;
462 sk_tools::ImageResultsAndExpectations* jsonSummaryPtr = NULL;
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000463 if (FLAGS_writeJsonSummaryPath.count() == 1) {
464 jsonSummaryPtr = &jsonSummary;
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000465 if (FLAGS_readJsonSummaryPath.count() == 1) {
466 SkASSERT(jsonSummary.readExpectationsFile(FLAGS_readJsonSummaryPath[0]));
467 }
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000468 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000469
borenet@google.com66bcbd12012-09-17 18:26:06 +0000470 int failures = 0;
scroggo@google.com604e0c22013-04-09 21:25:46 +0000471 for (int i = 0; i < FLAGS_readPath.count(); i ++) {
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000472 failures += process_input(FLAGS_readPath[i], &writePath, &mismatchPath, *renderer.get(),
473 jsonSummaryPtr);
junov@chromium.org777442d2012-06-12 14:56:36 +0000474 }
borenet@google.com66bcbd12012-09-17 18:26:06 +0000475 if (failures != 0) {
476 SkDebugf("Failed to render %i pictures.\n", failures);
477 return 1;
478 }
bsalomon24234fe2014-10-24 09:34:41 -0700479#if GR_CACHE_STATS && SK_SUPPORT_GPU
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000480 if (renderer->isUsingGpuDevice()) {
481 GrContext* ctx = renderer->getGrContext();
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000482 ctx->printCacheStats();
483 }
484#endif
bsalomon24234fe2014-10-24 09:34:41 -0700485
486#if GR_GPU_STATS && SK_SUPPORT_GPU
robertphillips754f4e92014-09-18 13:52:08 -0700487 if (FLAGS_gpuStats && renderer->isUsingGpuDevice()) {
mtkleinb9eb4ac2015-02-02 18:26:03 -0800488 renderer->getGrContext()->printGpuStats();
robertphillips754f4e92014-09-18 13:52:08 -0700489 }
490#endif
bsalomon24234fe2014-10-24 09:34:41 -0700491
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000492 if (FLAGS_writeJsonSummaryPath.count() == 1) {
epogerb492c6f2014-08-14 07:32:49 -0700493 // If there were any descriptions on the command line, insert them now.
494 for (int i=0; i<FLAGS_descriptions.count(); i++) {
495 SkTArray<SkString> tokens;
496 SkStrSplit(FLAGS_descriptions[i], "=", &tokens);
497 SkASSERT(tokens.count() == 2);
498 jsonSummary.addDescription(tokens[0].c_str(), tokens[1].c_str());
499 }
rmistry2529f2e2014-08-22 04:46:30 -0700500 if (FLAGS_imageBaseGSUrl.count() == 1) {
501 jsonSummary.setImageBaseGSUrl(FLAGS_imageBaseGSUrl[0]);
502 }
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000503 jsonSummary.writeToFile(FLAGS_writeJsonSummaryPath[0]);
504 }
caryclark@google.com868e1f62012-10-02 20:00:03 +0000505 return 0;
junov@chromium.org777442d2012-06-12 14:56:36 +0000506}
caryclark@google.com5987f582012-10-02 18:33:14 +0000507
508#if !defined SK_BUILD_FOR_IOS
509int main(int argc, char * const argv[]) {
510 return tool_main(argc, (char**) argv);
511}
512#endif