2012-06-12 14:56:36 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SkBitmap.h"
|
|
|
|
#include "SkCanvas.h"
|
2012-07-20 22:34:27 +00:00
|
|
|
#include "SkDevice.h"
|
2012-09-10 17:19:06 +00:00
|
|
|
#include "SkGraphics.h"
|
2012-08-23 20:53:25 +00:00
|
|
|
#include "SkMath.h"
|
2012-06-12 14:56:36 +00:00
|
|
|
#include "SkOSFile.h"
|
|
|
|
#include "SkPicture.h"
|
|
|
|
#include "SkStream.h"
|
|
|
|
#include "SkString.h"
|
2012-07-13 18:55:53 +00:00
|
|
|
#include "SkTArray.h"
|
2012-07-26 17:27:57 +00:00
|
|
|
#include "PictureRenderer.h"
|
2012-06-22 18:24:56 +00:00
|
|
|
#include "picture_utils.h"
|
2012-06-12 14:56:36 +00:00
|
|
|
|
|
|
|
static void usage(const char* argv0) {
|
|
|
|
SkDebugf("SkPicture rendering tool\n");
|
|
|
|
SkDebugf("\n"
|
|
|
|
"Usage: \n"
|
2012-07-20 22:34:27 +00:00
|
|
|
" %s <input>... <outputDir> \n"
|
2012-08-23 20:53:25 +00:00
|
|
|
" [--mode pipe | pow2tile minWidth height[%] | simple\n"
|
|
|
|
" | tile width[%] height[%]]\n"
|
2012-08-20 15:04:04 +00:00
|
|
|
" [--device bitmap"
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
" | gpu"
|
|
|
|
#endif
|
|
|
|
"]"
|
2012-06-12 14:56:36 +00:00
|
|
|
, argv0);
|
2012-07-20 22:34:27 +00:00
|
|
|
SkDebugf("\n\n");
|
2012-06-12 14:56:36 +00:00
|
|
|
SkDebugf(
|
2012-08-02 18:57:53 +00:00
|
|
|
" input: A list of directories and files to use as input. Files are\n"
|
|
|
|
" expected to have the .skp extension.\n\n");
|
2012-06-12 14:56:36 +00:00
|
|
|
SkDebugf(
|
2012-08-02 18:57:53 +00:00
|
|
|
" outputDir: directory to write the rendered images.\n\n");
|
2012-07-20 22:34:27 +00:00
|
|
|
SkDebugf(
|
2012-08-23 20:53:25 +00:00
|
|
|
" --mode pipe | pow2tile minWidth height[%] | simple\n"
|
|
|
|
" | tile width[%] height[%]: Run in the corresponding mode.\n"
|
|
|
|
" Default is simple.\n");
|
2012-07-20 22:34:27 +00:00
|
|
|
SkDebugf(
|
2012-08-02 18:57:53 +00:00
|
|
|
" pipe, Render using a SkGPipe.\n");
|
|
|
|
SkDebugf(
|
2012-08-23 20:53:25 +00:00
|
|
|
" 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(
|
2012-08-02 18:57:53 +00:00
|
|
|
" simple, Render using the default rendering method.\n");
|
|
|
|
SkDebugf(
|
|
|
|
" tile width[%] height[%], Do a simple render using tiles\n"
|
|
|
|
" with the given dimensions.\n");
|
2012-08-20 15:04:04 +00:00
|
|
|
SkDebugf("\n");
|
|
|
|
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
|
2012-08-20 15:04:12 +00:00
|
|
|
SkDebugf(
|
2012-08-20 15:04:04 +00:00
|
|
|
" gpu, Render to the GPU.\n");
|
|
|
|
#endif
|
2012-06-12 14:56:36 +00:00
|
|
|
}
|
|
|
|
|
2012-07-13 18:22:59 +00:00
|
|
|
static void make_output_filepath(SkString* path, const SkString& dir,
|
2012-06-12 14:56:36 +00:00
|
|
|
const SkString& name) {
|
2012-06-22 18:24:56 +00:00
|
|
|
sk_tools::make_filepath(path, dir, name);
|
2012-06-12 14:56:36 +00:00
|
|
|
path->remove(path->size() - 3, 3);
|
|
|
|
path->append("png");
|
|
|
|
}
|
|
|
|
|
2012-09-17 18:26:06 +00:00
|
|
|
static bool write_output(const SkString& outputDir, const SkString& inputFilename,
|
2012-08-21 19:05:08 +00:00
|
|
|
const sk_tools::PictureRenderer& renderer) {
|
2012-06-12 14:56:36 +00:00
|
|
|
SkString outputPath;
|
|
|
|
make_output_filepath(&outputPath, outputDir, inputFilename);
|
2012-08-21 19:05:08 +00:00
|
|
|
bool isWritten = renderer.write(outputPath);
|
2012-06-12 14:56:36 +00:00
|
|
|
if (!isWritten) {
|
|
|
|
SkDebugf("Could not write to file %s\n", outputPath.c_str());
|
|
|
|
}
|
2012-09-17 18:26:06 +00:00
|
|
|
return isWritten;
|
2012-06-12 14:56:36 +00:00
|
|
|
}
|
|
|
|
|
2012-09-17 18:26:06 +00:00
|
|
|
static bool render_picture(const SkString& inputPath, const SkString& outputDir,
|
2012-07-26 17:27:57 +00:00
|
|
|
sk_tools::PictureRenderer& renderer) {
|
2012-07-09 18:32:08 +00:00
|
|
|
SkString inputFilename;
|
|
|
|
sk_tools::get_basename(&inputFilename, inputPath);
|
2012-06-22 18:24:56 +00:00
|
|
|
|
2012-07-09 18:32:08 +00:00
|
|
|
SkFILEStream inputStream;
|
2012-06-22 18:24:56 +00:00
|
|
|
inputStream.setPath(inputPath.c_str());
|
|
|
|
if (!inputStream.isValid()) {
|
|
|
|
SkDebugf("Could not open file %s\n", inputPath.c_str());
|
2012-09-17 18:26:06 +00:00
|
|
|
return false;
|
2012-06-22 18:24:56 +00:00
|
|
|
}
|
|
|
|
|
2012-09-17 18:26:06 +00:00
|
|
|
bool success = false;
|
|
|
|
SkPicture* picture = SkNEW_ARGS(SkPicture, (&inputStream, &success));
|
2012-09-14 14:34:28 +00:00
|
|
|
SkAutoTUnref<SkPicture> aur(picture);
|
2012-09-17 18:26:06 +00:00
|
|
|
if (!success) {
|
|
|
|
SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-26 17:27:57 +00:00
|
|
|
|
2012-09-14 14:34:28 +00:00
|
|
|
SkDebugf("drawing... [%i %i] %s\n", picture->width(), picture->height(),
|
2012-09-10 18:18:38 +00:00
|
|
|
inputPath.c_str());
|
2012-09-14 02:01:10 +00:00
|
|
|
|
2012-09-14 14:34:28 +00:00
|
|
|
// rescale to avoid memory issues allocating a very large offscreen
|
|
|
|
sk_tools::resize_if_needed(&aur);
|
2012-09-13 17:44:32 +00:00
|
|
|
|
2012-09-14 14:34:28 +00:00
|
|
|
renderer.init(aur);
|
2012-08-07 17:11:33 +00:00
|
|
|
|
2012-09-07 15:21:18 +00:00
|
|
|
renderer.render(true);
|
|
|
|
|
|
|
|
renderer.resetState();
|
2012-08-07 17:11:33 +00:00
|
|
|
|
2012-09-17 18:26:06 +00:00
|
|
|
success = write_output(outputDir, inputFilename, renderer);
|
2012-08-07 17:11:33 +00:00
|
|
|
|
|
|
|
renderer.end();
|
2012-09-17 18:26:06 +00:00
|
|
|
return success;
|
2012-06-12 14:56:36 +00:00
|
|
|
}
|
|
|
|
|
2012-09-17 18:26:06 +00:00
|
|
|
static int process_input(const SkString& input, const SkString& outputDir,
|
2012-07-26 17:27:57 +00:00
|
|
|
sk_tools::PictureRenderer& renderer) {
|
2012-07-13 18:22:59 +00:00
|
|
|
SkOSFile::Iter iter(input.c_str(), "skp");
|
2012-07-09 18:32:08 +00:00
|
|
|
SkString inputFilename;
|
2012-09-17 18:26:06 +00:00
|
|
|
int failures = 0;
|
2012-07-09 18:32:08 +00:00
|
|
|
if (iter.next(&inputFilename)) {
|
|
|
|
do {
|
|
|
|
SkString inputPath;
|
|
|
|
sk_tools::make_filepath(&inputPath, input, inputFilename);
|
2012-09-17 18:26:06 +00:00
|
|
|
if (!render_picture(inputPath, outputDir, renderer))
|
|
|
|
++failures;
|
2012-07-09 18:32:08 +00:00
|
|
|
} while(iter.next(&inputFilename));
|
|
|
|
} else {
|
|
|
|
SkString inputPath(input);
|
2012-09-17 18:26:06 +00:00
|
|
|
if (!render_picture(inputPath, outputDir, renderer))
|
|
|
|
++failures;
|
2012-07-09 18:32:08 +00:00
|
|
|
}
|
2012-09-17 18:26:06 +00:00
|
|
|
return failures;
|
2012-07-09 18:32:08 +00:00
|
|
|
}
|
|
|
|
|
2012-07-13 18:22:59 +00:00
|
|
|
static void parse_commandline(int argc, char* const argv[], SkTArray<SkString>* inputs,
|
2012-07-26 17:27:57 +00:00
|
|
|
sk_tools::PictureRenderer*& renderer){
|
2012-07-13 18:22:59 +00:00
|
|
|
const char* argv0 = argv[0];
|
|
|
|
char* const* stop = argv + argc;
|
|
|
|
|
2012-08-20 15:04:04 +00:00
|
|
|
sk_tools::PictureRenderer::SkDeviceTypes deviceType =
|
|
|
|
sk_tools::PictureRenderer::kBitmap_DeviceType;
|
|
|
|
|
2012-07-13 18:22:59 +00:00
|
|
|
for (++argv; argv < stop; ++argv) {
|
2012-08-02 18:57:53 +00:00
|
|
|
if (0 == strcmp(*argv, "--mode")) {
|
|
|
|
SkDELETE(renderer);
|
|
|
|
|
2012-07-27 20:09:26 +00:00
|
|
|
++argv;
|
2012-08-02 18:57:53 +00:00
|
|
|
if (argv >= stop) {
|
|
|
|
SkDebugf("Missing mode for --mode\n");
|
|
|
|
usage(argv0);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 == strcmp(*argv, "pipe")) {
|
|
|
|
renderer = SkNEW(sk_tools::PipePictureRenderer);
|
|
|
|
} else if (0 == strcmp(*argv, "simple")) {
|
|
|
|
renderer = SkNEW(sk_tools::SimplePictureRenderer);
|
2012-08-23 20:53:25 +00:00
|
|
|
} else if ((0 == strcmp(*argv, "tile")) || (0 == strcmp(*argv, "pow2tile"))) {
|
|
|
|
char* mode = *argv;
|
|
|
|
bool isPowerOf2Mode = false;
|
|
|
|
|
|
|
|
if (0 == strcmp(*argv, "pow2tile")) {
|
|
|
|
isPowerOf2Mode = true;
|
|
|
|
}
|
|
|
|
|
2012-08-02 18:57:53 +00:00
|
|
|
sk_tools::TiledPictureRenderer* tileRenderer =
|
|
|
|
SkNEW(sk_tools::TiledPictureRenderer);
|
|
|
|
++argv;
|
|
|
|
if (argv >= stop) {
|
|
|
|
SkDELETE(tileRenderer);
|
2012-08-23 20:53:25 +00:00
|
|
|
SkDebugf("Missing width for --mode %s\n", mode);
|
2012-08-02 18:57:53 +00:00
|
|
|
usage(argv0);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2012-08-23 20:53:25 +00:00
|
|
|
if (isPowerOf2Mode) {
|
|
|
|
int minWidth = atoi(*argv);
|
|
|
|
|
|
|
|
if (!SkIsPow2(minWidth) || minWidth <= 0) {
|
|
|
|
SkDELETE(tileRenderer);
|
|
|
|
SkDebugf("--mode %s must be given a width"
|
|
|
|
" value that is a power of two\n", mode);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
tileRenderer->setTileMinPowerOf2Width(minWidth);
|
|
|
|
} else if (sk_tools::is_percentage(*argv)) {
|
2012-07-27 20:09:26 +00:00
|
|
|
tileRenderer->setTileWidthPercentage(atof(*argv));
|
|
|
|
if (!(tileRenderer->getTileWidthPercentage() > 0)) {
|
|
|
|
SkDELETE(tileRenderer);
|
2012-08-23 20:53:25 +00:00
|
|
|
SkDebugf("--mode %s must be given a width percentage > 0\n", mode);
|
2012-07-27 20:09:26 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tileRenderer->setTileWidth(atoi(*argv));
|
|
|
|
if (!(tileRenderer->getTileWidth() > 0)) {
|
|
|
|
SkDELETE(tileRenderer);
|
2012-08-23 20:53:25 +00:00
|
|
|
SkDebugf("--mode %s must be given a width > 0\n", mode);
|
2012-07-27 20:09:26 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
}
|
2012-08-02 18:57:53 +00:00
|
|
|
|
|
|
|
++argv;
|
|
|
|
if (argv >= stop) {
|
|
|
|
SkDELETE(tileRenderer);
|
2012-08-23 20:53:25 +00:00
|
|
|
SkDebugf("Missing height for --mode %s\n", mode);
|
2012-08-02 18:57:53 +00:00
|
|
|
usage(argv0);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2012-08-01 17:53:29 +00:00
|
|
|
if (sk_tools::is_percentage(*argv)) {
|
2012-07-27 20:09:26 +00:00
|
|
|
tileRenderer->setTileHeightPercentage(atof(*argv));
|
|
|
|
if (!(tileRenderer->getTileHeightPercentage() > 0)) {
|
|
|
|
SkDELETE(tileRenderer);
|
|
|
|
SkDebugf(
|
2012-08-23 20:53:25 +00:00
|
|
|
"--mode %s must be given a height percentage > 0\n", mode);
|
2012-07-27 20:09:26 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tileRenderer->setTileHeight(atoi(*argv));
|
|
|
|
if (!(tileRenderer->getTileHeight() > 0)) {
|
|
|
|
SkDELETE(tileRenderer);
|
2012-08-23 20:53:25 +00:00
|
|
|
SkDebugf("--mode %s must be given a height > 0\n", mode);
|
2012-07-27 20:09:26 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
}
|
2012-08-02 18:57:53 +00:00
|
|
|
|
|
|
|
renderer = tileRenderer;
|
2012-07-27 20:09:26 +00:00
|
|
|
} else {
|
2012-08-02 18:57:53 +00:00
|
|
|
SkDebugf("%s is not a valid mode for --mode\n", *argv);
|
2012-07-27 20:09:26 +00:00
|
|
|
usage(argv0);
|
|
|
|
exit(-1);
|
|
|
|
}
|
2012-08-20 15:04:04 +00:00
|
|
|
} else if (0 == strcmp(*argv, "--device")) {
|
|
|
|
++argv;
|
|
|
|
if (argv >= stop) {
|
|
|
|
SkDebugf("Missing mode for --deivce\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
|
|
|
|
else {
|
|
|
|
SkDebugf("%s is not a valid mode for --device\n", *argv);
|
|
|
|
usage(argv0);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2012-07-20 22:34:27 +00:00
|
|
|
} else if ((0 == strcmp(*argv, "-h")) || (0 == strcmp(*argv, "--help"))) {
|
2012-07-26 17:27:57 +00:00
|
|
|
SkDELETE(renderer);
|
2012-07-20 22:34:27 +00:00
|
|
|
usage(argv0);
|
|
|
|
exit(-1);
|
2012-07-16 17:29:16 +00:00
|
|
|
} else {
|
|
|
|
inputs->push_back(SkString(*argv));
|
|
|
|
}
|
2012-06-12 14:56:36 +00:00
|
|
|
}
|
2012-07-20 22:34:27 +00:00
|
|
|
|
|
|
|
if (inputs->count() < 2) {
|
2012-07-26 17:27:57 +00:00
|
|
|
SkDELETE(renderer);
|
2012-07-20 22:34:27 +00:00
|
|
|
usage(argv0);
|
|
|
|
exit(-1);
|
|
|
|
}
|
2012-07-26 17:27:57 +00:00
|
|
|
|
|
|
|
if (NULL == renderer) {
|
|
|
|
renderer = SkNEW(sk_tools::SimplePictureRenderer);
|
|
|
|
}
|
2012-08-20 15:03:29 +00:00
|
|
|
|
2012-08-20 15:04:04 +00:00
|
|
|
renderer->setDeviceType(deviceType);
|
2012-07-13 18:22:59 +00:00
|
|
|
}
|
2012-06-12 14:56:36 +00:00
|
|
|
|
2012-07-13 18:22:59 +00:00
|
|
|
int main(int argc, char* const argv[]) {
|
2012-09-17 18:26:06 +00:00
|
|
|
SkAutoGraphics ag;
|
2012-07-13 18:22:59 +00:00
|
|
|
SkTArray<SkString> inputs;
|
2012-07-26 17:27:57 +00:00
|
|
|
sk_tools::PictureRenderer* renderer = NULL;
|
2012-07-20 22:34:27 +00:00
|
|
|
|
2012-07-26 17:27:57 +00:00
|
|
|
parse_commandline(argc, argv, &inputs, renderer);
|
2012-07-13 18:22:59 +00:00
|
|
|
SkString outputDir = inputs[inputs.count() - 1];
|
2012-07-26 17:27:57 +00:00
|
|
|
SkASSERT(renderer);
|
2012-06-12 14:56:36 +00:00
|
|
|
|
2012-09-17 18:26:06 +00:00
|
|
|
int failures = 0;
|
2012-07-13 18:22:59 +00:00
|
|
|
for (int i = 0; i < inputs.count() - 1; i ++) {
|
2012-09-17 18:26:06 +00:00
|
|
|
failures += process_input(inputs[i], outputDir, *renderer);
|
|
|
|
}
|
|
|
|
if (failures != 0) {
|
|
|
|
SkDebugf("Failed to render %i pictures.\n", failures);
|
|
|
|
return 1;
|
2012-06-12 14:56:36 +00:00
|
|
|
}
|
2012-09-13 15:40:37 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
#if GR_CACHE_STATS
|
|
|
|
if (renderer->isUsingGpuDevice()) {
|
|
|
|
GrContext* ctx = renderer->getGrContext();
|
|
|
|
|
|
|
|
ctx->printCacheStats();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2012-07-26 17:27:57 +00:00
|
|
|
SkDELETE(renderer);
|
2012-06-12 14:56:36 +00:00
|
|
|
}
|