10de2fe8b9dd7cc71595f6d54a66b39fcb1aca8e
[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         backing()->updateAfterLayout(RenderLayerBacking::CompositingChildren, isUpdateRoot);
421 #endif
422         
423     // With all our children positioned, now update our marquee if we need to.
424     if (m_marquee) {
425         // FIXME: would like to use TemporaryChange<> but it doesn't work with bitfields.
426         bool oldUpdatingMarqueePosition = m_updatingMarqueePosition;
427         m_updatingMarqueePosition = true;
428         m_marquee->updateMarqueePosition();
429         m_updatingMarqueePosition = oldUpdatingMarqueePosition;
430     }
431
432     if (geometryMap)
433         geometryMap->popMappingsToAncestor(parent());
434 }
435
436 LayoutRect RenderLayer::repaintRectIncludingNonCompositingDescendants() const
437 {
438     LayoutRect repaintRect = m_repaintRect;
439     for (RenderLayer* child = firstChild(); child; child = child->nextSibling()) {
440         // Don't include repaint rects for composited child layers; they will paint themselves and have a different origin.
441         if (child->isComposited())
442             continue;
443
444         repaintRect.unite(child->repaintRectIncludingNonCompositingDescendants());
445     }
446     return repaintRect;
447 }
448
449 void RenderLayer::setAncestorChainHasSelfPaintingLayerDescendant()
450 {
451     for (RenderLayer* layer = this; layer; layer = layer->parent()) {
452         if (!layer->m_hasSelfPaintingLayerDescendantDirty && layer->hasSelfPaintingLayerDescendant())
453             break;
454
455         layer->m_hasSelfPaintingLayerDescendantDirty = false;
456         layer->m_hasSelfPaintingLayerDescendant = true;
457     }
458 }
459
460 void RenderLayer::dirtyAncestorChainHasSelfPaintingLayerDescendantStatus()
461 {
462     for (RenderLayer* layer = this; layer; layer = layer->parent()) {
463         layer->m_hasSelfPaintingLayerDescendantDirty = true;
464         // If we have reached a self-painting layer, we know our parent should have a self-painting descendant
465         // in this case, there is no need to dirty our ancestors further.
466         if (layer->isSelfPaintingLayer()) {
467             ASSERT(!parent() || parent()->m_hasSelfPaintingLayerDescendantDirty || parent()->hasSelfPaintingLayerDescendant());
468             break;
469         }
470     }
471 }
472
473 void RenderLayer::computeRepaintRects(const RenderLayerModelObject* repaintContainer, const RenderGeometryMap* geometryMap)
474 {
475     ASSERT(!m_visibleContentStatusDirty);
476
477     m_repaintRect = renderer()->clippedOverflowRectForRepaint(repaintContainer);
478     m_outlineBox = renderer()->outlineBoundsForRepaint(repaintContainer, geometryMap);
479 }
480
481
482 void RenderLayer::computeRepaintRectsIncludingDescendants()
483 {
484     // FIXME: computeRepaintRects() has to walk up the parent chain for every layer to compute the rects.
485     // We should make this more efficient.
486     // FIXME: it's wrong to call this when layout is not up-to-date, which we do.
487     computeRepaintRects(renderer()->containerForRepaint());
488
489     for (RenderLayer* layer = firstChild(); layer; layer = layer->nextSibling())
490         layer->computeRepaintRectsIncludingDescendants();
491 }
492
493 void RenderLayer::clearRepaintRects()
494 {
495     ASSERT(!m_hasVisibleContent);
496     ASSERT(!m_visibleContentStatusDirty);
497
498     m_repaintRect = IntRect();
499     m_outlineBox = IntRect();
500 }
501
502 void RenderLayer::updateLayerPositionsAfterDocumentScroll()
503 {
504     ASSERT(this == renderer()->view()->layer());
505
506     RenderGeometryMap geometryMap(UseTransforms);
507     updateLayerPositionsAfterScroll(&geometryMap);
508 }
509
510 void RenderLayer::updateLayerPositionsAfterOverflowScroll()
511 {
512     RenderGeometryMap geometryMap(UseTransforms);
513     RenderView* view = renderer()->view();
514     if (this != view->layer())
515         geometryMap.pushMappingsToAncestor(parent(), 0);
516
517     // FIXME: why is it OK to not check the ancestors of this layer in order to
518     // initialize the HasSeenViewportConstrainedAncestor and HasSeenAncestorWithOverflowClip flags?
519     updateLayerPositionsAfterScroll(&geometryMap, IsOverflowScroll);
520 }
521
522 void RenderLayer::updateLayerPositionsAfterScroll(RenderGeometryMap* geometryMap, UpdateLayerPositionsAfterScrollFlags flags)
523 {
524     // FIXME: This shouldn't be needed, but there are some corner cases where
525     // these flags are still dirty. Update so that the check below is valid.
526     updateDescendantDependentFlags();
527
528     // If we have no visible content and no visible descendants, there is no point recomputing
529     // our rectangles as they will be empty. If our visibility changes, we are expected to
530     // recompute all our positions anyway.
531     if (!m_hasVisibleDescendant && !m_hasVisibleContent)
532         return;
533
534     bool positionChanged = updateLayerPosition();
535     if (positionChanged)
536         flags |= HasChangedAncestor;
537
538     if (geometryMap)
539         geometryMap->pushMappingsToAncestor(this, parent());
540
541     if (flags & HasChangedAncestor || flags & HasSeenViewportConstrainedAncestor || flags & IsOverflowScroll)
542         clearClipRects();
543
544     if (renderer()->style()->hasViewportConstrainedPosition())
545         flags |= HasSeenViewportConstrainedAncestor;
546
547     if (renderer()->hasOverflowClip())
548         flags |= HasSeenAncestorWithOverflowClip;
549
550     if (flags & HasSeenViewportConstrainedAncestor
551         || (flags & IsOverflowScroll && flags & HasSeenAncestorWithOverflowClip && !m_canSkipRepaintRectsUpdateOnScroll)) {
552         // FIXME: We could track the repaint container as we walk down the tree.
553         computeRepaintRects(renderer()->containerForRepaint(), geometryMap);
554     } else {
555         // Check that our cached rects are correct.
556         ASSERT(m_repaintRect == renderer()->clippedOverflowRectForRepaint(renderer()->containerForRepaint()));
557         ASSERT(m_outlineBox == renderer()->outlineBoundsForRepaint(renderer()->containerForRepaint(), geometryMap));
558     }
559     
560     for (RenderLayer* child = firstChild(); child; child = child->nextSibling())
561         child->updateLayerPositionsAfterScroll(geometryMap, flags);
562
563     // We don't update our reflection as scrolling is a translation which does not change the size()
564     // of an object, thus RenderReplica will still repaint itself properly as the layer position was
565     // updated above.
566
567     if (m_marquee) {
568         bool oldUpdatingMarqueePosition = m_updatingMarqueePosition;
569         m_updatingMarqueePosition = true;
570         m_marquee->updateMarqueePosition();
571         m_updatingMarqueePosition = oldUpdatingMarqueePosition;
572     }
573
574     if (geometryMap)
575         geometryMap->popMappingsToAncestor(parent());
576 }
577
578 #if ENABLE(CSS_COMPOSITING)
579 void RenderLayer::updateBlendMode()
580 {
581     BlendMode newBlendMode = renderer()->style()->blendMode();
582     if (newBlendMode != m_blendMode) {
583         m_blendMode = newBlendMode;
584         if (backing())
585             backing()->setBlendMode(newBlendMode);
586     }
587 }
588 #endif
589
590 void RenderLayer::updateTransform()
591 {
592     // hasTransform() on the renderer is also true when there is transform-style: preserve-3d or perspective set,
593     // so check style too.
594     bool hasTransform = renderer()->hasTransform() && renderer()->style()->hasTransform();
595     bool had3DTransform = has3DTransform();
596
597     bool hadTransform = m_transform;
598     if (hasTransform != hadTransform) {
599         if (hasTransform)
600             m_transform = adoptPtr(new TransformationMatrix);
601         else
602             m_transform.clear();
603         
604         // Layers with transforms act as clip rects roots, so clear the cached clip rects here.
605         clearClipRectsIncludingDescendants();
606     }
607     
608     if (hasTransform) {
609         RenderBox* box = renderBox();
610         ASSERT(box);
611         m_transform->makeIdentity();
612         box->style()->applyTransform(*m_transform, box->pixelSnappedBorderBoxRect().size(), RenderStyle::IncludeTransformOrigin);
613         makeMatrixRenderable(*m_transform, canRender3DTransforms());
614     }
615
616     if (had3DTransform != has3DTransform())
617         dirty3DTransformedDescendantStatus();
618 }
619
620 TransformationMatrix RenderLayer::currentTransform(RenderStyle::ApplyTransformOrigin applyOrigin) const
621 {
622     if (!m_transform)
623         return TransformationMatrix();
624
625 #if USE(ACCELERATED_COMPOSITING)
626     if (renderer()->style()->isRunningAcceleratedAnimation()) {
627         TransformationMatrix currTransform;
628         RefPtr<RenderStyle> style = renderer()->animation()->getAnimatedStyleForRenderer(renderer());
629         style->applyTransform(currTransform, renderBox()->pixelSnappedBorderBoxRect().size(), applyOrigin);
630         makeMatrixRenderable(currTransform, canRender3DTransforms());
631         return currTransform;
632     }
633
634     // m_transform includes transform-origin, so we need to recompute the transform here.
635     if (applyOrigin == RenderStyle::ExcludeTransformOrigin) {
636         RenderBox* box = renderBox();
637         TransformationMatrix currTransform;
638         box->style()->applyTransform(currTransform, box->pixelSnappedBorderBoxRect().size(), RenderStyle::ExcludeTransformOrigin);
639         makeMatrixRenderable(currTransform, canRender3DTransforms());
640         return currTransform;
641     }
642 #else
643     UNUSED_PARAM(applyOrigin);
644 #endif
645
646     return *m_transform;
647 }
648
649 TransformationMatrix RenderLayer::renderableTransform(PaintBehavior paintBehavior) const
650 {
651     if (!m_transform)
652         return TransformationMatrix();
653     
654     if (paintBehavior & PaintBehaviorFlattenCompositingLayers) {
655         TransformationMatrix matrix = *m_transform;
656         makeMatrixRenderable(matrix, false /* flatten 3d */);
657         return matrix;
658     }
659
660     return *m_transform;
661 }
662
663 static bool checkContainingBlockChainForPagination(RenderLayerModelObject* renderer, RenderBox* ancestorColumnsRenderer)
664 {
665     RenderView* view = renderer->view();
666     RenderLayerModelObject* prevBlock = renderer;
667     RenderBlock* containingBlock;
668     for (containingBlock = renderer->containingBlock();
669          containingBlock && containingBlock != view && containingBlock != ancestorColumnsRenderer;
670          containingBlock = containingBlock->containingBlock())
671         prevBlock = containingBlock;
672     
673     // If the columns block wasn't in our containing block chain, then we aren't paginated by it.
674     if (containingBlock != ancestorColumnsRenderer)
675         return false;
676         
677     // If the previous block is absolutely positioned, then we can't be paginated by the columns block.
678     if (prevBlock->isOutOfFlowPositioned())
679         return false;
680         
681     // Otherwise we are paginated by the columns block.
682     return true;
683 }
684
685 void RenderLayer::updatePagination()
686 {
687     m_isPaginated = false;
688     if (isComposited() || !parent())
689         return; // FIXME: We will have to deal with paginated compositing layers someday.
690                 // FIXME: For now the RenderView can't be paginated.  Eventually printing will move to a model where it is though.
691     
692     if (isNormalFlowOnly()) {
693         m_isPaginated = parent()->renderer()->hasColumns();
694         return;
695     }
696
697     // If we're not normal flow, then we need to look for a multi-column object between us and our stacking context.
698     RenderLayer* ancestorStackingContext = stackingContext();
699     for (RenderLayer* curr = parent(); curr; curr = curr->parent()) {
700         if (curr->renderer()->hasColumns()) {
701             m_isPaginated = checkContainingBlockChainForPagination(renderer(), curr->renderBox());
702             return;
703         }
704         if (curr == ancestorStackingContext)
705             return;
706     }
707 }
708
709 void RenderLayer::setHasVisibleContent()
710
711     if (m_hasVisibleContent && !m_visibleContentStatusDirty) {
712         ASSERT(!parent() || parent()->hasVisibleDescendant());
713         return;
714     }
715
716     m_visibleContentStatusDirty = false; 
717     m_hasVisibleContent = true;
718     computeRepaintRects(renderer()->containerForRepaint());
719     if (!isNormalFlowOnly()) {
720         // We don't collect invisible layers in z-order lists if we are not in compositing mode.
721         // As we became visible, we need to dirty our stacking contexts ancestors to be properly
722         // collected. FIXME: When compositing, we could skip this dirtying phase.
723         for (RenderLayer* sc = stackingContext(); sc; sc = sc->stackingContext()) {
724             sc->dirtyZOrderLists();
725             if (sc->hasVisibleContent())
726                 break;
727         }
728     }
729
730     if (parent())
731         parent()->setAncestorChainHasVisibleDescendant();
732 }
733
734 void RenderLayer::dirtyVisibleContentStatus() 
735
736     m_visibleContentStatusDirty = true; 
737     if (parent())
738         parent()->dirtyAncestorChainVisibleDescendantStatus();
739 }
740
741 void RenderLayer::dirtyAncestorChainVisibleDescendantStatus()
742 {
743     for (RenderLayer* layer = this; layer; layer = layer->parent()) {
744         if (layer->m_visibleDescendantStatusDirty)
745             break;
746
747         layer->m_visibleDescendantStatusDirty = true;
748     }
749 }
750
751 void RenderLayer::setAncestorChainHasVisibleDescendant()
752 {
753     for (RenderLayer* layer = this; layer; layer = layer->parent()) {
754         if (!layer->m_visibleDescendantStatusDirty && layer->hasVisibleDescendant())
755             break;
756
757         layer->m_hasVisibleDescendant = true;
758         layer->m_visibleDescendantStatusDirty = false;
759     }
760 }
761
762 void RenderLayer::updateDescendantDependentFlags()
763 {
764     if (m_visibleDescendantStatusDirty || m_hasSelfPaintingLayerDescendantDirty) {
765         m_hasVisibleDescendant = false;
766         m_hasSelfPaintingLayerDescendant = false;
767         for (RenderLayer* child = firstChild(); child; child = child->nextSibling()) {
768             child->updateDescendantDependentFlags();
769
770             bool hasVisibleDescendant = child->m_hasVisibleContent || child->m_hasVisibleDescendant;
771             bool hasSelfPaintingLayerDescendant = child->isSelfPaintingLayer() || child->hasSelfPaintingLayerDescendant();
772
773             m_hasVisibleDescendant |= hasVisibleDescendant;
774             m_hasSelfPaintingLayerDescendant |= hasSelfPaintingLayerDescendant;
775
776             if (m_hasVisibleDescendant && m_hasSelfPaintingLayerDescendant)
777                 break;
778         }
779         m_visibleDescendantStatusDirty = false;
780         m_hasSelfPaintingLayerDescendantDirty = false;
781     }
782
783     if (m_visibleContentStatusDirty) {
784         if (renderer()->style()->visibility() == VISIBLE)
785             m_hasVisibleContent = true;
786         else {
787             // layer may be hidden but still have some visible content, check for this
788             m_hasVisibleContent = false;
789             RenderObject* r = renderer()->firstChild();
790             while (r) {
791                 if (r->style()->visibility() == VISIBLE && !r->hasLayer()) {
792                     m_hasVisibleContent = true;
793                     break;
794                 }
795                 if (r->firstChild() && !r->hasLayer())
796                     r = r->firstChild();
797                 else if (r->nextSibling())
798                     r = r->nextSibling();
799                 else {
800                     do {
801                         r = r->parent();
802                         if (r == renderer())
803                             r = 0;
804                     } while (r && !r->nextSibling());
805                     if (r)
806                         r = r->nextSibling();
807                 }
808             }
809         }    
810         m_visibleContentStatusDirty = false; 
811     }
812 }
813
814 void RenderLayer::dirty3DTransformedDescendantStatus()
815 {
816     RenderLayer* curr = stackingContext();
817     if (curr)
818         curr->m_3DTransformedDescendantStatusDirty = true;
819         
820     // This propagates up through preserve-3d hierarchies to the enclosing flattening layer.
821     // Note that preserves3D() creates stacking context, so we can just run up the stacking contexts.
822     while (curr && curr->preserves3D()) {
823         curr->m_3DTransformedDescendantStatusDirty = true;
824         curr = curr->stackingContext();
825     }
826 }
827
828 // Return true if this layer or any preserve-3d descendants have 3d.
829 bool RenderLayer::update3DTransformedDescendantStatus()
830 {
831     if (m_3DTransformedDescendantStatusDirty) {
832         m_has3DTransformedDescendant = false;
833
834         updateZOrderLists();
835
836         // Transformed or preserve-3d descendants can only be in the z-order lists, not
837         // in the normal flow list, so we only need to check those.
838         if (Vector<RenderLayer*>* positiveZOrderList = posZOrderList()) {
839             for (unsigned i = 0; i < positiveZOrderList->size(); ++i)
840                 m_has3DTransformedDescendant |= positiveZOrderList->at(i)->update3DTransformedDescendantStatus();
841         }
842
843         // Now check our negative z-index children.
844         if (Vector<RenderLayer*>* negativeZOrderList = negZOrderList()) {
845             for (unsigned i = 0; i < negativeZOrderList->size(); ++i)
846                 m_has3DTransformedDescendant |= negativeZOrderList->at(i)->update3DTransformedDescendantStatus();
847         }
848         
849         m_3DTransformedDescendantStatusDirty = false;
850     }
851     
852     // If we live in a 3d hierarchy, then the layer at the root of that hierarchy needs
853     // the m_has3DTransformedDescendant set.
854     if (preserves3D())
855         return has3DTransform() || m_has3DTransformedDescendant;
856
857     return has3DTransform();
858 }
859
860 bool RenderLayer::updateLayerPosition()
861 {
862     LayoutPoint localPoint;
863     LayoutSize inlineBoundingBoxOffset; // We don't put this into the RenderLayer x/y for inlines, so we need to subtract it out when done.
864     if (renderer()->isInline() && renderer()->isRenderInline()) {
865         RenderInline* inlineFlow = toRenderInline(renderer());
866         IntRect lineBox = inlineFlow->linesBoundingBox();
867         setSize(lineBox.size());
868         inlineBoundingBoxOffset = toSize(lineBox.location());
869         localPoint += inlineBoundingBoxOffset;
870     } else if (RenderBox* box = renderBox()) {
871         // FIXME: Is snapping the size really needed here for the RenderBox case?
872         setSize(pixelSnappedIntSize(box->size(), box->location()));
873         localPoint += box->topLeftLocationOffset();
874     }
875
876     if (!renderer()->isOutOfFlowPositioned() && renderer()->parent()) {
877         // We must adjust our position by walking up the render tree looking for the
878         // nearest enclosing object with a layer.
879         RenderObject* curr = renderer()->parent();
880         while (curr && !curr->hasLayer()) {
881             if (curr->isBox() && !curr->isTableRow()) {
882                 // Rows and cells share the same coordinate space (that of the section).
883                 // Omit them when computing our xpos/ypos.
884                 localPoint += toRenderBox(curr)->topLeftLocationOffset();
885             }
886             curr = curr->parent();
887         }
888         if (curr->isBox() && curr->isTableRow()) {
889             // Put ourselves into the row coordinate space.
890             localPoint -= toRenderBox(curr)->topLeftLocationOffset();
891         }
892     }
893     
894     // Subtract our parent's scroll offset.
895     if (renderer()->isOutOfFlowPositioned() && enclosingPositionedAncestor()) {
896         RenderLayer* positionedParent = enclosingPositionedAncestor();
897
898         // For positioned layers, we subtract out the enclosing positioned layer's scroll offset.
899         LayoutSize offset = positionedParent->scrolledContentOffset();
900         localPoint -= offset;
901         
902         if (renderer()->isOutOfFlowPositioned() && positionedParent->renderer()->isInFlowPositioned() && positionedParent->renderer()->isRenderInline()) {
903             LayoutSize offset = toRenderInline(positionedParent->renderer())->offsetForInFlowPositionedInline(toRenderBox(renderer()));
904             localPoint += offset;
905         }
906     } else if (parent()) {
907         if (isComposited()) {
908             // FIXME: Composited layers ignore pagination, so about the best we can do is make sure they're offset into the appropriate column.
909             // They won't split across columns properly.
910             LayoutSize columnOffset;
911             if (!parent()->renderer()->hasColumns() && parent()->renderer()->isRoot() && renderer()->view()->hasColumns())
912                 renderer()->view()->adjustForColumns(columnOffset, localPoint);
913             else
914                 parent()->renderer()->adjustForColumns(columnOffset, localPoint);
915
916             localPoint += columnOffset;
917         }
918
919         IntSize scrollOffset = parent()->scrolledContentOffset();
920         localPoint -= scrollOffset;
921     }
922     
923     bool positionOrOffsetChanged = false;
924     if (renderer()->isInFlowPositioned()) {
925         LayoutSize newOffset = toRenderBoxModelObject(renderer())->offsetForInFlowPosition();
926         positionOrOffsetChanged = newOffset != m_offsetForInFlowPosition;
927         m_offsetForInFlowPosition = newOffset;
928         localPoint.move(m_offsetForInFlowPosition);
929     } else {
930         m_offsetForInFlowPosition = LayoutSize();
931     }
932
933     // FIXME: We'd really like to just get rid of the concept of a layer rectangle and rely on the renderers.
934     localPoint -= inlineBoundingBoxOffset;
935     
936     positionOrOffsetChanged |= location() != localPoint;
937     setLocation(localPoint);
938     return positionOrOffsetChanged;
939 }
940
941 TransformationMatrix RenderLayer::perspectiveTransform() const
942 {
943     if (!renderer()->hasTransform())
944         return TransformationMatrix();
945
946     RenderStyle* style = renderer()->style();
947     if (!style->hasPerspective())
948         return TransformationMatrix();
949
950     // Maybe fetch the perspective from the backing?
951     const IntRect borderBox = toRenderBox(renderer())->pixelSnappedBorderBoxRect();
952     const float boxWidth = borderBox.width();
953     const float boxHeight = borderBox.height();
954
955     float perspectiveOriginX = floatValueForLength(style->perspectiveOriginX(), boxWidth);
956     float perspectiveOriginY = floatValueForLength(style->perspectiveOriginY(), boxHeight);
957
958     // A perspective origin of 0,0 makes the vanishing point in the center of the element.
959     // We want it to be in the top-left, so subtract half the height and width.
960     perspectiveOriginX -= boxWidth / 2.0f;
961     perspectiveOriginY -= boxHeight / 2.0f;
962     
963     TransformationMatrix t;
964     t.translate(perspectiveOriginX, perspectiveOriginY);
965     t.applyPerspective(style->perspective());
966     t.translate(-perspectiveOriginX, -perspectiveOriginY);
967     
968     return t;
969 }
970
971 FloatPoint RenderLayer::perspectiveOrigin() const
972 {
973     if (!renderer()->hasTransform())
974         return FloatPoint();
975
976     const LayoutRect borderBox = toRenderBox(renderer())->borderBoxRect();
977     RenderStyle* style = renderer()->style();
978
979     return FloatPoint(floatValueForLength(style->perspectiveOriginX(), borderBox.width()),
980                       floatValueForLength(style->perspectiveOriginY(), borderBox.height()));
981 }
982
983 RenderLayer* RenderLayer::stackingContext() const
984 {
985     RenderLayer* layer = parent();
986     while (layer && !layer->isStackingContext())
987         layer = layer->parent();
988
989     ASSERT(!layer || layer->isStackingContext());
990     return layer;
991 }
992
993 static inline bool isPositionedContainer(RenderLayer* layer)
994 {
995     RenderLayerModelObject* layerRenderer = layer->renderer();
996     return layer->isRootLayer() || layerRenderer->isPositioned() || layer->hasTransform();
997 }
998
999 static inline bool isFixedPositionedContainer(RenderLayer* layer)
1000 {
1001     return layer->isRootLayer() || layer->hasTransform();
1002 }
1003
1004 RenderLayer* RenderLayer::enclosingPositionedAncestor() const
1005 {
1006     RenderLayer* curr = parent();
1007     while (curr && !isPositionedContainer(curr))
1008         curr = curr->parent();
1009
1010     return curr;
1011 }
1012
1013 RenderLayer* RenderLayer::enclosingScrollableLayer() const
1014 {
1015     for (RenderObject* nextRenderer = renderer()->parent(); nextRenderer; nextRenderer = nextRenderer->parent()) {
1016         if (nextRenderer->isBox() && toRenderBox(nextRenderer)->canBeScrolledAndHasScrollableArea())
1017             return nextRenderer->enclosingLayer();
1018     }
1019
1020     return 0;
1021 }
1022
1023 IntRect RenderLayer::scrollableAreaBoundingBox() const
1024 {
1025     return renderer()->absoluteBoundingBoxRect();
1026 }
1027
1028 RenderLayer* RenderLayer::enclosingTransformedAncestor() const
1029 {
1030     RenderLayer* curr = parent();
1031     while (curr && !curr->isRootLayer() && !curr->transform())
1032         curr = curr->parent();
1033
1034     return curr;
1035 }
1036
1037 static inline const RenderLayer* compositingContainer(const RenderLayer* layer)
1038 {
1039     return layer->isNormalFlowOnly() ? layer->parent() : layer->stackingContext();
1040 }
1041
1042 inline bool RenderLayer::shouldRepaintAfterLayout() const
1043 {
1044 #if USE(ACCELERATED_COMPOSITING)
1045     if (m_repaintStatus == NeedsNormalRepaint)
1046         return true;
1047
1048     // Composited layers that were moved during a positioned movement only
1049     // layout, don't need to be repainted. They just need to be recomposited.
1050     ASSERT(m_repaintStatus == NeedsFullRepaintForPositionedMovementLayout);
1051     return !isComposited();
1052 #else
1053     return true;
1054 #endif
1055 }
1056
1057 #if USE(ACCELERATED_COMPOSITING)
1058 RenderLayer* RenderLayer::enclosingCompositingLayer(bool includeSelf) const
1059 {
1060     if (includeSelf && isComposited())
1061         return const_cast<RenderLayer*>(this);
1062
1063     for (const RenderLayer* curr = compositingContainer(this); curr; curr = compositingContainer(curr)) {
1064         if (curr->isComposited())
1065             return const_cast<RenderLayer*>(curr);
1066     }
1067          
1068     return 0;
1069 }
1070
1071 RenderLayer* RenderLayer::enclosingCompositingLayerForRepaint(bool includeSelf) const
1072 {
1073     if (includeSelf && isComposited() && !backing()->paintsIntoCompositedAncestor())
1074         return const_cast<RenderLayer*>(this);
1075
1076     for (const RenderLayer* curr = compositingContainer(this); curr; curr = compositingContainer(curr)) {
1077         if (curr->isComposited() && !curr->backing()->paintsIntoCompositedAncestor())
1078             return const_cast<RenderLayer*>(curr);
1079     }
1080          
1081     return 0;
1082 }
1083 #endif
1084
1085 #if ENABLE(CSS_FILTERS)
1086 RenderLayer* RenderLayer::enclosingFilterLayer(bool includeSelf) const
1087 {
1088     const RenderLayer* curr = includeSelf ? this : parent();
1089     for (; curr; curr = curr->parent()) {
1090         if (curr->requiresFullLayerImageForFilters())
1091             return const_cast<RenderLayer*>(curr);
1092     }
1093     
1094     return 0;
1095 }
1096
1097 RenderLayer* RenderLayer::enclosingFilterRepaintLayer() const
1098 {
1099     for (const RenderLayer* curr = this; curr; curr = curr->parent()) {
1100         if ((curr != this && curr->requiresFullLayerImageForFilters()) || curr->isComposited() || curr->isRootLayer())
1101             return const_cast<RenderLayer*>(curr);
1102     }
1103     return 0;
1104 }
1105
1106 void RenderLayer::setFilterBackendNeedsRepaintingInRect(const LayoutRect& rect, bool immediate)
1107 {
1108     if (rect.isEmpty())
1109         return;
1110     
1111     LayoutRect rectForRepaint = rect;
1112     
1113 #if ENABLE(CSS_FILTERS)
1114     if (renderer()->style()->hasFilterOutsets()) {
1115         int topOutset;
1116         int rightOutset;
1117         int bottomOutset;
1118         int leftOutset;
1119         renderer()->style()->getFilterOutsets(topOutset, rightOutset, bottomOutset, leftOutset);
1120         rectForRepaint.move(-leftOutset, -topOutset);
1121         rectForRepaint.expand(leftOutset + rightOutset, topOutset + bottomOutset);
1122     }
1123 #endif
1124
1125     RenderLayerFilterInfo* filterInfo = this->filterInfo();
1126     ASSERT(filterInfo);
1127     filterInfo->expandDirtySourceRect(rectForRepaint);
1128     
1129 #if ENABLE(CSS_SHADERS)
1130     ASSERT(filterInfo->renderer());
1131     if (filterInfo->renderer()->hasCustomShaderFilter()) {
1132         // If we have at least one custom shader, we need to update the whole bounding box of the layer, because the
1133         // shader can address any ouput pixel.
1134         // Note: This is only for output rect, so there's no need to expand the dirty source rect.
1135         rectForRepaint.unite(calculateLayerBounds(this));
1136     }
1137 #endif
1138     
1139     RenderLayer* parentLayer = enclosingFilterRepaintLayer();
1140     ASSERT(parentLayer);
1141     FloatQuad repaintQuad(rectForRepaint);
1142     LayoutRect parentLayerRect = renderer()->localToContainerQuad(repaintQuad, parentLayer->renderer()).enclosingBoundingBox();
1143     
1144 #if USE(ACCELERATED_COMPOSITING)
1145     if (parentLayer->isComposited()) {
1146         if (!parentLayer->backing()->paintsIntoWindow()) {
1147             parentLayer->setBackingNeedsRepaintInRect(parentLayerRect);
1148             return;
1149         }
1150         // If the painting goes to window, redirect the painting to the parent RenderView.
1151         parentLayer = renderer()->view()->layer();
1152         parentLayerRect = renderer()->localToContainerQuad(repaintQuad, parentLayer->renderer()).enclosingBoundingBox();
1153     }
1154 #endif
1155
1156     if (parentLayer->paintsWithFilters()) {
1157         parentLayer->setFilterBackendNeedsRepaintingInRect(parentLayerRect, immediate);
1158         return;        
1159     }
1160     
1161     if (parentLayer->isRootLayer()) {
1162         RenderView* view = toRenderView(parentLayer->renderer());
1163         view->repaintViewRectangle(parentLayerRect, immediate);
1164         return;
1165     }
1166     
1167     ASSERT_NOT_REACHED();
1168 }
1169
1170 bool RenderLayer::hasAncestorWithFilterOutsets() const
1171 {
1172     for (const RenderLayer* curr = this; curr; curr = curr->parent()) {
1173         RenderLayerModelObject* renderer = curr->renderer();
1174         if (renderer->style()->hasFilterOutsets())
1175             return true;
1176     }
1177     return false;
1178 }
1179 #endif
1180     
1181 RenderLayer* RenderLayer::clippingRootForPainting() const
1182 {
1183 #if USE(ACCELERATED_COMPOSITING)
1184     if (isComposited())
1185         return const_cast<RenderLayer*>(this);
1186 #endif
1187
1188     const RenderLayer* current = this;
1189     while (current) {
1190         if (current->isRootLayer())
1191             return const_cast<RenderLayer*>(current);
1192
1193         current = compositingContainer(current);
1194         ASSERT(current);
1195         if (current->transform()
1196 #if USE(ACCELERATED_COMPOSITING)
1197             || (current->isComposited() && !current->backing()->paintsIntoCompositedAncestor())
1198 #endif
1199         )
1200             return const_cast<RenderLayer*>(current);
1201     }
1202
1203     ASSERT_NOT_REACHED();
1204     return 0;
1205 }
1206
1207 LayoutPoint RenderLayer::absoluteToContents(const LayoutPoint& absolutePoint) const
1208 {
1209     // We don't use convertToLayerCoords because it doesn't know about transforms
1210     return roundedLayoutPoint(renderer()->absoluteToLocal(absolutePoint, UseTransforms | SnapOffsetForTransforms));
1211 }
1212
1213 bool RenderLayer::cannotBlitToWindow() const
1214 {
1215     if (isTransparent() || hasReflection() || hasTransform())
1216         return true;
1217     if (!parent())
1218         return false;
1219     return parent()->cannotBlitToWindow();
1220 }
1221
1222 bool RenderLayer::isTransparent() const
1223 {
1224 #if ENABLE(SVG)
1225     if (renderer()->node() && renderer()->node()->namespaceURI() == SVGNames::svgNamespaceURI)
1226         return false;
1227 #endif
1228     return renderer()->isTransparent() || renderer()->hasMask();
1229 }
1230
1231 RenderLayer* RenderLayer::transparentPaintingAncestor()
1232 {
1233     if (isComposited())
1234         return 0;
1235
1236     for (RenderLayer* curr = parent(); curr; curr = curr->parent()) {
1237         if (curr->isComposited())
1238             return 0;
1239         if (curr->isTransparent())
1240             return curr;
1241     }
1242     return 0;
1243 }
1244
1245 static LayoutRect transparencyClipBox(const RenderLayer*, const RenderLayer* rootLayer, PaintBehavior);
1246
1247 static void expandClipRectForDescendantsAndReflection(LayoutRect& clipRect, const RenderLayer* layer, const RenderLayer* rootLayer, PaintBehavior paintBehavior)
1248 {
1249     // If we have a mask, then the clip is limited to the border box area (and there is
1250     // no need to examine child layers).
1251     if (!layer->renderer()->hasMask()) {
1252         // Note: we don't have to walk z-order lists since transparent elements always establish
1253         // a stacking context.  This means we can just walk the layer tree directly.
1254         for (RenderLayer* curr = layer->firstChild(); curr; curr = curr->nextSibling()) {
1255             if (!layer->reflection() || layer->reflectionLayer() != curr)
1256                 clipRect.unite(transparencyClipBox(curr, rootLayer, paintBehavior));
1257         }
1258     }
1259
1260     // If we have a reflection, then we need to account for that when we push the clip.  Reflect our entire
1261     // current transparencyClipBox to catch all child layers.
1262     // FIXME: Accelerated compositing will eventually want to do something smart here to avoid incorporating this
1263     // size into the parent layer.
1264     if (layer->renderer()->hasReflection()) {
1265         LayoutPoint delta;
1266         layer->convertToLayerCoords(rootLayer, delta);
1267         clipRect.move(-delta.x(), -delta.y());
1268         clipRect.unite(layer->renderBox()->reflectedRect(clipRect));
1269         clipRect.moveBy(delta);
1270     }
1271 }
1272
1273 static LayoutRect transparencyClipBox(const RenderLayer* layer, const RenderLayer* rootLayer, PaintBehavior paintBehavior)
1274 {
1275     // FIXME: Although this function completely ignores CSS-imposed clipping, we did already intersect with the
1276     // paintDirtyRect, and that should cut down on the amount we have to paint.  Still it
1277     // would be better to respect clips.
1278     
1279     if (rootLayer != layer && layer->paintsWithTransform(paintBehavior)) {
1280         // The best we can do here is to use enclosed bounding boxes to establish a "fuzzy" enough clip to encompass
1281         // the transformed layer and all of its children.
1282         LayoutPoint delta;
1283         layer->convertToLayerCoords(rootLayer, delta);
1284
1285         TransformationMatrix transform;
1286         transform.translate(delta.x(), delta.y());
1287         transform = transform * *layer->transform();
1288
1289         LayoutRect clipRect = layer->boundingBox(layer);
1290         expandClipRectForDescendantsAndReflection(clipRect, layer, layer, paintBehavior);
1291         return transform.mapRect(clipRect);
1292     }
1293     
1294     LayoutRect clipRect = layer->boundingBox(rootLayer);
1295     expandClipRectForDescendantsAndReflection(clipRect, layer, rootLayer, paintBehavior);
1296     return clipRect;
1297 }
1298
1299 LayoutRect RenderLayer::paintingExtent(const RenderLayer* rootLayer, const LayoutRect& paintDirtyRect, PaintBehavior paintBehavior)
1300 {
1301     return intersection(transparencyClipBox(this, rootLayer, paintBehavior), paintDirtyRect);
1302 }
1303
1304 void RenderLayer::beginTransparencyLayers(GraphicsContext* context, const RenderLayer* rootLayer, const LayoutRect& paintDirtyRect, PaintBehavior paintBehavior)
1305 {
1306     if (context->paintingDisabled() || (paintsWithTransparency(paintBehavior) && m_usedTransparency))
1307         return;
1308     
1309     RenderLayer* ancestor = transparentPaintingAncestor();
1310     if (ancestor)
1311         ancestor->beginTransparencyLayers(context, rootLayer, paintDirtyRect, paintBehavior);
1312     
1313     if (paintsWithTransparency(paintBehavior)) {
1314         m_usedTransparency = true;
1315         context->save();
1316         LayoutRect clipRect = paintingExtent(rootLayer, paintDirtyRect, paintBehavior);
1317         context->clip(clipRect);
1318         context->beginTransparencyLayer(renderer()->opacity());
1319 #ifdef REVEAL_TRANSPARENCY_LAYERS
1320         context->setFillColor(Color(0.0f, 0.0f, 0.5f, 0.2f), ColorSpaceDeviceRGB);
1321         context->fillRect(clipRect);
1322 #endif
1323     }
1324 }
1325
1326 void* RenderLayer::operator new(size_t sz, RenderArena* renderArena)
1327 {
1328     return renderArena->allocate(sz);
1329 }
1330
1331 void RenderLayer::operator delete(void* ptr, size_t sz)
1332 {
1333     // Stash size where destroy can find it.
1334     *(size_t *)ptr = sz;
1335 }
1336
1337 void RenderLayer::destroy(RenderArena* renderArena)
1338 {
1339     delete this;
1340
1341     // Recover the size left there for us by operator delete and free the memory.
1342     renderArena->free(*(size_t *)this, this);
1343 }
1344
1345 void RenderLayer::addChild(RenderLayer* child, RenderLayer* beforeChild)
1346 {
1347     RenderLayer* prevSibling = beforeChild ? beforeChild->previousSibling() : lastChild();
1348     if (prevSibling) {
1349         child->setPreviousSibling(prevSibling);
1350         prevSibling->setNextSibling(child);
1351         ASSERT(prevSibling != child);
1352     } else
1353         setFirstChild(child);
1354
1355     if (beforeChild) {
1356         beforeChild->setPreviousSibling(child);
1357         child->setNextSibling(beforeChild);
1358         ASSERT(beforeChild != child);
1359     } else
1360         setLastChild(child);
1361
1362     child->setParent(this);
1363
1364     if (child->isNormalFlowOnly())
1365         dirtyNormalFlowList();
1366
1367     if (!child->isNormalFlowOnly() || child->firstChild()) {
1368         // Dirty the z-order list in which we are contained.  The stackingContext() can be null in the
1369         // case where we're building up generated content layers.  This is ok, since the lists will start
1370         // off dirty in that case anyway.
1371         child->dirtyStackingContextZOrderLists();
1372     }
1373
1374     child->updateDescendantDependentFlags();
1375     if (child->m_hasVisibleContent || child->m_hasVisibleDescendant)
1376         setAncestorChainHasVisibleDescendant();
1377
1378     if (child->isSelfPaintingLayer() || child->hasSelfPaintingLayerDescendant())
1379         setAncestorChainHasSelfPaintingLayerDescendant();
1380
1381 #if USE(ACCELERATED_COMPOSITING)
1382     compositor()->layerWasAdded(this, child);
1383 #endif
1384 }
1385
1386 RenderLayer* RenderLayer::removeChild(RenderLayer* oldChild)
1387 {
1388 #if USE(ACCELERATED_COMPOSITING)
1389     if (!renderer()->documentBeingDestroyed())
1390         compositor()->layerWillBeRemoved(this, oldChild);
1391 #endif
1392
1393     // remove the child
1394     if (oldChild->previousSibling())
1395         oldChild->previousSibling()->setNextSibling(oldChild->nextSibling());
1396     if (oldChild->nextSibling())
1397         oldChild->nextSibling()->setPreviousSibling(oldChild->previousSibling());
1398
1399     if (m_first == oldChild)
1400         m_first = oldChild->nextSibling();
1401     if (m_last == oldChild)
1402         m_last = oldChild->previousSibling();
1403
1404     if (oldChild->isNormalFlowOnly())
1405         dirtyNormalFlowList();
1406     if (!oldChild->isNormalFlowOnly() || oldChild->firstChild()) { 
1407         // Dirty the z-order list in which we are contained.  When called via the
1408         // reattachment process in removeOnlyThisLayer, the layer may already be disconnected
1409         // from the main layer tree, so we need to null-check the |stackingContext| value.
1410         oldChild->dirtyStackingContextZOrderLists();
1411     }
1412
1413     oldChild->setPreviousSibling(0);
1414     oldChild->setNextSibling(0);
1415     oldChild->setParent(0);
1416     
1417     oldChild->updateDescendantDependentFlags();
1418     if (oldChild->m_hasVisibleContent || oldChild->m_hasVisibleDescendant)
1419         dirtyAncestorChainVisibleDescendantStatus();
1420
1421     if (oldChild->isSelfPaintingLayer() || oldChild->hasSelfPaintingLayerDescendant())
1422         dirtyAncestorChainHasSelfPaintingLayerDescendantStatus();
1423
1424     return oldChild;
1425 }
1426
1427 void RenderLayer::removeOnlyThisLayer()
1428 {
1429     if (!m_parent)
1430         return;
1431
1432     // Mark that we are about to lose our layer. This makes render tree
1433     // walks ignore this layer while we're removing it.
1434     m_renderer->setHasLayer(false);
1435
1436 #if USE(ACCELERATED_COMPOSITING)
1437     compositor()->layerWillBeRemoved(m_parent, this);
1438 #endif
1439
1440     // Dirty the clip rects.
1441     clearClipRectsIncludingDescendants();
1442
1443     RenderLayer* nextSib = nextSibling();
1444
1445     // Remove the child reflection layer before moving other child layers.
1446     // The reflection layer should not be moved to the parent.
1447     if (reflection())
1448         removeChild(reflectionLayer());
1449
1450     // Now walk our kids and reattach them to our parent.
1451     RenderLayer* current = m_first;
1452     while (current) {
1453         RenderLayer* next = current->nextSibling();
1454         removeChild(current);
1455         m_parent->addChild(current, nextSib);
1456         current->setRepaintStatus(NeedsFullRepaint);
1457         // updateLayerPositions depends on hasLayer() already being false for proper layout.
1458         ASSERT(!renderer()->hasLayer());
1459         current->updateLayerPositions(0); // FIXME: use geometry map.
1460         current = next;
1461     }
1462
1463     // Remove us from the parent.
1464     m_parent->removeChild(this);
1465     m_renderer->destroyLayer();
1466 }
1467
1468 void RenderLayer::insertOnlyThisLayer()
1469 {
1470     if (!m_parent && renderer()->parent()) {
1471         // We need to connect ourselves when our renderer() has a parent.
1472         // Find our enclosingLayer and add ourselves.
1473         RenderLayer* parentLayer = renderer()->parent()->enclosingLayer();
1474         ASSERT(parentLayer);
1475         RenderLayer* beforeChild = parentLayer->reflectionLayer() != this ? renderer()->parent()->findNextLayer(parentLayer, renderer()) : 0;
1476         parentLayer->addChild(this, beforeChild);
1477     }
1478
1479     // Remove all descendant layers from the hierarchy and add them to the new position.
1480     for (RenderObject* curr = renderer()->firstChild(); curr; curr = curr->nextSibling())
1481         curr->moveLayers(m_parent, this);
1482
1483     // Clear out all the clip rects.
1484     clearClipRectsIncludingDescendants();
1485 }
1486
1487 void RenderLayer::convertToPixelSnappedLayerCoords(const RenderLayer* ancestorLayer, IntPoint& roundedLocation) const
1488 {
1489     LayoutPoint location = roundedLocation;
1490     convertToLayerCoords(ancestorLayer, location);
1491     roundedLocation = roundedIntPoint(location);
1492 }
1493
1494 void RenderLayer::convertToPixelSnappedLayerCoords(const RenderLayer* ancestorLayer, IntRect& roundedRect) const
1495 {
1496     LayoutRect rect = roundedRect;
1497     convertToLayerCoords(ancestorLayer, rect);
1498     roundedRect = pixelSnappedIntRect(rect);
1499 }
1500
1501 // Returns the layer reached on the walk up towards the ancestor.
1502 static inline const RenderLayer* accumulateOffsetTowardsAncestor(const RenderLayer* layer, const RenderLayer* ancestorLayer, LayoutPoint& location)
1503 {
1504     ASSERT(ancestorLayer != layer);
1505
1506     const RenderLayerModelObject* renderer = layer->renderer();
1507     EPosition position = renderer->style()->position();
1508
1509     // FIXME: Positioning of out-of-flow(fixed, absolute) elements collected in a RenderFlowThread
1510     // may need to be revisited in a future patch.
1511     // If the fixed renderer is inside a RenderFlowThread, we should not compute location using localToAbsolute,
1512     // since localToAbsolute maps the coordinates from named flow to regions coordinates and regions can be
1513     // positioned in a completely different place in the viewport (RenderView).
1514     if (position == FixedPosition && !renderer->inRenderFlowThread() && (!ancestorLayer || ancestorLayer == renderer->view()->layer())) {
1515         // If the fixed layer's container is the root, just add in the offset of the view. We can obtain this by calling
1516         // localToAbsolute() on the RenderView.
1517         FloatPoint absPos = renderer->localToAbsolute(FloatPoint(), IsFixed);
1518         location += LayoutSize(absPos.x(), absPos.y());
1519         return ancestorLayer;
1520     }
1521
1522     // For the fixed positioned elements inside a render flow thread, we should also skip the code path below
1523     // Otherwise, for the case of ancestorLayer == rootLayer and fixed positioned element child of a transformed
1524     // element in render flow thread, we will hit the fixed positioned container before hitting the ancestor layer.
1525     if (position == FixedPosition && !renderer->inRenderFlowThread()) {
1526         // For a fixed layers, we need to walk up to the root to see if there's a fixed position container
1527         // (e.g. a transformed layer). It's an error to call convertToLayerCoords() across a layer with a transform,
1528         // so we should always find the ancestor at or before we find the fixed position container.
1529         RenderLayer* fixedPositionContainerLayer = 0;
1530         bool foundAncestor = false;
1531         for (RenderLayer* currLayer = layer->parent(); currLayer; currLayer = currLayer->parent()) {
1532             if (currLayer == ancestorLayer)
1533                 foundAncestor = true;
1534
1535             if (isFixedPositionedContainer(currLayer)) {
1536                 fixedPositionContainerLayer = currLayer;
1537                 ASSERT_UNUSED(foundAncestor, foundAncestor);
1538                 break;
1539             }
1540         }
1541         
1542         ASSERT(fixedPositionContainerLayer); // We should have hit the RenderView's layer at least.
1543
1544         if (fixedPositionContainerLayer != ancestorLayer) {
1545             LayoutPoint fixedContainerCoords;
1546             layer->convertToLayerCoords(fixedPositionContainerLayer, fixedContainerCoords);
1547
1548             LayoutPoint ancestorCoords;
1549             ancestorLayer->convertToLayerCoords(fixedPositionContainerLayer, ancestorCoords);
1550
1551             location += (fixedContainerCoords - ancestorCoords);
1552             return ancestorLayer;
1553         }
1554     }
1555     
1556     RenderLayer* parentLayer;
1557     if (position == AbsolutePosition || position == FixedPosition) {
1558         // Do what enclosingPositionedAncestor() does, but check for ancestorLayer along the way.
1559         parentLayer = layer->parent();
1560         bool foundAncestorFirst = false;
1561         while (parentLayer) {
1562             // RenderFlowThread is a positioned container, child of RenderView, positioned at (0,0).
1563             // This implies that, for out-of-flow positioned elements inside a RenderFlowThread,
1564             // we are bailing out before reaching root layer.
1565             if (isPositionedContainer(parentLayer))
1566                 break;
1567
1568             if (parentLayer == ancestorLayer) {
1569                 foundAncestorFirst = true;
1570                 break;
1571             }
1572
1573             parentLayer = parentLayer->parent();
1574         }
1575
1576         // We should not reach RenderView layer past the RenderFlowThread layer for any
1577         // children of the RenderFlowThread.
1578         if (renderer->inRenderFlowThread() && !renderer->isRenderFlowThread())
1579             ASSERT(parentLayer != renderer->view()->layer());
1580
1581         if (foundAncestorFirst) {
1582             // Found ancestorLayer before the abs. positioned container, so compute offset of both relative
1583             // to enclosingPositionedAncestor and subtract.
1584             RenderLayer* positionedAncestor = parentLayer->enclosingPositionedAncestor();
1585
1586             LayoutPoint thisCoords;
1587             layer->convertToLayerCoords(positionedAncestor, thisCoords);
1588             
1589             LayoutPoint ancestorCoords;
1590             ancestorLayer->convertToLayerCoords(positionedAncestor, ancestorCoords);
1591
1592             location += (thisCoords - ancestorCoords);
1593             return ancestorLayer;
1594         }
1595     } else
1596         parentLayer = layer->parent();
1597     
1598     if (!parentLayer)
1599         return 0;
1600
1601     location += toSize(layer->location());
1602     return parentLayer;
1603 }
1604
1605 void RenderLayer::convertToLayerCoords(const RenderLayer* ancestorLayer, LayoutPoint& location) const
1606 {
1607     if (ancestorLayer == this)
1608         return;
1609
1610     const RenderLayer* currLayer = this;
1611     while (currLayer && currLayer != ancestorLayer)
1612         currLayer = accumulateOffsetTowardsAncestor(currLayer, ancestorLayer, location);
1613 }
1614
1615 void RenderLayer::convertToLayerCoords(const RenderLayer* ancestorLayer, LayoutRect& rect) const
1616 {
1617     LayoutPoint delta;
1618     convertToLayerCoords(ancestorLayer, delta);
1619     rect.move(-delta.x(), -delta.y());
1620 }
1621
1622 #if USE(ACCELERATED_COMPOSITING)
1623 bool RenderLayer::usesCompositedScrolling() const
1624 {
1625     if (!scrollsOverflow() || !allowsScrolling())
1626         return false;
1627
1628 #if ENABLE(ACCELERATED_OVERFLOW_SCROLLING)
1629     return renderer()->style()->useTouchOverflowScrolling();
1630 #else
1631     return false;
1632 #endif
1633 }
1634 #endif
1635
1636 static inline int adjustedScrollDelta(int beginningDelta) {
1637     // This implemention matches Firefox's.
1638     // http://mxr.mozilla.org/firefox/source/toolkit/content/widgets/browser.xml#856.
1639     const int speedReducer = 12;
1640
1641     int adjustedDelta = beginningDelta / speedReducer;
1642     if (adjustedDelta > 1)
1643         adjustedDelta = static_cast<int>(adjustedDelta * sqrt(static_cast<double>(adjustedDelta))) - 1;
1644     else if (adjustedDelta < -1)
1645         adjustedDelta = static_cast<int>(adjustedDelta * sqrt(static_cast<double>(-adjustedDelta))) + 1;
1646
1647     return adjustedDelta;
1648 }
1649
1650 static inline IntSize adjustedScrollDelta(const IntSize& delta)
1651 {
1652     return IntSize(adjustedScrollDelta(delta.width()), adjustedScrollDelta(delta.height()));
1653 }
1654
1655 void RenderLayer::panScrollFromPoint(const IntPoint& sourcePoint)
1656 {
1657     Frame* frame = renderer()->frame();
1658     if (!frame)
1659         return;
1660     
1661     IntPoint currentMousePosition = frame->eventHandler()->currentMousePosition();
1662     
1663     // We need to check if the current mouse position is out of the window. When the mouse is out of the window, the position is incoherent
1664     static IntPoint previousMousePosition;
1665     if (currentMousePosition.x() < 0 || currentMousePosition.y() < 0)
1666         currentMousePosition = previousMousePosition;
1667     else
1668         previousMousePosition = currentMousePosition;
1669
1670     IntSize delta = currentMousePosition - sourcePoint;
1671
1672     if (abs(delta.width()) <= ScrollView::noPanScrollRadius) // at the center we let the space for the icon
1673         delta.setWidth(0);
1674     if (abs(delta.height()) <= ScrollView::noPanScrollRadius)
1675         delta.setHeight(0);
1676
1677     scrollByRecursively(adjustedScrollDelta(delta), ScrollOffsetClamped);
1678 }
1679
1680 void RenderLayer::scrollByRecursively(const IntSize& delta, ScrollOffsetClamping clamp)
1681 {
1682     if (delta.isZero())
1683         return;
1684
1685     bool restrictedByLineClamp = false;
1686     if (renderer()->parent())
1687         restrictedByLineClamp = !renderer()->parent()->style()->lineClamp().isNone();
1688
1689     if (renderer()->hasOverflowClip() && !restrictedByLineClamp) {
1690         IntSize newScrollOffset = scrollOffset() + delta;
1691         scrollToOffset(newScrollOffset, clamp);
1692
1693         // If this layer can't do the scroll we ask the next layer up that can scroll to try
1694         IntSize remainingScrollOffset = newScrollOffset - scrollOffset();
1695         if (!remainingScrollOffset.isZero() && renderer()->parent()) {
1696             if (RenderLayer* scrollableLayer = enclosingScrollableLayer())
1697                 scrollableLayer->scrollByRecursively(remainingScrollOffset);
1698
1699             Frame* frame = renderer()->frame();
1700             if (frame)
1701                 frame->eventHandler()->updateAutoscrollRenderer();
1702         }
1703     } else if (renderer()->view()->frameView()) {
1704         // If we are here, we were called on a renderer that can be programmatically scrolled, but doesn't
1705         // have an overflow clip. Which means that it is a document node that can be scrolled.
1706         renderer()->view()->frameView()->scrollBy(delta);
1707         // FIXME: If we didn't scroll the whole way, do we want to try looking at the frames ownerElement? 
1708         // https://bugs.webkit.org/show_bug.cgi?id=28237
1709     }
1710 }
1711
1712 IntSize RenderLayer::clampScrollOffset(const IntSize& scrollOffset) const
1713 {
1714     RenderBox* box = renderBox();
1715     ASSERT(box);
1716
1717     int maxX = scrollWidth() - box->pixelSnappedClientWidth();
1718     int maxY = scrollHeight() - box->pixelSnappedClientHeight();
1719
1720     int x = max(min(scrollOffset.width(), maxX), 0);
1721     int y = max(min(scrollOffset.height(), maxY), 0);
1722     return IntSize(x, y);
1723 }
1724
1725 void RenderLayer::scrollToOffset(const IntSize& scrollOffset, ScrollOffsetClamping clamp)
1726 {
1727     IntSize newScrollOffset = clamp == ScrollOffsetClamped ? clampScrollOffset(scrollOffset) : scrollOffset;
1728     if (newScrollOffset != this->scrollOffset())
1729         scrollToOffsetWithoutAnimation(toPoint(newScrollOffset));
1730 }
1731
1732 void RenderLayer::scrollTo(int x, int y)
1733 {
1734     RenderBox* box = renderBox();
1735     if (!box)
1736         return;
1737
1738     if (box->style()->overflowX() != OMARQUEE) {
1739         // Ensure that the dimensions will be computed if they need to be (for overflow:hidden blocks).
1740         if (m_scrollDimensionsDirty)
1741             computeScrollDimensions();
1742     }
1743     
1744     // FIXME: Eventually, we will want to perform a blit.  For now never
1745     // blit, since the check for blitting is going to be very
1746     // complicated (since it will involve testing whether our layer
1747     // is either occluded by another layer or clipped by an enclosing
1748     // layer or contains fixed backgrounds, etc.).
1749     IntSize newScrollOffset = IntSize(x - scrollOrigin().x(), y - scrollOrigin().y());
1750     if (m_scrollOffset == newScrollOffset)
1751         return;
1752     m_scrollOffset = newScrollOffset;
1753
1754     Frame* frame = renderer()->frame();
1755     InspectorInstrumentation::willScrollLayer(frame);
1756
1757     RenderView* view = renderer()->view();
1758     
1759     // We should have a RenderView if we're trying to scroll.
1760     ASSERT(view);
1761
1762     // Update the positions of our child layers (if needed as only fixed layers should be impacted by a scroll).
1763     // We don't update compositing layers, because we need to do a deep update from the compositing ancestor.
1764     bool inLayout = view ? view->frameView()->isInLayout() : false;
1765     if (!inLayout) {
1766         // If we're in the middle of layout, we'll just update layers once layout has finished.
1767         updateLayerPositionsAfterOverflowScroll();
1768         if (view) {
1769             // Update regions, scrolling may change the clip of a particular region.
1770 #if ENABLE(DASHBOARD_SUPPORT) || ENABLE(DRAGGABLE_REGION)
1771             view->frameView()->updateAnnotatedRegions();
1772 #endif
1773             view->updateWidgetPositions();
1774         }
1775
1776         if (!m_updatingMarqueePosition) {
1777             // Avoid updating compositing layers if, higher on the stack, we're already updating layer
1778             // positions. Updating layer positions requires a full walk of up-to-date RenderLayers, and
1779             // in this case we're still updating their positions; we'll update compositing layers later
1780             // when that completes.
1781             updateCompositingLayersAfterScroll();
1782         }
1783     }
1784
1785     RenderLayerModelObject* repaintContainer = renderer()->containerForRepaint();
1786     if (frame) {
1787         // The caret rect needs to be invalidated after scrolling
1788         frame->selection()->setCaretRectNeedsUpdate();
1789
1790         FloatQuad quadForFakeMouseMoveEvent = FloatQuad(m_repaintRect);
1791         if (repaintContainer)
1792             quadForFakeMouseMoveEvent = repaintContainer->localToAbsoluteQuad(quadForFakeMouseMoveEvent, SnapOffsetForTransforms);
1793         frame->eventHandler()->dispatchFakeMouseMoveEventSoonInQuad(quadForFakeMouseMoveEvent);
1794     }
1795
1796     // Just schedule a full repaint of our object.
1797     if (view && !usesCompositedScrolling())
1798         renderer()->repaintUsingContainer(repaintContainer, pixelSnappedIntRect(m_repaintRect));
1799
1800     // Schedule the scroll DOM event.
1801     if (renderer()->node())
1802         renderer()->node()->document()->eventQueue()->enqueueOrDispatchScrollEvent(renderer()->node(), DocumentEventQueue::ScrollEventElementTarget);
1803
1804     InspectorInstrumentation::didScrollLayer(frame);
1805 }
1806
1807 static inline bool frameElementAndViewPermitScroll(HTMLFrameElement* frameElement, FrameView* frameView) 
1808 {
1809     // If scrollbars aren't explicitly forbidden, permit scrolling.
1810     if (frameElement && frameElement->scrollingMode() != ScrollbarAlwaysOff)
1811         return true;
1812
1813     // If scrollbars are forbidden, user initiated scrolls should obviously be ignored.
1814     if (frameView->wasScrolledByUser())
1815         return false;
1816
1817     // Forbid autoscrolls when scrollbars are off, but permits other programmatic scrolls,
1818     // like navigation to an anchor.
1819     return !frameView->frame()->eventHandler()->autoscrollInProgress();
1820 }
1821
1822 void RenderLayer::scrollRectToVisible(const LayoutRect& rect, const ScrollAlignment& alignX, const ScrollAlignment& alignY)
1823 {
1824     RenderLayer* parentLayer = 0;
1825     LayoutRect newRect = rect;
1826
1827     // We may end up propagating a scroll event. It is important that we suspend events until 
1828     // the end of the function since they could delete the layer or the layer's renderer().
1829     FrameView* frameView = renderer()->document()->view();
1830     if (frameView)
1831         frameView->pauseScheduledEvents();
1832
1833     bool restrictedByLineClamp = false;
1834     if (renderer()->parent()) {
1835         parentLayer = renderer()->parent()->enclosingLayer();
1836         restrictedByLineClamp = !renderer()->parent()->style()->lineClamp().isNone();
1837     }
1838
1839     if (renderer()->hasOverflowClip() && !restrictedByLineClamp) {
1840         // Don't scroll to reveal an overflow layer that is restricted by the -webkit-line-clamp property.
1841         // This will prevent us from revealing text hidden by the slider in Safari RSS.
1842         RenderBox* box = renderBox();
1843         ASSERT(box);
1844         FloatPoint absPos = box->localToAbsolute();
1845         absPos.move(box->borderLeft(), box->borderTop());
1846
1847         LayoutRect layerBounds = LayoutRect(absPos.x() + scrollXOffset(), absPos.y() + scrollYOffset(), box->clientWidth(), box->clientHeight());
1848         LayoutRect exposeRect = LayoutRect(rect.x() + scrollXOffset(), rect.y() + scrollYOffset(), rect.width(), rect.height());
1849         LayoutRect r = getRectToExpose(layerBounds, exposeRect, alignX, alignY);
1850         
1851         int roundedAdjustedX = roundToInt(r.x() - absPos.x());
1852         int roundedAdjustedY = roundToInt(r.y() - absPos.y());
1853         IntSize clampedScrollOffset = clampScrollOffset(IntSize(roundedAdjustedX, roundedAdjustedY));
1854         if (clampedScrollOffset != scrollOffset()) {
1855             IntSize oldScrollOffset = scrollOffset();
1856             scrollToOffset(clampedScrollOffset);
1857             IntSize scrollOffsetDifference = scrollOffset() - oldScrollOffset;
1858             newRect.move(-scrollOffsetDifference);
1859         }
1860     } else if (!parentLayer && renderer()->isBox() && renderBox()->canBeProgramaticallyScrolled()) {
1861         if (frameView) {
1862             Element* ownerElement = 0;
1863             if (renderer()->document())
1864                 ownerElement = renderer()->document()->ownerElement();
1865
1866             if (ownerElement && ownerElement->renderer()) {
1867                 HTMLFrameElement* frameElement = 0;
1868
1869                 if (ownerElement->hasTagName(frameTag) || ownerElement->hasTagName(iframeTag))
1870                     frameElement = static_cast<HTMLFrameElement*>(ownerElement);
1871
1872                 if (frameElementAndViewPermitScroll(frameElement, frameView)) {
1873                     LayoutRect viewRect = frameView->visibleContentRect();
1874                     LayoutRect exposeRect = getRectToExpose(viewRect, rect, alignX, alignY);
1875
1876                     int xOffset = roundToInt(exposeRect.x());
1877                     int yOffset = roundToInt(exposeRect.y());
1878                     // Adjust offsets if they're outside of the allowable range.
1879                     xOffset = max(0, min(frameView->contentsWidth(), xOffset));
1880                     yOffset = max(0, min(frameView->contentsHeight(), yOffset));
1881
1882                     frameView->setScrollPosition(IntPoint(xOffset, yOffset));
1883                     if (frameView->safeToPropagateScrollToParent()) {
1884                         parentLayer = ownerElement->renderer()->enclosingLayer();
1885                         newRect.setX(rect.x() - frameView->scrollX() + frameView->x());
1886                         newRect.setY(rect.y() - frameView->scrollY() + frameView->y());
1887                     } else
1888                         parentLayer = 0;
1889                 }
1890             } else {
1891                 LayoutRect viewRect = frameView->visibleContentRect();
1892                 LayoutRect r = getRectToExpose(viewRect, rect, alignX, alignY);
1893                 
1894                 frameView->setScrollPosition(roundedIntPoint(r.location()));
1895
1896                 // This is the outermost view of a web page, so after scrolling this view we
1897                 // scroll its container by calling Page::scrollRectIntoView.
1898                 // This only has an effect on the Mac platform in applications
1899                 // that put web views into scrolling containers, such as Mac OS X Mail.
1900                 // The canAutoscroll function in EventHandler also knows about this.
1901                 if (Frame* frame = frameView->frame()) {
1902                     if (Page* page = frame->page())
1903                         page->chrome()->scrollRectIntoView(pixelSnappedIntRect(rect));
1904                 }
1905             }
1906         }
1907     }
1908     
1909     if (parentLayer)
1910         parentLayer->scrollRectToVisible(newRect, alignX, alignY);
1911
1912     if (frameView)
1913         frameView->resumeScheduledEvents();
1914 }
1915
1916 void RenderLayer::updateCompositingLayersAfterScroll()
1917 {
1918 #if USE(ACCELERATED_COMPOSITING)
1919     if (compositor()->inCompositingMode()) {
1920         // Our stacking context is guaranteed to contain all of our descendants that may need
1921         // repositioning, so update compositing layers from there.
1922         if (RenderLayer* compositingAncestor = stackingContext()->enclosingCompositingLayer()) {
1923             if (compositor()->compositingConsultsOverlap())
1924                 compositor()->updateCompositingLayers(CompositingUpdateOnScroll, compositingAncestor);
1925             else {
1926                 bool isUpdateRoot = true;
1927                 compositingAncestor->backing()->updateAfterLayout(RenderLayerBacking::AllDescendants, isUpdateRoot);
1928             }
1929         }
1930     }
1931 #endif
1932 }
1933
1934 LayoutRect RenderLayer::getRectToExpose(const LayoutRect &visibleRect, const LayoutRect &exposeRect, const ScrollAlignment& alignX, const ScrollAlignment& alignY)
1935 {
1936     // Determine the appropriate X behavior.
1937     ScrollBehavior scrollX;
1938     LayoutRect exposeRectX(exposeRect.x(), visibleRect.y(), exposeRect.width(), visibleRect.height());
1939     LayoutUnit intersectWidth = intersection(visibleRect, exposeRectX).width();
1940     if (intersectWidth == exposeRect.width() || intersectWidth >= MIN_INTERSECT_FOR_REVEAL)
1941         // If the rectangle is fully visible, use the specified visible behavior.
1942         // If the rectangle is partially visible, but over a certain threshold,
1943         // then treat it as fully visible to avoid unnecessary horizontal scrolling
1944         scrollX = ScrollAlignment::getVisibleBehavior(alignX);
1945     else if (intersectWidth == visibleRect.width()) {
1946         // If the rect is bigger than the visible area, don't bother trying to center. Other alignments will work.
1947         scrollX = ScrollAlignment::getVisibleBehavior(alignX);
1948         if (scrollX == alignCenter)
1949             scrollX = noScroll;
1950     } else if (intersectWidth > 0)
1951         // If the rectangle is partially visible, but not above the minimum threshold, use the specified partial behavior
1952         scrollX = ScrollAlignment::getPartialBehavior(alignX);
1953     else
1954         scrollX = ScrollAlignment::getHiddenBehavior(alignX);
1955     // If we're trying to align to the closest edge, and the exposeRect is further right
1956     // than the visibleRect, and not bigger than the visible area, then align with the right.
1957     if (scrollX == alignToClosestEdge && exposeRect.maxX() > visibleRect.maxX() && exposeRect.width() < visibleRect.width())
1958         scrollX = alignRight;
1959
1960     // Given the X behavior, compute the X coordinate.
1961     LayoutUnit x;
1962     if (scrollX == noScroll) 
1963         x = visibleRect.x();
1964     else if (scrollX == alignRight)
1965         x = exposeRect.maxX() - visibleRect.width();
1966     else if (scrollX == alignCenter)
1967         x = exposeRect.x() + (exposeRect.width() - visibleRect.width()) / 2;
1968     else
1969         x = exposeRect.x();
1970
1971     // Determine the appropriate Y behavior.
1972     ScrollBehavior scrollY;
1973     LayoutRect exposeRectY(visibleRect.x(), exposeRect.y(), visibleRect.width(), exposeRect.height());
1974     LayoutUnit intersectHeight = intersection(visibleRect, exposeRectY).height();
1975     if (intersectHeight == exposeRect.height())
1976         // If the rectangle is fully visible, use the specified visible behavior.
1977         scrollY = ScrollAlignment::getVisibleBehavior(alignY);
1978     else if (intersectHeight == visibleRect.height()) {
1979         // If the rect is bigger than the visible area, don't bother trying to center. Other alignments will work.
1980         scrollY = ScrollAlignment::getVisibleBehavior(alignY);
1981         if (scrollY == alignCenter)
1982             scrollY = noScroll;
1983     } else if (intersectHeight > 0)
1984         // If the rectangle is partially visible, use the specified partial behavior
1985         scrollY = ScrollAlignment::getPartialBehavior(alignY);
1986     else
1987         scrollY = ScrollAlignment::getHiddenBehavior(alignY);
1988     // If we're trying to align to the closest edge, and the exposeRect is further down
1989     // than the visibleRect, and not bigger than the visible area, then align with the bottom.
1990     if (scrollY == alignToClosestEdge && exposeRect.maxY() > visibleRect.maxY() && exposeRect.height() < visibleRect.height())
1991         scrollY = alignBottom;
1992
1993     // Given the Y behavior, compute the Y coordinate.
1994     LayoutUnit y;
1995     if (scrollY == noScroll) 
1996         y = visibleRect.y();
1997     else if (scrollY == alignBottom)
1998         y = exposeRect.maxY() - visibleRect.height();
1999     else if (scrollY == alignCenter)
2000         y = exposeRect.y() + (exposeRect.height() - visibleRect.height()) / 2;
2001     else
2002         y = exposeRect.y();
2003
2004     return LayoutRect(LayoutPoint(x, y), visibleRect.size());
2005 }
2006
2007 void RenderLayer::autoscroll()
2008 {
2009     Frame* frame = renderer()->frame();
2010     if (!frame)
2011         return;
2012
2013     FrameView* frameView = frame->view();
2014     if (!frameView)
2015         return;
2016
2017 #if ENABLE(DRAG_SUPPORT)
2018     frame->eventHandler()->updateSelectionForMouseDrag();
2019 #endif
2020
2021     IntPoint currentDocumentPosition = frameView->windowToContents(frame->eventHandler()->currentMousePosition());
2022     scrollRectToVisible(LayoutRect(currentDocumentPosition, LayoutSize(1, 1)), ScrollAlignment::alignToEdgeIfNeeded, ScrollAlignment::alignToEdgeIfNeeded);
2023 }
2024
2025 void RenderLayer::resize(const PlatformMouseEvent& evt, const LayoutSize& oldOffset)
2026 {
2027     // FIXME: This should be possible on generated content but is not right now.
2028     if (!inResizeMode() || !renderer()->hasOverflowClip() || !renderer()->node())
2029         return;
2030
2031     ASSERT(renderer()->node()->isElementNode());
2032     Element* element = static_cast<Element*>(renderer()->node());
2033     RenderBox* renderer = toRenderBox(element->renderer());
2034
2035     EResize resize = renderer->style()->resize();
2036     if (resize == RESIZE_NONE)
2037         return;
2038
2039     Document* document = element->document();
2040     if (!document->frame()->eventHandler()->mousePressed())
2041         return;
2042
2043     float zoomFactor = renderer->style()->effectiveZoom();
2044
2045     LayoutSize newOffset = offsetFromResizeCorner(document->view()->windowToContents(evt.position()));
2046     newOffset.setWidth(newOffset.width() / zoomFactor);
2047     newOffset.setHeight(newOffset.height() / zoomFactor);
2048     
2049     LayoutSize currentSize = LayoutSize(renderer->width() / zoomFactor, renderer->height() / zoomFactor);
2050     LayoutSize minimumSize = element->minimumSizeForResizing().shrunkTo(currentSize);
2051     element->setMinimumSizeForResizing(minimumSize);
2052     
2053     LayoutSize adjustedOldOffset = LayoutSize(oldOffset.width() / zoomFactor, oldOffset.height() / zoomFactor);
2054     if (renderer->style()->shouldPlaceBlockDirectionScrollbarOnLogicalLeft()) {
2055         newOffset.setWidth(-newOffset.width());
2056         adjustedOldOffset.setWidth(-adjustedOldOffset.width());
2057     }
2058     
2059     LayoutSize difference = (currentSize + newOffset - adjustedOldOffset).expandedTo(minimumSize) - currentSize;
2060
2061     ASSERT(element->isStyledElement());
2062     StyledElement* styledElement = static_cast<StyledElement*>(element);
2063     bool isBoxSizingBorder = renderer->style()->boxSizing() == BORDER_BOX;
2064
2065     if (resize != RESIZE_VERTICAL && difference.width()) {
2066         if (element->isFormControlElement()) {
2067             // Make implicit margins from the theme explicit (see <http://bugs.webkit.org/show_bug.cgi?id=9547>).
2068             styledElement->setInlineStyleProperty(CSSPropertyMarginLeft, String::number(renderer->marginLeft() / zoomFactor) + "px", false);
2069             styledElement->setInlineStyleProperty(CSSPropertyMarginRight, String::number(renderer->marginRight() / zoomFactor) + "px", false);
2070         }
2071         LayoutUnit baseWidth = renderer->width() - (isBoxSizingBorder ? LayoutUnit() : renderer->borderAndPaddingWidth());
2072         baseWidth = baseWidth / zoomFactor;
2073         styledElement->setInlineStyleProperty(CSSPropertyWidth, String::number(roundToInt(baseWidth + difference.width())) + "px", false);
2074     }
2075
2076     if (resize != RESIZE_HORIZONTAL && difference.height()) {
2077         if (element->isFormControlElement()) {
2078             // Make implicit margins from the theme explicit (see <http://bugs.webkit.org/show_bug.cgi?id=9547>).
2079             styledElement->setInlineStyleProperty(CSSPropertyMarginTop, String::number(renderer->marginTop() / zoomFactor) + "px", false);
2080             styledElement->setInlineStyleProperty(CSSPropertyMarginBottom, String::number(renderer->marginBottom() / zoomFactor) + "px", false);
2081         }
2082         LayoutUnit baseHeight = renderer->height() - (isBoxSizingBorder ? LayoutUnit() : renderer->borderAndPaddingHeight());
2083         baseHeight = baseHeight / zoomFactor;
2084         styledElement->setInlineStyleProperty(CSSPropertyHeight, String::number(roundToInt(baseHeight + difference.height())) + "px", false);
2085     }
2086
2087     document->updateLayout();
2088
2089     // FIXME (Radar 4118564): We should also autoscroll the window as necessary to keep the point under the cursor in view.
2090 }
2091
2092 int RenderLayer::scrollSize(ScrollbarOrientation orientation) const
2093 {
2094     Scrollbar* scrollbar = ((orientation == HorizontalScrollbar) ? m_hBar : m_vBar).get();
2095     return scrollbar ? (scrollbar->totalSize() - scrollbar->visibleSize()) : 0;
2096 }
2097
2098 void RenderLayer::setScrollOffset(const IntPoint& offset)
2099 {
2100     scrollTo(offset.x(), offset.y());
2101 }
2102
2103 int RenderLayer::scrollPosition(Scrollbar* scrollbar) const
2104 {
2105     if (scrollbar->orientation() == HorizontalScrollbar)
2106         return scrollXOffset();
2107     if (scrollbar->orientation() == VerticalScrollbar)
2108         return scrollYOffset();
2109     return 0;
2110 }
2111
2112 IntPoint RenderLayer::scrollPosition() const
2113 {
2114     return scrollOrigin() + m_scrollOffset;
2115 }
2116
2117 IntPoint RenderLayer::minimumScrollPosition() const
2118 {
2119     return scrollOrigin();
2120 }
2121
2122 IntPoint RenderLayer::maximumScrollPosition() const
2123 {
2124     // FIXME: m_scrollSize may not be up-to-date if m_scrollDimensionsDirty is true.
2125     return scrollOrigin() + roundedIntSize(m_scrollSize) - visibleContentRect(true).size();
2126 }
2127
2128 IntRect RenderLayer::visibleContentRect(bool includeScrollbars) const
2129 {
2130     int verticalScrollbarWidth = 0;
2131     int horizontalScrollbarHeight = 0;
2132     if (includeScrollbars) {
2133         verticalScrollbarWidth = (verticalScrollbar() && !verticalScrollbar()->isOverlayScrollbar()) ? verticalScrollbar()->width() : 0;
2134         horizontalScrollbarHeight = (horizontalScrollbar() && !horizontalScrollbar()->isOverlayScrollbar()) ? horizontalScrollbar()->height() : 0;
2135     }
2136     
2137     return IntRect(IntPoint(scrollXOffset(), scrollYOffset()),
2138                    IntSize(max(0, m_layerSize.width() - verticalScrollbarWidth), 
2139                            max(0, m_layerSize.height() - horizontalScrollbarHeight)));
2140 }
2141
2142 IntSize RenderLayer::overhangAmount() const
2143 {
2144     return IntSize();
2145 }
2146
2147 bool RenderLayer::isActive() const
2148 {
2149     Page* page = renderer()->frame()->page();
2150     return page && page->focusController()->isActive();
2151 }
2152
2153 static int cornerStart(const RenderLayer* layer, int minX, int maxX, int thickness)
2154 {
2155     if (layer->renderer()->style()->shouldPlaceBlockDirectionScrollbarOnLogicalLeft())
2156         return minX + layer->renderer()->style()->borderLeftWidth();
2157     return maxX - thickness - layer->renderer()->style()->borderRightWidth();
2158 }
2159
2160 static IntRect cornerRect(const RenderLayer* layer, const IntRect& bounds)
2161 {
2162     int horizontalThickness;
2163     int verticalThickness;
2164     if (!layer->verticalScrollbar() && !layer->horizontalScrollbar()) {
2165         // FIXME: This isn't right.  We need to know the thickness of custom scrollbars
2166         // even when they don't exist in order to set the resizer square size properly.
2167         horizontalThickness = ScrollbarTheme::theme()->scrollbarThickness();
2168         verticalThickness = horizontalThickness;
2169     } else if (layer->verticalScrollbar() && !layer->horizontalScrollbar()) {
2170         horizontalThickness = layer->verticalScrollbar()->width();
2171         verticalThickness = horizontalThickness;
2172     } else if (layer->horizontalScrollbar() && !layer->verticalScrollbar()) {
2173         verticalThickness = layer->horizontalScrollbar()->height();
2174         horizontalThickness = verticalThickness;
2175     } else {
2176         horizontalThickness = layer->verticalScrollbar()->width();
2177         verticalThickness = layer->horizontalScrollbar()->height();
2178     }
2179     return IntRect(cornerStart(layer, bounds.x(), bounds.maxX(), horizontalThickness),
2180                    bounds.maxY() - verticalThickness - layer->renderer()->style()->borderBottomWidth(),
2181                    horizontalThickness, verticalThickness);
2182 }
2183
2184 IntRect RenderLayer::scrollCornerRect() const
2185 {
2186     // We have a scrollbar corner when a scrollbar is visible and not filling the entire length of the box.
2187     // This happens when:
2188     // (a) A resizer is present and at least one scrollbar is present
2189     // (b) Both scrollbars are present.
2190     bool hasHorizontalBar = horizontalScrollbar();
2191     bool hasVerticalBar = verticalScrollbar();
2192     bool hasResizer = renderer()->style()->resize() != RESIZE_NONE;
2193     if ((hasHorizontalBar && hasVerticalBar) || (hasResizer && (hasHorizontalBar || hasVerticalBar)))
2194         return cornerRect(this, renderBox()->pixelSnappedBorderBoxRect());
2195     return IntRect();
2196 }
2197
2198 static IntRect resizerCornerRect(const RenderLayer* layer, const IntRect& bounds)
2199 {
2200     ASSERT(layer->renderer()->isBox());
2201     if (layer->renderer()->style()->resize() == RESIZE_NONE)
2202         return IntRect();
2203     return cornerRect(layer, bounds);
2204 }
2205
2206 IntRect RenderLayer::scrollCornerAndResizerRect() const
2207 {
2208     RenderBox* box = renderBox();
2209     if (!box)
2210         return IntRect();
2211     IntRect scrollCornerAndResizer = scrollCornerRect();
2212     if (scrollCornerAndResizer.isEmpty())
2213         scrollCornerAndResizer = resizerCornerRect(this, box->pixelSnappedBorderBoxRect());
2214     return scrollCornerAndResizer;
2215 }
2216
2217 bool RenderLayer::isScrollCornerVisible() const
2218 {
2219     ASSERT(renderer()->isBox());
2220     return !scrollCornerRect().isEmpty();
2221 }
2222
2223 IntRect RenderLayer::convertFromScrollbarToContainingView(const Scrollbar* scrollbar, const IntRect& scrollbarRect) const
2224 {
2225     RenderView* view = renderer()->view();
2226     if (!view)
2227         return scrollbarRect;
2228
2229     IntRect rect = scrollbarRect;
2230     rect.move(scrollbarOffset(scrollbar));
2231
2232     return view->frameView()->convertFromRenderer(renderer(), rect);
2233 }
2234
2235 IntRect RenderLayer::convertFromContainingViewToScrollbar(const Scrollbar* scrollbar, const IntRect& parentRect) const
2236 {
2237     RenderView* view = renderer()->view();
2238     if (!view)
2239         return parentRect;
2240
2241     IntRect rect = view->frameView()->convertToRenderer(renderer(), parentRect);
2242     rect.move(-scrollbarOffset(scrollbar));
2243     return rect;
2244 }
2245
2246 IntPoint RenderLayer::convertFromScrollbarToContainingView(const Scrollbar* scrollbar, const IntPoint& scrollbarPoint) const
2247 {
2248     RenderView* view = renderer()->view();
2249     if (!view)
2250         return scrollbarPoint;
2251
2252     IntPoint point = scrollbarPoint;
2253     point.move(scrollbarOffset(scrollbar));
2254     return view->frameView()->convertFromRenderer(renderer(), point);
2255 }
2256
2257 IntPoint RenderLayer::convertFromContainingViewToScrollbar(const Scrollbar* scrollbar, const IntPoint& parentPoint) const
2258 {
2259     RenderView* view = renderer()->view();
2260     if (!view)
2261         return parentPoint;
2262
2263     IntPoint point = view->frameView()->convertToRenderer(renderer(), parentPoint);
2264
2265     point.move(-scrollbarOffset(scrollbar));
2266     return point;
2267 }
2268
2269 IntSize RenderLayer::contentsSize() const
2270 {
2271     return IntSize(scrollWidth(), scrollHeight());
2272 }
2273
2274 int RenderLayer::visibleHeight() const
2275 {
2276     return m_layerSize.height();
2277 }
2278
2279 int RenderLayer::visibleWidth() const
2280 {
2281     return m_layerSize.width();
2282 }
2283
2284 bool RenderLayer::shouldSuspendScrollAnimations() const
2285 {
2286     RenderView* view = renderer()->view();
2287     if (!view)
2288         return true;
2289     return view->frameView()->shouldSuspendScrollAnimations();
2290 }
2291
2292 bool RenderLayer::scrollbarsCanBeActive() const
2293 {
2294     RenderView* view = renderer()->view();
2295     if (!view)
2296         return false;
2297     return view->frameView()->scrollbarsCanBeActive();
2298 }
2299
2300 IntPoint RenderLayer::currentMousePosition() const
2301 {
2302     return renderer()->frame() ? renderer()->frame()->eventHandler()->currentMousePosition() : IntPoint();
2303 }
2304
2305 IntRect RenderLayer::rectForHorizontalScrollbar(const IntRect& borderBoxRect) const
2306 {
2307     if (!m_hBar)
2308         return IntRect();
2309
2310     const RenderBox* box = renderBox();
2311     const IntRect& scrollCorner = scrollCornerRect();
2312
2313     return IntRect(horizontalScrollbarStart(borderBoxRect.x()),
2314         borderBoxRect.maxY() - box->borderBottom() - m_hBar->height(),
2315         borderBoxRect.width() - (box->borderLeft() + box->borderRight()) - scrollCorner.width(),
2316         m_hBar->height());
2317 }
2318
2319 IntRect RenderLayer::rectForVerticalScrollbar(const IntRect& borderBoxRect) const
2320 {
2321     if (!m_vBar)
2322         return IntRect();
2323
2324     const RenderBox* box = renderBox();
2325     const IntRect& scrollCorner = scrollCornerRect();
2326
2327     return IntRect(verticalScrollbarStart(borderBoxRect.x(), borderBoxRect.maxX()),
2328         borderBoxRect.y() + box->borderTop(),
2329         m_vBar->width(),
2330         borderBoxRect.height() - (box->borderTop() + box->borderBottom()) - scrollCorner.height());
2331 }
2332
2333 LayoutUnit RenderLayer::verticalScrollbarStart(int minX, int maxX) const
2334 {
2335     const RenderBox* box = renderBox();
2336     if (renderer()->style()->shouldPlaceBlockDirectionScrollbarOnLogicalLeft())
2337         return minX + box->borderLeft();
2338     return maxX - box->borderRight() - m_vBar->width();
2339 }
2340
2341 LayoutUnit RenderLayer::horizontalScrollbarStart(int minX) const
2342 {
2343     const RenderBox* box = renderBox();
2344     int x = minX + box->borderLeft();
2345     if (renderer()->style()->shouldPlaceBlockDirectionScrollbarOnLogicalLeft())
2346         x += m_vBar ? m_vBar->width() : resizerCornerRect(this, box->pixelSnappedBorderBoxRect()).width();
2347     return x;
2348 }
2349
2350 IntSize RenderLayer::scrollbarOffset(const Scrollbar* scrollbar) const
2351 {
2352     RenderBox* box = renderBox();
2353
2354     if (scrollbar == m_vBar.get())
2355         return IntSize(verticalScrollbarStart(0, box->width()), box->borderTop());
2356
2357     if (scrollbar == m_hBar.get())
2358         return IntSize(horizontalScrollbarStart(0), box->height() - box->borderBottom() - scrollbar->height());
2359     
2360     ASSERT_NOT_REACHED();
2361     return IntSize();
2362 }
2363
2364 void RenderLayer::invalidateScrollbarRect(Scrollbar* scrollbar, const IntRect& rect)
2365 {
2366 #if USE(ACCELERATED_COMPOSITING)
2367     if (scrollbar == m_vBar.get()) {
2368         if (GraphicsLayer* layer = layerForVerticalScrollbar()) {
2369             layer->setNeedsDisplayInRect(rect);
2370             return;
2371         }
2372     } else {
2373         if (GraphicsLayer* layer = layerForHorizontalScrollbar()) {
2374             layer->setNeedsDisplayInRect(rect);
2375             return;
2376         }
2377     }
2378 #endif
2379     IntRect scrollRect = rect;
2380     RenderBox* box = renderBox();
2381     ASSERT(box);
2382     // If we are not yet inserted into the tree, there is no need to repaint.
2383     if (!box->parent())
2384         return;
2385
2386     if (scrollbar == m_vBar.get())
2387         scrollRect.move(verticalScrollbarStart(0, box->width()), box->borderTop());
2388     else
2389         scrollRect.move(horizontalScrollbarStart(0), box->height() - box->borderBottom() - scrollbar->height());
2390     renderer()->repaintRectangle(scrollRect);
2391 }
2392
2393 void RenderLayer::invalidateScrollCornerRect(const IntRect& rect)
2394 {
2395 #if USE(ACCELERATED_COMPOSITING)
2396     if (GraphicsLayer* layer = layerForScrollCorner()) {
2397         layer->setNeedsDisplayInRect(rect);
2398         return;
2399     }
2400 #endif
2401     if (m_scrollCorner)
2402         m_scrollCorner->repaintRectangle(rect);
2403     if (m_resizer)
2404         m_resizer->repaintRectangle(rect);
2405 }
2406
2407 PassRefPtr<Scrollbar> RenderLayer::createScrollbar(ScrollbarOrientation orientation)
2408 {
2409     RefPtr<Scrollbar> widget;
2410     RenderObject* actualRenderer = renderer()->node() ? renderer()->node()->shadowAncestorNode()->renderer() : renderer();
2411     bool hasCustomScrollbarStyle = actualRenderer->isBox() && actualRenderer->style()->hasPseudoStyle(SCROLLBAR);
2412     if (hasCustomScrollbarStyle)
2413         widget = RenderScrollbar::createCustomScrollbar(this, orientation, actualRenderer->node());
2414     else {
2415         widget = Scrollbar::createNativeScrollbar(this, orientation, RegularScrollbar);
2416         if (orientation == HorizontalScrollbar)
2417             didAddHorizontalScrollbar(widget.get());
2418         else 
2419             didAddVerticalScrollbar(widget.get());
2420     }
2421     renderer()->document()->view()->addChild(widget.get());        
2422     return widget.release();
2423 }
2424
2425 void RenderLayer::destroyScrollbar(ScrollbarOrientation orientation)
2426 {
2427     RefPtr<Scrollbar>& scrollbar = orientation == HorizontalScrollbar ? m_hBar : m_vBar;
2428     if (!scrollbar)
2429         return;
2430
2431     if (!scrollbar->isCustomScrollbar()) {
2432         if (orientation == HorizontalScrollbar)
2433             willRemoveHorizontalScrollbar(scrollbar.get());
2434         else
2435             willRemoveVerticalScrollbar(scrollbar.get());
2436     }
2437
2438     scrollbar->removeFromParent();
2439     scrollbar->disconnectFromScrollableArea();
2440     scrollbar = 0;
2441 }
2442
2443 bool RenderLayer::scrollsOverflow() const
2444 {
2445     if (!renderer()->isBox())
2446         return false;
2447
2448     return toRenderBox(renderer())->scrollsOverflow();
2449 }
2450
2451 bool RenderLayer::allowsScrolling() const
2452 {
2453     return (m_hBar && m_hBar->enabled()) || (m_vBar && m_vBar->enabled());
2454 }
2455
2456 void RenderLayer::setHasHorizontalScrollbar(bool hasScrollbar)
2457 {
2458     if (hasScrollbar == hasHorizontalScrollbar())
2459         return;
2460
2461     if (hasScrollbar)
2462         m_hBar = createScrollbar(HorizontalScrollbar);
2463     else
2464         destroyScrollbar(HorizontalScrollbar);
2465
2466     // Destroying or creating one bar can cause our scrollbar corner to come and go.  We need to update the opposite scrollbar's style.
2467     if (m_hBar)
2468         m_hBar->styleChanged();
2469     if (m_vBar)
2470         m_vBar->styleChanged();
2471
2472     // Force an update since we know the scrollbars have changed things.
2473 #if ENABLE(DASHBOARD_SUPPORT) || ENABLE(DRAGGABLE_REGION)
2474     if (renderer()->document()->hasAnnotatedRegions())
2475         renderer()->document()->setAnnotatedRegionsDirty(true);
2476 #endif
2477 }
2478
2479 void RenderLayer::setHasVerticalScrollbar(bool hasScrollbar)
2480 {
2481     if (hasScrollbar == hasVerticalScrollbar())
2482         return;
2483
2484     if (hasScrollbar)
2485         m_vBar = createScrollbar(VerticalScrollbar);
2486     else
2487         destroyScrollbar(VerticalScrollbar);
2488
2489      // Destroying or creating one bar can cause our scrollbar corner to come and go.  We need to update the opposite scrollbar's style.
2490     if (m_hBar)
2491         m_hBar->styleChanged();
2492     if (m_vBar)
2493         m_vBar->styleChanged();
2494
2495     // Force an update since we know the scrollbars have changed things.
2496 #if ENABLE(DASHBOARD_SUPPORT) || ENABLE(DRAGGABLE_REGION)
2497     if (renderer()->document()->hasAnnotatedRegions())
2498         renderer()->document()->setAnnotatedRegionsDirty(true);
2499 #endif
2500 }
2501
2502 ScrollableArea* RenderLayer::enclosingScrollableArea() const
2503 {
2504     if (RenderLayer* scrollableLayer = enclosingScrollableLayer())
2505         return scrollableLayer;
2506
2507     // FIXME: We should return the frame view here (or possibly an ancestor frame view,
2508     // if the frame view isn't scrollable.
2509     return 0;
2510 }
2511
2512 int RenderLayer::verticalScrollbarWidth(OverlayScrollbarSizeRelevancy relevancy) const
2513 {
2514     if (!m_vBar || (m_vBar->isOverlayScrollbar() && relevancy == IgnoreOverlayScrollbarSize))
2515         return 0;
2516     return m_vBar->width();
2517 }
2518
2519 int RenderLayer::horizontalScrollbarHeight(OverlayScrollbarSizeRelevancy relevancy) const
2520 {
2521     if (!m_hBar || (m_hBar->isOverlayScrollbar() && relevancy == IgnoreOverlayScrollbarSize))
2522         return 0;
2523     return m_hBar->height();
2524 }
2525
2526 IntSize RenderLayer::offsetFromResizeCorner(const IntPoint& absolutePoint) const
2527 {
2528     // Currently the resize corner is either the bottom right corner or the bottom left corner.
2529     // FIXME: This assumes the location is 0, 0. Is this guaranteed to always be the case?
2530     IntSize elementSize = size();
2531     if (renderer()->style()->shouldPlaceBlockDirectionScrollbarOnLogicalLeft())
2532         elementSize.setWidth(0);
2533     IntPoint resizerPoint = toPoint(elementSize);
2534     IntPoint localPoint = roundedIntPoint(absoluteToContents(absolutePoint));
2535     return localPoint - resizerPoint;
2536 }
2537
2538 bool RenderLayer::hasOverflowControls() const
2539 {
2540     return m_hBar || m_vBar || m_scrollCorner || renderer()->style()->resize() != RESIZE_NONE;
2541 }
2542
2543 void RenderLayer::positionOverflowControls(const IntSize& offsetFromRoot)
2544 {
2545     if (!m_hBar && !m_vBar && (!renderer()->hasOverflowClip() || renderer()->style()->resize() == RESIZE_NONE))
2546         return;
2547     
2548     RenderBox* box = renderBox();
2549     if (!box)
2550         return;
2551
2552     const IntRect borderBox = box->pixelSnappedBorderBoxRect();
2553     const IntRect& scrollCorner = scrollCornerRect();
2554     IntRect absBounds(borderBox.location() + offsetFromRoot, borderBox.size());
2555     if (m_vBar) {
2556         IntRect vBarRect = rectForVerticalScrollbar(borderBox);
2557         vBarRect.move(offsetFromRoot);
2558         m_vBar->setFrameRect(vBarRect);
2559     }
2560     
2561     if (m_hBar) {
2562         IntRect hBarRect = rectForHorizontalScrollbar(borderBox);
2563         hBarRect.move(offsetFromRoot);
2564         m_hBar->setFrameRect(hBarRect);
2565     }
2566     
2567     if (m_scrollCorner)
2568         m_scrollCorner->setFrameRect(scrollCorner);
2569     if (m_resizer)
2570         m_resizer->setFrameRect(resizerCornerRect(this, borderBox));
2571
2572 #if USE(ACCELERATED_COMPOSITING)    
2573     if (isComposited())
2574         backing()->positionOverflowControlsLayers(offsetFromRoot);
2575 #endif
2576 }
2577
2578 int RenderLayer::scrollWidth() const
2579 {
2580     ASSERT(renderBox());
2581     if (m_scrollDimensionsDirty)
2582         const_cast<RenderLayer*>(this)->computeScrollDimensions();
2583     return snapSizeToPixel(m_scrollSize.width(), renderBox()->clientLeft() + renderBox()->x());
2584 }
2585
2586 int RenderLayer::scrollHeight() const
2587 {
2588     ASSERT(renderBox());
2589     if (m_scrollDimensionsDirty)
2590         const_cast<RenderLayer*>(this)->computeScrollDimensions();
2591     return snapSizeToPixel(m_scrollSize.height(), renderBox()->clientTop() + renderBox()->y());
2592 }
2593
2594 LayoutUnit RenderLayer::overflowTop() const
2595 {
2596     RenderBox* box = renderBox();
2597     LayoutRect overflowRect(box->layoutOverflowRect());
2598     box->flipForWritingMode(overflowRect);
2599     return overflowRect.y();
2600 }
2601
2602 LayoutUnit RenderLayer::overflowBottom() const
2603 {
2604     RenderBox* box = renderBox();
2605     LayoutRect overflowRect(box->layoutOverflowRect());
2606     box->flipForWritingMode(overflowRect);
2607     return overflowRect.maxY();
2608 }
2609
2610 LayoutUnit RenderLayer::overflowLeft() const
2611 {
2612     RenderBox* box = renderBox();
2613     LayoutRect overflowRect(box->layoutOverflowRect());
2614     box->flipForWritingMode(overflowRect);
2615     return overflowRect.x();
2616 }
2617
2618 LayoutUnit RenderLayer::overflowRight() const
2619 {
2620     RenderBox* box = renderBox();
2621     LayoutRect overflowRect(box->layoutOverflowRect());
2622     box->flipForWritingMode(overflowRect);
2623     return overflowRect.maxX();
2624 }
2625
2626 void RenderLayer::computeScrollDimensions()
2627 {
2628     RenderBox* box = renderBox();
2629     ASSERT(box);
2630
2631     m_scrollDimensionsDirty = false;
2632
2633     m_scrollSize.setWidth(overflowRight() - overflowLeft());
2634     m_scrollSize.setHeight(overflowBottom() - overflowTop());
2635
2636     int scrollableLeftOverflow = overflowLeft() - box->borderLeft();
2637     int scrollableTopOverflow = overflowTop() - box->borderTop();
2638     setScrollOrigin(IntPoint(-scrollableLeftOverflow, -scrollableTopOverflow));
2639 }
2640
2641 bool RenderLayer::hasScrollableHorizontalOverflow() const
2642 {
2643     return hasHorizontalOverflow() && renderBox()->scrollsOverflowX();
2644 }
2645
2646 bool RenderLayer::hasScrollableVerticalOverflow() const
2647 {
2648     return hasVerticalOverflow() && renderBox()->scrollsOverflowY();
2649 }
2650
2651 bool RenderLayer::hasHorizontalOverflow() const
2652 {
2653     ASSERT(!m_scrollDimensionsDirty);
2654
2655     return scrollWidth() > renderBox()->pixelSnappedClientWidth();
2656 }
2657
2658 bool RenderLayer::hasVerticalOverflow() const
2659 {
2660     ASSERT(!m_scrollDimensionsDirty);
2661
2662     return scrollHeight() > renderBox()->pixelSnappedClientHeight();
2663 }
2664
2665 void RenderLayer::updateScrollbarsAfterLayout()
2666 {
2667     RenderBox* box = renderBox();
2668     ASSERT(box);
2669
2670     // List box parts handle the scrollbars by themselves so we have nothing to do.
2671     if (box->style()->appearance() == ListboxPart)
2672         return;
2673
2674     bool hasHorizontalOverflow = this->hasHorizontalOverflow();
2675     bool hasVerticalOverflow = this->hasVerticalOverflow();
2676
2677     // overflow:scroll should just enable/disable.
2678     if (renderer()->style()->overflowX() == OSCROLL)
2679         m_hBar->setEnabled(hasHorizontalOverflow);
2680     if (renderer()->style()->overflowY() == OSCROLL)
2681         m_vBar->setEnabled(hasVerticalOverflow);
2682
2683     // overflow:auto may need to lay out again if scrollbars got added/removed.
2684     bool autoHorizontalScrollBarChanged = box->hasAutoHorizontalScrollbar() && (hasHorizontalScrollbar() != hasHorizontalOverflow);
2685     bool autoVerticalScrollBarChanged = box->hasAutoVerticalScrollbar() && (hasVerticalScrollbar() != hasVerticalOverflow);
2686
2687     if (autoHorizontalScrollBarChanged || autoVerticalScrollBarChanged) {
2688         if (box->hasAutoHorizontalScrollbar())
2689             setHasHorizontalScrollbar(hasHorizontalOverflow);
2690         if (box->hasAutoVerticalScrollbar())
2691             setHasVerticalScrollbar(hasVerticalOverflow);
2692
2693         updateSelfPaintingLayer();
2694
2695         // Force an update since we know the scrollbars have changed things.
2696 #if ENABLE(DASHBOARD_SUPPORT) || ENABLE(DRAGGABLE_REGION)
2697         if (renderer()->document()->hasAnnotatedRegions())
2698             renderer()->document()->setAnnotatedRegionsDirty(true);
2699 #endif
2700
2701         renderer()->repaint();
2702
2703         if (renderer()->style()->overflowX() == OAUTO || renderer()->style()->overflowY() == OAUTO) {
2704             if (!m_inOverflowRelayout) {
2705                 // Our proprietary overflow: overlay value doesn't trigger a layout.
2706                 m_inOverflowRelayout = true;
2707                 renderer()->setNeedsLayout(true, MarkOnlyThis);
2708                 if (renderer()->isRenderBlock()) {
2709                     RenderBlock* block = toRenderBlock(renderer());
2710                     block->scrollbarsChanged(autoHorizontalScrollBarChanged, autoVerticalScrollBarChanged);
2711                     block->layoutBlock(true);
2712                 } else
2713                     renderer()->layout();
2714                 m_inOverflowRelayout = false;
2715             }
2716         }
2717     }
2718
2719     // Set up the range (and page step/line step).
2720     if (m_hBar) {
2721         int clientWidth = box->pixelSnappedClientWidth();
2722         int pageStep = max(max<int>(clientWidth * Scrollbar::minFractionToStepWhenPaging(), clientWidth - Scrollbar::maxOverlapBetweenPages()), 1);
2723         m_hBar->setSteps(Scrollbar::pixelsPerLineStep(), pageStep);
2724         m_hBar->setProportion(clientWidth, m_scrollSize.width());
2725     }
2726     if (m_vBar) {
2727         int clientHeight = box->pixelSnappedClientHeight();
2728         int pageStep = max(max<int>(clientHeight * Scrollbar::minFractionToStepWhenPaging(), clientHeight - Scrollbar::maxOverlapBetweenPages()), 1);
2729         m_vBar->setSteps(Scrollbar::pixelsPerLineStep(), pageStep);
2730         m_vBar->setProportion(clientHeight, m_scrollSize.height());
2731     }
2732
2733     updateScrollableAreaSet(hasScrollableHorizontalOverflow() || hasScrollableVerticalOverflow());
2734 }
2735
2736 void RenderLayer::updateScrollInfoAfterLayout()
2737 {
2738     RenderBox* box = renderBox();
2739     if (!box)
2740         return;
2741
2742     m_scrollDimensionsDirty = true;
2743     IntSize originalScrollOffset = scrollOffset();
2744
2745     computeScrollDimensions();
2746
2747     if (box->style()->overflowX() != OMARQUEE) {
2748         // Layout may cause us to be at an invalid scroll position. In this case we need
2749         // to pull our scroll offsets back to the max (or push them up to the min).
2750         IntSize clampedScrollOffset = clampScrollOffset(scrollOffset());
2751         if (clampedScrollOffset != scrollOffset())
2752             scrollToOffset(clampedScrollOffset);
2753     }
2754
2755     updateScrollbarsAfterLayout();
2756
2757     if (originalScrollOffset != scrollOffset())
2758         scrollToOffsetWithoutAnimation(toPoint(scrollOffset()));
2759
2760 #if USE(ACCELERATED_COMPOSITING)
2761     // Composited scrolling may need to be enabled or disabled if the amount of overflow changed.
2762     if (renderer()->view() && compositor()->updateLayerCompositingState(this))
2763         compositor()->setCompositingLayersNeedRebuild();
2764 #endif
2765 }
2766
2767 bool RenderLayer::overflowControlsIntersectRect(const IntRect& localRect) const
2768 {
2769     const IntRect borderBox = renderBox()->pixelSnappedBorderBoxRect();
2770
2771     if (rectForHorizontalScrollbar(borderBox).intersects(localRect))
2772         return true;
2773
2774     if (rectForVerticalScrollbar(borderBox).intersects(localRect))
2775         return true;
2776
2777     if (scrollCornerRect().intersects(localRect))
2778         return true;
2779     
2780     if (resizerCornerRect(this, borderBox).intersects(localRect))
2781         return true;
2782
2783     return false;
2784 }
2785
2786 void RenderLayer::paintOverflowControls(GraphicsContext* context, const IntPoint& paintOffset, const IntRect& damageRect, bool paintingOverlayControls)
2787 {
2788     // Don't do anything if we have no overflow.
2789     if (!renderer()->hasOverflowClip())
2790         return;
2791
2792     // Overlay scrollbars paint in a second pass through the layer tree so that they will paint
2793     // on top of everything else. If this is the normal painting pass, paintingOverlayControls
2794     // will be false, and we should just tell the root layer that there are overlay scrollbars
2795     // that need to be painted. That will cause the second pass through the layer tree to run,
2796     // and we'll paint the scrollbars then. In the meantime, cache tx and ty so that the 
2797     // second pass doesn't need to re-enter the RenderTree to get it right.
2798     if (hasOverlayScrollbars() && !paintingOverlayControls) {
2799         m_cachedOverlayScrollbarOffset = paintOffset;
2800 #if USE(ACCELERATED_COMPOSITING)
2801         // It's not necessary to do the second pass if the scrollbars paint into layers.
2802         if ((m_hBar && layerForHorizontalScrollbar()) || (m_vBar && layerForVerticalScrollbar()))
2803             return;
2804 #endif
2805         IntRect localDamgeRect = damageRect;
2806         localDamgeRect.moveBy(-paintOffset);
2807         if (!overflowControlsIntersectRect(localDamgeRect))
2808             return;
2809
2810         RenderView* renderView = renderer()->view();
2811
2812         RenderLayer* paintingRoot = 0;
2813 #if USE(ACCELERATED_COMPOSITING)
2814         paintingRoot = enclosingCompositingLayer();
2815 #endif
2816         if (!paintingRoot)
2817             paintingRoot = renderView->layer();
2818
2819         paintingRoot->setContainsDirtyOverlayScrollbars(true);
2820         return;
2821     }
2822
2823     // This check is required to avoid painting custom CSS scrollbars twice.
2824     if (paintingOverlayControls && !hasOverlayScrollbars())
2825         return;
2826
2827     IntPoint adjustedPaintOffset = paintOffset;
2828     if (paintingOverlayControls)
2829         adjustedPaintOffset = m_cachedOverlayScrollbarOffset;
2830
2831     // Move the scrollbar widgets if necessary.  We normally move and resize widgets during layout, but sometimes
2832     // widgets can move without layout occurring (most notably when you scroll a document that
2833     // contains fixed positioned elements).
2834     positionOverflowControls(toSize(adjustedPaintOffset));
2835
2836     // Now that we're sure the scrollbars are in the right place, paint them.
2837     if (m_hBar
2838 #if USE(ACCELERATED_COMPOSITING)
2839         && !layerForHorizontalScrollbar()
2840 #endif
2841               )
2842         m_hBar->paint(context, damageRect);
2843     if (m_vBar
2844 #if USE(ACCELERATED_COMPOSITING)
2845         && !layerForVerticalScrollbar()
2846 #endif
2847               )
2848         m_vBar->paint(context, damageRect);
2849
2850 #if USE(ACCELERATED_COMPOSITING)
2851     if (layerForScrollCorner())
2852         return;
2853 #endif
2854
2855     // We fill our scroll corner with white if we have a scrollbar that doesn't run all the way up to the
2856     // edge of the box.
2857     paintScrollCorner(context, adjustedPaintOffset, damageRect);
2858     
2859     // Paint our resizer last, since it sits on top of the scroll corner.
2860     paintResizer(context, adjustedPaintOffset, damageRect);
2861 }
2862
2863 void RenderLayer::paintScrollCorner(GraphicsContext* context, const IntPoint& paintOffset, const IntRect& damageRect)
2864 {
2865     RenderBox* box = renderBox();
2866     ASSERT(box);
2867
2868     IntRect absRect = scrollCornerRect();
2869     absRect.moveBy(paintOffset);
2870     if (!absRect.intersects(damageRect))
2871         return;
2872
2873     if (context->updatingControlTints()) {
2874         updateScrollCornerStyle();
2875         return;
2876     }
2877
2878     if (m_scrollCorner) {
2879         m_scrollCorner->paintIntoRect(context, paintOffset, absRect);
2880         return;
2881     }
2882
2883     // We don't want to paint white if we have overlay scrollbars, since we need
2884     // to see what is behind it.
2885     if (!hasOverlayScrollbars())
2886         context->fillRect(absRect, Color::white, box->style()->colorSpace());
2887 }
2888
2889 void RenderLayer::drawPlatformResizerImage(GraphicsContext* context, IntRect resizerCornerRect)
2890 {
2891     float deviceScaleFactor = WebCore::deviceScaleFactor(renderer()->frame());
2892
2893     RefPtr<Image> resizeCornerImage;
2894     IntSize cornerResizerSize;
2895     if (deviceScaleFactor >= 2) {
2896         DEFINE_STATIC_LOCAL(Image*, resizeCornerImageHiRes, (Image::loadPlatformResource("textAreaResizeCorner@2x").leakRef()));
2897         resizeCornerImage = resizeCornerImageHiRes;
2898         cornerResizerSize = resizeCornerImage->size();
2899         cornerResizerSize.scale(0.5f);
2900     } else {
2901         DEFINE_STATIC_LOCAL(Image*, resizeCornerImageLoRes, (Image::loadPlatformResource("textAreaResizeCorner").leakRef()));
2902         resizeCornerImage = resizeCornerImageLoRes;
2903         cornerResizerSize = resizeCornerImage->size();
2904     }
2905
2906     if (renderer()->style()->shouldPlaceBlockDirectionScrollbarOnLogicalLeft()) {
2907         context->save();
2908         context->translate(resizerCornerRect.x() + cornerResizerSize.width(), resizerCornerRect.y() + resizerCornerRect.height() - cornerResizerSize.height());
2909         context->scale(FloatSize(-1.0, 1.0));
2910         context->drawImage(resizeCornerImage.get(), renderer()->style()->colorSpace(), IntRect(IntPoint(), cornerResizerSize));
2911         context->restore();
2912         return;
2913     }
2914     IntRect imageRect(resizerCornerRect.maxXMaxYCorner() - cornerResizerSize, cornerResizerSize);
2915     context->drawImage(resizeCornerImage.get(), renderer()->style()->colorSpace(), imageRect);
2916 }
2917
2918 void RenderLayer::paintResizer(GraphicsContext* context, const IntPoint& paintOffset, const IntRect& damageRect)
2919 {
2920     if (renderer()->style()->resize() == RESIZE_NONE)
2921         return;
2922
2923     RenderBox* box = renderBox();
2924     ASSERT(box);
2925
2926     IntRect absRect = resizerCornerRect(this, box->pixelSnappedBorderBoxRect());
2927     absRect.moveBy(paintOffset);
2928     if (!absRect.intersects(damageRect))
2929         return;
2930
2931     if (context->updatingControlTints()) {
2932         updateResizerStyle();
2933         return;
2934     }
2935     
2936     if (m_resizer) {
2937         m_resizer->paintIntoRect(context, paintOffset, absRect);
2938         return;
2939     }
2940
2941     drawPlatformResizerImage(context, absRect);
2942
2943     // Draw a frame around the resizer (1px grey line) if there are any scrollbars present.
2944     // Clipping will exclude the right and bottom edges of this frame.
2945     if (!hasOverlayScrollbars() && (m_vBar || m_hBar)) {
2946         GraphicsContextStateSaver stateSaver(*context);
2947         context->clip(absRect);
2948         IntRect largerCorner = absRect;
2949         largerCorner.setSize(IntSize(largerCorner.width() + 1, largerCorner.height() + 1));
2950         context->setStrokeColor(Color(makeRGB(217, 217, 217)), ColorSpaceDeviceRGB);
2951         context->setStrokeThickness(1.0f);
2952         context->setFillColor(Color::transparent, ColorSpaceDeviceRGB);
2953         context->drawRect(largerCorner);
2954     }
2955 }
2956
2957 bool RenderLayer::isPointInResizeControl(const IntPoint& absolutePoint) const
2958 {
2959     if (!renderer()->hasOverflowClip() || renderer()->style()->resize() == RESIZE_NONE)
2960         return false;
2961     
2962     RenderBox* box = renderBox();
2963     ASSERT(box);
2964
2965     IntPoint localPoint = roundedIntPoint(absoluteToContents(absolutePoint));
2966
2967     IntRect localBounds(0, 0, box->pixelSnappedWidth(), box->pixelSnappedHeight());
2968     return resizerCornerRect(this, localBounds).contains(localPoint);
2969 }
2970     
2971 bool RenderLayer::hitTestOverflowControls(HitTestResult& result, const IntPoint& localPoint)
2972 {
2973     if (!m_hBar && !m_vBar && (!renderer()->hasOverflowClip() || renderer()->style()->resize() == RESIZE_NONE))
2974         return false;
2975
2976     RenderBox* box = renderBox();
2977     ASSERT(box);
2978     
2979     IntRect resizeControlRect;
2980     if (renderer()->style()->resize() != RESIZE_NONE) {
2981         resizeControlRect = resizerCornerRect(this, box->pixelSnappedBorderBoxRect());
2982         if (resizeControlRect.contains(localPoint))
2983             return true;
2984     }
2985
2986     int resizeControlSize = max(resizeControlRect.height(), 0);
2987
2988     // FIXME: We should hit test the m_scrollCorner and pass it back through the result.
2989
2990     if (m_vBar && m_vBar->shouldParticipateInHitTesting()) {
2991         LayoutRect vBarRect(verticalScrollbarStart(0, box->width()),
2992                             box->borderTop(),
2993                             m_vBar->width(),
2994                             box->height() - (box->borderTop() + box->borderBottom()) - (m_hBar ? m_hBar->height() : resizeControlSize));
2995         if (vBarRect.contains(localPoint)) {
2996             result.setScrollbar(m_vBar.get());
2997             return true;
2998         }
2999     }
3000
3001     resizeControlSize = max(resizeControlRect.width(), 0);
3002     if (m_hBar && m_hBar->shouldParticipateInHitTesting()) {
3003         LayoutRect hBarRect(horizontalScrollbarStart(0),
3004                             box->height() - box->borderBottom() - m_hBar->height(),
3005                             box->width() - (box->borderLeft() + box->borderRight()) - (m_vBar ? m_vBar->width() : resizeControlSize),
3006                             m_hBar->height());
3007         if (hBarRect.contains(localPoint)) {
3008             result.setScrollbar(m_hBar.get());
3009             return true;
3010         }
3011     }
3012
3013     return false;
3014 }
3015
3016 bool RenderLayer::scroll(ScrollDirection direction, ScrollGranularity granularity, float multiplier)
3017 {
3018     return ScrollableArea::scroll(direction, granularity, multiplier);
3019 }
3020
3021 void RenderLayer::paint(GraphicsContext* context, const LayoutRect& damageRect, PaintBehavior paintBehavior, RenderObject* paintingRoot, RenderRegion* region, PaintLayerFlags paintFlags)
3022 {
3023     OverlapTestRequestMap overlapTestRequests;
3024
3025     LayerPaintingInfo paintingInfo(this, enclosingIntRect(damageRect), paintBehavior, LayoutSize(), paintingRoot, region, &overlapTestRequests);
3026     paintLayer(context, paintingInfo, paintFlags);
3027
3028     OverlapTestRequestMap::iterator end = overlapTestRequests.end();
3029     for (OverlapTestRequestMap::iterator it = overlapTestRequests.begin(); it != end; ++it)
3030         it->key->setOverlapTestResult(false);
3031 }
3032
3033 void RenderLayer::paintOverlayScrollbars(GraphicsContext* context, const LayoutRect& damageRect, PaintBehavior paintBehavior, RenderObject* paintingRoot)
3034 {
3035     if (!m_containsDirtyOverlayScrollbars)
3036         return;
3037
3038     LayerPaintingInfo paintingInfo(this, enclosingIntRect(damageRect), paintBehavior, LayoutSize(), paintingRoot);
3039     paintLayer(context, paintingInfo, PaintLayerPaintingOverlayScrollbars);
3040
3041     m_containsDirtyOverlayScrollbars = false;
3042 }
3043
3044 #ifndef DISABLE_ROUNDED_CORNER_CLIPPING
3045 static bool inContainingBlockChain(RenderLayer* startLayer, RenderLayer* endLayer)
3046 {
3047     if (startLayer == endLayer)
3048         return true;
3049     
3050     RenderView* view = startLayer->renderer()->view();
3051     for (RenderBlock* currentBlock = startLayer->renderer()->containingBlock(); currentBlock && currentBlock != view; currentBlock = currentBlock->containingBlock()) {
3052         if (currentBlock->layer() == endLayer)
3053             return true;
3054     }
3055     
3056     return false;
3057 }
3058 #endif
3059
3060 void RenderLayer::clipToRect(RenderLayer* rootLayer, GraphicsContext* context, const LayoutRect& paintDirtyRect, const ClipRect& clipRect,
3061                              BorderRadiusClippingRule rule)
3062 {
3063     if (clipRect.rect() == paintDirtyRect)
3064         return;
3065     context->save();
3066     context->clip(pixelSnappedIntRect(clipRect.rect()));
3067     
3068     if (!clipRect.hasRadius())
3069         return;
3070
3071 #ifndef DISABLE_ROUNDED_CORNER_CLIPPING
3072     // If the clip rect has been tainted by a border radius, then we have to walk up our layer chain applying the clips from
3073     // any layers with overflow. The condition for being able to apply these clips is that the overflow object be in our
3074     // containing block chain so we check that also.
3075     for (RenderLayer* layer = rule == IncludeSelfForBorderRadius ? this : parent(); layer; layer = layer->parent()) {
3076         if (layer->renderer()->hasOverflowClip() && layer->renderer()->style()->hasBorderRadius() && inContainingBlockChain(this, layer)) {
3077                 LayoutPoint delta;
3078                 layer->convertToLayerCoords(rootLayer, delta);
3079                 context->addRoundedRectClip(layer->renderer()->style()->getRoundedInnerBorderFor(LayoutRect(delta, layer->size())));
3080         }
3081
3082         if (layer == rootLayer)
3083             break;
3084     }
3085 #endif
3086 }
3087
3088 void RenderLayer::restoreClip(GraphicsContext* context, const LayoutRect& paintDirtyRect, const ClipRect& clipRect)
3089 {
3090     if (clipRect.rect() == paintDirtyRect)
3091         return;
3092     context->restore();
3093 }
3094
3095 static void performOverlapTests(OverlapTestRequestMap& overlapTestRequests, const RenderLayer* rootLayer, const RenderLayer* layer)
3096 {
3097     Vector<OverlapTestRequestClient*> overlappedRequestClients;
3098     OverlapTestRequestMap::iterator end = overlapTestRequests.end();
3099     LayoutRect boundingBox = layer->boundingBox(rootLayer);
3100     for (OverlapTestRequestMap::iterator it = overlapTestRequests.begin(); it != end; ++it) {
3101         if (!boundingBox.intersects(it->value))
3102             continue;
3103
3104         it->key->setOverlapTestResult(true);
3105         overlappedRequestClients.append(it->key);
3106     }
3107     for (size_t i = 0; i < overlappedRequestClients.size(); ++i)
3108         overlapTestRequests.remove(overlappedRequestClients[i]);
3109 }
3110
3111 #if USE(ACCELERATED_COMPOSITING)
3112 static bool shouldDoSoftwarePaint(const RenderLayer* layer, bool paintingReflection)
3113 {
3114     return paintingReflection && !layer->has3DTransform();
3115 }
3116 #endif
3117     
3118 static inline bool shouldSuppressPaintingLayer(RenderLayer* layer)
3119 {
3120     // Avoid painting descendants of the root layer when stylesheets haven't loaded. This eliminates FOUC.
3121     // It's ok not to draw, because later on, when all the stylesheets do load, updateStyleSelector on the Document
3122     // will do a full repaint().
3123     if (layer->renderer()->document()->didLayoutWithPendingStylesheets() && !layer->isRootLayer() && !layer->renderer()->isRoot())
3124         return true;
3125
3126     // Avoid painting all layers if the document is in a state where visual updates aren't allowed.
3127     // A full repaint will occur in Document::implicitClose() if painting is suppressed here.
3128     if (!layer->renderer()->document()->visualUpdatesAllowed())
3129         return true;
3130
3131     return false;
3132 }
3133
3134
3135 void RenderLayer::paintLayer(GraphicsContext* context, const LayerPaintingInfo& paintingInfo, PaintLayerFlags paintFlags)
3136 {
3137 #if USE(ACCELERATED_COMPOSITING)
3138     if (isComposited()) {
3139         // The updatingControlTints() painting pass goes through compositing layers,
3140         // but we need to ensure that we don't cache clip rects computed with the wrong root in this case.
3141         if (context->updatingControlTints() || (paintingInfo.paintBehavior & PaintBehaviorFlattenCompositingLayers))
3142             paintFlags |= PaintLayerTemporaryClipRects;
3143         else if (!backing()->paintsIntoWindow()
3144             && !backing()->paintsIntoCompositedAncestor()
3145             && !shouldDoSoftwarePaint(this, paintFlags & PaintLayerPaintingReflection)) {
3146             // If this RenderLayer should paint into its backing, that will be done via RenderLayerBacking::paintIntoLayer().
3147             return;
3148         }
3149     } else if (compositor()->fixedPositionLayerNotCompositedReason(this) == RenderLayerCompositor::LayerBoundsOutOfView) {
3150         // Don't paint out-of-view fixed position layers (when doing prepainting) because they will never be visible
3151         // unless their position or viewport size is changed.
3152         return;
3153     }
3154 #endif
3155
3156     // Non self-painting leaf layers don't need to be painted as their renderer() should properly paint itself.
3157     if (!isSelfPaintingLayer() && !hasSelfPaintingLayerDescendant())
3158         return;
3159
3160     if (shouldSuppressPaintingLayer(this))
3161         return;
3162     
3163     // If this layer is totally invisible then there is nothing to paint.
3164     if (!renderer()->opacity())
3165         return;
3166
3167     if (paintsWithTransparency(paintingInfo.paintBehavior))
3168         paintFlags |= PaintLayerHaveTransparency;
3169
3170     // PaintLayerAppliedTransform is used in RenderReplica, to avoid applying the transform twice.
3171     if (paintsWithTransform(paintingInfo.paintBehavior) && !(paintFlags & PaintLayerAppliedTransform)) {
3172         TransformationMatrix layerTransform = renderableTransform(paintingInfo.paintBehavior);
3173         // If the transform can't be inverted, then don't paint anything.
3174         if (!layerTransform.isInvertible())
3175             return;
3176
3177         // If we have a transparency layer enclosing us and we are the root of a transform, then we need to establish the transparency
3178         // layer from the parent now, assuming there is a parent
3179         if (paintFlags & PaintLayerHaveTransparency) {
3180             if (parent())
3181                 parent()->beginTransparencyLayers(context, paintingInfo.rootLayer, paintingInfo.paintDirtyRect, paintingInfo.paintBehavior);
3182             else
3183                 beginTransparencyLayers(context, paintingInfo.rootLayer, paintingInfo.paintDirtyRect, paintingInfo.paintBehavior);
3184         }
3185
3186         // Make sure the parent's clip rects have been calculated.
3187         ClipRect clipRect = paintingInfo.paintDirtyRect;
3188         if (parent()) {
3189             ClipRectsContext clipRectsContext(paintingInfo.rootLayer, paintingInfo.region, (paintFlags & PaintLayerTemporaryClipRects) ? TemporaryClipRects : PaintingClipRects,
3190                 IgnoreOverlayScrollbarSize, (paintFlags & PaintLayerPaintingOverflowContents) ? IgnoreOverflowClip : RespectOverflowClip);
3191             clipRect = backgroundClipRect(clipRectsContext);
3192             clipRect.intersect(paintingInfo.paintDirtyRect);
3193         
3194             // Push the parent coordinate space's clip.
3195             parent()->clipToRect(paintingInfo.rootLayer, context, paintingInfo.paintDirtyRect, clipRect);
3196         }
3197
3198         // Adjust the transform such that the renderer's upper left corner will paint at (0,0) in user space.
3199         // This involves subtracting out the position of the layer in our current coordinate space, but preserving
3200         // the accumulated error for sub-pixel layout.
3201         LayoutPoint delta;
3202         convertToLayerCoords(paintingInfo.rootLayer, delta);
3203         TransformationMatrix transform(layerTransform);
3204         IntPoint roundedDelta = roundedIntPoint(delta);
3205         transform.translateRight(roundedDelta.x(), roundedDelta.y());
3206         LayoutSize adjustedSubPixelAccumulation = paintingInfo.subPixelAccumulation + (delta - roundedDelta);
3207         
3208         // Apply the transform.
3209         {
3210             GraphicsContextStateSaver stateSaver(*context);
3211             context->concatCTM(transform.toAffineTransform());
3212
3213             // Now do a paint with the root layer shifted to be us.
3214             LayerPaintingInfo transformedPaintingInfo(this, enclosingIntRect(transform.inverse().mapRect(paintingInfo.paintDirtyRect)), paintingInfo.paintBehavior, adjustedSubPixelAccumulation, paintingInfo.paintingRoot, paintingInfo.region, paintingInfo.overlapTestRequests);
3215             paintLayerContentsAndReflection(context, transformedPaintingInfo, paintFlags);
3216         }        
3217
3218         // Restore the clip.
3219         if (parent())
3220             parent()->restoreClip(context, paintingInfo.paintDirtyRect, clipRect);
3221
3222         return;
3223     }
3224     
3225     paintLayerContentsAndReflection(context, paintingInfo, paintFlags);
3226 }
3227
3228 void RenderLayer::paintLayerContentsAndReflection(GraphicsContext* context, const LayerPaintingInfo& paintingInfo, PaintLayerFlags paintFlags)
3229 {
3230     ASSERT(isSelfPaintingLayer() || hasSelfPaintingLayerDescendant());
3231
3232     PaintLayerFlags localPaintFlags = paintFlags & ~(PaintLayerAppliedTransform);
3233
3234     // Paint the reflection first if we have one.
3235     if (m_reflection && !m_paintingInsideReflection) {
3236         // Mark that we are now inside replica painting.
3237         m_paintingInsideReflection = true;
3238         reflectionLayer()->paintLayer(context, paintingInfo, localPaintFlags | PaintLayerPaintingReflection);
3239         m_paintingInsideReflection = false;
3240     }
3241
3242     localPaintFlags |= PaintLayerPaintingCompositingAllPhases;
3243     paintLayerContents(context, paintingInfo, localPaintFlags);
3244 }
3245
3246 void RenderLayer::paintLayerContents(GraphicsContext* context, const LayerPaintingInfo& paintingInfo, PaintLayerFlags paintFlags)
3247 {
3248     ASSERT(isSelfPaintingLayer() || hasSelfPaintingLayerDescendant());
3249
3250     PaintLayerFlags localPaintFlags = paintFlags & ~(PaintLayerAppliedTransform);
3251     bool haveTransparency = localPaintFlags & PaintLayerHaveTransparency;
3252     bool isSelfPaintingLayer = this->isSelfPaintingLayer();
3253     bool isPaintingOverlayScrollbars = paintFlags & PaintLayerPaintingOverlayScrollbars;
3254     // Outline always needs to be painted even if we have no visible content.
3255     bool shouldPaintOutline = isSelfPaintingLayer && !isPaintingOverlayScrollbars;
3256     bool shouldPaintContent = m_hasVisibleContent && isSelfPaintingLayer && !isPaintingOverlayScrollbars;
3257
3258     bool useClipRect = true;
3259     GraphicsContext* transparencyLayerContext = context;
3260     
3261     // Ensure our lists are up-to-date.
3262     updateLayerListsIfNeeded();
3263
3264     LayoutPoint offsetFromRoot;
3265     convertToLayerCoords(paintingInfo.rootLayer, offsetFromRoot);
3266
3267     IntRect rootRelativeBounds;
3268     bool rootRelativeBoundsComputed = false;
3269
3270     bool didQuantizeFonts = true;
3271     bool scrollingOnMainThread = true;
3272     Frame* frame = renderer()->frame();
3273 #if ENABLE(THREADED_SCROLLING)
3274     if (frame) {
3275         if (Page* page = frame->page()) {
3276             if (ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator())
3277                 scrollingOnMainThread = scrollingCoordinator->shouldUpdateScrollLayerPositionOnMainThread();
3278         }
3279     }
3280 #endif
3281
3282     // FIXME: We shouldn't have to disable subpixel quantization for overflow clips or subframes once we scroll those
3283     // things on the scrolling thread.
3284     bool needToAdjustSubpixelQuantization = scrollingOnMainThread || (renderer()->hasOverflowClip() && !usesCompositedScrolling()) || (frame && frame->ownerElement());
3285     if (needToAdjustSubpixelQuantization) {
3286         didQuantizeFonts = context->shouldSubpixelQuantizeFonts();
3287         context->setShouldSubpixelQuantizeFonts(false);
3288     }
3289
3290     // Apply clip-path to context.
3291     bool hasClipPath = false;
3292     RenderStyle* style = renderer()->style();
3293     if (renderer()->hasClipPath() && !context->paintingDisabled() && style) {
3294         ASSERT(style->clipPath());
3295         if (style->clipPath()->getOperationType() == ClipPathOperation::SHAPE) {
3296             hasClipPath = true;
3297             context->save();
3298             ShapeClipPathOperation* clipPath = static_cast<ShapeClipPathOperation*>(style->clipPath());
3299
3300             if (!rootRelativeBoundsComputed) {
3301                 rootRelativeBounds = calculateLayerBounds(paintingInfo.rootLayer, &offsetFromRoot, 0);
3302                 rootRelativeBoundsComputed = true;
3303             }
3304
3305             context->clipPath(clipPath->path(rootRelativeBounds), clipPath->windRule());
3306         }
3307 #if ENABLE(SVG)
3308         else if (style->clipPath()->getOperationType() == ClipPathOperation::REFERENCE) {
3309             ReferenceClipPathOperation* referenceClipPathOperation = static_cast<ReferenceClipPathOperation*>(style->clipPath());
3310             Document* document = renderer()->document();
3311             // FIXME: It doesn't work with forward or external SVG references (https://bugs.webkit.org/show_bug.cgi?id=90405)
3312             Element* element = document ? document->getElementById(referenceClipPathOperation->fragment()) : 0;
3313             if (element && element->hasTagName(SVGNames::clipPathTag) && element->renderer()) {
3314                 if (!rootRelativeBoundsComputed) {
3315                     rootRelativeBounds = calculateLayerBounds(paintingInfo.rootLayer, &offsetFromRoot, 0);
3316                     rootRelativeBoundsComputed = true;
3317                 }
3318             
3319                 static_cast<RenderSVGResourceClipper*>(element->renderer())->applyClippingToContext(renderer(), rootRelativeBounds, paintingInfo.paintDirtyRect, context);
3320             }
3321         }
3322 #endif
3323     }
3324
3325     LayerPaintingInfo localPaintingInfo(paintingInfo);
3326 #if ENABLE(CSS_FILTERS)
3327     FilterEffectRendererHelper filterPainter(filterRenderer() && paintsWithFilters());
3328     if (filterPainter.haveFilterEffect() && !context->paintingDisabled()) {
3329         RenderLayerFilterInfo* filterInfo = this->filterInfo();
3330         ASSERT(filterInfo);
3331         LayoutRect filterRepaintRect = filterInfo->dirtySourceRect();
3332         filterRepaintRect.move(offsetFromRoot.x(), offsetFromRoot.y());
3333
3334         if (!rootRelativeBoundsComputed) {
3335             rootRelativeBounds = calculateLayerBounds(paintingInfo.rootLayer, &offsetFromRoot, 0);
3336             rootRelativeBoundsComputed = true;
3337         }
3338
3339         if (filterPainter.prepareFilterEffect(this, rootRelativeBounds, paintingInfo.paintDirtyRect, filterRepaintRect)) {
3340             // Now we know for sure, that the source image will be updated, so we can revert our tracking repaint rect back to zero.
3341             filterInfo->resetDirtySourceRect();
3342
3343             // Rewire the old context to a memory buffer, so that we can capture the contents of the layer.
3344             // NOTE: We saved the old context in the "transparencyLayerContext" local variable, to be able to start a transparency layer
3345             // on the original context and avoid duplicating "beginFilterEffect" after each transpareny layer call. Also, note that 
3346             // beginTransparencyLayers will only create a single lazy transparency layer, even though it is called twice in this method.
3347             context = filterPainter.beginFilterEffect(context);
3348
3349             // Check that we didn't fail to allocate the graphics context for the offscreen buffer.
3350             if (filterPainter.hasStartedFilterEffect()) {
3351                 localPaintingInfo.paintDirtyRect = filterPainter.repaintRect();
3352                 // If the filter needs the full source image, we need to avoid using the clip rectangles.
3353                 // Otherwise, if for example this layer has overflow:hidden, a drop shadow will not compute correctly.
3354                 // Note that we will still apply the clipping on the final rendering of the filter.
3355                 useClipRect = !filterRenderer()->hasFilterThatMovesPixels();
3356             }
3357         }
3358     }
3359 #endif
3360
3361     // Calculate the clip rects we should use only when we need them.
3362     LayoutRect layerBounds;
3363     ClipRect damageRect, clipRectToApply, outlineRect;
3364     LayoutPoint paintOffset;
3365
3366     if (shouldPaintContent || shouldPaintOutline || isPaintingOverlayScrollbars) {
3367         ClipRectsContext clipRectsContext(localPaintingInfo.rootLayer, localPaintingInfo.region, (localPaintFlags & PaintLayerTemporaryClipRects) ? TemporaryClipRects : PaintingClipRects, IgnoreOverlayScrollbarSize, localPaintFlags & PaintLayerPaintingOverflowContents ? IgnoreOverflowClip : RespectOverflowClip);
3368         calculateRects(clipRectsContext, localPaintingInfo.paintDirtyRect, layerBounds, damageRect, clipRectToApply, outlineRect, &offsetFromRoot);
3369         paintOffset = toPoint(layerBounds.location() - renderBoxLocation() + localPaintingInfo.subPixelAccumulation);
3370         if (this == localPaintingInfo.rootLayer)
3371             paintOffset = roundedIntPoint(paintOffset);
3372     }
3373
3374     bool forceBlackText = localPaintingInfo.paintBehavior & PaintBehaviorForceBlackText;
3375     bool selectionOnly  = localPaintingInfo.paintBehavior & PaintBehaviorSelectionOnly;
3376     
3377     // If this layer's renderer is a child of the paintingRoot, we render unconditionally, which
3378     // is done by passing a nil paintingRoot down to our renderer (as if no paintingRoot was ever set).
3379     // Else, our renderer tree may or may not contain the painting root, so we pass that root along
3380     // so it will be tested against as we descend through the renderers.
3381     RenderObject* paintingRootForRenderer = 0;
3382     if (localPaintingInfo.paintingRoot && !renderer()->isDescendantOf(localPaintingInfo.paintingRoot))
3383         paintingRootForRenderer = localPaintingInfo.paintingRoot;
3384
3385     if (localPaintingInfo.overlapTestRequests && isSelfPaintingLayer)
3386         performOverlapTests(*localPaintingInfo.overlapTestRequests, localPaintingInfo.rootLayer, this);
3387
3388     // We want to paint our layer, but only if we intersect the damage rect.
3389     if (this != localPaintingInfo.rootLayer || !(localPaintFlags & PaintLayerPaintingOverflowContents))
3390         shouldPaintContent &= intersectsDamageRect(layerBounds, damageRect.rect(), localPaintingInfo.rootLayer, &offsetFromRoot);
3391     
3392     if (localPaintFlags & PaintLayerPaintingCompositingBackgroundPhase) {
3393         if (shouldPaintContent && !selectionOnly) {
3394             // Begin transparency layers lazily now that we know we have to paint something.
3395             if (haveTransparency)
3396                 beginTransparencyLayers(transparencyLayerContext, localPaintingInfo.rootLayer, localPaintingInfo.paintDirtyRect, localPaintingInfo.paintBehavior);
3397         
3398             if (useClipRect) {
3399                 // Paint our background first, before painting any child layers.
3400                 // Establish the clip used to paint our background.
3401                 clipToRect(localPaintingInfo.rootLayer, context, localPaintingInfo.paintDirtyRect, damageRect, DoNotIncludeSelfForBorderRadius); // Background painting will handle clipping to self.
3402             }
3403             
3404             // Paint the background.
3405             PaintInfo paintInfo(context, pixelSnappedIntRect(damageRect.rect()), PaintPhaseBlockBackground, false, paintingRootForRenderer, localPaintingInfo.region, 0);
3406             renderer()->paint(paintInfo, paintOffset);
3407
3408             if (useClipRect) {
3409                 // Restore the clip.
3410                 restoreClip(context, localPaintingInfo.paintDirtyRect, damageRect);
3411             }
3412         }
3413
3414         // Now walk the sorted list of children with negative z-indices.
3415         paintList(negZOrderList(), context, localPaintingInfo, localPaintFlags);
3416     }
3417     
3418     if (localPaintFlags & PaintLayerPaintingCompositingForegroundPhase) {
3419         // Now establish the appropriate clip and paint our child RenderObjects.
3420         if (shouldPaintContent && !clipRectToApply.isEmpty()) {
3421             // Begin transparency layers lazily now that we know we have to paint something.
3422             if (haveTransparency)
3423                 beginTransparencyLayers(transparencyLayerContext, localPaintingInfo.rootLayer, localPaintingInfo.paintDirtyRect, localPaintingInfo.paintBehavior);
3424
3425             if (useClipRect) {
3426                 // Set up the clip used when painting our children.
3427                 clipToRect(localPaintingInfo.rootLayer, context, localPaintingInfo.paintDirtyRect, clipRectToApply);
3428             }
3429             
3430             PaintInfo paintInfo(context, pixelSnappedIntRect(clipRectToApply.rect()),
3431                                 selectionOnly ? PaintPhaseSelection : PaintPhaseChildBlockBackgrounds,
3432                                 forceBlackText, paintingRootForRenderer, localPaintingInfo.region, 0);
3433             renderer()->paint(paintInfo, paintOffset);
3434             if (!selectionOnly) {
3435                 paintInfo.phase = PaintPhaseFloat;
3436                 renderer()->paint(paintInfo, paintOffset);
3437                 paintInfo.phase = PaintPhaseForeground;
3438                 paintInfo.overlapTestRequests = localPaintingInfo.overlapTestRequests;
3439                 renderer()->paint(paintInfo, paintOffset);
3440                 paintInfo.phase = PaintPhaseChildOutlines;
3441                 renderer()->paint(paintInfo, paintOffset);
3442             }
3443
3444             if (useClipRect) {
3445                 // Now restore our clip.
3446                 restoreClip(context, localPaintingInfo.paintDirtyRect, clipRectToApply);
3447             }
3448         }
3449
3450         if (shouldPaintOutline && !outlineRect.isEmpty()) {
3451             // Paint our own outline
3452             PaintInfo paintInfo(context, pixelSnappedIntRect(outlineRect.rect()), PaintPhaseSelfOutline, false, paintingRootForRenderer, localPaintingInfo.region, 0);
3453             clipToRect(localPaintingInfo.rootLayer, context, localPaintingInfo.paintDirtyRect, outlineRect, DoNotIncludeSelfForBorderRadius);
3454             renderer()->paint(paintInfo, paintOffset);
3455             restoreClip(context, localPaintingInfo.paintDirtyRect, outlineRect);
3456         }
3457     
3458         // Paint any child layers that have overflow.
3459         paintList(m_normalFlowList.get(), context, localPaintingInfo, localPaintFlags);
3460     
3461         // Now walk the sorted list of children with positive z-indices.
3462         paintList(posZOrderList(), context, localPaintingInfo, localPaintFlags);
3463     }
3464
3465     if (isPaintingOverlayScrollbars) {
3466         clipToRect(localPaintingInfo.rootLayer, context, localPaintingInfo.paintDirtyRect, damageRect);
3467         paintOverflowControls(context, roundedIntPoint(paintOffset), pixelSnappedIntRect(damageRect.rect()), true);
3468         restoreClip(context, localPaintingInfo.paintDirtyRect, damageRect);
3469     }
3470
3471 #if ENABLE(CSS_FILTERS)
3472     if (filterPainter.hasStartedFilterEffect()) {
3473         // Apply the correct clipping (ie. overflow: hidden).
3474         clipToRect(localPaintingInfo.rootLayer, transparencyLayerContext, localPaintingInfo.paintDirtyRect, damageRect);
3475         context = filterPainter.applyFilterEffect();
3476         restoreClip(transparencyLayerContext, localPaintingInfo.paintDirtyRect, damageRect);
3477     }
3478 #endif
3479
3480     // Make sure that we now use the original transparency context.
3481     ASSERT(transparencyLayerContext == context);
3482
3483     if ((localPaintFlags & PaintLayerPaintingCompositingMaskPhase) && shouldPaintContent && renderer()->hasMask() && !selectionOnly) {
3484         if (useClipRect)
3485             clipToRect(localPaintingInfo.rootLayer, context, localPaintingInfo.paintDirtyRect, damageRect, DoNotIncludeSelfForBorderRadius); // Mask painting will handle clipping to self.
3486         
3487         // Paint the mask.
3488         PaintInfo paintInfo(context, pixelSnappedIntRect(damageRect.rect()), PaintPhaseMask, false, paintingRootForRenderer, localPaintingInfo.region, 0);
3489         renderer()->paint(paintInfo, paintOffset);
3490         
3491         if (useClipRect) {
3492             // Restore the clip.
3493             restoreClip(context, localPaintingInfo.paintDirtyRect, damageRect);
3494         }
3495     }
3496
3497     // End our transparency layer
3498     if (haveTransparency && m_usedTransparency && !m_paintingInsideReflection) {
3499         context->endTransparencyLayer();
3500         context->restore();
3501         m_usedTransparency = false;
3502     }
3503
3504     // Re-set this to whatever it was before we painted the layer.
3505     if (needToAdjustSubpixelQuantization)
3506         context->setShouldSubpixelQuantizeFonts(didQuantizeFonts);
3507
3508     if (hasClipPath)
3509         context->restore();
3510 }
3511
3512 void RenderLayer::paintList(Vector<RenderLayer*>* list, GraphicsContext* context, const LayerPaintingInfo& paintingInfo, PaintLayerFlags paintFlags)
3513 {
3514     if (!list)
3515         return;
3516
3517     if (!hasSelfPaintingLayerDescendant())
3518         return;
3519
3520 #if !ASSERT_DISABLED
3521     LayerListMutationDetector mutationChecker(this);
3522 #endif
3523
3524     for (size_t i = 0; i < list->size(); ++i) {
3525         RenderLayer* childLayer = list->at(i);
3526         if (!childLayer->isPaginated())
3527             childLayer->paintLayer(context, paintingInfo, paintFlags);
3528         else
3529             paintPaginatedChildLayer(childLayer, context, paintingInfo, paintFlags);
3530     }
3531 }
3532
3533 void RenderLayer::paintPaginatedChildLayer(RenderLayer* childLayer, GraphicsContext* context, const LayerPaintingInfo& paintingInfo, PaintLayerFlags paintFlags)
3534 {
3535     // We need to do multiple passes, breaking up our child layer into strips.
3536     Vector<RenderLayer*> columnLayers;
3537     RenderLayer* ancestorLayer = isNormalFlowOnly() ? parent() : stackingContext();
3538     for (RenderLayer* curr = childLayer->parent(); curr; curr = curr->parent()) {
3539         if (curr->renderer()->hasColumns() && checkContainingBlockChainForPagination(childLayer->renderer(), curr->renderBox()))
3540             columnLayers.append(curr);
3541         if (curr == ancestorLayer)
3542             break;
3543     }
3544
3545     // It is possible for paintLayer() to be called after the child layer ceases to be paginated but before
3546     // updateLayerPositions() is called and resets the isPaginated() flag, see <rdar://problem/10098679>.
3547     // If this is the case, just bail out, since the upcoming call to updateLayerPositions() will repaint the layer.
3548     if (!columnLayers.size())
3549         return;
3550
3551     paintChildLayerIntoColumns(childLayer, context, paintingInfo, paintFlags, columnLayers, columnLayers.size() - 1);
3552 }
3553
3554 void RenderLayer::paintChildLayerIntoColumns(RenderLayer* childLayer, GraphicsContext* context, const LayerPaintingInfo& paintingInfo,
3555     PaintLayerFlags paintFlags, const Vector<RenderLayer*>& columnLayers, size_t colIndex)
3556 {
3557     RenderBlock* columnBlock = toRenderBlock(columnLayers[colIndex]->renderer());
3558
3559     ASSERT(columnBlock && columnBlock->hasColumns());
3560     if (!columnBlock || !columnBlock->hasColumns())
3561         return;
3562     
3563     LayoutPoint layerOffset;
3564     // FIXME: It looks suspicious to call convertToLayerCoords here
3565     // as canUseConvertToLayerCoords is true for this layer.
3566     columnBlock->layer()->convertToLayerCoords(paintingInfo.rootLayer, layerOffset);
3567     
3568     bool isHorizontal = columnBlock->style()->isHorizontalWritingMode();
3569
3570     ColumnInfo* colInfo = columnBlock->columnInfo();
3571     unsigned colCount = columnBlock->columnCount(colInfo);
3572     LayoutUnit currLogicalTopOffset = 0;
3573     for (unsigned i = 0; i < colCount; i++) {
3574         // For each rect, we clip to the rect, and then we adjust our coords.
3575         LayoutRect colRect = columnBlock->columnRectAt(colInfo, i);
3576         columnBlock->flipForWritingMode(colRect);
3577         LayoutUnit logicalLeftOffset = (isHorizontal ? colRect.x() : colRect.y()) - columnBlock->logicalLeftOffsetForContent();
3578         LayoutSize offset;
3579         if (isHorizontal) {
3580             if (colInfo->progressionAxis() == ColumnInfo::InlineAxis)
3581                 offset = LayoutSize(logicalLeftOffset, currLogicalTopOffset);
3582             else
3583                 offset = LayoutSize(0, colRect.y() + currLogicalTopOffset - columnBlock->borderTop() - columnBlock->paddingTop());
3584         } else {
3585             if (colInfo->progressionAxis() == ColumnInfo::InlineAxis)
3586                 offset = LayoutSize(currLogicalTopOffset, logicalLeftOffset);
3587             else
3588                 offset = LayoutSize(colRect.x() + currLogicalTopOffset - columnBlock->borderLeft() - columnBlock->paddingLeft(), 0);
3589         }
3590
3591         colRect.moveBy(layerOffset);
3592
3593         LayoutRect localDirtyRect(paintingInfo.paintDirtyRect);
3594         localDirtyRect.intersect(colRect);
3595         
3596         if (!localDirtyRect.isEmpty()) {
3597             GraphicsContextStateSaver stateSaver(*context);
3598             
3599             // Each strip pushes a clip, since column boxes are specified as being
3600             // like overflow:hidden.
3601             context->clip(pixelSnappedIntRect(colRect));
3602
3603             if (!colIndex) {
3604                 // Apply a translation transform to change where the layer paints.
3605                 TransformationMatrix oldTransform;
3606                 bool oldHasTransform = childLayer->transform();
3607                 if (oldHasTransform)
3608                     oldTransform = *childLayer->transform();
3609                 TransformationMatrix newTransform(oldTransform);
3610                 newTransform.translateRight(roundToInt(offset.width()), roundToInt(offset.height()));
3611                 
3612                 childLayer->m_transform = adoptPtr(new TransformationMatrix(newTransform));
3613                 
3614                 LayerPaintingInfo localPaintingInfo(paintingInfo);
3615                 localPaintingInfo.paintDirtyRect = localDirtyRect;
3616                 childLayer->paintLayer(context, localPaintingInfo, paintFlags);
3617
3618                 if (oldHasTransform)
3619                     childLayer->m_transform = adoptPtr(new TransformationMatrix(oldTransform));
3620                 else
3621                     childLayer->m_transform.clear();
3622             } else {
3623                 // Adjust the transform such that the renderer's upper left corner will paint at (0,0) in user space.
3624                 // This involves subtracting out the position of the layer in our current coordinate space.
3625                 LayoutPoint childOffset;
3626                 columnLayers[colIndex - 1]->convertToLayerCoords(paintingInfo.rootLayer, childOffset);
3627                 TransformationMatrix transform;
3628                 transform.translateRight(roundToInt(childOffset.x() + offset.width()), roundToInt(childOffset.y() + offset.height()));
3629                 
3630                 // Apply the transform.
3631                 context->concatCTM(transform.toAffineTransform());
3632
3633                 // Now do a paint with the root layer shifted to be the next multicol block.
3634                 LayerPaintingInfo columnPaintingInfo(paintingInfo);
3635                 columnPaintingInfo.rootLayer = columnLayers[colIndex - 1];
3636                 columnPaintingInfo.paintDirtyRect = transform.inverse().mapRect(localDirtyRect);
3637                 paintChildLayerIntoColumns(childLayer, context, columnPaintingInfo, paintFlags, columnLayers, colIndex - 1);
3638             }
3639         }
3640
3641         // Move to the next position.
3642         LayoutUnit blockDelta = isHorizontal ? colRect.height() : colRect.width();
3643         if (columnBlock->style()->isFlippedBlocksWritingMode())
3644             currLogicalTopOffset += blockDelta;
3645         else
3646             currLogicalTopOffset -= blockDelta;
3647     }
3648 }
3649
3650 static inline LayoutRect frameVisibleRect(RenderObject* renderer)
3651 {
3652     FrameView* frameView = renderer->document()->view();
3653     if (!frameView)
3654         return LayoutRect();
3655
3656     return frameView->visibleContentRect();
3657 }
3658
3659 bool RenderLayer::hitTest(const HitTestRequest& request, HitTestResult& result)
3660 {
3661     return hitTest(request, result.hitTestLocation(), result);
3662 }
3663
3664 bool RenderLayer::hitTest(const HitTestRequest& request, const HitTestLocation& hitTestLocation, HitTestResult& result)
3665 {
3666     ASSERT(isSelfPaintingLayer() || hasSelfPaintingLayerDescendant());
3667
3668     renderer()->document()->updateLayout();
3669     
3670     LayoutRect hitTestArea = renderer()->isRenderFlowThread() ? toRenderFlowThread(renderer())->borderBoxRect() : renderer()->view()->documentRect();
3671     if (!request.ignoreClipping())
3672         hitTestArea.intersect(frameVisibleRect(renderer()));
3673
3674     RenderLayer* insideLayer = hitTestLayer(this, 0, request, result, hitTestArea, hitTestLocation, false);
3675     if (!insideLayer) {
3676         // We didn't hit any layer. If we are the root layer and the mouse is -- or just was -- down, 
3677         // return ourselves. We do this so mouse events continue getting delivered after a drag has 
3678         // exited the WebView, and so hit testing over a scrollbar hits the content document.
3679         if ((request.active() || request.release()) && isRootLayer()) {
3680             renderer()->updateHitTestResult(result, toRenderView(renderer())->flipForWritingMode(hitTestLocation.point()));
3681             insideLayer = this;
3682         }
3683     }
3684
3685     // Now determine if the result is inside an anchor - if the urlElement isn't already set.
3686     Node* node = result.innerNode();
3687     if (node && !result.URLElement())
3688         result.setURLElement(static_cast<Element*>(node->enclosingLinkEventParentOrSelf()));
3689
3690     // Now return whether we were inside this layer (this will always be true for the root
3691     // layer).
3692     return insideLayer;
3693 }
3694
3695 Node* RenderLayer::enclosingElement() const
3696 {
3697     for (RenderObject* r = renderer(); r; r = r->parent()) {
3698         if (Node* e = r->node())
3699             return e;
3700     }
3701     ASSERT_NOT_REACHED();
3702     return 0;
3703 }
3704
3705 #if ENABLE(DIALOG_ELEMENT)
3706 bool RenderLayer::isInTopLayer() const
3707 {
3708     Node* node = renderer()->node();
3709     return node && node->isElementNode() && toElement(node)->isInTopLayer();
3710 }
3711
3712 bool RenderLayer::isInTopLayerSubtree() const
3713 {
3714     for (const RenderLayer* layer = this; layer; layer = layer->parent()) {
3715         if (layer->isInTopLayer())
3716             return true;
3717     }
3718     return false;
3719 }
3720 #endif
3721
3722 // Compute the z-offset of the point in the transformState.
3723 // This is effectively projecting a ray normal to the plane of ancestor, finding where that
3724 // ray intersects target, and computing the z delta between those two points.
3725 static double computeZOffset(const HitTestingTransformState& transformState)
3726 {
3727     // We got an affine transform, so no z-offset
3728     if (transformState.m_accumulatedTransform.isAffine())
3729         return 0;
3730
3731     // Flatten the point into the target plane
3732     FloatPoint targetPoint = transformState.mappedPoint();
3733     
3734     // Now map the point back through the transform, which computes Z.
3735     FloatPoint3D backmappedPoint = transformState.m_accumulatedTransform.mapPoint(FloatPoint3D(targetPoint));
3736     return backmappedPoint.z();
3737 }
3738
3739 PassRefPtr<HitTestingTransformState> RenderLayer::createLocalTransformState(RenderLayer* rootLayer, RenderLayer* containerLayer,
3740                                         const LayoutRect& hitTestRect, const HitTestLocation& hitTestLocation,
3741                                         const HitTestingTransformState* containerTransformState) const
3742 {
3743     RefPtr<HitTestingTransformState> transformState;
3744     LayoutPoint offset;
3745     if (containerTransformState) {
3746         // If we're already computing transform state, then it's relative to the container (which we know is non-null).
3747         transformState = HitTestingTransformState::create(*containerTransformState);
3748         convertToLayerCoords(containerLayer, offset);
3749     } else {
3750         // If this is the first time we need to make transform state, then base it off of hitTestLocation,
3751         // which is relative to rootLayer.
3752         transformState = HitTestingTransformState::create(hitTestLocation.transformedPoint(), hitTestLocation.transformedRect(), FloatQuad(hitTestRect));
3753         convertToLayerCoords(rootLayer, offset);
3754     }
3755     
3756     RenderObject* containerRenderer = containerLayer ? containerLayer->renderer() : 0;
3757     if (renderer()->shouldUseTransformFromContainer(containerRenderer)) {
3758         TransformationMatrix containerTransform;
3759         renderer()->getTransformFromContainer(containerRenderer, toLayoutSize(offset), containerTransform);
3760         transformState->applyTransform(containerTransform, HitTestingTransformState::AccumulateTransform);
3761     } else {
3762         transformState->translate(offset.x(), offset.y(), HitTestingTransformState::AccumulateTransform);
3763     }
3764     
3765     return transformState;
3766 }
3767
3768
3769 static bool isHitCandidate(const RenderLayer* hitLayer, bool canDepthSort, double* zOffset, const HitTestingTransformState* transformState)
3770 {
3771     if (!hitLayer)
3772         return false;
3773
3774     // The hit layer is depth-sorting with other layers, so just say that it was hit.
3775     if (canDepthSort)
3776         return true;
3777     
3778     // We need to look at z-depth to decide if this layer was hit.
3779     if (zOffset) {
3780         ASSERT(transformState);
3781         // This is actually computing our z, but that's OK because the hitLayer is coplanar with us.
3782         double childZOffset = computeZOffset(*transformState);
3783         if (childZOffset > *zOffset) {
3784             *zOffset = childZOffset;
3785             return true;
3786         }
3787         return false;
3788     }
3789
3790     return true;
3791 }
3792
3793 // hitTestLocation and hitTestRect are relative to rootLayer.
3794 // A 'flattening' layer is one preserves3D() == false.
3795 // transformState.m_accumulatedTransform holds the transform from the containing flattening layer.
3796 // transformState.m_lastPlanarPoint is the hitTestLocation in the plane of the containing flattening layer.
3797 // transformState.m_lastPlanarQuad is the hitTestRect as a quad in the plane of the containing flattening layer.
3798 // 
3799 // If zOffset is non-null (which indicates that the caller wants z offset information), 
3800 //  *zOffset on return is the z offset of the hit point relative to the containing flattening layer.
3801 RenderLayer* RenderLayer::hitTestLayer(RenderLayer* rootLayer, RenderLayer* containerLayer, const HitTestRequest& request, HitTestResult& result,
3802                                        const LayoutRect& hitTestRect, const HitTestLocation& hitTestLocation, bool appliedTransform,
3803                                        const HitTestingTransformState* transformState, double* zOffset)
3804 {
3805     if (!isSelfPaintingLayer() && !hasSelfPaintingLayerDescendant())
3806         return 0;
3807
3808     // The natural thing would be to keep HitTestingTransformState on the stack, but it's big, so we heap-allocate.
3809
3810     // Apply a transform if we have one.
3811     if (transform() && !appliedTransform) {
3812         // Make sure the parent's clip rects have been calculated.
3813         if (parent()) {
3814             ClipRectsContext clipRectsContext(rootLayer, hitTestLocation.region(), RootRelativeClipRects, IncludeOverlayScrollbarSize);
3815             ClipRect clipRect = backgroundClipRect(clipRectsContext);
3816             // Go ahead and test the enclosing clip now.
3817             if (!clipRect.intersects(hitTestLocation))
3818                 return 0;
3819         }
3820
3821         // Create a transform state to accumulate this transform.
3822         RefPtr<HitTestingTransformState> newTransformState = createLocalTransformState(rootLayer, containerLayer, hitTestRect, hitTestLocation, transformState);
3823
3824         // If the transform can't be inverted, then don't hit test this layer at all.
3825         if (!newTransformState->m_accumulatedTransform.isInvertible())
3826             return 0;
3827
3828         // Compute the point and the hit test rect in the coords of this layer by using the values
3829         // from the transformState, which store the point and quad in the coords of the last flattened
3830         // layer, and the accumulated transform which lets up map through preserve-3d layers.
3831         //
3832         // We can't just map hitTestLocation and hitTestRect because they may have been flattened (losing z)
3833         // by our container.
3834         FloatPoint localPoint = newTransformState->mappedPoint();
3835         FloatQuad localPointQuad = newTransformState->mappedQuad();
3836         LayoutRect localHitTestRect = newTransformState->boundsOfMappedArea();
3837         HitTestLocation newHitTestLocation;
3838         if (hitTestLocation.isRectBasedTest())
3839             newHitTestLocation = HitTestLocation(localPoint, localPointQuad);
3840         else
3841             newHitTestLocation = HitTestLocation(localPoint);
3842
3843         // Now do a hit test with the root layer shifted to be us.
3844         return hitTestLayer(this, containerLayer, request, result, localHitTestRect, newHitTestLocation, true, newTransformState.get(), zOffset);
3845     }
3846
3847     // Ensure our lists and 3d status are up-to-date.
3848     updateCompositingAndLayerListsIfNeeded();
3849     update3DTransformedDescendantStatus();
3850
3851     RefPtr<HitTestingTransformState> localTransformState;
3852     if (appliedTransform) {
3853         // We computed the correct state in the caller (above code), so just reference it.
3854         ASSERT(transformState);
3855         localTransformState = const_cast<HitTestingTransformState*>(transformState);
3856     } else if (transformState || m_has3DTransformedDescendant || preserves3D()) {
3857         // We need transform state for the first time, or to offset the container state, so create it here.
3858         localTransformState = createLocalTransformState(rootLayer, containerLayer, hitTestRect, hitTestLocation, transformState);
3859     }
3860
3861     // Check for hit test on backface if backface-visibility is 'hidden'
3862     if (localTransformState && renderer()->style()->backfaceVisibility() == BackfaceVisibilityHidden) {
3863         TransformationMatrix invertedMatrix = localTransformState->m_accumulatedTransform.inverse();
3864         // If the z-vector of the matrix is negative, the back is facing towards the viewer.
3865         if (invertedMatrix.m33() < 0)
3866             return 0;
3867     }
3868
3869     RefPtr<HitTestingTransformState> unflattenedTransformState = localTransformState;
3870     if (localTransformState && !preserves3D()) {
3871         // Keep a copy of the pre-flattening state, for computing z-offsets for the container
3872         unflattenedTransformState = HitTestingTransformState::create(*localTransformState);
3873         // This layer is flattening, so flatten the state passed to descendants.
3874         localTransformState->flatten();
3875     }
3876     
3877     // Calculate the clip rects we should use.
3878     LayoutRect layerBounds;
3879     ClipRect bgRect;
3880     ClipRect fgRect;
3881     ClipRect outlineRect;
3882
3883     ClipRectsContext clipRectsContext(rootLayer, hitTestLocation.region(), RootRelativeClipRects, IncludeOverlayScrollbarSize);
3884     calculateRects(clipRectsContext, hitTestRect, layerBounds, bgRect, fgRect, outlineRect);
3885     
3886     // The following are used for keeping track of the z-depth of the hit point of 3d-transformed
3887     // descendants.
3888     double localZOffset = -numeric_limits<double>::infinity();
3889     double* zOffsetForDescendantsPtr = 0;
3890     double* zOffsetForContentsPtr = 0;
3891     
3892     bool depthSortDescendants = false;
3893     if (preserves3D()) {
3894         depthSortDescendants = true;
3895         // Our layers can depth-test with our container, so share the z depth pointer with the container, if it passed one down.
3896         zOffsetForDescendantsPtr = zOffset ? zOffset : &localZOffset;
3897         zOffsetForContentsPtr = zOffset ? zOffset : &localZOffset;
3898     } else if (m_has3DTransformedDescendant) {
3899         // Flattening layer with 3d children; use a local zOffset pointer to depth-test children and foreground.
3900         depthSortDescendants = true;
3901         zOffsetForDescendantsPtr = zOffset ? zOffset : &localZOffset;
3902         zOffsetForContentsPtr = zOffset ? zOffset : &localZOffset;
3903     } else if (zOffset) {
3904         zOffsetForDescendantsPtr = 0;
3905         // Container needs us to give back a z offset for the hit layer.
3906         zOffsetForContentsPtr = zOffset;
3907     }
3908
3909     // This variable tracks which layer the mouse ends up being inside.
3910     RenderLayer* candidateLayer = 0;
3911
3912     // Begin by walking our list of positive layers from highest z-index down to the lowest z-index.
3913     RenderLayer* hitLayer = hitTestList(posZOrderList(), rootLayer, request, result, hitTestRect, hitTestLocation,
3914                                         localTransformState.get(), zOffsetForDescendantsPtr, zOffset, unflattenedTransformState.get(), depthSortDescendants);
3915     if (hitLayer) {
3916         if (!depthSortDescendants)
3917             return hitLayer;
3918         candidateLayer = hitLayer;
3919     }
3920
3921     // Now check our overflow objects.