VirtualBox

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

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

crOpenGL: don't notify event when there is no parent window

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

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