Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / graphics / GraphicsLayer.h
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
101     GraphicsLayer* parent() const { return m_parent; };
102     void setParent(GraphicsLayer*); // Internal use only.
103
104     // Returns true if the layer has the given layer as an ancestor (excluding self).
105     bool hasAncestor(GraphicsLayer*) const;
106
107     const Vector<GraphicsLayer*>& children() const { return m_children; }
108     // Returns true if the child list changed.
109     bool setChildren(const GraphicsLayerVector&);
110
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);
117
118     void removeAllChildren();
119     void removeFromParent();
120
121     GraphicsLayer* maskLayer() const { return m_maskLayer; }
122     void setMaskLayer(GraphicsLayer*);
123
124     GraphicsLayer* contentsClippingMaskLayer() const { return m_contentsClippingMaskLayer; }
125     void setContentsClippingMaskLayer(GraphicsLayer*);
126
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; }
135
136     const FloatPoint& replicatedLayerPosition() const { return m_replicatedLayerPosition; }
137     void setReplicatedLayerPosition(const FloatPoint& p) { m_replicatedLayerPosition = p; }
138
139     enum ShouldSetNeedsDisplay {
140         DontSetNeedsDisplay,
141         SetNeedsDisplay
142     };
143
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);
147
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&);
151
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&);
156
157     // The size of the layer.
158     const FloatSize& size() const { return m_size; }
159     void setSize(const FloatSize&);
160
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; }
164
165     const TransformationMatrix& transform() const { return m_transform; }
166     void setTransform(const TransformationMatrix&);
167
168     bool shouldFlattenTransform() const { return m_shouldFlattenTransform; }
169     void setShouldFlattenTransform(bool);
170
171     int renderingContext() const { return m_3dRenderingContext; }
172     void setRenderingContext(int id);
173
174     bool masksToBounds() const { return m_masksToBounds; }
175     void setMasksToBounds(bool);
176
177     bool drawsContent() const { return m_drawsContent; }
178     void setDrawsContent(bool);
179
180     bool contentsAreVisible() const { return m_contentsVisible; }
181     void setContentsVisible(bool);
182
183     void setScrollParent(blink::WebLayer*);
184     void setClipParent(blink::WebLayer*);
185
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&);
191
192     // opaque means that we know the layer contents have no alpha
193     bool contentsOpaque() const { return m_contentsOpaque; }
194     void setContentsOpaque(bool);
195
196     bool backfaceVisibility() const { return m_backfaceVisibility; }
197     void setBackfaceVisibility(bool visible);
198
199     float opacity() const { return m_opacity; }
200     void setOpacity(float);
201
202     blink::WebBlendMode blendMode() const { return m_blendMode; }
203     void setBlendMode(blink::WebBlendMode);
204
205     bool isRootForIsolatedGroup() const { return m_isRootForIsolatedGroup; }
206     void setIsRootForIsolatedGroup(bool);
207
208     const FilterOperations& filters() const { return m_filters; }
209
210     // Returns true if filter can be rendered by the compositor
211     bool setFilters(const FilterOperations&);
212     void setBackgroundFilters(const FilterOperations&);
213
214     // Some GraphicsLayers paint only the foreground or the background content
215     GraphicsLayerPaintingPhase paintingPhase() const { return m_paintingPhase; }
216     void setPaintingPhase(GraphicsLayerPaintingPhase);
217
218     void setNeedsDisplay();
219     // mark the given rect (in layer coords) as needing dispay. Never goes deep.
220     void setNeedsDisplayInRect(const FloatRect&);
221
222     void setContentsNeedsDisplay();
223
224     // Set that the position/size of the contents (image or video).
225     IntRect contentsRect() const { return m_contentsRect; }
226     void setContentsRect(const IntRect&);
227
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);
234
235     // Layer contents
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; }
240
241     // Callback from the underlying graphics system to draw layer contents.
242     void paintGraphicsLayerContents(GraphicsContext&, const IntRect& clip);
243
244     // For hosting this GraphicsLayer in a native layer hierarchy.
245     blink::WebLayer* platformLayer() const;
246
247     enum CompositingCoordinatesOrientation { CompositingCoordinatesTopDown, CompositingCoordinatesBottomUp };
248
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; }
252
253     typedef HashMap<int, int> RenderingContextMap;
254     void dumpLayer(TextStream&, int indent, LayerTreeFlags, RenderingContextMap&) const;
255
256     int paintCount() const { return m_paintCount; }
257
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);
261
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; }
265
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;
270
271     void resetTrackedRepaints();
272     void addRepaintRect(const FloatRect&);
273
274     void collectTrackedRepaintRects(Vector<FloatRect>&) const;
275
276     void addLinkHighlight(LinkHighlightClient*);
277     void removeLinkHighlight(LinkHighlightClient*);
278     // Exposed for tests
279     unsigned numLinkHighlights() { return m_linkHighlights.size(); }
280     LinkHighlightClient* linkHighlight(int i) { return m_linkHighlights[i]; }
281
282     void setScrollableArea(ScrollableArea*, bool isMainFrame);
283     ScrollableArea* scrollableArea() const { return m_scrollableArea; }
284
285     blink::WebContentLayer* contentLayer() const { return m_layer.get(); }
286
287     static void registerContentsLayer(blink::WebLayer*);
288     static void unregisterContentsLayer(blink::WebLayer*);
289
290     // GraphicsContextPainter implementation.
291     virtual void paint(GraphicsContext&, const IntRect& clip) OVERRIDE;
292
293     // WebAnimationDelegate implementation.
294     virtual void notifyAnimationStarted(double monotonicTime, blink::WebAnimation::TargetProperty) OVERRIDE;
295     virtual void notifyAnimationFinished(double monotonicTime, blink::WebAnimation::TargetProperty) OVERRIDE;
296
297     // WebLayerScrollClient implementation.
298     virtual void didScroll() OVERRIDE;
299
300 protected:
301     explicit GraphicsLayer(GraphicsLayerClient*);
302     // GraphicsLayerFactoryChromium that wants to create a GraphicsLayer need to be friends.
303     friend class blink::GraphicsLayerFactoryChromium;
304
305     // Exposed for tests.
306     virtual blink::WebLayer* contentsLayer() const { return m_contentsLayer; }
307
308 private:
309     // Adds a child without calling updateChildList(), so that adding children
310     // can be batched before updating.
311     void addChildInternal(GraphicsLayer*);
312
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(); }
317
318     void setReplicatedLayer(GraphicsLayer* layer) { m_replicatedLayer = layer; }
319
320     int incrementPaintCount() { return ++m_paintCount; }
321
322     void dumpProperties(TextStream&, int indent, LayerTreeFlags, RenderingContextMap&) const;
323
324     // Helper functions used by settors to keep layer's the state consistent.
325     void updateChildList();
326     void updateLayerIsDrawable();
327     void updateContentsRect();
328
329     void setContentsTo(blink::WebLayer*);
330     void setupContentsLayer(blink::WebLayer*);
331     void clearContentsLayerIfUnregistered();
332     blink::WebLayer* contentsLayerIfRegistered();
333
334     GraphicsLayerClient* m_client;
335
336     // Offset from the owning renderer
337     IntSize m_offsetFromRenderer;
338
339     // Position is relative to the parent GraphicsLayer
340     FloatPoint m_position;
341     FloatPoint3D m_anchorPoint;
342     FloatSize m_size;
343     FloatPoint m_boundsOrigin;
344
345     TransformationMatrix m_transform;
346
347     Color m_backgroundColor;
348     float m_opacity;
349     float m_zPosition;
350
351     blink::WebBlendMode m_blendMode;
352
353     FilterOperations m_filters;
354
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;
362
363     bool m_hasScrollParent : 1;
364     bool m_hasClipParent : 1;
365
366     GraphicsLayerPaintingPhase m_paintingPhase;
367     CompositingCoordinatesOrientation m_contentsOrientation; // affects orientation of layer contents
368
369     Vector<GraphicsLayer*> m_children;
370     GraphicsLayer* m_parent;
371
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.
374
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.
379
380     IntRect m_contentsRect;
381
382     int m_paintCount;
383
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
391     // on.
392     int m_contentsLayerId;
393
394     Vector<LinkHighlightClient*> m_linkHighlights;
395
396     OwnPtr<OpaqueRectTrackingContentLayerDelegate> m_opaqueRectTrackingContentLayerDelegate;
397
398     ScrollableArea* m_scrollableArea;
399     GraphicsLayerDebugInfo m_debugInfo;
400     int m_3dRenderingContext;
401 };
402
403 } // namespace WebCore
404
405 #ifndef NDEBUG
406 // Outside the WebCore namespace for ease of invocation from gdb.
407 void PLATFORM_EXPORT showGraphicsLayerTree(const WebCore::GraphicsLayer*);
408 #endif
409
410 #endif // GraphicsLayer_h