VirtualBox

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

Last change on this file since 17935 was 17318, checked in by vboxsync, 16 years ago

Additions/x11/vboxvideo: more DRI updates

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