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