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