VirtualBox

source: vbox/trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_presenter.cpp@ 48521

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

crOpenGL: more output redirect enhance

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 26.6 KB
Line 
1/* $Id: server_presenter.cpp 47628 2013-08-09 09:08:20Z vboxsync $ */
2
3/** @file
4 * Presenter API
5 */
6
7/*
8 * Copyright (C) 2012-2013 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18#include "cr_spu.h"
19#include "chromium.h"
20#include "cr_error.h"
21#include "cr_net.h"
22#include "cr_rand.h"
23#include "server_dispatch.h"
24#include "server.h"
25#include "cr_mem.h"
26#include "cr_string.h"
27#include <cr_vreg.h>
28
29#include <iprt/cdefs.h>
30#include <iprt/types.h>
31#include <iprt/asm.h>
32#include <iprt/mem.h>
33#include <iprt/list.h>
34#include <iprt/memcache.h>
35
36#include "render/renderspu.h"
37
38/* DISPLAY */
39
40int CrDpInit(PCR_DISPLAY pDisplay)
41{
42 const GLint visBits = cr_server.MainContextInfo.CreateInfo.visualBits;
43 if (crServerMuralInit(&pDisplay->Mural, "", visBits, -1, GL_FALSE) < 0)
44 {
45 crWarning("crServerMuralInit failed!");
46 return VERR_GENERAL_FAILURE;
47 }
48
49 crServerMuralVisibleRegion(&pDisplay->Mural, 0, NULL);
50 crServerMuralShow(&pDisplay->Mural, GL_TRUE);
51
52 pDisplay->fForcePresent = GL_FALSE;
53 return VINF_SUCCESS;
54}
55
56void CrDpTerm(PCR_DISPLAY pDisplay)
57{
58 crServerMuralTerm(&pDisplay->Mural);
59}
60
61void CrDpResize(PCR_DISPLAY pDisplay, int32_t xPos, int32_t yPos, uint32_t width, uint32_t height)
62{
63 if (xPos != pDisplay->Mural.gX
64 || yPos != pDisplay->Mural.gY
65 || width != pDisplay->Mural.width
66 || height != pDisplay->Mural.height)
67 {
68 crServerMuralPosition(&pDisplay->Mural, xPos, yPos, GL_TRUE);
69 if (!crServerMuralSize(&pDisplay->Mural, width, height))
70 crServerCheckMuralGeometry(&pDisplay->Mural);
71 }
72 else
73 crServerCheckMuralGeometry(&pDisplay->Mural);
74}
75
76void CrDpReparent(PCR_DISPLAY pDisplay, CRScreenInfo *pScreen)
77{
78 renderspuSetWindowId(pScreen->winID);
79 crServerWindowReparent(&pDisplay->Mural);
80 renderspuSetWindowId(cr_server.screen[0].winID);
81
82 CrDpResize(pDisplay, pScreen->x, pScreen->y, pScreen->w, pScreen->h);
83}
84
85
86int CrDpSaveState(PCR_DISPLAY pDisplay, PSSMHANDLE pSSM)
87{
88 VBOXVR_SCR_COMPOSITOR_ITERATOR Iter;
89 CrVrScrCompositorIterInit(&pDisplay->Mural.Compositor, &Iter);
90 PVBOXVR_SCR_COMPOSITOR_ENTRY pEntry;
91 uint32_t u32 = 0;
92 while ((pEntry = CrVrScrCompositorIterNext(&Iter)) != NULL)
93 {
94 ++u32;
95 }
96
97 int rc = SSMR3PutU32(pSSM, u32);
98 AssertRCReturn(rc, rc);
99
100 CrVrScrCompositorIterInit(&pDisplay->Mural.Compositor, &Iter);
101
102 while ((pEntry = CrVrScrCompositorIterNext(&Iter)) != NULL)
103 {
104 CR_DISPLAY_ENTRY *pDEntry = CR_DENTRY_FROM_CENTRY(pEntry);
105 rc = CrDemEntrySaveState(pDEntry, pSSM);
106 AssertRCReturn(rc, rc);
107
108 u32 = CrVrScrCompositorEntryFlagsGet(&pDEntry->CEntry);
109 rc = SSMR3PutU32(pSSM, u32);
110 AssertRCReturn(rc, rc);
111
112 rc = SSMR3PutS32(pSSM, CrVrScrCompositorEntryPosGet(&pDEntry->CEntry)->x);
113 AssertRCReturn(rc, rc);
114
115 rc = SSMR3PutS32(pSSM, CrVrScrCompositorEntryPosGet(&pDEntry->CEntry)->y);
116 AssertRCReturn(rc, rc);
117
118 const RTRECT * pRects;
119 rc = CrVrScrCompositorEntryRegionsGet(&pDisplay->Mural.Compositor, &pDEntry->CEntry, &u32, NULL, NULL, &pRects);
120 AssertRCReturn(rc, rc);
121
122 rc = SSMR3PutU32(pSSM, u32);
123 AssertRCReturn(rc, rc);
124
125 if (u32)
126 {
127 rc = SSMR3PutMem(pSSM, pRects, u32 * sizeof (*pRects));
128 AssertRCReturn(rc, rc);
129 }
130 }
131
132 return VINF_SUCCESS;
133}
134
135int CrDpLoadState(PCR_DISPLAY pDisplay, PSSMHANDLE pSSM, uint32_t version)
136{
137 uint32_t u32 = 0;
138 int rc = SSMR3GetU32(pSSM, &u32);
139 AssertRCReturn(rc, rc);
140
141 if (!u32)
142 return VINF_SUCCESS;
143
144 CrDpEnter(pDisplay);
145
146 for (uint32_t i = 0; i < u32; ++i)
147 {
148 CR_DISPLAY_ENTRY *pDEntry;
149 rc = CrDemEntryLoadState(&cr_server.PresentTexturepMap, &pDEntry, pSSM);
150 AssertRCReturn(rc, rc);
151
152 uint32_t fFlags;
153 rc = SSMR3GetU32(pSSM, &fFlags);
154 AssertRCReturn(rc, rc);
155
156 CrVrScrCompositorEntryFlagsSet(&pDEntry->CEntry, fFlags);
157
158 RTPOINT Pos;
159 rc = SSMR3GetS32(pSSM, &Pos.x);
160 AssertRCReturn(rc, rc);
161
162 rc = SSMR3GetS32(pSSM, &Pos.y);
163 AssertRCReturn(rc, rc);
164
165 uint32_t cRects;
166 rc = SSMR3GetU32(pSSM, &cRects);
167 AssertRCReturn(rc, rc);
168
169 RTRECT * pRects = NULL;
170 if (cRects)
171 {
172 pRects = (RTRECT *)crAlloc(cRects * sizeof (*pRects));
173 AssertReturn(pRects, VERR_NO_MEMORY);
174
175 rc = SSMR3GetMem(pSSM, pRects, cRects * sizeof (*pRects));
176 AssertRCReturn(rc, rc);
177 }
178
179 rc = CrDpEntryRegionsAdd(pDisplay, pDEntry, &Pos, (uint32_t)cRects, (const RTRECT*)pRects, NULL);
180 AssertRCReturn(rc, rc);
181
182 if (pRects)
183 crFree(pRects);
184 }
185
186 CrDpLeave(pDisplay);
187
188 return VINF_SUCCESS;
189}
190
191
192int CrDpEntryRegionsSet(PCR_DISPLAY pDisplay, PCR_DISPLAY_ENTRY pEntry, const RTPOINT *pPos, uint32_t cRegions, const RTRECT *paRegions)
193{
194 int rc = CrVrScrCompositorEntryRegionsSet(&pDisplay->Mural.Compositor, pEntry ? &pEntry->CEntry : NULL, pPos, cRegions, paRegions, false, NULL);
195 return rc;
196}
197
198void crDbgDumpRect(uint32_t i, const RTRECT *pRect)
199{
200 crDebug("%d: (%d;%d) X (%d;%d)", i, pRect->xLeft, pRect->yTop, pRect->xRight, pRect->yBottom);
201}
202
203void crDbgDumpRects(uint32_t cRects, const RTRECT *paRects)
204{
205 crDebug("Dumping rects (%d)", cRects);
206 for (uint32_t i = 0; i < cRects; ++i)
207 {
208 crDbgDumpRect(i, &paRects[i]);
209 }
210 crDebug("End Dumping rects (%d)", cRects);
211}
212
213int CrDpEntryRegionsAdd(PCR_DISPLAY pDisplay, PCR_DISPLAY_ENTRY pEntry, const RTPOINT *pPos, uint32_t cRegions, const RTRECT *paRegions, CR_DISPLAY_ENTRY_MAP *pMap)
214{
215 uint32_t fChangeFlags = 0;
216 VBOXVR_SCR_COMPOSITOR_ENTRY *pReplacedScrEntry = NULL;
217
218 if (pMap)
219 CrDemEnter(pMap);
220
221 int rc = CrVrScrCompositorEntryRegionsAdd(&pDisplay->Mural.Compositor, pEntry ? &pEntry->CEntry : NULL, pPos, cRegions, paRegions, false, &pReplacedScrEntry, &fChangeFlags);
222 if (RT_SUCCESS(rc))
223 {
224 if (fChangeFlags & VBOXVR_COMPOSITOR_CF_REGIONS_CHANGED)
225 {
226 uint32_t cRects;
227 const RTRECT *pRects;
228 rc = CrVrScrCompositorRegionsGet(&pDisplay->Mural.Compositor, &cRects, NULL, &pRects, NULL);
229 if (RT_SUCCESS(rc))
230 crServerMuralVisibleRegion(&pDisplay->Mural, cRects, (GLint *)pRects);
231 else
232 crWarning("CrVrScrCompositorRegionsGet failed, rc %d", rc);
233
234 Assert(!pReplacedScrEntry);
235 }
236 else if (fChangeFlags & VBOXVR_COMPOSITOR_CF_ENTRY_REPLACED)
237 {
238 Assert(pReplacedScrEntry);
239 }
240 else
241 {
242 Assert(!pReplacedScrEntry);
243 }
244 }
245 else
246 crWarning("CrVrScrCompositorEntryRegionsAdd failed, rc %d", rc);
247
248 if (pMap)
249 CrDemLeave(pMap, CR_DENTRY_FROM_CENTRY(pEntry), CR_DENTRY_FROM_CENTRY(pReplacedScrEntry));
250
251 return rc;
252}
253
254void CrDpRegionsClear(PCR_DISPLAY pDisplay)
255{
256 bool fChanged = false;
257 CrVrScrCompositorRegionsClear(&pDisplay->Mural.Compositor, &fChanged);
258 if (fChanged)
259 {
260 crServerMuralVisibleRegion(&pDisplay->Mural, 0, NULL);
261 }
262}
263
264#define PCR_DISPLAY_ENTRY_FROM_CENTRY(_pe) ((PCR_DISPLAY_ENTRY)((uint8_t*)(_pe) - RT_OFFSETOF(CR_DISPLAY_ENTRY, CEntry)))
265static DECLCALLBACK(void) crDpEntryCEntryReleaseCB(const struct VBOXVR_SCR_COMPOSITOR *pCompositor, struct VBOXVR_SCR_COMPOSITOR_ENTRY *pEntry, struct VBOXVR_SCR_COMPOSITOR_ENTRY *pReplacingEntry)
266{
267 PCR_DISPLAY_ENTRY pCEntry = PCR_DISPLAY_ENTRY_FROM_CENTRY(pEntry);
268 CrDemEntryRelease(pCEntry);
269}
270
271void CrDpEntryInit(PCR_DISPLAY_ENTRY pEntry, const VBOXVR_TEXTURE *pTextureData, uint32_t fFlags, PFNVBOXVRSCRCOMPOSITOR_ENTRY_RELEASED pfnEntryReleased)
272{
273 CrVrScrCompositorEntryInit(&pEntry->CEntry, pTextureData, pfnEntryReleased);
274 CrVrScrCompositorEntryFlagsSet(&pEntry->CEntry, fFlags);
275 CrVrScrCompositorEntryInit(&pEntry->RootVrCEntry, pTextureData, NULL);
276 CrVrScrCompositorEntryFlagsSet(&pEntry->RootVrCEntry, fFlags);
277 pEntry->pvORInstance = NULL;
278 pEntry->idPBO = 0;
279 pEntry->idInvertTex = 0;
280}
281
282void CrDpEntryCleanup(PCR_DISPLAY_ENTRY pDEntry)
283{
284 if (pDEntry->idPBO)
285 {
286 CRASSERT(cr_server.bUsePBOForReadback);
287 cr_server.head_spu->dispatch_table.DeleteBuffersARB(1, &pDEntry->idPBO);
288 pDEntry->idPBO = 0;
289 }
290
291 if (pDEntry->idInvertTex)
292 {
293 cr_server.head_spu->dispatch_table.DeleteTextures(1, &pDEntry->idInvertTex);
294 pDEntry->idInvertTex = 0;
295 }
296
297 if (pDEntry->pvORInstance)
298 {
299 cr_server.outputRedirect.CROREnd(pDEntry->pvORInstance);
300 pDEntry->pvORInstance = NULL;
301 }
302}
303
304void CrDpEnter(PCR_DISPLAY pDisplay)
305{
306 pDisplay->fForcePresent |= crServerVBoxCompositionPresentNeeded(&pDisplay->Mural);
307 crServerVBoxCompositionDisableEnter(&pDisplay->Mural);
308}
309
310void CrDpLeave(PCR_DISPLAY pDisplay)
311{
312 pDisplay->Mural.fDataPresented = GL_TRUE;
313 pDisplay->Mural.fOrPresentOnReenable = GL_TRUE;
314 crServerVBoxCompositionDisableLeave(&pDisplay->Mural, pDisplay->fForcePresent);
315 pDisplay->fForcePresent = GL_FALSE;
316}
317
318typedef struct CR_DEM_ENTRY_INFO
319{
320 CRTextureObj *pTobj;
321 uint32_t cEntries;
322} CR_DEM_ENTRY_INFO;
323
324typedef struct CR_DEM_ENTRY
325{
326 CR_DISPLAY_ENTRY Entry;
327 CR_DEM_ENTRY_INFO *pInfo;
328 CR_DISPLAY_ENTRY_MAP *pMap;
329 RTLISTNODE Node;
330} CR_DEM_ENTRY;
331
332#define PCR_DEM_ENTRY_FROM_ENTRY(_pEntry) ((CR_DEM_ENTRY*)((uint8_t*)(_pEntry) - RT_OFFSETOF(CR_DEM_ENTRY, Entry)))
333
334static RTMEMCACHE g_VBoxCrDemLookasideList;
335static RTMEMCACHE g_VBoxCrDemInfoLookasideList;
336
337int CrDemGlobalInit()
338{
339 int rc = RTMemCacheCreate(&g_VBoxCrDemLookasideList, sizeof (CR_DEM_ENTRY),
340 0, /* size_t cbAlignment */
341 UINT32_MAX, /* uint32_t cMaxObjects */
342 NULL, /* PFNMEMCACHECTOR pfnCtor*/
343 NULL, /* PFNMEMCACHEDTOR pfnDtor*/
344 NULL, /* void *pvUser*/
345 0 /* uint32_t fFlags*/
346 );
347 if (RT_SUCCESS(rc))
348 {
349 rc = RTMemCacheCreate(&g_VBoxCrDemInfoLookasideList, sizeof (CR_DEM_ENTRY_INFO),
350 0, /* size_t cbAlignment */
351 UINT32_MAX, /* uint32_t cMaxObjects */
352 NULL, /* PFNMEMCACHECTOR pfnCtor*/
353 NULL, /* PFNMEMCACHEDTOR pfnDtor*/
354 NULL, /* void *pvUser*/
355 0 /* uint32_t fFlags*/
356 );
357 if (RT_SUCCESS(rc))
358 return VINF_SUCCESS;
359 else
360 crWarning("RTMemCacheCreate failed rc %d", rc);
361
362 RTMemCacheDestroy(g_VBoxCrDemLookasideList);
363 }
364 else
365 crWarning("RTMemCacheCreate failed rc %d", rc);
366 return VINF_SUCCESS;
367}
368
369void CrDemTeGlobalTerm()
370{
371 RTMemCacheDestroy(g_VBoxCrDemLookasideList);
372 RTMemCacheDestroy(g_VBoxCrDemInfoLookasideList);
373}
374
375static CR_DEM_ENTRY* crDemEntryAlloc()
376{
377 return (CR_DEM_ENTRY*)RTMemCacheAlloc(g_VBoxCrDemLookasideList);
378}
379
380static CR_DEM_ENTRY_INFO* crDemEntryInfoAlloc()
381{
382 return (CR_DEM_ENTRY_INFO*)RTMemCacheAlloc(g_VBoxCrDemInfoLookasideList);
383}
384
385static void crDemEntryFree(CR_DEM_ENTRY* pDemEntry)
386{
387 CrDpEntryCleanup(&pDemEntry->Entry);
388 RTMemCacheFree(g_VBoxCrDemLookasideList, pDemEntry);
389}
390
391static void crDemEntryInfoFree(CR_DEM_ENTRY_INFO* pDemEntryInfo)
392{
393 RTMemCacheFree(g_VBoxCrDemInfoLookasideList, pDemEntryInfo);
394}
395
396void crDemEntryRelease(PCR_DISPLAY_ENTRY_MAP pMap, CR_DEM_ENTRY *pDemEntry)
397{
398 CR_DEM_ENTRY_INFO *pInfo = pDemEntry->pInfo;
399 CRTextureObj *pTobj = pInfo->pTobj;
400
401 --pInfo->cEntries;
402
403 if (!pInfo->cEntries)
404 {
405 CR_STATE_SHAREDOBJ_USAGE_CLEAR(pInfo->pTobj, cr_server.MainContextInfo.pContext);
406
407 crHashtableDelete(pMap->pTexIdToDemInfoMap, pTobj->id, NULL);
408
409 crDemEntryInfoFree(pInfo);
410 }
411
412 if (!CR_STATE_SHAREDOBJ_USAGE_IS_USED(pTobj))
413 {
414 CRSharedState *pShared = crStateGlobalSharedAcquire();
415
416 CRASSERT(pShared);
417 /* on the host side, we need to delete an ogl texture object here as well, which crStateDeleteTextureCallback will do
418 * in addition to calling crStateDeleteTextureObject to delete a state object */
419 crHashtableDelete(pShared->textureTable, pTobj->id, crStateDeleteTextureCallback);
420
421 crStateGlobalSharedRelease();
422 }
423
424 crStateGlobalSharedRelease();
425
426 if (!pMap->cEntered)
427 crDemEntryFree(pDemEntry);
428 else
429 RTListNodeInsertAfter(&pMap->ReleasedList, &pDemEntry->Node);
430}
431
432int CrDemInit(PCR_DISPLAY_ENTRY_MAP pMap)
433{
434 pMap->pTexIdToDemInfoMap = crAllocHashtable();
435 if (pMap->pTexIdToDemInfoMap)
436 {
437 RTListInit(&pMap->ReleasedList);
438 return VINF_SUCCESS;
439 }
440
441 crWarning("crAllocHashtable failed");
442 return VERR_NO_MEMORY;
443}
444
445void CrDemTerm(PCR_DISPLAY_ENTRY_MAP pMap)
446{
447 CRASSERT(RTListIsEmpty(&pMap->ReleasedList));
448 CRASSERT(!pMap->cEntered);
449 crFreeHashtable(pMap->pTexIdToDemInfoMap, NULL);
450 pMap->pTexIdToDemInfoMap = NULL;
451}
452
453void CrDemEnter(PCR_DISPLAY_ENTRY_MAP pMap)
454{
455 ++pMap->cEntered;
456 Assert(pMap->cEntered);
457}
458
459void CrDemLeave(PCR_DISPLAY_ENTRY_MAP pMap, PCR_DISPLAY_ENTRY pNewEntry, PCR_DISPLAY_ENTRY pReplacedEntry)
460{
461 Assert(pMap->cEntered);
462 --pMap->cEntered;
463 Assert(!pReplacedEntry || pNewEntry);
464 if (pNewEntry && pReplacedEntry)
465 {
466 CR_DEM_ENTRY *pNewDemEntry = PCR_DEM_ENTRY_FROM_ENTRY(pNewEntry);
467 CR_DEM_ENTRY *pReplacedDemEntry = PCR_DEM_ENTRY_FROM_ENTRY(pReplacedEntry);
468 Assert(!RTListIsEmpty(&pMap->ReleasedList));
469 Assert(!RTListIsEmpty(&pReplacedDemEntry->Node));
470 Assert(RTListIsEmpty(&pNewDemEntry->Node));
471 Assert(!pNewDemEntry->Entry.pvORInstance);
472 if (!pNewDemEntry->Entry.pvORInstance)
473 {
474 pNewDemEntry->Entry.pvORInstance = pReplacedDemEntry->Entry.pvORInstance;
475 pReplacedDemEntry->Entry.pvORInstance = NULL;
476 }
477 RTListNodeRemove(&pReplacedDemEntry->Node);
478 crDemEntryFree(pReplacedDemEntry);
479 }
480
481 if (!pMap->cEntered)
482 {
483 CR_DEM_ENTRY *pCurEntry, *pNextEntry;
484 RTListForEachSafe(&pMap->ReleasedList, pCurEntry, pNextEntry, CR_DEM_ENTRY, Node)
485 {
486 RTListNodeRemove(&pCurEntry->Node);
487 crDemEntryFree(pCurEntry);
488 }
489 }
490}
491
492void CrDemEntryRelease(PCR_DISPLAY_ENTRY pEntry)
493{
494 CR_DEM_ENTRY *pDemEntry = PCR_DEM_ENTRY_FROM_ENTRY(pEntry);
495 crDemEntryRelease(pDemEntry->pMap, pDemEntry);
496}
497
498int CrDemEntrySaveState(PCR_DISPLAY_ENTRY pEntry, PSSMHANDLE pSSM)
499{
500 CR_DEM_ENTRY *pDemEntry = PCR_DEM_ENTRY_FROM_ENTRY(pEntry);
501 int rc = SSMR3PutU32(pSSM, pDemEntry->pInfo->pTobj->id);
502 AssertRCReturn(rc, rc);
503 return rc;
504}
505
506int CrDemEntryLoadState(PCR_DISPLAY_ENTRY_MAP pMap, PCR_DISPLAY_ENTRY *ppEntry, PSSMHANDLE pSSM)
507{
508 uint32_t u32;
509 int rc = SSMR3GetU32(pSSM, &u32);
510 AssertRCReturn(rc, rc);
511
512 PCR_DISPLAY_ENTRY pEntry = CrDemEntryAcquire(pMap, u32, CRBLT_F_INVERT_SRC_YCOORDS);
513 if (!pEntry)
514 {
515 crWarning("CrDemEntryAcquire failed");
516 return VERR_NO_MEMORY;
517 }
518
519 *ppEntry = pEntry;
520 return VINF_SUCCESS;
521}
522
523PCR_DISPLAY_ENTRY CrDemEntryAcquire(PCR_DISPLAY_ENTRY_MAP pMap, GLuint idTexture, uint32_t fFlags)
524{
525 CR_DEM_ENTRY *pDemEntry = NULL;
526
527 CRSharedState *pShared = crStateGlobalSharedAcquire();
528 if (!pShared)
529 {
530 crWarning("pShared is null!");
531 return NULL;
532 }
533
534 CRTextureObj *pTobj = (CRTextureObj*)crHashtableSearch(pShared->textureTable, idTexture);
535 if (!pTobj)
536 {
537 crWarning("pTobj is null!");
538 crStateGlobalSharedRelease();
539 return NULL;
540 }
541
542 Assert(pTobj->id == idTexture);
543
544 GLuint hwId = crStateGetTextureObjHWID(pTobj);
545 if (!hwId)
546 {
547 crWarning("hwId is null!");
548 crStateGlobalSharedRelease();
549 return NULL;
550 }
551
552 VBOXVR_TEXTURE TextureData;
553 TextureData.width = pTobj->level[0]->width;
554 TextureData.height = pTobj->level[0]->height;
555 TextureData.target = pTobj->target;
556 TextureData.hwid = hwId;
557
558 pDemEntry = crDemEntryAlloc();
559 if (!pDemEntry)
560 {
561 crWarning("crDemEntryAlloc failed allocating CR_DEM_ENTRY");
562 crStateGlobalSharedRelease();
563 return NULL;
564 }
565
566 CrDpEntryInit(&pDemEntry->Entry, &TextureData, fFlags, crDpEntryCEntryReleaseCB);
567
568 CR_DEM_ENTRY_INFO *pInfo = (CR_DEM_ENTRY_INFO*)crHashtableSearch(pMap->pTexIdToDemInfoMap, pTobj->id);
569 if (!pInfo)
570 {
571 pInfo = crDemEntryInfoAlloc();
572 CRASSERT(pInfo);
573 crHashtableAdd(pMap->pTexIdToDemInfoMap, pTobj->id, pInfo);
574 pInfo->cEntries = 0;
575 pInfo->pTobj = pTobj;
576 }
577
578 ++pInfo->cEntries;
579 pDemEntry->pInfo = pInfo;
580 pDemEntry->pMap = pMap;
581 RTListInit(&pDemEntry->Node);
582
583 /* just use main context info's context to hold the texture reference */
584 CR_STATE_SHAREDOBJ_USAGE_SET(pTobj, cr_server.MainContextInfo.pContext);
585
586 return &pDemEntry->Entry;
587}
588
589PCR_DISPLAY crServerDisplayGetInitialized(uint32_t idScreen)
590{
591 if (idScreen >= CR_MAX_GUEST_MONITORS)
592 {
593 crWarning("invalid idScreen %d", idScreen);
594 return NULL;
595 }
596
597 if (ASMBitTest(cr_server.DisplaysInitMap, idScreen))
598 {
599 Assert(cr_server.aDispplays[idScreen].Mural.screenId == idScreen);
600 return &cr_server.aDispplays[idScreen];
601 }
602 return NULL;
603}
604
605static PCR_DISPLAY crServerDisplayGet(uint32_t idScreen)
606{
607 if (idScreen >= CR_MAX_GUEST_MONITORS)
608 {
609 crWarning("invalid idScreen %d", idScreen);
610 return NULL;
611 }
612
613 if (ASMBitTest(cr_server.DisplaysInitMap, idScreen))
614 {
615 Assert(cr_server.aDispplays[idScreen].Mural.screenId == idScreen);
616 return &cr_server.aDispplays[idScreen];
617 }
618
619 int rc = CrDpInit(&cr_server.aDispplays[idScreen]);
620 if (RT_SUCCESS(rc))
621 {
622 CrDpResize(&cr_server.aDispplays[idScreen],
623 cr_server.screen[idScreen].x, cr_server.screen[idScreen].y,
624 cr_server.screen[idScreen].w, cr_server.screen[idScreen].h);
625 ASMBitSet(cr_server.DisplaysInitMap, idScreen);
626 return &cr_server.aDispplays[idScreen];
627 }
628 else
629 {
630 crWarning("CrDpInit failed for screen %d", idScreen);
631 }
632
633 return NULL;
634}
635
636int crServerDisplaySaveState(PSSMHANDLE pSSM)
637{
638 int rc;
639 int cDisplays = 0, i;
640 for (i = 0; i < cr_server.screenCount; ++i)
641 {
642 if (ASMBitTest(cr_server.DisplaysInitMap, i) && !CrDpIsEmpty(&cr_server.aDispplays[i]))
643 ++cDisplays;
644 }
645
646 rc = SSMR3PutS32(pSSM, cDisplays);
647 AssertRCReturn(rc, rc);
648
649 if (!cDisplays)
650 return VINF_SUCCESS;
651
652 rc = SSMR3PutS32(pSSM, cr_server.screenCount);
653 AssertRCReturn(rc, rc);
654
655 for (i = 0; i < cr_server.screenCount; ++i)
656 {
657 rc = SSMR3PutS32(pSSM, cr_server.screen[i].x);
658 AssertRCReturn(rc, rc);
659
660 rc = SSMR3PutS32(pSSM, cr_server.screen[i].y);
661 AssertRCReturn(rc, rc);
662
663 rc = SSMR3PutU32(pSSM, cr_server.screen[i].w);
664 AssertRCReturn(rc, rc);
665
666 rc = SSMR3PutU32(pSSM, cr_server.screen[i].h);
667 AssertRCReturn(rc, rc);
668 }
669
670 for (i = 0; i < cr_server.screenCount; ++i)
671 {
672 if (ASMBitTest(cr_server.DisplaysInitMap, i) && !CrDpIsEmpty(&cr_server.aDispplays[i]))
673 {
674 rc = SSMR3PutS32(pSSM, i);
675 AssertRCReturn(rc, rc);
676
677 rc = CrDpSaveState(&cr_server.aDispplays[i], pSSM);
678 AssertRCReturn(rc, rc);
679 }
680 }
681
682 return VINF_SUCCESS;
683}
684
685int crServerDisplayLoadState(PSSMHANDLE pSSM, uint32_t u32Version)
686{
687 int rc;
688 int cDisplays, screenCount, i;
689
690 rc = SSMR3GetS32(pSSM, &cDisplays);
691 AssertRCReturn(rc, rc);
692
693 if (!cDisplays)
694 return VINF_SUCCESS;
695
696 rc = SSMR3GetS32(pSSM, &screenCount);
697 AssertRCReturn(rc, rc);
698
699 CRASSERT(screenCount == cr_server.screenCount);
700
701 crServerVBoxCompositionSetEnableStateGlobal(GL_FALSE);
702
703 for (i = 0; i < cr_server.screenCount; ++i)
704 {
705 int32_t x, y;
706 uint32_t w, h;
707 rc = SSMR3GetS32(pSSM, &x);
708 AssertRCReturn(rc, rc);
709
710 rc = SSMR3GetS32(pSSM, &y);
711 AssertRCReturn(rc, rc);
712
713 rc = SSMR3GetU32(pSSM, &w);
714 AssertRCReturn(rc, rc);
715
716 rc = SSMR3GetU32(pSSM, &h);
717 AssertRCReturn(rc, rc);
718
719 rc = crVBoxServerMapScreen(i, x, y, w, h, cr_server.screen[i].winID);
720 AssertRCReturn(rc, rc);
721 }
722
723 crServerVBoxCompositionSetEnableStateGlobal(GL_TRUE);
724
725 for (i = 0; i < cDisplays; ++i)
726 {
727 int iScreen;
728
729 rc = SSMR3GetS32(pSSM, &iScreen);
730 AssertRCReturn(rc, rc);
731
732 PCR_DISPLAY pDisplay = crServerDisplayGet((uint32_t)iScreen);
733 if (!pDisplay)
734 {
735 crWarning("crServerDisplayGet failed");
736 return VERR_GENERAL_FAILURE;
737 }
738
739 rc = CrDpLoadState(pDisplay, pSSM, u32Version);
740 AssertRCReturn(rc, rc);
741 }
742
743 return VINF_SUCCESS;
744}
745
746void crServerDisplayTermAll()
747{
748 int i;
749 for (i = 0; i < cr_server.screenCount; ++i)
750 {
751 if (ASMBitTest(cr_server.DisplaysInitMap, i))
752 {
753 CrDpTerm(&cr_server.aDispplays[i]);
754 ASMBitClear(cr_server.DisplaysInitMap, i);
755 }
756 }
757}
758
759void CrHlpFreeTexImage(CRContext *pCurCtx, GLuint idPBO, void *pvData)
760{
761 if (idPBO)
762 {
763 cr_server.head_spu->dispatch_table.UnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
764 if (pCurCtx)
765 cr_server.head_spu->dispatch_table.BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pCurCtx->bufferobject.packBuffer->hwid);
766 else
767 cr_server.head_spu->dispatch_table.BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
768 }
769 else
770 {
771 crFree(pvData);
772 if (pCurCtx && crStateIsBufferBoundForCtx(pCurCtx, GL_PIXEL_PACK_BUFFER_ARB))
773 cr_server.head_spu->dispatch_table.BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pCurCtx->bufferobject.packBuffer->hwid);
774 }
775}
776
777void CrHlpPutTexImage(CRContext *pCurCtx, const VBOXVR_TEXTURE *pTexture, GLenum enmFormat, void *pvData)
778{
779 CRASSERT(pTexture->hwid);
780 cr_server.head_spu->dispatch_table.BindTexture(pTexture->target, pTexture->hwid);
781
782 if (!pCurCtx || crStateIsBufferBoundForCtx(pCurCtx, GL_PIXEL_UNPACK_BUFFER_ARB))
783 {
784 cr_server.head_spu->dispatch_table.BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
785 }
786
787 /*read the texture, note pixels are NULL for PBO case as it's offset in the buffer*/
788 cr_server.head_spu->dispatch_table.TexSubImage2D(GL_TEXTURE_2D, 0 /* level*/, 0 /*xoffset*/, 0 /*yoffset*/, pTexture->width, pTexture->height, enmFormat, GL_UNSIGNED_BYTE, pvData);
789
790 /*restore gl state*/
791 if (pCurCtx)
792 {
793 CRTextureObj *pTObj;
794 CRTextureLevel *pTImg;
795 crStateGetTextureObjectAndImage(pCurCtx, pTexture->target, 0, &pTObj, &pTImg);
796
797 GLuint uid = pTObj->hwid;
798 cr_server.head_spu->dispatch_table.BindTexture(pTexture->target, uid);
799 }
800 else
801 {
802 cr_server.head_spu->dispatch_table.BindTexture(pTexture->target, 0);
803 }
804
805 if (pCurCtx && crStateIsBufferBoundForCtx(pCurCtx, GL_PIXEL_UNPACK_BUFFER_ARB))
806 cr_server.head_spu->dispatch_table.BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pCurCtx->bufferobject.unpackBuffer->hwid);
807}
808
809void* CrHlpGetTexImage(CRContext *pCurCtx, const VBOXVR_TEXTURE *pTexture, GLuint idPBO, GLenum enmFormat)
810{
811 void *pvData = NULL;
812 cr_server.head_spu->dispatch_table.BindTexture(pTexture->target, pTexture->hwid);
813
814 if (idPBO)
815 {
816 cr_server.head_spu->dispatch_table.BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, idPBO);
817 }
818 else
819 {
820 if (!pCurCtx || crStateIsBufferBoundForCtx(pCurCtx, GL_PIXEL_PACK_BUFFER_ARB))
821 {
822 cr_server.head_spu->dispatch_table.BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
823 }
824
825 pvData = crAlloc(4*pTexture->width*pTexture->height);
826 if (!pvData)
827 {
828 crWarning("Out of memory in CrHlpGetTexImage");
829 return NULL;
830 }
831 }
832
833 /*read the texture, note pixels are NULL for PBO case as it's offset in the buffer*/
834 cr_server.head_spu->dispatch_table.GetTexImage(GL_TEXTURE_2D, 0, enmFormat, GL_UNSIGNED_BYTE, pvData);
835
836 /*restore gl state*/
837 if (pCurCtx)
838 {
839 CRTextureObj *pTObj;
840 CRTextureLevel *pTImg;
841 crStateGetTextureObjectAndImage(pCurCtx, pTexture->target, 0, &pTObj, &pTImg);
842
843 GLuint uid = pTObj->hwid;
844 cr_server.head_spu->dispatch_table.BindTexture(pTexture->target, uid);
845 }
846 else
847 {
848 cr_server.head_spu->dispatch_table.BindTexture(pTexture->target, 0);
849 }
850
851 if (idPBO)
852 {
853 pvData = cr_server.head_spu->dispatch_table.MapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
854 if (!pvData)
855 {
856 crWarning("Failed to MapBuffer in CrHlpGetTexImage");
857 return NULL;
858 }
859 }
860
861 CRASSERT(pvData);
862 return pvData;
863}
864
865void SERVER_DISPATCH_APIENTRY
866crServerDispatchVBoxTexPresent(GLuint texture, GLuint cfg, GLint xPos, GLint yPos, GLint cRects, const GLint *pRects)
867{
868 uint32_t idScreen = CR_PRESENT_GET_SCREEN(cfg);
869 if (idScreen >= CR_MAX_GUEST_MONITORS)
870 {
871 crWarning("Invalid guest screen");
872 return;
873 }
874
875 PCR_DISPLAY pDisplay;
876 PCR_DISPLAY_ENTRY pEntry = NULL;
877
878 if (texture)
879 {
880 pEntry = CrDemEntryAcquire(&cr_server.PresentTexturepMap, texture, (cfg & CR_PRESENT_FLAG_TEX_NONINVERT_YCOORD) ? 0 : CRBLT_F_INVERT_SRC_YCOORDS);
881 if (!pEntry)
882 {
883 crWarning("CrDemEntryAcquire Failed");
884 return;
885 }
886
887 pDisplay = crServerDisplayGet(idScreen);
888 if (!pDisplay)
889 {
890 crWarning("crServerDisplayGet Failed");
891 return;
892 }
893 }
894 else
895 {
896 pDisplay = crServerDisplayGetInitialized(idScreen);
897 if (!pDisplay)
898 {
899 /* no display initialized, and nothing to present */
900 return;
901 }
902 }
903
904 CrDpEnter(pDisplay);
905
906 if (!(cfg & CR_PRESENT_FLAG_CLEAR_RECTS))
907 {
908 RTPOINT Point = {xPos, yPos};
909 int rc = CrDpEntryRegionsAdd(pDisplay, pEntry, &Point, (uint32_t)cRects, (const RTRECT*)pRects, &cr_server.PresentTexturepMap);
910 if (!RT_SUCCESS(rc))
911 {
912 crWarning("CrDpEntryRegionsAdd Failed rc %d", rc);
913// if (pEntry)
914// CrDemEntryRelease(pEntry);
915 return;
916 }
917 }
918 else
919 {
920 CrDpRegionsClear(pDisplay);
921 }
922
923 CrDpLeave(pDisplay);
924}
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