robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +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 | |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame] | 8 | #include "SkDevice.h" |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 9 | #include "SkGraphics.h" |
robertphillips@google.com | 4e4d75b | 2012-11-12 18:03:19 +0000 | [diff] [blame] | 10 | #include "SkImageDecoder.h" |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame] | 11 | #include "SkImageEncoder.h" |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 12 | #include "SkOSFile.h" |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 13 | #include "SkPicture.h" |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame] | 14 | #include "SkPicturePlayback.h" |
| 15 | #include "SkPictureRecord.h" |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 16 | #include "SkStream.h" |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 17 | #include "picture_utils.h" |
robertphillips@google.com | d3d377f | 2012-12-07 20:56:13 +0000 | [diff] [blame] | 18 | #include "path_utils.h" |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 19 | |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 20 | static void usage() { |
robertphillips@google.com | d3d377f | 2012-12-07 20:56:13 +0000 | [diff] [blame] | 21 | SkDebugf("Usage: filter -i inFile [-o outFile] [--input-dir path] [--output-dir path]\n"); |
| 22 | SkDebugf(" [-p pathFile] [-t textureDir] [-h|--help]\n\n"); |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 23 | SkDebugf(" -i inFile : file to file.\n"); |
| 24 | SkDebugf(" -o outFile : result of filtering.\n"); |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 25 | SkDebugf(" --input-dir : process all files in dir with .skp extension.\n"); |
| 26 | SkDebugf(" --output-dir : results of filtering the input dir.\n"); |
robertphillips@google.com | d3d377f | 2012-12-07 20:56:13 +0000 | [diff] [blame] | 27 | SkDebugf(" -p pathFile : file in which to place compileable path data.\n"); |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 28 | SkDebugf(" -t textureDir : directory in which to place textures. (only available w/ single file)\n"); |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 29 | SkDebugf(" -h|--help : Show this help message.\n"); |
| 30 | } |
| 31 | |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame] | 32 | // SkFilterRecord allows the filter to manipulate the read in SkPicture |
| 33 | class SkFilterRecord : public SkPictureRecord { |
| 34 | public: |
robertphillips@google.com | d3d377f | 2012-12-07 20:56:13 +0000 | [diff] [blame] | 35 | SkFilterRecord(uint32_t recordFlags, SkDevice* device, SkFILEWStream* pathStream) |
robertphillips@google.com | 63f1127 | 2012-10-24 19:30:41 +0000 | [diff] [blame] | 36 | : INHERITED(recordFlags, device) |
| 37 | , fTransSkipped(0) |
| 38 | , fTransTot(0) |
| 39 | , fScalesSkipped(0) |
robertphillips@google.com | d3d377f | 2012-12-07 20:56:13 +0000 | [diff] [blame] | 40 | , fScalesTot(0) |
| 41 | , fPathStream(pathStream) { |
| 42 | } |
| 43 | |
| 44 | virtual ~SkFilterRecord() { |
| 45 | } |
| 46 | |
| 47 | virtual bool clipPath(const SkPath& path, SkRegion::Op op, bool doAntiAlias) SK_OVERRIDE { |
| 48 | if (!path.isRect(NULL) && 4 < path.countPoints()) { |
| 49 | sk_tools::dump_path(fPathStream, path); |
| 50 | } |
| 51 | return INHERITED::clipPath(path, op, doAntiAlias); |
| 52 | } |
| 53 | |
| 54 | virtual void drawPath(const SkPath& path, const SkPaint& p) SK_OVERRIDE { |
| 55 | if (!path.isRect(NULL) && 4 < path.countPoints()) { |
| 56 | sk_tools::dump_path(fPathStream, path); |
| 57 | } |
| 58 | INHERITED::drawPath(path, p); |
robertphillips@google.com | 63f1127 | 2012-10-24 19:30:41 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE { |
| 62 | ++fTransTot; |
| 63 | |
robertphillips@google.com | 4e4d75b | 2012-11-12 18:03:19 +0000 | [diff] [blame] | 64 | #if 0 |
robertphillips@google.com | 63f1127 | 2012-10-24 19:30:41 +0000 | [diff] [blame] | 65 | if (0 == dx && 0 == dy) { |
| 66 | ++fTransSkipped; |
| 67 | return true; |
| 68 | } |
robertphillips@google.com | 4e4d75b | 2012-11-12 18:03:19 +0000 | [diff] [blame] | 69 | #endif |
robertphillips@google.com | 63f1127 | 2012-10-24 19:30:41 +0000 | [diff] [blame] | 70 | |
| 71 | return INHERITED::translate(dx, dy); |
| 72 | } |
| 73 | |
| 74 | virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE { |
| 75 | ++fScalesTot; |
| 76 | |
robertphillips@google.com | 4e4d75b | 2012-11-12 18:03:19 +0000 | [diff] [blame] | 77 | #if 0 |
robertphillips@google.com | 63f1127 | 2012-10-24 19:30:41 +0000 | [diff] [blame] | 78 | if (SK_Scalar1 == sx && SK_Scalar1 == sy) { |
| 79 | ++fScalesSkipped; |
| 80 | return true; |
| 81 | } |
robertphillips@google.com | 4e4d75b | 2012-11-12 18:03:19 +0000 | [diff] [blame] | 82 | #endif |
robertphillips@google.com | 63f1127 | 2012-10-24 19:30:41 +0000 | [diff] [blame] | 83 | |
| 84 | return INHERITED::scale(sx, sy); |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void saveImages(const SkString& path) { |
| 88 | SkTRefArray<SkBitmap>* bitmaps = fBitmapHeap->extractBitmaps(); |
| 89 | |
| 90 | if (NULL != bitmaps) { |
| 91 | for (int i = 0; i < bitmaps->count(); ++i) { |
| 92 | SkString filename(path); |
| 93 | if (!path.endsWith("\\")) { |
| 94 | filename.append("\\"); |
| 95 | } |
| 96 | filename.append("image"); |
| 97 | filename.appendS32(i); |
| 98 | filename.append(".png"); |
| 99 | |
| 100 | SkImageEncoder::EncodeFile(filename.c_str(), (*bitmaps)[i], |
| 101 | SkImageEncoder::kPNG_Type, 0); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | bitmaps->unref(); |
| 106 | } |
| 107 | |
robertphillips@google.com | 63f1127 | 2012-10-24 19:30:41 +0000 | [diff] [blame] | 108 | void report() { |
| 109 | SkDebugf("%d Trans skipped (out of %d)\n", fTransSkipped, fTransTot); |
| 110 | SkDebugf("%d Scales skipped (out of %d)\n", fScalesSkipped, fScalesTot); |
| 111 | } |
| 112 | |
| 113 | protected: |
| 114 | int fTransSkipped; |
| 115 | int fTransTot; |
| 116 | |
| 117 | int fScalesSkipped; |
| 118 | int fScalesTot; |
| 119 | |
robertphillips@google.com | d3d377f | 2012-12-07 20:56:13 +0000 | [diff] [blame] | 120 | SkFILEWStream* fPathStream; |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame] | 121 | private: |
| 122 | typedef SkPictureRecord INHERITED; |
| 123 | }; |
| 124 | |
robertphillips@google.com | 63f1127 | 2012-10-24 19:30:41 +0000 | [diff] [blame] | 125 | // Wrap SkPicture to allow installation of a SkFilterRecord object |
| 126 | class SkFilterPicture : public SkPicture { |
| 127 | public: |
robertphillips@google.com | 831c726 | 2012-10-25 14:45:08 +0000 | [diff] [blame] | 128 | SkFilterPicture(int width, int height, SkPictureRecord* record) { |
| 129 | fWidth = width; |
| 130 | fHeight = height; |
robertphillips@google.com | 63f1127 | 2012-10-24 19:30:41 +0000 | [diff] [blame] | 131 | fRecord = record; |
| 132 | SkSafeRef(fRecord); |
| 133 | } |
| 134 | |
| 135 | private: |
| 136 | typedef SkPicture INHERITED; |
| 137 | }; |
| 138 | |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame^] | 139 | static int filter_picture(const SkString& inFile, const SkString& outFile, |
robertphillips@google.com | d3d377f | 2012-12-07 20:56:13 +0000 | [diff] [blame] | 140 | const SkString& textureDir, SkFILEWStream *pathStream) { |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 141 | SkPicture* inPicture = NULL; |
| 142 | |
| 143 | SkFILEStream inStream(inFile.c_str()); |
| 144 | if (inStream.isValid()) { |
| 145 | inPicture = SkNEW_ARGS(SkPicture, (&inStream, NULL, &SkImageDecoder::DecodeStream)); |
| 146 | } |
| 147 | |
| 148 | if (NULL == inPicture) { |
| 149 | SkDebugf("Could not read file %s\n", inFile.c_str()); |
| 150 | return -1; |
| 151 | } |
| 152 | |
| 153 | SkBitmap bm; |
| 154 | bm.setConfig(SkBitmap::kNo_Config, inPicture->width(), inPicture->height()); |
| 155 | SkAutoTUnref<SkDevice> dev(SkNEW_ARGS(SkDevice, (bm))); |
| 156 | |
robertphillips@google.com | d3d377f | 2012-12-07 20:56:13 +0000 | [diff] [blame] | 157 | SkAutoTUnref<SkFilterRecord> filterRecord(SkNEW_ARGS(SkFilterRecord, (0, dev, pathStream))); |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 158 | |
| 159 | // Playback the read in picture to the SkFilterRecorder to allow filtering |
| 160 | filterRecord->beginRecording(); |
| 161 | inPicture->draw(filterRecord); |
| 162 | filterRecord->endRecording(); |
| 163 | |
| 164 | filterRecord->report(); |
| 165 | |
| 166 | if (!outFile.isEmpty()) { |
| 167 | SkFilterPicture outPicture(inPicture->width(), inPicture->height(), filterRecord); |
| 168 | SkFILEWStream outStream(outFile.c_str()); |
| 169 | |
| 170 | outPicture.serialize(&outStream); |
| 171 | } |
| 172 | |
| 173 | if (!textureDir.isEmpty()) { |
| 174 | filterRecord->saveImages(textureDir); |
| 175 | } |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
tfarina@chromium.org | a5b7cc0 | 2012-10-08 14:41:10 +0000 | [diff] [blame] | 180 | // This function is not marked as 'static' so it can be referenced externally |
| 181 | // in the iOS build. |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame^] | 182 | int tool_main(int argc, char** argv); // suppress a warning on mac |
| 183 | |
caryclark@google.com | 9598f42 | 2012-10-09 12:32:37 +0000 | [diff] [blame] | 184 | int tool_main(int argc, char** argv) { |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 185 | SkGraphics::Init(); |
| 186 | |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame] | 187 | if (argc < 3) { |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 188 | usage(); |
| 189 | return -1; |
| 190 | } |
| 191 | |
robertphillips@google.com | d3d377f | 2012-12-07 20:56:13 +0000 | [diff] [blame] | 192 | SkString inFile, outFile, inDir, outDir, textureDir, pathFile; |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame] | 193 | |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 194 | char* const* stop = argv + argc; |
| 195 | for (++argv; argv < stop; ++argv) { |
| 196 | if (strcmp(*argv, "-i") == 0) { |
| 197 | argv++; |
| 198 | if (argv < stop && **argv) { |
| 199 | inFile.set(*argv); |
| 200 | } else { |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame] | 201 | SkDebugf("missing arg for -i\n"); |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 202 | usage(); |
| 203 | return -1; |
| 204 | } |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 205 | } else if (strcmp(*argv, "--input-dir") == 0) { |
| 206 | argv++; |
| 207 | if (argv < stop && **argv) { |
| 208 | inDir.set(*argv); |
| 209 | } else { |
| 210 | SkDebugf("missing arg for --input-dir\n"); |
| 211 | usage(); |
| 212 | return -1; |
| 213 | } |
| 214 | } else if (strcmp(*argv, "--output-dir") == 0) { |
| 215 | argv++; |
| 216 | if (argv < stop && **argv) { |
| 217 | outDir.set(*argv); |
| 218 | } else { |
| 219 | SkDebugf("missing arg for --output-dir\n"); |
| 220 | usage(); |
| 221 | return -1; |
| 222 | } |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 223 | } else if (strcmp(*argv, "-o") == 0) { |
| 224 | argv++; |
| 225 | if (argv < stop && **argv) { |
| 226 | outFile.set(*argv); |
| 227 | } else { |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame] | 228 | SkDebugf("missing arg for -o\n"); |
| 229 | usage(); |
| 230 | return -1; |
| 231 | } |
robertphillips@google.com | d3d377f | 2012-12-07 20:56:13 +0000 | [diff] [blame] | 232 | } else if (strcmp(*argv, "-p") == 0) { |
| 233 | argv++; |
| 234 | if (argv < stop && **argv) { |
| 235 | pathFile.set(*argv); |
| 236 | } else { |
| 237 | SkDebugf("missing arg for -p\n"); |
| 238 | usage(); |
| 239 | return -1; |
| 240 | } |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame] | 241 | } else if (strcmp(*argv, "-t") == 0) { |
| 242 | argv++; |
| 243 | if (argv < stop && **argv) { |
| 244 | textureDir.set(*argv); |
| 245 | } else { |
| 246 | SkDebugf("missing arg for -t\n"); |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 247 | usage(); |
| 248 | return -1; |
| 249 | } |
| 250 | } else if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) { |
| 251 | usage(); |
| 252 | return 0; |
| 253 | } else { |
| 254 | SkDebugf("unknown arg %s\n", *argv); |
| 255 | usage(); |
| 256 | return -1; |
| 257 | } |
| 258 | } |
| 259 | |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 260 | if(!inDir.isEmpty() && !textureDir.isEmpty()) { |
| 261 | SkDebugf("ERROR: The textureDir option is not permitted when passing an input directory.\n"); |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame] | 262 | usage(); |
| 263 | return -1; |
| 264 | } |
| 265 | |
robertphillips@google.com | d3d377f | 2012-12-07 20:56:13 +0000 | [diff] [blame] | 266 | SkFILEWStream *pathStream = NULL; |
| 267 | |
| 268 | if (!pathFile.isEmpty()) { |
| 269 | pathStream = new SkFILEWStream(pathFile.c_str()); |
| 270 | if (!pathStream->isValid()) { |
| 271 | SkDebugf("Could open path file %s\n", pathFile.c_str()); |
| 272 | delete pathStream; |
| 273 | return -1; |
| 274 | } |
| 275 | |
| 276 | sk_tools::dump_path_prefix(pathStream); |
| 277 | } |
| 278 | |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 279 | SkOSFile::Iter iter(inDir.c_str(), "skp"); |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame^] | 280 | |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 281 | SkString inputFilename, outputFilename; |
| 282 | if (iter.next(&inputFilename)) { |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 283 | |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 284 | do { |
| 285 | sk_tools::make_filepath(&inFile, inDir, inputFilename); |
| 286 | if (!outDir.isEmpty()) { |
| 287 | sk_tools::make_filepath(&outFile, outDir, inputFilename); |
| 288 | } |
| 289 | SkDebugf("Executing %s\n", inputFilename.c_str()); |
robertphillips@google.com | d3d377f | 2012-12-07 20:56:13 +0000 | [diff] [blame] | 290 | filter_picture(inFile, outFile, textureDir, pathStream); |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 291 | } while(iter.next(&inputFilename)); |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 292 | |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 293 | } else if (!inFile.isEmpty()) { |
robertphillips@google.com | d3d377f | 2012-12-07 20:56:13 +0000 | [diff] [blame] | 294 | filter_picture(inFile, outFile, textureDir, pathStream); |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 295 | } else { |
| 296 | usage(); |
robertphillips@google.com | d3d377f | 2012-12-07 20:56:13 +0000 | [diff] [blame] | 297 | if (NULL != pathStream) { |
| 298 | delete pathStream; |
| 299 | pathStream = NULL; |
| 300 | } |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 301 | return -1; |
| 302 | } |
| 303 | |
robertphillips@google.com | d3d377f | 2012-12-07 20:56:13 +0000 | [diff] [blame] | 304 | if (NULL != pathStream) { |
| 305 | sk_tools::dump_path_suffix(pathStream); |
| 306 | delete pathStream; |
| 307 | pathStream = NULL; |
| 308 | } |
| 309 | |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 310 | SkGraphics::Term(); |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | #if !defined SK_BUILD_FOR_IOS |
| 315 | int main(int argc, char * const argv[]) { |
caryclark@google.com | 9598f42 | 2012-10-09 12:32:37 +0000 | [diff] [blame] | 316 | return tool_main(argc, (char**) argv); |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 317 | } |
| 318 | #endif |