Flutter Engine
clip_rect_layer.cc
Go to the documentation of this file.
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 #include "flutter/flow/paint_utils.h"
7 
8 namespace flutter {
9 
10 ClipRectLayer::ClipRectLayer(const SkRect& clip_rect, Clip clip_behavior)
11  : clip_rect_(clip_rect), clip_behavior_(clip_behavior) {
12  FML_DCHECK(clip_behavior != Clip::none);
13 }
14 
15 #ifdef FLUTTER_ENABLE_DIFF_CONTEXT
16 
17 void ClipRectLayer::Diff(DiffContext* context, const Layer* old_layer) {
18  DiffContext::AutoSubtreeRestore subtree(context);
19  auto* prev = static_cast<const ClipRectLayer*>(old_layer);
20  if (!context->IsSubtreeDirty()) {
21  FML_DCHECK(prev);
22  if (clip_behavior_ != prev->clip_behavior_ ||
23  clip_rect_ != prev->clip_rect_) {
24  context->MarkSubtreeDirty(context->GetOldLayerPaintRegion(old_layer));
25  }
26  }
27  if (context->PushCullRect(clip_rect_)) {
28  DiffChildren(context, prev);
29  }
30  context->SetLayerPaintRegion(this, context->CurrentSubtreeRegion());
31 }
32 
33 #endif // FLUTTER_ENABLE_DIFF_CONTEXT
34 
35 void ClipRectLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) {
36  TRACE_EVENT0("flutter", "ClipRectLayer::Preroll");
37 
38  SkRect previous_cull_rect = context->cull_rect;
39  if (!context->cull_rect.intersect(clip_rect_)) {
40  context->cull_rect.setEmpty();
41  }
44  context->mutators_stack.PushClipRect(clip_rect_);
45 
46  SkRect child_paint_bounds = SkRect::MakeEmpty();
47  PrerollChildren(context, matrix, &child_paint_bounds);
48  if (child_paint_bounds.intersect(clip_rect_)) {
49  set_paint_bounds(child_paint_bounds);
50  }
51 
52  context->mutators_stack.Pop();
53  context->cull_rect = previous_cull_rect;
54 }
55 
56 void ClipRectLayer::Paint(PaintContext& context) const {
57  TRACE_EVENT0("flutter", "ClipRectLayer::Paint");
58  FML_DCHECK(needs_painting(context));
59 
60  SkAutoCanvasRestore save(context.internal_nodes_canvas, true);
61  context.internal_nodes_canvas->clipRect(clip_rect_,
62  clip_behavior_ != Clip::hardEdge);
63 
64  if (UsesSaveLayer()) {
65  context.internal_nodes_canvas->saveLayer(clip_rect_, nullptr);
66  }
67  PaintChildren(context);
68  if (UsesSaveLayer()) {
69  context.internal_nodes_canvas->restore();
70  if (context.checkerboard_offscreen_layers) {
71  DrawCheckerboard(context.internal_nodes_canvas, clip_rect_);
72  }
73  }
74 }
75 
76 } // namespace flutter
const bool checkerboard_offscreen_layers
Definition: layer.h:152
#define TRACE_EVENT0(category_group, name)
Definition: trace_event.h:90
#define FML_DCHECK(condition)
Definition: logging.h:86
void Paint(PaintContext &context) const override
ClipRectLayer(const SkRect &clip_rect, Clip clip_behavior)
void PrerollChildren(PrerollContext *context, const SkMatrix &child_matrix, SkRect *child_paint_bounds)
bool needs_painting(PaintContext &context) const
Definition: layer.h:249
void PushClipRect(const SkRect &rect)
static AutoPrerollSaveLayerState Create(PrerollContext *preroll_context, bool save_layer_is_active=true, bool layer_itself_performs_readback=false)
Definition: layer.cc:44
void Preroll(PrerollContext *context, const SkMatrix &matrix) override
void set_paint_bounds(const SkRect &paint_bounds)
Definition: layer.h:240
Clip
Definition: layer.h:40
SkCanvas * internal_nodes_canvas
Definition: layer.h:144
MutatorsStack & mutators_stack
Definition: layer.h:46
bool UsesSaveLayer() const
void DrawCheckerboard(SkCanvas *canvas, SkColor c1, SkColor c2, int size)
Definition: paint_utils.cc:29
void PaintChildren(PaintContext &context) const