VirtualBox

source: vbox/trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_main.c@ 44746

Last change on this file since 44746 was 44746, checked in by vboxsync, 12 years ago

crOpenGL: saved state fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 80.6 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_net.h"
9#include "cr_unpack.h"
10#include "cr_error.h"
11#include "cr_glstate.h"
12#include "cr_string.h"
13#include "cr_mem.h"
14#include "cr_hash.h"
15#include "cr_vreg.h"
16#include "cr_environment.h"
17#include "cr_pixeldata.h"
18#include "server_dispatch.h"
19#include "state/cr_texture.h"
20#include "render/renderspu.h"
21#include <signal.h>
22#include <stdlib.h>
23#define DEBUG_FP_EXCEPTIONS 0
24#if DEBUG_FP_EXCEPTIONS
25#include <fpu_control.h>
26#include <math.h>
27#endif
28#include <iprt/assert.h>
29#include <VBox/err.h>
30
31#ifdef VBOXCR_LOGFPS
32#include <iprt/timer.h>
33#endif
34
35#ifdef VBOX_WITH_CRHGSMI
36# include <VBox/HostServices/VBoxCrOpenGLSvc.h>
37uint8_t* g_pvVRamBase = NULL;
38uint32_t g_cbVRam = 0;
39HCRHGSMICMDCOMPLETION g_hCrHgsmiCompletion = NULL;
40PFNCRHGSMICMDCOMPLETION g_pfnCrHgsmiCompletion = NULL;
41#endif
42
43/**
44 * \mainpage CrServerLib
45 *
46 * \section CrServerLibIntroduction Introduction
47 *
48 * Chromium consists of all the top-level files in the cr
49 * directory. The core module basically takes care of API dispatch,
50 * and OpenGL state management.
51 */
52
53
54/**
55 * CRServer global data
56 */
57CRServer cr_server;
58
59int tearingdown = 0; /* can't be static */
60
61DECLINLINE(int32_t) crVBoxServerClientGet(uint32_t u32ClientID, CRClient **ppClient)
62{
63 CRClient *pClient = NULL;
64 int32_t i;
65
66 *ppClient = NULL;
67
68 for (i = 0; i < cr_server.numClients; i++)
69 {
70 if (cr_server.clients[i] && cr_server.clients[i]->conn
71 && cr_server.clients[i]->conn->u32ClientID==u32ClientID)
72 {
73 pClient = cr_server.clients[i];
74 break;
75 }
76 }
77 if (!pClient)
78 {
79 crWarning("client not found!");
80 return VERR_INVALID_PARAMETER;
81 }
82
83 if (!pClient->conn->vMajor)
84 {
85 crWarning("no major version specified for client!");
86 return VERR_NOT_SUPPORTED;
87 }
88
89 *ppClient = pClient;
90
91 return VINF_SUCCESS;
92}
93
94
95/**
96 * Return pointer to server's first SPU.
97 */
98SPU*
99crServerHeadSPU(void)
100{
101 return cr_server.head_spu;
102}
103
104
105
106static void DeleteBarrierCallback( void *data )
107{
108 CRServerBarrier *barrier = (CRServerBarrier *) data;
109 crFree(barrier->waiting);
110 crFree(barrier);
111}
112
113
114static void deleteContextInfoCallback( void *data )
115{
116 CRContextInfo *c = (CRContextInfo *) data;
117 crStateDestroyContext(c->pContext);
118 if (c->CreateInfo.pszDpyName)
119 crFree(c->CreateInfo.pszDpyName);
120 crFree(c);
121}
122
123static void deleteMuralInfoCallback( void *data )
124{
125 CRMuralInfo *m = (CRMuralInfo *) data;
126 if (m->spuWindow) /* <- do not do term for default mural as it does not contain any info to be freed,
127 * and renderspu will destroy it up itself*/
128 {
129 crServerMuralTerm(m);
130 }
131 crFree(m);
132}
133
134static void crServerTearDown( void )
135{
136 GLint i;
137 CRClientNode *pNode, *pNext;
138
139 /* avoid a race condition */
140 if (tearingdown)
141 return;
142
143 tearingdown = 1;
144
145 crStateSetCurrent( NULL );
146
147 cr_server.curClient = NULL;
148 cr_server.run_queue = NULL;
149
150 crFree( cr_server.overlap_intens );
151 cr_server.overlap_intens = NULL;
152
153 /* needed to make sure window dummy mural not get created on mural destruction
154 * and generally this should be zeroed up */
155 cr_server.currentCtxInfo = NULL;
156 cr_server.currentWindow = 0;
157 cr_server.currentNativeWindow = 0;
158 cr_server.currentMural = NULL;
159
160 /* sync our state with renderspu,
161 * do it before mural & context deletion to avoid deleting currently set murals/contexts*/
162 cr_server.head_spu->dispatch_table.MakeCurrent(0, 0, 0);
163
164 /* Deallocate all semaphores */
165 crFreeHashtable(cr_server.semaphores, crFree);
166 cr_server.semaphores = NULL;
167
168 /* Deallocate all barriers */
169 crFreeHashtable(cr_server.barriers, DeleteBarrierCallback);
170 cr_server.barriers = NULL;
171
172 /* Free all context info */
173 crFreeHashtable(cr_server.contextTable, deleteContextInfoCallback);
174
175 /* Free vertex programs */
176 crFreeHashtable(cr_server.programTable, crFree);
177
178 /* Free dummy murals */
179 crFreeHashtable(cr_server.dummyMuralTable, deleteMuralInfoCallback);
180
181 /* Free murals */
182 crFreeHashtable(cr_server.muralTable, deleteMuralInfoCallback);
183
184 for (i = 0; i < cr_server.numClients; i++) {
185 if (cr_server.clients[i]) {
186 CRConnection *conn = cr_server.clients[i]->conn;
187 crNetFreeConnection(conn);
188 crFree(cr_server.clients[i]);
189 }
190 }
191 cr_server.numClients = 0;
192
193 pNode = cr_server.pCleanupClient;
194 while (pNode)
195 {
196 pNext=pNode->next;
197 crFree(pNode->pClient);
198 crFree(pNode);
199 pNode=pNext;
200 }
201 cr_server.pCleanupClient = NULL;
202
203#if 1
204 /* disable these two lines if trying to get stack traces with valgrind */
205 crSPUUnloadChain(cr_server.head_spu);
206 cr_server.head_spu = NULL;
207#endif
208
209 crStateDestroy();
210
211 crNetTearDown();
212
213 VBoxVrTerm();
214}
215
216static void crServerClose( unsigned int id )
217{
218 crError( "Client disconnected!" );
219 (void) id;
220}
221
222static void crServerCleanup( int sigio )
223{
224 crServerTearDown();
225
226 tearingdown = 0;
227}
228
229
230void
231crServerSetPort(int port)
232{
233 cr_server.tcpip_port = port;
234}
235
236
237
238static void
239crPrintHelp(void)
240{
241 printf("Usage: crserver [OPTIONS]\n");
242 printf("Options:\n");
243 printf(" -mothership URL Specifies URL for contacting the mothership.\n");
244 printf(" URL is of the form [protocol://]hostname[:port]\n");
245 printf(" -port N Specifies the port number this server will listen to.\n");
246 printf(" -help Prints this information.\n");
247}
248
249
250/**
251 * Do CRServer initializations. After this, we can begin servicing clients.
252 */
253void
254crServerInit(int argc, char *argv[])
255{
256 int i;
257 char *mothership = NULL;
258 CRMuralInfo *defaultMural;
259 int rc = VBoxVrInit();
260 if (!RT_SUCCESS(rc))
261 {
262 crWarning("VBoxVrInit failed, rc %d", rc);
263 return;
264 }
265
266 for (i = 1 ; i < argc ; i++)
267 {
268 if (!crStrcmp( argv[i], "-mothership" ))
269 {
270 if (i == argc - 1)
271 {
272 crError( "-mothership requires an argument" );
273 }
274 mothership = argv[i+1];
275 i++;
276 }
277 else if (!crStrcmp( argv[i], "-port" ))
278 {
279 /* This is the port on which we'll accept client connections */
280 if (i == argc - 1)
281 {
282 crError( "-port requires an argument" );
283 }
284 cr_server.tcpip_port = crStrToInt(argv[i+1]);
285 i++;
286 }
287 else if (!crStrcmp( argv[i], "-vncmode" ))
288 {
289 cr_server.vncMode = 1;
290 }
291 else if (!crStrcmp( argv[i], "-help" ))
292 {
293 crPrintHelp();
294 exit(0);
295 }
296 }
297
298 signal( SIGTERM, crServerCleanup );
299 signal( SIGINT, crServerCleanup );
300#ifndef WINDOWS
301 signal( SIGPIPE, SIG_IGN );
302#endif
303
304#if DEBUG_FP_EXCEPTIONS
305 {
306 fpu_control_t mask;
307 _FPU_GETCW(mask);
308 mask &= ~(_FPU_MASK_IM | _FPU_MASK_DM | _FPU_MASK_ZM
309 | _FPU_MASK_OM | _FPU_MASK_UM);
310 _FPU_SETCW(mask);
311 }
312#endif
313
314 cr_server.bUseMultipleContexts = (crGetenv( "CR_SERVER_ENABLE_MULTIPLE_CONTEXTS" ) != NULL);
315
316 if (cr_server.bUseMultipleContexts)
317 {
318 crInfo("Info: using multiple contexts!");
319 crDebug("Debug: using multiple contexts!");
320 }
321
322 cr_server.firstCallCreateContext = GL_TRUE;
323 cr_server.firstCallMakeCurrent = GL_TRUE;
324 cr_server.bForceMakeCurrentOnClientSwitch = GL_FALSE;
325
326 /*
327 * Create default mural info and hash table.
328 */
329 cr_server.muralTable = crAllocHashtable();
330 defaultMural = (CRMuralInfo *) crCalloc(sizeof(CRMuralInfo));
331 crHashtableAdd(cr_server.muralTable, 0, defaultMural);
332
333 cr_server.programTable = crAllocHashtable();
334
335 crNetInit(crServerRecv, crServerClose);
336 crStateInit();
337
338 crServerSetVBoxConfiguration();
339
340 crStateLimitsInit( &(cr_server.limits) );
341
342 /*
343 * Default context
344 */
345 cr_server.contextTable = crAllocHashtable();
346 cr_server.curClient->currentCtxInfo = &cr_server.MainContextInfo;
347
348 cr_server.dummyMuralTable = crAllocHashtable();
349
350 crServerInitDispatch();
351 crStateDiffAPI( &(cr_server.head_spu->dispatch_table) );
352
353 crUnpackSetReturnPointer( &(cr_server.return_ptr) );
354 crUnpackSetWritebackPointer( &(cr_server.writeback_ptr) );
355
356 cr_server.barriers = crAllocHashtable();
357 cr_server.semaphores = crAllocHashtable();
358}
359
360void crVBoxServerTearDown(void)
361{
362 crServerTearDown();
363}
364
365/**
366 * Do CRServer initializations. After this, we can begin servicing clients.
367 */
368GLboolean crVBoxServerInit(void)
369{
370 CRMuralInfo *defaultMural;
371
372 int rc = VBoxVrInit();
373 if (!RT_SUCCESS(rc))
374 {
375 crWarning("VBoxVrInit failed, rc %d", rc);
376 return GL_FALSE;
377 }
378
379#if DEBUG_FP_EXCEPTIONS
380 {
381 fpu_control_t mask;
382 _FPU_GETCW(mask);
383 mask &= ~(_FPU_MASK_IM | _FPU_MASK_DM | _FPU_MASK_ZM
384 | _FPU_MASK_OM | _FPU_MASK_UM);
385 _FPU_SETCW(mask);
386 }
387#endif
388
389 cr_server.bUseMultipleContexts = (crGetenv( "CR_SERVER_ENABLE_MULTIPLE_CONTEXTS" ) != NULL);
390
391 if (cr_server.bUseMultipleContexts)
392 {
393 crInfo("Info: using multiple contexts!");
394 crDebug("Debug: using multiple contexts!");
395 }
396
397 crNetInit(crServerRecv, crServerClose);
398
399 cr_server.firstCallCreateContext = GL_TRUE;
400 cr_server.firstCallMakeCurrent = GL_TRUE;
401
402 cr_server.bIsInLoadingState = GL_FALSE;
403 cr_server.bIsInSavingState = GL_FALSE;
404 cr_server.bForceMakeCurrentOnClientSwitch = GL_FALSE;
405
406 cr_server.pCleanupClient = NULL;
407
408 /*
409 * Create default mural info and hash table.
410 */
411 cr_server.muralTable = crAllocHashtable();
412 defaultMural = (CRMuralInfo *) crCalloc(sizeof(CRMuralInfo));
413 crHashtableAdd(cr_server.muralTable, 0, defaultMural);
414
415 cr_server.programTable = crAllocHashtable();
416
417 crStateInit();
418
419 crStateLimitsInit( &(cr_server.limits) );
420
421 cr_server.barriers = crAllocHashtable();
422 cr_server.semaphores = crAllocHashtable();
423
424 crUnpackSetReturnPointer( &(cr_server.return_ptr) );
425 crUnpackSetWritebackPointer( &(cr_server.writeback_ptr) );
426
427 /*
428 * Default context
429 */
430 cr_server.contextTable = crAllocHashtable();
431
432 cr_server.dummyMuralTable = crAllocHashtable();
433
434 crServerSetVBoxConfigurationHGCM();
435
436 if (!cr_server.head_spu)
437 return GL_FALSE;
438
439 crServerInitDispatch();
440 crStateDiffAPI( &(cr_server.head_spu->dispatch_table) );
441
442 /*Check for PBO support*/
443 if (crStateGetCurrent()->extensions.ARB_pixel_buffer_object)
444 {
445 cr_server.bUsePBOForReadback=GL_TRUE;
446 }
447
448 return GL_TRUE;
449}
450
451int32_t crVBoxServerAddClient(uint32_t u32ClientID)
452{
453 CRClient *newClient;
454
455 if (cr_server.numClients>=CR_MAX_CLIENTS)
456 {
457 return VERR_MAX_THRDS_REACHED;
458 }
459
460 newClient = (CRClient *) crCalloc(sizeof(CRClient));
461 crDebug("crServer: AddClient u32ClientID=%d", u32ClientID);
462
463 newClient->spu_id = 0;
464 newClient->currentCtxInfo = &cr_server.MainContextInfo;
465 newClient->currentContextNumber = -1;
466 newClient->conn = crNetAcceptClient(cr_server.protocol, NULL,
467 cr_server.tcpip_port,
468 cr_server.mtu, 0);
469 newClient->conn->u32ClientID = u32ClientID;
470
471 cr_server.clients[cr_server.numClients++] = newClient;
472
473 crServerAddToRunQueue(newClient);
474
475 return VINF_SUCCESS;
476}
477
478void crVBoxServerRemoveClient(uint32_t u32ClientID)
479{
480 CRClient *pClient=NULL;
481 int32_t i;
482
483 crDebug("crServer: RemoveClient u32ClientID=%d", u32ClientID);
484
485 for (i = 0; i < cr_server.numClients; i++)
486 {
487 if (cr_server.clients[i] && cr_server.clients[i]->conn
488 && cr_server.clients[i]->conn->u32ClientID==u32ClientID)
489 {
490 pClient = cr_server.clients[i];
491 break;
492 }
493 }
494 //if (!pClient) return VERR_INVALID_PARAMETER;
495 if (!pClient)
496 {
497 crWarning("Invalid client id %u passed to crVBoxServerRemoveClient", u32ClientID);
498 return;
499 }
500
501#ifdef VBOX_WITH_CRHGSMI
502 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
503#endif
504
505 /* Disconnect the client */
506 pClient->conn->Disconnect(pClient->conn);
507
508 /* Let server clear client from the queue */
509 crServerDeleteClient(pClient);
510}
511
512static int32_t crVBoxServerInternalClientWriteRead(CRClient *pClient)
513{
514#ifdef VBOXCR_LOGFPS
515 uint64_t tstart, tend;
516#endif
517
518 /*crDebug("=>crServer: ClientWrite u32ClientID=%d", u32ClientID);*/
519
520
521#ifdef VBOXCR_LOGFPS
522 tstart = RTTimeNanoTS();
523#endif
524
525 /* This should be setup already */
526 CRASSERT(pClient->conn->pBuffer);
527 CRASSERT(pClient->conn->cbBuffer);
528#ifdef VBOX_WITH_CRHGSMI
529 CRVBOXHGSMI_CMDDATA_ASSERT_CONSISTENT(&pClient->conn->CmdData);
530#endif
531
532 if (
533#ifdef VBOX_WITH_CRHGSMI
534 !CRVBOXHGSMI_CMDDATA_IS_SET(&pClient->conn->CmdData) &&
535#endif
536 cr_server.run_queue->client != pClient
537 && crServerClientInBeginEnd(cr_server.run_queue->client))
538 {
539 crDebug("crServer: client %d blocked, allow_redir_ptr = 0", pClient->conn->u32ClientID);
540 pClient->conn->allow_redir_ptr = 0;
541 }
542 else
543 {
544 pClient->conn->allow_redir_ptr = 1;
545 }
546
547 crNetRecv();
548 CRASSERT(pClient->conn->pBuffer==NULL && pClient->conn->cbBuffer==0);
549 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
550
551 crServerServiceClients();
552
553#if 0
554 if (pClient->currentMural) {
555 crStateViewport( 0, 0, 500, 500 );
556 pClient->currentMural->viewportValidated = GL_FALSE;
557 cr_server.head_spu->dispatch_table.Viewport( 0, 0, 500, 500 );
558 crStateViewport( 0, 0, 600, 600 );
559 pClient->currentMural->viewportValidated = GL_FALSE;
560 cr_server.head_spu->dispatch_table.Viewport( 0, 0, 600, 600 );
561
562 crStateMatrixMode(GL_PROJECTION);
563 cr_server.head_spu->dispatch_table.MatrixMode(GL_PROJECTION);
564 crServerDispatchLoadIdentity();
565 crStateFrustum(-0.6, 0.6, -0.5, 0.5, 1.5, 150.0);
566 cr_server.head_spu->dispatch_table.Frustum(-0.6, 0.6, -0.5, 0.5, 1.5, 150.0);
567 crServerDispatchLoadIdentity();
568 crStateFrustum(-0.5, 0.5, -0.5, 0.5, 1.5, 150.0);
569 cr_server.head_spu->dispatch_table.Frustum(-0.5, 0.5, -0.5, 0.5, 1.5, 150.0);
570
571 crStateMatrixMode(GL_MODELVIEW);
572 cr_server.head_spu->dispatch_table.MatrixMode(GL_MODELVIEW);
573 crServerDispatchLoadIdentity();
574 crStateFrustum(-0.5, 0.5, -0.5, 0.5, 1.5, 150.0);
575 cr_server.head_spu->dispatch_table.Frustum(-0.5, 0.5, -0.5, 0.5, 1.5, 150.0);
576 crServerDispatchLoadIdentity();
577 }
578#endif
579
580 crStateResetCurrentPointers(&cr_server.current);
581
582#ifndef VBOX_WITH_CRHGSMI
583 CRASSERT(!pClient->conn->allow_redir_ptr || crNetNumMessages(pClient->conn)==0);
584#endif
585
586#ifdef VBOXCR_LOGFPS
587 tend = RTTimeNanoTS();
588 pClient->timeUsed += tend-tstart;
589#endif
590 /*crDebug("<=crServer: ClientWrite u32ClientID=%d", u32ClientID);*/
591
592 return VINF_SUCCESS;
593}
594
595
596int32_t crVBoxServerClientWrite(uint32_t u32ClientID, uint8_t *pBuffer, uint32_t cbBuffer)
597{
598 CRClient *pClient=NULL;
599 int32_t rc = crVBoxServerClientGet(u32ClientID, &pClient);
600
601 if (RT_FAILURE(rc))
602 return rc;
603
604
605 CRASSERT(pBuffer);
606
607 /* This should never fire unless we start to multithread */
608 CRASSERT(pClient->conn->pBuffer==NULL && pClient->conn->cbBuffer==0);
609
610 pClient->conn->pBuffer = pBuffer;
611 pClient->conn->cbBuffer = cbBuffer;
612#ifdef VBOX_WITH_CRHGSMI
613 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
614#endif
615
616 return crVBoxServerInternalClientWriteRead(pClient);
617}
618
619int32_t crVBoxServerInternalClientRead(CRClient *pClient, uint8_t *pBuffer, uint32_t *pcbBuffer)
620{
621 if (pClient->conn->cbHostBuffer > *pcbBuffer)
622 {
623 crDebug("crServer: [%lx] ClientRead u32ClientID=%d FAIL, host buffer too small %d of %d",
624 crThreadID(), pClient->conn->u32ClientID, *pcbBuffer, pClient->conn->cbHostBuffer);
625
626 /* Return the size of needed buffer */
627 *pcbBuffer = pClient->conn->cbHostBuffer;
628
629 return VERR_BUFFER_OVERFLOW;
630 }
631
632 *pcbBuffer = pClient->conn->cbHostBuffer;
633
634 if (*pcbBuffer)
635 {
636 CRASSERT(pClient->conn->pHostBuffer);
637
638 crMemcpy(pBuffer, pClient->conn->pHostBuffer, *pcbBuffer);
639 pClient->conn->cbHostBuffer = 0;
640 }
641
642 return VINF_SUCCESS;
643}
644
645int32_t crVBoxServerClientRead(uint32_t u32ClientID, uint8_t *pBuffer, uint32_t *pcbBuffer)
646{
647 CRClient *pClient=NULL;
648 int32_t rc = crVBoxServerClientGet(u32ClientID, &pClient);
649
650 if (RT_FAILURE(rc))
651 return rc;
652
653#ifdef VBOX_WITH_CRHGSMI
654 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
655#endif
656
657 return crVBoxServerInternalClientRead(pClient, pBuffer, pcbBuffer);
658}
659
660int32_t crVBoxServerClientSetVersion(uint32_t u32ClientID, uint32_t vMajor, uint32_t vMinor)
661{
662 CRClient *pClient=NULL;
663 int32_t i;
664
665 for (i = 0; i < cr_server.numClients; i++)
666 {
667 if (cr_server.clients[i] && cr_server.clients[i]->conn
668 && cr_server.clients[i]->conn->u32ClientID==u32ClientID)
669 {
670 pClient = cr_server.clients[i];
671 break;
672 }
673 }
674 if (!pClient) return VERR_INVALID_PARAMETER;
675
676 pClient->conn->vMajor = vMajor;
677 pClient->conn->vMinor = vMinor;
678
679 if (vMajor != CR_PROTOCOL_VERSION_MAJOR
680 || vMinor != CR_PROTOCOL_VERSION_MINOR)
681 {
682 return VERR_NOT_SUPPORTED;
683 }
684 else return VINF_SUCCESS;
685}
686
687int32_t crVBoxServerClientSetPID(uint32_t u32ClientID, uint64_t pid)
688{
689 CRClient *pClient=NULL;
690 int32_t i;
691
692 for (i = 0; i < cr_server.numClients; i++)
693 {
694 if (cr_server.clients[i] && cr_server.clients[i]->conn
695 && cr_server.clients[i]->conn->u32ClientID==u32ClientID)
696 {
697 pClient = cr_server.clients[i];
698 break;
699 }
700 }
701 if (!pClient) return VERR_INVALID_PARAMETER;
702
703 pClient->pid = pid;
704
705 return VINF_SUCCESS;
706}
707
708int
709CRServerMain(int argc, char *argv[])
710{
711 crServerInit(argc, argv);
712
713 crServerSerializeRemoteStreams();
714
715 crServerTearDown();
716
717 tearingdown = 0;
718
719 return 0;
720}
721
722static void crVBoxServerSaveMuralCB(unsigned long key, void *data1, void *data2)
723{
724 CRMuralInfo *pMI = (CRMuralInfo*) data1;
725 PSSMHANDLE pSSM = (PSSMHANDLE) data2;
726 int32_t rc;
727
728 CRASSERT(pMI && pSSM);
729
730 /* Don't store default mural */
731 if (!key) return;
732
733 rc = SSMR3PutMem(pSSM, &key, sizeof(key));
734 CRASSERT(rc == VINF_SUCCESS);
735
736 rc = SSMR3PutMem(pSSM, pMI, RT_OFFSETOF(CRMuralInfo, CreateInfo));
737 CRASSERT(rc == VINF_SUCCESS);
738
739 if (pMI->pVisibleRects)
740 {
741 rc = SSMR3PutMem(pSSM, pMI->pVisibleRects, 4*sizeof(GLint)*pMI->cVisibleRects);
742 }
743
744 rc = SSMR3PutMem(pSSM, pMI->ctxUsage, sizeof (pMI->ctxUsage));
745 CRASSERT(rc == VINF_SUCCESS);
746}
747
748/* @todo add hashtable walker with result info and intermediate abort */
749static void crVBoxServerSaveCreateInfoCB(unsigned long key, void *data1, void *data2)
750{
751 CRCreateInfo_t *pCreateInfo = (CRCreateInfo_t *)data1;
752 PSSMHANDLE pSSM = (PSSMHANDLE) data2;
753 int32_t rc;
754
755 CRASSERT(pCreateInfo && pSSM);
756
757 /* Don't store default mural create info */
758 if (!key) return;
759
760 rc = SSMR3PutMem(pSSM, &key, sizeof(key));
761 CRASSERT(rc == VINF_SUCCESS);
762
763 rc = SSMR3PutMem(pSSM, pCreateInfo, sizeof(*pCreateInfo));
764 CRASSERT(rc == VINF_SUCCESS);
765
766 if (pCreateInfo->pszDpyName)
767 {
768 rc = SSMR3PutStrZ(pSSM, pCreateInfo->pszDpyName);
769 CRASSERT(rc == VINF_SUCCESS);
770 }
771}
772
773static void crVBoxServerSaveCreateInfoFromMuralInfoCB(unsigned long key, void *data1, void *data2)
774{
775 CRMuralInfo *pMural = (CRMuralInfo *)data1;
776 CRCreateInfo_t *pCreateInfo = &pMural->CreateInfo;
777 crVBoxServerSaveCreateInfoCB(key, pCreateInfo, data2);
778}
779
780static void crVBoxServerSaveCreateInfoFromCtxInfoCB(unsigned long key, void *data1, void *data2)
781{
782 CRContextInfo *pContextInfo = (CRContextInfo *)data1;
783 CRCreateInfo_t CreateInfo = pContextInfo->CreateInfo;
784 /* saved state contains internal id */
785 CreateInfo.externalID = pContextInfo->pContext->id;
786 crVBoxServerSaveCreateInfoCB(key, &CreateInfo, data2);
787}
788
789static void crVBoxServerSyncTextureCB(unsigned long key, void *data1, void *data2)
790{
791 CRTextureObj *pTexture = (CRTextureObj *) data1;
792 CRContext *pContext = (CRContext *) data2;
793
794 CRASSERT(pTexture && pContext);
795 crStateTextureObjectDiff(pContext, NULL, NULL, pTexture, GL_TRUE);
796}
797
798typedef struct CRVBOX_SAVE_STATE_GLOBAL
799{
800 /* context id -> mural association
801 * on context data save, each context will be made current with the corresponding mural from this table
802 * thus saving the mural front & back buffer data */
803 CRHashTable *contextMuralTable;
804 /* mural id -> context info
805 * for murals that do not have associated context in contextMuralTable
806 * we still need to save*/
807 CRHashTable *additionalMuralContextTable;
808
809 PSSMHANDLE pSSM;
810
811 int rc;
812} CRVBOX_SAVE_STATE_GLOBAL, *PCRVBOX_SAVE_STATE_GLOBAL;
813
814
815typedef struct CRVBOX_CTXWND_CTXWALKER_CB
816{
817 PCRVBOX_SAVE_STATE_GLOBAL pGlobal;
818 CRHashTable *usedMuralTable;
819 GLuint cAdditionalMurals;
820} CRVBOX_CTXWND_CTXWALKER_CB, *PCRVBOX_CTXWND_CTXWALKER_CB;
821
822static void crVBoxServerBuildAdditionalWindowContextMapCB(unsigned long key, void *data1, void *data2)
823{
824 CRMuralInfo * pMural = (CRMuralInfo *) data1;
825 PCRVBOX_CTXWND_CTXWALKER_CB pData = (PCRVBOX_CTXWND_CTXWALKER_CB)data2;
826 CRContextInfo *pContextInfo = NULL;
827
828 if (!pMural->CreateInfo.externalID)
829 return;
830
831 if (crHashtableSearch(pData->usedMuralTable, pMural->CreateInfo.externalID))
832 {
833 Assert(crHashtableGetDataKey(pData->pGlobal->contextMuralTable, pMural, NULL));
834 return;
835 }
836
837 Assert(!crHashtableGetDataKey(pData->pGlobal->contextMuralTable, pMural, NULL));
838
839 if (cr_server.MainContextInfo.CreateInfo.visualBits == pMural->CreateInfo.visualBits)
840 {
841 pContextInfo = &cr_server.MainContextInfo;
842 }
843 else
844 {
845 crWarning("different visual bits not implemented!");
846 pContextInfo = &cr_server.MainContextInfo;
847 }
848
849 crHashtableAdd(pData->pGlobal->additionalMuralContextTable, pMural->CreateInfo.externalID, pContextInfo);
850}
851
852
853typedef struct CRVBOX_CTXWND_WNDWALKER_CB
854{
855 PCRVBOX_SAVE_STATE_GLOBAL pGlobal;
856 CRHashTable *usedMuralTable;
857 CRContextInfo *pContextInfo;
858 CRMuralInfo * pMural;
859} CRVBOX_CTXWND_WNDWALKER_CB, *PCRVBOX_CTXWND_WNDWALKER_CB;
860
861static void crVBoxServerBuildContextWindowMapWindowWalkerCB(unsigned long key, void *data1, void *data2)
862{
863 CRMuralInfo * pMural = (CRMuralInfo *) data1;
864 PCRVBOX_CTXWND_WNDWALKER_CB pData = (PCRVBOX_CTXWND_WNDWALKER_CB)data2;
865
866 Assert(pData->pMural != pMural);
867 Assert(pData->pContextInfo);
868
869 if (pData->pMural)
870 return;
871
872 if (!pMural->CreateInfo.externalID)
873 return;
874
875 if (!CR_STATE_SHAREDOBJ_USAGE_IS_SET(pMural, pData->pContextInfo->pContext))
876 return;
877
878 if (crHashtableSearch(pData->usedMuralTable, pMural->CreateInfo.externalID))
879 return;
880
881 CRASSERT(pMural->CreateInfo.visualBits == pData->pContextInfo->CreateInfo.visualBits);
882 pData->pMural = pMural;
883}
884
885static void crVBoxServerBuildContextUsedWindowMapCB(unsigned long key, void *data1, void *data2)
886{
887 CRContextInfo *pContextInfo = (CRContextInfo *)data1;
888 PCRVBOX_CTXWND_CTXWALKER_CB pData = (PCRVBOX_CTXWND_CTXWALKER_CB)data2;
889
890 if (!pContextInfo->currentMural)
891 return;
892
893 crHashtableAdd(pData->pGlobal->contextMuralTable, pContextInfo->CreateInfo.externalID, pContextInfo->currentMural);
894 crHashtableAdd(pData->usedMuralTable, pContextInfo->currentMural->CreateInfo.externalID, pContextInfo->currentMural);
895}
896
897CRMuralInfo * crServerGetDummyMural(GLint visualBits)
898{
899 CRMuralInfo * pMural = (CRMuralInfo *)crHashtableSearch(cr_server.dummyMuralTable, visualBits);
900 if (!pMural)
901 {
902 GLint id;
903 pMural = (CRMuralInfo *) crCalloc(sizeof(CRMuralInfo));
904 if (!pMural)
905 {
906 crWarning("crCalloc failed!");
907 return NULL;
908 }
909 id = crServerMuralInit(pMural, "", visualBits, -1);
910 if (id < 0)
911 {
912 crWarning("crServerMuralInit failed!");
913 crFree(pMural);
914 return NULL;
915 }
916
917 crHashtableAdd(cr_server.dummyMuralTable, visualBits, pMural);
918 }
919
920 return pMural;
921}
922
923static void crVBoxServerBuildContextUnusedWindowMapCB(unsigned long key, void *data1, void *data2)
924{
925 CRContextInfo *pContextInfo = (CRContextInfo *)data1;
926 PCRVBOX_CTXWND_CTXWALKER_CB pData = (PCRVBOX_CTXWND_CTXWALKER_CB)data2;
927 CRMuralInfo * pMural = NULL;
928
929 if (pContextInfo->currentMural)
930 return;
931
932 Assert(crHashtableNumElements(pData->pGlobal->contextMuralTable) <= crHashtableNumElements(cr_server.muralTable) - 1);
933 if (crHashtableNumElements(pData->pGlobal->contextMuralTable) < crHashtableNumElements(cr_server.muralTable) - 1)
934 {
935 CRVBOX_CTXWND_WNDWALKER_CB MuralData;
936 MuralData.pGlobal = pData->pGlobal;
937 MuralData.usedMuralTable = pData->usedMuralTable;
938 MuralData.pContextInfo = pContextInfo;
939 MuralData.pMural = NULL;
940
941 crHashtableWalk(cr_server.muralTable, crVBoxServerBuildContextWindowMapWindowWalkerCB, &MuralData);
942
943 pMural = MuralData.pMural;
944
945 }
946
947 if (!pMural)
948 {
949 pMural = crServerGetDummyMural(pContextInfo->CreateInfo.visualBits);
950 if (!pMural)
951 {
952 crWarning("crServerGetDummyMural failed");
953 return;
954 }
955 }
956 else
957 {
958 crHashtableAdd(pData->usedMuralTable, pMural->CreateInfo.externalID, pMural);
959 ++pData->cAdditionalMurals;
960 }
961
962 crHashtableAdd(pData->pGlobal->contextMuralTable, pContextInfo->CreateInfo.externalID, pMural);
963}
964
965static void crVBoxServerBuildSaveStateGlobal(PCRVBOX_SAVE_STATE_GLOBAL pGlobal)
966{
967 CRVBOX_CTXWND_CTXWALKER_CB Data;
968 GLuint cMurals;
969 pGlobal->contextMuralTable = crAllocHashtable();
970 pGlobal->additionalMuralContextTable = crAllocHashtable();
971 /* 1. go through all contexts and match all having currentMural set */
972 Data.pGlobal = pGlobal;
973 Data.usedMuralTable = crAllocHashtable();
974 Data.cAdditionalMurals = 0;
975 crHashtableWalk(cr_server.contextTable, crVBoxServerBuildContextUsedWindowMapCB, &Data);
976
977 cMurals = crHashtableNumElements(pGlobal->contextMuralTable);
978 CRASSERT(cMurals <= crHashtableNumElements(cr_server.contextTable));
979 CRASSERT(cMurals <= crHashtableNumElements(cr_server.muralTable) - 1);
980 CRASSERT(cMurals == crHashtableNumElements(Data.usedMuralTable));
981 if (cMurals < crHashtableNumElements(cr_server.contextTable))
982 {
983 Data.cAdditionalMurals = 0;
984 crHashtableWalk(cr_server.contextTable, crVBoxServerBuildContextUnusedWindowMapCB, &Data);
985 }
986
987 CRASSERT(crHashtableNumElements(pGlobal->contextMuralTable) == crHashtableNumElements(cr_server.contextTable));
988 CRASSERT(cMurals + Data.cAdditionalMurals <= crHashtableNumElements(cr_server.muralTable) - 1);
989 if (cMurals + Data.cAdditionalMurals < crHashtableNumElements(cr_server.muralTable) - 1)
990 {
991 crHashtableWalk(cr_server.muralTable, crVBoxServerBuildAdditionalWindowContextMapCB, &Data);
992 CRASSERT(cMurals + Data.cAdditionalMurals + crHashtableNumElements(pGlobal->additionalMuralContextTable) == crHashtableNumElements(cr_server.muralTable) - 1);
993 }
994
995 crFreeHashtable(Data.usedMuralTable, NULL);
996}
997
998static int crVBoxServerSaveFBImage(PSSMHANDLE pSSM)
999{
1000 int32_t rc;
1001 CRContext *pContext;
1002 CRMuralInfo *pMural;
1003 GLint cbData;
1004
1005 CRASSERT(cr_server.currentCtxInfo);
1006 CRASSERT(cr_server.currentCtxInfo->currentMural);
1007
1008 pContext = cr_server.currentCtxInfo->pContext;
1009 pMural = cr_server.currentCtxInfo->currentMural;
1010 /* do crStateAcquireFBImage no matter whether offscreen drawing is used or not
1011 * in the former case this would just free pContext->buffer.pFrontImg and pContext->buffer.pFrontImg
1012 */
1013 rc = crStateAcquireFBImage(pContext);
1014 AssertRCReturn(rc, rc);
1015
1016 if (!pMural->width || !pMural->height)
1017 return VINF_SUCCESS;
1018
1019
1020 cbData = crPixelSize(GL_RGBA, GL_UNSIGNED_BYTE) * pMural->width * pMural->height;
1021
1022 if (!pMural->fUseFBO)
1023 {
1024 CRASSERT(pMural->width == pContext->buffer.storedWidth);
1025 CRASSERT(pMural->width == pContext->buffer.width);
1026 CRASSERT(pMural->height == pContext->buffer.storedHeight);
1027 CRASSERT(pMural->height == pContext->buffer.height);
1028
1029 rc = SSMR3PutMem(pSSM, pContext->buffer.pFrontImg, cbData);
1030 AssertRCReturn(rc, rc);
1031 rc = SSMR3PutMem(pSSM, pContext->buffer.pBackImg, cbData);
1032 AssertRCReturn(rc, rc);
1033
1034 crStateFreeFBImage(pContext);
1035 }
1036 else
1037 {
1038 VBOXVR_TEXTURE Tex;
1039 void *pvData;
1040 GLuint idPBO = cr_server.bUsePBOForReadback ? pMural->idPBO : 0;
1041
1042 CRASSERT(!pContext->buffer.width);
1043 CRASSERT(!pContext->buffer.height);
1044
1045 if (idPBO)
1046 {
1047 CRASSERT(pMural->fboWidth == pMural->width);
1048 CRASSERT(pMural->fboHeight == pMural->height);
1049 }
1050
1051 Tex.width = pMural->width;
1052 Tex.height = pMural->height;
1053 Tex.target = GL_TEXTURE_2D;
1054 Tex.hwid = pMural->aidColorTexs[CR_SERVER_FBO_FB_IDX(pMural)];
1055
1056 CRASSERT(Tex.hwid);
1057
1058 pvData = CrHlpGetTexImage(pContext, &Tex, idPBO);
1059 if (!pvData)
1060 {
1061 crWarning("CrHlpGetTexImage failed for frontbuffer");
1062 return VERR_NO_MEMORY;
1063 }
1064
1065 rc = SSMR3PutMem(pSSM, pvData, cbData);
1066
1067 CrHlpFreeTexImage(pContext, idPBO, pvData);
1068
1069 AssertRCReturn(rc, rc);
1070
1071 Tex.hwid = pMural->aidColorTexs[CR_SERVER_FBO_BB_IDX(pMural)];
1072
1073 CRASSERT(Tex.hwid);
1074
1075 pvData = CrHlpGetTexImage(pContext, &Tex, idPBO);
1076 if (!pvData)
1077 {
1078 crWarning("CrHlpGetTexImage failed for backbuffer");
1079 return VERR_NO_MEMORY;
1080 }
1081
1082 rc = SSMR3PutMem(pSSM, pvData, cbData);
1083
1084 CrHlpFreeTexImage(pContext, idPBO, pvData);
1085
1086 AssertRCReturn(rc, rc);
1087 }
1088
1089 return VINF_SUCCESS;
1090}
1091
1092#define CRSERVER_ASSERTRC_RETURN_VOID(_rc) do { \
1093 if(!RT_SUCCESS((_rc))) { \
1094 AssertFailed(); \
1095 return; \
1096 } \
1097 } while (0)
1098
1099static void crVBoxServerSaveAdditionalMuralsCB(unsigned long key, void *data1, void *data2)
1100{
1101 CRContextInfo *pContextInfo = (CRContextInfo *) data1;
1102 PCRVBOX_SAVE_STATE_GLOBAL pData = (PCRVBOX_SAVE_STATE_GLOBAL)data2;
1103 CRMuralInfo *pMural = (CRMuralInfo*)crHashtableSearch(cr_server.muralTable, key);
1104 PSSMHANDLE pSSM = pData->pSSM;
1105 CRbitvalue initialCtxUsage[CR_MAX_BITARRAY];
1106 CRMuralInfo *pInitialCurMural = pContextInfo->currentMural;
1107
1108 crMemcpy(initialCtxUsage, pMural->ctxUsage, sizeof (initialCtxUsage));
1109
1110 CRSERVER_ASSERTRC_RETURN_VOID(pData->rc);
1111
1112 pData->rc = SSMR3PutMem(pSSM, &key, sizeof(key));
1113 CRSERVER_ASSERTRC_RETURN_VOID(pData->rc);
1114
1115 pData->rc = SSMR3PutMem(pSSM, &pContextInfo->CreateInfo.externalID, sizeof(pContextInfo->CreateInfo.externalID));
1116 CRSERVER_ASSERTRC_RETURN_VOID(pData->rc);
1117
1118 crServerPerformMakeCurrent(pMural, pContextInfo);
1119
1120 pData->rc = crVBoxServerSaveFBImage(pSSM);
1121
1122 /* restore the reference data, we synchronize it with the HW state in a later crServerPerformMakeCurrent call */
1123 crMemcpy(pMural->ctxUsage, initialCtxUsage, sizeof (initialCtxUsage));
1124 pContextInfo->currentMural = pInitialCurMural;
1125
1126 CRSERVER_ASSERTRC_RETURN_VOID(pData->rc);
1127}
1128
1129static void crVBoxServerSaveContextStateCB(unsigned long key, void *data1, void *data2)
1130{
1131 CRContextInfo *pContextInfo = (CRContextInfo *) data1;
1132 CRContext *pContext = pContextInfo->pContext;
1133 PCRVBOX_SAVE_STATE_GLOBAL pData = (PCRVBOX_SAVE_STATE_GLOBAL)data2;
1134 PSSMHANDLE pSSM = pData->pSSM;
1135 CRMuralInfo *pMural = (CRMuralInfo*)crHashtableSearch(pData->contextMuralTable, key);
1136 CRMuralInfo *pContextCurrentMural = pContextInfo->currentMural;
1137 const int32_t i32Dummy = 0;
1138
1139 AssertCompile(sizeof (i32Dummy) == sizeof (pMural->CreateInfo.externalID));
1140 CRSERVER_ASSERTRC_RETURN_VOID(pData->rc);
1141
1142 CRASSERT(pContext && pSSM);
1143 CRASSERT(pMural);
1144 CRASSERT(pMural->CreateInfo.externalID);
1145
1146 /* We could have skipped saving the key and use similar callback to load context states back,
1147 * but there's no guarantee we'd traverse hashtable in same order after loading.
1148 */
1149 pData->rc = SSMR3PutMem(pSSM, &key, sizeof(key));
1150 CRSERVER_ASSERTRC_RETURN_VOID(pData->rc);
1151
1152#ifdef DEBUG_misha
1153 {
1154 unsigned long id;
1155 if (!crHashtableGetDataKey(cr_server.contextTable, pContextInfo, &id))
1156 crWarning("No client id for server ctx %d", pContextInfo->CreateInfo.externalID);
1157 else
1158 CRASSERT(id == key);
1159 }
1160#endif
1161
1162#ifdef CR_STATE_NO_TEXTURE_IMAGE_STORE
1163 if (pContextInfo->currentMural
1164 || crHashtableSearch(cr_server.muralTable, pMural->CreateInfo.externalID) /* <- this is not a dummy mural */
1165 )
1166 {
1167 CRASSERT(pMural->CreateInfo.externalID);
1168 CRASSERT(!crHashtableSearch(cr_server.dummyMuralTable, pMural->CreateInfo.externalID));
1169 pData->rc = SSMR3PutMem(pSSM, &pMural->CreateInfo.externalID, sizeof(pMural->CreateInfo.externalID));
1170 }
1171 else
1172 {
1173 /* this is a dummy mural */
1174 CRASSERT(!pMural->width);
1175 CRASSERT(!pMural->height);
1176 CRASSERT(crHashtableSearch(cr_server.dummyMuralTable, pMural->CreateInfo.externalID));
1177 pData->rc = SSMR3PutMem(pSSM, &i32Dummy, sizeof(pMural->CreateInfo.externalID));
1178 }
1179 CRSERVER_ASSERTRC_RETURN_VOID(pData->rc);
1180
1181 CRASSERT(CR_STATE_SHAREDOBJ_USAGE_IS_SET(pMural, pContext));
1182 CRASSERT(pContextInfo->currentMural == pMural || !pContextInfo->currentMural);
1183 CRASSERT(cr_server.curClient);
1184
1185 crServerPerformMakeCurrent(pMural, pContextInfo);
1186#endif
1187
1188 pData->rc = crStateSaveContext(pContext, pSSM);
1189 CRSERVER_ASSERTRC_RETURN_VOID(pData->rc);
1190
1191 pData->rc = crVBoxServerSaveFBImage(pSSM);
1192 CRSERVER_ASSERTRC_RETURN_VOID(pData->rc);
1193
1194 /* restore the initial current mural */
1195 pContextInfo->currentMural = pContextCurrentMural;
1196}
1197
1198#if 0
1199typedef struct CR_SERVER_CHECK_BUFFERS
1200{
1201 CRBufferObject *obj;
1202 CRContext *ctx;
1203}CR_SERVER_CHECK_BUFFERS, *PCR_SERVER_CHECK_BUFFERS;
1204
1205static void crVBoxServerCheckConsistencyContextBuffersCB(unsigned long key, void *data1, void *data2)
1206{
1207 CRContextInfo* pContextInfo = (CRContextInfo*)data1;
1208 CRContext *ctx = pContextInfo->pContext;
1209 PCR_SERVER_CHECK_BUFFERS pBuffers = (PCR_SERVER_CHECK_BUFFERS)data2;
1210 CRBufferObject *obj = pBuffers->obj;
1211 CRBufferObjectState *b = &(ctx->bufferobject);
1212 int j, k;
1213
1214 if (obj == b->arrayBuffer)
1215 {
1216 Assert(!pBuffers->ctx || pBuffers->ctx == ctx);
1217 pBuffers->ctx = ctx;
1218 }
1219 if (obj == b->elementsBuffer)
1220 {
1221 Assert(!pBuffers->ctx || pBuffers->ctx == ctx);
1222 pBuffers->ctx = ctx;
1223 }
1224#ifdef CR_ARB_pixel_buffer_object
1225 if (obj == b->packBuffer)
1226 {
1227 Assert(!pBuffers->ctx || pBuffers->ctx == ctx);
1228 pBuffers->ctx = ctx;
1229 }
1230 if (obj == b->unpackBuffer)
1231 {
1232 Assert(!pBuffers->ctx || pBuffers->ctx == ctx);
1233 pBuffers->ctx = ctx;
1234 }
1235#endif
1236
1237#ifdef CR_ARB_vertex_buffer_object
1238 for (j=0; j<CRSTATECLIENT_MAX_VERTEXARRAYS; ++j)
1239 {
1240 CRClientPointer *cp = crStateGetClientPointerByIndex(j, &ctx->client.array);
1241 if (obj == cp->buffer)
1242 {
1243 Assert(!pBuffers->ctx || pBuffers->ctx == ctx);
1244 pBuffers->ctx = ctx;
1245 }
1246 }
1247
1248 for (k=0; k<ctx->client.vertexArrayStackDepth; ++k)
1249 {
1250 CRVertexArrays *pArray = &ctx->client.vertexArrayStack[k];
1251 for (j=0; j<CRSTATECLIENT_MAX_VERTEXARRAYS; ++j)
1252 {
1253 CRClientPointer *cp = crStateGetClientPointerByIndex(j, pArray);
1254 if (obj == cp->buffer)
1255 {
1256 Assert(!pBuffers->ctx || pBuffers->ctx == ctx);
1257 pBuffers->ctx = ctx;
1258 }
1259 }
1260 }
1261#endif
1262}
1263
1264static void crVBoxServerCheckConsistencyBuffersCB(unsigned long key, void *data1, void *data2)
1265{
1266 CRBufferObject *obj = (CRBufferObject *)data1;
1267 CR_SERVER_CHECK_BUFFERS Buffers = {0};
1268 Buffers.obj = obj;
1269 crHashtableWalk(cr_server.contextTable, crVBoxServerCheckConsistencyContextBuffersCB, (void*)&Buffers);
1270}
1271
1272//static void crVBoxServerCheckConsistency2CB(unsigned long key, void *data1, void *data2)
1273//{
1274// CRContextInfo* pContextInfo1 = (CRContextInfo*)data1;
1275// CRContextInfo* pContextInfo2 = (CRContextInfo*)data2;
1276//
1277// CRASSERT(pContextInfo1->pContext);
1278// CRASSERT(pContextInfo2->pContext);
1279//
1280// if (pContextInfo1 == pContextInfo2)
1281// {
1282// CRASSERT(pContextInfo1->pContext == pContextInfo2->pContext);
1283// return;
1284// }
1285//
1286// CRASSERT(pContextInfo1->pContext != pContextInfo2->pContext);
1287// CRASSERT(pContextInfo1->pContext->shared);
1288// CRASSERT(pContextInfo2->pContext->shared);
1289// CRASSERT(pContextInfo1->pContext->shared == pContextInfo2->pContext->shared);
1290// if (pContextInfo1->pContext->shared != pContextInfo2->pContext->shared)
1291// return;
1292//
1293// crHashtableWalk(pContextInfo1->pContext->shared->buffersTable, crVBoxServerCheckConsistencyBuffersCB, pContextInfo2);
1294//}
1295static void crVBoxServerCheckSharedCB(unsigned long key, void *data1, void *data2)
1296{
1297 CRContextInfo* pContextInfo = (CRContextInfo*)data1;
1298 void **ppShared = (void**)data2;
1299 if (!*ppShared)
1300 *ppShared = pContextInfo->pContext->shared;
1301 else
1302 Assert(pContextInfo->pContext->shared == *ppShared);
1303}
1304
1305static void crVBoxServerCheckConsistency()
1306{
1307 CRSharedState *pShared = NULL;
1308 crHashtableWalk(cr_server.contextTable, crVBoxServerCheckSharedCB, (void*)&pShared);
1309 Assert(pShared);
1310 if (pShared)
1311 {
1312 crHashtableWalk(pShared->buffersTable, crVBoxServerCheckConsistencyBuffersCB, NULL);
1313 }
1314}
1315#endif
1316
1317static uint32_t g_hackVBoxServerSaveLoadCallsLeft = 0;
1318
1319DECLEXPORT(int32_t) crVBoxServerSaveState(PSSMHANDLE pSSM)
1320{
1321 int32_t rc, i;
1322 uint32_t ui32;
1323 GLboolean b;
1324 unsigned long key;
1325 GLenum err;
1326#ifdef CR_STATE_NO_TEXTURE_IMAGE_STORE
1327 CRClient *curClient;
1328 CRMuralInfo *curMural = NULL;
1329 CRContextInfo *curCtxInfo = NULL;
1330#endif
1331 CRVBOX_SAVE_STATE_GLOBAL Data;
1332
1333 crMemset(&Data, 0, sizeof (Data));
1334
1335#if 0
1336 crVBoxServerCheckConsistency();
1337#endif
1338
1339 /* We shouldn't be called if there's no clients at all*/
1340 CRASSERT(cr_server.numClients>0);
1341
1342 /* @todo it's hack atm */
1343 /* We want to be called only once to save server state but atm we're being called from svcSaveState
1344 * for every connected client (e.g. guest opengl application)
1345 */
1346 if (!cr_server.bIsInSavingState) /* It's first call */
1347 {
1348 cr_server.bIsInSavingState = GL_TRUE;
1349
1350 /* Store number of clients */
1351 rc = SSMR3PutU32(pSSM, (uint32_t) cr_server.numClients);
1352 AssertRCReturn(rc, rc);
1353
1354 g_hackVBoxServerSaveLoadCallsLeft = cr_server.numClients;
1355 }
1356
1357 g_hackVBoxServerSaveLoadCallsLeft--;
1358
1359 /* Do nothing until we're being called last time */
1360 if (g_hackVBoxServerSaveLoadCallsLeft>0)
1361 {
1362 return VINF_SUCCESS;
1363 }
1364
1365 /* Save rendering contexts creation info */
1366 ui32 = crHashtableNumElements(cr_server.contextTable);
1367 rc = SSMR3PutU32(pSSM, (uint32_t) ui32);
1368 AssertRCReturn(rc, rc);
1369 crHashtableWalk(cr_server.contextTable, crVBoxServerSaveCreateInfoFromCtxInfoCB, pSSM);
1370
1371#ifdef CR_STATE_NO_TEXTURE_IMAGE_STORE
1372 curClient = cr_server.curClient;
1373 /* Save current win and ctx IDs, as we'd rebind contexts when saving textures */
1374 if (curClient)
1375 {
1376 curCtxInfo = cr_server.curClient->currentCtxInfo;
1377 curMural = cr_server.curClient->currentMural;
1378 }
1379 else if (cr_server.numClients)
1380 {
1381 cr_server.curClient = cr_server.clients[0];
1382 }
1383#endif
1384
1385 /* first save windows info */
1386 /* Save windows creation info */
1387 ui32 = crHashtableNumElements(cr_server.muralTable);
1388 /* There should be default mural always */
1389 CRASSERT(ui32>=1);
1390 rc = SSMR3PutU32(pSSM, (uint32_t) ui32-1);
1391 AssertRCReturn(rc, rc);
1392 crHashtableWalk(cr_server.muralTable, crVBoxServerSaveCreateInfoFromMuralInfoCB, pSSM);
1393
1394 /* Save cr_server.muralTable
1395 * @todo we don't need it all, just geometry info actually
1396 */
1397 rc = SSMR3PutU32(pSSM, (uint32_t) ui32-1);
1398 AssertRCReturn(rc, rc);
1399 crHashtableWalk(cr_server.muralTable, crVBoxServerSaveMuralCB, pSSM);
1400
1401 /* we need to save front & backbuffer data for each mural first create a context -> mural association */
1402 crVBoxServerBuildSaveStateGlobal(&Data);
1403
1404 rc = crStateSaveGlobals(pSSM);
1405 AssertRCReturn(rc, rc);
1406
1407 Data.pSSM = pSSM;
1408 /* Save contexts state tracker data */
1409 /* @todo For now just some blind data dumps,
1410 * but I've a feeling those should be saved/restored in a very strict sequence to
1411 * allow diff_api to work correctly.
1412 * Should be tested more with multiply guest opengl apps working when saving VM snapshot.
1413 */
1414 crHashtableWalk(cr_server.contextTable, crVBoxServerSaveContextStateCB, &Data);
1415 AssertRCReturn(Data.rc, Data.rc);
1416
1417 ui32 = crHashtableNumElements(Data.additionalMuralContextTable);
1418 rc = SSMR3PutU32(pSSM, (uint32_t) ui32);
1419 AssertRCReturn(rc, rc);
1420
1421 crHashtableWalk(Data.additionalMuralContextTable, crVBoxServerSaveAdditionalMuralsCB, &Data);
1422 AssertRCReturn(Data.rc, Data.rc);
1423
1424#ifdef CR_STATE_NO_TEXTURE_IMAGE_STORE
1425 cr_server.curClient = curClient;
1426 /* Restore original win and ctx IDs*/
1427 if (curClient && curMural && curCtxInfo)
1428 {
1429 crServerPerformMakeCurrent(curMural, curCtxInfo);
1430 }
1431 else
1432 {
1433 cr_server.bForceMakeCurrentOnClientSwitch = GL_TRUE;
1434 }
1435#endif
1436
1437 /* Save clients info */
1438 for (i = 0; i < cr_server.numClients; i++)
1439 {
1440 if (cr_server.clients[i] && cr_server.clients[i]->conn)
1441 {
1442 CRClient *pClient = cr_server.clients[i];
1443
1444 rc = SSMR3PutU32(pSSM, pClient->conn->u32ClientID);
1445 AssertRCReturn(rc, rc);
1446
1447 rc = SSMR3PutU32(pSSM, pClient->conn->vMajor);
1448 AssertRCReturn(rc, rc);
1449
1450 rc = SSMR3PutU32(pSSM, pClient->conn->vMinor);
1451 AssertRCReturn(rc, rc);
1452
1453 rc = SSMR3PutMem(pSSM, pClient, sizeof(*pClient));
1454 AssertRCReturn(rc, rc);
1455
1456 if (pClient->currentCtxInfo && pClient->currentCtxInfo->pContext && pClient->currentContextNumber>=0)
1457 {
1458 b = crHashtableGetDataKey(cr_server.contextTable, pClient->currentCtxInfo, &key);
1459 CRASSERT(b);
1460 rc = SSMR3PutMem(pSSM, &key, sizeof(key));
1461 AssertRCReturn(rc, rc);
1462 }
1463
1464 if (pClient->currentMural && pClient->currentWindow>=0)
1465 {
1466 b = crHashtableGetDataKey(cr_server.muralTable, pClient->currentMural, &key);
1467 CRASSERT(b);
1468 rc = SSMR3PutMem(pSSM, &key, sizeof(key));
1469 AssertRCReturn(rc, rc);
1470 }
1471 }
1472 }
1473
1474 /* all context gl error states should have now be synced with chromium erro states,
1475 * reset the error if any */
1476 while ((err = cr_server.head_spu->dispatch_table.GetError()) != GL_NO_ERROR)
1477 crWarning("crServer: glGetError %d after saving snapshot", err);
1478
1479 cr_server.bIsInSavingState = GL_FALSE;
1480
1481 return VINF_SUCCESS;
1482}
1483
1484static DECLCALLBACK(CRContext*) crVBoxServerGetContextCB(void* pvData)
1485{
1486 CRContextInfo* pContextInfo = (CRContextInfo*)pvData;
1487 CRASSERT(pContextInfo);
1488 CRASSERT(pContextInfo->pContext);
1489 return pContextInfo->pContext;
1490}
1491
1492static int32_t crVBoxServerLoadMurals(PSSMHANDLE pSSM, uint32_t version)
1493{
1494 unsigned long key;
1495 uint32_t ui, uiNumElems;
1496 /* Load windows */
1497 int32_t rc = SSMR3GetU32(pSSM, &uiNumElems);
1498 AssertRCReturn(rc, rc);
1499 for (ui=0; ui<uiNumElems; ++ui)
1500 {
1501 CRCreateInfo_t createInfo;
1502 char psz[200];
1503 GLint winID;
1504 unsigned long key;
1505
1506 rc = SSMR3GetMem(pSSM, &key, sizeof(key));
1507 AssertRCReturn(rc, rc);
1508 rc = SSMR3GetMem(pSSM, &createInfo, sizeof(createInfo));
1509 AssertRCReturn(rc, rc);
1510
1511 if (createInfo.pszDpyName)
1512 {
1513 rc = SSMR3GetStrZEx(pSSM, psz, 200, NULL);
1514 AssertRCReturn(rc, rc);
1515 createInfo.pszDpyName = psz;
1516 }
1517
1518 winID = crServerDispatchWindowCreateEx(createInfo.pszDpyName, createInfo.visualBits, key);
1519 CRASSERT((int64_t)winID == (int64_t)key);
1520 }
1521
1522 /* Load cr_server.muralTable */
1523 rc = SSMR3GetU32(pSSM, &uiNumElems);
1524 AssertRCReturn(rc, rc);
1525 for (ui=0; ui<uiNumElems; ++ui)
1526 {
1527 CRMuralInfo muralInfo;
1528
1529 rc = SSMR3GetMem(pSSM, &key, sizeof(key));
1530 AssertRCReturn(rc, rc);
1531 rc = SSMR3GetMem(pSSM, &muralInfo, RT_OFFSETOF(CRMuralInfo, CreateInfo));
1532 AssertRCReturn(rc, rc);
1533
1534 if (version <= SHCROGL_SSM_VERSION_BEFORE_FRONT_DRAW_TRACKING)
1535 muralInfo.bFbDraw = GL_TRUE;
1536
1537 if (muralInfo.pVisibleRects)
1538 {
1539 muralInfo.pVisibleRects = crAlloc(4*sizeof(GLint)*muralInfo.cVisibleRects);
1540 if (!muralInfo.pVisibleRects)
1541 {
1542 return VERR_NO_MEMORY;
1543 }
1544
1545 rc = SSMR3GetMem(pSSM, muralInfo.pVisibleRects, 4*sizeof(GLint)*muralInfo.cVisibleRects);
1546 AssertRCReturn(rc, rc);
1547 }
1548
1549 if (version >= SHCROGL_SSM_VERSION_WITH_WINDOW_CTX_USAGE)
1550 {
1551 CRMuralInfo *pActualMural = (CRMuralInfo *)crHashtableSearch(cr_server.muralTable, key);;
1552 CRASSERT(pActualMural);
1553 rc = SSMR3GetMem(pSSM, pActualMural->ctxUsage, sizeof (pActualMural->ctxUsage));
1554 CRASSERT(rc == VINF_SUCCESS);
1555 }
1556
1557 /* Restore windows geometry info */
1558 crServerDispatchWindowSize(key, muralInfo.width, muralInfo.height);
1559 crServerDispatchWindowPosition(key, muralInfo.gX, muralInfo.gY);
1560 /* Same workaround as described in stub.c:stubUpdateWindowVisibileRegions for compiz on a freshly booted VM*/
1561 if (muralInfo.bReceivedRects)
1562 {
1563 crServerDispatchWindowVisibleRegion(key, muralInfo.cVisibleRects, muralInfo.pVisibleRects);
1564 }
1565 crServerDispatchWindowShow(key, muralInfo.bVisible);
1566
1567 if (muralInfo.pVisibleRects)
1568 {
1569 crFree(muralInfo.pVisibleRects);
1570 }
1571 }
1572
1573 CRASSERT(RT_SUCCESS(rc));
1574 return VINF_SUCCESS;
1575}
1576
1577static int crVBoxServerLoadFBImage(PSSMHANDLE pSSM, uint32_t version,
1578 CRContextInfo* pContextInfo, CRMuralInfo *pMural)
1579{
1580 CRContext *pContext = pContextInfo->pContext;
1581 GLint storedWidth, storedHeight;
1582 int32_t rc = VINF_SUCCESS;
1583
1584 if (version > SHCROGL_SSM_VERSION_WITH_BUGGY_FB_IMAGE_DATA)
1585 {
1586 CRASSERT(cr_server.currentCtxInfo == pContextInfo);
1587 CRASSERT(cr_server.currentMural = pMural);
1588 storedWidth = pMural->width;
1589 storedHeight = pMural->height;
1590 }
1591 else
1592 {
1593 storedWidth = pContext->buffer.storedWidth;
1594 storedHeight = pContext->buffer.storedHeight;
1595 }
1596
1597 if (storedWidth && storedHeight)
1598 {
1599 CRBufferState *pBuf = &pContext->buffer;
1600 GLint cbData;
1601 void *pData;
1602
1603 cbData = crPixelSize(GL_RGBA, GL_UNSIGNED_BYTE) * storedWidth * storedHeight;
1604
1605 pData = crAlloc(cbData);
1606 if (!pData)
1607 {
1608 crWarning("crAlloc failed trying to allocate %d of fb data", cbData);
1609 pBuf->pFrontImg = NULL;
1610 pBuf->pBackImg = NULL;
1611 return VERR_NO_MEMORY;
1612 }
1613
1614 rc = SSMR3GetMem(pSSM, pData, cbData);
1615 AssertRCReturn(rc, rc);
1616
1617 pBuf->pFrontImg = pData;
1618
1619 pData = crAlloc(cbData);
1620 if (!pData)
1621 {
1622 crWarning("crAlloc failed trying to allocate %d of bb data", cbData);
1623 pBuf->pBackImg = NULL;
1624 return VERR_NO_MEMORY;
1625 }
1626
1627 rc = SSMR3GetMem(pSSM, pData, cbData);
1628 AssertRCReturn(rc, rc);
1629
1630 pBuf->pBackImg = pData;
1631
1632 if (version > SHCROGL_SSM_VERSION_WITH_BUGGY_FB_IMAGE_DATA)
1633 {
1634 /* can apply the data right away */
1635 if (!pMural->fUseFBO)
1636 {
1637 CRASSERT(cr_server.bForceOffscreenRendering == CR_SERVER_REDIR_NONE);
1638 crStateApplyFBImage(pContext);
1639 }
1640 else
1641 {
1642 VBOXVR_TEXTURE Tex;
1643 Tex.width = pMural->width;
1644 Tex.height = pMural->height;
1645 Tex.target = GL_TEXTURE_2D;
1646
1647 CRASSERT(cr_server.bForceOffscreenRendering > CR_SERVER_REDIR_NONE);
1648
1649 if (pBuf->pFrontImg)
1650 {
1651 Tex.hwid = pMural->aidColorTexs[CR_SERVER_FBO_FB_IDX(pMural)];
1652 CRASSERT(Tex.hwid);
1653 CrHlpPutTexImage(pContext, &Tex, pBuf->pFrontImg);
1654 }
1655
1656 if (pBuf->pBackImg)
1657 {
1658 Tex.hwid = pMural->aidColorTexs[CR_SERVER_FBO_BB_IDX(pMural)];
1659 CRASSERT(Tex.hwid);
1660 CrHlpPutTexImage(pContext, &Tex, pBuf->pBackImg);
1661 }
1662
1663 crStateFreeFBImage(pContext);
1664 }
1665 CRASSERT(!pBuf->pFrontImg);
1666 CRASSERT(!pBuf->pBackImg);
1667 }
1668 }
1669
1670 CRASSERT(RT_SUCCESS(rc));
1671 return VINF_SUCCESS;
1672}
1673
1674DECLEXPORT(int32_t) crVBoxServerLoadState(PSSMHANDLE pSSM, uint32_t version)
1675{
1676 int32_t rc, i;
1677 uint32_t ui, uiNumElems;
1678 unsigned long key;
1679 GLenum err;
1680
1681 if (!cr_server.bIsInLoadingState)
1682 {
1683 /* AssertRCReturn(...) will leave us in loading state, but it doesn't matter as we'd be failing anyway */
1684 cr_server.bIsInLoadingState = GL_TRUE;
1685
1686 /* Read number of clients */
1687 rc = SSMR3GetU32(pSSM, &g_hackVBoxServerSaveLoadCallsLeft);
1688 AssertRCReturn(rc, rc);
1689 }
1690
1691 g_hackVBoxServerSaveLoadCallsLeft--;
1692
1693 /* Do nothing until we're being called last time */
1694 if (g_hackVBoxServerSaveLoadCallsLeft>0)
1695 {
1696 return VINF_SUCCESS;
1697 }
1698
1699 if (version < SHCROGL_SSM_VERSION_BEFORE_CTXUSAGE_BITS)
1700 {
1701 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
1702 }
1703
1704 /* Load and recreate rendering contexts */
1705 rc = SSMR3GetU32(pSSM, &uiNumElems);
1706 AssertRCReturn(rc, rc);
1707 for (ui=0; ui<uiNumElems; ++ui)
1708 {
1709 CRCreateInfo_t createInfo;
1710 char psz[200];
1711 GLint ctxID;
1712 CRContextInfo* pContextInfo;
1713 CRContext* pContext;
1714
1715 rc = SSMR3GetMem(pSSM, &key, sizeof(key));
1716 AssertRCReturn(rc, rc);
1717 rc = SSMR3GetMem(pSSM, &createInfo, sizeof(createInfo));
1718 AssertRCReturn(rc, rc);
1719
1720 if (createInfo.pszDpyName)
1721 {
1722 rc = SSMR3GetStrZEx(pSSM, psz, 200, NULL);
1723 AssertRCReturn(rc, rc);
1724 createInfo.pszDpyName = psz;
1725 }
1726
1727 ctxID = crServerDispatchCreateContextEx(createInfo.pszDpyName, createInfo.visualBits, 0, key, createInfo.externalID /* <-saved state stores internal id here*/);
1728 CRASSERT((int64_t)ctxID == (int64_t)key);
1729
1730 pContextInfo = (CRContextInfo*) crHashtableSearch(cr_server.contextTable, key);
1731 CRASSERT(pContextInfo);
1732 CRASSERT(pContextInfo->pContext);
1733 pContext = pContextInfo->pContext;
1734 pContext->shared->id=-1;
1735 }
1736
1737 if (version > SHCROGL_SSM_VERSION_WITH_BUGGY_FB_IMAGE_DATA)
1738 {
1739 /* we have a mural data here */
1740 rc = crVBoxServerLoadMurals(pSSM, version);
1741 AssertRCReturn(rc, rc);
1742 }
1743
1744 if (version > SHCROGL_SSM_VERSION_WITH_BUGGY_FB_IMAGE_DATA && uiNumElems)
1745 {
1746 /* set the current client to allow doing crServerPerformMakeCurrent later */
1747 CRASSERT(cr_server.numClients);
1748 cr_server.curClient = cr_server.clients[0];
1749 }
1750
1751 rc = crStateLoadGlobals(pSSM, version);
1752 AssertRCReturn(rc, rc);
1753
1754 /* Restore context state data */
1755 for (ui=0; ui<uiNumElems; ++ui)
1756 {
1757 CRContextInfo* pContextInfo;
1758 CRContext *pContext;
1759 CRMuralInfo *pMural = NULL;
1760 int32_t winId = 0;
1761
1762 rc = SSMR3GetMem(pSSM, &key, sizeof(key));
1763 AssertRCReturn(rc, rc);
1764
1765 pContextInfo = (CRContextInfo*) crHashtableSearch(cr_server.contextTable, key);
1766 CRASSERT(pContextInfo);
1767 CRASSERT(pContextInfo->pContext);
1768 pContext = pContextInfo->pContext;
1769
1770 if (version > SHCROGL_SSM_VERSION_WITH_BUGGY_FB_IMAGE_DATA)
1771 {
1772 rc = SSMR3GetMem(pSSM, &winId, sizeof(winId));
1773 AssertRCReturn(rc, rc);
1774
1775 if (winId)
1776 {
1777 pMural = (CRMuralInfo*)crHashtableSearch(cr_server.muralTable, winId);
1778 CRASSERT(pMural);
1779 }
1780 else
1781 {
1782 /* null winId means a dummy mural, get it */
1783 pMural = crServerGetDummyMural(pContextInfo->CreateInfo.visualBits);
1784 CRASSERT(pMural);
1785 }
1786
1787 crServerPerformMakeCurrent(pMural, pContextInfo);
1788 }
1789
1790 rc = crStateLoadContext(pContext, cr_server.contextTable, crVBoxServerGetContextCB, pSSM, version);
1791 AssertRCReturn(rc, rc);
1792
1793 /*Restore front/back buffer images*/
1794 rc = crVBoxServerLoadFBImage(pSSM, version, pContextInfo, pMural);
1795 AssertRCReturn(rc, rc);
1796 }
1797
1798 if (version > SHCROGL_SSM_VERSION_WITH_BUGGY_FB_IMAGE_DATA)
1799 {
1800 CRContextInfo *pContextInfo;
1801 CRMuralInfo *pMural;
1802 GLint ctxId;
1803
1804 rc = SSMR3GetU32(pSSM, &uiNumElems);
1805 AssertRCReturn(rc, rc);
1806 for (ui=0; ui<uiNumElems; ++ui)
1807 {
1808 CRbitvalue initialCtxUsage[CR_MAX_BITARRAY];
1809 CRMuralInfo *pInitialCurMural;
1810
1811 rc = SSMR3GetMem(pSSM, &key, sizeof(key));
1812 AssertRCReturn(rc, rc);
1813
1814 rc = SSMR3GetMem(pSSM, &ctxId, sizeof(ctxId));
1815 AssertRCReturn(rc, rc);
1816
1817 pMural = (CRMuralInfo*)crHashtableSearch(cr_server.muralTable, key);
1818 CRASSERT(pMural);
1819 if (ctxId)
1820 {
1821 pContextInfo = (CRContextInfo *)crHashtableSearch(cr_server.contextTable, ctxId);
1822 CRASSERT(pContextInfo);
1823 }
1824 else
1825 pContextInfo = &cr_server.MainContextInfo;
1826
1827 crMemcpy(initialCtxUsage, pMural->ctxUsage, sizeof (initialCtxUsage));
1828 pInitialCurMural = pContextInfo->currentMural;
1829
1830 crServerPerformMakeCurrent(pMural, pContextInfo);
1831
1832 rc = crVBoxServerLoadFBImage(pSSM, version, pContextInfo, pMural);
1833 AssertRCReturn(rc, rc);
1834
1835 /* restore the reference data, we synchronize it with the HW state in a later crServerPerformMakeCurrent call */
1836 crMemcpy(pMural->ctxUsage, initialCtxUsage, sizeof (initialCtxUsage));
1837 pContextInfo->currentMural = pInitialCurMural;
1838 }
1839
1840 if (cr_server.currentCtxInfo != &cr_server.MainContextInfo)
1841 {
1842 /* most ogl data gets loaded to hw on chromium 3D state switch, i.e. inside crStateMakeCurrent -> crStateSwitchContext
1843 * to force the crStateSwitchContext being called later, we need to set the current context to our dummy one */
1844 pMural = crServerGetDummyMural(cr_server.MainContextInfo.CreateInfo.visualBits);
1845 CRASSERT(pMural);
1846 crServerPerformMakeCurrent(pMural, &cr_server.MainContextInfo);
1847 }
1848
1849 cr_server.curClient = NULL;
1850 cr_server.bForceMakeCurrentOnClientSwitch = GL_TRUE;
1851 }
1852 else
1853 {
1854 CRServerFreeIDsPool_t dummyIdsPool;
1855
1856 /* we have a mural data here */
1857 rc = crVBoxServerLoadMurals(pSSM, version);
1858 AssertRCReturn(rc, rc);
1859
1860 /* not used any more, just read it out and ignore */
1861 rc = SSMR3GetMem(pSSM, &dummyIdsPool, sizeof(dummyIdsPool));
1862 CRASSERT(rc == VINF_SUCCESS);
1863 }
1864
1865 /* Load clients info */
1866 for (i = 0; i < cr_server.numClients; i++)
1867 {
1868 if (cr_server.clients[i] && cr_server.clients[i]->conn)
1869 {
1870 CRClient *pClient = cr_server.clients[i];
1871 CRClient client;
1872 unsigned long ctxID=-1, winID=-1;
1873
1874 rc = SSMR3GetU32(pSSM, &ui);
1875 AssertRCReturn(rc, rc);
1876 /* If this assert fires, then we should search correct client in the list first*/
1877 CRASSERT(ui == pClient->conn->u32ClientID);
1878
1879 if (version>=4)
1880 {
1881 rc = SSMR3GetU32(pSSM, &pClient->conn->vMajor);
1882 AssertRCReturn(rc, rc);
1883
1884 rc = SSMR3GetU32(pSSM, &pClient->conn->vMinor);
1885 AssertRCReturn(rc, rc);
1886 }
1887
1888 rc = SSMR3GetMem(pSSM, &client, sizeof(client));
1889 CRASSERT(rc == VINF_SUCCESS);
1890
1891 client.conn = pClient->conn;
1892 /* We can't reassign client number, as we'd get wrong results in TranslateTextureID
1893 * and fail to bind old textures.
1894 */
1895 /*client.number = pClient->number;*/
1896 *pClient = client;
1897
1898 pClient->currentContextNumber = -1;
1899 pClient->currentCtxInfo = &cr_server.MainContextInfo;
1900 pClient->currentMural = NULL;
1901 pClient->currentWindow = -1;
1902
1903 cr_server.curClient = pClient;
1904
1905 if (client.currentCtxInfo && client.currentContextNumber>=0)
1906 {
1907 rc = SSMR3GetMem(pSSM, &ctxID, sizeof(ctxID));
1908 AssertRCReturn(rc, rc);
1909 client.currentCtxInfo = (CRContextInfo*) crHashtableSearch(cr_server.contextTable, ctxID);
1910 CRASSERT(client.currentCtxInfo);
1911 CRASSERT(client.currentCtxInfo->pContext);
1912 //pClient->currentCtx = client.currentCtx;
1913 //pClient->currentContextNumber = ctxID;
1914 }
1915
1916 if (client.currentMural && client.currentWindow>=0)
1917 {
1918 rc = SSMR3GetMem(pSSM, &winID, sizeof(winID));
1919 AssertRCReturn(rc, rc);
1920 client.currentMural = (CRMuralInfo*) crHashtableSearch(cr_server.muralTable, winID);
1921 CRASSERT(client.currentMural);
1922 //pClient->currentMural = client.currentMural;
1923 //pClient->currentWindow = winID;
1924 }
1925
1926 /* Restore client active context and window */
1927 crServerDispatchMakeCurrent(winID, 0, ctxID);
1928
1929 if (0)
1930 {
1931// CRContext *tmpCtx;
1932// CRCreateInfo_t *createInfo;
1933 GLfloat one[4] = { 1, 1, 1, 1 };
1934 GLfloat amb[4] = { 0.4f, 0.4f, 0.4f, 1.0f };
1935
1936 crServerDispatchMakeCurrent(winID, 0, ctxID);
1937
1938 crHashtableWalk(client.currentCtxInfo->pContext->shared->textureTable, crVBoxServerSyncTextureCB, client.currentCtxInfo->pContext);
1939
1940 crStateTextureObjectDiff(client.currentCtxInfo->pContext, NULL, NULL, &client.currentCtxInfo->pContext->texture.base1D, GL_TRUE);
1941 crStateTextureObjectDiff(client.currentCtxInfo->pContext, NULL, NULL, &client.currentCtxInfo->pContext->texture.base2D, GL_TRUE);
1942 crStateTextureObjectDiff(client.currentCtxInfo->pContext, NULL, NULL, &client.currentCtxInfo->pContext->texture.base3D, GL_TRUE);
1943#ifdef CR_ARB_texture_cube_map
1944 crStateTextureObjectDiff(client.currentCtxInfo->pContext, NULL, NULL, &client.currentCtxInfo->pContext->texture.baseCubeMap, GL_TRUE);
1945#endif
1946#ifdef CR_NV_texture_rectangle
1947 //@todo this doesn't work as expected
1948 //crStateTextureObjectDiff(client.currentCtxInfo->pContext, NULL, NULL, &client.currentCtxInfo->pContext->texture.baseRect, GL_TRUE);
1949#endif
1950 /*cr_server.head_spu->dispatch_table.Materialfv(GL_FRONT_AND_BACK, GL_AMBIENT, amb);
1951 cr_server.head_spu->dispatch_table.LightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
1952 cr_server.head_spu->dispatch_table.Lightfv(GL_LIGHT1, GL_DIFFUSE, one);
1953
1954 cr_server.head_spu->dispatch_table.Enable(GL_LIGHTING);
1955 cr_server.head_spu->dispatch_table.Enable(GL_LIGHT0);
1956 cr_server.head_spu->dispatch_table.Enable(GL_LIGHT1);
1957
1958 cr_server.head_spu->dispatch_table.Enable(GL_CULL_FACE);
1959 cr_server.head_spu->dispatch_table.Enable(GL_TEXTURE_2D);*/
1960
1961 //crStateViewport( 0, 0, 600, 600 );
1962 //pClient->currentMural->viewportValidated = GL_FALSE;
1963 //cr_server.head_spu->dispatch_table.Viewport( 0, 0, 600, 600 );
1964
1965 //crStateMatrixMode(GL_PROJECTION);
1966 //cr_server.head_spu->dispatch_table.MatrixMode(GL_PROJECTION);
1967
1968 //crStateLoadIdentity();
1969 //cr_server.head_spu->dispatch_table.LoadIdentity();
1970
1971 //crStateFrustum(-0.5, 0.5, -0.5, 0.5, 1.5, 150.0);
1972 //cr_server.head_spu->dispatch_table.Frustum(-0.5, 0.5, -0.5, 0.5, 1.5, 150.0);
1973
1974 //crStateMatrixMode(GL_MODELVIEW);
1975 //cr_server.head_spu->dispatch_table.MatrixMode(GL_MODELVIEW);
1976 //crServerDispatchLoadIdentity();
1977 //crStateFrustum(-0.5, 0.5, -0.5, 0.5, 1.5, 150.0);
1978 //cr_server.head_spu->dispatch_table.Frustum(-0.5, 0.5, -0.5, 0.5, 1.5, 150.0);
1979 //crServerDispatchLoadIdentity();
1980
1981 /*createInfo = (CRCreateInfo_t *) crHashtableSearch(cr_server.pContextCreateInfoTable, ctxID);
1982 CRASSERT(createInfo);
1983 tmpCtx = crStateCreateContext(NULL, createInfo->visualBits, NULL);
1984 CRASSERT(tmpCtx);
1985 crStateDiffContext(tmpCtx, client.currentCtxInfo->pContext);
1986 crStateDestroyContext(tmpCtx);*/
1987 }
1988 }
1989 }
1990
1991 //crServerDispatchMakeCurrent(-1, 0, -1);
1992
1993 cr_server.curClient = NULL;
1994
1995 while ((err = cr_server.head_spu->dispatch_table.GetError()) != GL_NO_ERROR)
1996 crWarning("crServer: glGetError %d after loading snapshot", err);
1997
1998 cr_server.bIsInLoadingState = GL_FALSE;
1999
2000#if 0
2001 crVBoxServerCheckConsistency();
2002#endif
2003
2004 return VINF_SUCCESS;
2005}
2006
2007#define SCREEN(i) (cr_server.screen[i])
2008#define MAPPED(screen) ((screen).winID != 0)
2009
2010static void crVBoxServerReparentMuralCB(unsigned long key, void *data1, void *data2)
2011{
2012 CRMuralInfo *pMI = (CRMuralInfo*) data1;
2013 int *sIndex = (int*) data2;
2014
2015 if (pMI->screenId == *sIndex)
2016 {
2017 renderspuReparentWindow(pMI->spuWindow);
2018 }
2019}
2020
2021static void crVBoxServerCheckMuralCB(unsigned long key, void *data1, void *data2)
2022{
2023 CRMuralInfo *pMI = (CRMuralInfo*) data1;
2024 (void) data2;
2025
2026 crServerCheckMuralGeometry(pMI);
2027}
2028
2029DECLEXPORT(int32_t) crVBoxServerSetScreenCount(int sCount)
2030{
2031 int i;
2032
2033 if (sCount>CR_MAX_GUEST_MONITORS)
2034 return VERR_INVALID_PARAMETER;
2035
2036 /*Shouldn't happen yet, but to be safe in future*/
2037 for (i=0; i<cr_server.screenCount; ++i)
2038 {
2039 if (MAPPED(SCREEN(i)))
2040 crWarning("Screen count is changing, but screen[%i] is still mapped", i);
2041 return VERR_NOT_IMPLEMENTED;
2042 }
2043
2044 cr_server.screenCount = sCount;
2045
2046 for (i=0; i<sCount; ++i)
2047 {
2048 SCREEN(i).winID = 0;
2049 }
2050
2051 return VINF_SUCCESS;
2052}
2053
2054DECLEXPORT(int32_t) crVBoxServerUnmapScreen(int sIndex)
2055{
2056 crDebug("crVBoxServerUnmapScreen(%i)", sIndex);
2057
2058 if (sIndex<0 || sIndex>=cr_server.screenCount)
2059 return VERR_INVALID_PARAMETER;
2060
2061 if (MAPPED(SCREEN(sIndex)))
2062 {
2063 SCREEN(sIndex).winID = 0;
2064 renderspuSetWindowId(0);
2065
2066 crHashtableWalk(cr_server.muralTable, crVBoxServerReparentMuralCB, &sIndex);
2067 }
2068
2069 renderspuSetWindowId(SCREEN(0).winID);
2070 return VINF_SUCCESS;
2071}
2072
2073DECLEXPORT(int32_t) crVBoxServerMapScreen(int sIndex, int32_t x, int32_t y, uint32_t w, uint32_t h, uint64_t winID)
2074{
2075 crDebug("crVBoxServerMapScreen(%i) [%i,%i:%u,%u %x]", sIndex, x, y, w, h, winID);
2076
2077 if (sIndex<0 || sIndex>=cr_server.screenCount)
2078 return VERR_INVALID_PARAMETER;
2079
2080 if (MAPPED(SCREEN(sIndex)) && SCREEN(sIndex).winID!=winID)
2081 {
2082 crDebug("Mapped screen[%i] is being remapped.", sIndex);
2083 crVBoxServerUnmapScreen(sIndex);
2084 }
2085
2086 SCREEN(sIndex).winID = winID;
2087 SCREEN(sIndex).x = x;
2088 SCREEN(sIndex).y = y;
2089 SCREEN(sIndex).w = w;
2090 SCREEN(sIndex).h = h;
2091
2092 renderspuSetWindowId(SCREEN(sIndex).winID);
2093 crHashtableWalk(cr_server.muralTable, crVBoxServerReparentMuralCB, &sIndex);
2094 renderspuSetWindowId(SCREEN(0).winID);
2095
2096 crHashtableWalk(cr_server.muralTable, crVBoxServerCheckMuralCB, NULL);
2097
2098#ifndef WINDOWS
2099 /*Restore FB content for clients, which have current window on a screen being remapped*/
2100 {
2101 GLint i;
2102
2103 for (i = 0; i < cr_server.numClients; i++)
2104 {
2105 cr_server.curClient = cr_server.clients[i];
2106 if (cr_server.curClient->currentCtxInfo
2107 && cr_server.curClient->currentCtxInfo->pContext
2108 && (cr_server.curClient->currentCtxInfo->pContext->buffer.pFrontImg || cr_server.curClient->currentCtxInfo->pContext->buffer.pBackImg)
2109 && cr_server.curClient->currentMural
2110 && cr_server.curClient->currentMural->screenId == sIndex
2111 && cr_server.curClient->currentCtxInfo->pContext->buffer.storedHeight == h
2112 && cr_server.curClient->currentCtxInfo->pContext->buffer.storedWidth == w)
2113 {
2114 int clientWindow = cr_server.curClient->currentWindow;
2115 int clientContext = cr_server.curClient->currentContextNumber;
2116
2117 if (clientWindow && clientWindow != cr_server.currentWindow)
2118 {
2119 crServerDispatchMakeCurrent(clientWindow, 0, clientContext);
2120 }
2121
2122 crStateApplyFBImage(cr_server.curClient->currentCtxInfo->pContext);
2123 }
2124 }
2125 cr_server.curClient = NULL;
2126 }
2127#endif
2128
2129 {
2130 PCR_DISPLAY pDisplay = crServerDisplayGetInitialized(sIndex);
2131 if (pDisplay)
2132 CrDpResize(pDisplay, w, h, w, h);
2133 }
2134
2135 return VINF_SUCCESS;
2136}
2137
2138DECLEXPORT(int32_t) crVBoxServerSetRootVisibleRegion(GLint cRects, GLint *pRects)
2139{
2140 renderspuSetRootVisibleRegion(cRects, pRects);
2141
2142 return VINF_SUCCESS;
2143}
2144
2145DECLEXPORT(void) crVBoxServerSetPresentFBOCB(PFNCRSERVERPRESENTFBO pfnPresentFBO)
2146{
2147 cr_server.pfnPresentFBO = pfnPresentFBO;
2148}
2149
2150int32_t crServerSetOffscreenRenderingMode(GLubyte value)
2151{
2152 if (cr_server.bForceOffscreenRendering==value)
2153 {
2154 return VINF_SUCCESS;
2155 }
2156
2157 if (value > CR_SERVER_REDIR_MAXVAL)
2158 {
2159 crWarning("crServerSetOffscreenRenderingMode: invalid arg: %d", value);
2160 return VERR_INVALID_PARAMETER;
2161 }
2162
2163 if (value && !crServerSupportRedirMuralFBO())
2164 {
2165 return VERR_NOT_SUPPORTED;
2166 }
2167
2168 cr_server.bForceOffscreenRendering=value;
2169
2170 crHashtableWalk(cr_server.muralTable, crVBoxServerCheckMuralCB, NULL);
2171
2172 return VINF_SUCCESS;
2173}
2174
2175DECLEXPORT(int32_t) crVBoxServerSetOffscreenRendering(GLboolean value)
2176{
2177 return crServerSetOffscreenRenderingMode(value ? CR_SERVER_REDIR_FBO_RAM : cr_server.bOffscreenRenderingDefault);
2178}
2179
2180DECLEXPORT(int32_t) crVBoxServerOutputRedirectSet(const CROutputRedirect *pCallbacks)
2181{
2182 /* No need for a synchronization as this is single threaded. */
2183 if (pCallbacks)
2184 {
2185 cr_server.outputRedirect = *pCallbacks;
2186 cr_server.bUseOutputRedirect = true;
2187 }
2188 else
2189 {
2190 cr_server.bUseOutputRedirect = false;
2191 }
2192
2193 // @todo dynamically intercept already existing output:
2194 // crHashtableWalk(cr_server.muralTable, crVBoxServerOutputRedirectCB, NULL);
2195
2196 return VINF_SUCCESS;
2197}
2198
2199static void crVBoxServerUpdateScreenViewportCB(unsigned long key, void *data1, void *data2)
2200{
2201 CRMuralInfo *mural = (CRMuralInfo*) data1;
2202 int *sIndex = (int*) data2;
2203
2204 if (mural->screenId != *sIndex)
2205 return;
2206
2207 crServerCheckMuralGeometry(mural);
2208}
2209
2210
2211DECLEXPORT(int32_t) crVBoxServerSetScreenViewport(int sIndex, int32_t x, int32_t y, uint32_t w, uint32_t h)
2212{
2213 CRScreenViewportInfo *pVieport;
2214 GLboolean fPosChanged, fSizeChanged;
2215
2216 crDebug("crVBoxServerSetScreenViewport(%i)", sIndex);
2217
2218 if (sIndex<0 || sIndex>=cr_server.screenCount)
2219 {
2220 crWarning("crVBoxServerSetScreenViewport: invalid screen id %d", sIndex);
2221 return VERR_INVALID_PARAMETER;
2222 }
2223
2224 pVieport = &cr_server.screenVieport[sIndex];
2225 fPosChanged = (pVieport->x != x || pVieport->y != y);
2226 fSizeChanged = (pVieport->w != w || pVieport->h != h);
2227
2228 if (!fPosChanged && !fSizeChanged)
2229 {
2230 crDebug("crVBoxServerSetScreenViewport: no changes");
2231 return VINF_SUCCESS;
2232 }
2233
2234 if (fPosChanged)
2235 {
2236 pVieport->x = x;
2237 pVieport->y = y;
2238
2239 crHashtableWalk(cr_server.muralTable, crVBoxServerUpdateScreenViewportCB, &sIndex);
2240 }
2241
2242 if (fSizeChanged)
2243 {
2244 pVieport->w = w;
2245 pVieport->h = h;
2246
2247 /* no need to do anything here actually */
2248 }
2249 return VINF_SUCCESS;
2250}
2251
2252
2253#ifdef VBOX_WITH_CRHGSMI
2254/* We moved all CrHgsmi command processing to crserverlib to keep the logic of dealing with CrHgsmi commands in one place.
2255 *
2256 * For now we need the notion of CrHgdmi commands in the crserver_lib to be able to complete it asynchronously once it is really processed.
2257 * This help avoiding the "blocked-client" issues. The client is blocked if another client is doing begin-end stuff.
2258 * For now we eliminated polling that could occur on block, which caused a higher-priority thread (in guest) polling for the blocked command complition
2259 * to block the lower-priority thread trying to complete the blocking command.
2260 * And removed extra memcpy done on blocked command arrival.
2261 *
2262 * In the future we will extend CrHgsmi functionality to maintain texture data directly in CrHgsmi allocation to avoid extra memcpy-ing with PBO,
2263 * implement command completion and stuff necessary for GPU scheduling to work properly for WDDM Windows guests, etc.
2264 *
2265 * NOTE: it is ALWAYS responsibility of the crVBoxServerCrHgsmiCmd to complete the command!
2266 * */
2267int32_t crVBoxServerCrHgsmiCmd(struct VBOXVDMACMD_CHROMIUM_CMD *pCmd, uint32_t cbCmd)
2268{
2269 int32_t rc;
2270 uint32_t cBuffers = pCmd->cBuffers;
2271 uint32_t cParams;
2272 uint32_t cbHdr;
2273 CRVBOXHGSMIHDR *pHdr;
2274 uint32_t u32Function;
2275 uint32_t u32ClientID;
2276 CRClient *pClient;
2277
2278 if (!g_pvVRamBase)
2279 {
2280 crWarning("g_pvVRamBase is not initialized");
2281 crServerCrHgsmiCmdComplete(pCmd, VERR_INVALID_STATE);
2282 return VINF_SUCCESS;
2283 }
2284
2285 if (!cBuffers)
2286 {
2287 crWarning("zero buffers passed in!");
2288 crServerCrHgsmiCmdComplete(pCmd, VERR_INVALID_PARAMETER);
2289 return VINF_SUCCESS;
2290 }
2291
2292 cParams = cBuffers-1;
2293
2294 cbHdr = pCmd->aBuffers[0].cbBuffer;
2295 pHdr = VBOXCRHGSMI_PTR_SAFE(pCmd->aBuffers[0].offBuffer, cbHdr, CRVBOXHGSMIHDR);
2296 if (!pHdr)
2297 {
2298 crWarning("invalid header buffer!");
2299 crServerCrHgsmiCmdComplete(pCmd, VERR_INVALID_PARAMETER);
2300 return VINF_SUCCESS;
2301 }
2302
2303 if (cbHdr < sizeof (*pHdr))
2304 {
2305 crWarning("invalid header buffer size!");
2306 crServerCrHgsmiCmdComplete(pCmd, VERR_INVALID_PARAMETER);
2307 return VINF_SUCCESS;
2308 }
2309
2310 u32Function = pHdr->u32Function;
2311 u32ClientID = pHdr->u32ClientID;
2312
2313 switch (u32Function)
2314 {
2315 case SHCRGL_GUEST_FN_WRITE:
2316 {
2317 crDebug(("svcCall: SHCRGL_GUEST_FN_WRITE\n"));
2318
2319 /* @todo: Verify */
2320 if (cParams == 1)
2321 {
2322 CRVBOXHGSMIWRITE* pFnCmd = (CRVBOXHGSMIWRITE*)pHdr;
2323 VBOXVDMACMD_CHROMIUM_BUFFER *pBuf = &pCmd->aBuffers[1];
2324 /* Fetch parameters. */
2325 uint32_t cbBuffer = pBuf->cbBuffer;
2326 uint8_t *pBuffer = VBOXCRHGSMI_PTR_SAFE(pBuf->offBuffer, cbBuffer, uint8_t);
2327
2328 if (cbHdr < sizeof (*pFnCmd))
2329 {
2330 crWarning("invalid write cmd buffer size!");
2331 rc = VERR_INVALID_PARAMETER;
2332 break;
2333 }
2334
2335 CRASSERT(cbBuffer);
2336 if (!pBuffer)
2337 {
2338 crWarning("invalid buffer data received from guest!");
2339 rc = VERR_INVALID_PARAMETER;
2340 break;
2341 }
2342
2343 rc = crVBoxServerClientGet(u32ClientID, &pClient);
2344 if (RT_FAILURE(rc))
2345 {
2346 break;
2347 }
2348
2349 /* This should never fire unless we start to multithread */
2350 CRASSERT(pClient->conn->pBuffer==NULL && pClient->conn->cbBuffer==0);
2351 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
2352
2353 pClient->conn->pBuffer = pBuffer;
2354 pClient->conn->cbBuffer = cbBuffer;
2355 CRVBOXHGSMI_CMDDATA_SET(&pClient->conn->CmdData, pCmd, pHdr);
2356 rc = crVBoxServerInternalClientWriteRead(pClient);
2357 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
2358 return rc;
2359 }
2360 else
2361 {
2362 crWarning("invalid number of args");
2363 rc = VERR_INVALID_PARAMETER;
2364 break;
2365 }
2366 break;
2367 }
2368
2369 case SHCRGL_GUEST_FN_INJECT:
2370 {
2371 crDebug(("svcCall: SHCRGL_GUEST_FN_INJECT\n"));
2372
2373 /* @todo: Verify */
2374 if (cParams == 1)
2375 {
2376 CRVBOXHGSMIINJECT *pFnCmd = (CRVBOXHGSMIINJECT*)pHdr;
2377 /* Fetch parameters. */
2378 uint32_t u32InjectClientID = pFnCmd->u32ClientID;
2379 VBOXVDMACMD_CHROMIUM_BUFFER *pBuf = &pCmd->aBuffers[1];
2380 uint32_t cbBuffer = pBuf->cbBuffer;
2381 uint8_t *pBuffer = VBOXCRHGSMI_PTR_SAFE(pBuf->offBuffer, cbBuffer, uint8_t);
2382
2383 if (cbHdr < sizeof (*pFnCmd))
2384 {
2385 crWarning("invalid inject cmd buffer size!");
2386 rc = VERR_INVALID_PARAMETER;
2387 break;
2388 }
2389
2390 CRASSERT(cbBuffer);
2391 if (!pBuffer)
2392 {
2393 crWarning("invalid buffer data received from guest!");
2394 rc = VERR_INVALID_PARAMETER;
2395 break;
2396 }
2397
2398 rc = crVBoxServerClientGet(u32InjectClientID, &pClient);
2399 if (RT_FAILURE(rc))
2400 {
2401 break;
2402 }
2403
2404 /* This should never fire unless we start to multithread */
2405 CRASSERT(pClient->conn->pBuffer==NULL && pClient->conn->cbBuffer==0);
2406 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
2407
2408 pClient->conn->pBuffer = pBuffer;
2409 pClient->conn->cbBuffer = cbBuffer;
2410 CRVBOXHGSMI_CMDDATA_SET(&pClient->conn->CmdData, pCmd, pHdr);
2411 rc = crVBoxServerInternalClientWriteRead(pClient);
2412 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
2413 return rc;
2414 }
2415
2416 crWarning("invalid number of args");
2417 rc = VERR_INVALID_PARAMETER;
2418 break;
2419 }
2420
2421 case SHCRGL_GUEST_FN_READ:
2422 {
2423 crDebug(("svcCall: SHCRGL_GUEST_FN_READ\n"));
2424
2425 /* @todo: Verify */
2426 if (cParams == 1)
2427 {
2428 CRVBOXHGSMIREAD *pFnCmd = (CRVBOXHGSMIREAD*)pHdr;
2429 VBOXVDMACMD_CHROMIUM_BUFFER *pBuf = &pCmd->aBuffers[1];
2430 /* Fetch parameters. */
2431 uint32_t cbBuffer = pBuf->cbBuffer;
2432 uint8_t *pBuffer = VBOXCRHGSMI_PTR_SAFE(pBuf->offBuffer, cbBuffer, uint8_t);
2433
2434 if (cbHdr < sizeof (*pFnCmd))
2435 {
2436 crWarning("invalid read cmd buffer size!");
2437 rc = VERR_INVALID_PARAMETER;
2438 break;
2439 }
2440
2441
2442 if (!pBuffer)
2443 {
2444 crWarning("invalid buffer data received from guest!");
2445 rc = VERR_INVALID_PARAMETER;
2446 break;
2447 }
2448
2449 rc = crVBoxServerClientGet(u32ClientID, &pClient);
2450 if (RT_FAILURE(rc))
2451 {
2452 break;
2453 }
2454
2455 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
2456
2457 rc = crVBoxServerInternalClientRead(pClient, pBuffer, &cbBuffer);
2458
2459 /* Return the required buffer size always */
2460 pFnCmd->cbBuffer = cbBuffer;
2461
2462 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
2463
2464 /* the read command is never pended, complete it right away */
2465 pHdr->result = rc;
2466 crServerCrHgsmiCmdComplete(pCmd, VINF_SUCCESS);
2467 return VINF_SUCCESS;
2468 }
2469
2470 crWarning("invalid number of args");
2471 rc = VERR_INVALID_PARAMETER;
2472 break;
2473 }
2474
2475 case SHCRGL_GUEST_FN_WRITE_READ:
2476 {
2477 crDebug(("svcCall: SHCRGL_GUEST_FN_WRITE_READ\n"));
2478
2479 /* @todo: Verify */
2480 if (cParams == 2)
2481 {
2482 CRVBOXHGSMIWRITEREAD *pFnCmd = (CRVBOXHGSMIWRITEREAD*)pHdr;
2483 VBOXVDMACMD_CHROMIUM_BUFFER *pBuf = &pCmd->aBuffers[1];
2484 VBOXVDMACMD_CHROMIUM_BUFFER *pWbBuf = &pCmd->aBuffers[2];
2485
2486 /* Fetch parameters. */
2487 uint32_t cbBuffer = pBuf->cbBuffer;
2488 uint8_t *pBuffer = VBOXCRHGSMI_PTR_SAFE(pBuf->offBuffer, cbBuffer, uint8_t);
2489
2490 uint32_t cbWriteback = pWbBuf->cbBuffer;
2491 char *pWriteback = VBOXCRHGSMI_PTR_SAFE(pWbBuf->offBuffer, cbWriteback, char);
2492
2493 if (cbHdr < sizeof (*pFnCmd))
2494 {
2495 crWarning("invalid write_read cmd buffer size!");
2496 rc = VERR_INVALID_PARAMETER;
2497 break;
2498 }
2499
2500
2501 CRASSERT(cbBuffer);
2502 if (!pBuffer)
2503 {
2504 crWarning("invalid write buffer data received from guest!");
2505 rc = VERR_INVALID_PARAMETER;
2506 break;
2507 }
2508
2509 CRASSERT(cbWriteback);
2510 if (!pWriteback)
2511 {
2512 crWarning("invalid writeback buffer data received from guest!");
2513 rc = VERR_INVALID_PARAMETER;
2514 break;
2515 }
2516 rc = crVBoxServerClientGet(u32ClientID, &pClient);
2517 if (RT_FAILURE(rc))
2518 {
2519 pHdr->result = rc;
2520 crServerCrHgsmiCmdComplete(pCmd, VINF_SUCCESS);
2521 return rc;
2522 }
2523
2524 /* This should never fire unless we start to multithread */
2525 CRASSERT(pClient->conn->pBuffer==NULL && pClient->conn->cbBuffer==0);
2526 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
2527
2528 pClient->conn->pBuffer = pBuffer;
2529 pClient->conn->cbBuffer = cbBuffer;
2530 CRVBOXHGSMI_CMDDATA_SETWB(&pClient->conn->CmdData, pCmd, pHdr, pWriteback, cbWriteback, &pFnCmd->cbWriteback);
2531 rc = crVBoxServerInternalClientWriteRead(pClient);
2532 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
2533 return rc;
2534 }
2535
2536 crWarning("invalid number of args");
2537 rc = VERR_INVALID_PARAMETER;
2538 break;
2539 }
2540
2541 case SHCRGL_GUEST_FN_SET_VERSION:
2542 {
2543 crWarning("invalid function");
2544 rc = VERR_NOT_IMPLEMENTED;
2545 break;
2546 }
2547
2548 case SHCRGL_GUEST_FN_SET_PID:
2549 {
2550 crWarning("invalid function");
2551 rc = VERR_NOT_IMPLEMENTED;
2552 break;
2553 }
2554
2555 default:
2556 {
2557 crWarning("invalid function");
2558 rc = VERR_NOT_IMPLEMENTED;
2559 break;
2560 }
2561
2562 }
2563
2564 /* we can be on fail only here */
2565 CRASSERT(RT_FAILURE(rc));
2566 pHdr->result = rc;
2567 crServerCrHgsmiCmdComplete(pCmd, VINF_SUCCESS);
2568 return rc;
2569}
2570
2571int32_t crVBoxServerCrHgsmiCtl(struct VBOXVDMACMD_CHROMIUM_CTL *pCtl, uint32_t cbCtl)
2572{
2573 int rc = VINF_SUCCESS;
2574
2575 switch (pCtl->enmType)
2576 {
2577 case VBOXVDMACMD_CHROMIUM_CTL_TYPE_CRHGSMI_SETUP:
2578 {
2579 PVBOXVDMACMD_CHROMIUM_CTL_CRHGSMI_SETUP pSetup = (PVBOXVDMACMD_CHROMIUM_CTL_CRHGSMI_SETUP)pCtl;
2580 g_pvVRamBase = (uint8_t*)pSetup->pvVRamBase;
2581 g_cbVRam = pSetup->cbVRam;
2582 rc = VINF_SUCCESS;
2583 break;
2584 }
2585 case VBOXVDMACMD_CHROMIUM_CTL_TYPE_SAVESTATE_BEGIN:
2586 case VBOXVDMACMD_CHROMIUM_CTL_TYPE_SAVESTATE_END:
2587 rc = VINF_SUCCESS;
2588 break;
2589 case VBOXVDMACMD_CHROMIUM_CTL_TYPE_CRHGSMI_SETUP_COMPLETION:
2590 {
2591 PVBOXVDMACMD_CHROMIUM_CTL_CRHGSMI_SETUP_COMPLETION pSetup = (PVBOXVDMACMD_CHROMIUM_CTL_CRHGSMI_SETUP_COMPLETION)pCtl;
2592 g_hCrHgsmiCompletion = pSetup->hCompletion;
2593 g_pfnCrHgsmiCompletion = pSetup->pfnCompletion;
2594 rc = VINF_SUCCESS;
2595 break;
2596 }
2597 default:
2598 AssertMsgFailed(("invalid param %d", pCtl->enmType));
2599 rc = VERR_INVALID_PARAMETER;
2600 }
2601
2602 /* NOTE: Control commands can NEVER be pended here, this is why its a task of a caller (Main)
2603 * to complete them accordingly.
2604 * This approach allows using host->host and host->guest commands in the same way here
2605 * making the command completion to be the responsibility of the command originator.
2606 * E.g. ctl commands can be both Hgcm Host synchronous commands that do not require completion at all,
2607 * or Hgcm Host Fast Call commands that do require completion. All this details are hidden here */
2608 return rc;
2609}
2610#endif
Note: See TracBrowser for help on using the repository browser.

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