Created my own flag parser, based off of gflags.

Share common code between bench_ and render_ to set up the PictureRenderer.
Fix an include error in SkPictureRenderer.h.
Simplified parameter passing in render_pictures_main.
Switch to using an SkAutoTUnref for the PictureRenderer.
I also changed the input format somewhat, so the buildbots need to be updated as well: https://codereview.appspot.com/7441044/
Fixed a bug in PictureBenchmark where calling setTimeIndividualTiles(false) sets the member variable to true.
Removed setDeviceType from PictureBenchmark, since only the PictureRenderer needs to know which device type to use.

Some changes to the input format:
'--logPerIter' no longer takes a 1 or 0. Instead, '--logPerIter'
turns it on and '--nologPerIter' turns it off (with off as the
default). (Note that this is for bench_pictures; bench still uses the old format)

Change '--device' to '--config' and 'bitmap' to '8888' to be
the same as gm.

Requires '--r' before inputs (to match gm), though there can be multiple inputs following it.

Changed --enable-deferred-image-decoding (which no one uses but me yet anyway) to --deferImageDecoding, since the former is incompatible with the flag parser.

Changes to behavior:
Show a short error message on failure (rather than the explanation of all
flags).

BUG=https://code.google.com/p/skia/issues/detail?id=1094

Review URL: https://codereview.appspot.com/7230053

git-svn-id: http://skia.googlecode.com/svn/trunk@7961 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/tools/render_pictures_main.cpp b/tools/render_pictures_main.cpp
index 146b045..5d64e74 100644
--- a/tools/render_pictures_main.cpp
+++ b/tools/render_pictures_main.cpp
@@ -10,6 +10,7 @@
 #include "SkBitmapFactory.h"
 #include "SkCanvas.h"
 #include "SkDevice.h"
+#include "SkFlags.h"
 #include "SkGraphics.h"
 #include "SkImageDecoder.h"
 #include "SkImageEncoder.h"
@@ -20,109 +21,21 @@
 #include "SkString.h"
 #include "SkTArray.h"
 #include "PictureRenderer.h"
+#include "PictureRenderingFlags.h"
 #include "picture_utils.h"
 
