blob: 8e791d9608dd9b0e1009e9e16aa8ab0cc419f971 [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));
169 SkCanvas* canvas = pdfDocument->beginPage(
170 SkIntToScalar(picture->width()),
171 SkIntToScalar(picture->height()));
172 canvas->drawPicture(picture);
173 canvas->flush();
174 return pdfDocument->close();
edisonn@google.comc319abe2012-11-01 19:52:38 +0000175}
176
halcanary0bef17a2014-08-07 07:24:47 -0700177static bool operator<(const SkString& a, const SkString& b) {
178 return strcmp(a.c_str(), b.c_str()) < 0;
179}
180
halcanary8b2cb332014-08-11 13:08:27 -0700181/**
182 * @param A list of directories or a skp files.
183 * @returns an alphabetical list of skp files.
edisonn@google.comc319abe2012-11-01 19:52:38 +0000184 */
halcanary8b2cb332014-08-11 13:08:27 -0700185static void process_input_files(
halcanary0bef17a2014-08-07 07:24:47 -0700186 const SkCommandLineFlags::StringArray& inputs,
halcanary8b2cb332014-08-11 13:08:27 -0700187 SkTArray<SkString>* files) {
halcanary0bef17a2014-08-07 07:24:47 -0700188 for (int i = 0; i < inputs.count(); i ++) {
189 const char* input = inputs[i];
190 if (sk_isdir(input)) {
191 SkOSFile::Iter iter(input, SKP_FILE_EXTENSION);
192 SkString inputFilename;
193 while (iter.next(&inputFilename)) {
194 if (!SkCommandLineFlags::ShouldSkip(
195 FLAGS_match, inputFilename.c_str())) {
halcanary8b2cb332014-08-11 13:08:27 -0700196 files->push_back(
halcanary0bef17a2014-08-07 07:24:47 -0700197 SkOSPath::Join(input, inputFilename.c_str()));
198 }
199 }
200 } else {
201 if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, input)) {
halcanary8b2cb332014-08-11 13:08:27 -0700202 files->push_back(SkString(input));
edisonn@google.comc319abe2012-11-01 19:52:38 +0000203 }
204 }
halcanary0bef17a2014-08-07 07:24:47 -0700205 }
halcanary8b2cb332014-08-11 13:08:27 -0700206 if (files->count() > 0) {
207 SkTQSort<SkString>(files->begin(), files->end() - 1);
halcanary0d154ee2014-08-11 11:33:51 -0700208 }
edisonn@google.comc319abe2012-11-01 19:52:38 +0000209}
210
halcanary8b2cb332014-08-11 13:08:27 -0700211/** For each input skp file, read it, render it to pdf and write. the
212 * output to a pdf file
213 */
edisonn@google.com184487c2013-03-08 18:00:16 +0000214int tool_main_core(int argc, char** argv);
215int tool_main_core(int argc, char** argv) {
halcanary0bef17a2014-08-07 07:24:47 -0700216 SkCommandLineFlags::Parse(argc, argv);
217
edisonn@google.comc319abe2012-11-01 19:52:38 +0000218 SkAutoGraphics ag;
edisonn@google.comc319abe2012-11-01 19:52:38 +0000219
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000220 SkString outputDir;
halcanary0bef17a2014-08-07 07:24:47 -0700221 if (FLAGS_outputDir.count() > 0) {
222 outputDir = FLAGS_outputDir[0];
halcanary8b2cb332014-08-11 13:08:27 -0700223 if (!sk_mkdir(outputDir.c_str())) {
224 SkDebugf("Unable to mkdir '%s'\n", outputDir.c_str());
225 return 1;
226 }
edisonn@google.comc319abe2012-11-01 19:52:38 +0000227 }
228
halcanary8b2cb332014-08-11 13:08:27 -0700229 SkTArray<SkString> files;
230 process_input_files(FLAGS_inputPaths, &files);
halcanary0bef17a2014-08-07 07:24:47 -0700231
halcanary8b2cb332014-08-11 13:08:27 -0700232 size_t maximumPathLength = 0;
233 for (int i = 0; i < files.count(); i ++) {
234 SkString basename = SkOSPath::Basename(files[i].c_str());
235 maximumPathLength = SkTMax(maximumPathLength, basename.size());
halcanary0d154ee2014-08-11 11:33:51 -0700236 }
237
halcanary8b2cb332014-08-11 13:08:27 -0700238 int failures = 0;
239 for (int i = 0; i < files.count(); i ++) {
240 SkString basename = SkOSPath::Basename(files[i].c_str());
241
242 SkFILEStream inputStream;
243 inputStream.setPath(files[i].c_str());
244 if (!inputStream.isValid()) {
245 SkDebugf("Could not open file %s\n", files[i].c_str());
246 ++failures;
247 continue;
248 }
249
250 SkAutoTUnref<SkPicture> picture(
251 SkPicture::CreateFromStream(&inputStream));
252 if (NULL == picture.get()) {
253 SkDebugf("Could not read an SkPicture from %s\n",
254 files[i].c_str());
255 ++failures;
256 continue;
257 }
258 SkDebugf("[%-4i %6i] %-*s", picture->width(), picture->height(),
259 maximumPathLength, basename.c_str());
260
261 SkAutoTDelete<SkWStream> stream(open_stream(outputDir, files[i]));
262 if (!stream.get()) {
263 ++failures;
264 continue;
265 }
266 if (!pdf_to_stream(picture, stream.get(), encode_to_dct_data)) {
267 SkDebugf("Error in PDF Serialization.");
268 ++failures;
269 }
270
mtkleinafb43792014-08-19 15:55:55 -0700271 int max_rss_mb = sk_tools::getMaxResidentSetSizeMB();
272 if (max_rss_mb >= 0) {
273 SkDebugf(" %4dM peak rss", max_rss_mb);
halcanary8b2cb332014-08-11 13:08:27 -0700274 }
275
276 SkDebugf("\n");
277 }
edisonn@google.comc319abe2012-11-01 19:52:38 +0000278 if (failures != 0) {
halcanary8b2cb332014-08-11 13:08:27 -0700279 SkDebugf("Failed to render %i of %i PDFs.\n", failures, files.count());
edisonn@google.comc319abe2012-11-01 19:52:38 +0000280 return 1;
281 }
edisonn@google.com184487c2013-03-08 18:00:16 +0000282
283 return 0;
284}
285
286int tool_main(int argc, char** argv);
287int tool_main(int argc, char** argv) {
288#ifdef SK_USE_CDB
289 setUpDebuggingFromArgs(argv[0]);
290 __try {
291#endif
292 return tool_main_core(argc, argv);
293#ifdef SK_USE_CDB
294 }
295 __except(GenerateDumpAndPrintCallstack(GetExceptionInformation()))
296 {
297 return -1;
298 }
299#endif
humper@google.comf2863292013-01-07 20:08:56 +0000300 return 0;
edisonn@google.comc319abe2012-11-01 19:52:38 +0000301}
edisonn@google.com9cf5b282012-11-01 20:07:33 +0000302#if !defined SK_BUILD_FOR_IOS
303int main(int argc, char * const argv[]) {
304 return tool_main(argc, (char**) argv);
305}
306#endif