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