Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / RenderView.h
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  * Copyright (C) 2006 Apple Computer, 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 "core/frame/FrameView.h"
26 #include "core/rendering/LayoutState.h"
27 #include "core/rendering/RenderBlockFlow.h"
28 #include "platform/PODFreeListArena.h"
29 #include "platform/scroll/ScrollableArea.h"
30 #include "wtf/OwnPtr.h"
31
32 namespace WebCore {
33
34 class FlowThreadController;
35 class RenderLayerCompositor;
36 class RenderQuote;
37
38 // The root of the render tree, corresponding to the CSS initial containing block.
39 // It's dimensions match that of the logical viewport (which may be different from
40 // the visible viewport in fixed-layout mode), and it is always at position (0,0)
41 // relative to the document (and so isn't necessarily in view).
42 class RenderView FINAL : public RenderBlockFlow {
43 public:
44     explicit RenderView(Document*);
45     virtual ~RenderView();
46
47     bool hitTest(const HitTestRequest&, HitTestResult&);
48     bool hitTest(const HitTestRequest&, const HitTestLocation&, HitTestResult&);
49
50     virtual const char* renderName() const OVERRIDE { return "RenderView"; }
51
52     virtual bool isRenderView() const OVERRIDE { return true; }
53
54     virtual LayerType layerTypeRequired() const OVERRIDE { return NormalLayer; }
55
56     virtual bool isChildAllowed(RenderObject*, RenderStyle*) const OVERRIDE;
57
58     virtual void layout() OVERRIDE;
59     virtual void updateLogicalWidth() OVERRIDE;
60     virtual void computeLogicalHeight(LayoutUnit logicalHeight, LayoutUnit logicalTop, LogicalExtentComputedValues&) const OVERRIDE;
61
62     virtual LayoutUnit availableLogicalHeight(AvailableLogicalHeightType) const OVERRIDE;
63
64     // The same as the FrameView's layoutHeight/layoutWidth but with null check guards.
65     int viewHeight(IncludeScrollbarsInRect = ExcludeScrollbars) const;
66     int viewWidth(IncludeScrollbarsInRect = ExcludeScrollbars) const;
67     int viewLogicalWidth() const
68     {
69         return style()->isHorizontalWritingMode() ? viewWidth(ExcludeScrollbars) : viewHeight(ExcludeScrollbars);
70     }
71     int viewLogicalHeight() const;
72
73     float zoomFactor() const;
74
75     FrameView* frameView() const { return m_frameView; }
76
77     virtual void computeRectForRepaint(const RenderLayerModelObject* repaintContainer, LayoutRect&, bool fixed = false) const OVERRIDE;
78     void repaintViewRectangle(const LayoutRect&) const;
79
80     void repaintViewAndCompositedLayers();
81
82     virtual void paint(PaintInfo&, const LayoutPoint&) OVERRIDE;
83     virtual void paintBoxDecorations(PaintInfo&, const LayoutPoint&) OVERRIDE;
84
85     enum SelectionRepaintMode { RepaintNewXOROld, RepaintNewMinusOld, RepaintNothing };
86     void setSelection(RenderObject* start, int startPos, RenderObject* end, int endPos, SelectionRepaintMode = RepaintNewXOROld);
87     void getSelection(RenderObject*& startRenderer, int& startOffset, RenderObject*& endRenderer, int& endOffset) const;
88     void clearSelection();
89     RenderObject* selectionStart() const { return m_selectionStart; }
90     RenderObject* selectionEnd() const { return m_selectionEnd; }
91     IntRect selectionBounds(bool clipToVisibleContent = true) const;
92     void selectionStartEnd(int& startPos, int& endPos) const;
93     void repaintSelection() const;
94
95     virtual void absoluteRects(Vector<IntRect>&, const LayoutPoint& accumulatedOffset) const OVERRIDE;
96     virtual void absoluteQuads(Vector<FloatQuad>&, bool* wasFixed) const OVERRIDE;
97
98     virtual LayoutRect viewRect() const OVERRIDE;
99
100     // layoutDelta is used transiently during layout to store how far an object has moved from its
101     // last layout location, in order to repaint correctly.
102     // If we're doing a full repaint m_layoutState will be 0, but in that case layoutDelta doesn't matter.
103     LayoutSize layoutDelta() const
104     {
105         ASSERT(!RuntimeEnabledFeatures::repaintAfterLayoutEnabled());
106         return m_layoutState ? m_layoutState->layoutDelta() : LayoutSize();
107     }
108     void addLayoutDelta(const LayoutSize& delta)
109     {
110         ASSERT(!RuntimeEnabledFeatures::repaintAfterLayoutEnabled());
111         if (m_layoutState)
112             m_layoutState->addLayoutDelta(delta);
113     }
114
115 #if !ASSERT_DISABLED
116     bool layoutDeltaMatches(const LayoutSize& delta)
117     {
118         ASSERT(!RuntimeEnabledFeatures::repaintAfterLayoutEnabled());
119         if (!m_layoutState)
120             return false;
121         return (delta.width() == m_layoutState->layoutDelta().width() || m_layoutState->layoutDeltaXSaturated()) && (delta.height() == m_layoutState->layoutDelta().height() || m_layoutState->layoutDeltaYSaturated());
122     }
123 #endif
124
125     bool doingFullRepaint() const { return m_frameView->needsFullRepaint(); }
126
127     // Subtree push
128     void pushLayoutState(RenderObject&);
129
130     void popLayoutState()
131     {
132         LayoutState* state = m_layoutState;
133         m_layoutState = state->next();
134         delete state;
135         popLayoutStateForCurrentFlowThread();
136     }
137
138     bool shouldDisableLayoutStateForSubtree(RenderObject&) const;
139
140     // Returns true if layoutState should be used for its cached offset and clip.
141     bool layoutStateEnabled() const { return m_layoutStateDisableCount == 0 && m_layoutState; }
142     LayoutState* layoutState() const { return m_layoutState; }
143
144     bool canUseLayoutStateForContainer(const RenderObject* repaintContainer) const
145     {
146         // FIXME: LayoutState should be enabled for other repaint containers than the RenderView. crbug.com/363834
147         return layoutStateEnabled() && (repaintContainer == this);
148     }
149
150     virtual void updateHitTestResult(HitTestResult&, const LayoutPoint&) OVERRIDE;
151
152     LayoutUnit pageLogicalHeight() const { return m_pageLogicalHeight; }
153     void setPageLogicalHeight(LayoutUnit height)
154     {
155         if (m_pageLogicalHeight != height) {
156             m_pageLogicalHeight = height;
157             m_pageLogicalHeightChanged = true;
158         }
159     }
160     bool pageLogicalHeightChanged() const { return m_pageLogicalHeightChanged; }
161
162     // Notification that this view moved into or out of a native window.
163     void setIsInWindow(bool);
164
165     RenderLayerCompositor* compositor();
166     bool usesCompositing() const;
167
168     IntRect unscaledDocumentRect() const;
169     LayoutRect backgroundRect(RenderBox* backgroundRenderer) const;
170
171     IntRect documentRect() const;
172
173     // Renderer that paints the root background has background-images which all have background-attachment: fixed.
174     bool rootBackgroundIsEntirelyFixed() const;
175
176     FlowThreadController* flowThreadController();
177
178     IntervalArena* intervalArena();
179
180     void setRenderQuoteHead(RenderQuote* head) { m_renderQuoteHead = head; }
181     RenderQuote* renderQuoteHead() const { return m_renderQuoteHead; }
182
183     // FIXME: This is a work around because the current implementation of counters
184     // requires walking the entire tree repeatedly and most pages don't actually use either
185     // feature so we shouldn't take the performance hit when not needed. Long term we should
186     // rewrite the counter and quotes code.
187     void addRenderCounter() { m_renderCounterCount++; }
188     void removeRenderCounter() { ASSERT(m_renderCounterCount > 0); m_renderCounterCount--; }
189     bool hasRenderCounters() { return m_renderCounterCount; }
190
191     virtual bool backgroundIsKnownToBeOpaqueInRect(const LayoutRect& localRect) const OVERRIDE;
192
193     double layoutViewportWidth() const;
194     double layoutViewportHeight() const;
195
196     // Suspends the LayoutState optimization. Used under transforms that cannot be represented by
197     // LayoutState (common in SVG) and when manipulating the render tree during layout in ways
198     // that can trigger repaint of a non-child (e.g. when a list item moves its list marker around).
199     // Note that even when disabled, LayoutState is still used to store layoutDelta.
200     // These functions may only be accessed by LayoutStateMaintainer or LayoutStateDisabler.
201     void disableLayoutState() { m_layoutStateDisableCount++; }
202     void enableLayoutState() { ASSERT(m_layoutStateDisableCount > 0); m_layoutStateDisableCount--; }
203
204 private:
205     virtual void mapLocalToContainer(const RenderLayerModelObject* repaintContainer, TransformState&, MapCoordinatesFlags = ApplyContainerFlip, bool* wasFixed = 0) const OVERRIDE;
206     virtual const RenderObject* pushMappingToContainer(const RenderLayerModelObject* ancestorToStopAt, RenderGeometryMap&) const OVERRIDE;
207     virtual void mapAbsoluteToLocalPoint(MapCoordinatesFlags, TransformState&) const OVERRIDE;
208     virtual void computeSelfHitTestRects(Vector<LayoutRect>&, const LayoutPoint& layerOffset) const OVERRIDE;
209
210     bool shouldRepaint(const LayoutRect&) const;
211
212     bool rootFillsViewportBackground(RenderBox* rootBox) const;
213
214     // These functions may only be accessed by LayoutStateMaintainer.
215     bool pushLayoutState(RenderBox& renderer, const LayoutSize& offset, LayoutUnit pageHeight = 0, bool pageHeightChanged = false, ColumnInfo* colInfo = 0)
216     {
217         // We push LayoutState even if layoutState is disabled because it stores layoutDelta too.
218         if ((!doingFullRepaint() || RuntimeEnabledFeatures::repaintAfterLayoutEnabled()) || m_layoutState->isPaginated() || renderer.hasColumns() || renderer.flowThreadContainingBlock()
219             ) {
220             pushLayoutStateForCurrentFlowThread(renderer);
221             m_layoutState = new LayoutState(m_layoutState, renderer, offset, pageHeight, pageHeightChanged, colInfo);
222             return true;
223         }
224         return false;
225     }
226
227     void layoutContent();
228 #ifndef NDEBUG
229     void checkLayoutState();
230 #endif
231
232     void positionDialog(RenderBox*);
233     void positionDialogs();
234
235     void pushLayoutStateForCurrentFlowThread(const RenderObject&);
236     void popLayoutStateForCurrentFlowThread();
237
238     friend class LayoutStateMaintainer;
239     friend class LayoutStateDisabler;
240     friend class RootLayoutStateScope;
241
242     bool shouldUsePrintingLayout() const;
243
244     FrameView* m_frameView;
245
246     RenderObject* m_selectionStart;
247     RenderObject* m_selectionEnd;
248
249     int m_selectionStartPos;
250     int m_selectionEndPos;
251
252     LayoutUnit m_pageLogicalHeight;
253     bool m_pageLogicalHeightChanged;
254     LayoutState* m_layoutState;
255     unsigned m_layoutStateDisableCount;
256     OwnPtr<RenderLayerCompositor> m_compositor;
257     OwnPtr<FlowThreadController> m_flowThreadController;
258     RefPtr<IntervalArena> m_intervalArena;
259
260     RenderQuote* m_renderQuoteHead;
261     unsigned m_renderCounterCount;
262 };
263
264 DEFINE_RENDER_OBJECT_TYPE_CASTS(RenderView, isRenderView());
265
266 class RootLayoutStateScope {
267 public:
268     explicit RootLayoutStateScope(RenderObject& obj)
269         : m_view(*obj.view())
270         , m_rootLayoutState(m_view.pageLogicalHeight(), m_view.pageLogicalHeightChanged())
271         , m_isRenderView(obj.isRenderView())
272     {
273         // If the invalidation root isn't the renderView we have to make sure it
274         // gets added correctly to LayoutState otherwise we'll lose the margins that
275         // are set in the ancestors of the roots.
276         if (m_isRenderView) {
277             ASSERT(!m_view.m_layoutState);
278             m_view.m_layoutState = &m_rootLayoutState;
279         } else {
280             m_view.pushLayoutState(obj);
281         }
282     }
283
284     ~RootLayoutStateScope()
285     {
286         if (m_isRenderView) {
287             ASSERT(m_view.m_layoutState == &m_rootLayoutState);
288             m_view.m_layoutState = 0;
289         } else {
290             m_view.popLayoutState();
291         }
292     }
293 private:
294     RenderView& m_view;
295     LayoutState m_rootLayoutState;
296     bool m_isRenderView;
297 };
298
299 // Stack-based class to assist with LayoutState push/pop
300 class LayoutStateMaintainer {
301     WTF_MAKE_NONCOPYABLE(LayoutStateMaintainer);
302 public:
303     // ctor to push now
304     explicit LayoutStateMaintainer(RenderBox& root, const LayoutSize& offset, LayoutUnit pageHeight = 0, bool pageHeightChanged = false, ColumnInfo* colInfo = 0)
305         : m_view(*root.view())
306         , m_disabled(root.shouldDisableLayoutState())
307         , m_didStart(false)
308         , m_didEnd(false)
309         , m_didCreateLayoutState(false)
310     {
311         push(root, offset, pageHeight, pageHeightChanged, colInfo);
312     }
313
314     // ctor to maybe push later
315     explicit LayoutStateMaintainer(RenderBox& root)
316         : m_view(*root.view())
317         , m_disabled(false)
318         , m_didStart(false)
319         , m_didEnd(false)
320         , m_didCreateLayoutState(false)
321     {
322     }
323
324     ~LayoutStateMaintainer()
325     {
326         if (m_didStart)
327             pop();
328         ASSERT(m_didStart == m_didEnd);   // if this fires, it means that someone did a push(), but forgot to pop().
329     }
330
331     void push(RenderBox& root, const LayoutSize& offset, LayoutUnit pageHeight = 0, bool pageHeightChanged = false, ColumnInfo* colInfo = 0)
332     {
333         ASSERT(!m_didStart);
334         // We push state even if disabled, because we still need to store layoutDelta
335         m_didCreateLayoutState = m_view.pushLayoutState(root, offset, pageHeight, pageHeightChanged, colInfo);
336         if (m_disabled && m_didCreateLayoutState)
337             m_view.disableLayoutState();
338         m_didStart = true;
339     }
340
341     bool didPush() const { return m_didStart; }
342
343 private:
344     void pop()
345     {
346         ASSERT(m_didStart && !m_didEnd);
347         if (m_didCreateLayoutState) {
348             m_view.popLayoutState();
349             if (m_disabled)
350                 m_view.enableLayoutState();
351         }
352
353         m_didEnd = true;
354     }
355
356     RenderView& m_view;
357     bool m_disabled : 1;        // true if the offset and clip part of layoutState is disabled
358     bool m_didStart : 1;        // true if we did a push or disable
359     bool m_didEnd : 1;          // true if we popped or re-enabled
360     bool m_didCreateLayoutState : 1; // true if we actually made a layout state.
361 };
362
363 class LayoutStateDisabler {
364     WTF_MAKE_NONCOPYABLE(LayoutStateDisabler);
365 public:
366     LayoutStateDisabler(const RenderObject& root)
367         : m_view(*root.view())
368     {
369         m_view.disableLayoutState();
370     }
371
372     ~LayoutStateDisabler()
373     {
374         m_view.enableLayoutState();
375     }
376 private:
377     RenderView& m_view;
378 };
379
380 } // namespace WebCore
381
382 #endif // RenderView_h