Home | History | Annotate | Download | only in rendering
      1 /*
      2  * Copyright (C) 2009, 2010, 2011 Apple Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #ifndef CompositedLayerMapping_h
     27 #define CompositedLayerMapping_h
     28 
     29 #include "core/rendering/RenderLayer.h"
     30 #include "platform/geometry/FloatPoint.h"
     31 #include "platform/geometry/FloatPoint3D.h"
     32 #include "platform/graphics/GraphicsLayer.h"
     33 #include "platform/graphics/GraphicsLayerClient.h"
     34 #include "platform/transforms/TransformationMatrix.h"
     35 
     36 namespace WebCore {
     37 
     38 class KeyframeList;
     39 class RenderLayerCompositor;
     40 class WebAnimationProvider;
     41 
     42 enum CompositingLayerType {
     43     NormalCompositingLayer, // non-tiled layer with backing store
     44     MediaCompositingLayer, // layer that contains an image, video, webGL or plugin
     45     ContainerCompositingLayer // layer with no backing store
     46 };
     47 
     48 
     49 // A GraphicsLayerPaintInfo contains all the info needed to paint a partial subtree of RenderLayers into a GraphicsLayer.
     50 struct GraphicsLayerPaintInfo {
     51     RenderLayer* renderLayer;
     52 
     53     IntRect compositedBounds;
     54 
     55     // A temporary offset used for squashing layers, when the origin of the
     56     // squashing layer is not yet known.
     57     IntSize offsetFromBackingRoot;
     58 
     59     IntSize offsetFromRenderer;
     60 
     61     GraphicsLayerPaintingPhase paintingPhase;
     62 
     63     bool isBackgroundLayer;
     64 };
     65 
     66 // CompositedLayerMapping keeps track of how RenderLayers of the render tree correspond to
     67 // GraphicsLayers of the composited layer tree. Each instance of CompositedLayerMapping
     68 // manages a small cluster of GraphicsLayers and the references to which RenderLayers
     69 // and paint phases contribute to each GraphicsLayer.
     70 //
     71 // Currently (Oct. 2013) there is one CompositedLayerMapping for each RenderLayer,
     72 // but this is likely to evolve soon.
     73 class CompositedLayerMapping : public GraphicsLayerClient {
     74     WTF_MAKE_NONCOPYABLE(CompositedLayerMapping); WTF_MAKE_FAST_ALLOCATED;
     75 public:
     76     explicit CompositedLayerMapping(RenderLayer*);
     77     ~CompositedLayerMapping();
     78 
     79     RenderLayer* owningLayer() const { return m_owningLayer; }
     80 
     81     enum UpdateAfterLayoutFlag {
     82         CompositingChildrenOnly = 1 << 0,
     83         NeedsFullRepaint = 1 << 1,
     84         IsUpdateRoot = 1 << 2
     85     };
     86     typedef unsigned UpdateAfterLayoutFlags;
     87     void updateAfterLayout(UpdateAfterLayoutFlags);
     88 
     89     // Returns true if layer configuration changed.
     90     bool updateGraphicsLayerConfiguration();
     91     // Update graphics layer position and bounds.
     92     void updateGraphicsLayerGeometry(); // make private
     93     // Update whether layer needs blending.
     94     void updateContentsOpaque();
     95 
     96     GraphicsLayer* mainGraphicsLayer() const { return m_graphicsLayer.get(); }
     97 
     98     // Layer to clip children
     99     bool hasClippingLayer() const { return m_childContainmentLayer; }
    100     GraphicsLayer* clippingLayer() const { return m_childContainmentLayer.get(); }
    101 
    102     // Layer to get clipped by ancestor
    103     bool hasAncestorClippingLayer() const { return m_ancestorClippingLayer; }
    104     GraphicsLayer* ancestorClippingLayer() const { return m_ancestorClippingLayer.get(); }
    105 
    106     bool hasContentsLayer() const { return m_foregroundLayer; }
    107     GraphicsLayer* foregroundLayer() const { return m_foregroundLayer.get(); }
    108 
    109     GraphicsLayer* backgroundLayer() const { return m_backgroundLayer.get(); }
    110     bool backgroundLayerPaintsFixedRootBackground() const { return m_backgroundLayerPaintsFixedRootBackground; }
    111 
    112     bool hasScrollingLayer() const { return m_scrollingLayer; }
    113     GraphicsLayer* scrollingLayer() const { return m_scrollingLayer.get(); }
    114     GraphicsLayer* scrollingContentsLayer() const { return m_scrollingContentsLayer.get(); }
    115 
    116     bool hasMaskLayer() const { return m_maskLayer; }
    117     bool hasChildClippingMaskLayer() const { return m_childClippingMaskLayer; }
    118 
    119     GraphicsLayer* parentForSublayers() const;
    120     GraphicsLayer* childForSuperlayers() const;
    121 
    122     GraphicsLayer* squashingLayer() const { return m_squashingLayer.get(); }
    123 
    124     // Returns true for a composited layer that has no backing store of its own, so
    125     // paints into some ancestor layer.
    126     bool paintsIntoCompositedAncestor() const { return !(m_requiresOwnBackingStoreForAncestorReasons || m_requiresOwnBackingStoreForIntrinsicReasons); }
    127 
    128     // Updates whether a backing store is needed based on the layer's compositing ancestor's
    129     // properties; returns true if the need for a backing store for ancestor reasons changed.
    130     bool updateRequiresOwnBackingStoreForAncestorReasons(const RenderLayer* compositingAncestor);
    131 
    132     // Updates whether a backing store is needed for intrinsic reasons (that is, based on the
    133     // layer's own properties or compositing reasons); returns true if the intrinsic need for
    134     // a backing store changed.
    135     bool updateRequiresOwnBackingStoreForIntrinsicReasons();
    136 
    137     void setContentsNeedDisplay();
    138     // r is in the coordinate space of the layer's render object
    139     void setContentsNeedDisplayInRect(const IntRect&);
    140 
    141     // Notification from the renderer that its content changed.
    142     void contentChanged(ContentChangeType);
    143 
    144     // Interface to start, finish, suspend and resume animations and transitions
    145     bool startTransition(double, CSSPropertyID, const RenderStyle* fromStyle, const RenderStyle* toStyle);
    146     void transitionPaused(double timeOffset, CSSPropertyID);
    147     void transitionFinished(CSSPropertyID);
    148 
    149     bool startAnimation(double timeOffset, const CSSAnimationData*, const KeyframeList& keyframes);
    150     void animationPaused(double timeOffset, const String& name);
    151     void animationFinished(const String& name);
    152 
    153     IntRect compositedBounds() const;
    154     void setCompositedBounds(const IntRect&);
    155     void updateCompositedBounds();
    156 
    157     void updateAfterWidgetResize();
    158     void positionOverflowControlsLayers(const IntSize& offsetFromRoot);
    159     bool hasUnpositionedOverflowControlsLayers() const;
    160 
    161     void addRenderLayerToSquashingGraphicsLayer(RenderLayer*, IntSize offsetFromTargetBacking, size_t nextSquashedLayerIndex);
    162     void finishAccumulatingSquashingLayers(size_t nextSquashedLayerIndex);
    163 
    164     // GraphicsLayerClient interface
    165     virtual void notifyAnimationStarted(const GraphicsLayer*, double wallClockTime, double monotonicTime) OVERRIDE;
    166 
    167     virtual void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const IntRect& clip) OVERRIDE;
    168 
    169     virtual void didCommitChangesForLayer(const GraphicsLayer*) const OVERRIDE;
    170     virtual bool getCurrentTransform(const GraphicsLayer*, TransformationMatrix&) const OVERRIDE;
    171 
    172     virtual bool isTrackingRepaints() const OVERRIDE;
    173 
    174     PassOwnPtr<Vector<FloatRect> > collectTrackedRepaintRects() const;
    175 
    176 #ifndef NDEBUG
    177     virtual void verifyNotPainting();
    178 #endif
    179 
    180     IntRect contentsBox() const;
    181     IntRect backgroundBox() const;
    182 
    183     // For informative purposes only.
    184     CompositingLayerType compositingLayerType() const;
    185 
    186     GraphicsLayer* layerForHorizontalScrollbar() const { return m_layerForHorizontalScrollbar.get(); }
    187     GraphicsLayer* layerForVerticalScrollbar() const { return m_layerForVerticalScrollbar.get(); }
    188     GraphicsLayer* layerForScrollCorner() const { return m_layerForScrollCorner.get(); }
    189 
    190     void updateFilters(const RenderStyle*);
    191     bool canCompositeFilters() const { return m_canCompositeFilters; }
    192 
    193     // Return an estimate of the backing store area (in pixels) allocated by this object's GraphicsLayers.
    194     double backingStoreMemoryEstimate() const;
    195 
    196     void setBlendMode(blink::WebBlendMode);
    197 
    198     virtual String debugName(const GraphicsLayer*) OVERRIDE;
    199 
    200 private:
    201     void createPrimaryGraphicsLayer();
    202     void destroyGraphicsLayers();
    203 
    204     PassOwnPtr<GraphicsLayer> createGraphicsLayer(CompositingReasons);
    205 
    206     RenderLayerModelObject* renderer() const { return m_owningLayer->renderer(); }
    207     RenderLayerCompositor* compositor() const { return m_owningLayer->compositor(); }
    208 
    209     void updateInternalHierarchy();
    210     bool updateClippingLayers(bool needsAncestorClip, bool needsDescendantClip);
    211     bool updateOverflowControlsLayers(bool needsHorizontalScrollbarLayer, bool needsVerticalScrollbarLayer, bool needsScrollCornerLayer);
    212     bool updateForegroundLayer(bool needsForegroundLayer);
    213     bool updateBackgroundLayer(bool needsBackgroundLayer);
    214     bool updateMaskLayer(bool needsMaskLayer);
    215     bool updateClippingMaskLayers(bool needsChildClippingMaskLayer);
    216     bool requiresHorizontalScrollbarLayer() const { return m_owningLayer->scrollableArea() && m_owningLayer->scrollableArea()->horizontalScrollbar(); }
    217     bool requiresVerticalScrollbarLayer() const { return m_owningLayer->scrollableArea() && m_owningLayer->scrollableArea()->verticalScrollbar(); }
    218     bool requiresScrollCornerLayer() const { return m_owningLayer->scrollableArea() && !m_owningLayer->scrollableArea()->scrollCornerAndResizerRect().isEmpty(); }
    219     bool updateScrollingLayers(bool scrollingLayers);
    220     void updateScrollParent(RenderLayer*);
    221     void updateClipParent(RenderLayer*);
    222     bool updateSquashingLayers(bool needsSquashingLayers);
    223     void updateDrawsContent(bool isSimpleContainer);
    224     void registerScrollingLayers();
    225 
    226     void setBackgroundLayerPaintsFixedRootBackground(bool);
    227 
    228     GraphicsLayerPaintingPhase paintingPhaseForPrimaryLayer() const;
    229 
    230     IntSize contentOffsetInCompostingLayer() const;
    231     // Result is transform origin in pixels.
    232     FloatPoint3D computeTransformOrigin(const IntRect& borderBox) const;
    233     // Result is perspective origin in pixels.
    234     FloatPoint computePerspectiveOrigin(const IntRect& borderBox) const;
    235 
    236     void updateOpacity(const RenderStyle*);
    237     void updateTransform(const RenderStyle*);
    238     void updateLayerBlendMode(const RenderStyle*);
    239     void updateIsRootForIsolatedGroup();
    240     // Return the opacity value that this layer should use for compositing.
    241     float compositingOpacity(float rendererOpacity) const;
    242 
    243     bool isMainFrameRenderViewLayer() const;
    244 
    245     bool paintsBoxDecorations() const;
    246     bool paintsChildren() const;
    247 
    248     // Returns true if this compositing layer has no visible content.
    249     bool isSimpleContainerCompositingLayer() const;
    250     // Returns true if this layer has content that needs to be rendered by painting into the backing store.
    251     bool containsPaintedContent(bool isSimpleContainer) const;
    252     // Returns true if the RenderLayer just contains an image that we can composite directly.
    253     bool isDirectlyCompositedImage() const;
    254     void updateImageContents();
    255 
    256     Color rendererBackgroundColor() const;
    257     void updateBackgroundColor(bool isSimpleContainer);
    258     void updateContentsRect(bool isSimpleContainer);
    259 
    260     void updateCompositingReasons();
    261 
    262     bool hasVisibleNonCompositingDescendantLayers() const;
    263 
    264     bool shouldClipCompositedBounds() const;
    265 
    266     void paintsIntoCompositedAncestorChanged();
    267 
    268     void doPaintTask(GraphicsLayerPaintInfo&, GraphicsContext*, const IntRect& clip);
    269 
    270     RenderLayer* m_owningLayer;
    271 
    272     // The hierarchy of layers that is maintained by the CompositedLayerMapping looks like this:
    273     //
    274     //  + m_ancestorClippingLayer [OPTIONAL]
    275     //     + m_graphicsLayer
    276     //        + m_childContainmentLayer [OPTIONAL] <-OR-> m_scrollingLayer [OPTIONAL]
    277     //                                                     + m_scrollingContentsLayer [OPTIONAL]
    278     //
    279     // We need an ancestor clipping layer if our clipping ancestor is not our ancestor in the
    280     // clipping tree. Here's what that might look like.
    281     //
    282     // Let A = the clipping ancestor,
    283     //     B = the clip descendant, and
    284     //     SC = the stacking context that is the ancestor of A and B in the stacking tree.
    285     //
    286     // SC
    287     //  + A = m_graphicsLayer
    288     //  |  + m_childContainmentLayer
    289     //  |     + ...
    290     //  ...
    291     //  |
    292     //  + B = m_ancestorClippingLayer [+]
    293     //     + m_graphicsLayer
    294     //        + ...
    295     //
    296     // In this case B is clipped by another layer that doesn't happen to be its ancestor: A.
    297     // So we create an ancestor clipping layer for B, [+], which ensures that B is clipped
    298     // as if it had been A's descendant.
    299     OwnPtr<GraphicsLayer> m_ancestorClippingLayer; // Only used if we are clipped by an ancestor which is not a stacking context.
    300     OwnPtr<GraphicsLayer> m_graphicsLayer;
    301     OwnPtr<GraphicsLayer> m_childContainmentLayer; // Only used if we have clipping on a stacking context with compositing children.
    302     OwnPtr<GraphicsLayer> m_scrollingLayer; // Only used if the layer is using composited scrolling.
    303     OwnPtr<GraphicsLayer> m_scrollingContentsLayer; // Only used if the layer is using composited scrolling.
    304 
    305     // This layer is also added to the hierarchy by the RLB, but in a different way than
    306     // the layers above. It's added to m_graphicsLayer as its mask layer (naturally) if
    307     // we have a mask, and isn't part of the typical hierarchy (it has no children).
    308     OwnPtr<GraphicsLayer> m_maskLayer; // Only used if we have a mask.
    309     OwnPtr<GraphicsLayer> m_childClippingMaskLayer; // Only used if we have to clip child layers or accelerated contents with border radius or clip-path.
    310 
    311     // There are two other (optional) layers whose painting is managed by the CompositedLayerMapping,
    312     // but whose position in the hierarchy is maintained by the RenderLayerCompositor. These
    313     // are the foreground and background layers. The foreground layer exists if we have composited
    314     // descendants with negative z-order. We need the extra layer in this case because the layer
    315     // needs to draw both below (for the background, say) and above (for the normal flow content, say)
    316     // the negative z-order descendants and this is impossible with a single layer. The RLC handles
    317     // inserting m_foregroundLayer in the correct position in our descendant list for us (right after
    318     // the neg z-order dsecendants).
    319     //
    320     // The background layer is only created if this is the root layer and our background is entirely
    321     // fixed. In this case we want to put the background in a separate composited layer so that when
    322     // we scroll, we don't have to re-raster the background into position. This layer is also inserted
    323     // into the tree by the RLC as it gets a special home. This layer becomes a descendant of the
    324     // frame clipping layer. That is:
    325     //   ...
    326     //     + frame clipping layer
    327     //       + m_backgroundLayer
    328     //       + frame scrolling layer
    329     //         + root content layer
    330     //
    331     // With the hierarchy set up like this, the root content layer is able to scroll without affecting
    332     // the background layer (or repainting).
    333     OwnPtr<GraphicsLayer> m_foregroundLayer; // Only used in cases where we need to draw the foreground separately.
    334     OwnPtr<GraphicsLayer> m_backgroundLayer; // Only used in cases where we need to draw the background separately.
    335 
    336     OwnPtr<GraphicsLayer> m_layerForHorizontalScrollbar;
    337     OwnPtr<GraphicsLayer> m_layerForVerticalScrollbar;
    338     OwnPtr<GraphicsLayer> m_layerForScrollCorner;
    339 
    340     OwnPtr<WebAnimationProvider> m_animationProvider;
    341 
    342     OwnPtr<GraphicsLayer> m_squashingContainmentLayer; // Only used if any squashed layers exist, to contain the squashed layers as siblings to the rest of the GraphicsLayer tree chunk.
    343     OwnPtr<GraphicsLayer> m_squashingLayer; // Only used if any squashed layers exist, this is the backing that squashed layers paint into.
    344     Vector<GraphicsLayerPaintInfo> m_squashedLayers;
    345 
    346     IntRect m_compositedBounds;
    347 
    348     bool m_artificiallyInflatedBounds; // bounds had to be made non-zero to make transform-origin work
    349     bool m_boundsConstrainedByClipping;
    350     bool m_isMainFrameRenderViewLayer;
    351     bool m_requiresOwnBackingStoreForIntrinsicReasons;
    352     bool m_requiresOwnBackingStoreForAncestorReasons;
    353     bool m_canCompositeFilters;
    354     bool m_backgroundLayerPaintsFixedRootBackground;
    355 };
    356 
    357 } // namespace WebCore
    358 
    359 #endif // CompositedLayerMapping_h
    360