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