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" |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 10 | #include "SkDevice.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" |
commit-bot@chromium.org | 608ea65 | 2013-10-03 19:29:21 +0000 | [diff] [blame] | 16 | #include "SkPixelRef.h" |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 17 | #include "SkStream.h" |
| 18 | #include "SkTArray.h" |
halcanary | 0bef17a | 2014-08-07 07:24:47 -0700 | [diff] [blame] | 19 | #include "SkTSort.h" |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 20 | #include "PdfRenderer.h" |
halcanary | 0d154ee | 2014-08-11 11:33:51 -0700 | [diff] [blame^] | 21 | #include "ProcStats.h" |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 22 | #include "picture_utils.h" |
| 23 | |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 24 | __SK_FORCE_IMAGE_DECODER_LINKING; |
| 25 | |
edisonn@google.com | 184487c | 2013-03-08 18:00:16 +0000 | [diff] [blame] | 26 | #ifdef SK_USE_CDB |
| 27 | #include "win_dbghelp.h" |
| 28 | #endif |
| 29 | |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 30 | /** |
| 31 | * render_pdfs |
| 32 | * |
| 33 | * Given list of directories and files to use as input, expects to find .skp |
| 34 | * files and it will convert them to .pdf files writing them in the output |
| 35 | * directory. |
| 36 | * |
| 37 | * Returns zero exit code if all .skp files were converted successfully, |
| 38 | * otherwise returns error code 1. |
| 39 | */ |
| 40 | |
| 41 | static const char PDF_FILE_EXTENSION[] = "pdf"; |
| 42 | static const char SKP_FILE_EXTENSION[] = "skp"; |
| 43 | |
halcanary | 0bef17a | 2014-08-07 07:24:47 -0700 | [diff] [blame] | 44 | |
| 45 | DEFINE_string2(inputPaths, r, "", |
| 46 | "A list of directories and files to use as input. " |
| 47 | "Files are expected to have the .skp extension."); |
| 48 | |
| 49 | DEFINE_string2(outputDir, w, "", |
| 50 | "Directory to write the rendered pdfs."); |
| 51 | |
| 52 | DEFINE_string2(match, m, "", |
| 53 | "[~][^]substring[$] [...] of filenames to run.\n" |
| 54 | "Multiple matches may be separated by spaces.\n" |
| 55 | "~ causes a matching file to always be skipped\n" |
| 56 | "^ requires the start of the file to match\n" |
| 57 | "$ requires the end of the file to match\n" |
| 58 | "^ and $ requires an exact match\n" |
| 59 | "If a file does not match any list entry,\n" |
| 60 | "it is skipped unless some list entry starts with ~"); |
| 61 | |
| 62 | DEFINE_int32(jpegQuality, 100, |
| 63 | "Encodes images in JPEG at quality level N, which can be in " |
| 64 | "range 0-100). N = -1 will disable JPEG compression. " |
| 65 | "Default is N = 100, maximum quality."); |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 66 | |
| 67 | /** Replaces the extension of a file. |
| 68 | * @param path File name whose extension will be changed. |
| 69 | * @param old_extension The old extension. |
| 70 | * @param new_extension The new extension. |
| 71 | * @returns false if the file did not has the expected extension. |
| 72 | * if false is returned, contents of path are undefined. |
| 73 | */ |
| 74 | static bool replace_filename_extension(SkString* path, |
| 75 | const char old_extension[], |
| 76 | const char new_extension[]) { |
| 77 | if (path->endsWith(old_extension)) { |
| 78 | path->remove(path->size() - strlen(old_extension), |
| 79 | strlen(old_extension)); |
| 80 | if (!path->endsWith(".")) { |
| 81 | return false; |
| 82 | } |
| 83 | path->append(new_extension); |
| 84 | return true; |
| 85 | } |
| 86 | return false; |
| 87 | } |
| 88 | |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 89 | // the size_t* parameter is deprecated, so we ignore it |
| 90 | static SkData* encode_to_dct_data(size_t*, const SkBitmap& bitmap) { |
halcanary | 0bef17a | 2014-08-07 07:24:47 -0700 | [diff] [blame] | 91 | if (FLAGS_jpegQuality == -1) { |
commit-bot@chromium.org | 608ea65 | 2013-10-03 19:29:21 +0000 | [diff] [blame] | 92 | return NULL; |
| 93 | } |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 94 | |
commit-bot@chromium.org | 608ea65 | 2013-10-03 19:29:21 +0000 | [diff] [blame] | 95 | SkBitmap bm = bitmap; |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 96 | #if defined(SK_BUILD_FOR_MAC) |
commit-bot@chromium.org | 608ea65 | 2013-10-03 19:29:21 +0000 | [diff] [blame] | 97 | // Workaround bug #1043 where bitmaps with referenced pixels cause |
| 98 | // CGImageDestinationFinalize to crash |
| 99 | SkBitmap copy; |
commit-bot@chromium.org | d5f032d | 2014-02-24 18:51:43 +0000 | [diff] [blame] | 100 | bitmap.deepCopyTo(©); |
commit-bot@chromium.org | 608ea65 | 2013-10-03 19:29:21 +0000 | [diff] [blame] | 101 | bm = copy; |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 102 | #endif |
| 103 | |
halcanary | 0bef17a | 2014-08-07 07:24:47 -0700 | [diff] [blame] | 104 | return SkImageEncoder::EncodeData( |
| 105 | bm, SkImageEncoder::kJPEG_Type, FLAGS_jpegQuality); |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 106 | } |
| 107 | |
skia.committer@gmail.com | 760f2d9 | 2012-11-02 02:01:24 +0000 | [diff] [blame] | 108 | /** Builds the output filename. path = dir/name, and it replaces expected |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 109 | * .skp extension with .pdf extention. |
| 110 | * @param path Output filename. |
| 111 | * @param name The name of the file. |
| 112 | * @returns false if the file did not has the expected extension. |
| 113 | * if false is returned, contents of path are undefined. |
| 114 | */ |
| 115 | static bool make_output_filepath(SkString* path, const SkString& dir, |
| 116 | const SkString& name) { |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 117 | *path = SkOSPath::Join(dir.c_str(), name.c_str()); |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 118 | return replace_filename_extension(path, |
| 119 | SKP_FILE_EXTENSION, |
| 120 | PDF_FILE_EXTENSION); |
| 121 | } |
| 122 | |
| 123 | /** Write the output of pdf renderer to a file. |
| 124 | * @param outputDir Output dir. |
| 125 | * @param inputFilename The skp file that was read. |
| 126 | * @param renderer The object responsible to write the pdf file. |
| 127 | */ |
commit-bot@chromium.org | 5e00989 | 2013-10-14 13:42:12 +0000 | [diff] [blame] | 128 | static SkWStream* open_stream(const SkString& outputDir, |
| 129 | const SkString& inputFilename) { |
edisonn@google.com | 4fa566b | 2013-01-11 20:30:41 +0000 | [diff] [blame] | 130 | if (outputDir.isEmpty()) { |
commit-bot@chromium.org | 5e00989 | 2013-10-14 13:42:12 +0000 | [diff] [blame] | 131 | return SkNEW(SkDynamicMemoryWStream); |
edisonn@google.com | 4fa566b | 2013-01-11 20:30:41 +0000 | [diff] [blame] | 132 | } |
| 133 | |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 134 | SkString outputPath; |
| 135 | if (!make_output_filepath(&outputPath, outputDir, inputFilename)) { |
commit-bot@chromium.org | 5e00989 | 2013-10-14 13:42:12 +0000 | [diff] [blame] | 136 | return NULL; |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 137 | } |
edisonn@google.com | 4fa566b | 2013-01-11 20:30:41 +0000 | [diff] [blame] | 138 | |
commit-bot@chromium.org | 5e00989 | 2013-10-14 13:42:12 +0000 | [diff] [blame] | 139 | SkFILEWStream* stream = SkNEW_ARGS(SkFILEWStream, (outputPath.c_str())); |
| 140 | if (!stream->isValid()) { |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 141 | 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] | 142 | return NULL; |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 143 | } |
edisonn@google.com | 4fa566b | 2013-01-11 20:30:41 +0000 | [diff] [blame] | 144 | |
commit-bot@chromium.org | 5e00989 | 2013-10-14 13:42:12 +0000 | [diff] [blame] | 145 | return stream; |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /** Reads an skp file, renders it to pdf and writes the output to a pdf file |
| 149 | * @param inputPath The skp file to be read. |
| 150 | * @param outputDir Output dir. |
| 151 | * @param renderer The object responsible to render the skp object into pdf. |
| 152 | */ |
| 153 | static bool render_pdf(const SkString& inputPath, const SkString& outputDir, |
| 154 | sk_tools::PdfRenderer& renderer) { |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 155 | SkString inputFilename = SkOSPath::Basename(inputPath.c_str()); |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 156 | |
| 157 | SkFILEStream inputStream; |
| 158 | inputStream.setPath(inputPath.c_str()); |
| 159 | if (!inputStream.isValid()) { |
| 160 | SkDebugf("Could not open file %s\n", inputPath.c_str()); |
| 161 | return false; |
| 162 | } |
| 163 | |
halcanary | f91b47f | 2014-08-01 11:54:48 -0700 | [diff] [blame] | 164 | SkAutoTUnref<SkPicture> picture(SkPicture::CreateFromStream(&inputStream)); |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 165 | |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 166 | if (NULL == picture.get()) { |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 167 | SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str()); |
| 168 | return false; |
| 169 | } |
| 170 | |
halcanary | 0bef17a | 2014-08-07 07:24:47 -0700 | [diff] [blame] | 171 | SkDebugf("exporting... [%-4i %6i] %s\n", |
| 172 | picture->width(), picture->height(), inputPath.c_str()); |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 173 | |
commit-bot@chromium.org | 5e00989 | 2013-10-14 13:42:12 +0000 | [diff] [blame] | 174 | SkWStream* stream(open_stream(outputDir, inputFilename)); |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 175 | |
commit-bot@chromium.org | 5e00989 | 2013-10-14 13:42:12 +0000 | [diff] [blame] | 176 | if (!stream) { |
| 177 | return false; |
| 178 | } |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 179 | |
commit-bot@chromium.org | 5e00989 | 2013-10-14 13:42:12 +0000 | [diff] [blame] | 180 | renderer.init(picture, stream); |
| 181 | |
| 182 | bool success = renderer.render(); |
| 183 | SkDELETE(stream); |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 184 | |
| 185 | renderer.end(); |
commit-bot@chromium.org | 5e00989 | 2013-10-14 13:42:12 +0000 | [diff] [blame] | 186 | |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 187 | return success; |
| 188 | } |
| 189 | |
halcanary | 0bef17a | 2014-08-07 07:24:47 -0700 | [diff] [blame] | 190 | static bool operator<(const SkString& a, const SkString& b) { |
| 191 | return strcmp(a.c_str(), b.c_str()) < 0; |
| 192 | } |
| 193 | |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 194 | /** For each file in the directory or for the file passed in input, call |
| 195 | * render_pdf. |
| 196 | * @param input A directory or an skp file. |
| 197 | * @param outputDir Output dir. |
| 198 | * @param renderer The object responsible to render the skp object into pdf. |
| 199 | */ |
halcanary | 0bef17a | 2014-08-07 07:24:47 -0700 | [diff] [blame] | 200 | static int process_input( |
| 201 | const SkCommandLineFlags::StringArray& inputs, |
| 202 | const SkString& outputDir, |
| 203 | sk_tools::PdfRenderer& renderer) { |
| 204 | SkTArray<SkString> files; |
| 205 | for (int i = 0; i < inputs.count(); i ++) { |
| 206 | const char* input = inputs[i]; |
| 207 | if (sk_isdir(input)) { |
| 208 | SkOSFile::Iter iter(input, SKP_FILE_EXTENSION); |
| 209 | SkString inputFilename; |
| 210 | while (iter.next(&inputFilename)) { |
| 211 | if (!SkCommandLineFlags::ShouldSkip( |
| 212 | FLAGS_match, inputFilename.c_str())) { |
| 213 | files.push_back( |
| 214 | SkOSPath::Join(input, inputFilename.c_str())); |
| 215 | } |
| 216 | } |
| 217 | } else { |
| 218 | if (!SkCommandLineFlags::ShouldSkip(FLAGS_match, input)) { |
| 219 | files.push_back(SkString(input)); |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 220 | } |
| 221 | } |
halcanary | 0bef17a | 2014-08-07 07:24:47 -0700 | [diff] [blame] | 222 | } |
halcanary | 0d154ee | 2014-08-11 11:33:51 -0700 | [diff] [blame^] | 223 | if (files.count() > 0) { |
| 224 | SkTQSort<SkString>(files.begin(), files.end() - 1); |
| 225 | } |
halcanary | 0bef17a | 2014-08-07 07:24:47 -0700 | [diff] [blame] | 226 | int failures = 0; |
| 227 | for (int i = 0; i < files.count(); i ++) { |
| 228 | if (!render_pdf(files[i], outputDir, renderer)) { |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 229 | ++failures; |
| 230 | } |
| 231 | } |
| 232 | return failures; |
| 233 | } |
| 234 | |
edisonn@google.com | 184487c | 2013-03-08 18:00:16 +0000 | [diff] [blame] | 235 | int tool_main_core(int argc, char** argv); |
| 236 | int tool_main_core(int argc, char** argv) { |
halcanary | 0bef17a | 2014-08-07 07:24:47 -0700 | [diff] [blame] | 237 | SkCommandLineFlags::Parse(argc, argv); |
| 238 | |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 239 | SkAutoGraphics ag; |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 240 | |
| 241 | SkAutoTUnref<sk_tools::PdfRenderer> |
commit-bot@chromium.org | 608ea65 | 2013-10-03 19:29:21 +0000 | [diff] [blame] | 242 | renderer(SkNEW_ARGS(sk_tools::SimplePdfRenderer, (encode_to_dct_data))); |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 243 | SkASSERT(renderer.get()); |
| 244 | |
edisonn@google.com | 4fa566b | 2013-01-11 20:30:41 +0000 | [diff] [blame] | 245 | SkString outputDir; |
halcanary | 0bef17a | 2014-08-07 07:24:47 -0700 | [diff] [blame] | 246 | if (FLAGS_outputDir.count() > 0) { |
| 247 | outputDir = FLAGS_outputDir[0]; |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 248 | } |
| 249 | |
halcanary | 0bef17a | 2014-08-07 07:24:47 -0700 | [diff] [blame] | 250 | int failures = process_input(FLAGS_inputPaths, outputDir, *renderer); |
| 251 | |
halcanary | 0d154ee | 2014-08-11 11:33:51 -0700 | [diff] [blame^] | 252 | int max_rss_kb = sk_tools::getMaxResidentSetSizeKB(); |
| 253 | if (max_rss_kb >= 0) { |
| 254 | SkDebugf("%4dM peak ResidentSetSize\n", max_rss_kb / 1024); |
| 255 | } |
| 256 | |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 257 | if (failures != 0) { |
| 258 | SkDebugf("Failed to render %i PDFs.\n", failures); |
| 259 | return 1; |
| 260 | } |
edisonn@google.com | 184487c | 2013-03-08 18:00:16 +0000 | [diff] [blame] | 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | int tool_main(int argc, char** argv); |
| 266 | int tool_main(int argc, char** argv) { |
| 267 | #ifdef SK_USE_CDB |
| 268 | setUpDebuggingFromArgs(argv[0]); |
| 269 | __try { |
| 270 | #endif |
| 271 | return tool_main_core(argc, argv); |
| 272 | #ifdef SK_USE_CDB |
| 273 | } |
| 274 | __except(GenerateDumpAndPrintCallstack(GetExceptionInformation())) |
| 275 | { |
| 276 | return -1; |
| 277 | } |
| 278 | #endif |
humper@google.com | f286329 | 2013-01-07 20:08:56 +0000 | [diff] [blame] | 279 | return 0; |
edisonn@google.com | c319abe | 2012-11-01 19:52:38 +0000 | [diff] [blame] | 280 | } |
| 281 | |
edisonn@google.com | 9cf5b28 | 2012-11-01 20:07:33 +0000 | [diff] [blame] | 282 | #if !defined SK_BUILD_FOR_IOS |
| 283 | int main(int argc, char * const argv[]) { |
| 284 | return tool_main(argc, (char**) argv); |
| 285 | } |
| 286 | #endif |