blob: dd5f6421fb93af9f784a9cac07360b3b3d3826f0 [file] [log] [blame]
edisonn@google.comc319abe2012-11-01 19:52:38 +00001/*
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 "SkCanvas.h"
halcanary0bef17a2014-08-07 07:24:47 -07009#include "SkCommandLineFlags.h"
halcanary8b2cb332014-08-11 13:08:27 -070010#include "SkDocument.h"
scroggo@google.com7def5e12013-05-31 14:00:10 +000011#include "SkForceLinking.h"
edisonn@google.comc319abe2012-11-01 19:52:38 +000012#include "SkGraphics.h"
edisonn@google.comd9dfa182013-04-24 13:01:01 +000013#include "SkImageEncoder.h"
edisonn@google.comc319abe2012-11-01 19:52:38 +000014#include "SkOSFile.h"
15#include "SkPicture.h"
16#include "SkStream.h"
17#include "SkTArray.h"
halcanary0bef17a2014-08-07 07:24:47 -070018#include "SkTSort.h"
halcanary0d154ee2014-08-11 11:33:51 -070019#include "ProcStats.h"
edisonn@google.comc319abe2012-11-01 19:52:38 +000020
scroggo@google.com7def5e12013-05-31 14:00:10 +000021__SK_FORCE_IMAGE_DECODER_LINKING;
22
edisonn@google.com184487c2013-03-08 18:00:16 +000023#ifdef SK_USE_CDB
24#include "win_dbghelp.h"
25#endif
26
edisonn@google.comc319abe2012-11-01 19:52:38 +000027/**
28 * render_pdfs
29 *
30 * Given list of directories and files to use as input, expects to find .skp
31 * files and it will convert them to .pdf files writing them in the output
32 * directory.
33 *
34 * Returns zero exit code if all .skp files were converted successfully,
35 * otherwise returns error code 1.
36 */
37
38static const char PDF_FILE_EXTENSION[] = "pdf";
39static const char SKP_FILE_EXTENSION[] = "skp";
40
halcanary0bef17a2014-08-07 07:24:47 -070041
42DEFINE_string2(inputPaths, r, "",
43 "A list of directories and files to use as input. "
44 "Files are expected to have the .skp extension.");
45
46DEFINE_string2(outputDir, w, "",
47 "Directory to write the rendered pdfs.");
48
49DEFINE_string2(match, m, "",
50 "[~][^]substring[$] [...] of filenames to run.\n"
51 "Multiple matches may be separated by spaces.\n"
52 "~ causes a matching file to always be skipped\n"
53 "^ requires the start of the file to match\n"
54 "$ requires the end of the file to match\n"
55 "^ and $ requires an exact match\n"
56 "If a file does not match any list entry,\n"
57 "it is skipped unless some list entry starts with ~");
58
59DEFINE_int32(jpegQuality, 100,
60 "Encodes images in JPEG at quality level N, which can be in "
61 "range 0-100). N = -1 will disable JPEG compression. "
62 "Default is N = 100, maximum quality.");
edisonn@google.comc319abe2012-11-01 19:52:38 +000063
64/** Replaces the extension of a file.
65 * @param path File name whose extension will be changed.
66 * @param old_extension The old extension.
67 * @param new_extension The new extension.
68 * @returns false if the file did not has the expected extension.
69 * if false is returned, contents of path are undefined.
70 */
71static bool replace_filename_extension(SkString* path,
72 const char old_extension[],
73 const char new_extension[]) {
74 if (path->endsWith(old_extension)) {
75 path->remove(path->size() - strlen(old_extension),
76 strlen(old_extension));
77 if (!path->endsWith(".")) {
78 return false;
79 }
80 path->append(new_extension);
81 return true;
82 }
83 return false;
84}
85
reed@google.com672588b2014-01-08 15:42:01 +000086// the size_t* parameter is deprecated, so we ignore it
87static SkData* encode_to_dct_data(size_t*, const SkBitmap& bitmap) {
halcanary0bef17a2014-08-07 07:24:47 -070088 if (FLAGS_jpegQuality == -1) {
commit-bot@chromium.org608ea652013-10-03 19:29:21 +000089 return NULL;
90 }
edisonn@google.comd9dfa182013-04-24 13:01:01 +000091
commit-bot@chromium.org608ea652013-10-03 19:29:21 +000092 SkBitmap bm = bitmap;
edisonn@google.comd9dfa182013-04-24 13:01:01 +000093#if defined(SK_BUILD_FOR_MAC)
commit-bot@chromium.org608ea652013-10-03 19:29:21 +000094 // Workaround bug #1043 where bitmaps with referenced pixels cause
95 // CGImageDestinationFinalize to crash
96 SkBitmap copy;
commit-bot@chromium.orgd5f032d2014-02-24 18:51:43 +000097 bitmap.deepCopyTo(&copy);
commit-bot@chromium.org608ea652013-10-03 19:29:21 +000098 bm = copy;
edisonn@google.comd9dfa182013-04-24 13:01:01 +000099#endif
100
halcanary0bef17a2014-08-07 07:24:47 -0700101 return SkImageEncoder::EncodeData(
102 bm, SkImageEncoder::kJPEG_Type, FLAGS_jpegQuality);
edisonn@google.comd9dfa182013-04-24 13:01:01 +0000103}
104
skia.committer@gmail.com760f2d92012-11-02 02:01:24 +0000105/** Builds the output filename. path = dir/name, and it replaces expected
edisonn@google.comc319abe2012-11-01 19:52:38 +0000106 * .skp extension with .pdf extention.
107 * @param path Output filename.
108 * @param name The name of the file.
109 * @returns false if the file did not has the expected extension.
110 * if false is returned, contents of path are undefined.
111 */
112static bool make_output_filepath(SkString* path, const SkString& dir,
113 const SkString& name) {
tfarinaa8e2e152014-07-28 19:26:58 -0700114 *path = SkOSPath::Join(dir.c_str(), name.c_str());
edisonn@google.comc319abe2012-11-01 19:52:38 +0000115 return replace_filename_extension(path,
116 SKP_FILE_EXTENSION,
117 PDF_FILE_EXTENSION);
118}
119
halcanary8b2cb332014-08-11 13:08:27 -0700120namespace {
121// This is a write-only stream.
122class NullWStream : public SkWStream {
123public:
124 NullWStream() : fBytesWritten(0) { }
125 virtual bool write(const void*, size_t size) SK_OVERRIDE {
126 fBytesWritten += size;
127 return true;
128 }
129 virtual size_t bytesWritten() const SK_OVERRIDE { return fBytesWritten; }
130 size_t fBytesWritten;
131};
132} // namespace
133
edisonn@google.comc319abe2012-11-01 19:52:38 +0000134/** Write the output of pdf renderer to a file.
135 * @param outputDir Output dir.
136 * @param inputFilename The skp file that was read.
137 * @param renderer The object responsible to write the pdf file.
138 */
commit-bot@chromium.org5e009892013-10-14 13:42:12 +0000139static SkWStream* open_stream(const SkString& outputDir,
140 const SkString& inputFilename) {
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000141 if (outputDir.isEmpty()) {
halcanary8b2cb332014-08-11 13:08:27 -0700142 return SkNEW(NullWStream);
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000143 }
144
edisonn@google.comc319abe2012-11-01 19:52:38 +0000145 SkString outputPath;
146 if (!make_output_filepath(&outputPath, outputDir, inputFilename)) {
commit-bot@chromium.org5e009892013-10-14 13:42:12 +0000147 return NULL;
edisonn@google.comc319abe2012-11-01 19:52:38 +0000148 }
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000149
halcanary8b2cb332014-08-11 13:08:27 -0700150 SkAutoTDelete<SkFILEWStream> stream(
151 SkNEW_ARGS(SkFILEWStream, (outputPath.c_str())));
152 if (!stream.get() || !stream->isValid()) {
edisonn@google.comc319abe2012-11-01 19:52:38 +0000153 SkDebugf("Could not write to file %s\n", outputPath.c_str());
commit-bot@chromium.org5e009892013-10-14 13:42:12 +0000154 return NULL;
edisonn@google.comc319abe2012-11-01 19:52:38 +0000155 }
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000156
halcanary8b2cb332014-08-11 13:08:27 -0700157 return stream.detach();
edisonn@google.comc319abe2012-11-01 19:52:38 +0000158}
159
halcanary8b2cb332014-08-11 13:08:27 -0700160/**
161 * Given a SkPicture, write a one-page PDF document to the given
162 * output, using the provided encoder.
edisonn@google.comc319abe2012-11-01 19:52:38 +0000163 */
halcanary8b2cb332014-08-11 13:08:27 -0700164static bool pdf_to_stream(SkPicture* picture,
165 SkWStream* output,
166 SkPicture::EncodeBitmap encoder) {
167 SkAutoTUnref<SkDocument> pdfDocument(
168 SkDocument::CreatePDF(output, NULL, encoder));
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700169 SkCanvas* canvas = pdfDocument->beginPage(picture->cullRect().width(),
170 picture->cullRect().height());
halcanary8b2cb332014-08-11 13:08:27 -0700171 canvas->drawPicture(picture);
172 canvas->flush();
173 return pdfDocument->close();
edisonn@google.comc319abe2012-11-01 19:52:38 +0000174}
175
halcanary0bef17a2014-08-07 07:24:47 -0700176static bool operator<(const SkString& a, const SkString& b) {
177 return strcmp(a.c_str(), b.c_str()) < 0;
178}
179
halcanary8b2cb332014-08-11 13:08:27 -0700180/**
181 * @param A list of directories or a skp files.
182 * @returns an alphabetical list of skp files.
edisonn@google.comc319abe2012-11-01 19:52:38 +0000183 */
halcanary8b2cb332014-08-11 13:08:27 -0700184static void process_input_files(
halcanary0bef17a2014-08-07 07:24:47 -0700185 const SkCommandLineFlags::StringArray& inputs,
halcanary8b2cb332014-08-11 13:08:27 -0700186 SkTArray<SkString>* files) {
halcanary0bef17a2014-08-07 07:24:47 -0700187 for (int i = 0; i < inputs.count(); i ++) {
188 const char* input = inputs[i];
189 if (sk_isdir(input)) {
190 SkOSFile::Iter iter(input, SKP_FILE_EXTENSION);
191 SkString inputFilename;
192 while (iter.next(&inputFilename)) {
193 if (!SkCommandLineFlags::ShouldSkip(
194 FLAGS_match, inputFilename.c_str())) {
halcanary8b2cb332014-08-11 13:08:27 -0700195 files->push_back(
halcanary0bef17a2014-08-07 07:24:47 -0700196 SkOSPath::Join(input, inputFilename.c_str()));
197 }
198 }
199 } else {
200 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, input)) {
halcanary8b2cb332014-08-11 13:08:27 -0700201 files->push_back(SkString(input));
edisonn@google.comc319abe2012-11-01 19:52:38 +0000202 }
203 }
halcanary0bef17a2014-08-07 07:24:47 -0700204 }
halcanary8b2cb332014-08-11 13:08:27 -0700205 if (files->count() > 0) {
206 SkTQSort<SkString>(files->begin(), files->end() - 1);
halcanary0d154ee2014-08-11 11:33:51 -0700207 }
edisonn@google.comc319abe2012-11-01 19:52:38 +0000208}
209
halcanary8b2cb332014-08-11 13:08:27 -0700210/** For each input skp file, read it, render it to pdf and write. the
211 * output to a pdf file
212 */
edisonn@google.com184487c2013-03-08 18:00:16 +0000213int tool_main_core(int argc, char** argv);
214int tool_main_core(int argc, char** argv) {
halcanary0bef17a2014-08-07 07:24:47 -0700215 SkCommandLineFlags::Parse(argc, argv);
216
edisonn@google.comc319abe2012-11-01 19:52:38 +0000217 SkAutoGraphics ag;
edisonn@google.comc319abe2012-11-01 19:52:38 +0000218
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000219 SkString outputDir;
halcanary0bef17a2014-08-07 07:24:47 -0700220 if (FLAGS_outputDir.count() > 0) {
221 outputDir = FLAGS_outputDir[0];
halcanary8b2cb332014-08-11 13:08:27 -0700222 if (!sk_mkdir(outputDir.c_str())) {
223 SkDebugf("Unable to mkdir '%s'\n", outputDir.c_str());
224 return 1;
225 }
edisonn@google.comc319abe2012-11-01 19:52:38 +0000226 }
227
halcanary8b2cb332014-08-11 13:08:27 -0700228 SkTArray<SkString> files;
229 process_input_files(FLAGS_inputPaths, &files);
halcanary0bef17a2014-08-07 07:24:47 -0700230
halcanary8b2cb332014-08-11 13:08:27 -0700231 size_t maximumPathLength = 0;
232 for (int i = 0; i < files.count(); i ++) {
233 SkString basename = SkOSPath::Basename(files[i].c_str());
234 maximumPathLength = SkTMax(maximumPathLength, basename.size());
halcanary0d154ee2014-08-11 11:33:51 -0700235 }
236
halcanary8b2cb332014-08-11 13:08:27 -0700237 int failures = 0;
238 for (int i = 0; i < files.count(); i ++) {
239 SkString basename = SkOSPath::Basename(files[i].c_str());
240
241 SkFILEStream inputStream;
242 inputStream.setPath(files[i].c_str());
243 if (!inputStream.isValid()) {
244 SkDebugf("Could not open file %s\n", files[i].c_str());
245 ++failures;
246 continue;
247 }
248
249 SkAutoTUnref<SkPicture> picture(
250 SkPicture::CreateFromStream(&inputStream));
251 if (NULL == picture.get()) {
252 SkDebugf("Could not read an SkPicture from %s\n",
253 files[i].c_str());
254 ++failures;
255 continue;
256 }
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700257 SkDebugf("[%f,%f,%f,%f] %-*s",
258 picture->cullRect().fLeft, picture->cullRect().fTop,
259 picture->cullRect().fRight, picture->cullRect().fBottom,
260 maximumPathLength, basename.c_str());
halcanary8b2cb332014-08-11 13:08:27 -0700261
262 SkAutoTDelete<SkWStream> stream(open_stream(outputDir, files[i]));
263 if (!stream.get()) {
264 ++failures;
265 continue;
266 }
267 if (!pdf_to_stream(picture, stream.get(), encode_to_dct_data)) {
268 SkDebugf("Error in PDF Serialization.");
269 ++failures;
270 }
271
mtkleinafb43792014-08-19 15:55:55 -0700272 int max_rss_mb = sk_tools::getMaxResidentSetSizeMB();
273 if (max_rss_mb >= 0) {
274 SkDebugf(" %4dM peak rss", max_rss_mb);
halcanary8b2cb332014-08-11 13:08:27 -0700275 }
276
277 SkDebugf("\n");
278 }
edisonn@google.comc319abe2012-11-01 19:52:38 +0000279 if (failures != 0) {
halcanary8b2cb332014-08-11 13:08:27 -0700280 SkDebugf("Failed to render %i of %i PDFs.\n", failures, files.count());
edisonn@google.comc319abe2012-11-01 19:52:38 +0000281 return 1;
282 }
edisonn@google.com184487c2013-03-08 18:00:16 +0000283
284 return 0;
285}
286
287int tool_main(int argc, char** argv);
288int tool_main(int argc, char** argv) {
289#ifdef SK_USE_CDB
290 setUpDebuggingFromArgs(argv[0]);
291 __try {
292#endif
293 return tool_main_core(argc, argv);
294#ifdef SK_USE_CDB
295 }
296 __except(GenerateDumpAndPrintCallstack(GetExceptionInformation()))
297 {
298 return -1;
299 }
300#endif
humper@google.comf2863292013-01-07 20:08:56 +0000301 return 0;
edisonn@google.comc319abe2012-11-01 19:52:38 +0000302}
edisonn@google.com9cf5b282012-11-01 20:07:33 +0000303#if !defined SK_BUILD_FOR_IOS
304int main(int argc, char * const argv[]) {
305 return tool_main(argc, (char**) argv);
306}
307#endif