Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkRecordOpts.h" | 8 #include "SkRecordOpts.h" |
| 9 | 9 |
| 10 #include "SkRecordPattern.h" | 10 #include "SkRecordPattern.h" |
| 11 #include "SkRecords.h" | 11 #include "SkRecords.h" |
| 12 #include "SkTDArray.h" | 12 #include "SkTDArray.h" |
| 13 | 13 |
| 14 using namespace SkRecords; | 14 using namespace SkRecords; |
| 15 | 15 |
| 16 void SkRecordOptimize(SkRecord* record) { | |
| 17 // This might be useful as a first pass in the future if we want to weed | |
| 18 // out junk for other optimization passes. Right now, nothing needs it, | |
| 19 // and the bounding box hierarchy will do the work of skipping no-op | |
| 20 // Save-NoDraw-Restore sequences better than we can here. | |
| 21 //SkRecordNoopSaveRestores(record); | |
| 22 | |
| 23 SkRecordNoopSaveLayerDrawRestores(record); | |
| 24 SkRecordMergeSvgOpacityAndFilterLayers(record); | |
| 25 } | |
| 26 | |
| 27 // Most of the optimizations in this file are pattern-based. These are all defi ned as structs with: | 16 // Most of the optimizations in this file are pattern-based. These are all defi ned as structs with: |
| 28 // - a Pattern typedef | 17 // - a Pattern typedef |
| 29 // - a bool onMatch(SkRceord*, Pattern*, int begin, int end) method, | 18 // - a bool onMatch(SkRceord*, Pattern*, int begin, int end) method, |
| 30 // which returns true if it made changes and false if not. | 19 // which returns true if it made changes and false if not. |
| 31 | 20 |
| 32 // Run a pattern-based optimization once across the SkRecord, returning true if it made any changes. | 21 // Run a pattern-based optimization once across the SkRecord, returning true if it made any changes. |
| 33 // It looks for spans which match Pass::Pattern, and when found calls onMatch() with the pattern, | 22 // It looks for spans which match Pass::Pattern, and when found calls onMatch() with the pattern, |
| 34 // record, and [begin,end) span of the commands that matched. | 23 // record, and [begin,end) span of the commands that matched. |
| 35 template <typename Pass> | 24 template <typename Pass> |
| 36 static bool apply(Pass* pass, SkRecord* record) { | 25 static bool apply(Pass* pass, SkRecord* record) { |
| 37 typename Pass::Pattern pattern; | 26 typename Pass::Pattern pattern; |
| 38 bool changed = false; | 27 bool changed = false; |
| 39 int begin, end = 0; | 28 int begin, end = 0; |
| 40 | 29 |
| 41 while (pattern.search(record, &begin, &end)) { | 30 while (pattern.search(record, &begin, &end)) { |
| 42 changed |= pass->onMatch(record, &pattern, begin, end); | 31 changed |= pass->onMatch(record, &pattern, begin, end); |
| 43 } | 32 } |
| 44 return changed; | 33 return changed; |
| 45 } | 34 } |
| 46 | 35 |
| 36 //////////////////////////////////////////////////////////////////////////////// /////////////////// | |
| 37 | |
| 38 struct MultipleSetMatrixNooper { | |
| 39 typedef Pattern2<Is<SetMatrix>, | |
| 40 // TODO: tried Star<Or<Is<Noop>, Is<SetMatrix>>>, but it see med to not match | |
|
mtklein
2015/11/19 16:26:56
After refamiliarizing myself with how these patter
reed1
2015/11/19 17:29:44
Done.
Does this mean I should loop myself, or is
| |
| 41 Is<SetMatrix> > | |
| 42 Pattern; | |
| 43 | |
| 44 bool onMatch(SkRecord* record, Pattern* pattern, int begin, int end) { | |
| 45 SkASSERT(begin + 2 == end); | |
| 46 record->replace<NoOp>(begin); // SetMatrix | |
| 47 return true; | |
| 48 } | |
| 49 }; | |
| 50 static void multiple_set_matrices(SkRecord* record) { | |
|
mtklein
2015/11/19 16:26:56
I've found it butt-saving to have each pass unit t
| |
| 51 MultipleSetMatrixNooper pass; | |
|
mtklein
2015/11/19 16:26:56
Might want to move the struct in here too like wit
reed1
2015/11/19 17:29:44
Done.
| |
| 52 | |
| 53 // No need to loop, as we never "open up" opportunities for more of this typ e of optimization. | |
| 54 apply(&pass, record); | |
| 55 } | |
| 56 | |
| 57 //////////////////////////////////////////////////////////////////////////////// /////////////////// | |
| 58 | |
| 59 #if 0 // experimental, but needs knowledge of previous matrix to operate corre ctly | |
| 60 static void apply_matrix_to_draw_params(SkRecord* record) { | |
| 61 struct { | |
| 62 typedef Pattern3<Is<SetMatrix>, | |
| 63 Is<DrawBitmapRect>, | |
| 64 Is<Restore> > | |
| 65 Pattern; | |
| 66 | |
| 67 bool onMatch(SkRecord* record, Pattern* pattern, int begin, int end) { | |
| 68 const SkMatrix& m = pattern->first<SetMatrix>()->matrix; | |
| 69 if (m.getType() > (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask )) { | |
| 70 SkDebugf("too complex\n"); | |
| 71 return false; // can't handle rotation or perspective | |
| 72 } | |
| 73 if (m.getScaleX() < 0 || m.getScaleY() < 0) { | |
| 74 SkDebugf("negative\n"); | |
| 75 return false; // can't represent a flip in a rect | |
| 76 } | |
| 77 DrawBitmapRect* dbr = pattern->second<DrawBitmapRect>(); | |
| 78 m.dump(); | |
| 79 // wack the rect, and eliminate the matrix | |
| 80 m.mapRect(&dbr->dst); | |
| 81 record->replace<NoOp>(begin); | |
| 82 return true; | |
| 83 } | |
| 84 } pass; | |
| 85 apply(&pass, record); | |
| 86 } | |
| 87 #endif | |
| 88 | |
| 89 //////////////////////////////////////////////////////////////////////////////// /////////////////// | |
| 90 | |
| 47 // Turns the logical NoOp Save and Restore in Save-Draw*-Restore patterns into a ctual NoOps. | 91 // Turns the logical NoOp Save and Restore in Save-Draw*-Restore patterns into a ctual NoOps. |
| 48 struct SaveOnlyDrawsRestoreNooper { | 92 struct SaveOnlyDrawsRestoreNooper { |
| 49 typedef Pattern3<Is<Save>, | 93 typedef Pattern3<Is<Save>, |
| 50 Star<Or<Is<NoOp>, IsDraw> >, | 94 Star<Or<Is<NoOp>, IsDraw> >, |
| 51 Is<Restore> > | 95 Is<Restore> > |
| 52 Pattern; | 96 Pattern; |
| 53 | 97 |
| 54 bool onMatch(SkRecord* record, Pattern* pattern, int begin, int end) { | 98 bool onMatch(SkRecord* record, Pattern* pattern, int begin, int end) { |
| 55 record->replace<NoOp>(begin); // Save | 99 record->replace<NoOp>(begin); // Save |
| 56 record->replace<NoOp>(end-1); // Restore | 100 record->replace<NoOp>(end-1); // Restore |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 223 record->replace<NoOp>(saveLayerIndex); // SaveLayer | 267 record->replace<NoOp>(saveLayerIndex); // SaveLayer |
| 224 record->replace<NoOp>(saveLayerIndex + 6); // Restore | 268 record->replace<NoOp>(saveLayerIndex + 6); // Restore |
| 225 return true; | 269 return true; |
| 226 } | 270 } |
| 227 }; | 271 }; |
| 228 | 272 |
| 229 void SkRecordMergeSvgOpacityAndFilterLayers(SkRecord* record) { | 273 void SkRecordMergeSvgOpacityAndFilterLayers(SkRecord* record) { |
| 230 SvgOpacityAndFilterLayerMergePass pass; | 274 SvgOpacityAndFilterLayerMergePass pass; |
| 231 apply(&pass, record); | 275 apply(&pass, record); |
| 232 } | 276 } |
| 277 | |
| 278 //////////////////////////////////////////////////////////////////////////////// /////////////////// | |
| 279 | |
| 280 void SkRecordOptimize(SkRecord* record) { | |
| 281 // This might be useful as a first pass in the future if we want to weed | |
| 282 // out junk for other optimization passes. Right now, nothing needs it, | |
| 283 // and the bounding box hierarchy will do the work of skipping no-op | |
| 284 // Save-NoDraw-Restore sequences better than we can here. | |
| 285 //SkRecordNoopSaveRestores(record); | |
| 286 | |
| 287 SkRecordNoopSaveLayerDrawRestores(record); | |
| 288 SkRecordMergeSvgOpacityAndFilterLayers(record); | |
| 289 } | |
| 290 | |
| 291 void SkRecordOptimize2(SkRecord* record) { | |
| 292 multiple_set_matrices(record); | |
| 293 | |
| 294 SkRecordNoopSaveRestores(record); | |
| 295 | |
| 296 SkRecordNoopSaveLayerDrawRestores(record); | |
| 297 SkRecordMergeSvgOpacityAndFilterLayers(record); | |
| 298 } | |
| 299 | |
| OLD | NEW |