VirtualBox

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

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

crOpenGL: more on new command submission

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 117.3 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#include <VBox/log.h>
31
32#ifdef VBOXCR_LOGFPS
33#include <iprt/timer.h>
34#endif
35
36#ifdef VBOX_WITH_CRHGSMI
37# include <VBox/HostServices/VBoxCrOpenGLSvc.h>
38uint8_t* g_pvVRamBase = NULL;
39uint32_t g_cbVRam = 0;
40HCRHGSMICMDCOMPLETION g_hCrHgsmiCompletion = NULL;
41PFNCRHGSMICMDCOMPLETION g_pfnCrHgsmiCompletion = NULL;
42#endif
43
44/**
45 * \mainpage CrServerLib
46 *
47 * \section CrServerLibIntroduction Introduction
48 *
49 * Chromium consists of all the top-level files in the cr
50 * directory. The core module basically takes care of API dispatch,
51 * and OpenGL state management.
52 */
53
54
55/**
56 * CRServer global data
57 */
58CRServer cr_server;
59
60int tearingdown = 0; /* can't be static */
61
62DECLINLINE(int32_t) crVBoxServerClientGet(uint32_t u32ClientID, CRClient **ppClient)
63{
64 CRClient *pClient = NULL;
65 int32_t i;
66
67 *ppClient = NULL;
68
69 for (i = 0; i < cr_server.numClients; i++)
70 {
71 if (cr_server.clients[i] && cr_server.clients[i]->conn
72 && cr_server.clients[i]->conn->u32ClientID==u32ClientID)
73 {
74 pClient = cr_server.clients[i];
75 break;
76 }
77 }
78 if (!pClient)
79 {
80 crWarning("client not found!");
81 return VERR_INVALID_PARAMETER;
82 }
83
84 if (!pClient->conn->vMajor)
85 {
86 crWarning("no major version specified for client!");
87 return VERR_NOT_SUPPORTED;
88 }
89
90 *ppClient = pClient;
91
92 return VINF_SUCCESS;
93}
94
95
96/**
97 * Return pointer to server's first SPU.
98 */
99SPU*
100crServerHeadSPU(void)
101{
102 return cr_server.head_spu;
103}
104
105
106
107static void DeleteBarrierCallback( void *data )
108{
109 CRServerBarrier *barrier = (CRServerBarrier *) data;
110 crFree(barrier->waiting);
111 crFree(barrier);
112}
113
114
115static void deleteContextInfoCallback( void *data )
116{
117 CRContextInfo *c = (CRContextInfo *) data;
118 crStateDestroyContext(c->pContext);
119 if (c->CreateInfo.pszDpyName)
120 crFree(c->CreateInfo.pszDpyName);
121 crFree(c);
122}
123
124static void deleteMuralInfoCallback( void *data )
125{
126 CRMuralInfo *m = (CRMuralInfo *) data;
127 if (m->spuWindow != CR_RENDER_DEFAULT_WINDOW_ID) /* <- do not do term for default mural as it does not contain any info to be freed,
128 * and renderspu will destroy it up itself*/
129 {
130 crServerMuralTerm(m);
131 }
132 crFree(m);
133}
134
135static void crServerTearDown( void )
136{
137 GLint i;
138 CRClientNode *pNode, *pNext;
139 GLboolean fOldEnableDiff;
140
141 /* avoid a race condition */
142 if (tearingdown)
143 return;
144
145 tearingdown = 1;
146
147 crStateSetCurrent( NULL );
148
149 cr_server.curClient = NULL;
150 cr_server.run_queue = NULL;
151
152 crFree( cr_server.overlap_intens );
153 cr_server.overlap_intens = NULL;
154
155 /* needed to make sure window dummy mural not get created on mural destruction
156 * and generally this should be zeroed up */
157 cr_server.currentCtxInfo = NULL;
158 cr_server.currentWindow = -1;
159 cr_server.currentNativeWindow = 0;
160 cr_server.currentMural = NULL;
161
162 /* sync our state with renderspu,
163 * do it before mural & context deletion to avoid deleting currently set murals/contexts*/
164 cr_server.head_spu->dispatch_table.MakeCurrent(0, 0, 0);
165
166 /* Deallocate all semaphores */
167 crFreeHashtable(cr_server.semaphores, crFree);
168 cr_server.semaphores = NULL;
169
170 /* Deallocate all barriers */
171 crFreeHashtable(cr_server.barriers, DeleteBarrierCallback);
172 cr_server.barriers = NULL;
173
174 /* Free all context info */
175 crFreeHashtable(cr_server.contextTable, deleteContextInfoCallback);
176
177 /* synchronize with reality */
178 fOldEnableDiff = crStateEnableDiffOnMakeCurrent(GL_FALSE);
179 if(cr_server.MainContextInfo.pContext)
180 crStateMakeCurrent(cr_server.MainContextInfo.pContext);
181 crStateEnableDiffOnMakeCurrent(fOldEnableDiff);
182
183 /* Free vertex programs */
184 crFreeHashtable(cr_server.programTable, crFree);
185
186 /* Free murals */
187 crFreeHashtable(cr_server.muralTable, deleteMuralInfoCallback);
188
189 CrPMgrTerm();
190
191 if (CrBltIsInitialized(&cr_server.Blitter))
192 {
193 CrBltTerm(&cr_server.Blitter);
194 }
195
196 /* Free dummy murals */
197 crFreeHashtable(cr_server.dummyMuralTable, deleteMuralInfoCallback);
198
199 for (i = 0; i < cr_server.numClients; i++) {
200 if (cr_server.clients[i]) {
201 CRConnection *conn = cr_server.clients[i]->conn;
202 crNetFreeConnection(conn);
203 crFree(cr_server.clients[i]);
204 }
205 }
206 cr_server.numClients = 0;
207
208 pNode = cr_server.pCleanupClient;
209 while (pNode)
210 {
211 pNext=pNode->next;
212 crFree(pNode->pClient);
213 crFree(pNode);
214 pNode=pNext;
215 }
216 cr_server.pCleanupClient = NULL;
217
218 if (crServerRpwIsInitialized(&cr_server.RpwWorker))
219 {
220 crServerRpwTerm(&cr_server.RpwWorker);
221 }
222
223#if 1
224 /* disable these two lines if trying to get stack traces with valgrind */
225 crSPUUnloadChain(cr_server.head_spu);
226 cr_server.head_spu = NULL;
227#endif
228
229 crStateDestroy();
230
231 crNetTearDown();
232
233 VBoxVrListClear(&cr_server.RootVr);
234
235 VBoxVrTerm();
236}
237
238static void crServerClose( unsigned int id )
239{
240 crError( "Client disconnected!" );
241 (void) id;
242}
243
244static void crServerCleanup( int sigio )
245{
246 crServerTearDown();
247
248 tearingdown = 0;
249}
250
251
252void
253crServerSetPort(int port)
254{
255 cr_server.tcpip_port = port;
256}
257
258
259
260static void
261crPrintHelp(void)
262{
263 printf("Usage: crserver [OPTIONS]\n");
264 printf("Options:\n");
265 printf(" -mothership URL Specifies URL for contacting the mothership.\n");
266 printf(" URL is of the form [protocol://]hostname[:port]\n");
267 printf(" -port N Specifies the port number this server will listen to.\n");
268 printf(" -help Prints this information.\n");
269}
270
271
272/**
273 * Do CRServer initializations. After this, we can begin servicing clients.
274 */
275void
276crServerInit(int argc, char *argv[])
277{
278 int i;
279 const char*env;
280 char *mothership = NULL;
281 CRMuralInfo *defaultMural;
282 int rc = VBoxVrInit();
283 if (!RT_SUCCESS(rc))
284 {
285 crWarning("VBoxVrInit failed, rc %d", rc);
286 return;
287 }
288
289 for (i = 1 ; i < argc ; i++)
290 {
291 if (!crStrcmp( argv[i], "-mothership" ))
292 {
293 if (i == argc - 1)
294 {
295 crError( "-mothership requires an argument" );
296 }
297 mothership = argv[i+1];
298 i++;
299 }
300 else if (!crStrcmp( argv[i], "-port" ))
301 {
302 /* This is the port on which we'll accept client connections */
303 if (i == argc - 1)
304 {
305 crError( "-port requires an argument" );
306 }
307 cr_server.tcpip_port = crStrToInt(argv[i+1]);
308 i++;
309 }
310 else if (!crStrcmp( argv[i], "-vncmode" ))
311 {
312 cr_server.vncMode = 1;
313 }
314 else if (!crStrcmp( argv[i], "-help" ))
315 {
316 crPrintHelp();
317 exit(0);
318 }
319 }
320
321 signal( SIGTERM, crServerCleanup );
322 signal( SIGINT, crServerCleanup );
323#ifndef WINDOWS
324 signal( SIGPIPE, SIG_IGN );
325#endif
326
327#if DEBUG_FP_EXCEPTIONS
328 {
329 fpu_control_t mask;
330 _FPU_GETCW(mask);
331 mask &= ~(_FPU_MASK_IM | _FPU_MASK_DM | _FPU_MASK_ZM
332 | _FPU_MASK_OM | _FPU_MASK_UM);
333 _FPU_SETCW(mask);
334 }
335#endif
336
337 cr_server.bUseMultipleContexts = (crGetenv( "CR_SERVER_ENABLE_MULTIPLE_CONTEXTS" ) != NULL);
338
339 if (cr_server.bUseMultipleContexts)
340 {
341 crInfo("Info: using multiple contexts!");
342 crDebug("Debug: using multiple contexts!");
343 }
344
345 cr_server.firstCallCreateContext = GL_TRUE;
346 cr_server.firstCallMakeCurrent = GL_TRUE;
347 cr_server.bForceMakeCurrentOnClientSwitch = GL_FALSE;
348
349 /*
350 * Create default mural info and hash table.
351 */
352 cr_server.muralTable = crAllocHashtable();
353 defaultMural = (CRMuralInfo *) crCalloc(sizeof(CRMuralInfo));
354 defaultMural->spuWindow = CR_RENDER_DEFAULT_WINDOW_ID;
355 crHashtableAdd(cr_server.muralTable, 0, defaultMural);
356
357 cr_server.programTable = crAllocHashtable();
358
359 crNetInit(crServerRecv, crServerClose);
360 crStateInit();
361
362 crServerSetVBoxConfiguration();
363
364 crStateLimitsInit( &(cr_server.limits) );
365
366 /*
367 * Default context
368 */
369 cr_server.contextTable = crAllocHashtable();
370 cr_server.curClient->currentCtxInfo = &cr_server.MainContextInfo;
371
372 cr_server.dummyMuralTable = crAllocHashtable();
373
374 CrPMgrInit();
375
376 cr_server.fRootVrOn = GL_FALSE;
377 VBoxVrListInit(&cr_server.RootVr);
378 crMemset(&cr_server.RootVrCurPoint, 0, sizeof (cr_server.RootVrCurPoint));
379
380 crMemset(&cr_server.RpwWorker, 0, sizeof (cr_server.RpwWorker));
381
382 env = crGetenv("CR_SERVER_BFB");
383 if (env)
384 {
385 cr_server.fBlitterMode = env[0] - '0';
386 }
387 else
388 {
389 cr_server.fBlitterMode = CR_SERVER_BFB_DISABLED;
390 }
391 crMemset(&cr_server.Blitter, 0, sizeof (cr_server.Blitter));
392
393 crServerInitDispatch();
394 crServerInitTmpCtxDispatch();
395 crStateDiffAPI( &(cr_server.head_spu->dispatch_table) );
396
397#ifdef VBOX_WITH_CRSERVER_DUMPER
398 crMemset(&cr_server.Recorder, 0, sizeof (cr_server.Recorder));
399 crMemset(&cr_server.RecorderBlitter, 0, sizeof (cr_server.RecorderBlitter));
400 crMemset(&cr_server.DbgPrintDumper, 0, sizeof (cr_server.DbgPrintDumper));
401 crMemset(&cr_server.HtmlDumper, 0, sizeof (cr_server.HtmlDumper));
402 cr_server.pDumper = NULL;
403#endif
404
405 crUnpackSetReturnPointer( &(cr_server.return_ptr) );
406 crUnpackSetWritebackPointer( &(cr_server.writeback_ptr) );
407
408 cr_server.barriers = crAllocHashtable();
409 cr_server.semaphores = crAllocHashtable();
410}
411
412void crVBoxServerTearDown(void)
413{
414 crServerTearDown();
415}
416
417/**
418 * Do CRServer initializations. After this, we can begin servicing clients.
419 */
420GLboolean crVBoxServerInit(void)
421{
422 CRMuralInfo *defaultMural;
423 const char*env;
424 int rc = VBoxVrInit();
425 if (!RT_SUCCESS(rc))
426 {
427 crWarning("VBoxVrInit failed, rc %d", rc);
428 return GL_FALSE;
429 }
430
431#if DEBUG_FP_EXCEPTIONS
432 {
433 fpu_control_t mask;
434 _FPU_GETCW(mask);
435 mask &= ~(_FPU_MASK_IM | _FPU_MASK_DM | _FPU_MASK_ZM
436 | _FPU_MASK_OM | _FPU_MASK_UM);
437 _FPU_SETCW(mask);
438 }
439#endif
440
441 cr_server.bUseMultipleContexts = (crGetenv( "CR_SERVER_ENABLE_MULTIPLE_CONTEXTS" ) != NULL);
442
443 if (cr_server.bUseMultipleContexts)
444 {
445 crInfo("Info: using multiple contexts!");
446 crDebug("Debug: using multiple contexts!");
447 }
448
449 crNetInit(crServerRecv, crServerClose);
450
451 cr_server.firstCallCreateContext = GL_TRUE;
452 cr_server.firstCallMakeCurrent = GL_TRUE;
453
454 cr_server.bIsInLoadingState = GL_FALSE;
455 cr_server.bIsInSavingState = GL_FALSE;
456 cr_server.bForceMakeCurrentOnClientSwitch = GL_FALSE;
457
458 cr_server.pCleanupClient = NULL;
459
460 /*
461 * Create default mural info and hash table.
462 */
463 cr_server.muralTable = crAllocHashtable();
464 defaultMural = (CRMuralInfo *) crCalloc(sizeof(CRMuralInfo));
465 defaultMural->spuWindow = CR_RENDER_DEFAULT_WINDOW_ID;
466 crHashtableAdd(cr_server.muralTable, 0, defaultMural);
467
468 cr_server.programTable = crAllocHashtable();
469
470 crStateInit();
471
472 crStateLimitsInit( &(cr_server.limits) );
473
474 cr_server.barriers = crAllocHashtable();
475 cr_server.semaphores = crAllocHashtable();
476
477 crUnpackSetReturnPointer( &(cr_server.return_ptr) );
478 crUnpackSetWritebackPointer( &(cr_server.writeback_ptr) );
479
480 /*
481 * Default context
482 */
483 cr_server.contextTable = crAllocHashtable();
484
485 cr_server.dummyMuralTable = crAllocHashtable();
486
487 CrPMgrInit();
488
489 cr_server.fRootVrOn = GL_FALSE;
490 VBoxVrListInit(&cr_server.RootVr);
491 crMemset(&cr_server.RootVrCurPoint, 0, sizeof (cr_server.RootVrCurPoint));
492
493 crMemset(&cr_server.RpwWorker, 0, sizeof (cr_server.RpwWorker));
494
495 env = crGetenv("CR_SERVER_BFB");
496 if (env)
497 {
498 cr_server.fBlitterMode = env[0] - '0';
499 }
500 else
501 {
502 cr_server.fBlitterMode = CR_SERVER_BFB_DISABLED;
503 }
504 crMemset(&cr_server.Blitter, 0, sizeof (cr_server.Blitter));
505
506 crServerSetVBoxConfigurationHGCM();
507
508 if (!cr_server.head_spu)
509 return GL_FALSE;
510
511 crServerInitDispatch();
512 crServerInitTmpCtxDispatch();
513 crStateDiffAPI( &(cr_server.head_spu->dispatch_table) );
514
515#ifdef VBOX_WITH_CRSERVER_DUMPER
516 crMemset(&cr_server.Recorder, 0, sizeof (cr_server.Recorder));
517 crMemset(&cr_server.RecorderBlitter, 0, sizeof (cr_server.RecorderBlitter));
518 crMemset(&cr_server.DbgPrintDumper, 0, sizeof (cr_server.DbgPrintDumper));
519 crMemset(&cr_server.HtmlDumper, 0, sizeof (cr_server.HtmlDumper));
520 cr_server.pDumper = NULL;
521#endif
522
523 /*Check for PBO support*/
524 if (crStateGetCurrent()->extensions.ARB_pixel_buffer_object)
525 {
526 cr_server.bUsePBOForReadback=GL_TRUE;
527 }
528
529 return GL_TRUE;
530}
531
532int32_t crVBoxServerAddClient(uint32_t u32ClientID)
533{
534 CRClient *newClient;
535
536 if (cr_server.numClients>=CR_MAX_CLIENTS)
537 {
538 return VERR_MAX_THRDS_REACHED;
539 }
540
541 newClient = (CRClient *) crCalloc(sizeof(CRClient));
542 crDebug("crServer: AddClient u32ClientID=%d", u32ClientID);
543
544 newClient->spu_id = 0;
545 newClient->currentCtxInfo = &cr_server.MainContextInfo;
546 newClient->currentContextNumber = -1;
547 newClient->conn = crNetAcceptClient(cr_server.protocol, NULL,
548 cr_server.tcpip_port,
549 cr_server.mtu, 0);
550 newClient->conn->u32ClientID = u32ClientID;
551
552 cr_server.clients[cr_server.numClients++] = newClient;
553
554 crServerAddToRunQueue(newClient);
555
556 return VINF_SUCCESS;
557}
558
559void crVBoxServerRemoveClient(uint32_t u32ClientID)
560{
561 CRClient *pClient=NULL;
562 int32_t i;
563
564 crDebug("crServer: RemoveClient u32ClientID=%d", u32ClientID);
565
566 for (i = 0; i < cr_server.numClients; i++)
567 {
568 if (cr_server.clients[i] && cr_server.clients[i]->conn
569 && cr_server.clients[i]->conn->u32ClientID==u32ClientID)
570 {
571 pClient = cr_server.clients[i];
572 break;
573 }
574 }
575 //if (!pClient) return VERR_INVALID_PARAMETER;
576 if (!pClient)
577 {
578 crWarning("Invalid client id %u passed to crVBoxServerRemoveClient", u32ClientID);
579 return;
580 }
581
582#ifdef VBOX_WITH_CRHGSMI
583 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
584#endif
585
586 /* Disconnect the client */
587 pClient->conn->Disconnect(pClient->conn);
588
589 /* Let server clear client from the queue */
590 crServerDeleteClient(pClient);
591}
592
593static int32_t crVBoxServerInternalClientWriteRead(CRClient *pClient)
594{
595#ifdef VBOXCR_LOGFPS
596 uint64_t tstart, tend;
597#endif
598
599 /*crDebug("=>crServer: ClientWrite u32ClientID=%d", u32ClientID);*/
600
601
602#ifdef VBOXCR_LOGFPS
603 tstart = RTTimeNanoTS();
604#endif
605
606 /* This should be setup already */
607 CRASSERT(pClient->conn->pBuffer);
608 CRASSERT(pClient->conn->cbBuffer);
609#ifdef VBOX_WITH_CRHGSMI
610 CRVBOXHGSMI_CMDDATA_ASSERT_CONSISTENT(&pClient->conn->CmdData);
611#endif
612
613 if (
614#ifdef VBOX_WITH_CRHGSMI
615 !CRVBOXHGSMI_CMDDATA_IS_SET(&pClient->conn->CmdData) &&
616#endif
617 cr_server.run_queue->client != pClient
618 && crServerClientInBeginEnd(cr_server.run_queue->client))
619 {
620 crDebug("crServer: client %d blocked, allow_redir_ptr = 0", pClient->conn->u32ClientID);
621 pClient->conn->allow_redir_ptr = 0;
622 }
623 else
624 {
625 pClient->conn->allow_redir_ptr = 1;
626 }
627
628 crNetRecv();
629 CRASSERT(pClient->conn->pBuffer==NULL && pClient->conn->cbBuffer==0);
630 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
631
632 crServerServiceClients();
633
634#if 0
635 if (pClient->currentMural) {
636 crStateViewport( 0, 0, 500, 500 );
637 pClient->currentMural->viewportValidated = GL_FALSE;
638 cr_server.head_spu->dispatch_table.Viewport( 0, 0, 500, 500 );
639 crStateViewport( 0, 0, 600, 600 );
640 pClient->currentMural->viewportValidated = GL_FALSE;
641 cr_server.head_spu->dispatch_table.Viewport( 0, 0, 600, 600 );
642
643 crStateMatrixMode(GL_PROJECTION);
644 cr_server.head_spu->dispatch_table.MatrixMode(GL_PROJECTION);
645 crServerDispatchLoadIdentity();
646 crStateFrustum(-0.6, 0.6, -0.5, 0.5, 1.5, 150.0);
647 cr_server.head_spu->dispatch_table.Frustum(-0.6, 0.6, -0.5, 0.5, 1.5, 150.0);
648 crServerDispatchLoadIdentity();
649 crStateFrustum(-0.5, 0.5, -0.5, 0.5, 1.5, 150.0);
650 cr_server.head_spu->dispatch_table.Frustum(-0.5, 0.5, -0.5, 0.5, 1.5, 150.0);
651
652 crStateMatrixMode(GL_MODELVIEW);
653 cr_server.head_spu->dispatch_table.MatrixMode(GL_MODELVIEW);
654 crServerDispatchLoadIdentity();
655 crStateFrustum(-0.5, 0.5, -0.5, 0.5, 1.5, 150.0);
656 cr_server.head_spu->dispatch_table.Frustum(-0.5, 0.5, -0.5, 0.5, 1.5, 150.0);
657 crServerDispatchLoadIdentity();
658 }
659#endif
660
661 crStateResetCurrentPointers(&cr_server.current);
662
663#ifndef VBOX_WITH_CRHGSMI
664 CRASSERT(!pClient->conn->allow_redir_ptr || crNetNumMessages(pClient->conn)==0);
665#endif
666
667#ifdef VBOXCR_LOGFPS
668 tend = RTTimeNanoTS();
669 pClient->timeUsed += tend-tstart;
670#endif
671 /*crDebug("<=crServer: ClientWrite u32ClientID=%d", u32ClientID);*/
672
673 return VINF_SUCCESS;
674}
675
676
677int32_t crVBoxServerClientWrite(uint32_t u32ClientID, uint8_t *pBuffer, uint32_t cbBuffer)
678{
679 CRClient *pClient=NULL;
680 int32_t rc = crVBoxServerClientGet(u32ClientID, &pClient);
681
682 if (RT_FAILURE(rc))
683 return rc;
684
685
686 CRASSERT(pBuffer);
687
688 /* This should never fire unless we start to multithread */
689 CRASSERT(pClient->conn->pBuffer==NULL && pClient->conn->cbBuffer==0);
690
691 pClient->conn->pBuffer = pBuffer;
692 pClient->conn->cbBuffer = cbBuffer;
693#ifdef VBOX_WITH_CRHGSMI
694 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
695#endif
696
697 return crVBoxServerInternalClientWriteRead(pClient);
698}
699
700int32_t crVBoxServerInternalClientRead(CRClient *pClient, uint8_t *pBuffer, uint32_t *pcbBuffer)
701{
702 if (pClient->conn->cbHostBuffer > *pcbBuffer)
703 {
704 crDebug("crServer: [%lx] ClientRead u32ClientID=%d FAIL, host buffer too small %d of %d",
705 crThreadID(), pClient->conn->u32ClientID, *pcbBuffer, pClient->conn->cbHostBuffer);
706
707 /* Return the size of needed buffer */
708 *pcbBuffer = pClient->conn->cbHostBuffer;
709
710 return VERR_BUFFER_OVERFLOW;
711 }
712
713 *pcbBuffer = pClient->conn->cbHostBuffer;
714
715 if (*pcbBuffer)
716 {
717 CRASSERT(pClient->conn->pHostBuffer);
718
719 crMemcpy(pBuffer, pClient->conn->pHostBuffer, *pcbBuffer);
720 pClient->conn->cbHostBuffer = 0;
721 }
722
723 return VINF_SUCCESS;
724}
725
726int32_t crVBoxServerClientRead(uint32_t u32ClientID, uint8_t *pBuffer, uint32_t *pcbBuffer)
727{
728 CRClient *pClient=NULL;
729 int32_t rc = crVBoxServerClientGet(u32ClientID, &pClient);
730
731 if (RT_FAILURE(rc))
732 return rc;
733
734#ifdef VBOX_WITH_CRHGSMI
735 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
736#endif
737
738 return crVBoxServerInternalClientRead(pClient, pBuffer, pcbBuffer);
739}
740
741extern DECLEXPORT(int32_t) crVBoxServerClientGetCaps(uint32_t u32ClientID, uint32_t *pu32Caps)
742{
743 *pu32Caps = cr_server.u32Caps;
744 return VINF_SUCCESS;
745}
746
747int32_t crVBoxServerClientSetVersion(uint32_t u32ClientID, uint32_t vMajor, uint32_t vMinor)
748{
749 CRClient *pClient=NULL;
750 int32_t i;
751
752 for (i = 0; i < cr_server.numClients; i++)
753 {
754 if (cr_server.clients[i] && cr_server.clients[i]->conn
755 && cr_server.clients[i]->conn->u32ClientID==u32ClientID)
756 {
757 pClient = cr_server.clients[i];
758 break;
759 }
760 }
761 if (!pClient) return VERR_INVALID_PARAMETER;
762
763 pClient->conn->vMajor = vMajor;
764 pClient->conn->vMinor = vMinor;
765
766 if (vMajor != CR_PROTOCOL_VERSION_MAJOR
767 || vMinor != CR_PROTOCOL_VERSION_MINOR)
768 {
769 return VERR_NOT_SUPPORTED;
770 }
771 else return VINF_SUCCESS;
772}
773
774int32_t crVBoxServerClientSetPID(uint32_t u32ClientID, uint64_t pid)
775{
776 CRClient *pClient=NULL;
777 int32_t i;
778
779 for (i = 0; i < cr_server.numClients; i++)
780 {
781 if (cr_server.clients[i] && cr_server.clients[i]->conn
782 && cr_server.clients[i]->conn->u32ClientID==u32ClientID)
783 {
784 pClient = cr_server.clients[i];
785 break;
786 }
787 }
788 if (!pClient) return VERR_INVALID_PARAMETER;
789
790 pClient->pid = pid;
791
792 return VINF_SUCCESS;
793}
794
795int
796CRServerMain(int argc, char *argv[])
797{
798 crServerInit(argc, argv);
799
800 crServerSerializeRemoteStreams();
801
802 crServerTearDown();
803
804 tearingdown = 0;
805
806 return 0;
807}
808
809static void crVBoxServerSaveMuralCB(unsigned long key, void *data1, void *data2)
810{
811 CRMuralInfo *pMI = (CRMuralInfo*) data1;
812 PSSMHANDLE pSSM = (PSSMHANDLE) data2;
813 int32_t rc;
814
815 CRASSERT(pMI && pSSM);
816
817 /* Don't store default mural */
818 if (!key) return;
819
820 rc = SSMR3PutMem(pSSM, &key, sizeof(key));
821 CRASSERT(rc == VINF_SUCCESS);
822
823 rc = SSMR3PutMem(pSSM, pMI, RT_OFFSETOF(CRMuralInfo, CreateInfo));
824 CRASSERT(rc == VINF_SUCCESS);
825
826 if (pMI->pVisibleRects)
827 {
828 rc = SSMR3PutMem(pSSM, pMI->pVisibleRects, 4*sizeof(GLint)*pMI->cVisibleRects);
829 }
830
831 rc = SSMR3PutMem(pSSM, pMI->ctxUsage, sizeof (pMI->ctxUsage));
832 CRASSERT(rc == VINF_SUCCESS);
833}
834
835/* @todo add hashtable walker with result info and intermediate abort */
836static void crVBoxServerSaveCreateInfoCB(unsigned long key, void *data1, void *data2)
837{
838 CRCreateInfo_t *pCreateInfo = (CRCreateInfo_t *)data1;
839 PSSMHANDLE pSSM = (PSSMHANDLE) data2;
840 int32_t rc;
841
842 CRASSERT(pCreateInfo && pSSM);
843
844 /* Don't store default mural create info */
845 if (!key) return;
846
847 rc = SSMR3PutMem(pSSM, &key, sizeof(key));
848 CRASSERT(rc == VINF_SUCCESS);
849
850 rc = SSMR3PutMem(pSSM, pCreateInfo, sizeof(*pCreateInfo));
851 CRASSERT(rc == VINF_SUCCESS);
852
853 if (pCreateInfo->pszDpyName)
854 {
855 rc = SSMR3PutStrZ(pSSM, pCreateInfo->pszDpyName);
856 CRASSERT(rc == VINF_SUCCESS);
857 }
858}
859
860static void crVBoxServerSaveCreateInfoFromMuralInfoCB(unsigned long key, void *data1, void *data2)
861{
862 CRMuralInfo *pMural = (CRMuralInfo *)data1;
863 CRCreateInfo_t CreateInfo;
864 CreateInfo.pszDpyName = pMural->CreateInfo.pszDpyName;
865 CreateInfo.visualBits = pMural->CreateInfo.requestedVisualBits;
866 CreateInfo.externalID = pMural->CreateInfo.externalID;
867 crVBoxServerSaveCreateInfoCB(key, &CreateInfo, data2);
868}
869
870static void crVBoxServerSaveCreateInfoFromCtxInfoCB(unsigned long key, void *data1, void *data2)
871{
872 CRContextInfo *pContextInfo = (CRContextInfo *)data1;
873 CRCreateInfo_t CreateInfo;
874 CreateInfo.pszDpyName = pContextInfo->CreateInfo.pszDpyName;
875 CreateInfo.visualBits = pContextInfo->CreateInfo.requestedVisualBits;
876 /* saved state contains internal id */
877 CreateInfo.externalID = pContextInfo->pContext->id;
878 crVBoxServerSaveCreateInfoCB(key, &CreateInfo, data2);
879}
880
881static void crVBoxServerSyncTextureCB(unsigned long key, void *data1, void *data2)
882{
883 CRTextureObj *pTexture = (CRTextureObj *) data1;
884 CRContext *pContext = (CRContext *) data2;
885
886 CRASSERT(pTexture && pContext);
887 crStateTextureObjectDiff(pContext, NULL, NULL, pTexture, GL_TRUE);
888}
889
890typedef struct CRVBOX_SAVE_STATE_GLOBAL
891{
892 /* context id -> mural association
893 * on context data save, each context will be made current with the corresponding mural from this table
894 * thus saving the mural front & back buffer data */
895 CRHashTable *contextMuralTable;
896 /* mural id -> context info
897 * for murals that do not have associated context in contextMuralTable
898 * we still need to save*/
899 CRHashTable *additionalMuralContextTable;
900
901 PSSMHANDLE pSSM;
902
903 int rc;
904} CRVBOX_SAVE_STATE_GLOBAL, *PCRVBOX_SAVE_STATE_GLOBAL;
905
906
907typedef struct CRVBOX_CTXWND_CTXWALKER_CB
908{
909 PCRVBOX_SAVE_STATE_GLOBAL pGlobal;
910 CRHashTable *usedMuralTable;
911 GLuint cAdditionalMurals;
912} CRVBOX_CTXWND_CTXWALKER_CB, *PCRVBOX_CTXWND_CTXWALKER_CB;
913
914static void crVBoxServerBuildAdditionalWindowContextMapCB(unsigned long key, void *data1, void *data2)
915{
916 CRMuralInfo * pMural = (CRMuralInfo *) data1;
917 PCRVBOX_CTXWND_CTXWALKER_CB pData = (PCRVBOX_CTXWND_CTXWALKER_CB)data2;
918 CRContextInfo *pContextInfo = NULL;
919
920 if (!pMural->CreateInfo.externalID)
921 {
922 CRASSERT(!key);
923 return;
924 }
925
926 if (crHashtableSearch(pData->usedMuralTable, pMural->CreateInfo.externalID))
927 {
928 Assert(crHashtableGetDataKey(pData->pGlobal->contextMuralTable, pMural, NULL));
929 return;
930 }
931
932 Assert(!crHashtableGetDataKey(pData->pGlobal->contextMuralTable, pMural, NULL));
933
934 if (cr_server.MainContextInfo.CreateInfo.realVisualBits == pMural->CreateInfo.realVisualBits)
935 {
936 pContextInfo = &cr_server.MainContextInfo;
937 }
938 else
939 {
940 crWarning("different visual bits not implemented!");
941 pContextInfo = &cr_server.MainContextInfo;
942 }
943
944 crHashtableAdd(pData->pGlobal->additionalMuralContextTable, pMural->CreateInfo.externalID, pContextInfo);
945}
946
947
948typedef struct CRVBOX_CTXWND_WNDWALKER_CB
949{
950 PCRVBOX_SAVE_STATE_GLOBAL pGlobal;
951 CRHashTable *usedMuralTable;
952 CRContextInfo *pContextInfo;
953 CRMuralInfo * pMural;
954} CRVBOX_CTXWND_WNDWALKER_CB, *PCRVBOX_CTXWND_WNDWALKER_CB;
955
956static void crVBoxServerBuildContextWindowMapWindowWalkerCB(unsigned long key, void *data1, void *data2)
957{
958 CRMuralInfo * pMural = (CRMuralInfo *) data1;
959 PCRVBOX_CTXWND_WNDWALKER_CB pData = (PCRVBOX_CTXWND_WNDWALKER_CB)data2;
960
961 Assert(pData->pMural != pMural);
962 Assert(pData->pContextInfo);
963
964 if (pData->pMural)
965 return;
966
967 if (!pMural->CreateInfo.externalID)
968 {
969 CRASSERT(!key);
970 return;
971 }
972
973 if (!CR_STATE_SHAREDOBJ_USAGE_IS_SET(pMural, pData->pContextInfo->pContext))
974 return;
975
976 if (crHashtableSearch(pData->usedMuralTable, pMural->CreateInfo.externalID))
977 return;
978
979 CRASSERT(pMural->CreateInfo.realVisualBits == pData->pContextInfo->CreateInfo.realVisualBits);
980 pData->pMural = pMural;
981}
982
983static void crVBoxServerBuildContextUsedWindowMapCB(unsigned long key, void *data1, void *data2)
984{
985 CRContextInfo *pContextInfo = (CRContextInfo *)data1;
986 PCRVBOX_CTXWND_CTXWALKER_CB pData = (PCRVBOX_CTXWND_CTXWALKER_CB)data2;
987
988 if (!pContextInfo->currentMural)
989 return;
990
991 crHashtableAdd(pData->pGlobal->contextMuralTable, pContextInfo->CreateInfo.externalID, pContextInfo->currentMural);
992 crHashtableAdd(pData->usedMuralTable, pContextInfo->currentMural->CreateInfo.externalID, pContextInfo->currentMural);
993}
994
995CRMuralInfo * crServerGetDummyMural(GLint visualBits)
996{
997 CRMuralInfo * pMural = (CRMuralInfo *)crHashtableSearch(cr_server.dummyMuralTable, visualBits);
998 if (!pMural)
999 {
1000 GLint id;
1001 pMural = (CRMuralInfo *) crCalloc(sizeof(CRMuralInfo));
1002 if (!pMural)
1003 {
1004 crWarning("crCalloc failed!");
1005 return NULL;
1006 }
1007 id = crServerMuralInit(pMural, "", visualBits, 0);
1008 if (id < 0)
1009 {
1010 crWarning("crServerMuralInit failed!");
1011 crFree(pMural);
1012 return NULL;
1013 }
1014
1015 crHashtableAdd(cr_server.dummyMuralTable, visualBits, pMural);
1016 }
1017
1018 return pMural;
1019}
1020
1021static void crVBoxServerBuildContextUnusedWindowMapCB(unsigned long key, void *data1, void *data2)
1022{
1023 CRContextInfo *pContextInfo = (CRContextInfo *)data1;
1024 PCRVBOX_CTXWND_CTXWALKER_CB pData = (PCRVBOX_CTXWND_CTXWALKER_CB)data2;
1025 CRMuralInfo * pMural = NULL;
1026
1027 if (pContextInfo->currentMural)
1028 return;
1029
1030 Assert(crHashtableNumElements(pData->pGlobal->contextMuralTable) <= crHashtableNumElements(cr_server.muralTable) - 1);
1031 if (crHashtableNumElements(pData->pGlobal->contextMuralTable) < crHashtableNumElements(cr_server.muralTable) - 1)
1032 {
1033 CRVBOX_CTXWND_WNDWALKER_CB MuralData;
1034 MuralData.pGlobal = pData->pGlobal;
1035 MuralData.usedMuralTable = pData->usedMuralTable;
1036 MuralData.pContextInfo = pContextInfo;
1037 MuralData.pMural = NULL;
1038
1039 crHashtableWalk(cr_server.muralTable, crVBoxServerBuildContextWindowMapWindowWalkerCB, &MuralData);
1040
1041 pMural = MuralData.pMural;
1042
1043 }
1044
1045 if (!pMural)
1046 {
1047 pMural = crServerGetDummyMural(pContextInfo->CreateInfo.realVisualBits);
1048 if (!pMural)
1049 {
1050 crWarning("crServerGetDummyMural failed");
1051 return;
1052 }
1053 }
1054 else
1055 {
1056 crHashtableAdd(pData->usedMuralTable, pMural->CreateInfo.externalID, pMural);
1057 ++pData->cAdditionalMurals;
1058 }
1059
1060 crHashtableAdd(pData->pGlobal->contextMuralTable, pContextInfo->CreateInfo.externalID, pMural);
1061}
1062
1063static void crVBoxServerBuildSaveStateGlobal(PCRVBOX_SAVE_STATE_GLOBAL pGlobal)
1064{
1065 CRVBOX_CTXWND_CTXWALKER_CB Data;
1066 GLuint cMurals;
1067 pGlobal->contextMuralTable = crAllocHashtable();
1068 pGlobal->additionalMuralContextTable = crAllocHashtable();
1069 /* 1. go through all contexts and match all having currentMural set */
1070 Data.pGlobal = pGlobal;
1071 Data.usedMuralTable = crAllocHashtable();
1072 Data.cAdditionalMurals = 0;
1073 crHashtableWalk(cr_server.contextTable, crVBoxServerBuildContextUsedWindowMapCB, &Data);
1074
1075 cMurals = crHashtableNumElements(pGlobal->contextMuralTable);
1076 CRASSERT(cMurals <= crHashtableNumElements(cr_server.contextTable));
1077 CRASSERT(cMurals <= crHashtableNumElements(cr_server.muralTable) - 1);
1078 CRASSERT(cMurals == crHashtableNumElements(Data.usedMuralTable));
1079 if (cMurals < crHashtableNumElements(cr_server.contextTable))
1080 {
1081 Data.cAdditionalMurals = 0;
1082 crHashtableWalk(cr_server.contextTable, crVBoxServerBuildContextUnusedWindowMapCB, &Data);
1083 }
1084
1085 CRASSERT(crHashtableNumElements(pGlobal->contextMuralTable) == crHashtableNumElements(cr_server.contextTable));
1086 CRASSERT(cMurals + Data.cAdditionalMurals <= crHashtableNumElements(cr_server.muralTable) - 1);
1087 if (cMurals + Data.cAdditionalMurals < crHashtableNumElements(cr_server.muralTable) - 1)
1088 {
1089 crHashtableWalk(cr_server.muralTable, crVBoxServerBuildAdditionalWindowContextMapCB, &Data);
1090 CRASSERT(cMurals + Data.cAdditionalMurals + crHashtableNumElements(pGlobal->additionalMuralContextTable) == crHashtableNumElements(cr_server.muralTable) - 1);
1091 }
1092
1093 crFreeHashtable(Data.usedMuralTable, NULL);
1094}
1095
1096static void crVBoxServerFBImageDataTerm(CRFBData *pData)
1097{
1098 GLuint i;
1099 for (i = 0; i < pData->cElements; ++i)
1100 {
1101 CRFBDataElement * pEl = &pData->aElements[i];
1102 if (pEl->pvData)
1103 {
1104 crFree(pEl->pvData);
1105 /* sanity */
1106 pEl->pvData = NULL;
1107 }
1108 }
1109 pData->cElements = 0;
1110}
1111
1112static int crVBoxServerFBImageDataInitEx(CRFBData *pData, CRContextInfo *pCtxInfo, CRMuralInfo *pMural, GLboolean fWrite, uint32_t version, GLuint overrideWidth, GLuint overrideHeight)
1113{
1114 CRContext *pContext;
1115 GLuint i;
1116 GLfloat *pF;
1117 CRFBDataElement *pEl;
1118 GLuint width;
1119 GLuint height;
1120
1121 crMemset(pData, 0, sizeof (*pData));
1122
1123 pContext = pCtxInfo->pContext;
1124
1125 /* the version should be always actual when we do reads,
1126 * i.e. it could differ on writes when snapshot is getting loaded */
1127 CRASSERT(fWrite || version == SHCROGL_SSM_VERSION);
1128
1129 width = overrideWidth ? overrideWidth : pMural->width;
1130 height = overrideHeight ? overrideHeight : pMural->height;
1131
1132 if (!width || !height)
1133 return VINF_SUCCESS;
1134
1135 if (pMural)
1136 {
1137 if (fWrite)
1138 {
1139 if (!pContext->framebufferobject.drawFB)
1140 pData->idOverrrideFBO = CR_SERVER_FBO_FOR_IDX(pMural, pMural->iCurDrawBuffer);
1141 }
1142 else
1143 {
1144 if (!pContext->framebufferobject.readFB)
1145 pData->idOverrrideFBO = CR_SERVER_FBO_FOR_IDX(pMural, pMural->iCurReadBuffer);
1146 }
1147 }
1148 pData->cElements = 0;
1149
1150 pEl = &pData->aElements[pData->cElements];
1151 pEl->idFBO = pMural && pMural->fRedirected ? pMural->aidFBOs[CR_SERVER_FBO_FB_IDX(pMural)] : 0;
1152 pEl->enmBuffer = pData->aElements[1].idFBO ? GL_COLOR_ATTACHMENT0 : GL_FRONT;
1153 pEl->posX = 0;
1154 pEl->posY = 0;
1155 pEl->width = width;
1156 pEl->height = height;
1157 pEl->enmFormat = GL_RGBA;
1158 pEl->enmType = GL_UNSIGNED_BYTE;
1159 pEl->cbData = width * height * 4;
1160 pEl->pvData = crCalloc(pEl->cbData);
1161 if (!pEl->pvData)
1162 {
1163 crVBoxServerFBImageDataTerm(pData);
1164 crWarning("crVBoxServerFBImageDataInit: crCalloc failed");
1165 return VERR_NO_MEMORY;
1166 }
1167 ++pData->cElements;
1168
1169 /* there is a lot of code that assumes we have double buffering, just assert here to print a warning in the log
1170 * so that we know that something irregular is going on */
1171 CRASSERT(pCtxInfo->CreateInfo.requestedVisualBits & CR_DOUBLE_BIT);
1172 if ((pCtxInfo->CreateInfo.requestedVisualBits & CR_DOUBLE_BIT)
1173 || version < SHCROGL_SSM_VERSION_WITH_SINGLE_DEPTH_STENCIL /* <- older version had a typo which lead to back always being used,
1174 * no matter what the visual bits are */
1175 )
1176 {
1177 pEl = &pData->aElements[pData->cElements];
1178 pEl->idFBO = pMural && pMural->fRedirected ? pMural->aidFBOs[CR_SERVER_FBO_BB_IDX(pMural)] : 0;
1179 pEl->enmBuffer = pData->aElements[1].idFBO ? GL_COLOR_ATTACHMENT0 : GL_BACK;
1180 pEl->posX = 0;
1181 pEl->posY = 0;
1182 pEl->width = width;
1183 pEl->height = height;
1184 pEl->enmFormat = GL_RGBA;
1185 pEl->enmType = GL_UNSIGNED_BYTE;
1186 pEl->cbData = width * height * 4;
1187 pEl->pvData = crCalloc(pEl->cbData);
1188 if (!pEl->pvData)
1189 {
1190 crVBoxServerFBImageDataTerm(pData);
1191 crWarning("crVBoxServerFBImageDataInit: crCalloc failed");
1192 return VERR_NO_MEMORY;
1193 }
1194 ++pData->cElements;
1195 }
1196
1197 if (version < SHCROGL_SSM_VERSION_WITH_SAVED_DEPTH_STENCIL_BUFFER)
1198 return VINF_SUCCESS;
1199
1200
1201 if (version < SHCROGL_SSM_VERSION_WITH_SINGLE_DEPTH_STENCIL)
1202 {
1203/* if (pCtxInfo->CreateInfo.requestedVisualBits & CR_DEPTH_BIT) */ /* <- older version had a typo which lead to back always being used,
1204 * no matter what the visual bits are */
1205 {
1206 AssertCompile(sizeof (GLfloat) == 4);
1207 pEl = &pData->aElements[pData->cElements];
1208 pEl->idFBO = pMural && pMural->fRedirected ? pMural->aidFBOs[CR_SERVER_FBO_FB_IDX(pMural)] : 0;
1209 pEl->enmBuffer = 0; /* we do not care */
1210 pEl->posX = 0;
1211 pEl->posY = 0;
1212 pEl->width = width;
1213 pEl->height = height;
1214 pEl->enmFormat = GL_DEPTH_COMPONENT;
1215 pEl->enmType = GL_FLOAT;
1216 pEl->cbData = width * height * 4;
1217 pEl->pvData = crCalloc(pEl->cbData);
1218 if (!pEl->pvData)
1219 {
1220 crVBoxServerFBImageDataTerm(pData);
1221 crWarning("crVBoxServerFBImageDataInit: crCalloc failed");
1222 return VERR_NO_MEMORY;
1223 }
1224
1225 /* init to default depth value, just in case */
1226 pF = (GLfloat*)pEl->pvData;
1227 for (i = 0; i < width * height; ++i)
1228 {
1229 pF[i] = 1.;
1230 }
1231 ++pData->cElements;
1232 }
1233
1234 /* if (pCtxInfo->CreateInfo.requestedVisualBits & CR_STENCIL_BIT) */ /* <- older version had a typo which lead to back always being used,
1235 * no matter what the visual bits are */
1236 {
1237 AssertCompile(sizeof (GLuint) == 4);
1238 pEl = &pData->aElements[pData->cElements];
1239 pEl->idFBO = pMural && pMural->fRedirected ? pMural->aidFBOs[CR_SERVER_FBO_FB_IDX(pMural)] : 0;
1240 pEl->enmBuffer = 0; /* we do not care */
1241 pEl->posX = 0;
1242 pEl->posY = 0;
1243 pEl->width = width;
1244 pEl->height = height;
1245 pEl->enmFormat = GL_STENCIL_INDEX;
1246 pEl->enmType = GL_UNSIGNED_INT;
1247 pEl->cbData = width * height * 4;
1248 pEl->pvData = crCalloc(pEl->cbData);
1249 if (!pEl->pvData)
1250 {
1251 crVBoxServerFBImageDataTerm(pData);
1252 crWarning("crVBoxServerFBImageDataInit: crCalloc failed");
1253 return VERR_NO_MEMORY;
1254 }
1255 ++pData->cElements;
1256 }
1257 return VINF_SUCCESS;
1258 }
1259
1260 if ((pCtxInfo->CreateInfo.requestedVisualBits & CR_STENCIL_BIT)
1261 || (pCtxInfo->CreateInfo.requestedVisualBits & CR_DEPTH_BIT))
1262 {
1263 pEl = &pData->aElements[pData->cElements];
1264 pEl->idFBO = pMural && pMural->fRedirected ? pMural->aidFBOs[CR_SERVER_FBO_FB_IDX(pMural)] : 0;
1265 pEl->enmBuffer = 0; /* we do not care */
1266 pEl->posX = 0;
1267 pEl->posY = 0;
1268 pEl->width = width;
1269 pEl->height = height;
1270 pEl->enmFormat = GL_DEPTH_STENCIL;
1271 pEl->enmType = GL_UNSIGNED_INT_24_8;
1272 pEl->cbData = width * height * 4;
1273 pEl->pvData = crCalloc(pEl->cbData);
1274 if (!pEl->pvData)
1275 {
1276 crVBoxServerFBImageDataTerm(pData);
1277 crWarning("crVBoxServerFBImageDataInit: crCalloc failed");
1278 return VERR_NO_MEMORY;
1279 }
1280 ++pData->cElements;
1281 }
1282 return VINF_SUCCESS;
1283}
1284
1285static int crVBoxServerFBImageDataInit(CRFBData *pData, CRContextInfo *pCtxInfo, CRMuralInfo *pMural, GLboolean fWrite)
1286{
1287 return crVBoxServerFBImageDataInitEx(pData, pCtxInfo, pMural, fWrite, SHCROGL_SSM_VERSION, 0, 0);
1288}
1289
1290static int crVBoxServerSaveFBImage(PSSMHANDLE pSSM)
1291{
1292 CRContextInfo *pCtxInfo;
1293 CRContext *pContext;
1294 CRMuralInfo *pMural;
1295 int32_t rc;
1296 GLuint i;
1297 struct
1298 {
1299 CRFBData data;
1300 CRFBDataElement buffer[3]; /* CRFBData::aElements[1] + buffer[3] gives 4: back, front, depth and stencil */
1301 } Data;
1302
1303 Assert(sizeof (Data) >= RT_OFFSETOF(CRFBData, aElements[4]));
1304
1305 pCtxInfo = cr_server.currentCtxInfo;
1306 pContext = pCtxInfo->pContext;
1307 pMural = pCtxInfo->currentMural;
1308
1309 rc = crVBoxServerFBImageDataInit(&Data.data, pCtxInfo, pMural, GL_FALSE);
1310 if (!RT_SUCCESS(rc))
1311 {
1312 crWarning("crVBoxServerFBImageDataInit failed rc %d", rc);
1313 return rc;
1314 }
1315
1316 rc = crStateAcquireFBImage(pContext, &Data.data);
1317 AssertRCReturn(rc, rc);
1318
1319 for (i = 0; i < Data.data.cElements; ++i)
1320 {
1321 CRFBDataElement * pEl = &Data.data.aElements[i];
1322 rc = SSMR3PutMem(pSSM, pEl->pvData, pEl->cbData);
1323 AssertRCReturn(rc, rc);
1324 }
1325
1326 crVBoxServerFBImageDataTerm(&Data.data);
1327
1328 return VINF_SUCCESS;
1329}
1330
1331#define CRSERVER_ASSERTRC_RETURN_VOID(_rc) do { \
1332 if(!RT_SUCCESS((_rc))) { \
1333 AssertFailed(); \
1334 return; \
1335 } \
1336 } while (0)
1337
1338static void crVBoxServerSaveAdditionalMuralsCB(unsigned long key, void *data1, void *data2)
1339{
1340 CRContextInfo *pContextInfo = (CRContextInfo *) data1;
1341 PCRVBOX_SAVE_STATE_GLOBAL pData = (PCRVBOX_SAVE_STATE_GLOBAL)data2;
1342 CRMuralInfo *pMural = (CRMuralInfo*)crHashtableSearch(cr_server.muralTable, key);
1343 PSSMHANDLE pSSM = pData->pSSM;
1344 CRbitvalue initialCtxUsage[CR_MAX_BITARRAY];
1345 CRMuralInfo *pInitialCurMural = pContextInfo->currentMural;
1346
1347 crMemcpy(initialCtxUsage, pMural->ctxUsage, sizeof (initialCtxUsage));
1348
1349 CRSERVER_ASSERTRC_RETURN_VOID(pData->rc);
1350
1351 pData->rc = SSMR3PutMem(pSSM, &key, sizeof(key));
1352 CRSERVER_ASSERTRC_RETURN_VOID(pData->rc);
1353
1354 pData->rc = SSMR3PutMem(pSSM, &pContextInfo->CreateInfo.externalID, sizeof(pContextInfo->CreateInfo.externalID));
1355 CRSERVER_ASSERTRC_RETURN_VOID(pData->rc);
1356
1357 crServerPerformMakeCurrent(pMural, pContextInfo);
1358
1359 pData->rc = crVBoxServerSaveFBImage(pSSM);
1360
1361 /* restore the reference data, we synchronize it with the HW state in a later crServerPerformMakeCurrent call */
1362 crMemcpy(pMural->ctxUsage, initialCtxUsage, sizeof (initialCtxUsage));
1363 pContextInfo->currentMural = pInitialCurMural;
1364
1365 CRSERVER_ASSERTRC_RETURN_VOID(pData->rc);
1366}
1367
1368static void crVBoxServerSaveContextStateCB(unsigned long key, void *data1, void *data2)
1369{
1370 CRContextInfo *pContextInfo = (CRContextInfo *) data1;
1371 CRContext *pContext = pContextInfo->pContext;
1372 PCRVBOX_SAVE_STATE_GLOBAL pData = (PCRVBOX_SAVE_STATE_GLOBAL)data2;
1373 PSSMHANDLE pSSM = pData->pSSM;
1374 CRMuralInfo *pMural = (CRMuralInfo*)crHashtableSearch(pData->contextMuralTable, key);
1375 CRMuralInfo *pContextCurrentMural = pContextInfo->currentMural;
1376 const int32_t i32Dummy = 0;
1377
1378 AssertCompile(sizeof (i32Dummy) == sizeof (pMural->CreateInfo.externalID));
1379 CRSERVER_ASSERTRC_RETURN_VOID(pData->rc);
1380
1381 CRASSERT(pContext && pSSM);
1382 CRASSERT(pMural);
1383 CRASSERT(pMural->CreateInfo.externalID);
1384
1385 /* We could have skipped saving the key and use similar callback to load context states back,
1386 * but there's no guarantee we'd traverse hashtable in same order after loading.
1387 */
1388 pData->rc = SSMR3PutMem(pSSM, &key, sizeof(key));
1389 CRSERVER_ASSERTRC_RETURN_VOID(pData->rc);
1390
1391#ifdef DEBUG_misha
1392 {
1393 unsigned long id;
1394 if (!crHashtableGetDataKey(cr_server.contextTable, pContextInfo, &id))
1395 crWarning("No client id for server ctx %d", pContextInfo->CreateInfo.externalID);
1396 else
1397 CRASSERT(id == key);
1398 }
1399#endif
1400
1401#ifdef CR_STATE_NO_TEXTURE_IMAGE_STORE
1402 if (pContextInfo->currentMural
1403 || crHashtableSearch(cr_server.muralTable, pMural->CreateInfo.externalID) /* <- this is not a dummy mural */
1404 )
1405 {
1406 CRASSERT(pMural->CreateInfo.externalID);
1407 CRASSERT(!crHashtableSearch(cr_server.dummyMuralTable, pMural->CreateInfo.externalID));
1408 pData->rc = SSMR3PutMem(pSSM, &pMural->CreateInfo.externalID, sizeof(pMural->CreateInfo.externalID));
1409 }
1410 else
1411 {
1412 /* this is a dummy mural */
1413 CRASSERT(!pMural->width);
1414 CRASSERT(!pMural->height);
1415 CRASSERT(crHashtableSearch(cr_server.dummyMuralTable, pMural->CreateInfo.externalID));
1416 pData->rc = SSMR3PutMem(pSSM, &i32Dummy, sizeof(pMural->CreateInfo.externalID));
1417 }
1418 CRSERVER_ASSERTRC_RETURN_VOID(pData->rc);
1419
1420 CRASSERT(CR_STATE_SHAREDOBJ_USAGE_IS_SET(pMural, pContext));
1421 CRASSERT(pContextInfo->currentMural == pMural || !pContextInfo->currentMural);
1422 CRASSERT(cr_server.curClient);
1423
1424 crServerPerformMakeCurrent(pMural, pContextInfo);
1425#endif
1426
1427 pData->rc = crStateSaveContext(pContext, pSSM);
1428 CRSERVER_ASSERTRC_RETURN_VOID(pData->rc);
1429
1430 pData->rc = crVBoxServerSaveFBImage(pSSM);
1431 CRSERVER_ASSERTRC_RETURN_VOID(pData->rc);
1432
1433 /* restore the initial current mural */
1434 pContextInfo->currentMural = pContextCurrentMural;
1435}
1436
1437#if 0
1438typedef struct CR_SERVER_CHECK_BUFFERS
1439{
1440 CRBufferObject *obj;
1441 CRContext *ctx;
1442}CR_SERVER_CHECK_BUFFERS, *PCR_SERVER_CHECK_BUFFERS;
1443
1444static void crVBoxServerCheckConsistencyContextBuffersCB(unsigned long key, void *data1, void *data2)
1445{
1446 CRContextInfo* pContextInfo = (CRContextInfo*)data1;
1447 CRContext *ctx = pContextInfo->pContext;
1448 PCR_SERVER_CHECK_BUFFERS pBuffers = (PCR_SERVER_CHECK_BUFFERS)data2;
1449 CRBufferObject *obj = pBuffers->obj;
1450 CRBufferObjectState *b = &(ctx->bufferobject);
1451 int j, k;
1452
1453 if (obj == b->arrayBuffer)
1454 {
1455 Assert(!pBuffers->ctx || pBuffers->ctx == ctx);
1456 pBuffers->ctx = ctx;
1457 }
1458 if (obj == b->elementsBuffer)
1459 {
1460 Assert(!pBuffers->ctx || pBuffers->ctx == ctx);
1461 pBuffers->ctx = ctx;
1462 }
1463#ifdef CR_ARB_pixel_buffer_object
1464 if (obj == b->packBuffer)
1465 {
1466 Assert(!pBuffers->ctx || pBuffers->ctx == ctx);
1467 pBuffers->ctx = ctx;
1468 }
1469 if (obj == b->unpackBuffer)
1470 {
1471 Assert(!pBuffers->ctx || pBuffers->ctx == ctx);
1472 pBuffers->ctx = ctx;
1473 }
1474#endif
1475
1476#ifdef CR_ARB_vertex_buffer_object
1477 for (j=0; j<CRSTATECLIENT_MAX_VERTEXARRAYS; ++j)
1478 {
1479 CRClientPointer *cp = crStateGetClientPointerByIndex(j, &ctx->client.array);
1480 if (obj == cp->buffer)
1481 {
1482 Assert(!pBuffers->ctx || pBuffers->ctx == ctx);
1483 pBuffers->ctx = ctx;
1484 }
1485 }
1486
1487 for (k=0; k<ctx->client.vertexArrayStackDepth; ++k)
1488 {
1489 CRVertexArrays *pArray = &ctx->client.vertexArrayStack[k];
1490 for (j=0; j<CRSTATECLIENT_MAX_VERTEXARRAYS; ++j)
1491 {
1492 CRClientPointer *cp = crStateGetClientPointerByIndex(j, pArray);
1493 if (obj == cp->buffer)
1494 {
1495 Assert(!pBuffers->ctx || pBuffers->ctx == ctx);
1496 pBuffers->ctx = ctx;
1497 }
1498 }
1499 }
1500#endif
1501}
1502
1503static void crVBoxServerCheckConsistencyBuffersCB(unsigned long key, void *data1, void *data2)
1504{
1505 CRBufferObject *obj = (CRBufferObject *)data1;
1506 CR_SERVER_CHECK_BUFFERS Buffers = {0};
1507 Buffers.obj = obj;
1508 crHashtableWalk(cr_server.contextTable, crVBoxServerCheckConsistencyContextBuffersCB, (void*)&Buffers);
1509}
1510
1511//static void crVBoxServerCheckConsistency2CB(unsigned long key, void *data1, void *data2)
1512//{
1513// CRContextInfo* pContextInfo1 = (CRContextInfo*)data1;
1514// CRContextInfo* pContextInfo2 = (CRContextInfo*)data2;
1515//
1516// CRASSERT(pContextInfo1->pContext);
1517// CRASSERT(pContextInfo2->pContext);
1518//
1519// if (pContextInfo1 == pContextInfo2)
1520// {
1521// CRASSERT(pContextInfo1->pContext == pContextInfo2->pContext);
1522// return;
1523// }
1524//
1525// CRASSERT(pContextInfo1->pContext != pContextInfo2->pContext);
1526// CRASSERT(pContextInfo1->pContext->shared);
1527// CRASSERT(pContextInfo2->pContext->shared);
1528// CRASSERT(pContextInfo1->pContext->shared == pContextInfo2->pContext->shared);
1529// if (pContextInfo1->pContext->shared != pContextInfo2->pContext->shared)
1530// return;
1531//
1532// crHashtableWalk(pContextInfo1->pContext->shared->buffersTable, crVBoxServerCheckConsistencyBuffersCB, pContextInfo2);
1533//}
1534static void crVBoxServerCheckSharedCB(unsigned long key, void *data1, void *data2)
1535{
1536 CRContextInfo* pContextInfo = (CRContextInfo*)data1;
1537 void **ppShared = (void**)data2;
1538 if (!*ppShared)
1539 *ppShared = pContextInfo->pContext->shared;
1540 else
1541 Assert(pContextInfo->pContext->shared == *ppShared);
1542}
1543
1544static void crVBoxServerCheckConsistency()
1545{
1546 CRSharedState *pShared = NULL;
1547 crHashtableWalk(cr_server.contextTable, crVBoxServerCheckSharedCB, (void*)&pShared);
1548 Assert(pShared);
1549 if (pShared)
1550 {
1551 crHashtableWalk(pShared->buffersTable, crVBoxServerCheckConsistencyBuffersCB, NULL);
1552 }
1553}
1554#endif
1555
1556static uint32_t g_hackVBoxServerSaveLoadCallsLeft = 0;
1557
1558DECLEXPORT(int32_t) crVBoxServerSaveState(PSSMHANDLE pSSM)
1559{
1560 int32_t rc, i;
1561 uint32_t ui32;
1562 GLboolean b;
1563 unsigned long key;
1564 GLenum err;
1565#ifdef CR_STATE_NO_TEXTURE_IMAGE_STORE
1566 CRClient *curClient;
1567 CRMuralInfo *curMural = NULL;
1568 CRContextInfo *curCtxInfo = NULL;
1569#endif
1570 CRVBOX_SAVE_STATE_GLOBAL Data;
1571
1572 crMemset(&Data, 0, sizeof (Data));
1573
1574#if 0
1575 crVBoxServerCheckConsistency();
1576#endif
1577
1578 /* We shouldn't be called if there's no clients at all*/
1579 CRASSERT(cr_server.numClients>0);
1580
1581 /* @todo it's hack atm */
1582 /* We want to be called only once to save server state but atm we're being called from svcSaveState
1583 * for every connected client (e.g. guest opengl application)
1584 */
1585 if (!cr_server.bIsInSavingState) /* It's first call */
1586 {
1587 cr_server.bIsInSavingState = GL_TRUE;
1588
1589 /* Store number of clients */
1590 rc = SSMR3PutU32(pSSM, (uint32_t) cr_server.numClients);
1591 AssertRCReturn(rc, rc);
1592
1593 g_hackVBoxServerSaveLoadCallsLeft = cr_server.numClients;
1594 }
1595
1596 g_hackVBoxServerSaveLoadCallsLeft--;
1597
1598 /* Do nothing until we're being called last time */
1599 if (g_hackVBoxServerSaveLoadCallsLeft>0)
1600 {
1601 return VINF_SUCCESS;
1602 }
1603
1604#ifdef DEBUG_misha
1605#define CR_DBG_STR_STATE_SAVE_START "VBox.Cr.StateSaveStart"
1606#define CR_DBG_STR_STATE_SAVE_STOP "VBox.Cr.StateSaveStop"
1607
1608 if (cr_server.head_spu->dispatch_table.StringMarkerGREMEDY)
1609 cr_server.head_spu->dispatch_table.StringMarkerGREMEDY(sizeof (CR_DBG_STR_STATE_SAVE_START), CR_DBG_STR_STATE_SAVE_START);
1610#endif
1611
1612 /* Save rendering contexts creation info */
1613 ui32 = crHashtableNumElements(cr_server.contextTable);
1614 rc = SSMR3PutU32(pSSM, (uint32_t) ui32);
1615 AssertRCReturn(rc, rc);
1616 crHashtableWalk(cr_server.contextTable, crVBoxServerSaveCreateInfoFromCtxInfoCB, pSSM);
1617
1618#ifdef CR_STATE_NO_TEXTURE_IMAGE_STORE
1619 curClient = cr_server.curClient;
1620 /* Save current win and ctx IDs, as we'd rebind contexts when saving textures */
1621 if (curClient)
1622 {
1623 curCtxInfo = cr_server.curClient->currentCtxInfo;
1624 curMural = cr_server.curClient->currentMural;
1625 }
1626 else if (cr_server.numClients)
1627 {
1628 cr_server.curClient = cr_server.clients[0];
1629 }
1630#endif
1631
1632 /* first save windows info */
1633 /* Save windows creation info */
1634 ui32 = crHashtableNumElements(cr_server.muralTable);
1635 /* There should be default mural always */
1636 CRASSERT(ui32>=1);
1637 rc = SSMR3PutU32(pSSM, (uint32_t) ui32-1);
1638 AssertRCReturn(rc, rc);
1639 crHashtableWalk(cr_server.muralTable, crVBoxServerSaveCreateInfoFromMuralInfoCB, pSSM);
1640
1641 /* Save cr_server.muralTable
1642 * @todo we don't need it all, just geometry info actually
1643 */
1644 rc = SSMR3PutU32(pSSM, (uint32_t) ui32-1);
1645 AssertRCReturn(rc, rc);
1646 crHashtableWalk(cr_server.muralTable, crVBoxServerSaveMuralCB, pSSM);
1647
1648 /* we need to save front & backbuffer data for each mural first create a context -> mural association */
1649 crVBoxServerBuildSaveStateGlobal(&Data);
1650
1651 rc = crStateSaveGlobals(pSSM);
1652 AssertRCReturn(rc, rc);
1653
1654 Data.pSSM = pSSM;
1655 /* Save contexts state tracker data */
1656 /* @todo For now just some blind data dumps,
1657 * but I've a feeling those should be saved/restored in a very strict sequence to
1658 * allow diff_api to work correctly.
1659 * Should be tested more with multiply guest opengl apps working when saving VM snapshot.
1660 */
1661 crHashtableWalk(cr_server.contextTable, crVBoxServerSaveContextStateCB, &Data);
1662 AssertRCReturn(Data.rc, Data.rc);
1663
1664 ui32 = crHashtableNumElements(Data.additionalMuralContextTable);
1665 rc = SSMR3PutU32(pSSM, (uint32_t) ui32);
1666 AssertRCReturn(rc, rc);
1667
1668 crHashtableWalk(Data.additionalMuralContextTable, crVBoxServerSaveAdditionalMuralsCB, &Data);
1669 AssertRCReturn(Data.rc, Data.rc);
1670
1671#ifdef CR_STATE_NO_TEXTURE_IMAGE_STORE
1672 cr_server.curClient = curClient;
1673 /* Restore original win and ctx IDs*/
1674 if (curClient && curMural && curCtxInfo)
1675 {
1676 crServerPerformMakeCurrent(curMural, curCtxInfo);
1677 }
1678 else
1679 {
1680 cr_server.bForceMakeCurrentOnClientSwitch = GL_TRUE;
1681 }
1682#endif
1683
1684 /* Save clients info */
1685 for (i = 0; i < cr_server.numClients; i++)
1686 {
1687 if (cr_server.clients[i] && cr_server.clients[i]->conn)
1688 {
1689 CRClient *pClient = cr_server.clients[i];
1690
1691 rc = SSMR3PutU32(pSSM, pClient->conn->u32ClientID);
1692 AssertRCReturn(rc, rc);
1693
1694 rc = SSMR3PutU32(pSSM, pClient->conn->vMajor);
1695 AssertRCReturn(rc, rc);
1696
1697 rc = SSMR3PutU32(pSSM, pClient->conn->vMinor);
1698 AssertRCReturn(rc, rc);
1699
1700 rc = SSMR3PutMem(pSSM, pClient, sizeof(*pClient));
1701 AssertRCReturn(rc, rc);
1702
1703 if (pClient->currentCtxInfo && pClient->currentCtxInfo->pContext && pClient->currentContextNumber > 0)
1704 {
1705 b = crHashtableGetDataKey(cr_server.contextTable, pClient->currentCtxInfo, &key);
1706 CRASSERT(b);
1707 rc = SSMR3PutMem(pSSM, &key, sizeof(key));
1708 AssertRCReturn(rc, rc);
1709 }
1710
1711 if (pClient->currentMural && pClient->currentWindow > 0)
1712 {
1713 b = crHashtableGetDataKey(cr_server.muralTable, pClient->currentMural, &key);
1714 CRASSERT(b);
1715 rc = SSMR3PutMem(pSSM, &key, sizeof(key));
1716 AssertRCReturn(rc, rc);
1717 }
1718 }
1719 }
1720
1721 rc = CrPMgrSaveState(pSSM);
1722 AssertRCReturn(rc, rc);
1723
1724 /* all context gl error states should have now be synced with chromium erro states,
1725 * reset the error if any */
1726 while ((err = cr_server.head_spu->dispatch_table.GetError()) != GL_NO_ERROR)
1727 crWarning("crServer: glGetError %d after saving snapshot", err);
1728
1729 cr_server.bIsInSavingState = GL_FALSE;
1730
1731#ifdef DEBUG_misha
1732 if (cr_server.head_spu->dispatch_table.StringMarkerGREMEDY)
1733 cr_server.head_spu->dispatch_table.StringMarkerGREMEDY(sizeof (CR_DBG_STR_STATE_SAVE_STOP), CR_DBG_STR_STATE_SAVE_STOP);
1734#endif
1735
1736 return VINF_SUCCESS;
1737}
1738
1739static DECLCALLBACK(CRContext*) crVBoxServerGetContextCB(void* pvData)
1740{
1741 CRContextInfo* pContextInfo = (CRContextInfo*)pvData;
1742 CRASSERT(pContextInfo);
1743 CRASSERT(pContextInfo->pContext);
1744 return pContextInfo->pContext;
1745}
1746
1747typedef struct CR_SERVER_LOADSTATE_READER
1748{
1749 PSSMHANDLE pSSM;
1750 uint32_t cbBuffer;
1751 uint32_t cbData;
1752 uint32_t offData;
1753 uint8_t *pu8Buffer;
1754} CR_SERVER_LOADSTATE_READER;
1755
1756static void crServerLsrInit(CR_SERVER_LOADSTATE_READER *pReader, PSSMHANDLE pSSM)
1757{
1758 memset(pReader, 0, sizeof (*pReader));
1759 pReader->pSSM = pSSM;
1760}
1761
1762static void crServerLsrTerm(CR_SERVER_LOADSTATE_READER *pReader)
1763{
1764 if (pReader->pu8Buffer)
1765 RTMemFree(pReader->pu8Buffer);
1766
1767 /* sanity */
1768 memset(pReader, 0, sizeof (*pReader));
1769}
1770
1771static int crServerLsrDataGetMem(CR_SERVER_LOADSTATE_READER *pReader, void *pvBuffer, uint32_t cbBuffer)
1772{
1773 int rc = VINF_SUCCESS;
1774 uint32_t cbRemaining = cbBuffer;
1775 if (pReader->cbData)
1776 {
1777 uint8_t cbData = RT_MIN(pReader->cbData, cbBuffer);
1778 memcpy(pvBuffer, pReader->pu8Buffer + pReader->offData, cbData);
1779 pReader->cbData -= cbData;
1780 pReader->offData += cbData;
1781
1782 cbRemaining -= cbData;
1783 pvBuffer = ((uint8_t*)pvBuffer) + cbData;
1784 }
1785
1786 if (cbRemaining)
1787 {
1788 rc = SSMR3GetMem(pReader->pSSM, pvBuffer, cbRemaining);
1789 AssertRC(rc);
1790 }
1791
1792 return rc;
1793}
1794
1795static int crServerLsrDataGetU32(CR_SERVER_LOADSTATE_READER *pReader, uint32_t *pu32)
1796{
1797 return crServerLsrDataGetMem(pReader, pu32, sizeof (*pu32));
1798}
1799
1800static int crServerLsrDataPutMem(CR_SERVER_LOADSTATE_READER *pReader, void *pvBuffer, uint32_t cbBuffer)
1801{
1802 if (!pReader->cbData && pReader->cbBuffer >= cbBuffer)
1803 {
1804 pReader->offData = 0;
1805 pReader->cbData = cbBuffer;
1806 memcpy(pReader->pu8Buffer, pvBuffer, cbBuffer);
1807 }
1808 else if (pReader->offData >= cbBuffer)
1809 {
1810 pReader->offData -= cbBuffer;
1811 pReader->cbData += cbBuffer;
1812 memcpy(pReader->pu8Buffer + pReader->offData, pvBuffer, cbBuffer);
1813 }
1814 else
1815 {
1816 uint8_t *pu8Buffer = pReader->pu8Buffer;
1817
1818 pReader->pu8Buffer = (uint8_t*)RTMemAlloc(cbBuffer + pReader->cbData);
1819 if (!pReader->pu8Buffer)
1820 {
1821 crWarning("failed to allocate mem %d", cbBuffer + pReader->cbData);
1822 return VERR_NO_MEMORY;
1823 }
1824
1825 memcpy(pReader->pu8Buffer, pvBuffer, cbBuffer);
1826 if (pu8Buffer)
1827 {
1828 memcpy(pReader->pu8Buffer + cbBuffer, pu8Buffer + pReader->offData, pReader->cbData);
1829 RTMemFree(pu8Buffer);
1830 }
1831 else
1832 {
1833 Assert(!pReader->cbData);
1834 }
1835 pReader->offData = 0;
1836 pReader->cbData += cbBuffer;
1837 }
1838
1839 return VINF_SUCCESS;
1840}
1841
1842/* data to be skipped */
1843
1844typedef struct CR_SERVER_BUGGY_MURAL_DATA_2
1845{
1846 void*ListHead_pNext;
1847 void*ListHead_pPrev;
1848 uint32_t cEntries;
1849} CR_SERVER_BUGGY_MURAL_DATA_2;
1850typedef struct CR_SERVER_BUGGY_MURAL_DATA_1
1851{
1852 /* VBOXVR_COMPOSITOR_ENTRY Ce; */
1853 void*Ce_Node_pNext;
1854 void*Ce_Node_pPrev;
1855 CR_SERVER_BUGGY_MURAL_DATA_2 Vr;
1856 /* VBOXVR_TEXTURE Tex; */
1857 uint32_t Tex_width;
1858 uint32_t Tex_height;
1859 uint32_t Tex_target;
1860 uint32_t Tex_hwid;
1861 /* RTPOINT Pos; */
1862 uint32_t Pos_x;
1863 uint32_t Pos_y;
1864 uint32_t fChanged;
1865 uint32_t cRects;
1866 void* paSrcRects;
1867 void* paDstRects;
1868} CR_SERVER_BUGGY_MURAL_DATA_1;
1869
1870typedef struct CR_SERVER_BUGGY_MURAL_DATA_4
1871{
1872 uint32_t u32Magic;
1873 int32_t cLockers;
1874 RTNATIVETHREAD NativeThreadOwner;
1875 int32_t cNestings;
1876 uint32_t fFlags;
1877 void* EventSem;
1878 R3R0PTRTYPE(PRTLOCKVALRECEXCL) pValidatorRec;
1879 RTHCPTR Alignment;
1880} CR_SERVER_BUGGY_MURAL_DATA_4;
1881
1882typedef struct CR_SERVER_BUGGY_MURAL_DATA_3
1883{
1884 void*Compositor_List_pNext;
1885 void*Compositor_List_pPrev;
1886 void*Compositor_pfnEntryRemoved;
1887 float StretchX;
1888 float StretchY;
1889 uint32_t cRects;
1890 uint32_t cRectsBuffer;
1891 void*paSrcRects;
1892 void*paDstRects;
1893 CR_SERVER_BUGGY_MURAL_DATA_4 CritSect;
1894} CR_SERVER_BUGGY_MURAL_DATA_3;
1895
1896typedef struct CR_SERVER_BUGGY_MURAL_DATA
1897{
1898 uint8_t fRootVrOn;
1899 CR_SERVER_BUGGY_MURAL_DATA_1 RootVrCEntry;
1900 CR_SERVER_BUGGY_MURAL_DATA_3 RootVrCompositor;
1901} CR_SERVER_BUGGY_MURAL_DATA;
1902
1903AssertCompile(sizeof (CR_SERVER_BUGGY_MURAL_DATA) < sizeof (CRClient));
1904
1905static int32_t crVBoxServerLoadMurals(CR_SERVER_LOADSTATE_READER *pReader, uint32_t version)
1906{
1907 unsigned long key;
1908 uint32_t ui, uiNumElems;
1909 bool fBuggyMuralData = false;
1910 /* Load windows */
1911 int32_t rc = crServerLsrDataGetU32(pReader, &uiNumElems);
1912 AssertRCReturn(rc, rc);
1913 for (ui=0; ui<uiNumElems; ++ui)
1914 {
1915 CRCreateInfo_t createInfo;
1916 char psz[200];
1917 GLint winID;
1918 unsigned long key;
1919
1920 rc = crServerLsrDataGetMem(pReader, &key, sizeof(key));
1921 AssertRCReturn(rc, rc);
1922 rc = crServerLsrDataGetMem(pReader, &createInfo, sizeof(createInfo));
1923 AssertRCReturn(rc, rc);
1924
1925 CRASSERT(!pReader->cbData);
1926
1927 if (createInfo.pszDpyName)
1928 {
1929 rc = SSMR3GetStrZEx(pReader->pSSM, psz, 200, NULL);
1930 AssertRCReturn(rc, rc);
1931 createInfo.pszDpyName = psz;
1932 }
1933
1934 winID = crServerDispatchWindowCreateEx(createInfo.pszDpyName, createInfo.visualBits, key);
1935 CRASSERT((int64_t)winID == (int64_t)key);
1936 }
1937
1938 /* Load cr_server.muralTable */
1939 rc = SSMR3GetU32(pReader->pSSM, &uiNumElems);
1940 AssertRCReturn(rc, rc);
1941 for (ui=0; ui<uiNumElems; ++ui)
1942 {
1943 CRMuralInfo muralInfo;
1944 CRMuralInfo *pActualMural = NULL;
1945
1946 rc = crServerLsrDataGetMem(pReader, &key, sizeof(key));
1947 AssertRCReturn(rc, rc);
1948 rc = crServerLsrDataGetMem(pReader, &muralInfo, RT_OFFSETOF(CRMuralInfo, CreateInfo));
1949 AssertRCReturn(rc, rc);
1950
1951 if (version <= SHCROGL_SSM_VERSION_BEFORE_FRONT_DRAW_TRACKING)
1952 muralInfo.bFbDraw = GL_TRUE;
1953
1954 if (!ui && version == SHCROGL_SSM_VERSION_WITH_BUGGY_MURAL_INFO)
1955 {
1956 /* Lookahead buffer used to determine whether the data erroneously stored root visible regions data */
1957 union
1958 {
1959 void * apv[1];
1960 CR_SERVER_BUGGY_MURAL_DATA Data;
1961 /* need to chak spuWindow, so taking the offset of filed following it*/
1962 uint8_t au8[RT_OFFSETOF(CRMuralInfo, screenId)];
1963 RTRECT aVisRects[sizeof (CR_SERVER_BUGGY_MURAL_DATA) / sizeof (RTRECT)];
1964 } LaBuf;
1965
1966 do {
1967 /* first value is bool (uint8_t) value followed by pointer-size-based alignment.
1968 * the mural memory is zero-initialized initially, so we can be sure the padding is zeroed */
1969 rc = crServerLsrDataGetMem(pReader, &LaBuf, sizeof (LaBuf));
1970 AssertRCReturn(rc, rc);
1971 if (LaBuf.apv[0] != NULL && LaBuf.apv[0] != ((void*)1))
1972 break;
1973
1974 /* check that the pointers are either valid or NULL */
1975 if(LaBuf.Data.RootVrCEntry.Ce_Node_pNext && !RT_VALID_PTR(LaBuf.Data.RootVrCEntry.Ce_Node_pNext))
1976 break;
1977 if(LaBuf.Data.RootVrCEntry.Ce_Node_pPrev && !RT_VALID_PTR(LaBuf.Data.RootVrCEntry.Ce_Node_pPrev))
1978 break;
1979 if(LaBuf.Data.RootVrCEntry.Vr.ListHead_pNext && !RT_VALID_PTR(LaBuf.Data.RootVrCEntry.Vr.ListHead_pNext))
1980 break;
1981 if(LaBuf.Data.RootVrCEntry.Vr.ListHead_pPrev && !RT_VALID_PTR(LaBuf.Data.RootVrCEntry.Vr.ListHead_pPrev))
1982 break;
1983
1984 /* the entry can can be the only one within the (mural) compositor,
1985 * so its compositor entry node can either contain NULL pNext and pPrev,
1986 * or both of them pointing to compositor's list head */
1987 if (LaBuf.Data.RootVrCEntry.Ce_Node_pNext != LaBuf.Data.RootVrCEntry.Ce_Node_pPrev)
1988 break;
1989
1990 /* can either both or none be NULL */
1991 if (!LaBuf.Data.RootVrCEntry.Ce_Node_pNext != !LaBuf.Data.RootVrCEntry.Ce_Node_pPrev)
1992 break;
1993
1994 if (!LaBuf.Data.fRootVrOn)
1995 {
1996 if (LaBuf.Data.RootVrCEntry.Ce_Node_pNext || LaBuf.Data.RootVrCEntry.Ce_Node_pPrev)
1997 break;
1998
1999 /* either non-initialized (zeroed) or empty list */
2000 if (LaBuf.Data.RootVrCEntry.Vr.ListHead_pNext != LaBuf.Data.RootVrCEntry.Vr.ListHead_pPrev)
2001 break;
2002
2003 if (LaBuf.Data.RootVrCEntry.Vr.cEntries)
2004 break;
2005 }
2006 else
2007 {
2008 /* the entry should be initialized */
2009 if (!LaBuf.Data.RootVrCEntry.Vr.ListHead_pNext)
2010 break;
2011 if (!LaBuf.Data.RootVrCEntry.Vr.ListHead_pPrev)
2012 break;
2013
2014 if (LaBuf.Data.RootVrCEntry.Vr.cEntries)
2015 {
2016 /* entry should be in compositor list*/
2017 if (LaBuf.Data.RootVrCEntry.Ce_Node_pPrev == NULL)
2018 break;
2019 CRASSERT(LaBuf.Data.RootVrCEntry.Ce_Node_pNext);
2020 }
2021 else
2022 {
2023 /* entry should NOT be in compositor list*/
2024 if (LaBuf.Data.RootVrCEntry.Ce_Node_pPrev != NULL)
2025 break;
2026 CRASSERT(!LaBuf.Data.RootVrCEntry.Ce_Node_pNext);
2027 }
2028 }
2029
2030#if 0
2031 if (muralInfo.pVisibleRects)
2032 {
2033 int j;
2034 int cRects = RT_MIN(muralInfo.cVisibleRects, RT_ELEMENTS(LaBuf.aVisRects));
2035 CRASSERT(cRects);
2036 for (j = 0; j < cRects; ++j)
2037 {
2038 PRTRECT pRect = &LaBuf.aVisRects[j];
2039 if (pRect->xLeft >= pRect->xRight)
2040 break;
2041 if (pRect->yTop >= pRect->yBottom)
2042 break;
2043 if (pRect->xLeft < 0 || pRect->xRight < 0
2044 || pRect->yTop < 0 || pRect->yBottom < 0)
2045 break;
2046 if (pRect->xLeft > (GLint)muralInfo.width
2047 || pRect->xRight > (GLint)muralInfo.width)
2048 break;
2049 if (pRect->yTop > (GLint)muralInfo.height
2050 || pRect->yBottom > (GLint)muralInfo.height)
2051 break;
2052 }
2053
2054 if (j < cRects)
2055 {
2056 fBuggyMuralData = true;
2057 break;
2058 }
2059 }
2060
2061 if (muralInfo.pVisibleRects)
2062 {
2063 /* @todo: do we actually need any further checks here? */
2064 fBuggyMuralData = true;
2065 break;
2066 }
2067
2068 /* no visible regions*/
2069
2070 if (ui == uiNumElems - 1)
2071 {
2072 /* this is the last mural, next it goes idsPool, whose content can not match the above template again */
2073 fBuggyMuralData = true;
2074 break;
2075 }
2076
2077 /* next it goes a next mural info */
2078// if (!fExpectPtr)
2079// {
2080// CRMuralInfo *pNextSpuWindowInfoMural = (CRMuralInfo*)((void*)&LaBuf);
2081// if (!pNextSpuWindowInfoMural->spuWindow)
2082// fBuggyMuralData = true;
2083//
2084// break;
2085// }
2086#endif
2087 /* fExpectPtr == true, the valid pointer values should not match possible mural width/height/position */
2088 fBuggyMuralData = true;
2089 break;
2090
2091 } while (0);
2092
2093 rc = crServerLsrDataPutMem(pReader, &LaBuf, sizeof (LaBuf));
2094 AssertRCReturn(rc, rc);
2095 }
2096
2097 if (fBuggyMuralData)
2098 {
2099 CR_SERVER_BUGGY_MURAL_DATA Tmp;
2100 rc = crServerLsrDataGetMem(pReader, &Tmp, sizeof (Tmp));
2101 AssertRCReturn(rc, rc);
2102 }
2103
2104 if (muralInfo.pVisibleRects)
2105 {
2106 muralInfo.pVisibleRects = crAlloc(4*sizeof(GLint)*muralInfo.cVisibleRects);
2107 if (!muralInfo.pVisibleRects)
2108 {
2109 return VERR_NO_MEMORY;
2110 }
2111
2112 rc = crServerLsrDataGetMem(pReader, muralInfo.pVisibleRects, 4*sizeof(GLint)*muralInfo.cVisibleRects);
2113 AssertRCReturn(rc, rc);
2114 }
2115
2116 pActualMural = (CRMuralInfo *)crHashtableSearch(cr_server.muralTable, key);
2117 CRASSERT(pActualMural);
2118
2119 if (version >= SHCROGL_SSM_VERSION_WITH_WINDOW_CTX_USAGE)
2120 {
2121 rc = crServerLsrDataGetMem(pReader, pActualMural->ctxUsage, sizeof (pActualMural->ctxUsage));
2122 CRASSERT(rc == VINF_SUCCESS);
2123 }
2124
2125 /* Restore windows geometry info */
2126 crServerDispatchWindowSize(key, muralInfo.width, muralInfo.height);
2127 crServerDispatchWindowPosition(key, muralInfo.gX, muralInfo.gY);
2128 /* Same workaround as described in stub.c:stubUpdateWindowVisibileRegions for compiz on a freshly booted VM*/
2129 if (muralInfo.bReceivedRects)
2130 {
2131 crServerDispatchWindowVisibleRegion(key, muralInfo.cVisibleRects, muralInfo.pVisibleRects);
2132 }
2133 crServerDispatchWindowShow(key, muralInfo.bVisible);
2134
2135 if (muralInfo.pVisibleRects)
2136 {
2137 crFree(muralInfo.pVisibleRects);
2138 }
2139 }
2140
2141 CRASSERT(RT_SUCCESS(rc));
2142 return VINF_SUCCESS;
2143}
2144
2145static int crVBoxServerLoadFBImage(PSSMHANDLE pSSM, uint32_t version,
2146 CRContextInfo* pContextInfo, CRMuralInfo *pMural)
2147{
2148 CRContext *pContext = pContextInfo->pContext;
2149 int32_t rc = VINF_SUCCESS;
2150 GLuint i;
2151 /* can apply the data right away */
2152 struct
2153 {
2154 CRFBData data;
2155 CRFBDataElement buffer[3]; /* CRFBData::aElements[1] + buffer[3] gives 4: back, front, depth and stencil */
2156 } Data;
2157
2158 Assert(sizeof (Data) >= RT_OFFSETOF(CRFBData, aElements[4]));
2159
2160 if (version >= SHCROGL_SSM_VERSION_WITH_SAVED_DEPTH_STENCIL_BUFFER)
2161 {
2162 if (!pMural->width || !pMural->height)
2163 return VINF_SUCCESS;
2164
2165 rc = crVBoxServerFBImageDataInitEx(&Data.data, pContextInfo, pMural, GL_TRUE, version, 0, 0);
2166 if (!RT_SUCCESS(rc))
2167 {
2168 crWarning("crVBoxServerFBImageDataInit failed rc %d", rc);
2169 return rc;
2170 }
2171 }
2172 else
2173 {
2174 GLint storedWidth, storedHeight;
2175
2176 if (version > SHCROGL_SSM_VERSION_WITH_BUGGY_FB_IMAGE_DATA)
2177 {
2178 CRASSERT(cr_server.currentCtxInfo == pContextInfo);
2179 CRASSERT(cr_server.currentMural == pMural);
2180 storedWidth = pMural->width;
2181 storedHeight = pMural->height;
2182 }
2183 else
2184 {
2185 storedWidth = pContext->buffer.storedWidth;
2186 storedHeight = pContext->buffer.storedHeight;
2187 }
2188
2189 if (!storedWidth || !storedHeight)
2190 return VINF_SUCCESS;
2191
2192 rc = crVBoxServerFBImageDataInitEx(&Data.data, pContextInfo, pMural, GL_TRUE, version, storedWidth, storedHeight);
2193 if (!RT_SUCCESS(rc))
2194 {
2195 crWarning("crVBoxServerFBImageDataInit failed rc %d", rc);
2196 return rc;
2197 }
2198 }
2199
2200 CRASSERT(Data.data.cElements);
2201
2202 for (i = 0; i < Data.data.cElements; ++i)
2203 {
2204 CRFBDataElement * pEl = &Data.data.aElements[i];
2205 rc = SSMR3GetMem(pSSM, pEl->pvData, pEl->cbData);
2206 AssertRCReturn(rc, rc);
2207 }
2208
2209 if (version > SHCROGL_SSM_VERSION_WITH_BUGGY_FB_IMAGE_DATA)
2210 {
2211 CRBufferState *pBuf = &pContext->buffer;
2212 /* can apply the data right away */
2213 CRASSERT(cr_server.currentCtxInfo == &cr_server.MainContextInfo);
2214 CRASSERT(cr_server.currentMural);
2215
2216 cr_server.head_spu->dispatch_table.MakeCurrent( pMural->spuWindow,
2217 0,
2218 pContextInfo->SpuContext >= 0
2219 ? pContextInfo->SpuContext
2220 : cr_server.MainContextInfo.SpuContext);
2221 crStateApplyFBImage(pContext, &Data.data);
2222 CRASSERT(!pBuf->pFrontImg);
2223 CRASSERT(!pBuf->pBackImg);
2224 crVBoxServerFBImageDataTerm(&Data.data);
2225
2226 crServerPresentFBO(pMural);
2227
2228 CRASSERT(cr_server.currentMural);
2229 cr_server.head_spu->dispatch_table.MakeCurrent( cr_server.currentMural->spuWindow,
2230 0,
2231 cr_server.currentCtxInfo->SpuContext >= 0
2232 ? cr_server.currentCtxInfo->SpuContext
2233 : cr_server.MainContextInfo.SpuContext);
2234 }
2235 else
2236 {
2237 CRBufferState *pBuf = &pContext->buffer;
2238 CRASSERT(!pBuf->pFrontImg);
2239 CRASSERT(!pBuf->pBackImg);
2240 CRASSERT(Data.data.cElements); /* <- older versions always saved front and back, and we filtered out the null-sized buffers above */
2241
2242 if (Data.data.cElements)
2243 {
2244 CRFBData *pLazyData = crAlloc(RT_OFFSETOF(CRFBData, aElements[Data.data.cElements]));
2245 if (!RT_SUCCESS(rc))
2246 {
2247 crVBoxServerFBImageDataTerm(&Data.data);
2248 crWarning("crAlloc failed");
2249 return VERR_NO_MEMORY;
2250 }
2251
2252 crMemcpy(pLazyData, &Data.data, RT_OFFSETOF(CRFBData, aElements[Data.data.cElements]));
2253 pBuf->pFrontImg = pLazyData;
2254 }
2255 }
2256
2257 CRASSERT(RT_SUCCESS(rc));
2258 return VINF_SUCCESS;
2259}
2260
2261DECLEXPORT(int32_t) crVBoxServerLoadState(PSSMHANDLE pSSM, uint32_t version)
2262{
2263 int32_t rc, i;
2264 uint32_t ui, uiNumElems;
2265 unsigned long key;
2266 GLenum err;
2267 CR_SERVER_LOADSTATE_READER Reader;
2268
2269 if (!cr_server.bIsInLoadingState)
2270 {
2271 /* AssertRCReturn(...) will leave us in loading state, but it doesn't matter as we'd be failing anyway */
2272 cr_server.bIsInLoadingState = GL_TRUE;
2273
2274 /* Read number of clients */
2275 rc = SSMR3GetU32(pSSM, &g_hackVBoxServerSaveLoadCallsLeft);
2276 AssertRCReturn(rc, rc);
2277 }
2278
2279 g_hackVBoxServerSaveLoadCallsLeft--;
2280
2281 /* Do nothing until we're being called last time */
2282 if (g_hackVBoxServerSaveLoadCallsLeft>0)
2283 {
2284 return VINF_SUCCESS;
2285 }
2286
2287 if (version < SHCROGL_SSM_VERSION_BEFORE_CTXUSAGE_BITS)
2288 {
2289 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
2290 }
2291
2292 crServerLsrInit(&Reader, pSSM);
2293
2294#ifdef DEBUG_misha
2295#define CR_DBG_STR_STATE_LOAD_START "VBox.Cr.StateLoadStart"
2296#define CR_DBG_STR_STATE_LOAD_STOP "VBox.Cr.StateLoadStop"
2297
2298 if (cr_server.head_spu->dispatch_table.StringMarkerGREMEDY)
2299 cr_server.head_spu->dispatch_table.StringMarkerGREMEDY(sizeof (CR_DBG_STR_STATE_LOAD_START), CR_DBG_STR_STATE_LOAD_START);
2300#endif
2301
2302 /* Load and recreate rendering contexts */
2303 rc = SSMR3GetU32(pSSM, &uiNumElems);
2304 AssertRCReturn(rc, rc);
2305 for (ui=0; ui<uiNumElems; ++ui)
2306 {
2307 CRCreateInfo_t createInfo;
2308 char psz[200];
2309 GLint ctxID;
2310 CRContextInfo* pContextInfo;
2311 CRContext* pContext;
2312
2313 rc = SSMR3GetMem(pSSM, &key, sizeof(key));
2314 AssertRCReturn(rc, rc);
2315 rc = SSMR3GetMem(pSSM, &createInfo, sizeof(createInfo));
2316 AssertRCReturn(rc, rc);
2317
2318 if (createInfo.pszDpyName)
2319 {
2320 rc = SSMR3GetStrZEx(pSSM, psz, 200, NULL);
2321 AssertRCReturn(rc, rc);
2322 createInfo.pszDpyName = psz;
2323 }
2324
2325 ctxID = crServerDispatchCreateContextEx(createInfo.pszDpyName, createInfo.visualBits, 0, key, createInfo.externalID /* <-saved state stores internal id here*/);
2326 CRASSERT((int64_t)ctxID == (int64_t)key);
2327
2328 pContextInfo = (CRContextInfo*) crHashtableSearch(cr_server.contextTable, key);
2329 CRASSERT(pContextInfo);
2330 CRASSERT(pContextInfo->pContext);
2331 pContext = pContextInfo->pContext;
2332 pContext->shared->id=-1;
2333 }
2334
2335 if (version > SHCROGL_SSM_VERSION_WITH_BUGGY_FB_IMAGE_DATA)
2336 {
2337 CRASSERT(!Reader.pu8Buffer);
2338 /* we have a mural data here */
2339 rc = crVBoxServerLoadMurals(&Reader, version);
2340 AssertRCReturn(rc, rc);
2341 CRASSERT(!Reader.pu8Buffer);
2342 }
2343
2344 if (version > SHCROGL_SSM_VERSION_WITH_BUGGY_FB_IMAGE_DATA && uiNumElems)
2345 {
2346 /* set the current client to allow doing crServerPerformMakeCurrent later */
2347 CRASSERT(cr_server.numClients);
2348 cr_server.curClient = cr_server.clients[0];
2349 }
2350
2351 rc = crStateLoadGlobals(pSSM, version);
2352 AssertRCReturn(rc, rc);
2353
2354 if (uiNumElems)
2355 {
2356 /* ensure we have main context set up as current */
2357 CRMuralInfo *pMural;
2358 CRASSERT(cr_server.MainContextInfo.SpuContext > 0);
2359 CRASSERT(!cr_server.currentCtxInfo);
2360 CRASSERT(!cr_server.currentMural);
2361 pMural = crServerGetDummyMural(cr_server.MainContextInfo.CreateInfo.realVisualBits);
2362 CRASSERT(pMural);
2363 crServerPerformMakeCurrent(pMural, &cr_server.MainContextInfo);
2364 }
2365
2366 /* Restore context state data */
2367 for (ui=0; ui<uiNumElems; ++ui)
2368 {
2369 CRContextInfo* pContextInfo;
2370 CRContext *pContext;
2371 CRMuralInfo *pMural = NULL;
2372 int32_t winId = 0;
2373
2374 rc = SSMR3GetMem(pSSM, &key, sizeof(key));
2375 AssertRCReturn(rc, rc);
2376
2377 pContextInfo = (CRContextInfo*) crHashtableSearch(cr_server.contextTable, key);
2378 CRASSERT(pContextInfo);
2379 CRASSERT(pContextInfo->pContext);
2380 pContext = pContextInfo->pContext;
2381
2382 if (version > SHCROGL_SSM_VERSION_WITH_BUGGY_FB_IMAGE_DATA)
2383 {
2384 rc = SSMR3GetMem(pSSM, &winId, sizeof(winId));
2385 AssertRCReturn(rc, rc);
2386
2387 if (winId)
2388 {
2389 pMural = (CRMuralInfo*)crHashtableSearch(cr_server.muralTable, winId);
2390 CRASSERT(pMural);
2391 }
2392 else
2393 {
2394 /* null winId means a dummy mural, get it */
2395 pMural = crServerGetDummyMural(pContextInfo->CreateInfo.realVisualBits);
2396 CRASSERT(pMural);
2397 }
2398 }
2399
2400 rc = crStateLoadContext(pContext, cr_server.contextTable, crVBoxServerGetContextCB, pSSM, version);
2401 AssertRCReturn(rc, rc);
2402
2403 /*Restore front/back buffer images*/
2404 rc = crVBoxServerLoadFBImage(pSSM, version, pContextInfo, pMural);
2405 AssertRCReturn(rc, rc);
2406 }
2407
2408 if (version > SHCROGL_SSM_VERSION_WITH_BUGGY_FB_IMAGE_DATA)
2409 {
2410 CRContextInfo *pContextInfo;
2411 CRMuralInfo *pMural;
2412 GLint ctxId;
2413
2414 rc = SSMR3GetU32(pSSM, &uiNumElems);
2415 AssertRCReturn(rc, rc);
2416 for (ui=0; ui<uiNumElems; ++ui)
2417 {
2418 CRbitvalue initialCtxUsage[CR_MAX_BITARRAY];
2419 CRMuralInfo *pInitialCurMural;
2420
2421 rc = SSMR3GetMem(pSSM, &key, sizeof(key));
2422 AssertRCReturn(rc, rc);
2423
2424 rc = SSMR3GetMem(pSSM, &ctxId, sizeof(ctxId));
2425 AssertRCReturn(rc, rc);
2426
2427 pMural = (CRMuralInfo*)crHashtableSearch(cr_server.muralTable, key);
2428 CRASSERT(pMural);
2429 if (ctxId)
2430 {
2431 pContextInfo = (CRContextInfo *)crHashtableSearch(cr_server.contextTable, ctxId);
2432 CRASSERT(pContextInfo);
2433 }
2434 else
2435 pContextInfo = &cr_server.MainContextInfo;
2436
2437 crMemcpy(initialCtxUsage, pMural->ctxUsage, sizeof (initialCtxUsage));
2438 pInitialCurMural = pContextInfo->currentMural;
2439
2440 rc = crVBoxServerLoadFBImage(pSSM, version, pContextInfo, pMural);
2441 AssertRCReturn(rc, rc);
2442
2443 /* restore the reference data, we synchronize it with the HW state in a later crServerPerformMakeCurrent call */
2444 crMemcpy(pMural->ctxUsage, initialCtxUsage, sizeof (initialCtxUsage));
2445 pContextInfo->currentMural = pInitialCurMural;
2446 }
2447
2448 CRASSERT(!uiNumElems || cr_server.currentCtxInfo == &cr_server.MainContextInfo);
2449
2450 cr_server.curClient = NULL;
2451 cr_server.bForceMakeCurrentOnClientSwitch = GL_TRUE;
2452 }
2453 else
2454 {
2455 CRServerFreeIDsPool_t dummyIdsPool;
2456
2457 CRASSERT(!Reader.pu8Buffer);
2458
2459 /* we have a mural data here */
2460 rc = crVBoxServerLoadMurals(&Reader, version);
2461 AssertRCReturn(rc, rc);
2462
2463 /* not used any more, just read it out and ignore */
2464 rc = crServerLsrDataGetMem(&Reader, &dummyIdsPool, sizeof(dummyIdsPool));
2465 CRASSERT(rc == VINF_SUCCESS);
2466 }
2467
2468 /* Load clients info */
2469 for (i = 0; i < cr_server.numClients; i++)
2470 {
2471 if (cr_server.clients[i] && cr_server.clients[i]->conn)
2472 {
2473 CRClient *pClient = cr_server.clients[i];
2474 CRClient client;
2475 unsigned long ctxID=-1, winID=-1;
2476
2477 rc = crServerLsrDataGetU32(&Reader, &ui);
2478 AssertRCReturn(rc, rc);
2479 /* If this assert fires, then we should search correct client in the list first*/
2480 CRASSERT(ui == pClient->conn->u32ClientID);
2481
2482 if (version>=4)
2483 {
2484 rc = crServerLsrDataGetU32(&Reader, &pClient->conn->vMajor);
2485 AssertRCReturn(rc, rc);
2486
2487 rc = crServerLsrDataGetU32(&Reader, &pClient->conn->vMinor);
2488 AssertRCReturn(rc, rc);
2489 }
2490
2491 rc = crServerLsrDataGetMem(&Reader, &client, sizeof(client));
2492 CRASSERT(rc == VINF_SUCCESS);
2493
2494 client.conn = pClient->conn;
2495 /* We can't reassign client number, as we'd get wrong results in TranslateTextureID
2496 * and fail to bind old textures.
2497 */
2498 /*client.number = pClient->number;*/
2499 *pClient = client;
2500
2501 pClient->currentContextNumber = -1;
2502 pClient->currentCtxInfo = &cr_server.MainContextInfo;
2503 pClient->currentMural = NULL;
2504 pClient->currentWindow = -1;
2505
2506 cr_server.curClient = pClient;
2507
2508 if (client.currentCtxInfo && client.currentContextNumber > 0)
2509 {
2510 rc = crServerLsrDataGetMem(&Reader, &ctxID, sizeof(ctxID));
2511 AssertRCReturn(rc, rc);
2512 client.currentCtxInfo = (CRContextInfo*) crHashtableSearch(cr_server.contextTable, ctxID);
2513 CRASSERT(client.currentCtxInfo);
2514 CRASSERT(client.currentCtxInfo->pContext);
2515 //pClient->currentCtx = client.currentCtx;
2516 //pClient->currentContextNumber = ctxID;
2517 }
2518
2519 if (client.currentMural && client.currentWindow > 0)
2520 {
2521 rc = crServerLsrDataGetMem(&Reader, &winID, sizeof(winID));
2522 AssertRCReturn(rc, rc);
2523 client.currentMural = (CRMuralInfo*) crHashtableSearch(cr_server.muralTable, winID);
2524 CRASSERT(client.currentMural);
2525 //pClient->currentMural = client.currentMural;
2526 //pClient->currentWindow = winID;
2527 }
2528
2529 CRASSERT(!Reader.cbData);
2530
2531 /* Restore client active context and window */
2532 crServerDispatchMakeCurrent(winID, 0, ctxID);
2533
2534 if (0)
2535 {
2536// CRContext *tmpCtx;
2537// CRCreateInfo_t *createInfo;
2538 GLfloat one[4] = { 1, 1, 1, 1 };
2539 GLfloat amb[4] = { 0.4f, 0.4f, 0.4f, 1.0f };
2540
2541 crServerDispatchMakeCurrent(winID, 0, ctxID);
2542
2543 crHashtableWalk(client.currentCtxInfo->pContext->shared->textureTable, crVBoxServerSyncTextureCB, client.currentCtxInfo->pContext);
2544
2545 crStateTextureObjectDiff(client.currentCtxInfo->pContext, NULL, NULL, &client.currentCtxInfo->pContext->texture.base1D, GL_TRUE);
2546 crStateTextureObjectDiff(client.currentCtxInfo->pContext, NULL, NULL, &client.currentCtxInfo->pContext->texture.base2D, GL_TRUE);
2547 crStateTextureObjectDiff(client.currentCtxInfo->pContext, NULL, NULL, &client.currentCtxInfo->pContext->texture.base3D, GL_TRUE);
2548#ifdef CR_ARB_texture_cube_map
2549 crStateTextureObjectDiff(client.currentCtxInfo->pContext, NULL, NULL, &client.currentCtxInfo->pContext->texture.baseCubeMap, GL_TRUE);
2550#endif
2551#ifdef CR_NV_texture_rectangle
2552 //@todo this doesn't work as expected
2553 //crStateTextureObjectDiff(client.currentCtxInfo->pContext, NULL, NULL, &client.currentCtxInfo->pContext->texture.baseRect, GL_TRUE);
2554#endif
2555 /*cr_server.head_spu->dispatch_table.Materialfv(GL_FRONT_AND_BACK, GL_AMBIENT, amb);
2556 cr_server.head_spu->dispatch_table.LightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
2557 cr_server.head_spu->dispatch_table.Lightfv(GL_LIGHT1, GL_DIFFUSE, one);
2558
2559 cr_server.head_spu->dispatch_table.Enable(GL_LIGHTING);
2560 cr_server.head_spu->dispatch_table.Enable(GL_LIGHT0);
2561 cr_server.head_spu->dispatch_table.Enable(GL_LIGHT1);
2562
2563 cr_server.head_spu->dispatch_table.Enable(GL_CULL_FACE);
2564 cr_server.head_spu->dispatch_table.Enable(GL_TEXTURE_2D);*/
2565
2566 //crStateViewport( 0, 0, 600, 600 );
2567 //pClient->currentMural->viewportValidated = GL_FALSE;
2568 //cr_server.head_spu->dispatch_table.Viewport( 0, 0, 600, 600 );
2569
2570 //crStateMatrixMode(GL_PROJECTION);
2571 //cr_server.head_spu->dispatch_table.MatrixMode(GL_PROJECTION);
2572
2573 //crStateLoadIdentity();
2574 //cr_server.head_spu->dispatch_table.LoadIdentity();
2575
2576 //crStateFrustum(-0.5, 0.5, -0.5, 0.5, 1.5, 150.0);
2577 //cr_server.head_spu->dispatch_table.Frustum(-0.5, 0.5, -0.5, 0.5, 1.5, 150.0);
2578
2579 //crStateMatrixMode(GL_MODELVIEW);
2580 //cr_server.head_spu->dispatch_table.MatrixMode(GL_MODELVIEW);
2581 //crServerDispatchLoadIdentity();
2582 //crStateFrustum(-0.5, 0.5, -0.5, 0.5, 1.5, 150.0);
2583 //cr_server.head_spu->dispatch_table.Frustum(-0.5, 0.5, -0.5, 0.5, 1.5, 150.0);
2584 //crServerDispatchLoadIdentity();
2585
2586 /*createInfo = (CRCreateInfo_t *) crHashtableSearch(cr_server.pContextCreateInfoTable, ctxID);
2587 CRASSERT(createInfo);
2588 tmpCtx = crStateCreateContext(NULL, createInfo->visualBits, NULL);
2589 CRASSERT(tmpCtx);
2590 crStateDiffContext(tmpCtx, client.currentCtxInfo->pContext);
2591 crStateDestroyContext(tmpCtx);*/
2592 }
2593 }
2594 }
2595
2596 //crServerDispatchMakeCurrent(-1, 0, -1);
2597
2598 cr_server.curClient = NULL;
2599
2600 if (version >= SHCROGL_SSM_VERSION_WITH_SCREEN_INFO)
2601 {
2602 rc = CrPMgrLoadState(pSSM, version);
2603 AssertRCReturn(rc, rc);
2604 }
2605
2606 while ((err = cr_server.head_spu->dispatch_table.GetError()) != GL_NO_ERROR)
2607 crWarning("crServer: glGetError %d after loading snapshot", err);
2608
2609 cr_server.bIsInLoadingState = GL_FALSE;
2610
2611#if 0
2612 crVBoxServerCheckConsistency();
2613#endif
2614
2615#ifdef DEBUG_misha
2616 if (cr_server.head_spu->dispatch_table.StringMarkerGREMEDY)
2617 cr_server.head_spu->dispatch_table.StringMarkerGREMEDY(sizeof (CR_DBG_STR_STATE_LOAD_STOP), CR_DBG_STR_STATE_LOAD_STOP);
2618#endif
2619
2620 CRASSERT(!Reader.cbData);
2621 crServerLsrTerm(&Reader);
2622
2623 return VINF_SUCCESS;
2624}
2625
2626#define SCREEN(i) (cr_server.screen[i])
2627#define MAPPED(screen) ((screen).winID != 0)
2628
2629extern DECLEXPORT(void) crServerVBoxSetNotifyEventCB(PFNCRSERVERNOTIFYEVENT pfnCb)
2630{
2631 cr_server.pfnNotifyEventCB = pfnCb;
2632}
2633
2634void crVBoxServerNotifyEvent(int32_t idScreen, uint32_t uEvent, void*pvData)
2635{
2636 /* this is something unexpected, but just in case */
2637 if (idScreen >= cr_server.screenCount)
2638 {
2639 crWarning("invalid screen id %d", idScreen);
2640 return;
2641 }
2642
2643 cr_server.pfnNotifyEventCB(idScreen, uEvent, pvData);
2644}
2645
2646void crServerWindowReparent(CRMuralInfo *pMural)
2647{
2648 pMural->fHasParentWindow = !!cr_server.screen[pMural->screenId].winID;
2649
2650 renderspuReparentWindow(pMural->spuWindow);
2651}
2652
2653static void crVBoxServerReparentMuralCB(unsigned long key, void *data1, void *data2)
2654{
2655 CRMuralInfo *pMI = (CRMuralInfo*) data1;
2656 int *sIndex = (int*) data2;
2657
2658 if (pMI->screenId == *sIndex)
2659 {
2660 crServerWindowReparent(pMI);
2661 }
2662}
2663
2664DECLEXPORT(int32_t) crVBoxServerSetScreenCount(int sCount)
2665{
2666 int i;
2667
2668 if (sCount>CR_MAX_GUEST_MONITORS)
2669 return VERR_INVALID_PARAMETER;
2670
2671 /*Shouldn't happen yet, but to be safe in future*/
2672 for (i=0; i<cr_server.screenCount; ++i)
2673 {
2674 if (MAPPED(SCREEN(i)))
2675 crWarning("Screen count is changing, but screen[%i] is still mapped", i);
2676 return VERR_NOT_IMPLEMENTED;
2677 }
2678
2679 cr_server.screenCount = sCount;
2680
2681 for (i=0; i<sCount; ++i)
2682 {
2683 SCREEN(i).winID = 0;
2684 }
2685
2686 return VINF_SUCCESS;
2687}
2688
2689DECLEXPORT(int32_t) crVBoxServerUnmapScreen(int sIndex)
2690{
2691 crDebug("crVBoxServerUnmapScreen(%i)", sIndex);
2692
2693 if (sIndex<0 || sIndex>=cr_server.screenCount)
2694 return VERR_INVALID_PARAMETER;
2695
2696 if (MAPPED(SCREEN(sIndex)))
2697 {
2698 SCREEN(sIndex).winID = 0;
2699 renderspuSetWindowId(0);
2700
2701 crHashtableWalk(cr_server.muralTable, crVBoxServerReparentMuralCB, &sIndex);
2702
2703 crHashtableWalk(cr_server.dummyMuralTable, crVBoxServerReparentMuralCB, &sIndex);
2704
2705 CrPMgrScreenChanged((uint32_t)sIndex);
2706 }
2707
2708 renderspuSetWindowId(SCREEN(0).winID);
2709
2710 return VINF_SUCCESS;
2711}
2712
2713DECLEXPORT(int32_t) crVBoxServerMapScreen(int sIndex, int32_t x, int32_t y, uint32_t w, uint32_t h, uint64_t winID)
2714{
2715 crDebug("crVBoxServerMapScreen(%i) [%i,%i:%u,%u %x]", sIndex, x, y, w, h, winID);
2716
2717 if (sIndex<0 || sIndex>=cr_server.screenCount)
2718 return VERR_INVALID_PARAMETER;
2719
2720 if (MAPPED(SCREEN(sIndex)) && SCREEN(sIndex).winID!=winID)
2721 {
2722 crDebug("Mapped screen[%i] is being remapped.", sIndex);
2723 crVBoxServerUnmapScreen(sIndex);
2724 }
2725
2726 SCREEN(sIndex).winID = winID;
2727 SCREEN(sIndex).x = x;
2728 SCREEN(sIndex).y = y;
2729 SCREEN(sIndex).w = w;
2730 SCREEN(sIndex).h = h;
2731
2732 renderspuSetWindowId(SCREEN(sIndex).winID);
2733 crHashtableWalk(cr_server.muralTable, crVBoxServerReparentMuralCB, &sIndex);
2734
2735 crHashtableWalk(cr_server.dummyMuralTable, crVBoxServerReparentMuralCB, &sIndex);
2736 renderspuSetWindowId(SCREEN(0).winID);
2737
2738#ifndef WINDOWS
2739 /*Restore FB content for clients, which have current window on a screen being remapped*/
2740 {
2741 GLint i;
2742
2743 for (i = 0; i < cr_server.numClients; i++)
2744 {
2745 cr_server.curClient = cr_server.clients[i];
2746 if (cr_server.curClient->currentCtxInfo
2747 && cr_server.curClient->currentCtxInfo->pContext
2748 && (cr_server.curClient->currentCtxInfo->pContext->buffer.pFrontImg)
2749 && cr_server.curClient->currentMural
2750 && cr_server.curClient->currentMural->screenId == sIndex
2751 && cr_server.curClient->currentCtxInfo->pContext->buffer.storedHeight == h
2752 && cr_server.curClient->currentCtxInfo->pContext->buffer.storedWidth == w)
2753 {
2754 int clientWindow = cr_server.curClient->currentWindow;
2755 int clientContext = cr_server.curClient->currentContextNumber;
2756 CRFBData *pLazyData = (CRFBData *)cr_server.curClient->currentCtxInfo->pContext->buffer.pFrontImg;
2757
2758 if (clientWindow && clientWindow != cr_server.currentWindow)
2759 {
2760 crServerDispatchMakeCurrent(clientWindow, 0, clientContext);
2761 }
2762
2763 crStateApplyFBImage(cr_server.curClient->currentCtxInfo->pContext, pLazyData);
2764 crStateFreeFBImageLegacy(cr_server.curClient->currentCtxInfo->pContext);
2765 }
2766 }
2767 cr_server.curClient = NULL;
2768 }
2769#endif
2770
2771 CrPMgrScreenChanged((uint32_t)sIndex);
2772
2773 return VINF_SUCCESS;
2774}
2775
2776DECLEXPORT(int32_t) crVBoxServerSetRootVisibleRegion(GLint cRects, const RTRECT *pRects)
2777{
2778 int32_t rc = VINF_SUCCESS;
2779 GLboolean fOldRootVrOn = cr_server.fRootVrOn;
2780
2781 /* non-zero rects pointer indicate rects are present and switched on
2782 * i.e. cRects==0 and pRects!=NULL means root visible regioning is ON and there are no visible regions,
2783 * while pRects==NULL means root visible regioning is OFF, i.e. everything is visible */
2784 if (pRects)
2785 {
2786 crMemset(&cr_server.RootVrCurPoint, 0, sizeof (cr_server.RootVrCurPoint));
2787 rc = VBoxVrListRectsSet(&cr_server.RootVr, cRects, pRects, NULL);
2788 if (!RT_SUCCESS(rc))
2789 {
2790 crWarning("VBoxVrListRectsSet failed! rc %d", rc);
2791 return rc;
2792 }
2793
2794 cr_server.fRootVrOn = GL_TRUE;
2795 }
2796 else
2797 {
2798 if (!cr_server.fRootVrOn)
2799 return VINF_SUCCESS;
2800
2801 VBoxVrListClear(&cr_server.RootVr);
2802
2803 cr_server.fRootVrOn = GL_FALSE;
2804 }
2805
2806 if (!fOldRootVrOn != !cr_server.fRootVrOn)
2807 {
2808 rc = CrPMgrModeRootVr(cr_server.fRootVrOn);
2809 if (!RT_SUCCESS(rc))
2810 {
2811 crWarning("CrPMgrModeRootVr failed rc %d", rc);
2812 return rc;
2813 }
2814 }
2815 else if (cr_server.fRootVrOn)
2816 {
2817 rc = CrPMgrRootVrUpdate();
2818 if (!RT_SUCCESS(rc))
2819 {
2820 crWarning("CrPMgrRootVrUpdate failed rc %d", rc);
2821 return rc;
2822 }
2823 }
2824
2825 return VINF_SUCCESS;
2826}
2827
2828DECLEXPORT(void) crVBoxServerSetPresentFBOCB(PFNCRSERVERPRESENTFBO pfnPresentFBO)
2829{
2830 cr_server.pfnPresentFBO = pfnPresentFBO;
2831}
2832
2833DECLEXPORT(int32_t) crVBoxServerSetOffscreenRendering(GLboolean value)
2834{
2835 return CrPMgrModeVrdp(value);
2836}
2837
2838DECLEXPORT(int32_t) crVBoxServerOutputRedirectSet(const CROutputRedirect *pCallbacks)
2839{
2840 /* No need for a synchronization as this is single threaded. */
2841 if (pCallbacks)
2842 {
2843 cr_server.outputRedirect = *pCallbacks;
2844 }
2845 else
2846 {
2847 memset (&cr_server.outputRedirect, 0, sizeof (cr_server.outputRedirect));
2848 }
2849
2850 return VINF_SUCCESS;
2851}
2852
2853DECLEXPORT(int32_t) crVBoxServerSetScreenViewport(int sIndex, int32_t x, int32_t y, uint32_t w, uint32_t h)
2854{
2855 CRScreenViewportInfo *pViewport;
2856 RTRECT NewRect;
2857 int rc;
2858
2859 crDebug("crVBoxServerSetScreenViewport(%i)", sIndex);
2860
2861 if (sIndex<0 || sIndex>=cr_server.screenCount)
2862 {
2863 crWarning("crVBoxServerSetScreenViewport: invalid screen id %d", sIndex);
2864 return VERR_INVALID_PARAMETER;
2865 }
2866
2867 NewRect.xLeft = x;
2868 NewRect.yTop = y;
2869 NewRect.xRight = x + w;
2870 NewRect.yBottom = y + h;
2871
2872 pViewport = &cr_server.screenVieport[sIndex];
2873 /*always do viewport updates no matter whether the rectangle actually changes,
2874 * this is needed to ensure window is adjusted properly on OSX */
2875 pViewport->Rect = NewRect;
2876 rc = CrPMgrViewportUpdate((uint32_t)sIndex);
2877 if (!RT_SUCCESS(rc))
2878 {
2879 crWarning("CrPMgrViewportUpdate failed %d", rc);
2880 return rc;
2881 }
2882
2883 return VINF_SUCCESS;
2884}
2885
2886
2887#ifdef VBOX_WITH_CRHGSMI
2888
2889static int32_t crVBoxServerCmdVbvaCrCmdProcess(struct VBOXCMDVBVA_CRCMD_CMD *pCmd)
2890{
2891 int32_t rc;
2892 uint32_t cBuffers = pCmd->cBuffers;
2893 uint32_t cParams;
2894 uint32_t cbHdr;
2895 CRVBOXHGSMIHDR *pHdr;
2896 uint32_t u32Function;
2897 uint32_t u32ClientID;
2898 CRClient *pClient;
2899
2900 if (!g_pvVRamBase)
2901 {
2902 crWarning("g_pvVRamBase is not initialized");
2903 return VERR_INVALID_STATE;
2904 }
2905
2906 if (!cBuffers)
2907 {
2908 crWarning("zero buffers passed in!");
2909 return VERR_INVALID_PARAMETER;
2910 }
2911
2912 cParams = cBuffers-1;
2913
2914 cbHdr = pCmd->aBuffers[0].cbBuffer;
2915 pHdr = VBOXCRHGSMI_PTR_SAFE(pCmd->aBuffers[0].offBuffer, cbHdr, CRVBOXHGSMIHDR);
2916 if (!pHdr)
2917 {
2918 crWarning("invalid header buffer!");
2919 return VERR_INVALID_PARAMETER;
2920 }
2921
2922 if (cbHdr < sizeof (*pHdr))
2923 {
2924 crWarning("invalid header buffer size!");
2925 return VERR_INVALID_PARAMETER;
2926 }
2927
2928 u32Function = pHdr->u32Function;
2929 u32ClientID = pHdr->u32ClientID;
2930
2931 switch (u32Function)
2932 {
2933 case SHCRGL_GUEST_FN_WRITE:
2934 {
2935 Log(("svcCall: SHCRGL_GUEST_FN_WRITE\n"));
2936
2937 /* @todo: Verify */
2938 if (cParams == 1)
2939 {
2940 CRVBOXHGSMIWRITE* pFnCmd = (CRVBOXHGSMIWRITE*)pHdr;
2941 VBOXCMDVBVA_CRCMD_BUFFER *pBuf = &pCmd->aBuffers[1];
2942 /* Fetch parameters. */
2943 uint32_t cbBuffer = pBuf->cbBuffer;
2944 uint8_t *pBuffer = VBOXCRHGSMI_PTR_SAFE(pBuf->offBuffer, cbBuffer, uint8_t);
2945
2946 if (cbHdr < sizeof (*pFnCmd))
2947 {
2948 crWarning("invalid write cmd buffer size!");
2949 rc = VERR_INVALID_PARAMETER;
2950 break;
2951 }
2952
2953 CRASSERT(cbBuffer);
2954 if (!pBuffer)
2955 {
2956 crWarning("invalid buffer data received from guest!");
2957 rc = VERR_INVALID_PARAMETER;
2958 break;
2959 }
2960
2961 rc = crVBoxServerClientGet(u32ClientID, &pClient);
2962 if (RT_FAILURE(rc))
2963 {
2964 break;
2965 }
2966
2967 /* This should never fire unless we start to multithread */
2968 CRASSERT(pClient->conn->pBuffer==NULL && pClient->conn->cbBuffer==0);
2969 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
2970
2971 pClient->conn->pBuffer = pBuffer;
2972 pClient->conn->cbBuffer = cbBuffer;
2973 CRVBOXHGSMI_CMDDATA_SET(&pClient->conn->CmdData, pCmd, pHdr, false);
2974 rc = crVBoxServerInternalClientWriteRead(pClient);
2975 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
2976 return rc;
2977 }
2978 else
2979 {
2980 crWarning("invalid number of args");
2981 rc = VERR_INVALID_PARAMETER;
2982 break;
2983 }
2984 break;
2985 }
2986
2987 case SHCRGL_GUEST_FN_INJECT:
2988 {
2989 Log(("svcCall: SHCRGL_GUEST_FN_INJECT\n"));
2990
2991 /* @todo: Verify */
2992 if (cParams == 1)
2993 {
2994 CRVBOXHGSMIINJECT *pFnCmd = (CRVBOXHGSMIINJECT*)pHdr;
2995 /* Fetch parameters. */
2996 uint32_t u32InjectClientID = pFnCmd->u32ClientID;
2997 VBOXCMDVBVA_CRCMD_BUFFER *pBuf = &pCmd->aBuffers[1];
2998 uint32_t cbBuffer = pBuf->cbBuffer;
2999 uint8_t *pBuffer = VBOXCRHGSMI_PTR_SAFE(pBuf->offBuffer, cbBuffer, uint8_t);
3000
3001 if (cbHdr < sizeof (*pFnCmd))
3002 {
3003 crWarning("invalid inject cmd buffer size!");
3004 rc = VERR_INVALID_PARAMETER;
3005 break;
3006 }
3007
3008 CRASSERT(cbBuffer);
3009 if (!pBuffer)
3010 {
3011 crWarning("invalid buffer data received from guest!");
3012 rc = VERR_INVALID_PARAMETER;
3013 break;
3014 }
3015
3016 rc = crVBoxServerClientGet(u32InjectClientID, &pClient);
3017 if (RT_FAILURE(rc))
3018 {
3019 break;
3020 }
3021
3022 /* This should never fire unless we start to multithread */
3023 CRASSERT(pClient->conn->pBuffer==NULL && pClient->conn->cbBuffer==0);
3024 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
3025
3026 pClient->conn->pBuffer = pBuffer;
3027 pClient->conn->cbBuffer = cbBuffer;
3028 CRVBOXHGSMI_CMDDATA_SET(&pClient->conn->CmdData, pCmd, pHdr, false);
3029 rc = crVBoxServerInternalClientWriteRead(pClient);
3030 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
3031 return rc;
3032 }
3033
3034 crWarning("invalid number of args");
3035 rc = VERR_INVALID_PARAMETER;
3036 break;
3037 }
3038
3039 case SHCRGL_GUEST_FN_READ:
3040 {
3041 Log(("svcCall: SHCRGL_GUEST_FN_READ\n"));
3042
3043 /* @todo: Verify */
3044 if (cParams == 1)
3045 {
3046 CRVBOXHGSMIREAD *pFnCmd = (CRVBOXHGSMIREAD*)pHdr;
3047 VBOXCMDVBVA_CRCMD_BUFFER *pBuf = &pCmd->aBuffers[1];
3048 /* Fetch parameters. */
3049 uint32_t cbBuffer = pBuf->cbBuffer;
3050 uint8_t *pBuffer = VBOXCRHGSMI_PTR_SAFE(pBuf->offBuffer, cbBuffer, uint8_t);
3051
3052 if (cbHdr < sizeof (*pFnCmd))
3053 {
3054 crWarning("invalid read cmd buffer size!");
3055 rc = VERR_INVALID_PARAMETER;
3056 break;
3057 }
3058
3059
3060 if (!pBuffer)
3061 {
3062 crWarning("invalid buffer data received from guest!");
3063 rc = VERR_INVALID_PARAMETER;
3064 break;
3065 }
3066
3067 rc = crVBoxServerClientGet(u32ClientID, &pClient);
3068 if (RT_FAILURE(rc))
3069 {
3070 break;
3071 }
3072
3073 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
3074
3075 rc = crVBoxServerInternalClientRead(pClient, pBuffer, &cbBuffer);
3076
3077 /* Return the required buffer size always */
3078 pFnCmd->cbBuffer = cbBuffer;
3079
3080 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
3081
3082 /* the read command is never pended, complete it right away */
3083 pHdr->result = rc;
3084
3085 return VINF_SUCCESS;
3086 }
3087
3088 crWarning("invalid number of args");
3089 rc = VERR_INVALID_PARAMETER;
3090 break;
3091 }
3092
3093 case SHCRGL_GUEST_FN_WRITE_READ:
3094 {
3095 Log(("svcCall: SHCRGL_GUEST_FN_WRITE_READ\n"));
3096
3097 /* @todo: Verify */
3098 if (cParams == 2)
3099 {
3100 CRVBOXHGSMIWRITEREAD *pFnCmd = (CRVBOXHGSMIWRITEREAD*)pHdr;
3101 VBOXCMDVBVA_CRCMD_BUFFER *pBuf = &pCmd->aBuffers[1];
3102 VBOXCMDVBVA_CRCMD_BUFFER *pWbBuf = &pCmd->aBuffers[2];
3103
3104 /* Fetch parameters. */
3105 uint32_t cbBuffer = pBuf->cbBuffer;
3106 uint8_t *pBuffer = VBOXCRHGSMI_PTR_SAFE(pBuf->offBuffer, cbBuffer, uint8_t);
3107
3108 uint32_t cbWriteback = pWbBuf->cbBuffer;
3109 char *pWriteback = VBOXCRHGSMI_PTR_SAFE(pWbBuf->offBuffer, cbWriteback, char);
3110
3111 if (cbHdr < sizeof (*pFnCmd))
3112 {
3113 crWarning("invalid write_read cmd buffer size!");
3114 rc = VERR_INVALID_PARAMETER;
3115 break;
3116 }
3117
3118
3119 CRASSERT(cbBuffer);
3120 if (!pBuffer)
3121 {
3122 crWarning("invalid write buffer data received from guest!");
3123 rc = VERR_INVALID_PARAMETER;
3124 break;
3125 }
3126
3127 CRASSERT(cbWriteback);
3128 if (!pWriteback)
3129 {
3130 crWarning("invalid writeback buffer data received from guest!");
3131 rc = VERR_INVALID_PARAMETER;
3132 break;
3133 }
3134 rc = crVBoxServerClientGet(u32ClientID, &pClient);
3135 if (RT_FAILURE(rc))
3136 {
3137 pHdr->result = rc;
3138 return VINF_SUCCESS;
3139 }
3140
3141 /* This should never fire unless we start to multithread */
3142 CRASSERT(pClient->conn->pBuffer==NULL && pClient->conn->cbBuffer==0);
3143 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
3144
3145 pClient->conn->pBuffer = pBuffer;
3146 pClient->conn->cbBuffer = cbBuffer;
3147 CRVBOXHGSMI_CMDDATA_SETWB(&pClient->conn->CmdData, pCmd, pHdr, pWriteback, cbWriteback, &pFnCmd->cbWriteback, false);
3148 rc = crVBoxServerInternalClientWriteRead(pClient);
3149 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
3150 return rc;
3151 }
3152
3153 crWarning("invalid number of args");
3154 rc = VERR_INVALID_PARAMETER;
3155 break;
3156 }
3157
3158 case SHCRGL_GUEST_FN_SET_VERSION:
3159 {
3160 crWarning("invalid function");
3161 rc = VERR_NOT_IMPLEMENTED;
3162 break;
3163 }
3164
3165 case SHCRGL_GUEST_FN_SET_PID:
3166 {
3167 crWarning("invalid function");
3168 rc = VERR_NOT_IMPLEMENTED;
3169 break;
3170 }
3171
3172 default:
3173 {
3174 crWarning("invalid function");
3175 rc = VERR_NOT_IMPLEMENTED;
3176 break;
3177 }
3178
3179 }
3180
3181 /* we can be on fail only here */
3182 CRASSERT(RT_FAILURE(rc));
3183 pHdr->result = rc;
3184
3185 return rc;
3186}
3187
3188static DECLCALLBACK(int) crVBoxCrCmdEnable(HVBOXCRCMDSVR hSvr, VBOXCRCMD_SVRENABLE_INFO *pInfo)
3189{
3190 cr_server.CrCmdClientInfo = *pInfo;
3191 AssertFailed();
3192 return VERR_NOT_IMPLEMENTED;
3193}
3194
3195static DECLCALLBACK(int) crVBoxCrCmdDisable(HVBOXCRCMDSVR hSvr)
3196{
3197 AssertFailed();
3198 memset(&cr_server.CrCmdClientInfo, 0, sizeof (cr_server.CrCmdClientInfo));
3199 return VERR_NOT_IMPLEMENTED;
3200}
3201
3202static DECLCALLBACK(int) crVBoxCrCmdHostCtl(HVBOXCRCMDSVR hSvr, uint8_t* pCmd, uint32_t cbCmd)
3203{
3204 AssertFailed();
3205 return VERR_NOT_IMPLEMENTED;
3206}
3207
3208static DECLCALLBACK(int) crVBoxCrCmdGuestCtl(HVBOXCRCMDSVR hSvr, uint8_t* pCmd, uint32_t cbCmd)
3209{
3210 AssertFailed();
3211 return VERR_NOT_IMPLEMENTED;
3212}
3213
3214static DECLCALLBACK(int) crVBoxCrCmdSaveState(HVBOXCRCMDSVR hSvr, PSSMHANDLE pSSM)
3215{
3216 AssertFailed();
3217 return VERR_NOT_IMPLEMENTED;
3218}
3219
3220static DECLCALLBACK(int) crVBoxCrCmdLoadState(HVBOXCRCMDSVR hSvr, PSSMHANDLE pSSM, uint32_t u32Version)
3221{
3222 AssertFailed();
3223 return VERR_NOT_IMPLEMENTED;
3224}
3225
3226
3227static DECLCALLBACK(int) crVBoxCrCmdCmd(HVBOXCRCMDSVR hSvr, PVBOXCMDVBVA_HDR pCmd, uint32_t cbCmd)
3228{
3229 AssertFailed();
3230 switch (pCmd->u8OpCode)
3231 {
3232 case VBOXCMDVBVA_OPTYPE_CRCMD:
3233 {
3234 VBOXCMDVBVA_CRCMD *pCrCmdDr = (VBOXCMDVBVA_CRCMD*)pCmd;
3235 VBOXCMDVBVA_CRCMD_CMD *pCrCmd = &pCrCmdDr->Cmd;
3236 int rc = crVBoxServerCmdVbvaCrCmdProcess(pCrCmd);
3237 if (RT_SUCCESS(rc))
3238 {
3239 /* success */
3240 pCmd->u.i8Result = 0;
3241 }
3242 else
3243 {
3244 crWarning("crVBoxServerCmdVbvaCrCmdProcess failed, rc %d", rc);
3245 pCmd->u.i8Result = -1;
3246 }
3247 break;
3248 }
3249 case VBOXCMDVBVA_OPTYPE_BLT_OFFPRIMSZFMT_OR_ID:
3250 {
3251 crVBoxServerCrCmdBltProcess(pCmd, cbCmd);
3252 break;
3253 }
3254 default:
3255 WARN(("unsupported command"));
3256 pCmd->u.i8Result = -1;
3257 }
3258 return VINF_SUCCESS;
3259}
3260
3261/* We moved all CrHgsmi command processing to crserverlib to keep the logic of dealing with CrHgsmi commands in one place.
3262 *
3263 * 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.
3264 * This help avoiding the "blocked-client" issues. The client is blocked if another client is doing begin-end stuff.
3265 * For now we eliminated polling that could occur on block, which caused a higher-priority thread (in guest) polling for the blocked command complition
3266 * to block the lower-priority thread trying to complete the blocking command.
3267 * And removed extra memcpy done on blocked command arrival.
3268 *
3269 * In the future we will extend CrHgsmi functionality to maintain texture data directly in CrHgsmi allocation to avoid extra memcpy-ing with PBO,
3270 * implement command completion and stuff necessary for GPU scheduling to work properly for WDDM Windows guests, etc.
3271 *
3272 * NOTE: it is ALWAYS responsibility of the crVBoxServerCrHgsmiCmd to complete the command!
3273 * */
3274
3275
3276int32_t crVBoxServerCrHgsmiCmd(struct VBOXVDMACMD_CHROMIUM_CMD *pCmd, uint32_t cbCmd)
3277{
3278
3279 int32_t rc;
3280 uint32_t cBuffers = pCmd->cBuffers;
3281 uint32_t cParams;
3282 uint32_t cbHdr;
3283 CRVBOXHGSMIHDR *pHdr;
3284 uint32_t u32Function;
3285 uint32_t u32ClientID;
3286 CRClient *pClient;
3287
3288 if (!g_pvVRamBase)
3289 {
3290 crWarning("g_pvVRamBase is not initialized");
3291
3292 crServerCrHgsmiCmdComplete(pCmd, VERR_INVALID_STATE);
3293 return VINF_SUCCESS;
3294 }
3295
3296 if (!cBuffers)
3297 {
3298 crWarning("zero buffers passed in!");
3299
3300 crServerCrHgsmiCmdComplete(pCmd, VERR_INVALID_PARAMETER);
3301 return VINF_SUCCESS;
3302 }
3303
3304 cParams = cBuffers-1;
3305
3306 cbHdr = pCmd->aBuffers[0].cbBuffer;
3307 pHdr = VBOXCRHGSMI_PTR_SAFE(pCmd->aBuffers[0].offBuffer, cbHdr, CRVBOXHGSMIHDR);
3308 if (!pHdr)
3309 {
3310 crWarning("invalid header buffer!");
3311
3312 crServerCrHgsmiCmdComplete(pCmd, VERR_INVALID_PARAMETER);
3313 return VINF_SUCCESS;
3314 }
3315
3316 if (cbHdr < sizeof (*pHdr))
3317 {
3318 crWarning("invalid header buffer size!");
3319
3320 crServerCrHgsmiCmdComplete(pCmd, VERR_INVALID_PARAMETER);
3321 return VINF_SUCCESS;
3322 }
3323
3324 u32Function = pHdr->u32Function;
3325 u32ClientID = pHdr->u32ClientID;
3326
3327 switch (u32Function)
3328 {
3329 case SHCRGL_GUEST_FN_WRITE:
3330 {
3331 Log(("svcCall: SHCRGL_GUEST_FN_WRITE\n"));
3332
3333 /* @todo: Verify */
3334 if (cParams == 1)
3335 {
3336 CRVBOXHGSMIWRITE* pFnCmd = (CRVBOXHGSMIWRITE*)pHdr;
3337 VBOXVDMACMD_CHROMIUM_BUFFER *pBuf = &pCmd->aBuffers[1];
3338 /* Fetch parameters. */
3339 uint32_t cbBuffer = pBuf->cbBuffer;
3340 uint8_t *pBuffer = VBOXCRHGSMI_PTR_SAFE(pBuf->offBuffer, cbBuffer, uint8_t);
3341
3342 if (cbHdr < sizeof (*pFnCmd))
3343 {
3344 crWarning("invalid write cmd buffer size!");
3345 rc = VERR_INVALID_PARAMETER;
3346 break;
3347 }
3348
3349 CRASSERT(cbBuffer);
3350 if (!pBuffer)
3351 {
3352 crWarning("invalid buffer data received from guest!");
3353 rc = VERR_INVALID_PARAMETER;
3354 break;
3355 }
3356
3357 rc = crVBoxServerClientGet(u32ClientID, &pClient);
3358 if (RT_FAILURE(rc))
3359 {
3360 break;
3361 }
3362
3363 /* This should never fire unless we start to multithread */
3364 CRASSERT(pClient->conn->pBuffer==NULL && pClient->conn->cbBuffer==0);
3365 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
3366
3367 pClient->conn->pBuffer = pBuffer;
3368 pClient->conn->cbBuffer = cbBuffer;
3369 CRVBOXHGSMI_CMDDATA_SET(&pClient->conn->CmdData, pCmd, pHdr, true);
3370 rc = crVBoxServerInternalClientWriteRead(pClient);
3371 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
3372 return rc;
3373 }
3374 else
3375 {
3376 crWarning("invalid number of args");
3377 rc = VERR_INVALID_PARAMETER;
3378 break;
3379 }
3380 break;
3381 }
3382
3383 case SHCRGL_GUEST_FN_INJECT:
3384 {
3385 Log(("svcCall: SHCRGL_GUEST_FN_INJECT\n"));
3386
3387 /* @todo: Verify */
3388 if (cParams == 1)
3389 {
3390 CRVBOXHGSMIINJECT *pFnCmd = (CRVBOXHGSMIINJECT*)pHdr;
3391 /* Fetch parameters. */
3392 uint32_t u32InjectClientID = pFnCmd->u32ClientID;
3393 VBOXVDMACMD_CHROMIUM_BUFFER *pBuf = &pCmd->aBuffers[1];
3394 uint32_t cbBuffer = pBuf->cbBuffer;
3395 uint8_t *pBuffer = VBOXCRHGSMI_PTR_SAFE(pBuf->offBuffer, cbBuffer, uint8_t);
3396
3397 if (cbHdr < sizeof (*pFnCmd))
3398 {
3399 crWarning("invalid inject cmd buffer size!");
3400 rc = VERR_INVALID_PARAMETER;
3401 break;
3402 }
3403
3404 CRASSERT(cbBuffer);
3405 if (!pBuffer)
3406 {
3407 crWarning("invalid buffer data received from guest!");
3408 rc = VERR_INVALID_PARAMETER;
3409 break;
3410 }
3411
3412 rc = crVBoxServerClientGet(u32InjectClientID, &pClient);
3413 if (RT_FAILURE(rc))
3414 {
3415 break;
3416 }
3417
3418 /* This should never fire unless we start to multithread */
3419 CRASSERT(pClient->conn->pBuffer==NULL && pClient->conn->cbBuffer==0);
3420 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
3421
3422 pClient->conn->pBuffer = pBuffer;
3423 pClient->conn->cbBuffer = cbBuffer;
3424 CRVBOXHGSMI_CMDDATA_SET(&pClient->conn->CmdData, pCmd, pHdr, true);
3425 rc = crVBoxServerInternalClientWriteRead(pClient);
3426 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
3427 return rc;
3428 }
3429
3430 crWarning("invalid number of args");
3431 rc = VERR_INVALID_PARAMETER;
3432 break;
3433 }
3434
3435 case SHCRGL_GUEST_FN_READ:
3436 {
3437 Log(("svcCall: SHCRGL_GUEST_FN_READ\n"));
3438
3439 /* @todo: Verify */
3440 if (cParams == 1)
3441 {
3442 CRVBOXHGSMIREAD *pFnCmd = (CRVBOXHGSMIREAD*)pHdr;
3443 VBOXVDMACMD_CHROMIUM_BUFFER *pBuf = &pCmd->aBuffers[1];
3444 /* Fetch parameters. */
3445 uint32_t cbBuffer = pBuf->cbBuffer;
3446 uint8_t *pBuffer = VBOXCRHGSMI_PTR_SAFE(pBuf->offBuffer, cbBuffer, uint8_t);
3447
3448 if (cbHdr < sizeof (*pFnCmd))
3449 {
3450 crWarning("invalid read cmd buffer size!");
3451 rc = VERR_INVALID_PARAMETER;
3452 break;
3453 }
3454
3455
3456 if (!pBuffer)
3457 {
3458 crWarning("invalid buffer data received from guest!");
3459 rc = VERR_INVALID_PARAMETER;
3460 break;
3461 }
3462
3463 rc = crVBoxServerClientGet(u32ClientID, &pClient);
3464 if (RT_FAILURE(rc))
3465 {
3466 break;
3467 }
3468
3469 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
3470
3471 rc = crVBoxServerInternalClientRead(pClient, pBuffer, &cbBuffer);
3472
3473 /* Return the required buffer size always */
3474 pFnCmd->cbBuffer = cbBuffer;
3475
3476 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
3477
3478 /* the read command is never pended, complete it right away */
3479 pHdr->result = rc;
3480
3481 crServerCrHgsmiCmdComplete(pCmd, VINF_SUCCESS);
3482 return VINF_SUCCESS;
3483 }
3484
3485 crWarning("invalid number of args");
3486 rc = VERR_INVALID_PARAMETER;
3487 break;
3488 }
3489
3490 case SHCRGL_GUEST_FN_WRITE_READ:
3491 {
3492 Log(("svcCall: SHCRGL_GUEST_FN_WRITE_READ\n"));
3493
3494 /* @todo: Verify */
3495 if (cParams == 2)
3496 {
3497 CRVBOXHGSMIWRITEREAD *pFnCmd = (CRVBOXHGSMIWRITEREAD*)pHdr;
3498 VBOXVDMACMD_CHROMIUM_BUFFER *pBuf = &pCmd->aBuffers[1];
3499 VBOXVDMACMD_CHROMIUM_BUFFER *pWbBuf = &pCmd->aBuffers[2];
3500
3501 /* Fetch parameters. */
3502 uint32_t cbBuffer = pBuf->cbBuffer;
3503 uint8_t *pBuffer = VBOXCRHGSMI_PTR_SAFE(pBuf->offBuffer, cbBuffer, uint8_t);
3504
3505 uint32_t cbWriteback = pWbBuf->cbBuffer;
3506 char *pWriteback = VBOXCRHGSMI_PTR_SAFE(pWbBuf->offBuffer, cbWriteback, char);
3507
3508 if (cbHdr < sizeof (*pFnCmd))
3509 {
3510 crWarning("invalid write_read cmd buffer size!");
3511 rc = VERR_INVALID_PARAMETER;
3512 break;
3513 }
3514
3515
3516 CRASSERT(cbBuffer);
3517 if (!pBuffer)
3518 {
3519 crWarning("invalid write buffer data received from guest!");
3520 rc = VERR_INVALID_PARAMETER;
3521 break;
3522 }
3523
3524 CRASSERT(cbWriteback);
3525 if (!pWriteback)
3526 {
3527 crWarning("invalid writeback buffer data received from guest!");
3528 rc = VERR_INVALID_PARAMETER;
3529 break;
3530 }
3531 rc = crVBoxServerClientGet(u32ClientID, &pClient);
3532 if (RT_FAILURE(rc))
3533 {
3534 pHdr->result = rc;
3535 crServerCrHgsmiCmdComplete(pCmd, VINF_SUCCESS);
3536 return rc;
3537 }
3538
3539 /* This should never fire unless we start to multithread */
3540 CRASSERT(pClient->conn->pBuffer==NULL && pClient->conn->cbBuffer==0);
3541 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
3542
3543 pClient->conn->pBuffer = pBuffer;
3544 pClient->conn->cbBuffer = cbBuffer;
3545 CRVBOXHGSMI_CMDDATA_SETWB(&pClient->conn->CmdData, pCmd, pHdr, pWriteback, cbWriteback, &pFnCmd->cbWriteback, true);
3546 rc = crVBoxServerInternalClientWriteRead(pClient);
3547 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
3548 return rc;
3549 }
3550
3551 crWarning("invalid number of args");
3552 rc = VERR_INVALID_PARAMETER;
3553 break;
3554 }
3555
3556 case SHCRGL_GUEST_FN_SET_VERSION:
3557 {
3558 crWarning("invalid function");
3559 rc = VERR_NOT_IMPLEMENTED;
3560 break;
3561 }
3562
3563 case SHCRGL_GUEST_FN_SET_PID:
3564 {
3565 crWarning("invalid function");
3566 rc = VERR_NOT_IMPLEMENTED;
3567 break;
3568 }
3569
3570 default:
3571 {
3572 crWarning("invalid function");
3573 rc = VERR_NOT_IMPLEMENTED;
3574 break;
3575 }
3576
3577 }
3578
3579 /* we can be on fail only here */
3580 CRASSERT(RT_FAILURE(rc));
3581 pHdr->result = rc;
3582
3583 crServerCrHgsmiCmdComplete(pCmd, VINF_SUCCESS);
3584 return rc;
3585
3586}
3587
3588static DECLCALLBACK(bool) crVBoxServerHasData()
3589{
3590 HCR_FRAMEBUFFER hFb = CrPMgrFbGetFirstEnabled();
3591 for (;
3592 hFb;
3593 hFb = CrPMgrFbGetNextEnabled(hFb))
3594 {
3595 if (CrFbHas3DData(hFb))
3596 return true;
3597 }
3598
3599 return false;
3600}
3601
3602int32_t crVBoxServerCrHgsmiCtl(struct VBOXVDMACMD_CHROMIUM_CTL *pCtl, uint32_t cbCtl)
3603{
3604 int rc = VINF_SUCCESS;
3605
3606 switch (pCtl->enmType)
3607 {
3608 case VBOXVDMACMD_CHROMIUM_CTL_TYPE_CRHGSMI_SETUP:
3609 {
3610 PVBOXVDMACMD_CHROMIUM_CTL_CRHGSMI_SETUP pSetup = (PVBOXVDMACMD_CHROMIUM_CTL_CRHGSMI_SETUP)pCtl;
3611 g_pvVRamBase = (uint8_t*)pSetup->pvVRamBase;
3612 g_cbVRam = pSetup->cbVRam;
3613 pSetup->CrCmdServerInfo.hSvr = NULL;
3614 pSetup->CrCmdServerInfo.pfnEnable = crVBoxCrCmdEnable;
3615 pSetup->CrCmdServerInfo.pfnDisable = crVBoxCrCmdDisable;
3616 pSetup->CrCmdServerInfo.pfnCmd = crVBoxCrCmdCmd;
3617 pSetup->CrCmdServerInfo.pfnHostCtl = crVBoxCrCmdHostCtl;
3618 pSetup->CrCmdServerInfo.pfnGuestCtl = crVBoxCrCmdGuestCtl;
3619 pSetup->CrCmdServerInfo.pfnSaveState = crVBoxCrCmdSaveState;
3620 pSetup->CrCmdServerInfo.pfnLoadState = crVBoxCrCmdLoadState;
3621 rc = VINF_SUCCESS;
3622 break;
3623 }
3624 case VBOXVDMACMD_CHROMIUM_CTL_TYPE_SAVESTATE_BEGIN:
3625 case VBOXVDMACMD_CHROMIUM_CTL_TYPE_SAVESTATE_END:
3626 rc = VINF_SUCCESS;
3627 break;
3628 case VBOXVDMACMD_CHROMIUM_CTL_TYPE_CRHGSMI_SETUP_MAINCB:
3629 {
3630 PVBOXVDMACMD_CHROMIUM_CTL_CRHGSMI_SETUP_MAINCB pSetup = (PVBOXVDMACMD_CHROMIUM_CTL_CRHGSMI_SETUP_MAINCB)pCtl;
3631 g_hCrHgsmiCompletion = pSetup->hCompletion;
3632 g_pfnCrHgsmiCompletion = pSetup->pfnCompletion;
3633
3634 pSetup->MainInterface.pfnHasData = crVBoxServerHasData;
3635
3636 rc = VINF_SUCCESS;
3637 break;
3638 }
3639 default:
3640 AssertMsgFailed(("invalid param %d", pCtl->enmType));
3641 rc = VERR_INVALID_PARAMETER;
3642 }
3643
3644 /* NOTE: Control commands can NEVER be pended here, this is why its a task of a caller (Main)
3645 * to complete them accordingly.
3646 * This approach allows using host->host and host->guest commands in the same way here
3647 * making the command completion to be the responsibility of the command originator.
3648 * E.g. ctl commands can be both Hgcm Host synchronous commands that do not require completion at all,
3649 * or Hgcm Host Fast Call commands that do require completion. All this details are hidden here */
3650 return rc;
3651}
3652
3653void crVBoxServerDefaultContextSet()
3654{
3655 GLint spuWindow, spuCtx;
3656
3657 if (cr_server.MainContextInfo.SpuContext)
3658 {
3659 CRMuralInfo *pMural = crServerGetDummyMural(cr_server.MainContextInfo.CreateInfo.realVisualBits);
3660 if (!pMural)
3661 {
3662 WARN(("dummy mural is NULL"));
3663 spuCtx = CR_RENDER_DEFAULT_CONTEXT_ID;
3664 spuWindow = CR_RENDER_DEFAULT_WINDOW_ID;
3665 }
3666 else
3667 {
3668 spuCtx = cr_server.MainContextInfo.SpuContext;
3669 spuWindow = pMural->CreateInfo.realVisualBits;
3670 }
3671 }
3672 else
3673 {
3674 spuCtx = CR_RENDER_DEFAULT_CONTEXT_ID;
3675 spuWindow = CR_RENDER_DEFAULT_WINDOW_ID;
3676 }
3677
3678 cr_server.head_spu->dispatch_table.MakeCurrent(spuWindow, 0, spuCtx);
3679}
3680
3681int32_t crVBoxServerHgcmEnable(HVBOXCRCMDCTL_REMAINING_HOST_COMMAND hRHCmd, PFNVBOXCRCMDCTL_REMAINING_HOST_COMMAND pfnRHCmd)
3682{
3683 int rc = VINF_SUCCESS;
3684 uint8_t* pCtl;
3685 uint32_t cbCtl;
3686
3687 if (cr_server.numClients)
3688 {
3689 WARN(("cr_server.numClients(%d) is not NULL", cr_server.numClients));
3690 return VERR_INVALID_STATE;
3691 }
3692
3693 for (pCtl = pfnRHCmd(hRHCmd, &cbCtl, rc); pCtl; pCtl = pfnRHCmd(hRHCmd, &cbCtl, rc))
3694 {
3695 rc = crVBoxCrCmdHostCtl(NULL, pCtl, cbCtl);
3696 }
3697
3698 crVBoxServerDefaultContextSet();
3699
3700 return VINF_SUCCESS;
3701}
3702
3703int32_t crVBoxServerHgcmDisable()
3704{
3705 if (cr_server.numClients)
3706 {
3707 WARN(("cr_server.numClients(%d) is not NULL", cr_server.numClients));
3708 return VERR_INVALID_STATE;
3709 }
3710
3711 cr_server.head_spu->dispatch_table.MakeCurrent(0, 0, 0);
3712
3713 return VINF_SUCCESS;
3714}
3715
3716#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