• 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 #include "flutter/flow/layers/opacity_layer.h"
6 
7 #include "flutter/flow/layers/transform_layer.h"
8 
9 namespace flutter {
10 
OpacityLayer(int alpha,const SkPoint & offset)11 OpacityLayer::OpacityLayer(int alpha, const SkPoint& offset)
12     : alpha_(alpha), offset_(offset) {}
13 
14 OpacityLayer::~OpacityLayer() = default;
15 
EnsureSingleChild()16 void OpacityLayer::EnsureSingleChild() {
17   FML_DCHECK(layers().size() > 0);  // OpacityLayer should never be a leaf
18 
19   if (layers().size() == 1) {
20     return;
21   }
22 
23   // Be careful: SkMatrix's default constructor doesn't initialize the matrix to
24   // identity. Hence we have to explicitly call SkMatrix::setIdentity.
25   SkMatrix identity;
26   identity.setIdentity();
27   auto new_child = std::make_shared<flutter::TransformLayer>(identity);
28 
29   for (auto& child : layers()) {
30     new_child->Add(child);
31   }
32   ClearChildren();
33   Add(new_child);
34 }
35 
Preroll(PrerollContext * context,const SkMatrix & matrix)36 void OpacityLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) {
37   EnsureSingleChild();
38   SkMatrix child_matrix = matrix;
39   child_matrix.postTranslate(offset_.fX, offset_.fY);
40   context->mutators_stack.PushTransform(
41       SkMatrix::MakeTrans(offset_.fX, offset_.fY));
42   context->mutators_stack.PushOpacity(alpha_);
43   ContainerLayer::Preroll(context, child_matrix);
44   context->mutators_stack.Pop();
45   context->mutators_stack.Pop();
46   set_paint_bounds(paint_bounds().makeOffset(offset_.fX, offset_.fY));
47   // See |EnsureSingleChild|.
48   FML_DCHECK(layers().size() == 1);
49 
50 // Opacity Layer Cache will create SkSurface which can not be released.
51 #ifndef OHOS_PLATFORM
52   if (context->raster_cache &&
53       SkRect::Intersects(context->cull_rect, paint_bounds())) {
54     Layer* child = layers()[0].get();
55     SkMatrix ctm = child_matrix;
56 #ifndef SUPPORT_FRACTIONAL_TRANSLATION
57     ctm = RasterCache::GetIntegralTransCTM(ctm);
58 #endif
59     context->raster_cache->Prepare(context, child, ctm);
60   }
61 #endif
62 }
63 
Paint(PaintContext & context) const64 void OpacityLayer::Paint(PaintContext& context) const {
65   TRACE_EVENT0("flutter", "OpacityLayer::Paint");
66   FML_DCHECK(needs_painting());
67 
68   SkPaint paint;
69   paint.setAlpha(alpha_);
70 
71   SkAutoCanvasRestore save(context.internal_nodes_canvas, true);
72   context.internal_nodes_canvas->translate(offset_.fX, offset_.fY);
73 
74 #ifndef SUPPORT_FRACTIONAL_TRANSLATION
75   context.internal_nodes_canvas->setMatrix(RasterCache::GetIntegralTransCTM(
76       context.leaf_nodes_canvas->getTotalMatrix()));
77 #endif
78 
79   // See |EnsureSingleChild|.
80   FML_DCHECK(layers().size() == 1);
81 
82   // Embedded platform views are changing the canvas in the middle of the paint
83   // traversal. To make sure we paint on the right canvas, when the embedded
84   // platform views preview is enabled (context.view_embedded is not null) we
85   // don't use the cache.
86   if (context.view_embedder == nullptr && context.raster_cache) {
87     const SkMatrix& ctm = context.leaf_nodes_canvas->getTotalMatrix();
88     RasterCacheResult child_cache =
89         context.raster_cache->Get(layers()[0].get(), ctm);
90     if (child_cache.is_valid()) {
91       child_cache.draw(*context.leaf_nodes_canvas, &paint);
92       return;
93     }
94   }
95 
96   // Skia may clip the content with saveLayerBounds (although it's not a
97   // guaranteed clip). So we have to provide a big enough saveLayerBounds. To do
98   // so, we first remove the offset from paint bounds since it's already in the
99   // matrix. Then we round out the bounds because of our
100   // RasterCache::GetIntegralTransCTM optimization.
101   //
102   // Note that the following lines are only accessible when the raster cache is
103   // not available (e.g., when we're using the software backend in golden
104   // tests).
105   SkRect saveLayerBounds;
106   paint_bounds()
107       .makeOffset(-offset_.fX, -offset_.fY)
108       .roundOut(&saveLayerBounds);
109 
110   Layer::AutoSaveLayer save_layer =
111       Layer::AutoSaveLayer::Create(context, saveLayerBounds, &paint);
112   PaintChildren(context);
113 }
114 
115 }  // namespace flutter
116