blob: d523c9cf6e7de57acec60f623992d1a889ed0459 [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]adc93fa72011-06-21 19:47:3912#include "ui/gfx/rect.h"
13#include "ui/gfx/transform.h"
[email protected]c155c252011-07-29 16:17:5514#include "ui/gfx/compositor/compositor.h"
[email protected]adc93fa72011-06-21 19:47:3915
[email protected]892ad8a2011-07-27 02:47:2216class SkCanvas;
[email protected]adc93fa72011-06-21 19:47:3917
18namespace ui {
19
20class Compositor;
21class 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]fe377e12011-08-18 17:37:3629class COMPOSITOR_EXPORT Layer {
[email protected]adc93fa72011-06-21 19:47:3930 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]ad7258912011-08-29 20:33:5347 // Returns true if this Layer contains |other| somewhere in its children.
48 bool Contains(const Layer* other) const;
49
[email protected]adc93fa72011-06-21 19:47:3950 // The transform, relative to the parent.
[email protected]c155c252011-07-29 16:17:5551 void SetTransform(const ui::Transform& transform);
[email protected]adc93fa72011-06-21 19:47:3952 const ui::Transform& transform() const { return transform_; }
53
54 // The bounds, relative to the parent.
[email protected]c155c252011-07-29 16:17:5555 void SetBounds(const gfx::Rect& bounds);
[email protected]adc93fa72011-06-21 19:47:3956 const gfx::Rect& bounds() const { return bounds_; }
57
[email protected]ad7258912011-08-29 20:33:5358 // 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]c155c252011-07-29 16:17:5565 // 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]adc93fa72011-06-21 19:47:3971 // The compositor.
72 const Compositor* compositor() const { return compositor_; }
73 Compositor* compositor() { return compositor_; }
74
[email protected]1cbbee3c2011-06-24 12:32:1975 // Passing NULL will cause the layer to get a texture from its compositor.
76 void SetTexture(ui::Texture* texture);
[email protected]536987c82011-06-28 03:46:0877 const ui::Texture* texture() const { return texture_.get(); }
[email protected]1cbbee3c2011-06-24 12:32:1978
[email protected]892ad8a2011-07-27 02:47:2279 // Resets the canvas of the texture.
80 void SetCanvas(const SkCanvas& canvas, const gfx::Point& origin);
[email protected]adc93fa72011-06-21 19:47:3981
[email protected]c155c252011-07-29 16:17:5582// 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]adc93fa72011-06-21 19:47:3999 void Draw();
100
101 private:
[email protected]c155c252011-07-29 16:17:55102 // 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]ad7258912011-08-29 20:33:53115 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]adc93fa72011-06-21 19:47:39121 Compositor* compositor_;
122
[email protected]51f1b482011-06-23 16:52:12123 scoped_refptr<ui::Texture> texture_;
[email protected]adc93fa72011-06-21 19:47:39124
125 Layer* parent_;
126
127 std::vector<Layer*> children_;
128
129 ui::Transform transform_;
130
131 gfx::Rect bounds_;
132
[email protected]c155c252011-07-29 16:17:55133 bool fills_bounds_opaquely_;
134
135 gfx::Rect hole_rect_;
136
[email protected]adc93fa72011-06-21 19:47:39137 DISALLOW_COPY_AND_ASSIGN(Layer);
138};
139
140} // namespace ui
141
142#endif // UI_GFX_COMPOSITOR_LAYER_H_