Home | History | Annotate | Download | only in tools
      1 /*
      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 
      8 #include "LazyDecodeBitmap.h"
      9 #include "CopyTilesRenderer.h"
     10 #include "SkBitmap.h"
     11 #include "SkDevice.h"
     12 #include "SkCommandLineFlags.h"
     13 #include "SkGraphics.h"
     14 #include "SkImageDecoder.h"
     15 #include "SkImageEncoder.h"
     16 #include "SkMath.h"
     17 #include "SkOSFile.h"
     18 #include "SkPicture.h"
     19 #include "SkPictureRecorder.h"
     20 #include "SkStream.h"
     21 #include "SkString.h"
     22 
     23 #include "image_expectations.h"
     24 #include "PictureRenderer.h"
     25 #include "PictureRenderingFlags.h"
     26 #include "picture_utils.h"
     27 
     28 // Flags used by this file, alphabetically:
     29 DEFINE_int32(clone, 0, "Clone the picture n times before rendering.");
     30 DECLARE_bool(deferImageDecoding);
     31 DEFINE_int32(maxComponentDiff, 256, "Maximum diff on a component, 0 - 256. Components that differ "
     32              "by more than this amount are considered errors, though all diffs are reported. "
     33              "Requires --validate.");
     34 DEFINE_string(mismatchPath, "", "Write images for tests that failed due to "
     35               "pixel mismatches into this directory.");
     36 DEFINE_string(readJsonSummaryPath, "", "JSON file to read image expectations from.");
     37 DECLARE_string(readPath);
     38 DEFINE_bool(writeChecksumBasedFilenames, false,
     39             "When writing out images, use checksum-based filenames.");
     40 DEFINE_bool(writeEncodedImages, false, "Any time the skp contains an encoded image, write it to a "
     41             "file rather than decoding it. Requires writePath to be set. Skips drawing the full "
     42             "skp to a file. Not compatible with deferImageDecoding.");
     43 DEFINE_string(writeJsonSummaryPath, "", "File to write a JSON summary of image results to.");
     44 DEFINE_string2(writePath, w, "", "Directory to write the rendered images into.");
     45 DEFINE_bool(writeWholeImage, false, "In tile mode, write the entire rendered image to a "
     46             "file, instead of an image for each tile.");
     47 DEFINE_bool(validate, false, "Verify that the rendered image contains the same pixels as "
     48             "the picture rendered in simple mode. When used in conjunction with --bbh, results "
     49             "are validated against the picture rendered in the same mode, but without the bbh.");
     50 
     51 DEFINE_bool(bench_record, false, "If true, drop into an infinite loop of recording the picture.");
     52 
     53 DEFINE_bool(preprocess, false, "If true, perform device specific preprocessing before rendering.");
     54 
     55 ////////////////////////////////////////////////////////////////////////////////////////////////////
     56 
     57 /**
     58  *  Table for translating from format of data to a suffix.
     59  */
     60 struct Format {
     61     SkImageDecoder::Format  fFormat;
     62     const char*             fSuffix;
     63 };
     64 static 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  */
     78 static 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  */
     90 static SkString gInputFileName;
     91 
     92 /**
     93  *  Number to be appended to the image file name so that it is unique.
     94  */
     95 static 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  */
    104 static 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  */
    119 static 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]);
    127     outPath = SkOSPath::SkPathJoin(dir.c_str(), name.c_str());
    128     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.
    133     return SkImageDecoder::DecodeStream(&memStream, bitmap, kUnknown_SkColorType,
    134                                         SkImageDecoder::kDecodeBounds_Mode);
    135 }
    136 
    137 ////////////////////////////////////////////////////////////////////////////////////////////////////
    138 
    139 /**
    140  * Called only by render_picture().
    141  */
    142 static bool render_picture_internal(const SkString& inputPath, const SkString* writePath,
    143                                     const SkString* mismatchPath,
    144                                     sk_tools::PictureRenderer& renderer,
    145                                     SkBitmap** out) {
    146     SkString inputFilename = SkOSPath::SkBasename(inputPath.c_str());
    147     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);
    154     }
    155 
    156     SkFILEStream inputStream;
    157     inputStream.setPath(inputPath.c_str());
    158     if (!inputStream.isValid()) {
    159         SkDebugf("Could not open file %s\n", inputPath.c_str());
    160         return false;
    161     }
    162 
    163     SkPicture::InstallPixelRefProc proc;
    164     if (FLAGS_deferImageDecoding) {
    165         proc = &sk_tools::LazyDecodeBitmap;
    166     } else if (FLAGS_writeEncodedImages) {
    167         SkASSERT(!FLAGS_writePath.isEmpty());
    168         reset_image_file_base_name(inputFilename);
    169         proc = &write_image_to_file;
    170     } else {
    171         proc = &SkImageDecoder::DecodeMemory;
    172     }
    173 
    174     SkDebugf("deserializing... %s\n", inputPath.c_str());
    175 
    176     SkPicture* picture = SkPicture::CreateFromStream(&inputStream, proc);
    177 
    178     if (NULL == picture) {
    179         SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str());
    180         return false;
    181     }
    182 
    183     while (FLAGS_bench_record) {
    184         SkPictureRecorder recorder;
    185         picture->draw(recorder.beginRecording(picture->width(), picture->height(), NULL, 0));
    186         SkAutoTUnref<SkPicture> other(recorder.endRecording());
    187     }
    188 
    189     for (int i = 0; i < FLAGS_clone; ++i) {
    190         SkPicture* clone = picture->clone();
    191         SkDELETE(picture);
    192         picture = clone;
    193     }
    194 
    195     SkDebugf("drawing... [%i %i] %s\n", picture->width(), picture->height(),
    196              inputPath.c_str());
    197 
    198     renderer.init(picture, &writePathString, &mismatchPathString, &inputFilename,
    199                   FLAGS_writeChecksumBasedFilenames);
    200 
    201     if (FLAGS_preprocess) {
    202         if (NULL != renderer.getCanvas()) {
    203             renderer.getCanvas()->EXPERIMENTAL_optimize(renderer.getPicture());
    204         }
    205     }
    206 
    207     renderer.setup();
    208     renderer.enableWrites();
    209 
    210     bool success = renderer.render(out);
    211     if (!success) {
    212         SkDebugf("Failed to render %s\n", inputFilename.c_str());
    213     }
    214 
    215     renderer.end();
    216 
    217     SkDELETE(picture);
    218     return success;
    219 }
    220 
    221 static inline int getByte(uint32_t value, int index) {
    222     SkASSERT(0 <= index && index < 4);
    223     return (value >> (index * 8)) & 0xFF;
    224 }
    225 
    226 static int MaxByteDiff(uint32_t v1, uint32_t v2) {
    227     return SkMax32(SkMax32(abs(getByte(v1, 0) - getByte(v2, 0)), abs(getByte(v1, 1) - getByte(v2, 1))),
    228                    SkMax32(abs(getByte(v1, 2) - getByte(v2, 2)), abs(getByte(v1, 3) - getByte(v2, 3))));
    229 }
    230 
    231 class AutoRestoreBbhType {
    232 public:
    233     AutoRestoreBbhType() {
    234         fRenderer = NULL;
    235         fSavedBbhType = sk_tools::PictureRenderer::kNone_BBoxHierarchyType;
    236     }
    237 
    238     void set(sk_tools::PictureRenderer* renderer,
    239              sk_tools::PictureRenderer::BBoxHierarchyType bbhType) {
    240         fRenderer = renderer;
    241         fSavedBbhType = renderer->getBBoxHierarchyType();
    242         renderer->setBBoxHierarchyType(bbhType);
    243     }
    244 
    245     ~AutoRestoreBbhType() {
    246         if (NULL != fRenderer) {
    247             fRenderer->setBBoxHierarchyType(fSavedBbhType);
    248         }
    249     }
    250 
    251 private:
    252     sk_tools::PictureRenderer* fRenderer;
    253     sk_tools::PictureRenderer::BBoxHierarchyType fSavedBbhType;
    254 };
    255 
    256 /**
    257  * Render the SKP file(s) within inputPath.
    258  *
    259  * @param inputPath path to an individual SKP file, or a directory of SKP files
    260  * @param writePath if not NULL, write all image(s) generated into this directory
    261  * @param mismatchPath if not NULL, write any image(s) not matching expectations into this directory
    262  * @param renderer PictureRenderer to use to render the SKPs
    263  * @param jsonSummaryPtr if not NULL, add the image(s) generated to this summary
    264  */
    265 static bool render_picture(const SkString& inputPath, const SkString* writePath,
    266                            const SkString* mismatchPath, sk_tools::PictureRenderer& renderer,
    267                            sk_tools::ImageResultsAndExpectations *jsonSummaryPtr) {
    268     int diffs[256] = {0};
    269     SkBitmap* bitmap = NULL;
    270     renderer.setJsonSummaryPtr(jsonSummaryPtr);
    271     bool success = render_picture_internal(inputPath,
    272         FLAGS_writeWholeImage ? NULL : writePath,
    273         FLAGS_writeWholeImage ? NULL : mismatchPath,
    274         renderer,
    275         FLAGS_validate || FLAGS_writeWholeImage ? &bitmap : NULL);
    276 
    277     if (!success || ((FLAGS_validate || FLAGS_writeWholeImage) && bitmap == NULL)) {
    278         SkDebugf("Failed to draw the picture.\n");
    279         SkDELETE(bitmap);
    280         return false;
    281     }
    282 
    283     if (FLAGS_validate) {
    284         SkBitmap* referenceBitmap = NULL;
    285         sk_tools::PictureRenderer* referenceRenderer;
    286         // If the renderer uses a BBoxHierarchy, then the reference renderer
    287         // will be the same renderer, without the bbh.
    288         AutoRestoreBbhType arbbh;
    289         if (sk_tools::PictureRenderer::kNone_BBoxHierarchyType !=
    290             renderer.getBBoxHierarchyType()) {
    291             referenceRenderer = &renderer;
    292             referenceRenderer->ref();  // to match auto unref below
    293             arbbh.set(referenceRenderer, sk_tools::PictureRenderer::kNone_BBoxHierarchyType);
    294         } else {
    295             referenceRenderer = SkNEW(sk_tools::SimplePictureRenderer);
    296         }
    297         SkAutoTUnref<sk_tools::PictureRenderer> aurReferenceRenderer(referenceRenderer);
    298 
    299         success = render_picture_internal(inputPath, NULL, NULL, *referenceRenderer,
    300                                           &referenceBitmap);
    301 
    302         if (!success || NULL == referenceBitmap || NULL == referenceBitmap->getPixels()) {
    303             SkDebugf("Failed to draw the reference picture.\n");
    304             SkDELETE(bitmap);
    305             SkDELETE(referenceBitmap);
    306             return false;
    307         }
    308 
    309         if (success && (bitmap->width() != referenceBitmap->width())) {
    310             SkDebugf("Expected image width: %i, actual image width %i.\n",
    311                      referenceBitmap->width(), bitmap->width());
    312             SkDELETE(bitmap);
    313             SkDELETE(referenceBitmap);
    314             return false;
    315         }
    316         if (success && (bitmap->height() != referenceBitmap->height())) {
    317             SkDebugf("Expected image height: %i, actual image height %i",
    318                      referenceBitmap->height(), bitmap->height());
    319             SkDELETE(bitmap);
    320             SkDELETE(referenceBitmap);
    321             return false;
    322         }
    323 
    324         for (int y = 0; success && y < bitmap->height(); y++) {
    325             for (int x = 0; success && x < bitmap->width(); x++) {
    326                 int diff = MaxByteDiff(*referenceBitmap->getAddr32(x, y),
    327                                        *bitmap->getAddr32(x, y));
    328                 SkASSERT(diff >= 0 && diff <= 255);
    329                 diffs[diff]++;
    330 
    331                 if (diff > FLAGS_maxComponentDiff) {
    332                     SkDebugf("Expected pixel at (%i %i) exceedds maximum "
    333                                  "component diff of %i: 0x%x, actual 0x%x\n",
    334                              x, y, FLAGS_maxComponentDiff,
    335                              *referenceBitmap->getAddr32(x, y),
    336                              *bitmap->getAddr32(x, y));
    337                     SkDELETE(bitmap);
    338                     SkDELETE(referenceBitmap);
    339                     return false;
    340                 }
    341             }
    342         }
    343         SkDELETE(referenceBitmap);
    344 
    345         for (int i = 1; i <= 255; ++i) {
    346             if(diffs[i] > 0) {
    347                 SkDebugf("Number of pixels with max diff of %i is %i\n", i, diffs[i]);
    348             }
    349         }
    350     }
    351 
    352     if (FLAGS_writeWholeImage) {
    353         sk_tools::force_all_opaque(*bitmap);
    354 
    355         SkString inputFilename = SkOSPath::SkBasename(inputPath.c_str());
    356         SkString outputFilename(inputFilename);
    357         sk_tools::replace_char(&outputFilename, '.', '_');
    358         outputFilename.append(".png");
    359 
    360         if (NULL != jsonSummaryPtr) {
    361             sk_tools::ImageDigest imageDigest(*bitmap);
    362             jsonSummaryPtr->add(inputFilename.c_str(), outputFilename.c_str(), imageDigest);
    363             if ((NULL != mismatchPath) && !mismatchPath->isEmpty() &&
    364                 !jsonSummaryPtr->matchesExpectation(inputFilename.c_str(), imageDigest)) {
    365                 success &= sk_tools::write_bitmap_to_disk(*bitmap, *mismatchPath, NULL,
    366                                                           outputFilename);
    367             }
    368         }
    369 
    370         if ((NULL != writePath) && !writePath->isEmpty()) {
    371             success &= sk_tools::write_bitmap_to_disk(*bitmap, *writePath, NULL, outputFilename);
    372         }
    373     }
    374     SkDELETE(bitmap);
    375 
    376     return success;
    377 }
    378 
    379 
    380 static int process_input(const char* input, const SkString* writePath,
    381                          const SkString* mismatchPath, sk_tools::PictureRenderer& renderer,
    382                          sk_tools::ImageResultsAndExpectations *jsonSummaryPtr) {
    383     SkOSFile::Iter iter(input, "skp");
    384     SkString inputFilename;
    385     int failures = 0;
    386     SkDebugf("process_input, %s\n", input);
    387     if (iter.next(&inputFilename)) {
    388         do {
    389             SkString inputPath = SkOSPath::SkPathJoin(input, inputFilename.c_str());
    390             if (!render_picture(inputPath, writePath, mismatchPath, renderer, jsonSummaryPtr)) {
    391                 ++failures;
    392             }
    393         } while(iter.next(&inputFilename));
    394     } else if (SkStrEndsWith(input, ".skp")) {
    395         SkString inputPath(input);
    396         if (!render_picture(inputPath, writePath, mismatchPath, renderer, jsonSummaryPtr)) {
    397             ++failures;
    398         }
    399     } else {
    400         SkString warning;
    401         warning.printf("Warning: skipping %s\n", input);
    402         SkDebugf(warning.c_str());
    403     }
    404     return failures;
    405 }
    406 
    407 int tool_main(int argc, char** argv);
    408 int tool_main(int argc, char** argv) {
    409     SkCommandLineFlags::SetUsage("Render .skp files.");
    410     SkCommandLineFlags::Parse(argc, argv);
    411 
    412     if (FLAGS_readPath.isEmpty()) {
    413         SkDebugf(".skp files or directories are required.\n");
    414         exit(-1);
    415     }
    416 
    417     if (FLAGS_maxComponentDiff < 0 || FLAGS_maxComponentDiff > 256) {
    418         SkDebugf("--maxComponentDiff must be between 0 and 256\n");
    419         exit(-1);
    420     }
    421 
    422     if (FLAGS_maxComponentDiff != 256 && !FLAGS_validate) {
    423         SkDebugf("--maxComponentDiff requires --validate\n");
    424         exit(-1);
    425     }
    426 
    427     if (FLAGS_clone < 0) {
    428         SkDebugf("--clone must be >= 0. Was %i\n", FLAGS_clone);
    429         exit(-1);
    430     }
    431 
    432     if (FLAGS_writeEncodedImages) {
    433         if (FLAGS_writePath.isEmpty()) {
    434             SkDebugf("--writeEncodedImages requires --writePath\n");
    435             exit(-1);
    436         }
    437         if (FLAGS_deferImageDecoding) {
    438             SkDebugf("--writeEncodedImages is not compatible with --deferImageDecoding\n");
    439             exit(-1);
    440         }
    441     }
    442 
    443     SkString errorString;
    444     SkAutoTUnref<sk_tools::PictureRenderer> renderer(parseRenderer(errorString,
    445                                                                    kRender_PictureTool));
    446     if (errorString.size() > 0) {
    447         SkDebugf("%s\n", errorString.c_str());
    448     }
    449 
    450     if (renderer.get() == NULL) {
    451         exit(-1);
    452     }
    453 
    454     SkAutoGraphics ag;
    455 
    456     SkString writePath;
    457     if (FLAGS_writePath.count() == 1) {
    458         writePath.set(FLAGS_writePath[0]);
    459     }
    460     SkString mismatchPath;
    461     if (FLAGS_mismatchPath.count() == 1) {
    462         mismatchPath.set(FLAGS_mismatchPath[0]);
    463     }
    464     sk_tools::ImageResultsAndExpectations jsonSummary;
    465     sk_tools::ImageResultsAndExpectations* jsonSummaryPtr = NULL;
    466     if (FLAGS_writeJsonSummaryPath.count() == 1) {
    467         jsonSummaryPtr = &jsonSummary;
    468         if (FLAGS_readJsonSummaryPath.count() == 1) {
    469             SkASSERT(jsonSummary.readExpectationsFile(FLAGS_readJsonSummaryPath[0]));
    470         }
    471     }
    472 
    473     int failures = 0;
    474     for (int i = 0; i < FLAGS_readPath.count(); i ++) {
    475         failures += process_input(FLAGS_readPath[i], &writePath, &mismatchPath, *renderer.get(),
    476                                   jsonSummaryPtr);
    477     }
    478     if (failures != 0) {
    479         SkDebugf("Failed to render %i pictures.\n", failures);
    480         return 1;
    481     }
    482 #if SK_SUPPORT_GPU
    483 #if GR_CACHE_STATS
    484     if (renderer->isUsingGpuDevice()) {
    485         GrContext* ctx = renderer->getGrContext();
    486         ctx->printCacheStats();
    487 #ifdef SK_DEVELOPER
    488         ctx->dumpFontCache();
    489 #endif
    490     }
    491 #endif
    492 #endif
    493     if (FLAGS_writeJsonSummaryPath.count() == 1) {
    494         jsonSummary.writeToFile(FLAGS_writeJsonSummaryPath[0]);
    495     }
    496     return 0;
    497 }
    498 
    499 #if !defined SK_BUILD_FOR_IOS
    500 int main(int argc, char * const argv[]) {
    501     return tool_main(argc, (char**) argv);
    502 }
    503 #endif
    504