VirtualBox

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

Last change on this file since 25127 was 25068, checked in by vboxsync, 15 years ago

DevVGA_VBVA: logging.

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