-
-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathSyphonServerImpl.mm
More file actions
197 lines (165 loc) · 5.43 KB
/
Copy pathSyphonServerImpl.mm
File metadata and controls
197 lines (165 loc) · 5.43 KB
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
/*
* SyphonServerImpl.mm
*
* (c) 2026 Alexandre Quessy -- alexandre(@)quessy(.)net
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Objective-C++ glue publishing MapMap's output composition as a Syphon
* server. Compiled only on macOS (gated in core.pri); manual reference
* counting (no ARC).
*
* Each frame we MSAA-resolve/blit the output canvas's framebuffer into the
* server's own FBO (bindToDrawFrameOfSize/unbindAndPublish), so the work is
* entirely on the GPU.
*/
#include "SyphonOutput.h"
#ifdef HAVE_SYPHON
#include <QDebug>
#import <Foundation/Foundation.h>
#import <OpenGL/OpenGL.h>
#import <OpenGL/gl.h>
#import <OpenGL/glext.h>
#import <Syphon/SyphonOpenGLServer.h>
namespace mmp {
static NSString* qStringToNS(const QString& s)
{
return [NSString stringWithUTF8String:(s.isEmpty() ? "MapMap" : s.toUtf8().constData())];
}
class SyphonServerImpl
{
public:
SyphonServerImpl()
: _server(nil), _serverCtx(NULL), _failed(false), _logged(false) {}
~SyphonServerImpl() { teardown(); }
void teardown()
{
if (_server != nil)
{
[_server stop];
[_server release];
_server = nil;
}
_serverCtx = NULL;
_failed = false;
_logged = false;
}
void setName(const QString& name)
{
@autoreleasepool {
if (_server != nil)
_server.name = qStringToNS(name);
}
}
void publish(GLuint sourceFbo, int w, int h, const QString& name)
{
if (w <= 0 || h <= 0)
return;
CGLContextObj cgl = CGLGetCurrentContext();
if (cgl == NULL)
return;
@autoreleasepool {
// (Re)create the server if needed, or if the GL context changed.
if (_server != nil && _serverCtx != cgl)
teardown();
if (_server == nil)
{
if (_failed)
return; // Already failed on this context; don't retry every frame.
_server = [[SyphonOpenGLServer alloc] initWithName:qStringToNS(name)
context:cgl
options:nil];
if (_server == nil)
{
_failed = true;
NSLog(@"[SyphonOutput] SyphonOpenGLServer init FAILED for context %p "
@"(legacy OpenGL output may be unsupported on this GPU).", cgl);
return;
}
_serverCtx = cgl;
NSLog(@"[SyphonOutput] server started: %@ context %p", qStringToNS(name), cgl);
}
glGetError(); // clear any pre-existing error
// Bind the server's FBO and blit our composition into it. A same-size
// blit from the (multisampled) widget FBO resolves MSAA in one step.
if ([_server bindToDrawFrameOfSize:NSMakeSize(w, h)])
{
GLint prevRead = 0;
glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING_EXT, &prevRead);
glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, sourceFbo);
glBlitFramebufferEXT(0, 0, w, h, 0, 0, w, h,
GL_COLOR_BUFFER_BIT, GL_NEAREST);
glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, (GLuint) prevRead);
[_server unbindAndPublish]; // restores the previously-bound FBO + flushes
if (!_logged)
{
_logged = true;
NSLog(@"[SyphonOutput] first publish %dx%d hasClients=%d glError=0x%x",
w, h, (int) [_server hasClients], (unsigned) glGetError());
}
}
else if (!_logged)
{
_logged = true;
NSLog(@"[SyphonOutput] bindToDrawFrameOfSize FAILED at %dx%d", w, h);
}
}
}
private:
SyphonOpenGLServer* _server;
CGLContextObj _serverCtx; // context the server was created with
bool _failed; // init failed on _serverCtx; stop retrying
bool _logged; // one-shot diagnostics
};
// ---------------------------------------------------------------------------
// SyphonOutput (C++ side).
// ---------------------------------------------------------------------------
SyphonOutput::SyphonOutput()
: _enabled(false),
_serverName(QStringLiteral("MapMap")),
_impl(new SyphonServerImpl())
{
}
SyphonOutput::~SyphonOutput()
{
delete _impl;
}
void SyphonOutput::setEnabled(bool on)
{
if (on == _enabled)
return;
_enabled = on;
// The server is created lazily on the next publish (needs a GL context); when
// disabling, tear it down so it disappears from the Syphon directory.
if (!on && _impl)
_impl->teardown();
}
void SyphonOutput::setServerName(const QString& name)
{
_serverName = name.isEmpty() ? QStringLiteral("MapMap") : name;
if (_impl)
_impl->setName(_serverName);
}
void SyphonOutput::publishCurrentFramebuffer()
{
if (!_enabled || !_impl)
return;
GLint fbo = 0;
GLint viewport[4] = { 0, 0, 0, 0 };
glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &fbo);
glGetIntegerv(GL_VIEWPORT, viewport);
_impl->publish((GLuint) fbo, viewport[2], viewport[3], _serverName);
}
}
#endif // HAVE_SYPHON