| 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 | static bool apply(Pass* pass, SkRecord* record) { | 
|---|
| 27 | typename Pass::Match match; | 
|---|
| 28 | bool changed = false; | 
|---|
| 29 | int begin, end = 0; | 
|---|
| 30 |  | 
|---|
| 31 | while (match.search(record, &begin, &end)) { | 
|---|
| 32 | changed |= pass->onMatch(record, &match, begin, end); | 
|---|
| 33 | } | 
|---|
| 34 | return changed; | 
|---|
| 35 | } | 
|---|
| 36 |  | 
|---|
| 37 | /////////////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 38 |  | 
|---|
| 39 | static void multiple_set_matrices(SkRecord* record) { | 
|---|
| 40 | struct { | 
|---|
| 41 | typedef Pattern<Is<SetMatrix>, | 
|---|
| 42 | Greedy<Is<NoOp>>, | 
|---|
| 43 | Is<SetMatrix> > | 
|---|
| 44 | Match; | 
|---|
| 45 |  | 
|---|
| 46 | bool onMatch(SkRecord* record, Match* pattern, int begin, int end) { | 
|---|
| 47 | record->replace<NoOp>(begin);  // first SetMatrix | 
|---|
| 48 | return true; | 
|---|
| 49 | } | 
|---|
| 50 | } pass; | 
|---|
| 51 | while (apply(&pass, record)); | 
|---|
| 52 | } | 
|---|
| 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 | bool onMatch(SkRecord* record, Match*, int begin, int end) { | 
|---|
| 84 | record->replace<NoOp>(begin);  // Save | 
|---|
| 85 | record->replace<NoOp>(end-1);  // Restore | 
|---|
| 86 | return true; | 
|---|
| 87 | } | 
|---|
| 88 | }; | 
|---|
| 89 |  | 
|---|
| 90 | static bool fold_opacity_layer_color_to_paint(const SkPaint* layerPaint, | 
|---|
| 91 | bool isSaveLayer, | 
|---|
| 92 | 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 | if (!paint->isSrcOver()) { | 
|---|
| 100 | return false; | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 | 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 | return false; | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | 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 | return false; | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | if (layerPaint) { | 
|---|
| 123 | const uint32_t layerColor = layerPaint->getColor(); | 
|---|
| 124 | // The layer paint color must have only alpha component. | 
|---|
| 125 | if (SK_ColorTRANSPARENT != SkColorSetA(layerColor, SK_AlphaTRANSPARENT)) { | 
|---|
| 126 | return false; | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | // The layer paint can not have any effects. | 
|---|
| 130 | if (layerPaint->getPathEffect()  || | 
|---|
| 131 | layerPaint->getShader()      || | 
|---|
| 132 | !layerPaint->isSrcOver()     || | 
|---|
| 133 | layerPaint->getMaskFilter()  || | 
|---|
| 134 | layerPaint->getColorFilter() || | 
|---|
| 135 | layerPaint->getImageFilter()) { | 
|---|
| 136 | return false; | 
|---|
| 137 | } | 
|---|
| 138 | paint->setAlpha(SkMulDiv255Round(paint->getAlpha(), SkColorGetA(layerColor))); | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | return true; | 
|---|
| 142 | } | 
|---|
| 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 | bool onMatch(SkRecord* record, Match*, int begin, int end) { | 
|---|
| 157 | // The entire span between Save and Restore (inclusively) does nothing. | 
|---|
| 158 | for (int i = begin; i < end; i++) { | 
|---|
| 159 | record->replace<NoOp>(i); | 
|---|
| 160 | } | 
|---|
| 161 | return true; | 
|---|
| 162 | } | 
|---|
| 163 | }; | 
|---|
| 164 | void SkRecordNoopSaveRestores(SkRecord* record) { | 
|---|
| 165 | SaveOnlyDrawsRestoreNooper onlyDraws; | 
|---|
| 166 | SaveNoDrawsRestoreNooper noDraws; | 
|---|
| 167 |  | 
|---|
| 168 | // Run until they stop changing things. | 
|---|
| 169 | while (apply(&onlyDraws, record) || apply(&noDraws, record)); | 
|---|
| 170 | } | 
|---|
| 171 |  | 
|---|
| 172 | #ifndef SK_BUILD_FOR_ANDROID_FRAMEWORK | 
|---|
| 173 | static bool effectively_srcover(const SkPaint* paint) { | 
|---|
| 174 | if (!paint || paint->isSrcOver()) { | 
|---|
| 175 | return true; | 
|---|
| 176 | } | 
|---|
| 177 | // src-mode with opaque and no effects (which might change opaqueness) is ok too. | 
|---|
| 178 | return !paint->getShader() && !paint->getColorFilter() && !paint->getImageFilter() && | 
|---|
| 179 | 0xFF == paint->getAlpha() && paint->getBlendMode() == SkBlendMode::kSrc; | 
|---|
| 180 | } | 
|---|
| 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 | bool onMatch(SkRecord* record, Match* match, int begin, int end) { | 
|---|
| 188 | if (match->first<SaveLayer>()->backdrop || match->first<SaveLayer>()->clipMask) { | 
|---|
| 189 | // can't throw away the layer if we have a backdrop or clip mask | 
|---|
| 190 | return false; | 
|---|
| 191 | } | 
|---|
| 192 |  | 
|---|
| 193 | if (match->first<SaveLayer>()->saveLayerFlags & | 
|---|
| 194 | SkCanvasPriv::kDontClipToLayer_SaveLayerFlag) { | 
|---|
| 195 | // can't throw away the layer if set | 
|---|
| 196 | return false; | 
|---|
| 197 | } | 
|---|
| 198 |  | 
|---|
| 199 | // A SaveLayer's bounds field is just a hint, so we should be free to ignore it. | 
|---|
| 200 | SkPaint* layerPaint = match->first<SaveLayer>()->paint; | 
|---|
| 201 | SkPaint* drawPaint = match->second<SkPaint>(); | 
|---|
| 202 |  | 
|---|
| 203 | if (nullptr == layerPaint && effectively_srcover(drawPaint)) { | 
|---|
| 204 | // There wasn't really any point to this SaveLayer at all. | 
|---|
| 205 | return KillSaveLayerAndRestore(record, begin); | 
|---|
| 206 | } | 
|---|
| 207 |  | 
|---|
| 208 | if (drawPaint == nullptr) { | 
|---|
| 209 | // We can just give the draw the SaveLayer's paint. | 
|---|
| 210 | // TODO(mtklein): figure out how to do this clearly | 
|---|
| 211 | return false; | 
|---|
| 212 | } | 
|---|
| 213 |  | 
|---|
| 214 | if (!fold_opacity_layer_color_to_paint(layerPaint, false /*isSaveLayer*/, drawPaint)) { | 
|---|
| 215 | return false; | 
|---|
| 216 | } | 
|---|
| 217 |  | 
|---|
| 218 | return KillSaveLayerAndRestore(record, begin); | 
|---|
| 219 | } | 
|---|
| 220 |  | 
|---|
| 221 | static bool KillSaveLayerAndRestore(SkRecord* record, int saveLayerIndex) { | 
|---|
| 222 | record->replace<NoOp>(saveLayerIndex);    // SaveLayer | 
|---|
| 223 | record->replace<NoOp>(saveLayerIndex+2);  // Restore | 
|---|
| 224 | return true; | 
|---|
| 225 | } | 
|---|
| 226 | }; | 
|---|
| 227 | void SkRecordNoopSaveLayerDrawRestores(SkRecord* record) { | 
|---|
| 228 | SaveLayerDrawRestoreNooper pass; | 
|---|
| 229 | apply(&pass, record); | 
|---|
| 230 | } | 
|---|
| 231 | #endif | 
|---|
| 232 |  | 
|---|
| 233 | /* For SVG generated: | 
|---|
| 234 | SaveLayer (non-opaque, typically for CSS opacity) | 
|---|
| 235 | Save | 
|---|
| 236 | ClipRect | 
|---|
| 237 | SaveLayer (typically for SVG filter) | 
|---|
| 238 | Restore | 
|---|
| 239 | Restore | 
|---|
| 240 | Restore | 
|---|
| 241 | */ | 
|---|
| 242 | struct SvgOpacityAndFilterLayerMergePass { | 
|---|
| 243 | typedef Pattern<Is<SaveLayer>, Is<Save>, Is<ClipRect>, Is<SaveLayer>, | 
|---|
| 244 | Is<Restore>, Is<Restore>, Is<Restore>> Match; | 
|---|
| 245 |  | 
|---|
| 246 | bool onMatch(SkRecord* record, Match* match, int begin, int end) { | 
|---|
| 247 | if (match->first<SaveLayer>()->backdrop) { | 
|---|
| 248 | // can't throw away the layer if we have a backdrop | 
|---|
| 249 | return false; | 
|---|
| 250 | } | 
|---|
| 251 |  | 
|---|
| 252 | SkPaint* opacityPaint = match->first<SaveLayer>()->paint; | 
|---|
| 253 | if (nullptr == opacityPaint) { | 
|---|
| 254 | // There wasn't really any point to this SaveLayer at all. | 
|---|
| 255 | return KillSaveLayerAndRestore(record, begin); | 
|---|
| 256 | } | 
|---|
| 257 |  | 
|---|
| 258 | // This layer typically contains a filter, but this should work for layers with for other | 
|---|
| 259 | // purposes too. | 
|---|
| 260 | SkPaint* filterLayerPaint = match->fourth<SaveLayer>()->paint; | 
|---|
| 261 | if (filterLayerPaint == nullptr) { | 
|---|
| 262 | // We can just give the inner SaveLayer the paint of the outer SaveLayer. | 
|---|
| 263 | // TODO(mtklein): figure out how to do this clearly | 
|---|
| 264 | return false; | 
|---|
| 265 | } | 
|---|
| 266 |  | 
|---|
| 267 | if (!fold_opacity_layer_color_to_paint(opacityPaint, true /*isSaveLayer*/, | 
|---|
| 268 | filterLayerPaint)) { | 
|---|
| 269 | return false; | 
|---|
| 270 | } | 
|---|
| 271 |  | 
|---|
| 272 | return KillSaveLayerAndRestore(record, begin); | 
|---|
| 273 | } | 
|---|
| 274 |  | 
|---|
| 275 | static bool KillSaveLayerAndRestore(SkRecord* record, int saveLayerIndex) { | 
|---|
| 276 | record->replace<NoOp>(saveLayerIndex);     // SaveLayer | 
|---|
| 277 | record->replace<NoOp>(saveLayerIndex + 6); // Restore | 
|---|
| 278 | return true; | 
|---|
| 279 | } | 
|---|
| 280 | }; | 
|---|
| 281 |  | 
|---|
| 282 | void SkRecordMergeSvgOpacityAndFilterLayers(SkRecord* record) { | 
|---|
| 283 | SvgOpacityAndFilterLayerMergePass pass; | 
|---|
| 284 | apply(&pass, record); | 
|---|
| 285 | } | 
|---|
| 286 |  | 
|---|
| 287 | /////////////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 288 |  | 
|---|
| 289 | void SkRecordOptimize(SkRecord* record) { | 
|---|
| 290 | // This might be useful  as a first pass in the future if we want to weed | 
|---|
| 291 | // out junk for other optimization passes.  Right now, nothing needs it, | 
|---|
| 292 | // and the bounding box hierarchy will do the work of skipping no-op | 
|---|
| 293 | // Save-NoDraw-Restore sequences better than we can here. | 
|---|
| 294 | // As there is a known problem with this peephole and drawAnnotation, disable this. | 
|---|
| 295 | // If we want to enable this we must first fix this bug: | 
|---|
| 296 | //     https://bugs.chromium.org/p/skia/issues/detail?id=5548 | 
|---|
| 297 | //    SkRecordNoopSaveRestores(record); | 
|---|
| 298 |  | 
|---|
| 299 | // Turn off this optimization completely for Android framework | 
|---|
| 300 | // because it makes the following Android CTS test fail: | 
|---|
| 301 | // android.uirendering.cts.testclasses.LayerTests#testSaveLayerClippedWithAlpha | 
|---|
| 302 | #ifndef SK_BUILD_FOR_ANDROID_FRAMEWORK | 
|---|
| 303 | SkRecordNoopSaveLayerDrawRestores(record); | 
|---|
| 304 | #endif | 
|---|
| 305 | SkRecordMergeSvgOpacityAndFilterLayers(record); | 
|---|
| 306 |  | 
|---|
| 307 | record->defrag(); | 
|---|
| 308 | } | 
|---|
| 309 |  | 
|---|
| 310 | void SkRecordOptimize2(SkRecord* record) { | 
|---|
| 311 | multiple_set_matrices(record); | 
|---|
| 312 | SkRecordNoopSaveRestores(record); | 
|---|
| 313 | // See why we turn this off in SkRecordOptimize above. | 
|---|
| 314 | #ifndef SK_BUILD_FOR_ANDROID_FRAMEWORK | 
|---|
| 315 | SkRecordNoopSaveLayerDrawRestores(record); | 
|---|
| 316 | #endif | 
|---|
| 317 | SkRecordMergeSvgOpacityAndFilterLayers(record); | 
|---|
| 318 |  | 
|---|
| 319 | record->defrag(); | 
|---|
| 320 | } | 
|---|
| 321 |  | 
|---|