edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 1 | /* |
| 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" |
halcanary | 0bef17a | 2014-08-07 07:24:47 -0700 | [diff] [blame] | 9 | #include "SkCommandLineFlags.h" |
halcanary | 8b2cb33 | 2014-08-11 13:08:27 -0700 | [diff] [blame] | 10 | #include "SkDocument.h" |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 11 | #include "SkForceLinking.h" |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 12 | #include "SkGraphics.h" |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 13 | #include "SkImageEncoder.h" |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 14 | #include "SkOSFile.h" |
| 15 | #include "SkPicture.h" |
| 16 | #include "SkStream.h" |
| 17 | #include "SkTArray.h" |
halcanary | 0bef17a | 2014-08-07 07:24:47 -0700 | [diff] [blame] | 18 | #include "SkTSort.h" |
halcanary | 0d154ee | 2014-08-11 11:33:51 -0700 | [diff] [blame] | 19 | #include "ProcStats.h" |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 20 | |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 21 | __SK_FORCE_IMAGE_DECODER_LINKING; |
| 22 | |
edisonn@google.com | 184487c | 2013-03-08 18:00:16 +0000 | [diff] [blame] | 23 | #ifdef SK_USE_CDB |
| 24 | #include "win_dbghelp.h" |
| 25 | #endif |
| 26 | |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 27 | /** |
| 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 | |
| 38 | static const char PDF_FILE_EXTENSION[] = "pdf"; |
| 39 | static const char SKP_FILE_EXTENSION[] = "skp"; |
| 40 | |
halcanary | 0bef17a | 2014-08-07 07:24:47 -0700 | [diff] [blame] | 41 | |
| 42 | DEFINE_string2(inputPaths, r, "", |
| 43 | "A list of directories and files to use as input. " |
| 44 | "Files are expected to have the .skp extension."); |
| 45 | |
| 46 | DEFINE_string2(outputDir, w, "", |
| 47 | "Directory to write the rendered pdfs."); |
| 48 | |
| 49 | DEFINE_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 | |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 59 | /** Replaces the extension of a file. |
| 60 | * @param path File name whose extension will be changed. |
| 61 | * @param old_extension The old extension. |
| 62 | * @param new_extension The new extension. |
| 63 | * @returns false if the file did not has the expected extension. |
| 64 | * if false is returned, contents of path are undefined. |
| 65 | */ |
| 66 | static bool replace_filename_extension(SkString* path, |
| 67 | const char old_extension[], |
| 68 | const char new_extension[]) { |
| 69 | if (path->endsWith(old_extension)) { |
| 70 | path->remove(path->size() - strlen(old_extension), |
| 71 | strlen(old_extension)); |
| 72 | if (!path->endsWith(".")) { |
| 73 | return false; |
| 74 | } |
| 75 | path->append(new_extension); |
| 76 | return true; |
| 77 | } |
| 78 | return false; |
| 79 | } |
| 80 | |
skia.committer@gmail.com | 760f2d9 | 2012-11-02 02:01:24 +0000 | [diff] [blame] | 81 | /** Builds the output filename. path = dir/name, and it replaces expected |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 82 | * .skp extension with .pdf extention. |
| 83 | * @param path Output filename. |
| 84 | * @param name The name of the file. |
| 85 | * @returns false if the file did not has the expected extension. |
| 86 | * if false is returned, contents of path are undefined. |
| 87 | */ |
| 88 | static bool make_output_filepath(SkString* path, const SkString& dir, |
| 89 | const SkString& name) { |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 90 | *path = SkOSPath::Join(dir.c_str(), name.c_str()); |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 91 | return replace_filename_extension(path, |
| 92 | SKP_FILE_EXTENSION, |
| 93 | PDF_FILE_EXTENSION); |
| 94 | } |
| 95 | |
halcanary | 8b2cb33 | 2014-08-11 13:08:27 -0700 | [diff] [blame] | 96 | namespace { |
| 97 | // This is a write-only stream. |
| 98 | class NullWStream : public SkWStream { |
| 99 | public: |
| 100 | NullWStream() : fBytesWritten(0) { } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 101 | bool write(const void*, size_t size) override { |
halcanary | 8b2cb33 | 2014-08-11 13:08:27 -0700 | [diff] [blame] | 102 | fBytesWritten += size; |
| 103 | return true; |
| 104 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 105 | size_t bytesWritten() const override { return fBytesWritten; } |
halcanary | 8b2cb33 | 2014-08-11 13:08:27 -0700 | [diff] [blame] | 106 | size_t fBytesWritten; |
| 107 | }; |
| 108 | } // namespace |
| 109 | |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 110 | /** Write the output of pdf renderer to a file. |
| 111 | * @param outputDir Output dir. |
| 112 | * @param inputFilename The skp file that was read. |
| 113 | * @param renderer The object responsible to write the pdf file. |
| 114 | */ |
commit-bot@chromium.org | 5e00989 | 2013-10-14 13:42:12 +0000 | [diff] [blame] | 115 | static SkWStream* open_stream(const SkString& outputDir, |
| 116 | const SkString& inputFilename) { |
edisonn@google.com | 4fa566b | 2013-01-11 20:30:41 +0000 | [diff] [blame] | 117 | if (outputDir.isEmpty()) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame^] | 118 | return new NullWStream; |
edisonn@google.com | 4fa566b | 2013-01-11 20:30:41 +0000 | [diff] [blame] | 119 | } |
| 120 | |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 121 | SkString outputPath; |
| 122 | if (!make_output_filepath(&outputPath, outputDir, inputFilename)) { |
commit-bot@chromium.org | 5e00989 | 2013-10-14 13:42:12 +0000 | [diff] [blame] | 123 | return NULL; |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 124 | } |
edisonn@google.com | 4fa566b | 2013-01-11 20:30:41 +0000 | [diff] [blame] | 125 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame^] | 126 | SkAutoTDelete<SkFILEWStream> stream(new SkFILEWStream(outputPath.c_str())); |
halcanary | 8b2cb33 | 2014-08-11 13:08:27 -0700 | [diff] [blame] | 127 | if (!stream.get() || !stream->isValid()) { |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 128 | SkDebugf("Could not write to file %s\n", outputPath.c_str()); |
commit-bot@chromium.org | 5e00989 | 2013-10-14 13:42:12 +0000 | [diff] [blame] | 129 | return NULL; |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 130 | } |
edisonn@google.com | 4fa566b | 2013-01-11 20:30:41 +0000 | [diff] [blame] | 131 | |
halcanary | 8b2cb33 | 2014-08-11 13:08:27 -0700 | [diff] [blame] | 132 | return stream.detach(); |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 133 | } |
| 134 | |
halcanary | 8b2cb33 | 2014-08-11 13:08:27 -0700 | [diff] [blame] | 135 | /** |
| 136 | * Given a SkPicture, write a one-page PDF document to the given |
| 137 | * output, using the provided encoder. |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 138 | */ |
halcanary | 8b2cb33 | 2014-08-11 13:08:27 -0700 | [diff] [blame] | 139 | static bool pdf_to_stream(SkPicture* picture, |
halcanary | 8c92dc1 | 2015-02-19 18:50:05 -0800 | [diff] [blame] | 140 | SkWStream* output) { |
halcanary | 8b2cb33 | 2014-08-11 13:08:27 -0700 | [diff] [blame] | 141 | SkAutoTUnref<SkDocument> pdfDocument( |
halcanary | 8c92dc1 | 2015-02-19 18:50:05 -0800 | [diff] [blame] | 142 | SkDocument::CreatePDF(output)); |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 143 | SkCanvas* canvas = pdfDocument->beginPage(picture->cullRect().width(), |
| 144 | picture->cullRect().height()); |
halcanary | 8b2cb33 | 2014-08-11 13:08:27 -0700 | [diff] [blame] | 145 | canvas->drawPicture(picture); |
| 146 | canvas->flush(); |
| 147 | return pdfDocument->close(); |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 148 | } |
| 149 | |
halcanary | 0bef17a | 2014-08-07 07:24:47 -0700 | [diff] [blame] | 150 | static bool operator<(const SkString& a, const SkString& b) { |
| 151 | return strcmp(a.c_str(), b.c_str()) < 0; |
| 152 | } |
| 153 | |
halcanary | 8b2cb33 | 2014-08-11 13:08:27 -0700 | [diff] [blame] | 154 | /** |
| 155 | * @param A list of directories or a skp files. |
| 156 | * @returns an alphabetical list of skp files. |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 157 | */ |
halcanary | 8b2cb33 | 2014-08-11 13:08:27 -0700 | [diff] [blame] | 158 | static void process_input_files( |
halcanary | 0bef17a | 2014-08-07 07:24:47 -0700 | [diff] [blame] | 159 | const SkCommandLineFlags::StringArray& inputs, |
halcanary | 8b2cb33 | 2014-08-11 13:08:27 -0700 | [diff] [blame] | 160 | SkTArray<SkString>* files) { |
halcanary | 0bef17a | 2014-08-07 07:24:47 -0700 | [diff] [blame] | 161 | for (int i = 0; i < inputs.count(); i ++) { |
| 162 | const char* input = inputs[i]; |
| 163 | if (sk_isdir(input)) { |
| 164 | SkOSFile::Iter iter(input, SKP_FILE_EXTENSION); |
| 165 | SkString inputFilename; |
| 166 | while (iter.next(&inputFilename)) { |
| 167 | if (!SkCommandLineFlags::ShouldSkip( |
| 168 | FLAGS_match, inputFilename.c_str())) { |
halcanary | 8b2cb33 | 2014-08-11 13:08:27 -0700 | [diff] [blame] | 169 | files->push_back( |
halcanary | 0bef17a | 2014-08-07 07:24:47 -0700 | [diff] [blame] | 170 | SkOSPath::Join(input, inputFilename.c_str())); |
| 171 | } |
| 172 | } |
| 173 | } else { |
| 174 | if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, input)) { |
halcanary | 8b2cb33 | 2014-08-11 13:08:27 -0700 | [diff] [blame] | 175 | files->push_back(SkString(input)); |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
halcanary | 0bef17a | 2014-08-07 07:24:47 -0700 | [diff] [blame] | 178 | } |
halcanary | 8b2cb33 | 2014-08-11 13:08:27 -0700 | [diff] [blame] | 179 | if (files->count() > 0) { |
| 180 | SkTQSort<SkString>(files->begin(), files->end() - 1); |
halcanary | 0d154ee | 2014-08-11 11:33:51 -0700 | [diff] [blame] | 181 | } |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 182 | } |
| 183 | |
halcanary | 8b2cb33 | 2014-08-11 13:08:27 -0700 | [diff] [blame] | 184 | /** For each input skp file, read it, render it to pdf and write. the |
| 185 | * output to a pdf file |
| 186 | */ |
edisonn@google.com | 184487c | 2013-03-08 18:00:16 +0000 | [diff] [blame] | 187 | int tool_main_core(int argc, char** argv); |
| 188 | int tool_main_core(int argc, char** argv) { |
halcanary | 0bef17a | 2014-08-07 07:24:47 -0700 | [diff] [blame] | 189 | SkCommandLineFlags::Parse(argc, argv); |
| 190 | |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 191 | SkAutoGraphics ag; |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 192 | |
edisonn@google.com | 4fa566b | 2013-01-11 20:30:41 +0000 | [diff] [blame] | 193 | SkString outputDir; |
halcanary | 0bef17a | 2014-08-07 07:24:47 -0700 | [diff] [blame] | 194 | if (FLAGS_outputDir.count() > 0) { |
| 195 | outputDir = FLAGS_outputDir[0]; |
halcanary | 8b2cb33 | 2014-08-11 13:08:27 -0700 | [diff] [blame] | 196 | if (!sk_mkdir(outputDir.c_str())) { |
| 197 | SkDebugf("Unable to mkdir '%s'\n", outputDir.c_str()); |
| 198 | return 1; |
| 199 | } |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 200 | } |
| 201 | |
halcanary | 8b2cb33 | 2014-08-11 13:08:27 -0700 | [diff] [blame] | 202 | SkTArray<SkString> files; |
| 203 | process_input_files(FLAGS_inputPaths, &files); |
halcanary | 0bef17a | 2014-08-07 07:24:47 -0700 | [diff] [blame] | 204 | |
halcanary | 8b2cb33 | 2014-08-11 13:08:27 -0700 | [diff] [blame] | 205 | size_t maximumPathLength = 0; |
| 206 | for (int i = 0; i < files.count(); i ++) { |
| 207 | SkString basename = SkOSPath::Basename(files[i].c_str()); |
| 208 | maximumPathLength = SkTMax(maximumPathLength, basename.size()); |
halcanary | 0d154ee | 2014-08-11 11:33:51 -0700 | [diff] [blame] | 209 | } |
| 210 | |
halcanary | 8b2cb33 | 2014-08-11 13:08:27 -0700 | [diff] [blame] | 211 | int failures = 0; |
| 212 | for (int i = 0; i < files.count(); i ++) { |
| 213 | SkString basename = SkOSPath::Basename(files[i].c_str()); |
| 214 | |
| 215 | SkFILEStream inputStream; |
| 216 | inputStream.setPath(files[i].c_str()); |
| 217 | if (!inputStream.isValid()) { |
| 218 | SkDebugf("Could not open file %s\n", files[i].c_str()); |
| 219 | ++failures; |
| 220 | continue; |
| 221 | } |
| 222 | |
| 223 | SkAutoTUnref<SkPicture> picture( |
| 224 | SkPicture::CreateFromStream(&inputStream)); |
| 225 | if (NULL == picture.get()) { |
| 226 | SkDebugf("Could not read an SkPicture from %s\n", |
| 227 | files[i].c_str()); |
| 228 | ++failures; |
| 229 | continue; |
| 230 | } |
halcanary | 5bb9700 | 2014-10-16 10:32:52 -0700 | [diff] [blame] | 231 | SkDebugf("[%6g %6g %6g %6g] %-*s", |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 232 | picture->cullRect().fLeft, picture->cullRect().fTop, |
| 233 | picture->cullRect().fRight, picture->cullRect().fBottom, |
| 234 | maximumPathLength, basename.c_str()); |
halcanary | 8b2cb33 | 2014-08-11 13:08:27 -0700 | [diff] [blame] | 235 | |
| 236 | SkAutoTDelete<SkWStream> stream(open_stream(outputDir, files[i])); |
| 237 | if (!stream.get()) { |
| 238 | ++failures; |
| 239 | continue; |
| 240 | } |
halcanary | 8c92dc1 | 2015-02-19 18:50:05 -0800 | [diff] [blame] | 241 | if (!pdf_to_stream(picture, stream.get())) { |
halcanary | 8b2cb33 | 2014-08-11 13:08:27 -0700 | [diff] [blame] | 242 | SkDebugf("Error in PDF Serialization."); |
| 243 | ++failures; |
| 244 | } |
| 245 | |
mtklein | afb4379 | 2014-08-19 15:55:55 -0700 | [diff] [blame] | 246 | int max_rss_mb = sk_tools::getMaxResidentSetSizeMB(); |
| 247 | if (max_rss_mb >= 0) { |
| 248 | SkDebugf(" %4dM peak rss", max_rss_mb); |
halcanary | 8b2cb33 | 2014-08-11 13:08:27 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | SkDebugf("\n"); |
| 252 | } |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 253 | if (failures != 0) { |
halcanary | 8b2cb33 | 2014-08-11 13:08:27 -0700 | [diff] [blame] | 254 | SkDebugf("Failed to render %i of %i PDFs.\n", failures, files.count()); |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 255 | return 1; |
| 256 | } |
edisonn@google.com | 184487c | 2013-03-08 18:00:16 +0000 | [diff] [blame] | 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | int tool_main(int argc, char** argv); |
| 262 | int tool_main(int argc, char** argv) { |
| 263 | #ifdef SK_USE_CDB |
| 264 | setUpDebuggingFromArgs(argv[0]); |
| 265 | __try { |
| 266 | #endif |
| 267 | return tool_main_core(argc, argv); |
| 268 | #ifdef SK_USE_CDB |
| 269 | } |
| 270 | __except(GenerateDumpAndPrintCallstack(GetExceptionInformation())) |
| 271 | { |
| 272 | return -1; |
| 273 | } |
| 274 | #endif |
humper@google.com | f286329 | 2013-01-07 20:08:56 +0000 | [diff] [blame] | 275 | return 0; |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 276 | } |
edisonn@google.com | 9cf5b28 | 2012-11-01 20:07:33 +0000 | [diff] [blame] | 277 | #if !defined SK_BUILD_FOR_IOS |
| 278 | int main(int argc, char * const argv[]) { |
| 279 | return tool_main(argc, (char**) argv); |
| 280 | } |
| 281 | #endif |