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