pdfviewer: measure mem usage, command line flags library.

Review URL: https://codereview.chromium.org/18562010

git-svn-id: http://skia.googlecode.com/svn/trunk@9996 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
edisonn@google.com 2013-07-11 12:27:21 +00:00
parent 76d119279e
commit a5aaa7998f
7 changed files with 62 additions and 78 deletions

View File

@ -16,7 +16,6 @@
#include "SkStream.h" #include "SkStream.h"
#include "SkTypeface.h" #include "SkTypeface.h"
#include "SkTArray.h" #include "SkTArray.h"
#include "picture_utils.h"
#include <iostream> #include <iostream>
#include <cstdio> #include <cstdio>
@ -2013,3 +2012,8 @@ SkRect SkPdfRenderer::MediaBox(int page) const {
SkASSERT(fPdfDoc); SkASSERT(fPdfDoc);
return fPdfDoc->MediaBox(page); return fPdfDoc->MediaBox(page);
} }
size_t SkPdfRenderer::bytesUsed() {
return fPdfDoc ? fPdfDoc->bytesUsed() : 0;
}

View File

@ -1,6 +1,6 @@
#include "SkCanvas.h" #include "SkCanvas.h"
#include "SkCommandLineFlags.h"
#include "SkDevice.h" #include "SkDevice.h"
#include "SkForceLinking.h"
#include "SkGraphics.h" #include "SkGraphics.h"
#include "SkImageDecoder.h" #include "SkImageDecoder.h"
#include "SkImageEncoder.h" #include "SkImageEncoder.h"
@ -13,6 +13,11 @@
#include "SkPdfRenderer.h" #include "SkPdfRenderer.h"
DEFINE_string2(readPath, r, "", "pdf files or directories of pdf files to process.");
DEFINE_string2(writePath, w, "", "Directory to write the rendered pages.");
DEFINE_bool2(noExtensionForOnePagePdf, n, false, "No page extension if only one page.");
DEFINE_bool2(showMemoryUsage, m, false, "Show Memory usage.");
/** /**
* Given list of directories and files to use as input, expects to find .pdf * Given list of directories and files to use as input, expects to find .pdf
* files and it will convert them to .png files writing them in the same directory * files and it will convert them to .png files writing them in the same directory
@ -25,24 +30,6 @@
static const char PDF_FILE_EXTENSION[] = "pdf"; static const char PDF_FILE_EXTENSION[] = "pdf";
static const char PNG_FILE_EXTENSION[] = "png"; static const char PNG_FILE_EXTENSION[] = "png";
// TODO(edisonn): add ability to write to a new directory.
static void usage(const char* argv0) {
SkDebugf("PDF to PNG rendering tool\n");
SkDebugf("\n"
"Usage: \n"
" %s <input>... [-w <outputDir>] [-n | --no-page-ext] \n"
, 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 pdfs.\n\n");
SkDebugf(
" -n: no page extension if only one page.\n\n");
SkDebugf("\n");
}
/** Replaces the extension of a file. /** Replaces the extension of a file.
* @param path File name whose extension will be changed. * @param path File name whose extension will be changed.
* @param old_extension The old extension. * @param old_extension The old extension.
@ -144,7 +131,7 @@ static bool render_page(const SkString& outputDir,
* @param renderer The object responsible to render the skp object into pdf. * @param renderer The object responsible to render the skp object into pdf.
*/ */
static bool process_pdf(const SkString& inputPath, const SkString& outputDir, static bool process_pdf(const SkString& inputPath, const SkString& outputDir,
SkPdfRenderer& renderer, bool noPageExt) { SkPdfRenderer& renderer, bool noPageExt, bool showMemoryUsage) {
SkDebugf("Loading PDF: %s\n", inputPath.c_str()); SkDebugf("Loading PDF: %s\n", inputPath.c_str());
SkString inputFilename; SkString inputFilename;
@ -162,6 +149,9 @@ static bool process_pdf(const SkString& inputPath, const SkString& outputDir,
success = renderer.load(inputPath); success = renderer.load(inputPath);
if (success) { if (success) {
if (showMemoryUsage) {
SkDebugf("Memory usage after load: %u\n", (unsigned int)renderer.bytesUsed());
}
if (!renderer.pages()) if (!renderer.pages())
{ {
SkDebugf("ERROR: Empty PDF Document %s\n", inputPath.c_str()); SkDebugf("ERROR: Empty PDF Document %s\n", inputPath.c_str());
@ -169,6 +159,7 @@ static bool process_pdf(const SkString& inputPath, const SkString& outputDir,
} else { } else {
for (int pn = 0; pn < renderer.pages(); ++pn) { for (int pn = 0; pn < renderer.pages(); ++pn) {
success = render_page(outputDir, inputFilename, renderer, noPageExt && renderer.pages() == 1 ? -1 : pn) && success; success = render_page(outputDir, inputFilename, renderer, noPageExt && renderer.pages() == 1 ? -1 : pn) && success;
SkDebugf("Memory usage after page %i rendered: %u\n", pn, (unsigned int)renderer.bytesUsed());
} }
} }
} }
@ -182,73 +173,52 @@ static bool process_pdf(const SkString& inputPath, const SkString& outputDir,
* @param outputDir Output dir. * @param outputDir Output dir.
* @param renderer The object responsible to render the skp object into pdf. * @param renderer The object responsible to render the skp object into pdf.
*/ */
static int process_input(const SkString& input, const SkString& outputDir, static int process_input(const char* input, const SkString& outputDir,
SkPdfRenderer& renderer, bool noPageExt) { SkPdfRenderer& renderer, bool noPageExt, bool showMemoryUsage) {
int failures = 0; int failures = 0;
if (sk_isdir(input.c_str())) { if (sk_isdir(input)) {
SkOSFile::Iter iter(input.c_str(), PDF_FILE_EXTENSION); SkOSFile::Iter iter(input, PDF_FILE_EXTENSION);
SkString inputFilename; SkString inputFilename;
while (iter.next(&inputFilename)) { while (iter.next(&inputFilename)) {
SkString inputPath; SkString inputPath;
sk_tools::make_filepath(&inputPath, input, inputFilename); SkString _input;
if (!process_pdf(inputPath, outputDir, renderer, noPageExt)) { _input.append(input);
sk_tools::make_filepath(&inputPath, _input, inputFilename);
if (!process_pdf(inputPath, outputDir, renderer, noPageExt, showMemoryUsage)) {
++failures; ++failures;
} }
} }
} else { } else {
SkString inputPath(input); SkString inputPath(input);
if (!process_pdf(inputPath, outputDir, renderer, noPageExt)) { if (!process_pdf(inputPath, outputDir, renderer, noPageExt, showMemoryUsage)) {
++failures; ++failures;
} }
} }
return failures; return failures;
} }
static void parse_commandline(int argc, char* const argv[],
SkTArray<SkString>* inputs,
SkString* outputDir, bool* noPageExt) {
const char* argv0 = argv[0];
char* const* stop = argv + argc;
for (++argv; argv < stop; ++argv) {
if ((0 == strcmp(*argv, "-h")) || (0 == strcmp(*argv, "--help"))) {
usage(argv0);
exit(-1);
} else if ((0 == strcmp(*argv, "-n")) || (0 == strcmp(*argv, "--no-page-ext"))) {
*noPageExt = true;
} else if (0 == strcmp(*argv, "-w")) {
++argv;
if (argv >= stop) {
SkDebugf("Missing outputDir for -w\n");
usage(argv0);
exit(-1);
}
*outputDir = SkString(*argv);
} else {
inputs->push_back(SkString(*argv));
}
}
if (inputs->count() < 1) {
usage(argv0);
exit(-1);
}
}
int tool_main(int argc, char** argv); int tool_main(int argc, char** argv);
int tool_main(int argc, char** argv) { int tool_main(int argc, char** argv) {
SkAutoGraphics ag; SkCommandLineFlags::SetUsage("Parse and Render .pdf files (pdf viewer).");
SkTArray<SkString> inputs; SkCommandLineFlags::Parse(argc, argv);
if (FLAGS_readPath.isEmpty()) {
SkDebugf(".pdf files or directories are required.\n");
exit(-1);
}
SkPdfRenderer renderer; SkPdfRenderer renderer;
SkString outputDir; SkString outputDir;
bool noPageExt = false; if (FLAGS_writePath.count() == 1) {
parse_commandline(argc, argv, &inputs, &outputDir, &noPageExt); outputDir.set(FLAGS_writePath[0]);
}
int failures = 0; int failures = 0;
for (int i = 0; i < inputs.count(); i ++) { for (int i = 0; i < FLAGS_readPath.count(); i ++) {
failures += process_input(inputs[i], outputDir, renderer, noPageExt); failures += process_input(FLAGS_readPath[i], outputDir, renderer,
FLAGS_noExtensionForOnePagePdf,
FLAGS_showMemoryUsage);
renderer.unload(); renderer.unload();
} }

View File

@ -416,3 +416,11 @@ SkPdfObject* SkNativeParsedPDF::resolveReference(const SkPdfObject* ref) {
// TODO(edisonn): fix the mess with const, probably we need to remove it pretty much everywhere // TODO(edisonn): fix the mess with const, probably we need to remove it pretty much everywhere
return (SkPdfObject*)ref; return (SkPdfObject*)ref;
} }
size_t SkNativeParsedPDF::bytesUsed() {
return fAllocator->bytesUsed() +
fContentLength +
fObjects.count() * sizeof(PublicObjectEntry) +
fPages.count() * sizeof(SkPdfPageObjectDictionary*) +
sizeof(*this);
}

View File

@ -17,8 +17,6 @@ class SkPdfCatalogDictionary;
class SkPdfPageObjectDictionary; class SkPdfPageObjectDictionary;
class SkPdfPageTreeNodeDictionary; class SkPdfPageTreeNodeDictionary;
class SkPdfNativeTokenizer; class SkPdfNativeTokenizer;
class SkNativeParsedPDF { class SkNativeParsedPDF {
@ -63,6 +61,9 @@ public:
SkPdfObject* resolveReference(const SkPdfObject* ref); SkPdfObject* resolveReference(const SkPdfObject* ref);
// Reports an approximation of all the memory usage.
size_t bytesUsed();
private: private:
unsigned char* readCrossReferenceSection(unsigned char* xrefStart, unsigned char* trailerEnd); unsigned char* readCrossReferenceSection(unsigned char* xrefStart, unsigned char* trailerEnd);

View File

@ -651,6 +651,7 @@ unsigned char* nextObject(unsigned char* start, unsigned char* end, SkPdfObject*
} }
SkPdfObject* SkPdfAllocator::allocBlock() { SkPdfObject* SkPdfAllocator::allocBlock() {
fSizeInBytes += BUFFER_SIZE * sizeof(SkPdfObject);
return new SkPdfObject[BUFFER_SIZE]; return new SkPdfObject[BUFFER_SIZE];
} }
@ -675,6 +676,7 @@ SkPdfObject* SkPdfAllocator::allocObject() {
fHistory.push(fCurrent); fHistory.push(fCurrent);
fCurrent = allocBlock(); fCurrent = allocBlock();
fCurrentUsed = 0; fCurrentUsed = 0;
fSizeInBytes += sizeof(SkPdfObject*);
} }
fCurrentUsed++; fCurrentUsed++;
return &fCurrent[fCurrentUsed - 1]; return &fCurrent[fCurrentUsed - 1];

View File

@ -83,9 +83,11 @@ class SkPdfAllocator {
int fCurrentUsed; int fCurrentUsed;
SkPdfObject* allocBlock(); SkPdfObject* allocBlock();
size_t fSizeInBytes;
public: public:
SkPdfAllocator() { SkPdfAllocator() {
fSizeInBytes = sizeof(*this);
fCurrent = allocBlock(); fCurrent = allocBlock();
fCurrentUsed = 0; fCurrentUsed = 0;
} }
@ -98,8 +100,13 @@ public:
void* alloc(size_t bytes) { void* alloc(size_t bytes) {
void* data = malloc(bytes); void* data = malloc(bytes);
fHandles.push(data); fHandles.push(data);
fSizeInBytes += bytes;
return data; return data;
} }
size_t bytesUsed() {
return fSizeInBytes;
}
}; };
class SkNativeParsedPDF; class SkNativeParsedPDF;

View File

@ -56,7 +56,6 @@
}, },
], ],
'include_dirs': [ 'include_dirs': [
'../tools',
'../experimental/PdfViewer', '../experimental/PdfViewer',
'../experimental/PdfViewer/pdfparser', '../experimental/PdfViewer/pdfparser',
'../experimental/PdfViewer/pdfparser/autogen', '../experimental/PdfViewer/pdfparser/autogen',
@ -65,16 +64,9 @@
], ],
'dependencies': [ 'dependencies': [
'core.gyp:core', 'core.gyp:core',
'effects.gyp:effects',
'images.gyp:images', 'images.gyp:images',
'pdf.gyp:pdf', 'zlib.gyp:zlib',
'ports.gyp:ports',
'tools.gyp:picture_utils',
], ],
'link_settings': {
'libraries': [
],
},
}, },
{ {
'target_name': 'pdfviewer', 'target_name': 'pdfviewer',
@ -87,7 +79,6 @@
'../experimental/PdfViewer/pdf_viewer_main.cpp', '../experimental/PdfViewer/pdf_viewer_main.cpp',
], ],
'include_dirs': [ 'include_dirs': [
'../tools',
'../experimental/PdfViewer', '../experimental/PdfViewer',
'../experimental/PdfViewer/pdfparser', '../experimental/PdfViewer/pdfparser',
'../experimental/PdfViewer/pdfparser/autogen', '../experimental/PdfViewer/pdfparser/autogen',
@ -96,8 +87,9 @@
], ],
'dependencies': [ 'dependencies': [
'core.gyp:core', 'core.gyp:core',
'images.gyp:images', 'flags.gyp:flags',
'libpdfviewer', 'libpdfviewer',
'tools.gyp:picture_utils',
], ],
}, },
], ],