2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2006, 2015 Apple Inc.
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.
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.
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.
25 #include "FrameView.h"
26 #include "LayoutState.h"
28 #include "RenderBlockFlow.h"
29 #include "RenderWidget.h"
30 #include "SelectionSubtreeRoot.h"
32 #include <wtf/HashSet.h>
34 #if ENABLE(SERVICE_CONTROLS)
35 #include "SelectionRectGatherer.h"
40 class FlowThreadController;
41 class ImageQualityController;
42 class RenderLayerCompositor;
45 class RenderView final : public RenderBlockFlow, public SelectionSubtreeRoot {
47 RenderView(Document&, Ref<RenderStyle>&&);
48 virtual ~RenderView();
50 WEBCORE_EXPORT bool hitTest(const HitTestRequest&, HitTestResult&);
51 bool hitTest(const HitTestRequest&, const HitTestLocation&, HitTestResult&);
53 virtual const char* renderName() const override { return "RenderView"; }
55 virtual bool requiresLayer() const override { return true; }
57 virtual bool isChildAllowed(const RenderObject&, const RenderStyle&) const override;
59 virtual void layout() override;
60 virtual void updateLogicalWidth() override;
61 virtual void computeLogicalHeight(LayoutUnit logicalHeight, LayoutUnit logicalTop, LogicalExtentComputedValues&) const override;
63 virtual LayoutUnit availableLogicalHeight(AvailableLogicalHeightType) const override;
65 // The same as the FrameView's layoutHeight/layoutWidth but with null check guards.
66 int viewHeight() const;
67 int viewWidth() const;
68 int viewLogicalWidth() const { return style().isHorizontalWritingMode() ? viewWidth() : viewHeight(); }
69 int viewLogicalHeight() const;
71 LayoutUnit clientLogicalWidthForFixedPosition() const;
72 LayoutUnit clientLogicalHeightForFixedPosition() const;
74 float zoomFactor() const;
76 FrameView& frameView() const { return m_frameView; }
78 virtual LayoutRect visualOverflowRect() const override;
79 virtual void computeRectForRepaint(const RenderLayerModelObject* repaintContainer, LayoutRect&, bool fixed = false) const override;
80 void repaintRootContents();
81 void repaintViewRectangle(const LayoutRect&) const;
82 void repaintViewAndCompositedLayers();
84 virtual void paint(PaintInfo&, const LayoutPoint&) override;
85 virtual void paintBoxDecorations(PaintInfo&, const LayoutPoint&) override;
87 enum SelectionRepaintMode { RepaintNewXOROld, RepaintNewMinusOld, RepaintNothing };
88 void setSelection(RenderObject* start, int startPos, RenderObject* end, int endPos, SelectionRepaintMode = RepaintNewXOROld);
89 void getSelection(RenderObject*& startRenderer, int& startOffset, RenderObject*& endRenderer, int& endOffset) const;
90 void clearSelection();
91 RenderObject* selectionUnsplitStart() const { return m_selectionUnsplitStart; }
92 RenderObject* selectionUnsplitEnd() const { return m_selectionUnsplitEnd; }
93 IntRect selectionBounds(bool clipToVisibleContent = true) const;
94 void repaintSelection() const;
96 bool printing() const;
98 virtual void absoluteRects(Vector<IntRect>&, const LayoutPoint& accumulatedOffset) const override;
99 virtual void absoluteQuads(Vector<FloatQuad>&, bool* wasFixed) const override;
101 void setMaximalOutlineSize(int o);
102 int maximalOutlineSize() const { return m_maximalOutlineSize; }
104 LayoutRect viewRect() const;
106 // layoutDelta is used transiently during layout to store how far an object has moved from its
107 // last layout location, in order to repaint correctly.
108 // If we're doing a full repaint m_layoutState will be 0, but in that case layoutDelta doesn't matter.
109 LayoutSize layoutDelta() const
111 return m_layoutState ? m_layoutState->m_layoutDelta : LayoutSize();
113 void addLayoutDelta(const LayoutSize& delta)
116 m_layoutState->m_layoutDelta += delta;
117 #if !ASSERT_DISABLED && ENABLE(SATURATED_LAYOUT_ARITHMETIC)
118 m_layoutState->m_layoutDeltaXSaturated |= m_layoutState->m_layoutDelta.width() == LayoutUnit::max() || m_layoutState->m_layoutDelta.width() == LayoutUnit::min();
119 m_layoutState->m_layoutDeltaYSaturated |= m_layoutState->m_layoutDelta.height() == LayoutUnit::max() || m_layoutState->m_layoutDelta.height() == LayoutUnit::min();
125 bool layoutDeltaMatches(const LayoutSize& delta)
129 #if ENABLE(SATURATED_LAYOUT_ARITHMETIC)
130 return (delta.width() == m_layoutState->m_layoutDelta.width() || m_layoutState->m_layoutDeltaXSaturated) && (delta.height() == m_layoutState->m_layoutDelta.height() || m_layoutState->m_layoutDeltaYSaturated);
132 return delta == m_layoutState->m_layoutDelta;
137 bool doingFullRepaint() const { return frameView().needsFullRepaint(); }
140 void pushLayoutState(RenderObject&);
141 void popLayoutState(RenderObject&) { return popLayoutState(); } // Just doing this to keep popLayoutState() private and to make the subtree calls symmetrical.
143 bool shouldDisableLayoutStateForSubtree(RenderObject*) const;
145 // Returns true if layoutState should be used for its cached offset and clip.
146 bool layoutStateEnabled() const { return m_layoutStateDisableCount == 0 && m_layoutState; }
147 LayoutState* layoutState() const { return m_layoutState.get(); }
149 virtual void updateHitTestResult(HitTestResult&, const LayoutPoint&) override;
151 LayoutUnit pageLogicalHeight() const { return m_pageLogicalHeight; }
152 void setPageLogicalHeight(LayoutUnit height)
154 if (m_pageLogicalHeight != height) {
155 m_pageLogicalHeight = height;
156 m_pageLogicalHeightChanged = true;
159 LayoutUnit pageOrViewLogicalHeight() const;
161 // This method is used to assign a page number only when pagination modes have
162 // a block progression. This happens with vertical-rl books for example, but it
163 // doesn't happen for normal horizontal-tb books. This is a very specialized
164 // function and should not be mistaken for a general page number API.
165 unsigned pageNumberForBlockProgressionOffset(int offset) const;
167 unsigned pageCount() const;
169 // FIXME: These functions are deprecated. No code should be added that uses these.
170 int bestTruncatedAt() const { return m_legacyPrinting.m_bestTruncatedAt; }
171 void setBestTruncatedAt(int y, RenderBoxModelObject* forRenderer, bool forcedBreak = false);
172 int truncatedAt() const { return m_legacyPrinting.m_truncatedAt; }
173 void setTruncatedAt(int y)
175 m_legacyPrinting.m_truncatedAt = y;
176 m_legacyPrinting.m_bestTruncatedAt = 0;
177 m_legacyPrinting.m_truncatorWidth = 0;
178 m_legacyPrinting.m_forcedPageBreak = false;
180 const IntRect& printRect() const { return m_legacyPrinting.m_printRect; }
181 void setPrintRect(const IntRect& r) { m_legacyPrinting.m_printRect = r; }
182 // End deprecated functions.
184 // Notification that this view moved into or out of a native window.
185 void setIsInWindow(bool);
187 WEBCORE_EXPORT RenderLayerCompositor& compositor();
188 WEBCORE_EXPORT bool usesCompositing() const;
190 WEBCORE_EXPORT IntRect unscaledDocumentRect() const;
191 LayoutRect unextendedBackgroundRect(RenderBox* backgroundRenderer) const;
192 LayoutRect backgroundRect(RenderBox* backgroundRenderer) const;
194 WEBCORE_EXPORT IntRect documentRect() const;
196 // Renderer that paints the root background has background-images which all have background-attachment: fixed.
197 bool rootBackgroundIsEntirelyFixed() const;
199 bool hasRenderNamedFlowThreads() const;
200 bool checkTwoPassLayoutForAutoHeightRegions() const;
201 FlowThreadController& flowThreadController();
203 virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle) override;
205 IntSize viewportSizeForCSSViewportUnits() const;
207 void setRenderQuoteHead(RenderQuote* head) { m_renderQuoteHead = head; }
208 RenderQuote* renderQuoteHead() const { return m_renderQuoteHead; }
210 // FIXME: This is a work around because the current implementation of counters
211 // requires walking the entire tree repeatedly and most pages don't actually use either
212 // feature so we shouldn't take the performance hit when not needed. Long term we should
213 // rewrite the counter and quotes code.
214 void addRenderCounter() { m_renderCounterCount++; }
215 void removeRenderCounter() { ASSERT(m_renderCounterCount > 0); m_renderCounterCount--; }
216 bool hasRenderCounters() { return m_renderCounterCount; }
218 ImageQualityController& imageQualityController();
220 void setHasSoftwareFilters(bool hasSoftwareFilters) { m_hasSoftwareFilters = hasSoftwareFilters; }
221 bool hasSoftwareFilters() const { return m_hasSoftwareFilters; }
223 uint64_t rendererCount() const { return m_rendererCount; }
224 void didCreateRenderer() { ++m_rendererCount; }
225 void didDestroyRenderer() { --m_rendererCount; }
227 void resumePausedImageAnimationsIfNeeded(IntRect visibleRect);
228 void addRendererWithPausedImageAnimations(RenderElement&);
229 void removeRendererWithPausedImageAnimations(RenderElement&);
231 class RepaintRegionAccumulator {
232 WTF_MAKE_NONCOPYABLE(RepaintRegionAccumulator);
234 RepaintRegionAccumulator(RenderView*);
235 ~RepaintRegionAccumulator();
238 RenderView* m_rootView;
239 bool m_wasAccumulatingRepaintRegion;
242 void scheduleLazyRepaint(RenderBox&);
243 void unscheduleLazyRepaint(RenderBox&);
245 void protectRenderWidgetUntilLayoutIsDone(RenderWidget& widget) { m_protectedRenderWidgets.append(&widget); }
246 void releaseProtectedRenderWidgets() { m_protectedRenderWidgets.clear(); }
248 #if ENABLE(CSS_SCROLL_SNAP)
249 void registerBoxWithScrollSnapCoordinates(const RenderBox&);
250 void unregisterBoxWithScrollSnapCoordinates(const RenderBox&);
251 const HashSet<const RenderBox*>& boxesWithScrollSnapCoordinates() { return m_boxesWithScrollSnapCoordinates; }
255 virtual void mapLocalToContainer(const RenderLayerModelObject* repaintContainer, TransformState&, MapCoordinatesFlags, bool* wasFixed) const override;
256 virtual const RenderObject* pushMappingToContainer(const RenderLayerModelObject* ancestorToStopAt, RenderGeometryMap&) const override;
257 virtual void mapAbsoluteToLocalPoint(MapCoordinatesFlags, TransformState&) const override;
258 virtual bool requiresColumns(int desiredColumnCount) const override;
261 void initializeLayoutState(LayoutState&);
263 virtual void computeColumnCountAndWidth() override;
265 bool shouldRepaint(const LayoutRect&) const;
266 void flushAccumulatedRepaintRegion() const;
268 // These functions may only be accessed by LayoutStateMaintainer.
269 bool pushLayoutState(RenderBox& renderer, const LayoutSize& offset, LayoutUnit pageHeight = 0, bool pageHeightChanged = false)
271 // We push LayoutState even if layoutState is disabled because it stores layoutDelta too.
272 if (!doingFullRepaint() || m_layoutState->isPaginated() || renderer.flowThreadContainingBlock()
273 || m_layoutState->lineGrid() || (renderer.style().lineGrid() != RenderStyle::initialLineGrid() && renderer.isRenderBlockFlow())) {
274 m_layoutState = std::make_unique<LayoutState>(WTF::move(m_layoutState), &renderer, offset, pageHeight, pageHeightChanged);
275 pushLayoutStateForCurrentFlowThread(renderer);
281 void popLayoutState()
283 popLayoutStateForCurrentFlowThread();
284 m_layoutState = WTF::move(m_layoutState->m_next);
287 // Suspends the LayoutState optimization. Used under transforms that cannot be represented by
288 // LayoutState (common in SVG) and when manipulating the render tree during layout in ways
289 // that can trigger repaint of a non-child (e.g. when a list item moves its list marker around).
290 // Note that even when disabled, LayoutState is still used to store layoutDelta.
291 // These functions may only be accessed by LayoutStateMaintainer or LayoutStateDisabler.
292 void disableLayoutState() { m_layoutStateDisableCount++; }
293 void enableLayoutState() { ASSERT(m_layoutStateDisableCount > 0); m_layoutStateDisableCount--; }
295 void layoutContent(const LayoutState&);
296 void layoutContentInAutoLogicalHeightRegions(const LayoutState&);
297 void layoutContentToComputeOverflowInRegions(const LayoutState&);
299 void checkLayoutState(const LayoutState&);
302 void pushLayoutStateForCurrentFlowThread(const RenderObject&);
303 void popLayoutStateForCurrentFlowThread();
305 friend class LayoutStateMaintainer;
306 friend class LayoutStateDisabler;
308 virtual bool isScrollableOrRubberbandableBox() const override;
310 void splitSelectionBetweenSubtrees(const RenderObject* startRenderer, int startPos, const RenderObject* endRenderer, int endPos, SelectionRepaintMode blockRepaintMode);
311 void clearSubtreeSelection(const SelectionSubtreeRoot&, SelectionRepaintMode, OldSelectionData&) const;
312 void updateSelectionForSubtrees(RenderSubtreesMap&, SelectionRepaintMode);
313 void applySubtreeSelection(const SelectionSubtreeRoot&, SelectionRepaintMode, const OldSelectionData&);
314 LayoutRect subtreeSelectionBounds(const SelectionSubtreeRoot&, bool clipToVisibleContent = true) const;
315 void repaintSubtreeSelection(const SelectionSubtreeRoot&) const;
318 FrameView& m_frameView;
320 RenderObject* m_selectionUnsplitStart;
321 RenderObject* m_selectionUnsplitEnd;
322 int m_selectionUnsplitStartPos;
323 int m_selectionUnsplitEndPos;
325 uint64_t m_rendererCount;
327 mutable std::unique_ptr<Region> m_accumulatedRepaintRegion;
329 // FIXME: Only used by embedded WebViews inside AppKit NSViews. Find a way to remove.
330 struct LegacyPrinting {
332 : m_bestTruncatedAt(0)
334 , m_truncatorWidth(0)
335 , m_forcedPageBreak(false)
338 int m_bestTruncatedAt;
340 int m_truncatorWidth;
342 bool m_forcedPageBreak;
344 LegacyPrinting m_legacyPrinting;
345 // End deprecated members.
347 int m_maximalOutlineSize; // Used to apply a fudge factor to dirty-rect checks on blocks/tables.
349 bool shouldUsePrintingLayout() const;
351 void lazyRepaintTimerFired();
353 Timer m_lazyRepaintTimer;
354 HashSet<RenderBox*> m_renderersNeedingLazyRepaint;
356 std::unique_ptr<ImageQualityController> m_imageQualityController;
357 LayoutUnit m_pageLogicalHeight;
358 bool m_pageLogicalHeightChanged;
359 std::unique_ptr<LayoutState> m_layoutState;
360 unsigned m_layoutStateDisableCount;
361 std::unique_ptr<RenderLayerCompositor> m_compositor;
362 std::unique_ptr<FlowThreadController> m_flowThreadController;
364 RenderQuote* m_renderQuoteHead;
365 unsigned m_renderCounterCount;
367 bool m_selectionWasCaret;
368 bool m_hasSoftwareFilters;
370 HashSet<RenderElement*> m_renderersWithPausedImageAnimation;
371 Vector<RefPtr<RenderWidget>> m_protectedRenderWidgets;
373 #if ENABLE(SERVICE_CONTROLS)
374 SelectionRectGatherer m_selectionRectGatherer;
376 #if ENABLE(CSS_SCROLL_SNAP)
377 HashSet<const RenderBox*> m_boxesWithScrollSnapCoordinates;
381 // Stack-based class to assist with LayoutState push/pop
382 class LayoutStateMaintainer {
383 WTF_MAKE_NONCOPYABLE(LayoutStateMaintainer);
385 // Constructor to push now.
386 explicit LayoutStateMaintainer(RenderView& view, RenderBox& root, LayoutSize offset, bool disableState = false, LayoutUnit pageHeight = 0, bool pageHeightChanged = false)
388 , m_disabled(disableState)
391 , m_didCreateLayoutState(false)
393 push(root, offset, pageHeight, pageHeightChanged);
396 // Constructor to maybe push later.
397 explicit LayoutStateMaintainer(RenderView& view)
402 , m_didCreateLayoutState(false)
406 ~LayoutStateMaintainer()
408 ASSERT(m_didStart == m_didEnd); // if this fires, it means that someone did a push(), but forgot to pop().
411 void push(RenderBox& root, LayoutSize offset, LayoutUnit pageHeight = 0, bool pageHeightChanged = false)
414 // We push state even if disabled, because we still need to store layoutDelta
415 m_didCreateLayoutState = m_view.pushLayoutState(root, offset, pageHeight, pageHeightChanged);
416 if (m_disabled && m_didCreateLayoutState)
417 m_view.disableLayoutState();
425 if (m_didCreateLayoutState) {
426 m_view.popLayoutState();
428 m_view.enableLayoutState();
435 bool didPush() const { return m_didStart; }
439 bool m_disabled : 1; // true if the offset and clip part of layoutState is disabled
440 bool m_didStart : 1; // true if we did a push or disable
441 bool m_didEnd : 1; // true if we popped or re-enabled
442 bool m_didCreateLayoutState : 1; // true if we actually made a layout state.
445 class LayoutStateDisabler {
446 WTF_MAKE_NONCOPYABLE(LayoutStateDisabler);
448 LayoutStateDisabler(RenderView* view)
452 m_view->disableLayoutState();
455 ~LayoutStateDisabler()
458 m_view->enableLayoutState();
464 } // namespace WebCore
466 SPECIALIZE_TYPE_TRAITS_RENDER_OBJECT(RenderView, isRenderView())
468 #endif // RenderView_h