layer.h revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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/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"
30
31class SkCanvas;
32
33namespace cc {
34class ContentLayer;
35class CopyOutputRequest;
36class DelegatedFrameProvider;
37class DelegatedRendererLayer;
38class Layer;
39class ResourceUpdateQueue;
40class SolidColorLayer;
41class TextureLayer;
42struct ReturnedResource;
43typedef std::vector<ReturnedResource> ReturnedResourceArray;
44}
45
46namespace ui {
47
48class Compositor;
49class LayerAnimator;
50class LayerOwner;
51class Texture;
52
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.
59//
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.
63class 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) {
69 public:
70  Layer();
71  explicit Layer(LayerType type);
72  virtual ~Layer();
73
74  static bool UsingPictureLayer();
75
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();
79
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);
83
84  LayerDelegate* delegate() { return delegate_; }
85  void set_delegate(LayerDelegate* delegate) { delegate_ = delegate; }
86
87  LayerOwner* owner() { return owner_; }
88
89  // Adds a new Layer to this Layer.
90  void Add(Layer* child);
91
92  // Removes a Layer from this Layer.
93  void Remove(Layer* child);
94
95  // Stacks |child| above all other children.
96  void StackAtTop(Layer* child);
97
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);
102
103  // Stacks |child| below all other children.
104  void StackAtBottom(Layer* child);
105
106  // Stacks |child| directly below |other|.  Both must be children of this
107  // layer.
108  void StackBelow(Layer* child, Layer* other);
109
110  // Returns the child Layers.
111  const std::vector<Layer*>& children() const { return children_; }
112
113  // The parent.
114  const Layer* parent() const { return parent_; }
115  Layer* parent() { return parent_; }
116
117  LayerType type() const { return type_; }
118
119  // Returns true if this Layer contains |other| somewhere in its children.
120  bool Contains(const Layer* other) const;
121
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);
126
127  // Returns the layer's animator. Creates a default animator of one has not
128  // been set. Will not return NULL.
129  LayerAnimator* GetAnimator();
130
131  // The transform, relative to the parent.
132  void SetTransform(const gfx::Transform& transform);
133  gfx::Transform transform() const;
134
135  // Return the target transform if animator is running, or the current
136  // transform otherwise.
137  gfx::Transform GetTargetTransform() const;
138
139  // The bounds, relative to the parent.
140  void SetBounds(const gfx::Rect& bounds);
141  const gfx::Rect& bounds() const { return bounds_; }
142
143  // Return the target bounds if animator is running, or the current bounds
144  // otherwise.
145  gfx::Rect GetTargetBounds() const;
146
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;
151
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);
156
157  // Returns the actual opacity, which the opacity of this layer multipled by
158  // the combined opacity of the parent.
159  float GetCombinedOpacity() const;
160
161  // Blur pixels by this amount in anything below the layer and visible through
162  // the layer.
163  int background_blur() const { return background_blur_radius_; }
164  void SetBackgroundBlur(int blur_radius);
165
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);
171
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);
177
178  // Return the target brightness if animator is running, or the current
179  // brightness otherwise.
180  float GetTargetBrightness() const;
181
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);
187
188  // Return the target grayscale if animator is running, or the current
189  // grayscale otherwise.
190  float GetTargetGrayscale() const;
191
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);
195
196  // Invert the layer.
197  bool layer_inverted() const { return layer_inverted_; }
198  void SetLayerInverted(bool inverted);
199
200  // Return the target opacity if animator is running, or the current opacity
201  // otherwise.
202  float GetTargetOpacity() const;
203
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_; }
211
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_; }
216
217  // Returns the target visibility if the animator is running. Otherwise, it
218  // returns the current visibility.
219  bool GetTargetVisibility() const;
220
221  // Returns true if this Layer is drawn. A Layer is drawn only if all ancestors
222  // are visible.
223  bool IsDrawn() const;
224
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;
228
229  // Converts a point from the coordinates of |source| to the coordinates of
230  // |target|. Necessarily, |source| and |target| must inhabit the same Layer
231  // tree.
232  static void ConvertPointToLayer(const Layer* source,
233                                  const Layer* target,
234                                  gfx::Point* point);
235
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;
241
242  // Converts a ui::Layer's transform to the transform on the corresponding
243  // cc::Layer.
244  static gfx::Transform ConvertTransformToCCTransform(
245      const gfx::Transform& transform,
246      float device_scale_factor);
247
248  // See description in View for details
249  void SetFillsBoundsOpaquely(bool fills_bounds_opaquely);
250  bool fills_bounds_opaquely() const { return fills_bounds_opaquely_; }
251
252  const std::string& name() const { return name_; }
253  void set_name(const std::string& name) { name_ = name; }
254
255  const ui::Texture* texture() const { return texture_.get(); }
256
257  // Assigns a new external texture.  |texture| can be NULL to disable external
258  // updates.
259  void SetExternalTexture(ui::Texture* texture);
260  ui::Texture* external_texture() { return texture_.get(); }
261
262  // Set new TextureMailbox for this layer. Note that |mailbox| may hold a
263  // shared memory resource or an actual mailbox for a texture.
264  void SetTextureMailbox(const cc::TextureMailbox& mailbox,
265                         scoped_ptr<cc::SingleReleaseCallback> release_callback,
266                         float scale_factor);
267  cc::TextureMailbox GetTextureMailbox(float* scale_factor);
268
269  // Begins showing delegated frames from the |frame_provider|.
270  void SetShowDelegatedContent(cc::DelegatedFrameProvider* frame_provider,
271                               gfx::Size frame_size_in_dip);
272
273  bool has_external_content() {
274    return texture_layer_.get() || delegated_renderer_layer_.get();
275  }
276
277  void SetShowPaintedContent();
278
279  // Sets the layer's fill color.  May only be called for LAYER_SOLID_COLOR.
280  void SetColor(SkColor color);
281
282  // Adds |invalid_rect| to the Layer's pending invalid rect and calls
283  // ScheduleDraw(). Returns false if the paint request is ignored.
284  bool SchedulePaint(const gfx::Rect& invalid_rect);
285
286  // Schedules a redraw of the layer tree at the compositor.
287  // Note that this _does not_ invalidate any region of this layer; use
288  // SchedulePaint() for that.
289  void ScheduleDraw();
290
291  // Uses damaged rectangles recorded in |damaged_region_| to invalidate the
292  // |cc_layer_|.
293  void SendDamagedRects();
294
295  const SkRegion& damaged_region() const { return damaged_region_; }
296
297  // Suppresses painting the content by disconnecting |delegate_|.
298  void SuppressPaint();
299
300  // Notifies the layer that the device scale factor has changed.
301  void OnDeviceScaleFactorChanged(float device_scale_factor);
302
303  // Sets whether the layer should scale its content. If true, the canvas will
304  // be scaled in software rendering mode before it is passed to
305  // |LayerDelegate::OnPaintLayer|.
306  // Set to false if the delegate handles scaling.
307  // NOTE: if this is called during |LayerDelegate::OnPaint|, the new value will
308  // not apply to the canvas passed to the pending draw.
309  void set_scale_content(bool scale_content) { scale_content_ = scale_content; }
310
311  // Returns true if the layer scales its content.
312  bool scale_content() const { return scale_content_; }
313
314  // Requets a copy of the layer's output as a texture or bitmap.
315  void RequestCopyOfOutput(scoped_ptr<cc::CopyOutputRequest> request);
316
317  // ContentLayerClient
318  virtual void PaintContents(
319      SkCanvas* canvas, const gfx::Rect& clip, gfx::RectF* opaque) OVERRIDE;
320  virtual void DidChangeLayerCanUseLCDText() OVERRIDE {}
321
322  cc::Layer* cc_layer() { return cc_layer_; }
323
324  // TextureLayerClient
325  virtual unsigned PrepareTexture() OVERRIDE;
326  virtual bool PrepareTextureMailbox(
327      cc::TextureMailbox* mailbox,
328      scoped_ptr<cc::SingleReleaseCallback>* release_callback,
329      bool use_shared_memory) OVERRIDE;
330
331  float device_scale_factor() const { return device_scale_factor_; }
332
333  // Forces a render surface to be used on this layer. This has no positive
334  // impact, and is only used for benchmarking/testing purpose.
335  void SetForceRenderSurface(bool force);
336  bool force_render_surface() const { return force_render_surface_; }
337
338  // LayerClient
339  virtual scoped_refptr<base::debug::ConvertableToTraceFormat>
340      TakeDebugInfo() OVERRIDE;
341
342  // LayerAnimationEventObserver
343  virtual void OnAnimationStarted(const cc::AnimationEvent& event) OVERRIDE;
344
345  // Whether this layer has animations waiting to get sent to its cc::Layer.
346  bool HasPendingThreadedAnimations() {
347    return pending_threaded_animations_.size() != 0;
348  }
349
350  // Triggers a call to SwitchToLayer.
351  void SwitchCCLayerForTest();
352
353 private:
354  friend class LayerOwner;
355
356  // Stacks |child| above or below |other|.  Helper method for StackAbove() and
357  // StackBelow().
358  void StackRelativeTo(Layer* child, Layer* other, bool above);
359
360  bool ConvertPointForAncestor(const Layer* ancestor, gfx::Point* point) const;
361  bool ConvertPointFromAncestor(const Layer* ancestor, gfx::Point* point) const;
362
363  // Implementation of LayerAnimatorDelegate
364  virtual void SetBoundsFromAnimation(const gfx::Rect& bounds) OVERRIDE;
365  virtual void SetTransformFromAnimation(
366      const gfx::Transform& transform) OVERRIDE;
367  virtual void SetOpacityFromAnimation(float opacity) OVERRIDE;
368  virtual void SetVisibilityFromAnimation(bool visibility) OVERRIDE;
369  virtual void SetBrightnessFromAnimation(float brightness) OVERRIDE;
370  virtual void SetGrayscaleFromAnimation(float grayscale) OVERRIDE;
371  virtual void SetColorFromAnimation(SkColor color) OVERRIDE;
372  virtual void ScheduleDrawForAnimation() OVERRIDE;
373  virtual const gfx::Rect& GetBoundsForAnimation() const OVERRIDE;
374  virtual gfx::Transform GetTransformForAnimation() const OVERRIDE;
375  virtual float GetOpacityForAnimation() const OVERRIDE;
376  virtual bool GetVisibilityForAnimation() const OVERRIDE;
377  virtual float GetBrightnessForAnimation() const OVERRIDE;
378  virtual float GetGrayscaleForAnimation() const OVERRIDE;
379  virtual SkColor GetColorForAnimation() const OVERRIDE;
380  virtual float GetDeviceScaleFactor() const OVERRIDE;
381  virtual void AddThreadedAnimation(
382      scoped_ptr<cc::Animation> animation) OVERRIDE;
383  virtual void RemoveThreadedAnimation(int animation_id) OVERRIDE;
384
385  // Creates a corresponding composited layer for |type_|.
386  void CreateWebLayer();
387
388  // Recomputes and sets to |cc_layer_|.
389  void RecomputeCCTransformFromTransform(const gfx::Transform& transform);
390  void RecomputeDrawsContentAndUVRect();
391  void RecomputePosition();
392
393  // Set all filters which got applied to the layer.
394  void SetLayerFilters();
395
396  // Set all filters which got applied to the layer background.
397  void SetLayerBackgroundFilters();
398
399  // Cleanup |cc_layer_| and replaces it with |new_layer|.
400  void SwitchToLayer(scoped_refptr<cc::Layer> new_layer);
401
402  // We cannot send animations to our cc_layer_ until we have been added to a
403  // layer tree. Instead, we hold on to these animations in
404  // pending_threaded_animations_, and expect SendPendingThreadedAnimations to
405  // be called once we have been added to a tree.
406  void SendPendingThreadedAnimations();
407
408  const LayerType type_;
409
410  Compositor* compositor_;
411
412  scoped_refptr<ui::Texture> texture_;
413
414  Layer* parent_;
415
416  // This layer's children, in bottom-to-top stacking order.
417  std::vector<Layer*> children_;
418
419  gfx::Rect bounds_;
420
421  // Visibility of this layer. See SetVisible/IsDrawn for more details.
422  bool visible_;
423
424  bool force_render_surface_;
425
426  bool fills_bounds_opaquely_;
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  LayerOwner* owner_;
460
461  scoped_refptr<LayerAnimator> animator_;
462
463  // Animations that are passed to AddThreadedAnimation before this layer is
464  // added to a tree.
465  cc::ScopedPtrVector<cc::Animation> pending_threaded_animations_;
466
467  // Ownership of the layer is held through one of the strongly typed layer
468  // pointers, depending on which sort of layer this is.
469  scoped_refptr<cc::Layer> content_layer_;
470  scoped_refptr<cc::TextureLayer> texture_layer_;
471  scoped_refptr<cc::SolidColorLayer> solid_color_layer_;
472  scoped_refptr<cc::DelegatedRendererLayer> delegated_renderer_layer_;
473  cc::Layer* cc_layer_;
474
475  // If true, the layer scales the canvas and the texture with the device scale
476  // factor as apporpriate. When true, the texture size is in DIP.
477  bool scale_content_;
478
479  // A cached copy of |Compositor::device_scale_factor()|.
480  float device_scale_factor_;
481
482  // A cached copy of the TextureMailbox given texture_layer_.
483  cc::TextureMailbox mailbox_;
484
485  // Device scale factor in which mailbox_ was rendered in.
486  float mailbox_scale_factor_;
487
488  // The size of the delegated frame in DIP, set when SetShowDelegatedContent
489  // was called.
490  gfx::Size delegated_frame_size_in_dip_;
491
492  DISALLOW_COPY_AND_ASSIGN(Layer);
493};
494
495}  // namespace ui
496
497#endif  // UI_COMPOSITOR_LAYER_H_
498