layer_impl.h revision b2df76ea8fec9e32f6f3718986dba0d95315b29c
1// Copyright 2011 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_IMPL_H_
6#define CC_LAYERS_LAYER_IMPL_H_
7
8#include <string>
9#include <vector>
10
11#include "base/logging.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/values.h"
14#include "cc/animation/layer_animation_controller.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/base/scoped_ptr_vector.h"
19#include "cc/input/input_handler.h"
20#include "cc/layers/draw_properties.h"
21#include "cc/layers/layer_lists.h"
22#include "cc/layers/layer_position_constraint.h"
23#include "cc/layers/render_surface_impl.h"
24#include "cc/quads/render_pass.h"
25#include "cc/quads/shared_quad_state.h"
26#include "cc/resources/resource_provider.h"
27#include "skia/ext/refptr.h"
28#include "third_party/WebKit/Source/Platform/chromium/public/WebFilterOperations.h"
29#include "third_party/skia/include/core/SkColor.h"
30#include "third_party/skia/include/core/SkImageFilter.h"
31#include "third_party/skia/include/core/SkPicture.h"
32#include "ui/gfx/rect.h"
33#include "ui/gfx/rect_f.h"
34#include "ui/gfx/transform.h"
35
36namespace base {
37class DictionaryValue;
38}
39
40namespace cc {
41
42class LayerTreeHostImpl;
43class LayerTreeImpl;
44class QuadSink;
45class Renderer;
46class ScrollbarAnimationController;
47class ScrollbarLayerImpl;
48class Layer;
49
50struct AppendQuadsData;
51
52class CC_EXPORT LayerImpl : LayerAnimationValueObserver {
53 public:
54  static scoped_ptr<LayerImpl> Create(LayerTreeImpl* tree_impl, int id) {
55    return make_scoped_ptr(new LayerImpl(tree_impl, id));
56  }
57
58  virtual ~LayerImpl();
59
60  int id() const { return layer_id_; }
61
62  // LayerAnimationValueObserver implementation.
63  virtual void OnOpacityAnimated(float opacity) OVERRIDE;
64  virtual void OnTransformAnimated(const gfx::Transform& transform) OVERRIDE;
65  virtual bool IsActive() const OVERRIDE;
66
67  // Tree structure.
68  LayerImpl* parent() { return parent_; }
69  const LayerImpl* parent() const { return parent_; }
70  const OwnedLayerImplList& children() const { return children_; }
71  OwnedLayerImplList& children() { return children_; }
72  LayerImpl* child_at(size_t index) const { return children_[index]; }
73  void AddChild(scoped_ptr<LayerImpl> child);
74  scoped_ptr<LayerImpl> RemoveChild(LayerImpl* child);
75  void set_parent(LayerImpl* parent) { parent_ = parent; }
76  // Warning: This does not preserve tree structure invariants.
77  void ClearChildList();
78
79  void PassRequestCopyCallbacks(
80      std::vector<RenderPass::RequestCopyAsBitmapCallback>* callbacks);
81  void TakeRequestCopyCallbacks(
82      std::vector<RenderPass::RequestCopyAsBitmapCallback>* callbacks);
83  bool HasRequestCopyCallback() const {
84    return !request_copy_callbacks_.empty();
85  }
86
87  void SetMaskLayer(scoped_ptr<LayerImpl> mask_layer);
88  LayerImpl* mask_layer() { return mask_layer_.get(); }
89  const LayerImpl* mask_layer() const { return mask_layer_.get(); }
90  scoped_ptr<LayerImpl> TakeMaskLayer();
91
92  void SetReplicaLayer(scoped_ptr<LayerImpl> replica_layer);
93  LayerImpl* replica_layer() { return replica_layer_.get(); }
94  const LayerImpl* replica_layer() const { return replica_layer_.get(); }
95  scoped_ptr<LayerImpl> TakeReplicaLayer();
96
97  bool has_mask() const { return mask_layer_; }
98  bool has_replica() const { return replica_layer_; }
99  bool replica_has_mask() const {
100    return replica_layer_ && (mask_layer_ || replica_layer_->mask_layer_);
101  }
102
103  LayerTreeImpl* layer_tree_impl() const { return layer_tree_impl_; }
104
105  scoped_ptr<SharedQuadState> CreateSharedQuadState() const;
106  // WillDraw must be called before AppendQuads. If WillDraw is called,
107  // DidDraw is guaranteed to be called before another WillDraw or before
108  // the layer is destroyed. To enforce this, any class that overrides
109  // WillDraw/DqidDraw must call the base class version.
110  virtual void WillDraw(ResourceProvider* resource_provider);
111  virtual void AppendQuads(QuadSink* quad_sink,
112                           AppendQuadsData* append_quads_data) {}
113  virtual void DidDraw(ResourceProvider* resource_provider);
114
115  virtual ResourceProvider::ResourceId ContentsResourceId() const;
116
117  virtual bool HasDelegatedContent() const;
118  virtual bool HasContributingDelegatedRenderPasses() const;
119  virtual RenderPass::Id FirstContributingRenderPassId() const;
120  virtual RenderPass::Id NextContributingRenderPassId(RenderPass::Id id) const;
121
122  virtual void UpdateTilePriorities() {}
123
124  virtual ScrollbarLayerImpl* ToScrollbarLayer();
125
126  // Returns true if this layer has content to draw.
127  void SetDrawsContent(bool draws_content);
128  bool DrawsContent() const { return draws_content_; }
129
130  bool force_render_surface() const { return force_render_surface_; }
131  void SetForceRenderSurface(bool force) { force_render_surface_ = force; }
132
133  void SetAnchorPoint(gfx::PointF anchor_point);
134  gfx::PointF anchor_point() const { return anchor_point_; }
135
136  void SetAnchorPointZ(float anchor_point_z);
137  float anchor_point_z() const { return anchor_point_z_; }
138
139  void SetBackgroundColor(SkColor background_color);
140  SkColor background_color() const { return background_color_; }
141
142  void SetFilters(const WebKit::WebFilterOperations& filters);
143  const WebKit::WebFilterOperations& filters() const { return filters_; }
144
145  void SetBackgroundFilters(const WebKit::WebFilterOperations& filters);
146  const WebKit::WebFilterOperations& background_filters() const {
147    return background_filters_;
148  }
149
150  void SetFilter(const skia::RefPtr<SkImageFilter>& filter);
151  skia::RefPtr<SkImageFilter> filter() const { return filter_; }
152
153  void SetMasksToBounds(bool masks_to_bounds);
154  bool masks_to_bounds() const { return masks_to_bounds_; }
155
156  void SetContentsOpaque(bool opaque);
157  bool contents_opaque() const { return contents_opaque_; }
158
159  void SetOpacity(float opacity);
160  float opacity() const { return opacity_; }
161  bool OpacityIsAnimating() const;
162  bool OpacityIsAnimatingOnImplOnly() const;
163
164  void SetPosition(gfx::PointF position);
165  gfx::PointF position() const { return position_; }
166
167  void SetIsContainerForFixedPositionLayers(bool container) {
168    is_container_for_fixed_position_layers_ = container;
169  }
170  // This is a non-trivial function in Layer.
171  bool IsContainerForFixedPositionLayers() const {
172    return is_container_for_fixed_position_layers_;
173  }
174
175  void SetFixedContainerSizeDelta(gfx::Vector2dF delta) {
176    fixed_container_size_delta_ = delta;
177  }
178  gfx::Vector2dF fixed_container_size_delta() const {
179    return fixed_container_size_delta_;
180  }
181
182  void SetPositionConstraint(const LayerPositionConstraint& constraint) {
183    position_constraint_ = constraint;
184  }
185  const LayerPositionConstraint& position_constraint() const {
186    return position_constraint_;
187  }
188
189  void SetPreserves3d(bool preserves_3d);
190  bool preserves_3d() const { return preserves_3d_; }
191
192  void SetUseParentBackfaceVisibility(bool use) {
193    use_parent_backface_visibility_ = use;
194  }
195  bool use_parent_backface_visibility() const {
196    return use_parent_backface_visibility_;
197  }
198
199  void SetSublayerTransform(const gfx::Transform& sublayer_transform);
200  const gfx::Transform& sublayer_transform() const {
201    return sublayer_transform_;
202  }
203
204  // Debug layer name.
205  void SetDebugName(const std::string& debug_name) { debug_name_ = debug_name; }
206  std::string debug_name() const { return debug_name_; }
207
208  bool ShowDebugBorders() const;
209
210  // These invalidate the host's render surface layer list.  The caller
211  // is responsible for calling set_needs_update_draw_properties on the tree
212  // so that its list can be recreated.
213  void CreateRenderSurface();
214  void ClearRenderSurface();
215
216  DrawProperties<LayerImpl, RenderSurfaceImpl>& draw_properties() {
217    return draw_properties_;
218  }
219  const DrawProperties<LayerImpl, RenderSurfaceImpl>& draw_properties() const {
220    return draw_properties_;
221  }
222
223  // The following are shortcut accessors to get various information from
224  // draw_properties_
225  const gfx::Transform& draw_transform() const {
226    return draw_properties_.target_space_transform;
227  }
228  const gfx::Transform& screen_space_transform() const {
229    return draw_properties_.screen_space_transform;
230  }
231  float draw_opacity() const { return draw_properties_.opacity; }
232  bool draw_opacity_is_animating() const {
233    return draw_properties_.opacity_is_animating;
234  }
235  bool draw_transform_is_animating() const {
236    return draw_properties_.target_space_transform_is_animating;
237  }
238  bool screen_space_transform_is_animating() const {
239    return draw_properties_.screen_space_transform_is_animating;
240  }
241  bool screen_space_opacity_is_animating() const {
242    return draw_properties_.screen_space_opacity_is_animating;
243  }
244  bool can_use_lcd_text() const { return draw_properties_.can_use_lcd_text; }
245  bool is_clipped() const { return draw_properties_.is_clipped; }
246  gfx::Rect clip_rect() const { return draw_properties_.clip_rect; }
247  gfx::Rect drawable_content_rect() const {
248    return draw_properties_.drawable_content_rect;
249  }
250  gfx::Rect visible_content_rect() const {
251    return draw_properties_.visible_content_rect;
252  }
253  LayerImpl* render_target() {
254    DCHECK(!draw_properties_.render_target ||
255           draw_properties_.render_target->render_surface());
256    return draw_properties_.render_target;
257  }
258  const LayerImpl* render_target() const {
259    DCHECK(!draw_properties_.render_target ||
260           draw_properties_.render_target->render_surface());
261    return draw_properties_.render_target;
262  }
263  RenderSurfaceImpl* render_surface() const {
264    return draw_properties_.render_surface.get();
265  }
266
267  // The client should be responsible for setting bounds, content bounds and
268  // contents scale to appropriate values. LayerImpl doesn't calculate any of
269  // them from the other values.
270
271  void SetBounds(gfx::Size bounds);
272  gfx::Size bounds() const { return bounds_; }
273
274  void SetContentBounds(gfx::Size content_bounds);
275  gfx::Size content_bounds() const { return draw_properties_.content_bounds; }
276
277  float contents_scale_x() const { return draw_properties_.contents_scale_x; }
278  float contents_scale_y() const { return draw_properties_.contents_scale_y; }
279  void SetContentsScale(float contents_scale_x, float contents_scale_y);
280
281  virtual void CalculateContentsScale(float ideal_contents_scale,
282                                      float device_scale_factor,
283                                      float page_scale_factor,
284                                      bool animating_transform_to_screen,
285                                      float* contents_scale_x,
286                                      float* contents_scale_y,
287                                      gfx::Size* content_bounds);
288
289  void SetScrollOffsetDelegate(
290      LayerScrollOffsetDelegate* scroll_offset_delegate);
291  void SetScrollOffset(gfx::Vector2d scroll_offset);
292  gfx::Vector2d scroll_offset() const { return scroll_offset_; }
293
294  void SetMaxScrollOffset(gfx::Vector2d max_scroll_offset);
295  gfx::Vector2d max_scroll_offset() const { return max_scroll_offset_; }
296
297  void SetScrollDelta(gfx::Vector2dF scroll_delta);
298  gfx::Vector2dF ScrollDelta() const;
299
300  gfx::Vector2dF TotalScrollOffset() const;
301
302  void SetSentScrollDelta(gfx::Vector2d sent_scroll_delta);
303  gfx::Vector2d sent_scroll_delta() const { return sent_scroll_delta_; }
304
305  // Returns the delta of the scroll that was outside of the bounds of the
306  // initial scroll
307  gfx::Vector2dF ScrollBy(gfx::Vector2dF scroll);
308
309  void SetScrollable(bool scrollable) { scrollable_ = scrollable; }
310  bool scrollable() const { return scrollable_; }
311
312  void SetShouldScrollOnMainThread(bool should_scroll_on_main_thread) {
313    should_scroll_on_main_thread_ = should_scroll_on_main_thread;
314  }
315  bool should_scroll_on_main_thread() const {
316    return should_scroll_on_main_thread_;
317  }
318
319  void SetHaveWheelEventHandlers(bool have_wheel_event_handlers) {
320    have_wheel_event_handlers_ = have_wheel_event_handlers;
321  }
322  bool have_wheel_event_handlers() const { return have_wheel_event_handlers_; }
323
324  void SetNonFastScrollableRegion(const Region& region) {
325    non_fast_scrollable_region_ = region;
326  }
327  const Region& non_fast_scrollable_region() const {
328    return non_fast_scrollable_region_;
329  }
330
331  void SetTouchEventHandlerRegion(const Region& region) {
332    touch_event_handler_region_ = region;
333  }
334  const Region& touch_event_handler_region() const {
335    return touch_event_handler_region_;
336  }
337
338  void SetDrawCheckerboardForMissingTiles(bool checkerboard) {
339    draw_checkerboard_for_missing_tiles_ = checkerboard;
340  }
341  bool DrawCheckerboardForMissingTiles() const;
342
343  InputHandler::ScrollStatus TryScroll(
344      gfx::PointF screen_space_point,
345      InputHandler::ScrollInputType type) const;
346
347  void SetDoubleSided(bool double_sided);
348  bool double_sided() const { return double_sided_; }
349
350  void SetTransform(const gfx::Transform& transform);
351  const gfx::Transform& transform() const { return transform_; }
352  bool TransformIsAnimating() const;
353  bool TransformIsAnimatingOnImplOnly() const;
354
355  // Note this rect is in layer space (not content space).
356  void set_update_rect(const gfx::RectF& update_rect) {
357    update_rect_ = update_rect;
358  }
359  const gfx::RectF& update_rect() const { return update_rect_; }
360
361  std::string LayerTreeAsText() const;
362  virtual base::DictionaryValue* LayerTreeAsJson() const;
363
364  void SetStackingOrderChanged(bool stacking_order_changed);
365
366  bool LayerPropertyChanged() const {
367    return layer_property_changed_ || LayerIsAlwaysDamaged();
368  }
369  bool LayerSurfacePropertyChanged() const;
370
371  void ResetAllChangeTrackingForSubtree();
372
373  virtual bool LayerIsAlwaysDamaged() const;
374
375  LayerAnimationController* layer_animation_controller() {
376    return layer_animation_controller_.get();
377  }
378
379  virtual Region VisibleContentOpaqueRegion() const;
380
381  virtual void DidBecomeActive();
382
383  // Indicates that the surface previously used to render this layer
384  // was lost and that a new one has been created. Won't be called
385  // until the new surface has been created successfully.
386  virtual void DidLoseOutputSurface();
387
388  ScrollbarAnimationController* scrollbar_animation_controller() const {
389    return scrollbar_animation_controller_.get();
390  }
391
392  void SetScrollbarOpacity(float opacity);
393
394  void SetHorizontalScrollbarLayer(ScrollbarLayerImpl* scrollbar_layer);
395  ScrollbarLayerImpl* horizontal_scrollbar_layer() {
396    return horizontal_scrollbar_layer_;
397  }
398
399  void SetVerticalScrollbarLayer(ScrollbarLayerImpl* scrollbar_layer);
400  ScrollbarLayerImpl* vertical_scrollbar_layer() {
401    return vertical_scrollbar_layer_;
402  }
403
404  gfx::Rect LayerRectToContentRect(const gfx::RectF& layer_rect) const;
405
406  virtual skia::RefPtr<SkPicture> GetPicture();
407
408  virtual bool CanClipSelf() const;
409
410  virtual bool AreVisibleResourcesReady() const;
411
412  virtual scoped_ptr<LayerImpl> CreateLayerImpl(LayerTreeImpl* tree_impl);
413  virtual void PushPropertiesTo(LayerImpl* layer);
414
415  scoped_ptr<base::Value> AsValue() const;
416
417 protected:
418  LayerImpl(LayerTreeImpl* layer_impl, int id);
419
420  // Get the color and size of the layer's debug border.
421  virtual void GetDebugBorderProperties(SkColor* color, float* width) const;
422
423  void AppendDebugBorderQuad(QuadSink* quad_sink,
424                             const SharedQuadState* shared_quad_state,
425                             AppendQuadsData* append_quads_data) const;
426
427  virtual void DumpLayerProperties(std::string* str, int indent) const;
428  static std::string IndentString(int indent);
429
430  virtual void AsValueInto(base::DictionaryValue* dict) const;
431
432  void NoteLayerSurfacePropertyChanged();
433  void NoteLayerPropertyChanged();
434  void NoteLayerPropertyChangedForSubtree();
435
436  // Note carefully this does not affect the current layer.
437  void NoteLayerPropertyChangedForDescendants();
438
439 private:
440  void UpdateScrollbarPositions();
441
442  virtual const char* LayerTypeAsString() const;
443
444  void DumpLayer(std::string* str, int indent) const;
445
446  // Properties internal to LayerImpl
447  LayerImpl* parent_;
448  OwnedLayerImplList children_;
449  // mask_layer_ can be temporarily stolen during tree sync, we need this ID to
450  // confirm newly assigned layer is still the previous one
451  int mask_layer_id_;
452  scoped_ptr<LayerImpl> mask_layer_;
453  int replica_layer_id_;  // ditto
454  scoped_ptr<LayerImpl> replica_layer_;
455  int layer_id_;
456  LayerTreeImpl* layer_tree_impl_;
457
458  // Properties synchronized from the associated Layer.
459  gfx::PointF anchor_point_;
460  float anchor_point_z_;
461  gfx::Size bounds_;
462  gfx::Vector2d scroll_offset_;
463  LayerScrollOffsetDelegate* scroll_offset_delegate_;
464  bool scrollable_;
465  bool should_scroll_on_main_thread_;
466  bool have_wheel_event_handlers_;
467  Region non_fast_scrollable_region_;
468  Region touch_event_handler_region_;
469  SkColor background_color_;
470  bool stacking_order_changed_;
471
472  // Whether the "back" of this layer should draw.
473  bool double_sided_;
474
475  // Tracks if drawing-related properties have changed since last redraw.
476  bool layer_property_changed_;
477
478  // Indicates that a property has changed on this layer that would not
479  // affect the pixels on its target surface, but would require redrawing
480  // the target_surface onto its ancestor target_surface.
481  // For layers that do not own a surface this flag acts as
482  // layer_property_changed_.
483  bool layer_surface_property_changed_;
484
485  bool masks_to_bounds_;
486  bool contents_opaque_;
487  float opacity_;
488  gfx::PointF position_;
489  bool preserves_3d_;
490  bool use_parent_backface_visibility_;
491  bool draw_checkerboard_for_missing_tiles_;
492  gfx::Transform sublayer_transform_;
493  gfx::Transform transform_;
494
495  bool draws_content_;
496  bool force_render_surface_;
497
498  // Set for the layer that other layers are fixed to.
499  bool is_container_for_fixed_position_layers_;
500  // This property is effective when
501  // is_container_for_fixed_position_layers_ == true,
502  gfx::Vector2dF fixed_container_size_delta_;
503
504  LayerPositionConstraint position_constraint_;
505
506  gfx::Vector2dF scroll_delta_;
507  gfx::Vector2d sent_scroll_delta_;
508  gfx::Vector2d max_scroll_offset_;
509  gfx::Vector2dF last_scroll_offset_;
510
511  // The global depth value of the center of the layer. This value is used
512  // to sort layers from back to front.
513  float draw_depth_;
514
515  // Debug layer name.
516  std::string debug_name_;
517
518  WebKit::WebFilterOperations filters_;
519  WebKit::WebFilterOperations background_filters_;
520  skia::RefPtr<SkImageFilter> filter_;
521
522#ifndef NDEBUG
523  bool between_will_draw_and_did_draw_;
524#endif
525
526  // Rect indicating what was repainted/updated during update.
527  // Note that plugin layers bypass this and leave it empty.
528  // Uses layer's content space.
529  gfx::RectF update_rect_;
530
531  // Manages animations for this layer.
532  scoped_refptr<LayerAnimationController> layer_animation_controller_;
533
534  // Manages scrollbars for this layer
535  scoped_ptr<ScrollbarAnimationController> scrollbar_animation_controller_;
536
537  // Weak pointers to this layer's scrollbars, if it has them. Updated during
538  // tree synchronization.
539  ScrollbarLayerImpl* horizontal_scrollbar_layer_;
540  ScrollbarLayerImpl* vertical_scrollbar_layer_;
541
542  std::vector<RenderPass::RequestCopyAsBitmapCallback> request_copy_callbacks_;
543
544  // Group of properties that need to be computed based on the layer tree
545  // hierarchy before layers can be drawn.
546  DrawProperties<LayerImpl, RenderSurfaceImpl> draw_properties_;
547
548  DISALLOW_COPY_AND_ASSIGN(LayerImpl);
549};
550
551}  // namespace cc
552
553#endif  // CC_LAYERS_LAYER_IMPL_H_
554