1 // Copyright (c) 2012 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 UI_COMPOSITOR_LAYER_H_ 6 #define UI_COMPOSITOR_LAYER_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/compiler_specific.h" 12 #include "base/memory/ref_counted.h" 13 #include "base/memory/scoped_ptr.h" 14 #include "base/message_loop/message_loop.h" 15 #include "cc/animation/animation_events.h" 16 #include "cc/animation/layer_animation_event_observer.h" 17 #include "cc/base/scoped_ptr_vector.h" 18 #include "cc/layers/content_layer_client.h" 19 #include "cc/layers/layer_client.h" 20 #include "cc/layers/texture_layer_client.h" 21 #include "cc/resources/texture_mailbox.h" 22 #include "third_party/skia/include/core/SkColor.h" 23 #include "third_party/skia/include/core/SkRegion.h" 24 #include "ui/compositor/compositor.h" 25 #include "ui/compositor/layer_animation_delegate.h" 26 #include "ui/compositor/layer_delegate.h" 27 #include "ui/compositor/layer_type.h" 28 #include "ui/gfx/rect.h" 29 #include "ui/gfx/transform.h" 30 31 class SkCanvas; 32 33 namespace cc { 34 class ContentLayer; 35 class CopyOutputRequest; 36 class DelegatedFrameProvider; 37 class DelegatedRendererLayer; 38 class Layer; 39 class ResourceUpdateQueue; 40 class SolidColorLayer; 41 class TextureLayer; 42 struct ReturnedResource; 43 typedef std::vector<ReturnedResource> ReturnedResourceArray; 44 } 45 46 namespace ui { 47 48 class Compositor; 49 class LayerAnimator; 50 class LayerOwner; 51 52 // Layer manages a texture, transform and a set of child Layers. Any View that 53 // has enabled layers ends up creating a Layer to manage the texture. 54 // A Layer can also be created without a texture, in which case it renders 55 // nothing and is simply used as a node in a hierarchy of layers. 56 // Coordinate system used in layers is DIP (Density Independent Pixel) 57 // coordinates unless explicitly mentioned as pixel coordinates. 58 // 59 // NOTE: Unlike Views, each Layer does *not* own its child Layers. If you 60 // delete a Layer and it has children, the parent of each child Layer is set to 61 // NULL, but the children are not deleted. 62 class COMPOSITOR_EXPORT Layer 63 : public LayerAnimationDelegate, 64 NON_EXPORTED_BASE(public cc::ContentLayerClient), 65 NON_EXPORTED_BASE(public cc::TextureLayerClient), 66 NON_EXPORTED_BASE(public cc::LayerClient), 67 NON_EXPORTED_BASE(public cc::LayerAnimationEventObserver) { 68 public: 69 Layer(); 70 explicit Layer(LayerType type); 71 virtual ~Layer(); 72 73 static bool UsingPictureLayer(); 74 75 // Retrieves the Layer's compositor. The Layer will walk up its parent chain 76 // to locate it. Returns NULL if the Layer is not attached to a compositor. 77 Compositor* GetCompositor(); 78 79 // Called by the compositor when the Layer is set as its root Layer. This can 80 // only ever be called on the root layer. 81 void SetCompositor(Compositor* compositor); 82 83 LayerDelegate* delegate() { return delegate_; } 84 void set_delegate(LayerDelegate* delegate) { delegate_ = delegate; } 85 86 LayerOwner* owner() { return owner_; } 87 88 // Adds a new Layer to this Layer. 89 void Add(Layer* child); 90 91 // Removes a Layer from this Layer. 92 void Remove(Layer* child); 93 94 // Stacks |child| above all other children. 95 void StackAtTop(Layer* child); 96 97 // Stacks |child| directly above |other|. Both must be children of this 98 // layer. Note that if |child| is initially stacked even higher, calling this 99 // method will result in |child| being lowered in the stacking order. 100 void StackAbove(Layer* child, Layer* other); 101 102 // Stacks |child| below all other children. 103 void StackAtBottom(Layer* child); 104 105 // Stacks |child| directly below |other|. Both must be children of this 106 // layer. 107 void StackBelow(Layer* child, Layer* other); 108 109 // Returns the child Layers. 110 const std::vector<Layer*>& children() const { return children_; } 111 112 // The parent. 113 const Layer* parent() const { return parent_; } 114 Layer* parent() { return parent_; } 115 116 LayerType type() const { return type_; } 117 118 // Returns true if this Layer contains |other| somewhere in its children. 119 bool Contains(const Layer* other) const; 120 121 // The layer's animator is responsible for causing automatic animations when 122 // properties are set. It also manages a queue of pending animations and 123 // handles blending of animations. The layer takes ownership of the animator. 124 void SetAnimator(LayerAnimator* animator); 125 126 // Returns the layer's animator. Creates a default animator of one has not 127 // been set. Will not return NULL. 128 LayerAnimator* GetAnimator(); 129 130 // The transform, relative to the parent. 131 void SetTransform(const gfx::Transform& transform); 132 gfx::Transform transform() const; 133 134 // Return the target transform if animator is running, or the current 135 // transform otherwise. 136 gfx::Transform GetTargetTransform() const; 137 138 // The bounds, relative to the parent. 139 void SetBounds(const gfx::Rect& bounds); 140 const gfx::Rect& bounds() const { return bounds_; } 141 142 // The offset from our parent (stored in bounds.origin()) is an integer but we 143 // may need to be at a fractional pixel offset to align properly on screen. 144 void SetSubpixelPositionOffset(const gfx::Vector2dF offset); 145 146 // Return the target bounds if animator is running, or the current bounds 147 // otherwise. 148 gfx::Rect GetTargetBounds() const; 149 150 // Sets/gets whether or not drawing of child layers should be clipped to the 151 // bounds of this layer. 152 void SetMasksToBounds(bool masks_to_bounds); 153 bool GetMasksToBounds() const; 154 155 // The opacity of the layer. The opacity is applied to each pixel of the 156 // texture (resulting alpha = opacity * alpha). 157 float opacity() const; 158 void SetOpacity(float opacity); 159 160 // Returns the actual opacity, which the opacity of this layer multipled by 161 // the combined opacity of the parent. 162 float GetCombinedOpacity() const; 163 164 // Blur pixels by this amount in anything below the layer and visible through 165 // the layer. 166 int background_blur() const { return background_blur_radius_; } 167 void SetBackgroundBlur(int blur_radius); 168 169 // Saturate all pixels of this layer by this amount. 170 // This effect will get "combined" with the inverted, 171 // brightness and grayscale setting. 172 float layer_saturation() const { return layer_saturation_; } 173 void SetLayerSaturation(float saturation); 174 175 // Change the brightness of all pixels from this layer by this amount. 176 // This effect will get "combined" with the inverted, saturate 177 // and grayscale setting. 178 float layer_brightness() const { return layer_brightness_; } 179 void SetLayerBrightness(float brightness); 180 181 // Return the target brightness if animator is running, or the current 182 // brightness otherwise. 183 float GetTargetBrightness() const; 184 185 // Change the grayscale of all pixels from this layer by this amount. 186 // This effect will get "combined" with the inverted, saturate 187 // and brightness setting. 188 float layer_grayscale() const { return layer_grayscale_; } 189 void SetLayerGrayscale(float grayscale); 190 191 // Return the target grayscale if animator is running, or the current 192 // grayscale otherwise. 193 float GetTargetGrayscale() const; 194 195 // Zoom the background by a factor of |zoom|. The effect is blended along the 196 // edge across |inset| pixels. 197 void SetBackgroundZoom(float zoom, int inset); 198 199 // Set the shape of this layer. 200 void SetAlphaShape(scoped_ptr<SkRegion> region); 201 202 // Invert the layer. 203 bool layer_inverted() const { return layer_inverted_; } 204 void SetLayerInverted(bool inverted); 205 206 // Return the target opacity if animator is running, or the current opacity 207 // otherwise. 208 float GetTargetOpacity() const; 209 210 // Set a layer mask for a layer. 211 // Note the provided layer mask can neither have a layer mask itself nor can 212 // it have any children. The ownership of |layer_mask| will not be 213 // transferred with this call. 214 // Furthermore: A mask layer can only be set to one layer. 215 void SetMaskLayer(Layer* layer_mask); 216 Layer* layer_mask_layer() { return layer_mask_; } 217 218 // Sets the visibility of the Layer. A Layer may be visible but not 219 // drawn. This happens if any ancestor of a Layer is not visible. 220 void SetVisible(bool visible); 221 bool visible() const { return visible_; } 222 223 // Returns the target visibility if the animator is running. Otherwise, it 224 // returns the current visibility. 225 bool GetTargetVisibility() const; 226 227 // Returns true if this Layer is drawn. A Layer is drawn only if all ancestors 228 // are visible. 229 bool IsDrawn() const; 230 231 // Returns true if this layer can have a texture (has_texture_ is true) 232 // and is not completely obscured by a child. 233 bool ShouldDraw() const; 234 235 // Converts a point from the coordinates of |source| to the coordinates of 236 // |target|. Necessarily, |source| and |target| must inhabit the same Layer 237 // tree. 238 static void ConvertPointToLayer(const Layer* source, 239 const Layer* target, 240 gfx::Point* point); 241 242 // Converts a transform to be relative to the given |ancestor|. Returns 243 // whether success (that is, whether the given ancestor was really an 244 // ancestor of this layer). 245 bool GetTargetTransformRelativeTo(const Layer* ancestor, 246 gfx::Transform* transform) const; 247 248 // See description in View for details 249 void SetFillsBoundsOpaquely(bool fills_bounds_opaquely); 250 bool fills_bounds_opaquely() const { return fills_bounds_opaquely_; } 251 252 // Set to true if this layer always paints completely within its bounds. If so 253 // we can omit an unnecessary clear, even if the layer is transparent. 254 void SetFillsBoundsCompletely(bool fills_bounds_completely); 255 256 const std::string& name() const { return name_; } 257 void set_name(const std::string& name) { name_ = name; } 258 259 // Set new TextureMailbox for this layer. Note that |mailbox| may hold a 260 // shared memory resource or an actual mailbox for a texture. 261 void SetTextureMailbox(const cc::TextureMailbox& mailbox, 262 scoped_ptr<cc::SingleReleaseCallback> release_callback, 263 gfx::Size texture_size_in_dip); 264 void SetTextureSize(gfx::Size texture_size_in_dip); 265 266 // Begins showing delegated frames from the |frame_provider|. 267 void SetShowDelegatedContent(cc::DelegatedFrameProvider* frame_provider, 268 gfx::Size frame_size_in_dip); 269 270 bool has_external_content() { 271 return texture_layer_.get() || delegated_renderer_layer_.get(); 272 } 273 274 void SetShowPaintedContent(); 275 276 // Sets the layer's fill color. May only be called for LAYER_SOLID_COLOR. 277 void SetColor(SkColor color); 278 279 // Adds |invalid_rect| to the Layer's pending invalid rect and calls 280 // ScheduleDraw(). Returns false if the paint request is ignored. 281 bool SchedulePaint(const gfx::Rect& invalid_rect); 282 283 // Schedules a redraw of the layer tree at the compositor. 284 // Note that this _does not_ invalidate any region of this layer; use 285 // SchedulePaint() for that. 286 void ScheduleDraw(); 287 288 // Uses damaged rectangles recorded in |damaged_region_| to invalidate the 289 // |cc_layer_|. 290 void SendDamagedRects(); 291 292 const SkRegion& damaged_region() const { return damaged_region_; } 293 294 void CompleteAllAnimations(); 295 296 // Suppresses painting the content by disconnecting |delegate_|. 297 void SuppressPaint(); 298 299 // Notifies the layer that the device scale factor has changed. 300 void OnDeviceScaleFactorChanged(float device_scale_factor); 301 302 // Requets a copy of the layer's output as a texture or bitmap. 303 void RequestCopyOfOutput(scoped_ptr<cc::CopyOutputRequest> request); 304 305 // ContentLayerClient 306 virtual void PaintContents( 307 SkCanvas* canvas, 308 const gfx::Rect& clip, 309 gfx::RectF* opaque, 310 ContentLayerClient::GraphicsContextStatus gc_status) OVERRIDE; 311 virtual void DidChangeLayerCanUseLCDText() OVERRIDE {} 312 virtual bool FillsBoundsCompletely() const OVERRIDE; 313 314 cc::Layer* cc_layer() { return cc_layer_; } 315 316 // TextureLayerClient 317 virtual bool PrepareTextureMailbox( 318 cc::TextureMailbox* mailbox, 319 scoped_ptr<cc::SingleReleaseCallback>* release_callback, 320 bool use_shared_memory) OVERRIDE; 321 322 float device_scale_factor() const { return device_scale_factor_; } 323 324 // Forces a render surface to be used on this layer. This has no positive 325 // impact, and is only used for benchmarking/testing purpose. 326 void SetForceRenderSurface(bool force); 327 bool force_render_surface() const { return force_render_surface_; } 328 329 // LayerClient 330 virtual scoped_refptr<base::debug::ConvertableToTraceFormat> 331 TakeDebugInfo() OVERRIDE; 332 333 // LayerAnimationEventObserver 334 virtual void OnAnimationStarted(const cc::AnimationEvent& event) OVERRIDE; 335 336 // Whether this layer has animations waiting to get sent to its cc::Layer. 337 bool HasPendingThreadedAnimations() { 338 return pending_threaded_animations_.size() != 0; 339 } 340 341 // Triggers a call to SwitchToLayer. 342 void SwitchCCLayerForTest(); 343 344 private: 345 friend class LayerOwner; 346 347 void CollectAnimators(std::vector<scoped_refptr<LayerAnimator> >* animators); 348 349 // Stacks |child| above or below |other|. Helper method for StackAbove() and 350 // StackBelow(). 351 void StackRelativeTo(Layer* child, Layer* other, bool above); 352 353 bool ConvertPointForAncestor(const Layer* ancestor, gfx::Point* point) const; 354 bool ConvertPointFromAncestor(const Layer* ancestor, gfx::Point* point) const; 355 356 // Implementation of LayerAnimatorDelegate 357 virtual void SetBoundsFromAnimation(const gfx::Rect& bounds) OVERRIDE; 358 virtual void SetTransformFromAnimation( 359 const gfx::Transform& transform) OVERRIDE; 360 virtual void SetOpacityFromAnimation(float opacity) OVERRIDE; 361 virtual void SetVisibilityFromAnimation(bool visibility) OVERRIDE; 362 virtual void SetBrightnessFromAnimation(float brightness) OVERRIDE; 363 virtual void SetGrayscaleFromAnimation(float grayscale) OVERRIDE; 364 virtual void SetColorFromAnimation(SkColor color) OVERRIDE; 365 virtual void ScheduleDrawForAnimation() OVERRIDE; 366 virtual const gfx::Rect& GetBoundsForAnimation() const OVERRIDE; 367 virtual gfx::Transform GetTransformForAnimation() const OVERRIDE; 368 virtual float GetOpacityForAnimation() const OVERRIDE; 369 virtual bool GetVisibilityForAnimation() const OVERRIDE; 370 virtual float GetBrightnessForAnimation() const OVERRIDE; 371 virtual float GetGrayscaleForAnimation() const OVERRIDE; 372 virtual SkColor GetColorForAnimation() const OVERRIDE; 373 virtual float GetDeviceScaleFactor() const OVERRIDE; 374 virtual void AddThreadedAnimation( 375 scoped_ptr<cc::Animation> animation) OVERRIDE; 376 virtual void RemoveThreadedAnimation(int animation_id) OVERRIDE; 377 virtual LayerAnimatorCollection* GetLayerAnimatorCollection() OVERRIDE; 378 379 // Creates a corresponding composited layer for |type_|. 380 void CreateWebLayer(); 381 382 // Recomputes and sets to |cc_layer_|. 383 void RecomputeDrawsContentAndUVRect(); 384 void RecomputePosition(); 385 386 // Set all filters which got applied to the layer. 387 void SetLayerFilters(); 388 389 // Set all filters which got applied to the layer background. 390 void SetLayerBackgroundFilters(); 391 392 // Cleanup |cc_layer_| and replaces it with |new_layer|. 393 void SwitchToLayer(scoped_refptr<cc::Layer> new_layer); 394 395 // We cannot send animations to our cc_layer_ until we have been added to a 396 // layer tree. Instead, we hold on to these animations in 397 // pending_threaded_animations_, and expect SendPendingThreadedAnimations to 398 // be called once we have been added to a tree. 399 void SendPendingThreadedAnimations(); 400 401 void AddAnimatorsInTreeToCollection(LayerAnimatorCollection* collection); 402 void RemoveAnimatorsInTreeFromCollection(LayerAnimatorCollection* collection); 403 404 // Returns whether the layer has an animating LayerAnimator. 405 bool IsAnimating() const; 406 407 const LayerType type_; 408 409 Compositor* compositor_; 410 411 Layer* parent_; 412 413 // This layer's children, in bottom-to-top stacking order. 414 std::vector<Layer*> children_; 415 416 gfx::Rect bounds_; 417 gfx::Vector2dF subpixel_position_offset_; 418 419 // Visibility of this layer. See SetVisible/IsDrawn for more details. 420 bool visible_; 421 422 bool force_render_surface_; 423 424 bool fills_bounds_opaquely_; 425 bool fills_bounds_completely_; 426 427 // Union of damaged rects, in pixel coordinates, to be used when 428 // compositor is ready to paint the content. 429 SkRegion damaged_region_; 430 431 int background_blur_radius_; 432 433 // Several variables which will change the visible representation of 434 // the layer. 435 float layer_saturation_; 436 float layer_brightness_; 437 float layer_grayscale_; 438 bool layer_inverted_; 439 440 // The associated mask layer with this layer. 441 Layer* layer_mask_; 442 // The back link from the mask layer to it's associated masked layer. 443 // We keep this reference for the case that if the mask layer gets deleted 444 // while attached to the main layer before the main layer is deleted. 445 Layer* layer_mask_back_link_; 446 447 // The zoom factor to scale the layer by. Zooming is disabled when this is 448 // set to 1. 449 float zoom_; 450 451 // Width of the border in pixels, where the scaling is blended. 452 int zoom_inset_; 453 454 // Shape of the window. 455 scoped_ptr<SkRegion> alpha_shape_; 456 457 std::string name_; 458 459 LayerDelegate* delegate_; 460 461 LayerOwner* owner_; 462 463 scoped_refptr<LayerAnimator> animator_; 464 465 // Animations that are passed to AddThreadedAnimation before this layer is 466 // added to a tree. 467 cc::ScopedPtrVector<cc::Animation> pending_threaded_animations_; 468 469 // Ownership of the layer is held through one of the strongly typed layer 470 // pointers, depending on which sort of layer this is. 471 scoped_refptr<cc::Layer> content_layer_; 472 scoped_refptr<cc::TextureLayer> texture_layer_; 473 scoped_refptr<cc::SolidColorLayer> solid_color_layer_; 474 scoped_refptr<cc::DelegatedRendererLayer> delegated_renderer_layer_; 475 cc::Layer* cc_layer_; 476 477 // A cached copy of |Compositor::device_scale_factor()|. 478 float device_scale_factor_; 479 480 // The mailbox used by texture_layer_. 481 cc::TextureMailbox mailbox_; 482 483 // The callback to release the mailbox. This is only set after 484 // SetTextureMailbox is called, before we give it to the TextureLayer. 485 scoped_ptr<cc::SingleReleaseCallback> mailbox_release_callback_; 486 487 // The size of the frame or texture in DIP, set when SetShowDelegatedContent 488 // or SetTextureMailbox was called. 489 gfx::Size frame_size_in_dip_; 490 491 DISALLOW_COPY_AND_ASSIGN(Layer); 492 }; 493 494 } // namespace ui 495 496 #endif // UI_COMPOSITOR_LAYER_H_ 497