layer.h revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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 float device_scale_factor); 238 239 // See description in View for details 240 void SetFillsBoundsOpaquely(bool fills_bounds_opaquely); 241 bool fills_bounds_opaquely() const { return fills_bounds_opaquely_; } 242 243 const std::string& name() const { return name_; } 244 void set_name(const std::string& name) { name_ = name; } 245 246 const ui::Texture* texture() const { return texture_.get(); } 247 248 // Assigns a new external texture. |texture| can be NULL to disable external 249 // updates. 250 void SetExternalTexture(ui::Texture* texture); 251 ui::Texture* external_texture() { return texture_.get(); } 252 253 // Sets a delegated frame, coming from a child compositor. 254 void SetDelegatedFrame(scoped_ptr<cc::DelegatedFrameData> frame, 255 gfx::Size frame_size_in_dip); 256 257 // Gets unused resources to recycle to the child compositor. 258 void TakeUnusedResourcesForChildCompositor( 259 cc::TransferableResourceArray* array); 260 261 // Sets the layer's fill color. May only be called for LAYER_SOLID_COLOR. 262 void SetColor(SkColor color); 263 264 // Adds |invalid_rect| to the Layer's pending invalid rect and calls 265 // ScheduleDraw(). Returns false if the paint request is ignored. 266 bool SchedulePaint(const gfx::Rect& invalid_rect); 267 268 // Schedules a redraw of the layer tree at the compositor. 269 // Note that this _does not_ invalidate any region of this layer; use 270 // SchedulePaint() for that. 271 void ScheduleDraw(); 272 273 // Sends damaged rectangles recorded in |damaged_region_| to 274 // |compostior_| to repaint the content. 275 void SendDamagedRects(); 276 277 // Suppresses painting the content by disgarding damaged region and ignoring 278 // new paint requests. 279 void SuppressPaint(); 280 281 // Notifies the layer that the device scale factor has changed. 282 void OnDeviceScaleFactorChanged(float device_scale_factor); 283 284 // Sets whether the layer should scale its content. If true, the canvas will 285 // be scaled in software rendering mode before it is passed to 286 // |LayerDelegate::OnPaint|. 287 // Set to false if the delegate handles scaling. 288 // NOTE: if this is called during |LayerDelegate::OnPaint|, the new value will 289 // not apply to the canvas passed to the pending draw. 290 void set_scale_content(bool scale_content) { scale_content_ = scale_content; } 291 292 // Returns true if the layer scales its content. 293 bool scale_content() const { return scale_content_; } 294 295 // Sometimes the Layer is being updated by something other than SetCanvas 296 // (e.g. the GPU process on UI_COMPOSITOR_IMAGE_TRANSPORT). 297 bool layer_updated_externally() const { return layer_updated_externally_; } 298 299 // ContentLayerClient 300 virtual void PaintContents( 301 SkCanvas* canvas, gfx::Rect clip, gfx::RectF* opaque) OVERRIDE; 302 virtual void DidChangeLayerCanUseLCDText() OVERRIDE {} 303 304 cc::Layer* cc_layer() { return cc_layer_; } 305 306 // TextureLayerClient 307 virtual unsigned PrepareTexture(cc::ResourceUpdateQueue* queue) OVERRIDE; 308 virtual WebKit::WebGraphicsContext3D* Context3d() OVERRIDE; 309 virtual bool PrepareTextureMailbox(cc::TextureMailbox* mailbox) 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 void RecomputePosition(); 373 374 // Set all filters which got applied to the layer. 375 void SetLayerFilters(); 376 377 // Set all filters which got applied to the layer background. 378 void SetLayerBackgroundFilters(); 379 380 void UpdateIsDrawn(); 381 382 void SwitchToLayer(scoped_refptr<cc::Layer> new_layer); 383 384 // We cannot send animations to our cc_layer_ until we have been added to a 385 // layer tree. Instead, we hold on to these animations in 386 // pending_threaded_animations_, and expect SendPendingThreadedAnimations to 387 // be called once we have been added to a tree. 388 void SendPendingThreadedAnimations(); 389 390 const LayerType type_; 391 392 Compositor* compositor_; 393 394 scoped_refptr<ui::Texture> texture_; 395 396 Layer* parent_; 397 398 // This layer's children, in bottom-to-top stacking order. 399 std::vector<Layer*> children_; 400 401 gfx::Rect bounds_; 402 403 // Visibility of this layer. See SetVisible/IsDrawn for more details. 404 bool visible_; 405 406 // Computed based on the visibility of this layer and its ancestors. 407 bool is_drawn_; 408 409 bool force_render_surface_; 410 411 bool fills_bounds_opaquely_; 412 413 // If true the layer is always up to date. 414 bool layer_updated_externally_; 415 416 // Union of damaged rects, in pixel coordinates, to be used when 417 // compositor is ready to paint the content. 418 SkRegion damaged_region_; 419 420 int background_blur_radius_; 421 422 // Several variables which will change the visible representation of 423 // the layer. 424 float layer_saturation_; 425 float layer_brightness_; 426 float layer_grayscale_; 427 bool layer_inverted_; 428 429 // The associated mask layer with this layer. 430 Layer* layer_mask_; 431 // The back link from the mask layer to it's associated masked layer. 432 // We keep this reference for the case that if the mask layer gets deleted 433 // while attached to the main layer before the main layer is deleted. 434 Layer* layer_mask_back_link_; 435 436 // The zoom factor to scale the layer by. Zooming is disabled when this is 437 // set to 1. 438 float zoom_; 439 440 // Width of the border in pixels, where the scaling is blended. 441 int zoom_inset_; 442 443 std::string name_; 444 445 LayerDelegate* delegate_; 446 447 scoped_refptr<LayerAnimator> animator_; 448 449 // Animations that are passed to AddThreadedAnimation before this layer is 450 // added to a tree. 451 cc::ScopedPtrVector<cc::Animation> pending_threaded_animations_; 452 453 // Ownership of the layer is held through one of the strongly typed layer 454 // pointers, depending on which sort of layer this is. 455 scoped_refptr<cc::ContentLayer> content_layer_; 456 scoped_refptr<cc::TextureLayer> texture_layer_; 457 scoped_refptr<cc::SolidColorLayer> solid_color_layer_; 458 scoped_refptr<cc::DelegatedRendererLayer> delegated_renderer_layer_; 459 cc::Layer* cc_layer_; 460 461 // If true, the layer scales the canvas and the texture with the device scale 462 // factor as appropriate. When true, the texture size is in DIP. 463 bool scale_content_; 464 465 // A cached copy of |Compositor::device_scale_factor()|. 466 float device_scale_factor_; 467 468 // The size of the delegated frame in DIP, set when SetDelegatedFrame was 469 // called. 470 gfx::Size delegated_frame_size_in_dip_; 471 472 DISALLOW_COPY_AND_ASSIGN(Layer); 473}; 474 475} // namespace ui 476 477#endif // UI_COMPOSITOR_LAYER_H_ 478