VirtualBox

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

Last change on this file since 3392 was 3358, checked in by vboxsync, 17 years ago

Fixed a deadlock in Display.

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

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