| 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) { | 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 |
| 17 SkRecordNoopSaveLayerDrawRestores(record); | 23 SkRecordNoopSaveLayerDrawRestores(record); |
| 18 SkRecordNoopSaveRestores(record); | |
| 19 SkRecordReduceDrawPosTextStrength(record); | |
| 20 } | 24 } |
| 21 | 25 |
| 22 // Most of the optimizations in this file are pattern-based. These are all defi
ned as structs with: | 26 // Most of the optimizations in this file are pattern-based. These are all defi
ned as structs with: |
| 23 // - a Pattern typedef | 27 // - a Pattern typedef |
| 24 // - a bool onMatch(SkRceord*, Pattern*, unsigned begin, unsigned end) method, | 28 // - a bool onMatch(SkRceord*, Pattern*, unsigned begin, unsigned end) method, |
| 25 // which returns true if it made changes and false if not. | 29 // which returns true if it made changes and false if not. |
| 26 | 30 |
| 27 // Run a pattern-based optimization once across the SkRecord, returning true if
it made any changes. | 31 // Run a pattern-based optimization once across the SkRecord, returning true if
it made any changes. |
| 28 // It looks for spans which match Pass::Pattern, and when found calls onMatch()
with the pattern, | 32 // It looks for spans which match Pass::Pattern, and when found calls onMatch()
with the pattern, |
| 29 // record, and [begin,end) span of the commands that matched. | 33 // record, and [begin,end) span of the commands that matched. |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 } | 144 } |
| 141 static bool IsOnlyAlpha(SkColor color) { | 145 static bool IsOnlyAlpha(SkColor color) { |
| 142 return SK_ColorTRANSPARENT == SkColorSetA(color, SK_AlphaTRANSPARENT); | 146 return SK_ColorTRANSPARENT == SkColorSetA(color, SK_AlphaTRANSPARENT); |
| 143 } | 147 } |
| 144 }; | 148 }; |
| 145 void SkRecordNoopSaveLayerDrawRestores(SkRecord* record) { | 149 void SkRecordNoopSaveLayerDrawRestores(SkRecord* record) { |
| 146 SaveLayerDrawRestoreNooper pass; | 150 SaveLayerDrawRestoreNooper pass; |
| 147 apply(&pass, record); | 151 apply(&pass, record); |
| 148 } | 152 } |
| 149 | 153 |
| 150 | |
| 151 // Replaces DrawPosText with DrawPosTextH when all Y coordinates are equal. | |
| 152 struct StrengthReducer { | |
| 153 typedef Pattern1<Is<DrawPosText> > Pattern; | |
| 154 | |
| 155 bool onMatch(SkRecord* record, Pattern* pattern, unsigned begin, unsigned en
d) { | |
| 156 SkASSERT(end == begin + 1); | |
| 157 DrawPosText* draw = pattern->first<DrawPosText>(); | |
| 158 | |
| 159 const unsigned points = draw->paint.countText(draw->text, draw->byteLeng
th); | |
| 160 if (points == 0) { | |
| 161 return false; // No point (ha!). | |
| 162 } | |
| 163 | |
| 164 const SkScalar firstY = draw->pos[0].fY; | |
| 165 for (unsigned i = 1; i < points; i++) { | |
| 166 if (draw->pos[i].fY != firstY) { | |
| 167 return false; // Needs full power of DrawPosText. | |
| 168 } | |
| 169 } | |
| 170 // All ys are the same. We can replace DrawPosText with DrawPosTextH. | |
| 171 | |
| 172 // draw->pos is points SkPoints, [(x,y),(x,y),(x,y),(x,y), ... ]. | |
| 173 // We're going to squint and look at that as 2*points SkScalars, [x,y,x,
y,x,y,x,y, ...]. | |
| 174 // Then we'll rearrange things so all the xs are in order up front, clob
bering the ys. | |
| 175 SK_COMPILE_ASSERT(sizeof(SkPoint) == 2 * sizeof(SkScalar), SquintingIsNo
tSafe); | |
| 176 SkScalar* scalars = &draw->pos[0].fX; | |
| 177 for (unsigned i = 0; i < 2*points; i += 2) { | |
| 178 scalars[i/2] = scalars[i]; | |
| 179 } | |
| 180 | |
| 181 // Extend lifetime of draw to the end of the loop so we can copy its pai
nt. | |
| 182 Adopted<DrawPosText> adopted(draw); | |
| 183 SkNEW_PLACEMENT_ARGS(record->replace<DrawPosTextH>(begin, adopted), | |
| 184 DrawPosTextH, | |
| 185 (draw->paint, draw->text, draw->byteLength, scalars
, firstY)); | |
| 186 return true; | |
| 187 } | |
| 188 }; | |
| 189 void SkRecordReduceDrawPosTextStrength(SkRecord* record) { | |
| 190 StrengthReducer pass; | |
| 191 apply(&pass, record); | |
| 192 } | |
| 193 | |
| OLD | NEW |