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 "FrameView.h"
26 #include "LayoutState.h"
27 #include "RenderBlock.h"
28 #include <wtf/OwnPtr.h>
29 
30 namespace WebCore {
31 
32 class RenderWidget;
33 
34 #if USE(ACCELERATED_COMPOSITING)
35 class RenderLayerCompositor;
36 #endif
37 
38 class RenderView : public RenderBlock {
39 public:
40     RenderView(Node*, FrameView*);
41     virtual ~RenderView();
42 
renderName()43     virtual const char* renderName() const { return "RenderView"; }
44 
isRenderView()45     virtual bool isRenderView() const { return true; }
46 
requiresLayer()47     virtual bool requiresLayer() const { return true; }
48 
49     virtual bool isChildAllowed(RenderObject*, RenderStyle*) const;
50 
51     virtual void layout();
52     virtual void computeLogicalWidth();
53     virtual void computeLogicalHeight();
54     virtual void computePreferredLogicalWidths();
55 
56     // The same as the FrameView's layoutHeight/layoutWidth but with null check guards.
57     int viewHeight() const;
58     int viewWidth() const;
viewLogicalWidth()59     int viewLogicalWidth() const { return style()->isHorizontalWritingMode() ? viewWidth() : viewHeight(); }
viewLogicalHeight()60     int viewLogicalHeight() const { return style()->isHorizontalWritingMode() ? viewHeight() : viewWidth(); }
61 
62     float zoomFactor() const;
63 
frameView()64     FrameView* frameView() const { return m_frameView; }
65 
66     virtual void computeRectForRepaint(RenderBoxModelObject* repaintContainer, IntRect&, bool fixed = false);
67     virtual void repaintViewRectangle(const IntRect&, bool immediate = false);
68     // Repaint the view, and all composited layers that intersect the given absolute rectangle.
69     // FIXME: ideally we'd never have to do this, if all repaints are container-relative.
70     virtual void repaintRectangleInViewAndCompositedLayers(const IntRect&, bool immediate = false);
71 
72     virtual void paint(PaintInfo&, int tx, int ty);
73     virtual void paintBoxDecorations(PaintInfo&, int tx, int ty);
74 
75     enum SelectionRepaintMode { RepaintNewXOROld, RepaintNewMinusOld };
76     void setSelection(RenderObject* start, int startPos, RenderObject* end, int endPos, SelectionRepaintMode = RepaintNewXOROld);
77     void clearSelection();
selectionStart()78     RenderObject* selectionStart() const { return m_selectionStart; }
selectionEnd()79     RenderObject* selectionEnd() const { return m_selectionEnd; }
80     IntRect selectionBounds(bool clipToVisibleContent = true) const;
81     void selectionStartEnd(int& startPos, int& endPos) const;
82 
83     bool printing() const;
84 
85     virtual void absoluteRects(Vector<IntRect>&, int tx, int ty);
86     virtual void absoluteQuads(Vector<FloatQuad>&);
87 
88 #if USE(ACCELERATED_COMPOSITING)
89     void setMaximalOutlineSize(int o);
90 #else
setMaximalOutlineSize(int o)91     void setMaximalOutlineSize(int o) { m_maximalOutlineSize = o; }
92 #endif
maximalOutlineSize()93     int maximalOutlineSize() const { return m_maximalOutlineSize; }
94 
95     virtual IntRect viewRect() const;
96 
97     void updateWidgetPositions();
98     void addWidget(RenderWidget*);
99     void removeWidget(RenderWidget*);
100 
101     void notifyWidgets(WidgetNotification);
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.
layoutDelta()106     IntSize layoutDelta() const
107     {
108         return m_layoutState ? m_layoutState->m_layoutDelta : IntSize();
109     }
addLayoutDelta(const IntSize & delta)110     void addLayoutDelta(const IntSize& delta)
111     {
112         if (m_layoutState)
113             m_layoutState->m_layoutDelta += delta;
114     }
115 
doingFullRepaint()116     bool doingFullRepaint() const { return m_frameView->needsFullRepaint(); }
117 
118     // Subtree push/pop
119     void pushLayoutState(RenderObject*);
popLayoutState(RenderObject *)120     void popLayoutState(RenderObject*) { return popLayoutState(); } // Just doing this to keep popLayoutState() private and to make the subtree calls symmetrical.
121 
122     bool shouldDisableLayoutStateForSubtree(RenderObject*) const;
123 
124     // Returns true if layoutState should be used for its cached offset and clip.
layoutStateEnabled()125     bool layoutStateEnabled() const { return m_layoutStateDisableCount == 0 && m_layoutState; }
layoutState()126     LayoutState* layoutState() const { return m_layoutState; }
127 
128     // Suspends the LayoutState optimization. Used under transforms that cannot be represented by
129     // LayoutState (common in SVG) and when manipulating the render tree during layout in ways
130     // that can trigger repaint of a non-child (e.g. when a list item moves its list marker around).
131     // Note that even when disabled, LayoutState is still used to store layoutDelta.
disableLayoutState()132     void disableLayoutState() { m_layoutStateDisableCount++; }
enableLayoutState()133     void enableLayoutState() { ASSERT(m_layoutStateDisableCount > 0); m_layoutStateDisableCount--; }
134 
135     virtual void updateHitTestResult(HitTestResult&, const IntPoint&);
136 
pageLogicalHeight()137     unsigned pageLogicalHeight() const { return m_pageLogicalHeight; }
setPageLogicalHeight(unsigned height)138     void setPageLogicalHeight(unsigned height)
139     {
140         if (m_pageLogicalHeight != height) {
141             m_pageLogicalHeight = height;
142             m_pageLogicalHeightChanged = true;
143         }
144     }
145 
146     // FIXME: These functions are deprecated. No code should be added that uses these.
bestTruncatedAt()147     int bestTruncatedAt() const { return m_legacyPrinting.m_bestTruncatedAt; }
148     void setBestTruncatedAt(int y, RenderBoxModelObject* forRenderer, bool forcedBreak = false);
truncatedAt()149     int truncatedAt() const { return m_legacyPrinting.m_truncatedAt; }
setTruncatedAt(int y)150     void setTruncatedAt(int y)
151     {
152         m_legacyPrinting.m_truncatedAt = y;
153         m_legacyPrinting.m_bestTruncatedAt = 0;
154         m_legacyPrinting.m_truncatorWidth = 0;
155         m_legacyPrinting.m_forcedPageBreak = false;
156     }
printRect()157     const IntRect& printRect() const { return m_legacyPrinting.m_printRect; }
setPrintRect(const IntRect & r)158     void setPrintRect(const IntRect& r) { m_legacyPrinting.m_printRect = r; }
159     // End deprecated functions.
160 
161     // Notifications that this view became visible in a window, or will be
162     // removed from the window.
163     void didMoveOnscreen();
164     void willMoveOffscreen();
165 
166 #if USE(ACCELERATED_COMPOSITING)
167     RenderLayerCompositor* compositor();
168     bool usesCompositing() const;
169 #endif
170 
171     int docTop() const;
172     int docBottom() const;
docHeight()173     int docHeight() const { return docBottom() - docTop(); }
174     int docLeft() const;
175     int docRight() const;
docWidth()176     int docWidth() const { return docRight() - docLeft(); }
documentRect()177     IntRect documentRect() const { return IntRect(docLeft(), docTop(), docWidth(), docHeight()); }
178 
179 protected:
180     virtual void mapLocalToContainer(RenderBoxModelObject* repaintContainer, bool useTransforms, bool fixed, TransformState&) const;
181     virtual void mapAbsoluteToLocalPoint(bool fixed, bool useTransforms, TransformState&) const;
182 
183 private:
184     bool shouldRepaint(const IntRect& r) const;
185 
186     // These functions may only be accessed by LayoutStateMaintainer.
187     bool pushLayoutState(RenderBox* renderer, const IntSize& offset, int pageHeight = 0, bool pageHeightChanged = false, ColumnInfo* colInfo = 0)
188     {
189         // We push LayoutState even if layoutState is disabled because it stores layoutDelta too.
190         if (!doingFullRepaint() || renderer->hasColumns() || m_layoutState->isPaginated()) {
191             m_layoutState = new (renderArena()) LayoutState(m_layoutState, renderer, offset, pageHeight, pageHeightChanged, colInfo);
192             return true;
193         }
194         return false;
195     }
196 
popLayoutState()197     void popLayoutState()
198     {
199         LayoutState* state = m_layoutState;
200         m_layoutState = state->m_next;
201         state->destroy(renderArena());
202     }
203 
204     size_t getRetainedWidgets(Vector<RenderWidget*>&);
205     void releaseWidgets(Vector<RenderWidget*>&);
206 
207     friend class LayoutStateMaintainer;
208 
209 protected:
210     FrameView* m_frameView;
211 
212     RenderObject* m_selectionStart;
213     RenderObject* m_selectionEnd;
214     int m_selectionStartPos;
215     int m_selectionEndPos;
216 
217     // FIXME: Only used by embedded WebViews inside AppKit NSViews.  Find a way to remove.
218     struct LegacyPrinting {
LegacyPrintingLegacyPrinting219         LegacyPrinting()
220             : m_bestTruncatedAt(0)
221             , m_truncatedAt(0)
222             , m_truncatorWidth(0)
223             , m_forcedPageBreak(false)
224         { }
225 
226         int m_bestTruncatedAt;
227         int m_truncatedAt;
228         int m_truncatorWidth;
229         IntRect m_printRect;
230         bool m_forcedPageBreak;
231     };
232     LegacyPrinting m_legacyPrinting;
233     // End deprecated members.
234 
235     int m_maximalOutlineSize; // Used to apply a fudge factor to dirty-rect checks on blocks/tables.
236 
237     typedef HashSet<RenderWidget*> RenderWidgetSet;
238     RenderWidgetSet m_widgets;
239 
240 private:
241     unsigned m_pageLogicalHeight;
242     bool m_pageLogicalHeightChanged;
243     LayoutState* m_layoutState;
244     unsigned m_layoutStateDisableCount;
245 #if USE(ACCELERATED_COMPOSITING)
246     OwnPtr<RenderLayerCompositor> m_compositor;
247 #endif
248 };
249 
toRenderView(RenderObject * object)250 inline RenderView* toRenderView(RenderObject* object)
251 {
252     ASSERT(!object || object->isRenderView());
253     return static_cast<RenderView*>(object);
254 }
255 
toRenderView(const RenderObject * object)256 inline const RenderView* toRenderView(const RenderObject* object)
257 {
258     ASSERT(!object || object->isRenderView());
259     return static_cast<const RenderView*>(object);
260 }
261 
262 // This will catch anyone doing an unnecessary cast.
263 void toRenderView(const RenderView*);
264 
265 
266 // Stack-based class to assist with LayoutState push/pop
267 class LayoutStateMaintainer {
268     WTF_MAKE_NONCOPYABLE(LayoutStateMaintainer);
269 public:
270     // ctor to push now
271     LayoutStateMaintainer(RenderView* view, RenderBox* root, IntSize offset, bool disableState = false, int pageHeight = 0, bool pageHeightChanged = false, ColumnInfo* colInfo = 0)
m_view(view)272         : m_view(view)
273         , m_disabled(disableState)
274         , m_didStart(false)
275         , m_didEnd(false)
276         , m_didCreateLayoutState(false)
277     {
278         push(root, offset, pageHeight, pageHeightChanged, colInfo);
279     }
280 
281     // ctor to maybe push later
LayoutStateMaintainer(RenderView * view)282     LayoutStateMaintainer(RenderView* view)
283         : m_view(view)
284         , m_disabled(false)
285         , m_didStart(false)
286         , m_didEnd(false)
287         , m_didCreateLayoutState(false)
288     {
289     }
290 
~LayoutStateMaintainer()291     ~LayoutStateMaintainer()
292     {
293         ASSERT(m_didStart == m_didEnd);   // if this fires, it means that someone did a push(), but forgot to pop().
294     }
295 
296     void push(RenderBox* root, IntSize offset, int pageHeight = 0, bool pageHeightChanged = false, ColumnInfo* colInfo = 0)
297     {
298         ASSERT(!m_didStart);
299         // We push state even if disabled, because we still need to store layoutDelta
300         m_didCreateLayoutState = m_view->pushLayoutState(root, offset, pageHeight, pageHeightChanged, colInfo);
301         if (m_disabled && m_didCreateLayoutState)
302             m_view->disableLayoutState();
303         m_didStart = true;
304     }
305 
pop()306     void pop()
307     {
308         if (m_didStart) {
309             ASSERT(!m_didEnd);
310             if (m_didCreateLayoutState) {
311                 m_view->popLayoutState();
312                 if (m_disabled)
313                     m_view->enableLayoutState();
314             }
315 
316             m_didEnd = true;
317         }
318     }
319 
didPush()320     bool didPush() const { return m_didStart; }
321 
322 private:
323     RenderView* m_view;
324     bool m_disabled : 1;        // true if the offset and clip part of layoutState is disabled
325     bool m_didStart : 1;        // true if we did a push or disable
326     bool m_didEnd : 1;          // true if we popped or re-enabled
327     bool m_didCreateLayoutState : 1; // true if we actually made a layout state.
328 };
329 
330 } // namespace WebCore
331 
332 #endif // RenderView_h
333