webkitpy: Special-case win32 calls in file_lock and executive.
[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()
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, toRenderBox(actualRenderer));
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 (scrollbar->isCustomScrollbar())
2214             toRenderScrollbar(scrollbar.get())->clearOwningRenderer();
2215         else {
2216             if (orientation == HorizontalScrollbar)
2217                 willRemoveHorizontalScrollbar(scrollbar.get());
2218             else
2219                 willRemoveVerticalScrollbar(scrollbar.get());
2220         }
2221
2222         scrollbar->removeFromParent();
2223         scrollbar->disconnectFromScrollableArea();
2224         scrollbar = 0;
2225     }
2226 }
2227
2228 bool RenderLayer::scrollsOverflow() const
2229 {
2230     if (!renderer()->isBox())
2231         return false;
2232
2233     return toRenderBox(renderer())->scrollsOverflow();
2234 }
2235
2236 bool RenderLayer::allowsScrolling() const
2237 {
2238     return (m_hBar && m_hBar->enabled()) || (m_vBar && m_vBar->enabled());
2239 }
2240
2241 void RenderLayer::setHasHorizontalScrollbar(bool hasScrollbar)
2242 {
2243     if (hasScrollbar == hasHorizontalScrollbar())
2244         return;
2245
2246     if (hasScrollbar)
2247         m_hBar = createScrollbar(HorizontalScrollbar);
2248     else
2249         destroyScrollbar(HorizontalScrollbar);
2250
2251     // Destroying or creating one bar can cause our scrollbar corner to come and go.  We need to update the opposite scrollbar's style.
2252     if (m_hBar)
2253         m_hBar->styleChanged();
2254     if (m_vBar)
2255         m_vBar->styleChanged();
2256
2257 #if ENABLE(DASHBOARD_SUPPORT)
2258     // Force an update since we know the scrollbars have changed things.
2259     if (renderer()->document()->hasDashboardRegions())
2260         renderer()->document()->setDashboardRegionsDirty(true);
2261 #endif
2262 }
2263
2264 void RenderLayer::setHasVerticalScrollbar(bool hasScrollbar)
2265 {
2266     if (hasScrollbar == hasVerticalScrollbar())
2267         return;
2268
2269     if (hasScrollbar)
2270         m_vBar = createScrollbar(VerticalScrollbar);
2271     else
2272         destroyScrollbar(VerticalScrollbar);
2273
2274      // Destroying or creating one bar can cause our scrollbar corner to come and go.  We need to update the opposite scrollbar's style.
2275     if (m_hBar)
2276         m_hBar->styleChanged();
2277     if (m_vBar)
2278         m_vBar->styleChanged();
2279
2280 #if ENABLE(DASHBOARD_SUPPORT)
2281     // Force an update since we know the scrollbars have changed things.
2282     if (renderer()->document()->hasDashboardRegions())
2283         renderer()->document()->setDashboardRegionsDirty(true);
2284 #endif
2285 }
2286
2287 ScrollableArea* RenderLayer::enclosingScrollableArea() const
2288 {
2289     if (RenderLayer* scrollableLayer = enclosingScrollableLayer())
2290         return scrollableLayer;
2291
2292     // FIXME: We should return the frame view here (or possibly an ancestor frame view,
2293     // if the frame view isn't scrollable.
2294     return 0;
2295 }
2296
2297 int RenderLayer::verticalScrollbarWidth(OverlayScrollbarSizeRelevancy relevancy) const
2298 {
2299     if (!m_vBar || (m_vBar->isOverlayScrollbar() && relevancy == IgnoreOverlayScrollbarSize))
2300         return 0;
2301     return m_vBar->width();
2302 }
2303
2304 int RenderLayer::horizontalScrollbarHeight(OverlayScrollbarSizeRelevancy relevancy) const
2305 {
2306     if (!m_hBar || (m_hBar->isOverlayScrollbar() && relevancy == IgnoreOverlayScrollbarSize))
2307         return 0;
2308     return m_hBar->height();
2309 }
2310
2311 IntSize RenderLayer::offsetFromResizeCorner(const IntPoint& absolutePoint) const
2312 {
2313     // Currently the resize corner is either the bottom right corner or the bottom left corner.
2314     // FIXME: This assumes the location is 0, 0. Is this guaranteed to always be the case?
2315     IntSize elementSize = size();
2316     if (renderer()->style()->shouldPlaceBlockDirectionScrollbarOnLogicalLeft())
2317         elementSize.setWidth(0);
2318     IntPoint resizerPoint = toPoint(elementSize);
2319     IntPoint localPoint = roundedIntPoint(absoluteToContents(absolutePoint));
2320     return localPoint - resizerPoint;
2321 }
2322
2323 bool RenderLayer::hasOverflowControls() const
2324 {
2325     return m_hBar || m_vBar || m_scrollCorner || renderer()->style()->resize() != RESIZE_NONE;
2326 }
2327
2328 void RenderLayer::positionOverflowControls(const IntSize& offsetFromLayer)
2329 {
2330     if (!m_hBar && !m_vBar && (!renderer()->hasOverflowClip() || renderer()->style()->resize() == RESIZE_NONE))
2331         return;
2332     
2333     RenderBox* box = renderBox();
2334     if (!box)
2335         return;
2336
2337     const IntRect borderBox = box->pixelSnappedBorderBoxRect();
2338     const IntRect& scrollCorner = scrollCornerRect();
2339     IntRect absBounds(borderBox.location() + offsetFromLayer, borderBox.size());
2340     if (m_vBar)
2341         m_vBar->setFrameRect(IntRect(verticalScrollbarStart(absBounds.x(), absBounds.maxX()),
2342                                      absBounds.y() + box->borderTop(),
2343                                      m_vBar->width(),
2344                                      absBounds.height() - (box->borderTop() + box->borderBottom()) - scrollCorner.height()));
2345
2346     if (m_hBar)
2347         m_hBar->setFrameRect(IntRect(horizontalScrollbarStart(absBounds.x()),
2348                                      absBounds.maxY() - box->borderBottom() - m_hBar->height(),
2349                                      absBounds.width() - (box->borderLeft() + box->borderRight()) - scrollCorner.width(),
2350                                      m_hBar->height()));
2351
2352 #if USE(ACCELERATED_COMPOSITING)
2353     if (GraphicsLayer* layer = layerForHorizontalScrollbar()) {
2354         if (m_hBar) {
2355             layer->setPosition(m_hBar->frameRect().location() - offsetFromLayer);
2356             layer->setSize(m_hBar->frameRect().size());
2357         }
2358         layer->setDrawsContent(m_hBar);
2359     }
2360     if (GraphicsLayer* layer = layerForVerticalScrollbar()) {
2361         if (m_vBar) {
2362             layer->setPosition(m_vBar->frameRect().location() - offsetFromLayer);
2363             layer->setSize(m_vBar->frameRect().size());
2364         }
2365         layer->setDrawsContent(m_vBar);
2366     }
2367
2368     if (GraphicsLayer* layer = layerForScrollCorner()) {
2369         const LayoutRect& scrollCornerAndResizer = scrollCornerAndResizerRect();
2370         layer->setPosition(scrollCornerAndResizer.location());
2371         layer->setSize(scrollCornerAndResizer.size());
2372         layer->setDrawsContent(!scrollCornerAndResizer.isEmpty());
2373     }
2374 #endif
2375
2376     if (m_scrollCorner)
2377         m_scrollCorner->setFrameRect(scrollCorner);
2378     if (m_resizer)
2379         m_resizer->setFrameRect(resizerCornerRect(this, borderBox));
2380 }
2381
2382 int RenderLayer::scrollWidth() const
2383 {
2384     ASSERT(renderBox());
2385     if (m_scrollDimensionsDirty)
2386         const_cast<RenderLayer*>(this)->computeScrollDimensions();
2387     return snapSizeToPixel(m_scrollSize.width(), renderBox()->clientLeft());
2388 }
2389
2390 int RenderLayer::scrollHeight() const
2391 {
2392     ASSERT(renderBox());
2393     if (m_scrollDimensionsDirty)
2394         const_cast<RenderLayer*>(this)->computeScrollDimensions();
2395     return snapSizeToPixel(m_scrollSize.height(), renderBox()->clientTop());
2396 }
2397
2398 LayoutUnit RenderLayer::overflowTop() const
2399 {
2400     RenderBox* box = renderBox();
2401     LayoutRect overflowRect(box->layoutOverflowRect());
2402     box->flipForWritingMode(overflowRect);
2403     return overflowRect.y();
2404 }
2405
2406 LayoutUnit RenderLayer::overflowBottom() const
2407 {
2408     RenderBox* box = renderBox();
2409     LayoutRect overflowRect(box->layoutOverflowRect());
2410     box->flipForWritingMode(overflowRect);
2411     return overflowRect.maxY();
2412 }
2413
2414 LayoutUnit RenderLayer::overflowLeft() const
2415 {
2416     RenderBox* box = renderBox();
2417     LayoutRect overflowRect(box->layoutOverflowRect());
2418     box->flipForWritingMode(overflowRect);
2419     return overflowRect.x();
2420 }
2421
2422 LayoutUnit RenderLayer::overflowRight() const
2423 {
2424     RenderBox* box = renderBox();
2425     LayoutRect overflowRect(box->layoutOverflowRect());
2426     box->flipForWritingMode(overflowRect);
2427     return overflowRect.maxX();
2428 }
2429
2430 void RenderLayer::computeScrollDimensions()
2431 {
2432     RenderBox* box = renderBox();
2433     ASSERT(box);
2434
2435     m_scrollDimensionsDirty = false;
2436
2437     m_scrollOverflow.setWidth(overflowLeft() - box->borderLeft());
2438     m_scrollOverflow.setHeight(overflowTop() - box->borderTop());
2439
2440     m_scrollSize.setWidth(overflowRight() - overflowLeft());
2441     m_scrollSize.setHeight(overflowBottom() - overflowTop());
2442
2443     setScrollOrigin(IntPoint(-m_scrollOverflow.width(), -m_scrollOverflow.height()));
2444 }
2445
2446 bool RenderLayer::hasHorizontalOverflow() const
2447 {
2448     ASSERT(!m_scrollDimensionsDirty);
2449
2450     return scrollWidth() > renderBox()->pixelSnappedClientWidth();
2451 }
2452
2453 bool RenderLayer::hasVerticalOverflow() const
2454 {
2455     ASSERT(!m_scrollDimensionsDirty);
2456
2457     return scrollHeight() > renderBox()->pixelSnappedClientHeight();
2458 }
2459
2460 void RenderLayer::updateScrollbarsAfterLayout()
2461 {
2462     RenderBox* box = renderBox();
2463     ASSERT(box);
2464
2465     bool hasHorizontalOverflow = this->hasHorizontalOverflow();
2466     bool hasVerticalOverflow = this->hasVerticalOverflow();
2467
2468     // overflow:scroll should just enable/disable.
2469     if (m_hBar && renderer()->style()->overflowX() == OSCROLL)
2470         m_hBar->setEnabled(hasHorizontalOverflow);
2471     if (m_vBar && renderer()->style()->overflowY() == OSCROLL)
2472         m_vBar->setEnabled(hasVerticalOverflow);
2473
2474     // overflow:auto may need to lay out again if scrollbars got added/removed.
2475     bool autoHorizontalScrollBarChanged = box->hasAutoHorizontalScrollbar() && (hasHorizontalScrollbar() != hasHorizontalOverflow);
2476     bool autoVerticalScrollBarChanged = box->hasAutoVerticalScrollbar() && (hasVerticalScrollbar() != hasVerticalOverflow);
2477
2478     if (autoHorizontalScrollBarChanged || autoVerticalScrollBarChanged) {
2479         if (box->hasAutoHorizontalScrollbar())
2480             setHasHorizontalScrollbar(hasHorizontalOverflow);
2481         if (box->hasAutoVerticalScrollbar())
2482             setHasVerticalScrollbar(hasVerticalOverflow);
2483
2484 #if ENABLE(DASHBOARD_SUPPORT)
2485         // Force an update since we know the scrollbars have changed things.
2486         if (renderer()->document()->hasDashboardRegions())
2487             renderer()->document()->setDashboardRegionsDirty(true);
2488 #endif
2489
2490         renderer()->repaint();
2491
2492         if (renderer()->style()->overflowX() == OAUTO || renderer()->style()->overflowY() == OAUTO) {
2493             if (!m_inOverflowRelayout) {
2494                 // Our proprietary overflow: overlay value doesn't trigger a layout.
2495                 m_inOverflowRelayout = true;
2496                 renderer()->setNeedsLayout(true, MarkOnlyThis);
2497                 if (renderer()->isRenderBlock()) {
2498                     RenderBlock* block = toRenderBlock(renderer());
2499                     block->scrollbarsChanged(autoHorizontalScrollBarChanged, autoVerticalScrollBarChanged);
2500                     block->layoutBlock(true);
2501                 } else
2502                     renderer()->layout();
2503                 m_inOverflowRelayout = false;
2504             }
2505         }
2506     }
2507
2508     // Set up the range (and page step/line step).
2509     if (m_hBar) {
2510         int clientWidth = box->pixelSnappedClientWidth();
2511         int pageStep = max(max<int>(clientWidth * Scrollbar::minFractionToStepWhenPaging(), clientWidth - Scrollbar::maxOverlapBetweenPages()), 1);
2512         m_hBar->setSteps(Scrollbar::pixelsPerLineStep(), pageStep);
2513         m_hBar->setProportion(clientWidth, m_scrollSize.width());
2514     }
2515     if (m_vBar) {
2516         int clientHeight = box->pixelSnappedClientHeight();
2517         int pageStep = max(max<int>(clientHeight * Scrollbar::minFractionToStepWhenPaging(), clientHeight - Scrollbar::maxOverlapBetweenPages()), 1);
2518         m_vBar->setSteps(Scrollbar::pixelsPerLineStep(), pageStep);
2519         m_vBar->setProportion(clientHeight, m_scrollSize.height());
2520     }
2521
2522     updateScrollableAreaSet((hasHorizontalOverflow || hasVerticalOverflow) && scrollsOverflow());
2523 }
2524
2525 void RenderLayer::updateScrollInfoAfterLayout()
2526 {
2527     RenderBox* box = renderBox();
2528     if (!box)
2529         return;
2530
2531     m_scrollDimensionsDirty = true;
2532     IntSize scrollOffsetOriginal(scrollXOffset(), scrollYOffset());
2533
2534     computeScrollDimensions();
2535
2536     if (box->style()->overflowX() != OMARQUEE) {
2537         // Layout may cause us to be at an invalid scroll position. In this case we need
2538         // to pull our scroll offsets back to the max (or push them up to the min).
2539         int newX = max(0, min<int>(scrollXOffset(), scrollWidth() - box->clientWidth()));
2540         int newY = max(0, min<int>(scrollYOffset(), scrollHeight() - box->clientHeight()));
2541         if (newX != scrollXOffset() || newY != scrollYOffset())
2542             scrollToOffset(newX, newY);
2543     }
2544
2545     updateScrollbarsAfterLayout();
2546
2547     if (scrollOffsetOriginal != scrollOffset())
2548         scrollToOffsetWithoutAnimation(IntPoint(scrollXOffset(), scrollYOffset()));
2549 }
2550
2551 void RenderLayer::paintOverflowControls(GraphicsContext* context, const IntPoint& paintOffset, const IntRect& damageRect, bool paintingOverlayControls)
2552 {
2553     // Don't do anything if we have no overflow.
2554     if (!renderer()->hasOverflowClip())
2555         return;
2556
2557     // Overlay scrollbars paint in a second pass through the layer tree so that they will paint
2558     // on top of everything else. If this is the normal painting pass, paintingOverlayControls
2559     // will be false, and we should just tell the root layer that there are overlay scrollbars
2560     // that need to be painted. That will cause the second pass through the layer tree to run,
2561     // and we'll paint the scrollbars then. In the meantime, cache tx and ty so that the 
2562     // second pass doesn't need to re-enter the RenderTree to get it right.
2563     if (hasOverlayScrollbars() && !paintingOverlayControls) {
2564         RenderView* renderView = renderer()->view();
2565         renderView->layer()->setContainsDirtyOverlayScrollbars(true);
2566         m_cachedOverlayScrollbarOffset = paintOffset;
2567         renderView->frameView()->setContainsScrollableAreaWithOverlayScrollbars(true);
2568         return;
2569     }
2570
2571     // This check is required to avoid painting custom CSS scrollbars twice.
2572     if (paintingOverlayControls && !hasOverlayScrollbars())
2573         return;
2574
2575     IntPoint adjustedPaintOffset = paintOffset;
2576     if (paintingOverlayControls)
2577         adjustedPaintOffset = m_cachedOverlayScrollbarOffset;
2578
2579     // Move the scrollbar widgets if necessary.  We normally move and resize widgets during layout, but sometimes
2580     // widgets can move without layout occurring (most notably when you scroll a document that
2581     // contains fixed positioned elements).
2582     positionOverflowControls(toSize(adjustedPaintOffset));
2583
2584     // Now that we're sure the scrollbars are in the right place, paint them.
2585     if (m_hBar
2586 #if USE(ACCELERATED_COMPOSITING)
2587         && !layerForHorizontalScrollbar()
2588 #endif
2589               )
2590         m_hBar->paint(context, damageRect);
2591     if (m_vBar
2592 #if USE(ACCELERATED_COMPOSITING)
2593         && !layerForVerticalScrollbar()
2594 #endif
2595               )
2596         m_vBar->paint(context, damageRect);
2597
2598 #if USE(ACCELERATED_COMPOSITING)
2599     if (layerForScrollCorner())
2600         return;
2601 #endif
2602
2603     // We fill our scroll corner with white if we have a scrollbar that doesn't run all the way up to the
2604     // edge of the box.
2605     paintScrollCorner(context, adjustedPaintOffset, damageRect);
2606     
2607     // Paint our resizer last, since it sits on top of the scroll corner.
2608     paintResizer(context, adjustedPaintOffset, damageRect);
2609 }
2610
2611 void RenderLayer::paintScrollCorner(GraphicsContext* context, const IntPoint& paintOffset, const IntRect& damageRect)
2612 {
2613     RenderBox* box = renderBox();
2614     ASSERT(box);
2615
2616     IntRect absRect = scrollCornerRect();
2617     absRect.moveBy(paintOffset);
2618     if (!absRect.intersects(damageRect))
2619         return;
2620
2621     if (context->updatingControlTints()) {
2622         updateScrollCornerStyle();
2623         return;
2624     }
2625
2626     if (m_scrollCorner) {
2627         m_scrollCorner->paintIntoRect(context, paintOffset, absRect);
2628         return;
2629     }
2630
2631     // We don't want to paint white if we have overlay scrollbars, since we need
2632     // to see what is behind it.
2633     if (!hasOverlayScrollbars())
2634         context->fillRect(absRect, Color::white, box->style()->colorSpace());
2635 }
2636
2637 void RenderLayer::drawPlatformResizerImage(GraphicsContext* context, IntRect resizerCornerRect)
2638 {
2639     float deviceScaleFactor = WebCore::deviceScaleFactor(renderer()->frame());
2640
2641     RefPtr<Image> resizeCornerImage;
2642     IntSize cornerResizerSize;
2643     if (deviceScaleFactor >= 2) {
2644         DEFINE_STATIC_LOCAL(Image*, resizeCornerImageHiRes, (Image::loadPlatformResource("textAreaResizeCorner@2x").leakRef()));
2645         resizeCornerImage = resizeCornerImageHiRes;
2646         cornerResizerSize = resizeCornerImage->size();
2647         cornerResizerSize.scale(0.5f);
2648     } else {
2649         DEFINE_STATIC_LOCAL(Image*, resizeCornerImageLoRes, (Image::loadPlatformResource("textAreaResizeCorner").leakRef()));
2650         resizeCornerImage = resizeCornerImageLoRes;
2651         cornerResizerSize = resizeCornerImage->size();
2652     }
2653
2654     IntRect imageRect(resizerCornerRect.maxXMaxYCorner() - cornerResizerSize, cornerResizerSize);
2655     if (renderer()->style()->shouldPlaceBlockDirectionScrollbarOnLogicalLeft()) {
2656         context->save();
2657         context->translate(imageRect.x(), imageRect.y());
2658         imageRect.setX(0);
2659         imageRect.setY(0);
2660         context->scale(FloatSize(-1.0, 1.0));
2661         context->drawImage(resizeCornerImage.get(), renderer()->style()->colorSpace(), imageRect);
2662         context->restore();
2663         return;
2664     }
2665     context->drawImage(resizeCornerImage.get(), renderer()->style()->colorSpace(), imageRect);
2666 }
2667
2668 void RenderLayer::paintResizer(GraphicsContext* context, const IntPoint& paintOffset, const IntRect& damageRect)
2669 {
2670     if (renderer()->style()->resize() == RESIZE_NONE)
2671         return;
2672
2673     RenderBox* box = renderBox();
2674     ASSERT(box);
2675
2676     IntRect absRect = resizerCornerRect(this, box->pixelSnappedBorderBoxRect());
2677     absRect.moveBy(paintOffset);
2678     if (!absRect.intersects(damageRect))
2679         return;
2680
2681     if (context->updatingControlTints()) {
2682         updateResizerStyle();
2683         return;
2684     }
2685     
2686     if (m_resizer) {
2687         m_resizer->paintIntoRect(context, paintOffset, absRect);
2688         return;
2689     }
2690
2691     drawPlatformResizerImage(context, absRect);
2692
2693     // Draw a frame around the resizer (1px grey line) if there are any scrollbars present.
2694     // Clipping will exclude the right and bottom edges of this frame.
2695     if (!hasOverlayScrollbars() && (m_vBar || m_hBar)) {
2696         GraphicsContextStateSaver stateSaver(*context);
2697         context->clip(absRect);
2698         IntRect largerCorner = absRect;
2699         largerCorner.setSize(IntSize(largerCorner.width() + 1, largerCorner.height() + 1));
2700         context->setStrokeColor(Color(makeRGB(217, 217, 217)), ColorSpaceDeviceRGB);
2701         context->setStrokeThickness(1.0f);
2702         context->setFillColor(Color::transparent, ColorSpaceDeviceRGB);
2703         context->drawRect(largerCorner);
2704     }
2705 }
2706
2707 bool RenderLayer::isPointInResizeControl(const IntPoint& absolutePoint) const
2708 {
2709     if (!renderer()->hasOverflowClip() || renderer()->style()->resize() == RESIZE_NONE)
2710         return false;
2711     
2712     RenderBox* box = renderBox();
2713     ASSERT(box);
2714
2715     IntPoint localPoint = roundedIntPoint(absoluteToContents(absolutePoint));
2716
2717     IntRect localBounds(0, 0, box->pixelSnappedWidth(), box->pixelSnappedHeight());
2718     return resizerCornerRect(this, localBounds).contains(localPoint);
2719 }
2720     
2721 bool RenderLayer::hitTestOverflowControls(HitTestResult& result, const IntPoint& localPoint)
2722 {
2723     if (!m_hBar && !m_vBar && (!renderer()->hasOverflowClip() || renderer()->style()->resize() == RESIZE_NONE))
2724         return false;
2725
2726     RenderBox* box = renderBox();
2727     ASSERT(box);
2728     
2729     IntRect resizeControlRect;
2730     if (renderer()->style()->resize() != RESIZE_NONE) {
2731         resizeControlRect = resizerCornerRect(this, box->pixelSnappedBorderBoxRect());
2732         if (resizeControlRect.contains(localPoint))
2733             return true;
2734     }
2735
2736     int resizeControlSize = max(resizeControlRect.height(), 0);
2737
2738     if (m_vBar && m_vBar->shouldParticipateInHitTesting()) {
2739         LayoutRect vBarRect(verticalScrollbarStart(0, box->width()),
2740                             box->borderTop(),
2741                             m_vBar->width(),
2742                             box->height() - (box->borderTop() + box->borderBottom()) - (m_hBar ? m_hBar->height() : resizeControlSize));
2743         if (vBarRect.contains(localPoint)) {
2744             result.setScrollbar(m_vBar.get());
2745             return true;
2746         }
2747     }
2748
2749     resizeControlSize = max(resizeControlRect.width(), 0);
2750     if (m_hBar && m_hBar->shouldParticipateInHitTesting()) {
2751         LayoutRect hBarRect(horizontalScrollbarStart(0),
2752                             box->height() - box->borderBottom() - m_hBar->height(),
2753                             box->width() - (box->borderLeft() + box->borderRight()) - (m_vBar ? m_vBar->width() : resizeControlSize),
2754                             m_hBar->height());
2755         if (hBarRect.contains(localPoint)) {
2756             result.setScrollbar(m_hBar.get());
2757             return true;
2758         }
2759     }
2760
2761     return false;
2762 }
2763
2764 bool RenderLayer::scroll(ScrollDirection direction, ScrollGranularity granularity, float multiplier)
2765 {
2766     return ScrollableArea::scroll(direction, granularity, multiplier);
2767 }
2768
2769 void RenderLayer::paint(GraphicsContext* context, const LayoutRect& damageRect, PaintBehavior paintBehavior, RenderObject* paintingRoot, RenderRegion* region, PaintLayerFlags paintFlags)
2770 {
2771     OverlapTestRequestMap overlapTestRequests;
2772     paintLayer(this, context, damageRect, paintBehavior, paintingRoot, region, &overlapTestRequests, paintFlags);
2773     OverlapTestRequestMap::iterator end = overlapTestRequests.end();
2774     for (OverlapTestRequestMap::iterator it = overlapTestRequests.begin(); it != end; ++it)
2775         it->first->setOverlapTestResult(false);
2776 }
2777
2778 void RenderLayer::paintOverlayScrollbars(GraphicsContext* context, const LayoutRect& damageRect, PaintBehavior paintBehavior, RenderObject* paintingRoot)
2779 {
2780     if (!m_containsDirtyOverlayScrollbars)
2781         return;
2782     paintLayer(this, context, damageRect, paintBehavior, paintingRoot, 0, 0, PaintLayerHaveTransparency | PaintLayerTemporaryClipRects 
2783                | PaintLayerPaintingOverlayScrollbars);
2784     m_containsDirtyOverlayScrollbars = false;
2785 }
2786
2787 #ifndef DISABLE_ROUNDED_CORNER_CLIPPING
2788 static bool inContainingBlockChain(RenderLayer* startLayer, RenderLayer* endLayer)
2789 {
2790     if (startLayer == endLayer)
2791         return true;
2792     
2793     RenderView* view = startLayer->renderer()->view();
2794     for (RenderBlock* currentBlock = startLayer->renderer()->containingBlock(); currentBlock && currentBlock != view; currentBlock = currentBlock->containingBlock()) {
2795         if (currentBlock->layer() == endLayer)
2796             return true;
2797     }
2798     
2799     return false;
2800 }
2801 #endif
2802
2803 void RenderLayer::clipToRect(RenderLayer* rootLayer, GraphicsContext* context, const LayoutRect& paintDirtyRect, const ClipRect& clipRect,
2804                              BorderRadiusClippingRule rule)
2805 {
2806     if (clipRect.rect() == paintDirtyRect)
2807         return;
2808     context->save();
2809     context->clip(pixelSnappedIntRect(clipRect.rect()));
2810     
2811     if (!clipRect.hasRadius())
2812         return;
2813
2814 #ifndef DISABLE_ROUNDED_CORNER_CLIPPING
2815     // If the clip rect has been tainted by a border radius, then we have to walk up our layer chain applying the clips from
2816     // any layers with overflow. The condition for being able to apply these clips is that the overflow object be in our
2817     // containing block chain so we check that also.
2818     for (RenderLayer* layer = rule == IncludeSelfForBorderRadius ? this : parent(); layer; layer = layer->parent()) {
2819         if (layer->renderer()->hasOverflowClip() && layer->renderer()->style()->hasBorderRadius() && inContainingBlockChain(this, layer)) {
2820                 LayoutPoint delta;
2821                 layer->convertToLayerCoords(rootLayer, delta);
2822                 context->addRoundedRectClip(layer->renderer()->style()->getRoundedInnerBorderFor(LayoutRect(delta, layer->size())));
2823         }
2824
2825         if (layer == rootLayer)
2826             break;
2827     }
2828 #endif
2829 }
2830
2831 void RenderLayer::restoreClip(GraphicsContext* context, const LayoutRect& paintDirtyRect, const ClipRect& clipRect)
2832 {
2833     if (clipRect.rect() == paintDirtyRect)
2834         return;
2835     context->restore();
2836 }
2837
2838 static void performOverlapTests(OverlapTestRequestMap& overlapTestRequests, const RenderLayer* rootLayer, const RenderLayer* layer)
2839 {
2840     Vector<OverlapTestRequestClient*> overlappedRequestClients;
2841     OverlapTestRequestMap::iterator end = overlapTestRequests.end();
2842     LayoutRect boundingBox = layer->boundingBox(rootLayer);
2843     for (OverlapTestRequestMap::iterator it = overlapTestRequests.begin(); it != end; ++it) {
2844         if (!boundingBox.intersects(it->second))
2845             continue;
2846
2847         it->first->setOverlapTestResult(true);
2848         overlappedRequestClients.append(it->first);
2849     }
2850     for (size_t i = 0; i < overlappedRequestClients.size(); ++i)
2851         overlapTestRequests.remove(overlappedRequestClients[i]);
2852 }
2853
2854 #if USE(ACCELERATED_COMPOSITING)
2855 static bool shouldDoSoftwarePaint(const RenderLayer* layer, bool paintingReflection)
2856 {
2857     return paintingReflection && !layer->has3DTransform();
2858 }
2859 #endif
2860     
2861 static inline bool shouldSuppressPaintingLayer(RenderLayer* layer)
2862 {
2863     // Avoid painting descendants of the root layer when stylesheets haven't loaded. This eliminates FOUC.
2864     // It's ok not to draw, because later on, when all the stylesheets do load, updateStyleSelector on the Document
2865     // will do a full repaint().
2866     if (layer->renderer()->document()->didLayoutWithPendingStylesheets() && !layer->renderer()->isRenderView() && !layer->renderer()->isRoot())
2867         return true;
2868
2869     // Avoid painting all layers if the document is in a state where visual updates aren't allowed.
2870     // A full repaint will occur in Document::implicitClose() if painting is suppressed here.
2871     if (!layer->renderer()->document()->visualUpdatesAllowed())
2872         return true;
2873
2874     return false;
2875 }
2876
2877
2878 void RenderLayer::paintLayer(RenderLayer* rootLayer, GraphicsContext* context,
2879                         const LayoutRect& paintDirtyRect, PaintBehavior paintBehavior,
2880                         RenderObject* paintingRoot, RenderRegion* region, OverlapTestRequestMap* overlapTestRequests,
2881                         PaintLayerFlags paintFlags)
2882 {
2883 #if USE(ACCELERATED_COMPOSITING)
2884     if (isComposited()) {
2885         // The updatingControlTints() painting pass goes through compositing layers,
2886         // but we need to ensure that we don't cache clip rects computed with the wrong root in this case.
2887         if (context->updatingControlTints() || (paintBehavior & PaintBehaviorFlattenCompositingLayers))
2888             paintFlags |= PaintLayerTemporaryClipRects;
2889         else if (!backing()->paintsIntoWindow() && !backing()->paintsIntoCompositedAncestor() && !shouldDoSoftwarePaint(this, paintFlags & PaintLayerPaintingReflection)) {
2890             // If this RenderLayer should paint into its backing, that will be done via RenderLayerBacking::paintIntoLayer().
2891             return;
2892         }
2893     }
2894 #endif
2895
2896     if (shouldSuppressPaintingLayer(this))
2897         return;
2898     
2899     // If this layer is totally invisible then there is nothing to paint.
2900     if (!renderer()->opacity())
2901         return;
2902
2903     if (paintsWithTransparency(paintBehavior))
2904         paintFlags |= PaintLayerHaveTransparency;
2905
2906     // PaintLayerAppliedTransform is used in RenderReplica, to avoid applying the transform twice.
2907     if (paintsWithTransform(paintBehavior) && !(paintFlags & PaintLayerAppliedTransform)) {
2908         TransformationMatrix layerTransform = renderableTransform(paintBehavior);
2909         // If the transform can't be inverted, then don't paint anything.
2910         if (!layerTransform.isInvertible())
2911             return;
2912
2913         // If we have a transparency layer enclosing us and we are the root of a transform, then we need to establish the transparency
2914         // layer from the parent now, assuming there is a parent
2915         if (paintFlags & PaintLayerHaveTransparency) {
2916             if (parent())
2917                 parent()->beginTransparencyLayers(context, rootLayer, paintDirtyRect, paintBehavior);
2918             else
2919                 beginTransparencyLayers(context, rootLayer, paintDirtyRect, paintBehavior);
2920         }
2921
2922         // Make sure the parent's clip rects have been calculated.
2923         ClipRect clipRect = paintDirtyRect;
2924         if (parent()) {
2925             clipRect = backgroundClipRect(rootLayer, region, paintFlags & PaintLayerTemporaryClipRects);
2926             clipRect.intersect(paintDirtyRect);
2927         
2928             // Push the parent coordinate space's clip.
2929             parent()->clipToRect(rootLayer, context, paintDirtyRect, clipRect);
2930         }
2931
2932         // Adjust the transform such that the renderer's upper left corner will paint at (0,0) in user space.
2933         // This involves subtracting out the position of the layer in our current coordinate space.
2934         LayoutPoint delta;
2935         convertToLayerCoords(rootLayer, delta);
2936         TransformationMatrix transform(layerTransform);
2937         transform.translateRight(delta.x(), delta.y());
2938         
2939         // Apply the transform.
2940         {
2941             GraphicsContextStateSaver stateSaver(*context);
2942             context->concatCTM(transform.toAffineTransform());
2943
2944             // Now do a paint with the root layer shifted to be us.
2945             paintLayerContentsAndReflection(this, context, transform.inverse().mapRect(paintDirtyRect), paintBehavior, paintingRoot, region, overlapTestRequests, paintFlags);
2946         }        
2947
2948         // Restore the clip.
2949         if (parent())
2950             parent()->restoreClip(context, paintDirtyRect, clipRect);
2951
2952         return;
2953     }
2954     
2955     paintLayerContentsAndReflection(rootLayer, context, paintDirtyRect, paintBehavior, paintingRoot, region, overlapTestRequests, paintFlags);
2956 }
2957
2958 void RenderLayer::paintLayerContentsAndReflection(RenderLayer* rootLayer, GraphicsContext* context,
2959                         const LayoutRect& paintDirtyRect, PaintBehavior paintBehavior,
2960                         RenderObject* paintingRoot, RenderRegion* region, OverlapTestRequestMap* overlapTestRequests,
2961                         PaintLayerFlags paintFlags)
2962 {
2963     PaintLayerFlags localPaintFlags = paintFlags & ~(PaintLayerAppliedTransform);
2964
2965     // Paint the reflection first if we have one.
2966     if (m_reflection && !m_paintingInsideReflection) {
2967         // Mark that we are now inside replica painting.
2968         m_paintingInsideReflection = true;
2969         reflectionLayer()->paintLayer(rootLayer, context, paintDirtyRect, paintBehavior, paintingRoot, region, overlapTestRequests, localPaintFlags | PaintLayerPaintingReflection);
2970         m_paintingInsideReflection = false;
2971     }
2972
2973     localPaintFlags |= PaintLayerPaintingCompositingAllPhases;
2974     paintLayerContents(rootLayer, context, paintDirtyRect, paintBehavior, paintingRoot, region, overlapTestRequests, localPaintFlags);
2975 }
2976
2977 void RenderLayer::paintLayerContents(RenderLayer* rootLayer, GraphicsContext* context, 
2978                         const LayoutRect& parentPaintDirtyRect, PaintBehavior paintBehavior,
2979                         RenderObject* paintingRoot, RenderRegion* region, OverlapTestRequestMap* overlapTestRequests,
2980                         PaintLayerFlags paintFlags)
2981 {
2982     PaintLayerFlags localPaintFlags = paintFlags & ~(PaintLayerAppliedTransform);
2983     bool haveTransparency = localPaintFlags & PaintLayerHaveTransparency;
2984     bool isSelfPaintingLayer = this->isSelfPaintingLayer();
2985     bool isPaintingOverlayScrollbars = paintFlags & PaintLayerPaintingOverlayScrollbars;
2986     // Outline always needs to be painted even if we have no visible content.
2987     bool shouldPaintOutline = isSelfPaintingLayer && !isPaintingOverlayScrollbars;
2988     bool shouldPaintContent = m_hasVisibleContent && isSelfPaintingLayer && !isPaintingOverlayScrollbars;
2989
2990     // Calculate the clip rects we should use only when we need them.
2991     LayoutRect layerBounds;
2992     ClipRect damageRect, clipRectToApply, outlineRect;
2993     LayoutPoint paintOffset;
2994     LayoutRect paintDirtyRect = parentPaintDirtyRect;
2995     
2996     bool useClipRect = true;
2997     GraphicsContext* transparencyLayerContext = context;
2998     
2999     // Ensure our lists are up-to-date.
3000     updateLayerListsIfNeeded();
3001
3002 #if ENABLE(CSS_FILTERS)
3003     FilterEffectRendererHelper filterPainter(filterRenderer() && paintsWithFilters());
3004     if (filterPainter.haveFilterEffect() && !context->paintingDisabled()) {
3005         LayoutPoint rootLayerOffset;
3006         convertToLayerCoords(rootLayer, rootLayerOffset);
3007         RenderLayerFilterInfo* filterInfo = this->filterInfo();
3008         ASSERT(filterInfo);
3009         LayoutRect filterRepaintRect = filterInfo->dirtySourceRect();
3010         filterRepaintRect.move(rootLayerOffset.x(), rootLayerOffset.y());
3011         if (filterPainter.prepareFilterEffect(this, calculateLayerBounds(this, rootLayer, 0), parentPaintDirtyRect, filterRepaintRect)) {
3012             // Now we know for sure, that the source image will be updated, so we can revert our tracking repaint rect back to zero.
3013             filterInfo->resetDirtySourceRect();
3014
3015             // Rewire the old context to a memory buffer, so that we can capture the contents of the layer.
3016             // NOTE: We saved the old context in the "transparencyLayerContext" local variable, to be able to start a transparency layer
3017             // on the original context and avoid duplicating "beginFilterEffect" after each transpareny layer call. Also, note that 
3018             // beginTransparencyLayers will only create a single lazy transparency layer, even though it is called twice in this method.
3019             context = filterPainter.beginFilterEffect(context);
3020
3021             // Check that we didn't fail to allocate the graphics context for the offscreen buffer.
3022             if (filterPainter.hasStartedFilterEffect()) {
3023                 paintDirtyRect = filterPainter.repaintRect();
3024                 // If the filter needs the full source image, we need to avoid using the clip rectangles.
3025                 // Otherwise, if for example this layer has overflow:hidden, a drop shadow will not compute correctly.
3026                 // Note that we will still apply the clipping on the final rendering of the filter.
3027                 useClipRect = !filterRenderer()->hasFilterThatMovesPixels();
3028             }
3029         }
3030     }
3031 #endif
3032     
3033     if (shouldPaintContent || shouldPaintOutline || isPaintingOverlayScrollbars) {
3034         calculateRects(rootLayer, region, paintDirtyRect, layerBounds, damageRect, clipRectToApply, outlineRect, localPaintFlags & PaintLayerTemporaryClipRects);
3035         paintOffset = toPoint(layerBounds.location() - renderBoxLocation());
3036     }
3037
3038     bool forceBlackText = paintBehavior & PaintBehaviorForceBlackText;
3039     bool selectionOnly  = paintBehavior & PaintBehaviorSelectionOnly;
3040     
3041     // If this layer's renderer is a child of the paintingRoot, we render unconditionally, which
3042     // is done by passing a nil paintingRoot down to our renderer (as if no paintingRoot was ever set).
3043     // Else, our renderer tree may or may not contain the painting root, so we pass that root along
3044     // so it will be tested against as we descend through the renderers.
3045     RenderObject* paintingRootForRenderer = 0;
3046     if (paintingRoot && !renderer()->isDescendantOf(paintingRoot))
3047         paintingRootForRenderer = paintingRoot;
3048
3049     if (overlapTestRequests && isSelfPaintingLayer)
3050         performOverlapTests(*overlapTestRequests, rootLayer, this);
3051
3052     // We want to paint our layer, but only if we intersect the damage rect.
3053     shouldPaintContent &= intersectsDamageRect(layerBounds, damageRect.rect(), rootLayer);
3054     
3055     if (localPaintFlags & PaintLayerPaintingCompositingBackgroundPhase) {
3056         if (shouldPaintContent && !selectionOnly) {
3057             // Begin transparency layers lazily now that we know we have to paint something.
3058             if (haveTransparency)
3059                 beginTransparencyLayers(transparencyLayerContext, rootLayer, paintDirtyRect, paintBehavior);
3060         
3061             if (useClipRect) {
3062                 // Paint our background first, before painting any child layers.
3063                 // Establish the clip used to paint our background.
3064                 clipToRect(rootLayer, context, paintDirtyRect, damageRect, DoNotIncludeSelfForBorderRadius); // Background painting will handle clipping to self.
3065             }
3066             
3067             // Paint the background.
3068             PaintInfo paintInfo(context, pixelSnappedIntRect(damageRect.rect()), PaintPhaseBlockBackground, false, paintingRootForRenderer, region, 0);
3069             renderer()->paint(paintInfo, paintOffset);
3070
3071             if (useClipRect) {
3072                 // Restore the clip.
3073                 restoreClip(context, paintDirtyRect, damageRect);
3074             }
3075         }
3076
3077         // Now walk the sorted list of children with negative z-indices.
3078         paintList(m_negZOrderList, rootLayer, context, paintDirtyRect, paintBehavior, paintingRoot, region, overlapTestRequests, localPaintFlags);
3079     }
3080     
3081     if (localPaintFlags & PaintLayerPaintingCompositingForegroundPhase) {
3082         // Now establish the appropriate clip and paint our child RenderObjects.
3083         if (shouldPaintContent && !clipRectToApply.isEmpty()) {
3084             // Begin transparency layers lazily now that we know we have to paint something.
3085             if (haveTransparency)
3086                 beginTransparencyLayers(transparencyLayerContext, rootLayer, paintDirtyRect, paintBehavior);
3087
3088             if (useClipRect) {
3089                 // Set up the clip used when painting our children.
3090                 clipToRect(rootLayer, context, paintDirtyRect, clipRectToApply);
3091             }
3092             
3093             PaintInfo paintInfo(context, pixelSnappedIntRect(clipRectToApply.rect()),
3094                                 selectionOnly ? PaintPhaseSelection : PaintPhaseChildBlockBackgrounds,
3095                                 forceBlackText, paintingRootForRenderer, region, 0);
3096             renderer()->paint(paintInfo, paintOffset);
3097             if (!selectionOnly) {
3098                 paintInfo.phase = PaintPhaseFloat;
3099                 renderer()->paint(paintInfo, paintOffset);
3100                 paintInfo.phase = PaintPhaseForeground;
3101                 paintInfo.overlapTestRequests = overlapTestRequests;
3102                 renderer()->paint(paintInfo, paintOffset);
3103                 paintInfo.phase = PaintPhaseChildOutlines;
3104                 renderer()->paint(paintInfo, paintOffset);
3105             }
3106
3107             if (useClipRect) {
3108                 // Now restore our clip.
3109                 restoreClip(context, paintDirtyRect, clipRectToApply);
3110             }
3111         }
3112
3113         if (shouldPaintOutline && !outlineRect.isEmpty()) {
3114             // Paint our own outline
3115             PaintInfo paintInfo(context, pixelSnappedIntRect(outlineRect.rect()), PaintPhaseSelfOutline, false, paintingRootForRenderer, region, 0);
3116             clipToRect(rootLayer, context, paintDirtyRect, outlineRect, DoNotIncludeSelfForBorderRadius);
3117             renderer()->paint(paintInfo, paintOffset);
3118             restoreClip(context, paintDirtyRect, outlineRect);
3119         }
3120     
3121         // Paint any child layers that have overflow.
3122         paintList(m_normalFlowList, rootLayer, context, paintDirtyRect, paintBehavior, paintingRoot, region, overlapTestRequests, localPaintFlags);
3123     
3124         // Now walk the sorted list of children with positive z-indices.
3125         paintList(m_posZOrderList, rootLayer, context, paintDirtyRect, paintBehavior, paintingRoot, region, overlapTestRequests, localPaintFlags);
3126     }
3127     
3128     if ((localPaintFlags & PaintLayerPaintingCompositingMaskPhase) && shouldPaintContent && renderer()->hasMask() && !selectionOnly) {
3129         if (useClipRect)
3130             clipToRect(rootLayer, context, paintDirtyRect, damageRect, DoNotIncludeSelfForBorderRadius); // Mask painting will handle clipping to self.
3131
3132         // Paint the mask.
3133         PaintInfo paintInfo(context, pixelSnappedIntRect(damageRect.rect()), PaintPhaseMask, false, paintingRootForRenderer, region, 0);
3134         renderer()->paint(paintInfo, paintOffset);
3135         
3136         if (useClipRect) {
3137             // Restore the clip.
3138             restoreClip(context, paintDirtyRect, damageRect);
3139         }
3140     }
3141
3142     if (isPaintingOverlayScrollbars) {
3143         clipToRect(rootLayer, context, paintDirtyRect, damageRect);
3144         paintOverflowControls(context, roundedIntPoint(paintOffset), pixelSnappedIntRect(damageRect.rect()), true);
3145         restoreClip(context, paintDirtyRect, damageRect);
3146     }
3147
3148 #if ENABLE(CSS_FILTERS)
3149     if (filterPainter.hasStartedFilterEffect()) {
3150         // Apply the correct clipping (ie. overflow: hidden).
3151         clipToRect(rootLayer, transparencyLayerContext, paintDirtyRect, damageRect);
3152         context = filterPainter.applyFilterEffect();
3153         restoreClip(transparencyLayerContext, paintDirtyRect, damageRect);
3154     }
3155 #endif
3156
3157     // Make sure that we now use the original transparency context.
3158     ASSERT(transparencyLayerContext == context);
3159     
3160     // End our transparency layer
3161     if (haveTransparency && m_usedTransparency && !m_paintingInsideReflection) {
3162         context->endTransparencyLayer();
3163         context->restore();
3164         m_usedTransparency = false;
3165     }
3166 }
3167
3168 void RenderLayer::paintList(Vector<RenderLayer*>* list, RenderLayer* rootLayer, GraphicsContext* context,
3169                             const LayoutRect& paintDirtyRect, PaintBehavior paintBehavior,
3170                             RenderObject* paintingRoot, RenderRegion* region, OverlapTestRequestMap* overlapTestRequests,
3171                             PaintLayerFlags paintFlags)
3172 {
3173     if (!list)
3174         return;
3175
3176 #if !ASSERT_DISABLED
3177     LayerListMutationDetector mutationChecker(this);
3178 #endif
3179
3180     for (size_t i = 0; i < list->size(); ++i) {
3181         RenderLayer* childLayer = list->at(i);
3182         if (!childLayer->isPaginated())
3183             childLayer->paintLayer(rootLayer, context, paintDirtyRect, paintBehavior, paintingRoot, region, overlapTestRequests, paintFlags);
3184         else
3185             paintPaginatedChildLayer(childLayer, rootLayer, context, paintDirtyRect, paintBehavior, paintingRoot, region, overlapTestRequests, paintFlags);
3186     }
3187 }
3188
3189 void RenderLayer::paintPaginatedChildLayer(RenderLayer* childLayer, RenderLayer* rootLayer, GraphicsContext* context,
3190                                            const LayoutRect& paintDirtyRect, PaintBehavior paintBehavior,
3191                                            RenderObject* paintingRoot, RenderRegion* region, OverlapTestRequestMap* overlapTestRequests,
3192                                            PaintLayerFlags paintFlags)
3193 {
3194     // We need to do multiple passes, breaking up our child layer into strips.
3195     Vector<RenderLayer*> columnLayers;
3196     RenderLayer* ancestorLayer = isNormalFlowOnly() ? parent() : stackingContext();
3197     for (RenderLayer* curr = childLayer->parent(); curr; curr = curr->parent()) {
3198         if (curr->renderer()->hasColumns() && checkContainingBlockChainForPagination(childLayer->renderer(), curr->renderBox()))
3199             columnLayers.append(curr);
3200         if (curr == ancestorLayer)
3201             break;
3202     }
3203
3204     // It is possible for paintLayer() to be called after the child layer ceases to be paginated but before
3205     // updateLayerPositions() is called and resets the isPaginated() flag, see <rdar://problem/10098679>.
3206     // If this is the case, just bail out, since the upcoming call to updateLayerPositions() will repaint the layer.
3207     if (!columnLayers.size())
3208         return;
3209
3210     paintChildLayerIntoColumns(childLayer, rootLayer, context, paintDirtyRect, paintBehavior, paintingRoot, region, overlapTestRequests, paintFlags, columnLayers, columnLayers.size() - 1);
3211 }
3212
3213 void RenderLayer::paintChildLayerIntoColumns(RenderLayer* childLayer, RenderLayer* rootLayer, GraphicsContext* context,
3214                                              const LayoutRect& paintDirtyRect, PaintBehavior paintBehavior,
3215                                              RenderObject* paintingRoot, RenderRegion* region, OverlapTestRequestMap* overlapTestRequests,
3216                                              PaintLayerFlags paintFlags, const Vector<RenderLayer*>& columnLayers, size_t colIndex)
3217 {
3218     RenderBlock* columnBlock = toRenderBlock(columnLayers[colIndex]->renderer());
3219
3220     ASSERT(columnBlock && columnBlock->hasColumns());
3221     if (!columnBlock || !columnBlock->hasColumns())
3222         return;
3223     
3224     LayoutPoint layerOffset;
3225     // FIXME: It looks suspicious to call convertToLayerCoords here
3226     // as canUseConvertToLayerCoords is true for this layer.
3227     columnBlock->layer()->convertToLayerCoords(rootLayer, layerOffset);
3228     
3229     bool isHorizontal = columnBlock->style()->isHorizontalWritingMode();
3230
3231     ColumnInfo* colInfo = columnBlock->columnInfo();
3232     unsigned colCount = columnBlock->columnCount(colInfo);
3233     int currLogicalTopOffset = 0;
3234     for (unsigned i = 0; i < colCount; i++) {
3235         // For each rect, we clip to the rect, and then we adjust our coords.
3236         LayoutRect colRect = columnBlock->columnRectAt(colInfo, i);
3237         columnBlock->flipForWritingMode(colRect);
3238         int logicalLeftOffset = (isHorizontal ? colRect.x() : colRect.y()) - columnBlock->logicalLeftOffsetForContent();
3239         LayoutSize offset;
3240         if (isHorizontal) {
3241             if (colInfo->progressionAxis() == ColumnInfo::InlineAxis)
3242                 offset = LayoutSize(logicalLeftOffset, currLogicalTopOffset);
3243             else
3244                 offset = LayoutSize(0, colRect.y() + currLogicalTopOffset - columnBlock->borderTop() - columnBlock->paddingTop());
3245         } else {
3246             if (colInfo->progressionAxis() == ColumnInfo::InlineAxis)
3247                 offset = LayoutSize(currLogicalTopOffset, logicalLeftOffset);
3248             else
3249                 offset = LayoutSize(colRect.x() + currLogicalTopOffset - columnBlock->borderLeft() - columnBlock->paddingLeft(), 0);
3250         }
3251
3252         colRect.moveBy(layerOffset);
3253
3254         LayoutRect localDirtyRect(paintDirtyRect);
3255         localDirtyRect.intersect(colRect);
3256         
3257         if (!localDirtyRect.isEmpty()) {
3258             GraphicsContextStateSaver stateSaver(*context);
3259             
3260             // Each strip pushes a clip, since column boxes are specified as being
3261             // like overflow:hidden.
3262             context->clip(colRect);
3263
3264             if (!colIndex) {
3265                 // Apply a translation transform to change where the layer paints.
3266                 TransformationMatrix oldTransform;
3267                 bool oldHasTransform = childLayer->transform();
3268                 if (oldHasTransform)
3269                     oldTransform = *childLayer->transform();
3270                 TransformationMatrix newTransform(oldTransform);
3271                 newTransform.translateRight(offset.width(), offset.height());
3272                 
3273                 childLayer->m_transform = adoptPtr(new TransformationMatrix(newTransform));
3274                 childLayer->paintLayer(rootLayer, context, localDirtyRect, paintBehavior, paintingRoot, region, overlapTestRequests, paintFlags);
3275                 if (oldHasTransform)
3276                     childLayer->m_transform = adoptPtr(new TransformationMatrix(oldTransform));
3277                 else
3278                     childLayer->m_transform.clear();
3279             } else {
3280                 // Adjust the transform such that the renderer's upper left corner will paint at (0,0) in user space.
3281                 // This involves subtracting out the position of the layer in our current coordinate space.
3282                 LayoutPoint childOffset;
3283                 columnLayers[colIndex - 1]->convertToLayerCoords(rootLayer, childOffset);
3284                 TransformationMatrix transform;
3285                 transform.translateRight(childOffset.x() + offset.width(), childOffset.y() + offset.height());
3286                 
3287                 // Apply the transform.
3288                 context->concatCTM(transform.toAffineTransform());
3289
3290                 // Now do a paint with the root layer shifted to be the next multicol block.
3291                 paintChildLayerIntoColumns(childLayer, columnLayers[colIndex - 1], context, transform.inverse().mapRect(localDirtyRect), paintBehavior, 
3292                                            paintingRoot, region, overlapTestRequests, paintFlags, 
3293                                            columnLayers, colIndex - 1);
3294             }
3295         }
3296
3297         // Move to the next position.
3298         int blockDelta = isHorizontal ? colRect.height() : colRect.width();
3299         if (columnBlock->style()->isFlippedBlocksWritingMode())
3300             currLogicalTopOffset += blockDelta;
3301         else
3302             currLogicalTopOffset -= blockDelta;
3303     }
3304 }
3305
3306 static inline LayoutRect frameVisibleRect(RenderObject* renderer)
3307 {
3308     FrameView* frameView = renderer->document()->view();
3309     if (!frameView)
3310         return LayoutRect();
3311
3312     return frameView->visibleContentRect();
3313 }
3314
3315 bool RenderLayer::hitTest(const HitTestRequest& request, HitTestResult& result)
3316 {
3317     renderer()->document()->updateLayout();
3318     
3319     LayoutRect hitTestArea = renderer()->isRenderFlowThread() ? toRenderFlowThread(renderer())->borderBoxRect() : renderer()->view()->documentRect();
3320     if (!request.ignoreClipping())
3321         hitTestArea.intersect(frameVisibleRect(renderer()));
3322
3323     RenderLayer* insideLayer = hitTestLayer(this, 0, request, result, hitTestArea, result.point(), false);
3324     if (!insideLayer) {
3325         // We didn't hit any layer. If we are the root layer and the mouse is -- or just was -- down, 
3326         // return ourselves. We do this so mouse events continue getting delivered after a drag has 
3327         // exited the WebView, and so hit testing over a scrollbar hits the content document.
3328         if ((request.active() || request.release()) && renderer()->isRenderView()) {
3329             renderer()->updateHitTestResult(result, result.point());
3330             insideLayer = this;
3331         }
3332     }
3333
3334     // Now determine if the result is inside an anchor - if the urlElement isn't already set.
3335     Node* node = result.innerNode();
3336     if (node && !result.URLElement())
3337         result.setURLElement(static_cast<Element*>(node->enclosingLinkEventParentOrSelf()));
3338
3339     // Next set up the correct :hover/:active state along the new chain.
3340     updateHoverActiveState(request, result);
3341     
3342     // Now return whether we were inside this layer (this will always be true for the root
3343     // layer).
3344     return insideLayer;
3345 }
3346
3347 Node* RenderLayer::enclosingElement() const
3348 {
3349     for (RenderObject* r = renderer(); r; r = r->parent()) {
3350         if (Node* e = r->node())
3351             return e;
3352     }
3353     ASSERT_NOT_REACHED();
3354     return 0;
3355 }
3356
3357 // Compute the z-offset of the point in the transformState.
3358 // This is effectively projecting a ray normal to the plane of ancestor, finding where that
3359 // ray intersects target, and computing the z delta between those two points.
3360 static double computeZOffset(const HitTestingTransformState& transformState)
3361 {
3362     // We got an affine transform, so no z-offset
3363     if (transformState.m_accumulatedTransform.isAffine())
3364         return 0;
3365
3366     // Flatten the point into the target plane
3367     FloatPoint targetPoint = transformState.mappedPoint();
3368     
3369     // Now map the point back through the transform, which computes Z.
3370     FloatPoint3D backmappedPoint = transformState.m_accumulatedTransform.mapPoint(FloatPoint3D(targetPoint));
3371     return backmappedPoint.z();
3372 }
3373
3374 PassRefPtr<HitTestingTransformState> RenderLayer::createLocalTransformState(RenderLayer* rootLayer, RenderLayer* containerLayer,
3375                                         const LayoutRect& hitTestRect, const LayoutPoint& hitTestPoint,
3376                                         const HitTestingTransformState* containerTransformState) const
3377 {
3378     RefPtr<HitTestingTransformState> transformState;
3379     LayoutPoint offset;
3380     if (containerTransformState) {
3381         // If we're already computing transform state, then it's relative to the container (which we know is non-null).
3382         transformState = HitTestingTransformState::create(*containerTransformState);
3383         convertToLayerCoords(containerLayer, offset);
3384     } else {
3385         // If this is the first time we need to make transform state, then base it off of hitTestPoint,
3386         // which is relative to rootLayer.
3387         transformState = HitTestingTransformState::create(hitTestPoint, FloatQuad(hitTestRect));
3388         convertToLayerCoords(rootLayer, offset);
3389     }
3390     
3391     RenderObject* containerRenderer = containerLayer ? containerLayer->renderer() : 0;
3392     if (renderer()->shouldUseTransformFromContainer(containerRenderer)) {
3393         TransformationMatrix containerTransform;
3394         renderer()->getTransformFromContainer(containerRenderer, toLayoutSize(offset), containerTransform);
3395         transformState->applyTransform(containerTransform, HitTestingTransformState::AccumulateTransform);
3396     } else {
3397         transformState->translate(offset.x(), offset.y(), HitTestingTransformState::AccumulateTransform);
3398     }
3399     
3400     return transformState;
3401 }
3402
3403
3404 static bool isHitCandidate(const RenderLayer* hitLayer, bool canDepthSort, double* zOffset, const HitTestingTransformState* transformState)
3405 {
3406     if (!hitLayer)
3407         return false;
3408
3409     // The hit layer is depth-sorting with other layers, so just say that it was hit.
3410     if (canDepthSort)
3411         return true;
3412     
3413     // We need to look at z-depth to decide if this layer was hit.
3414     if (zOffset) {
3415         ASSERT(transformState);
3416         // This is actually computing our z, but that's OK because the hitLayer is coplanar with us.
3417         double childZOffset = computeZOffset(*transformState);
3418         if (childZOffset > *zOffset) {
3419             *zOffset = childZOffset;
3420             return true;
3421         }
3422         return false;
3423     }
3424
3425     return true;
3426 }