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