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