-
Notifications
You must be signed in to change notification settings - Fork 43
/
ImageBufferView.swift
201 lines (170 loc) · 5.22 KB
/
ImageBufferView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//
// ImageBufferView.swift
// visionOS
//
// Created by Saagar Jha on 10/10/23.
//
import MetalKit
import SwiftUI
struct ImageBufferView: View, Equatable {
let imageBuffer: CVImageBuffer
var mask: Bool = false
static var useMetal: Bool {
#if targetEnvironment(simulator)
true
#else
false
#endif
}
var body: some View {
if ImageBufferView.useMetal {
AcceleratedImageBufferView<MetalAccelerator>(imageBuffer: imageBuffer)
} else {
if mask {
let image = CIImage(cvImageBuffer: imageBuffer)
let cgImage = CIContext().createCGImage(image, from: image.extent)!
Image(uiImage: UIImage(cgImage: cgImage))
.resizable()
.aspectRatio(contentMode: .fit)
} else {
AcceleratedImageBufferView<CoreAnimationAccelerator>(imageBuffer: imageBuffer)
}
}
}
static func == (lhs: Self, rhs: Self) -> Bool {
guard !useMetal else {
return false
}
CVPixelBufferLockBaseAddress(lhs.imageBuffer, .readOnly)
defer {
CVPixelBufferUnlockBaseAddress(lhs.imageBuffer, .readOnly)
}
let left = UnsafeRawBufferPointer(start: CVPixelBufferGetBaseAddress(lhs.imageBuffer), count: CVPixelBufferGetDataSize(lhs.imageBuffer))
CVPixelBufferLockBaseAddress(rhs.imageBuffer, .readOnly)
defer {
CVPixelBufferUnlockBaseAddress(rhs.imageBuffer, .readOnly)
}
let right = UnsafeRawBufferPointer(start: CVPixelBufferGetBaseAddress(rhs.imageBuffer), count: CVPixelBufferGetDataSize(rhs.imageBuffer))
return left.count == right.count && memcmp(left.baseAddress, right.baseAddress, min(left.count, right.count)) == 0
}
}
class CoreAnimationAccelerator: UIView, ImageBufferAccelerator {
let sublayer = CALayer()
func setImageBuffer(_ imageBuffer: CVImageBuffer) {
sublayer.contents = imageBuffer
}
override func layoutSubviews() {
sublayer.frame = bounds
}
}
extension CoreAnimationAccelerator {
convenience init() {
self.init(frame: .zero)
layer.addSublayer(sublayer)
sublayer.contentsGravity = .resizeAspect
}
}
class MetalAccelerator: UIView, ImageBufferAccelerator {
var imageBuffer: CVImageBuffer!
var image: CIImage! {
didSet {
Task {
await render()
}
}
}
override class var layerClass: AnyClass {
CAMetalLayer.self
}
override func layoutSubviews() {
layer.frame = bounds
// If the view lays out again, refresh the Metal layer.
Task {
await render()
}
}
var metalLayer: CAMetalLayer {
layer as! CAMetalLayer
}
let colorSpace = CGColorSpaceCreateDeviceRGB()
var commandQueue: MTLCommandQueue!
var context: CIContext!
actor Drawable {
var layer: CAMetalLayer
let queue = DispatchQueue(label: "\(Bundle.main.bundleIdentifier!).\(_typeName(Drawable.self, qualified: false))")
var lastContinuation: CheckedContinuation<CAMetalDrawable?, Never>?
init(layer: CAMetalLayer) {
self.layer = layer
}
func requestDrawable() async -> CAMetalDrawable? {
let layer = self.layer
lastContinuation?.resume(returning: nil)
lastContinuation = nil
return await withCheckedContinuation { continuation in
lastContinuation = continuation
queue.async {
let drawable = layer.nextDrawable()
Task {
await self.yieldDrawable(drawable)
}
}
}
}
func yieldDrawable(_ drawable: CAMetalDrawable?) {
lastContinuation?.resume(returning: drawable)
lastContinuation = nil
}
}
var drawable: Drawable!
func setImageBuffer(_ imageBuffer: CVImageBuffer) {
if imageBuffer != self.imageBuffer {
image = CIImage(cvImageBuffer: imageBuffer)
}
}
func render() async {
guard frame != .zero,
let drawable = await drawable.requestDrawable()
else {
return
}
let texture = drawable.texture
let commandBuffer = commandQueue.makeCommandBuffer()!
let scale = min(metalLayer.drawableSize.width / image.extent.width, metalLayer.drawableSize.height / image.extent.height)
let origin = CGPoint(x: (metalLayer.drawableSize.width - scale * image.extent.width) / 2, y: (metalLayer.drawableSize.height - scale * image.extent.height) / 2)
let scaled =
image
.transformed(by: image.orientationTransform(for: .downMirrored))
.transformed(by: CGAffineTransform(scaleX: scale, y: scale))
.transformed(by: .init(translationX: origin.x, y: origin.y))
context.render(scaled, to: texture, commandBuffer: commandBuffer, bounds: .init(origin: .zero, size: metalLayer.drawableSize), colorSpace: colorSpace)
commandBuffer.present(drawable)
commandBuffer.commit()
}
}
extension MetalAccelerator {
convenience init() {
self.init(frame: .zero)
drawable = .init(layer: metalLayer)
metalLayer.isOpaque = false
// FIXME: figure out how drawables are reserved
// metalLayer.allowsNextDrawableTimeout = false
metalLayer.contentsScale = contentScaleFactor
metalLayer.framebufferOnly = false
let device = metalLayer.preferredDevice!
metalLayer.device = device
commandQueue = device.makeCommandQueue()
context = CIContext(mtlDevice: device)
}
}
protocol ImageBufferAccelerator: UIView {
func setImageBuffer(_ imageBuffer: CVImageBuffer)
}
struct AcceleratedImageBufferView<UIViewType: ImageBufferAccelerator>: UIViewRepresentable {
let imageBuffer: CVImageBuffer
func makeUIView(context: Context) -> UIViewType {
return UIViewType()
}
func updateUIView(_ uiView: UIViewType, context: Context) {
uiView.setImageBuffer(imageBuffer)
}
}