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