VirtualBox

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

Last change on this file since 50625 was 50625, checked in by vboxsync, 11 years ago

crOpenGL: misc bugfixes and cleanup

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 20.5 KB
Line 
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 if (!cr_server.numClients)
286 {
287 /* if no clients, the guest driver may be unloaded,
288 * and thus the visible regions situation might not be under control anymore,
289 * so cleanup the 3D framebuffer data here
290 * @todo: what really should happen is that guest driver on unload
291 * posts some request to host that would copy the current framebuffer 3D data to the 2D buffer
292 * (i.e. to the memory used by the standard IFramebuffer API) */
293 HCR_FRAMEBUFFER hFb;
294 for (hFb = CrPMgrFbGetFirstEnabled(); hFb; hFb = CrPMgrFbGetNextEnabled(hFb))
295 {
296 CrFbRegionsClear(hFb);
297 }
298 }
299}
300
301/**
302 * Test if the given client is in the middle of a glBegin/End or
303 * glNewList/EndList pair.
304 * This is used to test if we can advance to the next client.
305 * \return GL_TRUE if so, GL_FALSE otherwise.
306 */
307GLboolean
308crServerClientInBeginEnd(const CRClient *client)
309{
310 if (client->currentCtxInfo
311 && client->currentCtxInfo->pContext
312 && (client->currentCtxInfo->pContext->lists.currentIndex != 0 ||
313 client->currentCtxInfo->pContext->current.inBeginEnd ||
314 client->currentCtxInfo->pContext->occlusion.currentQueryObject)) {
315 return GL_TRUE;
316 }
317 else {
318 return GL_FALSE;
319 }
320}
321
322
323/**
324 * Find the next client in the run queue that's not blocked and has a
325 * waiting message.
326 * Check if all clients are blocked (on barriers, semaphores), if so we've
327 * deadlocked!
328 * If no clients have a waiting message, call crNetRecv to get something
329 * if 'block' is true, else return NULL if 'block' if false.
330 */
331static RunQueue *
332getNextClient(GLboolean block)
333{
334 while (1)
335 {
336 if (cr_server.run_queue)
337 {
338 GLboolean all_blocked = GL_TRUE;
339 GLboolean done_something = GL_FALSE;
340 RunQueue *start = cr_server.run_queue;
341
342 /* check if this client's connection has gone away */
343 if (!cr_server.run_queue->client->conn
344 || (cr_server.run_queue->client->conn->type == CR_NO_CONNECTION
345 && crNetNumMessages(cr_server.run_queue->client->conn) == 0))
346 {
347 crServerDeleteClient( cr_server.run_queue->client );
348 start = cr_server.run_queue;
349 }
350
351 if (cr_server.run_queue == NULL) {
352 /* empty queue */
353 return NULL;
354 }
355
356 if (crServerClientInBeginEnd(cr_server.run_queue->client)) {
357 /* We _must_ service this client and no other.
358 * If we've got a message waiting on this client's connection we'll
359 * service it. Else, return NULL.
360 */
361 if (crNetNumMessages(cr_server.run_queue->client->conn) > 0)
362 return cr_server.run_queue;
363 else
364 return NULL;
365 }
366
367 /* loop over entries in run queue, looking for next one that's ready */
368 while (!done_something || cr_server.run_queue != start)
369 {
370 done_something = GL_TRUE;
371 if (!cr_server.run_queue->blocked)
372 {
373 all_blocked = GL_FALSE;
374 }
375 if (!cr_server.run_queue->blocked
376 && cr_server.run_queue->client->conn
377 && crNetNumMessages(cr_server.run_queue->client->conn) > 0)
378 {
379 /* OK, this client isn't blocked and has a queued message */
380 return cr_server.run_queue;
381 }
382 cr_server.run_queue = cr_server.run_queue->next;
383 }
384
385 if (all_blocked)
386 {
387 /* XXX crError is fatal? Should this be an info/warning msg? */
388 crError( "crserver: DEADLOCK! (numClients=%d, all blocked)",
389 cr_server.numClients );
390 if (cr_server.numClients < (int) cr_server.maxBarrierCount) {
391 crError("Waiting for more clients!!!");
392 while (cr_server.numClients < (int) cr_server.maxBarrierCount) {
393 crNetRecv();
394 }
395 }
396 }
397 }
398
399 if (!block)
400 return NULL;
401
402 /* no one had any work, get some! */
403 crNetRecv();
404
405 } /* while */
406
407 /* UNREACHED */
408 /* return NULL; */
409}
410
411
412/**
413 * This function takes the given message (which should be a buffer of
414 * rendering commands) and executes it.
415 */
416static void
417crServerDispatchMessage(CRConnection *conn, CRMessage *msg)
418{
419 const CRMessageOpcodes *msg_opcodes;
420 int opcodeBytes;
421 const char *data_ptr;
422#ifdef VBOX_WITH_CRHGSMI
423 PCRVBOXHGSMI_CMDDATA pCmdData = NULL;
424#endif
425
426 if (msg->header.type == CR_MESSAGE_REDIR_PTR)
427 {
428#ifdef VBOX_WITH_CRHGSMI
429 pCmdData = &msg->redirptr.CmdData;
430#endif
431 msg = (CRMessage *) msg->redirptr.pMessage;
432 }
433
434 CRASSERT(msg->header.type == CR_MESSAGE_OPCODES);
435
436 msg_opcodes = (const CRMessageOpcodes *) msg;
437 opcodeBytes = (msg_opcodes->numOpcodes + 3) & ~0x03;
438
439#ifdef VBOXCR_LOGFPS
440 CRASSERT(cr_server.curClient && cr_server.curClient->conn && cr_server.curClient->conn->id == msg->header.conn_id);
441 cr_server.curClient->conn->opcodes_count += msg_opcodes->numOpcodes;
442#endif
443
444 data_ptr = (const char *) msg_opcodes + sizeof(CRMessageOpcodes) + opcodeBytes;
445 crUnpack(data_ptr, /* first command's operands */
446 data_ptr - 1, /* first command's opcode */
447 msg_opcodes->numOpcodes, /* how many opcodes */
448 &(cr_server.dispatch)); /* the CR dispatch table */
449
450#ifdef VBOX_WITH_CRHGSMI
451 if (pCmdData)
452 {
453 int rc = VINF_SUCCESS;
454 CRVBOXHGSMI_CMDDATA_ASSERT_CONSISTENT(pCmdData);
455 if (CRVBOXHGSMI_CMDDATA_IS_SETWB(pCmdData))
456 {
457 uint32_t cbWriteback = pCmdData->cbWriteback;
458 rc = crVBoxServerInternalClientRead(conn->pClient, (uint8_t*)pCmdData->pWriteback, &cbWriteback);
459 Assert(rc == VINF_SUCCESS || rc == VERR_BUFFER_OVERFLOW);
460 *pCmdData->pcbWriteback = cbWriteback;
461 }
462 VBOXCRHGSMI_CMD_CHECK_COMPLETE(pCmdData, rc);
463 }
464#endif
465}
466
467
468typedef enum
469{
470 CLIENT_GONE = 1, /* the client has disconnected */
471 CLIENT_NEXT = 2, /* we can advance to next client */
472 CLIENT_MORE = 3 /* we need to keep servicing current client */
473} ClientStatus;
474
475
476/**
477 * Process incoming/pending message for the given client (queue entry).
478 * \return CLIENT_GONE if this client has gone away/exited,
479 * CLIENT_NEXT if we can advance to the next client
480 * CLIENT_MORE if we have to process more messages for this client.
481 */
482static ClientStatus
483crServerServiceClient(const RunQueue *qEntry)
484{
485 CRMessage *msg;
486 CRConnection *conn;
487
488 /* set current client pointer */
489 cr_server.curClient = qEntry->client;
490
491 conn = cr_server.run_queue->client->conn;
492
493 /* service current client as long as we can */
494 while (conn && conn->type != CR_NO_CONNECTION &&
495 crNetNumMessages(conn) > 0) {
496 unsigned int len;
497
498 /*
499 crDebug("%d messages on %p",
500 crNetNumMessages(conn), (void *) conn);
501 */
502
503 /* Don't use GetMessage, because we want to do our own crNetRecv() calls
504 * here ourself.
505 * Note that crNetPeekMessage() DOES remove the message from the queue
506 * if there is one.
507 */
508 len = crNetPeekMessage( conn, &msg );
509 CRASSERT(len > 0);
510 if (msg->header.type != CR_MESSAGE_OPCODES
511 && msg->header.type != CR_MESSAGE_REDIR_PTR) {
512 crError( "SPU %d sent me CRAP (type=0x%x)",
513 cr_server.curClient->spu_id, msg->header.type );
514 }
515
516 /* Do the context switch here. No sense in switching before we
517 * really have any work to process. This is a no-op if we're
518 * not really switching contexts.
519 *
520 * XXX This isn't entirely sound. The crStateMakeCurrent() call
521 * will compute the state difference and dispatch it using
522 * the head SPU's dispatch table.
523 *
524 * This is a problem if this is the first buffer coming in,
525 * and the head SPU hasn't had a chance to do a MakeCurrent()
526 * yet (likely because the MakeCurrent() command is in the
527 * buffer itself).
528 *
529 * At best, in this case, the functions are no-ops, and
530 * are essentially ignored by the SPU. In the typical
531 * case, things aren't too bad; if the SPU just calls
532 * crState*() functions to update local state, everything
533 * will work just fine.
534 *
535 * In the worst (but unusual) case where a nontrivial
536 * SPU is at the head of a crserver's SPU chain (say,
537 * in a multiple-tiered "tilesort" arrangement, as
538 * seen in the "multitilesort.conf" configuration), the
539 * SPU may rely on state set during the MakeCurrent() that
540 * may not be present yet, because no MakeCurrent() has
541 * yet been dispatched.
542 *
543 * This headache will have to be revisited in the future;
544 * for now, SPUs that could head a crserver's SPU chain
545 * will have to detect the case that their functions are
546 * being called outside of a MakeCurrent(), and will have
547 * to handle the situation gracefully. (This is currently
548 * the case with the "tilesort" SPU.)
549 */
550
551#if 0
552 crStateMakeCurrent( cr_server.curClient->currentCtx );
553#else
554 /* Check if the current window is the one that the client wants to
555 * draw into. If not, dispatch a MakeCurrent to activate the proper
556 * window.
557 */
558 if (cr_server.curClient) {
559 int clientWindow = cr_server.curClient->currentWindow;
560 int clientContext = cr_server.curClient->currentContextNumber;
561 CRContextInfo *clientCtxInfo = cr_server.curClient->currentCtxInfo;
562 if (clientCtxInfo != cr_server.currentCtxInfo
563 || clientWindow != cr_server.currentWindow
564 || cr_server.bForceMakeCurrentOnClientSwitch) {
565 crServerDispatchMakeCurrent(clientWindow, 0, clientContext);
566 /*
567 CRASSERT(cr_server.currentWindow == clientWindow);
568 */
569 }
570 }
571#endif
572
573 /* Force scissor, viewport and projection matrix update in
574 * crServerSetOutputBounds().
575 */
576 cr_server.currentSerialNo = 0;
577
578 /* Commands get dispatched here */
579 crServerDispatchMessage( conn, msg );
580
581 crNetFree( conn, msg );
582
583 if (qEntry->blocked) {
584 /* Note/assert: we should not be inside a glBegin/End or glNewList/
585 * glEndList pair at this time!
586 */
587 CRASSERT(0);
588 return CLIENT_NEXT;
589 }
590
591 } /* while */
592
593 /*
594 * Check if client/connection is gone
595 */
596 if (!conn || conn->type == CR_NO_CONNECTION) {
597 crDebug("Delete client %p at %d", cr_server.run_queue->client, __LINE__);
598 crServerDeleteClient( cr_server.run_queue->client );
599 return CLIENT_GONE;
600 }
601
602 /*
603 * Determine if we can advance to next client.
604 * If we're currently inside a glBegin/End primitive or building a display
605 * list we can't service another client until we're done with the
606 * primitive/list.
607 */
608 if (crServerClientInBeginEnd(cr_server.curClient)) {
609 /* The next message has to come from the current client's connection. */
610 CRASSERT(!qEntry->blocked);
611 return CLIENT_MORE;
612 }
613 else {
614 /* get next client */
615 return CLIENT_NEXT;
616 }
617}
618
619
620
621/**
622 * Check if any of the clients need servicing.
623 * If so, service one client and return.
624 * Else, just return.
625 */
626void
627crServerServiceClients(void)
628{
629 RunQueue *q;
630
631 q = getNextClient(GL_FALSE); /* don't block */
632 while (q)
633 {
634 ClientStatus stat = crServerServiceClient(q);
635 if (stat == CLIENT_NEXT && cr_server.run_queue->next) {
636 /* advance to next client */
637 cr_server.run_queue = cr_server.run_queue->next;
638 }
639 q = getNextClient(GL_FALSE);
640 }
641}
642
643
644
645
646/**
647 * Main crserver loop. Service connections from all connected clients.
648 * XXX add a config option to specify whether the crserver
649 * should exit when there's no more clients.
650 */
651void
652crServerSerializeRemoteStreams(void)
653{
654 /*MSG msg;*/
655
656 while (cr_server.run_queue)
657 {
658 crServerServiceClients();
659 /*if (PeekMessage( &msg, 0, 0, 0, PM_REMOVE ))
660 {
661 if (msg.message == WM_QUIT)
662 {
663 PostQuitMessage((int)msg.wParam);
664 break;
665 }
666 TranslateMessage( &msg );
667 DispatchMessage( &msg );
668 }*/
669 }
670}
671
672
673/**
674 * This will be called by the network layer when it's received a new message.
675 */
676int
677crServerRecv( CRConnection *conn, CRMessage *msg, unsigned int len )
678{
679 CRMessage *pRealMsg;
680 (void) len;
681
682 pRealMsg = (msg->header.type!=CR_MESSAGE_REDIR_PTR) ? msg : (CRMessage*) msg->redirptr.pMessage;
683
684 switch( pRealMsg->header.type )
685 {
686 /* Called when using multiple threads */
687 case CR_MESSAGE_NEWCLIENT:
688 crServerAddNewClient();
689 return 1; /* msg handled */
690 default:
691 /*crWarning( "Why is the crserver getting a message of type 0x%x?",
692 msg->header.type ); */
693 ;
694 }
695 return 0; /* not handled */
696}
Note: See TracBrowser for help on using the repository browser.

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