summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/paint/compositing/graphics_layer_tree_builder.cc
blob: 311d4a87190f40755cd44eb149418a3c789efe64 (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
/*
 * Copyright (C) 2009, 2010 Apple Inc. All rights reserved.
 * Copyright (C) 2014 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

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

#include "third_party/blink/renderer/core/html/media/html_media_element.h"
#include "third_party/blink/renderer/core/html/media/html_video_element.h"
#include "third_party/blink/renderer/core/layout/layout_embedded_content.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_paint_order_iterator.h"

namespace blink {

GraphicsLayerTreeBuilder::GraphicsLayerTreeBuilder() = default;

static bool ShouldAppendLayer(const PaintLayer& layer) {
  auto* video_element =
      DynamicTo<HTMLVideoElement>(layer.GetLayoutObject().GetNode());
  if (video_element && video_element->IsFullscreen() &&
      video_element->UsesOverlayFullscreenVideo()) {
    return false;
  }
  return true;
}

void GraphicsLayerTreeBuilder::Rebuild(PaintLayer& layer,
                                       GraphicsLayerVector& child_layers) {
  PendingOverflowControlReparents ignored;
  RebuildRecursive(layer, child_layers, ignored);
}

using PendingPair = std::pair<const PaintLayer*, wtf_size_t>;

void GraphicsLayerTreeBuilder::RebuildRecursive(
    PaintLayer& layer,
    GraphicsLayerVector& child_layers,
    PendingOverflowControlReparents& pending_reparents) {
  // Make the layer compositing if necessary, and set up clipping and content
  // layers.  Note that we can only do work here that is independent of whether
  // the descendant layers have been processed. computeCompositingRequirements()
  // will already have done the paint invalidation if necessary.

  const bool has_composited_layer_mapping = layer.HasCompositedLayerMapping();
  CompositedLayerMapping* current_composited_layer_mapping =
      layer.GetCompositedLayerMapping();

  // If this layer has a compositedLayerMapping, then that is where we place
  // subsequent children GraphicsLayers.  Otherwise children continue to append
  // to the child list of the enclosing layer.
  GraphicsLayerVector this_layer_children;
  GraphicsLayerVector* layer_vector_for_children =
      has_composited_layer_mapping ? &this_layer_children : &child_layers;

  PendingOverflowControlReparents this_pending_reparents_children;
  PendingOverflowControlReparents* pending_reparents_for_children =
      has_composited_layer_mapping ? &this_pending_reparents_children
                                   : &pending_reparents;

#if DCHECK_IS_ON()
  PaintLayerListMutationDetector mutation_checker(layer);
#endif

  bool recursion_blocked_by_display_lock =
      layer.GetLayoutObject().PrePaintBlockedByDisplayLock(
          DisplayLockLifecycleTarget::kChildren);
  // If the recursion is blocked meaningfully (i.e. we would have recursed,
  // since the layer has children), then we should inform the display-lock
  // context that we blocked a graphics layer recursion, so that we can ensure
  // to rebuild the tree once we're unlocked.
  if (recursion_blocked_by_display_lock && layer.FirstChild()) {
    auto* context = layer.GetLayoutObject().GetDisplayLockContext();
    DCHECK(context);
    context->NotifyGraphicsLayerRebuildBlocked();
  }

  if (layer.IsStackingContextWithNegativeZOrderChildren()) {
    if (!recursion_blocked_by_display_lock) {
      PaintLayerPaintOrderIterator iterator(layer, kNegativeZOrderChildren);
      while (PaintLayer* child_layer = iterator.Next()) {
        RebuildRecursive(*child_layer, *layer_vector_for_children,
                         *pending_reparents_for_children);
      }
    }

    // If a negative z-order child is compositing, we get a foreground layer
    // which needs to get parented.
    if (has_composited_layer_mapping &&
        current_composited_layer_mapping->ForegroundLayer()) {
      layer_vector_for_children->push_back(
          current_composited_layer_mapping->ForegroundLayer());
    }
  }

  if (!recursion_blocked_by_display_lock) {
    PaintLayerPaintOrderIterator iterator(layer,
                                          kNormalFlowAndPositiveZOrderChildren);
    while (PaintLayer* child_layer = iterator.Next()) {
      RebuildRecursive(*child_layer, *layer_vector_for_children,
                       *pending_reparents_for_children);
    }
  }

  if (layer.GetLayoutObject().IsLayoutEmbeddedContent()) {
    DCHECK(this_layer_children.IsEmpty());
    PaintLayerCompositor* inner_compositor =
        PaintLayerCompositor::FrameContentsCompositor(
            ToLayoutEmbeddedContent(layer.GetLayoutObject()));
    if (inner_compositor) {
      // If the embedded frame is render-throttled, it might not be compositing
      // clean at this point. In that case, we still need to connect its
      // existing root graphics layer, so we need to query the stale compositing
      // state.
      DisableCompositingQueryAsserts disabler;
      if (inner_compositor->InCompositingMode()) {
        if (GraphicsLayer* inner_root_layer =
                inner_compositor->RootGraphicsLayer()) {
          layer_vector_for_children->push_back(inner_root_layer);
        }
      }
      inner_compositor->ClearRootLayerAttachmentDirty();
    }
  }

  if (has_composited_layer_mapping) {
    // TODO(szager): Remove after diagnosing crash crbug.com/1092673
    CHECK(current_composited_layer_mapping);

    // Apply all pending reparents by inserting the overflow controls
    // root layers into |this_layer_children|. To do this, first sort
    // them by index. Then insert them one-by-one into the array,
    // incrementing |offset| by one each time to account for previous
    // insertions.
    wtf_size_t offset = 0;
    Vector<PendingPair> pending;
    for (auto& item : *pending_reparents_for_children)
      pending.push_back(std::make_pair(item.key, item.value));
    pending_reparents_for_children->clear();

    std::sort(pending.begin(), pending.end(),
              [](const PendingPair& a, const PendingPair& b) {
                return a.second < b.second;
              });
    for (auto& item : pending) {
      if (auto* layer = item.first->GetCompositedLayerMapping()
                            ->DetachLayerForOverflowControls()) {
        this_layer_children.insert(item.second + offset, layer);
        offset++;
      }
    }

    if (!this_layer_children.IsEmpty())
      current_composited_layer_mapping->SetSublayers(this_layer_children);

    if (ShouldAppendLayer(layer)) {
      child_layers.push_back(
          current_composited_layer_mapping->MainGraphicsLayer());
    }
  }

  // Also insert for self, to handle the case of scrollers with negative
  // z-index children (the scrolbars should still paint on top of the
  // scroller itself).
  if (layer.GetLayoutObject().IsStacked() && has_composited_layer_mapping &&
      layer.GetCompositedLayerMapping()->NeedsToReparentOverflowControls())
    pending_reparents.Set(&layer, child_layers.size());

  // Set or overwrite the entry in |pending_reparents| for this scroller.
  // Overlay scrollbars need to paint on top of all content under the scroller,
  // so keep overwriting if we find a PaintLayer that is later in paint order.
  const PaintLayer* scroll_parent = layer.ScrollParent();
  if (layer.GetLayoutObject().IsStacked() && scroll_parent &&
      scroll_parent->HasCompositedLayerMapping() &&
      scroll_parent->GetCompositedLayerMapping()
          ->NeedsToReparentOverflowControls())
    pending_reparents.Set(layer.ScrollParent(), child_layers.size());
}

}  // namespace blink