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