OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef UI_GFX_COMPOSITOR_LAYER_H_ | |
6 #define UI_GFX_COMPOSITOR_LAYER_H_ | |
7 #pragma once | |
8 | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/compiler_specific.h" | |
13 #include "base/memory/ref_counted.h" | |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "base/message_loop.h" | |
16 #include "third_party/skia/include/core/SkColor.h" | |
17 #include "third_party/skia/include/core/SkRegion.h" | |
18 #include "third_party/WebKit/Source/Platform/chromium/public/WebContentLayerClie
nt.h" | |
19 #include "third_party/WebKit/Source/Platform/chromium/public/WebLayer.h" | |
20 #include "ui/gfx/rect.h" | |
21 #include "ui/gfx/transform.h" | |
22 #include "ui/gfx/compositor/compositor.h" | |
23 #include "ui/gfx/compositor/layer_animation_delegate.h" | |
24 #include "ui/gfx/compositor/layer_delegate.h" | |
25 #include "ui/gfx/compositor/layer_type.h" | |
26 | |
27 class SkCanvas; | |
28 | |
29 namespace ui { | |
30 | |
31 class Compositor; | |
32 class LayerAnimator; | |
33 class Texture; | |
34 | |
35 // Layer manages a texture, transform and a set of child Layers. Any View that | |
36 // has enabled layers ends up creating a Layer to manage the texture. | |
37 // A Layer can also be created without a texture, in which case it renders | |
38 // nothing and is simply used as a node in a hierarchy of layers. | |
39 // | |
40 // NOTE: unlike Views, each Layer does *not* own its children views. If you | |
41 // delete a Layer and it has children, the parent of each child layer is set to | |
42 // NULL, but the children are not deleted. | |
43 class COMPOSITOR_EXPORT Layer : | |
44 public LayerAnimationDelegate, | |
45 NON_EXPORTED_BASE(public WebKit::WebContentLayerClient) { | |
46 public: | |
47 Layer(); | |
48 explicit Layer(LayerType type); | |
49 virtual ~Layer(); | |
50 | |
51 // Retrieves the Layer's compositor. The Layer will walk up its parent chain | |
52 // to locate it. Returns NULL if the Layer is not attached to a compositor. | |
53 Compositor* GetCompositor(); | |
54 | |
55 // Called by the compositor when the Layer is set as its root Layer. This can | |
56 // only ever be called on the root layer. | |
57 void SetCompositor(Compositor* compositor); | |
58 | |
59 LayerDelegate* delegate() { return delegate_; } | |
60 void set_delegate(LayerDelegate* delegate) { delegate_ = delegate; } | |
61 | |
62 // Adds a new Layer to this Layer. | |
63 void Add(Layer* child); | |
64 | |
65 // Removes a Layer from this Layer. | |
66 void Remove(Layer* child); | |
67 | |
68 // Stacks |child| above all other children. | |
69 void StackAtTop(Layer* child); | |
70 | |
71 // Stacks |child| directly above |other|. Both must be children of this | |
72 // layer. Note that if |child| is initially stacked even higher, calling this | |
73 // method will result in |child| being lowered in the stacking order. | |
74 void StackAbove(Layer* child, Layer* other); | |
75 | |
76 // Stacks |child| below all other children. | |
77 void StackAtBottom(Layer* child); | |
78 | |
79 // Stacks |child| directly below |other|. Both must be children of this | |
80 // layer. | |
81 void StackBelow(Layer* child, Layer* other); | |
82 | |
83 // Returns the child Layers. | |
84 const std::vector<Layer*>& children() const { return children_; } | |
85 | |
86 // The parent. | |
87 const Layer* parent() const { return parent_; } | |
88 Layer* parent() { return parent_; } | |
89 | |
90 LayerType type() const { return type_; } | |
91 | |
92 // Returns true if this Layer contains |other| somewhere in its children. | |
93 bool Contains(const Layer* other) const; | |
94 | |
95 // The layer's animator is responsible for causing automatic animations when | |
96 // properties are set. It also manages a queue of pending animations and | |
97 // handles blending of animations. The layer takes ownership of the animator. | |
98 void SetAnimator(LayerAnimator* animator); | |
99 | |
100 // Returns the layer's animator. Creates a default animator of one has not | |
101 // been set. Will not return NULL. | |
102 LayerAnimator* GetAnimator(); | |
103 | |
104 // The transform, relative to the parent. | |
105 void SetTransform(const Transform& transform); | |
106 const Transform& transform() const { return transform_; } | |
107 | |
108 // Return the target transform if animator is running, or the current | |
109 // transform otherwise. | |
110 Transform GetTargetTransform() const; | |
111 | |
112 // The bounds, relative to the parent. | |
113 void SetBounds(const gfx::Rect& bounds); | |
114 const gfx::Rect& bounds() const { return bounds_; } | |
115 | |
116 // Return the target bounds if animator is running, or the current bounds | |
117 // otherwise. | |
118 gfx::Rect GetTargetBounds() const; | |
119 | |
120 // Sets/gets whether or not drawing of child layers should be clipped to the | |
121 // bounds of this layer. | |
122 void SetMasksToBounds(bool masks_to_bounds); | |
123 bool GetMasksToBounds() const; | |
124 | |
125 // The opacity of the layer. The opacity is applied to each pixel of the | |
126 // texture (resulting alpha = opacity * alpha). | |
127 float opacity() const { return opacity_; } | |
128 void SetOpacity(float opacity); | |
129 | |
130 // Blur pixels by this amount in anything below the layer and visible through | |
131 // the layer. | |
132 int background_blur() const { return background_blur_radius_; } | |
133 void SetBackgroundBlur(int blur_radius); | |
134 | |
135 // Return the target opacity if animator is running, or the current opacity | |
136 // otherwise. | |
137 float GetTargetOpacity() const; | |
138 | |
139 // Sets the visibility of the Layer. A Layer may be visible but not | |
140 // drawn. This happens if any ancestor of a Layer is not visible. | |
141 void SetVisible(bool visible); | |
142 bool visible() const { return visible_; } | |
143 | |
144 // Returns the target visibility if the animator is running. Otherwise, it | |
145 // returns the current visibility. | |
146 bool GetTargetVisibility() const; | |
147 | |
148 // Returns true if this Layer is drawn. A Layer is drawn only if all ancestors | |
149 // are visible. | |
150 bool IsDrawn() const; | |
151 | |
152 // Returns true if this layer can have a texture (has_texture_ is true) | |
153 // and is not completely obscured by a child. | |
154 bool ShouldDraw() const; | |
155 | |
156 // Converts a point from the coordinates of |source| to the coordinates of | |
157 // |target|. Necessarily, |source| and |target| must inhabit the same Layer | |
158 // tree. | |
159 static void ConvertPointToLayer(const Layer* source, | |
160 const Layer* target, | |
161 gfx::Point* point); | |
162 | |
163 // See description in View for details | |
164 void SetFillsBoundsOpaquely(bool fills_bounds_opaquely); | |
165 bool fills_bounds_opaquely() const { return fills_bounds_opaquely_; } | |
166 | |
167 const std::string& name() const { return name_; } | |
168 void set_name(const std::string& name) { name_ = name; } | |
169 | |
170 const ui::Texture* texture() const { return texture_.get(); } | |
171 | |
172 // Assigns a new external texture. |texture| can be NULL to disable external | |
173 // updates. | |
174 void SetExternalTexture(ui::Texture* texture); | |
175 | |
176 // Sets the layer's fill color. May only be called for LAYER_SOLID_COLOR. | |
177 void SetColor(SkColor color); | |
178 | |
179 // Adds |invalid_rect| to the Layer's pending invalid rect and calls | |
180 // ScheduleDraw(). Returns false if the paint request is ignored. | |
181 bool SchedulePaint(const gfx::Rect& invalid_rect); | |
182 | |
183 // Schedules a redraw of the layer tree at the compositor. | |
184 // Note that this _does not_ invalidate any region of this layer; use | |
185 // SchedulePaint() for that. | |
186 void ScheduleDraw(); | |
187 | |
188 // Sends damaged rectangles recorded in |damaged_region_| to | |
189 // |compostior_| to repaint the content. | |
190 void SendDamagedRects(); | |
191 | |
192 // Suppresses painting the content by disgarding damaged region and ignoring | |
193 // new paint requests. | |
194 void SuppressPaint(); | |
195 | |
196 // Sometimes the Layer is being updated by something other than SetCanvas | |
197 // (e.g. the GPU process on UI_COMPOSITOR_IMAGE_TRANSPORT). | |
198 bool layer_updated_externally() const { return layer_updated_externally_; } | |
199 | |
200 // WebContentLayerClient | |
201 virtual void paintContents(WebKit::WebCanvas*, const WebKit::WebRect& clip); | |
202 | |
203 WebKit::WebLayer web_layer() { return web_layer_; } | |
204 | |
205 private: | |
206 struct LayerProperties { | |
207 public: | |
208 ui::Layer* layer; | |
209 ui::Transform transform_relative_to_root; | |
210 }; | |
211 | |
212 // TODO(vollick): Eventually, if a non-leaf node has an opacity of less than | |
213 // 1.0, we'll render to a separate texture, and then apply the alpha. | |
214 // Currently, we multiply our opacity by all our ancestor's opacities and | |
215 // use the combined result, but this is only temporary. | |
216 float GetCombinedOpacity() const; | |
217 | |
218 // Stacks |child| above or below |other|. Helper method for StackAbove() and | |
219 // StackBelow(). | |
220 void StackRelativeTo(Layer* child, Layer* other, bool above); | |
221 | |
222 bool ConvertPointForAncestor(const Layer* ancestor, gfx::Point* point) const; | |
223 bool ConvertPointFromAncestor(const Layer* ancestor, gfx::Point* point) const; | |
224 | |
225 bool GetTransformRelativeTo(const Layer* ancestor, | |
226 Transform* transform) const; | |
227 | |
228 // The only externally updated layers are ones that get their pixels from | |
229 // WebKit and WebKit does not produce valid alpha values. All other layers | |
230 // should have valid alpha. | |
231 bool has_valid_alpha_channel() const { return !layer_updated_externally_; } | |
232 | |
233 // Following are invoked from the animation or if no animation exists to | |
234 // update the values immediately. | |
235 void SetBoundsImmediately(const gfx::Rect& bounds); | |
236 void SetTransformImmediately(const ui::Transform& transform); | |
237 void SetOpacityImmediately(float opacity); | |
238 void SetVisibilityImmediately(bool visibility); | |
239 | |
240 // Implementation of LayerAnimatorDelegate | |
241 virtual void SetBoundsFromAnimation(const gfx::Rect& bounds) OVERRIDE; | |
242 virtual void SetTransformFromAnimation(const Transform& transform) OVERRIDE; | |
243 virtual void SetOpacityFromAnimation(float opacity) OVERRIDE; | |
244 virtual void SetVisibilityFromAnimation(bool visibility) OVERRIDE; | |
245 virtual void ScheduleDrawForAnimation() OVERRIDE; | |
246 virtual const gfx::Rect& GetBoundsForAnimation() const OVERRIDE; | |
247 virtual const Transform& GetTransformForAnimation() const OVERRIDE; | |
248 virtual float GetOpacityForAnimation() const OVERRIDE; | |
249 virtual bool GetVisibilityForAnimation() const OVERRIDE; | |
250 | |
251 void CreateWebLayer(); | |
252 void RecomputeTransform(); | |
253 void RecomputeDrawsContentAndUVRect(); | |
254 void RecomputeDebugBorderColor(); | |
255 | |
256 const LayerType type_; | |
257 | |
258 Compositor* compositor_; | |
259 | |
260 scoped_refptr<ui::Texture> texture_; | |
261 | |
262 Layer* parent_; | |
263 | |
264 // This layer's children, in bottom-to-top stacking order. | |
265 std::vector<Layer*> children_; | |
266 | |
267 ui::Transform transform_; | |
268 | |
269 gfx::Rect bounds_; | |
270 | |
271 // Visibility of this layer. See SetVisible/IsDrawn for more details. | |
272 bool visible_; | |
273 | |
274 bool fills_bounds_opaquely_; | |
275 | |
276 // If true the layer is always up to date. | |
277 bool layer_updated_externally_; | |
278 | |
279 // Union of damaged rects to be used when compositor is ready to | |
280 // paint the content. | |
281 SkRegion damaged_region_; | |
282 | |
283 float opacity_; | |
284 int background_blur_radius_; | |
285 | |
286 std::string name_; | |
287 | |
288 LayerDelegate* delegate_; | |
289 | |
290 scoped_ptr<LayerAnimator> animator_; | |
291 | |
292 WebKit::WebLayer web_layer_; | |
293 bool web_layer_is_accelerated_; | |
294 bool show_debug_borders_; | |
295 | |
296 DISALLOW_COPY_AND_ASSIGN(Layer); | |
297 }; | |
298 | |
299 } // namespace ui | |
300 | |
301 #endif // UI_GFX_COMPOSITOR_LAYER_H_ | |
OLD | NEW |