VirtualBox

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

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

crOpenGL: include 3D data in framebuffer saved state snapshot, saved state fix

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 114.7 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 Assert(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 HCR_FRAMEBUFFER hFb;
2603
2604 rc = CrPMgrLoadState(pSSM, version);
2605 AssertRCReturn(rc, rc);
2606 }
2607
2608 while ((err = cr_server.head_spu->dispatch_table.GetError()) != GL_NO_ERROR)
2609 crWarning("crServer: glGetError %d after loading snapshot", err);
2610
2611 cr_server.bIsInLoadingState = GL_FALSE;
2612
2613#if 0
2614 crVBoxServerCheckConsistency();
2615#endif
2616
2617#ifdef DEBUG_misha
2618 if (cr_server.head_spu->dispatch_table.StringMarkerGREMEDY)
2619 cr_server.head_spu->dispatch_table.StringMarkerGREMEDY(sizeof (CR_DBG_STR_STATE_LOAD_STOP), CR_DBG_STR_STATE_LOAD_STOP);
2620#endif
2621
2622 CRASSERT(!Reader.cbData);
2623 crServerLsrTerm(&Reader);
2624
2625 return VINF_SUCCESS;
2626}
2627
2628#define SCREEN(i) (cr_server.screen[i])
2629#define MAPPED(screen) ((screen).winID != 0)
2630
2631extern DECLEXPORT(void) crServerVBoxSetNotifyEventCB(PFNCRSERVERNOTIFYEVENT pfnCb)
2632{
2633 cr_server.pfnNotifyEventCB = pfnCb;
2634}
2635
2636void crVBoxServerNotifyEvent(int32_t idScreen, uint32_t uEvent, void*pvData)
2637{
2638 /* this is something unexpected, but just in case */
2639 if (idScreen >= cr_server.screenCount)
2640 {
2641 crWarning("invalid screen id %d", idScreen);
2642 return;
2643 }
2644
2645 cr_server.pfnNotifyEventCB(idScreen, uEvent, pvData);
2646}
2647
2648void crServerWindowReparent(CRMuralInfo *pMural)
2649{
2650 pMural->fHasParentWindow = !!cr_server.screen[pMural->screenId].winID;
2651
2652 renderspuReparentWindow(pMural->spuWindow);
2653}
2654
2655static void crVBoxServerReparentMuralCB(unsigned long key, void *data1, void *data2)
2656{
2657 CRMuralInfo *pMI = (CRMuralInfo*) data1;
2658 int *sIndex = (int*) data2;
2659
2660 if (pMI->screenId == *sIndex)
2661 {
2662 crServerWindowReparent(pMI);
2663 }
2664}
2665
2666DECLEXPORT(int32_t) crVBoxServerSetScreenCount(int sCount)
2667{
2668 int i;
2669
2670 if (sCount>CR_MAX_GUEST_MONITORS)
2671 return VERR_INVALID_PARAMETER;
2672
2673 /*Shouldn't happen yet, but to be safe in future*/
2674 for (i=0; i<cr_server.screenCount; ++i)
2675 {
2676 if (MAPPED(SCREEN(i)))
2677 crWarning("Screen count is changing, but screen[%i] is still mapped", i);
2678 return VERR_NOT_IMPLEMENTED;
2679 }
2680
2681 cr_server.screenCount = sCount;
2682
2683 for (i=0; i<sCount; ++i)
2684 {
2685 SCREEN(i).winID = 0;
2686 }
2687
2688 return VINF_SUCCESS;
2689}
2690
2691DECLEXPORT(int32_t) crVBoxServerUnmapScreen(int sIndex)
2692{
2693 crDebug("crVBoxServerUnmapScreen(%i)", sIndex);
2694
2695 if (sIndex<0 || sIndex>=cr_server.screenCount)
2696 return VERR_INVALID_PARAMETER;
2697
2698 if (MAPPED(SCREEN(sIndex)))
2699 {
2700 SCREEN(sIndex).winID = 0;
2701 renderspuSetWindowId(0);
2702
2703 crHashtableWalk(cr_server.muralTable, crVBoxServerReparentMuralCB, &sIndex);
2704
2705 crHashtableWalk(cr_server.dummyMuralTable, crVBoxServerReparentMuralCB, &sIndex);
2706
2707 CrPMgrScreenChanged((uint32_t)sIndex);
2708 }
2709
2710 renderspuSetWindowId(SCREEN(0).winID);
2711
2712 return VINF_SUCCESS;
2713}
2714
2715DECLEXPORT(int32_t) crVBoxServerMapScreen(int sIndex, int32_t x, int32_t y, uint32_t w, uint32_t h, uint64_t winID)
2716{
2717 crDebug("crVBoxServerMapScreen(%i) [%i,%i:%u,%u %x]", sIndex, x, y, w, h, winID);
2718
2719 if (sIndex<0 || sIndex>=cr_server.screenCount)
2720 return VERR_INVALID_PARAMETER;
2721
2722 if (MAPPED(SCREEN(sIndex)) && SCREEN(sIndex).winID!=winID)
2723 {
2724 crDebug("Mapped screen[%i] is being remapped.", sIndex);
2725 crVBoxServerUnmapScreen(sIndex);
2726 }
2727
2728 SCREEN(sIndex).winID = winID;
2729 SCREEN(sIndex).x = x;
2730 SCREEN(sIndex).y = y;
2731 SCREEN(sIndex).w = w;
2732 SCREEN(sIndex).h = h;
2733
2734 renderspuSetWindowId(SCREEN(sIndex).winID);
2735 crHashtableWalk(cr_server.muralTable, crVBoxServerReparentMuralCB, &sIndex);
2736
2737 crHashtableWalk(cr_server.dummyMuralTable, crVBoxServerReparentMuralCB, &sIndex);
2738 renderspuSetWindowId(SCREEN(0).winID);
2739
2740#ifndef WINDOWS
2741 /*Restore FB content for clients, which have current window on a screen being remapped*/
2742 {
2743 GLint i;
2744
2745 for (i = 0; i < cr_server.numClients; i++)
2746 {
2747 cr_server.curClient = cr_server.clients[i];
2748 if (cr_server.curClient->currentCtxInfo
2749 && cr_server.curClient->currentCtxInfo->pContext
2750 && (cr_server.curClient->currentCtxInfo->pContext->buffer.pFrontImg)
2751 && cr_server.curClient->currentMural
2752 && cr_server.curClient->currentMural->screenId == sIndex
2753 && cr_server.curClient->currentCtxInfo->pContext->buffer.storedHeight == h
2754 && cr_server.curClient->currentCtxInfo->pContext->buffer.storedWidth == w)
2755 {
2756 int clientWindow = cr_server.curClient->currentWindow;
2757 int clientContext = cr_server.curClient->currentContextNumber;
2758 CRFBData *pLazyData = (CRFBData *)cr_server.curClient->currentCtxInfo->pContext->buffer.pFrontImg;
2759
2760 if (clientWindow && clientWindow != cr_server.currentWindow)
2761 {
2762 crServerDispatchMakeCurrent(clientWindow, 0, clientContext);
2763 }
2764
2765 crStateApplyFBImage(cr_server.curClient->currentCtxInfo->pContext, pLazyData);
2766 crStateFreeFBImageLegacy(cr_server.curClient->currentCtxInfo->pContext);
2767 }
2768 }
2769 cr_server.curClient = NULL;
2770 }
2771#endif
2772
2773 CrPMgrScreenChanged((uint32_t)sIndex);
2774
2775 return VINF_SUCCESS;
2776}
2777
2778DECLEXPORT(int32_t) crVBoxServerSetRootVisibleRegion(GLint cRects, const RTRECT *pRects)
2779{
2780 int32_t rc = VINF_SUCCESS;
2781 GLboolean fOldRootVrOn = cr_server.fRootVrOn;
2782
2783 /* non-zero rects pointer indicate rects are present and switched on
2784 * i.e. cRects==0 and pRects!=NULL means root visible regioning is ON and there are no visible regions,
2785 * while pRects==NULL means root visible regioning is OFF, i.e. everything is visible */
2786 if (pRects)
2787 {
2788 crMemset(&cr_server.RootVrCurPoint, 0, sizeof (cr_server.RootVrCurPoint));
2789 rc = VBoxVrListRectsSet(&cr_server.RootVr, cRects, pRects, NULL);
2790 if (!RT_SUCCESS(rc))
2791 {
2792 crWarning("VBoxVrListRectsSet failed! rc %d", rc);
2793 return rc;
2794 }
2795
2796 cr_server.fRootVrOn = GL_TRUE;
2797 }
2798 else
2799 {
2800 if (!cr_server.fRootVrOn)
2801 return VINF_SUCCESS;
2802
2803 VBoxVrListClear(&cr_server.RootVr);
2804
2805 cr_server.fRootVrOn = GL_FALSE;
2806 }
2807
2808 if (!fOldRootVrOn != !cr_server.fRootVrOn)
2809 {
2810 rc = CrPMgrModeRootVr(cr_server.fRootVrOn);
2811 if (!RT_SUCCESS(rc))
2812 {
2813 crWarning("CrPMgrModeRootVr failed rc %d", rc);
2814 return rc;
2815 }
2816 }
2817 else if (cr_server.fRootVrOn)
2818 {
2819 rc = CrPMgrRootVrUpdate();
2820 if (!RT_SUCCESS(rc))
2821 {
2822 crWarning("CrPMgrRootVrUpdate failed rc %d", rc);
2823 return rc;
2824 }
2825 }
2826
2827 return VINF_SUCCESS;
2828}
2829
2830DECLEXPORT(void) crVBoxServerSetPresentFBOCB(PFNCRSERVERPRESENTFBO pfnPresentFBO)
2831{
2832 cr_server.pfnPresentFBO = pfnPresentFBO;
2833}
2834
2835DECLEXPORT(int32_t) crVBoxServerSetOffscreenRendering(GLboolean value)
2836{
2837 return CrPMgrModeVrdp(value);
2838}
2839
2840DECLEXPORT(int32_t) crVBoxServerOutputRedirectSet(const CROutputRedirect *pCallbacks)
2841{
2842 /* No need for a synchronization as this is single threaded. */
2843 if (pCallbacks)
2844 {
2845 cr_server.outputRedirect = *pCallbacks;
2846 }
2847 else
2848 {
2849 memset (&cr_server.outputRedirect, 0, sizeof (cr_server.outputRedirect));
2850 }
2851
2852 return VINF_SUCCESS;
2853}
2854
2855DECLEXPORT(int32_t) crVBoxServerSetScreenViewport(int sIndex, int32_t x, int32_t y, uint32_t w, uint32_t h)
2856{
2857 CRScreenViewportInfo *pViewport;
2858 RTRECT NewRect;
2859 int rc;
2860
2861 crDebug("crVBoxServerSetScreenViewport(%i)", sIndex);
2862
2863 if (sIndex<0 || sIndex>=cr_server.screenCount)
2864 {
2865 crWarning("crVBoxServerSetScreenViewport: invalid screen id %d", sIndex);
2866 return VERR_INVALID_PARAMETER;
2867 }
2868
2869 NewRect.xLeft = x;
2870 NewRect.yTop = y;
2871 NewRect.xRight = x + w;
2872 NewRect.yBottom = y + h;
2873
2874 pViewport = &cr_server.screenVieport[sIndex];
2875 /*always do viewport updates no matter whether the rectangle actually changes,
2876 * this is needed to ensure window is adjusted properly on OSX */
2877 pViewport->Rect = NewRect;
2878 rc = CrPMgrViewportUpdate((uint32_t)sIndex);
2879 if (!RT_SUCCESS(rc))
2880 {
2881 crWarning("CrPMgrViewportUpdate failed %d", rc);
2882 return rc;
2883 }
2884
2885 return VINF_SUCCESS;
2886}
2887
2888
2889#ifdef VBOX_WITH_CRHGSMI
2890
2891static int32_t crVBoxServerCmdVbvaCrCmdProcess(struct VBOXCMDVBVA_CRCMD_CMD *pCmd)
2892{
2893 int32_t rc;
2894 uint32_t cBuffers = pCmd->cBuffers;
2895 uint32_t cParams;
2896 uint32_t cbHdr;
2897 CRVBOXHGSMIHDR *pHdr;
2898 uint32_t u32Function;
2899 uint32_t u32ClientID;
2900 CRClient *pClient;
2901
2902 if (!g_pvVRamBase)
2903 {
2904 crWarning("g_pvVRamBase is not initialized");
2905 return VERR_INVALID_STATE;
2906 }
2907
2908 if (!cBuffers)
2909 {
2910 crWarning("zero buffers passed in!");
2911 return VERR_INVALID_PARAMETER;
2912 }
2913
2914 cParams = cBuffers-1;
2915
2916 cbHdr = pCmd->aBuffers[0].cbBuffer;
2917 pHdr = VBOXCRHGSMI_PTR_SAFE(pCmd->aBuffers[0].offBuffer, cbHdr, CRVBOXHGSMIHDR);
2918 if (!pHdr)
2919 {
2920 crWarning("invalid header buffer!");
2921 return VERR_INVALID_PARAMETER;
2922 }
2923
2924 if (cbHdr < sizeof (*pHdr))
2925 {
2926 crWarning("invalid header buffer size!");
2927 return VERR_INVALID_PARAMETER;
2928 }
2929
2930 u32Function = pHdr->u32Function;
2931 u32ClientID = pHdr->u32ClientID;
2932
2933 switch (u32Function)
2934 {
2935 case SHCRGL_GUEST_FN_WRITE:
2936 {
2937 Log(("svcCall: SHCRGL_GUEST_FN_WRITE\n"));
2938
2939 /* @todo: Verify */
2940 if (cParams == 1)
2941 {
2942 CRVBOXHGSMIWRITE* pFnCmd = (CRVBOXHGSMIWRITE*)pHdr;
2943 VBOXCMDVBVA_CRCMD_BUFFER *pBuf = &pCmd->aBuffers[1];
2944 /* Fetch parameters. */
2945 uint32_t cbBuffer = pBuf->cbBuffer;
2946 uint8_t *pBuffer = VBOXCRHGSMI_PTR_SAFE(pBuf->offBuffer, cbBuffer, uint8_t);
2947
2948 if (cbHdr < sizeof (*pFnCmd))
2949 {
2950 crWarning("invalid write cmd buffer size!");
2951 rc = VERR_INVALID_PARAMETER;
2952 break;
2953 }
2954
2955 CRASSERT(cbBuffer);
2956 if (!pBuffer)
2957 {
2958 crWarning("invalid buffer data received from guest!");
2959 rc = VERR_INVALID_PARAMETER;
2960 break;
2961 }
2962
2963 rc = crVBoxServerClientGet(u32ClientID, &pClient);
2964 if (RT_FAILURE(rc))
2965 {
2966 break;
2967 }
2968
2969 /* This should never fire unless we start to multithread */
2970 CRASSERT(pClient->conn->pBuffer==NULL && pClient->conn->cbBuffer==0);
2971 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
2972
2973 pClient->conn->pBuffer = pBuffer;
2974 pClient->conn->cbBuffer = cbBuffer;
2975 CRVBOXHGSMI_CMDDATA_SET(&pClient->conn->CmdData, pCmd, pHdr, false);
2976 rc = crVBoxServerInternalClientWriteRead(pClient);
2977 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
2978 return rc;
2979 }
2980 else
2981 {
2982 crWarning("invalid number of args");
2983 rc = VERR_INVALID_PARAMETER;
2984 break;
2985 }
2986 break;
2987 }
2988
2989 case SHCRGL_GUEST_FN_INJECT:
2990 {
2991 Log(("svcCall: SHCRGL_GUEST_FN_INJECT\n"));
2992
2993 /* @todo: Verify */
2994 if (cParams == 1)
2995 {
2996 CRVBOXHGSMIINJECT *pFnCmd = (CRVBOXHGSMIINJECT*)pHdr;
2997 /* Fetch parameters. */
2998 uint32_t u32InjectClientID = pFnCmd->u32ClientID;
2999 VBOXCMDVBVA_CRCMD_BUFFER *pBuf = &pCmd->aBuffers[1];
3000 uint32_t cbBuffer = pBuf->cbBuffer;
3001 uint8_t *pBuffer = VBOXCRHGSMI_PTR_SAFE(pBuf->offBuffer, cbBuffer, uint8_t);
3002
3003 if (cbHdr < sizeof (*pFnCmd))
3004 {
3005 crWarning("invalid inject cmd buffer size!");
3006 rc = VERR_INVALID_PARAMETER;
3007 break;
3008 }
3009
3010 CRASSERT(cbBuffer);
3011 if (!pBuffer)
3012 {
3013 crWarning("invalid buffer data received from guest!");
3014 rc = VERR_INVALID_PARAMETER;
3015 break;
3016 }
3017
3018 rc = crVBoxServerClientGet(u32InjectClientID, &pClient);
3019 if (RT_FAILURE(rc))
3020 {
3021 break;
3022 }
3023
3024 /* This should never fire unless we start to multithread */
3025 CRASSERT(pClient->conn->pBuffer==NULL && pClient->conn->cbBuffer==0);
3026 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
3027
3028 pClient->conn->pBuffer = pBuffer;
3029 pClient->conn->cbBuffer = cbBuffer;
3030 CRVBOXHGSMI_CMDDATA_SET(&pClient->conn->CmdData, pCmd, pHdr, false);
3031 rc = crVBoxServerInternalClientWriteRead(pClient);
3032 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
3033 return rc;
3034 }
3035
3036 crWarning("invalid number of args");
3037 rc = VERR_INVALID_PARAMETER;
3038 break;
3039 }
3040
3041 case SHCRGL_GUEST_FN_READ:
3042 {
3043 Log(("svcCall: SHCRGL_GUEST_FN_READ\n"));
3044
3045 /* @todo: Verify */
3046 if (cParams == 1)
3047 {
3048 CRVBOXHGSMIREAD *pFnCmd = (CRVBOXHGSMIREAD*)pHdr;
3049 VBOXCMDVBVA_CRCMD_BUFFER *pBuf = &pCmd->aBuffers[1];
3050 /* Fetch parameters. */
3051 uint32_t cbBuffer = pBuf->cbBuffer;
3052 uint8_t *pBuffer = VBOXCRHGSMI_PTR_SAFE(pBuf->offBuffer, cbBuffer, uint8_t);
3053
3054 if (cbHdr < sizeof (*pFnCmd))
3055 {
3056 crWarning("invalid read cmd buffer size!");
3057 rc = VERR_INVALID_PARAMETER;
3058 break;
3059 }
3060
3061
3062 if (!pBuffer)
3063 {
3064 crWarning("invalid buffer data received from guest!");
3065 rc = VERR_INVALID_PARAMETER;
3066 break;
3067 }
3068
3069 rc = crVBoxServerClientGet(u32ClientID, &pClient);
3070 if (RT_FAILURE(rc))
3071 {
3072 break;
3073 }
3074
3075 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
3076
3077 rc = crVBoxServerInternalClientRead(pClient, pBuffer, &cbBuffer);
3078
3079 /* Return the required buffer size always */
3080 pFnCmd->cbBuffer = cbBuffer;
3081
3082 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
3083
3084 /* the read command is never pended, complete it right away */
3085 pHdr->result = rc;
3086
3087 return VINF_SUCCESS;
3088 }
3089
3090 crWarning("invalid number of args");
3091 rc = VERR_INVALID_PARAMETER;
3092 break;
3093 }
3094
3095 case SHCRGL_GUEST_FN_WRITE_READ:
3096 {
3097 Log(("svcCall: SHCRGL_GUEST_FN_WRITE_READ\n"));
3098
3099 /* @todo: Verify */
3100 if (cParams == 2)
3101 {
3102 CRVBOXHGSMIWRITEREAD *pFnCmd = (CRVBOXHGSMIWRITEREAD*)pHdr;
3103 VBOXCMDVBVA_CRCMD_BUFFER *pBuf = &pCmd->aBuffers[1];
3104 VBOXCMDVBVA_CRCMD_BUFFER *pWbBuf = &pCmd->aBuffers[2];
3105
3106 /* Fetch parameters. */
3107 uint32_t cbBuffer = pBuf->cbBuffer;
3108 uint8_t *pBuffer = VBOXCRHGSMI_PTR_SAFE(pBuf->offBuffer, cbBuffer, uint8_t);
3109
3110 uint32_t cbWriteback = pWbBuf->cbBuffer;
3111 char *pWriteback = VBOXCRHGSMI_PTR_SAFE(pWbBuf->offBuffer, cbWriteback, char);
3112
3113 if (cbHdr < sizeof (*pFnCmd))
3114 {
3115 crWarning("invalid write_read cmd buffer size!");
3116 rc = VERR_INVALID_PARAMETER;
3117 break;
3118 }
3119
3120
3121 CRASSERT(cbBuffer);
3122 if (!pBuffer)
3123 {
3124 crWarning("invalid write buffer data received from guest!");
3125 rc = VERR_INVALID_PARAMETER;
3126 break;
3127 }
3128
3129 CRASSERT(cbWriteback);
3130 if (!pWriteback)
3131 {
3132 crWarning("invalid writeback buffer data received from guest!");
3133 rc = VERR_INVALID_PARAMETER;
3134 break;
3135 }
3136 rc = crVBoxServerClientGet(u32ClientID, &pClient);
3137 if (RT_FAILURE(rc))
3138 {
3139 pHdr->result = rc;
3140 return VINF_SUCCESS;
3141 }
3142
3143 /* This should never fire unless we start to multithread */
3144 CRASSERT(pClient->conn->pBuffer==NULL && pClient->conn->cbBuffer==0);
3145 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
3146
3147 pClient->conn->pBuffer = pBuffer;
3148 pClient->conn->cbBuffer = cbBuffer;
3149 CRVBOXHGSMI_CMDDATA_SETWB(&pClient->conn->CmdData, pCmd, pHdr, pWriteback, cbWriteback, &pFnCmd->cbWriteback, false);
3150 rc = crVBoxServerInternalClientWriteRead(pClient);
3151 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
3152 return rc;
3153 }
3154
3155 crWarning("invalid number of args");
3156 rc = VERR_INVALID_PARAMETER;
3157 break;
3158 }
3159
3160 case SHCRGL_GUEST_FN_SET_VERSION:
3161 {
3162 crWarning("invalid function");
3163 rc = VERR_NOT_IMPLEMENTED;
3164 break;
3165 }
3166
3167 case SHCRGL_GUEST_FN_SET_PID:
3168 {
3169 crWarning("invalid function");
3170 rc = VERR_NOT_IMPLEMENTED;
3171 break;
3172 }
3173
3174 default:
3175 {
3176 crWarning("invalid function");
3177 rc = VERR_NOT_IMPLEMENTED;
3178 break;
3179 }
3180
3181 }
3182
3183 /* we can be on fail only here */
3184 CRASSERT(RT_FAILURE(rc));
3185 pHdr->result = rc;
3186
3187 return rc;
3188}
3189
3190static int32_t crVBoxServerCrCmdProcess(PVBOXCMDVBVA_HDR pCmd, uint32_t cbCmd)
3191{
3192 switch (pCmd->u8OpCode)
3193 {
3194 case VBOXCMDVBVA_OPTYPE_CRCMD:
3195 {
3196 VBOXCMDVBVA_CRCMD *pCrCmdDr = (VBOXCMDVBVA_CRCMD*)pCmd;
3197 VBOXCMDVBVA_CRCMD_CMD *pCrCmd = &pCrCmdDr->Cmd;
3198 int rc = crVBoxServerCmdVbvaCrCmdProcess(pCrCmd);
3199 if (RT_SUCCESS(rc))
3200 {
3201 /* success */
3202 pCmd->i8Result = 0;
3203 }
3204 else
3205 {
3206 crWarning("crVBoxServerCmdVbvaCrCmdProcess failed, rc %d", rc);
3207 pCmd->i8Result = -1;
3208 }
3209 break;
3210 }
3211 case VBOXCMDVBVA_OPTYPE_BLT_OFFPRIMSZFMT_OR_ID:
3212 {
3213 crVBoxServerCrCmdBltProcess(pCmd, cbCmd);
3214 break;
3215 }
3216 default:
3217 WARN(("unsupported command"));
3218 pCmd->i8Result = -1;
3219 }
3220 return VINF_SUCCESS;
3221}
3222
3223int32_t crVBoxServerCrCmdNotifyCmds()
3224{
3225 PVBOXCMDVBVA_HDR pCmd = NULL;
3226 uint32_t cbCmd;
3227
3228 for (;;)
3229 {
3230 int rc = cr_server.CltInfo.pfnCmdGet(cr_server.CltInfo.hClient, &pCmd, &cbCmd);
3231 if (rc == VINF_EOF)
3232 return VINF_SUCCESS;
3233 if (!RT_SUCCESS(rc))
3234 return rc;
3235
3236 rc = crVBoxServerCrCmdProcess(pCmd, cbCmd);
3237 if (!RT_SUCCESS(rc))
3238 return rc;
3239 }
3240
3241 /* should not be here! */
3242 AssertFailed();
3243 return VERR_INTERNAL_ERROR;
3244}
3245
3246/* We moved all CrHgsmi command processing to crserverlib to keep the logic of dealing with CrHgsmi commands in one place.
3247 *
3248 * 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.
3249 * This help avoiding the "blocked-client" issues. The client is blocked if another client is doing begin-end stuff.
3250 * For now we eliminated polling that could occur on block, which caused a higher-priority thread (in guest) polling for the blocked command complition
3251 * to block the lower-priority thread trying to complete the blocking command.
3252 * And removed extra memcpy done on blocked command arrival.
3253 *
3254 * In the future we will extend CrHgsmi functionality to maintain texture data directly in CrHgsmi allocation to avoid extra memcpy-ing with PBO,
3255 * implement command completion and stuff necessary for GPU scheduling to work properly for WDDM Windows guests, etc.
3256 *
3257 * NOTE: it is ALWAYS responsibility of the crVBoxServerCrHgsmiCmd to complete the command!
3258 * */
3259
3260
3261int32_t crVBoxServerCrHgsmiCmd(struct VBOXVDMACMD_CHROMIUM_CMD *pCmd, uint32_t cbCmd)
3262{
3263
3264 int32_t rc;
3265 uint32_t cBuffers = pCmd->cBuffers;
3266 uint32_t cParams;
3267 uint32_t cbHdr;
3268 CRVBOXHGSMIHDR *pHdr;
3269 uint32_t u32Function;
3270 uint32_t u32ClientID;
3271 CRClient *pClient;
3272
3273 if (!g_pvVRamBase)
3274 {
3275 crWarning("g_pvVRamBase is not initialized");
3276
3277 crServerCrHgsmiCmdComplete(pCmd, VERR_INVALID_STATE);
3278 return VINF_SUCCESS;
3279 }
3280
3281 if (!cBuffers)
3282 {
3283 crWarning("zero buffers passed in!");
3284
3285 crServerCrHgsmiCmdComplete(pCmd, VERR_INVALID_PARAMETER);
3286 return VINF_SUCCESS;
3287 }
3288
3289 cParams = cBuffers-1;
3290
3291 cbHdr = pCmd->aBuffers[0].cbBuffer;
3292 pHdr = VBOXCRHGSMI_PTR_SAFE(pCmd->aBuffers[0].offBuffer, cbHdr, CRVBOXHGSMIHDR);
3293 if (!pHdr)
3294 {
3295 crWarning("invalid header buffer!");
3296
3297 crServerCrHgsmiCmdComplete(pCmd, VERR_INVALID_PARAMETER);
3298 return VINF_SUCCESS;
3299 }
3300
3301 if (cbHdr < sizeof (*pHdr))
3302 {
3303 crWarning("invalid header buffer size!");
3304
3305 crServerCrHgsmiCmdComplete(pCmd, VERR_INVALID_PARAMETER);
3306 return VINF_SUCCESS;
3307 }
3308
3309 u32Function = pHdr->u32Function;
3310 u32ClientID = pHdr->u32ClientID;
3311
3312 switch (u32Function)
3313 {
3314 case SHCRGL_GUEST_FN_WRITE:
3315 {
3316 Log(("svcCall: SHCRGL_GUEST_FN_WRITE\n"));
3317
3318 /* @todo: Verify */
3319 if (cParams == 1)
3320 {
3321 CRVBOXHGSMIWRITE* pFnCmd = (CRVBOXHGSMIWRITE*)pHdr;
3322 VBOXVDMACMD_CHROMIUM_BUFFER *pBuf = &pCmd->aBuffers[1];
3323 /* Fetch parameters. */
3324 uint32_t cbBuffer = pBuf->cbBuffer;
3325 uint8_t *pBuffer = VBOXCRHGSMI_PTR_SAFE(pBuf->offBuffer, cbBuffer, uint8_t);
3326
3327 if (cbHdr < sizeof (*pFnCmd))
3328 {
3329 crWarning("invalid write cmd buffer size!");
3330 rc = VERR_INVALID_PARAMETER;
3331 break;
3332 }
3333
3334 CRASSERT(cbBuffer);
3335 if (!pBuffer)
3336 {
3337 crWarning("invalid buffer data received from guest!");
3338 rc = VERR_INVALID_PARAMETER;
3339 break;
3340 }
3341
3342 rc = crVBoxServerClientGet(u32ClientID, &pClient);
3343 if (RT_FAILURE(rc))
3344 {
3345 break;
3346 }
3347
3348 /* This should never fire unless we start to multithread */
3349 CRASSERT(pClient->conn->pBuffer==NULL && pClient->conn->cbBuffer==0);
3350 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
3351
3352 pClient->conn->pBuffer = pBuffer;
3353 pClient->conn->cbBuffer = cbBuffer;
3354 CRVBOXHGSMI_CMDDATA_SET(&pClient->conn->CmdData, pCmd, pHdr, true);
3355 rc = crVBoxServerInternalClientWriteRead(pClient);
3356 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
3357 return rc;
3358 }
3359 else
3360 {
3361 crWarning("invalid number of args");
3362 rc = VERR_INVALID_PARAMETER;
3363 break;
3364 }
3365 break;
3366 }
3367
3368 case SHCRGL_GUEST_FN_INJECT:
3369 {
3370 Log(("svcCall: SHCRGL_GUEST_FN_INJECT\n"));
3371
3372 /* @todo: Verify */
3373 if (cParams == 1)
3374 {
3375 CRVBOXHGSMIINJECT *pFnCmd = (CRVBOXHGSMIINJECT*)pHdr;
3376 /* Fetch parameters. */
3377 uint32_t u32InjectClientID = pFnCmd->u32ClientID;
3378 VBOXVDMACMD_CHROMIUM_BUFFER *pBuf = &pCmd->aBuffers[1];
3379 uint32_t cbBuffer = pBuf->cbBuffer;
3380 uint8_t *pBuffer = VBOXCRHGSMI_PTR_SAFE(pBuf->offBuffer, cbBuffer, uint8_t);
3381
3382 if (cbHdr < sizeof (*pFnCmd))
3383 {
3384 crWarning("invalid inject cmd buffer size!");
3385 rc = VERR_INVALID_PARAMETER;
3386 break;
3387 }
3388
3389 CRASSERT(cbBuffer);
3390 if (!pBuffer)
3391 {
3392 crWarning("invalid buffer data received from guest!");
3393 rc = VERR_INVALID_PARAMETER;
3394 break;
3395 }
3396
3397 rc = crVBoxServerClientGet(u32InjectClientID, &pClient);
3398 if (RT_FAILURE(rc))
3399 {
3400 break;
3401 }
3402
3403 /* This should never fire unless we start to multithread */
3404 CRASSERT(pClient->conn->pBuffer==NULL && pClient->conn->cbBuffer==0);
3405 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
3406
3407 pClient->conn->pBuffer = pBuffer;
3408 pClient->conn->cbBuffer = cbBuffer;
3409 CRVBOXHGSMI_CMDDATA_SET(&pClient->conn->CmdData, pCmd, pHdr, true);
3410 rc = crVBoxServerInternalClientWriteRead(pClient);
3411 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
3412 return rc;
3413 }
3414
3415 crWarning("invalid number of args");
3416 rc = VERR_INVALID_PARAMETER;
3417 break;
3418 }
3419
3420 case SHCRGL_GUEST_FN_READ:
3421 {
3422 Log(("svcCall: SHCRGL_GUEST_FN_READ\n"));
3423
3424 /* @todo: Verify */
3425 if (cParams == 1)
3426 {
3427 CRVBOXHGSMIREAD *pFnCmd = (CRVBOXHGSMIREAD*)pHdr;
3428 VBOXVDMACMD_CHROMIUM_BUFFER *pBuf = &pCmd->aBuffers[1];
3429 /* Fetch parameters. */
3430 uint32_t cbBuffer = pBuf->cbBuffer;
3431 uint8_t *pBuffer = VBOXCRHGSMI_PTR_SAFE(pBuf->offBuffer, cbBuffer, uint8_t);
3432
3433 if (cbHdr < sizeof (*pFnCmd))
3434 {
3435 crWarning("invalid read cmd buffer size!");
3436 rc = VERR_INVALID_PARAMETER;
3437 break;
3438 }
3439
3440
3441 if (!pBuffer)
3442 {
3443 crWarning("invalid buffer data received from guest!");
3444 rc = VERR_INVALID_PARAMETER;
3445 break;
3446 }
3447
3448 rc = crVBoxServerClientGet(u32ClientID, &pClient);
3449 if (RT_FAILURE(rc))
3450 {
3451 break;
3452 }
3453
3454 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
3455
3456 rc = crVBoxServerInternalClientRead(pClient, pBuffer, &cbBuffer);
3457
3458 /* Return the required buffer size always */
3459 pFnCmd->cbBuffer = cbBuffer;
3460
3461 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
3462
3463 /* the read command is never pended, complete it right away */
3464 pHdr->result = rc;
3465
3466 crServerCrHgsmiCmdComplete(pCmd, VINF_SUCCESS);
3467 return VINF_SUCCESS;
3468 }
3469
3470 crWarning("invalid number of args");
3471 rc = VERR_INVALID_PARAMETER;
3472 break;
3473 }
3474
3475 case SHCRGL_GUEST_FN_WRITE_READ:
3476 {
3477 Log(("svcCall: SHCRGL_GUEST_FN_WRITE_READ\n"));
3478
3479 /* @todo: Verify */
3480 if (cParams == 2)
3481 {
3482 CRVBOXHGSMIWRITEREAD *pFnCmd = (CRVBOXHGSMIWRITEREAD*)pHdr;
3483 VBOXVDMACMD_CHROMIUM_BUFFER *pBuf = &pCmd->aBuffers[1];
3484 VBOXVDMACMD_CHROMIUM_BUFFER *pWbBuf = &pCmd->aBuffers[2];
3485
3486 /* Fetch parameters. */
3487 uint32_t cbBuffer = pBuf->cbBuffer;
3488 uint8_t *pBuffer = VBOXCRHGSMI_PTR_SAFE(pBuf->offBuffer, cbBuffer, uint8_t);
3489
3490 uint32_t cbWriteback = pWbBuf->cbBuffer;
3491 char *pWriteback = VBOXCRHGSMI_PTR_SAFE(pWbBuf->offBuffer, cbWriteback, char);
3492
3493 if (cbHdr < sizeof (*pFnCmd))
3494 {
3495 crWarning("invalid write_read cmd buffer size!");
3496 rc = VERR_INVALID_PARAMETER;
3497 break;
3498 }
3499
3500
3501 CRASSERT(cbBuffer);
3502 if (!pBuffer)
3503 {
3504 crWarning("invalid write buffer data received from guest!");
3505 rc = VERR_INVALID_PARAMETER;
3506 break;
3507 }
3508
3509 CRASSERT(cbWriteback);
3510 if (!pWriteback)
3511 {
3512 crWarning("invalid writeback buffer data received from guest!");
3513 rc = VERR_INVALID_PARAMETER;
3514 break;
3515 }
3516 rc = crVBoxServerClientGet(u32ClientID, &pClient);
3517 if (RT_FAILURE(rc))
3518 {
3519 pHdr->result = rc;
3520 crServerCrHgsmiCmdComplete(pCmd, VINF_SUCCESS);
3521 return rc;
3522 }
3523
3524 /* This should never fire unless we start to multithread */
3525 CRASSERT(pClient->conn->pBuffer==NULL && pClient->conn->cbBuffer==0);
3526 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
3527
3528 pClient->conn->pBuffer = pBuffer;
3529 pClient->conn->cbBuffer = cbBuffer;
3530 CRVBOXHGSMI_CMDDATA_SETWB(&pClient->conn->CmdData, pCmd, pHdr, pWriteback, cbWriteback, &pFnCmd->cbWriteback, true);
3531 rc = crVBoxServerInternalClientWriteRead(pClient);
3532 CRVBOXHGSMI_CMDDATA_ASSERT_CLEANED(&pClient->conn->CmdData);
3533 return rc;
3534 }
3535
3536 crWarning("invalid number of args");
3537 rc = VERR_INVALID_PARAMETER;
3538 break;
3539 }
3540
3541 case SHCRGL_GUEST_FN_SET_VERSION:
3542 {
3543 crWarning("invalid function");
3544 rc = VERR_NOT_IMPLEMENTED;
3545 break;
3546 }
3547
3548 case SHCRGL_GUEST_FN_SET_PID:
3549 {
3550 crWarning("invalid function");
3551 rc = VERR_NOT_IMPLEMENTED;
3552 break;
3553 }
3554
3555 default:
3556 {
3557 crWarning("invalid function");
3558 rc = VERR_NOT_IMPLEMENTED;
3559 break;
3560 }
3561
3562 }
3563
3564 /* we can be on fail only here */
3565 CRASSERT(RT_FAILURE(rc));
3566 pHdr->result = rc;
3567
3568 crServerCrHgsmiCmdComplete(pCmd, VINF_SUCCESS);
3569 return rc;
3570
3571}
3572
3573static DECLCALLBACK(bool) crVBoxServerHasData()
3574{
3575 HCR_FRAMEBUFFER hFb = CrPMgrFbGetFirstEnabled();
3576 for (;
3577 hFb;
3578 hFb = CrPMgrFbGetNextEnabled(hFb))
3579 {
3580 if (CrFbHas3DData(hFb))
3581 return true;
3582 }
3583
3584 return false;
3585}
3586
3587int32_t crVBoxServerCrHgsmiCtl(struct VBOXVDMACMD_CHROMIUM_CTL *pCtl, uint32_t cbCtl)
3588{
3589 int rc = VINF_SUCCESS;
3590
3591 switch (pCtl->enmType)
3592 {
3593 case VBOXVDMACMD_CHROMIUM_CTL_TYPE_CRHGSMI_SETUP:
3594 {
3595 PVBOXVDMACMD_CHROMIUM_CTL_CRHGSMI_SETUP pSetup = (PVBOXVDMACMD_CHROMIUM_CTL_CRHGSMI_SETUP)pCtl;
3596 g_pvVRamBase = (uint8_t*)pSetup->pvVRamBase;
3597 g_cbVRam = pSetup->cbVRam;
3598 cr_server.CltInfo = *pSetup->pCrCmdClientInfo;
3599 rc = VINF_SUCCESS;
3600 break;
3601 }
3602 case VBOXVDMACMD_CHROMIUM_CTL_TYPE_SAVESTATE_BEGIN:
3603 case VBOXVDMACMD_CHROMIUM_CTL_TYPE_SAVESTATE_END:
3604 rc = VINF_SUCCESS;
3605 break;
3606 case VBOXVDMACMD_CHROMIUM_CTL_TYPE_CRHGSMI_SETUP_MAINCB:
3607 {
3608 PVBOXVDMACMD_CHROMIUM_CTL_CRHGSMI_SETUP_MAINCB pSetup = (PVBOXVDMACMD_CHROMIUM_CTL_CRHGSMI_SETUP_MAINCB)pCtl;
3609 g_hCrHgsmiCompletion = pSetup->hCompletion;
3610 g_pfnCrHgsmiCompletion = pSetup->pfnCompletion;
3611
3612 pSetup->MainInterface.pfnHasData = crVBoxServerHasData;
3613
3614 rc = VINF_SUCCESS;
3615 break;
3616 }
3617 default:
3618 AssertMsgFailed(("invalid param %d", pCtl->enmType));
3619 rc = VERR_INVALID_PARAMETER;
3620 }
3621
3622 /* NOTE: Control commands can NEVER be pended here, this is why its a task of a caller (Main)
3623 * to complete them accordingly.
3624 * This approach allows using host->host and host->guest commands in the same way here
3625 * making the command completion to be the responsibility of the command originator.
3626 * E.g. ctl commands can be both Hgcm Host synchronous commands that do not require completion at all,
3627 * or Hgcm Host Fast Call commands that do require completion. All this details are hidden here */
3628 return rc;
3629}
3630
3631#endif
Note: See TracBrowser for help on using the repository browser.

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