VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Miniport/VBoxVideoHGSMI.cpp@ 29745

Last change on this file since 29745 was 29742, checked in by vboxsync, 15 years ago

wddm/2D: more impl (get caps + initialization working)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 53.3 KB
Line 
1/* $Id: VBoxVideoHGSMI.cpp 29742 2010-05-21 15:58:56Z vboxsync $ */
2/** @file
3 * VirtualBox Video miniport driver for NT/2k/XP - HGSMI related functions.
4 */
5
6/*
7 * Copyright (C) 2006-2009 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19#include "VBoxVideo.h"
20#include "Helper.h"
21
22#include <iprt/asm.h>
23#include <iprt/log.h>
24#include <iprt/thread.h>
25#include <VBox/VMMDev.h>
26#include <VBox/VBoxGuest.h>
27#include <VBox/VBoxVideo.h>
28
29#include <VBoxDisplay.h>
30
31#include "vboxioctl.h"
32
33#define MEM_TAG 'HVBV'
34
35void HGSMINotifyHostCmdComplete (PDEVICE_EXTENSION PrimaryExtension, HGSMIOFFSET offt)
36{
37 VBoxHGSMIHostWrite(PrimaryExtension, offt);
38}
39
40void HGSMIClearIrq (PDEVICE_EXTENSION PrimaryExtension)
41{
42 VBoxHGSMIHostWrite(PrimaryExtension, HGSMIOFFSET_VOID);
43}
44
45static void HGSMIHostCmdComplete (PDEVICE_EXTENSION PrimaryExtension, void * pvMem)
46{
47 HGSMIOFFSET offMem = HGSMIPointerToOffset (&PrimaryExtension->u.primary.areaHostHeap, HGSMIBufferHeaderFromData (pvMem));
48 Assert(offMem != HGSMIOFFSET_VOID);
49 if(offMem != HGSMIOFFSET_VOID)
50 {
51 HGSMINotifyHostCmdComplete (PrimaryExtension, offMem);
52 }
53}
54
55static void hgsmiHostCmdProcess(PDEVICE_EXTENSION PrimaryExtension, HGSMIOFFSET offBuffer)
56{
57 int rc = HGSMIBufferProcess (&PrimaryExtension->u.primary.areaHostHeap,
58 &PrimaryExtension->u.primary.channels,
59 offBuffer);
60 Assert(!RT_FAILURE(rc));
61 if(RT_FAILURE(rc))
62 {
63 /* failure means the command was not submitted to the handler for some reason
64 * it's our responsibility to notify its completion in this case */
65 HGSMINotifyHostCmdComplete(PrimaryExtension, offBuffer);
66 }
67 /* if the cmd succeeded it's responsibility of the callback to complete it */
68}
69
70static HGSMIOFFSET hgsmiGetHostBuffer (PDEVICE_EXTENSION PrimaryExtension)
71{
72 return VBoxHGSMIHostRead(PrimaryExtension);
73}
74
75static void hgsmiHostCommandQueryProcess (PDEVICE_EXTENSION PrimaryExtension)
76{
77 HGSMIOFFSET offset = hgsmiGetHostBuffer (PrimaryExtension);
78 Assert(offset != HGSMIOFFSET_VOID);
79 if(offset != HGSMIOFFSET_VOID)
80 {
81 hgsmiHostCmdProcess(PrimaryExtension, offset);
82 }
83}
84
85#define VBOX_HGSMI_LOCK(_pe, _plock, _dpc, _pold) \
86 do { \
87 if(_dpc) \
88 { \
89 VBoxVideoCmnSpinLockAcquireAtDpcLevel(_pe, _plock); \
90 } \
91 else \
92 {\
93 VBoxVideoCmnSpinLockAcquire(_pe, _plock, _pold); \
94 }\
95 } while(0)
96
97#define VBOX_HGSMI_UNLOCK(_pe, _plock, _dpc, _pold) \
98 do { \
99 if(_dpc) \
100 { \
101 VBoxVideoCmnSpinLockReleaseFromDpcLevel(_pe, _plock); \
102 } \
103 else \
104 {\
105 VBoxVideoCmnSpinLockRelease(_pe, _plock, _pold); \
106 }\
107 } while(0)
108
109VOID VBoxVideoHGSMIDpc(
110 IN PVOID HwDeviceExtension,
111 IN PVOID Context
112 )
113{
114 PDEVICE_EXTENSION PrimaryExtension = (PDEVICE_EXTENSION)HwDeviceExtension;
115 uint32_t flags = (uint32_t)Context;
116 bool bProcessing = false;
117 VBOXVCMNIRQL OldIrql;
118 /* we check if another thread is processing the queue and exit if so */
119 do
120 {
121 bool bLock = false;
122 if(!(PrimaryExtension->u.primary.pHostFlags->u32HostFlags & HGSMIHOSTFLAGS_COMMANDS_PENDING))
123 {
124 if(!bProcessing)
125 {
126 break;
127 }
128 VBOX_HGSMI_LOCK(PrimaryExtension, &PrimaryExtension->u.primary.pSynchLock, flags, &OldIrql);
129 if(!(PrimaryExtension->u.primary.pHostFlags->u32HostFlags & HGSMIHOSTFLAGS_COMMANDS_PENDING))
130 {
131 Assert(PrimaryExtension->u.primary.bHostCmdProcessing);
132 PrimaryExtension->u.primary.bHostCmdProcessing = false;
133 VBOX_HGSMI_UNLOCK(PrimaryExtension, &PrimaryExtension->u.primary.pSynchLock, flags, OldIrql);
134 break;
135 }
136 VBOX_HGSMI_UNLOCK(PrimaryExtension, &PrimaryExtension->u.primary.pSynchLock, flags, OldIrql);
137 }
138 else
139 {
140 if(!bProcessing)
141 {
142 VBOX_HGSMI_LOCK(PrimaryExtension, &PrimaryExtension->u.primary.pSynchLock, flags, &OldIrql);
143 if(!(PrimaryExtension->u.primary.pHostFlags->u32HostFlags & HGSMIHOSTFLAGS_COMMANDS_PENDING)
144 || PrimaryExtension->u.primary.bHostCmdProcessing)
145 {
146 VBOX_HGSMI_UNLOCK(PrimaryExtension, &PrimaryExtension->u.primary.pSynchLock, flags, OldIrql);
147 break;
148 }
149 Assert(!PrimaryExtension->u.primary.bHostCmdProcessing);
150 PrimaryExtension->u.primary.bHostCmdProcessing = true;
151 VBOX_HGSMI_UNLOCK(PrimaryExtension, &PrimaryExtension->u.primary.pSynchLock, flags, OldIrql);
152 bProcessing = true;
153 }
154 }
155
156 Assert(bProcessing);
157 Assert(PrimaryExtension->u.primary.bHostCmdProcessing);
158 Assert((PrimaryExtension->u.primary.pHostFlags->u32HostFlags & HGSMIHOSTFLAGS_COMMANDS_PENDING) != 0);
159 bProcessing = true;
160
161 hgsmiHostCommandQueryProcess (PrimaryExtension);
162 } while(true/*!PrimaryExtension->u.primary.bPollingStop*/);
163}
164
165/* Detect whether HGSMI is supported by the host. */
166BOOLEAN VBoxHGSMIIsSupported (PDEVICE_EXTENSION PrimaryExtension)
167{
168 USHORT DispiId;
169
170 VBoxVideoCmnPortWriteUshort((PUSHORT)VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_ID);
171 VBoxVideoCmnPortWriteUshort((PUSHORT)VBE_DISPI_IOPORT_DATA, VBE_DISPI_ID_HGSMI);
172
173 DispiId = VBoxVideoCmnPortReadUshort((PUSHORT)VBE_DISPI_IOPORT_DATA);
174
175 return (DispiId == VBE_DISPI_ID_HGSMI);
176}
177
178typedef int FNHGSMICALLINIT (PDEVICE_EXTENSION PrimaryExtension, void *pvContext, void *pvData);
179typedef FNHGSMICALLINIT *PFNHGSMICALLINIT;
180
181typedef int FNHGSMICALLFINALIZE (PDEVICE_EXTENSION PrimaryExtension, void *pvContext, void *pvData);
182typedef FNHGSMICALLFINALIZE *PFNHGSMICALLFINALIZE;
183
184void* vboxHGSMIBufferAlloc(PDEVICE_EXTENSION PrimaryExtension,
185 HGSMISIZE cbData,
186 uint8_t u8Ch,
187 uint16_t u16Op)
188{
189#ifdef VBOXWDDM
190 /* @todo: add synchronization */
191#endif
192 return HGSMIHeapAlloc (&PrimaryExtension->u.primary.hgsmiAdapterHeap, cbData, u8Ch, u16Op);
193}
194
195void vboxHGSMIBufferFree (PDEVICE_EXTENSION PrimaryExtension, void *pvBuffer)
196{
197#ifdef VBOXWDDM
198 /* @todo: add synchronization */
199#endif
200 HGSMIHeapFree (&PrimaryExtension->u.primary.hgsmiAdapterHeap, pvBuffer);
201}
202
203int vboxHGSMIBufferSubmit (PDEVICE_EXTENSION PrimaryExtension, void *pvBuffer)
204{
205 /* Initialize the buffer and get the offset for port IO. */
206 HGSMIOFFSET offBuffer = HGSMIHeapBufferOffset (&PrimaryExtension->u.primary.hgsmiAdapterHeap, pvBuffer);
207
208 Assert(offBuffer != HGSMIOFFSET_VOID);
209 if (offBuffer != HGSMIOFFSET_VOID)
210 {
211 /* Submit the buffer to the host. */
212 VBoxHGSMIGuestWrite(PrimaryExtension, offBuffer);
213 return VINF_SUCCESS;
214 }
215
216 return VERR_INVALID_PARAMETER;
217}
218
219static int vboxCallChannel (PDEVICE_EXTENSION PrimaryExtension,
220 uint8_t u8Ch,
221 uint16_t u16Op,
222 HGSMISIZE cbData,
223 PFNHGSMICALLINIT pfnInit,
224 PFNHGSMICALLFINALIZE pfnFinalize,
225 void *pvContext)
226{
227 int rc = VINF_SUCCESS;
228
229 /* Allocate the IO buffer. */
230#ifndef VBOXWDDM
231 if (PrimaryExtension->pPrimary != PrimaryExtension)
232 {
233 dprintf(("VBoxVideo::vboxCallChannel: not primary extension %p!!!\n", PrimaryExtension));
234 return VERR_INVALID_PARAMETER;
235 }
236#endif
237
238 void *p = HGSMIHeapAlloc (&PrimaryExtension->u.primary.hgsmiAdapterHeap, cbData, u8Ch, u16Op);
239
240 if (!p)
241 {
242 rc = VERR_NO_MEMORY;
243 }
244 else
245 {
246 /* Prepare data to be sent to the host. */
247 if (pfnInit)
248 {
249 rc = pfnInit (PrimaryExtension, pvContext, p);
250 }
251
252 if (RT_SUCCESS (rc))
253 {
254 /* Initialize the buffer and get the offset for port IO. */
255 HGSMIOFFSET offBuffer = HGSMIHeapBufferOffset (&PrimaryExtension->u.primary.hgsmiAdapterHeap,
256 p);
257
258 /* Submit the buffer to the host. */
259 VBoxHGSMIGuestWrite(PrimaryExtension, offBuffer);
260
261 if (pfnFinalize)
262 {
263 rc = pfnFinalize (PrimaryExtension, pvContext, p);
264 }
265 }
266 else
267 {
268 AssertFailed ();
269 rc = VERR_INTERNAL_ERROR;
270 }
271
272 /* Free the IO buffer. */
273 HGSMIHeapFree (&PrimaryExtension->u.primary.hgsmiAdapterHeap, p);
274 }
275
276 return rc;
277}
278
279static int vboxCallVBVA (PDEVICE_EXTENSION PrimaryExtension,
280 uint16_t u16Op,
281 HGSMISIZE cbData,
282 PFNHGSMICALLINIT pfnInit,
283 PFNHGSMICALLFINALIZE pfnFinalize,
284 void *pvContext)
285{
286 return vboxCallChannel (PrimaryExtension,
287 HGSMI_CH_VBVA,
288 u16Op,
289 cbData,
290 pfnInit,
291 pfnFinalize,
292 pvContext);
293}
294
295typedef struct _QUERYCONFCTX
296{
297 uint32_t u32Index;
298 ULONG *pulValue;
299} QUERYCONFCTX;
300
301static int vbvaInitQueryConf (PDEVICE_EXTENSION PrimaryExtension, void *pvContext, void *pvData)
302{
303 NOREF (PrimaryExtension);
304
305 QUERYCONFCTX *pCtx = (QUERYCONFCTX *)pvContext;
306 VBVACONF32 *p = (VBVACONF32 *)pvData;
307
308 p->u32Index = pCtx->u32Index;
309 p->u32Value = 0;
310
311 return VINF_SUCCESS;
312}
313
314static int vbvaFinalizeQueryConf (PDEVICE_EXTENSION PrimaryExtension, void *pvContext, void *pvData)
315{
316 NOREF (PrimaryExtension);
317
318 QUERYCONFCTX *pCtx = (QUERYCONFCTX *)pvContext;
319 VBVACONF32 *p = (VBVACONF32 *)pvData;
320
321 if (pCtx->pulValue)
322 {
323 *pCtx->pulValue = p->u32Value;
324 }
325
326 dprintf(("VBoxVideo::vboxQueryConf: u32Value = %d\n", p->u32Value));
327 return VINF_SUCCESS;
328}
329
330static int vboxQueryConfHGSMI (PDEVICE_EXTENSION PrimaryExtension, uint32_t u32Index, ULONG *pulValue)
331{
332 dprintf(("VBoxVideo::vboxQueryConf: u32Index = %d\n", u32Index));
333
334 QUERYCONFCTX context;
335
336 context.u32Index = u32Index;
337 context.pulValue = pulValue;
338
339 int rc = vboxCallVBVA (PrimaryExtension,
340 VBVA_QUERY_CONF32,
341 sizeof (VBVACONF32),
342 vbvaInitQueryConf,
343 vbvaFinalizeQueryConf,
344 &context);
345
346 dprintf(("VBoxVideo::vboxQueryConf: rc = %d\n", rc));
347
348 return rc;
349}
350#ifndef VBOXWDDM
351static int vbvaInitInfoDisplay (PDEVICE_EXTENSION PrimaryExtension, void *pvContext, void *pvData)
352{
353 NOREF (pvContext);
354 VBVAINFOVIEW *p = (VBVAINFOVIEW *)pvData;
355
356 int i;
357 PDEVICE_EXTENSION Extension;
358
359 for (i = 0, Extension = PrimaryExtension;
360 i < PrimaryExtension->u.primary.cDisplays && Extension;
361 i++, Extension = Extension->pNext)
362 {
363 p[i].u32ViewIndex = Extension->iDevice;
364 p[i].u32ViewOffset = Extension->ulFrameBufferOffset;
365 p[i].u32ViewSize = PrimaryExtension->u.primary.ulMaxFrameBufferSize;
366
367 /* How much VRAM should be reserved for the guest drivers to use VBVA. */
368 const uint32_t cbReservedVRAM = VBVA_DISPLAY_INFORMATION_SIZE + VBVA_MIN_BUFFER_SIZE;
369
370 p[i].u32MaxScreenSize = p[i].u32ViewSize > cbReservedVRAM?
371 p[i].u32ViewSize - cbReservedVRAM:
372 0;
373 }
374
375 if (i == PrimaryExtension->u.primary.cDisplays && Extension == NULL)
376 {
377 return VINF_SUCCESS;
378 }
379
380 AssertFailed ();
381 return VERR_INTERNAL_ERROR;
382}
383#else
384int vbvaInitInfoCaps (PDEVICE_EXTENSION PrimaryExtension, void *pvContext, void *pvData)
385{
386 VBVACAPS *pCaps = (VBVACAPS*)pvData;
387 pCaps->rc = VERR_NOT_IMPLEMENTED;
388 pCaps->fCaps = VBVACAPS_COMPLETEGCMD_BY_IOREAD | VBVACAPS_IRQ;
389 return VINF_SUCCESS;
390}
391
392
393int vbvaFinalizeInfoCaps (PDEVICE_EXTENSION PrimaryExtension, void *pvContext, void *pvData)
394{
395 VBVACAPS *pCaps = (VBVACAPS*)pvData;
396 AssertRC(pCaps->rc);
397 return pCaps->rc;
398}
399#endif
400
401static int vbvaInitInfoHeap (PDEVICE_EXTENSION PrimaryExtension, void *pvContext, void *pvData)
402{
403 NOREF (pvContext);
404 VBVAINFOHEAP *p = (VBVAINFOHEAP *)pvData;
405
406 p->u32HeapOffset = PrimaryExtension->u.primary.cbVRAM
407 - PrimaryExtension->u.primary.cbMiniportHeap
408 - VBVA_ADAPTER_INFORMATION_SIZE;
409 p->u32HeapSize = PrimaryExtension->u.primary.cbMiniportHeap;
410
411 return VINF_SUCCESS;
412}
413
414static int hgsmiInitFlagsLocation (PDEVICE_EXTENSION PrimaryExtension, void *pvContext, void *pvData)
415{
416 NOREF (pvContext);
417 HGSMIBUFFERLOCATION *p = (HGSMIBUFFERLOCATION *)pvData;
418
419 p->offLocation = PrimaryExtension->u.primary.cbVRAM - sizeof (HGSMIHOSTFLAGS);
420 p->cbLocation = sizeof (HGSMIHOSTFLAGS);
421
422 return VINF_SUCCESS;
423}
424
425
426static int vboxSetupAdapterInfoHGSMI (PDEVICE_EXTENSION PrimaryExtension)
427{
428 dprintf(("VBoxVideo::vboxSetupAdapterInfo\n"));
429
430 /* setup the flags first to ensure they are initialized by the time the host heap is ready */
431 int rc = vboxCallChannel(PrimaryExtension,
432 HGSMI_CH_HGSMI,
433 HGSMI_CC_HOST_FLAGS_LOCATION,
434 sizeof (HGSMIBUFFERLOCATION),
435 hgsmiInitFlagsLocation,
436 NULL,
437 NULL);
438 AssertRC(rc);
439 if(RT_SUCCESS (rc))
440 {
441#ifndef VBOXWDDM
442 rc = vboxCallVBVA (PrimaryExtension,
443 VBVA_INFO_VIEW,
444 sizeof (VBVAINFOVIEW) * PrimaryExtension->u.primary.cDisplays,
445 vbvaInitInfoDisplay,
446 NULL,
447 NULL);
448 AssertRC(rc);
449 if (RT_SUCCESS (rc))
450#else
451 /* in case of WDDM we do not control the framebuffer location,
452 * i.e. it is assigned by Video Memory Manager,
453 * The FB information should be passed to guest from our DxgkDdiSetVidPnSourceAddress callback */
454
455 /* Inform about caps */
456 rc = vboxCallVBVA (PrimaryExtension,
457 VBVA_INFO_CAPS,
458 sizeof (VBVACAPS),
459 vbvaInitInfoCaps,
460 vbvaFinalizeInfoCaps,
461 NULL);
462 AssertRC(rc);
463 if (RT_SUCCESS (rc))
464#endif
465 {
466 /* Report the host heap location. */
467 rc = vboxCallVBVA (PrimaryExtension,
468 VBVA_INFO_HEAP,
469 sizeof (VBVAINFOHEAP),
470 vbvaInitInfoHeap,
471 NULL,
472 NULL);
473 AssertRC(rc);
474 }
475 }
476
477
478 dprintf(("VBoxVideo::vboxSetupAdapterInfo finished rc = %d\n", rc));
479
480 return rc;
481}
482
483#ifndef VBOXWDDM
484VP_STATUS vboxWaitForSingleObjectVoid(IN PVOID HwDeviceExtension, IN PVOID Object, IN PLARGE_INTEGER Timeout OPTIONAL)
485{
486 return ERROR_INVALID_FUNCTION;
487}
488
489LONG vboxSetEventVoid(IN PVOID HwDeviceExtension, IN PEVENT pEvent)
490{
491 return 0;
492}
493
494VOID vboxClearEventVoid (IN PVOID HwDeviceExtension, IN PEVENT pEvent)
495{
496}
497
498VP_STATUS vboxCreateEventVoid(IN PVOID HwDeviceExtension, IN ULONG EventFlag, IN PVOID Unused, OUT PEVENT *ppEvent)
499{
500 return ERROR_INVALID_FUNCTION;
501}
502
503VP_STATUS vboxDeleteEventVoid(IN PVOID HwDeviceExtension, IN PEVENT pEvent)
504{
505 return ERROR_INVALID_FUNCTION;
506}
507
508VP_STATUS vboxCreateSpinLockVoid (IN PVOID HwDeviceExtension, OUT PSPIN_LOCK *SpinLock)
509{
510 return ERROR_INVALID_FUNCTION;
511}
512
513VP_STATUS vboxDeleteSpinLockVoid (IN PVOID HwDeviceExtension, IN PSPIN_LOCK SpinLock)
514{
515 return ERROR_INVALID_FUNCTION;
516}
517
518VOID vboxAcquireSpinLockVoid (IN PVOID HwDeviceExtension, IN PSPIN_LOCK SpinLock, OUT PUCHAR OldIrql)
519{
520}
521
522VOID vboxReleaseSpinLockVoid (IN PVOID HwDeviceExtension, IN PSPIN_LOCK SpinLock, IN UCHAR NewIrql)
523{
524}
525
526VOID vboxAcquireSpinLockAtDpcLevelVoid (IN PVOID HwDeviceExtension, IN PSPIN_LOCK SpinLock)
527{
528}
529
530VOID vboxReleaseSpinLockFromDpcLevelVoid (IN PVOID HwDeviceExtension, IN PSPIN_LOCK SpinLock)
531{
532}
533
534PVOID vboxAllocatePoolVoid(IN PVOID HwDeviceExtension, IN VBOXVP_POOL_TYPE PoolType, IN size_t NumberOfBytes, IN ULONG Tag)
535{
536 return NULL;
537}
538
539VOID vboxFreePoolVoid(IN PVOID HwDeviceExtension, IN PVOID Ptr)
540{
541}
542
543BOOLEAN vboxQueueDpcVoid(IN PVOID HwDeviceExtension, IN PMINIPORT_DPC_ROUTINE CallbackRoutine, IN PVOID Context)
544{
545 return FALSE;
546}
547
548void VBoxSetupVideoPortFunctions(PDEVICE_EXTENSION PrimaryExtension, VBOXVIDEOPORTPROCS *pCallbacks, PVIDEO_PORT_CONFIG_INFO pConfigInfo)
549{
550 memset(pCallbacks, 0, sizeof(VBOXVIDEOPORTPROCS));
551
552 if (vboxQueryWinVersion() <= WINNT4)
553 {
554 /* VideoPortGetProcAddress is available for >= win2k */
555 pCallbacks->pfnWaitForSingleObject = vboxWaitForSingleObjectVoid;
556 pCallbacks->pfnSetEvent = vboxSetEventVoid;
557 pCallbacks->pfnClearEvent = vboxClearEventVoid;
558 pCallbacks->pfnCreateEvent = vboxCreateEventVoid;
559 pCallbacks->pfnDeleteEvent = vboxDeleteEventVoid;
560 pCallbacks->pfnCreateSpinLock = vboxCreateSpinLockVoid;
561 pCallbacks->pfnDeleteSpinLock = vboxDeleteSpinLockVoid;
562 pCallbacks->pfnAcquireSpinLock = vboxAcquireSpinLockVoid;
563 pCallbacks->pfnReleaseSpinLock = vboxReleaseSpinLockVoid;
564 pCallbacks->pfnAcquireSpinLockAtDpcLevel = vboxAcquireSpinLockAtDpcLevelVoid;
565 pCallbacks->pfnReleaseSpinLockFromDpcLevel = vboxReleaseSpinLockFromDpcLevelVoid;
566 pCallbacks->pfnAllocatePool = vboxAllocatePoolVoid;
567 pCallbacks->pfnFreePool = vboxFreePoolVoid;
568 pCallbacks->pfnQueueDpc = vboxQueueDpcVoid;
569 return;
570 }
571
572 pCallbacks->pfnWaitForSingleObject = (PFNWAITFORSINGLEOBJECT)(pConfigInfo->VideoPortGetProcAddress)
573 (PrimaryExtension,
574 (PUCHAR)"VideoPortWaitForSingleObject");
575 Assert(pCallbacks->pfnWaitForSingleObject);
576
577 pCallbacks->pfnSetEvent = (PFNSETEVENT)(pConfigInfo->VideoPortGetProcAddress)
578 (PrimaryExtension,
579 (PUCHAR)"VideoPortSetEvent");
580 Assert(pCallbacks->pfnSetEvent);
581
582 pCallbacks->pfnClearEvent = (PFNCLEAREVENT)(pConfigInfo->VideoPortGetProcAddress)
583 (PrimaryExtension,
584 (PUCHAR)"VideoPortClearEvent");
585 Assert(pCallbacks->pfnClearEvent);
586
587 pCallbacks->pfnCreateEvent = (PFNCREATEEVENT)(pConfigInfo->VideoPortGetProcAddress)
588 (PrimaryExtension,
589 (PUCHAR)"VideoPortCreateEvent");
590 Assert(pCallbacks->pfnCreateEvent);
591
592 pCallbacks->pfnDeleteEvent = (PFNDELETEEVENT)(pConfigInfo->VideoPortGetProcAddress)
593 (PrimaryExtension,
594 (PUCHAR)"VideoPortDeleteEvent");
595 Assert(pCallbacks->pfnDeleteEvent);
596
597 if(pCallbacks->pfnWaitForSingleObject
598 && pCallbacks->pfnSetEvent
599 && pCallbacks->pfnClearEvent
600 && pCallbacks->pfnCreateEvent
601 && pCallbacks->pfnDeleteEvent)
602 {
603 pCallbacks->fSupportedTypes |= VBOXVIDEOPORTPROCS_EVENT;
604 }
605 else
606 {
607 pCallbacks->pfnWaitForSingleObject = vboxWaitForSingleObjectVoid;
608 pCallbacks->pfnSetEvent = vboxSetEventVoid;
609 pCallbacks->pfnClearEvent = vboxClearEventVoid;
610 pCallbacks->pfnCreateEvent = vboxCreateEventVoid;
611 pCallbacks->pfnDeleteEvent = vboxDeleteEventVoid;
612 }
613
614 pCallbacks->pfnCreateSpinLock = (PFNCREATESPINLOCK)(pConfigInfo->VideoPortGetProcAddress)
615 (PrimaryExtension,
616 (PUCHAR)"VideoPortCreateSpinLock");
617 Assert(pCallbacks->pfnCreateSpinLock);
618
619 pCallbacks->pfnDeleteSpinLock = (PFNDELETESPINLOCK)(pConfigInfo->VideoPortGetProcAddress)
620 (PrimaryExtension,
621 (PUCHAR)"VideoPortDeleteSpinLock");
622 Assert(pCallbacks->pfnDeleteSpinLock);
623
624 pCallbacks->pfnAcquireSpinLock = (PFNACQUIRESPINLOCK)(pConfigInfo->VideoPortGetProcAddress)
625 (PrimaryExtension,
626 (PUCHAR)"VideoPortAcquireSpinLock");
627 Assert(pCallbacks->pfnAcquireSpinLock);
628
629 pCallbacks->pfnReleaseSpinLock = (PFNRELEASESPINLOCK)(pConfigInfo->VideoPortGetProcAddress)
630 (PrimaryExtension,
631 (PUCHAR)"VideoPortReleaseSpinLock");
632 Assert(pCallbacks->pfnReleaseSpinLock);
633
634 pCallbacks->pfnAcquireSpinLockAtDpcLevel = (PFNACQUIRESPINLOCKATDPCLEVEL)(pConfigInfo->VideoPortGetProcAddress)
635 (PrimaryExtension,
636 (PUCHAR)"VideoPortAcquireSpinLockAtDpcLevel");
637 Assert(pCallbacks->pfnAcquireSpinLockAtDpcLevel);
638
639 pCallbacks->pfnReleaseSpinLockFromDpcLevel = (PFNRELEASESPINLOCKFROMDPCLEVEL)(pConfigInfo->VideoPortGetProcAddress)
640 (PrimaryExtension,
641 (PUCHAR)"VideoPortReleaseSpinLockFromDpcLevel");
642 Assert(pCallbacks->pfnReleaseSpinLockFromDpcLevel);
643
644 if(pCallbacks->pfnCreateSpinLock
645 && pCallbacks->pfnDeleteSpinLock
646 && pCallbacks->pfnAcquireSpinLock
647 && pCallbacks->pfnReleaseSpinLock
648 && pCallbacks->pfnAcquireSpinLockAtDpcLevel
649 && pCallbacks->pfnReleaseSpinLockFromDpcLevel)
650 {
651 pCallbacks->fSupportedTypes |= VBOXVIDEOPORTPROCS_SPINLOCK;
652 }
653 else
654 {
655 pCallbacks->pfnCreateSpinLock = vboxCreateSpinLockVoid;
656 pCallbacks->pfnDeleteSpinLock = vboxDeleteSpinLockVoid;
657 pCallbacks->pfnAcquireSpinLock = vboxAcquireSpinLockVoid;
658 pCallbacks->pfnReleaseSpinLock = vboxReleaseSpinLockVoid;
659 pCallbacks->pfnAcquireSpinLockAtDpcLevel = vboxAcquireSpinLockAtDpcLevelVoid;
660 pCallbacks->pfnReleaseSpinLockFromDpcLevel = vboxReleaseSpinLockFromDpcLevelVoid;
661 }
662
663 pCallbacks->pfnAllocatePool = (PFNALLOCATEPOOL)(pConfigInfo->VideoPortGetProcAddress)
664 (PrimaryExtension,
665 (PUCHAR)"VideoPortAllocatePool");
666 Assert(pCallbacks->pfnAllocatePool);
667
668 pCallbacks->pfnFreePool = (PFNFREEPOOL)(pConfigInfo->VideoPortGetProcAddress)
669 (PrimaryExtension,
670 (PUCHAR)"VideoPortFreePool");
671 Assert(pCallbacks->pfnFreePool);
672
673 if(pCallbacks->pfnAllocatePool
674 && pCallbacks->pfnFreePool)
675 {
676 pCallbacks->fSupportedTypes |= VBOXVIDEOPORTPROCS_POOL;
677 }
678 else
679 {
680 pCallbacks->pfnAllocatePool = vboxAllocatePoolVoid;
681 pCallbacks->pfnFreePool = vboxFreePoolVoid;
682 }
683
684 pCallbacks->pfnQueueDpc = (PFNQUEUEDPC)(pConfigInfo->VideoPortGetProcAddress)
685 (PrimaryExtension,
686 (PUCHAR)"VideoPortQueueDpc");
687 Assert(pCallbacks->pfnQueueDpc);
688
689 if(pCallbacks->pfnQueueDpc)
690 {
691 pCallbacks->fSupportedTypes |= VBOXVIDEOPORTPROCS_DPC;
692 }
693 else
694 {
695 pCallbacks->pfnQueueDpc = vboxQueueDpcVoid;
696 }
697
698#ifdef DEBUG_misha
699 Assert(pCallbacks->fSupportedTypes & VBOXVIDEOPORTPROCS_EVENT);
700 Assert(pCallbacks->fSupportedTypes & VBOXVIDEOPORTPROCS_SPINLOCK);
701#endif
702}
703#endif
704
705/**
706 * Helper function to register secondary displays (DualView). Note that this will not
707 * be available on pre-XP versions, and some editions on XP will fail because they are
708 * intentionally crippled.
709 *
710 * HGSMI variant is a bit different because it uses only HGSMI interface (VBVA channel)
711 * to talk to the host.
712 */
713VOID VBoxSetupDisplaysHGSMI(PDEVICE_EXTENSION PrimaryExtension,
714#ifndef VBOXWDDM
715 PVIDEO_PORT_CONFIG_INFO pConfigInfo,
716#endif
717 ULONG AdapterMemorySize)
718{
719 VP_STATUS rc = NO_ERROR;
720
721 dprintf(("VBoxVideo::VBoxSetupDisplays: PrimaryExtension = %p\n",
722 PrimaryExtension));
723
724 /* Preinitialize the primary extension.
725 * Note: bVBoxVideoSupported is set to FALSE, because HGSMI is active instead.
726 */
727 PrimaryExtension->pNext = NULL;
728#ifndef VBOXWDDM
729 PrimaryExtension->pPrimary = PrimaryExtension;
730 PrimaryExtension->iDevice = 0;
731 PrimaryExtension->ulFrameBufferOffset = 0;
732 PrimaryExtension->ulFrameBufferSize = 0;
733#endif
734 PrimaryExtension->u.primary.ulVbvaEnabled = 0;
735 PrimaryExtension->u.primary.bVBoxVideoSupported = FALSE;
736#ifndef VBOXWDDM
737 PrimaryExtension->u.primary.cDisplays = 1;
738#endif
739 PrimaryExtension->u.primary.cbVRAM = AdapterMemorySize;
740 PrimaryExtension->u.primary.cbMiniportHeap = 0;
741 PrimaryExtension->u.primary.pvMiniportHeap = NULL;
742 PrimaryExtension->u.primary.pvAdapterInformation = NULL;
743 PrimaryExtension->u.primary.ulMaxFrameBufferSize = 0;
744 PrimaryExtension->u.primary.bHGSMI = VBoxHGSMIIsSupported (PrimaryExtension);
745 VBoxVideoCmnMemZero(&PrimaryExtension->u.primary.areaHostHeap, sizeof(HGSMIAREA));
746 VBoxVideoCmnMemZero(&PrimaryExtension->areaDisplay, sizeof(HGSMIAREA));
747
748 if (PrimaryExtension->u.primary.IOPortGuest == 0)
749 {
750 PrimaryExtension->u.primary.bHGSMI = false;
751 }
752
753 if (PrimaryExtension->u.primary.bHGSMI)
754 {
755 /* Map the adapter information. It will be needed for HGSMI IO. */
756 rc = VBoxMapAdapterMemory (PrimaryExtension,
757 &PrimaryExtension->u.primary.pvAdapterInformation,
758 PrimaryExtension->u.primary.cbVRAM - VBVA_ADAPTER_INFORMATION_SIZE,
759 VBVA_ADAPTER_INFORMATION_SIZE
760 );
761 if (rc != NO_ERROR)
762 {
763 dprintf(("VBoxVideo::VBoxSetupDisplays: VBoxMapAdapterMemory pvAdapterInfoirrmation failed rc = %d\n",
764 rc));
765
766 PrimaryExtension->u.primary.bHGSMI = FALSE;
767 }
768 else
769 {
770 /* Setup a HGSMI heap within the adapter information area. */
771 rc = HGSMIHeapSetup (&PrimaryExtension->u.primary.hgsmiAdapterHeap,
772 PrimaryExtension->u.primary.pvAdapterInformation,
773 VBVA_ADAPTER_INFORMATION_SIZE - sizeof(HGSMIHOSTFLAGS),
774 PrimaryExtension->u.primary.cbVRAM - VBVA_ADAPTER_INFORMATION_SIZE,
775 false /*fOffsetBased*/);
776
777 if (RT_FAILURE (rc))
778 {
779 dprintf(("VBoxVideo::VBoxSetupDisplays: HGSMIHeapSetup failed rc = %d\n",
780 rc));
781
782 PrimaryExtension->u.primary.bHGSMI = FALSE;
783 }
784 else
785 {
786 PrimaryExtension->u.primary.pHostFlags = (HGSMIHOSTFLAGS*)(((uint8_t*)PrimaryExtension->u.primary.pvAdapterInformation)
787 + VBVA_ADAPTER_INFORMATION_SIZE - sizeof(HGSMIHOSTFLAGS));
788 }
789 }
790 }
791
792 /* Setup the host heap and the adapter memory. */
793 if (PrimaryExtension->u.primary.bHGSMI)
794 {
795 /* The miniport heap is used for the host buffers. */
796 ULONG cbMiniportHeap = 0;
797 vboxQueryConfHGSMI (PrimaryExtension, VBOX_VBVA_CONF32_HOST_HEAP_SIZE, &cbMiniportHeap);
798
799 if (cbMiniportHeap != 0)
800 {
801 /* Do not allow too big heap. No more than 25% of VRAM is allowed. */
802 ULONG cbMiniportHeapMaxSize = AdapterMemorySize / 4;
803
804 if (cbMiniportHeapMaxSize >= VBVA_ADAPTER_INFORMATION_SIZE)
805 {
806 cbMiniportHeapMaxSize -= VBVA_ADAPTER_INFORMATION_SIZE;
807 }
808
809 if (cbMiniportHeap > cbMiniportHeapMaxSize)
810 {
811 cbMiniportHeap = cbMiniportHeapMaxSize;
812 }
813
814 /* Round up to 4096 bytes. */
815 PrimaryExtension->u.primary.cbMiniportHeap = (cbMiniportHeap + 0xFFF) & ~0xFFF;
816
817 dprintf(("VBoxVideo::VBoxSetupDisplays: cbMiniportHeap = 0x%08X, PrimaryExtension->u.primary.cbMiniportHeap = 0x%08X, cbMiniportHeapMaxSize = 0x%08X\n",
818 cbMiniportHeap, PrimaryExtension->u.primary.cbMiniportHeap, cbMiniportHeapMaxSize));
819
820 /* Map the heap region.
821 *
822 * Note: the heap will be used for the host buffers submitted to the guest.
823 * The miniport driver is responsible for reading FIFO and notifying
824 * display drivers.
825 */
826 rc = VBoxMapAdapterMemory (PrimaryExtension,
827 &PrimaryExtension->u.primary.pvMiniportHeap,
828 PrimaryExtension->u.primary.cbVRAM
829 - VBVA_ADAPTER_INFORMATION_SIZE
830 - PrimaryExtension->u.primary.cbMiniportHeap,
831 PrimaryExtension->u.primary.cbMiniportHeap
832 );
833 if (rc != NO_ERROR)
834 {
835 PrimaryExtension->u.primary.pvMiniportHeap = NULL;
836 PrimaryExtension->u.primary.cbMiniportHeap = 0;
837 PrimaryExtension->u.primary.bHGSMI = FALSE;
838 }
839 else
840 {
841 HGSMIOFFSET offBase = PrimaryExtension->u.primary.cbVRAM
842 - VBVA_ADAPTER_INFORMATION_SIZE
843 - PrimaryExtension->u.primary.cbMiniportHeap;
844
845 /* Init the host hap area. Buffers from the host will be placed there. */
846 HGSMIAreaInitialize (&PrimaryExtension->u.primary.areaHostHeap,
847 PrimaryExtension->u.primary.pvMiniportHeap,
848 PrimaryExtension->u.primary.cbMiniportHeap,
849 offBase);
850 }
851 }
852 else
853 {
854 /* Host has not requested a heap. */
855 PrimaryExtension->u.primary.pvMiniportHeap = NULL;
856 PrimaryExtension->u.primary.cbMiniportHeap = 0;
857 }
858 }
859
860 /* Check whether the guest supports multimonitors. */
861 if (PrimaryExtension->u.primary.bHGSMI)
862 {
863#ifndef VBOXWDDM
864 typedef VP_STATUS (*PFNCREATESECONDARYDISPLAY)(PVOID, PVOID *, ULONG);
865 PFNCREATESECONDARYDISPLAY pfnCreateSecondaryDisplay = NULL;
866
867 /* Dynamically query the VideoPort import to be binary compatible across Windows versions */
868 if (vboxQueryWinVersion() > WINNT4)
869 {
870 /* This bluescreens on NT4, hence the above version check */
871 pfnCreateSecondaryDisplay = (PFNCREATESECONDARYDISPLAY)(pConfigInfo->VideoPortGetProcAddress)
872 (PrimaryExtension,
873 (PUCHAR)"VideoPortCreateSecondaryDisplay");
874 }
875
876 if (pfnCreateSecondaryDisplay != NULL)
877#endif
878 {
879 /* Query the configured number of displays. */
880 ULONG cDisplays = 0;
881 vboxQueryConfHGSMI (PrimaryExtension, VBOX_VBVA_CONF32_MONITOR_COUNT, &cDisplays);
882
883 dprintf(("VBoxVideo::VBoxSetupDisplays: cDisplays = %d\n",
884 cDisplays));
885
886 if (cDisplays == 0 || cDisplays > VBOX_VIDEO_MAX_SCREENS)
887 {
888 /* Host reported some bad value. Continue in the 1 screen mode. */
889 cDisplays = 1;
890 }
891
892#ifndef VBOXWDDM
893 PDEVICE_EXTENSION pPrev = PrimaryExtension;
894
895 ULONG iDisplay;
896 for (iDisplay = 1; iDisplay < cDisplays; iDisplay++)
897 {
898 PDEVICE_EXTENSION SecondaryExtension = NULL;
899 rc = pfnCreateSecondaryDisplay (PrimaryExtension, (PVOID*)&SecondaryExtension, VIDEO_DUALVIEW_REMOVABLE);
900
901 dprintf(("VBoxVideo::VBoxSetupDisplays: VideoPortCreateSecondaryDisplay returned %#x, SecondaryExtension = %p\n",
902 rc, SecondaryExtension));
903
904 if (rc != NO_ERROR)
905 {
906 break;
907 }
908
909 SecondaryExtension->pNext = NULL;
910 SecondaryExtension->pPrimary = PrimaryExtension;
911 SecondaryExtension->iDevice = iDisplay;
912 SecondaryExtension->ulFrameBufferOffset = 0;
913 SecondaryExtension->ulFrameBufferSize = 0;
914 SecondaryExtension->u.secondary.bEnabled = FALSE;
915
916 /* Update the list pointers. */
917 pPrev->pNext = SecondaryExtension;
918 pPrev = SecondaryExtension;
919
920 /* Take the successfully created display into account. */
921 PrimaryExtension->u.primary.cDisplays++;
922 }
923#else
924 /* simply store the number of monitors, we will deal with VidPN stuff later */
925 PrimaryExtension->cSources = cDisplays;
926#endif
927 }
928
929 /* Failure to create secondary displays is not fatal */
930 rc = NO_ERROR;
931 }
932
933#ifndef VBOXWDDM
934 /* Now when the number of monitors is known and extensions are created,
935 * calculate the layout of framebuffers.
936 */
937 VBoxComputeFrameBufferSizes (PrimaryExtension);
938#endif
939
940 if (PrimaryExtension->u.primary.bHGSMI)
941 {
942 /* Setup the information for the host. */
943 rc = vboxSetupAdapterInfoHGSMI (PrimaryExtension);
944
945 if (RT_FAILURE (rc))
946 {
947 PrimaryExtension->u.primary.bHGSMI = FALSE;
948 }
949 }
950
951#ifdef VBOXWDDM
952 if (PrimaryExtension->u.primary.bHGSMI)
953 {
954 ULONG ulAvailable = PrimaryExtension->u.primary.cbVRAM
955 - PrimaryExtension->u.primary.cbMiniportHeap
956 - VBVA_ADAPTER_INFORMATION_SIZE;
957
958 ULONG ulSize = ulAvailable / 2;
959
960 if (ulSize > VBOXWDDM_C_VDMA_BUFFER_SIZE)
961 ulSize = VBOXWDDM_C_VDMA_BUFFER_SIZE;
962
963 /* Align down to 4096 bytes. */
964 ulSize &= ~0xFFF;
965 ULONG offset = ulAvailable - ulSize;
966
967 Assert(!(offset & 0xFFF));
968
969 rc = vboxVdmaCreate (PrimaryExtension, &PrimaryExtension->u.primary.Vdma, offset, ulSize);
970 AssertRC(rc);
971 if (RT_SUCCESS(rc))
972 {
973 /* can enable it right away since the host does not need any screen/FB info
974 * for basic DMA functionality */
975 rc = vboxVdmaEnable(PrimaryExtension, &PrimaryExtension->u.primary.Vdma);
976 AssertRC(rc);
977 if (RT_FAILURE(rc))
978 vboxVdmaDestroy(PrimaryExtension, &PrimaryExtension->u.primary.Vdma);
979 }
980
981#ifdef VBOXWDDM_RENDER_FROM_SHADOW
982 if (RT_SUCCESS(rc))
983 {
984 ulAvailable = offset;
985 ulSize = ulAvailable / 2;
986 ulSize /= PrimaryExtension->cSources;
987 Assert(ulSize > VBVA_MIN_BUFFER_SIZE);
988 if (ulSize > VBVA_MIN_BUFFER_SIZE)
989 {
990 ULONG ulRatio = ulSize/VBVA_MIN_BUFFER_SIZE;
991 ulRatio >>= 4; /* /= 16; */
992 if (ulRatio)
993 ulSize = VBVA_MIN_BUFFER_SIZE * ulRatio;
994 else
995 ulSize = VBVA_MIN_BUFFER_SIZE;
996 }
997 else
998 {
999 /* todo: ?? */
1000 }
1001
1002 ulSize &= ~0xFFF;
1003 Assert(ulSize);
1004
1005 Assert(ulSize * PrimaryExtension->cSources < ulAvailable);
1006
1007 for (int i = PrimaryExtension->cSources-1; i >= 0; --i)
1008 {
1009 offset -= ulSize;
1010 rc = vboxVbvaCreate(PrimaryExtension, &PrimaryExtension->aSources[i].Vbva, offset, ulSize, i);
1011 AssertRC(rc);
1012 if (RT_SUCCESS(rc))
1013 {
1014 rc = vboxVbvaEnable(PrimaryExtension, &PrimaryExtension->aSources[i].Vbva);
1015 AssertRC(rc);
1016 if (RT_FAILURE(rc))
1017 {
1018 /* @todo: de-initialize */
1019 }
1020 }
1021 }
1022 }
1023#endif
1024
1025 if (RT_FAILURE(rc))
1026 PrimaryExtension->u.primary.bHGSMI = FALSE;
1027 }
1028#endif
1029
1030 if (!PrimaryExtension->u.primary.bHGSMI)
1031 {
1032 /* Unmap the memory if VBoxVideo is not supported. */
1033 VBoxUnmapAdapterMemory (PrimaryExtension, &PrimaryExtension->u.primary.pvMiniportHeap, PrimaryExtension->u.primary.cbMiniportHeap);
1034 VBoxUnmapAdapterMemory (PrimaryExtension, &PrimaryExtension->u.primary.pvAdapterInformation, VBVA_ADAPTER_INFORMATION_SIZE);
1035
1036 HGSMIHeapDestroy (&PrimaryExtension->u.primary.hgsmiAdapterHeap);
1037 }
1038
1039 if (PrimaryExtension->u.primary.bHGSMI)
1040 {
1041 VBoxVideoCmnSpinLockCreate(PrimaryExtension, &PrimaryExtension->u.primary.pSynchLock);
1042 }
1043
1044 dprintf(("VBoxVideo::VBoxSetupDisplays: finished\n"));
1045}
1046
1047
1048/*
1049 * Send the pointer shape to the host.
1050 */
1051typedef struct _MOUSEPOINTERSHAPECTX
1052{
1053 VIDEO_POINTER_ATTRIBUTES *pPointerAttr;
1054 uint32_t cbData;
1055 int32_t i32Result;
1056} MOUSEPOINTERSHAPECTX;
1057
1058static int vbvaInitMousePointerShape (PDEVICE_EXTENSION PrimaryExtension, void *pvContext, void *pvData)
1059{
1060 NOREF (PrimaryExtension);
1061
1062 MOUSEPOINTERSHAPECTX *pCtx = (MOUSEPOINTERSHAPECTX *)pvContext;
1063 VBVAMOUSEPOINTERSHAPE *p = (VBVAMOUSEPOINTERSHAPE *)pvData;
1064
1065 /* Will be updated by the host. */
1066 p->i32Result = VINF_SUCCESS;
1067
1068 /* We have our custom flags in the field */
1069 p->fu32Flags = pCtx->pPointerAttr->Enable & 0x0000FFFF;
1070
1071 p->u32HotX = (pCtx->pPointerAttr->Enable >> 16) & 0xFF;
1072 p->u32HotY = (pCtx->pPointerAttr->Enable >> 24) & 0xFF;
1073 p->u32Width = pCtx->pPointerAttr->Width;
1074 p->u32Height = pCtx->pPointerAttr->Height;
1075
1076 if (p->fu32Flags & VBOX_MOUSE_POINTER_SHAPE)
1077 {
1078 /* If shape is supplied, then alway create the pointer visible.
1079 * See comments in 'vboxUpdatePointerShape'
1080 */
1081 p->fu32Flags |= VBOX_MOUSE_POINTER_VISIBLE;
1082
1083 /* Copy the actual pointer data. */
1084 memcpy (p->au8Data, pCtx->pPointerAttr->Pixels, pCtx->cbData);
1085 }
1086
1087 return VINF_SUCCESS;
1088}
1089
1090static int vbvaFinalizeMousePointerShape (PDEVICE_EXTENSION PrimaryExtension, void *pvContext, void *pvData)
1091{
1092 NOREF (PrimaryExtension);
1093
1094 MOUSEPOINTERSHAPECTX *pCtx = (MOUSEPOINTERSHAPECTX *)pvContext;
1095 VBVAMOUSEPOINTERSHAPE *p = (VBVAMOUSEPOINTERSHAPE *)pvData;
1096
1097 pCtx->i32Result = p->i32Result;
1098
1099 return VINF_SUCCESS;
1100}
1101
1102BOOLEAN vboxUpdatePointerShape (PDEVICE_EXTENSION DeviceExtension,
1103 PVIDEO_POINTER_ATTRIBUTES pointerAttr,
1104 uint32_t cbLength)
1105{
1106#ifndef VBOXWDDM
1107 PDEVICE_EXTENSION PrimaryExtension = DeviceExtension->pPrimary;
1108
1109 /* In multimonitor case the HW mouse pointer is the same on all screens,
1110 * and Windows calls each display driver with the same pointer data: visible for
1111 * the screen where the pointer is and invisible for other screens.
1112 *
1113 * This driver passes the shape to the host only from primary screen and makes
1114 * the pointer always visible (in vbvaInitMousePointerShape).
1115 *
1116 * The simple solution makes it impossible to create the shape and keep the mouse
1117 * pointer invisible. New shapes will be created visible.
1118 * But:
1119 * 1) VBox frontends actually ignore the visibility flag if VBOX_MOUSE_POINTER_SHAPE
1120 * is set and always create new pointers visible.
1121 * 2) Windows uses DrvMovePointer to hide the pointer, which will still work.
1122 */
1123
1124 if (DeviceExtension->iDevice != PrimaryExtension->iDevice)
1125 {
1126 dprintf(("vboxUpdatePointerShape: ignore non primary device %d(%d)\n",
1127 DeviceExtension->iDevice, PrimaryExtension->iDevice));
1128 /* Success. */
1129 return TRUE;
1130 }
1131#else
1132 PDEVICE_EXTENSION PrimaryExtension = DeviceExtension;
1133#endif
1134
1135 uint32_t cbData = 0;
1136
1137 if (pointerAttr->Enable & VBOX_MOUSE_POINTER_SHAPE)
1138 {
1139 /* Size of the pointer data: sizeof (AND mask) + sizeof (XOR_MASK) */
1140 cbData = ((((pointerAttr->Width + 7) / 8) * pointerAttr->Height + 3) & ~3)
1141 + pointerAttr->Width * 4 * pointerAttr->Height;
1142 }
1143
1144#ifndef DEBUG_misha
1145 dprintf(("vboxUpdatePointerShape: cbData %d, %dx%d\n",
1146 cbData, pointerAttr->Width, pointerAttr->Height));
1147#endif
1148
1149 if (cbData > cbLength - sizeof(VIDEO_POINTER_ATTRIBUTES))
1150 {
1151 dprintf(("vboxUpdatePointerShape: calculated pointer data size is too big (%d bytes, limit %d)\n",
1152 cbData, cbLength - sizeof(VIDEO_POINTER_ATTRIBUTES)));
1153 return FALSE;
1154 }
1155
1156 MOUSEPOINTERSHAPECTX ctx;
1157
1158 ctx.pPointerAttr = pointerAttr;
1159 ctx.cbData = cbData;
1160 ctx.i32Result = VERR_NOT_SUPPORTED;
1161
1162 int rc = vboxCallVBVA (PrimaryExtension,
1163 VBVA_MOUSE_POINTER_SHAPE,
1164 sizeof (VBVAMOUSEPOINTERSHAPE) + cbData,
1165 vbvaInitMousePointerShape,
1166 vbvaFinalizeMousePointerShape,
1167 &ctx);
1168#ifndef DEBUG_misha
1169 dprintf(("VBoxVideo::vboxMousePointerShape: rc %d, i32Result = %d\n", rc, ctx.i32Result));
1170#endif
1171
1172 return RT_SUCCESS(rc) && RT_SUCCESS(ctx.i32Result);
1173}
1174
1175typedef struct _VBVAMINIPORT_CHANNELCONTEXT
1176{
1177 PFNHGSMICHANNELHANDLER pfnChannelHandler;
1178 void *pvChannelHandler;
1179}VBVAMINIPORT_CHANNELCONTEXT;
1180
1181typedef struct _VBVADISP_CHANNELCONTEXT
1182{
1183 struct _VBVAHOSTCMD * pFirstCmd;
1184 struct _VBVAHOSTCMD * pLastCmd;
1185 VBOXVCMNSPIN_LOCK pSynchLock;
1186#ifdef DEBUG
1187 int cCmds;
1188#endif
1189 bool bValid;
1190}VBVADISP_CHANNELCONTEXT;
1191
1192#ifdef DEBUG
1193void dbgCheckListLocked(const VBVADISP_CHANNELCONTEXT *pList, struct _VBVAHOSTCMD * pCmd)
1194{
1195 int counter = 0;
1196 for(struct _VBVAHOSTCMD * pCur = pList->pFirstCmd; pCur; pCur=pCur->u.pNext)
1197 {
1198 Assert(pCur != pCmd);
1199 if(pCur == pList->pLastCmd)
1200 {
1201 Assert(pCur->u.pNext == NULL);
1202 }
1203 if(pCur->u.pNext == NULL)
1204 {
1205 Assert(pCur == pList->pLastCmd);
1206 }
1207 counter++;
1208 }
1209
1210 Assert(counter == pList->cCmds);
1211}
1212
1213void dbgCheckList(PDEVICE_EXTENSION PrimaryExtension, VBVADISP_CHANNELCONTEXT *pList, struct _VBVAHOSTCMD * pCmd)
1214{
1215 VBOXVCMNIRQL oldIrql;
1216 VBoxVideoCmnSpinLockAcquire(PrimaryExtension, &pList->pSynchLock, &oldIrql);
1217
1218 dbgCheckListLocked(pList, pCmd);
1219
1220 VBoxVideoCmnSpinLockRelease(PrimaryExtension, &pList->pSynchLock, oldIrql);
1221}
1222
1223#define DBG_CHECKLIST_LOCKED(_pl, pc) dbgCheckListLocked(_pl, pc)
1224#define DBG_CHECKLIST(_pe, _pl, pc) dbgCheckList(_pe, _pl, pc)
1225
1226#else
1227#define DBG_CHECKLIST_LOCKED(_pl, pc) do{}while(0)
1228#define DBG_CHECKLIST(_pe, _pl, pc) do{}while(0)
1229#endif
1230
1231
1232typedef struct _VBVA_CHANNELCONTEXTS
1233{
1234 PDEVICE_EXTENSION PrimaryExtension;
1235 uint32_t cUsed;
1236 uint32_t cContexts;
1237 VBVAMINIPORT_CHANNELCONTEXT mpContext;
1238 VBVADISP_CHANNELCONTEXT aContexts[1];
1239}VBVA_CHANNELCONTEXTS;
1240
1241static int vboxVBVADeleteChannelContexts(PDEVICE_EXTENSION PrimaryExtension, VBVA_CHANNELCONTEXTS * pContext)
1242{
1243 VBoxVideoCmnMemFree(PrimaryExtension,pContext);
1244 return VINF_SUCCESS;
1245}
1246
1247static int vboxVBVACreateChannelContexts(PDEVICE_EXTENSION PrimaryExtension, VBVA_CHANNELCONTEXTS ** ppContext)
1248{
1249#ifndef VBOXWDDM
1250 uint32_t cDisplays = (uint32_t)PrimaryExtension->u.primary.cDisplays;
1251#else
1252 uint32_t cDisplays = (uint32_t)PrimaryExtension->cSources;
1253#endif
1254 const size_t size = RT_OFFSETOF(VBVA_CHANNELCONTEXTS, aContexts[cDisplays]);
1255 VBVA_CHANNELCONTEXTS * pContext = (VBVA_CHANNELCONTEXTS*)VBoxVideoCmnMemAllocNonPaged(PrimaryExtension, size, MEM_TAG);
1256 if(pContext)
1257 {
1258 memset(pContext, 0, size);
1259 pContext->cContexts = cDisplays;
1260 pContext->PrimaryExtension = PrimaryExtension;
1261 *ppContext = pContext;
1262 return VINF_SUCCESS;
1263 }
1264 return VERR_GENERAL_FAILURE;
1265}
1266
1267static VBVADISP_CHANNELCONTEXT* vboxVBVAFindHandlerInfo(VBVA_CHANNELCONTEXTS *pCallbacks, int iId)
1268{
1269 if(iId < 0)
1270 {
1271 return NULL;
1272 }
1273 else if(pCallbacks->cContexts > (uint32_t)iId)
1274 {
1275 return &pCallbacks->aContexts[iId];
1276 }
1277 return NULL;
1278}
1279
1280#ifndef VBOXWDDM
1281DECLCALLBACK(void) hgsmiHostCmdComplete (HVBOXVIDEOHGSMI hHGSMI, struct _VBVAHOSTCMD * pCmd)
1282{
1283 PDEVICE_EXTENSION PrimaryExtension = ((PDEVICE_EXTENSION)hHGSMI)->pPrimary;
1284 HGSMIHostCmdComplete (PrimaryExtension, pCmd);
1285}
1286
1287DECLCALLBACK(int) hgsmiHostCmdRequest (HVBOXVIDEOHGSMI hHGSMI, uint8_t u8Channel, struct _VBVAHOSTCMD ** ppCmd)
1288{
1289// if(display < 0)
1290// return VERR_INVALID_PARAMETER;
1291 if(!ppCmd)
1292 return VERR_INVALID_PARAMETER;
1293
1294 PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)hHGSMI;
1295 PDEVICE_EXTENSION PrimaryExtension = pDevExt->pPrimary;
1296
1297 /* pick up the host commands */
1298 VBoxVideoHGSMIDpc(PrimaryExtension, NULL);
1299
1300 HGSMICHANNEL * pChannel = HGSMIChannelFindById (&PrimaryExtension->u.primary.channels, u8Channel);
1301 if(pChannel)
1302 {
1303 VBVA_CHANNELCONTEXTS * pContexts = (VBVA_CHANNELCONTEXTS *)pChannel->handler.pvHandler;
1304 VBVADISP_CHANNELCONTEXT *pDispContext = vboxVBVAFindHandlerInfo(pContexts, pDevExt->iDevice);
1305 Assert(pDispContext);
1306 if(pDispContext)
1307 {
1308 UCHAR oldIrql;
1309 VBoxVideoCmnSpinLockAcquire(PrimaryExtension, &pDispContext->pSynchLock, &oldIrql);
1310
1311 DBG_CHECKLIST_LOCKED(pDispContext, NULL);
1312
1313 *ppCmd = pDispContext->pFirstCmd;
1314 pDispContext->pFirstCmd = NULL;
1315 pDispContext->pLastCmd = NULL;
1316#ifdef DEBUG
1317 pDispContext->cCmds = 0;
1318#endif
1319 VBoxVideoCmnSpinLockRelease(PrimaryExtension, &pDispContext->pSynchLock, oldIrql);
1320
1321 DBG_CHECKLIST(PrimaryExtension, pDispContext, NULL);
1322
1323 return VINF_SUCCESS;
1324 }
1325 }
1326
1327 return VERR_INVALID_PARAMETER;
1328}
1329#endif
1330
1331static DECLCALLBACK(int) vboxVBVAChannelGenericHandler(void *pvHandler, uint16_t u16ChannelInfo, void *pvBuffer, HGSMISIZE cbBuffer)
1332{
1333 VBVA_CHANNELCONTEXTS *pCallbacks = (VBVA_CHANNELCONTEXTS*)pvHandler;
1334// Assert(0);
1335 Assert(cbBuffer > VBVAHOSTCMD_HDRSIZE);
1336 if(cbBuffer > VBVAHOSTCMD_HDRSIZE)
1337 {
1338 VBVAHOSTCMD *pHdr = (VBVAHOSTCMD*)pvBuffer;
1339 Assert(pHdr->iDstID >= 0);
1340 if(pHdr->iDstID >= 0)
1341 {
1342 VBVADISP_CHANNELCONTEXT* pHandler = vboxVBVAFindHandlerInfo(pCallbacks, pHdr->iDstID);
1343 Assert(pHandler && pHandler->bValid);
1344 if(pHandler && pHandler->bValid)
1345 {
1346 VBVAHOSTCMD *pFirst = NULL, *pLast = NULL;
1347 for(VBVAHOSTCMD *pCur = pHdr; pCur; )
1348 {
1349 Assert(!pCur->u.Data);
1350 Assert(!pFirst);
1351 Assert(!pLast);
1352
1353 switch(u16ChannelInfo)
1354 {
1355 case VBVAHG_DISPLAY_CUSTOM:
1356 {
1357 if(pLast)
1358 {
1359 pLast->u.pNext = pCur;
1360 pLast = pCur;
1361 }
1362 else
1363 {
1364 pFirst = pCur;
1365 pLast = pCur;
1366 }
1367 Assert(!pCur->u.Data);
1368 //TODO: use offset here
1369 pCur = pCur->u.pNext;
1370 Assert(!pCur);
1371 Assert(pFirst);
1372 Assert(pFirst == pLast);
1373 break;
1374 }
1375 case VBVAHG_EVENT:
1376 {
1377 VBVAHOSTCMDEVENT *pEventCmd = VBVAHOSTCMD_BODY(pCur, VBVAHOSTCMDEVENT);
1378#ifndef VBOXWDDM
1379 PEVENT pEvent = (PEVENT)pEventCmd->pEvent;
1380 pCallbacks->PrimaryExtension->u.primary.VideoPortProcs.pfnSetEvent(
1381 pCallbacks->PrimaryExtension,
1382 pEvent);
1383#else
1384 PKEVENT pEvent = (PKEVENT)pEventCmd->pEvent;
1385 KeSetEvent(pEvent, 0, FALSE);
1386#endif
1387 }
1388 default:
1389 {
1390 DBG_CHECKLIST(pCallbacks->PrimaryExtension, pHandler, pCur);
1391 Assert(u16ChannelInfo==VBVAHG_EVENT);
1392 Assert(!pCur->u.Data);
1393 //TODO: use offset here
1394 if(pLast)
1395 pLast->u.pNext = pCur->u.pNext;
1396 VBVAHOSTCMD * pNext = pCur->u.pNext;
1397 pCur->u.pNext = NULL;
1398 HGSMIHostCmdComplete(pCallbacks->PrimaryExtension, pCur);
1399 pCur = pNext;
1400 Assert(!pCur);
1401 Assert(!pFirst);
1402 Assert(pFirst == pLast);
1403 break;
1404 }
1405 }
1406 }
1407
1408 DBG_CHECKLIST(pCallbacks->PrimaryExtension, pHandler, pFirst);
1409
1410 /* we do not support lists currently */
1411 Assert(pFirst == pLast);
1412 if(pLast)
1413 {
1414 Assert(pLast->u.pNext == NULL);
1415 }
1416
1417 if(pFirst)
1418 {
1419 Assert(pLast);
1420 UCHAR oldIrql;
1421 VBoxVideoCmnSpinLockAcquire(pCallbacks->PrimaryExtension,
1422 &pHandler->pSynchLock,
1423 &oldIrql);
1424
1425 DBG_CHECKLIST_LOCKED(pHandler, pFirst);
1426
1427 if(pHandler->pLastCmd)
1428 {
1429 pHandler->pLastCmd->u.pNext = pFirst;
1430 Assert(pHandler->pFirstCmd);
1431 }
1432 else
1433 {
1434 Assert(!pHandler->pFirstCmd);
1435 pHandler->pFirstCmd = pFirst;
1436 }
1437 pHandler->pLastCmd = pLast;
1438#ifdef DEBUG
1439 pHandler->cCmds++;
1440#endif
1441 DBG_CHECKLIST_LOCKED(pHandler, NULL);
1442
1443 VBoxVideoCmnSpinLockRelease(pCallbacks->PrimaryExtension,
1444 &pHandler->pSynchLock,
1445 oldIrql);
1446 }
1447 else
1448 {
1449 Assert(!pLast);
1450 }
1451 return VINF_SUCCESS;
1452 }
1453 }
1454 else
1455 {
1456 //TODO: impl
1457// HGSMIMINIPORT_CHANNELCONTEXT *pHandler = vboxVideoHGSMIFindHandler;
1458// if(pHandler && pHandler->pfnChannelHandler)
1459// {
1460// pHandler->pfnChannelHandler(pHandler->pvChannelHandler, u16ChannelInfo, pHdr, cbBuffer);
1461//
1462// return VINF_SUCCESS;
1463// }
1464 }
1465 }
1466 /* no handlers were found, need to complete the command here */
1467 HGSMIHostCmdComplete(pCallbacks->PrimaryExtension, pvBuffer);
1468 return VINF_SUCCESS;
1469}
1470
1471static HGSMICHANNELHANDLER g_OldHandler;
1472
1473int vboxVBVAChannelDisplayEnable(PDEVICE_EXTENSION PrimaryExtension,
1474 int iDisplay, /* negative would mean this is a miniport handler */
1475 uint8_t u8Channel)
1476{
1477 VBVA_CHANNELCONTEXTS * pContexts;
1478 HGSMICHANNEL * pChannel = HGSMIChannelFindById (&PrimaryExtension->u.primary.channels, u8Channel);
1479 if(!pChannel)
1480 {
1481 int rc = vboxVBVACreateChannelContexts(PrimaryExtension, &pContexts);
1482 if(RT_FAILURE(rc))
1483 {
1484 return rc;
1485 }
1486 }
1487 else
1488 {
1489 pContexts = (VBVA_CHANNELCONTEXTS *)pChannel->handler.pvHandler;
1490 }
1491
1492 VBVADISP_CHANNELCONTEXT *pDispContext = vboxVBVAFindHandlerInfo(pContexts, iDisplay);
1493 Assert(pDispContext);
1494 if(pDispContext)
1495 {
1496#ifdef DEBUGVHWASTRICT
1497 Assert(!pDispContext->bValid);
1498#endif
1499 Assert(!pDispContext->pFirstCmd);
1500 Assert(!pDispContext->pLastCmd);
1501 if(!pDispContext->bValid)
1502 {
1503 pDispContext->bValid = true;
1504 pDispContext->pFirstCmd = NULL;
1505 pDispContext->pLastCmd= NULL;
1506#ifdef DEBUG
1507 pDispContext->cCmds = 0;
1508#endif
1509
1510 VBoxVideoCmnSpinLockCreate(PrimaryExtension, &pDispContext->pSynchLock);
1511
1512 int rc = VINF_SUCCESS;
1513 if(!pChannel)
1514 {
1515 rc = HGSMIChannelRegister (&PrimaryExtension->u.primary.channels,
1516 u8Channel,
1517 "VGA Miniport HGSMI channel",
1518 vboxVBVAChannelGenericHandler,
1519 pContexts,
1520 &g_OldHandler);
1521 }
1522
1523 if(RT_SUCCESS(rc))
1524 {
1525 pContexts->cUsed++;
1526 return VINF_SUCCESS;
1527 }
1528 }
1529 }
1530
1531 if(!pChannel)
1532 {
1533 vboxVBVADeleteChannelContexts(PrimaryExtension, pContexts);
1534 }
1535
1536 return VERR_GENERAL_FAILURE;
1537}
1538
1539/** @todo Mouse pointer position to be read from VMMDev memory, address of the memory region
1540 * can be queried from VMMDev via an IOCTL. This VMMDev memory region will contain
1541 * host information which is needed by the guest.
1542 *
1543 * Reading will not cause a switch to the host.
1544 *
1545 * Have to take into account:
1546 * * synchronization: host must write to the memory only from EMT,
1547 * large structures must be read under flag, which tells the host
1548 * that the guest is currently reading the memory (OWNER flag?).
1549 * * guest writes: may be allocate a page for the host info and make
1550 * the page readonly for the guest.
1551 * * the information should be available only for additions drivers.
1552 * * VMMDev additions driver will inform the host which version of the info it expects,
1553 * host must support all versions.
1554 *
1555 */
1556
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