Home | History | Annotate | Download | only in rendering
      1 /*
      2  * Copyright (C) 2009 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 RenderLayerCompositor_h
     27 #define RenderLayerCompositor_h
     28 
     29 #include "RenderLayer.h"
     30 #include "RenderLayerBacking.h"
     31 
     32 namespace WebCore {
     33 
     34 #define PROFILE_LAYER_REBUILD 0
     35 
     36 class GraphicsLayer;
     37 #if ENABLE(VIDEO)
     38 class RenderVideo;
     39 #endif
     40 
     41 enum CompositingUpdateType {
     42     CompositingUpdateAfterLayoutOrStyleChange,
     43     CompositingUpdateOnPaitingOrHitTest,
     44     CompositingUpdateOnScroll
     45 };
     46 
     47 // RenderLayerCompositor manages the hierarchy of
     48 // composited RenderLayers. It determines which RenderLayers
     49 // become compositing, and creates and maintains a hierarchy of
     50 // GraphicsLayers based on the RenderLayer painting order.
     51 //
     52 // There is one RenderLayerCompositor per RenderView.
     53 
     54 class RenderLayerCompositor {
     55 public:
     56     RenderLayerCompositor(RenderView*);
     57     ~RenderLayerCompositor();
     58 
     59     // Return true if this RenderView is in "compositing mode" (i.e. has one or more
     60     // composited RenderLayers)
     61     bool inCompositingMode() const { return m_compositing; }
     62     // This will make a compositing layer at the root automatically, and hook up to
     63     // the native view/window system.
     64     void enableCompositingMode(bool enable = true);
     65 
     66     // Returns true if the accelerated compositing is enabled
     67     bool hasAcceleratedCompositing() const { return m_hasAcceleratedCompositing; }
     68 
     69     bool showDebugBorders() const { return m_showDebugBorders; }
     70     bool showRepaintCounter() const { return m_showRepaintCounter; }
     71 
     72     // Copy the accelerated compositing related flags from Settings
     73     void cacheAcceleratedCompositingFlags();
     74 
     75     // Called when the layer hierarchy needs to be updated (compositing layers have been
     76     // created, destroyed or re-parented).
     77     void setCompositingLayersNeedRebuild(bool needRebuild = true);
     78     bool compositingLayersNeedRebuild() const { return m_compositingLayersNeedRebuild; }
     79 
     80     // Controls whether or not to consult geometry when deciding which layers need
     81     // to be composited. Defaults to true.
     82     void setCompositingConsultsOverlap(bool b) { m_compositingConsultsOverlap = b; }
     83     bool compositingConsultsOverlap() const { return m_compositingConsultsOverlap; }
     84 
     85     void scheduleSync();
     86 
     87     // Rebuild the tree of compositing layers
     88     void updateCompositingLayers(CompositingUpdateType = CompositingUpdateAfterLayoutOrStyleChange, RenderLayer* updateRoot = 0);
     89 
     90     // Update the compositing state of the given layer. Returns true if that state changed.
     91     enum CompositingChangeRepaint { CompositingChangeRepaintNow, CompositingChangeWillRepaintLater };
     92     bool updateLayerCompositingState(RenderLayer*, CompositingChangeRepaint = CompositingChangeRepaintNow);
     93 
     94     // Update the geometry for compositing children of compositingAncestor.
     95     void updateCompositingDescendantGeometry(RenderLayer* compositingAncestor, RenderLayer* layer, RenderLayerBacking::UpdateDepth);
     96 
     97     // Whether layer's backing needs a graphics layer to do clipping by an ancestor (non-stacking-context parent with overflow).
     98     bool clippedByAncestor(RenderLayer*) const;
     99     // Whether layer's backing needs a graphics layer to clip z-order children of the given layer.
    100     bool clipsCompositingDescendants(const RenderLayer*) const;
    101 
    102     // Whether the given layer needs an extra 'contents' layer.
    103     bool needsContentsCompositingLayer(const RenderLayer*) const;
    104     // Return the bounding box required for compositing layer and its childern, relative to ancestorLayer.
    105     // If layerBoundingBox is not 0, on return it contains the bounding box of this layer only.
    106     IntRect calculateCompositedBounds(const RenderLayer* layer, const RenderLayer* ancestorLayer);
    107 
    108     // Repaint the appropriate layers when the given RenderLayer starts or stops being composited.
    109     void repaintOnCompositingChange(RenderLayer*);
    110 
    111     // Notify us that a layer has been added or removed
    112     void layerWasAdded(RenderLayer* parent, RenderLayer* child);
    113     void layerWillBeRemoved(RenderLayer* parent, RenderLayer* child);
    114 
    115     // Get the nearest ancestor layer that has overflow or clip, but is not a stacking context
    116     RenderLayer* enclosingNonStackingClippingLayer(const RenderLayer* layer) const;
    117 
    118     // Repaint parts of all composited layers that intersect the given absolute rectangle.
    119     void repaintCompositedLayersAbsoluteRect(const IntRect&);
    120 
    121     RenderLayer* rootRenderLayer() const;
    122     GraphicsLayer* rootPlatformLayer() const;
    123 
    124     void didMoveOnscreen();
    125     void willMoveOffscreen();
    126 
    127     void updateRootLayerPosition();
    128 
    129     void didStartAcceleratedAnimation();
    130 
    131 #if ENABLE(VIDEO)
    132     // Use by RenderVideo to ask if it should try to use accelerated compositing.
    133     bool canAccelerateVideoRendering(RenderVideo*) const;
    134 #endif
    135 
    136     // Walk the tree looking for layers with 3d transforms. Useful in case you need
    137     // to know if there is non-affine content, e.g. for drawing into an image.
    138     bool has3DContent() const;
    139 
    140 private:
    141     // Whether the given RL needs a compositing layer.
    142     bool needsToBeComposited(const RenderLayer*) const;
    143     // Whether the layer has an intrinsic need for compositing layer.
    144     bool requiresCompositingLayer(const RenderLayer*) const;
    145 
    146     // Make or destroy the backing for this layer; returns true if backing changed.
    147     bool updateBacking(RenderLayer*, CompositingChangeRepaint shouldRepaint);
    148 
    149     // Repaint the given rect (which is layer's coords), and regions of child layers that intersect that rect.
    150     void recursiveRepaintLayerRect(RenderLayer* layer, const IntRect& rect);
    151 
    152     typedef HashMap<RenderLayer*, IntRect> OverlapMap;
    153     static void addToOverlapMap(OverlapMap&, RenderLayer*, IntRect& layerBounds, bool& boundsComputed);
    154     static bool overlapsCompositedLayers(OverlapMap&, const IntRect& layerBounds);
    155 
    156     // Returns true if any layer's compositing changed
    157     void computeCompositingRequirements(RenderLayer*, OverlapMap*, struct CompositingState&, bool& layersChanged);
    158 
    159     // Recurses down the tree, parenting descendant compositing layers and collecting an array of child layers for the current compositing layer.
    160     void rebuildCompositingLayerTree(RenderLayer* layer, const struct CompositingState&, Vector<GraphicsLayer*>& childGraphicsLayersOfEnclosingLayer);
    161 
    162     // Recurses down the tree, updating layer geometry only.
    163     void updateLayerTreeGeometry(RenderLayer*);
    164 
    165     // Hook compositing layers together
    166     void setCompositingParent(RenderLayer* childLayer, RenderLayer* parentLayer);
    167     void removeCompositedChildren(RenderLayer*);
    168 
    169     void parentInRootLayer(RenderLayer*);
    170 
    171     bool layerHas3DContent(const RenderLayer*) const;
    172 
    173     void ensureRootPlatformLayer();
    174     void destroyRootPlatformLayer();
    175 
    176     // Whether a running transition or animation enforces the need for a compositing layer.
    177     bool requiresCompositingForAnimation(RenderObject*) const;
    178     bool requiresCompositingForTransform(RenderObject*) const;
    179     bool requiresCompositingForVideo(RenderObject*) const;
    180     bool requiresCompositingForCanvas(RenderObject*) const;
    181     bool requiresCompositingForPlugin(RenderObject*) const;
    182     bool requiresCompositingWhenDescendantsAreCompositing(RenderObject*) const;
    183 
    184 #if PLATFORM(ANDROID)
    185     // Whether we are on a mobile site
    186     bool requiresCompositingForMobileSites(const RenderLayer* layer) const;
    187 #endif
    188 
    189 private:
    190     RenderView* m_renderView;
    191     OwnPtr<GraphicsLayer> m_rootPlatformLayer;
    192     bool m_hasAcceleratedCompositing;
    193     bool m_showDebugBorders;
    194     bool m_showRepaintCounter;
    195     bool m_compositingConsultsOverlap;
    196     bool m_compositing;
    197     bool m_rootLayerAttached;
    198     bool m_compositingLayersNeedRebuild;
    199 
    200 #if PROFILE_LAYER_REBUILD
    201     int m_rootLayerUpdateCount;
    202 #endif
    203 };
    204 
    205 
    206 } // namespace WebCore
    207 
    208 #endif // RenderLayerCompositor_h
    209