VirtualBox

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

Last change on this file since 3227 was 3227, checked in by vboxsync, 18 years ago

warning

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 73.8 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.215389.xyz. 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/pdm.h>
35#include <VBox/cfgm.h>
36#include <VBox/err.h>
37#include <VBox/vm.h>
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 mLastLineSize = 0;
89 mLastColorDepth = 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, FramebufferPixelFormat_T pixelFormat, void *pvVRAM, uint32_t cbLine, int w, int h)
225{
226 Assert (pFramebuffer);
227
228 /* Call the framebuffer to try and set required pixelFormat. */
229 BOOL finished = TRUE;
230
231 pFramebuffer->RequestResize (uScreenId, pixelFormat, (BYTE *) pvVRAM, 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, uint32_t cbLine, int w, int h)
255{
256 LogRel (("Display::handleDisplayResize(): uScreenId = %d, pvVRAM=%p w=%d h=%d bpp=%d cbLine=0x%X\n",
257 uScreenId, pvVRAM, w, h, bpp, cbLine));
258
259 /* If there is no framebuffer, this call is not interesting. */
260 if ( uScreenId >= mcMonitors
261 || maFramebuffers[uScreenId].pFramebuffer.isNull())
262 {
263 return VINF_SUCCESS;
264 }
265
266 mLastAddress = pvVRAM;
267 mLastLineSize = cbLine;
268 mLastColorDepth = bpp,
269 mLastWidth = w;
270 mLastHeight = h;
271
272 FramebufferPixelFormat_T pixelFormat;
273
274 switch (bpp)
275 {
276 case 32: pixelFormat = FramebufferPixelFormat_PixelFormatRGB32; break;
277 case 24: pixelFormat = FramebufferPixelFormat_PixelFormatRGB24; break;
278 case 16: pixelFormat = FramebufferPixelFormat_PixelFormatRGB16; break;
279 default: pixelFormat = FramebufferPixelFormat_PixelFormatDefault; cbLine = 0;
280 }
281
282 /* Atomically set the resize status before calling the framebuffer. The new InProgress status will
283 * disable access to the VGA device by the EMT thread.
284 */
285 bool f = ASMAtomicCmpXchgU32 (&maFramebuffers[uScreenId].u32ResizeStatus, ResizeStatus_InProgress, ResizeStatus_Void);
286 AssertReleaseMsg(f, ("f = %d\n", f));NOREF(f);
287
288 /* The framebuffer is locked in the state.
289 * The lock is kept, because the framebuffer is in undefined state.
290 */
291 maFramebuffers[uScreenId].pFramebuffer->Lock();
292
293 int rc = callFramebufferResize (maFramebuffers[uScreenId].pFramebuffer, uScreenId, pixelFormat, pvVRAM, cbLine, w, h);
294 if (rc == VINF_VGA_RESIZE_IN_PROGRESS)
295 {
296 /* Immediately return to the caller. ResizeCompleted will be called back by the
297 * GUI thread. The ResizeCompleted callback will change the resize status from
298 * InProgress to UpdateDisplayData. The latter status will be checked by the
299 * display timer callback on EMT and all required adjustments will be done there.
300 */
301 return rc;
302 }
303
304 /* Set the status so the 'handleResizeCompleted' would work. */
305 f = ASMAtomicCmpXchgU32 (&maFramebuffers[uScreenId].u32ResizeStatus, ResizeStatus_UpdateDisplayData, ResizeStatus_InProgress);
306 AssertRelease(f);NOREF(f);
307
308 /* The method also unlocks the framebuffer. */
309 handleResizeCompletedEMT();
310
311 return VINF_SUCCESS;
312}
313
314/**
315 * Framebuffer has been resized.
316 * Read the new display data and unlock the framebuffer.
317 *
318 * @thread EMT
319 */
320void Display::handleResizeCompletedEMT (void)
321{
322 LogFlowFunc(("\n"));
323
324 unsigned uScreenId;
325 for (uScreenId = 0; uScreenId < mcMonitors; uScreenId++)
326 {
327 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
328
329 /* Try to into non resizing state. */
330 bool f = ASMAtomicCmpXchgU32 (&pFBInfo->u32ResizeStatus, ResizeStatus_Void, ResizeStatus_UpdateDisplayData);
331
332 if (f == false)
333 {
334 /* This is not the display that has completed resizing. */
335 continue;
336 }
337
338 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN && !pFBInfo->pFramebuffer.isNull())
339 {
340 /* Primary framebuffer has completed the resize. Update the connector data for VGA device. */
341 updateDisplayData();
342
343 /* Check the framebuffer pixel format to setup the rendering in VGA device. */
344 FramebufferPixelFormat_T newPixelFormat;
345 pFBInfo->pFramebuffer->COMGETTER(PixelFormat) (&newPixelFormat);
346
347 pFBInfo->fDefaultFormat = (newPixelFormat == FramebufferPixelFormat_PixelFormatDefault);
348
349 mpDrv->pUpPort->pfnSetRenderVRAM (mpDrv->pUpPort, pFBInfo->fDefaultFormat);
350 }
351
352#ifdef DEBUG_sunlover
353 if (!stam)
354 {
355 /* protect mpVM */
356 Console::SafeVMPtr pVM (mParent);
357 AssertComRC (pVM.rc());
358
359 STAM_REG(pVM, &StatDisplayRefresh, STAMTYPE_PROFILE, "/PROF/Display/Refresh", STAMUNIT_TICKS_PER_CALL, "Time spent in EMT for display updates.");
360 stam = 1;
361 }
362#endif /* DEBUG_sunlover */
363
364 /* Inform VRDP server about the change of display parameters. */
365 LogFlowFunc (("Calling VRDP\n"));
366 mParent->consoleVRDPServer()->SendResize();
367
368 if (!pFBInfo->pFramebuffer.isNull())
369 {
370 /* Unlock framebuffer after evrything is done. */
371 pFBInfo->pFramebuffer->Unlock();
372 }
373 }
374}
375
376#ifndef VRDP_MC
377static void checkCoordBounds (int *px, int *py, int *pw, int *ph, int cx, int cy)
378{
379 /* Correct negative x and y coordinates. */
380 if (*px < 0)
381 {
382 *px += *pw; /* Compute xRight which is also the new width. */
383
384 *pw = (*px < 0)? 0: *px;
385
386 *px = 0;
387 }
388
389 if (*py < 0)
390 {
391 *py += *ph; /* Compute xBottom, which is also the new height. */
392
393 *ph = (*py < 0)? 0: *py;
394
395 *py = 0;
396 }
397
398 /* Also check if coords are greater than the display resolution. */
399 if (*px + *pw > cx)
400 {
401 *pw = cx > *px? cx - *px: 0;
402 }
403
404 if (*py + *ph > cy)
405 {
406 *ph = cy > *py? cy - *py: 0;
407 }
408}
409#endif
410
411unsigned mapCoordsToScreen(DISPLAYFBINFO *pInfos, unsigned cInfos, int *px, int *py, int *pw, int *ph)
412{
413 DISPLAYFBINFO *pInfo = pInfos;
414 unsigned uScreenId;
415 Log(("mapCoordsToScreen: %d,%d %dx%d\n", *px, *py, *pw, *ph));
416 for (uScreenId = 0; uScreenId < cInfos; uScreenId++, pInfo++)
417 {
418 Log((" [%d] %d,%d %dx%d\n", uScreenId, pInfo->xOrigin, pInfo->yOrigin, pInfo->w, pInfo->h));
419 if ( (pInfo->xOrigin <= *px && *px < pInfo->xOrigin + (int)pInfo->w)
420 && (pInfo->yOrigin <= *py && *py < pInfo->yOrigin + (int)pInfo->h))
421 {
422 /* The rectangle belongs to the screen. Correct coordinates. */
423 *px -= pInfo->xOrigin;
424 *py -= pInfo->yOrigin;
425 Log((" -> %d,%d", *px, *py));
426 break;
427 }
428 }
429 if (uScreenId == cInfos)
430 {
431 /* Map to primary screen. */
432 uScreenId = 0;
433 }
434 Log((" scr %d\n", uScreenId));
435 return uScreenId;
436}
437
438
439/**
440 * Handles display update event.
441 *
442 * @param x Update area x coordinate
443 * @param y Update area y coordinate
444 * @param w Update area width
445 * @param h Update area height
446 *
447 * @thread EMT
448 */
449void Display::handleDisplayUpdate (int x, int y, int w, int h)
450{
451#ifdef DEBUG_sunlover
452 LogFlowFunc (("%d,%d %dx%d (%d,%d)\n",
453 x, y, w, h, mpDrv->Connector.cx, mpDrv->Connector.cy));
454#endif /* DEBUG_sunlover */
455
456#ifdef VRDP_MC
457 unsigned uScreenId = mapCoordsToScreen(maFramebuffers, mcMonitors, &x, &y, &w, &h);
458#else
459 checkCoordBounds (&x, &y, &w, &h, mpDrv->Connector.cx, mpDrv->Connector.cy);
460#endif /* VRDP_MC */
461
462#ifdef DEBUG_sunlover
463 LogFlowFunc (("%d,%d %dx%d (checked)\n", x, y, w, h));
464#endif /* DEBUG_sunlover */
465
466 IFramebuffer *pFramebuffer = maFramebuffers[uScreenId].pFramebuffer;
467
468 // if there is no framebuffer, this call is not interesting
469 if (pFramebuffer == NULL)
470 return;
471
472 pFramebuffer->Lock();
473
474 /* special processing for the internal framebuffer */
475 if (mInternalFramebuffer)
476 {
477 pFramebuffer->Unlock();
478 } else
479 {
480 /* callback into the framebuffer to notify it */
481 BOOL finished = FALSE;
482
483 RTSemEventMultiReset(mUpdateSem);
484
485 pFramebuffer->NotifyUpdate(x, y, w, h, &finished);
486
487 if (!finished)
488 {
489 /*
490 * the framebuffer needs more time to process
491 * the event so we have to halt the VM until it's done
492 */
493 pFramebuffer->Unlock();
494 RTSemEventMultiWait(mUpdateSem, RT_INDEFINITE_WAIT);
495 } else
496 {
497 pFramebuffer->Unlock();
498 }
499
500 if (!mfVideoAccelEnabled)
501 {
502 /* When VBVA is enabled, the VRDP server is informed in the VideoAccelFlush.
503 * Inform the server here only if VBVA is disabled.
504 */
505 mParent->consoleVRDPServer()->SendUpdateBitmap(uScreenId, x, y, w, h);
506 }
507 }
508 return;
509}
510
511typedef struct _VBVADIRTYREGION
512{
513 /* Copies of object's pointers used by vbvaRgn functions. */
514 DISPLAYFBINFO *paFramebuffers;
515 unsigned cMonitors;
516 Display *pDisplay;
517 PPDMIDISPLAYPORT pPort;
518
519} VBVADIRTYREGION;
520
521static void vbvaRgnInit (VBVADIRTYREGION *prgn, DISPLAYFBINFO *paFramebuffers, unsigned cMonitors, Display *pd, PPDMIDISPLAYPORT pp)
522{
523 prgn->paFramebuffers = paFramebuffers;
524 prgn->cMonitors = cMonitors;
525 prgn->pDisplay = pd;
526 prgn->pPort = pp;
527
528 unsigned uScreenId;
529 for (uScreenId = 0; uScreenId < cMonitors; uScreenId++)
530 {
531 DISPLAYFBINFO *pFBInfo = &prgn->paFramebuffers[uScreenId];
532
533 memset (&pFBInfo->dirtyRect, 0, sizeof (pFBInfo->dirtyRect));
534 }
535}
536
537static void vbvaRgnDirtyRect (VBVADIRTYREGION *prgn, unsigned uScreenId, VBVACMDHDR *phdr)
538{
539 LogFlowFunc (("x = %d, y = %d, w = %d, h = %d\n",
540 phdr->x, phdr->y, phdr->w, phdr->h));
541
542 /*
543 * Here update rectangles are accumulated to form an update area.
544 * @todo
545 * Now the simpliest method is used which builds one rectangle that
546 * includes all update areas. A bit more advanced method can be
547 * employed here. The method should be fast however.
548 */
549 if (phdr->w == 0 || phdr->h == 0)
550 {
551 /* Empty rectangle. */
552 return;
553 }
554
555 int32_t xRight = phdr->x + phdr->w;
556 int32_t yBottom = phdr->y + phdr->h;
557
558 DISPLAYFBINFO *pFBInfo = &prgn->paFramebuffers[uScreenId];
559
560 if (pFBInfo->dirtyRect.xRight == 0)
561 {
562 /* This is the first rectangle to be added. */
563 pFBInfo->dirtyRect.xLeft = phdr->x;
564 pFBInfo->dirtyRect.yTop = phdr->y;
565 pFBInfo->dirtyRect.xRight = xRight;
566 pFBInfo->dirtyRect.yBottom = yBottom;
567 }
568 else
569 {
570 /* Adjust region coordinates. */
571 if (pFBInfo->dirtyRect.xLeft > phdr->x)
572 {
573 pFBInfo->dirtyRect.xLeft = phdr->x;
574 }
575
576 if (pFBInfo->dirtyRect.yTop > phdr->y)
577 {
578 pFBInfo->dirtyRect.yTop = phdr->y;
579 }
580
581 if (pFBInfo->dirtyRect.xRight < xRight)
582 {
583 pFBInfo->dirtyRect.xRight = xRight;
584 }
585
586 if (pFBInfo->dirtyRect.yBottom < yBottom)
587 {
588 pFBInfo->dirtyRect.yBottom = yBottom;
589 }
590 }
591
592 if (pFBInfo->fDefaultFormat)
593 {
594 //@todo pfnUpdateDisplayRect must take the vram offset parameter for the framebuffer
595 prgn->pPort->pfnUpdateDisplayRect (prgn->pPort, phdr->x, phdr->y, phdr->w, phdr->h);
596 prgn->pDisplay->handleDisplayUpdate (phdr->x, phdr->y, phdr->w, phdr->h);
597 }
598
599 return;
600}
601
602static void vbvaRgnUpdateFramebuffer (VBVADIRTYREGION *prgn, unsigned uScreenId)
603{
604 DISPLAYFBINFO *pFBInfo = &prgn->paFramebuffers[uScreenId];
605
606 uint32_t w = pFBInfo->dirtyRect.xRight - pFBInfo->dirtyRect.xLeft;
607 uint32_t h = pFBInfo->dirtyRect.yBottom - pFBInfo->dirtyRect.yTop;
608
609 if (!pFBInfo->fDefaultFormat && pFBInfo->pFramebuffer && w != 0 && h != 0)
610 {
611 //@todo pfnUpdateDisplayRect must take the vram offset parameter for the framebuffer
612 prgn->pPort->pfnUpdateDisplayRect (prgn->pPort, pFBInfo->dirtyRect.xLeft, pFBInfo->dirtyRect.yTop, w, h);
613 prgn->pDisplay->handleDisplayUpdate (pFBInfo->dirtyRect.xLeft, pFBInfo->dirtyRect.yTop, w, h);
614 }
615}
616
617static void vbvaSetMemoryFlags (VBVAMEMORY *pVbvaMemory,
618 bool fVideoAccelEnabled,
619 bool fVideoAccelVRDP,
620 uint32_t fu32SupportedOrders,
621 DISPLAYFBINFO *paFBInfos,
622 unsigned cFBInfos)
623{
624 if (pVbvaMemory)
625 {
626 /* This called only on changes in mode. So reset VRDP always. */
627 uint32_t fu32Flags = VBVA_F_MODE_VRDP_RESET;
628
629 if (fVideoAccelEnabled)
630 {
631 fu32Flags |= VBVA_F_MODE_ENABLED;
632
633 if (fVideoAccelVRDP)
634 {
635 fu32Flags |= VBVA_F_MODE_VRDP | VBVA_F_MODE_VRDP_ORDER_MASK;
636
637 pVbvaMemory->fu32SupportedOrders = fu32SupportedOrders;
638 }
639 }
640
641 pVbvaMemory->fu32ModeFlags = fu32Flags;
642 }
643
644 unsigned uScreenId;
645 for (uScreenId = 0; uScreenId < cFBInfos; uScreenId++)
646 {
647 if (paFBInfos[uScreenId].pHostEvents)
648 {
649 paFBInfos[uScreenId].pHostEvents->fu32Events |= VBOX_VIDEO_INFO_HOST_EVENTS_F_VRDP_RESET;
650 }
651 }
652}
653
654bool Display::VideoAccelAllowed (void)
655{
656 return true;
657}
658
659/**
660 * @thread EMT
661 */
662int Display::VideoAccelEnable (bool fEnable, VBVAMEMORY *pVbvaMemory)
663{
664 int rc = VINF_SUCCESS;
665
666 /* Called each time the guest wants to use acceleration,
667 * or when the VGA device disables acceleration,
668 * or when restoring the saved state with accel enabled.
669 *
670 * VGA device disables acceleration on each video mode change
671 * and on reset.
672 *
673 * Guest enabled acceleration at will. And it has to enable
674 * acceleration after a mode change.
675 */
676 LogFlowFunc (("mfVideoAccelEnabled = %d, fEnable = %d, pVbvaMemory = %p\n",
677 mfVideoAccelEnabled, fEnable, pVbvaMemory));
678
679 /* Strictly check parameters. Callers must not pass anything in the case. */
680 Assert((fEnable && pVbvaMemory) || (!fEnable && pVbvaMemory == NULL));
681
682 if (!VideoAccelAllowed ())
683 {
684 return VERR_NOT_SUPPORTED;
685 }
686
687 /*
688 * Verify that the VM is in running state. If it is not,
689 * then this must be postponed until it goes to running.
690 */
691 if (!mfMachineRunning)
692 {
693 Assert (!mfVideoAccelEnabled);
694
695 LogFlowFunc (("Machine is not yet running.\n"));
696
697 if (fEnable)
698 {
699 mfPendingVideoAccelEnable = fEnable;
700 mpPendingVbvaMemory = pVbvaMemory;
701 }
702
703 return rc;
704 }
705
706 /* Check that current status is not being changed */
707 if (mfVideoAccelEnabled == fEnable)
708 {
709 return rc;
710 }
711
712 if (mfVideoAccelEnabled)
713 {
714 /* Process any pending orders and empty the VBVA ring buffer. */
715 VideoAccelFlush ();
716 }
717
718 if (!fEnable && mpVbvaMemory)
719 {
720 mpVbvaMemory->fu32ModeFlags &= ~VBVA_F_MODE_ENABLED;
721 }
722
723 /* Safety precaution. There is no more VBVA until everything is setup! */
724 mpVbvaMemory = NULL;
725 mfVideoAccelEnabled = false;
726
727 /* Update entire display. */
728 if (maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN].u32ResizeStatus == ResizeStatus_Void)
729 {
730 mpDrv->pUpPort->pfnUpdateDisplayAll(mpDrv->pUpPort);
731 }
732
733 /* Everything OK. VBVA status can be changed. */
734
735 /* Notify the VMMDev, which saves VBVA status in the saved state,
736 * and needs to know current status.
737 */
738 PPDMIVMMDEVPORT pVMMDevPort = mParent->getVMMDev()->getVMMDevPort ();
739
740 if (pVMMDevPort)
741 {
742 pVMMDevPort->pfnVBVAChange (pVMMDevPort, fEnable);
743 }
744
745 if (fEnable)
746 {
747 mpVbvaMemory = pVbvaMemory;
748 mfVideoAccelEnabled = true;
749
750 /* Initialize the hardware memory. */
751 vbvaSetMemoryFlags (mpVbvaMemory, mfVideoAccelEnabled, mfVideoAccelVRDP, mfu32SupportedOrders, maFramebuffers, mcMonitors);
752 mpVbvaMemory->off32Data = 0;
753 mpVbvaMemory->off32Free = 0;
754
755 memset (mpVbvaMemory->aRecords, 0, sizeof (mpVbvaMemory->aRecords));
756 mpVbvaMemory->indexRecordFirst = 0;
757 mpVbvaMemory->indexRecordFree = 0;
758
759 LogRel(("VBVA: Enabled.\n"));
760 }
761 else
762 {
763 LogRel(("VBVA: Disabled.\n"));
764 }
765
766 LogFlowFunc (("VideoAccelEnable: rc = %Vrc.\n", rc));
767
768 return rc;
769}
770
771#ifdef VBOX_VRDP
772#ifdef VRDP_MC
773/* Called always by one VRDP server thread. Can be thread-unsafe.
774 */
775void Display::VideoAccelVRDP (bool fEnable)
776{
777#if 0
778 /* Supporting all orders. */
779 uint32_t fu32SupportedOrders = ~0;
780#endif
781
782 int c = fEnable?
783 ASMAtomicIncS32 (&mcVideoAccelVRDPRefs):
784 ASMAtomicDecS32 (&mcVideoAccelVRDPRefs);
785
786 Assert (c >= 0);
787
788 if (c == 0)
789 {
790 /* The last client has disconnected, and the accel can be
791 * disabled.
792 */
793 Assert (fEnable == false);
794
795 mfVideoAccelVRDP = false;
796 mfu32SupportedOrders = 0;
797
798 vbvaSetMemoryFlags (mpVbvaMemory, mfVideoAccelEnabled, mfVideoAccelVRDP, mfu32SupportedOrders, maFramebuffers, mcMonitors);
799
800 LogRel(("VBVA: VRDP acceleration has been disabled.\n"));
801 }
802 else if ( c == 1
803 && !mfVideoAccelVRDP)
804 {
805 /* The first client has connected. Enable the accel.
806 */
807 Assert (fEnable == true);
808
809 mfVideoAccelVRDP = true;
810 /* Supporting all orders. */
811 mfu32SupportedOrders = ~0;
812
813 vbvaSetMemoryFlags (mpVbvaMemory, mfVideoAccelEnabled, mfVideoAccelVRDP, mfu32SupportedOrders, maFramebuffers, mcMonitors);
814
815 LogRel(("VBVA: VRDP acceleration has been requested.\n"));
816 }
817 else
818 {
819 /* A client is connected or disconnected but there is no change in the
820 * accel state. It remains enabled.
821 */
822 Assert (mfVideoAccelVRDP == true);
823 }
824}
825#else
826void Display::VideoAccelVRDP (bool fEnable, uint32_t fu32SupportedOrders)
827{
828 Assert (mfVideoAccelVRDP != fEnable);
829
830 mfVideoAccelVRDP = fEnable;
831
832 if (fEnable)
833 {
834 mfu32SupportedOrders = fu32SupportedOrders;
835 }
836 else
837 {
838 mfu32SupportedOrders = 0;
839 }
840
841 vbvaSetMemoryFlags (mpVbvaMemory, mfVideoAccelEnabled, mfVideoAccelVRDP, mfu32SupportedOrders, maFramebuffers, mcMonitors);
842
843 LogRel(("VBVA: VRDP acceleration has been %s.\n", fEnable? "requested": "disabled"));
844}
845#endif /* VRDP_MC */
846#endif /* VBOX_VRDP */
847
848static bool vbvaVerifyRingBuffer (VBVAMEMORY *pVbvaMemory)
849{
850 return true;
851}
852
853static void vbvaFetchBytes (VBVAMEMORY *pVbvaMemory, uint8_t *pu8Dst, uint32_t cbDst)
854{
855 if (cbDst >= VBVA_RING_BUFFER_SIZE)
856 {
857 AssertMsgFailed (("cbDst = 0x%08X, ring buffer size 0x%08X", cbDst, VBVA_RING_BUFFER_SIZE));
858 return;
859 }
860
861 uint32_t u32BytesTillBoundary = VBVA_RING_BUFFER_SIZE - pVbvaMemory->off32Data;
862 uint8_t *src = &pVbvaMemory->au8RingBuffer[pVbvaMemory->off32Data];
863 int32_t i32Diff = cbDst - u32BytesTillBoundary;
864
865 if (i32Diff <= 0)
866 {
867 /* Chunk will not cross buffer boundary. */
868 memcpy (pu8Dst, src, cbDst);
869 }
870 else
871 {
872 /* Chunk crosses buffer boundary. */
873 memcpy (pu8Dst, src, u32BytesTillBoundary);
874 memcpy (pu8Dst + u32BytesTillBoundary, &pVbvaMemory->au8RingBuffer[0], i32Diff);
875 }
876
877 /* Advance data offset. */
878 pVbvaMemory->off32Data = (pVbvaMemory->off32Data + cbDst) % VBVA_RING_BUFFER_SIZE;
879
880 return;
881}
882
883
884static bool vbvaPartialRead (uint8_t **ppu8, uint32_t *pcb, uint32_t cbRecord, VBVAMEMORY *pVbvaMemory)
885{
886 uint8_t *pu8New;
887
888 LogFlow(("MAIN::DisplayImpl::vbvaPartialRead: p = %p, cb = %d, cbRecord 0x%08X\n",
889 *ppu8, *pcb, cbRecord));
890
891 if (*ppu8)
892 {
893 Assert (*pcb);
894 pu8New = (uint8_t *)RTMemRealloc (*ppu8, cbRecord);
895 }
896 else
897 {
898 Assert (!*pcb);
899 pu8New = (uint8_t *)RTMemAlloc (cbRecord);
900 }
901
902 if (!pu8New)
903 {
904 /* Memory allocation failed, fail the function. */
905 Log(("MAIN::vbvaPartialRead: failed to (re)alocate memory for partial record!!! cbRecord 0x%08X\n",
906 cbRecord));
907
908 if (*ppu8)
909 {
910 RTMemFree (*ppu8);
911 }
912
913 *ppu8 = NULL;
914 *pcb = 0;
915
916 return false;
917 }
918
919 /* Fetch data from the ring buffer. */
920 vbvaFetchBytes (pVbvaMemory, pu8New + *pcb, cbRecord - *pcb);
921
922 *ppu8 = pu8New;
923 *pcb = cbRecord;
924
925 return true;
926}
927
928/* For contiguous chunks just return the address in the buffer.
929 * For crossing boundary - allocate a buffer from heap.
930 */
931bool Display::vbvaFetchCmd (VBVACMDHDR **ppHdr, uint32_t *pcbCmd)
932{
933 uint32_t indexRecordFirst = mpVbvaMemory->indexRecordFirst;
934 uint32_t indexRecordFree = mpVbvaMemory->indexRecordFree;
935
936#ifdef DEBUG_sunlover
937 LogFlowFunc (("first = %d, free = %d\n",
938 indexRecordFirst, indexRecordFree));
939#endif /* DEBUG_sunlover */
940
941 if (!vbvaVerifyRingBuffer (mpVbvaMemory))
942 {
943 return false;
944 }
945
946 if (indexRecordFirst == indexRecordFree)
947 {
948 /* No records to process. Return without assigning output variables. */
949 return true;
950 }
951
952 VBVARECORD *pRecord = &mpVbvaMemory->aRecords[indexRecordFirst];
953
954#ifdef DEBUG_sunlover
955 LogFlowFunc (("cbRecord = 0x%08X\n", pRecord->cbRecord));
956#endif /* DEBUG_sunlover */
957
958 uint32_t cbRecord = pRecord->cbRecord & ~VBVA_F_RECORD_PARTIAL;
959
960 if (mcbVbvaPartial)
961 {
962 /* There is a partial read in process. Continue with it. */
963
964 Assert (mpu8VbvaPartial);
965
966 LogFlowFunc (("continue partial record mcbVbvaPartial = %d cbRecord 0x%08X, first = %d, free = %d\n",
967 mcbVbvaPartial, pRecord->cbRecord, indexRecordFirst, indexRecordFree));
968
969 if (cbRecord > mcbVbvaPartial)
970 {
971 /* New data has been added to the record. */
972 if (!vbvaPartialRead (&mpu8VbvaPartial, &mcbVbvaPartial, cbRecord, mpVbvaMemory))
973 {
974 return false;
975 }
976 }
977
978 if (!(pRecord->cbRecord & VBVA_F_RECORD_PARTIAL))
979 {
980 /* The record is completed by guest. Return it to the caller. */
981 *ppHdr = (VBVACMDHDR *)mpu8VbvaPartial;
982 *pcbCmd = mcbVbvaPartial;
983
984 mpu8VbvaPartial = NULL;
985 mcbVbvaPartial = 0;
986
987 /* Advance the record index. */
988 mpVbvaMemory->indexRecordFirst = (indexRecordFirst + 1) % VBVA_MAX_RECORDS;
989
990#ifdef DEBUG_sunlover
991 LogFlowFunc (("partial done ok, data = %d, free = %d\n",
992 mpVbvaMemory->off32Data, mpVbvaMemory->off32Free));
993#endif /* DEBUG_sunlover */
994 }
995
996 return true;
997 }
998
999 /* A new record need to be processed. */
1000 if (pRecord->cbRecord & VBVA_F_RECORD_PARTIAL)
1001 {
1002 /* Current record is being written by guest. '=' is important here. */
1003 if (cbRecord >= VBVA_RING_BUFFER_SIZE - VBVA_RING_BUFFER_THRESHOLD)
1004 {
1005 /* Partial read must be started. */
1006 if (!vbvaPartialRead (&mpu8VbvaPartial, &mcbVbvaPartial, cbRecord, mpVbvaMemory))
1007 {
1008 return false;
1009 }
1010
1011 LogFlowFunc (("started partial record mcbVbvaPartial = 0x%08X cbRecord 0x%08X, first = %d, free = %d\n",
1012 mcbVbvaPartial, pRecord->cbRecord, indexRecordFirst, indexRecordFree));
1013 }
1014
1015 return true;
1016 }
1017
1018 /* Current record is complete. If it is not empty, process it. */
1019 if (cbRecord)
1020 {
1021 /* The size of largest contiguos chunk in the ring biffer. */
1022 uint32_t u32BytesTillBoundary = VBVA_RING_BUFFER_SIZE - mpVbvaMemory->off32Data;
1023
1024 /* The ring buffer pointer. */
1025 uint8_t *au8RingBuffer = &mpVbvaMemory->au8RingBuffer[0];
1026
1027 /* The pointer to data in the ring buffer. */
1028 uint8_t *src = &au8RingBuffer[mpVbvaMemory->off32Data];
1029
1030 /* Fetch or point the data. */
1031 if (u32BytesTillBoundary >= cbRecord)
1032 {
1033 /* The command does not cross buffer boundary. Return address in the buffer. */
1034 *ppHdr = (VBVACMDHDR *)src;
1035
1036 /* Advance data offset. */
1037 mpVbvaMemory->off32Data = (mpVbvaMemory->off32Data + cbRecord) % VBVA_RING_BUFFER_SIZE;
1038 }
1039 else
1040 {
1041 /* The command crosses buffer boundary. Rare case, so not optimized. */
1042 uint8_t *dst = (uint8_t *)RTMemAlloc (cbRecord);
1043
1044 if (!dst)
1045 {
1046 LogFlowFunc (("could not allocate %d bytes from heap!!!\n", cbRecord));
1047 mpVbvaMemory->off32Data = (mpVbvaMemory->off32Data + cbRecord) % VBVA_RING_BUFFER_SIZE;
1048 return false;
1049 }
1050
1051 vbvaFetchBytes (mpVbvaMemory, dst, cbRecord);
1052
1053 *ppHdr = (VBVACMDHDR *)dst;
1054
1055#ifdef DEBUG_sunlover
1056 LogFlowFunc (("Allocated from heap %p\n", dst));
1057#endif /* DEBUG_sunlover */
1058 }
1059 }
1060
1061 *pcbCmd = cbRecord;
1062
1063 /* Advance the record index. */
1064 mpVbvaMemory->indexRecordFirst = (indexRecordFirst + 1) % VBVA_MAX_RECORDS;
1065
1066#ifdef DEBUG_sunlover
1067 LogFlowFunc (("done ok, data = %d, free = %d\n",
1068 mpVbvaMemory->off32Data, mpVbvaMemory->off32Free));
1069#endif /* DEBUG_sunlover */
1070
1071 return true;
1072}
1073
1074void Display::vbvaReleaseCmd (VBVACMDHDR *pHdr, int32_t cbCmd)
1075{
1076 uint8_t *au8RingBuffer = mpVbvaMemory->au8RingBuffer;
1077
1078 if ( (uint8_t *)pHdr >= au8RingBuffer
1079 && (uint8_t *)pHdr < &au8RingBuffer[VBVA_RING_BUFFER_SIZE])
1080 {
1081 /* The pointer is inside ring buffer. Must be continuous chunk. */
1082 Assert (VBVA_RING_BUFFER_SIZE - ((uint8_t *)pHdr - au8RingBuffer) >= cbCmd);
1083
1084 /* Do nothing. */
1085
1086 Assert (!mpu8VbvaPartial && mcbVbvaPartial == 0);
1087 }
1088 else
1089 {
1090 /* The pointer is outside. It is then an allocated copy. */
1091
1092#ifdef DEBUG_sunlover
1093 LogFlowFunc (("Free heap %p\n", pHdr));
1094#endif /* DEBUG_sunlover */
1095
1096 if ((uint8_t *)pHdr == mpu8VbvaPartial)
1097 {
1098 mpu8VbvaPartial = NULL;
1099 mcbVbvaPartial = 0;
1100 }
1101 else
1102 {
1103 Assert (!mpu8VbvaPartial && mcbVbvaPartial == 0);
1104 }
1105
1106 RTMemFree (pHdr);
1107 }
1108
1109 return;
1110}
1111
1112
1113/**
1114 * Called regularly on the DisplayRefresh timer.
1115 * Also on behalf of guest, when the ring buffer is full.
1116 *
1117 * @thread EMT
1118 */
1119void Display::VideoAccelFlush (void)
1120{
1121#ifdef DEBUG_sunlover
1122 LogFlowFunc (("mfVideoAccelEnabled = %d\n", mfVideoAccelEnabled));
1123#endif /* DEBUG_sunlover */
1124
1125 if (!mfVideoAccelEnabled)
1126 {
1127 Log(("Display::VideoAccelFlush: called with disabled VBVA!!! Ignoring.\n"));
1128 return;
1129 }
1130
1131 /* Here VBVA is enabled and we have the accelerator memory pointer. */
1132 Assert(mpVbvaMemory);
1133
1134#ifdef DEBUG_sunlover
1135 LogFlowFunc (("indexRecordFirst = %d, indexRecordFree = %d, off32Data = %d, off32Free = %d\n",
1136 mpVbvaMemory->indexRecordFirst, mpVbvaMemory->indexRecordFree, mpVbvaMemory->off32Data, mpVbvaMemory->off32Free));
1137#endif /* DEBUG_sunlover */
1138
1139 /* Quick check for "nothing to update" case. */
1140 if (mpVbvaMemory->indexRecordFirst == mpVbvaMemory->indexRecordFree)
1141 {
1142 return;
1143 }
1144
1145 /* Process the ring buffer */
1146 unsigned uScreenId;
1147 for (uScreenId = 0; uScreenId < mcMonitors; uScreenId++)
1148 {
1149 if (!maFramebuffers[uScreenId].pFramebuffer.isNull())
1150 {
1151 maFramebuffers[uScreenId].pFramebuffer->Lock ();
1152 }
1153 }
1154
1155 /* Initialize dirty rectangles accumulator. */
1156 VBVADIRTYREGION rgn;
1157 vbvaRgnInit (&rgn, maFramebuffers, mcMonitors, this, mpDrv->pUpPort);
1158
1159 for (;;)
1160 {
1161 VBVACMDHDR *phdr = NULL;
1162 uint32_t cbCmd = ~0;
1163
1164 /* Fetch the command data. */
1165 if (!vbvaFetchCmd (&phdr, &cbCmd))
1166 {
1167 Log(("Display::VideoAccelFlush: unable to fetch command. off32Data = %d, off32Free = %d. Disabling VBVA!!!\n",
1168 mpVbvaMemory->off32Data, mpVbvaMemory->off32Free));
1169
1170 /* Disable VBVA on those processing errors. */
1171 VideoAccelEnable (false, NULL);
1172
1173 break;
1174 }
1175
1176 if (cbCmd == uint32_t(~0))
1177 {
1178 /* No more commands yet in the queue. */
1179 break;
1180 }
1181
1182 if (cbCmd != 0)
1183 {
1184#ifdef DEBUG_sunlover
1185 LogFlowFunc (("hdr: cbCmd = %d, x=%d, y=%d, w=%d, h=%d\n",
1186 cbCmd, phdr->x, phdr->y, phdr->w, phdr->h));
1187#endif /* DEBUG_sunlover */
1188
1189 VBVACMDHDR hdrSaved = *phdr;
1190
1191 int x = phdr->x;
1192 int y = phdr->y;
1193 int w = phdr->w;
1194 int h = phdr->h;
1195
1196 uScreenId = mapCoordsToScreen(maFramebuffers, mcMonitors, &x, &y, &w, &h);
1197
1198 phdr->x = (int16_t)x;
1199 phdr->y = (int16_t)y;
1200 phdr->w = (uint16_t)w;
1201 phdr->h = (uint16_t)h;
1202
1203 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
1204
1205 if (pFBInfo->u32ResizeStatus == ResizeStatus_Void)
1206 {
1207 /* Handle the command.
1208 *
1209 * Guest is responsible for updating the guest video memory.
1210 * The Windows guest does all drawing using Eng*.
1211 *
1212 * For local output, only dirty rectangle information is used
1213 * to update changed areas.
1214 *
1215 * Dirty rectangles are accumulated to exclude overlapping updates and
1216 * group small updates to a larger one.
1217 */
1218
1219 /* Accumulate the update. */
1220 vbvaRgnDirtyRect (&rgn, uScreenId, phdr);
1221
1222 /* Forward the command to VRDP server. */
1223 mParent->consoleVRDPServer()->SendUpdate (uScreenId, phdr, cbCmd);
1224
1225 *phdr = hdrSaved;
1226 }
1227 }
1228
1229 vbvaReleaseCmd (phdr, cbCmd);
1230 }
1231
1232 for (uScreenId = 0; uScreenId < mcMonitors; uScreenId++)
1233 {
1234 if (!maFramebuffers[uScreenId].pFramebuffer.isNull())
1235 {
1236 maFramebuffers[uScreenId].pFramebuffer->Unlock ();
1237 }
1238
1239 if (maFramebuffers[uScreenId].u32ResizeStatus == ResizeStatus_Void)
1240 {
1241 /* Draw the framebuffer. */
1242 vbvaRgnUpdateFramebuffer (&rgn, uScreenId);
1243 }
1244 }
1245}
1246
1247
1248// IDisplay properties
1249/////////////////////////////////////////////////////////////////////////////
1250
1251/**
1252 * Returns the current display width in pixel
1253 *
1254 * @returns COM status code
1255 * @param width Address of result variable.
1256 */
1257STDMETHODIMP Display::COMGETTER(Width) (ULONG *width)
1258{
1259 if (!width)
1260 return E_POINTER;
1261
1262 AutoLock alock (this);
1263 CHECK_READY();
1264
1265 CHECK_CONSOLE_DRV (mpDrv);
1266
1267 *width = mpDrv->Connector.cx;
1268 return S_OK;
1269}
1270
1271/**
1272 * Returns the current display height in pixel
1273 *
1274 * @returns COM status code
1275 * @param height Address of result variable.
1276 */
1277STDMETHODIMP Display::COMGETTER(Height) (ULONG *height)
1278{
1279 if (!height)
1280 return E_POINTER;
1281
1282 AutoLock alock (this);
1283 CHECK_READY();
1284
1285 CHECK_CONSOLE_DRV (mpDrv);
1286
1287 *height = mpDrv->Connector.cy;
1288 return S_OK;
1289}
1290
1291/**
1292 * Returns the current display color depth in bits
1293 *
1294 * @returns COM status code
1295 * @param colorDepth Address of result variable.
1296 */
1297STDMETHODIMP Display::COMGETTER(ColorDepth) (ULONG *colorDepth)
1298{
1299 if (!colorDepth)
1300 return E_INVALIDARG;
1301
1302 AutoLock alock (this);
1303 CHECK_READY();
1304
1305 CHECK_CONSOLE_DRV (mpDrv);
1306
1307 uint32_t cBits = 0;
1308 int rc = mpDrv->pUpPort->pfnQueryColorDepth(mpDrv->pUpPort, &cBits);
1309 AssertRC(rc);
1310 *colorDepth = cBits;
1311 return S_OK;
1312}
1313
1314
1315// IDisplay methods
1316/////////////////////////////////////////////////////////////////////////////
1317
1318STDMETHODIMP Display::SetupInternalFramebuffer (ULONG depth)
1319{
1320 LogFlowFunc (("\n"));
1321
1322 AutoLock lock (this);
1323 CHECK_READY();
1324
1325 /*
1326 * Create an internal framebuffer only if depth is not zero. Otherwise, we
1327 * reset back to the "black hole" state as it was at Display construction.
1328 */
1329 ComPtr <IFramebuffer> frameBuf;
1330 if (depth)
1331 {
1332 ComObjPtr <InternalFramebuffer> internal;
1333 internal.createObject();
1334 internal->init (640, 480, depth);
1335 frameBuf = internal; // query interface
1336 }
1337
1338 Console::SafeVMPtrQuiet pVM (mParent);
1339 if (pVM.isOk())
1340 {
1341 /* Must leave the lock here because the changeFramebuffer will also obtain it. */
1342 lock.leave ();
1343
1344 /* send request to the EMT thread */
1345 PVMREQ pReq = NULL;
1346 int vrc = VMR3ReqCall (pVM, &pReq, RT_INDEFINITE_WAIT,
1347 (PFNRT) changeFramebuffer, 3,
1348 this, static_cast <IFramebuffer *> (frameBuf),
1349 true /* aInternal */, VBOX_VIDEO_PRIMARY_SCREEN);
1350 if (VBOX_SUCCESS (vrc))
1351 vrc = pReq->iStatus;
1352 VMR3ReqFree (pReq);
1353
1354 lock.enter ();
1355
1356 ComAssertRCRet (vrc, E_FAIL);
1357 }
1358 else
1359 {
1360 /* No VM is created (VM is powered off), do a direct call */
1361 int vrc = changeFramebuffer (this, frameBuf, true /* aInternal */, VBOX_VIDEO_PRIMARY_SCREEN);
1362 ComAssertRCRet (vrc, E_FAIL);
1363 }
1364
1365 return S_OK;
1366}
1367
1368STDMETHODIMP Display::LockFramebuffer (BYTE **address)
1369{
1370 if (!address)
1371 return E_POINTER;
1372
1373 AutoLock lock(this);
1374 CHECK_READY();
1375
1376 /* only allowed for internal framebuffers */
1377 if (mInternalFramebuffer && !mFramebufferOpened && !maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN].pFramebuffer.isNull())
1378 {
1379 CHECK_CONSOLE_DRV (mpDrv);
1380
1381 maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN].pFramebuffer->Lock();
1382 mFramebufferOpened = true;
1383 *address = mpDrv->Connector.pu8Data;
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::UnlockFramebuffer()
1392{
1393 AutoLock lock(this);
1394 CHECK_READY();
1395
1396 if (mFramebufferOpened)
1397 {
1398 CHECK_CONSOLE_DRV (mpDrv);
1399
1400 maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN].pFramebuffer->Unlock();
1401 mFramebufferOpened = false;
1402 return S_OK;
1403 }
1404
1405 return setError (E_FAIL,
1406 tr ("Framebuffer locking is allowed only for the internal framebuffer"));
1407}
1408
1409STDMETHODIMP Display::RegisterExternalFramebuffer (IFramebuffer *frameBuf)
1410{
1411 LogFlowFunc (("\n"));
1412
1413 if (!frameBuf)
1414 return E_POINTER;
1415
1416 AutoLock lock (this);
1417 CHECK_READY();
1418
1419 Console::SafeVMPtrQuiet pVM (mParent);
1420 if (pVM.isOk())
1421 {
1422 /* Must leave the lock here because the changeFramebuffer will also obtain it. */
1423 lock.leave ();
1424
1425 /* send request to the EMT thread */
1426 PVMREQ pReq = NULL;
1427 int vrc = VMR3ReqCall (pVM, &pReq, RT_INDEFINITE_WAIT,
1428 (PFNRT) changeFramebuffer, 3,
1429 this, frameBuf, false /* aInternal */, VBOX_VIDEO_PRIMARY_SCREEN);
1430 if (VBOX_SUCCESS (vrc))
1431 vrc = pReq->iStatus;
1432 VMR3ReqFree (pReq);
1433
1434 lock.enter ();
1435
1436 ComAssertRCRet (vrc, E_FAIL);
1437 }
1438 else
1439 {
1440 /* No VM is created (VM is powered off), do a direct call */
1441 int vrc = changeFramebuffer (this, frameBuf, false /* aInternal */, VBOX_VIDEO_PRIMARY_SCREEN);
1442 ComAssertRCRet (vrc, E_FAIL);
1443 }
1444
1445 return S_OK;
1446}
1447
1448STDMETHODIMP Display::SetFramebuffer (ULONG aScreenId, IFramebuffer * aFramebuffer)
1449{
1450 LogFlowFunc (("\n"));
1451
1452 if (!aFramebuffer)
1453 return E_POINTER;
1454
1455 AutoLock lock (this);
1456 CHECK_READY();
1457
1458 Console::SafeVMPtrQuiet pVM (mParent);
1459 if (pVM.isOk())
1460 {
1461 /* Must leave the lock here because the changeFramebuffer will also obtain it. */
1462 lock.leave ();
1463
1464 /* send request to the EMT thread */
1465 PVMREQ pReq = NULL;
1466 int vrc = VMR3ReqCall (pVM, &pReq, RT_INDEFINITE_WAIT,
1467 (PFNRT) changeFramebuffer, 3,
1468 this, aFramebuffer, false /* aInternal */, aScreenId);
1469 if (VBOX_SUCCESS (vrc))
1470 vrc = pReq->iStatus;
1471 VMR3ReqFree (pReq);
1472
1473 lock.enter ();
1474
1475 ComAssertRCRet (vrc, E_FAIL);
1476 }
1477 else
1478 {
1479 /* No VM is created (VM is powered off), do a direct call */
1480 int vrc = changeFramebuffer (this, aFramebuffer, false /* aInternal */, aScreenId);
1481 ComAssertRCRet (vrc, E_FAIL);
1482 }
1483
1484 return S_OK;
1485}
1486
1487STDMETHODIMP Display::QueryFramebuffer (ULONG aScreenId, IFramebuffer * * aFramebuffer, LONG * aXOrigin, LONG * aYOrigin)
1488{
1489 LogFlowFunc (("aScreenId = %d\n", aScreenId));
1490
1491 if (!aFramebuffer)
1492 return E_POINTER;
1493
1494 AutoLock lock (this);
1495 CHECK_READY();
1496
1497 /* @todo this should be actually done on EMT. */
1498 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
1499
1500 *aFramebuffer = pFBInfo->pFramebuffer;
1501 if (*aFramebuffer)
1502 (*aFramebuffer)->AddRef ();
1503 if (aXOrigin)
1504 *aXOrigin = pFBInfo->xOrigin;
1505 if (aYOrigin)
1506 *aYOrigin = pFBInfo->yOrigin;
1507
1508 return S_OK;
1509}
1510
1511STDMETHODIMP Display::SetVideoModeHint(ULONG aWidth, ULONG aHeight, ULONG aColorDepth, ULONG aDisplay)
1512{
1513 AutoLock lock(this);
1514 CHECK_READY();
1515
1516 CHECK_CONSOLE_DRV (mpDrv);
1517
1518 /*
1519 * Do some rough checks for valid input
1520 */
1521 ULONG width = aWidth;
1522 if (!width)
1523 width = mpDrv->Connector.cx;
1524 ULONG height = aHeight;
1525 if (!height)
1526 height = mpDrv->Connector.cy;
1527 ULONG bpp = aColorDepth;
1528 if (!bpp)
1529 {
1530 uint32_t cBits = 0;
1531 int rc = mpDrv->pUpPort->pfnQueryColorDepth(mpDrv->pUpPort, &cBits);
1532 AssertRC(rc);
1533 bpp = cBits;
1534 }
1535 ULONG cMonitors;
1536 mParent->machine()->COMGETTER(MonitorCount)(&cMonitors);
1537 if (cMonitors == 0 && aDisplay > 0)
1538 return E_INVALIDARG;
1539 if (aDisplay >= cMonitors)
1540 return E_INVALIDARG;
1541
1542// sunlover 20070614: It is up to the guest to decide whether the hint is valid.
1543// ULONG vramSize;
1544// mParent->machine()->COMGETTER(VRAMSize)(&vramSize);
1545// /* enough VRAM? */
1546// if ((width * height * (bpp / 8)) > (vramSize * 1024 * 1024))
1547// return setError(E_FAIL, tr("Not enough VRAM for the selected video mode"));
1548
1549 if (mParent->getVMMDev())
1550 mParent->getVMMDev()->getVMMDevPort()->pfnRequestDisplayChange(mParent->getVMMDev()->getVMMDevPort(), aWidth, aHeight, aColorDepth, aDisplay);
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 lineSize = 0;
1834 rc = pFramebuffer->COMGETTER(LineSize) (&lineSize);
1835 AssertComRC (rc);
1836 ULONG colorDepth = 0;
1837 rc = pFramebuffer->COMGETTER(ColorDepth) (&colorDepth);
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 mLastLineSize != lineSize ||
1855 mLastColorDepth != colorDepth ||
1856 mLastWidth != (int) width ||
1857 mLastHeight != (int) height))
1858 {
1859 handleDisplayResize (VBOX_VIDEO_PRIMARY_SCREEN, mLastColorDepth,
1860 mLastAddress,
1861 mLastLineSize,
1862 mLastWidth,
1863 mLastHeight);
1864 return;
1865 }
1866
1867 mpDrv->Connector.pu8Data = (uint8_t *) address;
1868 mpDrv->Connector.cbScanline = lineSize;
1869 mpDrv->Connector.cBits = colorDepth;
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 (("pDrv->pDisplay->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
1991 LogFlowFunc (("pDrv->pDisplay->mfVideoAccelEnabled = %d\n",
1992 pDrv->pDisplay->mfVideoAccelEnabled));
1993#endif /* DEBUG_sunlover */
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#ifdef VRDP_MC
2077 /* Inform to VRDP server that the current display update sequence is
2078 * completed. At this moment the framebuffer memory contains a definite
2079 * image, that is synchronized with the orders already sent to VRDP client.
2080 * The server can now process redraw requests from clients or initial
2081 * fullscreen updates for new clients.
2082 */
2083 Assert (pDisplay->mParent && pDisplay->mParent->consoleVRDPServer());
2084 pDisplay->mParent->consoleVRDPServer()->SendUpdate (uScreenId, NULL, 0);
2085#endif /* VRDP_MC */
2086 }
2087 }
2088
2089#ifdef DEBUG_sunlover
2090 STAM_PROFILE_STOP(&StatDisplayRefresh, a);
2091 LogFlowFunc (("leave\n"));
2092#endif /* DEBUG_sunlover */
2093}
2094
2095/**
2096 * Reset notification
2097 *
2098 * @see PDMIDISPLAYCONNECTOR::pfnReset
2099 */
2100DECLCALLBACK(void) Display::displayResetCallback(PPDMIDISPLAYCONNECTOR pInterface)
2101{
2102 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2103
2104 LogFlowFunc (("\n"));
2105
2106 /* Disable VBVA mode. */
2107 pDrv->pDisplay->VideoAccelEnable (false, NULL);
2108}
2109
2110/**
2111 * LFBModeChange notification
2112 *
2113 * @see PDMIDISPLAYCONNECTOR::pfnLFBModeChange
2114 */
2115DECLCALLBACK(void) Display::displayLFBModeChangeCallback(PPDMIDISPLAYCONNECTOR pInterface, bool fEnabled)
2116{
2117 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2118
2119 LogFlowFunc (("fEnabled=%d\n", fEnabled));
2120
2121 NOREF(fEnabled);
2122
2123 /* Disable VBVA mode in any case. The guest driver reenables VBVA mode if necessary. */
2124 pDrv->pDisplay->VideoAccelEnable (false, NULL);
2125}
2126
2127/**
2128 * Adapter information change notification.
2129 *
2130 * @see PDMIDISPLAYCONNECTOR::pfnProcessAdapterData
2131 */
2132DECLCALLBACK(void) Display::displayProcessAdapterDataCallback(PPDMIDISPLAYCONNECTOR pInterface, void *pvVRAM, uint32_t u32VRAMSize)
2133{
2134 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2135
2136 if (pvVRAM == NULL)
2137 {
2138 unsigned i;
2139 for (i = 0; i < pDrv->pDisplay->mcMonitors; i++)
2140 {
2141 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[i];
2142
2143 pFBInfo->u32Offset = 0;
2144 pFBInfo->u32MaxFramebufferSize = 0;
2145 pFBInfo->u32InformationSize = 0;
2146 }
2147 }
2148 else
2149 {
2150 uint8_t *pu8 = (uint8_t *)pvVRAM;
2151 pu8 += u32VRAMSize - VBOX_VIDEO_ADAPTER_INFORMATION_SIZE;
2152
2153 // @todo
2154 uint8_t *pu8End = pu8 + VBOX_VIDEO_ADAPTER_INFORMATION_SIZE;
2155
2156 VBOXVIDEOINFOHDR *pHdr;
2157
2158 for (;;)
2159 {
2160 pHdr = (VBOXVIDEOINFOHDR *)pu8;
2161 pu8 += sizeof (VBOXVIDEOINFOHDR);
2162
2163 if (pu8 >= pu8End)
2164 {
2165 LogRel(("VBoxVideo: Guest adapter information overflow!!!\n"));
2166 break;
2167 }
2168
2169 if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_DISPLAY)
2170 {
2171 if (pHdr->u16Length != sizeof (VBOXVIDEOINFODISPLAY))
2172 {
2173 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "DISPLAY", pHdr->u16Length));
2174 break;
2175 }
2176
2177 VBOXVIDEOINFODISPLAY *pDisplay = (VBOXVIDEOINFODISPLAY *)pu8;
2178
2179 if (pDisplay->u32Index >= pDrv->pDisplay->mcMonitors)
2180 {
2181 LogRel(("VBoxVideo: Guest adapter information invalid display index %d!!!\n", pDisplay->u32Index));
2182 break;
2183 }
2184
2185 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[pDisplay->u32Index];
2186
2187 pFBInfo->u32Offset = pDisplay->u32Offset;
2188 pFBInfo->u32MaxFramebufferSize = pDisplay->u32FramebufferSize;
2189 pFBInfo->u32InformationSize = pDisplay->u32InformationSize;
2190
2191 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));
2192 }
2193 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_END)
2194 {
2195 if (pHdr->u16Length != 0)
2196 {
2197 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "END", pHdr->u16Length));
2198 break;
2199 }
2200
2201 break;
2202 }
2203 else
2204 {
2205 LogRel(("Guest adapter information contains unsupported type %d\n", pHdr->u8Type));
2206 }
2207
2208 pu8 += pHdr->u16Length;
2209 }
2210 }
2211}
2212
2213/**
2214 * Display information change notification.
2215 *
2216 * @see PDMIDISPLAYCONNECTOR::pfnProcessDisplayData
2217 */
2218DECLCALLBACK(void) Display::displayProcessDisplayDataCallback(PPDMIDISPLAYCONNECTOR pInterface, void *pvVRAM, unsigned uScreenId)
2219{
2220 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2221
2222 if (uScreenId >= pDrv->pDisplay->mcMonitors)
2223 {
2224 LogRel(("VBoxVideo: Guest display information invalid display index %d!!!\n", uScreenId));
2225 return;
2226 }
2227
2228 /* Get the display information strcuture. */
2229 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[uScreenId];
2230
2231 uint8_t *pu8 = (uint8_t *)pvVRAM;
2232 pu8 += pFBInfo->u32Offset + pFBInfo->u32MaxFramebufferSize;
2233
2234 // @todo
2235 uint8_t *pu8End = pu8 + pFBInfo->u32InformationSize;
2236
2237 VBOXVIDEOINFOHDR *pHdr;
2238
2239 for (;;)
2240 {
2241 pHdr = (VBOXVIDEOINFOHDR *)pu8;
2242 pu8 += sizeof (VBOXVIDEOINFOHDR);
2243
2244 if (pu8 >= pu8End)
2245 {
2246 LogRel(("VBoxVideo: Guest display information overflow!!!\n"));
2247 break;
2248 }
2249
2250 if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_SCREEN)
2251 {
2252 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOSCREEN))
2253 {
2254 LogRel(("VBoxVideo: Guest display information %s invalid length %d!!!\n", "SCREEN", pHdr->u16Length));
2255 break;
2256 }
2257
2258 VBOXVIDEOINFOSCREEN *pScreen = (VBOXVIDEOINFOSCREEN *)pu8;
2259
2260 pFBInfo->xOrigin = pScreen->xOrigin;
2261 pFBInfo->yOrigin = pScreen->yOrigin;
2262
2263 pFBInfo->w = pScreen->u16Width;
2264 pFBInfo->h = pScreen->u16Height;
2265
2266 LogFlow(("VBOX_VIDEO_INFO_TYPE_SCREEN: (%p) %d: at %d,%d, linesize 0x%X, size %dx%d, bpp %d, flags 0x%02X\n",
2267 pHdr, uScreenId, pScreen->xOrigin, pScreen->yOrigin, pScreen->u32LineSize, pScreen->u16Width, pScreen->u16Height, pScreen->bitsPerPixel, pScreen->u8Flags));
2268
2269 if (uScreenId != VBOX_VIDEO_PRIMARY_SCREEN)
2270 {
2271 /* Primary screen resize is initiated by the VGA device. */
2272 pDrv->pDisplay->handleDisplayResize(uScreenId, pScreen->bitsPerPixel, (uint8_t *)pvVRAM + pFBInfo->u32Offset, pScreen->u32LineSize, pScreen->u16Width, pScreen->u16Height);
2273 }
2274 }
2275 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_END)
2276 {
2277 if (pHdr->u16Length != 0)
2278 {
2279 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "END", pHdr->u16Length));
2280 break;
2281 }
2282
2283 break;
2284 }
2285 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_HOST_EVENTS)
2286 {
2287 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOHOSTEVENTS))
2288 {
2289 LogRel(("VBoxVideo: Guest display information %s invalid length %d!!!\n", "HOST_EVENTS", pHdr->u16Length));
2290 break;
2291 }
2292
2293 VBOXVIDEOINFOHOSTEVENTS *pHostEvents = (VBOXVIDEOINFOHOSTEVENTS *)pu8;
2294
2295 pFBInfo->pHostEvents = pHostEvents;
2296
2297 LogFlow(("VBOX_VIDEO_INFO_TYPE_HOSTEVENTS: (%p)\n",
2298 pHostEvents));
2299 }
2300 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_LINK)
2301 {
2302 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOLINK))
2303 {
2304 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "LINK", pHdr->u16Length));
2305 break;
2306 }
2307
2308 VBOXVIDEOINFOLINK *pLink = (VBOXVIDEOINFOLINK *)pu8;
2309 pu8 += pLink->i32Offset;
2310 }
2311 else
2312 {
2313 LogRel(("Guest display information contains unsupported type %d\n", pHdr->u8Type));
2314 }
2315
2316 pu8 += pHdr->u16Length;
2317 }
2318}
2319
2320/**
2321 * Queries an interface to the driver.
2322 *
2323 * @returns Pointer to interface.
2324 * @returns NULL if the interface was not supported by the driver.
2325 * @param pInterface Pointer to this interface structure.
2326 * @param enmInterface The requested interface identification.
2327 */
2328DECLCALLBACK(void *) Display::drvQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
2329{
2330 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
2331 PDRVMAINDISPLAY pDrv = PDMINS2DATA(pDrvIns, PDRVMAINDISPLAY);
2332 switch (enmInterface)
2333 {
2334 case PDMINTERFACE_BASE:
2335 return &pDrvIns->IBase;
2336 case PDMINTERFACE_DISPLAY_CONNECTOR:
2337 return &pDrv->Connector;
2338 default:
2339 return NULL;
2340 }
2341}
2342
2343
2344/**
2345 * Destruct a display driver instance.
2346 *
2347 * @returns VBox status.
2348 * @param pDrvIns The driver instance data.
2349 */
2350DECLCALLBACK(void) Display::drvDestruct(PPDMDRVINS pDrvIns)
2351{
2352 PDRVMAINDISPLAY pData = PDMINS2DATA(pDrvIns, PDRVMAINDISPLAY);
2353 LogFlowFunc (("iInstance=%d\n", pDrvIns->iInstance));
2354 if (pData->pDisplay)
2355 {
2356 AutoLock displayLock (pData->pDisplay);
2357 pData->pDisplay->mpDrv = NULL;
2358 pData->pDisplay->mpVMMDev = NULL;
2359 pData->pDisplay->mLastAddress = NULL;
2360 pData->pDisplay->mLastLineSize = 0;
2361 pData->pDisplay->mLastColorDepth = 0,
2362 pData->pDisplay->mLastWidth = 0;
2363 pData->pDisplay->mLastHeight = 0;
2364 }
2365}
2366
2367
2368/**
2369 * Construct a display driver instance.
2370 *
2371 * @returns VBox status.
2372 * @param pDrvIns The driver instance data.
2373 * If the registration structure is needed, pDrvIns->pDrvReg points to it.
2374 * @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
2375 * of the driver instance. It's also found in pDrvIns->pCfgHandle, but like
2376 * iInstance it's expected to be used a bit in this function.
2377 */
2378DECLCALLBACK(int) Display::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle)
2379{
2380 PDRVMAINDISPLAY pData = PDMINS2DATA(pDrvIns, PDRVMAINDISPLAY);
2381 LogFlowFunc (("iInstance=%d\n", pDrvIns->iInstance));
2382
2383 /*
2384 * Validate configuration.
2385 */
2386 if (!CFGMR3AreValuesValid(pCfgHandle, "Object\0"))
2387 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
2388 PPDMIBASE pBaseIgnore;
2389 int rc = pDrvIns->pDrvHlp->pfnAttach(pDrvIns, &pBaseIgnore);
2390 if (rc != VERR_PDM_NO_ATTACHED_DRIVER)
2391 {
2392 AssertMsgFailed(("Configuration error: Not possible to attach anything to this driver!\n"));
2393 return VERR_PDM_DRVINS_NO_ATTACH;
2394 }
2395
2396 /*
2397 * Init Interfaces.
2398 */
2399 pDrvIns->IBase.pfnQueryInterface = Display::drvQueryInterface;
2400
2401 pData->Connector.pfnResize = Display::displayResizeCallback;
2402 pData->Connector.pfnUpdateRect = Display::displayUpdateCallback;
2403 pData->Connector.pfnRefresh = Display::displayRefreshCallback;
2404 pData->Connector.pfnReset = Display::displayResetCallback;
2405 pData->Connector.pfnLFBModeChange = Display::displayLFBModeChangeCallback;
2406 pData->Connector.pfnProcessAdapterData = Display::displayProcessAdapterDataCallback;
2407 pData->Connector.pfnProcessDisplayData = Display::displayProcessDisplayDataCallback;
2408
2409 /*
2410 * Get the IDisplayPort interface of the above driver/device.
2411 */
2412 pData->pUpPort = (PPDMIDISPLAYPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_DISPLAY_PORT);
2413 if (!pData->pUpPort)
2414 {
2415 AssertMsgFailed(("Configuration error: No display port interface above!\n"));
2416 return VERR_PDM_MISSING_INTERFACE_ABOVE;
2417 }
2418
2419 /*
2420 * Get the Display object pointer and update the mpDrv member.
2421 */
2422 void *pv;
2423 rc = CFGMR3QueryPtr(pCfgHandle, "Object", &pv);
2424 if (VBOX_FAILURE(rc))
2425 {
2426 AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Vrc\n", rc));
2427 return rc;
2428 }
2429 pData->pDisplay = (Display *)pv; /** @todo Check this cast! */
2430 pData->pDisplay->mpDrv = pData;
2431
2432 /*
2433 * Update our display information according to the framebuffer
2434 */
2435 pData->pDisplay->updateDisplayData();
2436
2437 /*
2438 * Start periodic screen refreshes
2439 */
2440 pData->pUpPort->pfnSetRefreshRate(pData->pUpPort, 20);
2441
2442 return VINF_SUCCESS;
2443}
2444
2445
2446/**
2447 * Display driver registration record.
2448 */
2449const PDMDRVREG Display::DrvReg =
2450{
2451 /* u32Version */
2452 PDM_DRVREG_VERSION,
2453 /* szDriverName */
2454 "MainDisplay",
2455 /* pszDescription */
2456 "Main display driver (Main as in the API).",
2457 /* fFlags */
2458 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
2459 /* fClass. */
2460 PDM_DRVREG_CLASS_DISPLAY,
2461 /* cMaxInstances */
2462 ~0,
2463 /* cbInstance */
2464 sizeof(DRVMAINDISPLAY),
2465 /* pfnConstruct */
2466 Display::drvConstruct,
2467 /* pfnDestruct */
2468 Display::drvDestruct,
2469 /* pfnIOCtl */
2470 NULL,
2471 /* pfnPowerOn */
2472 NULL,
2473 /* pfnReset */
2474 NULL,
2475 /* pfnSuspend */
2476 NULL,
2477 /* pfnResume */
2478 NULL,
2479 /* pfnDetach */
2480 NULL
2481};
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette