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