VirtualBox

source: vbox/trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_stream.c@ 45248

最後變更 在這個檔案從45248是 44290,由 vboxsync 提交於 12 年 前

crOpenGL: saved state fixes & improvements

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id
檔案大小: 19.9 KB
 
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 "server.h"
8#include "cr_unpack.h"
9#include "cr_error.h"
10#include "cr_mem.h"
11#include "server_dispatch.h"
12
13
14/**
15 * Accept a new client connection, create a new CRClient and add to run queue.
16 */
17void
18crServerAddNewClient(void)
19{
20 CRClient *newClient = (CRClient *) crCalloc(sizeof(CRClient));
21
22 if (newClient) {
23 newClient->spu_id = cr_server.client_spu_id;
24 newClient->conn = crNetAcceptClient( cr_server.protocol, NULL,
25 cr_server.tcpip_port,
26 cr_server.mtu, 1 );
27
28 newClient->currentCtxInfo = &cr_server.MainContextInfo;
29
30 /* add to array */
31 cr_server.clients[cr_server.numClients++] = newClient;
32
33 crServerAddToRunQueue( newClient );
34 }
35}
36
37
38/**
39 * Check if client is in the run queue.
40 */
41static GLboolean
42FindClientInQueue(CRClient *client)
43{
44 RunQueue *q = cr_server.run_queue;
45 while (q) {
46 if (q->client == client) {
47 return 1;
48 }
49 q = q->next;
50 if (q == cr_server.run_queue)
51 return 0; /* back head */
52 }
53 return 0;
54}
55
56
57#if 0
58static int
59PrintQueue(void)
60{
61 RunQueue *q = cr_server.run_queue;
62 int count = 0;
63 crDebug("Queue entries:");
64 while (q) {
65 count++;
66 crDebug("Entry: %p client: %p", q, q->client);
67 q = q->next;
68 if (q == cr_server.run_queue)
69 return count;
70 }
71 return count;
72}
73#endif
74
75
76void crServerAddToRunQueue( CRClient *client )
77{
78 RunQueue *q = (RunQueue *) crAlloc( sizeof( *q ) );
79
80#ifdef VBOX_WITH_CRHGSMI
81 client->conn->pClient = client;
82 CRVBOXHGSMI_CMDDATA_CLEANUP(&client->conn->CmdData);
83#endif
84
85 /* give this client a unique number if needed */
86 if (!client->number) {
87 client->number = client->conn->u32ClientID;
88 }
89
90 crDebug("Adding client %p to the run queue", client);
91
92 if (FindClientInQueue(client)) {
93 crError("CRServer: client %p already in the queue!", client);
94 }
95
96 q->client = client;
97 q->blocked = 0;
98
99 if (!cr_server.run_queue)
100 {
101 /* adding to empty queue */
102 cr_server.run_queue = q;
103 q->next = q;
104 q->prev = q;
105 }
106 else
107 {
108 /* insert in doubly-linked list */
109 q->next = cr_server.run_queue->next;
110 cr_server.run_queue->next->prev = q;
111
112 q->prev = cr_server.run_queue;
113 cr_server.run_queue->next = q;
114 }
115}
116
117static void crServerCleanupClient(CRClient *client)
118{
119 int32_t pos;
120 CRClient *oldclient = cr_server.curClient;
121
122 cr_server.curClient = client;
123
124 /* Destroy any windows created by the client */
125 for (pos = 0; pos<CR_MAX_WINDOWS; pos++)
126 {
127 if (client->windowList[pos])
128 {
129 cr_server.dispatch.WindowDestroy(client->windowList[pos]);
130 }
131 }
132
133 /* Check if we have context(s) made by this client left, could happen if client side code is lazy */
134 for (pos = 0; pos<CR_MAX_CONTEXTS; pos++)
135 {
136 if (client->contextList[pos])
137 {
138 cr_server.dispatch.DestroyContext(client->contextList[pos]);
139 }
140 }
141
142 cr_server.curClient = oldclient;
143}
144
145static void crServerCleanupByPID(uint64_t pid)
146{
147 CRClientNode *pNode=cr_server.pCleanupClient, *pNext;
148
149 while (pNode)
150 {
151 if (pNode->pClient->pid==pid)
152 {
153 crServerCleanupClient(pNode->pClient);
154 crFree(pNode->pClient);
155 if (pNode->prev)
156 {
157 pNode->prev->next=pNode->next;
158 }
159 else
160 {
161 cr_server.pCleanupClient=pNode->next;
162 }
163 if (pNode->next)
164 {
165 pNode->next->prev = pNode->prev;
166 }
167
168 pNext=pNode->next;
169 crFree(pNode);
170 pNode=pNext;
171 }
172 else
173 {
174 pNode=pNode->next;
175 }
176 }
177}
178
179void
180crServerDeleteClient( CRClient *client )
181{
182 int i, j;
183 int cleanup=1;
184
185 crDebug("Deleting client %p (%d msgs left)", client, crNetNumMessages(client->conn));
186
187#if 0
188 if (crNetNumMessages(client->conn) > 0) {
189 crDebug("Delay destroying client: message still pending");
190 return;
191 }
192#endif
193
194 if (!FindClientInQueue(client)) {
195 /* this should never happen */
196 crError("CRServer: client %p not found in the queue!", client);
197 }
198
199 /* remove from clients[] array */
200 for (i = 0; i < cr_server.numClients; i++) {
201 if (cr_server.clients[i] == client) {
202 /* found it */
203 for (j = i; j < cr_server.numClients - 1; j++)
204 cr_server.clients[j] = cr_server.clients[j + 1];
205 cr_server.numClients--;
206 break;
207 }
208 }
209
210 /* check if there're any other guest threads in same process */
211 for (i=0; i < cr_server.numClients; i++)
212 {
213 if (cr_server.clients[i]->pid==client->pid)
214 {
215 cleanup=0;
216 break;
217 }
218 }
219
220 if (cleanup)
221 {
222 crServerCleanupClient(client);
223 }
224
225 /* remove from the run queue */
226 if (cr_server.run_queue)
227 {
228 RunQueue *q = cr_server.run_queue;
229 RunQueue *qStart = cr_server.run_queue;
230 do {
231 if (q->client == client)
232 {
233 /* this test seems a bit excessive */
234 if ((q->next == q->prev) && (q->next == q) && (cr_server.run_queue == q))
235 {
236 /* We're removing/deleting the only client */
237 CRASSERT(cr_server.numClients == 0);
238 crFree(q);
239 cr_server.run_queue = NULL;
240 cr_server.curClient = NULL;
241 crDebug("Last client deleted - empty run queue.");
242 }
243 else
244 {
245 /* remove from doubly linked list and free the node */
246 if (cr_server.curClient == q->client)
247 cr_server.curClient = NULL;
248 if (cr_server.run_queue == q)
249 cr_server.run_queue = q->next;
250 q->prev->next = q->next;
251 q->next->prev = q->prev;
252 crFree(q);
253 }
254 break;
255 }
256 q = q->next;
257 } while (q != qStart);
258 }
259
260 crNetFreeConnection(client->conn);
261 client->conn = NULL;
262
263 if (cleanup)
264 {
265 crServerCleanupByPID(client->pid);
266 crFree(client);
267 }
268 else
269 {
270 CRClientNode *pNode = (CRClientNode *)crAlloc(sizeof(CRClientNode));
271 if (!pNode)
272 {
273 crWarning("Not enough memory, forcing client cleanup");
274 crServerCleanupClient(client);
275 crServerCleanupByPID(client->pid);
276 crFree(client);
277 return;
278 }
279 pNode->pClient = client;
280 pNode->prev = NULL;
281 pNode->next = cr_server.pCleanupClient;
282 cr_server.pCleanupClient = pNode;
283 }
284}
285
286/**
287 * Test if the given client is in the middle of a glBegin/End or
288 * glNewList/EndList pair.
289 * This is used to test if we can advance to the next client.
290 * \return GL_TRUE if so, GL_FALSE otherwise.
291 */
292GLboolean
293crServerClientInBeginEnd(const CRClient *client)
294{
295 if (client->currentCtxInfo
296 && client->currentCtxInfo->pContext
297 && (client->currentCtxInfo->pContext->lists.currentIndex != 0 ||
298 client->currentCtxInfo->pContext->current.inBeginEnd ||
299 client->currentCtxInfo->pContext->occlusion.currentQueryObject)) {
300 return GL_TRUE;
301 }
302 else {
303 return GL_FALSE;
304 }
305}
306
307
308/**
309 * Find the next client in the run queue that's not blocked and has a
310 * waiting message.
311 * Check if all clients are blocked (on barriers, semaphores), if so we've
312 * deadlocked!
313 * If no clients have a waiting message, call crNetRecv to get something
314 * if 'block' is true, else return NULL if 'block' if false.
315 */
316static RunQueue *
317getNextClient(GLboolean block)
318{
319 while (1)
320 {
321 if (cr_server.run_queue)
322 {
323 GLboolean all_blocked = GL_TRUE;
324 GLboolean done_something = GL_FALSE;
325 RunQueue *start = cr_server.run_queue;
326
327 /* check if this client's connection has gone away */
328 if (!cr_server.run_queue->client->conn
329 || (cr_server.run_queue->client->conn->type == CR_NO_CONNECTION
330 && crNetNumMessages(cr_server.run_queue->client->conn) == 0))
331 {
332 crServerDeleteClient( cr_server.run_queue->client );
333 start = cr_server.run_queue;
334 }
335
336 if (cr_server.run_queue == NULL) {
337 /* empty queue */
338 return NULL;
339 }
340
341 if (crServerClientInBeginEnd(cr_server.run_queue->client)) {
342 /* We _must_ service this client and no other.
343 * If we've got a message waiting on this client's connection we'll
344 * service it. Else, return NULL.
345 */
346 if (crNetNumMessages(cr_server.run_queue->client->conn) > 0)
347 return cr_server.run_queue;
348 else
349 return NULL;
350 }
351
352 /* loop over entries in run queue, looking for next one that's ready */
353 while (!done_something || cr_server.run_queue != start)
354 {
355 done_something = GL_TRUE;
356 if (!cr_server.run_queue->blocked)
357 {
358 all_blocked = GL_FALSE;
359 }
360 if (!cr_server.run_queue->blocked
361 && cr_server.run_queue->client->conn
362 && crNetNumMessages(cr_server.run_queue->client->conn) > 0)
363 {
364 /* OK, this client isn't blocked and has a queued message */
365 return cr_server.run_queue;
366 }
367 cr_server.run_queue = cr_server.run_queue->next;
368 }
369
370 if (all_blocked)
371 {
372 /* XXX crError is fatal? Should this be an info/warning msg? */
373 crError( "crserver: DEADLOCK! (numClients=%d, all blocked)",
374 cr_server.numClients );
375 if (cr_server.numClients < (int) cr_server.maxBarrierCount) {
376 crError("Waiting for more clients!!!");
377 while (cr_server.numClients < (int) cr_server.maxBarrierCount) {
378 crNetRecv();
379 }
380 }
381 }
382 }
383
384 if (!block)
385 return NULL;
386
387 /* no one had any work, get some! */
388 crNetRecv();
389
390 } /* while */
391
392 /* UNREACHED */
393 /* return NULL; */
394}
395
396
397/**
398 * This function takes the given message (which should be a buffer of
399 * rendering commands) and executes it.
400 */
401static void
402crServerDispatchMessage(CRConnection *conn, CRMessage *msg)
403{
404 const CRMessageOpcodes *msg_opcodes;
405 int opcodeBytes;
406 const char *data_ptr;
407#ifdef VBOX_WITH_CRHGSMI
408 PCRVBOXHGSMI_CMDDATA pCmdData = NULL;
409#endif
410
411 if (msg->header.type == CR_MESSAGE_REDIR_PTR)
412 {
413#ifdef VBOX_WITH_CRHGSMI
414 pCmdData = &msg->redirptr.CmdData;
415#endif
416 msg = (CRMessage *) msg->redirptr.pMessage;
417 }
418
419 CRASSERT(msg->header.type == CR_MESSAGE_OPCODES);
420
421 msg_opcodes = (const CRMessageOpcodes *) msg;
422 opcodeBytes = (msg_opcodes->numOpcodes + 3) & ~0x03;
423
424#ifdef VBOXCR_LOGFPS
425 CRASSERT(cr_server.curClient && cr_server.curClient->conn && cr_server.curClient->conn->id == msg->header.conn_id);
426 cr_server.curClient->conn->opcodes_count += msg_opcodes->numOpcodes;
427#endif
428
429 data_ptr = (const char *) msg_opcodes + sizeof(CRMessageOpcodes) + opcodeBytes;
430 crUnpack(data_ptr, /* first command's operands */
431 data_ptr - 1, /* first command's opcode */
432 msg_opcodes->numOpcodes, /* how many opcodes */
433 &(cr_server.dispatch)); /* the CR dispatch table */
434
435#ifdef VBOX_WITH_CRHGSMI
436 if (pCmdData)
437 {
438 int rc = VINF_SUCCESS;
439 CRVBOXHGSMI_CMDDATA_ASSERT_CONSISTENT(pCmdData);
440 if (CRVBOXHGSMI_CMDDATA_IS_SETWB(pCmdData))
441 {
442 uint32_t cbWriteback = pCmdData->cbWriteback;
443 rc = crVBoxServerInternalClientRead(conn->pClient, (uint8_t*)pCmdData->pWriteback, &cbWriteback);
444 CRASSERT(rc == VINF_SUCCESS || rc == VERR_BUFFER_OVERFLOW);
445 *pCmdData->pcbWriteback = cbWriteback;
446 }
447 VBOXCRHGSMI_CMD_CHECK_COMPLETE(pCmdData, rc);
448 }
449#endif
450}
451
452
453typedef enum
454{
455 CLIENT_GONE = 1, /* the client has disconnected */
456 CLIENT_NEXT = 2, /* we can advance to next client */
457 CLIENT_MORE = 3 /* we need to keep servicing current client */
458} ClientStatus;
459
460
461/**
462 * Process incoming/pending message for the given client (queue entry).
463 * \return CLIENT_GONE if this client has gone away/exited,
464 * CLIENT_NEXT if we can advance to the next client
465 * CLIENT_MORE if we have to process more messages for this client.
466 */
467static ClientStatus
468crServerServiceClient(const RunQueue *qEntry)
469{
470 CRMessage *msg;
471 CRConnection *conn;
472
473 /* set current client pointer */
474 cr_server.curClient = qEntry->client;
475
476 conn = cr_server.run_queue->client->conn;
477
478 /* service current client as long as we can */
479 while (conn && conn->type != CR_NO_CONNECTION &&
480 crNetNumMessages(conn) > 0) {
481 unsigned int len;
482
483 /*
484 crDebug("%d messages on %p",
485 crNetNumMessages(conn), (void *) conn);
486 */
487
488 /* Don't use GetMessage, because we want to do our own crNetRecv() calls
489 * here ourself.
490 * Note that crNetPeekMessage() DOES remove the message from the queue
491 * if there is one.
492 */
493 len = crNetPeekMessage( conn, &msg );
494 CRASSERT(len > 0);
495 if (msg->header.type != CR_MESSAGE_OPCODES
496 && msg->header.type != CR_MESSAGE_REDIR_PTR) {
497 crError( "SPU %d sent me CRAP (type=0x%x)",
498 cr_server.curClient->spu_id, msg->header.type );
499 }
500
501 /* Do the context switch here. No sense in switching before we
502 * really have any work to process. This is a no-op if we're
503 * not really switching contexts.
504 *
505 * XXX This isn't entirely sound. The crStateMakeCurrent() call
506 * will compute the state difference and dispatch it using
507 * the head SPU's dispatch table.
508 *
509 * This is a problem if this is the first buffer coming in,
510 * and the head SPU hasn't had a chance to do a MakeCurrent()
511 * yet (likely because the MakeCurrent() command is in the
512 * buffer itself).
513 *
514 * At best, in this case, the functions are no-ops, and
515 * are essentially ignored by the SPU. In the typical
516 * case, things aren't too bad; if the SPU just calls
517 * crState*() functions to update local state, everything
518 * will work just fine.
519 *
520 * In the worst (but unusual) case where a nontrivial
521 * SPU is at the head of a crserver's SPU chain (say,
522 * in a multiple-tiered "tilesort" arrangement, as
523 * seen in the "multitilesort.conf" configuration), the
524 * SPU may rely on state set during the MakeCurrent() that
525 * may not be present yet, because no MakeCurrent() has
526 * yet been dispatched.
527 *
528 * This headache will have to be revisited in the future;
529 * for now, SPUs that could head a crserver's SPU chain
530 * will have to detect the case that their functions are
531 * being called outside of a MakeCurrent(), and will have
532 * to handle the situation gracefully. (This is currently
533 * the case with the "tilesort" SPU.)
534 */
535
536#if 0
537 crStateMakeCurrent( cr_server.curClient->currentCtx );
538#else
539 /* Check if the current window is the one that the client wants to
540 * draw into. If not, dispatch a MakeCurrent to activate the proper
541 * window.
542 */
543 if (cr_server.curClient) {
544 int clientWindow = cr_server.curClient->currentWindow;
545 int clientContext = cr_server.curClient->currentContextNumber;
546 CRContextInfo *clientCtxInfo = cr_server.curClient->currentCtxInfo;
547 if (clientCtxInfo != cr_server.currentCtxInfo
548 || clientWindow != cr_server.currentWindow
549 || cr_server.bForceMakeCurrentOnClientSwitch) {
550 crServerDispatchMakeCurrent(clientWindow, 0, clientContext);
551 /*
552 CRASSERT(cr_server.currentWindow == clientWindow);
553 */
554 }
555 }
556#endif
557
558 /* Force scissor, viewport and projection matrix update in
559 * crServerSetOutputBounds().
560 */
561 cr_server.currentSerialNo = 0;
562
563 /* Commands get dispatched here */
564 crServerDispatchMessage( conn, msg );
565
566 crNetFree( conn, msg );
567
568 if (qEntry->blocked) {
569 /* Note/assert: we should not be inside a glBegin/End or glNewList/
570 * glEndList pair at this time!
571 */
572 CRASSERT(0);
573 return CLIENT_NEXT;
574 }
575
576 } /* while */
577
578 /*
579 * Check if client/connection is gone
580 */
581 if (!conn || conn->type == CR_NO_CONNECTION) {
582 crDebug("Delete client %p at %d", cr_server.run_queue->client, __LINE__);
583 crServerDeleteClient( cr_server.run_queue->client );
584 return CLIENT_GONE;
585 }
586
587 /*
588 * Determine if we can advance to next client.
589 * If we're currently inside a glBegin/End primitive or building a display
590 * list we can't service another client until we're done with the
591 * primitive/list.
592 */
593 if (crServerClientInBeginEnd(cr_server.curClient)) {
594 /* The next message has to come from the current client's connection. */
595 CRASSERT(!qEntry->blocked);
596 return CLIENT_MORE;
597 }
598 else {
599 /* get next client */
600 return CLIENT_NEXT;
601 }
602}
603
604
605
606/**
607 * Check if any of the clients need servicing.
608 * If so, service one client and return.
609 * Else, just return.
610 */
611void
612crServerServiceClients(void)
613{
614 RunQueue *q;
615
616 q = getNextClient(GL_FALSE); /* don't block */
617 while (q)
618 {
619 ClientStatus stat = crServerServiceClient(q);
620 if (stat == CLIENT_NEXT && cr_server.run_queue->next) {
621 /* advance to next client */
622 cr_server.run_queue = cr_server.run_queue->next;
623 }
624 q = getNextClient(GL_FALSE);
625 }
626}
627
628
629
630
631/**
632 * Main crserver loop. Service connections from all connected clients.
633 * XXX add a config option to specify whether the crserver
634 * should exit when there's no more clients.
635 */
636void
637crServerSerializeRemoteStreams(void)
638{
639 /*MSG msg;*/
640
641 while (cr_server.run_queue)
642 {
643 crServerServiceClients();
644 /*if (PeekMessage( &msg, 0, 0, 0, PM_REMOVE ))
645 {
646 if (msg.message == WM_QUIT)
647 {
648 PostQuitMessage((int)msg.wParam);
649 break;
650 }
651 TranslateMessage( &msg );
652 DispatchMessage( &msg );
653 }*/
654 }
655}
656
657
658/**
659 * This will be called by the network layer when it's received a new message.
660 */
661int
662crServerRecv( CRConnection *conn, CRMessage *msg, unsigned int len )
663{
664 CRMessage *pRealMsg;
665 (void) len;
666
667 pRealMsg = (msg->header.type!=CR_MESSAGE_REDIR_PTR) ? msg : (CRMessage*) msg->redirptr.pMessage;
668
669 switch( pRealMsg->header.type )
670 {
671 /* Called when using multiple threads */
672 case CR_MESSAGE_NEWCLIENT:
673 crServerAddNewClient();
674 return 1; /* msg handled */
675 default:
676 /*crWarning( "Why is the crserver getting a message of type 0x%x?",
677 msg->header.type ); */
678 ;
679 }
680 return 0; /* not handled */
681}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette