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/clip_rect_layer.h"
6
7namespace flutter {
8
9ClipRectLayer::ClipRectLayer(const SkRect& clip_rect, Clip clip_behavior)
10 : clip_rect_(clip_rect), clip_behavior_(clip_behavior) {
11 FML_DCHECK(clip_behavior != Clip::none);
12}
13
14void ClipRectLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) {
15 TRACE_EVENT0("flutter", "ClipRectLayer::Preroll");
16
17 SkRect previous_cull_rect = context->cull_rect;
18 children_inside_clip_ = context->cull_rect.intersect(clip_rect_);
19 if (children_inside_clip_) {
20 TRACE_EVENT_INSTANT0("flutter", "children inside clip rect");
21
22 Layer::AutoPrerollSaveLayerState save =
23 Layer::AutoPrerollSaveLayerState::Create(context, UsesSaveLayer());
24 context->mutators_stack.PushClipRect(clip_rect_);
25 SkRect child_paint_bounds = SkRect::MakeEmpty();
26 PrerollChildren(context, matrix, &child_paint_bounds);
27
28 if (child_paint_bounds.intersect(clip_rect_)) {
29 set_paint_bounds(child_paint_bounds);
30 }
31 context->mutators_stack.Pop();
32 }
33 context->cull_rect = previous_cull_rect;
34}
35
36#if defined(LEGACY_FUCHSIA_EMBEDDER)
37
38void ClipRectLayer::UpdateScene(SceneUpdateContext& context) {
39 TRACE_EVENT0("flutter", "ClipRectLayer::UpdateScene");
40 FML_DCHECK(needs_system_composite());
41
42 // TODO(liyuqian): respect clip_behavior_
43 SceneUpdateContext::Clip clip(context, clip_rect_);
44 UpdateSceneChildren(context);
45}
46
47#endif
48
49void ClipRectLayer::Paint(PaintContext& context) const {
50 TRACE_EVENT0("flutter", "ClipRectLayer::Paint");
51 FML_DCHECK(needs_painting());
52
53 if (!children_inside_clip_) {
54 TRACE_EVENT_INSTANT0("flutter", "children not inside clip rect, skipping");
55 return;
56 }
57
58 SkAutoCanvasRestore save(context.internal_nodes_canvas, true);
59 context.internal_nodes_canvas->clipRect(clip_rect_,
60 clip_behavior_ != Clip::hardEdge);
61
62 if (UsesSaveLayer()) {
63 context.internal_nodes_canvas->saveLayer(clip_rect_, nullptr);
64 }
65 PaintChildren(context);
66 if (UsesSaveLayer()) {
67 context.internal_nodes_canvas->restore();
68 }
69}
70
71} // namespace flutter
72