layer.h revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
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 ResourceUpdateQueue; 40class SolidColorLayer; 41class TextureLayer; 42struct ReturnedResource; 43typedef std::vector<ReturnedResource> ReturnedResourceArray; 44} 45 46namespace ui { 47 48class Compositor; 49class LayerAnimator; 50class 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. 62class 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 // Return the target bounds if animator is running, or the current bounds 143 // otherwise. 144 gfx::Rect GetTargetBounds() const; 145 146 // Sets/gets whether or not drawing of child layers should be clipped to the 147 // bounds of this layer. 148 void SetMasksToBounds(bool masks_to_bounds); 149 bool GetMasksToBounds() const; 150 151 // The opacity of the layer. The opacity is applied to each pixel of the 152 // texture (resulting alpha = opacity * alpha). 153 float opacity() const; 154 void SetOpacity(float opacity); 155 156 // Returns the actual opacity, which the opacity of this layer multipled by 157 // the combined opacity of the parent. 158 float GetCombinedOpacity() const; 159 160 // Blur pixels by this amount in anything below the layer and visible through 161 // the layer. 162 int background_blur() const { return background_blur_radius_; } 163 void SetBackgroundBlur(int blur_radius); 164 165 // Saturate all pixels of this layer by this amount. 166 // This effect will get "combined" with the inverted, 167 // brightness and grayscale setting. 168 float layer_saturation() const { return layer_saturation_; } 169 void SetLayerSaturation(float saturation); 170 171 // Change the brightness of all pixels from this layer by this amount. 172 // This effect will get "combined" with the inverted, saturate 173 // and grayscale setting. 174 float layer_brightness() const { return layer_brightness_; } 175 void SetLayerBrightness(float brightness); 176 177 // Return the target brightness if animator is running, or the current 178 // brightness otherwise. 179 float GetTargetBrightness() const; 180 181 // Change the grayscale of all pixels from this layer by this amount. 182 // This effect will get "combined" with the inverted, saturate 183 // and brightness setting. 184 float layer_grayscale() const { return layer_grayscale_; } 185 void SetLayerGrayscale(float grayscale); 186 187 // Return the target grayscale if animator is running, or the current 188 // grayscale otherwise. 189 float GetTargetGrayscale() const; 190 191 // Zoom the background by a factor of |zoom|. The effect is blended along the 192 // edge across |inset| pixels. 193 void SetBackgroundZoom(float zoom, int inset); 194 195 // Invert the layer. 196 bool layer_inverted() const { return layer_inverted_; } 197 void SetLayerInverted(bool inverted); 198 199 // Return the target opacity if animator is running, or the current opacity 200 // otherwise. 201 float GetTargetOpacity() const; 202 203 // Set a layer mask for a layer. 204 // Note the provided layer mask can neither have a layer mask itself nor can 205 // it have any children. The ownership of |layer_mask| will not be 206 // transferred with this call. 207 // Furthermore: A mask layer can only be set to one layer. 208 void SetMaskLayer(Layer* layer_mask); 209 Layer* layer_mask_layer() { return layer_mask_; } 210 211 // Sets the visibility of the Layer. A Layer may be visible but not 212 // drawn. This happens if any ancestor of a Layer is not visible. 213 void SetVisible(bool visible); 214 bool visible() const { return visible_; } 215 216 // Returns the target visibility if the animator is running. Otherwise, it 217 // returns the current visibility. 218 bool GetTargetVisibility() const; 219 220 // Returns true if this Layer is drawn. A Layer is drawn only if all ancestors 221 // are visible. 222 bool IsDrawn() const; 223 224 // Returns true if this layer can have a texture (has_texture_ is true) 225 // and is not completely obscured by a child. 226 bool ShouldDraw() const; 227 228 // Converts a point from the coordinates of |source| to the coordinates of 229 // |target|. Necessarily, |source| and |target| must inhabit the same Layer 230 // tree. 231 static void ConvertPointToLayer(const Layer* source, 232 const Layer* target, 233 gfx::Point* point); 234 235 // Converts a transform to be relative to the given |ancestor|. Returns 236 // whether success (that is, whether the given ancestor was really an 237 // ancestor of this layer). 238 bool GetTargetTransformRelativeTo(const Layer* ancestor, 239 gfx::Transform* transform) const; 240 241 // Converts a ui::Layer's transform to the transform on the corresponding 242 // cc::Layer. 243 static gfx::Transform ConvertTransformToCCTransform( 244 const gfx::Transform& transform, 245 float device_scale_factor); 246 247 // See description in View for details 248 void SetFillsBoundsOpaquely(bool fills_bounds_opaquely); 249 bool fills_bounds_opaquely() const { return fills_bounds_opaquely_; } 250 251 // Set to true if this layer always paints completely within its bounds. If so 252 // we can omit an unnecessary clear, even if the layer is transparent. 253 void SetFillsBoundsCompletely(bool fills_bounds_completely); 254 255 const std::string& name() const { return name_; } 256 void set_name(const std::string& name) { name_ = name; } 257 258 // Set new TextureMailbox for this layer. Note that |mailbox| may hold a 259 // shared memory resource or an actual mailbox for a texture. 260 void SetTextureMailbox(const cc::TextureMailbox& mailbox, 261 scoped_ptr<cc::SingleReleaseCallback> release_callback, 262 gfx::Size texture_size_in_dip); 263 void SetTextureSize(gfx::Size texture_size_in_dip); 264 265 // Begins showing delegated frames from the |frame_provider|. 266 void SetShowDelegatedContent(cc::DelegatedFrameProvider* frame_provider, 267 gfx::Size frame_size_in_dip); 268 269 bool has_external_content() { 270 return texture_layer_.get() || delegated_renderer_layer_.get(); 271 } 272 273 void SetShowPaintedContent(); 274 275 // Sets the layer's fill color. May only be called for LAYER_SOLID_COLOR. 276 void SetColor(SkColor color); 277 278 // Adds |invalid_rect| to the Layer's pending invalid rect and calls 279 // ScheduleDraw(). Returns false if the paint request is ignored. 280 bool SchedulePaint(const gfx::Rect& invalid_rect); 281 282 // Schedules a redraw of the layer tree at the compositor. 283 // Note that this _does not_ invalidate any region of this layer; use 284 // SchedulePaint() for that. 285 void ScheduleDraw(); 286 287 // Uses damaged rectangles recorded in |damaged_region_| to invalidate the 288 // |cc_layer_|. 289 void SendDamagedRects(); 290 291 const SkRegion& damaged_region() const { return damaged_region_; } 292 293 // Suppresses painting the content by disconnecting |delegate_|. 294 void SuppressPaint(); 295 296 // Notifies the layer that the device scale factor has changed. 297 void OnDeviceScaleFactorChanged(float device_scale_factor); 298 299 // Sets whether the layer should scale its content. If true, the canvas will 300 // be scaled in software rendering mode before it is passed to 301 // |LayerDelegate::OnPaintLayer|. 302 // Set to false if the delegate handles scaling. 303 // NOTE: if this is called during |LayerDelegate::OnPaint|, the new value will 304 // not apply to the canvas passed to the pending draw. 305 void set_scale_content(bool scale_content) { scale_content_ = scale_content; } 306 307 // Returns true if the layer scales its content. 308 bool scale_content() const { return scale_content_; } 309 310 // Requets a copy of the layer's output as a texture or bitmap. 311 void RequestCopyOfOutput(scoped_ptr<cc::CopyOutputRequest> request); 312 313 // ContentLayerClient 314 virtual void PaintContents( 315 SkCanvas* canvas, const gfx::Rect& clip, gfx::RectF* opaque) OVERRIDE; 316 virtual void DidChangeLayerCanUseLCDText() OVERRIDE {} 317 virtual bool FillsBoundsCompletely() const OVERRIDE; 318 319 cc::Layer* cc_layer() { return cc_layer_; } 320 321 // TextureLayerClient 322 virtual bool PrepareTextureMailbox( 323 cc::TextureMailbox* mailbox, 324 scoped_ptr<cc::SingleReleaseCallback>* release_callback, 325 bool use_shared_memory) OVERRIDE; 326 327 float device_scale_factor() const { return device_scale_factor_; } 328 329 // Forces a render surface to be used on this layer. This has no positive 330 // impact, and is only used for benchmarking/testing purpose. 331 void SetForceRenderSurface(bool force); 332 bool force_render_surface() const { return force_render_surface_; } 333 334 // LayerClient 335 virtual scoped_refptr<base::debug::ConvertableToTraceFormat> 336 TakeDebugInfo() OVERRIDE; 337 338 // LayerAnimationEventObserver 339 virtual void OnAnimationStarted(const cc::AnimationEvent& event) OVERRIDE; 340 341 // Whether this layer has animations waiting to get sent to its cc::Layer. 342 bool HasPendingThreadedAnimations() { 343 return pending_threaded_animations_.size() != 0; 344 } 345 346 // Triggers a call to SwitchToLayer. 347 void SwitchCCLayerForTest(); 348 349 private: 350 friend class LayerOwner; 351 352 // Stacks |child| above or below |other|. Helper method for StackAbove() and 353 // StackBelow(). 354 void StackRelativeTo(Layer* child, Layer* other, bool above); 355 356 bool ConvertPointForAncestor(const Layer* ancestor, gfx::Point* point) const; 357 bool ConvertPointFromAncestor(const Layer* ancestor, gfx::Point* point) const; 358 359 // Implementation of LayerAnimatorDelegate 360 virtual void SetBoundsFromAnimation(const gfx::Rect& bounds) OVERRIDE; 361 virtual void SetTransformFromAnimation( 362 const gfx::Transform& transform) OVERRIDE; 363 virtual void SetOpacityFromAnimation(float opacity) OVERRIDE; 364 virtual void SetVisibilityFromAnimation(bool visibility) OVERRIDE; 365 virtual void SetBrightnessFromAnimation(float brightness) OVERRIDE; 366 virtual void SetGrayscaleFromAnimation(float grayscale) OVERRIDE; 367 virtual void SetColorFromAnimation(SkColor color) OVERRIDE; 368 virtual void ScheduleDrawForAnimation() OVERRIDE; 369 virtual const gfx::Rect& GetBoundsForAnimation() const OVERRIDE; 370 virtual gfx::Transform GetTransformForAnimation() const OVERRIDE; 371 virtual float GetOpacityForAnimation() const OVERRIDE; 372 virtual bool GetVisibilityForAnimation() const OVERRIDE; 373 virtual float GetBrightnessForAnimation() const OVERRIDE; 374 virtual float GetGrayscaleForAnimation() const OVERRIDE; 375 virtual SkColor GetColorForAnimation() const OVERRIDE; 376 virtual float GetDeviceScaleFactor() const OVERRIDE; 377 virtual void AddThreadedAnimation( 378 scoped_ptr<cc::Animation> animation) OVERRIDE; 379 virtual void RemoveThreadedAnimation(int animation_id) OVERRIDE; 380 381 // Creates a corresponding composited layer for |type_|. 382 void CreateWebLayer(); 383 384 // Recomputes and sets to |cc_layer_|. 385 void RecomputeCCTransformFromTransform(const gfx::Transform& transform); 386 void RecomputeDrawsContentAndUVRect(); 387 void RecomputePosition(); 388 389 // Set all filters which got applied to the layer. 390 void SetLayerFilters(); 391 392 // Set all filters which got applied to the layer background. 393 void SetLayerBackgroundFilters(); 394 395 // Cleanup |cc_layer_| and replaces it with |new_layer|. 396 void SwitchToLayer(scoped_refptr<cc::Layer> new_layer); 397 398 // We cannot send animations to our cc_layer_ until we have been added to a 399 // layer tree. Instead, we hold on to these animations in 400 // pending_threaded_animations_, and expect SendPendingThreadedAnimations to 401 // be called once we have been added to a tree. 402 void SendPendingThreadedAnimations(); 403 404 const LayerType type_; 405 406 Compositor* compositor_; 407 408 Layer* parent_; 409 410 // This layer's children, in bottom-to-top stacking order. 411 std::vector<Layer*> children_; 412 413 gfx::Rect bounds_; 414 415 // Visibility of this layer. See SetVisible/IsDrawn for more details. 416 bool visible_; 417 418 bool force_render_surface_; 419 420 bool fills_bounds_opaquely_; 421 bool fills_bounds_completely_; 422 423 // Union of damaged rects, in pixel coordinates, to be used when 424 // compositor is ready to paint the content. 425 SkRegion damaged_region_; 426 427 int background_blur_radius_; 428 429 // Several variables which will change the visible representation of 430 // the layer. 431 float layer_saturation_; 432 float layer_brightness_; 433 float layer_grayscale_; 434 bool layer_inverted_; 435 436 // The associated mask layer with this layer. 437 Layer* layer_mask_; 438 // The back link from the mask layer to it's associated masked layer. 439 // We keep this reference for the case that if the mask layer gets deleted 440 // while attached to the main layer before the main layer is deleted. 441 Layer* layer_mask_back_link_; 442 443 // The zoom factor to scale the layer by. Zooming is disabled when this is 444 // set to 1. 445 float zoom_; 446 447 // Width of the border in pixels, where the scaling is blended. 448 int zoom_inset_; 449 450 std::string name_; 451 452 LayerDelegate* delegate_; 453 454 LayerOwner* owner_; 455 456 scoped_refptr<LayerAnimator> animator_; 457 458 // Animations that are passed to AddThreadedAnimation before this layer is 459 // added to a tree. 460 cc::ScopedPtrVector<cc::Animation> pending_threaded_animations_; 461 462 // Ownership of the layer is held through one of the strongly typed layer 463 // pointers, depending on which sort of layer this is. 464 scoped_refptr<cc::Layer> content_layer_; 465 scoped_refptr<cc::TextureLayer> texture_layer_; 466 scoped_refptr<cc::SolidColorLayer> solid_color_layer_; 467 scoped_refptr<cc::DelegatedRendererLayer> delegated_renderer_layer_; 468 cc::Layer* cc_layer_; 469 470 // If true, the layer scales the canvas and the texture with the device scale 471 // factor as apporpriate. When true, the texture size is in DIP. 472 bool scale_content_; 473 474 // A cached copy of |Compositor::device_scale_factor()|. 475 float device_scale_factor_; 476 477 // The mailbox used by texture_layer_. 478 cc::TextureMailbox mailbox_; 479 480 // The callback to release the mailbox. This is only set after 481 // SetTextureMailbox is called, before we give it to the TextureLayer. 482 scoped_ptr<cc::SingleReleaseCallback> mailbox_release_callback_; 483 484 // The size of the frame or texture in DIP, set when SetShowDelegatedContent 485 // or SetTextureMailbox was called. 486 gfx::Size frame_size_in_dip_; 487 488 DISALLOW_COPY_AND_ASSIGN(Layer); 489}; 490 491} // namespace ui 492 493#endif // UI_COMPOSITOR_LAYER_H_ 494