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 RenderLayerBacking_h 27 #define RenderLayerBacking_h 28 29 #include "core/platform/graphics/FloatPoint.h" 30 #include "core/platform/graphics/FloatPoint3D.h" 31 #include "core/platform/graphics/GraphicsLayer.h" 32 #include "core/platform/graphics/GraphicsLayerClient.h" 33 #include "core/platform/graphics/transforms/TransformationMatrix.h" 34 #include "core/rendering/RenderLayer.h" 35 36 namespace WebCore { 37 38 class KeyframeList; 39 class RenderLayerCompositor; 40 41 enum CompositingLayerType { 42 NormalCompositingLayer, // non-tiled layer with backing store 43 MediaCompositingLayer, // layer that contains an image, video, webGL or plugin 44 ContainerCompositingLayer // layer with no backing store 45 }; 46 47 48 // A GraphicsLayerPaintInfo contains all the info needed to paint a partial subtree of RenderLayers into a GraphicsLayer. 49 struct GraphicsLayerPaintInfo { 50 RenderLayer* renderLayer; 51 52 IntRect compositedBounds; 53 54 IntSize offsetFromRenderer; 55 56 GraphicsLayerPaintingPhase paintingPhase; 57 58 bool isBackgroundLayer; 59 }; 60 61 // RenderLayerBacking controls the compositing behavior for a single RenderLayer. 62 // It holds the various GraphicsLayers, and makes decisions about intra-layer rendering 63 // optimizations. 64 // 65 // There is one RenderLayerBacking for each RenderLayer that is composited. 66 67 class RenderLayerBacking : public GraphicsLayerClient { 68 WTF_MAKE_NONCOPYABLE(RenderLayerBacking); WTF_MAKE_FAST_ALLOCATED; 69 public: 70 explicit RenderLayerBacking(RenderLayer*); 71 ~RenderLayerBacking(); 72 73 RenderLayer* owningLayer() const { return m_owningLayer; } 74 75 enum UpdateAfterLayoutFlag { 76 CompositingChildrenOnly = 1 << 0, 77 NeedsFullRepaint = 1 << 1, 78 IsUpdateRoot = 1 << 2 79 }; 80 typedef unsigned UpdateAfterLayoutFlags; 81 void updateAfterLayout(UpdateAfterLayoutFlags); 82 83 // Returns true if layer configuration changed. 84 bool updateGraphicsLayerConfiguration(); 85 // Update graphics layer position and bounds. 86 void updateGraphicsLayerGeometry(); // make private 87 // Update whether layer needs blending. 88 void updateContentsOpaque(); 89 90 GraphicsLayer* graphicsLayer() const { return m_graphicsLayer.get(); } 91 92 // Layer to clip children 93 bool hasClippingLayer() const { return m_childContainmentLayer; } 94 GraphicsLayer* clippingLayer() const { return m_childContainmentLayer.get(); } 95 96 // Layer to get clipped by ancestor 97 bool hasAncestorClippingLayer() const { return m_ancestorClippingLayer != 0; } 98 GraphicsLayer* ancestorClippingLayer() const { return m_ancestorClippingLayer.get(); } 99 100 bool hasContentsLayer() const { return m_foregroundLayer != 0; } 101 GraphicsLayer* foregroundLayer() const { return m_foregroundLayer.get(); } 102 103 GraphicsLayer* backgroundLayer() const { return m_backgroundLayer.get(); } 104 bool backgroundLayerPaintsFixedRootBackground() const { return m_backgroundLayerPaintsFixedRootBackground; } 105 106 bool hasScrollingLayer() const { return m_scrollingLayer; } 107 GraphicsLayer* scrollingLayer() const { return m_scrollingLayer.get(); } 108 GraphicsLayer* scrollingContentsLayer() const { return m_scrollingContentsLayer.get(); } 109 110 bool hasMaskLayer() const { return m_maskLayer != 0; } 111 112 GraphicsLayer* parentForSublayers() const; 113 GraphicsLayer* childForSuperlayers() const; 114 115 // Returns true for a composited layer that has no backing store of its own, so 116 // paints into some ancestor layer. 117 bool paintsIntoCompositedAncestor() const { return !m_requiresOwnBackingStore; } 118 119 void setRequiresOwnBackingStore(bool); 120 121 void setContentsNeedDisplay(); 122 // r is in the coordinate space of the layer's render object 123 void setContentsNeedDisplayInRect(const IntRect&); 124 125 // Notification from the renderer that its content changed. 126 void contentChanged(ContentChangeType); 127 128 // Interface to start, finish, suspend and resume animations and transitions 129 bool startTransition(double, CSSPropertyID, const RenderStyle* fromStyle, const RenderStyle* toStyle); 130 void transitionPaused(double timeOffset, CSSPropertyID); 131 void transitionFinished(CSSPropertyID); 132 133 bool startAnimation(double timeOffset, const CSSAnimationData* anim, const KeyframeList& keyframes); 134 void animationPaused(double timeOffset, const String& name); 135 void animationFinished(const String& name); 136 137 void suspendAnimations(double time = 0); 138 void resumeAnimations(); 139 140 IntRect compositedBounds() const; 141 void setCompositedBounds(const IntRect&); 142 void updateCompositedBounds(); 143 144 void updateAfterWidgetResize(); 145 void positionOverflowControlsLayers(const IntSize& offsetFromRoot); 146 bool hasUnpositionedOverflowControlsLayers() const; 147 148 // GraphicsLayerClient interface 149 virtual void notifyAnimationStarted(const GraphicsLayer*, double startTime) OVERRIDE; 150 151 virtual void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const IntRect& clip) OVERRIDE; 152 153 virtual void didCommitChangesForLayer(const GraphicsLayer*) const OVERRIDE; 154 virtual bool getCurrentTransform(const GraphicsLayer*, TransformationMatrix&) const OVERRIDE; 155 156 virtual bool isTrackingRepaints() const OVERRIDE; 157 158 #ifndef NDEBUG 159 virtual void verifyNotPainting(); 160 #endif 161 162 IntRect contentsBox() const; 163 IntRect backgroundBox() const; 164 165 // For informative purposes only. 166 CompositingLayerType compositingLayerType() const; 167 168 GraphicsLayer* layerForHorizontalScrollbar() const { return m_layerForHorizontalScrollbar.get(); } 169 GraphicsLayer* layerForVerticalScrollbar() const { return m_layerForVerticalScrollbar.get(); } 170 GraphicsLayer* layerForScrollCorner() const { return m_layerForScrollCorner.get(); } 171 172 void updateFilters(const RenderStyle*); 173 bool canCompositeFilters() const { return m_canCompositeFilters; } 174 175 // Return an estimate of the backing store area (in pixels) allocated by this object's GraphicsLayers. 176 double backingStoreMemoryEstimate() const; 177 178 void setBlendMode(BlendMode); 179 180 private: 181 void createPrimaryGraphicsLayer(); 182 void destroyGraphicsLayers(); 183 184 PassOwnPtr<GraphicsLayer> createGraphicsLayer(const String& name, CompositingReasons); 185 186 RenderLayerModelObject* renderer() const { return m_owningLayer->renderer(); } 187 RenderLayerCompositor* compositor() const { return m_owningLayer->compositor(); } 188 189 void updateInternalHierarchy(); 190 bool updateClippingLayers(bool needsAncestorClip, bool needsDescendantClip); 191 bool updateOverflowControlsLayers(bool needsHorizontalScrollbarLayer, bool needsVerticalScrollbarLayer, bool needsScrollCornerLayer); 192 bool updateForegroundLayer(bool needsForegroundLayer); 193 bool updateBackgroundLayer(bool needsBackgroundLayer); 194 bool updateMaskLayer(bool needsMaskLayer); 195 bool requiresHorizontalScrollbarLayer() const { return m_owningLayer->horizontalScrollbar(); } 196 bool requiresVerticalScrollbarLayer() const { return m_owningLayer->verticalScrollbar(); } 197 bool requiresScrollCornerLayer() const { return !m_owningLayer->scrollCornerAndResizerRect().isEmpty(); } 198 bool updateScrollingLayers(bool scrollingLayers); 199 void updateDrawsContent(bool isSimpleContainer); 200 void registerScrollingLayers(); 201 202 void setBackgroundLayerPaintsFixedRootBackground(bool); 203 204 GraphicsLayerPaintingPhase paintingPhaseForPrimaryLayer() const; 205 206 IntSize contentOffsetInCompostingLayer() const; 207 // Result is transform origin in pixels. 208 FloatPoint3D computeTransformOrigin(const IntRect& borderBox) const; 209 // Result is perspective origin in pixels. 210 FloatPoint computePerspectiveOrigin(const IntRect& borderBox) const; 211 212 void updateOpacity(const RenderStyle*); 213 void updateTransform(const RenderStyle*); 214 void updateLayerBlendMode(const RenderStyle*); 215 // Return the opacity value that this layer should use for compositing. 216 float compositingOpacity(float rendererOpacity) const; 217 218 bool isMainFrameRenderViewLayer() const; 219 220 bool paintsBoxDecorations() const; 221 bool paintsChildren() const; 222 223 // Returns true if this compositing layer has no visible content. 224 bool isSimpleContainerCompositingLayer() const; 225 // Returns true if this layer has content that needs to be rendered by painting into the backing store. 226 bool containsPaintedContent(bool isSimpleContainer) const; 227 // Returns true if the RenderLayer just contains an image that we can composite directly. 228 bool isDirectlyCompositedImage() const; 229 void updateImageContents(); 230 231 Color rendererBackgroundColor() const; 232 void updateBackgroundColor(bool isSimpleContainer); 233 void updateContentsRect(bool isSimpleContainer); 234 235 void updateCompositingReasons(); 236 237 bool hasVisibleNonCompositingDescendantLayers() const; 238 239 bool shouldClipCompositedBounds() const; 240 241 void doPaintTask(GraphicsLayerPaintInfo&, GraphicsContext*, const IntRect& clip); 242 243 static CSSPropertyID graphicsLayerToCSSProperty(AnimatedPropertyID); 244 static AnimatedPropertyID cssToGraphicsLayerProperty(CSSPropertyID); 245 246 RenderLayer* m_owningLayer; 247 248 OwnPtr<GraphicsLayer> m_ancestorClippingLayer; // Only used if we are clipped by an ancestor which is not a stacking context. 249 OwnPtr<GraphicsLayer> m_graphicsLayer; 250 OwnPtr<GraphicsLayer> m_foregroundLayer; // Only used in cases where we need to draw the foreground separately. 251 OwnPtr<GraphicsLayer> m_backgroundLayer; // Only used in cases where we need to draw the background separately. 252 OwnPtr<GraphicsLayer> m_childContainmentLayer; // Only used if we have clipping on a stacking context with compositing children, or if the layer has a tile cache. 253 OwnPtr<GraphicsLayer> m_maskLayer; // Only used if we have a mask. 254 255 OwnPtr<GraphicsLayer> m_layerForHorizontalScrollbar; 256 OwnPtr<GraphicsLayer> m_layerForVerticalScrollbar; 257 OwnPtr<GraphicsLayer> m_layerForScrollCorner; 258 259 OwnPtr<GraphicsLayer> m_scrollingLayer; // Only used if the layer is using composited scrolling. 260 OwnPtr<GraphicsLayer> m_scrollingContentsLayer; // Only used if the layer is using composited scrolling. 261 262 uint64_t m_scrollLayerID; 263 264 IntRect m_compositedBounds; 265 266 bool m_artificiallyInflatedBounds; // bounds had to be made non-zero to make transform-origin work 267 bool m_boundsConstrainedByClipping; 268 bool m_isMainFrameRenderViewLayer; 269 bool m_requiresOwnBackingStore; 270 bool m_canCompositeFilters; 271 bool m_backgroundLayerPaintsFixedRootBackground; 272 }; 273 274 } // namespace WebCore 275 276 #endif // RenderLayerBacking_h 277