5e76e30ffe4d3cd01b292eacafa65ca9d5857c59
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / RenderFlowThread.h
1 /*
2  * Copyright (C) 2011 Adobe Systems Incorporated. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above
9  *    copyright notice, this list of conditions and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above
12  *    copyright notice, this list of conditions and the following
13  *    disclaimer in the documentation and/or other materials
14  *    provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
25  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #ifndef RenderFlowThread_h
31 #define RenderFlowThread_h
32
33
34 #include "core/rendering/RenderBlockFlow.h"
35 #include "wtf/HashCountedSet.h"
36 #include "wtf/ListHashSet.h"
37 #include "wtf/PassRefPtr.h"
38
39 namespace WebCore {
40
41 struct LayerFragment;
42 typedef Vector<LayerFragment, 1> LayerFragments;
43 class RenderFlowThread;
44 class RenderStyle;
45 class RenderRegion;
46
47 typedef ListHashSet<RenderRegion*> RenderRegionList;
48
49 // RenderFlowThread is used to collect all the render objects that participate in a
50 // flow thread. It will also help in doing the layout. However, it will not render
51 // directly to screen. Instead, RenderRegion objects will redirect their paint
52 // and nodeAtPoint methods to this object. Each RenderRegion will actually be a viewPort
53 // of the RenderFlowThread.
54
55 class RenderFlowThread: public RenderBlockFlow {
56 public:
57     RenderFlowThread();
58     virtual ~RenderFlowThread() { };
59
60     virtual bool isRenderFlowThread() const OVERRIDE FINAL { return true; }
61     virtual bool isRenderMultiColumnFlowThread() const { return false; }
62
63     virtual void layout() OVERRIDE;
64
65     // Always create a RenderLayer for the RenderFlowThread so that we
66     // can easily avoid drawing the children directly.
67     virtual LayerType layerTypeRequired() const OVERRIDE FINAL { return NormalLayer; }
68
69     virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction) OVERRIDE FINAL;
70
71     virtual void addRegionToThread(RenderRegion*) = 0;
72     virtual void removeRegionFromThread(RenderRegion*);
73     const RenderRegionList& renderRegionList() const { return m_regionList; }
74
75     virtual void computeLogicalHeight(LayoutUnit logicalHeight, LayoutUnit logicalTop, LogicalExtentComputedValues&) const OVERRIDE;
76
77     bool hasRegions() const { return m_regionList.size(); }
78
79     void validateRegions();
80     void invalidateRegions();
81     bool hasValidRegionInfo() const { return !m_regionsInvalidated && !m_regionList.isEmpty(); }
82
83     void repaintRectangleInRegions(const LayoutRect&) const;
84
85     LayoutPoint adjustedPositionRelativeToOffsetParent(const RenderBoxModelObject&, const LayoutPoint&);
86
87     LayoutUnit pageLogicalTopForOffset(LayoutUnit);
88     LayoutUnit pageLogicalHeightForOffset(LayoutUnit);
89     LayoutUnit pageRemainingLogicalHeightForOffset(LayoutUnit, PageBoundaryRule = IncludePageBoundary);
90
91     virtual void setPageBreak(LayoutUnit /*offset*/, LayoutUnit /*spaceShortage*/) { }
92     virtual void updateMinimumPageHeight(LayoutUnit /*offset*/, LayoutUnit /*minHeight*/) { }
93
94     virtual RenderRegion* regionAtBlockOffset(LayoutUnit) const;
95
96     bool regionsHaveUniformLogicalHeight() const { return m_regionsHaveUniformLogicalHeight; }
97
98     RenderRegion* firstRegion() const;
99     RenderRegion* lastRegion() const;
100
101     void setRegionRangeForBox(const RenderBox*, LayoutUnit offsetFromLogicalTopOfFirstPage);
102     void getRegionRangeForBox(const RenderBox*, RenderRegion*& startRegion, RenderRegion*& endRegion) const;
103
104     virtual bool addForcedRegionBreak(LayoutUnit, RenderObject* breakChild, bool isBefore, LayoutUnit* offsetBreakAdjustment = 0) { return false; }
105
106     virtual bool isPageLogicalHeightKnown() const { return true; }
107     bool pageLogicalSizeChanged() const { return m_pageLogicalSizeChanged; }
108
109     void collectLayerFragments(LayerFragments&, const LayoutRect& layerBoundingBox, const LayoutRect& dirtyRect);
110     LayoutRect fragmentsBoundingBox(const LayoutRect& layerBoundingBox);
111
112     void pushFlowThreadLayoutState(const RenderObject&);
113     void popFlowThreadLayoutState();
114     LayoutUnit offsetFromLogicalTopOfFirstRegion(const RenderBlock*) const;
115
116     // Used to estimate the maximum height of the flow thread.
117     static LayoutUnit maxLogicalHeight() { return LayoutUnit::max() / 2; }
118
119 protected:
120     virtual const char* renderName() const = 0;
121
122     void updateRegionsFlowThreadPortionRect();
123     bool shouldRepaint(const LayoutRect&) const;
124
125     bool cachedOffsetFromLogicalTopOfFirstRegion(const RenderBox*, LayoutUnit&) const;
126     void setOffsetFromLogicalTopOfFirstRegion(const RenderBox*, LayoutUnit);
127     void clearOffsetFromLogicalTopOfFirstRegion(const RenderBox*);
128
129     const RenderBox* currentStatePusherRenderBox() const;
130
131     RenderRegionList m_regionList;
132
133     class RenderRegionRange {
134     public:
135         RenderRegionRange()
136         {
137             setRange(0, 0);
138         }
139
140         RenderRegionRange(RenderRegion* start, RenderRegion* end)
141         {
142             setRange(start, end);
143         }
144
145         void setRange(RenderRegion* start, RenderRegion* end)
146         {
147             m_startRegion = start;
148             m_endRegion = end;
149         }
150
151         RenderRegion* startRegion() const { return m_startRegion; }
152         RenderRegion* endRegion() const { return m_endRegion; }
153
154     private:
155         RenderRegion* m_startRegion;
156         RenderRegion* m_endRegion;
157     };
158
159     typedef PODInterval<LayoutUnit, RenderRegion*> RegionInterval;
160     typedef PODIntervalTree<LayoutUnit, RenderRegion*> RegionIntervalTree;
161
162     class RegionSearchAdapter {
163     public:
164         RegionSearchAdapter(LayoutUnit offset)
165             : m_offset(offset)
166             , m_result(0)
167         {
168         }
169
170         const LayoutUnit& lowValue() const { return m_offset; }
171         const LayoutUnit& highValue() const { return m_offset; }
172         void collectIfNeeded(const RegionInterval&);
173
174         RenderRegion* result() const { return m_result; }
175
176     private:
177         LayoutUnit m_offset;
178         RenderRegion* m_result;
179     };
180
181     // A maps from RenderBox
182     typedef HashMap<const RenderBox*, RenderRegionRange> RenderRegionRangeMap;
183     RenderRegionRangeMap m_regionRangeMap;
184
185     // Stack of objects that pushed a LayoutState object on the RenderView. The
186     // objects on the stack are the ones that are curently in the process of being
187     // laid out.
188     ListHashSet<const RenderObject*> m_statePusherObjectsStack;
189     typedef HashMap<const RenderBox*, LayoutUnit> RenderBoxToOffsetMap;
190     RenderBoxToOffsetMap m_boxesToOffsetMap;
191
192     RegionIntervalTree m_regionIntervalTree;
193
194     bool m_regionsInvalidated : 1;
195     bool m_regionsHaveUniformLogicalHeight : 1;
196     bool m_pageLogicalSizeChanged : 1;
197 };
198
199 DEFINE_RENDER_OBJECT_TYPE_CASTS(RenderFlowThread, isRenderFlowThread());
200
201 class CurrentRenderFlowThreadMaintainer {
202     WTF_MAKE_NONCOPYABLE(CurrentRenderFlowThreadMaintainer);
203 public:
204     CurrentRenderFlowThreadMaintainer(RenderFlowThread*);
205     ~CurrentRenderFlowThreadMaintainer();
206 private:
207     RenderFlowThread* m_renderFlowThread;
208     RenderFlowThread* m_previousRenderFlowThread;
209 };
210
211 // These structures are used by PODIntervalTree for debugging.
212 #ifndef NDEBUG
213 template <> struct ValueToString<LayoutUnit> {
214     static String string(const LayoutUnit value) { return String::number(value.toFloat()); }
215 };
216
217 template <> struct ValueToString<RenderRegion*> {
218     static String string(const RenderRegion* value) { return String::format("%p", value); }
219 };
220 #endif
221
222 } // namespace WebCore
223
224 #endif // RenderFlowThread_h