-
Notifications
You must be signed in to change notification settings - Fork 217
/
CPRGenerator.m
246 lines (185 loc) · 7.17 KB
/
CPRGenerator.m
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
228
229
230
231
232
233
/*=========================================================================
Program: OsiriX
Copyright (c) OsiriX Team
All rights reserved.
Distributed under GNU - LGPL
See http://www.osirix-viewer.com/copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
=========================================================================*/
#import "CPRGenerator.h"
#import "CPRVolumeData.h"
#import "CPRGeneratorRequest.h"
#import "CPRGeneratorOperation.h"
#import "DCMPix.h"
static NSOperationQueue *_synchronousRequestQueue = nil;
NSString * const _CPRGeneratorRunLoopMode = @"_CPRGeneratorRunLoopMode";
@interface CPRGenerator ()
+ (NSOperationQueue *)_synchronousRequestQueue;
- (void)_didFinishOperation;
- (void)_cullGeneratedFrameTimes;
- (void)_logFrameRate:(NSTimer *)timer;
@end
@implementation CPRGenerator
@synthesize delegate = _delegate;
@synthesize volumeData = _volumeData;
+ (NSOperationQueue *)_synchronousRequestQueue
{
@synchronized (self) {
if (_synchronousRequestQueue == nil) {
_synchronousRequestQueue = [[NSOperationQueue alloc] init];
NSUInteger threads = [[NSProcessInfo processInfo] processorCount];
if( threads > 2)
threads = 2;
[_synchronousRequestQueue setMaxConcurrentOperationCount: threads];
}
}
return _synchronousRequestQueue;
}
+ (CPRVolumeData *)synchronousRequestVolume:(CPRGeneratorRequest *)request volumeData:(CPRVolumeData *)volumeData
{
CPRGeneratorOperation *operation;
NSOperationQueue *operationQueue;
CPRVolumeData *generatedVolume;
operation = [[[request operationClass] alloc] initWithRequest:request volumeData:volumeData];
[operation setQueuePriority:NSOperationQueuePriorityVeryHigh];
operationQueue = [self _synchronousRequestQueue];
[operationQueue addOperation:operation];
[operationQueue waitUntilAllOperationsAreFinished];
generatedVolume = [[operation.generatedVolume retain] autorelease];
[operation release];
return generatedVolume;
}
- (id)initWithVolumeData:(CPRVolumeData *)volumeData
{
assert([NSThread isMainThread]);
if ( (self = [super init]) ) {
_volumeData = [volumeData retain];
_generatorQueue = [[NSOperationQueue alloc] init];
NSUInteger threads = [[NSProcessInfo processInfo] processorCount];
if( threads > 2)
threads = 2;
[_generatorQueue setMaxConcurrentOperationCount: threads];
_observedOperations = [[NSMutableSet alloc] init];
_finishedOperations = [[NSMutableArray alloc] init];
_generatedFrameTimes = [[NSMutableArray alloc] init];
}
return self;
}
- (void)dealloc
{
[_volumeData release];
_volumeData = nil;
[_generatorQueue release];
_generatorQueue = nil;
[_observedOperations release];
_observedOperations = nil;
[_finishedOperations release];
_finishedOperations = nil;
[_generatedFrameTimes release];
_generatedFrameTimes = nil;
[super dealloc];
}
- (void)runUntilAllRequestsAreFinished
{
assert([NSThread isMainThread]);
while( [_observedOperations count] > 0) {
[[NSRunLoop mainRunLoop] runMode:_CPRGeneratorRunLoopMode beforeDate:[NSDate distantFuture]];
}
}
- (void)requestVolume:(CPRGeneratorRequest *)request
{
CPRGeneratorOperation *operation;
assert([NSThread isMainThread]);
for (operation in _observedOperations) {
if ([operation isExecuting] == NO && [operation isFinished] == NO) {
[operation cancel];
}
}
operation = [[[request operationClass] alloc] initWithRequest:[[request copy] autorelease] volumeData:_volumeData];
[operation setQueuePriority:NSOperationQueuePriorityNormal];
[self retain]; // so that the generator can't disappear while the operation is running
[operation addObserver:self forKeyPath:@"isFinished" options:0 context:&self->_generatorQueue];
[_observedOperations addObject:operation];
[_generatorQueue addOperation:operation];
[operation release];
}
- (CGFloat)frameRate
{
assert([NSThread isMainThread]);
[self _cullGeneratedFrameTimes];
return (CGFloat)[_generatedFrameTimes count] / 4.0;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
CPRGeneratorOperation *generatorOperation;
if (context == &self->_generatorQueue) {
assert([object isKindOfClass:[CPRGeneratorOperation class]]);
generatorOperation = (CPRGeneratorOperation *)object;
if ([keyPath isEqualToString:@"isFinished"]) {
if ([object isFinished]) {
@synchronized (_finishedOperations) {
[_finishedOperations addObject:generatorOperation];
}
[self performSelectorOnMainThread:@selector(_didFinishOperation) withObject:nil waitUntilDone:NO
modes:[NSArray arrayWithObjects:NSRunLoopCommonModes, _CPRGeneratorRunLoopMode, nil]];
}
}
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
- (void)_didFinishOperation;
{
CPRVolumeData *volumeData;
CPRGeneratorOperation *operation;
NSArray *finishedOperations;
NSInteger i;
BOOL sentGeneratedVolume;
assert([NSThread isMainThread]);
sentGeneratedVolume = NO;
@synchronized (_finishedOperations) {
finishedOperations = [_finishedOperations copy];
[_finishedOperations removeAllObjects];
}
for (i = [finishedOperations count] - 1; i >= 0; i--) {
operation = [finishedOperations objectAtIndex:i];
[operation removeObserver:self forKeyPath:@"isFinished"];
[self autorelease]; // to match the retain in -[CPRGenerator requestVolume:]
volumeData = operation.generatedVolume;
if (volumeData && [operation isCancelled] == NO && sentGeneratedVolume == NO) {
[_generatedFrameTimes addObject:[NSDate date]];
[self _cullGeneratedFrameTimes];
if ([_delegate respondsToSelector:@selector(generator:didGenerateVolume:request:)]) {
[_delegate generator:self didGenerateVolume:operation.generatedVolume request:operation.request];
}
sentGeneratedVolume = YES;
} else {
if ([_delegate respondsToSelector:@selector(generator:didAbandonRequest:)]) {
[_delegate generator:self didAbandonRequest:operation.request];
}
}
[_observedOperations removeObject:operation];
}
[finishedOperations release];
}
- (void)_cullGeneratedFrameTimes
{
BOOL done;
assert([NSThread isMainThread]);
// remove times that are older than 4 seconds
done = NO;
while (!done) {
if ([_generatedFrameTimes count] && [[_generatedFrameTimes objectAtIndex:0] timeIntervalSinceNow] < -4.0) {
[_generatedFrameTimes removeObjectAtIndex:0];
} else {
done = YES;
}
}
}
- (void)_logFrameRate:(NSTimer *)timer
{
NSLog(@"CPRGenerator frame rate: %f", [self frameRate]);
}
@end