layer.h revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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;
51
52// Layer manages a texture, transform and a set of child Layers. Any View that
53// has enabled layers ends up creating a Layer to manage the texture.
54// A Layer can also be created without a texture, in which case it renders
55// nothing and is simply used as a node in a hierarchy of layers.
56// Coordinate system used in layers is DIP (Density Independent Pixel)
57// coordinates unless explicitly mentioned as pixel coordinates.
58//
59// NOTE: Unlike Views, each Layer does *not* own its child Layers. If you
60// delete a Layer and it has children, the parent of each child Layer is set to
61// NULL, but the children are not deleted.
62class COMPOSITOR_EXPORT Layer
63    : public LayerAnimationDelegate,
64      NON_EXPORTED_BASE(public cc::ContentLayerClient),
65      NON_EXPORTED_BASE(public cc::TextureLayerClient),
66      NON_EXPORTED_BASE(public cc::LayerClient),
67      NON_EXPORTED_BASE(public cc::LayerAnimationEventObserver) {
68 public:
69  Layer();
70  explicit Layer(LayerType type);
71  virtual ~Layer();
72
73  static bool UsingPictureLayer();
74
75  // Retrieves the Layer's compositor. The Layer will walk up its parent chain
76  // to locate it. Returns NULL if the Layer is not attached to a compositor.
77  Compositor* GetCompositor();
78
79  // Called by the compositor when the Layer is set as its root Layer. This can
80  // only ever be called on the root layer.
81  void SetCompositor(Compositor* compositor);
82
83  LayerDelegate* delegate() { return delegate_; }
84  void set_delegate(LayerDelegate* delegate) { delegate_ = delegate; }
85
86  LayerOwner* owner() { return owner_; }
87
88  // Adds a new Layer to this Layer.
89  void Add(Layer* child);
90
91  // Removes a Layer from this Layer.
92  void Remove(Layer* child);
93
94  // Stacks |child| above all other children.
95  void StackAtTop(Layer* child);
96
97  // Stacks |child| directly above |other|.  Both must be children of this
98  // layer.  Note that if |child| is initially stacked even higher, calling this
99  // method will result in |child| being lowered in the stacking order.
100  void StackAbove(Layer* child, Layer* other);
101
102  // Stacks |child| below all other children.
103  void StackAtBottom(Layer* child);
104
105  // Stacks |child| directly below |other|.  Both must be children of this
106  // layer.
107  void StackBelow(Layer* child, Layer* other);
108
109  // Returns the child Layers.
110  const std::vector<Layer*>& children() const { return children_; }
111
112  // The parent.
113  const Layer* parent() const { return parent_; }
114  Layer* parent() { return parent_; }
115
116  LayerType type() const { return type_; }
117
118  // Returns true if this Layer contains |other| somewhere in its children.
119  bool Contains(const Layer* other) const;
120
121  // The layer's animator is responsible for causing automatic animations when
122  // properties are set. It also manages a queue of pending animations and
123  // handles blending of animations. The layer takes ownership of the animator.
124  void SetAnimator(LayerAnimator* animator);
125
126  // Returns the layer's animator. Creates a default animator of one has not
127  // been set. Will not return NULL.
128  LayerAnimator* GetAnimator();
129
130  // The transform, relative to the parent.
131  void SetTransform(const gfx::Transform& transform);
132  gfx::Transform transform() const;
133
134  // Return the target transform if animator is running, or the current
135  // transform otherwise.
136  gfx::Transform GetTargetTransform() const;
137
138  // The bounds, relative to the parent.
139  void SetBounds(const gfx::Rect& bounds);
140  const gfx::Rect& bounds() const { return bounds_; }
141
142  // Return the target bounds if animator is running, or the current bounds
143  // otherwise.
144  gfx::Rect GetTargetBounds() const;
145
146  // Sets/gets whether or not drawing of child layers should be clipped to the
147  // bounds of this layer.
148  void SetMasksToBounds(bool masks_to_bounds);
149  bool GetMasksToBounds() const;
150
151  // The opacity of the layer. The opacity is applied to each pixel of the
152  // texture (resulting alpha = opacity * alpha).
153  float opacity() const;
154  void SetOpacity(float opacity);
155
156  // Returns the actual opacity, which the opacity of this layer multipled by
157  // the combined opacity of the parent.
158  float GetCombinedOpacity() const;
159
160  // Blur pixels by this amount in anything below the layer and visible through
161  // the layer.
162  int background_blur() const { return background_blur_radius_; }
163  void SetBackgroundBlur(int blur_radius);
164
165  // Saturate all pixels of this layer by this amount.
166  // This effect will get "combined" with the inverted,
167  // brightness and grayscale setting.
168  float layer_saturation() const { return layer_saturation_; }
169  void SetLayerSaturation(float saturation);
170
171  // Change the brightness of all pixels from this layer by this amount.
172  // This effect will get "combined" with the inverted, saturate
173  // and grayscale setting.
174  float layer_brightness() const { return layer_brightness_; }
175  void SetLayerBrightness(float brightness);
176
177  // Return the target brightness if animator is running, or the current
178  // brightness otherwise.
179  float GetTargetBrightness() const;
180
181  // Change the grayscale of all pixels from this layer by this amount.
182  // This effect will get "combined" with the inverted, saturate
183  // and brightness setting.
184  float layer_grayscale() const { return layer_grayscale_; }
185  void SetLayerGrayscale(float grayscale);
186
187  // Return the target grayscale if animator is running, or the current
188  // grayscale otherwise.
189  float GetTargetGrayscale() const;
190
191  // Zoom the background by a factor of |zoom|. The effect is blended along the
192  // edge across |inset| pixels.
193  void SetBackgroundZoom(float zoom, int inset);
194
195  // Set the shape of this layer.
196  void SetAlphaShape(scoped_ptr<SkRegion> region);
197
198  // Invert the layer.
199  bool layer_inverted() const { return layer_inverted_; }
200  void SetLayerInverted(bool inverted);
201
202  // Return the target opacity if animator is running, or the current opacity
203  // otherwise.
204  float GetTargetOpacity() const;
205
206  // Set a layer mask for a layer.
207  // Note the provided layer mask can neither have a layer mask itself nor can
208  // it have any children. The ownership of |layer_mask| will not be
209  // transferred with this call.
210  // Furthermore: A mask layer can only be set to one layer.
211  void SetMaskLayer(Layer* layer_mask);
212  Layer* layer_mask_layer() { return layer_mask_; }
213
214  // Sets the visibility of the Layer. A Layer may be visible but not
215  // drawn. This happens if any ancestor of a Layer is not visible.
216  void SetVisible(bool visible);
217  bool visible() const { return visible_; }
218
219  // Returns the target visibility if the animator is running. Otherwise, it
220  // returns the current visibility.
221  bool GetTargetVisibility() const;
222
223  // Returns true if this Layer is drawn. A Layer is drawn only if all ancestors
224  // are visible.
225  bool IsDrawn() const;
226
227  // Returns true if this layer can have a texture (has_texture_ is true)
228  // and is not completely obscured by a child.
229  bool ShouldDraw() const;
230
231  // Converts a point from the coordinates of |source| to the coordinates of
232  // |target|. Necessarily, |source| and |target| must inhabit the same Layer
233  // tree.
234  static void ConvertPointToLayer(const Layer* source,
235                                  const Layer* target,
236                                  gfx::Point* point);
237
238  // Converts a transform to be relative to the given |ancestor|. Returns
239  // whether success (that is, whether the given ancestor was really an
240  // ancestor of this layer).
241  bool GetTargetTransformRelativeTo(const Layer* ancestor,
242                                    gfx::Transform* transform) const;
243
244  // See description in View for details
245  void SetFillsBoundsOpaquely(bool fills_bounds_opaquely);
246  bool fills_bounds_opaquely() const { return fills_bounds_opaquely_; }
247
248  // Set to true if this layer always paints completely within its bounds. If so
249  // we can omit an unnecessary clear, even if the layer is transparent.
250  void SetFillsBoundsCompletely(bool fills_bounds_completely);
251
252  const std::string& name() const { return name_; }
253  void set_name(const std::string& name) { name_ = name; }
254
255  // Set new TextureMailbox for this layer. Note that |mailbox| may hold a
256  // shared memory resource or an actual mailbox for a texture.
257  void SetTextureMailbox(const cc::TextureMailbox& mailbox,
258                         scoped_ptr<cc::SingleReleaseCallback> release_callback,
259                         gfx::Size texture_size_in_dip);
260  void SetTextureSize(gfx::Size texture_size_in_dip);
261
262  // Begins showing delegated frames from the |frame_provider|.
263  void SetShowDelegatedContent(cc::DelegatedFrameProvider* frame_provider,
264                               gfx::Size frame_size_in_dip);
265
266  bool has_external_content() {
267    return texture_layer_.get() || delegated_renderer_layer_.get();
268  }
269
270  void SetShowPaintedContent();
271
272  // Sets the layer's fill color.  May only be called for LAYER_SOLID_COLOR.
273  void SetColor(SkColor color);
274
275  // Adds |invalid_rect| to the Layer's pending invalid rect and calls
276  // ScheduleDraw(). Returns false if the paint request is ignored.
277  bool SchedulePaint(const gfx::Rect& invalid_rect);
278
279  // Schedules a redraw of the layer tree at the compositor.
280  // Note that this _does not_ invalidate any region of this layer; use
281  // SchedulePaint() for that.
282  void ScheduleDraw();
283
284  // Uses damaged rectangles recorded in |damaged_region_| to invalidate the
285  // |cc_layer_|.
286  void SendDamagedRects();
287
288  const SkRegion& damaged_region() const { return damaged_region_; }
289
290  // Suppresses painting the content by disconnecting |delegate_|.
291  void SuppressPaint();
292
293  // Notifies the layer that the device scale factor has changed.
294  void OnDeviceScaleFactorChanged(float device_scale_factor);
295
296  // Requets a copy of the layer's output as a texture or bitmap.
297  void RequestCopyOfOutput(scoped_ptr<cc::CopyOutputRequest> request);
298
299  // ContentLayerClient
300  virtual void PaintContents(
301      SkCanvas* canvas,
302      const gfx::Rect& clip,
303      gfx::RectF* opaque,
304      ContentLayerClient::GraphicsContextStatus gc_status) OVERRIDE;
305  virtual void DidChangeLayerCanUseLCDText() OVERRIDE {}
306  virtual bool FillsBoundsCompletely() const OVERRIDE;
307
308  cc::Layer* cc_layer() { return cc_layer_; }
309
310  // TextureLayerClient
311  virtual bool PrepareTextureMailbox(
312      cc::TextureMailbox* mailbox,
313      scoped_ptr<cc::SingleReleaseCallback>* release_callback,
314      bool use_shared_memory) OVERRIDE;
315
316  float device_scale_factor() const { return device_scale_factor_; }
317
318  // Forces a render surface to be used on this layer. This has no positive
319  // impact, and is only used for benchmarking/testing purpose.
320  void SetForceRenderSurface(bool force);
321  bool force_render_surface() const { return force_render_surface_; }
322
323  // LayerClient
324  virtual scoped_refptr<base::debug::ConvertableToTraceFormat>
325      TakeDebugInfo() OVERRIDE;
326
327  // LayerAnimationEventObserver
328  virtual void OnAnimationStarted(const cc::AnimationEvent& event) OVERRIDE;
329
330  // Whether this layer has animations waiting to get sent to its cc::Layer.
331  bool HasPendingThreadedAnimations() {
332    return pending_threaded_animations_.size() != 0;
333  }
334
335  // Triggers a call to SwitchToLayer.
336  void SwitchCCLayerForTest();
337
338 private:
339  friend class LayerOwner;
340
341  // Stacks |child| above or below |other|.  Helper method for StackAbove() and
342  // StackBelow().
343  void StackRelativeTo(Layer* child, Layer* other, bool above);
344
345  bool ConvertPointForAncestor(const Layer* ancestor, gfx::Point* point) const;
346  bool ConvertPointFromAncestor(const Layer* ancestor, gfx::Point* point) const;
347
348  // Implementation of LayerAnimatorDelegate
349  virtual void SetBoundsFromAnimation(const gfx::Rect& bounds) OVERRIDE;
350  virtual void SetTransformFromAnimation(
351      const gfx::Transform& transform) OVERRIDE;
352  virtual void SetOpacityFromAnimation(float opacity) OVERRIDE;
353  virtual void SetVisibilityFromAnimation(bool visibility) OVERRIDE;
354  virtual void SetBrightnessFromAnimation(float brightness) OVERRIDE;
355  virtual void SetGrayscaleFromAnimation(float grayscale) OVERRIDE;
356  virtual void SetColorFromAnimation(SkColor color) OVERRIDE;
357  virtual void ScheduleDrawForAnimation() OVERRIDE;
358  virtual const gfx::Rect& GetBoundsForAnimation() const OVERRIDE;
359  virtual gfx::Transform GetTransformForAnimation() const OVERRIDE;
360  virtual float GetOpacityForAnimation() const OVERRIDE;
361  virtual bool GetVisibilityForAnimation() const OVERRIDE;
362  virtual float GetBrightnessForAnimation() const OVERRIDE;
363  virtual float GetGrayscaleForAnimation() const OVERRIDE;
364  virtual SkColor GetColorForAnimation() const OVERRIDE;
365  virtual float GetDeviceScaleFactor() const OVERRIDE;
366  virtual void AddThreadedAnimation(
367      scoped_ptr<cc::Animation> animation) OVERRIDE;
368  virtual void RemoveThreadedAnimation(int animation_id) OVERRIDE;
369
370  // Creates a corresponding composited layer for |type_|.
371  void CreateWebLayer();
372
373  // Recomputes and sets to |cc_layer_|.
374  void RecomputeDrawsContentAndUVRect();
375  void RecomputePosition();
376
377  // Set all filters which got applied to the layer.
378  void SetLayerFilters();
379
380  // Set all filters which got applied to the layer background.
381  void SetLayerBackgroundFilters();
382
383  // Cleanup |cc_layer_| and replaces it with |new_layer|.
384  void SwitchToLayer(scoped_refptr<cc::Layer> new_layer);
385
386  // We cannot send animations to our cc_layer_ until we have been added to a
387  // layer tree. Instead, we hold on to these animations in
388  // pending_threaded_animations_, and expect SendPendingThreadedAnimations to
389  // be called once we have been added to a tree.
390  void SendPendingThreadedAnimations();
391
392  const LayerType type_;
393
394  Compositor* compositor_;
395
396  Layer* parent_;
397
398  // This layer's children, in bottom-to-top stacking order.
399  std::vector<Layer*> children_;
400
401  gfx::Rect bounds_;
402
403  // Visibility of this layer. See SetVisible/IsDrawn for more details.
404  bool visible_;
405
406  bool force_render_surface_;
407
408  bool fills_bounds_opaquely_;
409  bool fills_bounds_completely_;
410
411  // Union of damaged rects, in pixel coordinates, to be used when
412  // compositor is ready to paint the content.
413  SkRegion damaged_region_;
414
415  int background_blur_radius_;
416
417  // Several variables which will change the visible representation of
418  // the layer.
419  float layer_saturation_;
420  float layer_brightness_;
421  float layer_grayscale_;
422  bool layer_inverted_;
423
424  // The associated mask layer with this layer.
425  Layer* layer_mask_;
426  // The back link from the mask layer to it's associated masked layer.
427  // We keep this reference for the case that if the mask layer gets deleted
428  // while attached to the main layer before the main layer is deleted.
429  Layer* layer_mask_back_link_;
430
431  // The zoom factor to scale the layer by.  Zooming is disabled when this is
432  // set to 1.
433  float zoom_;
434
435  // Width of the border in pixels, where the scaling is blended.
436  int zoom_inset_;
437
438  // Shape of the window.
439  scoped_ptr<SkRegion> alpha_shape_;
440
441  std::string name_;
442
443  LayerDelegate* delegate_;
444
445  LayerOwner* owner_;
446
447  scoped_refptr<LayerAnimator> animator_;
448
449  // Animations that are passed to AddThreadedAnimation before this layer is
450  // added to a tree.
451  cc::ScopedPtrVector<cc::Animation> pending_threaded_animations_;
452
453  // Ownership of the layer is held through one of the strongly typed layer
454  // pointers, depending on which sort of layer this is.
455  scoped_refptr<cc::Layer> content_layer_;
456  scoped_refptr<cc::TextureLayer> texture_layer_;
457  scoped_refptr<cc::SolidColorLayer> solid_color_layer_;
458  scoped_refptr<cc::DelegatedRendererLayer> delegated_renderer_layer_;
459  cc::Layer* cc_layer_;
460
461  // A cached copy of |Compositor::device_scale_factor()|.
462  float device_scale_factor_;
463
464  // The mailbox used by texture_layer_.
465  cc::TextureMailbox mailbox_;
466
467  // The callback to release the mailbox. This is only set after
468  // SetTextureMailbox is called, before we give it to the TextureLayer.
469  scoped_ptr<cc::SingleReleaseCallback> mailbox_release_callback_;
470
471  // The size of the frame or texture in DIP, set when SetShowDelegatedContent
472  // or SetTextureMailbox was called.
473  gfx::Size frame_size_in_dip_;
474
475  DISALLOW_COPY_AND_ASSIGN(Layer);
476};
477
478}  // namespace ui
479
480#endif  // UI_COMPOSITOR_LAYER_H_
481