Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(158)

Side by Side Diff: content/common/gpu/ca_layer_partial_damage_tree_mac.mm

Issue 1647523002: Mac overlays: Move CALayerPartialDamageTree to its own file (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 #include "content/common/gpu/ca_layer_partial_damage_tree_mac.h"
6
7 #include "base/command_line.h"
8 #include "base/mac/scoped_nsobject.h"
9 #include "base/mac/sdk_forward_declarations.h"
10 #include "ui/base/ui_base_switches.h"
11 #include "ui/gfx/transform.h"
12
13 @interface CALayer(Private)
14 -(void)setContentsChanged;
15 @end
16
17 namespace content {
18 namespace {
19
20 // When selecting a CALayer to re-use for partial damage, this is the maximum
21 // fraction of the merged layer's pixels that may be not-updated by the swap
22 // before we consider the CALayer to not be a good enough match, and create a
23 // new one.
24 const float kMaximumPartialDamageWasteFraction = 1.2f;
25
26 // The maximum number of partial damage layers that may be created before we
27 // give up and remove them all (doing full damage in the process).
28 const size_t kMaximumPartialDamageLayers = 8;
29
30 } // namespace
31
32 class CALayerPartialDamageTree::OverlayPlane {
33 public:
34 static linked_ptr<OverlayPlane> CreateWithFrameRect(
35 int z_order,
36 base::ScopedCFTypeRef<IOSurfaceRef> io_surface,
37 const gfx::RectF& pixel_frame_rect,
38 const gfx::RectF& contents_rect) {
39 gfx::Transform transform;
40 transform.Translate(pixel_frame_rect.x(), pixel_frame_rect.y());
41 return linked_ptr<OverlayPlane>(
42 new OverlayPlane(z_order, io_surface, contents_rect, pixel_frame_rect));
43 }
44
45 ~OverlayPlane() {
46 [ca_layer setContents:nil];
47 [ca_layer removeFromSuperlayer];
48 ca_layer.reset();
49 }
50
51 const int z_order;
52 const base::ScopedCFTypeRef<IOSurfaceRef> io_surface;
53 const gfx::RectF contents_rect;
54 const gfx::RectF pixel_frame_rect;
55 bool layer_needs_update;
56 base::scoped_nsobject<CALayer> ca_layer;
57
58 void TakeCALayerFrom(OverlayPlane* other_plane) {
59 ca_layer.swap(other_plane->ca_layer);
60 }
61
62 void UpdateProperties(float scale_factor) {
63 if (layer_needs_update) {
64 [ca_layer setOpaque:YES];
65
66 id new_contents = static_cast<id>(io_surface.get());
67 if ([ca_layer contents] == new_contents && z_order == 0)
68 [ca_layer setContentsChanged];
69 else
70 [ca_layer setContents:new_contents];
71 [ca_layer setContentsRect:contents_rect.ToCGRect()];
72
73 [ca_layer setAnchorPoint:CGPointZero];
74
75 if ([ca_layer respondsToSelector:(@selector(setContentsScale:))])
76 [ca_layer setContentsScale:scale_factor];
77 gfx::RectF dip_frame_rect = gfx::RectF(pixel_frame_rect);
78 dip_frame_rect.Scale(1 / scale_factor);
79 [ca_layer setBounds:CGRectMake(0, 0, dip_frame_rect.width(),
80 dip_frame_rect.height())];
81 [ca_layer
82 setPosition:CGPointMake(dip_frame_rect.x(), dip_frame_rect.y())];
83 }
84 static bool show_borders =
85 base::CommandLine::ForCurrentProcess()->HasSwitch(
86 switches::kShowMacOverlayBorders);
87 if (show_borders) {
88 base::ScopedCFTypeRef<CGColorRef> color;
89 if (!layer_needs_update) {
90 // Green represents contents that are unchanged across frames.
91 color.reset(CGColorCreateGenericRGB(0, 1, 0, 1));
92 } else {
93 // Red represents damaged contents.
94 color.reset(CGColorCreateGenericRGB(1, 0, 0, 1));
95 }
96 [ca_layer setBorderWidth:1];
97 [ca_layer setBorderColor:color];
98 }
99 layer_needs_update = false;
100 }
101
102 private:
103 OverlayPlane(int z_order,
104 base::ScopedCFTypeRef<IOSurfaceRef> io_surface,
105 const gfx::RectF& contents_rect,
106 const gfx::RectF& pixel_frame_rect)
107 : z_order(z_order),
108 io_surface(io_surface),
109 contents_rect(contents_rect),
110 pixel_frame_rect(pixel_frame_rect),
111 layer_needs_update(true) {}
112 };
113
114 void CALayerPartialDamageTree::UpdateRootAndPartialDamagePlanes(
115 CALayerPartialDamageTree* old_tree,
116 const gfx::RectF& pixel_damage_rect) {
117 // This is the plane that will be updated this frame. It may be the root plane
118 // or a child plane.
119 linked_ptr<OverlayPlane> plane_for_swap;
120
121 // If the frame's size changed, if we haven't updated the root layer, if
122 // we have full damage, or if we don't support remote layers, then use the
123 // root layer directly.
124 if (!allow_partial_swap_ || !old_tree ||
125 old_tree->root_plane_->pixel_frame_rect !=
126 root_plane_->pixel_frame_rect ||
127 pixel_damage_rect == root_plane_->pixel_frame_rect) {
128 plane_for_swap = root_plane_;
129 }
130
131 // Walk though the old tree's partial damage layers and see if there is one
132 // that is appropriate to re-use.
133 if (!plane_for_swap.get() && !pixel_damage_rect.IsEmpty()) {
134 gfx::RectF plane_to_reuse_dip_enlarged_rect;
135
136 // Find the last partial damage plane to re-use the CALayer from. Grow the
137 // new rect for this layer to include this damage, and all nearby partial
138 // damage layers.
139 linked_ptr<OverlayPlane> plane_to_reuse;
140 for (auto& old_plane : old_tree->partial_damage_planes_) {
141 gfx::RectF dip_enlarged_rect = old_plane->pixel_frame_rect;
142 dip_enlarged_rect.Union(pixel_damage_rect);
143
144 // Compute the fraction of the pixels that would not be updated by this
145 // swap. If it is too big, try another layer.
146 float waste_fraction = dip_enlarged_rect.size().GetArea() * 1.f /
147 pixel_damage_rect.size().GetArea();
148 if (waste_fraction > kMaximumPartialDamageWasteFraction)
149 continue;
150
151 plane_to_reuse = old_plane;
152 plane_to_reuse_dip_enlarged_rect.Union(dip_enlarged_rect);
153 }
154
155 if (plane_to_reuse.get()) {
156 gfx::RectF enlarged_contents_rect = plane_to_reuse_dip_enlarged_rect;
157 enlarged_contents_rect.Scale(1. / root_plane_->pixel_frame_rect.width(),
158 1. / root_plane_->pixel_frame_rect.height());
159
160 plane_for_swap = OverlayPlane::CreateWithFrameRect(
161 0, root_plane_->io_surface, plane_to_reuse_dip_enlarged_rect,
162 enlarged_contents_rect);
163
164 plane_for_swap->TakeCALayerFrom(plane_to_reuse.get());
165 if (plane_to_reuse != old_tree->partial_damage_planes_.back())
166 [plane_for_swap->ca_layer removeFromSuperlayer];
167 }
168 }
169
170 // If we haven't found an appropriate layer to re-use, create a new one, if
171 // we haven't already created too many.
172 if (!plane_for_swap.get() && !pixel_damage_rect.IsEmpty() &&
173 old_tree->partial_damage_planes_.size() < kMaximumPartialDamageLayers) {
174 gfx::RectF contents_rect = gfx::RectF(pixel_damage_rect);
175 contents_rect.Scale(1. / root_plane_->pixel_frame_rect.width(),
176 1. / root_plane_->pixel_frame_rect.height());
177 plane_for_swap = OverlayPlane::CreateWithFrameRect(
178 0, root_plane_->io_surface, pixel_damage_rect, contents_rect);
179 }
180
181 // And if we still don't have a layer, use the root layer.
182 if (!plane_for_swap.get() && !pixel_damage_rect.IsEmpty())
183 plane_for_swap = root_plane_;
184
185 // Walk all old partial damage planes. Remove anything that is now completely
186 // covered, and move everything else into the new |partial_damage_planes_|.
187 if (old_tree) {
188 for (auto& old_plane : old_tree->partial_damage_planes_) {
189 // Intersect the planes' frames with the new root plane to ensure that
190 // they don't get kept alive inappropriately.
191 gfx::RectF old_plane_frame_rect = old_plane->pixel_frame_rect;
192 old_plane_frame_rect.Intersect(root_plane_->pixel_frame_rect);
193
194 bool old_plane_covered_by_swap = false;
195 if (plane_for_swap.get() &&
196 plane_for_swap->pixel_frame_rect.Contains(old_plane_frame_rect)) {
197 old_plane_covered_by_swap = true;
198 }
199 if (!old_plane_covered_by_swap) {
200 DCHECK(old_plane->ca_layer);
201 partial_damage_planes_.push_back(old_plane);
202 }
203 }
204 if (plane_for_swap != root_plane_)
205 root_plane_ = old_tree->root_plane_;
206 }
207
208 // Finally, add the new swap's plane at the back of the list, if it exists.
209 if (plane_for_swap.get() && plane_for_swap != root_plane_) {
210 partial_damage_planes_.push_back(plane_for_swap);
211 }
212 }
213
214 void CALayerPartialDamageTree::UpdateRootAndPartialDamageCALayers(
215 CALayer* superlayer,
216 float scale_factor) {
217 if (!allow_partial_swap_) {
218 DCHECK(partial_damage_planes_.empty());
219 return;
220 }
221
222 // Allocate and update CALayers for the backbuffer and partial damage layers.
223 if (!root_plane_->ca_layer) {
224 root_plane_->ca_layer.reset([[CALayer alloc] init]);
225 [superlayer setSublayers:nil];
226 [superlayer addSublayer:root_plane_->ca_layer];
227 }
228 for (auto& plane : partial_damage_planes_) {
229 if (!plane->ca_layer) {
230 DCHECK(plane == partial_damage_planes_.back());
231 plane->ca_layer.reset([[CALayer alloc] init]);
232 }
233 if (![plane->ca_layer superlayer]) {
234 DCHECK(plane == partial_damage_planes_.back());
235 [superlayer addSublayer:plane->ca_layer];
236 }
237 }
238 root_plane_->UpdateProperties(scale_factor);
239 for (auto& plane : partial_damage_planes_)
240 plane->UpdateProperties(scale_factor);
241 }
242
243 CALayerPartialDamageTree::CALayerPartialDamageTree(
244 bool allow_partial_swap,
245 base::ScopedCFTypeRef<IOSurfaceRef> io_surface,
246 const gfx::Rect& pixel_frame_rect)
247 : allow_partial_swap_(allow_partial_swap) {
248 root_plane_ = OverlayPlane::CreateWithFrameRect(
249 0, io_surface, gfx::RectF(pixel_frame_rect), gfx::RectF(0, 0, 1, 1));
250 }
251
252 CALayerPartialDamageTree::~CALayerPartialDamageTree() {}
253
254 base::ScopedCFTypeRef<IOSurfaceRef>
255 CALayerPartialDamageTree::RootLayerIOSurface() {
256 return root_plane_->io_surface;
257 }
258
259 void CALayerPartialDamageTree::CommitCALayers(
260 CALayer* superlayer,
261 scoped_ptr<CALayerPartialDamageTree> old_tree,
262 float scale_factor,
263 const gfx::Rect& pixel_damage_rect) {
264 UpdateRootAndPartialDamagePlanes(old_tree.get(),
265 gfx::RectF(pixel_damage_rect));
266 UpdateRootAndPartialDamageCALayers(superlayer, scale_factor);
267 }
268
269 } // namespace content
OLDNEW
« no previous file with comments | « content/common/gpu/ca_layer_partial_damage_tree_mac.h ('k') | content/common/gpu/image_transport_surface_overlay_mac.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698