2 * Copyright (C) 2009 Apple Inc. All rights reserved.
3 * Copyright (C) 2013 Intel Corporation. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #ifndef GraphicsLayer_h
28 #define GraphicsLayer_h
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"
53 class GraphicsLayerFactoryChromium;
61 class GraphicsContext;
63 class GraphicsLayerFactory;
68 // FIXME: find a better home for this declaration.
69 class PLATFORM_EXPORT LinkHighlightClient {
71 virtual void invalidate() = 0;
72 virtual void clearCurrentGraphicsLayer() = 0;
73 virtual blink::WebLayer* layer() = 0;
76 virtual ~LinkHighlightClient() { }
79 typedef Vector<GraphicsLayer*, 64> GraphicsLayerVector;
81 // GraphicsLayer is an abstraction for a rendering surface with backing store,
82 // which may have associated transformation and animations.
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;
87 static PassOwnPtr<GraphicsLayer> create(GraphicsLayerFactory*, GraphicsLayerClient*);
89 virtual ~GraphicsLayer();
91 GraphicsLayerClient* client() const { return m_client; }
93 // blink::WebLayerClient implementation.
94 virtual blink::WebGraphicsLayerDebugInfo* takeDebugInfoFor(blink::WebLayer*) OVERRIDE;
96 GraphicsLayerDebugInfo& debugInfo();
98 void setCompositingReasons(CompositingReasons);
99 CompositingReasons compositingReasons() const { return m_debugInfo.compositingReasons(); }
101 GraphicsLayer* parent() const { return m_parent; };
102 void setParent(GraphicsLayer*); // Internal use only.
104 // Returns true if the layer has the given layer as an ancestor (excluding self).
105 bool hasAncestor(GraphicsLayer*) const;
107 const Vector<GraphicsLayer*>& children() const { return m_children; }
108 // Returns true if the child list changed.
109 bool setChildren(const GraphicsLayerVector&);
111 // Add child layers. If the child is already parented, it will be removed from its old parent.
112 void addChild(GraphicsLayer*);
113 void addChildAtIndex(GraphicsLayer*, int index);
114 void addChildAbove(GraphicsLayer*, GraphicsLayer* sibling);
115 void addChildBelow(GraphicsLayer*, GraphicsLayer* sibling);
116 bool replaceChild(GraphicsLayer* oldChild, GraphicsLayer* newChild);
118 void removeAllChildren();
119 void removeFromParent();
121 GraphicsLayer* maskLayer() const { return m_maskLayer; }
122 void setMaskLayer(GraphicsLayer*);
124 GraphicsLayer* contentsClippingMaskLayer() const { return m_contentsClippingMaskLayer; }
125 void setContentsClippingMaskLayer(GraphicsLayer*);
127 // The given layer will replicate this layer and its children; the replica renders behind this layer.
128 void setReplicatedByLayer(GraphicsLayer*);
129 // Whether this layer is being replicated by another layer.
130 bool isReplicated() const { return m_replicaLayer; }
131 // The layer that replicates this layer (if any).
132 GraphicsLayer* replicaLayer() const { return m_replicaLayer; }
133 // The layer being replicated.
134 GraphicsLayer* replicatedLayer() const { return m_replicatedLayer; }
136 const FloatPoint& replicatedLayerPosition() const { return m_replicatedLayerPosition; }
137 void setReplicatedLayerPosition(const FloatPoint& p) { m_replicatedLayerPosition = p; }
139 enum ShouldSetNeedsDisplay {
144 // Offset is origin of the renderer minus origin of the graphics layer (so either zero or negative).
145 IntSize offsetFromRenderer() const { return m_offsetFromRenderer; }
146 void setOffsetFromRenderer(const IntSize&, ShouldSetNeedsDisplay = SetNeedsDisplay);
148 // The position of the layer (the location of its top-left corner in its parent)
149 const FloatPoint& position() const { return m_position; }
150 void setPosition(const FloatPoint&);
152 // Anchor point: (0, 0) is top left, (1, 1) is bottom right. The anchor point
153 // affects the origin of the transforms.
154 const FloatPoint3D& anchorPoint() const { return m_anchorPoint; }
155 void setAnchorPoint(const FloatPoint3D&);
157 // The size of the layer.
158 const FloatSize& size() const { return m_size; }
159 void setSize(const FloatSize&);
161 // The boundOrigin affects the offset at which content is rendered, and sublayers are positioned.
162 const FloatPoint& boundsOrigin() const { return m_boundsOrigin; }
163 void setBoundsOrigin(const FloatPoint& origin) { m_boundsOrigin = origin; }
165 const TransformationMatrix& transform() const { return m_transform; }
166 void setTransform(const TransformationMatrix&);
168 bool shouldFlattenTransform() const { return m_shouldFlattenTransform; }
169 void setShouldFlattenTransform(bool);
171 int renderingContext() const { return m_3dRenderingContext; }
172 void setRenderingContext(int id);
174 bool masksToBounds() const { return m_masksToBounds; }
175 void setMasksToBounds(bool);
177 bool drawsContent() const { return m_drawsContent; }
178 void setDrawsContent(bool);
180 bool contentsAreVisible() const { return m_contentsVisible; }
181 void setContentsVisible(bool);
183 void setScrollParent(blink::WebLayer*);
184 void setClipParent(blink::WebLayer*);
186 // For special cases, e.g. drawing missing tiles on Android.
187 // The compositor should never paint this color in normal cases because the RenderLayer
188 // will paint background by itself.
189 const Color& backgroundColor() const { return m_backgroundColor; }
190 void setBackgroundColor(const Color&);
192 // opaque means that we know the layer contents have no alpha
193 bool contentsOpaque() const { return m_contentsOpaque; }
194 void setContentsOpaque(bool);
196 bool backfaceVisibility() const { return m_backfaceVisibility; }
197 void setBackfaceVisibility(bool visible);
199 float opacity() const { return m_opacity; }
200 void setOpacity(float);
202 blink::WebBlendMode blendMode() const { return m_blendMode; }
203 void setBlendMode(blink::WebBlendMode);
205 bool isRootForIsolatedGroup() const { return m_isRootForIsolatedGroup; }
206 void setIsRootForIsolatedGroup(bool);
208 const FilterOperations& filters() const { return m_filters; }
210 // Returns true if filter can be rendered by the compositor
211 bool setFilters(const FilterOperations&);
212 void setBackgroundFilters(const FilterOperations&);
214 // Some GraphicsLayers paint only the foreground or the background content
215 GraphicsLayerPaintingPhase paintingPhase() const { return m_paintingPhase; }
216 void setPaintingPhase(GraphicsLayerPaintingPhase);
218 void setNeedsDisplay();
219 // mark the given rect (in layer coords) as needing dispay. Never goes deep.
220 void setNeedsDisplayInRect(const FloatRect&);
222 void setContentsNeedsDisplay();
224 // Set that the position/size of the contents (image or video).
225 IntRect contentsRect() const { return m_contentsRect; }
226 void setContentsRect(const IntRect&);
228 // Return true if the animation is handled by the compositing system. If this returns
229 // false, the animation will be run by AnimationController.
230 // These methods handle both transitions and keyframe animations.
231 bool addAnimation(PassOwnPtr<blink::WebAnimation>);
232 void pauseAnimation(int animationId, double /*timeOffset*/);
233 void removeAnimation(int animationId);
236 void setContentsToImage(Image*);
237 void setContentsToNinePatch(Image*, const IntRect& aperture);
238 void setContentsToPlatformLayer(blink::WebLayer* layer) { setContentsTo(layer); }
239 bool hasContentsLayer() const { return m_contentsLayer; }
241 // Callback from the underlying graphics system to draw layer contents.
242 void paintGraphicsLayerContents(GraphicsContext&, const IntRect& clip);
244 // For hosting this GraphicsLayer in a native layer hierarchy.
245 blink::WebLayer* platformLayer() const;
247 enum CompositingCoordinatesOrientation { CompositingCoordinatesTopDown, CompositingCoordinatesBottomUp };
249 // Flippedness of the contents of this layer. Does not affect sublayer geometry.
250 void setContentsOrientation(CompositingCoordinatesOrientation orientation) { m_contentsOrientation = orientation; }
251 CompositingCoordinatesOrientation contentsOrientation() const { return m_contentsOrientation; }
253 typedef HashMap<int, int> RenderingContextMap;
254 void dumpLayer(TextStream&, int indent, LayerTreeFlags, RenderingContextMap&) const;
256 int paintCount() const { return m_paintCount; }
258 // z-position is the z-equivalent of position(). It's only used for debugging purposes.
259 float zPosition() const { return m_zPosition; }
260 void setZPosition(float);
262 // If the exposed rect of this layer changes, returns true if this or descendant layers need a flush,
263 // for example to allocate new tiles.
264 bool visibleRectChangeRequiresFlush(const FloatRect& /* clipRect */) const { return false; }
266 // Return a string with a human readable form of the layer tree, If debug is true
267 // pointers for the layers and timing data will be included in the returned string.
268 String layerTreeAsText(LayerTreeFlags = LayerTreeNormal) const;
269 String debugName(blink::WebLayer*) const;
271 void resetTrackedRepaints();
272 void addRepaintRect(const FloatRect&);
274 void collectTrackedRepaintRects(Vector<FloatRect>&) const;
276 void addLinkHighlight(LinkHighlightClient*);
277 void removeLinkHighlight(LinkHighlightClient*);
279 unsigned numLinkHighlights() { return m_linkHighlights.size(); }
280 LinkHighlightClient* linkHighlight(int i) { return m_linkHighlights[i]; }
282 void setScrollableArea(ScrollableArea*, bool isMainFrame);
283 ScrollableArea* scrollableArea() const { return m_scrollableArea; }
285 blink::WebContentLayer* contentLayer() const { return m_layer.get(); }
287 static void registerContentsLayer(blink::WebLayer*);
288 static void unregisterContentsLayer(blink::WebLayer*);
290 // GraphicsContextPainter implementation.
291 virtual void paint(GraphicsContext&, const IntRect& clip) OVERRIDE;
293 // WebAnimationDelegate implementation.
294 virtual void notifyAnimationStarted(double monotonicTime, blink::WebAnimation::TargetProperty) OVERRIDE;
295 virtual void notifyAnimationFinished(double monotonicTime, blink::WebAnimation::TargetProperty) OVERRIDE;
297 // WebLayerScrollClient implementation.
298 virtual void didScroll() OVERRIDE;
301 explicit GraphicsLayer(GraphicsLayerClient*);
302 // GraphicsLayerFactoryChromium that wants to create a GraphicsLayer need to be friends.
303 friend class blink::GraphicsLayerFactoryChromium;
305 // Exposed for tests.
306 virtual blink::WebLayer* contentsLayer() const { return m_contentsLayer; }
309 // Adds a child without calling updateChildList(), so that adding children
310 // can be batched before updating.
311 void addChildInternal(GraphicsLayer*);
313 // This method is used by platform GraphicsLayer classes to clear the filters
314 // when compositing is not done in hardware. It is not virtual, so the caller
315 // needs to notifiy the change to the platform layer as needed.
316 void clearFilters() { m_filters.clear(); }
318 void setReplicatedLayer(GraphicsLayer* layer) { m_replicatedLayer = layer; }
320 int incrementPaintCount() { return ++m_paintCount; }
322 void dumpProperties(TextStream&, int indent, LayerTreeFlags, RenderingContextMap&) const;
324 // Helper functions used by settors to keep layer's the state consistent.
325 void updateChildList();
326 void updateLayerIsDrawable();
327 void updateContentsRect();
329 void setContentsTo(blink::WebLayer*);
330 void setupContentsLayer(blink::WebLayer*);
331 void clearContentsLayerIfUnregistered();
332 blink::WebLayer* contentsLayerIfRegistered();
334 GraphicsLayerClient* m_client;
336 // Offset from the owning renderer
337 IntSize m_offsetFromRenderer;
339 // Position is relative to the parent GraphicsLayer
340 FloatPoint m_position;
341 FloatPoint3D m_anchorPoint;
343 FloatPoint m_boundsOrigin;
345 TransformationMatrix m_transform;
347 Color m_backgroundColor;
351 blink::WebBlendMode m_blendMode;
353 FilterOperations m_filters;
355 bool m_contentsOpaque : 1;
356 bool m_shouldFlattenTransform: 1;
357 bool m_backfaceVisibility : 1;
358 bool m_masksToBounds : 1;
359 bool m_drawsContent : 1;
360 bool m_contentsVisible : 1;
361 bool m_isRootForIsolatedGroup : 1;
363 bool m_hasScrollParent : 1;
364 bool m_hasClipParent : 1;
366 GraphicsLayerPaintingPhase m_paintingPhase;
367 CompositingCoordinatesOrientation m_contentsOrientation; // affects orientation of layer contents
369 Vector<GraphicsLayer*> m_children;
370 GraphicsLayer* m_parent;
372 GraphicsLayer* m_maskLayer; // Reference to mask layer. We don't own this.
373 GraphicsLayer* m_contentsClippingMaskLayer; // Reference to clipping mask layer. We don't own this.
375 GraphicsLayer* m_replicaLayer; // A layer that replicates this layer. We only allow one, for now.
376 // The replica is not parented; this is the primary reference to it.
377 GraphicsLayer* m_replicatedLayer; // For a replica layer, a reference to the original layer.
378 FloatPoint m_replicatedLayerPosition; // For a replica layer, the position of the replica.
380 IntRect m_contentsRect;
384 OwnPtr<blink::WebContentLayer> m_layer;
385 OwnPtr<blink::WebImageLayer> m_imageLayer;
386 OwnPtr<blink::WebNinePatchLayer> m_ninePatchLayer;
387 blink::WebLayer* m_contentsLayer;
388 // We don't have ownership of m_contentsLayer, but we do want to know if a given layer is the
389 // same as our current layer in setContentsTo(). Since m_contentsLayer may be deleted at this point,
390 // we stash an ID away when we know m_contentsLayer is alive and use that for comparisons from that point
392 int m_contentsLayerId;
394 Vector<LinkHighlightClient*> m_linkHighlights;
396 OwnPtr<OpaqueRectTrackingContentLayerDelegate> m_opaqueRectTrackingContentLayerDelegate;
398 ScrollableArea* m_scrollableArea;
399 GraphicsLayerDebugInfo m_debugInfo;
400 int m_3dRenderingContext;
403 } // namespace WebCore
406 // Outside the WebCore namespace for ease of invocation from gdb.
407 void PLATFORM_EXPORT showGraphicsLayerTree(const WebCore::GraphicsLayer*);
410 #endif // GraphicsLayer_h