summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/paint/compositing/compositing_inputs_updater.cc
blob: 65440c163bdd6935bfea6fc3d9b16c3ed704c9fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/core/paint/compositing/compositing_inputs_updater.h"

#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/frame/local_frame_view.h"
#include "third_party/blink/renderer/core/layout/layout_block.h"
#include "third_party/blink/renderer/core/layout/layout_view.h"
#include "third_party/blink/renderer/core/paint/compositing/composited_layer_mapping.h"
#include "third_party/blink/renderer/core/paint/compositing/paint_layer_compositor.h"
#include "third_party/blink/renderer/core/paint/paint_layer.h"
#include "third_party/blink/renderer/core/paint/paint_layer_scrollable_area.h"
#include "third_party/blink/renderer/platform/instrumentation/tracing/trace_event.h"
#include "third_party/blink/renderer/platform/instrumentation/use_counter.h"

namespace blink {
static const LayoutBoxModelObject* ClippingContainerFromClipChainParent(
    const PaintLayer* clip_chain_parent) {
  return clip_chain_parent->GetLayoutObject().HasClipRelatedProperty()
             ? &clip_chain_parent->GetLayoutObject()
             : clip_chain_parent->ClippingContainer();
}

CompositingInputsUpdater::CompositingInputsUpdater(
    PaintLayer* root_layer,
    PaintLayer* compositing_inputs_root)
    : root_layer_(root_layer),
      compositing_inputs_root_(compositing_inputs_root) {}

CompositingInputsUpdater::~CompositingInputsUpdater() = default;

bool CompositingInputsUpdater::LayerOrDescendantShouldBeComposited(
    PaintLayer* layer) {
  PaintLayerCompositor* compositor =
      layer->GetLayoutObject().View()->Compositor();
  return layer->DescendantHasDirectOrScrollingCompositingReason() ||
         layer->NeedsCompositedScrolling() ||
         (compositor->CanBeComposited(layer) &&
          layer->DirectCompositingReasons());
}

void CompositingInputsUpdater::Update() {
  TRACE_EVENT0("blink", "CompositingInputsUpdater::update");

  AncestorInfo info;
  UpdateType update_type = kDoNotForceUpdate;
  PaintLayer* layer =
      compositing_inputs_root_ ? compositing_inputs_root_ : root_layer_;
  CompositingReasons initial_compositing_reasons =
      layer->DirectCompositingReasons();
  ApplyAncestorInfoToSelfAndAncestorsRecursively(layer, update_type, info);
  UpdateSelfAndDescendantsRecursively(layer, update_type, info);

  // The layer has changed from non-compositing to compositing
  if (initial_compositing_reasons == CompositingReason::kNone &&
      LayerOrDescendantShouldBeComposited(layer)) {
    // Update all parent layers
    PaintLayer* parent_layer = layer->Parent();
    while (parent_layer) {
      parent_layer->SetDescendantHasDirectOrScrollingCompositingReason(true);
      parent_layer = parent_layer->Parent();
    }
  }
}

void CompositingInputsUpdater::ApplyAncestorInfoToSelfAndAncestorsRecursively(
    PaintLayer* layer,
    UpdateType& update_type,
    AncestorInfo& info) {
  if (!layer)
    return;

  // We first recursively call ApplyAncestorInfoToSelfAndAncestorsRecursively()
  // to ensure that we start to compute the geometry_map_ and AncestorInfo from
  // the root layer (as we need to do a top-down tree walk to incrementally
  // update this information).
  ApplyAncestorInfoToSelfAndAncestorsRecursively(layer->Parent(), update_type,
                                                 info);
  geometry_map_.PushMappingsToAncestor(layer, layer->Parent());
  UpdateAncestorInfo(layer, update_type, info);
  if (layer != compositing_inputs_root_ &&
      (layer->IsRootLayer() || layer->GetLayoutObject().HasOverflowClip()))
    info.last_overflow_clip_layer = layer;
}

void CompositingInputsUpdater::UpdateSelfAndDescendantsRecursively(
    PaintLayer* layer,
    UpdateType update_type,
    AncestorInfo info) {
  LayoutBoxModelObject& layout_object = layer->GetLayoutObject();
  const ComputedStyle& style = layout_object.StyleRef();

  const PaintLayer* previous_overflow_layer = layer->AncestorOverflowLayer();
  layer->UpdateAncestorOverflowLayer(info.last_overflow_clip_layer);
  if (info.last_overflow_clip_layer && layer->NeedsCompositingInputsUpdate() &&
      style.HasStickyConstrainedPosition()) {
    if (info.last_overflow_clip_layer != previous_overflow_layer) {
      // Old ancestor scroller should no longer have these constraints.
      DCHECK(!previous_overflow_layer ||
             !previous_overflow_layer->GetScrollableArea() ||
             !previous_overflow_layer->GetScrollableArea()
                  ->GetStickyConstraintsMap()
                  .Contains(layer));

      // If our ancestor scroller has changed and the previous one was the
      // root layer, we are no longer viewport constrained.
      if (previous_overflow_layer && previous_overflow_layer->IsRootLayer()) {
        layout_object.View()->GetFrameView()->RemoveViewportConstrainedObject(
            layout_object);
      }
    }

    if (info.last_overflow_clip_layer->IsRootLayer()) {
      layout_object.View()->GetFrameView()->AddViewportConstrainedObject(
          layout_object);
    }
    layout_object.UpdateStickyPositionConstraints();

    // Sticky position constraints and ancestor overflow scroller affect
    // the sticky layer position, so we need to update it again here.
    // TODO(flackr): This should be refactored in the future to be clearer
    // (i.e. update layer position and ancestor inputs updates in the
    // same walk)
    layer->UpdateLayerPosition();
  }

  // geometry_map_ has been already updated in ApplyAncestorInfo() and
  // UpdateAncestorInfo has been already computed in ApplyAncestorInfo() for
  // layers from root_layer_ down to compositing_inputs_root_ both included.
  if (layer != root_layer_ && layer != compositing_inputs_root_) {
    geometry_map_.PushMappingsToAncestor(layer, layer->Parent());
    UpdateAncestorInfo(layer, update_type, info);
  }
  if (layer->IsRootLayer() || layout_object.HasOverflowClip())
    info.last_overflow_clip_layer = layer;

  PaintLayerCompositor* compositor =
      layer->GetLayoutObject().View()->Compositor();

  // The sequence of updates to compositing triggers goes like this:
  // 1. Apply all triggers from kComboAllDirectNonStyleDeterminedReasons for
  //    |layer|. This may depend on ancestor composited scrolling (i.e. step
  //    2 for an ancestor PaintLayer).
  // 2. Put |layer| in composited scrolling mode if needed.
  // 3. Reset DescendantHasDirectCompositingReason to false for |layer|.
  // 4. Recurse into child PaintLayers.
  // 5. Set DescendantHasDirectCompositingReason to true if it was for any
  //    child.
  // 6. If |layer| is the root, composite if
  //    DescendantHasDirectCompositingReason is true for |layer|.

  layer->SetPotentialCompositingReasonsFromNonStyle(
      CompositingReasonFinder::NonStyleDeterminedDirectReasons(*layer));

  if (layer->GetScrollableArea()) {
    layer->GetScrollableArea()->UpdateNeedsCompositedScrolling(
        compositor->CanBeComposited(layer) &&
        layer->DirectCompositingReasons());
  }

  // Note that prepaint may use the compositing information, so only skip
  // recursing it if we're skipping prepaint.
  bool recursion_blocked_by_display_lock =
      layer->GetLayoutObject().PrePaintBlockedByDisplayLock(
          DisplayLockLifecycleTarget::kChildren);

  bool should_recurse = (layer->ChildNeedsCompositingInputsUpdate() ||
                         update_type == kForceUpdate) &&
                        !recursion_blocked_by_display_lock;

  layer->SetDescendantHasDirectOrScrollingCompositingReason(false);
  bool descendant_has_direct_compositing_reason = false;
  for (PaintLayer* child = layer->FirstChild(); child;
       child = child->NextSibling()) {
    if (should_recurse)
      UpdateSelfAndDescendantsRecursively(child, update_type, info);
    descendant_has_direct_compositing_reason |=
        LayerOrDescendantShouldBeComposited(child);
  }
  layer->SetDescendantHasDirectOrScrollingCompositingReason(
      descendant_has_direct_compositing_reason);

  if (layer->IsRootLayer() && layer->ScrollsOverflow() &&
      layer->DescendantHasDirectOrScrollingCompositingReason() &&
      !layer->NeedsCompositedScrolling())
    layer->GetScrollableArea()->UpdateNeedsCompositedScrolling(true);

  // If display lock blocked this recursion, then keep the dirty bit around
  // since it is a breadcrumb that will allow us to recurse later when we unlock
  // the element.
  if (!recursion_blocked_by_display_lock)
    layer->ClearChildNeedsCompositingInputsUpdate();

  geometry_map_.PopMappingsToAncestor(layer->Parent());

  if (layer->SelfPaintingStatusChanged()) {
    layer->ClearSelfPaintingStatusChanged();
    // If the floating object becomes non-self-painting, so some ancestor should
    // paint it; if it becomes self-painting, it should paint itself and no
    // ancestor should paint it.
    if (layout_object.IsFloating()) {
      LayoutBlockFlow::UpdateAncestorShouldPaintFloatingObject(
          *layer->GetLayoutBox());
    }
  }

  compositor->ClearCompositingInputsRoot();
}

void CompositingInputsUpdater::UpdateAncestorInfo(PaintLayer* const layer,
                                                  UpdateType& update_type,
                                                  AncestorInfo& info) {
  LayoutBoxModelObject& layout_object = layer->GetLayoutObject();
  const ComputedStyle& style = layout_object.StyleRef();

  PaintLayer* enclosing_stacking_composited_layer =
      info.enclosing_stacking_composited_layer;
  PaintLayer* enclosing_squashing_composited_layer =
      info.enclosing_squashing_composited_layer;
  switch (layer->GetCompositingState()) {
    case kNotComposited:
      break;
    case kPaintsIntoOwnBacking:
      if (style.IsStackingContext())
        enclosing_stacking_composited_layer = layer;
      break;
    case kPaintsIntoGroupedBacking:
      enclosing_squashing_composited_layer =
          &layer->GroupedMapping()->OwningLayer();
      break;
  }

  if (layer->NeedsCompositingInputsUpdate()) {
    if (enclosing_stacking_composited_layer) {
      enclosing_stacking_composited_layer->GetCompositedLayerMapping()
          ->SetNeedsGraphicsLayerUpdate(kGraphicsLayerUpdateSubtree);
    }
    if (enclosing_squashing_composited_layer) {
      enclosing_squashing_composited_layer->GetCompositedLayerMapping()
          ->SetNeedsGraphicsLayerUpdate(kGraphicsLayerUpdateSubtree);
    }

    update_type = kForceUpdate;
  }

  if (style.GetPosition() == EPosition::kAbsolute) {
    info.escape_clip_to = info.escape_clip_to_for_absolute;
    info.scrolling_ancestor = info.scrolling_ancestor_for_absolute;
    info.needs_reparent_scroll = info.needs_reparent_scroll_for_absolute;
  } else if (style.GetPosition() == EPosition::kFixed) {
    info.escape_clip_to = info.escape_clip_to_for_fixed;
    info.scrolling_ancestor = info.scrolling_ancestor_for_fixed;
    info.needs_reparent_scroll = info.needs_reparent_scroll_for_fixed;
  }

  if (layout_object.ShouldApplyLayoutContainment())
    info.nearest_contained_layout_layer = layer;

  if (update_type == kForceUpdate)
    UpdateAncestorDependentCompositingInputs(layer, info);

  info.enclosing_stacking_composited_layer =
      enclosing_stacking_composited_layer;
  info.enclosing_squashing_composited_layer =
      enclosing_squashing_composited_layer;

  // Handles sibling scroll problem, i.e. a non-stacking context scroller
  // needs to propagate scroll to its descendants that are siblings in
  // paint order. For example:
  // <div style="overflow:scroll;">
  //   <div style="position:relative;">Paint sibling.</div>
  // </div>
  if (layer->ScrollsOverflow()) {
    info.scrolling_ancestor = layer;
    info.needs_reparent_scroll = true;
  }
  if (layout_object.CanContainAbsolutePositionObjects()) {
    info.clip_chain_parent_for_absolute = layer;
    info.escape_clip_to_for_absolute = info.escape_clip_to;
    info.scrolling_ancestor_for_absolute = info.scrolling_ancestor;
    info.needs_reparent_scroll_for_absolute = info.needs_reparent_scroll;
  }

  // LayoutView isn't really the containing block for fixed-pos descendants
  // in the sense that they don't scroll along with its in-flow contents.
  // However LayoutView does clip them.
  if (layout_object.CanContainFixedPositionObjects() &&
      !layout_object.IsLayoutView()) {
    info.clip_chain_parent_for_fixed = layer;
    info.escape_clip_to_for_fixed = info.escape_clip_to;
    info.scrolling_ancestor_for_fixed = info.scrolling_ancestor;
    info.needs_reparent_scroll_for_fixed = info.needs_reparent_scroll;
  }
  if (layout_object.IsLayoutView())
    info.clip_chain_parent_for_fixed = layer;

  // CSS clip affects all descendants, not just containing-block descendants.
  // We don't have to set clip_chain_parent_for_absolute here because CSS clip
  // requires position:absolute, so the element must contain absolute-positioned
  // descendants.
  // However it is incorrect to let fixed-positioned descendants to inherit the
  // clip state from this element either, because the overflow clip and the
  // inherited clip of the current element shouldn't apply to them if the
  // current element is not a fixed-pos container. This is a known bug but too
  // difficult to fix in SPv1 compositing.
  if (layout_object.HasClip())
    info.clip_chain_parent_for_fixed = layer;

  if (style.IsStackingContext()) {
    info.escape_clip_to = nullptr;
    const LayoutBoxModelObject* clipping_container =
        ClippingContainerFromClipChainParent(layer);
    info.escape_clip_to_for_absolute =
        ClippingContainerFromClipChainParent(
            info.clip_chain_parent_for_absolute) != clipping_container
            ? info.clip_chain_parent_for_absolute
            : nullptr;
    info.escape_clip_to_for_fixed =
        ClippingContainerFromClipChainParent(
            info.clip_chain_parent_for_fixed) != clipping_container
            ? info.clip_chain_parent_for_fixed
            : nullptr;
    // Workaround crbug.com/817175
    // We can't escape clip to a layer that paints after us, because in SPv1
    // cc needs to reverse engineer clip tree from the layer tree, and we
    // can't refer to a clip node that hasn't been built yet.
    // This will result in wrong clip in some rare cases, for example:
    // <div style="display:grid;">
    //   <div style="z-index:-1; overflow:hidden;">
    //     <div style="position:absolute;"></div>
    //   </div>
    // </div>
    if (info.escape_clip_to_for_absolute && style.ZIndex() < 0 &&
        !info.escape_clip_to_for_absolute->GetLayoutObject()
             .StyleRef()
             .IsStackingContext())
      info.escape_clip_to_for_absolute = nullptr;
    if (info.escape_clip_to_for_fixed && style.ZIndex() < 0 &&
        !info.escape_clip_to_for_fixed->GetLayoutObject()
             .StyleRef()
             .IsStackingContext())
      info.escape_clip_to_for_fixed = nullptr;

    info.needs_reparent_scroll = info.needs_reparent_scroll_for_absolute =
        info.needs_reparent_scroll_for_fixed = false;
  }

  if (layout_object.IsStickyPositioned())
    info.is_under_position_sticky = true;
}

void CompositingInputsUpdater::UpdateAncestorDependentCompositingInputs(
    PaintLayer* layer,
    const AncestorInfo& info) {
  if (layer->IsRootLayer()) {
    layer->UpdateAncestorDependentCompositingInputs(
        PaintLayer::AncestorDependentCompositingInputs());
    return;
  }

  PaintLayer::AncestorDependentCompositingInputs properties;
  LayoutBoxModelObject& layout_object = layer->GetLayoutObject();

  // The final value for |unclipped_absolute_bounding_box| needs to be
  // in absolute, unscrolled space, without any scroll applied.
  properties.unclipped_absolute_bounding_box =
      EnclosingIntRect(geometry_map_.AbsoluteRect(
          layer->BoundingBoxForCompositingOverlapTest()));

  bool affected_by_scroll = root_layer_->GetScrollableArea() &&
                            layer->IsAffectedByScrollOf(root_layer_);

  // At ths point, |unclipped_absolute_bounding_box| is in viewport space.
  // To convert to absolute space, add scroll offset for non-fixed layers.
  if (affected_by_scroll) {
    properties.unclipped_absolute_bounding_box.Move(
        RoundedIntSize(root_layer_->GetScrollableArea()->GetScrollOffset()));
  }

  // For sticky-positioned elements, the scroll offset is sometimes included and
  // sometimes not, depending on whether the sticky element is affixed or still
  // scrolling. This makes caching difficult, as compared to Fixed position
  // elements which have consistent behavior. So we disable caching for
  // sticky-positioned subtrees.
  ClipRectsCacheSlot cache_slot = info.is_under_position_sticky
                                      ? kUncachedClipRects
                                      : kAbsoluteClipRectsIgnoringViewportClip;

  ClipRect clip_rect;
  layer->Clipper(PaintLayer::GeometryMapperOption::kDoNotUseGeometryMapper)
      .CalculateBackgroundClipRect(
          ClipRectsContext(root_layer_,
                           &root_layer_->GetLayoutObject().FirstFragment(),
                           cache_slot, kIgnorePlatformOverlayScrollbarSize,
                           kIgnoreOverflowClipAndScroll),
          clip_rect);
  IntRect snapped_clip_rect = PixelSnappedIntRect(clip_rect.Rect());
  // |snapped_clip_rect| is in absolute space space, but with scroll applied.
  // To convert to absolute, unscrolled space, subtract scroll offsets for
  // fixed layers.
  if (root_layer_->GetScrollableArea() && !affected_by_scroll) {
    snapped_clip_rect.Move(
        RoundedIntSize(-root_layer_->GetScrollableArea()->GetScrollOffset()));
  }

  properties.clipped_absolute_bounding_box =
      properties.unclipped_absolute_bounding_box;
  properties.clipped_absolute_bounding_box.Intersect(snapped_clip_rect);

  const PaintLayer* parent = layer->Parent();
  properties.opacity_ancestor =
      parent->IsTransparent() ? parent : parent->OpacityAncestor();
  properties.transform_ancestor =
      parent->Transform() ? parent : parent->TransformAncestor();
  properties.filter_ancestor =
      parent->HasFilterInducingProperty() ? parent : parent->FilterAncestor();
  properties.clip_path_ancestor = parent->GetLayoutObject().HasClipPath()
                                      ? parent
                                      : parent->ClipPathAncestor();
  properties.mask_ancestor =
      parent->GetLayoutObject().HasMask() ? parent : parent->MaskAncestor();

  EPosition position = layout_object.StyleRef().GetPosition();
  properties.nearest_fixed_position_layer =
      position == EPosition::kFixed ? layer
                                    : parent->NearestFixedPositionLayer();

  PaintLayer* clip_chain_parent = layer->Parent();
  if (position == EPosition::kAbsolute)
    clip_chain_parent = info.clip_chain_parent_for_absolute;
  else if (position == EPosition::kFixed)
    clip_chain_parent = info.clip_chain_parent_for_fixed;
  properties.clipping_container =
      ClippingContainerFromClipChainParent(clip_chain_parent);
  properties.clip_parent = info.escape_clip_to;

  properties.ancestor_scrolling_layer = info.scrolling_ancestor;
  if (info.needs_reparent_scroll && layout_object.StyleRef().IsStacked())
    properties.scroll_parent = info.scrolling_ancestor;

  properties.is_under_position_sticky = info.is_under_position_sticky;
  properties.nearest_contained_layout_layer =
      info.nearest_contained_layout_layer;

  layer->UpdateAncestorDependentCompositingInputs(properties);
}

#if DCHECK_IS_ON()

void CompositingInputsUpdater::AssertNeedsCompositingInputsUpdateBitsCleared(
    PaintLayer* layer) {
  bool recursion_blocked_by_display_lock =
      layer->GetLayoutObject().PrePaintBlockedByDisplayLock(
          DisplayLockLifecycleTarget::kChildren);

  DCHECK(recursion_blocked_by_display_lock ||
         !layer->ChildNeedsCompositingInputsUpdate());
  DCHECK(!layer->NeedsCompositingInputsUpdate());

  if (recursion_blocked_by_display_lock)
    return;

  for (PaintLayer* child = layer->FirstChild(); child;
       child = child->NextSibling())
    AssertNeedsCompositingInputsUpdateBitsCleared(child);
}

#endif

}  // namespace blink