VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/DisplayImplLegacy.cpp@ 102493

Last change on this file since 102493 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 31.0 KB
Line 
1/* $Id: DisplayImplLegacy.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * VirtualBox IDisplay implementation, helpers for legacy GAs.
4 *
5 * Methods and helpers to support old Guest Additions 3.x or older.
6 * This is not used by the current Guest Additions.
7 */
8
9/*
10 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
11 *
12 * This file is part of VirtualBox base platform packages, as
13 * available from https://www.virtualbox.org.
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation, in version 3 of the
18 * License.
19 *
20 * This program is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, see <https://www.gnu.org/licenses>.
27 *
28 * SPDX-License-Identifier: GPL-3.0-only
29 */
30
31#define LOG_GROUP LOG_GROUP_MAIN_DISPLAY
32#include "LoggingNew.h"
33
34#include "DisplayImpl.h"
35#include "ConsoleImpl.h"
36#include "ConsoleVRDPServer.h"
37#include "VMMDev.h"
38#include <VBox/VMMDev.h>
39
40/* generated header */
41#include "VBoxEvents.h"
42
43
44int videoAccelConstruct(VIDEOACCEL *pVideoAccel)
45{
46 pVideoAccel->pVbvaMemory = NULL;
47 pVideoAccel->fVideoAccelEnabled = false;
48
49 pVideoAccel->pu8VbvaPartial = NULL;
50 pVideoAccel->cbVbvaPartial = 0;
51
52 pVideoAccel->hXRoadsVideoAccel = NIL_RTSEMXROADS;
53 int vrc = RTSemXRoadsCreate(&pVideoAccel->hXRoadsVideoAccel);
54 AssertRC(vrc);
55
56 return vrc;
57}
58
59void videoAccelDestroy(VIDEOACCEL *pVideoAccel)
60{
61 RTSemXRoadsDestroy(pVideoAccel->hXRoadsVideoAccel);
62 RT_ZERO(*pVideoAccel);
63}
64
65static unsigned mapCoordsToScreen(DISPLAYFBINFO *pInfos, unsigned cInfos, int *px, int *py, int *pw, int *ph)
66{
67 RT_NOREF(pw, ph);
68
69 DISPLAYFBINFO *pInfo = pInfos;
70 unsigned uScreenId;
71 Log9(("mapCoordsToScreen: %d,%d %dx%d\n", *px, *py, *pw, *ph));
72 for (uScreenId = 0; uScreenId < cInfos; uScreenId++, pInfo++)
73 {
74 Log9((" [%d] %d,%d %dx%d\n", uScreenId, pInfo->xOrigin, pInfo->yOrigin, pInfo->w, pInfo->h));
75 if ( (pInfo->xOrigin <= *px && *px < pInfo->xOrigin + (int)pInfo->w)
76 && (pInfo->yOrigin <= *py && *py < pInfo->yOrigin + (int)pInfo->h))
77 {
78 /* The rectangle belongs to the screen. Correct coordinates. */
79 *px -= pInfo->xOrigin;
80 *py -= pInfo->yOrigin;
81 Log9((" -> %d,%d", *px, *py));
82 break;
83 }
84 }
85 if (uScreenId == cInfos)
86 {
87 /* Map to primary screen. */
88 uScreenId = 0;
89 }
90 Log9((" scr %d\n", uScreenId));
91 return uScreenId;
92}
93
94
95typedef struct _VBVADIRTYREGION
96{
97 /* Copies of object's pointers used by vbvaRgn functions. */
98 DISPLAYFBINFO *paFramebuffers;
99 unsigned cMonitors;
100 Display *pDisplay;
101 PPDMIDISPLAYPORT pPort;
102
103 /* The rectangle that includes all dirty rectangles. */
104 RTRECT aDirtyRects[SchemaDefs::MaxGuestMonitors];
105
106} VBVADIRTYREGION;
107
108static void vbvaRgnInit(VBVADIRTYREGION *prgn, DISPLAYFBINFO *paFramebuffers, unsigned cMonitors,
109 Display *pd, PPDMIDISPLAYPORT pp)
110{
111 prgn->paFramebuffers = paFramebuffers;
112 prgn->cMonitors = cMonitors;
113 prgn->pDisplay = pd;
114 prgn->pPort = pp;
115
116 RT_ZERO(prgn->aDirtyRects);
117}
118
119static void vbvaRgnDirtyRect(VBVADIRTYREGION *prgn, unsigned uScreenId, VBVACMDHDR *phdr)
120{
121 Log9(("x = %d, y = %d, w = %d, h = %d\n", phdr->x, phdr->y, phdr->w, phdr->h));
122
123 /*
124 * Here update rectangles are accumulated to form an update area.
125 */
126 /** @todo
127 * Now the simplest method is used which builds one rectangle that
128 * includes all update areas. A bit more advanced method can be
129 * employed here. The method should be fast however.
130 */
131 if (phdr->w == 0 || phdr->h == 0)
132 {
133 /* Empty rectangle. */
134 return;
135 }
136
137 int32_t xRight = phdr->x + phdr->w;
138 int32_t yBottom = phdr->y + phdr->h;
139
140 RTRECT *pDirtyRect = &prgn->aDirtyRects[uScreenId];
141 DISPLAYFBINFO *pFBInfo = &prgn->paFramebuffers[uScreenId];
142
143 if (pDirtyRect->xRight == 0)
144 {
145 /* This is the first rectangle to be added. */
146 pDirtyRect->xLeft = phdr->x;
147 pDirtyRect->yTop = phdr->y;
148 pDirtyRect->xRight = xRight;
149 pDirtyRect->yBottom = yBottom;
150 }
151 else
152 {
153 /* Adjust region coordinates. */
154 if (pDirtyRect->xLeft > phdr->x)
155 {
156 pDirtyRect->xLeft = phdr->x;
157 }
158
159 if (pDirtyRect->yTop > phdr->y)
160 {
161 pDirtyRect->yTop = phdr->y;
162 }
163
164 if (pDirtyRect->xRight < xRight)
165 {
166 pDirtyRect->xRight = xRight;
167 }
168
169 if (pDirtyRect->yBottom < yBottom)
170 {
171 pDirtyRect->yBottom = yBottom;
172 }
173 }
174
175 if (pFBInfo->fDefaultFormat)
176 {
177 /// @todo pfnUpdateDisplayRect must take the vram offset parameter for the framebuffer
178 prgn->pPort->pfnUpdateDisplayRect(prgn->pPort, phdr->x, phdr->y, phdr->w, phdr->h);
179 prgn->pDisplay->i_handleDisplayUpdate(uScreenId, phdr->x, phdr->y, phdr->w, phdr->h);
180 }
181
182 return;
183}
184
185static void vbvaRgnUpdateFramebuffer(VBVADIRTYREGION *prgn, unsigned uScreenId)
186{
187 RTRECT *pDirtyRect = &prgn->aDirtyRects[uScreenId];
188 DISPLAYFBINFO *pFBInfo = &prgn->paFramebuffers[uScreenId];
189
190 uint32_t w = pDirtyRect->xRight - pDirtyRect->xLeft;
191 uint32_t h = pDirtyRect->yBottom - pDirtyRect->yTop;
192
193 if (!pFBInfo->fDefaultFormat && w != 0 && h != 0)
194 {
195 /// @todo pfnUpdateDisplayRect must take the vram offset parameter for the framebuffer
196 prgn->pPort->pfnUpdateDisplayRect(prgn->pPort, pDirtyRect->xLeft, pDirtyRect->yTop, w, h);
197 prgn->pDisplay->i_handleDisplayUpdate(uScreenId, pDirtyRect->xLeft, pDirtyRect->yTop, w, h);
198 }
199}
200
201void i_vbvaSetMemoryFlags(VBVAMEMORY *pVbvaMemory,
202 bool fVideoAccelEnabled,
203 bool fVideoAccelVRDP,
204 uint32_t fu32SupportedOrders,
205 DISPLAYFBINFO *paFBInfos,
206 unsigned cFBInfos)
207{
208 if (pVbvaMemory)
209 {
210 /* This called only on changes in mode. So reset VRDP always. */
211 uint32_t fu32Flags = VBVA_F_MODE_VRDP_RESET;
212
213 if (fVideoAccelEnabled)
214 {
215 fu32Flags |= VBVA_F_MODE_ENABLED;
216
217 if (fVideoAccelVRDP)
218 {
219 fu32Flags |= VBVA_F_MODE_VRDP | VBVA_F_MODE_VRDP_ORDER_MASK;
220
221 pVbvaMemory->fu32SupportedOrders = fu32SupportedOrders;
222 }
223 }
224
225 pVbvaMemory->fu32ModeFlags = fu32Flags;
226 }
227
228 unsigned uScreenId;
229 for (uScreenId = 0; uScreenId < cFBInfos; uScreenId++)
230 {
231 if (paFBInfos[uScreenId].pHostEvents)
232 {
233 paFBInfos[uScreenId].pHostEvents->fu32Events |= VBOX_VIDEO_INFO_HOST_EVENTS_F_VRDP_RESET;
234 }
235 }
236}
237
238bool Display::i_VideoAccelAllowed(void)
239{
240 return true;
241}
242
243int videoAccelEnterVGA(VIDEOACCEL *pVideoAccel)
244{
245 return RTSemXRoadsNSEnter(pVideoAccel->hXRoadsVideoAccel);
246}
247
248void videoAccelLeaveVGA(VIDEOACCEL *pVideoAccel)
249{
250 RTSemXRoadsNSLeave(pVideoAccel->hXRoadsVideoAccel);
251}
252
253int videoAccelEnterVMMDev(VIDEOACCEL *pVideoAccel)
254{
255 return RTSemXRoadsEWEnter(pVideoAccel->hXRoadsVideoAccel);
256}
257
258void videoAccelLeaveVMMDev(VIDEOACCEL *pVideoAccel)
259{
260 RTSemXRoadsEWLeave(pVideoAccel->hXRoadsVideoAccel);
261}
262
263/**
264 * @thread EMT
265 */
266int Display::i_VideoAccelEnable(bool fEnable, VBVAMEMORY *pVbvaMemory, PPDMIDISPLAYPORT pUpPort)
267{
268 LogRelFlowFunc(("fEnable = %d\n", fEnable));
269
270 int vrc = i_videoAccelEnable(fEnable, pVbvaMemory, pUpPort);
271
272 LogRelFlowFunc(("%Rrc.\n", vrc));
273 return vrc;
274}
275
276int Display::i_videoAccelEnable(bool fEnable, VBVAMEMORY *pVbvaMemory, PPDMIDISPLAYPORT pUpPort)
277{
278 VIDEOACCEL *pVideoAccel = &mVideoAccelLegacy;
279
280 /* Called each time the guest wants to use acceleration,
281 * or when the VGA device disables acceleration,
282 * or when restoring the saved state with accel enabled.
283 *
284 * VGA device disables acceleration on each video mode change
285 * and on reset.
286 *
287 * Guest enabled acceleration at will. And it has to enable
288 * acceleration after a mode change.
289 */
290 LogRelFlowFunc(("mfVideoAccelEnabled = %d, fEnable = %d, pVbvaMemory = %p\n",
291 pVideoAccel->fVideoAccelEnabled, fEnable, pVbvaMemory));
292
293 /* Strictly check parameters. Callers must not pass anything in the case. */
294 Assert((fEnable && pVbvaMemory) || (!fEnable && pVbvaMemory == NULL));
295
296 if (!i_VideoAccelAllowed ())
297 return VERR_NOT_SUPPORTED;
298
299 /* Check that current status is not being changed */
300 if (pVideoAccel->fVideoAccelEnabled == fEnable)
301 return VINF_SUCCESS;
302
303 if (pVideoAccel->fVideoAccelEnabled)
304 {
305 /* Process any pending orders and empty the VBVA ring buffer. */
306 i_videoAccelFlush (pUpPort);
307 }
308
309 if (!fEnable && pVideoAccel->pVbvaMemory)
310 pVideoAccel->pVbvaMemory->fu32ModeFlags &= ~VBVA_F_MODE_ENABLED;
311
312 if (fEnable)
313 {
314 /* Process any pending VGA device changes, resize. */
315 pUpPort->pfnUpdateDisplayAll(pUpPort, /* fFailOnResize = */ false);
316 }
317
318 /* Protect the videoaccel state transition. */
319 RTCritSectEnter(&mVideoAccelLock);
320
321 if (fEnable)
322 {
323 /* Initialize the hardware memory. */
324 i_vbvaSetMemoryFlags(pVbvaMemory, true, mfVideoAccelVRDP,
325 mfu32SupportedOrders, maFramebuffers, mcMonitors);
326 pVbvaMemory->off32Data = 0;
327 pVbvaMemory->off32Free = 0;
328
329 memset(pVbvaMemory->aRecords, 0, sizeof(pVbvaMemory->aRecords));
330 pVbvaMemory->indexRecordFirst = 0;
331 pVbvaMemory->indexRecordFree = 0;
332
333 pVideoAccel->pVbvaMemory = pVbvaMemory;
334 pVideoAccel->fVideoAccelEnabled = true;
335
336 LogRel(("VBVA: Enabled.\n"));
337 }
338 else
339 {
340 pVideoAccel->pVbvaMemory = NULL;
341 pVideoAccel->fVideoAccelEnabled = false;
342
343 LogRel(("VBVA: Disabled.\n"));
344 }
345
346 RTCritSectLeave(&mVideoAccelLock);
347
348 if (!fEnable)
349 {
350 pUpPort->pfnUpdateDisplayAll(pUpPort, /* fFailOnResize = */ false);
351 }
352
353 /* Notify the VMMDev, which saves VBVA status in the saved state,
354 * and needs to know current status.
355 */
356 VMMDev *pVMMDev = mParent->i_getVMMDev();
357 if (pVMMDev)
358 {
359 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
360 if (pVMMDevPort)
361 pVMMDevPort->pfnVBVAChange(pVMMDevPort, fEnable);
362 }
363
364 LogRelFlowFunc(("VINF_SUCCESS.\n"));
365 return VINF_SUCCESS;
366}
367
368static bool i_vbvaVerifyRingBuffer(VBVAMEMORY *pVbvaMemory)
369{
370 RT_NOREF(pVbvaMemory);
371 return true;
372}
373
374static void i_vbvaFetchBytes(VBVAMEMORY *pVbvaMemory, uint8_t *pu8Dst, uint32_t cbDst)
375{
376 if (cbDst >= VBVA_RING_BUFFER_SIZE)
377 {
378 AssertMsgFailed(("cbDst = 0x%08X, ring buffer size 0x%08X\n", cbDst, VBVA_RING_BUFFER_SIZE));
379 return;
380 }
381
382 uint32_t u32BytesTillBoundary = VBVA_RING_BUFFER_SIZE - pVbvaMemory->off32Data;
383 uint8_t *src = &pVbvaMemory->au8RingBuffer[pVbvaMemory->off32Data];
384 int32_t i32Diff = cbDst - u32BytesTillBoundary;
385
386 if (i32Diff <= 0)
387 {
388 /* Chunk will not cross buffer boundary. */
389 memcpy (pu8Dst, src, cbDst);
390 }
391 else
392 {
393 /* Chunk crosses buffer boundary. */
394 memcpy(pu8Dst, src, u32BytesTillBoundary);
395 memcpy(pu8Dst + u32BytesTillBoundary, &pVbvaMemory->au8RingBuffer[0], i32Diff);
396 }
397
398 /* Advance data offset. */
399 pVbvaMemory->off32Data = (pVbvaMemory->off32Data + cbDst) % VBVA_RING_BUFFER_SIZE;
400
401 return;
402}
403
404
405static bool i_vbvaPartialRead(uint8_t **ppu8, uint32_t *pcb, uint32_t cbRecord, VBVAMEMORY *pVbvaMemory)
406{
407 uint8_t *pu8New;
408
409 LogFlow(("MAIN::DisplayImpl::vbvaPartialRead: p = %p, cb = %d, cbRecord 0x%08X\n",
410 *ppu8, *pcb, cbRecord));
411
412 if (*ppu8)
413 {
414 Assert (*pcb);
415 pu8New = (uint8_t *)RTMemRealloc(*ppu8, cbRecord);
416 }
417 else
418 {
419 Assert (!*pcb);
420 pu8New = (uint8_t *)RTMemAlloc(cbRecord);
421 }
422
423 if (!pu8New)
424 {
425 /* Memory allocation failed, fail the function. */
426 Log(("MAIN::vbvaPartialRead: failed to (re)alocate memory for partial record!!! cbRecord 0x%08X\n",
427 cbRecord));
428
429 if (*ppu8)
430 {
431 RTMemFree(*ppu8);
432 }
433
434 *ppu8 = NULL;
435 *pcb = 0;
436
437 return false;
438 }
439
440 /* Fetch data from the ring buffer. */
441 i_vbvaFetchBytes(pVbvaMemory, pu8New + *pcb, cbRecord - *pcb);
442
443 *ppu8 = pu8New;
444 *pcb = cbRecord;
445
446 return true;
447}
448
449/* For contiguous chunks just return the address in the buffer.
450 * For crossing boundary - allocate a buffer from heap.
451 */
452static bool i_vbvaFetchCmd(VIDEOACCEL *pVideoAccel, VBVACMDHDR **ppHdr, uint32_t *pcbCmd)
453{
454 VBVAMEMORY *pVbvaMemory = pVideoAccel->pVbvaMemory;
455
456 uint32_t indexRecordFirst = pVbvaMemory->indexRecordFirst;
457 uint32_t indexRecordFree = pVbvaMemory->indexRecordFree;
458
459#ifdef DEBUG_sunlover
460 LogFlowFunc(("first = %d, free = %d\n",
461 indexRecordFirst, indexRecordFree));
462#endif /* DEBUG_sunlover */
463
464 if (!i_vbvaVerifyRingBuffer(pVbvaMemory))
465 {
466 return false;
467 }
468
469 if (indexRecordFirst == indexRecordFree)
470 {
471 /* No records to process. Return without assigning output variables. */
472 return true;
473 }
474
475 uint32_t cbRecordCurrent = ASMAtomicReadU32(&pVbvaMemory->aRecords[indexRecordFirst].cbRecord);
476
477#ifdef DEBUG_sunlover
478 LogFlowFunc(("cbRecord = 0x%08X\n", cbRecordCurrent));
479#endif /* DEBUG_sunlover */
480
481 uint32_t cbRecord = cbRecordCurrent & ~VBVA_F_RECORD_PARTIAL;
482
483 if (pVideoAccel->cbVbvaPartial)
484 {
485 /* There is a partial read in process. Continue with it. */
486
487 Assert(pVideoAccel->pu8VbvaPartial);
488
489 LogFlowFunc(("continue partial record cbVbvaPartial = %d cbRecord 0x%08X, first = %d, free = %d\n",
490 pVideoAccel->cbVbvaPartial, cbRecordCurrent, indexRecordFirst, indexRecordFree));
491
492 if (cbRecord > pVideoAccel->cbVbvaPartial)
493 {
494 /* New data has been added to the record. */
495 if (!i_vbvaPartialRead(&pVideoAccel->pu8VbvaPartial, &pVideoAccel->cbVbvaPartial, cbRecord, pVbvaMemory))
496 {
497 return false;
498 }
499 }
500
501 if (!(cbRecordCurrent & VBVA_F_RECORD_PARTIAL))
502 {
503 /* The record is completed by guest. Return it to the caller. */
504 *ppHdr = (VBVACMDHDR *)pVideoAccel->pu8VbvaPartial;
505 *pcbCmd = pVideoAccel->cbVbvaPartial;
506
507 pVideoAccel->pu8VbvaPartial = NULL;
508 pVideoAccel->cbVbvaPartial = 0;
509
510 /* Advance the record index. */
511 pVbvaMemory->indexRecordFirst = (indexRecordFirst + 1) % VBVA_MAX_RECORDS;
512
513#ifdef DEBUG_sunlover
514 LogFlowFunc(("partial done ok, data = %d, free = %d\n",
515 pVbvaMemory->off32Data, pVbvaMemory->off32Free));
516#endif /* DEBUG_sunlover */
517 }
518
519 return true;
520 }
521
522 /* A new record need to be processed. */
523 if (cbRecordCurrent & VBVA_F_RECORD_PARTIAL)
524 {
525 /* Current record is being written by guest. '=' is important here. */
526 if (cbRecord >= VBVA_RING_BUFFER_SIZE - VBVA_RING_BUFFER_THRESHOLD)
527 {
528 /* Partial read must be started. */
529 if (!i_vbvaPartialRead(&pVideoAccel->pu8VbvaPartial, &pVideoAccel->cbVbvaPartial, cbRecord, pVbvaMemory))
530 {
531 return false;
532 }
533
534 LogFlowFunc(("started partial record cbVbvaPartial = 0x%08X cbRecord 0x%08X, first = %d, free = %d\n",
535 pVideoAccel->cbVbvaPartial, cbRecordCurrent, indexRecordFirst, indexRecordFree));
536 }
537
538 return true;
539 }
540
541 /* Current record is complete. If it is not empty, process it. */
542 if (cbRecord)
543 {
544 /* The size of largest contiguous chunk in the ring biffer. */
545 uint32_t u32BytesTillBoundary = VBVA_RING_BUFFER_SIZE - pVbvaMemory->off32Data;
546
547 /* The ring buffer pointer. */
548 uint8_t *au8RingBuffer = &pVbvaMemory->au8RingBuffer[0];
549
550 /* The pointer to data in the ring buffer. */
551 uint8_t *src = &au8RingBuffer[pVbvaMemory->off32Data];
552
553 /* Fetch or point the data. */
554 if (u32BytesTillBoundary >= cbRecord)
555 {
556 /* The command does not cross buffer boundary. Return address in the buffer. */
557 *ppHdr = (VBVACMDHDR *)src;
558
559 /* Advance data offset. */
560 pVbvaMemory->off32Data = (pVbvaMemory->off32Data + cbRecord) % VBVA_RING_BUFFER_SIZE;
561 }
562 else
563 {
564 /* The command crosses buffer boundary. Rare case, so not optimized. */
565 uint8_t *dst = (uint8_t *)RTMemAlloc(cbRecord);
566
567 if (!dst)
568 {
569 LogRelFlowFunc(("could not allocate %d bytes from heap!!!\n", cbRecord));
570 pVbvaMemory->off32Data = (pVbvaMemory->off32Data + cbRecord) % VBVA_RING_BUFFER_SIZE;
571 return false;
572 }
573
574 i_vbvaFetchBytes(pVbvaMemory, dst, cbRecord);
575
576 *ppHdr = (VBVACMDHDR *)dst;
577
578#ifdef DEBUG_sunlover
579 LogFlowFunc(("Allocated from heap %p\n", dst));
580#endif /* DEBUG_sunlover */
581 }
582 }
583
584 *pcbCmd = cbRecord;
585
586 /* Advance the record index. */
587 pVbvaMemory->indexRecordFirst = (indexRecordFirst + 1) % VBVA_MAX_RECORDS;
588
589#ifdef DEBUG_sunlover
590 LogFlowFunc(("done ok, data = %d, free = %d\n",
591 pVbvaMemory->off32Data, pVbvaMemory->off32Free));
592#endif /* DEBUG_sunlover */
593
594 return true;
595}
596
597static void i_vbvaReleaseCmd(VIDEOACCEL *pVideoAccel, VBVACMDHDR *pHdr, int32_t cbCmd)
598{
599 RT_NOREF(cbCmd);
600 uint8_t *au8RingBuffer = pVideoAccel->pVbvaMemory->au8RingBuffer;
601
602 if ( (uint8_t *)pHdr >= au8RingBuffer
603 && (uint8_t *)pHdr < &au8RingBuffer[VBVA_RING_BUFFER_SIZE])
604 {
605 /* The pointer is inside ring buffer. Must be continuous chunk. */
606 Assert(VBVA_RING_BUFFER_SIZE - ((uint8_t *)pHdr - au8RingBuffer) >= cbCmd);
607
608 /* Do nothing. */
609
610 Assert(!pVideoAccel->pu8VbvaPartial && pVideoAccel->cbVbvaPartial == 0);
611 }
612 else
613 {
614 /* The pointer is outside. It is then an allocated copy. */
615
616#ifdef DEBUG_sunlover
617 LogFlowFunc(("Free heap %p\n", pHdr));
618#endif /* DEBUG_sunlover */
619
620 if ((uint8_t *)pHdr == pVideoAccel->pu8VbvaPartial)
621 {
622 pVideoAccel->pu8VbvaPartial = NULL;
623 pVideoAccel->cbVbvaPartial = 0;
624 }
625 else
626 {
627 Assert(!pVideoAccel->pu8VbvaPartial && pVideoAccel->cbVbvaPartial == 0);
628 }
629
630 RTMemFree(pHdr);
631 }
632
633 return;
634}
635
636
637/**
638 * Called regularly on the DisplayRefresh timer.
639 * Also on behalf of guest, when the ring buffer is full.
640 *
641 * @thread EMT
642 */
643void Display::i_VideoAccelFlush(PPDMIDISPLAYPORT pUpPort)
644{
645 int vrc = i_videoAccelFlush(pUpPort);
646 if (RT_FAILURE(vrc))
647 {
648 /* Disable on errors. */
649 i_videoAccelEnable(false, NULL, pUpPort);
650 }
651}
652
653int Display::i_videoAccelFlush(PPDMIDISPLAYPORT pUpPort)
654{
655 VIDEOACCEL *pVideoAccel = &mVideoAccelLegacy;
656 VBVAMEMORY *pVbvaMemory = pVideoAccel->pVbvaMemory;
657
658#ifdef DEBUG_sunlover_2
659 LogFlowFunc(("fVideoAccelEnabled = %d\n", pVideoAccel->fVideoAccelEnabled));
660#endif /* DEBUG_sunlover_2 */
661
662 if (!pVideoAccel->fVideoAccelEnabled)
663 {
664 Log(("Display::VideoAccelFlush: called with disabled VBVA!!! Ignoring.\n"));
665 return VINF_SUCCESS;
666 }
667
668 /* Here VBVA is enabled and we have the accelerator memory pointer. */
669 Assert(pVbvaMemory);
670
671#ifdef DEBUG_sunlover_2
672 LogFlowFunc(("indexRecordFirst = %d, indexRecordFree = %d, off32Data = %d, off32Free = %d\n",
673 pVbvaMemory->indexRecordFirst, pVbvaMemory->indexRecordFree,
674 pVbvaMemory->off32Data, pVbvaMemory->off32Free));
675#endif /* DEBUG_sunlover_2 */
676
677 /* Quick check for "nothing to update" case. */
678 if (pVbvaMemory->indexRecordFirst == pVbvaMemory->indexRecordFree)
679 {
680 return VINF_SUCCESS;
681 }
682
683 /* Process the ring buffer */
684 unsigned uScreenId;
685
686 /* Initialize dirty rectangles accumulator. */
687 VBVADIRTYREGION rgn;
688 vbvaRgnInit(&rgn, maFramebuffers, mcMonitors, this, pUpPort);
689
690 for (;;)
691 {
692 VBVACMDHDR *phdr = NULL;
693 uint32_t cbCmd = UINT32_MAX;
694
695 /* Fetch the command data. */
696 if (!i_vbvaFetchCmd(pVideoAccel, &phdr, &cbCmd))
697 {
698 Log(("Display::VideoAccelFlush: unable to fetch command. off32Data = %d, off32Free = %d. Disabling VBVA!!!\n",
699 pVbvaMemory->off32Data, pVbvaMemory->off32Free));
700 return VERR_INVALID_STATE;
701 }
702
703 if (cbCmd == uint32_t(~0))
704 {
705 /* No more commands yet in the queue. */
706#ifdef DEBUG_sunlover
707 LogFlowFunc(("no command\n"));
708#endif /* DEBUG_sunlover */
709 break;
710 }
711
712 if (cbCmd != 0)
713 {
714#ifdef DEBUG_sunlover
715 LogFlowFunc(("hdr: cbCmd = %d, x=%d, y=%d, w=%d, h=%d\n",
716 cbCmd, phdr->x, phdr->y, phdr->w, phdr->h));
717#endif /* DEBUG_sunlover */
718
719 VBVACMDHDR hdrSaved = *phdr;
720
721 int x = phdr->x;
722 int y = phdr->y;
723 int w = phdr->w;
724 int h = phdr->h;
725
726 uScreenId = mapCoordsToScreen(maFramebuffers, mcMonitors, &x, &y, &w, &h);
727
728 phdr->x = (int16_t)x;
729 phdr->y = (int16_t)y;
730 phdr->w = (uint16_t)w;
731 phdr->h = (uint16_t)h;
732
733 /* Handle the command.
734 *
735 * Guest is responsible for updating the guest video memory.
736 * The Windows guest does all drawing using Eng*.
737 *
738 * For local output, only dirty rectangle information is used
739 * to update changed areas.
740 *
741 * Dirty rectangles are accumulated to exclude overlapping updates and
742 * group small updates to a larger one.
743 */
744
745 /* Accumulate the update. */
746 vbvaRgnDirtyRect(&rgn, uScreenId, phdr);
747
748 /* Forward the command to VRDP server. */
749 mParent->i_consoleVRDPServer()->SendUpdate(uScreenId, phdr, cbCmd);
750
751 *phdr = hdrSaved;
752 }
753
754 i_vbvaReleaseCmd(pVideoAccel, phdr, cbCmd);
755 }
756
757 for (uScreenId = 0; uScreenId < mcMonitors; uScreenId++)
758 {
759 /* Draw the framebuffer. */
760 vbvaRgnUpdateFramebuffer(&rgn, uScreenId);
761 }
762 return VINF_SUCCESS;
763}
764
765int Display::i_videoAccelRefreshProcess(PPDMIDISPLAYPORT pUpPort)
766{
767 int vrc = VWRN_INVALID_STATE; /* Default is to do a display update in VGA device. */
768
769 VIDEOACCEL *pVideoAccel = &mVideoAccelLegacy;
770
771 videoAccelEnterVGA(pVideoAccel);
772
773 if (pVideoAccel->fVideoAccelEnabled)
774 {
775 Assert(pVideoAccel->pVbvaMemory);
776 vrc = i_videoAccelFlush(pUpPort);
777 if (RT_FAILURE(vrc))
778 {
779 /* Disable on errors. */
780 i_videoAccelEnable(false, NULL, pUpPort);
781 vrc = VWRN_INVALID_STATE; /* Do a display update in VGA device. */
782 }
783 else
784 {
785 vrc = VINF_SUCCESS;
786 }
787 }
788
789 videoAccelLeaveVGA(pVideoAccel);
790
791 return vrc;
792}
793
794void Display::processAdapterData(void *pvVRAM, uint32_t u32VRAMSize)
795{
796 RT_NOREF(u32VRAMSize);
797 if (pvVRAM == NULL)
798 {
799 unsigned i;
800 for (i = 0; i < mcMonitors; i++)
801 {
802 DISPLAYFBINFO *pFBInfo = &maFramebuffers[i];
803
804 pFBInfo->u32Offset = 0;
805 pFBInfo->u32MaxFramebufferSize = 0;
806 pFBInfo->u32InformationSize = 0;
807 }
808 }
809#ifndef VBOX_WITH_HGSMI
810 else
811 {
812 uint8_t *pu8 = (uint8_t *)pvVRAM;
813 pu8 += u32VRAMSize - VBOX_VIDEO_ADAPTER_INFORMATION_SIZE;
814
815 /// @todo
816 uint8_t *pu8End = pu8 + VBOX_VIDEO_ADAPTER_INFORMATION_SIZE;
817
818 VBOXVIDEOINFOHDR *pHdr;
819
820 for (;;)
821 {
822 pHdr = (VBOXVIDEOINFOHDR *)pu8;
823 pu8 += sizeof(VBOXVIDEOINFOHDR);
824
825 if (pu8 >= pu8End)
826 {
827 LogRel(("VBoxVideo: Guest adapter information overflow!!!\n"));
828 break;
829 }
830
831 if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_DISPLAY)
832 {
833 if (pHdr->u16Length != sizeof(VBOXVIDEOINFODISPLAY))
834 {
835 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "DISPLAY", pHdr->u16Length));
836 break;
837 }
838
839 VBOXVIDEOINFODISPLAY *pDisplay = (VBOXVIDEOINFODISPLAY *)pu8;
840
841 if (pDisplay->u32Index >= mcMonitors)
842 {
843 LogRel(("VBoxVideo: Guest adapter information invalid display index %d!!!\n", pDisplay->u32Index));
844 break;
845 }
846
847 DISPLAYFBINFO *pFBInfo = &maFramebuffers[pDisplay->u32Index];
848
849 pFBInfo->u32Offset = pDisplay->u32Offset;
850 pFBInfo->u32MaxFramebufferSize = pDisplay->u32FramebufferSize;
851 pFBInfo->u32InformationSize = pDisplay->u32InformationSize;
852
853 LogRelFlow(("VBOX_VIDEO_INFO_TYPE_DISPLAY: %d: at 0x%08X, size 0x%08X, info 0x%08X\n", pDisplay->u32Index,
854 pDisplay->u32Offset, pDisplay->u32FramebufferSize, pDisplay->u32InformationSize));
855 }
856 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_QUERY_CONF32)
857 {
858 if (pHdr->u16Length != sizeof(VBOXVIDEOINFOQUERYCONF32))
859 {
860 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "CONF32", pHdr->u16Length));
861 break;
862 }
863
864 VBOXVIDEOINFOQUERYCONF32 *pConf32 = (VBOXVIDEOINFOQUERYCONF32 *)pu8;
865
866 switch (pConf32->u32Index)
867 {
868 case VBOX_VIDEO_QCI32_MONITOR_COUNT:
869 {
870 pConf32->u32Value = mcMonitors;
871 } break;
872
873 case VBOX_VIDEO_QCI32_OFFSCREEN_HEAP_SIZE:
874 {
875 /** @todo make configurable. */
876 pConf32->u32Value = _1M;
877 } break;
878
879 default:
880 LogRel(("VBoxVideo: CONF32 %d not supported!!! Skipping.\n", pConf32->u32Index));
881 }
882 }
883 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_END)
884 {
885 if (pHdr->u16Length != 0)
886 {
887 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "END", pHdr->u16Length));
888 break;
889 }
890
891 break;
892 }
893 else if (pHdr->u8Type != VBOX_VIDEO_INFO_TYPE_NV_HEAP)
894 {
895 /** @todo why is Additions/WINNT/Graphics/Miniport/VBoxVideo. cpp pushing this to us? */
896 LogRel(("Guest adapter information contains unsupported type %d. The block has been skipped.\n", pHdr->u8Type));
897 }
898
899 pu8 += pHdr->u16Length;
900 }
901 }
902#endif /* !VBOX_WITH_HGSMI */
903}
904
905void Display::processDisplayData(void *pvVRAM, unsigned uScreenId)
906{
907 if (uScreenId >= mcMonitors)
908 {
909 LogRel(("VBoxVideo: Guest display information invalid display index %d!!!\n", uScreenId));
910 return;
911 }
912
913 /* Get the display information structure. */
914 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
915
916 uint8_t *pu8 = (uint8_t *)pvVRAM;
917 pu8 += pFBInfo->u32Offset + pFBInfo->u32MaxFramebufferSize;
918
919 /// @todo
920 uint8_t *pu8End = pu8 + pFBInfo->u32InformationSize;
921
922 VBOXVIDEOINFOHDR *pHdr;
923
924 for (;;)
925 {
926 pHdr = (VBOXVIDEOINFOHDR *)pu8;
927 pu8 += sizeof(VBOXVIDEOINFOHDR);
928
929 if (pu8 >= pu8End)
930 {
931 LogRel(("VBoxVideo: Guest display information overflow!!!\n"));
932 break;
933 }
934
935 if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_SCREEN)
936 {
937 if (pHdr->u16Length != sizeof(VBOXVIDEOINFOSCREEN))
938 {
939 LogRel(("VBoxVideo: Guest display information %s invalid length %d!!!\n", "SCREEN", pHdr->u16Length));
940 break;
941 }
942
943 VBOXVIDEOINFOSCREEN *pScreen = (VBOXVIDEOINFOSCREEN *)pu8;
944
945 pFBInfo->xOrigin = pScreen->xOrigin;
946 pFBInfo->yOrigin = pScreen->yOrigin;
947
948 pFBInfo->w = pScreen->u16Width;
949 pFBInfo->h = pScreen->u16Height;
950
951 LogRelFlow(("VBOX_VIDEO_INFO_TYPE_SCREEN: (%p) %d: at %d,%d, linesize 0x%X, size %dx%d, bpp %d, flags 0x%02X\n",
952 pHdr, uScreenId, pScreen->xOrigin, pScreen->yOrigin, pScreen->u32LineSize, pScreen->u16Width,
953 pScreen->u16Height, pScreen->bitsPerPixel, pScreen->u8Flags));
954
955 if (uScreenId != VBOX_VIDEO_PRIMARY_SCREEN)
956 {
957 /* Primary screen resize is eeeeeeeee by the VGA device. */
958 if (pFBInfo->fDisabled)
959 {
960 pFBInfo->fDisabled = false;
961 ::FireGuestMonitorChangedEvent(mParent->i_getEventSource(), GuestMonitorChangedEventType_Enabled, uScreenId,
962 pFBInfo->xOrigin, pFBInfo->yOrigin, pFBInfo->w, pFBInfo->h);
963 }
964
965 i_handleDisplayResize(uScreenId, pScreen->bitsPerPixel,
966 (uint8_t *)pvVRAM + pFBInfo->u32Offset,
967 pScreen->u32LineSize,
968 pScreen->u16Width, pScreen->u16Height,
969 VBVA_SCREEN_F_ACTIVE,
970 pScreen->xOrigin, pScreen->yOrigin, false);
971 }
972 }
973 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_END)
974 {
975 if (pHdr->u16Length != 0)
976 {
977 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "END", pHdr->u16Length));
978 break;
979 }
980
981 break;
982 }
983 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_HOST_EVENTS)
984 {
985 if (pHdr->u16Length != sizeof(VBOXVIDEOINFOHOSTEVENTS))
986 {
987 LogRel(("VBoxVideo: Guest display information %s invalid length %d!!!\n", "HOST_EVENTS", pHdr->u16Length));
988 break;
989 }
990
991 VBOXVIDEOINFOHOSTEVENTS *pHostEvents = (VBOXVIDEOINFOHOSTEVENTS *)pu8;
992
993 pFBInfo->pHostEvents = pHostEvents;
994
995 LogFlow(("VBOX_VIDEO_INFO_TYPE_HOSTEVENTS: (%p)\n",
996 pHostEvents));
997 }
998 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_LINK)
999 {
1000 if (pHdr->u16Length != sizeof(VBOXVIDEOINFOLINK))
1001 {
1002 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "LINK", pHdr->u16Length));
1003 break;
1004 }
1005
1006 VBOXVIDEOINFOLINK *pLink = (VBOXVIDEOINFOLINK *)pu8;
1007 pu8 += pLink->i32Offset;
1008 }
1009 else
1010 {
1011 LogRel(("Guest display information contains unsupported type %d\n", pHdr->u8Type));
1012 }
1013
1014 pu8 += pHdr->u16Length;
1015 }
1016}
1017
1018/* vi: set tabstop=4 shiftwidth=4 expandtab: */
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