Fix the issue that Web Audio test case fails on PR3.
[framework/web/webkit-efl.git] / Source / WebCore / rendering / RenderRegion.cpp
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 #include "config.h"
31 #include "RenderRegion.h"
32
33 #include "FlowThreadController.h"
34 #include "GraphicsContext.h"
35 #include "HitTestResult.h"
36 #include "IntRect.h"
37 #include "PaintInfo.h"
38 #include "RenderBoxRegionInfo.h"
39 #include "RenderNamedFlowThread.h"
40 #include "RenderView.h"
41 #include "StyleResolver.h"
42
43 namespace WebCore {
44
45 RenderRegion::RenderRegion(Node* node, RenderFlowThread* flowThread)
46     : RenderReplaced(node, IntSize())
47     , m_flowThread(flowThread)
48     , m_parentNamedFlowThread(0)
49     , m_isValid(false)
50     , m_hasCustomRegionStyle(false)
51     , m_regionState(RegionUndefined)
52     , m_dispatchRegionLayoutUpdateEvent(false)
53 {
54 }
55
56 LayoutUnit RenderRegion::logicalWidthForFlowThreadContent() const
57 {
58     return m_flowThread->isHorizontalWritingMode() ? contentWidth() : contentHeight();
59 }
60
61 LayoutUnit RenderRegion::logicalHeightForFlowThreadContent() const
62 {
63     return m_flowThread->isHorizontalWritingMode() ? contentHeight() : contentWidth();
64 }
65
66 LayoutRect RenderRegion::regionOverflowRect() const
67 {
68     // FIXME: Would like to just use hasOverflowClip() but we aren't a block yet. When RenderRegion is eliminated and
69     // folded into RenderBlock, switch to hasOverflowClip().
70     bool clipX = style()->overflowX() != OVISIBLE;
71     bool clipY = style()->overflowY() != OVISIBLE;
72     if ((clipX && clipY) || !isValid() || !m_flowThread)
73         return regionRect();
74
75     LayoutRect flowThreadOverflow = m_flowThread->visualOverflowRect();
76
77     // Only clip along the flow thread axis.
78     LayoutUnit outlineSize = maximalOutlineSize(PaintPhaseOutline);
79     LayoutRect clipRect;
80     if (m_flowThread->isHorizontalWritingMode()) {
81         LayoutUnit minY = isFirstRegion() ? (flowThreadOverflow.y() - outlineSize) : regionRect().y();
82         LayoutUnit maxY = isLastRegion() ? max(regionRect().maxY(), flowThreadOverflow.maxY()) + outlineSize : regionRect().maxY();
83         LayoutUnit minX = clipX ? regionRect().x() : (flowThreadOverflow.x() - outlineSize);
84         LayoutUnit maxX = clipX ? regionRect().maxX() : (flowThreadOverflow.maxX() + outlineSize);
85         clipRect = LayoutRect(minX, minY, maxX - minX, maxY - minY);
86     } else {
87         LayoutUnit minX = isFirstRegion() ? (flowThreadOverflow.x() - outlineSize) : regionRect().x();
88         LayoutUnit maxX = isLastRegion() ? max(regionRect().maxX(), flowThreadOverflow.maxX()) + outlineSize : regionRect().maxX();
89         LayoutUnit minY = clipY ? regionRect().y() : (flowThreadOverflow.y() - outlineSize);
90         LayoutUnit maxY = clipY ? regionRect().maxY() : (flowThreadOverflow.maxY() + outlineSize);
91         clipRect = LayoutRect(minX, minY, maxX - minX, maxY - minY);
92     }
93
94     return clipRect;
95 }
96
97 bool RenderRegion::isFirstRegion() const
98 {
99     ASSERT(isValid() && m_flowThread);
100     return m_flowThread->firstRegion() == this;
101 }
102
103 bool RenderRegion::isLastRegion() const
104 {
105     ASSERT(isValid() && m_flowThread);
106     return m_flowThread->lastRegion() == this;
107 }
108
109 void RenderRegion::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
110 {
111     // Delegate painting of content in region to RenderFlowThread.
112     if (!m_flowThread || !isValid())
113         return;
114
115     setRegionObjectsRegionStyle();
116     m_flowThread->paintIntoRegion(paintInfo, this, LayoutPoint(paintOffset.x() + borderLeft() + paddingLeft(), paintOffset.y() + borderTop() + paddingTop()));
117     restoreRegionObjectsOriginalStyle();
118 }
119
120 // Hit Testing
121 bool RenderRegion::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestPoint& pointInContainer, const LayoutPoint& accumulatedOffset, HitTestAction action)
122 {
123     if (!isValid())
124         return false;
125
126     LayoutPoint adjustedLocation = accumulatedOffset + location();
127
128     // Check our bounds next. For this purpose always assume that we can only be hit in the
129     // foreground phase (which is true for replaced elements like images).
130     LayoutRect boundsRect = borderBoxRectInRegion(pointInContainer.region());
131     boundsRect.moveBy(adjustedLocation);
132     if (visibleToHitTesting() && action == HitTestForeground && pointInContainer.intersects(boundsRect)) {
133         // Check the contents of the RenderFlowThread.
134         if (m_flowThread && m_flowThread->hitTestRegion(this, request, result, pointInContainer, LayoutPoint(adjustedLocation.x() + borderLeft() + paddingLeft(), adjustedLocation.y() + borderTop() + paddingTop())))
135             return true;
136         updateHitTestResult(result, pointInContainer.point() - toLayoutSize(adjustedLocation));
137         if (!result.addNodeToRectBasedTestResult(node(), pointInContainer, boundsRect))
138             return true;
139     }
140
141     return false;
142 }
143
144 void RenderRegion::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
145 {
146     RenderReplaced::styleDidChange(diff, oldStyle);
147
148     // If the region is not attached to any thread, there is no need to check
149     // whether the region has region styling since no content will be displayed
150     // into the region.
151     if (!m_flowThread) {
152         setHasCustomRegionStyle(false);
153         return;
154     }
155
156     bool customRegionStyle = false;
157     if (node()) {
158         Element* regionElement = static_cast<Element*>(node());
159         customRegionStyle = view()->document()->styleResolver()->checkRegionStyle(regionElement);
160     }
161     setHasCustomRegionStyle(customRegionStyle);
162     m_flowThread->checkRegionsWithStyling();
163 }
164
165 void RenderRegion::layout()
166 {
167     RenderReplaced::layout();
168     if (m_flowThread && isValid()) {
169         LayoutRect oldRegionRect(regionRect());
170         if (!isHorizontalWritingMode())
171             oldRegionRect = oldRegionRect.transposedRect();
172         if (oldRegionRect.width() != logicalWidthForFlowThreadContent() || oldRegionRect.height() != logicalHeightForFlowThreadContent())
173             m_flowThread->invalidateRegions();
174     }
175
176     // FIXME: We need to find a way to set up overflow properly. Our flow thread hasn't gotten a layout
177     // yet, so we can't look to it for correct information. It's possible we could wait until after the RenderFlowThread
178     // gets a layout, and then try to propagate overflow information back to the region, and then mark for a second layout.
179     // That second layout would then be able to use the information from the RenderFlowThread to set up overflow.
180     //
181     // The big problem though is that overflow needs to be region-specific. We can't simply use the RenderFlowThread's global
182     // overflow values, since then we'd always think any narrow region had huge overflow (all the way to the width of the
183     // RenderFlowThread itself).
184     //
185     // We'll need to expand RenderBoxRegionInfo to also hold left and right overflow values.
186 }
187
188 void RenderRegion::attachRegion()
189 {
190     if (!m_flowThread)
191         return;
192
193     // By now the flow thread should already be added to the rendering tree,
194     // so we go up the rendering parents and check that this region is not part of the same
195     // flow that it actually needs to display. It would create a circular reference.
196     RenderObject* parentObject = parent();
197     m_parentNamedFlowThread = 0;
198     for ( ; parentObject; parentObject = parentObject->parent()) {
199         if (parentObject->isRenderNamedFlowThread()) {
200             m_parentNamedFlowThread = toRenderNamedFlowThread(parentObject);
201             // Do not take into account a region that links a flow with itself. The dependency
202             // cannot change, so it is not worth adding it to the list.
203             if (m_flowThread == m_parentNamedFlowThread) {
204                 m_flowThread = 0;
205                 return;
206             }
207             break;
208         }
209     }
210
211     m_flowThread->addRegionToThread(this);
212 }
213
214 void RenderRegion::detachRegion()
215 {
216     if (m_flowThread)
217         m_flowThread->removeRegionFromThread(this);
218 }
219
220 RenderBoxRegionInfo* RenderRegion::renderBoxRegionInfo(const RenderBox* box) const
221 {
222     if (!m_isValid || !m_flowThread)
223         return 0;
224     return m_renderBoxRegionInfo.get(box);
225 }
226
227 RenderBoxRegionInfo* RenderRegion::setRenderBoxRegionInfo(const RenderBox* box, LayoutUnit logicalLeftInset, LayoutUnit logicalRightInset,
228     bool containingBlockChainIsInset)
229 {
230     ASSERT(m_isValid && m_flowThread);
231     if (!m_isValid || !m_flowThread)
232         return 0;
233
234     OwnPtr<RenderBoxRegionInfo>& boxInfo = m_renderBoxRegionInfo.add(box, nullptr).iterator->second;
235     if (boxInfo)
236         *boxInfo = RenderBoxRegionInfo(logicalLeftInset, logicalRightInset, containingBlockChainIsInset);
237     else
238         boxInfo = adoptPtr(new RenderBoxRegionInfo(logicalLeftInset, logicalRightInset, containingBlockChainIsInset));
239
240     return boxInfo.get();
241 }
242
243 PassOwnPtr<RenderBoxRegionInfo> RenderRegion::takeRenderBoxRegionInfo(const RenderBox* box)
244 {
245     return m_renderBoxRegionInfo.take(box);
246 }
247
248 void RenderRegion::removeRenderBoxRegionInfo(const RenderBox* box)
249 {
250     m_renderBoxRegionInfo.remove(box);
251 }
252
253 void RenderRegion::deleteAllRenderBoxRegionInfo()
254 {
255     m_renderBoxRegionInfo.clear();
256 }
257
258 LayoutUnit RenderRegion::offsetFromLogicalTopOfFirstPage() const
259 {
260     if (!m_isValid || !m_flowThread)
261         return 0;
262     if (m_flowThread->isHorizontalWritingMode())
263         return regionRect().y();
264     return regionRect().x();
265 }
266
267 void RenderRegion::setRegionObjectsRegionStyle()
268 {
269     if (!hasCustomRegionStyle())
270         return;
271
272     // Start from content nodes and recursively compute the style in region for the render objects below.
273     // If the style in region was already computed, used that style instead of computing a new one.
274     RenderNamedFlowThread* namedFlow = view()->flowThreadController()->ensureRenderFlowThreadWithName(style()->regionThread());
275     const NamedFlowContentNodes& contentNodes = namedFlow->contentNodes();
276
277     for (NamedFlowContentNodes::const_iterator iter = contentNodes.begin(), end = contentNodes.end(); iter != end; ++iter) {
278         const Node* node = *iter;
279         // The list of content nodes contains also the nodes with display:none.
280         if (!node->renderer())
281             continue;
282
283         RenderObject* object = node->renderer();
284         // If the content node does not flow any of its children in this region,
285         // we do not compute any style for them in this region.
286         if (!flowThread()->objectInFlowRegion(object, this))
287             continue;
288
289         // If the object has style in region, use that instead of computing a new one.
290         RenderObjectRegionStyleMap::iterator it = m_renderObjectRegionStyle.find(object);
291         RefPtr<RenderStyle> objectStyleInRegion;
292         bool objectRegionStyleCached = false;
293         if (it != m_renderObjectRegionStyle.end()) {
294             objectStyleInRegion = it->second.style;
295             ASSERT(it->second.cached);
296             objectRegionStyleCached = true;
297         } else
298             objectStyleInRegion = computeStyleInRegion(object);
299
300         setObjectStyleInRegion(object, objectStyleInRegion, objectRegionStyleCached);
301
302         computeChildrenStyleInRegion(object);
303     }
304 }
305
306 void RenderRegion::restoreRegionObjectsOriginalStyle()
307 {
308     if (!hasCustomRegionStyle())
309         return;
310
311     RenderObjectRegionStyleMap temp;
312     for (RenderObjectRegionStyleMap::iterator iter = m_renderObjectRegionStyle.begin(), end = m_renderObjectRegionStyle.end(); iter != end; ++iter) {
313         RenderObject* object = const_cast<RenderObject*>(iter->first);
314         RefPtr<RenderStyle> objectRegionStyle = object->style();
315         RefPtr<RenderStyle> objectOriginalStyle = iter->second.style;
316         object->setStyleInternal(objectOriginalStyle);
317
318         bool shouldCacheRegionStyle = iter->second.cached;
319         if (!shouldCacheRegionStyle) {
320             // Check whether we should cache the computed style in region.
321             unsigned changedContextSensitiveProperties = ContextSensitivePropertyNone;
322             StyleDifference styleDiff = objectOriginalStyle->diff(objectRegionStyle.get(), changedContextSensitiveProperties);
323             if (styleDiff < StyleDifferenceLayoutPositionedMovementOnly)
324                 shouldCacheRegionStyle = true;
325         }
326         if (shouldCacheRegionStyle) {
327             ObjectRegionStyleInfo styleInfo;
328             styleInfo.style = objectRegionStyle;
329             styleInfo.cached = true;
330             temp.set(object, styleInfo);
331         }
332     }
333
334     m_renderObjectRegionStyle.swap(temp);
335 }
336
337 PassRefPtr<RenderStyle> RenderRegion::computeStyleInRegion(const RenderObject* object)
338 {
339     ASSERT(object);
340     ASSERT(object->view());
341     ASSERT(object->view()->document());
342     ASSERT(!object->isAnonymous());
343     ASSERT(object->node() && object->node()->isElementNode());
344
345     Element* element = toElement(object->node());
346     RefPtr<RenderStyle> renderObjectRegionStyle = object->view()->document()->styleResolver()->styleForElement(element, 0, DisallowStyleSharing, MatchAllRules, this);
347
348     return renderObjectRegionStyle.release();
349 }
350  
351 void RenderRegion::computeChildrenStyleInRegion(const RenderObject* object)
352 {
353     for (RenderObject* child = object->firstChild(); child; child = child->nextSibling()) {
354
355         RenderObjectRegionStyleMap::iterator it = m_renderObjectRegionStyle.find(child);
356
357         RefPtr<RenderStyle> childStyleInRegion;
358         bool objectRegionStyleCached = false;
359         if (it != m_renderObjectRegionStyle.end()) {
360             childStyleInRegion = it->second.style;
361             objectRegionStyleCached = true;
362         } else {
363             if (child->isAnonymous())
364                 childStyleInRegion = RenderStyle::createAnonymousStyleWithDisplay(object->style(), child->style()->display());
365             else if (child->isText())
366                 childStyleInRegion = RenderStyle::clone(object->style());
367             else
368                 childStyleInRegion = computeStyleInRegion(child);
369         }
370
371         setObjectStyleInRegion(child, childStyleInRegion, objectRegionStyleCached);
372
373         computeChildrenStyleInRegion(child);
374     }
375 }
376  
377 void RenderRegion::setObjectStyleInRegion(RenderObject* object, PassRefPtr<RenderStyle> styleInRegion, bool objectRegionStyleCached)
378 {
379     RefPtr<RenderStyle> objectOriginalStyle = object->style();
380     object->setStyleInternal(styleInRegion);
381
382     if (object->isBoxModelObject() && !object->hasBoxDecorations()) {
383         bool hasBoxDecorations = object->isTableCell()
384         || object->style()->hasBackground()
385         || object->style()->hasBorder()
386         || object->style()->hasAppearance()
387         || object->style()->boxShadow();
388         object->setHasBoxDecorations(hasBoxDecorations);
389     }
390
391     ObjectRegionStyleInfo styleInfo;
392     styleInfo.style = objectOriginalStyle;
393     styleInfo.cached = objectRegionStyleCached;
394     m_renderObjectRegionStyle.set(object, styleInfo);
395 }
396  
397 void RenderRegion::clearObjectStyleInRegion(const RenderObject* object)
398 {
399     ASSERT(object);
400     m_renderObjectRegionStyle.remove(object);
401
402     // Clear the style for the children of this object.
403     for (RenderObject* child = object->firstChild(); child; child = child->nextSibling())
404         clearObjectStyleInRegion(child);
405 }
406
407 } // namespace WebCore