-
Notifications
You must be signed in to change notification settings - Fork 604
/
Pipeline.swift
executable file
·227 lines (192 loc) · 6.91 KB
/
Pipeline.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// MARK: -
// MARK: Basic types
import Foundation
public protocol ImageSource {
var targets:TargetContainer { get }
func transmitPreviousImage(to target:ImageConsumer, atIndex:UInt)
}
public protocol ImageConsumer:AnyObject {
var maximumInputs:UInt { get }
var sources:SourceContainer { get }
func newFramebufferAvailable(_ framebuffer:Framebuffer, fromSourceIndex:UInt)
}
public protocol ImageProcessingOperation: ImageConsumer, ImageSource {
}
infix operator --> : AdditionPrecedence
//precedencegroup ProcessingOperationPrecedence {
// associativity: left
//// higherThan: Multiplicative
//}
@discardableResult public func --><T:ImageConsumer>(source:ImageSource, destination:T) -> T {
source.addTarget(destination)
return destination
}
// MARK: -
// MARK: Extensions and supporting types
public extension ImageSource {
public func addTarget(_ target:ImageConsumer, atTargetIndex:UInt? = nil) {
if let targetIndex = atTargetIndex {
target.setSource(self, atIndex:targetIndex)
targets.append(target, indexAtTarget:targetIndex)
transmitPreviousImage(to:target, atIndex:targetIndex)
} else if let indexAtTarget = target.addSource(self) {
targets.append(target, indexAtTarget:indexAtTarget)
transmitPreviousImage(to:target, atIndex:indexAtTarget)
} else {
debugPrint("Warning: tried to add target beyond target's input capacity")
}
}
public func removeAllTargets() {
for (target, index) in targets {
target.removeSourceAtIndex(index)
}
targets.removeAll()
}
public func updateTargetsWithFramebuffer(_ framebuffer:Framebuffer) {
if targets.count == 0 { // Deal with the case where no targets are attached by immediately returning framebuffer to cache
framebuffer.lock()
framebuffer.unlock()
} else {
// Lock first for each output, to guarantee proper ordering on multi-output operations
for _ in targets {
framebuffer.lock()
}
}
for (target, index) in targets {
target.newFramebufferAvailable(framebuffer, fromSourceIndex:index)
}
}
}
public extension ImageConsumer {
public func addSource(_ source:ImageSource) -> UInt? {
return sources.append(source, maximumInputs:maximumInputs)
}
public func setSource(_ source:ImageSource, atIndex:UInt) {
_ = sources.insert(source, atIndex:atIndex, maximumInputs:maximumInputs)
}
public func removeSourceAtIndex(_ index:UInt) {
sources.removeAtIndex(index)
}
}
class WeakImageConsumer {
weak var value:ImageConsumer?
let indexAtTarget:UInt
init (value:ImageConsumer, indexAtTarget:UInt) {
self.indexAtTarget = indexAtTarget
self.value = value
}
}
public class TargetContainer:Sequence {
var targets = [WeakImageConsumer]()
var count:Int { get {return targets.count}}
#if !os(Linux)
let dispatchQueue = DispatchQueue(label:"com.sunsetlakesoftware.GPUImage.targetContainerQueue", attributes: [])
#endif
public init() {
}
public func append(_ target:ImageConsumer, indexAtTarget:UInt) {
// TODO: Don't allow the addition of a target more than once
#if os(Linux)
self.targets.append(WeakImageConsumer(value:target, indexAtTarget:indexAtTarget))
#else
dispatchQueue.async{
self.targets.append(WeakImageConsumer(value:target, indexAtTarget:indexAtTarget))
}
#endif
}
public func makeIterator() -> AnyIterator<(ImageConsumer, UInt)> {
var index = 0
return AnyIterator { () -> (ImageConsumer, UInt)? in
#if os(Linux)
if (index >= self.targets.count) {
return nil
}
while (self.targets[index].value == nil) {
self.targets.remove(at:index)
if (index >= self.targets.count) {
return nil
}
}
index += 1
return (self.targets[index - 1].value!, self.targets[index - 1].indexAtTarget)
#else
return self.dispatchQueue.sync{
if (index >= self.targets.count) {
return nil
}
while (self.targets[index].value == nil) {
self.targets.remove(at:index)
if (index >= self.targets.count) {
return nil
}
}
index += 1
return (self.targets[index - 1].value!, self.targets[index - 1].indexAtTarget)
}
#endif
}
}
public func removeAll() {
#if os(Linux)
self.targets.removeAll()
#else
dispatchQueue.async{
self.targets.removeAll()
}
#endif
}
}
public class SourceContainer {
var sources:[UInt:ImageSource] = [:]
public init() {
}
public func append(_ source:ImageSource, maximumInputs:UInt) -> UInt? {
var currentIndex:UInt = 0
while currentIndex < maximumInputs {
if (sources[currentIndex] == nil) {
sources[currentIndex] = source
return currentIndex
}
currentIndex += 1
}
return nil
}
public func insert(_ source:ImageSource, atIndex:UInt, maximumInputs:UInt) -> UInt {
guard (atIndex < maximumInputs) else { fatalError("ERROR: Attempted to set a source beyond the maximum number of inputs on this operation") }
sources[atIndex] = source
return atIndex
}
public func removeAtIndex(_ index:UInt) {
sources[index] = nil
}
}
public class ImageRelay: ImageProcessingOperation {
public var newImageCallback:((Framebuffer) -> ())?
public let sources = SourceContainer()
public let targets = TargetContainer()
public let maximumInputs:UInt = 1
public var preventRelay:Bool = false
public init() {
}
public func transmitPreviousImage(to target:ImageConsumer, atIndex:UInt) {
sources.sources[0]?.transmitPreviousImage(to:self, atIndex:0)
}
public func newFramebufferAvailable(_ framebuffer:Framebuffer, fromSourceIndex:UInt) {
if let newImageCallback = newImageCallback {
newImageCallback(framebuffer)
}
if (!preventRelay) {
relayFramebufferOnward(framebuffer)
}
}
public func relayFramebufferOnward(_ framebuffer:Framebuffer) {
// Need to override to guarantee a removal of the previously applied lock
for _ in targets {
framebuffer.lock()
}
framebuffer.unlock()
for (target, index) in targets {
target.newFramebufferAvailable(framebuffer, fromSourceIndex:index)
}
}
}