blob: 76fef3e99cc4d9af65fb1d4f31f73608b02ef9c2 [file] [log] [blame]
[email protected]adc93fa72011-06-21 19:47:391// Copyright (c) 2011 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.
4
5#ifndef UI_GFX_COMPOSITOR_LAYER_H_
6#define UI_GFX_COMPOSITOR_LAYER_H_
7#pragma once
8
9#include <vector>
10
[email protected]7fca53d42011-09-29 15:38:1211#include "base/compiler_specific.h"
[email protected]51f1b482011-06-23 16:52:1212#include "base/memory/ref_counted.h"
[email protected]7fca53d42011-09-29 15:38:1213#include "base/memory/scoped_ptr.h"
[email protected]00b86982011-09-01 00:02:0914#include "base/message_loop.h"
[email protected]332749032011-10-22 00:32:4615#include "third_party/WebKit/Source/WebKit/chromium/public/WebContentLayerClient.h"
[email protected]4b58d552011-10-26 00:54:4616#include "third_party/WebKit/Source/WebKit/chromium/public/WebLayer.h"
[email protected]332749032011-10-22 00:32:4617#include "third_party/WebKit/Source/WebKit/chromium/public/WebLayerClient.h"
[email protected]adc93fa72011-06-21 19:47:3918#include "ui/gfx/rect.h"
19#include "ui/gfx/transform.h"
[email protected]c155c252011-07-29 16:17:5520#include "ui/gfx/compositor/compositor.h"
[email protected]e876c272011-11-02 16:42:4521#include "ui/gfx/compositor/layer_animation_delegate.h"
[email protected]00b86982011-09-01 00:02:0922#include "ui/gfx/compositor/layer_delegate.h"
[email protected]adc93fa72011-06-21 19:47:3923
[email protected]892ad8a2011-07-27 02:47:2224class SkCanvas;
[email protected]adc93fa72011-06-21 19:47:3925
26namespace ui {
27
[email protected]adc93fa72011-06-21 19:47:3928class Compositor;
[email protected]fe7074c62011-10-28 15:22:5429class LayerAnimator;
30class LayerAnimationSequence;
[email protected]adc93fa72011-06-21 19:47:3931class Texture;
32
33// Layer manages a texture, transform and a set of child Layers. Any View that
34// has enabled layers ends up creating a Layer to manage the texture.
[email protected]28cd2bb2011-09-19 21:04:1935// A Layer can also be created without a texture, in which case it renders
36// nothing and is simply used as a node in a hierarchy of layers.
[email protected]adc93fa72011-06-21 19:47:3937//
38// NOTE: unlike Views, each Layer does *not* own its children views. If you
39// delete a Layer and it has children, the parent of each child layer is set to
40// NULL, but the children are not deleted.
[email protected]332749032011-10-22 00:32:4641class COMPOSITOR_EXPORT Layer :
[email protected]e876c272011-11-02 16:42:4542 public LayerAnimationDelegate,
[email protected]332749032011-10-22 00:32:4643 NON_EXPORTED_BASE(public WebKit::WebLayerClient),
44 NON_EXPORTED_BASE(public WebKit::WebContentLayerClient) {
[email protected]adc93fa72011-06-21 19:47:3945 public:
[email protected]993d6b322011-09-27 19:14:3846 enum LayerType {
[email protected]28cd2bb2011-09-19 21:04:1947 LAYER_HAS_NO_TEXTURE = 0,
48 LAYER_HAS_TEXTURE = 1
49 };
50
[email protected]7ab3f272011-11-16 00:51:5651 Layer();
52 explicit Layer(LayerType type);
[email protected]8f2a15d2011-09-29 15:50:5953 virtual ~Layer();
[email protected]adc93fa72011-06-21 19:47:3954
[email protected]993d6b322011-09-27 19:14:3855 // Retrieves the Layer's compositor. The Layer will walk up its parent chain
56 // to locate it. Returns NULL if the Layer is not attached to a compositor.
57 Compositor* GetCompositor();
58
59 // Called by the compositor when the Layer is set as its root Layer. This can
60 // only ever be called on the root layer.
61 void SetCompositor(Compositor* compositor);
62
[email protected]00b86982011-09-01 00:02:0963 LayerDelegate* delegate() { return delegate_; }
64 void set_delegate(LayerDelegate* delegate) { delegate_ = delegate; }
65
[email protected]adc93fa72011-06-21 19:47:3966 // Adds a new Layer to this Layer.
67 void Add(Layer* child);
68
69 // Removes a Layer from this Layer.
70 void Remove(Layer* child);
71
[email protected]18dab372011-10-03 21:21:4472 // Moves a child to the end of the child list.
73 void MoveToFront(Layer* child);
74
[email protected]62dd7ea2011-11-08 15:51:4775 // Moves |child| to be above |other|. Does nothing if |other| is already above
76 // |child|.
77 void MoveAbove(Layer* child, Layer* other);
78
[email protected]adc93fa72011-06-21 19:47:3979 // Returns the child Layers.
[email protected]25ae9a12011-10-12 14:55:2280 const std::vector<Layer*>& children() const { return children_; }
[email protected]adc93fa72011-06-21 19:47:3981
82 // The parent.
83 const Layer* parent() const { return parent_; }
84 Layer* parent() { return parent_; }
85
[email protected]ad7258912011-08-29 20:33:5386 // Returns true if this Layer contains |other| somewhere in its children.
87 bool Contains(const Layer* other) const;
88
[email protected]fe7074c62011-10-28 15:22:5489 // The layer's animator is responsible for causing automatic animations when
90 // properties are set. It also manages a queue of pending animations and
91 // handles blending of animations. The layer takes ownership of the animator.
92 void SetAnimator(LayerAnimator* animator);
93
94 // Returns the layer's animator. Creates a default animator of one has not
95 // been set. Will not return NULL.
96 LayerAnimator* GetAnimator();
[email protected]7fca53d42011-09-29 15:38:1297
[email protected]adc93fa72011-06-21 19:47:3998 // The transform, relative to the parent.
[email protected]fe7074c62011-10-28 15:22:5499 void SetTransform(const Transform& transform);
100 const Transform& transform() const { return transform_; }
101
102 // Return the target transform if animator is running, or the current
103 // transform otherwise.
104 Transform GetTargetTransform() const;
[email protected]adc93fa72011-06-21 19:47:39105
106 // The bounds, relative to the parent.
[email protected]c155c252011-07-29 16:17:55107 void SetBounds(const gfx::Rect& bounds);
[email protected]adc93fa72011-06-21 19:47:39108 const gfx::Rect& bounds() const { return bounds_; }
109
[email protected]5ba9d5f2011-10-20 01:58:33110 // Return the target bounds if animator is running, or the current bounds
111 // otherwise.
112 gfx::Rect GetTargetBounds() const;
113
[email protected]7fca53d42011-09-29 15:38:12114 // The opacity of the layer. The opacity is applied to each pixel of the
115 // texture (resulting alpha = opacity * alpha).
116 float opacity() const { return opacity_; }
117 void SetOpacity(float opacity);
118
[email protected]3f1f5e6a2011-11-11 15:09:02119 // Return the target opacity if animator is running, or the current opacity
[email protected]fe7074c62011-10-28 15:22:54120 // otherwise.
121 float GetTargetOpacity() const;
122
[email protected]e4e8afef2011-10-05 13:58:33123 // Sets the visibility of the Layer. A Layer may be visible but not
124 // drawn. This happens if any ancestor of a Layer is not visible.
[email protected]993d6b322011-09-27 19:14:38125 void SetVisible(bool visible);
[email protected]f941f47a2011-10-15 18:44:51126 bool visible() const { return visible_; }
[email protected]3aa43942011-09-13 20:59:53127
[email protected]e4e8afef2011-10-05 13:58:33128 // Returns true if this Layer is drawn. A Layer is drawn only if all ancestors
129 // are visible.
130 bool IsDrawn() const;
131
[email protected]c1f67302011-09-27 14:18:09132 // Returns true if this layer can have a texture (has_texture_ is true)
133 // and is not completely obscured by a child.
[email protected]332749032011-10-22 00:32:46134 bool ShouldDraw() const;
[email protected]c1f67302011-09-27 14:18:09135
[email protected]ad7258912011-08-29 20:33:53136 // Converts a point from the coordinates of |source| to the coordinates of
137 // |target|. Necessarily, |source| and |target| must inhabit the same Layer
138 // tree.
139 static void ConvertPointToLayer(const Layer* source,
140 const Layer* target,
141 gfx::Point* point);
142
[email protected]c155c252011-07-29 16:17:55143 // See description in View for details
144 void SetFillsBoundsOpaquely(bool fills_bounds_opaquely);
145 bool fills_bounds_opaquely() const { return fills_bounds_opaquely_; }
146
[email protected]a50ff372011-11-03 16:06:28147 // Returns the invalid rectangle. That is, the region of the layer that needs
148 // to be repainted. This is exposed for testing and isn't generally useful.
149 const gfx::Rect& invalid_rect() const { return invalid_rect_; }
150
[email protected]c155c252011-07-29 16:17:55151 const gfx::Rect& hole_rect() const { return hole_rect_; }
152
[email protected]536987c82011-06-28 03:46:08153 const ui::Texture* texture() const { return texture_.get(); }
[email protected]1cbbee3c2011-06-24 12:32:19154
[email protected]28cd2bb2011-09-19 21:04:19155 // |texture| cannot be NULL, and this function cannot be called more than
156 // once.
157 // TODO(beng): This can be removed from the API when we are in a
158 // single-compositor world.
159 void SetExternalTexture(ui::Texture* texture);
160
[email protected]892ad8a2011-07-27 02:47:22161 // Resets the canvas of the texture.
162 void SetCanvas(const SkCanvas& canvas, const gfx::Point& origin);
[email protected]adc93fa72011-06-21 19:47:39163
[email protected]870119a2011-09-30 18:13:22164 // Adds |invalid_rect| to the Layer's pending invalid rect and calls
165 // ScheduleDraw().
[email protected]00b86982011-09-01 00:02:09166 void SchedulePaint(const gfx::Rect& invalid_rect);
167
[email protected]870119a2011-09-30 18:13:22168 // Schedules a redraw of the layer tree at the compositor.
169 void ScheduleDraw();
170
[email protected]6b29ebb2011-09-29 17:41:20171 // Does drawing for the layer.
[email protected]adc93fa72011-06-21 19:47:39172 void Draw();
173
[email protected]3aa43942011-09-13 20:59:53174 // Draws a tree of Layers, by calling Draw() on each in the hierarchy starting
175 // with the receiver.
176 void DrawTree();
177
[email protected]a97527b2011-09-14 15:44:38178 // Sometimes the Layer is being updated by something other than SetCanvas
179 // (e.g. the GPU process on TOUCH_UI).
180 bool layer_updated_externally() const { return layer_updated_externally_; }
181
[email protected]332749032011-10-22 00:32:46182 // WebLayerClient
183 virtual void notifyNeedsComposite();
184
185 // WebContentLayerClient
186 virtual void paintContents(WebKit::WebCanvas*, const WebKit::WebRect& clip);
187
188#if defined(USE_WEBKIT_COMPOSITOR)
[email protected]4b58d552011-10-26 00:54:46189 WebKit::WebLayer web_layer() { return web_layer_; }
[email protected]332749032011-10-22 00:32:46190#endif
191
[email protected]adc93fa72011-06-21 19:47:39192 private:
[email protected]714e38432011-11-15 17:10:39193 struct LayerProperties {
194 public:
195 ui::Layer* layer;
196 ui::Transform transform_relative_to_root;
197 };
198
[email protected]b4bb9ca2011-09-23 20:53:14199 // TODO(vollick): Eventually, if a non-leaf node has an opacity of less than
200 // 1.0, we'll render to a separate texture, and then apply the alpha.
201 // Currently, we multiply our opacity by all our ancestor's opacities and
202 // use the combined result, but this is only temporary.
203 float GetCombinedOpacity() const;
204
[email protected]00b86982011-09-01 00:02:09205 // Called during the Draw() pass to freshen the Layer's contents from the
206 // delegate.
207 void UpdateLayerCanvas();
208
[email protected]714e38432011-11-15 17:10:39209 // Called to indicate that a layer's properties have changed and that the
210 // holes for the layers must be recomputed.
211 void SetNeedsToRecomputeHole();
212
213 // Resets |hole_rect_| to the empty rect for all layers below and
214 // including this one.
215 void ClearHoleRects();
216
217 // Does a preorder traversal of layers starting with this layer. Omits layers
218 // which cannot punch a hole in another layer such as non visible layers
219 // and layers which don't fill their bounds opaquely.
220 void GetLayerProperties(const ui::Transform& current_transform,
221 std::vector<LayerProperties>* traverasal);
222
[email protected]c155c252011-07-29 16:17:55223 // A hole in a layer is an area in the layer that does not get drawn
224 // because this area is covered up with another layer which is known to be
225 // opaque.
226 // This method computes the dimension of the hole (if there is one)
227 // based on whether one of its child nodes is always opaque.
[email protected]714e38432011-11-15 17:10:39228 // Note: This method should only be called from the root.
[email protected]c155c252011-07-29 16:17:55229 void RecomputeHole();
230
[email protected]714e38432011-11-15 17:10:39231 void set_hole_rect(const gfx::Rect& hole_rect) {
232 hole_rect_ = hole_rect;
233 }
[email protected]6b29ebb2011-09-29 17:41:20234
235 // Determines the regions that don't intersect |rect| and places the
236 // result in |sides|.
237 //
238 // rect_____________________________
239 // |xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx|
240 // |xxxxxxxxxxxxx top xxxxxxxxxxxxxx|
241 // |________________________________|
242 // |xxxxx| |xxxxx|
243 // |xxxxx|region_to_punch_out |xxxxx|
244 // |left | |right|
245 // |_____|____________________|_____|
246 // |xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx|
247 // |xxxxxxxxxx bottom xxxxxxxxxxxxxx|
248 // |________________________________|
249 static void PunchHole(const gfx::Rect& rect,
250 const gfx::Rect& region_to_punch_out,
251 std::vector<gfx::Rect>* sides);
252
[email protected]714e38432011-11-15 17:10:39253 // Drops texture just for this layer.
254 void DropTexture();
255
[email protected]993d6b322011-09-27 19:14:38256 // Drop all textures for layers below and including this one. Called when
257 // the layer is removed from a hierarchy. Textures will be re-generated if
258 // the layer is subsequently re-attached and needs to be drawn.
259 void DropTextures();
260
[email protected]ad7258912011-08-29 20:33:53261 bool ConvertPointForAncestor(const Layer* ancestor, gfx::Point* point) const;
262 bool ConvertPointFromAncestor(const Layer* ancestor, gfx::Point* point) const;
263
264 bool GetTransformRelativeTo(const Layer* ancestor,
265 Transform* transform) const;
266
[email protected]b4bb9ca2011-09-23 20:53:14267 // The only externally updated layers are ones that get their pixels from
268 // WebKit and WebKit does not produce valid alpha values. All other layers
269 // should have valid alpha.
270 bool has_valid_alpha_channel() const { return !layer_updated_externally_; }
271
[email protected]7fca53d42011-09-29 15:38:12272 // Following are invoked from the animation or if no animation exists to
273 // update the values immediately.
274 void SetBoundsImmediately(const gfx::Rect& bounds);
275 void SetTransformImmediately(const ui::Transform& transform);
276 void SetOpacityImmediately(float opacity);
277
[email protected]fe7074c62011-10-28 15:22:54278 // Implementation of LayerAnimatorDelegate
279 virtual void SetBoundsFromAnimation(const gfx::Rect& bounds) OVERRIDE;
280 virtual void SetTransformFromAnimation(const Transform& transform) OVERRIDE;
281 virtual void SetOpacityFromAnimation(float opacity) OVERRIDE;
282 virtual void ScheduleDrawForAnimation() OVERRIDE;
283 virtual const gfx::Rect& GetBoundsForAnimation() const OVERRIDE;
284 virtual const Transform& GetTransformForAnimation() const OVERRIDE;
285 virtual float GetOpacityForAnimation() const OVERRIDE;
[email protected]7fca53d42011-09-29 15:38:12286
[email protected]332749032011-10-22 00:32:46287#if defined(USE_WEBKIT_COMPOSITOR)
288 void CreateWebLayer();
289 void RecomputeTransform();
[email protected]b9616d592011-11-14 20:04:42290 void RecomputeDrawsContentAndUVRect();
[email protected]332749032011-10-22 00:32:46291#endif
292
[email protected]993d6b322011-09-27 19:14:38293 const LayerType type_;
294
[email protected]adc93fa72011-06-21 19:47:39295 Compositor* compositor_;
296
[email protected]51f1b482011-06-23 16:52:12297 scoped_refptr<ui::Texture> texture_;
[email protected]adc93fa72011-06-21 19:47:39298
299 Layer* parent_;
300
301 std::vector<Layer*> children_;
302
303 ui::Transform transform_;
304
305 gfx::Rect bounds_;
306
[email protected]e4e8afef2011-10-05 13:58:33307 // Visibility of this layer. See SetVisible/IsDrawn for more details.
[email protected]3aa43942011-09-13 20:59:53308 bool visible_;
309
[email protected]c155c252011-07-29 16:17:55310 bool fills_bounds_opaquely_;
311
312 gfx::Rect hole_rect_;
313
[email protected]714e38432011-11-15 17:10:39314 bool recompute_hole_;
315
[email protected]00b86982011-09-01 00:02:09316 gfx::Rect invalid_rect_;
317
[email protected]a97527b2011-09-14 15:44:38318 // If true the layer is always up to date.
319 bool layer_updated_externally_;
320
[email protected]b4bb9ca2011-09-23 20:53:14321 float opacity_;
322
[email protected]00b86982011-09-01 00:02:09323 LayerDelegate* delegate_;
324
[email protected]fe7074c62011-10-28 15:22:54325 scoped_ptr<LayerAnimator> animator_;
[email protected]7fca53d42011-09-29 15:38:12326
[email protected]332749032011-10-22 00:32:46327#if defined(USE_WEBKIT_COMPOSITOR)
[email protected]4b58d552011-10-26 00:54:46328 WebKit::WebLayer web_layer_;
329 bool web_layer_is_accelerated_;
[email protected]332749032011-10-22 00:32:46330#endif
331
[email protected]adc93fa72011-06-21 19:47:39332 DISALLOW_COPY_AND_ASSIGN(Layer);
333};
334
335} // namespace ui
336
337#endif // UI_GFX_COMPOSITOR_LAYER_H_