summaryrefslogtreecommitdiff
path: root/chromium/content/common/gpu/ca_layer_tree_mac.h
blob: 163474eb9cddc7b2e195044fd17bbcf391540f28 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright 2016 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 CONTENT_COMMON_GPU_CA_LAYER_TREE_MAC_H_
#define CONTENT_COMMON_GPU_CA_LAYER_TREE_MAC_H_

#include <IOSurface/IOSurface.h>
#include <QuartzCore/QuartzCore.h>
#include <deque>
#include <vector>

#include "base/mac/scoped_cftyperef.h"
#include "base/mac/scoped_nsobject.h"
#include "base/memory/scoped_ptr.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_f.h"
#include "ui/gfx/transform.h"

namespace content {

// The CALayerTree will construct a hierarchy of CALayers from a linear list,
// using the algorithm and structure referenced described in
// https://docs.google.com/document/d/1DtSN9zzvCF44_FQPM7ie01UxGHagQ66zfF5L9HnigQY/edit?usp=sharing
class CALayerTree {
 public:
  CALayerTree();

  // This will remove all CALayers from this tree from their superlayer.
  ~CALayerTree();

  // Append the description of a new CALayer to the tree. This will not
  // create any new CALayers until CommitScheduledCALayers is called. This
  // cannot be called anymore after CommitScheduledCALayers has been called.
  bool ScheduleCALayer(bool is_clipped,
                       const gfx::Rect& clip_rect,
                       unsigned sorting_context_id,
                       const gfx::Transform& transform,
                       base::ScopedCFTypeRef<IOSurfaceRef> io_surface,
                       const gfx::RectF& contents_rect,
                       const gfx::Rect& rect,
                       unsigned background_color,
                       unsigned edge_aa_mask,
                       float opacity);

  // Create a CALayer tree for the scheduled layers, and set |superlayer| to
  // have only this tree as its sublayers. If |old_tree| is non-null, then try
  // to re-use the CALayers of |old_tree| as much as possible. |old_tree| will
  // be destroyed at the end of the function, and any CALayers in it which were
  // not re-used by |this| will be removed from the CALayer hierarchy.
  void CommitScheduledCALayers(CALayer* superlayer,
                               scoped_ptr<CALayerTree> old_tree,
                               float scale_factor);

 private:
  struct RootLayer;
  struct ClipAndSortingLayer;
  struct TransformLayer;
  struct ContentLayer;

  struct RootLayer {
    RootLayer();

    // This will remove |ca_layer| from its superlayer, if |ca_layer| is
    // non-nil.
    ~RootLayer();

    // Append a new content layer, without modifying the actual CALayer
    // structure.
    bool AddContentLayer(bool is_clipped,
                         const gfx::Rect& clip_rect,
                         unsigned sorting_context_id,
                         const gfx::Transform& transform,
                         base::ScopedCFTypeRef<IOSurfaceRef> io_surface,
                         const gfx::RectF& contents_rect,
                         const gfx::Rect& rect,
                         unsigned background_color,
                         unsigned edge_aa_mask,
                         float opacity);

    // Allocate CALayers for this layer and its children, and set their
    // properties appropriately. Re-use the CALayers from |old_layer| if
    // possible. If re-using a CALayer from |old_layer|, reset its |ca_layer|
    // to nil, so that its destructor will not remove an active CALayer.
    void CommitToCA(CALayer* superlayer,
                    RootLayer* old_layer,
                    float scale_factor);

    std::vector<ClipAndSortingLayer> clip_and_sorting_layers;
    base::scoped_nsobject<CALayer> ca_layer;

   private:
    DISALLOW_COPY_AND_ASSIGN(RootLayer);
  };
  struct ClipAndSortingLayer {
    ClipAndSortingLayer(bool is_clipped,
                        gfx::Rect clip_rect,
                        unsigned sorting_context_id,
                        bool is_singleton_sorting_context);
    ClipAndSortingLayer(ClipAndSortingLayer&& layer);

    // See the behavior of RootLayer for the effects of these functions on the
    // |ca_layer| member and |old_layer| argument.
    ~ClipAndSortingLayer();
    void AddContentLayer(const gfx::Transform& transform,
                         base::ScopedCFTypeRef<IOSurfaceRef> io_surface,
                         const gfx::RectF& contents_rect,
                         const gfx::Rect& rect,
                         unsigned background_color,
                         unsigned edge_aa_mask,
                         float opacity);
    void CommitToCA(CALayer* superlayer,
                    ClipAndSortingLayer* old_layer,
                    float scale_factor);

    std::vector<TransformLayer> transform_layers;
    bool is_clipped = false;
    gfx::Rect clip_rect;
    unsigned sorting_context_id = 0;
    bool is_singleton_sorting_context = false;
    base::scoped_nsobject<CALayer> ca_layer;

   private:
    DISALLOW_COPY_AND_ASSIGN(ClipAndSortingLayer);
  };
  struct TransformLayer {
    TransformLayer(const gfx::Transform& transform);
    TransformLayer(TransformLayer&& layer);

    // See the behavior of RootLayer for the effects of these functions on the
    // |ca_layer| member and |old_layer| argument.
    ~TransformLayer();
    void AddContentLayer(base::ScopedCFTypeRef<IOSurfaceRef> io_surface,
                         const gfx::RectF& contents_rect,
                         const gfx::Rect& rect,
                         unsigned background_color,
                         unsigned edge_aa_mask,
                         float opacity);
    void CommitToCA(CALayer* superlayer,
                    TransformLayer* old_layer,
                    float scale_factor);

    gfx::Transform transform;
    std::vector<ContentLayer> content_layers;
    base::scoped_nsobject<CALayer> ca_layer;

   private:
    DISALLOW_COPY_AND_ASSIGN(TransformLayer);
  };
  struct ContentLayer {
    ContentLayer(base::ScopedCFTypeRef<IOSurfaceRef> io_surface,
                 const gfx::RectF& contents_rect,
                 const gfx::Rect& rect,
                 unsigned background_color,
                 unsigned edge_aa_mask,
                 float opacity);
    ContentLayer(ContentLayer&& layer);

    // See the behavior of RootLayer for the effects of these functions on the
    // |ca_layer| member and |old_layer| argument.
    ~ContentLayer();
    void CommitToCA(CALayer* parent,
                    ContentLayer* old_layer,
                    float scale_factor);

    base::ScopedCFTypeRef<IOSurfaceRef> io_surface;
    gfx::RectF contents_rect;
    gfx::Rect rect;
    unsigned background_color = 0;
    unsigned edge_aa_mask = 0;
    float opacity = 1;
    base::scoped_nsobject<CALayer> ca_layer;

   private:
    DISALLOW_COPY_AND_ASSIGN(ContentLayer);
  };

  RootLayer root_layer_;
  float scale_factor_ = 1;
  bool has_committed_ = false;

 private:
  DISALLOW_COPY_AND_ASSIGN(CALayerTree);
};

}  // namespace content

#endif  // CONTENT_COMMON_GPU_CA_LAYER_TREE_MAC_H_