blob: d7a213cb47e74650db2885c7c75b96b3c04205a9 [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.");
robertphillips754f4e92014-09-18 13:52:08 -070039#if GR_GPU_STATS
40DEFINE_bool(gpuStats, false, "Only meaningful with gpu configurations. "
41 "Report some GPU call statistics.");
42#endif
epogerb492c6f2014-08-14 07:32:49 -070043DEFINE_bool(preprocess, false, "If true, perform device specific preprocessing before rendering.");
commit-bot@chromium.org205ce482014-05-12 15:37:20 +000044DEFINE_string(readJsonSummaryPath, "", "JSON file to read image expectations from.");
scroggo@google.com604e0c22013-04-09 21:25:46 +000045DECLARE_string(readPath);
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000046DEFINE_bool(writeChecksumBasedFilenames, false,
47 "When writing out images, use checksum-based filenames.");
scroggo@google.com1125d392013-05-03 20:43:37 +000048DEFINE_bool(writeEncodedImages, false, "Any time the skp contains an encoded image, write it to a "
49 "file rather than decoding it. Requires writePath to be set. Skips drawing the full "
50 "skp to a file. Not compatible with deferImageDecoding.");
commit-bot@chromium.org4610a462014-04-29 19:39:22 +000051DEFINE_string(writeJsonSummaryPath, "", "File to write a JSON summary of image results to.");
52DEFINE_string2(writePath, w, "", "Directory to write the rendered images into.");
scroggo@google.com161e1ba2013-03-04 16:41:06 +000053DEFINE_bool(writeWholeImage, false, "In tile mode, write the entire rendered image to a "
54 "file, instead of an image for each tile.");
55DEFINE_bool(validate, false, "Verify that the rendered image contains the same pixels as "
junov@chromium.orge286e842013-03-13 17:27:16 +000056 "the picture rendered in simple mode. When used in conjunction with --bbh, results "
57 "are validated against the picture rendered in the same mode, but without the bbh.");
junov@chromium.org777442d2012-06-12 14:56:36 +000058
scroggo@google.com1125d392013-05-03 20:43:37 +000059////////////////////////////////////////////////////////////////////////////////////////////////////
60
61/**
62 * Table for translating from format of data to a suffix.
63 */
64struct Format {
65 SkImageDecoder::Format fFormat;
66 const char* fSuffix;
67};
68static const Format gFormats[] = {
69 { SkImageDecoder::kBMP_Format, ".bmp" },
70 { SkImageDecoder::kGIF_Format, ".gif" },
71 { SkImageDecoder::kICO_Format, ".ico" },
72 { SkImageDecoder::kJPEG_Format, ".jpg" },
73 { SkImageDecoder::kPNG_Format, ".png" },
74 { SkImageDecoder::kWBMP_Format, ".wbmp" },
75 { SkImageDecoder::kWEBP_Format, ".webp" },
76 { SkImageDecoder::kUnknown_Format, "" },
77};
78
79/**
80 * Get an appropriate suffix for an image format.
81 */
82static const char* get_suffix_from_format(SkImageDecoder::Format format) {
83 for (size_t i = 0; i < SK_ARRAY_COUNT(gFormats); i++) {
84 if (gFormats[i].fFormat == format) {
85 return gFormats[i].fSuffix;
86 }
87 }
88 return "";
89}
90
91/**
92 * Base name for an image file created from the encoded data in an skp.
93 */
94static SkString gInputFileName;
95
96/**
97 * Number to be appended to the image file name so that it is unique.
98 */
99static uint32_t gImageNo;
100
101/**
102 * Set up the name for writing encoded data to a file.
103 * Sets gInputFileName to name, minus any extension ".*"
104 * Sets gImageNo to 0, so images from file "X.skp" will
105 * look like "X_<gImageNo>.<suffix>", beginning with 0
106 * for each new skp.
107 */
108static void reset_image_file_base_name(const SkString& name) {
109 gImageNo = 0;
110 // Remove ".skp"
111 const char* cName = name.c_str();
112 const char* dot = strrchr(cName, '.');
113 if (dot != NULL) {
114 gInputFileName.set(cName, dot - cName);
115 } else {
116 gInputFileName.set(name);
117 }
118}
119
120/**
121 * Write the raw encoded bitmap data to a file.
122 */
123static bool write_image_to_file(const void* buffer, size_t size, SkBitmap* bitmap) {
124 SkASSERT(!FLAGS_writePath.isEmpty());
125 SkMemoryStream memStream(buffer, size);
126 SkString outPath;
127 SkImageDecoder::Format format = SkImageDecoder::GetStreamFormat(&memStream);
128 SkString name = SkStringPrintf("%s_%d%s", gInputFileName.c_str(), gImageNo++,
129 get_suffix_from_format(format));
130 SkString dir(FLAGS_writePath[0]);
tfarinaa8e2e152014-07-28 19:26:58 -0700131 outPath = SkOSPath::Join(dir.c_str(), name.c_str());
scroggo@google.com1125d392013-05-03 20:43:37 +0000132 SkFILEWStream fileStream(outPath.c_str());
133 if (!(fileStream.isValid() && fileStream.write(buffer, size))) {
134 SkDebugf("Failed to write encoded data to \"%s\"\n", outPath.c_str());
135 }
136 // Put in a dummy bitmap.
reedbfefc7c2014-06-12 17:40:00 -0700137 return SkImageDecoder::DecodeStream(&memStream, bitmap, kUnknown_SkColorType,
scroggo@google.com1125d392013-05-03 20:43:37 +0000138 SkImageDecoder::kDecodeBounds_Mode);
139}
140
141////////////////////////////////////////////////////////////////////////////////////////////////////
142
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000143/**
144 * Called only by render_picture().
145 */
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000146static bool render_picture_internal(const SkString& inputPath, const SkString* writePath,
147 const SkString* mismatchPath,
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000148 sk_tools::PictureRenderer& renderer,
149 SkBitmap** out) {
tfarinaa8e2e152014-07-28 19:26:58 -0700150 SkString inputFilename = SkOSPath::Basename(inputPath.c_str());
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000151 SkString writePathString;
bsalomon49f085d2014-09-05 13:34:00 -0700152 if (writePath && writePath->size() > 0 && !FLAGS_writeEncodedImages) {
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000153 writePathString.set(*writePath);
154 }
155 SkString mismatchPathString;
bsalomon49f085d2014-09-05 13:34:00 -0700156 if (mismatchPath && mismatchPath->size() > 0) {
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000157 mismatchPathString.set(*mismatchPath);
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000158 }
twiz@google.coma31b8bb2012-06-22 18:24:56 +0000159
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000160 SkFILEStream inputStream;
twiz@google.coma31b8bb2012-06-22 18:24:56 +0000161 inputStream.setPath(inputPath.c_str());
162 if (!inputStream.isValid()) {
163 SkDebugf("Could not open file %s\n", inputPath.c_str());
borenet@google.com66bcbd12012-09-17 18:26:06 +0000164 return false;
twiz@google.coma31b8bb2012-06-22 18:24:56 +0000165 }
166
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000167 SkPicture::InstallPixelRefProc proc;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000168 if (FLAGS_deferImageDecoding) {
commit-bot@chromium.org56799e22013-07-16 18:21:46 +0000169 proc = &sk_tools::LazyDecodeBitmap;
scroggo@google.com1125d392013-05-03 20:43:37 +0000170 } else if (FLAGS_writeEncodedImages) {
171 SkASSERT(!FLAGS_writePath.isEmpty());
172 reset_image_file_base_name(inputFilename);
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000173 proc = &write_image_to_file;
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000174 } else {
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000175 proc = &SkImageDecoder::DecodeMemory;
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000176 }
scroggo@google.com1125d392013-05-03 20:43:37 +0000177
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000178 SkDebugf("deserializing... %s\n", inputPath.c_str());
179
robertphillipse9470752014-08-12 10:12:40 -0700180 SkAutoTUnref<SkPicture> picture(SkPicture::CreateFromStream(&inputStream, proc));
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000181
182 if (NULL == picture) {
borenet@google.com66bcbd12012-09-17 18:26:06 +0000183 SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str());
184 return false;
185 }
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000186
robertphillipse9470752014-08-12 10:12:40 -0700187 if (FLAGS_preprocess) {
188 // Because the GPU preprocessing step relies on the in-memory picture
189 // statistics we need to rerecord the picture here
190 SkPictureRecorder recorder;
robertphillipsc5ba71d2014-09-04 08:42:50 -0700191 picture->playback(recorder.beginRecording(picture->cullRect().width(),
192 picture->cullRect().height(),
193 NULL, 0));
robertphillipse9470752014-08-12 10:12:40 -0700194 picture.reset(recorder.endRecording());
195 }
196
commit-bot@chromium.orgff36a1d2013-07-24 20:37:30 +0000197 while (FLAGS_bench_record) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000198 SkPictureRecorder recorder;
robertphillipsc5ba71d2014-09-04 08:42:50 -0700199 picture->playback(recorder.beginRecording(picture->cullRect().width(),
200 picture->cullRect().height(),
201 NULL, 0));
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000202 SkAutoTUnref<SkPicture> other(recorder.endRecording());
commit-bot@chromium.orgff36a1d2013-07-24 20:37:30 +0000203 }
204
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700205 SkDebugf("drawing... [%f %f %f %f] %s\n",
206 picture->cullRect().fLeft, picture->cullRect().fTop,
207 picture->cullRect().fRight, picture->cullRect().fBottom,
borenet@google.com03fcee82012-09-10 18:18:38 +0000208 inputPath.c_str());
skia.committer@gmail.com1d225f22012-09-14 02:01:10 +0000209
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000210 renderer.init(picture, &writePathString, &mismatchPathString, &inputFilename,
211 FLAGS_writeChecksumBasedFilenames);
skia.committer@gmail.comeb849e52014-03-17 03:02:17 +0000212
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000213 if (FLAGS_preprocess) {
bsalomon49f085d2014-09-05 13:34:00 -0700214 if (renderer.getCanvas()) {
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000215 renderer.getCanvas()->EXPERIMENTAL_optimize(renderer.getPicture());
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000216 }
217 }
218
scroggo@google.comb4773b42012-10-01 20:06:09 +0000219 renderer.setup();
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000220 renderer.enableWrites();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000221
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000222 bool success = renderer.render(out);
223 if (!success) {
224 SkDebugf("Failed to render %s\n", inputFilename.c_str());
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000225 }
scroggo@google.com9a412522012-09-07 15:21:18 +0000226
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000227 renderer.end();
edisonn@google.com84f548c2012-12-18 22:24:03 +0000228
borenet@google.com66bcbd12012-09-17 18:26:06 +0000229 return success;
junov@chromium.org777442d2012-06-12 14:56:36 +0000230}
231
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000232static inline int getByte(uint32_t value, int index) {
233 SkASSERT(0 <= index && index < 4);
234 return (value >> (index * 8)) & 0xFF;
235}
236
237static int MaxByteDiff(uint32_t v1, uint32_t v2) {
238 return SkMax32(SkMax32(abs(getByte(v1, 0) - getByte(v2, 0)), abs(getByte(v1, 1) - getByte(v2, 1))),
239 SkMax32(abs(getByte(v1, 2) - getByte(v2, 2)), abs(getByte(v1, 3) - getByte(v2, 3))));
240}
241
junov@chromium.orge286e842013-03-13 17:27:16 +0000242class AutoRestoreBbhType {
243public:
244 AutoRestoreBbhType() {
245 fRenderer = NULL;
junov@chromium.orgd34fda12013-03-13 19:03:26 +0000246 fSavedBbhType = sk_tools::PictureRenderer::kNone_BBoxHierarchyType;
junov@chromium.orge286e842013-03-13 17:27:16 +0000247 }
248
249 void set(sk_tools::PictureRenderer* renderer,
250 sk_tools::PictureRenderer::BBoxHierarchyType bbhType) {
251 fRenderer = renderer;
252 fSavedBbhType = renderer->getBBoxHierarchyType();
253 renderer->setBBoxHierarchyType(bbhType);
254 }
255
256 ~AutoRestoreBbhType() {
bsalomon49f085d2014-09-05 13:34:00 -0700257 if (fRenderer) {
junov@chromium.orge286e842013-03-13 17:27:16 +0000258 fRenderer->setBBoxHierarchyType(fSavedBbhType);
259 }
260 }
261
262private:
263 sk_tools::PictureRenderer* fRenderer;
264 sk_tools::PictureRenderer::BBoxHierarchyType fSavedBbhType;
265};
junov@chromium.orge286e842013-03-13 17:27:16 +0000266
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000267/**
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000268 * Render the SKP file(s) within inputPath.
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000269 *
270 * @param inputPath path to an individual SKP file, or a directory of SKP files
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000271 * @param writePath if not NULL, write all image(s) generated into this directory
272 * @param mismatchPath if not NULL, write any image(s) not matching expectations into this directory
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000273 * @param renderer PictureRenderer to use to render the SKPs
274 * @param jsonSummaryPtr if not NULL, add the image(s) generated to this summary
275 */
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000276static bool render_picture(const SkString& inputPath, const SkString* writePath,
277 const SkString* mismatchPath, sk_tools::PictureRenderer& renderer,
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000278 sk_tools::ImageResultsAndExpectations *jsonSummaryPtr) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000279 int diffs[256] = {0};
edisonn@google.com84f548c2012-12-18 22:24:03 +0000280 SkBitmap* bitmap = NULL;
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000281 renderer.setJsonSummaryPtr(jsonSummaryPtr);
282 bool success = render_picture_internal(inputPath,
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000283 FLAGS_writeWholeImage ? NULL : writePath,
284 FLAGS_writeWholeImage ? NULL : mismatchPath,
edisonn@google.com84f548c2012-12-18 22:24:03 +0000285 renderer,
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000286 FLAGS_validate || FLAGS_writeWholeImage ? &bitmap : NULL);
edisonn@google.com84f548c2012-12-18 22:24:03 +0000287
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000288 if (!success || ((FLAGS_validate || FLAGS_writeWholeImage) && bitmap == NULL)) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000289 SkDebugf("Failed to draw the picture.\n");
290 SkDELETE(bitmap);
291 return false;
292 }
293
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000294 if (FLAGS_validate) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000295 SkBitmap* referenceBitmap = NULL;
junov@chromium.orge286e842013-03-13 17:27:16 +0000296 sk_tools::PictureRenderer* referenceRenderer;
297 // If the renderer uses a BBoxHierarchy, then the reference renderer
skia.committer@gmail.com03682be2013-03-14 07:02:51 +0000298 // will be the same renderer, without the bbh.
junov@chromium.orge286e842013-03-13 17:27:16 +0000299 AutoRestoreBbhType arbbh;
300 if (sk_tools::PictureRenderer::kNone_BBoxHierarchyType !=
301 renderer.getBBoxHierarchyType()) {
302 referenceRenderer = &renderer;
303 referenceRenderer->ref(); // to match auto unref below
304 arbbh.set(referenceRenderer, sk_tools::PictureRenderer::kNone_BBoxHierarchyType);
305 } else {
krajcevskib1aded82014-08-18 07:52:17 -0700306#if SK_SUPPORT_GPU
307 referenceRenderer = SkNEW_ARGS(sk_tools::SimplePictureRenderer,
308 (renderer.getGrContextOptions()));
309#else
junov@chromium.orge286e842013-03-13 17:27:16 +0000310 referenceRenderer = SkNEW(sk_tools::SimplePictureRenderer);
krajcevskib1aded82014-08-18 07:52:17 -0700311#endif
junov@chromium.orge286e842013-03-13 17:27:16 +0000312 }
313 SkAutoTUnref<sk_tools::PictureRenderer> aurReferenceRenderer(referenceRenderer);
314
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000315 success = render_picture_internal(inputPath, NULL, NULL, *referenceRenderer,
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000316 &referenceBitmap);
edisonn@google.com84f548c2012-12-18 22:24:03 +0000317
junov@chromium.orgc19c1912013-03-12 19:56:49 +0000318 if (!success || NULL == referenceBitmap || NULL == referenceBitmap->getPixels()) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000319 SkDebugf("Failed to draw the reference picture.\n");
320 SkDELETE(bitmap);
321 SkDELETE(referenceBitmap);
322 return false;
323 }
324
325 if (success && (bitmap->width() != referenceBitmap->width())) {
326 SkDebugf("Expected image width: %i, actual image width %i.\n",
327 referenceBitmap->width(), bitmap->width());
328 SkDELETE(bitmap);
329 SkDELETE(referenceBitmap);
330 return false;
331 }
332 if (success && (bitmap->height() != referenceBitmap->height())) {
333 SkDebugf("Expected image height: %i, actual image height %i",
334 referenceBitmap->height(), bitmap->height());
335 SkDELETE(bitmap);
336 SkDELETE(referenceBitmap);
337 return false;
338 }
skia.committer@gmail.coma7d8e3e2012-12-19 02:01:38 +0000339
edisonn@google.com84f548c2012-12-18 22:24:03 +0000340 for (int y = 0; success && y < bitmap->height(); y++) {
341 for (int x = 0; success && x < bitmap->width(); x++) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000342 int diff = MaxByteDiff(*referenceBitmap->getAddr32(x, y),
343 *bitmap->getAddr32(x, y));
344 SkASSERT(diff >= 0 && diff <= 255);
345 diffs[diff]++;
skia.committer@gmail.com4d28d982013-01-17 07:06:06 +0000346
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000347 if (diff > FLAGS_maxComponentDiff) {
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000348 SkDebugf("Expected pixel at (%i %i) exceedds maximum "
349 "component diff of %i: 0x%x, actual 0x%x\n",
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000350 x, y, FLAGS_maxComponentDiff,
edisonn@google.com01754bf2013-01-11 16:08:07 +0000351 *referenceBitmap->getAddr32(x, y),
edisonn@google.com84f548c2012-12-18 22:24:03 +0000352 *bitmap->getAddr32(x, y));
353 SkDELETE(bitmap);
354 SkDELETE(referenceBitmap);
355 return false;
356 }
357 }
358 }
359 SkDELETE(referenceBitmap);
edisonn@google.comca1b3ff2013-01-16 18:18:48 +0000360
361 for (int i = 1; i <= 255; ++i) {
362 if(diffs[i] > 0) {
363 SkDebugf("Number of pixels with max diff of %i is %i\n", i, diffs[i]);
364 }
365 }
edisonn@google.com84f548c2012-12-18 22:24:03 +0000366 }
367
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000368 if (FLAGS_writeWholeImage) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000369 sk_tools::force_all_opaque(*bitmap);
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000370
tfarinaa8e2e152014-07-28 19:26:58 -0700371 SkString inputFilename = SkOSPath::Basename(inputPath.c_str());
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000372 SkString outputFilename(inputFilename);
373 sk_tools::replace_char(&outputFilename, '.', '_');
374 outputFilename.append(".png");
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000375
bsalomon49f085d2014-09-05 13:34:00 -0700376 if (jsonSummaryPtr) {
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000377 sk_tools::ImageDigest imageDigest(*bitmap);
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000378 jsonSummaryPtr->add(inputFilename.c_str(), outputFilename.c_str(), imageDigest);
bsalomon49f085d2014-09-05 13:34:00 -0700379 if ((mismatchPath) && !mismatchPath->isEmpty() &&
epoger85b438d2014-08-21 23:21:32 -0700380 !jsonSummaryPtr->getExpectation(inputFilename.c_str()).matches(imageDigest)) {
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000381 success &= sk_tools::write_bitmap_to_disk(*bitmap, *mismatchPath, NULL,
382 outputFilename);
383 }
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000384 }
385
bsalomon49f085d2014-09-05 13:34:00 -0700386 if ((writePath) && !writePath->isEmpty()) {
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000387 success &= sk_tools::write_bitmap_to_disk(*bitmap, *writePath, NULL, outputFilename);
edisonn@google.com84f548c2012-12-18 22:24:03 +0000388 }
389 }
390 SkDELETE(bitmap);
391
392 return success;
393}
394
395
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000396static int process_input(const char* input, const SkString* writePath,
397 const SkString* mismatchPath, sk_tools::PictureRenderer& renderer,
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000398 sk_tools::ImageResultsAndExpectations *jsonSummaryPtr) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000399 SkOSFile::Iter iter(input, "skp");
junov@chromium.org777442d2012-06-12 14:56:36 +0000400 SkString inputFilename;
borenet@google.com66bcbd12012-09-17 18:26:06 +0000401 int failures = 0;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000402 SkDebugf("process_input, %s\n", input);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000403 if (iter.next(&inputFilename)) {
404 do {
tfarinaa8e2e152014-07-28 19:26:58 -0700405 SkString inputPath = SkOSPath::Join(input, inputFilename.c_str());
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000406 if (!render_picture(inputPath, writePath, mismatchPath, renderer, jsonSummaryPtr)) {
borenet@google.com57837bf2012-09-19 17:28:29 +0000407 ++failures;
408 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000409 } while(iter.next(&inputFilename));
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000410 } else if (SkStrEndsWith(input, ".skp")) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000411 SkString inputPath(input);
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000412 if (!render_picture(inputPath, writePath, mismatchPath, renderer, jsonSummaryPtr)) {
borenet@google.com57837bf2012-09-19 17:28:29 +0000413 ++failures;
414 }
415 } else {
416 SkString warning;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000417 warning.printf("Warning: skipping %s\n", input);
borenet@google.com57837bf2012-09-19 17:28:29 +0000418 SkDebugf(warning.c_str());
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000419 }
borenet@google.com66bcbd12012-09-17 18:26:06 +0000420 return failures;
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000421}
422
caryclark@google.com5987f582012-10-02 18:33:14 +0000423int tool_main(int argc, char** argv);
424int tool_main(int argc, char** argv) {
scroggo@google.comd9ba9a02013-03-21 19:43:15 +0000425 SkCommandLineFlags::SetUsage("Render .skp files.");
426 SkCommandLineFlags::Parse(argc, argv);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000427
scroggo@google.com604e0c22013-04-09 21:25:46 +0000428 if (FLAGS_readPath.isEmpty()) {
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000429 SkDebugf(".skp files or directories are required.\n");
430 exit(-1);
431 }
432
433 if (FLAGS_maxComponentDiff < 0 || FLAGS_maxComponentDiff > 256) {
434 SkDebugf("--maxComponentDiff must be between 0 and 256\n");
435 exit(-1);
436 }
437
438 if (FLAGS_maxComponentDiff != 256 && !FLAGS_validate) {
439 SkDebugf("--maxComponentDiff requires --validate\n");
440 exit(-1);
441 }
442
scroggo@google.com1125d392013-05-03 20:43:37 +0000443 if (FLAGS_writeEncodedImages) {
444 if (FLAGS_writePath.isEmpty()) {
445 SkDebugf("--writeEncodedImages requires --writePath\n");
446 exit(-1);
447 }
448 if (FLAGS_deferImageDecoding) {
449 SkDebugf("--writeEncodedImages is not compatible with --deferImageDecoding\n");
450 exit(-1);
451 }
452 }
453
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000454 SkString errorString;
455 SkAutoTUnref<sk_tools::PictureRenderer> renderer(parseRenderer(errorString,
456 kRender_PictureTool));
457 if (errorString.size() > 0) {
458 SkDebugf("%s\n", errorString.c_str());
459 }
460
461 if (renderer.get() == NULL) {
462 exit(-1);
463 }
464
borenet@google.com66bcbd12012-09-17 18:26:06 +0000465 SkAutoGraphics ag;
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000466
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000467 SkString writePath;
scroggo@google.com604e0c22013-04-09 21:25:46 +0000468 if (FLAGS_writePath.count() == 1) {
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000469 writePath.set(FLAGS_writePath[0]);
470 }
471 SkString mismatchPath;
472 if (FLAGS_mismatchPath.count() == 1) {
473 mismatchPath.set(FLAGS_mismatchPath[0]);
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000474 }
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000475 sk_tools::ImageResultsAndExpectations jsonSummary;
476 sk_tools::ImageResultsAndExpectations* jsonSummaryPtr = NULL;
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000477 if (FLAGS_writeJsonSummaryPath.count() == 1) {
478 jsonSummaryPtr = &jsonSummary;
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000479 if (FLAGS_readJsonSummaryPath.count() == 1) {
480 SkASSERT(jsonSummary.readExpectationsFile(FLAGS_readJsonSummaryPath[0]));
481 }
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000482 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000483
borenet@google.com66bcbd12012-09-17 18:26:06 +0000484 int failures = 0;
scroggo@google.com604e0c22013-04-09 21:25:46 +0000485 for (int i = 0; i < FLAGS_readPath.count(); i ++) {
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000486 failures += process_input(FLAGS_readPath[i], &writePath, &mismatchPath, *renderer.get(),
487 jsonSummaryPtr);
junov@chromium.org777442d2012-06-12 14:56:36 +0000488 }
borenet@google.com66bcbd12012-09-17 18:26:06 +0000489 if (failures != 0) {
490 SkDebugf("Failed to render %i pictures.\n", failures);
491 return 1;
492 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000493#if SK_SUPPORT_GPU
494#if GR_CACHE_STATS
495 if (renderer->isUsingGpuDevice()) {
496 GrContext* ctx = renderer->getGrContext();
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000497 ctx->printCacheStats();
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000498#ifdef SK_DEVELOPER
499 ctx->dumpFontCache();
500#endif
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000501 }
502#endif
robertphillips754f4e92014-09-18 13:52:08 -0700503#if GR_GPU_STATS
504 if (FLAGS_gpuStats && renderer->isUsingGpuDevice()) {
505 GrContext* ctx = renderer->getGrContext();
506 SkDebugf("RenderTarget Binds: %d\n", ctx->gpuStats()->renderTargetBinds());
507 SkDebugf("Shader Compilations: %d\n", ctx->gpuStats()->shaderCompilations());
508 }
509#endif
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000510#endif
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000511 if (FLAGS_writeJsonSummaryPath.count() == 1) {
epogerb492c6f2014-08-14 07:32:49 -0700512 // If there were any descriptions on the command line, insert them now.
513 for (int i=0; i<FLAGS_descriptions.count(); i++) {
514 SkTArray<SkString> tokens;
515 SkStrSplit(FLAGS_descriptions[i], "=", &tokens);
516 SkASSERT(tokens.count() == 2);
517 jsonSummary.addDescription(tokens[0].c_str(), tokens[1].c_str());
518 }
rmistry2529f2e2014-08-22 04:46:30 -0700519 if (FLAGS_imageBaseGSUrl.count() == 1) {
520 jsonSummary.setImageBaseGSUrl(FLAGS_imageBaseGSUrl[0]);
521 }
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000522 jsonSummary.writeToFile(FLAGS_writeJsonSummaryPath[0]);
523 }
caryclark@google.com868e1f62012-10-02 20:00:03 +0000524 return 0;
junov@chromium.org777442d2012-06-12 14:56:36 +0000525}
caryclark@google.com5987f582012-10-02 18:33:14 +0000526
527#if !defined SK_BUILD_FOR_IOS
528int main(int argc, char * const argv[]) {
529 return tool_main(argc, (char**) argv);
530}
531#endif