Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / compositor / layer.h
blob0ff6a9611eab1390323d12dd2ec658168a4c7a9f
1 // Copyright (c) 2012 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.
5 #ifndef UI_COMPOSITOR_LAYER_H_
6 #define UI_COMPOSITOR_LAYER_H_
8 #include <string>
9 #include <vector>
11 #include "base/compiler_specific.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/message_loop/message_loop.h"
15 #include "cc/animation/animation_events.h"
16 #include "cc/animation/layer_animation_event_observer.h"
17 #include "cc/base/scoped_ptr_vector.h"
18 #include "cc/layers/content_layer_client.h"
19 #include "cc/layers/layer_client.h"
20 #include "cc/layers/texture_layer_client.h"
21 #include "cc/resources/texture_mailbox.h"
22 #include "cc/surfaces/surface_id.h"
23 #include "third_party/skia/include/core/SkColor.h"
24 #include "third_party/skia/include/core/SkRegion.h"
25 #include "ui/compositor/compositor.h"
26 #include "ui/compositor/layer_animation_delegate.h"
27 #include "ui/compositor/layer_delegate.h"
28 #include "ui/compositor/layer_type.h"
29 #include "ui/gfx/rect.h"
30 #include "ui/gfx/transform.h"
32 class SkCanvas;
34 namespace cc {
35 class ContentLayer;
36 class CopyOutputRequest;
37 class DelegatedFrameProvider;
38 class DelegatedRendererLayer;
39 class Layer;
40 class NinePatchLayer;
41 class ResourceUpdateQueue;
42 class SolidColorLayer;
43 class SurfaceLayer;
44 class TextureLayer;
45 struct ReturnedResource;
46 typedef std::vector<ReturnedResource> ReturnedResourceArray;
49 namespace ui {
51 class Compositor;
52 class LayerAnimator;
53 class LayerOwner;
55 // Layer manages a texture, transform and a set of child Layers. Any View that
56 // has enabled layers ends up creating a Layer to manage the texture.
57 // A Layer can also be created without a texture, in which case it renders
58 // nothing and is simply used as a node in a hierarchy of layers.
59 // Coordinate system used in layers is DIP (Density Independent Pixel)
60 // coordinates unless explicitly mentioned as pixel coordinates.
62 // NOTE: Unlike Views, each Layer does *not* own its child Layers. If you
63 // delete a Layer and it has children, the parent of each child Layer is set to
64 // NULL, but the children are not deleted.
65 class COMPOSITOR_EXPORT Layer
66 : public LayerAnimationDelegate,
67 NON_EXPORTED_BASE(public cc::ContentLayerClient),
68 NON_EXPORTED_BASE(public cc::TextureLayerClient),
69 NON_EXPORTED_BASE(public cc::LayerClient),
70 NON_EXPORTED_BASE(public cc::LayerAnimationEventObserver) {
71 public:
72 Layer();
73 explicit Layer(LayerType type);
74 virtual ~Layer();
76 static bool UsingPictureLayer();
78 // Retrieves the Layer's compositor. The Layer will walk up its parent chain
79 // to locate it. Returns NULL if the Layer is not attached to a compositor.
80 Compositor* GetCompositor();
82 // Called by the compositor when the Layer is set as its root Layer. This can
83 // only ever be called on the root layer.
84 void SetCompositor(Compositor* compositor);
86 LayerDelegate* delegate() { return delegate_; }
87 void set_delegate(LayerDelegate* delegate) { delegate_ = delegate; }
89 LayerOwner* owner() { return owner_; }
91 // Adds a new Layer to this Layer.
92 void Add(Layer* child);
94 // Removes a Layer from this Layer.
95 void Remove(Layer* child);
97 // Stacks |child| above all other children.
98 void StackAtTop(Layer* child);
100 // Stacks |child| directly above |other|. Both must be children of this
101 // layer. Note that if |child| is initially stacked even higher, calling this
102 // method will result in |child| being lowered in the stacking order.
103 void StackAbove(Layer* child, Layer* other);
105 // Stacks |child| below all other children.
106 void StackAtBottom(Layer* child);
108 // Stacks |child| directly below |other|. Both must be children of this
109 // layer.
110 void StackBelow(Layer* child, Layer* other);
112 // Returns the child Layers.
113 const std::vector<Layer*>& children() const { return children_; }
115 // The parent.
116 const Layer* parent() const { return parent_; }
117 Layer* parent() { return parent_; }
119 LayerType type() const { return type_; }
121 // Returns true if this Layer contains |other| somewhere in its children.
122 bool Contains(const Layer* other) const;
124 // The layer's animator is responsible for causing automatic animations when
125 // properties are set. It also manages a queue of pending animations and
126 // handles blending of animations. The layer takes ownership of the animator.
127 void SetAnimator(LayerAnimator* animator);
129 // Returns the layer's animator. Creates a default animator of one has not
130 // been set. Will not return NULL.
131 LayerAnimator* GetAnimator();
133 // The transform, relative to the parent.
134 void SetTransform(const gfx::Transform& transform);
135 gfx::Transform transform() const;
137 // Return the target transform if animator is running, or the current
138 // transform otherwise.
139 gfx::Transform GetTargetTransform() const;
141 // The bounds, relative to the parent.
142 void SetBounds(const gfx::Rect& bounds);
143 const gfx::Rect& bounds() const { return bounds_; }
145 // The offset from our parent (stored in bounds.origin()) is an integer but we
146 // may need to be at a fractional pixel offset to align properly on screen.
147 void SetSubpixelPositionOffset(const gfx::Vector2dF offset);
148 const gfx::Vector2dF& subpixel_position_offset() const {
149 return subpixel_position_offset_;
152 // Return the target bounds if animator is running, or the current bounds
153 // otherwise.
154 gfx::Rect GetTargetBounds() const;
156 // Sets/gets whether or not drawing of child layers should be clipped to the
157 // bounds of this layer.
158 void SetMasksToBounds(bool masks_to_bounds);
159 bool GetMasksToBounds() const;
161 // The opacity of the layer. The opacity is applied to each pixel of the
162 // texture (resulting alpha = opacity * alpha).
163 float opacity() const;
164 void SetOpacity(float opacity);
166 // Returns the actual opacity, which the opacity of this layer multipled by
167 // the combined opacity of the parent.
168 float GetCombinedOpacity() const;
170 // Blur pixels by this amount in anything below the layer and visible through
171 // the layer.
172 int background_blur() const { return background_blur_radius_; }
173 void SetBackgroundBlur(int blur_radius);
175 // Saturate all pixels of this layer by this amount.
176 // This effect will get "combined" with the inverted,
177 // brightness and grayscale setting.
178 float layer_saturation() const { return layer_saturation_; }
179 void SetLayerSaturation(float saturation);
181 // Change the brightness of all pixels from this layer by this amount.
182 // This effect will get "combined" with the inverted, saturate
183 // and grayscale setting.
184 float layer_brightness() const { return layer_brightness_; }
185 void SetLayerBrightness(float brightness);
187 // Return the target brightness if animator is running, or the current
188 // brightness otherwise.
189 float GetTargetBrightness() const;
191 // Change the grayscale of all pixels from this layer by this amount.
192 // This effect will get "combined" with the inverted, saturate
193 // and brightness setting.
194 float layer_grayscale() const { return layer_grayscale_; }
195 void SetLayerGrayscale(float grayscale);
197 // Return the target grayscale if animator is running, or the current
198 // grayscale otherwise.
199 float GetTargetGrayscale() const;
201 // Zoom the background by a factor of |zoom|. The effect is blended along the
202 // edge across |inset| pixels.
203 void SetBackgroundZoom(float zoom, int inset);
205 // Set the shape of this layer.
206 void SetAlphaShape(scoped_ptr<SkRegion> region);
208 // Invert the layer.
209 bool layer_inverted() const { return layer_inverted_; }
210 void SetLayerInverted(bool inverted);
212 // Return the target opacity if animator is running, or the current opacity
213 // otherwise.
214 float GetTargetOpacity() const;
216 // Set a layer mask for a layer.
217 // Note the provided layer mask can neither have a layer mask itself nor can
218 // it have any children. The ownership of |layer_mask| will not be
219 // transferred with this call.
220 // Furthermore: A mask layer can only be set to one layer.
221 void SetMaskLayer(Layer* layer_mask);
222 Layer* layer_mask_layer() { return layer_mask_; }
224 // Sets the visibility of the Layer. A Layer may be visible but not
225 // drawn. This happens if any ancestor of a Layer is not visible.
226 void SetVisible(bool visible);
227 bool visible() const { return visible_; }
229 // Returns the target visibility if the animator is running. Otherwise, it
230 // returns the current visibility.
231 bool GetTargetVisibility() const;
233 // Returns true if this Layer is drawn. A Layer is drawn only if all ancestors
234 // are visible.
235 bool IsDrawn() const;
237 // Returns true if this layer can have a texture (has_texture_ is true)
238 // and is not completely obscured by a child.
239 bool ShouldDraw() const;
241 // Converts a point from the coordinates of |source| to the coordinates of
242 // |target|. Necessarily, |source| and |target| must inhabit the same Layer
243 // tree.
244 static void ConvertPointToLayer(const Layer* source,
245 const Layer* target,
246 gfx::Point* point);
248 // Converts a transform to be relative to the given |ancestor|. Returns
249 // whether success (that is, whether the given ancestor was really an
250 // ancestor of this layer).
251 bool GetTargetTransformRelativeTo(const Layer* ancestor,
252 gfx::Transform* transform) const;
254 // See description in View for details
255 void SetFillsBoundsOpaquely(bool fills_bounds_opaquely);
256 bool fills_bounds_opaquely() const { return fills_bounds_opaquely_; }
258 // Set to true if this layer always paints completely within its bounds. If so
259 // we can omit an unnecessary clear, even if the layer is transparent.
260 void SetFillsBoundsCompletely(bool fills_bounds_completely);
262 const std::string& name() const { return name_; }
263 void set_name(const std::string& name) { name_ = name; }
265 // Set new TextureMailbox for this layer. Note that |mailbox| may hold a
266 // shared memory resource or an actual mailbox for a texture.
267 void SetTextureMailbox(const cc::TextureMailbox& mailbox,
268 scoped_ptr<cc::SingleReleaseCallback> release_callback,
269 gfx::Size texture_size_in_dip);
270 void SetTextureSize(gfx::Size texture_size_in_dip);
272 // Begins showing delegated frames from the |frame_provider|.
273 void SetShowDelegatedContent(cc::DelegatedFrameProvider* frame_provider,
274 gfx::Size frame_size_in_dip);
276 // Begins showing content from a surface with a particular id.
277 void SetShowSurface(cc::SurfaceId id, gfx::Size frame_size_in_dip);
279 bool has_external_content() {
280 return texture_layer_.get() || delegated_renderer_layer_.get() ||
281 surface_layer_.get();
284 void SetShowPaintedContent();
286 // Sets the layer's fill color. May only be called for LAYER_SOLID_COLOR.
287 void SetColor(SkColor color);
289 // Updates the nine patch layer's bitmap and aperture. May only be called for
290 // LAYER_NINE_PATCH.
291 void UpdateNinePatchLayerBitmap(const SkBitmap& bitmap,
292 const gfx::Rect& aperture);
294 // Updates the nine patch layer's border. May only be called for
295 // LAYER_NINE_PATCH.
296 void UpdateNinePatchLayerBorder(const gfx::Rect& border);
298 // Adds |invalid_rect| to the Layer's pending invalid rect and calls
299 // ScheduleDraw(). Returns false if the paint request is ignored.
300 bool SchedulePaint(const gfx::Rect& invalid_rect);
302 // Schedules a redraw of the layer tree at the compositor.
303 // Note that this _does not_ invalidate any region of this layer; use
304 // SchedulePaint() for that.
305 void ScheduleDraw();
307 // Uses damaged rectangles recorded in |damaged_region_| to invalidate the
308 // |cc_layer_|.
309 void SendDamagedRects();
311 const SkRegion& damaged_region() const { return damaged_region_; }
313 void CompleteAllAnimations();
315 // Suppresses painting the content by disconnecting |delegate_|.
316 void SuppressPaint();
318 // Notifies the layer that the device scale factor has changed.
319 void OnDeviceScaleFactorChanged(float device_scale_factor);
321 // Notifies the layer that one of its children has received a new
322 // delegated frame.
323 void OnDelegatedFrameDamage(const gfx::Rect& damage_rect_in_dip);
325 // Requets a copy of the layer's output as a texture or bitmap.
326 void RequestCopyOfOutput(scoped_ptr<cc::CopyOutputRequest> request);
328 // ContentLayerClient
329 virtual void PaintContents(
330 SkCanvas* canvas,
331 const gfx::Rect& clip,
332 gfx::RectF* opaque,
333 ContentLayerClient::GraphicsContextStatus gc_status) OVERRIDE;
334 virtual void DidChangeLayerCanUseLCDText() OVERRIDE {}
335 virtual bool FillsBoundsCompletely() const OVERRIDE;
337 cc::Layer* cc_layer() { return cc_layer_; }
339 // TextureLayerClient
340 virtual bool PrepareTextureMailbox(
341 cc::TextureMailbox* mailbox,
342 scoped_ptr<cc::SingleReleaseCallback>* release_callback,
343 bool use_shared_memory) OVERRIDE;
345 float device_scale_factor() const { return device_scale_factor_; }
347 // Forces a render surface to be used on this layer. This has no positive
348 // impact, and is only used for benchmarking/testing purpose.
349 void SetForceRenderSurface(bool force);
350 bool force_render_surface() const { return force_render_surface_; }
352 // LayerClient
353 virtual scoped_refptr<base::debug::ConvertableToTraceFormat>
354 TakeDebugInfo() OVERRIDE;
356 // LayerAnimationEventObserver
357 virtual void OnAnimationStarted(const cc::AnimationEvent& event) OVERRIDE;
359 // Whether this layer has animations waiting to get sent to its cc::Layer.
360 bool HasPendingThreadedAnimations() {
361 return pending_threaded_animations_.size() != 0;
364 // Triggers a call to SwitchToLayer.
365 void SwitchCCLayerForTest();
367 private:
368 friend class LayerOwner;
370 void CollectAnimators(std::vector<scoped_refptr<LayerAnimator> >* animators);
372 // Stacks |child| above or below |other|. Helper method for StackAbove() and
373 // StackBelow().
374 void StackRelativeTo(Layer* child, Layer* other, bool above);
376 bool ConvertPointForAncestor(const Layer* ancestor, gfx::Point* point) const;
377 bool ConvertPointFromAncestor(const Layer* ancestor, gfx::Point* point) const;
379 // Implementation of LayerAnimatorDelegate
380 virtual void SetBoundsFromAnimation(const gfx::Rect& bounds) OVERRIDE;
381 virtual void SetTransformFromAnimation(
382 const gfx::Transform& transform) OVERRIDE;
383 virtual void SetOpacityFromAnimation(float opacity) OVERRIDE;
384 virtual void SetVisibilityFromAnimation(bool visibility) OVERRIDE;
385 virtual void SetBrightnessFromAnimation(float brightness) OVERRIDE;
386 virtual void SetGrayscaleFromAnimation(float grayscale) OVERRIDE;
387 virtual void SetColorFromAnimation(SkColor color) OVERRIDE;
388 virtual void ScheduleDrawForAnimation() OVERRIDE;
389 virtual const gfx::Rect& GetBoundsForAnimation() const OVERRIDE;
390 virtual gfx::Transform GetTransformForAnimation() const OVERRIDE;
391 virtual float GetOpacityForAnimation() const OVERRIDE;
392 virtual bool GetVisibilityForAnimation() const OVERRIDE;
393 virtual float GetBrightnessForAnimation() const OVERRIDE;
394 virtual float GetGrayscaleForAnimation() const OVERRIDE;
395 virtual SkColor GetColorForAnimation() const OVERRIDE;
396 virtual float GetDeviceScaleFactor() const OVERRIDE;
397 virtual void AddThreadedAnimation(
398 scoped_ptr<cc::Animation> animation) OVERRIDE;
399 virtual void RemoveThreadedAnimation(int animation_id) OVERRIDE;
400 virtual LayerAnimatorCollection* GetLayerAnimatorCollection() OVERRIDE;
402 // Creates a corresponding composited layer for |type_|.
403 void CreateWebLayer();
405 // Recomputes and sets to |cc_layer_|.
406 void RecomputeDrawsContentAndUVRect();
407 void RecomputePosition();
409 // Set all filters which got applied to the layer.
410 void SetLayerFilters();
412 // Set all filters which got applied to the layer background.
413 void SetLayerBackgroundFilters();
415 // Cleanup |cc_layer_| and replaces it with |new_layer|.
416 void SwitchToLayer(scoped_refptr<cc::Layer> new_layer);
418 // We cannot send animations to our cc_layer_ until we have been added to a
419 // layer tree. Instead, we hold on to these animations in
420 // pending_threaded_animations_, and expect SendPendingThreadedAnimations to
421 // be called once we have been added to a tree.
422 void SendPendingThreadedAnimations();
424 void AddAnimatorsInTreeToCollection(LayerAnimatorCollection* collection);
425 void RemoveAnimatorsInTreeFromCollection(LayerAnimatorCollection* collection);
427 // Returns whether the layer has an animating LayerAnimator.
428 bool IsAnimating() const;
430 const LayerType type_;
432 Compositor* compositor_;
434 Layer* parent_;
436 // This layer's children, in bottom-to-top stacking order.
437 std::vector<Layer*> children_;
439 gfx::Rect bounds_;
440 gfx::Vector2dF subpixel_position_offset_;
442 // Visibility of this layer. See SetVisible/IsDrawn for more details.
443 bool visible_;
445 bool force_render_surface_;
447 bool fills_bounds_opaquely_;
448 bool fills_bounds_completely_;
450 // Union of damaged rects, in pixel coordinates, to be used when
451 // compositor is ready to paint the content.
452 SkRegion damaged_region_;
454 int background_blur_radius_;
456 // Several variables which will change the visible representation of
457 // the layer.
458 float layer_saturation_;
459 float layer_brightness_;
460 float layer_grayscale_;
461 bool layer_inverted_;
463 // The associated mask layer with this layer.
464 Layer* layer_mask_;
465 // The back link from the mask layer to it's associated masked layer.
466 // We keep this reference for the case that if the mask layer gets deleted
467 // while attached to the main layer before the main layer is deleted.
468 Layer* layer_mask_back_link_;
470 // The zoom factor to scale the layer by. Zooming is disabled when this is
471 // set to 1.
472 float zoom_;
474 // Width of the border in pixels, where the scaling is blended.
475 int zoom_inset_;
477 // Shape of the window.
478 scoped_ptr<SkRegion> alpha_shape_;
480 std::string name_;
482 LayerDelegate* delegate_;
484 LayerOwner* owner_;
486 scoped_refptr<LayerAnimator> animator_;
488 // Animations that are passed to AddThreadedAnimation before this layer is
489 // added to a tree.
490 cc::ScopedPtrVector<cc::Animation> pending_threaded_animations_;
492 // Ownership of the layer is held through one of the strongly typed layer
493 // pointers, depending on which sort of layer this is.
494 scoped_refptr<cc::Layer> content_layer_;
495 scoped_refptr<cc::NinePatchLayer> nine_patch_layer_;
496 scoped_refptr<cc::TextureLayer> texture_layer_;
497 scoped_refptr<cc::SolidColorLayer> solid_color_layer_;
498 scoped_refptr<cc::DelegatedRendererLayer> delegated_renderer_layer_;
499 scoped_refptr<cc::SurfaceLayer> surface_layer_;
500 cc::Layer* cc_layer_;
502 // A cached copy of |Compositor::device_scale_factor()|.
503 float device_scale_factor_;
505 // The mailbox used by texture_layer_.
506 cc::TextureMailbox mailbox_;
508 // The callback to release the mailbox. This is only set after
509 // SetTextureMailbox is called, before we give it to the TextureLayer.
510 scoped_ptr<cc::SingleReleaseCallback> mailbox_release_callback_;
512 // The size of the frame or texture in DIP, set when SetShowDelegatedContent
513 // or SetTextureMailbox was called.
514 gfx::Size frame_size_in_dip_;
516 DISALLOW_COPY_AND_ASSIGN(Layer);
519 } // namespace ui
521 #endif // UI_COMPOSITOR_LAYER_H_