Mailbox support for texture layers.
[chromium-blink-merge.git] / cc / layer_impl.h
blob2219cf77bc3e7e9cfa440ccfa0a867c7ab88454c
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 "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 "ui/gfx/rect.h"
28 #include "ui/gfx/rect_f.h"
29 #include "ui/gfx/transform.h"
31 namespace base {
32 class DictionaryValue;
35 namespace cc {
37 class LayerTreeHostImpl;
38 class LayerTreeImpl;
39 class QuadSink;
40 class Renderer;
41 class ScrollbarAnimationController;
42 class ScrollbarLayerImpl;
43 class Layer;
45 struct AppendQuadsData;
47 class CC_EXPORT LayerImpl : LayerAnimationValueObserver {
48 public:
49 typedef ScopedPtrVector<LayerImpl> LayerList;
51 static scoped_ptr<LayerImpl> create(LayerTreeImpl* treeImpl, int id)
53 return make_scoped_ptr(new LayerImpl(treeImpl, id));
56 virtual ~LayerImpl();
58 int id() const;
60 // LayerAnimationValueObserver implementation.
61 virtual void OnOpacityAnimated(float) OVERRIDE;
62 virtual void OnTransformAnimated(const gfx::Transform&) OVERRIDE;
63 virtual bool IsActive() const OVERRIDE;
65 // Tree structure.
66 LayerImpl* parent() { return m_parent; }
67 const LayerImpl* parent() const { return m_parent; }
68 const LayerList& children() const { return m_children; }
69 void addChild(scoped_ptr<LayerImpl>);
70 scoped_ptr<LayerImpl> removeChild(LayerImpl* child);
71 void removeAllChildren();
73 void setMaskLayer(scoped_ptr<LayerImpl>);
74 LayerImpl* maskLayer() { return m_maskLayer.get(); }
75 const LayerImpl* maskLayer() const { return m_maskLayer.get(); }
77 void setReplicaLayer(scoped_ptr<LayerImpl>);
78 LayerImpl* replicaLayer() { return m_replicaLayer.get(); }
79 const LayerImpl* replicaLayer() const { return m_replicaLayer.get(); }
81 bool hasMask() const { return m_maskLayer; }
82 bool hasReplica() const { return m_replicaLayer; }
83 bool replicaHasMask() const { return m_replicaLayer && (m_maskLayer || m_replicaLayer->m_maskLayer); }
85 LayerTreeImpl* layerTreeImpl() const { return m_layerTreeImpl; }
87 scoped_ptr<SharedQuadState> createSharedQuadState() const;
88 // willDraw must be called before appendQuads. If willDraw is called,
89 // didDraw is guaranteed to be called before another willDraw or before
90 // the layer is destroyed. To enforce this, any class that overrides
91 // willDraw/didDraw must call the base class version.
92 virtual void willDraw(ResourceProvider*);
93 virtual void appendQuads(QuadSink&, AppendQuadsData&) { }
94 virtual void didDraw(ResourceProvider*);
96 virtual ResourceProvider::ResourceId contentsResourceId() const;
98 virtual bool hasDelegatedContent() const;
99 virtual bool hasContributingDelegatedRenderPasses() const;
100 virtual RenderPass::Id firstContributingRenderPassId() const;
101 virtual RenderPass::Id nextContributingRenderPassId(RenderPass::Id) const;
103 // Returns true if this layer has content to draw.
104 void setDrawsContent(bool);
105 bool drawsContent() const { return m_drawsContent; }
107 bool forceRenderSurface() const { return m_forceRenderSurface; }
108 void setForceRenderSurface(bool force) { m_forceRenderSurface = force; }
110 void setAnchorPoint(const gfx::PointF&);
111 const gfx::PointF& anchorPoint() const { return m_anchorPoint; }
113 void setAnchorPointZ(float);
114 float anchorPointZ() const { return m_anchorPointZ; }
116 void setBackgroundColor(SkColor);
117 SkColor backgroundColor() const { return m_backgroundColor; }
119 void setFilters(const WebKit::WebFilterOperations&);
120 const WebKit::WebFilterOperations& filters() const { return m_filters; }
122 void setBackgroundFilters(const WebKit::WebFilterOperations&);
123 const WebKit::WebFilterOperations& backgroundFilters() const { return m_backgroundFilters; }
125 void setFilter(const skia::RefPtr<SkImageFilter>&);
126 skia::RefPtr<SkImageFilter> filter() const { return m_filter; }
128 void setMasksToBounds(bool);
129 bool masksToBounds() const { return m_masksToBounds; }
131 void setContentsOpaque(bool);
132 bool contentsOpaque() const { return m_contentsOpaque; }
134 void setOpacity(float);
135 float opacity() const;
136 bool opacityIsAnimating() const;
138 void setPosition(const gfx::PointF&);
139 const gfx::PointF& position() const { return m_position; }
141 void setIsContainerForFixedPositionLayers(bool isContainerForFixedPositionLayers) { m_isContainerForFixedPositionLayers = isContainerForFixedPositionLayers; }
142 bool isContainerForFixedPositionLayers() const { return m_isContainerForFixedPositionLayers; }
144 void setFixedToContainerLayer(bool fixedToContainerLayer = true) { m_fixedToContainerLayer = fixedToContainerLayer;}
145 bool fixedToContainerLayer() const { return m_fixedToContainerLayer; }
147 void setPreserves3D(bool);
148 bool preserves3D() const { return m_preserves3D; }
150 void setUseParentBackfaceVisibility(bool useParentBackfaceVisibility) { m_useParentBackfaceVisibility = useParentBackfaceVisibility; }
151 bool useParentBackfaceVisibility() const { return m_useParentBackfaceVisibility; }
153 void setSublayerTransform(const gfx::Transform&);
154 const gfx::Transform& sublayerTransform() const { return m_sublayerTransform; }
156 // Debug layer name.
157 void setDebugName(const std::string& debugName) { m_debugName = debugName; }
158 std::string debugName() const { return m_debugName; }
160 bool showDebugBorders() const;
162 // These invalidate the host's render surface layer list. The caller
163 // is responsible for calling setNeedsUpdateDrawProperties on the host
164 // so that its list can be recreated.
165 void createRenderSurface();
166 void clearRenderSurface() { m_drawProperties.render_surface.reset(); }
168 DrawProperties<LayerImpl, RenderSurfaceImpl>& drawProperties() { return m_drawProperties; }
169 const DrawProperties<LayerImpl, RenderSurfaceImpl>& drawProperties() const { return m_drawProperties; }
171 // The following are shortcut accessors to get various information from m_drawProperties
172 const gfx::Transform& drawTransform() const { return m_drawProperties.target_space_transform; }
173 const gfx::Transform& screenSpaceTransform() const { return m_drawProperties.screen_space_transform; }
174 float drawOpacity() const { return m_drawProperties.opacity; }
175 bool drawOpacityIsAnimating() const { return m_drawProperties.opacity_is_animating; }
176 bool drawTransformIsAnimating() const { return m_drawProperties.target_space_transform_is_animating; }
177 bool screenSpaceTransformIsAnimating() const { return m_drawProperties.screen_space_transform_is_animating; }
178 bool screenSpaceOpacityIsAnimating() const { return m_drawProperties.screen_space_opacity_is_animating; }
179 bool canUseLCDText() const { return m_drawProperties.can_use_lcd_text; }
180 bool isClipped() const { return m_drawProperties.is_clipped; }
181 const gfx::Rect& clipRect() const { return m_drawProperties.clip_rect; }
182 const gfx::Rect& drawableContentRect() const { return m_drawProperties.drawable_content_rect; }
183 const gfx::Rect& visibleContentRect() const { return m_drawProperties.visible_content_rect; }
184 LayerImpl* renderTarget() { DCHECK(!m_drawProperties.render_target || m_drawProperties.render_target->renderSurface()); return m_drawProperties.render_target; }
185 const LayerImpl* renderTarget() const { DCHECK(!m_drawProperties.render_target || m_drawProperties.render_target->renderSurface()); return m_drawProperties.render_target; }
186 RenderSurfaceImpl* renderSurface() const { return m_drawProperties.render_surface.get(); }
188 // The client should be responsible for setting bounds, contentBounds and
189 // contentsScale to appropriate values. LayerImpl doesn't calculate any of
190 // them from the other values.
192 void setBounds(const gfx::Size&);
193 const gfx::Size& bounds() const { return m_bounds; }
195 void setContentBounds(const gfx::Size&);
196 gfx::Size contentBounds() const { return m_drawProperties.content_bounds; }
198 float contentsScaleX() const { return m_drawProperties.contents_scale_x; }
199 float contentsScaleY() const { return m_drawProperties.contents_scale_y; }
200 void setContentsScale(float contentsScaleX, float contentsScaleY);
202 virtual void calculateContentsScale(
203 float idealContentsScale,
204 float* contentsScaleX,
205 float* contentsScaleY,
206 gfx::Size* contentBounds);
208 gfx::Vector2d scrollOffset() const { return m_scrollOffset; }
209 void setScrollOffset(gfx::Vector2d);
211 gfx::Vector2d maxScrollOffset() const {return m_maxScrollOffset; }
212 void setMaxScrollOffset(gfx::Vector2d);
214 const gfx::Vector2dF& scrollDelta() const { return m_scrollDelta; }
215 void setScrollDelta(const gfx::Vector2dF&);
217 const gfx::Transform& implTransform() const { return m_implTransform; }
218 void setImplTransform(const gfx::Transform& transform);
220 const gfx::Vector2d& sentScrollDelta() const { return m_sentScrollDelta; }
221 void setSentScrollDelta(const gfx::Vector2d& sentScrollDelta);
223 // Returns the delta of the scroll that was outside of the bounds of the initial scroll
224 gfx::Vector2dF scrollBy(const gfx::Vector2dF& scroll);
226 bool scrollable() const { return m_scrollable; }
227 void setScrollable(bool scrollable) { m_scrollable = scrollable; }
229 bool shouldScrollOnMainThread() const { return m_shouldScrollOnMainThread; }
230 void setShouldScrollOnMainThread(bool shouldScrollOnMainThread) { m_shouldScrollOnMainThread = shouldScrollOnMainThread; }
232 bool haveWheelEventHandlers() const { return m_haveWheelEventHandlers; }
233 void setHaveWheelEventHandlers(bool haveWheelEventHandlers) { m_haveWheelEventHandlers = haveWheelEventHandlers; }
235 const Region& nonFastScrollableRegion() const { return m_nonFastScrollableRegion; }
236 void setNonFastScrollableRegion(const Region& region) { m_nonFastScrollableRegion = region; }
238 const Region& touchEventHandlerRegion() const { return m_touchEventHandlerRegion; }
239 void setTouchEventHandlerRegion(const Region& region) { m_touchEventHandlerRegion = region; }
241 void setDrawCheckerboardForMissingTiles(bool checkerboard) { m_drawCheckerboardForMissingTiles = checkerboard; }
242 bool drawCheckerboardForMissingTiles() const;
244 InputHandlerClient::ScrollStatus tryScroll(const gfx::PointF& screenSpacePoint, InputHandlerClient::ScrollInputType) const;
246 bool doubleSided() const { return m_doubleSided; }
247 void setDoubleSided(bool);
249 void setTransform(const gfx::Transform&);
250 const gfx::Transform& transform() const;
251 bool transformIsAnimating() const;
253 const gfx::RectF& updateRect() const { return m_updateRect; }
254 void setUpdateRect(const gfx::RectF& updateRect) { m_updateRect = updateRect; }
256 std::string layerTreeAsText() const;
257 virtual base::DictionaryValue* layerTreeAsJson() const;
259 void setStackingOrderChanged(bool);
261 bool layerPropertyChanged() const { return m_layerPropertyChanged || layerIsAlwaysDamaged(); }
262 bool layerSurfacePropertyChanged() const;
264 void resetAllChangeTrackingForSubtree();
266 virtual bool layerIsAlwaysDamaged() const;
268 LayerAnimationController* layerAnimationController() { return m_layerAnimationController.get(); }
270 virtual Region visibleContentOpaqueRegion() const;
272 virtual void didUpdateTransforms() { }
274 // Indicates that the surface previously used to render this layer
275 // was lost and that a new one has been created. Won't be called
276 // until the new surface has been created successfully.
277 virtual void didLoseOutputSurface();
279 ScrollbarAnimationController* scrollbarAnimationController() const { return m_scrollbarAnimationController.get(); }
281 void setHorizontalScrollbarLayer(ScrollbarLayerImpl*);
282 ScrollbarLayerImpl* horizontalScrollbarLayer();
283 const ScrollbarLayerImpl* horizontalScrollbarLayer() const;
285 void setVerticalScrollbarLayer(ScrollbarLayerImpl*);
286 ScrollbarLayerImpl* verticalScrollbarLayer();
287 const ScrollbarLayerImpl* verticalScrollbarLayer() const;
289 gfx::Rect layerRectToContentRect(const gfx::RectF& layerRect) const;
291 protected:
292 LayerImpl(LayerTreeImpl* layerImpl, int);
294 // Get the color and size of the layer's debug border.
295 virtual void getDebugBorderProperties(SkColor*, float* width) const;
297 void appendDebugBorderQuad(QuadSink&, const SharedQuadState*, AppendQuadsData&) const;
299 virtual void dumpLayerProperties(std::string*, int indent) const;
300 static std::string indentString(int indent);
302 private:
303 scoped_ptr<LayerImpl> takeMaskLayer();
304 scoped_ptr<LayerImpl> takeReplicaLayer();
306 void setParent(LayerImpl* parent) { m_parent = parent; }
307 friend class TreeSynchronizer;
308 void clearChildList(); // Warning: This does not preserve tree structure invariants and so is only exposed to the tree synchronizer.
310 void noteLayerSurfacePropertyChanged();
311 void noteLayerPropertyChanged();
312 void noteLayerPropertyChangedForSubtree();
314 // Note carefully this does not affect the current layer.
315 void noteLayerPropertyChangedForDescendants();
317 virtual const char* layerTypeAsString() const;
319 void dumpLayer(std::string*, int indent) const;
321 // Properties internal to LayerImpl
322 LayerImpl* m_parent;
323 LayerList m_children;
324 // m_maskLayer can be temporarily stolen during tree sync, we need this ID to confirm newly assigned layer is still the previous one
325 int m_maskLayerId;
326 scoped_ptr<LayerImpl> m_maskLayer;
327 int m_replicaLayerId; // ditto
328 scoped_ptr<LayerImpl> m_replicaLayer;
329 int m_layerId;
330 LayerTreeImpl* m_layerTreeImpl;
332 // Properties synchronized from the associated Layer.
333 gfx::PointF m_anchorPoint;
334 float m_anchorPointZ;
335 gfx::Size m_bounds;
336 gfx::Vector2d m_scrollOffset;
337 bool m_scrollable;
338 bool m_shouldScrollOnMainThread;
339 bool m_haveWheelEventHandlers;
340 Region m_nonFastScrollableRegion;
341 Region m_touchEventHandlerRegion;
342 SkColor m_backgroundColor;
344 // Whether the "back" of this layer should draw.
345 bool m_doubleSided;
347 // Tracks if drawing-related properties have changed since last redraw.
348 bool m_layerPropertyChanged;
350 // Indicates that a property has changed on this layer that would not
351 // affect the pixels on its target surface, but would require redrawing
352 // but would require redrawing the targetSurface onto its ancestor targetSurface.
353 // For layers that do not own a surface this flag acts as m_layerPropertyChanged.
354 bool m_layerSurfacePropertyChanged;
356 bool m_masksToBounds;
357 bool m_contentsOpaque;
358 float m_opacity;
359 gfx::PointF m_position;
360 bool m_preserves3D;
361 bool m_useParentBackfaceVisibility;
362 bool m_drawCheckerboardForMissingTiles;
363 gfx::Transform m_sublayerTransform;
364 gfx::Transform m_transform;
366 bool m_drawsContent;
367 bool m_forceRenderSurface;
369 // Set for the layer that other layers are fixed to.
370 bool m_isContainerForFixedPositionLayers;
371 // This is true if the layer should be fixed to the closest ancestor container.
372 bool m_fixedToContainerLayer;
374 gfx::Vector2dF m_scrollDelta;
375 gfx::Vector2d m_sentScrollDelta;
376 gfx::Vector2d m_maxScrollOffset;
377 gfx::Transform m_implTransform;
379 // The global depth value of the center of the layer. This value is used
380 // to sort layers from back to front.
381 float m_drawDepth;
383 // Debug layer name.
384 std::string m_debugName;
386 WebKit::WebFilterOperations m_filters;
387 WebKit::WebFilterOperations m_backgroundFilters;
388 skia::RefPtr<SkImageFilter> m_filter;
390 #ifndef NDEBUG
391 bool m_betweenWillDrawAndDidDraw;
392 #endif
394 // Rect indicating what was repainted/updated during update.
395 // Note that plugin layers bypass this and leave it empty.
396 // Uses layer's content space.
397 gfx::RectF m_updateRect;
399 // Manages animations for this layer.
400 scoped_refptr<LayerAnimationController> m_layerAnimationController;
402 // Manages scrollbars for this layer
403 scoped_ptr<ScrollbarAnimationController> m_scrollbarAnimationController;
405 // Group of properties that need to be computed based on the layer tree
406 // hierarchy before layers can be drawn.
407 DrawProperties<LayerImpl, RenderSurfaceImpl> m_drawProperties;
409 DISALLOW_COPY_AND_ASSIGN(LayerImpl);
414 #endif // CC_LAYER_IMPL_H_