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/rect.h"
31 #include "ui/gfx/transform.h"
37 class CopyOutputRequest;
38 class DelegatedFrameProvider;
39 class DelegatedRendererLayer;
42 class ResourceUpdateQueue;
43 class SolidColorLayer;
46 struct ReturnedResource;
47 typedef std::vector<ReturnedResource> ReturnedResourceArray;
56 // Layer manages a texture, transform and a set of child Layers. Any View that
57 // has enabled layers ends up creating a Layer to manage the texture.
58 // A Layer can also be created without a texture, in which case it renders
59 // nothing and is simply used as a node in a hierarchy of layers.
60 // Coordinate system used in layers is DIP (Density Independent Pixel)
61 // coordinates unless explicitly mentioned as pixel coordinates.
63 // NOTE: Unlike Views, each Layer does *not* own its child Layers. If you
64 // delete a Layer and it has children, the parent of each child Layer is set to
65 // NULL, but the children are not deleted.
66 class COMPOSITOR_EXPORT Layer
67 : public LayerAnimationDelegate,
68 NON_EXPORTED_BASE(public cc::ContentLayerClient),
69 NON_EXPORTED_BASE(public cc::TextureLayerClient),
70 NON_EXPORTED_BASE(public cc::LayerClient),
71 NON_EXPORTED_BASE(public cc::LayerAnimationEventObserver) {
74 explicit Layer(LayerType type);
77 static bool UsingPictureLayer();
79 // Retrieves the Layer's compositor. The Layer will walk up its parent chain
80 // to locate it. Returns NULL if the Layer is not attached to a compositor.
81 Compositor* GetCompositor() {
82 return const_cast<Compositor*>(
83 const_cast<const Layer*>(this)->GetCompositor());
85 const Compositor* GetCompositor() const;
87 // Called by the compositor when the Layer is set as its root Layer. This can
88 // only ever be called on the root layer.
89 void SetCompositor(Compositor* compositor);
91 LayerDelegate* delegate() { return delegate_; }
92 void set_delegate(LayerDelegate* delegate) { delegate_ = delegate; }
94 LayerOwner* owner() { return owner_; }
96 // Adds a new Layer to this Layer.
97 void Add(Layer* child);
99 // Removes a Layer from this Layer.
100 void Remove(Layer* child);
102 // Stacks |child| above all other children.
103 void StackAtTop(Layer* child);
105 // Stacks |child| directly above |other|. Both must be children of this
106 // layer. Note that if |child| is initially stacked even higher, calling this
107 // method will result in |child| being lowered in the stacking order.
108 void StackAbove(Layer* child, Layer* other);
110 // Stacks |child| below all other children.
111 void StackAtBottom(Layer* child);
113 // Stacks |child| directly below |other|. Both must be children of this
115 void StackBelow(Layer* child, Layer* other);
117 // Returns the child Layers.
118 const std::vector<Layer*>& children() const { return children_; }
121 const Layer* parent() const { return parent_; }
122 Layer* parent() { return parent_; }
124 LayerType type() const { return type_; }
126 // Returns true if this Layer contains |other| somewhere in its children.
127 bool Contains(const Layer* other) const;
129 // The layer's animator is responsible for causing automatic animations when
130 // properties are set. It also manages a queue of pending animations and
131 // handles blending of animations. The layer takes ownership of the animator.
132 void SetAnimator(LayerAnimator* animator);
134 // Returns the layer's animator. Creates a default animator of one has not
135 // been set. Will not return NULL.
136 LayerAnimator* GetAnimator();
138 // The transform, relative to the parent.
139 void SetTransform(const gfx::Transform& transform);
140 gfx::Transform transform() const;
142 // Return the target transform if animator is running, or the current
143 // transform otherwise.
144 gfx::Transform GetTargetTransform() const;
146 // The bounds, relative to the parent.
147 void SetBounds(const gfx::Rect& bounds);
148 const gfx::Rect& bounds() const { return bounds_; }
150 // The offset from our parent (stored in bounds.origin()) is an integer but we
151 // may need to be at a fractional pixel offset to align properly on screen.
152 void SetSubpixelPositionOffset(const gfx::Vector2dF offset);
153 const gfx::Vector2dF& subpixel_position_offset() const {
154 return subpixel_position_offset_;
157 // Return the target bounds if animator is running, or the current bounds
159 gfx::Rect GetTargetBounds() const;
161 // Sets/gets whether or not drawing of child layers should be clipped to the
162 // bounds of this layer.
163 void SetMasksToBounds(bool masks_to_bounds);
164 bool GetMasksToBounds() const;
166 // The opacity of the layer. The opacity is applied to each pixel of the
167 // texture (resulting alpha = opacity * alpha).
168 float opacity() const;
169 void SetOpacity(float opacity);
171 // Returns the actual opacity, which the opacity of this layer multipled by
172 // the combined opacity of the parent.
173 float GetCombinedOpacity() const;
175 // Blur pixels by this amount in anything below the layer and visible through
177 int background_blur() const { return background_blur_radius_; }
178 void SetBackgroundBlur(int blur_radius);
180 // Saturate all pixels of this layer by this amount.
181 // This effect will get "combined" with the inverted,
182 // brightness and grayscale setting.
183 float layer_saturation() const { return layer_saturation_; }
184 void SetLayerSaturation(float saturation);
186 // Change the brightness of all pixels from this layer by this amount.
187 // This effect will get "combined" with the inverted, saturate
188 // and grayscale setting.
189 float layer_brightness() const { return layer_brightness_; }
190 void SetLayerBrightness(float brightness);
192 // Return the target brightness if animator is running, or the current
193 // brightness otherwise.
194 float GetTargetBrightness() const;
196 // Change the grayscale of all pixels from this layer by this amount.
197 // This effect will get "combined" with the inverted, saturate
198 // and brightness setting.
199 float layer_grayscale() const { return layer_grayscale_; }
200 void SetLayerGrayscale(float grayscale);
202 // Return the target grayscale if animator is running, or the current
203 // grayscale otherwise.
204 float GetTargetGrayscale() const;
206 // Zoom the background by a factor of |zoom|. The effect is blended along the
207 // edge across |inset| pixels.
208 void SetBackgroundZoom(float zoom, int inset);
210 // Set the shape of this layer.
211 SkRegion* alpha_shape() const { return alpha_shape_.get(); }
212 void SetAlphaShape(scoped_ptr<SkRegion> region);
215 bool layer_inverted() const { return layer_inverted_; }
216 void SetLayerInverted(bool inverted);
218 // Return the target opacity if animator is running, or the current opacity
220 float GetTargetOpacity() const;
222 // Set a layer mask for a layer.
223 // Note the provided layer mask can neither have a layer mask itself nor can
224 // it have any children. The ownership of |layer_mask| will not be
225 // transferred with this call.
226 // Furthermore: A mask layer can only be set to one layer.
227 void SetMaskLayer(Layer* layer_mask);
228 Layer* layer_mask_layer() { return layer_mask_; }
230 // Sets the visibility of the Layer. A Layer may be visible but not
231 // drawn. This happens if any ancestor of a Layer is not visible.
232 void SetVisible(bool visible);
233 bool visible() const { return visible_; }
235 // Returns the target visibility if the animator is running. Otherwise, it
236 // returns the current visibility.
237 bool GetTargetVisibility() const;
239 // Returns true if this Layer is drawn. A Layer is drawn only if all ancestors
241 bool IsDrawn() const;
243 // Returns true if this layer can have a texture (has_texture_ is true)
244 // and is not completely obscured by a child.
245 bool ShouldDraw() const;
247 // Converts a point from the coordinates of |source| to the coordinates of
248 // |target|. Necessarily, |source| and |target| must inhabit the same Layer
250 static void ConvertPointToLayer(const Layer* source,
254 // Converts a transform to be relative to the given |ancestor|. Returns
255 // whether success (that is, whether the given ancestor was really an
256 // ancestor of this layer).
257 bool GetTargetTransformRelativeTo(const Layer* ancestor,
258 gfx::Transform* transform) const;
260 // See description in View for details
261 void SetFillsBoundsOpaquely(bool fills_bounds_opaquely);
262 bool fills_bounds_opaquely() const { return fills_bounds_opaquely_; }
264 // Set to true if this layer always paints completely within its bounds. If so
265 // we can omit an unnecessary clear, even if the layer is transparent.
266 void SetFillsBoundsCompletely(bool fills_bounds_completely);
268 const std::string& name() const { return name_; }
269 void set_name(const std::string& name) { name_ = name; }
271 // Set new TextureMailbox for this layer. Note that |mailbox| may hold a
272 // shared memory resource or an actual mailbox for a texture.
273 void SetTextureMailbox(const cc::TextureMailbox& mailbox,
274 scoped_ptr<cc::SingleReleaseCallback> release_callback,
275 gfx::Size texture_size_in_dip);
276 void SetTextureSize(gfx::Size texture_size_in_dip);
278 // Begins showing delegated frames from the |frame_provider|.
279 void SetShowDelegatedContent(cc::DelegatedFrameProvider* frame_provider,
280 gfx::Size frame_size_in_dip);
282 // Begins showing content from a surface with a particular id.
283 void SetShowSurface(cc::SurfaceId surface_id,
284 const cc::SurfaceLayer::SatisfyCallback& satisfy_callback,
285 const cc::SurfaceLayer::RequireCallback& require_callback,
286 gfx::Size surface_size,
287 gfx::Size frame_size_in_dip);
289 bool has_external_content() {
290 return texture_layer_.get() || delegated_renderer_layer_.get() ||
291 surface_layer_.get();
294 // Show a solid color instead of delegated or surface contents.
295 void SetShowSolidColorContent();
297 // Sets the layer's fill color. May only be called for LAYER_SOLID_COLOR.
298 void SetColor(SkColor color);
300 // Updates the nine patch layer's bitmap, aperture and border. May only be
301 // called for LAYER_NINE_PATCH.
302 void UpdateNinePatchLayerBitmap(const SkBitmap& bitmap);
303 void UpdateNinePatchLayerAperture(const gfx::Rect& aperture);
304 void UpdateNinePatchLayerBorder(const gfx::Rect& border);
306 // Adds |invalid_rect| to the Layer's pending invalid rect and calls
307 // ScheduleDraw(). Returns false if the paint request is ignored.
308 bool SchedulePaint(const gfx::Rect& invalid_rect);
310 // Schedules a redraw of the layer tree at the compositor.
311 // Note that this _does not_ invalidate any region of this layer; use
312 // SchedulePaint() for that.
315 // Uses damaged rectangles recorded in |damaged_region_| to invalidate the
317 void SendDamagedRects();
319 const SkRegion& damaged_region() const { return damaged_region_; }
321 void CompleteAllAnimations();
323 // Suppresses painting the content by disconnecting |delegate_|.
324 void SuppressPaint();
326 // Notifies the layer that the device scale factor has changed.
327 void OnDeviceScaleFactorChanged(float device_scale_factor);
329 // Notifies the layer that one of its children has received a new
331 void OnDelegatedFrameDamage(const gfx::Rect& damage_rect_in_dip);
333 // Requets a copy of the layer's output as a texture or bitmap.
334 void RequestCopyOfOutput(scoped_ptr<cc::CopyOutputRequest> request);
336 // ContentLayerClient
339 const gfx::Rect& clip,
340 ContentLayerClient::GraphicsContextStatus gc_status) override;
341 void DidChangeLayerCanUseLCDText() override {}
342 bool FillsBoundsCompletely() const override;
344 cc::Layer* cc_layer() { return cc_layer_; }
346 // TextureLayerClient
347 bool PrepareTextureMailbox(
348 cc::TextureMailbox* mailbox,
349 scoped_ptr<cc::SingleReleaseCallback>* release_callback,
350 bool use_shared_memory) override;
352 float device_scale_factor() const { return device_scale_factor_; }
354 // Forces a render surface to be used on this layer. This has no positive
355 // impact, and is only used for benchmarking/testing purpose.
356 void SetForceRenderSurface(bool force);
357 bool force_render_surface() const { return force_render_surface_; }
360 scoped_refptr<base::debug::ConvertableToTraceFormat> TakeDebugInfo() override;
362 // LayerAnimationEventObserver
363 void OnAnimationStarted(const cc::AnimationEvent& event) override;
365 // Whether this layer has animations waiting to get sent to its cc::Layer.
366 bool HasPendingThreadedAnimations() {
367 return pending_threaded_animations_.size() != 0;
370 // Triggers a call to SwitchToLayer.
371 void SwitchCCLayerForTest();
374 friend class LayerOwner;
376 void CollectAnimators(std::vector<scoped_refptr<LayerAnimator> >* animators);
378 // Stacks |child| above or below |other|. Helper method for StackAbove() and
380 void StackRelativeTo(Layer* child, Layer* other, bool above);
382 bool ConvertPointForAncestor(const Layer* ancestor, gfx::Point* point) const;
383 bool ConvertPointFromAncestor(const Layer* ancestor, gfx::Point* point) const;
385 // Implementation of LayerAnimatorDelegate
386 void SetBoundsFromAnimation(const gfx::Rect& bounds) override;
387 void SetTransformFromAnimation(const gfx::Transform& transform) override;
388 void SetOpacityFromAnimation(float opacity) override;
389 void SetVisibilityFromAnimation(bool visibility) override;
390 void SetBrightnessFromAnimation(float brightness) override;
391 void SetGrayscaleFromAnimation(float grayscale) override;
392 void SetColorFromAnimation(SkColor color) override;
393 void ScheduleDrawForAnimation() override;
394 const gfx::Rect& GetBoundsForAnimation() const override;
395 gfx::Transform GetTransformForAnimation() const override;
396 float GetOpacityForAnimation() const override;
397 bool GetVisibilityForAnimation() const override;
398 float GetBrightnessForAnimation() const override;
399 float GetGrayscaleForAnimation() const override;
400 SkColor GetColorForAnimation() const override;
401 float GetDeviceScaleFactor() const override;
402 void AddThreadedAnimation(scoped_ptr<cc::Animation> animation) override;
403 void RemoveThreadedAnimation(int animation_id) override;
404 LayerAnimatorCollection* GetLayerAnimatorCollection() override;
406 // Creates a corresponding composited layer for |type_|.
407 void CreateCcLayer();
409 // Recomputes and sets to |cc_layer_|.
410 void RecomputeDrawsContentAndUVRect();
411 void RecomputePosition();
413 // Set all filters which got applied to the layer.
414 void SetLayerFilters();
416 // Set all filters which got applied to the layer background.
417 void SetLayerBackgroundFilters();
419 // Cleanup |cc_layer_| and replaces it with |new_layer|.
420 void SwitchToLayer(scoped_refptr<cc::Layer> new_layer);
422 // We cannot send animations to our cc_layer_ until we have been added to a
423 // layer tree. Instead, we hold on to these animations in
424 // pending_threaded_animations_, and expect SendPendingThreadedAnimations to
425 // be called once we have been added to a tree.
426 void SendPendingThreadedAnimations();
428 void AddAnimatorsInTreeToCollection(LayerAnimatorCollection* collection);
429 void RemoveAnimatorsInTreeFromCollection(LayerAnimatorCollection* collection);
431 // Returns whether the layer has an animating LayerAnimator.
432 bool IsAnimating() const;
434 const LayerType type_;
436 Compositor* compositor_;
440 // This layer's children, in bottom-to-top stacking order.
441 std::vector<Layer*> children_;
444 gfx::Vector2dF subpixel_position_offset_;
446 // Visibility of this layer. See SetVisible/IsDrawn for more details.
449 bool force_render_surface_;
451 bool fills_bounds_opaquely_;
452 bool fills_bounds_completely_;
454 // Union of damaged rects, in pixel coordinates, to be used when
455 // compositor is ready to paint the content.
456 SkRegion damaged_region_;
458 int background_blur_radius_;
460 // Several variables which will change the visible representation of
462 float layer_saturation_;
463 float layer_brightness_;
464 float layer_grayscale_;
465 bool layer_inverted_;
467 // The associated mask layer with this layer.
469 // The back link from the mask layer to it's associated masked layer.
470 // We keep this reference for the case that if the mask layer gets deleted
471 // while attached to the main layer before the main layer is deleted.
472 Layer* layer_mask_back_link_;
474 // The zoom factor to scale the layer by. Zooming is disabled when this is
478 // Width of the border in pixels, where the scaling is blended.
481 // Shape of the window.
482 scoped_ptr<SkRegion> alpha_shape_;
486 LayerDelegate* delegate_;
490 scoped_refptr<LayerAnimator> animator_;
492 // Animations that are passed to AddThreadedAnimation before this layer is
494 cc::ScopedPtrVector<cc::Animation> pending_threaded_animations_;
496 // Ownership of the layer is held through one of the strongly typed layer
497 // pointers, depending on which sort of layer this is.
498 scoped_refptr<cc::Layer> content_layer_;
499 scoped_refptr<cc::NinePatchLayer> nine_patch_layer_;
500 scoped_refptr<cc::TextureLayer> texture_layer_;
501 scoped_refptr<cc::SolidColorLayer> solid_color_layer_;
502 scoped_refptr<cc::DelegatedRendererLayer> delegated_renderer_layer_;
503 scoped_refptr<cc::SurfaceLayer> surface_layer_;
504 cc::Layer* cc_layer_;
506 // A cached copy of |Compositor::device_scale_factor()|.
507 float device_scale_factor_;
509 // The mailbox used by texture_layer_.
510 cc::TextureMailbox mailbox_;
512 // The callback to release the mailbox. This is only set after
513 // SetTextureMailbox is called, before we give it to the TextureLayer.
514 scoped_ptr<cc::SingleReleaseCallback> mailbox_release_callback_;
516 // The size of the frame or texture in DIP, set when SetShowDelegatedContent
517 // or SetTextureMailbox was called.
518 gfx::Size frame_size_in_dip_;
520 DISALLOW_COPY_AND_ASSIGN(Layer);
525 #endif // UI_COMPOSITOR_LAYER_H_