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 | 2e87ba0 | 2013-04-08 15:45:30 +0000 | [diff] [blame] | 24 | SkDebugf(" -i inFile : file to filter.\n"); |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 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 | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 43 | |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 44 | // Check for: |
| 45 | // SAVE_LAYER |
| 46 | // DRAW_BITMAP_RECT_TO_RECT |
| 47 | // RESTORE |
| 48 | // where the saveLayer's color can be moved into the drawBitmapRect |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 49 | static bool check_0(SkDebugCanvas* canvas, int curCommand) { |
| 50 | if (SAVE_LAYER != canvas->getDrawCommandAt(curCommand)->getType() || |
| 51 | canvas->getSize() <= curCommand+2 || |
| 52 | DRAW_BITMAP_RECT_TO_RECT != canvas->getDrawCommandAt(curCommand+1)->getType() || |
| 53 | RESTORE != canvas->getDrawCommandAt(curCommand+2)->getType()) { |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 54 | return false; |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 55 | } |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 56 | |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 57 | SaveLayer* saveLayer = (SaveLayer*) canvas->getDrawCommandAt(curCommand); |
| 58 | DrawBitmapRect* dbmr = (DrawBitmapRect*) canvas->getDrawCommandAt(curCommand+1); |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 59 | |
| 60 | const SkPaint* saveLayerPaint = saveLayer->paint(); |
| 61 | SkPaint* dbmrPaint = dbmr->paint(); |
| 62 | |
skia.committer@gmail.com | 3d18d06 | 2013-02-14 07:01:34 +0000 | [diff] [blame] | 63 | // For this optimization we only fold the saveLayer and drawBitmapRect |
robertphillips@google.com | c525704 | 2013-04-02 15:30:03 +0000 | [diff] [blame] | 64 | // together if the saveLayer's draw is simple (i.e., no fancy effects) |
| 65 | // and the only difference in the colors is their alpha value |
robertphillips@google.com | 1780a3c | 2013-02-13 13:27:44 +0000 | [diff] [blame] | 66 | SkColor layerColor = saveLayerPaint->getColor() | 0xFF000000; // force opaque |
robertphillips@google.com | c525704 | 2013-04-02 15:30:03 +0000 | [diff] [blame] | 67 | SkColor dbmrColor = dbmrPaint->getColor() | 0xFF000000; // force opaque |
robertphillips@google.com | 1780a3c | 2013-02-13 13:27:44 +0000 | [diff] [blame] | 68 | |
robertphillips@google.com | c525704 | 2013-04-02 15:30:03 +0000 | [diff] [blame] | 69 | // If either operation lacks a paint then the collapse is trivial |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 70 | return NULL == saveLayerPaint || |
| 71 | NULL == dbmrPaint || |
robertphillips@google.com | c525704 | 2013-04-02 15:30:03 +0000 | [diff] [blame] | 72 | (is_simple(*saveLayerPaint) && dbmrColor == layerColor); |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // Fold the saveLayer's alpha into the drawBitmapRect and remove the saveLayer |
| 76 | // and restore |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 77 | static void apply_0(SkDebugCanvas* canvas, int curCommand) { |
| 78 | SaveLayer* saveLayer = (SaveLayer*) canvas->getDrawCommandAt(curCommand); |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 79 | const SkPaint* saveLayerPaint = saveLayer->paint(); |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 80 | |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 81 | // if (NULL == saveLayerPaint) the dbmr's paint doesn't need to be changed |
skia.committer@gmail.com | 05a2ee0 | 2013-04-02 07:01:34 +0000 | [diff] [blame] | 82 | if (NULL != saveLayerPaint) { |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 83 | DrawBitmapRect* dbmr = (DrawBitmapRect*) canvas->getDrawCommandAt(curCommand+1); |
| 84 | SkPaint* dbmrPaint = dbmr->paint(); |
| 85 | |
| 86 | if (NULL == dbmrPaint) { |
| 87 | // if the DBMR doesn't have a paint just use the saveLayer's |
| 88 | dbmr->setPaint(*saveLayerPaint); |
| 89 | } else if (NULL != saveLayerPaint) { |
robertphillips@google.com | c525704 | 2013-04-02 15:30:03 +0000 | [diff] [blame] | 90 | // Both paints are present so their alphas need to be combined |
| 91 | SkColor color = saveLayerPaint->getColor(); |
| 92 | int a0 = SkColorGetA(color); |
| 93 | |
| 94 | color = dbmrPaint->getColor(); |
| 95 | int a1 = SkColorGetA(color); |
| 96 | |
| 97 | int newA = SkMulDiv255Round(a0, a1); |
| 98 | SkASSERT(newA <= 0xFF); |
| 99 | |
| 100 | SkColor newColor = SkColorSetA(color, newA); |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 101 | dbmrPaint->setColor(newColor); |
| 102 | } |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 103 | } |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 104 | |
| 105 | canvas->deleteDrawCommandAt(curCommand+2); // restore |
| 106 | canvas->deleteDrawCommandAt(curCommand); // saveLayer |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // Check for: |
| 110 | // SAVE_LAYER |
| 111 | // SAVE |
| 112 | // CLIP_RECT |
| 113 | // DRAW_BITMAP_RECT_TO_RECT |
| 114 | // RESTORE |
| 115 | // RESTORE |
| 116 | // where the saveLayer's color can be moved into the drawBitmapRect |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 117 | static bool check_1(SkDebugCanvas* canvas, int curCommand) { |
| 118 | if (SAVE_LAYER != canvas->getDrawCommandAt(curCommand)->getType() || |
| 119 | canvas->getSize() <= curCommand+5 || |
| 120 | SAVE != canvas->getDrawCommandAt(curCommand+1)->getType() || |
| 121 | CLIP_RECT != canvas->getDrawCommandAt(curCommand+2)->getType() || |
| 122 | DRAW_BITMAP_RECT_TO_RECT != canvas->getDrawCommandAt(curCommand+3)->getType() || |
| 123 | RESTORE != canvas->getDrawCommandAt(curCommand+4)->getType() || |
| 124 | RESTORE != canvas->getDrawCommandAt(curCommand+5)->getType()) { |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 125 | return false; |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 126 | } |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 127 | |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 128 | SaveLayer* saveLayer = (SaveLayer*) canvas->getDrawCommandAt(curCommand); |
| 129 | DrawBitmapRect* dbmr = (DrawBitmapRect*) canvas->getDrawCommandAt(curCommand+3); |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 130 | |
| 131 | const SkPaint* saveLayerPaint = saveLayer->paint(); |
| 132 | SkPaint* dbmrPaint = dbmr->paint(); |
| 133 | |
skia.committer@gmail.com | 3d18d06 | 2013-02-14 07:01:34 +0000 | [diff] [blame] | 134 | // For this optimization we only fold the saveLayer and drawBitmapRect |
robertphillips@google.com | 1780a3c | 2013-02-13 13:27:44 +0000 | [diff] [blame] | 135 | // together if the saveLayer's draw is simple (i.e., no fancy effects) and |
| 136 | // and the only difference in the colors is that the saveLayer's can have |
| 137 | // an alpha while the drawBitmapRect's is opaque. |
| 138 | // TODO: it should be possible to fold them together even if they both |
| 139 | // have different non-255 alphas but this is low priority since we have |
| 140 | // never seen that case |
| 141 | // If either operation lacks a paint then the collapse is trivial |
| 142 | SkColor layerColor = saveLayerPaint->getColor() | 0xFF000000; // force opaque |
| 143 | |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 144 | return NULL == saveLayerPaint || |
| 145 | NULL == dbmrPaint || |
robertphillips@google.com | 1780a3c | 2013-02-13 13:27:44 +0000 | [diff] [blame] | 146 | (is_simple(*saveLayerPaint) && dbmrPaint->getColor() == layerColor); |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // Fold the saveLayer's alpha into the drawBitmapRect and remove the saveLayer |
| 150 | // and restore |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 151 | static void apply_1(SkDebugCanvas* canvas, int curCommand) { |
| 152 | SaveLayer* saveLayer = (SaveLayer*) canvas->getDrawCommandAt(curCommand); |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 153 | const SkPaint* saveLayerPaint = saveLayer->paint(); |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 154 | |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 155 | // if (NULL == saveLayerPaint) the dbmr's paint doesn't need to be changed |
| 156 | if (NULL != saveLayerPaint) { |
| 157 | DrawBitmapRect* dbmr = (DrawBitmapRect*) canvas->getDrawCommandAt(curCommand+3); |
| 158 | SkPaint* dbmrPaint = dbmr->paint(); |
| 159 | |
| 160 | if (NULL == dbmrPaint) { |
| 161 | dbmr->setPaint(*saveLayerPaint); |
| 162 | } else { |
| 163 | SkColor newColor = SkColorSetA(dbmrPaint->getColor(), |
| 164 | SkColorGetA(saveLayerPaint->getColor())); |
| 165 | dbmrPaint->setColor(newColor); |
| 166 | } |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 167 | } |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 168 | |
| 169 | canvas->deleteDrawCommandAt(curCommand+5); // restore |
| 170 | canvas->deleteDrawCommandAt(curCommand); // saveLayer |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 171 | } |
| 172 | |
robertphillips@google.com | febc0ec | 2013-03-11 22:53:11 +0000 | [diff] [blame] | 173 | // Check for: |
| 174 | // SAVE |
| 175 | // CLIP_RECT |
| 176 | // DRAW_RECT |
| 177 | // RESTORE |
| 178 | // where the rect is entirely within the clip and the clip is an intersect |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 179 | static bool check_2(SkDebugCanvas* canvas, int curCommand) { |
| 180 | if (SAVE != canvas->getDrawCommandAt(curCommand)->getType() || |
| 181 | canvas->getSize() <= curCommand+4 || |
| 182 | CLIP_RECT != canvas->getDrawCommandAt(curCommand+1)->getType() || |
| 183 | DRAW_RECT != canvas->getDrawCommandAt(curCommand+2)->getType() || |
| 184 | RESTORE != canvas->getDrawCommandAt(curCommand+3)->getType()) { |
robertphillips@google.com | febc0ec | 2013-03-11 22:53:11 +0000 | [diff] [blame] | 185 | return false; |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 186 | } |
robertphillips@google.com | febc0ec | 2013-03-11 22:53:11 +0000 | [diff] [blame] | 187 | |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 188 | ClipRect* cr = (ClipRect*) canvas->getDrawCommandAt(curCommand+1); |
| 189 | DrawRectC* dr = (DrawRectC*) canvas->getDrawCommandAt(curCommand+2); |
robertphillips@google.com | febc0ec | 2013-03-11 22:53:11 +0000 | [diff] [blame] | 190 | |
| 191 | if (SkRegion::kIntersect_Op != cr->op()) { |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | return cr->rect().contains(dr->rect()); |
| 196 | } |
| 197 | |
| 198 | // Remove everything but the drawRect |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 199 | static void apply_2(SkDebugCanvas* canvas, int curCommand) { |
| 200 | canvas->deleteDrawCommandAt(curCommand+3); // restore |
| 201 | // drawRect |
| 202 | canvas->deleteDrawCommandAt(curCommand+1); // clipRect |
| 203 | canvas->deleteDrawCommandAt(curCommand); // save |
robertphillips@google.com | febc0ec | 2013-03-11 22:53:11 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | // Check for: |
| 207 | // SAVE |
| 208 | // CLIP_RRECT |
| 209 | // DRAW_RECT |
| 210 | // RESTORE |
| 211 | // where the rect entirely encloses the clip |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 212 | static bool check_3(SkDebugCanvas* canvas, int curCommand) { |
| 213 | if (SAVE != canvas->getDrawCommandAt(curCommand)->getType() || |
| 214 | canvas->getSize() <= curCommand+4 || |
| 215 | CLIP_RRECT != canvas->getDrawCommandAt(curCommand+1)->getType() || |
| 216 | DRAW_RECT != canvas->getDrawCommandAt(curCommand+2)->getType() || |
| 217 | RESTORE != canvas->getDrawCommandAt(curCommand+3)->getType()) { |
robertphillips@google.com | febc0ec | 2013-03-11 22:53:11 +0000 | [diff] [blame] | 218 | return false; |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 219 | } |
robertphillips@google.com | febc0ec | 2013-03-11 22:53:11 +0000 | [diff] [blame] | 220 | |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 221 | ClipRRect* crr = (ClipRRect*) canvas->getDrawCommandAt(curCommand+1); |
| 222 | DrawRectC* dr = (DrawRectC*) canvas->getDrawCommandAt(curCommand+2); |
robertphillips@google.com | febc0ec | 2013-03-11 22:53:11 +0000 | [diff] [blame] | 223 | |
| 224 | if (SkRegion::kIntersect_Op != crr->op()) { |
| 225 | return false; |
| 226 | } |
| 227 | |
| 228 | return dr->rect().contains(crr->rrect().rect()); |
| 229 | } |
| 230 | |
| 231 | // Replace everything with a drawRRect with the paint from the drawRect |
| 232 | // and the AA settings from the clipRRect |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 233 | static void apply_3(SkDebugCanvas* canvas, int curCommand) { |
robertphillips@google.com | febc0ec | 2013-03-11 22:53:11 +0000 | [diff] [blame] | 234 | |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 235 | canvas->deleteDrawCommandAt(curCommand+3); // restore |
| 236 | |
| 237 | ClipRRect* crr = (ClipRRect*) canvas->getDrawCommandAt(curCommand+1); |
| 238 | DrawRectC* dr = (DrawRectC*) canvas->getDrawCommandAt(curCommand+2); |
robertphillips@google.com | febc0ec | 2013-03-11 22:53:11 +0000 | [diff] [blame] | 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); |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 244 | canvas->setDrawCommandAt(curCommand+2, drr); |
| 245 | |
| 246 | canvas->deleteDrawCommandAt(curCommand+1); // clipRRect |
| 247 | canvas->deleteDrawCommandAt(curCommand); // save |
robertphillips@google.com | febc0ec | 2013-03-11 22:53:11 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | // Check for: |
| 251 | // SAVE |
| 252 | // CLIP_RECT |
| 253 | // DRAW_BITMAP_RECT_TO_RECT |
| 254 | // RESTORE |
| 255 | // where the rect and drawBitmapRect dst exactly match |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 256 | static bool check_4(SkDebugCanvas* canvas, int curCommand) { |
| 257 | if (SAVE != canvas->getDrawCommandAt(curCommand)->getType() || |
| 258 | canvas->getSize() <= curCommand+4 || |
| 259 | CLIP_RECT != canvas->getDrawCommandAt(curCommand+1)->getType() || |
| 260 | DRAW_BITMAP_RECT_TO_RECT != canvas->getDrawCommandAt(curCommand+2)->getType() || |
| 261 | RESTORE != canvas->getDrawCommandAt(curCommand+3)->getType()) { |
robertphillips@google.com | febc0ec | 2013-03-11 22:53:11 +0000 | [diff] [blame] | 262 | return false; |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 263 | } |
robertphillips@google.com | febc0ec | 2013-03-11 22:53:11 +0000 | [diff] [blame] | 264 | |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 265 | ClipRect* cr = (ClipRect*) canvas->getDrawCommandAt(curCommand+1); |
| 266 | DrawBitmapRect* dbmr = (DrawBitmapRect*) canvas->getDrawCommandAt(curCommand+2); |
robertphillips@google.com | febc0ec | 2013-03-11 22:53:11 +0000 | [diff] [blame] | 267 | |
| 268 | if (SkRegion::kIntersect_Op != cr->op()) { |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | return dbmr->dstRect() == cr->rect(); |
| 273 | } |
| 274 | |
| 275 | // Remove everything but the drawBitmapRect |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 276 | static void apply_4(SkDebugCanvas* canvas, int curCommand) { |
| 277 | canvas->deleteDrawCommandAt(curCommand+3); // restore |
| 278 | // drawBitmapRectToRect |
| 279 | canvas->deleteDrawCommandAt(curCommand+1); // clipRect |
| 280 | canvas->deleteDrawCommandAt(curCommand); // save |
robertphillips@google.com | febc0ec | 2013-03-11 22:53:11 +0000 | [diff] [blame] | 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 |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 286 | static bool check_5(SkDebugCanvas* canvas, int curCommand) { |
| 287 | if (TRANSLATE != canvas->getDrawCommandAt(curCommand)->getType()) { |
robertphillips@google.com | 9105ad0 | 2013-03-17 18:46:16 +0000 | [diff] [blame] | 288 | return false; |
| 289 | } |
| 290 | |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 291 | Translate* t = (Translate*) canvas->getDrawCommandAt(curCommand); |
robertphillips@google.com | 9105ad0 | 2013-03-17 18:46:16 +0000 | [diff] [blame] | 292 | |
| 293 | return 0 == t->x() && 0 == t->y(); |
| 294 | } |
| 295 | |
| 296 | // Just remove the translate |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 297 | static void apply_5(SkDebugCanvas* canvas, int curCommand) { |
| 298 | canvas->deleteDrawCommandAt(curCommand); // translate |
robertphillips@google.com | 9105ad0 | 2013-03-17 18:46:16 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | // Check for: |
| 302 | // SCALE |
| 303 | // where the scale is 1,1 |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 304 | static bool check_6(SkDebugCanvas* canvas, int curCommand) { |
| 305 | if (SCALE != canvas->getDrawCommandAt(curCommand)->getType()) { |
robertphillips@google.com | 9105ad0 | 2013-03-17 18:46:16 +0000 | [diff] [blame] | 306 | return false; |
| 307 | } |
| 308 | |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 309 | Scale* s = (Scale*) canvas->getDrawCommandAt(curCommand); |
robertphillips@google.com | 9105ad0 | 2013-03-17 18:46:16 +0000 | [diff] [blame] | 310 | |
| 311 | return SK_Scalar1 == s->x() && SK_Scalar1 == s->y(); |
| 312 | } |
| 313 | |
| 314 | // Just remove the scale |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 315 | static void apply_6(SkDebugCanvas* canvas, int curCommand) { |
| 316 | canvas->deleteDrawCommandAt(curCommand); // scale |
robertphillips@google.com | 9105ad0 | 2013-03-17 18:46:16 +0000 | [diff] [blame] | 317 | } |
| 318 | |
robertphillips@google.com | c3410b8 | 2013-03-28 12:25:25 +0000 | [diff] [blame] | 319 | // Check for: |
| 320 | // SAVE |
| 321 | // CLIP_RECT |
| 322 | // SAVE_LAYER |
| 323 | // SAVE |
| 324 | // CLIP_RECT |
| 325 | // SAVE_LAYER |
| 326 | // SAVE |
| 327 | // CLIP_RECT |
| 328 | // DRAWBITMAPRECTTORECT |
| 329 | // RESTORE |
| 330 | // RESTORE |
| 331 | // RESTORE |
| 332 | // RESTORE |
| 333 | // RESTORE |
| 334 | // where: |
| 335 | // all the clipRect's are BW, nested, intersections |
| 336 | // the drawBitmapRectToRect is a 1-1 copy from src to dest |
| 337 | // the last (smallest) clip rect is a subset of the drawBitmapRectToRect's dest rect |
| 338 | // all the saveLayer's paints can be rolled into the drawBitmapRectToRect's paint |
| 339 | // This pattern is used by Google spreadsheet when drawing the toolbar buttons |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 340 | static bool check_7(SkDebugCanvas* canvas, int curCommand) { |
| 341 | if (SAVE != canvas->getDrawCommandAt(curCommand)->getType() || |
| 342 | canvas->getSize() <= curCommand+13 || |
| 343 | CLIP_RECT != canvas->getDrawCommandAt(curCommand+1)->getType() || |
| 344 | SAVE_LAYER != canvas->getDrawCommandAt(curCommand+2)->getType() || |
| 345 | SAVE != canvas->getDrawCommandAt(curCommand+3)->getType() || |
| 346 | CLIP_RECT != canvas->getDrawCommandAt(curCommand+4)->getType() || |
| 347 | SAVE_LAYER != canvas->getDrawCommandAt(curCommand+5)->getType() || |
| 348 | SAVE != canvas->getDrawCommandAt(curCommand+6)->getType() || |
| 349 | CLIP_RECT != canvas->getDrawCommandAt(curCommand+7)->getType() || |
| 350 | DRAW_BITMAP_RECT_TO_RECT != canvas->getDrawCommandAt(curCommand+8)->getType() || |
| 351 | RESTORE != canvas->getDrawCommandAt(curCommand+9)->getType() || |
| 352 | RESTORE != canvas->getDrawCommandAt(curCommand+10)->getType() || |
| 353 | RESTORE != canvas->getDrawCommandAt(curCommand+11)->getType() || |
| 354 | RESTORE != canvas->getDrawCommandAt(curCommand+12)->getType() || |
| 355 | RESTORE != canvas->getDrawCommandAt(curCommand+13)->getType()) { |
robertphillips@google.com | c3410b8 | 2013-03-28 12:25:25 +0000 | [diff] [blame] | 356 | return false; |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 357 | } |
robertphillips@google.com | c3410b8 | 2013-03-28 12:25:25 +0000 | [diff] [blame] | 358 | |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 359 | ClipRect* clip0 = (ClipRect*) canvas->getDrawCommandAt(curCommand+1); |
| 360 | SaveLayer* saveLayer0 = (SaveLayer*) canvas->getDrawCommandAt(curCommand+2); |
| 361 | ClipRect* clip1 = (ClipRect*) canvas->getDrawCommandAt(curCommand+4); |
| 362 | SaveLayer* saveLayer1 = (SaveLayer*) canvas->getDrawCommandAt(curCommand+5); |
| 363 | ClipRect* clip2 = (ClipRect*) canvas->getDrawCommandAt(curCommand+7); |
| 364 | DrawBitmapRect* dbmr = (DrawBitmapRect*) canvas->getDrawCommandAt(curCommand+8); |
robertphillips@google.com | c3410b8 | 2013-03-28 12:25:25 +0000 | [diff] [blame] | 365 | |
| 366 | if (clip0->doAA() || clip1->doAA() || clip2->doAA()) { |
| 367 | return false; |
| 368 | } |
| 369 | |
| 370 | if (SkRegion::kIntersect_Op != clip0->op() || |
| 371 | SkRegion::kIntersect_Op != clip1->op() || |
| 372 | SkRegion::kIntersect_Op != clip2->op()) { |
| 373 | return false; |
| 374 | } |
| 375 | |
skia.committer@gmail.com | 6acd09e | 2013-03-29 07:01:22 +0000 | [diff] [blame] | 376 | if (!clip0->rect().contains(clip1->rect()) || |
robertphillips@google.com | c3410b8 | 2013-03-28 12:25:25 +0000 | [diff] [blame] | 377 | !clip1->rect().contains(clip2->rect())) { |
| 378 | return false; |
| 379 | } |
| 380 | |
| 381 | // The src->dest mapping needs to be 1-to-1 |
| 382 | if (NULL == dbmr->srcRect()) { |
| 383 | if (dbmr->bitmap().width() != dbmr->dstRect().width() || |
| 384 | dbmr->bitmap().height() != dbmr->dstRect().height()) { |
| 385 | return false; |
| 386 | } |
| 387 | } else { |
| 388 | if (dbmr->srcRect()->width() != dbmr->dstRect().width() || |
| 389 | dbmr->srcRect()->height() != dbmr->dstRect().height()) { |
| 390 | return false; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | if (!dbmr->dstRect().contains(clip2->rect())) { |
| 395 | return false; |
| 396 | } |
| 397 | |
| 398 | const SkPaint* saveLayerPaint0 = saveLayer0->paint(); |
| 399 | const SkPaint* saveLayerPaint1 = saveLayer1->paint(); |
| 400 | |
| 401 | if ((NULL != saveLayerPaint0 && !is_simple(*saveLayerPaint0)) || |
| 402 | (NULL != saveLayerPaint1 && !is_simple(*saveLayerPaint1))) { |
| 403 | return false; |
| 404 | } |
| 405 | |
| 406 | SkPaint* dbmrPaint = dbmr->paint(); |
| 407 | |
| 408 | if (NULL == dbmrPaint) { |
skia.committer@gmail.com | 6acd09e | 2013-03-29 07:01:22 +0000 | [diff] [blame] | 409 | return true; |
robertphillips@google.com | c3410b8 | 2013-03-28 12:25:25 +0000 | [diff] [blame] | 410 | } |
skia.committer@gmail.com | 6acd09e | 2013-03-29 07:01:22 +0000 | [diff] [blame] | 411 | |
robertphillips@google.com | c3410b8 | 2013-03-28 12:25:25 +0000 | [diff] [blame] | 412 | if (NULL != saveLayerPaint0) { |
| 413 | SkColor layerColor0 = saveLayerPaint0->getColor() | 0xFF000000; // force opaque |
| 414 | if (dbmrPaint->getColor() != layerColor0) { |
| 415 | return false; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | if (NULL != saveLayerPaint1) { |
| 420 | SkColor layerColor1 = saveLayerPaint1->getColor() | 0xFF000000; // force opaque |
| 421 | if (dbmrPaint->getColor() != layerColor1) { |
| 422 | return false; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | return true; |
| 427 | } |
| 428 | |
| 429 | // Reduce to a single drawBitmapRectToRect call by folding the clipRect's into |
| 430 | // the src and dst Rects and the saveLayer paints into the drawBitmapRectToRect's |
| 431 | // paint. |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 432 | static void apply_7(SkDebugCanvas* canvas, int curCommand) { |
| 433 | SaveLayer* saveLayer0 = (SaveLayer*) canvas->getDrawCommandAt(curCommand+2); |
| 434 | SaveLayer* saveLayer1 = (SaveLayer*) canvas->getDrawCommandAt(curCommand+5); |
| 435 | ClipRect* clip2 = (ClipRect*) canvas->getDrawCommandAt(curCommand+7); |
| 436 | DrawBitmapRect* dbmr = (DrawBitmapRect*) canvas->getDrawCommandAt(curCommand+8); |
robertphillips@google.com | c3410b8 | 2013-03-28 12:25:25 +0000 | [diff] [blame] | 437 | |
| 438 | SkScalar newSrcLeft = dbmr->srcRect()->fLeft + clip2->rect().fLeft - dbmr->dstRect().fLeft; |
| 439 | SkScalar newSrcTop = dbmr->srcRect()->fTop + clip2->rect().fTop - dbmr->dstRect().fTop; |
| 440 | |
skia.committer@gmail.com | 6acd09e | 2013-03-29 07:01:22 +0000 | [diff] [blame] | 441 | SkRect newSrc = SkRect::MakeXYWH(newSrcLeft, newSrcTop, |
robertphillips@google.com | c3410b8 | 2013-03-28 12:25:25 +0000 | [diff] [blame] | 442 | clip2->rect().width(), clip2->rect().height()); |
| 443 | |
| 444 | dbmr->setSrcRect(newSrc); |
| 445 | dbmr->setDstRect(clip2->rect()); |
| 446 | |
| 447 | SkColor color = 0xFF000000; |
| 448 | int a0, a1; |
| 449 | |
| 450 | const SkPaint* saveLayerPaint0 = saveLayer0->paint(); |
| 451 | if (NULL != saveLayerPaint0) { |
| 452 | color = saveLayerPaint0->getColor(); |
| 453 | a0 = SkColorGetA(color); |
| 454 | } else { |
| 455 | a0 = 0xFF; |
| 456 | } |
| 457 | |
| 458 | const SkPaint* saveLayerPaint1 = saveLayer1->paint(); |
| 459 | if (NULL != saveLayerPaint1) { |
| 460 | color = saveLayerPaint1->getColor(); |
| 461 | a1 = SkColorGetA(color); |
| 462 | } else { |
| 463 | a1 = 0xFF; |
| 464 | } |
| 465 | |
robertphillips@google.com | c525704 | 2013-04-02 15:30:03 +0000 | [diff] [blame] | 466 | int newA = SkMulDiv255Round(a0, a1); |
robertphillips@google.com | c3410b8 | 2013-03-28 12:25:25 +0000 | [diff] [blame] | 467 | SkASSERT(newA <= 0xFF); |
| 468 | |
| 469 | SkPaint* dbmrPaint = dbmr->paint(); |
| 470 | |
| 471 | if (NULL != dbmrPaint) { |
| 472 | SkColor newColor = SkColorSetA(dbmrPaint->getColor(), newA); |
| 473 | dbmrPaint->setColor(newColor); |
| 474 | } else { |
| 475 | SkColor newColor = SkColorSetA(color, newA); |
| 476 | |
| 477 | SkPaint newPaint; |
| 478 | newPaint.setColor(newColor); |
| 479 | dbmr->setPaint(newPaint); |
| 480 | } |
| 481 | |
| 482 | // remove everything except the drawbitmaprect |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 483 | canvas->deleteDrawCommandAt(curCommand+13); // restore |
| 484 | canvas->deleteDrawCommandAt(curCommand+12); // restore |
| 485 | canvas->deleteDrawCommandAt(curCommand+11); // restore |
| 486 | canvas->deleteDrawCommandAt(curCommand+10); // restore |
| 487 | canvas->deleteDrawCommandAt(curCommand+9); // restore |
| 488 | canvas->deleteDrawCommandAt(curCommand+7); // clipRect |
| 489 | canvas->deleteDrawCommandAt(curCommand+6); // save |
| 490 | canvas->deleteDrawCommandAt(curCommand+5); // saveLayer |
| 491 | canvas->deleteDrawCommandAt(curCommand+4); // clipRect |
| 492 | canvas->deleteDrawCommandAt(curCommand+3); // save |
| 493 | canvas->deleteDrawCommandAt(curCommand+2); // saveLayer |
| 494 | canvas->deleteDrawCommandAt(curCommand+1); // clipRect |
| 495 | canvas->deleteDrawCommandAt(curCommand); // save |
robertphillips@google.com | c3410b8 | 2013-03-28 12:25:25 +0000 | [diff] [blame] | 496 | } |
robertphillips@google.com | 9105ad0 | 2013-03-17 18:46:16 +0000 | [diff] [blame] | 497 | |
commit-bot@chromium.org | 3bdf164 | 2013-04-01 21:00:27 +0000 | [diff] [blame] | 498 | // Check for: |
| 499 | // SAVE |
| 500 | // CLIP_RECT |
| 501 | // DRAWBITMAPRECTTORECT |
| 502 | // RESTORE |
| 503 | // where: |
| 504 | // the drawBitmapRectToRect is a 1-1 copy from src to dest |
| 505 | // the clip rect is BW and a subset of the drawBitmapRectToRect's dest rect |
| 506 | static bool check_8(SkDebugCanvas* canvas, int curCommand) { |
| 507 | if (SAVE != canvas->getDrawCommandAt(curCommand)->getType() || |
| 508 | canvas->getSize() <= curCommand+4 || |
| 509 | CLIP_RECT != canvas->getDrawCommandAt(curCommand+1)->getType() || |
| 510 | DRAW_BITMAP_RECT_TO_RECT != canvas->getDrawCommandAt(curCommand+2)->getType() || |
| 511 | RESTORE != canvas->getDrawCommandAt(curCommand+3)->getType()) { |
| 512 | return false; |
| 513 | } |
| 514 | |
| 515 | ClipRect* clip = (ClipRect*) canvas->getDrawCommandAt(curCommand+1); |
| 516 | DrawBitmapRect* dbmr = (DrawBitmapRect*) canvas->getDrawCommandAt(curCommand+2); |
| 517 | |
| 518 | if (clip->doAA() || SkRegion::kIntersect_Op != clip->op()) { |
| 519 | return false; |
| 520 | } |
| 521 | |
| 522 | // The src->dest mapping needs to be 1-to-1 |
| 523 | if (NULL == dbmr->srcRect()) { |
| 524 | if (dbmr->bitmap().width() != dbmr->dstRect().width() || |
| 525 | dbmr->bitmap().height() != dbmr->dstRect().height()) { |
| 526 | return false; |
| 527 | } |
| 528 | } else { |
| 529 | if (dbmr->srcRect()->width() != dbmr->dstRect().width() || |
| 530 | dbmr->srcRect()->height() != dbmr->dstRect().height()) { |
| 531 | return false; |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | if (!dbmr->dstRect().contains(clip->rect())) { |
| 536 | return false; |
| 537 | } |
| 538 | |
| 539 | return true; |
| 540 | } |
| 541 | |
| 542 | // Fold the clipRect into the drawBitmapRectToRect's src and dest rects |
| 543 | static void apply_8(SkDebugCanvas* canvas, int curCommand) { |
| 544 | ClipRect* clip = (ClipRect*) canvas->getDrawCommandAt(curCommand+1); |
| 545 | DrawBitmapRect* dbmr = (DrawBitmapRect*) canvas->getDrawCommandAt(curCommand+2); |
| 546 | |
| 547 | SkScalar newSrcLeft, newSrcTop; |
| 548 | |
| 549 | if (NULL != dbmr->srcRect()) { |
| 550 | newSrcLeft = dbmr->srcRect()->fLeft + clip->rect().fLeft - dbmr->dstRect().fLeft; |
| 551 | newSrcTop = dbmr->srcRect()->fTop + clip->rect().fTop - dbmr->dstRect().fTop; |
| 552 | } else { |
| 553 | newSrcLeft = clip->rect().fLeft - dbmr->dstRect().fLeft; |
| 554 | newSrcTop = clip->rect().fTop - dbmr->dstRect().fTop; |
| 555 | } |
| 556 | |
| 557 | SkRect newSrc = SkRect::MakeXYWH(newSrcLeft, newSrcTop, |
| 558 | clip->rect().width(), clip->rect().height()); |
| 559 | |
| 560 | dbmr->setSrcRect(newSrc); |
| 561 | dbmr->setDstRect(clip->rect()); |
| 562 | |
| 563 | // remove everything except the drawbitmaprect |
| 564 | canvas->deleteDrawCommandAt(curCommand+3); |
| 565 | canvas->deleteDrawCommandAt(curCommand+1); |
| 566 | canvas->deleteDrawCommandAt(curCommand); |
| 567 | } |
| 568 | |
| 569 | // Check for: |
| 570 | // SAVE |
| 571 | // CLIP_RECT |
| 572 | // DRAWBITMAPRECTTORECT |
| 573 | // RESTORE |
| 574 | // where: |
| 575 | // clipRect is BW and encloses the DBMR2R's dest rect |
| 576 | static bool check_9(SkDebugCanvas* canvas, int curCommand) { |
| 577 | if (SAVE != canvas->getDrawCommandAt(curCommand)->getType() || |
| 578 | canvas->getSize() <= curCommand+4 || |
| 579 | CLIP_RECT != canvas->getDrawCommandAt(curCommand+1)->getType() || |
| 580 | DRAW_BITMAP_RECT_TO_RECT != canvas->getDrawCommandAt(curCommand+2)->getType() || |
| 581 | RESTORE != canvas->getDrawCommandAt(curCommand+3)->getType()) { |
| 582 | return false; |
| 583 | } |
| 584 | |
| 585 | ClipRect* clip = (ClipRect*) canvas->getDrawCommandAt(curCommand+1); |
| 586 | DrawBitmapRect* dbmr = (DrawBitmapRect*) canvas->getDrawCommandAt(curCommand+2); |
| 587 | |
| 588 | if (clip->doAA() || SkRegion::kIntersect_Op != clip->op()) { |
| 589 | return false; |
| 590 | } |
| 591 | |
| 592 | if (!clip->rect().contains(dbmr->dstRect())) { |
| 593 | return false; |
| 594 | } |
| 595 | |
| 596 | return true; |
| 597 | } |
| 598 | |
| 599 | // remove everything except the drawbitmaprect |
| 600 | static void apply_9(SkDebugCanvas* canvas, int curCommand) { |
| 601 | canvas->deleteDrawCommandAt(curCommand+3); // restore |
| 602 | // drawBitmapRectToRect |
| 603 | canvas->deleteDrawCommandAt(curCommand+1); // clipRect |
| 604 | canvas->deleteDrawCommandAt(curCommand); // save |
| 605 | } |
| 606 | |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 607 | typedef bool (*PFCheck)(SkDebugCanvas* canvas, int curCommand); |
| 608 | typedef void (*PFApply)(SkDebugCanvas* canvas, int curCommand); |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 609 | |
| 610 | struct OptTableEntry { |
| 611 | PFCheck fCheck; |
| 612 | PFApply fApply; |
| 613 | int fNumTimesApplied; |
| 614 | } gOptTable[] = { |
| 615 | { check_0, apply_0, 0 }, |
| 616 | { check_1, apply_1, 0 }, |
robertphillips@google.com | febc0ec | 2013-03-11 22:53:11 +0000 | [diff] [blame] | 617 | { check_2, apply_2, 0 }, |
| 618 | { check_3, apply_3, 0 }, |
| 619 | { check_4, apply_4, 0 }, |
robertphillips@google.com | 9105ad0 | 2013-03-17 18:46:16 +0000 | [diff] [blame] | 620 | { check_5, apply_5, 0 }, |
| 621 | { check_6, apply_6, 0 }, |
robertphillips@google.com | c3410b8 | 2013-03-28 12:25:25 +0000 | [diff] [blame] | 622 | { check_7, apply_7, 0 }, |
commit-bot@chromium.org | 3bdf164 | 2013-04-01 21:00:27 +0000 | [diff] [blame] | 623 | { check_8, apply_8, 0 }, |
| 624 | { check_9, apply_9, 0 }, |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 625 | }; |
| 626 | |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 627 | |
robertphillips@google.com | 3b0a9fe | 2013-01-31 15:56:22 +0000 | [diff] [blame] | 628 | static int filter_picture(const SkString& inFile, const SkString& outFile) { |
robertphillips@google.com | 4d98b74 | 2013-05-10 14:51:54 +0000 | [diff] [blame^] | 629 | SkAutoTDelete<SkPicture> inPicture; |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 630 | |
| 631 | SkFILEStream inStream(inFile.c_str()); |
| 632 | if (inStream.isValid()) { |
robertphillips@google.com | 4d98b74 | 2013-05-10 14:51:54 +0000 | [diff] [blame^] | 633 | inPicture.reset(SkNEW_ARGS(SkPicture, (&inStream, NULL, &SkImageDecoder::DecodeMemory))); |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 634 | } |
| 635 | |
robertphillips@google.com | 4d98b74 | 2013-05-10 14:51:54 +0000 | [diff] [blame^] | 636 | if (NULL == inPicture.get()) { |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 637 | SkDebugf("Could not read file %s\n", inFile.c_str()); |
| 638 | return -1; |
| 639 | } |
| 640 | |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 641 | int localCount[SK_ARRAY_COUNT(gOptTable)]; |
| 642 | |
| 643 | memset(localCount, 0, sizeof(localCount)); |
| 644 | |
robertphillips@google.com | 3b0a9fe | 2013-01-31 15:56:22 +0000 | [diff] [blame] | 645 | SkDebugCanvas debugCanvas(inPicture->width(), inPicture->height()); |
| 646 | debugCanvas.setBounds(inPicture->width(), inPicture->height()); |
| 647 | inPicture->draw(&debugCanvas); |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 648 | |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 649 | // delete the initial save and restore since replaying the commands will |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 650 | // re-add them |
robertphillips@google.com | 50c84da | 2013-04-01 18:18:49 +0000 | [diff] [blame] | 651 | if (debugCanvas.getSize() > 1) { |
| 652 | debugCanvas.deleteDrawCommandAt(0); |
| 653 | debugCanvas.deleteDrawCommandAt(debugCanvas.getSize()-1); |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 654 | } |
| 655 | |
robertphillips@google.com | d9c1853 | 2013-04-01 19:10:21 +0000 | [diff] [blame] | 656 | bool changed = true; |
robertphillips@google.com | 2e87ba0 | 2013-04-08 15:45:30 +0000 | [diff] [blame] | 657 | int numBefore = debugCanvas.getSize(); |
robertphillips@google.com | d9c1853 | 2013-04-01 19:10:21 +0000 | [diff] [blame] | 658 | |
| 659 | while (changed) { |
| 660 | changed = false; |
| 661 | for (int i = 0; i < debugCanvas.getSize(); ++i) { |
| 662 | for (size_t opt = 0; opt < SK_ARRAY_COUNT(gOptTable); ++opt) { |
| 663 | if ((*gOptTable[opt].fCheck)(&debugCanvas, i)) { |
| 664 | (*gOptTable[opt].fApply)(&debugCanvas, i); |
| 665 | |
| 666 | ++gOptTable[opt].fNumTimesApplied; |
| 667 | ++localCount[opt]; |
| 668 | |
| 669 | if (debugCanvas.getSize() == i) { |
| 670 | // the optimization removed all the remaining operations |
| 671 | break; |
| 672 | } |
| 673 | |
| 674 | opt = 0; // try all the opts all over again |
| 675 | changed = true; |
| 676 | } |
robertphillips@google.com | 3b0a9fe | 2013-01-31 15:56:22 +0000 | [diff] [blame] | 677 | } |
| 678 | } |
| 679 | } |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 680 | |
robertphillips@google.com | 2e87ba0 | 2013-04-08 15:45:30 +0000 | [diff] [blame] | 681 | int numAfter = debugCanvas.getSize(); |
| 682 | |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 683 | if (!outFile.isEmpty()) { |
robertphillips@google.com | 3b0a9fe | 2013-01-31 15:56:22 +0000 | [diff] [blame] | 684 | SkPicture outPicture; |
| 685 | |
| 686 | SkCanvas* canvas = outPicture.beginRecording(inPicture->width(), inPicture->height()); |
| 687 | debugCanvas.draw(canvas); |
| 688 | outPicture.endRecording(); |
| 689 | |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 690 | SkFILEWStream outStream(outFile.c_str()); |
| 691 | |
| 692 | outPicture.serialize(&outStream); |
| 693 | } |
| 694 | |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 695 | bool someOptFired = false; |
| 696 | for (size_t opt = 0; opt < SK_ARRAY_COUNT(gOptTable); ++opt) { |
| 697 | if (0 != localCount[opt]) { |
| 698 | SkDebugf("%d: %d ", opt, localCount[opt]); |
| 699 | someOptFired = true; |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | if (!someOptFired) { |
| 704 | SkDebugf("No opts fired\n"); |
| 705 | } else { |
skia.committer@gmail.com | 3284017 | 2013-04-09 07:01:27 +0000 | [diff] [blame] | 706 | SkDebugf("\t before: %d after: %d delta: %d\n", |
robertphillips@google.com | 2e87ba0 | 2013-04-08 15:45:30 +0000 | [diff] [blame] | 707 | numBefore, numAfter, numBefore-numAfter); |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 708 | } |
| 709 | |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 710 | return 0; |
| 711 | } |
| 712 | |
tfarina@chromium.org | a5b7cc0 | 2012-10-08 14:41:10 +0000 | [diff] [blame] | 713 | // This function is not marked as 'static' so it can be referenced externally |
| 714 | // in the iOS build. |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 715 | int tool_main(int argc, char** argv); // suppress a warning on mac |
| 716 | |
caryclark@google.com | 9598f42 | 2012-10-09 12:32:37 +0000 | [diff] [blame] | 717 | int tool_main(int argc, char** argv) { |
robertphillips@google.com | 4d98b74 | 2013-05-10 14:51:54 +0000 | [diff] [blame^] | 718 | #if SK_ENABLE_INST_COUNT |
| 719 | gPrintInstCount = true; |
| 720 | #endif |
| 721 | |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 722 | SkGraphics::Init(); |
| 723 | |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame] | 724 | if (argc < 3) { |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 725 | usage(); |
| 726 | return -1; |
| 727 | } |
| 728 | |
robertphillips@google.com | 3b0a9fe | 2013-01-31 15:56:22 +0000 | [diff] [blame] | 729 | SkString inFile, outFile, inDir, outDir; |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame] | 730 | |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 731 | char* const* stop = argv + argc; |
| 732 | for (++argv; argv < stop; ++argv) { |
| 733 | if (strcmp(*argv, "-i") == 0) { |
| 734 | argv++; |
| 735 | if (argv < stop && **argv) { |
| 736 | inFile.set(*argv); |
| 737 | } else { |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame] | 738 | SkDebugf("missing arg for -i\n"); |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 739 | usage(); |
| 740 | return -1; |
| 741 | } |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 742 | } else if (strcmp(*argv, "--input-dir") == 0) { |
| 743 | argv++; |
| 744 | if (argv < stop && **argv) { |
| 745 | inDir.set(*argv); |
| 746 | } else { |
| 747 | SkDebugf("missing arg for --input-dir\n"); |
| 748 | usage(); |
| 749 | return -1; |
| 750 | } |
| 751 | } else if (strcmp(*argv, "--output-dir") == 0) { |
| 752 | argv++; |
| 753 | if (argv < stop && **argv) { |
| 754 | outDir.set(*argv); |
| 755 | } else { |
| 756 | SkDebugf("missing arg for --output-dir\n"); |
| 757 | usage(); |
| 758 | return -1; |
| 759 | } |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 760 | } else if (strcmp(*argv, "-o") == 0) { |
| 761 | argv++; |
| 762 | if (argv < stop && **argv) { |
| 763 | outFile.set(*argv); |
| 764 | } else { |
robertphillips@google.com | 801cee1 | 2012-10-19 19:06:11 +0000 | [diff] [blame] | 765 | SkDebugf("missing arg for -o\n"); |
| 766 | usage(); |
| 767 | return -1; |
| 768 | } |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 769 | } else if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) { |
| 770 | usage(); |
| 771 | return 0; |
| 772 | } else { |
| 773 | SkDebugf("unknown arg %s\n", *argv); |
| 774 | usage(); |
| 775 | return -1; |
| 776 | } |
| 777 | } |
| 778 | |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 779 | SkOSFile::Iter iter(inDir.c_str(), "skp"); |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 780 | |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 781 | SkString inputFilename, outputFilename; |
| 782 | if (iter.next(&inputFilename)) { |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 783 | |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 784 | do { |
| 785 | sk_tools::make_filepath(&inFile, inDir, inputFilename); |
| 786 | if (!outDir.isEmpty()) { |
| 787 | sk_tools::make_filepath(&outFile, outDir, inputFilename); |
| 788 | } |
| 789 | SkDebugf("Executing %s\n", inputFilename.c_str()); |
robertphillips@google.com | 3b0a9fe | 2013-01-31 15:56:22 +0000 | [diff] [blame] | 790 | filter_picture(inFile, outFile); |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 791 | } while(iter.next(&inputFilename)); |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 792 | |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 793 | } else if (!inFile.isEmpty()) { |
robertphillips@google.com | 3b0a9fe | 2013-01-31 15:56:22 +0000 | [diff] [blame] | 794 | filter_picture(inFile, outFile); |
djsollen@google.com | a09e883 | 2012-11-13 18:50:33 +0000 | [diff] [blame] | 795 | } else { |
| 796 | usage(); |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 797 | return -1; |
| 798 | } |
| 799 | |
robertphillips@google.com | 7374355 | 2013-02-05 20:51:49 +0000 | [diff] [blame] | 800 | for (size_t opt = 0; opt < SK_ARRAY_COUNT(gOptTable); ++opt) { |
| 801 | SkDebugf("opt %d: %d\n", opt, gOptTable[opt].fNumTimesApplied); |
| 802 | } |
| 803 | |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 804 | SkGraphics::Term(); |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 805 | return 0; |
| 806 | } |
| 807 | |
| 808 | #if !defined SK_BUILD_FOR_IOS |
| 809 | int main(int argc, char * const argv[]) { |
caryclark@google.com | 9598f42 | 2012-10-09 12:32:37 +0000 | [diff] [blame] | 810 | return tool_main(argc, (char**) argv); |
robertphillips@google.com | c7e4a5a | 2012-10-04 13:00:33 +0000 | [diff] [blame] | 811 | } |
| 812 | #endif |