OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/common/gpu/ca_layer_partial_damage_tree_mac.h" | 5 #include "content/common/gpu/ca_layer_partial_damage_tree_mac.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/mac/scoped_nsobject.h" | 8 #include "base/mac/scoped_nsobject.h" |
9 #include "base/mac/sdk_forward_declarations.h" | 9 #include "base/mac/sdk_forward_declarations.h" |
10 #include "ui/base/ui_base_switches.h" | 10 #include "ui/base/ui_base_switches.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 const float kMaximumPartialDamageWasteFraction = 1.2f; | 24 const float kMaximumPartialDamageWasteFraction = 1.2f; |
25 | 25 |
26 // The maximum number of partial damage layers that may be created before we | 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). | 27 // give up and remove them all (doing full damage in the process). |
28 const size_t kMaximumPartialDamageLayers = 8; | 28 const size_t kMaximumPartialDamageLayers = 8; |
29 | 29 |
30 } // namespace | 30 } // namespace |
31 | 31 |
32 class CALayerPartialDamageTree::OverlayPlane { | 32 class CALayerPartialDamageTree::OverlayPlane { |
33 public: | 33 public: |
34 static linked_ptr<OverlayPlane> CreateWithFrameRect( | 34 OverlayPlane(base::ScopedCFTypeRef<IOSurfaceRef> io_surface, |
35 int z_order, | 35 const gfx::Rect& pixel_frame_rect, |
36 base::ScopedCFTypeRef<IOSurfaceRef> io_surface, | 36 const gfx::RectF& contents_rect) |
37 const gfx::RectF& pixel_frame_rect, | 37 : io_surface(io_surface), |
38 const gfx::RectF& contents_rect) { | 38 contents_rect(contents_rect), |
39 gfx::Transform transform; | 39 pixel_frame_rect(pixel_frame_rect), |
40 transform.Translate(pixel_frame_rect.x(), pixel_frame_rect.y()); | 40 layer_needs_update(true) {} |
41 return linked_ptr<OverlayPlane>( | |
42 new OverlayPlane(z_order, io_surface, contents_rect, pixel_frame_rect)); | |
43 } | |
44 | 41 |
45 ~OverlayPlane() { | 42 ~OverlayPlane() { |
46 [ca_layer setContents:nil]; | 43 [ca_layer setContents:nil]; |
47 [ca_layer removeFromSuperlayer]; | 44 [ca_layer removeFromSuperlayer]; |
48 ca_layer.reset(); | 45 ca_layer.reset(); |
49 } | 46 } |
50 | 47 |
51 const int z_order; | |
52 const base::ScopedCFTypeRef<IOSurfaceRef> io_surface; | 48 const base::ScopedCFTypeRef<IOSurfaceRef> io_surface; |
53 const gfx::RectF contents_rect; | 49 const gfx::RectF contents_rect; |
54 const gfx::RectF pixel_frame_rect; | 50 const gfx::Rect pixel_frame_rect; |
55 bool layer_needs_update; | 51 bool layer_needs_update; |
56 base::scoped_nsobject<CALayer> ca_layer; | 52 base::scoped_nsobject<CALayer> ca_layer; |
57 | 53 |
58 void TakeCALayerFrom(OverlayPlane* other_plane) { | 54 void TakeCALayerFrom(OverlayPlane* other_plane) { |
59 ca_layer.swap(other_plane->ca_layer); | 55 ca_layer.swap(other_plane->ca_layer); |
60 } | 56 } |
61 | 57 |
62 void UpdateProperties(float scale_factor) { | 58 void UpdateProperties(float scale_factor) { |
63 if (layer_needs_update) { | 59 if (layer_needs_update) { |
64 [ca_layer setOpaque:YES]; | 60 [ca_layer setOpaque:YES]; |
65 | 61 |
66 id new_contents = static_cast<id>(io_surface.get()); | 62 id new_contents = static_cast<id>(io_surface.get()); |
67 if ([ca_layer contents] == new_contents && z_order == 0) | 63 if ([ca_layer contents] == new_contents) |
68 [ca_layer setContentsChanged]; | 64 [ca_layer setContentsChanged]; |
69 else | 65 else |
70 [ca_layer setContents:new_contents]; | 66 [ca_layer setContents:new_contents]; |
71 [ca_layer setContentsRect:contents_rect.ToCGRect()]; | 67 [ca_layer setContentsRect:contents_rect.ToCGRect()]; |
72 | 68 |
73 [ca_layer setAnchorPoint:CGPointZero]; | 69 [ca_layer setAnchorPoint:CGPointZero]; |
74 | 70 |
75 if ([ca_layer respondsToSelector:(@selector(setContentsScale:))]) | 71 if ([ca_layer respondsToSelector:(@selector(setContentsScale:))]) |
76 [ca_layer setContentsScale:scale_factor]; | 72 [ca_layer setContentsScale:scale_factor]; |
77 gfx::RectF dip_frame_rect = gfx::RectF(pixel_frame_rect); | 73 gfx::RectF dip_frame_rect = gfx::RectF(pixel_frame_rect); |
(...skipping 15 matching lines...) Expand all Loading... |
93 // Red represents damaged contents. | 89 // Red represents damaged contents. |
94 color.reset(CGColorCreateGenericRGB(1, 0, 0, 1)); | 90 color.reset(CGColorCreateGenericRGB(1, 0, 0, 1)); |
95 } | 91 } |
96 [ca_layer setBorderWidth:1]; | 92 [ca_layer setBorderWidth:1]; |
97 [ca_layer setBorderColor:color]; | 93 [ca_layer setBorderColor:color]; |
98 } | 94 } |
99 layer_needs_update = false; | 95 layer_needs_update = false; |
100 } | 96 } |
101 | 97 |
102 private: | 98 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 }; | 99 }; |
113 | 100 |
114 void CALayerPartialDamageTree::UpdateRootAndPartialDamagePlanes( | 101 void CALayerPartialDamageTree::UpdatePartialDamagePlanes( |
115 CALayerPartialDamageTree* old_tree, | 102 CALayerPartialDamageTree* old_tree, |
116 const gfx::RectF& pixel_damage_rect) { | 103 const gfx::Rect& pixel_damage_rect) { |
117 // This is the plane that will be updated this frame. It may be the root plane | 104 // Don't create partial damage layers if partial swap is disabled. |
118 // or a child plane. | 105 if (!allow_partial_swap_) |
119 linked_ptr<OverlayPlane> plane_for_swap; | 106 return; |
| 107 // Only create partial damage layers when building on top of an existing tree. |
| 108 if (!old_tree) |
| 109 return; |
| 110 // If the frame size has changed, discard all of the old partial damage |
| 111 // layers. |
| 112 if (old_tree->root_plane_->pixel_frame_rect != root_plane_->pixel_frame_rect) |
| 113 return; |
| 114 // If there is full damage, discard all of the old partial damage layers. |
| 115 if (pixel_damage_rect == root_plane_->pixel_frame_rect) |
| 116 return; |
120 | 117 |
121 // If the frame's size changed, if we haven't updated the root layer, if | 118 // If there is no damage, don't change anything. |
122 // we have full damage, or if we don't support remote layers, then use the | 119 if (pixel_damage_rect.IsEmpty()) { |
123 // root layer directly. | 120 std::swap(partial_damage_planes_, old_tree->partial_damage_planes_); |
124 if (!allow_partial_swap_ || !old_tree || | 121 return; |
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 } | 122 } |
130 | 123 |
131 // Walk though the old tree's partial damage layers and see if there is one | 124 // Find the last partial damage plane to re-use the CALayer from. Grow the |
132 // that is appropriate to re-use. | 125 // new rect for this layer to include this damage, and all nearby partial |
133 if (!plane_for_swap.get() && !pixel_damage_rect.IsEmpty()) { | 126 // damage layers. |
134 gfx::RectF plane_to_reuse_dip_enlarged_rect; | 127 scoped_ptr<OverlayPlane> plane_for_swap; |
| 128 { |
| 129 auto plane_to_reuse_iter = old_tree->partial_damage_planes_.end(); |
| 130 gfx::Rect plane_to_reuse_enlarged_pixel_damage_rect; |
135 | 131 |
136 // Find the last partial damage plane to re-use the CALayer from. Grow the | 132 for (auto old_plane_iter = old_tree->partial_damage_planes_.begin(); |
137 // new rect for this layer to include this damage, and all nearby partial | 133 old_plane_iter != old_tree->partial_damage_planes_.end(); |
138 // damage layers. | 134 ++old_plane_iter) { |
139 linked_ptr<OverlayPlane> plane_to_reuse; | 135 gfx::Rect enlarged_pixel_damage_rect = |
140 for (auto& old_plane : old_tree->partial_damage_planes_) { | 136 (*old_plane_iter)->pixel_frame_rect; |
141 gfx::RectF dip_enlarged_rect = old_plane->pixel_frame_rect; | 137 enlarged_pixel_damage_rect.Union(pixel_damage_rect); |
142 dip_enlarged_rect.Union(pixel_damage_rect); | |
143 | 138 |
144 // Compute the fraction of the pixels that would not be updated by this | 139 // Compute the fraction of the pixels that would not be updated by this |
145 // swap. If it is too big, try another layer. | 140 // swap. If it is too big, try another layer. |
146 float waste_fraction = dip_enlarged_rect.size().GetArea() * 1.f / | 141 float waste_fraction = enlarged_pixel_damage_rect.size().GetArea() * 1.f / |
147 pixel_damage_rect.size().GetArea(); | 142 pixel_damage_rect.size().GetArea(); |
148 if (waste_fraction > kMaximumPartialDamageWasteFraction) | 143 if (waste_fraction > kMaximumPartialDamageWasteFraction) |
149 continue; | 144 continue; |
150 | 145 |
151 plane_to_reuse = old_plane; | 146 plane_to_reuse_iter = old_plane_iter; |
152 plane_to_reuse_dip_enlarged_rect.Union(dip_enlarged_rect); | 147 plane_to_reuse_enlarged_pixel_damage_rect.Union( |
| 148 enlarged_pixel_damage_rect); |
153 } | 149 } |
154 | 150 if (plane_to_reuse_iter != old_tree->partial_damage_planes_.end()) { |
155 if (plane_to_reuse.get()) { | 151 gfx::RectF enlarged_contents_rect = |
156 gfx::RectF enlarged_contents_rect = plane_to_reuse_dip_enlarged_rect; | 152 gfx::RectF(plane_to_reuse_enlarged_pixel_damage_rect); |
157 enlarged_contents_rect.Scale(1. / root_plane_->pixel_frame_rect.width(), | 153 enlarged_contents_rect.Scale(1. / root_plane_->pixel_frame_rect.width(), |
158 1. / root_plane_->pixel_frame_rect.height()); | 154 1. / root_plane_->pixel_frame_rect.height()); |
159 | 155 |
160 plane_for_swap = OverlayPlane::CreateWithFrameRect( | 156 plane_for_swap.reset(new OverlayPlane( |
161 0, root_plane_->io_surface, plane_to_reuse_dip_enlarged_rect, | 157 root_plane_->io_surface, plane_to_reuse_enlarged_pixel_damage_rect, |
162 enlarged_contents_rect); | 158 enlarged_contents_rect)); |
163 | 159 |
164 plane_for_swap->TakeCALayerFrom(plane_to_reuse.get()); | 160 plane_for_swap->TakeCALayerFrom((*plane_to_reuse_iter).get()); |
165 if (plane_to_reuse != old_tree->partial_damage_planes_.back()) | 161 if (*plane_to_reuse_iter != old_tree->partial_damage_planes_.back()) { |
| 162 CALayer* superlayer = [plane_for_swap->ca_layer superlayer]; |
166 [plane_for_swap->ca_layer removeFromSuperlayer]; | 163 [plane_for_swap->ca_layer removeFromSuperlayer]; |
| 164 [superlayer addSublayer:plane_for_swap->ca_layer]; |
| 165 } |
167 } | 166 } |
168 } | 167 } |
169 | 168 |
170 // If we haven't found an appropriate layer to re-use, create a new one, if | 169 // If we haven't found an appropriate layer to re-use, create a new one, if |
171 // we haven't already created too many. | 170 // we haven't already created too many. |
172 if (!plane_for_swap.get() && !pixel_damage_rect.IsEmpty() && | 171 if (!plane_for_swap.get() && |
173 old_tree->partial_damage_planes_.size() < kMaximumPartialDamageLayers) { | 172 old_tree->partial_damage_planes_.size() < kMaximumPartialDamageLayers) { |
174 gfx::RectF contents_rect = gfx::RectF(pixel_damage_rect); | 173 gfx::RectF contents_rect = gfx::RectF(pixel_damage_rect); |
175 contents_rect.Scale(1. / root_plane_->pixel_frame_rect.width(), | 174 contents_rect.Scale(1. / root_plane_->pixel_frame_rect.width(), |
176 1. / root_plane_->pixel_frame_rect.height()); | 175 1. / root_plane_->pixel_frame_rect.height()); |
177 plane_for_swap = OverlayPlane::CreateWithFrameRect( | 176 plane_for_swap.reset(new OverlayPlane(root_plane_->io_surface, |
178 0, root_plane_->io_surface, pixel_damage_rect, contents_rect); | 177 pixel_damage_rect, contents_rect)); |
179 } | 178 } |
180 | 179 |
181 // And if we still don't have a layer, use the root layer. | 180 // And if we still don't have a layer, do full damage. |
182 if (!plane_for_swap.get() && !pixel_damage_rect.IsEmpty()) | 181 if (!plane_for_swap.get()) |
183 plane_for_swap = root_plane_; | 182 return; |
184 | 183 |
185 // Walk all old partial damage planes. Remove anything that is now completely | 184 // Walk all old partial damage planes. Remove anything that is now completely |
186 // covered, and move everything else into the new |partial_damage_planes_|. | 185 // covered, and move everything else into the new |partial_damage_planes_|. |
187 if (old_tree) { | 186 for (auto& old_plane : old_tree->partial_damage_planes_) { |
188 for (auto& old_plane : old_tree->partial_damage_planes_) { | 187 if (!old_plane.get()) |
189 // Intersect the planes' frames with the new root plane to ensure that | 188 continue; |
190 // they don't get kept alive inappropriately. | 189 // Intersect the planes' frames with the new root plane to ensure that |
191 gfx::RectF old_plane_frame_rect = old_plane->pixel_frame_rect; | 190 // they don't get kept alive inappropriately. |
192 old_plane_frame_rect.Intersect(root_plane_->pixel_frame_rect); | 191 gfx::Rect old_plane_frame_rect = old_plane->pixel_frame_rect; |
| 192 old_plane_frame_rect.Intersect(root_plane_->pixel_frame_rect); |
193 | 193 |
194 bool old_plane_covered_by_swap = false; | 194 bool old_plane_covered_by_swap = false; |
195 if (plane_for_swap.get() && | 195 if (plane_for_swap.get() && |
196 plane_for_swap->pixel_frame_rect.Contains(old_plane_frame_rect)) { | 196 plane_for_swap->pixel_frame_rect.Contains(old_plane_frame_rect)) { |
197 old_plane_covered_by_swap = true; | 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 } | 198 } |
204 if (plane_for_swap != root_plane_) | 199 if (!old_plane_covered_by_swap) { |
205 root_plane_ = old_tree->root_plane_; | 200 DCHECK(old_plane->ca_layer); |
| 201 partial_damage_planes_.push_back(std::move(old_plane)); |
| 202 } |
206 } | 203 } |
207 | 204 |
208 // Finally, add the new swap's plane at the back of the list, if it exists. | 205 partial_damage_planes_.push_back(std::move(plane_for_swap)); |
209 if (plane_for_swap.get() && plane_for_swap != root_plane_) { | 206 } |
210 partial_damage_planes_.push_back(plane_for_swap); | 207 |
| 208 void CALayerPartialDamageTree::UpdateRootAndPartialDamagePlanes( |
| 209 scoped_ptr<CALayerPartialDamageTree> old_tree, |
| 210 const gfx::Rect& pixel_damage_rect) { |
| 211 // First update the partial damage tree. |
| 212 UpdatePartialDamagePlanes(old_tree.get(), pixel_damage_rect); |
| 213 if (old_tree) { |
| 214 if (partial_damage_planes_.empty()) { |
| 215 // If there are no partial damage planes, then we will be updating the |
| 216 // root layer. Take the CALayer from the old tree. |
| 217 root_plane_->TakeCALayerFrom(old_tree->root_plane_.get()); |
| 218 } else { |
| 219 // If there is a partial damage tree, then just take the old plane |
| 220 // from the previous frame, so that there is no update to it. |
| 221 root_plane_.swap(old_tree->root_plane_); |
| 222 } |
211 } | 223 } |
212 } | 224 } |
213 | 225 |
214 void CALayerPartialDamageTree::UpdateRootAndPartialDamageCALayers( | 226 void CALayerPartialDamageTree::UpdateCALayers(CALayer* superlayer, |
215 CALayer* superlayer, | 227 float scale_factor) { |
216 float scale_factor) { | |
217 if (!allow_partial_swap_) { | 228 if (!allow_partial_swap_) { |
218 DCHECK(partial_damage_planes_.empty()); | 229 DCHECK(partial_damage_planes_.empty()); |
219 return; | 230 return; |
220 } | 231 } |
221 | 232 |
222 // Allocate and update CALayers for the backbuffer and partial damage layers. | 233 // Allocate and update CALayers for the backbuffer and partial damage layers. |
223 if (!root_plane_->ca_layer) { | 234 if (!root_plane_->ca_layer) { |
| 235 DCHECK(partial_damage_planes_.empty()); |
224 root_plane_->ca_layer.reset([[CALayer alloc] init]); | 236 root_plane_->ca_layer.reset([[CALayer alloc] init]); |
225 [superlayer setSublayers:nil]; | 237 [superlayer setSublayers:nil]; |
226 [superlayer addSublayer:root_plane_->ca_layer]; | 238 [superlayer addSublayer:root_plane_->ca_layer]; |
227 } | 239 } |
228 for (auto& plane : partial_damage_planes_) { | 240 for (auto& plane : partial_damage_planes_) { |
229 if (!plane->ca_layer) { | 241 if (!plane->ca_layer) { |
230 DCHECK(plane == partial_damage_planes_.back()); | 242 DCHECK(plane == partial_damage_planes_.back()); |
231 plane->ca_layer.reset([[CALayer alloc] init]); | 243 plane->ca_layer.reset([[CALayer alloc] init]); |
232 } | 244 } |
233 if (![plane->ca_layer superlayer]) { | 245 if (![plane->ca_layer superlayer]) { |
234 DCHECK(plane == partial_damage_planes_.back()); | 246 DCHECK(plane == partial_damage_planes_.back()); |
235 [superlayer addSublayer:plane->ca_layer]; | 247 [superlayer addSublayer:plane->ca_layer]; |
236 } | 248 } |
237 } | 249 } |
238 root_plane_->UpdateProperties(scale_factor); | 250 root_plane_->UpdateProperties(scale_factor); |
239 for (auto& plane : partial_damage_planes_) | 251 for (auto& plane : partial_damage_planes_) |
240 plane->UpdateProperties(scale_factor); | 252 plane->UpdateProperties(scale_factor); |
241 } | 253 } |
242 | 254 |
243 CALayerPartialDamageTree::CALayerPartialDamageTree( | 255 CALayerPartialDamageTree::CALayerPartialDamageTree( |
244 bool allow_partial_swap, | 256 bool allow_partial_swap, |
245 base::ScopedCFTypeRef<IOSurfaceRef> io_surface, | 257 base::ScopedCFTypeRef<IOSurfaceRef> io_surface, |
246 const gfx::Rect& pixel_frame_rect) | 258 const gfx::Rect& pixel_frame_rect) |
247 : allow_partial_swap_(allow_partial_swap) { | 259 : allow_partial_swap_(allow_partial_swap) { |
248 root_plane_ = OverlayPlane::CreateWithFrameRect( | 260 root_plane_.reset( |
249 0, io_surface, gfx::RectF(pixel_frame_rect), gfx::RectF(0, 0, 1, 1)); | 261 new OverlayPlane(io_surface, pixel_frame_rect, gfx::RectF(0, 0, 1, 1))); |
250 } | 262 } |
251 | 263 |
252 CALayerPartialDamageTree::~CALayerPartialDamageTree() {} | 264 CALayerPartialDamageTree::~CALayerPartialDamageTree() {} |
253 | 265 |
254 base::ScopedCFTypeRef<IOSurfaceRef> | 266 base::ScopedCFTypeRef<IOSurfaceRef> |
255 CALayerPartialDamageTree::RootLayerIOSurface() { | 267 CALayerPartialDamageTree::RootLayerIOSurface() { |
256 return root_plane_->io_surface; | 268 return root_plane_->io_surface; |
257 } | 269 } |
258 | 270 |
259 void CALayerPartialDamageTree::CommitCALayers( | 271 void CALayerPartialDamageTree::CommitCALayers( |
260 CALayer* superlayer, | 272 CALayer* superlayer, |
261 scoped_ptr<CALayerPartialDamageTree> old_tree, | 273 scoped_ptr<CALayerPartialDamageTree> old_tree, |
262 float scale_factor, | 274 float scale_factor, |
263 const gfx::Rect& pixel_damage_rect) { | 275 const gfx::Rect& pixel_damage_rect) { |
264 UpdateRootAndPartialDamagePlanes(old_tree.get(), | 276 UpdateRootAndPartialDamagePlanes(std::move(old_tree), pixel_damage_rect); |
265 gfx::RectF(pixel_damage_rect)); | 277 UpdateCALayers(superlayer, scale_factor); |
266 UpdateRootAndPartialDamageCALayers(superlayer, scale_factor); | |
267 } | 278 } |
268 | 279 |
269 } // namespace content | 280 } // namespace content |
OLD | NEW |