1 // Copyright 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 #ifndef LayerChromium_h
7 #define LayerChromium_h
9 #if USE(ACCELERATED_COMPOSITING)
11 #include "base/memory/ref_counted.h"
12 #include "CCLayerAnimationController.h"
13 #include "CCOcclusionTracker.h"
14 #include "CCPrioritizedTexture.h"
15 #include "FloatPoint.h"
17 #include "RenderSurfaceChromium.h"
19 #include <public/WebFilterOperations.h>
20 #include <public/WebTransformationMatrix.h>
23 #include <wtf/OwnPtr.h>
24 #include <wtf/PassOwnPtr.h>
27 class WebAnimationDelegate
;
28 class WebLayerScrollClient
;
33 class CCActiveAnimation
;
34 struct CCAnimationEvent
;
35 class CCLayerAnimationDelegate
;
37 class CCLayerTreeHost
;
38 class CCTextureUpdateQueue
;
39 class ScrollbarLayerChromium
;
40 struct CCAnimationEvent
;
41 struct CCRenderingStats
;
43 // Base class for composited layers. Special layer types are derived from
45 class LayerChromium
: public base::RefCounted
<LayerChromium
>, public CCLayerAnimationControllerClient
{
47 typedef std::vector
<scoped_refptr
<LayerChromium
> > LayerList
;
49 static scoped_refptr
<LayerChromium
> create();
51 // CCLayerAnimationControllerClient implementation
52 virtual int id() const OVERRIDE
;
53 virtual void setOpacityFromAnimation(float) OVERRIDE
;
54 virtual float opacity() const OVERRIDE
;
55 virtual void setTransformFromAnimation(const WebKit::WebTransformationMatrix
&) OVERRIDE
;
56 // A layer's transform operates layer space. That is, entirely in logical,
57 // non-page-scaled pixels (that is, they have page zoom baked in, but not page scale).
58 // The root layer is a special case -- it operates in physical pixels.
59 virtual const WebKit::WebTransformationMatrix
& transform() const OVERRIDE
;
61 LayerChromium
* rootLayer();
62 LayerChromium
* parent() const;
63 void addChild(scoped_refptr
<LayerChromium
>);
64 void insertChild(scoped_refptr
<LayerChromium
>, size_t index
);
65 void replaceChild(LayerChromium
* reference
, scoped_refptr
<LayerChromium
> newLayer
);
66 void removeFromParent();
67 void removeAllChildren();
68 void setChildren(const LayerList
&);
70 const LayerList
& children() const { return m_children
; }
72 void setAnchorPoint(const FloatPoint
&);
73 FloatPoint
anchorPoint() const { return m_anchorPoint
; }
75 void setAnchorPointZ(float);
76 float anchorPointZ() const { return m_anchorPointZ
; }
78 void setBackgroundColor(SkColor
);
79 SkColor
backgroundColor() const { return m_backgroundColor
; }
81 // A layer's bounds are in logical, non-page-scaled pixels (however, the
82 // root layer's bounds are in physical pixels).
83 void setBounds(const IntSize
&);
84 const IntSize
& bounds() const { return m_bounds
; }
85 virtual IntSize
contentBounds() const;
87 void setMasksToBounds(bool);
88 bool masksToBounds() const { return m_masksToBounds
; }
90 void setMaskLayer(LayerChromium
*);
91 LayerChromium
* maskLayer() const { return m_maskLayer
.get(); }
93 virtual void setNeedsDisplayRect(const FloatRect
& dirtyRect
);
94 void setNeedsDisplay() { setNeedsDisplayRect(FloatRect(FloatPoint(), bounds())); }
95 virtual bool needsDisplay() const;
97 void setOpacity(float);
98 bool opacityIsAnimating() const;
100 void setFilters(const WebKit::WebFilterOperations
&);
101 const WebKit::WebFilterOperations
& filters() const { return m_filters
; }
103 // Background filters are filters applied to what is behind this layer, when they are viewed through non-opaque
104 // regions in this layer. They are used through the WebLayer interface, and are not exposed to HTML.
105 void setBackgroundFilters(const WebKit::WebFilterOperations
&);
106 const WebKit::WebFilterOperations
& backgroundFilters() const { return m_backgroundFilters
; }
108 virtual void setContentsOpaque(bool);
109 bool contentsOpaque() const { return m_contentsOpaque
; }
111 void setPosition(const FloatPoint
&);
112 FloatPoint
position() const { return m_position
; }
114 void setIsContainerForFixedPositionLayers(bool);
115 bool isContainerForFixedPositionLayers() const { return m_isContainerForFixedPositionLayers
; }
117 void setFixedToContainerLayer(bool);
118 bool fixedToContainerLayer() const { return m_fixedToContainerLayer
; }
120 void setSublayerTransform(const WebKit::WebTransformationMatrix
&);
121 const WebKit::WebTransformationMatrix
& sublayerTransform() const { return m_sublayerTransform
; }
123 void setTransform(const WebKit::WebTransformationMatrix
&);
124 bool transformIsAnimating() const;
126 const IntRect
& visibleContentRect() const { return m_visibleContentRect
; }
127 void setVisibleContentRect(const IntRect
& visibleContentRect
) { m_visibleContentRect
= visibleContentRect
; }
129 void setScrollPosition(const IntPoint
&);
130 const IntPoint
& scrollPosition() const { return m_scrollPosition
; }
132 void setMaxScrollPosition(const IntSize
&);
133 const IntSize
& maxScrollPosition() const { return m_maxScrollPosition
; }
135 void setScrollable(bool);
136 bool scrollable() const { return m_scrollable
; }
138 void setShouldScrollOnMainThread(bool);
139 bool shouldScrollOnMainThread() const { return m_shouldScrollOnMainThread
; }
141 void setHaveWheelEventHandlers(bool);
142 bool haveWheelEventHandlers() const { return m_haveWheelEventHandlers
; }
144 void setNonFastScrollableRegion(const Region
&);
145 void setNonFastScrollableRegionChanged() { m_nonFastScrollableRegionChanged
= true; }
146 const Region
& nonFastScrollableRegion() const { return m_nonFastScrollableRegion
; }
148 void setLayerScrollClient(WebKit::WebLayerScrollClient
* layerScrollClient
) { m_layerScrollClient
= layerScrollClient
; }
150 void setDrawCheckerboardForMissingTiles(bool);
151 bool drawCheckerboardForMissingTiles() const { return m_drawCheckerboardForMissingTiles
; }
153 bool forceRenderSurface() const { return m_forceRenderSurface
; }
154 void setForceRenderSurface(bool);
156 IntSize
scrollDelta() const { return IntSize(); }
158 void setImplTransform(const WebKit::WebTransformationMatrix
&);
159 const WebKit::WebTransformationMatrix
& implTransform() const { return m_implTransform
; }
161 void setDoubleSided(bool);
162 bool doubleSided() const { return m_doubleSided
; }
164 void setPreserves3D(bool preserve3D
) { m_preserves3D
= preserve3D
; }
165 bool preserves3D() const { return m_preserves3D
; }
167 void setUseParentBackfaceVisibility(bool useParentBackfaceVisibility
) { m_useParentBackfaceVisibility
= useParentBackfaceVisibility
; }
168 bool useParentBackfaceVisibility() const { return m_useParentBackfaceVisibility
; }
170 virtual void setUseLCDText(bool);
171 bool useLCDText() const { return m_useLCDText
; }
173 virtual void setLayerTreeHost(CCLayerTreeHost
*);
175 bool hasContributingDelegatedRenderPasses() const { return false; }
177 void setIsDrawable(bool);
179 void setReplicaLayer(LayerChromium
*);
180 LayerChromium
* replicaLayer() const { return m_replicaLayer
.get(); }
182 bool hasMask() const { return m_maskLayer
; }
183 bool hasReplica() const { return m_replicaLayer
; }
184 bool replicaHasMask() const { return m_replicaLayer
&& (m_maskLayer
|| m_replicaLayer
->m_maskLayer
); }
186 // These methods typically need to be overwritten by derived classes.
187 virtual bool drawsContent() const;
188 virtual void update(CCTextureUpdateQueue
&, const CCOcclusionTracker
*, CCRenderingStats
&) { }
189 virtual bool needMoreUpdates();
190 virtual void setIsMask(bool) { }
191 virtual void bindContentsTexture() { }
192 virtual bool needsContentsScale() const;
194 void setDebugBorderColor(SkColor
);
195 void setDebugBorderWidth(float);
196 void setDebugName(const std::string
&);
198 virtual void pushPropertiesTo(CCLayerImpl
*);
200 void clearRenderSurface() { m_renderSurface
.clear(); }
201 RenderSurfaceChromium
* renderSurface() const { return m_renderSurface
.get(); }
202 void createRenderSurface();
204 float drawOpacity() const { return m_drawOpacity
; }
205 void setDrawOpacity(float opacity
) { m_drawOpacity
= opacity
; }
207 bool drawOpacityIsAnimating() const { return m_drawOpacityIsAnimating
; }
208 void setDrawOpacityIsAnimating(bool drawOpacityIsAnimating
) { m_drawOpacityIsAnimating
= drawOpacityIsAnimating
; }
210 LayerChromium
* renderTarget() const { ASSERT(!m_renderTarget
|| m_renderTarget
->renderSurface()); return m_renderTarget
; }
211 void setRenderTarget(LayerChromium
* target
) { m_renderTarget
= target
; }
213 bool drawTransformIsAnimating() const { return m_drawTransformIsAnimating
; }
214 void setDrawTransformIsAnimating(bool animating
) { m_drawTransformIsAnimating
= animating
; }
215 bool screenSpaceTransformIsAnimating() const { return m_screenSpaceTransformIsAnimating
; }
216 void setScreenSpaceTransformIsAnimating(bool animating
) { m_screenSpaceTransformIsAnimating
= animating
; }
218 // This moves from layer space, with origin in the center to target space with origin in the top left.
219 // That is, it converts from logical, non-page-scaled, to target pixels (and if the target is the
220 // root render surface, then this converts to physical pixels).
221 const WebKit::WebTransformationMatrix
& drawTransform() const { return m_drawTransform
; }
222 void setDrawTransform(const WebKit::WebTransformationMatrix
& matrix
) { m_drawTransform
= matrix
; }
223 // This moves from content space, with origin the top left to screen space with origin in the top left.
224 // It converts logical, non-page-scaled pixels to physical pixels.
225 const WebKit::WebTransformationMatrix
& screenSpaceTransform() const { return m_screenSpaceTransform
; }
226 void setScreenSpaceTransform(const WebKit::WebTransformationMatrix
& matrix
) { m_screenSpaceTransform
= matrix
; }
227 const IntRect
& drawableContentRect() const { return m_drawableContentRect
; }
228 void setDrawableContentRect(const IntRect
& rect
) { m_drawableContentRect
= rect
; }
230 // The contentsScale converts from logical, non-page-scaled pixels to target pixels.
231 // The contentsScale is 1 for the root layer as it is already in physical pixels.
232 float contentsScale() const { return m_contentsScale
; }
233 void setContentsScale(float);
234 // The scale from the initial CSS transform of the layer, including its ancestors, but
235 // not including the deviceScaleFactor or pageScaleFactor.
236 float initialCssScale() const { return m_initialCssScale
; }
237 void setInitialCssScale(float scale
) { m_initialCssScale
= scale
; }
239 // When true, the layer's contents are not scaled by the current page scale factor.
240 // setBoundsContainPageScale recursively sets the value on all child layers.
241 void setBoundsContainPageScale(bool);
242 bool boundsContainPageScale() const { return m_boundsContainPageScale
; }
244 // Returns true if any of the layer's descendants has content to draw.
245 bool descendantDrawsContent();
247 CCLayerTreeHost
* layerTreeHost() const { return m_layerTreeHost
; }
249 // Set the priority of all desired textures in this layer.
250 virtual void setTexturePriorities(const CCPriorityCalculator
&) { }
252 bool addAnimation(PassOwnPtr
<CCActiveAnimation
>);
253 void pauseAnimation(int animationId
, double timeOffset
);
254 void removeAnimation(int animationId
);
256 void suspendAnimations(double monotonicTime
);
257 void resumeAnimations(double monotonicTime
);
259 CCLayerAnimationController
* layerAnimationController() { return m_layerAnimationController
.get(); }
260 void setLayerAnimationController(PassOwnPtr
<CCLayerAnimationController
>);
261 PassOwnPtr
<CCLayerAnimationController
> releaseLayerAnimationController();
263 void setLayerAnimationDelegate(WebKit::WebAnimationDelegate
* layerAnimationDelegate
) { m_layerAnimationDelegate
= layerAnimationDelegate
; }
265 bool hasActiveAnimation() const;
267 virtual void notifyAnimationStarted(const CCAnimationEvent
&, double wallClockTime
);
268 virtual void notifyAnimationFinished(double wallClockTime
);
270 virtual Region
visibleContentOpaqueRegion() const;
272 virtual ScrollbarLayerChromium
* toScrollbarLayerChromium();
275 friend class CCLayerImpl
;
276 friend class TreeSynchronizer
;
277 virtual ~LayerChromium();
281 void setNeedsCommit();
283 IntRect
layerRectToContentRect(const WebKit::WebRect
& layerRect
);
285 // This flag is set when layer need repainting/updating.
288 // Tracks whether this layer may have changed stacking order with its siblings.
289 bool m_stackingOrderChanged
;
291 // The update rect is the region of the compositor resource that was actually updated by the compositor.
292 // For layers that may do updating outside the compositor's control (i.e. plugin layers), this information
293 // is not available and the update rect will remain empty.
294 // Note this rect is in layer space (not content space).
295 FloatRect m_updateRect
;
297 scoped_refptr
<LayerChromium
> m_maskLayer
;
299 // Constructs a CCLayerImpl of the correct runtime type for this LayerChromium type.
300 virtual PassOwnPtr
<CCLayerImpl
> createCCLayerImpl();
304 friend class base::RefCounted
<LayerChromium
>;
306 void setParent(LayerChromium
*);
307 bool hasAncestor(LayerChromium
*) const;
308 bool descendantIsFixedToContainerLayer() const;
310 size_t numChildren() const { return m_children
.size(); }
312 // Returns the index of the child or -1 if not found.
313 int indexOfChild(const LayerChromium
*);
315 // This should only be called from removeFromParent.
316 void removeChild(LayerChromium
*);
318 LayerList m_children
;
319 LayerChromium
* m_parent
;
321 // LayerChromium instances have a weak pointer to their CCLayerTreeHost.
322 // This pointer value is nil when a LayerChromium is not in a tree and is
323 // updated via setLayerTreeHost() if a layer moves between trees.
324 CCLayerTreeHost
* m_layerTreeHost
;
326 OwnPtr
<CCLayerAnimationController
> m_layerAnimationController
;
331 // Uses layer's content space.
332 IntRect m_visibleContentRect
;
334 IntPoint m_scrollPosition
;
335 IntSize m_maxScrollPosition
;
337 bool m_shouldScrollOnMainThread
;
338 bool m_haveWheelEventHandlers
;
339 Region m_nonFastScrollableRegion
;
340 bool m_nonFastScrollableRegionChanged
;
341 FloatPoint m_position
;
342 FloatPoint m_anchorPoint
;
343 SkColor m_backgroundColor
;
344 SkColor m_debugBorderColor
;
345 float m_debugBorderWidth
;
346 std::string m_debugName
;
348 WebKit::WebFilterOperations m_filters
;
349 WebKit::WebFilterOperations m_backgroundFilters
;
350 float m_anchorPointZ
;
351 bool m_isContainerForFixedPositionLayers
;
352 bool m_fixedToContainerLayer
;
354 bool m_masksToBounds
;
355 bool m_contentsOpaque
;
359 bool m_useParentBackfaceVisibility
;
360 bool m_drawCheckerboardForMissingTiles
;
361 bool m_forceRenderSurface
;
363 WebKit::WebTransformationMatrix m_transform
;
364 WebKit::WebTransformationMatrix m_sublayerTransform
;
366 // Replica layer used for reflections.
367 scoped_refptr
<LayerChromium
> m_replicaLayer
;
369 // Transient properties.
370 OwnPtr
<RenderSurfaceChromium
> m_renderSurface
;
372 bool m_drawOpacityIsAnimating
;
374 LayerChromium
* m_renderTarget
;
376 WebKit::WebTransformationMatrix m_drawTransform
;
377 WebKit::WebTransformationMatrix m_screenSpaceTransform
;
378 bool m_drawTransformIsAnimating
;
379 bool m_screenSpaceTransformIsAnimating
;
381 // Uses target surface space.
382 IntRect m_drawableContentRect
;
383 float m_contentsScale
;
384 float m_initialCssScale
;
385 bool m_boundsContainPageScale
;
387 WebKit::WebTransformationMatrix m_implTransform
;
389 WebKit::WebAnimationDelegate
* m_layerAnimationDelegate
;
390 WebKit::WebLayerScrollClient
* m_layerScrollClient
;
393 void sortLayers(std::vector
<scoped_refptr
<LayerChromium
> >::iterator
, std::vector
<scoped_refptr
<LayerChromium
> >::iterator
, void*);
396 #endif // USE(ACCELERATED_COMPOSITING)