mac: Add the flag "-gline-tables-only" to reduce dSYM size. (attempt #2)
[chromium-blink-merge.git] / ui / compositor / layer.h
blobbb8b1af6de89c7dd944136a3df2c4b080d88b62b
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);
305 // Updates the nine patch layer's image, aperture and border. May only be
306 // called for LAYER_NINE_PATCH.
307 void UpdateNinePatchLayerImage(const gfx::ImageSkia& image);
308 void UpdateNinePatchLayerAperture(const gfx::Rect& aperture_in_dip);
309 void UpdateNinePatchLayerBorder(const gfx::Rect& border);
311 // Adds |invalid_rect| to the Layer's pending invalid rect and calls
312 // ScheduleDraw(). Returns false if the paint request is ignored.
313 bool SchedulePaint(const gfx::Rect& invalid_rect);
315 // Schedules a redraw of the layer tree at the compositor.
316 // Note that this _does not_ invalidate any region of this layer; use
317 // SchedulePaint() for that.
318 void ScheduleDraw();
320 // Uses damaged rectangles recorded in |damaged_region_| to invalidate the
321 // |cc_layer_|.
322 void SendDamagedRects();
323 void ClearDamagedRects();
325 const cc::Region& damaged_region() const { return damaged_region_; }
327 void CompleteAllAnimations();
329 // Suppresses painting the content by disconnecting |delegate_|.
330 void SuppressPaint();
332 // Notifies the layer that the device scale factor has changed.
333 void OnDeviceScaleFactorChanged(float device_scale_factor);
335 // Notifies the layer that one of its children has received a new
336 // delegated frame.
337 void OnDelegatedFrameDamage(const gfx::Rect& damage_rect_in_dip);
339 // Requets a copy of the layer's output as a texture or bitmap.
340 void RequestCopyOfOutput(scoped_ptr<cc::CopyOutputRequest> request);
342 // ContentLayerClient
343 void PaintContents(
344 SkCanvas* canvas,
345 const gfx::Rect& clip,
346 ContentLayerClient::PaintingControlSetting painting_control) override;
347 void PaintContentsToDisplayList(
348 cc::DisplayItemList* display_list,
349 const gfx::Rect& clip,
350 ContentLayerClient::PaintingControlSetting painting_control) override;
351 bool FillsBoundsCompletely() const override;
353 cc::Layer* cc_layer() { return cc_layer_; }
355 // TextureLayerClient
356 bool PrepareTextureMailbox(
357 cc::TextureMailbox* mailbox,
358 scoped_ptr<cc::SingleReleaseCallback>* release_callback,
359 bool use_shared_memory) override;
361 float device_scale_factor() const { return device_scale_factor_; }
363 // Forces a render surface to be used on this layer. This has no positive
364 // impact, and is only used for benchmarking/testing purpose.
365 void SetForceRenderSurface(bool force);
366 bool force_render_surface() const { return force_render_surface_; }
368 // LayerClient
369 scoped_refptr<base::trace_event::ConvertableToTraceFormat> TakeDebugInfo()
370 override;
372 // LayerAnimationEventObserver
373 void OnAnimationStarted(const cc::AnimationEvent& event) override;
375 // Whether this layer has animations waiting to get sent to its cc::Layer.
376 bool HasPendingThreadedAnimations() {
377 return pending_threaded_animations_.size() != 0;
380 // Triggers a call to SwitchToLayer.
381 void SwitchCCLayerForTest();
383 private:
384 friend class LayerOwner;
386 void CollectAnimators(std::vector<scoped_refptr<LayerAnimator> >* animators);
388 // Stacks |child| above or below |other|. Helper method for StackAbove() and
389 // StackBelow().
390 void StackRelativeTo(Layer* child, Layer* other, bool above);
392 bool ConvertPointForAncestor(const Layer* ancestor, gfx::Point* point) const;
393 bool ConvertPointFromAncestor(const Layer* ancestor, gfx::Point* point) const;
395 // Implementation of LayerAnimatorDelegate
396 void SetBoundsFromAnimation(const gfx::Rect& bounds) override;
397 void SetTransformFromAnimation(const gfx::Transform& transform) override;
398 void SetOpacityFromAnimation(float opacity) override;
399 void SetVisibilityFromAnimation(bool visibility) override;
400 void SetBrightnessFromAnimation(float brightness) override;
401 void SetGrayscaleFromAnimation(float grayscale) override;
402 void SetColorFromAnimation(SkColor color) override;
403 void ScheduleDrawForAnimation() override;
404 const gfx::Rect& GetBoundsForAnimation() const override;
405 gfx::Transform GetTransformForAnimation() const override;
406 float GetOpacityForAnimation() const override;
407 bool GetVisibilityForAnimation() const override;
408 float GetBrightnessForAnimation() const override;
409 float GetGrayscaleForAnimation() const override;
410 SkColor GetColorForAnimation() const override;
411 float GetDeviceScaleFactor() const override;
412 void AddThreadedAnimation(scoped_ptr<cc::Animation> animation) override;
413 void RemoveThreadedAnimation(int animation_id) override;
414 LayerAnimatorCollection* GetLayerAnimatorCollection() override;
416 // Creates a corresponding composited layer for |type_|.
417 void CreateCcLayer();
419 // Recomputes and sets to |cc_layer_|.
420 void RecomputeDrawsContentAndUVRect();
421 void RecomputePosition();
423 // Set all filters which got applied to the layer.
424 void SetLayerFilters();
426 // Set all filters which got applied to the layer background.
427 void SetLayerBackgroundFilters();
429 // Cleanup |cc_layer_| and replaces it with |new_layer|.
430 void SwitchToLayer(scoped_refptr<cc::Layer> new_layer);
432 // We cannot send animations to our cc_layer_ until we have been added to a
433 // layer tree. Instead, we hold on to these animations in
434 // pending_threaded_animations_, and expect SendPendingThreadedAnimations to
435 // be called once we have been added to a tree.
436 void SendPendingThreadedAnimations();
438 void AddAnimatorsInTreeToCollection(LayerAnimatorCollection* collection);
439 void RemoveAnimatorsInTreeFromCollection(LayerAnimatorCollection* collection);
441 // Returns whether the layer has an animating LayerAnimator.
442 bool IsAnimating() const;
444 const LayerType type_;
446 Compositor* compositor_;
448 Layer* parent_;
450 // This layer's children, in bottom-to-top stacking order.
451 std::vector<Layer*> children_;
453 gfx::Rect bounds_;
454 gfx::Vector2dF subpixel_position_offset_;
456 // Visibility of this layer. See SetVisible/IsDrawn for more details.
457 bool visible_;
459 bool force_render_surface_;
461 bool fills_bounds_opaquely_;
462 bool fills_bounds_completely_;
464 // Union of damaged rects, in layer space, to be used when compositor is ready
465 // to paint the content.
466 cc::Region damaged_region_;
468 int background_blur_radius_;
470 // Several variables which will change the visible representation of
471 // the layer.
472 float layer_saturation_;
473 float layer_brightness_;
474 float layer_grayscale_;
475 bool layer_inverted_;
477 // The associated mask layer with this layer.
478 Layer* layer_mask_;
479 // The back link from the mask layer to it's associated masked layer.
480 // We keep this reference for the case that if the mask layer gets deleted
481 // while attached to the main layer before the main layer is deleted.
482 Layer* layer_mask_back_link_;
484 // The zoom factor to scale the layer by. Zooming is disabled when this is
485 // set to 1.
486 float zoom_;
488 // Width of the border in pixels, where the scaling is blended.
489 int zoom_inset_;
491 // Shape of the window.
492 scoped_ptr<SkRegion> alpha_shape_;
494 std::string name_;
496 LayerDelegate* delegate_;
498 LayerOwner* owner_;
500 scoped_refptr<LayerAnimator> animator_;
502 // Animations that are passed to AddThreadedAnimation before this layer is
503 // added to a tree.
504 cc::ScopedPtrVector<cc::Animation> pending_threaded_animations_;
506 // Ownership of the layer is held through one of the strongly typed layer
507 // pointers, depending on which sort of layer this is.
508 scoped_refptr<cc::Layer> content_layer_;
509 scoped_refptr<cc::NinePatchLayer> nine_patch_layer_;
510 scoped_refptr<cc::TextureLayer> texture_layer_;
511 scoped_refptr<cc::SolidColorLayer> solid_color_layer_;
512 scoped_refptr<cc::DelegatedRendererLayer> delegated_renderer_layer_;
513 scoped_refptr<cc::SurfaceLayer> surface_layer_;
514 cc::Layer* cc_layer_;
516 // A cached copy of |Compositor::device_scale_factor()|.
517 float device_scale_factor_;
519 // A cached copy of the nine patch layer's image and aperture.
520 // These are required for device scale factor change.
521 gfx::ImageSkia nine_patch_layer_image_;
522 gfx::Rect nine_patch_layer_aperture_;
524 // The mailbox used by texture_layer_.
525 cc::TextureMailbox mailbox_;
527 // The callback to release the mailbox. This is only set after
528 // SetTextureMailbox is called, before we give it to the TextureLayer.
529 scoped_ptr<cc::SingleReleaseCallback> mailbox_release_callback_;
531 // The size of the frame or texture in DIP, set when SetShowDelegatedContent
532 // or SetTextureMailbox was called.
533 gfx::Size frame_size_in_dip_;
535 DISALLOW_COPY_AND_ASSIGN(Layer);
538 } // namespace ui
540 #endif // UI_COMPOSITOR_LAYER_H_