Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / cc / layers / layer.h
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 <set>
9 #include <string>
10
11 #include "base/callback.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/observer_list.h"
14 #include "cc/animation/layer_animation_controller.h"
15 #include "cc/animation/layer_animation_value_observer.h"
16 #include "cc/animation/layer_animation_value_provider.h"
17 #include "cc/base/cc_export.h"
18 #include "cc/base/region.h"
19 #include "cc/base/scoped_ptr_vector.h"
20 #include "cc/debug/micro_benchmark.h"
21 #include "cc/layers/draw_properties.h"
22 #include "cc/layers/layer_lists.h"
23 #include "cc/layers/layer_position_constraint.h"
24 #include "cc/layers/paint_properties.h"
25 #include "cc/layers/render_surface.h"
26 #include "cc/output/filter_operations.h"
27 #include "cc/trees/occlusion_tracker.h"
28 #include "skia/ext/refptr.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 "third_party/skia/include/core/SkXfermode.h"
33 #include "ui/gfx/rect.h"
34 #include "ui/gfx/rect_f.h"
35 #include "ui/gfx/transform.h"
36
37 namespace gfx {
38 class BoxF;
39 }
40
41 namespace base {
42 namespace debug {
43 class ConvertableToTraceFormat;
44 }
45 }
46
47 namespace cc {
48
49 class Animation;
50 class AnimationDelegate;
51 struct AnimationEvent;
52 class CopyOutputRequest;
53 class LayerAnimationDelegate;
54 class LayerAnimationEventObserver;
55 class LayerClient;
56 class LayerImpl;
57 class LayerTreeHost;
58 class LayerTreeImpl;
59 class PriorityCalculator;
60 class RenderingStatsInstrumentation;
61 class ResourceUpdateQueue;
62 class ScrollbarLayerInterface;
63 struct AnimationEvent;
64
65 // Base class for composited layers. Special layer types are derived from
66 // this class.
67 class CC_EXPORT Layer : public base::RefCounted<Layer>,
68                         public LayerAnimationValueObserver,
69                         public LayerAnimationValueProvider {
70  public:
71   typedef RenderSurfaceLayerList RenderSurfaceListType;
72   typedef LayerList LayerListType;
73   typedef RenderSurface RenderSurfaceType;
74
75   enum LayerIdLabels {
76     INVALID_ID = -1,
77   };
78
79   static scoped_refptr<Layer> Create();
80
81   int id() const { return layer_id_; }
82
83   Layer* RootLayer();
84   Layer* parent() { return parent_; }
85   const Layer* parent() const { return parent_; }
86   void AddChild(scoped_refptr<Layer> child);
87   void InsertChild(scoped_refptr<Layer> child, size_t index);
88   void ReplaceChild(Layer* reference, scoped_refptr<Layer> new_layer);
89   void RemoveFromParent();
90   void RemoveAllChildren();
91   void SetChildren(const LayerList& children);
92   bool HasAncestor(const Layer* ancestor) const;
93
94   const LayerList& children() const { return children_; }
95   Layer* child_at(size_t index) { return children_[index].get(); }
96
97   // This requests the layer and its subtree be rendered and given to the
98   // callback. If the copy is unable to be produced (the layer is destroyed
99   // first), then the callback is called with a NULL/empty result.
100   void RequestCopyOfOutput(scoped_ptr<CopyOutputRequest> request);
101   bool HasCopyRequest() const {
102     return !copy_requests_.empty();
103   }
104
105   void SetAnchorPoint(const gfx::PointF& anchor_point);
106   gfx::PointF anchor_point() const { return anchor_point_; }
107
108   void SetAnchorPointZ(float anchor_point_z);
109   float anchor_point_z() const { return anchor_point_z_; }
110
111   virtual void SetBackgroundColor(SkColor background_color);
112   SkColor background_color() const { return background_color_; }
113   // If contents_opaque(), return an opaque color else return a
114   // non-opaque color.  Tries to return background_color(), if possible.
115   SkColor SafeOpaqueBackgroundColor() const;
116
117   // A layer's bounds are in logical, non-page-scaled pixels (however, the
118   // root layer's bounds are in physical pixels).
119   void SetBounds(gfx::Size bounds);
120   gfx::Size bounds() const { return bounds_; }
121
122   void SetMasksToBounds(bool masks_to_bounds);
123   bool masks_to_bounds() const { return masks_to_bounds_; }
124
125   void SetMaskLayer(Layer* mask_layer);
126   Layer* mask_layer() { return mask_layer_.get(); }
127   const Layer* mask_layer() const { return mask_layer_.get(); }
128
129   virtual void SetNeedsDisplayRect(const gfx::RectF& dirty_rect);
130   void SetNeedsDisplay() { SetNeedsDisplayRect(gfx::RectF(bounds())); }
131
132   void SetOpacity(float opacity);
133   float opacity() const { return opacity_; }
134   bool OpacityIsAnimating() const;
135   virtual bool OpacityCanAnimateOnImplThread() const;
136
137   void SetBlendMode(SkXfermode::Mode blend_mode);
138   SkXfermode::Mode blend_mode() const { return blend_mode_; }
139
140   bool uses_default_blend_mode() const {
141     return blend_mode_ == SkXfermode::kSrcOver_Mode;
142   }
143
144   // A layer is root for an isolated group when it and all its descendants are
145   // drawn over a black and fully transparent background, creating an isolated
146   // group. It should be used along with SetBlendMode(), in order to restrict
147   // layers within the group to blend with layers outside this group.
148   void SetIsRootForIsolatedGroup(bool root);
149   bool is_root_for_isolated_group() const {
150     return is_root_for_isolated_group_;
151   }
152
153   void SetFilters(const FilterOperations& filters);
154   const FilterOperations& filters() const { return filters_; }
155   bool FilterIsAnimating() const;
156
157   // Background filters are filters applied to what is behind this layer, when
158   // they are viewed through non-opaque regions in this layer. They are used
159   // through the WebLayer interface, and are not exposed to HTML.
160   void SetBackgroundFilters(const FilterOperations& filters);
161   const FilterOperations& background_filters() const {
162     return background_filters_;
163   }
164
165   virtual void SetContentsOpaque(bool opaque);
166   bool contents_opaque() const { return contents_opaque_; }
167
168   void SetPosition(const gfx::PointF& position);
169   gfx::PointF position() const { return position_; }
170
171   void SetIsContainerForFixedPositionLayers(bool container);
172   bool IsContainerForFixedPositionLayers() const;
173
174   void SetPositionConstraint(const LayerPositionConstraint& constraint);
175   const LayerPositionConstraint& position_constraint() const {
176     return position_constraint_;
177   }
178
179   void SetSublayerTransform(const gfx::Transform& sublayer_transform);
180   const gfx::Transform& sublayer_transform() const {
181     return sublayer_transform_;
182   }
183
184   void SetTransform(const gfx::Transform& transform);
185   const gfx::Transform& transform() const { return transform_; }
186   bool TransformIsAnimating() const;
187
188   void SetScrollParent(Layer* parent);
189
190   Layer* scroll_parent() { return scroll_parent_; }
191   const Layer* scroll_parent() const { return scroll_parent_; }
192
193   void AddScrollChild(Layer* child);
194   void RemoveScrollChild(Layer* child);
195
196   std::set<Layer*>* scroll_children() { return scroll_children_.get(); }
197   const std::set<Layer*>* scroll_children() const {
198     return scroll_children_.get();
199   }
200
201   void SetClipParent(Layer* ancestor);
202
203   Layer* clip_parent() { return clip_parent_; }
204   const Layer* clip_parent() const {
205     return clip_parent_;
206   }
207
208   void AddClipChild(Layer* child);
209   void RemoveClipChild(Layer* child);
210
211   std::set<Layer*>* clip_children() { return clip_children_.get(); }
212   const std::set<Layer*>* clip_children() const {
213     return clip_children_.get();
214   }
215
216   DrawProperties<Layer>& draw_properties() { return draw_properties_; }
217   const DrawProperties<Layer>& draw_properties() const {
218     return draw_properties_;
219   }
220
221   // The following are shortcut accessors to get various information from
222   // draw_properties_
223   const gfx::Transform& draw_transform() const {
224     return draw_properties_.target_space_transform;
225   }
226   const gfx::Transform& screen_space_transform() const {
227     return draw_properties_.screen_space_transform;
228   }
229   float draw_opacity() const { return draw_properties_.opacity; }
230   bool draw_opacity_is_animating() const {
231     return draw_properties_.opacity_is_animating;
232   }
233   bool draw_transform_is_animating() const {
234     return draw_properties_.target_space_transform_is_animating;
235   }
236   bool screen_space_transform_is_animating() const {
237     return draw_properties_.screen_space_transform_is_animating;
238   }
239   bool screen_space_opacity_is_animating() const {
240     return draw_properties_.screen_space_opacity_is_animating;
241   }
242   bool can_use_lcd_text() const { return draw_properties_.can_use_lcd_text; }
243   bool is_clipped() const { return draw_properties_.is_clipped; }
244   gfx::Rect clip_rect() const { return draw_properties_.clip_rect; }
245   gfx::Rect drawable_content_rect() const {
246     return draw_properties_.drawable_content_rect;
247   }
248   gfx::Rect visible_content_rect() const {
249     return draw_properties_.visible_content_rect;
250   }
251   Layer* render_target() {
252     DCHECK(!draw_properties_.render_target ||
253            draw_properties_.render_target->render_surface());
254     return draw_properties_.render_target;
255   }
256   const Layer* render_target() const {
257     DCHECK(!draw_properties_.render_target ||
258            draw_properties_.render_target->render_surface());
259     return draw_properties_.render_target;
260   }
261   RenderSurface* render_surface() const {
262     return draw_properties_.render_surface.get();
263   }
264   int num_unclipped_descendants() const {
265     return draw_properties_.num_unclipped_descendants;
266   }
267
268   void SetScrollOffset(gfx::Vector2d scroll_offset);
269   gfx::Vector2d scroll_offset() const { return scroll_offset_; }
270   void SetScrollOffsetFromImplSide(gfx::Vector2d scroll_offset);
271
272   void SetMaxScrollOffset(gfx::Vector2d max_scroll_offset);
273   gfx::Vector2d max_scroll_offset() const { return max_scroll_offset_; }
274
275   void SetScrollable(bool scrollable);
276   bool scrollable() const { return scrollable_; }
277
278   void SetUserScrollable(bool horizontal, bool vertical);
279   bool user_scrollable_horizontal() const {
280     return user_scrollable_horizontal_;
281   }
282   bool user_scrollable_vertical() const { return user_scrollable_vertical_; }
283
284   void SetShouldScrollOnMainThread(bool should_scroll_on_main_thread);
285   bool should_scroll_on_main_thread() const {
286     return should_scroll_on_main_thread_;
287   }
288
289   void SetHaveWheelEventHandlers(bool have_wheel_event_handlers);
290   bool have_wheel_event_handlers() const { return have_wheel_event_handlers_; }
291
292   void SetNonFastScrollableRegion(const Region& non_fast_scrollable_region);
293   const Region& non_fast_scrollable_region() const {
294     return non_fast_scrollable_region_;
295   }
296
297   void SetTouchEventHandlerRegion(const Region& touch_event_handler_region);
298   const Region& touch_event_handler_region() const {
299     return touch_event_handler_region_;
300   }
301
302   void set_did_scroll_callback(const base::Closure& callback) {
303     did_scroll_callback_ = callback;
304   }
305
306   void SetDrawCheckerboardForMissingTiles(bool checkerboard);
307   bool DrawCheckerboardForMissingTiles() const {
308     return draw_checkerboard_for_missing_tiles_;
309   }
310
311   void SetForceRenderSurface(bool force_render_surface);
312   bool force_render_surface() const { return force_render_surface_; }
313
314   gfx::Vector2d ScrollDelta() const { return gfx::Vector2d(); }
315   gfx::Vector2dF TotalScrollOffset() const {
316     // Floating point to match the LayerImpl version.
317     return scroll_offset() + ScrollDelta();
318   }
319
320   void SetDoubleSided(bool double_sided);
321   bool double_sided() const { return double_sided_; }
322
323   void SetPreserves3d(bool preserves_3d) { preserves_3d_ = preserves_3d; }
324   bool preserves_3d() const { return preserves_3d_; }
325
326   void set_use_parent_backface_visibility(bool use) {
327     use_parent_backface_visibility_ = use;
328   }
329   bool use_parent_backface_visibility() const {
330     return use_parent_backface_visibility_;
331   }
332
333   virtual void SetLayerTreeHost(LayerTreeHost* host);
334
335   bool HasDelegatedContent() const { return false; }
336   bool HasContributingDelegatedRenderPasses() const { return false; }
337
338   void SetIsDrawable(bool is_drawable);
339
340   void SetHideLayerAndSubtree(bool hide);
341   bool hide_layer_and_subtree() const { return hide_layer_and_subtree_; }
342
343   void SetReplicaLayer(Layer* layer);
344   Layer* replica_layer() { return replica_layer_.get(); }
345   const Layer* replica_layer() const { return replica_layer_.get(); }
346
347   bool has_mask() const { return !!mask_layer_.get(); }
348   bool has_replica() const { return !!replica_layer_.get(); }
349   bool replica_has_mask() const {
350     return replica_layer_.get() &&
351            (mask_layer_.get() || replica_layer_->mask_layer_.get());
352   }
353
354   // These methods typically need to be overwritten by derived classes.
355   virtual bool DrawsContent() const;
356   virtual void SavePaintProperties();
357   // Returns true iff any resources were updated that need to be committed.
358   virtual bool Update(ResourceUpdateQueue* queue,
359                       const OcclusionTracker* occlusion);
360   virtual bool NeedMoreUpdates();
361   virtual void SetIsMask(bool is_mask) {}
362   virtual void ReduceMemoryUsage() {}
363   virtual void OnOutputSurfaceCreated() {}
364
365   virtual scoped_refptr<base::debug::ConvertableToTraceFormat> TakeDebugInfo();
366
367   void SetLayerClient(LayerClient* client) { client_ = client; }
368
369   virtual void PushPropertiesTo(LayerImpl* layer);
370
371   void CreateRenderSurface();
372   void ClearRenderSurface();
373
374   // The contents scale converts from logical, non-page-scaled pixels to target
375   // pixels. The contents scale is 1 for the root layer as it is already in
376   // physical pixels. By default contents scale is forced to be 1 except for
377   // subclasses of ContentsScalingLayer.
378   float contents_scale_x() const { return draw_properties_.contents_scale_x; }
379   float contents_scale_y() const { return draw_properties_.contents_scale_y; }
380   gfx::Size content_bounds() const { return draw_properties_.content_bounds; }
381
382   virtual void CalculateContentsScale(float ideal_contents_scale,
383                                       float device_scale_factor,
384                                       float page_scale_factor,
385                                       bool animating_transform_to_screen,
386                                       float* contents_scale_x,
387                                       float* contents_scale_y,
388                                       gfx::Size* content_bounds);
389
390   LayerTreeHost* layer_tree_host() { return layer_tree_host_; }
391   const LayerTreeHost* layer_tree_host() const { return layer_tree_host_; }
392
393   // Set the priority of all desired textures in this layer.
394   virtual void SetTexturePriorities(const PriorityCalculator& priority_calc) {}
395
396   bool AddAnimation(scoped_ptr<Animation> animation);
397   void PauseAnimation(int animation_id, double time_offset);
398   void RemoveAnimation(int animation_id);
399
400   LayerAnimationController* layer_animation_controller() {
401     return layer_animation_controller_.get();
402   }
403   void SetLayerAnimationControllerForTest(
404       scoped_refptr<LayerAnimationController> controller);
405
406   void set_layer_animation_delegate(AnimationDelegate* delegate) {
407     layer_animation_controller_->set_layer_animation_delegate(delegate);
408   }
409
410   bool HasActiveAnimation() const;
411
412   void AddLayerAnimationEventObserver(
413       LayerAnimationEventObserver* animation_observer);
414   void RemoveLayerAnimationEventObserver(
415       LayerAnimationEventObserver* animation_observer);
416
417   virtual Region VisibleContentOpaqueRegion() const;
418
419   virtual ScrollbarLayerInterface* ToScrollbarLayer();
420
421   gfx::Rect LayerRectToContentRect(const gfx::RectF& layer_rect) const;
422
423   virtual skia::RefPtr<SkPicture> GetPicture() const;
424
425   // Constructs a LayerImpl of the correct runtime type for this Layer type.
426   virtual scoped_ptr<LayerImpl> CreateLayerImpl(LayerTreeImpl* tree_impl);
427
428   bool NeedsDisplayForTesting() const { return !update_rect_.IsEmpty(); }
429   void ResetNeedsDisplayForTesting() { update_rect_ = gfx::RectF(); }
430
431   RenderingStatsInstrumentation* rendering_stats_instrumentation() const;
432
433   const PaintProperties& paint_properties() const {
434     return paint_properties_;
435   }
436
437   // The scale at which contents should be rastered, to match the scale at
438   // which they will drawn to the screen. This scale is a component of the
439   // contents scale but does not include page/device scale factors.
440   // TODO(danakj): This goes away when TiledLayer goes away.
441   void set_raster_scale(float scale) { raster_scale_ = scale; }
442   float raster_scale() const { return raster_scale_; }
443   bool raster_scale_is_unknown() const { return raster_scale_ == 0.f; }
444
445   virtual bool SupportsLCDText() const;
446
447   bool needs_push_properties() const { return needs_push_properties_; }
448   bool descendant_needs_push_properties() const {
449     return num_dependents_need_push_properties_ > 0;
450   }
451
452   virtual void RunMicroBenchmark(MicroBenchmark* benchmark);
453
454  protected:
455   friend class LayerImpl;
456   friend class TreeSynchronizer;
457   virtual ~Layer();
458
459   Layer();
460
461   // These SetNeeds functions are in order of severity of update:
462   //
463   // Called when this layer has been modified in some way, but isn't sure
464   // that it needs a commit yet.  It needs CalcDrawProperties and UpdateLayers
465   // before it knows whether or not a commit is required.
466   void SetNeedsUpdate();
467   // Called when a property has been modified in a way that the layer
468   // knows immediately that a commit is required.  This implies SetNeedsUpdate
469   // as well as SetNeedsPushProperties to push that property.
470   void SetNeedsCommit();
471   // Called when there's been a change in layer structure.  Implies both
472   // SetNeedsUpdate and SetNeedsCommit, but not SetNeedsPushProperties.
473   void SetNeedsFullTreeSync();
474
475   // Called when the next commit should wait until the pending tree is activated
476   // before finishing the commit and unblocking the main thread. Used to ensure
477   // unused resources on the impl thread are returned before commit completes.
478   void SetNextCommitWaitsForActivation();
479
480   // Called when the blend mode or filters have been changed.
481   void SetNeedsFilterContextIfNeeded();
482
483   void SetNeedsPushProperties();
484   void AddDependentNeedsPushProperties();
485   void RemoveDependentNeedsPushProperties();
486   bool parent_should_know_need_push_properties() const {
487     return needs_push_properties() || descendant_needs_push_properties();
488   }
489
490   bool IsPropertyChangeAllowed() const;
491
492   // If this layer has a scroll parent, it removes |this| from its list of
493   // scroll children.
494   void RemoveFromScrollTree();
495
496   // If this layer has a clip parent, it removes |this| from its list of clip
497   // children.
498   void RemoveFromClipTree();
499
500   void reset_raster_scale_to_unknown() { raster_scale_ = 0.f; }
501
502   // This flag is set when the layer needs to push properties to the impl
503   // side.
504   bool needs_push_properties_;
505
506   // The number of direct children or dependent layers that need to be recursed
507   // to in order for them or a descendent of them to push properties to the impl
508   // side.
509   int num_dependents_need_push_properties_;
510
511   // Tracks whether this layer may have changed stacking order with its
512   // siblings.
513   bool stacking_order_changed_;
514
515   // The update rect is the region of the compositor resource that was
516   // actually updated by the compositor. For layers that may do updating
517   // outside the compositor's control (i.e. plugin layers), this information
518   // is not available and the update rect will remain empty.
519   // Note this rect is in layer space (not content space).
520   gfx::RectF update_rect_;
521
522   scoped_refptr<Layer> mask_layer_;
523
524   int layer_id_;
525
526   // When true, the layer is about to perform an update. Any commit requests
527   // will be handled implicitly after the update completes.
528   bool ignore_set_needs_commit_;
529
530  private:
531   friend class base::RefCounted<Layer>;
532
533   void SetParent(Layer* layer);
534   bool DescendantIsFixedToContainerLayer() const;
535
536   // Returns the index of the child or -1 if not found.
537   int IndexOfChild(const Layer* reference);
538
539   // This should only be called from RemoveFromParent().
540   void RemoveChildOrDependent(Layer* child);
541
542   // LayerAnimationValueProvider implementation.
543   virtual gfx::Vector2dF ScrollOffsetForAnimation() const OVERRIDE;
544
545   // LayerAnimationValueObserver implementation.
546   virtual void OnFilterAnimated(const FilterOperations& filters) OVERRIDE;
547   virtual void OnOpacityAnimated(float opacity) OVERRIDE;
548   virtual void OnTransformAnimated(const gfx::Transform& transform) OVERRIDE;
549   virtual void OnScrollOffsetAnimated(
550       const gfx::Vector2dF& scroll_offset) OVERRIDE;
551   virtual void OnAnimationWaitingForDeletion() OVERRIDE;
552   virtual bool IsActive() const OVERRIDE;
553
554   LayerList children_;
555   Layer* parent_;
556
557   // Layer instances have a weak pointer to their LayerTreeHost.
558   // This pointer value is nil when a Layer is not in a tree and is
559   // updated via SetLayerTreeHost() if a layer moves between trees.
560   LayerTreeHost* layer_tree_host_;
561
562   scoped_refptr<LayerAnimationController> layer_animation_controller_;
563
564   // Layer properties.
565   gfx::Size bounds_;
566
567   gfx::Vector2d scroll_offset_;
568   gfx::Vector2d max_scroll_offset_;
569   bool scrollable_ : 1;
570   bool should_scroll_on_main_thread_ : 1;
571   bool have_wheel_event_handlers_ : 1;
572   bool user_scrollable_horizontal_ : 1;
573   bool user_scrollable_vertical_ : 1;
574   bool is_root_for_isolated_group_ : 1;
575   bool is_container_for_fixed_position_layers_ : 1;
576   bool is_drawable_ : 1;
577   bool hide_layer_and_subtree_ : 1;
578   bool masks_to_bounds_ : 1;
579   bool contents_opaque_ : 1;
580   bool double_sided_ : 1;
581   bool preserves_3d_ : 1;
582   bool use_parent_backface_visibility_ : 1;
583   bool draw_checkerboard_for_missing_tiles_ : 1;
584   bool force_render_surface_ : 1;
585   Region non_fast_scrollable_region_;
586   Region touch_event_handler_region_;
587   gfx::PointF position_;
588   gfx::PointF anchor_point_;
589   SkColor background_color_;
590   float opacity_;
591   SkXfermode::Mode blend_mode_;
592   FilterOperations filters_;
593   FilterOperations background_filters_;
594   float anchor_point_z_;
595   LayerPositionConstraint position_constraint_;
596   Layer* scroll_parent_;
597   scoped_ptr<std::set<Layer*> > scroll_children_;
598
599   Layer* clip_parent_;
600   scoped_ptr<std::set<Layer*> > clip_children_;
601
602   gfx::Transform transform_;
603   gfx::Transform sublayer_transform_;
604
605   // Replica layer used for reflections.
606   scoped_refptr<Layer> replica_layer_;
607
608   // Transient properties.
609   float raster_scale_;
610
611   LayerClient* client_;
612
613   ScopedPtrVector<CopyOutputRequest> copy_requests_;
614
615   base::Closure did_scroll_callback_;
616
617   DrawProperties<Layer> draw_properties_;
618
619   PaintProperties paint_properties_;
620
621   DISALLOW_COPY_AND_ASSIGN(Layer);
622 };
623
624 }  // namespace cc
625
626 #endif  // CC_LAYERS_LAYER_H_