| 1 | // Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef FLUTTER_FLOW_LAYERS_IMAGE_FILTER_LAYER_H_ |
| 6 | #define FLUTTER_FLOW_LAYERS_IMAGE_FILTER_LAYER_H_ |
| 7 | |
| 8 | #include "flutter/flow/layers/container_layer.h" |
| 9 | |
| 10 | #include "third_party/skia/include/core/SkImageFilter.h" |
| 11 | |
| 12 | namespace flutter { |
| 13 | |
| 14 | class ImageFilterLayer : public MergedContainerLayer { |
| 15 | public: |
| 16 | ImageFilterLayer(sk_sp<SkImageFilter> filter); |
| 17 | |
| 18 | void Preroll(PrerollContext* context, const SkMatrix& matrix) override; |
| 19 | |
| 20 | void Paint(PaintContext& context) const override; |
| 21 | |
| 22 | private: |
| 23 | // The ImageFilterLayer might cache the filtered output of this layer |
| 24 | // if the layer remains stable (if it is not animating for instance). |
| 25 | // If the ImageFilterLayer is not the same between rendered frames, |
| 26 | // though, it will cache its children instead and filter their cached |
| 27 | // output on the fly. |
| 28 | // Caching just the children saves the time to render them and also |
| 29 | // avoids a rendering surface switch to draw them. |
| 30 | // Caching the layer itself avoids all of that and additionally avoids |
| 31 | // the cost of applying the filter, but can be worse than caching the |
| 32 | // children if the filter itself is not stable from frame to frame. |
| 33 | // This constant controls how many times we will Preroll and Paint this |
| 34 | // same ImageFilterLayer before we consider the layer and filter to be |
| 35 | // stable enough to switch from caching the children to caching the |
| 36 | // filtered output of this layer. |
| 37 | static constexpr int kMinimumRendersBeforeCachingFilterLayer = 3; |
| 38 | |
| 39 | sk_sp<SkImageFilter> filter_; |
| 40 | sk_sp<SkImageFilter> transformed_filter_; |
| 41 | int render_count_; |
| 42 | |
| 43 | FML_DISALLOW_COPY_AND_ASSIGN(ImageFilterLayer); |
| 44 | }; |
| 45 | |
| 46 | } // namespace flutter |
| 47 | |
| 48 | #endif // FLUTTER_FLOW_LAYERS_IMAGE_FILTER_LAYER_H_ |
| 49 | |