VirtualBox

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

Last change on this file since 4753 was 4499, checked in by vboxsync, 17 years ago

Accidental commit

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