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