• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_OPACITY_LAYER_H_
6 #define FLUTTER_FLOW_LAYERS_OPACITY_LAYER_H_
7 
8 #include "flutter/flow/layers/container_layer.h"
9 
10 namespace flutter {
11 
12 // Don't add an OpacityLayer with no children to the layer tree. Painting an
13 // OpacityLayer is very costly due to the saveLayer call. If there's no child,
14 // having the OpacityLayer or not has the same effect. In debug_unopt build, the
15 // |EnsureSingleChild| will assert if there are no children.
16 class OpacityLayer : public ContainerLayer {
17  public:
18   // An offset is provided here because OpacityLayer.addToScene method in the
19   // Flutter framework can take an optional offset argument.
20   //
21   // By default, that offset is always zero, and all the offsets are handled by
22   // some parent TransformLayers. But we allow the offset to be non-zero for
23   // backward compatibility. If it's non-zero, the old behavior is to propage
24   // that offset to all the leaf layers (e.g., PictureLayer). That will make
25   // the retained rendering inefficient as a small offset change could propagate
26   // to many leaf layers. Therefore we try to capture that offset here to stop
27   // the propagation as repainting the OpacityLayer is expensive.
28   OpacityLayer(int alpha, const SkPoint& offset);
29   ~OpacityLayer() override;
30 
31   void Preroll(PrerollContext* context, const SkMatrix& matrix) override;
32 
33   void Paint(PaintContext& context) const override;
34 
35   // TODO(chinmaygarde): Once SCN-139 is addressed, introduce a new node in the
36   // session scene hierarchy.
37 
38  private:
39   int alpha_;
40   SkPoint offset_;
41 
42   // Restructure (if necessary) OpacityLayer to have only one child.
43   //
44   // This is needed to ensure that retained rendering can always be applied to
45   // save the costly saveLayer.
46   //
47   // If there are multiple children, this creates a new identity TransformLayer,
48   // sets all children to be the TransformLayer's children, and sets that
49   // TransformLayer as the single child of this OpacityLayer.
50   void EnsureSingleChild();
51 
52   FML_DISALLOW_COPY_AND_ASSIGN(OpacityLayer);
53 };
54 
55 }  // namespace flutter
56 
57 #endif  // FLUTTER_FLOW_LAYERS_OPACITY_LAYER_H_
58