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