1 /*
2 * Copyright 2014 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
8 #include "SkRecordOpts.h"
9
10 #include "SkRecordPattern.h"
11 #include "SkRecords.h"
12 #include "SkTDArray.h"
13
14 using namespace SkRecords;
15
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
23 SkRecordNoopSaveLayerDrawRestores(record);
24 SkRecordMergeSvgOpacityAndFilterLayers(record);
25 }
26
27 // Most of the optimizations in this file are pattern-based. These are all defined as structs with:
28 // - a Pattern typedef
29 // - a bool onMatch(SkRceord*, Pattern*, unsigned begin, unsigned end) method,
30 // which returns true if it made changes and false if not.
31
32 // 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,
34 // record, and [begin,end) span of the commands that matched.
35 template <typename Pass>
apply(Pass * pass,SkRecord * record)36 static bool apply(Pass* pass, SkRecord* record) {
37 typename Pass::Pattern pattern;
38 bool changed = false;
39 unsigned begin, end = 0;
40
41 while (pattern.search(record, &begin, &end)) {
42 changed |= pass->onMatch(record, &pattern, begin, end);
43 }
44 return changed;
45 }
46
47 // Turns the logical NoOp Save and Restore in Save-Draw*-Restore patterns into actual NoOps.
48 struct SaveOnlyDrawsRestoreNooper {
49 typedef Pattern3<Is<Save>,
50 Star<Or<Is<NoOp>, IsDraw> >,
51 Is<Restore> >
52 Pattern;
53
onMatchSaveOnlyDrawsRestoreNooper54 bool onMatch(SkRecord* record, Pattern* pattern, unsigned begin, unsigned end) {
55 record->replace<NoOp>(begin); // Save
56 record->replace<NoOp>(end-1); // Restore
57 return true;
58 }
59 };
60
fold_opacity_layer_color_to_paint(const SkPaint & layerPaint,bool isSaveLayer,SkPaint * paint)61 static bool fold_opacity_layer_color_to_paint(const SkPaint& layerPaint,
62 bool isSaveLayer,
63 SkPaint* paint) {
64 // We assume layerPaint is always from a saveLayer. If isSaveLayer is
65 // true, we assume paint is too.
66
67 // The alpha folding can proceed if the filter layer paint does not have properties which cause
68 // the resulting filter layer to be "blended" in complex ways to the parent layer. For example,
69 // looper drawing unmodulated filter layer twice and then modulating the result produces
70 // different image to drawing modulated filter layer twice.
71 // TODO: most likely the looper and only some xfer modes are the hard constraints
72 if (paint->getXfermode() || paint->getLooper()) {
73 return false;
74 }
75
76 if (!isSaveLayer && paint->getImageFilter()) {
77 // For normal draws, the paint color is used as one input for the color for the draw. Image
78 // filter will operate on the result, and thus we can not change the input.
79 // For layer saves, the image filter is applied to the layer contents. The layer is then
80 // modulated with the paint color, so it's fine to proceed with the fold for saveLayer
81 // paints with image filters.
82 return false;
83 }
84
85 if (paint->getColorFilter()) {
86 // Filter input depends on the paint color.
87
88 // Here we could filter the color if we knew the draw is going to be uniform color. This
89 // should be detectable as drawPath/drawRect/.. without a shader being uniform, while
90 // drawBitmap/drawSprite or a shader being non-uniform. However, current matchers don't
91 // give the type out easily, so just do not optimize that at the moment.
92 return false;
93 }
94
95 const uint32_t layerColor = layerPaint.getColor();
96 // The layer paint color must have only alpha component.
97 if (SK_ColorTRANSPARENT != SkColorSetA(layerColor, SK_AlphaTRANSPARENT)) {
98 return false;
99 }
100
101 // The layer paint can not have any effects.
102 if (layerPaint.getPathEffect() ||
103 layerPaint.getShader() ||
104 layerPaint.getXfermode() ||
105 layerPaint.getMaskFilter() ||
106 layerPaint.getColorFilter() ||
107 layerPaint.getRasterizer() ||
108 layerPaint.getLooper() ||
109 layerPaint.getImageFilter()) {
110 return false;
111 }
112
113 paint->setAlpha(SkMulDiv255Round(paint->getAlpha(), SkColorGetA(layerColor)));
114
115 return true;
116 }
117
118 // Turns logical no-op Save-[non-drawing command]*-Restore patterns into actual no-ops.
119 struct SaveNoDrawsRestoreNooper {
120 // Star matches greedily, so we also have to exclude Save and Restore.
121 // Nested SaveLayers need to be excluded, or we'll match their Restore!
122 typedef Pattern3<Is<Save>,
123 Star<Not<Or4<Is<Save>,
124 Is<SaveLayer>,
125 Is<Restore>,
126 IsDraw> > >,
127 Is<Restore> >
128 Pattern;
129
onMatchSaveNoDrawsRestoreNooper130 bool onMatch(SkRecord* record, Pattern* pattern, unsigned begin, unsigned end) {
131 // The entire span between Save and Restore (inclusively) does nothing.
132 for (unsigned i = begin; i < end; i++) {
133 record->replace<NoOp>(i);
134 }
135 return true;
136 }
137 };
SkRecordNoopSaveRestores(SkRecord * record)138 void SkRecordNoopSaveRestores(SkRecord* record) {
139 SaveOnlyDrawsRestoreNooper onlyDraws;
140 SaveNoDrawsRestoreNooper noDraws;
141
142 // Run until they stop changing things.
143 while (apply(&onlyDraws, record) || apply(&noDraws, record));
144 }
145
146 // For some SaveLayer-[drawing command]-Restore patterns, merge the SaveLayer's alpha into the
147 // draw, and no-op the SaveLayer and Restore.
148 struct SaveLayerDrawRestoreNooper {
149 typedef Pattern3<Is<SaveLayer>, IsDraw, Is<Restore> > Pattern;
150
onMatchSaveLayerDrawRestoreNooper151 bool onMatch(SkRecord* record, Pattern* pattern, unsigned begin, unsigned end) {
152 // A SaveLayer's bounds field is just a hint, so we should be free to ignore it.
153 SkPaint* layerPaint = pattern->first<SaveLayer>()->paint;
154 if (NULL == layerPaint) {
155 // There wasn't really any point to this SaveLayer at all.
156 return KillSaveLayerAndRestore(record, begin);
157 }
158
159 SkPaint* drawPaint = pattern->second<SkPaint>();
160 if (drawPaint == NULL) {
161 // We can just give the draw the SaveLayer's paint.
162 // TODO(mtklein): figure out how to do this clearly
163 return false;
164 }
165
166 if (!fold_opacity_layer_color_to_paint(*layerPaint, false /*isSaveLayer*/, drawPaint)) {
167 return false;
168 }
169
170 return KillSaveLayerAndRestore(record, begin);
171 }
172
KillSaveLayerAndRestoreSaveLayerDrawRestoreNooper173 static bool KillSaveLayerAndRestore(SkRecord* record, unsigned saveLayerIndex) {
174 record->replace<NoOp>(saveLayerIndex); // SaveLayer
175 record->replace<NoOp>(saveLayerIndex+2); // Restore
176 return true;
177 }
178 };
SkRecordNoopSaveLayerDrawRestores(SkRecord * record)179 void SkRecordNoopSaveLayerDrawRestores(SkRecord* record) {
180 SaveLayerDrawRestoreNooper pass;
181 apply(&pass, record);
182 }
183
184
185 /* For SVG generated:
186 SaveLayer (non-opaque, typically for CSS opacity)
187 Save
188 ClipRect
189 SaveLayer (typically for SVG filter)
190 Restore
191 Restore
192 Restore
193 */
194 struct SvgOpacityAndFilterLayerMergePass {
195 typedef Pattern7<Is<SaveLayer>, Is<Save>, Is<ClipRect>, Is<SaveLayer>,
196 Is<Restore>, Is<Restore>, Is<Restore> > Pattern;
197
onMatchSvgOpacityAndFilterLayerMergePass198 bool onMatch(SkRecord* record, Pattern* pattern, unsigned begin, unsigned end) {
199 SkPaint* opacityPaint = pattern->first<SaveLayer>()->paint;
200 if (NULL == opacityPaint) {
201 // There wasn't really any point to this SaveLayer at all.
202 return KillSaveLayerAndRestore(record, begin);
203 }
204
205 // This layer typically contains a filter, but this should work for layers with for other
206 // purposes too.
207 SkPaint* filterLayerPaint = pattern->fourth<SaveLayer>()->paint;
208 if (filterLayerPaint == NULL) {
209 // We can just give the inner SaveLayer the paint of the outer SaveLayer.
210 // TODO(mtklein): figure out how to do this clearly
211 return false;
212 }
213
214 if (!fold_opacity_layer_color_to_paint(*opacityPaint, true /*isSaveLayer*/,
215 filterLayerPaint)) {
216 return false;
217 }
218
219 return KillSaveLayerAndRestore(record, begin);
220 }
221
KillSaveLayerAndRestoreSvgOpacityAndFilterLayerMergePass222 static bool KillSaveLayerAndRestore(SkRecord* record, unsigned saveLayerIndex) {
223 record->replace<NoOp>(saveLayerIndex); // SaveLayer
224 record->replace<NoOp>(saveLayerIndex + 6); // Restore
225 return true;
226 }
227 };
228
SkRecordMergeSvgOpacityAndFilterLayers(SkRecord * record)229 void SkRecordMergeSvgOpacityAndFilterLayers(SkRecord* record) {
230 SvgOpacityAndFilterLayerMergePass pass;
231 apply(&pass, record);
232 }
233