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