blob: 5bc3819cf57eabf351cb5638c8df17eafc019bfe [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
[email protected]33699e622011-11-17 18:29:309#include <string>
[email protected]adc93fa72011-06-21 19:47:3910#include <vector>
11
[email protected]7fca53d42011-09-29 15:38:1212#include "base/compiler_specific.h"
[email protected]51f1b482011-06-23 16:52:1213#include "base/memory/ref_counted.h"
[email protected]7fca53d42011-09-29 15:38:1214#include "base/memory/scoped_ptr.h"
[email protected]00b86982011-09-01 00:02:0915#include "base/message_loop.h"
[email protected]332749032011-10-22 00:32:4616#include "third_party/WebKit/Source/WebKit/chromium/public/WebContentLayerClient.h"
[email protected]4b58d552011-10-26 00:54:4617#include "third_party/WebKit/Source/WebKit/chromium/public/WebLayer.h"
[email protected]332749032011-10-22 00:32:4618#include "third_party/WebKit/Source/WebKit/chromium/public/WebLayerClient.h"
[email protected]adc93fa72011-06-21 19:47:3919#include "ui/gfx/rect.h"
20#include "ui/gfx/transform.h"
[email protected]c155c252011-07-29 16:17:5521#include "ui/gfx/compositor/compositor.h"
[email protected]e876c272011-11-02 16:42:4522#include "ui/gfx/compositor/layer_animation_delegate.h"
[email protected]00b86982011-09-01 00:02:0923#include "ui/gfx/compositor/layer_delegate.h"
[email protected]adc93fa72011-06-21 19:47:3924
[email protected]892ad8a2011-07-27 02:47:2225class SkCanvas;
[email protected]adc93fa72011-06-21 19:47:3926
27namespace ui {
28
29class Compositor;
[email protected]fe7074c62011-10-28 15:22:5430class LayerAnimator;
[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]5e4e61f2011-11-22 16:55:2472 // Stacks |child| above all other children.
73 void StackAtTop(Layer* child);
[email protected]18dab372011-10-03 21:21:4474
[email protected]ebc335f2011-11-23 00:43:5175 // Stacks |child| directly above |other|. Both must be children of this
76 // layer. Note that if |child| is initially stacked even higher, calling this
77 // method will result in |child| being lowered in the stacking order.
[email protected]5e4e61f2011-11-22 16:55:2478 void StackAbove(Layer* child, Layer* other);
[email protected]62dd7ea2011-11-08 15:51:4779
[email protected]adc93fa72011-06-21 19:47:3980 // Returns the child Layers.
[email protected]25ae9a12011-10-12 14:55:2281 const std::vector<Layer*>& children() const { return children_; }
[email protected]adc93fa72011-06-21 19:47:3982
83 // The parent.
84 const Layer* parent() const { return parent_; }
85 Layer* parent() { return parent_; }
86
[email protected]ad7258912011-08-29 20:33:5387 // Returns true if this Layer contains |other| somewhere in its children.
88 bool Contains(const Layer* other) const;
89
[email protected]fe7074c62011-10-28 15:22:5490 // The layer's animator is responsible for causing automatic animations when
91 // properties are set. It also manages a queue of pending animations and
92 // handles blending of animations. The layer takes ownership of the animator.
93 void SetAnimator(LayerAnimator* animator);
94
95 // Returns the layer's animator. Creates a default animator of one has not
96 // been set. Will not return NULL.
97 LayerAnimator* GetAnimator();
[email protected]7fca53d42011-09-29 15:38:1298
[email protected]adc93fa72011-06-21 19:47:3999 // The transform, relative to the parent.
[email protected]fe7074c62011-10-28 15:22:54100 void SetTransform(const Transform& transform);
101 const Transform& transform() const { return transform_; }
102
103 // Return the target transform if animator is running, or the current
104 // transform otherwise.
105 Transform GetTargetTransform() const;
[email protected]adc93fa72011-06-21 19:47:39106
107 // The bounds, relative to the parent.
[email protected]c155c252011-07-29 16:17:55108 void SetBounds(const gfx::Rect& bounds);
[email protected]adc93fa72011-06-21 19:47:39109 const gfx::Rect& bounds() const { return bounds_; }
110
[email protected]5ba9d5f2011-10-20 01:58:33111 // Return the target bounds if animator is running, or the current bounds
112 // otherwise.
113 gfx::Rect GetTargetBounds() const;
114
[email protected]7fca53d42011-09-29 15:38:12115 // The opacity of the layer. The opacity is applied to each pixel of the
116 // texture (resulting alpha = opacity * alpha).
117 float opacity() const { return opacity_; }
118 void SetOpacity(float opacity);
119
[email protected]3f1f5e6a2011-11-11 15:09:02120 // Return the target opacity if animator is running, or the current opacity
[email protected]fe7074c62011-10-28 15:22:54121 // otherwise.
122 float GetTargetOpacity() const;
123
[email protected]e4e8afef2011-10-05 13:58:33124 // Sets the visibility of the Layer. A Layer may be visible but not
125 // drawn. This happens if any ancestor of a Layer is not visible.
[email protected]993d6b322011-09-27 19:14:38126 void SetVisible(bool visible);
[email protected]f941f47a2011-10-15 18:44:51127 bool visible() const { return visible_; }
[email protected]3aa43942011-09-13 20:59:53128
[email protected]e4e8afef2011-10-05 13:58:33129 // Returns true if this Layer is drawn. A Layer is drawn only if all ancestors
130 // are visible.
131 bool IsDrawn() const;
132
[email protected]c1f67302011-09-27 14:18:09133 // Returns true if this layer can have a texture (has_texture_ is true)
134 // and is not completely obscured by a child.
[email protected]332749032011-10-22 00:32:46135 bool ShouldDraw() const;
[email protected]c1f67302011-09-27 14:18:09136
[email protected]ad7258912011-08-29 20:33:53137 // Converts a point from the coordinates of |source| to the coordinates of
138 // |target|. Necessarily, |source| and |target| must inhabit the same Layer
139 // tree.
140 static void ConvertPointToLayer(const Layer* source,
141 const Layer* target,
142 gfx::Point* point);
143
[email protected]c155c252011-07-29 16:17:55144 // See description in View for details
145 void SetFillsBoundsOpaquely(bool fills_bounds_opaquely);
146 bool fills_bounds_opaquely() const { return fills_bounds_opaquely_; }
147
[email protected]a50ff372011-11-03 16:06:28148 // Returns the invalid rectangle. That is, the region of the layer that needs
149 // to be repainted. This is exposed for testing and isn't generally useful.
150 const gfx::Rect& invalid_rect() const { return invalid_rect_; }
151
[email protected]c155c252011-07-29 16:17:55152 const gfx::Rect& hole_rect() const { return hole_rect_; }
153
[email protected]33699e622011-11-17 18:29:30154 const std::string& name() const { return name_; }
155 void set_name(const std::string& name) { name_ = name; }
156
[email protected]536987c82011-06-28 03:46:08157 const ui::Texture* texture() const { return texture_.get(); }
[email protected]1cbbee3c2011-06-24 12:32:19158
[email protected]28cd2bb2011-09-19 21:04:19159 // |texture| cannot be NULL, and this function cannot be called more than
160 // once.
161 // TODO(beng): This can be removed from the API when we are in a
162 // single-compositor world.
163 void SetExternalTexture(ui::Texture* texture);
164
[email protected]892ad8a2011-07-27 02:47:22165 // Resets the canvas of the texture.
166 void SetCanvas(const SkCanvas& canvas, const gfx::Point& origin);
[email protected]adc93fa72011-06-21 19:47:39167
[email protected]870119a2011-09-30 18:13:22168 // Adds |invalid_rect| to the Layer's pending invalid rect and calls
169 // ScheduleDraw().
[email protected]00b86982011-09-01 00:02:09170 void SchedulePaint(const gfx::Rect& invalid_rect);
171
[email protected]870119a2011-09-30 18:13:22172 // Schedules a redraw of the layer tree at the compositor.
173 void ScheduleDraw();
174
[email protected]6b29ebb2011-09-29 17:41:20175 // Does drawing for the layer.
[email protected]adc93fa72011-06-21 19:47:39176 void Draw();
177
[email protected]3aa43942011-09-13 20:59:53178 // Draws a tree of Layers, by calling Draw() on each in the hierarchy starting
179 // with the receiver.
180 void DrawTree();
181
[email protected]a97527b2011-09-14 15:44:38182 // Sometimes the Layer is being updated by something other than SetCanvas
[email protected]396ecd52011-11-22 00:08:52183 // (e.g. the GPU process on UI_COMPOSITOR_IMAGE_TRANSPORT).
[email protected]a97527b2011-09-14 15:44:38184 bool layer_updated_externally() const { return layer_updated_externally_; }
185
[email protected]332749032011-10-22 00:32:46186 // WebLayerClient
187 virtual void notifyNeedsComposite();
188
189 // WebContentLayerClient
190 virtual void paintContents(WebKit::WebCanvas*, const WebKit::WebRect& clip);
191
192#if defined(USE_WEBKIT_COMPOSITOR)
[email protected]4b58d552011-10-26 00:54:46193 WebKit::WebLayer web_layer() { return web_layer_; }
[email protected]332749032011-10-22 00:32:46194#endif
195
[email protected]adc93fa72011-06-21 19:47:39196 private:
[email protected]714e38432011-11-15 17:10:39197 struct LayerProperties {
198 public:
199 ui::Layer* layer;
200 ui::Transform transform_relative_to_root;
201 };
202
[email protected]b4bb9ca2011-09-23 20:53:14203 // TODO(vollick): Eventually, if a non-leaf node has an opacity of less than
204 // 1.0, we'll render to a separate texture, and then apply the alpha.
205 // Currently, we multiply our opacity by all our ancestor's opacities and
206 // use the combined result, but this is only temporary.
207 float GetCombinedOpacity() const;
208
[email protected]00b86982011-09-01 00:02:09209 // Called during the Draw() pass to freshen the Layer's contents from the
210 // delegate.
211 void UpdateLayerCanvas();
212
[email protected]714e38432011-11-15 17:10:39213 // Called to indicate that a layer's properties have changed and that the
214 // holes for the layers must be recomputed.
215 void SetNeedsToRecomputeHole();
216
217 // Resets |hole_rect_| to the empty rect for all layers below and
218 // including this one.
219 void ClearHoleRects();
220
221 // Does a preorder traversal of layers starting with this layer. Omits layers
222 // which cannot punch a hole in another layer such as non visible layers
223 // and layers which don't fill their bounds opaquely.
224 void GetLayerProperties(const ui::Transform& current_transform,
225 std::vector<LayerProperties>* traverasal);
226
[email protected]c155c252011-07-29 16:17:55227 // A hole in a layer is an area in the layer that does not get drawn
228 // because this area is covered up with another layer which is known to be
229 // opaque.
230 // This method computes the dimension of the hole (if there is one)
231 // based on whether one of its child nodes is always opaque.
[email protected]714e38432011-11-15 17:10:39232 // Note: This method should only be called from the root.
[email protected]c155c252011-07-29 16:17:55233 void RecomputeHole();
234
[email protected]714e38432011-11-15 17:10:39235 void set_hole_rect(const gfx::Rect& hole_rect) {
236 hole_rect_ = hole_rect;
237 }
[email protected]6b29ebb2011-09-29 17:41:20238
239 // Determines the regions that don't intersect |rect| and places the
240 // result in |sides|.
241 //
242 // rect_____________________________
243 // |xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx|
244 // |xxxxxxxxxxxxx top xxxxxxxxxxxxxx|
245 // |________________________________|
246 // |xxxxx| |xxxxx|
247 // |xxxxx|region_to_punch_out |xxxxx|
248 // |left | |right|
249 // |_____|____________________|_____|
250 // |xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx|
251 // |xxxxxxxxxx bottom xxxxxxxxxxxxxx|
252 // |________________________________|
253 static void PunchHole(const gfx::Rect& rect,
254 const gfx::Rect& region_to_punch_out,
255 std::vector<gfx::Rect>* sides);
256
[email protected]714e38432011-11-15 17:10:39257 // Drops texture just for this layer.
258 void DropTexture();
259
[email protected]993d6b322011-09-27 19:14:38260 // Drop all textures for layers below and including this one. Called when
261 // the layer is removed from a hierarchy. Textures will be re-generated if
262 // the layer is subsequently re-attached and needs to be drawn.
263 void DropTextures();
264
[email protected]ad7258912011-08-29 20:33:53265 bool ConvertPointForAncestor(const Layer* ancestor, gfx::Point* point) const;
266 bool ConvertPointFromAncestor(const Layer* ancestor, gfx::Point* point) const;
267
268 bool GetTransformRelativeTo(const Layer* ancestor,
269 Transform* transform) const;
270
[email protected]b4bb9ca2011-09-23 20:53:14271 // The only externally updated layers are ones that get their pixels from
272 // WebKit and WebKit does not produce valid alpha values. All other layers
273 // should have valid alpha.
274 bool has_valid_alpha_channel() const { return !layer_updated_externally_; }
275
[email protected]7fca53d42011-09-29 15:38:12276 // Following are invoked from the animation or if no animation exists to
277 // update the values immediately.
278 void SetBoundsImmediately(const gfx::Rect& bounds);
279 void SetTransformImmediately(const ui::Transform& transform);
280 void SetOpacityImmediately(float opacity);
281
[email protected]fe7074c62011-10-28 15:22:54282 // Implementation of LayerAnimatorDelegate
283 virtual void SetBoundsFromAnimation(const gfx::Rect& bounds) OVERRIDE;
284 virtual void SetTransformFromAnimation(const Transform& transform) OVERRIDE;
285 virtual void SetOpacityFromAnimation(float opacity) OVERRIDE;
286 virtual void ScheduleDrawForAnimation() OVERRIDE;
287 virtual const gfx::Rect& GetBoundsForAnimation() const OVERRIDE;
288 virtual const Transform& GetTransformForAnimation() const OVERRIDE;
289 virtual float GetOpacityForAnimation() const OVERRIDE;
[email protected]7fca53d42011-09-29 15:38:12290
[email protected]332749032011-10-22 00:32:46291#if defined(USE_WEBKIT_COMPOSITOR)
292 void CreateWebLayer();
293 void RecomputeTransform();
[email protected]b9616d592011-11-14 20:04:42294 void RecomputeDrawsContentAndUVRect();
[email protected]332749032011-10-22 00:32:46295#endif
296
[email protected]993d6b322011-09-27 19:14:38297 const LayerType type_;
298
[email protected]adc93fa72011-06-21 19:47:39299 Compositor* compositor_;
300
[email protected]51f1b482011-06-23 16:52:12301 scoped_refptr<ui::Texture> texture_;
[email protected]adc93fa72011-06-21 19:47:39302
303 Layer* parent_;
304
[email protected]ebc335f2011-11-23 00:43:51305 // This layer's children, in bottom-to-top stacking order.
[email protected]adc93fa72011-06-21 19:47:39306 std::vector<Layer*> children_;
307
308 ui::Transform transform_;
309
310 gfx::Rect bounds_;
311
[email protected]e4e8afef2011-10-05 13:58:33312 // Visibility of this layer. See SetVisible/IsDrawn for more details.
[email protected]3aa43942011-09-13 20:59:53313 bool visible_;
314
[email protected]c155c252011-07-29 16:17:55315 bool fills_bounds_opaquely_;
316
317 gfx::Rect hole_rect_;
318
[email protected]714e38432011-11-15 17:10:39319 bool recompute_hole_;
320
[email protected]00b86982011-09-01 00:02:09321 gfx::Rect invalid_rect_;
322
[email protected]a97527b2011-09-14 15:44:38323 // If true the layer is always up to date.
324 bool layer_updated_externally_;
325
[email protected]b4bb9ca2011-09-23 20:53:14326 float opacity_;
327
[email protected]33699e622011-11-17 18:29:30328 std::string name_;
329
[email protected]00b86982011-09-01 00:02:09330 LayerDelegate* delegate_;
331
[email protected]fe7074c62011-10-28 15:22:54332 scoped_ptr<LayerAnimator> animator_;
[email protected]7fca53d42011-09-29 15:38:12333
[email protected]332749032011-10-22 00:32:46334#if defined(USE_WEBKIT_COMPOSITOR)
[email protected]4b58d552011-10-26 00:54:46335 WebKit::WebLayer web_layer_;
336 bool web_layer_is_accelerated_;
[email protected]332749032011-10-22 00:32:46337#endif
338
[email protected]adc93fa72011-06-21 19:47:39339 DISALLOW_COPY_AND_ASSIGN(Layer);
340};
341
342} // namespace ui
343
344#endif // UI_GFX_COMPOSITOR_LAYER_H_