1 | /* Copyright (c) 2001, Stanford University
|
---|
2 | * All rights reserved
|
---|
3 | *
|
---|
4 | * See the file LICENSE.txt for information on redistributing this software.
|
---|
5 | */
|
---|
6 |
|
---|
7 | #include "packspu.h"
|
---|
8 | #include "cr_mem.h"
|
---|
9 | #include "cr_packfunctions.h"
|
---|
10 | #include "cr_string.h"
|
---|
11 | #include "packspu_proto.h"
|
---|
12 |
|
---|
13 | #define MAGIC_OFFSET 3000
|
---|
14 |
|
---|
15 | /*
|
---|
16 | * Allocate a new ThreadInfo structure, setup a connection to the
|
---|
17 | * server, allocate/init a packer context, bind this ThreadInfo to
|
---|
18 | * the calling thread with crSetTSD().
|
---|
19 | * We'll always call this function at least once even if we're not
|
---|
20 | * using threads.
|
---|
21 | */
|
---|
22 | ThreadInfo *packspuNewThread(
|
---|
23 | #if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
|
---|
24 | struct VBOXUHGSMI *pHgsmi
|
---|
25 | #endif
|
---|
26 | )
|
---|
27 | {
|
---|
28 | ThreadInfo *thread=NULL;
|
---|
29 | int i;
|
---|
30 |
|
---|
31 | #ifdef CHROMIUM_THREADSAFE
|
---|
32 | crLockMutex(&_PackMutex);
|
---|
33 | #else
|
---|
34 | CRASSERT(pack_spu.numThreads == 0);
|
---|
35 | #endif
|
---|
36 |
|
---|
37 | #if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
|
---|
38 | CRASSERT(!CRPACKSPU_IS_WDDM_CRHGSMI() == !pHgsmi);
|
---|
39 | #endif
|
---|
40 |
|
---|
41 | CRASSERT(pack_spu.numThreads < MAX_THREADS);
|
---|
42 | for (i=0; i<MAX_THREADS; ++i)
|
---|
43 | {
|
---|
44 | if (!pack_spu.thread[i].inUse)
|
---|
45 | {
|
---|
46 | thread = &pack_spu.thread[i];
|
---|
47 | break;
|
---|
48 | }
|
---|
49 | }
|
---|
50 | CRASSERT(thread);
|
---|
51 |
|
---|
52 | thread->inUse = GL_TRUE;
|
---|
53 | if (!CRPACKSPU_IS_WDDM_CRHGSMI())
|
---|
54 | thread->id = crThreadID();
|
---|
55 | else
|
---|
56 | thread->id = THREAD_OFFSET_MAGIC + i;
|
---|
57 | thread->currentContext = NULL;
|
---|
58 | thread->bInjectThread = GL_FALSE;
|
---|
59 |
|
---|
60 | /* connect to the server */
|
---|
61 | thread->netServer.name = crStrdup( pack_spu.name );
|
---|
62 | thread->netServer.buffer_size = pack_spu.buffer_size;
|
---|
63 | packspuConnectToServer( &(thread->netServer)
|
---|
64 | #if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
|
---|
65 | , pHgsmi
|
---|
66 | #endif
|
---|
67 | );
|
---|
68 | CRASSERT(thread->netServer.conn);
|
---|
69 | /* packer setup */
|
---|
70 | CRASSERT(thread->packer == NULL);
|
---|
71 | thread->packer = crPackNewContext( pack_spu.swap );
|
---|
72 | CRASSERT(thread->packer);
|
---|
73 | crPackInitBuffer( &(thread->buffer), crNetAlloc(thread->netServer.conn),
|
---|
74 | thread->netServer.conn->buffer_size, thread->netServer.conn->mtu );
|
---|
75 | thread->buffer.canBarf = thread->netServer.conn->Barf ? GL_TRUE : GL_FALSE;
|
---|
76 | crPackSetBuffer( thread->packer, &thread->buffer );
|
---|
77 | crPackFlushFunc( thread->packer, packspuFlush );
|
---|
78 | crPackFlushArg( thread->packer, (void *) thread );
|
---|
79 | crPackSendHugeFunc( thread->packer, packspuHuge );
|
---|
80 |
|
---|
81 | if (!CRPACKSPU_IS_WDDM_CRHGSMI())
|
---|
82 | {
|
---|
83 | crPackSetContext( thread->packer );
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | #ifdef CHROMIUM_THREADSAFE
|
---|
88 | if (!CRPACKSPU_IS_WDDM_CRHGSMI())
|
---|
89 | {
|
---|
90 | crSetTSD(&_PackTSD, thread);
|
---|
91 | }
|
---|
92 | #endif
|
---|
93 |
|
---|
94 | pack_spu.numThreads++;
|
---|
95 |
|
---|
96 | #ifdef CHROMIUM_THREADSAFE
|
---|
97 | crUnlockMutex(&_PackMutex);
|
---|
98 | #endif
|
---|
99 | return thread;
|
---|
100 | }
|
---|
101 |
|
---|
102 | GLint PACKSPU_APIENTRY
|
---|
103 | packspu_VBoxConCreate(struct VBOXUHGSMI *pHgsmi)
|
---|
104 | {
|
---|
105 | #if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
|
---|
106 | ThreadInfo * thread;
|
---|
107 | CRASSERT(CRPACKSPU_IS_WDDM_CRHGSMI());
|
---|
108 | CRASSERT(pHgsmi);
|
---|
109 |
|
---|
110 | thread = packspuNewThread(pHgsmi);
|
---|
111 |
|
---|
112 | if (thread)
|
---|
113 | {
|
---|
114 | CRASSERT(thread->id);
|
---|
115 | CRASSERT(thread->id - THREAD_OFFSET_MAGIC < RT_ELEMENTS(pack_spu.thread)
|
---|
116 | && GET_THREAD_VAL_ID(thread->id) == thread);
|
---|
117 | return thread->id;
|
---|
118 | }
|
---|
119 | crError("packspuNewThread failed");
|
---|
120 | #endif
|
---|
121 | return 0;
|
---|
122 | }
|
---|
123 |
|
---|
124 | void PACKSPU_APIENTRY
|
---|
125 | packspu_VBoxConFlush(GLint con)
|
---|
126 | {
|
---|
127 | #if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
|
---|
128 | GET_THREAD_ID(thread, con);
|
---|
129 | CRASSERT(con);
|
---|
130 | CRASSERT(CRPACKSPU_IS_WDDM_CRHGSMI());
|
---|
131 | CRASSERT(thread->packer);
|
---|
132 | packspuFlush((void *) thread);
|
---|
133 | #else
|
---|
134 | crError("VBoxConFlush not implemented!");
|
---|
135 | #endif
|
---|
136 | }
|
---|
137 |
|
---|
138 | void PACKSPU_APIENTRY
|
---|
139 | packspu_VBoxConDestroy(GLint con)
|
---|
140 | {
|
---|
141 | #if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
|
---|
142 | GET_THREAD_ID(thread, con);
|
---|
143 | CRASSERT(con);
|
---|
144 | CRASSERT(CRPACKSPU_IS_WDDM_CRHGSMI());
|
---|
145 | CRASSERT(pack_spu.numThreads>0);
|
---|
146 | CRASSERT(thread->packer);
|
---|
147 | packspuFlush((void *) thread);
|
---|
148 |
|
---|
149 | crLockMutex(&_PackMutex);
|
---|
150 |
|
---|
151 | crPackDeleteContext(thread->packer);
|
---|
152 |
|
---|
153 | if (thread->buffer.pack)
|
---|
154 | {
|
---|
155 | crNetFree(thread->netServer.conn, thread->buffer.pack);
|
---|
156 | thread->buffer.pack = NULL;
|
---|
157 | }
|
---|
158 |
|
---|
159 | crNetFreeConnection(thread->netServer.conn);
|
---|
160 |
|
---|
161 | if (thread->netServer.name)
|
---|
162 | crFree(thread->netServer.name);
|
---|
163 |
|
---|
164 | pack_spu.numThreads--;
|
---|
165 | /*note can't shift the array here, because other threads have TLS references to array elements*/
|
---|
166 | crMemZero(thread, sizeof(ThreadInfo));
|
---|
167 |
|
---|
168 | #if 0
|
---|
169 | if (&pack_spu.thread[pack_spu.idxThreadInUse]==thread)
|
---|
170 | {
|
---|
171 | int i;
|
---|
172 | crError("Should not be here since idxThreadInUse should be always 0 for the dummy connection created in packSPUInit!");
|
---|
173 | for (i=0; i<MAX_THREADS; ++i)
|
---|
174 | {
|
---|
175 | if (pack_spu.thread[i].inUse)
|
---|
176 | {
|
---|
177 | pack_spu.idxThreadInUse=i;
|
---|
178 | break;
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|
182 | #endif
|
---|
183 | crUnlockMutex(&_PackMutex);
|
---|
184 | #endif
|
---|
185 | }
|
---|
186 |
|
---|
187 |
|
---|
188 | GLint PACKSPU_APIENTRY
|
---|
189 | packspu_VBoxCreateContext( GLint con, const char *dpyName, GLint visual, GLint shareCtx )
|
---|
190 | {
|
---|
191 | GET_THREAD(thread);
|
---|
192 | CRPackContext * curPacker = crPackGetContext();
|
---|
193 | ThreadInfo *curThread = thread;
|
---|
194 | int writeback = 1;
|
---|
195 | GLint serverCtx = (GLint) -1;
|
---|
196 | int slot;
|
---|
197 |
|
---|
198 | CRASSERT(!curThread == !curPacker);
|
---|
199 | CRASSERT(!curThread || !curPacker || curThread->packer == curPacker);
|
---|
200 | #ifdef CHROMIUM_THREADSAFE
|
---|
201 | crLockMutex(&_PackMutex);
|
---|
202 | #endif
|
---|
203 |
|
---|
204 | #if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
|
---|
205 | CRASSERT(!con == !CRPACKSPU_IS_WDDM_CRHGSMI());
|
---|
206 | #endif
|
---|
207 |
|
---|
208 | if (CRPACKSPU_IS_WDDM_CRHGSMI())
|
---|
209 | {
|
---|
210 | if (!con)
|
---|
211 | {
|
---|
212 | crError("connection should be specified!");
|
---|
213 | return -1;
|
---|
214 | }
|
---|
215 | thread = GET_THREAD_VAL_ID(con);
|
---|
216 | }
|
---|
217 | else
|
---|
218 | {
|
---|
219 | CRASSERT(!con);
|
---|
220 | if (!thread)
|
---|
221 | {
|
---|
222 | thread = packspuNewThread(
|
---|
223 | #if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
|
---|
224 | NULL
|
---|
225 | #endif
|
---|
226 | );
|
---|
227 | }
|
---|
228 | }
|
---|
229 | CRASSERT(thread);
|
---|
230 | CRASSERT(thread->packer);
|
---|
231 |
|
---|
232 | if (shareCtx > 0) {
|
---|
233 | /* translate to server ctx id */
|
---|
234 | shareCtx -= MAGIC_OFFSET;
|
---|
235 | if (shareCtx >= 0 && shareCtx < pack_spu.numContexts) {
|
---|
236 | shareCtx = pack_spu.context[shareCtx].serverCtx;
|
---|
237 | }
|
---|
238 | }
|
---|
239 |
|
---|
240 | crPackSetContext( thread->packer );
|
---|
241 |
|
---|
242 | /* Pack the command */
|
---|
243 | if (pack_spu.swap)
|
---|
244 | crPackCreateContextSWAP( dpyName, visual, shareCtx, &serverCtx, &writeback );
|
---|
245 | else
|
---|
246 | crPackCreateContext( dpyName, visual, shareCtx, &serverCtx, &writeback );
|
---|
247 |
|
---|
248 | /* Flush buffer and get return value */
|
---|
249 | packspuFlush(thread);
|
---|
250 | if (!(thread->netServer.conn->actual_network))
|
---|
251 | {
|
---|
252 | /* HUMUNGOUS HACK TO MATCH SERVER NUMBERING
|
---|
253 | *
|
---|
254 | * The hack exists solely to make file networking work for now. This
|
---|
255 | * is totally gross, but since the server expects the numbers to start
|
---|
256 | * from 5000, we need to write them out this way. This would be
|
---|
257 | * marginally less gross if the numbers (500 and 5000) were maybe
|
---|
258 | * some sort of #define'd constants somewhere so the client and the
|
---|
259 | * server could be aware of how each other were numbering things in
|
---|
260 | * cases like file networking where they actually
|
---|
261 | * care.
|
---|
262 | *
|
---|
263 | * -Humper
|
---|
264 | *
|
---|
265 | */
|
---|
266 | serverCtx = 5000;
|
---|
267 | }
|
---|
268 | else {
|
---|
269 | CRPACKSPU_WRITEBACK_WAIT(thread, writeback);
|
---|
270 |
|
---|
271 | if (pack_spu.swap) {
|
---|
272 | serverCtx = (GLint) SWAP32(serverCtx);
|
---|
273 | }
|
---|
274 | if (serverCtx < 0) {
|
---|
275 | #ifdef CHROMIUM_THREADSAFE
|
---|
276 | crUnlockMutex(&_PackMutex);
|
---|
277 | #endif
|
---|
278 | crWarning("Failure in packspu_CreateContext");
|
---|
279 |
|
---|
280 | if (CRPACKSPU_IS_WDDM_CRHGSMI())
|
---|
281 | {
|
---|
282 | /* restore the packer context to the tls */
|
---|
283 | crPackSetContext(curPacker);
|
---|
284 | }
|
---|
285 | return -1; /* failed */
|
---|
286 | }
|
---|
287 | }
|
---|
288 |
|
---|
289 | /* find an empty context slot */
|
---|
290 | for (slot = 0; slot < pack_spu.numContexts; slot++) {
|
---|
291 | if (!pack_spu.context[slot].clientState) {
|
---|
292 | /* found empty slot */
|
---|
293 | break;
|
---|
294 | }
|
---|
295 | }
|
---|
296 | if (slot == pack_spu.numContexts) {
|
---|
297 | pack_spu.numContexts++;
|
---|
298 | }
|
---|
299 |
|
---|
300 | if (CRPACKSPU_IS_WDDM_CRHGSMI())
|
---|
301 | {
|
---|
302 | thread->currentContext = &pack_spu.context[slot];
|
---|
303 | pack_spu.context[slot].currentThread = thread;
|
---|
304 | }
|
---|
305 |
|
---|
306 | /* Fill in the new context info */
|
---|
307 | /* XXX fix-up sharedCtx param here */
|
---|
308 | pack_spu.context[slot].clientState = crStateCreateContext(NULL, visual, NULL);
|
---|
309 | pack_spu.context[slot].clientState->bufferobject.retainBufferData = GL_TRUE;
|
---|
310 | pack_spu.context[slot].serverCtx = serverCtx;
|
---|
311 |
|
---|
312 | #ifdef CHROMIUM_THREADSAFE
|
---|
313 | crUnlockMutex(&_PackMutex);
|
---|
314 | #endif
|
---|
315 |
|
---|
316 | if (CRPACKSPU_IS_WDDM_CRHGSMI())
|
---|
317 | {
|
---|
318 | /* restore the packer context to the tls */
|
---|
319 | crPackSetContext(curPacker);
|
---|
320 | }
|
---|
321 |
|
---|
322 | return MAGIC_OFFSET + slot;
|
---|
323 | }
|
---|
324 |
|
---|
325 | GLint PACKSPU_APIENTRY
|
---|
326 | packspu_CreateContext( const char *dpyName, GLint visual, GLint shareCtx )
|
---|
327 | {
|
---|
328 | return packspu_VBoxCreateContext( 0, dpyName, visual, shareCtx );
|
---|
329 | }
|
---|
330 |
|
---|
331 |
|
---|
332 | void PACKSPU_APIENTRY packspu_DestroyContext( GLint ctx )
|
---|
333 | {
|
---|
334 | GET_THREAD(thread);
|
---|
335 | ThreadInfo *curThread = thread;
|
---|
336 | const int slot = ctx - MAGIC_OFFSET;
|
---|
337 | ContextInfo *context, *curContext;
|
---|
338 | CRPackContext * curPacker = crPackGetContext();
|
---|
339 |
|
---|
340 | CRASSERT(slot >= 0);
|
---|
341 | CRASSERT(slot < pack_spu.numContexts);
|
---|
342 |
|
---|
343 | context = &(pack_spu.context[slot]);
|
---|
344 |
|
---|
345 | if (CRPACKSPU_IS_WDDM_CRHGSMI())
|
---|
346 | {
|
---|
347 | thread = context->currentThread;
|
---|
348 | crPackSetContext(thread->packer);
|
---|
349 | CRASSERT(!(thread->packer == curPacker) == !(thread == curThread));
|
---|
350 | }
|
---|
351 | CRASSERT(thread);
|
---|
352 | curContext = curThread ? curThread->currentContext : NULL;
|
---|
353 |
|
---|
354 | if (pack_spu.swap)
|
---|
355 | crPackDestroyContextSWAP( context->serverCtx );
|
---|
356 | else
|
---|
357 | crPackDestroyContext( context->serverCtx );
|
---|
358 |
|
---|
359 | crStateDestroyContext( context->clientState );
|
---|
360 |
|
---|
361 | context->clientState = NULL;
|
---|
362 | context->serverCtx = 0;
|
---|
363 | context->currentThread = NULL;
|
---|
364 |
|
---|
365 | if (curContext == context)
|
---|
366 | {
|
---|
367 | if (!CRPACKSPU_IS_WDDM_CRHGSMI())
|
---|
368 | {
|
---|
369 | curThread->currentContext = NULL;
|
---|
370 | }
|
---|
371 | else
|
---|
372 | {
|
---|
373 | CRASSERT(thread == curThread);
|
---|
374 | crSetTSD(&_PackTSD, NULL);
|
---|
375 | crPackSetContext(NULL);
|
---|
376 | }
|
---|
377 | crStateMakeCurrent( NULL );
|
---|
378 | }
|
---|
379 | else
|
---|
380 | {
|
---|
381 | if (CRPACKSPU_IS_WDDM_CRHGSMI())
|
---|
382 | {
|
---|
383 | crPackSetContext(curPacker);
|
---|
384 | }
|
---|
385 | }
|
---|
386 | }
|
---|
387 |
|
---|
388 | void PACKSPU_APIENTRY packspu_MakeCurrent( GLint window, GLint nativeWindow, GLint ctx )
|
---|
389 | {
|
---|
390 | ThreadInfo *thread;
|
---|
391 | GLint serverCtx;
|
---|
392 | ContextInfo *newCtx;
|
---|
393 |
|
---|
394 | if (!CRPACKSPU_IS_WDDM_CRHGSMI())
|
---|
395 | {
|
---|
396 | thread = GET_THREAD_VAL();
|
---|
397 | if (!thread) {
|
---|
398 | thread = packspuNewThread(
|
---|
399 | #if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
|
---|
400 | NULL
|
---|
401 | #endif
|
---|
402 | );
|
---|
403 | }
|
---|
404 | CRASSERT(thread);
|
---|
405 | CRASSERT(thread->packer);
|
---|
406 | }
|
---|
407 |
|
---|
408 | if (ctx) {
|
---|
409 | const int slot = ctx - MAGIC_OFFSET;
|
---|
410 |
|
---|
411 | CRASSERT(slot >= 0);
|
---|
412 | CRASSERT(slot < pack_spu.numContexts);
|
---|
413 |
|
---|
414 | newCtx = &pack_spu.context[slot];
|
---|
415 | CRASSERT(newCtx->clientState); /* verify valid */
|
---|
416 |
|
---|
417 | if (CRPACKSPU_IS_WDDM_CRHGSMI())
|
---|
418 | {
|
---|
419 | thread = newCtx->currentThread;
|
---|
420 | CRASSERT(thread);
|
---|
421 | crSetTSD(&_PackTSD, thread);
|
---|
422 | crPackSetContext( thread->packer );
|
---|
423 | }
|
---|
424 | else
|
---|
425 | {
|
---|
426 | if (!newCtx->fAutoFlush)
|
---|
427 | {
|
---|
428 | if (newCtx->currentThread && newCtx->currentThread != thread)
|
---|
429 | {
|
---|
430 | crLockMutex(&_PackMutex);
|
---|
431 | /* do a flush for the previously assigned thread
|
---|
432 | * to ensure all commands issued there are submitted */
|
---|
433 | if (newCtx->currentThread
|
---|
434 | && newCtx->currentThread->inUse
|
---|
435 | && newCtx->currentThread->netServer.conn
|
---|
436 | && newCtx->currentThread->packer && newCtx->currentThread->packer->currentBuffer)
|
---|
437 | {
|
---|
438 | packspuFlush((void *) newCtx->currentThread);
|
---|
439 | }
|
---|
440 | crUnlockMutex(&_PackMutex);
|
---|
441 | }
|
---|
442 | newCtx->currentThread = thread;
|
---|
443 | }
|
---|
444 |
|
---|
445 | thread->currentContext = newCtx;
|
---|
446 | crPackSetContext( thread->packer );
|
---|
447 | }
|
---|
448 |
|
---|
449 | crStateMakeCurrent( newCtx->clientState );
|
---|
450 | //crStateSetCurrentPointers(newCtx->clientState, &thread->packer->current);
|
---|
451 | serverCtx = pack_spu.context[slot].serverCtx;
|
---|
452 | }
|
---|
453 | else {
|
---|
454 | crStateMakeCurrent( NULL );
|
---|
455 | if (CRPACKSPU_IS_WDDM_CRHGSMI())
|
---|
456 | {
|
---|
457 | thread = GET_THREAD_VAL();
|
---|
458 | if (!thread)
|
---|
459 | {
|
---|
460 | CRASSERT(crPackGetContext() == NULL);
|
---|
461 | return;
|
---|
462 | }
|
---|
463 | CRASSERT(thread->currentContext);
|
---|
464 | CRASSERT(thread->packer == crPackGetContext());
|
---|
465 | }
|
---|
466 | else
|
---|
467 | {
|
---|
468 | thread->currentContext = NULL;
|
---|
469 | }
|
---|
470 | newCtx = NULL;
|
---|
471 | serverCtx = 0;
|
---|
472 | }
|
---|
473 |
|
---|
474 | if (pack_spu.swap)
|
---|
475 | crPackMakeCurrentSWAP( window, nativeWindow, serverCtx );
|
---|
476 | else
|
---|
477 | crPackMakeCurrent( window, nativeWindow, serverCtx );
|
---|
478 |
|
---|
479 | {
|
---|
480 | GET_THREAD(t);
|
---|
481 | (void) t;
|
---|
482 | CRASSERT(t);
|
---|
483 | }
|
---|
484 | }
|
---|