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