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