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