1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
/*
* Copyright 2014 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkRecordOpts.h"
#include "SkRecordPattern.h"
#include "SkRecords.h"
#include "SkTDArray.h"
using namespace SkRecords;
// Most of the optimizations in this file are pattern-based. These are all defined as structs with:
// - a Match typedef
// - a bool onMatch(SkRceord*, Match*, int begin, int end) method,
// which returns true if it made changes and false if not.
// Run a pattern-based optimization once across the SkRecord, returning true if it made any changes.
// It looks for spans which match Pass::Match, and when found calls onMatch() with that pattern,
// record, and [begin,end) span of the commands that matched.
template <typename Pass>
static bool apply(Pass* pass, SkRecord* record) {
typename Pass::Match match;
bool changed = false;
int begin, end = 0;
while (match.search(record, &begin, &end)) {
changed |= pass->onMatch(record, &match, begin, end);
}
return changed;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
static void multiple_set_matrices(SkRecord* record) {
struct {
typedef Pattern<Is<SetMatrix>,
Greedy<Is<NoOp>>,
Is<SetMatrix> >
Match;
bool onMatch(SkRecord* record, Match* pattern, int begin, int end) {
record->replace<NoOp>(begin); // first SetMatrix
return true;
}
} pass;
while (apply(&pass, record));
}
///////////////////////////////////////////////////////////////////////////////////////////////////
#if 0 // experimental, but needs knowledge of previous matrix to operate correctly
static void apply_matrix_to_draw_params(SkRecord* record) {
struct {
typedef Pattern<Is<SetMatrix>,
Greedy<Is<NoOp>>,
Is<SetMatrix> >
Pattern;
bool onMatch(SkRecord* record, Pattern* pattern, int begin, int end) {
record->replace<NoOp>(begin); // first SetMatrix
return true;
}
} pass;
// No need to loop, as we never "open up" opportunities for more of this type of optimization.
apply(&pass, record);
}
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////
// Turns the logical NoOp Save and Restore in Save-Draw*-Restore patterns into actual NoOps.
struct SaveOnlyDrawsRestoreNooper {
typedef Pattern<Is<Save>,
Greedy<Or<Is<NoOp>, IsDraw>>,
Is<Restore>>
Match;
bool onMatch(SkRecord* record, Match*, int begin, int end) {
record->replace<NoOp>(begin); // Save
record->replace<NoOp>(end-1); // Restore
return true;
}
};
static bool fold_opacity_layer_color_to_paint(const SkPaint* layerPaint,
bool isSaveLayer,
SkPaint* paint) {
// We assume layerPaint is always from a saveLayer. If isSaveLayer is
// true, we assume paint is too.
// The alpha folding can proceed if the filter layer paint does not have properties which cause
// the resulting filter layer to be "blended" in complex ways to the parent layer. For example,
// looper drawing unmodulated filter layer twice and then modulating the result produces
// different image to drawing modulated filter layer twice.
// TODO: most likely the looper and only some xfer modes are the hard constraints
if (!paint->isSrcOver() || paint->getLooper()) {
return false;
}
if (!isSaveLayer && paint->getImageFilter()) {
// For normal draws, the paint color is used as one input for the color for the draw. Image
// filter will operate on the result, and thus we can not change the input.
// For layer saves, the image filter is applied to the layer contents. The layer is then
// modulated with the paint color, so it's fine to proceed with the fold for saveLayer
// paints with image filters.
return false;
}
if (paint->getColorFilter()) {
// Filter input depends on the paint color.
// Here we could filter the color if we knew the draw is going to be uniform color. This
// should be detectable as drawPath/drawRect/.. without a shader being uniform, while
// drawBitmap/drawSprite or a shader being non-uniform. However, current matchers don't
// give the type out easily, so just do not optimize that at the moment.
return false;
}
if (layerPaint) {
const uint32_t layerColor = layerPaint->getColor();
// The layer paint color must have only alpha component.
if (SK_ColorTRANSPARENT != SkColorSetA(layerColor, SK_AlphaTRANSPARENT)) {
return false;
}
// The layer paint can not have any effects.
if (layerPaint->getPathEffect() ||
layerPaint->getShader() ||
!layerPaint->isSrcOver() ||
layerPaint->getMaskFilter() ||
layerPaint->getColorFilter() ||
layerPaint->getRasterizer() ||
layerPaint->getLooper() ||
layerPaint->getImageFilter()) {
return false;
}
paint->setAlpha(SkMulDiv255Round(paint->getAlpha(), SkColorGetA(layerColor)));
}
return true;
}
// Turns logical no-op Save-[non-drawing command]*-Restore patterns into actual no-ops.
struct SaveNoDrawsRestoreNooper {
// Greedy matches greedily, so we also have to exclude Save and Restore.
// Nested SaveLayers need to be excluded, or we'll match their Restore!
typedef Pattern<Is<Save>,
Greedy<Not<Or<Is<Save>,
Is<SaveLayer>,
Is<Restore>,
IsDraw>>>,
Is<Restore>>
Match;
bool onMatch(SkRecord* record, Match*, int begin, int end) {
// The entire span between Save and Restore (inclusively) does nothing.
for (int i = begin; i < end; i++) {
record->replace<NoOp>(i);
}
return true;
}
};
void SkRecordNoopSaveRestores(SkRecord* record) {
SaveOnlyDrawsRestoreNooper onlyDraws;
SaveNoDrawsRestoreNooper noDraws;
// Run until they stop changing things.
while (apply(&onlyDraws, record) || apply(&noDraws, record));
}
#ifndef SK_BUILD_FOR_ANDROID_FRAMEWORK
static bool effectively_srcover(const SkPaint* paint) {
if (!paint || paint->isSrcOver()) {
return true;
}
// src-mode with opaque and no effects (which might change opaqueness) is ok too.
return !paint->getShader() && !paint->getColorFilter() && !paint->getImageFilter() &&
0xFF == paint->getAlpha() && paint->getBlendMode() == SkBlendMode::kSrc;
}
// For some SaveLayer-[drawing command]-Restore patterns, merge the SaveLayer's alpha into the
// draw, and no-op the SaveLayer and Restore.
struct SaveLayerDrawRestoreNooper {
typedef Pattern<Is<SaveLayer>, IsDraw, Is<Restore>> Match;
bool onMatch(SkRecord* record, Match* match, int begin, int end) {
if (match->first<SaveLayer>()->backdrop || match->first<SaveLayer>()->clipMask) {
// can't throw away the layer if we have a backdrop or clip mask
return false;
}
if (match->first<SaveLayer>()->saveLayerFlags & (1U << 31)) {
// can't throw away the layer if the kDontClipToLayer_PrivateSaveLayerFlag is set
return false;
}
// A SaveLayer's bounds field is just a hint, so we should be free to ignore it.
SkPaint* layerPaint = match->first<SaveLayer>()->paint;
SkPaint* drawPaint = match->second<SkPaint>();
if (nullptr == layerPaint && effectively_srcover(drawPaint)) {
// There wasn't really any point to this SaveLayer at all.
return KillSaveLayerAndRestore(record, begin);
}
if (drawPaint == nullptr) {
// We can just give the draw the SaveLayer's paint.
// TODO(mtklein): figure out how to do this clearly
return false;
}
if (!fold_opacity_layer_color_to_paint(layerPaint, false /*isSaveLayer*/, drawPaint)) {
return false;
}
return KillSaveLayerAndRestore(record, begin);
}
static bool KillSaveLayerAndRestore(SkRecord* record, int saveLayerIndex) {
record->replace<NoOp>(saveLayerIndex); // SaveLayer
record->replace<NoOp>(saveLayerIndex+2); // Restore
return true;
}
};
void SkRecordNoopSaveLayerDrawRestores(SkRecord* record) {
SaveLayerDrawRestoreNooper pass;
apply(&pass, record);
}
#endif
/* For SVG generated:
SaveLayer (non-opaque, typically for CSS opacity)
Save
ClipRect
SaveLayer (typically for SVG filter)
Restore
Restore
Restore
*/
struct SvgOpacityAndFilterLayerMergePass {
typedef Pattern<Is<SaveLayer>, Is<Save>, Is<ClipRect>, Is<SaveLayer>,
Is<Restore>, Is<Restore>, Is<Restore>> Match;
bool onMatch(SkRecord* record, Match* match, int begin, int end) {
if (match->first<SaveLayer>()->backdrop) {
// can't throw away the layer if we have a backdrop
return false;
}
SkPaint* opacityPaint = match->first<SaveLayer>()->paint;
if (nullptr == opacityPaint) {
// There wasn't really any point to this SaveLayer at all.
return KillSaveLayerAndRestore(record, begin);
}
// This layer typically contains a filter, but this should work for layers with for other
// purposes too.
SkPaint* filterLayerPaint = match->fourth<SaveLayer>()->paint;
if (filterLayerPaint == nullptr) {
// We can just give the inner SaveLayer the paint of the outer SaveLayer.
// TODO(mtklein): figure out how to do this clearly
return false;
}
if (!fold_opacity_layer_color_to_paint(opacityPaint, true /*isSaveLayer*/,
filterLayerPaint)) {
return false;
}
return KillSaveLayerAndRestore(record, begin);
}
static bool KillSaveLayerAndRestore(SkRecord* record, int saveLayerIndex) {
record->replace<NoOp>(saveLayerIndex); // SaveLayer
record->replace<NoOp>(saveLayerIndex + 6); // Restore
return true;
}
};
void SkRecordMergeSvgOpacityAndFilterLayers(SkRecord* record) {
SvgOpacityAndFilterLayerMergePass pass;
apply(&pass, record);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void SkRecordOptimize(SkRecord* record) {
// This might be useful as a first pass in the future if we want to weed
// out junk for other optimization passes. Right now, nothing needs it,
// and the bounding box hierarchy will do the work of skipping no-op
// Save-NoDraw-Restore sequences better than we can here.
// As there is a known problem with this peephole and drawAnnotation, disable this.
// If we want to enable this we must first fix this bug:
// https://bugs.chromium.org/p/skia/issues/detail?id=5548
// SkRecordNoopSaveRestores(record);
// Turn off this optimization completely for Android framework
// because it makes the following Android CTS test fail:
// android.uirendering.cts.testclasses.LayerTests#testSaveLayerClippedWithAlpha
#ifndef SK_BUILD_FOR_ANDROID_FRAMEWORK
SkRecordNoopSaveLayerDrawRestores(record);
#endif
SkRecordMergeSvgOpacityAndFilterLayers(record);
record->defrag();
}
void SkRecordOptimize2(SkRecord* record) {
multiple_set_matrices(record);
SkRecordNoopSaveRestores(record);
// See why we turn this off in SkRecordOptimize above.
#ifndef SK_BUILD_FOR_ANDROID_FRAMEWORK
SkRecordNoopSaveLayerDrawRestores(record);
#endif
SkRecordMergeSvgOpacityAndFilterLayers(record);
record->defrag();
}
|