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 static void SkRecordNoopSaveLayerSaveLayerRestores(SkRecord* record); | |
mtklein
2015/01/21 16:20:29
This is a sign we need unit tests. :) You'll want
Kimmo Kinnunen
2015/01/22 13:55:01
Done.
| |
15 | 16 |
16 void SkRecordOptimize(SkRecord* record) { | 17 void SkRecordOptimize(SkRecord* record) { |
17 // This might be useful as a first pass in the future if we want to weed | 18 // 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 // 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 // and the bounding box hierarchy will do the work of skipping no-op |
20 // Save-NoDraw-Restore sequences better than we can here. | 21 // Save-NoDraw-Restore sequences better than we can here. |
21 //SkRecordNoopSaveRestores(record); | 22 //SkRecordNoopSaveRestores(record); |
22 | 23 |
23 SkRecordNoopSaveLayerDrawRestores(record); | 24 SkRecordNoopSaveLayerDrawRestores(record); |
25 SkRecordNoopSaveLayerSaveLayerRestores(record); | |
mtklein
2015/01/21 16:20:30
Can you see any SKPs where these optimizations fee
Kimmo Kinnunen
2015/01/22 13:55:01
No, they are not related. They can not generate ne
| |
24 } | 26 } |
25 | 27 |
26 // Most of the optimizations in this file are pattern-based. These are all defi ned as structs with: | 28 // Most of the optimizations in this file are pattern-based. These are all defi ned as structs with: |
27 // - a Pattern typedef | 29 // - a Pattern typedef |
28 // - a bool onMatch(SkRceord*, Pattern*, unsigned begin, unsigned end) method, | 30 // - a bool onMatch(SkRceord*, Pattern*, unsigned begin, unsigned end) method, |
29 // which returns true if it made changes and false if not. | 31 // which returns true if it made changes and false if not. |
30 | 32 |
31 // Run a pattern-based optimization once across the SkRecord, returning true if it made any changes. | 33 // Run a pattern-based optimization once across the SkRecord, returning true if it made any changes. |
32 // It looks for spans which match Pass::Pattern, and when found calls onMatch() with the pattern, | 34 // It looks for spans which match Pass::Pattern, and when found calls onMatch() with the pattern, |
33 // record, and [begin,end) span of the commands that matched. | 35 // record, and [begin,end) span of the commands that matched. |
(...skipping 15 matching lines...) Expand all Loading... | |
49 Star<Or<Is<NoOp>, IsDraw> >, | 51 Star<Or<Is<NoOp>, IsDraw> >, |
50 Is<Restore> > | 52 Is<Restore> > |
51 Pattern; | 53 Pattern; |
52 | 54 |
53 bool onMatch(SkRecord* record, Pattern* pattern, unsigned begin, unsigned en d) { | 55 bool onMatch(SkRecord* record, Pattern* pattern, unsigned begin, unsigned en d) { |
54 record->replace<NoOp>(begin); // Save | 56 record->replace<NoOp>(begin); // Save |
55 record->replace<NoOp>(end-1); // Restore | 57 record->replace<NoOp>(end-1); // Restore |
56 return true; | 58 return true; |
57 } | 59 } |
58 }; | 60 }; |
61 | |
62 static bool has_any_effect(const SkPaint& paint) { | |
63 return paint.getPathEffect() || | |
64 paint.getShader() || | |
65 paint.getXfermode() || | |
66 paint.getMaskFilter() || | |
67 paint.getColorFilter() || | |
68 paint.getRasterizer() || | |
69 paint.getLooper() || | |
70 paint.getImageFilter(); | |
71 } | |
72 | |
73 static bool is_only_alpha(SkColor color) { | |
74 return SK_ColorTRANSPARENT == SkColorSetA(color, SK_AlphaTRANSPARENT); | |
75 } | |
76 | |
77 static SkColor blend_opacity_layer_color(SkColor baseColor, SkColor opacityLayer Color) { | |
78 return SkColorSetA(baseColor, SkFloatToIntRound(SkColorGetA(baseColor) * | |
79 SkColorGetA(opacityLayerColo r) / 255.0f)); | |
80 } | |
81 | |
59 // Turns logical no-op Save-[non-drawing command]*-Restore patterns into actual no-ops. | 82 // Turns logical no-op Save-[non-drawing command]*-Restore patterns into actual no-ops. |
60 struct SaveNoDrawsRestoreNooper { | 83 struct SaveNoDrawsRestoreNooper { |
61 // Star matches greedily, so we also have to exclude Save and Restore. | 84 // Star matches greedily, so we also have to exclude Save and Restore. |
62 // Nested SaveLayers need to be excluded, or we'll match their Restore! | 85 // Nested SaveLayers need to be excluded, or we'll match their Restore! |
63 typedef Pattern3<Is<Save>, | 86 typedef Pattern3<Is<Save>, |
64 Star<Not<Or4<Is<Save>, | 87 Star<Not<Or4<Is<Save>, |
65 Is<SaveLayer>, | 88 Is<SaveLayer>, |
66 Is<Restore>, | 89 Is<Restore>, |
67 IsDraw> > >, | 90 IsDraw> > >, |
68 Is<Restore> > | 91 Is<Restore> > |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
104 | 127 |
105 SkPaint* drawPaint = pattern->second<SkPaint>(); | 128 SkPaint* drawPaint = pattern->second<SkPaint>(); |
106 if (drawPaint == NULL) { | 129 if (drawPaint == NULL) { |
107 // We can just give the draw the SaveLayer's paint. | 130 // We can just give the draw the SaveLayer's paint. |
108 // TODO(mtklein): figure out how to do this clearly | 131 // TODO(mtklein): figure out how to do this clearly |
109 return false; | 132 return false; |
110 } | 133 } |
111 | 134 |
112 const uint32_t layerColor = layerPaint->getColor(); | 135 const uint32_t layerColor = layerPaint->getColor(); |
113 const uint32_t drawColor = drawPaint->getColor(); | 136 const uint32_t drawColor = drawPaint->getColor(); |
114 if (!IsOnlyAlpha(layerColor) || HasAnyEffect(*layerPaint) || CantFoldAlp ha(*drawPaint)) { | 137 if (!is_only_alpha(layerColor) || has_any_effect(*layerPaint) || CantFol dAlpha(*drawPaint)) { |
115 // Too fancy for us. | 138 // Too fancy for us. |
116 return false; | 139 return false; |
117 } | 140 } |
118 | 141 |
119 drawPaint->setColor(SkColorSetA(drawColor, | 142 drawPaint->setColor(blend_opacity_layer_color(drawColor, layerColor)); |
120 SkFloatToIntRound(SkColorGetA(drawColor) * | |
121 SkColorGetA(layerColor ) / 255.0f))); | |
122 return KillSaveLayerAndRestore(record, begin); | 143 return KillSaveLayerAndRestore(record, begin); |
123 } | 144 } |
124 | 145 |
125 static bool KillSaveLayerAndRestore(SkRecord* record, unsigned saveLayerInde x) { | |
126 record->replace<NoOp>(saveLayerIndex); // SaveLayer | |
127 record->replace<NoOp>(saveLayerIndex+2); // Restore | |
128 return true; | |
129 } | |
130 | |
131 static bool HasAnyEffect(const SkPaint& paint) { | |
132 return paint.getPathEffect() || | |
133 paint.getShader() || | |
134 paint.getXfermode() || | |
135 paint.getMaskFilter() || | |
136 paint.getColorFilter() || | |
137 paint.getRasterizer() || | |
138 paint.getLooper() || | |
139 paint.getImageFilter(); | |
140 } | |
141 | |
142 // The alpha folding can proceed if the single draw's paint has a shader, | 146 // The alpha folding can proceed if the single draw's paint has a shader, |
143 // path effect, mask filter and/or rasterizer. | 147 // path effect, mask filter and/or rasterizer. |
144 // TODO: most likely the looper and only some xfer modes are the hard | 148 // TODO: most likely the looper and only some xfer modes are the hard |
145 // constraints | 149 // constraints |
146 static bool CantFoldAlpha(const SkPaint& paint) { | 150 static bool CantFoldAlpha(const SkPaint& paint) { |
147 return paint.getXfermode() || | 151 return paint.getXfermode() || |
148 paint.getColorFilter() || | 152 paint.getColorFilter() || |
149 paint.getLooper() || | 153 paint.getLooper() || |
150 paint.getImageFilter(); | 154 paint.getImageFilter(); |
151 } | 155 } |
152 | 156 |
153 static bool IsOnlyAlpha(SkColor color) { | 157 static bool KillSaveLayerAndRestore(SkRecord* record, unsigned saveLayerInde x) { |
154 return SK_ColorTRANSPARENT == SkColorSetA(color, SK_AlphaTRANSPARENT); | 158 record->replace<NoOp>(saveLayerIndex); // SaveLayer |
159 record->replace<NoOp>(saveLayerIndex+2); // Restore | |
160 return true; | |
155 } | 161 } |
156 }; | 162 }; |
157 void SkRecordNoopSaveLayerDrawRestores(SkRecord* record) { | 163 void SkRecordNoopSaveLayerDrawRestores(SkRecord* record) { |
158 SaveLayerDrawRestoreNooper pass; | 164 SaveLayerDrawRestoreNooper pass; |
159 apply(&pass, record); | 165 apply(&pass, record); |
160 } | 166 } |
161 | 167 |
168 | |
169 /* For SVG generated: | |
170 SaveLayer (non-opaque) | |
mtklein
2015/01/13 21:12:20
This pattern seems very specific. This makes me w
Kimmo Kinnunen
2015/01/22 13:55:01
Acknowledged.
| |
171 Save | |
172 Clip Rect | |
173 Save Layer | |
174 Restore | |
175 Restore | |
176 Restore | |
177 */ | |
178 struct SaveLayerSaveLayerRestoreNooper { | |
mtklein
2015/01/21 16:20:29
Might be I've set a bad naming precedent here. Ma
Kimmo Kinnunen
2015/01/22 13:55:01
Done.
| |
179 typedef Pattern7<Is<SaveLayer>, Is<Save>, Is<ClipRect>, Is<SaveLayer>, | |
180 Is<Restore>, Is<Restore>, Is<Restore> > Pattern; | |
181 | |
182 bool onMatch(SkRecord* record, Pattern* pattern, unsigned begin, unsigned en d) { | |
183 SaveLayer* opacityLayer = pattern->first<SaveLayer>(); | |
184 if (opacityLayer->bounds != NULL) { | |
185 // SaveLayer with bounds is too tricky for us. | |
mtklein
2015/01/21 16:20:29
I removed this check in the other pattern. I thin
Kimmo Kinnunen
2015/01/22 13:55:01
Done.
| |
186 return false; | |
187 } | |
188 | |
189 SkPaint* opacityPaint = opacityLayer->paint; | |
190 if (NULL == opacityPaint) { | |
191 // There wasn't really any point to this SaveLayer at all. | |
192 return KillSaveLayerAndRestore(record, begin); | |
193 } | |
194 | |
195 SkPaint* secondLayerPaint = pattern->fourth<SaveLayer>()->paint; | |
196 if (secondLayerPaint == NULL) { | |
197 // We can just give the draw the SaveLayer's paint. | |
mtklein
2015/01/21 16:20:29
// We can just give the inner SaveLayer the outer
Kimmo Kinnunen
2015/01/22 13:55:01
Done.
| |
198 // TODO(mtklein): figure out how to do this clearly | |
199 return false; | |
200 } | |
201 | |
202 const uint32_t opacityColor = opacityPaint->getColor(); | |
203 const uint32_t secondLayerColor = secondLayerPaint->getColor(); | |
204 if (!is_only_alpha(opacityColor) || has_any_effect(*opacityPaint)) { | |
mtklein
2015/01/21 16:20:29
We're not doing a CantFoldAlpha check here, and I'
Kimmo Kinnunen
2015/01/22 13:55:01
Done.
| |
205 // Too fancy for us. | |
206 return false; | |
207 } | |
208 | |
209 secondLayerPaint->setColor(blend_opacity_layer_color(secondLayerColor, o pacityColor)); | |
210 return KillSaveLayerAndRestore(record, begin); | |
211 } | |
212 | |
213 static bool KillSaveLayerAndRestore(SkRecord* record, unsigned saveLayerInde x) { | |
214 record->replace<NoOp>(saveLayerIndex); // SaveLayer | |
215 record->replace<NoOp>(saveLayerIndex + 6); // Restore | |
216 return true; | |
217 } | |
218 }; | |
219 | |
220 static void SkRecordNoopSaveLayerSaveLayerRestores(SkRecord* record) { | |
221 SaveLayerSaveLayerRestoreNooper pass; | |
222 apply(&pass, record); | |
223 } | |
OLD | NEW |