add a use_alsa gyp setting
[chromium-blink-merge.git] / cc / layer_impl.h
blob20b2384cb7f464348cb9dd31a4813154de3a1254
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_
8 #include <string>
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/values.h"
13 #include "cc/cc_export.h"
14 #include "cc/draw_properties.h"
15 #include "cc/input_handler.h"
16 #include "cc/layer_animation_controller.h"
17 #include "cc/layer_animation_value_observer.h"
18 #include "cc/region.h"
19 #include "cc/render_pass.h"
20 #include "cc/render_surface_impl.h"
21 #include "cc/resource_provider.h"
22 #include "cc/scoped_ptr_vector.h"
23 #include "cc/shared_quad_state.h"
24 #include "skia/ext/refptr.h"
25 #include "third_party/WebKit/Source/Platform/chromium/public/WebFilterOperations.h"
26 #include "third_party/skia/include/core/SkColor.h"
27 #include "third_party/skia/include/core/SkImageFilter.h"
28 #include "third_party/skia/include/core/SkPicture.h"
29 #include "ui/gfx/rect.h"
30 #include "ui/gfx/rect_f.h"
31 #include "ui/gfx/transform.h"
33 namespace base {
34 class DictionaryValue;
37 namespace cc {
39 class LayerTreeHostImpl;
40 class LayerTreeImpl;
41 class QuadSink;
42 class Renderer;
43 class ScrollbarAnimationController;
44 class ScrollbarLayerImpl;
45 class Layer;
47 struct AppendQuadsData;
49 class CC_EXPORT LayerImpl : LayerAnimationValueObserver {
50 public:
51 typedef ScopedPtrVector<LayerImpl> LayerList;
53 static scoped_ptr<LayerImpl> create(LayerTreeImpl* treeImpl, int id)
55 return make_scoped_ptr(new LayerImpl(treeImpl, id));
58 virtual ~LayerImpl();
60 int id() const;
62 // LayerAnimationValueObserver implementation.
63 virtual void OnOpacityAnimated(float) OVERRIDE;
64 virtual void OnTransformAnimated(const gfx::Transform&) OVERRIDE;
65 virtual bool IsActive() const OVERRIDE;
67 // Tree structure.
68 LayerImpl* parent() { return m_parent; }
69 const LayerImpl* parent() const { return m_parent; }
70 const LayerList& children() const { return m_children; }
71 LayerList& children() { return m_children; }
72 LayerImpl* childAt(size_t index) const;
73 void addChild(scoped_ptr<LayerImpl>);
74 scoped_ptr<LayerImpl> removeChild(LayerImpl* child);
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; }
167 // Debug layer name.
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 bool animatingTransformToScreen,
216 float* contentsScaleX,
217 float* contentsScaleY,
218 gfx::Size* contentBounds);
220 gfx::Vector2d scrollOffset() const { return m_scrollOffset; }
221 void setScrollOffset(gfx::Vector2d);
223 gfx::Vector2d maxScrollOffset() const {return m_maxScrollOffset; }
224 void setMaxScrollOffset(gfx::Vector2d);
226 const gfx::Vector2dF& scrollDelta() const { return m_scrollDelta; }
227 void setScrollDelta(const gfx::Vector2dF&);
229 const gfx::Transform& implTransform() const { return m_implTransform; }
230 void setImplTransform(const gfx::Transform& transform);
232 const gfx::Vector2d& sentScrollDelta() const { return m_sentScrollDelta; }
233 void setSentScrollDelta(const gfx::Vector2d& sentScrollDelta);
235 // Returns the delta of the scroll that was outside of the bounds of the initial scroll
236 gfx::Vector2dF scrollBy(const gfx::Vector2dF& scroll);
238 bool scrollable() const { return m_scrollable; }
239 void setScrollable(bool scrollable) { m_scrollable = scrollable; }
241 bool shouldScrollOnMainThread() const { return m_shouldScrollOnMainThread; }
242 void setShouldScrollOnMainThread(bool shouldScrollOnMainThread) { m_shouldScrollOnMainThread = shouldScrollOnMainThread; }
244 bool haveWheelEventHandlers() const { return m_haveWheelEventHandlers; }
245 void setHaveWheelEventHandlers(bool haveWheelEventHandlers) { m_haveWheelEventHandlers = haveWheelEventHandlers; }
247 const Region& nonFastScrollableRegion() const { return m_nonFastScrollableRegion; }
248 void setNonFastScrollableRegion(const Region& region) { m_nonFastScrollableRegion = region; }
250 const Region& touchEventHandlerRegion() const { return m_touchEventHandlerRegion; }
251 void setTouchEventHandlerRegion(const Region& region) { m_touchEventHandlerRegion = region; }
253 void setDrawCheckerboardForMissingTiles(bool checkerboard) { m_drawCheckerboardForMissingTiles = checkerboard; }
254 bool drawCheckerboardForMissingTiles() const;
256 InputHandlerClient::ScrollStatus tryScroll(const gfx::PointF& screenSpacePoint, InputHandlerClient::ScrollInputType) const;
258 bool doubleSided() const { return m_doubleSided; }
259 void setDoubleSided(bool);
261 void setTransform(const gfx::Transform&);
262 const gfx::Transform& transform() const;
263 bool transformIsAnimating() const;
265 const gfx::RectF& updateRect() const { return m_updateRect; }
266 void setUpdateRect(const gfx::RectF& updateRect) { m_updateRect = updateRect; }
268 std::string layerTreeAsText() const;
269 virtual base::DictionaryValue* layerTreeAsJson() const;
271 void setStackingOrderChanged(bool);
273 bool layerPropertyChanged() const { return m_layerPropertyChanged || layerIsAlwaysDamaged(); }
274 bool layerSurfacePropertyChanged() const;
276 void resetAllChangeTrackingForSubtree();
278 virtual bool layerIsAlwaysDamaged() const;
280 LayerAnimationController* layerAnimationController() { return m_layerAnimationController.get(); }
282 virtual Region visibleContentOpaqueRegion() const;
284 virtual void didBecomeActive();
286 // Indicates that the surface previously used to render this layer
287 // was lost and that a new one has been created. Won't be called
288 // until the new surface has been created successfully.
289 virtual void didLoseOutputSurface();
291 ScrollbarAnimationController* scrollbarAnimationController() const { return m_scrollbarAnimationController.get(); }
293 void setScrollbarOpacity(float opacity);
295 void setHorizontalScrollbarLayer(ScrollbarLayerImpl*);
296 ScrollbarLayerImpl* horizontalScrollbarLayer() { return m_horizontalScrollbarLayer; }
298 void setVerticalScrollbarLayer(ScrollbarLayerImpl*);
299 ScrollbarLayerImpl* verticalScrollbarLayer() { return m_verticalScrollbarLayer; }
301 gfx::Rect layerRectToContentRect(const gfx::RectF& layerRect) const;
303 virtual skia::RefPtr<SkPicture> getPicture();
305 virtual bool canClipSelf() const;
307 virtual bool areVisibleResourcesReady() const;
309 virtual scoped_ptr<LayerImpl> createLayerImpl(LayerTreeImpl*);
310 virtual void pushPropertiesTo(LayerImpl*);
312 virtual scoped_ptr<base::Value> AsValue() const;
314 protected:
315 LayerImpl(LayerTreeImpl* layerImpl, int);
317 // Get the color and size of the layer's debug border.
318 virtual void getDebugBorderProperties(SkColor*, float* width) const;
320 void appendDebugBorderQuad(QuadSink&, const SharedQuadState*, AppendQuadsData&) const;
322 virtual void dumpLayerProperties(std::string*, int indent) const;
323 static std::string indentString(int indent);
325 void AsValueInto(base::DictionaryValue* dict) const;
327 private:
328 void updateScrollbarPositions();
330 void noteLayerSurfacePropertyChanged();
331 void noteLayerPropertyChanged();
332 void noteLayerPropertyChangedForSubtree();
334 // Note carefully this does not affect the current layer.
335 void noteLayerPropertyChangedForDescendants();
337 virtual const char* layerTypeAsString() const;
339 void dumpLayer(std::string*, int indent) const;
341 // Properties internal to LayerImpl
342 LayerImpl* m_parent;
343 LayerList m_children;
344 // m_maskLayer can be temporarily stolen during tree sync, we need this ID to confirm newly assigned layer is still the previous one
345 int m_maskLayerId;
346 scoped_ptr<LayerImpl> m_maskLayer;
347 int m_replicaLayerId; // ditto
348 scoped_ptr<LayerImpl> m_replicaLayer;
349 int m_layerId;
350 LayerTreeImpl* m_layerTreeImpl;
352 // Properties synchronized from the associated Layer.
353 gfx::PointF m_anchorPoint;
354 float m_anchorPointZ;
355 gfx::Size m_bounds;
356 gfx::Vector2d m_scrollOffset;
357 bool m_scrollable;
358 bool m_shouldScrollOnMainThread;
359 bool m_haveWheelEventHandlers;
360 Region m_nonFastScrollableRegion;
361 Region m_touchEventHandlerRegion;
362 SkColor m_backgroundColor;
363 bool m_stackingOrderChanged;
365 // Whether the "back" of this layer should draw.
366 bool m_doubleSided;
368 // Tracks if drawing-related properties have changed since last redraw.
369 bool m_layerPropertyChanged;
371 // Indicates that a property has changed on this layer that would not
372 // affect the pixels on its target surface, but would require redrawing
373 // the targetSurface onto its ancestor targetSurface.
374 // For layers that do not own a surface this flag acts as m_layerPropertyChanged.
375 bool m_layerSurfacePropertyChanged;
377 bool m_masksToBounds;
378 bool m_contentsOpaque;
379 float m_opacity;
380 gfx::PointF m_position;
381 bool m_preserves3D;
382 bool m_useParentBackfaceVisibility;
383 bool m_drawCheckerboardForMissingTiles;
384 gfx::Transform m_sublayerTransform;
385 gfx::Transform m_transform;
387 bool m_drawsContent;
388 bool m_forceRenderSurface;
390 // Set for the layer that other layers are fixed to.
391 bool m_isContainerForFixedPositionLayers;
392 // This is true if the layer should be fixed to the closest ancestor container.
393 bool m_fixedToContainerLayer;
395 gfx::Vector2dF m_scrollDelta;
396 gfx::Vector2d m_sentScrollDelta;
397 gfx::Vector2d m_maxScrollOffset;
398 gfx::Transform m_implTransform;
399 gfx::Vector2dF m_lastScrollOffset;
401 // The global depth value of the center of the layer. This value is used
402 // to sort layers from back to front.
403 float m_drawDepth;
405 // Debug layer name.
406 std::string m_debugName;
408 WebKit::WebFilterOperations m_filters;
409 WebKit::WebFilterOperations m_backgroundFilters;
410 skia::RefPtr<SkImageFilter> m_filter;
412 #ifndef NDEBUG
413 bool m_betweenWillDrawAndDidDraw;
414 #endif
416 // Rect indicating what was repainted/updated during update.
417 // Note that plugin layers bypass this and leave it empty.
418 // Uses layer's content space.
419 gfx::RectF m_updateRect;
421 // Manages animations for this layer.
422 scoped_refptr<LayerAnimationController> m_layerAnimationController;
424 // Manages scrollbars for this layer
425 scoped_ptr<ScrollbarAnimationController> m_scrollbarAnimationController;
427 // Weak pointers to this layer's scrollbars, if it has them. Updated during
428 // tree synchronization.
429 ScrollbarLayerImpl* m_horizontalScrollbarLayer;
430 ScrollbarLayerImpl* m_verticalScrollbarLayer;
432 // Group of properties that need to be computed based on the layer tree
433 // hierarchy before layers can be drawn.
434 DrawProperties<LayerImpl, RenderSurfaceImpl> m_drawProperties;
436 DISALLOW_COPY_AND_ASSIGN(LayerImpl);
441 #endif // CC_LAYER_IMPL_H_