VirtualBox

source: vbox/trunk/src/VBox/Additions/x11/vboxvideo/vboxutils.c@ 22633

Last change on this file since 22633 was 22445, checked in by vboxsync, 15 years ago

Additions/x11/vboxvideo: query the right resolution from the host when a video mode hint is sent

  • Property svn:eol-style set to native
File size: 33.4 KB
Line 
1/** @file
2 * VirtualBox X11 Additions graphics driver utility functions
3 */
4
5/*
6 * Copyright (C) 2006-2007 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#include <VBox/VMMDev.h>
22#include <VBox/VBoxGuestLib.h>
23
24#ifndef PCIACCESS
25# include <xf86Pci.h>
26# include <Pci.h>
27#endif
28
29#include "xf86.h"
30#define NEED_XF86_TYPES
31#ifdef NO_ANSIC
32# include <string.h>
33#else
34# include "xf86_ansic.h"
35#endif
36#include "compiler.h"
37#include "cursorstr.h"
38
39#include "vboxvideo.h"
40
41#define VBOX_MAX_CURSOR_WIDTH 64
42#define VBOX_MAX_CURSOR_HEIGHT 64
43
44/**************************************************************************
45* Debugging functions and macros *
46**************************************************************************/
47
48/* #define DEBUG_POINTER */
49
50#define BOOL_STR(a) ((a) ? "TRUE" : "FALSE")
51
52#ifdef DEBUG_VIDEO
53# define TRACE_LINE() do \
54 { \
55 ErrorF ("%s: line %d\n", __FUNCTION__, __LINE__); \
56 } while(0)
57# define PUT_PIXEL(c) ErrorF ("%c", c)
58#else /* DEBUG_VIDEO not defined */
59# define PUT_PIXEL(c) do { } while(0)
60# define TRACE_LINE() do { } while(0)
61#endif /* DEBUG_VIDEO not defined */
62
63/** Macro to printf an error message and return from a function */
64#define RETERROR(scrnIndex, RetVal, ...) \
65 do \
66 { \
67 xf86DrvMsg(scrnIndex, X_ERROR, __VA_ARGS__); \
68 return RetVal; \
69 } \
70 while (0)
71
72#ifdef DEBUG_POINTER
73static void
74vbox_show_shape(unsigned short w, unsigned short h, CARD32 bg, unsigned char *image)
75{
76 size_t x, y;
77 unsigned short pitch;
78 CARD32 *color;
79 unsigned char *mask;
80 size_t sizeMask;
81
82 image += offsetof(VMMDevReqMousePointer, pointerData);
83 mask = image;
84 pitch = (w + 7) / 8;
85 sizeMask = (pitch * h + 3) & ~3;
86 color = (CARD32 *)(image + sizeMask);
87
88 TRACE_ENTRY();
89 for (y = 0; y < h; ++y, mask += pitch, color += w)
90 {
91 for (x = 0; x < w; ++x)
92 {
93 if (mask[x / 8] & (1 << (7 - (x % 8))))
94 ErrorF (" ");
95 else
96 {
97 CARD32 c = color[x];
98 if (c == bg)
99 ErrorF("Y");
100 else
101 ErrorF("X");
102 }
103 }
104 ErrorF("\n");
105 }
106}
107#endif
108
109/**************************************************************************
110* Helper functions and macros *
111**************************************************************************/
112
113/* This is called by the X server every time it loads a new cursor to see
114 * whether our "cursor hardware" can handle the cursor. This provides us with
115 * a mechanism (the only one!) to switch back from a software to a hardware
116 * cursor. */
117static Bool
118vbox_host_uses_hwcursor(ScrnInfoPtr pScrn)
119{
120 Bool rc = TRUE;
121 uint32_t fFeatures = 0;
122 VBOXPtr pVBox = pScrn->driverPrivate;
123
124 TRACE_ENTRY();
125 /* We may want to force the use of a software cursor. Currently this is
126 * needed if the guest uses a large virtual resolution, as in this case
127 * the host and guest tend to disagree about the pointer location. */
128 if (pVBox->forceSWCursor)
129 rc = FALSE;
130 /* Query information about mouse integration from the host. */
131 if (rc) {
132 int vrc = VbglR3GetMouseStatus(&fFeatures, NULL, NULL);
133 if (RT_FAILURE(vrc)) {
134 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
135 "Unable to determine whether the virtual machine supports mouse pointer integration - request initialization failed with return code %d\n", vrc);
136 rc = FALSE;
137 }
138 }
139 /* If we got the information from the host then make sure the host wants
140 * to draw the pointer. */
141 if (rc)
142 {
143 if (fFeatures & VMMDEV_MOUSE_GUEST_CAN_ABSOLUTE)
144 /* Assume this will never be unloaded as long as the X session is
145 * running. */
146 pVBox->mouseDriverLoaded = TRUE;
147 if ( (fFeatures & VMMDEV_MOUSE_HOST_CANNOT_HWPOINTER)
148 || !pVBox->mouseDriverLoaded
149 || !(fFeatures & VMMDEV_MOUSE_HOST_CAN_ABSOLUTE)
150 )
151 rc = FALSE;
152 }
153 TRACE_LOG("rc=%s\n", BOOL_STR(rc));
154 return rc;
155}
156
157/**
158 * Macro to disable VBVA extensions and return, for use when an
159 * unexplained error occurs.
160 */
161#define DISABLE_VBVA_AND_RETURN(pScrn, ...) \
162 do \
163 { \
164 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, __VA_ARGS__); \
165 vboxDisableVbva(pScrn); \
166 pVBox->useVbva = FALSE; \
167 return; \
168 } \
169 while (0)
170
171/**************************************************************************
172* Main functions *
173**************************************************************************/
174
175void
176vbox_close(ScrnInfoPtr pScrn, VBOXPtr pVBox)
177{
178 TRACE_ENTRY();
179
180 xfree (pVBox->reqp);
181 pVBox->reqp = NULL;
182 TRACE_EXIT();
183}
184
185/**
186 * Callback function called by the X server to tell us about dirty
187 * rectangles in the video buffer.
188 *
189 * @param pScreen pointer to the information structure for the current
190 * screen
191 * @param iRects Number of dirty rectangles to update
192 * @param aRects Array of structures containing the coordinates of the
193 * rectangles
194 */
195static void
196vboxHandleDirtyRect(ScrnInfoPtr pScrn, int iRects, BoxPtr aRects)
197{
198 VBVACMDHDR cmdHdr;
199 VBOXPtr pVBox;
200 VBVARECORD *pRecord;
201 VBVAMEMORY *pMem;
202 CARD32 indexRecordNext;
203 CARD32 off32Data;
204 CARD32 off32Free;
205 INT32 i32Diff;
206 CARD32 cbHwBufferAvail;
207 int scrnIndex;
208 int i;
209
210 pVBox = pScrn->driverPrivate;
211 TRACE_ENTRY();
212 if (pVBox->useVbva == FALSE)
213 return;
214 pMem = pVBox->pVbvaMemory;
215 /* Just return quietly if VBVA is not currently active. */
216 if ((pMem->fu32ModeFlags & VBVA_F_MODE_ENABLED) == 0)
217 return;
218 scrnIndex = pScrn->scrnIndex;
219
220 for (i = 0; i < iRects; i++)
221 {
222 cmdHdr.x = (int16_t)aRects[i].x1 - pVBox->viewportX;
223 cmdHdr.y = (int16_t)aRects[i].y1 - pVBox->viewportY;
224 cmdHdr.w = (uint16_t)(aRects[i].x2 - aRects[i].x1);
225 cmdHdr.h = (uint16_t)(aRects[i].y2 - aRects[i].y1);
226
227 /* Get the active record and move the pointer along */
228 indexRecordNext = (pMem->indexRecordFree + 1) % VBVA_MAX_RECORDS;
229 if (indexRecordNext == pMem->indexRecordFirst)
230 {
231 /* All slots in the records queue are used. */
232 if (VbglR3VideoAccelFlush() < 0)
233 DISABLE_VBVA_AND_RETURN(pScrn,
234 "Unable to clear the VirtualBox graphics acceleration queue "
235 "- the request to the virtual machine failed. Switching to "
236 "unaccelerated mode.\n");
237 }
238 if (indexRecordNext == pMem->indexRecordFirst)
239 DISABLE_VBVA_AND_RETURN(pScrn,
240 "Failed to clear the VirtualBox graphics acceleration queue. "
241 "Switching to unaccelerated mode.\n");
242 pRecord = &pMem->aRecords[pMem->indexRecordFree];
243 /* Mark the record as being updated. */
244 pRecord->cbRecord = VBVA_F_RECORD_PARTIAL;
245 pMem->indexRecordFree = indexRecordNext;
246 /* Compute how many bytes we have in the ring buffer. */
247 off32Free = pMem->off32Free;
248 off32Data = pMem->off32Data;
249 /* Free is writing position. Data is reading position.
250 * Data == Free means buffer is free.
251 * There must be always gap between free and data when data
252 * are in the buffer.
253 * Guest only changes free, host only changes data.
254 */
255 i32Diff = off32Data - off32Free;
256 cbHwBufferAvail = i32Diff > 0 ? i32Diff : VBVA_RING_BUFFER_SIZE + i32Diff;
257 if (cbHwBufferAvail <= VBVA_RING_BUFFER_THRESHOLD)
258 {
259 if (VbglR3VideoAccelFlush() < 0)
260 DISABLE_VBVA_AND_RETURN(pScrn,
261 "Unable to clear the VirtualBox graphics acceleration queue "
262 "- the request to the virtual machine failed. Switching to "
263 "unaccelerated mode.\n");
264 /* Calculate the free space again. */
265 off32Free = pMem->off32Free;
266 off32Data = pMem->off32Data;
267 i32Diff = off32Data - off32Free;
268 cbHwBufferAvail = i32Diff > 0? i32Diff:
269 VBVA_RING_BUFFER_SIZE + i32Diff;
270 if (cbHwBufferAvail <= VBVA_RING_BUFFER_THRESHOLD)
271 DISABLE_VBVA_AND_RETURN(pScrn,
272 "No space left in the VirtualBox graphics acceleration command buffer, "
273 "despite clearing the queue. Switching to unaccelerated mode.\n");
274 }
275 /* Now copy the data into the buffer */
276 if (off32Free + sizeof(cmdHdr) < VBVA_RING_BUFFER_SIZE)
277 {
278 memcpy(&pMem->au8RingBuffer[off32Free], &cmdHdr, sizeof(cmdHdr));
279 pMem->off32Free = pMem->off32Free + sizeof(cmdHdr);
280 }
281 else
282 {
283 CARD32 u32First = VBVA_RING_BUFFER_SIZE - off32Free;
284 /* The following is impressively ugly! */
285 CARD8 *pu8Second = (CARD8 *)&cmdHdr + u32First;
286 CARD32 u32Second = sizeof(cmdHdr) - u32First;
287 memcpy(&pMem->au8RingBuffer[off32Free], &cmdHdr, u32First);
288 if (u32Second)
289 memcpy(&pMem->au8RingBuffer[0], pu8Second, u32Second);
290 pMem->off32Free = u32Second;
291 }
292 pRecord->cbRecord += sizeof(cmdHdr);
293 /* Mark the record completed. */
294 pRecord->cbRecord &= ~VBVA_F_RECORD_PARTIAL;
295 }
296}
297
298#ifdef PCIACCESS
299/* As of X.org server 1.5, we are using the pciaccess library functions to
300 * access PCI. This structure describes our VMM device. */
301/** Structure describing the VMM device */
302static const struct pci_id_match vboxVMMDevID =
303{ VMMDEV_VENDORID, VMMDEV_DEVICEID, PCI_MATCH_ANY, PCI_MATCH_ANY,
304 0, 0, 0 };
305#endif
306
307/**
308 * Initialise VirtualBox's accelerated video extensions.
309 *
310 * @returns TRUE on success, FALSE on failure
311 */
312static Bool
313vboxInitVbva(int scrnIndex, ScreenPtr pScreen, VBOXPtr pVBox)
314{
315#ifdef PCIACCESS
316 struct pci_device_iterator *devIter = NULL;
317
318 TRACE_ENTRY();
319 pVBox->vmmDevInfo = NULL;
320 devIter = pci_id_match_iterator_create(&vboxVMMDevID);
321 if (devIter)
322 {
323 pVBox->vmmDevInfo = pci_device_next(devIter);
324 pci_iterator_destroy(devIter);
325 }
326 if (pVBox->vmmDevInfo)
327 {
328 if (pci_device_probe(pVBox->vmmDevInfo) != 0)
329 {
330 xf86DrvMsg (scrnIndex, X_ERROR,
331 "Failed to probe VMM device (vendor=%04x, devid=%04x)\n",
332 pVBox->vmmDevInfo->vendor_id,
333 pVBox->vmmDevInfo->device_id);
334 }
335 else
336 {
337 if (pci_device_map_range(pVBox->vmmDevInfo,
338 pVBox->vmmDevInfo->regions[1].base_addr,
339 pVBox->vmmDevInfo->regions[1].size,
340 PCI_DEV_MAP_FLAG_WRITABLE,
341 (void **)&pVBox->pVMMDevMemory) != 0)
342 xf86DrvMsg (scrnIndex, X_ERROR,
343 "Failed to map VMM device range\n");
344 }
345 }
346#else
347 PCITAG pciTag;
348 ADDRESS pciAddress;
349
350 TRACE_ENTRY();
351 /* Locate the device. It should already have been enabled by
352 the kernel driver. */
353 pciTag = pciFindFirst((unsigned) VMMDEV_DEVICEID << 16 | VMMDEV_VENDORID,
354 (CARD32) ~0);
355 if (pciTag == PCI_NOT_FOUND)
356 {
357 xf86DrvMsg(scrnIndex, X_ERROR,
358 "Could not find the VirtualBox base device on the PCI bus.\n");
359 return FALSE;
360 }
361 /* Read the address and size of the second I/O region. */
362 pciAddress = pciReadLong(pciTag, PCI_MAP_REG_START + 4);
363 if (pciAddress == 0 || pciAddress == (CARD32) ~0)
364 RETERROR(scrnIndex, FALSE,
365 "The VirtualBox base device contains an invalid memory address.\n");
366 if (PCI_MAP_IS64BITMEM(pciAddress))
367 RETERROR(scrnIndex, FALSE,
368 "The VirtualBox base device has a 64bit mapping address. "
369 "This is currently not supported.\n");
370 /* Map it. We hardcode the size as X does not export the
371 function needed to determine it. */
372 pVBox->pVMMDevMemory = xf86MapPciMem(scrnIndex, 0, pciTag, pciAddress,
373 sizeof(VMMDevMemory));
374#endif
375 if (pVBox->pVMMDevMemory == NULL)
376 {
377 xf86DrvMsg(scrnIndex, X_ERROR,
378 "Failed to map VirtualBox video extension memory.\n");
379 return FALSE;
380 }
381 pVBox->pVbvaMemory = &pVBox->pVMMDevMemory->vbvaMemory;
382 /* Set up the dirty rectangle handler. Since this seems to be a
383 delicate operation, and removing it doubly so, this will
384 remain in place whether it is needed or not, and will simply
385 return if VBVA is not active. I assume that it will be active
386 most of the time. */
387 if (ShadowFBInit2(pScreen, NULL, vboxHandleDirtyRect) != TRUE)
388 {
389 xf86DrvMsg(scrnIndex, X_ERROR,
390 "Unable to install dirty rectangle handler for VirtualBox graphics acceleration.\n");
391 return FALSE;
392 }
393 return TRUE;
394}
395
396Bool
397vbox_init(int scrnIndex, VBOXPtr pVBox)
398{
399 Bool rc = TRUE;
400 int vrc;
401 uint32_t fMouseFeatures = 0;
402
403 TRACE_ENTRY();
404 pVBox->useVbva = FALSE;
405 vrc = VbglR3Init();
406 if (RT_FAILURE(vrc))
407 {
408 xf86DrvMsg(scrnIndex, X_ERROR,
409 "Failed to initialize the VirtualBox device (rc=%d) - make sure that the VirtualBox guest additions are properly installed. If you are not sure, try reinstalling them. The X Window graphics drivers will run in compatibility mode.\n",
410 vrc);
411 rc = FALSE;
412 }
413 pVBox->useDevice = rc;
414 /* We can't switch to a software cursor at will without help from
415 * VBoxClient. So tell that to the host and wait for VBoxClient to
416 * change this. */
417 vrc = VbglR3GetMouseStatus(&fMouseFeatures, NULL, NULL);
418 if (RT_SUCCESS(vrc))
419 VbglR3SetMouseStatus( fMouseFeatures
420 | VMMDEV_MOUSE_GUEST_NEEDS_HOST_CURSOR);
421 return rc;
422}
423
424Bool
425vbox_open(ScrnInfoPtr pScrn, ScreenPtr pScreen, VBOXPtr pVBox)
426{
427 int rc;
428 void *p;
429 size_t size;
430 int scrnIndex = pScrn->scrnIndex;
431
432 TRACE_ENTRY();
433
434 if (!pVBox->useDevice)
435 return FALSE;
436
437 if (pVBox->reqp)
438 {
439 /* still open, just re-enable VBVA after CloseScreen was called */
440 pVBox->useVbva = vboxInitVbva(scrnIndex, pScreen, pVBox);
441 return TRUE;
442 }
443
444 size = vmmdevGetRequestSize(VMMDevReq_SetPointerShape);
445 p = xcalloc(1, size);
446 if (p)
447 {
448 rc = vmmdevInitRequest(p, VMMDevReq_SetPointerShape);
449 if (RT_SUCCESS(rc))
450 {
451 pVBox->reqp = p;
452 pVBox->pCurs = NULL;
453 pVBox->pointerHeaderSize = size;
454 pVBox->useVbva = vboxInitVbva(scrnIndex, pScreen, pVBox);
455 return TRUE;
456 }
457 xf86DrvMsg(scrnIndex, X_ERROR, "Could not init VMM request: rc = %d\n", rc);
458 xfree(p);
459 }
460 xf86DrvMsg(scrnIndex, X_ERROR, "Could not allocate %lu bytes for VMM request\n", (unsigned long)size);
461 return FALSE;
462}
463
464Bool
465vbox_device_available(VBOXPtr pVBox)
466{
467 return pVBox->useDevice;
468}
469
470static void
471vbox_vmm_hide_cursor(ScrnInfoPtr pScrn, VBOXPtr pVBox)
472{
473 int rc;
474
475 TRACE_ENTRY();
476 pVBox->reqp->fFlags = 0;
477 rc = VbglR3SetPointerShapeReq(pVBox->reqp);
478 if (RT_FAILURE(rc))
479 {
480 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Could not hide the virtual mouse pointer.\n");
481 /* Play safe, and disable the hardware cursor until the next mode
482 * switch, since obviously something happened that we didn't
483 * anticipate. */
484 pVBox->forceSWCursor = TRUE;
485 }
486}
487
488static void
489vbox_vmm_show_cursor(ScrnInfoPtr pScrn, VBOXPtr pVBox)
490{
491 int rc;
492
493 TRACE_ENTRY();
494 if (vbox_host_uses_hwcursor(pScrn)) {
495 pVBox->reqp->fFlags = VBOX_MOUSE_POINTER_VISIBLE;
496 rc = VbglR3SetPointerShapeReq(pVBox->reqp);
497 if (RT_FAILURE(rc)) {
498 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Could not unhide the virtual mouse pointer.\n");
499 /* Play safe, and disable the hardware cursor until the next mode
500 * switch, since obviously something happened that we didn't
501 * anticipate. */
502 pVBox->forceSWCursor = TRUE;
503 }
504 }
505}
506
507static void
508vbox_vmm_load_cursor_image(ScrnInfoPtr pScrn, VBOXPtr pVBox,
509 unsigned char *image)
510{
511 int rc;
512 VMMDevReqMousePointer *reqp;
513 reqp = (VMMDevReqMousePointer *)image;
514
515 TRACE_LOG("w=%d h=%d size=%d\n", reqp->width, reqp->height, reqp->header.size);
516#ifdef DEBUG_POINTER
517 vbox_show_shape(reqp->width, reqp->height, 0, image);
518#endif
519
520 rc = VbglR3SetPointerShapeReq(reqp);
521 if (RT_FAILURE(rc)) {
522 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Unable to set the virtual mouse pointer image.\n");
523 /* Play safe, and disable the hardware cursor until the next mode
524 * switch, since obviously something happened that we didn't
525 * anticipate. */
526 pVBox->forceSWCursor = TRUE;
527 }
528}
529
530static void
531vbox_set_cursor_colors(ScrnInfoPtr pScrn, int bg, int fg)
532{
533 TRACE_ENTRY();
534
535 NOREF(pScrn);
536 NOREF(bg);
537 NOREF(fg);
538 /* ErrorF("vbox_set_cursor_colors NOT IMPLEMENTED\n"); */
539}
540
541
542static void
543vbox_set_cursor_position(ScrnInfoPtr pScrn, int x, int y)
544{
545 /* Nothing to do here, as we are telling the guest where the mouse is,
546 * not vice versa. */
547 NOREF(pScrn);
548 NOREF(x);
549 NOREF(y);
550}
551
552static void
553vbox_hide_cursor(ScrnInfoPtr pScrn)
554{
555 VBOXPtr pVBox = pScrn->driverPrivate;
556
557 TRACE_ENTRY();
558
559 vbox_vmm_hide_cursor(pScrn, pVBox);
560}
561
562static void
563vbox_show_cursor(ScrnInfoPtr pScrn)
564{
565 VBOXPtr pVBox = pScrn->driverPrivate;
566
567 TRACE_ENTRY();
568
569 vbox_vmm_show_cursor(pScrn, pVBox);
570}
571
572static void
573vbox_load_cursor_image(ScrnInfoPtr pScrn, unsigned char *image)
574{
575 VBOXPtr pVBox = pScrn->driverPrivate;
576
577 TRACE_ENTRY();
578
579 vbox_vmm_load_cursor_image(pScrn, pVBox, image);
580}
581
582static Bool
583vbox_use_hw_cursor(ScreenPtr pScreen, CursorPtr pCurs)
584{
585 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
586 return vbox_host_uses_hwcursor(pScrn);
587}
588
589static unsigned char
590color_to_byte(unsigned c)
591{
592 return (c >> 8) & 0xff;
593}
594
595static unsigned char *
596vbox_realize_cursor(xf86CursorInfoPtr infoPtr, CursorPtr pCurs)
597{
598 VBOXPtr pVBox;
599 CursorBitsPtr bitsp;
600 unsigned short w, h, x, y;
601 unsigned char *c, *p, *pm, *ps, *m;
602 size_t sizeRequest, sizeRgba, sizeMask, srcPitch, dstPitch;
603 CARD32 fc, bc, *cp;
604 int rc, scrnIndex = infoPtr->pScrn->scrnIndex;
605 VMMDevReqMousePointer *reqp;
606
607 TRACE_ENTRY();
608 pVBox = infoPtr->pScrn->driverPrivate;
609 bitsp = pCurs->bits;
610 w = bitsp->width;
611 h = bitsp->height;
612
613 if (!w || !h || w > VBOX_MAX_CURSOR_WIDTH || h > VBOX_MAX_CURSOR_HEIGHT)
614 RETERROR(scrnIndex, NULL,
615 "Error invalid cursor dimensions %dx%d\n", w, h);
616
617 if ((bitsp->xhot > w) || (bitsp->yhot > h))
618 RETERROR(scrnIndex, NULL,
619 "Error invalid cursor hotspot location %dx%d (max %dx%d)\n",
620 bitsp->xhot, bitsp->yhot, w, h);
621
622 srcPitch = PixmapBytePad (bitsp->width, 1);
623 dstPitch = (w + 7) / 8;
624 sizeMask = ((dstPitch * h) + 3) & (size_t) ~3;
625 sizeRgba = w * h * 4;
626 pVBox->pointerSize = sizeMask + sizeRgba;
627 sizeRequest = pVBox->pointerSize + pVBox->pointerHeaderSize;
628
629 p = c = xcalloc (1, sizeRequest);
630 if (!c)
631 RETERROR(scrnIndex, NULL,
632 "Error failed to alloc %lu bytes for cursor\n",
633 (unsigned long) sizeRequest);
634
635 rc = vmmdevInitRequest((VMMDevRequestHeader *)p, VMMDevReq_SetPointerShape);
636 if (RT_FAILURE(rc))
637 {
638 xf86DrvMsg(scrnIndex, X_ERROR, "Could not init VMM request: rc = %d\n", rc);
639 xfree(p);
640 return NULL;
641 }
642
643 m = p + offsetof(VMMDevReqMousePointer, pointerData);
644 cp = (CARD32 *)(m + sizeMask);
645
646 TRACE_LOG ("w=%d h=%d sm=%d sr=%d p=%d\n",
647 w, h, (int) sizeMask, (int) sizeRgba, (int) dstPitch);
648 TRACE_LOG ("m=%p c=%p cp=%p\n", m, c, (void *)cp);
649
650 fc = color_to_byte (pCurs->foreBlue)
651 | (color_to_byte (pCurs->foreGreen) << 8)
652 | (color_to_byte (pCurs->foreRed) << 16);
653
654 bc = color_to_byte (pCurs->backBlue)
655 | (color_to_byte (pCurs->backGreen) << 8)
656 | (color_to_byte (pCurs->backRed) << 16);
657
658 /*
659 * Convert the Xorg source/mask bits to the and/xor bits VBox needs.
660 * Xorg:
661 * The mask is a bitmap indicating which parts of the cursor are
662 * transparent and which parts are drawn. The source is a bitmap
663 * indicating which parts of the non-transparent portion of the
664 * the cursor should be painted in the foreground color and which
665 * should be painted in the background color. By default, set bits
666 * indicate the opaque part of the mask bitmap and clear bits
667 * indicate the transparent part.
668 * VBox:
669 * The color data is the XOR mask. The AND mask bits determine
670 * which pixels of the color data (XOR mask) will replace (overwrite)
671 * the screen pixels (AND mask bit = 0) and which ones will be XORed
672 * with existing screen pixels (AND mask bit = 1).
673 * For example when you have the AND mask all 0, then you see the
674 * correct mouse pointer image surrounded by black square.
675 */
676 for (pm = bitsp->mask, ps = bitsp->source, y = 0;
677 y < h;
678 ++y, pm += srcPitch, ps += srcPitch, m += dstPitch)
679 {
680 for (x = 0; x < w; ++x)
681 {
682 if (pm[x / 8] & (1 << (x % 8)))
683 {
684 /* opaque, leave AND mask bit at 0 */
685 if (ps[x / 8] & (1 << (x % 8)))
686 {
687 *cp++ = fc;
688 PUT_PIXEL('X');
689 }
690 else
691 {
692 *cp++ = bc;
693 PUT_PIXEL('*');
694 }
695 }
696 else
697 {
698 /* transparent, set AND mask bit */
699 m[x / 8] |= 1 << (7 - (x % 8));
700 /* don't change the screen pixel */
701 *cp++ = 0;
702 PUT_PIXEL(' ');
703 }
704 }
705 PUT_PIXEL('\n');
706 }
707
708 reqp = (VMMDevReqMousePointer *)p;
709 reqp->width = w;
710 reqp->height = h;
711 reqp->xHot = bitsp->xhot;
712 reqp->yHot = bitsp->yhot;
713 reqp->fFlags = VBOX_MOUSE_POINTER_VISIBLE | VBOX_MOUSE_POINTER_SHAPE;
714 reqp->header.size = sizeRequest;
715
716#ifdef DEBUG_POINTER
717 ErrorF("shape = %p\n", p);
718 vbox_show_shape(w, h, bc, c);
719#endif
720
721 return p;
722}
723
724#ifdef ARGB_CURSOR
725static Bool
726vbox_use_hw_cursor_argb(ScreenPtr pScreen, CursorPtr pCurs)
727{
728 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
729 Bool rc = TRUE;
730
731 TRACE_ENTRY();
732 if (!vbox_host_uses_hwcursor(pScrn))
733 rc = FALSE;
734 if ( rc
735 && ( (pCurs->bits->height > VBOX_MAX_CURSOR_HEIGHT)
736 || (pCurs->bits->width > VBOX_MAX_CURSOR_WIDTH)
737 || (pScrn->bitsPerPixel <= 8)
738 )
739 )
740 rc = FALSE;
741 TRACE_LOG("rc=%s\n", BOOL_STR(rc));
742 return rc;
743}
744
745
746static void
747vbox_load_cursor_argb(ScrnInfoPtr pScrn, CursorPtr pCurs)
748{
749 VBOXPtr pVBox;
750 VMMDevReqMousePointer *reqp;
751 CursorBitsPtr bitsp;
752 unsigned short w, h;
753 unsigned short cx, cy;
754 unsigned char *pm;
755 CARD32 *pc;
756 size_t sizeRequest, sizeMask;
757 CARD8 *p;
758 int scrnIndex;
759
760 TRACE_ENTRY();
761 pVBox = pScrn->driverPrivate;
762 bitsp = pCurs->bits;
763 w = bitsp->width;
764 h = bitsp->height;
765 scrnIndex = pScrn->scrnIndex;
766
767 /* Mask must be generated for alpha cursors, that is required by VBox. */
768 /* note: (michael) the next struct must be 32bit aligned. */
769 sizeMask = ((w + 7) / 8 * h + 3) & ~3;
770
771 if (!w || !h || w > VBOX_MAX_CURSOR_WIDTH || h > VBOX_MAX_CURSOR_HEIGHT)
772 RETERROR(scrnIndex, ,
773 "Error invalid cursor dimensions %dx%d\n", w, h);
774
775 if ((bitsp->xhot > w) || (bitsp->yhot > h))
776 RETERROR(scrnIndex, ,
777 "Error invalid cursor hotspot location %dx%d (max %dx%d)\n",
778 bitsp->xhot, bitsp->yhot, w, h);
779
780 pVBox->pointerSize = w * h * 4 + sizeMask;
781 sizeRequest = pVBox->pointerSize + pVBox->pointerHeaderSize;
782 p = xcalloc(1, sizeRequest);
783 if (!p)
784 RETERROR(scrnIndex, ,
785 "Error failed to alloc %lu bytes for cursor\n",
786 (unsigned long)sizeRequest);
787
788 reqp = (VMMDevReqMousePointer *)p;
789 *reqp = *pVBox->reqp;
790 reqp->width = w;
791 reqp->height = h;
792 reqp->xHot = bitsp->xhot;
793 reqp->yHot = bitsp->yhot;
794 reqp->fFlags = VBOX_MOUSE_POINTER_VISIBLE | VBOX_MOUSE_POINTER_SHAPE
795 | VBOX_MOUSE_POINTER_ALPHA;
796 reqp->header.size = sizeRequest;
797
798 memcpy(p + offsetof(VMMDevReqMousePointer, pointerData) + sizeMask, bitsp->argb, w * h * 4);
799
800 /* Emulate the AND mask. */
801 pm = p + offsetof(VMMDevReqMousePointer, pointerData);
802 pc = bitsp->argb;
803
804 /* Init AND mask to 1 */
805 memset(pm, 0xFF, sizeMask);
806
807 /*
808 * The additions driver must provide the AND mask for alpha cursors. The host frontend
809 * which can handle alpha channel, will ignore the AND mask and draw an alpha cursor.
810 * But if the host does not support ARGB, then it simply uses the AND mask and the color
811 * data to draw a normal color cursor.
812 */
813 for (cy = 0; cy < h; cy++)
814 {
815 unsigned char bitmask = 0x80;
816
817 for (cx = 0; cx < w; cx++, bitmask >>= 1)
818 {
819 if (bitmask == 0)
820 bitmask = 0x80;
821
822 if (pc[cx] >= 0xF0000000)
823 pm[cx / 8] &= ~bitmask;
824 }
825
826 /* Point to next source and dest scans */
827 pc += w;
828 pm += (w + 7) / 8;
829 }
830
831 VbglR3SetPointerShapeReq(reqp);
832 xfree(p);
833}
834#endif
835
836Bool
837vbox_cursor_init(ScreenPtr pScreen)
838{
839 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
840 VBOXPtr pVBox = pScrn->driverPrivate;
841 xf86CursorInfoPtr pCurs = NULL;
842 Bool rc = TRUE;
843
844 TRACE_ENTRY();
845 if (!pVBox->useDevice)
846 return FALSE;
847 pVBox->pCurs = pCurs = xf86CreateCursorInfoRec();
848 if (!pCurs) {
849 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
850 "Failed to create X Window cursor information structures for virtual mouse.\n");
851 rc = FALSE;
852 }
853 if (rc) {
854 pCurs->MaxWidth = VBOX_MAX_CURSOR_WIDTH;
855 pCurs->MaxHeight = VBOX_MAX_CURSOR_HEIGHT;
856 pCurs->Flags = HARDWARE_CURSOR_TRUECOLOR_AT_8BPP
857 | HARDWARE_CURSOR_SOURCE_MASK_INTERLEAVE_1
858 | HARDWARE_CURSOR_BIT_ORDER_MSBFIRST;
859
860 pCurs->SetCursorColors = vbox_set_cursor_colors;
861 pCurs->SetCursorPosition = vbox_set_cursor_position;
862 pCurs->LoadCursorImage = vbox_load_cursor_image;
863 pCurs->HideCursor = vbox_hide_cursor;
864 pCurs->ShowCursor = vbox_show_cursor;
865 pCurs->UseHWCursor = vbox_use_hw_cursor;
866 pCurs->RealizeCursor = vbox_realize_cursor;
867
868#ifdef ARGB_CURSOR
869 pCurs->UseHWCursorARGB = vbox_use_hw_cursor_argb;
870 pCurs->LoadCursorARGB = vbox_load_cursor_argb;
871#endif
872
873 /* Hide the host cursor before we initialise if we wish to use a
874 * software cursor. */
875 if (pVBox->forceSWCursor)
876 vbox_vmm_hide_cursor(pScrn, pVBox);
877 rc = xf86InitCursor(pScreen, pCurs);
878 }
879 if (!rc)
880 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
881 "Failed to enable mouse pointer integration.\n");
882 if (!rc && (pCurs != NULL))
883 xf86DestroyCursorInfoRec(pCurs);
884 return rc;
885}
886
887/**
888 * Inform VBox that we will supply it with dirty rectangle information
889 * and install the dirty rectangle handler.
890 *
891 * @returns TRUE for success, FALSE for failure
892 * @param pScrn Pointer to a structure describing the X screen in use
893 */
894Bool
895vboxEnableVbva(ScrnInfoPtr pScrn)
896{
897 bool rc = TRUE;
898 int scrnIndex = pScrn->scrnIndex;
899 VBOXPtr pVBox = pScrn->driverPrivate;
900
901 TRACE_ENTRY();
902 if (pVBox->useVbva != TRUE)
903 rc = FALSE;
904 if (rc && RT_FAILURE(VbglR3VideoAccelEnable(true)))
905 /* Request not accepted - disable for old hosts. */
906 xf86DrvMsg(scrnIndex, X_ERROR,
907 "Unable to activate VirtualBox graphics acceleration "
908 "- the request to the virtual machine failed. "
909 "You may be running an old version of VirtualBox.\n");
910 pVBox->useVbva = rc;
911 if (!rc)
912 VbglR3VideoAccelEnable(false);
913 return rc;
914}
915
916/**
917 * Inform VBox that we will stop supplying it with dirty rectangle
918 * information. This function is intended to be called when an X
919 * virtual terminal is disabled, or the X server is terminated.
920 *
921 * @returns TRUE for success, FALSE for failure
922 * @param pScrn Pointer to a structure describing the X screen in use
923 */
924Bool
925vboxDisableVbva(ScrnInfoPtr pScrn)
926{
927 int rc;
928 int scrnIndex = pScrn->scrnIndex;
929 VBOXPtr pVBox = pScrn->driverPrivate;
930
931 TRACE_ENTRY();
932 if (pVBox->useVbva != TRUE) /* Ths function should not have been called */
933 return FALSE;
934 rc = VbglR3VideoAccelEnable(false);
935 if (RT_FAILURE(rc))
936 {
937 xf86DrvMsg(scrnIndex, X_ERROR,
938 "Unable to disable VirtualBox graphics acceleration "
939 "- the request to the virtual machine failed.\n");
940 }
941 else
942 memset(pVBox->pVbvaMemory, 0, sizeof(VBVAMEMORY));
943 return TRUE;
944}
945
946/**
947 * Inform VBox that we are aware of advanced graphics functions
948 * (i.e. dynamic resizing, seamless).
949 *
950 * @returns TRUE for success, FALSE for failure
951 */
952Bool
953vboxEnableGraphicsCap(VBOXPtr pVBox)
954{
955 TRACE_ENTRY();
956 if (!pVBox->useDevice)
957 return FALSE;
958 return RT_SUCCESS(VbglR3SetGuestCaps(VMMDEV_GUEST_SUPPORTS_GRAPHICS, 0));
959}
960
961/**
962 * Inform VBox that we are no longer aware of advanced graphics functions
963 * (i.e. dynamic resizing, seamless).
964 *
965 * @returns TRUE for success, FALSE for failure
966 */
967Bool
968vboxDisableGraphicsCap(VBOXPtr pVBox)
969{
970 TRACE_ENTRY();
971 if (!pVBox->useDevice)
972 return FALSE;
973 return RT_SUCCESS(VbglR3SetGuestCaps(0, VMMDEV_GUEST_SUPPORTS_GRAPHICS));
974}
975
976/**
977 * Query the last display change request.
978 *
979 * @returns boolean success indicator.
980 * @param pScrn Pointer to the X screen info structure.
981 * @param pcx Where to store the horizontal pixel resolution (0 = do not change).
982 * @param pcy Where to store the vertical pixel resolution (0 = do not change).
983 * @param pcBits Where to store the bits per pixel (0 = do not change).
984 * @param iDisplay Where to store the display number the request was for - 0 for the
985 * primary display, 1 for the first secondary, etc.
986 */
987Bool
988vboxGetDisplayChangeRequest(ScrnInfoPtr pScrn, uint32_t *pcx, uint32_t *pcy,
989 uint32_t *pcBits, uint32_t *piDisplay)
990{
991 VBOXPtr pVBox = pScrn->driverPrivate;
992 TRACE_ENTRY();
993 if (!pVBox->useDevice)
994 return FALSE;
995 int rc = VbglR3GetDisplayChangeRequest(pcx, pcy, pcBits, piDisplay, true);
996 if (RT_SUCCESS(rc))
997 return TRUE;
998 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Failed to obtain the last resolution requested by the guest, rc=%d.\n", rc);
999 return FALSE;
1000}
1001
1002
1003/**
1004 * Query the host as to whether it likes a specific video mode.
1005 *
1006 * @returns the result of the query
1007 * @param cx the width of the mode being queried
1008 * @param cy the height of the mode being queried
1009 * @param cBits the bpp of the mode being queried
1010 */
1011Bool
1012vboxHostLikesVideoMode(ScrnInfoPtr pScrn, uint32_t cx, uint32_t cy, uint32_t cBits)
1013{
1014 VBOXPtr pVBox = pScrn->driverPrivate;
1015 TRACE_ENTRY();
1016 if (!pVBox->useDevice)
1017 return TRUE; /* If we can't ask the host then we like everything. */
1018 return VbglR3HostLikesVideoMode(cx, cy, cBits);
1019}
1020
1021/**
1022 * Save video mode parameters to the registry.
1023 *
1024 * @returns iprt status value
1025 * @param pszName the name to save the mode parameters under
1026 * @param cx mode width
1027 * @param cy mode height
1028 * @param cBits bits per pixel for the mode
1029 */
1030Bool
1031vboxSaveVideoMode(ScrnInfoPtr pScrn, uint32_t cx, uint32_t cy, uint32_t cBits)
1032{
1033 VBOXPtr pVBox = pScrn->driverPrivate;
1034 TRACE_ENTRY();
1035 if (!pVBox->useDevice)
1036 return FALSE;
1037 return RT_SUCCESS(VbglR3SaveVideoMode("SavedMode", cx, cy, cBits));
1038}
1039
1040/**
1041 * Retrieve video mode parameters from the registry.
1042 *
1043 * @returns iprt status value
1044 * @param pszName the name under which the mode parameters are saved
1045 * @param pcx where to store the mode width
1046 * @param pcy where to store the mode height
1047 * @param pcBits where to store the bits per pixel for the mode
1048 */
1049Bool
1050vboxRetrieveVideoMode(ScrnInfoPtr pScrn, uint32_t *pcx, uint32_t *pcy, uint32_t *pcBits)
1051{
1052 VBOXPtr pVBox = pScrn->driverPrivate;
1053 TRACE_ENTRY();
1054 if (!pVBox->useDevice)
1055 return FALSE;
1056 int rc = VbglR3RetrieveVideoMode("SavedMode", pcx, pcy, pcBits);
1057 if (RT_SUCCESS(rc))
1058 TRACE_LOG("Retrieved a video mode of %dx%dx%d\n", *pcx, *pcy, *pcBits);
1059 else
1060 TRACE_LOG("Failed to retrieve video mode, error %d\n", rc);
1061 return (RT_SUCCESS(rc));
1062}
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