ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / ui / compositor / layer.h
blobbcdbd4cf3a184cdee2d987ce509e440dde735555
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);
277 void SetTextureFlipped(bool flipped);
278 bool TextureFlipped() const;
280 // Begins showing delegated frames from the |frame_provider|.
281 void SetShowDelegatedContent(cc::DelegatedFrameProvider* frame_provider,
282 gfx::Size frame_size_in_dip);
284 // Begins showing content from a surface with a particular id.
285 void SetShowSurface(cc::SurfaceId surface_id,
286 const cc::SurfaceLayer::SatisfyCallback& satisfy_callback,
287 const cc::SurfaceLayer::RequireCallback& require_callback,
288 gfx::Size surface_size,
289 float scale,
290 gfx::Size frame_size_in_dip);
292 bool has_external_content() {
293 return texture_layer_.get() || delegated_renderer_layer_.get() ||
294 surface_layer_.get();
297 // Show a solid color instead of delegated or surface contents.
298 void SetShowSolidColorContent();
300 // Sets the layer's fill color. May only be called for LAYER_SOLID_COLOR.
301 void SetColor(SkColor color);
303 // Updates the nine patch layer's bitmap, aperture and border. May only be
304 // called for LAYER_NINE_PATCH.
305 void UpdateNinePatchLayerBitmap(const SkBitmap& bitmap);
306 void UpdateNinePatchLayerAperture(const gfx::Rect& aperture);
307 void UpdateNinePatchLayerBorder(const gfx::Rect& border);
309 // Adds |invalid_rect| to the Layer's pending invalid rect and calls
310 // ScheduleDraw(). Returns false if the paint request is ignored.
311 bool SchedulePaint(const gfx::Rect& invalid_rect);
313 // Schedules a redraw of the layer tree at the compositor.
314 // Note that this _does not_ invalidate any region of this layer; use
315 // SchedulePaint() for that.
316 void ScheduleDraw();
318 // Uses damaged rectangles recorded in |damaged_region_| to invalidate the
319 // |cc_layer_|.
320 void SendDamagedRects();
322 const SkRegion& damaged_region() const { return damaged_region_; }
324 void CompleteAllAnimations();
326 // Suppresses painting the content by disconnecting |delegate_|.
327 void SuppressPaint();
329 // Notifies the layer that the device scale factor has changed.
330 void OnDeviceScaleFactorChanged(float device_scale_factor);
332 // Notifies the layer that one of its children has received a new
333 // delegated frame.
334 void OnDelegatedFrameDamage(const gfx::Rect& damage_rect_in_dip);
336 // Requets a copy of the layer's output as a texture or bitmap.
337 void RequestCopyOfOutput(scoped_ptr<cc::CopyOutputRequest> request);
339 // ContentLayerClient
340 void PaintContents(
341 SkCanvas* canvas,
342 const gfx::Rect& clip,
343 ContentLayerClient::PaintingControlSetting painting_control) override;
344 scoped_refptr<cc::DisplayItemList> PaintContentsToDisplayList(
345 const gfx::Rect& clip,
346 ContentLayerClient::PaintingControlSetting painting_control) override;
347 bool FillsBoundsCompletely() const override;
349 cc::Layer* cc_layer() { return cc_layer_; }
351 // TextureLayerClient
352 bool PrepareTextureMailbox(
353 cc::TextureMailbox* mailbox,
354 scoped_ptr<cc::SingleReleaseCallback>* release_callback,
355 bool use_shared_memory) override;
357 float device_scale_factor() const { return device_scale_factor_; }
359 // Forces a render surface to be used on this layer. This has no positive
360 // impact, and is only used for benchmarking/testing purpose.
361 void SetForceRenderSurface(bool force);
362 bool force_render_surface() const { return force_render_surface_; }
364 // LayerClient
365 scoped_refptr<base::trace_event::ConvertableToTraceFormat> TakeDebugInfo()
366 override;
368 // LayerAnimationEventObserver
369 void OnAnimationStarted(const cc::AnimationEvent& event) override;
371 // Whether this layer has animations waiting to get sent to its cc::Layer.
372 bool HasPendingThreadedAnimations() {
373 return pending_threaded_animations_.size() != 0;
376 // Triggers a call to SwitchToLayer.
377 void SwitchCCLayerForTest();
379 private:
380 friend class LayerOwner;
382 void CollectAnimators(std::vector<scoped_refptr<LayerAnimator> >* animators);
384 // Stacks |child| above or below |other|. Helper method for StackAbove() and
385 // StackBelow().
386 void StackRelativeTo(Layer* child, Layer* other, bool above);
388 bool ConvertPointForAncestor(const Layer* ancestor, gfx::Point* point) const;
389 bool ConvertPointFromAncestor(const Layer* ancestor, gfx::Point* point) const;
391 // Implementation of LayerAnimatorDelegate
392 void SetBoundsFromAnimation(const gfx::Rect& bounds) override;
393 void SetTransformFromAnimation(const gfx::Transform& transform) override;
394 void SetOpacityFromAnimation(float opacity) override;
395 void SetVisibilityFromAnimation(bool visibility) override;
396 void SetBrightnessFromAnimation(float brightness) override;
397 void SetGrayscaleFromAnimation(float grayscale) override;
398 void SetColorFromAnimation(SkColor color) override;
399 void ScheduleDrawForAnimation() override;
400 const gfx::Rect& GetBoundsForAnimation() const override;
401 gfx::Transform GetTransformForAnimation() const override;
402 float GetOpacityForAnimation() const override;
403 bool GetVisibilityForAnimation() const override;
404 float GetBrightnessForAnimation() const override;
405 float GetGrayscaleForAnimation() const override;
406 SkColor GetColorForAnimation() const override;
407 float GetDeviceScaleFactor() const override;
408 void AddThreadedAnimation(scoped_ptr<cc::Animation> animation) override;
409 void RemoveThreadedAnimation(int animation_id) override;
410 LayerAnimatorCollection* GetLayerAnimatorCollection() override;
412 // Creates a corresponding composited layer for |type_|.
413 void CreateCcLayer();
415 // Recomputes and sets to |cc_layer_|.
416 void RecomputeDrawsContentAndUVRect();
417 void RecomputePosition();
419 // Set all filters which got applied to the layer.
420 void SetLayerFilters();
422 // Set all filters which got applied to the layer background.
423 void SetLayerBackgroundFilters();
425 // Cleanup |cc_layer_| and replaces it with |new_layer|.
426 void SwitchToLayer(scoped_refptr<cc::Layer> new_layer);
428 // We cannot send animations to our cc_layer_ until we have been added to a
429 // layer tree. Instead, we hold on to these animations in
430 // pending_threaded_animations_, and expect SendPendingThreadedAnimations to
431 // be called once we have been added to a tree.
432 void SendPendingThreadedAnimations();
434 void AddAnimatorsInTreeToCollection(LayerAnimatorCollection* collection);
435 void RemoveAnimatorsInTreeFromCollection(LayerAnimatorCollection* collection);
437 // Returns whether the layer has an animating LayerAnimator.
438 bool IsAnimating() const;
440 const LayerType type_;
442 Compositor* compositor_;
444 Layer* parent_;
446 // This layer's children, in bottom-to-top stacking order.
447 std::vector<Layer*> children_;
449 gfx::Rect bounds_;
450 gfx::Vector2dF subpixel_position_offset_;
452 // Visibility of this layer. See SetVisible/IsDrawn for more details.
453 bool visible_;
455 bool force_render_surface_;
457 bool fills_bounds_opaquely_;
458 bool fills_bounds_completely_;
460 // Union of damaged rects, in pixel coordinates, to be used when
461 // compositor is ready to paint the content.
462 SkRegion damaged_region_;
464 int background_blur_radius_;
466 // Several variables which will change the visible representation of
467 // the layer.
468 float layer_saturation_;
469 float layer_brightness_;
470 float layer_grayscale_;
471 bool layer_inverted_;
473 // The associated mask layer with this layer.
474 Layer* layer_mask_;
475 // The back link from the mask layer to it's associated masked layer.
476 // We keep this reference for the case that if the mask layer gets deleted
477 // while attached to the main layer before the main layer is deleted.
478 Layer* layer_mask_back_link_;
480 // The zoom factor to scale the layer by. Zooming is disabled when this is
481 // set to 1.
482 float zoom_;
484 // Width of the border in pixels, where the scaling is blended.
485 int zoom_inset_;
487 // Shape of the window.
488 scoped_ptr<SkRegion> alpha_shape_;
490 std::string name_;
492 LayerDelegate* delegate_;
494 LayerOwner* owner_;
496 scoped_refptr<LayerAnimator> animator_;
498 // Animations that are passed to AddThreadedAnimation before this layer is
499 // added to a tree.
500 cc::ScopedPtrVector<cc::Animation> pending_threaded_animations_;
502 // Ownership of the layer is held through one of the strongly typed layer
503 // pointers, depending on which sort of layer this is.
504 scoped_refptr<cc::Layer> content_layer_;
505 scoped_refptr<cc::NinePatchLayer> nine_patch_layer_;
506 scoped_refptr<cc::TextureLayer> texture_layer_;
507 scoped_refptr<cc::SolidColorLayer> solid_color_layer_;
508 scoped_refptr<cc::DelegatedRendererLayer> delegated_renderer_layer_;
509 scoped_refptr<cc::SurfaceLayer> surface_layer_;
510 cc::Layer* cc_layer_;
512 // A cached copy of |Compositor::device_scale_factor()|.
513 float device_scale_factor_;
515 // The mailbox used by texture_layer_.
516 cc::TextureMailbox mailbox_;
518 // The callback to release the mailbox. This is only set after
519 // SetTextureMailbox is called, before we give it to the TextureLayer.
520 scoped_ptr<cc::SingleReleaseCallback> mailbox_release_callback_;
522 // The size of the frame or texture in DIP, set when SetShowDelegatedContent
523 // or SetTextureMailbox was called.
524 gfx::Size frame_size_in_dip_;
526 DISALLOW_COPY_AND_ASSIGN(Layer);
529 } // namespace ui
531 #endif // UI_COMPOSITOR_LAYER_H_