blob: 34e5e58f53d0eac4949c4e236f02b3e5c47e9133 [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"
edisonn@google.comc319abe2012-11-01 19:52:38 +000010#include "SkDevice.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"
commit-bot@chromium.org608ea652013-10-03 19:29:21 +000016#include "SkPixelRef.h"
edisonn@google.comc319abe2012-11-01 19:52:38 +000017#include "SkStream.h"
18#include "SkTArray.h"
halcanary0bef17a2014-08-07 07:24:47 -070019#include "SkTSort.h"
edisonn@google.comc319abe2012-11-01 19:52:38 +000020#include "PdfRenderer.h"
halcanary0d154ee2014-08-11 11:33:51 -070021#include "ProcStats.h"
edisonn@google.comc319abe2012-11-01 19:52:38 +000022#include "picture_utils.h"
23
scroggo@google.com7def5e12013-05-31 14:00:10 +000024__SK_FORCE_IMAGE_DECODER_LINKING;
25
edisonn@google.com184487c2013-03-08 18:00:16 +000026#ifdef SK_USE_CDB
27#include "win_dbghelp.h"
28#endif
29
edisonn@google.comc319abe2012-11-01 19:52:38 +000030/**
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
41static const char PDF_FILE_EXTENSION[] = "pdf";
42static const char SKP_FILE_EXTENSION[] = "skp";
43
halcanary0bef17a2014-08-07 07:24:47 -070044
45DEFINE_string2(inputPaths, r, "",
46 "A list of directories and files to use as input. "
47 "Files are expected to have the .skp extension.");
48
49DEFINE_string2(outputDir, w, "",
50 "Directory to write the rendered pdfs.");
51
52DEFINE_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
62DEFINE_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.comc319abe2012-11-01 19:52:38 +000066
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 */
74static 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.com672588b2014-01-08 15:42:01 +000089// the size_t* parameter is deprecated, so we ignore it
90static SkData* encode_to_dct_data(size_t*, const SkBitmap& bitmap) {
halcanary0bef17a2014-08-07 07:24:47 -070091 if (FLAGS_jpegQuality == -1) {
commit-bot@chromium.org608ea652013-10-03 19:29:21 +000092 return NULL;
93 }
edisonn@google.comd9dfa182013-04-24 13:01:01 +000094
commit-bot@chromium.org608ea652013-10-03 19:29:21 +000095 SkBitmap bm = bitmap;
edisonn@google.comd9dfa182013-04-24 13:01:01 +000096#if defined(SK_BUILD_FOR_MAC)
commit-bot@chromium.org608ea652013-10-03 19:29:21 +000097 // Workaround bug #1043 where bitmaps with referenced pixels cause
98 // CGImageDestinationFinalize to crash
99 SkBitmap copy;
commit-bot@chromium.orgd5f032d2014-02-24 18:51:43 +0000100 bitmap.deepCopyTo(&copy);
commit-bot@chromium.org608ea652013-10-03 19:29:21 +0000101 bm = copy;
edisonn@google.comd9dfa182013-04-24 13:01:01 +0000102#endif
103
halcanary0bef17a2014-08-07 07:24:47 -0700104 return SkImageEncoder::EncodeData(
105 bm, SkImageEncoder::kJPEG_Type, FLAGS_jpegQuality);
edisonn@google.comd9dfa182013-04-24 13:01:01 +0000106}
107
skia.committer@gmail.com760f2d92012-11-02 02:01:24 +0000108/** Builds the output filename. path = dir/name, and it replaces expected
edisonn@google.comc319abe2012-11-01 19:52:38 +0000109 * .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 */
115static bool make_output_filepath(SkString* path, const SkString& dir,
116 const SkString& name) {
tfarinaa8e2e152014-07-28 19:26:58 -0700117 *path = SkOSPath::Join(dir.c_str(), name.c_str());
edisonn@google.comc319abe2012-11-01 19:52:38 +0000118 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.org5e009892013-10-14 13:42:12 +0000128static SkWStream* open_stream(const SkString& outputDir,
129 const SkString& inputFilename) {
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000130 if (outputDir.isEmpty()) {
commit-bot@chromium.org5e009892013-10-14 13:42:12 +0000131 return SkNEW(SkDynamicMemoryWStream);
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000132 }
133
edisonn@google.comc319abe2012-11-01 19:52:38 +0000134 SkString outputPath;
135 if (!make_output_filepath(&outputPath, outputDir, inputFilename)) {
commit-bot@chromium.org5e009892013-10-14 13:42:12 +0000136 return NULL;
edisonn@google.comc319abe2012-11-01 19:52:38 +0000137 }
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000138
commit-bot@chromium.org5e009892013-10-14 13:42:12 +0000139 SkFILEWStream* stream = SkNEW_ARGS(SkFILEWStream, (outputPath.c_str()));
140 if (!stream->isValid()) {
edisonn@google.comc319abe2012-11-01 19:52:38 +0000141 SkDebugf("Could not write to file %s\n", outputPath.c_str());
commit-bot@chromium.org5e009892013-10-14 13:42:12 +0000142 return NULL;
edisonn@google.comc319abe2012-11-01 19:52:38 +0000143 }
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000144
commit-bot@chromium.org5e009892013-10-14 13:42:12 +0000145 return stream;
edisonn@google.comc319abe2012-11-01 19:52:38 +0000146}
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 */
153static bool render_pdf(const SkString& inputPath, const SkString& outputDir,
154 sk_tools::PdfRenderer& renderer) {
tfarinaa8e2e152014-07-28 19:26:58 -0700155 SkString inputFilename = SkOSPath::Basename(inputPath.c_str());
edisonn@google.comc319abe2012-11-01 19:52:38 +0000156
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
halcanaryf91b47f2014-08-01 11:54:48 -0700164 SkAutoTUnref<SkPicture> picture(SkPicture::CreateFromStream(&inputStream));
edisonn@google.comc319abe2012-11-01 19:52:38 +0000165
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000166 if (NULL == picture.get()) {
edisonn@google.comc319abe2012-11-01 19:52:38 +0000167 SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str());
168 return false;
169 }
170
halcanary0bef17a2014-08-07 07:24:47 -0700171 SkDebugf("exporting... [%-4i %6i] %s\n",
172 picture->width(), picture->height(), inputPath.c_str());
edisonn@google.comc319abe2012-11-01 19:52:38 +0000173
commit-bot@chromium.org5e009892013-10-14 13:42:12 +0000174 SkWStream* stream(open_stream(outputDir, inputFilename));
edisonn@google.comc319abe2012-11-01 19:52:38 +0000175
commit-bot@chromium.org5e009892013-10-14 13:42:12 +0000176 if (!stream) {
177 return false;
178 }
edisonn@google.comc319abe2012-11-01 19:52:38 +0000179
commit-bot@chromium.org5e009892013-10-14 13:42:12 +0000180 renderer.init(picture, stream);
181
182 bool success = renderer.render();
183 SkDELETE(stream);
edisonn@google.comc319abe2012-11-01 19:52:38 +0000184
185 renderer.end();
commit-bot@chromium.org5e009892013-10-14 13:42:12 +0000186
edisonn@google.comc319abe2012-11-01 19:52:38 +0000187 return success;
188}
189
halcanary0bef17a2014-08-07 07:24:47 -0700190static bool operator<(const SkString& a, const SkString& b) {
191 return strcmp(a.c_str(), b.c_str()) < 0;
192}
193
edisonn@google.comc319abe2012-11-01 19:52:38 +0000194/** 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 */
halcanary0bef17a2014-08-07 07:24:47 -0700200static 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.comc319abe2012-11-01 19:52:38 +0000220 }
221 }
halcanary0bef17a2014-08-07 07:24:47 -0700222 }
halcanary0d154ee2014-08-11 11:33:51 -0700223 if (files.count() > 0) {
224 SkTQSort<SkString>(files.begin(), files.end() - 1);
225 }
halcanary0bef17a2014-08-07 07:24:47 -0700226 int failures = 0;
227 for (int i = 0; i < files.count(); i ++) {
228 if (!render_pdf(files[i], outputDir, renderer)) {
edisonn@google.comc319abe2012-11-01 19:52:38 +0000229 ++failures;
230 }
231 }
232 return failures;
233}
234
edisonn@google.com184487c2013-03-08 18:00:16 +0000235int tool_main_core(int argc, char** argv);
236int tool_main_core(int argc, char** argv) {
halcanary0bef17a2014-08-07 07:24:47 -0700237 SkCommandLineFlags::Parse(argc, argv);
238
edisonn@google.comc319abe2012-11-01 19:52:38 +0000239 SkAutoGraphics ag;
edisonn@google.comc319abe2012-11-01 19:52:38 +0000240
241 SkAutoTUnref<sk_tools::PdfRenderer>
commit-bot@chromium.org608ea652013-10-03 19:29:21 +0000242 renderer(SkNEW_ARGS(sk_tools::SimplePdfRenderer, (encode_to_dct_data)));
edisonn@google.comc319abe2012-11-01 19:52:38 +0000243 SkASSERT(renderer.get());
244
edisonn@google.com4fa566b2013-01-11 20:30:41 +0000245 SkString outputDir;
halcanary0bef17a2014-08-07 07:24:47 -0700246 if (FLAGS_outputDir.count() > 0) {
247 outputDir = FLAGS_outputDir[0];
edisonn@google.comc319abe2012-11-01 19:52:38 +0000248 }
249
halcanary0bef17a2014-08-07 07:24:47 -0700250 int failures = process_input(FLAGS_inputPaths, outputDir, *renderer);
251
halcanary0d154ee2014-08-11 11:33:51 -0700252 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.comc319abe2012-11-01 19:52:38 +0000257 if (failures != 0) {
258 SkDebugf("Failed to render %i PDFs.\n", failures);
259 return 1;
260 }
edisonn@google.com184487c2013-03-08 18:00:16 +0000261
262 return 0;
263}
264
265int tool_main(int argc, char** argv);
266int 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.comf2863292013-01-07 20:08:56 +0000279 return 0;
edisonn@google.comc319abe2012-11-01 19:52:38 +0000280}
281
edisonn@google.com9cf5b282012-11-01 20:07:33 +0000282#if !defined SK_BUILD_FOR_IOS
283int main(int argc, char * const argv[]) {
284 return tool_main(argc, (char**) argv);
285}
286#endif