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_LAYERS_LAYER_IMPL_H_
6 #define CC_LAYERS_LAYER_IMPL_H_
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/values.h"
14 #include "cc/animation/layer_animation_controller.h"
15 #include "cc/animation/layer_animation_value_observer.h"
16 #include "cc/base/cc_export.h"
17 #include "cc/base/region.h"
18 #include "cc/base/scoped_ptr_vector.h"
19 #include "cc/input/input_handler.h"
20 #include "cc/layers/compositing_reasons.h"
21 #include "cc/layers/draw_properties.h"
22 #include "cc/layers/layer_lists.h"
23 #include "cc/layers/layer_position_constraint.h"
24 #include "cc/layers/render_surface_impl.h"
25 #include "cc/output/filter_operations.h"
26 #include "cc/quads/render_pass.h"
27 #include "cc/quads/shared_quad_state.h"
28 #include "cc/resources/resource_provider.h"
29 #include "skia/ext/refptr.h"
30 #include "third_party/skia/include/core/SkColor.h"
31 #include "third_party/skia/include/core/SkImageFilter.h"
32 #include "third_party/skia/include/core/SkPicture.h"
33 #include "ui/gfx/rect.h"
34 #include "ui/gfx/rect_f.h"
35 #include "ui/gfx/transform.h"
38 class DictionaryValue;
43 class LayerTreeHostImpl;
47 class ScrollbarAnimationController;
48 class ScrollbarLayerImplBase;
51 struct AppendQuadsData;
57 DRAW_MODE_RESOURCELESS_SOFTWARE
60 class CC_EXPORT LayerImpl : LayerAnimationValueObserver {
62 typedef LayerImplList RenderSurfaceListType;
63 typedef LayerImplList LayerListType;
64 typedef RenderSurfaceImpl RenderSurfaceType;
66 static scoped_ptr<LayerImpl> Create(LayerTreeImpl* tree_impl, int id) {
67 return make_scoped_ptr(new LayerImpl(tree_impl, id));
72 int id() const { return layer_id_; }
74 // LayerAnimationValueObserver implementation.
75 virtual void OnFilterAnimated(const FilterOperations& filters) OVERRIDE;
76 virtual void OnOpacityAnimated(float opacity) OVERRIDE;
77 virtual void OnTransformAnimated(const gfx::Transform& transform) OVERRIDE;
78 virtual bool IsActive() const OVERRIDE;
81 LayerImpl* parent() { return parent_; }
82 const LayerImpl* parent() const { return parent_; }
83 const OwnedLayerImplList& children() const { return children_; }
84 OwnedLayerImplList& children() { return children_; }
85 LayerImpl* child_at(size_t index) const { return children_[index]; }
86 void AddChild(scoped_ptr<LayerImpl> child);
87 scoped_ptr<LayerImpl> RemoveChild(LayerImpl* child);
88 void set_parent(LayerImpl* parent) { parent_ = parent; }
89 // Warning: This does not preserve tree structure invariants.
90 void ClearChildList();
92 bool HasAncestor(const LayerImpl* ancestor) const;
94 void SetScrollParent(LayerImpl* parent);
96 LayerImpl* scroll_parent() { return scroll_parent_; }
97 const LayerImpl* scroll_parent() const { return scroll_parent_; }
99 void SetScrollChildren(std::set<LayerImpl*>* children);
100 void RemoveScrollChild(LayerImpl* child);
102 std::set<LayerImpl*>* scroll_children() { return scroll_children_.get(); }
103 const std::set<LayerImpl*>* scroll_children() const {
104 return scroll_children_.get();
107 void SetClipParent(LayerImpl* ancestor);
109 LayerImpl* clip_parent() {
112 const LayerImpl* clip_parent() const {
116 void SetClipChildren(std::set<LayerImpl*>* children);
117 void RemoveClipChild(LayerImpl* child);
119 std::set<LayerImpl*>* clip_children() { return clip_children_.get(); }
120 const std::set<LayerImpl*>* clip_children() const {
121 return clip_children_.get();
124 void PassCopyRequests(ScopedPtrVector<CopyOutputRequest>* requests);
125 void TakeCopyRequestsAndTransformToTarget(
126 ScopedPtrVector<CopyOutputRequest>* request);
127 bool HasCopyRequest() const { return !copy_requests_.empty(); }
129 void SetMaskLayer(scoped_ptr<LayerImpl> mask_layer);
130 LayerImpl* mask_layer() { return mask_layer_.get(); }
131 const LayerImpl* mask_layer() const { return mask_layer_.get(); }
132 scoped_ptr<LayerImpl> TakeMaskLayer();
134 void SetReplicaLayer(scoped_ptr<LayerImpl> replica_layer);
135 LayerImpl* replica_layer() { return replica_layer_.get(); }
136 const LayerImpl* replica_layer() const { return replica_layer_.get(); }
137 scoped_ptr<LayerImpl> TakeReplicaLayer();
139 bool has_mask() const { return mask_layer_; }
140 bool has_replica() const { return replica_layer_; }
141 bool replica_has_mask() const {
142 return replica_layer_ && (mask_layer_ || replica_layer_->mask_layer_);
145 LayerTreeImpl* layer_tree_impl() const { return layer_tree_impl_; }
147 scoped_ptr<SharedQuadState> CreateSharedQuadState() const;
148 // WillDraw must be called before AppendQuads. If WillDraw returns false,
149 // AppendQuads and DidDraw will not be called. If WillDraw returns true,
150 // DidDraw is guaranteed to be called before another WillDraw or before
151 // the layer is destroyed. To enforce this, any class that overrides
152 // WillDraw/DidDraw must call the base class version only if WillDraw
154 virtual bool WillDraw(DrawMode draw_mode,
155 ResourceProvider* resource_provider);
156 virtual void AppendQuads(QuadSink* quad_sink,
157 AppendQuadsData* append_quads_data) {}
158 virtual void DidDraw(ResourceProvider* resource_provider);
160 virtual ResourceProvider::ResourceId ContentsResourceId() const;
162 virtual bool HasDelegatedContent() const;
163 virtual bool HasContributingDelegatedRenderPasses() const;
164 virtual RenderPass::Id FirstContributingRenderPassId() const;
165 virtual RenderPass::Id NextContributingRenderPassId(RenderPass::Id id) const;
167 virtual void UpdateTilePriorities() {}
169 virtual ScrollbarLayerImplBase* ToScrollbarLayer();
171 // Returns true if this layer has content to draw.
172 void SetDrawsContent(bool draws_content);
173 bool DrawsContent() const { return draws_content_; }
175 void SetHideLayerAndSubtree(bool hide);
176 bool hide_layer_and_subtree() const { return hide_layer_and_subtree_; }
178 bool force_render_surface() const { return force_render_surface_; }
179 void SetForceRenderSurface(bool force) { force_render_surface_ = force; }
181 void SetAnchorPoint(gfx::PointF anchor_point);
182 gfx::PointF anchor_point() const { return anchor_point_; }
184 void SetAnchorPointZ(float anchor_point_z);
185 float anchor_point_z() const { return anchor_point_z_; }
187 void SetBackgroundColor(SkColor background_color);
188 SkColor background_color() const { return background_color_; }
189 // If contents_opaque(), return an opaque color else return a
190 // non-opaque color. Tries to return background_color(), if possible.
191 SkColor SafeOpaqueBackgroundColor() const;
193 void SetFilters(const FilterOperations& filters);
194 const FilterOperations& filters() const { return filters_; }
195 bool FilterIsAnimating() const;
196 bool FilterIsAnimatingOnImplOnly() const;
198 void SetBackgroundFilters(const FilterOperations& filters);
199 const FilterOperations& background_filters() const {
200 return background_filters_;
203 void SetMasksToBounds(bool masks_to_bounds);
204 bool masks_to_bounds() const { return masks_to_bounds_; }
206 void SetContentsOpaque(bool opaque);
207 bool contents_opaque() const { return contents_opaque_; }
209 void SetOpacity(float opacity);
210 float opacity() const { return opacity_; }
211 bool OpacityIsAnimating() const;
212 bool OpacityIsAnimatingOnImplOnly() const;
214 void SetPosition(gfx::PointF position);
215 gfx::PointF position() const { return position_; }
217 void SetIsContainerForFixedPositionLayers(bool container) {
218 is_container_for_fixed_position_layers_ = container;
220 // This is a non-trivial function in Layer.
221 bool IsContainerForFixedPositionLayers() const {
222 return is_container_for_fixed_position_layers_;
225 void SetFixedContainerSizeDelta(gfx::Vector2dF delta) {
226 fixed_container_size_delta_ = delta;
228 gfx::Vector2dF fixed_container_size_delta() const {
229 return fixed_container_size_delta_;
232 void SetPositionConstraint(const LayerPositionConstraint& constraint) {
233 position_constraint_ = constraint;
235 const LayerPositionConstraint& position_constraint() const {
236 return position_constraint_;
239 void SetPreserves3d(bool preserves_3d);
240 bool preserves_3d() const { return preserves_3d_; }
242 void SetUseParentBackfaceVisibility(bool use) {
243 use_parent_backface_visibility_ = use;
245 bool use_parent_backface_visibility() const {
246 return use_parent_backface_visibility_;
249 void SetSublayerTransform(const gfx::Transform& sublayer_transform);
250 const gfx::Transform& sublayer_transform() const {
251 return sublayer_transform_;
255 void SetDebugName(const std::string& debug_name) { debug_name_ = debug_name; }
256 std::string debug_name() const { return debug_name_; }
258 void SetCompositingReasons(CompositingReasons reasons) {
259 compositing_reasons_ = reasons;
262 CompositingReasons compositing_reasons() const {
263 return compositing_reasons_;
266 bool ShowDebugBorders() const;
268 // These invalidate the host's render surface layer list. The caller
269 // is responsible for calling set_needs_update_draw_properties on the tree
270 // so that its list can be recreated.
271 void CreateRenderSurface();
272 void ClearRenderSurface();
274 DrawProperties<LayerImpl>& draw_properties() {
275 return draw_properties_;
277 const DrawProperties<LayerImpl>& draw_properties() const {
278 return draw_properties_;
281 // The following are shortcut accessors to get various information from
283 const gfx::Transform& draw_transform() const {
284 return draw_properties_.target_space_transform;
286 const gfx::Transform& screen_space_transform() const {
287 return draw_properties_.screen_space_transform;
289 float draw_opacity() const { return draw_properties_.opacity; }
290 bool draw_opacity_is_animating() const {
291 return draw_properties_.opacity_is_animating;
293 bool draw_transform_is_animating() const {
294 return draw_properties_.target_space_transform_is_animating;
296 bool screen_space_transform_is_animating() const {
297 return draw_properties_.screen_space_transform_is_animating;
299 bool screen_space_opacity_is_animating() const {
300 return draw_properties_.screen_space_opacity_is_animating;
302 bool can_use_lcd_text() const { return draw_properties_.can_use_lcd_text; }
303 bool is_clipped() const { return draw_properties_.is_clipped; }
304 gfx::Rect clip_rect() const { return draw_properties_.clip_rect; }
305 gfx::Rect drawable_content_rect() const {
306 return draw_properties_.drawable_content_rect;
308 gfx::Rect visible_content_rect() const {
309 return draw_properties_.visible_content_rect;
311 LayerImpl* render_target() {
312 DCHECK(!draw_properties_.render_target ||
313 draw_properties_.render_target->render_surface());
314 return draw_properties_.render_target;
316 const LayerImpl* render_target() const {
317 DCHECK(!draw_properties_.render_target ||
318 draw_properties_.render_target->render_surface());
319 return draw_properties_.render_target;
321 RenderSurfaceImpl* render_surface() const {
322 return draw_properties_.render_surface.get();
324 int num_unclipped_descendants() const {
325 return draw_properties_.num_unclipped_descendants;
328 // The client should be responsible for setting bounds, content bounds and
329 // contents scale to appropriate values. LayerImpl doesn't calculate any of
330 // them from the other values.
332 void SetBounds(gfx::Size bounds);
333 gfx::Size bounds() const { return bounds_; }
335 void SetContentBounds(gfx::Size content_bounds);
336 gfx::Size content_bounds() const { return draw_properties_.content_bounds; }
338 float contents_scale_x() const { return draw_properties_.contents_scale_x; }
339 float contents_scale_y() const { return draw_properties_.contents_scale_y; }
340 void SetContentsScale(float contents_scale_x, float contents_scale_y);
342 virtual void CalculateContentsScale(float ideal_contents_scale,
343 float device_scale_factor,
344 float page_scale_factor,
345 bool animating_transform_to_screen,
346 float* contents_scale_x,
347 float* contents_scale_y,
348 gfx::Size* content_bounds);
350 void SetScrollOffsetDelegate(
351 LayerScrollOffsetDelegate* scroll_offset_delegate);
352 bool IsExternalFlingActive() const;
354 void SetScrollOffset(gfx::Vector2d scroll_offset);
355 void SetScrollOffsetAndDelta(gfx::Vector2d scroll_offset,
356 gfx::Vector2dF scroll_delta);
357 gfx::Vector2d scroll_offset() const { return scroll_offset_; }
359 void SetMaxScrollOffset(gfx::Vector2d max_scroll_offset);
360 gfx::Vector2d max_scroll_offset() const { return max_scroll_offset_; }
362 void SetScrollDelta(gfx::Vector2dF scroll_delta);
363 gfx::Vector2dF ScrollDelta() const;
365 gfx::Vector2dF TotalScrollOffset() const;
367 void SetSentScrollDelta(gfx::Vector2d sent_scroll_delta);
368 gfx::Vector2d sent_scroll_delta() const { return sent_scroll_delta_; }
370 // Returns the delta of the scroll that was outside of the bounds of the
372 gfx::Vector2dF ScrollBy(gfx::Vector2dF scroll);
374 void SetScrollable(bool scrollable) { scrollable_ = scrollable; }
375 bool scrollable() const { return scrollable_; }
377 void set_user_scrollable_horizontal(bool scrollable) {
378 user_scrollable_horizontal_ = scrollable;
380 void set_user_scrollable_vertical(bool scrollable) {
381 user_scrollable_vertical_ = scrollable;
384 void ApplySentScrollDeltasFromAbortedCommit();
385 void ApplyScrollDeltasSinceBeginMainFrame();
387 void SetShouldScrollOnMainThread(bool should_scroll_on_main_thread) {
388 should_scroll_on_main_thread_ = should_scroll_on_main_thread;
390 bool should_scroll_on_main_thread() const {
391 return should_scroll_on_main_thread_;
394 void SetHaveWheelEventHandlers(bool have_wheel_event_handlers) {
395 have_wheel_event_handlers_ = have_wheel_event_handlers;
397 bool have_wheel_event_handlers() const { return have_wheel_event_handlers_; }
399 void SetNonFastScrollableRegion(const Region& region) {
400 non_fast_scrollable_region_ = region;
402 const Region& non_fast_scrollable_region() const {
403 return non_fast_scrollable_region_;
406 void SetTouchEventHandlerRegion(const Region& region) {
407 touch_event_handler_region_ = region;
409 const Region& touch_event_handler_region() const {
410 return touch_event_handler_region_;
413 void SetDrawCheckerboardForMissingTiles(bool checkerboard) {
414 draw_checkerboard_for_missing_tiles_ = checkerboard;
416 bool DrawCheckerboardForMissingTiles() const;
418 InputHandler::ScrollStatus TryScroll(
419 gfx::PointF screen_space_point,
420 InputHandler::ScrollInputType type) const;
422 void SetDoubleSided(bool double_sided);
423 bool double_sided() const { return double_sided_; }
425 void SetTransform(const gfx::Transform& transform);
426 const gfx::Transform& transform() const { return transform_; }
427 bool TransformIsAnimating() const;
428 bool TransformIsAnimatingOnImplOnly() const;
430 // Note this rect is in layer space (not content space).
431 void set_update_rect(const gfx::RectF& update_rect) {
432 update_rect_ = update_rect;
434 const gfx::RectF& update_rect() const { return update_rect_; }
436 virtual base::DictionaryValue* LayerTreeAsJson() const;
438 void SetStackingOrderChanged(bool stacking_order_changed);
440 bool LayerPropertyChanged() const {
441 return layer_property_changed_ || LayerIsAlwaysDamaged();
444 void ResetAllChangeTrackingForSubtree();
446 virtual bool LayerIsAlwaysDamaged() const;
448 LayerAnimationController* layer_animation_controller() {
449 return layer_animation_controller_.get();
452 virtual Region VisibleContentOpaqueRegion() const;
454 virtual void DidBecomeActive();
456 virtual void DidBeginTracing();
458 // Indicates that the surface previously used to render this layer
459 // was lost and that a new one has been created. Won't be called
460 // until the new surface has been created successfully.
461 virtual void DidLoseOutputSurface();
463 ScrollbarAnimationController* scrollbar_animation_controller() const {
464 return scrollbar_animation_controller_.get();
467 void SetHorizontalScrollbarLayer(ScrollbarLayerImplBase* scrollbar_layer);
468 ScrollbarLayerImplBase* horizontal_scrollbar_layer() {
469 return horizontal_scrollbar_layer_;
472 void SetVerticalScrollbarLayer(ScrollbarLayerImplBase* scrollbar_layer);
473 ScrollbarLayerImplBase* vertical_scrollbar_layer() {
474 return vertical_scrollbar_layer_;
477 gfx::Rect LayerRectToContentRect(const gfx::RectF& layer_rect) const;
479 virtual skia::RefPtr<SkPicture> GetPicture();
481 virtual bool AreVisibleResourcesReady() const;
483 virtual scoped_ptr<LayerImpl> CreateLayerImpl(LayerTreeImpl* tree_impl);
484 virtual void PushPropertiesTo(LayerImpl* layer);
486 scoped_ptr<base::Value> AsValue() const;
487 virtual size_t GPUMemoryUsageInBytes() const;
489 // TODO(danakj): Be true only if needed. crbug.com/259511
490 bool needs_push_properties() const { return true; }
491 bool descendant_needs_push_properties() const { return true; }
494 LayerImpl(LayerTreeImpl* layer_impl, int id);
496 // Get the color and size of the layer's debug border.
497 virtual void GetDebugBorderProperties(SkColor* color, float* width) const;
499 void AppendDebugBorderQuad(QuadSink* quad_sink,
500 const SharedQuadState* shared_quad_state,
501 AppendQuadsData* append_quads_data) const;
502 void AppendDebugBorderQuad(QuadSink* quad_sink,
503 const SharedQuadState* shared_quad_state,
504 AppendQuadsData* append_quads_data,
508 virtual void AsValueInto(base::DictionaryValue* dict) const;
510 void NoteLayerPropertyChanged();
511 void NoteLayerPropertyChangedForSubtree();
513 // Note carefully this does not affect the current layer.
514 void NoteLayerPropertyChangedForDescendants();
517 void UpdateScrollbarPositions();
519 virtual const char* LayerTypeAsString() const;
521 // Properties internal to LayerImpl
523 OwnedLayerImplList children_;
525 LayerImpl* scroll_parent_;
527 // Storing a pointer to a set rather than a set since this will be rarely
528 // used. If this pointer turns out to be too heavy, we could have this (and
529 // the scroll parent above) be stored in a LayerImpl -> scroll_info
531 scoped_ptr<std::set<LayerImpl*> > scroll_children_;
533 LayerImpl* clip_parent_;
534 scoped_ptr<std::set<LayerImpl*> > clip_children_;
536 // mask_layer_ can be temporarily stolen during tree sync, we need this ID to
537 // confirm newly assigned layer is still the previous one
539 scoped_ptr<LayerImpl> mask_layer_;
540 int replica_layer_id_; // ditto
541 scoped_ptr<LayerImpl> replica_layer_;
543 LayerTreeImpl* layer_tree_impl_;
545 // Properties synchronized from the associated Layer.
546 gfx::PointF anchor_point_;
547 float anchor_point_z_;
549 gfx::Vector2d scroll_offset_;
550 LayerScrollOffsetDelegate* scroll_offset_delegate_;
552 bool should_scroll_on_main_thread_;
553 bool have_wheel_event_handlers_;
554 bool user_scrollable_horizontal_;
555 bool user_scrollable_vertical_;
556 Region non_fast_scrollable_region_;
557 Region touch_event_handler_region_;
558 SkColor background_color_;
559 bool stacking_order_changed_;
561 // Whether the "back" of this layer should draw.
564 // Tracks if drawing-related properties have changed since last redraw.
565 bool layer_property_changed_;
567 bool masks_to_bounds_;
568 bool contents_opaque_;
570 gfx::PointF position_;
572 bool use_parent_backface_visibility_;
573 bool draw_checkerboard_for_missing_tiles_;
574 gfx::Transform sublayer_transform_;
575 gfx::Transform transform_;
578 bool hide_layer_and_subtree_;
579 bool force_render_surface_;
581 // Set for the layer that other layers are fixed to.
582 bool is_container_for_fixed_position_layers_;
583 // This property is effective when
584 // is_container_for_fixed_position_layers_ == true,
585 gfx::Vector2dF fixed_container_size_delta_;
587 LayerPositionConstraint position_constraint_;
589 gfx::Vector2dF scroll_delta_;
590 gfx::Vector2d sent_scroll_delta_;
591 gfx::Vector2d max_scroll_offset_;
592 gfx::Vector2dF last_scroll_offset_;
594 // The global depth value of the center of the layer. This value is used
595 // to sort layers from back to front.
599 std::string debug_name_;
600 CompositingReasons compositing_reasons_;
602 FilterOperations filters_;
603 FilterOperations background_filters_;
606 DrawMode current_draw_mode_;
609 // Rect indicating what was repainted/updated during update.
610 // Note that plugin layers bypass this and leave it empty.
611 // Uses layer's content space.
612 gfx::RectF update_rect_;
614 // Manages animations for this layer.
615 scoped_refptr<LayerAnimationController> layer_animation_controller_;
617 // Manages scrollbars for this layer
618 scoped_ptr<ScrollbarAnimationController> scrollbar_animation_controller_;
620 // Weak pointers to this layer's scrollbars, if it has them. Updated during
621 // tree synchronization.
622 ScrollbarLayerImplBase* horizontal_scrollbar_layer_;
623 ScrollbarLayerImplBase* vertical_scrollbar_layer_;
625 ScopedPtrVector<CopyOutputRequest> copy_requests_;
627 // Group of properties that need to be computed based on the layer tree
628 // hierarchy before layers can be drawn.
629 DrawProperties<LayerImpl> draw_properties_;
631 DISALLOW_COPY_AND_ASSIGN(LayerImpl);
636 #endif // CC_LAYERS_LAYER_IMPL_H_