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