GraphicsLayer.h revision 65f03d4f644ce73618e5f4f50dd694b26f55ae12
1/*
2 * Copyright (C) 2009 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 GraphicsLayer_h
27#define GraphicsLayer_h
28
29#if USE(ACCELERATED_COMPOSITING)
30
31#include "Animation.h"
32#include "Color.h"
33#include "FloatPoint.h"
34#include "FloatPoint3D.h"
35#include "FloatSize.h"
36#include "GraphicsLayerClient.h"
37#include "IntRect.h"
38#include "TransformationMatrix.h"
39#include "TransformOperations.h"
40#include <wtf/OwnPtr.h>
41#include <wtf/PassOwnPtr.h>
42
43#if USE(TEXTURE_MAPPER)
44#include "texmap/TextureMapperPlatformLayer.h"
45#endif
46
47#if PLATFORM(MAC)
48#ifdef __OBJC__
49@class WebLayer;
50@class CALayer;
51typedef CALayer PlatformLayer;
52#else
53typedef void* PlatformLayer;
54#endif
55#elif PLATFORM(WIN)
56typedef struct _CACFLayer PlatformLayer;
57#elif PLATFORM(QT)
58#if USE(TEXTURE_MAPPER)
59namespace WebCore {
60class TextureMapperPlatformLayer;
61typedef TextureMapperPlatformLayer PlatformLayer;
62};
63#else
64QT_BEGIN_NAMESPACE
65class QGraphicsObject;
66QT_END_NAMESPACE
67namespace WebCore {
68typedef QGraphicsObject PlatformLayer;
69}
70#endif
71#elif PLATFORM(CHROMIUM)
72namespace WebCore {
73class LayerChromium;
74typedef LayerChromium PlatformLayer;
75}
76#elif PLATFORM(ANDROID)
77namespace WebCore {
78class LayerAndroid;
79typedef LayerAndroid PlatformLayer;
80typedef void* NativeLayer;
81}
82#else
83typedef void* PlatformLayer;
84#endif
85
86enum LayerTreeAsTextBehaviorFlags {
87    LayerTreeAsTextBehaviorNormal = 0,
88    LayerTreeAsTextDebug = 1 << 0, // Dump extra debugging info like layer addresses.
89};
90typedef unsigned LayerTreeAsTextBehavior;
91
92namespace WebCore {
93
94class FloatPoint3D;
95class GraphicsContext;
96class Image;
97class TextStream;
98class TimingFunction;
99
100// Base class for animation values (also used for transitions). Here to
101// represent values for properties being animated via the GraphicsLayer,
102// without pulling in style-related data from outside of the platform directory.
103class AnimationValue : public Noncopyable {
104public:
105    AnimationValue(float keyTime, PassRefPtr<TimingFunction> timingFunction = 0)
106        : m_keyTime(keyTime)
107    {
108        if (timingFunction)
109            m_timingFunction = timingFunction;
110    }
111
112    virtual ~AnimationValue() { }
113
114    float keyTime() const { return m_keyTime; }
115    const TimingFunction* timingFunction() const { return m_timingFunction.get(); }
116
117private:
118    float m_keyTime;
119    RefPtr<TimingFunction> m_timingFunction;
120};
121
122// Used to store one float value of an animation.
123class FloatAnimationValue : public AnimationValue {
124public:
125    FloatAnimationValue(float keyTime, float value, PassRefPtr<TimingFunction> timingFunction = 0)
126        : AnimationValue(keyTime, timingFunction)
127        , m_value(value)
128    {
129    }
130
131    float value() const { return m_value; }
132
133private:
134    float m_value;
135};
136
137// Used to store one transform value in a keyframe list.
138class TransformAnimationValue : public AnimationValue {
139public:
140    TransformAnimationValue(float keyTime, const TransformOperations* value = 0, PassRefPtr<TimingFunction> timingFunction = 0)
141        : AnimationValue(keyTime, timingFunction)
142    {
143        if (value)
144            m_value = adoptPtr(new TransformOperations(*value));
145    }
146
147    const TransformOperations* value() const { return m_value.get(); }
148
149private:
150    OwnPtr<TransformOperations> m_value;
151};
152
153// Used to store a series of values in a keyframe list. Values will all be of the same type,
154// which can be inferred from the property.
155class KeyframeValueList : public Noncopyable {
156public:
157
158    KeyframeValueList(AnimatedPropertyID property)
159        : m_property(property)
160    {
161    }
162
163    ~KeyframeValueList()
164    {
165        deleteAllValues(m_values);
166    }
167
168    AnimatedPropertyID property() const { return m_property; }
169
170    size_t size() const { return m_values.size(); }
171    const AnimationValue* at(size_t i) const { return m_values.at(i); }
172
173    // Insert, sorted by keyTime. Takes ownership of the pointer.
174    void insert(const AnimationValue*);
175
176protected:
177    Vector<const AnimationValue*> m_values;
178    AnimatedPropertyID m_property;
179};
180
181
182
183// GraphicsLayer is an abstraction for a rendering surface with backing store,
184// which may have associated transformation and animations.
185
186class GraphicsLayer {
187public:
188
189    static PassOwnPtr<GraphicsLayer> create(GraphicsLayerClient*);
190
191    virtual ~GraphicsLayer();
192
193    GraphicsLayerClient* client() const { return m_client; }
194
195    // Layer name. Only used to identify layers in debug output
196    const String& name() const { return m_name; }
197    virtual void setName(const String& name) { m_name = name; }
198
199    GraphicsLayer* parent() const { return m_parent; };
200    void setParent(GraphicsLayer* layer) { m_parent = layer; } // Internal use only.
201
202    // Returns true if the layer has the given layer as an ancestor (excluding self).
203    bool hasAncestor(GraphicsLayer*) const;
204
205    const Vector<GraphicsLayer*>& children() const { return m_children; }
206    // Returns true if the child list changed.
207    virtual bool setChildren(const Vector<GraphicsLayer*>&);
208
209    // Add child layers. If the child is already parented, it will be removed from its old parent.
210    virtual void addChild(GraphicsLayer*);
211    virtual void addChildAtIndex(GraphicsLayer*, int index);
212    virtual void addChildAbove(GraphicsLayer* layer, GraphicsLayer* sibling);
213    virtual void addChildBelow(GraphicsLayer* layer, GraphicsLayer* sibling);
214    virtual bool replaceChild(GraphicsLayer* oldChild, GraphicsLayer* newChild);
215
216    void removeAllChildren();
217    virtual void removeFromParent();
218
219    GraphicsLayer* maskLayer() const { return m_maskLayer; }
220    virtual void setMaskLayer(GraphicsLayer* layer) { m_maskLayer = layer; }
221
222    // The given layer will replicate this layer and its children; the replica renders behind this layer.
223    virtual void setReplicatedByLayer(GraphicsLayer*);
224    // Whether this layer is being replicated by another layer.
225    bool isReplicated() const { return m_replicaLayer; }
226    // The layer that replicates this layer (if any).
227    GraphicsLayer* replicaLayer() const { return m_replicaLayer; }
228
229    const FloatPoint& replicatedLayerPosition() const { return m_replicatedLayerPosition; }
230    void setReplicatedLayerPosition(const FloatPoint& p) { m_replicatedLayerPosition = p; }
231
232    // Offset is origin of the renderer minus origin of the graphics layer (so either zero or negative).
233    IntSize offsetFromRenderer() const { return m_offsetFromRenderer; }
234    void setOffsetFromRenderer(const IntSize& offset) { m_offsetFromRenderer = offset; }
235
236    // The position of the layer (the location of its top-left corner in its parent)
237    const FloatPoint& position() const { return m_position; }
238    virtual void setPosition(const FloatPoint& p) { m_position = p; }
239
240    // Anchor point: (0, 0) is top left, (1, 1) is bottom right. The anchor point
241    // affects the origin of the transforms.
242    const FloatPoint3D& anchorPoint() const { return m_anchorPoint; }
243    virtual void setAnchorPoint(const FloatPoint3D& p) { m_anchorPoint = p; }
244
245    // The bounds of the layer
246    const FloatSize& size() const { return m_size; }
247    virtual void setSize(const FloatSize& size) { m_size = size; }
248
249    const TransformationMatrix& transform() const { return m_transform; }
250    virtual void setTransform(const TransformationMatrix& t) { m_transform = t; }
251
252    const TransformationMatrix& childrenTransform() const { return m_childrenTransform; }
253    virtual void setChildrenTransform(const TransformationMatrix& t) { m_childrenTransform = t; }
254
255    bool preserves3D() const { return m_preserves3D; }
256    virtual void setPreserves3D(bool b) { m_preserves3D = b; }
257
258    bool masksToBounds() const { return m_masksToBounds; }
259    virtual void setMasksToBounds(bool b) { m_masksToBounds = b; }
260
261    bool drawsContent() const { return m_drawsContent; }
262    virtual void setDrawsContent(bool b) { m_drawsContent = b; }
263
264    bool acceleratesDrawing() const { return m_acceleratesDrawing; }
265    virtual void setAcceleratesDrawing(bool b) { m_acceleratesDrawing = b; }
266
267    // The color used to paint the layer backgrounds
268    const Color& backgroundColor() const { return m_backgroundColor; }
269    virtual void setBackgroundColor(const Color&);
270    virtual void clearBackgroundColor();
271    bool backgroundColorSet() const { return m_backgroundColorSet; }
272
273    // opaque means that we know the layer contents have no alpha
274    bool contentsOpaque() const { return m_contentsOpaque; }
275    virtual void setContentsOpaque(bool b) { m_contentsOpaque = b; }
276
277    bool backfaceVisibility() const { return m_backfaceVisibility; }
278    virtual void setBackfaceVisibility(bool b) { m_backfaceVisibility = b; }
279
280    float opacity() const { return m_opacity; }
281    virtual void setOpacity(float opacity) { m_opacity = opacity; }
282
283    // Some GraphicsLayers paint only the foreground or the background content
284    GraphicsLayerPaintingPhase paintingPhase() const { return m_paintingPhase; }
285    void setPaintingPhase(GraphicsLayerPaintingPhase phase) { m_paintingPhase = phase; }
286
287    virtual void setNeedsDisplay() = 0;
288    // mark the given rect (in layer coords) as needing dispay. Never goes deep.
289    virtual void setNeedsDisplayInRect(const FloatRect&) = 0;
290
291    virtual void setContentsNeedsDisplay() { };
292
293    // Set that the position/size of the contents (image or video).
294    IntRect contentsRect() const { return m_contentsRect; }
295    virtual void setContentsRect(const IntRect& r) { m_contentsRect = r; }
296
297    // Transitions are identified by a special animation name that cannot clash with a keyframe identifier.
298    static String animationNameForTransition(AnimatedPropertyID);
299
300    // Return true if the animation is handled by the compositing system. If this returns
301    // false, the animation will be run by AnimationController.
302    // These methods handle both transitions and keyframe animations.
303    virtual bool addAnimation(const KeyframeValueList&, const IntSize& /*boxSize*/, const Animation*, const String& /*animationName*/, double /*timeOffset*/)  { return false; }
304    virtual void pauseAnimation(const String& /*animationName*/, double /*timeOffset*/) { }
305    virtual void removeAnimation(const String& /*animationName*/) { }
306
307    virtual void suspendAnimations(double time);
308    virtual void resumeAnimations();
309
310    // Layer contents
311    virtual void setContentsToImage(Image*) { }
312    virtual void setContentsToMedia(PlatformLayer*) { } // video or plug-in
313    virtual void setContentsBackgroundColor(const Color&) { }
314    virtual void setContentsToCanvas(PlatformLayer*) { }
315    virtual bool hasContentsLayer() const { return false; }
316
317    // Callback from the underlying graphics system to draw layer contents.
318    void paintGraphicsLayerContents(GraphicsContext&, const IntRect& clip);
319    // Callback from the underlying graphics system when the layer has been displayed
320    virtual void layerDidDisplay(PlatformLayer*) { }
321
322    // For hosting this GraphicsLayer in a native layer hierarchy.
323    virtual PlatformLayer* platformLayer() const { return 0; }
324
325    // Change the scale at which the contents are rendered. Note that contentsScale may not return
326    // the same value passed to setContentsScale(), because of clamping and hysteresis.
327    virtual float contentsScale() const { return 1; }
328    virtual void setContentsScale(float) { }
329
330    void dumpLayer(TextStream&, int indent = 0, LayerTreeAsTextBehavior = LayerTreeAsTextBehaviorNormal) const;
331
332    int repaintCount() const { return m_repaintCount; }
333    int incrementRepaintCount() { return ++m_repaintCount; }
334
335    enum CompositingCoordinatesOrientation { CompositingCoordinatesTopDown, CompositingCoordinatesBottomUp };
336
337    // Flippedness of the contents of this layer. Does not affect sublayer geometry.
338    virtual void setContentsOrientation(CompositingCoordinatesOrientation orientation) { m_contentsOrientation = orientation; }
339    CompositingCoordinatesOrientation contentsOrientation() const { return m_contentsOrientation; }
340
341    bool showDebugBorders() const { return m_client ? m_client->showDebugBorders() : false; }
342    bool showRepaintCounter() const { return m_client ? m_client->showRepaintCounter() : false; }
343
344    void updateDebugIndicators();
345
346    virtual void setDebugBackgroundColor(const Color&) { }
347    virtual void setDebugBorder(const Color&, float /*borderWidth*/) { }
348    // z-position is the z-equivalent of position(). It's only used for debugging purposes.
349    virtual float zPosition() const { return m_zPosition; }
350    virtual void setZPosition(float);
351
352    virtual void distributeOpacity(float);
353    virtual float accumulatedOpacity() const;
354
355    // Some compositing systems may do internal batching to synchronize compositing updates
356    // with updates drawn into the window. These methods flush internal batched state on this layer
357    // and descendant layers, and this layer only.
358    virtual void syncCompositingState() { }
359    virtual void syncCompositingStateForThisLayerOnly() { }
360
361    // Return a string with a human readable form of the layer tree, If debug is true
362    // pointers for the layers and timing data will be included in the returned string.
363    String layerTreeAsText(LayerTreeAsTextBehavior = LayerTreeAsTextBehaviorNormal) const;
364
365    bool usingTiledLayer() const { return m_usingTiledLayer; }
366
367protected:
368
369    typedef Vector<TransformOperation::OperationType> TransformOperationList;
370    // Given a list of TransformAnimationValues, return an array of transform operations.
371    // On return, if hasBigRotation is true, functions contain rotations of >= 180 degrees
372    static void fetchTransformOperationList(const KeyframeValueList&, TransformOperationList&, bool& isValid, bool& hasBigRotation);
373
374    virtual void setOpacityInternal(float) { }
375
376    // The layer being replicated.
377    GraphicsLayer* replicatedLayer() const { return m_replicatedLayer; }
378    virtual void setReplicatedLayer(GraphicsLayer* layer) { m_replicatedLayer = layer; }
379
380    GraphicsLayer(GraphicsLayerClient*);
381
382    void dumpProperties(TextStream&, int indent, LayerTreeAsTextBehavior) const;
383
384    GraphicsLayerClient* m_client;
385    String m_name;
386
387    // Offset from the owning renderer
388    IntSize m_offsetFromRenderer;
389
390    // Position is relative to the parent GraphicsLayer
391    FloatPoint m_position;
392    FloatPoint3D m_anchorPoint;
393    FloatSize m_size;
394    TransformationMatrix m_transform;
395    TransformationMatrix m_childrenTransform;
396
397    Color m_backgroundColor;
398    float m_opacity;
399    float m_zPosition;
400
401    bool m_backgroundColorSet : 1;
402    bool m_contentsOpaque : 1;
403    bool m_preserves3D: 1;
404    bool m_backfaceVisibility : 1;
405    bool m_usingTiledLayer : 1;
406    bool m_masksToBounds : 1;
407    bool m_drawsContent : 1;
408    bool m_acceleratesDrawing : 1;
409
410    GraphicsLayerPaintingPhase m_paintingPhase;
411    CompositingCoordinatesOrientation m_contentsOrientation; // affects orientation of layer contents
412
413    Vector<GraphicsLayer*> m_children;
414    GraphicsLayer* m_parent;
415
416    GraphicsLayer* m_maskLayer; // Reference to mask layer. We don't own this.
417
418    GraphicsLayer* m_replicaLayer; // A layer that replicates this layer. We only allow one, for now.
419                                   // The replica is not parented; this is the primary reference to it.
420    GraphicsLayer* m_replicatedLayer; // For a replica layer, a reference to the original layer.
421    FloatPoint m_replicatedLayerPosition; // For a replica layer, the position of the replica.
422
423    IntRect m_contentsRect;
424
425    int m_repaintCount;
426};
427
428
429} // namespace WebCore
430
431#ifndef NDEBUG
432// Outside the WebCore namespace for ease of invocation from gdb.
433void showGraphicsLayerTree(const WebCore::GraphicsLayer* layer);
434#endif
435
436#endif // USE(ACCELERATED_COMPOSITING)
437
438#endif // GraphicsLayer_h
439
440