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