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