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