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/scoped_ptr_vector.h"
18 #include "cc/layers/content_layer_client.h"
19 #include "cc/layers/layer_client.h"
20 #include "cc/layers/surface_layer.h"
21 #include "cc/layers/texture_layer_client.h"
22 #include "cc/resources/texture_mailbox.h"
23 #include "cc/surfaces/surface_id.h"
24 #include "third_party/skia/include/core/SkColor.h"
25 #include "third_party/skia/include/core/SkRegion.h"
26 #include "ui/compositor/compositor.h"
27 #include "ui/compositor/layer_animation_delegate.h"
28 #include "ui/compositor/layer_delegate.h"
29 #include "ui/compositor/layer_type.h"
30 #include "ui/gfx/geometry/rect.h"
31 #include "ui/gfx/image/image_skia.h"
32 #include "ui/gfx/transform.h"
38 class CopyOutputRequest
;
39 class DelegatedFrameProvider
;
40 class DelegatedRendererLayer
;
43 class ResourceUpdateQueue
;
44 class SolidColorLayer
;
47 struct ReturnedResource
;
48 typedef std::vector
<ReturnedResource
> ReturnedResourceArray
;
57 // Layer manages a texture, transform and a set of child Layers. Any View that
58 // has enabled layers ends up creating a Layer to manage the texture.
59 // A Layer can also be created without a texture, in which case it renders
60 // nothing and is simply used as a node in a hierarchy of layers.
61 // Coordinate system used in layers is DIP (Density Independent Pixel)
62 // coordinates unless explicitly mentioned as pixel coordinates.
64 // NOTE: Unlike Views, each Layer does *not* own its child Layers. If you
65 // delete a Layer and it has children, the parent of each child Layer is set to
66 // NULL, but the children are not deleted.
67 class COMPOSITOR_EXPORT Layer
68 : public LayerAnimationDelegate
,
69 NON_EXPORTED_BASE(public cc::ContentLayerClient
),
70 NON_EXPORTED_BASE(public cc::TextureLayerClient
),
71 NON_EXPORTED_BASE(public cc::LayerClient
),
72 NON_EXPORTED_BASE(public cc::LayerAnimationEventObserver
) {
75 explicit Layer(LayerType type
);
78 static bool UsingPictureLayer();
80 // Retrieves the Layer's compositor. The Layer will walk up its parent chain
81 // to locate it. Returns NULL if the Layer is not attached to a compositor.
82 Compositor
* GetCompositor() {
83 return const_cast<Compositor
*>(
84 const_cast<const Layer
*>(this)->GetCompositor());
86 const Compositor
* GetCompositor() const;
88 // Called by the compositor when the Layer is set as its root Layer. This can
89 // only ever be called on the root layer.
90 void SetCompositor(Compositor
* compositor
);
92 LayerDelegate
* delegate() { return delegate_
; }
93 void set_delegate(LayerDelegate
* delegate
) { delegate_
= delegate
; }
95 LayerOwner
* owner() { return owner_
; }
97 // Adds a new Layer to this Layer.
98 void Add(Layer
* child
);
100 // Removes a Layer from this Layer.
101 void Remove(Layer
* child
);
103 // Stacks |child| above all other children.
104 void StackAtTop(Layer
* child
);
106 // Stacks |child| directly above |other|. Both must be children of this
107 // layer. Note that if |child| is initially stacked even higher, calling this
108 // method will result in |child| being lowered in the stacking order.
109 void StackAbove(Layer
* child
, Layer
* other
);
111 // Stacks |child| below all other children.
112 void StackAtBottom(Layer
* child
);
114 // Stacks |child| directly below |other|. Both must be children of this
116 void StackBelow(Layer
* child
, Layer
* other
);
118 // Returns the child Layers.
119 const std::vector
<Layer
*>& children() const { return children_
; }
122 const Layer
* parent() const { return parent_
; }
123 Layer
* parent() { return parent_
; }
125 LayerType
type() const { return type_
; }
127 // Returns true if this Layer contains |other| somewhere in its children.
128 bool Contains(const Layer
* other
) const;
130 // The layer's animator is responsible for causing automatic animations when
131 // properties are set. It also manages a queue of pending animations and
132 // handles blending of animations. The layer takes ownership of the animator.
133 void SetAnimator(LayerAnimator
* animator
);
135 // Returns the layer's animator. Creates a default animator of one has not
136 // been set. Will not return NULL.
137 LayerAnimator
* GetAnimator();
139 // The transform, relative to the parent.
140 void SetTransform(const gfx::Transform
& transform
);
141 gfx::Transform
transform() const;
143 // Return the target transform if animator is running, or the current
144 // transform otherwise.
145 gfx::Transform
GetTargetTransform() const;
147 // The bounds, relative to the parent.
148 void SetBounds(const gfx::Rect
& bounds
);
149 const gfx::Rect
& bounds() const { return bounds_
; }
151 // The offset from our parent (stored in bounds.origin()) is an integer but we
152 // may need to be at a fractional pixel offset to align properly on screen.
153 void SetSubpixelPositionOffset(const gfx::Vector2dF offset
);
154 const gfx::Vector2dF
& subpixel_position_offset() const {
155 return subpixel_position_offset_
;
158 // Return the target bounds if animator is running, or the current bounds
160 gfx::Rect
GetTargetBounds() const;
162 // Sets/gets whether or not drawing of child layers should be clipped to the
163 // bounds of this layer.
164 void SetMasksToBounds(bool masks_to_bounds
);
165 bool GetMasksToBounds() const;
167 // The opacity of the layer. The opacity is applied to each pixel of the
168 // texture (resulting alpha = opacity * alpha).
169 float opacity() const;
170 void SetOpacity(float opacity
);
172 // Returns the actual opacity, which the opacity of this layer multipled by
173 // the combined opacity of the parent.
174 float GetCombinedOpacity() const;
176 // Blur pixels by this amount in anything below the layer and visible through
178 int background_blur() const { return background_blur_radius_
; }
179 void SetBackgroundBlur(int blur_radius
);
181 // Saturate all pixels of this layer by this amount.
182 // This effect will get "combined" with the inverted,
183 // brightness and grayscale setting.
184 float layer_saturation() const { return layer_saturation_
; }
185 void SetLayerSaturation(float saturation
);
187 // Change the brightness of all pixels from this layer by this amount.
188 // This effect will get "combined" with the inverted, saturate
189 // and grayscale setting.
190 float layer_brightness() const { return layer_brightness_
; }
191 void SetLayerBrightness(float brightness
);
193 // Return the target brightness if animator is running, or the current
194 // brightness otherwise.
195 float GetTargetBrightness() const;
197 // Change the grayscale of all pixels from this layer by this amount.
198 // This effect will get "combined" with the inverted, saturate
199 // and brightness setting.
200 float layer_grayscale() const { return layer_grayscale_
; }
201 void SetLayerGrayscale(float grayscale
);
203 // Return the target grayscale if animator is running, or the current
204 // grayscale otherwise.
205 float GetTargetGrayscale() const;
207 // Zoom the background by a factor of |zoom|. The effect is blended along the
208 // edge across |inset| pixels.
209 void SetBackgroundZoom(float zoom
, int inset
);
211 // Set the shape of this layer.
212 SkRegion
* alpha_shape() const { return alpha_shape_
.get(); }
213 void SetAlphaShape(scoped_ptr
<SkRegion
> region
);
216 bool layer_inverted() const { return layer_inverted_
; }
217 void SetLayerInverted(bool inverted
);
219 // Return the target opacity if animator is running, or the current opacity
221 float GetTargetOpacity() const;
223 // Set a layer mask for a layer.
224 // Note the provided layer mask can neither have a layer mask itself nor can
225 // it have any children. The ownership of |layer_mask| will not be
226 // transferred with this call.
227 // Furthermore: A mask layer can only be set to one layer.
228 void SetMaskLayer(Layer
* layer_mask
);
229 Layer
* layer_mask_layer() { return layer_mask_
; }
231 // Sets the visibility of the Layer. A Layer may be visible but not
232 // drawn. This happens if any ancestor of a Layer is not visible.
233 void SetVisible(bool visible
);
234 bool visible() const { return visible_
; }
236 // Returns the target visibility if the animator is running. Otherwise, it
237 // returns the current visibility.
238 bool GetTargetVisibility() const;
240 // Returns true if this Layer is drawn. A Layer is drawn only if all ancestors
242 bool IsDrawn() const;
244 // Returns true if this layer can have a texture (has_texture_ is true)
245 // and is not completely obscured by a child.
246 bool ShouldDraw() const;
248 // Converts a point from the coordinates of |source| to the coordinates of
249 // |target|. Necessarily, |source| and |target| must inhabit the same Layer
251 static void ConvertPointToLayer(const Layer
* source
,
255 // Converts a transform to be relative to the given |ancestor|. Returns
256 // whether success (that is, whether the given ancestor was really an
257 // ancestor of this layer).
258 bool GetTargetTransformRelativeTo(const Layer
* ancestor
,
259 gfx::Transform
* transform
) const;
261 // See description in View for details
262 void SetFillsBoundsOpaquely(bool fills_bounds_opaquely
);
263 bool fills_bounds_opaquely() const { return fills_bounds_opaquely_
; }
265 // Set to true if this layer always paints completely within its bounds. If so
266 // we can omit an unnecessary clear, even if the layer is transparent.
267 void SetFillsBoundsCompletely(bool fills_bounds_completely
);
269 const std::string
& name() const { return name_
; }
270 void set_name(const std::string
& name
) { name_
= name
; }
272 // Set new TextureMailbox for this layer. Note that |mailbox| may hold a
273 // shared memory resource or an actual mailbox for a texture.
274 void SetTextureMailbox(const cc::TextureMailbox
& mailbox
,
275 scoped_ptr
<cc::SingleReleaseCallback
> release_callback
,
276 gfx::Size texture_size_in_dip
);
277 void SetTextureSize(gfx::Size texture_size_in_dip
);
278 void SetTextureFlipped(bool flipped
);
279 bool TextureFlipped() const;
281 // Begins showing delegated frames from the |frame_provider|.
282 void SetShowDelegatedContent(cc::DelegatedFrameProvider
* frame_provider
,
283 gfx::Size frame_size_in_dip
);
285 // Begins showing content from a surface with a particular id.
286 void SetShowSurface(cc::SurfaceId surface_id
,
287 const cc::SurfaceLayer::SatisfyCallback
& satisfy_callback
,
288 const cc::SurfaceLayer::RequireCallback
& require_callback
,
289 gfx::Size surface_size
,
291 gfx::Size frame_size_in_dip
);
293 bool has_external_content() {
294 return texture_layer_
.get() || delegated_renderer_layer_
.get() ||
295 surface_layer_
.get();
298 // Show a solid color instead of delegated or surface contents.
299 void SetShowSolidColorContent();
301 // Sets the layer's fill color. May only be called for LAYER_SOLID_COLOR.
302 void SetColor(SkColor color
);
304 // Updates the nine patch layer's image, aperture and border. May only be
305 // called for LAYER_NINE_PATCH.
306 void UpdateNinePatchLayerImage(const gfx::ImageSkia
& image
);
307 void UpdateNinePatchLayerAperture(const gfx::Rect
& aperture_in_dip
);
308 void UpdateNinePatchLayerBorder(const gfx::Rect
& border
);
310 // Adds |invalid_rect| to the Layer's pending invalid rect and calls
311 // ScheduleDraw(). Returns false if the paint request is ignored.
312 bool SchedulePaint(const gfx::Rect
& invalid_rect
);
314 // Schedules a redraw of the layer tree at the compositor.
315 // Note that this _does not_ invalidate any region of this layer; use
316 // SchedulePaint() for that.
319 // Uses damaged rectangles recorded in |damaged_region_| to invalidate the
321 void SendDamagedRects();
323 const SkRegion
& damaged_region() const { return damaged_region_
; }
325 void CompleteAllAnimations();
327 // Suppresses painting the content by disconnecting |delegate_|.
328 void SuppressPaint();
330 // Notifies the layer that the device scale factor has changed.
331 void OnDeviceScaleFactorChanged(float device_scale_factor
);
333 // Notifies the layer that one of its children has received a new
335 void OnDelegatedFrameDamage(const gfx::Rect
& damage_rect_in_dip
);
337 // Requets a copy of the layer's output as a texture or bitmap.
338 void RequestCopyOfOutput(scoped_ptr
<cc::CopyOutputRequest
> request
);
340 // ContentLayerClient
343 const gfx::Rect
& clip
,
344 ContentLayerClient::PaintingControlSetting painting_control
) override
;
345 void PaintContentsToDisplayList(
346 cc::DisplayItemList
* display_list
,
347 const gfx::Rect
& clip
,
348 ContentLayerClient::PaintingControlSetting painting_control
) override
;
349 bool FillsBoundsCompletely() const override
;
351 cc::Layer
* cc_layer() { return cc_layer_
; }
353 // TextureLayerClient
354 bool PrepareTextureMailbox(
355 cc::TextureMailbox
* mailbox
,
356 scoped_ptr
<cc::SingleReleaseCallback
>* release_callback
,
357 bool use_shared_memory
) override
;
359 float device_scale_factor() const { return device_scale_factor_
; }
361 // Forces a render surface to be used on this layer. This has no positive
362 // impact, and is only used for benchmarking/testing purpose.
363 void SetForceRenderSurface(bool force
);
364 bool force_render_surface() const { return force_render_surface_
; }
367 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
> TakeDebugInfo()
370 // LayerAnimationEventObserver
371 void OnAnimationStarted(const cc::AnimationEvent
& event
) override
;
373 // Whether this layer has animations waiting to get sent to its cc::Layer.
374 bool HasPendingThreadedAnimations() {
375 return pending_threaded_animations_
.size() != 0;
378 // Triggers a call to SwitchToLayer.
379 void SwitchCCLayerForTest();
382 friend class LayerOwner
;
384 void CollectAnimators(std::vector
<scoped_refptr
<LayerAnimator
> >* animators
);
386 // Stacks |child| above or below |other|. Helper method for StackAbove() and
388 void StackRelativeTo(Layer
* child
, Layer
* other
, bool above
);
390 bool ConvertPointForAncestor(const Layer
* ancestor
, gfx::Point
* point
) const;
391 bool ConvertPointFromAncestor(const Layer
* ancestor
, gfx::Point
* point
) const;
393 // Implementation of LayerAnimatorDelegate
394 void SetBoundsFromAnimation(const gfx::Rect
& bounds
) override
;
395 void SetTransformFromAnimation(const gfx::Transform
& transform
) override
;
396 void SetOpacityFromAnimation(float opacity
) override
;
397 void SetVisibilityFromAnimation(bool visibility
) override
;
398 void SetBrightnessFromAnimation(float brightness
) override
;
399 void SetGrayscaleFromAnimation(float grayscale
) override
;
400 void SetColorFromAnimation(SkColor color
) override
;
401 void ScheduleDrawForAnimation() override
;
402 const gfx::Rect
& GetBoundsForAnimation() const override
;
403 gfx::Transform
GetTransformForAnimation() const override
;
404 float GetOpacityForAnimation() const override
;
405 bool GetVisibilityForAnimation() const override
;
406 float GetBrightnessForAnimation() const override
;
407 float GetGrayscaleForAnimation() const override
;
408 SkColor
GetColorForAnimation() const override
;
409 float GetDeviceScaleFactor() const override
;
410 void AddThreadedAnimation(scoped_ptr
<cc::Animation
> animation
) override
;
411 void RemoveThreadedAnimation(int animation_id
) override
;
412 LayerAnimatorCollection
* GetLayerAnimatorCollection() override
;
414 // Creates a corresponding composited layer for |type_|.
415 void CreateCcLayer();
417 // Recomputes and sets to |cc_layer_|.
418 void RecomputeDrawsContentAndUVRect();
419 void RecomputePosition();
421 // Set all filters which got applied to the layer.
422 void SetLayerFilters();
424 // Set all filters which got applied to the layer background.
425 void SetLayerBackgroundFilters();
427 // Cleanup |cc_layer_| and replaces it with |new_layer|.
428 void SwitchToLayer(scoped_refptr
<cc::Layer
> new_layer
);
430 // We cannot send animations to our cc_layer_ until we have been added to a
431 // layer tree. Instead, we hold on to these animations in
432 // pending_threaded_animations_, and expect SendPendingThreadedAnimations to
433 // be called once we have been added to a tree.
434 void SendPendingThreadedAnimations();
436 void AddAnimatorsInTreeToCollection(LayerAnimatorCollection
* collection
);
437 void RemoveAnimatorsInTreeFromCollection(LayerAnimatorCollection
* collection
);
439 // Returns whether the layer has an animating LayerAnimator.
440 bool IsAnimating() const;
442 const LayerType type_
;
444 Compositor
* compositor_
;
448 // This layer's children, in bottom-to-top stacking order.
449 std::vector
<Layer
*> children_
;
452 gfx::Vector2dF subpixel_position_offset_
;
454 // Visibility of this layer. See SetVisible/IsDrawn for more details.
457 bool force_render_surface_
;
459 bool fills_bounds_opaquely_
;
460 bool fills_bounds_completely_
;
462 // Union of damaged rects, in pixel coordinates, to be used when
463 // compositor is ready to paint the content.
464 SkRegion damaged_region_
;
466 int background_blur_radius_
;
468 // Several variables which will change the visible representation of
470 float layer_saturation_
;
471 float layer_brightness_
;
472 float layer_grayscale_
;
473 bool layer_inverted_
;
475 // The associated mask layer with this layer.
477 // The back link from the mask layer to it's associated masked layer.
478 // We keep this reference for the case that if the mask layer gets deleted
479 // while attached to the main layer before the main layer is deleted.
480 Layer
* layer_mask_back_link_
;
482 // The zoom factor to scale the layer by. Zooming is disabled when this is
486 // Width of the border in pixels, where the scaling is blended.
489 // Shape of the window.
490 scoped_ptr
<SkRegion
> alpha_shape_
;
494 LayerDelegate
* delegate_
;
498 scoped_refptr
<LayerAnimator
> animator_
;
500 // Animations that are passed to AddThreadedAnimation before this layer is
502 cc::ScopedPtrVector
<cc::Animation
> pending_threaded_animations_
;
504 // Ownership of the layer is held through one of the strongly typed layer
505 // pointers, depending on which sort of layer this is.
506 scoped_refptr
<cc::Layer
> content_layer_
;
507 scoped_refptr
<cc::NinePatchLayer
> nine_patch_layer_
;
508 scoped_refptr
<cc::TextureLayer
> texture_layer_
;
509 scoped_refptr
<cc::SolidColorLayer
> solid_color_layer_
;
510 scoped_refptr
<cc::DelegatedRendererLayer
> delegated_renderer_layer_
;
511 scoped_refptr
<cc::SurfaceLayer
> surface_layer_
;
512 cc::Layer
* cc_layer_
;
514 // A cached copy of |Compositor::device_scale_factor()|.
515 float device_scale_factor_
;
517 // A cached copy of the nine patch layer's image and aperture.
518 // These are required for device scale factor change.
519 gfx::ImageSkia nine_patch_layer_image_
;
520 gfx::Rect nine_patch_layer_aperture_
;
522 // The mailbox used by texture_layer_.
523 cc::TextureMailbox mailbox_
;
525 // The callback to release the mailbox. This is only set after
526 // SetTextureMailbox is called, before we give it to the TextureLayer.
527 scoped_ptr
<cc::SingleReleaseCallback
> mailbox_release_callback_
;
529 // The size of the frame or texture in DIP, set when SetShowDelegatedContent
530 // or SetTextureMailbox was called.
531 gfx::Size frame_size_in_dip_
;
533 DISALLOW_COPY_AND_ASSIGN(Layer
);
538 #endif // UI_COMPOSITOR_LAYER_H_