1 // Copyright 2011 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.
5 #ifndef CC_LAYER_IMPL_H_
6 #define CC_LAYER_IMPL_H_
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "cc/cc_export.h"
13 #include "cc/draw_properties.h"
14 #include "cc/input_handler.h"
15 #include "cc/layer_animation_controller.h"
16 #include "cc/layer_animation_value_observer.h"
17 #include "cc/region.h"
18 #include "cc/render_pass.h"
19 #include "cc/render_surface_impl.h"
20 #include "cc/resource_provider.h"
21 #include "cc/scoped_ptr_vector.h"
22 #include "cc/shared_quad_state.h"
23 #include "skia/ext/refptr.h"
24 #include "third_party/WebKit/Source/Platform/chromium/public/WebFilterOperations.h"
25 #include "third_party/skia/include/core/SkColor.h"
26 #include "third_party/skia/include/core/SkImageFilter.h"
27 #include "third_party/skia/include/core/SkPicture.h"
28 #include "ui/gfx/rect.h"
29 #include "ui/gfx/rect_f.h"
30 #include "ui/gfx/transform.h"
33 class DictionaryValue
;
38 class LayerTreeHostImpl
;
42 class ScrollbarAnimationController
;
43 class ScrollbarLayerImpl
;
46 struct AppendQuadsData
;
48 class CC_EXPORT LayerImpl
: LayerAnimationValueObserver
{
50 typedef ScopedPtrVector
<LayerImpl
> LayerList
;
52 static scoped_ptr
<LayerImpl
> create(LayerTreeImpl
* treeImpl
, int id
)
54 return make_scoped_ptr(new LayerImpl(treeImpl
, id
));
61 // LayerAnimationValueObserver implementation.
62 virtual void OnOpacityAnimated(float) OVERRIDE
;
63 virtual void OnTransformAnimated(const gfx::Transform
&) OVERRIDE
;
64 virtual bool IsActive() const OVERRIDE
;
67 LayerImpl
* parent() { return m_parent
; }
68 const LayerImpl
* parent() const { return m_parent
; }
69 const LayerList
& children() const { return m_children
; }
70 LayerList
& children() { return m_children
; }
71 LayerImpl
* childAt(size_t index
) const;
72 void addChild(scoped_ptr
<LayerImpl
>);
73 scoped_ptr
<LayerImpl
> removeChild(LayerImpl
* child
);
74 void removeAllChildren();
75 void setParent(LayerImpl
* parent
) { m_parent
= parent
; }
76 void clearChildList(); // Warning: This does not preserve tree structure invariants.
78 void setMaskLayer(scoped_ptr
<LayerImpl
>);
79 LayerImpl
* maskLayer() { return m_maskLayer
.get(); }
80 const LayerImpl
* maskLayer() const { return m_maskLayer
.get(); }
81 scoped_ptr
<LayerImpl
> takeMaskLayer();
83 void setReplicaLayer(scoped_ptr
<LayerImpl
>);
84 LayerImpl
* replicaLayer() { return m_replicaLayer
.get(); }
85 const LayerImpl
* replicaLayer() const { return m_replicaLayer
.get(); }
86 scoped_ptr
<LayerImpl
> takeReplicaLayer();
88 bool hasMask() const { return m_maskLayer
; }
89 bool hasReplica() const { return m_replicaLayer
; }
90 bool replicaHasMask() const { return m_replicaLayer
&& (m_maskLayer
|| m_replicaLayer
->m_maskLayer
); }
92 LayerTreeImpl
* layerTreeImpl() const { return m_layerTreeImpl
; }
94 scoped_ptr
<SharedQuadState
> createSharedQuadState() const;
95 // willDraw must be called before appendQuads. If willDraw is called,
96 // didDraw is guaranteed to be called before another willDraw or before
97 // the layer is destroyed. To enforce this, any class that overrides
98 // willDraw/didDraw must call the base class version.
99 virtual void willDraw(ResourceProvider
*);
100 virtual void appendQuads(QuadSink
&, AppendQuadsData
&) { }
101 virtual void didDraw(ResourceProvider
*);
103 virtual ResourceProvider::ResourceId
contentsResourceId() const;
105 virtual bool hasDelegatedContent() const;
106 virtual bool hasContributingDelegatedRenderPasses() const;
107 virtual RenderPass::Id
firstContributingRenderPassId() const;
108 virtual RenderPass::Id
nextContributingRenderPassId(RenderPass::Id
) const;
110 virtual void updateTilePriorities() { }
112 virtual ScrollbarLayerImpl
* toScrollbarLayer();
114 // Returns true if this layer has content to draw.
115 void setDrawsContent(bool);
116 bool drawsContent() const { return m_drawsContent
; }
118 bool forceRenderSurface() const { return m_forceRenderSurface
; }
119 void setForceRenderSurface(bool force
) { m_forceRenderSurface
= force
; }
121 void setAnchorPoint(const gfx::PointF
&);
122 const gfx::PointF
& anchorPoint() const { return m_anchorPoint
; }
124 void setAnchorPointZ(float);
125 float anchorPointZ() const { return m_anchorPointZ
; }
127 void setBackgroundColor(SkColor
);
128 SkColor
backgroundColor() const { return m_backgroundColor
; }
130 void setFilters(const WebKit::WebFilterOperations
&);
131 const WebKit::WebFilterOperations
& filters() const { return m_filters
; }
133 void setBackgroundFilters(const WebKit::WebFilterOperations
&);
134 const WebKit::WebFilterOperations
& backgroundFilters() const { return m_backgroundFilters
; }
136 void setFilter(const skia::RefPtr
<SkImageFilter
>&);
137 skia::RefPtr
<SkImageFilter
> filter() const { return m_filter
; }
139 void setMasksToBounds(bool);
140 bool masksToBounds() const { return m_masksToBounds
; }
142 void setContentsOpaque(bool);
143 bool contentsOpaque() const { return m_contentsOpaque
; }
145 void setOpacity(float);
146 float opacity() const;
147 bool opacityIsAnimating() const;
149 void setPosition(const gfx::PointF
&);
150 const gfx::PointF
& position() const { return m_position
; }
152 void setIsContainerForFixedPositionLayers(bool isContainerForFixedPositionLayers
) { m_isContainerForFixedPositionLayers
= isContainerForFixedPositionLayers
; }
153 bool isContainerForFixedPositionLayers() const { return m_isContainerForFixedPositionLayers
; }
155 void setFixedToContainerLayer(bool fixedToContainerLayer
= true) { m_fixedToContainerLayer
= fixedToContainerLayer
;}
156 bool fixedToContainerLayer() const { return m_fixedToContainerLayer
; }
158 void setPreserves3D(bool);
159 bool preserves3D() const { return m_preserves3D
; }
161 void setUseParentBackfaceVisibility(bool useParentBackfaceVisibility
) { m_useParentBackfaceVisibility
= useParentBackfaceVisibility
; }
162 bool useParentBackfaceVisibility() const { return m_useParentBackfaceVisibility
; }
164 void setSublayerTransform(const gfx::Transform
&);
165 const gfx::Transform
& sublayerTransform() const { return m_sublayerTransform
; }
168 void setDebugName(const std::string
& debugName
) { m_debugName
= debugName
; }
169 std::string
debugName() const { return m_debugName
; }
171 bool showDebugBorders() const;
173 // These invalidate the host's render surface layer list. The caller
174 // is responsible for calling setNeedsUpdateDrawProperties on the host
175 // so that its list can be recreated.
176 void createRenderSurface();
177 void clearRenderSurface() { m_drawProperties
.render_surface
.reset(); }
179 DrawProperties
<LayerImpl
, RenderSurfaceImpl
>& drawProperties() { return m_drawProperties
; }
180 const DrawProperties
<LayerImpl
, RenderSurfaceImpl
>& drawProperties() const { return m_drawProperties
; }
182 // The following are shortcut accessors to get various information from m_drawProperties
183 const gfx::Transform
& drawTransform() const { return m_drawProperties
.target_space_transform
; }
184 const gfx::Transform
& screenSpaceTransform() const { return m_drawProperties
.screen_space_transform
; }
185 float drawOpacity() const { return m_drawProperties
.opacity
; }
186 bool drawOpacityIsAnimating() const { return m_drawProperties
.opacity_is_animating
; }
187 bool drawTransformIsAnimating() const { return m_drawProperties
.target_space_transform_is_animating
; }
188 bool screenSpaceTransformIsAnimating() const { return m_drawProperties
.screen_space_transform_is_animating
; }
189 bool screenSpaceOpacityIsAnimating() const { return m_drawProperties
.screen_space_opacity_is_animating
; }
190 bool canUseLCDText() const { return m_drawProperties
.can_use_lcd_text
; }
191 bool isClipped() const { return m_drawProperties
.is_clipped
; }
192 const gfx::Rect
& clipRect() const { return m_drawProperties
.clip_rect
; }
193 const gfx::Rect
& drawableContentRect() const { return m_drawProperties
.drawable_content_rect
; }
194 const gfx::Rect
& visibleContentRect() const { return m_drawProperties
.visible_content_rect
; }
195 LayerImpl
* renderTarget() { DCHECK(!m_drawProperties
.render_target
|| m_drawProperties
.render_target
->renderSurface()); return m_drawProperties
.render_target
; }
196 const LayerImpl
* renderTarget() const { DCHECK(!m_drawProperties
.render_target
|| m_drawProperties
.render_target
->renderSurface()); return m_drawProperties
.render_target
; }
197 RenderSurfaceImpl
* renderSurface() const { return m_drawProperties
.render_surface
.get(); }
199 // The client should be responsible for setting bounds, contentBounds and
200 // contentsScale to appropriate values. LayerImpl doesn't calculate any of
201 // them from the other values.
203 void setBounds(const gfx::Size
&);
204 const gfx::Size
& bounds() const { return m_bounds
; }
206 void setContentBounds(const gfx::Size
&);
207 gfx::Size
contentBounds() const { return m_drawProperties
.content_bounds
; }
209 float contentsScaleX() const { return m_drawProperties
.contents_scale_x
; }
210 float contentsScaleY() const { return m_drawProperties
.contents_scale_y
; }
211 void setContentsScale(float contentsScaleX
, float contentsScaleY
);
213 virtual void calculateContentsScale(
214 float idealContentsScale
,
215 float* contentsScaleX
,
216 float* contentsScaleY
,
217 gfx::Size
* contentBounds
);
219 gfx::Vector2d
scrollOffset() const { return m_scrollOffset
; }
220 void setScrollOffset(gfx::Vector2d
);
222 gfx::Vector2d
maxScrollOffset() const {return m_maxScrollOffset
; }
223 void setMaxScrollOffset(gfx::Vector2d
);
225 const gfx::Vector2dF
& scrollDelta() const { return m_scrollDelta
; }
226 void setScrollDelta(const gfx::Vector2dF
&);
228 const gfx::Transform
& implTransform() const { return m_implTransform
; }
229 void setImplTransform(const gfx::Transform
& transform
);
231 const gfx::Vector2d
& sentScrollDelta() const { return m_sentScrollDelta
; }
232 void setSentScrollDelta(const gfx::Vector2d
& sentScrollDelta
);
234 // Returns the delta of the scroll that was outside of the bounds of the initial scroll
235 gfx::Vector2dF
scrollBy(const gfx::Vector2dF
& scroll
);
237 bool scrollable() const { return m_scrollable
; }
238 void setScrollable(bool scrollable
) { m_scrollable
= scrollable
; }
240 bool shouldScrollOnMainThread() const { return m_shouldScrollOnMainThread
; }
241 void setShouldScrollOnMainThread(bool shouldScrollOnMainThread
) { m_shouldScrollOnMainThread
= shouldScrollOnMainThread
; }
243 bool haveWheelEventHandlers() const { return m_haveWheelEventHandlers
; }
244 void setHaveWheelEventHandlers(bool haveWheelEventHandlers
) { m_haveWheelEventHandlers
= haveWheelEventHandlers
; }
246 const Region
& nonFastScrollableRegion() const { return m_nonFastScrollableRegion
; }
247 void setNonFastScrollableRegion(const Region
& region
) { m_nonFastScrollableRegion
= region
; }
249 const Region
& touchEventHandlerRegion() const { return m_touchEventHandlerRegion
; }
250 void setTouchEventHandlerRegion(const Region
& region
) { m_touchEventHandlerRegion
= region
; }
252 void setDrawCheckerboardForMissingTiles(bool checkerboard
) { m_drawCheckerboardForMissingTiles
= checkerboard
; }
253 bool drawCheckerboardForMissingTiles() const;
255 InputHandlerClient::ScrollStatus
tryScroll(const gfx::PointF
& screenSpacePoint
, InputHandlerClient::ScrollInputType
) const;
257 bool doubleSided() const { return m_doubleSided
; }
258 void setDoubleSided(bool);
260 void setTransform(const gfx::Transform
&);
261 const gfx::Transform
& transform() const;
262 bool transformIsAnimating() const;
264 const gfx::RectF
& updateRect() const { return m_updateRect
; }
265 void setUpdateRect(const gfx::RectF
& updateRect
) { m_updateRect
= updateRect
; }
267 std::string
layerTreeAsText() const;
268 virtual base::DictionaryValue
* layerTreeAsJson() const;
270 void setStackingOrderChanged(bool);
272 bool layerPropertyChanged() const { return m_layerPropertyChanged
|| layerIsAlwaysDamaged(); }
273 bool layerSurfacePropertyChanged() const;
275 void resetAllChangeTrackingForSubtree();
277 virtual bool layerIsAlwaysDamaged() const;
279 LayerAnimationController
* layerAnimationController() { return m_layerAnimationController
.get(); }
281 virtual Region
visibleContentOpaqueRegion() const;
283 virtual void didBecomeActive();
285 // Indicates that the surface previously used to render this layer
286 // was lost and that a new one has been created. Won't be called
287 // until the new surface has been created successfully.
288 virtual void didLoseOutputSurface();
290 ScrollbarAnimationController
* scrollbarAnimationController() const { return m_scrollbarAnimationController
.get(); }
292 void setScrollbarOpacity(float opacity
);
294 void setHorizontalScrollbarLayer(ScrollbarLayerImpl
*);
295 ScrollbarLayerImpl
* horizontalScrollbarLayer() { return m_horizontalScrollbarLayer
; }
297 void setVerticalScrollbarLayer(ScrollbarLayerImpl
*);
298 ScrollbarLayerImpl
* verticalScrollbarLayer() { return m_verticalScrollbarLayer
; }
300 gfx::Rect
layerRectToContentRect(const gfx::RectF
& layerRect
) const;
302 virtual skia::RefPtr
<SkPicture
> getPicture();
304 virtual bool canClipSelf() const;
306 virtual bool areVisibleResourcesReady() const;
308 virtual scoped_ptr
<LayerImpl
> createLayerImpl(LayerTreeImpl
*);
309 virtual void pushPropertiesTo(LayerImpl
*);
312 LayerImpl(LayerTreeImpl
* layerImpl
, int);
314 // Get the color and size of the layer's debug border.
315 virtual void getDebugBorderProperties(SkColor
*, float* width
) const;
317 void appendDebugBorderQuad(QuadSink
&, const SharedQuadState
*, AppendQuadsData
&) const;
319 virtual void dumpLayerProperties(std::string
*, int indent
) const;
320 static std::string
indentString(int indent
);
323 void updateScrollbarPositions();
325 void noteLayerSurfacePropertyChanged();
326 void noteLayerPropertyChanged();
327 void noteLayerPropertyChangedForSubtree();
329 // Note carefully this does not affect the current layer.
330 void noteLayerPropertyChangedForDescendants();
332 virtual const char* layerTypeAsString() const;
334 void dumpLayer(std::string
*, int indent
) const;
336 // Properties internal to LayerImpl
338 LayerList m_children
;
339 // m_maskLayer can be temporarily stolen during tree sync, we need this ID to confirm newly assigned layer is still the previous one
341 scoped_ptr
<LayerImpl
> m_maskLayer
;
342 int m_replicaLayerId
; // ditto
343 scoped_ptr
<LayerImpl
> m_replicaLayer
;
345 LayerTreeImpl
* m_layerTreeImpl
;
347 // Properties synchronized from the associated Layer.
348 gfx::PointF m_anchorPoint
;
349 float m_anchorPointZ
;
351 gfx::Vector2d m_scrollOffset
;
353 bool m_shouldScrollOnMainThread
;
354 bool m_haveWheelEventHandlers
;
355 Region m_nonFastScrollableRegion
;
356 Region m_touchEventHandlerRegion
;
357 SkColor m_backgroundColor
;
358 bool m_stackingOrderChanged
;
360 // Whether the "back" of this layer should draw.
363 // Tracks if drawing-related properties have changed since last redraw.
364 bool m_layerPropertyChanged
;
366 // Indicates that a property has changed on this layer that would not
367 // affect the pixels on its target surface, but would require redrawing
368 // but would require redrawing the targetSurface onto its ancestor targetSurface.
369 // For layers that do not own a surface this flag acts as m_layerPropertyChanged.
370 bool m_layerSurfacePropertyChanged
;
372 bool m_masksToBounds
;
373 bool m_contentsOpaque
;
375 gfx::PointF m_position
;
377 bool m_useParentBackfaceVisibility
;
378 bool m_drawCheckerboardForMissingTiles
;
379 gfx::Transform m_sublayerTransform
;
380 gfx::Transform m_transform
;
383 bool m_forceRenderSurface
;
385 // Set for the layer that other layers are fixed to.
386 bool m_isContainerForFixedPositionLayers
;
387 // This is true if the layer should be fixed to the closest ancestor container.
388 bool m_fixedToContainerLayer
;
390 gfx::Vector2dF m_scrollDelta
;
391 gfx::Vector2d m_sentScrollDelta
;
392 gfx::Vector2d m_maxScrollOffset
;
393 gfx::Transform m_implTransform
;
394 gfx::Vector2dF m_lastScrollOffset
;
396 // The global depth value of the center of the layer. This value is used
397 // to sort layers from back to front.
401 std::string m_debugName
;
403 WebKit::WebFilterOperations m_filters
;
404 WebKit::WebFilterOperations m_backgroundFilters
;
405 skia::RefPtr
<SkImageFilter
> m_filter
;
408 bool m_betweenWillDrawAndDidDraw
;
411 // Rect indicating what was repainted/updated during update.
412 // Note that plugin layers bypass this and leave it empty.
413 // Uses layer's content space.
414 gfx::RectF m_updateRect
;
416 // Manages animations for this layer.
417 scoped_refptr
<LayerAnimationController
> m_layerAnimationController
;
419 // Manages scrollbars for this layer
420 scoped_ptr
<ScrollbarAnimationController
> m_scrollbarAnimationController
;
422 // Weak pointers to this layer's scrollbars, if it has them. Updated during
423 // tree synchronization.
424 ScrollbarLayerImpl
* m_horizontalScrollbarLayer
;
425 ScrollbarLayerImpl
* m_verticalScrollbarLayer
;
427 // Group of properties that need to be computed based on the layer tree
428 // hierarchy before layers can be drawn.
429 DrawProperties
<LayerImpl
, RenderSurfaceImpl
> m_drawProperties
;
431 DISALLOW_COPY_AND_ASSIGN(LayerImpl
);
436 #endif // CC_LAYER_IMPL_H_