[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] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 12 | #include "ui/gfx/rect.h" |
| 13 | #include "ui/gfx/transform.h" |
[email protected] | c155c25 | 2011-07-29 16:17:55 | [diff] [blame] | 14 | #include "ui/gfx/compositor/compositor.h" |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 15 | |
[email protected] | 892ad8a | 2011-07-27 02:47:22 | [diff] [blame] | 16 | class SkCanvas; |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 17 | |
| 18 | namespace ui { |
| 19 | |
| 20 | class Compositor; |
| 21 | class Texture; |
| 22 | |
| 23 | // Layer manages a texture, transform and a set of child Layers. Any View that |
| 24 | // has enabled layers ends up creating a Layer to manage the texture. |
| 25 | // |
| 26 | // NOTE: unlike Views, each Layer does *not* own its children views. If you |
| 27 | // delete a Layer and it has children, the parent of each child layer is set to |
| 28 | // NULL, but the children are not deleted. |
[email protected] | fe377e1 | 2011-08-18 17:37:36 | [diff] [blame] | 29 | class COMPOSITOR_EXPORT Layer { |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 30 | public: |
| 31 | explicit Layer(Compositor* compositor); |
| 32 | ~Layer(); |
| 33 | |
| 34 | // Adds a new Layer to this Layer. |
| 35 | void Add(Layer* child); |
| 36 | |
| 37 | // Removes a Layer from this Layer. |
| 38 | void Remove(Layer* child); |
| 39 | |
| 40 | // Returns the child Layers. |
| 41 | const std::vector<Layer*>& children() { return children_; } |
| 42 | |
| 43 | // The parent. |
| 44 | const Layer* parent() const { return parent_; } |
| 45 | Layer* parent() { return parent_; } |
| 46 | |
[email protected] | ad725891 | 2011-08-29 20:33:53 | [diff] [blame^] | 47 | // Returns true if this Layer contains |other| somewhere in its children. |
| 48 | bool Contains(const Layer* other) const; |
| 49 | |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 50 | // The transform, relative to the parent. |
[email protected] | c155c25 | 2011-07-29 16:17:55 | [diff] [blame] | 51 | void SetTransform(const ui::Transform& transform); |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 52 | const ui::Transform& transform() const { return transform_; } |
| 53 | |
| 54 | // The bounds, relative to the parent. |
[email protected] | c155c25 | 2011-07-29 16:17:55 | [diff] [blame] | 55 | void SetBounds(const gfx::Rect& bounds); |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 56 | const gfx::Rect& bounds() const { return bounds_; } |
| 57 | |
[email protected] | ad725891 | 2011-08-29 20:33:53 | [diff] [blame^] | 58 | // Converts a point from the coordinates of |source| to the coordinates of |
| 59 | // |target|. Necessarily, |source| and |target| must inhabit the same Layer |
| 60 | // tree. |
| 61 | static void ConvertPointToLayer(const Layer* source, |
| 62 | const Layer* target, |
| 63 | gfx::Point* point); |
| 64 | |
[email protected] | c155c25 | 2011-07-29 16:17:55 | [diff] [blame] | 65 | // See description in View for details |
| 66 | void SetFillsBoundsOpaquely(bool fills_bounds_opaquely); |
| 67 | bool fills_bounds_opaquely() const { return fills_bounds_opaquely_; } |
| 68 | |
| 69 | const gfx::Rect& hole_rect() const { return hole_rect_; } |
| 70 | |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 71 | // The compositor. |
| 72 | const Compositor* compositor() const { return compositor_; } |
| 73 | Compositor* compositor() { return compositor_; } |
| 74 | |
[email protected] | 1cbbee3c | 2011-06-24 12:32:19 | [diff] [blame] | 75 | // Passing NULL will cause the layer to get a texture from its compositor. |
| 76 | void SetTexture(ui::Texture* texture); |
[email protected] | 536987c8 | 2011-06-28 03:46:08 | [diff] [blame] | 77 | const ui::Texture* texture() const { return texture_.get(); } |
[email protected] | 1cbbee3c | 2011-06-24 12:32:19 | [diff] [blame] | 78 | |
[email protected] | 892ad8a | 2011-07-27 02:47:22 | [diff] [blame] | 79 | // Resets the canvas of the texture. |
| 80 | void SetCanvas(const SkCanvas& canvas, const gfx::Point& origin); |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 81 | |
[email protected] | c155c25 | 2011-07-29 16:17:55 | [diff] [blame] | 82 | // Draws the layer with hole if hole is non empty. |
| 83 | // hole looks like: |
| 84 | // |
| 85 | // layer____________________________ |
| 86 | // |xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx| |
| 87 | // |xxxxxxxxxxxxx top xxxxxxxxxxxxxx| |
| 88 | // |________________________________| |
| 89 | // |xxxxx| |xxxxx| |
| 90 | // |xxxxx| Hole Rect |xxxxx| |
| 91 | // |left | (not composited) |right| |
| 92 | // |_____|____________________|_____| |
| 93 | // |xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx| |
| 94 | // |xxxxxxxxxx bottom xxxxxxxxxxxxxx| |
| 95 | // |________________________________| |
| 96 | // |
| 97 | // Legend: |
| 98 | // composited area: x |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 99 | void Draw(); |
| 100 | |
| 101 | private: |
[email protected] | c155c25 | 2011-07-29 16:17:55 | [diff] [blame] | 102 | // calls Texture::Draw only if the region to be drawn is non empty |
| 103 | void DrawRegion(const ui::TextureDrawParams& params, |
| 104 | const gfx::Rect& region_to_draw); |
| 105 | |
| 106 | // A hole in a layer is an area in the layer that does not get drawn |
| 107 | // because this area is covered up with another layer which is known to be |
| 108 | // opaque. |
| 109 | // This method computes the dimension of the hole (if there is one) |
| 110 | // based on whether one of its child nodes is always opaque. |
| 111 | // Note: For simpicity's sake, currently a hole is only created if the child |
| 112 | // view has no transfrom with respect to its parent. |
| 113 | void RecomputeHole(); |
| 114 | |
[email protected] | ad725891 | 2011-08-29 20:33:53 | [diff] [blame^] | 115 | bool ConvertPointForAncestor(const Layer* ancestor, gfx::Point* point) const; |
| 116 | bool ConvertPointFromAncestor(const Layer* ancestor, gfx::Point* point) const; |
| 117 | |
| 118 | bool GetTransformRelativeTo(const Layer* ancestor, |
| 119 | Transform* transform) const; |
| 120 | |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 121 | Compositor* compositor_; |
| 122 | |
[email protected] | 51f1b48 | 2011-06-23 16:52:12 | [diff] [blame] | 123 | scoped_refptr<ui::Texture> texture_; |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 124 | |
| 125 | Layer* parent_; |
| 126 | |
| 127 | std::vector<Layer*> children_; |
| 128 | |
| 129 | ui::Transform transform_; |
| 130 | |
| 131 | gfx::Rect bounds_; |
| 132 | |
[email protected] | c155c25 | 2011-07-29 16:17:55 | [diff] [blame] | 133 | bool fills_bounds_opaquely_; |
| 134 | |
| 135 | gfx::Rect hole_rect_; |
| 136 | |
[email protected] | adc93fa7 | 2011-06-21 19:47:39 | [diff] [blame] | 137 | DISALLOW_COPY_AND_ASSIGN(Layer); |
| 138 | }; |
| 139 | |
| 140 | } // namespace ui |
| 141 | |
| 142 | #endif // UI_GFX_COMPOSITOR_LAYER_H_ |