-static void usage(const char* argv0) {
-    SkDebugf("SkPicture rendering tool\n");
-    SkDebugf("\n"
-"Usage: \n"
-"     %s <input>... \n"
-"     [-w <outputDir>]\n"
-"     [--mode pow2tile minWidth height | copyTile width height | simple\n"
-"         | tile width height]\n"
-"     [--pipe]\n"
-"     [--bbh bbhType]\n"
-"     [--multi count]\n"
-"     [--validate [--maxComponentDiff n]]\n"
-"     [--writeWholeImage]\n"
-"     [--clone n]\n"
-"     [--enable-deferred-image-decoding]\n"
-"     [--viewport width height][--scale sf]\n"
-"     [--device bitmap"
-#if SK_SUPPORT_GPU
-" | gpu"
-#endif
-#if SK_ANGLE
-" | angle"
-#endif
-"]"
-, argv0);
-    SkDebugf("\n\n");
-    SkDebugf(
-"     input:     A list of directories and files to use as input. Files are\n"
-"                expected to have the .skp extension.\n\n");
-    SkDebugf(
-"     outputDir: directory to write the rendered images.\n\n");
-    SkDebugf(
-"     --mode pow2tile minWidth height | copyTile width height | simple\n"
-"          | tile width height | rerecord: Run in the corresponding mode.\n"
-"                                     Default is simple.\n");
-    SkDebugf(
-"                     pow2tile minWidth height, Creates tiles with widths\n"
-"                                                  that are all a power of two\n"
-"                                                  such that they minimize the\n"
-"                                                  amount of wasted tile space.\n"
-"                                                  minWidth is the minimum width\n"
-"                                                  of these tiles and must be a\n"
-"                                                  power of two. A simple render\n"
-"                                                  is done with these tiles.\n");
-    SkDebugf(
-"                     simple, Render using the default rendering method.\n"
-"                     rerecord, Record the picture as a new skp, with the bitmaps PNG encoded.\n"
-             );
-    SkDebugf(
-"                     tile width height, Do a simple render using tiles\n"
-"                                              with the given dimensions.\n"
-"                     copyTile width height, Draw the picture, then copy it into tiles.\n"
-"                                                Does not support percentages.\n"
-"                                                If the picture is large enough, breaks it into\n"
-"                                                larger tiles (and draws the picture once per\n"
-"                                                larger tile) to avoid creating a large canvas.\n"
-"                                                Add --tiles x y to specify the number of tiles\n"
-"                                                per larger tile in the x and y direction.\n"
-             );
-    SkDebugf("\n");
-    SkDebugf(
-"     --multi count : Set the number of threads for multi threaded drawing. Must be greater\n"
-"                     than 1. Only works with tiled rendering.\n"
-"     --enable-deferred-image-decoding : Defer decoding until drawing images. Has no effect if\n"
-"                      the provided skp does not have its images encoded.\n"
-"     --viewport width height : Set the viewport.\n"
-"     --scale sf : Scale drawing by sf.\n"
-"     --pipe: Benchmark SkGPipe rendering. Currently incompatible with \"mode\".\n");
-    SkDebugf(
-"     --validate: Verify that the rendered image contains the same pixels as "
-"the picture rendered in simple mode.\n"
-"     --maxComponentDiff: maximum diff on a component. Default is 256, "
-"which means we report but we do not generate an error.\n"
-"     --writeWholeImage: In tile mode, write the entire rendered image to a "
-"file, instead of an image for each tile.\n");
-    SkDebugf(
-"     --clone n: Clone the picture n times before rendering.\n");
-    SkDebugf(
-"     --bbh bbhType [width height]: Set the bounding box hierarchy type to\n"
-"                     be used. Accepted values are: none, rtree, grid. Default\n"
-"                     value is none. Not compatible with --pipe. With value\n"
-"                     'grid', width and height must be specified. 'grid' can\n"
-"                     only be used with modes tile, record, and\n"
-"                     playbackCreation.");
-    SkDebugf(
-"     --device bitmap"
-#if SK_SUPPORT_GPU
-" | gpu"
-#endif
-": Use the corresponding device. Default is bitmap.\n");
-    SkDebugf(
-"                     bitmap, Render to a bitmap.\n");
-#if SK_SUPPORT_GPU
-    SkDebugf(
-"                     gpu, Render to the GPU.\n");
-#endif
-#if SK_ANGLE
-    SkDebugf(
-"                     angle, Render using angle.\n");
-#endif
-}
+// Flags used by this file, alphabetically:
+DEFINE_int32(clone, 0, "Clone the picture n times before rendering.");
+DECLARE_bool(deferImageDecoding);
+DEFINE_int32(maxComponentDiff, 256, "Maximum diff on a component, 0 - 256. Components that differ "
+             "by more than this amount are considered errors, though all diffs are reported. "
+             "Requires --validate.");
+DECLARE_string(r);
+DEFINE_string(w, "", "Directory to write the rendered images.");
+DEFINE_bool(writeWholeImage, false, "In tile mode, write the entire rendered image to a "
+            "file, instead of an image for each tile.");
+DEFINE_bool(validate, false, "Verify that the rendered image contains the same pixels as "
+            "the picture rendered in simple mode.");
 
 static void make_output_filepath(SkString* path, const SkString& dir,
                                  const SkString& name) {
@@ -131,8 +44,6 @@
     path->remove(path->size() - 4, 4);
 }
 
-bool lazy_decode = false;
-
 #include "SkData.h"
 #include "SkLruImageCache.h"
 
