Migrate from ints to unsigneds when referring to indices into strings
[WebKit-https.git] / Source / WebCore / rendering / RenderView.h
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  * Copyright (C) 2006, 2015 Apple Inc.
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
22 #ifndef RenderView_h
23 #define RenderView_h
24
25 #include "FrameView.h"
26 #include "LayoutState.h"
27 #include "Region.h"
28 #include "RenderBlockFlow.h"
29 #include "RenderWidget.h"
30 #include "SelectionSubtreeRoot.h"
31 #include <memory>
32 #include <wtf/HashSet.h>
33
34 #if ENABLE(SERVICE_CONTROLS)
35 #include "SelectionRectGatherer.h"
36 #endif
37
38 namespace WebCore {
39
40 class FlowThreadController;
41 class ImageQualityController;
42 class RenderLayerCompositor;
43 class RenderQuote;
44
45 class RenderView final : public RenderBlockFlow, public SelectionSubtreeRoot {
46 public:
47     RenderView(Document&, RenderStyle&&);
48     virtual ~RenderView();
49
50     WEBCORE_EXPORT bool hitTest(const HitTestRequest&, HitTestResult&);
51     bool hitTest(const HitTestRequest&, const HitTestLocation&, HitTestResult&);
52
53     const char* renderName() const override { return "RenderView"; }
54
55     bool requiresLayer() const override { return true; }
56
57     bool isChildAllowed(const RenderObject&, const RenderStyle&) const override;
58
59     void layout() override;
60     void updateLogicalWidth() override;
61     void computeLogicalHeight(LayoutUnit logicalHeight, LayoutUnit logicalTop, LogicalExtentComputedValues&) const override;
62
63     LayoutUnit availableLogicalHeight(AvailableLogicalHeightType) const override;
64
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;
70
71     LayoutUnit clientLogicalWidthForFixedPosition() const;
72     LayoutUnit clientLogicalHeightForFixedPosition() const;
73
74     float zoomFactor() const;
75
76     FrameView& frameView() const { return m_frameView; }
77
78     LayoutRect visualOverflowRect() const override;
79     LayoutRect computeRectForRepaint(const LayoutRect&, const RenderLayerModelObject* repaintContainer, RepaintContext = { }) const override;
80     void repaintRootContents();
81     void repaintViewRectangle(const LayoutRect&) const;
82     void repaintViewAndCompositedLayers();
83
84     void paint(PaintInfo&, const LayoutPoint&) override;
85     void paintBoxDecorations(PaintInfo&, const LayoutPoint&) override;
86
87     enum SelectionRepaintMode { RepaintNewXOROld, RepaintNewMinusOld, RepaintNothing };
88     void setSelection(RenderObject* start, Optional<unsigned> startPos, RenderObject* endObject, Optional<unsigned> endPos, SelectionRepaintMode = RepaintNewXOROld);
89     void getSelection(RenderObject*& startRenderer, Optional<unsigned>& startOffset, RenderObject*& endRenderer, Optional<unsigned>& 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;
95
96     bool printing() const;
97
98     void absoluteRects(Vector<IntRect>&, const LayoutPoint& accumulatedOffset) const override;
99     void absoluteQuads(Vector<FloatQuad>&, bool* wasFixed) const override;
100
101     LayoutRect viewRect() const;
102
103     // layoutDelta is used transiently during layout to store how far an object has moved from its
104     // last layout location, in order to repaint correctly.
105     // If we're doing a full repaint m_layoutState will be 0, but in that case layoutDelta doesn't matter.
106     LayoutSize layoutDelta() const
107     {
108         return m_layoutState ? m_layoutState->m_layoutDelta : LayoutSize();
109     }
110     void addLayoutDelta(const LayoutSize& delta) 
111     {
112         if (m_layoutState) {
113             m_layoutState->m_layoutDelta += delta;
114 #if !ASSERT_DISABLED
115             m_layoutState->m_layoutDeltaXSaturated |= m_layoutState->m_layoutDelta.width() == LayoutUnit::max() || m_layoutState->m_layoutDelta.width() == LayoutUnit::min();
116             m_layoutState->m_layoutDeltaYSaturated |= m_layoutState->m_layoutDelta.height() == LayoutUnit::max() || m_layoutState->m_layoutDelta.height() == LayoutUnit::min();
117 #endif
118         }
119     }
120
121 #if !ASSERT_DISABLED
122     bool layoutDeltaMatches(const LayoutSize& delta)
123     {
124         if (!m_layoutState)
125             return false;
126         return (delta.width() == m_layoutState->m_layoutDelta.width() || m_layoutState->m_layoutDeltaXSaturated) && (delta.height() == m_layoutState->m_layoutDelta.height() || m_layoutState->m_layoutDeltaYSaturated);
127     }
128 #endif
129
130     bool doingFullRepaint() const { return frameView().needsFullRepaint(); }
131
132     // Subtree push/pop
133     void pushLayoutState(RenderObject&);
134     void popLayoutState(RenderObject&) { return popLayoutState(); } // Just doing this to keep popLayoutState() private and to make the subtree calls symmetrical.
135
136     // Returns true if layoutState should be used for its cached offset and clip.
137     bool layoutStateEnabled() const { return m_layoutStateDisableCount == 0 && m_layoutState; }
138     LayoutState* layoutState() const { return m_layoutState.get(); }
139
140     void updateHitTestResult(HitTestResult&, const LayoutPoint&) override;
141
142     LayoutUnit pageLogicalHeight() const { return m_pageLogicalHeight; }
143     void setPageLogicalHeight(LayoutUnit height)
144     {
145         if (m_pageLogicalHeight != height) {
146             m_pageLogicalHeight = height;
147             m_pageLogicalHeightChanged = true;
148         }
149     }
150     LayoutUnit pageOrViewLogicalHeight() const;
151
152     // This method is used to assign a page number only when pagination modes have
153     // a block progression. This happens with vertical-rl books for example, but it
154     // doesn't happen for normal horizontal-tb books. This is a very specialized
155     // function and should not be mistaken for a general page number API.
156     unsigned pageNumberForBlockProgressionOffset(int offset) const;
157
158     unsigned pageCount() const;
159
160     // FIXME: These functions are deprecated. No code should be added that uses these.
161     int bestTruncatedAt() const { return m_legacyPrinting.m_bestTruncatedAt; }
162     void setBestTruncatedAt(int y, RenderBoxModelObject* forRenderer, bool forcedBreak = false);
163     int truncatedAt() const { return m_legacyPrinting.m_truncatedAt; }
164     void setTruncatedAt(int y)
165     { 
166         m_legacyPrinting.m_truncatedAt = y;
167         m_legacyPrinting.m_bestTruncatedAt = 0;
168         m_legacyPrinting.m_truncatorWidth = 0;
169         m_legacyPrinting.m_forcedPageBreak = false;
170     }
171     const IntRect& printRect() const { return m_legacyPrinting.m_printRect; }
172     void setPrintRect(const IntRect& r) { m_legacyPrinting.m_printRect = r; }
173     // End deprecated functions.
174
175     // Notification that this view moved into or out of a native window.
176     void setIsInWindow(bool);
177
178     WEBCORE_EXPORT RenderLayerCompositor& compositor();
179     WEBCORE_EXPORT bool usesCompositing() const;
180
181     bool usesFirstLineRules() const { return m_usesFirstLineRules; }
182     bool usesFirstLetterRules() const { return m_usesFirstLetterRules; }
183     void setUsesFirstLineRules(bool value) { m_usesFirstLineRules = value; }
184     void setUsesFirstLetterRules(bool value) { m_usesFirstLetterRules = value; }
185
186     WEBCORE_EXPORT IntRect unscaledDocumentRect() const;
187     LayoutRect unextendedBackgroundRect() const;
188     LayoutRect backgroundRect() const;
189
190     WEBCORE_EXPORT IntRect documentRect() const;
191
192     // Renderer that paints the root background has background-images which all have background-attachment: fixed.
193     bool rootBackgroundIsEntirelyFixed() const;
194     
195     bool hasRenderNamedFlowThreads() const;
196     bool checkTwoPassLayoutForAutoHeightRegions() const;
197     FlowThreadController& flowThreadController();
198
199     void styleDidChange(StyleDifference, const RenderStyle* oldStyle) override;
200
201     IntSize viewportSizeForCSSViewportUnits() const;
202
203     void setRenderQuoteHead(RenderQuote* head) { m_renderQuoteHead = head; }
204     RenderQuote* renderQuoteHead() const { return m_renderQuoteHead; }
205
206     // FIXME: This is a work around because the current implementation of counters
207     // requires walking the entire tree repeatedly and most pages don't actually use either
208     // feature so we shouldn't take the performance hit when not needed. Long term we should
209     // rewrite the counter and quotes code.
210     void addRenderCounter() { m_renderCounterCount++; }
211     void removeRenderCounter() { ASSERT(m_renderCounterCount > 0); m_renderCounterCount--; }
212     bool hasRenderCounters() { return m_renderCounterCount; }
213     
214     ImageQualityController& imageQualityController();
215
216     void setHasSoftwareFilters(bool hasSoftwareFilters) { m_hasSoftwareFilters = hasSoftwareFilters; }
217     bool hasSoftwareFilters() const { return m_hasSoftwareFilters; }
218
219     uint64_t rendererCount() const { return m_rendererCount; }
220     void didCreateRenderer() { ++m_rendererCount; }
221     void didDestroyRenderer() { --m_rendererCount; }
222
223     void updateVisibleViewportRect(const IntRect&);
224     void registerForVisibleInViewportCallback(RenderElement&);
225     void unregisterForVisibleInViewportCallback(RenderElement&);
226     void resumePausedImageAnimationsIfNeeded(IntRect visibleRect);
227     void addRendererWithPausedImageAnimations(RenderElement&);
228     void removeRendererWithPausedImageAnimations(RenderElement&);
229
230     class RepaintRegionAccumulator {
231         WTF_MAKE_NONCOPYABLE(RepaintRegionAccumulator);
232     public:
233         RepaintRegionAccumulator(RenderView*);
234         ~RepaintRegionAccumulator();
235
236     private:
237         RenderView* m_rootView;
238         bool m_wasAccumulatingRepaintRegion;
239     };
240
241     void scheduleLazyRepaint(RenderBox&);
242     void unscheduleLazyRepaint(RenderBox&);
243
244     void protectRenderWidgetUntilLayoutIsDone(RenderWidget& widget) { m_protectedRenderWidgets.append(&widget); }
245     void releaseProtectedRenderWidgets() { m_protectedRenderWidgets.clear(); }
246
247 #if ENABLE(CSS_SCROLL_SNAP)
248     void registerBoxWithScrollSnapCoordinates(const RenderBox&);
249     void unregisterBoxWithScrollSnapCoordinates(const RenderBox&);
250     const HashSet<const RenderBox*>& boxesWithScrollSnapCoordinates() { return m_boxesWithScrollSnapCoordinates; }
251 #endif
252
253 protected:
254     void mapLocalToContainer(const RenderLayerModelObject* repaintContainer, TransformState&, MapCoordinatesFlags, bool* wasFixed) const override;
255     const RenderObject* pushMappingToContainer(const RenderLayerModelObject* ancestorToStopAt, RenderGeometryMap&) const override;
256     void mapAbsoluteToLocalPoint(MapCoordinatesFlags, TransformState&) const override;
257     bool requiresColumns(int desiredColumnCount) const override;
258
259 private:
260     void initializeLayoutState(LayoutState&);
261
262     void computeColumnCountAndWidth() override;
263
264     bool shouldRepaint(const LayoutRect&) const;
265     void flushAccumulatedRepaintRegion() const;
266
267     // These functions may only be accessed by LayoutStateMaintainer.
268     bool pushLayoutState(RenderBox& renderer, const LayoutSize& offset, LayoutUnit pageHeight = 0, bool pageHeightChanged = false)
269     {
270         // We push LayoutState even if layoutState is disabled because it stores layoutDelta too.
271         if (!doingFullRepaint() || m_layoutState->isPaginated() || renderer.flowThreadContainingBlock()
272             || m_layoutState->lineGrid() || (renderer.style().lineGrid() != RenderStyle::initialLineGrid() && renderer.isRenderBlockFlow())) {
273             m_layoutState = std::make_unique<LayoutState>(WTFMove(m_layoutState), &renderer, offset, pageHeight, pageHeightChanged);
274             pushLayoutStateForCurrentFlowThread(renderer);
275             return true;
276         }
277         return false;
278     }
279
280     void popLayoutState()
281     {
282         popLayoutStateForCurrentFlowThread();
283         m_layoutState = WTFMove(m_layoutState->m_next);
284     }
285
286     // Suspends the LayoutState optimization. Used under transforms that cannot be represented by
287     // LayoutState (common in SVG) and when manipulating the render tree during layout in ways
288     // that can trigger repaint of a non-child (e.g. when a list item moves its list marker around).
289     // Note that even when disabled, LayoutState is still used to store layoutDelta.
290     // These functions may only be accessed by LayoutStateMaintainer or LayoutStateDisabler.
291     void disableLayoutState() { m_layoutStateDisableCount++; }
292     void enableLayoutState() { ASSERT(m_layoutStateDisableCount > 0); m_layoutStateDisableCount--; }
293
294     void layoutContent(const LayoutState&);
295     void layoutContentInAutoLogicalHeightRegions(const LayoutState&);
296     void layoutContentToComputeOverflowInRegions(const LayoutState&);
297 #ifndef NDEBUG
298     void checkLayoutState(const LayoutState&);
299 #endif
300
301     void pushLayoutStateForCurrentFlowThread(const RenderObject&);
302     void popLayoutStateForCurrentFlowThread();
303
304     friend class LayoutStateMaintainer;
305     friend class LayoutStateDisabler;
306     friend class SubtreeLayoutStateMaintainer;
307
308     bool isScrollableOrRubberbandableBox() const override;
309
310     void splitSelectionBetweenSubtrees(const RenderObject* startRenderer, Optional<unsigned> startPos, const RenderObject* endRenderer, Optional<unsigned> 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;
316
317 private:
318     FrameView& m_frameView;
319
320     RenderObject* m_selectionUnsplitStart { nullptr };
321     RenderObject* m_selectionUnsplitEnd { nullptr };
322     Optional<unsigned> m_selectionUnsplitStartPos;
323     Optional<unsigned> m_selectionUnsplitEndPos;
324
325     // Include this RenderView.
326     uint64_t m_rendererCount { 1 };
327
328     mutable std::unique_ptr<Region> m_accumulatedRepaintRegion;
329
330     // FIXME: Only used by embedded WebViews inside AppKit NSViews.  Find a way to remove.
331     struct LegacyPrinting {
332         int m_bestTruncatedAt { 0 };
333         int m_truncatedAt { 0 };
334         int m_truncatorWidth { 0 };
335         IntRect m_printRect;
336         bool m_forcedPageBreak { false };
337     };
338     LegacyPrinting m_legacyPrinting;
339     // End deprecated members.
340
341     bool shouldUsePrintingLayout() const;
342
343     void lazyRepaintTimerFired();
344
345     Timer m_lazyRepaintTimer;
346     HashSet<RenderBox*> m_renderersNeedingLazyRepaint;
347
348     std::unique_ptr<ImageQualityController> m_imageQualityController;
349     LayoutUnit m_pageLogicalHeight;
350     bool m_pageLogicalHeightChanged { false };
351     std::unique_ptr<LayoutState> m_layoutState;
352     unsigned m_layoutStateDisableCount { 0 };
353     std::unique_ptr<RenderLayerCompositor> m_compositor;
354     std::unique_ptr<FlowThreadController> m_flowThreadController;
355
356     RenderQuote* m_renderQuoteHead { nullptr };
357     unsigned m_renderCounterCount { 0 };
358
359     bool m_selectionWasCaret { false };
360     bool m_hasSoftwareFilters { false };
361     bool m_usesFirstLineRules { false };
362     bool m_usesFirstLetterRules { false };
363
364     HashSet<RenderElement*> m_renderersWithPausedImageAnimation;
365     HashSet<RenderElement*> m_visibleInViewportRenderers;
366     Vector<RefPtr<RenderWidget>> m_protectedRenderWidgets;
367
368 #if ENABLE(SERVICE_CONTROLS)
369     SelectionRectGatherer m_selectionRectGatherer;
370 #endif
371 #if ENABLE(CSS_SCROLL_SNAP)
372     HashSet<const RenderBox*> m_boxesWithScrollSnapCoordinates;
373 #endif
374 };
375
376 // Stack-based class to assist with LayoutState push/pop
377 class LayoutStateMaintainer {
378     WTF_MAKE_NONCOPYABLE(LayoutStateMaintainer);
379 public:
380     // Constructor to push now.
381     explicit LayoutStateMaintainer(RenderView& view, RenderBox& root, LayoutSize offset, bool disableState = false, LayoutUnit pageHeight = 0, bool pageHeightChanged = false)
382         : m_view(view)
383         , m_disabled(disableState)
384     {
385         push(root, offset, pageHeight, pageHeightChanged);
386     }
387
388     // Constructor to maybe push later.
389     explicit LayoutStateMaintainer(RenderView& view)
390         : m_view(view)
391     {
392     }
393
394     ~LayoutStateMaintainer()
395     {
396         ASSERT(!m_didCallPush || m_didCallPush == m_didCallPop);
397     }
398
399     void push(RenderBox& root, LayoutSize offset, LayoutUnit pageHeight = 0, bool pageHeightChanged = false)
400     {
401         ASSERT(!m_didCallPush);
402         m_didCallPush = true;
403         // We push state even if disabled, because we still need to store layoutDelta
404         m_didPushLayoutState = m_view.pushLayoutState(root, offset, pageHeight, pageHeightChanged);
405         if (!m_didPushLayoutState)
406             return;
407         if (m_disabled)
408             m_view.disableLayoutState();
409     }
410
411     void pop()
412     {
413         ASSERT(!m_didCallPop);
414         m_didCallPop = true;
415         if (!m_didCallPush)
416             return;
417         if (!m_didPushLayoutState)
418             return;
419         m_view.popLayoutState();
420         if (m_disabled)
421             m_view.enableLayoutState();
422     }
423
424     bool didPush() const { return m_didCallPush; }
425
426 private:
427     RenderView& m_view;
428     bool m_disabled { false };
429     bool m_didCallPush { false };
430     bool m_didCallPop { false };
431     bool m_didPushLayoutState { false };
432 };
433
434 class LayoutStateDisabler {
435     WTF_MAKE_NONCOPYABLE(LayoutStateDisabler);
436 public:
437     LayoutStateDisabler(RenderView& view)
438         : m_view(view)
439     {
440         m_view.disableLayoutState();
441     }
442
443     ~LayoutStateDisabler()
444     {
445         m_view.enableLayoutState();
446     }
447
448 private:
449     RenderView& m_view;
450 };
451
452 } // namespace WebCore
453
454 SPECIALIZE_TYPE_TRAITS_RENDER_OBJECT(RenderView, isRenderView())
455
456 #endif // RenderView_h