VirtualBox

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

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

crOpenGL: fix flickering on osx

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 166.1 KB
Line 
1/* $Id: server_presenter.cpp 51442 2014-05-28 11:09:30Z 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#include <cr_htable.h>
29#include <cr_bmpscale.h>
30
31#include <iprt/cdefs.h>
32#include <iprt/types.h>
33#include <iprt/asm.h>
34#include <iprt/mem.h>
35#include <iprt/list.h>
36
37
38#ifdef DEBUG_misha
39# define VBOXVDBG_MEMCACHE_DISABLE
40#endif
41
42#ifndef VBOXVDBG_MEMCACHE_DISABLE
43# include <iprt/memcache.h>
44#endif
45
46#include "render/renderspu.h"
47
48class ICrFbDisplay
49{
50public:
51 virtual int UpdateBegin(struct CR_FRAMEBUFFER *pFb) = 0;
52 virtual void UpdateEnd(struct CR_FRAMEBUFFER *pFb) = 0;
53
54 virtual int EntryCreated(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry) = 0;
55 virtual int EntryAdded(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry) = 0;
56 virtual int EntryReplaced(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hNewEntry, HCR_FRAMEBUFFER_ENTRY hReplacedEntry) = 0;
57 virtual int EntryTexChanged(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry) = 0;
58 virtual int EntryRemoved(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry) = 0;
59 virtual int EntryDestroyed(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry) = 0;
60 virtual int EntryPosChanged(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry) = 0;
61
62 virtual int RegionsChanged(struct CR_FRAMEBUFFER *pFb) = 0;
63
64 virtual int FramebufferChanged(struct CR_FRAMEBUFFER *pFb) = 0;
65
66 virtual ~ICrFbDisplay() {}
67};
68
69class CrFbDisplayComposite;
70class CrFbDisplayBase;
71class CrFbDisplayWindow;
72class CrFbDisplayWindowRootVr;
73class CrFbDisplayVrdp;
74
75typedef struct CR_FRAMEBUFFER
76{
77 VBOXVR_SCR_COMPOSITOR Compositor;
78 struct VBVAINFOSCREEN ScreenInfo;
79 void *pvVram;
80 ICrFbDisplay *pDisplay;
81 RTLISTNODE EntriesList;
82 uint32_t cEntries; /* <- just for debugging */
83 uint32_t cUpdating;
84 CRHTABLE SlotTable;
85} CR_FRAMEBUFFER;
86
87typedef union CR_FBENTRY_FLAGS
88{
89 struct {
90 uint32_t fCreateNotified : 1;
91 uint32_t fInList : 1;
92 uint32_t Reserved : 30;
93 };
94 uint32_t Value;
95} CR_FBENTRY_FLAGS;
96
97typedef struct CR_FRAMEBUFFER_ENTRY
98{
99 VBOXVR_SCR_COMPOSITOR_ENTRY Entry;
100 RTLISTNODE Node;
101 uint32_t cRefs;
102 CR_FBENTRY_FLAGS Flags;
103 CRHTABLE HTable;
104} CR_FRAMEBUFFER_ENTRY;
105
106typedef struct CR_FBTEX
107{
108 CR_TEXDATA Tex;
109 CRTextureObj *pTobj;
110} CR_FBTEX;
111
112#define PCR_FBTEX_FROM_TEX(_pTex) ((CR_FBTEX*)((uint8_t*)(_pTex) - RT_OFFSETOF(CR_FBTEX, Tex)))
113#define PCR_FRAMEBUFFER_FROM_COMPOSITOR(_pCompositor) ((CR_FRAMEBUFFER*)((uint8_t*)(_pCompositor) - RT_OFFSETOF(CR_FRAMEBUFFER, Compositor)))
114#define PCR_FBENTRY_FROM_ENTRY(_pEntry) ((CR_FRAMEBUFFER_ENTRY*)((uint8_t*)(_pEntry) - RT_OFFSETOF(CR_FRAMEBUFFER_ENTRY, Entry)))
115
116typedef struct CR_FB_INFO
117{
118 CrFbDisplayComposite *pDpComposite;
119 uint32_t u32Id;
120 VBOXCMDVBVA_SCREENMAP_DECL(uint32_t, aTargetMap);
121} CR_FB_INFO;
122
123typedef struct CR_FBDISPLAY_INFO
124{
125 CrFbDisplayWindow *pDpWin;
126 CrFbDisplayWindowRootVr *pDpWinRootVr;
127 CrFbDisplayVrdp *pDpVrdp;
128 uint32_t u32Id;
129 int32_t iFb;
130} CR_FBDISPLAY_INFO;
131
132typedef struct CR_PRESENTER_GLOBALS
133{
134#ifndef VBOXVDBG_MEMCACHE_DISABLE
135 RTMEMCACHE FbEntryLookasideList;
136 RTMEMCACHE FbTexLookasideList;
137 RTMEMCACHE CEntryLookasideList;
138#endif
139 uint32_t u32DisplayMode;
140 uint32_t u32DisabledDisplayMode;
141 bool fEnabled;
142 CRHashTable *pFbTexMap;
143 CR_FBDISPLAY_INFO aDisplayInfos[CR_MAX_GUEST_MONITORS];
144 CR_FBMAP FramebufferInitMap;
145 CR_FRAMEBUFFER aFramebuffers[CR_MAX_GUEST_MONITORS];
146 CR_FB_INFO aFbInfos[CR_MAX_GUEST_MONITORS];
147 bool fWindowsForceHidden;
148 uint32_t cbTmpBuf;
149 void *pvTmpBuf;
150 uint32_t cbTmpBuf2;
151 void *pvTmpBuf2;
152} CR_PRESENTER_GLOBALS;
153
154static CR_PRESENTER_GLOBALS g_CrPresenter;
155
156/* FRAMEBUFFER */
157
158void CrFbInit(CR_FRAMEBUFFER *pFb, uint32_t idFb)
159{
160 RTRECT Rect;
161 Rect.xLeft = 0;
162 Rect.yTop = 0;
163 Rect.xRight = 1;
164 Rect.yBottom = 1;
165 memset(pFb, 0, sizeof (*pFb));
166 pFb->ScreenInfo.u16Flags = VBVA_SCREEN_F_DISABLED;
167 pFb->ScreenInfo.u32ViewIndex = idFb;
168 CrVrScrCompositorInit(&pFb->Compositor, &Rect);
169 RTListInit(&pFb->EntriesList);
170 CrHTableCreate(&pFb->SlotTable, 0);
171}
172
173bool CrFbIsEnabled(CR_FRAMEBUFFER *pFb)
174{
175 return !(pFb->ScreenInfo.u16Flags & VBVA_SCREEN_F_DISABLED);
176}
177
178HCR_FRAMEBUFFER_ENTRY CrFbEntryFromCompositorEntry(const struct VBOXVR_SCR_COMPOSITOR_ENTRY* pCEntry);
179
180const struct VBOXVR_SCR_COMPOSITOR* CrFbGetCompositor(CR_FRAMEBUFFER *pFb)
181{
182 return &pFb->Compositor;
183}
184
185DECLINLINE(CR_FRAMEBUFFER*) CrFbFromCompositor(const struct VBOXVR_SCR_COMPOSITOR* pCompositor)
186{
187 return RT_FROM_MEMBER(pCompositor, CR_FRAMEBUFFER, Compositor);
188}
189
190const struct VBVAINFOSCREEN* CrFbGetScreenInfo(HCR_FRAMEBUFFER hFb)
191{
192 return &hFb->ScreenInfo;
193}
194
195void* CrFbGetVRAM(HCR_FRAMEBUFFER hFb)
196{
197 return hFb->pvVram;
198}
199
200int CrFbUpdateBegin(CR_FRAMEBUFFER *pFb)
201{
202 ++pFb->cUpdating;
203
204 if (pFb->cUpdating == 1)
205 {
206 if (pFb->pDisplay)
207 pFb->pDisplay->UpdateBegin(pFb);
208 }
209
210 return VINF_SUCCESS;
211}
212
213void CrFbUpdateEnd(CR_FRAMEBUFFER *pFb)
214{
215 if (!pFb->cUpdating)
216 {
217 WARN(("invalid UpdateEnd call!"));
218 return;
219 }
220
221 --pFb->cUpdating;
222
223 if (!pFb->cUpdating)
224 {
225 if (pFb->pDisplay)
226 pFb->pDisplay->UpdateEnd(pFb);
227 }
228}
229
230bool CrFbIsUpdating(const CR_FRAMEBUFFER *pFb)
231{
232 return !!pFb->cUpdating;
233}
234
235bool CrFbHas3DData(HCR_FRAMEBUFFER hFb)
236{
237 return !CrVrScrCompositorIsEmpty(&hFb->Compositor);
238}
239
240static void crFbImgFromScreenVram(const VBVAINFOSCREEN *pScreen, void *pvVram, CR_BLITTER_IMG *pImg)
241{
242 pImg->pvData = pvVram;
243 pImg->cbData = pScreen->u32LineSize * pScreen->u32Height;
244 pImg->enmFormat = GL_BGRA;
245 pImg->width = pScreen->u32Width;
246 pImg->height = pScreen->u32Height;
247 pImg->bpp = pScreen->u16BitsPerPixel;
248 pImg->pitch = pScreen->u32LineSize;
249}
250
251static void crFbImgFromDimPtrBGRA(void *pvVram, uint32_t width, uint32_t height, CR_BLITTER_IMG *pImg)
252{
253 pImg->pvData = pvVram;
254 pImg->cbData = width * height * 4;
255 pImg->enmFormat = GL_BGRA;
256 pImg->width = width;
257 pImg->height = height;
258 pImg->bpp = 32;
259 pImg->pitch = width * 4;
260}
261
262static int8_t crFbImgFromDimOffVramBGRA(VBOXCMDVBVAOFFSET offVRAM, uint32_t width, uint32_t height, CR_BLITTER_IMG *pImg)
263{
264 uint32_t cbBuff = width * height * 4;
265 if (offVRAM >= g_cbVRam
266 || offVRAM + cbBuff >= g_cbVRam)
267 {
268 WARN(("invalid param"));
269 return -1;
270 }
271
272 uint8_t *pu8Buf = g_pvVRamBase + offVRAM;
273 crFbImgFromDimPtrBGRA(pu8Buf, width, height, pImg);
274
275 return 0;
276}
277
278static int8_t crFbImgFromDescBGRA(const VBOXCMDVBVA_ALLOCDESC *pDesc, CR_BLITTER_IMG *pImg)
279{
280 return crFbImgFromDimOffVramBGRA(pDesc->Info.u.offVRAM, pDesc->u16Width, pDesc->u16Height, pImg);
281}
282
283static void crFbImgFromFb(HCR_FRAMEBUFFER hFb, CR_BLITTER_IMG *pImg)
284{
285 const VBVAINFOSCREEN *pScreen = CrFbGetScreenInfo(hFb);
286 void *pvVram = CrFbGetVRAM(hFb);
287 crFbImgFromScreenVram(pScreen, pvVram, pImg);
288}
289
290static int crFbTexDataGetContents(CR_TEXDATA *pTex, const RTPOINT *pPos, uint32_t cRects, const RTRECT *pRects, CR_BLITTER_IMG *pDst)
291{
292 const CR_BLITTER_IMG *pSrcImg;
293 int rc = CrTdBltDataAcquire(pTex, GL_BGRA, false, &pSrcImg);
294 if (!RT_SUCCESS(rc))
295 {
296 WARN(("CrTdBltDataAcquire failed rc %d", rc));
297 return rc;
298 }
299
300 CrMBltImg(pSrcImg, pPos, cRects, pRects, pDst);
301
302 CrTdBltDataRelease(pTex);
303
304 return VINF_SUCCESS;
305}
306
307static int crFbBltGetContentsScaledDirect(HCR_FRAMEBUFFER hFb, const RTRECTSIZE *pSrcRectSize, const RTRECT *pDstRect, uint32_t cRects, const RTRECT *pRects, CR_BLITTER_IMG *pDst)
308{
309 VBOXVR_LIST List;
310 uint32_t c2DRects = 0;
311 CR_TEXDATA *pEnteredTex = NULL;
312 PCR_BLITTER pEnteredBlitter = NULL;
313 uint32_t width = 0, height = 0;
314 RTPOINT ScaledEntryPoint = {0};
315 RTRECT ScaledSrcRect = {0};
316
317 VBOXVR_SCR_COMPOSITOR_CONST_ITERATOR Iter;
318 int32_t srcWidth = pSrcRectSize->cx;
319 int32_t srcHeight = pSrcRectSize->cy;
320 int32_t dstWidth = pDstRect->xRight - pDstRect->xLeft;
321 int32_t dstHeight = pDstRect->yBottom - pDstRect->yTop;
322
323 RTPOINT DstPoint = {pDstRect->xLeft, pDstRect->yTop};
324 float strX = ((float)dstWidth) / srcWidth;
325 float strY = ((float)dstHeight) / srcHeight;
326 bool fScale = (dstWidth != srcWidth || dstHeight != srcHeight);
327 Assert(fScale);
328
329 VBoxVrListInit(&List);
330 int rc = VBoxVrListRectsAdd(&List, 1, CrVrScrCompositorRectGet(&hFb->Compositor), NULL);
331 if (!RT_SUCCESS(rc))
332 {
333 WARN(("VBoxVrListRectsAdd failed rc %d", rc));
334 goto end;
335 }
336
337 CrVrScrCompositorConstIterInit(&hFb->Compositor, &Iter);
338
339 for(const VBOXVR_SCR_COMPOSITOR_ENTRY *pEntry = CrVrScrCompositorConstIterNext(&Iter);
340 pEntry;
341 pEntry = CrVrScrCompositorConstIterNext(&Iter))
342 {
343 uint32_t cRegions;
344 const RTRECT *pRegions;
345 rc = CrVrScrCompositorEntryRegionsGet(&hFb->Compositor, pEntry, &cRegions, NULL, NULL, &pRegions);
346 if (!RT_SUCCESS(rc))
347 {
348 WARN(("CrVrScrCompositorEntryRegionsGet failed rc %d", rc));
349 goto end;
350 }
351
352 rc = VBoxVrListRectsSubst(&List, cRegions, pRegions, NULL);
353 if (!RT_SUCCESS(rc))
354 {
355 WARN(("VBoxVrListRectsSubst failed rc %d", rc));
356 goto end;
357 }
358
359 for (uint32_t j = 0; j < cRegions; ++j)
360 {
361 /* rects are in dst coordinates,
362 * while the pReg is in source coords
363 * convert */
364 const RTRECT * pReg = &pRegions[j];
365 RTRECT ScaledReg;
366 /* scale */
367 VBoxRectScaled(pReg, strX, strY, &ScaledReg);
368 /* translate */
369 VBoxRectTranslate(&ScaledReg, pDstRect->xLeft, pDstRect->yTop);
370
371 for (uint32_t i = 0; i < cRects; ++i)
372 {
373 const RTRECT * pRect = &pRects[i];
374
375 RTRECT Intersection;
376 VBoxRectIntersected(pRect, &ScaledReg, &Intersection);
377 if (VBoxRectIsZero(&Intersection))
378 continue;
379
380 CR_TEXDATA *pTex = CrVrScrCompositorEntryTexGet(pEntry);
381 const CR_BLITTER_IMG *pSrcImg;
382
383 if (pEnteredTex != pTex)
384 {
385 if (!pEnteredBlitter)
386 {
387 pEnteredBlitter = CrTdBlitterGet(pTex);
388 rc = CrBltEnter(pEnteredBlitter);
389 if (!RT_SUCCESS(rc))
390 {
391 WARN(("CrBltEnter failed %d", rc));
392 pEnteredBlitter = NULL;
393 goto end;
394 }
395 }
396
397 if (pEnteredTex)
398 {
399 CrTdBltLeave(pEnteredTex);
400
401 pEnteredTex = NULL;
402
403 if (pEnteredBlitter != CrTdBlitterGet(pTex))
404 {
405 WARN(("blitters not equal!"));
406 CrBltLeave(pEnteredBlitter);
407
408 pEnteredBlitter = CrTdBlitterGet(pTex);
409 rc = CrBltEnter(pEnteredBlitter);
410 if (!RT_SUCCESS(rc))
411 {
412 WARN(("CrBltEnter failed %d", rc));
413 pEnteredBlitter = NULL;
414 goto end;
415 }
416 }
417 }
418
419 rc = CrTdBltEnter(pTex);
420 if (!RT_SUCCESS(rc))
421 {
422 WARN(("CrTdBltEnter failed %d", rc));
423 goto end;
424 }
425
426 pEnteredTex = pTex;
427
428 const VBOXVR_TEXTURE *pVrTex = CrTdTexGet(pTex);
429
430 width = CR_FLOAT_RCAST(uint32_t, strX * pVrTex->width);
431 height = CR_FLOAT_RCAST(uint32_t, strY * pVrTex->height);
432 ScaledEntryPoint.x = CR_FLOAT_RCAST(int32_t, strX * CrVrScrCompositorEntryRectGet(pEntry)->xLeft) + pDstRect->xLeft;
433 ScaledEntryPoint.y = CR_FLOAT_RCAST(int32_t, strY * CrVrScrCompositorEntryRectGet(pEntry)->yTop) + pDstRect->yTop;
434 ScaledSrcRect.xLeft = ScaledEntryPoint.x;
435 ScaledSrcRect.yTop = ScaledEntryPoint.y;
436 ScaledSrcRect.xRight = width + ScaledEntryPoint.x;
437 ScaledSrcRect.yBottom = height + ScaledEntryPoint.y;
438 }
439
440 VBoxRectIntersect(&Intersection, &ScaledSrcRect);
441 if (VBoxRectIsZero(&Intersection))
442 continue;
443
444 rc = CrTdBltDataAcquireScaled(pTex, GL_BGRA, false, width, height, &pSrcImg);
445 if (!RT_SUCCESS(rc))
446 {
447 WARN(("CrTdBltDataAcquire failed rc %d", rc));
448 goto end;
449 }
450
451 bool fInvert = !(CrVrScrCompositorEntryFlagsGet(pEntry) & CRBLT_F_INVERT_SRC_YCOORDS);
452
453 CrMBltImgRect(pSrcImg, &ScaledEntryPoint, fInvert, &Intersection, pDst);
454
455 CrTdBltDataReleaseScaled(pTex, pSrcImg);
456 }
457 }
458 }
459
460 c2DRects = VBoxVrListRectsCount(&List);
461 if (c2DRects)
462 {
463 if (g_CrPresenter.cbTmpBuf2 < c2DRects * sizeof (RTRECT))
464 {
465 if (g_CrPresenter.pvTmpBuf2)
466 RTMemFree(g_CrPresenter.pvTmpBuf2);
467
468 g_CrPresenter.cbTmpBuf2 = (c2DRects + 10) * sizeof (RTRECT);
469 g_CrPresenter.pvTmpBuf2 = RTMemAlloc(g_CrPresenter.cbTmpBuf2);
470 if (!g_CrPresenter.pvTmpBuf2)
471 {
472 WARN(("RTMemAlloc failed!"));
473 g_CrPresenter.cbTmpBuf2 = 0;
474 rc = VERR_NO_MEMORY;
475 goto end;
476 }
477 }
478
479 RTRECT *p2DRects = (RTRECT *)g_CrPresenter.pvTmpBuf2;
480
481 rc = VBoxVrListRectsGet(&List, c2DRects, p2DRects);
482 if (!RT_SUCCESS(rc))
483 {
484 WARN(("VBoxVrListRectsGet failed, rc %d", rc));
485 goto end;
486 }
487
488 const RTRECT *pCompRect = CrVrScrCompositorRectGet(&hFb->Compositor);
489
490 CR_BLITTER_IMG FbImg;
491
492 crFbImgFromFb(hFb, &FbImg);
493
494 for (uint32_t i = 0; i < c2DRects; ++i)
495 {
496 VBoxRectScale(&p2DRects[i], strX, strY);
497 }
498
499 CrMBltImgScaled(&FbImg, pSrcRectSize, pDstRect, c2DRects, p2DRects, pDst);
500 }
501
502end:
503
504 if (pEnteredTex)
505 CrTdBltLeave(pEnteredTex);
506
507 if (pEnteredBlitter)
508 CrBltLeave(pEnteredBlitter);
509
510 VBoxVrListClear(&List);
511
512 return rc;
513}
514
515static int crFbBltGetContentsScaledCPU(HCR_FRAMEBUFFER hFb, const RTRECTSIZE *pSrcRectSize, const RTRECT *pDstRect, uint32_t cRects, const RTRECT *pRects, CR_BLITTER_IMG *pImg)
516{
517 WARN(("not implemented!"));
518 return VERR_NOT_IMPLEMENTED;
519#if 0
520 int32_t srcWidth = pSrcRectSize->cx;
521 int32_t srcHeight = pSrcRectSize->cy;
522 int32_t dstWidth = pDstRect->xRight - pDstRect->xLeft;
523 int32_t dstHeight = pDstRect->yBottom - pDstRect->yTop;
524
525 RTPOINT DstPoint = {pDstRect->xLeft, pDstRect->yTop};
526 float strX = ((float)dstWidth) / srcWidth;
527 float strY = ((float)dstHeight) / srcHeight;
528
529 RTPOINT UnscaledPos;
530 UnscaledPos.x = CR_FLOAT_RCAST(int32_t, pDstRect->xLeft / strX);
531 UnscaledPos.y = CR_FLOAT_RCAST(int32_t, pDstRect->yTop / strY);
532
533 /* destination is bigger than the source, do 3D data stretching with CPU */
534 CR_BLITTER_IMG Img;
535 Img.cbData = srcWidth * srcHeight * 4;
536 Img.pvData = RTMemAlloc(Img.cbData);
537 if (!Img.pvData)
538 {
539 WARN(("RTMemAlloc Failed"));
540 return VERR_NO_MEMORY;
541 }
542 Img.enmFormat = pImg->enmFormat;
543 Img.width = srcWidth;
544 Img.height = srcHeight;
545 Img.bpp = pImg->bpp;
546 Img.pitch = Img.width * 4;
547
548 int rc = CrFbBltGetContents(hFb, &UnscaledPos, cRects, pRects, &Img);
549 if (RT_SUCCESS(rc))
550 {
551 CrBmpScale32((uint8_t *)pImg->pvData,
552 pImg->pitch,
553 pImg->width, pImg->height,
554 (const uint8_t *)Img.pvData,
555 Img.pitch,
556 Img.width, Img.height);
557 }
558 else
559 WARN(("CrFbBltGetContents failed %d", rc));
560
561 RTMemFree(Img.pvData);
562
563 return rc;
564#endif
565}
566
567int CrFbBltGetContents(HCR_FRAMEBUFFER hFb, const RTPOINT *pPos, uint32_t cRects, const RTRECT *pRects, CR_BLITTER_IMG *pDst)
568{
569 VBOXVR_LIST List;
570 uint32_t c2DRects = 0;
571 CR_TEXDATA *pEnteredTex = NULL;
572 PCR_BLITTER pEnteredBlitter = NULL;
573 RTPOINT EntryPoint = {0};
574
575 VBoxVrListInit(&List);
576 int rc = VBoxVrListRectsAdd(&List, 1, CrVrScrCompositorRectGet(&hFb->Compositor), NULL);
577 if (!RT_SUCCESS(rc))
578 {
579 WARN(("VBoxVrListRectsAdd failed rc %d", rc));
580 goto end;
581 }
582
583 VBOXVR_SCR_COMPOSITOR_CONST_ITERATOR Iter;
584 CrVrScrCompositorConstIterInit(&hFb->Compositor, &Iter);
585
586 for(const VBOXVR_SCR_COMPOSITOR_ENTRY *pEntry = CrVrScrCompositorConstIterNext(&Iter);
587 pEntry;
588 pEntry = CrVrScrCompositorConstIterNext(&Iter))
589 {
590 uint32_t cRegions;
591 const RTRECT *pRegions;
592 rc = CrVrScrCompositorEntryRegionsGet(&hFb->Compositor, pEntry, &cRegions, NULL, NULL, &pRegions);
593 if (!RT_SUCCESS(rc))
594 {
595 WARN(("CrVrScrCompositorEntryRegionsGet failed rc %d", rc));
596 goto end;
597 }
598
599 rc = VBoxVrListRectsSubst(&List, cRegions, pRegions, NULL);
600 if (!RT_SUCCESS(rc))
601 {
602 WARN(("VBoxVrListRectsSubst failed rc %d", rc));
603 goto end;
604 }
605
606 for (uint32_t j = 0; j < cRegions; ++j)
607 {
608 /* rects are in dst coordinates,
609 * while the pReg is in source coords
610 * convert */
611 const RTRECT * pReg = &pRegions[j];
612 RTRECT SrcReg;
613 /* translate */
614 VBoxRectTranslated(pReg, pPos->x, pPos->y, &SrcReg);
615
616 for (uint32_t i = 0; i < cRects; ++i)
617 {
618 const RTRECT * pRect = &pRects[i];
619
620 RTRECT Intersection;
621 VBoxRectIntersected(pRect, &SrcReg, &Intersection);
622 if (VBoxRectIsZero(&Intersection))
623 continue;
624
625 CR_TEXDATA *pTex = CrVrScrCompositorEntryTexGet(pEntry);
626 const CR_BLITTER_IMG *pSrcImg;
627
628 if (pEnteredTex != pTex)
629 {
630 if (!pEnteredBlitter)
631 {
632 pEnteredBlitter = CrTdBlitterGet(pTex);
633 rc = CrBltEnter(pEnteredBlitter);
634 if (!RT_SUCCESS(rc))
635 {
636 WARN(("CrBltEnter failed %d", rc));
637 pEnteredBlitter = NULL;
638 goto end;
639 }
640 }
641
642 if (pEnteredTex)
643 {
644 CrTdBltLeave(pEnteredTex);
645
646 pEnteredTex = NULL;
647
648 if (pEnteredBlitter != CrTdBlitterGet(pTex))
649 {
650 WARN(("blitters not equal!"));
651 CrBltLeave(pEnteredBlitter);
652
653 pEnteredBlitter = CrTdBlitterGet(pTex);
654 rc = CrBltEnter(pEnteredBlitter);
655 if (!RT_SUCCESS(rc))
656 {
657 WARN(("CrBltEnter failed %d", rc));
658 pEnteredBlitter = NULL;
659 goto end;
660 }
661 }
662 }
663
664 rc = CrTdBltEnter(pTex);
665 if (!RT_SUCCESS(rc))
666 {
667 WARN(("CrTdBltEnter failed %d", rc));
668 goto end;
669 }
670
671 pEnteredTex = pTex;
672 EntryPoint.x = CrVrScrCompositorEntryRectGet(pEntry)->xLeft + pPos->x;
673 EntryPoint.y = CrVrScrCompositorEntryRectGet(pEntry)->yTop + pPos->y;
674 }
675
676 rc = CrTdBltDataAcquire(pTex, GL_BGRA, false, &pSrcImg);
677 if (!RT_SUCCESS(rc))
678 {
679 WARN(("CrTdBltDataAcquire failed rc %d", rc));
680 goto end;
681 }
682
683 bool fInvert = !(CrVrScrCompositorEntryFlagsGet(pEntry) & CRBLT_F_INVERT_SRC_YCOORDS);
684
685 CrMBltImgRect(pSrcImg, &EntryPoint, fInvert, &Intersection, pDst);
686
687 CrTdBltDataRelease(pTex);
688 }
689 }
690 }
691
692 c2DRects = VBoxVrListRectsCount(&List);
693 if (c2DRects)
694 {
695 if (g_CrPresenter.cbTmpBuf2 < c2DRects * sizeof (RTRECT))
696 {
697 if (g_CrPresenter.pvTmpBuf2)
698 RTMemFree(g_CrPresenter.pvTmpBuf2);
699
700 g_CrPresenter.cbTmpBuf2 = (c2DRects + 10) * sizeof (RTRECT);
701 g_CrPresenter.pvTmpBuf2 = RTMemAlloc(g_CrPresenter.cbTmpBuf2);
702 if (!g_CrPresenter.pvTmpBuf2)
703 {
704 WARN(("RTMemAlloc failed!"));
705 g_CrPresenter.cbTmpBuf2 = 0;
706 rc = VERR_NO_MEMORY;
707 goto end;
708 }
709 }
710
711 RTRECT *p2DRects = (RTRECT *)g_CrPresenter.pvTmpBuf2;
712
713 rc = VBoxVrListRectsGet(&List, c2DRects, p2DRects);
714 if (!RT_SUCCESS(rc))
715 {
716 WARN(("VBoxVrListRectsGet failed, rc %d", rc));
717 goto end;
718 }
719
720 const RTRECT *pCompRect = CrVrScrCompositorRectGet(&hFb->Compositor);
721
722 CR_BLITTER_IMG FbImg;
723
724 crFbImgFromFb(hFb, &FbImg);
725
726 CrMBltImg(&FbImg, pPos, c2DRects, p2DRects, pDst);
727 }
728
729end:
730
731 if (pEnteredTex)
732 CrTdBltLeave(pEnteredTex);
733
734 if (pEnteredBlitter)
735 CrBltLeave(pEnteredBlitter);
736
737 VBoxVrListClear(&List);
738
739 return rc;
740}
741
742int CrFbBltGetContentsEx(HCR_FRAMEBUFFER hFb, const RTRECTSIZE *pSrcRectSize, const RTRECT *pDstRect, uint32_t cRects, const RTRECT *pRects, CR_BLITTER_IMG *pImg)
743{
744 uint32_t srcWidth = pSrcRectSize->cx;
745 uint32_t srcHeight = pSrcRectSize->cy;
746 uint32_t dstWidth = pDstRect->xRight - pDstRect->xLeft;
747 uint32_t dstHeight = pDstRect->yBottom - pDstRect->yTop;
748 if (srcWidth == dstWidth
749 && srcHeight == dstHeight)
750 {
751 RTPOINT Pos = {pDstRect->xLeft, pDstRect->yTop};
752 return CrFbBltGetContents(hFb, &Pos, cRects, pRects, pImg);
753 }
754 if (!CrFbHas3DData(hFb)
755 || (srcWidth * srcHeight > dstWidth * dstHeight))
756 return crFbBltGetContentsScaledDirect(hFb, pSrcRectSize, pDstRect, cRects, pRects, pImg);
757
758 return crFbBltGetContentsScaledCPU(hFb, pSrcRectSize, pDstRect, cRects, pRects, pImg);
759}
760
761static void crFbBltPutContentsFbVram(HCR_FRAMEBUFFER hFb, const RTPOINT *pPos, uint32_t cRects, const RTRECT *pRects, CR_BLITTER_IMG *pSrc)
762{
763 const RTRECT *pCompRect = CrVrScrCompositorRectGet(&hFb->Compositor);
764
765 CR_BLITTER_IMG FbImg;
766
767 crFbImgFromFb(hFb, &FbImg);
768
769 CrMBltImg(pSrc, pPos, cRects, pRects, &FbImg);
770}
771
772static void crFbClrFillFbVram(HCR_FRAMEBUFFER hFb, uint32_t cRects, const RTRECT *pRects, uint32_t u32Color)
773{
774 CR_BLITTER_IMG FbImg;
775
776 crFbImgFromFb(hFb, &FbImg);
777
778 CrMClrFillImg(&FbImg, cRects, pRects, u32Color);
779}
780
781int CrFbClrFill(HCR_FRAMEBUFFER hFb, uint32_t cRects, const RTRECT *pRects, uint32_t u32Color)
782{
783 if (!hFb->cUpdating)
784 {
785 WARN(("framebuffer not updating"));
786 return VERR_INVALID_STATE;
787 }
788
789 crFbClrFillFbVram(hFb, cRects, pRects, u32Color);
790
791 RTPOINT DstPoint = {0, 0};
792
793 int rc = CrFbEntryRegionsAdd(hFb, NULL, &DstPoint, cRects, pRects, false);
794 if (!RT_SUCCESS(rc))
795 {
796 WARN(("CrFbEntryRegionsAdd failed %d", rc));
797 return rc;
798 }
799
800 return VINF_SUCCESS;
801}
802
803static int crFbBltPutContents(HCR_FRAMEBUFFER hFb, const RTPOINT *pPos, uint32_t cRects, const RTRECT *pRects, CR_BLITTER_IMG *pImg)
804{
805 crFbBltPutContentsFbVram(hFb, pPos, cRects, pRects, pImg);
806
807 int rc = CrFbEntryRegionsAdd(hFb, NULL, pPos, cRects, pRects, false);
808 if (!RT_SUCCESS(rc))
809 {
810 WARN(("CrFbEntryRegionsAdd failed %d", rc));
811 return rc;
812 }
813
814 return VINF_SUCCESS;
815}
816
817int CrFbBltPutContents(HCR_FRAMEBUFFER hFb, const RTPOINT *pPos, uint32_t cRects, const RTRECT *pRects, CR_BLITTER_IMG *pImg)
818{
819 if (!hFb->cUpdating)
820 {
821 WARN(("framebuffer not updating"));
822 return VERR_INVALID_STATE;
823 }
824
825 return crFbBltPutContents(hFb, pPos, cRects, pRects, pImg);
826}
827
828static int crFbRegionsIsIntersectRects(HCR_FRAMEBUFFER hFb, uint32_t cRects, const RTRECT *pRects, bool *pfRegChanged)
829{
830 uint32_t cCompRects;
831 const RTRECT *pCompRects;
832 int rc = CrVrScrCompositorRegionsGet(&hFb->Compositor, &cCompRects, NULL, NULL, &pCompRects);
833 if (!RT_SUCCESS(rc))
834 {
835 WARN(("CrVrScrCompositorRegionsGet failed rc %d", rc));
836 return rc;
837 }
838
839 bool fRegChanged = false;
840 for (uint32_t i = 0; i < cCompRects; ++i)
841 {
842 const RTRECT *pCompRect = &pCompRects[i];
843 for (uint32_t j = 0; j < cRects; ++j)
844 {
845 const RTRECT *pRect = &pRects[j];
846 if (VBoxRectIsIntersect(pCompRect, pRect))
847 {
848 *pfRegChanged = true;
849 return VINF_SUCCESS;
850 }
851 }
852 }
853
854 *pfRegChanged = false;
855 return VINF_SUCCESS;
856}
857
858int CrFbBltPutContentsNe(HCR_FRAMEBUFFER hFb, const RTPOINT *pPos, uint32_t cRects, const RTRECT *pRects, CR_BLITTER_IMG *pImg)
859{
860 bool fRegChanged = false;
861 int rc = crFbRegionsIsIntersectRects(hFb, cRects, pRects, &fRegChanged);
862 if (!RT_SUCCESS(rc))
863 {
864 WARN(("crFbRegionsIsIntersectRects failed rc %d", rc));
865 return rc;
866 }
867
868 if (fRegChanged)
869 {
870 rc = CrFbUpdateBegin(hFb);
871 if (RT_SUCCESS(rc))
872 {
873 rc = CrFbBltPutContents(hFb, pPos, cRects, pRects, pImg);
874 if (!RT_SUCCESS(rc))
875 WARN(("CrFbBltPutContents failed rc %d", rc));
876 CrFbUpdateEnd(hFb);
877 }
878 else
879 WARN(("CrFbUpdateBegin failed rc %d", rc));
880
881 return rc;
882 }
883
884 crFbBltPutContentsFbVram(hFb, pPos, cRects, pRects, pImg);
885 return VINF_SUCCESS;
886}
887
888int CrFbClrFillNe(HCR_FRAMEBUFFER hFb, uint32_t cRects, const RTRECT *pRects, uint32_t u32Color)
889{
890 bool fRegChanged = false;
891 int rc = crFbRegionsIsIntersectRects(hFb, cRects, pRects, &fRegChanged);
892 if (!RT_SUCCESS(rc))
893 {
894 WARN(("crFbRegionsIsIntersectRects failed rc %d", rc));
895 return rc;
896 }
897
898 if (fRegChanged)
899 {
900 rc = CrFbUpdateBegin(hFb);
901 if (RT_SUCCESS(rc))
902 {
903 rc = CrFbClrFill(hFb, cRects, pRects, u32Color);
904 if (!RT_SUCCESS(rc))
905 WARN(("CrFbClrFill failed rc %d", rc));
906 CrFbUpdateEnd(hFb);
907 }
908 else
909 WARN(("CrFbUpdateBegin failed rc %d", rc));
910
911 return rc;
912 }
913
914 crFbClrFillFbVram(hFb, cRects, pRects, u32Color);
915 return VINF_SUCCESS;
916}
917
918int CrFbResize(CR_FRAMEBUFFER *pFb, const struct VBVAINFOSCREEN * pScreen, void *pvVRAM)
919{
920 if (!pFb->cUpdating)
921 {
922 WARN(("no update in progress"));
923 return VERR_INVALID_STATE;
924 }
925
926 if (pScreen->u16Flags & VBVA_SCREEN_F_DISABLED)
927 {
928 CrVrScrCompositorClear(&pFb->Compositor);
929 }
930
931 RTRECT Rect;
932 Rect.xLeft = 0;
933 Rect.yTop = 0;
934 Rect.xRight = pScreen->u32Width;
935 Rect.yBottom = pScreen->u32Height;
936 int rc = CrVrScrCompositorRectSet(&pFb->Compositor, &Rect, NULL);
937 if (!RT_SUCCESS(rc))
938 {
939 WARN(("CrVrScrCompositorRectSet failed rc %d", rc));
940 return rc;
941 }
942
943 pFb->ScreenInfo = *pScreen;
944 pFb->pvVram = pvVRAM ? pvVRAM : g_pvVRamBase + pScreen->u32StartOffset;
945
946 if (pFb->pDisplay)
947 pFb->pDisplay->FramebufferChanged(pFb);
948
949 return VINF_SUCCESS;
950}
951
952void CrFbTerm(CR_FRAMEBUFFER *pFb)
953{
954 if (pFb->cUpdating)
955 {
956 WARN(("update in progress"));
957 return;
958 }
959 uint32_t idFb = pFb->ScreenInfo.u32ViewIndex;
960
961 CrVrScrCompositorClear(&pFb->Compositor);
962 CrHTableDestroy(&pFb->SlotTable);
963
964 Assert(RTListIsEmpty(&pFb->EntriesList));
965 Assert(!pFb->cEntries);
966
967 memset(pFb, 0, sizeof (*pFb));
968
969 pFb->ScreenInfo.u16Flags = VBVA_SCREEN_F_DISABLED;
970 pFb->ScreenInfo.u32ViewIndex = idFb;
971}
972
973ICrFbDisplay* CrFbDisplayGet(CR_FRAMEBUFFER *pFb)
974{
975 return pFb->pDisplay;
976}
977
978int CrFbDisplaySet(CR_FRAMEBUFFER *pFb, ICrFbDisplay *pDisplay)
979{
980 if (pFb->cUpdating)
981 {
982 WARN(("update in progress"));
983 return VERR_INVALID_STATE;
984 }
985
986 if (pFb->pDisplay == pDisplay)
987 return VINF_SUCCESS;
988
989 pFb->pDisplay = pDisplay;
990
991 return VINF_SUCCESS;
992}
993
994#define CR_PMGR_MODE_WINDOW 0x1
995/* mutually exclusive with CR_PMGR_MODE_WINDOW */
996#define CR_PMGR_MODE_ROOTVR 0x2
997#define CR_PMGR_MODE_VRDP 0x4
998#define CR_PMGR_MODE_ALL 0x7
999
1000static int crPMgrModeModifyGlobal(uint32_t u32ModeAdd, uint32_t u32ModeRemove);
1001static void crPMgrCleanUnusedDisplays();
1002
1003static CR_FBTEX* crFbTexAlloc()
1004{
1005#ifndef VBOXVDBG_MEMCACHE_DISABLE
1006 return (CR_FBTEX*)RTMemCacheAlloc(g_CrPresenter.FbTexLookasideList);
1007#else
1008 return (CR_FBTEX*)RTMemAlloc(sizeof (CR_FBTEX));
1009#endif
1010}
1011
1012static void crFbTexFree(CR_FBTEX *pTex)
1013{
1014#ifndef VBOXVDBG_MEMCACHE_DISABLE
1015 RTMemCacheFree(g_CrPresenter.FbTexLookasideList, pTex);
1016#else
1017 RTMemFree(pTex);
1018#endif
1019}
1020
1021static CR_FRAMEBUFFER_ENTRY* crFbEntryAlloc()
1022{
1023#ifndef VBOXVDBG_MEMCACHE_DISABLE
1024 return (CR_FRAMEBUFFER_ENTRY*)RTMemCacheAlloc(g_CrPresenter.FbEntryLookasideList);
1025#else
1026 return (CR_FRAMEBUFFER_ENTRY*)RTMemAlloc(sizeof (CR_FRAMEBUFFER_ENTRY));
1027#endif
1028}
1029
1030static void crFbEntryFree(CR_FRAMEBUFFER_ENTRY *pEntry)
1031{
1032 Assert(!CrVrScrCompositorEntryIsUsed(&pEntry->Entry));
1033#ifndef VBOXVDBG_MEMCACHE_DISABLE
1034 RTMemCacheFree(g_CrPresenter.FbEntryLookasideList, pEntry);
1035#else
1036 RTMemFree(pEntry);
1037#endif
1038}
1039
1040DECLCALLBACK(void) crFbTexRelease(CR_TEXDATA *pTex)
1041{
1042 CR_FBTEX *pFbTex = PCR_FBTEX_FROM_TEX(pTex);
1043 CRTextureObj *pTobj = pFbTex->pTobj;
1044
1045 CrTdBltDataCleanupNe(pTex);
1046
1047 if (pTobj)
1048 {
1049 CR_STATE_SHAREDOBJ_USAGE_CLEAR(pTobj, cr_server.MainContextInfo.pContext);
1050
1051 crHashtableDelete(g_CrPresenter.pFbTexMap, pTobj->id, NULL);
1052
1053 if (!CR_STATE_SHAREDOBJ_USAGE_IS_USED(pTobj))
1054 {
1055 CRSharedState *pShared = crStateGlobalSharedAcquire();
1056
1057 CRASSERT(pShared);
1058 /* on the host side, we need to delete an ogl texture object here as well, which crStateDeleteTextureCallback will do
1059 * in addition to calling crStateDeleteTextureObject to delete a state object */
1060 crHashtableDelete(pShared->textureTable, pTobj->id, crStateDeleteTextureCallback);
1061
1062 crStateGlobalSharedRelease();
1063 }
1064
1065 crStateGlobalSharedRelease();
1066 }
1067
1068 crFbTexFree(pFbTex);
1069}
1070
1071void CrFbTexDataInit(CR_TEXDATA* pFbTex, const VBOXVR_TEXTURE *pTex, PFNCRTEXDATA_RELEASED pfnTextureReleased)
1072{
1073 PCR_BLITTER pBlitter = crServerVBoxBlitterGet();
1074
1075 CrTdInit(pFbTex, pTex, pBlitter, pfnTextureReleased);
1076}
1077
1078static CR_FBTEX* crFbTexCreate(const VBOXVR_TEXTURE *pTex)
1079{
1080 CR_FBTEX *pFbTex = crFbTexAlloc();
1081 if (!pFbTex)
1082 {
1083 WARN(("crFbTexAlloc failed!"));
1084 return NULL;
1085 }
1086
1087 CrFbTexDataInit(&pFbTex->Tex, pTex, crFbTexRelease);
1088 pFbTex->pTobj = NULL;
1089
1090 return pFbTex;
1091}
1092
1093CR_TEXDATA* CrFbTexDataCreate(const VBOXVR_TEXTURE *pTex)
1094{
1095 CR_FBTEX *pFbTex = crFbTexCreate(pTex);
1096 if (!pFbTex)
1097 {
1098 WARN(("crFbTexCreate failed!"));
1099 return NULL;
1100 }
1101
1102 return &pFbTex->Tex;
1103}
1104
1105static CR_FBTEX* crFbTexAcquire(GLuint idTexture)
1106{
1107 CR_FBTEX *pFbTex = (CR_FBTEX *)crHashtableSearch(g_CrPresenter.pFbTexMap, idTexture);
1108 if (pFbTex)
1109 {
1110 CrTdAddRef(&pFbTex->Tex);
1111 return pFbTex;
1112 }
1113
1114 CRSharedState *pShared = crStateGlobalSharedAcquire();
1115 if (!pShared)
1116 {
1117 WARN(("pShared is null!"));
1118 return NULL;
1119 }
1120
1121 CRTextureObj *pTobj = (CRTextureObj*)crHashtableSearch(pShared->textureTable, idTexture);
1122 if (!pTobj)
1123 {
1124 LOG(("pTobj is null!"));
1125 crStateGlobalSharedRelease();
1126 return NULL;
1127 }
1128
1129 Assert(pTobj->id == idTexture);
1130
1131 GLuint hwid = crStateGetTextureObjHWID(pTobj);
1132 if (!hwid)
1133 {
1134 WARN(("hwId is null!"));
1135 crStateGlobalSharedRelease();
1136 return NULL;
1137 }
1138
1139 VBOXVR_TEXTURE Tex;
1140 Tex.width = pTobj->level[0]->width;
1141 Tex.height = pTobj->level[0]->height;
1142 Tex.hwid = hwid;
1143 Tex.target = pTobj->target;
1144
1145 pFbTex = crFbTexCreate(&Tex);
1146 if (!pFbTex)
1147 {
1148 WARN(("crFbTexCreate failed!"));
1149 crStateGlobalSharedRelease();
1150 return NULL;
1151 }
1152
1153 CR_STATE_SHAREDOBJ_USAGE_SET(pTobj, cr_server.MainContextInfo.pContext);
1154
1155 pFbTex->pTobj = pTobj;
1156
1157 crHashtableAdd(g_CrPresenter.pFbTexMap, idTexture, pFbTex);
1158
1159 return pFbTex;
1160}
1161
1162static CR_TEXDATA* CrFbTexDataAcquire(GLuint idTexture)
1163{
1164 CR_FBTEX* pTex = crFbTexAcquire(idTexture);
1165 if (!pTex)
1166 {
1167 WARN(("crFbTexAcquire failed for %d", idTexture));
1168 return NULL;
1169 }
1170
1171 return &pTex->Tex;
1172}
1173
1174static void crFbEntryMarkDestroyed(CR_FRAMEBUFFER *pFb, CR_FRAMEBUFFER_ENTRY* pEntry)
1175{
1176 if (pEntry->Flags.fCreateNotified)
1177 {
1178 pEntry->Flags.fCreateNotified = 0;
1179 if (pFb->pDisplay)
1180 pFb->pDisplay->EntryDestroyed(pFb, pEntry);
1181
1182 CR_TEXDATA *pTex = CrVrScrCompositorEntryTexGet(&pEntry->Entry);
1183 if (pTex)
1184 CrTdBltDataInvalidateNe(pTex);
1185 }
1186}
1187
1188static void crFbEntryDestroy(CR_FRAMEBUFFER *pFb, CR_FRAMEBUFFER_ENTRY* pEntry)
1189{
1190 crFbEntryMarkDestroyed(pFb, pEntry);
1191 CrVrScrCompositorEntryCleanup(&pEntry->Entry);
1192 CrHTableDestroy(&pEntry->HTable);
1193 Assert(pFb->cEntries);
1194 RTListNodeRemove(&pEntry->Node);
1195 --pFb->cEntries;
1196 crFbEntryFree(pEntry);
1197}
1198
1199DECLINLINE(uint32_t) crFbEntryAddRef(CR_FRAMEBUFFER_ENTRY* pEntry)
1200{
1201 return ++pEntry->cRefs;
1202}
1203
1204DECLINLINE(uint32_t) crFbEntryRelease(CR_FRAMEBUFFER *pFb, CR_FRAMEBUFFER_ENTRY* pEntry)
1205{
1206 uint32_t cRefs = --pEntry->cRefs;
1207 if (!cRefs)
1208 crFbEntryDestroy(pFb, pEntry);
1209 return cRefs;
1210}
1211
1212static DECLCALLBACK(void) crFbEntryReleased(const struct VBOXVR_SCR_COMPOSITOR *pCompositor, struct VBOXVR_SCR_COMPOSITOR_ENTRY *pEntry, struct VBOXVR_SCR_COMPOSITOR_ENTRY *pReplacingEntry)
1213{
1214 CR_FRAMEBUFFER *pFb = PCR_FRAMEBUFFER_FROM_COMPOSITOR(pCompositor);
1215 CR_FRAMEBUFFER_ENTRY *pFbEntry = PCR_FBENTRY_FROM_ENTRY(pEntry);
1216 CR_FRAMEBUFFER_ENTRY *pFbReplacingEntry = pReplacingEntry ? PCR_FBENTRY_FROM_ENTRY(pReplacingEntry) : NULL;
1217 if (pFbReplacingEntry)
1218 {
1219 /*replace operation implies the replaced entry gets auto-destroyed,
1220 * while all its data gets moved to the *clean* replacing entry
1221 * 1. ensure the replacing entry is cleaned up */
1222 crFbEntryMarkDestroyed(pFb, pFbReplacingEntry);
1223
1224 CrHTableMoveTo(&pFbEntry->HTable, &pFbReplacingEntry->HTable);
1225
1226 CR_TEXDATA *pTex = CrVrScrCompositorEntryTexGet(&pFbEntry->Entry);
1227 CR_TEXDATA *pReplacingTex = CrVrScrCompositorEntryTexGet(&pFbReplacingEntry->Entry);
1228
1229 CrTdBltScaleCacheMoveTo(pTex, pReplacingTex);
1230
1231 if (pFb->pDisplay)
1232 pFb->pDisplay->EntryReplaced(pFb, pFbReplacingEntry, pFbEntry);
1233
1234 CrTdBltDataInvalidateNe(pTex);
1235
1236 /* 2. mark the replaced entry is destroyed */
1237 Assert(pFbEntry->Flags.fCreateNotified);
1238 Assert(pFbEntry->Flags.fInList);
1239 pFbEntry->Flags.fCreateNotified = 0;
1240 pFbEntry->Flags.fInList = 0;
1241 pFbReplacingEntry->Flags.fCreateNotified = 1;
1242 pFbReplacingEntry->Flags.fInList = 1;
1243 }
1244 else
1245 {
1246 if (pFbEntry->Flags.fInList)
1247 {
1248 pFbEntry->Flags.fInList = 0;
1249 if (pFb->pDisplay)
1250 pFb->pDisplay->EntryRemoved(pFb, pFbEntry);
1251
1252 CR_TEXDATA *pTex = CrVrScrCompositorEntryTexGet(&pFbEntry->Entry);
1253 if (pTex)
1254 CrTdBltDataInvalidateNe(pTex);
1255 }
1256 }
1257
1258 crFbEntryRelease(pFb, pFbEntry);
1259}
1260
1261static CR_FRAMEBUFFER_ENTRY* crFbEntryCreate(CR_FRAMEBUFFER *pFb, CR_TEXDATA* pTex, const RTRECT *pRect, uint32_t fFlags)
1262{
1263 CR_FRAMEBUFFER_ENTRY *pEntry = crFbEntryAlloc();
1264 if (!pEntry)
1265 {
1266 WARN(("crFbEntryAlloc failed!"));
1267 return NULL;
1268 }
1269
1270 CrVrScrCompositorEntryInit(&pEntry->Entry, pRect, pTex, crFbEntryReleased);
1271 CrVrScrCompositorEntryFlagsSet(&pEntry->Entry, fFlags);
1272 pEntry->cRefs = 1;
1273 pEntry->Flags.Value = 0;
1274 CrHTableCreate(&pEntry->HTable, 0);
1275
1276 RTListAppend(&pFb->EntriesList, &pEntry->Node);
1277 ++pFb->cEntries;
1278
1279 return pEntry;
1280}
1281
1282int CrFbEntryCreateForTexData(CR_FRAMEBUFFER *pFb, struct CR_TEXDATA *pTex, uint32_t fFlags, HCR_FRAMEBUFFER_ENTRY *phEntry)
1283{
1284 RTRECT Rect;
1285 Rect.xLeft = 0;
1286 Rect.yTop = 0;
1287 Rect.xRight = pTex->Tex.width;
1288 Rect.yBottom = pTex->Tex.height;
1289 CR_FRAMEBUFFER_ENTRY* pEntry = crFbEntryCreate(pFb, pTex, &Rect, fFlags);
1290 if (!pEntry)
1291 {
1292 WARN(("crFbEntryCreate failed"));
1293 return VERR_NO_MEMORY;
1294 }
1295
1296 *phEntry = pEntry;
1297 return VINF_SUCCESS;
1298}
1299
1300int CrFbEntryTexDataUpdate(CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY pEntry, struct CR_TEXDATA *pTex)
1301{
1302 if (!pFb->cUpdating)
1303 {
1304 WARN(("framebuffer not updating"));
1305 return VERR_INVALID_STATE;
1306 }
1307
1308 if (pTex)
1309 CrVrScrCompositorEntryTexSet(&pEntry->Entry, pTex);
1310
1311 if (CrVrScrCompositorEntryIsUsed(&pEntry->Entry))
1312 {
1313 if (pFb->pDisplay)
1314 pFb->pDisplay->EntryTexChanged(pFb, pEntry);
1315
1316 CR_TEXDATA *pTex = CrVrScrCompositorEntryTexGet(&pEntry->Entry);
1317 if (pTex)
1318 CrTdBltDataInvalidateNe(pTex);
1319 }
1320
1321 return VINF_SUCCESS;
1322}
1323
1324
1325int CrFbEntryCreateForTexId(CR_FRAMEBUFFER *pFb, GLuint idTexture, uint32_t fFlags, HCR_FRAMEBUFFER_ENTRY *phEntry)
1326{
1327 CR_FBTEX* pFbTex = crFbTexAcquire(idTexture);
1328 if (!pFbTex)
1329 {
1330 LOG(("crFbTexAcquire failed"));
1331 return VERR_INVALID_PARAMETER;
1332 }
1333
1334 CR_TEXDATA* pTex = &pFbTex->Tex;
1335 int rc = CrFbEntryCreateForTexData(pFb, pTex, fFlags, phEntry);
1336 if (!RT_SUCCESS(rc))
1337 {
1338 WARN(("CrFbEntryCreateForTexData failed rc %d", rc));
1339 }
1340
1341 /*always release the tex, the CrFbEntryCreateForTexData will do incref as necessary */
1342 CrTdRelease(pTex);
1343 return rc;
1344}
1345
1346void CrFbEntryAddRef(CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry)
1347{
1348 ++hEntry->cRefs;
1349}
1350
1351void CrFbEntryRelease(CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry)
1352{
1353 crFbEntryRelease(pFb, hEntry);
1354}
1355
1356static int8_t crVBoxServerCrCmdBltPrimaryVramGenericProcess(uint32_t u32PrimaryID, VBOXCMDVBVAOFFSET offVRAM, uint32_t width, uint32_t height, const RTPOINT *pPos, uint32_t cRects, const RTRECT *pRects, bool fToPrimary);
1357
1358int CrFbRegionsClear(HCR_FRAMEBUFFER hFb)
1359{
1360 if (!hFb->cUpdating)
1361 {
1362 WARN(("framebuffer not updating"));
1363 return VERR_INVALID_STATE;
1364 }
1365
1366 uint32_t cRegions;
1367 const RTRECT *pRegions;
1368 int rc = CrVrScrCompositorRegionsGet(&hFb->Compositor, &cRegions, NULL, NULL, &pRegions);
1369 if (!RT_SUCCESS(rc))
1370 {
1371 WARN(("CrVrScrCompositorEntryRegionsGet failed rc %d", rc));
1372 return rc;
1373 }
1374
1375 const struct VBVAINFOSCREEN* pScreen = CrFbGetScreenInfo(hFb);
1376 VBOXCMDVBVAOFFSET offVRAM = (VBOXCMDVBVAOFFSET)(((uintptr_t)CrFbGetVRAM(hFb)) - ((uintptr_t)g_pvVRamBase));
1377 RTPOINT Pos = {0,0};
1378 int8_t i8Result = crVBoxServerCrCmdBltPrimaryVramGenericProcess(pScreen->u32ViewIndex, offVRAM, pScreen->u32Width, pScreen->u32Height, &Pos, cRegions, pRegions, true);
1379 if (i8Result)
1380 {
1381 WARN(("crVBoxServerCrCmdBltPrimaryVramGenericProcess failed"));
1382 return VERR_INTERNAL_ERROR;
1383 }
1384
1385#ifdef DEBUG
1386 {
1387 uint32_t cTmpRegions;
1388 const RTRECT *pTmpRegions;
1389 int tmpRc = CrVrScrCompositorRegionsGet(&hFb->Compositor, &cTmpRegions, NULL, NULL, &pTmpRegions);
1390 if (!RT_SUCCESS(tmpRc))
1391 {
1392 WARN(("CrVrScrCompositorEntryRegionsGet failed rc %d", tmpRc));
1393 }
1394 Assert(!cTmpRegions);
1395 }
1396#endif
1397
1398 /* just in case */
1399 bool fChanged = false;
1400 CrVrScrCompositorRegionsClear(&hFb->Compositor, &fChanged);
1401 Assert(!fChanged);
1402
1403 if (cRegions)
1404 {
1405 if (hFb->pDisplay)
1406 hFb->pDisplay->RegionsChanged(hFb);
1407 }
1408
1409 return VINF_SUCCESS;
1410}
1411
1412int CrFbEntryRegionsAdd(CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry, const RTPOINT *pPos, uint32_t cRegions, const RTRECT *paRegions, bool fPosRelated)
1413{
1414 if (!pFb->cUpdating)
1415 {
1416 WARN(("framebuffer not updating"));
1417 return VERR_INVALID_STATE;
1418 }
1419
1420 uint32_t fChangeFlags = 0;
1421 VBOXVR_SCR_COMPOSITOR_ENTRY *pReplacedScrEntry = NULL;
1422 VBOXVR_SCR_COMPOSITOR_ENTRY *pNewEntry;
1423 bool fEntryWasInList;
1424
1425 if (hEntry)
1426 {
1427 crFbEntryAddRef(hEntry);
1428 pNewEntry = &hEntry->Entry;
1429 fEntryWasInList = CrVrScrCompositorEntryIsUsed(pNewEntry);
1430
1431 Assert(!hEntry->Flags.fInList == !fEntryWasInList);
1432 }
1433 else
1434 {
1435 pNewEntry = NULL;
1436 fEntryWasInList = false;
1437 }
1438
1439 int rc = CrVrScrCompositorEntryRegionsAdd(&pFb->Compositor, hEntry ? &hEntry->Entry : NULL, pPos, cRegions, paRegions, fPosRelated, &pReplacedScrEntry, &fChangeFlags);
1440 if (RT_SUCCESS(rc))
1441 {
1442 if (fChangeFlags & VBOXVR_COMPOSITOR_CF_REGIONS_CHANGED)
1443 {
1444 if (!fEntryWasInList && pNewEntry)
1445 {
1446 Assert(CrVrScrCompositorEntryIsUsed(pNewEntry));
1447 if (!hEntry->Flags.fCreateNotified)
1448 {
1449 hEntry->Flags.fCreateNotified = 1;
1450 if (pFb->pDisplay)
1451 pFb->pDisplay->EntryCreated(pFb, hEntry);
1452 }
1453
1454#ifdef DEBUG_misha
1455 /* in theory hEntry->Flags.fInList can be set if entry is replaced,
1456 * but then modified to fit the compositor rects,
1457 * and so we get the regions changed notification as a result
1458 * this should not generally happen though, so put an assertion to debug that situation */
1459 Assert(!hEntry->Flags.fInList);
1460#endif
1461 if (!hEntry->Flags.fInList)
1462 {
1463 hEntry->Flags.fInList = 1;
1464
1465 if (pFb->pDisplay)
1466 pFb->pDisplay->EntryAdded(pFb, hEntry);
1467 }
1468 }
1469 if (pFb->pDisplay)
1470 pFb->pDisplay->RegionsChanged(pFb);
1471
1472 Assert(!pReplacedScrEntry);
1473 }
1474 else if (fChangeFlags & VBOXVR_COMPOSITOR_CF_ENTRY_REPLACED)
1475 {
1476 Assert(pReplacedScrEntry);
1477 /* we have already processed that in a "release" callback */
1478 Assert(hEntry);
1479 }
1480 else
1481 {
1482 Assert(!fChangeFlags);
1483 Assert(!pReplacedScrEntry);
1484 }
1485
1486 if (hEntry)
1487 {
1488 if (CrVrScrCompositorEntryIsUsed(&hEntry->Entry))
1489 {
1490 if (pFb->pDisplay)
1491 pFb->pDisplay->EntryTexChanged(pFb, hEntry);
1492
1493 CR_TEXDATA *pTex = CrVrScrCompositorEntryTexGet(&hEntry->Entry);
1494 if (pTex)
1495 CrTdBltDataInvalidateNe(pTex);
1496 }
1497 }
1498 }
1499 else
1500 WARN(("CrVrScrCompositorEntryRegionsAdd failed, rc %d", rc));
1501
1502 return rc;
1503}
1504
1505int CrFbEntryRegionsSet(CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry, const RTPOINT *pPos, uint32_t cRegions, const RTRECT *paRegions, bool fPosRelated)
1506{
1507 if (!pFb->cUpdating)
1508 {
1509 WARN(("framebuffer not updating"));
1510 return VERR_INVALID_STATE;
1511 }
1512
1513 bool fChanged = 0;
1514 VBOXVR_SCR_COMPOSITOR_ENTRY *pReplacedScrEntry = NULL;
1515 VBOXVR_SCR_COMPOSITOR_ENTRY *pNewEntry;
1516 bool fEntryWasInList;
1517
1518 if (hEntry)
1519 {
1520 crFbEntryAddRef(hEntry);
1521 pNewEntry = &hEntry->Entry;
1522 fEntryWasInList = CrVrScrCompositorEntryIsUsed(pNewEntry);
1523 Assert(!hEntry->Flags.fInList == !fEntryWasInList);
1524 }
1525 else
1526 {
1527 pNewEntry = NULL;
1528 fEntryWasInList = false;
1529 }
1530
1531 int rc = CrVrScrCompositorEntryRegionsSet(&pFb->Compositor, pNewEntry, pPos, cRegions, paRegions, fPosRelated, &fChanged);
1532 if (RT_SUCCESS(rc))
1533 {
1534 if (fChanged)
1535 {
1536 if (!fEntryWasInList && pNewEntry)
1537 {
1538 if (CrVrScrCompositorEntryIsUsed(pNewEntry))
1539 {
1540 if (!hEntry->Flags.fCreateNotified)
1541 {
1542 hEntry->Flags.fCreateNotified = 1;
1543
1544 if (pFb->pDisplay)
1545 pFb->pDisplay->EntryCreated(pFb, hEntry);
1546 }
1547
1548 Assert(!hEntry->Flags.fInList);
1549 hEntry->Flags.fInList = 1;
1550
1551 if (pFb->pDisplay)
1552 pFb->pDisplay->EntryAdded(pFb, hEntry);
1553 }
1554 }
1555
1556 if (pFb->pDisplay)
1557 pFb->pDisplay->RegionsChanged(pFb);
1558 }
1559
1560 if (hEntry)
1561 {
1562 if (CrVrScrCompositorEntryIsUsed(&hEntry->Entry))
1563 {
1564 if (pFb->pDisplay)
1565 pFb->pDisplay->EntryTexChanged(pFb, hEntry);
1566
1567 CR_TEXDATA *pTex = CrVrScrCompositorEntryTexGet(&hEntry->Entry);
1568 if (pTex)
1569 CrTdBltDataInvalidateNe(pTex);
1570 }
1571 }
1572 }
1573 else
1574 WARN(("CrVrScrCompositorEntryRegionsSet failed, rc %d", rc));
1575
1576 return rc;
1577}
1578
1579const struct VBOXVR_SCR_COMPOSITOR_ENTRY* CrFbEntryGetCompositorEntry(HCR_FRAMEBUFFER_ENTRY hEntry)
1580{
1581 return &hEntry->Entry;
1582}
1583
1584HCR_FRAMEBUFFER_ENTRY CrFbEntryFromCompositorEntry(const struct VBOXVR_SCR_COMPOSITOR_ENTRY* pCEntry)
1585{
1586 return RT_FROM_MEMBER(pCEntry, CR_FRAMEBUFFER_ENTRY, Entry);
1587}
1588
1589void CrFbVisitCreatedEntries(HCR_FRAMEBUFFER hFb, PFNCR_FRAMEBUFFER_ENTRIES_VISITOR_CB pfnVisitorCb, void *pvContext)
1590{
1591 HCR_FRAMEBUFFER_ENTRY hEntry, hNext;
1592 RTListForEachSafe(&hFb->EntriesList, hEntry, hNext, CR_FRAMEBUFFER_ENTRY, Node)
1593 {
1594 if (hEntry->Flags.fCreateNotified)
1595 {
1596 if (!pfnVisitorCb(hFb, hEntry, pvContext))
1597 return;
1598 }
1599 }
1600}
1601
1602
1603CRHTABLE_HANDLE CrFbDDataAllocSlot(CR_FRAMEBUFFER *pFb)
1604{
1605 return CrHTablePut(&pFb->SlotTable, (void*)1);
1606}
1607
1608void CrFbDDataReleaseSlot(CR_FRAMEBUFFER *pFb, CRHTABLE_HANDLE hSlot, PFNCR_FRAMEBUFFER_SLOT_RELEASE_CB pfnReleaseCb, void *pvContext)
1609{
1610 HCR_FRAMEBUFFER_ENTRY hEntry, hNext;
1611 RTListForEachSafe(&pFb->EntriesList, hEntry, hNext, CR_FRAMEBUFFER_ENTRY, Node)
1612 {
1613 if (CrFbDDataEntryGet(hEntry, hSlot))
1614 {
1615 if (pfnReleaseCb)
1616 pfnReleaseCb(pFb, hEntry, pvContext);
1617
1618 CrFbDDataEntryClear(hEntry, hSlot);
1619 }
1620 }
1621
1622 CrHTableRemove(&pFb->SlotTable, hSlot);
1623}
1624
1625int CrFbDDataEntryPut(HCR_FRAMEBUFFER_ENTRY hEntry, CRHTABLE_HANDLE hSlot, void *pvData)
1626{
1627 return CrHTablePutToSlot(&hEntry->HTable, hSlot, pvData);
1628}
1629
1630void* CrFbDDataEntryClear(HCR_FRAMEBUFFER_ENTRY hEntry, CRHTABLE_HANDLE hSlot)
1631{
1632 return CrHTableRemove(&hEntry->HTable, hSlot);
1633}
1634
1635void* CrFbDDataEntryGet(HCR_FRAMEBUFFER_ENTRY hEntry, CRHTABLE_HANDLE hSlot)
1636{
1637 return CrHTableGet(&hEntry->HTable, hSlot);
1638}
1639
1640typedef union CR_FBDISPBASE_FLAGS
1641{
1642 struct {
1643 uint32_t fRegionsShanged : 1;
1644 uint32_t Reserved : 31;
1645 };
1646 uint32_t u32Value;
1647} CR_FBDISPBASE_FLAGS;
1648
1649class CrFbDisplayBase : public ICrFbDisplay
1650{
1651public:
1652 CrFbDisplayBase() :
1653 mpContainer(NULL),
1654 mpFb(NULL),
1655 mcUpdates(0),
1656 mhSlot(CRHTABLE_HANDLE_INVALID)
1657 {
1658 mFlags.u32Value = 0;
1659 }
1660
1661 virtual bool isComposite()
1662 {
1663 return false;
1664 }
1665
1666 class CrFbDisplayComposite* getContainer()
1667 {
1668 return mpContainer;
1669 }
1670
1671 bool isInList()
1672 {
1673 return !!mpContainer;
1674 }
1675
1676 bool isUpdating()
1677 {
1678 return !!mcUpdates;
1679 }
1680
1681 int setRegionsChanged()
1682 {
1683 if (!mcUpdates)
1684 {
1685 WARN(("err"));
1686 return VERR_INVALID_STATE;
1687 }
1688
1689 mFlags.fRegionsShanged = 1;
1690 return VINF_SUCCESS;
1691 }
1692
1693 int setFramebuffer(struct CR_FRAMEBUFFER *pFb)
1694 {
1695 if (mcUpdates)
1696 {
1697 WARN(("trying to set framebuffer while update is in progress"));
1698 return VERR_INVALID_STATE;
1699 }
1700
1701 if (mpFb == pFb)
1702 return VINF_SUCCESS;
1703
1704 int rc = setFramebufferBegin(pFb);
1705 if (!RT_SUCCESS(rc))
1706 {
1707 WARN(("err"));
1708 return rc;
1709 }
1710
1711 if (mpFb)
1712 {
1713 rc = fbCleanup();
1714 if (!RT_SUCCESS(rc))
1715 {
1716 WARN(("err"));
1717 setFramebufferEnd(pFb);
1718 return rc;
1719 }
1720 }
1721
1722 mpFb = pFb;
1723
1724 if (mpFb)
1725 {
1726 rc = fbSync();
1727 if (!RT_SUCCESS(rc))
1728 {
1729 WARN(("err"));
1730 setFramebufferEnd(pFb);
1731 return rc;
1732 }
1733 }
1734
1735 setFramebufferEnd(pFb);
1736 return VINF_SUCCESS;
1737 }
1738
1739 struct CR_FRAMEBUFFER* getFramebuffer()
1740 {
1741 return mpFb;
1742 }
1743
1744 virtual int UpdateBegin(struct CR_FRAMEBUFFER *pFb)
1745 {
1746 ++mcUpdates;
1747 Assert(!mFlags.fRegionsShanged || mcUpdates > 1);
1748 return VINF_SUCCESS;
1749 }
1750
1751 virtual void UpdateEnd(struct CR_FRAMEBUFFER *pFb)
1752 {
1753 --mcUpdates;
1754 Assert(mcUpdates < UINT32_MAX/2);
1755 if (!mcUpdates)
1756 onUpdateEnd();
1757 }
1758
1759 virtual int EntryCreated(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry)
1760 {
1761 if (!mcUpdates)
1762 {
1763 WARN(("err"));
1764 return VERR_INVALID_STATE;
1765 }
1766 return VINF_SUCCESS;
1767 }
1768
1769 virtual int EntryAdded(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry)
1770 {
1771 if (!mcUpdates)
1772 {
1773 WARN(("err"));
1774 return VERR_INVALID_STATE;
1775 }
1776 mFlags.fRegionsShanged = 1;
1777 return VINF_SUCCESS;
1778 }
1779
1780 virtual int EntryReplaced(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hNewEntry, HCR_FRAMEBUFFER_ENTRY hReplacedEntry)
1781 {
1782 if (!mcUpdates)
1783 {
1784 WARN(("err"));
1785 return VERR_INVALID_STATE;
1786 }
1787 return VINF_SUCCESS;
1788 }
1789
1790 virtual int EntryTexChanged(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry)
1791 {
1792 if (!mcUpdates)
1793 {
1794 WARN(("err"));
1795 return VERR_INVALID_STATE;
1796 }
1797 return VINF_SUCCESS;
1798 }
1799
1800 virtual int EntryRemoved(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry)
1801 {
1802 if (!mcUpdates)
1803 {
1804 WARN(("err"));
1805 return VERR_INVALID_STATE;
1806 }
1807 mFlags.fRegionsShanged = 1;
1808 return VINF_SUCCESS;
1809 }
1810
1811 virtual int EntryDestroyed(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry)
1812 {
1813 return VINF_SUCCESS;
1814 }
1815
1816 virtual int EntryPosChanged(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry)
1817 {
1818 if (!mcUpdates)
1819 {
1820 WARN(("err"));
1821 return VERR_INVALID_STATE;
1822 }
1823 mFlags.fRegionsShanged = 1;
1824 return VINF_SUCCESS;
1825 }
1826
1827 virtual int RegionsChanged(struct CR_FRAMEBUFFER *pFb)
1828 {
1829 if (!mcUpdates)
1830 {
1831 WARN(("err"));
1832 return VERR_INVALID_STATE;
1833 }
1834 mFlags.fRegionsShanged = 1;
1835 return VINF_SUCCESS;
1836 }
1837
1838 virtual int FramebufferChanged(struct CR_FRAMEBUFFER *pFb)
1839 {
1840 if (!mcUpdates)
1841 {
1842 WARN(("err"));
1843 return VERR_INVALID_STATE;
1844 }
1845 return VINF_SUCCESS;
1846 }
1847
1848 virtual ~CrFbDisplayBase();
1849
1850 /*@todo: move to protected and switch from RTLISTNODE*/
1851 RTLISTNODE mNode;
1852 class CrFbDisplayComposite* mpContainer;
1853protected:
1854 virtual void onUpdateEnd()
1855 {
1856 if (mFlags.fRegionsShanged)
1857 {
1858 mFlags.fRegionsShanged = 0;
1859 if (getFramebuffer()) /*<-dont't do anything on cleanup*/
1860 ueRegions();
1861 }
1862 }
1863
1864 virtual void ueRegions()
1865 {
1866 }
1867
1868 static DECLCALLBACK(bool) entriesCreateCb(HCR_FRAMEBUFFER hFb, HCR_FRAMEBUFFER_ENTRY hEntry, void *pvContext)
1869 {
1870 int rc = ((ICrFbDisplay*)(pvContext))->EntryCreated(hFb, hEntry);
1871 if (!RT_SUCCESS(rc))
1872 {
1873 WARN(("err"));
1874 }
1875 return true;
1876 }
1877
1878 static DECLCALLBACK(bool) entriesDestroyCb(HCR_FRAMEBUFFER hFb, HCR_FRAMEBUFFER_ENTRY hEntry, void *pvContext)
1879 {
1880 int rc = ((ICrFbDisplay*)(pvContext))->EntryDestroyed(hFb, hEntry);
1881 if (!RT_SUCCESS(rc))
1882 {
1883 WARN(("err"));
1884 }
1885 return true;
1886 }
1887
1888 int fbSynchAddAllEntries()
1889 {
1890 VBOXVR_SCR_COMPOSITOR_CONST_ITERATOR Iter;
1891 const VBOXVR_SCR_COMPOSITOR_ENTRY *pEntry;
1892
1893 CrVrScrCompositorConstIterInit(CrFbGetCompositor(mpFb), &Iter);
1894
1895 int rc = VINF_SUCCESS;
1896
1897 CrFbVisitCreatedEntries(mpFb, entriesCreateCb, this);
1898
1899 while ((pEntry = CrVrScrCompositorConstIterNext(&Iter)) != NULL)
1900 {
1901 HCR_FRAMEBUFFER_ENTRY hEntry = CrFbEntryFromCompositorEntry(pEntry);
1902
1903 rc = EntryAdded(mpFb, hEntry);
1904 if (!RT_SUCCESS(rc))
1905 {
1906 WARN(("err"));
1907 EntryDestroyed(mpFb, hEntry);
1908 break;
1909 }
1910 }
1911
1912 return rc;
1913 }
1914
1915 int fbCleanupRemoveAllEntries()
1916 {
1917 VBOXVR_SCR_COMPOSITOR_CONST_ITERATOR Iter;
1918 const VBOXVR_SCR_COMPOSITOR_ENTRY *pEntry;
1919
1920 CrVrScrCompositorConstIterInit(CrFbGetCompositor(mpFb), &Iter);
1921
1922 int rc = VINF_SUCCESS;
1923
1924 while ((pEntry = CrVrScrCompositorConstIterNext(&Iter)) != NULL)
1925 {
1926 HCR_FRAMEBUFFER_ENTRY hEntry = CrFbEntryFromCompositorEntry(pEntry);
1927 rc = EntryRemoved(mpFb, hEntry);
1928 if (!RT_SUCCESS(rc))
1929 {
1930 WARN(("err"));
1931 break;
1932 }
1933
1934 CrFbVisitCreatedEntries(mpFb, entriesDestroyCb, this);
1935 }
1936
1937 return rc;
1938 }
1939
1940 virtual int setFramebufferBegin(struct CR_FRAMEBUFFER *pFb)
1941 {
1942 return UpdateBegin(pFb);
1943 }
1944 virtual void setFramebufferEnd(struct CR_FRAMEBUFFER *pFb)
1945 {
1946 UpdateEnd(pFb);
1947 }
1948
1949 static DECLCALLBACK(void) slotEntryReleaseCB(HCR_FRAMEBUFFER hFb, HCR_FRAMEBUFFER_ENTRY hEntry, void *pvContext)
1950 {
1951 }
1952
1953 virtual void slotRelease()
1954 {
1955 Assert(mhSlot);
1956 CrFbDDataReleaseSlot(mpFb, mhSlot, slotEntryReleaseCB, this);
1957 }
1958
1959 virtual int fbCleanup()
1960 {
1961 if (mhSlot)
1962 {
1963 slotRelease();
1964 mhSlot = 0;
1965 }
1966 mpFb = NULL;
1967 return VINF_SUCCESS;
1968 }
1969
1970 virtual int fbSync()
1971 {
1972 return VINF_SUCCESS;
1973 }
1974
1975 CRHTABLE_HANDLE slotGet()
1976 {
1977 if (!mhSlot)
1978 {
1979 if (mpFb)
1980 mhSlot = CrFbDDataAllocSlot(mpFb);
1981 }
1982
1983 return mhSlot;
1984 }
1985
1986private:
1987 struct CR_FRAMEBUFFER *mpFb;
1988 uint32_t mcUpdates;
1989 CRHTABLE_HANDLE mhSlot;
1990 CR_FBDISPBASE_FLAGS mFlags;
1991};
1992
1993class CrFbDisplayComposite : public CrFbDisplayBase
1994{
1995public:
1996 CrFbDisplayComposite() :
1997 mcDisplays(0)
1998 {
1999 RTListInit(&mDisplays);
2000 }
2001
2002 virtual bool isComposite()
2003 {
2004 return true;
2005 }
2006
2007 uint32_t getDisplayCount()
2008 {
2009 return mcDisplays;
2010 }
2011
2012 bool add(CrFbDisplayBase *pDisplay)
2013 {
2014 if (pDisplay->isInList())
2015 {
2016 WARN(("entry in list already"));
2017 return false;
2018 }
2019
2020 RTListAppend(&mDisplays, &pDisplay->mNode);
2021 pDisplay->mpContainer = this;
2022 pDisplay->setFramebuffer(getFramebuffer());
2023 ++mcDisplays;
2024 return true;
2025 }
2026
2027 bool remove(CrFbDisplayBase *pDisplay, bool fCleanupDisplay = true)
2028 {
2029 if (pDisplay->getContainer() != this)
2030 {
2031 WARN(("invalid entry container"));
2032 return false;
2033 }
2034
2035 RTListNodeRemove(&pDisplay->mNode);
2036 pDisplay->mpContainer = NULL;
2037 if (fCleanupDisplay)
2038 pDisplay->setFramebuffer(NULL);
2039 --mcDisplays;
2040 return true;
2041 }
2042
2043 CrFbDisplayBase* first()
2044 {
2045 return RTListGetFirstCpp(&mDisplays, CrFbDisplayBase, mNode);
2046 }
2047
2048 CrFbDisplayBase* next(CrFbDisplayBase* pDisplay)
2049 {
2050 if (pDisplay->getContainer() != this)
2051 {
2052 WARN(("invalid entry container"));
2053 return NULL;
2054 }
2055
2056 return RTListGetNextCpp(&mDisplays, pDisplay, CrFbDisplayBase, mNode);
2057 }
2058
2059 virtual int setFramebuffer(struct CR_FRAMEBUFFER *pFb)
2060 {
2061 CrFbDisplayBase::setFramebuffer(pFb);
2062
2063 CrFbDisplayBase *pIter;
2064 RTListForEachCpp(&mDisplays, pIter, CrFbDisplayBase, mNode)
2065 {
2066 pIter->setFramebuffer(pFb);
2067 }
2068
2069 return VINF_SUCCESS;
2070 }
2071
2072 virtual int UpdateBegin(struct CR_FRAMEBUFFER *pFb)
2073 {
2074 int rc = CrFbDisplayBase::UpdateBegin(pFb);
2075 if (!RT_SUCCESS(rc))
2076 {
2077 WARN(("err"));
2078 return rc;
2079 }
2080
2081 CrFbDisplayBase *pIter;
2082 RTListForEachCpp(&mDisplays, pIter, CrFbDisplayBase, mNode)
2083 {
2084 rc = pIter->UpdateBegin(pFb);
2085 if (!RT_SUCCESS(rc))
2086 {
2087 WARN(("err"));
2088 return rc;
2089 }
2090 }
2091 return VINF_SUCCESS;
2092 }
2093
2094 virtual void UpdateEnd(struct CR_FRAMEBUFFER *pFb)
2095 {
2096 CrFbDisplayBase *pIter;
2097 RTListForEachCpp(&mDisplays, pIter, CrFbDisplayBase, mNode)
2098 {
2099 pIter->UpdateEnd(pFb);
2100 }
2101
2102 CrFbDisplayBase::UpdateEnd(pFb);
2103 }
2104
2105 virtual int EntryAdded(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry)
2106 {
2107 int rc = CrFbDisplayBase::EntryAdded(pFb, hEntry);
2108 if (!RT_SUCCESS(rc))
2109 {
2110 WARN(("err"));
2111 return rc;
2112 }
2113
2114 CrFbDisplayBase *pIter;
2115 RTListForEachCpp(&mDisplays, pIter, CrFbDisplayBase, mNode)
2116 {
2117 int rc = pIter->EntryAdded(pFb, hEntry);
2118 if (!RT_SUCCESS(rc))
2119 {
2120 WARN(("err"));
2121 return rc;
2122 }
2123 }
2124 return VINF_SUCCESS;
2125 }
2126
2127 virtual int EntryCreated(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry)
2128 {
2129 int rc = CrFbDisplayBase::EntryAdded(pFb, hEntry);
2130 if (!RT_SUCCESS(rc))
2131 {
2132 WARN(("err"));
2133 return rc;
2134 }
2135
2136 CrFbDisplayBase *pIter;
2137 RTListForEachCpp(&mDisplays, pIter, CrFbDisplayBase, mNode)
2138 {
2139 int rc = pIter->EntryCreated(pFb, hEntry);
2140 if (!RT_SUCCESS(rc))
2141 {
2142 WARN(("err"));
2143 return rc;
2144 }
2145 }
2146 return VINF_SUCCESS;
2147 }
2148
2149 virtual int EntryReplaced(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hNewEntry, HCR_FRAMEBUFFER_ENTRY hReplacedEntry)
2150 {
2151 int rc = CrFbDisplayBase::EntryReplaced(pFb, hNewEntry, hReplacedEntry);
2152 if (!RT_SUCCESS(rc))
2153 {
2154 WARN(("err"));
2155 return rc;
2156 }
2157
2158 CrFbDisplayBase *pIter;
2159 RTListForEachCpp(&mDisplays, pIter, CrFbDisplayBase, mNode)
2160 {
2161 int rc = pIter->EntryReplaced(pFb, hNewEntry, hReplacedEntry);
2162 if (!RT_SUCCESS(rc))
2163 {
2164 WARN(("err"));
2165 return rc;
2166 }
2167 }
2168 return VINF_SUCCESS;
2169 }
2170
2171 virtual int EntryTexChanged(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry)
2172 {
2173 int rc = CrFbDisplayBase::EntryTexChanged(pFb, hEntry);
2174 if (!RT_SUCCESS(rc))
2175 {
2176 WARN(("err"));
2177 return rc;
2178 }
2179
2180 CrFbDisplayBase *pIter;
2181 RTListForEachCpp(&mDisplays, pIter, CrFbDisplayBase, mNode)
2182 {
2183 int rc = pIter->EntryTexChanged(pFb, hEntry);
2184 if (!RT_SUCCESS(rc))
2185 {
2186 WARN(("err"));
2187 return rc;
2188 }
2189 }
2190 return VINF_SUCCESS;
2191 }
2192
2193 virtual int EntryRemoved(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry)
2194 {
2195 int rc = CrFbDisplayBase::EntryRemoved(pFb, hEntry);
2196 if (!RT_SUCCESS(rc))
2197 {
2198 WARN(("err"));
2199 return rc;
2200 }
2201
2202 CrFbDisplayBase *pIter;
2203 RTListForEachCpp(&mDisplays, pIter, CrFbDisplayBase, mNode)
2204 {
2205 int rc = pIter->EntryRemoved(pFb, hEntry);
2206 if (!RT_SUCCESS(rc))
2207 {
2208 WARN(("err"));
2209 return rc;
2210 }
2211 }
2212 return VINF_SUCCESS;
2213 }
2214
2215 virtual int EntryDestroyed(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry)
2216 {
2217 int rc = CrFbDisplayBase::EntryDestroyed(pFb, hEntry);
2218 if (!RT_SUCCESS(rc))
2219 {
2220 WARN(("err"));
2221 return rc;
2222 }
2223
2224 CrFbDisplayBase *pIter;
2225 RTListForEachCpp(&mDisplays, pIter, CrFbDisplayBase, mNode)
2226 {
2227 int rc = pIter->EntryDestroyed(pFb, hEntry);
2228 if (!RT_SUCCESS(rc))
2229 {
2230 WARN(("err"));
2231 return rc;
2232 }
2233 }
2234 return VINF_SUCCESS;
2235 }
2236
2237 virtual int RegionsChanged(struct CR_FRAMEBUFFER *pFb)
2238 {
2239 int rc = CrFbDisplayBase::RegionsChanged(pFb);
2240 if (!RT_SUCCESS(rc))
2241 {
2242 WARN(("err"));
2243 return rc;
2244 }
2245
2246 CrFbDisplayBase *pIter;
2247 RTListForEachCpp(&mDisplays, pIter, CrFbDisplayBase, mNode)
2248 {
2249 int rc = pIter->RegionsChanged(pFb);
2250 if (!RT_SUCCESS(rc))
2251 {
2252 WARN(("err"));
2253 return rc;
2254 }
2255 }
2256 return VINF_SUCCESS;
2257 }
2258
2259 virtual int FramebufferChanged(struct CR_FRAMEBUFFER *pFb)
2260 {
2261 int rc = CrFbDisplayBase::FramebufferChanged(pFb);
2262 if (!RT_SUCCESS(rc))
2263 {
2264 WARN(("err"));
2265 return rc;
2266 }
2267
2268 CrFbDisplayBase *pIter;
2269 RTListForEachCpp(&mDisplays, pIter, CrFbDisplayBase, mNode)
2270 {
2271 int rc = pIter->FramebufferChanged(pFb);
2272 if (!RT_SUCCESS(rc))
2273 {
2274 WARN(("err"));
2275 return rc;
2276 }
2277 }
2278 return VINF_SUCCESS;
2279 }
2280
2281 virtual ~CrFbDisplayComposite()
2282 {
2283 cleanup();
2284 }
2285
2286 void cleanup(bool fCleanupDisplays = true)
2287 {
2288 CrFbDisplayBase *pIter, *pIterNext;
2289 RTListForEachSafeCpp(&mDisplays, pIter, pIterNext, CrFbDisplayBase, mNode)
2290 {
2291 remove(pIter, fCleanupDisplays);
2292 }
2293 }
2294private:
2295 RTLISTNODE mDisplays;
2296 uint32_t mcDisplays;
2297};
2298
2299typedef union CR_FBWIN_FLAGS
2300{
2301 struct {
2302 uint32_t fVisible : 1;
2303 uint32_t fDataPresented : 1;
2304 uint32_t fForcePresentOnReenable : 1;
2305 uint32_t fCompositoEntriesModified : 1;
2306 uint32_t Reserved : 28;
2307 };
2308 uint32_t Value;
2309} CR_FBWIN_FLAGS;
2310
2311class CrFbWindow
2312{
2313public:
2314 CrFbWindow(uint64_t parentId) :
2315 mSpuWindow(0),
2316 mpCompositor(NULL),
2317 mcUpdates(0),
2318 mxPos(0),
2319 myPos(0),
2320 mWidth(0),
2321 mHeight(0),
2322 mParentId(parentId)
2323 {
2324 mFlags.Value = 0;
2325 }
2326
2327 bool IsCreated() const
2328 {
2329 return !!mSpuWindow;
2330 }
2331
2332 bool IsVisivle() const
2333 {
2334 return mFlags.fVisible;
2335 }
2336
2337 void Destroy()
2338 {
2339 CRASSERT(!mcUpdates);
2340
2341 if (!mSpuWindow)
2342 return;
2343
2344 cr_server.head_spu->dispatch_table.WindowDestroy(mSpuWindow);
2345
2346 mSpuWindow = 0;
2347 mFlags.fDataPresented = 0;
2348 }
2349
2350 int Reparent(uint64_t parentId)
2351 {
2352 if (!checkInitedUpdating())
2353 {
2354 WARN(("err"));
2355 return VERR_INVALID_STATE;
2356 }
2357
2358 uint64_t oldParentId = mParentId;
2359
2360 mParentId = parentId;
2361
2362 if (mSpuWindow)
2363 {
2364 if (oldParentId && !parentId && mFlags.fVisible)
2365 cr_server.head_spu->dispatch_table.WindowShow(mSpuWindow, false);
2366
2367 renderspuSetWindowId(mParentId);
2368 renderspuReparentWindow(mSpuWindow);
2369 renderspuSetWindowId(cr_server.screen[0].winID);
2370
2371 if (parentId)
2372 cr_server.head_spu->dispatch_table.WindowPosition(mSpuWindow, mxPos, myPos);
2373
2374 if (!oldParentId && parentId && mFlags.fVisible)
2375 cr_server.head_spu->dispatch_table.WindowShow(mSpuWindow, true);
2376 }
2377
2378 return VINF_SUCCESS;
2379 }
2380
2381 int SetVisible(bool fVisible)
2382 {
2383 if (!checkInitedUpdating())
2384 {
2385 WARN(("err"));
2386 return VERR_INVALID_STATE;
2387 }
2388
2389 LOG(("CrWIN: Vidible [%d]", fVisible));
2390
2391 if (!fVisible != !mFlags.fVisible)
2392 {
2393 mFlags.fVisible = fVisible;
2394 if (mSpuWindow && mParentId)
2395 cr_server.head_spu->dispatch_table.WindowShow(mSpuWindow, fVisible);
2396 }
2397
2398 return VINF_SUCCESS;
2399 }
2400
2401 int SetSize(uint32_t width, uint32_t height)
2402 {
2403 if (!checkInitedUpdating())
2404 {
2405 WARN(("err"));
2406 return VERR_INVALID_STATE;
2407 }
2408
2409 LOG(("CrWIN: Size [%d ; %d]", width, height));
2410
2411 if (mWidth != width || mHeight != height)
2412 {
2413 mFlags.fCompositoEntriesModified = 1;
2414 mWidth = width;
2415 mHeight = height;
2416 if (mSpuWindow)
2417 cr_server.head_spu->dispatch_table.WindowSize(mSpuWindow, width, height);
2418 }
2419
2420 return VINF_SUCCESS;
2421 }
2422
2423 int SetPosition(int32_t x, int32_t y)
2424 {
2425 if (!checkInitedUpdating())
2426 {
2427 WARN(("err"));
2428 return VERR_INVALID_STATE;
2429 }
2430
2431 LOG(("CrWIN: Pos [%d ; %d]", x, y));
2432// always do WindowPosition to ensure window is adjusted properly
2433// if (x != mxPos || y != myPos)
2434 {
2435 mxPos = x;
2436 myPos = y;
2437 if (mSpuWindow)
2438 cr_server.head_spu->dispatch_table.WindowPosition(mSpuWindow, x, y);
2439 }
2440
2441 return VINF_SUCCESS;
2442 }
2443
2444 int SetVisibleRegionsChanged()
2445 {
2446 if (!checkInitedUpdating())
2447 {
2448 WARN(("err"));
2449 return VERR_INVALID_STATE;
2450 }
2451
2452 mFlags.fCompositoEntriesModified = 1;
2453 return VINF_SUCCESS;
2454 }
2455
2456 int SetCompositor(const struct VBOXVR_SCR_COMPOSITOR * pCompositor)
2457 {
2458 if (!checkInitedUpdating())
2459 {
2460 WARN(("err"));
2461 return VERR_INVALID_STATE;
2462 }
2463
2464 mpCompositor = pCompositor;
2465 mFlags.fCompositoEntriesModified = 1;
2466 return VINF_SUCCESS;
2467 }
2468
2469 int UpdateBegin()
2470 {
2471 ++mcUpdates;
2472 if (mcUpdates > 1)
2473 return VINF_SUCCESS;
2474
2475 Assert(!mFlags.fForcePresentOnReenable);
2476// Assert(!mFlags.fCompositoEntriesModified);
2477
2478 if (mFlags.fDataPresented)
2479 {
2480 Assert(mSpuWindow);
2481 cr_server.head_spu->dispatch_table.VBoxPresentComposition(mSpuWindow, NULL, NULL);
2482 mFlags.fForcePresentOnReenable = isPresentNeeded();
2483 }
2484
2485 return VINF_SUCCESS;
2486 }
2487
2488 void UpdateEnd()
2489 {
2490 --mcUpdates;
2491 Assert(mcUpdates < UINT32_MAX/2);
2492 if (mcUpdates)
2493 return;
2494
2495 checkRegions();
2496
2497 if (mSpuWindow)
2498 {
2499 bool fPresentNeeded = isPresentNeeded();
2500 if (fPresentNeeded || mFlags.fForcePresentOnReenable)
2501 {
2502 mFlags.fForcePresentOnReenable = false;
2503 if (mpCompositor)
2504 cr_server.head_spu->dispatch_table.VBoxPresentComposition(mSpuWindow, mpCompositor, NULL);
2505 else
2506 {
2507 VBOXVR_SCR_COMPOSITOR TmpCompositor;
2508 RTRECT Rect;
2509 Rect.xLeft = 0;
2510 Rect.yTop = 0;
2511 Rect.xRight = mWidth;
2512 Rect.yBottom = mHeight;
2513 CrVrScrCompositorInit(&TmpCompositor, &Rect);
2514 /* this is a cleanup operation
2515 * empty compositor is guarantid to be released on VBoxPresentComposition return */
2516 cr_server.head_spu->dispatch_table.VBoxPresentComposition(mSpuWindow, mpCompositor, NULL);
2517 }
2518 g_pLed->Asserted.s.fWriting = 1;
2519 }
2520
2521 /* even if the above branch is entered due to mFlags.fForcePresentOnReenable,
2522 * the backend should clean up the compositor as soon as presentation is performed */
2523 mFlags.fDataPresented = fPresentNeeded;
2524 }
2525 else
2526 {
2527 Assert(!mFlags.fDataPresented);
2528 Assert(!mFlags.fForcePresentOnReenable);
2529 }
2530 }
2531
2532 uint64_t GetParentId()
2533 {
2534 return mParentId;
2535 }
2536
2537 int Create()
2538 {
2539 if (mSpuWindow)
2540 {
2541 //WARN(("window already created"));
2542 return VINF_ALREADY_INITIALIZED;
2543 }
2544
2545 CRASSERT(cr_server.fVisualBitsDefault);
2546 renderspuSetWindowId(mParentId);
2547 mSpuWindow = cr_server.head_spu->dispatch_table.WindowCreate("", cr_server.fVisualBitsDefault);
2548 renderspuSetWindowId(cr_server.screen[0].winID);
2549 if (mSpuWindow < 0) {
2550 WARN(("WindowCreate failed"));
2551 return VERR_GENERAL_FAILURE;
2552 }
2553
2554 cr_server.head_spu->dispatch_table.WindowSize(mSpuWindow, mWidth, mHeight);
2555 cr_server.head_spu->dispatch_table.WindowPosition(mSpuWindow, mxPos, myPos);
2556
2557 checkRegions();
2558
2559 if (mParentId && mFlags.fVisible)
2560 cr_server.head_spu->dispatch_table.WindowShow(mSpuWindow, true);
2561
2562 return VINF_SUCCESS;
2563 }
2564
2565 ~CrFbWindow()
2566 {
2567 Destroy();
2568 }
2569protected:
2570 void checkRegions()
2571 {
2572 if (!mSpuWindow)
2573 return;
2574
2575 if (!mFlags.fCompositoEntriesModified)
2576 return;
2577
2578 uint32_t cRects;
2579 const RTRECT *pRects;
2580 if (mpCompositor)
2581 {
2582 int rc = CrVrScrCompositorRegionsGet(mpCompositor, &cRects, NULL, &pRects, NULL);
2583 if (!RT_SUCCESS(rc))
2584 {
2585 WARN(("CrVrScrCompositorRegionsGet failed rc %d", rc));
2586 cRects = 0;
2587 pRects = NULL;
2588 }
2589 }
2590 else
2591 {
2592 cRects = 0;
2593 pRects = NULL;
2594 }
2595
2596 cr_server.head_spu->dispatch_table.WindowVisibleRegion(mSpuWindow, cRects, (const GLint*)pRects);
2597
2598 mFlags.fCompositoEntriesModified = 0;
2599 }
2600
2601 bool isPresentNeeded()
2602 {
2603 return mFlags.fVisible && mWidth && mHeight && mpCompositor && !CrVrScrCompositorIsEmpty(mpCompositor);
2604 }
2605
2606 bool checkInitedUpdating()
2607 {
2608 if (!mcUpdates)
2609 {
2610 WARN(("not updating"));
2611 return false;
2612 }
2613
2614 return true;
2615 }
2616private:
2617 GLint mSpuWindow;
2618 const struct VBOXVR_SCR_COMPOSITOR * mpCompositor;
2619 uint32_t mcUpdates;
2620 int32_t mxPos;
2621 int32_t myPos;
2622 uint32_t mWidth;
2623 uint32_t mHeight;
2624 CR_FBWIN_FLAGS mFlags;
2625 uint64_t mParentId;
2626};
2627
2628typedef union CR_FBDISPWINDOW_FLAGS
2629{
2630 struct {
2631 uint32_t fNeVisible : 1;
2632 uint32_t fNeForce : 1;
2633 uint32_t Reserved : 30;
2634 };
2635 uint32_t u32Value;
2636} CR_FBDISPWINDOW_FLAGS;
2637class CrFbDisplayWindow : public CrFbDisplayBase
2638{
2639public:
2640 CrFbDisplayWindow(CrFbWindow *pWindow, const RTRECT *pViewportRect) :
2641 mpWindow(pWindow),
2642 mViewportRect(*pViewportRect),
2643 mu32Screen(~0)
2644 {
2645 mFlags.u32Value = 0;
2646 CRASSERT(pWindow);
2647 }
2648
2649 virtual ~CrFbDisplayWindow()
2650 {
2651 if (mpWindow)
2652 delete mpWindow;
2653 }
2654
2655 virtual int UpdateBegin(struct CR_FRAMEBUFFER *pFb)
2656 {
2657 int rc = mpWindow->UpdateBegin();
2658 if (RT_SUCCESS(rc))
2659 {
2660 rc = CrFbDisplayBase::UpdateBegin(pFb);
2661 if (RT_SUCCESS(rc))
2662 return VINF_SUCCESS;
2663 else
2664 WARN(("err"));
2665 }
2666 else
2667 WARN(("err"));
2668
2669 return rc;
2670 }
2671
2672 virtual void UpdateEnd(struct CR_FRAMEBUFFER *pFb)
2673 {
2674 CrFbDisplayBase::UpdateEnd(pFb);
2675
2676 mpWindow->UpdateEnd();
2677 }
2678
2679 virtual int RegionsChanged(struct CR_FRAMEBUFFER *pFb)
2680 {
2681 int rc = CrFbDisplayBase::RegionsChanged(pFb);
2682 if (!RT_SUCCESS(rc))
2683 {
2684 WARN(("err"));
2685 return rc;
2686 }
2687
2688 if (mpWindow->GetParentId())
2689 {
2690 rc = mpWindow->Create();
2691 if (!RT_SUCCESS(rc))
2692 {
2693 WARN(("err"));
2694 return rc;
2695 }
2696 }
2697
2698 return VINF_SUCCESS;
2699 }
2700
2701 virtual int EntryCreated(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry)
2702 {
2703 int rc = CrFbDisplayBase::EntryCreated(pFb, hEntry);
2704 if (!RT_SUCCESS(rc))
2705 {
2706 WARN(("err"));
2707 return rc;
2708 }
2709
2710 if (mpWindow->GetParentId())
2711 {
2712 rc = mpWindow->Create();
2713 if (!RT_SUCCESS(rc))
2714 {
2715 WARN(("err"));
2716 return rc;
2717 }
2718 }
2719
2720 return VINF_SUCCESS;
2721 }
2722
2723 virtual int EntryReplaced(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hNewEntry, HCR_FRAMEBUFFER_ENTRY hReplacedEntry)
2724 {
2725 int rc = CrFbDisplayBase::EntryReplaced(pFb, hNewEntry, hReplacedEntry);
2726 if (!RT_SUCCESS(rc))
2727 {
2728 WARN(("err"));
2729 return rc;
2730 }
2731
2732 if (mpWindow->GetParentId())
2733 {
2734 rc = mpWindow->Create();
2735 if (!RT_SUCCESS(rc))
2736 {
2737 WARN(("err"));
2738 return rc;
2739 }
2740 }
2741
2742 return VINF_SUCCESS;
2743 }
2744
2745 virtual int EntryTexChanged(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry)
2746 {
2747 int rc = CrFbDisplayBase::EntryTexChanged(pFb, hEntry);
2748 if (!RT_SUCCESS(rc))
2749 {
2750 WARN(("err"));
2751 return rc;
2752 }
2753
2754 if (mpWindow->GetParentId())
2755 {
2756 rc = mpWindow->Create();
2757 if (!RT_SUCCESS(rc))
2758 {
2759 WARN(("err"));
2760 return rc;
2761 }
2762 }
2763
2764 return VINF_SUCCESS;
2765 }
2766
2767 virtual int FramebufferChanged(struct CR_FRAMEBUFFER *pFb)
2768 {
2769 int rc = CrFbDisplayBase::FramebufferChanged(pFb);
2770 if (!RT_SUCCESS(rc))
2771 {
2772 WARN(("err"));
2773 return rc;
2774 }
2775
2776 return screenChanged();
2777 }
2778
2779 const RTRECT* getViewportRect()
2780 {
2781 return &mViewportRect;
2782 }
2783
2784 virtual int setViewportRect(const RTRECT *pViewportRect)
2785 {
2786 if (!isUpdating())
2787 {
2788 WARN(("not updating!"));
2789 return VERR_INVALID_STATE;
2790 }
2791
2792// always call SetPosition to ensure window is adjustep properly
2793// if (pViewportRect->xLeft != mViewportRect.xLeft || pViewportRect->yTop != mViewportRect.yTop)
2794 {
2795 const RTRECT* pRect = getRect();
2796 int rc = mpWindow->SetPosition(pRect->xLeft - pViewportRect->xLeft, pRect->yTop - pViewportRect->yTop);
2797 if (!RT_SUCCESS(rc))
2798 {
2799 WARN(("SetPosition failed"));
2800 return rc;
2801 }
2802 }
2803
2804 mViewportRect = *pViewportRect;
2805
2806 return VINF_SUCCESS;
2807 }
2808
2809 virtual CrFbWindow * windowDetach()
2810 {
2811 if (isUpdating())
2812 {
2813 WARN(("updating!"));
2814 return NULL;
2815 }
2816
2817 CrFbWindow * pWindow = mpWindow;
2818 if (mpWindow)
2819 {
2820 windowCleanup();
2821 mpWindow = NULL;
2822 }
2823 return pWindow;
2824 }
2825
2826 virtual CrFbWindow * windowAttach(CrFbWindow * pNewWindow)
2827 {
2828 if (isUpdating())
2829 {
2830 WARN(("updating!"));
2831 return NULL;
2832 }
2833
2834 CrFbWindow * pOld = mpWindow;
2835 if (mpWindow)
2836 windowDetach();
2837
2838 mpWindow = pNewWindow;
2839 if (pNewWindow)
2840 windowSync();
2841
2842 return mpWindow;
2843 }
2844
2845 virtual int reparent(uint64_t parentId)
2846 {
2847 if (!isUpdating())
2848 {
2849 WARN(("not updating!"));
2850 return VERR_INVALID_STATE;
2851 }
2852
2853 int rc = mpWindow->Reparent(parentId);
2854 if (!RT_SUCCESS(rc))
2855 WARN(("window reparent failed"));
2856
2857 mFlags.fNeForce = 1;
2858
2859 return rc;
2860 }
2861
2862 virtual bool isVisible()
2863 {
2864 HCR_FRAMEBUFFER hFb = getFramebuffer();
2865 if (!hFb)
2866 return false;
2867 const struct VBOXVR_SCR_COMPOSITOR* pCompositor = CrFbGetCompositor(hFb);
2868 return !CrVrScrCompositorIsEmpty(pCompositor);
2869 }
2870
2871 int winVisibilityChanged()
2872 {
2873 HCR_FRAMEBUFFER hFb = getFramebuffer();
2874 if (!hFb || !CrFbIsEnabled(hFb))
2875 {
2876 Assert(!mpWindow->IsVisivle());
2877 return VINF_SUCCESS;
2878 }
2879
2880 int rc = mpWindow->UpdateBegin();
2881 if (RT_SUCCESS(rc))
2882 {
2883 rc = mpWindow->SetVisible(!g_CrPresenter.fWindowsForceHidden);
2884 if (!RT_SUCCESS(rc))
2885 WARN(("SetVisible failed, rc %d", rc));
2886
2887 mpWindow->UpdateEnd();
2888 }
2889 else
2890 WARN(("UpdateBegin failed, rc %d", rc));
2891
2892 return rc;
2893 }
2894
2895protected:
2896 virtual void onUpdateEnd()
2897 {
2898 CrFbDisplayBase::onUpdateEnd();
2899 bool fVisible = isVisible();
2900 if (mFlags.fNeVisible != fVisible || mFlags.fNeForce)
2901 {
2902 crVBoxServerNotifyEvent(mu32Screen, VBOX3D_NOTIFY_EVENT_TYPE_VISIBLE_3DDATA, fVisible ? (void*)1 : NULL);
2903 mFlags.fNeVisible = fVisible;
2904 mFlags.fNeForce = 0;
2905 }
2906 }
2907
2908 virtual void ueRegions()
2909 {
2910 mpWindow->SetVisibleRegionsChanged();
2911 }
2912
2913 virtual int screenChanged()
2914 {
2915 if (!isUpdating())
2916 {
2917 WARN(("not updating!"));
2918 return VERR_INVALID_STATE;
2919 }
2920
2921 int rc = windowDimensionsSync();
2922 if (!RT_SUCCESS(rc))
2923 {
2924 WARN(("windowDimensionsSync failed rc %d", rc));
2925 return rc;
2926 }
2927
2928 return VINF_SUCCESS;
2929 }
2930
2931 virtual int windowSetCompositor(bool fSet)
2932 {
2933 if (fSet)
2934 {
2935 const struct VBOXVR_SCR_COMPOSITOR* pCompositor = CrFbGetCompositor(getFramebuffer());
2936 return mpWindow->SetCompositor(pCompositor);
2937 }
2938 return mpWindow->SetCompositor(NULL);
2939 }
2940
2941 virtual int windowCleanup()
2942 {
2943 int rc = mpWindow->UpdateBegin();
2944 if (!RT_SUCCESS(rc))
2945 {
2946 WARN(("err"));
2947 return rc;
2948 }
2949
2950 rc = windowDimensionsSync(true);
2951 if (!RT_SUCCESS(rc))
2952 {
2953 WARN(("err"));
2954 mpWindow->UpdateEnd();
2955 return rc;
2956 }
2957
2958 rc = windowSetCompositor(false);
2959 if (!RT_SUCCESS(rc))
2960 {
2961 WARN(("err"));
2962 mpWindow->UpdateEnd();
2963 return rc;
2964 }
2965
2966 mpWindow->UpdateEnd();
2967
2968 return VINF_SUCCESS;
2969 }
2970
2971 virtual int fbCleanup()
2972 {
2973 int rc = windowCleanup();
2974 if (!RT_SUCCESS(rc))
2975 {
2976 WARN(("windowCleanup failed"));
2977 return rc;
2978 }
2979 return CrFbDisplayBase::fbCleanup();
2980 }
2981
2982 int windowDimensionsSync(bool fForceCleanup = false)
2983 {
2984 int rc = VINF_SUCCESS;
2985
2986 HCR_FRAMEBUFFER hFb = getFramebuffer();
2987 if (!fForceCleanup && hFb && CrFbIsEnabled(hFb))
2988 {
2989 const RTRECT* pRect = getRect();
2990
2991 rc = mpWindow->SetPosition(pRect->xLeft - mViewportRect.xLeft, pRect->yTop - mViewportRect.yTop);
2992 if (!RT_SUCCESS(rc))
2993 {
2994 WARN(("err"));
2995 return rc;
2996 }
2997
2998 setRegionsChanged();
2999
3000 rc = mpWindow->SetSize((uint32_t)(pRect->xRight - pRect->xLeft), (uint32_t)(pRect->yBottom - pRect->yTop));
3001 if (!RT_SUCCESS(rc))
3002 {
3003 WARN(("err"));
3004 return rc;
3005 }
3006
3007 rc = mpWindow->SetVisible(!g_CrPresenter.fWindowsForceHidden);
3008 if (!RT_SUCCESS(rc))
3009 {
3010 WARN(("err"));
3011 return rc;
3012 }
3013 }
3014 else
3015 {
3016 rc = mpWindow->SetVisible(false);
3017 if (!RT_SUCCESS(rc))
3018 {
3019 WARN(("err"));
3020 return rc;
3021 }
3022 }
3023
3024 return rc;
3025 }
3026
3027 virtual int windowSync()
3028 {
3029 int rc = mpWindow->UpdateBegin();
3030 if (!RT_SUCCESS(rc))
3031 {
3032 WARN(("err"));
3033 return rc;
3034 }
3035
3036 rc = windowSetCompositor(true);
3037 if (!RT_SUCCESS(rc))
3038 {
3039 WARN(("err"));
3040 mpWindow->UpdateEnd();
3041 return rc;
3042 }
3043
3044 rc = windowDimensionsSync();
3045 if (!RT_SUCCESS(rc))
3046 {
3047 WARN(("err"));
3048 mpWindow->UpdateEnd();
3049 return rc;
3050 }
3051
3052 mpWindow->UpdateEnd();
3053
3054 return rc;
3055 }
3056
3057 virtual int fbSync()
3058 {
3059 int rc = CrFbDisplayBase::fbSync();
3060 if (!RT_SUCCESS(rc))
3061 {
3062 WARN(("err"));
3063 return rc;
3064 }
3065
3066 HCR_FRAMEBUFFER hFb = getFramebuffer();
3067
3068 mu32Screen = CrFbGetScreenInfo(hFb)->u32ViewIndex;
3069
3070 rc = windowSync();
3071 if (!RT_SUCCESS(rc))
3072 {
3073 WARN(("windowSync failed %d", rc));
3074 return rc;
3075 }
3076
3077 if (CrFbHas3DData(hFb))
3078 {
3079 if (mpWindow->GetParentId())
3080 {
3081 rc = mpWindow->Create();
3082 if (!RT_SUCCESS(rc))
3083 {
3084 WARN(("err"));
3085 return rc;
3086 }
3087 }
3088 }
3089
3090 return VINF_SUCCESS;
3091 }
3092
3093 virtual const struct RTRECT* getRect()
3094 {
3095 const struct VBOXVR_SCR_COMPOSITOR* pCompositor = CrFbGetCompositor(getFramebuffer());
3096 return CrVrScrCompositorRectGet(pCompositor);
3097 }
3098
3099 CrFbWindow* getWindow() {return mpWindow;}
3100private:
3101 CrFbWindow *mpWindow;
3102 RTRECT mViewportRect;
3103 CR_FBDISPWINDOW_FLAGS mFlags;
3104 uint32_t mu32Screen;
3105};
3106
3107class CrFbDisplayWindowRootVr : public CrFbDisplayWindow
3108{
3109public:
3110 CrFbDisplayWindowRootVr(CrFbWindow *pWindow, const RTRECT *pViewportRect) :
3111 CrFbDisplayWindow(pWindow, pViewportRect)
3112 {
3113 CrVrScrCompositorInit(&mCompositor, NULL);
3114 }
3115
3116 virtual int EntryCreated(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry)
3117 {
3118 int rc = CrFbDisplayWindow::EntryCreated(pFb, hEntry);
3119 if (!RT_SUCCESS(rc))
3120 {
3121 WARN(("err"));
3122 return rc;
3123 }
3124
3125 Assert(!CrFbDDataEntryGet(hEntry, slotGet()));
3126
3127 const VBOXVR_SCR_COMPOSITOR_ENTRY* pSrcEntry = CrFbEntryGetCompositorEntry(hEntry);
3128 VBOXVR_SCR_COMPOSITOR_ENTRY *pMyEntry = entryAlloc();
3129 CrVrScrCompositorEntryInit(pMyEntry, CrVrScrCompositorEntryRectGet(pSrcEntry), CrVrScrCompositorEntryTexGet(pSrcEntry), NULL);
3130 CrVrScrCompositorEntryFlagsSet(pMyEntry, CrVrScrCompositorEntryFlagsGet(pSrcEntry));
3131 rc = CrFbDDataEntryPut(hEntry, slotGet(), pMyEntry);
3132 if (!RT_SUCCESS(rc))
3133 {
3134 WARN(("CrFbDDataEntryPut failed rc %d", rc));
3135 entryFree(pMyEntry);
3136 return rc;
3137 }
3138
3139 return VINF_SUCCESS;
3140 }
3141
3142 virtual int EntryAdded(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry)
3143 {
3144 int rc = CrFbDisplayWindow::EntryAdded(pFb, hEntry);
3145 if (!RT_SUCCESS(rc))
3146 {
3147 WARN(("err"));
3148 return rc;
3149 }
3150
3151 const VBOXVR_SCR_COMPOSITOR_ENTRY* pSrcEntry = CrFbEntryGetCompositorEntry(hEntry);
3152 VBOXVR_SCR_COMPOSITOR_ENTRY *pMyEntry = (VBOXVR_SCR_COMPOSITOR_ENTRY*)CrFbDDataEntryGet(hEntry, slotGet());
3153 Assert(pMyEntry);
3154 CrVrScrCompositorEntryTexSet(pMyEntry, CrVrScrCompositorEntryTexGet(pSrcEntry));
3155
3156 return VINF_SUCCESS;
3157 }
3158
3159 virtual int EntryReplaced(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hNewEntry, HCR_FRAMEBUFFER_ENTRY hReplacedEntry)
3160 {
3161 int rc = CrFbDisplayWindow::EntryReplaced(pFb, hNewEntry, hReplacedEntry);
3162 if (!RT_SUCCESS(rc))
3163 {
3164 WARN(("err"));
3165 return rc;
3166 }
3167
3168 const VBOXVR_SCR_COMPOSITOR_ENTRY* pSrcNewEntry = CrFbEntryGetCompositorEntry(hNewEntry);
3169 VBOXVR_SCR_COMPOSITOR_ENTRY *pMyEntry = (VBOXVR_SCR_COMPOSITOR_ENTRY*)CrFbDDataEntryGet(hNewEntry, slotGet());
3170 CrVrScrCompositorEntryTexSet(pMyEntry, CrVrScrCompositorEntryTexGet(pSrcNewEntry));
3171
3172 return VINF_SUCCESS;
3173 }
3174
3175 virtual int EntryTexChanged(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry)
3176 {
3177 int rc = CrFbDisplayWindow::EntryTexChanged(pFb, hEntry);
3178 if (!RT_SUCCESS(rc))
3179 {
3180 WARN(("err"));
3181 return rc;
3182 }
3183
3184 const VBOXVR_SCR_COMPOSITOR_ENTRY* pSrcEntry = CrFbEntryGetCompositorEntry(hEntry);
3185 VBOXVR_SCR_COMPOSITOR_ENTRY *pMyEntry = (VBOXVR_SCR_COMPOSITOR_ENTRY*)CrFbDDataEntryGet(hEntry, slotGet());
3186 CrVrScrCompositorEntryTexSet(pMyEntry, CrVrScrCompositorEntryTexGet(pSrcEntry));
3187
3188 return VINF_SUCCESS;
3189 }
3190
3191 virtual int EntryRemoved(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry)
3192 {
3193 int rc = CrFbDisplayWindow::EntryRemoved(pFb, hEntry);
3194 if (!RT_SUCCESS(rc))
3195 {
3196 WARN(("err"));
3197 return rc;
3198 }
3199
3200 VBOXVR_SCR_COMPOSITOR_ENTRY *pMyEntry = (VBOXVR_SCR_COMPOSITOR_ENTRY*)CrFbDDataEntryGet(hEntry, slotGet());
3201 rc = CrVrScrCompositorEntryRegionsSet(&mCompositor, pMyEntry, NULL, 0, NULL, false, NULL);
3202 if (!RT_SUCCESS(rc))
3203 {
3204 WARN(("err"));
3205 return rc;
3206 }
3207
3208 return VINF_SUCCESS;
3209 }
3210
3211 virtual int EntryDestroyed(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry)
3212 {
3213 int rc = CrFbDisplayWindow::EntryDestroyed(pFb, hEntry);
3214 if (!RT_SUCCESS(rc))
3215 {
3216 WARN(("err"));
3217 return rc;
3218 }
3219
3220 const VBOXVR_SCR_COMPOSITOR_ENTRY* pSrcEntry = CrFbEntryGetCompositorEntry(hEntry);
3221 VBOXVR_SCR_COMPOSITOR_ENTRY *pMyEntry = (VBOXVR_SCR_COMPOSITOR_ENTRY*)CrFbDDataEntryGet(hEntry, slotGet());
3222 CrVrScrCompositorEntryCleanup(pMyEntry);
3223 entryFree(pMyEntry);
3224
3225 return VINF_SUCCESS;
3226 }
3227
3228 virtual int setViewportRect(const RTRECT *pViewportRect)
3229 {
3230 int rc = CrFbDisplayWindow::setViewportRect(pViewportRect);
3231 if (!RT_SUCCESS(rc))
3232 {
3233 WARN(("err"));
3234 return rc;
3235 }
3236
3237 rc = setRegionsChanged();
3238 if (!RT_SUCCESS(rc))
3239 {
3240 WARN(("err"));
3241 return rc;
3242 }
3243
3244 return VINF_SUCCESS;
3245 }
3246
3247protected:
3248 virtual int windowSetCompositor(bool fSet)
3249 {
3250 if (fSet)
3251 return getWindow()->SetCompositor(&mCompositor);
3252 return getWindow()->SetCompositor(NULL);
3253 }
3254
3255 virtual void ueRegions()
3256 {
3257 synchCompositorRegions();
3258 }
3259
3260 int compositorMarkUpdated()
3261 {
3262 CrVrScrCompositorClear(&mCompositor);
3263
3264 int rc = CrVrScrCompositorRectSet(&mCompositor, CrVrScrCompositorRectGet(CrFbGetCompositor(getFramebuffer())), NULL);
3265 if (!RT_SUCCESS(rc))
3266 {
3267 WARN(("err"));
3268 return rc;
3269 }
3270
3271 rc = setRegionsChanged();
3272 if (!RT_SUCCESS(rc))
3273 {
3274 WARN(("screenChanged failed %d", rc));
3275 return rc;
3276 }
3277
3278 return VINF_SUCCESS;
3279 }
3280
3281 virtual int screenChanged()
3282 {
3283 int rc = compositorMarkUpdated();
3284 if (!RT_SUCCESS(rc))
3285 {
3286 WARN(("err"));
3287 return rc;
3288 }
3289
3290 rc = CrFbDisplayWindow::screenChanged();
3291 if (!RT_SUCCESS(rc))
3292 {
3293 WARN(("screenChanged failed %d", rc));
3294 return rc;
3295 }
3296
3297 return VINF_SUCCESS;
3298 }
3299
3300 virtual const struct RTRECT* getRect()
3301 {
3302 return CrVrScrCompositorRectGet(&mCompositor);
3303 }
3304
3305 virtual int fbCleanup()
3306 {
3307 int rc = clearCompositor();
3308 if (!RT_SUCCESS(rc))
3309 {
3310 WARN(("err"));
3311 return rc;
3312 }
3313
3314 return CrFbDisplayWindow::fbCleanup();
3315 }
3316
3317 virtual int fbSync()
3318 {
3319 int rc = synchCompositor();
3320 if (!RT_SUCCESS(rc))
3321 {
3322 WARN(("err"));
3323 return rc;
3324 }
3325
3326 return CrFbDisplayWindow::fbSync();
3327 }
3328
3329 VBOXVR_SCR_COMPOSITOR_ENTRY* entryAlloc()
3330 {
3331#ifndef VBOXVDBG_MEMCACHE_DISABLE
3332 return (VBOXVR_SCR_COMPOSITOR_ENTRY*)RTMemCacheAlloc(g_CrPresenter.CEntryLookasideList);
3333#else
3334 return (VBOXVR_SCR_COMPOSITOR_ENTRY*)RTMemAlloc(sizeof (VBOXVR_SCR_COMPOSITOR_ENTRY));
3335#endif
3336 }
3337
3338 void entryFree(VBOXVR_SCR_COMPOSITOR_ENTRY* pEntry)
3339 {
3340 Assert(!CrVrScrCompositorEntryIsUsed(pEntry));
3341#ifndef VBOXVDBG_MEMCACHE_DISABLE
3342 RTMemCacheFree(g_CrPresenter.CEntryLookasideList, pEntry);
3343#else
3344 RTMemFree(pEntry);
3345#endif
3346 }
3347
3348 int synchCompositorRegions()
3349 {
3350 int rc;
3351
3352 rootVrTranslateForPos();
3353
3354 /* ensure the rootvr compositor does not hold any data,
3355 * i.e. cleanup all rootvr entries data */
3356 CrVrScrCompositorClear(&mCompositor);
3357
3358 rc = CrVrScrCompositorIntersectedList(CrFbGetCompositor(getFramebuffer()), &cr_server.RootVr, &mCompositor, rootVrGetCEntry, this, NULL);
3359 if (!RT_SUCCESS(rc))
3360 {
3361 WARN(("CrVrScrCompositorIntersectedList failed, rc %d", rc));
3362 return rc;
3363 }
3364
3365 return getWindow()->SetVisibleRegionsChanged();
3366 }
3367
3368 virtual int synchCompositor()
3369 {
3370 int rc = compositorMarkUpdated();
3371 if (!RT_SUCCESS(rc))
3372 {
3373 WARN(("compositorMarkUpdated failed, rc %d", rc));
3374 return rc;
3375 }
3376
3377 rc = fbSynchAddAllEntries();
3378 if (!RT_SUCCESS(rc))
3379 {
3380 WARN(("fbSynchAddAllEntries failed, rc %d", rc));
3381 return rc;
3382 }
3383
3384 return rc;
3385 }
3386
3387 virtual int clearCompositor()
3388 {
3389 return fbCleanupRemoveAllEntries();
3390 }
3391
3392 void rootVrTranslateForPos()
3393 {
3394 const RTRECT *pRect = getViewportRect();
3395 const struct VBVAINFOSCREEN* pScreen = CrFbGetScreenInfo(getFramebuffer());
3396 int32_t x = pScreen->i32OriginX;
3397 int32_t y = pScreen->i32OriginY;
3398 int32_t dx = cr_server.RootVrCurPoint.x - x;
3399 int32_t dy = cr_server.RootVrCurPoint.y - y;
3400
3401 cr_server.RootVrCurPoint.x = x;
3402 cr_server.RootVrCurPoint.y = y;
3403
3404 VBoxVrListTranslate(&cr_server.RootVr, dx, dy);
3405 }
3406
3407 static DECLCALLBACK(VBOXVR_SCR_COMPOSITOR_ENTRY*) rootVrGetCEntry(const VBOXVR_SCR_COMPOSITOR_ENTRY*pEntry, void *pvContext)
3408 {
3409 CrFbDisplayWindowRootVr *pThis = (CrFbDisplayWindowRootVr*)pvContext;
3410 HCR_FRAMEBUFFER_ENTRY hEntry = CrFbEntryFromCompositorEntry(pEntry);
3411 VBOXVR_SCR_COMPOSITOR_ENTRY *pMyEntry = (VBOXVR_SCR_COMPOSITOR_ENTRY*)CrFbDDataEntryGet(hEntry, pThis->slotGet());
3412 Assert(!CrVrScrCompositorEntryIsUsed(pMyEntry));
3413 CrVrScrCompositorEntryRectSet(&pThis->mCompositor, pMyEntry, CrVrScrCompositorEntryRectGet(pEntry));
3414 return pMyEntry;
3415 }
3416private:
3417 VBOXVR_SCR_COMPOSITOR mCompositor;
3418};
3419
3420class CrFbDisplayVrdp : public CrFbDisplayBase
3421{
3422public:
3423 CrFbDisplayVrdp()
3424 {
3425 memset(&mPos, 0, sizeof (mPos));
3426 }
3427
3428 virtual int EntryCreated(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry)
3429 {
3430 int rc = CrFbDisplayBase::EntryCreated(pFb, hEntry);
3431 if (!RT_SUCCESS(rc))
3432 {
3433 WARN(("EntryAdded failed rc %d", rc));
3434 return rc;
3435 }
3436
3437 Assert(!CrFbDDataEntryGet(hEntry, slotGet()));
3438 rc = vrdpCreate(pFb, hEntry);
3439 if (!RT_SUCCESS(rc))
3440 {
3441 WARN(("vrdpCreate failed rc %d", rc));
3442 return rc;
3443 }
3444
3445 return VINF_SUCCESS;
3446 }
3447
3448 virtual int EntryReplaced(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hNewEntry, HCR_FRAMEBUFFER_ENTRY hReplacedEntry)
3449 {
3450 int rc = CrFbDisplayBase::EntryReplaced(pFb, hNewEntry, hReplacedEntry);
3451 if (!RT_SUCCESS(rc))
3452 {
3453 WARN(("err"));
3454 return rc;
3455 }
3456
3457 const VBOXVR_SCR_COMPOSITOR_ENTRY* pReplacedEntry = CrFbEntryGetCompositorEntry(hReplacedEntry);
3458 CR_TEXDATA *pReplacedTex = CrVrScrCompositorEntryTexGet(pReplacedEntry);
3459 const VBOXVR_SCR_COMPOSITOR_ENTRY* pNewEntry = CrFbEntryGetCompositorEntry(hNewEntry);
3460 CR_TEXDATA *pNewTex = CrVrScrCompositorEntryTexGet(pNewEntry);
3461
3462 CrTdBltDataInvalidateNe(pReplacedTex);
3463
3464 rc = CrTdBltEnter(pNewTex);
3465 if (RT_SUCCESS(rc))
3466 {
3467 rc = vrdpFrame(hNewEntry);
3468 CrTdBltLeave(pNewTex);
3469 }
3470 else
3471 WARN(("CrTdBltEnter failed %d", rc));
3472
3473 return rc;
3474 }
3475
3476 virtual int EntryTexChanged(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry)
3477 {
3478 int rc = CrFbDisplayBase::EntryTexChanged(pFb, hEntry);
3479 if (!RT_SUCCESS(rc))
3480 {
3481 WARN(("err"));
3482 return rc;
3483 }
3484
3485 const VBOXVR_SCR_COMPOSITOR_ENTRY* pEntry = CrFbEntryGetCompositorEntry(hEntry);
3486 CR_TEXDATA *pTex = CrVrScrCompositorEntryTexGet(pEntry);
3487
3488 rc = CrTdBltEnter(pTex);
3489 if (RT_SUCCESS(rc))
3490 {
3491 rc = vrdpFrame(hEntry);
3492 CrTdBltLeave(pTex);
3493 }
3494 else
3495 WARN(("CrTdBltEnter failed %d", rc));
3496
3497 return rc;
3498 }
3499
3500 virtual int EntryRemoved(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry)
3501 {
3502 int rc = CrFbDisplayBase::EntryRemoved(pFb, hEntry);
3503 if (!RT_SUCCESS(rc))
3504 {
3505 WARN(("err"));
3506 return rc;
3507 }
3508
3509 const VBOXVR_SCR_COMPOSITOR_ENTRY* pEntry = CrFbEntryGetCompositorEntry(hEntry);
3510 CR_TEXDATA *pTex = CrVrScrCompositorEntryTexGet(pEntry);
3511 CrTdBltDataInvalidateNe(pTex);
3512
3513 return vrdpRegions(pFb, hEntry);
3514 }
3515
3516 virtual int EntryDestroyed(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry)
3517 {
3518 int rc = CrFbDisplayBase::EntryDestroyed(pFb, hEntry);
3519 if (!RT_SUCCESS(rc))
3520 {
3521 WARN(("err"));
3522 return rc;
3523 }
3524
3525 vrdpDestroy(hEntry);
3526 return VINF_SUCCESS;
3527 }
3528
3529 virtual int EntryPosChanged(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry)
3530 {
3531 int rc = CrFbDisplayBase::EntryPosChanged(pFb, hEntry);
3532 if (!RT_SUCCESS(rc))
3533 {
3534 WARN(("err"));
3535 return rc;
3536 }
3537
3538 vrdpGeometry(hEntry);
3539
3540 return VINF_SUCCESS;
3541 }
3542
3543 virtual int RegionsChanged(struct CR_FRAMEBUFFER *pFb)
3544 {
3545 int rc = CrFbDisplayBase::RegionsChanged(pFb);
3546 if (!RT_SUCCESS(rc))
3547 {
3548 WARN(("err"));
3549 return rc;
3550 }
3551
3552 return vrdpRegionsAll(pFb);
3553 }
3554
3555 virtual int FramebufferChanged(struct CR_FRAMEBUFFER *pFb)
3556 {
3557 int rc = CrFbDisplayBase::FramebufferChanged(pFb);
3558 if (!RT_SUCCESS(rc))
3559 {
3560 WARN(("err"));
3561 return rc;
3562 }
3563
3564 syncPos();
3565
3566 rc = vrdpSyncEntryAll(pFb);
3567 if (!RT_SUCCESS(rc))
3568 {
3569 WARN(("err"));
3570 return rc;
3571 }
3572
3573 return vrdpRegionsAll(pFb);
3574 }
3575
3576protected:
3577 void syncPos()
3578 {
3579 const struct VBVAINFOSCREEN* pScreenInfo = CrFbGetScreenInfo(getFramebuffer());
3580 mPos.x = pScreenInfo->i32OriginX;
3581 mPos.y = pScreenInfo->i32OriginY;
3582 }
3583
3584 virtual int fbCleanup()
3585 {
3586 int rc = fbCleanupRemoveAllEntries();
3587 if (!RT_SUCCESS(rc))
3588 {
3589 WARN(("err"));
3590 return rc;
3591 }
3592
3593 return CrFbDisplayBase::fbCleanup();
3594 }
3595
3596 virtual int fbSync()
3597 {
3598 syncPos();
3599
3600 int rc = fbSynchAddAllEntries();
3601 if (!RT_SUCCESS(rc))
3602 {
3603 WARN(("err"));
3604 return rc;
3605 }
3606
3607 return CrFbDisplayBase::fbSync();
3608 }
3609protected:
3610 void vrdpDestroy(HCR_FRAMEBUFFER_ENTRY hEntry)
3611 {
3612 void *pVrdp = CrFbDDataEntryGet(hEntry, slotGet());
3613 cr_server.outputRedirect.CROREnd(pVrdp);
3614 }
3615
3616 void vrdpGeometry(HCR_FRAMEBUFFER_ENTRY hEntry)
3617 {
3618 void *pVrdp = CrFbDDataEntryGet(hEntry, slotGet());
3619 const VBOXVR_SCR_COMPOSITOR_ENTRY* pEntry = CrFbEntryGetCompositorEntry(hEntry);
3620
3621 cr_server.outputRedirect.CRORGeometry(pVrdp,
3622 mPos.x + CrVrScrCompositorEntryRectGet(pEntry)->xLeft,
3623 mPos.y + CrVrScrCompositorEntryRectGet(pEntry)->yTop,
3624 CrVrScrCompositorEntryTexGet(pEntry)->Tex.width,
3625 CrVrScrCompositorEntryTexGet(pEntry)->Tex.height);
3626 }
3627
3628 int vrdpRegions(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry)
3629 {
3630 void *pVrdp = CrFbDDataEntryGet(hEntry, slotGet());
3631 const struct VBOXVR_SCR_COMPOSITOR* pCompositor = CrFbGetCompositor(pFb);
3632 const VBOXVR_SCR_COMPOSITOR_ENTRY* pEntry = CrFbEntryGetCompositorEntry(hEntry);
3633 uint32_t cRects;
3634 const RTRECT *pRects;
3635
3636 int rc = CrVrScrCompositorEntryRegionsGet(pCompositor, pEntry, &cRects, NULL, &pRects, NULL);
3637 if (!RT_SUCCESS(rc))
3638 {
3639 WARN(("CrVrScrCompositorEntryRegionsGet failed, rc %d", rc));
3640 return rc;
3641 }
3642
3643 cr_server.outputRedirect.CRORVisibleRegion(pVrdp, cRects, pRects);
3644 return VINF_SUCCESS;
3645 }
3646
3647 int vrdpFrame(HCR_FRAMEBUFFER_ENTRY hEntry)
3648 {
3649 void *pVrdp = CrFbDDataEntryGet(hEntry, slotGet());
3650 const VBOXVR_SCR_COMPOSITOR_ENTRY* pEntry = CrFbEntryGetCompositorEntry(hEntry);
3651 CR_TEXDATA *pTex = CrVrScrCompositorEntryTexGet(pEntry);
3652 const CR_BLITTER_IMG *pImg;
3653 CrTdBltDataInvalidateNe(pTex);
3654 int rc = CrTdBltDataAcquire(pTex, GL_BGRA, !!(CrVrScrCompositorEntryFlagsGet(pEntry) & CRBLT_F_INVERT_SRC_YCOORDS), &pImg);
3655 if (!RT_SUCCESS(rc))
3656 {
3657 WARN(("CrTdBltDataAcquire failed rc %d", rc));
3658 return rc;
3659 }
3660
3661 cr_server.outputRedirect.CRORFrame(pVrdp, pImg->pvData, pImg->cbData);
3662 CrTdBltDataRelease(pTex);
3663 return VINF_SUCCESS;
3664 }
3665
3666 int vrdpRegionsAll(struct CR_FRAMEBUFFER *pFb)
3667 {
3668 const struct VBOXVR_SCR_COMPOSITOR* pCompositor = CrFbGetCompositor(pFb);
3669 VBOXVR_SCR_COMPOSITOR_CONST_ITERATOR Iter;
3670 CrVrScrCompositorConstIterInit(pCompositor, &Iter);
3671 const VBOXVR_SCR_COMPOSITOR_ENTRY *pEntry;
3672 while ((pEntry = CrVrScrCompositorConstIterNext(&Iter)) != NULL)
3673 {
3674 HCR_FRAMEBUFFER_ENTRY hEntry = CrFbEntryFromCompositorEntry(pEntry);
3675 vrdpRegions(pFb, hEntry);
3676 }
3677
3678 return VINF_SUCCESS;
3679 }
3680
3681 int vrdpSynchEntry(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry)
3682 {
3683 vrdpGeometry(hEntry);
3684
3685 return vrdpRegions(pFb, hEntry);;
3686 }
3687
3688 int vrdpSyncEntryAll(struct CR_FRAMEBUFFER *pFb)
3689 {
3690 const struct VBOXVR_SCR_COMPOSITOR* pCompositor = CrFbGetCompositor(pFb);
3691 VBOXVR_SCR_COMPOSITOR_CONST_ITERATOR Iter;
3692 CrVrScrCompositorConstIterInit(pCompositor, &Iter);
3693 const VBOXVR_SCR_COMPOSITOR_ENTRY *pEntry;
3694 while ((pEntry = CrVrScrCompositorConstIterNext(&Iter)) != NULL)
3695 {
3696 HCR_FRAMEBUFFER_ENTRY hEntry = CrFbEntryFromCompositorEntry(pEntry);
3697 int rc = vrdpSynchEntry(pFb, hEntry);
3698 if (!RT_SUCCESS(rc))
3699 {
3700 WARN(("vrdpSynchEntry failed rc %d", rc));
3701 return rc;
3702 }
3703 }
3704
3705 return VINF_SUCCESS;
3706 }
3707
3708 int vrdpCreate(HCR_FRAMEBUFFER hFb, HCR_FRAMEBUFFER_ENTRY hEntry)
3709 {
3710 void *pVrdp;
3711
3712 /* Query supported formats. */
3713 uint32_t cbFormats = 4096;
3714 char *pachFormats = (char *)crAlloc(cbFormats);
3715
3716 if (!pachFormats)
3717 {
3718 WARN(("crAlloc failed"));
3719 return VERR_NO_MEMORY;
3720 }
3721
3722 int rc = cr_server.outputRedirect.CRORContextProperty(cr_server.outputRedirect.pvContext,
3723 0 /* H3DOR_PROP_FORMATS */, // @todo from a header
3724 pachFormats, cbFormats, &cbFormats);
3725 if (RT_SUCCESS(rc))
3726 {
3727 if (RTStrStr(pachFormats, "H3DOR_FMT_RGBA_TOPDOWN"))
3728 {
3729 cr_server.outputRedirect.CRORBegin(cr_server.outputRedirect.pvContext,
3730 &pVrdp,
3731 "H3DOR_FMT_RGBA_TOPDOWN"); // @todo from a header
3732
3733 if (pVrdp)
3734 {
3735 rc = CrFbDDataEntryPut(hEntry, slotGet(), pVrdp);
3736 if (RT_SUCCESS(rc))
3737 {
3738 vrdpGeometry(hEntry);
3739 vrdpRegions(hFb, hEntry);
3740 //vrdpFrame(hEntry);
3741 return VINF_SUCCESS;
3742 }
3743 else
3744 WARN(("CrFbDDataEntryPut failed rc %d", rc));
3745
3746 cr_server.outputRedirect.CROREnd(pVrdp);
3747 }
3748 else
3749 {
3750 WARN(("CRORBegin failed"));
3751 rc = VERR_GENERAL_FAILURE;
3752 }
3753 }
3754 }
3755 else
3756 WARN(("CRORContextProperty failed rc %d", rc));
3757
3758 crFree(pachFormats);
3759
3760 return rc;
3761 }
3762private:
3763 RTPOINT mPos;
3764};
3765
3766CrFbDisplayBase::~CrFbDisplayBase()
3767{
3768 Assert(!mcUpdates);
3769
3770 if (mpContainer)
3771 mpContainer->remove(this);
3772}
3773
3774
3775#if 0
3776
3777
3778
3779
3780
3781void crDbgDumpRect(uint32_t i, const RTRECT *pRect)
3782{
3783 crDebug("%d: (%d;%d) X (%d;%d)", i, pRect->xLeft, pRect->yTop, pRect->xRight, pRect->yBottom);
3784}
3785
3786void crDbgDumpRects(uint32_t cRects, const RTRECT *paRects)
3787{
3788 crDebug("Dumping rects (%d)", cRects);
3789 for (uint32_t i = 0; i < cRects; ++i)
3790 {
3791 crDbgDumpRect(i, &paRects[i]);
3792 }
3793 crDebug("End Dumping rects (%d)", cRects);
3794}
3795
3796#endif
3797
3798class CrFbDisplayEntryDataMonitor : public CrFbDisplayBase
3799{
3800public:
3801 virtual int EntryReplaced(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hNewEntry, HCR_FRAMEBUFFER_ENTRY hReplacedEntry)
3802 {
3803 entryDataChanged(pFb, hReplacedEntry);
3804 return VINF_SUCCESS;
3805 }
3806
3807 virtual int EntryTexChanged(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry)
3808 {
3809 entryDataChanged(pFb, hEntry);
3810 return VINF_SUCCESS;
3811 }
3812
3813 virtual int EntryRemoved(struct CR_FRAMEBUFFER *pFb, HCR_FRAMEBUFFER_ENTRY hEntry)
3814 {
3815 entryDataChanged(pFb, hEntry);
3816 return VINF_SUCCESS;
3817 }
3818protected:
3819 virtual void entryDataChanged(HCR_FRAMEBUFFER hFb, HCR_FRAMEBUFFER_ENTRY hEntry)
3820 {
3821
3822 }
3823};
3824
3825int CrPMgrDisable()
3826{
3827 if (!g_CrPresenter.fEnabled)
3828 return VINF_SUCCESS;
3829
3830 g_CrPresenter.u32DisabledDisplayMode = g_CrPresenter.u32DisplayMode;
3831
3832 int rc = crPMgrModeModifyGlobal(0, CR_PMGR_MODE_WINDOW);
3833 if (RT_FAILURE(rc))
3834 {
3835 WARN(("crPMgrModeModifyGlobal failed %d", rc));
3836 return rc;
3837 }
3838
3839 crPMgrCleanUnusedDisplays();
3840
3841 g_CrPresenter.fEnabled = false;
3842
3843 return VINF_SUCCESS;
3844}
3845
3846int CrPMgrEnable()
3847{
3848 if (g_CrPresenter.fEnabled)
3849 return VINF_SUCCESS;
3850
3851 g_CrPresenter.fEnabled = true;
3852
3853 int rc = crPMgrModeModifyGlobal(g_CrPresenter.u32DisabledDisplayMode, 0);
3854 if (RT_FAILURE(rc))
3855 {
3856 WARN(("crPMgrModeModifyGlobal failed %d", rc));
3857 g_CrPresenter.fEnabled = false;
3858 return rc;
3859 }
3860
3861 g_CrPresenter.u32DisabledDisplayMode = 0;
3862
3863 return VINF_SUCCESS;
3864}
3865
3866int CrPMgrInit()
3867{
3868 int rc = VINF_SUCCESS;
3869 memset(&g_CrPresenter, 0, sizeof (g_CrPresenter));
3870 g_CrPresenter.fEnabled = true;
3871 for (int i = 0; i < RT_ELEMENTS(g_CrPresenter.aDisplayInfos); ++i)
3872 {
3873 g_CrPresenter.aDisplayInfos[i].u32Id = i;
3874 g_CrPresenter.aDisplayInfos[i].iFb = -1;
3875
3876 g_CrPresenter.aFbInfos[i].u32Id = i;
3877 }
3878
3879 g_CrPresenter.pFbTexMap = crAllocHashtable();
3880 if (g_CrPresenter.pFbTexMap)
3881 {
3882#ifndef VBOXVDBG_MEMCACHE_DISABLE
3883 rc = RTMemCacheCreate(&g_CrPresenter.FbEntryLookasideList, sizeof (CR_FRAMEBUFFER_ENTRY),
3884 0, /* size_t cbAlignment */
3885 UINT32_MAX, /* uint32_t cMaxObjects */
3886 NULL, /* PFNMEMCACHECTOR pfnCtor*/
3887 NULL, /* PFNMEMCACHEDTOR pfnDtor*/
3888 NULL, /* void *pvUser*/
3889 0 /* uint32_t fFlags*/
3890 );
3891 if (RT_SUCCESS(rc))
3892 {
3893 rc = RTMemCacheCreate(&g_CrPresenter.FbTexLookasideList, sizeof (CR_FBTEX),
3894 0, /* size_t cbAlignment */
3895 UINT32_MAX, /* uint32_t cMaxObjects */
3896 NULL, /* PFNMEMCACHECTOR pfnCtor*/
3897 NULL, /* PFNMEMCACHEDTOR pfnDtor*/
3898 NULL, /* void *pvUser*/
3899 0 /* uint32_t fFlags*/
3900 );
3901 if (RT_SUCCESS(rc))
3902 {
3903 rc = RTMemCacheCreate(&g_CrPresenter.CEntryLookasideList, sizeof (VBOXVR_SCR_COMPOSITOR_ENTRY),
3904 0, /* size_t cbAlignment */
3905 UINT32_MAX, /* uint32_t cMaxObjects */
3906 NULL, /* PFNMEMCACHECTOR pfnCtor*/
3907 NULL, /* PFNMEMCACHEDTOR pfnDtor*/
3908 NULL, /* void *pvUser*/
3909 0 /* uint32_t fFlags*/
3910 );
3911 if (RT_SUCCESS(rc))
3912 {
3913#endif
3914 rc = crPMgrModeModifyGlobal(CR_PMGR_MODE_WINDOW, 0);
3915 if (RT_SUCCESS(rc))
3916 return VINF_SUCCESS;
3917 else
3918 WARN(("crPMgrModeModifyGlobal failed rc %d", rc));
3919#ifndef VBOXVDBG_MEMCACHE_DISABLE
3920 RTMemCacheDestroy(g_CrPresenter.CEntryLookasideList);
3921 }
3922 else
3923 WARN(("RTMemCacheCreate failed rc %d", rc));
3924
3925 RTMemCacheDestroy(g_CrPresenter.FbTexLookasideList);
3926 }
3927 else
3928 WARN(("RTMemCacheCreate failed rc %d", rc));
3929
3930 RTMemCacheDestroy(g_CrPresenter.FbEntryLookasideList);
3931 }
3932 else
3933 WARN(("RTMemCacheCreate failed rc %d", rc));
3934#endif
3935 }
3936 else
3937 {
3938 WARN(("crAllocHashtable failed"));
3939 rc = VERR_NO_MEMORY;
3940 }
3941 return rc;
3942}
3943
3944void CrPMgrTerm()
3945{
3946 crPMgrModeModifyGlobal(0, CR_PMGR_MODE_ALL);
3947
3948 HCR_FRAMEBUFFER hFb;
3949
3950 for (hFb = CrPMgrFbGetFirstInitialized();
3951 hFb;
3952 hFb = CrPMgrFbGetNextInitialized(hFb))
3953 {
3954 uint32_t iFb = CrFbGetScreenInfo(hFb)->u32ViewIndex;
3955 CrFbDisplaySet(hFb, NULL);
3956 CR_FB_INFO *pFbInfo = &g_CrPresenter.aFbInfos[iFb];
3957 if (pFbInfo->pDpComposite)
3958 {
3959 delete pFbInfo->pDpComposite;
3960 pFbInfo->pDpComposite = NULL;
3961 }
3962
3963 CrFbTerm(hFb);
3964 }
3965
3966 crPMgrCleanUnusedDisplays();
3967
3968#ifndef VBOXVDBG_MEMCACHE_DISABLE
3969 RTMemCacheDestroy(g_CrPresenter.FbEntryLookasideList);
3970 RTMemCacheDestroy(g_CrPresenter.FbTexLookasideList);
3971 RTMemCacheDestroy(g_CrPresenter.CEntryLookasideList);
3972#endif
3973 crFreeHashtable(g_CrPresenter.pFbTexMap, NULL);
3974
3975 if (g_CrPresenter.pvTmpBuf)
3976 RTMemFree(g_CrPresenter.pvTmpBuf);
3977
3978 if (g_CrPresenter.pvTmpBuf2)
3979 RTMemFree(g_CrPresenter.pvTmpBuf2);
3980
3981 memset(&g_CrPresenter, 0, sizeof (g_CrPresenter));
3982}
3983
3984HCR_FRAMEBUFFER CrPMgrFbGet(uint32_t idFb)
3985{
3986 if (idFb >= CR_MAX_GUEST_MONITORS)
3987 {
3988 WARN(("invalid idFb %d", idFb));
3989 return NULL;
3990 }
3991
3992 if (!CrFBmIsSet(&g_CrPresenter.FramebufferInitMap, idFb))
3993 {
3994 CrFbInit(&g_CrPresenter.aFramebuffers[idFb], idFb);
3995 CrFBmSetAtomic(&g_CrPresenter.FramebufferInitMap, idFb);
3996 }
3997 else
3998 Assert(g_CrPresenter.aFramebuffers[idFb].ScreenInfo.u32ViewIndex == idFb);
3999
4000 return &g_CrPresenter.aFramebuffers[idFb];
4001}
4002
4003HCR_FRAMEBUFFER CrPMgrFbGetInitialized(uint32_t idFb)
4004{
4005 if (idFb >= CR_MAX_GUEST_MONITORS)
4006 {
4007 WARN(("invalid idFb %d", idFb));
4008 return NULL;
4009 }
4010
4011 if (!CrFBmIsSet(&g_CrPresenter.FramebufferInitMap, idFb))
4012 {
4013 return NULL;
4014 }
4015 else
4016 Assert(g_CrPresenter.aFramebuffers[idFb].ScreenInfo.u32ViewIndex == idFb);
4017
4018 return &g_CrPresenter.aFramebuffers[idFb];
4019}
4020
4021HCR_FRAMEBUFFER CrPMgrFbGetEnabled(uint32_t idFb)
4022{
4023 HCR_FRAMEBUFFER hFb = CrPMgrFbGetInitialized(idFb);
4024
4025 if(hFb && CrFbIsEnabled(hFb))
4026 return hFb;
4027
4028 return NULL;
4029}
4030
4031HCR_FRAMEBUFFER CrPMgrFbGetEnabledForScreen(uint32_t idScreen)
4032{
4033 if (idScreen >= (uint32_t)cr_server.screenCount)
4034 {
4035 WARN(("invalid target id"));
4036 return NULL;
4037 }
4038
4039 const CR_FBDISPLAY_INFO *pDpInfo = &g_CrPresenter.aDisplayInfos[idScreen];
4040 if (pDpInfo->iFb < 0)
4041 return NULL;
4042
4043 return CrPMgrFbGetEnabled(pDpInfo->iFb);
4044}
4045
4046static HCR_FRAMEBUFFER crPMgrFbGetNextEnabled(uint32_t i)
4047{
4048 for (;i < (uint32_t)cr_server.screenCount; ++i)
4049 {
4050 HCR_FRAMEBUFFER hFb = CrPMgrFbGetEnabled(i);
4051 if (hFb)
4052 return hFb;
4053 }
4054
4055 return NULL;
4056}
4057
4058static HCR_FRAMEBUFFER crPMgrFbGetNextInitialized(uint32_t i)
4059{
4060 for (;i < (uint32_t)cr_server.screenCount; ++i)
4061 {
4062 HCR_FRAMEBUFFER hFb = CrPMgrFbGetInitialized(i);
4063 if (hFb)
4064 return hFb;
4065 }
4066
4067 return NULL;
4068}
4069
4070HCR_FRAMEBUFFER CrPMgrFbGetFirstEnabled()
4071{
4072 HCR_FRAMEBUFFER hFb = crPMgrFbGetNextEnabled(0);
4073// if (!hFb)
4074// WARN(("no enabled framebuffer found"));
4075 return hFb;
4076}
4077
4078HCR_FRAMEBUFFER CrPMgrFbGetNextEnabled(HCR_FRAMEBUFFER hFb)
4079{
4080 return crPMgrFbGetNextEnabled(hFb->ScreenInfo.u32ViewIndex+1);
4081}
4082
4083HCR_FRAMEBUFFER CrPMgrFbGetFirstInitialized()
4084{
4085 HCR_FRAMEBUFFER hFb = crPMgrFbGetNextInitialized(0);
4086// if (!hFb)
4087// WARN(("no initialized framebuffer found"));
4088 return hFb;
4089}
4090
4091HCR_FRAMEBUFFER CrPMgrFbGetNextInitialized(HCR_FRAMEBUFFER hFb)
4092{
4093 return crPMgrFbGetNextInitialized(hFb->ScreenInfo.u32ViewIndex+1);
4094}
4095
4096HCR_FRAMEBUFFER CrPMgrFbGetEnabledByVramStart(VBOXCMDVBVAOFFSET offVRAM)
4097{
4098 for (HCR_FRAMEBUFFER hFb = CrPMgrFbGetFirstEnabled();
4099 hFb;
4100 hFb = CrPMgrFbGetNextEnabled(hFb))
4101 {
4102 const VBVAINFOSCREEN *pScreen = CrFbGetScreenInfo(hFb);
4103 if (pScreen->u32StartOffset == offVRAM)
4104 return hFb;
4105 }
4106
4107 return NULL;
4108}
4109
4110
4111static uint32_t crPMgrModeAdjustVal(uint32_t u32Mode)
4112{
4113 u32Mode = CR_PMGR_MODE_ALL & u32Mode;
4114 if (CR_PMGR_MODE_ROOTVR & u32Mode)
4115 u32Mode &= ~CR_PMGR_MODE_WINDOW;
4116 return u32Mode;
4117}
4118
4119int CrPMgrScreenChanged(uint32_t idScreen)
4120{
4121 if (idScreen >= CR_MAX_GUEST_MONITORS)
4122 {
4123 WARN(("invalid idScreen %d", idScreen));
4124 return VERR_INVALID_PARAMETER;
4125 }
4126
4127 CR_FBDISPLAY_INFO *pDpInfo = &g_CrPresenter.aDisplayInfos[idScreen];
4128 if (pDpInfo->pDpWin)
4129 {
4130 HCR_FRAMEBUFFER hFb = pDpInfo->iFb >= 0 ? CrPMgrFbGet(pDpInfo->iFb) : NULL;
4131 if (hFb && CrFbIsUpdating(hFb))
4132 {
4133 WARN(("trying to update viewport while framebuffer is being updated"));
4134 return VERR_INVALID_STATE;
4135 }
4136
4137 int rc = pDpInfo->pDpWin->UpdateBegin(hFb);
4138 if (RT_SUCCESS(rc))
4139 {
4140 pDpInfo->pDpWin->reparent(cr_server.screen[idScreen].winID);
4141
4142 pDpInfo->pDpWin->UpdateEnd(hFb);
4143 }
4144 else
4145 WARN(("UpdateBegin failed %d", rc));
4146 }
4147
4148 return VINF_SUCCESS;
4149}
4150
4151int CrPMgrViewportUpdate(uint32_t idScreen)
4152{
4153 if (idScreen >= CR_MAX_GUEST_MONITORS)
4154 {
4155 WARN(("invalid idScreen %d", idScreen));
4156 return VERR_INVALID_PARAMETER;
4157 }
4158
4159 CR_FBDISPLAY_INFO *pDpInfo = &g_CrPresenter.aDisplayInfos[idScreen];
4160 if (pDpInfo->iFb >= 0)
4161 {
4162 HCR_FRAMEBUFFER hFb = CrPMgrFbGet(pDpInfo->iFb);
4163 if (CrFbIsUpdating(hFb))
4164 {
4165 WARN(("trying to update viewport while framebuffer is being updated"));
4166 return VERR_INVALID_STATE;
4167 }
4168
4169 int rc = pDpInfo->pDpWin->UpdateBegin(hFb);
4170 if (RT_SUCCESS(rc))
4171 {
4172 pDpInfo->pDpWin->setViewportRect(&cr_server.screenVieport[idScreen].Rect);
4173 pDpInfo->pDpWin->UpdateEnd(hFb);
4174 }
4175 else
4176 WARN(("UpdateBegin failed %d", rc));
4177 }
4178
4179 return VINF_SUCCESS;
4180}
4181
4182static int crPMgrFbDisconnectDisplay(HCR_FRAMEBUFFER hFb, CrFbDisplayBase *pDp)
4183{
4184 if (pDp->getFramebuffer() != hFb)
4185 return VINF_SUCCESS;
4186
4187 CrFbDisplayBase * pCurDp = (CrFbDisplayBase*)CrFbDisplayGet(hFb);
4188 if (!pCurDp)
4189 {
4190 WARN(("no display set, unexpected"));
4191 return VERR_INTERNAL_ERROR;
4192 }
4193
4194 if (pCurDp == pDp)
4195 {
4196 pDp->setFramebuffer(NULL);
4197 CrFbDisplaySet(hFb, NULL);
4198 return VINF_SUCCESS;
4199 }
4200
4201 uint32_t idFb = CrFbGetScreenInfo(hFb)->u32ViewIndex;
4202 CR_FB_INFO *pFbInfo = &g_CrPresenter.aFbInfos[idFb];
4203 if (pFbInfo->pDpComposite != pCurDp)
4204 {
4205 WARN(("misconfig, expectig the curret framebuffer to be present, and thus composite is expected"));
4206 return VERR_INTERNAL_ERROR;
4207 }
4208
4209 if (pDp->getContainer() == pFbInfo->pDpComposite)
4210 {
4211 pFbInfo->pDpComposite->remove(pDp);
4212 uint32_t cDisplays = pFbInfo->pDpComposite->getDisplayCount();
4213 if (cDisplays <= 1)
4214 {
4215 Assert(cDisplays == 1);
4216 CrFbDisplayBase *pDpFirst = pFbInfo->pDpComposite->first();
4217 if (pDpFirst)
4218 pFbInfo->pDpComposite->remove(pDpFirst, false);
4219 CrFbDisplaySet(hFb, pDpFirst);
4220 }
4221 return VINF_SUCCESS;
4222 }
4223
4224 WARN(("misconfig"));
4225 return VERR_INTERNAL_ERROR;
4226}
4227
4228static int crPMgrFbConnectDisplay(HCR_FRAMEBUFFER hFb, CrFbDisplayBase *pDp)
4229{
4230 if (pDp->getFramebuffer() == hFb)
4231 return VINF_SUCCESS;
4232
4233 CrFbDisplayBase * pCurDp = (CrFbDisplayBase*)CrFbDisplayGet(hFb);
4234 if (!pCurDp)
4235 {
4236 pDp->setFramebuffer(hFb);
4237 CrFbDisplaySet(hFb, pDp);
4238 return VINF_SUCCESS;
4239 }
4240
4241 if (pCurDp == pDp)
4242 {
4243 WARN(("misconfig, current framebuffer is not expected to be set"));
4244 return VERR_INTERNAL_ERROR;
4245 }
4246
4247 uint32_t idFb = CrFbGetScreenInfo(hFb)->u32ViewIndex;
4248 CR_FB_INFO *pFbInfo = &g_CrPresenter.aFbInfos[idFb];
4249 if (pFbInfo->pDpComposite != pCurDp)
4250 {
4251 if (!pFbInfo->pDpComposite)
4252 {
4253 pFbInfo->pDpComposite = new CrFbDisplayComposite();
4254 pFbInfo->pDpComposite->setFramebuffer(hFb);
4255 }
4256
4257 pFbInfo->pDpComposite->add(pCurDp);
4258 CrFbDisplaySet(hFb, pFbInfo->pDpComposite);
4259 }
4260
4261 pFbInfo->pDpComposite->add(pDp);
4262 return VINF_SUCCESS;
4263}
4264
4265static int crPMgrFbDisconnectTarget(HCR_FRAMEBUFFER hFb, uint32_t i)
4266{
4267 uint32_t idFb = CrFbGetScreenInfo(hFb)->u32ViewIndex;
4268 CR_FB_INFO *pFbInfo = &g_CrPresenter.aFbInfos[idFb];
4269 CR_FBDISPLAY_INFO *pDpInfo = &g_CrPresenter.aDisplayInfos[i];
4270 if (pDpInfo->iFb != idFb)
4271 {
4272 WARN(("target not connected"));
4273 Assert(!ASMBitTest(pFbInfo->aTargetMap, i));
4274 return VINF_SUCCESS;
4275 }
4276
4277 Assert(ASMBitTest(pFbInfo->aTargetMap, i));
4278
4279 int rc = VINF_SUCCESS;
4280 if (pDpInfo->pDpVrdp)
4281 {
4282 rc = crPMgrFbDisconnectDisplay(hFb, pDpInfo->pDpVrdp);
4283 if (RT_FAILURE(rc))
4284 {
4285 WARN(("crPMgrFbDisconnectDisplay failed %d", rc));
4286 return rc;
4287 }
4288 }
4289
4290 if (pDpInfo->pDpWinRootVr)
4291 {
4292 rc = crPMgrFbDisconnectDisplay(hFb, pDpInfo->pDpWinRootVr);
4293 if (RT_FAILURE(rc))
4294 {
4295 WARN(("crPMgrFbDisconnectDisplay failed %d", rc));
4296 return rc;
4297 }
4298 }
4299
4300 if (pDpInfo->pDpWin)
4301 {
4302 rc = crPMgrFbDisconnectDisplay(hFb, pDpInfo->pDpWin);
4303 if (RT_FAILURE(rc))
4304 {
4305 WARN(("crPMgrFbDisconnectDisplay failed %d", rc));
4306 return rc;
4307 }
4308 }
4309
4310 ASMBitClear(pFbInfo->aTargetMap, i);
4311 pDpInfo->iFb = -1;
4312
4313 return VINF_SUCCESS;
4314}
4315
4316static void crPMgrDpWinRootVrCreate(CR_FBDISPLAY_INFO *pDpInfo)
4317{
4318 if (!pDpInfo->pDpWinRootVr)
4319 {
4320 CrFbWindow *pWin = NULL;
4321 if (pDpInfo->pDpWin)
4322 {
4323 pWin = pDpInfo->pDpWin->windowDetach();
4324 CRASSERT(pWin);
4325 delete pDpInfo->pDpWin;
4326 pDpInfo->pDpWin = NULL;
4327 }
4328 else
4329 pWin = new CrFbWindow(cr_server.screen[pDpInfo->u32Id].winID);
4330
4331 pDpInfo->pDpWinRootVr = new CrFbDisplayWindowRootVr(pWin, &cr_server.screenVieport[pDpInfo->u32Id].Rect);
4332 pDpInfo->pDpWin = pDpInfo->pDpWinRootVr;
4333 }
4334}
4335
4336static void crPMgrDpWinCreate(CR_FBDISPLAY_INFO *pDpInfo)
4337{
4338 CrFbWindow *pWin = NULL;
4339 if (pDpInfo->pDpWinRootVr)
4340 {
4341 CRASSERT(pDpInfo->pDpWinRootVr == pDpInfo->pDpWin);
4342 pWin = pDpInfo->pDpWin->windowDetach();
4343 CRASSERT(pWin);
4344 delete pDpInfo->pDpWinRootVr;
4345 pDpInfo->pDpWinRootVr = NULL;
4346 pDpInfo->pDpWin = NULL;
4347 }
4348
4349 if (!pDpInfo->pDpWin)
4350 {
4351 if (!pWin)
4352 pWin = new CrFbWindow(cr_server.screen[pDpInfo->u32Id].winID);
4353
4354 pDpInfo->pDpWin = new CrFbDisplayWindow(pWin, &cr_server.screenVieport[pDpInfo->u32Id].Rect);
4355 }
4356}
4357
4358static int crPMgrFbDisconnectTargetDisplays(HCR_FRAMEBUFFER hFb, CR_FBDISPLAY_INFO *pDpInfo, uint32_t u32ModeRemove)
4359{
4360 int rc = VINF_SUCCESS;
4361 if (u32ModeRemove & CR_PMGR_MODE_ROOTVR)
4362 {
4363 CRASSERT(pDpInfo->pDpWinRootVr);
4364 CRASSERT(pDpInfo->pDpWin == pDpInfo->pDpWinRootVr);
4365 rc = crPMgrFbDisconnectDisplay(hFb, pDpInfo->pDpWinRootVr);
4366 if (RT_FAILURE(rc))
4367 {
4368 WARN(("crPMgrFbDisconnectDisplay pDpWinRootVr failed %d", rc));
4369 return rc;
4370 }
4371 }
4372 else if (u32ModeRemove & CR_PMGR_MODE_WINDOW)
4373 {
4374 CRASSERT(!pDpInfo->pDpWinRootVr);
4375 CRASSERT(pDpInfo->pDpWin);
4376 rc = crPMgrFbDisconnectDisplay(hFb, pDpInfo->pDpWin);
4377 if (RT_FAILURE(rc))
4378 {
4379 WARN(("crPMgrFbDisconnectDisplay pDpWin failed %d", rc));
4380 return rc;
4381 }
4382 }
4383
4384 if (u32ModeRemove & CR_PMGR_MODE_VRDP)
4385 {
4386 CRASSERT(pDpInfo->pDpVrdp);
4387 rc = crPMgrFbDisconnectDisplay(hFb, pDpInfo->pDpVrdp);
4388 if (RT_FAILURE(rc))
4389 {
4390 WARN(("crPMgrFbDisconnectDisplay pDpVrdp failed %d", rc));
4391 return rc;
4392 }
4393 }
4394
4395 return VINF_SUCCESS;
4396}
4397
4398static int crPMgrFbConnectTargetDisplays(HCR_FRAMEBUFFER hFb, CR_FBDISPLAY_INFO *pDpInfo, uint32_t u32ModeAdd)
4399{
4400 int rc = VINF_SUCCESS;
4401
4402 if (u32ModeAdd & CR_PMGR_MODE_ROOTVR)
4403 {
4404 crPMgrDpWinRootVrCreate(pDpInfo);
4405
4406 rc = crPMgrFbConnectDisplay(hFb, pDpInfo->pDpWinRootVr);
4407 if (RT_FAILURE(rc))
4408 {
4409 WARN(("crPMgrFbConnectDisplay pDpWinRootVr failed %d", rc));
4410 return rc;
4411 }
4412 }
4413 else if (u32ModeAdd & CR_PMGR_MODE_WINDOW)
4414 {
4415 crPMgrDpWinCreate(pDpInfo);
4416
4417 rc = crPMgrFbConnectDisplay(hFb, pDpInfo->pDpWin);
4418 if (RT_FAILURE(rc))
4419 {
4420 WARN(("crPMgrFbConnectDisplay pDpWin failed %d", rc));
4421 return rc;
4422 }
4423 }
4424
4425 if (u32ModeAdd & CR_PMGR_MODE_VRDP)
4426 {
4427 if (!pDpInfo->pDpVrdp)
4428 pDpInfo->pDpVrdp = new CrFbDisplayVrdp();
4429
4430 rc = crPMgrFbConnectDisplay(hFb, pDpInfo->pDpVrdp);
4431 if (RT_FAILURE(rc))
4432 {
4433 WARN(("crPMgrFbConnectDisplay pDpVrdp failed %d", rc));
4434 return rc;
4435 }
4436 }
4437
4438 return VINF_SUCCESS;
4439}
4440
4441static int crPMgrFbConnectTarget(HCR_FRAMEBUFFER hFb, uint32_t i)
4442{
4443 uint32_t idFb = CrFbGetScreenInfo(hFb)->u32ViewIndex;
4444 CR_FB_INFO *pFbInfo = &g_CrPresenter.aFbInfos[idFb];
4445 CR_FBDISPLAY_INFO *pDpInfo = &g_CrPresenter.aDisplayInfos[i];
4446 if (pDpInfo->iFb == idFb)
4447 {
4448 WARN(("target not connected"));
4449 Assert(ASMBitTest(pFbInfo->aTargetMap, i));
4450 return VINF_SUCCESS;
4451 }
4452
4453 Assert(!ASMBitTest(pFbInfo->aTargetMap, i));
4454
4455 int rc = VINF_SUCCESS;
4456
4457 if (pDpInfo->iFb != -1)
4458 {
4459 Assert(pDpInfo->iFb < cr_server.screenCount);
4460 HCR_FRAMEBUFFER hAssignedFb = CrPMgrFbGet(pDpInfo->iFb);
4461 Assert(hAssignedFb);
4462 rc = crPMgrFbDisconnectTarget(hAssignedFb, i);
4463 if (RT_FAILURE(rc))
4464 {
4465 WARN(("crPMgrFbDisconnectTarget failed %d", rc));
4466 return rc;
4467 }
4468 }
4469
4470 rc = crPMgrFbConnectTargetDisplays(hFb, pDpInfo, g_CrPresenter.u32DisplayMode);
4471 if (RT_FAILURE(rc))
4472 {
4473 WARN(("crPMgrFbConnectTargetDisplays failed %d", rc));
4474 return rc;
4475 }
4476
4477 ASMBitSet(pFbInfo->aTargetMap, i);
4478 pDpInfo->iFb = idFb;
4479
4480 return VINF_SUCCESS;
4481}
4482
4483static int crPMgrFbDisconnect(HCR_FRAMEBUFFER hFb, const uint32_t *pTargetMap)
4484{
4485 int rc = VINF_SUCCESS;
4486 for (int i = ASMBitFirstSet(pTargetMap, cr_server.screenCount);
4487 i >= 0;
4488 i = ASMBitNextSet(pTargetMap, cr_server.screenCount, i))
4489 {
4490 rc = crPMgrFbDisconnectTarget(hFb, (uint32_t)i);
4491 if (RT_FAILURE(rc))
4492 {
4493 WARN(("crPMgrFbDisconnectTarget failed %d", rc));
4494 return rc;
4495 }
4496 }
4497
4498 return VINF_SUCCESS;
4499}
4500
4501static int crPMgrFbConnect(HCR_FRAMEBUFFER hFb, const uint32_t *pTargetMap)
4502{
4503 int rc = VINF_SUCCESS;
4504 for (int i = ASMBitFirstSet(pTargetMap, cr_server.screenCount);
4505 i >= 0;
4506 i = ASMBitNextSet(pTargetMap, cr_server.screenCount, i))
4507 {
4508 rc = crPMgrFbConnectTarget(hFb, (uint32_t)i);
4509 if (RT_FAILURE(rc))
4510 {
4511 WARN(("crPMgrFbConnectTarget failed %d", rc));
4512 return rc;
4513 }
4514 }
4515
4516 return VINF_SUCCESS;
4517}
4518
4519static int crPMgrModeModifyTarget(HCR_FRAMEBUFFER hFb, uint32_t iDisplay, uint32_t u32ModeAdd, uint32_t u32ModeRemove)
4520{
4521 CR_FBDISPLAY_INFO *pDpInfo = &g_CrPresenter.aDisplayInfos[iDisplay];
4522 int rc = crPMgrFbDisconnectTargetDisplays(hFb, pDpInfo, u32ModeRemove);
4523 if (RT_FAILURE(rc))
4524 {
4525 WARN(("crPMgrFbDisconnectTargetDisplays failed %d", rc));
4526 return rc;
4527 }
4528
4529 rc = crPMgrFbConnectTargetDisplays(hFb, pDpInfo, u32ModeAdd);
4530 if (RT_FAILURE(rc))
4531 {
4532 WARN(("crPMgrFbConnectTargetDisplays failed %d", rc));
4533 return rc;
4534 }
4535
4536 return VINF_SUCCESS;
4537}
4538
4539static int crPMgrModeModify(HCR_FRAMEBUFFER hFb, uint32_t u32ModeAdd, uint32_t u32ModeRemove)
4540{
4541 int rc = VINF_SUCCESS;
4542 uint32_t idFb = CrFbGetScreenInfo(hFb)->u32ViewIndex;
4543 CR_FB_INFO *pFbInfo = &g_CrPresenter.aFbInfos[idFb];
4544 for (int i = ASMBitFirstSet(pFbInfo->aTargetMap, cr_server.screenCount);
4545 i >= 0;
4546 i = ASMBitNextSet(pFbInfo->aTargetMap, cr_server.screenCount, i))
4547 {
4548 rc = crPMgrModeModifyTarget(hFb, (uint32_t)i, u32ModeAdd, u32ModeRemove);
4549 if (RT_FAILURE(rc))
4550 {
4551 WARN(("crPMgrModeModifyTarget failed %d", rc));
4552 return rc;
4553 }
4554 }
4555
4556 return VINF_SUCCESS;
4557}
4558
4559static void crPMgrCleanUnusedDisplays()
4560{
4561 for (int i = 0; i < cr_server.screenCount; ++i)
4562 {
4563 CR_FBDISPLAY_INFO *pDpInfo = &g_CrPresenter.aDisplayInfos[i];
4564
4565 if (pDpInfo->pDpWinRootVr)
4566 {
4567 if (!pDpInfo->pDpWinRootVr->getFramebuffer())
4568 {
4569 delete pDpInfo->pDpWinRootVr;
4570 pDpInfo->pDpWinRootVr = NULL;
4571 pDpInfo->pDpWin = NULL;
4572 }
4573 else
4574 WARN(("pDpWinRootVr is used"));
4575 }
4576 else if (pDpInfo->pDpWin)
4577 {
4578 if (!pDpInfo->pDpWin->getFramebuffer())
4579 {
4580 delete pDpInfo->pDpWin;
4581 pDpInfo->pDpWin = NULL;
4582 }
4583 else
4584 WARN(("pDpWin is used"));
4585 }
4586
4587 if (pDpInfo->pDpVrdp)
4588 {
4589 if (!pDpInfo->pDpVrdp->getFramebuffer())
4590 {
4591 delete pDpInfo->pDpVrdp;
4592 pDpInfo->pDpVrdp = NULL;
4593 }
4594 else
4595 WARN(("pDpVrdp is used"));
4596 }
4597 }
4598}
4599
4600static int crPMgrModeModifyGlobal(uint32_t u32ModeAdd, uint32_t u32ModeRemove)
4601{
4602 uint32_t u32InternalMode = g_CrPresenter.fEnabled ? g_CrPresenter.u32DisplayMode : g_CrPresenter.u32DisabledDisplayMode;
4603
4604 u32ModeRemove = ((u32ModeRemove | crPMgrModeAdjustVal(u32ModeRemove)) & CR_PMGR_MODE_ALL);
4605 u32ModeAdd = crPMgrModeAdjustVal(u32ModeAdd);
4606 u32ModeRemove &= u32InternalMode;
4607 u32ModeAdd &= ~(u32ModeRemove | u32InternalMode);
4608 uint32_t u32ModeResulting = ((u32InternalMode | u32ModeAdd) & ~u32ModeRemove);
4609 uint32_t u32Tmp = crPMgrModeAdjustVal(u32ModeResulting);
4610 if (u32Tmp != u32ModeResulting)
4611 {
4612 u32ModeAdd |= (u32Tmp & ~u32ModeResulting);
4613 u32ModeRemove |= (~u32Tmp & u32ModeResulting);
4614 u32ModeResulting = u32Tmp;
4615 Assert(u32ModeResulting == ((u32InternalMode | u32ModeAdd) & ~u32ModeRemove));
4616 }
4617 if (!u32ModeRemove && !u32ModeAdd)
4618 return VINF_SUCCESS;
4619
4620 uint32_t u32DisplayMode = (g_CrPresenter.u32DisplayMode | u32ModeAdd) & ~u32ModeRemove;
4621 if (!g_CrPresenter.fEnabled)
4622 {
4623 Assert(g_CrPresenter.u32DisplayMode == 0);
4624 g_CrPresenter.u32DisabledDisplayMode = u32DisplayMode;
4625 return VINF_SUCCESS;
4626 }
4627
4628 g_CrPresenter.u32DisplayMode = u32DisplayMode;
4629
4630 /* disabled framebuffers may still have displays attached */
4631 for (HCR_FRAMEBUFFER hFb = CrPMgrFbGetFirstInitialized();
4632 hFb;
4633 hFb = CrPMgrFbGetNextInitialized(hFb))
4634 {
4635 crPMgrModeModify(hFb, u32ModeAdd, u32ModeRemove);
4636 }
4637
4638 return VINF_SUCCESS;
4639}
4640
4641int CrPMgrModeVrdp(bool fEnable)
4642{
4643 uint32_t u32ModeAdd, u32ModeRemove;
4644 if (fEnable)
4645 {
4646 u32ModeAdd = CR_PMGR_MODE_VRDP;
4647 u32ModeRemove = 0;
4648 }
4649 else
4650 {
4651 u32ModeAdd = 0;
4652 u32ModeRemove = CR_PMGR_MODE_VRDP;
4653 }
4654 return crPMgrModeModifyGlobal(u32ModeAdd, u32ModeRemove);
4655}
4656
4657int CrPMgrModeRootVr(bool fEnable)
4658{
4659 uint32_t u32ModeAdd, u32ModeRemove;
4660 if (fEnable)
4661 {
4662 u32ModeAdd = CR_PMGR_MODE_ROOTVR;
4663 u32ModeRemove = CR_PMGR_MODE_WINDOW;
4664 }
4665 else
4666 {
4667 u32ModeAdd = CR_PMGR_MODE_WINDOW;
4668 u32ModeRemove = CR_PMGR_MODE_ROOTVR;
4669 }
4670
4671 return crPMgrModeModifyGlobal(u32ModeAdd, u32ModeRemove);
4672}
4673
4674int CrPMgrModeWinVisible(bool fEnable)
4675{
4676 if (!g_CrPresenter.fWindowsForceHidden == !!fEnable)
4677 return VINF_SUCCESS;
4678
4679 g_CrPresenter.fWindowsForceHidden = !fEnable;
4680
4681 for (int i = 0; i < cr_server.screenCount; ++i)
4682 {
4683 CR_FBDISPLAY_INFO *pDpInfo = &g_CrPresenter.aDisplayInfos[i];
4684
4685 if (pDpInfo->pDpWin)
4686 pDpInfo->pDpWin->winVisibilityChanged();
4687 }
4688
4689 return VINF_SUCCESS;
4690}
4691
4692int CrPMgrRootVrUpdate()
4693{
4694 for (HCR_FRAMEBUFFER hFb = CrPMgrFbGetFirstEnabled();
4695 hFb;
4696 hFb = CrPMgrFbGetNextEnabled(hFb))
4697 {
4698 if (!CrFbHas3DData(hFb))
4699 continue;
4700
4701 uint32_t idFb = CrFbGetScreenInfo(hFb)->u32ViewIndex;
4702 CR_FB_INFO *pFbInfo = &g_CrPresenter.aFbInfos[idFb];
4703 int rc = CrFbUpdateBegin(hFb);
4704 if (RT_SUCCESS(rc))
4705 {
4706 for (int i = ASMBitFirstSet(pFbInfo->aTargetMap, cr_server.screenCount);
4707 i >= 0;
4708 i = ASMBitNextSet(pFbInfo->aTargetMap, cr_server.screenCount, i))
4709 {
4710 CR_FBDISPLAY_INFO *pDpInfo = &g_CrPresenter.aDisplayInfos[i];
4711 Assert(pDpInfo->iFb == (int32_t)idFb);
4712
4713 pDpInfo->pDpWinRootVr->RegionsChanged(hFb);
4714 }
4715
4716 CrFbUpdateEnd(hFb);
4717 }
4718 else
4719 WARN(("CrFbUpdateBegin failed %d", rc));
4720 }
4721
4722 return VINF_SUCCESS;
4723}
4724
4725/*helper function that calls CrFbUpdateBegin for all enabled framebuffers */
4726int CrPMgrHlpGlblUpdateBegin(CR_FBMAP *pMap)
4727{
4728 CrFBmInit(pMap);
4729 for (HCR_FRAMEBUFFER hFb = CrPMgrFbGetFirstEnabled();
4730 hFb;
4731 hFb = CrPMgrFbGetNextEnabled(hFb))
4732 {
4733 int rc = CrFbUpdateBegin(hFb);
4734 if (!RT_SUCCESS(rc))
4735 {
4736 WARN(("UpdateBegin failed, rc %d", rc));
4737 for (HCR_FRAMEBUFFER hTmpFb = CrPMgrFbGetFirstEnabled();
4738 hFb != hTmpFb;
4739 hTmpFb = CrPMgrFbGetNextEnabled(hTmpFb))
4740 {
4741 CrFbUpdateEnd(hTmpFb);
4742 CrFBmClear(pMap, CrFbGetScreenInfo(hFb)->u32ViewIndex);
4743 }
4744 return rc;
4745 }
4746
4747 CrFBmSet(pMap, CrFbGetScreenInfo(hFb)->u32ViewIndex);
4748 }
4749
4750 return VINF_SUCCESS;
4751}
4752
4753/*helper function that calls CrFbUpdateEnd for all framebuffers being updated */
4754void CrPMgrHlpGlblUpdateEnd(CR_FBMAP *pMap)
4755{
4756 for (uint32_t i = 0; i < (uint32_t)cr_server.screenCount; ++i)
4757 {
4758 if (!CrFBmIsSet(pMap, i))
4759 continue;
4760
4761 HCR_FRAMEBUFFER hFb = CrPMgrFbGetInitialized(i);
4762 CRASSERT(hFb);
4763 CrFbUpdateEnd(hFb);
4764 }
4765}
4766
4767int CrPMgrResize(const struct VBVAINFOSCREEN *pScreen, void *pvVRAM, const uint32_t *pTargetMap)
4768{
4769 int rc = VINF_SUCCESS;
4770
4771 if (pScreen->u32ViewIndex == 0xffffffff)
4772 {
4773 /* this is just a request to disable targets, search and disable */
4774 for (int i = ASMBitFirstSet(pTargetMap, cr_server.screenCount);
4775 i >= 0;
4776 i = ASMBitNextSet(pTargetMap, cr_server.screenCount, i))
4777 {
4778 CR_FBDISPLAY_INFO *pDpInfo = &g_CrPresenter.aDisplayInfos[i];
4779 if (pDpInfo->iFb < 0)
4780 continue;
4781
4782 Assert(pDpInfo->iFb < cr_server.screenCount);
4783 HCR_FRAMEBUFFER hAssignedFb = CrPMgrFbGet(pDpInfo->iFb);
4784
4785 rc = crPMgrFbDisconnectTarget(hAssignedFb, (uint32_t)i);
4786 if (RT_FAILURE(rc))
4787 {
4788 WARN(("crPMgrFbDisconnectTarget failed %d", rc));
4789 return rc;
4790 }
4791 }
4792
4793 return VINF_SUCCESS;
4794 }
4795
4796 HCR_FRAMEBUFFER hFb = CrPMgrFbGet(pScreen->u32ViewIndex);
4797 if (!hFb)
4798 {
4799 WARN(("CrPMgrFbGet failed"));
4800 return VERR_INVALID_PARAMETER;
4801 }
4802
4803 const VBVAINFOSCREEN *pFbScreen = CrFbGetScreenInfo(hFb);
4804 bool fFbInfoChanged = true;
4805
4806 if (!memcmp(pFbScreen, pScreen, sizeof (*pScreen)))
4807 {
4808 if (!pvVRAM || pvVRAM == CrFbGetVRAM(hFb))
4809 fFbInfoChanged = false;
4810 }
4811
4812 CR_FB_INFO *pFbInfo = &g_CrPresenter.aFbInfos[pScreen->u32ViewIndex];
4813
4814 VBOXCMDVBVA_SCREENMAP_DECL(uint32_t, aRemovedTargetMap);
4815 VBOXCMDVBVA_SCREENMAP_DECL(uint32_t, aAddedTargetMap);
4816
4817 bool fDisplaysAdded = false, fDisplaysRemoved = false;
4818
4819 memcpy(aRemovedTargetMap, pFbInfo->aTargetMap, sizeof (aRemovedTargetMap));
4820
4821 if (pScreen->u16Flags & VBVA_SCREEN_F_DISABLED)
4822 {
4823 /* so far there is no need in keeping displays attached to disabled Framebffer,
4824 * just disconnect everything */
4825 for (int i = 0; i < RT_ELEMENTS(aRemovedTargetMap); ++i)
4826 {
4827 if (aRemovedTargetMap[i])
4828 {
4829 fDisplaysRemoved = true;
4830 break;
4831 }
4832 }
4833
4834 memset(aAddedTargetMap, 0, sizeof (aAddedTargetMap));
4835 }
4836 else
4837 {
4838 for (int i = 0; i < RT_ELEMENTS(aRemovedTargetMap); ++i)
4839 {
4840 aRemovedTargetMap[i] = (aRemovedTargetMap[i] & ~pTargetMap[i]);
4841 if (aRemovedTargetMap[i])
4842 fDisplaysRemoved = true;
4843 }
4844
4845 memcpy(aAddedTargetMap, pFbInfo->aTargetMap, sizeof (aAddedTargetMap));
4846 for (int i = 0; i < RT_ELEMENTS(aAddedTargetMap); ++i)
4847 {
4848 aAddedTargetMap[i] = (pTargetMap[i] & ~aAddedTargetMap[i]);
4849 if (aAddedTargetMap[i])
4850 fDisplaysAdded = true;
4851 }
4852 }
4853
4854 if (!fFbInfoChanged && !fDisplaysRemoved && !fDisplaysAdded)
4855 {
4856 crDebug("resize: no changes");
4857 return VINF_SUCCESS;
4858 }
4859
4860 if (fDisplaysRemoved)
4861 {
4862 rc = crPMgrFbDisconnect(hFb, aRemovedTargetMap);
4863 if (RT_FAILURE(rc))
4864 {
4865 WARN(("crPMgrFbDisconnect failed %d", rc));
4866 return rc;
4867 }
4868 }
4869
4870 if (fFbInfoChanged)
4871 {
4872 rc = CrFbUpdateBegin(hFb);
4873 if (!RT_SUCCESS(rc))
4874 {
4875 WARN(("CrFbUpdateBegin failed %d", rc));
4876 return rc;
4877 }
4878
4879 crVBoxServerMuralFbResizeBegin(hFb);
4880
4881 rc = CrFbResize(hFb, pScreen, pvVRAM);
4882 if (!RT_SUCCESS(rc))
4883 {
4884 WARN(("CrFbResize failed %d", rc));
4885 }
4886
4887 crVBoxServerMuralFbResizeEnd(hFb);
4888
4889 CrFbUpdateEnd(hFb);
4890 }
4891
4892 if (fDisplaysAdded)
4893 {
4894 rc = crPMgrFbConnect(hFb, aAddedTargetMap);
4895 if (RT_FAILURE(rc))
4896 {
4897 WARN(("crPMgrFbConnect failed %d", rc));
4898 return rc;
4899 }
4900 }
4901
4902 return VINF_SUCCESS;
4903}
4904
4905int CrFbEntrySaveState(CR_FRAMEBUFFER *pFb, CR_FRAMEBUFFER_ENTRY *hEntry, PSSMHANDLE pSSM)
4906{
4907 const struct VBOXVR_SCR_COMPOSITOR_ENTRY *pEntry = CrFbEntryGetCompositorEntry(hEntry);
4908 CR_TEXDATA *pTexData = CrVrScrCompositorEntryTexGet(pEntry);
4909 CR_FBTEX *pFbTex = PCR_FBTEX_FROM_TEX(pTexData);
4910 int rc = SSMR3PutU32(pSSM, pFbTex->pTobj->id);
4911 AssertRCReturn(rc, rc);
4912 uint32_t u32 = 0;
4913
4914 u32 = CrVrScrCompositorEntryFlagsGet(pEntry);
4915 rc = SSMR3PutU32(pSSM, u32);
4916 AssertRCReturn(rc, rc);
4917
4918 const RTRECT *pRect = CrVrScrCompositorEntryRectGet(pEntry);
4919
4920 rc = SSMR3PutS32(pSSM, pRect->xLeft);
4921 AssertRCReturn(rc, rc);
4922 rc = SSMR3PutS32(pSSM, pRect->yTop);
4923 AssertRCReturn(rc, rc);
4924#if 0
4925 rc = SSMR3PutS32(pSSM, pRect->xRight);
4926 AssertRCReturn(rc, rc);
4927 rc = SSMR3PutS32(pSSM, pRect->yBottom);
4928 AssertRCReturn(rc, rc);
4929#endif
4930
4931 rc = CrVrScrCompositorEntryRegionsGet(&pFb->Compositor, pEntry, &u32, NULL, NULL, &pRect);
4932 AssertRCReturn(rc, rc);
4933
4934 rc = SSMR3PutU32(pSSM, u32);
4935 AssertRCReturn(rc, rc);
4936
4937 if (u32)
4938 {
4939 rc = SSMR3PutMem(pSSM, pRect, u32 * sizeof (*pRect));
4940 AssertRCReturn(rc, rc);
4941 }
4942 return rc;
4943}
4944
4945int CrFbSaveState(CR_FRAMEBUFFER *pFb, PSSMHANDLE pSSM)
4946{
4947 VBOXVR_SCR_COMPOSITOR_CONST_ITERATOR Iter;
4948 CrVrScrCompositorConstIterInit(&pFb->Compositor, &Iter);
4949 const VBOXVR_SCR_COMPOSITOR_ENTRY *pEntry;
4950 uint32_t u32 = 0;
4951 while ((pEntry = CrVrScrCompositorConstIterNext(&Iter)) != NULL)
4952 {
4953 CR_TEXDATA *pTexData = CrVrScrCompositorEntryTexGet(pEntry);
4954 CRASSERT(pTexData);
4955 CR_FBTEX *pFbTex = PCR_FBTEX_FROM_TEX(pTexData);
4956 if (pFbTex->pTobj)
4957 ++u32;
4958 }
4959
4960 int rc = SSMR3PutU32(pSSM, u32);
4961 AssertRCReturn(rc, rc);
4962
4963 CrVrScrCompositorConstIterInit(&pFb->Compositor, &Iter);
4964
4965 while ((pEntry = CrVrScrCompositorConstIterNext(&Iter)) != NULL)
4966 {
4967 CR_TEXDATA *pTexData = CrVrScrCompositorEntryTexGet(pEntry);
4968 CR_FBTEX *pFbTex = PCR_FBTEX_FROM_TEX(pTexData);
4969 if (pFbTex->pTobj)
4970 {
4971 HCR_FRAMEBUFFER_ENTRY hEntry = CrFbEntryFromCompositorEntry(pEntry);
4972 rc = CrFbEntrySaveState(pFb, hEntry, pSSM);
4973 AssertRCReturn(rc, rc);
4974 }
4975 }
4976
4977 return VINF_SUCCESS;
4978}
4979
4980int CrPMgrSaveState(PSSMHANDLE pSSM)
4981{
4982 int rc;
4983 int cDisplays = 0, i;
4984
4985 for (i = 0; i < cr_server.screenCount; ++i)
4986 {
4987 if (CrPMgrFbGetEnabled(i))
4988 ++cDisplays;
4989 }
4990
4991 rc = SSMR3PutS32(pSSM, cDisplays);
4992 AssertRCReturn(rc, rc);
4993
4994 if (!cDisplays)
4995 return VINF_SUCCESS;
4996
4997 rc = SSMR3PutS32(pSSM, cr_server.screenCount);
4998 AssertRCReturn(rc, rc);
4999
5000 for (i = 0; i < cr_server.screenCount; ++i)
5001 {
5002 CR_FRAMEBUFFER *hFb = CrPMgrFbGetEnabled(i);
5003 if (hFb)
5004 {
5005 Assert(hFb->ScreenInfo.u32ViewIndex == i);
5006 rc = SSMR3PutU32(pSSM, hFb->ScreenInfo.u32ViewIndex);
5007 AssertRCReturn(rc, rc);
5008
5009 rc = SSMR3PutS32(pSSM, hFb->ScreenInfo.i32OriginX);
5010 AssertRCReturn(rc, rc);
5011
5012 rc = SSMR3PutS32(pSSM, hFb->ScreenInfo.i32OriginY);
5013 AssertRCReturn(rc, rc);
5014
5015 rc = SSMR3PutU32(pSSM, hFb->ScreenInfo.u32StartOffset);
5016 AssertRCReturn(rc, rc);
5017
5018 rc = SSMR3PutU32(pSSM, hFb->ScreenInfo.u32LineSize);
5019 AssertRCReturn(rc, rc);
5020
5021 rc = SSMR3PutU32(pSSM, hFb->ScreenInfo.u32Width);
5022 AssertRCReturn(rc, rc);
5023
5024 rc = SSMR3PutU32(pSSM, hFb->ScreenInfo.u32Height);
5025 AssertRCReturn(rc, rc);
5026
5027 rc = SSMR3PutU16(pSSM, hFb->ScreenInfo.u16BitsPerPixel);
5028 AssertRCReturn(rc, rc);
5029
5030 rc = SSMR3PutU16(pSSM, hFb->ScreenInfo.u16Flags);
5031 AssertRCReturn(rc, rc);
5032
5033 rc = SSMR3PutU32(pSSM, hFb->ScreenInfo.u32StartOffset);
5034 AssertRCReturn(rc, rc);
5035
5036 CR_FB_INFO *pFbInfo = &g_CrPresenter.aFbInfos[hFb->ScreenInfo.u32ViewIndex];
5037 rc = SSMR3PutMem(pSSM, pFbInfo->aTargetMap, sizeof (pFbInfo->aTargetMap));
5038 AssertRCReturn(rc, rc);
5039
5040 rc = CrFbSaveState(hFb, pSSM);
5041 AssertRCReturn(rc, rc);
5042 }
5043 }
5044
5045 return VINF_SUCCESS;
5046}
5047
5048int CrFbEntryLoadState(CR_FRAMEBUFFER *pFb, PSSMHANDLE pSSM, uint32_t version)
5049{
5050 uint32_t texture;
5051 int rc = SSMR3GetU32(pSSM, &texture);
5052 AssertRCReturn(rc, rc);
5053
5054 uint32_t fFlags;
5055 rc = SSMR3GetU32(pSSM, &fFlags);
5056 AssertRCReturn(rc, rc);
5057
5058
5059 HCR_FRAMEBUFFER_ENTRY hEntry;
5060
5061 rc = CrFbEntryCreateForTexId(pFb, texture, fFlags, &hEntry);
5062 if (!RT_SUCCESS(rc))
5063 {
5064 WARN(("CrFbEntryCreateForTexId Failed"));
5065 return rc;
5066 }
5067
5068 Assert(hEntry);
5069
5070 const struct VBOXVR_SCR_COMPOSITOR_ENTRY *pEntry = CrFbEntryGetCompositorEntry(hEntry);
5071 CR_TEXDATA *pTexData = CrVrScrCompositorEntryTexGet(pEntry);
5072 CR_FBTEX *pFbTex = PCR_FBTEX_FROM_TEX(pTexData);
5073
5074 RTPOINT Point;
5075 rc = SSMR3GetS32(pSSM, &Point.x);
5076 AssertRCReturn(rc, rc);
5077
5078 rc = SSMR3GetS32(pSSM, &Point.y);
5079 AssertRCReturn(rc, rc);
5080
5081 uint32_t cRects;
5082 rc = SSMR3GetU32(pSSM, &cRects);
5083 AssertRCReturn(rc, rc);
5084
5085 RTRECT * pRects = NULL;
5086 if (cRects)
5087 {
5088 pRects = (RTRECT *)crAlloc(cRects * sizeof (*pRects));
5089 AssertReturn(pRects, VERR_NO_MEMORY);
5090
5091 rc = SSMR3GetMem(pSSM, pRects, cRects * sizeof (*pRects));
5092 AssertRCReturn(rc, rc);
5093 }
5094
5095 rc = CrFbEntryRegionsSet(pFb, hEntry, &Point, cRects, pRects, false);
5096 AssertRCReturn(rc, rc);
5097
5098 if (pRects)
5099 crFree(pRects);
5100
5101 CrFbEntryRelease(pFb, hEntry);
5102
5103 return VINF_SUCCESS;
5104}
5105
5106int CrFbLoadState(CR_FRAMEBUFFER *pFb, PSSMHANDLE pSSM, uint32_t version)
5107{
5108 uint32_t u32 = 0;
5109 int rc = SSMR3GetU32(pSSM, &u32);
5110 AssertRCReturn(rc, rc);
5111
5112 if (!u32)
5113 return VINF_SUCCESS;
5114
5115 rc = CrFbUpdateBegin(pFb);
5116 AssertRCReturn(rc, rc);
5117
5118 for (uint32_t i = 0; i < u32; ++i)
5119 {
5120 rc = CrFbEntryLoadState(pFb, pSSM, version);
5121 AssertRCReturn(rc, rc);
5122 }
5123
5124 CrFbUpdateEnd(pFb);
5125
5126 return VINF_SUCCESS;
5127}
5128
5129int CrPMgrLoadState(PSSMHANDLE pSSM, uint32_t version)
5130{
5131 int rc;
5132 int cDisplays, screenCount, i;
5133
5134 rc = SSMR3GetS32(pSSM, &cDisplays);
5135 AssertRCReturn(rc, rc);
5136
5137 if (!cDisplays)
5138 return VINF_SUCCESS;
5139
5140 rc = SSMR3GetS32(pSSM, &screenCount);
5141 AssertRCReturn(rc, rc);
5142
5143 CRASSERT(screenCount == cr_server.screenCount);
5144
5145 CRScreenInfo screen[CR_MAX_GUEST_MONITORS];
5146
5147 if (version < SHCROGL_SSM_VERSION_WITH_FB_INFO)
5148 {
5149 for (i = 0; i < cr_server.screenCount; ++i)
5150 {
5151 rc = SSMR3GetS32(pSSM, &screen[i].x);
5152 AssertRCReturn(rc, rc);
5153
5154 rc = SSMR3GetS32(pSSM, &screen[i].y);
5155 AssertRCReturn(rc, rc);
5156
5157 rc = SSMR3GetU32(pSSM, &screen[i].w);
5158 AssertRCReturn(rc, rc);
5159
5160 rc = SSMR3GetU32(pSSM, &screen[i].h);
5161 AssertRCReturn(rc, rc);
5162 }
5163 }
5164
5165 for (i = 0; i < cDisplays; ++i)
5166 {
5167 int iScreen;
5168
5169 rc = SSMR3GetS32(pSSM, &iScreen);
5170 AssertRCReturn(rc, rc);
5171
5172 CR_FRAMEBUFFER *pFb = CrPMgrFbGet(iScreen);
5173 Assert(pFb);
5174
5175 VBVAINFOSCREEN Screen;
5176
5177 Screen.u32ViewIndex = iScreen;
5178
5179 VBOXCMDVBVA_SCREENMAP_DECL(uint32_t, aTargetMap);
5180
5181 memset(aTargetMap, 0, sizeof (aTargetMap));
5182 ASMBitSet(aTargetMap, iScreen);
5183
5184 if (version < SHCROGL_SSM_VERSION_WITH_FB_INFO)
5185 {
5186 memset(&Screen, 0, sizeof (Screen));
5187 Screen.u32LineSize = 4 * screen[iScreen].w;
5188 Screen.u32Width = screen[iScreen].w;
5189 Screen.u32Height = screen[iScreen].h;
5190 Screen.u16BitsPerPixel = 4;
5191 Screen.u16Flags = VBVA_SCREEN_F_ACTIVE;
5192 }
5193 else
5194 {
5195 rc = SSMR3GetS32(pSSM, &Screen.i32OriginX);
5196 AssertRCReturn(rc, rc);
5197
5198 rc = SSMR3GetS32(pSSM, &Screen.i32OriginY);
5199 AssertRCReturn(rc, rc);
5200
5201 rc = SSMR3GetU32(pSSM, &Screen.u32StartOffset);
5202 AssertRCReturn(rc, rc);
5203
5204 rc = SSMR3GetU32(pSSM, &Screen.u32LineSize);
5205 AssertRCReturn(rc, rc);
5206
5207 rc = SSMR3GetU32(pSSM, &Screen.u32Width);
5208 AssertRCReturn(rc, rc);
5209
5210 rc = SSMR3GetU32(pSSM, &Screen.u32Height);
5211 AssertRCReturn(rc, rc);
5212
5213 rc = SSMR3GetU16(pSSM, &Screen.u16BitsPerPixel);
5214 AssertRCReturn(rc, rc);
5215
5216 rc = SSMR3GetU16(pSSM, &Screen.u16Flags);
5217 AssertRCReturn(rc, rc);
5218
5219 rc = SSMR3GetU32(pSSM, &Screen.u32StartOffset);
5220 AssertRCReturn(rc, rc);
5221 if (Screen.u32StartOffset == 0xffffffff)
5222 {
5223 WARN(("not expected offVram"));
5224 Screen.u32StartOffset = 0;
5225 }
5226
5227 if (version >= SHCROGL_SSM_VERSION_WITH_SCREEN_MAP_REORDERED)
5228 {
5229 rc = SSMR3GetMem(pSSM, aTargetMap, sizeof (aTargetMap));
5230 AssertRCReturn(rc, rc);
5231 }
5232
5233 if (version == SHCROGL_SSM_VERSION_WITH_SCREEN_MAP)
5234 {
5235 VBOXCMDVBVA_SCREENMAP_DECL(uint32_t, aEmptyTargetMap);
5236
5237 memset(aEmptyTargetMap, 0, sizeof (aEmptyTargetMap));
5238
5239 rc = CrPMgrResize(&Screen, cr_server.fCrCmdEnabled ? NULL : CrFbGetVRAM(pFb), aEmptyTargetMap);
5240 AssertRCReturn(rc, rc);
5241
5242 rc = CrFbLoadState(pFb, pSSM, version);
5243 AssertRCReturn(rc, rc);
5244
5245 rc = SSMR3GetMem(pSSM, aTargetMap, sizeof (aTargetMap));
5246 AssertRCReturn(rc, rc);
5247 }
5248 }
5249
5250 rc = CrPMgrResize(&Screen, cr_server.fCrCmdEnabled ? NULL : CrFbGetVRAM(pFb), aTargetMap);
5251 AssertRCReturn(rc, rc);
5252
5253 if (version >= SHCROGL_SSM_VERSION_WITH_FB_INFO && version != SHCROGL_SSM_VERSION_WITH_SCREEN_MAP)
5254 {
5255 rc = CrFbLoadState(pFb, pSSM, version);
5256 AssertRCReturn(rc, rc);
5257 }
5258 }
5259
5260 return VINF_SUCCESS;
5261}
5262
5263
5264void SERVER_DISPATCH_APIENTRY
5265crServerDispatchVBoxTexPresent(GLuint texture, GLuint cfg, GLint xPos, GLint yPos, GLint cRects, const GLint *pRects)
5266{
5267 uint32_t idFb = CR_PRESENT_GET_SCREEN(cfg);
5268 if (idFb >= CR_MAX_GUEST_MONITORS)
5269 {
5270 WARN(("Invalid guest screen"));
5271 return;
5272 }
5273
5274 HCR_FRAMEBUFFER hFb = CrPMgrFbGetEnabled(idFb);
5275 if (!hFb)
5276 {
5277 WARN(("request to present on disabled framebuffer, ignore"));
5278 return;
5279 }
5280
5281 HCR_FRAMEBUFFER_ENTRY hEntry;
5282 int rc;
5283 if (texture)
5284 {
5285 rc = CrFbEntryCreateForTexId(hFb, texture, (cfg & CR_PRESENT_FLAG_TEX_NONINVERT_YCOORD) ? 0 : CRBLT_F_INVERT_SRC_YCOORDS, &hEntry);
5286 if (!RT_SUCCESS(rc))
5287 {
5288 LOG(("CrFbEntryCreateForTexId Failed"));
5289 return;
5290 }
5291
5292 Assert(hEntry);
5293
5294#if 0
5295 if (!(cfg & CR_PRESENT_FLAG_CLEAR_RECTS))
5296 {
5297 CR_SERVER_DUMP_TEXPRESENT(&pEntry->CEntry.Tex);
5298 }
5299#endif
5300 }
5301 else
5302 hEntry = NULL;
5303
5304 rc = CrFbUpdateBegin(hFb);
5305 if (RT_SUCCESS(rc))
5306 {
5307 if (!(cfg & CR_PRESENT_FLAG_CLEAR_RECTS))
5308 {
5309 RTPOINT Point = {xPos, yPos};
5310 rc = CrFbEntryRegionsAdd(hFb, hEntry, &Point, (uint32_t)cRects, (const RTRECT*)pRects, false);
5311 }
5312 else
5313 {
5314 CrFbRegionsClear(hFb);
5315 }
5316
5317 CrFbUpdateEnd(hFb);
5318 }
5319 else
5320 {
5321 WARN(("CrFbUpdateBegin Failed"));
5322 }
5323
5324 if (hEntry)
5325 CrFbEntryRelease(hFb, hEntry);
5326}
5327
5328DECLINLINE(void) crVBoxPRectUnpack(const VBOXCMDVBVA_RECT *pVbvaRect, RTRECT *pRect)
5329{
5330 pRect->xLeft = pVbvaRect->xLeft;
5331 pRect->yTop = pVbvaRect->yTop;
5332 pRect->xRight = pVbvaRect->xRight;
5333 pRect->yBottom = pVbvaRect->yBottom;
5334}
5335
5336DECLINLINE(void) crVBoxPRectUnpacks(const VBOXCMDVBVA_RECT *paVbvaRects, RTRECT *paRects, uint32_t cRects)
5337{
5338 uint32_t i = 0;
5339 for (; i < cRects; ++i)
5340 {
5341 crVBoxPRectUnpack(&paVbvaRects[i], &paRects[i]);
5342 }
5343}
5344
5345static RTRECT * crVBoxServerCrCmdBltRecsUnpack(const VBOXCMDVBVA_RECT *pPRects, uint32_t cRects)
5346{
5347 if (g_CrPresenter.cbTmpBuf < cRects * sizeof (RTRECT))
5348 {
5349 if (g_CrPresenter.pvTmpBuf)
5350 RTMemFree(g_CrPresenter.pvTmpBuf);
5351
5352 g_CrPresenter.cbTmpBuf = (cRects + 10) * sizeof (RTRECT);
5353 g_CrPresenter.pvTmpBuf = RTMemAlloc(g_CrPresenter.cbTmpBuf);
5354 if (!g_CrPresenter.pvTmpBuf)
5355 {
5356 WARN(("RTMemAlloc failed!"));
5357 g_CrPresenter.cbTmpBuf = 0;
5358 return NULL;
5359 }
5360 }
5361
5362 RTRECT *pRects = (RTRECT *)g_CrPresenter.pvTmpBuf;
5363 crVBoxPRectUnpacks(pPRects, pRects, cRects);
5364
5365 return pRects;
5366}
5367
5368static void crPMgrPrimaryUpdateScreen(HCR_FRAMEBUFFER hFb, uint32_t idScreen, uint32_t cRects, const RTRECT *pRects)
5369{
5370 const VBVAINFOSCREEN *pScreen = CrFbGetScreenInfo(hFb);
5371
5372 bool fDirtyEmpty = true;
5373 RTRECT dirtyRect;
5374 cr_server.CrCmdClientInfo.pfnCltScrUpdateBegin(cr_server.CrCmdClientInfo.hCltScr, idScreen);
5375
5376 VBVACMDHDR hdr;
5377 for (uint32_t i = 0; i < cRects; ++i)
5378 {
5379 hdr.x = pRects[i].xLeft;
5380 hdr.y = pRects[i].yTop;
5381 hdr.w = hdr.x + pRects[i].xRight;
5382 hdr.h = hdr.y + pRects[i].yBottom;
5383
5384 cr_server.CrCmdClientInfo.pfnCltScrUpdateProcess(cr_server.CrCmdClientInfo.hCltScr, idScreen, &hdr, sizeof (hdr));
5385
5386 if (fDirtyEmpty)
5387 {
5388 /* This is the first rectangle to be added. */
5389 dirtyRect.xLeft = pRects[i].xLeft;
5390 dirtyRect.yTop = pRects[i].yTop;
5391 dirtyRect.xRight = pRects[i].xRight;
5392 dirtyRect.yBottom = pRects[i].yBottom;
5393 fDirtyEmpty = false;
5394 }
5395 else
5396 {
5397 /* Adjust region coordinates. */
5398 if (dirtyRect.xLeft > pRects[i].xLeft)
5399 {
5400 dirtyRect.xLeft = pRects[i].xLeft;
5401 }
5402
5403 if (dirtyRect.yTop > pRects[i].yTop)
5404 {
5405 dirtyRect.yTop = pRects[i].yTop;
5406 }
5407
5408 if (dirtyRect.xRight < pRects[i].xRight)
5409 {
5410 dirtyRect.xRight = pRects[i].xRight;
5411 }
5412
5413 if (dirtyRect.yBottom < pRects[i].yBottom)
5414 {
5415 dirtyRect.yBottom = pRects[i].yBottom;
5416 }
5417 }
5418 }
5419
5420 if (dirtyRect.xRight - dirtyRect.xLeft)
5421 {
5422 cr_server.CrCmdClientInfo.pfnCltScrUpdateEnd(cr_server.CrCmdClientInfo.hCltScr, idScreen, pScreen->i32OriginX + dirtyRect.xLeft, pScreen->i32OriginY + dirtyRect.yTop,
5423 dirtyRect.xRight - dirtyRect.xLeft, dirtyRect.yBottom - dirtyRect.yTop);
5424 }
5425 else
5426 {
5427 cr_server.CrCmdClientInfo.pfnCltScrUpdateEnd(cr_server.CrCmdClientInfo.hCltScr, idScreen, 0, 0, 0, 0);
5428 }
5429
5430}
5431
5432static void crPMgrPrimaryUpdate(HCR_FRAMEBUFFER hFb, uint32_t cRects, const RTRECT *pRects)
5433{
5434 if (!cRects)
5435 return;
5436
5437 const VBVAINFOSCREEN *pScreen = CrFbGetScreenInfo(hFb);
5438
5439 uint32_t idFb = pScreen->u32ViewIndex;
5440 CR_FB_INFO *pFbInfo = &g_CrPresenter.aFbInfos[idFb];
5441
5442 for (int i = ASMBitFirstSet(pFbInfo->aTargetMap, cr_server.screenCount);
5443 i >= 0;
5444 i = ASMBitNextSet(pFbInfo->aTargetMap, cr_server.screenCount, i))
5445 {
5446 crPMgrPrimaryUpdateScreen(hFb, i, cRects, pRects);
5447 }
5448}
5449
5450static int8_t crVBoxServerCrCmdBltPrimaryVramGenericProcess(uint32_t u32PrimaryID, VBOXCMDVBVAOFFSET offVRAM, uint32_t width, uint32_t height, const RTPOINT *pPos, uint32_t cRects, const RTRECT *pRects, bool fToPrimary)
5451{
5452 CR_BLITTER_IMG Img;
5453 int8_t i8Result = crFbImgFromDimOffVramBGRA(offVRAM, width, height, &Img);
5454 if (i8Result)
5455 {
5456 WARN(("invalid param"));
5457 return -1;
5458 }
5459
5460 HCR_FRAMEBUFFER hFb = CrPMgrFbGetEnabled(u32PrimaryID);
5461 if (!hFb)
5462 {
5463 WARN(("request to present on disabled framebuffer"));
5464 return -1;
5465 }
5466
5467 if (!fToPrimary)
5468 {
5469 int rc = CrFbBltGetContents(hFb, pPos, cRects, pRects, &Img);
5470 if (!RT_SUCCESS(rc))
5471 {
5472 WARN(("CrFbBltGetContents failed %d", rc));
5473 return -1;
5474 }
5475
5476 return 0;
5477 }
5478
5479 int rc = CrFbBltPutContentsNe(hFb, pPos, cRects, pRects, &Img);
5480 if (!RT_SUCCESS(rc))
5481 {
5482 WARN(("CrFbBltPutContentsNe failed %d", rc));
5483 return -1;
5484 }
5485
5486 return 0;
5487}
5488
5489static int8_t crVBoxServerCrCmdBltPrimaryProcess(const VBOXCMDVBVA_BLT_PRIMARY *pCmd, uint32_t cbCmd)
5490{
5491 uint32_t u32PrimaryID = (uint32_t)pCmd->Hdr.Hdr.u.u8PrimaryID;
5492 HCR_FRAMEBUFFER hFb = CrPMgrFbGetEnabled(u32PrimaryID);
5493 if (!hFb)
5494 {
5495 WARN(("request to present on disabled framebuffer, ignore"));
5496 return 0;
5497 }
5498
5499 uint32_t cRects;
5500 const VBOXCMDVBVA_RECT *pPRects = pCmd->aRects;
5501 if ((cbCmd - RT_OFFSETOF(VBOXCMDVBVA_BLT_PRIMARY, aRects)) % sizeof (VBOXCMDVBVA_RECT))
5502 {
5503 WARN(("invalid argument size"));
5504 return -1;
5505 }
5506
5507 cRects = (cbCmd - RT_OFFSETOF(VBOXCMDVBVA_BLT_PRIMARY, aRects)) / sizeof (VBOXCMDVBVA_RECT);
5508
5509 RTRECT *pRects = crVBoxServerCrCmdBltRecsUnpack(pPRects, cRects);
5510 if (!pRects)
5511 {
5512 WARN(("crVBoxServerCrCmdBltRecsUnpack failed"));
5513 return -1;
5514 }
5515
5516 uint8_t u8Flags = pCmd->Hdr.Hdr.u8Flags;
5517
5518 if (u8Flags & VBOXCMDVBVA_OPF_OPERAND2_ISID)
5519 {
5520 uint32_t texId = pCmd->alloc.u.id;
5521 if (!texId)
5522 {
5523 WARN(("texId is NULL!\n"));
5524 return -1;
5525 }
5526
5527 if (u8Flags & VBOXCMDVBVA_OPF_BLT_DIR_IN_2)
5528 {
5529 WARN(("blit from primary to texture not implemented"));
5530 return -1;
5531 }
5532
5533 crServerDispatchVBoxTexPresent(texId, u32PrimaryID, pCmd->Hdr.Pos.x, pCmd->Hdr.Pos.y, cRects, (const GLint*)pRects);
5534
5535 return 0;
5536 }
5537 else
5538 {
5539 const VBVAINFOSCREEN *pScreen = CrFbGetScreenInfo(hFb);
5540 uint32_t width = pScreen->u32Width, height = pScreen->u32Height;
5541 VBOXCMDVBVAOFFSET offVRAM = pCmd->alloc.u.offVRAM;
5542
5543 bool fToPrymary = !(u8Flags & VBOXCMDVBVA_OPF_BLT_DIR_IN_2);
5544 RTPOINT Pos = {pCmd->Hdr.Pos.x, pCmd->Hdr.Pos.y};
5545 int8_t i8Result = crVBoxServerCrCmdBltPrimaryVramGenericProcess(u32PrimaryID, offVRAM, width, height, &Pos, cRects, pRects, fToPrymary);
5546 if (i8Result < 0)
5547 {
5548 WARN(("crVBoxServerCrCmdBltPrimaryVramGenericProcess failed"));
5549 return i8Result;
5550 }
5551
5552 if (!fToPrymary)
5553 return 0;
5554 }
5555
5556 crPMgrPrimaryUpdate(hFb, cRects, pRects);
5557
5558 return 0;
5559}
5560
5561static int8_t crVBoxServerCrCmdBltIdToVramMem(uint32_t hostId, VBOXCMDVBVAOFFSET offVRAM, uint32_t width, uint32_t height, const RTPOINT *pPos, uint32_t cRects, const RTRECT *pRects)
5562{
5563 CR_TEXDATA* pTex = CrFbTexDataAcquire(hostId);
5564 if (!pTex)
5565 {
5566 WARN(("pTex failed for %d", hostId));
5567 return -1;
5568 }
5569
5570 const VBOXVR_TEXTURE *pVrTex = CrTdTexGet(pTex);
5571 if (!width)
5572 {
5573 width = pVrTex->width;
5574 height = pVrTex->height;
5575 }
5576
5577 CR_BLITTER_IMG Img;
5578 int8_t i8Result = crFbImgFromDimOffVramBGRA(offVRAM, width, height, &Img);
5579 if (i8Result)
5580 {
5581 WARN(("invalid param"));
5582 return -1;
5583 }
5584
5585 int rc = CrTdBltEnter(pTex);
5586 if (!RT_SUCCESS(rc))
5587 {
5588 WARN(("CrTdBltEnter failed %d", rc));
5589 return -1;
5590 }
5591
5592 rc = crFbTexDataGetContents(pTex, pPos, cRects, pRects, &Img);
5593
5594 CrTdBltLeave(pTex);
5595
5596 CrTdRelease(pTex);
5597
5598 if (!RT_SUCCESS(rc))
5599 {
5600 WARN(("crFbTexDataGetContents failed %d", rc));
5601 return -1;
5602 }
5603
5604 return 0;
5605}
5606
5607static int8_t crVBoxServerCrCmdBltIdToVram(uint32_t hostId, VBOXCMDVBVAOFFSET offVRAM, uint32_t width, uint32_t height, const RTPOINT *pPos, uint32_t cRects, const RTRECT *pRects)
5608{
5609 HCR_FRAMEBUFFER hFb = CrPMgrFbGetEnabledByVramStart(offVRAM);
5610 if (hFb)
5611 {
5612 const VBVAINFOSCREEN *pScreen = CrFbGetScreenInfo(hFb);
5613 Assert(!width || pScreen->u32Width == width);
5614 Assert(!height || pScreen->u32Height == height);
5615
5616 crServerDispatchVBoxTexPresent(hostId, pScreen->u32ViewIndex, pPos->x, pPos->y, cRects, (const GLint*)pRects);
5617 return 0;
5618 }
5619
5620 return crVBoxServerCrCmdBltIdToVramMem(hostId, offVRAM, width, height, pPos, cRects, pRects);
5621}
5622
5623static int8_t crVBoxServerCrCmdBltVramToVramMem(VBOXCMDVBVAOFFSET offSrcVRAM, uint32_t srcWidth, uint32_t srcHeight, VBOXCMDVBVAOFFSET offDstVRAM, uint32_t dstWidth, uint32_t dstHeight, const RTPOINT *pPos, uint32_t cRects, const RTRECT *pRects)
5624{
5625 CR_BLITTER_IMG srcImg, dstImg;
5626 int8_t i8Result = crFbImgFromDimOffVramBGRA(offSrcVRAM, srcWidth, srcHeight, &srcImg);
5627 if (i8Result)
5628 {
5629 WARN(("invalid param"));
5630 return -1;
5631 }
5632
5633 i8Result = crFbImgFromDimOffVramBGRA(offDstVRAM, dstWidth, dstHeight, &dstImg);
5634 if (i8Result)
5635 {
5636 WARN(("invalid param"));
5637 return -1;
5638 }
5639
5640 CrMBltImg(&srcImg, pPos, cRects, pRects, &dstImg);
5641
5642 return 0;
5643}
5644
5645static int8_t crVBoxServerCrCmdBltVramToVram(VBOXCMDVBVAOFFSET offSrcVRAM, uint32_t srcWidth, uint32_t srcHeight,
5646 VBOXCMDVBVAOFFSET offDstVRAM, uint32_t dstWidth, uint32_t dstHeight,
5647 const RTPOINT *pPos, uint32_t cRects, const RTRECT *pRects)
5648{
5649 HCR_FRAMEBUFFER hSrcFb = CrPMgrFbGetEnabledByVramStart(offSrcVRAM);
5650 HCR_FRAMEBUFFER hDstFb = CrPMgrFbGetEnabledByVramStart(offDstVRAM);
5651
5652 if (hDstFb)
5653 {
5654 if (hSrcFb)
5655 {
5656 WARN(("blit from one framebuffer, wow"));
5657
5658 int rc = CrFbUpdateBegin(hSrcFb);
5659 if (RT_SUCCESS(rc))
5660 {
5661 CrFbRegionsClear(hSrcFb);
5662
5663 CrFbUpdateEnd(hSrcFb);
5664 }
5665 else
5666 WARN(("CrFbUpdateBegin failed %d", rc));
5667 }
5668
5669 CR_BLITTER_IMG Img;
5670 int8_t i8Result = crFbImgFromDimOffVramBGRA(offSrcVRAM, srcWidth, srcHeight, &Img);
5671 if (i8Result)
5672 {
5673 WARN(("invalid param"));
5674 return -1;
5675 }
5676
5677 const VBVAINFOSCREEN *pScreen = CrFbGetScreenInfo(hDstFb);
5678 if (pScreen->u32Width == dstWidth && pScreen->u32Height == dstHeight)
5679 {
5680 int rc = CrFbBltPutContentsNe(hDstFb, pPos, cRects, pRects, &Img);
5681 if (RT_FAILURE(rc))
5682 {
5683 WARN(("CrFbBltPutContentsNe failed %d", rc));
5684 return -1;
5685 }
5686 }
5687 else
5688 {
5689 int rc = CrFbUpdateBegin(hDstFb);
5690 if (RT_SUCCESS(rc))
5691 {
5692 CrFbRegionsClear(hDstFb);
5693
5694 CrFbUpdateEnd(hDstFb);
5695 }
5696 else
5697 WARN(("CrFbUpdateBegin failed %d", rc));
5698
5699 rc = crVBoxServerCrCmdBltVramToVramMem(offSrcVRAM, srcWidth, srcHeight, offDstVRAM, dstWidth, dstHeight, pPos, cRects, pRects);
5700 if (RT_FAILURE(rc))
5701 {
5702 WARN(("crVBoxServerCrCmdBltVramToVramMem failed, %d", rc));
5703 return -1;
5704 }
5705 }
5706
5707 crPMgrPrimaryUpdate(hDstFb, cRects, pRects);
5708
5709 return 0;
5710 }
5711 else if (hSrcFb)
5712 {
5713 CR_BLITTER_IMG Img;
5714 int8_t i8Result = crFbImgFromDimOffVramBGRA(offDstVRAM, dstWidth, dstHeight, &Img);
5715 if (i8Result)
5716 {
5717 WARN(("invalid param"));
5718 return -1;
5719 }
5720
5721 const VBVAINFOSCREEN *pScreen = CrFbGetScreenInfo(hSrcFb);
5722 if (pScreen->u32Width == srcWidth && pScreen->u32Height == srcHeight)
5723 {
5724 int rc = CrFbBltGetContents(hSrcFb, pPos, cRects, pRects, &Img);
5725 if (RT_FAILURE(rc))
5726 {
5727 WARN(("CrFbBltGetContents failed %d", rc));
5728 return -1;
5729 }
5730 }
5731 else
5732 {
5733 int rc = CrFbUpdateBegin(hSrcFb);
5734 if (RT_SUCCESS(rc))
5735 {
5736 CrFbRegionsClear(hSrcFb);
5737
5738 CrFbUpdateEnd(hSrcFb);
5739 }
5740 else
5741 WARN(("CrFbUpdateBegin failed %d", rc));
5742
5743 rc = crVBoxServerCrCmdBltVramToVramMem(offSrcVRAM, srcWidth, srcHeight, offDstVRAM, dstWidth, dstHeight, pPos, cRects, pRects);
5744 if (RT_FAILURE(rc))
5745 {
5746 WARN(("crVBoxServerCrCmdBltVramToVramMem failed, %d", rc));
5747 return -1;
5748 }
5749 }
5750
5751 return 0;
5752 }
5753
5754 return crVBoxServerCrCmdBltVramToVramMem(offSrcVRAM, srcWidth, srcHeight, offDstVRAM, dstWidth, dstHeight, pPos, cRects, pRects);
5755}
5756
5757
5758static int8_t crVBoxServerCrCmdBltOffIdProcess(const VBOXCMDVBVA_BLT_OFFPRIMSZFMT_OR_ID *pCmd, uint32_t cbCmd)
5759{
5760 uint32_t cRects;
5761 const VBOXCMDVBVA_RECT *pPRects = pCmd->aRects;
5762 if ((cbCmd - RT_OFFSETOF(VBOXCMDVBVA_BLT_OFFPRIMSZFMT_OR_ID, aRects)) % sizeof (VBOXCMDVBVA_RECT))
5763 {
5764 WARN(("invalid argument size"));
5765 return -1;
5766 }
5767
5768 cRects = (cbCmd - RT_OFFSETOF(VBOXCMDVBVA_BLT_OFFPRIMSZFMT_OR_ID, aRects)) / sizeof (VBOXCMDVBVA_RECT);
5769
5770 RTRECT *pRects = crVBoxServerCrCmdBltRecsUnpack(pPRects, cRects);
5771 if (!pRects)
5772 {
5773 WARN(("crVBoxServerCrCmdBltRecsUnpack failed"));
5774 return -1;
5775 }
5776
5777 uint8_t u8Flags = pCmd->Hdr.Hdr.u8Flags;
5778 uint32_t hostId = pCmd->id;
5779
5780 Assert(u8Flags & VBOXCMDVBVA_OPF_OPERAND2_ISID);
5781
5782 if (!hostId)
5783 {
5784 WARN(("zero host id"));
5785 return -1;
5786 }
5787
5788 if (u8Flags & VBOXCMDVBVA_OPF_OPERAND1_ISID)
5789 {
5790 WARN(("blit from texture to texture not implemented"));
5791 return -1;
5792 }
5793
5794 if (u8Flags & VBOXCMDVBVA_OPF_BLT_DIR_IN_2)
5795 {
5796 WARN(("blit to texture not implemented"));
5797 return -1;
5798 }
5799
5800 VBOXCMDVBVAOFFSET offVRAM = pCmd->alloc.u.offVRAM;
5801
5802 RTPOINT Pos = {pCmd->Hdr.Pos.x, pCmd->Hdr.Pos.y};
5803 return crVBoxServerCrCmdBltIdToVram(hostId, offVRAM, 0, 0, &Pos, cRects, pRects);
5804}
5805
5806static int8_t crVBoxServerCrCmdBltSameDimOrId(const VBOXCMDVBVA_BLT_SAMEDIM_A8R8G8B8 *pCmd, uint32_t cbCmd)
5807{
5808 uint32_t cRects;
5809 const VBOXCMDVBVA_RECT *pPRects = pCmd->aRects;
5810 if ((cbCmd - RT_OFFSETOF(VBOXCMDVBVA_BLT_SAMEDIM_A8R8G8B8, aRects)) % sizeof (VBOXCMDVBVA_RECT))
5811 {
5812 WARN(("invalid argument size"));
5813 return -1;
5814 }
5815
5816 cRects = (cbCmd - RT_OFFSETOF(VBOXCMDVBVA_BLT_SAMEDIM_A8R8G8B8, aRects)) / sizeof (VBOXCMDVBVA_RECT);
5817
5818 RTRECT *pRects = crVBoxServerCrCmdBltRecsUnpack(pPRects, cRects);
5819 if (!pRects)
5820 {
5821 WARN(("crVBoxServerCrCmdBltRecsUnpack failed"));
5822 return -1;
5823 }
5824
5825 uint8_t u8Flags = pCmd->Hdr.Hdr.u8Flags;
5826 VBOXCMDVBVAOFFSET offVRAM = pCmd->alloc1.Info.u.offVRAM;
5827 uint32_t width = pCmd->alloc1.u16Width;
5828 uint32_t height = pCmd->alloc1.u16Height;
5829 RTPOINT Pos = {pCmd->Hdr.Pos.x, pCmd->Hdr.Pos.y};
5830
5831 if (u8Flags & VBOXCMDVBVA_OPF_OPERAND2_ISID)
5832 {
5833 uint32_t hostId = pCmd->info2.u.id;
5834
5835 if (!hostId)
5836 {
5837 WARN(("zero host id"));
5838 return -1;
5839 }
5840
5841 if (u8Flags & VBOXCMDVBVA_OPF_OPERAND1_ISID)
5842 {
5843 WARN(("blit from texture to texture not implemented"));
5844 return -1;
5845 }
5846
5847 if (u8Flags & VBOXCMDVBVA_OPF_BLT_DIR_IN_2)
5848 {
5849 WARN(("blit to texture not implemented"));
5850 return -1;
5851 }
5852
5853 return crVBoxServerCrCmdBltIdToVram(hostId, offVRAM, width, height, &Pos, cRects, pRects);
5854 }
5855
5856 if (u8Flags & VBOXCMDVBVA_OPF_OPERAND1_ISID)
5857 {
5858 if (!(u8Flags & VBOXCMDVBVA_OPF_BLT_DIR_IN_2))
5859 {
5860 WARN(("blit to texture not implemented"));
5861 return -1;
5862 }
5863
5864 return crVBoxServerCrCmdBltIdToVram(pCmd->alloc1.Info.u.id, pCmd->info2.u.offVRAM, width, height, &Pos, cRects, pRects);
5865 }
5866
5867 if (u8Flags & VBOXCMDVBVA_OPF_BLT_DIR_IN_2)
5868 crVBoxServerCrCmdBltVramToVram(offVRAM, width, height, pCmd->info2.u.offVRAM, width, height, &Pos, cRects, pRects);
5869 else
5870 crVBoxServerCrCmdBltVramToVram(pCmd->info2.u.offVRAM, width, height, offVRAM, width, height, &Pos, cRects, pRects);
5871
5872 return 0;
5873}
5874
5875static int8_t crVBoxServerCrCmdBltGenericBGRAProcess(const VBOXCMDVBVA_BLT_GENERIC_A8R8G8B8 *pCmd, uint32_t cbCmd)
5876{
5877 uint32_t cRects;
5878 const VBOXCMDVBVA_RECT *pPRects = pCmd->aRects;
5879 if ((cbCmd - RT_OFFSETOF(VBOXCMDVBVA_BLT_GENERIC_A8R8G8B8, aRects)) % sizeof (VBOXCMDVBVA_RECT))
5880 {
5881 WARN(("invalid argument size"));
5882 return -1;
5883 }
5884
5885 cRects = (cbCmd - RT_OFFSETOF(VBOXCMDVBVA_BLT_GENERIC_A8R8G8B8, aRects)) / sizeof (VBOXCMDVBVA_RECT);
5886
5887 RTRECT *pRects = crVBoxServerCrCmdBltRecsUnpack(pPRects, cRects);
5888 if (!pRects)
5889 {
5890 WARN(("crVBoxServerCrCmdBltRecsUnpack failed"));
5891 return -1;
5892 }
5893
5894 uint8_t u8Flags = pCmd->Hdr.Hdr.u8Flags;
5895 RTPOINT Pos = {pCmd->Hdr.Pos.x, pCmd->Hdr.Pos.y};
5896
5897 if (u8Flags & VBOXCMDVBVA_OPF_OPERAND2_ISID)
5898 {
5899 if (u8Flags & VBOXCMDVBVA_OPF_OPERAND1_ISID)
5900 {
5901 WARN(("blit from texture to texture not implemented"));
5902 return -1;
5903 }
5904
5905 if (u8Flags & VBOXCMDVBVA_OPF_BLT_DIR_IN_2)
5906 {
5907 WARN(("blit to texture not implemented"));
5908 return -1;
5909 }
5910
5911 return crVBoxServerCrCmdBltIdToVram(pCmd->alloc2.Info.u.id, pCmd->alloc1.Info.u.offVRAM, pCmd->alloc1.u16Width, pCmd->alloc1.u16Height, &Pos, cRects, pRects);
5912 }
5913 else
5914 {
5915 if (u8Flags & VBOXCMDVBVA_OPF_OPERAND1_ISID)
5916 {
5917 if (!(u8Flags & VBOXCMDVBVA_OPF_BLT_DIR_IN_2))
5918 {
5919 WARN(("blit to texture not implemented"));
5920 return -1;
5921 }
5922
5923 RTPOINT Pos = {pCmd->Hdr.Pos.x, pCmd->Hdr.Pos.y};
5924 return crVBoxServerCrCmdBltIdToVram(pCmd->alloc1.Info.u.id, pCmd->alloc2.Info.u.offVRAM, pCmd->alloc2.u16Width, pCmd->alloc2.u16Height, &Pos, cRects, pRects);
5925 }
5926
5927 if (u8Flags & VBOXCMDVBVA_OPF_BLT_DIR_IN_2)
5928 crVBoxServerCrCmdBltVramToVram(pCmd->alloc1.Info.u.offVRAM, pCmd->alloc1.u16Width, pCmd->alloc1.u16Height, pCmd->alloc2.Info.u.offVRAM, pCmd->alloc2.u16Width, pCmd->alloc2.u16Height, &Pos, cRects, pRects);
5929 else
5930 crVBoxServerCrCmdBltVramToVram(pCmd->alloc2.Info.u.offVRAM, pCmd->alloc2.u16Width, pCmd->alloc2.u16Height, pCmd->alloc1.Info.u.offVRAM, pCmd->alloc1.u16Width, pCmd->alloc1.u16Height, &Pos, cRects, pRects);
5931
5932 return 0;
5933 }
5934}
5935
5936static int8_t crVBoxServerCrCmdClrFillPrimaryGenericProcess(uint32_t u32PrimaryID, const RTRECT *pRects, uint32_t cRects, uint32_t u32Color)
5937{
5938 HCR_FRAMEBUFFER hFb = CrPMgrFbGetEnabled(u32PrimaryID);
5939 if (!hFb)
5940 {
5941 WARN(("request to present on disabled framebuffer, ignore"));
5942 return 0;
5943 }
5944
5945 int rc = CrFbClrFillNe(hFb, cRects, pRects, u32Color);
5946 if (!RT_SUCCESS(rc))
5947 {
5948 WARN(("CrFbClrFillNe failed %d", rc));
5949 return -1;
5950 }
5951
5952 return 0;
5953}
5954
5955static int8_t crVBoxServerCrCmdClrFillVramGenericProcess(VBOXCMDVBVAOFFSET offVRAM, uint32_t width, uint32_t height, const RTRECT *pRects, uint32_t cRects, uint32_t u32Color)
5956{
5957 CR_BLITTER_IMG Img;
5958 int8_t i8Result = crFbImgFromDimOffVramBGRA(offVRAM, width, height, &Img);
5959 if (i8Result)
5960 {
5961 WARN(("invalid param"));
5962 return -1;
5963 }
5964
5965 CrMClrFillImg(&Img, cRects, pRects, u32Color);
5966
5967 return 0;
5968}
5969
5970static int8_t crVBoxServerCrCmdClrFillGenericBGRAProcess(const VBOXCMDVBVA_CLRFILL_GENERIC_A8R8G8B8 *pCmd, uint32_t cbCmd)
5971{
5972 uint32_t cRects;
5973 const VBOXCMDVBVA_RECT *pPRects = pCmd->aRects;
5974 if ((cbCmd - RT_OFFSETOF(VBOXCMDVBVA_CLRFILL_GENERIC_A8R8G8B8, aRects)) % sizeof (VBOXCMDVBVA_RECT))
5975 {
5976 WARN(("invalid argument size"));
5977 return -1;
5978 }
5979
5980 cRects = (cbCmd - RT_OFFSETOF(VBOXCMDVBVA_CLRFILL_GENERIC_A8R8G8B8, aRects)) / sizeof (VBOXCMDVBVA_RECT);
5981
5982 RTRECT *pRects = crVBoxServerCrCmdBltRecsUnpack(pPRects, cRects);
5983 if (!pRects)
5984 {
5985 WARN(("crVBoxServerCrCmdBltRecsUnpack failed"));
5986 return -1;
5987 }
5988
5989// uint8_t u8Flags = pCmd->Hdr.Hdr.u8Flags;
5990 int8_t i8Result = crVBoxServerCrCmdClrFillVramGenericProcess(pCmd->dst.Info.u.offVRAM, pCmd->dst.u16Width, pCmd->dst.u16Height, pRects, cRects, pCmd->Hdr.u32Color);
5991 if (i8Result < 0)
5992 {
5993 WARN(("crVBoxServerCrCmdClrFillVramGenericProcess failed"));
5994 return i8Result;
5995 }
5996
5997 return 0;
5998}
5999
6000int8_t crVBoxServerCrCmdClrFillProcess(const VBOXCMDVBVA_CLRFILL_HDR *pCmd, uint32_t cbCmd)
6001{
6002 uint8_t u8Flags = pCmd->Hdr.u8Flags;
6003 uint8_t u8Cmd = (VBOXCMDVBVA_OPF_CLRFILL_TYPE_MASK & u8Flags);
6004
6005 switch (u8Cmd)
6006 {
6007 case VBOXCMDVBVA_OPF_CLRFILL_TYPE_GENERIC_A8R8G8B8:
6008 {
6009 if (cbCmd < sizeof (VBOXCMDVBVA_CLRFILL_GENERIC_A8R8G8B8))
6010 {
6011 WARN(("VBOXCMDVBVA_CLRFILL_GENERIC_A8R8G8B8: invalid command size"));
6012 return -1;
6013 }
6014
6015 return crVBoxServerCrCmdClrFillGenericBGRAProcess((const VBOXCMDVBVA_CLRFILL_GENERIC_A8R8G8B8*)pCmd, cbCmd);
6016 }
6017 default:
6018 WARN(("unsupported command"));
6019 return -1;
6020 }
6021
6022}
6023
6024int8_t crVBoxServerCrCmdBltProcess(const VBOXCMDVBVA_BLT_HDR *pCmd, uint32_t cbCmd)
6025{
6026 uint8_t u8Flags = pCmd->Hdr.u8Flags;
6027 uint8_t u8Cmd = (VBOXCMDVBVA_OPF_BLT_TYPE_MASK & u8Flags);
6028
6029 switch (u8Cmd)
6030 {
6031 case VBOXCMDVBVA_OPF_BLT_TYPE_SAMEDIM_A8R8G8B8:
6032 {
6033 if (cbCmd < sizeof (VBOXCMDVBVA_BLT_SAMEDIM_A8R8G8B8))
6034 {
6035 WARN(("VBOXCMDVBVA_BLT_SAMEDIM_A8R8G8B8: invalid command size"));
6036 return -1;
6037 }
6038
6039 return crVBoxServerCrCmdBltSameDimOrId((const VBOXCMDVBVA_BLT_SAMEDIM_A8R8G8B8 *)pCmd, cbCmd);
6040 }
6041 case VBOXCMDVBVA_OPF_BLT_TYPE_OFFPRIMSZFMT_OR_ID:
6042 {
6043 if (cbCmd < sizeof (VBOXCMDVBVA_BLT_OFFPRIMSZFMT_OR_ID))
6044 {
6045 WARN(("VBOXCMDVBVA_OPF_BLT_TYPE_OFFPRIMSZFMT_OR_ID: invalid command size"));
6046 return -1;
6047 }
6048
6049 return crVBoxServerCrCmdBltOffIdProcess((const VBOXCMDVBVA_BLT_OFFPRIMSZFMT_OR_ID *)pCmd, cbCmd);
6050 }
6051 case VBOXCMDVBVA_OPF_BLT_TYPE_GENERIC_A8R8G8B8:
6052 {
6053 if (cbCmd < sizeof (VBOXCMDVBVA_BLT_GENERIC_A8R8G8B8))
6054 {
6055 WARN(("VBOXCMDVBVA_OPF_BLT_TYPE_GENERIC_A8R8G8B8: invalid command size"));
6056 return -1;
6057 }
6058
6059 return crVBoxServerCrCmdBltGenericBGRAProcess((const VBOXCMDVBVA_BLT_GENERIC_A8R8G8B8 *)pCmd, cbCmd);
6060 }
6061 default:
6062 WARN(("unsupported command"));
6063 return -1;
6064 }
6065}
6066
6067int8_t crVBoxServerCrCmdFlipProcess(const VBOXCMDVBVA_FLIP *pFlip)
6068{
6069 uint32_t hostId;
6070 if (pFlip->Hdr.u8Flags & VBOXCMDVBVA_OPF_OPERAND1_ISID)
6071 {
6072 hostId = pFlip->src.u.id;
6073 if (!hostId)
6074 {
6075 WARN(("hostId is NULL"));
6076 return -1;
6077 }
6078 }
6079 else
6080 {
6081 WARN(("VBOXCMDVBVA_OPF_ALLOC_SRCID not specified"));
6082 hostId = 0;
6083 }
6084
6085 uint32_t idFb = pFlip->Hdr.u.u8PrimaryID;
6086 HCR_FRAMEBUFFER hFb = CrPMgrFbGetEnabled(idFb);
6087 if (!hFb)
6088 {
6089 WARN(("request to present on disabled framebuffer, ignore"));
6090 return 0;
6091 }
6092
6093 const RTRECT *pRect = CrVrScrCompositorRectGet(&hFb->Compositor);
6094 crServerDispatchVBoxTexPresent(hostId, idFb, 0, 0, 1, (const GLint*)pRect);
6095 return 0;
6096}
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