layer.h revision 0f1bc08d4cfcc34181b0b5cbf065c40f687bf740
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 bool AnimatedBoundsForBox(const gfx::BoxF& box, gfx::BoxF* bounds) { 379 return layer_animation_controller_->AnimatedBoundsForBox(box, bounds); 380 } 381 382 LayerAnimationController* layer_animation_controller() { 383 return layer_animation_controller_.get(); 384 } 385 void SetLayerAnimationControllerForTest( 386 scoped_refptr<LayerAnimationController> controller); 387 388 void set_layer_animation_delegate(AnimationDelegate* delegate) { 389 layer_animation_controller_->set_layer_animation_delegate(delegate); 390 } 391 392 bool HasActiveAnimation() const; 393 394 void AddLayerAnimationEventObserver( 395 LayerAnimationEventObserver* animation_observer); 396 void RemoveLayerAnimationEventObserver( 397 LayerAnimationEventObserver* animation_observer); 398 399 virtual Region VisibleContentOpaqueRegion() const; 400 401 virtual ScrollbarLayerInterface* ToScrollbarLayer(); 402 403 gfx::Rect LayerRectToContentRect(const gfx::RectF& layer_rect) const; 404 405 virtual skia::RefPtr<SkPicture> GetPicture() const; 406 407 // Constructs a LayerImpl of the correct runtime type for this Layer type. 408 virtual scoped_ptr<LayerImpl> CreateLayerImpl(LayerTreeImpl* tree_impl); 409 410 bool NeedsDisplayForTesting() const { return !update_rect_.IsEmpty(); } 411 void ResetNeedsDisplayForTesting() { update_rect_ = gfx::RectF(); } 412 413 RenderingStatsInstrumentation* rendering_stats_instrumentation() const; 414 415 const PaintProperties& paint_properties() const { 416 return paint_properties_; 417 } 418 419 // The scale at which contents should be rastered, to match the scale at 420 // which they will drawn to the screen. This scale is a component of the 421 // contents scale but does not include page/device scale factors. 422 // TODO(danakj): This goes away when TiledLayer goes away. 423 void set_raster_scale(float scale) { raster_scale_ = scale; } 424 float raster_scale() const { return raster_scale_; } 425 bool raster_scale_is_unknown() const { return raster_scale_ == 0.f; } 426 427 virtual bool SupportsLCDText() const; 428 429 bool needs_push_properties() const { return needs_push_properties_; } 430 bool descendant_needs_push_properties() const { 431 return num_dependents_need_push_properties_ > 0; 432 } 433 434 virtual void RunMicroBenchmark(MicroBenchmark* benchmark); 435 436 protected: 437 friend class LayerImpl; 438 friend class TreeSynchronizer; 439 virtual ~Layer(); 440 441 Layer(); 442 443 // These SetNeeds functions are in order of severity of update: 444 // 445 // Called when this layer has been modified in some way, but isn't sure 446 // that it needs a commit yet. It needs CalcDrawProperties and UpdateLayers 447 // before it knows whether or not a commit is required. 448 void SetNeedsUpdate(); 449 // Called when a property has been modified in a way that the layer 450 // knows immediately that a commit is required. This implies SetNeedsUpdate 451 // as well as SetNeedsPushProperties to push that property. 452 void SetNeedsCommit(); 453 // Called when there's been a change in layer structure. Implies both 454 // SetNeedsUpdate and SetNeedsCommit, but not SetNeedsPushProperties. 455 void SetNeedsFullTreeSync(); 456 457 // Called when the next commit should wait until the pending tree is activated 458 // before finishing the commit and unblocking the main thread. Used to ensure 459 // unused resources on the impl thread are returned before commit completes. 460 void SetNextCommitWaitsForActivation(); 461 462 void SetNeedsPushProperties(); 463 void AddDependentNeedsPushProperties(); 464 void RemoveDependentNeedsPushProperties(); 465 bool parent_should_know_need_push_properties() const { 466 return needs_push_properties() || descendant_needs_push_properties(); 467 } 468 469 bool IsPropertyChangeAllowed() const; 470 471 // If this layer has a scroll parent, it removes |this| from its list of 472 // scroll children. 473 void RemoveFromScrollTree(); 474 475 // If this layer has a clip parent, it removes |this| from its list of clip 476 // children. 477 void RemoveFromClipTree(); 478 479 void reset_raster_scale_to_unknown() { raster_scale_ = 0.f; } 480 481 // This flag is set when the layer needs to push properties to the impl 482 // side. 483 bool needs_push_properties_; 484 485 // The number of direct children or dependent layers that need to be recursed 486 // to in order for them or a descendent of them to push properties to the impl 487 // side. 488 int num_dependents_need_push_properties_; 489 490 // Tracks whether this layer may have changed stacking order with its 491 // siblings. 492 bool stacking_order_changed_; 493 494 // The update rect is the region of the compositor resource that was 495 // actually updated by the compositor. For layers that may do updating 496 // outside the compositor's control (i.e. plugin layers), this information 497 // is not available and the update rect will remain empty. 498 // Note this rect is in layer space (not content space). 499 gfx::RectF update_rect_; 500 501 scoped_refptr<Layer> mask_layer_; 502 503 int layer_id_; 504 505 // When true, the layer is about to perform an update. Any commit requests 506 // will be handled implicitly after the update completes. 507 bool ignore_set_needs_commit_; 508 509 private: 510 friend class base::RefCounted<Layer>; 511 512 void SetParent(Layer* layer); 513 bool DescendantIsFixedToContainerLayer() const; 514 515 // Returns the index of the child or -1 if not found. 516 int IndexOfChild(const Layer* reference); 517 518 // This should only be called from RemoveFromParent(). 519 void RemoveChildOrDependent(Layer* child); 520 521 // LayerAnimationValueObserver implementation. 522 virtual void OnFilterAnimated(const FilterOperations& filters) OVERRIDE; 523 virtual void OnOpacityAnimated(float opacity) OVERRIDE; 524 virtual void OnTransformAnimated(const gfx::Transform& transform) OVERRIDE; 525 virtual bool IsActive() const OVERRIDE; 526 527 LayerList children_; 528 Layer* parent_; 529 530 // Layer instances have a weak pointer to their LayerTreeHost. 531 // This pointer value is nil when a Layer is not in a tree and is 532 // updated via SetLayerTreeHost() if a layer moves between trees. 533 LayerTreeHost* layer_tree_host_; 534 535 scoped_refptr<LayerAnimationController> layer_animation_controller_; 536 537 // Layer properties. 538 gfx::Size bounds_; 539 540 gfx::Vector2d scroll_offset_; 541 gfx::Vector2d max_scroll_offset_; 542 bool scrollable_; 543 bool should_scroll_on_main_thread_; 544 bool have_wheel_event_handlers_; 545 bool user_scrollable_horizontal_; 546 bool user_scrollable_vertical_; 547 Region non_fast_scrollable_region_; 548 Region touch_event_handler_region_; 549 gfx::PointF position_; 550 gfx::PointF anchor_point_; 551 SkColor background_color_; 552 CompositingReasons compositing_reasons_; 553 float opacity_; 554 FilterOperations filters_; 555 FilterOperations background_filters_; 556 float anchor_point_z_; 557 bool is_container_for_fixed_position_layers_; 558 LayerPositionConstraint position_constraint_; 559 bool is_drawable_; 560 bool hide_layer_and_subtree_; 561 bool masks_to_bounds_; 562 bool contents_opaque_; 563 bool double_sided_; 564 bool preserves_3d_; 565 bool use_parent_backface_visibility_; 566 bool draw_checkerboard_for_missing_tiles_; 567 bool force_render_surface_; 568 Layer* scroll_parent_; 569 scoped_ptr<std::set<Layer*> > scroll_children_; 570 571 Layer* clip_parent_; 572 scoped_ptr<std::set<Layer*> > clip_children_; 573 574 gfx::Transform transform_; 575 gfx::Transform sublayer_transform_; 576 577 // Replica layer used for reflections. 578 scoped_refptr<Layer> replica_layer_; 579 580 // Transient properties. 581 float raster_scale_; 582 583 LayerClient* client_; 584 585 ScopedPtrVector<CopyOutputRequest> copy_requests_; 586 587 base::Closure did_scroll_callback_; 588 589 DrawProperties<Layer> draw_properties_; 590 591 PaintProperties paint_properties_; 592 593 DISALLOW_COPY_AND_ASSIGN(Layer); 594}; 595 596} // namespace cc 597 598#endif // CC_LAYERS_LAYER_H_ 599