Home | History | Annotate | Download | only in graphics
      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/OpaqueRectTrackingContentLayerDelegate.h"
     38 #include "platform/graphics/filters/FilterOperations.h"
     39 #include "platform/transforms/TransformationMatrix.h"
     40 #include "public/platform/WebAnimationDelegate.h"
     41 #include "public/platform/WebCompositingReasons.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 WebGraphicsLayerDebugInfo;
     56 class WebLayer;
     57 }
     58 
     59 namespace WebCore {
     60 
     61 class FloatRect;
     62 class GraphicsContext;
     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 // GraphicsLayer is an abstraction for a rendering surface with backing store,
     80 // which may have associated transformation and animations.
     81 
     82 class PLATFORM_EXPORT GraphicsLayer : public GraphicsContextPainter, public blink::WebAnimationDelegate, public blink::WebLayerScrollClient, public blink::WebLayerClient {
     83     WTF_MAKE_NONCOPYABLE(GraphicsLayer); WTF_MAKE_FAST_ALLOCATED;
     84 public:
     85     static PassOwnPtr<GraphicsLayer> create(GraphicsLayerFactory*, GraphicsLayerClient*);
     86 
     87     virtual ~GraphicsLayer();
     88 
     89     GraphicsLayerClient* client() const { return m_client; }
     90 
     91     // blink::WebLayerClient implementation.
     92     virtual blink::WebString debugName(blink::WebLayer*) OVERRIDE;
     93     virtual blink::WebGraphicsLayerDebugInfo* takeDebugInfo() OVERRIDE;
     94 
     95     void setCompositingReasons(blink::WebCompositingReasons);
     96     blink::WebCompositingReasons compositingReasons() const { return m_compositingReasons; }
     97 
     98     GraphicsLayer* parent() const { return m_parent; };
     99     void setParent(GraphicsLayer*); // Internal use only.
    100 
    101     // Returns true if the layer has the given layer as an ancestor (excluding self).
    102     bool hasAncestor(GraphicsLayer*) const;
    103 
    104     const Vector<GraphicsLayer*>& children() const { return m_children; }
    105     // Returns true if the child list changed.
    106     bool setChildren(const Vector<GraphicsLayer*>&);
    107 
    108     // Add child layers. If the child is already parented, it will be removed from its old parent.
    109     void addChild(GraphicsLayer*);
    110     void addChildAtIndex(GraphicsLayer*, int index);
    111     void addChildAbove(GraphicsLayer*, GraphicsLayer* sibling);
    112     void addChildBelow(GraphicsLayer*, GraphicsLayer* sibling);
    113     bool replaceChild(GraphicsLayer* oldChild, GraphicsLayer* newChild);
    114 
    115     void removeAllChildren();
    116     void removeFromParent();
    117 
    118     GraphicsLayer* maskLayer() const { return m_maskLayer; }
    119     void setMaskLayer(GraphicsLayer*);
    120 
    121     GraphicsLayer* contentsClippingMaskLayer() const { return m_contentsClippingMaskLayer; }
    122     void setContentsClippingMaskLayer(GraphicsLayer*);
    123 
    124     // The given layer will replicate this layer and its children; the replica renders behind this layer.
    125     void setReplicatedByLayer(GraphicsLayer*);
    126     // Whether this layer is being replicated by another layer.
    127     bool isReplicated() const { return m_replicaLayer; }
    128     // The layer that replicates this layer (if any).
    129     GraphicsLayer* replicaLayer() const { return m_replicaLayer; }
    130     // The layer being replicated.
    131     GraphicsLayer* replicatedLayer() const { return m_replicatedLayer; }
    132 
    133     const FloatPoint& replicatedLayerPosition() const { return m_replicatedLayerPosition; }
    134     void setReplicatedLayerPosition(const FloatPoint& p) { m_replicatedLayerPosition = p; }
    135 
    136     enum ShouldSetNeedsDisplay {
    137         DontSetNeedsDisplay,
    138         SetNeedsDisplay
    139     };
    140 
    141     // Offset is origin of the renderer minus origin of the graphics layer (so either zero or negative).
    142     IntSize offsetFromRenderer() const { return m_offsetFromRenderer; }
    143     void setOffsetFromRenderer(const IntSize&, ShouldSetNeedsDisplay = SetNeedsDisplay);
    144 
    145     // The position of the layer (the location of its top-left corner in its parent)
    146     const FloatPoint& position() const { return m_position; }
    147     void setPosition(const FloatPoint&);
    148 
    149     // Anchor point: (0, 0) is top left, (1, 1) is bottom right. The anchor point
    150     // affects the origin of the transforms.
    151     const FloatPoint3D& anchorPoint() const { return m_anchorPoint; }
    152     void setAnchorPoint(const FloatPoint3D&);
    153 
    154     // The size of the layer.
    155     const FloatSize& size() const { return m_size; }
    156     void setSize(const FloatSize&);
    157 
    158     // The boundOrigin affects the offset at which content is rendered, and sublayers are positioned.
    159     const FloatPoint& boundsOrigin() const { return m_boundsOrigin; }
    160     void setBoundsOrigin(const FloatPoint& origin) { m_boundsOrigin = origin; }
    161 
    162     const TransformationMatrix& transform() const { return m_transform; }
    163     void setTransform(const TransformationMatrix&);
    164 
    165     const TransformationMatrix& childrenTransform() const { return m_childrenTransform; }
    166     void setChildrenTransform(const TransformationMatrix&);
    167 
    168     bool preserves3D() const { return m_preserves3D; }
    169     void setPreserves3D(bool);
    170 
    171     bool masksToBounds() const { return m_masksToBounds; }
    172     void setMasksToBounds(bool);
    173 
    174     bool drawsContent() const { return m_drawsContent; }
    175     void setDrawsContent(bool);
    176 
    177     bool contentsAreVisible() const { return m_contentsVisible; }
    178     void setContentsVisible(bool);
    179 
    180     void setScrollParent(blink::WebLayer*);
    181     void setClipParent(blink::WebLayer*);
    182 
    183     void setDebugInfo(blink::WebGraphicsLayerDebugInfo*);
    184 
    185     // For special cases, e.g. drawing missing tiles on Android.
    186     // The compositor should never paint this color in normal cases because the RenderLayer
    187     // will paint background by itself.
    188     const Color& backgroundColor() const { return m_backgroundColor; }
    189     void setBackgroundColor(const Color&);
    190 
    191     // opaque means that we know the layer contents have no alpha
    192     bool contentsOpaque() const { return m_contentsOpaque; }
    193     void setContentsOpaque(bool);
    194 
    195     bool backfaceVisibility() const { return m_backfaceVisibility; }
    196     void setBackfaceVisibility(bool visible);
    197 
    198     float opacity() const { return m_opacity; }
    199     void setOpacity(float);
    200 
    201     blink::WebBlendMode blendMode() const { return m_blendMode; }
    202     void setBlendMode(blink::WebBlendMode);
    203 
    204     bool isRootForIsolatedGroup() const { return m_isRootForIsolatedGroup; }
    205     void setIsRootForIsolatedGroup(bool);
    206 
    207     const FilterOperations& filters() const { return m_filters; }
    208 
    209     // Returns true if filter can be rendered by the compositor
    210     bool setFilters(const FilterOperations&);
    211     void setBackgroundFilters(const FilterOperations&);
    212 
    213     // Some GraphicsLayers paint only the foreground or the background content
    214     GraphicsLayerPaintingPhase paintingPhase() const { return m_paintingPhase; }
    215     void setPaintingPhase(GraphicsLayerPaintingPhase phase) { m_paintingPhase = phase; }
    216 
    217     void setNeedsDisplay();
    218     // mark the given rect (in layer coords) as needing dispay. Never goes deep.
    219     void setNeedsDisplayInRect(const FloatRect&);
    220 
    221     void setContentsNeedsDisplay();
    222 
    223     // Set that the position/size of the contents (image or video).
    224     IntRect contentsRect() const { return m_contentsRect; }
    225     void setContentsRect(const IntRect&);
    226 
    227     // Return true if the animation is handled by the compositing system. If this returns
    228     // false, the animation will be run by AnimationController.
    229     // These methods handle both transitions and keyframe animations.
    230     bool addAnimation(PassOwnPtr<blink::WebAnimation>);
    231     void pauseAnimation(int animationId, double /*timeOffset*/);
    232     void removeAnimation(int animationId);
    233 
    234     // Layer contents
    235     void setContentsToImage(Image*);
    236     void setContentsToNinePatch(Image*, const IntRect& aperture);
    237     // Pass an invalid color to remove the contents layer.
    238     void setContentsToSolidColor(const Color&);
    239     void setContentsToPlatformLayer(blink::WebLayer* layer) { setContentsTo(layer); }
    240     bool hasContentsLayer() const { return m_contentsLayer; }
    241 
    242     // Callback from the underlying graphics system to draw layer contents.
    243     void paintGraphicsLayerContents(GraphicsContext&, const IntRect& clip);
    244 
    245     // For hosting this GraphicsLayer in a native layer hierarchy.
    246     blink::WebLayer* platformLayer() const;
    247 
    248     enum CompositingCoordinatesOrientation { CompositingCoordinatesTopDown, CompositingCoordinatesBottomUp };
    249 
    250     // Flippedness of the contents of this layer. Does not affect sublayer geometry.
    251     void setContentsOrientation(CompositingCoordinatesOrientation orientation) { m_contentsOrientation = orientation; }
    252     CompositingCoordinatesOrientation contentsOrientation() const { return m_contentsOrientation; }
    253 
    254     void dumpLayer(TextStream&, int indent, LayerTreeFlags) 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     void distributeOpacity(float);
    263     float accumulatedOpacity() const;
    264 
    265     // If the exposed rect of this layer changes, returns true if this or descendant layers need a flush,
    266     // for example to allocate new tiles.
    267     bool visibleRectChangeRequiresFlush(const FloatRect& /* clipRect */) const { return false; }
    268 
    269     // Return a string with a human readable form of the layer tree, If debug is true
    270     // pointers for the layers and timing data will be included in the returned string.
    271     String layerTreeAsText(LayerTreeFlags = LayerTreeNormal) const;
    272 
    273     // Return an estimate of the backing store memory cost (in bytes). May be incorrect for tiled layers.
    274     double backingStoreMemoryEstimate() const;
    275 
    276     void resetTrackedRepaints();
    277     void addRepaintRect(const FloatRect&);
    278 
    279     void collectTrackedRepaintRects(Vector<FloatRect>&) const;
    280 
    281     void addLinkHighlight(LinkHighlightClient*);
    282     void removeLinkHighlight(LinkHighlightClient*);
    283     // Exposed for tests
    284     unsigned numLinkHighlights() { return m_linkHighlights.size(); }
    285     LinkHighlightClient* linkHighlight(int i) { return m_linkHighlights[i]; }
    286 
    287     void setScrollableArea(ScrollableArea*, bool isMainFrame);
    288     ScrollableArea* scrollableArea() const { return m_scrollableArea; }
    289 
    290     blink::WebContentLayer* contentLayer() const { return m_layer.get(); }
    291 
    292     static void registerContentsLayer(blink::WebLayer*);
    293     static void unregisterContentsLayer(blink::WebLayer*);
    294 
    295     // GraphicsContextPainter implementation.
    296     virtual void paint(GraphicsContext&, const IntRect& clip) OVERRIDE;
    297 
    298     // WebAnimationDelegate implementation.
    299     virtual void notifyAnimationStarted(double wallClockTime, double monotonicTime, blink::WebAnimation::TargetProperty) OVERRIDE;
    300     virtual void notifyAnimationFinished(double wallClockTime, double monotonicTime, blink::WebAnimation::TargetProperty) OVERRIDE;
    301 
    302     // WebLayerScrollClient implementation.
    303     virtual void didScroll() OVERRIDE;
    304 
    305 protected:
    306     explicit GraphicsLayer(GraphicsLayerClient*);
    307     // GraphicsLayerFactoryChromium that wants to create a GraphicsLayer need to be friends.
    308     friend class blink::GraphicsLayerFactoryChromium;
    309 
    310     // Exposed for tests.
    311     virtual blink::WebLayer* contentsLayer() const { return m_contentsLayer; }
    312 
    313 private:
    314     // Adds a child without calling updateChildList(), so that adding children
    315     // can be batched before updating.
    316     void addChildInternal(GraphicsLayer*);
    317 
    318     // This method is used by platform GraphicsLayer classes to clear the filters
    319     // when compositing is not done in hardware. It is not virtual, so the caller
    320     // needs to notifiy the change to the platform layer as needed.
    321     void clearFilters() { m_filters.clear(); }
    322 
    323     void setReplicatedLayer(GraphicsLayer* layer) { m_replicatedLayer = layer; }
    324 
    325     int incrementPaintCount() { return ++m_paintCount; }
    326 
    327     void dumpProperties(TextStream&, int indent, LayerTreeFlags) const;
    328 
    329     // Helper functions used by settors to keep layer's the state consistent.
    330     void updateChildList();
    331     void updateLayerIsDrawable();
    332     void updateContentsRect();
    333 
    334     void setContentsTo(blink::WebLayer*);
    335     void setupContentsLayer(blink::WebLayer*);
    336     void clearContentsLayerIfUnregistered();
    337     blink::WebLayer* contentsLayerIfRegistered();
    338 
    339     GraphicsLayerClient* m_client;
    340 
    341     // Offset from the owning renderer
    342     IntSize m_offsetFromRenderer;
    343 
    344     // Position is relative to the parent GraphicsLayer
    345     FloatPoint m_position;
    346     FloatPoint3D m_anchorPoint;
    347     FloatSize m_size;
    348     FloatPoint m_boundsOrigin;
    349 
    350     TransformationMatrix m_transform;
    351     TransformationMatrix m_childrenTransform;
    352 
    353     Color m_backgroundColor;
    354     float m_opacity;
    355     float m_zPosition;
    356 
    357     blink::WebBlendMode m_blendMode;
    358 
    359     FilterOperations m_filters;
    360 
    361     bool m_contentsOpaque : 1;
    362     bool m_preserves3D: 1;
    363     bool m_backfaceVisibility : 1;
    364     bool m_masksToBounds : 1;
    365     bool m_drawsContent : 1;
    366     bool m_contentsVisible : 1;
    367     bool m_isRootForIsolatedGroup : 1;
    368 
    369     bool m_hasScrollParent : 1;
    370     bool m_hasClipParent : 1;
    371 
    372     GraphicsLayerPaintingPhase m_paintingPhase;
    373     CompositingCoordinatesOrientation m_contentsOrientation; // affects orientation of layer contents
    374 
    375     Vector<GraphicsLayer*> m_children;
    376     GraphicsLayer* m_parent;
    377 
    378     GraphicsLayer* m_maskLayer; // Reference to mask layer. We don't own this.
    379     GraphicsLayer* m_contentsClippingMaskLayer; // Reference to clipping mask layer. We don't own this.
    380 
    381     GraphicsLayer* m_replicaLayer; // A layer that replicates this layer. We only allow one, for now.
    382                                    // The replica is not parented; this is the primary reference to it.
    383     GraphicsLayer* m_replicatedLayer; // For a replica layer, a reference to the original layer.
    384     FloatPoint m_replicatedLayerPosition; // For a replica layer, the position of the replica.
    385 
    386     IntRect m_contentsRect;
    387 
    388     int m_paintCount;
    389 
    390     OwnPtr<blink::WebContentLayer> m_layer;
    391     OwnPtr<blink::WebImageLayer> m_imageLayer;
    392     OwnPtr<blink::WebNinePatchLayer> m_ninePatchLayer;
    393     Color m_contentsSolidColor;
    394     OwnPtr<blink::WebSolidColorLayer> m_solidColorLayer;
    395     blink::WebLayer* m_contentsLayer;
    396     // We don't have ownership of m_contentsLayer, but we do want to know if a given layer is the
    397     // same as our current layer in setContentsTo(). Since m_contentsLayer may be deleted at this point,
    398     // we stash an ID away when we know m_contentsLayer is alive and use that for comparisons from that point
    399     // on.
    400     int m_contentsLayerId;
    401 
    402     Vector<LinkHighlightClient*> m_linkHighlights;
    403 
    404     OwnPtr<OpaqueRectTrackingContentLayerDelegate> m_opaqueRectTrackingContentLayerDelegate;
    405 
    406     ScrollableArea* m_scrollableArea;
    407     blink::WebCompositingReasons m_compositingReasons;
    408     blink::WebGraphicsLayerDebugInfo* m_debugInfo;
    409 };
    410 
    411 
    412 } // namespace WebCore
    413 
    414 #ifndef NDEBUG
    415 // Outside the WebCore namespace for ease of invocation from gdb.
    416 void showGraphicsLayerTree(const WebCore::GraphicsLayer* layer);
    417 #endif
    418 
    419 #endif // GraphicsLayer_h
    420