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.
5 #ifndef UI_COMPOSITOR_LAYER_H_
6 #define UI_COMPOSITOR_LAYER_H_
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/region.h"
18 #include "cc/base/scoped_ptr_vector.h"
19 #include "cc/layers/content_layer_client.h"
20 #include "cc/layers/layer_client.h"
21 #include "cc/layers/surface_layer.h"
22 #include "cc/layers/texture_layer_client.h"
23 #include "cc/resources/texture_mailbox.h"
24 #include "cc/surfaces/surface_id.h"
25 #include "third_party/skia/include/core/SkColor.h"
26 #include "third_party/skia/include/core/SkRegion.h"
27 #include "ui/compositor/compositor.h"
28 #include "ui/compositor/layer_animation_delegate.h"
29 #include "ui/compositor/layer_delegate.h"
30 #include "ui/compositor/layer_type.h"
31 #include "ui/gfx/geometry/rect.h"
32 #include "ui/gfx/image/image_skia.h"
33 #include "ui/gfx/transform.h"
39 class CopyOutputRequest
;
40 class DelegatedFrameProvider
;
41 class DelegatedRendererLayer
;
44 class ResourceUpdateQueue
;
45 class SolidColorLayer
;
48 struct ReturnedResource
;
49 typedef std::vector
<ReturnedResource
> ReturnedResourceArray
;
58 // Layer manages a texture, transform and a set of child Layers. Any View that
59 // has enabled layers ends up creating a Layer to manage the texture.
60 // A Layer can also be created without a texture, in which case it renders
61 // nothing and is simply used as a node in a hierarchy of layers.
62 // Coordinate system used in layers is DIP (Density Independent Pixel)
63 // coordinates unless explicitly mentioned as pixel coordinates.
65 // NOTE: Unlike Views, each Layer does *not* own its child Layers. If you
66 // delete a Layer and it has children, the parent of each child Layer is set to
67 // NULL, but the children are not deleted.
68 class COMPOSITOR_EXPORT Layer
69 : public LayerAnimationDelegate
,
70 NON_EXPORTED_BASE(public cc::ContentLayerClient
),
71 NON_EXPORTED_BASE(public cc::TextureLayerClient
),
72 NON_EXPORTED_BASE(public cc::LayerClient
),
73 NON_EXPORTED_BASE(public cc::LayerAnimationEventObserver
) {
76 explicit Layer(LayerType type
);
79 static const cc::LayerSettings
& UILayerSettings();
80 static void InitializeUILayerSettings();
82 // Retrieves the Layer's compositor. The Layer will walk up its parent chain
83 // to locate it. Returns NULL if the Layer is not attached to a compositor.
84 Compositor
* GetCompositor() {
85 return const_cast<Compositor
*>(
86 const_cast<const Layer
*>(this)->GetCompositor());
88 const Compositor
* GetCompositor() const;
90 // Called by the compositor when the Layer is set as its root Layer. This can
91 // only ever be called on the root layer.
92 void SetCompositor(Compositor
* compositor
,
93 scoped_refptr
<cc::Layer
> root_layer
);
94 void ResetCompositor();
96 LayerDelegate
* delegate() { return delegate_
; }
97 void set_delegate(LayerDelegate
* delegate
) { delegate_
= delegate
; }
99 LayerOwner
* owner() { return owner_
; }
101 // Adds a new Layer to this Layer.
102 void Add(Layer
* child
);
104 // Removes a Layer from this Layer.
105 void Remove(Layer
* child
);
107 // Stacks |child| above all other children.
108 void StackAtTop(Layer
* child
);
110 // Stacks |child| directly above |other|. Both must be children of this
111 // layer. Note that if |child| is initially stacked even higher, calling this
112 // method will result in |child| being lowered in the stacking order.
113 void StackAbove(Layer
* child
, Layer
* other
);
115 // Stacks |child| below all other children.
116 void StackAtBottom(Layer
* child
);
118 // Stacks |child| directly below |other|. Both must be children of this
120 void StackBelow(Layer
* child
, Layer
* other
);
122 // Returns the child Layers.
123 const std::vector
<Layer
*>& children() const { return children_
; }
126 const Layer
* parent() const { return parent_
; }
127 Layer
* parent() { return parent_
; }
129 LayerType
type() const { return type_
; }
131 // Returns true if this Layer contains |other| somewhere in its children.
132 bool Contains(const Layer
* other
) const;
134 // The layer's animator is responsible for causing automatic animations when
135 // properties are set. It also manages a queue of pending animations and
136 // handles blending of animations. The layer takes ownership of the animator.
137 void SetAnimator(LayerAnimator
* animator
);
139 // Returns the layer's animator. Creates a default animator of one has not
140 // been set. Will not return NULL.
141 LayerAnimator
* GetAnimator();
143 // The transform, relative to the parent.
144 void SetTransform(const gfx::Transform
& transform
);
145 gfx::Transform
transform() const;
147 gfx::PointF
position() const { return cc_layer_
->position(); }
149 // Return the target transform if animator is running, or the current
150 // transform otherwise.
151 gfx::Transform
GetTargetTransform() const;
153 // The bounds, relative to the parent.
154 void SetBounds(const gfx::Rect
& bounds
);
155 const gfx::Rect
& bounds() const { return bounds_
; }
156 const gfx::Size
& size() const { return bounds_
.size(); }
158 // The offset from our parent (stored in bounds.origin()) is an integer but we
159 // may need to be at a fractional pixel offset to align properly on screen.
160 void SetSubpixelPositionOffset(const gfx::Vector2dF
& offset
);
161 const gfx::Vector2dF
& subpixel_position_offset() const {
162 return subpixel_position_offset_
;
165 // Return the target bounds if animator is running, or the current bounds
167 gfx::Rect
GetTargetBounds() const;
169 // Sets/gets whether or not drawing of child layers should be clipped to the
170 // bounds of this layer.
171 void SetMasksToBounds(bool masks_to_bounds
);
172 bool GetMasksToBounds() const;
174 // The opacity of the layer. The opacity is applied to each pixel of the
175 // texture (resulting alpha = opacity * alpha).
176 float opacity() const;
177 void SetOpacity(float opacity
);
179 // Returns the actual opacity, which the opacity of this layer multipled by
180 // the combined opacity of the parent.
181 float GetCombinedOpacity() const;
183 // Blur pixels by this amount in anything below the layer and visible through
185 int background_blur() const { return background_blur_radius_
; }
186 void SetBackgroundBlur(int blur_radius
);
188 // Saturate all pixels of this layer by this amount.
189 // This effect will get "combined" with the inverted,
190 // brightness and grayscale setting.
191 float layer_saturation() const { return layer_saturation_
; }
192 void SetLayerSaturation(float saturation
);
194 // Change the brightness of all pixels from this layer by this amount.
195 // This effect will get "combined" with the inverted, saturate
196 // and grayscale setting.
197 float layer_brightness() const { return layer_brightness_
; }
198 void SetLayerBrightness(float brightness
);
200 // Return the target brightness if animator is running, or the current
201 // brightness otherwise.
202 float GetTargetBrightness() const;
204 // Change the grayscale of all pixels from this layer by this amount.
205 // This effect will get "combined" with the inverted, saturate
206 // and brightness setting.
207 float layer_grayscale() const { return layer_grayscale_
; }
208 void SetLayerGrayscale(float grayscale
);
210 // Return the target grayscale if animator is running, or the current
211 // grayscale otherwise.
212 float GetTargetGrayscale() const;
214 // Zoom the background by a factor of |zoom|. The effect is blended along the
215 // edge across |inset| pixels.
216 void SetBackgroundZoom(float zoom
, int inset
);
218 // Set the shape of this layer.
219 SkRegion
* alpha_shape() const { return alpha_shape_
.get(); }
220 void SetAlphaShape(scoped_ptr
<SkRegion
> region
);
223 bool layer_inverted() const { return layer_inverted_
; }
224 void SetLayerInverted(bool inverted
);
226 // Return the target opacity if animator is running, or the current opacity
228 float GetTargetOpacity() const;
230 // Set a layer mask for a layer.
231 // Note the provided layer mask can neither have a layer mask itself nor can
232 // it have any children. The ownership of |layer_mask| will not be
233 // transferred with this call.
234 // Furthermore: A mask layer can only be set to one layer.
235 void SetMaskLayer(Layer
* layer_mask
);
236 Layer
* layer_mask_layer() { return layer_mask_
; }
238 // Sets the visibility of the Layer. A Layer may be visible but not
239 // drawn. This happens if any ancestor of a Layer is not visible.
240 void SetVisible(bool visible
);
241 bool visible() const { return visible_
; }
243 // Returns the target visibility if the animator is running. Otherwise, it
244 // returns the current visibility.
245 bool GetTargetVisibility() const;
247 // Returns true if this Layer is drawn. A Layer is drawn only if all ancestors
249 bool IsDrawn() const;
251 // Returns true if this layer can have a texture (has_texture_ is true)
252 // and is not completely obscured by a child.
253 bool ShouldDraw() const;
255 // Converts a point from the coordinates of |source| to the coordinates of
256 // |target|. Necessarily, |source| and |target| must inhabit the same Layer
258 static void ConvertPointToLayer(const Layer
* source
,
262 // Converts a transform to be relative to the given |ancestor|. Returns
263 // whether success (that is, whether the given ancestor was really an
264 // ancestor of this layer).
265 bool GetTargetTransformRelativeTo(const Layer
* ancestor
,
266 gfx::Transform
* transform
) const;
268 // See description in View for details
269 void SetFillsBoundsOpaquely(bool fills_bounds_opaquely
);
270 bool fills_bounds_opaquely() const { return fills_bounds_opaquely_
; }
272 // Set to true if this layer always paints completely within its bounds. If so
273 // we can omit an unnecessary clear, even if the layer is transparent.
274 void SetFillsBoundsCompletely(bool fills_bounds_completely
);
276 const std::string
& name() const { return name_
; }
277 void set_name(const std::string
& name
) { name_
= name
; }
279 // Set new TextureMailbox for this layer. Note that |mailbox| may hold a
280 // shared memory resource or an actual mailbox for a texture.
281 void SetTextureMailbox(const cc::TextureMailbox
& mailbox
,
282 scoped_ptr
<cc::SingleReleaseCallback
> release_callback
,
283 gfx::Size texture_size_in_dip
);
284 void SetTextureSize(gfx::Size texture_size_in_dip
);
285 void SetTextureFlipped(bool flipped
);
286 bool TextureFlipped() const;
288 // Begins showing delegated frames from the |frame_provider|.
289 void SetShowDelegatedContent(cc::DelegatedFrameProvider
* frame_provider
,
290 gfx::Size frame_size_in_dip
);
292 // Begins showing content from a surface with a particular id.
293 void SetShowSurface(cc::SurfaceId surface_id
,
294 const cc::SurfaceLayer::SatisfyCallback
& satisfy_callback
,
295 const cc::SurfaceLayer::RequireCallback
& require_callback
,
296 gfx::Size surface_size
,
298 gfx::Size frame_size_in_dip
);
300 bool has_external_content() {
301 return texture_layer_
.get() || delegated_renderer_layer_
.get() ||
302 surface_layer_
.get();
305 // Show a solid color instead of delegated or surface contents.
306 void SetShowSolidColorContent();
308 // Sets the layer's fill color. May only be called for LAYER_SOLID_COLOR.
309 void SetColor(SkColor color
);
310 SkColor
GetTargetColor();
311 SkColor
background_color() const;
313 // Updates the nine patch layer's image, aperture and border. May only be
314 // called for LAYER_NINE_PATCH.
315 void UpdateNinePatchLayerImage(const gfx::ImageSkia
& image
);
316 void UpdateNinePatchLayerAperture(const gfx::Rect
& aperture_in_dip
);
317 void UpdateNinePatchLayerBorder(const gfx::Rect
& border
);
319 // Adds |invalid_rect| to the Layer's pending invalid rect and calls
320 // ScheduleDraw(). Returns false if the paint request is ignored.
321 bool SchedulePaint(const gfx::Rect
& invalid_rect
);
323 // Schedules a redraw of the layer tree at the compositor.
324 // Note that this _does not_ invalidate any region of this layer; use
325 // SchedulePaint() for that.
328 // Uses damaged rectangles recorded in |damaged_region_| to invalidate the
330 void SendDamagedRects();
331 void ClearDamagedRects();
333 const cc::Region
& damaged_region() const { return damaged_region_
; }
335 void CompleteAllAnimations();
337 // Suppresses painting the content by disconnecting |delegate_|.
338 void SuppressPaint();
340 // Notifies the layer that the device scale factor has changed.
341 void OnDeviceScaleFactorChanged(float device_scale_factor
);
343 // Notifies the layer that one of its children has received a new
345 void OnDelegatedFrameDamage(const gfx::Rect
& damage_rect_in_dip
);
347 // Requets a copy of the layer's output as a texture or bitmap.
348 void RequestCopyOfOutput(scoped_ptr
<cc::CopyOutputRequest
> request
);
350 // ContentLayerClient
353 const gfx::Rect
& clip
,
354 ContentLayerClient::PaintingControlSetting painting_control
) override
;
355 scoped_refptr
<cc::DisplayItemList
> PaintContentsToDisplayList(
356 const gfx::Rect
& clip
,
357 ContentLayerClient::PaintingControlSetting painting_control
) override
;
358 bool FillsBoundsCompletely() const override
;
359 size_t GetApproximateUnsharedMemoryUsage() const override
;
361 cc::Layer
* cc_layer_for_testing() { return cc_layer_
; }
363 // TextureLayerClient
364 bool PrepareTextureMailbox(
365 cc::TextureMailbox
* mailbox
,
366 scoped_ptr
<cc::SingleReleaseCallback
>* release_callback
,
367 bool use_shared_memory
) override
;
369 float device_scale_factor() const { return device_scale_factor_
; }
371 // Forces a render surface to be used on this layer. This has no positive
372 // impact, and is only used for benchmarking/testing purpose.
373 void SetForceRenderSurface(bool force
);
374 bool force_render_surface() const { return force_render_surface_
; }
377 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
> TakeDebugInfo()
380 // LayerAnimationEventObserver
381 void OnAnimationStarted(const cc::AnimationEvent
& event
) override
;
383 // Whether this layer has animations waiting to get sent to its cc::Layer.
384 bool HasPendingThreadedAnimations() {
385 return pending_threaded_animations_
.size() != 0;
388 // Triggers a call to SwitchToLayer.
389 void SwitchCCLayerForTest();
392 friend class LayerOwner
;
394 void CollectAnimators(std::vector
<scoped_refptr
<LayerAnimator
> >* animators
);
396 // Stacks |child| above or below |other|. Helper method for StackAbove() and
398 void StackRelativeTo(Layer
* child
, Layer
* other
, bool above
);
400 bool ConvertPointForAncestor(const Layer
* ancestor
, gfx::Point
* point
) const;
401 bool ConvertPointFromAncestor(const Layer
* ancestor
, gfx::Point
* point
) const;
403 // Implementation of LayerAnimatorDelegate
404 void SetBoundsFromAnimation(const gfx::Rect
& bounds
) override
;
405 void SetTransformFromAnimation(const gfx::Transform
& transform
) override
;
406 void SetOpacityFromAnimation(float opacity
) override
;
407 void SetVisibilityFromAnimation(bool visibility
) override
;
408 void SetBrightnessFromAnimation(float brightness
) override
;
409 void SetGrayscaleFromAnimation(float grayscale
) override
;
410 void SetColorFromAnimation(SkColor color
) override
;
411 void ScheduleDrawForAnimation() override
;
412 const gfx::Rect
& GetBoundsForAnimation() const override
;
413 gfx::Transform
GetTransformForAnimation() const override
;
414 float GetOpacityForAnimation() const override
;
415 bool GetVisibilityForAnimation() const override
;
416 float GetBrightnessForAnimation() const override
;
417 float GetGrayscaleForAnimation() const override
;
418 SkColor
GetColorForAnimation() const override
;
419 float GetDeviceScaleFactor() const override
;
420 void AddThreadedAnimation(scoped_ptr
<cc::Animation
> animation
) override
;
421 void RemoveThreadedAnimation(int animation_id
) override
;
422 LayerAnimatorCollection
* GetLayerAnimatorCollection() override
;
424 // Creates a corresponding composited layer for |type_|.
425 void CreateCcLayer();
427 // Recomputes and sets to |cc_layer_|.
428 void RecomputeDrawsContentAndUVRect();
429 void RecomputePosition();
431 // Set all filters which got applied to the layer.
432 void SetLayerFilters();
434 // Set all filters which got applied to the layer background.
435 void SetLayerBackgroundFilters();
437 // Cleanup |cc_layer_| and replaces it with |new_layer|.
438 void SwitchToLayer(scoped_refptr
<cc::Layer
> new_layer
);
440 // We cannot send animations to our cc_layer_ until we have been added to a
441 // layer tree. Instead, we hold on to these animations in
442 // pending_threaded_animations_, and expect SendPendingThreadedAnimations to
443 // be called once we have been added to a tree.
444 void SendPendingThreadedAnimations();
446 void AddAnimatorsInTreeToCollection(LayerAnimatorCollection
* collection
);
447 void RemoveAnimatorsInTreeFromCollection(LayerAnimatorCollection
* collection
);
449 // Returns whether the layer has an animating LayerAnimator.
450 bool IsAnimating() const;
452 const LayerType type_
;
454 Compositor
* compositor_
;
458 // This layer's children, in bottom-to-top stacking order.
459 std::vector
<Layer
*> children_
;
462 gfx::Vector2dF subpixel_position_offset_
;
464 // Visibility of this layer. See SetVisible/IsDrawn for more details.
467 bool force_render_surface_
;
469 bool fills_bounds_opaquely_
;
470 bool fills_bounds_completely_
;
472 // Union of damaged rects, in layer space, to be used when compositor is ready
473 // to paint the content.
474 cc::Region damaged_region_
;
476 int background_blur_radius_
;
478 // Several variables which will change the visible representation of
480 float layer_saturation_
;
481 float layer_brightness_
;
482 float layer_grayscale_
;
483 bool layer_inverted_
;
485 // The associated mask layer with this layer.
487 // The back link from the mask layer to it's associated masked layer.
488 // We keep this reference for the case that if the mask layer gets deleted
489 // while attached to the main layer before the main layer is deleted.
490 Layer
* layer_mask_back_link_
;
492 // The zoom factor to scale the layer by. Zooming is disabled when this is
496 // Width of the border in pixels, where the scaling is blended.
499 // Shape of the window.
500 scoped_ptr
<SkRegion
> alpha_shape_
;
504 LayerDelegate
* delegate_
;
508 scoped_refptr
<LayerAnimator
> animator_
;
510 // Animations that are passed to AddThreadedAnimation before this layer is
512 cc::ScopedPtrVector
<cc::Animation
> pending_threaded_animations_
;
514 // Ownership of the layer is held through one of the strongly typed layer
515 // pointers, depending on which sort of layer this is.
516 scoped_refptr
<cc::Layer
> content_layer_
;
517 scoped_refptr
<cc::NinePatchLayer
> nine_patch_layer_
;
518 scoped_refptr
<cc::TextureLayer
> texture_layer_
;
519 scoped_refptr
<cc::SolidColorLayer
> solid_color_layer_
;
520 scoped_refptr
<cc::DelegatedRendererLayer
> delegated_renderer_layer_
;
521 scoped_refptr
<cc::SurfaceLayer
> surface_layer_
;
522 cc::Layer
* cc_layer_
;
524 // A cached copy of |Compositor::device_scale_factor()|.
525 float device_scale_factor_
;
527 // A cached copy of the nine patch layer's image and aperture.
528 // These are required for device scale factor change.
529 gfx::ImageSkia nine_patch_layer_image_
;
530 gfx::Rect nine_patch_layer_aperture_
;
532 // The mailbox used by texture_layer_.
533 cc::TextureMailbox mailbox_
;
535 // The callback to release the mailbox. This is only set after
536 // SetTextureMailbox is called, before we give it to the TextureLayer.
537 scoped_ptr
<cc::SingleReleaseCallback
> mailbox_release_callback_
;
539 // The size of the frame or texture in DIP, set when SetShowDelegatedContent
540 // or SetTextureMailbox was called.
541 gfx::Size frame_size_in_dip_
;
543 DISALLOW_COPY_AND_ASSIGN(Layer
);
548 #endif // UI_COMPOSITOR_LAYER_H_