RenderObject::computeRectForRepaint/computeFloatRectForRepaint should return the...
[WebKit-https.git] / Source / WebCore / rendering / RenderView.cpp
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #include "config.h"
22 #include "RenderView.h"
23
24 #include "Document.h"
25 #include "Element.h"
26 #include "FloatQuad.h"
27 #include "FloatingObjects.h"
28 #include "FlowThreadController.h"
29 #include "Frame.h"
30 #include "FrameSelection.h"
31 #include "FrameView.h"
32 #include "GraphicsContext.h"
33 #include "HTMLFrameOwnerElement.h"
34 #include "HTMLIFrameElement.h"
35 #include "HitTestResult.h"
36 #include "ImageQualityController.h"
37 #include "NodeTraversal.h"
38 #include "Page.h"
39 #include "RenderGeometryMap.h"
40 #include "RenderIterator.h"
41 #include "RenderLayer.h"
42 #include "RenderLayerBacking.h"
43 #include "RenderLayerCompositor.h"
44 #include "RenderMultiColumnFlowThread.h"
45 #include "RenderMultiColumnSet.h"
46 #include "RenderMultiColumnSpannerPlaceholder.h"
47 #include "RenderNamedFlowThread.h"
48 #include "RenderSelectionInfo.h"
49 #include "RenderWidget.h"
50 #include "Settings.h"
51 #include "StyleInheritedData.h"
52 #include "TransformState.h"
53 #include <wtf/StackStats.h>
54
55 namespace WebCore {
56
57 struct FrameFlatteningLayoutDisallower {
58     FrameFlatteningLayoutDisallower(FrameView& frameView)
59         : m_frameView(frameView)
60         , m_disallowLayout(frameView.frame().settings().frameFlatteningEnabled())
61     {
62         if (m_disallowLayout)
63             m_frameView.startDisallowingLayout();
64     }
65
66     ~FrameFlatteningLayoutDisallower()
67     {
68         if (m_disallowLayout)
69             m_frameView.endDisallowingLayout();
70     }
71
72 private:
73     FrameView& m_frameView;
74     bool m_disallowLayout { false };
75 };
76
77 struct SelectionIterator {
78     SelectionIterator(RenderObject* start)
79         : m_current(start)
80     {
81         checkForSpanner();
82     }
83     
84     RenderObject* current() const
85     {
86         return m_current;
87     }
88     
89     RenderObject* next()
90     {
91         RenderObject* currentSpan = m_spannerStack.isEmpty() ? nullptr : m_spannerStack.last()->spanner();
92         m_current = m_current->nextInPreOrder(currentSpan);
93         checkForSpanner();
94         if (!m_current && currentSpan) {
95             RenderObject* placeholder = m_spannerStack.last();
96             m_spannerStack.removeLast();
97             m_current = placeholder->nextInPreOrder();
98             checkForSpanner();
99         }
100         return m_current;
101     }
102
103 private:
104     void checkForSpanner()
105     {
106         if (!is<RenderMultiColumnSpannerPlaceholder>(m_current))
107             return;
108         auto& placeholder = downcast<RenderMultiColumnSpannerPlaceholder>(*m_current);
109         m_spannerStack.append(&placeholder);
110         m_current = placeholder.spanner();
111     }
112
113     RenderObject* m_current { nullptr };
114     Vector<RenderMultiColumnSpannerPlaceholder*> m_spannerStack;
115 };
116
117 RenderView::RenderView(Document& document, Ref<RenderStyle>&& style)
118     : RenderBlockFlow(document, WTF::move(style))
119     , m_frameView(*document.view())
120     , m_selectionUnsplitStart(nullptr)
121     , m_selectionUnsplitEnd(nullptr)
122     , m_selectionUnsplitStartPos(-1)
123     , m_selectionUnsplitEndPos(-1)
124     , m_lazyRepaintTimer(*this, &RenderView::lazyRepaintTimerFired)
125     , m_pageLogicalHeight(0)
126     , m_pageLogicalHeightChanged(false)
127     , m_layoutState(nullptr)
128     , m_layoutStateDisableCount(0)
129     , m_renderQuoteHead(nullptr)
130     , m_renderCounterCount(0)
131     , m_selectionWasCaret(false)
132     , m_hasSoftwareFilters(false)
133 #if ENABLE(SERVICE_CONTROLS)
134     , m_selectionRectGatherer(*this)
135 #endif
136 {
137     setIsRenderView();
138
139     // FIXME: We should find a way to enforce this at compile time.
140     ASSERT(document.view());
141
142     // init RenderObject attributes
143     setInline(false);
144     
145     m_minPreferredLogicalWidth = 0;
146     m_maxPreferredLogicalWidth = 0;
147
148     setPreferredLogicalWidthsDirty(true, MarkOnlyThis);
149     
150     setPositionState(AbsolutePosition); // to 0,0 :)
151 }
152
153 RenderView::~RenderView()
154 {
155 }
156
157 void RenderView::scheduleLazyRepaint(RenderBox& renderer)
158 {
159     if (renderer.renderBoxNeedsLazyRepaint())
160         return;
161     renderer.setRenderBoxNeedsLazyRepaint(true);
162     m_renderersNeedingLazyRepaint.add(&renderer);
163     if (!m_lazyRepaintTimer.isActive())
164         m_lazyRepaintTimer.startOneShot(0);
165 }
166
167 void RenderView::unscheduleLazyRepaint(RenderBox& renderer)
168 {
169     if (!renderer.renderBoxNeedsLazyRepaint())
170         return;
171     renderer.setRenderBoxNeedsLazyRepaint(false);
172     m_renderersNeedingLazyRepaint.remove(&renderer);
173     if (m_renderersNeedingLazyRepaint.isEmpty())
174         m_lazyRepaintTimer.stop();
175 }
176
177 void RenderView::lazyRepaintTimerFired()
178 {
179     bool shouldRepaint = !document().inPageCache();
180
181     for (auto& renderer : m_renderersNeedingLazyRepaint) {
182         if (shouldRepaint)
183             renderer->repaint();
184         renderer->setRenderBoxNeedsLazyRepaint(false);
185     }
186     m_renderersNeedingLazyRepaint.clear();
187 }
188
189 bool RenderView::hitTest(const HitTestRequest& request, HitTestResult& result)
190 {
191     return hitTest(request, result.hitTestLocation(), result);
192 }
193
194 bool RenderView::hitTest(const HitTestRequest& request, const HitTestLocation& location, HitTestResult& result)
195 {
196     document().updateLayout();
197
198     FrameFlatteningLayoutDisallower disallower(frameView());
199
200     if (layer()->hitTest(request, location, result))
201         return true;
202
203     // FIXME: Consider if this test should be done unconditionally.
204     if (request.allowsFrameScrollbars()) {
205         // ScrollView scrollbars are not the same as RenderLayer scrollbars tested by RenderLayer::hitTestOverflowControls,
206         // so we need to test ScrollView scrollbars separately here.
207         Scrollbar* frameScrollbar = frameView().scrollbarAtPoint(location.roundedPoint());
208         if (frameScrollbar) {
209             result.setScrollbar(frameScrollbar);
210             return true;
211         }
212     }
213
214     return false;
215 }
216
217 void RenderView::computeLogicalHeight(LayoutUnit logicalHeight, LayoutUnit, LogicalExtentComputedValues& computedValues) const
218 {
219     computedValues.m_extent = !shouldUsePrintingLayout() ? LayoutUnit(viewLogicalHeight()) : logicalHeight;
220 }
221
222 void RenderView::updateLogicalWidth()
223 {
224     if (!shouldUsePrintingLayout())
225         setLogicalWidth(viewLogicalWidth());
226 }
227
228 LayoutUnit RenderView::availableLogicalHeight(AvailableLogicalHeightType) const
229 {
230     // Make sure block progression pagination for percentages uses the column extent and
231     // not the view's extent. See https://bugs.webkit.org/show_bug.cgi?id=135204.
232     if (multiColumnFlowThread() && multiColumnFlowThread()->firstMultiColumnSet())
233         return multiColumnFlowThread()->firstMultiColumnSet()->computedColumnHeight();
234
235 #if PLATFORM(IOS)
236     // Workaround for <rdar://problem/7166808>.
237     if (document().isPluginDocument() && frameView().useFixedLayout())
238         return frameView().fixedLayoutSize().height();
239 #endif
240     return isHorizontalWritingMode() ? frameView().visibleHeight() : frameView().visibleWidth();
241 }
242
243 bool RenderView::isChildAllowed(const RenderObject& child, const RenderStyle&) const
244 {
245     return child.isBox();
246 }
247
248 void RenderView::layoutContent(const LayoutState& state)
249 {
250     UNUSED_PARAM(state);
251     ASSERT(needsLayout());
252
253     RenderBlockFlow::layout();
254     if (hasRenderNamedFlowThreads())
255         flowThreadController().layoutRenderNamedFlowThreads();
256 #ifndef NDEBUG
257     checkLayoutState(state);
258 #endif
259 }
260
261 #ifndef NDEBUG
262 void RenderView::checkLayoutState(const LayoutState& state)
263 {
264     ASSERT(layoutDeltaMatches(LayoutSize()));
265     ASSERT(!m_layoutStateDisableCount);
266     ASSERT(m_layoutState.get() == &state);
267 }
268 #endif
269
270 void RenderView::initializeLayoutState(LayoutState& state)
271 {
272     // FIXME: May be better to push a clip and avoid issuing offscreen repaints.
273     state.m_clipped = false;
274
275     state.m_pageLogicalHeight = m_pageLogicalHeight;
276     state.m_pageLogicalHeightChanged = m_pageLogicalHeightChanged;
277     state.m_isPaginated = state.m_pageLogicalHeight;
278 }
279
280 // The algorithm below assumes this is a full layout. In case there are previously computed values for regions, supplemental steps are taken
281 // to ensure the results are the same as those obtained from a full layout (i.e. the auto-height regions from all the flows are marked as needing
282 // layout).
283 // 1. The flows are laid out from the outer flow to the inner flow. This successfully computes the outer non-auto-height regions size so the 
284 // inner flows have the necessary information to correctly fragment the content.
285 // 2. The flows are laid out from the inner flow to the outer flow. After an inner flow is laid out it goes into the constrained layout phase
286 // and marks the auto-height regions they need layout. This means the outer flows will relayout if they depend on regions with auto-height regions
287 // belonging to inner flows. This step will correctly set the computedAutoHeight for the auto-height regions. It's possible for non-auto-height
288 // regions to relayout if they depend on auto-height regions. This will invalidate the inner flow threads and mark them as needing layout.
289 // 3. The last step is to do one last layout if there are pathological dependencies between non-auto-height regions and auto-height regions
290 // as detected in the previous step.
291 void RenderView::layoutContentInAutoLogicalHeightRegions(const LayoutState& state)
292 {
293     // We need to invalidate all the flows with auto-height regions if one such flow needs layout.
294     // If none is found we do a layout a check back again afterwards.
295     if (!flowThreadController().updateFlowThreadsNeedingLayout()) {
296         // Do a first layout of the content. In some cases more layouts are not needed (e.g. only flows with non-auto-height regions have changed).
297         layoutContent(state);
298
299         // If we find no named flow needing a two step layout after the first layout, exit early.
300         // Otherwise, initiate the two step layout algorithm and recompute all the flows.
301         if (!flowThreadController().updateFlowThreadsNeedingTwoStepLayout())
302             return;
303     }
304
305     // Layout to recompute all the named flows with auto-height regions.
306     layoutContent(state);
307
308     // Propagate the computed auto-height values upwards.
309     // Non-auto-height regions may invalidate the flow thread because they depended on auto-height regions, but that's ok.
310     flowThreadController().updateFlowThreadsIntoConstrainedPhase();
311
312     // Do one last layout that should update the auto-height regions found in the main flow
313     // and solve pathological dependencies between regions (e.g. a non-auto-height region depending
314     // on an auto-height one).
315     if (needsLayout())
316         layoutContent(state);
317 }
318
319 void RenderView::layoutContentToComputeOverflowInRegions(const LayoutState& state)
320 {
321     if (!hasRenderNamedFlowThreads())
322         return;
323
324     // First pass through the flow threads and mark the regions as needing a simple layout.
325     // The regions extract the overflow from the flow thread and pass it to their containg
326     // block chain.
327     flowThreadController().updateFlowThreadsIntoOverflowPhase();
328     if (needsLayout())
329         layoutContent(state);
330
331     // In case scrollbars resized the regions a new pass is necessary to update the flow threads
332     // and recompute the overflow on regions. This is the final state of the flow threads.
333     flowThreadController().updateFlowThreadsIntoFinalPhase();
334     if (needsLayout())
335         layoutContent(state);
336
337     // Finally reset the layout state of the flow threads.
338     flowThreadController().updateFlowThreadsIntoMeasureContentPhase();
339 }
340
341 void RenderView::layout()
342 {
343     StackStats::LayoutCheckPoint layoutCheckPoint;
344     if (!document().paginated())
345         setPageLogicalHeight(0);
346
347     if (shouldUsePrintingLayout())
348         m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = logicalWidth();
349
350     // Use calcWidth/Height to get the new width/height, since this will take the full page zoom factor into account.
351     bool relayoutChildren = !shouldUsePrintingLayout() && (width() != viewWidth() || height() != viewHeight());
352     if (relayoutChildren) {
353         setChildNeedsLayout(MarkOnlyThis);
354
355         for (auto& box : childrenOfType<RenderBox>(*this)) {
356             if (box.hasRelativeLogicalHeight()
357                 || box.style().logicalHeight().isPercentOrCalculated()
358                 || box.style().logicalMinHeight().isPercentOrCalculated()
359                 || box.style().logicalMaxHeight().isPercentOrCalculated()
360                 || box.isSVGRoot()
361                 )
362                 box.setChildNeedsLayout(MarkOnlyThis);
363         }
364     }
365
366     ASSERT(!m_layoutState);
367     if (!needsLayout())
368         return;
369
370     m_layoutState = std::make_unique<LayoutState>();
371     initializeLayoutState(*m_layoutState);
372
373     m_pageLogicalHeightChanged = false;
374
375     if (checkTwoPassLayoutForAutoHeightRegions())
376         layoutContentInAutoLogicalHeightRegions(*m_layoutState);
377     else
378         layoutContent(*m_layoutState);
379
380     layoutContentToComputeOverflowInRegions(*m_layoutState);
381
382 #ifndef NDEBUG
383     checkLayoutState(*m_layoutState);
384 #endif
385     m_layoutState = nullptr;
386     clearNeedsLayout();
387 }
388
389 LayoutUnit RenderView::pageOrViewLogicalHeight() const
390 {
391     if (document().printing())
392         return pageLogicalHeight();
393     
394     if (multiColumnFlowThread() && !style().hasInlineColumnAxis()) {
395         if (int pageLength = frameView().pagination().pageLength)
396             return pageLength;
397     }
398
399     return viewLogicalHeight();
400 }
401
402 LayoutUnit RenderView::clientLogicalWidthForFixedPosition() const
403 {
404     // FIXME: If the FrameView's fixedVisibleContentRect() is not empty, perhaps it should be consulted here too?
405     if (frameView().fixedElementsLayoutRelativeToFrame())
406         return (isHorizontalWritingMode() ? frameView().visibleWidth() : frameView().visibleHeight()) / frameView().frame().frameScaleFactor();
407
408 #if PLATFORM(IOS)
409     if (frameView().useCustomFixedPositionLayoutRect())
410         return isHorizontalWritingMode() ? frameView().customFixedPositionLayoutRect().width() : frameView().customFixedPositionLayoutRect().height();
411 #endif
412
413     return clientLogicalWidth();
414 }
415
416 LayoutUnit RenderView::clientLogicalHeightForFixedPosition() const
417 {
418     // FIXME: If the FrameView's fixedVisibleContentRect() is not empty, perhaps it should be consulted here too?
419     if (frameView().fixedElementsLayoutRelativeToFrame())
420         return (isHorizontalWritingMode() ? frameView().visibleHeight() : frameView().visibleWidth()) / frameView().frame().frameScaleFactor();
421
422 #if PLATFORM(IOS)
423     if (frameView().useCustomFixedPositionLayoutRect())
424         return isHorizontalWritingMode() ? frameView().customFixedPositionLayoutRect().height() : frameView().customFixedPositionLayoutRect().width();
425 #endif
426
427     return clientLogicalHeight();
428 }
429
430 #if PLATFORM(IOS)
431 static inline LayoutSize fixedPositionOffset(const FrameView& frameView)
432 {
433     return frameView.useCustomFixedPositionLayoutRect() ? (frameView.customFixedPositionLayoutRect().location() - LayoutPoint()) : frameView.scrollOffset();
434 }
435 #endif
436
437 void RenderView::mapLocalToContainer(const RenderLayerModelObject* repaintContainer, TransformState& transformState, MapCoordinatesFlags mode, bool* wasFixed) const
438 {
439     // If a container was specified, and was not nullptr or the RenderView,
440     // then we should have found it by now.
441     ASSERT_ARG(repaintContainer, !repaintContainer || repaintContainer == this);
442     ASSERT_UNUSED(wasFixed, !wasFixed || *wasFixed == (mode & IsFixed));
443
444     if (!repaintContainer && mode & UseTransforms && shouldUseTransformFromContainer(nullptr)) {
445         TransformationMatrix t;
446         getTransformFromContainer(nullptr, LayoutSize(), t);
447         transformState.applyTransform(t);
448     }
449     
450     if (mode & IsFixed)
451 #if PLATFORM(IOS)
452         transformState.move(fixedPositionOffset(m_frameView));
453 #else
454         transformState.move(frameView().scrollOffsetForFixedPosition());
455 #endif
456 }
457
458 const RenderObject* RenderView::pushMappingToContainer(const RenderLayerModelObject* ancestorToStopAt, RenderGeometryMap& geometryMap) const
459 {
460     // If a container was specified, and was not nullptr or the RenderView,
461     // then we should have found it by now.
462     ASSERT_ARG(ancestorToStopAt, !ancestorToStopAt || ancestorToStopAt == this);
463
464 #if PLATFORM(IOS)
465     LayoutSize scrollOffset = fixedPositionOffset(frameView());
466 #else
467     LayoutSize scrollOffset = frameView().scrollOffsetForFixedPosition();
468 #endif
469
470     if (!ancestorToStopAt && shouldUseTransformFromContainer(nullptr)) {
471         TransformationMatrix t;
472         getTransformFromContainer(nullptr, LayoutSize(), t);
473         geometryMap.pushView(this, scrollOffset, &t);
474     } else
475         geometryMap.pushView(this, scrollOffset);
476
477     return nullptr;
478 }
479
480 void RenderView::mapAbsoluteToLocalPoint(MapCoordinatesFlags mode, TransformState& transformState) const
481 {
482     if (mode & IsFixed)
483 #if PLATFORM(IOS)
484         transformState.move(fixedPositionOffset(frameView()));
485 #else
486         transformState.move(frameView().scrollOffsetForFixedPosition());
487 #endif
488
489     if (mode & UseTransforms && shouldUseTransformFromContainer(nullptr)) {
490         TransformationMatrix t;
491         getTransformFromContainer(nullptr, LayoutSize(), t);
492         transformState.applyTransform(t);
493     }
494 }
495
496 bool RenderView::requiresColumns(int) const
497 {
498     return frameView().pagination().mode != Pagination::Unpaginated;
499 }
500
501 void RenderView::computeColumnCountAndWidth()
502 {
503     int columnWidth = contentLogicalWidth();
504     if (style().hasInlineColumnAxis()) {
505         if (int pageLength = frameView().pagination().pageLength)
506             columnWidth = pageLength;
507     }
508     setComputedColumnCountAndWidth(1, columnWidth);
509 }
510
511 void RenderView::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
512 {
513     // If we ever require layout but receive a paint anyway, something has gone horribly wrong.
514     ASSERT(!needsLayout());
515     // RenderViews should never be called to paint with an offset not on device pixels.
516     ASSERT(LayoutPoint(IntPoint(paintOffset.x(), paintOffset.y())) == paintOffset);
517
518     // This avoids painting garbage between columns if there is a column gap.
519     if (frameView().pagination().mode != Pagination::Unpaginated && paintInfo.shouldPaintWithinRoot(*this))
520         paintInfo.context().fillRect(paintInfo.rect, frameView().baseBackgroundColor(), ColorSpaceDeviceRGB);
521
522     paintObject(paintInfo, paintOffset);
523 }
524
525 static inline bool rendererObscuresBackground(RenderElement* rootObject)
526 {
527     if (!rootObject)
528         return false;
529     
530     const RenderStyle& style = rootObject->style();
531     if (style.visibility() != VISIBLE
532         || style.opacity() != 1
533         || style.hasTransform())
534         return false;
535     
536     if (rootObject->isComposited())
537         return false;
538
539     if (rootObject->rendererForRootBackground().style().backgroundClip() == TextFillBox)
540         return false;
541
542     if (style.hasBorderRadius())
543         return false;
544
545     return true;
546 }
547
548 void RenderView::paintBoxDecorations(PaintInfo& paintInfo, const LayoutPoint&)
549 {
550     if (!paintInfo.shouldPaintWithinRoot(*this))
551         return;
552
553     // Check to see if we are enclosed by a layer that requires complex painting rules.  If so, we cannot blit
554     // when scrolling, and we need to use slow repaints.  Examples of layers that require this are transparent layers,
555     // layers with reflections, or transformed layers.
556     // FIXME: This needs to be dynamic.  We should be able to go back to blitting if we ever stop being inside
557     // a transform, transparency layer, etc.
558     for (HTMLFrameOwnerElement* element = document().ownerElement(); element && element->renderer(); element = element->document().ownerElement()) {
559         RenderLayer* layer = element->renderer()->enclosingLayer();
560         if (layer->cannotBlitToWindow()) {
561             frameView().setCannotBlitToWindow();
562             break;
563         }
564
565         if (RenderLayer* compositingLayer = layer->enclosingCompositingLayerForRepaint()) {
566             if (!compositingLayer->backing()->paintsIntoWindow()) {
567                 frameView().setCannotBlitToWindow();
568                 break;
569             }
570         }
571     }
572
573     if (document().ownerElement())
574         return;
575
576     if (paintInfo.skipRootBackground())
577         return;
578
579     bool rootFillsViewport = false;
580     bool rootObscuresBackground = false;
581     Element* documentElement = document().documentElement();
582     if (RenderElement* rootRenderer = documentElement ? documentElement->renderer() : nullptr) {
583         // The document element's renderer is currently forced to be a block, but may not always be.
584         RenderBox* rootBox = is<RenderBox>(*rootRenderer) ? downcast<RenderBox>(rootRenderer) : nullptr;
585         rootFillsViewport = rootBox && !rootBox->x() && !rootBox->y() && rootBox->width() >= width() && rootBox->height() >= height();
586         rootObscuresBackground = rendererObscuresBackground(rootRenderer);
587     }
588
589     bool backgroundShouldExtendBeyondPage = frameView().frame().settings().backgroundShouldExtendBeyondPage();
590     compositor().setRootExtendedBackgroundColor(backgroundShouldExtendBeyondPage ? frameView().documentBackgroundColor() : Color());
591
592     Page* page = document().page();
593     float pageScaleFactor = page ? page->pageScaleFactor() : 1;
594
595     // If painting will entirely fill the view, no need to fill the background.
596     if (rootFillsViewport && rootObscuresBackground && pageScaleFactor >= 1)
597         return;
598
599     // This code typically only executes if the root element's visibility has been set to hidden,
600     // if there is a transform on the <html>, or if there is a page scale factor less than 1.
601     // Only fill with a background color (typically white) if we're the root document, 
602     // since iframes/frames with no background in the child document should show the parent's background.
603     // We use the base background color unless the backgroundShouldExtendBeyondPage setting is set,
604     // in which case we use the document's background color.
605     if (frameView().isTransparent()) // FIXME: This needs to be dynamic. We should be able to go back to blitting if we ever stop being transparent.
606         frameView().setCannotBlitToWindow(); // The parent must show behind the child.
607     else {
608         Color documentBackgroundColor = frameView().documentBackgroundColor();
609         Color backgroundColor = (backgroundShouldExtendBeyondPage && documentBackgroundColor.isValid()) ? documentBackgroundColor : frameView().baseBackgroundColor();
610         if (backgroundColor.alpha()) {
611             CompositeOperator previousOperator = paintInfo.context().compositeOperation();
612             paintInfo.context().setCompositeOperation(CompositeCopy);
613             paintInfo.context().fillRect(paintInfo.rect, backgroundColor, style().colorSpace());
614             paintInfo.context().setCompositeOperation(previousOperator);
615         } else
616             paintInfo.context().clearRect(paintInfo.rect);
617     }
618 }
619
620 bool RenderView::shouldRepaint(const LayoutRect& rect) const
621 {
622     return !printing() && !rect.isEmpty();
623 }
624
625 void RenderView::repaintRootContents()
626 {
627     if (layer()->isComposited()) {
628         layer()->setBackingNeedsRepaint(GraphicsLayer::DoNotClipToLayer);
629         return;
630     }
631     repaint();
632 }
633
634 void RenderView::repaintViewRectangle(const LayoutRect& repaintRect) const
635 {
636     if (!shouldRepaint(repaintRect))
637         return;
638
639     // FIXME: enclosingRect is needed as long as we integral snap ScrollView/FrameView/RenderWidget size/position.
640     IntRect enclosingRect = enclosingIntRect(repaintRect);
641     if (auto ownerElement = document().ownerElement()) {
642         RenderBox* ownerBox = ownerElement->renderBox();
643         if (!ownerBox)
644             return;
645         LayoutRect viewRect = this->viewRect();
646 #if PLATFORM(IOS)
647         // Don't clip using the visible rect since clipping is handled at a higher level on iPhone.
648         LayoutRect adjustedRect = enclosingRect;
649 #else
650         LayoutRect adjustedRect = intersection(enclosingRect, viewRect);
651 #endif
652         adjustedRect.moveBy(-viewRect.location());
653         adjustedRect.moveBy(ownerBox->contentBoxRect().location());
654         ownerBox->repaintRectangle(adjustedRect);
655         return;
656     }
657
658     frameView().addTrackedRepaintRect(snapRectToDevicePixels(repaintRect, document().deviceScaleFactor()));
659     if (!m_accumulatedRepaintRegion) {
660         frameView().repaintContentRectangle(enclosingRect);
661         return;
662     }
663     m_accumulatedRepaintRegion->unite(enclosingRect);
664
665     // Region will get slow if it gets too complex. Merge all rects so far to bounds if this happens.
666     // FIXME: Maybe there should be a region type that does this automatically.
667     static const unsigned maximumRepaintRegionGridSize = 16 * 16;
668     if (m_accumulatedRepaintRegion->gridSize() > maximumRepaintRegionGridSize)
669         m_accumulatedRepaintRegion = std::make_unique<Region>(m_accumulatedRepaintRegion->bounds());
670 }
671
672 void RenderView::flushAccumulatedRepaintRegion() const
673 {
674     ASSERT(!document().ownerElement());
675     ASSERT(m_accumulatedRepaintRegion);
676     auto repaintRects = m_accumulatedRepaintRegion->rects();
677     for (auto& rect : repaintRects)
678         frameView().repaintContentRectangle(rect);
679     m_accumulatedRepaintRegion = nullptr;
680 }
681
682 void RenderView::repaintViewAndCompositedLayers()
683 {
684     repaintRootContents();
685
686     RenderLayerCompositor& compositor = this->compositor();
687     if (compositor.inCompositingMode())
688         compositor.repaintCompositedLayers();
689 }
690
691 LayoutRect RenderView::visualOverflowRect() const
692 {
693     if (frameView().paintsEntireContents())
694         return layoutOverflowRect();
695
696     return RenderBlockFlow::visualOverflowRect();
697 }
698
699 LayoutRect RenderView::computeRectForRepaint(const LayoutRect& rect, const RenderLayerModelObject* repaintContainer, bool fixed) const
700 {
701     // If a container was specified, and was not nullptr or the RenderView,
702     // then we should have found it by now.
703     ASSERT_ARG(repaintContainer, !repaintContainer || repaintContainer == this);
704
705     if (printing())
706         return rect;
707     
708     LayoutRect adjustedRect = rect;
709     if (style().isFlippedBlocksWritingMode()) {
710         // We have to flip by hand since the view's logical height has not been determined.  We
711         // can use the viewport width and height.
712         if (style().isHorizontalWritingMode())
713             adjustedRect.setY(viewHeight() - adjustedRect.maxY());
714         else
715             adjustedRect.setX(viewWidth() - adjustedRect.maxX());
716     }
717
718     if (fixed) {
719 #if PLATFORM(IOS)
720         adjustedRect.move(fixedPositionOffset(frameView()));
721 #else
722         adjustedRect.move(frameView().scrollOffsetForFixedPosition());
723 #endif
724     }
725         
726     // Apply our transform if we have one (because of full page zooming).
727     if (!repaintContainer && layer() && layer()->transform())
728         adjustedRect = LayoutRect(layer()->transform()->mapRect(snapRectToDevicePixels(adjustedRect, document().deviceScaleFactor())));
729     return adjustedRect;
730 }
731
732 bool RenderView::isScrollableOrRubberbandableBox() const
733 {
734     // The main frame might be allowed to rubber-band even if there is no content to scroll to. This is unique to
735     // the main frame; subframes and overflow areas have to have content that can be scrolled to in order to rubber-band.
736     FrameView::Scrollability defineScrollable = frame().ownerElement() ? FrameView::Scrollability::Scrollable : FrameView::Scrollability::ScrollableOrRubberbandable;
737     return frameView().isScrollable(defineScrollable);
738 }
739
740 void RenderView::absoluteRects(Vector<IntRect>& rects, const LayoutPoint& accumulatedOffset) const
741 {
742     rects.append(snappedIntRect(accumulatedOffset, layer()->size()));
743 }
744
745 void RenderView::absoluteQuads(Vector<FloatQuad>& quads, bool* wasFixed) const
746 {
747     if (wasFixed)
748         *wasFixed = false;
749     quads.append(FloatRect(FloatPoint(), layer()->size()));
750 }
751
752 static RenderObject* rendererAfterPosition(RenderObject* object, unsigned offset)
753 {
754     if (!object)
755         return nullptr;
756
757     RenderObject* child = object->childAt(offset);
758     return child ? child : object->nextInPreOrderAfterChildren();
759 }
760
761 IntRect RenderView::selectionBounds(bool clipToVisibleContent) const
762 {
763     LayoutRect selRect = subtreeSelectionBounds(*this, clipToVisibleContent);
764
765     if (hasRenderNamedFlowThreads()) {
766         for (auto* namedFlowThread : *m_flowThreadController->renderNamedFlowThreadList()) {
767             LayoutRect currRect = subtreeSelectionBounds(*namedFlowThread, clipToVisibleContent);
768             selRect.unite(currRect);
769         }
770     }
771
772     return snappedIntRect(selRect);
773 }
774
775 LayoutRect RenderView::subtreeSelectionBounds(const SelectionSubtreeRoot& root, bool clipToVisibleContent) const
776 {
777     typedef HashMap<RenderObject*, std::unique_ptr<RenderSelectionInfo>> SelectionMap;
778     SelectionMap selectedObjects;
779
780     RenderObject* os = root.selectionData().selectionStart();
781     RenderObject* stop = rendererAfterPosition(root.selectionData().selectionEnd(), root.selectionData().selectionEndPos());
782     SelectionIterator selectionIterator(os);
783     while (os && os != stop) {
784         if ((os->canBeSelectionLeaf() || os == root.selectionData().selectionStart() || os == root.selectionData().selectionEnd()) && os->selectionState() != SelectionNone) {
785             // Blocks are responsible for painting line gaps and margin gaps. They must be examined as well.
786             selectedObjects.set(os, std::make_unique<RenderSelectionInfo>(*os, clipToVisibleContent));
787             RenderBlock* cb = os->containingBlock();
788             while (cb && !cb->isRenderView()) {
789                 std::unique_ptr<RenderSelectionInfo>& blockInfo = selectedObjects.add(cb, nullptr).iterator->value;
790                 if (blockInfo)
791                     break;
792                 blockInfo = std::make_unique<RenderSelectionInfo>(*cb, clipToVisibleContent);
793                 cb = cb->containingBlock();
794             }
795         }
796
797         os = selectionIterator.next();
798     }
799
800     // Now create a single bounding box rect that encloses the whole selection.
801     LayoutRect selRect;
802     SelectionMap::iterator end = selectedObjects.end();
803     for (SelectionMap::iterator i = selectedObjects.begin(); i != end; ++i) {
804         RenderSelectionInfo* info = i->value.get();
805         // RenderSelectionInfo::rect() is in the coordinates of the repaintContainer, so map to page coordinates.
806         LayoutRect currRect = info->rect();
807         if (RenderLayerModelObject* repaintContainer = info->repaintContainer()) {
808             FloatQuad absQuad = repaintContainer->localToAbsoluteQuad(FloatRect(currRect));
809             currRect = absQuad.enclosingBoundingBox(); 
810         }
811         selRect.unite(currRect);
812     }
813     return selRect;
814 }
815
816 void RenderView::repaintSelection() const
817 {
818     repaintSubtreeSelection(*this);
819
820     if (hasRenderNamedFlowThreads()) {
821         for (auto* namedFlowThread : *m_flowThreadController->renderNamedFlowThreadList())
822             repaintSubtreeSelection(*namedFlowThread);
823     }
824 }
825
826 void RenderView::repaintSubtreeSelection(const SelectionSubtreeRoot& root) const
827 {
828     HashSet<RenderBlock*> processedBlocks;
829
830     RenderObject* end = rendererAfterPosition(root.selectionData().selectionEnd(), root.selectionData().selectionEndPos());
831     SelectionIterator selectionIterator(root.selectionData().selectionStart());
832     for (RenderObject* o = selectionIterator.current(); o && o != end; o = selectionIterator.next()) {
833         if (!o->canBeSelectionLeaf() && o != root.selectionData().selectionStart() && o != root.selectionData().selectionEnd())
834             continue;
835         if (o->selectionState() == SelectionNone)
836             continue;
837
838         RenderSelectionInfo(*o, true).repaint();
839
840         // Blocks are responsible for painting line gaps and margin gaps. They must be examined as well.
841         for (RenderBlock* block = o->containingBlock(); block && !block->isRenderView(); block = block->containingBlock()) {
842             if (!processedBlocks.add(block).isNewEntry)
843                 break;
844             RenderSelectionInfo(*block, true).repaint();
845         }
846     }
847 }
848
849 // Compositing layer dimensions take outline size into account, so we have to recompute layer
850 // bounds when it changes.
851 // FIXME: This is ugly; it would be nice to have a better way to do this.
852 void RenderView::setMaximalOutlineSize(int outlineSize)
853 {
854     if (outlineSize == m_maximalOutlineSize)
855         return;
856     
857     m_maximalOutlineSize = outlineSize;
858     // maximalOutlineSize affects compositing layer dimensions.
859     // FIXME: this really just needs to be a geometry update.
860     compositor().setCompositingLayersNeedRebuild();
861 }
862
863 void RenderView::setSelection(RenderObject* start, int startPos, RenderObject* end, int endPos, SelectionRepaintMode blockRepaintMode)
864 {
865     // Make sure both our start and end objects are defined.
866     // Check www.msnbc.com and try clicking around to find the case where this happened.
867     if ((start && !end) || (end && !start))
868         return;
869
870     bool caretChanged = m_selectionWasCaret != frame().selection().isCaret();
871     m_selectionWasCaret = frame().selection().isCaret();
872     // Just return if the selection hasn't changed.
873     if (m_selectionUnsplitStart == start && m_selectionUnsplitStartPos == startPos
874         && m_selectionUnsplitEnd == end && m_selectionUnsplitEndPos == endPos && !caretChanged) {
875         return;
876     }
877
878 #if ENABLE(SERVICE_CONTROLS)
879     // Clear the current rects and create a notifier for the new rects we are about to gather.
880     // The Notifier updates the Editor when it goes out of scope and is destroyed.
881     std::unique_ptr<SelectionRectGatherer::Notifier> rectNotifier = m_selectionRectGatherer.clearAndCreateNotifier();
882 #endif // ENABLE(SERVICE_CONTROLS)
883     // Set global positions for new selection.
884     m_selectionUnsplitStart = start;
885     m_selectionUnsplitStartPos = startPos;
886     m_selectionUnsplitEnd = end;
887     m_selectionUnsplitEndPos = endPos;
888
889     // If there is no RenderNamedFlowThreads we follow the regular selection.
890     if (!hasRenderNamedFlowThreads()) {
891         RenderSubtreesMap singleSubtreeMap;
892         singleSubtreeMap.set(this, SelectionSubtreeData(start, startPos, end, endPos));
893         updateSelectionForSubtrees(singleSubtreeMap, blockRepaintMode);
894         return;
895     }
896
897     splitSelectionBetweenSubtrees(start, startPos, end, endPos, blockRepaintMode);
898 }
899
900 void RenderView::splitSelectionBetweenSubtrees(const RenderObject* start, int startPos, const RenderObject* end, int endPos, SelectionRepaintMode blockRepaintMode)
901 {
902     // Compute the visible selection end points for each of the subtrees.
903     RenderSubtreesMap renderSubtreesMap;
904
905     SelectionSubtreeData initialSelection;
906     renderSubtreesMap.set(this, initialSelection);
907     for (auto* namedFlowThread : *flowThreadController().renderNamedFlowThreadList())
908         renderSubtreesMap.set(namedFlowThread, initialSelection);
909
910     if (start && end) {
911         Node* startNode = start->node();
912         Node* endNode = end->node();
913         ASSERT(endNode);
914         Node* stopNode = NodeTraversal::nextSkippingChildren(*endNode);
915
916         for (Node* node = startNode; node != stopNode; node = NodeTraversal::next(*node)) {
917             RenderObject* renderer = node->renderer();
918             if (!renderer)
919                 continue;
920
921             SelectionSubtreeRoot& root = renderer->selectionRoot();
922             SelectionSubtreeData selectionData = renderSubtreesMap.get(&root);
923             if (selectionData.selectionClear()) {
924                 selectionData.setSelectionStart(node->renderer());
925                 selectionData.setSelectionStartPos(node == startNode ? startPos : 0);
926             }
927
928             selectionData.setSelectionEnd(node->renderer());
929             if (node == endNode)
930                 selectionData.setSelectionEndPos(endPos);
931             else
932                 selectionData.setSelectionEndPos(node->offsetInCharacters() ? node->maxCharacterOffset() : node->countChildNodes());
933
934             renderSubtreesMap.set(&root, selectionData);
935         }
936     }
937     
938     updateSelectionForSubtrees(renderSubtreesMap, blockRepaintMode);
939 }
940
941 void RenderView::updateSelectionForSubtrees(RenderSubtreesMap& renderSubtreesMap, SelectionRepaintMode blockRepaintMode)
942 {
943     SubtreeOldSelectionDataMap oldSelectionDataMap;
944     for (auto& subtreeSelectionInfo : renderSubtreesMap) {
945         SelectionSubtreeRoot& root = *subtreeSelectionInfo.key;
946         std::unique_ptr<OldSelectionData> oldSelectionData = std::make_unique<OldSelectionData>();
947
948         clearSubtreeSelection(root, blockRepaintMode, *oldSelectionData);
949         oldSelectionDataMap.set(&root, WTF::move(oldSelectionData));
950
951         root.setSelectionData(subtreeSelectionInfo.value);
952         if (hasRenderNamedFlowThreads())
953             root.adjustForVisibleSelection(document());
954     }
955
956     // Update selection status for the objects inside the selection subtrees.
957     // This needs to be done after the previous loop updated the selectionStart/End
958     // parameters of all subtrees because we're going to be climbing up the containing
959     // block chain and we might end up in a different selection subtree.
960     for (const auto* subtreeSelectionRoot : renderSubtreesMap.keys()) {
961         OldSelectionData& oldSelectionData = *oldSelectionDataMap.get(subtreeSelectionRoot);
962         applySubtreeSelection(*subtreeSelectionRoot, blockRepaintMode, oldSelectionData);
963     }
964 }
965
966 static inline bool isValidObjectForNewSelection(const SelectionSubtreeRoot& root, const RenderObject& object)
967 {
968     return (object.canBeSelectionLeaf() || &object == root.selectionData().selectionStart() || &object == root.selectionData().selectionEnd()) && object.selectionState() != RenderObject::SelectionNone && object.containingBlock();
969 }
970
971 void RenderView::clearSubtreeSelection(const SelectionSubtreeRoot& root, SelectionRepaintMode blockRepaintMode, OldSelectionData& oldSelectionData) const
972 {
973     // Record the old selected objects.  These will be used later
974     // when we compare against the new selected objects.
975     oldSelectionData.selectionStartPos = root.selectionData().selectionStartPos();
976     oldSelectionData.selectionEndPos = root.selectionData().selectionEndPos();
977     
978     // Blocks contain selected objects and fill gaps between them, either on the left, right, or in between lines and blocks.
979     // In order to get the repaint rect right, we have to examine left, middle, and right rects individually, since otherwise
980     // the union of those rects might remain the same even when changes have occurred.
981
982     RenderObject* os = root.selectionData().selectionStart();
983     RenderObject* stop = rendererAfterPosition(root.selectionData().selectionEnd(), root.selectionData().selectionEndPos());
984     SelectionIterator selectionIterator(os);
985     while (os && os != stop) {
986         if (isValidObjectForNewSelection(root, *os)) {
987             // Blocks are responsible for painting line gaps and margin gaps.  They must be examined as well.
988             oldSelectionData.selectedObjects.set(os, std::make_unique<RenderSelectionInfo>(*os, true));
989             if (blockRepaintMode == RepaintNewXOROld) {
990                 RenderBlock* cb = os->containingBlock();
991                 while (cb && !cb->isRenderView()) {
992                     std::unique_ptr<RenderBlockSelectionInfo>& blockInfo = oldSelectionData.selectedBlocks.add(cb, nullptr).iterator->value;
993                     if (blockInfo)
994                         break;
995                     blockInfo = std::make_unique<RenderBlockSelectionInfo>(*cb);
996                     cb = cb->containingBlock();
997                 }
998             }
999         }
1000
1001         os = selectionIterator.next();
1002     }
1003
1004     for (auto* selectedObject : oldSelectionData.selectedObjects.keys())
1005         selectedObject->setSelectionStateIfNeeded(SelectionNone);
1006 }
1007
1008 void RenderView::applySubtreeSelection(const SelectionSubtreeRoot& root, SelectionRepaintMode blockRepaintMode, const OldSelectionData& oldSelectionData)
1009 {
1010     // Update the selection status of all objects between selectionStart and selectionEnd
1011     if (root.selectionData().selectionStart() && root.selectionData().selectionStart() == root.selectionData().selectionEnd())
1012         root.selectionData().selectionStart()->setSelectionStateIfNeeded(SelectionBoth);
1013     else {
1014         if (root.selectionData().selectionStart())
1015             root.selectionData().selectionStart()->setSelectionStateIfNeeded(SelectionStart);
1016         if (root.selectionData().selectionEnd())
1017             root.selectionData().selectionEnd()->setSelectionStateIfNeeded(SelectionEnd);
1018     }
1019
1020     RenderObject* selectionStart = root.selectionData().selectionStart();
1021     RenderObject* selectionEnd = rendererAfterPosition(root.selectionData().selectionEnd(), root.selectionData().selectionEndPos());
1022     SelectionIterator selectionIterator(selectionStart);
1023     for (RenderObject* currentRenderer = selectionStart; currentRenderer && currentRenderer != selectionEnd; currentRenderer = selectionIterator.next()) {
1024         if (currentRenderer == root.selectionData().selectionStart() || currentRenderer == root.selectionData().selectionEnd())
1025             continue;
1026         if (!currentRenderer->canBeSelectionLeaf())
1027             continue;
1028         // FIXME: Move this logic to SelectionIterator::next()
1029         if (&currentRenderer->selectionRoot() != &root)
1030             continue;
1031         currentRenderer->setSelectionStateIfNeeded(SelectionInside);
1032     }
1033
1034     if (blockRepaintMode != RepaintNothing)
1035         layer()->clearBlockSelectionGapsBounds();
1036
1037     // Now that the selection state has been updated for the new objects, walk them again and
1038     // put them in the new objects list.
1039     SelectedObjectMap newSelectedObjects;
1040     SelectedBlockMap newSelectedBlocks;
1041     selectionIterator = SelectionIterator(selectionStart);
1042     for (RenderObject* currentRenderer = selectionStart; currentRenderer && currentRenderer != selectionEnd; currentRenderer = selectionIterator.next()) {
1043         if (isValidObjectForNewSelection(root, *currentRenderer)) {
1044             std::unique_ptr<RenderSelectionInfo> selectionInfo = std::make_unique<RenderSelectionInfo>(*currentRenderer, true);
1045
1046 #if ENABLE(SERVICE_CONTROLS)
1047             for (auto& rect : selectionInfo->collectedSelectionRects())
1048                 m_selectionRectGatherer.addRect(selectionInfo->repaintContainer(), rect);
1049             if (!currentRenderer->isTextOrLineBreak())
1050                 m_selectionRectGatherer.setTextOnly(false);
1051 #endif
1052
1053             newSelectedObjects.set(currentRenderer, WTF::move(selectionInfo));
1054
1055             RenderBlock* containingBlock = currentRenderer->containingBlock();
1056             while (containingBlock && !containingBlock->isRenderView()) {
1057                 std::unique_ptr<RenderBlockSelectionInfo>& blockInfo = newSelectedBlocks.add(containingBlock, nullptr).iterator->value;
1058                 if (blockInfo)
1059                     break;
1060                 blockInfo = std::make_unique<RenderBlockSelectionInfo>(*containingBlock);
1061                 containingBlock = containingBlock->containingBlock();
1062
1063 #if ENABLE(SERVICE_CONTROLS)
1064                 m_selectionRectGatherer.addGapRects(blockInfo->repaintContainer(), blockInfo->rects());
1065 #endif
1066             }
1067         }
1068     }
1069
1070     if (blockRepaintMode == RepaintNothing)
1071         return;
1072
1073     // Have any of the old selected objects changed compared to the new selection?
1074     for (const auto& selectedObjectInfo : oldSelectionData.selectedObjects) {
1075         RenderObject* obj = selectedObjectInfo.key;
1076         RenderSelectionInfo* newInfo = newSelectedObjects.get(obj);
1077         RenderSelectionInfo* oldInfo = selectedObjectInfo.value.get();
1078         if (!newInfo || oldInfo->rect() != newInfo->rect() || oldInfo->state() != newInfo->state()
1079             || (root.selectionData().selectionStart() == obj && oldSelectionData.selectionStartPos != root.selectionData().selectionStartPos())
1080             || (root.selectionData().selectionEnd() == obj && oldSelectionData.selectionEndPos != root.selectionData().selectionEndPos())) {
1081             oldInfo->repaint();
1082             if (newInfo) {
1083                 newInfo->repaint();
1084                 newSelectedObjects.remove(obj);
1085             }
1086         }
1087     }
1088
1089     // Any new objects that remain were not found in the old objects dict, and so they need to be updated.
1090     for (const auto& selectedObjectInfo : newSelectedObjects)
1091         selectedObjectInfo.value->repaint();
1092
1093     // Have any of the old blocks changed?
1094     for (const auto& selectedBlockInfo : oldSelectionData.selectedBlocks) {
1095         const RenderBlock* block = selectedBlockInfo.key;
1096         RenderBlockSelectionInfo* newInfo = newSelectedBlocks.get(block);
1097         RenderBlockSelectionInfo* oldInfo = selectedBlockInfo.value.get();
1098         if (!newInfo || oldInfo->rects() != newInfo->rects() || oldInfo->state() != newInfo->state()) {
1099             oldInfo->repaint();
1100             if (newInfo) {
1101                 newInfo->repaint();
1102                 newSelectedBlocks.remove(block);
1103             }
1104         }
1105     }
1106
1107     // Any new blocks that remain were not found in the old blocks dict, and so they need to be updated.
1108     for (const auto& selectedBlockInfo : newSelectedBlocks)
1109         selectedBlockInfo.value->repaint();
1110 }
1111
1112 void RenderView::getSelection(RenderObject*& startRenderer, int& startOffset, RenderObject*& endRenderer, int& endOffset) const
1113 {
1114     startRenderer = m_selectionUnsplitStart;
1115     startOffset = m_selectionUnsplitStartPos;
1116     endRenderer = m_selectionUnsplitEnd;
1117     endOffset = m_selectionUnsplitEndPos;
1118 }
1119
1120 void RenderView::clearSelection()
1121 {
1122     layer()->repaintBlockSelectionGaps();
1123     setSelection(nullptr, -1, nullptr, -1, RepaintNewMinusOld);
1124 }
1125
1126 bool RenderView::printing() const
1127 {
1128     return document().printing();
1129 }
1130
1131 bool RenderView::shouldUsePrintingLayout() const
1132 {
1133     if (!printing())
1134         return false;
1135     return frameView().frame().shouldUsePrintingLayout();
1136 }
1137
1138 LayoutRect RenderView::viewRect() const
1139 {
1140     if (shouldUsePrintingLayout())
1141         return LayoutRect(LayoutPoint(), size());
1142     return frameView().visibleContentRect(ScrollableArea::LegacyIOSDocumentVisibleRect);
1143 }
1144
1145 IntRect RenderView::unscaledDocumentRect() const
1146 {
1147     LayoutRect overflowRect(layoutOverflowRect());
1148     flipForWritingMode(overflowRect);
1149     return snappedIntRect(overflowRect);
1150 }
1151
1152 bool RenderView::rootBackgroundIsEntirelyFixed() const
1153 {
1154     RenderElement* rootObject = document().documentElement() ? document().documentElement()->renderer() : nullptr;
1155     if (!rootObject)
1156         return false;
1157
1158     return rootObject->rendererForRootBackground().hasEntirelyFixedBackground();
1159 }
1160     
1161 LayoutRect RenderView::unextendedBackgroundRect() const
1162 {
1163     // FIXME: What is this? Need to patch for new columns?
1164     return unscaledDocumentRect();
1165 }
1166     
1167 LayoutRect RenderView::backgroundRect() const
1168 {
1169     // FIXME: New columns care about this?
1170     if (frameView().hasExtendedBackgroundRectForPainting())
1171         return frameView().extendedBackgroundRectForPainting();
1172
1173     return unextendedBackgroundRect();
1174 }
1175
1176 IntRect RenderView::documentRect() const
1177 {
1178     FloatRect overflowRect(unscaledDocumentRect());
1179     if (hasTransform())
1180         overflowRect = layer()->currentTransform().mapRect(overflowRect);
1181     return IntRect(overflowRect);
1182 }
1183
1184 int RenderView::viewHeight() const
1185 {
1186     int height = 0;
1187     if (!shouldUsePrintingLayout()) {
1188         height = frameView().layoutHeight();
1189         height = frameView().useFixedLayout() ? ceilf(style().effectiveZoom() * float(height)) : height;
1190     }
1191     return height;
1192 }
1193
1194 int RenderView::viewWidth() const
1195 {
1196     int width = 0;
1197     if (!shouldUsePrintingLayout()) {
1198         width = frameView().layoutWidth();
1199         width = frameView().useFixedLayout() ? ceilf(style().effectiveZoom() * float(width)) : width;
1200     }
1201     return width;
1202 }
1203
1204 int RenderView::viewLogicalHeight() const
1205 {
1206     int height = style().isHorizontalWritingMode() ? viewHeight() : viewWidth();
1207     return height;
1208 }
1209
1210 float RenderView::zoomFactor() const
1211 {
1212     return frameView().frame().pageZoomFactor();
1213 }
1214
1215 void RenderView::pushLayoutState(RenderObject& root)
1216 {
1217     ASSERT(m_layoutStateDisableCount == 0);
1218     ASSERT(m_layoutState == 0);
1219
1220     m_layoutState = std::make_unique<LayoutState>(root);
1221     pushLayoutStateForCurrentFlowThread(root);
1222 }
1223
1224 bool RenderView::shouldDisableLayoutStateForSubtree(RenderObject* renderer) const
1225 {
1226     RenderObject* o = renderer;
1227     while (o) {
1228         if (o->hasTransform() || o->hasReflection())
1229             return true;
1230         o = o->container();
1231     }
1232     return false;
1233 }
1234
1235 IntSize RenderView::viewportSizeForCSSViewportUnits() const
1236 {
1237     return frameView().viewportSizeForCSSViewportUnits();
1238 }
1239
1240 void RenderView::updateHitTestResult(HitTestResult& result, const LayoutPoint& point)
1241 {
1242     if (result.innerNode())
1243         return;
1244
1245     if (multiColumnFlowThread() && multiColumnFlowThread()->firstMultiColumnSet())
1246         return multiColumnFlowThread()->firstMultiColumnSet()->updateHitTestResult(result, point);
1247
1248     Node* node = document().documentElement();
1249     if (node) {
1250         result.setInnerNode(node);
1251         if (!result.innerNonSharedNode())
1252             result.setInnerNonSharedNode(node);
1253
1254         LayoutPoint adjustedPoint = point;
1255         offsetForContents(adjustedPoint);
1256
1257         result.setLocalPoint(adjustedPoint);
1258     }
1259 }
1260
1261 // FIXME: This function is obsolete and only used by embedded WebViews inside AppKit NSViews.
1262 // Do not add callers of this function!
1263 // The idea here is to take into account what object is moving the pagination point, and
1264 // thus choose the best place to chop it.
1265 void RenderView::setBestTruncatedAt(int y, RenderBoxModelObject* forRenderer, bool forcedBreak)
1266 {
1267     // Nobody else can set a page break once we have a forced break.
1268     if (m_legacyPrinting.m_forcedPageBreak)
1269         return;
1270
1271     // Forced breaks always win over unforced breaks.
1272     if (forcedBreak) {
1273         m_legacyPrinting.m_forcedPageBreak = true;
1274         m_legacyPrinting.m_bestTruncatedAt = y;
1275         return;
1276     }
1277
1278     // Prefer the widest object that tries to move the pagination point
1279     IntRect boundingBox = forRenderer->borderBoundingBox();
1280     if (boundingBox.width() > m_legacyPrinting.m_truncatorWidth) {
1281         m_legacyPrinting.m_truncatorWidth = boundingBox.width();
1282         m_legacyPrinting.m_bestTruncatedAt = y;
1283     }
1284 }
1285
1286 bool RenderView::usesCompositing() const
1287 {
1288     return m_compositor && m_compositor->inCompositingMode();
1289 }
1290
1291 RenderLayerCompositor& RenderView::compositor()
1292 {
1293     if (!m_compositor)
1294         m_compositor = std::make_unique<RenderLayerCompositor>(*this);
1295
1296     return *m_compositor;
1297 }
1298
1299 void RenderView::setIsInWindow(bool isInWindow)
1300 {
1301     if (m_compositor)
1302         m_compositor->setIsInWindow(isInWindow);
1303 }
1304
1305 void RenderView::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
1306 {
1307     RenderBlockFlow::styleDidChange(diff, oldStyle);
1308     if (hasRenderNamedFlowThreads())
1309         flowThreadController().styleDidChange();
1310
1311     frameView().styleDidChange();
1312 }
1313
1314 bool RenderView::hasRenderNamedFlowThreads() const
1315 {
1316     return m_flowThreadController && m_flowThreadController->hasRenderNamedFlowThreads();
1317 }
1318
1319 bool RenderView::checkTwoPassLayoutForAutoHeightRegions() const
1320 {
1321     return hasRenderNamedFlowThreads() && m_flowThreadController->hasFlowThreadsWithAutoLogicalHeightRegions();
1322 }
1323
1324 FlowThreadController& RenderView::flowThreadController()
1325 {
1326     if (!m_flowThreadController)
1327         m_flowThreadController = std::make_unique<FlowThreadController>(this);
1328
1329     return *m_flowThreadController;
1330 }
1331
1332 void RenderView::pushLayoutStateForCurrentFlowThread(const RenderObject& object)
1333 {
1334     if (!m_flowThreadController)
1335         return;
1336
1337     RenderFlowThread* currentFlowThread = object.flowThreadContainingBlock();
1338     if (!currentFlowThread)
1339         return;
1340
1341     m_layoutState->setCurrentRenderFlowThread(currentFlowThread);
1342
1343     currentFlowThread->pushFlowThreadLayoutState(object);
1344 }
1345
1346 void RenderView::popLayoutStateForCurrentFlowThread()
1347 {
1348     if (!m_flowThreadController)
1349         return;
1350
1351     RenderFlowThread* currentFlowThread = m_layoutState->currentRenderFlowThread();
1352     if (!currentFlowThread)
1353         return;
1354
1355     currentFlowThread->popFlowThreadLayoutState();
1356 }
1357
1358 ImageQualityController& RenderView::imageQualityController()
1359 {
1360     if (!m_imageQualityController)
1361         m_imageQualityController = std::make_unique<ImageQualityController>(*this);
1362     return *m_imageQualityController;
1363 }
1364
1365 void RenderView::addRendererWithPausedImageAnimations(RenderElement& renderer)
1366 {
1367     if (renderer.hasPausedImageAnimations()) {
1368         ASSERT(m_renderersWithPausedImageAnimation.contains(&renderer));
1369         return;
1370     }
1371     renderer.setHasPausedImageAnimations(true);
1372     m_renderersWithPausedImageAnimation.add(&renderer);
1373 }
1374
1375 void RenderView::removeRendererWithPausedImageAnimations(RenderElement& renderer)
1376 {
1377     ASSERT(renderer.hasPausedImageAnimations());
1378     ASSERT(m_renderersWithPausedImageAnimation.contains(&renderer));
1379
1380     renderer.setHasPausedImageAnimations(false);
1381     m_renderersWithPausedImageAnimation.remove(&renderer);
1382 }
1383
1384 void RenderView::resumePausedImageAnimationsIfNeeded(IntRect visibleRect)
1385 {
1386     Vector<RenderElement*, 10> toRemove;
1387     for (auto* renderer : m_renderersWithPausedImageAnimation) {
1388         if (renderer->repaintForPausedImageAnimationsIfNeeded(visibleRect))
1389             toRemove.append(renderer);
1390     }
1391     for (auto& renderer : toRemove)
1392         removeRendererWithPausedImageAnimations(*renderer);
1393 }
1394
1395 RenderView::RepaintRegionAccumulator::RepaintRegionAccumulator(RenderView* view)
1396     : m_rootView(view ? view->document().topDocument().renderView() : nullptr)
1397 {
1398     if (!m_rootView)
1399         return;
1400     m_wasAccumulatingRepaintRegion = !!m_rootView->m_accumulatedRepaintRegion;
1401     if (!m_wasAccumulatingRepaintRegion)
1402         m_rootView->m_accumulatedRepaintRegion = std::make_unique<Region>();
1403 }
1404
1405 RenderView::RepaintRegionAccumulator::~RepaintRegionAccumulator()
1406 {
1407     if (!m_rootView)
1408         return;
1409     if (m_wasAccumulatingRepaintRegion)
1410         return;
1411     m_rootView->flushAccumulatedRepaintRegion();
1412 }
1413
1414 unsigned RenderView::pageNumberForBlockProgressionOffset(int offset) const
1415 {
1416     int columnNumber = 0;
1417     const Pagination& pagination = frameView().frame().page()->pagination();
1418     if (pagination.mode == Pagination::Unpaginated)
1419         return columnNumber;
1420     
1421     bool progressionIsInline = false;
1422     bool progressionIsReversed = false;
1423     
1424     if (multiColumnFlowThread()) {
1425         progressionIsInline = multiColumnFlowThread()->progressionIsInline();
1426         progressionIsReversed = multiColumnFlowThread()->progressionIsReversed();
1427     } else
1428         return columnNumber;
1429     
1430     if (!progressionIsInline) {
1431         if (!progressionIsReversed)
1432             columnNumber = (pagination.pageLength + pagination.gap - offset) / (pagination.pageLength + pagination.gap);
1433         else
1434             columnNumber = offset / (pagination.pageLength + pagination.gap);
1435     }
1436
1437     return columnNumber;
1438 }
1439
1440 unsigned RenderView::pageCount() const
1441 {
1442     const Pagination& pagination = frameView().frame().page()->pagination();
1443     if (pagination.mode == Pagination::Unpaginated)
1444         return 0;
1445     
1446     if (multiColumnFlowThread() && multiColumnFlowThread()->firstMultiColumnSet())
1447         return multiColumnFlowThread()->firstMultiColumnSet()->columnCount();
1448
1449     return 0;
1450 }
1451
1452 #if ENABLE(CSS_SCROLL_SNAP)
1453 void RenderView::registerBoxWithScrollSnapCoordinates(const RenderBox& box)
1454 {
1455     m_boxesWithScrollSnapCoordinates.add(&box);
1456 }
1457
1458 void RenderView::unregisterBoxWithScrollSnapCoordinates(const RenderBox& box)
1459 {
1460     m_boxesWithScrollSnapCoordinates.remove(&box);
1461 }
1462 #endif
1463
1464 } // namespace WebCore