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 | 3b0a9fe | 2013-01-31 15:56:22 +0000 | [diff] [blame] | 8 | #include "SkDebugCanvas.h" |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame] | 9 | #include "SkDevice.h" |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 10 | #include "SkGraphics.h" |
robertphillips@google.com | 4e4d75b | 2012-11-12 18:03:19 +0000 | [diff] [blame] | 11 | #include "SkImageDecoder.h" |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame] | 12 | #include "SkImageEncoder.h" |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 13 | #include "SkOSFile.h" |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 14 | #include "SkPicture.h" |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame] | 15 | #include "SkPicturePlayback.h" |
| 16 | #include "SkPictureRecord.h" |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 17 | #include "SkStream.h" |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 18 | #include "picture_utils.h" |
robertphillips@google.com | d3d377f | 2012-12-07 20:56:13 +0000 | [diff] [blame] | 19 | #include "path_utils.h" |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 20 | |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 21 | static void usage() { |
robertphillips@google.com | d3d377f | 2012-12-07 20:56:13 +0000 | [diff] [blame] | 22 | SkDebugf("Usage: filter -i inFile [-o outFile] [--input-dir path] [--output-dir path]\n"); |
robertphillips@google.com | 3b0a9fe | 2013-01-31 15:56:22 +0000 | [diff] [blame] | 23 | SkDebugf(" [-h|--help]\n\n"); |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 24 | SkDebugf(" -i inFile : file to file.\n"); |
| 25 | SkDebugf(" -o outFile : result of filtering.\n"); |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 26 | SkDebugf(" --input-dir : process all files in dir with .skp extension.\n"); |
| 27 | SkDebugf(" --output-dir : results of filtering the input dir.\n"); |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 28 | SkDebugf(" -h|--help : Show this help message.\n"); |
| 29 | } |
| 30 | |
robertphillips@google.com | 3b0a9fe | 2013-01-31 15:56:22 +0000 | [diff] [blame] | 31 | // Is the supplied paint simply a color? |
| 32 | static bool is_simple(const SkPaint& p) { |
| 33 | return NULL == p.getPathEffect() && |
| 34 | NULL == p.getShader() && |
| 35 | NULL == p.getXfermode() && |
| 36 | NULL == p.getMaskFilter() && |
| 37 | NULL == p.getColorFilter() && |
| 38 | NULL == p.getRasterizer() && |
| 39 | NULL == p.getLooper() && |
| 40 | NULL == p.getImageFilter(); |
| 41 | } |
robertphillips@google.com | d3d377f | 2012-12-07 20:56:13 +0000 | [diff] [blame] | 42 | |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 43 | // Check for: |
| 44 | // SAVE_LAYER |
| 45 | // DRAW_BITMAP_RECT_TO_RECT |
| 46 | // RESTORE |
| 47 | // where the saveLayer's color can be moved into the drawBitmapRect |
| 48 | static bool check_0(const SkTDArray<SkDrawCommand*>& commands, int curCommand) { |
| 49 | if (SAVE_LAYER != commands[curCommand]->getType() || |
| 50 | commands.count() <= curCommand+2 || |
| 51 | DRAW_BITMAP_RECT_TO_RECT != commands[curCommand+1]->getType() || |
| 52 | RESTORE != commands[curCommand+2]->getType()) |
| 53 | return false; |
| 54 | |
| 55 | SaveLayer* saveLayer = (SaveLayer*) commands[curCommand]; |
| 56 | DrawBitmapRect* dbmr = (DrawBitmapRect*) commands[curCommand+1]; |
| 57 | |
| 58 | const SkPaint* saveLayerPaint = saveLayer->paint(); |
| 59 | SkPaint* dbmrPaint = dbmr->paint(); |
| 60 | |
skia.committer@gmail.com | 3d18d06 | 2013-02-14 07:01:34 +0000 | [diff] [blame] | 61 | // For this optimization we only fold the saveLayer and drawBitmapRect |
robertphillips@google.com | 1780a3c | 2013-02-13 13:27:44 +0000 | [diff] [blame] | 62 | // together if the saveLayer's draw is simple (i.e., no fancy effects) and |
| 63 | // and the only difference in the colors is that the saveLayer's can have |
| 64 | // an alpha while the drawBitmapRect's is opaque. |
| 65 | // TODO: it should be possible to fold them together even if they both |
| 66 | // have different non-255 alphas but this is low priority since we have |
| 67 | // never seen that case |
| 68 | // If either operation lacks a paint then the collapse is trivial |
| 69 | SkColor layerColor = saveLayerPaint->getColor() | 0xFF000000; // force opaque |
| 70 | |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 71 | return NULL == saveLayerPaint || |
| 72 | NULL == dbmrPaint || |
robertphillips@google.com | 1780a3c | 2013-02-13 13:27:44 +0000 | [diff] [blame] | 73 | (is_simple(*saveLayerPaint) && dbmrPaint->getColor() == layerColor); |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | // Fold the saveLayer's alpha into the drawBitmapRect and remove the saveLayer |
| 77 | // and restore |
robertphillips@google.com | febc0ec | 2013-03-11 22:53:11 +0000 | [diff] [blame] | 78 | static void apply_0(SkTDArray<SkDrawCommand*>& commands, int curCommand) { |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 79 | SaveLayer* saveLayer = (SaveLayer*) commands[curCommand]; |
| 80 | DrawBitmapRect* dbmr = (DrawBitmapRect*) commands[curCommand+1]; |
| 81 | Restore* restore = (Restore*) commands[curCommand+2]; |
| 82 | |
| 83 | const SkPaint* saveLayerPaint = saveLayer->paint(); |
| 84 | SkPaint* dbmrPaint = dbmr->paint(); |
| 85 | |
| 86 | if (NULL == saveLayerPaint) { |
| 87 | saveLayer->setVisible(false); |
| 88 | restore->setVisible(false); |
| 89 | } else if (NULL == dbmrPaint) { |
| 90 | saveLayer->setVisible(false); |
| 91 | dbmr->setPaint(*saveLayerPaint); |
| 92 | restore->setVisible(false); |
| 93 | } else { |
| 94 | saveLayer->setVisible(false); |
| 95 | SkColor newColor = SkColorSetA(dbmrPaint->getColor(), |
| 96 | SkColorGetA(saveLayerPaint->getColor())); |
| 97 | dbmrPaint->setColor(newColor); |
| 98 | restore->setVisible(false); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // Check for: |
| 103 | // SAVE_LAYER |
| 104 | // SAVE |
| 105 | // CLIP_RECT |
| 106 | // DRAW_BITMAP_RECT_TO_RECT |
| 107 | // RESTORE |
| 108 | // RESTORE |
| 109 | // where the saveLayer's color can be moved into the drawBitmapRect |
| 110 | static bool check_1(const SkTDArray<SkDrawCommand*>& commands, int curCommand) { |
| 111 | if (SAVE_LAYER != commands[curCommand]->getType() || |
| 112 | commands.count() <= curCommand+5 || |
| 113 | SAVE != commands[curCommand+1]->getType() || |
| 114 | CLIP_RECT != commands[curCommand+2]->getType() || |
| 115 | DRAW_BITMAP_RECT_TO_RECT != commands[curCommand+3]->getType() || |
| 116 | RESTORE != commands[curCommand+4]->getType() || |
| 117 | RESTORE != commands[curCommand+5]->getType()) |
| 118 | return false; |
| 119 | |
| 120 | SaveLayer* saveLayer = (SaveLayer*) commands[curCommand]; |
| 121 | DrawBitmapRect* dbmr = (DrawBitmapRect*) commands[curCommand+3]; |
| 122 | |
| 123 | const SkPaint* saveLayerPaint = saveLayer->paint(); |
| 124 | SkPaint* dbmrPaint = dbmr->paint(); |
| 125 | |
skia.committer@gmail.com | 3d18d06 | 2013-02-14 07:01:34 +0000 | [diff] [blame] | 126 | // For this optimization we only fold the saveLayer and drawBitmapRect |
robertphillips@google.com | 1780a3c | 2013-02-13 13:27:44 +0000 | [diff] [blame] | 127 | // together if the saveLayer's draw is simple (i.e., no fancy effects) and |
| 128 | // and the only difference in the colors is that the saveLayer's can have |
| 129 | // an alpha while the drawBitmapRect's is opaque. |
| 130 | // TODO: it should be possible to fold them together even if they both |
| 131 | // have different non-255 alphas but this is low priority since we have |
| 132 | // never seen that case |
| 133 | // If either operation lacks a paint then the collapse is trivial |
| 134 | SkColor layerColor = saveLayerPaint->getColor() | 0xFF000000; // force opaque |
| 135 | |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 136 | return NULL == saveLayerPaint || |
| 137 | NULL == dbmrPaint || |
robertphillips@google.com | 1780a3c | 2013-02-13 13:27:44 +0000 | [diff] [blame] | 138 | (is_simple(*saveLayerPaint) && dbmrPaint->getColor() == layerColor); |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | // Fold the saveLayer's alpha into the drawBitmapRect and remove the saveLayer |
| 142 | // and restore |
robertphillips@google.com | febc0ec | 2013-03-11 22:53:11 +0000 | [diff] [blame] | 143 | static void apply_1(SkTDArray<SkDrawCommand*>& commands, int curCommand) { |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 144 | SaveLayer* saveLayer = (SaveLayer*) commands[curCommand]; |
| 145 | DrawBitmapRect* dbmr = (DrawBitmapRect*) commands[curCommand+3]; |
| 146 | Restore* restore = (Restore*) commands[curCommand+5]; |
| 147 | |
| 148 | const SkPaint* saveLayerPaint = saveLayer->paint(); |
| 149 | SkPaint* dbmrPaint = dbmr->paint(); |
| 150 | |
| 151 | if (NULL == saveLayerPaint) { |
| 152 | saveLayer->setVisible(false); |
| 153 | restore->setVisible(false); |
| 154 | } else if (NULL == dbmrPaint) { |
| 155 | saveLayer->setVisible(false); |
| 156 | dbmr->setPaint(*saveLayerPaint); |
| 157 | restore->setVisible(false); |
| 158 | } else { |
| 159 | saveLayer->setVisible(false); |
| 160 | SkColor newColor = SkColorSetA(dbmrPaint->getColor(), |
| 161 | SkColorGetA(saveLayerPaint->getColor())); |
| 162 | dbmrPaint->setColor(newColor); |
| 163 | restore->setVisible(false); |
| 164 | } |
| 165 | } |
| 166 | |
robertphillips@google.com | febc0ec | 2013-03-11 22:53:11 +0000 | [diff] [blame] | 167 | // Check for: |
| 168 | // SAVE |
| 169 | // CLIP_RECT |
| 170 | // DRAW_RECT |
| 171 | // RESTORE |
| 172 | // where the rect is entirely within the clip and the clip is an intersect |
| 173 | static bool check_2(const SkTDArray<SkDrawCommand*>& commands, int curCommand) { |
| 174 | if (SAVE != commands[curCommand]->getType() || |
| 175 | commands.count() <= curCommand+4 || |
| 176 | CLIP_RECT != commands[curCommand+1]->getType() || |
| 177 | DRAW_RECT != commands[curCommand+2]->getType() || |
| 178 | RESTORE != commands[curCommand+3]->getType()) |
| 179 | return false; |
| 180 | |
| 181 | ClipRect* cr = (ClipRect*) commands[curCommand+1]; |
| 182 | DrawRectC* dr = (DrawRectC*) commands[curCommand+2]; |
| 183 | |
| 184 | if (SkRegion::kIntersect_Op != cr->op()) { |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | return cr->rect().contains(dr->rect()); |
| 189 | } |
| 190 | |
| 191 | // Remove everything but the drawRect |
| 192 | static void apply_2(SkTDArray<SkDrawCommand*>& commands, int curCommand) { |
| 193 | Save* save = (Save*) commands[curCommand]; |
| 194 | ClipRect* cr = (ClipRect*) commands[curCommand+1]; |
| 195 | Restore* restore = (Restore*) commands[curCommand+3]; |
| 196 | |
| 197 | save->setVisible(false); |
| 198 | cr->setVisible(false); |
| 199 | // leave the drawRect alone |
| 200 | restore->setVisible(false); |
| 201 | } |
| 202 | |
| 203 | // Check for: |
| 204 | // SAVE |
| 205 | // CLIP_RRECT |
| 206 | // DRAW_RECT |
| 207 | // RESTORE |
| 208 | // where the rect entirely encloses the clip |
| 209 | static bool check_3(const SkTDArray<SkDrawCommand*>& commands, int curCommand) { |
| 210 | if (SAVE != commands[curCommand]->getType() || |
| 211 | commands.count() <= curCommand+4 || |
| 212 | CLIP_RRECT != commands[curCommand+1]->getType() || |
| 213 | DRAW_RECT != commands[curCommand+2]->getType() || |
| 214 | RESTORE != commands[curCommand+3]->getType()) |
| 215 | return false; |
| 216 | |
| 217 | ClipRRect* crr = (ClipRRect*) commands[curCommand+1]; |
| 218 | DrawRectC* dr = (DrawRectC*) commands[curCommand+2]; |
| 219 | |
| 220 | if (SkRegion::kIntersect_Op != crr->op()) { |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | return dr->rect().contains(crr->rrect().rect()); |
| 225 | } |
| 226 | |
| 227 | // Replace everything with a drawRRect with the paint from the drawRect |
| 228 | // and the AA settings from the clipRRect |
| 229 | static void apply_3(SkTDArray<SkDrawCommand*>& commands, int curCommand) { |
| 230 | Save* save = (Save*) commands[curCommand]; |
| 231 | ClipRRect* crr = (ClipRRect*) commands[curCommand+1]; |
| 232 | DrawRectC* dr = (DrawRectC*) commands[curCommand+2]; |
| 233 | Restore* restore = (Restore*) commands[curCommand+3]; |
| 234 | |
| 235 | save->setVisible(false); |
| 236 | crr->setVisible(false); |
| 237 | dr->setVisible(false); |
| 238 | restore->setVisible(false); |
| 239 | |
| 240 | // TODO: could skip paint re-creation if the AA settings already match |
robertphillips@google.com | 91217d0 | 2013-03-17 18:33:46 +0000 | [diff] [blame] | 241 | SkPaint newPaint = dr->paint(); |
robertphillips@google.com | febc0ec | 2013-03-11 22:53:11 +0000 | [diff] [blame] | 242 | newPaint.setAntiAlias(crr->doAA()); |
| 243 | DrawRRect* drr = new DrawRRect(crr->rrect(), newPaint); |
| 244 | commands[curCommand+2] = drr; |
| 245 | } |
| 246 | |
| 247 | // Check for: |
| 248 | // SAVE |
| 249 | // CLIP_RECT |
| 250 | // DRAW_BITMAP_RECT_TO_RECT |
| 251 | // RESTORE |
| 252 | // where the rect and drawBitmapRect dst exactly match |
| 253 | static bool check_4(const SkTDArray<SkDrawCommand*>& commands, int curCommand) { |
| 254 | if (SAVE != commands[curCommand]->getType() || |
| 255 | commands.count() <= curCommand+4 || |
| 256 | CLIP_RECT != commands[curCommand+1]->getType() || |
| 257 | DRAW_BITMAP_RECT_TO_RECT != commands[curCommand+2]->getType() || |
| 258 | RESTORE != commands[curCommand+3]->getType()) |
| 259 | return false; |
| 260 | |
| 261 | ClipRect* cr = (ClipRect*) commands[curCommand+1]; |
| 262 | DrawBitmapRect* dbmr = (DrawBitmapRect*) commands[curCommand+2]; |
| 263 | |
| 264 | if (SkRegion::kIntersect_Op != cr->op()) { |
| 265 | return false; |
| 266 | } |
| 267 | |
| 268 | return dbmr->dstRect() == cr->rect(); |
| 269 | } |
| 270 | |
| 271 | // Remove everything but the drawBitmapRect |
| 272 | static void apply_4(SkTDArray<SkDrawCommand*>& commands, int curCommand) { |
| 273 | Save* save = (Save*) commands[curCommand]; |
| 274 | ClipRect* cr = (ClipRect*) commands[curCommand+1]; |
| 275 | Restore* restore = (Restore*) commands[curCommand+3]; |
| 276 | |
| 277 | save->setVisible(false); |
| 278 | cr->setVisible(false); |
| 279 | // leave drawBitmapRect alone |
| 280 | restore->setVisible(false); |
| 281 | } |
| 282 | |
robertphillips@google.com | 9105ad0 | 2013-03-17 18:46:16 +0000 | [diff] [blame^] | 283 | // Check for: |
| 284 | // TRANSLATE |
| 285 | // where the translate is zero |
| 286 | static bool check_5(const SkTDArray<SkDrawCommand*>& commands, int curCommand) { |
| 287 | if (TRANSLATE != commands[curCommand]->getType()) { |
| 288 | return false; |
| 289 | } |
| 290 | |
| 291 | Translate* t = (Translate*) commands[curCommand]; |
| 292 | |
| 293 | return 0 == t->x() && 0 == t->y(); |
| 294 | } |
| 295 | |
| 296 | // Just remove the translate |
| 297 | static void apply_5(SkTDArray<SkDrawCommand*>& commands, int curCommand) { |
| 298 | Translate* t = (Translate*) commands[curCommand]; |
| 299 | |
| 300 | t->setVisible(false); |
| 301 | } |
| 302 | |
| 303 | // Check for: |
| 304 | // SCALE |
| 305 | // where the scale is 1,1 |
| 306 | static bool check_6(const SkTDArray<SkDrawCommand*>& commands, int curCommand) { |
| 307 | if (SCALE != commands[curCommand]->getType()) { |
| 308 | return false; |
| 309 | } |
| 310 | |
| 311 | Scale* s = (Scale*) commands[curCommand]; |
| 312 | |
| 313 | return SK_Scalar1 == s->x() && SK_Scalar1 == s->y(); |
| 314 | } |
| 315 | |
| 316 | // Just remove the scale |
| 317 | static void apply_6(SkTDArray<SkDrawCommand*>& commands, int curCommand) { |
| 318 | Scale* s = (Scale*) commands[curCommand]; |
| 319 | |
| 320 | s->setVisible(false); |
| 321 | } |
| 322 | |
| 323 | |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 324 | typedef bool (*PFCheck)(const SkTDArray<SkDrawCommand*>& commands, int curCommand); |
robertphillips@google.com | febc0ec | 2013-03-11 22:53:11 +0000 | [diff] [blame] | 325 | typedef void (*PFApply)(SkTDArray<SkDrawCommand*>& commands, int curCommand); |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 326 | |
| 327 | struct OptTableEntry { |
| 328 | PFCheck fCheck; |
| 329 | PFApply fApply; |
| 330 | int fNumTimesApplied; |
| 331 | } gOptTable[] = { |
| 332 | { check_0, apply_0, 0 }, |
| 333 | { check_1, apply_1, 0 }, |
robertphillips@google.com | febc0ec | 2013-03-11 22:53:11 +0000 | [diff] [blame] | 334 | { check_2, apply_2, 0 }, |
| 335 | { check_3, apply_3, 0 }, |
| 336 | { check_4, apply_4, 0 }, |
robertphillips@google.com | 9105ad0 | 2013-03-17 18:46:16 +0000 | [diff] [blame^] | 337 | { check_5, apply_5, 0 }, |
| 338 | { check_6, apply_6, 0 }, |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 339 | }; |
| 340 | |
robertphillips@google.com | 3b0a9fe | 2013-01-31 15:56:22 +0000 | [diff] [blame] | 341 | static int filter_picture(const SkString& inFile, const SkString& outFile) { |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 342 | SkPicture* inPicture = NULL; |
| 343 | |
| 344 | SkFILEStream inStream(inFile.c_str()); |
| 345 | if (inStream.isValid()) { |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 346 | inPicture = SkNEW_ARGS(SkPicture, (&inStream, NULL, &SkImageDecoder::DecodeMemory)); |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | if (NULL == inPicture) { |
| 350 | SkDebugf("Could not read file %s\n", inFile.c_str()); |
| 351 | return -1; |
| 352 | } |
| 353 | |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 354 | int localCount[SK_ARRAY_COUNT(gOptTable)]; |
| 355 | |
| 356 | memset(localCount, 0, sizeof(localCount)); |
| 357 | |
robertphillips@google.com | 3b0a9fe | 2013-01-31 15:56:22 +0000 | [diff] [blame] | 358 | SkDebugCanvas debugCanvas(inPicture->width(), inPicture->height()); |
| 359 | debugCanvas.setBounds(inPicture->width(), inPicture->height()); |
| 360 | inPicture->draw(&debugCanvas); |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 361 | |
robertphillips@google.com | febc0ec | 2013-03-11 22:53:11 +0000 | [diff] [blame] | 362 | SkTDArray<SkDrawCommand*>& commands = debugCanvas.getDrawCommands(); |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 363 | |
skia.committer@gmail.com | ae68392 | 2013-02-06 07:01:54 +0000 | [diff] [blame] | 364 | // hide the initial save and restore since replaying the commands will |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 365 | // re-add them |
| 366 | if (commands.count() > 0) { |
| 367 | commands[0]->setVisible(false); |
| 368 | commands[commands.count()-1]->setVisible(false); |
| 369 | } |
| 370 | |
robertphillips@google.com | 3b0a9fe | 2013-01-31 15:56:22 +0000 | [diff] [blame] | 371 | for (int i = 0; i < commands.count(); ++i) { |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 372 | for (size_t opt = 0; opt < SK_ARRAY_COUNT(gOptTable); ++opt) { |
| 373 | if ((*gOptTable[opt].fCheck)(commands, i)) { |
| 374 | (*gOptTable[opt].fApply)(commands, i); |
| 375 | ++gOptTable[opt].fNumTimesApplied; |
| 376 | ++localCount[opt]; |
robertphillips@google.com | 3b0a9fe | 2013-01-31 15:56:22 +0000 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | } |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 380 | |
| 381 | if (!outFile.isEmpty()) { |
robertphillips@google.com | 3b0a9fe | 2013-01-31 15:56:22 +0000 | [diff] [blame] | 382 | SkPicture outPicture; |
| 383 | |
| 384 | SkCanvas* canvas = outPicture.beginRecording(inPicture->width(), inPicture->height()); |
| 385 | debugCanvas.draw(canvas); |
| 386 | outPicture.endRecording(); |
| 387 | |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 388 | SkFILEWStream outStream(outFile.c_str()); |
| 389 | |
| 390 | outPicture.serialize(&outStream); |
| 391 | } |
| 392 | |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 393 | bool someOptFired = false; |
| 394 | for (size_t opt = 0; opt < SK_ARRAY_COUNT(gOptTable); ++opt) { |
| 395 | if (0 != localCount[opt]) { |
| 396 | SkDebugf("%d: %d ", opt, localCount[opt]); |
| 397 | someOptFired = true; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | if (!someOptFired) { |
| 402 | SkDebugf("No opts fired\n"); |
| 403 | } else { |
| 404 | SkDebugf("\n"); |
| 405 | } |
| 406 | |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 407 | return 0; |
| 408 | } |
| 409 | |
tfarina@chromium.org | a5b7cc0 | 2012-10-08 14:41:10 +0000 | [diff] [blame] | 410 | // This function is not marked as 'static' so it can be referenced externally |
| 411 | // in the iOS build. |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 412 | int tool_main(int argc, char** argv); // suppress a warning on mac |
| 413 | |
caryclark@google.com | 9598f42 | 2012-10-09 12:32:37 +0000 | [diff] [blame] | 414 | int tool_main(int argc, char** argv) { |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 415 | SkGraphics::Init(); |
| 416 | |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame] | 417 | if (argc < 3) { |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 418 | usage(); |
| 419 | return -1; |
| 420 | } |
| 421 | |
robertphillips@google.com | 3b0a9fe | 2013-01-31 15:56:22 +0000 | [diff] [blame] | 422 | SkString inFile, outFile, inDir, outDir; |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame] | 423 | |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 424 | char* const* stop = argv + argc; |
| 425 | for (++argv; argv < stop; ++argv) { |
| 426 | if (strcmp(*argv, "-i") == 0) { |
| 427 | argv++; |
| 428 | if (argv < stop && **argv) { |
| 429 | inFile.set(*argv); |
| 430 | } else { |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame] | 431 | SkDebugf("missing arg for -i\n"); |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 432 | usage(); |
| 433 | return -1; |
| 434 | } |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 435 | } else if (strcmp(*argv, "--input-dir") == 0) { |
| 436 | argv++; |
| 437 | if (argv < stop && **argv) { |
| 438 | inDir.set(*argv); |
| 439 | } else { |
| 440 | SkDebugf("missing arg for --input-dir\n"); |
| 441 | usage(); |
| 442 | return -1; |
| 443 | } |
| 444 | } else if (strcmp(*argv, "--output-dir") == 0) { |
| 445 | argv++; |
| 446 | if (argv < stop && **argv) { |
| 447 | outDir.set(*argv); |
| 448 | } else { |
| 449 | SkDebugf("missing arg for --output-dir\n"); |
| 450 | usage(); |
| 451 | return -1; |
| 452 | } |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 453 | } else if (strcmp(*argv, "-o") == 0) { |
| 454 | argv++; |
| 455 | if (argv < stop && **argv) { |
| 456 | outFile.set(*argv); |
| 457 | } else { |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame] | 458 | SkDebugf("missing arg for -o\n"); |
| 459 | usage(); |
| 460 | return -1; |
| 461 | } |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 462 | } else if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) { |
| 463 | usage(); |
| 464 | return 0; |
| 465 | } else { |
| 466 | SkDebugf("unknown arg %s\n", *argv); |
| 467 | usage(); |
| 468 | return -1; |
| 469 | } |
| 470 | } |
| 471 | |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 472 | SkOSFile::Iter iter(inDir.c_str(), "skp"); |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 473 | |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 474 | SkString inputFilename, outputFilename; |
| 475 | if (iter.next(&inputFilename)) { |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 476 | |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 477 | do { |
| 478 | sk_tools::make_filepath(&inFile, inDir, inputFilename); |
| 479 | if (!outDir.isEmpty()) { |
| 480 | sk_tools::make_filepath(&outFile, outDir, inputFilename); |
| 481 | } |
| 482 | SkDebugf("Executing %s\n", inputFilename.c_str()); |
robertphillips@google.com | 3b0a9fe | 2013-01-31 15:56:22 +0000 | [diff] [blame] | 483 | filter_picture(inFile, outFile); |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 484 | } while(iter.next(&inputFilename)); |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 485 | |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 486 | } else if (!inFile.isEmpty()) { |
robertphillips@google.com | 3b0a9fe | 2013-01-31 15:56:22 +0000 | [diff] [blame] | 487 | filter_picture(inFile, outFile); |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 488 | } else { |
| 489 | usage(); |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 490 | return -1; |
| 491 | } |
| 492 | |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 493 | for (size_t opt = 0; opt < SK_ARRAY_COUNT(gOptTable); ++opt) { |
| 494 | SkDebugf("opt %d: %d\n", opt, gOptTable[opt].fNumTimesApplied); |
| 495 | } |
| 496 | |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 497 | SkGraphics::Term(); |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 498 | return 0; |
| 499 | } |
| 500 | |
| 501 | #if !defined SK_BUILD_FOR_IOS |
| 502 | int main(int argc, char * const argv[]) { |
caryclark@google.com | 9598f42 | 2012-10-09 12:32:37 +0000 | [diff] [blame] | 503 | return tool_main(argc, (char**) argv); |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 504 | } |
| 505 | #endif |