-
Notifications
You must be signed in to change notification settings - Fork 82
/
SyphonOpenGLServer.m
299 lines (259 loc) · 8.97 KB
/
SyphonOpenGLServer.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
SyphonOpenGLServer.m
Syphon
Copyright 2010-2011 bangnoise (Tom Butterworth) & vade (Anton Marini).
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#import "SyphonOpenGLServer.h"
#import "SyphonOpenGLImage.h"
#import "SyphonServerRendererLegacyGL.h"
#import "SyphonServerRendererCoreGL.h"
#import "SyphonPrivate.h"
#import "SyphonCGL.h"
#import "SyphonSubclassing.h"
#import <Cocoa/Cocoa.h>
#import <IOSurface/IOSurface.h>
// These are declared in core and legacy headers but this class is profile agnostic
// so define our own versions here
#define SYPHON_GL_TEXTURE_RECT 0x84F5
#define SYPHON_GL_TEXTURE_2D 0x0DE1
@implementation SyphonOpenGLServer
{
@private
SyphonServerRendererGL * _renderer;
CGLContextObj _shareContext;
BOOL _pushPending;
SyphonOpenGLImage *_surfaceTexture;
BOOL _wantsContextChanges;
GLint _virtualScreen;
}
// TODO: delete if we move these out of SyphonServer.h
// (they are redeclared from SyphonServerBase.h)
@dynamic name;
@dynamic serverDescription;
@dynamic hasClients;
+ (GLuint)integerValueForKey:(NSString *)key fromOptions:(NSDictionary *)options
{
NSNumber *number = [options objectForKey:key];
if ([number respondsToSelector:@selector(unsignedIntValue)])
{
return [number unsignedIntValue];
}
return 0;
}
- (id)init
{
self = [super init];
if (self)
{
self = nil;
}
return self;
}
- (instancetype)initWithName:(NSString*)serverName context:(CGLContextObj)context options:(NSDictionary *)options
{
self = [super initWithName:serverName options:options];
if(self)
{
if (context == NULL)
{
return nil;
}
// We check for changes to the context's virtual screen, so set it to an invalid value
// so our first binding counts as a change
_virtualScreen = -1;
GLuint MSAASampleCount = [[self class] integerValueForKey:SyphonServerOptionAntialiasSampleCount fromOptions:options];
GLuint depthBufferResolution = [[self class] integerValueForKey:SyphonServerOptionDepthBufferResolution fromOptions:options];
GLuint stencilBufferResolution = [[self class] integerValueForKey:SyphonServerOptionStencilBufferResolution fromOptions:options];
if (MSAASampleCount > 0 || (stencilBufferResolution > 0 && SyphonOpenGLContextIsLegacy(context)))
{
// For MSAA we need to check we don't exceed GL_MAX_SAMPLES when the context changes
// If we have a stencil buffer in a Legacy context, we rely on the GL_EXT_packed_depth_stencil extension
_wantsContextChanges = YES;
}
#ifdef SYPHON_CORE_SHARE
_shareContext = CGLRetainContext(context);
#endif
if (SyphonOpenGLContextIsLegacy(context))
{
_renderer = [[SyphonServerRendererLegacyGL alloc] initWithContext:context
MSAASampleCount:MSAASampleCount
depthBufferResolution:depthBufferResolution
stencilBufferResolution:stencilBufferResolution];
}
else
{
#ifdef SYPHON_CORE_SHARE
context = SyphonOpenGLCreateSharedContext(context);
#endif
_renderer = [[SyphonServerRendererCoreGL alloc] initWithContext:context
MSAASampleCount:MSAASampleCount
depthBufferResolution:depthBufferResolution
stencilBufferResolution:stencilBufferResolution];
#ifdef SYPHON_CORE_SHARE
CGLReleaseContext(context);
#endif
}
}
return self;
}
- (void) dealloc
{
[self destroyResources];
#ifdef SYPHON_CORE_SHARE
if (_shareContext)
{
CGLReleaseContext(_shareContext);
}
#endif
}
- (CGLContextObj)context
{
#ifdef SYPHON_CORE_SHARE
return _shareContext;
#else
return (_renderer).context;
#endif
}
- (void)stop
{
[self destroyResources];
[super stop];
}
- (BOOL)bindToDrawFrameOfSize:(NSSize)size inContext:(BOOL)isInContext
{
// TODO: we should probably check we're not already bound and raise an exception here
// to enforce proper use
#if !SYPHON_DEBUG_NO_DRAWING
// If we have changed screens, we need to check we can still use any extensions we rely on
// If the dimensions of the image have changed, rebuild the IOSurface/FBO/Texture combo.
if((_wantsContextChanges && [self capabilitiesDidChange]) || ! NSEqualSizes(_surfaceTexture.textureSize, size))
{
if (!isInContext)
{
[_renderer beginInContext];
}
[self destroyResources];
[self setupIOSurfaceForSize:size];
if (!isInContext)
{
[_renderer endInContext];
}
_pushPending = YES;
}
if (_surfaceTexture == nil)
{
return NO;
}
[_renderer bind];
#endif // SYPHON_DEBUG_NO_DRAWING
return YES;
}
- (BOOL)bindToDrawFrameOfSize:(NSSize)size
{
return [self bindToDrawFrameOfSize:size inContext:NO];
}
- (void)unbindAndPublish
{
#if !SYPHON_DEBUG_NO_DRAWING
[_renderer unbind];
#endif // SYPHON_DEBUG_NO_DRAWING
if (_pushPending)
{
#if !SYPHON_DEBUG_NO_DRAWING
// Our IOSurface won't update until the next glFlush(). Usually we rely on our host doing this, but
// we must do it for the first frame on a new surface to avoid sending surface details for a surface
// which has no clean image.
[_renderer flush];
#endif // SYPHON_DEBUG_NO_DRAWING
_pushPending = NO;
}
[self publish];
}
- (void)publishFrameTexture:(GLuint)texID textureTarget:(GLenum)target imageRegion:(NSRect)region textureDimensions:(NSSize)size flipped:(BOOL)isFlipped
{
[_renderer beginInContext];
if(texID != 0 && ((target == SYPHON_GL_TEXTURE_2D) || (target == SYPHON_GL_TEXTURE_RECT)) &&
[self bindToDrawFrameOfSize:region.size inContext:YES])
{
#if !SYPHON_DEBUG_NO_DRAWING
[_renderer drawFrameTexture:texID textureTarget:target imageRegion:region textureDimensions:size flipped:isFlipped];
#endif // SYPHON_DEBUG_NO_DRAWING
[self unbindAndPublish];
}
[_renderer endInContext];
}
- (SyphonOpenGLImage *)newFrameImage
{
return _surfaceTexture;
}
#pragma mark -
#pragma mark Private methods
#pragma mark FBO & IOSurface handling
- (BOOL)capabilitiesDidChange
{
#if !SYPHON_DEBUG_NO_DRAWING
GLint screen;
CGLGetVirtualScreen(_renderer.context, &screen);
if (screen != _virtualScreen)
{
_virtualScreen = screen;
[_renderer beginInContext];
BOOL changed = [_renderer capabilitiesDidChange];
[_renderer endInContext];
SYPHONLOG(@"SyphonOpenGLServer: renderer change, required capabilities %@", changed ? @"changed" : @"did not change");
return changed;
}
#endif // SYPHON_DEBUG_NO_DRAWING
return NO;
}
- (void) setupIOSurfaceForSize:(NSSize)size
{
#if !SYPHON_DEBUG_NO_DRAWING
// init our texture and IOSurface
// newSurfaceForWidth:height: returns a retained IOSurface, we release it
// once we are done with it
IOSurfaceRef surface = [self newSurfaceForWidth:size.width height:size.height options:nil];
_surfaceTexture = [_renderer newImageForSurface:surface];
if (surface)
{
CFRelease(surface);
}
if (_surfaceTexture)
{
[_renderer setupForBackingTexture:_surfaceTexture.textureName
width:_surfaceTexture.textureSize.width
height:_surfaceTexture.textureSize.height];
}
else
{
[_renderer destroySizedResources];
}
#endif // SYPHON_DEBUG_NO_DRAWING
}
- (void)destroyResources
{
#if !SYPHON_DEBUG_NO_DRAWING
[self destroySurface];
[_renderer destroySizedResources];
_surfaceTexture = nil;
#endif // SYPHON_DEBUG_NO_DRAWING
}
@end