-
-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathClientWin.cpp
More file actions
263 lines (236 loc) · 5.66 KB
/
Copy pathClientWin.cpp
File metadata and controls
263 lines (236 loc) · 5.66 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
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
// Copyright (C)2004 Landmark Graphics Corporation
// Copyright (C)2005, 2006 Sun Microsystems, Inc.
// Copyright (C)2009, 2011, 2014, 2019, 2021, 2025 D. R. Commander
//
// This library is free software and may be redistributed and/or modified under
// the terms of the wxWindows Library License, Version 3.1 or (at your option)
// any later version. The full license is in the LICENSE.txt file included
// with this distribution.
//
// This library 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
// wxWindows Library License for more details.
#include "ClientWin.h"
#include "Error.h"
#include "Log.h"
#include "Profiler.h"
#include "GLFrame.h"
using namespace util;
using namespace common;
using namespace client;
extern Display *maindpy;
ClientWin::ClientWin(int dpynum_, Window window_, int drawMethod_,
bool stereo_) : drawMethod(drawMethod_), reqDrawMethod(drawMethod_),
fb(NULL), cfindex(0), deadYet(false), thread(NULL), stereo(stereo_)
{
if(dpynum_ < 0 || dpynum_ > 65535 || !window_)
throw(Error("ClientWin::ClientWin()", "Invalid argument"));
dpynum = dpynum_; window = window_;
#ifdef USEXV
for(int i = 0; i < NFRAMES; i++) xvframes[i] = NULL;
#endif
if(drawMethod == RR_DRAWAUTO) drawMethod = RR_DRAWX11;
if(stereo) drawMethod = RR_DRAWOGL;
initGL();
initX11();
thread = new Thread(this);
thread->start();
}
ClientWin::~ClientWin(void)
{
deadYet = true;
q.release();
if(thread) thread->stop();
delete fb; fb = NULL;
#ifdef USEXV
for(int i = 0; i < NFRAMES; i++)
{
if(xvframes[i])
{
xvframes[i]->signalComplete(); delete xvframes[i]; xvframes[i] = NULL;
}
}
#endif
for(int i = 0; i < NFRAMES; i++) cframes[i].signalComplete();
delete thread; thread = NULL;
}
void ClientWin::initGL(void)
{
GLFrame *newfb = NULL;
char dpystr[80];
snprintf(dpystr, 80, ":%d.0", dpynum);
CriticalSection::SafeLock l(mutex);
if(drawMethod == RR_DRAWOGL)
{
try
{
newfb = new GLFrame(dpystr, window);
if(!newfb) throw("Could not allocate class instance");
}
catch(std::exception &e)
{
vglout.println("OpenGL error-- %s\nUsing X11 drawing instead", e.what());
delete newfb; newfb = NULL;
drawMethod = RR_DRAWX11;
vglout.PRINTLN("Stereo requires OpenGL drawing. Disabling stereo.");
stereo = false;
return;
}
}
if(newfb)
{
if(fb)
{
if(fb->isGL) delete ((GLFrame *)fb);
else delete ((FBXFrame *)fb);
}
fb = (Frame *)newfb;
}
}
void ClientWin::initX11(void)
{
FBXFrame *newfb = NULL;
char dpystr[80];
snprintf(dpystr, 80, ":%d.0", dpynum);
CriticalSection::SafeLock l(mutex);
if(drawMethod == RR_DRAWX11)
{
try
{
newfb = new FBXFrame(dpystr, window);
if(!newfb) throw("Could not allocate class instance");
}
catch(...)
{
delete newfb; newfb = NULL;
throw;
}
}
if(newfb)
{
if(fb)
{
if(fb->isGL) { delete ((GLFrame *)fb); }
else delete ((FBXFrame *)fb);
}
fb = (Frame *)newfb;
}
}
int ClientWin::match(int dpynum_, Window window_)
{
return dpynum == dpynum_ && window == window_;
}
Frame *ClientWin::getFrame(bool useXV)
{
Frame *f = NULL;
if(thread) thread->checkError();
cfmutex.lock();
#ifdef USEXV
if(useXV)
{
if(!xvframes[cfindex])
{
char dpystr[80];
snprintf(dpystr, 80, ":%d.0", dpynum);
xvframes[cfindex] = new XVFrame(dpystr, window);
if(!xvframes[cfindex]) THROW("Could not allocate class instance");
}
f = (Frame *)xvframes[cfindex];
}
else
#endif
f = (Frame *)&cframes[cfindex];
cfindex = (cfindex + 1) % NFRAMES;
cfmutex.unlock();
f->waitUntilComplete();
if(thread) thread->checkError();
return f;
}
void ClientWin::drawFrame(Frame *f)
{
if(thread) thread->checkError();
if(!f->isXV)
{
CompressedFrame *c = (CompressedFrame *)f;
if((c->rhdr.flags == RR_RIGHT || c->hdr.flags == RR_LEFT) && !stereo)
{
stereo = true;
if(drawMethod != RR_DRAWOGL)
{
drawMethod = RR_DRAWOGL;
initGL();
}
}
if((c->hdr.flags == 0) && stereo)
{
stereo = false;
drawMethod = reqDrawMethod;
if(drawMethod == RR_DRAWAUTO) drawMethod = RR_DRAWX11;
initX11();
}
}
q.add(f);
}
void ClientWin::run(void)
{
Profiler pt("Total "), pb("Blit "), pd("Decompress");
Frame *f = NULL; long bytes = 0;
try
{
while(!deadYet)
{
void *ftemp = NULL;
q.get(&ftemp); f = (Frame *)ftemp; if(deadYet) break;
if(!f)
throw(Error("ClientWin::run()", "Invalid image received from queue"));
CriticalSection::SafeLock l(mutex);
#ifdef USEXV
if(f->isXV)
{
if(f->hdr.flags != RR_EOF)
{
pb.startFrame();
((XVFrame *)f)->redraw();
pb.endFrame(f->hdr.width * f->hdr.height, 0, 1);
pt.endFrame(f->hdr.width * f->hdr.height, bytes, 1);
bytes = 0;
pt.startFrame();
}
}
else
#endif
{
if(f->hdr.flags == RR_EOF)
{
pb.startFrame();
if(fb->isGL) ((GLFrame *)fb)->init(f->hdr, stereo);
else ((FBXFrame *)fb)->init(f->hdr);
if(fb->isGL) ((GLFrame *)fb)->redraw();
else ((FBXFrame *)fb)->redraw();
pb.endFrame(fb->hdr.framew * fb->hdr.frameh, 0, 1);
pt.endFrame(fb->hdr.framew * fb->hdr.frameh, bytes, 1);
bytes = 0;
pt.startFrame();
}
else
{
pd.startFrame();
if(fb->isGL) *((GLFrame *)fb) = *((CompressedFrame *)f);
else *((FBXFrame *)fb) = *((CompressedFrame *)f);
pd.endFrame(f->hdr.width * f->hdr.height, 0,
(double)(f->hdr.width * f->hdr.height) /
(double)(f->hdr.framew * f->hdr.frameh));
bytes += f->hdr.size;
}
}
f->signalComplete();
}
}
catch(std::exception &e)
{
if(thread) thread->setError(e);
if(f) f->signalComplete();
throw;
}
}