2010-09-08 Peter Kasting <pkasting@google.com>
[WebKit-https.git] / WebCore / rendering / RenderLayer.cpp
1 /*
2  * Copyright (C) 2006, 2007, 2008, 2009, 2010 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 "CSSStyleDeclaration.h"
50 #include "CSSStyleSelector.h"
51 #include "Chrome.h"
52 #include "Document.h"
53 #include "EventHandler.h"
54 #include "EventNames.h"
55 #include "FloatPoint3D.h"
56 #include "FloatRect.h"
57 #include "FocusController.h"
58 #include "Frame.h"
59 #include "FrameTree.h"
60 #include "FrameView.h"
61 #include "Gradient.h"
62 #include "GraphicsContext.h"
63 #include "HTMLFrameOwnerElement.h"
64 #include "HTMLNames.h"
65 #include "HitTestRequest.h"
66 #include "HitTestResult.h"
67 #include "OverflowEvent.h"
68 #include "OverlapTestRequestClient.h"
69 #include "Page.h"
70 #include "PlatformMouseEvent.h"
71 #include "RenderArena.h"
72 #include "RenderInline.h"
73 #include "RenderMarquee.h"
74 #include "RenderReplica.h"
75 #include "RenderScrollbar.h"
76 #include "RenderScrollbarPart.h"
77 #include "RenderTheme.h"
78 #include "RenderTreeAsText.h"
79 #include "RenderView.h"
80 #include "ScaleTransformOperation.h"
81 #include "Scrollbar.h"
82 #include "ScrollbarTheme.h"
83 #include "SelectionController.h"
84 #include "TextStream.h"
85 #include "TransformState.h"
86 #include "TransformationMatrix.h"
87 #include "TranslateTransformOperation.h"
88 #include <wtf/StdLibExtras.h>
89 #include <wtf/UnusedParam.h>
90 #include <wtf/text/CString.h>
91
92 #if USE(ACCELERATED_COMPOSITING)
93 #include "RenderLayerBacking.h"
94 #include "RenderLayerCompositor.h"
95 #endif
96
97 #if ENABLE(SVG)
98 #include "SVGNames.h"
99 #endif
100
101 #define MIN_INTERSECT_FOR_REVEAL 32
102
103 using namespace std;
104
105 namespace WebCore {
106
107 using namespace HTMLNames;
108
109 const int MinimumWidthWhileResizing = 100;
110 const int MinimumHeightWhileResizing = 40;
111
112 void* ClipRects::operator new(size_t sz, RenderArena* renderArena) throw()
113 {
114     return renderArena->allocate(sz);
115 }
116
117 void ClipRects::operator delete(void* ptr, size_t sz)
118 {
119     // Stash size where destroy can find it.
120     *(size_t *)ptr = sz;
121 }
122
123 void ClipRects::destroy(RenderArena* renderArena)
124 {
125     delete this;
126     
127     // Recover the size left there for us by operator delete and free the memory.
128     renderArena->free(*(size_t *)this, this);
129 }
130
131 RenderLayer::RenderLayer(RenderBoxModelObject* renderer)
132     : m_renderer(renderer)
133     , m_parent(0)
134     , m_previous(0)
135     , m_next(0)
136     , m_first(0)
137     , m_last(0)
138     , m_relX(0)
139     , m_relY(0)
140     , m_x(0)
141     , m_y(0)
142     , m_width(0)
143     , m_height(0)
144     , m_scrollX(0)
145     , m_scrollY(0)
146     , m_scrollOriginX(0)
147     , m_scrollLeftOverflow(0)
148     , m_scrollWidth(0)
149     , m_scrollHeight(0)
150     , m_inResizeMode(false)
151     , m_posZOrderList(0)
152     , m_negZOrderList(0)
153     , m_normalFlowList(0)
154     , m_clipRects(0) 
155 #ifndef NDEBUG    
156     , m_clipRectsRoot(0)
157 #endif
158     , m_scrollDimensionsDirty(true)
159     , m_zOrderListsDirty(true)
160     , m_normalFlowListDirty(true)
161     , m_isNormalFlowOnly(shouldBeNormalFlowOnly())
162     , m_usedTransparency(false)
163     , m_paintingInsideReflection(false)
164     , m_inOverflowRelayout(false)
165     , m_needsFullRepaint(false)
166     , m_overflowStatusDirty(true)
167     , m_visibleContentStatusDirty(true)
168     , m_hasVisibleContent(false)
169     , m_visibleDescendantStatusDirty(false)
170     , m_hasVisibleDescendant(false)
171     , m_isPaginated(false)
172     , m_3DTransformedDescendantStatusDirty(true)
173     , m_has3DTransformedDescendant(false)
174 #if USE(ACCELERATED_COMPOSITING)
175     , m_hasCompositingDescendant(false)
176     , m_mustOverlapCompositedLayers(false)
177 #endif
178     , m_marquee(0)
179     , m_staticX(0)
180     , m_staticY(0)
181     , m_reflection(0)
182     , m_scrollCorner(0)
183     , m_resizer(0)
184 {
185     if (!renderer->firstChild() && renderer->style()) {
186         m_visibleContentStatusDirty = false;
187         m_hasVisibleContent = renderer->style()->visibility() == VISIBLE;
188     }
189 }
190
191 RenderLayer::~RenderLayer()
192 {
193     if (inResizeMode() && !renderer()->documentBeingDestroyed()) {
194         if (Frame* frame = renderer()->frame())
195             frame->eventHandler()->resizeLayerDestroyed();
196     }
197
198     destroyScrollbar(HorizontalScrollbar);
199     destroyScrollbar(VerticalScrollbar);
200
201     // Child layers will be deleted by their corresponding render objects, so
202     // we don't need to delete them ourselves.
203
204     delete m_posZOrderList;
205     delete m_negZOrderList;
206     delete m_normalFlowList;
207     delete m_marquee;
208
209 #if USE(ACCELERATED_COMPOSITING)
210     clearBacking();
211 #endif
212     
213     // Make sure we have no lingering clip rects.
214     ASSERT(!m_clipRects);
215     
216     if (m_reflection)
217         removeReflection();
218     
219     if (m_scrollCorner)
220         m_scrollCorner->destroy();
221     if (m_resizer)
222         m_resizer->destroy();
223 }
224
225 #if USE(ACCELERATED_COMPOSITING)
226 RenderLayerCompositor* RenderLayer::compositor() const
227 {
228     ASSERT(renderer()->view());
229     return renderer()->view()->compositor();
230 }
231
232 void RenderLayer::rendererContentChanged()
233 {
234     // This can get called when video becomes accelerated, so the layers may change.
235     if (compositor()->updateLayerCompositingState(this))
236         compositor()->setCompositingLayersNeedRebuild();
237
238     if (m_backing)
239         m_backing->rendererContentChanged();
240 }
241 #endif // USE(ACCELERATED_COMPOSITING)
242
243 bool RenderLayer::hasAcceleratedCompositing() const
244 {
245 #if USE(ACCELERATED_COMPOSITING)
246     return compositor()->hasAcceleratedCompositing();
247 #else
248     return false;
249 #endif
250 }
251
252 void RenderLayer::updateLayerPositions(UpdateLayerPositionsFlags flags, IntPoint* cachedOffset)
253 {
254     if (flags & DoFullRepaint) {
255         renderer()->repaint();
256 #if USE(ACCELERATED_COMPOSITING)
257         flags &= ~CheckForRepaint;
258         // We need the full repaint to propagate to child layers if we are hardware compositing.
259         if (!compositor()->inCompositingMode())
260             flags &= ~DoFullRepaint;
261 #else
262         flags &= ~(CheckForRepaint | DoFullRepaint);
263 #endif
264     }
265     
266
267     updateLayerPosition(); // For relpositioned layers or non-positioned layers,
268                            // we need to keep in sync, since we may have shifted relative
269                            // to our parent layer.
270     IntPoint oldCachedOffset;
271     if (cachedOffset) {
272         // We can't cache our offset to the repaint container if the mapping is anything more complex than a simple translation
273         bool disableOffsetCache = renderer()->hasColumns() || renderer()->hasTransform() || isComposited();
274 #if ENABLE(SVG)
275         disableOffsetCache = disableOffsetCache || renderer()->isSVGRoot();
276 #endif
277         if (disableOffsetCache)
278             cachedOffset = 0; // If our cached offset is invalid make sure it's not passed to any of our children
279         else {
280             oldCachedOffset = *cachedOffset;
281             // Frequently our parent layer's renderer will be the same as our renderer's containing block.  In that case,
282             // we just update the cache using our offset to our parent (which is m_x / m_y).  Otherwise, regenerated cached
283             // offsets to the root from the render tree.
284             if (!m_parent || m_parent->renderer() == renderer()->containingBlock())
285                 cachedOffset->move(m_x, m_y); // Fast case
286             else {
287                 int x = 0;
288                 int y = 0;
289                 convertToLayerCoords(root(), x, y);
290                 *cachedOffset = IntPoint(x, y);
291             }
292         }
293     }
294
295     int x = 0;
296     int y = 0;
297     if (cachedOffset) {
298         x += cachedOffset->x();
299         y += cachedOffset->y();
300 #ifndef NDEBUG
301         int nonCachedX = 0;
302         int nonCachedY = 0;
303         convertToLayerCoords(root(), nonCachedX, nonCachedY);
304         ASSERT(x == nonCachedX);
305         ASSERT(y == nonCachedY);
306 #endif
307     } else
308         convertToLayerCoords(root(), x, y);
309     positionOverflowControls(x, y);
310
311     updateVisibilityStatus();
312
313     updateTransform();
314
315     if (flags & UpdatePagination)
316         updatePagination();
317     else
318         m_isPaginated = false;
319
320     if (m_hasVisibleContent) {
321         RenderView* view = renderer()->view();
322         ASSERT(view);
323         // FIXME: Optimize using LayoutState and remove the disableLayoutState() call
324         // from updateScrollInfoAfterLayout().
325         ASSERT(!view->layoutStateEnabled());
326
327         RenderBoxModelObject* repaintContainer = renderer()->containerForRepaint();
328         IntRect newRect = renderer()->clippedOverflowRectForRepaint(repaintContainer);
329         IntRect newOutlineBox = renderer()->outlineBoundsForRepaint(repaintContainer, cachedOffset);
330         // FIXME: Should ASSERT that value calculated for newOutlineBox using the cached offset is the same
331         // as the value not using the cached offset, but we can't due to https://bugs.webkit.org/show_bug.cgi?id=37048
332         if (flags & CheckForRepaint) {
333             if (view && !view->printing()) {
334                 if (m_needsFullRepaint) {
335                     renderer()->repaintUsingContainer(repaintContainer, m_repaintRect);
336                     if (newRect != m_repaintRect)
337                         renderer()->repaintUsingContainer(repaintContainer, newRect);
338                 } else
339                     renderer()->repaintAfterLayoutIfNeeded(repaintContainer, m_repaintRect, m_outlineBox, &newRect, &newOutlineBox);
340             }
341         }
342         m_repaintRect = newRect;
343         m_outlineBox = newOutlineBox;
344     } else {
345         m_repaintRect = IntRect();
346         m_outlineBox = IntRect();
347     }
348
349     m_needsFullRepaint = false;
350
351     // Go ahead and update the reflection's position and size.
352     if (m_reflection)
353         m_reflection->layout();
354
355 #if USE(ACCELERATED_COMPOSITING)
356     // Clear the IsCompositingUpdateRoot flag once we've found the first compositing layer in this update.
357     bool isUpdateRoot = (flags & IsCompositingUpdateRoot);
358     if (isComposited())
359         flags &= ~IsCompositingUpdateRoot;
360 #endif
361
362     if (renderer()->hasColumns())
363         flags |= UpdatePagination;
364
365     for (RenderLayer* child = firstChild(); child; child = child->nextSibling())
366         child->updateLayerPositions(flags, cachedOffset);
367
368 #if USE(ACCELERATED_COMPOSITING)
369     if ((flags & UpdateCompositingLayers) && isComposited())
370         backing()->updateAfterLayout(RenderLayerBacking::CompositingChildren, isUpdateRoot);
371 #endif
372         
373     // With all our children positioned, now update our marquee if we need to.
374     if (m_marquee)
375         m_marquee->updateMarqueePosition();
376
377     if (cachedOffset)
378         *cachedOffset = oldCachedOffset;
379 }
380
381 IntRect RenderLayer::repaintRectIncludingDescendants() const
382 {
383     IntRect repaintRect = m_repaintRect;
384     for (RenderLayer* child = firstChild(); child; child = child->nextSibling())
385         repaintRect.unite(child->repaintRectIncludingDescendants());
386     return repaintRect;
387 }
388
389 void RenderLayer::computeRepaintRects()
390 {
391     RenderBoxModelObject* repaintContainer = renderer()->containerForRepaint();
392     m_repaintRect = renderer()->clippedOverflowRectForRepaint(repaintContainer);
393     m_outlineBox = renderer()->outlineBoundsForRepaint(repaintContainer);
394 }
395
396 void RenderLayer::updateRepaintRectsAfterScroll(bool fixed)
397 {
398     if (fixed || renderer()->style()->position() == FixedPosition) {
399         computeRepaintRects();
400         fixed = true;
401     } else if (renderer()->hasTransform()) {
402         // Transforms act as fixed position containers, so nothing inside a
403         // transformed element can be fixed relative to the viewport if the
404         // transformed element is not fixed itself or child of a fixed element.
405         return;
406     }
407
408     for (RenderLayer* child = firstChild(); child; child = child->nextSibling())
409         child->updateRepaintRectsAfterScroll(fixed);
410 }
411
412 void RenderLayer::updateTransform()
413 {
414     // hasTransform() on the renderer is also true when there is transform-style: preserve-3d or perspective set,
415     // so check style too.
416     bool hasTransform = renderer()->hasTransform() && renderer()->style()->hasTransform();
417     bool had3DTransform = has3DTransform();
418
419     bool hadTransform = m_transform;
420     if (hasTransform != hadTransform) {
421         if (hasTransform)
422             m_transform.set(new TransformationMatrix);
423         else
424             m_transform.clear();
425     }
426     
427     if (hasTransform) {
428         RenderBox* box = renderBox();
429         ASSERT(box);
430         m_transform->makeIdentity();
431         box->style()->applyTransform(*m_transform, box->borderBoxRect().size(), RenderStyle::IncludeTransformOrigin);
432         makeMatrixRenderable(*m_transform, hasAcceleratedCompositing());
433     }
434
435     if (had3DTransform != has3DTransform())
436         dirty3DTransformedDescendantStatus();
437 }
438
439 TransformationMatrix RenderLayer::currentTransform() const
440 {
441     if (!m_transform)
442         return TransformationMatrix();
443
444 #if USE(ACCELERATED_COMPOSITING)
445     if (renderer()->style()->isRunningAcceleratedAnimation()) {
446         TransformationMatrix currTransform;
447         RefPtr<RenderStyle> style = renderer()->animation()->getAnimatedStyleForRenderer(renderer());
448         style->applyTransform(currTransform, renderBox()->borderBoxRect().size(), RenderStyle::IncludeTransformOrigin);
449         makeMatrixRenderable(currTransform, hasAcceleratedCompositing());
450         return currTransform;
451     }
452 #endif
453
454     return *m_transform;
455 }
456
457 TransformationMatrix RenderLayer::renderableTransform(PaintBehavior paintBehavior) const
458 {
459     if (!m_transform)
460         return TransformationMatrix();
461     
462     if (paintBehavior & PaintBehaviorFlattenCompositingLayers) {
463         TransformationMatrix matrix = *m_transform;
464         makeMatrixRenderable(matrix, false /* flatten 3d */);
465         return matrix;
466     }
467
468     return *m_transform;
469 }
470
471 void RenderLayer::updatePagination()
472 {
473     m_isPaginated = false;
474     if (isComposited() || !parent() || renderer()->isPositioned())
475         return; // FIXME: We will have to deal with paginated compositing layers someday.
476                 // FIXME: For now the RenderView can't be paginated.  Eventually printing will move to a model where it is though.
477     
478     if (isNormalFlowOnly()) {
479         m_isPaginated = parent()->renderer()->hasColumns();
480         return;
481     }
482
483     // If we're not normal flow, then we need to look for a multi-column object between us and our stacking context.
484     RenderLayer* ancestorStackingContext = stackingContext();
485     for (RenderLayer* curr = parent(); curr; curr = curr->parent()) {
486         if (curr->renderer()->hasColumns()) {
487             m_isPaginated = true;
488             return;
489         }
490         if (curr == ancestorStackingContext || (curr->parent() && curr->parent()->renderer()->isPositioned()))
491             return;
492     }
493 }
494
495 void RenderLayer::setHasVisibleContent(bool b)
496
497     if (m_hasVisibleContent == b && !m_visibleContentStatusDirty)
498         return;
499     m_visibleContentStatusDirty = false; 
500     m_hasVisibleContent = b;
501     if (m_hasVisibleContent) {
502         RenderBoxModelObject* repaintContainer = renderer()->containerForRepaint();
503         m_repaintRect = renderer()->clippedOverflowRectForRepaint(repaintContainer);
504         m_outlineBox = renderer()->outlineBoundsForRepaint(repaintContainer);
505         if (!isNormalFlowOnly())
506             dirtyStackingContextZOrderLists();
507     }
508     if (parent())
509         parent()->childVisibilityChanged(m_hasVisibleContent);
510 }
511
512 void RenderLayer::dirtyVisibleContentStatus() 
513
514     m_visibleContentStatusDirty = true; 
515     if (parent())
516         parent()->dirtyVisibleDescendantStatus();
517 }
518
519 void RenderLayer::childVisibilityChanged(bool newVisibility) 
520
521     if (m_hasVisibleDescendant == newVisibility || m_visibleDescendantStatusDirty)
522         return;
523     if (newVisibility) {
524         RenderLayer* l = this;
525         while (l && !l->m_visibleDescendantStatusDirty && !l->m_hasVisibleDescendant) {
526             l->m_hasVisibleDescendant = true;
527             l = l->parent();
528         }
529     } else 
530         dirtyVisibleDescendantStatus();
531 }
532
533 void RenderLayer::dirtyVisibleDescendantStatus()
534 {
535     RenderLayer* l = this;
536     while (l && !l->m_visibleDescendantStatusDirty) {
537         l->m_visibleDescendantStatusDirty = true;
538         l = l->parent();
539     }
540 }
541
542 void RenderLayer::updateVisibilityStatus()
543 {
544     if (m_visibleDescendantStatusDirty) {
545         m_hasVisibleDescendant = false;
546         for (RenderLayer* child = firstChild(); child; child = child->nextSibling()) {
547             child->updateVisibilityStatus();        
548             if (child->m_hasVisibleContent || child->m_hasVisibleDescendant) {
549                 m_hasVisibleDescendant = true;
550                 break;
551             }
552         }
553         m_visibleDescendantStatusDirty = false;
554     }
555
556     if (m_visibleContentStatusDirty) {
557         if (renderer()->style()->visibility() == VISIBLE)
558             m_hasVisibleContent = true;
559         else {
560             // layer may be hidden but still have some visible content, check for this
561             m_hasVisibleContent = false;
562             RenderObject* r = renderer()->firstChild();
563             while (r) {
564                 if (r->style()->visibility() == VISIBLE && !r->hasLayer()) {
565                     m_hasVisibleContent = true;
566                     break;
567                 }
568                 if (r->firstChild() && !r->hasLayer())
569                     r = r->firstChild();
570                 else if (r->nextSibling())
571                     r = r->nextSibling();
572                 else {
573                     do {
574                         r = r->parent();
575                         if (r == renderer())
576                             r = 0;
577                     } while (r && !r->nextSibling());
578                     if (r)
579                         r = r->nextSibling();
580                 }
581             }
582         }    
583         m_visibleContentStatusDirty = false; 
584     }
585 }
586
587 void RenderLayer::dirty3DTransformedDescendantStatus()
588 {
589     RenderLayer* curr = stackingContext();
590     if (curr)
591         curr->m_3DTransformedDescendantStatusDirty = true;
592         
593     // This propagates up through preserve-3d hierarchies to the enclosing flattening layer.
594     // Note that preserves3D() creates stacking context, so we can just run up the stacking contexts.
595     while (curr && curr->preserves3D()) {
596         curr->m_3DTransformedDescendantStatusDirty = true;
597         curr = curr->stackingContext();
598     }
599 }
600
601 // Return true if this layer or any preserve-3d descendants have 3d.
602 bool RenderLayer::update3DTransformedDescendantStatus()
603 {
604     if (m_3DTransformedDescendantStatusDirty) {
605         m_has3DTransformedDescendant = false;
606
607         // Transformed or preserve-3d descendants can only be in the z-order lists, not
608         // in the normal flow list, so we only need to check those.
609         if (m_posZOrderList) {
610             for (unsigned i = 0; i < m_posZOrderList->size(); ++i)
611                 m_has3DTransformedDescendant |= m_posZOrderList->at(i)->update3DTransformedDescendantStatus();
612         }
613
614         // Now check our negative z-index children.
615         if (m_negZOrderList) {
616             for (unsigned i = 0; i < m_negZOrderList->size(); ++i)
617                 m_has3DTransformedDescendant |= m_negZOrderList->at(i)->update3DTransformedDescendantStatus();
618         }
619         
620         m_3DTransformedDescendantStatusDirty = false;
621     }
622     
623     // If we live in a 3d hierarchy, then the layer at the root of that hierarchy needs
624     // the m_has3DTransformedDescendant set.
625     if (preserves3D())
626         return has3DTransform() || m_has3DTransformedDescendant;
627
628     return has3DTransform();
629 }
630
631 void RenderLayer::updateLayerPosition()
632 {
633     IntPoint localPoint;
634     IntSize inlineBoundingBoxOffset; // We don't put this into the RenderLayer x/y for inlines, so we need to subtract it out when done.
635     if (renderer()->isRenderInline()) {
636         RenderInline* inlineFlow = toRenderInline(renderer());
637         IntRect lineBox = inlineFlow->linesBoundingBox();
638         setWidth(lineBox.width());
639         setHeight(lineBox.height());
640         inlineBoundingBoxOffset = IntSize(lineBox.x(), lineBox.y());
641         localPoint += inlineBoundingBoxOffset;
642     } else if (RenderBox* box = renderBox()) {
643         setWidth(box->width());
644         setHeight(box->height());
645
646         if (!box->hasOverflowClip()) {
647             if (box->rightLayoutOverflow() > box->width())
648                 setWidth(box->rightLayoutOverflow());
649             if (box->bottomLayoutOverflow() > box->height())
650                 setHeight(box->bottomLayoutOverflow());
651         }
652         
653         localPoint += box->locationOffset();
654     }
655
656     // Clear our cached clip rect information.
657     clearClipRects();
658  
659     if (!renderer()->isPositioned() && renderer()->parent()) {
660         // We must adjust our position by walking up the render tree looking for the
661         // nearest enclosing object with a layer.
662         RenderObject* curr = renderer()->parent();
663         while (curr && !curr->hasLayer()) {
664             if (curr->isBox() && !curr->isTableRow()) {
665                 // Rows and cells share the same coordinate space (that of the section).
666                 // Omit them when computing our xpos/ypos.
667                 localPoint += toRenderBox(curr)->locationOffset();
668             }
669             curr = curr->parent();
670         }
671         if (curr->isBox() && curr->isTableRow()) {
672             // Put ourselves into the row coordinate space.
673             localPoint -= toRenderBox(curr)->locationOffset();
674         }
675     }
676     
677     // Subtract our parent's scroll offset.
678     if (renderer()->isPositioned() && enclosingPositionedAncestor()) {
679         RenderLayer* positionedParent = enclosingPositionedAncestor();
680
681         // For positioned layers, we subtract out the enclosing positioned layer's scroll offset.
682         IntSize offset = positionedParent->scrolledContentOffset();
683         localPoint -= offset;
684         
685         if (renderer()->isPositioned() && positionedParent->renderer()->isRelPositioned() && positionedParent->renderer()->isRenderInline()) {
686             IntSize offset = toRenderInline(positionedParent->renderer())->relativePositionedInlineOffset(toRenderBox(renderer()));
687             localPoint += offset;
688         }
689     } else if (parent()) {
690         if (isComposited()) {
691             // FIXME: Composited layers ignore pagination, so about the best we can do is make sure they're offset into the appropriate column.
692             // They won't split across columns properly.
693             IntSize columnOffset;
694             parent()->renderer()->adjustForColumns(columnOffset, localPoint);
695             localPoint += columnOffset;
696         }
697
698         IntSize scrollOffset = parent()->scrolledContentOffset();
699         localPoint -= scrollOffset;
700     }
701         
702     m_relX = m_relY = 0;
703     if (renderer()->isRelPositioned()) {
704         m_relX = renderer()->relativePositionOffsetX();
705         m_relY = renderer()->relativePositionOffsetY();
706         localPoint.move(m_relX, m_relY);
707     }
708
709     // FIXME: We'd really like to just get rid of the concept of a layer rectangle and rely on the renderers.
710     localPoint -= inlineBoundingBoxOffset;
711     setLocation(localPoint.x(), localPoint.y());
712 }
713
714 TransformationMatrix RenderLayer::perspectiveTransform() const
715 {
716     if (!renderer()->hasTransform())
717         return TransformationMatrix();
718
719     RenderStyle* style = renderer()->style();
720     if (!style->hasPerspective())
721         return TransformationMatrix();
722
723     // Maybe fetch the perspective from the backing?
724     const IntRect borderBox = toRenderBox(renderer())->borderBoxRect();
725     const float boxWidth = borderBox.width();
726     const float boxHeight = borderBox.height();
727
728     float perspectiveOriginX = style->perspectiveOriginX().calcFloatValue(boxWidth);
729     float perspectiveOriginY = style->perspectiveOriginY().calcFloatValue(boxHeight);
730
731     // A perspective origin of 0,0 makes the vanishing point in the center of the element.
732     // We want it to be in the top-left, so subtract half the height and width.
733     perspectiveOriginX -= boxWidth / 2.0f;
734     perspectiveOriginY -= boxHeight / 2.0f;
735     
736     TransformationMatrix t;
737     t.translate(perspectiveOriginX, perspectiveOriginY);
738     t.applyPerspective(style->perspective());
739     t.translate(-perspectiveOriginX, -perspectiveOriginY);
740     
741     return t;
742 }
743
744 FloatPoint RenderLayer::perspectiveOrigin() const
745 {
746     if (!renderer()->hasTransform())
747         return FloatPoint();
748
749     const IntRect borderBox = toRenderBox(renderer())->borderBoxRect();
750     RenderStyle* style = renderer()->style();
751
752     return FloatPoint(style->perspectiveOriginX().calcFloatValue(borderBox.width()),
753                       style->perspectiveOriginY().calcFloatValue(borderBox.height()));
754 }
755
756 RenderLayer* RenderLayer::stackingContext() const
757 {
758     RenderLayer* layer = parent();
759     while (layer && !layer->renderer()->isRenderView() && !layer->renderer()->isRoot() && layer->renderer()->style()->hasAutoZIndex())
760         layer = layer->parent();
761     return layer;
762 }
763
764 static inline bool isPositionedContainer(RenderLayer* layer)
765 {
766     RenderObject* o = layer->renderer();
767     return o->isRenderView() || o->isPositioned() || o->isRelPositioned() || layer->hasTransform();
768 }
769
770 static inline bool isFixedPositionedContainer(RenderLayer* layer)
771 {
772     RenderObject* o = layer->renderer();
773     return o->isRenderView() || layer->hasTransform();
774 }
775
776 RenderLayer* RenderLayer::enclosingPositionedAncestor() const
777 {
778     RenderLayer* curr = parent();
779     while (curr && !isPositionedContainer(curr))
780         curr = curr->parent();
781
782     return curr;
783 }
784
785 RenderLayer* RenderLayer::enclosingTransformedAncestor() const
786 {
787     RenderLayer* curr = parent();
788     while (curr && !curr->renderer()->isRenderView() && !curr->transform())
789         curr = curr->parent();
790
791     return curr;
792 }
793
794 static inline const RenderLayer* compositingContainer(const RenderLayer* layer)
795 {
796     return layer->isNormalFlowOnly() ? layer->parent() : layer->stackingContext();
797 }
798
799 #if USE(ACCELERATED_COMPOSITING)
800 RenderLayer* RenderLayer::enclosingCompositingLayer(bool includeSelf) const
801 {
802     if (includeSelf && isComposited())
803         return const_cast<RenderLayer*>(this);
804
805     for (const RenderLayer* curr = compositingContainer(this); curr; curr = compositingContainer(curr)) {
806         if (curr->isComposited())
807             return const_cast<RenderLayer*>(curr);
808     }
809          
810     return 0;
811 }
812 #endif
813
814 RenderLayer* RenderLayer::clippingRoot() const
815 {
816 #if USE(ACCELERATED_COMPOSITING)
817     if (isComposited())
818         return const_cast<RenderLayer*>(this);
819 #endif
820
821     const RenderLayer* current = this;
822     while (current) {
823         if (current->renderer()->isRenderView())
824             return const_cast<RenderLayer*>(current);
825
826         current = compositingContainer(current);
827         ASSERT(current);
828         if (current->transform()
829 #if USE(ACCELERATED_COMPOSITING)
830             || current->isComposited()
831 #endif
832         )
833             return const_cast<RenderLayer*>(current);
834     }
835
836     ASSERT_NOT_REACHED();
837     return 0;
838 }
839
840 IntPoint RenderLayer::absoluteToContents(const IntPoint& absolutePoint) const
841 {
842     // We don't use convertToLayerCoords because it doesn't know about transforms
843     return roundedIntPoint(renderer()->absoluteToLocal(absolutePoint, false, true));
844 }
845
846 bool RenderLayer::requiresSlowRepaints() const
847 {
848     if (isTransparent() || hasReflection() || hasTransform())
849         return true;
850     if (!parent())
851         return false;
852     return parent()->requiresSlowRepaints();
853 }
854
855 bool RenderLayer::isTransparent() const
856 {
857 #if ENABLE(SVG)
858     if (renderer()->node() && renderer()->node()->namespaceURI() == SVGNames::svgNamespaceURI)
859         return false;
860 #endif
861     return renderer()->isTransparent() || renderer()->hasMask();
862 }
863
864 RenderLayer* RenderLayer::transparentPaintingAncestor()
865 {
866     if (isComposited())
867         return 0;
868
869     for (RenderLayer* curr = parent(); curr; curr = curr->parent()) {
870         if (curr->isComposited())
871             return 0;
872         if (curr->isTransparent())
873             return curr;
874     }
875     return 0;
876 }
877
878 static IntRect transparencyClipBox(const RenderLayer* l, const RenderLayer* rootLayer, PaintBehavior paintBehavior);
879
880 static void expandClipRectForDescendantsAndReflection(IntRect& clipRect, const RenderLayer* l, const RenderLayer* rootLayer, PaintBehavior paintBehavior)
881 {
882     // If we have a mask, then the clip is limited to the border box area (and there is
883     // no need to examine child layers).
884     if (!l->renderer()->hasMask()) {
885         // Note: we don't have to walk z-order lists since transparent elements always establish
886         // a stacking context.  This means we can just walk the layer tree directly.
887         for (RenderLayer* curr = l->firstChild(); curr; curr = curr->nextSibling()) {
888             if (!l->reflection() || l->reflectionLayer() != curr)
889                 clipRect.unite(transparencyClipBox(curr, rootLayer, paintBehavior));
890         }
891     }
892
893     // If we have a reflection, then we need to account for that when we push the clip.  Reflect our entire
894     // current transparencyClipBox to catch all child layers.
895     // FIXME: Accelerated compositing will eventually want to do something smart here to avoid incorporating this
896     // size into the parent layer.
897     if (l->renderer()->hasReflection()) {
898         int deltaX = 0;
899         int deltaY = 0;
900         l->convertToLayerCoords(rootLayer, deltaX, deltaY);
901         clipRect.move(-deltaX, -deltaY);
902         clipRect.unite(l->renderBox()->reflectedRect(clipRect));
903         clipRect.move(deltaX, deltaY);
904     }
905 }
906
907 static IntRect transparencyClipBox(const RenderLayer* l, const RenderLayer* rootLayer, PaintBehavior paintBehavior)
908 {
909     // FIXME: Although this function completely ignores CSS-imposed clipping, we did already intersect with the
910     // paintDirtyRect, and that should cut down on the amount we have to paint.  Still it
911     // would be better to respect clips.
912     
913     if (rootLayer != l && l->paintsWithTransform(paintBehavior)) {
914         // The best we can do here is to use enclosed bounding boxes to establish a "fuzzy" enough clip to encompass
915         // the transformed layer and all of its children.
916         int x = 0;
917         int y = 0;
918         l->convertToLayerCoords(rootLayer, x, y);
919
920         TransformationMatrix transform;
921         transform.translate(x, y);
922         transform = *l->transform() * transform;
923
924         IntRect clipRect = l->boundingBox(l);
925         expandClipRectForDescendantsAndReflection(clipRect, l, l, paintBehavior);
926         return transform.mapRect(clipRect);
927     }
928     
929     IntRect clipRect = l->boundingBox(rootLayer);
930     expandClipRectForDescendantsAndReflection(clipRect, l, rootLayer, paintBehavior);
931     return clipRect;
932 }
933
934 void RenderLayer::beginTransparencyLayers(GraphicsContext* p, const RenderLayer* rootLayer, PaintBehavior paintBehavior)
935 {
936     if (p->paintingDisabled() || (paintsWithTransparency(paintBehavior) && m_usedTransparency))
937         return;
938     
939     RenderLayer* ancestor = transparentPaintingAncestor();
940     if (ancestor)
941         ancestor->beginTransparencyLayers(p, rootLayer, paintBehavior);
942     
943     if (paintsWithTransparency(paintBehavior)) {
944         m_usedTransparency = true;
945         p->save();
946         IntRect clipRect = transparencyClipBox(this, rootLayer, paintBehavior);
947         p->clip(clipRect);
948         p->beginTransparencyLayer(renderer()->opacity());
949 #ifdef REVEAL_TRANSPARENCY_LAYERS
950         p->setFillColor(Color(0.0f, 0.0f, 0.5f, 0.2f), DeviceColorSpace);
951         p->fillRect(clipRect);
952 #endif
953     }
954 }
955
956 void* RenderLayer::operator new(size_t sz, RenderArena* renderArena) throw()
957 {
958     return renderArena->allocate(sz);
959 }
960
961 void RenderLayer::operator delete(void* ptr, size_t sz)
962 {
963     // Stash size where destroy can find it.
964     *(size_t *)ptr = sz;
965 }
966
967 void RenderLayer::destroy(RenderArena* renderArena)
968 {
969     delete this;
970
971     // Recover the size left there for us by operator delete and free the memory.
972     renderArena->free(*(size_t *)this, this);
973 }
974
975 void RenderLayer::addChild(RenderLayer* child, RenderLayer* beforeChild)
976 {
977     RenderLayer* prevSibling = beforeChild ? beforeChild->previousSibling() : lastChild();
978     if (prevSibling) {
979         child->setPreviousSibling(prevSibling);
980         prevSibling->setNextSibling(child);
981         ASSERT(prevSibling != child);
982     } else
983         setFirstChild(child);
984
985     if (beforeChild) {
986         beforeChild->setPreviousSibling(child);
987         child->setNextSibling(beforeChild);
988         ASSERT(beforeChild != child);
989     } else
990         setLastChild(child);
991
992     child->setParent(this);
993
994     if (child->isNormalFlowOnly())
995         dirtyNormalFlowList();
996
997     if (!child->isNormalFlowOnly() || child->firstChild()) {
998         // Dirty the z-order list in which we are contained.  The stackingContext() can be null in the
999         // case where we're building up generated content layers.  This is ok, since the lists will start
1000         // off dirty in that case anyway.
1001         child->dirtyStackingContextZOrderLists();
1002     }
1003
1004     child->updateVisibilityStatus();
1005     if (child->m_hasVisibleContent || child->m_hasVisibleDescendant)
1006         childVisibilityChanged(true);
1007     
1008 #if USE(ACCELERATED_COMPOSITING)
1009     compositor()->layerWasAdded(this, child);
1010 #endif
1011 }
1012
1013 RenderLayer* RenderLayer::removeChild(RenderLayer* oldChild)
1014 {
1015 #if USE(ACCELERATED_COMPOSITING)
1016     if (!renderer()->documentBeingDestroyed())
1017         compositor()->layerWillBeRemoved(this, oldChild);
1018 #endif
1019
1020     // remove the child
1021     if (oldChild->previousSibling())
1022         oldChild->previousSibling()->setNextSibling(oldChild->nextSibling());
1023     if (oldChild->nextSibling())
1024         oldChild->nextSibling()->setPreviousSibling(oldChild->previousSibling());
1025
1026     if (m_first == oldChild)
1027         m_first = oldChild->nextSibling();
1028     if (m_last == oldChild)
1029         m_last = oldChild->previousSibling();
1030
1031     if (oldChild->isNormalFlowOnly())
1032         dirtyNormalFlowList();
1033     if (!oldChild->isNormalFlowOnly() || oldChild->firstChild()) { 
1034         // Dirty the z-order list in which we are contained.  When called via the
1035         // reattachment process in removeOnlyThisLayer, the layer may already be disconnected
1036         // from the main layer tree, so we need to null-check the |stackingContext| value.
1037         oldChild->dirtyStackingContextZOrderLists();
1038     }
1039
1040     oldChild->setPreviousSibling(0);
1041     oldChild->setNextSibling(0);
1042     oldChild->setParent(0);
1043     
1044     oldChild->updateVisibilityStatus();
1045     if (oldChild->m_hasVisibleContent || oldChild->m_hasVisibleDescendant)
1046         childVisibilityChanged(false);
1047     
1048     return oldChild;
1049 }
1050
1051 void RenderLayer::removeOnlyThisLayer()
1052 {
1053     if (!m_parent)
1054         return;
1055
1056     // Mark that we are about to lose our layer. This makes render tree
1057     // walks ignore this layer while we're removing it.
1058     m_renderer->setHasLayer(false);
1059
1060 #if USE(ACCELERATED_COMPOSITING)
1061     compositor()->layerWillBeRemoved(m_parent, this);
1062 #endif
1063
1064     // Dirty the clip rects.
1065     clearClipRectsIncludingDescendants();
1066
1067     // Remove us from the parent.
1068     RenderLayer* parent = m_parent;
1069     RenderLayer* nextSib = nextSibling();
1070     parent->removeChild(this);
1071     
1072     if (reflection())
1073         removeChild(reflectionLayer());
1074
1075     // Now walk our kids and reattach them to our parent.
1076     RenderLayer* current = m_first;
1077     while (current) {
1078         RenderLayer* next = current->nextSibling();
1079         removeChild(current);
1080         parent->addChild(current, nextSib);
1081         current->updateLayerPositions(); // Depends on hasLayer() already being false for proper layout.
1082         current = next;
1083     }
1084
1085     m_renderer->destroyLayer();
1086 }
1087
1088 void RenderLayer::insertOnlyThisLayer()
1089 {
1090     if (!m_parent && renderer()->parent()) {
1091         // We need to connect ourselves when our renderer() has a parent.
1092         // Find our enclosingLayer and add ourselves.
1093         RenderLayer* parentLayer = renderer()->parent()->enclosingLayer();
1094         ASSERT(parentLayer);
1095         RenderLayer* beforeChild = parentLayer->reflectionLayer() != this ? renderer()->parent()->findNextLayer(parentLayer, renderer()) : 0;
1096         parentLayer->addChild(this, beforeChild);
1097     }
1098
1099     // Remove all descendant layers from the hierarchy and add them to the new position.
1100     for (RenderObject* curr = renderer()->firstChild(); curr; curr = curr->nextSibling())
1101         curr->moveLayers(m_parent, this);
1102
1103     // Clear out all the clip rects.
1104     clearClipRectsIncludingDescendants();
1105 }
1106
1107 void 
1108 RenderLayer::convertToLayerCoords(const RenderLayer* ancestorLayer, int& xPos, int& yPos) const
1109 {
1110     if (ancestorLayer == this)
1111         return;
1112
1113     EPosition position = renderer()->style()->position();
1114     if (position == FixedPosition && (!ancestorLayer || ancestorLayer == renderer()->view()->layer())) {
1115         // If the fixed layer's container is the root, just add in the offset of the view. We can obtain this by calling
1116         // localToAbsolute() on the RenderView.
1117         FloatPoint absPos = renderer()->localToAbsolute(FloatPoint(), true);
1118         xPos += absPos.x();
1119         yPos += absPos.y();
1120         return;
1121     }
1122  
1123     if (position == FixedPosition) {
1124         // For a fixed layers, we need to walk up to the root to see if there's a fixed position container
1125         // (e.g. a transformed layer). It's an error to call convertToLayerCoords() across a layer with a transform,
1126         // so we should always find the ancestor at or before we find the fixed position container.
1127         RenderLayer* fixedPositionContainerLayer = 0;
1128         bool foundAncestor = false;
1129         for (RenderLayer* currLayer = parent(); currLayer; currLayer = currLayer->parent()) {
1130             if (currLayer == ancestorLayer)
1131                 foundAncestor = true;
1132
1133             if (isFixedPositionedContainer(currLayer)) {
1134                 fixedPositionContainerLayer = currLayer;
1135                 ASSERT(foundAncestor);
1136                 break;
1137             }
1138         }
1139         
1140         ASSERT(fixedPositionContainerLayer); // We should have hit the RenderView's layer at least.
1141
1142         if (fixedPositionContainerLayer != ancestorLayer) {
1143             int fixedContainerX = 0;
1144             int fixedContainerY = 0;
1145             convertToLayerCoords(fixedPositionContainerLayer, fixedContainerX, fixedContainerY);
1146             
1147             int ancestorX = 0;
1148             int ancestorY = 0;
1149             ancestorLayer->convertToLayerCoords(fixedPositionContainerLayer, ancestorX, ancestorY);
1150         
1151             xPos += (fixedContainerX - ancestorX);
1152             yPos += (fixedContainerY - ancestorY);
1153             return;
1154         }
1155     }
1156     
1157     RenderLayer* parentLayer;
1158     if (position == AbsolutePosition || position == FixedPosition) {
1159         // Do what enclosingPositionedAncestor() does, but check for ancestorLayer along the way.
1160         parentLayer = parent();
1161         bool foundAncestorFirst = false;
1162         while (parentLayer) {
1163             if (isPositionedContainer(parentLayer))
1164                 break;
1165
1166             if (parentLayer == ancestorLayer) {
1167                 foundAncestorFirst = true;
1168                 break;
1169             }
1170
1171             parentLayer = parentLayer->parent();
1172         }
1173
1174         if (foundAncestorFirst) {
1175             // Found ancestorLayer before the abs. positioned container, so compute offset of both relative
1176             // to enclosingPositionedAncestor and subtract.
1177             RenderLayer* positionedAncestor = parentLayer->enclosingPositionedAncestor();
1178
1179             int thisX = 0;
1180             int thisY = 0;
1181             convertToLayerCoords(positionedAncestor, thisX, thisY);
1182             
1183             int ancestorX = 0;
1184             int ancestorY = 0;
1185             ancestorLayer->convertToLayerCoords(positionedAncestor, ancestorX, ancestorY);
1186         
1187             xPos += (thisX - ancestorX);
1188             yPos += (thisY - ancestorY);
1189             return;
1190         }
1191     } else
1192         parentLayer = parent();
1193     
1194     if (!parentLayer)
1195         return;
1196     
1197     parentLayer->convertToLayerCoords(ancestorLayer, xPos, yPos);
1198     
1199     xPos += x();
1200     yPos += y();
1201 }
1202
1203 static inline int adjustedScrollDelta(int beginningDelta) {
1204     // This implemention matches Firefox's.
1205     // http://mxr.mozilla.org/firefox/source/toolkit/content/widgets/browser.xml#856.
1206     const int speedReducer = 12;
1207
1208     int adjustedDelta = beginningDelta / speedReducer;
1209     if (adjustedDelta > 1)
1210         adjustedDelta = static_cast<int>(adjustedDelta * sqrt(static_cast<double>(adjustedDelta))) - 1;
1211     else if (adjustedDelta < -1)
1212         adjustedDelta = static_cast<int>(adjustedDelta * sqrt(static_cast<double>(-adjustedDelta))) + 1;
1213
1214     return adjustedDelta;
1215 }
1216
1217 void RenderLayer::panScrollFromPoint(const IntPoint& sourcePoint) 
1218 {
1219     Frame* frame = renderer()->frame();
1220     if (!frame)
1221         return;
1222     
1223     IntPoint currentMousePosition = frame->eventHandler()->currentMousePosition();
1224     
1225     // 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
1226     static IntPoint previousMousePosition;
1227     if (currentMousePosition.x() < 0 || currentMousePosition.y() < 0)
1228         currentMousePosition = previousMousePosition;
1229     else
1230         previousMousePosition = currentMousePosition;
1231
1232     int xDelta = currentMousePosition.x() - sourcePoint.x();
1233     int yDelta = currentMousePosition.y() - sourcePoint.y();
1234
1235     if (abs(xDelta) <= ScrollView::noPanScrollRadius) // at the center we let the space for the icon
1236         xDelta = 0;
1237     if (abs(yDelta) <= ScrollView::noPanScrollRadius)
1238         yDelta = 0;
1239
1240     scrollByRecursively(adjustedScrollDelta(xDelta), adjustedScrollDelta(yDelta));
1241 }
1242
1243 void RenderLayer::scrollByRecursively(int xDelta, int yDelta)
1244 {
1245     if (!xDelta && !yDelta)
1246         return;
1247
1248     bool restrictedByLineClamp = false;
1249     if (renderer()->parent())
1250         restrictedByLineClamp = !renderer()->parent()->style()->lineClamp().isNone();
1251
1252     if (renderer()->hasOverflowClip() && !restrictedByLineClamp) {
1253         int newOffsetX = scrollXOffset() + xDelta;
1254         int newOffsetY = scrollYOffset() + yDelta;
1255         scrollToOffset(newOffsetX, newOffsetY);
1256
1257         // If this layer can't do the scroll we ask the next layer up that can scroll to try
1258         int leftToScrollX = newOffsetX - scrollXOffset();
1259         int leftToScrollY = newOffsetY - scrollYOffset();
1260         if ((leftToScrollX || leftToScrollY) && renderer()->parent()) {
1261             RenderObject* nextRenderer = renderer()->parent();
1262             while (nextRenderer) {
1263                 if (nextRenderer->isBox() && toRenderBox(nextRenderer)->canBeScrolledAndHasScrollableArea()) {
1264                     nextRenderer->enclosingLayer()->scrollByRecursively(leftToScrollX, leftToScrollY);
1265                     break;
1266                 }
1267                 nextRenderer = nextRenderer->parent();
1268             }
1269
1270             Frame* frame = renderer()->frame();
1271             if (frame)
1272                 frame->eventHandler()->updateAutoscrollRenderer();
1273         }
1274     } else if (renderer()->view()->frameView()) {
1275         // If we are here, we were called on a renderer that can be programmatically scrolled, but doesn't
1276         // have an overflow clip. Which means that it is a document node that can be scrolled.
1277         renderer()->view()->frameView()->scrollBy(IntSize(xDelta, yDelta));
1278         // FIXME: If we didn't scroll the whole way, do we want to try looking at the frames ownerElement? 
1279         // https://bugs.webkit.org/show_bug.cgi?id=28237
1280     }
1281 }
1282
1283 void RenderLayer::scrollToOffset(int x, int y, bool updateScrollbars, bool repaint)
1284 {
1285     RenderBox* box = renderBox();
1286     if (!box)
1287         return;
1288
1289     if (box->style()->overflowX() != OMARQUEE) {
1290         if (x < 0) x = 0;
1291         if (y < 0) y = 0;
1292     
1293         // Call the scrollWidth/Height functions so that the dimensions will be computed if they need
1294         // to be (for overflow:hidden blocks).
1295         int maxX = scrollWidth() - box->clientWidth();
1296         int maxY = scrollHeight() - box->clientHeight();
1297         
1298         if (x > maxX) x = maxX;
1299         if (y > maxY) y = maxY;
1300     }
1301     
1302     // FIXME: Eventually, we will want to perform a blit.  For now never
1303     // blit, since the check for blitting is going to be very
1304     // complicated (since it will involve testing whether our layer
1305     // is either occluded by another layer or clipped by an enclosing
1306     // layer or contains fixed backgrounds, etc.).
1307     int newScrollX = x - m_scrollOriginX;
1308     if (m_scrollY == y && m_scrollX == newScrollX)
1309         return;
1310     m_scrollX = newScrollX;
1311     m_scrollY = y;
1312
1313     // Update the positions of our child layers. Don't have updateLayerPositions() update
1314     // compositing layers, because we need to do a deep update from the compositing ancestor.
1315     for (RenderLayer* child = firstChild(); child; child = child->nextSibling())
1316         child->updateLayerPositions(0);
1317
1318     RenderView* view = renderer()->view();
1319     
1320     // We should have a RenderView if we're trying to scroll.
1321     ASSERT(view);
1322     if (view) {
1323 #if ENABLE(DASHBOARD_SUPPORT)
1324         // Update dashboard regions, scrolling may change the clip of a
1325         // particular region.
1326         view->frameView()->updateDashboardRegions();
1327 #endif
1328
1329         view->updateWidgetPositions();
1330     }
1331
1332 #if USE(ACCELERATED_COMPOSITING)
1333     if (compositor()->inCompositingMode()) {
1334         // Our stacking context is guaranteed to contain all of our descendants that may need
1335         // repositioning, so update compositing layers from there.
1336         if (RenderLayer* compositingAncestor = stackingContext()->enclosingCompositingLayer()) {
1337             if (compositor()->compositingConsultsOverlap())
1338                 compositor()->updateCompositingLayers(CompositingUpdateOnScroll, compositingAncestor);
1339             else {
1340                 bool isUpdateRoot = true;
1341                 compositingAncestor->backing()->updateAfterLayout(RenderLayerBacking::AllDescendants, isUpdateRoot);
1342             }
1343         }
1344     }
1345 #endif
1346
1347     RenderBoxModelObject* repaintContainer = renderer()->containerForRepaint();
1348     IntRect rectForRepaint = renderer()->clippedOverflowRectForRepaint(repaintContainer);
1349
1350     Frame* frame = renderer()->frame();
1351     if (frame) {
1352         // The caret rect needs to be invalidated after scrolling
1353         frame->selection()->setCaretRectNeedsUpdate();
1354
1355         FloatQuad quadForFakeMouseMoveEvent = FloatQuad(rectForRepaint);
1356         if (repaintContainer)
1357             quadForFakeMouseMoveEvent = repaintContainer->localToAbsoluteQuad(quadForFakeMouseMoveEvent);
1358         frame->eventHandler()->dispatchFakeMouseMoveEventSoonInQuad(quadForFakeMouseMoveEvent);
1359     }
1360
1361     // Just schedule a full repaint of our object.
1362     if (view && repaint)
1363         renderer()->repaintUsingContainer(repaintContainer, rectForRepaint);
1364
1365     if (updateScrollbars) {
1366         if (m_hBar)
1367             m_hBar->setValue(scrollXOffset(), Scrollbar::NotFromScrollAnimator);
1368         if (m_vBar)
1369             m_vBar->setValue(m_scrollY, Scrollbar::NotFromScrollAnimator);
1370     }
1371
1372     // Schedule the scroll DOM event.
1373     if (view) {
1374         if (FrameView* frameView = view->frameView())
1375             frameView->scheduleEvent(Event::create(eventNames().scrollEvent, false, false), renderer()->node());
1376     }
1377 }
1378
1379 void RenderLayer::scrollRectToVisible(const IntRect& rect, bool scrollToAnchor, const ScrollAlignment& alignX, const ScrollAlignment& alignY)
1380 {
1381     RenderLayer* parentLayer = 0;
1382     IntRect newRect = rect;
1383     int xOffset = 0, yOffset = 0;
1384
1385     // We may end up propagating a scroll event. It is important that we suspend events until 
1386     // the end of the function since they could delete the layer or the layer's renderer().
1387     FrameView* frameView = renderer()->document()->view();
1388     if (frameView)
1389         frameView->pauseScheduledEvents();
1390
1391     bool restrictedByLineClamp = false;
1392     if (renderer()->parent()) {
1393         parentLayer = renderer()->parent()->enclosingLayer();
1394         restrictedByLineClamp = !renderer()->parent()->style()->lineClamp().isNone();
1395     }
1396
1397     if (renderer()->hasOverflowClip() && !restrictedByLineClamp) {
1398         // Don't scroll to reveal an overflow layer that is restricted by the -webkit-line-clamp property.
1399         // This will prevent us from revealing text hidden by the slider in Safari RSS.
1400         RenderBox* box = renderBox();
1401         ASSERT(box);
1402         FloatPoint absPos = box->localToAbsolute();
1403         absPos.move(box->borderLeft(), box->borderTop());
1404
1405         IntRect layerBounds = IntRect(absPos.x() + scrollXOffset(), absPos.y() + scrollYOffset(), box->clientWidth(), box->clientHeight());
1406         IntRect exposeRect = IntRect(rect.x() + scrollXOffset(), rect.y() + scrollYOffset(), rect.width(), rect.height());
1407         IntRect r = getRectToExpose(layerBounds, exposeRect, alignX, alignY);
1408         
1409         xOffset = r.x() - absPos.x();
1410         yOffset = r.y() - absPos.y();
1411         // Adjust offsets if they're outside of the allowable range.
1412         xOffset = max(0, min(scrollWidth() - layerBounds.width(), xOffset));
1413         yOffset = max(0, min(scrollHeight() - layerBounds.height(), yOffset));
1414         
1415         if (xOffset != scrollXOffset() || yOffset != scrollYOffset()) {
1416             int diffX = scrollXOffset();
1417             int diffY = scrollYOffset();
1418             scrollToOffset(xOffset, yOffset);
1419             diffX = scrollXOffset() - diffX;
1420             diffY = scrollYOffset() - diffY;
1421             newRect.setX(rect.x() - diffX);
1422             newRect.setY(rect.y() - diffY);
1423         }
1424     } else if (!parentLayer && renderer()->isBox() && renderBox()->canBeProgramaticallyScrolled(scrollToAnchor)) {
1425         if (frameView) {
1426             if (renderer()->document() && renderer()->document()->ownerElement() && renderer()->document()->ownerElement()->renderer()) {
1427                 IntRect viewRect = frameView->visibleContentRect();
1428                 IntRect r = getRectToExpose(viewRect, rect, alignX, alignY);
1429                 
1430                 xOffset = r.x();
1431                 yOffset = r.y();
1432                 // Adjust offsets if they're outside of the allowable range.
1433                 xOffset = max(0, min(frameView->contentsWidth(), xOffset));
1434                 yOffset = max(0, min(frameView->contentsHeight(), yOffset));
1435
1436                 frameView->setScrollPosition(IntPoint(xOffset, yOffset));
1437                 parentLayer = renderer()->document()->ownerElement()->renderer()->enclosingLayer();
1438                 newRect.setX(rect.x() - frameView->scrollX() + frameView->x());
1439                 newRect.setY(rect.y() - frameView->scrollY() + frameView->y());
1440             } else {
1441                 IntRect viewRect = frameView->visibleContentRect();
1442                 IntRect r = getRectToExpose(viewRect, rect, alignX, alignY);
1443                 
1444                 frameView->setScrollPosition(r.location());
1445
1446                 // This is the outermost view of a web page, so after scrolling this view we
1447                 // scroll its container by calling Page::scrollRectIntoView.
1448                 // This only has an effect on the Mac platform in applications
1449                 // that put web views into scrolling containers, such as Mac OS X Mail.
1450                 // The canAutoscroll function in EventHandler also knows about this.
1451                 if (Frame* frame = frameView->frame()) {
1452                     if (Page* page = frame->page())
1453                         page->chrome()->scrollRectIntoView(rect);
1454                 }
1455             }
1456         }
1457     }
1458     
1459     if (parentLayer)
1460         parentLayer->scrollRectToVisible(newRect, scrollToAnchor, alignX, alignY);
1461
1462     if (frameView)
1463         frameView->resumeScheduledEvents();
1464 }
1465
1466 IntRect RenderLayer::getRectToExpose(const IntRect &visibleRect, const IntRect &exposeRect, const ScrollAlignment& alignX, const ScrollAlignment& alignY)
1467 {
1468     // Determine the appropriate X behavior.
1469     ScrollBehavior scrollX;
1470     IntRect exposeRectX(exposeRect.x(), visibleRect.y(), exposeRect.width(), visibleRect.height());
1471     int intersectWidth = intersection(visibleRect, exposeRectX).width();
1472     if (intersectWidth == exposeRect.width() || intersectWidth >= MIN_INTERSECT_FOR_REVEAL)
1473         // If the rectangle is fully visible, use the specified visible behavior.
1474         // If the rectangle is partially visible, but over a certain threshold,
1475         // then treat it as fully visible to avoid unnecessary horizontal scrolling
1476         scrollX = ScrollAlignment::getVisibleBehavior(alignX);
1477     else if (intersectWidth == visibleRect.width()) {
1478         // If the rect is bigger than the visible area, don't bother trying to center. Other alignments will work.
1479         scrollX = ScrollAlignment::getVisibleBehavior(alignX);
1480         if (scrollX == alignCenter)
1481             scrollX = noScroll;
1482     } else if (intersectWidth > 0)
1483         // If the rectangle is partially visible, but not above the minimum threshold, use the specified partial behavior
1484         scrollX = ScrollAlignment::getPartialBehavior(alignX);
1485     else
1486         scrollX = ScrollAlignment::getHiddenBehavior(alignX);
1487     // If we're trying to align to the closest edge, and the exposeRect is further right
1488     // than the visibleRect, and not bigger than the visible area, then align with the right.
1489     if (scrollX == alignToClosestEdge && exposeRect.right() > visibleRect.right() && exposeRect.width() < visibleRect.width())
1490         scrollX = alignRight;
1491
1492     // Given the X behavior, compute the X coordinate.
1493     int x;
1494     if (scrollX == noScroll) 
1495         x = visibleRect.x();
1496     else if (scrollX == alignRight)
1497         x = exposeRect.right() - visibleRect.width();
1498     else if (scrollX == alignCenter)
1499         x = exposeRect.x() + (exposeRect.width() - visibleRect.width()) / 2;
1500     else
1501         x = exposeRect.x();
1502
1503     // Determine the appropriate Y behavior.
1504     ScrollBehavior scrollY;
1505     IntRect exposeRectY(visibleRect.x(), exposeRect.y(), visibleRect.width(), exposeRect.height());
1506     int intersectHeight = intersection(visibleRect, exposeRectY).height();
1507     if (intersectHeight == exposeRect.height())
1508         // If the rectangle is fully visible, use the specified visible behavior.
1509         scrollY = ScrollAlignment::getVisibleBehavior(alignY);
1510     else if (intersectHeight == visibleRect.height()) {
1511         // If the rect is bigger than the visible area, don't bother trying to center. Other alignments will work.
1512         scrollY = ScrollAlignment::getVisibleBehavior(alignY);
1513         if (scrollY == alignCenter)
1514             scrollY = noScroll;
1515     } else if (intersectHeight > 0)
1516         // If the rectangle is partially visible, use the specified partial behavior
1517         scrollY = ScrollAlignment::getPartialBehavior(alignY);
1518     else
1519         scrollY = ScrollAlignment::getHiddenBehavior(alignY);
1520     // If we're trying to align to the closest edge, and the exposeRect is further down
1521     // than the visibleRect, and not bigger than the visible area, then align with the bottom.
1522     if (scrollY == alignToClosestEdge && exposeRect.bottom() > visibleRect.bottom() && exposeRect.height() < visibleRect.height())
1523         scrollY = alignBottom;
1524
1525     // Given the Y behavior, compute the Y coordinate.
1526     int y;
1527     if (scrollY == noScroll) 
1528         y = visibleRect.y();
1529     else if (scrollY == alignBottom)
1530         y = exposeRect.bottom() - visibleRect.height();
1531     else if (scrollY == alignCenter)
1532         y = exposeRect.y() + (exposeRect.height() - visibleRect.height()) / 2;
1533     else
1534         y = exposeRect.y();
1535
1536     return IntRect(IntPoint(x, y), visibleRect.size());
1537 }
1538
1539 void RenderLayer::autoscroll()
1540 {
1541     Frame* frame = renderer()->frame();
1542     if (!frame)
1543         return;
1544
1545     FrameView* frameView = frame->view();
1546     if (!frameView)
1547         return;
1548
1549 #if ENABLE(DRAG_SUPPORT)
1550     frame->eventHandler()->updateSelectionForMouseDrag();
1551 #endif
1552
1553     IntPoint currentDocumentPosition = frameView->windowToContents(frame->eventHandler()->currentMousePosition());
1554     scrollRectToVisible(IntRect(currentDocumentPosition, IntSize(1, 1)), false, ScrollAlignment::alignToEdgeIfNeeded, ScrollAlignment::alignToEdgeIfNeeded);
1555 }
1556
1557 void RenderLayer::resize(const PlatformMouseEvent& evt, const IntSize& oldOffset)
1558 {
1559     // FIXME: This should be possible on generated content but is not right now.
1560     if (!inResizeMode() || !renderer()->hasOverflowClip() || !renderer()->node())
1561         return;
1562
1563     // Set the width and height of the shadow ancestor node if there is one.
1564     // This is necessary for textarea elements since the resizable layer is in the shadow content.
1565     Element* element = static_cast<Element*>(renderer()->node()->shadowAncestorNode());
1566     RenderBox* renderer = toRenderBox(element->renderer());
1567
1568     EResize resize = renderer->style()->resize();
1569     if (resize == RESIZE_NONE)
1570         return;
1571
1572     Document* document = element->document();
1573     if (!document->frame()->eventHandler()->mousePressed())
1574         return;
1575
1576     float zoomFactor = renderer->style()->effectiveZoom();
1577
1578     IntSize newOffset = offsetFromResizeCorner(document->view()->windowToContents(evt.pos()));
1579     newOffset.setWidth(newOffset.width() / zoomFactor);
1580     newOffset.setHeight(newOffset.height() / zoomFactor);
1581     
1582     IntSize currentSize = IntSize(renderer->width() / zoomFactor, renderer->height() / zoomFactor);
1583     IntSize minimumSize = element->minimumSizeForResizing().shrunkTo(currentSize);
1584     element->setMinimumSizeForResizing(minimumSize);
1585     
1586     IntSize adjustedOldOffset = IntSize(oldOffset.width() / zoomFactor, oldOffset.height() / zoomFactor);
1587     
1588     IntSize difference = (currentSize + newOffset - adjustedOldOffset).expandedTo(minimumSize) - currentSize;
1589
1590     CSSStyleDeclaration* style = element->style();
1591     bool isBoxSizingBorder = renderer->style()->boxSizing() == BORDER_BOX;
1592
1593     ExceptionCode ec;
1594
1595     if (resize != RESIZE_VERTICAL && difference.width()) {
1596         if (element->isFormControlElement()) {
1597             // Make implicit margins from the theme explicit (see <http://bugs.webkit.org/show_bug.cgi?id=9547>).
1598             style->setProperty(CSSPropertyMarginLeft, String::number(renderer->marginLeft() / zoomFactor) + "px", false, ec);
1599             style->setProperty(CSSPropertyMarginRight, String::number(renderer->marginRight() / zoomFactor) + "px", false, ec);
1600         }
1601         int baseWidth = renderer->width() - (isBoxSizingBorder ? 0 : renderer->borderAndPaddingWidth());
1602         baseWidth = baseWidth / zoomFactor;
1603         style->setProperty(CSSPropertyWidth, String::number(baseWidth + difference.width()) + "px", false, ec);
1604     }
1605
1606     if (resize != RESIZE_HORIZONTAL && difference.height()) {
1607         if (element->isFormControlElement()) {
1608             // Make implicit margins from the theme explicit (see <http://bugs.webkit.org/show_bug.cgi?id=9547>).
1609             style->setProperty(CSSPropertyMarginTop, String::number(renderer->marginTop() / zoomFactor) + "px", false, ec);
1610             style->setProperty(CSSPropertyMarginBottom, String::number(renderer->marginBottom() / zoomFactor) + "px", false, ec);
1611         }
1612         int baseHeight = renderer->height() - (isBoxSizingBorder ? 0 : renderer->borderAndPaddingHeight());
1613         baseHeight = baseHeight / zoomFactor;
1614         style->setProperty(CSSPropertyHeight, String::number(baseHeight + difference.height()) + "px", false, ec);
1615     }
1616
1617     document->updateLayout();
1618
1619     // FIXME (Radar 4118564): We should also autoscroll the window as necessary to keep the point under the cursor in view.
1620 }
1621
1622 int RenderLayer::scrollSize(ScrollbarOrientation orientation) const
1623 {
1624     Scrollbar* scrollbar = ((orientation == HorizontalScrollbar) ? m_hBar : m_vBar).get();
1625     return scrollbar ? (scrollbar->totalSize() - scrollbar->visibleSize()) : 0;
1626 }
1627
1628 void RenderLayer::setScrollOffsetFromAnimation(const IntPoint& offset)
1629 {
1630     if (m_hBar)
1631         m_hBar->setValue(offset.x(), Scrollbar::FromScrollAnimator);
1632     if (m_vBar)
1633         m_vBar->setValue(offset.y(), Scrollbar::FromScrollAnimator);
1634 }
1635
1636 void RenderLayer::valueChanged(Scrollbar*)
1637 {
1638     // Update scroll position from scrollbars.
1639
1640     bool needUpdate = false;
1641     int newX = scrollXOffset();
1642     int newY = m_scrollY;
1643     
1644     if (m_hBar) {
1645         newX = m_hBar->value();
1646         if (newX != scrollXOffset())
1647            needUpdate = true;
1648     }
1649
1650     if (m_vBar) {
1651         newY = m_vBar->value();
1652         if (newY != m_scrollY)
1653            needUpdate = true;
1654     }
1655
1656     if (needUpdate)
1657         scrollToOffset(newX, newY, false);
1658 }
1659
1660 bool RenderLayer::isActive() const
1661 {
1662     Page* page = renderer()->frame()->page();
1663     return page && page->focusController()->isActive();
1664 }
1665
1666
1667 static IntRect cornerRect(const RenderLayer* layer, const IntRect& bounds)
1668 {
1669     int horizontalThickness;
1670     int verticalThickness;
1671     if (!layer->verticalScrollbar() && !layer->horizontalScrollbar()) {
1672         // FIXME: This isn't right.  We need to know the thickness of custom scrollbars
1673         // even when they don't exist in order to set the resizer square size properly.
1674         horizontalThickness = ScrollbarTheme::nativeTheme()->scrollbarThickness();
1675         verticalThickness = horizontalThickness;
1676     } else if (layer->verticalScrollbar() && !layer->horizontalScrollbar()) {
1677         horizontalThickness = layer->verticalScrollbar()->width();
1678         verticalThickness = horizontalThickness;
1679     } else if (layer->horizontalScrollbar() && !layer->verticalScrollbar()) {
1680         verticalThickness = layer->horizontalScrollbar()->height();
1681         horizontalThickness = verticalThickness;
1682     } else {
1683         horizontalThickness = layer->verticalScrollbar()->width();
1684         verticalThickness = layer->horizontalScrollbar()->height();
1685     }
1686     return IntRect(bounds.right() - horizontalThickness - layer->renderer()->style()->borderRightWidth(), 
1687                    bounds.bottom() - verticalThickness - layer->renderer()->style()->borderBottomWidth(),
1688                    horizontalThickness, verticalThickness);
1689 }
1690
1691 static IntRect scrollCornerRect(const RenderLayer* layer, const IntRect& bounds)
1692 {
1693     // We have a scrollbar corner when a scrollbar is visible and not filling the entire length of the box.
1694     // This happens when:
1695     // (a) A resizer is present and at least one scrollbar is present
1696     // (b) Both scrollbars are present.
1697     bool hasHorizontalBar = layer->horizontalScrollbar();
1698     bool hasVerticalBar = layer->verticalScrollbar();
1699     bool hasResizer = layer->renderer()->style()->resize() != RESIZE_NONE;
1700     if ((hasHorizontalBar && hasVerticalBar) || (hasResizer && (hasHorizontalBar || hasVerticalBar)))
1701         return cornerRect(layer, bounds);
1702     return IntRect();
1703 }
1704
1705 static IntRect resizerCornerRect(const RenderLayer* layer, const IntRect& bounds)
1706 {
1707     ASSERT(layer->renderer()->isBox());
1708     if (layer->renderer()->style()->resize() == RESIZE_NONE)
1709         return IntRect();
1710     return cornerRect(layer, bounds);
1711 }
1712
1713 bool RenderLayer::scrollbarCornerPresent() const
1714 {
1715     ASSERT(renderer()->isBox());
1716     return !scrollCornerRect(this, renderBox()->borderBoxRect()).isEmpty();
1717 }
1718
1719 IntRect RenderLayer::convertFromScrollbarToContainingView(const Scrollbar* scrollbar, const IntRect& scrollbarRect) const
1720 {
1721     RenderView* view = renderer()->view();
1722     if (!view)
1723         return scrollbarRect;
1724
1725     IntRect rect = scrollbarRect;
1726     rect.move(scrollbarOffset(scrollbar));
1727
1728     return view->frameView()->convertFromRenderer(renderer(), rect);
1729 }
1730
1731 IntRect RenderLayer::convertFromContainingViewToScrollbar(const Scrollbar* scrollbar, const IntRect& parentRect) const
1732 {
1733     RenderView* view = renderer()->view();
1734     if (!view)
1735         return parentRect;
1736
1737     IntRect rect = view->frameView()->convertToRenderer(renderer(), parentRect);
1738     rect.move(-scrollbarOffset(scrollbar));
1739     return rect;
1740 }
1741
1742 IntPoint RenderLayer::convertFromScrollbarToContainingView(const Scrollbar* scrollbar, const IntPoint& scrollbarPoint) const
1743 {
1744     RenderView* view = renderer()->view();
1745     if (!view)
1746         return scrollbarPoint;
1747
1748     IntPoint point = scrollbarPoint;
1749     point.move(scrollbarOffset(scrollbar));
1750     return view->frameView()->convertFromRenderer(renderer(), point);
1751 }
1752
1753 IntPoint RenderLayer::convertFromContainingViewToScrollbar(const Scrollbar* scrollbar, const IntPoint& parentPoint) const
1754 {
1755     RenderView* view = renderer()->view();
1756     if (!view)
1757         return parentPoint;
1758
1759     IntPoint point = view->frameView()->convertToRenderer(renderer(), parentPoint);
1760
1761     point.move(-scrollbarOffset(scrollbar));
1762     return point;
1763 }
1764
1765 IntSize RenderLayer::scrollbarOffset(const Scrollbar* scrollbar) const
1766 {
1767     RenderBox* box = renderBox();
1768
1769     if (scrollbar == m_vBar.get())
1770         return IntSize(box->width() - box->borderRight() - scrollbar->width(), box->borderTop());
1771
1772     if (scrollbar == m_hBar.get())
1773         return IntSize(box->borderLeft(), box->height() - box->borderBottom() - scrollbar->height());
1774     
1775     ASSERT_NOT_REACHED();
1776     return IntSize();
1777 }
1778
1779 void RenderLayer::invalidateScrollbarRect(Scrollbar* scrollbar, const IntRect& rect)
1780 {
1781     IntRect scrollRect = rect;
1782     RenderBox* box = renderBox();
1783     ASSERT(box);
1784     if (scrollbar == m_vBar.get())
1785         scrollRect.move(box->width() - box->borderRight() - scrollbar->width(), box->borderTop());
1786     else
1787         scrollRect.move(box->borderLeft(), box->height() - box->borderBottom() - scrollbar->height());
1788     renderer()->repaintRectangle(scrollRect);
1789 }
1790
1791 PassRefPtr<Scrollbar> RenderLayer::createScrollbar(ScrollbarOrientation orientation)
1792 {
1793     RefPtr<Scrollbar> widget;
1794     RenderObject* actualRenderer = renderer()->node() ? renderer()->node()->shadowAncestorNode()->renderer() : renderer();
1795     bool hasCustomScrollbarStyle = actualRenderer->isBox() && actualRenderer->style()->hasPseudoStyle(SCROLLBAR);
1796     if (hasCustomScrollbarStyle)
1797         widget = RenderScrollbar::createCustomScrollbar(this, orientation, toRenderBox(actualRenderer));
1798     else
1799         widget = Scrollbar::createNativeScrollbar(this, orientation, RegularScrollbar);
1800     renderer()->document()->view()->addChild(widget.get());        
1801     return widget.release();
1802 }
1803
1804 void RenderLayer::destroyScrollbar(ScrollbarOrientation orientation)
1805 {
1806     RefPtr<Scrollbar>& scrollbar = orientation == HorizontalScrollbar ? m_hBar : m_vBar;
1807     if (scrollbar) {
1808         if (scrollbar->isCustomScrollbar())
1809             static_cast<RenderScrollbar*>(scrollbar.get())->clearOwningRenderer();
1810
1811         scrollbar->removeFromParent();
1812         scrollbar->setClient(0);
1813         scrollbar = 0;
1814     }
1815 }
1816
1817 void RenderLayer::setHasHorizontalScrollbar(bool hasScrollbar)
1818 {
1819     if (hasScrollbar == (m_hBar != 0))
1820         return;
1821
1822     if (hasScrollbar)
1823         m_hBar = createScrollbar(HorizontalScrollbar);
1824     else
1825         destroyScrollbar(HorizontalScrollbar);
1826
1827     // Destroying or creating one bar can cause our scrollbar corner to come and go.  We need to update the opposite scrollbar's style.
1828     if (m_hBar)
1829         m_hBar->styleChanged();
1830     if (m_vBar)
1831         m_vBar->styleChanged();
1832
1833 #if ENABLE(DASHBOARD_SUPPORT)
1834     // Force an update since we know the scrollbars have changed things.
1835     if (renderer()->document()->hasDashboardRegions())
1836         renderer()->document()->setDashboardRegionsDirty(true);
1837 #endif
1838 }
1839
1840 void RenderLayer::setHasVerticalScrollbar(bool hasScrollbar)
1841 {
1842     if (hasScrollbar == (m_vBar != 0))
1843         return;
1844
1845     if (hasScrollbar)
1846         m_vBar = createScrollbar(VerticalScrollbar);
1847     else
1848         destroyScrollbar(VerticalScrollbar);
1849
1850      // Destroying or creating one bar can cause our scrollbar corner to come and go.  We need to update the opposite scrollbar's style.
1851     if (m_hBar)
1852         m_hBar->styleChanged();
1853     if (m_vBar)
1854         m_vBar->styleChanged();
1855
1856 #if ENABLE(DASHBOARD_SUPPORT)
1857     // Force an update since we know the scrollbars have changed things.
1858     if (renderer()->document()->hasDashboardRegions())
1859         renderer()->document()->setDashboardRegionsDirty(true);
1860 #endif
1861 }
1862
1863 int RenderLayer::verticalScrollbarWidth() const
1864 {
1865     if (!m_vBar)
1866         return 0;
1867     return m_vBar->width();
1868 }
1869
1870 int RenderLayer::horizontalScrollbarHeight() const
1871 {
1872     if (!m_hBar)
1873         return 0;
1874     return m_hBar->height();
1875 }
1876
1877 IntSize RenderLayer::offsetFromResizeCorner(const IntPoint& absolutePoint) const
1878 {
1879     // Currently the resize corner is always the bottom right corner
1880     IntPoint bottomRight(width(), height());
1881     IntPoint localPoint = absoluteToContents(absolutePoint);
1882     return localPoint - bottomRight;
1883 }
1884
1885 bool RenderLayer::hasOverflowControls() const
1886 {
1887     return m_hBar || m_vBar || m_scrollCorner || renderer()->style()->resize() != RESIZE_NONE;
1888 }
1889
1890 void RenderLayer::positionOverflowControls(int tx, int ty)
1891 {
1892     if (!m_hBar && !m_vBar && (!renderer()->hasOverflowClip() || renderer()->style()->resize() == RESIZE_NONE))
1893         return;
1894     
1895     RenderBox* box = renderBox();
1896     if (!box)
1897         return;
1898
1899     IntRect borderBox = box->borderBoxRect();
1900     IntRect scrollCorner(scrollCornerRect(this, borderBox));
1901     IntRect absBounds(borderBox.x() + tx, borderBox.y() + ty, borderBox.width(), borderBox.height());
1902     if (m_vBar)
1903         m_vBar->setFrameRect(IntRect(absBounds.right() - box->borderRight() - m_vBar->width(),
1904                                      absBounds.y() + box->borderTop(),
1905                                      m_vBar->width(),
1906                                      absBounds.height() - (box->borderTop() + box->borderBottom()) - scrollCorner.height()));
1907
1908     if (m_hBar)
1909         m_hBar->setFrameRect(IntRect(absBounds.x() + box->borderLeft(),
1910                                      absBounds.bottom() - box->borderBottom() - m_hBar->height(),
1911                                      absBounds.width() - (box->borderLeft() + box->borderRight()) - scrollCorner.width(),
1912                                      m_hBar->height()));
1913     
1914     if (m_scrollCorner)
1915         m_scrollCorner->setFrameRect(scrollCorner);
1916     if (m_resizer)
1917         m_resizer->setFrameRect(resizerCornerRect(this, borderBox));
1918 }
1919
1920 int RenderLayer::scrollWidth()
1921 {
1922     if (m_scrollDimensionsDirty)
1923         computeScrollDimensions();
1924     return m_scrollWidth;
1925 }
1926
1927 int RenderLayer::scrollHeight()
1928 {
1929     if (m_scrollDimensionsDirty)
1930         computeScrollDimensions();
1931     return m_scrollHeight;
1932 }
1933
1934 void RenderLayer::computeScrollDimensions(bool* needHBar, bool* needVBar)
1935 {
1936     RenderBox* box = renderBox();
1937     ASSERT(box);
1938     
1939     m_scrollDimensionsDirty = false;
1940     
1941     bool ltr = renderer()->style()->direction() == LTR;
1942
1943     int clientWidth = box->clientWidth();
1944     int clientHeight = box->clientHeight();
1945
1946     m_scrollLeftOverflow = ltr ? 0 : min(0, box->leftmostPosition(true, false) - box->borderLeft());
1947
1948     int rightPos = ltr ?
1949                     box->rightmostPosition(true, false) - box->borderLeft() :
1950                     clientWidth - m_scrollLeftOverflow;
1951     int bottomPos = box->lowestPosition(true, false) - box->borderTop();
1952
1953     m_scrollWidth = max(rightPos, clientWidth);
1954     m_scrollHeight = max(bottomPos, clientHeight);
1955     
1956     m_scrollOriginX = ltr ? 0 : m_scrollWidth - clientWidth;
1957
1958     if (needHBar)
1959         *needHBar = rightPos > clientWidth;
1960     if (needVBar)
1961         *needVBar = bottomPos > clientHeight;
1962 }
1963
1964 void RenderLayer::updateOverflowStatus(bool horizontalOverflow, bool verticalOverflow)
1965 {
1966     if (m_overflowStatusDirty) {
1967         m_horizontalOverflow = horizontalOverflow;
1968         m_verticalOverflow = verticalOverflow;
1969         m_overflowStatusDirty = false;
1970         
1971         return;
1972     }
1973     
1974     bool horizontalOverflowChanged = (m_horizontalOverflow != horizontalOverflow);
1975     bool verticalOverflowChanged = (m_verticalOverflow != verticalOverflow);
1976     
1977     if (horizontalOverflowChanged || verticalOverflowChanged) {
1978         m_horizontalOverflow = horizontalOverflow;
1979         m_verticalOverflow = verticalOverflow;
1980         
1981         if (FrameView* frameView = renderer()->document()->view()) {
1982             frameView->scheduleEvent(OverflowEvent::create(horizontalOverflowChanged, horizontalOverflow, verticalOverflowChanged, verticalOverflow),
1983                 renderer()->node());
1984         }
1985     }
1986 }
1987
1988 void
1989 RenderLayer::updateScrollInfoAfterLayout()
1990 {
1991     RenderBox* box = renderBox();
1992     if (!box)
1993         return;
1994
1995     m_scrollDimensionsDirty = true;
1996
1997     bool horizontalOverflow, verticalOverflow;
1998     computeScrollDimensions(&horizontalOverflow, &verticalOverflow);
1999
2000     if (box->style()->overflowX() != OMARQUEE) {
2001         // Layout may cause us to be in an invalid scroll position.  In this case we need
2002         // to pull our scroll offsets back to the max (or push them up to the min).
2003         int newX = max(0, min(scrollXOffset(), scrollWidth() - box->clientWidth()));
2004         int newY = max(0, min(m_scrollY, scrollHeight() - box->clientHeight()));
2005         if (newX != scrollXOffset() || newY != m_scrollY) {
2006             RenderView* view = renderer()->view();
2007             ASSERT(view);
2008             // scrollToOffset() may call updateLayerPositions(), which doesn't work
2009             // with LayoutState.
2010             // FIXME: Remove the disableLayoutState/enableLayoutState if the above changes.
2011             if (view)
2012                 view->disableLayoutState();
2013             scrollToOffset(newX, newY);
2014             if (view)
2015                 view->enableLayoutState();
2016         }
2017     }
2018
2019     bool haveHorizontalBar = m_hBar;
2020     bool haveVerticalBar = m_vBar;
2021     
2022     // overflow:scroll should just enable/disable.
2023     if (renderer()->style()->overflowX() == OSCROLL)
2024         m_hBar->setEnabled(horizontalOverflow);
2025     if (renderer()->style()->overflowY() == OSCROLL)
2026         m_vBar->setEnabled(verticalOverflow);
2027
2028     // A dynamic change from a scrolling overflow to overflow:hidden means we need to get rid of any
2029     // scrollbars that may be present.
2030     if (renderer()->style()->overflowX() == OHIDDEN && haveHorizontalBar)
2031         setHasHorizontalScrollbar(false);
2032     if (renderer()->style()->overflowY() == OHIDDEN && haveVerticalBar)
2033         setHasVerticalScrollbar(false);
2034     
2035     // overflow:auto may need to lay out again if scrollbars got added/removed.
2036     bool scrollbarsChanged = (box->hasAutoHorizontalScrollbar() && haveHorizontalBar != horizontalOverflow) || 
2037                              (box->hasAutoVerticalScrollbar() && haveVerticalBar != verticalOverflow);    
2038     if (scrollbarsChanged) {
2039         if (box->hasAutoHorizontalScrollbar())
2040             setHasHorizontalScrollbar(horizontalOverflow);
2041         if (box->hasAutoVerticalScrollbar())
2042             setHasVerticalScrollbar(verticalOverflow);
2043
2044 #if ENABLE(DASHBOARD_SUPPORT)
2045         // Force an update since we know the scrollbars have changed things.
2046         if (renderer()->document()->hasDashboardRegions())
2047             renderer()->document()->setDashboardRegionsDirty(true);
2048 #endif
2049
2050         renderer()->repaint();
2051
2052         if (renderer()->style()->overflowX() == OAUTO || renderer()->style()->overflowY() == OAUTO) {
2053             if (!m_inOverflowRelayout) {
2054                 // Our proprietary overflow: overlay value doesn't trigger a layout.
2055                 m_inOverflowRelayout = true;
2056                 renderer()->setNeedsLayout(true, false);
2057                 if (renderer()->isRenderBlock())
2058                     toRenderBlock(renderer())->layoutBlock(true);
2059                 else
2060                     renderer()->layout();
2061                 m_inOverflowRelayout = false;
2062             }
2063         }
2064     }
2065     
2066     // If overflow:scroll is turned into overflow:auto a bar might still be disabled (Bug 11985).
2067     if (m_hBar && box->hasAutoHorizontalScrollbar())
2068         m_hBar->setEnabled(true);
2069     if (m_vBar && box->hasAutoVerticalScrollbar())
2070         m_vBar->setEnabled(true);
2071
2072     // Set up the range (and page step/line step).
2073     if (m_hBar) {
2074         int clientWidth = box->clientWidth();
2075         int pageStep = max(max<int>(clientWidth * Scrollbar::minFractionToStepWhenPaging(), clientWidth - Scrollbar::maxOverlapBetweenPages()), 1);
2076         m_hBar->setSteps(Scrollbar::pixelsPerLineStep(), pageStep);
2077         m_hBar->setProportion(clientWidth, m_scrollWidth);
2078         // Explicitly set the horizontal scroll value.  This ensures that when a
2079         // right-to-left scrollable area's width (or content width) changes, the
2080         // top right corner of the content doesn't shift with respect to the top
2081         // right corner of the area. Conceptually, right-to-left areas have
2082         // their origin at the top-right, but RenderLayer is top-left oriented,
2083         // so this is needed to keep everything working (see how scrollXOffset()
2084         // differs from scrollYOffset() to get an idea of why the horizontal and
2085         // vertical scrollbars need to be treated differently).
2086         m_hBar->setValue(scrollXOffset(), Scrollbar::NotFromScrollAnimator);
2087     }
2088     if (m_vBar) {
2089         int clientHeight = box->clientHeight();
2090         int pageStep = max(max<int>(clientHeight * Scrollbar::minFractionToStepWhenPaging(), clientHeight - Scrollbar::maxOverlapBetweenPages()), 1);
2091         m_vBar->setSteps(Scrollbar::pixelsPerLineStep(), pageStep);
2092         m_vBar->setProportion(clientHeight, m_scrollHeight);
2093     }
2094  
2095     if (renderer()->node() && renderer()->document()->hasListenerType(Document::OVERFLOWCHANGED_LISTENER))
2096         updateOverflowStatus(horizontalOverflow, verticalOverflow);
2097 }
2098
2099 void RenderLayer::paintOverflowControls(GraphicsContext* context, int tx, int ty, const IntRect& damageRect)
2100 {
2101     // Don't do anything if we have no overflow.
2102     if (!renderer()->hasOverflowClip())
2103         return;
2104     
2105     // Move the scrollbar widgets if necessary.  We normally move and resize widgets during layout, but sometimes
2106     // widgets can move without layout occurring (most notably when you scroll a document that
2107     // contains fixed positioned elements).
2108     positionOverflowControls(tx, ty);
2109
2110     // Now that we're sure the scrollbars are in the right place, paint them.
2111     if (m_hBar)
2112         m_hBar->paint(context, damageRect);
2113     if (m_vBar)
2114         m_vBar->paint(context, damageRect);
2115
2116     // We fill our scroll corner with white if we have a scrollbar that doesn't run all the way up to the
2117     // edge of the box.
2118     paintScrollCorner(context, tx, ty, damageRect);
2119     
2120     // Paint our resizer last, since it sits on top of the scroll corner.
2121     paintResizer(context, tx, ty, damageRect);
2122 }
2123
2124 void RenderLayer::paintScrollCorner(GraphicsContext* context, int tx, int ty, const IntRect& damageRect)
2125 {
2126     RenderBox* box = renderBox();
2127     ASSERT(box);
2128
2129     IntRect cornerRect = scrollCornerRect(this, box->borderBoxRect());
2130     IntRect absRect = IntRect(cornerRect.x() + tx, cornerRect.y() + ty, cornerRect.width(), cornerRect.height());
2131     if (!absRect.intersects(damageRect))
2132         return;
2133
2134     if (context->updatingControlTints()) {
2135         updateScrollCornerStyle();
2136         return;
2137     }
2138
2139     if (m_scrollCorner) {
2140         m_scrollCorner->paintIntoRect(context, tx, ty, absRect);
2141         return;
2142     }
2143     
2144     context->fillRect(absRect, Color::white, box->style()->colorSpace());
2145 }
2146
2147 void RenderLayer::paintResizer(GraphicsContext* context, int tx, int ty, const IntRect& damageRect)
2148 {
2149     if (renderer()->style()->resize() == RESIZE_NONE)
2150         return;
2151
2152     RenderBox* box = renderBox();
2153     ASSERT(box);
2154
2155     IntRect cornerRect = resizerCornerRect(this, box->borderBoxRect());
2156     IntRect absRect = IntRect(cornerRect.x() + tx, cornerRect.y() + ty, cornerRect.width(), cornerRect.height());
2157     if (!absRect.intersects(damageRect))
2158         return;
2159
2160     if (context->updatingControlTints()) {
2161         updateResizerStyle();
2162         return;
2163     }
2164     
2165     if (m_resizer) {
2166         m_resizer->paintIntoRect(context, tx, ty, absRect);
2167         return;
2168     }
2169
2170     // Paint the resizer control.
2171     DEFINE_STATIC_LOCAL(RefPtr<Image>, resizeCornerImage, (Image::loadPlatformResource("textAreaResizeCorner")));
2172     IntPoint imagePoint(absRect.right() - resizeCornerImage->width(), absRect.bottom() - resizeCornerImage->height());
2173     context->drawImage(resizeCornerImage.get(), box->style()->colorSpace(), imagePoint);
2174
2175     // Draw a frame around the resizer (1px grey line) if there are any scrollbars present.
2176     // Clipping will exclude the right and bottom edges of this frame.
2177     if (m_hBar || m_vBar) {
2178         context->save();
2179         context->clip(absRect);
2180         IntRect largerCorner = absRect;
2181         largerCorner.setSize(IntSize(largerCorner.width() + 1, largerCorner.height() + 1));
2182         context->setStrokeColor(Color(makeRGB(217, 217, 217)), DeviceColorSpace);
2183         context->setStrokeThickness(1.0f);
2184         context->setFillColor(Color::transparent, DeviceColorSpace);
2185         context->drawRect(largerCorner);
2186         context->restore();
2187     }
2188 }
2189
2190 bool RenderLayer::isPointInResizeControl(const IntPoint& absolutePoint) const
2191 {
2192     if (!renderer()->hasOverflowClip() || renderer()->style()->resize() == RESIZE_NONE)
2193         return false;
2194     
2195     RenderBox* box = renderBox();
2196     ASSERT(box);
2197
2198     IntPoint localPoint = absoluteToContents(absolutePoint);
2199
2200     IntRect localBounds(0, 0, box->width(), box->height());
2201     return resizerCornerRect(this, localBounds).contains(localPoint);
2202 }
2203     
2204 bool RenderLayer::hitTestOverflowControls(HitTestResult& result, const IntPoint& localPoint)
2205 {
2206     if (!m_hBar && !m_vBar && (!renderer()->hasOverflowClip() || renderer()->style()->resize() == RESIZE_NONE))
2207         return false;
2208
2209     RenderBox* box = renderBox();
2210     ASSERT(box);
2211     
2212     IntRect resizeControlRect;
2213     if (renderer()->style()->resize() != RESIZE_NONE) {
2214         resizeControlRect = resizerCornerRect(this, box->borderBoxRect());
2215         if (resizeControlRect.contains(localPoint))
2216             return true;
2217     }
2218
2219     int resizeControlSize = max(resizeControlRect.height(), 0);
2220
2221     if (m_vBar) {
2222         IntRect vBarRect(box->width() - box->borderRight() - m_vBar->width(), 
2223                          box->borderTop(),
2224                          m_vBar->width(),
2225                          box->height() - (box->borderTop() + box->borderBottom()) - (m_hBar ? m_hBar->height() : resizeControlSize));
2226         if (vBarRect.contains(localPoint)) {
2227             result.setScrollbar(m_vBar.get());
2228             return true;
2229         }
2230     }
2231
2232     resizeControlSize = max(resizeControlRect.width(), 0);
2233     if (m_hBar) {
2234         IntRect hBarRect(box->borderLeft(),
2235                          box->height() - box->borderBottom() - m_hBar->height(),
2236                          box->width() - (box->borderLeft() + box->borderRight()) - (m_vBar ? m_vBar->width() : resizeControlSize),
2237                          m_hBar->height());
2238         if (hBarRect.contains(localPoint)) {
2239             result.setScrollbar(m_hBar.get());
2240             return true;
2241         }
2242     }
2243
2244     return false;
2245 }
2246
2247 bool RenderLayer::scroll(ScrollDirection direction, ScrollGranularity granularity, float multiplier)
2248 {
2249     bool didHorizontalScroll = false;
2250     bool didVerticalScroll = false;
2251     
2252     if (m_hBar) {
2253         // Special-case for the ScrollByDocument granularity. A document scroll
2254         // can only be up or down and in both cases the horizontal bar goes all
2255         // the way to the left.
2256         didHorizontalScroll = m_hBar->scroll((granularity == ScrollByDocument) ? ScrollLeft : direction, granularity, multiplier);
2257     }
2258
2259     if (m_vBar)
2260         didVerticalScroll = m_vBar->scroll(direction, granularity, multiplier);
2261
2262     return (didHorizontalScroll || didVerticalScroll);
2263 }
2264
2265 void RenderLayer::paint(GraphicsContext* p, const IntRect& damageRect, PaintBehavior paintBehavior, RenderObject *paintingRoot)
2266 {
2267     OverlapTestRequestMap overlapTestRequests;
2268     paintLayer(this, p, damageRect, paintBehavior, paintingRoot, &overlapTestRequests);
2269     OverlapTestRequestMap::iterator end = overlapTestRequests.end();
2270     for (OverlapTestRequestMap::iterator it = overlapTestRequests.begin(); it != end; ++it)
2271         it->first->setOverlapTestResult(false);
2272 }
2273
2274 static void setClip(GraphicsContext* p, const IntRect& paintDirtyRect, const IntRect& clipRect)
2275 {
2276     if (paintDirtyRect == clipRect)
2277         return;
2278     p->save();
2279     p->clip(clipRect);
2280 }
2281
2282 static void restoreClip(GraphicsContext* p, const IntRect& paintDirtyRect, const IntRect& clipRect)
2283 {
2284     if (paintDirtyRect == clipRect)
2285         return;
2286     p->restore();
2287 }
2288
2289 static void performOverlapTests(OverlapTestRequestMap& overlapTestRequests, const IntRect& layerBounds)
2290 {
2291     Vector<OverlapTestRequestClient*> overlappedRequestClients;
2292     OverlapTestRequestMap::iterator end = overlapTestRequests.end();
2293     for (OverlapTestRequestMap::iterator it = overlapTestRequests.begin(); it != end; ++it) {
2294         if (!layerBounds.intersects(it->second))
2295             continue;
2296
2297         it->first->setOverlapTestResult(true);
2298         overlappedRequestClients.append(it->first);
2299     }
2300     for (size_t i = 0; i < overlappedRequestClients.size(); ++i)
2301         overlapTestRequests.remove(overlappedRequestClients[i]);
2302 }
2303
2304 #if USE(ACCELERATED_COMPOSITING)
2305 static bool shouldDoSoftwarePaint(const RenderLayer* layer, bool paintingReflection)
2306 {
2307     return paintingReflection && !layer->has3DTransform();
2308 }
2309 #endif
2310
2311 void RenderLayer::paintLayer(RenderLayer* rootLayer, GraphicsContext* p,
2312                         const IntRect& paintDirtyRect, PaintBehavior paintBehavior,
2313                         RenderObject* paintingRoot, OverlapTestRequestMap* overlapTestRequests,
2314                         PaintLayerFlags paintFlags)
2315 {
2316 #if USE(ACCELERATED_COMPOSITING)
2317     if (isComposited()) {
2318         // The updatingControlTints() painting pass goes through compositing layers,
2319         // but we need to ensure that we don't cache clip rects computed with the wrong root in this case.
2320         if (p->updatingControlTints() || (paintBehavior & PaintBehaviorFlattenCompositingLayers))
2321             paintFlags |= PaintLayerTemporaryClipRects;
2322         else if (!backing()->paintingGoesToWindow() && !shouldDoSoftwarePaint(this, paintFlags & PaintLayerPaintingReflection)) {
2323             // If this RenderLayer should paint into its backing, that will be done via RenderLayerBacking::paintIntoLayer().
2324             return;
2325         }
2326     }
2327 #endif
2328
2329     // Avoid painting layers when stylesheets haven't loaded.  This eliminates FOUC.
2330     // It's ok not to draw, because later on, when all the stylesheets do load, updateStyleSelector on the Document
2331     // will do a full repaint().
2332     if (renderer()->document()->didLayoutWithPendingStylesheets() && !renderer()->isRenderView() && !renderer()->isRoot())
2333         return;
2334     
2335     // If this layer is totally invisible then there is nothing to paint.
2336     if (!renderer()->opacity())
2337         return;
2338
2339     if (paintsWithTransparency(paintBehavior))
2340         paintFlags |= PaintLayerHaveTransparency;
2341
2342     // Apply a transform if we have one.  A reflection is considered to be a transform, since it is a flip and a translate.
2343     if (paintsWithTransform(paintBehavior) && !(paintFlags & PaintLayerAppliedTransform)) {
2344         TransformationMatrix layerTransform = renderableTransform(paintBehavior);
2345         // If the transform can't be inverted, then don't paint anything.
2346         if (!layerTransform.isInvertible())
2347             return;
2348
2349         // If we have a transparency layer enclosing us and we are the root of a transform, then we need to establish the transparency
2350         // layer from the parent now.
2351         if (paintFlags & PaintLayerHaveTransparency)
2352             parent()->beginTransparencyLayers(p, rootLayer, paintBehavior);
2353   
2354         // Make sure the parent's clip rects have been calculated.
2355         IntRect clipRect = paintDirtyRect;
2356         if (parent()) {
2357             clipRect = backgroundClipRect(rootLayer, paintFlags & PaintLayerTemporaryClipRects);
2358             clipRect.intersect(paintDirtyRect);
2359         }
2360         
2361         // Push the parent coordinate space's clip.
2362         setClip(p, paintDirtyRect, clipRect);
2363
2364         // Adjust the transform such that the renderer's upper left corner will paint at (0,0) in user space.
2365         // This involves subtracting out the position of the layer in our current coordinate space.
2366         int x = 0;
2367         int y = 0;
2368         convertToLayerCoords(rootLayer, x, y);
2369         TransformationMatrix transform(layerTransform);
2370         transform.translateRight(x, y);
2371         
2372         // Apply the transform.
2373         p->save();
2374         p->concatCTM(transform.toAffineTransform());
2375
2376         // Now do a paint with the root layer shifted to be us.
2377         paintLayer(this, p, transform.inverse().mapRect(paintDirtyRect), paintBehavior, paintingRoot, overlapTestRequests, paintFlags | PaintLayerAppliedTransform);
2378
2379         p->restore();
2380         
2381         // Restore the clip.
2382         restoreClip(p, paintDirtyRect, clipRect);
2383         
2384         return;
2385     }
2386
2387     PaintLayerFlags localPaintFlags = paintFlags & ~PaintLayerAppliedTransform;
2388     bool haveTransparency = localPaintFlags & PaintLayerHaveTransparency;
2389
2390     // Paint the reflection first if we have one.
2391     if (m_reflection && !m_paintingInsideReflection) {
2392         // Mark that we are now inside replica painting.
2393         m_paintingInsideReflection = true;
2394         reflectionLayer()->paintLayer(rootLayer, p, paintDirtyRect, paintBehavior, paintingRoot, overlapTestRequests, localPaintFlags | PaintLayerPaintingReflection);
2395         m_paintingInsideReflection = false;
2396     }
2397
2398     // Calculate the clip rects we should use.
2399     IntRect layerBounds, damageRect, clipRectToApply, outlineRect;
2400     calculateRects(rootLayer, paintDirtyRect, layerBounds, damageRect, clipRectToApply, outlineRect, localPaintFlags & PaintLayerTemporaryClipRects);
2401     int x = layerBounds.x();
2402     int y = layerBounds.y();
2403     int tx = x - renderBoxX();
2404     int ty = y - renderBoxY();
2405                              
2406     // Ensure our lists are up-to-date.
2407     updateCompositingAndLayerListsIfNeeded();
2408
2409     bool forceBlackText = paintBehavior & PaintBehaviorForceBlackText;
2410     bool selectionOnly  = paintBehavior & PaintBehaviorSelectionOnly;
2411     
2412     // If this layer's renderer is a child of the paintingRoot, we render unconditionally, which
2413     // is done by passing a nil paintingRoot down to our renderer (as if no paintingRoot was ever set).
2414     // Else, our renderer tree may or may not contain the painting root, so we pass that root along
2415     // so it will be tested against as we descend through the renderers.
2416     RenderObject* paintingRootForRenderer = 0;
2417     if (paintingRoot && !renderer()->isDescendantOf(paintingRoot))
2418         paintingRootForRenderer = paintingRoot;
2419
2420     if (overlapTestRequests)
2421         performOverlapTests(*overlapTestRequests, layerBounds);
2422
2423     // We want to paint our layer, but only if we intersect the damage rect.
2424     bool shouldPaint = intersectsDamageRect(layerBounds, damageRect, rootLayer) && m_hasVisibleContent && isSelfPaintingLayer();
2425     if (shouldPaint && !selectionOnly && !damageRect.isEmpty()) {
2426         // Begin transparency layers lazily now that we know we have to paint something.
2427         if (haveTransparency)
2428             beginTransparencyLayers(p, rootLayer, paintBehavior);
2429         
2430         // Paint our background first, before painting any child layers.
2431         // Establish the clip used to paint our background.
2432         setClip(p, paintDirtyRect, damageRect);
2433
2434         // Paint the background.
2435         PaintInfo paintInfo(p, damageRect, PaintPhaseBlockBackground, false, paintingRootForRenderer, 0);
2436         renderer()->paint(paintInfo, tx, ty);
2437
2438         // Restore the clip.
2439         restoreClip(p, paintDirtyRect, damageRect);
2440     }
2441
2442     // Now walk the sorted list of children with negative z-indices.
2443     paintList(m_negZOrderList, rootLayer, p, paintDirtyRect, paintBehavior, paintingRoot, overlapTestRequests, localPaintFlags);
2444
2445     // Now establish the appropriate clip and paint our child RenderObjects.
2446     if (shouldPaint && !clipRectToApply.isEmpty()) {
2447         // Begin transparency layers lazily now that we know we have to paint something.
2448         if (haveTransparency)
2449             beginTransparencyLayers(p, rootLayer, paintBehavior);
2450
2451         // Set up the clip used when painting our children.
2452         setClip(p, paintDirtyRect, clipRectToApply);
2453         PaintInfo paintInfo(p, clipRectToApply, 
2454                                           selectionOnly ? PaintPhaseSelection : PaintPhaseChildBlockBackgrounds,
2455                                           forceBlackText, paintingRootForRenderer, 0);
2456         renderer()->paint(paintInfo, tx, ty);
2457         if (!selectionOnly) {
2458             paintInfo.phase = PaintPhaseFloat;
2459             renderer()->paint(paintInfo, tx, ty);
2460             paintInfo.phase = PaintPhaseForeground;
2461             paintInfo.overlapTestRequests = overlapTestRequests;
2462             renderer()->paint(paintInfo, tx, ty);
2463             paintInfo.phase = PaintPhaseChildOutlines;
2464             renderer()->paint(paintInfo, tx, ty);
2465         }
2466
2467         // Now restore our clip.
2468         restoreClip(p, paintDirtyRect, clipRectToApply);
2469     }
2470     
2471     if (!outlineRect.isEmpty() && isSelfPaintingLayer()) {
2472         // Paint our own outline
2473         PaintInfo paintInfo(p, outlineRect, PaintPhaseSelfOutline, false, paintingRootForRenderer, 0);
2474         setClip(p, paintDirtyRect, outlineRect);
2475         renderer()->paint(paintInfo, tx, ty);
2476         restoreClip(p, paintDirtyRect, outlineRect);
2477     }
2478     
2479     // Paint any child layers that have overflow.
2480     paintList(m_normalFlowList, rootLayer, p, paintDirtyRect, paintBehavior, paintingRoot, overlapTestRequests, localPaintFlags);
2481     
2482     // Now walk the sorted list of children with positive z-indices.
2483     paintList(m_posZOrderList, rootLayer, p, paintDirtyRect, paintBehavior, paintingRoot, overlapTestRequests, localPaintFlags);
2484         
2485     if (renderer()->hasMask() && shouldPaint && !selectionOnly && !damageRect.isEmpty()) {
2486         setClip(p, paintDirtyRect, damageRect);
2487
2488         // Paint the mask.
2489         PaintInfo paintInfo(p, damageRect, PaintPhaseMask, false, paintingRootForRenderer, 0);
2490         renderer()->paint(paintInfo, tx, ty);
2491         
2492         // Restore the clip.
2493         restoreClip(p, paintDirtyRect, damageRect);
2494     }
2495
2496     // End our transparency layer
2497     if (haveTransparency && m_usedTransparency && !m_paintingInsideReflection) {
2498         p->endTransparencyLayer();
2499         p->restore();
2500         m_usedTransparency = false;
2501     }
2502 }
2503
2504 void RenderLayer::paintList(Vector<RenderLayer*>* list, RenderLayer* rootLayer, GraphicsContext* p,
2505                             const IntRect& paintDirtyRect, PaintBehavior paintBehavior,
2506                             RenderObject* paintingRoot, OverlapTestRequestMap* overlapTestRequests,
2507                             PaintLayerFlags paintFlags)
2508 {
2509     if (!list)
2510         return;
2511     
2512     for (size_t i = 0; i < list->size(); ++i) {
2513         RenderLayer* childLayer = list->at(i);
2514         if (!childLayer->isPaginated())
2515             childLayer->paintLayer(rootLayer, p, paintDirtyRect, paintBehavior, paintingRoot, overlapTestRequests, paintFlags);
2516         else
2517             paintPaginatedChildLayer(childLayer, rootLayer, p, paintDirtyRect, paintBehavior, paintingRoot, overlapTestRequests, paintFlags);
2518     }
2519 }
2520
2521 void RenderLayer::paintPaginatedChildLayer(RenderLayer* childLayer, RenderLayer* rootLayer, GraphicsContext* context,
2522                                              const IntRect& paintDirtyRect, PaintBehavior paintBehavior,
2523                                              RenderObject* paintingRoot, OverlapTestRequestMap* overlapTestRequests,
2524                                              PaintLayerFlags paintFlags)
2525 {
2526     // We need to do multiple passes, breaking up our child layer into strips.
2527     ASSERT(!renderer()->isPositioned());
2528     Vector<RenderLayer*> columnLayers;
2529     RenderLayer* ancestorLayer = isNormalFlowOnly() ? parent() : stackingContext();
2530     for (RenderLayer* curr = childLayer->parent(); curr; curr = curr->parent()) {
2531         if (curr->renderer()->hasColumns())
2532             columnLayers.append(curr);
2533         if (curr == ancestorLayer || (curr->parent() && curr->parent()->renderer()->isPositioned()))
2534             break;
2535     }
2536
2537     ASSERT(columnLayers.size());
2538     
2539     paintChildLayerIntoColumns(childLayer, rootLayer, context, paintDirtyRect, paintBehavior, paintingRoot, overlapTestRequests, paintFlags, columnLayers, columnLayers.size() - 1);
2540 }
2541
2542 void RenderLayer::paintChildLayerIntoColumns(RenderLayer* childLayer, RenderLayer* rootLayer, GraphicsContext* context,
2543                                              const IntRect& paintDirtyRect, PaintBehavior paintBehavior,
2544                                              RenderObject* paintingRoot, OverlapTestRequestMap* overlapTestRequests,
2545                                              PaintLayerFlags paintFlags, const Vector<RenderLayer*>& columnLayers, size_t colIndex)
2546 {
2547     RenderBlock* columnBlock = toRenderBlock(columnLayers[colIndex]->renderer());
2548
2549     ASSERT(columnBlock && columnBlock->hasColumns());
2550     if (!columnBlock || !columnBlock->hasColumns())
2551         return;
2552     
2553     int layerX = 0;
2554     int layerY = 0;
2555     columnBlock->layer()->convertToLayerCoords(rootLayer, layerX, layerY);
2556     
2557     ColumnInfo* colInfo = columnBlock->columnInfo();
2558     unsigned colCount = colInfo->columnCount();
2559     int currYOffset = 0;
2560     for (unsigned i = 0; i < colCount; i++) {
2561         // For each rect, we clip to the rect, and then we adjust our coords.
2562         IntRect colRect = colInfo->columnRectAt(i);
2563         int currXOffset = colRect.x() - (columnBlock->borderLeft() + columnBlock->paddingLeft());
2564         colRect.move(layerX, layerY);
2565
2566         IntRect localDirtyRect(paintDirtyRect);
2567         localDirtyRect.intersect(colRect);
2568         
2569         if (!localDirtyRect.isEmpty()) {
2570             context->save();
2571             
2572             // Each strip pushes a clip, since column boxes are specified as being
2573             // like overflow:hidden.
2574             context->clip(colRect);
2575
2576             if (!colIndex) {
2577                 // Apply a translation transform to change where the layer paints.
2578                 TransformationMatrix oldTransform;
2579                 bool oldHasTransform = childLayer->transform();
2580                 if (oldHasTransform)
2581                     oldTransform = *childLayer->transform();
2582                 TransformationMatrix newTransform(oldTransform);
2583                 newTransform.translateRight(currXOffset, currYOffset);
2584                 
2585                 childLayer->m_transform.set(new TransformationMatrix(newTransform));
2586                 childLayer->paintLayer(rootLayer, context, localDirtyRect, paintBehavior, paintingRoot, overlapTestRequests, paintFlags);
2587                 if (oldHasTransform)
2588                     childLayer->m_transform.set(new TransformationMatrix(oldTransform));
2589                 else
2590                     childLayer->m_transform.clear();
2591             } else {
2592                 // Adjust the transform such that the renderer's upper left corner will paint at (0,0) in user space.
2593                 // This involves subtracting out the position of the layer in our current coordinate space.
2594                 int childX = 0;
2595                 int childY = 0;
2596                 columnLayers[colIndex - 1]->convertToLayerCoords(rootLayer, childX, childY);
2597                 TransformationMatrix transform;
2598                 transform.translateRight(childX + currXOffset, childY + currYOffset);
2599                 
2600                 // Apply the transform.
2601                 context->concatCTM(transform.toAffineTransform());
2602
2603                 // Now do a paint with the root layer shifted to be the next multicol block.
2604                 paintChildLayerIntoColumns(childLayer, columnLayers[colIndex - 1], context, transform.inverse().mapRect(localDirtyRect), paintBehavior, 
2605                                            paintingRoot, overlapTestRequests, paintFlags, 
2606                                            columnLayers, colIndex - 1);
2607             }
2608
2609             context->restore();
2610         }
2611
2612         // Move to the next position.
2613         currYOffset -= colRect.height();
2614     }
2615 }
2616
2617 static inline IntRect frameVisibleRect(RenderObject* renderer)
2618 {
2619     FrameView* frameView = renderer->document()->view();
2620     if (!frameView)
2621         return IntRect();
2622
2623     return frameView->visibleContentRect();
2624 }
2625
2626 bool RenderLayer::hitTest(const HitTestRequest& request, HitTestResult& result)
2627 {
2628     renderer()->document()->updateLayout();
2629     
2630     IntRect boundsRect(m_x, m_y, width(), height());
2631     if (!request.ignoreClipping())
2632         boundsRect.intersect(frameVisibleRect(renderer()));
2633
2634     RenderLayer* insideLayer = hitTestLayer(this, 0, request, result, boundsRect, result.point(), false);
2635     if (!insideLayer) {
2636         // We didn't hit any layer. If we are the root layer and the mouse is -- or just was -- down, 
2637         // return ourselves. We do this so mouse events continue getting delivered after a drag has 
2638         // exited the WebView, and so hit testing over a scrollbar hits the content document.
2639         if ((request.active() || request.mouseUp()) && renderer()->isRenderView()) {
2640             renderer()->updateHitTestResult(result, result.point());
2641             insideLayer = this;
2642         }
2643     }
2644
2645     // Now determine if the result is inside an anchor - if the urlElement isn't already set.
2646     Node* node = result.innerNode();
2647     if (node && !result.URLElement())
2648         result.setURLElement(static_cast<Element*>(node->enclosingLinkEventParentOrSelf()));
2649
2650     // Next set up the correct :hover/:active state along the new chain.
2651     updateHoverActiveState(request, result);
2652     
2653     // Now return whether we were inside this layer (this will always be true for the root
2654     // layer).
2655     return insideLayer;
2656 }
2657
2658 Node* RenderLayer::enclosingElement() const
2659 {
2660     for (RenderObject* r = renderer(); r; r = r->parent()) {
2661         if (Node* e = r->node())
2662             return e;
2663     }
2664     ASSERT_NOT_REACHED();
2665     return 0;
2666 }
2667
2668 // Compute the z-offset of the point in the transformState.
2669 // This is effectively projecting a ray normal to the plane of ancestor, finding where that
2670 // ray intersects target, and computing the z delta between those two points.
2671 static double computeZOffset(const HitTestingTransformState& transformState)
2672 {
2673     // We got an affine transform, so no z-offset
2674     if (transformState.m_accumulatedTransform.isAffine())
2675         return 0;
2676
2677     // Flatten the point into the target plane
2678     FloatPoint targetPoint = transformState.mappedPoint();
2679     
2680     // Now map the point back through the transform, which computes Z.
2681     FloatPoint3D backmappedPoint = transformState.m_accumulatedTransform.mapPoint(FloatPoint3D(targetPoint));
2682     return backmappedPoint.z();
2683 }
2684
2685 PassRefPtr<HitTestingTransformState> RenderLayer::createLocalTransformState(RenderLayer* rootLayer, RenderLayer* containerLayer,
2686                                         const IntRect& hitTestRect, const IntPoint& hitTestPoint,
2687                                         const HitTestingTransformState* containerTransformState) const
2688 {
2689     RefPtr<HitTestingTransformState> transformState;
2690     int offsetX = 0;
2691     int offsetY = 0;
2692     if (containerTransformState) {
2693         // If we're already computing transform state, then it's relative to the container (which we know is non-null).
2694         transformState = HitTestingTransformState::create(*containerTransformState);
2695         convertToLayerCoords(containerLayer, offsetX, offsetY);
2696     } else {
2697         // If this is the first time we need to make transform state, then base it off of hitTestPoint,
2698         // which is relative to rootLayer.
2699         transformState = HitTestingTransformState::create(hitTestPoint, FloatQuad(hitTestRect));
2700         convertToLayerCoords(rootLayer, offsetX, offsetY);
2701     }
2702     
2703     RenderObject* containerRenderer = containerLayer ? containerLayer->renderer() : 0;
2704     if (renderer()->shouldUseTransformFromContainer(containerRenderer)) {
2705         TransformationMatrix containerTransform;
2706         renderer()->getTransformFromContainer(containerRenderer, IntSize(offsetX, offsetY), containerTransform);
2707         transformState->applyTransform(containerTransform, HitTestingTransformState::AccumulateTransform);
2708     } else {
2709         transformState->translate(offsetX, offsetY, HitTestingTransformState::AccumulateTransform);
2710     }
2711     
2712     return transformState;
2713 }
2714
2715
2716 static bool isHitCandidate(const RenderLayer* hitLayer, bool canDepthSort, double* zOffset, const HitTestingTransformState* transformState)
2717 {
2718     if (!hitLayer)
2719         return false;
2720
2721     // The hit layer is depth-sorting with other layers, so just say that it was hit.
2722     if (canDepthSort)
2723         return true;
2724     
2725     // We need to look at z-depth to decide if this layer was hit.
2726     if (zOffset) {
2727         ASSERT(transformState);
2728         // This is actually computing our z, but that's OK because the hitLayer is coplanar with us.
2729         double childZOffset = computeZOffset(*transformState);
2730         if (childZOffset > *zOffset) {
2731             *zOffset = childZOffset;
2732             return true;
2733         }
2734         return false;
2735     }
2736
2737     return true;
2738 }
2739
2740 // hitTestPoint and hitTestRect are relative to rootLayer.
2741 // A 'flattening' layer is one preserves3D() == false.
2742 // transformState.m_accumulatedTransform holds the transform from the containing flattening layer.
2743 // transformState.m_lastPlanarPoint is the hitTestPoint in the plane of the containing flattening layer.
2744 // transformState.m_lastPlanarQuad is the hitTestRect as a quad in the plane of the containing flattening layer.
2745 // 
2746 // If zOffset is non-null (which indicates that the caller wants z offset information), 
2747 //  *zOffset on return is the z offset of the hit point relative to the containing flattening layer.
2748 RenderLayer* RenderLayer::hitTestLayer(RenderLayer* rootLayer, RenderLayer* containerLayer, const HitTestRequest& request, HitTestResult& result,
2749                                        const IntRect& hitTestRect, const IntPoint& hitTestPoint, bool appliedTransform,
2750                                        const HitTestingTransformState* transformState, double* zOffset)
2751 {
2752     // The natural thing would be to keep HitTestingTransformState on the stack, but it's big, so we heap-allocate.
2753
2754     bool useTemporaryClipRects = false;
2755 #if USE(ACCELERATED_COMPOSITING)
2756     useTemporaryClipRects = compositor()->inCompositingMode();
2757 #endif
2758
2759     IntRect hitTestArea = result.rectFromPoint(hitTestPoint);
2760
2761     // Apply a transform if we have one.
2762     if (transform() && !appliedTransform) {
2763         // Make sure the parent's clip rects have been calculated.
2764         if (parent()) {
2765             IntRect clipRect = backgroundClipRect(rootLayer, useTemporaryClipRects);
2766             // Go ahead and test the enclosing clip now.
2767             if (!clipRect.intersects(hitTestArea))
2768                 return 0;
2769         }
2770
2771         // Create a transform state to accumulate this transform.
2772         RefPtr<HitTestingTransformState> newTransformState = createLocalTransformState(rootLayer, containerLayer, hitTestRect, hitTestPoint, transformState);
2773
2774         // If the transform can't be inverted, then don't hit test this layer at all.
2775         if (!newTransformState->m_accumulatedTransform.isInvertible())
2776             return 0;
2777
2778         // Compute the point and the hit test rect in the coords of this layer by using the values
2779         // from the transformState, which store the point and quad in the coords of the last flattened
2780         // layer, and the accumulated transform which lets up map through preserve-3d layers.
2781         //
2782         // We can't just map hitTestPoint and hitTestRect because they may have been flattened (losing z)
2783         // by our container.
2784         IntPoint localPoint = roundedIntPoint(newTransformState->mappedPoint());
2785         IntRect localHitTestRect;
2786 #if USE(ACCELERATED_COMPOSITING)
2787         if (isComposited()) {
2788             // It doesn't make sense to project hitTestRect into the plane of this layer, so use the same bounds we use for painting.
2789             localHitTestRect = backing()->compositedBounds();
2790         } else
2791 #endif
2792             localHitTestRect = newTransformState->mappedQuad().enclosingBoundingBox();
2793
2794         // Now do a hit test with the root layer shifted to be us.
2795         return hitTestLayer(this, containerLayer, request, result, localHitTestRect, localPoint, true, newTransformState.get(), zOffset);
2796     }
2797
2798     // Ensure our lists and 3d status are up-to-date.
2799     updateCompositingAndLayerListsIfNeeded();
2800     update3DTransformedDescendantStatus();
2801     
2802     RefPtr<HitTestingTransformState> localTransformState;
2803     if (appliedTransform) {
2804         // We computed the correct state in the caller (above code), so just reference it.
2805         ASSERT(transformState);
2806         localTransformState = const_cast<HitTestingTransformState*>(transformState);
2807     } else if (transformState || m_has3DTransformedDescendant || preserves3D()) {
2808         // We need transform state for the first time, or to offset the container state, so create it here.
2809         localTransformState = createLocalTransformState(rootLayer, containerLayer, hitTestRect, hitTestPoint, transformState);
2810     }
2811
2812     // Check for hit test on backface if backface-visibility is 'hidden'
2813     if (localTransformState && renderer()->style()->backfaceVisibility() == BackfaceVisibilityHidden) {
2814         TransformationMatrix invertedMatrix = localTransformState->m_accumulatedTransform.inverse();
2815         // If the z-vector of the matrix is negative, the back is facing towards the viewer.
2816         if (invertedMatrix.m33() < 0)
2817             return 0;
2818     }
2819
2820     RefPtr<HitTestingTransformState> unflattenedTransformState = localTransformState;
2821     if (localTransformState && !preserves3D()) {
2822         // Keep a copy of the pre-flattening state, for computing z-offsets for the container
2823         unflattenedTransformState = HitTestingTransformState::create(*localTransformState);
2824         // This layer is flattening, so flatten the state passed to descendants.
2825         localTransformState->flatten();
2826     }
2827     
2828     // Calculate the clip rects we should use.
2829     IntRect layerBounds;
2830     IntRect bgRect;
2831     IntRect fgRect;
2832     IntRect outlineRect;
2833     calculateRects(rootLayer, hitTestRect, layerBounds, bgRect, fgRect, outlineRect, useTemporaryClipRects);
2834     
2835     // The following are used for keeping track of the z-depth of the hit point of 3d-transformed
2836     // descendants.
2837     double localZOffset = -numeric_limits<double>::infinity();
2838     double* zOffsetForDescendantsPtr = 0;
2839     double* zOffsetForContentsPtr = 0;
2840     
2841     bool depthSortDescendants = false;
2842     if (preserves3D()) {
2843         depthSortDescendants = true;
2844         // Our layers can depth-test with our container, so share the z depth pointer with the container, if it passed one down.
2845         zOffsetForDescendantsPtr = zOffset ? zOffset : &localZOffset;
2846         zOffsetForContentsPtr = zOffset ? zOffset : &localZOffset;
2847     } else if (m_has3DTransformedDescendant) {
2848         // Flattening layer with 3d children; use a local zOffset pointer to depth-test children and foreground.
2849         depthSortDescendants = true;
2850         zOffsetForDescendantsPtr = zOffset ? zOffset : &localZOffset;
2851         zOffsetForContentsPtr = zOffset ? zOffset : &localZOffset;
2852     } else if (zOffset) {
2853         zOffsetForDescendantsPtr = 0;
2854         // Container needs us to give back a z offset for the hit layer.
2855         zOffsetForContentsPtr = zOffset;
2856     }
2857     
2858     // This variable tracks which layer the mouse ends up being inside. 
2859     RenderLayer* candidateLayer = 0;
2860
2861     // Begin by walking our list of positive layers from highest z-index down to the lowest z-index.
2862     RenderLayer* hitLayer = hitTestList(m_posZOrderList, rootLayer, request, result, hitTestRect, hitTestPoint,
2863                                         localTransformState.get(), zOffsetForDescendantsPtr, zOffset, unflattenedTransformState.get(), depthSortDescendants);
2864     if (hitLayer) {
2865         if (!depthSortDescendants)
2866             return hitLayer;
2867         candidateLayer = hitLayer;
2868     }
2869
2870     // Now check our overflow objects.
2871     hitLayer = hitTestList(m_normalFlowList, rootLayer, request, result, hitTestRect, hitTestPoint,
2872                            localTransformState.get(), zOffsetForDescendantsPtr, zOffset, unflattenedTransformState.get(), depthSortDescendants);
2873     if (hitLayer) {
2874         if (!depthSortDescendants)
2875             return hitLayer;
2876         candidateLayer = hitLayer;
2877     }
2878
2879     // Next we want to see if the mouse pos is inside the child RenderObjects of the layer.
2880     if (fgRect.intersects(hitTestArea) && isSelfPaintingLayer()) {
2881         // Hit test with a temporary HitTestResult, because we only want to commit to 'result' if we know we're frontmost.
2882         HitTestResult tempResult(result.point(), result.padding());
2883         if (hitTestContents(request, tempResult, layerBounds, hitTestPoint, HitTestDescendants) &&
2884             isHitCandidate(this, false, zOffsetForContentsPtr, unflattenedTransformState.get())) {
2885             if (result.isRectBasedTest())
2886                 result.append(tempResult);
2887             else
2888                 result = tempResult;
2889             if (!depthSortDescendants)
2890                 return this;
2891             // Foreground can depth-sort with descendant layers, so keep this as a candidate.
2892             candidateLayer = this;
2893         } else if (result.isRectBasedTest())
2894             result.append(tempResult);
2895     }
2896
2897     // Now check our negative z-index children.
2898     hitLayer = hitTestList(m_negZOrderList, rootLayer, request, result, hitTestRect, hitTestPoint,
2899                                         localTransformState.get(), zOffsetForDescendantsPtr, zOffset, unflattenedTransformState.get(), depthSortDescendants);
2900     if (hitLayer) {
2901         if (!depthSortDescendants)
2902             return hitLayer;
2903         candidateLayer = hitLayer;
2904     }
2905
2906     // If we found a layer, return. Child layers, and foreground always render in front of background.
2907     if (candidateLayer)
2908         return candidateLayer;
2909
2910     if (bgRect.intersects(hitTestArea) && isSelfPaintingLayer()) {
2911         HitTestResult tempResult(result.point(), result.padding());
2912         if (hitTestContents(request, tempResult, layerBounds, hitTestPoint, HitTestSelf) &&
2913             isHitCandidate(this, false, zOffsetForContentsPtr, unflattenedTransformState.get())) {
2914             if (result.isRectBasedTest())
2915                 result.append(tempResult);
2916             else
2917                 result = tempResult;
2918             return this;
2919         } else if (result.isRectBasedTest())
2920             result.append(tempResult);
2921     }
2922     
2923     return 0;
2924 }
2925
2926 bool RenderLayer::hitTestContents(const HitTestRequest& request, HitTestResult& result, const IntRect& layerBounds, const IntPoint& hitTestPoint, HitTestFilter hitTestFilter) const
2927 {
2928     if (!renderer()->hitTest(request, result, hitTestPoint,
2929                             layerBounds.x() - renderBoxX(),
2930                             layerBounds.y() - renderBoxY(), 
2931                             hitTestFilter)) {
2932         // It's wrong to set innerNode, but then claim that you didn't hit anything, unless it is
2933         // a rect-based test.
2934         ASSERT(!result.innerNode() || (result.isRectBasedTest() && result.rectBasedTestResult().size()));
2935         return false;
2936     }
2937
2938     // For positioned generated content, we might still not have a
2939     // node by the time we get to the layer level, since none of
2940     // the content in the layer has an element. So just walk up
2941     // the tree.
2942     if (!result.innerNode() || !result.innerNonSharedNode()) {
2943         Node* e = enclosingElement();
2944         if (!result.innerNode())
2945             result.setInnerNode(e);
2946         if (!result.innerNonSharedNode())
2947             result.setInnerNonSharedNode(e);
2948     }
2949         
2950     return true;
2951 }
2952
2953 RenderLayer* RenderLayer::hitTestList(Vector<RenderLayer*>* list, RenderLayer* rootLayer,
2954                                       const HitTestRequest& request, HitTestResult& result,
2955                                       const IntRect& hitTestRect, const IntPoint& hitTestPoint,
2956                                       const HitTestingTransformState* transformState, 
2957                                       double* zOffsetForDescendants, double* zOffset,
2958                                       const HitTestingTransformState* unflattenedTransformState,
2959                                       bool depthSortDescendants)
2960 {
2961     if (!list)
2962         return 0;
2963     
2964     RenderLayer* resultLayer = 0;
2965     for (int i = list->size() - 1; i >= 0; --i) {
2966         RenderLayer* childLayer = list->at(i);
2967         RenderLayer* hitLayer = 0;
2968         HitTestResult tempResult(result.point(), result.padding());
2969         if (childLayer->isPaginated())
2970             hitLayer = hitTestPaginatedChildLayer(childLayer, rootLayer, request, tempResult, hitTestRect, hitTestPoint, transformState, zOffsetForDescendants);
2971         else
2972             hitLayer = childLayer->hitTestLayer(rootLayer, this, request, tempResult, hitTestRect, hitTestPoint, false, transformState, zOffsetForDescendants);
2973
2974         // If it a rect-based test, we can safely append the temporary result since it might had hit
2975         // nodes but not necesserily had hitLayer set.
2976         if (result.isRectBasedTest())
2977             result.append(tempResult);
2978
2979         if (isHitCandidate(hitLayer, depthSortDescendants, zOffset, unflattenedTransformState)) {
2980             resultLayer = hitLayer;
2981             if (!result.isRectBasedTest())
2982                 result = tempResult;
2983             if (!depthSortDescendants)
2984                 break;
2985         }
2986     }
2987     
2988     return resultLayer;
2989 }
2990
2991 RenderLayer* RenderLayer::hitTestPaginatedChildLayer(RenderLayer* childLayer, RenderLayer* rootLayer, const HitTestRequest& request, HitTestResult& result,
2992                                                      const IntRect& hitTestRect, const IntPoint& hitTestPoint, const HitTestingTransformState* transformState, double* zOffset)
2993 {
2994     ASSERT(!renderer()->isPositioned());
2995     Vector<RenderLayer*> columnLayers;
2996     RenderLayer* ancestorLayer = isNormalFlowOnly() ? parent() : stackingContext();
2997     for (RenderLayer* curr = childLayer->parent(); curr; curr = curr->parent()) {
2998         if (curr->renderer()->hasColumns())
2999             columnLayers.append(curr);
3000         if (curr == ancestorLayer || (curr->parent() && curr->parent()->renderer()->isPositioned()))
3001             break;
3002     }
3003
3004     ASSERT(columnLayers.size());
3005     return hitTestChildLayerColumns(childLayer, rootLayer, request, result, hitTestRect, hitTestPoint, transformState, zOffset,
3006                                     columnLayers, columnLayers.size() - 1);
3007 }
3008
3009 RenderLayer* RenderLayer::hitTestChildLayerColumns(RenderLayer* childLayer, RenderLayer* rootLayer, const HitTestRequest& request, HitTestResult& result,
3010                                                    const IntRect& hitTestRect, const IntPoint& hitTestPoint, const HitTestingTransformState* transformState, double* zOffset,
3011                                                    const Vector<RenderLayer*>& columnLayers, size_t columnIndex)
3012 {
3013     RenderBlock* columnBlock = toRenderBlock(columnLayers[columnIndex]->renderer());
3014
3015     ASSERT(columnBlock && columnBlock->hasColumns());
3016     if (!columnBlock || !columnBlock->hasColumns())
3017         return 0;
3018     
3019     int layerX = 0;
3020     int layerY = 0;
3021     columnBlock->layer()->convertToLayerCoords(rootLayer, layerX, layerY);
3022     
3023     ColumnInfo* colInfo = columnBlock->columnInfo();
3024     int colCount = colInfo->columnCount();
3025     
3026     // We have to go backwards from the last column to the first.
3027     int left = columnBlock->borderLeft() + columnBlock->paddingLeft();
3028     int currYOffset = 0;
3029     int i;
3030     for (i = 0; i < colCount; i++)
3031         currYOffset -= colInfo->columnRectAt(i).height();
3032     for (i = colCount - 1; i >= 0; i--) {
3033         // For each rect, we clip to the rect, and then we adjust our coords.
3034         IntRect colRect = colInfo->columnRectAt(i);
3035         int currXOffset = colRect.x() - left;
3036         currYOffset += colRect.height();
3037         colRect.move(layerX, layerY);
3038
3039         IntRect localClipRect(hitTestRect);
3040         localClipRect.intersect(colRect);
3041         
3042         if (!localClipRect.isEmpty() && localClipRect.intersects(result.rectFromPoint(hitTestPoint))) {
3043             RenderLayer* hitLayer = 0;
3044             if (!columnIndex) {
3045                 // Apply a translation transform to change where the layer paints.
3046                 TransformationMatrix oldTransform;
3047                 bool oldHasTransform = childLayer->transform();
3048                 if (oldHasTransform)
3049                     oldTransform = *childLayer->transform();
3050                 TransformationMatrix newTransform(oldTransform);
3051                 newTransform.translateRight(currXOffset, currYOffset);
3052                 
3053                 childLayer->m_transform.set(new TransformationMatrix(newTransform));
3054                 hitLayer = childLayer->hitTestLayer(rootLayer, columnLayers[0], request, result, localClipRect, hitTestPoint, false, transformState, zOffset);
3055                 if (oldHasTransform)
3056                     childLayer->m_transform.set(new TransformationMatrix(oldTransform));
3057                 else
3058                     childLayer->m_transform.clear();
3059             } else {
3060                 // Adjust the transform such that the renderer's upper left corner will be at (0,0) in user space.
3061                 // This involves subtracting out the position of the layer in our current coordinate space.
3062                 RenderLayer* nextLayer = columnLayers[columnIndex - 1];
3063                 RefPtr<HitTestingTransformState> newTransformState = nextLayer->createLocalTransformState(rootLayer, nextLayer, localClipRect, hitTestPoint, transformState);
3064                 newTransformState->translate(currXOffset, currYOffset, HitTestingTransformState::AccumulateTransform);
3065                 IntPoint localPoint = roundedIntPoint(newTransformState->mappedPoint());
3066                 IntRect localHitTestRect = newTransformState->mappedQuad().enclosingBoundingBox();
3067                 newTransformState->flatten();
3068
3069                 hitLayer = hitTestChildLayerColumns(childLayer, columnLayers[columnIndex - 1], request, result, localHitTestRect, localPoint,
3070                                                     newTransformState.get(), zOffset, columnLayers, columnIndex - 1);
3071             }
3072
3073             if (hitLayer)
3074                 return hitLayer;
3075         }
3076     }
3077
3078     return 0;
3079 }
3080
3081 void RenderLayer::updateClipRects(const RenderLayer* rootLayer)
3082 {
3083     if (m_clipRects) {
3084         ASSERT(rootLayer == m_clipRectsRoot);
3085         return; // We have the correct cached value.
3086     }
3087     
3088     // For transformed layers, the root layer was shifted to be us, so there is no need to
3089     // examine the parent.  We want to cache clip rects with us as the root.
3090     RenderLayer* parentLayer = rootLayer != this ? parent() : 0;
3091     if (parentLayer)
3092         parentLayer->updateClipRects(rootLayer);
3093
3094     ClipRects clipRects;
3095     calculateClipRects(rootLayer, clipRects, true);
3096
3097     if (parentLayer && parentLayer->clipRects() && clipRects == *parentLayer->clipRects())
3098         m_clipRects = parentLayer->clipRects();
3099     else
3100         m_clipRects = new (renderer()->renderArena()) ClipRects(clipRects);
3101     m_clipRects->ref();
3102 #ifndef NDEBUG
3103     m_clipRectsRoot = rootLayer;
3104 #endif
3105 }
3106
3107 void RenderLayer::calculateClipRects(const RenderLayer* rootLayer, ClipRects& clipRects, bool useCached) const
3108 {
3109     if (!parent()) {
3110         // The root layer's clip rect is always infinite.
3111         clipRects.reset(PaintInfo::infiniteRect());
3112         return;
3113     }
3114
3115     // For transformed layers, the root layer was shifted to be us, so there is no need to
3116     // examine the parent.  We want to cache clip rects with us as the root.
3117     RenderLayer* parentLayer = rootLayer != this ? parent() : 0;
3118     
3119     // Ensure that our parent's clip has been calculated so that we can examine the values.
3120     if (parentLayer) {
3121         if (useCached && parentLayer->clipRects())
3122             clipRects = *parentLayer->clipRects();
3123         else
3124             parentLayer->calculateClipRects(rootLayer, clipRects);
3125     }
3126     else
3127         clipRects.reset(PaintInfo::infiniteRect());
3128
3129     // A fixed object is essentially the root of its containing block hierarchy, so when
3130     // we encounter such an object, we reset our clip rects to the fixedClipRect.
3131     if (renderer()->style()->position() == FixedPosition) {
3132         clipRects.setPosClipRect(clipRects.fixedClipRect());
3133         clipRects.setOverflowClipRect(clipRects.fixedClipRect());
3134         clipRects.setFixed(true);
3135     }
3136     else if (renderer()->style()->position() == RelativePosition)
3137         clipRects.setPosClipRect(clipRects.overflowClipRect());
3138     else if (renderer()->style()->position() == AbsolutePosition)
3139         clipRects.setOverflowClipRect(clipRects.posClipRect());
3140     
3141     // Update the clip rects that will be passed to child layers.
3142     if (renderer()->hasOverflowClip() || renderer()->hasClip()) {
3143         // This layer establishes a clip of some kind.
3144         int x = 0;
3145         int y = 0;
3146         convertToLayerCoords(rootLayer, x, y);
3147         RenderView* view = renderer()->view();
3148         ASSERT(view);
3149         if (view && clipRects.fixed() && rootLayer->renderer() == view) {
3150             x -= view->frameView()->scrollX();
3151             y -= view->frameView()->scrollY();
3152         }
3153         
3154         if (renderer()->hasOverflowClip()) {
3155             IntRect newOverflowClip = toRenderBox(renderer())->overflowClipRect(x, y);
3156             clipRects.setOverflowClipRect(intersection(newOverflowClip, clipRects.overflowClipRect()));
3157             if (renderer()->isPositioned() || renderer()->isRelPositioned())
3158                 clipRects.setPosClipRect(intersection(newOverflowClip, clipRects.posClipRect()));
3159         }
3160         if (renderer()->hasClip()) {
3161             IntRect newPosClip = toRenderBox(renderer())->clipRect(x, y);
3162             clipRects.setPosClipRect(intersection(newPosClip, clipRects.posClipRect()));
3163             clipRects.setOverflowClipRect(intersection(newPosClip, clipRects.overflowClipRect()));
3164             clipRects.setFixedClipRect(intersection(newPosClip, clipRects.fixedClipRect()));
3165         }
3166     }
3167 }
3168
3169 void RenderLayer::parentClipRects(const RenderLayer* rootLayer, ClipRects& clipRects, bool temporaryClipRects) const
3170 {
3171     ASSERT(parent());
3172     if (temporaryClipRects) {
3173         parent()->calculateClipRects(rootLayer, clipRects);
3174         return;
3175     }
3176
3177     parent()->updateClipRects(rootLayer);
3178     clipRects = *parent()->clipRects();
3179 }
3180
3181 IntRect RenderLayer::backgroundClipRect(const RenderLayer* rootLayer, bool temporaryClipRects) const
3182 {
3183     IntRect backgroundRect;
3184     if (parent()) {
3185         ClipRects parentRects;
3186         parentClipRects(rootLayer, parentRects, temporaryClipRects);
3187         backgroundRect = renderer()->style()->position() == FixedPosition ? parentRects.fixedClipRect() :
3188                          (renderer()->isPositioned() ? parentRects.posClipRect() : 
3189                                                        parentRects.overflowClipRect());
3190         RenderView* view = renderer()->view();
3191         ASSERT(view);
3192         if (view && parentRects.fixed() && rootLayer->renderer() == view)
3193             backgroundRect.move(view->frameView()->scrollX(), view->frameView()->scrollY());
3194     }
3195     return backgroundRect;
3196 }
3197
3198 void RenderLayer::calculateRects(const RenderLayer* rootLayer, const IntRect& paintDirtyRect, IntRect& layerBounds,
3199                                  IntRect& backgroundRect, IntRect& foregroundRect, IntRect& outlineRect, bool temporaryClipRects) const
3200 {
3201     if (rootLayer != this && parent()) {
3202         backgroundRect = backgroundClipRect(rootLayer, temporaryClipRects);
3203         backgroundRect.intersect(paintDirtyRect);
3204     } else
3205         backgroundRect = paintDirtyRect;
3206
3207     foregroundRect = backgroundRect;
3208     outlineRect = backgroundRect;
3209     
3210     int x = 0;
3211     int y = 0;
3212     convertToLayerCoords(rootLayer, x, y);
3213     layerBounds = IntRect(x, y, width(), height());
3214     
3215     // Update the clip rects that will be passed to child layers.
3216     if (renderer()->hasOverflowClip() || renderer()->hasClip()) {
3217         // This layer establishes a clip of some kind.
3218         if (renderer()->hasOverflowClip())
3219             foregroundRect.intersect(toRenderBox(renderer())->overflowClipRect(x, y));
3220         if (renderer()->hasClip()) {
3221             // Clip applies to *us* as well, so go ahead and update the damageRect.
3222             IntRect newPosClip = toRenderBox(renderer())->clipRect(x, y);
3223             backgroundRect.intersect(newPosClip);
3224             foregroundRect.intersect(newPosClip);
3225             outlineRect.intersect(newPosClip);
3226         }
3227
3228         // If we establish a clip at all, then go ahead and make sure our background
3229         // rect is intersected with our layer's bounds.
3230         // FIXME: This could be changed to just use generic visual overflow.
3231         // See https://bugs.webkit.org/show_bug.cgi?id=37467 for more information.
3232         if (const ShadowData* boxShadow = renderer()->style()->boxShadow()) {
3233             IntRect overflow = layerBounds;
3234             do {
3235                 if (boxShadow->style() == Normal) {
3236                     IntRect shadowRect = layerBounds;
3237                     shadowRect.move(boxShadow->x(), boxShadow->y());
3238                     shadowRect.inflate(boxShadow->blur() + boxShadow->spread());
3239                     overflow.unite(shadowRect);
3240                 }
3241
3242                 boxShadow = boxShadow->next();
3243             } while (boxShadow);
3244             backgroundRect.intersect(overflow);
3245         } else
3246             backgroundRect.intersect(layerBounds);
3247     }
3248 }
3249
3250 IntRect RenderLayer::childrenClipRect() const
3251 {
3252     RenderLayer* rootLayer = renderer()->view()->layer();
3253     RenderLayer* clippingRootLayer = clippingRoot();
3254     IntRect layerBounds, backgroundRect, foregroundRect, outlineRect;
3255     calculateRects(clippingRootLayer, rootLayer->boundingBox(rootLayer), layerBounds, backgroundRect, foregroundRect, outlineRect);
3256     return clippingRootLayer->renderer()->localToAbsoluteQuad(FloatQuad(foregroundRect)).enclosingBoundingBox();
3257 }
3258
3259 IntRect RenderLayer::selfClipRect() const
3260 {
3261     RenderLayer* rootLayer = renderer()->view()->layer();
3262     RenderLayer* clippingRootLayer = clippingRoot();
3263     IntRect layerBounds, backgroundRect, foregroundRect, outlineRect;
3264     calculateRects(clippingRootLayer, rootLayer->boundingBox(rootLayer), layerBounds, backgroundRect, foregroundRect, outlineRect);
3265     return clippingRootLayer->renderer()->localToAbsoluteQuad(FloatQuad(backgroundRect)).enclosingBoundingBox();
3266 }
3267
3268 void RenderLayer::addBlockSelectionGapsBounds(const IntRect& bounds)
3269 {
3270     m_blockSelectionGapsBounds.unite(bounds);
3271 }
3272
3273 void RenderLayer::clearBlockSelectionGapsBounds()
3274 {
3275     m_blockSelectionGapsBounds = IntRect();
3276     for (RenderLayer* child = firstChild(); child; child = child->nextSibling())
3277         child->clearBlockSelectionGapsBounds();
3278 }
3279
3280 void RenderLayer::repaintBlockSelectionGaps()
3281 {
3282     for (RenderLayer* child = firstChild(); child; child = child->nextSibling())
3283         child->repaintBlockSelectionGaps();
3284
3285     if (m_blockSelectionGapsBounds.isEmpty())
3286         return;
3287
3288     IntRect rect = m_blockSelectionGapsBounds;
3289     rect.move(-scrolledContentOffset());
3290     if (renderer()->hasOverflowClip())
3291         rect.intersect(toRenderBox(renderer())->overflowClipRect(0, 0));
3292     if (renderer()->hasClip())
3293         rect.intersect(toRenderBox(renderer())->clipRect(0, 0));
3294     if (!rect.isEmpty())
3295         renderer()->repaintRectangle(rect);
3296 }
3297
3298 bool RenderLayer::intersectsDamageRect(const IntRect& layerBounds, const IntRect& damageRect, const RenderLayer* rootLayer) const
3299 {
3300     // Always examine the canvas and the root.
3301     // FIXME: Could eliminate the isRoot() check if we fix background painting so that the RenderView
3302     // paints the root's background.
3303     if (renderer()->isRenderView() || renderer()->isRoot())
3304         return true;
3305
3306     // If we aren't an inline flow, and our layer bounds do intersect the damage rect, then we 
3307     // can go ahead and return true.
3308     RenderView* view = renderer()->view();
3309     ASSERT(view);
3310     if (view && !renderer()->isRenderInline()) {
3311         IntRect b = layerBounds;
3312         b.inflate(view->maximalOutlineSize());
3313         if (b.intersects(damageRect))
3314             return true;
3315     }
3316         
3317     // Otherwise we need to compute the bounding box of this single layer and see if it intersects
3318     // the damage rect.
3319     return boundingBox(rootLayer).intersects(damageRect);
3320 }
3321
3322 IntRect RenderLayer::localBoundingBox() const
3323 {
3324     // There are three special cases we need to consider.
3325     // (1) Inline Flows.  For inline flows we will create a bounding box that fully encompasses all of the lines occupied by the
3326     // inline.  In other words, if some <span> wraps to three lines, we'll create a bounding box that fully encloses the
3327     // line boxes of all three lines (including overflow on those lines).
3328     // (2) Left/Top Overflow.  The width/height of layers already includes right/bottom overflow.  However, in the case of left/top
3329     // overflow, we have to create a bounding box that will extend to include this overflow.
3330     // (3) Floats.  When a layer has overhanging floats that it paints, we need to make sure to include these overhanging floats
3331     // as part of our bounding box.  We do this because we are the responsible layer for both hit testing and painting those
3332     // floats.
3333     IntRect result;
3334     if (renderer()->isRenderInline()) {
3335         // Go from our first line box to our last line box.
3336         RenderInline* inlineFlow = toRenderInline(renderer());
3337         InlineFlowBox* firstBox = inlineFlow->firstLineBox();
3338         if (!firstBox)
3339             return result;
3340         int top = firstBox->topVisibleOverflow();
3341         int bottom = inlineFlow->lastLineBox()->bottomVisibleOverflow();
3342         int left = firstBox->x();
3343         for (InlineFlowBox* curr = firstBox->nextLineBox(); curr; curr = curr->nextLineBox())
3344             left = min(left, curr->x());
3345         result = IntRect(left, top, width(), bottom - top);
3346     } else if (renderer()->isTableRow()) {
3347         // Our bounding box is just the union of all of our cells' border/overflow rects.
3348         for (RenderObject* child = renderer()->firstChild(); child; child = child->nextSibling()) {
3349             if (child->isTableCell()) {
3350                 IntRect bbox = toRenderBox(child)->borderBoxRect();
3351                 result.unite(bbox);
3352                 IntRect overflowRect = renderBox()->visibleOverflowRect();
3353                 if (bbox != overflowRect)
3354                     result.unite(overflowRect);
3355             }
3356         }
3357     } else {
3358         RenderBox* box = renderBox();
3359         ASSERT(box);
3360         if (box->hasMask())
3361             result = box->maskClipRect();
3362         else {
3363             IntRect bbox = box->borderBoxRect();
3364             result = bbox;
3365             IntRect overflowRect = box->visibleOverflowRect();
3366             if (bbox != overflowRect)
3367                 result.unite(overflowRect);
3368         }
3369     }
3370
3371     RenderView* view = renderer()->view();
3372     ASSERT(view);
3373     if (view)
3374         result.inflate(view->maximalOutlineSize()); // Used to apply a fudge factor to dirty-rect checks on blocks/tables.
3375
3376     return result;
3377 }
3378
3379 IntRect RenderLayer::boundingBox(const RenderLayer* ancestorLayer) const
3380 {    
3381     IntRect result = localBoundingBox();
3382