Coverage Report

Created: 2021-08-22 09:07

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