1/*
2 * Copyright (C) 2009, 2010 Apple Inc. All rights reserved.
3 * Copyright (C) 2014 Google Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28#include "core/rendering/compositing/GraphicsLayerUpdater.h"
29
30#include "core/html/HTMLMediaElement.h"
31#include "core/inspector/InspectorTraceEvents.h"
32#include "core/rendering/RenderLayer.h"
33#include "core/rendering/RenderLayerReflectionInfo.h"
34#include "core/rendering/RenderPart.h"
35#include "core/rendering/compositing/CompositedLayerMapping.h"
36#include "core/rendering/compositing/RenderLayerCompositor.h"
37#include "platform/TraceEvent.h"
38
39namespace blink {
40
41class GraphicsLayerUpdater::UpdateContext {
42public:
43    UpdateContext()
44        : m_compositingStackingContext(0)
45        , m_compositingAncestor(0)
46    {
47    }
48
49    UpdateContext(const UpdateContext& other, const RenderLayer& layer)
50        : m_compositingStackingContext(other.m_compositingStackingContext)
51        , m_compositingAncestor(other.compositingContainer(layer))
52    {
53        CompositingState compositingState = layer.compositingState();
54        if (compositingState != NotComposited && compositingState != PaintsIntoGroupedBacking) {
55            m_compositingAncestor = &layer;
56            if (layer.stackingNode()->isStackingContext())
57                m_compositingStackingContext = &layer;
58        }
59    }
60
61    const RenderLayer* compositingContainer(const RenderLayer& layer) const
62    {
63        return layer.stackingNode()->isNormalFlowOnly() ? m_compositingAncestor : m_compositingStackingContext;
64    }
65
66    const RenderLayer* compositingStackingContext() const
67    {
68        return m_compositingStackingContext;
69    }
70
71private:
72    const RenderLayer* m_compositingStackingContext;
73    const RenderLayer* m_compositingAncestor;
74};
75
76GraphicsLayerUpdater::GraphicsLayerUpdater()
77    : m_needsRebuildTree(false)
78{
79}
80
81GraphicsLayerUpdater::~GraphicsLayerUpdater()
82{
83}
84
85void GraphicsLayerUpdater::update(RenderLayer& layer, Vector<RenderLayer*>& layersNeedingPaintInvalidation)
86{
87    TRACE_EVENT0("blink", "GraphicsLayerUpdater::update");
88    updateRecursive(layer, DoNotForceUpdate, UpdateContext(), layersNeedingPaintInvalidation);
89    layer.compositor()->updateRootLayerPosition();
90}
91
92void GraphicsLayerUpdater::updateRecursive(RenderLayer& layer, UpdateType updateType, const UpdateContext& context, Vector<RenderLayer*>& layersNeedingPaintInvalidation)
93{
94    if (layer.hasCompositedLayerMapping()) {
95        CompositedLayerMapping* mapping = layer.compositedLayerMapping();
96
97        if (updateType == ForceUpdate || mapping->needsGraphicsLayerUpdate()) {
98            const RenderLayer* compositingContainer = context.compositingContainer(layer);
99            ASSERT(compositingContainer == layer.enclosingLayerWithCompositedLayerMapping(ExcludeSelf));
100
101            if (mapping->updateRequiresOwnBackingStoreForAncestorReasons(compositingContainer)) {
102                TRACE_LAYER_INVALIDATION(&layer, InspectorLayerInvalidationTrackingEvent::AncestorRequiresNewLayer);
103                layersNeedingPaintInvalidation.append(&layer);
104                updateType = ForceUpdate;
105            }
106
107            if (mapping->updateGraphicsLayerConfiguration())
108                m_needsRebuildTree = true;
109
110            mapping->updateGraphicsLayerGeometry(compositingContainer, context.compositingStackingContext(), layersNeedingPaintInvalidation);
111
112            if (mapping->hasUnpositionedOverflowControlsLayers())
113                layer.scrollableArea()->positionOverflowControls(IntSize());
114
115            updateType = mapping->updateTypeForChildren(updateType);
116            mapping->clearNeedsGraphicsLayerUpdate();
117        }
118    }
119
120    UpdateContext childContext(context, layer);
121    for (RenderLayer* child = layer.firstChild(); child; child = child->nextSibling())
122        updateRecursive(*child, updateType, childContext, layersNeedingPaintInvalidation);
123}
124
125#if ENABLE(ASSERT)
126
127void GraphicsLayerUpdater::assertNeedsToUpdateGraphicsLayerBitsCleared(RenderLayer& layer)
128{
129    if (layer.hasCompositedLayerMapping())
130        layer.compositedLayerMapping()->assertNeedsToUpdateGraphicsLayerBitsCleared();
131
132    for (RenderLayer* child = layer.firstChild(); child; child = child->nextSibling())
133        assertNeedsToUpdateGraphicsLayerBitsCleared(*child);
134}
135
136#endif
137
138} // namespace blink
139