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