VirtualBox

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

Last change on this file since 3411 was 3398, checked in by vboxsync, 17 years ago

Call VRDP on EMT only when Framebuffer is not being resized.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use