2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "LazyDecodeBitmap.h"
9 #include "CopyTilesRenderer.h"
12 #include "SkCommandLineFlags.h"
13 #include "SkGraphics.h"
14 #include "SkImageDecoder.h"
15 #include "SkImageEncoder.h"
18 #include "SkPicture.h"
19 #include "SkPictureRecorder.h"
23 #include "image_expectations.h"
24 #include "PictureRenderer.h"
25 #include "PictureRenderingFlags.h"
26 #include "picture_utils.h"
28 // Flags used by this file, alphabetically:
29 DECLARE_bool(deferImageDecoding);
30 DEFINE_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.");
33 DEFINE_string(mismatchPath, "", "Write images for tests that failed due to "
34 "pixel mismatches into this directory.");
35 DEFINE_string(readJsonSummaryPath, "", "JSON file to read image expectations from.");
36 DECLARE_string(readPath);
37 DEFINE_bool(writeChecksumBasedFilenames, false,
38 "When writing out images, use checksum-based filenames.");
39 DEFINE_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.");
42 DEFINE_string(writeJsonSummaryPath, "", "File to write a JSON summary of image results to.");
43 DEFINE_string2(writePath, w, "", "Directory to write the rendered images into.");
44 DEFINE_bool(writeWholeImage, false, "In tile mode, write the entire rendered image to a "
45 "file, instead of an image for each tile.");
46 DEFINE_bool(validate, false, "Verify that the rendered image contains the same pixels as "
47 "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.");
50 DEFINE_bool(bench_record, false, "If true, drop into an infinite loop of recording the picture.");
52 DEFINE_bool(preprocess, false, "If true, perform device specific preprocessing before rendering.");
54 ////////////////////////////////////////////////////////////////////////////////////////////////////
57 * Table for translating from format of data to a suffix.
60 SkImageDecoder::Format fFormat;
63 static 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, "" },
75 * Get an appropriate suffix for an image format.
77 static 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;
87 * Base name for an image file created from the encoded data in an skp.
89 static SkString gInputFileName;
92 * Number to be appended to the image file name so that it is unique.
94 static uint32_t gImageNo;
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
103 static void reset_image_file_base_name(const SkString& name) {
106 const char* cName = name.c_str();
107 const char* dot = strrchr(cName, '.');
109 gInputFileName.set(cName, dot - cName);
111 gInputFileName.set(name);
116 * Write the raw encoded bitmap data to a file.
118 static bool write_image_to_file(const void* buffer, size_t size, SkBitmap* bitmap) {
119 SkASSERT(!FLAGS_writePath.isEmpty());
120 SkMemoryStream memStream(buffer, size);
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]);
126 outPath = SkOSPath::SkPathJoin(dir.c_str(), name.c_str());
127 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());
131 // Put in a dummy bitmap.
132 return SkImageDecoder::DecodeStream(&memStream, bitmap, kUnknown_SkColorType,
133 SkImageDecoder::kDecodeBounds_Mode);
136 ////////////////////////////////////////////////////////////////////////////////////////////////////
139 * Called only by render_picture().
141 static bool render_picture_internal(const SkString& inputPath, const SkString* writePath,
142 const SkString* mismatchPath,
143 sk_tools::PictureRenderer& renderer,
145 SkString inputFilename = SkOSPath::SkBasename(inputPath.c_str());
146 SkString writePathString;
147 if (NULL != writePath && writePath->size() > 0 && !FLAGS_writeEncodedImages) {
148 writePathString.set(*writePath);
150 SkString mismatchPathString;
151 if (NULL != mismatchPath && mismatchPath->size() > 0) {
152 mismatchPathString.set(*mismatchPath);
155 SkFILEStream inputStream;
156 inputStream.setPath(inputPath.c_str());
157 if (!inputStream.isValid()) {
158 SkDebugf("Could not open file %s\n", inputPath.c_str());
162 SkPicture::InstallPixelRefProc proc;
163 if (FLAGS_deferImageDecoding) {
164 proc = &sk_tools::LazyDecodeBitmap;
165 } else if (FLAGS_writeEncodedImages) {
166 SkASSERT(!FLAGS_writePath.isEmpty());
167 reset_image_file_base_name(inputFilename);
168 proc = &write_image_to_file;
170 proc = &SkImageDecoder::DecodeMemory;
173 SkDebugf("deserializing... %s\n", inputPath.c_str());
175 SkPicture* picture = SkPicture::CreateFromStream(&inputStream, proc);
177 if (NULL == picture) {
178 SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str());
182 while (FLAGS_bench_record) {
183 SkPictureRecorder recorder;
184 picture->draw(recorder.beginRecording(picture->width(), picture->height(), NULL, 0));
185 SkAutoTUnref<SkPicture> other(recorder.endRecording());
188 SkDebugf("drawing... [%i %i] %s\n", picture->width(), picture->height(),
191 renderer.init(picture, &writePathString, &mismatchPathString, &inputFilename,
192 FLAGS_writeChecksumBasedFilenames);
194 if (FLAGS_preprocess) {
195 if (NULL != renderer.getCanvas()) {
196 renderer.getCanvas()->EXPERIMENTAL_optimize(renderer.getPicture());
201 renderer.enableWrites();
203 bool success = renderer.render(out);
205 SkDebugf("Failed to render %s\n", inputFilename.c_str());
214 static inline int getByte(uint32_t value, int index) {
215 SkASSERT(0 <= index && index < 4);
216 return (value >> (index * 8)) & 0xFF;
219 static int MaxByteDiff(uint32_t v1, uint32_t v2) {
220 return SkMax32(SkMax32(abs(getByte(v1, 0) - getByte(v2, 0)), abs(getByte(v1, 1) - getByte(v2, 1))),
221 SkMax32(abs(getByte(v1, 2) - getByte(v2, 2)), abs(getByte(v1, 3) - getByte(v2, 3))));
224 class AutoRestoreBbhType {
226 AutoRestoreBbhType() {
228 fSavedBbhType = sk_tools::PictureRenderer::kNone_BBoxHierarchyType;
231 void set(sk_tools::PictureRenderer* renderer,
232 sk_tools::PictureRenderer::BBoxHierarchyType bbhType) {
233 fRenderer = renderer;
234 fSavedBbhType = renderer->getBBoxHierarchyType();
235 renderer->setBBoxHierarchyType(bbhType);
238 ~AutoRestoreBbhType() {
239 if (NULL != fRenderer) {
240 fRenderer->setBBoxHierarchyType(fSavedBbhType);
245 sk_tools::PictureRenderer* fRenderer;
246 sk_tools::PictureRenderer::BBoxHierarchyType fSavedBbhType;
250 * Render the SKP file(s) within inputPath.
252 * @param inputPath path to an individual SKP file, or a directory of SKP files
253 * @param writePath if not NULL, write all image(s) generated into this directory
254 * @param mismatchPath if not NULL, write any image(s) not matching expectations into this directory
255 * @param renderer PictureRenderer to use to render the SKPs
256 * @param jsonSummaryPtr if not NULL, add the image(s) generated to this summary
258 static bool render_picture(const SkString& inputPath, const SkString* writePath,
259 const SkString* mismatchPath, sk_tools::PictureRenderer& renderer,
260 sk_tools::ImageResultsAndExpectations *jsonSummaryPtr) {
261 int diffs[256] = {0};
262 SkBitmap* bitmap = NULL;
263 renderer.setJsonSummaryPtr(jsonSummaryPtr);
264 bool success = render_picture_internal(inputPath,
265 FLAGS_writeWholeImage ? NULL : writePath,
266 FLAGS_writeWholeImage ? NULL : mismatchPath,
268 FLAGS_validate || FLAGS_writeWholeImage ? &bitmap : NULL);
270 if (!success || ((FLAGS_validate || FLAGS_writeWholeImage) && bitmap == NULL)) {
271 SkDebugf("Failed to draw the picture.\n");
276 if (FLAGS_validate) {
277 SkBitmap* referenceBitmap = NULL;
278 sk_tools::PictureRenderer* referenceRenderer;
279 // If the renderer uses a BBoxHierarchy, then the reference renderer
280 // will be the same renderer, without the bbh.
281 AutoRestoreBbhType arbbh;
282 if (sk_tools::PictureRenderer::kNone_BBoxHierarchyType !=
283 renderer.getBBoxHierarchyType()) {
284 referenceRenderer = &renderer;
285 referenceRenderer->ref(); // to match auto unref below
286 arbbh.set(referenceRenderer, sk_tools::PictureRenderer::kNone_BBoxHierarchyType);
288 referenceRenderer = SkNEW(sk_tools::SimplePictureRenderer);
290 SkAutoTUnref<sk_tools::PictureRenderer> aurReferenceRenderer(referenceRenderer);
292 success = render_picture_internal(inputPath, NULL, NULL, *referenceRenderer,
295 if (!success || NULL == referenceBitmap || NULL == referenceBitmap->getPixels()) {
296 SkDebugf("Failed to draw the reference picture.\n");
298 SkDELETE(referenceBitmap);
302 if (success && (bitmap->width() != referenceBitmap->width())) {
303 SkDebugf("Expected image width: %i, actual image width %i.\n",
304 referenceBitmap->width(), bitmap->width());
306 SkDELETE(referenceBitmap);
309 if (success && (bitmap->height() != referenceBitmap->height())) {
310 SkDebugf("Expected image height: %i, actual image height %i",
311 referenceBitmap->height(), bitmap->height());
313 SkDELETE(referenceBitmap);
317 for (int y = 0; success && y < bitmap->height(); y++) {
318 for (int x = 0; success && x < bitmap->width(); x++) {
319 int diff = MaxByteDiff(*referenceBitmap->getAddr32(x, y),
320 *bitmap->getAddr32(x, y));
321 SkASSERT(diff >= 0 && diff <= 255);
324 if (diff > FLAGS_maxComponentDiff) {
325 SkDebugf("Expected pixel at (%i %i) exceedds maximum "
326 "component diff of %i: 0x%x, actual 0x%x\n",
327 x, y, FLAGS_maxComponentDiff,
328 *referenceBitmap->getAddr32(x, y),
329 *bitmap->getAddr32(x, y));
331 SkDELETE(referenceBitmap);
336 SkDELETE(referenceBitmap);
338 for (int i = 1; i <= 255; ++i) {
340 SkDebugf("Number of pixels with max diff of %i is %i\n", i, diffs[i]);
345 if (FLAGS_writeWholeImage) {
346 sk_tools::force_all_opaque(*bitmap);
348 SkString inputFilename = SkOSPath::SkBasename(inputPath.c_str());
349 SkString outputFilename(inputFilename);
350 sk_tools::replace_char(&outputFilename, '.', '_');
351 outputFilename.append(".png");
353 if (NULL != jsonSummaryPtr) {
354 sk_tools::ImageDigest imageDigest(*bitmap);
355 jsonSummaryPtr->add(inputFilename.c_str(), outputFilename.c_str(), imageDigest);
356 if ((NULL != mismatchPath) && !mismatchPath->isEmpty() &&
357 !jsonSummaryPtr->matchesExpectation(inputFilename.c_str(), imageDigest)) {
358 success &= sk_tools::write_bitmap_to_disk(*bitmap, *mismatchPath, NULL,
363 if ((NULL != writePath) && !writePath->isEmpty()) {
364 success &= sk_tools::write_bitmap_to_disk(*bitmap, *writePath, NULL, outputFilename);
373 static int process_input(const char* input, const SkString* writePath,
374 const SkString* mismatchPath, sk_tools::PictureRenderer& renderer,
375 sk_tools::ImageResultsAndExpectations *jsonSummaryPtr) {
376 SkOSFile::Iter iter(input, "skp");
377 SkString inputFilename;
379 SkDebugf("process_input, %s\n", input);
380 if (iter.next(&inputFilename)) {
382 SkString inputPath = SkOSPath::SkPathJoin(input, inputFilename.c_str());
383 if (!render_picture(inputPath, writePath, mismatchPath, renderer, jsonSummaryPtr)) {
386 } while(iter.next(&inputFilename));
387 } else if (SkStrEndsWith(input, ".skp")) {
388 SkString inputPath(input);
389 if (!render_picture(inputPath, writePath, mismatchPath, renderer, jsonSummaryPtr)) {
394 warning.printf("Warning: skipping %s\n", input);
395 SkDebugf(warning.c_str());
400 int tool_main(int argc, char** argv);
401 int tool_main(int argc, char** argv) {
402 SkCommandLineFlags::SetUsage("Render .skp files.");
403 SkCommandLineFlags::Parse(argc, argv);
405 if (FLAGS_readPath.isEmpty()) {
406 SkDebugf(".skp files or directories are required.\n");
410 if (FLAGS_maxComponentDiff < 0 || FLAGS_maxComponentDiff > 256) {
411 SkDebugf("--maxComponentDiff must be between 0 and 256\n");
415 if (FLAGS_maxComponentDiff != 256 && !FLAGS_validate) {
416 SkDebugf("--maxComponentDiff requires --validate\n");
420 if (FLAGS_writeEncodedImages) {
421 if (FLAGS_writePath.isEmpty()) {
422 SkDebugf("--writeEncodedImages requires --writePath\n");
425 if (FLAGS_deferImageDecoding) {
426 SkDebugf("--writeEncodedImages is not compatible with --deferImageDecoding\n");
431 SkString errorString;
432 SkAutoTUnref<sk_tools::PictureRenderer> renderer(parseRenderer(errorString,
433 kRender_PictureTool));
434 if (errorString.size() > 0) {
435 SkDebugf("%s\n", errorString.c_str());
438 if (renderer.get() == NULL) {
445 if (FLAGS_writePath.count() == 1) {
446 writePath.set(FLAGS_writePath[0]);
448 SkString mismatchPath;
449 if (FLAGS_mismatchPath.count() == 1) {
450 mismatchPath.set(FLAGS_mismatchPath[0]);
452 sk_tools::ImageResultsAndExpectations jsonSummary;
453 sk_tools::ImageResultsAndExpectations* jsonSummaryPtr = NULL;
454 if (FLAGS_writeJsonSummaryPath.count() == 1) {
455 jsonSummaryPtr = &jsonSummary;
456 if (FLAGS_readJsonSummaryPath.count() == 1) {
457 SkASSERT(jsonSummary.readExpectationsFile(FLAGS_readJsonSummaryPath[0]));
462 for (int i = 0; i < FLAGS_readPath.count(); i ++) {
463 failures += process_input(FLAGS_readPath[i], &writePath, &mismatchPath, *renderer.get(),
467 SkDebugf("Failed to render %i pictures.\n", failures);
472 if (renderer->isUsingGpuDevice()) {
473 GrContext* ctx = renderer->getGrContext();
474 ctx->printCacheStats();
476 ctx->dumpFontCache();
481 if (FLAGS_writeJsonSummaryPath.count() == 1) {
482 jsonSummary.writeToFile(FLAGS_writeJsonSummaryPath[0]);
487 #if !defined SK_BUILD_FOR_IOS
488 int main(int argc, char * const argv[]) {
489 return tool_main(argc, (char**) argv);