1 /* 2 * Copyright (C) 2009 Apple Inc. All rights reserved. 3 * Copyright (C) 2013 Intel Corporation. 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 #ifndef GraphicsLayer_h 28 #define GraphicsLayer_h 29 30 #include "platform/PlatformExport.h" 31 #include "platform/geometry/FloatPoint.h" 32 #include "platform/geometry/FloatPoint3D.h" 33 #include "platform/geometry/FloatSize.h" 34 #include "platform/geometry/IntRect.h" 35 #include "platform/graphics/Color.h" 36 #include "platform/graphics/GraphicsLayerClient.h" 37 #include "platform/graphics/GraphicsLayerDebugInfo.h" 38 #include "platform/graphics/OpaqueRectTrackingContentLayerDelegate.h" 39 #include "platform/graphics/filters/FilterOperations.h" 40 #include "platform/transforms/TransformationMatrix.h" 41 #include "public/platform/WebAnimationDelegate.h" 42 #include "public/platform/WebContentLayer.h" 43 #include "public/platform/WebImageLayer.h" 44 #include "public/platform/WebLayerClient.h" 45 #include "public/platform/WebLayerScrollClient.h" 46 #include "public/platform/WebNinePatchLayer.h" 47 #include "public/platform/WebSolidColorLayer.h" 48 #include "wtf/OwnPtr.h" 49 #include "wtf/PassOwnPtr.h" 50 #include "wtf/Vector.h" 51 52 namespace blink { 53 class GraphicsLayerFactoryChromium; 54 class WebAnimation; 55 class WebLayer; 56 } 57 58 namespace WebCore { 59 60 class FloatRect; 61 class GraphicsContext; 62 class GraphicsLayer; 63 class GraphicsLayerFactory; 64 class Image; 65 class ScrollableArea; 66 class TextStream; 67 68 // FIXME: find a better home for this declaration. 69 class PLATFORM_EXPORT LinkHighlightClient { 70 public: 71 virtual void invalidate() = 0; 72 virtual void clearCurrentGraphicsLayer() = 0; 73 virtual blink::WebLayer* layer() = 0; 74 75 protected: 76 virtual ~LinkHighlightClient() { } 77 }; 78 79 typedef Vector<GraphicsLayer*, 64> GraphicsLayerVector; 80 81 // GraphicsLayer is an abstraction for a rendering surface with backing store, 82 // which may have associated transformation and animations. 83 84 class PLATFORM_EXPORT GraphicsLayer : public GraphicsContextPainter, public blink::WebAnimationDelegate, public blink::WebLayerScrollClient, public blink::WebLayerClient { 85 WTF_MAKE_NONCOPYABLE(GraphicsLayer); WTF_MAKE_FAST_ALLOCATED; 86 public: 87 static PassOwnPtr<GraphicsLayer> create(GraphicsLayerFactory*, GraphicsLayerClient*); 88 89 virtual ~GraphicsLayer(); 90 91 GraphicsLayerClient* client() const { return m_client; } 92 93 // blink::WebLayerClient implementation. 94 virtual blink::WebGraphicsLayerDebugInfo* takeDebugInfoFor(blink::WebLayer*) OVERRIDE; 95 96 GraphicsLayerDebugInfo& debugInfo(); 97 98 void setCompositingReasons(CompositingReasons); 99 CompositingReasons compositingReasons() const { return m_debugInfo.compositingReasons(); } 100 void setOwnerNodeId(int); 101 102 GraphicsLayer* parent() const { return m_parent; }; 103 void setParent(GraphicsLayer*); // Internal use only. 104 105 const Vector<GraphicsLayer*>& children() const { return m_children; } 106 // Returns true if the child list changed. 107 bool setChildren(const GraphicsLayerVector&); 108 109 // Add child layers. If the child is already parented, it will be removed from its old parent. 110 void addChild(GraphicsLayer*); 111 void addChildAtIndex(GraphicsLayer*, int index); 112 void addChildAbove(GraphicsLayer*, GraphicsLayer* sibling); 113 void addChildBelow(GraphicsLayer*, GraphicsLayer* sibling); 114 bool replaceChild(GraphicsLayer* oldChild, GraphicsLayer* newChild); 115 116 void removeAllChildren(); 117 void removeFromParent(); 118 119 GraphicsLayer* maskLayer() const { return m_maskLayer; } 120 void setMaskLayer(GraphicsLayer*); 121 122 GraphicsLayer* contentsClippingMaskLayer() const { return m_contentsClippingMaskLayer; } 123 void setContentsClippingMaskLayer(GraphicsLayer*); 124 125 // The given layer will replicate this layer and its children; the replica renders behind this layer. 126 void setReplicatedByLayer(GraphicsLayer*); 127 // The layer that replicates this layer (if any). 128 GraphicsLayer* replicaLayer() const { return m_replicaLayer; } 129 // The layer being replicated. 130 GraphicsLayer* replicatedLayer() const { return m_replicatedLayer; } 131 132 enum ShouldSetNeedsDisplay { 133 DontSetNeedsDisplay, 134 SetNeedsDisplay 135 }; 136 137 // Offset is origin of the renderer minus origin of the graphics layer (so either zero or negative). 138 IntSize offsetFromRenderer() const { return m_offsetFromRenderer; } 139 void setOffsetFromRenderer(const IntSize&, ShouldSetNeedsDisplay = SetNeedsDisplay); 140 141 // The position of the layer (the location of its top-left corner in its parent) 142 const FloatPoint& position() const { return m_position; } 143 void setPosition(const FloatPoint&); 144 145 const FloatPoint3D& transformOrigin() const { return m_transformOrigin; } 146 void setTransformOrigin(const FloatPoint3D&); 147 148 // The size of the layer. 149 const FloatSize& size() const { return m_size; } 150 void setSize(const FloatSize&); 151 152 // The boundOrigin affects the offset at which content is rendered, and sublayers are positioned. 153 const FloatPoint& boundsOrigin() const { return m_boundsOrigin; } 154 void setBoundsOrigin(const FloatPoint& origin) { m_boundsOrigin = origin; } 155 156 const TransformationMatrix& transform() const { return m_transform; } 157 void setTransform(const TransformationMatrix&); 158 void setShouldFlattenTransform(bool); 159 void setRenderingContext(int id); 160 void setMasksToBounds(bool); 161 162 bool drawsContent() const { return m_drawsContent; } 163 void setDrawsContent(bool); 164 165 bool contentsAreVisible() const { return m_contentsVisible; } 166 void setContentsVisible(bool); 167 168 void setScrollParent(blink::WebLayer*); 169 void setClipParent(blink::WebLayer*); 170 171 // For special cases, e.g. drawing missing tiles on Android. 172 // The compositor should never paint this color in normal cases because the RenderLayer 173 // will paint background by itself. 174 void setBackgroundColor(const Color&); 175 176 // opaque means that we know the layer contents have no alpha 177 bool contentsOpaque() const { return m_contentsOpaque; } 178 void setContentsOpaque(bool); 179 180 bool backfaceVisibility() const { return m_backfaceVisibility; } 181 void setBackfaceVisibility(bool visible); 182 183 float opacity() const { return m_opacity; } 184 void setOpacity(float); 185 186 void setBlendMode(blink::WebBlendMode); 187 void setIsRootForIsolatedGroup(bool); 188 189 // Returns true if filter can be rendered by the compositor 190 bool setFilters(const FilterOperations&); 191 void setBackgroundFilters(const FilterOperations&); 192 193 // Some GraphicsLayers paint only the foreground or the background content 194 void setPaintingPhase(GraphicsLayerPaintingPhase); 195 196 void setNeedsDisplay(); 197 // mark the given rect (in layer coords) as needing dispay. Never goes deep. 198 void setNeedsDisplayInRect(const FloatRect&); 199 200 void setContentsNeedsDisplay(); 201 202 // Set that the position/size of the contents (image or video). 203 void setContentsRect(const IntRect&); 204 205 // Return true if the animation is handled by the compositing system. If this returns 206 // false, the animation will be run by AnimationController. 207 // These methods handle both transitions and keyframe animations. 208 bool addAnimation(PassOwnPtr<blink::WebAnimation>); 209 void pauseAnimation(int animationId, double /*timeOffset*/); 210 void removeAnimation(int animationId); 211 212 // Layer contents 213 void setContentsToImage(Image*); 214 void setContentsToNinePatch(Image*, const IntRect& aperture); 215 void setContentsToPlatformLayer(blink::WebLayer* layer) { setContentsTo(layer); } 216 bool hasContentsLayer() const { return m_contentsLayer; } 217 218 // Callback from the underlying graphics system to draw layer contents. 219 void paintGraphicsLayerContents(GraphicsContext&, const IntRect& clip); 220 221 // For hosting this GraphicsLayer in a native layer hierarchy. 222 blink::WebLayer* platformLayer() const; 223 224 typedef HashMap<int, int> RenderingContextMap; 225 void dumpLayer(TextStream&, int indent, LayerTreeFlags, RenderingContextMap&) const; 226 227 int paintCount() const { return m_paintCount; } 228 229 // Return a string with a human readable form of the layer tree, If debug is true 230 // pointers for the layers and timing data will be included in the returned string. 231 String layerTreeAsText(LayerTreeFlags = LayerTreeNormal) const; 232 String debugName(blink::WebLayer*) const; 233 234 void resetTrackedRepaints(); 235 void addRepaintRect(const FloatRect&); 236 237 void collectTrackedRepaintRects(Vector<FloatRect>&) const; 238 239 void addLinkHighlight(LinkHighlightClient*); 240 void removeLinkHighlight(LinkHighlightClient*); 241 // Exposed for tests 242 unsigned numLinkHighlights() { return m_linkHighlights.size(); } 243 LinkHighlightClient* linkHighlight(int i) { return m_linkHighlights[i]; } 244 245 void setScrollableArea(ScrollableArea*, bool isMainFrame); 246 ScrollableArea* scrollableArea() const { return m_scrollableArea; } 247 248 blink::WebContentLayer* contentLayer() const { return m_layer.get(); } 249 250 static void registerContentsLayer(blink::WebLayer*); 251 static void unregisterContentsLayer(blink::WebLayer*); 252 253 // GraphicsContextPainter implementation. 254 virtual void paint(GraphicsContext&, const IntRect& clip) OVERRIDE; 255 256 // WebAnimationDelegate implementation. 257 virtual void notifyAnimationStarted(double monotonicTime, blink::WebAnimation::TargetProperty) OVERRIDE; 258 virtual void notifyAnimationFinished(double monotonicTime, blink::WebAnimation::TargetProperty) OVERRIDE; 259 260 // WebLayerScrollClient implementation. 261 virtual void didScroll() OVERRIDE; 262 263 protected: 264 explicit GraphicsLayer(GraphicsLayerClient*); 265 // GraphicsLayerFactoryChromium that wants to create a GraphicsLayer need to be friends. 266 friend class blink::GraphicsLayerFactoryChromium; 267 268 // Exposed for tests. 269 virtual blink::WebLayer* contentsLayer() const { return m_contentsLayer; } 270 271 private: 272 // Adds a child without calling updateChildList(), so that adding children 273 // can be batched before updating. 274 void addChildInternal(GraphicsLayer*); 275 276 #if ASSERT_ENABLED 277 bool hasAncestor(GraphicsLayer*) const; 278 #endif 279 280 // This method is used by platform GraphicsLayer classes to clear the filters 281 // when compositing is not done in hardware. It is not virtual, so the caller 282 // needs to notifiy the change to the platform layer as needed. 283 void clearFilters() { m_filters.clear(); } 284 285 void setReplicatedLayer(GraphicsLayer* layer) { m_replicatedLayer = layer; } 286 287 int incrementPaintCount() { return ++m_paintCount; } 288 289 void dumpProperties(TextStream&, int indent, LayerTreeFlags, RenderingContextMap&) const; 290 291 // Helper functions used by settors to keep layer's the state consistent. 292 void updateChildList(); 293 void updateLayerIsDrawable(); 294 void updateContentsRect(); 295 296 void setContentsTo(blink::WebLayer*); 297 void setupContentsLayer(blink::WebLayer*); 298 void clearContentsLayerIfUnregistered(); 299 blink::WebLayer* contentsLayerIfRegistered(); 300 301 GraphicsLayerClient* m_client; 302 303 // Offset from the owning renderer 304 IntSize m_offsetFromRenderer; 305 306 // Position is relative to the parent GraphicsLayer 307 FloatPoint m_position; 308 FloatSize m_size; 309 FloatPoint m_boundsOrigin; 310 311 TransformationMatrix m_transform; 312 FloatPoint3D m_transformOrigin; 313 314 Color m_backgroundColor; 315 float m_opacity; 316 317 blink::WebBlendMode m_blendMode; 318 319 FilterOperations m_filters; 320 321 bool m_hasTransformOrigin : 1; 322 bool m_contentsOpaque : 1; 323 bool m_shouldFlattenTransform: 1; 324 bool m_backfaceVisibility : 1; 325 bool m_masksToBounds : 1; 326 bool m_drawsContent : 1; 327 bool m_contentsVisible : 1; 328 bool m_isRootForIsolatedGroup : 1; 329 330 bool m_hasScrollParent : 1; 331 bool m_hasClipParent : 1; 332 333 GraphicsLayerPaintingPhase m_paintingPhase; 334 335 Vector<GraphicsLayer*> m_children; 336 GraphicsLayer* m_parent; 337 338 GraphicsLayer* m_maskLayer; // Reference to mask layer. We don't own this. 339 GraphicsLayer* m_contentsClippingMaskLayer; // Reference to clipping mask layer. We don't own this. 340 341 GraphicsLayer* m_replicaLayer; // A layer that replicates this layer. We only allow one, for now. 342 // The replica is not parented; this is the primary reference to it. 343 GraphicsLayer* m_replicatedLayer; // For a replica layer, a reference to the original layer. 344 FloatPoint m_replicatedLayerPosition; // For a replica layer, the position of the replica. 345 346 IntRect m_contentsRect; 347 348 int m_paintCount; 349 350 OwnPtr<blink::WebContentLayer> m_layer; 351 OwnPtr<blink::WebImageLayer> m_imageLayer; 352 OwnPtr<blink::WebNinePatchLayer> m_ninePatchLayer; 353 blink::WebLayer* m_contentsLayer; 354 // We don't have ownership of m_contentsLayer, but we do want to know if a given layer is the 355 // same as our current layer in setContentsTo(). Since m_contentsLayer may be deleted at this point, 356 // we stash an ID away when we know m_contentsLayer is alive and use that for comparisons from that point 357 // on. 358 int m_contentsLayerId; 359 360 Vector<LinkHighlightClient*> m_linkHighlights; 361 362 OwnPtr<OpaqueRectTrackingContentLayerDelegate> m_opaqueRectTrackingContentLayerDelegate; 363 364 ScrollableArea* m_scrollableArea; 365 GraphicsLayerDebugInfo m_debugInfo; 366 int m_3dRenderingContext; 367 }; 368 369 } // namespace WebCore 370 371 #ifndef NDEBUG 372 // Outside the WebCore namespace for ease of invocation from gdb. 373 void PLATFORM_EXPORT showGraphicsLayerTree(const WebCore::GraphicsLayer*); 374 #endif 375 376 #endif // GraphicsLayer_h 377