OLD | NEW |
(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_tree_mac.h" |
| 6 |
| 7 #include <AVFoundation/AVFoundation.h> |
| 8 #include <CoreMedia/CoreMedia.h> |
| 9 #include <CoreVideo/CoreVideo.h> |
| 10 |
| 11 #include "base/command_line.h" |
| 12 #include "base/mac/sdk_forward_declarations.h" |
| 13 #include "base/trace_event/trace_event.h" |
| 14 #include "gpu/GLES2/gl2extchromium.h" |
| 15 #include "third_party/skia/include/core/SkColor.h" |
| 16 #include "ui/base/cocoa/animation_utils.h" |
| 17 #include "ui/base/ui_base_switches.h" |
| 18 #include "ui/gfx/geometry/dip_util.h" |
| 19 |
| 20 #if !defined(MAC_OS_X_VERSION_10_8) || \ |
| 21 MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_8 |
| 22 extern NSString* const AVLayerVideoGravityResize; |
| 23 extern "C" void NSAccessibilityPostNotificationWithUserInfo( |
| 24 id object, |
| 25 NSString* notification, |
| 26 NSDictionary* user_info); |
| 27 extern "C" OSStatus CMSampleBufferCreateForImageBuffer( |
| 28 CFAllocatorRef, |
| 29 CVImageBufferRef, |
| 30 Boolean dataReady, |
| 31 CMSampleBufferMakeDataReadyCallback, |
| 32 void*, |
| 33 CMVideoFormatDescriptionRef, |
| 34 const CMSampleTimingInfo*, |
| 35 CMSampleBufferRef*); |
| 36 extern "C" CFArrayRef CMSampleBufferGetSampleAttachmentsArray(CMSampleBufferRef, |
| 37 Boolean); |
| 38 extern "C" OSStatus CMVideoFormatDescriptionCreateForImageBuffer( |
| 39 CFAllocatorRef, |
| 40 CVImageBufferRef, |
| 41 CMVideoFormatDescriptionRef*); |
| 42 extern "C" CMTime CMTimeMake(int64_t, int32_t); |
| 43 extern CFStringRef const kCMSampleAttachmentKey_DisplayImmediately; |
| 44 extern const CMTime kCMTimeInvalid; |
| 45 #endif // MAC_OS_X_VERSION_10_8 |
| 46 |
| 47 namespace content { |
| 48 |
| 49 namespace { |
| 50 |
| 51 // This will enqueue |io_surface| to be drawn by |av_layer| by wrapping |
| 52 // |io_surface| in a CVPixelBuffer. This will increase the in-use count |
| 53 // of and retain |io_surface| until it is no longer being displayed. |
| 54 bool AVSampleBufferDisplayLayerEnqueueIOSurface( |
| 55 AVSampleBufferDisplayLayer* av_layer, |
| 56 IOSurfaceRef io_surface) { |
| 57 OSStatus os_status = noErr; |
| 58 CVReturn cv_return = kCVReturnSuccess; |
| 59 |
| 60 base::ScopedCFTypeRef<CVPixelBufferRef> cv_pixel_buffer; |
| 61 cv_return = CVPixelBufferCreateWithIOSurface( |
| 62 nullptr, io_surface, nullptr, cv_pixel_buffer.InitializeInto()); |
| 63 if (cv_return != kCVReturnSuccess) { |
| 64 LOG(ERROR) << "CVPixelBufferCreateWithIOSurface failed with " << cv_return; |
| 65 return false; |
| 66 } |
| 67 |
| 68 base::ScopedCFTypeRef<CMVideoFormatDescriptionRef> video_info; |
| 69 os_status = CMVideoFormatDescriptionCreateForImageBuffer( |
| 70 nullptr, cv_pixel_buffer, video_info.InitializeInto()); |
| 71 if (os_status != noErr) { |
| 72 LOG(ERROR) << "CMVideoFormatDescriptionCreateForImageBuffer failed with " |
| 73 << os_status; |
| 74 return false; |
| 75 } |
| 76 |
| 77 // The frame time doesn't matter because we will specify to display |
| 78 // immediately. |
| 79 CMTime frame_time = CMTimeMake(0, 1); |
| 80 CMSampleTimingInfo timing_info = {frame_time, frame_time, kCMTimeInvalid}; |
| 81 |
| 82 base::ScopedCFTypeRef<CMSampleBufferRef> sample_buffer; |
| 83 os_status = CMSampleBufferCreateForImageBuffer( |
| 84 nullptr, cv_pixel_buffer, YES, nullptr, nullptr, video_info, &timing_info, |
| 85 sample_buffer.InitializeInto()); |
| 86 if (os_status != noErr) { |
| 87 LOG(ERROR) << "CMSampleBufferCreateForImageBuffer failed with " |
| 88 << os_status; |
| 89 return false; |
| 90 } |
| 91 |
| 92 // Specify to display immediately via the sample buffer attachments. |
| 93 CFArrayRef attachments = |
| 94 CMSampleBufferGetSampleAttachmentsArray(sample_buffer, YES); |
| 95 if (!attachments) { |
| 96 LOG(ERROR) << "CMSampleBufferGetSampleAttachmentsArray failed"; |
| 97 return false; |
| 98 } |
| 99 if (CFArrayGetCount(attachments) < 1) { |
| 100 LOG(ERROR) << "CMSampleBufferGetSampleAttachmentsArray result was empty"; |
| 101 return false; |
| 102 } |
| 103 CFMutableDictionaryRef attachments_dictionary = |
| 104 reinterpret_cast<CFMutableDictionaryRef>( |
| 105 const_cast<void*>(CFArrayGetValueAtIndex(attachments, 0))); |
| 106 if (!attachments_dictionary) { |
| 107 LOG(ERROR) << "Failed to get attachments dictionary"; |
| 108 return false; |
| 109 } |
| 110 CFDictionarySetValue(attachments_dictionary, |
| 111 kCMSampleAttachmentKey_DisplayImmediately, |
| 112 kCFBooleanTrue); |
| 113 |
| 114 [av_layer enqueueSampleBuffer:sample_buffer]; |
| 115 return true; |
| 116 } |
| 117 |
| 118 } // namespace |
| 119 |
| 120 CALayerTree::CALayerTree() {} |
| 121 CALayerTree::~CALayerTree() {} |
| 122 |
| 123 bool CALayerTree::ScheduleCALayer( |
| 124 bool is_clipped, |
| 125 const gfx::Rect& clip_rect, |
| 126 unsigned sorting_context_id, |
| 127 const gfx::Transform& transform, |
| 128 base::ScopedCFTypeRef<IOSurfaceRef> io_surface, |
| 129 const gfx::RectF& contents_rect, |
| 130 const gfx::Rect& rect, |
| 131 unsigned background_color, |
| 132 unsigned edge_aa_mask, |
| 133 float opacity) { |
| 134 // Excessive logging to debug white screens (crbug.com/583805). |
| 135 // TODO(ccameron): change this back to a DLOG. |
| 136 if (has_committed_) { |
| 137 LOG(ERROR) << "ScheduleCALayer called after CommitScheduledCALayers."; |
| 138 return false; |
| 139 } |
| 140 return root_layer_.AddContentLayer(is_clipped, clip_rect, sorting_context_id, |
| 141 transform, io_surface, contents_rect, rect, |
| 142 background_color, edge_aa_mask, opacity); |
| 143 } |
| 144 |
| 145 void CALayerTree::CommitScheduledCALayers(CALayer* superlayer, |
| 146 scoped_ptr<CALayerTree> old_tree, |
| 147 float scale_factor) { |
| 148 TRACE_EVENT0("gpu", "CALayerTree::CommitScheduledCALayers"); |
| 149 RootLayer* old_root_layer = nullptr; |
| 150 if (old_tree) { |
| 151 DCHECK(old_tree->has_committed_); |
| 152 if (old_tree->scale_factor_ == scale_factor) |
| 153 old_root_layer = &old_tree->root_layer_; |
| 154 } |
| 155 |
| 156 root_layer_.CommitToCA(superlayer, old_root_layer, scale_factor); |
| 157 // If there are any extra CALayers in |old_tree| that were not stolen by this |
| 158 // tree, they will be removed from the CALayer tree in this deallocation. |
| 159 old_tree.reset(); |
| 160 has_committed_ = true; |
| 161 scale_factor_ = scale_factor; |
| 162 } |
| 163 |
| 164 CALayerTree::RootLayer::RootLayer() {} |
| 165 |
| 166 // Note that for all destructors, the the CALayer will have been reset to nil if |
| 167 // another layer has taken it. |
| 168 CALayerTree::RootLayer::~RootLayer() { |
| 169 [ca_layer removeFromSuperlayer]; |
| 170 } |
| 171 |
| 172 CALayerTree::ClipAndSortingLayer::ClipAndSortingLayer( |
| 173 bool is_clipped, |
| 174 gfx::Rect clip_rect, |
| 175 unsigned sorting_context_id, |
| 176 bool is_singleton_sorting_context) |
| 177 : is_clipped(is_clipped), |
| 178 clip_rect(clip_rect), |
| 179 sorting_context_id(sorting_context_id), |
| 180 is_singleton_sorting_context(is_singleton_sorting_context) {} |
| 181 |
| 182 CALayerTree::ClipAndSortingLayer::ClipAndSortingLayer( |
| 183 ClipAndSortingLayer&& layer) |
| 184 : transform_layers(std::move(layer.transform_layers)), |
| 185 is_clipped(layer.is_clipped), |
| 186 clip_rect(layer.clip_rect), |
| 187 sorting_context_id(layer.sorting_context_id), |
| 188 is_singleton_sorting_context( |
| 189 layer.is_singleton_sorting_context), |
| 190 ca_layer(layer.ca_layer) { |
| 191 // Ensure that the ca_layer be reset, so that when the destructor is called, |
| 192 // the layer hierarchy is unaffected. |
| 193 // TODO(ccameron): Add a move constructor for scoped_nsobject to do this |
| 194 // automatically. |
| 195 layer.ca_layer.reset(); |
| 196 } |
| 197 |
| 198 CALayerTree::ClipAndSortingLayer::~ClipAndSortingLayer() { |
| 199 [ca_layer removeFromSuperlayer]; |
| 200 } |
| 201 |
| 202 CALayerTree::TransformLayer::TransformLayer(const gfx::Transform& transform) |
| 203 : transform(transform) {} |
| 204 |
| 205 CALayerTree::TransformLayer::TransformLayer(TransformLayer&& layer) |
| 206 : transform(layer.transform), |
| 207 content_layers(std::move(layer.content_layers)), |
| 208 ca_layer(layer.ca_layer) { |
| 209 layer.ca_layer.reset(); |
| 210 } |
| 211 |
| 212 CALayerTree::TransformLayer::~TransformLayer() { |
| 213 [ca_layer removeFromSuperlayer]; |
| 214 } |
| 215 |
| 216 CALayerTree::ContentLayer::ContentLayer( |
| 217 base::ScopedCFTypeRef<IOSurfaceRef> io_surface, |
| 218 const gfx::RectF& contents_rect, |
| 219 const gfx::Rect& rect, |
| 220 unsigned background_color, |
| 221 unsigned edge_aa_mask, |
| 222 float opacity) |
| 223 : io_surface(io_surface), |
| 224 contents_rect(contents_rect), |
| 225 rect(rect), |
| 226 background_color(background_color), |
| 227 ca_edge_aa_mask(0), |
| 228 opacity(opacity) { |
| 229 // Because the root layer has setGeometryFlipped:YES, there is some ambiguity |
| 230 // about what exactly top and bottom mean. This ambiguity is resolved in |
| 231 // different ways for solid color CALayers and for CALayers that have content |
| 232 // (surprise!). For CALayers with IOSurface content, the top edge in the AA |
| 233 // mask refers to what appears as the bottom edge on-screen. For CALayers |
| 234 // without content (solid color layers), the top edge in the AA mask is the |
| 235 // top edge on-screen. |
| 236 // http://crbug.com/567946 |
| 237 if (edge_aa_mask & GL_CA_LAYER_EDGE_LEFT_CHROMIUM) |
| 238 ca_edge_aa_mask |= kCALayerLeftEdge; |
| 239 if (edge_aa_mask & GL_CA_LAYER_EDGE_RIGHT_CHROMIUM) |
| 240 ca_edge_aa_mask |= kCALayerRightEdge; |
| 241 if (io_surface) { |
| 242 if (edge_aa_mask & GL_CA_LAYER_EDGE_TOP_CHROMIUM) |
| 243 ca_edge_aa_mask |= kCALayerBottomEdge; |
| 244 if (edge_aa_mask & GL_CA_LAYER_EDGE_BOTTOM_CHROMIUM) |
| 245 ca_edge_aa_mask |= kCALayerTopEdge; |
| 246 } else { |
| 247 if (edge_aa_mask & GL_CA_LAYER_EDGE_TOP_CHROMIUM) |
| 248 ca_edge_aa_mask |= kCALayerTopEdge; |
| 249 if (edge_aa_mask & GL_CA_LAYER_EDGE_BOTTOM_CHROMIUM) |
| 250 ca_edge_aa_mask |= kCALayerBottomEdge; |
| 251 } |
| 252 |
| 253 // Only allow 4:2:0 frames which fill the layer's contents to be promoted to |
| 254 // AV layers. |
| 255 if (IOSurfaceGetPixelFormat(io_surface) == |
| 256 kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange && |
| 257 contents_rect == gfx::RectF(0, 0, 1, 1)) { |
| 258 use_av_layer = true; |
| 259 } |
| 260 } |
| 261 |
| 262 CALayerTree::ContentLayer::ContentLayer(ContentLayer&& layer) |
| 263 : io_surface(layer.io_surface), |
| 264 contents_rect(layer.contents_rect), |
| 265 rect(layer.rect), |
| 266 background_color(layer.background_color), |
| 267 ca_edge_aa_mask(layer.ca_edge_aa_mask), |
| 268 opacity(layer.opacity), |
| 269 ca_layer(std::move(layer.ca_layer)), |
| 270 av_layer(std::move(layer.av_layer)), |
| 271 use_av_layer(layer.use_av_layer) { |
| 272 DCHECK(!layer.ca_layer); |
| 273 DCHECK(!layer.av_layer); |
| 274 } |
| 275 |
| 276 CALayerTree::ContentLayer::~ContentLayer() { |
| 277 [ca_layer removeFromSuperlayer]; |
| 278 } |
| 279 |
| 280 bool CALayerTree::RootLayer::AddContentLayer( |
| 281 bool is_clipped, |
| 282 const gfx::Rect& clip_rect, |
| 283 unsigned sorting_context_id, |
| 284 const gfx::Transform& transform, |
| 285 base::ScopedCFTypeRef<IOSurfaceRef> io_surface, |
| 286 const gfx::RectF& contents_rect, |
| 287 const gfx::Rect& rect, |
| 288 unsigned background_color, |
| 289 unsigned edge_aa_mask, |
| 290 float opacity) { |
| 291 bool needs_new_clip_and_sorting_layer = true; |
| 292 |
| 293 // In sorting_context_id 0, all quads are listed in back-to-front order. |
| 294 // This is accomplished by having the CALayers be siblings of each other. |
| 295 // If a quad has a 3D transform, it is necessary to put it in its own sorting |
| 296 // context, so that it will not intersect with quads before and after it. |
| 297 bool is_singleton_sorting_context = |
| 298 !sorting_context_id && !transform.IsFlat(); |
| 299 |
| 300 if (!clip_and_sorting_layers.empty()) { |
| 301 ClipAndSortingLayer& current_layer = clip_and_sorting_layers.back(); |
| 302 // It is in error to change the clipping settings within a non-zero sorting |
| 303 // context. The result will be incorrect layering and intersection. |
| 304 if (sorting_context_id && |
| 305 current_layer.sorting_context_id == sorting_context_id && |
| 306 (current_layer.is_clipped != is_clipped || |
| 307 current_layer.clip_rect != clip_rect)) { |
| 308 // Excessive logging to debug white screens (crbug.com/583805). |
| 309 // TODO(ccameron): change this back to a DLOG. |
| 310 LOG(ERROR) << "CALayer changed clip inside non-zero sorting context."; |
| 311 return false; |
| 312 } |
| 313 if (!is_singleton_sorting_context && |
| 314 !current_layer.is_singleton_sorting_context && |
| 315 current_layer.is_clipped == is_clipped && |
| 316 current_layer.clip_rect == clip_rect && |
| 317 current_layer.sorting_context_id == sorting_context_id) { |
| 318 needs_new_clip_and_sorting_layer = false; |
| 319 } |
| 320 } |
| 321 if (needs_new_clip_and_sorting_layer) { |
| 322 clip_and_sorting_layers.push_back( |
| 323 ClipAndSortingLayer(is_clipped, clip_rect, sorting_context_id, |
| 324 is_singleton_sorting_context)); |
| 325 } |
| 326 clip_and_sorting_layers.back().AddContentLayer( |
| 327 transform, io_surface, contents_rect, rect, background_color, |
| 328 edge_aa_mask, opacity); |
| 329 return true; |
| 330 } |
| 331 |
| 332 void CALayerTree::ClipAndSortingLayer::AddContentLayer( |
| 333 const gfx::Transform& transform, |
| 334 base::ScopedCFTypeRef<IOSurfaceRef> io_surface, |
| 335 const gfx::RectF& contents_rect, |
| 336 const gfx::Rect& rect, |
| 337 unsigned background_color, |
| 338 unsigned edge_aa_mask, |
| 339 float opacity) { |
| 340 bool needs_new_transform_layer = true; |
| 341 if (!transform_layers.empty()) { |
| 342 const TransformLayer& current_layer = transform_layers.back(); |
| 343 if (current_layer.transform == transform) |
| 344 needs_new_transform_layer = false; |
| 345 } |
| 346 if (needs_new_transform_layer) |
| 347 transform_layers.push_back(TransformLayer(transform)); |
| 348 transform_layers.back().AddContentLayer( |
| 349 io_surface, contents_rect, rect, background_color, edge_aa_mask, opacity); |
| 350 } |
| 351 |
| 352 void CALayerTree::TransformLayer::AddContentLayer( |
| 353 base::ScopedCFTypeRef<IOSurfaceRef> io_surface, |
| 354 const gfx::RectF& contents_rect, |
| 355 const gfx::Rect& rect, |
| 356 unsigned background_color, |
| 357 unsigned edge_aa_mask, |
| 358 float opacity) { |
| 359 content_layers.push_back(ContentLayer(io_surface, contents_rect, rect, |
| 360 background_color, edge_aa_mask, |
| 361 opacity)); |
| 362 } |
| 363 |
| 364 void CALayerTree::RootLayer::CommitToCA(CALayer* superlayer, |
| 365 RootLayer* old_layer, |
| 366 float scale_factor) { |
| 367 if (old_layer) { |
| 368 DCHECK(old_layer->ca_layer); |
| 369 std::swap(ca_layer, old_layer->ca_layer); |
| 370 } else { |
| 371 ca_layer.reset([[CALayer alloc] init]); |
| 372 [ca_layer setAnchorPoint:CGPointZero]; |
| 373 [superlayer setSublayers:nil]; |
| 374 [superlayer addSublayer:ca_layer]; |
| 375 [superlayer setBorderWidth:0]; |
| 376 } |
| 377 // Excessive logging to debug white screens (crbug.com/583805). |
| 378 // TODO(ccameron): change this back to a DCHECK. |
| 379 if ([ca_layer superlayer] != superlayer) { |
| 380 LOG(ERROR) << "CALayerTree root layer not attached to tree."; |
| 381 } |
| 382 |
| 383 for (size_t i = 0; i < clip_and_sorting_layers.size(); ++i) { |
| 384 ClipAndSortingLayer* old_clip_and_sorting_layer = nullptr; |
| 385 if (old_layer && i < old_layer->clip_and_sorting_layers.size()) { |
| 386 old_clip_and_sorting_layer = &old_layer->clip_and_sorting_layers[i]; |
| 387 } |
| 388 clip_and_sorting_layers[i].CommitToCA( |
| 389 ca_layer.get(), old_clip_and_sorting_layer, scale_factor); |
| 390 } |
| 391 } |
| 392 |
| 393 void CALayerTree::ClipAndSortingLayer::CommitToCA( |
| 394 CALayer* superlayer, |
| 395 ClipAndSortingLayer* old_layer, |
| 396 float scale_factor) { |
| 397 bool update_is_clipped = true; |
| 398 bool update_clip_rect = true; |
| 399 if (old_layer) { |
| 400 DCHECK(old_layer->ca_layer); |
| 401 std::swap(ca_layer, old_layer->ca_layer); |
| 402 update_is_clipped = old_layer->is_clipped != is_clipped; |
| 403 update_clip_rect = update_is_clipped || old_layer->clip_rect != clip_rect; |
| 404 } else { |
| 405 ca_layer.reset([[CALayer alloc] init]); |
| 406 [ca_layer setAnchorPoint:CGPointZero]; |
| 407 [superlayer addSublayer:ca_layer]; |
| 408 } |
| 409 // Excessive logging to debug white screens (crbug.com/583805). |
| 410 // TODO(ccameron): change this back to a DCHECK. |
| 411 if ([ca_layer superlayer] != superlayer) { |
| 412 LOG(ERROR) << "CALayerTree root layer not attached to tree."; |
| 413 } |
| 414 |
| 415 if (update_is_clipped) |
| 416 [ca_layer setMasksToBounds:is_clipped]; |
| 417 |
| 418 if (update_clip_rect) { |
| 419 if (is_clipped) { |
| 420 gfx::RectF dip_clip_rect = gfx::RectF(clip_rect); |
| 421 dip_clip_rect.Scale(1 / scale_factor); |
| 422 [ca_layer setPosition:CGPointMake(dip_clip_rect.x(), dip_clip_rect.y())]; |
| 423 [ca_layer setBounds:CGRectMake(0, 0, dip_clip_rect.width(), |
| 424 dip_clip_rect.height())]; |
| 425 [ca_layer |
| 426 setSublayerTransform:CATransform3DMakeTranslation( |
| 427 -dip_clip_rect.x(), -dip_clip_rect.y(), 0)]; |
| 428 } else { |
| 429 [ca_layer setPosition:CGPointZero]; |
| 430 [ca_layer setBounds:CGRectZero]; |
| 431 [ca_layer setSublayerTransform:CATransform3DIdentity]; |
| 432 } |
| 433 } |
| 434 |
| 435 for (size_t i = 0; i < transform_layers.size(); ++i) { |
| 436 TransformLayer* old_transform_layer = nullptr; |
| 437 if (old_layer && i < old_layer->transform_layers.size()) |
| 438 old_transform_layer = &old_layer->transform_layers[i]; |
| 439 transform_layers[i].CommitToCA(ca_layer.get(), old_transform_layer, |
| 440 scale_factor); |
| 441 } |
| 442 } |
| 443 |
| 444 void CALayerTree::TransformLayer::CommitToCA(CALayer* superlayer, |
| 445 TransformLayer* old_layer, |
| 446 float scale_factor) { |
| 447 bool update_transform = true; |
| 448 if (old_layer) { |
| 449 DCHECK(old_layer->ca_layer); |
| 450 std::swap(ca_layer, old_layer->ca_layer); |
| 451 update_transform = old_layer->transform != transform; |
| 452 } else { |
| 453 ca_layer.reset([[CATransformLayer alloc] init]); |
| 454 [superlayer addSublayer:ca_layer]; |
| 455 } |
| 456 DCHECK_EQ([ca_layer superlayer], superlayer); |
| 457 |
| 458 if (update_transform) { |
| 459 gfx::Transform pre_scale; |
| 460 gfx::Transform post_scale; |
| 461 pre_scale.Scale(1 / scale_factor, 1 / scale_factor); |
| 462 post_scale.Scale(scale_factor, scale_factor); |
| 463 gfx::Transform conjugated_transform = pre_scale * transform * post_scale; |
| 464 |
| 465 CATransform3D ca_transform; |
| 466 conjugated_transform.matrix().asColMajord(&ca_transform.m11); |
| 467 [ca_layer setTransform:ca_transform]; |
| 468 } |
| 469 |
| 470 for (size_t i = 0; i < content_layers.size(); ++i) { |
| 471 ContentLayer* old_content_layer = nullptr; |
| 472 if (old_layer && i < old_layer->content_layers.size()) |
| 473 old_content_layer = &old_layer->content_layers[i]; |
| 474 content_layers[i].CommitToCA(ca_layer.get(), old_content_layer, |
| 475 scale_factor); |
| 476 } |
| 477 } |
| 478 |
| 479 void CALayerTree::ContentLayer::CommitToCA(CALayer* superlayer, |
| 480 ContentLayer* old_layer, |
| 481 float scale_factor) { |
| 482 bool update_contents = true; |
| 483 bool update_contents_rect = true; |
| 484 bool update_rect = true; |
| 485 bool update_background_color = true; |
| 486 bool update_ca_edge_aa_mask = true; |
| 487 bool update_opacity = true; |
| 488 if (old_layer && old_layer->use_av_layer == use_av_layer) { |
| 489 DCHECK(old_layer->ca_layer); |
| 490 std::swap(ca_layer, old_layer->ca_layer); |
| 491 std::swap(av_layer, old_layer->av_layer); |
| 492 update_contents = old_layer->io_surface != io_surface; |
| 493 update_contents_rect = old_layer->contents_rect != contents_rect; |
| 494 update_rect = old_layer->rect != rect; |
| 495 update_background_color = old_layer->background_color != background_color; |
| 496 update_ca_edge_aa_mask = old_layer->ca_edge_aa_mask != ca_edge_aa_mask; |
| 497 update_opacity = old_layer->opacity != opacity; |
| 498 } else { |
| 499 if (use_av_layer) { |
| 500 av_layer.reset([[AVSampleBufferDisplayLayer alloc] init]); |
| 501 ca_layer.reset([av_layer retain]); |
| 502 [av_layer setVideoGravity:AVLayerVideoGravityResize]; |
| 503 } else { |
| 504 ca_layer.reset([[CALayer alloc] init]); |
| 505 } |
| 506 [ca_layer setAnchorPoint:CGPointZero]; |
| 507 [superlayer addSublayer:ca_layer]; |
| 508 } |
| 509 DCHECK_EQ([ca_layer superlayer], superlayer); |
| 510 bool update_anything = update_contents || update_contents_rect || |
| 511 update_rect || update_background_color || |
| 512 update_ca_edge_aa_mask || update_opacity; |
| 513 if (use_av_layer) { |
| 514 if (update_contents) |
| 515 AVSampleBufferDisplayLayerEnqueueIOSurface(av_layer, io_surface); |
| 516 } else { |
| 517 if (update_contents) { |
| 518 [ca_layer setContents:static_cast<id>(io_surface.get())]; |
| 519 if ([ca_layer respondsToSelector:(@selector(setContentsScale:))]) |
| 520 [ca_layer setContentsScale:scale_factor]; |
| 521 } |
| 522 if (update_contents_rect) |
| 523 [ca_layer setContentsRect:contents_rect.ToCGRect()]; |
| 524 } |
| 525 if (update_rect) { |
| 526 gfx::RectF dip_rect = gfx::RectF(rect); |
| 527 dip_rect.Scale(1 / scale_factor); |
| 528 [ca_layer setPosition:CGPointMake(dip_rect.x(), dip_rect.y())]; |
| 529 [ca_layer setBounds:CGRectMake(0, 0, dip_rect.width(), dip_rect.height())]; |
| 530 } |
| 531 if (update_background_color) { |
| 532 CGFloat rgba_color_components[4] = { |
| 533 SkColorGetR(background_color) / 255., |
| 534 SkColorGetG(background_color) / 255., |
| 535 SkColorGetB(background_color) / 255., |
| 536 SkColorGetA(background_color) / 255., |
| 537 }; |
| 538 base::ScopedCFTypeRef<CGColorRef> srgb_background_color(CGColorCreate( |
| 539 CGColorSpaceCreateWithName(kCGColorSpaceSRGB), rgba_color_components)); |
| 540 [ca_layer setBackgroundColor:srgb_background_color]; |
| 541 } |
| 542 if (update_ca_edge_aa_mask) |
| 543 [ca_layer setEdgeAntialiasingMask:ca_edge_aa_mask]; |
| 544 if (update_opacity) |
| 545 [ca_layer setOpacity:opacity]; |
| 546 |
| 547 static bool show_borders = base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 548 switches::kShowMacOverlayBorders); |
| 549 if (show_borders) { |
| 550 base::ScopedCFTypeRef<CGColorRef> color; |
| 551 if (update_anything) { |
| 552 if (use_av_layer) { |
| 553 // Green represents an AV layer that changed this frame. |
| 554 color.reset(CGColorCreateGenericRGB(0, 1, 0, 1)); |
| 555 } else { |
| 556 // Pink represents a CALayer that changed this frame. |
| 557 color.reset(CGColorCreateGenericRGB(1, 0, 1, 1)); |
| 558 } |
| 559 } else { |
| 560 // Grey represents a CALayer that has not changed. |
| 561 color.reset(CGColorCreateGenericRGB(0, 0, 0, 0.1)); |
| 562 } |
| 563 [ca_layer setBorderWidth:1]; |
| 564 [ca_layer setBorderColor:color]; |
| 565 } |
| 566 } |
| 567 |
| 568 } // namespace content |
OLD | NEW |