VirtualBox

source: vbox/trunk/src/VBox/Devices/Graphics/DevVGA_VBVA.cpp@ 34490

Last change on this file since 34490 was 34490, checked in by vboxsync, 14 years ago

VBoxVideo: remove leading underscore from structure names and make VBoxVideoGuest.h C-friendly

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 64.6 KB
Line 
1/** @file
2 * VirtualBox Video Acceleration (VBVA).
3 */
4
5/*
6 * Copyright (C) 2006-2009 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 */
16
17#define LOG_GROUP LOG_GROUP_DEV_VGA
18#include <VBox/pdmifs.h>
19#include <VBox/pdmdev.h>
20#include <VBox/pgm.h>
21#include <VBox/ssm.h>
22#include <VBox/VMMDev.h>
23#include <VBox/VBoxVideo.h>
24#include <iprt/alloc.h>
25#include <iprt/assert.h>
26#include <iprt/asm.h>
27#include <iprt/string.h>
28#include <iprt/param.h>
29#ifdef VBOX_WITH_VIDEOHWACCEL
30#include <iprt/semaphore.h>
31#endif
32
33#include "DevVGA.h"
34
35/* A very detailed logging. */
36#if 0 // def DEBUG_sunlover
37#define LOGVBVABUFFER(a) LogFlow(a)
38#else
39#define LOGVBVABUFFER(a) do {} while(0)
40#endif
41
42typedef struct VBVAPARTIALRECORD
43{
44 uint8_t *pu8;
45 uint32_t cb;
46} VBVAPARTIALRECORD;
47
48typedef struct VBVAVIEW
49{
50 VBVAINFOVIEW view;
51 VBVAINFOSCREEN screen;
52 VBVABUFFER *pVBVA;
53 uint32_t u32VBVAOffset;
54 VBVAPARTIALRECORD partialRecord;
55} VBVAVIEW;
56
57typedef struct VBVAMOUSESHAPEINFO
58{
59 bool fSet;
60 bool fVisible;
61 bool fAlpha;
62 uint32_t u32HotX;
63 uint32_t u32HotY;
64 uint32_t u32Width;
65 uint32_t u32Height;
66 uint32_t cbShape;
67 uint32_t cbAllocated;
68 uint8_t *pu8Shape;
69} VBVAMOUSESHAPEINFO;
70
71/* @todo saved state: save and restore VBVACONTEXT */
72typedef struct VBVACONTEXT
73{
74 uint32_t cViews;
75 VBVAVIEW aViews[64 /* @todo SchemaDefs::MaxGuestMonitors*/];
76 VBVAMOUSESHAPEINFO mouseShapeInfo;
77} VBVACONTEXT;
78
79/* Copies 'cb' bytes from the VBVA ring buffer to the 'pu8Dst'.
80 * Used for partial records or for records which cross the ring boundary.
81 */
82static void vbvaFetchBytes (VBVABUFFER *pVBVA, uint8_t *pu8Dst, uint32_t cb)
83{
84 /* @todo replace the 'if' with an assert. The caller must ensure this condition. */
85 if (cb >= pVBVA->cbData)
86 {
87 AssertMsgFailed (("cb = 0x%08X, ring buffer size 0x%08X", cb, pVBVA->cbData));
88 return;
89 }
90
91 const uint32_t u32BytesTillBoundary = pVBVA->cbData - pVBVA->off32Data;
92 const uint8_t *src = &pVBVA->au8Data[pVBVA->off32Data];
93 const int32_t i32Diff = cb - u32BytesTillBoundary;
94
95 if (i32Diff <= 0)
96 {
97 /* Chunk will not cross buffer boundary. */
98 memcpy (pu8Dst, src, cb);
99 }
100 else
101 {
102 /* Chunk crosses buffer boundary. */
103 memcpy (pu8Dst, src, u32BytesTillBoundary);
104 memcpy (pu8Dst + u32BytesTillBoundary, &pVBVA->au8Data[0], i32Diff);
105 }
106
107 /* Advance data offset. */
108 pVBVA->off32Data = (pVBVA->off32Data + cb) % pVBVA->cbData;
109
110 return;
111}
112
113
114static bool vbvaPartialRead (VBVAPARTIALRECORD *pPartialRecord, uint32_t cbRecord, VBVABUFFER *pVBVA)
115{
116 uint8_t *pu8New;
117
118 LOGVBVABUFFER(("vbvaPartialRead: p = %p, cb = %d, cbRecord 0x%08X\n",
119 pPartialRecord->pu8, pPartialRecord->cb, cbRecord));
120
121 if (pPartialRecord->pu8)
122 {
123 Assert (pPartialRecord->cb);
124 pu8New = (uint8_t *)RTMemRealloc (pPartialRecord->pu8, cbRecord);
125 }
126 else
127 {
128 Assert (!pPartialRecord->cb);
129 pu8New = (uint8_t *)RTMemAlloc (cbRecord);
130 }
131
132 if (!pu8New)
133 {
134 /* Memory allocation failed, fail the function. */
135 Log(("vbvaPartialRead: failed to (re)alocate memory for partial record!!! cbRecord 0x%08X\n",
136 cbRecord));
137
138 if (pPartialRecord->pu8)
139 {
140 RTMemFree (pPartialRecord->pu8);
141 }
142
143 pPartialRecord->pu8 = NULL;
144 pPartialRecord->cb = 0;
145
146 return false;
147 }
148
149 /* Fetch data from the ring buffer. */
150 vbvaFetchBytes (pVBVA, pu8New + pPartialRecord->cb, cbRecord - pPartialRecord->cb);
151
152 pPartialRecord->pu8 = pu8New;
153 pPartialRecord->cb = cbRecord;
154
155 return true;
156}
157
158/* For contiguous chunks just return the address in the buffer.
159 * For crossing boundary - allocate a buffer from heap.
160 */
161static bool vbvaFetchCmd (VBVAPARTIALRECORD *pPartialRecord, VBVABUFFER *pVBVA, VBVACMDHDR **ppHdr, uint32_t *pcbCmd)
162{
163 uint32_t indexRecordFirst = pVBVA->indexRecordFirst;
164 uint32_t indexRecordFree = pVBVA->indexRecordFree;
165
166 LOGVBVABUFFER(("first = %d, free = %d\n",
167 indexRecordFirst, indexRecordFree));
168
169 if (indexRecordFirst == indexRecordFree)
170 {
171 /* No records to process. Return without assigning output variables. */
172 return true;
173 }
174
175 VBVARECORD *pRecord = &pVBVA->aRecords[indexRecordFirst];
176
177 LOGVBVABUFFER(("cbRecord = 0x%08X\n", pRecord->cbRecord));
178
179 uint32_t cbRecord = pRecord->cbRecord & ~VBVA_F_RECORD_PARTIAL;
180
181 if (pPartialRecord->cb)
182 {
183 /* There is a partial read in process. Continue with it. */
184 Assert (pPartialRecord->pu8);
185
186 LOGVBVABUFFER(("continue partial record cb = %d cbRecord 0x%08X, first = %d, free = %d\n",
187 pPartialRecord->cb, pRecord->cbRecord, indexRecordFirst, indexRecordFree));
188
189 if (cbRecord > pPartialRecord->cb)
190 {
191 /* New data has been added to the record. */
192 if (!vbvaPartialRead (pPartialRecord, cbRecord, pVBVA))
193 {
194 return false;
195 }
196 }
197
198 if (!(pRecord->cbRecord & VBVA_F_RECORD_PARTIAL))
199 {
200 /* The record is completed by guest. Return it to the caller. */
201 *ppHdr = (VBVACMDHDR *)pPartialRecord->pu8;
202 *pcbCmd = pPartialRecord->cb;
203
204 pPartialRecord->pu8 = NULL;
205 pPartialRecord->cb = 0;
206
207 /* Advance the record index. */
208 pVBVA->indexRecordFirst = (indexRecordFirst + 1) % RT_ELEMENTS(pVBVA->aRecords);
209
210 LOGVBVABUFFER(("partial done ok, data = %d, free = %d\n",
211 pVBVA->off32Data, pVBVA->off32Free));
212 }
213
214 return true;
215 }
216
217 /* A new record need to be processed. */
218 if (pRecord->cbRecord & VBVA_F_RECORD_PARTIAL)
219 {
220 /* Current record is being written by guest. '=' is important here,
221 * because the guest will do a FLUSH at this condition.
222 * This partial record is too large for the ring buffer and must
223 * be accumulated in an allocated buffer.
224 */
225 if (cbRecord >= pVBVA->cbData - pVBVA->cbPartialWriteThreshold)
226 {
227 /* Partial read must be started. */
228 if (!vbvaPartialRead (pPartialRecord, cbRecord, pVBVA))
229 {
230 return false;
231 }
232
233 LOGVBVABUFFER(("started partial record cb = 0x%08X cbRecord 0x%08X, first = %d, free = %d\n",
234 pPartialRecord->cb, pRecord->cbRecord, indexRecordFirst, indexRecordFree));
235 }
236
237 return true;
238 }
239
240 /* Current record is complete. If it is not empty, process it. */
241 if (cbRecord)
242 {
243 /* The size of largest contiguous chunk in the ring biffer. */
244 uint32_t u32BytesTillBoundary = pVBVA->cbData - pVBVA->off32Data;
245
246 /* The pointer to data in the ring buffer. */
247 uint8_t *src = &pVBVA->au8Data[pVBVA->off32Data];
248
249 /* Fetch or point the data. */
250 if (u32BytesTillBoundary >= cbRecord)
251 {
252 /* The command does not cross buffer boundary. Return address in the buffer. */
253 *ppHdr = (VBVACMDHDR *)src;
254
255 /* Advance data offset. */
256 pVBVA->off32Data = (pVBVA->off32Data + cbRecord) % pVBVA->cbData;
257 }
258 else
259 {
260 /* The command crosses buffer boundary. Rare case, so not optimized. */
261 uint8_t *dst = (uint8_t *)RTMemAlloc (cbRecord);
262
263 if (!dst)
264 {
265 LogFlowFunc (("could not allocate %d bytes from heap!!!\n", cbRecord));
266 pVBVA->off32Data = (pVBVA->off32Data + cbRecord) % pVBVA->cbData;
267 return false;
268 }
269
270 vbvaFetchBytes (pVBVA, dst, cbRecord);
271
272 *ppHdr = (VBVACMDHDR *)dst;
273
274 LOGVBVABUFFER(("Allocated from heap %p\n", dst));
275 }
276 }
277
278 *pcbCmd = cbRecord;
279
280 /* Advance the record index. */
281 pVBVA->indexRecordFirst = (indexRecordFirst + 1) % RT_ELEMENTS(pVBVA->aRecords);
282
283 LOGVBVABUFFER(("done ok, data = %d, free = %d\n",
284 pVBVA->off32Data, pVBVA->off32Free));
285
286 return true;
287}
288
289static void vbvaReleaseCmd (VBVAPARTIALRECORD *pPartialRecord, VBVABUFFER *pVBVA, VBVACMDHDR *pHdr, uint32_t cbCmd)
290{
291 uint8_t *au8RingBuffer = &pVBVA->au8Data[0];
292
293 if ( (uint8_t *)pHdr >= au8RingBuffer
294 && (uint8_t *)pHdr < &au8RingBuffer[pVBVA->cbData])
295 {
296 /* The pointer is inside ring buffer. Must be continuous chunk. */
297 Assert (pVBVA->cbData - ((uint8_t *)pHdr - au8RingBuffer) >= cbCmd);
298
299 /* Do nothing. */
300
301 Assert (!pPartialRecord->pu8 && pPartialRecord->cb == 0);
302 }
303 else
304 {
305 /* The pointer is outside. It is then an allocated copy. */
306 LOGVBVABUFFER(("Free heap %p\n", pHdr));
307
308 if ((uint8_t *)pHdr == pPartialRecord->pu8)
309 {
310 pPartialRecord->pu8 = NULL;
311 pPartialRecord->cb = 0;
312 }
313 else
314 {
315 Assert (!pPartialRecord->pu8 && pPartialRecord->cb == 0);
316 }
317
318 RTMemFree (pHdr);
319 }
320
321 return;
322}
323
324static int vbvaFlushProcess (unsigned uScreenId, PVGASTATE pVGAState, VBVAPARTIALRECORD *pPartialRecord, VBVABUFFER *pVBVA)
325{
326 LOGVBVABUFFER(("uScreenId %d, indexRecordFirst = %d, indexRecordFree = %d, off32Data = %d, off32Free = %d\n",
327 uScreenId, pVBVA->indexRecordFirst, pVBVA->indexRecordFree, pVBVA->off32Data, pVBVA->off32Free));
328 struct {
329 /* The rectangle that includes all dirty rectangles. */
330 int32_t xLeft;
331 int32_t xRight;
332 int32_t yTop;
333 int32_t yBottom;
334 } dirtyRect;
335 RT_ZERO(dirtyRect);
336
337 bool fUpdate = false; /* Whether there were any updates. */
338 bool fDirtyEmpty = true;
339
340 for (;;)
341 {
342 VBVACMDHDR *phdr = NULL;
343 uint32_t cbCmd = ~0;
344
345 /* Fetch the command data. */
346 if (!vbvaFetchCmd (pPartialRecord, pVBVA, &phdr, &cbCmd))
347 {
348 LogFunc(("unable to fetch command. off32Data = %d, off32Free = %d!!!\n",
349 pVBVA->off32Data, pVBVA->off32Free));
350
351 /* @todo old code disabled VBVA processing here. */
352 return VERR_NOT_SUPPORTED;
353 }
354
355 if (cbCmd == uint32_t(~0))
356 {
357 /* No more commands yet in the queue. */
358 break;
359 }
360
361 if (cbCmd != 0)
362 {
363 if (!fUpdate)
364 {
365 pVGAState->pDrv->pfnVBVAUpdateBegin (pVGAState->pDrv, uScreenId);
366 fUpdate = true;
367 }
368
369 /* Updates the rectangle and sends the command to the VRDP server. */
370 pVGAState->pDrv->pfnVBVAUpdateProcess (pVGAState->pDrv, uScreenId, phdr, cbCmd);
371
372 int32_t xRight = phdr->x + phdr->w;
373 int32_t yBottom = phdr->y + phdr->h;
374
375 /* These are global coords, relative to the primary screen. */
376
377 LOGVBVABUFFER(("cbCmd = %d, x=%d, y=%d, w=%d, h=%d\n",
378 cbCmd, phdr->x, phdr->y, phdr->w, phdr->h));
379
380 /* Collect all rects into one. */
381 if (fDirtyEmpty)
382 {
383 /* This is the first rectangle to be added. */
384 dirtyRect.xLeft = phdr->x;
385 dirtyRect.yTop = phdr->y;
386 dirtyRect.xRight = xRight;
387 dirtyRect.yBottom = yBottom;
388 fDirtyEmpty = false;
389 }
390 else
391 {
392 /* Adjust region coordinates. */
393 if (dirtyRect.xLeft > phdr->x)
394 {
395 dirtyRect.xLeft = phdr->x;
396 }
397
398 if (dirtyRect.yTop > phdr->y)
399 {
400 dirtyRect.yTop = phdr->y;
401 }
402
403 if (dirtyRect.xRight < xRight)
404 {
405 dirtyRect.xRight = xRight;
406 }
407
408 if (dirtyRect.yBottom < yBottom)
409 {
410 dirtyRect.yBottom = yBottom;
411 }
412 }
413 }
414
415 vbvaReleaseCmd (pPartialRecord, pVBVA, phdr, cbCmd);
416 }
417
418 if (fUpdate)
419 {
420 if (dirtyRect.xRight - dirtyRect.xLeft)
421 {
422 pVGAState->pDrv->pfnVBVAUpdateEnd (pVGAState->pDrv, uScreenId, dirtyRect.xLeft, dirtyRect.yTop,
423 dirtyRect.xRight - dirtyRect.xLeft, dirtyRect.yBottom - dirtyRect.yTop);
424 }
425 else
426 {
427 pVGAState->pDrv->pfnVBVAUpdateEnd (pVGAState->pDrv, uScreenId, 0, 0, 0, 0);
428 }
429 }
430
431 return VINF_SUCCESS;
432}
433
434static int vbvaFlush (PVGASTATE pVGAState, VBVACONTEXT *pCtx)
435{
436 unsigned uScreenId;
437
438 for (uScreenId = 0; uScreenId < pCtx->cViews; uScreenId++)
439 {
440 VBVAPARTIALRECORD *pPartialRecord = &pCtx->aViews[uScreenId].partialRecord;
441 VBVABUFFER *pVBVA = pCtx->aViews[uScreenId].pVBVA;
442
443 if (pVBVA)
444 {
445 vbvaFlushProcess (uScreenId, pVGAState, pPartialRecord, pVBVA);
446 }
447 }
448
449 /* @todo rc */
450 return VINF_SUCCESS;
451}
452
453static int vbvaResize (PVGASTATE pVGAState, VBVAVIEW *pView, const VBVAINFOSCREEN *pNewScreen)
454{
455 /* Verify pNewScreen. */
456 /* @todo */
457
458 /* Apply these changes. */
459 pView->screen = *pNewScreen;
460
461 uint8_t *pu8VRAM = pVGAState->vram_ptrR3 + pView->view.u32ViewOffset;
462
463 int rc = pVGAState->pDrv->pfnVBVAResize (pVGAState->pDrv, &pView->view, &pView->screen, pu8VRAM);
464
465 /* @todo process VINF_VGA_RESIZE_IN_PROGRESS? */
466
467 return rc;
468}
469
470static int vbvaEnable (unsigned uScreenId, PVGASTATE pVGAState, VBVACONTEXT *pCtx, VBVABUFFER *pVBVA, uint32_t u32Offset)
471{
472 /* @todo old code did a UpdateDisplayAll at this place. */
473
474 int rc;
475
476 if (pVGAState->pDrv->pfnVBVAEnable)
477 {
478 pVBVA->hostFlags.u32HostEvents = 0;
479 pVBVA->hostFlags.u32SupportedOrders = 0;
480
481 rc = pVGAState->pDrv->pfnVBVAEnable (pVGAState->pDrv, uScreenId, &pVBVA->hostFlags);
482 }
483 else
484 {
485 rc = VERR_NOT_SUPPORTED;
486 }
487
488 if (RT_SUCCESS (rc))
489 {
490 /* pVBVA->hostFlags has been set up by pfnVBVAEnable. */
491 LogFlowFunc(("u32HostEvents 0x%08X, u32SupportedOrders 0x%08X\n",
492 pVBVA->hostFlags.u32HostEvents, pVBVA->hostFlags.u32SupportedOrders));
493
494 pCtx->aViews[uScreenId].partialRecord.pu8 = NULL;
495 pCtx->aViews[uScreenId].partialRecord.cb = 0;
496
497 pCtx->aViews[uScreenId].pVBVA = pVBVA;
498 pCtx->aViews[uScreenId].u32VBVAOffset = u32Offset;
499 }
500
501 return rc;
502}
503
504static int vbvaDisable (unsigned uScreenId, PVGASTATE pVGAState, VBVACONTEXT *pCtx)
505{
506 /* Process any pending orders and empty the VBVA ring buffer. */
507 vbvaFlush (pVGAState, pCtx);
508
509 VBVAVIEW *pView = &pCtx->aViews[uScreenId];
510
511 if (pView->pVBVA)
512 {
513 pView->pVBVA->hostFlags.u32HostEvents = 0;
514 pView->pVBVA->hostFlags.u32SupportedOrders = 0;
515
516 pView->partialRecord.pu8 = NULL;
517 pView->partialRecord.cb = 0;
518
519 pView->pVBVA = NULL;
520 pView->u32VBVAOffset = HGSMIOFFSET_VOID;
521 }
522
523 pVGAState->pDrv->pfnVBVADisable (pVGAState->pDrv, uScreenId);
524 return VINF_SUCCESS;
525}
526
527bool VBVAIsEnabled(PVGASTATE pVGAState)
528{
529 PHGSMIINSTANCE pHGSMI = pVGAState->pHGSMI;
530 if (pHGSMI)
531 {
532 VBVACONTEXT *pCtx = (VBVACONTEXT *)HGSMIContext(pHGSMI);
533 if (pCtx)
534 {
535 if (pCtx->cViews)
536 {
537 VBVAVIEW * pView = &pCtx->aViews[0];
538 if (pView->pVBVA)
539 return true;
540 }
541 }
542 }
543 return false;
544}
545
546#ifdef DEBUG_sunlover
547void dumpMouseShapeInfo(const VBVAMOUSESHAPEINFO *pMouseShapeInfo)
548{
549 LogFlow(("fSet = %d, fVisible %d, fAlpha %d, @%d,%d %dx%d (%p, %d/%d)\n",
550 pMouseShapeInfo->fSet,
551 pMouseShapeInfo->fVisible,
552 pMouseShapeInfo->fAlpha,
553 pMouseShapeInfo->u32HotX,
554 pMouseShapeInfo->u32HotY,
555 pMouseShapeInfo->u32Width,
556 pMouseShapeInfo->u32Height,
557 pMouseShapeInfo->pu8Shape,
558 pMouseShapeInfo->cbShape,
559 pMouseShapeInfo->cbAllocated
560 ));
561}
562#endif
563
564static int vbvaUpdateMousePointerShape(PVGASTATE pVGAState, VBVAMOUSESHAPEINFO *pMouseShapeInfo, bool fShape, const uint8_t *pu8Shape)
565{
566 int rc;
567 LogFlowFunc(("pVGAState %p, pMouseShapeInfo %p, fShape %d, pu8Shape %p\n",
568 pVGAState, pMouseShapeInfo, fShape, pu8Shape));
569#ifdef DEBUG_sunlover
570 dumpMouseShapeInfo(pMouseShapeInfo);
571#endif
572
573 if (fShape && pu8Shape != NULL)
574 {
575 rc = pVGAState->pDrv->pfnVBVAMousePointerShape (pVGAState->pDrv,
576 pMouseShapeInfo->fVisible,
577 pMouseShapeInfo->fAlpha,
578 pMouseShapeInfo->u32HotX,
579 pMouseShapeInfo->u32HotY,
580 pMouseShapeInfo->u32Width,
581 pMouseShapeInfo->u32Height,
582 pu8Shape);
583 }
584 else
585 {
586 rc = pVGAState->pDrv->pfnVBVAMousePointerShape (pVGAState->pDrv,
587 pMouseShapeInfo->fVisible,
588 false,
589 0, 0,
590 0, 0,
591 NULL);
592 }
593
594 return rc;
595}
596
597static int vbvaMousePointerShape (PVGASTATE pVGAState, VBVACONTEXT *pCtx, const VBVAMOUSEPOINTERSHAPE *pShape, HGSMISIZE cbShape)
598{
599 bool fVisible = (pShape->fu32Flags & VBOX_MOUSE_POINTER_VISIBLE) != 0;
600 bool fAlpha = (pShape->fu32Flags & VBOX_MOUSE_POINTER_ALPHA) != 0;
601 bool fShape = (pShape->fu32Flags & VBOX_MOUSE_POINTER_SHAPE) != 0;
602
603 HGSMISIZE cbPointerData = 0;
604
605 if (fShape)
606 {
607 cbPointerData = ((((pShape->u32Width + 7) / 8) * pShape->u32Height + 3) & ~3)
608 + pShape->u32Width * 4 * pShape->u32Height;
609 }
610
611 if (cbPointerData > cbShape - RT_OFFSETOF(VBVAMOUSEPOINTERSHAPE, au8Data))
612 {
613 Log(("vbvaMousePointerShape: calculated pointer data size is too big (%d bytes, limit %d)\n",
614 cbPointerData, cbShape - RT_OFFSETOF(VBVAMOUSEPOINTERSHAPE, au8Data)));
615 return VERR_INVALID_PARAMETER;
616 }
617
618 /* Save mouse info it will be used to restore mouse pointer after restoring saved state. */
619 pCtx->mouseShapeInfo.fSet = true;
620 pCtx->mouseShapeInfo.fVisible = fVisible;
621 pCtx->mouseShapeInfo.fAlpha = fAlpha;
622 if (fShape)
623 {
624 /* Data related to shape. */
625 pCtx->mouseShapeInfo.u32HotX = pShape->u32HotX;
626 pCtx->mouseShapeInfo.u32HotY = pShape->u32HotY;
627 pCtx->mouseShapeInfo.u32Width = pShape->u32Width;
628 pCtx->mouseShapeInfo.u32Height = pShape->u32Height;
629
630 /* Reallocate memory buffer if necessary. */
631 if (cbPointerData > pCtx->mouseShapeInfo.cbAllocated)
632 {
633 RTMemFree (pCtx->mouseShapeInfo.pu8Shape);
634 pCtx->mouseShapeInfo.pu8Shape = NULL;
635 pCtx->mouseShapeInfo.cbShape = 0;
636
637 uint8_t *pu8Shape = (uint8_t *)RTMemAlloc (cbPointerData);
638 if (pu8Shape)
639 {
640 pCtx->mouseShapeInfo.pu8Shape = pu8Shape;
641 pCtx->mouseShapeInfo.cbAllocated = cbPointerData;
642 }
643 }
644
645 /* Copy shape bitmaps. */
646 if (pCtx->mouseShapeInfo.pu8Shape)
647 {
648 memcpy (pCtx->mouseShapeInfo.pu8Shape, &pShape->au8Data[0], cbPointerData);
649 pCtx->mouseShapeInfo.cbShape = cbPointerData;
650 }
651 }
652
653 if (pVGAState->pDrv->pfnVBVAMousePointerShape == NULL)
654 {
655 return VERR_NOT_SUPPORTED;
656 }
657
658 int rc = vbvaUpdateMousePointerShape(pVGAState, &pCtx->mouseShapeInfo, fShape, &pShape->au8Data[0]);
659
660 return rc;
661}
662
663static unsigned vbvaViewFromOffset (PHGSMIINSTANCE pIns, VBVACONTEXT *pCtx, const void *pvBuffer)
664{
665 /* Check which view contains the buffer. */
666 HGSMIOFFSET offBuffer = HGSMIPointerToOffsetHost (pIns, pvBuffer);
667
668 if (offBuffer != HGSMIOFFSET_VOID)
669 {
670 unsigned uScreenId;
671
672 for (uScreenId = 0; uScreenId < pCtx->cViews; uScreenId++)
673 {
674 VBVAINFOVIEW *pView = &pCtx->aViews[uScreenId].view;
675
676 if ( pView->u32ViewSize > 0
677 && pView->u32ViewOffset <= offBuffer
678 && offBuffer <= pView->u32ViewOffset + pView->u32ViewSize - 1)
679 {
680 return pView->u32ViewIndex;
681 }
682 }
683 }
684
685 return ~0U;
686}
687
688#ifdef DEBUG_sunlover
689static void dumpctx(const VBVACONTEXT *pCtx)
690{
691 Log(("VBVACONTEXT dump: cViews %d\n", pCtx->cViews));
692
693 uint32_t iView;
694 for (iView = 0; iView < pCtx->cViews; iView++)
695 {
696 const VBVAVIEW *pView = &pCtx->aViews[iView];
697
698 Log((" view %d o 0x%x s 0x%x m 0x%x\n",
699 pView->view.u32ViewIndex,
700 pView->view.u32ViewOffset,
701 pView->view.u32ViewSize,
702 pView->view.u32MaxScreenSize));
703
704 Log((" screen %d @%d,%d s 0x%x l 0x%x %dx%d bpp %d f 0x%x\n",
705 pView->screen.u32ViewIndex,
706 pView->screen.i32OriginX,
707 pView->screen.i32OriginY,
708 pView->screen.u32StartOffset,
709 pView->screen.u32LineSize,
710 pView->screen.u32Width,
711 pView->screen.u32Height,
712 pView->screen.u16BitsPerPixel,
713 pView->screen.u16Flags));
714
715 Log((" VBVA o 0x%x p %p\n",
716 pView->u32VBVAOffset,
717 pView->pVBVA));
718
719 Log((" PR cb 0x%x p %p\n",
720 pView->partialRecord.cb,
721 pView->partialRecord.pu8));
722 }
723
724 dumpMouseShapeInfo(&pCtx->mouseShapeInfo);
725}
726#endif /* DEBUG_sunlover */
727
728#define VBOXVBVASAVEDSTATE_VHWAAVAILABLE_MAGIC 0x12345678
729#define VBOXVBVASAVEDSTATE_VHWAUNAVAILABLE_MAGIC 0x9abcdef0
730
731#ifdef VBOX_WITH_VIDEOHWACCEL
732static void vbvaVHWAHHCommandReinit(VBOXVHWACMD* pHdr, VBOXVHWACMD_TYPE enmCmd, int32_t iDisplay)
733{
734 memset(pHdr, 0, VBOXVHWACMD_HEADSIZE());
735 pHdr->cRefs = 1;
736 pHdr->iDisplay = iDisplay;
737 pHdr->rc = VERR_NOT_IMPLEMENTED;
738 pHdr->enmCmd = enmCmd;
739 pHdr->Flags = VBOXVHWACMD_FLAG_HH_CMD;
740}
741
742static VBOXVHWACMD* vbvaVHWAHHCommandCreate (PVGASTATE pVGAState, VBOXVHWACMD_TYPE enmCmd, int32_t iDisplay, VBOXVHWACMD_LENGTH cbCmd)
743{
744 VBOXVHWACMD* pHdr = (VBOXVHWACMD*)RTMemAlloc(cbCmd + VBOXVHWACMD_HEADSIZE());
745 Assert(pHdr);
746 if (pHdr)
747 vbvaVHWAHHCommandReinit(pHdr, enmCmd, iDisplay);
748
749 return pHdr;
750}
751
752DECLINLINE(void) vbvaVHWAHHCommandRelease (VBOXVHWACMD* pCmd)
753{
754 uint32_t cRefs = ASMAtomicDecU32(&pCmd->cRefs);
755 if(!cRefs)
756 {
757 RTMemFree(pCmd);
758 }
759}
760
761DECLINLINE(void) vbvaVHWAHHCommandRetain (VBOXVHWACMD* pCmd)
762{
763 ASMAtomicIncU32(&pCmd->cRefs);
764}
765
766static unsigned vbvaVHWAHandleCommand (PVGASTATE pVGAState, VBVACONTEXT *pCtx, PVBOXVHWACMD pCmd)
767{
768 if (pVGAState->pDrv->pfnVHWACommandProcess)
769 pVGAState->pDrv->pfnVHWACommandProcess(pVGAState->pDrv, pCmd);
770#ifdef DEBUG_misha
771 else
772 AssertFailed();
773#endif
774 return 0;
775}
776
777static DECLCALLBACK(void) vbvaVHWAHHCommandSetEventCallback(void * pContext)
778{
779 RTSemEventSignal((RTSEMEVENT)pContext);
780}
781
782static int vbvaVHWAHHCommandPost(PVGASTATE pVGAState, VBOXVHWACMD* pCmd)
783{
784 RTSEMEVENT hComplEvent;
785 int rc = RTSemEventCreate(&hComplEvent);
786 AssertRC(rc);
787 if(RT_SUCCESS(rc))
788 {
789 /* ensure the cmd is not deleted until we process it */
790 vbvaVHWAHHCommandRetain (pCmd);
791 VBOXVHWA_HH_CALLBACK_SET(pCmd, vbvaVHWAHHCommandSetEventCallback, (void*)hComplEvent);
792 vbvaVHWAHandleCommand(pVGAState, NULL, pCmd);
793 if((ASMAtomicReadU32((volatile uint32_t *)&pCmd->Flags) & VBOXVHWACMD_FLAG_HG_ASYNCH) != 0)
794 {
795 rc = RTSemEventWaitNoResume(hComplEvent, RT_INDEFINITE_WAIT);
796 }
797 else
798 {
799 /* the command is completed */
800 }
801
802 AssertRC(rc);
803 if(RT_SUCCESS(rc))
804 {
805 RTSemEventDestroy(hComplEvent);
806 }
807 vbvaVHWAHHCommandRelease(pCmd);
808 }
809 return rc;
810}
811
812int vbvaVHWAConstruct (PVGASTATE pVGAState)
813{
814 VBOXVHWACMD *pCmd = vbvaVHWAHHCommandCreate(pVGAState, VBOXVHWACMD_TYPE_HH_CONSTRUCT, 0, sizeof(VBOXVHWACMD_HH_CONSTRUCT));
815 Assert(pCmd);
816 if(pCmd)
817 {
818 uint32_t iDisplay = 0;
819 int rc = VINF_SUCCESS;
820 VBOXVHWACMD_HH_CONSTRUCT * pBody = VBOXVHWACMD_BODY(pCmd, VBOXVHWACMD_HH_CONSTRUCT);
821
822 do
823 {
824 memset(pBody, 0, sizeof(VBOXVHWACMD_HH_CONSTRUCT));
825
826 PPDMDEVINS pDevIns = pVGAState->pDevInsR3;
827 PVM pVM = PDMDevHlpGetVM(pDevIns);
828
829 pBody->pVM = pVM;
830 pBody->pvVRAM = pVGAState->vram_ptrR3;
831 pBody->cbVRAM = pVGAState->vram_size;
832
833 rc = vbvaVHWAHHCommandPost(pVGAState, pCmd);
834 AssertRC(rc);
835 if(RT_SUCCESS(rc))
836 {
837 rc = pCmd->rc;
838 AssertMsg(RT_SUCCESS(rc) || rc == VERR_NOT_IMPLEMENTED, ("%Rrc\n", rc));
839 if(rc == VERR_NOT_IMPLEMENTED)
840 {
841 /* @todo: set some flag in pVGAState indicating VHWA is not supported */
842 /* VERR_NOT_IMPLEMENTED is not a failure, we just do not support it */
843 rc = VINF_SUCCESS;
844 }
845
846 if (!RT_SUCCESS(rc))
847 break;
848 }
849 else
850 break;
851
852 ++iDisplay;
853 if (iDisplay >= pVGAState->cMonitors)
854 break;
855 vbvaVHWAHHCommandReinit(pCmd, VBOXVHWACMD_TYPE_HH_CONSTRUCT, (int32_t)iDisplay);
856 } while (true);
857
858 vbvaVHWAHHCommandRelease(pCmd);
859
860 return rc;
861 }
862 return VERR_OUT_OF_RESOURCES;
863}
864
865int vbvaVHWAReset (PVGASTATE pVGAState)
866{
867 /* ensure we have all pending cmds processed and h->g cmds disabled */
868 VBOXVHWACMD *pCmd = vbvaVHWAHHCommandCreate(pVGAState, VBOXVHWACMD_TYPE_HH_RESET, 0, 0);
869 Assert(pCmd);
870 if(pCmd)
871 {
872 int rc = VINF_SUCCESS;
873 uint32_t iDisplay = 0;
874
875 do
876 {
877 rc =vbvaVHWAHHCommandPost(pVGAState, pCmd);
878 AssertRC(rc);
879 if(RT_SUCCESS(rc))
880 {
881 rc = pCmd->rc;
882 AssertMsg(RT_SUCCESS(rc) || rc == VERR_NOT_IMPLEMENTED, ("%Rrc\n", rc));
883 if (rc == VERR_NOT_IMPLEMENTED)
884 rc = VINF_SUCCESS;
885 }
886
887 if (!RT_SUCCESS(rc))
888 break;
889
890 ++iDisplay;
891 if (iDisplay >= pVGAState->cMonitors)
892 break;
893 vbvaVHWAHHCommandReinit(pCmd, VBOXVHWACMD_TYPE_HH_RESET, (int32_t)iDisplay);
894
895 } while (true);
896
897 vbvaVHWAHHCommandRelease(pCmd);
898
899 return rc;
900 }
901 return VERR_OUT_OF_RESOURCES;
902}
903
904typedef DECLCALLBACK(bool) FNVBOXVHWAHHCMDPRECB(PVGASTATE pVGAState, VBOXVHWACMD *pCmd, uint32_t iDisplay, void *pvContext);
905typedef FNVBOXVHWAHHCMDPRECB *PFNVBOXVHWAHHCMDPRECB;
906
907typedef DECLCALLBACK(bool) FNVBOXVHWAHHCMDPOSTCB(PVGASTATE pVGAState, VBOXVHWACMD *pCmd, uint32_t iDisplay, int rc, void *pvContext);
908typedef FNVBOXVHWAHHCMDPOSTCB *PFNVBOXVHWAHHCMDPOSTCB;
909
910int vbvaVHWAHHPost(PVGASTATE pVGAState, VBOXVHWACMD *pCmd, PFNVBOXVHWAHHCMDPRECB pfnPre, PFNVBOXVHWAHHCMDPOSTCB pfnPost, void *pvContext)
911{
912 const VBOXVHWACMD_TYPE enmType = pCmd->enmCmd;
913 int rc = VINF_SUCCESS;
914 uint32_t iDisplay = 0;
915
916 do
917 {
918 if (!pfnPre || pfnPre(pVGAState, pCmd, iDisplay, pvContext))
919 {
920 rc = vbvaVHWAHHCommandPost(pVGAState, pCmd);
921 AssertRC(rc);
922 if (pfnPost)
923 {
924 if (!pfnPost(pVGAState, pCmd, iDisplay, rc, pvContext))
925 {
926 rc = VINF_SUCCESS;
927 break;
928 }
929 rc = VINF_SUCCESS;
930 }
931 else if(RT_SUCCESS(rc))
932 {
933 rc = pCmd->rc;
934 AssertMsg(RT_SUCCESS(rc) || rc == VERR_NOT_IMPLEMENTED, ("%Rrc\n", rc));
935 if(rc == VERR_NOT_IMPLEMENTED)
936 {
937 rc = VINF_SUCCESS;
938 }
939 }
940
941 if (!RT_SUCCESS(rc))
942 break;
943 }
944
945 ++iDisplay;
946 if (iDisplay >= pVGAState->cMonitors)
947 break;
948 vbvaVHWAHHCommandReinit(pCmd, enmType, (int32_t)iDisplay);
949 } while (true);
950
951 return rc;
952}
953
954/* @todo call this also on reset? */
955int vbvaVHWAEnable (PVGASTATE pVGAState, bool bEnable)
956{
957 const VBOXVHWACMD_TYPE enmType = bEnable ? VBOXVHWACMD_TYPE_HH_ENABLE : VBOXVHWACMD_TYPE_HH_DISABLE;
958 VBOXVHWACMD *pCmd = vbvaVHWAHHCommandCreate(pVGAState,
959 enmType,
960 0, 0);
961 Assert(pCmd);
962 if(pCmd)
963 {
964 int rc = vbvaVHWAHHPost (pVGAState, pCmd, NULL, NULL, NULL);
965 vbvaVHWAHHCommandRelease(pCmd);
966 return rc;
967 }
968 return VERR_OUT_OF_RESOURCES;
969}
970
971int vboxVBVASaveStatePrep (PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
972{
973 /* ensure we have no pending commands */
974 return vbvaVHWAEnable(PDMINS_2_DATA(pDevIns, PVGASTATE), false);
975}
976
977int vboxVBVASaveStateDone (PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
978{
979 /* ensure we have no pending commands */
980 return vbvaVHWAEnable(PDMINS_2_DATA(pDevIns, PVGASTATE), true);
981}
982
983int vbvaVHWACommandCompleteAsynch(PPDMIDISPLAYVBVACALLBACKS pInterface, PVBOXVHWACMD pCmd)
984{
985 int rc;
986 if((pCmd->Flags & VBOXVHWACMD_FLAG_HH_CMD) == 0)
987 {
988 PVGASTATE pVGAState = PPDMIDISPLAYVBVACALLBACKS_2_PVGASTATE(pInterface);
989 PHGSMIINSTANCE pIns = pVGAState->pHGSMI;
990
991 Assert(pCmd->Flags & VBOXVHWACMD_FLAG_HG_ASYNCH);
992#ifdef VBOX_WITH_WDDM
993 if (pVGAState->fGuestCaps & VBVACAPS_COMPLETEGCMD_BY_IOREAD)
994 {
995 rc = HGSMICompleteGuestCommand(pIns, pCmd, !!(pCmd->Flags & VBOXVHWACMD_FLAG_GH_ASYNCH_IRQ));
996 AssertRC(rc);
997 }
998 else
999#endif
1000 {
1001 VBVAHOSTCMD *pHostCmd;
1002 int32_t iDisplay = pCmd->iDisplay;
1003
1004 if(pCmd->Flags & VBOXVHWACMD_FLAG_GH_ASYNCH_EVENT)
1005 {
1006 rc = HGSMIHostCommandAlloc (pIns,
1007 (void**)&pHostCmd,
1008 VBVAHOSTCMD_SIZE(sizeof(VBVAHOSTCMDEVENT)),
1009 HGSMI_CH_VBVA,
1010 VBVAHG_EVENT);
1011 AssertRC(rc);
1012 if(RT_SUCCESS(rc))
1013 {
1014 memset(pHostCmd, 0 , VBVAHOSTCMD_SIZE(sizeof(VBVAHOSTCMDEVENT)));
1015 pHostCmd->iDstID = pCmd->iDisplay;
1016 pHostCmd->customOpCode = 0;
1017 VBVAHOSTCMDEVENT *pBody = VBVAHOSTCMD_BODY(pHostCmd, VBVAHOSTCMDEVENT);
1018 pBody->pEvent = pCmd->GuestVBVAReserved1;
1019 }
1020 }
1021 else
1022 {
1023 HGSMIOFFSET offCmd = HGSMIPointerToOffsetHost (pIns, pCmd);
1024 Assert(offCmd != HGSMIOFFSET_VOID);
1025 if(offCmd != HGSMIOFFSET_VOID)
1026 {
1027 rc = HGSMIHostCommandAlloc (pIns,
1028 (void**)&pHostCmd,
1029 VBVAHOSTCMD_SIZE(sizeof(VBVAHOSTCMDVHWACMDCOMPLETE)),
1030 HGSMI_CH_VBVA,
1031 VBVAHG_DISPLAY_CUSTOM);
1032 AssertRC(rc);
1033 if(RT_SUCCESS(rc))
1034 {
1035 memset(pHostCmd, 0 , VBVAHOSTCMD_SIZE(sizeof(VBVAHOSTCMDVHWACMDCOMPLETE)));
1036 pHostCmd->iDstID = pCmd->iDisplay;
1037 pHostCmd->customOpCode = VBVAHG_DCUSTOM_VHWA_CMDCOMPLETE;
1038 VBVAHOSTCMDVHWACMDCOMPLETE *pBody = VBVAHOSTCMD_BODY(pHostCmd, VBVAHOSTCMDVHWACMDCOMPLETE);
1039 pBody->offCmd = offCmd;
1040 }
1041 }
1042 else
1043 {
1044 rc = VERR_INVALID_PARAMETER;
1045 }
1046 }
1047
1048 if(RT_SUCCESS(rc))
1049 {
1050 rc = HGSMIHostCommandProcessAndFreeAsynch(pIns, pHostCmd, (pCmd->Flags & VBOXVHWACMD_FLAG_GH_ASYNCH_IRQ) != 0);
1051 AssertRC(rc);
1052 if(RT_SUCCESS(rc))
1053 {
1054 return rc;
1055 }
1056 HGSMIHostCommandFree (pIns, pHostCmd);
1057 }
1058 }
1059 }
1060 else
1061 {
1062 PFNVBOXVHWA_HH_CALLBACK pfn = VBOXVHWA_HH_CALLBACK_GET(pCmd);
1063 if(pfn)
1064 {
1065 pfn(VBOXVHWA_HH_CALLBACK_GET_ARG(pCmd));
1066 }
1067 rc = VINF_SUCCESS;
1068 }
1069 return rc;
1070}
1071
1072typedef struct VBOXVBVASAVEDSTATECBDATA
1073{
1074 PSSMHANDLE pSSM;
1075 int rc;
1076 bool ab2DOn[VBOX_VIDEO_MAX_SCREENS];
1077} VBOXVBVASAVEDSTATECBDATA, *PVBOXVBVASAVEDSTATECBDATA;
1078
1079static DECLCALLBACK(bool) vboxVBVASaveStateBeginPostCb(PVGASTATE pVGAState, VBOXVHWACMD *pCmd, uint32_t iDisplay, int rc, void *pvContext)
1080{
1081 PVBOXVBVASAVEDSTATECBDATA pData = (PVBOXVBVASAVEDSTATECBDATA)pvContext;
1082 if (RT_FAILURE(pData->rc))
1083 return false;
1084 if (RT_FAILURE(rc))
1085 {
1086 pData->rc = rc;
1087 return false;
1088 }
1089
1090 Assert(iDisplay < RT_ELEMENTS(pData->ab2DOn));
1091 if (iDisplay >= RT_ELEMENTS(pData->ab2DOn))
1092 {
1093 pData->rc = VERR_INVALID_PARAMETER;
1094 return false;
1095 }
1096
1097 Assert(RT_SUCCESS(pCmd->rc) || pCmd->rc == VERR_NOT_IMPLEMENTED);
1098 if (RT_SUCCESS(pCmd->rc))
1099 {
1100 pData->ab2DOn[iDisplay] = true;
1101 }
1102 else if (pCmd->rc != VERR_NOT_IMPLEMENTED)
1103 {
1104 pData->rc = pCmd->rc;
1105 return false;
1106 }
1107
1108 return true;
1109}
1110
1111static DECLCALLBACK(bool) vboxVBVASaveStatePerformPreCb(PVGASTATE pVGAState, VBOXVHWACMD *pCmd, uint32_t iDisplay, void *pvContext)
1112{
1113 PVBOXVBVASAVEDSTATECBDATA pData = (PVBOXVBVASAVEDSTATECBDATA)pvContext;
1114 if (RT_FAILURE(pData->rc))
1115 return false;
1116
1117 Assert(iDisplay < RT_ELEMENTS(pData->ab2DOn));
1118 if (iDisplay >= RT_ELEMENTS(pData->ab2DOn))
1119 {
1120 pData->rc = VERR_INVALID_PARAMETER;
1121 return false;
1122 }
1123
1124 int rc;
1125
1126 if (pData->ab2DOn[iDisplay])
1127 {
1128 rc = SSMR3PutU32 (pData->pSSM, VBOXVBVASAVEDSTATE_VHWAAVAILABLE_MAGIC); AssertRC(rc);
1129 if (RT_FAILURE(rc))
1130 {
1131 pData->rc = rc;
1132 return false;
1133 }
1134 return true;
1135 }
1136
1137 rc = SSMR3PutU32 (pData->pSSM, VBOXVBVASAVEDSTATE_VHWAUNAVAILABLE_MAGIC); AssertRC(rc);
1138 if (RT_FAILURE(rc))
1139 {
1140 pData->rc = rc;
1141 return false;
1142 }
1143
1144 return false;
1145}
1146
1147static DECLCALLBACK(bool) vboxVBVASaveStateEndPreCb(PVGASTATE pVGAState, VBOXVHWACMD *pCmd, uint32_t iDisplay, void *pvContext)
1148{
1149 PVBOXVBVASAVEDSTATECBDATA pData = (PVBOXVBVASAVEDSTATECBDATA)pvContext;
1150 Assert(iDisplay < RT_ELEMENTS(pData->ab2DOn));
1151 if (pData->ab2DOn[iDisplay])
1152 {
1153 return true;
1154 }
1155
1156 return false;
1157}
1158
1159static DECLCALLBACK(bool) vboxVBVALoadStatePerformPostCb(PVGASTATE pVGAState, VBOXVHWACMD *pCmd, uint32_t iDisplay, int rc, void *pvContext)
1160{
1161 PVBOXVBVASAVEDSTATECBDATA pData = (PVBOXVBVASAVEDSTATECBDATA)pvContext;
1162 if (RT_FAILURE(pData->rc))
1163 return false;
1164 if (RT_FAILURE(rc))
1165 {
1166 pData->rc = rc;
1167 return false;
1168 }
1169
1170 Assert(iDisplay < RT_ELEMENTS(pData->ab2DOn));
1171 if (iDisplay >= RT_ELEMENTS(pData->ab2DOn))
1172 {
1173 pData->rc = VERR_INVALID_PARAMETER;
1174 return false;
1175 }
1176
1177 Assert(RT_SUCCESS(pCmd->rc) || pCmd->rc == VERR_NOT_IMPLEMENTED);
1178 if (pCmd->rc == VERR_NOT_IMPLEMENTED)
1179 {
1180 pData->rc = SSMR3SkipToEndOfUnit(pData->pSSM);
1181 AssertRC(pData->rc);
1182 return false;
1183 }
1184 if (RT_FAILURE(pCmd->rc))
1185 {
1186 pData->rc = pCmd->rc;
1187 return false;
1188 }
1189
1190 return true;
1191}
1192
1193static DECLCALLBACK(bool) vboxVBVALoadStatePerformPreCb(PVGASTATE pVGAState, VBOXVHWACMD *pCmd, uint32_t iDisplay, void *pvContext)
1194{
1195 PVBOXVBVASAVEDSTATECBDATA pData = (PVBOXVBVASAVEDSTATECBDATA)pvContext;
1196 if (RT_FAILURE(pData->rc))
1197 return false;
1198
1199 Assert(iDisplay < RT_ELEMENTS(pData->ab2DOn));
1200 if (iDisplay >= RT_ELEMENTS(pData->ab2DOn))
1201 {
1202 pData->rc = VERR_INVALID_PARAMETER;
1203 return false;
1204 }
1205
1206 int rc;
1207 uint32_t u32;
1208 rc = SSMR3GetU32(pData->pSSM, &u32); AssertRC(rc);
1209 if (RT_FAILURE(rc))
1210 {
1211 pData->rc = rc;
1212 return false;
1213 }
1214
1215 switch (u32)
1216 {
1217 case VBOXVBVASAVEDSTATE_VHWAAVAILABLE_MAGIC:
1218 return true;
1219 case VBOXVBVASAVEDSTATE_VHWAUNAVAILABLE_MAGIC:
1220 return false;
1221 default:
1222 pData->rc = VERR_INVALID_STATE;
1223 return false;
1224 }
1225}
1226#endif /* #ifdef VBOX_WITH_VIDEOHWACCEL */
1227
1228int vboxVBVASaveDevStateExec (PVGASTATE pVGAState, PSSMHANDLE pSSM)
1229{
1230 PHGSMIINSTANCE pIns = pVGAState->pHGSMI;
1231 int rc = HGSMIHostSaveStateExec (pIns, pSSM);
1232 if (RT_SUCCESS(rc))
1233 {
1234 /* Save VBVACONTEXT. */
1235 VBVACONTEXT *pCtx = (VBVACONTEXT *)HGSMIContext (pIns);
1236
1237 if (!pCtx)
1238 {
1239 AssertFailed();
1240
1241 /* Still write a valid value to the SSM. */
1242 rc = SSMR3PutU32 (pSSM, 0);
1243 AssertRCReturn(rc, rc);
1244 }
1245 else
1246 {
1247#ifdef DEBUG_sunlover
1248 dumpctx(pCtx);
1249#endif
1250
1251 rc = SSMR3PutU32 (pSSM, pCtx->cViews);
1252 AssertRCReturn(rc, rc);
1253
1254 uint32_t iView;
1255 for (iView = 0; iView < pCtx->cViews; iView++)
1256 {
1257 VBVAVIEW *pView = &pCtx->aViews[iView];
1258
1259 rc = SSMR3PutU32 (pSSM, pView->view.u32ViewIndex);
1260 AssertRCReturn(rc, rc);
1261 rc = SSMR3PutU32 (pSSM, pView->view.u32ViewOffset);
1262 AssertRCReturn(rc, rc);
1263 rc = SSMR3PutU32 (pSSM, pView->view.u32ViewSize);
1264 AssertRCReturn(rc, rc);
1265 rc = SSMR3PutU32 (pSSM, pView->view.u32MaxScreenSize);
1266 AssertRCReturn(rc, rc);
1267
1268 rc = SSMR3PutU32 (pSSM, pView->screen.u32ViewIndex);
1269 AssertRCReturn(rc, rc);
1270 rc = SSMR3PutS32 (pSSM, pView->screen.i32OriginX);
1271 AssertRCReturn(rc, rc);
1272 rc = SSMR3PutS32 (pSSM, pView->screen.i32OriginY);
1273 AssertRCReturn(rc, rc);
1274 rc = SSMR3PutU32 (pSSM, pView->screen.u32StartOffset);
1275 AssertRCReturn(rc, rc);
1276 rc = SSMR3PutU32 (pSSM, pView->screen.u32LineSize);
1277 AssertRCReturn(rc, rc);
1278 rc = SSMR3PutU32 (pSSM, pView->screen.u32Width);
1279 AssertRCReturn(rc, rc);
1280 rc = SSMR3PutU32 (pSSM, pView->screen.u32Height);
1281 AssertRCReturn(rc, rc);
1282 rc = SSMR3PutU16 (pSSM, pView->screen.u16BitsPerPixel);
1283 AssertRCReturn(rc, rc);
1284 rc = SSMR3PutU16 (pSSM, pView->screen.u16Flags);
1285 AssertRCReturn(rc, rc);
1286
1287 rc = SSMR3PutU32 (pSSM, pView->pVBVA? pView->u32VBVAOffset: HGSMIOFFSET_VOID);
1288 AssertRCReturn(rc, rc);
1289
1290 rc = SSMR3PutU32 (pSSM, pView->partialRecord.cb);
1291 AssertRCReturn(rc, rc);
1292
1293 if (pView->partialRecord.cb > 0)
1294 {
1295 rc = SSMR3PutMem (pSSM, pView->partialRecord.pu8, pView->partialRecord.cb);
1296 AssertRCReturn(rc, rc);
1297 }
1298 }
1299
1300 /* Save mouse pointer shape information. */
1301 rc = SSMR3PutBool (pSSM, pCtx->mouseShapeInfo.fSet);
1302 AssertRCReturn(rc, rc);
1303 rc = SSMR3PutBool (pSSM, pCtx->mouseShapeInfo.fVisible);
1304 AssertRCReturn(rc, rc);
1305 rc = SSMR3PutBool (pSSM, pCtx->mouseShapeInfo.fAlpha);
1306 AssertRCReturn(rc, rc);
1307 rc = SSMR3PutU32 (pSSM, pCtx->mouseShapeInfo.u32HotX);
1308 AssertRCReturn(rc, rc);
1309 rc = SSMR3PutU32 (pSSM, pCtx->mouseShapeInfo.u32HotY);
1310 AssertRCReturn(rc, rc);
1311 rc = SSMR3PutU32 (pSSM, pCtx->mouseShapeInfo.u32Width);
1312 AssertRCReturn(rc, rc);
1313 rc = SSMR3PutU32 (pSSM, pCtx->mouseShapeInfo.u32Height);
1314 AssertRCReturn(rc, rc);
1315 rc = SSMR3PutU32 (pSSM, pCtx->mouseShapeInfo.cbShape);
1316 AssertRCReturn(rc, rc);
1317 if (pCtx->mouseShapeInfo.cbShape)
1318 {
1319 rc = SSMR3PutMem (pSSM, pCtx->mouseShapeInfo.pu8Shape, pCtx->mouseShapeInfo.cbShape);
1320 AssertRCReturn(rc, rc);
1321 }
1322
1323#ifdef VBOX_WITH_WDDM
1324 /* Size of some additional data. For future extensions. */
1325 rc = SSMR3PutU32 (pSSM, 4);
1326 AssertRCReturn(rc, rc);
1327 rc = SSMR3PutU32 (pSSM, pVGAState->fGuestCaps);
1328 AssertRCReturn(rc, rc);
1329#else
1330 /* Size of some additional data. For future extensions. */
1331 rc = SSMR3PutU32 (pSSM, 0);
1332 AssertRCReturn(rc, rc);
1333#endif
1334 }
1335 }
1336
1337 return rc;
1338}
1339
1340int vboxVBVASaveStateExec (PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
1341{
1342 PVGASTATE pVGAState = PDMINS_2_DATA(pDevIns, PVGASTATE);
1343 int rc;
1344#ifdef VBOX_WITH_VIDEOHWACCEL
1345 VBOXVBVASAVEDSTATECBDATA VhwaData = {0};
1346 VhwaData.pSSM = pSSM;
1347 uint32_t cbCmd = sizeof (VBOXVHWACMD_HH_SAVESTATE_SAVEPERFORM); /* maximum cmd size */
1348 VBOXVHWACMD *pCmd = vbvaVHWAHHCommandCreate(pVGAState, VBOXVHWACMD_TYPE_HH_SAVESTATE_SAVEBEGIN, 0, cbCmd);
1349 Assert(pCmd);
1350 if(pCmd)
1351 {
1352 vbvaVHWAHHPost (pVGAState, pCmd, NULL, vboxVBVASaveStateBeginPostCb, &VhwaData);
1353 rc = VhwaData.rc;
1354 AssertRC(rc);
1355 if (RT_SUCCESS(rc))
1356 {
1357#endif
1358 rc = vboxVBVASaveDevStateExec (pVGAState, pSSM);
1359 AssertRC(rc);
1360#ifdef VBOX_WITH_VIDEOHWACCEL
1361 if (RT_SUCCESS(rc))
1362 {
1363 vbvaVHWAHHCommandReinit(pCmd, VBOXVHWACMD_TYPE_HH_SAVESTATE_SAVEPERFORM, 0);
1364 VBOXVHWACMD_HH_SAVESTATE_SAVEPERFORM *pSave = VBOXVHWACMD_BODY(pCmd, VBOXVHWACMD_HH_SAVESTATE_SAVEPERFORM);
1365 pSave->pSSM = pSSM;
1366 vbvaVHWAHHPost (pVGAState, pCmd, vboxVBVASaveStatePerformPreCb, NULL, &VhwaData);
1367 rc = VhwaData.rc;
1368 AssertRC(rc);
1369 if (RT_SUCCESS(rc))
1370 {
1371 vbvaVHWAHHCommandReinit(pCmd, VBOXVHWACMD_TYPE_HH_SAVESTATE_SAVEEND, 0);
1372 vbvaVHWAHHPost (pVGAState, pCmd, vboxVBVASaveStateEndPreCb, NULL, &VhwaData);
1373 rc = VhwaData.rc;
1374 AssertRC(rc);
1375 }
1376 }
1377 }
1378
1379 vbvaVHWAHHCommandRelease(pCmd);
1380 }
1381 else
1382 rc = VERR_OUT_OF_RESOURCES;
1383#else
1384 if (RT_SUCCESS(rc))
1385 {
1386 for (uint32_t i = 0; i < pVGAState->cMonitors; ++i)
1387 {
1388 rc = SSMR3PutU32 (pSSM, VBOXVBVASAVEDSTATE_VHWAUNAVAILABLE_MAGIC);
1389 AssertRCReturn(rc, rc);
1390 }
1391 }
1392#endif
1393 return rc;
1394}
1395
1396int vboxVBVALoadStateExec (PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t u32Version)
1397{
1398 if (u32Version < VGA_SAVEDSTATE_VERSION_HGSMI)
1399 {
1400 /* Nothing was saved. */
1401 return VINF_SUCCESS;
1402 }
1403
1404 PVGASTATE pVGAState = PDMINS_2_DATA(pDevIns, PVGASTATE);
1405 PHGSMIINSTANCE pIns = pVGAState->pHGSMI;
1406 int rc = HGSMIHostLoadStateExec (pIns, pSSM, u32Version);
1407 if (RT_SUCCESS(rc))
1408 {
1409 /* Load VBVACONTEXT. */
1410 VBVACONTEXT *pCtx = (VBVACONTEXT *)HGSMIContext (pIns);
1411
1412 if (!pCtx)
1413 {
1414 /* This should not happen. */
1415 AssertFailed();
1416 rc = VERR_INVALID_PARAMETER;
1417 }
1418 else
1419 {
1420 uint32_t cViews = 0;
1421 rc = SSMR3GetU32 (pSSM, &cViews);
1422 AssertRCReturn(rc, rc);
1423
1424 uint32_t iView;
1425 for (iView = 0; iView < cViews; iView++)
1426 {
1427 VBVAVIEW *pView = &pCtx->aViews[iView];
1428
1429 rc = SSMR3GetU32 (pSSM, &pView->view.u32ViewIndex);
1430 AssertRCReturn(rc, rc);
1431 rc = SSMR3GetU32 (pSSM, &pView->view.u32ViewOffset);
1432 AssertRCReturn(rc, rc);
1433 rc = SSMR3GetU32 (pSSM, &pView->view.u32ViewSize);
1434 AssertRCReturn(rc, rc);
1435 rc = SSMR3GetU32 (pSSM, &pView->view.u32MaxScreenSize);
1436 AssertRCReturn(rc, rc);
1437
1438 rc = SSMR3GetU32 (pSSM, &pView->screen.u32ViewIndex);
1439 AssertRCReturn(rc, rc);
1440 rc = SSMR3GetS32 (pSSM, &pView->screen.i32OriginX);
1441 AssertRCReturn(rc, rc);
1442 rc = SSMR3GetS32 (pSSM, &pView->screen.i32OriginY);
1443 AssertRCReturn(rc, rc);
1444 rc = SSMR3GetU32 (pSSM, &pView->screen.u32StartOffset);
1445 AssertRCReturn(rc, rc);
1446 rc = SSMR3GetU32 (pSSM, &pView->screen.u32LineSize);
1447 AssertRCReturn(rc, rc);
1448 rc = SSMR3GetU32 (pSSM, &pView->screen.u32Width);
1449 AssertRCReturn(rc, rc);
1450 rc = SSMR3GetU32 (pSSM, &pView->screen.u32Height);
1451 AssertRCReturn(rc, rc);
1452 rc = SSMR3GetU16 (pSSM, &pView->screen.u16BitsPerPixel);
1453 AssertRCReturn(rc, rc);
1454 rc = SSMR3GetU16 (pSSM, &pView->screen.u16Flags);
1455 AssertRCReturn(rc, rc);
1456
1457 rc = SSMR3GetU32 (pSSM, &pView->u32VBVAOffset);
1458 AssertRCReturn(rc, rc);
1459
1460 rc = SSMR3GetU32 (pSSM, &pView->partialRecord.cb);
1461 AssertRCReturn(rc, rc);
1462
1463 if (pView->partialRecord.cb == 0)
1464 {
1465 pView->partialRecord.pu8 = NULL;
1466 }
1467 else
1468 {
1469 Assert(pView->partialRecord.pu8 == NULL); /* Should be it. */
1470
1471 uint8_t *pu8 = (uint8_t *)RTMemAlloc (pView->partialRecord.cb);
1472
1473 if (!pu8)
1474 {
1475 return VERR_NO_MEMORY;
1476 }
1477
1478 pView->partialRecord.pu8 = pu8;
1479
1480 rc = SSMR3GetMem (pSSM, pView->partialRecord.pu8, pView->partialRecord.cb);
1481 AssertRCReturn(rc, rc);
1482 }
1483
1484 if ( pView->u32VBVAOffset == HGSMIOFFSET_VOID
1485 || pView->screen.u32LineSize == 0) /* Earlier broken saved states. */
1486 {
1487 pView->pVBVA = NULL;
1488 }
1489 else
1490 {
1491 pView->pVBVA = (VBVABUFFER *)HGSMIOffsetToPointerHost (pIns, pView->u32VBVAOffset);
1492 }
1493 }
1494
1495 if (u32Version > VGA_SAVEDSTATE_VERSION_WITH_CONFIG)
1496 {
1497 /* Read mouse pointer shape information. */
1498 rc = SSMR3GetBool (pSSM, &pCtx->mouseShapeInfo.fSet);
1499 AssertRCReturn(rc, rc);
1500 rc = SSMR3GetBool (pSSM, &pCtx->mouseShapeInfo.fVisible);
1501 AssertRCReturn(rc, rc);
1502 rc = SSMR3GetBool (pSSM, &pCtx->mouseShapeInfo.fAlpha);
1503 AssertRCReturn(rc, rc);
1504 rc = SSMR3GetU32 (pSSM, &pCtx->mouseShapeInfo.u32HotX);
1505 AssertRCReturn(rc, rc);
1506 rc = SSMR3GetU32 (pSSM, &pCtx->mouseShapeInfo.u32HotY);
1507 AssertRCReturn(rc, rc);
1508 rc = SSMR3GetU32 (pSSM, &pCtx->mouseShapeInfo.u32Width);
1509 AssertRCReturn(rc, rc);
1510 rc = SSMR3GetU32 (pSSM, &pCtx->mouseShapeInfo.u32Height);
1511 AssertRCReturn(rc, rc);
1512 rc = SSMR3GetU32 (pSSM, &pCtx->mouseShapeInfo.cbShape);
1513 AssertRCReturn(rc, rc);
1514 if (pCtx->mouseShapeInfo.cbShape)
1515 {
1516 pCtx->mouseShapeInfo.pu8Shape = (uint8_t *)RTMemAlloc(pCtx->mouseShapeInfo.cbShape);
1517 if (pCtx->mouseShapeInfo.pu8Shape == NULL)
1518 {
1519 return VERR_NO_MEMORY;
1520 }
1521 pCtx->mouseShapeInfo.cbAllocated = pCtx->mouseShapeInfo.cbShape;
1522 rc = SSMR3GetMem (pSSM, pCtx->mouseShapeInfo.pu8Shape, pCtx->mouseShapeInfo.cbShape);
1523 AssertRCReturn(rc, rc);
1524 }
1525 else
1526 {
1527 pCtx->mouseShapeInfo.pu8Shape = NULL;
1528 }
1529
1530 /* Size of some additional data. For future extensions. */
1531 uint32_t cbExtra = 0;
1532 rc = SSMR3GetU32 (pSSM, &cbExtra);
1533 AssertRCReturn(rc, rc);
1534#ifdef VBOX_WITH_WDDM
1535 if (cbExtra >= 4)
1536 {
1537 rc = SSMR3GetU32 (pSSM, &pVGAState->fGuestCaps);
1538 AssertRCReturn(rc, rc);
1539 cbExtra -= 4;
1540 }
1541#endif
1542 if (cbExtra > 0)
1543 {
1544 rc = SSMR3Skip(pSSM, cbExtra);
1545 AssertRCReturn(rc, rc);
1546 }
1547 }
1548
1549 pCtx->cViews = iView;
1550 LogFlowFunc(("%d views loaded\n", pCtx->cViews));
1551
1552 if (u32Version > VGA_SAVEDSTATE_VERSION_WDDM)
1553 {
1554#ifdef VBOX_WITH_VIDEOHWACCEL
1555 uint32_t cbCmd = sizeof (VBOXVHWACMD_HH_SAVESTATE_LOADPERFORM); /* maximum cmd size */
1556 VBOXVHWACMD *pCmd = vbvaVHWAHHCommandCreate(pVGAState, VBOXVHWACMD_TYPE_HH_SAVESTATE_LOADPERFORM, 0, cbCmd);
1557 Assert(pCmd);
1558 if(pCmd)
1559 {
1560 VBOXVBVASAVEDSTATECBDATA VhwaData = {0};
1561 VhwaData.pSSM = pSSM;
1562 VBOXVHWACMD_HH_SAVESTATE_LOADPERFORM *pLoad = VBOXVHWACMD_BODY(pCmd, VBOXVHWACMD_HH_SAVESTATE_LOADPERFORM);
1563 pLoad->pSSM = pSSM;
1564 vbvaVHWAHHPost (pVGAState, pCmd, vboxVBVALoadStatePerformPreCb, vboxVBVALoadStatePerformPostCb, &VhwaData);
1565 rc = VhwaData.rc;
1566 AssertRC(rc);
1567 vbvaVHWAHHCommandRelease(pCmd);
1568 }
1569 else
1570 {
1571 rc = VERR_OUT_OF_RESOURCES;
1572 }
1573#else
1574 rc = SSMR3SkipToEndOfUnit(pSSM);
1575 AssertRCReturn(rc, rc);
1576#endif
1577 }
1578
1579#ifdef DEBUG_sunlover
1580 dumpctx(pCtx);
1581#endif
1582 }
1583 }
1584
1585 return rc;
1586}
1587
1588int vboxVBVALoadStateDone (PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
1589{
1590 PVGASTATE pVGAState = PDMINS_2_DATA(pDevIns, PVGASTATE);
1591 VBVACONTEXT *pCtx = (VBVACONTEXT *)HGSMIContext (pVGAState->pHGSMI);
1592
1593 if (pCtx)
1594 {
1595 uint32_t iView;
1596 for (iView = 0; iView < pCtx->cViews; iView++)
1597 {
1598 VBVAVIEW *pView = &pCtx->aViews[iView];
1599
1600 if (pView->pVBVA)
1601 {
1602 vbvaEnable (iView, pVGAState, pCtx, pView->pVBVA, pView->u32VBVAOffset);
1603 vbvaResize (pVGAState, pView, &pView->screen);
1604 }
1605 }
1606
1607 if (pCtx->mouseShapeInfo.fSet)
1608 {
1609 vbvaUpdateMousePointerShape(pVGAState, &pCtx->mouseShapeInfo, true, pCtx->mouseShapeInfo.pu8Shape);
1610 }
1611 }
1612
1613 return VINF_SUCCESS;
1614}
1615
1616/*
1617 *
1618 * New VBVA uses a new interface id: #define VBE_DISPI_ID_VBOX_VIDEO 0xBE01
1619 *
1620 * VBVA uses two 32 bits IO ports to write VRAM offsets of shared memory blocks for commands.
1621 * Read Write
1622 * Host port 0x3b0 to process completed
1623 * Guest port 0x3d0 control value? to process
1624 *
1625 */
1626
1627static DECLCALLBACK(void) vbvaNotifyGuest (void *pvCallback)
1628{
1629#if defined(VBOX_WITH_HGSMI) && (defined(VBOX_WITH_VIDEOHWACCEL) || defined(VBOX_WITH_VDMA) || defined(VBOX_WITH_WDDM))
1630 PVGASTATE pVGAState = (PVGASTATE)pvCallback;
1631 PPDMDEVINS pDevIns = pVGAState->pDevInsR3;
1632 PDMCritSectEnter(&pVGAState->lock, VERR_SEM_BUSY);
1633 HGSMISetHostGuestFlags(pVGAState->pHGSMI, HGSMIHOSTFLAGS_IRQ);
1634 PDMDevHlpPCISetIrq(pDevIns, 0, PDM_IRQ_LEVEL_HIGH);
1635 PDMCritSectLeave(&pVGAState->lock);
1636#else
1637 NOREF(pvCallback);
1638 /* Do nothing. Later the VMMDev/VGA IRQ can be used for the notification. */
1639#endif
1640}
1641
1642/* The guest submitted a buffer. @todo Verify all guest data. */
1643static DECLCALLBACK(int) vbvaChannelHandler (void *pvHandler, uint16_t u16ChannelInfo, void *pvBuffer, HGSMISIZE cbBuffer)
1644{
1645 int rc = VINF_SUCCESS;
1646
1647 LogFlowFunc(("pvHandler %p, u16ChannelInfo %d, pvBuffer %p, cbBuffer %u\n",
1648 pvHandler, u16ChannelInfo, pvBuffer, cbBuffer));
1649
1650 PVGASTATE pVGAState = (PVGASTATE)pvHandler;
1651 PHGSMIINSTANCE pIns = pVGAState->pHGSMI;
1652 VBVACONTEXT *pCtx = (VBVACONTEXT *)HGSMIContext (pIns);
1653
1654 switch (u16ChannelInfo)
1655 {
1656#ifdef VBOX_WITH_VDMA
1657 case VBVA_VDMA_CMD:
1658 {
1659 PVBOXVDMACBUF_DR pCmd = (PVBOXVDMACBUF_DR)VBoxSHGSMIBufferData ((PVBOXSHGSMIHEADER)pvBuffer);
1660 vboxVDMACommand(pVGAState->pVdma, pCmd);
1661 rc = VINF_SUCCESS;
1662 break;
1663 }
1664 case VBVA_VDMA_CTL:
1665 {
1666 PVBOXVDMA_CTL pCmd = (PVBOXVDMA_CTL)VBoxSHGSMIBufferData ((PVBOXSHGSMIHEADER)pvBuffer);
1667 vboxVDMAControl(pVGAState->pVdma, pCmd);
1668 rc = VINF_SUCCESS;
1669 break;
1670 }
1671#endif
1672 case VBVA_QUERY_CONF32:
1673 {
1674 if (cbBuffer < sizeof (VBVACONF32))
1675 {
1676 rc = VERR_INVALID_PARAMETER;
1677 break;
1678 }
1679
1680 VBVACONF32 *pConf32 = (VBVACONF32 *)pvBuffer;
1681 LogFlowFunc(("VBVA_QUERY_CONF32: u32Index %d, u32Value 0x%x\n",
1682 pConf32->u32Index, pConf32->u32Value));
1683
1684 if (pConf32->u32Index == VBOX_VBVA_CONF32_MONITOR_COUNT)
1685 {
1686 pConf32->u32Value = pCtx->cViews;
1687 }
1688 else if (pConf32->u32Index == VBOX_VBVA_CONF32_HOST_HEAP_SIZE)
1689 {
1690 /* @todo a value calculated from the vram size */
1691 pConf32->u32Value = 64*_1K;
1692 }
1693 else
1694 {
1695 Log(("Unsupported VBVA_QUERY_CONF32 index %d!!!\n",
1696 pConf32->u32Index));
1697 rc = VERR_INVALID_PARAMETER;
1698 }
1699 } break;
1700
1701 case VBVA_SET_CONF32:
1702 {
1703 if (cbBuffer < sizeof (VBVACONF32))
1704 {
1705 rc = VERR_INVALID_PARAMETER;
1706 break;
1707 }
1708
1709 VBVACONF32 *pConf32 = (VBVACONF32 *)pvBuffer;
1710 LogFlowFunc(("VBVA_SET_CONF32: u32Index %d, u32Value 0x%x\n",
1711 pConf32->u32Index, pConf32->u32Value));
1712
1713 if (pConf32->u32Index == VBOX_VBVA_CONF32_MONITOR_COUNT)
1714 {
1715 /* do nothing. this is a const. */
1716 }
1717 else if (pConf32->u32Index == VBOX_VBVA_CONF32_HOST_HEAP_SIZE)
1718 {
1719 /* do nothing. this is a const. */
1720 }
1721 else
1722 {
1723 Log(("Unsupported VBVA_SET_CONF32 index %d!!!\n",
1724 pConf32->u32Index));
1725 rc = VERR_INVALID_PARAMETER;
1726 }
1727 } break;
1728
1729 case VBVA_INFO_VIEW:
1730 {
1731 if (cbBuffer < sizeof (VBVAINFOVIEW))
1732 {
1733 rc = VERR_INVALID_PARAMETER;
1734 break;
1735 }
1736
1737 /* Guest submits an array of VBVAINFOVIEW structures. */
1738 VBVAINFOVIEW *pView = (VBVAINFOVIEW *)pvBuffer;
1739
1740 for (;
1741 cbBuffer >= sizeof (VBVAINFOVIEW);
1742 pView++, cbBuffer -= sizeof (VBVAINFOVIEW))
1743 {
1744 LogFlowFunc(("VBVA_INFO_VIEW: index %d, offset 0x%x, size 0x%x, max screen size 0x%x\n",
1745 pView->u32ViewIndex, pView->u32ViewOffset, pView->u32ViewSize, pView->u32MaxScreenSize));
1746
1747 /* @todo verify view data. */
1748 if (pView->u32ViewIndex >= pCtx->cViews)
1749 {
1750 Log(("View index too large %d!!!\n",
1751 pView->u32ViewIndex));
1752 rc = VERR_INVALID_PARAMETER;
1753 break;
1754 }
1755
1756 pCtx->aViews[pView->u32ViewIndex].view = *pView;
1757 }
1758 } break;
1759
1760 case VBVA_INFO_HEAP:
1761 {
1762 if (cbBuffer < sizeof (VBVAINFOHEAP))
1763 {
1764 rc = VERR_INVALID_PARAMETER;
1765 break;
1766 }
1767
1768 VBVAINFOHEAP *pHeap = (VBVAINFOHEAP *)pvBuffer;
1769 LogFlowFunc(("VBVA_INFO_HEAP: offset 0x%x, size 0x%x\n",
1770 pHeap->u32HeapOffset, pHeap->u32HeapSize));
1771
1772 rc = HGSMISetupHostHeap (pIns, pHeap->u32HeapOffset, pHeap->u32HeapSize);
1773 } break;
1774
1775 case VBVA_FLUSH:
1776 {
1777 if (cbBuffer < sizeof (VBVAFLUSH))
1778 {
1779 rc = VERR_INVALID_PARAMETER;
1780 break;
1781 }
1782
1783 VBVAFLUSH *pFlush = (VBVAFLUSH *)pvBuffer;
1784 LogFlowFunc(("VBVA_FLUSH: u32Reserved 0x%x\n",
1785 pFlush->u32Reserved));
1786
1787 rc = vbvaFlush (pVGAState, pCtx);
1788 } break;
1789
1790 case VBVA_INFO_SCREEN:
1791 {
1792 if (cbBuffer < sizeof (VBVAINFOSCREEN))
1793 {
1794 rc = VERR_INVALID_PARAMETER;
1795 break;
1796 }
1797
1798 VBVAINFOSCREEN *pScreen = (VBVAINFOSCREEN *)pvBuffer;
1799 LogFlowFunc(("VBVA_INFO_SCREEN: [%d] @%d,%d %dx%d, line 0x%x, BPP %d, flags 0x%x\n",
1800 pScreen->u32ViewIndex, pScreen->i32OriginX, pScreen->i32OriginY,
1801 pScreen->u32Width, pScreen->u32Height,
1802 pScreen->u32LineSize, pScreen->u16BitsPerPixel, pScreen->u16Flags));
1803
1804 if (pScreen->u32ViewIndex < RT_ELEMENTS (pCtx->aViews))
1805 {
1806 vbvaResize (pVGAState, &pCtx->aViews[pScreen->u32ViewIndex], pScreen);
1807 }
1808 else
1809 {
1810 Log(("View index too large %d!!!\n",
1811 pScreen->u32ViewIndex));
1812 rc = VERR_INVALID_PARAMETER;
1813 }
1814 } break;
1815
1816 case VBVA_ENABLE:
1817 {
1818 if (cbBuffer < sizeof (VBVAENABLE))
1819 {
1820 rc = VERR_INVALID_PARAMETER;
1821 break;
1822 }
1823
1824 VBVAENABLE *pEnable = (VBVAENABLE *)pvBuffer;
1825 unsigned uScreenId;
1826#ifdef VBOXWDDM_WITH_VBVA
1827 if (pEnable->u32Flags & VBVA_F_EXTENDED)
1828 {
1829 if (cbBuffer < sizeof (VBVAENABLE_EX))
1830 {
1831 rc = VERR_INVALID_PARAMETER;
1832 break;
1833 }
1834
1835 VBVAENABLE_EX *pEnableEx = (VBVAENABLE_EX *)pvBuffer;
1836 uScreenId = pEnableEx->u32ScreenId;
1837 }
1838 else
1839#endif
1840 {
1841 uScreenId = vbvaViewFromOffset (pIns, pCtx, pvBuffer);
1842 }
1843
1844 if (uScreenId == ~0U)
1845 {
1846 rc = VERR_INVALID_PARAMETER;
1847 break;
1848 }
1849
1850 LogFlowFunc(("VBVA_ENABLE[%d]: u32Flags 0x%x u32Offset 0x%x\n",
1851 uScreenId, pEnable->u32Flags, pEnable->u32Offset));
1852
1853 if ((pEnable->u32Flags & (VBVA_F_ENABLE | VBVA_F_DISABLE)) == VBVA_F_ENABLE)
1854 {
1855 /* Guest reported offset relative to view. */
1856 uint32_t u32Offset = pEnable->u32Offset;
1857#ifdef VBOXWDDM_WITH_VBVA
1858 if (!(pEnable->u32Flags & VBVA_F_ABSOFFSET))
1859#endif
1860 {
1861 u32Offset += pCtx->aViews[uScreenId].view.u32ViewOffset;
1862 }
1863
1864 VBVABUFFER *pVBVA = (VBVABUFFER *)HGSMIOffsetToPointerHost (pIns, u32Offset);
1865
1866 if (pVBVA)
1867 {
1868 /* Process any pending orders and empty the VBVA ring buffer. */
1869 vbvaFlush (pVGAState, pCtx);
1870
1871 rc = vbvaEnable (uScreenId, pVGAState, pCtx, pVBVA, u32Offset);
1872 }
1873 else
1874 {
1875 Log(("Invalid VBVABUFFER offset 0x%x!!!\n",
1876 pEnable->u32Offset));
1877 rc = VERR_INVALID_PARAMETER;
1878 }
1879 }
1880 else if ((pEnable->u32Flags & (VBVA_F_ENABLE | VBVA_F_DISABLE)) == VBVA_F_DISABLE)
1881 {
1882 rc = vbvaDisable (uScreenId, pVGAState, pCtx);
1883 }
1884 else
1885 {
1886 Log(("Invalid VBVA_ENABLE flags 0x%x!!!\n",
1887 pEnable->u32Flags));
1888 rc = VERR_INVALID_PARAMETER;
1889 }
1890
1891 pEnable->i32Result = rc;
1892 } break;
1893
1894 case VBVA_MOUSE_POINTER_SHAPE:
1895 {
1896 if (cbBuffer < sizeof (VBVAMOUSEPOINTERSHAPE))
1897 {
1898 rc = VERR_INVALID_PARAMETER;
1899 break;
1900 }
1901
1902 VBVAMOUSEPOINTERSHAPE *pShape = (VBVAMOUSEPOINTERSHAPE *)pvBuffer;
1903
1904 LogFlowFunc(("VBVA_MOUSE_POINTER_SHAPE: i32Result 0x%x, fu32Flags 0x%x, hot spot %d,%d, size %dx%d\n",
1905 pShape->i32Result,
1906 pShape->fu32Flags,
1907 pShape->u32HotX,
1908 pShape->u32HotY,
1909 pShape->u32Width,
1910 pShape->u32Height));
1911
1912 rc = vbvaMousePointerShape (pVGAState, pCtx, pShape, cbBuffer);
1913
1914 pShape->i32Result = rc;
1915 } break;
1916
1917
1918#ifdef VBOX_WITH_VIDEOHWACCEL
1919 case VBVA_VHWA_CMD:
1920 {
1921 rc = vbvaVHWAHandleCommand (pVGAState, pCtx, (PVBOXVHWACMD)pvBuffer);
1922 } break;
1923#endif
1924
1925#ifdef VBOX_WITH_WDDM
1926 case VBVA_INFO_CAPS:
1927 {
1928 if (cbBuffer < sizeof (VBVACAPS))
1929 {
1930 rc = VERR_INVALID_PARAMETER;
1931 break;
1932 }
1933
1934 VBVACAPS *pCaps = (VBVACAPS*)pvBuffer;
1935 pVGAState->fGuestCaps = pCaps->fCaps;
1936 pCaps->rc = VINF_SUCCESS;
1937 } break;
1938#endif
1939 default:
1940 Log(("Unsupported VBVA guest command %d!!!\n",
1941 u16ChannelInfo));
1942 break;
1943 }
1944
1945 return rc;
1946}
1947
1948void VBVAReset (PVGASTATE pVGAState)
1949{
1950 if (!pVGAState || !pVGAState->pHGSMI)
1951 {
1952 return;
1953 }
1954
1955 VBVACONTEXT *pCtx = (VBVACONTEXT *)HGSMIContext (pVGAState->pHGSMI);
1956
1957#ifdef VBOX_WITH_VIDEOHWACCEL
1958 vbvaVHWAReset (pVGAState);
1959#endif
1960
1961 uint32_t HgFlags = HGSMIReset (pVGAState->pHGSMI);
1962 if(HgFlags & HGSMIHOSTFLAGS_IRQ)
1963 {
1964 /* this means the IRQ is LEVEL_HIGH, need to reset it */
1965 PDMDevHlpPCISetIrq(pVGAState->pDevInsR3, 0, PDM_IRQ_LEVEL_LOW);
1966 }
1967
1968 if (pCtx)
1969 {
1970 vbvaFlush (pVGAState, pCtx);
1971
1972 unsigned uScreenId;
1973
1974 for (uScreenId = 0; uScreenId < pCtx->cViews; uScreenId++)
1975 {
1976 vbvaDisable (uScreenId, pVGAState, pCtx);
1977 }
1978
1979 pCtx->mouseShapeInfo.fSet = false;
1980 RTMemFree(pCtx->mouseShapeInfo.pu8Shape);
1981 pCtx->mouseShapeInfo.pu8Shape = NULL;
1982 pCtx->mouseShapeInfo.cbAllocated = 0;
1983 pCtx->mouseShapeInfo.cbShape = 0;
1984 }
1985
1986}
1987
1988int VBVAUpdateDisplay (PVGASTATE pVGAState)
1989{
1990 int rc = VERR_NOT_SUPPORTED; /* Assuming that the VGA device will have to do updates. */
1991
1992 VBVACONTEXT *pCtx = (VBVACONTEXT *)HGSMIContext (pVGAState->pHGSMI);
1993
1994 if (pCtx)
1995 {
1996 rc = vbvaFlush (pVGAState, pCtx);
1997
1998 if (RT_SUCCESS (rc))
1999 {
2000 if (!pCtx->aViews[0].pVBVA)
2001 {
2002 /* VBVA is not enabled for the first view, so VGA device must do updates. */
2003 rc = VERR_NOT_SUPPORTED;
2004 }
2005 }
2006 }
2007
2008 return rc;
2009}
2010
2011static HGSMICHANNELHANDLER sOldChannelHandler;
2012
2013int VBVAInit (PVGASTATE pVGAState)
2014{
2015 PPDMDEVINS pDevIns = pVGAState->pDevInsR3;
2016
2017 PVM pVM = PDMDevHlpGetVM(pDevIns);
2018
2019 int rc = HGSMICreate (&pVGAState->pHGSMI,
2020 pVM,
2021 "VBVA",
2022 0,
2023 pVGAState->vram_ptrR3,
2024 pVGAState->vram_size,
2025 vbvaNotifyGuest,
2026 pVGAState,
2027 sizeof (VBVACONTEXT));
2028
2029 if (RT_SUCCESS (rc))
2030 {
2031 rc = HGSMIHostChannelRegister (pVGAState->pHGSMI,
2032 HGSMI_CH_VBVA,
2033 vbvaChannelHandler,
2034 pVGAState,
2035 &sOldChannelHandler);
2036 if (RT_SUCCESS (rc))
2037 {
2038 VBVACONTEXT *pCtx = (VBVACONTEXT *)HGSMIContext (pVGAState->pHGSMI);
2039 pCtx->cViews = pVGAState->cMonitors;
2040 }
2041 }
2042
2043 return rc;
2044
2045}
2046
2047void VBVADestroy (PVGASTATE pVGAState)
2048{
2049 VBVACONTEXT *pCtx = (VBVACONTEXT *)HGSMIContext (pVGAState->pHGSMI);
2050
2051 if (pCtx)
2052 {
2053 pCtx->mouseShapeInfo.fSet = false;
2054 RTMemFree(pCtx->mouseShapeInfo.pu8Shape);
2055 pCtx->mouseShapeInfo.pu8Shape = NULL;
2056 pCtx->mouseShapeInfo.cbAllocated = 0;
2057 pCtx->mouseShapeInfo.cbShape = 0;
2058 }
2059
2060 HGSMIDestroy (pVGAState->pHGSMI);
2061 pVGAState->pHGSMI = NULL;
2062}
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