summaryrefslogtreecommitdiffstats
path: root/ui/gfx/compositor/layer.h
blob: 7bbc890a41f7a3c5d8617ba1f5b74519b5a99296 (plain)
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
// Copyright (c) 2011 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_GFX_COMPOSITOR_LAYER_H_
#define UI_GFX_COMPOSITOR_LAYER_H_
#pragma once

#include <vector>

#include "base/memory/ref_counted.h"
#include "ui/gfx/rect.h"
#include "ui/gfx/transform.h"
#include "ui/gfx/compositor/compositor.h"

class SkCanvas;

namespace ui {

class Compositor;
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.
//
// 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:
  explicit Layer(Compositor* compositor);
  ~Layer();

  // Adds a new Layer to this Layer.
  void Add(Layer* child);

  // Removes a Layer from this Layer.
  void Remove(Layer* child);

  // Returns the child Layers.
  const std::vector<Layer*>& children() { return children_; }

  // The parent.
  const Layer* parent() const { return parent_; }
  Layer* parent() { return parent_; }

  // The transform, relative to the parent.
  void SetTransform(const ui::Transform& transform);
  const ui::Transform& transform() const { return transform_; }

  // The bounds, relative to the parent.
  void SetBounds(const gfx::Rect& bounds);
  const gfx::Rect& bounds() const { return bounds_; }

  // See description in View for details
  void SetFillsBoundsOpaquely(bool fills_bounds_opaquely);
  bool fills_bounds_opaquely() const { return fills_bounds_opaquely_; }

  const gfx::Rect& hole_rect() const {  return hole_rect_; }

  // The compositor.
  const Compositor* compositor() const { return compositor_; }
  Compositor* compositor() { return compositor_; }

  // Passing NULL will cause the layer to get a texture from its compositor.
  void SetTexture(ui::Texture* texture);
  const ui::Texture* texture() const { return texture_.get(); }

  // Resets the canvas of the texture.
  void SetCanvas(const SkCanvas& canvas, const gfx::Point& origin);

// Draws the layer with hole if hole is non empty.
// hole looks like:
//
//  layer____________________________
//  |xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx|
//  |xxxxxxxxxxxxx top xxxxxxxxxxxxxx|
//  |________________________________|
//  |xxxxx|                    |xxxxx|
//  |xxxxx|      Hole Rect     |xxxxx|
//  |left | (not composited)   |right|
//  |_____|____________________|_____|
//  |xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx|
//  |xxxxxxxxxx bottom xxxxxxxxxxxxxx|
//  |________________________________|
//
// Legend:
//   composited area: x
  void Draw();

 private:
  // calls Texture::Draw only if the region to be drawn is non empty
  void DrawRegion(const ui::TextureDrawParams& params,
                  const gfx::Rect& region_to_draw);

  // A hole in a layer is an area in the layer that does not get drawn
  // because this area is covered up with another layer which is known to be
  // opaque.
  // This method computes the dimension of the hole (if there is one)
  // based on whether one of its child nodes is always opaque.
  // Note: For simpicity's sake, currently a hole is only created if the child
  // view has no transfrom with respect to its parent.
  void RecomputeHole();

  Compositor* compositor_;

  scoped_refptr<ui::Texture> texture_;

  Layer* parent_;

  std::vector<Layer*> children_;

  ui::Transform transform_;

  gfx::Rect bounds_;

  bool fills_bounds_opaquely_;

  gfx::Rect hole_rect_;

  DISALLOW_COPY_AND_ASSIGN(Layer);
};

}  // namespace ui

#endif  // UI_GFX_COMPOSITOR_LAYER_H_