cacheDisplayInRect 在内部是如何工作的?

How does cacheDisplayInRect work internally?

将位图数据写入 bitmapImageRep 时,[NSView cacheDisplayInRect:(NSRect)rect toBitmapImageRep:(NSBitmapImageRep *)bitmapImageRep] 内部是如何工作的?

例如它是遍历并访问所有子视图的 CALayer layer 并在调用 ...cacheDisplayInRect... 时组合它们,还是简单地编写它自己的层(然后它已经包含所有子视图的表示数据)。

我的问题的背景: 我使用了一个本地控件 (WKWebView),但它在屏幕截图 (example SO question) 时被破坏了,而不是重新发明轮子截取整个应用程序的屏幕截图,包括 WKWebView 我想修复 Cocoa 用于位图表示的方法。

我做了一些试验和错误,发现 ...cacheDisplayInRect... 和 ...drawInContext... 调用 - [NSView drawRect:] 在内部渲染元素。

- [CALayer drawInContext:context] 调用栈

#0  0x000000010000e28c in -[DXSecureWKWebView drawRect:] at /Users/x/dev/X/XSecureWKWebView.m:34
#1  0x00007fff8d386749 in -[NSView(NSInternal) _recursive:displayRectIgnoringOpacity:inGraphicsContext:CGContext:topView:shouldChangeFontReferenceColor:] ()
#2  0x00007fff8d386afe in -[NSView(NSInternal) _recursive:displayRectIgnoringOpacity:inGraphicsContext:CGContext:topView:shouldChangeFontReferenceColor:] ()
#3  0x00007fff8d386afe in -[NSView(NSInternal) _recursive:displayRectIgnoringOpacity:inGraphicsContext:CGContext:topView:shouldChangeFontReferenceColor:] ()
#4  0x00007fff8d386168 in __46-[NSView(NSLayerKitGlue) drawLayer:inContext:]_block_invoke ()
#5  0x00007fff8d385e11 in -[NSView(NSLayerKitGlue) _drawViewBackingLayer:inContext:drawingHandler:] ()
#6  0x00007fff8d385493 in -[NSView(NSLayerKitGlue) drawLayer:inContext:] ()

- [NSView cacheDisplayInRect:toBitmapImageRep:]调用堆栈

#0  0x000000010000e2dc in -[XSecureWKWebView drawRect:] at /Users/x/dev/x/XSecureWKWebView.m:34
#1  0x00007fff8d386749 in -[NSView(NSInternal) _recursive:displayRectIgnoringOpacity:inGraphicsContext:CGContext:topView:shouldChangeFontReferenceColor:] ()
#2  0x00007fff8d65fe1c in __50-[_NSViewBackingLayer _renderForegroundInContext:]_block_invoke ()
#3  0x00007fff8d385e11 in -[NSView(NSLayerKitGlue) _drawViewBackingLayer:inContext:drawingHandler:] ()
#4  0x00007fff8d50b740 in -[_NSViewBackingLayer _renderForegroundInContext:] ()
#5  0x00007fff934f5ce1 in -[CALayer renderInContext:] ()
#6  0x00007fff934f7aaf in -[CALayer _renderSublayersInContext:] ()
#7  0x00007fff8d50b807 in -[_NSViewBackingLayer _renderSublayersInContext:] ()
#8  0x00007fff934f5cf0 in -[CALayer renderInContext:] ()
#9  0x00007fff934f7aaf in -[CALayer _renderSublayersInContext:] ()
#10 0x00007fff8d50b807 in -[_NSViewBackingLayer _renderSublayersInContext:] ()
#11 0x00007fff934f5cf0 in -[CALayer renderInContext:] ()
#12 0x00007fff934f7aaf in -[CALayer _renderSublayersInContext:] ()
#13 0x00007fff8d50b807 in -[_NSViewBackingLayer _renderSublayersInContext:] ()
#14 0x00007fff934f5cf0 in -[CALayer renderInContext:] ()
#15 0x00007fff934f7aaf in -[CALayer _renderSublayersInContext:] ()
#16 0x00007fff8d50b807 in -[_NSViewBackingLayer _renderSublayersInContext:] ()
#17 0x00007fff934f5cf0 in -[CALayer renderInContext:] ()
#18 0x00007fff934f7aaf in -[CALayer _renderSublayersInContext:] ()
#19 0x00007fff8d50b807 in -[_NSViewBackingLayer _renderSublayersInContext:] ()
#20 0x00007fff934f5cf0 in -[CALayer renderInContext:] ()
#21 0x00007fff934f7aaf in -[CALayer _renderSublayersInContext:] ()
#22 0x00007fff8d50b807 in -[_NSViewBackingLayer _renderSublayersInContext:] ()
#23 0x00007fff934f5cf0 in -[CALayer renderInContext:] ()
#24 0x00007fff8d50b11d in -[NSView _layerBackedDisplayRectIgnoringOpacity:inContext:isRootView:flipContextIfNeedeed:] ()
#25 0x00007fff8d50ac74 in -[NSView displayRectIgnoringOpacity:inContext:] ()
#26 0x00007fff8d65f685 in __62-[NSView cacheDisplayInRect:toBitmapImageRep:includeSubviews:]_block_invoke ()
#27 0x00007fff8d65f3dc in __36-[NSBitmapImageRep _captureDrawing:]_block_invoke ()
#28 0x00007fff8d65f14c in -[NSBitmapImageRep _captureDrawing:] ()
#29 0x00007fff8d65f079 in -[NSView cacheDisplayInRect:toBitmapImageRep:includeSubviews:] ()