https://bugs.webkit.org/show_bug.cgi?id=86158
[WebKit-https.git] / Source / WebCore / rendering / RenderLayer.cpp
1 /*
2  * Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
3  *
4  * Portions are Copyright (C) 1998 Netscape Communications Corporation.
5  *
6  * Other contributors:
7  *   Robert O'Callahan <roc+@cs.cmu.edu>
8  *   David Baron <dbaron@fas.harvard.edu>
9  *   Christian Biesinger <cbiesinger@web.de>
10  *   Randall Jesup <rjesup@wgate.com>
11  *   Roland Mainz <roland.mainz@informatik.med.uni-giessen.de>
12  *   Josh Soref <timeless@mac.com>
13  *   Boris Zbarsky <bzbarsky@mit.edu>
14  *
15  * This library is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU Lesser General Public
17  * License as published by the Free Software Foundation; either
18  * version 2.1 of the License, or (at your option) any later version.
19  *
20  * This library is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23  * Lesser General Public License for more details.
24  *
25  * You should have received a copy of the GNU Lesser General Public
26  * License along with this library; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
28  *
29  * Alternatively, the contents of this file may be used under the terms
30  * of either the Mozilla Public License Version 1.1, found at
31  * http://www.mozilla.org/MPL/ (the "MPL") or the GNU General Public
32  * License Version 2.0, found at http://www.fsf.org/copyleft/gpl.html
33  * (the "GPL"), in which case the provisions of the MPL or the GPL are
34  * applicable instead of those above.  If you wish to allow use of your
35  * version of this file only under the terms of one of those two
36  * licenses (the MPL or the GPL) and not to allow others to use your
37  * version of this file under the LGPL, indicate your decision by
38  * deletingthe provisions above and replace them with the notice and
39  * other provisions required by the MPL or the GPL, as the case may be.
40  * If you do not delete the provisions above, a recipient may use your
41  * version of this file under any of the LGPL, the MPL or the GPL.
42  */
43
44 #include "config.h"
45 #include "RenderLayer.h"
46
47 #include "ColumnInfo.h"
48 #include "CSSPropertyNames.h"
49 #include "Chrome.h"
50 #include "Document.h"
51 #include "DocumentEventQueue.h"
52 #include "EventHandler.h"
53 #if ENABLE(CSS_FILTERS)
54 #include "FEColorMatrix.h"
55 #include "FEMerge.h"
56 #include "FilterEffectRenderer.h"
57 #endif
58 #include "FloatConversion.h"
59 #include "FloatPoint3D.h"
60 #include "FloatRect.h"
61 #include "FocusController.h"
62 #include "Frame.h"
63 #include "FrameSelection.h"
64 #include "FrameTree.h"
65 #include "FrameView.h"
66 #include "Gradient.h"
67 #include "GraphicsContext.h"
68 #include "HTMLFrameElement.h"
69 #include "HTMLFrameOwnerElement.h"
70 #include "HTMLNames.h"
71 #include "HitTestingTransformState.h"
72 #include "HitTestRequest.h"
73 #include "HitTestResult.h"
74 #include "OverflowEvent.h"
75 #include "OverlapTestRequestClient.h"
76 #include "Page.h"
77 #include "PlatformMouseEvent.h"
78 #include "RenderArena.h"
79 #include "RenderFlowThread.h"
80 #include "RenderInline.h"
81 #include "RenderMarquee.h"
82 #include "RenderReplica.h"
83 #include "RenderScrollbar.h"
84 #include "RenderScrollbarPart.h"
85 #include "RenderTheme.h"
86 #include "RenderTreeAsText.h"
87 #include "RenderView.h"
88 #include "ScaleTransformOperation.h"
89 #include "Scrollbar.h"
90 #include "ScrollbarTheme.h"
91 #include "Settings.h"
92 #include "SourceGraphic.h"
93 #include "StylePropertySet.h"
94 #include "StyleResolver.h"
95 #include "TextStream.h"
96 #include "TransformationMatrix.h"
97 #include "TranslateTransformOperation.h"
98 #include <wtf/StdLibExtras.h>
99 #include <wtf/UnusedParam.h>
100 #include <wtf/text/CString.h>
101
102 #if USE(ACCELERATED_COMPOSITING)
103 #include "RenderLayerBacking.h"
104 #include "RenderLayerCompositor.h"
105 #endif
106
107 #if ENABLE(SVG)
108 #include "SVGNames.h"
109 #endif
110
111 #if PLATFORM(CHROMIUM) || PLATFORM(BLACKBERRY)
112 // FIXME: border radius clipping triggers too-slow path on Chromium
113 // https://bugs.webkit.org/show_bug.cgi?id=69866
114 #define DISABLE_ROUNDED_CORNER_CLIPPING
115 #endif
116
117 #define MIN_INTERSECT_FOR_REVEAL 32
118
119 using namespace std;
120
121 namespace WebCore {
122
123 using namespace HTMLNames;
124
125 const int MinimumWidthWhileResizing = 100;
126 const int MinimumHeightWhileResizing = 40;
127
128 void* ClipRects::operator new(size_t sz, RenderArena* renderArena)
129 {
130     return renderArena->allocate(sz);
131 }
132
133 void ClipRects::operator delete(void* ptr, size_t sz)
134 {
135     // Stash size where destroy can find it.
136     *(size_t *)ptr = sz;
137 }
138
139 void ClipRects::destroy(RenderArena* renderArena)
140 {
141     delete this;
142     
143     // Recover the size left there for us by operator delete and free the memory.
144     renderArena->free(*(size_t *)this, this);
145 }
146
147 RenderLayer::RenderLayer(RenderBoxModelObject* renderer)
148     : m_inResizeMode(false)
149     , m_scrollDimensionsDirty(true)
150     , m_normalFlowListDirty(true)
151     , m_usedTransparency(false)
152     , m_paintingInsideReflection(false)
153     , m_inOverflowRelayout(false)
154     , m_repaintStatus(NeedsNormalRepaint)
155     , m_visibleContentStatusDirty(true)
156     , m_hasVisibleContent(false)
157     , m_visibleDescendantStatusDirty(false)
158     , m_hasVisibleDescendant(false)
159     , m_isPaginated(false)
160     , m_3DTransformedDescendantStatusDirty(true)
161     , m_has3DTransformedDescendant(false)
162 #if USE(ACCELERATED_COMPOSITING)
163     , m_hasCompositingDescendant(false)
164     , m_mustOverlapCompositedLayers(false)
165 #endif
166     , m_containsDirtyOverlayScrollbars(false)
167 #if !ASSERT_DISABLED
168     , m_layerListMutationAllowed(true)
169 #endif
170     , m_canSkipRepaintRectsUpdateOnScroll(renderer->isTableCell())
171 #if ENABLE(CSS_FILTERS)
172     , m_hasFilterInfo(false)
173 #endif
174     , m_renderer(renderer)
175     , m_parent(0)
176     , m_previous(0)
177     , m_next(0)
178     , m_first(0)
179     , m_last(0)
180     , m_posZOrderList(0)
181     , m_negZOrderList(0)
182     , m_normalFlowList(0)
183     , m_clipRects(0)
184 #ifndef NDEBUG
185     , m_clipRectsRoot(0)
186 #endif
187     , m_marquee(0)
188     , m_staticInlinePosition(0)
189     , m_staticBlockPosition(0)
190     , m_reflection(0)
191     , m_scrollCorner(0)
192     , m_resizer(0)
193 {
194     m_isNormalFlowOnly = shouldBeNormalFlowOnly();
195     // Non-stacking contexts should have empty z-order lists. As this is already the case,
196     // there is no need to dirty / recompute these lists.
197     m_zOrderListsDirty = isStackingContext();
198
199     ScrollableArea::setConstrainsScrollingToContentEdge(false);
200
201     if (!renderer->firstChild() && renderer->style()) {
202         m_visibleContentStatusDirty = false;
203         m_hasVisibleContent = renderer->style()->visibility() == VISIBLE;
204     }
205
206     Node* node = renderer->node();
207     if (node && node->isElementNode()) {
208         // We save and restore only the scrollOffset as the other scroll values are recalculated.
209         Element* element = toElement(node);
210         m_scrollOffset = element->savedLayerScrollOffset();
211         element->setSavedLayerScrollOffset(IntSize());
212     }
213 }
214
215 RenderLayer::~RenderLayer()
216 {
217     if (inResizeMode() && !renderer()->documentBeingDestroyed()) {
218         if (Frame* frame = renderer()->frame())
219             frame->eventHandler()->resizeLayerDestroyed();
220     }
221
222     if (Frame* frame = renderer()->frame()) {
223         if (FrameView* frameView = frame->view())
224             frameView->removeScrollableArea(this);
225     }
226
227     if (!m_renderer->documentBeingDestroyed()) {
228         Node* node = m_renderer->node();
229         if (node && node->isElementNode())
230             toElement(node)->setSavedLayerScrollOffset(m_scrollOffset);
231     }
232
233     destroyScrollbar(HorizontalScrollbar);
234     destroyScrollbar(VerticalScrollbar);
235
236     if (m_reflection)
237         removeReflection();
238     
239 #if ENABLE(CSS_FILTERS)
240     removeFilterInfoIfNeeded();
241 #endif
242
243     // Child layers will be deleted by their corresponding render objects, so
244     // we don't need to delete them ourselves.
245
246     delete m_posZOrderList;
247     delete m_negZOrderList;
248     delete m_normalFlowList;
249     delete m_marquee;
250
251 #if USE(ACCELERATED_COMPOSITING)
252     clearBacking(true);
253 #endif
254     
255     // Make sure we have no lingering clip rects.
256     ASSERT(!m_clipRects);
257     
258     if (m_scrollCorner)
259         m_scrollCorner->destroy();
260     if (m_resizer)
261         m_resizer->destroy();
262 }
263
264 #if USE(ACCELERATED_COMPOSITING)
265 RenderLayerCompositor* RenderLayer::compositor() const
266 {
267     ASSERT(renderer()->view());
268     return renderer()->view()->compositor();
269 }
270
271 void RenderLayer::contentChanged(ContentChangeType changeType)
272 {
273     // This can get called when video becomes accelerated, so the layers may change.
274     if ((changeType == CanvasChanged || changeType == VideoChanged || changeType == FullScreenChanged) && compositor()->updateLayerCompositingState(this))
275         compositor()->setCompositingLayersNeedRebuild();
276
277     if (m_backing)
278         m_backing->contentChanged(changeType);
279 }
280 #endif // USE(ACCELERATED_COMPOSITING)
281
282 bool RenderLayer::canRender3DTransforms() const
283 {
284 #if USE(ACCELERATED_COMPOSITING)
285     return compositor()->canRender3DTransforms();
286 #else
287     return false;
288 #endif
289 }
290
291 #if ENABLE(CSS_FILTERS)
292 bool RenderLayer::paintsWithFilters() const
293 {
294     // FIXME: Eventually there will be more factors than isComposited() to decide whether or not to render the filter
295     if (!renderer()->hasFilter())
296         return false;
297         
298 #if USE(ACCELERATED_COMPOSITING)
299     if (!isComposited())
300         return true;
301
302     if (!m_backing || !m_backing->canCompositeFilters())
303         return true;
304 #endif
305
306     return false;
307 }
308     
309 bool RenderLayer::requiresFullLayerImageForFilters() const 
310 {
311     if (!paintsWithFilters())
312         return false;
313     FilterEffectRenderer* filter = filterRenderer();
314     return filter ? filter->hasFilterThatMovesPixels() : false;
315 }
316 #endif
317
318 LayoutPoint RenderLayer::computeOffsetFromRoot(bool& hasLayerOffset) const
319 {
320     hasLayerOffset = true;
321
322     if (!parent())
323         return LayoutPoint();
324
325     // This is similar to root() but we check if an ancestor layer would
326     // prevent the optimization from working.
327     const RenderLayer* rootLayer = 0;
328     for (const RenderLayer* parentLayer = parent(); parentLayer; rootLayer = parentLayer, parentLayer = parentLayer->parent()) {
329         hasLayerOffset = parentLayer->canUseConvertToLayerCoords();
330         if (!hasLayerOffset)
331             return LayoutPoint();
332     }
333     ASSERT(rootLayer == root());
334
335     LayoutPoint offset;
336     parent()->convertToLayerCoords(rootLayer, offset);
337     return offset;
338 }
339
340 void RenderLayer::updateLayerPositions(LayoutPoint* offsetFromRoot, UpdateLayerPositionsFlags flags)
341 {
342 #if !ASSERT_DISABLED
343     if (offsetFromRoot) {
344         bool hasLayerOffset;
345         LayoutPoint computedOffsetFromRoot = computeOffsetFromRoot(hasLayerOffset);
346         ASSERT(hasLayerOffset);
347         ASSERT(*offsetFromRoot == computedOffsetFromRoot);
348     }
349 #endif
350
351     updateLayerPosition(); // For relpositioned layers or non-positioned layers,
352                            // we need to keep in sync, since we may have shifted relative
353                            // to our parent layer.
354     LayoutPoint oldOffsetFromRoot;
355     if (offsetFromRoot) {
356         // We can't cache our offset to the repaint container if the mapping is anything more complex than a simple translation
357         if (!canUseConvertToLayerCoords())
358             offsetFromRoot = 0; // If our cached offset is invalid make sure it's not passed to any of our children
359         else {
360             oldOffsetFromRoot = *offsetFromRoot;
361             // Frequently our parent layer's renderer will be the same as our renderer's containing block.  In that case,
362             // we just update the cache using our offset to our parent (which is m_topLeft). Otherwise, regenerated cached
363             // offsets to the root from the render tree.
364             if (!m_parent || m_parent->renderer() == renderer()->containingBlock())
365                 offsetFromRoot->move(m_topLeft.x(), m_topLeft.y()); // Fast case
366             else {
367                 LayoutPoint offset;
368                 convertToLayerCoords(root(), offset);
369                 *offsetFromRoot = offset;
370             }
371         }
372     }
373
374     LayoutPoint offset;
375     if (offsetFromRoot) {
376         offset = *offsetFromRoot;
377 #ifndef NDEBUG
378         LayoutPoint computedOffsetFromRoot;
379         convertToLayerCoords(root(), computedOffsetFromRoot);
380         ASSERT(offset == computedOffsetFromRoot);
381 #endif
382     } else {
383         // FIXME: It looks suspicious to call convertToLayerCoords here
384         // as canUseConvertToLayerCoords may be true for an ancestor layer.
385         convertToLayerCoords(root(), offset);
386     }
387     positionOverflowControls(toSize(roundedIntPoint(offset)));
388
389     updateVisibilityStatus();
390
391     if (flags & UpdatePagination)
392         updatePagination();
393     else
394         m_isPaginated = false;
395
396     if (m_hasVisibleContent) {
397         RenderView* view = renderer()->view();
398         ASSERT(view);
399         // FIXME: LayoutState does not work with RenderLayers as there is not a 1-to-1
400         // mapping between them and the RenderObjects. It would be neat to enable
401         // LayoutState outside the layout() phase and use it here.
402         ASSERT(!view->layoutStateEnabled());
403
404         RenderBoxModelObject* repaintContainer = renderer()->containerForRepaint();
405         LayoutRect oldRepaintRect = m_repaintRect;
406         LayoutRect oldOutlineBox = m_outlineBox;
407         computeRepaintRects(offsetFromRoot);
408         // FIXME: Should ASSERT that value calculated for m_outlineBox using the cached offset is the same
409         // as the value not using the cached offset, but we can't due to https://bugs.webkit.org/show_bug.cgi?id=37048
410         if (flags & CheckForRepaint) {
411             if (view && !view->printing()) {
412                 if (m_repaintStatus & NeedsFullRepaint) {
413                     renderer()->repaintUsingContainer(repaintContainer, oldRepaintRect);
414                     if (m_repaintRect != oldRepaintRect)
415                         renderer()->repaintUsingContainer(repaintContainer, m_repaintRect);
416                 } else if (shouldRepaintAfterLayout())
417                     renderer()->repaintAfterLayoutIfNeeded(repaintContainer, oldRepaintRect, oldOutlineBox, &m_repaintRect, &m_outlineBox);
418             }
419         }
420     } else
421         clearRepaintRects();
422
423     m_repaintStatus = NeedsNormalRepaint;
424
425     // Go ahead and update the reflection's position and size.
426     if (m_reflection)
427         m_reflection->layout();
428
429 #if USE(ACCELERATED_COMPOSITING)
430     // Clear the IsCompositingUpdateRoot flag once we've found the first compositing layer in this update.
431     bool isUpdateRoot = (flags & IsCompositingUpdateRoot);
432     if (isComposited())
433         flags &= ~IsCompositingUpdateRoot;
434 #endif
435
436     if (renderer()->hasColumns())
437         flags |= UpdatePagination;
438
439     for (RenderLayer* child = firstChild(); child; child = child->nextSibling())
440         child->updateLayerPositions(offsetFromRoot, flags);
441
442 #if USE(ACCELERATED_COMPOSITING)
443     if ((flags & UpdateCompositingLayers) && isComposited())
444         backing()->updateAfterLayout(RenderLayerBacking::CompositingChildren, isUpdateRoot);
445 #endif
446         
447     // With all our children positioned, now update our marquee if we need to.
448     if (m_marquee)
449         m_marquee->updateMarqueePosition();
450
451     if (offsetFromRoot)
452         *offsetFromRoot = oldOffsetFromRoot;
453 }
454
455 LayoutRect RenderLayer::repaintRectIncludingDescendants() const
456 {
457     LayoutRect repaintRect = m_repaintRect;
458     for (RenderLayer* child = firstChild(); child; child = child->nextSibling())
459         repaintRect.unite(child->repaintRectIncludingDescendants());
460     return repaintRect;
461 }
462
463 void RenderLayer::computeRepaintRects(LayoutPoint* offsetFromRoot)
464 {
465     ASSERT(!m_visibleContentStatusDirty);
466
467     RenderBoxModelObject* repaintContainer = renderer()->containerForRepaint();
468     m_repaintRect = renderer()->clippedOverflowRectForRepaint(repaintContainer);
469     m_outlineBox = renderer()->outlineBoundsForRepaint(repaintContainer, offsetFromRoot);
470 }
471
472 void RenderLayer::clearRepaintRects()
473 {
474     ASSERT(!m_hasVisibleContent);
475     ASSERT(!m_visibleContentStatusDirty);
476
477     m_repaintRect = IntRect();
478     m_outlineBox = IntRect();
479 }
480
481 void RenderLayer::updateLayerPositionsAfterScroll(UpdateLayerPositionsAfterScrollFlags flags)
482 {
483     // FIXME: This shouldn't be needed, but there are some corner cases where
484     // these flags are still dirty. Update so that the check below is valid.
485     updateVisibilityStatus();
486
487     // If we have no visible content and no visible descendants, there is no point recomputing
488     // our rectangles as they will be empty. If our visibility changes, we are expected to
489     // recompute all our positions anyway.
490     if (!m_hasVisibleDescendant && !m_hasVisibleContent)
491         return;
492
493     updateLayerPosition();
494
495     if ((flags & HasSeenFixedPositionedAncestor) || renderer()->style()->position() == FixedPosition) {
496         // FIXME: Is it worth passing the offsetFromRoot around like in updateLayerPositions?
497         computeRepaintRects();
498         flags |= HasSeenFixedPositionedAncestor;
499     } else if ((flags & HasSeenAncestorWithOverflowClip) && !m_canSkipRepaintRectsUpdateOnScroll) {
500         // If we have seen an overflow clip, we should update our repaint rects as clippedOverflowRectForRepaint
501         // intersects it with our ancestor overflow clip that may have moved.
502         computeRepaintRects();
503     }
504
505     if (renderer()->hasOverflowClip())
506         flags |= HasSeenAncestorWithOverflowClip;
507
508     for (RenderLayer* child = firstChild(); child; child = child->nextSibling())
509         child->updateLayerPositionsAfterScroll(flags);
510
511     // We don't update our reflection as scrolling is a translation which does not change the size()
512     // of an object, thus RenderReplica will still repaint itself properly as the layer position was
513     // updated above.
514
515     if (m_marquee)
516         m_marquee->updateMarqueePosition();
517 }
518
519 void RenderLayer::updateTransform()
520 {
521     // hasTransform() on the renderer is also true when there is transform-style: preserve-3d or perspective set,
522     // so check style too.
523     bool hasTransform = renderer()->hasTransform() && renderer()->style()->hasTransform();
524     bool had3DTransform = has3DTransform();
525
526     bool hadTransform = m_transform;
527     if (hasTransform != hadTransform) {
528         if (hasTransform)
529             m_transform = adoptPtr(new TransformationMatrix);
530         else
531             m_transform.clear();
532     }
533     
534     if (hasTransform) {
535         RenderBox* box = renderBox();
536         ASSERT(box);
537         m_transform->makeIdentity();
538         box->style()->applyTransform(*m_transform, box->borderBoxRect().size(), RenderStyle::IncludeTransformOrigin);
539         makeMatrixRenderable(*m_transform, canRender3DTransforms());
540     }
541
542     if (had3DTransform != has3DTransform())
543         dirty3DTransformedDescendantStatus();
544 }
545
546 TransformationMatrix RenderLayer::currentTransform() const
547 {
548     if (!m_transform)
549         return TransformationMatrix();
550
551 #if USE(ACCELERATED_COMPOSITING)
552     if (renderer()->style()->isRunningAcceleratedAnimation()) {
553         TransformationMatrix currTransform;
554         RefPtr<RenderStyle> style = renderer()->animation()->getAnimatedStyleForRenderer(renderer());
555         style->applyTransform(currTransform, renderBox()->borderBoxRect().size(), RenderStyle::IncludeTransformOrigin);
556         makeMatrixRenderable(currTransform, canRender3DTransforms());
557         return currTransform;
558     }
559 #endif
560
561     return *m_transform;
562 }
563
564 TransformationMatrix RenderLayer::renderableTransform(PaintBehavior paintBehavior) const
565 {
566     if (!m_transform)
567         return TransformationMatrix();
568     
569     if (paintBehavior & PaintBehaviorFlattenCompositingLayers) {
570         TransformationMatrix matrix = *m_transform;
571         makeMatrixRenderable(matrix, false /* flatten 3d */);
572         return matrix;
573     }
574
575     return *m_transform;
576 }
577
578 static bool checkContainingBlockChainForPagination(RenderBoxModelObject* renderer, RenderBox* ancestorColumnsRenderer)
579 {
580     RenderView* view = renderer->view();
581     RenderBoxModelObject* prevBlock = renderer;
582     RenderBlock* containingBlock;
583     for (containingBlock = renderer->containingBlock();
584          containingBlock && containingBlock != view && containingBlock != ancestorColumnsRenderer;
585          containingBlock = containingBlock->containingBlock())
586         prevBlock = containingBlock;
587     
588     // If the columns block wasn't in our containing block chain, then we aren't paginated by it.
589     if (containingBlock != ancestorColumnsRenderer)
590         return false;
591         
592     // If the previous block is absolutely positioned, then we can't be paginated by the columns block.
593     if (prevBlock->isPositioned())
594         return false;
595         
596     // Otherwise we are paginated by the columns block.
597     return true;
598 }
599
600 void RenderLayer::updatePagination()
601 {
602     m_isPaginated = false;
603     if (isComposited() || !parent())
604         return; // FIXME: We will have to deal with paginated compositing layers someday.
605                 // FIXME: For now the RenderView can't be paginated.  Eventually printing will move to a model where it is though.
606     
607     if (isNormalFlowOnly()) {
608         m_isPaginated = parent()->renderer()->hasColumns();
609         return;
610     }
611
612     // If we're not normal flow, then we need to look for a multi-column object between us and our stacking context.
613     RenderLayer* ancestorStackingContext = stackingContext();
614     for (RenderLayer* curr = parent(); curr; curr = curr->parent()) {
615         if (curr->renderer()->hasColumns()) {
616             m_isPaginated = checkContainingBlockChainForPagination(renderer(), curr->renderBox());
617             return;
618         }
619         if (curr == ancestorStackingContext)
620             return;
621     }
622 }
623
624 void RenderLayer::setHasVisibleContent(bool b)
625
626     if (m_hasVisibleContent == b && !m_visibleContentStatusDirty)
627         return;
628     m_visibleContentStatusDirty = false; 
629     m_hasVisibleContent = b;
630     if (m_hasVisibleContent) {
631         computeRepaintRects();
632         if (!isNormalFlowOnly()) {
633             for (RenderLayer* sc = stackingContext(); sc; sc = sc->stackingContext()) {
634                 sc->dirtyZOrderLists();
635                 if (sc->hasVisibleContent())
636                     break;
637             }
638         }
639     }
640     if (parent())
641         parent()->childVisibilityChanged(m_hasVisibleContent);
642 }
643
644 void RenderLayer::dirtyVisibleContentStatus() 
645
646     m_visibleContentStatusDirty = true; 
647     if (parent())
648         parent()->dirtyVisibleDescendantStatus();
649 }
650
651 void RenderLayer::childVisibilityChanged(bool newVisibility) 
652
653     if (m_hasVisibleDescendant == newVisibility || m_visibleDescendantStatusDirty)
654         return;
655     if (newVisibility) {
656         RenderLayer* l = this;
657         while (l && !l->m_visibleDescendantStatusDirty && !l->m_hasVisibleDescendant) {
658             l->m_hasVisibleDescendant = true;
659             l = l->parent();
660         }
661     } else 
662         dirtyVisibleDescendantStatus();
663 }
664
665 void RenderLayer::dirtyVisibleDescendantStatus()
666 {
667     RenderLayer* l = this;
668     while (l && !l->m_visibleDescendantStatusDirty) {
669         l->m_visibleDescendantStatusDirty = true;
670         l = l->parent();
671     }
672 }
673
674 void RenderLayer::updateVisibilityStatus()
675 {
676     if (m_visibleDescendantStatusDirty) {
677         m_hasVisibleDescendant = false;
678         for (RenderLayer* child = firstChild(); child; child = child->nextSibling()) {
679             child->updateVisibilityStatus();        
680             if (child->m_hasVisibleContent || child->m_hasVisibleDescendant) {
681                 m_hasVisibleDescendant = true;
682                 break;
683             }
684         }
685         m_visibleDescendantStatusDirty = false;
686     }
687
688     if (m_visibleContentStatusDirty) {
689         if (renderer()->style()->visibility() == VISIBLE)
690             m_hasVisibleContent = true;
691         else {
692             // layer may be hidden but still have some visible content, check for this
693             m_hasVisibleContent = false;
694             RenderObject* r = renderer()->firstChild();
695             while (r) {
696                 if (r->style()->visibility() == VISIBLE && !r->hasLayer()) {
697                     m_hasVisibleContent = true;
698                     break;
699                 }
700                 if (r->firstChild() && !r->hasLayer())
701                     r = r->firstChild();
702                 else if (r->nextSibling())
703                     r = r->nextSibling();
704                 else {
705                     do {
706                         r = r->parent();
707                         if (r == renderer())
708                             r = 0;
709                     } while (r && !r->nextSibling());
710                     if (r)
711                         r = r->nextSibling();
712                 }
713             }
714         }    
715         m_visibleContentStatusDirty = false; 
716     }
717 }
718
719 void RenderLayer::dirty3DTransformedDescendantStatus()
720 {
721     RenderLayer* curr = stackingContext();
722     if (curr)
723         curr->m_3DTransformedDescendantStatusDirty = true;
724         
725     // This propagates up through preserve-3d hierarchies to the enclosing flattening layer.
726     // Note that preserves3D() creates stacking context, so we can just run up the stacking contexts.
727     while (curr && curr->preserves3D()) {
728         curr->m_3DTransformedDescendantStatusDirty = true;
729         curr = curr->stackingContext();
730     }
731 }
732
733 // Return true if this layer or any preserve-3d descendants have 3d.
734 bool RenderLayer::update3DTransformedDescendantStatus()
735 {
736     if (m_3DTransformedDescendantStatusDirty) {
737         m_has3DTransformedDescendant = false;
738
739         // Transformed or preserve-3d descendants can only be in the z-order lists, not
740         // in the normal flow list, so we only need to check those.
741         if (m_posZOrderList) {
742             for (unsigned i = 0; i < m_posZOrderList->size(); ++i)
743                 m_has3DTransformedDescendant |= m_posZOrderList->at(i)->update3DTransformedDescendantStatus();
744         }
745
746         // Now check our negative z-index children.
747         if (m_negZOrderList) {
748             for (unsigned i = 0; i < m_negZOrderList->size(); ++i)
749                 m_has3DTransformedDescendant |= m_negZOrderList->at(i)->update3DTransformedDescendantStatus();
750         }
751         
752         m_3DTransformedDescendantStatusDirty = false;
753     }
754     
755     // If we live in a 3d hierarchy, then the layer at the root of that hierarchy needs
756     // the m_has3DTransformedDescendant set.
757     if (preserves3D())
758         return has3DTransform() || m_has3DTransformedDescendant;
759
760     return has3DTransform();
761 }
762
763 void RenderLayer::updateLayerPosition()
764 {
765     LayoutPoint localPoint;
766     LayoutSize inlineBoundingBoxOffset; // We don't put this into the RenderLayer x/y for inlines, so we need to subtract it out when done.
767     if (renderer()->isRenderInline()) {
768         RenderInline* inlineFlow = toRenderInline(renderer());
769         IntRect lineBox = inlineFlow->linesBoundingBox();
770         setSize(lineBox.size());
771         inlineBoundingBoxOffset = toSize(lineBox.location());
772         localPoint += inlineBoundingBoxOffset;
773     } else if (RenderBox* box = renderBox()) {
774         // FIXME: Is snapping the size really needed here for the RenderBox case?
775         setSize(pixelSnappedIntSize(box->size(), box->location()));
776         localPoint += box->topLeftLocationOffset();
777     }
778
779     // Clear our cached clip rect information.
780     clearClipRects();
781  
782     if (!renderer()->isPositioned() && renderer()->parent()) {
783         // We must adjust our position by walking up the render tree looking for the
784         // nearest enclosing object with a layer.
785         RenderObject* curr = renderer()->parent();
786         while (curr && !curr->hasLayer()) {
787             if (curr->isBox() && !curr->isTableRow()) {
788                 // Rows and cells share the same coordinate space (that of the section).
789                 // Omit them when computing our xpos/ypos.
790                 localPoint += toRenderBox(curr)->topLeftLocationOffset();
791             }
792             curr = curr->parent();
793         }
794         if (curr->isBox() && curr->isTableRow()) {
795             // Put ourselves into the row coordinate space.
796             localPoint -= toRenderBox(curr)->topLeftLocationOffset();
797         }
798     }
799     
800     // Subtract our parent's scroll offset.
801     if (renderer()->isPositioned() && enclosingPositionedAncestor()) {
802         RenderLayer* positionedParent = enclosingPositionedAncestor();
803
804         // For positioned layers, we subtract out the enclosing positioned layer's scroll offset.
805         LayoutSize offset = positionedParent->scrolledContentOffset();
806         localPoint -= offset;
807         
808         if (renderer()->isPositioned() && positionedParent->renderer()->isRelPositioned() && positionedParent->renderer()->isRenderInline()) {
809             LayoutSize offset = toRenderInline(positionedParent->renderer())->relativePositionedInlineOffset(toRenderBox(renderer()));
810             localPoint += offset;
811         }
812     } else if (parent()) {
813         if (isComposited()) {
814             // FIXME: Composited layers ignore pagination, so about the best we can do is make sure they're offset into the appropriate column.
815             // They won't split across columns properly.
816             LayoutSize columnOffset;
817             parent()->renderer()->adjustForColumns(columnOffset, localPoint);
818             localPoint += columnOffset;
819         }
820
821         LayoutSize scrollOffset = parent()->scrolledContentOffset();
822         localPoint -= scrollOffset;
823     }
824         
825     if (renderer()->isRelPositioned()) {
826         m_relativeOffset = renderer()->relativePositionOffset();
827         localPoint.move(m_relativeOffset);
828     } else {
829         m_relativeOffset = LayoutSize();
830     }
831
832     // FIXME: We'd really like to just get rid of the concept of a layer rectangle and rely on the renderers.
833     localPoint -= inlineBoundingBoxOffset;
834     setLocation(localPoint.x(), localPoint.y());
835 }
836
837 TransformationMatrix RenderLayer::perspectiveTransform() const
838 {
839     if (!renderer()->hasTransform())
840         return TransformationMatrix();
841
842     RenderStyle* style = renderer()->style();
843     if (!style->hasPerspective())
844         return TransformationMatrix();
845
846     // Maybe fetch the perspective from the backing?
847     const LayoutRect borderBox = toRenderBox(renderer())->borderBoxRect();
848     const float boxWidth = borderBox.width();
849     const float boxHeight = borderBox.height();
850
851     float perspectiveOriginX = floatValueForLength(style->perspectiveOriginX(), boxWidth);
852     float perspectiveOriginY = floatValueForLength(style->perspectiveOriginY(), boxHeight);
853
854     // A perspective origin of 0,0 makes the vanishing point in the center of the element.
855     // We want it to be in the top-left, so subtract half the height and width.
856     perspectiveOriginX -= boxWidth / 2.0f;
857     perspectiveOriginY -= boxHeight / 2.0f;
858     
859     TransformationMatrix t;
860     t.translate(perspectiveOriginX, perspectiveOriginY);
861     t.applyPerspective(style->perspective());
862     t.translate(-perspectiveOriginX, -perspectiveOriginY);
863     
864     return t;
865 }
866
867 FloatPoint RenderLayer::perspectiveOrigin() const
868 {
869     if (!renderer()->hasTransform())
870         return FloatPoint();
871
872     const LayoutRect borderBox = toRenderBox(renderer())->borderBoxRect();
873     RenderStyle* style = renderer()->style();
874
875     return FloatPoint(floatValueForLength(style->perspectiveOriginX(), borderBox.width()),
876                       floatValueForLength(style->perspectiveOriginY(), borderBox.height()));
877 }
878
879 RenderLayer* RenderLayer::stackingContext() const
880 {
881     RenderLayer* layer = parent();
882     while (layer && !layer->renderer()->isRenderView() && !layer->renderer()->isRoot() && layer->renderer()->style()->hasAutoZIndex())
883         layer = layer->parent();
884     return layer;
885 }
886
887 static inline bool isPositionedContainer(RenderLayer* layer)
888 {
889     RenderObject* o = layer->renderer();
890     return o->isRenderView() || o->isPositioned() || o->isRelPositioned() || layer->hasTransform();
891 }
892
893 static inline bool isFixedPositionedContainer(RenderLayer* layer)
894 {
895     RenderObject* o = layer->renderer();
896     return o->isRenderView() || layer->hasTransform();
897 }
898
899 RenderLayer* RenderLayer::enclosingPositionedAncestor() const
900 {
901     RenderLayer* curr = parent();
902     while (curr && !isPositionedContainer(curr))
903         curr = curr->parent();
904
905     return curr;
906 }
907
908 RenderLayer* RenderLayer::enclosingScrollableLayer() const
909 {
910     for (RenderObject* nextRenderer = renderer()->parent(); nextRenderer; nextRenderer = nextRenderer->parent()) {
911         if (nextRenderer->isBox() && toRenderBox(nextRenderer)->canBeScrolledAndHasScrollableArea())
912             return nextRenderer->enclosingLayer();
913     }
914
915     return 0;
916 }
917
918 IntRect RenderLayer::scrollableAreaBoundingBox() const
919 {
920     return renderer()->absoluteBoundingBoxRect();
921 }
922
923 RenderLayer* RenderLayer::enclosingTransformedAncestor() const
924 {
925     RenderLayer* curr = parent();
926     while (curr && !curr->renderer()->isRenderView() && !curr->transform())
927         curr = curr->parent();
928
929     return curr;
930 }
931
932 static inline const RenderLayer* compositingContainer(const RenderLayer* layer)
933 {
934     return layer->isNormalFlowOnly() ? layer->parent() : layer->stackingContext();
935 }
936
937 inline bool RenderLayer::shouldRepaintAfterLayout() const
938 {
939 #if USE(ACCELERATED_COMPOSITING)
940     if (m_repaintStatus == NeedsNormalRepaint)
941         return true;
942
943     // Composited layers that were moved during a positioned movement only
944     // layout, don't need to be repainted. They just need to be recomposited.
945     ASSERT(m_repaintStatus == NeedsFullRepaintForPositionedMovementLayout);
946     return !isComposited();
947 #else
948     return true;
949 #endif
950 }
951
952 #if USE(ACCELERATED_COMPOSITING)
953 RenderLayer* RenderLayer::enclosingCompositingLayer(bool includeSelf) const
954 {
955     if (includeSelf && isComposited())
956         return const_cast<RenderLayer*>(this);
957
958     for (const RenderLayer* curr = compositingContainer(this); curr; curr = compositingContainer(curr)) {
959         if (curr->isComposited())
960             return const_cast<RenderLayer*>(curr);
961     }
962          
963     return 0;
964 }
965
966 RenderLayer* RenderLayer::enclosingCompositingLayerForRepaint(bool includeSelf) const
967 {
968     if (includeSelf && isComposited() && !backing()->paintsIntoCompositedAncestor())
969         return const_cast<RenderLayer*>(this);
970
971     for (const RenderLayer* curr = compositingContainer(this); curr; curr = compositingContainer(curr)) {
972         if (curr->isComposited() && !curr->backing()->paintsIntoCompositedAncestor())
973             return const_cast<RenderLayer*>(curr);
974     }
975          
976     return 0;
977 }
978 #endif
979
980 #if ENABLE(CSS_FILTERS)
981 RenderLayer* RenderLayer::enclosingFilterLayer(bool includeSelf) const
982 {
983     const RenderLayer* curr = includeSelf ? this : parent();
984     for (; curr; curr = curr->parent()) {
985         if (curr->requiresFullLayerImageForFilters())
986             return const_cast<RenderLayer*>(curr);
987     }
988     
989     return 0;
990 }
991
992 RenderLayer* RenderLayer::enclosingFilterRepaintLayer() const
993 {
994     for (const RenderLayer* curr = this; curr; curr = curr->parent()) {
995         if ((curr != this && curr->requiresFullLayerImageForFilters()) || curr->isComposited() || curr->isRootLayer())
996             return const_cast<RenderLayer*>(curr);
997     }
998     return 0;
999 }
1000
1001 void RenderLayer::setFilterBackendNeedsRepaintingInRect(const LayoutRect& rect, bool immediate)
1002 {
1003     if (rect.isEmpty())
1004         return;
1005     
1006     LayoutRect rectForRepaint = rect;
1007     
1008 #if ENABLE(CSS_FILTERS)
1009     if (renderer()->style()->hasFilterOutsets()) {
1010         int topOutset;
1011         int rightOutset;
1012         int bottomOutset;
1013         int leftOutset;
1014         renderer()->style()->getFilterOutsets(topOutset, rightOutset, bottomOutset, leftOutset);
1015         rectForRepaint.move(-leftOutset, -topOutset);
1016         rectForRepaint.expand(leftOutset + rightOutset, topOutset + bottomOutset);
1017     }
1018 #endif
1019
1020     RenderLayerFilterInfo* filterInfo = this->filterInfo();
1021     ASSERT(filterInfo);
1022     filterInfo->expandDirtySourceRect(rectForRepaint);
1023     
1024 #if ENABLE(CSS_SHADERS)
1025     ASSERT(filterInfo->renderer());
1026     if (filterInfo->renderer()->hasCustomShaderFilter()) {
1027         // If we have at least one custom shader, we need to update the whole bounding box of the layer, because the
1028         // shader can address any ouput pixel.
1029         // Note: This is only for output rect, so there's no need to expand the dirty source rect.
1030         rectForRepaint.unite(calculateLayerBounds(this, this));
1031     }
1032 #endif
1033     
1034     RenderLayer* parentLayer = enclosingFilterRepaintLayer();
1035     ASSERT(parentLayer);
1036     FloatQuad repaintQuad(rectForRepaint);
1037     LayoutRect parentLayerRect = renderer()->localToContainerQuad(repaintQuad, parentLayer->renderer()).enclosingBoundingBox();
1038     
1039 #if USE(ACCELERATED_COMPOSITING)
1040     if (parentLayer->isComposited()) {
1041         if (!parentLayer->backing()->paintsIntoWindow()) {
1042             parentLayer->setBackingNeedsRepaintInRect(parentLayerRect);
1043             return;
1044         }
1045         // If the painting goes to window, redirect the painting to the parent RenderView.
1046         parentLayer = renderer()->view()->layer();
1047         parentLayerRect = renderer()->localToContainerQuad(repaintQuad, parentLayer->renderer()).enclosingBoundingBox();
1048     }
1049 #endif
1050
1051     if (parentLayer->paintsWithFilters()) {
1052         parentLayer->setFilterBackendNeedsRepaintingInRect(parentLayerRect, immediate);
1053         return;        
1054     }
1055     
1056     if (parentLayer->isRootLayer()) {
1057         RenderView* view = toRenderView(parentLayer->renderer());
1058         view->repaintViewRectangle(parentLayerRect, immediate);
1059         return;
1060     }
1061     
1062     ASSERT_NOT_REACHED();
1063 }
1064 #endif
1065     
1066 RenderLayer* RenderLayer::clippingRoot() const
1067 {
1068 #if USE(ACCELERATED_COMPOSITING)
1069     if (isComposited())
1070         return const_cast<RenderLayer*>(this);
1071 #endif
1072
1073     const RenderLayer* current = this;
1074     while (current) {
1075         if (current->renderer()->isRenderView())
1076             return const_cast<RenderLayer*>(current);
1077
1078         current = compositingContainer(current);
1079         ASSERT(current);
1080         if (current->transform()
1081 #if USE(ACCELERATED_COMPOSITING)
1082             || (current->isComposited() && !current->backing()->paintsIntoCompositedAncestor())
1083 #endif
1084         )
1085             return const_cast<RenderLayer*>(current);
1086     }
1087
1088     ASSERT_NOT_REACHED();
1089     return 0;
1090 }
1091
1092 LayoutPoint RenderLayer::absoluteToContents(const LayoutPoint& absolutePoint) const
1093 {
1094     // We don't use convertToLayerCoords because it doesn't know about transforms
1095     return roundedLayoutPoint(renderer()->absoluteToLocal(absolutePoint, false, true));
1096 }
1097
1098 bool RenderLayer::cannotBlitToWindow() const
1099 {
1100     if (isTransparent() || hasReflection() || hasTransform())
1101         return true;
1102     if (!parent())
1103         return false;
1104     return parent()->cannotBlitToWindow();
1105 }
1106
1107 bool RenderLayer::isTransparent() const
1108 {
1109 #if ENABLE(SVG)
1110     if (renderer()->node() && renderer()->node()->namespaceURI() == SVGNames::svgNamespaceURI)
1111         return false;
1112 #endif
1113     return renderer()->isTransparent() || renderer()->hasMask();
1114 }
1115
1116 RenderLayer* RenderLayer::transparentPaintingAncestor()
1117 {
1118     if (isComposited())
1119         return 0;
1120
1121     for (RenderLayer* curr = parent(); curr; curr = curr->parent()) {
1122         if (curr->isComposited())
1123             return 0;
1124         if (curr->isTransparent())
1125             return curr;
1126     }
1127     return 0;
1128 }
1129
1130 static LayoutRect transparencyClipBox(const RenderLayer*, const RenderLayer* rootLayer, PaintBehavior);
1131
1132 static void expandClipRectForDescendantsAndReflection(LayoutRect& clipRect, const RenderLayer* layer, const RenderLayer* rootLayer, PaintBehavior paintBehavior)
1133 {
1134     // If we have a mask, then the clip is limited to the border box area (and there is
1135     // no need to examine child layers).
1136     if (!layer->renderer()->hasMask()) {
1137         // Note: we don't have to walk z-order lists since transparent elements always establish
1138         // a stacking context.  This means we can just walk the layer tree directly.
1139         for (RenderLayer* curr = layer->firstChild(); curr; curr = curr->nextSibling()) {
1140             if (!layer->reflection() || layer->reflectionLayer() != curr)
1141                 clipRect.unite(transparencyClipBox(curr, rootLayer, paintBehavior));
1142         }
1143     }
1144
1145     // If we have a reflection, then we need to account for that when we push the clip.  Reflect our entire
1146     // current transparencyClipBox to catch all child layers.
1147     // FIXME: Accelerated compositing will eventually want to do something smart here to avoid incorporating this
1148     // size into the parent layer.
1149     if (layer->renderer()->hasReflection()) {
1150         LayoutPoint delta;
1151         layer->convertToLayerCoords(rootLayer, delta);
1152         clipRect.move(-delta.x(), -delta.y());
1153         clipRect.unite(layer->renderBox()->reflectedRect(clipRect));
1154         clipRect.moveBy(delta);
1155     }
1156 }
1157
1158 static LayoutRect transparencyClipBox(const RenderLayer* layer, const RenderLayer* rootLayer, PaintBehavior paintBehavior)
1159 {
1160     // FIXME: Although this function completely ignores CSS-imposed clipping, we did already intersect with the
1161     // paintDirtyRect, and that should cut down on the amount we have to paint.  Still it
1162     // would be better to respect clips.
1163     
1164     if (rootLayer != layer && layer->paintsWithTransform(paintBehavior)) {
1165         // The best we can do here is to use enclosed bounding boxes to establish a "fuzzy" enough clip to encompass
1166         // the transformed layer and all of its children.
1167         LayoutPoint delta;
1168         layer->convertToLayerCoords(rootLayer, delta);
1169
1170         TransformationMatrix transform;
1171         transform.translate(delta.x(), delta.y());
1172         transform = transform * *layer->transform();
1173
1174         LayoutRect clipRect = layer->boundingBox(layer);
1175         expandClipRectForDescendantsAndReflection(clipRect, layer, layer, paintBehavior);
1176         return transform.mapRect(clipRect);
1177     }
1178     
1179     LayoutRect clipRect = layer->boundingBox(rootLayer);
1180     expandClipRectForDescendantsAndReflection(clipRect, layer, rootLayer, paintBehavior);
1181     return clipRect;
1182 }
1183
1184 LayoutRect RenderLayer::paintingExtent(const RenderLayer* rootLayer, const LayoutRect& paintDirtyRect, PaintBehavior paintBehavior)
1185 {
1186     return intersection(transparencyClipBox(this, rootLayer, paintBehavior), paintDirtyRect);
1187 }
1188
1189 void RenderLayer::beginTransparencyLayers(GraphicsContext* context, const RenderLayer* rootLayer, const LayoutRect& paintDirtyRect, PaintBehavior paintBehavior)
1190 {
1191     if (context->paintingDisabled() || (paintsWithTransparency(paintBehavior) && m_usedTransparency))
1192         return;
1193     
1194     RenderLayer* ancestor = transparentPaintingAncestor();
1195     if (ancestor)
1196         ancestor->beginTransparencyLayers(context, rootLayer, paintDirtyRect, paintBehavior);
1197     
1198     if (paintsWithTransparency(paintBehavior)) {
1199         m_usedTransparency = true;
1200         context->save();
1201         LayoutRect clipRect = paintingExtent(rootLayer, paintDirtyRect, paintBehavior);
1202         context->clip(clipRect);
1203         context->beginTransparencyLayer(renderer()->opacity());
1204 #ifdef REVEAL_TRANSPARENCY_LAYERS
1205         context->setFillColor(Color(0.0f, 0.0f, 0.5f, 0.2f), ColorSpaceDeviceRGB);
1206         context->fillRect(clipRect);
1207 #endif
1208     }
1209 }
1210
1211 void* RenderLayer::operator new(size_t sz, RenderArena* renderArena)
1212 {
1213     return renderArena->allocate(sz);
1214 }
1215
1216 void RenderLayer::operator delete(void* ptr, size_t sz)
1217 {
1218     // Stash size where destroy can find it.
1219     *(size_t *)ptr = sz;
1220 }
1221
1222 void RenderLayer::destroy(RenderArena* renderArena)
1223 {
1224     delete this;
1225
1226     // Recover the size left there for us by operator delete and free the memory.
1227     renderArena->free(*(size_t *)this, this);
1228 }
1229
1230 void RenderLayer::addChild(RenderLayer* child, RenderLayer* beforeChild)
1231 {
1232     RenderLayer* prevSibling = beforeChild ? beforeChild->previousSibling() : lastChild();
1233     if (prevSibling) {
1234         child->setPreviousSibling(prevSibling);
1235         prevSibling->setNextSibling(child);
1236         ASSERT(prevSibling != child);
1237     } else
1238         setFirstChild(child);
1239
1240     if (beforeChild) {
1241         beforeChild->setPreviousSibling(child);
1242         child->setNextSibling(beforeChild);
1243         ASSERT(beforeChild != child);
1244     } else
1245         setLastChild(child);
1246
1247     child->setParent(this);
1248
1249     if (child->isNormalFlowOnly())
1250         dirtyNormalFlowList();
1251
1252     if (!child->isNormalFlowOnly() || child->firstChild()) {
1253         // Dirty the z-order list in which we are contained.  The stackingContext() can be null in the
1254         // case where we're building up generated content layers.  This is ok, since the lists will start
1255         // off dirty in that case anyway.
1256         child->dirtyStackingContextZOrderLists();
1257     }
1258
1259     child->updateVisibilityStatus();
1260     if (child->m_hasVisibleContent || child->m_hasVisibleDescendant)
1261         childVisibilityChanged(true);
1262     
1263 #if USE(ACCELERATED_COMPOSITING)
1264     compositor()->layerWasAdded(this, child);
1265 #endif
1266 }
1267
1268 RenderLayer* RenderLayer::removeChild(RenderLayer* oldChild)
1269 {
1270 #if USE(ACCELERATED_COMPOSITING)
1271     if (!renderer()->documentBeingDestroyed())
1272         compositor()->layerWillBeRemoved(this, oldChild);
1273 #endif
1274
1275     // remove the child
1276     if (oldChild->previousSibling())
1277         oldChild->previousSibling()->setNextSibling(oldChild->nextSibling());
1278     if (oldChild->nextSibling())
1279         oldChild->nextSibling()->setPreviousSibling(oldChild->previousSibling());
1280
1281     if (m_first == oldChild)
1282         m_first = oldChild->nextSibling();
1283     if (m_last == oldChild)
1284         m_last = oldChild->previousSibling();
1285
1286     if (oldChild->isNormalFlowOnly())
1287         dirtyNormalFlowList();
1288     if (!oldChild->isNormalFlowOnly() || oldChild->firstChild()) { 
1289         // Dirty the z-order list in which we are contained.  When called via the
1290         // reattachment process in removeOnlyThisLayer, the layer may already be disconnected
1291         // from the main layer tree, so we need to null-check the |stackingContext| value.
1292         oldChild->dirtyStackingContextZOrderLists();
1293     }
1294
1295     oldChild->setPreviousSibling(0);
1296     oldChild->setNextSibling(0);
1297     oldChild->setParent(0);
1298     
1299     oldChild->updateVisibilityStatus();
1300     if (oldChild->m_hasVisibleContent || oldChild->m_hasVisibleDescendant)
1301         childVisibilityChanged(false);
1302     
1303     return oldChild;
1304 }
1305
1306 void RenderLayer::removeOnlyThisLayer()
1307 {
1308     if (!m_parent)
1309         return;
1310
1311     // Mark that we are about to lose our layer. This makes render tree
1312     // walks ignore this layer while we're removing it.
1313     m_renderer->setHasLayer(false);
1314
1315 #if USE(ACCELERATED_COMPOSITING)
1316     compositor()->layerWillBeRemoved(m_parent, this);
1317 #endif
1318
1319     // Dirty the clip rects.
1320     clearClipRectsIncludingDescendants();
1321
1322     RenderLayer* nextSib = nextSibling();
1323     bool hasLayerOffset;
1324     const LayoutPoint offsetFromRootBeforeMove = computeOffsetFromRoot(hasLayerOffset);
1325
1326     // Remove the child reflection layer before moving other child layers.
1327     // The reflection layer should not be moved to the parent.
1328     if (reflection())
1329         removeChild(reflectionLayer());
1330
1331     // Now walk our kids and reattach them to our parent.
1332     RenderLayer* current = m_first;
1333     while (current) {
1334         RenderLayer* next = current->nextSibling();
1335         removeChild(current);
1336         m_parent->addChild(current, nextSib);
1337         current->setRepaintStatus(NeedsFullRepaint);
1338         LayoutPoint offsetFromRoot = offsetFromRootBeforeMove;
1339         // updateLayerPositions depends on hasLayer() already being false for proper layout.
1340         ASSERT(!renderer()->hasLayer());
1341         current->updateLayerPositions(hasLayerOffset ? &offsetFromRoot : 0);
1342         current = next;
1343     }
1344
1345     // Remove us from the parent.
1346     m_parent->removeChild(this);
1347     m_renderer->destroyLayer();
1348 }
1349
1350 void RenderLayer::insertOnlyThisLayer()
1351 {
1352     if (!m_parent && renderer()->parent()) {
1353         // We need to connect ourselves when our renderer() has a parent.
1354         // Find our enclosingLayer and add ourselves.
1355         RenderLayer* parentLayer = renderer()->parent()->enclosingLayer();
1356         ASSERT(parentLayer);
1357         RenderLayer* beforeChild = parentLayer->reflectionLayer() != this ? renderer()->parent()->findNextLayer(parentLayer, renderer()) : 0;
1358         parentLayer->addChild(this, beforeChild);
1359     }
1360
1361     // Remove all descendant layers from the hierarchy and add them to the new position.
1362     for (RenderObject* curr = renderer()->firstChild(); curr; curr = curr->nextSibling())
1363         curr->moveLayers(m_parent, this);
1364
1365     // Clear out all the clip rects.
1366     clearClipRectsIncludingDescendants();
1367 }
1368
1369 void RenderLayer::convertToPixelSnappedLayerCoords(const RenderLayer* ancestorLayer, IntPoint& roundedLocation) const
1370 {
1371     LayoutPoint location = roundedLocation;
1372     convertToLayerCoords(ancestorLayer, location);
1373     roundedLocation = roundedIntPoint(location);
1374 }
1375
1376 void RenderLayer::convertToPixelSnappedLayerCoords(const RenderLayer* ancestorLayer, IntRect& roundedRect) const
1377 {
1378     LayoutRect rect = roundedRect;
1379     convertToLayerCoords(ancestorLayer, rect);
1380     roundedRect = pixelSnappedIntRect(rect);
1381 }
1382
1383 void RenderLayer::convertToLayerCoords(const RenderLayer* ancestorLayer, LayoutPoint& location) const
1384 {
1385     if (ancestorLayer == this)
1386         return;
1387
1388     EPosition position = renderer()->style()->position();
1389     if (position == FixedPosition && (!ancestorLayer || ancestorLayer == renderer()->view()->layer())) {
1390         // If the fixed layer's container is the root, just add in the offset of the view. We can obtain this by calling
1391         // localToAbsolute() on the RenderView.
1392         FloatPoint absPos = renderer()->localToAbsolute(FloatPoint(), true);
1393         location += flooredLayoutSize(absPos);
1394         return;
1395     }
1396  
1397     if (position == FixedPosition) {
1398         // For a fixed layers, we need to walk up to the root to see if there's a fixed position container
1399         // (e.g. a transformed layer). It's an error to call convertToLayerCoords() across a layer with a transform,
1400         // so we should always find the ancestor at or before we find the fixed position container.
1401         RenderLayer* fixedPositionContainerLayer = 0;
1402         bool foundAncestor = false;
1403         for (RenderLayer* currLayer = parent(); currLayer; currLayer = currLayer->parent()) {
1404             if (currLayer == ancestorLayer)
1405                 foundAncestor = true;
1406
1407             if (isFixedPositionedContainer(currLayer)) {
1408                 fixedPositionContainerLayer = currLayer;
1409                 ASSERT_UNUSED(foundAncestor, foundAncestor);
1410                 break;
1411             }
1412         }
1413         
1414         ASSERT(fixedPositionContainerLayer); // We should have hit the RenderView's layer at least.
1415
1416         if (fixedPositionContainerLayer != ancestorLayer) {
1417             LayoutPoint fixedContainerCoords;
1418             convertToLayerCoords(fixedPositionContainerLayer, fixedContainerCoords);
1419
1420             LayoutPoint ancestorCoords;
1421             ancestorLayer->convertToLayerCoords(fixedPositionContainerLayer, ancestorCoords);
1422
1423             location += (fixedContainerCoords - ancestorCoords);
1424             return;
1425         }
1426     }
1427     
1428     RenderLayer* parentLayer;
1429     if (position == AbsolutePosition || position == FixedPosition) {
1430         // Do what enclosingPositionedAncestor() does, but check for ancestorLayer along the way.
1431         parentLayer = parent();
1432         bool foundAncestorFirst = false;
1433         while (parentLayer) {
1434             if (isPositionedContainer(parentLayer))
1435                 break;
1436
1437             if (parentLayer == ancestorLayer) {
1438                 foundAncestorFirst = true;
1439                 break;
1440             }
1441
1442             parentLayer = parentLayer->parent();
1443         }
1444
1445         if (foundAncestorFirst) {
1446             // Found ancestorLayer before the abs. positioned container, so compute offset of both relative
1447             // to enclosingPositionedAncestor and subtract.
1448             RenderLayer* positionedAncestor = parentLayer->enclosingPositionedAncestor();
1449
1450             LayoutPoint thisCoords;
1451             convertToLayerCoords(positionedAncestor, thisCoords);
1452             
1453             LayoutPoint ancestorCoords;
1454             ancestorLayer->convertToLayerCoords(positionedAncestor, ancestorCoords);
1455
1456             location += (thisCoords - ancestorCoords);
1457             return;
1458         }
1459     } else
1460         parentLayer = parent();
1461     
1462     if (!parentLayer)
1463         return;
1464
1465     parentLayer->convertToLayerCoords(ancestorLayer, location);
1466
1467     location += toSize(m_topLeft);
1468 }
1469
1470 void RenderLayer::convertToLayerCoords(const RenderLayer* ancestorLayer, LayoutRect& rect) const
1471 {
1472     LayoutPoint delta;
1473     convertToLayerCoords(ancestorLayer, delta);
1474     rect.move(-delta.x(), -delta.y());
1475 }
1476
1477 static inline int adjustedScrollDelta(int beginningDelta) {
1478     // This implemention matches Firefox's.
1479     // http://mxr.mozilla.org/firefox/source/toolkit/content/widgets/browser.xml#856.
1480     const int speedReducer = 12;
1481
1482     int adjustedDelta = beginningDelta / speedReducer;
1483     if (adjustedDelta > 1)
1484         adjustedDelta = static_cast<int>(adjustedDelta * sqrt(static_cast<double>(adjustedDelta))) - 1;
1485     else if (adjustedDelta < -1)
1486         adjustedDelta = static_cast<int>(adjustedDelta * sqrt(static_cast<double>(-adjustedDelta))) + 1;
1487
1488     return adjustedDelta;
1489 }
1490
1491 void RenderLayer::panScrollFromPoint(const LayoutPoint& sourcePoint) 
1492 {
1493     Frame* frame = renderer()->frame();
1494     if (!frame)
1495         return;
1496     
1497     IntPoint currentMousePosition = frame->eventHandler()->currentMousePosition();
1498     
1499     // We need to check if the current mouse position is out of the window. When the mouse is out of the window, the position is incoherent
1500     static IntPoint previousMousePosition;
1501     if (currentMousePosition.x() < 0 || currentMousePosition.y() < 0)
1502         currentMousePosition = previousMousePosition;
1503     else
1504         previousMousePosition = currentMousePosition;
1505
1506     int xDelta = currentMousePosition.x() - sourcePoint.x();
1507     int yDelta = currentMousePosition.y() - sourcePoint.y();
1508
1509     if (abs(xDelta) <= ScrollView::noPanScrollRadius) // at the center we let the space for the icon
1510         xDelta = 0;
1511     if (abs(yDelta) <= ScrollView::noPanScrollRadius)
1512         yDelta = 0;
1513
1514     scrollByRecursively(adjustedScrollDelta(xDelta), adjustedScrollDelta(yDelta), ScrollOffsetClamped);
1515 }
1516
1517 void RenderLayer::scrollByRecursively(int xDelta, int yDelta, ScrollOffsetClamping clamp)
1518 {
1519     if (!xDelta && !yDelta)
1520         return;
1521
1522     bool restrictedByLineClamp = false;
1523     if (renderer()->parent())
1524         restrictedByLineClamp = !renderer()->parent()->style()->lineClamp().isNone();
1525
1526     if (renderer()->hasOverflowClip() && !restrictedByLineClamp) {
1527         int newOffsetX = scrollXOffset() + xDelta;
1528         int newOffsetY = scrollYOffset() + yDelta;
1529         scrollToOffset(newOffsetX, newOffsetY, clamp);
1530
1531         // If this layer can't do the scroll we ask the next layer up that can scroll to try
1532         int leftToScrollX = newOffsetX - scrollXOffset();
1533         int leftToScrollY = newOffsetY - scrollYOffset();
1534         if ((leftToScrollX || leftToScrollY) && renderer()->parent()) {
1535             if (RenderLayer* scrollableLayer = enclosingScrollableLayer())
1536                 scrollableLayer->scrollByRecursively(leftToScrollX, leftToScrollY);
1537
1538             Frame* frame = renderer()->frame();
1539             if (frame)
1540                 frame->eventHandler()->updateAutoscrollRenderer();
1541         }
1542     } else if (renderer()->view()->frameView()) {
1543         // If we are here, we were called on a renderer that can be programmatically scrolled, but doesn't
1544         // have an overflow clip. Which means that it is a document node that can be scrolled.
1545         renderer()->view()->frameView()->scrollBy(IntSize(xDelta, yDelta));
1546         // FIXME: If we didn't scroll the whole way, do we want to try looking at the frames ownerElement? 
1547         // https://bugs.webkit.org/show_bug.cgi?id=28237
1548     }
1549 }
1550
1551 void RenderLayer::scrollToOffset(int x, int y, ScrollOffsetClamping clamp)
1552 {
1553     if (clamp == ScrollOffsetClamped) {
1554         RenderBox* box = renderBox();
1555         if (!box)
1556             return;
1557
1558         int maxX = scrollWidth() - box->clientWidth();
1559         int maxY = scrollHeight() - box->clientHeight();
1560
1561         x = min(max(x, 0), maxX);
1562         y = min(max(y, 0), maxY);
1563     }
1564
1565     IntPoint newScrollOffset(x, y);
1566     if (newScrollOffset != LayoutPoint(scrollXOffset(), scrollYOffset()))
1567         scrollToOffsetWithoutAnimation(newScrollOffset);
1568 }
1569
1570 void RenderLayer::scrollTo(int x, int y)
1571 {
1572     RenderBox* box = renderBox();
1573     if (!box)
1574         return;
1575
1576     if (box->style()->overflowX() != OMARQUEE) {
1577         // Ensure that the dimensions will be computed if they need to be (for overflow:hidden blocks).
1578         if (m_scrollDimensionsDirty)
1579             computeScrollDimensions();
1580     }
1581     
1582     // FIXME: Eventually, we will want to perform a blit.  For now never
1583     // blit, since the check for blitting is going to be very
1584     // complicated (since it will involve testing whether our layer
1585     // is either occluded by another layer or clipped by an enclosing
1586     // layer or contains fixed backgrounds, etc.).
1587     IntSize newScrollOffset = IntSize(x - scrollOrigin().x(), y - scrollOrigin().y());
1588     if (m_scrollOffset == newScrollOffset)
1589         return;
1590     m_scrollOffset = newScrollOffset;
1591
1592     // Update the positions of our child layers (if needed as only fixed layers should be impacted by a scroll).
1593     // We don't update compositing layers, because we need to do a deep update from the compositing ancestor.
1594     updateLayerPositionsAfterScroll();
1595
1596     RenderView* view = renderer()->view();
1597     
1598     // We should have a RenderView if we're trying to scroll.
1599     ASSERT(view);
1600     if (view) {
1601 #if ENABLE(DASHBOARD_SUPPORT)
1602         // Update dashboard regions, scrolling may change the clip of a
1603         // particular region.
1604         view->frameView()->updateDashboardRegions();
1605 #endif
1606
1607         view->updateWidgetPositions();
1608     }
1609
1610     updateCompositingLayersAfterScroll();
1611
1612     RenderBoxModelObject* repaintContainer = renderer()->containerForRepaint();
1613     Frame* frame = renderer()->frame();
1614     if (frame) {
1615         // The caret rect needs to be invalidated after scrolling
1616         frame->selection()->setCaretRectNeedsUpdate();
1617
1618         FloatQuad quadForFakeMouseMoveEvent = FloatQuad(m_repaintRect);
1619         if (repaintContainer)
1620             quadForFakeMouseMoveEvent = repaintContainer->localToAbsoluteQuad(quadForFakeMouseMoveEvent);
1621         frame->eventHandler()->dispatchFakeMouseMoveEventSoonInQuad(quadForFakeMouseMoveEvent);
1622     }
1623
1624     // Just schedule a full repaint of our object.
1625     if (view)
1626         renderer()->repaintUsingContainer(repaintContainer, m_repaintRect);
1627
1628     // Schedule the scroll DOM event.
1629     if (renderer()->node())
1630         renderer()->node()->document()->eventQueue()->enqueueOrDispatchScrollEvent(renderer()->node(), DocumentEventQueue::ScrollEventElementTarget);
1631 }
1632
1633 void RenderLayer::scrollRectToVisible(const LayoutRect& rect, const ScrollAlignment& alignX, const ScrollAlignment& alignY)
1634 {
1635     RenderLayer* parentLayer = 0;
1636     LayoutRect newRect = rect;
1637
1638     // We may end up propagating a scroll event. It is important that we suspend events until 
1639     // the end of the function since they could delete the layer or the layer's renderer().
1640     FrameView* frameView = renderer()->document()->view();
1641     if (frameView)
1642         frameView->pauseScheduledEvents();
1643
1644     bool restrictedByLineClamp = false;
1645     if (renderer()->parent()) {
1646         parentLayer = renderer()->parent()->enclosingLayer();
1647         restrictedByLineClamp = !renderer()->parent()->style()->lineClamp().isNone();
1648     }
1649
1650     if (renderer()->hasOverflowClip() && !restrictedByLineClamp) {
1651         // Don't scroll to reveal an overflow layer that is restricted by the -webkit-line-clamp property.
1652         // This will prevent us from revealing text hidden by the slider in Safari RSS.
1653         RenderBox* box = renderBox();
1654         ASSERT(box);
1655         FloatPoint absPos = box->localToAbsolute();
1656         absPos.move(box->borderLeft(), box->borderTop());
1657
1658         LayoutRect layerBounds = LayoutRect(absPos.x() + scrollXOffset(), absPos.y() + scrollYOffset(), box->clientWidth(), box->clientHeight());
1659         LayoutRect exposeRect = LayoutRect(rect.x() + scrollXOffset(), rect.y() + scrollYOffset(), rect.width(), rect.height());
1660         LayoutRect r = getRectToExpose(layerBounds, exposeRect, alignX, alignY);
1661         
1662         LayoutUnit adjustedX = r.x() - absPos.x();
1663         LayoutUnit adjustedY = r.y() - absPos.y();
1664         // Adjust offsets if they're outside of the allowable range.
1665         adjustedX = max<LayoutUnit>(0, min(scrollWidth() - layerBounds.width(), adjustedX));
1666         adjustedY = max<LayoutUnit>(0, min(scrollHeight() - layerBounds.height(), adjustedY));
1667
1668         int xOffset = roundToInt(adjustedX);
1669         int yOffset = roundToInt(adjustedY);
1670         
1671         if (xOffset != scrollXOffset() || yOffset != scrollYOffset()) {
1672             int diffX = scrollXOffset();
1673             int diffY = scrollYOffset();
1674             scrollToOffset(xOffset, yOffset);
1675             diffX = scrollXOffset() - diffX;
1676             diffY = scrollYOffset() - diffY;
1677             newRect.setX(rect.x() - diffX);
1678             newRect.setY(rect.y() - diffY);
1679         }
1680     } else if (!parentLayer && renderer()->isBox() && renderBox()->canBeProgramaticallyScrolled()) {
1681         if (frameView) {
1682             Element* ownerElement = 0;
1683             if (renderer()->document())
1684                 ownerElement = renderer()->document()->ownerElement();
1685
1686             if (ownerElement && ownerElement->renderer()) {
1687                 HTMLFrameElement* frameElement = 0;
1688
1689                 if (ownerElement->hasTagName(frameTag) || ownerElement->hasTagName(iframeTag))
1690                     frameElement = static_cast<HTMLFrameElement*>(ownerElement);
1691
1692                 if (frameElement && frameElement->scrollingMode() != ScrollbarAlwaysOff) {
1693                     LayoutRect viewRect = frameView->visibleContentRect();
1694                     LayoutRect exposeRect = getRectToExpose(viewRect, rect, alignX, alignY);
1695
1696                     int xOffset = roundToInt(exposeRect.x());
1697                     int yOffset = roundToInt(exposeRect.y());
1698                     // Adjust offsets if they're outside of the allowable range.
1699                     xOffset = max(0, min(frameView->contentsWidth(), xOffset));
1700                     yOffset = max(0, min(frameView->contentsHeight(), yOffset));
1701
1702                     frameView->setScrollPosition(IntPoint(xOffset, yOffset));
1703                     if (frameView->safeToPropagateScrollToParent()) {
1704                         parentLayer = ownerElement->renderer()->enclosingLayer();
1705                         newRect.setX(rect.x() - frameView->scrollX() + frameView->x());
1706                         newRect.setY(rect.y() - frameView->scrollY() + frameView->y());
1707                     } else
1708                         parentLayer = 0;
1709                 }
1710             } else {
1711                 LayoutRect viewRect = frameView->visibleContentRect();
1712                 LayoutRect r = getRectToExpose(viewRect, rect, alignX, alignY);
1713                 
1714                 frameView->setScrollPosition(roundedIntPoint(r.location()));
1715
1716                 // This is the outermost view of a web page, so after scrolling this view we
1717                 // scroll its container by calling Page::scrollRectIntoView.
1718                 // This only has an effect on the Mac platform in applications
1719                 // that put web views into scrolling containers, such as Mac OS X Mail.
1720                 // The canAutoscroll function in EventHandler also knows about this.
1721                 if (Frame* frame = frameView->frame()) {
1722                     if (Page* page = frame->page())
1723                         page->chrome()->scrollRectIntoView(pixelSnappedIntRect(rect));
1724                 }
1725             }
1726         }
1727     }
1728     
1729     if (parentLayer)
1730         parentLayer->scrollRectToVisible(newRect, alignX, alignY);
1731
1732     if (frameView)
1733         frameView->resumeScheduledEvents();
1734 }
1735
1736 void RenderLayer::updateCompositingLayersAfterScroll()
1737 {
1738 #if USE(ACCELERATED_COMPOSITING)
1739     if (compositor()->inCompositingMode()) {
1740         // Our stacking context is guaranteed to contain all of our descendants that may need
1741         // repositioning, so update compositing layers from there.
1742         if (RenderLayer* compositingAncestor = stackingContext()->enclosingCompositingLayer()) {
1743             if (compositor()->compositingConsultsOverlap())
1744                 compositor()->updateCompositingLayers(CompositingUpdateOnScroll, compositingAncestor);
1745             else {
1746                 bool isUpdateRoot = true;
1747                 compositingAncestor->backing()->updateAfterLayout(RenderLayerBacking::AllDescendants, isUpdateRoot);
1748             }
1749         }
1750     }
1751 #endif
1752 }
1753
1754 LayoutRect RenderLayer::getRectToExpose(const LayoutRect &visibleRect, const LayoutRect &exposeRect, const ScrollAlignment& alignX, const ScrollAlignment& alignY)
1755 {
1756     // Determine the appropriate X behavior.
1757     ScrollBehavior scrollX;
1758     LayoutRect exposeRectX(exposeRect.x(), visibleRect.y(), exposeRect.width(), visibleRect.height());
1759     LayoutUnit intersectWidth = intersection(visibleRect, exposeRectX).width();
1760     if (intersectWidth == exposeRect.width() || intersectWidth >= MIN_INTERSECT_FOR_REVEAL)
1761         // If the rectangle is fully visible, use the specified visible behavior.
1762         // If the rectangle is partially visible, but over a certain threshold,
1763         // then treat it as fully visible to avoid unnecessary horizontal scrolling
1764         scrollX = ScrollAlignment::getVisibleBehavior(alignX);
1765     else if (intersectWidth == visibleRect.width()) {
1766         // If the rect is bigger than the visible area, don't bother trying to center. Other alignments will work.
1767         scrollX = ScrollAlignment::getVisibleBehavior(alignX);
1768         if (scrollX == alignCenter)
1769             scrollX = noScroll;
1770     } else if (intersectWidth > 0)
1771         // If the rectangle is partially visible, but not above the minimum threshold, use the specified partial behavior
1772         scrollX = ScrollAlignment::getPartialBehavior(alignX);
1773     else
1774         scrollX = ScrollAlignment::getHiddenBehavior(alignX);
1775     // If we're trying to align to the closest edge, and the exposeRect is further right
1776     // than the visibleRect, and not bigger than the visible area, then align with the right.
1777     if (scrollX == alignToClosestEdge && exposeRect.maxX() > visibleRect.maxX() && exposeRect.width() < visibleRect.width())
1778         scrollX = alignRight;
1779
1780     // Given the X behavior, compute the X coordinate.
1781     LayoutUnit x;
1782     if (scrollX == noScroll) 
1783         x = visibleRect.x();
1784     else if (scrollX == alignRight)
1785         x = exposeRect.maxX() - visibleRect.width();
1786     else if (scrollX == alignCenter)
1787         x = exposeRect.x() + (exposeRect.width() - visibleRect.width()) / 2;
1788     else
1789         x = exposeRect.x();
1790
1791     // Determine the appropriate Y behavior.
1792     ScrollBehavior scrollY;
1793     LayoutRect exposeRectY(visibleRect.x(), exposeRect.y(), visibleRect.width(), exposeRect.height());
1794     LayoutUnit intersectHeight = intersection(visibleRect, exposeRectY).height();
1795     if (intersectHeight == exposeRect.height())
1796         // If the rectangle is fully visible, use the specified visible behavior.
1797         scrollY = ScrollAlignment::getVisibleBehavior(alignY);
1798     else if (intersectHeight == visibleRect.height()) {
1799         // If the rect is bigger than the visible area, don't bother trying to center. Other alignments will work.
1800         scrollY = ScrollAlignment::getVisibleBehavior(alignY);
1801         if (scrollY == alignCenter)
1802             scrollY = noScroll;
1803     } else if (intersectHeight > 0)
1804         // If the rectangle is partially visible, use the specified partial behavior
1805         scrollY = ScrollAlignment::getPartialBehavior(alignY);
1806     else
1807         scrollY = ScrollAlignment::getHiddenBehavior(alignY);
1808     // If we're trying to align to the closest edge, and the exposeRect is further down
1809     // than the visibleRect, and not bigger than the visible area, then align with the bottom.
1810     if (scrollY == alignToClosestEdge && exposeRect.maxY() > visibleRect.maxY() && exposeRect.height() < visibleRect.height())
1811         scrollY = alignBottom;
1812
1813     // Given the Y behavior, compute the Y coordinate.
1814     LayoutUnit y;
1815     if (scrollY == noScroll) 
1816         y = visibleRect.y();
1817     else if (scrollY == alignBottom)
1818         y = exposeRect.maxY() - visibleRect.height();
1819     else if (scrollY == alignCenter)
1820         y = exposeRect.y() + (exposeRect.height() - visibleRect.height()) / 2;
1821     else
1822         y = exposeRect.y();
1823
1824     return LayoutRect(LayoutPoint(x, y), visibleRect.size());
1825 }
1826
1827 void RenderLayer::autoscroll()
1828 {
1829     Frame* frame = renderer()->frame();
1830     if (!frame)
1831         return;
1832
1833     FrameView* frameView = frame->view();
1834     if (!frameView)
1835         return;
1836
1837 #if ENABLE(DRAG_SUPPORT)
1838     frame->eventHandler()->updateSelectionForMouseDrag();
1839 #endif
1840
1841     IntPoint currentDocumentPosition = frameView->windowToContents(frame->eventHandler()->currentMousePosition());
1842     scrollRectToVisible(LayoutRect(currentDocumentPosition, LayoutSize(1, 1)), ScrollAlignment::alignToEdgeIfNeeded, ScrollAlignment::alignToEdgeIfNeeded);
1843 }
1844
1845 void RenderLayer::resize(const PlatformMouseEvent& evt, const LayoutSize& oldOffset)
1846 {
1847     // FIXME: This should be possible on generated content but is not right now.
1848     if (!inResizeMode() || !renderer()->hasOverflowClip() || !renderer()->node())
1849         return;
1850
1851     // Set the width and height of the shadow ancestor node if there is one.
1852     // This is necessary for textarea elements since the resizable layer is in the shadow content.
1853     Element* element = static_cast<Element*>(renderer()->node()->shadowAncestorNode());
1854     RenderBox* renderer = toRenderBox(element->renderer());
1855
1856     EResize resize = renderer->style()->resize();
1857     if (resize == RESIZE_NONE)
1858         return;
1859
1860     Document* document = element->document();
1861     if (!document->frame()->eventHandler()->mousePressed())
1862         return;
1863
1864     float zoomFactor = renderer->style()->effectiveZoom();
1865
1866     LayoutSize newOffset = offsetFromResizeCorner(document->view()->windowToContents(evt.position()));
1867     newOffset.setWidth(newOffset.width() / zoomFactor);
1868     newOffset.setHeight(newOffset.height() / zoomFactor);
1869     
1870     LayoutSize currentSize = LayoutSize(renderer->width() / zoomFactor, renderer->height() / zoomFactor);
1871     LayoutSize minimumSize = element->minimumSizeForResizing().shrunkTo(currentSize);
1872     element->setMinimumSizeForResizing(minimumSize);
1873     
1874     LayoutSize adjustedOldOffset = LayoutSize(oldOffset.width() / zoomFactor, oldOffset.height() / zoomFactor);
1875     if (renderer->style()->shouldPlaceBlockDirectionScrollbarOnLogicalLeft()) {
1876         newOffset.setWidth(-newOffset.width());
1877         adjustedOldOffset.setWidth(-adjustedOldOffset.width());
1878     }
1879     
1880     LayoutSize difference = (currentSize + newOffset - adjustedOldOffset).expandedTo(minimumSize) - currentSize;
1881
1882     ASSERT(element->isStyledElement());
1883     StyledElement* styledElement = static_cast<StyledElement*>(element);
1884     bool isBoxSizingBorder = renderer->style()->boxSizing() == BORDER_BOX;
1885
1886     if (resize != RESIZE_VERTICAL && difference.width()) {
1887         if (element->isFormControlElement()) {
1888             // Make implicit margins from the theme explicit (see <http://bugs.webkit.org/show_bug.cgi?id=9547>).
1889             styledElement->setInlineStyleProperty(CSSPropertyMarginLeft, String::number(renderer->marginLeft() / zoomFactor) + "px", false);
1890             styledElement->setInlineStyleProperty(CSSPropertyMarginRight, String::number(renderer->marginRight() / zoomFactor) + "px", false);
1891         }
1892         LayoutUnit baseWidth = renderer->width() - (isBoxSizingBorder ? ZERO_LAYOUT_UNIT : renderer->borderAndPaddingWidth());
1893         baseWidth = baseWidth / zoomFactor;
1894         styledElement->setInlineStyleProperty(CSSPropertyWidth, String::number(roundToInt(baseWidth + difference.width())) + "px", false);
1895     }
1896
1897     if (resize != RESIZE_HORIZONTAL && difference.height()) {
1898         if (element->isFormControlElement()) {
1899             // Make implicit margins from the theme explicit (see <http://bugs.webkit.org/show_bug.cgi?id=9547>).
1900             styledElement->setInlineStyleProperty(CSSPropertyMarginTop, String::number(renderer->marginTop() / zoomFactor) + "px", false);
1901             styledElement->setInlineStyleProperty(CSSPropertyMarginBottom, String::number(renderer->marginBottom() / zoomFactor) + "px", false);
1902         }
1903         LayoutUnit baseHeight = renderer->height() - (isBoxSizingBorder ? ZERO_LAYOUT_UNIT : renderer->borderAndPaddingHeight());
1904         baseHeight = baseHeight / zoomFactor;
1905         styledElement->setInlineStyleProperty(CSSPropertyHeight, String::number(roundToInt(baseHeight + difference.height())) + "px", false);
1906     }
1907
1908     document->updateLayout();
1909
1910     // FIXME (Radar 4118564): We should also autoscroll the window as necessary to keep the point under the cursor in view.
1911 }
1912
1913 int RenderLayer::scrollSize(ScrollbarOrientation orientation) const
1914 {
1915     Scrollbar* scrollbar = ((orientation == HorizontalScrollbar) ? m_hBar : m_vBar).get();
1916     return scrollbar ? (scrollbar->totalSize() - scrollbar->visibleSize()) : 0;
1917 }
1918
1919 void RenderLayer::setScrollOffset(const IntPoint& offset)
1920 {
1921     scrollTo(offset.x(), offset.y());
1922 }
1923
1924 int RenderLayer::scrollPosition(Scrollbar* scrollbar) const
1925 {
1926     if (scrollbar->orientation() == HorizontalScrollbar)
1927         return scrollXOffset();
1928     if (scrollbar->orientation() == VerticalScrollbar)
1929         return scrollYOffset();
1930     return 0;
1931 }
1932
1933 IntPoint RenderLayer::scrollPosition() const
1934 {
1935     return scrollOrigin() + m_scrollOffset;
1936 }
1937
1938 IntPoint RenderLayer::minimumScrollPosition() const
1939 {
1940     return scrollOrigin();
1941 }
1942
1943 IntPoint RenderLayer::maximumScrollPosition() const
1944 {
1945     // FIXME: m_scrollSize may not be up-to-date if m_scrollDimensionsDirty is true.
1946     return scrollOrigin() + roundedIntSize(m_scrollSize) - visibleContentRect(true).size();
1947 }
1948
1949 IntRect RenderLayer::visibleContentRect(bool includeScrollbars) const
1950 {
1951     int verticalScrollbarWidth = 0;
1952     int horizontalScrollbarHeight = 0;
1953     if (includeScrollbars) {
1954         verticalScrollbarWidth = (verticalScrollbar() && !verticalScrollbar()->isOverlayScrollbar()) ? verticalScrollbar()->width() : 0;
1955         horizontalScrollbarHeight = (horizontalScrollbar() && !horizontalScrollbar()->isOverlayScrollbar()) ? horizontalScrollbar()->height() : 0;
1956     }
1957     
1958     return IntRect(IntPoint(scrollXOffset(), scrollYOffset()),
1959                    IntSize(max(0, m_layerSize.width() - verticalScrollbarWidth), 
1960                            max(0, m_layerSize.height() - horizontalScrollbarHeight)));
1961 }
1962
1963 IntSize RenderLayer::overhangAmount() const
1964 {
1965     return IntSize();
1966 }
1967
1968 bool RenderLayer::isActive() const
1969 {
1970     Page* page = renderer()->frame()->page();
1971     return page && page->focusController()->isActive();
1972 }
1973
1974 static int cornerStart(const RenderLayer* layer, int minX, int maxX, int thickness)
1975 {
1976     if (layer->renderer()->style()->shouldPlaceBlockDirectionScrollbarOnLogicalLeft())
1977         return minX + layer->renderer()->style()->borderLeftWidth();
1978     return maxX - thickness - layer->renderer()->style()->borderRightWidth();
1979 }
1980
1981 static IntRect cornerRect(const RenderLayer* layer, const IntRect& bounds)
1982 {
1983     int horizontalThickness;
1984     int verticalThickness;
1985     if (!layer->verticalScrollbar() && !layer->horizontalScrollbar()) {
1986         // FIXME: This isn't right.  We need to know the thickness of custom scrollbars
1987         // even when they don't exist in order to set the resizer square size properly.
1988         horizontalThickness = ScrollbarTheme::theme()->scrollbarThickness();
1989         verticalThickness = horizontalThickness;
1990     } else if (layer->verticalScrollbar() && !layer->horizontalScrollbar()) {
1991         horizontalThickness = layer->verticalScrollbar()->width();
1992         verticalThickness = horizontalThickness;
1993     } else if (layer->horizontalScrollbar() && !layer->verticalScrollbar()) {
1994         verticalThickness = layer->horizontalScrollbar()->height();
1995         horizontalThickness = verticalThickness;
1996     } else {
1997         horizontalThickness = layer->verticalScrollbar()->width();
1998         verticalThickness = layer->horizontalScrollbar()->height();
1999     }
2000     return IntRect(cornerStart(layer, bounds.x(), bounds.maxX(), horizontalThickness),
2001                    bounds.maxY() - verticalThickness - layer->renderer()->style()->borderBottomWidth(),
2002                    horizontalThickness, verticalThickness);
2003 }
2004
2005 IntRect RenderLayer::scrollCornerRect() const
2006 {
2007     // We have a scrollbar corner when a scrollbar is visible and not filling the entire length of the box.
2008     // This happens when:
2009     // (a) A resizer is present and at least one scrollbar is present
2010     // (b) Both scrollbars are present.
2011     bool hasHorizontalBar = horizontalScrollbar();
2012     bool hasVerticalBar = verticalScrollbar();
2013     bool hasResizer = renderer()->style()->resize() != RESIZE_NONE;
2014     if ((hasHorizontalBar && hasVerticalBar) || (hasResizer && (hasHorizontalBar || hasVerticalBar)))
2015         return cornerRect(this, renderBox()->pixelSnappedBorderBoxRect());
2016     return IntRect();
2017 }
2018
2019 static IntRect resizerCornerRect(const RenderLayer* layer, const IntRect& bounds)
2020 {
2021     ASSERT(layer->renderer()->isBox());
2022     if (layer->renderer()->style()->resize() == RESIZE_NONE)
2023         return IntRect();
2024     return cornerRect(layer, bounds);
2025 }
2026
2027 IntRect RenderLayer::scrollCornerAndResizerRect() const
2028 {
2029     RenderBox* box = renderBox();
2030     if (!box)
2031         return IntRect();
2032     IntRect scrollCornerAndResizer = scrollCornerRect();
2033     if (scrollCornerAndResizer.isEmpty())
2034         scrollCornerAndResizer = resizerCornerRect(this, box->pixelSnappedBorderBoxRect());
2035     return scrollCornerAndResizer;
2036 }
2037
2038 bool RenderLayer::isScrollCornerVisible() const
2039 {
2040     ASSERT(renderer()->isBox());
2041     return !scrollCornerRect().isEmpty();
2042 }
2043
2044 IntRect RenderLayer::convertFromScrollbarToContainingView(const Scrollbar* scrollbar, const IntRect& scrollbarRect) const
2045 {
2046     RenderView* view = renderer()->view();
2047     if (!view)
2048         return scrollbarRect;
2049
2050     IntRect rect = scrollbarRect;
2051     rect.move(scrollbarOffset(scrollbar));
2052
2053     return view->frameView()->convertFromRenderer(renderer(), rect);
2054 }
2055
2056 IntRect RenderLayer::convertFromContainingViewToScrollbar(const Scrollbar* scrollbar, const IntRect& parentRect) const
2057 {
2058     RenderView* view = renderer()->view();
2059     if (!view)
2060         return parentRect;
2061
2062     IntRect rect = view->frameView()->convertToRenderer(renderer(), parentRect);
2063     rect.move(-scrollbarOffset(scrollbar));
2064     return rect;
2065 }
2066
2067 IntPoint RenderLayer::convertFromScrollbarToContainingView(const Scrollbar* scrollbar, const IntPoint& scrollbarPoint) const
2068 {
2069     RenderView* view = renderer()->view();
2070     if (!view)
2071         return scrollbarPoint;
2072
2073     IntPoint point = scrollbarPoint;
2074     point.move(scrollbarOffset(scrollbar));
2075     return view->frameView()->convertFromRenderer(renderer(), point);
2076 }
2077
2078 IntPoint RenderLayer::convertFromContainingViewToScrollbar(const Scrollbar* scrollbar, const IntPoint& parentPoint) const
2079 {
2080     RenderView* view = renderer()->view();
2081     if (!view)
2082         return parentPoint;
2083
2084     IntPoint point = view->frameView()->convertToRenderer(renderer(), parentPoint);
2085
2086     point.move(-scrollbarOffset(scrollbar));
2087     return point;
2088 }
2089
2090 IntSize RenderLayer::contentsSize() const
2091 {
2092     return IntSize(scrollWidth(), scrollHeight());
2093 }
2094
2095 int RenderLayer::visibleHeight() const
2096 {
2097     return m_layerSize.height();
2098 }
2099
2100 int RenderLayer::visibleWidth() const
2101 {
2102     return m_layerSize.width();
2103 }
2104
2105 bool RenderLayer::shouldSuspendScrollAnimations() const
2106 {
2107     RenderView* view = renderer()->view();
2108     if (!view)
2109         return true;
2110     return view->frameView()->shouldSuspendScrollAnimations();
2111 }
2112
2113 bool RenderLayer::isOnActivePage() const
2114 {
2115     return !m_renderer->document()->inPageCache();
2116 }
2117
2118 IntPoint RenderLayer::currentMousePosition() const
2119 {
2120     return renderer()->frame() ? renderer()->frame()->eventHandler()->currentMousePosition() : IntPoint();
2121 }
2122
2123 LayoutUnit RenderLayer::verticalScrollbarStart(int minX, int maxX) const
2124 {
2125     const RenderBox* box = renderBox();
2126     if (renderer()->style()->shouldPlaceBlockDirectionScrollbarOnLogicalLeft())
2127         return minX + box->borderLeft();
2128     return maxX - box->borderRight() - m_vBar->width();
2129 }
2130
2131 LayoutUnit RenderLayer::horizontalScrollbarStart(int minX) const
2132 {
2133     const RenderBox* box = renderBox();
2134     int x = minX + box->borderLeft();
2135     if (renderer()->style()->shouldPlaceBlockDirectionScrollbarOnLogicalLeft())
2136         x += m_vBar ? m_vBar->width() : resizerCornerRect(this, box->pixelSnappedBorderBoxRect()).width();
2137     return x;
2138 }
2139
2140 IntSize RenderLayer::scrollbarOffset(const Scrollbar* scrollbar) const
2141 {
2142     RenderBox* box = renderBox();
2143
2144     if (scrollbar == m_vBar.get())
2145         return IntSize(verticalScrollbarStart(0, box->width()), box->borderTop());
2146
2147     if (scrollbar == m_hBar.get())
2148         return IntSize(horizontalScrollbarStart(0), box->height() - box->borderBottom() - scrollbar->height());
2149     
2150     ASSERT_NOT_REACHED();
2151     return IntSize();
2152 }
2153
2154 void RenderLayer::invalidateScrollbarRect(Scrollbar* scrollbar, const IntRect& rect)
2155 {
2156 #if USE(ACCELERATED_COMPOSITING)
2157     if (scrollbar == m_vBar.get()) {
2158         if (GraphicsLayer* layer = layerForVerticalScrollbar()) {
2159             layer->setNeedsDisplayInRect(rect);
2160             return;
2161         }
2162     } else {
2163         if (GraphicsLayer* layer = layerForHorizontalScrollbar()) {
2164             layer->setNeedsDisplayInRect(rect);
2165             return;
2166         }
2167     }
2168 #endif
2169     IntRect scrollRect = rect;
2170     RenderBox* box = renderBox();
2171     ASSERT(box);
2172     if (scrollbar == m_vBar.get())
2173         scrollRect.move(verticalScrollbarStart(0, box->width()), box->borderTop());
2174     else
2175         scrollRect.move(horizontalScrollbarStart(0), box->height() - box->borderBottom() - scrollbar->height());
2176     renderer()->repaintRectangle(scrollRect);
2177 }
2178
2179 void RenderLayer::invalidateScrollCornerRect(const IntRect& rect)
2180 {
2181 #if USE(ACCELERATED_COMPOSITING)
2182     if (GraphicsLayer* layer = layerForScrollCorner()) {
2183         layer->setNeedsDisplayInRect(rect);
2184         return;
2185     }
2186 #endif
2187     if (m_scrollCorner)
2188         m_scrollCorner->repaintRectangle(rect);
2189     if (m_resizer)
2190         m_resizer->repaintRectangle(rect);
2191 }
2192
2193 PassRefPtr<Scrollbar> RenderLayer::createScrollbar(ScrollbarOrientation orientation)
2194 {
2195     RefPtr<Scrollbar> widget;
2196     RenderObject* actualRenderer = renderer()->node() ? renderer()->node()->shadowAncestorNode()->renderer() : renderer();
2197     bool hasCustomScrollbarStyle = actualRenderer->isBox() && actualRenderer->style()->hasPseudoStyle(SCROLLBAR);
2198     if (hasCustomScrollbarStyle)
2199         widget = RenderScrollbar::createCustomScrollbar(this, orientation, actualRenderer->node());
2200     else {
2201         widget = Scrollbar::createNativeScrollbar(this, orientation, RegularScrollbar);
2202         if (orientation == HorizontalScrollbar)
2203             didAddHorizontalScrollbar(widget.get());
2204         else 
2205             didAddVerticalScrollbar(widget.get());
2206     }
2207     renderer()->document()->view()->addChild(widget.get());        
2208     return widget.release();
2209 }
2210
2211 void RenderLayer::destroyScrollbar(ScrollbarOrientation orientation)
2212 {
2213     RefPtr<Scrollbar>& scrollbar = orientation == HorizontalScrollbar ? m_hBar : m_vBar;
2214     if (scrollbar) {
2215         if (orientation == HorizontalScrollbar)
2216             willRemoveHorizontalScrollbar(scrollbar.get());
2217         else
2218             willRemoveVerticalScrollbar(scrollbar.get());
2219
2220         scrollbar->removeFromParent();
2221         scrollbar->disconnectFromScrollableArea();
2222         scrollbar = 0;
2223     }
2224 }
2225
2226 bool RenderLayer::scrollsOverflow() const
2227 {
2228     if (!renderer()->isBox())
2229         return false;
2230
2231     return toRenderBox(renderer())->scrollsOverflow();
2232 }
2233
2234 bool RenderLayer::allowsScrolling() const
2235 {
2236     return (m_hBar && m_hBar->enabled()) || (m_vBar && m_vBar->enabled());
2237 }
2238
2239 void RenderLayer::setHasHorizontalScrollbar(bool hasScrollbar)
2240 {
2241     if (hasScrollbar == hasHorizontalScrollbar())
2242         return;
2243
2244     if (hasScrollbar)
2245         m_hBar = createScrollbar(HorizontalScrollbar);
2246     else
2247         destroyScrollbar(HorizontalScrollbar);
2248
2249     // Destroying or creating one bar can cause our scrollbar corner to come and go.  We need to update the opposite scrollbar's style.
2250     if (m_hBar)
2251         m_hBar->styleChanged();
2252     if (m_vBar)
2253         m_vBar->styleChanged();
2254
2255 #if ENABLE(DASHBOARD_SUPPORT)
2256     // Force an update since we know the scrollbars have changed things.
2257     if (renderer()->document()->hasDashboardRegions())
2258         renderer()->document()->setDashboardRegionsDirty(true);
2259 #endif
2260 }
2261
2262 void RenderLayer::setHasVerticalScrollbar(bool hasScrollbar)
2263 {
2264     if (hasScrollbar == hasVerticalScrollbar())
2265         return;
2266
2267     if (hasScrollbar)
2268         m_vBar = createScrollbar(VerticalScrollbar);
2269     else
2270         destroyScrollbar(VerticalScrollbar);
2271
2272      // Destroying or creating one bar can cause our scrollbar corner to come and go.  We need to update the opposite scrollbar's style.
2273     if (m_hBar)
2274         m_hBar->styleChanged();
2275     if (m_vBar)
2276         m_vBar->styleChanged();
2277
2278 #if ENABLE(DASHBOARD_SUPPORT)
2279     // Force an update since we know the scrollbars have changed things.
2280     if (renderer()->document()->hasDashboardRegions())
2281         renderer()->document()->setDashboardRegionsDirty(true);
2282 #endif
2283 }
2284
2285 ScrollableArea* RenderLayer::enclosingScrollableArea() const
2286 {
2287     if (RenderLayer* scrollableLayer = enclosingScrollableLayer())
2288         return scrollableLayer;
2289
2290     // FIXME: We should return the frame view here (or possibly an ancestor frame view,
2291     // if the frame view isn't scrollable.
2292     return 0;
2293 }
2294
2295 int RenderLayer::verticalScrollbarWidth(OverlayScrollbarSizeRelevancy relevancy) const
2296 {
2297     if (!m_vBar || (m_vBar->isOverlayScrollbar() && relevancy == IgnoreOverlayScrollbarSize))
2298         return 0;
2299     return m_vBar->width();
2300 }
2301
2302 int RenderLayer::horizontalScrollbarHeight(OverlayScrollbarSizeRelevancy relevancy) const
2303 {
2304     if (!m_hBar || (m_hBar->isOverlayScrollbar() && relevancy == IgnoreOverlayScrollbarSize))
2305         return 0;
2306     return m_hBar->height();
2307 }
2308
2309 IntSize RenderLayer::offsetFromResizeCorner(const IntPoint& absolutePoint) const
2310 {
2311     // Currently the resize corner is either the bottom right corner or the bottom left corner.
2312     // FIXME: This assumes the location is 0, 0. Is this guaranteed to always be the case?
2313     IntSize elementSize = size();
2314     if (renderer()->style()->shouldPlaceBlockDirectionScrollbarOnLogicalLeft())
2315         elementSize.setWidth(0);
2316     IntPoint resizerPoint = toPoint(elementSize);
2317     IntPoint localPoint = roundedIntPoint(absoluteToContents(absolutePoint));
2318     return localPoint - resizerPoint;
2319 }
2320
2321 bool RenderLayer::hasOverflowControls() const
2322 {
2323     return m_hBar || m_vBar || m_scrollCorner || renderer()->style()->resize() != RESIZE_NONE;
2324 }
2325
2326 void RenderLayer::positionOverflowControls(const IntSize& offsetFromLayer)
2327 {
2328     if (!m_hBar && !m_vBar && (!renderer()->hasOverflowClip() || renderer()->style()->resize() == RESIZE_NONE))
2329         return;
2330     
2331     RenderBox* box = renderBox();
2332     if (!box)
2333         return;
2334
2335     const IntRect borderBox = box->pixelSnappedBorderBoxRect();
2336     const IntRect& scrollCorner = scrollCornerRect();
2337     IntRect absBounds(borderBox.location() + offsetFromLayer, borderBox.size());
2338     if (m_vBar)
2339         m_vBar->setFrameRect(IntRect(verticalScrollbarStart(absBounds.x(), absBounds.maxX()),
2340                                      absBounds.y() + box->borderTop(),
2341                                      m_vBar->width(),
2342                                      absBounds.height() - (box->borderTop() + box->borderBottom()) - scrollCorner.height()));
2343
2344     if (m_hBar)
2345         m_hBar->setFrameRect(IntRect(horizontalScrollbarStart(absBounds.x()),
2346                                      absBounds.maxY() - box->borderBottom() - m_hBar->height(),
2347                                      absBounds.width() - (box->borderLeft() + box->borderRight()) - scrollCorner.width(),
2348                                      m_hBar->height()));
2349
2350 #if USE(ACCELERATED_COMPOSITING)
2351     if (GraphicsLayer* layer = layerForHorizontalScrollbar()) {
2352         if (m_hBar) {
2353             layer->setPosition(m_hBar->frameRect().location() - offsetFromLayer);
2354             layer->setSize(m_hBar->frameRect().size());
2355         }
2356         layer->setDrawsContent(m_hBar);
2357     }
2358     if (GraphicsLayer* layer = layerForVerticalScrollbar()) {
2359         if (m_vBar) {
2360             layer->setPosition(m_vBar->frameRect().location() - offsetFromLayer);
2361             layer->setSize(m_vBar->frameRect().size());
2362         }
2363         layer->setDrawsContent(m_vBar);
2364     }
2365
2366     if (GraphicsLayer* layer = layerForScrollCorner()) {
2367         const LayoutRect& scrollCornerAndResizer = scrollCornerAndResizerRect();
2368         layer->setPosition(scrollCornerAndResizer.location());
2369         layer->setSize(scrollCornerAndResizer.size());
2370         layer->setDrawsContent(!scrollCornerAndResizer.isEmpty());
2371     }
2372 #endif
2373
2374     if (m_scrollCorner)
2375         m_scrollCorner->setFrameRect(scrollCorner);
2376     if (m_resizer)
2377         m_resizer->setFrameRect(resizerCornerRect(this, borderBox));
2378 }
2379
2380 int RenderLayer::scrollWidth() const
2381 {
2382     ASSERT(renderBox());
2383     if (m_scrollDimensionsDirty)
2384         const_cast<RenderLayer*>(this)->computeScrollDimensions();
2385     return snapSizeToPixel(m_scrollSize.width(), renderBox()->clientLeft());
2386 }
2387
2388 int RenderLayer::scrollHeight() const
2389 {
2390     ASSERT(renderBox());
2391     if (m_scrollDimensionsDirty)
2392         const_cast<RenderLayer*>(this)->computeScrollDimensions();
2393     return snapSizeToPixel(m_scrollSize.height(), renderBox()->clientTop());
2394 }
2395
2396 LayoutUnit RenderLayer::overflowTop() const
2397 {
2398     RenderBox* box = renderBox();
2399     LayoutRect overflowRect(box->layoutOverflowRect());
2400     box->flipForWritingMode(overflowRect);
2401     return overflowRect.y();
2402 }
2403
2404 LayoutUnit RenderLayer::overflowBottom() const
2405 {
2406     RenderBox* box = renderBox();
2407     LayoutRect overflowRect(box->layoutOverflowRect());
2408     box->flipForWritingMode(overflowRect);
2409     return overflowRect.maxY();
2410 }
2411
2412 LayoutUnit RenderLayer::overflowLeft() const
2413 {
2414     RenderBox* box = renderBox();
2415     LayoutRect overflowRect(box->layoutOverflowRect());
2416     box->flipForWritingMode(overflowRect);
2417     return overflowRect.x();
2418 }
2419
2420 LayoutUnit RenderLayer::overflowRight() const
2421 {
2422     RenderBox* box = renderBox();
2423     LayoutRect overflowRect(box->layoutOverflowRect());
2424     box->flipForWritingMode(overflowRect);
2425     return overflowRect.maxX();
2426 }
2427
2428 void RenderLayer::computeScrollDimensions()
2429 {
2430     RenderBox* box = renderBox();
2431     ASSERT(box);
2432
2433     m_scrollDimensionsDirty = false;
2434
2435     m_scrollOverflow.setWidth(overflowLeft() - box->borderLeft());
2436     m_scrollOverflow.setHeight(overflowTop() - box->borderTop());
2437
2438     m_scrollSize.setWidth(overflowRight() - overflowLeft());
2439     m_scrollSize.setHeight(overflowBottom() - overflowTop());
2440
2441     setScrollOrigin(IntPoint(-m_scrollOverflow.width(), -m_scrollOverflow.height()));
2442 }
2443
2444 bool RenderLayer::hasHorizontalOverflow() const
2445 {
2446     ASSERT(!m_scrollDimensionsDirty);
2447
2448     return scrollWidth() > renderBox()->pixelSnappedClientWidth();
2449 }
2450
2451 bool RenderLayer::hasVerticalOverflow() const
2452 {
2453     ASSERT(!m_scrollDimensionsDirty);
2454
2455     return scrollHeight() > renderBox()->pixelSnappedClientHeight();
2456 }
2457
2458 void RenderLayer::updateScrollbarsAfterLayout()
2459 {
2460     RenderBox* box = renderBox();
2461     ASSERT(box);
2462
2463     bool hasHorizontalOverflow = this->hasHorizontalOverflow();
2464     bool hasVerticalOverflow = this->hasVerticalOverflow();
2465
2466     // overflow:scroll should just enable/disable.
2467     if (m_hBar && renderer()->style()->overflowX() == OSCROLL)
2468         m_hBar->setEnabled(hasHorizontalOverflow);
2469     if (m_vBar && renderer()->style()->overflowY() == OSCROLL)
2470         m_vBar->setEnabled(hasVerticalOverflow);
2471
2472     // overflow:auto may need to lay out again if scrollbars got added/removed.
2473     bool autoHorizontalScrollBarChanged = box->hasAutoHorizontalScrollbar() && (hasHorizontalScrollbar() != hasHorizontalOverflow);
2474     bool autoVerticalScrollBarChanged = box->hasAutoVerticalScrollbar() && (hasVerticalScrollbar() != hasVerticalOverflow);
2475
2476     if (autoHorizontalScrollBarChanged || autoVerticalScrollBarChanged) {
2477         if (box->hasAutoHorizontalScrollbar())
2478             setHasHorizontalScrollbar(hasHorizontalOverflow);
2479         if (box->hasAutoVerticalScrollbar())
2480             setHasVerticalScrollbar(hasVerticalOverflow);
2481
2482 #if ENABLE(DASHBOARD_SUPPORT)
2483         // Force an update since we know the scrollbars have changed things.
2484         if (renderer()->document()->hasDashboardRegions())
2485             renderer()->document()->setDashboardRegionsDirty(true);
2486 #endif
2487
2488         renderer()->repaint();
2489
2490         if (renderer()->style()->overflowX() == OAUTO || renderer()->style()->overflowY() == OAUTO) {
2491             if (!m_inOverflowRelayout) {
2492                 // Our proprietary overflow: overlay value doesn't trigger a layout.
2493                 m_inOverflowRelayout = true;
2494                 renderer()->setNeedsLayout(true, MarkOnlyThis);
2495                 if (renderer()->isRenderBlock()) {
2496                     RenderBlock* block = toRenderBlock(renderer());
2497                     block->scrollbarsChanged(autoHorizontalScrollBarChanged, autoVerticalScrollBarChanged);
2498                     block->layoutBlock(true);
2499                 } else
2500                     renderer()->layout();
2501                 m_inOverflowRelayout = false;
2502             }
2503         }
2504     }
2505
2506     // Set up the range (and page step/line step).
2507     if (m_hBar) {
2508         int clientWidth = box->pixelSnappedClientWidth();
2509         int pageStep = max(max<int>(clientWidth * Scrollbar::minFractionToStepWhenPaging(), clientWidth - Scrollbar::maxOverlapBetweenPages()), 1);
2510         m_hBar->setSteps(Scrollbar::pixelsPerLineStep(), pageStep);
2511         m_hBar->setProportion(clientWidth, m_scrollSize.width());
2512     }
2513     if (m_vBar) {
2514         int clientHeight = box->pixelSnappedClientHeight();
2515         int pageStep = max(max<int>(clientHeight * Scrollbar::minFractionToStepWhenPaging(), clientHeight - Scrollbar::maxOverlapBetweenPages()), 1);
2516         m_vBar->setSteps(Scrollbar::pixelsPerLineStep(), pageStep);
2517         m_vBar->setProportion(clientHeight, m_scrollSize.height());
2518     }
2519
2520     updateScrollableAreaSet((hasHorizontalOverflow || hasVerticalOverflow) && scrollsOverflow());
2521 }
2522
2523 void RenderLayer::updateScrollInfoAfterLayout()
2524 {
2525     RenderBox* box = renderBox();
2526     if (!box)
2527         return;
2528
2529     m_scrollDimensionsDirty = true;
2530     IntSize scrollOffsetOriginal(scrollXOffset(), scrollYOffset());
2531
2532     computeScrollDimensions();
2533
2534     if (box->style()->overflowX() != OMARQUEE) {
2535         // Layout may cause us to be at an invalid scroll position. In this case we need
2536         // to pull our scroll offsets back to the max (or push them up to the min).
2537         int newX = max(0, min<int>(scrollXOffset(), scrollWidth() - box->clientWidth()));
2538         int newY = max(0, min<int>(scrollYOffset(), scrollHeight() - box->clientHeight()));
2539         if (newX != scrollXOffset() || newY != scrollYOffset())
2540             scrollToOffset(newX, newY);
2541     }
2542
2543     updateScrollbarsAfterLayout();
2544
2545     if (scrollOffsetOriginal != scrollOffset())
2546         scrollToOffsetWithoutAnimation(IntPoint(scrollXOffset(), scrollYOffset()));
2547 }
2548
2549 void RenderLayer::paintOverflowControls(GraphicsContext* context, const IntPoint& paintOffset, const IntRect& damageRect, bool paintingOverlayControls)
2550 {
2551     // Don't do anything if we have no overflow.
2552     if (!renderer()->hasOverflowClip())
2553         return;
2554
2555     // Overlay scrollbars paint in a second pass through the layer tree so that they will paint
2556     // on top of everything else. If this is the normal painting pass, paintingOverlayControls
2557     // will be false, and we should just tell the root layer that there are overlay scrollbars
2558     // that need to be painted. That will cause the second pass through the layer tree to run,
2559     // and we'll paint the scrollbars then. In the meantime, cache tx and ty so that the 
2560     // second pass doesn't need to re-enter the RenderTree to get it right.
2561     if (hasOverlayScrollbars() && !paintingOverlayControls) {
2562         m_cachedOverlayScrollbarOffset = paintOffset;
2563 #if USE(ACCELERATED_COMPOSITING)
2564         // It's not necessary to do the second pass if the scrollbars paint into layers.
2565         if ((m_hBar && layerForHorizontalScrollbar()) || (m_vBar && layerForVerticalScrollbar()))
2566             return;
2567 #endif
2568         RenderView* renderView = renderer()->view();
2569         renderView->layer()->setContainsDirtyOverlayScrollbars(true);
2570         renderView->frameView()->setContainsScrollableAreaWithOverlayScrollbars(true);
2571         return;
2572     }
2573
2574     // This check is required to avoid painting custom CSS scrollbars twice.
2575     if (paintingOverlayControls && !hasOverlayScrollbars())
2576         return;
2577
2578     IntPoint adjustedPaintOffset = paintOffset;
2579     if (paintingOverlayControls)
2580         adjustedPaintOffset = m_cachedOverlayScrollbarOffset;
2581
2582     // Move the scrollbar widgets if necessary.  We normally move and resize widgets during layout, but sometimes
2583     // widgets can move without layout occurring (most notably when you scroll a document that
2584     // contains fixed positioned elements).
2585     positionOverflowControls(toSize(adjustedPaintOffset));
2586
2587     // Now that we're sure the scrollbars are in the right place, paint them.
2588     if (m_hBar
2589 #if USE(ACCELERATED_COMPOSITING)
2590         && !layerForHorizontalScrollbar()
2591 #endif
2592               )
2593         m_hBar->paint(context, damageRect);
2594     if (m_vBar
2595 #if USE(ACCELERATED_COMPOSITING)
2596         && !layerForVerticalScrollbar()
2597 #endif
2598               )
2599         m_vBar->paint(context, damageRect);
2600
2601 #if USE(ACCELERATED_COMPOSITING)
2602     if (layerForScrollCorner())
2603         return;
2604 #endif
2605
2606     // We fill our scroll corner with white if we have a scrollbar that doesn't run all the way up to the
2607     // edge of the box.
2608     paintScrollCorner(context, adjustedPaintOffset, damageRect);
2609     
2610     // Paint our resizer last, since it sits on top of the scroll corner.
2611     paintResizer(context, adjustedPaintOffset, damageRect);
2612 }
2613
2614 void RenderLayer::paintScrollCorner(GraphicsContext* context, const IntPoint& paintOffset, const IntRect& damageRect)
2615 {
2616     RenderBox* box = renderBox();
2617     ASSERT(box);
2618
2619     IntRect absRect = scrollCornerRect();
2620     absRect.moveBy(paintOffset);
2621     if (!absRect.intersects(damageRect))
2622         return;
2623
2624     if (context->updatingControlTints()) {
2625         updateScrollCornerStyle();
2626         return;
2627     }
2628
2629     if (m_scrollCorner) {
2630         m_scrollCorner->paintIntoRect(context, paintOffset, absRect);
2631         return;
2632     }
2633
2634     // We don't want to paint white if we have overlay scrollbars, since we need
2635     // to see what is behind it.
2636     if (!hasOverlayScrollbars())
2637         context->fillRect(absRect, Color::white, box->style()->colorSpace());
2638 }
2639
2640 void RenderLayer::drawPlatformResizerImage(GraphicsContext* context, IntRect resizerCornerRect)
2641 {
2642     float deviceScaleFactor = WebCore::deviceScaleFactor(renderer()->frame());
2643
2644     RefPtr<Image> resizeCornerImage;
2645     IntSize cornerResizerSize;
2646     if (deviceScaleFactor >= 2) {
2647         DEFINE_STATIC_LOCAL(Image*, resizeCornerImageHiRes, (Image::loadPlatformResource("textAreaResizeCorner@2x").leakRef()));
2648         resizeCornerImage = resizeCornerImageHiRes;
2649         cornerResizerSize = resizeCornerImage->size();
2650         cornerResizerSize.scale(0.5f);
2651     } else {
2652         DEFINE_STATIC_LOCAL(Image*, resizeCornerImageLoRes, (Image::loadPlatformResource("textAreaResizeCorner").leakRef()));
2653         resizeCornerImage = resizeCornerImageLoRes;
2654         cornerResizerSize = resizeCornerImage->size();
2655     }
2656
2657     if (renderer()->style()->shouldPlaceBlockDirectionScrollbarOnLogicalLeft()) {
2658         context->save();
2659         context->translate(resizerCornerRect.x() + cornerResizerSize.width(), resizerCornerRect.y() + resizerCornerRect.height() - cornerResizerSize.height());
2660         context->scale(FloatSize(-1.0, 1.0));
2661         context->drawImage(resizeCornerImage.get(), renderer()->style()->colorSpace(), IntRect(IntPoint(), cornerResizerSize));
2662         context->restore();
2663         return;
2664     }
2665     IntRect imageRect(resizerCornerRect.maxXMaxYCorner() - cornerResizerSize, cornerResizerSize);
2666     context->drawImage(resizeCornerImage.get(), renderer()->style()->colorSpace(), imageRect);
2667 }
2668
2669 void RenderLayer::paintResizer(GraphicsContext* context, const IntPoint& paintOffset, const IntRect& damageRect)
2670 {
2671     if (renderer()->style()->resize() == RESIZE_NONE)
2672         return;
2673
2674     RenderBox* box = renderBox();
2675     ASSERT(box);
2676
2677     IntRect absRect = resizerCornerRect(this, box->pixelSnappedBorderBoxRect());
2678     absRect.moveBy(paintOffset);
2679     if (!absRect.intersects(damageRect))
2680         return;
2681
2682     if (context->updatingControlTints()) {
2683         updateResizerStyle();
2684         return;
2685     }
2686     
2687     if (m_resizer) {
2688         m_resizer->paintIntoRect(context, paintOffset, absRect);
2689         return;
2690     }
2691
2692     drawPlatformResizerImage(context, absRect);
2693
2694     // Draw a frame around the resizer (1px grey line) if there are any scrollbars present.
2695     // Clipping will exclude the right and bottom edges of this frame.
2696     if (!hasOverlayScrollbars() && (m_vBar || m_hBar)) {
2697         GraphicsContextStateSaver stateSaver(*context);
2698         context->clip(absRect);
2699         IntRect largerCorner = absRect;
2700         largerCorner.setSize(IntSize(largerCorner.width() + 1, largerCorner.height() + 1));
2701         context->setStrokeColor(Color(makeRGB(217, 217, 217)), ColorSpaceDeviceRGB);
2702         context->setStrokeThickness(1.0f);
2703         context->setFillColor(Color::transparent, ColorSpaceDeviceRGB);
2704         context->drawRect(largerCorner);
2705     }
2706 }
2707
2708 bool RenderLayer::isPointInResizeControl(const IntPoint& absolutePoint) const
2709 {
2710     if (!renderer()->hasOverflowClip() || renderer()->style()->resize() == RESIZE_NONE)
2711         return false;
2712     
2713     RenderBox* box = renderBox();
2714     ASSERT(box);
2715
2716     IntPoint localPoint = roundedIntPoint(absoluteToContents(absolutePoint));
2717
2718     IntRect localBounds(0, 0, box->pixelSnappedWidth(), box->pixelSnappedHeight());
2719     return resizerCornerRect(this, localBounds).contains(localPoint);
2720 }
2721     
2722 bool RenderLayer::hitTestOverflowControls(HitTestResult& result, const IntPoint& localPoint)
2723 {
2724     if (!m_hBar && !m_vBar && (!renderer()->hasOverflowClip() || renderer()->style()->resize() == RESIZE_NONE))
2725         return false;
2726
2727     RenderBox* box = renderBox();
2728     ASSERT(box);
2729     
2730     IntRect resizeControlRect;
2731     if (renderer()->style()->resize() != RESIZE_NONE) {
2732         resizeControlRect = resizerCornerRect(this, box->pixelSnappedBorderBoxRect());
2733         if (resizeControlRect.contains(localPoint))
2734             return true;
2735     }
2736
2737     int resizeControlSize = max(resizeControlRect.height(), 0);
2738
2739     if (m_vBar && m_vBar->shouldParticipateInHitTesting()) {
2740         LayoutRect vBarRect(verticalScrollbarStart(0, box->width()),
2741                             box->borderTop(),
2742                             m_vBar->width(),
2743                             box->height() - (box->borderTop() + box->borderBottom()) - (m_hBar ? m_hBar->height() : resizeControlSize));
2744         if (vBarRect.contains(localPoint)) {
2745             result.setScrollbar(m_vBar.get());
2746             return true;
2747         }
2748     }
2749
2750     resizeControlSize = max(resizeControlRect.width(), 0);
2751     if (m_hBar && m_hBar->shouldParticipateInHitTesting()) {
2752         LayoutRect hBarRect(horizontalScrollbarStart(0),
2753                             box->height() - box->borderBottom() - m_hBar->height(),
2754                             box->width() - (box->borderLeft() + box->borderRight()) - (m_vBar ? m_vBar->width() : resizeControlSize),
2755                             m_hBar->height());
2756         if (hBarRect.contains(localPoint)) {
2757             result.setScrollbar(m_hBar.get());
2758             return true;
2759         }
2760     }
2761
2762     return false;
2763 }
2764
2765 bool RenderLayer::scroll(ScrollDirection direction, ScrollGranularity granularity, float multiplier)
2766 {
2767     return ScrollableArea::scroll(direction, granularity, multiplier);
2768 }
2769
2770 void RenderLayer::paint(GraphicsContext* context, const LayoutRect& damageRect, PaintBehavior paintBehavior, RenderObject* paintingRoot, RenderRegion* region, PaintLayerFlags paintFlags)
2771 {
2772     OverlapTestRequestMap overlapTestRequests;
2773     paintLayer(this, context, damageRect, paintBehavior, paintingRoot, region, &overlapTestRequests, paintFlags);
2774     OverlapTestRequestMap::iterator end = overlapTestRequests.end();
2775     for (OverlapTestRequestMap::iterator it = overlapTestRequests.begin(); it != end; ++it)
2776         it->first->setOverlapTestResult(false);
2777 }
2778
2779 void RenderLayer::paintOverlayScrollbars(GraphicsContext* context, const LayoutRect& damageRect, PaintBehavior paintBehavior, RenderObject* paintingRoot)
2780 {
2781     if (!m_containsDirtyOverlayScrollbars)
2782         return;
2783     paintLayer(this, context, damageRect, paintBehavior, paintingRoot, 0, 0, PaintLayerHaveTransparency | PaintLayerTemporaryClipRects 
2784                | PaintLayerPaintingOverlayScrollbars);
2785     m_containsDirtyOverlayScrollbars = false;
2786 }
2787
2788 #ifndef DISABLE_ROUNDED_CORNER_CLIPPING
2789 static bool inContainingBlockChain(RenderLayer* startLayer, RenderLayer* endLayer)
2790 {
2791     if (startLayer == endLayer)
2792         return true;
2793     
2794     RenderView* view = startLayer->renderer()->view();
2795     for (RenderBlock* currentBlock = startLayer->renderer()->containingBlock(); currentBlock && currentBlock != view; currentBlock = currentBlock->containingBlock()) {
2796         if (currentBlock->layer() == endLayer)
2797             return true;
2798     }
2799     
2800     return false;
2801 }
2802 #endif
2803
2804 void RenderLayer::clipToRect(RenderLayer* rootLayer, GraphicsContext* context, const LayoutRect& paintDirtyRect, const ClipRect& clipRect,
2805                              BorderRadiusClippingRule rule)
2806 {
2807     if (clipRect.rect() == paintDirtyRect)
2808         return;
2809     context->save();
2810     context->clip(pixelSnappedIntRect(clipRect.rect()));
2811     
2812     if (!clipRect.hasRadius())
2813         return;
2814
2815 #ifndef DISABLE_ROUNDED_CORNER_CLIPPING
2816     // If the clip rect has been tainted by a border radius, then we have to walk up our layer chain applying the clips from
2817     // any layers with overflow. The condition for being able to apply these clips is that the overflow object be in our
2818     // containing block chain so we check that also.
2819     for (RenderLayer* layer = rule == IncludeSelfForBorderRadius ? this : parent(); layer; layer = layer->parent()) {
2820         if (layer->renderer()->hasOverflowClip() && layer->renderer()->style()->hasBorderRadius() && inContainingBlockChain(this, layer)) {
2821                 LayoutPoint delta;
2822                 layer->convertToLayerCoords(rootLayer, delta);
2823                 context->addRoundedRectClip(layer->renderer()->style()->getRoundedInnerBorderFor(LayoutRect(delta, layer->size())));
2824         }
2825
2826         if (layer == rootLayer)
2827             break;
2828     }
2829 #endif
2830 }
2831
2832 void RenderLayer::restoreClip(GraphicsContext* context, const LayoutRect& paintDirtyRect, const ClipRect& clipRect)
2833 {
2834     if (clipRect.rect() == paintDirtyRect)
2835         return;
2836     context->restore();
2837 }
2838
2839 static void performOverlapTests(OverlapTestRequestMap& overlapTestRequests, const RenderLayer* rootLayer, const RenderLayer* layer)
2840 {
2841     Vector<OverlapTestRequestClient*> overlappedRequestClients;
2842     OverlapTestRequestMap::iterator end = overlapTestRequests.end();
2843     LayoutRect boundingBox = layer->boundingBox(rootLayer);
2844     for (OverlapTestRequestMap::iterator it = overlapTestRequests.begin(); it != end; ++it) {
2845         if (!boundingBox.intersects(it->second))
2846             continue;
2847
2848         it->first->setOverlapTestResult(true);
2849         overlappedRequestClients.append(it->first);
2850     }
2851     for (size_t i = 0; i < overlappedRequestClients.size(); ++i)
2852         overlapTestRequests.remove(overlappedRequestClients[i]);
2853 }
2854
2855 #if USE(ACCELERATED_COMPOSITING)
2856 static bool shouldDoSoftwarePaint(const RenderLayer* layer, bool paintingReflection)
2857 {
2858     return paintingReflection && !layer->has3DTransform();
2859 }
2860 #endif
2861     
2862 static inline bool shouldSuppressPaintingLayer(RenderLayer* layer)
2863 {
2864     // Avoid painting descendants of the root layer when stylesheets haven't loaded. This eliminates FOUC.
2865     // It's ok not to draw, because later on, when all the stylesheets do load, updateStyleSelector on the Document
2866     // will do a full repaint().
2867     if (layer->renderer()->document()->didLayoutWithPendingStylesheets() && !layer->renderer()->isRenderView() && !layer->renderer()->isRoot())
2868         return true;
2869
2870     // Avoid painting all layers if the document is in a state where visual updates aren't allowed.
2871     // A full repaint will occur in Document::implicitClose() if painting is suppressed here.
2872     if (!layer->renderer()->document()->visualUpdatesAllowed())
2873         return true;
2874
2875     return false;
2876 }
2877
2878
2879 void RenderLayer::paintLayer(RenderLayer* rootLayer, GraphicsContext* context,
2880                         const LayoutRect& paintDirtyRect, PaintBehavior paintBehavior,
2881                         RenderObject* paintingRoot, RenderRegion* region, OverlapTestRequestMap* overlapTestRequests,
2882                         PaintLayerFlags paintFlags)
2883 {
2884 #if USE(ACCELERATED_COMPOSITING)
2885     if (isComposited()) {
2886         // The updatingControlTints() painting pass goes through compositing layers,
2887         // but we need to ensure that we don't cache clip rects computed with the wrong root in this case.
2888         if (context->updatingControlTints() || (paintBehavior & PaintBehaviorFlattenCompositingLayers))
2889             paintFlags |= PaintLayerTemporaryClipRects;
2890         else if (!backing()->paintsIntoWindow() && !backing()->paintsIntoCompositedAncestor() && !shouldDoSoftwarePaint(this, paintFlags & PaintLayerPaintingReflection) && !rootLayer->containsDirtyOverlayScrollbars()) {
2891             // If this RenderLayer should paint into its backing, that will be done via RenderLayerBacking::paintIntoLayer().
2892             return;
2893         }
2894     }
2895 #endif
2896
2897     if (shouldSuppressPaintingLayer(this))
2898         return;
2899     
2900     // If this layer is totally invisible then there is nothing to paint.
2901     if (!renderer()->opacity())
2902         return;
2903
2904     // Non self-painting leaf layers don't need to be painted as their renderer() should properly paint itself.
2905     if (!isSelfPaintingLayer() && !firstChild())
2906         return;
2907
2908     if (paintsWithTransparency(paintBehavior))
2909         paintFlags |= PaintLayerHaveTransparency;
2910
2911     // PaintLayerAppliedTransform is used in RenderReplica, to avoid applying the transform twice.
2912     if (paintsWithTransform(paintBehavior) && !(paintFlags & PaintLayerAppliedTransform)) {
2913         TransformationMatrix layerTransform = renderableTransform(paintBehavior);
2914         // If the transform can't be inverted, then don't paint anything.
2915         if (!layerTransform.isInvertible())
2916             return;
2917
2918         // If we have a transparency layer enclosing us and we are the root of a transform, then we need to establish the transparency
2919         // layer from the parent now, assuming there is a parent
2920         if (paintFlags & PaintLayerHaveTransparency) {
2921             if (parent())
2922                 parent()->beginTransparencyLayers(context, rootLayer, paintDirtyRect, paintBehavior);
2923             else
2924                 beginTransparencyLayers(context, rootLayer, paintDirtyRect, paintBehavior);
2925         }
2926
2927         // Make sure the parent's clip rects have been calculated.
2928         ClipRect clipRect = paintDirtyRect;
2929         if (parent()) {
2930             clipRect = backgroundClipRect(rootLayer, region, paintFlags & PaintLayerTemporaryClipRects);
2931             clipRect.intersect(paintDirtyRect);
2932         
2933             // Push the parent coordinate space's clip.
2934             parent()->clipToRect(rootLayer, context, paintDirtyRect, clipRect);
2935         }
2936
2937         // Adjust the transform such that the renderer's upper left corner will paint at (0,0) in user space.
2938         // This involves subtracting out the position of the layer in our current coordinate space.
2939         LayoutPoint delta;
2940         convertToLayerCoords(rootLayer, delta);
2941         TransformationMatrix transform(layerTransform);
2942         transform.translateRight(delta.x(), delta.y());
2943         
2944         // Apply the transform.
2945         {
2946             GraphicsContextStateSaver stateSaver(*context);
2947             context->concatCTM(transform.toAffineTransform());
2948
2949             // Now do a paint with the root layer shifted to be us.
2950             paintLayerContentsAndReflection(this, context, transform.inverse().mapRect(paintDirtyRect), paintBehavior, paintingRoot, region, overlapTestRequests, paintFlags);
2951         }        
2952
2953         // Restore the clip.
2954         if (parent())
2955             parent()->restoreClip(context, paintDirtyRect, clipRect);
2956
2957         return;
2958     }
2959     
2960     paintLayerContentsAndReflection(rootLayer, context, paintDirtyRect, paintBehavior, paintingRoot, region, overlapTestRequests, paintFlags);
2961 }
2962
2963 void RenderLayer::paintLayerContentsAndReflection(RenderLayer* rootLayer, GraphicsContext* context,
2964                         const LayoutRect& paintDirtyRect, PaintBehavior paintBehavior,
2965                         RenderObject* paintingRoot, RenderRegion* region, OverlapTestRequestMap* overlapTestRequests,
2966                         PaintLayerFlags paintFlags)
2967 {
2968     PaintLayerFlags localPaintFlags = paintFlags & ~(PaintLayerAppliedTransform);
2969
2970     // Paint the reflection first if we have one.
2971     if (m_reflection && !m_paintingInsideReflection) {
2972         // Mark that we are now inside replica painting.
2973         m_paintingInsideReflection = true;
2974         reflectionLayer()->paintLayer(rootLayer, context, paintDirtyRect, paintBehavior, paintingRoot, region, overlapTestRequests, localPaintFlags | PaintLayerPaintingReflection);
2975         m_paintingInsideReflection = false;
2976     }
2977
2978     localPaintFlags |= PaintLayerPaintingCompositingAllPhases;
2979     paintLayerContents(rootLayer, context, paintDirtyRect, paintBehavior, paintingRoot, region, overlapTestRequests, localPaintFlags);
2980 }
2981
2982 void RenderLayer::paintLayerContents(RenderLayer* rootLayer, GraphicsContext* context, 
2983                         const LayoutRect& parentPaintDirtyRect, PaintBehavior paintBehavior,
2984                         RenderObject* paintingRoot, RenderRegion* region, OverlapTestRequestMap* overlapTestRequests,
2985                         PaintLayerFlags paintFlags)
2986 {
2987     PaintLayerFlags localPaintFlags = paintFlags & ~(PaintLayerAppliedTransform);
2988     bool haveTransparency = localPaintFlags & PaintLayerHaveTransparency;
2989     bool isSelfPaintingLayer = this->isSelfPaintingLayer();
2990     bool isPaintingOverlayScrollbars = paintFlags & PaintLayerPaintingOverlayScrollbars;
2991     // Outline always needs to be painted even if we have no visible content.
2992     bool shouldPaintOutline = isSelfPaintingLayer && !isPaintingOverlayScrollbars;
2993     bool shouldPaintContent = m_hasVisibleContent && isSelfPaintingLayer && !isPaintingOverlayScrollbars;
2994
2995     // Calculate the clip rects we should use only when we need them.
2996     LayoutRect layerBounds;
2997     ClipRect damageRect, clipRectToApply, outlineRect;
2998     LayoutPoint paintOffset;
2999     LayoutRect paintDirtyRect = parentPaintDirtyRect;
3000     
3001     bool useClipRect = true;
3002     GraphicsContext* transparencyLayerContext = context;
3003     
3004     // Ensure our lists are up-to-date.
3005     updateLayerListsIfNeeded();
3006
3007 #if ENABLE(CSS_FILTERS)
3008     FilterEffectRendererHelper filterPainter(filterRenderer() && paintsWithFilters());
3009     if (filterPainter.haveFilterEffect() && !context->paintingDisabled()) {
3010         LayoutPoint rootLayerOffset;
3011         convertToLayerCoords(rootLayer, rootLayerOffset);
3012         RenderLayerFilterInfo* filterInfo = this->filterInfo();
3013         ASSERT(filterInfo);
3014         LayoutRect filterRepaintRect = filterInfo->dirtySourceRect();
3015         filterRepaintRect.move(rootLayerOffset.x(), rootLayerOffset.y());
3016         if (filterPainter.prepareFilterEffect(this, calculateLayerBounds(this, rootLayer, 0), parentPaintDirtyRect, filterRepaintRect)) {
3017             // Now we know for sure, that the source image will be updated, so we can revert our tracking repaint rect back to zero.
3018             filterInfo->resetDirtySourceRect();
3019
3020             // Rewire the old context to a memory buffer, so that we can capture the contents of the layer.
3021             // NOTE: We saved the old context in the "transparencyLayerContext" local variable, to be able to start a transparency layer
3022             // on the original context and avoid duplicating "beginFilterEffect" after each transpareny layer call. Also, note that 
3023             // beginTransparencyLayers will only create a single lazy transparency layer, even though it is called twice in this method.
3024             context = filterPainter.beginFilterEffect(context);
3025
3026             // Check that we didn't fail to allocate the graphics context for the offscreen buffer.
3027             if (filterPainter.hasStartedFilterEffect()) {
3028                 paintDirtyRect = filterPainter.repaintRect();
3029                 // If the filter needs the full source image, we need to avoid using the clip rectangles.
3030                 // Otherwise, if for example this layer has overflow:hidden, a drop shadow will not compute correctly.
3031                 // Note that we will still apply the clipping on the final rendering of the filter.
3032                 useClipRect = !filterRenderer()->hasFilterThatMovesPixels();
3033             }
3034         }
3035     }
3036 #endif
3037     
3038     if (shouldPaintContent || shouldPaintOutline || isPaintingOverlayScrollbars) {
3039         calculateRects(rootLayer, region, paintDirtyRect, layerBounds, damageRect, clipRectToApply, outlineRect, localPaintFlags & PaintLayerTemporaryClipRects);
3040         paintOffset = toPoint(layerBounds.location() - renderBoxLocation());
3041     }
3042
3043     bool forceBlackText = paintBehavior & PaintBehaviorForceBlackText;
3044     bool selectionOnly  = paintBehavior & PaintBehaviorSelectionOnly;
3045     
3046     // If this layer's renderer is a child of the paintingRoot, we render unconditionally, which
3047     // is done by passing a nil paintingRoot down to our renderer (as if no paintingRoot was ever set).
3048     // Else, our renderer tree may or may not contain the painting root, so we pass that root along
3049     // so it will be tested against as we descend through the renderers.
3050     RenderObject* paintingRootForRenderer = 0;
3051     if (paintingRoot && !renderer()->isDescendantOf(paintingRoot))
3052         paintingRootForRenderer = paintingRoot;
3053
3054     if (overlapTestRequests && isSelfPaintingLayer)
3055         performOverlapTests(*overlapTestRequests, rootLayer, this);
3056
3057     // We want to paint our layer, but only if we intersect the damage rect.
3058     shouldPaintContent &= intersectsDamageRect(layerBounds, damageRect.rect(), rootLayer);
3059     
3060     if (localPaintFlags & PaintLayerPaintingCompositingBackgroundPhase) {
3061         if (shouldPaintContent && !selectionOnly) {
3062             // Begin transparency layers lazily now that we know we have to paint something.
3063             if (haveTransparency)
3064                 beginTransparencyLayers(transparencyLayerContext, rootLayer, paintDirtyRect, paintBehavior);
3065         
3066             if (useClipRect) {
3067                 // Paint our background first, before painting any child layers.
3068                 // Establish the clip used to paint our background.
3069                 clipToRect(rootLayer, context, paintDirtyRect, damageRect, DoNotIncludeSelfForBorderRadius); // Background painting will handle clipping to self.
3070             }
3071             
3072             // Paint the background.
3073             PaintInfo paintInfo(context, pixelSnappedIntRect(damageRect.rect()), PaintPhaseBlockBackground, false, paintingRootForRenderer, region, 0);
3074             renderer()->paint(paintInfo, paintOffset);
3075
3076             if (useClipRect) {
3077                 // Restore the clip.
3078                 restoreClip(context, paintDirtyRect, damageRect);
3079             }
3080         }
3081
3082         // Now walk the sorted list of children with negative z-indices.
3083         paintList(m_negZOrderList, rootLayer, context, paintDirtyRect, paintBehavior, paintingRoot, region, overlapTestRequests, localPaintFlags);
3084     }
3085     
3086     if (localPaintFlags & PaintLayerPaintingCompositingForegroundPhase) {
3087         // Now establish the appropriate clip and paint our child RenderObjects.
3088         if (shouldPaintContent && !clipRectToApply.isEmpty()) {
3089             // Begin transparency layers lazily now that we know we have to paint something.
3090             if (haveTransparency)
3091                 beginTransparencyLayers(transparencyLayerContext, rootLayer, paintDirtyRect, paintBehavior);
3092
3093             if (useClipRect) {
3094                 // Set up the clip used when painting our children.
3095                 clipToRect(rootLayer, context, paintDirtyRect, clipRectToApply);
3096             }
3097             
3098             PaintInfo paintInfo(context, pixelSnappedIntRect(clipRectToApply.rect()),
3099                                 selectionOnly ? PaintPhaseSelection : PaintPhaseChildBlockBackgrounds,
3100                                 forceBlackText, paintingRootForRenderer, region, 0);
3101             renderer()->paint(paintInfo, paintOffset);
3102             if (!selectionOnly) {
3103                 paintInfo.phase = PaintPhaseFloat;
3104                 renderer()->paint(paintInfo, paintOffset);
3105                 paintInfo.phase = PaintPhaseForeground;
3106                 paintInfo.overlapTestRequests = overlapTestRequests;
3107                 renderer()->paint(paintInfo, paintOffset);
3108                 paintInfo.phase = PaintPhaseChildOutlines;
3109                 renderer()->paint(paintInfo, paintOffset);
3110             }
3111
3112             if (useClipRect) {
3113                 // Now restore our clip.
3114                 restoreClip(context, paintDirtyRect, clipRectToApply);
3115             }
3116         }
3117
3118         if (shouldPaintOutline && !outlineRect.isEmpty()) {
3119             // Paint our own outline
3120             PaintInfo paintInfo(context, pixelSnappedIntRect(outlineRect.rect()), PaintPhaseSelfOutline, false, paintingRootForRenderer, region, 0);
3121             clipToRect(rootLayer, context, paintDirtyRect, outlineRect, DoNotIncludeSelfForBorderRadius);
3122             renderer()->paint(paintInfo, paintOffset);
3123             restoreClip(context, paintDirtyRect, outlineRect);
3124         }
3125     
3126         // Paint any child layers that have overflow.
3127         paintList(m_normalFlowList, rootLayer, context, paintDirtyRect, paintBehavior, paintingRoot, region, overlapTestRequests, localPaintFlags);
3128     
3129         // Now walk the sorted list of children with positive z-indices.
3130         paintList(m_posZOrderList, rootLayer, context, paintDirtyRect, paintBehavior, paintingRoot, region, overlapTestRequests, localPaintFlags);
3131     }
3132     
3133     if ((localPaintFlags & PaintLayerPaintingCompositingMaskPhase) && shouldPaintContent && renderer()->hasMask() && !selectionOnly) {
3134         if (useClipRect)
3135             clipToRect(rootLayer, context, paintDirtyRect, damageRect, DoNotIncludeSelfForBorderRadius); // Mask painting will handle clipping to self.
3136
3137         // Paint the mask.
3138         PaintInfo paintInfo(context, pixelSnappedIntRect(damageRect.rect()), PaintPhaseMask, false, paintingRootForRenderer, region, 0);
3139         renderer()->paint(paintInfo, paintOffset);
3140         
3141         if (useClipRect) {
3142             // Restore the clip.
3143             restoreClip(context, paintDirtyRect, damageRect);
3144         }
3145     }
3146
3147     if (isPaintingOverlayScrollbars) {
3148         clipToRect(rootLayer, context, paintDirtyRect, damageRect);
3149         paintOverflowControls(context, roundedIntPoint(paintOffset), pixelSnappedIntRect(damageRect.rect()), true);
3150         restoreClip(context, paintDirtyRect, damageRect);
3151     }
3152
3153 #if ENABLE(CSS_FILTERS)
3154     if (filterPainter.hasStartedFilterEffect()) {
3155         // Apply the correct clipping (ie. overflow: hidden).
3156         clipToRect(rootLayer, transparencyLayerContext, paintDirtyRect, damageRect);
3157         context = filterPainter.applyFilterEffect();
3158         restoreClip(transparencyLayerContext, paintDirtyRect, damageRect);
3159     }
3160 #endif
3161
3162     // Make sure that we now use the original transparency context.
3163     ASSERT(transparencyLayerContext == context);
3164     
3165     // End our transparency layer
3166     if (haveTransparency && m_usedTransparency && !m_paintingInsideReflection) {
3167         context->endTransparencyLayer();
3168         context->restore();
3169         m_usedTransparency = false;
3170     }
3171 }
3172
3173 void RenderLayer::paintList(Vector<RenderLayer*>* list, RenderLayer* rootLayer, GraphicsContext* context,
3174                             const LayoutRect& paintDirtyRect, PaintBehavior paintBehavior,
3175                             RenderObject* paintingRoot, RenderRegion* region, OverlapTestRequestMap* overlapTestRequests,
3176                             PaintLayerFlags paintFlags)
3177 {
3178     if (!list)
3179         return;
3180
3181 #if !ASSERT_DISABLED
3182     LayerListMutationDetector mutationChecker(this);
3183 #endif
3184
3185     for (size_t i = 0; i < list->size(); ++i) {
3186         RenderLayer* childLayer = list->at(i);
3187         if (!childLayer->isPaginated())
3188             childLayer->paintLayer(rootLayer, context, paintDirtyRect, paintBehavior, paintingRoot, region, overlapTestRequests, paintFlags);
3189         else
3190             paintPaginatedChildLayer(childLayer, rootLayer, context, paintDirtyRect, paintBehavior, paintingRoot, region, overlapTestRequests, paintFlags);
3191     }
3192 }
3193
3194 void RenderLayer::paintPaginatedChildLayer(RenderLayer* childLayer, RenderLayer* rootLayer, GraphicsContext* context,
3195                                            const LayoutRect& paintDirtyRect, PaintBehavior paintBehavior,
3196                                            RenderObject* paintingRoot, RenderRegion* region, OverlapTestRequestMap* overlapTestRequests,
3197                                            PaintLayerFlags paintFlags)
3198 {
3199     // We need to do multiple passes, breaking up our child layer into strips.
3200     Vector<RenderLayer*> columnLayers;
3201     RenderLayer* ancestorLayer = isNormalFlowOnly() ? parent() : stackingContext();
3202     for (RenderLayer* curr = childLayer->parent(); curr; curr = curr->parent()) {
3203         if (curr->renderer()->hasColumns() && checkContainingBlockChainForPagination(childLayer->renderer(), curr->renderBox()))
3204             columnLayers.append(curr);
3205         if (curr == ancestorLayer)
3206             break;
3207     }
3208
3209     // It is possible for paintLayer() to be called after the child layer ceases to be paginated but before
3210     // updateLayerPositions() is called and resets the isPaginated() flag, see <rdar://problem/10098679>.
3211     // If this is the case, just bail out, since the upcoming call to updateLayerPositions() will repaint the layer.
3212     if (!columnLayers.size())
3213         return;
3214
3215     paintChildLayerIntoColumns(childLayer, rootLayer, context, paintDirtyRect, paintBehavior, paintingRoot, region, overlapTestRequests, paintFlags, columnLayers, columnLayers.size() - 1);
3216 }
3217
3218 void RenderLayer::paintChildLayerIntoColumns(RenderLayer* childLayer, RenderLayer* rootLayer, GraphicsContext* context,
3219                                              const LayoutRect& paintDirtyRect, PaintBehavior paintBehavior,
3220                                              RenderObject* paintingRoot, RenderRegion* region, OverlapTestRequestMap* overlapTestRequests,
3221                                              PaintLayerFlags paintFlags, const Vector<RenderLayer*>& columnLayers, size_t colIndex)
3222 {
3223     RenderBlock* columnBlock = toRenderBlock(columnLayers[colIndex]->renderer());
3224
3225     ASSERT(columnBlock && columnBlock->hasColumns());
3226     if (!columnBlock || !columnBlock->hasColumns())
3227         return;
3228     
3229     LayoutPoint layerOffset;
3230     // FIXME: It looks suspicious to call convertToLayerCoords here
3231     // as canUseConvertToLayerCoords is true for this layer.
3232     columnBlock->layer()->convertToLayerCoords(rootLayer, layerOffset);
3233     
3234     bool isHorizontal = columnBlock->style()->isHorizontalWritingMode();
3235
3236     ColumnInfo* colInfo = columnBlock->columnInfo();
3237     unsigned colCount = columnBlock->columnCount(colInfo);
3238     int currLogicalTopOffset = 0;
3239     for (unsigned i = 0; i < colCount; i++) {
3240         // For each rect, we clip to the rect, and then we adjust our coords.
3241         LayoutRect colRect = columnBlock->columnRectAt(colInfo, i);
3242         columnBlock->flipForWritingMode(colRect);
3243         int logicalLeftOffset = (isHorizontal ? colRect.x() : colRect.y()) - columnBlock->logicalLeftOffsetForContent();
3244         LayoutSize offset;
3245         if (isHorizontal) {
3246             if (colInfo->progressionAxis() == ColumnInfo::InlineAxis)
3247                 offset = LayoutSize(logicalLeftOffset, currLogicalTopOffset);
3248             else
3249                 offset = LayoutSize(0, colRect.y() + currLogicalTopOffset - columnBlock->borderTop() - columnBlock->paddingTop());
3250         } else {
3251             if (colInfo->progressionAxis() == ColumnInfo::InlineAxis)
3252                 offset = LayoutSize(currLogicalTopOffset, logicalLeftOffset);
3253             else
3254                 offset = LayoutSize(colRect.x() + currLogicalTopOffset - columnBlock->borderLeft() - columnBlock->paddingLeft(), 0);
3255         }
3256
3257         colRect.moveBy(layerOffset);
3258
3259         LayoutRect localDirtyRect(paintDirtyRect);
3260         localDirtyRect.intersect(colRect);
3261         
3262         if (!localDirtyRect.isEmpty()) {
3263             GraphicsContextStateSaver stateSaver(*context);
3264             
3265             // Each strip pushes a clip, since column boxes are specified as being
3266             // like overflow:hidden.
3267             context->clip(colRect);
3268
3269             if (!colIndex) {
3270                 // Apply a translation transform to change where the layer paints.
3271                 TransformationMatrix oldTransform;
3272                 bool oldHasTransform = childLayer->transform();
3273                 if (oldHasTransform)
3274                     oldTransform = *childLayer->transform();
3275                 TransformationMatrix newTransform(oldTransform);
3276                 newTransform.translateRight(offset.width(), offset.height());
3277                 
3278                 childLayer->m_transform = adoptPtr(new TransformationMatrix(newTransform));
3279                 childLayer->paintLayer(rootLayer, context, localDirtyRect, paintBehavior, paintingRoot, region, overlapTestRequests, paintFlags);
3280                 if (oldHasTransform)
3281                     childLayer->m_transform = adoptPtr(new TransformationMatrix(oldTransform));
3282                 else
3283                     childLayer->m_transform.clear();
3284             } else {
3285                 // Adjust the transform such that the renderer's upper left corner will paint at (0,0) in user space.
3286                 // This involves subtracting out the position of the layer in our current coordinate space.
3287                 LayoutPoint childOffset;
3288                 columnLayers[colIndex - 1]->convertToLayerCoords(rootLayer, childOffset);
3289                 TransformationMatrix transform;
3290                 transform.translateRight(childOffset.x() + offset.width(), childOffset.y() + offset.height());
3291                 
3292                 // Apply the transform.
3293                 context->concatCTM(transform.toAffineTransform());
3294
3295                 // Now do a paint with the root layer shifted to be the next multicol block.
3296                 paintChildLayerIntoColumns(childLayer, columnLayers[colIndex - 1], context, transform.inverse().mapRect(localDirtyRect), paintBehavior, 
3297                                            paintingRoot, region, overlapTestRequests, paintFlags, 
3298                                            columnLayers, colIndex - 1);
3299             }
3300         }
3301
3302         // Move to the next position.
3303         int blockDelta = isHorizontal ? colRect.height() : colRect.width();
3304         if (columnBlock->style()->isFlippedBlocksWritingMode())
3305             currLogicalTopOffset += blockDelta;
3306         else
3307             currLogicalTopOffset -= blockDelta;
3308     }
3309 }
3310
3311 static inline LayoutRect frameVisibleRect(RenderObject* renderer)
3312 {
3313     FrameView* frameView = renderer->document()->view();
3314     if (!frameView)
3315         return LayoutRect();
3316
3317     return frameView->visibleContentRect();
3318 }
3319
3320 bool RenderLayer::hitTest(const HitTestRequest& request, HitTestResult& result)
3321 {
3322     renderer()->document()->updateLayout();
3323     
3324     LayoutRect hitTestArea = renderer()->isRenderFlowThread() ? toRenderFlowThread(renderer())->borderBoxRect() : renderer()->view()->documentRect();
3325     if (!request.ignoreClipping())
3326         hitTestArea.intersect(frameVisibleRect(renderer()));
3327
3328     RenderLayer* insideLayer = hitTestLayer(this, 0, request, result, hitTestArea, result.point(), false);
3329     if (!insideLayer) {
3330         // We didn't hit any layer. If we are the root layer and the mouse is -- or just was -- down, 
3331         // return ourselves. We do this so mouse events continue getting delivered after a drag has 
3332         // exited the WebView, and so hit testing over a scrollbar hits the content document.
3333         if ((request.active() || request.release()) && renderer()->isRenderView()) {
3334             renderer()->updateHitTestResult(result, result.point());
3335             insideLayer = this;
3336         }
3337     }
3338
3339     // Now determine if the result is inside an anchor - if the urlElement isn't already set.
3340     Node* node = result.innerNode();
3341     if (node && !result.URLElement())
3342         result.setURLElement(static_cast<Element*>(node->enclosingLinkEventParentOrSelf()));
3343
3344     // Next set up the correct :hover/:active state along the new chain.
3345     updateHoverActiveState(request, result);
3346     
3347     // Now return whether we were inside this layer (this will always be true for the root
3348     // layer).
3349     return insideLayer;
3350 }
3351
3352 Node* RenderLayer::enclosingElement() const
3353 {
3354     for (RenderObject* r = renderer(); r; r = r->parent()) {
3355         if (Node* e = r->node())
3356             return e;
3357     }
3358     ASSERT_NOT_REACHED();
3359     return 0;
3360 }
3361
3362 // Compute the z-offset of the point in the transformState.
3363 // This is effectively projecting a ray normal to the plane of ancestor, finding where that
3364 // ray intersects target, and computing the z delta between those two points.
3365 static double computeZOffset(const HitTestingTransformState& transformState)
3366 {
3367     // We got an affine transform, so no z-offset
3368     if (transformState.m_accumulatedTransform.isAffine())
3369         return 0;
3370
3371     // Flatten the point into the target plane
3372     FloatPoint targetPoint = transformState.mappedPoint();
3373     
3374     // Now map the point back through the transform, which computes Z.
3375     FloatPoint3D backmappedPoint = transformState.m_accumulatedTransform.mapPoint(FloatPoint3D(targetPoint));
3376     return backmappedPoint.z();
3377 }
3378
3379 PassRefPtr<HitTestingTransformState> RenderLayer::createLocalTransformState(RenderLayer* rootLayer, RenderLayer* containerLayer,
3380                                         const LayoutRect& hitTestRect, const LayoutPoint& hitTestPoint,
3381                                         const HitTestingTransformState* containerTransformState) const
3382 {
3383     RefPtr<HitTestingTransformState> transformState;
3384     LayoutPoint offset;
3385     if (containerTransformState) {
3386         // If we're already computing transform state, then it's relative to the container (which we know is non-null).
3387         transformState = HitTestingTransformState::create(*containerTransformState);
3388         convertToLayerCoords(containerLayer, offset);
3389     } else {
3390         // If this is the first time we need to make transform state, then base it off of hitTestPoint,
3391         // which is relative to rootLayer.
3392         transformState = HitTestingTransformState::create(hitTestPoint, FloatQuad(hitTestRect));
3393         convertToLayerCoords(rootLayer, offset);
3394     }
3395     
3396     RenderObject* containerRenderer = containerLayer ? containerLayer->renderer() : 0;
3397     if (renderer()->shouldUseTransformFromContainer(containerRenderer)) {
3398         TransformationMatrix containerTransform;
3399         renderer()->getTransformFromContainer(containerRenderer, toLayoutSize(offset), containerTransform);
3400         transformState->applyTransform(containerTransform, HitTestingTransformState::AccumulateTransform);
3401     } else {
3402         transformState->translate(offset.x(), offset.y(), HitTestingTransformState::AccumulateTransform);
3403     }
3404     
3405     return transformState;
3406 }
3407
3408
3409 static bool isHitCandidate(const RenderLayer* hitLayer, bool canDepthSort, double* zOffset, const HitTestingTransformState* transformState)
3410 {
3411     if (!hitLayer)
3412         return false;
3413
3414     // The hit layer is depth-sorting with other layers, so just say that it was hit.
3415     if (canDepthSort)
3416         return true;
3417     
3418     // We need to look at z-depth to decide if this layer was hit.
3419     if (zOffset) {
3420         ASSERT(transformState);
3421         // This is actually computing our z, but that's OK because the hitLayer is coplanar with us.
3422         double childZOffset = computeZOffset(*transformState);
3423         if (childZOffset > *zOffset) {
3424             *zOffset = childZOffset;
3425             return true;
3426         }
3427         return false;
3428     }
3429
3430     return true;
3431 }
3432
3433 // hitTestPoint and hitTestRect are relative to rootLayer.
3434 // A 'flattening' layer is one preserves3D() == false.
3435 // transformState.m_accumulatedTransform holds the transform from the containing flattening layer.
3436 // transformState.m_lastPlanarPoint is the hitTestPoint in the plane of the containing flattening layer.
3437 // transformState.m_lastPlanarQuad is the hitTestRect as a quad in the plane of the containing flattening layer.
3438 // 
3439 // If zOffset is non-null (which indicates that the caller wants z offset information), 
3440 //  *zOffset on return is the z offset of the hit point relative to the containing flattening layer.
3441 RenderLayer* RenderLayer::hitTestLayer(RenderLayer* rootLayer, RenderLayer* containerLayer, const HitTestRequest& request, HitTestResult& result,
3442                                        const LayoutRect& hitTestRect, const LayoutPoint& hitTestPoint, bool appliedTransform,
3443                                        const HitTestingTransformState* transformState, double* zOffset)
3444 {
3445     // The natural thing would be to keep HitTestingTransformState on the stack, but it's big, so we heap-allocate.
3446
3447     bool useTemporaryClipRects = false;
3448 #if USE(ACCELERATED_COMPOSITING)
3449     useTemporaryClipRects = compositor()->inCompositingMode();
3450 #endif
3451     useTemporaryClipRects |= renderer()->view()->frameView()->containsScrollableAreaWithOverlayScrollbars();
3452
3453     LayoutRect hitTestArea = result.rectForPoint(hitTestPoint);
3454
3455     // Apply a transform if we have one.
3456     if (transform() && !appliedTransform) {
3457         // Make sure the parent's clip rects have been calculated.
3458         if (parent()) {
3459             ClipRect clipRect = backgroundClipRect(rootLayer, result.region(), useTemporaryClipRects, IncludeOverlayScrollbarSize);
3460             // Go ahead and test the enclosing clip now.
3461             if (!clipRect.intersects(hitTestArea))
3462                 return 0;
3463         }
3464
3465         // Create a transform state to accumulate this transform.
3466         RefPtr<HitTestingTransformState> newTransformState = createLocalTransformState(rootLayer, containerLayer, hitTestRect, hitTestPoint, transformState);
3467
3468         // If the transform can't be inverted, then don't hit test this layer at all.
3469         if (!newTransformState->m_accumulatedTransform.isInvertible())
3470             return 0;
3471
3472         // Compute the point and the hit test rect in the coords of this layer by using the values
3473         // from the transformState, which store the point and quad in the coords of the last flattened
3474         // layer, and the accumulated transform which lets up map through preserve-3d layers.
3475         //
3476         // We can't just map hitTestPoint and hitTestRect because they may have been flattened (losing z)
3477         // by our container.
3478         LayoutPoint localPoint = roundedLayoutPoint(newTransformState->mappedPoint());
3479         LayoutRect localHitTestRect = newTransformState->boundsOfMappedQuad();
3480
3481         // Now do a hit test with the root layer shifted to be us.
3482         return hitTestLayer(this, containerLayer, request, result, localHitTestRect, localPoint, true, newTransformState.get(), zOffset);
3483     }
3484
3485     // Ensure our lists and 3d status are up-to-date.
3486     updateCompositingAndLayerListsIfNeeded();
3487     update3DTransformedDescendantStatus();
3488     
3489     RefPtr<HitTestingTransformState> localTransformState;
3490     if (appliedTransform) {
3491         // We computed the correct state in the caller (above code), so just reference it.
3492         ASSERT(transformState);
3493         localTransformState = const_cast<HitTestingTransformState*>(transformState);
3494     } else if (transformState || m_has3DTransformedDescendant || preserves3D()) {
3495         // We need transform state for the first time, or to offset the container state, so create it here.
3496         localTransformState = createLocalTransformState(rootLayer, containerLayer, hitTestRect, hitTestPoint, transformState);
3497     }
3498
3499     // Check for hit test on backface if backface-visibility is 'hidden'
3500     if (localTransformState && renderer()->style()->backfaceVisibility() == BackfaceVisibilityHidden) {
3501         TransformationMatrix invertedMatrix = localTransformState->m_accumulatedTransform.inverse();
3502         // If the z-vector of the matrix is negative, the back is facing towards the viewer.
3503         if (invertedMatrix.m33() < 0)
3504             return 0;
3505     }
3506
3507     RefPtr<HitTestingTransformState> unflattenedTransformState = localTransformState;
3508     if (localTransformState && !preserves3D()) {
3509         // Keep a copy of the pre-flattening state, for computing z-offsets for the container
3510         unflattenedTransformState = HitTestingTransformState::create(*localTransformState);
3511         // This layer is flattening, so flatten the state passed to descendants.
3512         localTransformState->flatten();
3513     }
3514     
3515     // Calculate the clip rects we should use.
3516     LayoutRect layerBounds;
3517     ClipRect bgRect;
3518     ClipRect fgRect;
3519     ClipRect outlineRect;
3520     calculateRects(rootLayer, result.region(), hitTestRect, layerBounds, bgRect, fgRect, outlineRect, useTemporaryClipRects, IncludeOverlayScrollbarSize);
3521     
3522     // The following are used for keeping track of the z-depth of the hit point of 3d-transformed
3523     // descendants.
3524     double localZOffset = -numeric_limits<double>::infinity();
3525     double* zOffsetForDescendantsPtr = 0;
3526     double* zOffsetForContentsPtr = 0;
3527     
3528     bool depthSortDescendants = false;
3529     if (preserves3D()) {
3530         depthSortDescendants = true;
3531         // Our layers can depth-test with our container, so share the z depth pointer with the container, if it passed one down.
3532         zOffsetForDescendantsPtr = zOffset ? zOffset : &localZOffset;
3533         zOffsetForContentsPtr = zOffset ? zOffset : &localZOffset;
3534     } else if (m_has3DTransformedDescendant) {
3535         // Flattening layer with 3d children; use a local zOffset pointer to depth-test children and foreground.
3536         depthSortDescendants = true;
3537         zOffsetForDescendantsPtr = zOffset ? zOffset : &localZOffset;
3538         zOffsetForContentsPtr = zOffset ? zOffset : &localZOffset;
3539     } else if (zOffset) {
3540         zOffsetForDescendantsPtr = 0;
3541         // Container needs us to give back a z offset for the hit layer.
3542         zOffsetForContentsPtr = zOffset;
3543     }
3544     
3545     // This variable tracks which layer the mouse ends up being inside. 
3546     RenderLayer* candidateLayer = 0;
3547
3548     // Begin by walking our list of positive layers from highest z-index down to the lowest z-index.
3549     RenderLayer* hitLayer = hitTestList(m_posZOrderList, rootLayer, request, result, hitTestRect, hitTestPoint,
3550                                         localTransformState.get(), zOffsetForDescendantsPtr, zOffset, unflattenedTransformState.get(), depthSortDescendants);
3551     if (hitLayer) {
3552         if (!depthSortDescendants)
3553             return hitLayer;
3554         candidateLayer = hitLayer;
3555     }
3556
3557     // Now check our overflow objects.
3558     hitLayer = hitTestList(m_normalFlowList, rootLayer, request, result, hitTestRect, hitTestPoint,
3559                            localTransformState.get(), zOffsetForDescendantsPtr, zOffset, unflattenedTransformState.get(), depthSortDescendants);
3560     if (hitLayer) {
3561         if (!depthSortDescendants)
3562             return hitLayer;
3563         candidateLayer = hitLayer;
3564     }
3565
3566     // Next we want to see if the mouse pos is inside the child RenderObjects of the layer.
3567     if (fgRect.intersects(hitTestArea) && isSelfPaintingLayer()) {
3568         // Hit test with a temporary HitTestResult, because we only want to commit to 'result' if we know we're frontmost.
3569         HitTestResult tempResult(result.point(), result.topPadding(), result.rightPadding(), result.bottomPadding(), result.leftPadding(), result.shadowContentFilterPolicy());
3570         if (hitTestContents(request, tempResult, layerBounds, hitTestPoint, HitTestDescendants) &&
3571             isHitCandidate(this, false, zOffsetForContentsPtr, unflattenedTransformState.get())) {
3572             if (result.isRectBasedTest())
3573                 result.append(tempResult);
3574             else
3575                 result = tempResult;
3576             if (!depthSortDescendants)
3577                 return this;
3578             // Foreground can depth-sort with descendant layers, so keep this as a candidate.
3579             candidateLayer = this;
3580         } else if (result.isRectBasedTest())
3581             result.append(tempResult);
3582     }
3583
3584     // Now check our negative z-index children.
3585     hitLayer = hitTestList(m_negZOrderList, rootLayer, request, result, hitTestRect, hitTestPoint,
3586                                         localTransformState.get(), zOffsetForDescendantsPtr, zOffset, unflattenedTransformState.get(), depthSortDescendants);
3587     if (hitLayer) {
3588         if (!depthSortDescendants)
3589             return hitLayer;
3590         candidateLayer = hitLayer;
3591     }
3592
3593     // If we found a layer, return. Child layers, and foreground always render in front of background.
3594     if (candidateLayer)
3595         return candidateLayer;
3596
3597     if (bgRect.intersects(hitTestArea) && isSelfPaintingLayer()) {
3598         HitTestResult tempResult(result.point(), result.topPadding(), result.rightPadding(), result.bottomPadding(), result.leftPadding(), result.shadowContentFilterPolicy());
3599         if (hitTestContents(request, tempResult, layerBounds, hitTestPoint, HitTestSelf) &&
3600             isHitCandidate(this, false, zOffsetForContentsPtr, unflattenedTransformState.get())) {
3601             if (result.isRectBasedTest())
3602                 result.append(tempResult);
3603             else
3604                 result = tempResult;
3605             return this;
3606         } else if (result.isRectBasedTest())
3607             result.append(tempResult);
3608     }
3609     
3610     return 0;
3611 }
3612
3613 bool RenderLayer::hitTestContents(const HitTestRequest& request, HitTestResult& result, const LayoutRect& layerBounds, const LayoutPoint& hitTestPoint, HitTestFilter hitTestFilter) const
3614 {
3615     if (!renderer()->hitTest(request, result, hitTestPoint,
3616                             toLayoutPoint(layerBounds.location() - renderBoxLocation()),
3617                             hitTestFilter)) {
3618         // It's wrong to set innerNode, but then claim that you didn't hit anything, unless it is
3619         // a rect-based test.
3620         ASSERT(!result.innerNode() || (result.isRectBasedTest() && result.rectBasedTestResult().size()));
3621         return false;
3622     }
3623
3624     // For positioned generated content, we might still not have a
3625     // node by the time we get to the layer level, since none of
3626     // the content in the layer has an element. So just walk up
3627     // the tree.
3628     if (!result.innerNode() || !result.innerNonSharedNode()) {
3629         Node* e = enclosingElement();
3630         if (!result.innerNode())
3631             result.setInnerNode(e);
3632         if (!result.innerNonSharedNode())
3633             result.setInnerNonSharedNode(e);
3634     }
3635         
3636     return true;
3637 }
3638
3639 RenderLayer* RenderLayer::hitTestList(Vector<RenderLayer*>* list, RenderLayer* rootLayer,
3640                                       const HitTestRequest& request, HitTestResult& result,
3641                                       const LayoutRect& hitTestRect, const LayoutPoint& hitTestPoint,
3642                                       const HitTestingTransformState* transformState, 
3643                                       double* zOffsetForDescendants, double* zOffset,
3644                                       const HitTestingTransformState* unflattenedTransformState,
3645                                       bool depthSortDescendants)
3646 {
3647     if (!list)
3648         return 0;
3649     
3650     RenderLayer* resultLayer = 0;
3651     for (int i = list->size() - 1; i >= 0; --i) {
3652         RenderLayer* childLayer = list->at(i);
3653         RenderLayer* hitLayer = 0;
3654         HitTestResult tempResult(result.point(), result.topPadding(), result.rightPadding(), result.bottomPadding(), result.leftPadding(), result.shadowContentFilterPolicy());
3655         if (childLayer->isPaginated())
3656             hitLayer = hitTestPaginatedChildLayer(childLayer, rootLayer, request, tempResult, hitTestRect, hitTestPoint, transformState, zOffsetForDescendants);
3657         else
3658             hitLayer = childLayer->hitTestLayer(rootLayer, this, request, tempResult, hitTestRect, hitTestPoint, false, transformState, zOffsetForDescendants);
3659
3660         // If it a rect-based test, we can safely append the temporary result since it might had hit
3661         // nodes but not necesserily had hitLayer set.
3662         if (result.isRectBasedTest())
3663             result.append(tempResult);
3664
3665         if (isHitCandidate(hitLayer, depthSortDescendants, zOffset, unflattenedTransformState)) {
3666             resultLayer = hitLayer;
3667             if (!result.isRectBasedTest())
3668                 result = tempResult;
3669             if (!depthSortDescendants)
3670                 break;
3671         }
3672     }
3673     
3674     return resultLayer;
3675 }
3676
3677 RenderLayer* RenderLayer::hitTestPaginatedChildLayer(RenderLayer* childLayer, RenderLayer* rootLayer, const HitTestRequest& request, HitTestResult& result,
3678                                                      const LayoutRect& hitTestRect, const LayoutPoint& hitTestPoint, const HitTestingTransformState* transformState, double* zOffset)
3679 {
3680     Vector<RenderLayer*> columnLayers;
3681     RenderLayer* ancestorLayer = isNormalFlowOnly() ? parent() : stackingContext();
3682     for (RenderLayer* curr = childLayer->parent(); curr; curr = curr->parent()) {
3683         if (curr->renderer()->hasColumns() && checkContainingBlockChainForPagination(childLayer->renderer(), curr->renderBox()))
3684             columnLayers.append(curr);
3685         if (curr == ancestorLayer)
3686             break;
3687     }
3688
3689     ASSERT(columnLayers.size());
3690     return hitTestChildLayerColumns(childLayer, rootLayer, request, result, hitTestRect, hitTestPoint, transformState, zOffset,
3691                                     columnLayers, columnLayers.size() - 1);
3692 }
3693
3694 RenderLayer* RenderLayer::hitTestChildLayerColumns(RenderLayer* childLayer, RenderLayer* rootLayer, const HitTestRequest& request, HitTestResult& result,
3695                                                    const LayoutRect& hitTestRect, const LayoutPoint& hitTestPoint, const HitTestingTransformState* transformState, double* zOffset,
3696                                                    const Vector<RenderLayer*>& columnLayers, size_t columnIndex)
3697 {
3698     RenderBlock* columnBlock = toRenderBlock(columnLayers[columnIndex]->renderer());
3699
3700     ASSERT(columnBlock && columnBlock->hasColumns());
3701     if (!columnBlock || !columnBlock->hasColumns())
3702         return 0;
3703
3704     LayoutPoint layerOffset;
3705     columnBlock->layer()->convertToLayerCoords(rootLayer, layerOffset);
3706     
3707     ColumnInfo* colInfo = columnBlock->columnInfo();
3708     int colCount = columnBlock->columnCount(colInfo);
3709     
3710     // We have to go backwards from the last column to the first.
3711     bool isHorizontal = columnBlock->style()->isHorizontalWritingMode();
3712     LayoutUnit logicalLeft = columnBlock->logicalLeftOffsetForContent();
3713     LayoutUnit currLogicalTopOffset = 0;
3714     int i;
3715     for (i = 0; i < colCount; i++) {
3716         LayoutRect colRect = columnBlock->columnRectAt(colInfo, i);
3717         LayoutUnit blockDelta =  (isHorizontal ? colRect.height() : colRect.width());
3718         if (columnBlock->style()->isFlippedBlocksWritingMode())
3719             currLogicalTopOffset += blockDelta;
3720         else
3721             currLogicalTopOffset -= blockDelta;
3722     }
3723     for (i = colCount - 1; i >= 0; i--) {
3724         // For each rect, we clip to the rect, and then we adjust our coords.
3725         LayoutRect colRect = columnBlock->columnRectAt(colInfo, i);
3726         columnBlock->flipForWritingMode(colRect);
3727         LayoutUnit currLogicalLeftOffset = (isHorizontal ? colRect.x() : colRect.y()) - logicalLeft;
3728         LayoutUnit blockDelta =  (isHorizontal ? colRect.height() : colRect.width());
3729         if (columnBlock->style()->isFlippedBlocksWritingMode())
3730             currLogicalTopOffset -= blockDelta;
3731         else
3732             currLogicalTopOffset += blockDelta;
3733
3734         LayoutSize offset;
3735         if (isHorizontal) {
3736             if (colInfo->progressionAxis() == ColumnInfo::InlineAxis)
3737                 offset = LayoutSize(currLogicalLeftOffset, currLogicalTopOffset);
3738             else
3739                 offset = LayoutSize(0, colRect.y() + currLogicalTopOffset - columnBlock->borderTop() - columnBlock->paddingTop());
3740         } else {
3741             if (colInfo->progressionAxis() == ColumnInfo::InlineAxis)
3742                 offset = LayoutSize(currLogicalTopOffset, currLogicalLeftOffset);
3743             else
3744                 offset = LayoutSize(colRect.x() + currLogicalTopOffset - columnBlock->borderLeft() - columnBlock->paddingLeft(), 0);
3745         }
3746
3747         colRect.moveBy(layerOffset);
3748
3749         LayoutRect localClipRect(hitTestRect);
3750         localClipRect.intersect(colRect);
3751
3752         if (!localClipRect.isEmpty() && localClipRect.intersects(result.rectForPoint(hitTestPoint))) {
3753             RenderLayer* hitLayer = 0;
3754             if (!columnIndex) {
3755                 // Apply a translation transform to change where the layer paints.
3756                 TransformationMatrix oldTransform;
3757                 bool oldHasTransform = childLayer->transform();
3758                 if (oldHasTransform)
3759                     oldTransform = *childLayer->transform();
3760                 TransformationMatrix newTransform(oldTransform);
3761                 newTransform.translateRight(offset.width(), offset.height());
3762                 
3763                 childLayer->m_transform = adoptPtr(new TransformationMatrix(newTransform));
3764                 hitLayer = childLayer->hitTestLayer(rootLayer, columnLayers[0], request, result, localClipRect, hitTestPoint, false, transformState, zOffset);
3765                 if (oldHasTransform)
3766                     childLayer->m_transform = adoptPtr(new TransformationMatrix(oldTransform));
3767                 else
3768                     childLayer->m_transform.clear();
3769             } else {
3770                 // Adjust the transform such that the renderer's upper left corner will be at (0,0) in user space.
3771                 // This involves subtracting out the position of the layer in our current coordinate space.
3772                 RenderLayer* nextLayer = columnLayers[columnIndex - 1];
3773                 RefPtr<HitTestingTransformState> newTransformState = nextLayer->createLocalTransformState(rootLayer, nextLayer, localClipRect, hitTestPoint, transformState);
3774                 newTransformState->translate(offset.width(), offset.height(), HitTestingTransformState::AccumulateTransform);
3775                 LayoutPoint localPoint = roundedLayoutPoint(newTransformState->mappedPoint());
3776                 LayoutRect localHitTestRect = newTransformState->mappedQuad().enclosingBoundingBox();
3777                 newTransformState->flatten();
3778
3779                 hitLayer = hitTestChildLayerColumns(childLayer, columnLayers[columnIndex - 1], request, result, localHitTestRect, localPoint,
3780                                                     newTransformState.get(), zOffset, columnLayers, columnIndex - 1);
3781             }
3782
3783             if (hitLayer)
3784                 return hitLayer;
3785         }
3786     }
3787
3788     return 0;
3789 }
3790
3791 void RenderLayer::updateClipRects(const RenderLayer* rootLayer, RenderRegion* region, OverlayScrollbarSizeRelevancy relevancy)
3792 {
3793     if (m_clipRects) {
3794         ASSERT(rootLayer == m_clipRectsRoot);
3795         return; // We have the correct cached value.
3796     }
3797     
3798     // For transformed layers, the root layer was shifted to be us, so there is no need to
3799     // examine the parent.  We want to cache clip rects with us as the root.
3800     RenderLayer* parentLayer = rootLayer != this ? parent() : 0;
3801     if (parentLayer)
3802         parentLayer->updateClipRects(rootLayer, region, relevancy);
3803
3804     ClipRects clipRects;
3805     calculateClipRects(rootLayer, region, clipRects, true, relevancy);
3806
3807     if (parentLayer && parentLayer->clipRects() && clipRects == *parentLayer->clipRects())
3808         m_clipRects = parentLayer->clipRects();
3809     else
3810         m_clipRects = new (renderer()->renderArena()) ClipRects(clipRects);
3811     m_clipRects->ref();
3812 #ifndef NDEBUG
3813     m_clipRectsRoot = rootLayer;
3814 #endif
3815 }
3816
3817 void RenderLayer::calculateClipRects(const RenderLayer* rootLayer, RenderRegion* region, ClipRects& clipRects,
3818     bool useCached, OverlayScrollbarSizeRelevancy relevancy) const
3819 {
3820     if (!parent()) {
3821         // The root layer's clip rect is always infinite.
3822         clipRects.reset(PaintInfo::infiniteRect());
3823         return;
3824     }
3825
3826     // For transformed layers, the root layer was shifted to be us, so there is no need to
3827     // examine the parent.  We want to cache clip rects with us as the root.
3828     RenderLayer* parentLayer = rootLayer != this ? parent() : 0;
3829     
3830     // Ensure that our parent's clip has been calculated so that we can examine the values.
3831     if (parentLayer) {
3832         if (useCached && parentLayer->clipRects())
3833             clipRects = *parentLayer->clipRects();
3834         else
3835             parentLayer->calculateClipRects(rootLayer, region, clipRects);
3836     } else
3837         clipRects.reset(PaintInfo::infiniteRect());
3838
3839     // A fixed object is essentially the root of its containing block hierarchy, so when
3840     // we encounter such an object, we reset our clip rects to the fixedClipRect.
3841     if (renderer()->style()->position() == FixedPosition) {
3842         clipRects.setPosClipRect(clipRects.fixedClipRect());
3843         clipRects.setOverflowClipRect(clipRects.fixedClipRect());
3844         clipRects.setFixed(true);
3845     }
3846     else if (renderer()->style()->position() == RelativePosition)
3847         clipRects.setPosClipRect(clipRects.overflowClipRect());
3848     else if (renderer()->style()->position() == AbsolutePosition)
3849   &n