VirtualBox

source: vbox/trunk/src/VBox/Main/DisplayImpl.cpp@ 4041

Last change on this file since 4041 was 4027, checked in by vboxsync, 17 years ago

Direct draw heap and miniport heap memory reservation for Windows guest additions.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 74.2 KB
Line 
1/** @file
2 *
3 * VirtualBox COM class implementation
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22#include "DisplayImpl.h"
23#include "FramebufferImpl.h"
24#include "ConsoleImpl.h"
25#include "ConsoleVRDPServer.h"
26#include "VMMDev.h"
27
28#include "Logging.h"
29
30#include <iprt/semaphore.h>
31#include <iprt/thread.h>
32#include <iprt/asm.h>
33
34#include <VBox/pdmdrv.h>
35#ifdef DEBUG /* for VM_ASSERT_EMT(). */
36# include <VBox/vm.h>
37#endif
38
39/**
40 * Display driver instance data.
41 */
42typedef struct DRVMAINDISPLAY
43{
44 /** Pointer to the display object. */
45 Display *pDisplay;
46 /** Pointer to the driver instance structure. */
47 PPDMDRVINS pDrvIns;
48 /** Pointer to the keyboard port interface of the driver/device above us. */
49 PPDMIDISPLAYPORT pUpPort;
50 /** Our display connector interface. */
51 PDMIDISPLAYCONNECTOR Connector;
52} DRVMAINDISPLAY, *PDRVMAINDISPLAY;
53
54/** Converts PDMIDISPLAYCONNECTOR pointer to a DRVMAINDISPLAY pointer. */
55#define PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface) ( (PDRVMAINDISPLAY) ((uintptr_t)pInterface - RT_OFFSETOF(DRVMAINDISPLAY, Connector)) )
56
57#ifdef DEBUG_sunlover
58static STAMPROFILE StatDisplayRefresh;
59static int stam = 0;
60#endif /* DEBUG_sunlover */
61
62// constructor / destructor
63/////////////////////////////////////////////////////////////////////////////
64
65HRESULT Display::FinalConstruct()
66{
67 mpVbvaMemory = NULL;
68 mfVideoAccelEnabled = false;
69 mfVideoAccelVRDP = false;
70 mfu32SupportedOrders = 0;
71 mcVideoAccelVRDPRefs = 0;
72
73 mpPendingVbvaMemory = NULL;
74 mfPendingVideoAccelEnable = false;
75
76 mfMachineRunning = false;
77
78 mpu8VbvaPartial = NULL;
79 mcbVbvaPartial = 0;
80
81 mParent = NULL;
82 mpDrv = NULL;
83 mpVMMDev = NULL;
84 mfVMMDevInited = false;
85 RTSemEventMultiCreate(&mUpdateSem);
86
87 mLastAddress = NULL;
88 mLastBytesPerLine = 0;
89 mLastBitsPerPixel = 0,
90 mLastWidth = 0;
91 mLastHeight = 0;
92
93// mu32ResizeStatus = ResizeStatus_Void;
94
95 return S_OK;
96}
97
98void Display::FinalRelease()
99{
100 if (isReady())
101 uninit();
102}
103
104// public initializer/uninitializer for internal purposes only
105/////////////////////////////////////////////////////////////////////////////
106
107/**
108 * Initializes the display object.
109 *
110 * @returns COM result indicator
111 * @param parent handle of our parent object
112 * @param qemuConsoleData address of common console data structure
113 */
114HRESULT Display::init (Console *parent)
115{
116 LogFlowFunc (("isReady=%d", isReady()));
117
118 ComAssertRet (parent, E_INVALIDARG);
119
120 AutoLock alock (this);
121 ComAssertRet (!isReady(), E_UNEXPECTED);
122
123 mParent = parent;
124
125 /* reset the event sems */
126 RTSemEventMultiReset(mUpdateSem);
127
128 // by default, we have an internal framebuffer which is
129 // NULL, i.e. a black hole for no display output
130// mFramebuffer = 0;
131 mInternalFramebuffer = true;
132 mFramebufferOpened = false;
133 mSupportedAccelOps = 0;
134
135 ULONG ul;
136 mParent->machine()->COMGETTER(MonitorCount)(&ul);
137 mcMonitors = ul;
138
139 for (ul = 0; ul < mcMonitors; ul++)
140 {
141 maFramebuffers[ul].u32Offset = 0;
142 maFramebuffers[ul].u32MaxFramebufferSize = 0;
143 maFramebuffers[ul].u32InformationSize = 0;
144
145 maFramebuffers[ul].pFramebuffer = NULL;
146
147 maFramebuffers[ul].xOrigin = 0;
148 maFramebuffers[ul].yOrigin = 0;
149
150 maFramebuffers[ul].w = 0;
151 maFramebuffers[ul].h = 0;
152
153 maFramebuffers[ul].pHostEvents = NULL;
154
155 maFramebuffers[ul].u32ResizeStatus = ResizeStatus_Void;
156
157 maFramebuffers[ul].fDefaultFormat = false;
158
159 memset (&maFramebuffers[ul].dirtyRect, 0 , sizeof (maFramebuffers[ul].dirtyRect));
160 }
161
162 mParent->RegisterCallback(this);
163
164 setReady (true);
165 return S_OK;
166}
167
168/**
169 * Uninitializes the instance and sets the ready flag to FALSE.
170 * Called either from FinalRelease() or by the parent when it gets destroyed.
171 */
172void Display::uninit()
173{
174 LogFlowFunc (("isReady=%d\n", isReady()));
175
176 AutoLock alock (this);
177 AssertReturn (isReady(), (void) 0);
178
179// mFramebuffer.setNull();
180 ULONG ul;
181 for (ul = 0; ul < mcMonitors; ul++)
182 {
183 maFramebuffers[ul].pFramebuffer = NULL;
184 }
185
186 RTSemEventMultiDestroy(mUpdateSem);
187
188 if (mParent)
189 {
190 mParent->UnregisterCallback(this);
191 }
192
193 if (mpDrv)
194 mpDrv->pDisplay = NULL;
195 mpDrv = NULL;
196 mpVMMDev = NULL;
197 mfVMMDevInited = true;
198
199 setReady (false);
200}
201
202// IConsoleCallback method
203STDMETHODIMP Display::OnStateChange(MachineState_T machineState)
204{
205 if (machineState == MachineState_Running)
206 {
207 LogFlowFunc (("Machine running\n"));
208
209 mfMachineRunning = true;
210 }
211 else
212 {
213 mfMachineRunning = false;
214 }
215 return S_OK;
216}
217
218// public methods only for internal purposes
219/////////////////////////////////////////////////////////////////////////////
220
221/**
222 * @thread EMT
223 */
224static int callFramebufferResize (IFramebuffer *pFramebuffer, unsigned uScreenId,
225 ULONG pixelFormat, void *pvVRAM,
226 uint32_t bpp, uint32_t cbLine,
227 int w, int h)
228{
229 Assert (pFramebuffer);
230
231 /* Call the framebuffer to try and set required pixelFormat. */
232 BOOL finished = TRUE;
233
234 pFramebuffer->RequestResize (uScreenId, pixelFormat, (BYTE *) pvVRAM,
235 bpp, cbLine, w, h, &finished);
236
237 if (!finished)
238 {
239 LogFlowFunc (("External framebuffer wants us to wait!\n"));
240 return VINF_VGA_RESIZE_IN_PROGRESS;
241 }
242
243 return VINF_SUCCESS;
244}
245
246/**
247 * Handles display resize event.
248 * Disables access to VGA device;
249 * calls the framebuffer RequestResize method;
250 * if framebuffer resizes synchronously,
251 * updates the display connector data and enables access to the VGA device.
252 *
253 * @param w New display width
254 * @param h New display height
255 *
256 * @thread EMT
257 */
258int Display::handleDisplayResize (unsigned uScreenId, uint32_t bpp, void *pvVRAM,
259 uint32_t cbLine, int w, int h)
260{
261 LogRel (("Display::handleDisplayResize(): uScreenId = %d, pvVRAM=%p "
262 "w=%d h=%d bpp=%d cbLine=0x%X\n",
263 uScreenId, pvVRAM, w, h, bpp, cbLine));
264
265 /* If there is no framebuffer, this call is not interesting. */
266 if ( uScreenId >= mcMonitors
267 || maFramebuffers[uScreenId].pFramebuffer.isNull())
268 {
269 return VINF_SUCCESS;
270 }
271
272 mLastAddress = pvVRAM;
273 mLastBytesPerLine = cbLine;
274 mLastBitsPerPixel = bpp,
275 mLastWidth = w;
276 mLastHeight = h;
277
278 ULONG pixelFormat;
279
280 switch (bpp)
281 {
282 case 32:
283 case 24:
284 case 16:
285 pixelFormat = FramebufferPixelFormat_FOURCC_RGB;
286 break;
287 default:
288 pixelFormat = FramebufferPixelFormat_PixelFormatOpaque;
289 bpp = cbLine = 0;
290 break;
291 }
292
293 /* Atomically set the resize status before calling the framebuffer. The new InProgress status will
294 * disable access to the VGA device by the EMT thread.
295 */
296 bool f = ASMAtomicCmpXchgU32 (&maFramebuffers[uScreenId].u32ResizeStatus,
297 ResizeStatus_InProgress, ResizeStatus_Void);
298 AssertReleaseMsg(f, ("f = %d\n", f));NOREF(f);
299
300 /* The framebuffer is locked in the state.
301 * The lock is kept, because the framebuffer is in undefined state.
302 */
303 maFramebuffers[uScreenId].pFramebuffer->Lock();
304
305 int rc = callFramebufferResize (maFramebuffers[uScreenId].pFramebuffer, uScreenId,
306 pixelFormat, pvVRAM, bpp, cbLine, w, h);
307 if (rc == VINF_VGA_RESIZE_IN_PROGRESS)
308 {
309 /* Immediately return to the caller. ResizeCompleted will be called back by the
310 * GUI thread. The ResizeCompleted callback will change the resize status from
311 * InProgress to UpdateDisplayData. The latter status will be checked by the
312 * display timer callback on EMT and all required adjustments will be done there.
313 */
314 return rc;
315 }
316
317 /* Set the status so the 'handleResizeCompleted' would work. */
318 f = ASMAtomicCmpXchgU32 (&maFramebuffers[uScreenId].u32ResizeStatus,
319 ResizeStatus_UpdateDisplayData, ResizeStatus_InProgress);
320 AssertRelease(f);NOREF(f);
321
322 /* The method also unlocks the framebuffer. */
323 handleResizeCompletedEMT();
324
325 return VINF_SUCCESS;
326}
327
328/**
329 * Framebuffer has been resized.
330 * Read the new display data and unlock the framebuffer.
331 *
332 * @thread EMT
333 */
334void Display::handleResizeCompletedEMT (void)
335{
336 LogFlowFunc(("\n"));
337
338 unsigned uScreenId;
339 for (uScreenId = 0; uScreenId < mcMonitors; uScreenId++)
340 {
341 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
342
343 /* Try to into non resizing state. */
344 bool f = ASMAtomicCmpXchgU32 (&pFBInfo->u32ResizeStatus, ResizeStatus_Void, ResizeStatus_UpdateDisplayData);
345
346 if (f == false)
347 {
348 /* This is not the display that has completed resizing. */
349 continue;
350 }
351
352 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN && !pFBInfo->pFramebuffer.isNull())
353 {
354 /* Primary framebuffer has completed the resize. Update the connector data for VGA device. */
355 updateDisplayData();
356
357 /* Check the framebuffer pixel format to setup the rendering in VGA device. */
358 BOOL usesGuestVRAM = FALSE;
359 pFBInfo->pFramebuffer->COMGETTER(UsesGuestVRAM) (&usesGuestVRAM);
360
361 pFBInfo->fDefaultFormat = (usesGuestVRAM == FALSE);
362
363 mpDrv->pUpPort->pfnSetRenderVRAM (mpDrv->pUpPort, pFBInfo->fDefaultFormat);
364 }
365
366#ifdef DEBUG_sunlover
367 if (!stam)
368 {
369 /* protect mpVM */
370 Console::SafeVMPtr pVM (mParent);
371 AssertComRC (pVM.rc());
372
373 STAM_REG(pVM, &StatDisplayRefresh, STAMTYPE_PROFILE, "/PROF/Display/Refresh", STAMUNIT_TICKS_PER_CALL, "Time spent in EMT for display updates.");
374 stam = 1;
375 }
376#endif /* DEBUG_sunlover */
377
378 /* Inform VRDP server about the change of display parameters. */
379 LogFlowFunc (("Calling VRDP\n"));
380 mParent->consoleVRDPServer()->SendResize();
381
382 if (!pFBInfo->pFramebuffer.isNull())
383 {
384 /* Unlock framebuffer after evrything is done. */
385 pFBInfo->pFramebuffer->Unlock();
386 }
387 }
388}
389
390static void checkCoordBounds (int *px, int *py, int *pw, int *ph, int cx, int cy)
391{
392 /* Correct negative x and y coordinates. */
393 if (*px < 0)
394 {
395 *px += *pw; /* Compute xRight which is also the new width. */
396
397 *pw = (*px < 0)? 0: *px;
398
399 *px = 0;
400 }
401
402 if (*py < 0)
403 {
404 *py += *ph; /* Compute xBottom, which is also the new height. */
405
406 *ph = (*py < 0)? 0: *py;
407
408 *py = 0;
409 }
410
411 /* Also check if coords are greater than the display resolution. */
412 if (*px + *pw > cx)
413 {
414 *pw = cx > *px? cx - *px: 0;
415 }
416
417 if (*py + *ph > cy)
418 {
419 *ph = cy > *py? cy - *py: 0;
420 }
421}
422
423unsigned mapCoordsToScreen(DISPLAYFBINFO *pInfos, unsigned cInfos, int *px, int *py, int *pw, int *ph)
424{
425 DISPLAYFBINFO *pInfo = pInfos;
426 unsigned uScreenId;
427 LogSunlover (("mapCoordsToScreen: %d,%d %dx%d\n", *px, *py, *pw, *ph));
428 for (uScreenId = 0; uScreenId < cInfos; uScreenId++, pInfo++)
429 {
430 LogSunlover ((" [%d] %d,%d %dx%d\n", uScreenId, pInfo->xOrigin, pInfo->yOrigin, pInfo->w, pInfo->h));
431 if ( (pInfo->xOrigin <= *px && *px < pInfo->xOrigin + (int)pInfo->w)
432 && (pInfo->yOrigin <= *py && *py < pInfo->yOrigin + (int)pInfo->h))
433 {
434 /* The rectangle belongs to the screen. Correct coordinates. */
435 *px -= pInfo->xOrigin;
436 *py -= pInfo->yOrigin;
437 LogSunlover ((" -> %d,%d", *px, *py));
438 break;
439 }
440 }
441 if (uScreenId == cInfos)
442 {
443 /* Map to primary screen. */
444 uScreenId = 0;
445 }
446 LogSunlover ((" scr %d\n", uScreenId));
447 return uScreenId;
448}
449
450
451/**
452 * Handles display update event.
453 *
454 * @param x Update area x coordinate
455 * @param y Update area y coordinate
456 * @param w Update area width
457 * @param h Update area height
458 *
459 * @thread EMT
460 */
461void Display::handleDisplayUpdate (int x, int y, int w, int h)
462{
463#ifdef DEBUG_sunlover
464 LogFlowFunc (("%d,%d %dx%d (%d,%d)\n",
465 x, y, w, h, mpDrv->Connector.cx, mpDrv->Connector.cy));
466#endif /* DEBUG_sunlover */
467
468 unsigned uScreenId = mapCoordsToScreen(maFramebuffers, mcMonitors, &x, &y, &w, &h);
469
470#ifdef DEBUG_sunlover
471 LogFlowFunc (("%d,%d %dx%d (checked)\n", x, y, w, h));
472#endif /* DEBUG_sunlover */
473
474 IFramebuffer *pFramebuffer = maFramebuffers[uScreenId].pFramebuffer;
475
476 // if there is no framebuffer, this call is not interesting
477 if (pFramebuffer == NULL)
478 return;
479
480 pFramebuffer->Lock();
481
482 /* special processing for the internal framebuffer */
483 if (mInternalFramebuffer)
484 {
485 pFramebuffer->Unlock();
486 } else
487 {
488 /* callback into the framebuffer to notify it */
489 BOOL finished = FALSE;
490
491 RTSemEventMultiReset(mUpdateSem);
492
493 checkCoordBounds (&x, &y, &w, &h, mpDrv->Connector.cx, mpDrv->Connector.cy);
494
495 pFramebuffer->NotifyUpdate(x, y, w, h, &finished);
496
497 if (!finished)
498 {
499 /*
500 * the framebuffer needs more time to process
501 * the event so we have to halt the VM until it's done
502 */
503 pFramebuffer->Unlock();
504 RTSemEventMultiWait(mUpdateSem, RT_INDEFINITE_WAIT);
505 } else
506 {
507 pFramebuffer->Unlock();
508 }
509
510 if (!mfVideoAccelEnabled)
511 {
512 /* When VBVA is enabled, the VRDP server is informed in the VideoAccelFlush.
513 * Inform the server here only if VBVA is disabled.
514 */
515 if (maFramebuffers[uScreenId].u32ResizeStatus == ResizeStatus_Void)
516 {
517 mParent->consoleVRDPServer()->SendUpdateBitmap(uScreenId, x, y, w, h);
518 }
519 }
520 }
521 return;
522}
523
524typedef struct _VBVADIRTYREGION
525{
526 /* Copies of object's pointers used by vbvaRgn functions. */
527 DISPLAYFBINFO *paFramebuffers;
528 unsigned cMonitors;
529 Display *pDisplay;
530 PPDMIDISPLAYPORT pPort;
531
532} VBVADIRTYREGION;
533
534static void vbvaRgnInit (VBVADIRTYREGION *prgn, DISPLAYFBINFO *paFramebuffers, unsigned cMonitors, Display *pd, PPDMIDISPLAYPORT pp)
535{
536 prgn->paFramebuffers = paFramebuffers;
537 prgn->cMonitors = cMonitors;
538 prgn->pDisplay = pd;
539 prgn->pPort = pp;
540
541 unsigned uScreenId;
542 for (uScreenId = 0; uScreenId < cMonitors; uScreenId++)
543 {
544 DISPLAYFBINFO *pFBInfo = &prgn->paFramebuffers[uScreenId];
545
546 memset (&pFBInfo->dirtyRect, 0, sizeof (pFBInfo->dirtyRect));
547 }
548}
549
550static void vbvaRgnDirtyRect (VBVADIRTYREGION *prgn, unsigned uScreenId, VBVACMDHDR *phdr)
551{
552 LogSunlover (("x = %d, y = %d, w = %d, h = %d\n",
553 phdr->x, phdr->y, phdr->w, phdr->h));
554
555 /*
556 * Here update rectangles are accumulated to form an update area.
557 * @todo
558 * Now the simpliest method is used which builds one rectangle that
559 * includes all update areas. A bit more advanced method can be
560 * employed here. The method should be fast however.
561 */
562 if (phdr->w == 0 || phdr->h == 0)
563 {
564 /* Empty rectangle. */
565 return;
566 }
567
568 int32_t xRight = phdr->x + phdr->w;
569 int32_t yBottom = phdr->y + phdr->h;
570
571 DISPLAYFBINFO *pFBInfo = &prgn->paFramebuffers[uScreenId];
572
573 if (pFBInfo->dirtyRect.xRight == 0)
574 {
575 /* This is the first rectangle to be added. */
576 pFBInfo->dirtyRect.xLeft = phdr->x;
577 pFBInfo->dirtyRect.yTop = phdr->y;
578 pFBInfo->dirtyRect.xRight = xRight;
579 pFBInfo->dirtyRect.yBottom = yBottom;
580 }
581 else
582 {
583 /* Adjust region coordinates. */
584 if (pFBInfo->dirtyRect.xLeft > phdr->x)
585 {
586 pFBInfo->dirtyRect.xLeft = phdr->x;
587 }
588
589 if (pFBInfo->dirtyRect.yTop > phdr->y)
590 {
591 pFBInfo->dirtyRect.yTop = phdr->y;
592 }
593
594 if (pFBInfo->dirtyRect.xRight < xRight)
595 {
596 pFBInfo->dirtyRect.xRight = xRight;
597 }
598
599 if (pFBInfo->dirtyRect.yBottom < yBottom)
600 {
601 pFBInfo->dirtyRect.yBottom = yBottom;
602 }
603 }
604
605 if (pFBInfo->fDefaultFormat)
606 {
607 //@todo pfnUpdateDisplayRect must take the vram offset parameter for the framebuffer
608 prgn->pPort->pfnUpdateDisplayRect (prgn->pPort, phdr->x, phdr->y, phdr->w, phdr->h);
609 prgn->pDisplay->handleDisplayUpdate (phdr->x, phdr->y, phdr->w, phdr->h);
610 }
611
612 return;
613}
614
615static void vbvaRgnUpdateFramebuffer (VBVADIRTYREGION *prgn, unsigned uScreenId)
616{
617 DISPLAYFBINFO *pFBInfo = &prgn->paFramebuffers[uScreenId];
618
619 uint32_t w = pFBInfo->dirtyRect.xRight - pFBInfo->dirtyRect.xLeft;
620 uint32_t h = pFBInfo->dirtyRect.yBottom - pFBInfo->dirtyRect.yTop;
621
622 if (!pFBInfo->fDefaultFormat && pFBInfo->pFramebuffer && w != 0 && h != 0)
623 {
624 //@todo pfnUpdateDisplayRect must take the vram offset parameter for the framebuffer
625 prgn->pPort->pfnUpdateDisplayRect (prgn->pPort, pFBInfo->dirtyRect.xLeft, pFBInfo->dirtyRect.yTop, w, h);
626 prgn->pDisplay->handleDisplayUpdate (pFBInfo->dirtyRect.xLeft, pFBInfo->dirtyRect.yTop, w, h);
627 }
628}
629
630static void vbvaSetMemoryFlags (VBVAMEMORY *pVbvaMemory,
631 bool fVideoAccelEnabled,
632 bool fVideoAccelVRDP,
633 uint32_t fu32SupportedOrders,
634 DISPLAYFBINFO *paFBInfos,
635 unsigned cFBInfos)
636{
637 if (pVbvaMemory)
638 {
639 /* This called only on changes in mode. So reset VRDP always. */
640 uint32_t fu32Flags = VBVA_F_MODE_VRDP_RESET;
641
642 if (fVideoAccelEnabled)
643 {
644 fu32Flags |= VBVA_F_MODE_ENABLED;
645
646 if (fVideoAccelVRDP)
647 {
648 fu32Flags |= VBVA_F_MODE_VRDP | VBVA_F_MODE_VRDP_ORDER_MASK;
649
650 pVbvaMemory->fu32SupportedOrders = fu32SupportedOrders;
651 }
652 }
653
654 pVbvaMemory->fu32ModeFlags = fu32Flags;
655 }
656
657 unsigned uScreenId;
658 for (uScreenId = 0; uScreenId < cFBInfos; uScreenId++)
659 {
660 if (paFBInfos[uScreenId].pHostEvents)
661 {
662 paFBInfos[uScreenId].pHostEvents->fu32Events |= VBOX_VIDEO_INFO_HOST_EVENTS_F_VRDP_RESET;
663 }
664 }
665}
666
667bool Display::VideoAccelAllowed (void)
668{
669 return true;
670}
671
672/**
673 * @thread EMT
674 */
675int Display::VideoAccelEnable (bool fEnable, VBVAMEMORY *pVbvaMemory)
676{
677 int rc = VINF_SUCCESS;
678
679 /* Called each time the guest wants to use acceleration,
680 * or when the VGA device disables acceleration,
681 * or when restoring the saved state with accel enabled.
682 *
683 * VGA device disables acceleration on each video mode change
684 * and on reset.
685 *
686 * Guest enabled acceleration at will. And it has to enable
687 * acceleration after a mode change.
688 */
689 LogFlowFunc (("mfVideoAccelEnabled = %d, fEnable = %d, pVbvaMemory = %p\n",
690 mfVideoAccelEnabled, fEnable, pVbvaMemory));
691
692 /* Strictly check parameters. Callers must not pass anything in the case. */
693 Assert((fEnable && pVbvaMemory) || (!fEnable && pVbvaMemory == NULL));
694
695 if (!VideoAccelAllowed ())
696 {
697 return VERR_NOT_SUPPORTED;
698 }
699
700 /*
701 * Verify that the VM is in running state. If it is not,
702 * then this must be postponed until it goes to running.
703 */
704 if (!mfMachineRunning)
705 {
706 Assert (!mfVideoAccelEnabled);
707
708 LogFlowFunc (("Machine is not yet running.\n"));
709
710 if (fEnable)
711 {
712 mfPendingVideoAccelEnable = fEnable;
713 mpPendingVbvaMemory = pVbvaMemory;
714 }
715
716 return rc;
717 }
718
719 /* Check that current status is not being changed */
720 if (mfVideoAccelEnabled == fEnable)
721 {
722 return rc;
723 }
724
725 if (mfVideoAccelEnabled)
726 {
727 /* Process any pending orders and empty the VBVA ring buffer. */
728 VideoAccelFlush ();
729 }
730
731 if (!fEnable && mpVbvaMemory)
732 {
733 mpVbvaMemory->fu32ModeFlags &= ~VBVA_F_MODE_ENABLED;
734 }
735
736 /* Safety precaution. There is no more VBVA until everything is setup! */
737 mpVbvaMemory = NULL;
738 mfVideoAccelEnabled = false;
739
740 /* Update entire display. */
741 if (maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN].u32ResizeStatus == ResizeStatus_Void)
742 {
743 mpDrv->pUpPort->pfnUpdateDisplayAll(mpDrv->pUpPort);
744 }
745
746 /* Everything OK. VBVA status can be changed. */
747
748 /* Notify the VMMDev, which saves VBVA status in the saved state,
749 * and needs to know current status.
750 */
751 PPDMIVMMDEVPORT pVMMDevPort = mParent->getVMMDev()->getVMMDevPort ();
752
753 if (pVMMDevPort)
754 {
755 pVMMDevPort->pfnVBVAChange (pVMMDevPort, fEnable);
756 }
757
758 if (fEnable)
759 {
760 mpVbvaMemory = pVbvaMemory;
761 mfVideoAccelEnabled = true;
762
763 /* Initialize the hardware memory. */
764 vbvaSetMemoryFlags (mpVbvaMemory, mfVideoAccelEnabled, mfVideoAccelVRDP, mfu32SupportedOrders, maFramebuffers, mcMonitors);
765 mpVbvaMemory->off32Data = 0;
766 mpVbvaMemory->off32Free = 0;
767
768 memset (mpVbvaMemory->aRecords, 0, sizeof (mpVbvaMemory->aRecords));
769 mpVbvaMemory->indexRecordFirst = 0;
770 mpVbvaMemory->indexRecordFree = 0;
771
772 LogRel(("VBVA: Enabled.\n"));
773 }
774 else
775 {
776 LogRel(("VBVA: Disabled.\n"));
777 }
778
779 LogFlowFunc (("VideoAccelEnable: rc = %Vrc.\n", rc));
780
781 return rc;
782}
783
784#ifdef VBOX_VRDP
785/* Called always by one VRDP server thread. Can be thread-unsafe.
786 */
787void Display::VideoAccelVRDP (bool fEnable)
788{
789 int c = fEnable?
790 ASMAtomicIncS32 (&mcVideoAccelVRDPRefs):
791 ASMAtomicDecS32 (&mcVideoAccelVRDPRefs);
792
793 Assert (c >= 0);
794
795 if (c == 0)
796 {
797 /* The last client has disconnected, and the accel can be
798 * disabled.
799 */
800 Assert (fEnable == false);
801
802 mfVideoAccelVRDP = false;
803 mfu32SupportedOrders = 0;
804
805 vbvaSetMemoryFlags (mpVbvaMemory, mfVideoAccelEnabled, mfVideoAccelVRDP, mfu32SupportedOrders, maFramebuffers, mcMonitors);
806
807 LogRel(("VBVA: VRDP acceleration has been disabled.\n"));
808 }
809 else if ( c == 1
810 && !mfVideoAccelVRDP)
811 {
812 /* The first client has connected. Enable the accel.
813 */
814 Assert (fEnable == true);
815
816 mfVideoAccelVRDP = true;
817 /* Supporting all orders. */
818 mfu32SupportedOrders = ~0;
819
820 vbvaSetMemoryFlags (mpVbvaMemory, mfVideoAccelEnabled, mfVideoAccelVRDP, mfu32SupportedOrders, maFramebuffers, mcMonitors);
821
822 LogRel(("VBVA: VRDP acceleration has been requested.\n"));
823 }
824 else
825 {
826 /* A client is connected or disconnected but there is no change in the
827 * accel state. It remains enabled.
828 */
829 Assert (mfVideoAccelVRDP == true);
830 }
831}
832#endif /* VBOX_VRDP */
833
834static bool vbvaVerifyRingBuffer (VBVAMEMORY *pVbvaMemory)
835{
836 return true;
837}
838
839static void vbvaFetchBytes (VBVAMEMORY *pVbvaMemory, uint8_t *pu8Dst, uint32_t cbDst)
840{
841 if (cbDst >= VBVA_RING_BUFFER_SIZE)
842 {
843 AssertMsgFailed (("cbDst = 0x%08X, ring buffer size 0x%08X", cbDst, VBVA_RING_BUFFER_SIZE));
844 return;
845 }
846
847 uint32_t u32BytesTillBoundary = VBVA_RING_BUFFER_SIZE - pVbvaMemory->off32Data;
848 uint8_t *src = &pVbvaMemory->au8RingBuffer[pVbvaMemory->off32Data];
849 int32_t i32Diff = cbDst - u32BytesTillBoundary;
850
851 if (i32Diff <= 0)
852 {
853 /* Chunk will not cross buffer boundary. */
854 memcpy (pu8Dst, src, cbDst);
855 }
856 else
857 {
858 /* Chunk crosses buffer boundary. */
859 memcpy (pu8Dst, src, u32BytesTillBoundary);
860 memcpy (pu8Dst + u32BytesTillBoundary, &pVbvaMemory->au8RingBuffer[0], i32Diff);
861 }
862
863 /* Advance data offset. */
864 pVbvaMemory->off32Data = (pVbvaMemory->off32Data + cbDst) % VBVA_RING_BUFFER_SIZE;
865
866 return;
867}
868
869
870static bool vbvaPartialRead (uint8_t **ppu8, uint32_t *pcb, uint32_t cbRecord, VBVAMEMORY *pVbvaMemory)
871{
872 uint8_t *pu8New;
873
874 LogFlow(("MAIN::DisplayImpl::vbvaPartialRead: p = %p, cb = %d, cbRecord 0x%08X\n",
875 *ppu8, *pcb, cbRecord));
876
877 if (*ppu8)
878 {
879 Assert (*pcb);
880 pu8New = (uint8_t *)RTMemRealloc (*ppu8, cbRecord);
881 }
882 else
883 {
884 Assert (!*pcb);
885 pu8New = (uint8_t *)RTMemAlloc (cbRecord);
886 }
887
888 if (!pu8New)
889 {
890 /* Memory allocation failed, fail the function. */
891 Log(("MAIN::vbvaPartialRead: failed to (re)alocate memory for partial record!!! cbRecord 0x%08X\n",
892 cbRecord));
893
894 if (*ppu8)
895 {
896 RTMemFree (*ppu8);
897 }
898
899 *ppu8 = NULL;
900 *pcb = 0;
901
902 return false;
903 }
904
905 /* Fetch data from the ring buffer. */
906 vbvaFetchBytes (pVbvaMemory, pu8New + *pcb, cbRecord - *pcb);
907
908 *ppu8 = pu8New;
909 *pcb = cbRecord;
910
911 return true;
912}
913
914/* For contiguous chunks just return the address in the buffer.
915 * For crossing boundary - allocate a buffer from heap.
916 */
917bool Display::vbvaFetchCmd (VBVACMDHDR **ppHdr, uint32_t *pcbCmd)
918{
919 uint32_t indexRecordFirst = mpVbvaMemory->indexRecordFirst;
920 uint32_t indexRecordFree = mpVbvaMemory->indexRecordFree;
921
922#ifdef DEBUG_sunlover
923 LogFlowFunc (("first = %d, free = %d\n",
924 indexRecordFirst, indexRecordFree));
925#endif /* DEBUG_sunlover */
926
927 if (!vbvaVerifyRingBuffer (mpVbvaMemory))
928 {
929 return false;
930 }
931
932 if (indexRecordFirst == indexRecordFree)
933 {
934 /* No records to process. Return without assigning output variables. */
935 return true;
936 }
937
938 VBVARECORD *pRecord = &mpVbvaMemory->aRecords[indexRecordFirst];
939
940#ifdef DEBUG_sunlover
941 LogFlowFunc (("cbRecord = 0x%08X\n", pRecord->cbRecord));
942#endif /* DEBUG_sunlover */
943
944 uint32_t cbRecord = pRecord->cbRecord & ~VBVA_F_RECORD_PARTIAL;
945
946 if (mcbVbvaPartial)
947 {
948 /* There is a partial read in process. Continue with it. */
949
950 Assert (mpu8VbvaPartial);
951
952 LogFlowFunc (("continue partial record mcbVbvaPartial = %d cbRecord 0x%08X, first = %d, free = %d\n",
953 mcbVbvaPartial, pRecord->cbRecord, indexRecordFirst, indexRecordFree));
954
955 if (cbRecord > mcbVbvaPartial)
956 {
957 /* New data has been added to the record. */
958 if (!vbvaPartialRead (&mpu8VbvaPartial, &mcbVbvaPartial, cbRecord, mpVbvaMemory))
959 {
960 return false;
961 }
962 }
963
964 if (!(pRecord->cbRecord & VBVA_F_RECORD_PARTIAL))
965 {
966 /* The record is completed by guest. Return it to the caller. */
967 *ppHdr = (VBVACMDHDR *)mpu8VbvaPartial;
968 *pcbCmd = mcbVbvaPartial;
969
970 mpu8VbvaPartial = NULL;
971 mcbVbvaPartial = 0;
972
973 /* Advance the record index. */
974 mpVbvaMemory->indexRecordFirst = (indexRecordFirst + 1) % VBVA_MAX_RECORDS;
975
976#ifdef DEBUG_sunlover
977 LogFlowFunc (("partial done ok, data = %d, free = %d\n",
978 mpVbvaMemory->off32Data, mpVbvaMemory->off32Free));
979#endif /* DEBUG_sunlover */
980 }
981
982 return true;
983 }
984
985 /* A new record need to be processed. */
986 if (pRecord->cbRecord & VBVA_F_RECORD_PARTIAL)
987 {
988 /* Current record is being written by guest. '=' is important here. */
989 if (cbRecord >= VBVA_RING_BUFFER_SIZE - VBVA_RING_BUFFER_THRESHOLD)
990 {
991 /* Partial read must be started. */
992 if (!vbvaPartialRead (&mpu8VbvaPartial, &mcbVbvaPartial, cbRecord, mpVbvaMemory))
993 {
994 return false;
995 }
996
997 LogFlowFunc (("started partial record mcbVbvaPartial = 0x%08X cbRecord 0x%08X, first = %d, free = %d\n",
998 mcbVbvaPartial, pRecord->cbRecord, indexRecordFirst, indexRecordFree));
999 }
1000
1001 return true;
1002 }
1003
1004 /* Current record is complete. If it is not empty, process it. */
1005 if (cbRecord)
1006 {
1007 /* The size of largest contiguos chunk in the ring biffer. */
1008 uint32_t u32BytesTillBoundary = VBVA_RING_BUFFER_SIZE - mpVbvaMemory->off32Data;
1009
1010 /* The ring buffer pointer. */
1011 uint8_t *au8RingBuffer = &mpVbvaMemory->au8RingBuffer[0];
1012
1013 /* The pointer to data in the ring buffer. */
1014 uint8_t *src = &au8RingBuffer[mpVbvaMemory->off32Data];
1015
1016 /* Fetch or point the data. */
1017 if (u32BytesTillBoundary >= cbRecord)
1018 {
1019 /* The command does not cross buffer boundary. Return address in the buffer. */
1020 *ppHdr = (VBVACMDHDR *)src;
1021
1022 /* Advance data offset. */
1023 mpVbvaMemory->off32Data = (mpVbvaMemory->off32Data + cbRecord) % VBVA_RING_BUFFER_SIZE;
1024 }
1025 else
1026 {
1027 /* The command crosses buffer boundary. Rare case, so not optimized. */
1028 uint8_t *dst = (uint8_t *)RTMemAlloc (cbRecord);
1029
1030 if (!dst)
1031 {
1032 LogFlowFunc (("could not allocate %d bytes from heap!!!\n", cbRecord));
1033 mpVbvaMemory->off32Data = (mpVbvaMemory->off32Data + cbRecord) % VBVA_RING_BUFFER_SIZE;
1034 return false;
1035 }
1036
1037 vbvaFetchBytes (mpVbvaMemory, dst, cbRecord);
1038
1039 *ppHdr = (VBVACMDHDR *)dst;
1040
1041#ifdef DEBUG_sunlover
1042 LogFlowFunc (("Allocated from heap %p\n", dst));
1043#endif /* DEBUG_sunlover */
1044 }
1045 }
1046
1047 *pcbCmd = cbRecord;
1048
1049 /* Advance the record index. */
1050 mpVbvaMemory->indexRecordFirst = (indexRecordFirst + 1) % VBVA_MAX_RECORDS;
1051
1052#ifdef DEBUG_sunlover
1053 LogFlowFunc (("done ok, data = %d, free = %d\n",
1054 mpVbvaMemory->off32Data, mpVbvaMemory->off32Free));
1055#endif /* DEBUG_sunlover */
1056
1057 return true;
1058}
1059
1060void Display::vbvaReleaseCmd (VBVACMDHDR *pHdr, int32_t cbCmd)
1061{
1062 uint8_t *au8RingBuffer = mpVbvaMemory->au8RingBuffer;
1063
1064 if ( (uint8_t *)pHdr >= au8RingBuffer
1065 && (uint8_t *)pHdr < &au8RingBuffer[VBVA_RING_BUFFER_SIZE])
1066 {
1067 /* The pointer is inside ring buffer. Must be continuous chunk. */
1068 Assert (VBVA_RING_BUFFER_SIZE - ((uint8_t *)pHdr - au8RingBuffer) >= cbCmd);
1069
1070 /* Do nothing. */
1071
1072 Assert (!mpu8VbvaPartial && mcbVbvaPartial == 0);
1073 }
1074 else
1075 {
1076 /* The pointer is outside. It is then an allocated copy. */
1077
1078#ifdef DEBUG_sunlover
1079 LogFlowFunc (("Free heap %p\n", pHdr));
1080#endif /* DEBUG_sunlover */
1081
1082 if ((uint8_t *)pHdr == mpu8VbvaPartial)
1083 {
1084 mpu8VbvaPartial = NULL;
1085 mcbVbvaPartial = 0;
1086 }
1087 else
1088 {
1089 Assert (!mpu8VbvaPartial && mcbVbvaPartial == 0);
1090 }
1091
1092 RTMemFree (pHdr);
1093 }
1094
1095 return;
1096}
1097
1098
1099/**
1100 * Called regularly on the DisplayRefresh timer.
1101 * Also on behalf of guest, when the ring buffer is full.
1102 *
1103 * @thread EMT
1104 */
1105void Display::VideoAccelFlush (void)
1106{
1107#ifdef DEBUG_sunlover_2
1108 LogFlowFunc (("mfVideoAccelEnabled = %d\n", mfVideoAccelEnabled));
1109#endif /* DEBUG_sunlover_2 */
1110
1111 if (!mfVideoAccelEnabled)
1112 {
1113 Log(("Display::VideoAccelFlush: called with disabled VBVA!!! Ignoring.\n"));
1114 return;
1115 }
1116
1117 /* Here VBVA is enabled and we have the accelerator memory pointer. */
1118 Assert(mpVbvaMemory);
1119
1120#ifdef DEBUG_sunlover_2
1121 LogFlowFunc (("indexRecordFirst = %d, indexRecordFree = %d, off32Data = %d, off32Free = %d\n",
1122 mpVbvaMemory->indexRecordFirst, mpVbvaMemory->indexRecordFree, mpVbvaMemory->off32Data, mpVbvaMemory->off32Free));
1123#endif /* DEBUG_sunlover_2 */
1124
1125 /* Quick check for "nothing to update" case. */
1126 if (mpVbvaMemory->indexRecordFirst == mpVbvaMemory->indexRecordFree)
1127 {
1128 return;
1129 }
1130
1131 /* Process the ring buffer */
1132 unsigned uScreenId;
1133 for (uScreenId = 0; uScreenId < mcMonitors; uScreenId++)
1134 {
1135 if (!maFramebuffers[uScreenId].pFramebuffer.isNull())
1136 {
1137 maFramebuffers[uScreenId].pFramebuffer->Lock ();
1138 }
1139 }
1140
1141 /* Initialize dirty rectangles accumulator. */
1142 VBVADIRTYREGION rgn;
1143 vbvaRgnInit (&rgn, maFramebuffers, mcMonitors, this, mpDrv->pUpPort);
1144
1145 for (;;)
1146 {
1147 VBVACMDHDR *phdr = NULL;
1148 uint32_t cbCmd = ~0;
1149
1150 /* Fetch the command data. */
1151 if (!vbvaFetchCmd (&phdr, &cbCmd))
1152 {
1153 Log(("Display::VideoAccelFlush: unable to fetch command. off32Data = %d, off32Free = %d. Disabling VBVA!!!\n",
1154 mpVbvaMemory->off32Data, mpVbvaMemory->off32Free));
1155
1156 /* Disable VBVA on those processing errors. */
1157 VideoAccelEnable (false, NULL);
1158
1159 break;
1160 }
1161
1162 if (cbCmd == uint32_t(~0))
1163 {
1164 /* No more commands yet in the queue. */
1165 break;
1166 }
1167
1168 if (cbCmd != 0)
1169 {
1170#ifdef DEBUG_sunlover
1171 LogFlowFunc (("hdr: cbCmd = %d, x=%d, y=%d, w=%d, h=%d\n",
1172 cbCmd, phdr->x, phdr->y, phdr->w, phdr->h));
1173#endif /* DEBUG_sunlover */
1174
1175 VBVACMDHDR hdrSaved = *phdr;
1176
1177 int x = phdr->x;
1178 int y = phdr->y;
1179 int w = phdr->w;
1180 int h = phdr->h;
1181
1182 uScreenId = mapCoordsToScreen(maFramebuffers, mcMonitors, &x, &y, &w, &h);
1183
1184 phdr->x = (int16_t)x;
1185 phdr->y = (int16_t)y;
1186 phdr->w = (uint16_t)w;
1187 phdr->h = (uint16_t)h;
1188
1189 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
1190
1191 if (pFBInfo->u32ResizeStatus == ResizeStatus_Void)
1192 {
1193 /* Handle the command.
1194 *
1195 * Guest is responsible for updating the guest video memory.
1196 * The Windows guest does all drawing using Eng*.
1197 *
1198 * For local output, only dirty rectangle information is used
1199 * to update changed areas.
1200 *
1201 * Dirty rectangles are accumulated to exclude overlapping updates and
1202 * group small updates to a larger one.
1203 */
1204
1205 /* Accumulate the update. */
1206 vbvaRgnDirtyRect (&rgn, uScreenId, phdr);
1207
1208 /* Forward the command to VRDP server. */
1209 mParent->consoleVRDPServer()->SendUpdate (uScreenId, phdr, cbCmd);
1210
1211 *phdr = hdrSaved;
1212 }
1213 }
1214
1215 vbvaReleaseCmd (phdr, cbCmd);
1216 }
1217
1218 for (uScreenId = 0; uScreenId < mcMonitors; uScreenId++)
1219 {
1220 if (!maFramebuffers[uScreenId].pFramebuffer.isNull())
1221 {
1222 maFramebuffers[uScreenId].pFramebuffer->Unlock ();
1223 }
1224
1225 if (maFramebuffers[uScreenId].u32ResizeStatus == ResizeStatus_Void)
1226 {
1227 /* Draw the framebuffer. */
1228 vbvaRgnUpdateFramebuffer (&rgn, uScreenId);
1229 }
1230 }
1231}
1232
1233
1234// IDisplay properties
1235/////////////////////////////////////////////////////////////////////////////
1236
1237/**
1238 * Returns the current display width in pixel
1239 *
1240 * @returns COM status code
1241 * @param width Address of result variable.
1242 */
1243STDMETHODIMP Display::COMGETTER(Width) (ULONG *width)
1244{
1245 if (!width)
1246 return E_POINTER;
1247
1248 AutoLock alock (this);
1249 CHECK_READY();
1250
1251 CHECK_CONSOLE_DRV (mpDrv);
1252
1253 *width = mpDrv->Connector.cx;
1254 return S_OK;
1255}
1256
1257/**
1258 * Returns the current display height in pixel
1259 *
1260 * @returns COM status code
1261 * @param height Address of result variable.
1262 */
1263STDMETHODIMP Display::COMGETTER(Height) (ULONG *height)
1264{
1265 if (!height)
1266 return E_POINTER;
1267
1268 AutoLock alock (this);
1269 CHECK_READY();
1270
1271 CHECK_CONSOLE_DRV (mpDrv);
1272
1273 *height = mpDrv->Connector.cy;
1274 return S_OK;
1275}
1276
1277/**
1278 * Returns the current display color depth in bits
1279 *
1280 * @returns COM status code
1281 * @param bitsPerPixel Address of result variable.
1282 */
1283STDMETHODIMP Display::COMGETTER(BitsPerPixel) (ULONG *bitsPerPixel)
1284{
1285 if (!bitsPerPixel)
1286 return E_INVALIDARG;
1287
1288 AutoLock alock (this);
1289 CHECK_READY();
1290
1291 CHECK_CONSOLE_DRV (mpDrv);
1292
1293 uint32_t cBits = 0;
1294 int rc = mpDrv->pUpPort->pfnQueryColorDepth(mpDrv->pUpPort, &cBits);
1295 AssertRC(rc);
1296 *bitsPerPixel = cBits;
1297 return S_OK;
1298}
1299
1300
1301// IDisplay methods
1302/////////////////////////////////////////////////////////////////////////////
1303
1304STDMETHODIMP Display::SetupInternalFramebuffer (ULONG depth)
1305{
1306 LogFlowFunc (("\n"));
1307
1308 AutoLock lock (this);
1309 CHECK_READY();
1310
1311 /*
1312 * Create an internal framebuffer only if depth is not zero. Otherwise, we
1313 * reset back to the "black hole" state as it was at Display construction.
1314 */
1315 ComPtr <IFramebuffer> frameBuf;
1316 if (depth)
1317 {
1318 ComObjPtr <InternalFramebuffer> internal;
1319 internal.createObject();
1320 internal->init (640, 480, depth);
1321 frameBuf = internal; // query interface
1322 }
1323
1324 Console::SafeVMPtrQuiet pVM (mParent);
1325 if (pVM.isOk())
1326 {
1327 /* Must leave the lock here because the changeFramebuffer will also obtain it. */
1328 lock.leave ();
1329
1330 /* send request to the EMT thread */
1331 PVMREQ pReq = NULL;
1332 int vrc = VMR3ReqCall (pVM, &pReq, RT_INDEFINITE_WAIT,
1333 (PFNRT) changeFramebuffer, 3,
1334 this, static_cast <IFramebuffer *> (frameBuf),
1335 true /* aInternal */, VBOX_VIDEO_PRIMARY_SCREEN);
1336 if (VBOX_SUCCESS (vrc))
1337 vrc = pReq->iStatus;
1338 VMR3ReqFree (pReq);
1339
1340 lock.enter ();
1341
1342 ComAssertRCRet (vrc, E_FAIL);
1343 }
1344 else
1345 {
1346 /* No VM is created (VM is powered off), do a direct call */
1347 int vrc = changeFramebuffer (this, frameBuf, true /* aInternal */, VBOX_VIDEO_PRIMARY_SCREEN);
1348 ComAssertRCRet (vrc, E_FAIL);
1349 }
1350
1351 return S_OK;
1352}
1353
1354STDMETHODIMP Display::LockFramebuffer (BYTE **address)
1355{
1356 if (!address)
1357 return E_POINTER;
1358
1359 AutoLock lock(this);
1360 CHECK_READY();
1361
1362 /* only allowed for internal framebuffers */
1363 if (mInternalFramebuffer && !mFramebufferOpened && !maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN].pFramebuffer.isNull())
1364 {
1365 CHECK_CONSOLE_DRV (mpDrv);
1366
1367 maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN].pFramebuffer->Lock();
1368 mFramebufferOpened = true;
1369 *address = mpDrv->Connector.pu8Data;
1370 return S_OK;
1371 }
1372
1373 return setError (E_FAIL,
1374 tr ("Framebuffer locking is allowed only for the internal framebuffer"));
1375}
1376
1377STDMETHODIMP Display::UnlockFramebuffer()
1378{
1379 AutoLock lock(this);
1380 CHECK_READY();
1381
1382 if (mFramebufferOpened)
1383 {
1384 CHECK_CONSOLE_DRV (mpDrv);
1385
1386 maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN].pFramebuffer->Unlock();
1387 mFramebufferOpened = false;
1388 return S_OK;
1389 }
1390
1391 return setError (E_FAIL,
1392 tr ("Framebuffer locking is allowed only for the internal framebuffer"));
1393}
1394
1395STDMETHODIMP Display::RegisterExternalFramebuffer (IFramebuffer *frameBuf)
1396{
1397 LogFlowFunc (("\n"));
1398
1399 if (!frameBuf)
1400 return E_POINTER;
1401
1402 AutoLock lock (this);
1403 CHECK_READY();
1404
1405 Console::SafeVMPtrQuiet pVM (mParent);
1406 if (pVM.isOk())
1407 {
1408 /* Must leave the lock here because the changeFramebuffer will also obtain it. */
1409 lock.leave ();
1410
1411 /* send request to the EMT thread */
1412 PVMREQ pReq = NULL;
1413 int vrc = VMR3ReqCall (pVM, &pReq, RT_INDEFINITE_WAIT,
1414 (PFNRT) changeFramebuffer, 3,
1415 this, frameBuf, false /* aInternal */, VBOX_VIDEO_PRIMARY_SCREEN);
1416 if (VBOX_SUCCESS (vrc))
1417 vrc = pReq->iStatus;
1418 VMR3ReqFree (pReq);
1419
1420 lock.enter ();
1421
1422 ComAssertRCRet (vrc, E_FAIL);
1423 }
1424 else
1425 {
1426 /* No VM is created (VM is powered off), do a direct call */
1427 int vrc = changeFramebuffer (this, frameBuf, false /* aInternal */, VBOX_VIDEO_PRIMARY_SCREEN);
1428 ComAssertRCRet (vrc, E_FAIL);
1429 }
1430
1431 return S_OK;
1432}
1433
1434STDMETHODIMP Display::SetFramebuffer (ULONG aScreenId, IFramebuffer *aFramebuffer)
1435{
1436 LogFlowFunc (("\n"));
1437
1438 if (!aFramebuffer)
1439 return E_POINTER;
1440
1441 AutoLock lock (this);
1442 CHECK_READY();
1443
1444 Console::SafeVMPtrQuiet pVM (mParent);
1445 if (pVM.isOk())
1446 {
1447 /* Must leave the lock here because the changeFramebuffer will also obtain it. */
1448 lock.leave ();
1449
1450 /* send request to the EMT thread */
1451 PVMREQ pReq = NULL;
1452 int vrc = VMR3ReqCall (pVM, &pReq, RT_INDEFINITE_WAIT,
1453 (PFNRT) changeFramebuffer, 3,
1454 this, aFramebuffer, false /* aInternal */, aScreenId);
1455 if (VBOX_SUCCESS (vrc))
1456 vrc = pReq->iStatus;
1457 VMR3ReqFree (pReq);
1458
1459 lock.enter ();
1460
1461 ComAssertRCRet (vrc, E_FAIL);
1462 }
1463 else
1464 {
1465 /* No VM is created (VM is powered off), do a direct call */
1466 int vrc = changeFramebuffer (this, aFramebuffer, false /* aInternal */, aScreenId);
1467 ComAssertRCRet (vrc, E_FAIL);
1468 }
1469
1470 return S_OK;
1471}
1472
1473STDMETHODIMP Display::GetFramebuffer (ULONG aScreenId, IFramebuffer **aFramebuffer, LONG *aXOrigin, LONG *aYOrigin)
1474{
1475 LogFlowFunc (("aScreenId = %d\n", aScreenId));
1476
1477 if (!aFramebuffer)
1478 return E_POINTER;
1479
1480 AutoLock lock (this);
1481 CHECK_READY();
1482
1483 /* @todo this should be actually done on EMT. */
1484 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
1485
1486 *aFramebuffer = pFBInfo->pFramebuffer;
1487 if (*aFramebuffer)
1488 (*aFramebuffer)->AddRef ();
1489 if (aXOrigin)
1490 *aXOrigin = pFBInfo->xOrigin;
1491 if (aYOrigin)
1492 *aYOrigin = pFBInfo->yOrigin;
1493
1494 return S_OK;
1495}
1496
1497STDMETHODIMP Display::SetVideoModeHint(ULONG aWidth, ULONG aHeight, ULONG aBitsPerPixel, ULONG aDisplay)
1498{
1499 AutoLock lock(this);
1500 CHECK_READY();
1501
1502 CHECK_CONSOLE_DRV (mpDrv);
1503
1504 /*
1505 * Do some rough checks for valid input
1506 */
1507 ULONG width = aWidth;
1508 if (!width)
1509 width = mpDrv->Connector.cx;
1510 ULONG height = aHeight;
1511 if (!height)
1512 height = mpDrv->Connector.cy;
1513 ULONG bpp = aBitsPerPixel;
1514 if (!bpp)
1515 {
1516 uint32_t cBits = 0;
1517 int rc = mpDrv->pUpPort->pfnQueryColorDepth(mpDrv->pUpPort, &cBits);
1518 AssertRC(rc);
1519 bpp = cBits;
1520 }
1521 ULONG cMonitors;
1522 mParent->machine()->COMGETTER(MonitorCount)(&cMonitors);
1523 if (cMonitors == 0 && aDisplay > 0)
1524 return E_INVALIDARG;
1525 if (aDisplay >= cMonitors)
1526 return E_INVALIDARG;
1527
1528// sunlover 20070614: It is up to the guest to decide whether the hint is valid.
1529// ULONG vramSize;
1530// mParent->machine()->COMGETTER(VRAMSize)(&vramSize);
1531// /* enough VRAM? */
1532// if ((width * height * (bpp / 8)) > (vramSize * 1024 * 1024))
1533// return setError(E_FAIL, tr("Not enough VRAM for the selected video mode"));
1534
1535 /* Have to leave the lock because the pfnRequestDisplayChange will call EMT. */
1536 lock.leave ();
1537 if (mParent->getVMMDev())
1538 mParent->getVMMDev()->getVMMDevPort()->
1539 pfnRequestDisplayChange (mParent->getVMMDev()->getVMMDevPort(),
1540 aWidth, aHeight, aBitsPerPixel, aDisplay);
1541 return S_OK;
1542}
1543
1544STDMETHODIMP Display::SetSeamlessMode (BOOL enabled)
1545{
1546 AutoLock lock(this);
1547 CHECK_READY();
1548
1549 /* Have to leave the lock because the pfnRequestSeamlessChange will call EMT. */
1550 lock.leave ();
1551 if (mParent->getVMMDev())
1552 mParent->getVMMDev()->getVMMDevPort()->
1553 pfnRequestSeamlessChange (mParent->getVMMDev()->getVMMDevPort(),
1554 !!enabled);
1555 return S_OK;
1556}
1557
1558STDMETHODIMP Display::TakeScreenShot (BYTE *address, ULONG width, ULONG height)
1559{
1560 /// @todo (r=dmik) this function may take too long to complete if the VM
1561 // is doing something like saving state right now. Which, in case if it
1562 // is called on the GUI thread, will make it unresponsive. We should
1563 // check the machine state here (by enclosing the check and VMRequCall
1564 // within the Console lock to make it atomic).
1565
1566 LogFlowFuncEnter();
1567 LogFlowFunc (("address=%p, width=%d, height=%d\n",
1568 address, width, height));
1569
1570 if (!address)
1571 return E_POINTER;
1572 if (!width || !height)
1573 return E_INVALIDARG;
1574
1575 AutoLock lock(this);
1576 CHECK_READY();
1577
1578 CHECK_CONSOLE_DRV (mpDrv);
1579
1580 Console::SafeVMPtr pVM (mParent);
1581 CheckComRCReturnRC (pVM.rc());
1582
1583 HRESULT rc = S_OK;
1584
1585 LogFlowFunc (("Sending SCREENSHOT request\n"));
1586
1587 /*
1588 * First try use the graphics device features for making a snapshot.
1589 * This does not support streatching, is an optional feature (returns not supported).
1590 *
1591 * Note: It may cause a display resize. Watch out for deadlocks.
1592 */
1593 int rcVBox = VERR_NOT_SUPPORTED;
1594 if ( mpDrv->Connector.cx == width
1595 && mpDrv->Connector.cy == height)
1596 {
1597 PVMREQ pReq;
1598 size_t cbData = RT_ALIGN_Z(width, 4) * 4 * height;
1599 rcVBox = VMR3ReqCall(pVM, &pReq, RT_INDEFINITE_WAIT,
1600 (PFNRT)mpDrv->pUpPort->pfnSnapshot, 6, mpDrv->pUpPort,
1601 address, cbData, NULL, NULL, NULL);
1602 if (VBOX_SUCCESS(rcVBox))
1603 {
1604 rcVBox = pReq->iStatus;
1605 VMR3ReqFree(pReq);
1606 }
1607 }
1608
1609 /*
1610 * If the function returns not supported, or if streaching is requested,
1611 * we'll have to do all the work ourselves using the framebuffer data.
1612 */
1613 if (rcVBox == VERR_NOT_SUPPORTED || rcVBox == VERR_NOT_IMPLEMENTED)
1614 {
1615 /** @todo implement snapshot streching and generic snapshot fallback. */
1616 rc = setError (E_NOTIMPL, tr ("This feature is not implemented"));
1617 }
1618 else if (VBOX_FAILURE(rcVBox))
1619 rc = setError (E_FAIL,
1620 tr ("Could not take a screenshot (%Vrc)"), rcVBox);
1621
1622 LogFlowFunc (("rc=%08X\n", rc));
1623 LogFlowFuncLeave();
1624 return rc;
1625}
1626
1627STDMETHODIMP Display::DrawToScreen (BYTE *address, ULONG x, ULONG y,
1628 ULONG width, ULONG height)
1629{
1630 /// @todo (r=dmik) this function may take too long to complete if the VM
1631 // is doing something like saving state right now. Which, in case if it
1632 // is called on the GUI thread, will make it unresponsive. We should
1633 // check the machine state here (by enclosing the check and VMRequCall
1634 // within the Console lock to make it atomic).
1635
1636 LogFlowFuncEnter();
1637 LogFlowFunc (("address=%p, x=%d, y=%d, width=%d, height=%d\n",
1638 address, x, y, width, height));
1639
1640 if (!address)
1641 return E_POINTER;
1642 if (!width || !height)
1643 return E_INVALIDARG;
1644
1645 AutoLock lock(this);
1646 CHECK_READY();
1647
1648 CHECK_CONSOLE_DRV (mpDrv);
1649
1650 Console::SafeVMPtr pVM (mParent);
1651 CheckComRCReturnRC (pVM.rc());
1652
1653 /*
1654 * Again we're lazy and make the graphics device do all the
1655 * dirty convertion work.
1656 */
1657 PVMREQ pReq;
1658 int rcVBox = VMR3ReqCall(pVM, &pReq, RT_INDEFINITE_WAIT,
1659 (PFNRT)mpDrv->pUpPort->pfnDisplayBlt, 6, mpDrv->pUpPort,
1660 address, x, y, width, height);
1661 if (VBOX_SUCCESS(rcVBox))
1662 {
1663 rcVBox = pReq->iStatus;
1664 VMR3ReqFree(pReq);
1665 }
1666
1667 /*
1668 * If the function returns not supported, we'll have to do all the
1669 * work ourselves using the framebuffer.
1670 */
1671 HRESULT rc = S_OK;
1672 if (rcVBox == VERR_NOT_SUPPORTED || rcVBox == VERR_NOT_IMPLEMENTED)
1673 {
1674 /** @todo implement generic fallback for screen blitting. */
1675 rc = E_NOTIMPL;
1676 }
1677 else if (VBOX_FAILURE(rcVBox))
1678 rc = setError (E_FAIL,
1679 tr ("Could not draw to the screen (%Vrc)"), rcVBox);
1680//@todo
1681// else
1682// {
1683// /* All ok. Redraw the screen. */
1684// handleDisplayUpdate (x, y, width, height);
1685// }
1686
1687 LogFlowFunc (("rc=%08X\n", rc));
1688 LogFlowFuncLeave();
1689 return rc;
1690}
1691
1692/**
1693 * Does a full invalidation of the VM display and instructs the VM
1694 * to update it immediately.
1695 *
1696 * @returns COM status code
1697 */
1698STDMETHODIMP Display::InvalidateAndUpdate()
1699{
1700 LogFlowFuncEnter();
1701
1702 AutoLock lock(this);
1703 CHECK_READY();
1704
1705 CHECK_CONSOLE_DRV (mpDrv);
1706
1707 Console::SafeVMPtr pVM (mParent);
1708 CheckComRCReturnRC (pVM.rc());
1709
1710 HRESULT rc = S_OK;
1711
1712 LogFlowFunc (("Sending DPYUPDATE request\n"));
1713
1714 /* pdm.h says that this has to be called from the EMT thread */
1715 PVMREQ pReq;
1716 int rcVBox = VMR3ReqCallVoid(pVM, &pReq, RT_INDEFINITE_WAIT,
1717 (PFNRT)mpDrv->pUpPort->pfnUpdateDisplayAll, 1, mpDrv->pUpPort);
1718 if (VBOX_SUCCESS(rcVBox))
1719 VMR3ReqFree(pReq);
1720
1721 if (VBOX_FAILURE(rcVBox))
1722 rc = setError (E_FAIL,
1723 tr ("Could not invalidate and update the screen (%Vrc)"), rcVBox);
1724
1725 LogFlowFunc (("rc=%08X\n", rc));
1726 LogFlowFuncLeave();
1727 return rc;
1728}
1729
1730/**
1731 * Notification that the framebuffer has completed the
1732 * asynchronous resize processing
1733 *
1734 * @returns COM status code
1735 */
1736STDMETHODIMP Display::ResizeCompleted(ULONG aScreenId)
1737{
1738 LogFlowFunc (("\n"));
1739
1740 /// @todo (dmik) can we AutoLock alock (this); here?
1741 // do it when we switch this class to VirtualBoxBase_NEXT.
1742 // This will require general code review and may add some details.
1743 // In particular, we may want to check whether EMT is really waiting for
1744 // this notification, etc. It might be also good to obey the caller to make
1745 // sure this method is not called from more than one thread at a time
1746 // (and therefore don't use Display lock at all here to save some
1747 // milliseconds).
1748 CHECK_READY();
1749
1750 /* this is only valid for external framebuffers */
1751 if (mInternalFramebuffer)
1752 return setError (E_FAIL,
1753 tr ("Resize completed notification is valid only "
1754 "for external framebuffers"));
1755
1756 /* Set the flag indicating that the resize has completed and display data need to be updated. */
1757 bool f = ASMAtomicCmpXchgU32 (&maFramebuffers[aScreenId].u32ResizeStatus, ResizeStatus_UpdateDisplayData, ResizeStatus_InProgress);
1758 AssertRelease(f);NOREF(f);
1759
1760 return S_OK;
1761}
1762
1763/**
1764 * Notification that the framebuffer has completed the
1765 * asynchronous update processing
1766 *
1767 * @returns COM status code
1768 */
1769STDMETHODIMP Display::UpdateCompleted()
1770{
1771 LogFlowFunc (("\n"));
1772
1773 /// @todo (dmik) can we AutoLock alock (this); here?
1774 // do it when we switch this class to VirtualBoxBase_NEXT.
1775 // Tthis will require general code review and may add some details.
1776 // In particular, we may want to check whether EMT is really waiting for
1777 // this notification, etc. It might be also good to obey the caller to make
1778 // sure this method is not called from more than one thread at a time
1779 // (and therefore don't use Display lock at all here to save some
1780 // milliseconds).
1781 CHECK_READY();
1782
1783 /* this is only valid for external framebuffers */
1784 if (mInternalFramebuffer)
1785 return setError (E_FAIL,
1786 tr ("Resize completed notification is valid only "
1787 "for external framebuffers"));
1788
1789 maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN].pFramebuffer->Lock();
1790 /* signal our semaphore */
1791 RTSemEventMultiSignal(mUpdateSem);
1792 maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN].pFramebuffer->Unlock();
1793
1794 return S_OK;
1795}
1796
1797// private methods
1798/////////////////////////////////////////////////////////////////////////////
1799
1800/**
1801 * Helper to update the display information from the framebuffer.
1802 *
1803 * @param aCheckParams true to compare the parameters of the current framebuffer
1804 * and the new one and issue handleDisplayResize()
1805 * if they differ.
1806 * @thread EMT
1807 */
1808void Display::updateDisplayData (bool aCheckParams /* = false */)
1809{
1810 /* the driver might not have been constructed yet */
1811 if (!mpDrv)
1812 return;
1813
1814#if DEBUG
1815 /*
1816 * Sanity check. Note that this method may be called on EMT after Console
1817 * has started the power down procedure (but before our #drvDestruct() is
1818 * called, in which case pVM will aleady be NULL but mpDrv will not). Since
1819 * we don't really need pVM to proceed, we avoid this check in the release
1820 * build to save some ms (necessary to construct SafeVMPtrQuiet) in this
1821 * time-critical method.
1822 */
1823 Console::SafeVMPtrQuiet pVM (mParent);
1824 if (pVM.isOk())
1825 VM_ASSERT_EMT (pVM.raw());
1826#endif
1827
1828 /* The method is only relevant to the primary framebuffer. */
1829 IFramebuffer *pFramebuffer = maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN].pFramebuffer;
1830
1831 if (pFramebuffer)
1832 {
1833 HRESULT rc;
1834 BYTE *address = 0;
1835 rc = pFramebuffer->COMGETTER(Address) (&address);
1836 AssertComRC (rc);
1837 ULONG bytesPerLine = 0;
1838 rc = pFramebuffer->COMGETTER(BytesPerLine) (&bytesPerLine);
1839 AssertComRC (rc);
1840 ULONG bitsPerPixel = 0;
1841 rc = pFramebuffer->COMGETTER(BitsPerPixel) (&bitsPerPixel);
1842 AssertComRC (rc);
1843 ULONG width = 0;
1844 rc = pFramebuffer->COMGETTER(Width) (&width);
1845 AssertComRC (rc);
1846 ULONG height = 0;
1847 rc = pFramebuffer->COMGETTER(Height) (&height);
1848 AssertComRC (rc);
1849
1850 /*
1851 * Check current parameters with new ones and issue handleDisplayResize()
1852 * to let the new frame buffer adjust itself properly. Note that it will
1853 * result into a recursive updateDisplayData() call but with
1854 * aCheckOld = false.
1855 */
1856 if (aCheckParams &&
1857 (mLastAddress != address ||
1858 mLastBytesPerLine != bytesPerLine ||
1859 mLastBitsPerPixel != bitsPerPixel ||
1860 mLastWidth != (int) width ||
1861 mLastHeight != (int) height))
1862 {
1863 handleDisplayResize (VBOX_VIDEO_PRIMARY_SCREEN, mLastBitsPerPixel,
1864 mLastAddress,
1865 mLastBytesPerLine,
1866 mLastWidth,
1867 mLastHeight);
1868 return;
1869 }
1870
1871 mpDrv->Connector.pu8Data = (uint8_t *) address;
1872 mpDrv->Connector.cbScanline = bytesPerLine;
1873 mpDrv->Connector.cBits = bitsPerPixel;
1874 mpDrv->Connector.cx = width;
1875 mpDrv->Connector.cy = height;
1876 }
1877 else
1878 {
1879 /* black hole */
1880 mpDrv->Connector.pu8Data = NULL;
1881 mpDrv->Connector.cbScanline = 0;
1882 mpDrv->Connector.cBits = 0;
1883 mpDrv->Connector.cx = 0;
1884 mpDrv->Connector.cy = 0;
1885 }
1886}
1887
1888/**
1889 * Changes the current frame buffer. Called on EMT to avoid both
1890 * race conditions and excessive locking.
1891 *
1892 * @note locks this object for writing
1893 * @thread EMT
1894 */
1895/* static */
1896DECLCALLBACK(int) Display::changeFramebuffer (Display *that, IFramebuffer *aFB,
1897 bool aInternal, unsigned uScreenId)
1898{
1899 LogFlowFunc (("uScreenId = %d\n", uScreenId));
1900
1901 AssertReturn (that, VERR_INVALID_PARAMETER);
1902 AssertReturn (aFB || aInternal, VERR_INVALID_PARAMETER);
1903 AssertReturn (uScreenId >= 0 && uScreenId < that->mcMonitors, VERR_INVALID_PARAMETER);
1904
1905 /// @todo (r=dmik) AutoCaller
1906
1907 AutoLock alock (that);
1908
1909 DISPLAYFBINFO *pDisplayFBInfo = &that->maFramebuffers[uScreenId];
1910 pDisplayFBInfo->pFramebuffer = aFB;
1911
1912 that->mInternalFramebuffer = aInternal;
1913 that->mSupportedAccelOps = 0;
1914
1915 /* determine which acceleration functions are supported by this framebuffer */
1916 if (aFB && !aInternal)
1917 {
1918 HRESULT rc;
1919 BOOL accelSupported = FALSE;
1920 rc = aFB->OperationSupported (
1921 FramebufferAccelerationOperation_SolidFillAcceleration, &accelSupported);
1922 AssertComRC (rc);
1923 if (accelSupported)
1924 that->mSupportedAccelOps |=
1925 FramebufferAccelerationOperation_SolidFillAcceleration;
1926 accelSupported = FALSE;
1927 rc = aFB->OperationSupported (
1928 FramebufferAccelerationOperation_ScreenCopyAcceleration, &accelSupported);
1929 AssertComRC (rc);
1930 if (accelSupported)
1931 that->mSupportedAccelOps |=
1932 FramebufferAccelerationOperation_ScreenCopyAcceleration;
1933 }
1934
1935 that->mParent->consoleVRDPServer()->SendResize ();
1936
1937 that->updateDisplayData (true /* aCheckParams */);
1938
1939 return VINF_SUCCESS;
1940}
1941
1942/**
1943 * Handle display resize event issued by the VGA device for the primary screen.
1944 *
1945 * @see PDMIDISPLAYCONNECTOR::pfnResize
1946 */
1947DECLCALLBACK(int) Display::displayResizeCallback(PPDMIDISPLAYCONNECTOR pInterface,
1948 uint32_t bpp, void *pvVRAM, uint32_t cbLine, uint32_t cx, uint32_t cy)
1949{
1950 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
1951
1952 LogFlowFunc (("bpp %d, pvVRAM %p, cbLine %d, cx %d, cy %d\n",
1953 bpp, pvVRAM, cbLine, cx, cy));
1954
1955 return pDrv->pDisplay->handleDisplayResize(VBOX_VIDEO_PRIMARY_SCREEN, bpp, pvVRAM, cbLine, cx, cy);
1956}
1957
1958/**
1959 * Handle display update.
1960 *
1961 * @see PDMIDISPLAYCONNECTOR::pfnUpdateRect
1962 */
1963DECLCALLBACK(void) Display::displayUpdateCallback(PPDMIDISPLAYCONNECTOR pInterface,
1964 uint32_t x, uint32_t y, uint32_t cx, uint32_t cy)
1965{
1966 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
1967
1968#ifdef DEBUG_sunlover
1969 LogFlowFunc (("mfVideoAccelEnabled = %d, %d,%d %dx%d\n",
1970 pDrv->pDisplay->mfVideoAccelEnabled, x, y, cx, cy));
1971#endif /* DEBUG_sunlover */
1972
1973 /* This call does update regardless of VBVA status.
1974 * But in VBVA mode this is called only as result of
1975 * pfnUpdateDisplayAll in the VGA device.
1976 */
1977
1978 pDrv->pDisplay->handleDisplayUpdate(x, y, cx, cy);
1979}
1980
1981/**
1982 * Periodic display refresh callback.
1983 *
1984 * @see PDMIDISPLAYCONNECTOR::pfnRefresh
1985 */
1986DECLCALLBACK(void) Display::displayRefreshCallback(PPDMIDISPLAYCONNECTOR pInterface)
1987{
1988 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
1989
1990#ifdef DEBUG_sunlover
1991 STAM_PROFILE_START(&StatDisplayRefresh, a);
1992#endif /* DEBUG_sunlover */
1993
1994#ifdef DEBUG_sunlover_2
1995 LogFlowFunc (("pDrv->pDisplay->mfVideoAccelEnabled = %d\n",
1996 pDrv->pDisplay->mfVideoAccelEnabled));
1997#endif /* DEBUG_sunlover_2 */
1998
1999 Display *pDisplay = pDrv->pDisplay;
2000
2001 unsigned uScreenId;
2002 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
2003 {
2004 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
2005
2006 /* Check the resize status. The status can be checked normally because
2007 * the status affects only the EMT.
2008 */
2009 uint32_t u32ResizeStatus = pFBInfo->u32ResizeStatus;
2010
2011 if (u32ResizeStatus == ResizeStatus_UpdateDisplayData)
2012 {
2013 LogFlowFunc (("ResizeStatus_UpdateDisplayData %d\n", uScreenId));
2014 /* The framebuffer was resized and display data need to be updated. */
2015 pDisplay->handleResizeCompletedEMT ();
2016 /* Continue with normal processing because the status here is ResizeStatus_Void. */
2017 Assert (pFBInfo->u32ResizeStatus == ResizeStatus_Void);
2018 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
2019 {
2020 /* Repaint the display because VM continued to run during the framebuffer resize. */
2021 if (!pFBInfo->pFramebuffer.isNull())
2022 pDrv->pUpPort->pfnUpdateDisplayAll(pDrv->pUpPort);
2023 }
2024 /* Ignore the refresh for the screen to replay the logic. */
2025 continue;
2026 }
2027 else if (u32ResizeStatus == ResizeStatus_InProgress)
2028 {
2029 /* The framebuffer is being resized. Do not call the VGA device back. Immediately return. */
2030 LogFlowFunc (("ResizeStatus_InProcess\n"));
2031 continue;
2032 }
2033
2034 if (pFBInfo->pFramebuffer.isNull())
2035 {
2036 /*
2037 * Do nothing in the "black hole" mode to avoid copying guest
2038 * video memory to the frame buffer
2039 */
2040 }
2041 else
2042 {
2043 if (pDisplay->mfPendingVideoAccelEnable)
2044 {
2045 /* Acceleration was enabled while machine was not yet running
2046 * due to restoring from saved state. Update entire display and
2047 * actually enable acceleration.
2048 */
2049 Assert(pDisplay->mpPendingVbvaMemory);
2050
2051 /* Acceleration can not be yet enabled.*/
2052 Assert(pDisplay->mpVbvaMemory == NULL);
2053 Assert(!pDisplay->mfVideoAccelEnabled);
2054
2055 if (pDisplay->mfMachineRunning)
2056 {
2057 pDisplay->VideoAccelEnable (pDisplay->mfPendingVideoAccelEnable,
2058 pDisplay->mpPendingVbvaMemory);
2059
2060 /* Reset the pending state. */
2061 pDisplay->mfPendingVideoAccelEnable = false;
2062 pDisplay->mpPendingVbvaMemory = NULL;
2063 }
2064 }
2065 else
2066 {
2067 Assert(pDisplay->mpPendingVbvaMemory == NULL);
2068
2069 if (pDisplay->mfVideoAccelEnabled)
2070 {
2071 Assert(pDisplay->mpVbvaMemory);
2072 pDisplay->VideoAccelFlush ();
2073 }
2074 else
2075 {
2076 Assert(pDrv->Connector.pu8Data);
2077 pDrv->pUpPort->pfnUpdateDisplay(pDrv->pUpPort);
2078 }
2079 }
2080 /* Inform the VRDP server that the current display update sequence is
2081 * completed. At this moment the framebuffer memory contains a definite
2082 * image, that is synchronized with the orders already sent to VRDP client.
2083 * The server can now process redraw requests from clients or initial
2084 * fullscreen updates for new clients.
2085 */
2086 if (pFBInfo->u32ResizeStatus == ResizeStatus_Void)
2087 {
2088 Assert (pDisplay->mParent && pDisplay->mParent->consoleVRDPServer());
2089 pDisplay->mParent->consoleVRDPServer()->SendUpdate (uScreenId, NULL, 0);
2090 }
2091 }
2092 }
2093
2094#ifdef DEBUG_sunlover
2095 STAM_PROFILE_STOP(&StatDisplayRefresh, a);
2096#endif /* DEBUG_sunlover */
2097#ifdef DEBUG_sunlover_2
2098 LogFlowFunc (("leave\n"));
2099#endif /* DEBUG_sunlover_2 */
2100}
2101
2102/**
2103 * Reset notification
2104 *
2105 * @see PDMIDISPLAYCONNECTOR::pfnReset
2106 */
2107DECLCALLBACK(void) Display::displayResetCallback(PPDMIDISPLAYCONNECTOR pInterface)
2108{
2109 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2110
2111 LogFlowFunc (("\n"));
2112
2113 /* Disable VBVA mode. */
2114 pDrv->pDisplay->VideoAccelEnable (false, NULL);
2115}
2116
2117/**
2118 * LFBModeChange notification
2119 *
2120 * @see PDMIDISPLAYCONNECTOR::pfnLFBModeChange
2121 */
2122DECLCALLBACK(void) Display::displayLFBModeChangeCallback(PPDMIDISPLAYCONNECTOR pInterface, bool fEnabled)
2123{
2124 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2125
2126 LogFlowFunc (("fEnabled=%d\n", fEnabled));
2127
2128 NOREF(fEnabled);
2129
2130 /* Disable VBVA mode in any case. The guest driver reenables VBVA mode if necessary. */
2131 pDrv->pDisplay->VideoAccelEnable (false, NULL);
2132}
2133
2134/**
2135 * Adapter information change notification.
2136 *
2137 * @see PDMIDISPLAYCONNECTOR::pfnProcessAdapterData
2138 */
2139DECLCALLBACK(void) Display::displayProcessAdapterDataCallback(PPDMIDISPLAYCONNECTOR pInterface, void *pvVRAM, uint32_t u32VRAMSize)
2140{
2141 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2142
2143 if (pvVRAM == NULL)
2144 {
2145 unsigned i;
2146 for (i = 0; i < pDrv->pDisplay->mcMonitors; i++)
2147 {
2148 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[i];
2149
2150 pFBInfo->u32Offset = 0;
2151 pFBInfo->u32MaxFramebufferSize = 0;
2152 pFBInfo->u32InformationSize = 0;
2153 }
2154 }
2155 else
2156 {
2157 uint8_t *pu8 = (uint8_t *)pvVRAM;
2158 pu8 += u32VRAMSize - VBOX_VIDEO_ADAPTER_INFORMATION_SIZE;
2159
2160 // @todo
2161 uint8_t *pu8End = pu8 + VBOX_VIDEO_ADAPTER_INFORMATION_SIZE;
2162
2163 VBOXVIDEOINFOHDR *pHdr;
2164
2165 for (;;)
2166 {
2167 pHdr = (VBOXVIDEOINFOHDR *)pu8;
2168 pu8 += sizeof (VBOXVIDEOINFOHDR);
2169
2170 if (pu8 >= pu8End)
2171 {
2172 LogRel(("VBoxVideo: Guest adapter information overflow!!!\n"));
2173 break;
2174 }
2175
2176 if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_DISPLAY)
2177 {
2178 if (pHdr->u16Length != sizeof (VBOXVIDEOINFODISPLAY))
2179 {
2180 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "DISPLAY", pHdr->u16Length));
2181 break;
2182 }
2183
2184 VBOXVIDEOINFODISPLAY *pDisplay = (VBOXVIDEOINFODISPLAY *)pu8;
2185
2186 if (pDisplay->u32Index >= pDrv->pDisplay->mcMonitors)
2187 {
2188 LogRel(("VBoxVideo: Guest adapter information invalid display index %d!!!\n", pDisplay->u32Index));
2189 break;
2190 }
2191
2192 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[pDisplay->u32Index];
2193
2194 pFBInfo->u32Offset = pDisplay->u32Offset;
2195 pFBInfo->u32MaxFramebufferSize = pDisplay->u32FramebufferSize;
2196 pFBInfo->u32InformationSize = pDisplay->u32InformationSize;
2197
2198 LogFlow(("VBOX_VIDEO_INFO_TYPE_DISPLAY: %d: at 0x%08X, size 0x%08X, info 0x%08X\n", pDisplay->u32Index, pDisplay->u32Offset, pDisplay->u32FramebufferSize, pDisplay->u32InformationSize));
2199 }
2200 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_END)
2201 {
2202 if (pHdr->u16Length != 0)
2203 {
2204 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "END", pHdr->u16Length));
2205 break;
2206 }
2207
2208 break;
2209 }
2210 else
2211 {
2212 LogRel(("Guest adapter information contains unsupported type %d. The block has been skipped.\n", pHdr->u8Type));
2213 }
2214
2215 pu8 += pHdr->u16Length;
2216 }
2217 }
2218}
2219
2220/**
2221 * Display information change notification.
2222 *
2223 * @see PDMIDISPLAYCONNECTOR::pfnProcessDisplayData
2224 */
2225DECLCALLBACK(void) Display::displayProcessDisplayDataCallback(PPDMIDISPLAYCONNECTOR pInterface, void *pvVRAM, unsigned uScreenId)
2226{
2227 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2228
2229 if (uScreenId >= pDrv->pDisplay->mcMonitors)
2230 {
2231 LogRel(("VBoxVideo: Guest display information invalid display index %d!!!\n", uScreenId));
2232 return;
2233 }
2234
2235 /* Get the display information strcuture. */
2236 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[uScreenId];
2237
2238 uint8_t *pu8 = (uint8_t *)pvVRAM;
2239 pu8 += pFBInfo->u32Offset + pFBInfo->u32MaxFramebufferSize;
2240
2241 // @todo
2242 uint8_t *pu8End = pu8 + pFBInfo->u32InformationSize;
2243
2244 VBOXVIDEOINFOHDR *pHdr;
2245
2246 for (;;)
2247 {
2248 pHdr = (VBOXVIDEOINFOHDR *)pu8;
2249 pu8 += sizeof (VBOXVIDEOINFOHDR);
2250
2251 if (pu8 >= pu8End)
2252 {
2253 LogRel(("VBoxVideo: Guest display information overflow!!!\n"));
2254 break;
2255 }
2256
2257 if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_SCREEN)
2258 {
2259 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOSCREEN))
2260 {
2261 LogRel(("VBoxVideo: Guest display information %s invalid length %d!!!\n", "SCREEN", pHdr->u16Length));
2262 break;
2263 }
2264
2265 VBOXVIDEOINFOSCREEN *pScreen = (VBOXVIDEOINFOSCREEN *)pu8;
2266
2267 pFBInfo->xOrigin = pScreen->xOrigin;
2268 pFBInfo->yOrigin = pScreen->yOrigin;
2269
2270 pFBInfo->w = pScreen->u16Width;
2271 pFBInfo->h = pScreen->u16Height;
2272
2273 LogFlow(("VBOX_VIDEO_INFO_TYPE_SCREEN: (%p) %d: at %d,%d, linesize 0x%X, size %dx%d, bpp %d, flags 0x%02X\n",
2274 pHdr, uScreenId, pScreen->xOrigin, pScreen->yOrigin, pScreen->u32LineSize, pScreen->u16Width, pScreen->u16Height, pScreen->bitsPerPixel, pScreen->u8Flags));
2275
2276 if (uScreenId != VBOX_VIDEO_PRIMARY_SCREEN)
2277 {
2278 /* Primary screen resize is initiated by the VGA device. */
2279 pDrv->pDisplay->handleDisplayResize(uScreenId, pScreen->bitsPerPixel, (uint8_t *)pvVRAM + pFBInfo->u32Offset, pScreen->u32LineSize, pScreen->u16Width, pScreen->u16Height);
2280 }
2281 }
2282 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_END)
2283 {
2284 if (pHdr->u16Length != 0)
2285 {
2286 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "END", pHdr->u16Length));
2287 break;
2288 }
2289
2290 break;
2291 }
2292 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_HOST_EVENTS)
2293 {
2294 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOHOSTEVENTS))
2295 {
2296 LogRel(("VBoxVideo: Guest display information %s invalid length %d!!!\n", "HOST_EVENTS", pHdr->u16Length));
2297 break;
2298 }
2299
2300 VBOXVIDEOINFOHOSTEVENTS *pHostEvents = (VBOXVIDEOINFOHOSTEVENTS *)pu8;
2301
2302 pFBInfo->pHostEvents = pHostEvents;
2303
2304 LogFlow(("VBOX_VIDEO_INFO_TYPE_HOSTEVENTS: (%p)\n",
2305 pHostEvents));
2306 }
2307 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_LINK)
2308 {
2309 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOLINK))
2310 {
2311 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "LINK", pHdr->u16Length));
2312 break;
2313 }
2314
2315 VBOXVIDEOINFOLINK *pLink = (VBOXVIDEOINFOLINK *)pu8;
2316 pu8 += pLink->i32Offset;
2317 }
2318 else
2319 {
2320 LogRel(("Guest display information contains unsupported type %d\n", pHdr->u8Type));
2321 }
2322
2323 pu8 += pHdr->u16Length;
2324 }
2325}
2326
2327/**
2328 * Queries an interface to the driver.
2329 *
2330 * @returns Pointer to interface.
2331 * @returns NULL if the interface was not supported by the driver.
2332 * @param pInterface Pointer to this interface structure.
2333 * @param enmInterface The requested interface identification.
2334 */
2335DECLCALLBACK(void *) Display::drvQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
2336{
2337 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
2338 PDRVMAINDISPLAY pDrv = PDMINS2DATA(pDrvIns, PDRVMAINDISPLAY);
2339 switch (enmInterface)
2340 {
2341 case PDMINTERFACE_BASE:
2342 return &pDrvIns->IBase;
2343 case PDMINTERFACE_DISPLAY_CONNECTOR:
2344 return &pDrv->Connector;
2345 default:
2346 return NULL;
2347 }
2348}
2349
2350
2351/**
2352 * Destruct a display driver instance.
2353 *
2354 * @returns VBox status.
2355 * @param pDrvIns The driver instance data.
2356 */
2357DECLCALLBACK(void) Display::drvDestruct(PPDMDRVINS pDrvIns)
2358{
2359 PDRVMAINDISPLAY pData = PDMINS2DATA(pDrvIns, PDRVMAINDISPLAY);
2360 LogFlowFunc (("iInstance=%d\n", pDrvIns->iInstance));
2361 if (pData->pDisplay)
2362 {
2363 AutoLock displayLock (pData->pDisplay);
2364 pData->pDisplay->mpDrv = NULL;
2365 pData->pDisplay->mpVMMDev = NULL;
2366 pData->pDisplay->mLastAddress = NULL;
2367 pData->pDisplay->mLastBytesPerLine = 0;
2368 pData->pDisplay->mLastBitsPerPixel = 0,
2369 pData->pDisplay->mLastWidth = 0;
2370 pData->pDisplay->mLastHeight = 0;
2371 }
2372}
2373
2374
2375/**
2376 * Construct a display driver instance.
2377 *
2378 * @returns VBox status.
2379 * @param pDrvIns The driver instance data.
2380 * If the registration structure is needed, pDrvIns->pDrvReg points to it.
2381 * @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
2382 * of the driver instance. It's also found in pDrvIns->pCfgHandle, but like
2383 * iInstance it's expected to be used a bit in this function.
2384 */
2385DECLCALLBACK(int) Display::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle)
2386{
2387 PDRVMAINDISPLAY pData = PDMINS2DATA(pDrvIns, PDRVMAINDISPLAY);
2388 LogFlowFunc (("iInstance=%d\n", pDrvIns->iInstance));
2389
2390 /*
2391 * Validate configuration.
2392 */
2393 if (!CFGMR3AreValuesValid(pCfgHandle, "Object\0"))
2394 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
2395 PPDMIBASE pBaseIgnore;
2396 int rc = pDrvIns->pDrvHlp->pfnAttach(pDrvIns, &pBaseIgnore);
2397 if (rc != VERR_PDM_NO_ATTACHED_DRIVER)
2398 {
2399 AssertMsgFailed(("Configuration error: Not possible to attach anything to this driver!\n"));
2400 return VERR_PDM_DRVINS_NO_ATTACH;
2401 }
2402
2403 /*
2404 * Init Interfaces.
2405 */
2406 pDrvIns->IBase.pfnQueryInterface = Display::drvQueryInterface;
2407
2408 pData->Connector.pfnResize = Display::displayResizeCallback;
2409 pData->Connector.pfnUpdateRect = Display::displayUpdateCallback;
2410 pData->Connector.pfnRefresh = Display::displayRefreshCallback;
2411 pData->Connector.pfnReset = Display::displayResetCallback;
2412 pData->Connector.pfnLFBModeChange = Display::displayLFBModeChangeCallback;
2413 pData->Connector.pfnProcessAdapterData = Display::displayProcessAdapterDataCallback;
2414 pData->Connector.pfnProcessDisplayData = Display::displayProcessDisplayDataCallback;
2415
2416 /*
2417 * Get the IDisplayPort interface of the above driver/device.
2418 */
2419 pData->pUpPort = (PPDMIDISPLAYPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_DISPLAY_PORT);
2420 if (!pData->pUpPort)
2421 {
2422 AssertMsgFailed(("Configuration error: No display port interface above!\n"));
2423 return VERR_PDM_MISSING_INTERFACE_ABOVE;
2424 }
2425
2426 /*
2427 * Get the Display object pointer and update the mpDrv member.
2428 */
2429 void *pv;
2430 rc = CFGMR3QueryPtr(pCfgHandle, "Object", &pv);
2431 if (VBOX_FAILURE(rc))
2432 {
2433 AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Vrc\n", rc));
2434 return rc;
2435 }
2436 pData->pDisplay = (Display *)pv; /** @todo Check this cast! */
2437 pData->pDisplay->mpDrv = pData;
2438
2439 /*
2440 * Update our display information according to the framebuffer
2441 */
2442 pData->pDisplay->updateDisplayData();
2443
2444 /*
2445 * Start periodic screen refreshes
2446 */
2447 pData->pUpPort->pfnSetRefreshRate(pData->pUpPort, 20);
2448
2449 return VINF_SUCCESS;
2450}
2451
2452
2453/**
2454 * Display driver registration record.
2455 */
2456const PDMDRVREG Display::DrvReg =
2457{
2458 /* u32Version */
2459 PDM_DRVREG_VERSION,
2460 /* szDriverName */
2461 "MainDisplay",
2462 /* pszDescription */
2463 "Main display driver (Main as in the API).",
2464 /* fFlags */
2465 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
2466 /* fClass. */
2467 PDM_DRVREG_CLASS_DISPLAY,
2468 /* cMaxInstances */
2469 ~0,
2470 /* cbInstance */
2471 sizeof(DRVMAINDISPLAY),
2472 /* pfnConstruct */
2473 Display::drvConstruct,
2474 /* pfnDestruct */
2475 Display::drvDestruct,
2476 /* pfnIOCtl */
2477 NULL,
2478 /* pfnPowerOn */
2479 NULL,
2480 /* pfnReset */
2481 NULL,
2482 /* pfnSuspend */
2483 NULL,
2484 /* pfnResume */
2485 NULL,
2486 /* pfnDetach */
2487 NULL
2488};
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