@@ -166,8 +77,7 @@
 
 static bool render_picture(const SkString& inputPath, const SkString* outputDir,
                            sk_tools::PictureRenderer& renderer,
-                           SkBitmap** out,
-                           int clones) {
+                           SkBitmap** out) {
     SkString inputFilename;
     sk_tools::get_basename(&inputFilename, inputPath);
 
@@ -180,7 +90,7 @@
 
     bool success = false;
     SkPicture* picture;
-    if (lazy_decode) {
+    if (FLAGS_deferImageDecoding) {
         picture = SkNEW_ARGS(SkPicture, (&inputStream, &success, &lazy_decode_bitmap));
     } else {
         picture = SkNEW_ARGS(SkPicture, (&inputStream, &success, &SkImageDecoder::DecodeMemory));
@@ -190,7 +100,7 @@
         return false;
     }
 
-    for (int i = 0; i < clones; ++i) {
+    for (int i = 0; i < FLAGS_clone; ++i) {
         SkPicture* clone = picture->clone();
         SkDELETE(picture);
         picture = clone;
@@ -203,7 +113,7 @@
     renderer.setup();
 
     SkString* outputPath = NULL;
-    if (NULL != outputDir) {
+    if (NULL != outputDir && outputDir->size() > 0) {
         outputPath = SkNEW(SkString);
         make_output_filepath(outputPath, *outputDir, inputFilename);
     }
@@ -233,28 +143,25 @@
 }
 
 static bool render_picture(const SkString& inputPath, const SkString* outputDir,
-                           sk_tools::PictureRenderer& renderer,
-                           bool validate, int maxComponentDiff,
-                           bool writeWholeImage,
-                           int clones) {
+                           sk_tools::PictureRenderer& renderer) {
     int diffs[256] = {0};
     SkBitmap* bitmap = NULL;
     bool success = render_picture(inputPath,
-        writeWholeImage ? NULL : outputDir,
+        FLAGS_writeWholeImage ? NULL : outputDir,
         renderer,
-        validate || writeWholeImage ? &bitmap : NULL, clones);
+        FLAGS_validate || FLAGS_writeWholeImage ? &bitmap : NULL);
 
-    if (!success || ((validate || writeWholeImage) && bitmap == NULL)) {
+    if (!success || ((FLAGS_validate || FLAGS_writeWholeImage) && bitmap == NULL)) {
         SkDebugf("Failed to draw the picture.\n");
         SkDELETE(bitmap);
         return false;
     }
 
-    if (validate) {
+    if (FLAGS_validate) {
         SkBitmap* referenceBitmap = NULL;
         sk_tools::SimplePictureRenderer referenceRenderer;
         success = render_picture(inputPath, NULL, referenceRenderer,
-                                 &referenceBitmap, 0);
+                                 &referenceBitmap);
 
         if (!success || !referenceBitmap) {
             SkDebugf("Failed to draw the reference picture.\n");
@@ -285,10 +192,10 @@
                 SkASSERT(diff >= 0 && diff <= 255);
                 diffs[diff]++;
 
-                if (diff > maxComponentDiff) {
+                if (diff > FLAGS_maxComponentDiff) {
                     SkDebugf("Expected pixel at (%i %i) exceedds maximum "
                                  "component diff of %i: 0x%x, actual 0x%x\n",
-                             x, y, maxComponentDiff,
+                             x, y, FLAGS_maxComponentDiff,
                              *referenceBitmap->getAddr32(x, y),
                              *bitmap->getAddr32(x, y));
                     SkDELETE(bitmap);
@@ -306,9 +213,9 @@
         }
     }
 
-    if (writeWholeImage) {
+    if (FLAGS_writeWholeImage) {
         sk_tools::force_all_opaque(*bitmap);
-        if (NULL != outputDir && writeWholeImage) {
+        if (NULL != outputDir && FLAGS_writeWholeImage) {
             SkString inputFilename;
             sk_tools::get_basename(&inputFilename, inputPath);
             SkString outputPath;
@@ -327,479 +234,80 @@
 }
 
 
-static int process_input(const SkString& input, const SkString* outputDir,
-                         sk_tools::PictureRenderer& renderer,
-                         bool validate, int maxComponentDiff,
-                         bool writeWholeImage, int clones) {
-    SkOSFile::Iter iter(input.c_str(), "skp");
+static int process_input(const char* input, const SkString* outputDir,
+                         sk_tools::PictureRenderer& renderer) {
+    SkOSFile::Iter iter(input, "skp");
     SkString inputFilename;
     int failures = 0;
-    SkDebugf("process_input, %s\n", input.c_str());
+    SkDebugf("process_input, %s\n", input);
     if (iter.next(&inputFilename)) {
         do {
             SkString inputPath;
-            sk_tools::make_filepath(&inputPath, input, inputFilename);
-            if (!render_picture(inputPath, outputDir, renderer,
-                                validate, maxComponentDiff,
-                                writeWholeImage, clones)) {
+            SkString inputAsSkString(input);
+            sk_tools::make_filepath(&inputPath, inputAsSkString, inputFilename);
+            if (!render_picture(inputPath, outputDir, renderer)) {
                 ++failures;
             }
         } while(iter.next(&inputFilename));
-    } else if (SkStrEndsWith(input.c_str(), ".skp")) {
+    } else if (SkStrEndsWith(input, ".skp")) {
         SkString inputPath(input);
-        if (!render_picture(inputPath, outputDir, renderer,
-                            validate, maxComponentDiff,
-                            writeWholeImage, clones)) {
+        if (!render_picture(inputPath, outputDir, renderer)) {
             ++failures;
         }
     } else {
         SkString warning;
-        warning.printf("Warning: skipping %s\n", input.c_str());
+        warning.printf("Warning: skipping %s\n", input);
         SkDebugf(warning.c_str());
     }
     return failures;
 }
 
-static void parse_commandline(int argc, char* const argv[], SkTArray<SkString>* inputs,
-                              sk_tools::PictureRenderer*& renderer, SkString*& outputDir,
-                              bool* validate, int* maxComponentDiff,
-                              bool* writeWholeImage,
-                              int* clones){
-    const char* argv0 = argv[0];
-    char* const* stop = argv + argc;
-
-    sk_tools::PictureRenderer::SkDeviceTypes deviceType =
-        sk_tools::PictureRenderer::kBitmap_DeviceType;
-
-    bool usePipe = false;
-    int numThreads = 1;
-    bool useTiles = false;
-    const char* widthString = NULL;
-    const char* heightString = NULL;
-    int gridWidth = 0;
-    int gridHeight = 0;
-    bool isPowerOf2Mode = false;
-    bool isCopyMode = false;
-    const char* xTilesString = NULL;
-    const char* yTilesString = NULL;
-    const char* mode = NULL;
-    bool gridSupported = false;
-    sk_tools::PictureRenderer::BBoxHierarchyType bbhType =
-        sk_tools::PictureRenderer::kNone_BBoxHierarchyType;
-    *validate = false;
-    *maxComponentDiff = 256;
-    *writeWholeImage = false;
-    *clones = 0;
-    SkISize viewport;
-    viewport.setEmpty();
-    SkScalar scaleFactor = SK_Scalar1;
-
-    for (++argv; argv < stop; ++argv) {
-        if (0 == strcmp(*argv, "--mode")) {
-            if (renderer != NULL) {
-                renderer->unref();
-                SkDebugf("Cannot combine modes.\n");
-                usage(argv0);
-                exit(-1);
-            }
-
-            ++argv;
-            if (argv >= stop) {
-                SkDebugf("Missing mode for --mode\n");
-                usage(argv0);
-                exit(-1);
-            }
-
-            if (0 == strcmp(*argv, "simple")) {
-                renderer = SkNEW(sk_tools::SimplePictureRenderer);
-            } else if ((0 == strcmp(*argv, "tile")) || (0 == strcmp(*argv, "pow2tile"))
-                       || 0 == strcmp(*argv, "copyTile")) {
-                useTiles = true;
-                mode = *argv;
-
-                if (0 == strcmp(*argv, "pow2tile")) {
-                    isPowerOf2Mode = true;
-                } else if (0 == strcmp(*argv, "copyTile")) {
-                    isCopyMode = true;
-                } else {
-                    gridSupported = true;
-                }
-
-                ++argv;
-                if (argv >= stop) {
-                    SkDebugf("Missing width for --mode %s\n", mode);
-                    usage(argv0);
-                    exit(-1);
-                }
-
-                widthString = *argv;
-                ++argv;
-                if (argv >= stop) {
-                    SkDebugf("Missing height for --mode %s\n", mode);
-                    usage(argv0);
-                    exit(-1);
-                }
-                heightString = *argv;
-            } else if (0 == strcmp(*argv, "rerecord")) {
-                renderer = SkNEW(sk_tools::RecordPictureRenderer);
-            } else {
-                SkDebugf("%s is not a valid mode for --mode\n", *argv);
-                usage(argv0);
-                exit(-1);
-            }
-        } else if (0 == strcmp(*argv, "--bbh")) {
-            ++argv;
-            if (argv >= stop) {
-                SkDebugf("Missing value for --bbh\n");
-                usage(argv0);
-                exit(-1);
-            }
-            if (0 == strcmp(*argv, "none")) {
-                bbhType = sk_tools::PictureRenderer::kNone_BBoxHierarchyType;
-            } else if (0 == strcmp(*argv, "rtree")) {
-                bbhType = sk_tools::PictureRenderer::kRTree_BBoxHierarchyType;
-            } else if (0 == strcmp(*argv, "grid")) {
-                bbhType = sk_tools::PictureRenderer::kTileGrid_BBoxHierarchyType;
-                ++argv;
-                if (argv >= stop) {
-                    SkDebugf("Missing width for --bbh grid\n");
-                    usage(argv0);
-                    exit(-1);
-                }
-                gridWidth = atoi(*argv);
-                ++argv;
-                if (argv >= stop) {
-                    SkDebugf("Missing height for --bbh grid\n");
-                    usage(argv0);
-                    exit(-1);
-                }
-                gridHeight = atoi(*argv);
-            } else {
-                SkDebugf("%s is not a valid value for --bbhType\n", *argv);
-                usage(argv0);
-                exit(-1);;
-            }
-        } else if (0 == strcmp(*argv, "--viewport")) {
-            ++argv;
-            if (argv >= stop) {
-                SkDebugf("Missing width for --viewport\n");
-                usage(argv0);
-                exit(-1);
-            }
-            viewport.fWidth = atoi(*argv);
-            ++argv;
-            if (argv >= stop) {
-                SkDebugf("Missing height for --viewport\n");
-                usage(argv0);
-                exit(-1);
-            }
-            viewport.fHeight = atoi(*argv);
-        } else if (0 == strcmp(*argv, "--scale")) {
-            ++argv;
-            if (argv >= stop) {
-                SkDebugf("Missing scaleFactor for --scale\n");
-                usage(argv0);
-                exit(-1);
-            }
-            scaleFactor = SkDoubleToScalar(atof(*argv));
-        } else if (0 == strcmp(*argv, "--tiles")) {
-            ++argv;
-            if (argv >= stop) {
-                SkDebugf("Missing x for --tiles\n");
-                usage(argv0);
-                exit(-1);
-            }
-            xTilesString = *argv;
-            ++argv;
-            if (argv >= stop) {
-                SkDebugf("Missing y for --tiles\n");
-                usage(argv0);
-                exit(-1);
-            }
-            yTilesString = *argv;
-        } else if (0 == strcmp(*argv, "--pipe")) {
-            usePipe = true;
-        } else if (0 == strcmp(*argv, "--multi")) {
-            ++argv;
-            if (argv >= stop) {
-                SkSafeUnref(renderer);
-                SkDebugf("Missing arg for --multi\n");
-                usage(argv0);
-                exit(-1);
-            }
-            numThreads = atoi(*argv);
-            if (numThreads < 2) {
-                SkSafeUnref(renderer);
-                SkDebugf("Number of threads must be at least 2.\n");
-                usage(argv0);
-                exit(-1);
-            }
-        } else if (0 == strcmp(*argv, "--clone")) {
-            ++argv;
-            if (argv >= stop) {
-                SkSafeUnref(renderer);
-                SkDebugf("Missing arg for --clone\n");
-                usage(argv0);
-                exit(-1);
-            }
-            *clones = atoi(*argv);
-            if (*clones < 0) {
-                SkSafeUnref(renderer);
-                SkDebugf("Number of clones must be at least 0.\n");
-                usage(argv0);
-                exit(-1);
-            }
-        } else if (0 == strcmp(*argv, "--device")) {
-            ++argv;
-            if (argv >= stop) {
-                SkSafeUnref(renderer);
-                SkDebugf("Missing mode for --device\n");
-                usage(argv0);
-                exit(-1);
-            }
-
-            if (0 == strcmp(*argv, "bitmap")) {
-                deviceType = sk_tools::PictureRenderer::kBitmap_DeviceType;
-            }
-#if SK_SUPPORT_GPU
-            else if (0 == strcmp(*argv, "gpu")) {
-                deviceType = sk_tools::PictureRenderer::kGPU_DeviceType;
-            }
-#endif
-#if SK_ANGLE
-            else if (0 == strcmp(*argv, "angle")) {
-                deviceType = sk_tools::PictureRenderer::kAngle_DeviceType;
-            }
-#endif
-            else {
-                SkSafeUnref(renderer);
-                SkDebugf("%s is not a valid mode for --device\n", *argv);
-                usage(argv0);
-                exit(-1);
-            }
-        } else if (0 == strcmp(*argv, "--enable-deferred-image-decoding")) {
-            lazy_decode = true;
-        } else if ((0 == strcmp(*argv, "-h")) || (0 == strcmp(*argv, "--help"))) {
-            SkSafeUnref(renderer);
-            usage(argv0);
-            exit(-1);
-        } else if (0 == strcmp(*argv, "-w")) {
-            ++argv;
-            if (argv >= stop) {
-                SkDebugf("Missing output directory for -w\n");
-                usage(argv0);
-                exit(-1);
-            }
-            outputDir = SkNEW_ARGS(SkString, (*argv));
-        } else if (0 == strcmp(*argv, "--validate")) {
-            *validate = true;
-        } else if (0 == strcmp(*argv, "--maxComponentDiff")) {
-            if (!*validate) {
-                SkDebugf("--maxComponentDiff must be used only with --validate\n");
-                usage(argv0);
-                exit(-1);
-            }
-            ++argv;
-            if (argv >= stop) {
-                SkDebugf("Missing arg for --maxComponentDiff\n");
-                usage(argv0);
-                exit(-1);
-            }
-            *maxComponentDiff = atoi(*argv);
-            if (*maxComponentDiff < 0 || *maxComponentDiff > 256) {
-                SkSafeUnref(renderer);
-                SkDebugf("maxComponentDiff: 0 - 256.\n");
-                usage(argv0);
-                exit(-1);
-            }
-        } else if (0 == strcmp(*argv, "--writeWholeImage")) {
-            *writeWholeImage = true;
-        } else {
-            inputs->push_back(SkString(*argv));
-        }
-    }
-
-    if (numThreads > 1 && !useTiles) {
-        SkSafeUnref(renderer);
-        SkDebugf("Multithreaded drawing requires tiled rendering.\n");
-        usage(argv0);
-        exit(-1);
-    }
-
-    if (usePipe && sk_tools::PictureRenderer::kNone_BBoxHierarchyType != bbhType) {
-        SkDebugf("--pipe and --bbh cannot be used together\n");
-        usage(argv0);
-        exit(-1);
-    }
-
-    if (sk_tools::PictureRenderer::kTileGrid_BBoxHierarchyType == bbhType &&
-        !gridSupported) {
-        SkDebugf("'--bbh grid' is not compatible with specified --mode.\n");
-        usage(argv0);
-        exit(-1);
-    }
-
-    if (useTiles) {
-        SkASSERT(NULL == renderer);
-        sk_tools::TiledPictureRenderer* tiledRenderer;
-        if (isCopyMode) {
-            int x, y;
-            if (xTilesString != NULL) {
-                SkASSERT(yTilesString != NULL);
-                x = atoi(xTilesString);
-                y = atoi(yTilesString);
-                if (x <= 0 || y <= 0) {
-                    SkDebugf("--tiles must be given values > 0\n");
-                    usage(argv0);
-                    exit(-1);
-                }
-            } else {
-                x = y = 4;
-            }
-            tiledRenderer = SkNEW_ARGS(sk_tools::CopyTilesRenderer, (x, y));
-        } else if (numThreads > 1) {
-            tiledRenderer = SkNEW_ARGS(sk_tools::MultiCorePictureRenderer, (numThreads));
-        } else {
-            tiledRenderer = SkNEW(sk_tools::TiledPictureRenderer);
-        }
-        if (isPowerOf2Mode) {
-            int minWidth = atoi(widthString);
-            if (!SkIsPow2(minWidth) || minWidth < 0) {
-                tiledRenderer->unref();
-                SkString err;
-                err.printf("-mode %s must be given a width"
-                           " value that is a power of two\n", mode);
-                SkDebugf(err.c_str());
-                usage(argv0);
-                exit(-1);
-            }
-            tiledRenderer->setTileMinPowerOf2Width(minWidth);
-        } else if (sk_tools::is_percentage(widthString)) {
-            if (isCopyMode) {
-                tiledRenderer->unref();
-                SkString err;
-                err.printf("--mode %s does not support percentages.\n", mode);
-                SkDebugf(err.c_str());
-                usage(argv0);
-                exit(-1);
-            }
-            tiledRenderer->setTileWidthPercentage(atof(widthString));
-            if (!(tiledRenderer->getTileWidthPercentage() > 0)) {
-                tiledRenderer->unref();
-                SkDebugf("--mode %s must be given a width percentage > 0\n", mode);
-                usage(argv0);
-                exit(-1);
-            }
-        } else {
-            tiledRenderer->setTileWidth(atoi(widthString));
-            if (!(tiledRenderer->getTileWidth() > 0)) {
-                tiledRenderer->unref();
-                SkDebugf("--mode %s must be given a width > 0\n", mode);
-                usage(argv0);
-                exit(-1);
-            }
-        }
-
-        if (sk_tools::is_percentage(heightString)) {
-            if (isCopyMode) {
-                tiledRenderer->unref();
-                SkString err;
-                err.printf("--mode %s does not support percentages.\n", mode);
-                SkDebugf(err.c_str());
-                usage(argv0);
-                exit(-1);
-            }
-            tiledRenderer->setTileHeightPercentage(atof(heightString));
-            if (!(tiledRenderer->getTileHeightPercentage() > 0)) {
-                tiledRenderer->unref();
-                SkDebugf("--mode %s must be given a height percentage > 0\n", mode);
-                usage(argv0);
-                exit(-1);
-            }
-        } else {
-            tiledRenderer->setTileHeight(atoi(heightString));
-            if (!(tiledRenderer->getTileHeight() > 0)) {
-                tiledRenderer->unref();
-                SkDebugf("--mode %s must be given a height > 0\n", mode);
-                usage(argv0);
-                exit(-1);
-            }
-        }
-        if (numThreads > 1) {
-            switch (deviceType) {
-#if SK_SUPPORT_GPU
-                case sk_tools::PictureRenderer::kGPU_DeviceType:
-                    // fall through
-#endif
-#if SK_ANGLE
-                case sk_tools::PictureRenderer::kAngle_DeviceType:
-#endif
-                    tiledRenderer->unref();
-                    SkDebugf("GPU not compatible with multithreaded tiling.\n");
-                    usage(argv0);
-                    exit(-1);
-                    break;
-                default:
-                    break;
-            }
-        }
-        renderer = tiledRenderer;
-        if (usePipe) {
-            SkDebugf("Pipe rendering is currently not compatible with tiling.\n"
-                     "Turning off pipe.\n");
-        }
-    } else if (usePipe) {
-        if (renderer != NULL) {
-            renderer->unref();
-            SkDebugf("Pipe is incompatible with other modes.\n");
-            usage(argv0);
-            exit(-1);
-        }
-        renderer = SkNEW(sk_tools::PipePictureRenderer);
-    }
-
-    if (inputs->empty()) {
-        SkSafeUnref(renderer);
-        if (NULL != outputDir) {
-            SkDELETE(outputDir);
-        }
-        usage(argv0);
-        exit(-1);
-    }
-
-    if (NULL == renderer) {
-        renderer = SkNEW(sk_tools::SimplePictureRenderer);
-    }
-
-    renderer->setBBoxHierarchyType(bbhType);
-    renderer->setGridSize(gridWidth, gridHeight);
-    renderer->setViewport(viewport);
-    renderer->setScaleFactor(scaleFactor);
-    if (!renderer->setDeviceType(deviceType)) {
-        SkDebugf("Invalid device type.\n");
-        exit(-1);
-    }
-}
-
 int tool_main(int argc, char** argv);
 int tool_main(int argc, char** argv) {
+    SkFlags::SetUsage("Render .skp files.");
+    SkFlags::ParseCommandLine(argc, argv);
+
+    if (FLAGS_r.isEmpty()) {
+        SkDebugf(".skp files or directories are required.\n");
+        exit(-1);
+    }
+
+    if (FLAGS_maxComponentDiff < 0 || FLAGS_maxComponentDiff > 256) {
+        SkDebugf("--maxComponentDiff must be between 0 and 256\n");
+        exit(-1);
+    }
+
+    if (FLAGS_maxComponentDiff != 256 && !FLAGS_validate) {
+        SkDebugf("--maxComponentDiff requires --validate\n");
+        exit(-1);
+    }
+
+    if (FLAGS_clone < 0) {
+        SkDebugf("--clone must be >= 0. Was %i\n", FLAGS_clone);
+        exit(-1);
+    }
+
+    SkString errorString;
+    SkAutoTUnref<sk_tools::PictureRenderer> renderer(parseRenderer(errorString,
+                                                                   kRender_PictureTool));
+    if (errorString.size() > 0) {
+        SkDebugf("%s\n", errorString.c_str());
+    }
+
+    if (renderer.get() == NULL) {
+        exit(-1);
+    }
+
     SkAutoGraphics ag;
-    SkTArray<SkString> inputs;
-    sk_tools::PictureRenderer* renderer = NULL;
-    SkString* outputDir = NULL;
-    bool validate = false;
-    int maxComponentDiff = 256;
-    bool writeWholeImage = false;
-    int clones = 0;
-    parse_commandline(argc, argv, &inputs, renderer, outputDir,
-                      &validate, &maxComponentDiff, &writeWholeImage, &clones);
-    SkASSERT(renderer);
+
+    SkString outputDir;
+    if (FLAGS_w.count() == 1) {
+        outputDir.set(FLAGS_w[0]);
+    }
 
     int failures = 0;
-    for (int i = 0; i < inputs.count(); i ++) {
-        failures += process_input(inputs[i], outputDir, *renderer,
-                                  validate, maxComponentDiff,
-                                  writeWholeImage, clones);
+    for (int i = 0; i < FLAGS_r.count(); i ++) {
+        failures += process_input(FLAGS_r[i], &outputDir, *renderer.get());
     }
     if (failures != 0) {
         SkDebugf("Failed to render %i pictures.\n", failures);
@@ -814,10 +322,6 @@
     }
 #endif
 #endif
-    if (NULL != outputDir) {
-        SkDELETE(outputDir);
-    }
-    SkDELETE(renderer);
     return 0;
 }