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/texture_layer_client.h"
21 #include "cc/resources/texture_mailbox.h"
22 #include "third_party/skia/include/core/SkColor.h"
23 #include "third_party/skia/include/core/SkRegion.h"
24 #include "ui/compositor/compositor.h"
25 #include "ui/compositor/layer_animation_delegate.h"
26 #include "ui/compositor/layer_delegate.h"
27 #include "ui/compositor/layer_type.h"
28 #include "ui/gfx/rect.h"
29 #include "ui/gfx/transform.h"
35 class CopyOutputRequest;
36 class DelegatedFrameProvider;
37 class DelegatedRendererLayer;
39 class ResourceUpdateQueue;
40 class SolidColorLayer;
42 struct ReturnedResource;
43 typedef std::vector<ReturnedResource> ReturnedResourceArray;
53 // Layer manages a texture, transform and a set of child Layers. Any View that
54 // has enabled layers ends up creating a Layer to manage the texture.
55 // A Layer can also be created without a texture, in which case it renders
56 // nothing and is simply used as a node in a hierarchy of layers.
57 // Coordinate system used in layers is DIP (Density Independent Pixel)
58 // coordinates unless explicitly mentioned as pixel coordinates.
60 // NOTE: Unlike Views, each Layer does *not* own its child Layers. If you
61 // delete a Layer and it has children, the parent of each child Layer is set to
62 // NULL, but the children are not deleted.
63 class COMPOSITOR_EXPORT Layer
64 : public LayerAnimationDelegate,
65 NON_EXPORTED_BASE(public cc::ContentLayerClient),
66 NON_EXPORTED_BASE(public cc::TextureLayerClient),
67 NON_EXPORTED_BASE(public cc::LayerClient),
68 NON_EXPORTED_BASE(public cc::LayerAnimationEventObserver) {
71 explicit Layer(LayerType type);
74 static bool UsingPictureLayer();
76 // Retrieves the Layer's compositor. The Layer will walk up its parent chain
77 // to locate it. Returns NULL if the Layer is not attached to a compositor.
78 Compositor* GetCompositor();
80 // Called by the compositor when the Layer is set as its root Layer. This can
81 // only ever be called on the root layer.
82 void SetCompositor(Compositor* compositor);
84 LayerDelegate* delegate() { return delegate_; }
85 void set_delegate(LayerDelegate* delegate) { delegate_ = delegate; }
87 LayerOwner* owner() { return owner_; }
89 // Adds a new Layer to this Layer.
90 void Add(Layer* child);
92 // Removes a Layer from this Layer.
93 void Remove(Layer* child);
95 // Stacks |child| above all other children.
96 void StackAtTop(Layer* child);
98 // Stacks |child| directly above |other|. Both must be children of this
99 // layer. Note that if |child| is initially stacked even higher, calling this
100 // method will result in |child| being lowered in the stacking order.
101 void StackAbove(Layer* child, Layer* other);
103 // Stacks |child| below all other children.
104 void StackAtBottom(Layer* child);
106 // Stacks |child| directly below |other|. Both must be children of this
108 void StackBelow(Layer* child, Layer* other);
110 // Returns the child Layers.
111 const std::vector<Layer*>& children() const { return children_; }
114 const Layer* parent() const { return parent_; }
115 Layer* parent() { return parent_; }
117 LayerType type() const { return type_; }
119 // Returns true if this Layer contains |other| somewhere in its children.
120 bool Contains(const Layer* other) const;
122 // The layer's animator is responsible for causing automatic animations when
123 // properties are set. It also manages a queue of pending animations and
124 // handles blending of animations. The layer takes ownership of the animator.
125 void SetAnimator(LayerAnimator* animator);
127 // Returns the layer's animator. Creates a default animator of one has not
128 // been set. Will not return NULL.
129 LayerAnimator* GetAnimator();
131 // The transform, relative to the parent.
132 void SetTransform(const gfx::Transform& transform);
133 gfx::Transform transform() const;
135 // Return the target transform if animator is running, or the current
136 // transform otherwise.
137 gfx::Transform GetTargetTransform() const;
139 // The bounds, relative to the parent.
140 void SetBounds(const gfx::Rect& bounds);
141 const gfx::Rect& bounds() const { return bounds_; }
143 // Return the target bounds if animator is running, or the current bounds
145 gfx::Rect GetTargetBounds() const;
147 // Sets/gets whether or not drawing of child layers should be clipped to the
148 // bounds of this layer.
149 void SetMasksToBounds(bool masks_to_bounds);
150 bool GetMasksToBounds() const;
152 // The opacity of the layer. The opacity is applied to each pixel of the
153 // texture (resulting alpha = opacity * alpha).
154 float opacity() const;
155 void SetOpacity(float opacity);
157 // Returns the actual opacity, which the opacity of this layer multipled by
158 // the combined opacity of the parent.
159 float GetCombinedOpacity() const;
161 // Blur pixels by this amount in anything below the layer and visible through
163 int background_blur() const { return background_blur_radius_; }
164 void SetBackgroundBlur(int blur_radius);
166 // Saturate all pixels of this layer by this amount.
167 // This effect will get "combined" with the inverted,
168 // brightness and grayscale setting.
169 float layer_saturation() const { return layer_saturation_; }
170 void SetLayerSaturation(float saturation);
172 // Change the brightness of all pixels from this layer by this amount.
173 // This effect will get "combined" with the inverted, saturate
174 // and grayscale setting.
175 float layer_brightness() const { return layer_brightness_; }
176 void SetLayerBrightness(float brightness);
178 // Return the target brightness if animator is running, or the current
179 // brightness otherwise.
180 float GetTargetBrightness() const;
182 // Change the grayscale of all pixels from this layer by this amount.
183 // This effect will get "combined" with the inverted, saturate
184 // and brightness setting.
185 float layer_grayscale() const { return layer_grayscale_; }
186 void SetLayerGrayscale(float grayscale);
188 // Return the target grayscale if animator is running, or the current
189 // grayscale otherwise.
190 float GetTargetGrayscale() const;
192 // Zoom the background by a factor of |zoom|. The effect is blended along the
193 // edge across |inset| pixels.
194 void SetBackgroundZoom(float zoom, int inset);
197 bool layer_inverted() const { return layer_inverted_; }
198 void SetLayerInverted(bool inverted);
200 // Return the target opacity if animator is running, or the current opacity
202 float GetTargetOpacity() const;
204 // Set a layer mask for a layer.
205 // Note the provided layer mask can neither have a layer mask itself nor can
206 // it have any children. The ownership of |layer_mask| will not be
207 // transferred with this call.
208 // Furthermore: A mask layer can only be set to one layer.
209 void SetMaskLayer(Layer* layer_mask);
210 Layer* layer_mask_layer() { return layer_mask_; }
212 // Sets the visibility of the Layer. A Layer may be visible but not
213 // drawn. This happens if any ancestor of a Layer is not visible.
214 void SetVisible(bool visible);
215 bool visible() const { return visible_; }
217 // Returns the target visibility if the animator is running. Otherwise, it
218 // returns the current visibility.
219 bool GetTargetVisibility() const;
221 // Returns true if this Layer is drawn. A Layer is drawn only if all ancestors
223 bool IsDrawn() const;
225 // Returns true if this layer can have a texture (has_texture_ is true)
226 // and is not completely obscured by a child.
227 bool ShouldDraw() const;
229 // Converts a point from the coordinates of |source| to the coordinates of
230 // |target|. Necessarily, |source| and |target| must inhabit the same Layer
232 static void ConvertPointToLayer(const Layer* source,
236 // Converts a transform to be relative to the given |ancestor|. Returns
237 // whether success (that is, whether the given ancestor was really an
238 // ancestor of this layer).
239 bool GetTargetTransformRelativeTo(const Layer* ancestor,
240 gfx::Transform* transform) const;
242 // Converts a ui::Layer's transform to the transform on the corresponding
244 static gfx::Transform ConvertTransformToCCTransform(
245 const gfx::Transform& transform,
246 float device_scale_factor);
248 // See description in View for details
249 void SetFillsBoundsOpaquely(bool fills_bounds_opaquely);
250 bool fills_bounds_opaquely() const { return fills_bounds_opaquely_; }
252 // Set to true if this layer always paints completely within its bounds. If so
253 // we can omit an unnecessary clear, even if the layer is transparent.
254 void SetFillsBoundsCompletely(bool fills_bounds_completely);
256 const std::string& name() const { return name_; }
257 void set_name(const std::string& name) { name_ = name; }
259 const ui::Texture* texture() const { return texture_.get(); }
261 // Assigns a new external texture. |texture| can be NULL to disable external
263 void SetExternalTexture(ui::Texture* texture);
264 ui::Texture* external_texture() { return texture_.get(); }
266 // Set new TextureMailbox for this layer. Note that |mailbox| may hold a
267 // shared memory resource or an actual mailbox for a texture.
268 void SetTextureMailbox(const cc::TextureMailbox& mailbox,
269 scoped_ptr<cc::SingleReleaseCallback> release_callback,
271 cc::TextureMailbox GetTextureMailbox(float* scale_factor);
273 // Begins showing delegated frames from the |frame_provider|.
274 void SetShowDelegatedContent(cc::DelegatedFrameProvider* frame_provider,
275 gfx::Size frame_size_in_dip);
277 bool has_external_content() {
278 return texture_layer_.get() || delegated_renderer_layer_.get();
281 void SetShowPaintedContent();
283 // Sets the layer's fill color. May only be called for LAYER_SOLID_COLOR.
284 void SetColor(SkColor color);
286 // Adds |invalid_rect| to the Layer's pending invalid rect and calls
287 // ScheduleDraw(). Returns false if the paint request is ignored.
288 bool SchedulePaint(const gfx::Rect& invalid_rect);
290 // Schedules a redraw of the layer tree at the compositor.
291 // Note that this _does not_ invalidate any region of this layer; use
292 // SchedulePaint() for that.
295 // Uses damaged rectangles recorded in |damaged_region_| to invalidate the
297 void SendDamagedRects();
299 const SkRegion& damaged_region() const { return damaged_region_; }
301 // Suppresses painting the content by disconnecting |delegate_|.
302 void SuppressPaint();
304 // Notifies the layer that the device scale factor has changed.
305 void OnDeviceScaleFactorChanged(float device_scale_factor);
307 // Sets whether the layer should scale its content. If true, the canvas will
308 // be scaled in software rendering mode before it is passed to
309 // |LayerDelegate::OnPaintLayer|.
310 // Set to false if the delegate handles scaling.
311 // NOTE: if this is called during |LayerDelegate::OnPaint|, the new value will
312 // not apply to the canvas passed to the pending draw.
313 void set_scale_content(bool scale_content) { scale_content_ = scale_content; }
315 // Returns true if the layer scales its content.
316 bool scale_content() const { return scale_content_; }
318 // Requets a copy of the layer's output as a texture or bitmap.
319 void RequestCopyOfOutput(scoped_ptr<cc::CopyOutputRequest> request);
321 // ContentLayerClient
322 virtual void PaintContents(
323 SkCanvas* canvas, const gfx::Rect& clip, gfx::RectF* opaque) OVERRIDE;
324 virtual void DidChangeLayerCanUseLCDText() OVERRIDE {}
325 virtual bool FillsBoundsCompletely() const OVERRIDE;
327 cc::Layer* cc_layer() { return cc_layer_; }
329 // TextureLayerClient
330 virtual unsigned PrepareTexture() OVERRIDE;
331 virtual bool PrepareTextureMailbox(
332 cc::TextureMailbox* mailbox,
333 scoped_ptr<cc::SingleReleaseCallback>* release_callback,
334 bool use_shared_memory) OVERRIDE;
336 float device_scale_factor() const { return device_scale_factor_; }
338 // Forces a render surface to be used on this layer. This has no positive
339 // impact, and is only used for benchmarking/testing purpose.
340 void SetForceRenderSurface(bool force);
341 bool force_render_surface() const { return force_render_surface_; }
344 virtual scoped_refptr<base::debug::ConvertableToTraceFormat>
345 TakeDebugInfo() OVERRIDE;
347 // LayerAnimationEventObserver
348 virtual void OnAnimationStarted(const cc::AnimationEvent& event) OVERRIDE;
350 // Whether this layer has animations waiting to get sent to its cc::Layer.
351 bool HasPendingThreadedAnimations() {
352 return pending_threaded_animations_.size() != 0;
355 // Triggers a call to SwitchToLayer.
356 void SwitchCCLayerForTest();
359 friend class LayerOwner;
361 // Stacks |child| above or below |other|. Helper method for StackAbove() and
363 void StackRelativeTo(Layer* child, Layer* other, bool above);
365 bool ConvertPointForAncestor(const Layer* ancestor, gfx::Point* point) const;
366 bool ConvertPointFromAncestor(const Layer* ancestor, gfx::Point* point) const;
368 // Implementation of LayerAnimatorDelegate
369 virtual void SetBoundsFromAnimation(const gfx::Rect& bounds) OVERRIDE;
370 virtual void SetTransformFromAnimation(
371 const gfx::Transform& transform) OVERRIDE;
372 virtual void SetOpacityFromAnimation(float opacity) OVERRIDE;
373 virtual void SetVisibilityFromAnimation(bool visibility) OVERRIDE;
374 virtual void SetBrightnessFromAnimation(float brightness) OVERRIDE;
375 virtual void SetGrayscaleFromAnimation(float grayscale) OVERRIDE;
376 virtual void SetColorFromAnimation(SkColor color) OVERRIDE;
377 virtual void ScheduleDrawForAnimation() OVERRIDE;
378 virtual const gfx::Rect& GetBoundsForAnimation() const OVERRIDE;
379 virtual gfx::Transform GetTransformForAnimation() const OVERRIDE;
380 virtual float GetOpacityForAnimation() const OVERRIDE;
381 virtual bool GetVisibilityForAnimation() const OVERRIDE;
382 virtual float GetBrightnessForAnimation() const OVERRIDE;
383 virtual float GetGrayscaleForAnimation() const OVERRIDE;
384 virtual SkColor GetColorForAnimation() const OVERRIDE;
385 virtual float GetDeviceScaleFactor() const OVERRIDE;
386 virtual void AddThreadedAnimation(
387 scoped_ptr<cc::Animation> animation) OVERRIDE;
388 virtual void RemoveThreadedAnimation(int animation_id) OVERRIDE;
390 // Creates a corresponding composited layer for |type_|.
391 void CreateWebLayer();
393 // Recomputes and sets to |cc_layer_|.
394 void RecomputeCCTransformFromTransform(const gfx::Transform& transform);
395 void RecomputeDrawsContentAndUVRect();
396 void RecomputePosition();
398 // Set all filters which got applied to the layer.
399 void SetLayerFilters();
401 // Set all filters which got applied to the layer background.
402 void SetLayerBackgroundFilters();
404 // Cleanup |cc_layer_| and replaces it with |new_layer|.
405 void SwitchToLayer(scoped_refptr<cc::Layer> new_layer);
407 // We cannot send animations to our cc_layer_ until we have been added to a
408 // layer tree. Instead, we hold on to these animations in
409 // pending_threaded_animations_, and expect SendPendingThreadedAnimations to
410 // be called once we have been added to a tree.
411 void SendPendingThreadedAnimations();
413 const LayerType type_;
415 Compositor* compositor_;
417 scoped_refptr<ui::Texture> texture_;
421 // This layer's children, in bottom-to-top stacking order.
422 std::vector<Layer*> children_;
426 // Visibility of this layer. See SetVisible/IsDrawn for more details.
429 bool force_render_surface_;
431 bool fills_bounds_opaquely_;
432 bool fills_bounds_completely_;
434 // Union of damaged rects, in pixel coordinates, to be used when
435 // compositor is ready to paint the content.
436 SkRegion damaged_region_;
438 int background_blur_radius_;
440 // Several variables which will change the visible representation of
442 float layer_saturation_;
443 float layer_brightness_;
444 float layer_grayscale_;
445 bool layer_inverted_;
447 // The associated mask layer with this layer.
449 // The back link from the mask layer to it's associated masked layer.
450 // We keep this reference for the case that if the mask layer gets deleted
451 // while attached to the main layer before the main layer is deleted.
452 Layer* layer_mask_back_link_;
454 // The zoom factor to scale the layer by. Zooming is disabled when this is
458 // Width of the border in pixels, where the scaling is blended.
463 LayerDelegate* delegate_;
467 scoped_refptr<LayerAnimator> animator_;
469 // Animations that are passed to AddThreadedAnimation before this layer is
471 cc::ScopedPtrVector<cc::Animation> pending_threaded_animations_;
473 // Ownership of the layer is held through one of the strongly typed layer
474 // pointers, depending on which sort of layer this is.
475 scoped_refptr<cc::Layer> content_layer_;
476 scoped_refptr<cc::TextureLayer> texture_layer_;
477 scoped_refptr<cc::SolidColorLayer> solid_color_layer_;
478 scoped_refptr<cc::DelegatedRendererLayer> delegated_renderer_layer_;
479 cc::Layer* cc_layer_;
481 // If true, the layer scales the canvas and the texture with the device scale
482 // factor as apporpriate. When true, the texture size is in DIP.
485 // A cached copy of |Compositor::device_scale_factor()|.
486 float device_scale_factor_;
488 // A cached copy of the TextureMailbox given texture_layer_.
489 cc::TextureMailbox mailbox_;
491 // Device scale factor in which mailbox_ was rendered in.
492 float mailbox_scale_factor_;
494 // The size of the delegated frame in DIP, set when SetShowDelegatedContent
496 gfx::Size delegated_frame_size_in_dip_;
498 DISALLOW_COPY_AND_ASSIGN(Layer);
503 #endif // UI_COMPOSITOR_LAYER_H_