blob: 0850fdb8efb4f68911b245a33795bdf6a897bbeb [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]51f1b482011-06-23 16:52:1211#include "base/memory/ref_counted.h"
[email protected]00b86982011-09-01 00:02:0912#include "base/message_loop.h"
[email protected]adc93fa72011-06-21 19:47:3913#include "ui/gfx/rect.h"
14#include "ui/gfx/transform.h"
[email protected]c155c252011-07-29 16:17:5515#include "ui/gfx/compositor/compositor.h"
[email protected]00b86982011-09-01 00:02:0916#include "ui/gfx/compositor/layer_delegate.h"
[email protected]adc93fa72011-06-21 19:47:3917
[email protected]892ad8a2011-07-27 02:47:2218class SkCanvas;
[email protected]adc93fa72011-06-21 19:47:3919
20namespace ui {
21
22class Compositor;
23class Texture;
24
25// Layer manages a texture, transform and a set of child Layers. Any View that
26// has enabled layers ends up creating a Layer to manage the texture.
[email protected]28cd2bb2011-09-19 21:04:1927// A Layer can also be created without a texture, in which case it renders
28// nothing and is simply used as a node in a hierarchy of layers.
[email protected]adc93fa72011-06-21 19:47:3929//
30// NOTE: unlike Views, each Layer does *not* own its children views. If you
31// delete a Layer and it has children, the parent of each child layer is set to
32// NULL, but the children are not deleted.
[email protected]fe377e12011-08-18 17:37:3633class COMPOSITOR_EXPORT Layer {
[email protected]adc93fa72011-06-21 19:47:3934 public:
[email protected]28cd2bb2011-09-19 21:04:1935 enum TextureParam {
36 LAYER_HAS_NO_TEXTURE = 0,
37 LAYER_HAS_TEXTURE = 1
38 };
39
[email protected]adc93fa72011-06-21 19:47:3940 explicit Layer(Compositor* compositor);
[email protected]28cd2bb2011-09-19 21:04:1941 Layer(Compositor* compositor, TextureParam texture_param);
[email protected]adc93fa72011-06-21 19:47:3942 ~Layer();
43
[email protected]00b86982011-09-01 00:02:0944 LayerDelegate* delegate() { return delegate_; }
45 void set_delegate(LayerDelegate* delegate) { delegate_ = delegate; }
46
[email protected]adc93fa72011-06-21 19:47:3947 // Adds a new Layer to this Layer.
48 void Add(Layer* child);
49
50 // Removes a Layer from this Layer.
51 void Remove(Layer* child);
52
53 // Returns the child Layers.
54 const std::vector<Layer*>& children() { return children_; }
55
56 // The parent.
57 const Layer* parent() const { return parent_; }
58 Layer* parent() { return parent_; }
59
[email protected]ad7258912011-08-29 20:33:5360 // Returns true if this Layer contains |other| somewhere in its children.
61 bool Contains(const Layer* other) const;
62
[email protected]adc93fa72011-06-21 19:47:3963 // The transform, relative to the parent.
[email protected]c155c252011-07-29 16:17:5564 void SetTransform(const ui::Transform& transform);
[email protected]adc93fa72011-06-21 19:47:3965 const ui::Transform& transform() const { return transform_; }
66
67 // The bounds, relative to the parent.
[email protected]c155c252011-07-29 16:17:5568 void SetBounds(const gfx::Rect& bounds);
[email protected]adc93fa72011-06-21 19:47:3969 const gfx::Rect& bounds() const { return bounds_; }
70
[email protected]3aa43942011-09-13 20:59:5371 // Sets |visible_|. The Layer is drawn by Draw() only when visible_ is true.
72 bool visible() const { return visible_; }
73 void set_visible(bool visible) { visible_ = visible; }
74
[email protected]ad7258912011-08-29 20:33:5375 // Converts a point from the coordinates of |source| to the coordinates of
76 // |target|. Necessarily, |source| and |target| must inhabit the same Layer
77 // tree.
78 static void ConvertPointToLayer(const Layer* source,
79 const Layer* target,
80 gfx::Point* point);
81
[email protected]c155c252011-07-29 16:17:5582 // See description in View for details
83 void SetFillsBoundsOpaquely(bool fills_bounds_opaquely);
84 bool fills_bounds_opaquely() const { return fills_bounds_opaquely_; }
85
86 const gfx::Rect& hole_rect() const { return hole_rect_; }
87
[email protected]adc93fa72011-06-21 19:47:3988 // The compositor.
89 const Compositor* compositor() const { return compositor_; }
90 Compositor* compositor() { return compositor_; }
91
[email protected]536987c82011-06-28 03:46:0892 const ui::Texture* texture() const { return texture_.get(); }
[email protected]1cbbee3c2011-06-24 12:32:1993
[email protected]28cd2bb2011-09-19 21:04:1994 // |texture| cannot be NULL, and this function cannot be called more than
95 // once.
96 // TODO(beng): This can be removed from the API when we are in a
97 // single-compositor world.
98 void SetExternalTexture(ui::Texture* texture);
99
[email protected]892ad8a2011-07-27 02:47:22100 // Resets the canvas of the texture.
101 void SetCanvas(const SkCanvas& canvas, const gfx::Point& origin);
[email protected]adc93fa72011-06-21 19:47:39102
[email protected]00b86982011-09-01 00:02:09103 // Adds |invalid_rect| to the Layer's pending invalid rect, and schedules a
104 // repaint if the Layer has an associated LayerDelegate that can handle the
105 // repaint.
106 void SchedulePaint(const gfx::Rect& invalid_rect);
107
[email protected]3aa43942011-09-13 20:59:53108 // Draws the layer with hole if hole is non empty.
109 // hole looks like:
110 //
111 // layer____________________________
112 // |xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx|
113 // |xxxxxxxxxxxxx top xxxxxxxxxxxxxx|
114 // |________________________________|
115 // |xxxxx| |xxxxx|
116 // |xxxxx| Hole Rect |xxxxx|
117 // |left | (not composited) |right|
118 // |_____|____________________|_____|
119 // |xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx|
120 // |xxxxxxxxxx bottom xxxxxxxxxxxxxx|
121 // |________________________________|
122 //
123 // Legend:
124 // composited area: x
[email protected]adc93fa72011-06-21 19:47:39125 void Draw();
126
[email protected]3aa43942011-09-13 20:59:53127 // Draws a tree of Layers, by calling Draw() on each in the hierarchy starting
128 // with the receiver.
129 void DrawTree();
130
[email protected]a97527b2011-09-14 15:44:38131 // Sometimes the Layer is being updated by something other than SetCanvas
132 // (e.g. the GPU process on TOUCH_UI).
133 bool layer_updated_externally() const { return layer_updated_externally_; }
134
[email protected]b4bb9ca2011-09-23 20:53:14135 float opacity() const { return opacity_; }
136 void SetOpacity(float alpha);
137
[email protected]adc93fa72011-06-21 19:47:39138 private:
[email protected]b4bb9ca2011-09-23 20:53:14139 // TODO(vollick): Eventually, if a non-leaf node has an opacity of less than
140 // 1.0, we'll render to a separate texture, and then apply the alpha.
141 // Currently, we multiply our opacity by all our ancestor's opacities and
142 // use the combined result, but this is only temporary.
143 float GetCombinedOpacity() const;
144
[email protected]c155c252011-07-29 16:17:55145 // calls Texture::Draw only if the region to be drawn is non empty
146 void DrawRegion(const ui::TextureDrawParams& params,
147 const gfx::Rect& region_to_draw);
148
[email protected]00b86982011-09-01 00:02:09149 // Called during the Draw() pass to freshen the Layer's contents from the
150 // delegate.
151 void UpdateLayerCanvas();
152
[email protected]c155c252011-07-29 16:17:55153 // A hole in a layer is an area in the layer that does not get drawn
154 // because this area is covered up with another layer which is known to be
155 // opaque.
156 // This method computes the dimension of the hole (if there is one)
157 // based on whether one of its child nodes is always opaque.
158 // Note: For simpicity's sake, currently a hole is only created if the child
159 // view has no transfrom with respect to its parent.
160 void RecomputeHole();
161
[email protected]ad7258912011-08-29 20:33:53162 bool ConvertPointForAncestor(const Layer* ancestor, gfx::Point* point) const;
163 bool ConvertPointFromAncestor(const Layer* ancestor, gfx::Point* point) const;
164
165 bool GetTransformRelativeTo(const Layer* ancestor,
166 Transform* transform) const;
167
[email protected]b4bb9ca2011-09-23 20:53:14168 // The only externally updated layers are ones that get their pixels from
169 // WebKit and WebKit does not produce valid alpha values. All other layers
170 // should have valid alpha.
171 bool has_valid_alpha_channel() const { return !layer_updated_externally_; }
172
[email protected]adc93fa72011-06-21 19:47:39173 Compositor* compositor_;
174
[email protected]51f1b482011-06-23 16:52:12175 scoped_refptr<ui::Texture> texture_;
[email protected]adc93fa72011-06-21 19:47:39176
177 Layer* parent_;
178
179 std::vector<Layer*> children_;
180
181 ui::Transform transform_;
182
183 gfx::Rect bounds_;
184
[email protected]3aa43942011-09-13 20:59:53185 bool visible_;
186
[email protected]c155c252011-07-29 16:17:55187 bool fills_bounds_opaquely_;
188
189 gfx::Rect hole_rect_;
190
[email protected]00b86982011-09-01 00:02:09191 gfx::Rect invalid_rect_;
192
[email protected]a97527b2011-09-14 15:44:38193 // If true the layer is always up to date.
194 bool layer_updated_externally_;
195
[email protected]b4bb9ca2011-09-23 20:53:14196 float opacity_;
197
[email protected]00b86982011-09-01 00:02:09198 LayerDelegate* delegate_;
199
[email protected]adc93fa72011-06-21 19:47:39200 DISALLOW_COPY_AND_ASSIGN(Layer);
201};
202
203} // namespace ui
204
205#endif // UI_GFX_COMPOSITOR_LAYER_H_