Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / 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 "core/rendering/RenderLayer.h"
46
47 #include "core/CSSPropertyNames.h"
48 #include "core/HTMLNames.h"
49 #include "core/css/PseudoStyleRequest.h"
50 #include "core/dom/Document.h"
51 #include "core/dom/shadow/ShadowRoot.h"
52 #include "core/frame/DeprecatedScheduleStyleRecalcDuringLayout.h"
53 #include "core/frame/FrameView.h"
54 #include "core/frame/LocalFrame.h"
55 #include "core/html/HTMLFrameElement.h"
56 #include "core/page/Page.h"
57 #include "core/page/scrolling/ScrollingCoordinator.h"
58 #include "core/rendering/ColumnInfo.h"
59 #include "core/rendering/FilterEffectRenderer.h"
60 #include "core/rendering/HitTestRequest.h"
61 #include "core/rendering/HitTestResult.h"
62 #include "core/rendering/HitTestingTransformState.h"
63 #include "core/rendering/RenderFlowThread.h"
64 #include "core/rendering/RenderGeometryMap.h"
65 #include "core/rendering/RenderInline.h"
66 #include "core/rendering/RenderPart.h"
67 #include "core/rendering/RenderReplica.h"
68 #include "core/rendering/RenderScrollbar.h"
69 #include "core/rendering/RenderScrollbarPart.h"
70 #include "core/rendering/RenderTreeAsText.h"
71 #include "core/rendering/RenderView.h"
72 #include "core/rendering/compositing/CompositedLayerMapping.h"
73 #include "core/rendering/compositing/RenderLayerCompositor.h"
74 #include "core/rendering/svg/ReferenceFilterBuilder.h"
75 #include "platform/LengthFunctions.h"
76 #include "platform/Partitions.h"
77 #include "platform/RuntimeEnabledFeatures.h"
78 #include "platform/TraceEvent.h"
79 #include "platform/geometry/FloatPoint3D.h"
80 #include "platform/geometry/FloatRect.h"
81 #include "platform/geometry/TransformState.h"
82 #include "platform/graphics/filters/ReferenceFilter.h"
83 #include "platform/graphics/filters/SourceGraphic.h"
84 #include "platform/transforms/ScaleTransformOperation.h"
85 #include "platform/transforms/TransformationMatrix.h"
86 #include "platform/transforms/TranslateTransformOperation.h"
87 #include "public/platform/Platform.h"
88 #include "wtf/StdLibExtras.h"
89 #include "wtf/text/CString.h"
90
91 namespace blink {
92
93 namespace {
94
95 static CompositingQueryMode gCompositingQueryMode =
96     CompositingQueriesAreOnlyAllowedInCertainDocumentLifecyclePhases;
97
98 } // namespace
99
100 using namespace HTMLNames;
101
102 RenderLayer::RenderLayer(RenderLayerModelObject* renderer, LayerType type)
103     : m_layerType(type)
104     , m_hasSelfPaintingLayerDescendant(false)
105     , m_hasSelfPaintingLayerDescendantDirty(false)
106     , m_isRootLayer(renderer->isRenderView())
107     , m_usedTransparency(false)
108     , m_visibleContentStatusDirty(true)
109     , m_hasVisibleContent(false)
110     , m_visibleDescendantStatusDirty(false)
111     , m_hasVisibleDescendant(false)
112     , m_hasVisibleNonLayerContent(false)
113     , m_isPaginated(false)
114     , m_3DTransformedDescendantStatusDirty(true)
115     , m_has3DTransformedDescendant(false)
116     , m_containsDirtyOverlayScrollbars(false)
117     , m_hasFilterInfo(false)
118     , m_needsAncestorDependentCompositingInputsUpdate(true)
119     , m_needsDescendantDependentCompositingInputsUpdate(true)
120     , m_childNeedsCompositingInputsUpdate(true)
121     , m_hasCompositingDescendant(false)
122     , m_hasNonCompositedChild(false)
123     , m_shouldIsolateCompositedDescendants(false)
124     , m_lostGroupedMapping(false)
125     , m_renderer(renderer)
126     , m_parent(0)
127     , m_previous(0)
128     , m_next(0)
129     , m_first(0)
130     , m_last(0)
131     , m_staticInlinePosition(0)
132     , m_staticBlockPosition(0)
133     , m_enclosingPaginationLayer(0)
134     , m_potentialCompositingReasonsFromStyle(CompositingReasonNone)
135     , m_compositingReasons(CompositingReasonNone)
136     , m_groupedMapping(0)
137     , m_clipper(*renderer)
138 {
139     updateStackingNode();
140
141     m_isSelfPaintingLayer = shouldBeSelfPaintingLayer();
142
143     if (!renderer->slowFirstChild() && renderer->style()) {
144         m_visibleContentStatusDirty = false;
145         m_hasVisibleContent = renderer->style()->visibility() == VISIBLE;
146     }
147
148     updateScrollableArea();
149 }
150
151 RenderLayer::~RenderLayer()
152 {
153     if (renderer()->frame() && renderer()->frame()->page()) {
154         if (ScrollingCoordinator* scrollingCoordinator = renderer()->frame()->page()->scrollingCoordinator())
155             scrollingCoordinator->willDestroyRenderLayer(this);
156     }
157
158     removeFilterInfoIfNeeded();
159
160     if (groupedMapping()) {
161         DisableCompositingQueryAsserts disabler;
162         groupedMapping()->removeRenderLayerFromSquashingGraphicsLayer(this);
163         setGroupedMapping(0);
164     }
165
166     // Child layers will be deleted by their corresponding render objects, so
167     // we don't need to delete them ourselves.
168
169     clearCompositedLayerMapping(true);
170
171     if (m_reflectionInfo)
172         m_reflectionInfo->destroy();
173 }
174
175 String RenderLayer::debugName() const
176 {
177     if (isReflection()) {
178         return renderer()->parent()->debugName() + " (reflection)";
179     }
180     return renderer()->debugName();
181 }
182
183 RenderLayerCompositor* RenderLayer::compositor() const
184 {
185     if (!renderer()->view())
186         return 0;
187     return renderer()->view()->compositor();
188 }
189
190 void RenderLayer::contentChanged(ContentChangeType changeType)
191 {
192     // updateLayerCompositingState will query compositingReasons for accelerated overflow scrolling.
193     // This is tripped by LayoutTests/compositing/content-changed-chicken-egg.html
194     DisableCompositingQueryAsserts disabler;
195
196     if (changeType == CanvasChanged)
197         compositor()->setNeedsCompositingUpdate(CompositingUpdateAfterCompositingInputChange);
198
199     if (changeType == CanvasContextChanged) {
200         compositor()->setNeedsCompositingUpdate(CompositingUpdateAfterCompositingInputChange);
201
202         // Although we're missing test coverage, we need to call
203         // GraphicsLayer::setContentsToPlatformLayer with the new platform
204         // layer for this canvas.
205         // See http://crbug.com/349195
206         if (hasCompositedLayerMapping())
207             compositedLayerMapping()->setNeedsGraphicsLayerUpdate(GraphicsLayerUpdateSubtree);
208     }
209
210     if (m_compositedLayerMapping)
211         m_compositedLayerMapping->contentChanged(changeType);
212 }
213
214 bool RenderLayer::paintsWithFilters() const
215 {
216     if (!renderer()->hasFilter())
217         return false;
218
219     // https://code.google.com/p/chromium/issues/detail?id=343759
220     DisableCompositingQueryAsserts disabler;
221     return !m_compositedLayerMapping || compositingState() != PaintsIntoOwnBacking;
222 }
223
224 LayoutSize RenderLayer::subpixelAccumulation() const
225 {
226     return m_subpixelAccumulation;
227 }
228
229 void RenderLayer::setSubpixelAccumulation(const LayoutSize& size)
230 {
231     m_subpixelAccumulation = size;
232 }
233
234 void RenderLayer::updateLayerPositionsAfterLayout()
235 {
236     TRACE_EVENT0("blink", "RenderLayer::updateLayerPositionsAfterLayout");
237
238     m_clipper.clearClipRectsIncludingDescendants();
239     updateLayerPositionRecursive();
240
241     {
242         // FIXME: Remove incremental compositing updates after fixing the chicken/egg issues
243         // https://code.google.com/p/chromium/issues/detail?id=343756
244         DisableCompositingQueryAsserts disabler;
245         bool needsPaginationUpdate = isPaginated() || enclosingPaginationLayer();
246         updatePaginationRecursive(needsPaginationUpdate);
247     }
248 }
249
250 void RenderLayer::updateLayerPositionRecursive()
251 {
252     if (m_reflectionInfo)
253         m_reflectionInfo->reflection()->layout();
254
255     // FIXME: We should be able to remove this call because we don't care about
256     // any descendant-dependent flags, but code somewhere else is reading these
257     // flags and depending on us to update them.
258     updateDescendantDependentFlags();
259
260     for (RenderLayer* child = firstChild(); child; child = child->nextSibling())
261         child->updateLayerPositionRecursive();
262 }
263
264 void RenderLayer::updateHasSelfPaintingLayerDescendant() const
265 {
266     ASSERT(m_hasSelfPaintingLayerDescendantDirty);
267
268     m_hasSelfPaintingLayerDescendant = false;
269
270     for (RenderLayer* child = firstChild(); child; child = child->nextSibling()) {
271         if (child->isSelfPaintingLayer() || child->hasSelfPaintingLayerDescendant()) {
272             m_hasSelfPaintingLayerDescendant = true;
273             break;
274         }
275     }
276
277     m_hasSelfPaintingLayerDescendantDirty = false;
278 }
279
280 void RenderLayer::dirtyAncestorChainHasSelfPaintingLayerDescendantStatus()
281 {
282     for (RenderLayer* layer = this; layer; layer = layer->parent()) {
283         layer->m_hasSelfPaintingLayerDescendantDirty = true;
284         // If we have reached a self-painting layer, we know our parent should have a self-painting descendant
285         // in this case, there is no need to dirty our ancestors further.
286         if (layer->isSelfPaintingLayer()) {
287             ASSERT(!parent() || parent()->m_hasSelfPaintingLayerDescendantDirty || parent()->m_hasSelfPaintingLayerDescendant);
288             break;
289         }
290     }
291 }
292
293 bool RenderLayer::scrollsWithViewport() const
294 {
295     return renderer()->style()->position() == FixedPosition && renderer()->containerForFixedPosition() == renderer()->view();
296 }
297
298 bool RenderLayer::scrollsWithRespectTo(const RenderLayer* other) const
299 {
300     if (scrollsWithViewport() != other->scrollsWithViewport())
301         return true;
302     return ancestorScrollingLayer() != other->ancestorScrollingLayer();
303 }
304
305 void RenderLayer::updateTransformationMatrix()
306 {
307     if (m_transform) {
308         RenderBox* box = renderBox();
309         ASSERT(box);
310         m_transform->makeIdentity();
311         box->style()->applyTransform(*m_transform, box->pixelSnappedBorderBoxRect().size(), RenderStyle::IncludeTransformOrigin);
312         makeMatrixRenderable(*m_transform, compositor()->hasAcceleratedCompositing());
313     }
314 }
315
316 void RenderLayer::updateTransform(const RenderStyle* oldStyle, RenderStyle* newStyle)
317 {
318     if (oldStyle && newStyle->transformDataEquivalent(*oldStyle))
319         return;
320
321     // hasTransform() on the renderer is also true when there is transform-style: preserve-3d or perspective set,
322     // so check style too.
323     bool hasTransform = renderer()->hasTransformRelatedProperty() && newStyle->hasTransform();
324     bool had3DTransform = has3DTransform();
325
326     bool hadTransform = m_transform;
327     if (hasTransform != hadTransform) {
328         if (hasTransform)
329             m_transform = adoptPtr(new TransformationMatrix);
330         else
331             m_transform.clear();
332
333         // Layers with transforms act as clip rects roots, so clear the cached clip rects here.
334         m_clipper.clearClipRectsIncludingDescendants();
335     } else if (hasTransform) {
336         m_clipper.clearClipRectsIncludingDescendants(AbsoluteClipRects);
337     }
338
339     updateTransformationMatrix();
340
341     if (had3DTransform != has3DTransform())
342         dirty3DTransformedDescendantStatus();
343 }
344
345 static RenderLayer* enclosingLayerForContainingBlock(RenderLayer* layer)
346 {
347     if (RenderObject* containingBlock = layer->renderer()->containingBlock())
348         return containingBlock->enclosingLayer();
349     return 0;
350 }
351
352 RenderLayer* RenderLayer::renderingContextRoot()
353 {
354     RenderLayer* renderingContext = 0;
355
356     if (shouldPreserve3D())
357         renderingContext = this;
358
359     for (RenderLayer* current = enclosingLayerForContainingBlock(this); current && current->shouldPreserve3D(); current = enclosingLayerForContainingBlock(current))
360         renderingContext = current;
361
362     return renderingContext;
363 }
364
365 TransformationMatrix RenderLayer::currentTransform(RenderStyle::ApplyTransformOrigin applyOrigin) const
366 {
367     if (!m_transform)
368         return TransformationMatrix();
369
370     // m_transform includes transform-origin, so we need to recompute the transform here.
371     if (applyOrigin == RenderStyle::ExcludeTransformOrigin) {
372         RenderBox* box = renderBox();
373         TransformationMatrix currTransform;
374         box->style()->applyTransform(currTransform, box->pixelSnappedBorderBoxRect().size(), RenderStyle::ExcludeTransformOrigin);
375         makeMatrixRenderable(currTransform, compositor()->hasAcceleratedCompositing());
376         return currTransform;
377     }
378
379     return *m_transform;
380 }
381
382 TransformationMatrix RenderLayer::renderableTransform(PaintBehavior paintBehavior) const
383 {
384     if (!m_transform)
385         return TransformationMatrix();
386
387     if (paintBehavior & PaintBehaviorFlattenCompositingLayers) {
388         TransformationMatrix matrix = *m_transform;
389         makeMatrixRenderable(matrix, false /* flatten 3d */);
390         return matrix;
391     }
392
393     return *m_transform;
394 }
395
396 static bool checkContainingBlockChainForPagination(RenderLayerModelObject* renderer, RenderBox* ancestorColumnsRenderer)
397 {
398     RenderView* view = renderer->view();
399     RenderLayerModelObject* prevBlock = renderer;
400     RenderBlock* containingBlock;
401     for (containingBlock = renderer->containingBlock();
402          containingBlock && containingBlock != view && containingBlock != ancestorColumnsRenderer;
403          containingBlock = containingBlock->containingBlock())
404         prevBlock = containingBlock;
405
406     // If the columns block wasn't in our containing block chain, then we aren't paginated by it.
407     if (containingBlock != ancestorColumnsRenderer)
408         return false;
409
410     // If the previous block is absolutely positioned, then we can't be paginated by the columns block.
411     if (prevBlock->isOutOfFlowPositioned())
412         return false;
413
414     // Otherwise we are paginated by the columns block.
415     return true;
416 }
417
418 // Convert a bounding box from flow thread coordinates, relative to |layer|, to visual coordinates, relative to |ancestorLayer|.
419 static void convertFromFlowThreadToVisualBoundingBoxInAncestor(const RenderLayer* layer, const RenderLayer* ancestorLayer, LayoutRect& rect)
420 {
421     RenderLayer* paginationLayer = layer->enclosingPaginationLayer();
422     ASSERT(paginationLayer);
423     RenderFlowThread* flowThread = toRenderFlowThread(paginationLayer->renderer());
424
425     // First make the flow thread rectangle relative to the flow thread, not to |layer|.
426     LayoutPoint offsetWithinPaginationLayer;
427     layer->convertToLayerCoords(paginationLayer, offsetWithinPaginationLayer);
428     rect.moveBy(offsetWithinPaginationLayer);
429
430     // Then make the rectangle visual, relative to the fragmentation context. Split our box up into
431     // the actual fragment boxes that render in the columns/pages and unite those together to get
432     // our true bounding box.
433     rect = flowThread->fragmentsBoundingBox(rect);
434
435     // Finally, make the visual rectangle relative to |ancestorLayer|.
436     // FIXME: Handle nested fragmentation contexts (crbug.com/423076). For now just give up if there
437     // are different pagination layers involved.
438     if (!ancestorLayer->enclosingPaginationLayer() || ancestorLayer->enclosingPaginationLayer() != paginationLayer) {
439         // The easy case. The ancestor layer is not within the pagination layer.
440         paginationLayer->convertToLayerCoords(ancestorLayer, rect);
441         return;
442     }
443     // The ancestor layer is also inside the pagination layer, so we need to subtract the visual
444     // distance from the ancestor layer to the pagination layer.
445     LayoutPoint offsetFromPaginationLayerToAncestor;
446     ancestorLayer->convertToLayerCoords(paginationLayer, offsetFromPaginationLayerToAncestor);
447     offsetFromPaginationLayerToAncestor = flowThread->flowThreadPointToVisualPoint(offsetFromPaginationLayerToAncestor);
448     rect.moveBy(-offsetFromPaginationLayerToAncestor);
449 }
450
451 bool RenderLayer::useRegionBasedColumns() const
452 {
453     return renderer()->document().regionBasedColumnsEnabled();
454 }
455
456 void RenderLayer::updatePaginationRecursive(bool needsPaginationUpdate)
457 {
458     m_isPaginated = false;
459     m_enclosingPaginationLayer = 0;
460
461     if (useRegionBasedColumns() && renderer()->isRenderFlowThread())
462         needsPaginationUpdate = true;
463
464     if (needsPaginationUpdate)
465         updatePagination();
466
467     if (renderer()->hasColumns())
468         needsPaginationUpdate = true;
469
470     for (RenderLayer* child = firstChild(); child; child = child->nextSibling())
471         child->updatePaginationRecursive(needsPaginationUpdate);
472 }
473
474 void RenderLayer::updatePagination()
475 {
476     bool usesRegionBasedColumns = useRegionBasedColumns();
477     if ((!usesRegionBasedColumns && compositingState() != NotComposited) || !parent())
478         return; // FIXME: For now the RenderView can't be paginated.  Eventually printing will move to a model where it is though.
479
480     // The main difference between the paginated booleans for the old column code and the new column code
481     // is that each paginated layer has to paint on its own with the new code. There is no
482     // recurring into child layers. This means that the m_isPaginated bits for the new column code can't just be set on
483     // "roots" that get split and paint all their descendants. Instead each layer has to be checked individually and
484     // genuinely know if it is going to have to split itself up when painting only its contents (and not any other descendant
485     // layers). We track an enclosingPaginationLayer instead of using a simple bit, since we want to be able to get back
486     // to that layer easily.
487     if (usesRegionBasedColumns && renderer()->isRenderFlowThread()) {
488         m_enclosingPaginationLayer = this;
489         return;
490     }
491
492     if (m_stackingNode->isNormalFlowOnly()) {
493         if (usesRegionBasedColumns) {
494             // Content inside a transform is not considered to be paginated, since we simply
495             // paint the transform multiple times in each column, so we don't have to use
496             // fragments for the transformed content.
497             m_enclosingPaginationLayer = parent()->enclosingPaginationLayer();
498             if (m_enclosingPaginationLayer && m_enclosingPaginationLayer->hasTransformRelatedProperty())
499                 m_enclosingPaginationLayer = 0;
500         } else {
501             m_isPaginated = parent()->renderer()->hasColumns();
502         }
503         return;
504     }
505
506     // For the new columns code, we want to walk up our containing block chain looking for an enclosing layer. Once
507     // we find one, then we just check its pagination status.
508     if (usesRegionBasedColumns) {
509         RenderView* view = renderer()->view();
510         RenderBlock* containingBlock;
511         for (containingBlock = renderer()->containingBlock();
512              containingBlock && containingBlock != view;
513              containingBlock = containingBlock->containingBlock()) {
514             if (containingBlock->hasLayer()) {
515                 // Content inside a transform is not considered to be paginated, since we simply
516                 // paint the transform multiple times in each column, so we don't have to use
517                 // fragments for the transformed content.
518                 m_enclosingPaginationLayer = containingBlock->layer()->enclosingPaginationLayer();
519                 if (m_enclosingPaginationLayer && m_enclosingPaginationLayer->hasTransformRelatedProperty())
520                     m_enclosingPaginationLayer = 0;
521                 return;
522             }
523         }
524         return;
525     }
526
527     // If we're not normal flow, then we need to look for a multi-column object between us and our stacking container.
528     RenderLayerStackingNode* ancestorStackingContextNode = m_stackingNode->ancestorStackingContextNode();
529     for (RenderLayer* curr = parent(); curr; curr = curr->parent()) {
530         if (curr->renderer()->hasColumns()) {
531             m_isPaginated = checkContainingBlockChainForPagination(renderer(), curr->renderBox());
532             return;
533         }
534         if (curr->stackingNode() == ancestorStackingContextNode)
535             return;
536     }
537 }
538
539 LayoutPoint RenderLayer::positionFromPaintInvalidationBacking(const RenderObject* renderObject, const RenderLayerModelObject* paintInvalidationContainer, const PaintInvalidationState* paintInvalidationState)
540 {
541     FloatPoint point = renderObject->localToContainerPoint(FloatPoint(), paintInvalidationContainer, 0, 0, paintInvalidationState);
542
543     // FIXME: Eventually we are going to unify coordinates in GraphicsLayer space.
544     if (paintInvalidationContainer && paintInvalidationContainer->layer()->groupedMapping())
545         mapPointToPaintBackingCoordinates(paintInvalidationContainer, point);
546
547     return LayoutPoint(point);
548 }
549
550 void RenderLayer::mapPointToPaintBackingCoordinates(const RenderLayerModelObject* paintInvalidationContainer, FloatPoint& point)
551 {
552     RenderLayer* paintInvalidationLayer = paintInvalidationContainer->layer();
553     if (!paintInvalidationLayer->groupedMapping()) {
554         point.move(paintInvalidationLayer->compositedLayerMapping()->contentOffsetInCompositingLayer());
555         return;
556     }
557
558     RenderLayerModelObject* transformedAncestor = paintInvalidationLayer->enclosingTransformedAncestor()->renderer();
559     if (!transformedAncestor)
560         return;
561
562     // |paintInvalidationContainer| may have a local 2D transform on it, so take that into account when mapping into the space of the
563     // transformed ancestor.
564     point = paintInvalidationContainer->localToContainerPoint(point, transformedAncestor);
565
566     point.moveBy(-paintInvalidationLayer->groupedMapping()->squashingOffsetFromTransformedAncestor());
567 }
568
569 void RenderLayer::mapRectToPaintBackingCoordinates(const RenderLayerModelObject* paintInvalidationContainer, LayoutRect& rect)
570 {
571     RenderLayer* paintInvalidationLayer = paintInvalidationContainer->layer();
572     if (!paintInvalidationLayer->groupedMapping()) {
573         rect.move(paintInvalidationLayer->compositedLayerMapping()->contentOffsetInCompositingLayer());
574         return;
575     }
576
577     RenderLayerModelObject* transformedAncestor = paintInvalidationLayer->enclosingTransformedAncestor()->renderer();
578     if (!transformedAncestor)
579         return;
580
581     // |paintInvalidationContainer| may have a local 2D transform on it, so take that into account when mapping into the space of the
582     // transformed ancestor.
583     rect = LayoutRect(paintInvalidationContainer->localToContainerQuad(FloatRect(rect), transformedAncestor).boundingBox());
584
585     rect.moveBy(-paintInvalidationLayer->groupedMapping()->squashingOffsetFromTransformedAncestor());
586 }
587
588 void RenderLayer::mapRectToPaintInvalidationBacking(const RenderObject* renderObject, const RenderLayerModelObject* paintInvalidationContainer, LayoutRect& rect, const PaintInvalidationState* paintInvalidationState)
589 {
590     if (!paintInvalidationContainer->layer()->groupedMapping()) {
591         renderObject->mapRectToPaintInvalidationBacking(paintInvalidationContainer, rect, paintInvalidationState);
592         return;
593     }
594
595     // This code adjusts the paint invalidation rectangle to be in the space of the transformed ancestor of the grouped (i.e. squashed)
596     // layer. This is because all layers that squash together need to issue paint invalidations w.r.t. a single container that is
597     // an ancestor of all of them, in order to properly take into account any local transforms etc.
598     // FIXME: remove this special-case code that works around the paint invalidation code structure.
599     renderObject->mapRectToPaintInvalidationBacking(paintInvalidationContainer, rect, paintInvalidationState);
600
601     mapRectToPaintBackingCoordinates(paintInvalidationContainer, rect);
602 }
603
604 LayoutRect RenderLayer::computePaintInvalidationRect(const RenderObject* renderObject, const RenderLayer* paintInvalidationContainer, const PaintInvalidationState* paintInvalidationState)
605 {
606     if (!paintInvalidationContainer->groupedMapping())
607         return renderObject->computePaintInvalidationRect(paintInvalidationContainer->renderer(), paintInvalidationState);
608
609     LayoutRect rect = renderObject->clippedOverflowRectForPaintInvalidation(paintInvalidationContainer->renderer(), paintInvalidationState);
610     mapRectToPaintBackingCoordinates(paintInvalidationContainer->renderer(), rect);
611     return rect;
612 }
613
614 void RenderLayer::dirtyVisibleContentStatus()
615 {
616     m_visibleContentStatusDirty = true;
617     if (parent())
618         parent()->dirtyAncestorChainVisibleDescendantStatus();
619 }
620
621 void RenderLayer::potentiallyDirtyVisibleContentStatus(EVisibility visibility)
622 {
623     if (m_visibleContentStatusDirty)
624         return;
625     if (hasVisibleContent() == (visibility == VISIBLE))
626         return;
627     dirtyVisibleContentStatus();
628 }
629
630 void RenderLayer::dirtyAncestorChainVisibleDescendantStatus()
631 {
632     for (RenderLayer* layer = this; layer; layer = layer->parent()) {
633         if (layer->m_visibleDescendantStatusDirty)
634             break;
635
636         layer->m_visibleDescendantStatusDirty = true;
637     }
638 }
639
640 // FIXME: this is quite brute-force. We could be more efficient if we were to
641 // track state and update it as appropriate as changes are made in the Render tree.
642 void RenderLayer::updateScrollingStateAfterCompositingChange()
643 {
644     TRACE_EVENT0("blink", "RenderLayer::updateScrollingStateAfterCompositingChange");
645     m_hasVisibleNonLayerContent = false;
646     for (RenderObject* r = renderer()->slowFirstChild(); r; r = r->nextSibling()) {
647         if (!r->hasLayer()) {
648             m_hasVisibleNonLayerContent = true;
649             break;
650         }
651     }
652
653     m_hasNonCompositedChild = false;
654     for (RenderLayer* child = firstChild(); child; child = child->nextSibling()) {
655         if (child->compositingState() == NotComposited) {
656             m_hasNonCompositedChild = true;
657             return;
658         }
659     }
660 }
661
662 // The descendant-dependent flags system is badly broken because we clean dirty
663 // bits in upward tree walks, which means we need to call updateDescendantDependentFlags
664 // at every node in the tree to fully clean all the dirty bits. While we'll in
665 // the process of fixing this issue, updateDescendantDependentFlagsForEntireSubtree
666 // provides a big hammer for actually cleaning all the dirty bits in a subtree.
667 //
668 // FIXME: Remove this function once the descendant-dependent flags system keeps
669 // its dirty bits scoped to subtrees.
670 void RenderLayer::updateDescendantDependentFlagsForEntireSubtree()
671 {
672     updateDescendantDependentFlags();
673
674     for (RenderLayer* child = firstChild(); child; child = child->nextSibling())
675         child->updateDescendantDependentFlagsForEntireSubtree();
676 }
677
678 void RenderLayer::updateDescendantDependentFlags()
679 {
680     if (m_visibleDescendantStatusDirty) {
681         m_hasVisibleDescendant = false;
682
683         for (RenderLayer* child = firstChild(); child; child = child->nextSibling()) {
684             child->updateDescendantDependentFlags();
685
686             if (child->m_hasVisibleContent || child->m_hasVisibleDescendant) {
687                 m_hasVisibleDescendant = true;
688                 break;
689             }
690         }
691
692         m_visibleDescendantStatusDirty = false;
693     }
694
695     if (m_visibleContentStatusDirty) {
696         bool previouslyHasVisibleContent = m_hasVisibleContent;
697         if (renderer()->style()->visibility() == VISIBLE)
698             m_hasVisibleContent = true;
699         else {
700             // layer may be hidden but still have some visible content, check for this
701             m_hasVisibleContent = false;
702             RenderObject* r = renderer()->slowFirstChild();
703             while (r) {
704                 if (r->style()->visibility() == VISIBLE && !r->hasLayer()) {
705                     m_hasVisibleContent = true;
706                     break;
707                 }
708                 RenderObject* rendererFirstChild = r->slowFirstChild();
709                 if (rendererFirstChild && !r->hasLayer())
710                     r = rendererFirstChild;
711                 else if (r->nextSibling())
712                     r = r->nextSibling();
713                 else {
714                     do {
715                         r = r->parent();
716                         if (r == renderer())
717                             r = 0;
718                     } while (r && !r->nextSibling());
719                     if (r)
720                         r = r->nextSibling();
721                 }
722             }
723         }
724         m_visibleContentStatusDirty = false;
725
726         if (hasVisibleContent() != previouslyHasVisibleContent) {
727             setNeedsCompositingInputsUpdate();
728             // We need to tell m_renderer to recheck its rect because we
729             // pretend that invisible RenderObjects have 0x0 rects. Changing
730             // visibility therefore changes our rect and we need to visit
731             // this RenderObject during the invalidateTreeIfNeeded walk.
732             m_renderer->setMayNeedPaintInvalidation(true);
733         }
734     }
735 }
736
737 void RenderLayer::dirty3DTransformedDescendantStatus()
738 {
739     RenderLayerStackingNode* stackingNode = m_stackingNode->ancestorStackingContextNode();
740     if (!stackingNode)
741         return;
742
743     stackingNode->layer()->m_3DTransformedDescendantStatusDirty = true;
744
745     // This propagates up through preserve-3d hierarchies to the enclosing flattening layer.
746     // Note that preserves3D() creates stacking context, so we can just run up the stacking containers.
747     while (stackingNode && stackingNode->layer()->preserves3D()) {
748         stackingNode->layer()->m_3DTransformedDescendantStatusDirty = true;
749         stackingNode = stackingNode->ancestorStackingContextNode();
750     }
751 }
752
753 // Return true if this layer or any preserve-3d descendants have 3d.
754 bool RenderLayer::update3DTransformedDescendantStatus()
755 {
756     if (m_3DTransformedDescendantStatusDirty) {
757         m_has3DTransformedDescendant = false;
758
759         m_stackingNode->updateZOrderLists();
760
761         // Transformed or preserve-3d descendants can only be in the z-order lists, not
762         // in the normal flow list, so we only need to check those.
763         RenderLayerStackingNodeIterator iterator(*m_stackingNode.get(), PositiveZOrderChildren | NegativeZOrderChildren);
764         while (RenderLayerStackingNode* node = iterator.next())
765             m_has3DTransformedDescendant |= node->layer()->update3DTransformedDescendantStatus();
766
767         m_3DTransformedDescendantStatusDirty = false;
768     }
769
770     // If we live in a 3d hierarchy, then the layer at the root of that hierarchy needs
771     // the m_has3DTransformedDescendant set.
772     if (preserves3D())
773         return has3DTransform() || m_has3DTransformedDescendant;
774
775     return has3DTransform();
776 }
777
778 IntSize RenderLayer::size() const
779 {
780     if (renderer()->isInline() && renderer()->isRenderInline())
781         return toRenderInline(renderer())->linesBoundingBox().size();
782
783     // FIXME: Is snapping the size really needed here?
784     if (RenderBox* box = renderBox())
785         return pixelSnappedIntSize(box->size(), box->location());
786
787     return IntSize();
788 }
789
790 LayoutPoint RenderLayer::location() const
791 {
792     LayoutPoint localPoint;
793     LayoutSize inlineBoundingBoxOffset; // We don't put this into the RenderLayer x/y for inlines, so we need to subtract it out when done.
794
795     if (renderer()->isInline() && renderer()->isRenderInline()) {
796         RenderInline* inlineFlow = toRenderInline(renderer());
797         IntRect lineBox = inlineFlow->linesBoundingBox();
798         inlineBoundingBoxOffset = toSize(lineBox.location());
799         localPoint += inlineBoundingBoxOffset;
800     } else if (RenderBox* box = renderBox()) {
801         localPoint += box->topLeftLocationOffset();
802     }
803
804     if (!renderer()->isOutOfFlowPositioned() && renderer()->parent()) {
805         // We must adjust our position by walking up the render tree looking for the
806         // nearest enclosing object with a layer.
807         RenderObject* curr = renderer()->parent();
808         while (curr && !curr->hasLayer()) {
809             if (curr->isBox() && !curr->isTableRow()) {
810                 // Rows and cells share the same coordinate space (that of the section).
811                 // Omit them when computing our xpos/ypos.
812                 localPoint += toRenderBox(curr)->topLeftLocationOffset();
813             }
814             curr = curr->parent();
815         }
816         if (curr->isBox() && curr->isTableRow()) {
817             // Put ourselves into the row coordinate space.
818             localPoint -= toRenderBox(curr)->topLeftLocationOffset();
819         }
820     }
821
822     // Subtract our parent's scroll offset.
823     if (renderer()->isOutOfFlowPositioned() && enclosingPositionedAncestor()) {
824         RenderLayer* positionedParent = enclosingPositionedAncestor();
825
826         // For positioned layers, we subtract out the enclosing positioned layer's scroll offset.
827         if (positionedParent->renderer()->hasOverflowClip()) {
828             LayoutSize offset = positionedParent->renderBox()->scrolledContentOffset();
829             localPoint -= offset;
830         }
831
832         if (positionedParent->renderer()->isRelPositioned() && positionedParent->renderer()->isRenderInline()) {
833             LayoutSize offset = toRenderInline(positionedParent->renderer())->offsetForInFlowPositionedInline(*toRenderBox(renderer()));
834             localPoint += offset;
835         }
836     } else if (parent()) {
837         // FIXME: This code is very wrong, but luckily only needed in the old/current multicol
838         // implementation. The compositing system doesn't understand columns and we're hacking
839         // around that fact by faking the position of the RenderLayers when we think we'll end up
840         // being composited.
841         if (hasStyleDeterminedDirectCompositingReasons() && !useRegionBasedColumns()) {
842             // FIXME: Composited layers ignore pagination, so about the best we can do is make sure they're offset into the appropriate column.
843             // They won't split across columns properly.
844             if (!parent()->renderer()->hasColumns() && parent()->renderer()->isDocumentElement() && renderer()->view()->hasColumns())
845                 localPoint += renderer()->view()->columnOffset(localPoint);
846             else
847                 localPoint += parent()->renderer()->columnOffset(localPoint);
848         }
849
850         if (parent()->renderer()->hasOverflowClip()) {
851             IntSize scrollOffset = parent()->renderBox()->scrolledContentOffset();
852             localPoint -= scrollOffset;
853         }
854     }
855
856     localPoint.move(offsetForInFlowPosition());
857
858     // FIXME: We'd really like to just get rid of the concept of a layer rectangle and rely on the renderers.
859     localPoint -= inlineBoundingBoxOffset;
860
861     return localPoint;
862 }
863
864 const LayoutSize RenderLayer::offsetForInFlowPosition() const
865 {
866     return renderer()->isRelPositioned() ? toRenderBoxModelObject(renderer())->offsetForInFlowPosition() : LayoutSize();
867 }
868
869 TransformationMatrix RenderLayer::perspectiveTransform() const
870 {
871     if (!renderer()->hasTransformRelatedProperty())
872         return TransformationMatrix();
873
874     RenderStyle* style = renderer()->style();
875     if (!style->hasPerspective())
876         return TransformationMatrix();
877
878     // Maybe fetch the perspective from the backing?
879     const IntRect borderBox = toRenderBox(renderer())->pixelSnappedBorderBoxRect();
880     const float boxWidth = borderBox.width();
881     const float boxHeight = borderBox.height();
882
883     float perspectiveOriginX = floatValueForLength(style->perspectiveOriginX(), boxWidth);
884     float perspectiveOriginY = floatValueForLength(style->perspectiveOriginY(), boxHeight);
885
886     // A perspective origin of 0,0 makes the vanishing point in the center of the element.
887     // We want it to be in the top-left, so subtract half the height and width.
888     perspectiveOriginX -= boxWidth / 2.0f;
889     perspectiveOriginY -= boxHeight / 2.0f;
890
891     TransformationMatrix t;
892     t.translate(perspectiveOriginX, perspectiveOriginY);
893     t.applyPerspective(style->perspective());
894     t.translate(-perspectiveOriginX, -perspectiveOriginY);
895
896     return t;
897 }
898
899 FloatPoint RenderLayer::perspectiveOrigin() const
900 {
901     if (!renderer()->hasTransformRelatedProperty())
902         return FloatPoint();
903
904     const LayoutRect borderBox = toRenderBox(renderer())->borderBoxRect();
905     RenderStyle* style = renderer()->style();
906
907     return FloatPoint(floatValueForLength(style->perspectiveOriginX(), borderBox.width().toFloat()), floatValueForLength(style->perspectiveOriginY(), borderBox.height().toFloat()));
908 }
909
910 static inline bool isFixedPositionedContainer(RenderLayer* layer)
911 {
912     return layer->isRootLayer() || layer->hasTransformRelatedProperty();
913 }
914
915 RenderLayer* RenderLayer::enclosingPositionedAncestor() const
916 {
917     RenderLayer* curr = parent();
918     while (curr && !curr->isPositionedContainer())
919         curr = curr->parent();
920
921     return curr;
922 }
923
924 RenderLayer* RenderLayer::enclosingTransformedAncestor() const
925 {
926     RenderLayer* curr = parent();
927     while (curr && !curr->isRootLayer() && !curr->renderer()->hasTransformRelatedProperty())
928         curr = curr->parent();
929
930     return curr;
931 }
932
933 LayoutPoint RenderLayer::computeOffsetFromTransformedAncestor() const
934 {
935     const AncestorDependentCompositingInputs& properties = ancestorDependentCompositingInputs();
936
937     TransformState transformState(TransformState::ApplyTransformDirection, FloatPoint());
938     // FIXME: add a test that checks flipped writing mode and ApplyContainerFlip are correct.
939     renderer()->mapLocalToContainer(properties.transformAncestor ? properties.transformAncestor->renderer() : 0, transformState, ApplyContainerFlip);
940     transformState.flatten();
941     return LayoutPoint(transformState.lastPlanarPoint());
942 }
943
944 const RenderLayer* RenderLayer::compositingContainer() const
945 {
946     if (stackingNode()->isNormalFlowOnly())
947         return parent();
948     if (RenderLayerStackingNode* ancestorStackingNode = stackingNode()->ancestorStackingContextNode())
949         return ancestorStackingNode->layer();
950     return 0;
951 }
952
953 bool RenderLayer::isPaintInvalidationContainer() const
954 {
955     return compositingState() == PaintsIntoOwnBacking || compositingState() == PaintsIntoGroupedBacking;
956 }
957
958 // Note: enclosingCompositingLayer does not include squashed layers. Compositing stacking children of squashed layers
959 // receive graphics layers that are parented to the compositing ancestor of the squashed layer.
960 RenderLayer* RenderLayer::enclosingLayerWithCompositedLayerMapping(IncludeSelfOrNot includeSelf) const
961 {
962     ASSERT(isAllowedToQueryCompositingState());
963
964     if ((includeSelf == IncludeSelf) && compositingState() != NotComposited && compositingState() != PaintsIntoGroupedBacking)
965         return const_cast<RenderLayer*>(this);
966
967     for (const RenderLayer* curr = compositingContainer(); curr; curr = curr->compositingContainer()) {
968         if (curr->compositingState() != NotComposited && curr->compositingState() != PaintsIntoGroupedBacking)
969             return const_cast<RenderLayer*>(curr);
970     }
971
972     return 0;
973 }
974
975 // Return the enclosingCompositedLayerForPaintInvalidation for the given RenderLayer
976 // including crossing frame boundaries.
977 RenderLayer* RenderLayer::enclosingLayerForPaintInvalidationCrossingFrameBoundaries() const
978 {
979     const RenderLayer* layer = this;
980     RenderLayer* compositedLayer = 0;
981     while (!compositedLayer) {
982         compositedLayer = layer->enclosingLayerForPaintInvalidation();
983         if (!compositedLayer) {
984             RenderObject* owner = layer->renderer()->frame()->ownerRenderer();
985             if (!owner)
986                 break;
987             layer = owner->enclosingLayer();
988         }
989     }
990     return compositedLayer;
991 }
992
993 RenderLayer* RenderLayer::enclosingLayerForPaintInvalidation() const
994 {
995     ASSERT(isAllowedToQueryCompositingState());
996
997     if (isPaintInvalidationContainer())
998         return const_cast<RenderLayer*>(this);
999
1000     for (const RenderLayer* curr = parent(); curr; curr = curr->parent()) {
1001         if (curr->isPaintInvalidationContainer())
1002             return const_cast<RenderLayer*>(curr);
1003     }
1004
1005     return 0;
1006 }
1007
1008 void RenderLayer::setNeedsCompositingInputsUpdate()
1009 {
1010     m_needsAncestorDependentCompositingInputsUpdate = true;
1011     m_needsDescendantDependentCompositingInputsUpdate = true;
1012
1013     for (RenderLayer* current = this; current && !current->m_childNeedsCompositingInputsUpdate; current = current->parent())
1014         current->m_childNeedsCompositingInputsUpdate = true;
1015
1016     compositor()->setNeedsCompositingUpdate(CompositingUpdateAfterCompositingInputChange);
1017 }
1018
1019 void RenderLayer::updateAncestorDependentCompositingInputs(const AncestorDependentCompositingInputs& compositingInputs)
1020 {
1021     m_ancestorDependentCompositingInputs = compositingInputs;
1022     m_needsAncestorDependentCompositingInputsUpdate = false;
1023 }
1024
1025 void RenderLayer::updateDescendantDependentCompositingInputs(const DescendantDependentCompositingInputs& compositingInputs)
1026 {
1027     m_descendantDependentCompositingInputs = compositingInputs;
1028     m_needsDescendantDependentCompositingInputsUpdate = false;
1029 }
1030
1031 void RenderLayer::didUpdateCompositingInputs()
1032 {
1033     ASSERT(!needsCompositingInputsUpdate());
1034     m_childNeedsCompositingInputsUpdate = false;
1035     if (m_scrollableArea)
1036         m_scrollableArea->updateNeedsCompositedScrolling();
1037 }
1038
1039 void RenderLayer::setCompositingReasons(CompositingReasons reasons, CompositingReasons mask)
1040 {
1041     if ((compositingReasons() & mask) == (reasons & mask))
1042         return;
1043     m_compositingReasons = (reasons & mask) | (compositingReasons() & ~mask);
1044 }
1045
1046 void RenderLayer::setHasCompositingDescendant(bool hasCompositingDescendant)
1047 {
1048     if (m_hasCompositingDescendant == static_cast<unsigned>(hasCompositingDescendant))
1049         return;
1050
1051     m_hasCompositingDescendant = hasCompositingDescendant;
1052
1053     if (hasCompositedLayerMapping())
1054         compositedLayerMapping()->setNeedsGraphicsLayerUpdate(GraphicsLayerUpdateLocal);
1055 }
1056
1057 void RenderLayer::setShouldIsolateCompositedDescendants(bool shouldIsolateCompositedDescendants)
1058 {
1059     if (m_shouldIsolateCompositedDescendants == static_cast<unsigned>(shouldIsolateCompositedDescendants))
1060         return;
1061
1062     m_shouldIsolateCompositedDescendants = shouldIsolateCompositedDescendants;
1063
1064     if (hasCompositedLayerMapping())
1065         compositedLayerMapping()->setNeedsGraphicsLayerUpdate(GraphicsLayerUpdateLocal);
1066 }
1067
1068 bool RenderLayer::hasAncestorWithFilterOutsets() const
1069 {
1070     for (const RenderLayer* curr = this; curr; curr = curr->parent()) {
1071         RenderLayerModelObject* renderer = curr->renderer();
1072         if (renderer->style()->hasFilterOutsets())
1073             return true;
1074     }
1075     return false;
1076 }
1077
1078 RenderLayer* RenderLayer::transparentPaintingAncestor()
1079 {
1080     if (hasCompositedLayerMapping())
1081         return 0;
1082
1083     for (RenderLayer* curr = parent(); curr; curr = curr->parent()) {
1084         if (curr->hasCompositedLayerMapping())
1085             return 0;
1086         if (curr->isTransparent())
1087             return curr;
1088     }
1089     return 0;
1090 }
1091
1092 static void expandClipRectForDescendantsAndReflection(LayoutRect& clipRect, const RenderLayer* layer, const RenderLayer* rootLayer,
1093     RenderLayer::TransparencyClipBoxBehavior transparencyBehavior, const LayoutSize& subPixelAccumulation, PaintBehavior paintBehavior)
1094 {
1095     // If we have a mask, then the clip is limited to the border box area (and there is
1096     // no need to examine child layers).
1097     if (!layer->renderer()->hasMask()) {
1098         // Note: we don't have to walk z-order lists since transparent elements always establish
1099         // a stacking container. This means we can just walk the layer tree directly.
1100         for (RenderLayer* curr = layer->firstChild(); curr; curr = curr->nextSibling()) {
1101             if (!layer->reflectionInfo() || layer->reflectionInfo()->reflectionLayer() != curr)
1102                 clipRect.unite(RenderLayer::transparencyClipBox(curr, rootLayer, transparencyBehavior, RenderLayer::DescendantsOfTransparencyClipBox, subPixelAccumulation, paintBehavior));
1103         }
1104     }
1105
1106     // If we have a reflection, then we need to account for that when we push the clip.  Reflect our entire
1107     // current transparencyClipBox to catch all child layers.
1108     // FIXME: Accelerated compositing will eventually want to do something smart here to avoid incorporating this
1109     // size into the parent layer.
1110     if (layer->renderer()->hasReflection()) {
1111         LayoutPoint delta;
1112         layer->convertToLayerCoords(rootLayer, delta);
1113         clipRect.move(-delta.x(), -delta.y());
1114         clipRect.unite(layer->renderBox()->reflectedRect(clipRect));
1115         clipRect.moveBy(delta);
1116     }
1117 }
1118
1119 LayoutRect RenderLayer::transparencyClipBox(const RenderLayer* layer, const RenderLayer* rootLayer, TransparencyClipBoxBehavior transparencyBehavior,
1120     TransparencyClipBoxMode transparencyMode, const LayoutSize& subPixelAccumulation, PaintBehavior paintBehavior)
1121 {
1122     // FIXME: Although this function completely ignores CSS-imposed clipping, we did already intersect with the
1123     // paintDirtyRect, and that should cut down on the amount we have to paint.  Still it
1124     // would be better to respect clips.
1125
1126     if (rootLayer != layer && ((transparencyBehavior == PaintingTransparencyClipBox && layer->paintsWithTransform(paintBehavior))
1127         || (transparencyBehavior == HitTestingTransparencyClipBox && layer->hasTransformRelatedProperty()))) {
1128         // The best we can do here is to use enclosed bounding boxes to establish a "fuzzy" enough clip to encompass
1129         // the transformed layer and all of its children.
1130         const RenderLayer* paginationLayer = transparencyMode == DescendantsOfTransparencyClipBox ? layer->enclosingPaginationLayer() : 0;
1131         const RenderLayer* rootLayerForTransform = paginationLayer ? paginationLayer : rootLayer;
1132         LayoutPoint delta;
1133         layer->convertToLayerCoords(rootLayerForTransform, delta);
1134
1135         delta.move(subPixelAccumulation);
1136         IntPoint pixelSnappedDelta = roundedIntPoint(delta);
1137         TransformationMatrix transform;
1138         transform.translate(pixelSnappedDelta.x(), pixelSnappedDelta.y());
1139         transform = transform * *layer->transform();
1140
1141         // We don't use fragment boxes when collecting a transformed layer's bounding box, since it always
1142         // paints unfragmented.
1143         LayoutRect clipRect = layer->physicalBoundingBox(layer);
1144         expandClipRectForDescendantsAndReflection(clipRect, layer, layer, transparencyBehavior, subPixelAccumulation, paintBehavior);
1145         layer->renderer()->style()->filterOutsets().expandRect(clipRect);
1146         LayoutRect result = transform.mapRect(clipRect);
1147         if (!paginationLayer)
1148             return result;
1149
1150         // We have to break up the transformed extent across our columns.
1151         // Split our box up into the actual fragment boxes that render in the columns/pages and unite those together to
1152         // get our true bounding box.
1153         RenderFlowThread* enclosingFlowThread = toRenderFlowThread(paginationLayer->renderer());
1154         result = enclosingFlowThread->fragmentsBoundingBox(result);
1155
1156         LayoutPoint rootLayerDelta;
1157         paginationLayer->convertToLayerCoords(rootLayer, rootLayerDelta);
1158         result.moveBy(rootLayerDelta);
1159         return result;
1160     }
1161
1162     LayoutRect clipRect = layer->fragmentsBoundingBox(rootLayer);
1163     expandClipRectForDescendantsAndReflection(clipRect, layer, rootLayer, transparencyBehavior, subPixelAccumulation, paintBehavior);
1164     layer->renderer()->style()->filterOutsets().expandRect(clipRect);
1165     clipRect.move(subPixelAccumulation);
1166     return clipRect;
1167 }
1168
1169 LayoutRect RenderLayer::paintingExtent(const RenderLayer* rootLayer, const LayoutRect& paintDirtyRect, const LayoutSize& subPixelAccumulation, PaintBehavior paintBehavior)
1170 {
1171     return intersection(transparencyClipBox(this, rootLayer, PaintingTransparencyClipBox, RootOfTransparencyClipBox, subPixelAccumulation, paintBehavior), paintDirtyRect);
1172 }
1173
1174 void* RenderLayer::operator new(size_t sz)
1175 {
1176     return partitionAlloc(Partitions::getRenderingPartition(), sz);
1177 }
1178
1179 void RenderLayer::operator delete(void* ptr)
1180 {
1181     partitionFree(ptr);
1182 }
1183
1184 void RenderLayer::addChild(RenderLayer* child, RenderLayer* beforeChild)
1185 {
1186     RenderLayer* prevSibling = beforeChild ? beforeChild->previousSibling() : lastChild();
1187     if (prevSibling) {
1188         child->setPreviousSibling(prevSibling);
1189         prevSibling->setNextSibling(child);
1190         ASSERT(prevSibling != child);
1191     } else
1192         setFirstChild(child);
1193
1194     if (beforeChild) {
1195         beforeChild->setPreviousSibling(child);
1196         child->setNextSibling(beforeChild);
1197         ASSERT(beforeChild != child);
1198     } else
1199         setLastChild(child);
1200
1201     child->m_parent = this;
1202
1203     setNeedsCompositingInputsUpdate();
1204
1205     if (child->stackingNode()->isNormalFlowOnly())
1206         m_stackingNode->dirtyNormalFlowList();
1207
1208     if (!child->stackingNode()->isNormalFlowOnly() || child->firstChild()) {
1209         // Dirty the z-order list in which we are contained. The ancestorStackingContextNode() can be null in the
1210         // case where we're building up generated content layers. This is ok, since the lists will start
1211         // off dirty in that case anyway.
1212         child->stackingNode()->dirtyStackingContextZOrderLists();
1213     }
1214
1215     dirtyAncestorChainVisibleDescendantStatus();
1216     dirtyAncestorChainHasSelfPaintingLayerDescendantStatus();
1217
1218     child->updateDescendantDependentFlags();
1219 }
1220
1221 RenderLayer* RenderLayer::removeChild(RenderLayer* oldChild)
1222 {
1223     if (oldChild->previousSibling())
1224         oldChild->previousSibling()->setNextSibling(oldChild->nextSibling());
1225     if (oldChild->nextSibling())
1226         oldChild->nextSibling()->setPreviousSibling(oldChild->previousSibling());
1227
1228     if (m_first == oldChild)
1229         m_first = oldChild->nextSibling();
1230     if (m_last == oldChild)
1231         m_last = oldChild->previousSibling();
1232
1233     if (oldChild->stackingNode()->isNormalFlowOnly())
1234         m_stackingNode->dirtyNormalFlowList();
1235     if (!oldChild->stackingNode()->isNormalFlowOnly() || oldChild->firstChild()) {
1236         // Dirty the z-order list in which we are contained.  When called via the
1237         // reattachment process in removeOnlyThisLayer, the layer may already be disconnected
1238         // from the main layer tree, so we need to null-check the
1239         // |stackingContext| value.
1240         oldChild->stackingNode()->dirtyStackingContextZOrderLists();
1241     }
1242
1243     if (renderer()->style()->visibility() != VISIBLE)
1244         dirtyVisibleContentStatus();
1245
1246     oldChild->setPreviousSibling(0);
1247     oldChild->setNextSibling(0);
1248     oldChild->m_parent = 0;
1249
1250     dirtyAncestorChainHasSelfPaintingLayerDescendantStatus();
1251
1252     oldChild->updateDescendantDependentFlags();
1253
1254     if (oldChild->m_hasVisibleContent || oldChild->m_hasVisibleDescendant)
1255         dirtyAncestorChainVisibleDescendantStatus();
1256
1257     return oldChild;
1258 }
1259
1260 void RenderLayer::removeOnlyThisLayer()
1261 {
1262     if (!m_parent)
1263         return;
1264
1265     m_clipper.clearClipRectsIncludingDescendants();
1266
1267     RenderLayer* nextSib = nextSibling();
1268
1269     // Remove the child reflection layer before moving other child layers.
1270     // The reflection layer should not be moved to the parent.
1271     if (m_reflectionInfo)
1272         removeChild(m_reflectionInfo->reflectionLayer());
1273
1274     // Now walk our kids and reattach them to our parent.
1275     RenderLayer* current = m_first;
1276     while (current) {
1277         RenderLayer* next = current->nextSibling();
1278         removeChild(current);
1279         m_parent->addChild(current, nextSib);
1280
1281         // FIXME: We should call a specialized version of this function.
1282         current->updateLayerPositionsAfterLayout();
1283         current = next;
1284     }
1285
1286     // Remove us from the parent.
1287     m_parent->removeChild(this);
1288     m_renderer->destroyLayer();
1289 }
1290
1291 void RenderLayer::insertOnlyThisLayer()
1292 {
1293     if (!m_parent && renderer()->parent()) {
1294         // We need to connect ourselves when our renderer() has a parent.
1295         // Find our enclosingLayer and add ourselves.
1296         RenderLayer* parentLayer = renderer()->parent()->enclosingLayer();
1297         ASSERT(parentLayer);
1298         RenderLayer* beforeChild = !parentLayer->reflectionInfo() || parentLayer->reflectionInfo()->reflectionLayer() != this ? renderer()->parent()->findNextLayer(parentLayer, renderer()) : 0;
1299         parentLayer->addChild(this, beforeChild);
1300     }
1301
1302     // Remove all descendant layers from the hierarchy and add them to the new position.
1303     for (RenderObject* curr = renderer()->slowFirstChild(); curr; curr = curr->nextSibling())
1304         curr->moveLayers(m_parent, this);
1305
1306     // Clear out all the clip rects.
1307     m_clipper.clearClipRectsIncludingDescendants();
1308 }
1309
1310 // Returns the layer reached on the walk up towards the ancestor.
1311 static inline const RenderLayer* accumulateOffsetTowardsAncestor(const RenderLayer* layer, const RenderLayer* ancestorLayer, LayoutPoint& location)
1312 {
1313     ASSERT(ancestorLayer != layer);
1314
1315     const RenderLayerModelObject* renderer = layer->renderer();
1316     EPosition position = renderer->style()->position();
1317
1318     // FIXME: Positioning of out-of-flow(fixed, absolute) elements collected in a RenderFlowThread
1319     // may need to be revisited in a future patch.
1320     // If the fixed renderer is inside a RenderFlowThread, we should not compute location using localToAbsolute,
1321     // since localToAbsolute maps the coordinates from flow thread to regions coordinates and regions can be
1322     // positioned in a completely different place in the viewport (RenderView).
1323     if (position == FixedPosition && (!ancestorLayer || ancestorLayer == renderer->view()->layer())) {
1324         // If the fixed layer's container is the root, just add in the offset of the view. We can obtain this by calling
1325         // localToAbsolute() on the RenderView.
1326         FloatPoint absPos = renderer->localToAbsolute(FloatPoint(), IsFixed);
1327         location += LayoutSize(absPos.x(), absPos.y());
1328         return ancestorLayer;
1329     }
1330
1331     // For the fixed positioned elements inside a render flow thread, we should also skip the code path below
1332     // Otherwise, for the case of ancestorLayer == rootLayer and fixed positioned element child of a transformed
1333     // element in render flow thread, we will hit the fixed positioned container before hitting the ancestor layer.
1334     if (position == FixedPosition) {
1335         // For a fixed layers, we need to walk up to the root to see if there's a fixed position container
1336         // (e.g. a transformed layer). It's an error to call convertToLayerCoords() across a layer with a transform,
1337         // so we should always find the ancestor at or before we find the fixed position container.
1338         RenderLayer* fixedPositionContainerLayer = 0;
1339         bool foundAncestor = false;
1340         for (RenderLayer* currLayer = layer->parent(); currLayer; currLayer = currLayer->parent()) {
1341             if (currLayer == ancestorLayer)
1342                 foundAncestor = true;
1343
1344             if (isFixedPositionedContainer(currLayer)) {
1345                 fixedPositionContainerLayer = currLayer;
1346                 ASSERT_UNUSED(foundAncestor, foundAncestor);
1347                 break;
1348             }
1349         }
1350
1351         ASSERT(fixedPositionContainerLayer); // We should have hit the RenderView's layer at least.
1352
1353         if (fixedPositionContainerLayer != ancestorLayer) {
1354             LayoutPoint fixedContainerCoords;
1355             layer->convertToLayerCoords(fixedPositionContainerLayer, fixedContainerCoords);
1356
1357             LayoutPoint ancestorCoords;
1358             ancestorLayer->convertToLayerCoords(fixedPositionContainerLayer, ancestorCoords);
1359
1360             location += (fixedContainerCoords - ancestorCoords);
1361         } else {
1362             location += toSize(layer->location());
1363         }
1364         return ancestorLayer;
1365     }
1366
1367     RenderLayer* parentLayer;
1368     if (position == AbsolutePosition || position == FixedPosition) {
1369         // Do what enclosingPositionedAncestor() does, but check for ancestorLayer along the way.
1370         parentLayer = layer->parent();
1371         bool foundAncestorFirst = false;
1372         while (parentLayer) {
1373             // RenderFlowThread is a positioned container, child of RenderView, positioned at (0,0).
1374             // This implies that, for out-of-flow positioned elements inside a RenderFlowThread,
1375             // we are bailing out before reaching root layer.
1376             if (parentLayer->isPositionedContainer())
1377                 break;
1378
1379             if (parentLayer == ancestorLayer) {
1380                 foundAncestorFirst = true;
1381                 break;
1382             }
1383
1384             parentLayer = parentLayer->parent();
1385         }
1386
1387         // We should not reach RenderView layer past the RenderFlowThread layer for any
1388         // children of the RenderFlowThread.
1389         ASSERT(!renderer->flowThreadContainingBlock() || parentLayer != renderer->view()->layer());
1390
1391         if (foundAncestorFirst) {
1392             // Found ancestorLayer before the abs. positioned container, so compute offset of both relative
1393             // to enclosingPositionedAncestor and subtract.
1394             RenderLayer* positionedAncestor = parentLayer->enclosingPositionedAncestor();
1395
1396             LayoutPoint thisCoords;
1397             layer->convertToLayerCoords(positionedAncestor, thisCoords);
1398
1399             LayoutPoint ancestorCoords;
1400             ancestorLayer->convertToLayerCoords(positionedAncestor, ancestorCoords);
1401
1402             location += (thisCoords - ancestorCoords);
1403             return ancestorLayer;
1404         }
1405     } else
1406         parentLayer = layer->parent();
1407
1408     if (!parentLayer)
1409         return 0;
1410
1411     location += toSize(layer->location());
1412     return parentLayer;
1413 }
1414
1415 void RenderLayer::convertToLayerCoords(const RenderLayer* ancestorLayer, LayoutPoint& location) const
1416 {
1417     if (ancestorLayer == this)
1418         return;
1419
1420     const RenderLayer* currLayer = this;
1421     while (currLayer && currLayer != ancestorLayer)
1422         currLayer = accumulateOffsetTowardsAncestor(currLayer, ancestorLayer, location);
1423 }
1424
1425 void RenderLayer::convertToLayerCoords(const RenderLayer* ancestorLayer, LayoutRect& rect) const
1426 {
1427     LayoutPoint delta;
1428     convertToLayerCoords(ancestorLayer, delta);
1429     rect.moveBy(delta);
1430 }
1431
1432 void RenderLayer::didUpdateNeedsCompositedScrolling()
1433 {
1434     updateSelfPaintingLayer();
1435 }
1436
1437 void RenderLayer::updateReflectionInfo(const RenderStyle* oldStyle)
1438 {
1439     ASSERT(!oldStyle || !renderer()->style()->reflectionDataEquivalent(oldStyle));
1440     if (renderer()->hasReflection()) {
1441         if (!m_reflectionInfo)
1442             m_reflectionInfo = adoptPtrWillBeNoop(new RenderLayerReflectionInfo(*renderBox()));
1443         m_reflectionInfo->updateAfterStyleChange(oldStyle);
1444     } else if (m_reflectionInfo) {
1445         m_reflectionInfo->destroy();
1446         m_reflectionInfo = nullptr;
1447     }
1448 }
1449
1450 void RenderLayer::updateStackingNode()
1451 {
1452     if (requiresStackingNode())
1453         m_stackingNode = adoptPtr(new RenderLayerStackingNode(this));
1454     else
1455         m_stackingNode = nullptr;
1456 }
1457
1458 void RenderLayer::updateScrollableArea()
1459 {
1460     if (requiresScrollableArea())
1461         m_scrollableArea = adoptPtr(new RenderLayerScrollableArea(*this));
1462     else
1463         m_scrollableArea = nullptr;
1464 }
1465
1466 bool RenderLayer::hasOverflowControls() const
1467 {
1468     return m_scrollableArea && (m_scrollableArea->hasScrollbar() || m_scrollableArea->hasScrollCorner() || renderer()->style()->resize() != RESIZE_NONE);
1469 }
1470
1471 void RenderLayer::collectFragments(LayerFragments& fragments, const RenderLayer* rootLayer, const LayoutRect& dirtyRect,
1472     ClipRectsCacheSlot clipRectsCacheSlot, OverlayScrollbarSizeRelevancy inOverlayScrollbarSizeRelevancy, ShouldRespectOverflowClip respectOverflowClip, const LayoutPoint* offsetFromRoot,
1473     const LayoutSize& subPixelAccumulation, const LayoutRect* layerBoundingBox)
1474 {
1475     if (!enclosingPaginationLayer() || hasTransformRelatedProperty()) {
1476         // For unpaginated layers, there is only one fragment.
1477         LayerFragment fragment;
1478         ClipRectsContext clipRectsContext(rootLayer, clipRectsCacheSlot, inOverlayScrollbarSizeRelevancy, subPixelAccumulation);
1479         if (respectOverflowClip == IgnoreOverflowClip)
1480             clipRectsContext.setIgnoreOverflowClip();
1481         clipper().calculateRects(clipRectsContext, dirtyRect, fragment.layerBounds, fragment.backgroundRect, fragment.foregroundRect, fragment.outlineRect, offsetFromRoot);
1482         fragments.append(fragment);
1483         return;
1484     }
1485
1486     // Compute our offset within the enclosing pagination layer.
1487     LayoutPoint offsetWithinPaginatedLayer;
1488     convertToLayerCoords(enclosingPaginationLayer(), offsetWithinPaginatedLayer);
1489
1490     // Calculate clip rects relative to the enclosingPaginationLayer. The purpose of this call is to determine our bounds clipped to intermediate
1491     // layers between us and the pagination context. It's important to minimize the number of fragments we need to create and this helps with that.
1492     ClipRectsContext paginationClipRectsContext(enclosingPaginationLayer(), clipRectsCacheSlot, inOverlayScrollbarSizeRelevancy);
1493     if (respectOverflowClip == IgnoreOverflowClip)
1494         paginationClipRectsContext.setIgnoreOverflowClip();
1495     LayoutRect layerBoundsInFlowThread;
1496     ClipRect backgroundRectInFlowThread;
1497     ClipRect foregroundRectInFlowThread;
1498     ClipRect outlineRectInFlowThread;
1499     clipper().calculateRects(paginationClipRectsContext, PaintInfo::infiniteRect(), layerBoundsInFlowThread, backgroundRectInFlowThread, foregroundRectInFlowThread,
1500         outlineRectInFlowThread, &offsetWithinPaginatedLayer);
1501
1502     // Take our bounding box within the flow thread and clip it.
1503     LayoutRect layerBoundingBoxInFlowThread = layerBoundingBox ? *layerBoundingBox : physicalBoundingBox(enclosingPaginationLayer(), &offsetWithinPaginatedLayer);
1504     layerBoundingBoxInFlowThread.intersect(backgroundRectInFlowThread.rect());
1505
1506     // Make the dirty rect relative to the fragmentation context (multicol container, etc.).
1507     RenderFlowThread* enclosingFlowThread = toRenderFlowThread(enclosingPaginationLayer()->renderer());
1508     LayoutPoint offsetOfPaginationLayerFromRoot;
1509     // FIXME: more work needed if there are nested pagination layers.
1510     if (rootLayer != enclosingPaginationLayer() && rootLayer->enclosingPaginationLayer() == enclosingPaginationLayer()) {
1511         // The root layer is inside the fragmentation context. So we need to look inside it and find
1512         // the visual offset from the fragmentation context.
1513         LayoutPoint flowThreadOffset;
1514         rootLayer->convertToLayerCoords(enclosingPaginationLayer(), flowThreadOffset);
1515         offsetOfPaginationLayerFromRoot = -enclosingFlowThread->flowThreadPointToVisualPoint(flowThreadOffset);
1516     } else {
1517         enclosingPaginationLayer()->convertToLayerCoords(rootLayer, offsetOfPaginationLayerFromRoot);
1518     }
1519     LayoutRect dirtyRectInFlowThread(dirtyRect);
1520     dirtyRectInFlowThread.moveBy(-offsetOfPaginationLayerFromRoot);
1521
1522     // Tell the flow thread to collect the fragments. We pass enough information to create a minimal number of fragments based off the pages/columns
1523     // that intersect the actual dirtyRect as well as the pages/columns that intersect our layer's bounding box.
1524     enclosingFlowThread->collectLayerFragments(fragments, layerBoundingBoxInFlowThread, dirtyRectInFlowThread);
1525
1526     if (fragments.isEmpty())
1527         return;
1528
1529     // Get the parent clip rects of the pagination layer, since we need to intersect with that when painting column contents.
1530     ClipRect ancestorClipRect = dirtyRect;
1531     if (enclosingPaginationLayer()->parent()) {
1532         ClipRectsContext clipRectsContext(rootLayer, clipRectsCacheSlot, inOverlayScrollbarSizeRelevancy);
1533         if (respectOverflowClip == IgnoreOverflowClip)
1534             clipRectsContext.setIgnoreOverflowClip();
1535         ancestorClipRect = enclosingPaginationLayer()->clipper().backgroundClipRect(clipRectsContext);
1536         ancestorClipRect.intersect(dirtyRect);
1537     }
1538
1539     for (size_t i = 0; i < fragments.size(); ++i) {
1540         LayerFragment& fragment = fragments.at(i);
1541
1542         // Set our four rects with all clipping applied that was internal to the flow thread.
1543         fragment.setRects(layerBoundsInFlowThread, backgroundRectInFlowThread, foregroundRectInFlowThread, outlineRectInFlowThread);
1544
1545         // Shift to the root-relative physical position used when painting the flow thread in this fragment.
1546         fragment.moveBy(fragment.paginationOffset + offsetOfPaginationLayerFromRoot);
1547
1548         // Intersect the fragment with our ancestor's background clip so that e.g., columns in an overflow:hidden block are
1549         // properly clipped by the overflow.
1550         fragment.intersect(ancestorClipRect.rect());
1551
1552         // Now intersect with our pagination clip. This will typically mean we're just intersecting the dirty rect with the column
1553         // clip, so the column clip ends up being all we apply.
1554         fragment.intersect(fragment.paginationClip);
1555     }
1556 }
1557
1558 static inline LayoutRect frameVisibleRect(RenderObject* renderer)
1559 {
1560     FrameView* frameView = renderer->document().view();
1561     if (!frameView)
1562         return LayoutRect();
1563
1564     return frameView->visibleContentRect();
1565 }
1566
1567 bool RenderLayer::hitTest(const HitTestRequest& request, HitTestResult& result)
1568 {
1569     return hitTest(request, result.hitTestLocation(), result);
1570 }
1571
1572 bool RenderLayer::hitTest(const HitTestRequest& request, const HitTestLocation& hitTestLocation, HitTestResult& result)
1573 {
1574     ASSERT(isSelfPaintingLayer() || hasSelfPaintingLayerDescendant());
1575
1576     // RenderView should make sure to update layout before entering hit testing
1577     ASSERT(!renderer()->frame()->view()->layoutPending());
1578     ASSERT(!renderer()->document().renderView()->needsLayout());
1579
1580     // Start with frameVisibleRect to ensure we include the scrollbars.
1581     LayoutRect hitTestArea = frameVisibleRect(renderer());
1582     if (request.ignoreClipping())
1583         hitTestArea.unite(renderer()->view()->documentRect());
1584
1585     RenderLayer* insideLayer = hitTestLayer(this, 0, request, result, hitTestArea, hitTestLocation, false);
1586     if (!insideLayer) {
1587         // We didn't hit any layer. If we are the root layer and the mouse is -- or just was -- down,
1588         // return ourselves. We do this so mouse events continue getting delivered after a drag has
1589         // exited the WebView, and so hit testing over a scrollbar hits the content document.
1590         // In addtion, it is possible for the mouse to stay in the document but there is no element.
1591         // At that time, the events of the mouse should be fired.
1592         LayoutPoint hitPoint = hitTestLocation.point();
1593         if (!request.isChildFrameHitTest() && ((request.active() || request.release()) || (request.move() && hitTestArea.contains(hitPoint.x(), hitPoint.y()))) && isRootLayer()) {
1594             renderer()->updateHitTestResult(result, toRenderView(renderer())->flipForWritingMode(hitTestLocation.point()));
1595             insideLayer = this;
1596         }
1597     }
1598
1599     // Now determine if the result is inside an anchor - if the urlElement isn't already set.
1600     Node* node = result.innerNode();
1601     if (node && !result.URLElement())
1602         result.setURLElement(node->enclosingLinkEventParentOrSelf());
1603
1604     // Now return whether we were inside this layer (this will always be true for the root
1605     // layer).
1606     return insideLayer;
1607 }
1608
1609 Node* RenderLayer::enclosingElement() const
1610 {
1611     for (RenderObject* r = renderer(); r; r = r->parent()) {
1612         if (Node* e = r->node())
1613             return e;
1614     }
1615     ASSERT_NOT_REACHED();
1616     return 0;
1617 }
1618
1619 bool RenderLayer::isInTopLayer() const
1620 {
1621     Node* node = renderer()->node();
1622     return node && node->isElementNode() && toElement(node)->isInTopLayer();
1623 }
1624
1625 // Compute the z-offset of the point in the transformState.
1626 // This is effectively projecting a ray normal to the plane of ancestor, finding where that
1627 // ray intersects target, and computing the z delta between those two points.
1628 static double computeZOffset(const HitTestingTransformState& transformState)
1629 {
1630     // We got an affine transform, so no z-offset
1631     if (transformState.m_accumulatedTransform.isAffine())
1632         return 0;
1633
1634     // Flatten the point into the target plane
1635     FloatPoint targetPoint = transformState.mappedPoint();
1636
1637     // Now map the point back through the transform, which computes Z.
1638     FloatPoint3D backmappedPoint = transformState.m_accumulatedTransform.mapPoint(FloatPoint3D(targetPoint));
1639     return backmappedPoint.z();
1640 }
1641
1642 PassRefPtr<HitTestingTransformState> RenderLayer::createLocalTransformState(RenderLayer* rootLayer, RenderLayer* containerLayer,
1643                                         const LayoutRect& hitTestRect, const HitTestLocation& hitTestLocation,
1644                                         const HitTestingTransformState* containerTransformState,
1645                                         const LayoutPoint& translationOffset) const
1646 {
1647     RefPtr<HitTestingTransformState> transformState;
1648     LayoutPoint offset;
1649     if (containerTransformState) {
1650         // If we're already computing transform state, then it's relative to the container (which we know is non-null).
1651         transformState = HitTestingTransformState::create(*containerTransformState);
1652         convertToLayerCoords(containerLayer, offset);
1653     } else {
1654         // If this is the first time we need to make transform state, then base it off of hitTestLocation,
1655         // which is relative to rootLayer.
1656         transformState = HitTestingTransformState::create(hitTestLocation.transformedPoint(), hitTestLocation.transformedRect(), FloatQuad(hitTestRect));
1657         convertToLayerCoords(rootLayer, offset);
1658     }
1659     offset.moveBy(translationOffset);
1660
1661     RenderObject* containerRenderer = containerLayer ? containerLayer->renderer() : 0;
1662     if (renderer()->shouldUseTransformFromContainer(containerRenderer)) {
1663         TransformationMatrix containerTransform;
1664         renderer()->getTransformFromContainer(containerRenderer, toLayoutSize(offset), containerTransform);
1665         transformState->applyTransform(containerTransform, HitTestingTransformState::AccumulateTransform);
1666     } else {
1667         transformState->translate(offset.x(), offset.y(), HitTestingTransformState::AccumulateTransform);
1668     }
1669
1670     return transformState;
1671 }
1672
1673
1674 static bool isHitCandidate(const RenderLayer* hitLayer, bool canDepthSort, double* zOffset, const HitTestingTransformState* transformState)
1675 {
1676     if (!hitLayer)
1677         return false;
1678
1679     // The hit layer is depth-sorting with other layers, so just say that it was hit.
1680     if (canDepthSort)
1681         return true;
1682
1683     // We need to look at z-depth to decide if this layer was hit.
1684     if (zOffset) {
1685         ASSERT(transformState);
1686         // This is actually computing our z, but that's OK because the hitLayer is coplanar with us.
1687         double childZOffset = computeZOffset(*transformState);
1688         if (childZOffset > *zOffset) {
1689             *zOffset = childZOffset;
1690             return true;
1691         }
1692         return false;
1693     }
1694
1695     return true;
1696 }
1697
1698 // hitTestLocation and hitTestRect are relative to rootLayer.
1699 // A 'flattening' layer is one preserves3D() == false.
1700 // transformState.m_accumulatedTransform holds the transform from the containing flattening layer.
1701 // transformState.m_lastPlanarPoint is the hitTestLocation in the plane of the containing flattening layer.
1702 // transformState.m_lastPlanarQuad is the hitTestRect as a quad in the plane of the containing flattening layer.
1703 //
1704 // If zOffset is non-null (which indicates that the caller wants z offset information),
1705 //  *zOffset on return is the z offset of the hit point relative to the containing flattening layer.
1706 RenderLayer* RenderLayer::hitTestLayer(RenderLayer* rootLayer, RenderLayer* containerLayer, const HitTestRequest& request, HitTestResult& result,
1707                                        const LayoutRect& hitTestRect, const HitTestLocation& hitTestLocation, bool appliedTransform,
1708                                        const HitTestingTransformState* transformState, double* zOffset)
1709 {
1710     if (!isSelfPaintingLayer() && !hasSelfPaintingLayerDescendant())
1711         return 0;
1712
1713     // The natural thing would be to keep HitTestingTransformState on the stack, but it's big, so we heap-allocate.
1714
1715     // Apply a transform if we have one.
1716     if (transform() && !appliedTransform) {
1717         if (enclosingPaginationLayer())
1718             return hitTestTransformedLayerInFragments(rootLayer, containerLayer, request, result, hitTestRect, hitTestLocation, transformState, zOffset);
1719
1720         // Make sure the parent's clip rects have been calculated.
1721         if (parent()) {
1722             ClipRect clipRect = clipper().backgroundClipRect(ClipRectsContext(rootLayer, RootRelativeClipRects, IncludeOverlayScrollbarSize));
1723             // Go ahead and test the enclosing clip now.
1724             if (!clipRect.intersects(hitTestLocation))
1725                 return 0;
1726         }
1727
1728         return hitTestLayerByApplyingTransform(rootLayer, containerLayer, request, result, hitTestRect, hitTestLocation, transformState, zOffset);
1729     }
1730
1731     // Ensure our lists and 3d status are up-to-date.
1732     m_stackingNode->updateLayerListsIfNeeded();
1733     update3DTransformedDescendantStatus();
1734
1735     RefPtr<HitTestingTransformState> localTransformState;
1736     if (appliedTransform) {
1737         // We computed the correct state in the caller (above code), so just reference it.
1738         ASSERT(transformState);
1739         localTransformState = const_cast<HitTestingTransformState*>(transformState);
1740     } else if (transformState || m_has3DTransformedDescendant || preserves3D()) {
1741         // We need transform state for the first time, or to offset the container state, so create it here.
1742         localTransformState = createLocalTransformState(rootLayer, containerLayer, hitTestRect, hitTestLocation, transformState);
1743     }
1744
1745     // Check for hit test on backface if backface-visibility is 'hidden'
1746     if (localTransformState && renderer()->style()->backfaceVisibility() == BackfaceVisibilityHidden) {
1747         TransformationMatrix invertedMatrix = localTransformState->m_accumulatedTransform.inverse();
1748         // If the z-vector of the matrix is negative, the back is facing towards the viewer.
1749         if (invertedMatrix.m33() < 0)
1750             return 0;
1751     }
1752
1753     RefPtr<HitTestingTransformState> unflattenedTransformState = localTransformState;
1754     if (localTransformState && !preserves3D()) {
1755         // Keep a copy of the pre-flattening state, for computing z-offsets for the container
1756         unflattenedTransformState = HitTestingTransformState::create(*localTransformState);
1757         // This layer is flattening, so flatten the state passed to descendants.
1758         localTransformState->flatten();
1759     }
1760
1761     // The following are used for keeping track of the z-depth of the hit point of 3d-transformed
1762     // descendants.
1763     double localZOffset = -std::numeric_limits<double>::infinity();
1764     double* zOffsetForDescendantsPtr = 0;
1765     double* zOffsetForContentsPtr = 0;
1766
1767     bool depthSortDescendants = false;
1768     if (preserves3D()) {
1769         depthSortDescendants = true;
1770         // Our layers can depth-test with our container, so share the z depth pointer with the container, if it passed one down.
1771         zOffsetForDescendantsPtr = zOffset ? zOffset : &localZOffset;
1772         zOffsetForContentsPtr = zOffset ? zOffset : &localZOffset;
1773     } else if (zOffset) {
1774         zOffsetForDescendantsPtr = 0;
1775         // Container needs us to give back a z offset for the hit layer.
1776         zOffsetForContentsPtr = zOffset;
1777     }
1778
1779     // This variable tracks which layer the mouse ends up being inside.
1780     RenderLayer* candidateLayer = 0;
1781
1782     // Begin by walking our list of positive layers from highest z-index down to the lowest z-index.
1783     RenderLayer* hitLayer = hitTestChildren(PositiveZOrderChildren, rootLayer, request, result, hitTestRect, hitTestLocation,
1784                                         localTransformState.get(), zOffsetForDescendantsPtr, zOffset, unflattenedTransformState.get(), depthSortDescendants);
1785     if (hitLayer) {
1786         if (!depthSortDescendants)
1787             return hitLayer;
1788         candidateLayer = hitLayer;
1789     }
1790
1791     // Now check our overflow objects.
1792     hitLayer = hitTestChildren(NormalFlowChildren, rootLayer, request, result, hitTestRect, hitTestLocation,
1793                            localTransformState.get(), zOffsetForDescendantsPtr, zOffset, unflattenedTransformState.get(), depthSortDescendants);
1794     if (hitLayer) {
1795         if (!depthSortDescendants)
1796             return hitLayer;
1797         candidateLayer = hitLayer;
1798     }
1799
1800     // Collect the fragments. This will compute the clip rectangles for each layer fragment.
1801     LayerFragments layerFragments;
1802     collectFragments(layerFragments, rootLayer, hitTestRect, RootRelativeClipRects, IncludeOverlayScrollbarSize);
1803
1804     if (m_scrollableArea && m_scrollableArea->hitTestResizerInFragments(layerFragments, hitTestLocation)) {
1805         renderer()->updateHitTestResult(result, hitTestLocation.point());
1806         return this;
1807     }
1808
1809     // Next we want to see if the mouse pos is inside the child RenderObjects of the layer. Check
1810     // every fragment in reverse order.
1811     if (isSelfPaintingLayer()) {
1812         // Hit test with a temporary HitTestResult, because we only want to commit to 'result' if we know we're frontmost.
1813         HitTestResult tempResult(result.hitTestLocation());
1814         bool insideFragmentForegroundRect = false;
1815         if (hitTestContentsForFragments(layerFragments, request, tempResult, hitTestLocation, HitTestDescendants, insideFragmentForegroundRect)
1816             && isHitCandidate(this, false, zOffsetForContentsPtr, unflattenedTransformState.get())) {
1817             if (result.isRectBasedTest())
1818                 result.append(tempResult);
1819             else
1820                 result = tempResult;
1821             if (!depthSortDescendants)
1822                 return this;
1823             // Foreground can depth-sort with descendant layers, so keep this as a candidate.
1824             candidateLayer = this;
1825         } else if (insideFragmentForegroundRect && result.isRectBasedTest())
1826             result.append(tempResult);
1827     }
1828
1829     // Now check our negative z-index children.
1830     hitLayer = hitTestChildren(NegativeZOrderChildren, rootLayer, request, result, hitTestRect, hitTestLocation,
1831         localTransformState.get(), zOffsetForDescendantsPtr, zOffset, unflattenedTransformState.get(), depthSortDescendants);
1832     if (hitLayer) {
1833         if (!depthSortDescendants)
1834             return hitLayer;
1835         candidateLayer = hitLayer;
1836     }
1837
1838     // If we found a layer, return. Child layers, and foreground always render in front of background.
1839     if (candidateLayer)
1840         return candidateLayer;
1841
1842     if (isSelfPaintingLayer()) {
1843         HitTestResult tempResult(result.hitTestLocation());
1844         bool insideFragmentBackgroundRect = false;
1845         if (hitTestContentsForFragments(layerFragments, request, tempResult, hitTestLocation, HitTestSelf, insideFragmentBackgroundRect)
1846             && isHitCandidate(this, false, zOffsetForContentsPtr, unflattenedTransformState.get())) {
1847             if (result.isRectBasedTest())
1848                 result.append(tempResult);
1849             else
1850                 result = tempResult;
1851             return this;
1852         }
1853         if (insideFragmentBackgroundRect && result.isRectBasedTest())
1854             result.append(tempResult);
1855     }
1856
1857     return 0;
1858 }
1859
1860 bool RenderLayer::hitTestContentsForFragments(const LayerFragments& layerFragments, const HitTestRequest& request, HitTestResult& result,
1861     const HitTestLocation& hitTestLocation, HitTestFilter hitTestFilter, bool& insideClipRect) const
1862 {
1863     if (layerFragments.isEmpty())
1864         return false;
1865
1866     for (int i = layerFragments.size() - 1; i >= 0; --i) {
1867         const LayerFragment& fragment = layerFragments.at(i);
1868         if ((hitTestFilter == HitTestSelf && !fragment.backgroundRect.intersects(hitTestLocation))
1869             || (hitTestFilter == HitTestDescendants && !fragment.foregroundRect.intersects(hitTestLocation)))
1870             continue;
1871         insideClipRect = true;
1872         if (hitTestContents(request, result, fragment.layerBounds, hitTestLocation, hitTestFilter))
1873             return true;
1874     }
1875
1876     return false;
1877 }
1878
1879 RenderLayer* RenderLayer::hitTestTransformedLayerInFragments(RenderLayer* rootLayer, RenderLayer* containerLayer, const HitTestRequest& request, HitTestResult& result,
1880     const LayoutRect& hitTestRect, const HitTestLocation& hitTestLocation, const HitTestingTransformState* transformState, double* zOffset)
1881 {
1882     LayerFragments enclosingPaginationFragments;
1883     LayoutPoint offsetOfPaginationLayerFromRoot;
1884     // FIXME: We're missing a sub-pixel offset here crbug.com/348728
1885     LayoutRect transformedExtent = transparencyClipBox(this, enclosingPaginationLayer(), HitTestingTransparencyClipBox, RenderLayer::RootOfTransparencyClipBox, LayoutSize());
1886     enclosingPaginationLayer()->collectFragments(enclosingPaginationFragments, rootLayer, hitTestRect,
1887         RootRelativeClipRects, IncludeOverlayScrollbarSize, RespectOverflowClip, &offsetOfPaginationLayerFromRoot, LayoutSize(), &transformedExtent);
1888
1889     for (int i = enclosingPaginationFragments.size() - 1; i >= 0; --i) {
1890         const LayerFragment& fragment = enclosingPaginationFragments.at(i);
1891
1892         // Apply the page/column clip for this fragment, as well as any clips established by layers in between us and
1893         // the enclosing pagination layer.
1894         LayoutRect clipRect = fragment.backgroundRect.rect();
1895
1896         // Now compute the clips within a given fragment
1897         if (parent() != enclosingPaginationLayer()) {
1898             enclosingPaginationLayer()->convertToLayerCoords(rootLayer, offsetOfPaginationLayerFromRoot);
1899             LayoutRect parentClipRect = clipper().backgroundClipRect(ClipRectsContext(enclosingPaginationLayer(), RootRelativeClipRects, IncludeOverlayScrollbarSize)).rect();
1900             parentClipRect.moveBy(fragment.paginationOffset + offsetOfPaginationLayerFromRoot);
1901             clipRect.intersect(parentClipRect);
1902         }
1903
1904         if (!hitTestLocation.intersects(clipRect))
1905             continue;
1906
1907         RenderLayer* hitLayer = hitTestLayerByApplyingTransform(rootLayer, containerLayer, request, result, hitTestRect, hitTestLocation,
1908             transformState, zOffset, fragment.paginationOffset);
1909         if (hitLayer)
1910             return hitLayer;
1911     }
1912
1913     return 0;
1914 }
1915
1916 RenderLayer* RenderLayer::hitTestLayerByApplyingTransform(RenderLayer* rootLayer, RenderLayer* containerLayer, const HitTestRequest& request, HitTestResult& result,
1917     const LayoutRect& hitTestRect, const HitTestLocation& hitTestLocation, const HitTestingTransformState* transformState, double* zOffset,
1918     const LayoutPoint& translationOffset)
1919 {
1920     // Create a transform state to accumulate this transform.
1921     RefPtr<HitTestingTransformState> newTransformState = createLocalTransformState(rootLayer, containerLayer, hitTestRect, hitTestLocation, transformState, translationOffset);
1922
1923     // If the transform can't be inverted, then don't hit test this layer at all.
1924     if (!newTransformState->m_accumulatedTransform.isInvertible())
1925         return 0;
1926
1927     // Compute the point and the hit test rect in the coords of this layer by using the values
1928     // from the transformState, which store the point and quad in the coords of the last flattened
1929     // layer, and the accumulated transform which lets up map through preserve-3d layers.
1930     //
1931     // We can't just map hitTestLocation and hitTestRect because they may have been flattened (losing z)
1932     // by our container.
1933     FloatPoint localPoint = newTransformState->mappedPoint();
1934     FloatQuad localPointQuad = newTransformState->mappedQuad();
1935     LayoutRect localHitTestRect = newTransformState->boundsOfMappedArea();
1936     HitTestLocation newHitTestLocation;
1937     if (hitTestLocation.isRectBasedTest())
1938         newHitTestLocation = HitTestLocation(localPoint, localPointQuad);
1939     else
1940         newHitTestLocation = HitTestLocation(localPoint);
1941
1942     // Now do a hit test with the root layer shifted to be us.
1943     return hitTestLayer(this, containerLayer, request, result, localHitTestRect, newHitTestLocation, true, newTransformState.get(), zOffset);
1944 }
1945
1946 bool RenderLayer::hitTestContents(const HitTestRequest& request, HitTestResult& result, const LayoutRect& layerBounds, const HitTestLocation& hitTestLocation, HitTestFilter hitTestFilter) const
1947 {
1948     ASSERT(isSelfPaintingLayer() || hasSelfPaintingLayerDescendant());
1949
1950     if (!renderer()->hitTest(request, result, hitTestLocation, toLayoutPoint(layerBounds.location() - renderBoxLocation()), hitTestFilter)) {
1951         // It's wrong to set innerNode, but then claim that you didn't hit anything, unless it is
1952         // a rect-based test.
1953         ASSERT(!result.innerNode() || (result.isRectBasedTest() && result.rectBasedTestResult().size()));
1954         return false;
1955     }
1956
1957     // For positioned generated content, we might still not have a
1958     // node by the time we get to the layer level, since none of
1959     // the content in the layer has an element. So just walk up
1960     // the tree.
1961     if (!result.innerNode() || !result.innerNonSharedNode()) {
1962         Node* e = enclosingElement();
1963         if (!result.innerNode())
1964             result.setInnerNode(e);
1965         if (!result.innerNonSharedNode())
1966             result.setInnerNonSharedNode(e);
1967     }
1968
1969     return true;
1970 }
1971
1972 RenderLayer* RenderLayer::hitTestChildren(ChildrenIteration childrentoVisit, RenderLayer* rootLayer,
1973     const HitTestRequest& request, HitTestResult& result,
1974     const LayoutRect& hitTestRect, const HitTestLocation& hitTestLocation,
1975     const HitTestingTransformState* transformState,
1976     double* zOffsetForDescendants, double* zOffset,
1977     const HitTestingTransformState* unflattenedTransformState,
1978     bool depthSortDescendants)
1979 {
1980     if (!hasSelfPaintingLayerDescendant())
1981         return 0;
1982
1983     RenderLayer* resultLayer = 0;
1984     RenderLayerStackingNodeReverseIterator iterator(*m_stackingNode, childrentoVisit);
1985     while (RenderLayerStackingNode* child = iterator.next()) {
1986         RenderLayer* childLayer = child->layer();
1987         RenderLayer* hitLayer = 0;
1988         HitTestResult tempResult(result.hitTestLocation());
1989         if (childLayer->isPaginated())
1990             hitLayer = hitTestPaginatedChildLayer(childLayer, rootLayer, request, tempResult, hitTestRect, hitTestLocation, transformState, zOffsetForDescendants);
1991         else
1992             hitLayer = childLayer->hitTestLayer(rootLayer, this, request, tempResult, hitTestRect, hitTestLocation, false, transformState, zOffsetForDescendants);
1993
1994         // If it a rect-based test, we can safely append the temporary result since it might had hit
1995         // nodes but not necesserily had hitLayer set.
1996         if (result.isRectBasedTest())
1997             result.append(tempResult);
1998
1999         if (isHitCandidate(hitLayer, depthSortDescendants, zOffset, unflattenedTransformState)) {
2000             resultLayer = hitLayer;
2001             if (!result.isRectBasedTest())
2002                 result = tempResult;
2003             if (!depthSortDescendants)
2004                 break;
2005         }
2006     }
2007
2008     return resultLayer;
2009 }
2010
2011 RenderLayer* RenderLayer::hitTestPaginatedChildLayer(RenderLayer* childLayer, RenderLayer* rootLayer, const HitTestRequest& request, HitTestResult& result,
2012                                                      const LayoutRect& hitTestRect, const HitTestLocation& hitTestLocation, const HitTestingTransformState* transformState, double* zOffset)
2013 {
2014     Vector<RenderLayer*> columnLayers;
2015     RenderLayerStackingNode* ancestorNode = m_stackingNode->isNormalFlowOnly() ? parent()->stackingNode() : m_stackingNode->ancestorStackingContextNode();
2016     for (RenderLayer* curr = childLayer->parent(); curr; curr = curr->parent()) {
2017         if (curr->renderer()->hasColumns() && checkContainingBlockChainForPagination(childLayer->renderer(), curr->renderBox()))
2018             columnLayers.append(curr);
2019         if (curr->stackingNode() == ancestorNode)
2020             break;
2021     }
2022
2023     ASSERT(columnLayers.size());
2024     return hitTestChildLayerColumns(childLayer, rootLayer, request, result, hitTestRect, hitTestLocation, transformState, zOffset,
2025                                     columnLayers, columnLayers.size() - 1);
2026 }
2027
2028 RenderLayer* RenderLayer::hitTestChildLayerColumns(RenderLayer* childLayer, RenderLayer* rootLayer, const HitTestRequest& request, HitTestResult& result,
2029                                                    const LayoutRect& hitTestRect, const HitTestLocation& hitTestLocation, const HitTestingTransformState* transformState, double* zOffset,
2030                                                    const Vector<RenderLayer*>& columnLayers, size_t columnIndex)
2031 {
2032     RenderBlock* columnBlock = toRenderBlock(columnLayers[columnIndex]->renderer());
2033
2034     ASSERT(columnBlock && columnBlock->hasColumns());
2035     if (!columnBlock || !columnBlock->hasColumns())
2036         return 0;
2037
2038     LayoutPoint layerOffset;
2039     columnBlock->layer()->convertToLayerCoords(rootLayer, layerOffset);
2040
2041     ColumnInfo* colInfo = columnBlock->columnInfo();
2042     int colCount = columnBlock->columnCount(colInfo);
2043
2044     // We have to go backwards from the last column to the first.
2045     bool isHorizontal = columnBlock->style()->isHorizontalWritingMode();
2046     LayoutUnit logicalLeft = columnBlock->logicalLeftOffsetForContent();
2047     LayoutUnit currLogicalTopOffset = 0;
2048     int i;
2049     for (i = 0; i < colCount; i++) {
2050         LayoutRect colRect = columnBlock->columnRectAt(colInfo, i);
2051         LayoutUnit blockDelta =  (isHorizontal ? colRect.height() : colRect.width());
2052         if (columnBlock->style()->slowIsFlippedBlocksWritingMode())
2053             currLogicalTopOffset += blockDelta;
2054         else
2055             currLogicalTopOffset -= blockDelta;
2056     }
2057     for (i = colCount - 1; i >= 0; i--) {
2058         // For each rect, we clip to the rect, and then we adjust our coords.
2059         LayoutRect colRect = columnBlock->columnRectAt(colInfo, i);
2060         columnBlock->flipForWritingMode(colRect);
2061         LayoutUnit currLogicalLeftOffset = (isHorizontal ? colRect.x() : colRect.y()) - logicalLeft;
2062         LayoutUnit blockDelta =  (isHorizontal ? colRect.height() : colRect.width());
2063         if (columnBlock->style()->slowIsFlippedBlocksWritingMode())
2064             currLogicalTopOffset -= blockDelta;
2065         else
2066             currLogicalTopOffset += blockDelta;
2067
2068         LayoutSize offset;
2069         if (isHorizontal) {
2070             if (colInfo->progressionAxis() == ColumnInfo::InlineAxis)
2071                 offset = LayoutSize(currLogicalLeftOffset, currLogicalTopOffset);
2072             else
2073                 offset = LayoutSize(0, colRect.y() + currLogicalTopOffset - columnBlock->borderTop() - columnBlock->paddingTop());
2074         } else {
2075             if (colInfo->progressionAxis() == ColumnInfo::InlineAxis)
2076                 offset = LayoutSize(currLogicalTopOffset, currLogicalLeftOffset);
2077             else
2078                 offset = LayoutSize(colRect.x() + currLogicalTopOffset - columnBlock->borderLeft() - columnBlock->paddingLeft(), 0);
2079         }
2080
2081         colRect.moveBy(layerOffset);
2082
2083         LayoutRect localClipRect(hitTestRect);
2084         localClipRect.intersect(colRect);
2085
2086         if (!localClipRect.isEmpty() && hitTestLocation.intersects(localClipRect)) {
2087             RenderLayer* hitLayer = 0;
2088             if (!columnIndex) {
2089                 // Apply a translation transform to change where the layer paints.
2090                 TransformationMatrix oldTransform;
2091                 bool oldHasTransform = childLayer->transform();
2092                 if (oldHasTransform)
2093                     oldTransform = *childLayer->transform();
2094                 TransformationMatrix newTransform(oldTransform);
2095                 newTransform.translateRight(offset.width(), offset.height());
2096
2097                 childLayer->m_transform = adoptPtr(new TransformationMatrix(newTransform));
2098                 hitLayer = childLayer->hitTestLayer(rootLayer, columnLayers[0], request, result, localClipRect, hitTestLocation, false, transformState, zOffset);
2099                 if (oldHasTransform)
2100                     childLayer->m_transform = adoptPtr(new TransformationMatrix(oldTransform));
2101                 else
2102                     childLayer->m_transform.clear();
2103             } else {
2104                 // Adjust the transform such that the renderer's upper left corner will be at (0,0) in user space.
2105                 // This involves subtracting out the position of the layer in our current coordinate space.
2106                 RenderLayer* nextLayer = columnLayers[columnIndex - 1];
2107                 RefPtr<HitTestingTransformState> newTransformState = nextLayer->createLocalTransformState(rootLayer, nextLayer, localClipRect, hitTestLocation, transformState);
2108                 newTransformState->translate(offset.width(), offset.height(), HitTestingTransformState::AccumulateTransform);
2109                 FloatPoint localPoint = newTransformState->mappedPoint();
2110                 FloatQuad localPointQuad = newTransformState->mappedQuad();
2111                 LayoutRect localHitTestRect = newTransformState->mappedArea().enclosingBoundingBox();
2112                 HitTestLocation newHitTestLocation;
2113                 if (hitTestLocation.isRectBasedTest())
2114                     newHitTestLocation = HitTestLocation(localPoint, localPointQuad);
2115                 else
2116                     newHitTestLocation = HitTestLocation(localPoint);
2117                 newTransformState->flatten();
2118
2119                 hitLayer = hitTestChildLayerColumns(childLayer, columnLayers[columnIndex - 1], request, result, localHitTestRect, newHitTestLocation,
2120                                                     newTransformState.get(), zOffset, columnLayers, columnIndex - 1);
2121             }
2122
2123             if (hitLayer)
2124                 return hitLayer;
2125         }
2126     }
2127
2128     return 0;
2129 }
2130
2131 void RenderLayer::blockSelectionGapsBoundsChanged()
2132 {
2133     setNeedsCompositingInputsUpdate();
2134 }
2135
2136 void RenderLayer::addBlockSelectionGapsBounds(const LayoutRect& bounds)
2137 {
2138     m_blockSelectionGapsBounds.unite(enclosingIntRect(bounds));
2139     blockSelectionGapsBoundsChanged();
2140 }
2141
2142 void RenderLayer::clearBlockSelectionGapsBounds()
2143 {
2144     m_blockSelectionGapsBounds = IntRect();
2145     for (RenderLayer* child = firstChild(); child; child = child->nextSibling())
2146         child->clearBlockSelectionGapsBounds();
2147     blockSelectionGapsBoundsChanged();
2148 }
2149
2150 void RenderLayer::invalidatePaintForBlockSelectionGaps()
2151 {
2152     for (RenderLayer* child = firstChild(); child; child = child->nextSibling())
2153         child->invalidatePaintForBlockSelectionGaps();
2154
2155     if (m_blockSelectionGapsBounds.isEmpty())
2156         return;
2157
2158     LayoutRect rect = m_blockSelectionGapsBounds;
2159     if (renderer()->hasOverflowClip()) {
2160         RenderBox* box = renderBox();
2161         rect.move(-box->scrolledContentOffset());
2162         if (!scrollableArea()->usesCompositedScrolling())
2163             rect.intersect(box->overflowClipRect(LayoutPoint()));
2164     }
2165     if (renderer()->hasClip())
2166         rect.intersect(toRenderBox(renderer())->clipRect(LayoutPoint()));
2167     if (!rect.isEmpty())
2168         renderer()->invalidatePaintRectangle(rect);
2169 }
2170
2171 IntRect RenderLayer::blockSelectionGapsBounds() const
2172 {
2173     if (!renderer()->isRenderBlock())
2174         return IntRect();
2175
2176     RenderBlock* renderBlock = toRenderBlock(renderer());
2177     LayoutRect gapRects = renderBlock->selectionGapRectsForPaintInvalidation(renderBlock);
2178
2179     return pixelSnappedIntRect(gapRects);
2180 }
2181
2182 bool RenderLayer::hasBlockSelectionGapBounds() const
2183 {
2184     // FIXME: it would be more accurate to return !blockSelectionGapsBounds().isEmpty(), but this is impossible
2185     // at the moment because it causes invalid queries to layout-dependent code (crbug.com/372802).
2186     // ASSERT(renderer()->document().lifecycle().state() >= DocumentLifecycle::LayoutClean);
2187
2188     if (!renderer()->isRenderBlock())
2189         return false;
2190
2191     return toRenderBlock(renderer())->shouldPaintSelectionGaps();
2192 }
2193
2194 bool RenderLayer::intersectsDamageRect(const LayoutRect& layerBounds, const LayoutRect& damageRect, const RenderLayer* rootLayer, const LayoutPoint* offsetFromRoot) const
2195 {
2196     // Always examine the canvas and the root.
2197     // FIXME: Could eliminate the isDocumentElement() check if we fix background painting so that the RenderView
2198     // paints the root's background.
2199     if (isRootLayer() || renderer()->isDocumentElement())
2200         return true;
2201
2202     // If we aren't an inline flow, and our layer bounds do intersect the damage rect, then we
2203     // can go ahead and return true.
2204     RenderView* view = renderer()->view();
2205     ASSERT(view);
2206     if (view && !renderer()->isRenderInline()) {
2207         if (layerBounds.intersects(damageRect))
2208             return true;
2209     }
2210
2211     // Otherwise we need to compute the bounding box of this single layer and see if it intersects
2212     // the damage rect.
2213     return physicalBoundingBox(rootLayer, offsetFromRoot).intersects(damageRect);
2214 }
2215
2216 LayoutRect RenderLayer::logicalBoundingBox() const
2217 {
2218     // There are three special cases we need to consider.
2219     // (1) Inline Flows.  For inline flows we will create a bounding box that fully encompasses all of the lines occupied by the
2220     // inline.  In other words, if some <span> wraps to three lines, we'll create a bounding box that fully encloses the
2221     // line boxes of all three lines (including overflow on those lines).
2222     // (2) Left/Top Overflow.  The width/height of layers already includes right/bottom overflow.  However, in the case of left/top
2223     // overflow, we have to create a bounding box that will extend to include this overflow.
2224     // (3) Floats.  When a layer has overhanging floats that it paints, we need to make sure to include these overhanging floats
2225     // as part of our bounding box.  We do this because we are the responsible layer for both hit testing and painting those
2226     // floats.
2227     LayoutRect result;
2228     if (renderer()->isInline() && renderer()->isRenderInline()) {
2229         result = toRenderInline(renderer())->linesVisualOverflowBoundingBox();
2230     } else if (renderer()->isTableRow()) {
2231         // Our bounding box is just the union of all of our cells' border/overflow rects.
2232         for (RenderObject* child = renderer()->slowFirstChild(); child; child = child->nextSibling()) {
2233             if (child->isTableCell()) {
2234                 LayoutRect bbox = toRenderBox(child)->borderBoxRect();
2235                 result.unite(bbox);
2236                 LayoutRect overflowRect = renderBox()->visualOverflowRect();
2237                 if (bbox != overflowRect)
2238                     result.unite(overflowRect);
2239             }
2240         }
2241     } else {
2242         RenderBox* box = renderBox();
2243         ASSERT(box);
2244         result = box->borderBoxRect();
2245         result.unite(box->visualOverflowRect());
2246     }
2247
2248     ASSERT(renderer()->view());
2249     return result;
2250 }
2251
2252 static inline LayoutRect flippedLogicalBoundingBox(LayoutRect boundingBox, RenderObject* renderer)
2253 {
2254     if (!UNLIKELY(renderer->document().containsAnyRareWritingMode()))
2255         return boundingBox;
2256
2257     LayoutRect result = boundingBox;
2258     if (renderer->isBox())
2259         toRenderBox(renderer)->flipForWritingMode(result);
2260     else
2261         renderer->containingBlock()->flipForWritingMode(result);
2262     return result;
2263 }
2264
2265 LayoutRect RenderLayer::physicalBoundingBox(const RenderLayer* ancestorLayer, const LayoutPoint* offsetFromRoot) const
2266 {
2267     LayoutRect result = flippedLogicalBoundingBox(logicalBoundingBox(), renderer());
2268     if (offsetFromRoot)
2269         result.moveBy(*offsetFromRoot);
2270     else
2271         convertToLayerCoords(ancestorLayer, result);
2272     return result;
2273 }
2274
2275 LayoutRect RenderLayer::fragmentsBoundingBox(const RenderLayer* ancestorLayer) const
2276 {
2277     if (!enclosingPaginationLayer())
2278         return physicalBoundingBox(ancestorLayer);
2279
2280     LayoutRect result = flippedLogicalBoundingBox(logicalBoundingBox(), renderer());
2281     convertFromFlowThreadToVisualBoundingBoxInAncestor(this, ancestorLayer, result);
2282     return result;
2283 }
2284
2285 LayoutRect RenderLayer::boundingBoxForCompositingOverlapTest() const
2286 {
2287     return overlapBoundsIncludeChildren() ? boundingBoxForCompositing() : fragmentsBoundingBox(this);
2288 }
2289
2290 static void expandRectForReflectionAndStackingChildren(const RenderLayer* ancestorLayer, RenderLayer::CalculateBoundsOptions options, LayoutRect& result)
2291 {
2292     if (ancestorLayer->reflectionInfo() && !ancestorLayer->reflectionInfo()->reflectionLayer()->hasCompositedLayerMapping())
2293         result.unite(ancestorLayer->reflectionInfo()->reflectionLayer()->boundingBoxForCompositing(ancestorLayer));
2294
2295     ASSERT(ancestorLayer->stackingNode()->isStackingContext() || !ancestorLayer->stackingNode()->hasPositiveZOrderList());
2296
2297 #if ENABLE(ASSERT)
2298     LayerListMutationDetector mutationChecker(const_cast<RenderLayer*>(ancestorLayer)->stackingNode());
2299 #endif
2300
2301     RenderLayerStackingNodeIterator iterator(*ancestorLayer->stackingNode(), AllChildren);
2302     while (RenderLayerStackingNode* node = iterator.next()) {
2303         // Here we exclude both directly composited layers and squashing layers
2304         // because those RenderLayers don't paint into the graphics layer
2305         // for this RenderLayer. For example, the bounds of squashed RenderLayers
2306         // will be included in the computation of the appropriate squashing
2307         // GraphicsLayer.
2308         if (options != RenderLayer::ApplyBoundsChickenEggHacks && node->layer()->compositingState() != NotComposited)
2309             continue;
2310         result.unite(node->layer()->boundingBoxForCompositing(ancestorLayer, options));
2311     }
2312 }
2313
2314 LayoutRect RenderLayer::physicalBoundingBoxIncludingReflectionAndStackingChildren(const RenderLayer* ancestorLayer, const LayoutPoint& offsetFromRoot) const
2315 {
2316     LayoutPoint origin;
2317     LayoutRect result = physicalBoundingBox(ancestorLayer, &origin);
2318
2319     const_cast<RenderLayer*>(this)->stackingNode()->updateLayerListsIfNeeded();
2320
2321     expandRectForReflectionAndStackingChildren(this, DoNotApplyBoundsChickenEggHacks, result);
2322
2323     result.moveBy(offsetFromRoot);
2324     return result;
2325 }
2326
2327 LayoutRect RenderLayer::boundingBoxForCompositing(const RenderLayer* ancestorLayer, CalculateBoundsOptions options) const
2328 {
2329     if (!isSelfPaintingLayer())
2330         return LayoutRect();
2331
2332     if (!ancestorLayer)
2333         ancestorLayer = this;
2334
2335     // FIXME: This could be improved to do a check like hasVisibleNonCompositingDescendantLayers() (bug 92580).
2336     if (this != ancestorLayer && !hasVisibleContent() && !hasVisibleDescendant())
2337         return LayoutRect();
2338
2339     // The root layer is always just the size of the document.
2340     if (isRootLayer())
2341         return m_renderer->view()->unscaledDocumentRect();
2342
2343     // The layer created for the RenderFlowThread is just a helper for painting and hit-testing,
2344     // and should not contribute to the bounding box. The RenderMultiColumnSets will contribute
2345     // the correct size for the rendered content of the multicol container.
2346     if (useRegionBasedColumns() && renderer()->isRenderFlowThread())
2347         return LayoutRect();
2348
2349     LayoutRect result = clipper().localClipRect();
2350     if (result == PaintInfo::infiniteRect()) {
2351         LayoutPoint origin;
2352         result = physicalBoundingBox(ancestorLayer, &origin);
2353
2354         const_cast<RenderLayer*>(this)->stackingNode()->updateLayerListsIfNeeded();
2355
2356         // Reflections are implemented with RenderLayers that hang off of the reflected layer. However,
2357         // the reflection layer subtree does not include the subtree of the parent RenderLayer, so
2358         // a recursive computation of stacking children yields no results. This breaks cases when there are stacking
2359         // children of the parent, that need to be included in reflected composited bounds.
2360         // Fix this by including composited bounds of stacking children of the reflected RenderLayer.
2361         if (hasCompositedLayerMapping() && parent() && parent()->reflectionInfo() && parent()->reflectionInfo()->reflectionLayer() == this)
2362             expandRectForReflectionAndStackingChildren(parent(), options, result);
2363         else
2364             expandRectForReflectionAndStackingChildren(this, options, result);
2365
2366         // FIXME: We can optimize the size of the composited layers, by not enlarging
2367         // filtered areas with the outsets if we know that the filter is going to render in hardware.
2368         // https://bugs.webkit.org/show_bug.cgi?id=81239
2369         m_renderer->style()->filterOutsets().expandRect(result);
2370     }
2371
2372     if (paintsWithTransform(PaintBehaviorNormal) || (options == ApplyBoundsChickenEggHacks && transform()))
2373         result = transform()->mapRect(result);
2374
2375     if (enclosingPaginationLayer()) {
2376         convertFromFlowThreadToVisualBoundingBoxInAncestor(this, ancestorLayer, result);
2377         return result;
2378     }
2379     LayoutPoint delta;
2380     convertToLayerCoords(ancestorLayer, delta);
2381     result.moveBy(delta);
2382     return result;
2383 }
2384
2385 CompositingState RenderLayer::compositingState() const
2386 {
2387     ASSERT(isAllowedToQueryCompositingState());
2388
2389     // This is computed procedurally so there is no redundant state variable that
2390     // can get out of sync from the real actual compositing state.
2391
2392     if (m_groupedMapping) {
2393         ASSERT(compositor()->layerSquashingEnabled());
2394         ASSERT(!m_compositedLayerMapping);
2395         return PaintsIntoGroupedBacking;
2396     }
2397
2398     if (!m_compositedLayerMapping)
2399         return NotComposited;
2400
2401     return PaintsIntoOwnBacking;
2402 }
2403
2404 bool RenderLayer::isAllowedToQueryCompositingState() const
2405 {
2406     if (gCompositingQueryMode == CompositingQueriesAreAllowed)
2407         return true;
2408     return renderer()->document().lifecycle().state() >= DocumentLifecycle::InCompositingUpdate;
2409 }
2410
2411 CompositedLayerMapping* RenderLayer::compositedLayerMapping() const
2412 {
2413     ASSERT(isAllowedToQueryCompositingState());
2414     return m_compositedLayerMapping.get();
2415 }
2416
2417 GraphicsLayer* RenderLayer::graphicsLayerBacking() const
2418 {
2419     switch (compositingState()) {
2420     case NotComposited:
2421         return 0;
2422     case PaintsIntoGroupedBacking:
2423         return groupedMapping()->squashingLayer();
2424     default:
2425         return compositedLayerMapping()->mainGraphicsLayer();
2426     }
2427 }
2428
2429 GraphicsLayer* RenderLayer::graphicsLayerBackingForScrolling() const
2430 {
2431     switch (compositingState()) {
2432     case NotComposited:
2433         return 0;
2434     case PaintsIntoGroupedBacking:
2435         return groupedMapping()->squashingLayer();
2436     default:
2437         return compositedLayerMapping()->scrollingContentsLayer() ? compositedLayerMapping()->scrollingContentsLayer() : compositedLayerMapping()->mainGraphicsLayer();
2438     }
2439 }
2440
2441 CompositedLayerMapping* RenderLayer::ensureCompositedLayerMapping()
2442 {
2443     if (!m_compositedLayerMapping) {
2444         m_compositedLayerMapping = adoptPtr(new CompositedLayerMapping(*this));
2445         m_compositedLayerMapping->setNeedsGraphicsLayerUpdate(GraphicsLayerUpdateSubtree);
2446
2447         updateOrRemoveFilterEffectRenderer();
2448     }
2449     return m_compositedLayerMapping.get();
2450 }
2451
2452 void RenderLayer::clearCompositedLayerMapping(bool layerBeingDestroyed)
2453 {
2454     if (!layerBeingDestroyed) {
2455         // We need to make sure our decendants get a geometry update. In principle,
2456         // we could call setNeedsGraphicsLayerUpdate on our children, but that would
2457         // require walking the z-order lists to find them. Instead, we over-invalidate
2458         // by marking our parent as needing a geometry update.
2459         if (RenderLayer* compositingParent = enclosingLayerWithCompositedLayerMapping(ExcludeSelf))
2460             compositingParent->compositedLayerMapping()->setNeedsGraphicsLayerUpdate(GraphicsLayerUpdateSubtree);
2461     }
2462
2463     m_compositedLayerMapping.clear();
2464
2465     if (!layerBeingDestroyed)
2466         updateOrRemoveFilterEffectRenderer();
2467 }
2468
2469 void RenderLayer::setGroupedMapping(CompositedLayerMapping* groupedMapping, bool layerBeingDestroyed)
2470 {
2471     if (groupedMapping == m_groupedMapping)
2472         return;
2473
2474     if (!layerBeingDestroyed && m_groupedMapping) {
2475         m_groupedMapping->setNeedsGraphicsLayerUpdate(GraphicsLayerUpdateSubtree);
2476         m_groupedMapping->removeRenderLayerFromSquashingGraphicsLayer(this);
2477     }
2478     m_groupedMapping = groupedMapping;
2479     if (!layerBeingDestroyed && m_groupedMapping)
2480         m_groupedMapping->setNeedsGraphicsLayerUpdate(GraphicsLayerUpdateSubtree);
2481 }
2482
2483 bool RenderLayer::hasCompositedMask() const
2484 {
2485     return m_compositedLayerMapping && m_compositedLayerMapping->hasMaskLayer();
2486 }
2487
2488 bool RenderLayer::hasCompositedClippingMask() const
2489 {
2490     return m_compositedLayerMapping && m_compositedLayerMapping->hasChildClippingMaskLayer();
2491 }
2492
2493 bool RenderLayer::paintsWithTransform(PaintBehavior paintBehavior) const
2494 {
2495     return transform() && ((paintBehavior & PaintBehaviorFlattenCompositingLayers) || compositingState() != PaintsIntoOwnBacking);
2496 }
2497
2498 bool RenderLayer::backgroundIsKnownToBeOpaqueInRect(const LayoutRect& localRect) const
2499 {
2500     if (!isSelfPaintingLayer() && !hasSelfPaintingLayerDescendant())
2501         return false;
2502
2503     if (paintsWithTransparency(PaintBehaviorNormal))
2504         return false;
2505
2506     // We can't use hasVisibleContent(), because that will be true if our renderer is hidden, but some child
2507     // is visible and that child doesn't cover the entire rect.
2508     if (renderer()->style()->visibility() != VISIBLE)
2509         return false;
2510
2511     if (paintsWithFilters() && renderer()->style()->filter().hasFilterThatAffectsOpacity())
2512         return false;
2513
2514     // FIXME: Handle simple transforms.
2515     if (paintsWithTransform(PaintBehaviorNormal))
2516         return false;
2517
2518     // FIXME: Remove this check.
2519     // This function should not be called when layer-lists are dirty.
2520     // It is somehow getting triggered during style update.
2521     if (m_stackingNode->zOrderListsDirty() || m_stackingNode->normalFlowListDirty())
2522         return false;
2523
2524     // FIXME: We currently only check the immediate renderer,
2525     // which will miss many cases.
2526     if (renderer()->backgroundIsKnownToBeOpaqueInRect(localRect))
2527         return true;
2528
2529     // We can't consult child layers if we clip, since they might cover
2530     // parts of the rect that are clipped out.
2531     if (renderer()->hasOverflowClip())
2532         return false;
2533
2534     return childBackgroundIsKnownToBeOpaqueInRect(localRect);
2535 }
2536
2537 bool RenderLayer::childBackgroundIsKnownToBeOpaqueInRect(const LayoutRect& localRect) const
2538 {
2539     RenderLayerStackingNodeReverseIterator revertseIterator(*m_stackingNode, PositiveZOrderChildren | NormalFlowChildren | NegativeZOrderChildren);
2540     while (RenderLayerStackingNode* child = revertseIterator.next()) {
2541         const RenderLayer* childLayer = child->layer();
2542         // Stop at composited paint boundaries.
2543         if (childLayer->isPaintInvalidationContainer())
2544             continue;
2545
2546         if (!childLayer->canUseConvertToLayerCoords())
2547             continue;
2548
2549         LayoutPoint childOffset;
2550         LayoutRect childLocalRect(localRect);
2551         childLayer->convertToLayerCoords(this, childOffset);
2552         childLocalRect.moveBy(-childOffset);
2553
2554         if (childLayer->backgroundIsKnownToBeOpaqueInRect(childLocalRect))
2555             return true;
2556     }
2557     return false;
2558 }
2559
2560 bool RenderLayer::shouldBeSelfPaintingLayer() const
2561 {
2562     if (renderer()->isRenderPart() && toRenderPart(renderer())->requiresAcceleratedCompositing())
2563         return true;
2564     return m_layerType == NormalLayer
2565         || (m_scrollableArea && m_scrollableArea->hasOverlayScrollbars())
2566         || needsCompositedScrolling();
2567 }
2568
2569 void RenderLayer::updateSelfPaintingLayer()
2570 {
2571     bool isSelfPaintingLayer = shouldBeSelfPaintingLayer();
2572     if (this->isSelfPaintingLayer() == isSelfPaintingLayer)
2573         return;
2574
2575     m_isSelfPaintingLayer = isSelfPaintingLayer;
2576
2577     if (parent())
2578         parent()->dirtyAncestorChainHasSelfPaintingLayerDescendantStatus();
2579 }
2580
2581 bool RenderLayer::hasNonEmptyChildRenderers() const
2582 {
2583     // Some HTML can cause whitespace text nodes to have renderers, like:
2584     // <div>
2585     // <img src=...>
2586     // </div>
2587     // so test for 0x0 RenderTexts here
2588     for (RenderObject* child = renderer()->slowFirstChild(); child; child = child->nextSibling()) {
2589         if (!child->hasLayer()) {
2590             if (child->isRenderInline() || !child->isBox())
2591                 return true;
2592
2593             if (toRenderBox(child)->width() > 0 || toRenderBox(child)->height() > 0)
2594                 return true;
2595         }
2596     }
2597     return false;
2598 }
2599
2600 bool RenderLayer::hasBoxDecorationsOrBackground() const
2601 {
2602     return renderer()->style()->hasBoxDecorations() || renderer()->style()->hasBackground();
2603 }
2604
2605 bool RenderLayer::hasVisibleBoxDecorations() const
2606 {
2607     if (!hasVisibleContent())
2608         return false;
2609
2610     return hasBoxDecorationsOrBackground() || hasOverflowControls();
2611 }
2612
2613 void RenderLayer::updateFilters(const RenderStyle* oldStyle, const RenderStyle* newStyle)
2614 {
2615     if (!newStyle->hasFilter() && (!oldStyle || !oldStyle->hasFilter()))
2616         return;
2617
2618     updateOrRemoveFilterClients();
2619     updateOrRemoveFilterEffectRenderer();
2620 }
2621
2622 bool RenderLayer::attemptDirectCompositingUpdate(StyleDifference diff, const RenderStyle* oldStyle)
2623 {
2624     CompositingReasons oldPotentialCompositingReasonsFromStyle = m_potentialCompositingReasonsFromStyle;
2625     compositor()->updatePotentialCompositingReasonsFromStyle(this);
2626
2627     // This function implements an optimization for transforms and opacity.
2628     // A common pattern is for a touchmove handler to update the transform
2629     // and/or an opacity of an element every frame while the user moves their
2630     // finger across the screen. The conditions below recognize when the
2631     // compositing state is set up to receive a direct transform or opacity
2632     // update.
2633
2634     if (!diff.hasAtMostPropertySpecificDifferences(StyleDifference::TransformChanged | StyleDifference::OpacityChanged))
2635         return false;
2636     // The potentialCompositingReasonsFromStyle could have changed without
2637     // a corresponding StyleDifference if an animation started or ended.
2638     if (m_potentialCompositingReasonsFromStyle != oldPotentialCompositingReasonsFromStyle)
2639         return false;
2640     // We could add support for reflections if we updated the transform on
2641     // the reflection layers.
2642     if (renderer()->hasReflection())
2643         return false;
2644     // If we're unwinding a scheduleSVGFilterLayerUpdateHack(), then we can't
2645     // perform a direct compositing update because the filters code is going
2646     // to produce different output this time around. We can remove this code
2647     // once we fix the chicken/egg bugs in the filters code and delete the
2648     // scheduleSVGFilterLayerUpdateHack().
2649     if (renderer()->node() && renderer()->node()->svgFilterNeedsLayerUpdate())
2650         return false;
2651     if (!m_compositedLayerMapping)
2652         return false;
2653
2654     // To cut off almost all the work in the compositing update for
2655     // this case, we treat inline transforms has having assumed overlap
2656     // (similar to how we treat animated transforms). Notice that we read
2657     // CompositingReasonInlineTransform from the m_compositingReasons, which
2658     // means that the inline transform actually triggered assumed overlap in
2659     // the overlap map.
2660     if (diff.transformChanged() && !(m_compositingReasons & CompositingReasonInlineTransform))
2661         return false;
2662
2663     // We composite transparent RenderLayers differently from non-transparent
2664     // RenderLayers even when the non-transparent RenderLayers are already a
2665     // stacking context.
2666     if (diff.opacityChanged() && m_renderer->style()->hasOpacity() != oldStyle->hasOpacity())
2667         return false;
2668
2669     updateTransform(oldStyle, renderer()->style());
2670
2671     // FIXME: Consider introducing a smaller graphics layer update scope
2672     // that just handles transforms and opacity. GraphicsLayerUpdateLocal
2673     // will also program bounds, clips, and many other properties that could
2674     // not possibly have changed.
2675     m_compositedLayerMapping->setNeedsGraphicsLayerUpdate(GraphicsLayerUpdateLocal);
2676     compositor()->setNeedsCompositingUpdate(CompositingUpdateAfterGeometryChange);
2677     return true;
2678 }
2679
2680 void RenderLayer::styleChanged(StyleDifference diff, const RenderStyle* oldStyle)
2681 {
2682     if (attemptDirectCompositingUpdate(diff, oldStyle))
2683         return;
2684
2685     m_stackingNode->updateIsNormalFlowOnly();
2686     m_stackingNode->updateStackingNodesAfterStyleChange(oldStyle);
2687
2688     if (m_scrollableArea)
2689         m_scrollableArea->updateAfterStyleChange(oldStyle);
2690
2691     // Overlay scrollbars can make this layer self-painting so we need
2692     // to recompute the bit once scrollbars have been updated.
2693     updateSelfPaintingLayer();
2694
2695     if (!oldStyle || !renderer()->style()->reflectionDataEquivalent(oldStyle)) {
2696         ASSERT(!oldStyle || diff.needsFullLayout());
2697         updateReflectionInfo(oldStyle);
2698     }
2699
2700     updateDescendantDependentFlags();
2701
2702     updateTransform(oldStyle, renderer()->style());
2703     updateFilters(oldStyle, renderer()->style());
2704
2705     setNeedsCompositingInputsUpdate();
2706 }
2707
2708 bool RenderLayer::scrollsOverflow() const
2709 {
2710     if (RenderLayerScrollableArea* scrollableArea = this->scrollableArea())
2711         return scrollableArea->scrollsOverflow();
2712
2713     return false;
2714 }
2715
2716 FilterOperations RenderLayer::computeFilterOperations(const RenderStyle* style)
2717 {
2718     const FilterOperations& filters = style->filter();
2719     if (filters.hasReferenceFilter()) {
2720         for (size_t i = 0; i < filters.size(); ++i) {
2721             FilterOperation* filterOperation = filters.operations().at(i).get();
2722             if (filterOperation->type() != FilterOperation::REFERENCE)
2723                 continue;
2724             ReferenceFilterOperation* referenceOperation = toReferenceFilterOperation(filterOperation);
2725             // FIXME: Cache the ReferenceFilter if it didn't change.
2726             RefPtr<ReferenceFilter> referenceFilter = ReferenceFilter::create(style->effectiveZoom());
2727             referenceFilter->setLastEffect(ReferenceFilterBuilder::build(referenceFilter.get(), renderer(), referenceFilter->sourceGraphic(),
2728                 referenceOperation));
2729             referenceOperation->setFilter(referenceFilter.release());
2730         }
2731     }
2732
2733     return filters;
2734 }
2735
2736 void RenderLayer::updateOrRemoveFilterClients()
2737 {
2738     if (!hasFilter()) {
2739         removeFilterInfoIfNeeded();
2740         return;
2741     }
2742
2743     if (renderer()->style()->filter().hasReferenceFilter())
2744         ensureFilterInfo()->updateReferenceFilterClients(renderer()->style()->filter());
2745     else if (hasFilterInfo())
2746         filterInfo()->removeReferenceFilterClients();
2747 }
2748
2749 void RenderLayer::updateOrRemoveFilterEffectRenderer()
2750 {
2751     // FilterEffectRenderer is only used to render the filters in software mode,
2752     // so we always need to run updateOrRemoveFilterEffectRenderer after the composited
2753     // mode might have changed for this layer.
2754     if (!paintsWithFilters()) {
2755         // Don't delete the whole filter info here, because we might use it
2756         // for loading CSS shader files.
2757         if (RenderLayerFilterInfo* filterInfo = this->filterInfo())
2758             filterInfo->setRenderer(nullptr);
2759
2760         return;
2761     }
2762
2763     RenderLayerFilterInfo* filterInfo = ensureFilterInfo();
2764     if (!filterInfo->renderer()) {
2765         RefPtr<FilterEffectRenderer> filterRenderer = FilterEffectRenderer::create();
2766         filterInfo->setRenderer(filterRenderer.release());
2767     }
2768
2769     // If the filter fails to build, remove it from the layer. It will still attempt to
2770     // go through regular processing (e.g. compositing), but never apply anything.
2771     if (!filterInfo->renderer()->build(renderer(), computeFilterOperations(renderer()->style())))
2772         filterInfo->setRenderer(nullptr);
2773 }
2774
2775 void RenderLayer::filterNeedsPaintInvalidation()
2776 {
2777     {
2778         DeprecatedScheduleStyleRecalcDuringLayout marker(renderer()->document().lifecycle());
2779         // It's possible for scheduleSVGFilterLayerUpdateHack to schedule a style recalc, which
2780         // is a problem because this function can be called while performing layout.
2781         // Presumably this represents an illegal data flow of layout or compositing
2782         // information into the style system.
2783         toElement(renderer()->node())->scheduleSVGFilterLayerUpdateHack();
2784     }
2785
2786     renderer()->setShouldDoFullPaintInvalidation();
2787 }
2788
2789 void RenderLayer::addLayerHitTestRects(LayerHitTestRects& rects) const
2790 {
2791     computeSelfHitTestRects(rects);
2792     for (RenderLayer* child = firstChild(); child; child = child->nextSibling())
2793         child->addLayerHitTestRects(rects);
2794 }
2795
2796 void RenderLayer::computeSelfHitTestRects(LayerHitTestRects& rects) const
2797 {
2798     if (!size().isEmpty()) {
2799         Vector<LayoutRect> rect;
2800
2801         if (renderBox() && renderBox()->scrollsOverflow()) {
2802             // For scrolling layers, rects are taken to be in the space of the contents.
2803             // We need to include the bounding box of the layer in the space of its parent
2804             // (eg. for border / scroll bars) and if it's composited then the entire contents
2805             // as well as they may be on another composited layer. Skip reporting contents
2806             // for non-composited layers as they'll get projected to the same layer as the
2807             // bounding box.
2808             if (compositingState() != NotComposited)
2809                 rect.append(m_scrollableArea->overflowRect());
2810
2811             rects.set(this, rect);
2812             if (const RenderLayer* parentLayer = parent()) {
2813                 LayerHitTestRects::iterator iter = rects.find(parentLayer);
2814                 if (iter == rects.end()) {
2815                     rects.add(parentLayer, Vector<LayoutRect>()).storedValue->value.append(physicalBoundingBox(parentLayer));
2816                 } else {
2817                     iter->value.append(physicalBoundingBox(parentLayer));
2818                 }
2819             }
2820         } else {
2821             rect.append(logicalBoundingBox());
2822             rects.set(this, rect);
2823         }
2824     }
2825 }
2826
2827 void RenderLayer::setShouldDoFullPaintInvalidationIncludingNonCompositingDescendants()
2828 {
2829     renderer()->setShouldDoFullPaintInvalidation();
2830
2831     // Disable for reading compositingState() in isPaintInvalidationContainer() below.
2832     DisableCompositingQueryAsserts disabler;
2833
2834     for (RenderLayer* child = firstChild(); child; child = child->nextSibling()) {
2835         if (!child->isPaintInvalidationContainer())
2836             child->setShouldDoFullPaintInvalidationIncludingNonCompositingDescendants();
2837     }
2838 }
2839
2840 DisableCompositingQueryAsserts::DisableCompositingQueryAsserts()
2841     : m_disabler(gCompositingQueryMode, CompositingQueriesAreAllowed) { }
2842
2843 } // namespace blink
2844
2845 #ifndef NDEBUG
2846 void showLayerTree(const blink::RenderLayer* layer)
2847 {
2848     if (!layer)
2849         return;
2850
2851     if (blink::LocalFrame* frame = layer->renderer()->frame()) {
2852         WTF::String output = externalRepresentation(frame, blink::RenderAsTextShowAllLayers | blink::RenderAsTextShowLayerNesting | blink::RenderAsTextShowCompositedLayers | blink::RenderAsTextShowAddresses | blink::RenderAsTextShowIDAndClass | blink::RenderAsTextDontUpdateLayout | blink::RenderAsTextShowLayoutState);
2853         fprintf(stderr, "%s\n", output.utf8().data());
2854     }
2855 }
2856
2857 void showLayerTree(const blink::RenderObject* renderer)
2858 {
2859     if (!renderer)
2860         return;
2861     showLayerTree(renderer->enclosingLayer());
2862 }
2863 #endif