b103270ba5ed06b4524dfa604337ad938da79fd0
[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     virtual void updateHitTestResult(HitTestResult&, const LayoutPoint&) OVERRIDE;
145
146     LayoutUnit pageLogicalHeight() const { return m_pageLogicalHeight; }
147     void setPageLogicalHeight(LayoutUnit height)
148     {
149         if (m_pageLogicalHeight != height) {
150             m_pageLogicalHeight = height;
151             m_pageLogicalHeightChanged = true;
152         }
153     }
154     bool pageLogicalHeightChanged() const { return m_pageLogicalHeightChanged; }
155
156     // Notification that this view moved into or out of a native window.
157     void setIsInWindow(bool);
158
159     RenderLayerCompositor* compositor();
160     bool usesCompositing() const;
161
162     IntRect unscaledDocumentRect() const;
163     LayoutRect backgroundRect(RenderBox* backgroundRenderer) const;
164
165     IntRect documentRect() const;
166
167     // Renderer that paints the root background has background-images which all have background-attachment: fixed.
168     bool rootBackgroundIsEntirelyFixed() const;
169
170     FlowThreadController* flowThreadController();
171
172     IntervalArena* intervalArena();
173
174     void setRenderQuoteHead(RenderQuote* head) { m_renderQuoteHead = head; }
175     RenderQuote* renderQuoteHead() const { return m_renderQuoteHead; }
176
177     // FIXME: This is a work around because the current implementation of counters
178     // requires walking the entire tree repeatedly and most pages don't actually use either
179     // feature so we shouldn't take the performance hit when not needed. Long term we should
180     // rewrite the counter and quotes code.
181     void addRenderCounter() { m_renderCounterCount++; }
182     void removeRenderCounter() { ASSERT(m_renderCounterCount > 0); m_renderCounterCount--; }
183     bool hasRenderCounters() { return m_renderCounterCount; }
184
185     virtual bool backgroundIsKnownToBeOpaqueInRect(const LayoutRect& localRect) const OVERRIDE;
186
187     double layoutViewportWidth() const;
188     double layoutViewportHeight() const;
189
190     // Suspends the LayoutState optimization. Used under transforms that cannot be represented by
191     // LayoutState (common in SVG) and when manipulating the render tree during layout in ways
192     // that can trigger repaint of a non-child (e.g. when a list item moves its list marker around).
193     // Note that even when disabled, LayoutState is still used to store layoutDelta.
194     // These functions may only be accessed by LayoutStateMaintainer or LayoutStateDisabler.
195     void disableLayoutState() { m_layoutStateDisableCount++; }
196     void enableLayoutState() { ASSERT(m_layoutStateDisableCount > 0); m_layoutStateDisableCount--; }
197
198 private:
199     virtual void mapLocalToContainer(const RenderLayerModelObject* repaintContainer, TransformState&, MapCoordinatesFlags = ApplyContainerFlip, bool* wasFixed = 0) const OVERRIDE;
200     virtual const RenderObject* pushMappingToContainer(const RenderLayerModelObject* ancestorToStopAt, RenderGeometryMap&) const OVERRIDE;
201     virtual void mapAbsoluteToLocalPoint(MapCoordinatesFlags, TransformState&) const OVERRIDE;
202     virtual void computeSelfHitTestRects(Vector<LayoutRect>&, const LayoutPoint& layerOffset) const OVERRIDE;
203
204     bool shouldRepaint(const LayoutRect&) const;
205
206     bool rootFillsViewportBackground(RenderBox* rootBox) const;
207
208     // These functions may only be accessed by LayoutStateMaintainer.
209     bool pushLayoutState(RenderBox& renderer, const LayoutSize& offset, LayoutUnit pageHeight = 0, bool pageHeightChanged = false, ColumnInfo* colInfo = 0)
210     {
211         // We push LayoutState even if layoutState is disabled because it stores layoutDelta too.
212         if (!doingFullRepaint() || m_layoutState->isPaginated() || renderer.hasColumns() || renderer.flowThreadContainingBlock()
213             ) {
214             pushLayoutStateForCurrentFlowThread(renderer);
215             m_layoutState = new LayoutState(m_layoutState, renderer, offset, pageHeight, pageHeightChanged, colInfo);
216             return true;
217         }
218         return false;
219     }
220
221     void layoutContent();
222 #ifndef NDEBUG
223     void checkLayoutState();
224 #endif
225
226     void positionDialog(RenderBox*);
227     void positionDialogs();
228
229     void pushLayoutStateForCurrentFlowThread(const RenderObject&);
230     void popLayoutStateForCurrentFlowThread();
231
232     friend class LayoutStateMaintainer;
233     friend class LayoutStateDisabler;
234     friend class RootLayoutStateScope;
235
236     bool shouldUsePrintingLayout() const;
237
238     FrameView* m_frameView;
239
240     RenderObject* m_selectionStart;
241     RenderObject* m_selectionEnd;
242
243     int m_selectionStartPos;
244     int m_selectionEndPos;
245
246     LayoutUnit m_pageLogicalHeight;
247     bool m_pageLogicalHeightChanged;
248     LayoutState* m_layoutState;
249     unsigned m_layoutStateDisableCount;
250     OwnPtr<RenderLayerCompositor> m_compositor;
251     OwnPtr<FlowThreadController> m_flowThreadController;
252     RefPtr<IntervalArena> m_intervalArena;
253
254     RenderQuote* m_renderQuoteHead;
255     unsigned m_renderCounterCount;
256 };
257
258 DEFINE_RENDER_OBJECT_TYPE_CASTS(RenderView, isRenderView());
259
260 class RootLayoutStateScope {
261 public:
262     explicit RootLayoutStateScope(RenderView& view)
263         : m_view(view)
264         , m_rootLayoutState(view.pageLogicalHeight(), view.pageLogicalHeightChanged())
265     {
266         ASSERT(!m_view.m_layoutState);
267         m_view.m_layoutState = &m_rootLayoutState;
268     }
269
270     ~RootLayoutStateScope()
271     {
272         ASSERT(m_view.m_layoutState == &m_rootLayoutState);
273         m_view.m_layoutState = 0;
274     }
275 private:
276     RenderView& m_view;
277     LayoutState m_rootLayoutState;
278 };
279
280 // Stack-based class to assist with LayoutState push/pop
281 class LayoutStateMaintainer {
282     WTF_MAKE_NONCOPYABLE(LayoutStateMaintainer);
283 public:
284     // ctor to push now
285     explicit LayoutStateMaintainer(RenderBox& root, const LayoutSize& offset, LayoutUnit pageHeight = 0, bool pageHeightChanged = false, ColumnInfo* colInfo = 0)
286         : m_view(*root.view())
287         , m_disabled(root.shouldDisableLayoutState())
288         , m_didStart(false)
289         , m_didEnd(false)
290         , m_didCreateLayoutState(false)
291     {
292         push(root, offset, pageHeight, pageHeightChanged, colInfo);
293     }
294
295     // ctor to maybe push later
296     explicit LayoutStateMaintainer(RenderBox& root)
297         : m_view(*root.view())
298         , m_disabled(false)
299         , m_didStart(false)
300         , m_didEnd(false)
301         , m_didCreateLayoutState(false)
302     {
303     }
304
305     ~LayoutStateMaintainer()
306     {
307         if (m_didStart)
308             pop();
309         ASSERT(m_didStart == m_didEnd);   // if this fires, it means that someone did a push(), but forgot to pop().
310     }
311
312     void push(RenderBox& root, const LayoutSize& offset, LayoutUnit pageHeight = 0, bool pageHeightChanged = false, ColumnInfo* colInfo = 0)
313     {
314         ASSERT(!m_didStart);
315         // We push state even if disabled, because we still need to store layoutDelta
316         m_didCreateLayoutState = m_view.pushLayoutState(root, offset, pageHeight, pageHeightChanged, colInfo);
317         if (m_disabled && m_didCreateLayoutState)
318             m_view.disableLayoutState();
319         m_didStart = true;
320     }
321
322     bool didPush() const { return m_didStart; }
323
324 private:
325     void pop()
326     {
327         ASSERT(m_didStart && !m_didEnd);
328         if (m_didCreateLayoutState) {
329             m_view.popLayoutState();
330             if (m_disabled)
331                 m_view.enableLayoutState();
332         }
333
334         m_didEnd = true;
335     }
336
337     RenderView& m_view;
338     bool m_disabled : 1;        // true if the offset and clip part of layoutState is disabled
339     bool m_didStart : 1;        // true if we did a push or disable
340     bool m_didEnd : 1;          // true if we popped or re-enabled
341     bool m_didCreateLayoutState : 1; // true if we actually made a layout state.
342 };
343
344 class LayoutStateDisabler {
345     WTF_MAKE_NONCOPYABLE(LayoutStateDisabler);
346 public:
347     LayoutStateDisabler(const RenderBox& root)
348         : m_view(*root.view())
349     {
350         m_view.disableLayoutState();
351     }
352
353     ~LayoutStateDisabler()
354     {
355         m_view.enableLayoutState();
356     }
357 private:
358     RenderView& m_view;
359 };
360
361 } // namespace WebCore
362
363 #endif // RenderView_h