Move VISUAL_STATE promise to activation
[chromium-blink-merge.git] / ui / compositor / layer.h
blob571bdbfb123ff7b655657c6c834efda82766b437
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/region.h"
18 #include "cc/base/scoped_ptr_vector.h"
19 #include "cc/layers/content_layer_client.h"
20 #include "cc/layers/layer_client.h"
21 #include "cc/layers/surface_layer.h"
22 #include "cc/layers/texture_layer_client.h"
23 #include "cc/resources/texture_mailbox.h"
24 #include "cc/surfaces/surface_id.h"
25 #include "third_party/skia/include/core/SkColor.h"
26 #include "third_party/skia/include/core/SkRegion.h"
27 #include "ui/compositor/compositor.h"
28 #include "ui/compositor/layer_animation_delegate.h"
29 #include "ui/compositor/layer_delegate.h"
30 #include "ui/compositor/layer_type.h"
31 #include "ui/gfx/geometry/rect.h"
32 #include "ui/gfx/image/image_skia.h"
33 #include "ui/gfx/transform.h"
35 class SkCanvas;
37 namespace cc {
38 class ContentLayer;
39 class CopyOutputRequest;
40 class DelegatedFrameProvider;
41 class DelegatedRendererLayer;
42 class Layer;
43 class NinePatchLayer;
44 class ResourceUpdateQueue;
45 class SolidColorLayer;
46 class SurfaceLayer;
47 class TextureLayer;
48 struct ReturnedResource;
49 typedef std::vector<ReturnedResource> ReturnedResourceArray;
52 namespace ui {
54 class Compositor;
55 class LayerAnimator;
56 class LayerOwner;
58 // Layer manages a texture, transform and a set of child Layers. Any View that
59 // has enabled layers ends up creating a Layer to manage the texture.
60 // A Layer can also be created without a texture, in which case it renders
61 // nothing and is simply used as a node in a hierarchy of layers.
62 // Coordinate system used in layers is DIP (Density Independent Pixel)
63 // coordinates unless explicitly mentioned as pixel coordinates.
65 // NOTE: Unlike Views, each Layer does *not* own its child Layers. If you
66 // delete a Layer and it has children, the parent of each child Layer is set to
67 // NULL, but the children are not deleted.
68 class COMPOSITOR_EXPORT Layer
69 : public LayerAnimationDelegate,
70 NON_EXPORTED_BASE(public cc::ContentLayerClient),
71 NON_EXPORTED_BASE(public cc::TextureLayerClient),
72 NON_EXPORTED_BASE(public cc::LayerClient),
73 NON_EXPORTED_BASE(public cc::LayerAnimationEventObserver) {
74 public:
75 Layer();
76 explicit Layer(LayerType type);
77 ~Layer() override;
79 static bool UsingPictureLayer();
81 // Retrieves the Layer's compositor. The Layer will walk up its parent chain
82 // to locate it. Returns NULL if the Layer is not attached to a compositor.
83 Compositor* GetCompositor() {
84 return const_cast<Compositor*>(
85 const_cast<const Layer*>(this)->GetCompositor());
87 const Compositor* GetCompositor() const;
89 // Called by the compositor when the Layer is set as its root Layer. This can
90 // only ever be called on the root layer.
91 void SetCompositor(Compositor* compositor);
93 LayerDelegate* delegate() { return delegate_; }
94 void set_delegate(LayerDelegate* delegate) { delegate_ = delegate; }
96 LayerOwner* owner() { return owner_; }
98 // Adds a new Layer to this Layer.
99 void Add(Layer* child);
101 // Removes a Layer from this Layer.
102 void Remove(Layer* child);
104 // Stacks |child| above all other children.
105 void StackAtTop(Layer* child);
107 // Stacks |child| directly above |other|. Both must be children of this
108 // layer. Note that if |child| is initially stacked even higher, calling this
109 // method will result in |child| being lowered in the stacking order.
110 void StackAbove(Layer* child, Layer* other);
112 // Stacks |child| below all other children.
113 void StackAtBottom(Layer* child);
115 // Stacks |child| directly below |other|. Both must be children of this
116 // layer.
117 void StackBelow(Layer* child, Layer* other);
119 // Returns the child Layers.
120 const std::vector<Layer*>& children() const { return children_; }
122 // The parent.
123 const Layer* parent() const { return parent_; }
124 Layer* parent() { return parent_; }
126 LayerType type() const { return type_; }
128 // Returns true if this Layer contains |other| somewhere in its children.
129 bool Contains(const Layer* other) const;
131 // The layer's animator is responsible for causing automatic animations when
132 // properties are set. It also manages a queue of pending animations and
133 // handles blending of animations. The layer takes ownership of the animator.
134 void SetAnimator(LayerAnimator* animator);
136 // Returns the layer's animator. Creates a default animator of one has not
137 // been set. Will not return NULL.
138 LayerAnimator* GetAnimator();
140 // The transform, relative to the parent.
141 void SetTransform(const gfx::Transform& transform);
142 gfx::Transform transform() const;
144 // Return the target transform if animator is running, or the current
145 // transform otherwise.
146 gfx::Transform GetTargetTransform() const;
148 // The bounds, relative to the parent.
149 void SetBounds(const gfx::Rect& bounds);
150 const gfx::Rect& bounds() const { return bounds_; }
152 // The offset from our parent (stored in bounds.origin()) is an integer but we
153 // may need to be at a fractional pixel offset to align properly on screen.
154 void SetSubpixelPositionOffset(const gfx::Vector2dF& offset);
155 const gfx::Vector2dF& subpixel_position_offset() const {
156 return subpixel_position_offset_;
159 // Return the target bounds if animator is running, or the current bounds
160 // otherwise.
161 gfx::Rect GetTargetBounds() const;
163 // Sets/gets whether or not drawing of child layers should be clipped to the
164 // bounds of this layer.
165 void SetMasksToBounds(bool masks_to_bounds);
166 bool GetMasksToBounds() const;
168 // The opacity of the layer. The opacity is applied to each pixel of the
169 // texture (resulting alpha = opacity * alpha).
170 float opacity() const;
171 void SetOpacity(float opacity);
173 // Returns the actual opacity, which the opacity of this layer multipled by
174 // the combined opacity of the parent.
175 float GetCombinedOpacity() const;
177 // Blur pixels by this amount in anything below the layer and visible through
178 // the layer.
179 int background_blur() const { return background_blur_radius_; }
180 void SetBackgroundBlur(int blur_radius);
182 // Saturate all pixels of this layer by this amount.
183 // This effect will get "combined" with the inverted,
184 // brightness and grayscale setting.
185 float layer_saturation() const { return layer_saturation_; }
186 void SetLayerSaturation(float saturation);
188 // Change the brightness of all pixels from this layer by this amount.
189 // This effect will get "combined" with the inverted, saturate
190 // and grayscale setting.
191 float layer_brightness() const { return layer_brightness_; }
192 void SetLayerBrightness(float brightness);
194 // Return the target brightness if animator is running, or the current
195 // brightness otherwise.
196 float GetTargetBrightness() const;
198 // Change the grayscale of all pixels from this layer by this amount.
199 // This effect will get "combined" with the inverted, saturate
200 // and brightness setting.
201 float layer_grayscale() const { return layer_grayscale_; }
202 void SetLayerGrayscale(float grayscale);
204 // Return the target grayscale if animator is running, or the current
205 // grayscale otherwise.
206 float GetTargetGrayscale() const;
208 // Zoom the background by a factor of |zoom|. The effect is blended along the
209 // edge across |inset| pixels.
210 void SetBackgroundZoom(float zoom, int inset);
212 // Set the shape of this layer.
213 SkRegion* alpha_shape() const { return alpha_shape_.get(); }
214 void SetAlphaShape(scoped_ptr<SkRegion> region);
216 // Invert the layer.
217 bool layer_inverted() const { return layer_inverted_; }
218 void SetLayerInverted(bool inverted);
220 // Return the target opacity if animator is running, or the current opacity
221 // otherwise.
222 float GetTargetOpacity() const;
224 // Set a layer mask for a layer.
225 // Note the provided layer mask can neither have a layer mask itself nor can
226 // it have any children. The ownership of |layer_mask| will not be
227 // transferred with this call.
228 // Furthermore: A mask layer can only be set to one layer.
229 void SetMaskLayer(Layer* layer_mask);
230 Layer* layer_mask_layer() { return layer_mask_; }
232 // Sets the visibility of the Layer. A Layer may be visible but not
233 // drawn. This happens if any ancestor of a Layer is not visible.
234 void SetVisible(bool visible);
235 bool visible() const { return visible_; }
237 // Returns the target visibility if the animator is running. Otherwise, it
238 // returns the current visibility.
239 bool GetTargetVisibility() const;
241 // Returns true if this Layer is drawn. A Layer is drawn only if all ancestors
242 // are visible.
243 bool IsDrawn() const;
245 // Returns true if this layer can have a texture (has_texture_ is true)
246 // and is not completely obscured by a child.
247 bool ShouldDraw() const;
249 // Converts a point from the coordinates of |source| to the coordinates of
250 // |target|. Necessarily, |source| and |target| must inhabit the same Layer
251 // tree.
252 static void ConvertPointToLayer(const Layer* source,
253 const Layer* target,
254 gfx::Point* point);
256 // Converts a transform to be relative to the given |ancestor|. Returns
257 // whether success (that is, whether the given ancestor was really an
258 // ancestor of this layer).
259 bool GetTargetTransformRelativeTo(const Layer* ancestor,
260 gfx::Transform* transform) const;
262 // See description in View for details
263 void SetFillsBoundsOpaquely(bool fills_bounds_opaquely);
264 bool fills_bounds_opaquely() const { return fills_bounds_opaquely_; }
266 // Set to true if this layer always paints completely within its bounds. If so
267 // we can omit an unnecessary clear, even if the layer is transparent.
268 void SetFillsBoundsCompletely(bool fills_bounds_completely);
270 const std::string& name() const { return name_; }
271 void set_name(const std::string& name) { name_ = name; }
273 // Set new TextureMailbox for this layer. Note that |mailbox| may hold a
274 // shared memory resource or an actual mailbox for a texture.
275 void SetTextureMailbox(const cc::TextureMailbox& mailbox,
276 scoped_ptr<cc::SingleReleaseCallback> release_callback,
277 gfx::Size texture_size_in_dip);
278 void SetTextureSize(gfx::Size texture_size_in_dip);
279 void SetTextureFlipped(bool flipped);
280 bool TextureFlipped() const;
282 // Begins showing delegated frames from the |frame_provider|.
283 void SetShowDelegatedContent(cc::DelegatedFrameProvider* frame_provider,
284 gfx::Size frame_size_in_dip);
286 // Begins showing content from a surface with a particular id.
287 void SetShowSurface(cc::SurfaceId surface_id,
288 const cc::SurfaceLayer::SatisfyCallback& satisfy_callback,
289 const cc::SurfaceLayer::RequireCallback& require_callback,
290 gfx::Size surface_size,
291 float scale,
292 gfx::Size frame_size_in_dip);
294 bool has_external_content() {
295 return texture_layer_.get() || delegated_renderer_layer_.get() ||
296 surface_layer_.get();
299 // Show a solid color instead of delegated or surface contents.
300 void SetShowSolidColorContent();
302 // Sets the layer's fill color. May only be called for LAYER_SOLID_COLOR.
303 void SetColor(SkColor color);
304 SkColor GetTargetColor();
305 SkColor background_color() const;
307 // Updates the nine patch layer's image, aperture and border. May only be
308 // called for LAYER_NINE_PATCH.
309 void UpdateNinePatchLayerImage(const gfx::ImageSkia& image);
310 void UpdateNinePatchLayerAperture(const gfx::Rect& aperture_in_dip);
311 void UpdateNinePatchLayerBorder(const gfx::Rect& border);
313 // Adds |invalid_rect| to the Layer's pending invalid rect and calls
314 // ScheduleDraw(). Returns false if the paint request is ignored.
315 bool SchedulePaint(const gfx::Rect& invalid_rect);
317 // Schedules a redraw of the layer tree at the compositor.
318 // Note that this _does not_ invalidate any region of this layer; use
319 // SchedulePaint() for that.
320 void ScheduleDraw();
322 // Uses damaged rectangles recorded in |damaged_region_| to invalidate the
323 // |cc_layer_|.
324 void SendDamagedRects();
325 void ClearDamagedRects();
327 const cc::Region& damaged_region() const { return damaged_region_; }
329 void CompleteAllAnimations();
331 // Suppresses painting the content by disconnecting |delegate_|.
332 void SuppressPaint();
334 // Notifies the layer that the device scale factor has changed.
335 void OnDeviceScaleFactorChanged(float device_scale_factor);
337 // Notifies the layer that one of its children has received a new
338 // delegated frame.
339 void OnDelegatedFrameDamage(const gfx::Rect& damage_rect_in_dip);
341 // Requets a copy of the layer's output as a texture or bitmap.
342 void RequestCopyOfOutput(scoped_ptr<cc::CopyOutputRequest> request);
344 // ContentLayerClient
345 void PaintContents(
346 SkCanvas* canvas,
347 const gfx::Rect& clip,
348 ContentLayerClient::PaintingControlSetting painting_control) override;
349 void PaintContentsToDisplayList(
350 cc::DisplayItemList* display_list,
351 const gfx::Rect& clip,
352 ContentLayerClient::PaintingControlSetting painting_control) override;
353 bool FillsBoundsCompletely() const override;
355 cc::Layer* cc_layer() { return cc_layer_; }
357 // TextureLayerClient
358 bool PrepareTextureMailbox(
359 cc::TextureMailbox* mailbox,
360 scoped_ptr<cc::SingleReleaseCallback>* release_callback,
361 bool use_shared_memory) override;
363 float device_scale_factor() const { return device_scale_factor_; }
365 // Forces a render surface to be used on this layer. This has no positive
366 // impact, and is only used for benchmarking/testing purpose.
367 void SetForceRenderSurface(bool force);
368 bool force_render_surface() const { return force_render_surface_; }
370 // LayerClient
371 scoped_refptr<base::trace_event::ConvertableToTraceFormat> TakeDebugInfo()
372 override;
374 // LayerAnimationEventObserver
375 void OnAnimationStarted(const cc::AnimationEvent& event) override;
377 // Whether this layer has animations waiting to get sent to its cc::Layer.
378 bool HasPendingThreadedAnimations() {
379 return pending_threaded_animations_.size() != 0;
382 // Triggers a call to SwitchToLayer.
383 void SwitchCCLayerForTest();
385 private:
386 friend class LayerOwner;
388 void CollectAnimators(std::vector<scoped_refptr<LayerAnimator> >* animators);
390 // Stacks |child| above or below |other|. Helper method for StackAbove() and
391 // StackBelow().
392 void StackRelativeTo(Layer* child, Layer* other, bool above);
394 bool ConvertPointForAncestor(const Layer* ancestor, gfx::Point* point) const;
395 bool ConvertPointFromAncestor(const Layer* ancestor, gfx::Point* point) const;
397 // Implementation of LayerAnimatorDelegate
398 void SetBoundsFromAnimation(const gfx::Rect& bounds) override;
399 void SetTransformFromAnimation(const gfx::Transform& transform) override;
400 void SetOpacityFromAnimation(float opacity) override;
401 void SetVisibilityFromAnimation(bool visibility) override;
402 void SetBrightnessFromAnimation(float brightness) override;
403 void SetGrayscaleFromAnimation(float grayscale) override;
404 void SetColorFromAnimation(SkColor color) override;
405 void ScheduleDrawForAnimation() override;
406 const gfx::Rect& GetBoundsForAnimation() const override;
407 gfx::Transform GetTransformForAnimation() const override;
408 float GetOpacityForAnimation() const override;
409 bool GetVisibilityForAnimation() const override;
410 float GetBrightnessForAnimation() const override;
411 float GetGrayscaleForAnimation() const override;
412 SkColor GetColorForAnimation() const override;
413 float GetDeviceScaleFactor() const override;
414 void AddThreadedAnimation(scoped_ptr<cc::Animation> animation) override;
415 void RemoveThreadedAnimation(int animation_id) override;
416 LayerAnimatorCollection* GetLayerAnimatorCollection() override;
418 // Creates a corresponding composited layer for |type_|.
419 void CreateCcLayer();
421 // Recomputes and sets to |cc_layer_|.
422 void RecomputeDrawsContentAndUVRect();
423 void RecomputePosition();
425 // Set all filters which got applied to the layer.
426 void SetLayerFilters();
428 // Set all filters which got applied to the layer background.
429 void SetLayerBackgroundFilters();
431 // Cleanup |cc_layer_| and replaces it with |new_layer|.
432 void SwitchToLayer(scoped_refptr<cc::Layer> new_layer);
434 // We cannot send animations to our cc_layer_ until we have been added to a
435 // layer tree. Instead, we hold on to these animations in
436 // pending_threaded_animations_, and expect SendPendingThreadedAnimations to
437 // be called once we have been added to a tree.
438 void SendPendingThreadedAnimations();
440 void AddAnimatorsInTreeToCollection(LayerAnimatorCollection* collection);
441 void RemoveAnimatorsInTreeFromCollection(LayerAnimatorCollection* collection);
443 // Returns whether the layer has an animating LayerAnimator.
444 bool IsAnimating() const;
446 const LayerType type_;
448 Compositor* compositor_;
450 Layer* parent_;
452 // This layer's children, in bottom-to-top stacking order.
453 std::vector<Layer*> children_;
455 gfx::Rect bounds_;
456 gfx::Vector2dF subpixel_position_offset_;
458 // Visibility of this layer. See SetVisible/IsDrawn for more details.
459 bool visible_;
461 bool force_render_surface_;
463 bool fills_bounds_opaquely_;
464 bool fills_bounds_completely_;
466 // Union of damaged rects, in layer space, to be used when compositor is ready
467 // to paint the content.
468 cc::Region damaged_region_;
470 int background_blur_radius_;
472 // Several variables which will change the visible representation of
473 // the layer.
474 float layer_saturation_;
475 float layer_brightness_;
476 float layer_grayscale_;
477 bool layer_inverted_;
479 // The associated mask layer with this layer.
480 Layer* layer_mask_;
481 // The back link from the mask layer to it's associated masked layer.
482 // We keep this reference for the case that if the mask layer gets deleted
483 // while attached to the main layer before the main layer is deleted.
484 Layer* layer_mask_back_link_;
486 // The zoom factor to scale the layer by. Zooming is disabled when this is
487 // set to 1.
488 float zoom_;
490 // Width of the border in pixels, where the scaling is blended.
491 int zoom_inset_;
493 // Shape of the window.
494 scoped_ptr<SkRegion> alpha_shape_;
496 std::string name_;
498 LayerDelegate* delegate_;
500 LayerOwner* owner_;
502 scoped_refptr<LayerAnimator> animator_;
504 // Animations that are passed to AddThreadedAnimation before this layer is
505 // added to a tree.
506 cc::ScopedPtrVector<cc::Animation> pending_threaded_animations_;
508 // Ownership of the layer is held through one of the strongly typed layer
509 // pointers, depending on which sort of layer this is.
510 scoped_refptr<cc::Layer> content_layer_;
511 scoped_refptr<cc::NinePatchLayer> nine_patch_layer_;
512 scoped_refptr<cc::TextureLayer> texture_layer_;
513 scoped_refptr<cc::SolidColorLayer> solid_color_layer_;
514 scoped_refptr<cc::DelegatedRendererLayer> delegated_renderer_layer_;
515 scoped_refptr<cc::SurfaceLayer> surface_layer_;
516 cc::Layer* cc_layer_;
518 // A cached copy of |Compositor::device_scale_factor()|.
519 float device_scale_factor_;
521 // A cached copy of the nine patch layer's image and aperture.
522 // These are required for device scale factor change.
523 gfx::ImageSkia nine_patch_layer_image_;
524 gfx::Rect nine_patch_layer_aperture_;
526 // The mailbox used by texture_layer_.
527 cc::TextureMailbox mailbox_;
529 // The callback to release the mailbox. This is only set after
530 // SetTextureMailbox is called, before we give it to the TextureLayer.
531 scoped_ptr<cc::SingleReleaseCallback> mailbox_release_callback_;
533 // The size of the frame or texture in DIP, set when SetShowDelegatedContent
534 // or SetTextureMailbox was called.
535 gfx::Size frame_size_in_dip_;
537 DISALLOW_COPY_AND_ASSIGN(Layer);
540 } // namespace ui
542 #endif // UI_COMPOSITOR_LAYER_H_