layer.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
1// Copyright 2010 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 CC_LAYERS_LAYER_H_
6#define CC_LAYERS_LAYER_H_
7
8#include <string>
9#include <vector>
10
11#include "base/memory/ref_counted.h"
12#include "base/observer_list.h"
13#include "cc/animation/layer_animation_controller.h"
14#include "cc/animation/layer_animation_event_observer.h"
15#include "cc/animation/layer_animation_value_observer.h"
16#include "cc/base/cc_export.h"
17#include "cc/base/region.h"
18#include "cc/layers/draw_properties.h"
19#include "cc/layers/render_surface.h"
20#include "cc/trees/occlusion_tracker.h"
21#include "skia/ext/refptr.h"
22#include "third_party/WebKit/Source/Platform/chromium/public/WebFilterOperations.h"
23#include "third_party/skia/include/core/SkColor.h"
24#include "third_party/skia/include/core/SkImageFilter.h"
25#include "ui/gfx/rect.h"
26#include "ui/gfx/rect_f.h"
27#include "ui/gfx/transform.h"
28
29namespace WebKit {
30class WebAnimationDelegate;
31class WebLayerScrollClient;
32}
33
34namespace cc {
35
36class Animation;
37struct AnimationEvent;
38class LayerAnimationDelegate;
39class LayerImpl;
40class LayerTreeHost;
41class LayerTreeImpl;
42class PriorityCalculator;
43class ResourceUpdateQueue;
44class ScrollbarLayer;
45struct AnimationEvent;
46struct RenderingStats;
47
48// Base class for composited layers. Special layer types are derived from
49// this class.
50class CC_EXPORT Layer : public base::RefCounted<Layer>,
51                        public LayerAnimationValueObserver {
52 public:
53  typedef std::vector<scoped_refptr<Layer> > LayerList;
54  enum LayerIdLabels {
55    PINCH_ZOOM_ROOT_SCROLL_LAYER_ID = -2,
56    INVALID_ID = -1,
57  };
58
59  static scoped_refptr<Layer> Create();
60
61  int id() const { return layer_id_; }
62
63  Layer* RootLayer();
64  Layer* parent() { return parent_; }
65  const Layer* parent() const { return parent_; }
66  void AddChild(scoped_refptr<Layer> child);
67  void InsertChild(scoped_refptr<Layer> child, size_t index);
68  void ReplaceChild(Layer* reference, scoped_refptr<Layer> new_layer);
69  void RemoveFromParent();
70  void RemoveAllChildren();
71  void SetChildren(const LayerList& children);
72
73  const LayerList& children() const { return children_; }
74  Layer* child_at(size_t index) { return children_[index].get(); }
75
76  void SetAnchorPoint(gfx::PointF anchor_point);
77  gfx::PointF anchor_point() const { return anchor_point_; }
78
79  void SetAnchorPointZ(float anchor_point_z);
80  float anchor_point_z() const { return anchor_point_z_; }
81
82  virtual void SetBackgroundColor(SkColor background_color);
83  SkColor background_color() const { return background_color_; }
84
85  // A layer's bounds are in logical, non-page-scaled pixels (however, the
86  // root layer's bounds are in physical pixels).
87  void SetBounds(gfx::Size bounds);
88  gfx::Size bounds() const { return bounds_; }
89
90  void SetMasksToBounds(bool masks_to_bounds);
91  bool masks_to_bounds() const { return masks_to_bounds_; }
92
93  void SetMaskLayer(Layer* mask_layer);
94  Layer* mask_layer() { return mask_layer_.get(); }
95  const Layer* mask_layer() const { return mask_layer_.get(); }
96
97  virtual void SetNeedsDisplayRect(const gfx::RectF& dirty_rect);
98  void SetNeedsDisplay() { SetNeedsDisplayRect(gfx::RectF(bounds())); }
99
100  void SetOpacity(float opacity);
101  float opacity() const { return opacity_; }
102  bool OpacityIsAnimating() const;
103  virtual bool OpacityCanAnimateOnImplThread() const;
104
105  void SetFilters(const WebKit::WebFilterOperations& filters);
106  const WebKit::WebFilterOperations& filters() const { return filters_; }
107
108  void SetFilter(const skia::RefPtr<SkImageFilter>& filter);
109  skia::RefPtr<SkImageFilter> filter() const { return filter_; }
110
111  // Background filters are filters applied to what is behind this layer, when
112  // they are viewed through non-opaque regions in this layer. They are used
113  // through the WebLayer interface, and are not exposed to HTML.
114  void SetBackgroundFilters(const WebKit::WebFilterOperations& filters);
115  const WebKit::WebFilterOperations& background_filters() const {
116    return background_filters_;
117  }
118
119  virtual void SetContentsOpaque(bool opaque);
120  bool contents_opaque() const { return contents_opaque_; }
121
122  void SetPosition(gfx::PointF position);
123  gfx::PointF position() const { return position_; }
124
125  void SetIsContainerForFixedPositionLayers(bool container);
126  bool is_container_for_fixed_position_layers() const {
127    return is_container_for_fixed_position_layers_;
128  }
129
130  void SetFixedToContainerLayer(bool fixed_to_container_layer);
131  bool fixed_to_container_layer() const { return fixed_to_container_layer_; }
132
133  void SetSublayerTransform(const gfx::Transform& sublayer_transform);
134  const gfx::Transform& sublayer_transform() const {
135    return sublayer_transform_;
136  }
137
138  void SetTransform(const gfx::Transform& transform);
139  const gfx::Transform& transform() const { return transform_; }
140  bool TransformIsAnimating() const;
141
142  DrawProperties<Layer, RenderSurface>& draw_properties() {
143    return draw_properties_;
144  }
145  const DrawProperties<Layer, RenderSurface>& draw_properties() const {
146    return draw_properties_;
147  }
148
149  // The following are shortcut accessors to get various information from
150  // draw_properties_
151  const gfx::Transform& draw_transform() const {
152    return draw_properties_.target_space_transform;
153  }
154  const gfx::Transform& screen_space_transform() const {
155    return draw_properties_.screen_space_transform;
156  }
157  float draw_opacity() const { return draw_properties_.opacity; }
158  bool draw_opacity_is_animating() const {
159    return draw_properties_.opacity_is_animating;
160  }
161  bool draw_transform_is_animating() const {
162    return draw_properties_.target_space_transform_is_animating;
163  }
164  bool screen_space_transform_is_animating() const {
165    return draw_properties_.screen_space_transform_is_animating;
166  }
167  bool screen_space_opacity_is_animating() const {
168    return draw_properties_.screen_space_opacity_is_animating;
169  }
170  bool can_use_lcd_text() const { return draw_properties_.can_use_lcd_text; }
171  bool is_clipped() const { return draw_properties_.is_clipped; }
172  gfx::Rect clip_rect() const { return draw_properties_.clip_rect; }
173  gfx::Rect drawable_content_rect() const {
174    return draw_properties_.drawable_content_rect;
175  }
176  gfx::Rect visible_content_rect() const {
177    return draw_properties_.visible_content_rect;
178  }
179  Layer* render_target() {
180    DCHECK(!draw_properties_.render_target ||
181           draw_properties_.render_target->render_surface());
182    return draw_properties_.render_target;
183  }
184  const Layer* render_target() const {
185    DCHECK(!draw_properties_.render_target ||
186           draw_properties_.render_target->render_surface());
187    return draw_properties_.render_target;
188  }
189  RenderSurface* render_surface() const {
190    return draw_properties_.render_surface.get();
191  }
192
193  void SetScrollOffset(gfx::Vector2d scroll_offset);
194  gfx::Vector2d scroll_offset() const { return scroll_offset_; }
195
196  void SetMaxScrollOffset(gfx::Vector2d max_scroll_offset);
197  gfx::Vector2d max_scroll_offset() const { return max_scroll_offset_; }
198
199  void SetScrollable(bool scrollable);
200  bool scrollable() const { return scrollable_; }
201
202  void SetShouldScrollOnMainThread(bool should_scroll_on_main_thread);
203  bool should_scroll_on_main_thread() const {
204    return should_scroll_on_main_thread_;
205  }
206
207  void SetHaveWheelEventHandlers(bool have_wheel_event_handlers);
208  bool have_wheel_event_handlers() const { return have_wheel_event_handlers_; }
209
210  void SetNonFastScrollableRegion(const Region& non_fast_scrollable_region);
211  const Region& non_fast_scrollable_region() const {
212    return non_fast_scrollable_region_;
213  }
214
215  void SetTouchEventHandlerRegion(const Region& touch_event_handler_region);
216  const Region& touch_event_handler_region() const {
217    return touch_event_handler_region_;
218  }
219
220  void set_layer_scroll_client(WebKit::WebLayerScrollClient* client) {
221    layer_scroll_client_ = client;
222  }
223
224  void SetDrawCheckerboardForMissingTiles(bool checkerboard);
225  bool DrawCheckerboardForMissingTiles() const {
226    return draw_checkerboard_for_missing_tiles_;
227  }
228
229  void SetForceRenderSurface(bool force_render_surface);
230  bool force_render_surface() const { return force_render_surface_; }
231
232  gfx::Vector2d scroll_delta() const { return gfx::Vector2d(); }
233
234  void SetImplTransform(const gfx::Transform& transform);
235  const gfx::Transform& impl_transform() const { return impl_transform_; }
236
237  void SetDoubleSided(bool double_sided);
238  bool double_sided() const { return double_sided_; }
239
240  void SetPreserves3d(bool preserves_3d) { preserves_3d_ = preserves_3d; }
241  bool preserves_3d() const { return preserves_3d_; }
242
243  void set_use_parent_backface_visibility(bool use) {
244    use_parent_backface_visibility_ = use;
245  }
246  bool use_parent_backface_visibility() const {
247    return use_parent_backface_visibility_;
248  }
249
250  virtual void SetLayerTreeHost(LayerTreeHost* host);
251
252  bool HasDelegatedContent() const { return false; }
253  bool HasContributingDelegatedRenderPasses() const { return false; }
254
255  void SetIsDrawable(bool is_drawable);
256
257  void SetReplicaLayer(Layer* layer);
258  Layer* replica_layer() { return replica_layer_.get(); }
259  const Layer* replica_layer() const { return replica_layer_.get(); }
260
261  bool has_mask() const { return !!mask_layer_; }
262  bool has_replica() const { return !!replica_layer_; }
263  bool replica_has_mask() const {
264    return replica_layer_ && (mask_layer_ || replica_layer_->mask_layer_);
265  }
266
267  // These methods typically need to be overwritten by derived classes.
268  virtual bool DrawsContent() const;
269  virtual void Update(ResourceUpdateQueue* queue,
270                      const OcclusionTracker* occlusion,
271                      RenderingStats* stats) {}
272  virtual bool NeedMoreUpdates();
273  virtual void SetIsMask(bool is_mask) {}
274
275  void SetDebugName(const std::string& debug_name);
276
277  virtual void PushPropertiesTo(LayerImpl* layer);
278
279  void ClearRenderSurface() { draw_properties_.render_surface.reset(); }
280  void CreateRenderSurface();
281
282  // The contents scale converts from logical, non-page-scaled pixels to target
283  // pixels. The contents scale is 1 for the root layer as it is already in
284  // physical pixels. By default contents scale is forced to be 1 except for
285  // subclasses of ContentsScalingLayer.
286  float contents_scale_x() const { return draw_properties_.contents_scale_x; }
287  float contents_scale_y() const { return draw_properties_.contents_scale_y; }
288  gfx::Size content_bounds() const { return draw_properties_.content_bounds; }
289
290  virtual void CalculateContentsScale(float ideal_contents_scale,
291                                      bool animating_transform_to_screen,
292                                      float* contents_scale_x,
293                                      float* contents_scale_y,
294                                      gfx::Size* content_bounds);
295
296  // The scale at which contents should be rastered, to match the scale at
297  // which they will drawn to the screen. This scale is a component of the
298  // contentsScale() but does not include page/device scale factors.
299  void SetRasterScale(float scale);
300  float raster_scale() const { return raster_scale_; }
301
302  // When true, the RasterScale() will be set by the compositor. If false, it
303  // will use whatever value is given to it by the embedder.
304  bool automatically_compute_raster_scale() {
305    return automatically_compute_raster_scale_;
306  }
307  void SetAutomaticallyComputeRasterScale(bool automatic);
308
309  void ForceAutomaticRasterScaleToBeRecomputed();
310
311  // When true, the layer's contents are not scaled by the current page scale
312  // factor. SetBoundsContainPageScale() recursively sets the value on all
313  // child layers.
314  void SetBoundsContainPageScale(bool bounds_contain_page_scale);
315  bool bounds_contain_page_scale() const { return bounds_contain_page_scale_; }
316
317  LayerTreeHost* layer_tree_host() const { return layer_tree_host_; }
318
319  // Set the priority of all desired textures in this layer.
320  virtual void SetTexturePriorities(const PriorityCalculator& priority_calc) {}
321
322  bool AddAnimation(scoped_ptr<Animation> animation);
323  void PauseAnimation(int animation_id, double time_offset);
324  void RemoveAnimation(int animation_id);
325
326  void SuspendAnimations(double monotonic_time);
327  void ResumeAnimations(double monotonic_time);
328
329  LayerAnimationController* layer_animation_controller() {
330    return layer_animation_controller_.get();
331  }
332  void SetLayerAnimationController(
333      scoped_refptr<LayerAnimationController> controller);
334  scoped_refptr<LayerAnimationController> ReleaseLayerAnimationController();
335
336  void set_layer_animation_delegate(WebKit::WebAnimationDelegate* delegate) {
337    layer_animation_delegate_ = delegate;
338  }
339
340  bool HasActiveAnimation() const;
341
342  virtual void NotifyAnimationStarted(const AnimationEvent& event,
343                                      double wall_clock_time);
344  virtual void NotifyAnimationFinished(double wall_clock_time);
345  virtual void NotifyAnimationPropertyUpdate(const AnimationEvent& event);
346
347  void AddLayerAnimationEventObserver(
348      LayerAnimationEventObserver* animation_observer);
349  void RemoveLayerAnimationEventObserver(
350      LayerAnimationEventObserver* animation_observer);
351
352  virtual Region VisibleContentOpaqueRegion() const;
353
354  virtual ScrollbarLayer* ToScrollbarLayer();
355
356  gfx::Rect LayerRectToContentRect(const gfx::RectF& layer_rect) const;
357
358  // In impl-side painting, this returns true if this layer type is not
359  // compatible with the main thread running freely, such as a double-buffered
360  // canvas that doesn't want to be triple-buffered across all three trees.
361  virtual bool BlocksPendingCommit() const;
362  // Returns true if anything in this tree blocksPendingCommit.
363  bool BlocksPendingCommitRecursive() const;
364
365  virtual bool CanClipSelf() const;
366
367  // Constructs a LayerImpl of the correct runtime type for this Layer type.
368  virtual scoped_ptr<LayerImpl> CreateLayerImpl(LayerTreeImpl* tree_impl);
369
370  bool NeedsDisplayForTesting() const { return needs_display_; }
371  void ResetNeedsDisplayForTesting() { needs_display_ = false; }
372
373 protected:
374  friend class LayerImpl;
375  friend class TreeSynchronizer;
376  virtual ~Layer();
377
378  Layer();
379
380  void SetNeedsCommit();
381  void SetNeedsFullTreeSync();
382
383  // This flag is set when layer need repainting/updating.
384  bool needs_display_;
385
386  // Tracks whether this layer may have changed stacking order with its
387  // siblings.
388  bool stacking_order_changed_;
389
390  // The update rect is the region of the compositor resource that was
391  // actually updated by the compositor. For layers that may do updating
392  // outside the compositor's control (i.e. plugin layers), this information
393  // is not available and the update rect will remain empty.
394  // Note this rect is in layer space (not content space).
395  gfx::RectF update_rect_;
396
397  scoped_refptr<Layer> mask_layer_;
398
399  int layer_id_;
400
401  // When true, the layer is about to perform an update. Any commit requests
402  // will be handled implcitly after the update completes.
403  bool ignore_set_needs_commit_;
404
405 private:
406  friend class base::RefCounted<Layer>;
407
408  void SetParent(Layer* layer);
409  bool HasAncestor(Layer* ancestor) const;
410  bool DescendantIsFixedToContainerLayer() const;
411
412  // Returns the index of the child or -1 if not found.
413  int IndexOfChild(const Layer* reference);
414
415  // This should only be called from RemoveFromParent().
416  void RemoveChildOrDependent(Layer* child);
417
418  // LayerAnimationValueObserver implementation.
419  virtual void OnOpacityAnimated(float opacity) OVERRIDE;
420  virtual void OnTransformAnimated(const gfx::Transform& transform) OVERRIDE;
421  virtual bool IsActive() const OVERRIDE;
422
423  LayerList children_;
424  Layer* parent_;
425
426  // Layer instances have a weak pointer to their LayerTreeHost.
427  // This pointer value is nil when a Layer is not in a tree and is
428  // updated via SetLayerTreeHost() if a layer moves between trees.
429  LayerTreeHost* layer_tree_host_;
430
431  ObserverList<LayerAnimationEventObserver> layer_animation_observers_;
432
433  scoped_refptr<LayerAnimationController> layer_animation_controller_;
434
435  // Layer properties.
436  gfx::Size bounds_;
437
438  gfx::Vector2d scroll_offset_;
439  gfx::Vector2d max_scroll_offset_;
440  bool scrollable_;
441  bool should_scroll_on_main_thread_;
442  bool have_wheel_event_handlers_;
443  Region non_fast_scrollable_region_;
444  Region touch_event_handler_region_;
445  gfx::PointF position_;
446  gfx::PointF anchor_point_;
447  SkColor background_color_;
448  std::string debug_name_;
449  float opacity_;
450  skia::RefPtr<SkImageFilter> filter_;
451  WebKit::WebFilterOperations filters_;
452  WebKit::WebFilterOperations background_filters_;
453  float anchor_point_z_;
454  bool is_container_for_fixed_position_layers_;
455  bool fixed_to_container_layer_;
456  bool is_drawable_;
457  bool masks_to_bounds_;
458  bool contents_opaque_;
459  bool double_sided_;
460  bool preserves_3d_;
461  bool use_parent_backface_visibility_;
462  bool draw_checkerboard_for_missing_tiles_;
463  bool force_render_surface_;
464
465  gfx::Transform transform_;
466  gfx::Transform sublayer_transform_;
467
468  // Replica layer used for reflections.
469  scoped_refptr<Layer> replica_layer_;
470
471  // Transient properties.
472  float raster_scale_;
473  bool automatically_compute_raster_scale_;
474  bool bounds_contain_page_scale_;
475
476  gfx::Transform impl_transform_;
477
478  WebKit::WebAnimationDelegate* layer_animation_delegate_;
479  WebKit::WebLayerScrollClient* layer_scroll_client_;
480
481  DrawProperties<Layer, RenderSurface> draw_properties_;
482
483  DISALLOW_COPY_AND_ASSIGN(Layer);
484};
485
486}  // namespace cc
487
488#endif  // CC_LAYERS_LAYER_H_
489