layer.h revision 1e9bf3e0803691d0a228da41fc608347b6db4340
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. 4 5#ifndef CC_LAYERS_LAYER_H_ 6#define CC_LAYERS_LAYER_H_ 7 8#include <set> 9#include <string> 10 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/base/cc_export.h" 17#include "cc/base/region.h" 18#include "cc/base/scoped_ptr_vector.h" 19#include "cc/debug/micro_benchmark.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/paint_properties.h" 25#include "cc/layers/render_surface.h" 26#include "cc/output/filter_operations.h" 27#include "cc/trees/occlusion_tracker.h" 28#include "skia/ext/refptr.h" 29#include "third_party/skia/include/core/SkColor.h" 30#include "third_party/skia/include/core/SkImageFilter.h" 31#include "third_party/skia/include/core/SkPicture.h" 32#include "ui/gfx/rect.h" 33#include "ui/gfx/rect_f.h" 34#include "ui/gfx/transform.h" 35 36namespace gfx { 37class BoxF; 38} 39 40namespace cc { 41 42class Animation; 43class AnimationDelegate; 44struct AnimationEvent; 45class CopyOutputRequest; 46class LayerAnimationDelegate; 47class LayerAnimationEventObserver; 48class LayerClient; 49class LayerImpl; 50class LayerTreeHost; 51class LayerTreeImpl; 52class PriorityCalculator; 53class RenderingStatsInstrumentation; 54class ResourceUpdateQueue; 55class ScrollbarLayerInterface; 56struct AnimationEvent; 57 58// Base class for composited layers. Special layer types are derived from 59// this class. 60class CC_EXPORT Layer : public base::RefCounted<Layer>, 61 public LayerAnimationValueObserver { 62 public: 63 typedef RenderSurfaceLayerList RenderSurfaceListType; 64 typedef LayerList LayerListType; 65 typedef RenderSurface RenderSurfaceType; 66 67 enum LayerIdLabels { 68 INVALID_ID = -1, 69 }; 70 71 static scoped_refptr<Layer> Create(); 72 73 int id() const { return layer_id_; } 74 75 Layer* RootLayer(); 76 Layer* parent() { return parent_; } 77 const Layer* parent() const { return parent_; } 78 void AddChild(scoped_refptr<Layer> child); 79 void InsertChild(scoped_refptr<Layer> child, size_t index); 80 void ReplaceChild(Layer* reference, scoped_refptr<Layer> new_layer); 81 void RemoveFromParent(); 82 void RemoveAllChildren(); 83 void SetChildren(const LayerList& children); 84 bool HasAncestor(const Layer* ancestor) const; 85 86 const LayerList& children() const { return children_; } 87 Layer* child_at(size_t index) { return children_[index].get(); } 88 89 // This requests the layer and its subtree be rendered and given to the 90 // callback. If the copy is unable to be produced (the layer is destroyed 91 // first), then the callback is called with a NULL/empty result. 92 void RequestCopyOfOutput(scoped_ptr<CopyOutputRequest> request); 93 bool HasCopyRequest() const { 94 return !copy_requests_.empty(); 95 } 96 97 void SetAnchorPoint(gfx::PointF anchor_point); 98 gfx::PointF anchor_point() const { return anchor_point_; } 99 100 void SetAnchorPointZ(float anchor_point_z); 101 float anchor_point_z() const { return anchor_point_z_; } 102 103 virtual void SetBackgroundColor(SkColor background_color); 104 SkColor background_color() const { return background_color_; } 105 // If contents_opaque(), return an opaque color else return a 106 // non-opaque color. Tries to return background_color(), if possible. 107 SkColor SafeOpaqueBackgroundColor() const; 108 109 // A layer's bounds are in logical, non-page-scaled pixels (however, the 110 // root layer's bounds are in physical pixels). 111 void SetBounds(gfx::Size bounds); 112 gfx::Size bounds() const { return bounds_; } 113 114 void SetMasksToBounds(bool masks_to_bounds); 115 bool masks_to_bounds() const { return masks_to_bounds_; } 116 117 void SetMaskLayer(Layer* mask_layer); 118 Layer* mask_layer() { return mask_layer_.get(); } 119 const Layer* mask_layer() const { return mask_layer_.get(); } 120 121 virtual void SetNeedsDisplayRect(const gfx::RectF& dirty_rect); 122 void SetNeedsDisplay() { SetNeedsDisplayRect(gfx::RectF(bounds())); } 123 124 void SetOpacity(float opacity); 125 float opacity() const { return opacity_; } 126 bool OpacityIsAnimating() const; 127 virtual bool OpacityCanAnimateOnImplThread() const; 128 129 void SetFilters(const FilterOperations& filters); 130 const FilterOperations& filters() const { return filters_; } 131 bool FilterIsAnimating() const; 132 133 // Background filters are filters applied to what is behind this layer, when 134 // they are viewed through non-opaque regions in this layer. They are used 135 // through the WebLayer interface, and are not exposed to HTML. 136 void SetBackgroundFilters(const FilterOperations& filters); 137 const FilterOperations& background_filters() const { 138 return background_filters_; 139 } 140 141 virtual void SetContentsOpaque(bool opaque); 142 bool contents_opaque() const { return contents_opaque_; } 143 144 void SetPosition(gfx::PointF position); 145 gfx::PointF position() const { return position_; } 146 147 void SetIsContainerForFixedPositionLayers(bool container); 148 bool IsContainerForFixedPositionLayers() const; 149 150 void SetPositionConstraint(const LayerPositionConstraint& constraint); 151 const LayerPositionConstraint& position_constraint() const { 152 return position_constraint_; 153 } 154 155 void SetSublayerTransform(const gfx::Transform& sublayer_transform); 156 const gfx::Transform& sublayer_transform() const { 157 return sublayer_transform_; 158 } 159 160 void SetTransform(const gfx::Transform& transform); 161 const gfx::Transform& transform() const { return transform_; } 162 bool TransformIsAnimating() const; 163 164 void SetScrollParent(Layer* parent); 165 166 Layer* scroll_parent() { return scroll_parent_; } 167 const Layer* scroll_parent() const { return scroll_parent_; } 168 169 void AddScrollChild(Layer* child); 170 void RemoveScrollChild(Layer* child); 171 172 std::set<Layer*>* scroll_children() { return scroll_children_.get(); } 173 const std::set<Layer*>* scroll_children() const { 174 return scroll_children_.get(); 175 } 176 177 void SetClipParent(Layer* ancestor); 178 179 Layer* clip_parent() { return clip_parent_; } 180 const Layer* clip_parent() const { 181 return clip_parent_; 182 } 183 184 void AddClipChild(Layer* child); 185 void RemoveClipChild(Layer* child); 186 187 std::set<Layer*>* clip_children() { return clip_children_.get(); } 188 const std::set<Layer*>* clip_children() const { 189 return clip_children_.get(); 190 } 191 192 DrawProperties<Layer>& draw_properties() { return draw_properties_; } 193 const DrawProperties<Layer>& draw_properties() const { 194 return draw_properties_; 195 } 196 197 // The following are shortcut accessors to get various information from 198 // draw_properties_ 199 const gfx::Transform& draw_transform() const { 200 return draw_properties_.target_space_transform; 201 } 202 const gfx::Transform& screen_space_transform() const { 203 return draw_properties_.screen_space_transform; 204 } 205 float draw_opacity() const { return draw_properties_.opacity; } 206 bool draw_opacity_is_animating() const { 207 return draw_properties_.opacity_is_animating; 208 } 209 bool draw_transform_is_animating() const { 210 return draw_properties_.target_space_transform_is_animating; 211 } 212 bool screen_space_transform_is_animating() const { 213 return draw_properties_.screen_space_transform_is_animating; 214 } 215 bool screen_space_opacity_is_animating() const { 216 return draw_properties_.screen_space_opacity_is_animating; 217 } 218 bool can_use_lcd_text() const { return draw_properties_.can_use_lcd_text; } 219 bool is_clipped() const { return draw_properties_.is_clipped; } 220 gfx::Rect clip_rect() const { return draw_properties_.clip_rect; } 221 gfx::Rect drawable_content_rect() const { 222 return draw_properties_.drawable_content_rect; 223 } 224 gfx::Rect visible_content_rect() const { 225 return draw_properties_.visible_content_rect; 226 } 227 Layer* render_target() { 228 DCHECK(!draw_properties_.render_target || 229 draw_properties_.render_target->render_surface()); 230 return draw_properties_.render_target; 231 } 232 const Layer* render_target() const { 233 DCHECK(!draw_properties_.render_target || 234 draw_properties_.render_target->render_surface()); 235 return draw_properties_.render_target; 236 } 237 RenderSurface* render_surface() const { 238 return draw_properties_.render_surface.get(); 239 } 240 int num_unclipped_descendants() const { 241 return draw_properties_.num_unclipped_descendants; 242 } 243 244 void SetScrollOffset(gfx::Vector2d scroll_offset); 245 gfx::Vector2d scroll_offset() const { return scroll_offset_; } 246 void SetScrollOffsetFromImplSide(gfx::Vector2d scroll_offset); 247 248 void SetMaxScrollOffset(gfx::Vector2d max_scroll_offset); 249 gfx::Vector2d max_scroll_offset() const { return max_scroll_offset_; } 250 251 void SetScrollable(bool scrollable); 252 bool scrollable() const { return scrollable_; } 253 254 void SetUserScrollable(bool horizontal, bool vertical); 255 bool user_scrollable_horizontal() const { 256 return user_scrollable_horizontal_; 257 } 258 bool user_scrollable_vertical() const { return user_scrollable_vertical_; } 259 260 void SetShouldScrollOnMainThread(bool should_scroll_on_main_thread); 261 bool should_scroll_on_main_thread() const { 262 return should_scroll_on_main_thread_; 263 } 264 265 void SetHaveWheelEventHandlers(bool have_wheel_event_handlers); 266 bool have_wheel_event_handlers() const { return have_wheel_event_handlers_; } 267 268 void SetNonFastScrollableRegion(const Region& non_fast_scrollable_region); 269 const Region& non_fast_scrollable_region() const { 270 return non_fast_scrollable_region_; 271 } 272 273 void SetTouchEventHandlerRegion(const Region& touch_event_handler_region); 274 const Region& touch_event_handler_region() const { 275 return touch_event_handler_region_; 276 } 277 278 void set_did_scroll_callback(const base::Closure& callback) { 279 did_scroll_callback_ = callback; 280 } 281 282 void SetDrawCheckerboardForMissingTiles(bool checkerboard); 283 bool DrawCheckerboardForMissingTiles() const { 284 return draw_checkerboard_for_missing_tiles_; 285 } 286 287 void SetForceRenderSurface(bool force_render_surface); 288 bool force_render_surface() const { return force_render_surface_; } 289 290 gfx::Vector2d ScrollDelta() const { return gfx::Vector2d(); } 291 gfx::Vector2dF TotalScrollOffset() const { 292 // Floating point to match the LayerImpl version. 293 return scroll_offset() + ScrollDelta(); 294 } 295 296 void SetDoubleSided(bool double_sided); 297 bool double_sided() const { return double_sided_; } 298 299 void SetPreserves3d(bool preserves_3d) { preserves_3d_ = preserves_3d; } 300 bool preserves_3d() const { return preserves_3d_; } 301 302 void set_use_parent_backface_visibility(bool use) { 303 use_parent_backface_visibility_ = use; 304 } 305 bool use_parent_backface_visibility() const { 306 return use_parent_backface_visibility_; 307 } 308 309 virtual void SetLayerTreeHost(LayerTreeHost* host); 310 311 bool HasDelegatedContent() const { return false; } 312 bool HasContributingDelegatedRenderPasses() const { return false; } 313 314 void SetIsDrawable(bool is_drawable); 315 316 void SetHideLayerAndSubtree(bool hide); 317 bool hide_layer_and_subtree() const { return hide_layer_and_subtree_; } 318 319 void SetReplicaLayer(Layer* layer); 320 Layer* replica_layer() { return replica_layer_.get(); } 321 const Layer* replica_layer() const { return replica_layer_.get(); } 322 323 bool has_mask() const { return !!mask_layer_.get(); } 324 bool has_replica() const { return !!replica_layer_.get(); } 325 bool replica_has_mask() const { 326 return replica_layer_.get() && 327 (mask_layer_.get() || replica_layer_->mask_layer_.get()); 328 } 329 330 // These methods typically need to be overwritten by derived classes. 331 virtual bool DrawsContent() const; 332 virtual void SavePaintProperties(); 333 // Returns true iff any resources were updated that need to be committed. 334 virtual bool Update(ResourceUpdateQueue* queue, 335 const OcclusionTracker* occlusion); 336 virtual bool NeedMoreUpdates(); 337 virtual void SetIsMask(bool is_mask) {} 338 virtual void ReduceMemoryUsage() {} 339 virtual void OnOutputSurfaceCreated() {} 340 341 virtual std::string DebugName(); 342 343 void SetLayerClient(LayerClient* client) { client_ = client; } 344 345 void SetCompositingReasons(CompositingReasons reasons); 346 347 virtual void PushPropertiesTo(LayerImpl* layer); 348 349 void CreateRenderSurface(); 350 void ClearRenderSurface(); 351 352 // The contents scale converts from logical, non-page-scaled pixels to target 353 // pixels. The contents scale is 1 for the root layer as it is already in 354 // physical pixels. By default contents scale is forced to be 1 except for 355 // subclasses of ContentsScalingLayer. 356 float contents_scale_x() const { return draw_properties_.contents_scale_x; } 357 float contents_scale_y() const { return draw_properties_.contents_scale_y; } 358 gfx::Size content_bounds() const { return draw_properties_.content_bounds; } 359 360 virtual void CalculateContentsScale(float ideal_contents_scale, 361 float device_scale_factor, 362 float page_scale_factor, 363 bool animating_transform_to_screen, 364 float* contents_scale_x, 365 float* contents_scale_y, 366 gfx::Size* content_bounds); 367 368 LayerTreeHost* layer_tree_host() { return layer_tree_host_; } 369 const LayerTreeHost* layer_tree_host() const { return layer_tree_host_; } 370 371 // Set the priority of all desired textures in this layer. 372 virtual void SetTexturePriorities(const PriorityCalculator& priority_calc) {} 373 374 bool AddAnimation(scoped_ptr<Animation> animation); 375 void PauseAnimation(int animation_id, double time_offset); 376 void RemoveAnimation(int animation_id); 377 378 void SuspendAnimations(double monotonic_time); 379 380 bool AnimatedBoundsForBox(const gfx::BoxF& box, gfx::BoxF* bounds) { 381 return layer_animation_controller_->AnimatedBoundsForBox(box, bounds); 382 } 383 384 LayerAnimationController* layer_animation_controller() { 385 return layer_animation_controller_.get(); 386 } 387 void SetLayerAnimationControllerForTest( 388 scoped_refptr<LayerAnimationController> controller); 389 390 void set_layer_animation_delegate(AnimationDelegate* delegate) { 391 layer_animation_controller_->set_layer_animation_delegate(delegate); 392 } 393 394 bool HasActiveAnimation() const; 395 396 void AddLayerAnimationEventObserver( 397 LayerAnimationEventObserver* animation_observer); 398 void RemoveLayerAnimationEventObserver( 399 LayerAnimationEventObserver* animation_observer); 400 401 virtual Region VisibleContentOpaqueRegion() const; 402 403 virtual ScrollbarLayerInterface* ToScrollbarLayer(); 404 405 gfx::Rect LayerRectToContentRect(const gfx::RectF& layer_rect) const; 406 407 virtual skia::RefPtr<SkPicture> GetPicture() const; 408 409 // Constructs a LayerImpl of the correct runtime type for this Layer type. 410 virtual scoped_ptr<LayerImpl> CreateLayerImpl(LayerTreeImpl* tree_impl); 411 412 bool NeedsDisplayForTesting() const { return !update_rect_.IsEmpty(); } 413 void ResetNeedsDisplayForTesting() { update_rect_ = gfx::RectF(); } 414 415 RenderingStatsInstrumentation* rendering_stats_instrumentation() const; 416 417 const PaintProperties& paint_properties() const { 418 return paint_properties_; 419 } 420 421 // The scale at which contents should be rastered, to match the scale at 422 // which they will drawn to the screen. This scale is a component of the 423 // contents scale but does not include page/device scale factors. 424 // TODO(danakj): This goes away when TiledLayer goes away. 425 void set_raster_scale(float scale) { raster_scale_ = scale; } 426 float raster_scale() const { return raster_scale_; } 427 bool raster_scale_is_unknown() const { return raster_scale_ == 0.f; } 428 429 virtual bool SupportsLCDText() const; 430 431 bool needs_push_properties() const { return needs_push_properties_; } 432 bool descendant_needs_push_properties() const { 433 return num_dependents_need_push_properties_ > 0; 434 } 435 436 virtual void RunMicroBenchmark(MicroBenchmark* benchmark); 437 438 protected: 439 friend class LayerImpl; 440 friend class TreeSynchronizer; 441 virtual ~Layer(); 442 443 Layer(); 444 445 // These SetNeeds functions are in order of severity of update: 446 // 447 // Called when this layer has been modified in some way, but isn't sure 448 // that it needs a commit yet. It needs CalcDrawProperties and UpdateLayers 449 // before it knows whether or not a commit is required. 450 void SetNeedsUpdate(); 451 // Called when a property has been modified in a way that the layer 452 // knows immediately that a commit is required. This implies SetNeedsUpdate 453 // as well as SetNeedsPushProperties to push that property. 454 void SetNeedsCommit(); 455 // Called when there's been a change in layer structure. Implies both 456 // SetNeedsUpdate and SetNeedsCommit, but not SetNeedsPushProperties. 457 void SetNeedsFullTreeSync(); 458 459 // Called when the next commit should wait until the pending tree is activated 460 // before finishing the commit and unblocking the main thread. Used to ensure 461 // unused resources on the impl thread are returned before commit completes. 462 void SetNextCommitWaitsForActivation(); 463 464 void SetNeedsPushProperties(); 465 void AddDependentNeedsPushProperties(); 466 void RemoveDependentNeedsPushProperties(); 467 bool parent_should_know_need_push_properties() const { 468 return needs_push_properties() || descendant_needs_push_properties(); 469 } 470 471 bool IsPropertyChangeAllowed() const; 472 473 // If this layer has a scroll parent, it removes |this| from its list of 474 // scroll children. 475 void RemoveFromScrollTree(); 476 477 // If this layer has a clip parent, it removes |this| from its list of clip 478 // children. 479 void RemoveFromClipTree(); 480 481 void reset_raster_scale_to_unknown() { raster_scale_ = 0.f; } 482 483 // This flag is set when the layer needs to push properties to the impl 484 // side. 485 bool needs_push_properties_; 486 487 // The number of direct children or dependent layers that need to be recursed 488 // to in order for them or a descendent of them to push properties to the impl 489 // side. 490 int num_dependents_need_push_properties_; 491 492 // Tracks whether this layer may have changed stacking order with its 493 // siblings. 494 bool stacking_order_changed_; 495 496 // The update rect is the region of the compositor resource that was 497 // actually updated by the compositor. For layers that may do updating 498 // outside the compositor's control (i.e. plugin layers), this information 499 // is not available and the update rect will remain empty. 500 // Note this rect is in layer space (not content space). 501 gfx::RectF update_rect_; 502 503 scoped_refptr<Layer> mask_layer_; 504 505 int layer_id_; 506 507 // When true, the layer is about to perform an update. Any commit requests 508 // will be handled implicitly after the update completes. 509 bool ignore_set_needs_commit_; 510 511 private: 512 friend class base::RefCounted<Layer>; 513 514 void SetParent(Layer* layer); 515 bool DescendantIsFixedToContainerLayer() const; 516 517 // Returns the index of the child or -1 if not found. 518 int IndexOfChild(const Layer* reference); 519 520 // This should only be called from RemoveFromParent(). 521 void RemoveChildOrDependent(Layer* child); 522 523 // LayerAnimationValueObserver implementation. 524 virtual void OnFilterAnimated(const FilterOperations& filters) OVERRIDE; 525 virtual void OnOpacityAnimated(float opacity) OVERRIDE; 526 virtual void OnTransformAnimated(const gfx::Transform& transform) OVERRIDE; 527 virtual bool IsActive() const OVERRIDE; 528 529 LayerList children_; 530 Layer* parent_; 531 532 // Layer instances have a weak pointer to their LayerTreeHost. 533 // This pointer value is nil when a Layer is not in a tree and is 534 // updated via SetLayerTreeHost() if a layer moves between trees. 535 LayerTreeHost* layer_tree_host_; 536 537 scoped_refptr<LayerAnimationController> layer_animation_controller_; 538 539 // Layer properties. 540 gfx::Size bounds_; 541 542 gfx::Vector2d scroll_offset_; 543 gfx::Vector2d max_scroll_offset_; 544 bool scrollable_; 545 bool should_scroll_on_main_thread_; 546 bool have_wheel_event_handlers_; 547 bool user_scrollable_horizontal_; 548 bool user_scrollable_vertical_; 549 Region non_fast_scrollable_region_; 550 Region touch_event_handler_region_; 551 gfx::PointF position_; 552 gfx::PointF anchor_point_; 553 SkColor background_color_; 554 CompositingReasons compositing_reasons_; 555 float opacity_; 556 FilterOperations filters_; 557 FilterOperations background_filters_; 558 float anchor_point_z_; 559 bool is_container_for_fixed_position_layers_; 560 LayerPositionConstraint position_constraint_; 561 bool is_drawable_; 562 bool hide_layer_and_subtree_; 563 bool masks_to_bounds_; 564 bool contents_opaque_; 565 bool double_sided_; 566 bool preserves_3d_; 567 bool use_parent_backface_visibility_; 568 bool draw_checkerboard_for_missing_tiles_; 569 bool force_render_surface_; 570 Layer* scroll_parent_; 571 scoped_ptr<std::set<Layer*> > scroll_children_; 572 573 Layer* clip_parent_; 574 scoped_ptr<std::set<Layer*> > clip_children_; 575 576 gfx::Transform transform_; 577 gfx::Transform sublayer_transform_; 578 579 // Replica layer used for reflections. 580 scoped_refptr<Layer> replica_layer_; 581 582 // Transient properties. 583 float raster_scale_; 584 585 LayerClient* client_; 586 587 ScopedPtrVector<CopyOutputRequest> copy_requests_; 588 589 base::Closure did_scroll_callback_; 590 591 DrawProperties<Layer> draw_properties_; 592 593 PaintProperties paint_properties_; 594 595 DISALLOW_COPY_AND_ASSIGN(Layer); 596}; 597 598} // namespace cc 599 600#endif // CC_LAYERS_LAYER_H_ 601