[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 1 | // 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] | 51f1b48 | 2011-06-23 16:52:12 | [diff] [blame] | 11 | #include "base/memory/ref_counted.h" |
[email protected] | 00b8698 | 2011-09-01 00:02:09 | [diff] [blame] | 12 | #include "base/message_loop.h" |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 13 | #include "ui/gfx/rect.h" |
| 14 | #include "ui/gfx/transform.h" |
[email protected] | c155c25 | 2011-07-29 16:17:55 | [diff] [blame] | 15 | #include "ui/gfx/compositor/compositor.h" |
[email protected] | 00b8698 | 2011-09-01 00:02:09 | [diff] [blame] | 16 | #include "ui/gfx/compositor/layer_delegate.h" |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 17 | |
[email protected] | 892ad8a | 2011-07-27 02:47:22 | [diff] [blame] | 18 | class SkCanvas; |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 19 | |
| 20 | namespace ui { |
| 21 | |
| 22 | class Compositor; |
| 23 | class 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] | 28cd2bb | 2011-09-19 21:04:19 | [diff] [blame] | 27 | // 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] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 29 | // |
| 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] | fe377e1 | 2011-08-18 17:37:36 | [diff] [blame] | 33 | class COMPOSITOR_EXPORT Layer { |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 34 | public: |
[email protected] | 28cd2bb | 2011-09-19 21:04:19 | [diff] [blame] | 35 | enum TextureParam { |
| 36 | LAYER_HAS_NO_TEXTURE = 0, |
| 37 | LAYER_HAS_TEXTURE = 1 |
| 38 | }; |
| 39 | |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 40 | explicit Layer(Compositor* compositor); |
[email protected] | 28cd2bb | 2011-09-19 21:04:19 | [diff] [blame] | 41 | Layer(Compositor* compositor, TextureParam texture_param); |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 42 | ~Layer(); |
| 43 | |
[email protected] | 00b8698 | 2011-09-01 00:02:09 | [diff] [blame] | 44 | LayerDelegate* delegate() { return delegate_; } |
| 45 | void set_delegate(LayerDelegate* delegate) { delegate_ = delegate; } |
| 46 | |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 47 | // 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] | ad725891 | 2011-08-29 20:33:53 | [diff] [blame] | 60 | // Returns true if this Layer contains |other| somewhere in its children. |
| 61 | bool Contains(const Layer* other) const; |
| 62 | |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 63 | // The transform, relative to the parent. |
[email protected] | c155c25 | 2011-07-29 16:17:55 | [diff] [blame] | 64 | void SetTransform(const ui::Transform& transform); |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 65 | const ui::Transform& transform() const { return transform_; } |
| 66 | |
| 67 | // The bounds, relative to the parent. |
[email protected] | c155c25 | 2011-07-29 16:17:55 | [diff] [blame] | 68 | void SetBounds(const gfx::Rect& bounds); |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 69 | const gfx::Rect& bounds() const { return bounds_; } |
| 70 | |
[email protected] | 3aa4394 | 2011-09-13 20:59:53 | [diff] [blame] | 71 | // 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] | ad725891 | 2011-08-29 20:33:53 | [diff] [blame] | 75 | // 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] | c155c25 | 2011-07-29 16:17:55 | [diff] [blame] | 82 | // 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] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 88 | // The compositor. |
| 89 | const Compositor* compositor() const { return compositor_; } |
| 90 | Compositor* compositor() { return compositor_; } |
| 91 | |
[email protected] | 536987c8 | 2011-06-28 03:46:08 | [diff] [blame] | 92 | const ui::Texture* texture() const { return texture_.get(); } |
[email protected] | 1cbbee3c | 2011-06-24 12:32:19 | [diff] [blame] | 93 | |
[email protected] | 28cd2bb | 2011-09-19 21:04:19 | [diff] [blame] | 94 | // |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] | 892ad8a | 2011-07-27 02:47:22 | [diff] [blame] | 100 | // Resets the canvas of the texture. |
| 101 | void SetCanvas(const SkCanvas& canvas, const gfx::Point& origin); |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 102 | |
[email protected] | 00b8698 | 2011-09-01 00:02:09 | [diff] [blame] | 103 | // 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] | 3aa4394 | 2011-09-13 20:59:53 | [diff] [blame] | 108 | // 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] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 125 | void Draw(); |
| 126 | |
[email protected] | 3aa4394 | 2011-09-13 20:59:53 | [diff] [blame] | 127 | // Draws a tree of Layers, by calling Draw() on each in the hierarchy starting |
| 128 | // with the receiver. |
| 129 | void DrawTree(); |
| 130 | |
[email protected] | a97527b | 2011-09-14 15:44:38 | [diff] [blame] | 131 | // 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] | b4bb9ca | 2011-09-23 20:53:14 | [diff] [blame^] | 135 | float opacity() const { return opacity_; } |
| 136 | void SetOpacity(float alpha); |
| 137 | |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 138 | private: |
[email protected] | b4bb9ca | 2011-09-23 20:53:14 | [diff] [blame^] | 139 | // 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] | c155c25 | 2011-07-29 16:17:55 | [diff] [blame] | 145 | // 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] | 00b8698 | 2011-09-01 00:02:09 | [diff] [blame] | 149 | // Called during the Draw() pass to freshen the Layer's contents from the |
| 150 | // delegate. |
| 151 | void UpdateLayerCanvas(); |
| 152 | |
[email protected] | c155c25 | 2011-07-29 16:17:55 | [diff] [blame] | 153 | // 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] | ad725891 | 2011-08-29 20:33:53 | [diff] [blame] | 162 | 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] | b4bb9ca | 2011-09-23 20:53:14 | [diff] [blame^] | 168 | // 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] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 173 | Compositor* compositor_; |
| 174 | |
[email protected] | 51f1b48 | 2011-06-23 16:52:12 | [diff] [blame] | 175 | scoped_refptr<ui::Texture> texture_; |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 176 | |
| 177 | Layer* parent_; |
| 178 | |
| 179 | std::vector<Layer*> children_; |
| 180 | |
| 181 | ui::Transform transform_; |
| 182 | |
| 183 | gfx::Rect bounds_; |
| 184 | |
[email protected] | 3aa4394 | 2011-09-13 20:59:53 | [diff] [blame] | 185 | bool visible_; |
| 186 | |
[email protected] | c155c25 | 2011-07-29 16:17:55 | [diff] [blame] | 187 | bool fills_bounds_opaquely_; |
| 188 | |
| 189 | gfx::Rect hole_rect_; |
| 190 | |
[email protected] | 00b8698 | 2011-09-01 00:02:09 | [diff] [blame] | 191 | gfx::Rect invalid_rect_; |
| 192 | |
[email protected] | a97527b | 2011-09-14 15:44:38 | [diff] [blame] | 193 | // If true the layer is always up to date. |
| 194 | bool layer_updated_externally_; |
| 195 | |
[email protected] | b4bb9ca | 2011-09-23 20:53:14 | [diff] [blame^] | 196 | float opacity_; |
| 197 | |
[email protected] | 00b8698 | 2011-09-01 00:02:09 | [diff] [blame] | 198 | LayerDelegate* delegate_; |
| 199 | |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 200 | DISALLOW_COPY_AND_ASSIGN(Layer); |
| 201 | }; |
| 202 | |
| 203 | } // namespace ui |
| 204 | |
| 205 | #endif // UI_GFX_COMPOSITOR_LAYER_H_ |