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.
5 #ifndef CC_LAYERS_LAYER_H_
6 #define CC_LAYERS_LAYER_H_
11 #include "base/callback.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/observer_list.h"
14 #include "cc/animation/layer_animation_controller.h"
15 #include "cc/animation/layer_animation_value_observer.h"
16 #include "cc/animation/layer_animation_value_provider.h"
17 #include "cc/base/cc_export.h"
18 #include "cc/base/region.h"
19 #include "cc/base/scoped_ptr_vector.h"
20 #include "cc/debug/micro_benchmark.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/paint_properties.h"
25 #include "cc/layers/render_surface.h"
26 #include "cc/output/filter_operations.h"
27 #include "skia/ext/refptr.h"
28 #include "third_party/skia/include/core/SkColor.h"
29 #include "third_party/skia/include/core/SkImageFilter.h"
30 #include "third_party/skia/include/core/SkPicture.h"
31 #include "third_party/skia/include/core/SkXfermode.h"
32 #include "ui/gfx/rect.h"
33 #include "ui/gfx/rect_f.h"
34 #include "ui/gfx/transform.h"
42 class ConvertableToTraceFormat;
49 class AnimationDelegate;
50 struct AnimationEvent;
51 class CopyOutputRequest;
52 class LayerAnimationDelegate;
53 class LayerAnimationEventObserver;
58 class PriorityCalculator;
59 class RenderingStatsInstrumentation;
60 class ResourceUpdateQueue;
61 class ScrollbarLayerInterface;
62 struct AnimationEvent;
63 template <typename LayerType>
64 class OcclusionTracker;
66 // Base class for composited layers. Special layer types are derived from
68 class CC_EXPORT Layer : public base::RefCounted<Layer>,
69 public LayerAnimationValueObserver,
70 public LayerAnimationValueProvider {
72 typedef RenderSurfaceLayerList RenderSurfaceListType;
73 typedef LayerList LayerListType;
74 typedef RenderSurface RenderSurfaceType;
80 static scoped_refptr<Layer> Create();
82 int id() const { return layer_id_; }
85 Layer* parent() { return parent_; }
86 const Layer* parent() const { return parent_; }
87 void AddChild(scoped_refptr<Layer> child);
88 void InsertChild(scoped_refptr<Layer> child, size_t index);
89 void ReplaceChild(Layer* reference, scoped_refptr<Layer> new_layer);
90 void RemoveFromParent();
91 void RemoveAllChildren();
92 void SetChildren(const LayerList& children);
93 bool HasAncestor(const Layer* ancestor) const;
95 const LayerList& children() const { return children_; }
96 Layer* child_at(size_t index) { return children_[index].get(); }
98 // This requests the layer and its subtree be rendered and given to the
99 // callback. If the copy is unable to be produced (the layer is destroyed
100 // first), then the callback is called with a NULL/empty result.
101 void RequestCopyOfOutput(scoped_ptr<CopyOutputRequest> request);
102 bool HasCopyRequest() const {
103 return !copy_requests_.empty();
106 void SetAnchorPoint(const gfx::PointF& anchor_point);
107 gfx::PointF anchor_point() const { return anchor_point_; }
109 void SetAnchorPointZ(float anchor_point_z);
110 float anchor_point_z() const { return anchor_point_z_; }
112 virtual void SetBackgroundColor(SkColor background_color);
113 SkColor background_color() const { return background_color_; }
114 // If contents_opaque(), return an opaque color else return a
115 // non-opaque color. Tries to return background_color(), if possible.
116 SkColor SafeOpaqueBackgroundColor() const;
118 // A layer's bounds are in logical, non-page-scaled pixels (however, the
119 // root layer's bounds are in physical pixels).
120 void SetBounds(const gfx::Size& bounds);
121 gfx::Size bounds() const { return bounds_; }
123 void SetMasksToBounds(bool masks_to_bounds);
124 bool masks_to_bounds() const { return masks_to_bounds_; }
126 void SetMaskLayer(Layer* mask_layer);
127 Layer* mask_layer() { return mask_layer_.get(); }
128 const Layer* mask_layer() const { return mask_layer_.get(); }
130 virtual void SetNeedsDisplayRect(const gfx::RectF& dirty_rect);
131 void SetNeedsDisplay() { SetNeedsDisplayRect(gfx::RectF(bounds())); }
133 void SetOpacity(float opacity);
134 float opacity() const { return opacity_; }
135 bool OpacityIsAnimating() const;
136 virtual bool OpacityCanAnimateOnImplThread() const;
138 void SetBlendMode(SkXfermode::Mode blend_mode);
139 SkXfermode::Mode blend_mode() const { return blend_mode_; }
141 bool uses_default_blend_mode() const {
142 return blend_mode_ == SkXfermode::kSrcOver_Mode;
145 // A layer is root for an isolated group when it and all its descendants are
146 // drawn over a black and fully transparent background, creating an isolated
147 // group. It should be used along with SetBlendMode(), in order to restrict
148 // layers within the group to blend with layers outside this group.
149 void SetIsRootForIsolatedGroup(bool root);
150 bool is_root_for_isolated_group() const {
151 return is_root_for_isolated_group_;
154 void SetFilters(const FilterOperations& filters);
155 const FilterOperations& filters() const { return filters_; }
156 bool FilterIsAnimating() const;
158 // Background filters are filters applied to what is behind this layer, when
159 // they are viewed through non-opaque regions in this layer. They are used
160 // through the WebLayer interface, and are not exposed to HTML.
161 void SetBackgroundFilters(const FilterOperations& filters);
162 const FilterOperations& background_filters() const {
163 return background_filters_;
166 virtual void SetContentsOpaque(bool opaque);
167 bool contents_opaque() const { return contents_opaque_; }
169 void SetPosition(const gfx::PointF& position);
170 gfx::PointF position() const { return position_; }
172 void SetIsContainerForFixedPositionLayers(bool container);
173 bool IsContainerForFixedPositionLayers() const;
175 void SetPositionConstraint(const LayerPositionConstraint& constraint);
176 const LayerPositionConstraint& position_constraint() const {
177 return position_constraint_;
180 void SetTransform(const gfx::Transform& transform);
181 const gfx::Transform& transform() const { return transform_; }
182 bool TransformIsAnimating() const;
183 bool transform_is_invertible() const { return transform_is_invertible_; }
185 void SetScrollParent(Layer* parent);
187 Layer* scroll_parent() { return scroll_parent_; }
188 const Layer* scroll_parent() const { return scroll_parent_; }
190 void AddScrollChild(Layer* child);
191 void RemoveScrollChild(Layer* child);
193 std::set<Layer*>* scroll_children() { return scroll_children_.get(); }
194 const std::set<Layer*>* scroll_children() const {
195 return scroll_children_.get();
198 void SetClipParent(Layer* ancestor);
200 Layer* clip_parent() { return clip_parent_; }
201 const Layer* clip_parent() const {
205 void AddClipChild(Layer* child);
206 void RemoveClipChild(Layer* child);
208 std::set<Layer*>* clip_children() { return clip_children_.get(); }
209 const std::set<Layer*>* clip_children() const {
210 return clip_children_.get();
213 DrawProperties<Layer>& draw_properties() { return draw_properties_; }
214 const DrawProperties<Layer>& draw_properties() const {
215 return draw_properties_;
218 // The following are shortcut accessors to get various information from
220 const gfx::Transform& draw_transform() const {
221 return draw_properties_.target_space_transform;
223 const gfx::Transform& screen_space_transform() const {
224 return draw_properties_.screen_space_transform;
226 float draw_opacity() const { return draw_properties_.opacity; }
227 bool draw_opacity_is_animating() const {
228 return draw_properties_.opacity_is_animating;
230 bool draw_transform_is_animating() const {
231 return draw_properties_.target_space_transform_is_animating;
233 bool screen_space_transform_is_animating() const {
234 return draw_properties_.screen_space_transform_is_animating;
236 bool screen_space_opacity_is_animating() const {
237 return draw_properties_.screen_space_opacity_is_animating;
239 bool can_use_lcd_text() const { return draw_properties_.can_use_lcd_text; }
240 bool is_clipped() const { return draw_properties_.is_clipped; }
241 gfx::Rect clip_rect() const { return draw_properties_.clip_rect; }
242 gfx::Rect drawable_content_rect() const {
243 return draw_properties_.drawable_content_rect;
245 gfx::Rect visible_content_rect() const {
246 return draw_properties_.visible_content_rect;
248 Layer* render_target() {
249 DCHECK(!draw_properties_.render_target ||
250 draw_properties_.render_target->render_surface());
251 return draw_properties_.render_target;
253 const Layer* render_target() const {
254 DCHECK(!draw_properties_.render_target ||
255 draw_properties_.render_target->render_surface());
256 return draw_properties_.render_target;
258 RenderSurface* render_surface() const {
259 return draw_properties_.render_surface.get();
261 int num_unclipped_descendants() const {
262 return draw_properties_.num_unclipped_descendants;
265 void SetScrollOffset(gfx::Vector2d scroll_offset);
266 gfx::Vector2d scroll_offset() const { return scroll_offset_; }
267 void SetScrollOffsetFromImplSide(const gfx::Vector2d& scroll_offset);
269 void SetScrollClipLayerId(int clip_layer_id);
270 bool scrollable() const { return scroll_clip_layer_id_ != INVALID_ID; }
272 void SetUserScrollable(bool horizontal, bool vertical);
273 bool user_scrollable_horizontal() const {
274 return user_scrollable_horizontal_;
276 bool user_scrollable_vertical() const { return user_scrollable_vertical_; }
278 void SetShouldScrollOnMainThread(bool should_scroll_on_main_thread);
279 bool should_scroll_on_main_thread() const {
280 return should_scroll_on_main_thread_;
283 void SetHaveWheelEventHandlers(bool have_wheel_event_handlers);
284 bool have_wheel_event_handlers() const { return have_wheel_event_handlers_; }
286 void SetHaveScrollEventHandlers(bool have_scroll_event_handlers);
287 bool have_scroll_event_handlers() const {
288 return have_scroll_event_handlers_;
291 void SetNonFastScrollableRegion(const Region& non_fast_scrollable_region);
292 const Region& non_fast_scrollable_region() const {
293 return non_fast_scrollable_region_;
296 void SetTouchEventHandlerRegion(const Region& touch_event_handler_region);
297 const Region& touch_event_handler_region() const {
298 return touch_event_handler_region_;
301 void set_did_scroll_callback(const base::Closure& callback) {
302 did_scroll_callback_ = callback;
305 void SetDrawCheckerboardForMissingTiles(bool checkerboard);
306 bool draw_checkerboard_for_missing_tiles() const {
307 return draw_checkerboard_for_missing_tiles_;
310 void SetForceRenderSurface(bool force_render_surface);
311 bool force_render_surface() const { return force_render_surface_; }
313 gfx::Vector2d ScrollDelta() const { return gfx::Vector2d(); }
314 gfx::Vector2dF TotalScrollOffset() const {
315 // Floating point to match the LayerImpl version.
316 return scroll_offset() + ScrollDelta();
319 void SetDoubleSided(bool double_sided);
320 bool double_sided() const { return double_sided_; }
322 void SetShouldFlattenTransform(bool flatten);
323 bool should_flatten_transform() const { return should_flatten_transform_; }
325 void SetIs3dSorted(bool sorted);
326 bool is_3d_sorted() const { return is_3d_sorted_; }
328 void set_use_parent_backface_visibility(bool use) {
329 use_parent_backface_visibility_ = use;
331 bool use_parent_backface_visibility() const {
332 return use_parent_backface_visibility_;
335 virtual void SetLayerTreeHost(LayerTreeHost* host);
337 bool HasDelegatedContent() const { return false; }
338 bool HasContributingDelegatedRenderPasses() const { return false; }
340 void SetIsDrawable(bool is_drawable);
342 void SetHideLayerAndSubtree(bool hide);
343 bool hide_layer_and_subtree() const { return hide_layer_and_subtree_; }
345 void SetReplicaLayer(Layer* layer);
346 Layer* replica_layer() { return replica_layer_.get(); }
347 const Layer* replica_layer() const { return replica_layer_.get(); }
349 bool has_mask() const { return !!mask_layer_.get(); }
350 bool has_replica() const { return !!replica_layer_.get(); }
351 bool replica_has_mask() const {
352 return replica_layer_.get() &&
353 (mask_layer_.get() || replica_layer_->mask_layer_.get());
356 // These methods typically need to be overwritten by derived classes.
357 virtual bool DrawsContent() const;
358 virtual void SavePaintProperties();
359 // Returns true iff any resources were updated that need to be committed.
360 virtual bool Update(ResourceUpdateQueue* queue,
361 const OcclusionTracker<Layer>* occlusion);
362 virtual bool NeedMoreUpdates();
363 virtual void SetIsMask(bool is_mask) {}
364 virtual void ReduceMemoryUsage() {}
365 virtual void OnOutputSurfaceCreated() {}
366 virtual bool IsSuitableForGpuRasterization() const;
368 virtual scoped_refptr<base::debug::ConvertableToTraceFormat> TakeDebugInfo();
370 void SetLayerClient(LayerClient* client) { client_ = client; }
372 virtual void PushPropertiesTo(LayerImpl* layer);
374 void CreateRenderSurface();
375 void ClearRenderSurface();
376 void ClearRenderSurfaceLayerList();
378 // The contents scale converts from logical, non-page-scaled pixels to target
379 // pixels. The contents scale is 1 for the root layer as it is already in
380 // physical pixels. By default contents scale is forced to be 1 except for
381 // subclasses of ContentsScalingLayer.
382 float contents_scale_x() const { return draw_properties_.contents_scale_x; }
383 float contents_scale_y() const { return draw_properties_.contents_scale_y; }
384 gfx::Size content_bounds() const { return draw_properties_.content_bounds; }
386 virtual void CalculateContentsScale(float ideal_contents_scale,
387 float device_scale_factor,
388 float page_scale_factor,
389 float maximum_animation_contents_scale,
390 bool animating_transform_to_screen,
391 float* contents_scale_x,
392 float* contents_scale_y,
393 gfx::Size* content_bounds);
395 LayerTreeHost* layer_tree_host() { return layer_tree_host_; }
396 const LayerTreeHost* layer_tree_host() const { return layer_tree_host_; }
398 // Set the priority of all desired textures in this layer.
399 virtual void SetTexturePriorities(const PriorityCalculator& priority_calc) {}
401 bool AddAnimation(scoped_ptr<Animation> animation);
402 void PauseAnimation(int animation_id, double time_offset);
403 void RemoveAnimation(int animation_id);
405 LayerAnimationController* layer_animation_controller() {
406 return layer_animation_controller_.get();
408 void SetLayerAnimationControllerForTest(
409 scoped_refptr<LayerAnimationController> controller);
411 void set_layer_animation_delegate(AnimationDelegate* delegate) {
412 layer_animation_controller_->set_layer_animation_delegate(delegate);
415 bool HasActiveAnimation() const;
417 void AddLayerAnimationEventObserver(
418 LayerAnimationEventObserver* animation_observer);
419 void RemoveLayerAnimationEventObserver(
420 LayerAnimationEventObserver* animation_observer);
422 virtual Region VisibleContentOpaqueRegion() const;
424 virtual ScrollbarLayerInterface* ToScrollbarLayer();
426 gfx::Rect LayerRectToContentRect(const gfx::RectF& layer_rect) const;
428 virtual skia::RefPtr<SkPicture> GetPicture() const;
430 // Constructs a LayerImpl of the correct runtime type for this Layer type.
431 virtual scoped_ptr<LayerImpl> CreateLayerImpl(LayerTreeImpl* tree_impl);
433 bool NeedsDisplayForTesting() const { return !update_rect_.IsEmpty(); }
434 void ResetNeedsDisplayForTesting() { update_rect_ = gfx::RectF(); }
436 RenderingStatsInstrumentation* rendering_stats_instrumentation() const;
438 const PaintProperties& paint_properties() const {
439 return paint_properties_;
442 // The scale at which contents should be rastered, to match the scale at
443 // which they will drawn to the screen. This scale is a component of the
444 // contents scale but does not include page/device scale factors.
445 // TODO(danakj): This goes away when TiledLayer goes away.
446 void set_raster_scale(float scale) { raster_scale_ = scale; }
447 float raster_scale() const { return raster_scale_; }
448 bool raster_scale_is_unknown() const { return raster_scale_ == 0.f; }
450 virtual bool SupportsLCDText() const;
452 void SetNeedsPushProperties();
453 bool needs_push_properties() const { return needs_push_properties_; }
454 bool descendant_needs_push_properties() const {
455 return num_dependents_need_push_properties_ > 0;
457 void reset_needs_push_properties_for_testing() {
458 needs_push_properties_ = false;
461 virtual void RunMicroBenchmark(MicroBenchmark* benchmark);
464 friend class LayerImpl;
465 friend class TreeSynchronizer;
470 // These SetNeeds functions are in order of severity of update:
472 // Called when this layer has been modified in some way, but isn't sure
473 // that it needs a commit yet. It needs CalcDrawProperties and UpdateLayers
474 // before it knows whether or not a commit is required.
475 void SetNeedsUpdate();
476 // Called when a property has been modified in a way that the layer
477 // knows immediately that a commit is required. This implies SetNeedsUpdate
478 // as well as SetNeedsPushProperties to push that property.
479 void SetNeedsCommit();
480 // Called when there's been a change in layer structure. Implies both
481 // SetNeedsUpdate and SetNeedsCommit, but not SetNeedsPushProperties.
482 void SetNeedsFullTreeSync();
484 // Called when the next commit should wait until the pending tree is activated
485 // before finishing the commit and unblocking the main thread. Used to ensure
486 // unused resources on the impl thread are returned before commit completes.
487 void SetNextCommitWaitsForActivation();
489 void AddDependentNeedsPushProperties();
490 void RemoveDependentNeedsPushProperties();
491 bool parent_should_know_need_push_properties() const {
492 return needs_push_properties() || descendant_needs_push_properties();
495 bool IsPropertyChangeAllowed() const;
497 // If this layer has a scroll parent, it removes |this| from its list of
499 void RemoveFromScrollTree();
501 // If this layer has a clip parent, it removes |this| from its list of clip
503 void RemoveFromClipTree();
505 void reset_raster_scale_to_unknown() { raster_scale_ = 0.f; }
507 // This flag is set when the layer needs to push properties to the impl
509 bool needs_push_properties_;
511 // The number of direct children or dependent layers that need to be recursed
512 // to in order for them or a descendent of them to push properties to the impl
514 int num_dependents_need_push_properties_;
516 // Tracks whether this layer may have changed stacking order with its
518 bool stacking_order_changed_;
520 // The update rect is the region of the compositor resource that was
521 // actually updated by the compositor. For layers that may do updating
522 // outside the compositor's control (i.e. plugin layers), this information
523 // is not available and the update rect will remain empty.
524 // Note this rect is in layer space (not content space).
525 gfx::RectF update_rect_;
527 scoped_refptr<Layer> mask_layer_;
531 // When true, the layer is about to perform an update. Any commit requests
532 // will be handled implicitly after the update completes.
533 bool ignore_set_needs_commit_;
536 friend class base::RefCounted<Layer>;
538 void SetParent(Layer* layer);
539 bool DescendantIsFixedToContainerLayer() const;
541 // Returns the index of the child or -1 if not found.
542 int IndexOfChild(const Layer* reference);
544 // This should only be called from RemoveFromParent().
545 void RemoveChildOrDependent(Layer* child);
547 // LayerAnimationValueProvider implementation.
548 virtual gfx::Vector2dF ScrollOffsetForAnimation() const OVERRIDE;
550 // LayerAnimationValueObserver implementation.
551 virtual void OnFilterAnimated(const FilterOperations& filters) OVERRIDE;
552 virtual void OnOpacityAnimated(float opacity) OVERRIDE;
553 virtual void OnTransformAnimated(const gfx::Transform& transform) OVERRIDE;
554 virtual void OnScrollOffsetAnimated(
555 const gfx::Vector2dF& scroll_offset) OVERRIDE;
556 virtual void OnAnimationWaitingForDeletion() OVERRIDE;
557 virtual bool IsActive() const OVERRIDE;
562 // Layer instances have a weak pointer to their LayerTreeHost.
563 // This pointer value is nil when a Layer is not in a tree and is
564 // updated via SetLayerTreeHost() if a layer moves between trees.
565 LayerTreeHost* layer_tree_host_;
567 scoped_refptr<LayerAnimationController> layer_animation_controller_;
572 gfx::Vector2d scroll_offset_;
573 // This variable indicates which ancestor layer (if any) whose size,
574 // transformed relative to this layer, defines the maximum scroll offset for
576 int scroll_clip_layer_id_;
577 bool should_scroll_on_main_thread_ : 1;
578 bool have_wheel_event_handlers_ : 1;
579 bool have_scroll_event_handlers_ : 1;
580 bool user_scrollable_horizontal_ : 1;
581 bool user_scrollable_vertical_ : 1;
582 bool is_root_for_isolated_group_ : 1;
583 bool is_container_for_fixed_position_layers_ : 1;
584 bool is_drawable_ : 1;
585 bool hide_layer_and_subtree_ : 1;
586 bool masks_to_bounds_ : 1;
587 bool contents_opaque_ : 1;
588 bool double_sided_ : 1;
589 bool should_flatten_transform_ : 1;
590 bool use_parent_backface_visibility_ : 1;
591 bool draw_checkerboard_for_missing_tiles_ : 1;
592 bool force_render_surface_ : 1;
593 bool is_3d_sorted_ : 1;
594 bool transform_is_invertible_ : 1;
595 Region non_fast_scrollable_region_;
596 Region touch_event_handler_region_;
597 gfx::PointF position_;
598 gfx::PointF anchor_point_;
599 SkColor background_color_;
601 SkXfermode::Mode blend_mode_;
602 FilterOperations filters_;
603 FilterOperations background_filters_;
604 float anchor_point_z_;
605 LayerPositionConstraint position_constraint_;
606 Layer* scroll_parent_;
607 scoped_ptr<std::set<Layer*> > scroll_children_;
610 scoped_ptr<std::set<Layer*> > clip_children_;
612 gfx::Transform transform_;
614 // Replica layer used for reflections.
615 scoped_refptr<Layer> replica_layer_;
617 // Transient properties.
620 LayerClient* client_;
622 ScopedPtrVector<CopyOutputRequest> copy_requests_;
624 base::Closure did_scroll_callback_;
626 DrawProperties<Layer> draw_properties_;
628 PaintProperties paint_properties_;
630 DISALLOW_COPY_AND_ASSIGN(Layer);
635 #endif // CC_LAYERS_LAYER_H_