VirtualBox

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

Last change on this file since 3317 was 3278, checked in by vboxsync, 17 years ago

Removed some obsolete VRDP code.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 73.1 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 if (mParent->getVMMDev())
1519 mParent->getVMMDev()->getVMMDevPort()->pfnRequestDisplayChange(mParent->getVMMDev()->getVMMDevPort(), aWidth, aHeight, aColorDepth, aDisplay);
1520 return S_OK;
1521}
1522
1523STDMETHODIMP Display::TakeScreenShot (BYTE *address, ULONG width, ULONG height)
1524{
1525 /// @todo (r=dmik) this function may take too long to complete if the VM
1526 // is doing something like saving state right now. Which, in case if it
1527 // is called on the GUI thread, will make it unresponsive. We should
1528 // check the machine state here (by enclosing the check and VMRequCall
1529 // within the Console lock to make it atomic).
1530
1531 LogFlowFuncEnter();
1532 LogFlowFunc (("address=%p, width=%d, height=%d\n",
1533 address, width, height));
1534
1535 if (!address)
1536 return E_POINTER;
1537 if (!width || !height)
1538 return E_INVALIDARG;
1539
1540 AutoLock lock(this);
1541 CHECK_READY();
1542
1543 CHECK_CONSOLE_DRV (mpDrv);
1544
1545 Console::SafeVMPtr pVM (mParent);
1546 CheckComRCReturnRC (pVM.rc());
1547
1548 HRESULT rc = S_OK;
1549
1550 LogFlowFunc (("Sending SCREENSHOT request\n"));
1551
1552 /*
1553 * First try use the graphics device features for making a snapshot.
1554 * This does not support streatching, is an optional feature (returns not supported).
1555 *
1556 * Note: It may cause a display resize. Watch out for deadlocks.
1557 */
1558 int rcVBox = VERR_NOT_SUPPORTED;
1559 if ( mpDrv->Connector.cx == width
1560 && mpDrv->Connector.cy == height)
1561 {
1562 PVMREQ pReq;
1563 size_t cbData = RT_ALIGN_Z(width, 4) * 4 * height;
1564 rcVBox = VMR3ReqCall(pVM, &pReq, RT_INDEFINITE_WAIT,
1565 (PFNRT)mpDrv->pUpPort->pfnSnapshot, 6, mpDrv->pUpPort,
1566 address, cbData, NULL, NULL, NULL);
1567 if (VBOX_SUCCESS(rcVBox))
1568 {
1569 rcVBox = pReq->iStatus;
1570 VMR3ReqFree(pReq);
1571 }
1572 }
1573
1574 /*
1575 * If the function returns not supported, or if streaching is requested,
1576 * we'll have to do all the work ourselves using the framebuffer data.
1577 */
1578 if (rcVBox == VERR_NOT_SUPPORTED || rcVBox == VERR_NOT_IMPLEMENTED)
1579 {
1580 /** @todo implement snapshot streching and generic snapshot fallback. */
1581 rc = setError (E_NOTIMPL, tr ("This feature is not implemented"));
1582 }
1583 else if (VBOX_FAILURE(rcVBox))
1584 rc = setError (E_FAIL,
1585 tr ("Could not take a screenshot (%Vrc)"), rcVBox);
1586
1587 LogFlowFunc (("rc=%08X\n", rc));
1588 LogFlowFuncLeave();
1589 return rc;
1590}
1591
1592STDMETHODIMP Display::DrawToScreen (BYTE *address, ULONG x, ULONG y,
1593 ULONG width, ULONG height)
1594{
1595 /// @todo (r=dmik) this function may take too long to complete if the VM
1596 // is doing something like saving state right now. Which, in case if it
1597 // is called on the GUI thread, will make it unresponsive. We should
1598 // check the machine state here (by enclosing the check and VMRequCall
1599 // within the Console lock to make it atomic).
1600
1601 LogFlowFuncEnter();
1602 LogFlowFunc (("address=%p, x=%d, y=%d, width=%d, height=%d\n",
1603 address, x, y, width, height));
1604
1605 if (!address)
1606 return E_POINTER;
1607 if (!width || !height)
1608 return E_INVALIDARG;
1609
1610 AutoLock lock(this);
1611 CHECK_READY();
1612
1613 CHECK_CONSOLE_DRV (mpDrv);
1614
1615 Console::SafeVMPtr pVM (mParent);
1616 CheckComRCReturnRC (pVM.rc());
1617
1618 /*
1619 * Again we're lazy and make the graphics device do all the
1620 * dirty convertion work.
1621 */
1622 PVMREQ pReq;
1623 int rcVBox = VMR3ReqCall(pVM, &pReq, RT_INDEFINITE_WAIT,
1624 (PFNRT)mpDrv->pUpPort->pfnDisplayBlt, 6, mpDrv->pUpPort,
1625 address, x, y, width, height);
1626 if (VBOX_SUCCESS(rcVBox))
1627 {
1628 rcVBox = pReq->iStatus;
1629 VMR3ReqFree(pReq);
1630 }
1631
1632 /*
1633 * If the function returns not supported, we'll have to do all the
1634 * work ourselves using the framebuffer.
1635 */
1636 HRESULT rc = S_OK;
1637 if (rcVBox == VERR_NOT_SUPPORTED || rcVBox == VERR_NOT_IMPLEMENTED)
1638 {
1639 /** @todo implement generic fallback for screen blitting. */
1640 rc = E_NOTIMPL;
1641 }
1642 else if (VBOX_FAILURE(rcVBox))
1643 rc = setError (E_FAIL,
1644 tr ("Could not draw to the screen (%Vrc)"), rcVBox);
1645//@todo
1646// else
1647// {
1648// /* All ok. Redraw the screen. */
1649// handleDisplayUpdate (x, y, width, height);
1650// }
1651
1652 LogFlowFunc (("rc=%08X\n", rc));
1653 LogFlowFuncLeave();
1654 return rc;
1655}
1656
1657/**
1658 * Does a full invalidation of the VM display and instructs the VM
1659 * to update it immediately.
1660 *
1661 * @returns COM status code
1662 */
1663STDMETHODIMP Display::InvalidateAndUpdate()
1664{
1665 LogFlowFuncEnter();
1666
1667 AutoLock lock(this);
1668 CHECK_READY();
1669
1670 CHECK_CONSOLE_DRV (mpDrv);
1671
1672 Console::SafeVMPtr pVM (mParent);
1673 CheckComRCReturnRC (pVM.rc());
1674
1675 HRESULT rc = S_OK;
1676
1677 LogFlowFunc (("Sending DPYUPDATE request\n"));
1678
1679 /* pdm.h says that this has to be called from the EMT thread */
1680 PVMREQ pReq;
1681 int rcVBox = VMR3ReqCallVoid(pVM, &pReq, RT_INDEFINITE_WAIT,
1682 (PFNRT)mpDrv->pUpPort->pfnUpdateDisplayAll, 1, mpDrv->pUpPort);
1683 if (VBOX_SUCCESS(rcVBox))
1684 VMR3ReqFree(pReq);
1685
1686 if (VBOX_FAILURE(rcVBox))
1687 rc = setError (E_FAIL,
1688 tr ("Could not invalidate and update the screen (%Vrc)"), rcVBox);
1689
1690 LogFlowFunc (("rc=%08X\n", rc));
1691 LogFlowFuncLeave();
1692 return rc;
1693}
1694
1695/**
1696 * Notification that the framebuffer has completed the
1697 * asynchronous resize processing
1698 *
1699 * @returns COM status code
1700 */
1701STDMETHODIMP Display::ResizeCompleted(ULONG aScreenId)
1702{
1703 LogFlowFunc (("\n"));
1704
1705 /// @todo (dmik) can we AutoLock alock (this); here?
1706 // do it when we switch this class to VirtualBoxBase_NEXT.
1707 // This will require general code review and may add some details.
1708 // In particular, we may want to check whether EMT is really waiting for
1709 // this notification, etc. It might be also good to obey the caller to make
1710 // sure this method is not called from more than one thread at a time
1711 // (and therefore don't use Display lock at all here to save some
1712 // milliseconds).
1713 CHECK_READY();
1714
1715 /* this is only valid for external framebuffers */
1716 if (mInternalFramebuffer)
1717 return setError (E_FAIL,
1718 tr ("Resize completed notification is valid only "
1719 "for external framebuffers"));
1720
1721 /* Set the flag indicating that the resize has completed and display data need to be updated. */
1722 bool f = ASMAtomicCmpXchgU32 (&maFramebuffers[aScreenId].u32ResizeStatus, ResizeStatus_UpdateDisplayData, ResizeStatus_InProgress);
1723 AssertRelease(f);NOREF(f);
1724
1725 return S_OK;
1726}
1727
1728/**
1729 * Notification that the framebuffer has completed the
1730 * asynchronous update processing
1731 *
1732 * @returns COM status code
1733 */
1734STDMETHODIMP Display::UpdateCompleted()
1735{
1736 LogFlowFunc (("\n"));
1737
1738 /// @todo (dmik) can we AutoLock alock (this); here?
1739 // do it when we switch this class to VirtualBoxBase_NEXT.
1740 // Tthis will require general code review and may add some details.
1741 // In particular, we may want to check whether EMT is really waiting for
1742 // this notification, etc. It might be also good to obey the caller to make
1743 // sure this method is not called from more than one thread at a time
1744 // (and therefore don't use Display lock at all here to save some
1745 // milliseconds).
1746 CHECK_READY();
1747
1748 /* this is only valid for external framebuffers */
1749 if (mInternalFramebuffer)
1750 return setError (E_FAIL,
1751 tr ("Resize completed notification is valid only "
1752 "for external framebuffers"));
1753
1754 maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN].pFramebuffer->Lock();
1755 /* signal our semaphore */
1756 RTSemEventMultiSignal(mUpdateSem);
1757 maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN].pFramebuffer->Unlock();
1758
1759 return S_OK;
1760}
1761
1762// private methods
1763/////////////////////////////////////////////////////////////////////////////
1764
1765/**
1766 * Helper to update the display information from the framebuffer.
1767 *
1768 * @param aCheckParams true to compare the parameters of the current framebuffer
1769 * and the new one and issue handleDisplayResize()
1770 * if they differ.
1771 * @thread EMT
1772 */
1773void Display::updateDisplayData (bool aCheckParams /* = false */)
1774{
1775 /* the driver might not have been constructed yet */
1776 if (!mpDrv)
1777 return;
1778
1779#if DEBUG
1780 /*
1781 * Sanity check. Note that this method may be called on EMT after Console
1782 * has started the power down procedure (but before our #drvDestruct() is
1783 * called, in which case pVM will aleady be NULL but mpDrv will not). Since
1784 * we don't really need pVM to proceed, we avoid this check in the release
1785 * build to save some ms (necessary to construct SafeVMPtrQuiet) in this
1786 * time-critical method.
1787 */
1788 Console::SafeVMPtrQuiet pVM (mParent);
1789 if (pVM.isOk())
1790 VM_ASSERT_EMT (pVM.raw());
1791#endif
1792
1793 /* The method is only relevant to the primary framebuffer. */
1794 IFramebuffer *pFramebuffer = maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN].pFramebuffer;
1795
1796 if (pFramebuffer)
1797 {
1798 HRESULT rc;
1799 BYTE *address = 0;
1800 rc = pFramebuffer->COMGETTER(Address) (&address);
1801 AssertComRC (rc);
1802 ULONG lineSize = 0;
1803 rc = pFramebuffer->COMGETTER(LineSize) (&lineSize);
1804 AssertComRC (rc);
1805 ULONG colorDepth = 0;
1806 rc = pFramebuffer->COMGETTER(ColorDepth) (&colorDepth);
1807 AssertComRC (rc);
1808 ULONG width = 0;
1809 rc = pFramebuffer->COMGETTER(Width) (&width);
1810 AssertComRC (rc);
1811 ULONG height = 0;
1812 rc = pFramebuffer->COMGETTER(Height) (&height);
1813 AssertComRC (rc);
1814
1815 /*
1816 * Check current parameters with new ones and issue handleDisplayResize()
1817 * to let the new frame buffer adjust itself properly. Note that it will
1818 * result into a recursive updateDisplayData() call but with
1819 * aCheckOld = false.
1820 */
1821 if (aCheckParams &&
1822 (mLastAddress != address ||
1823 mLastLineSize != lineSize ||
1824 mLastColorDepth != colorDepth ||
1825 mLastWidth != (int) width ||
1826 mLastHeight != (int) height))
1827 {
1828 handleDisplayResize (VBOX_VIDEO_PRIMARY_SCREEN, mLastColorDepth,
1829 mLastAddress,
1830 mLastLineSize,
1831 mLastWidth,
1832 mLastHeight);
1833 return;
1834 }
1835
1836 mpDrv->Connector.pu8Data = (uint8_t *) address;
1837 mpDrv->Connector.cbScanline = lineSize;
1838 mpDrv->Connector.cBits = colorDepth;
1839 mpDrv->Connector.cx = width;
1840 mpDrv->Connector.cy = height;
1841 }
1842 else
1843 {
1844 /* black hole */
1845 mpDrv->Connector.pu8Data = NULL;
1846 mpDrv->Connector.cbScanline = 0;
1847 mpDrv->Connector.cBits = 0;
1848 mpDrv->Connector.cx = 0;
1849 mpDrv->Connector.cy = 0;
1850 }
1851}
1852
1853/**
1854 * Changes the current frame buffer. Called on EMT to avoid both
1855 * race conditions and excessive locking.
1856 *
1857 * @note locks this object for writing
1858 * @thread EMT
1859 */
1860/* static */
1861DECLCALLBACK(int) Display::changeFramebuffer (Display *that, IFramebuffer *aFB,
1862 bool aInternal, unsigned uScreenId)
1863{
1864 LogFlowFunc (("uScreenId = %d\n", uScreenId));
1865
1866 AssertReturn (that, VERR_INVALID_PARAMETER);
1867 AssertReturn (aFB || aInternal, VERR_INVALID_PARAMETER);
1868 AssertReturn (uScreenId >= 0 && uScreenId < that->mcMonitors, VERR_INVALID_PARAMETER);
1869
1870 /// @todo (r=dmik) AutoCaller
1871
1872 AutoLock alock (that);
1873
1874 DISPLAYFBINFO *pDisplayFBInfo = &that->maFramebuffers[uScreenId];
1875 pDisplayFBInfo->pFramebuffer = aFB;
1876
1877 that->mInternalFramebuffer = aInternal;
1878 that->mSupportedAccelOps = 0;
1879
1880 /* determine which acceleration functions are supported by this framebuffer */
1881 if (aFB && !aInternal)
1882 {
1883 HRESULT rc;
1884 BOOL accelSupported = FALSE;
1885 rc = aFB->OperationSupported (
1886 FramebufferAccelerationOperation_SolidFillAcceleration, &accelSupported);
1887 AssertComRC (rc);
1888 if (accelSupported)
1889 that->mSupportedAccelOps |=
1890 FramebufferAccelerationOperation_SolidFillAcceleration;
1891 accelSupported = FALSE;
1892 rc = aFB->OperationSupported (
1893 FramebufferAccelerationOperation_ScreenCopyAcceleration, &accelSupported);
1894 AssertComRC (rc);
1895 if (accelSupported)
1896 that->mSupportedAccelOps |=
1897 FramebufferAccelerationOperation_ScreenCopyAcceleration;
1898 }
1899
1900 that->mParent->consoleVRDPServer()->SendResize ();
1901
1902 that->updateDisplayData (true /* aCheckParams */);
1903
1904 return VINF_SUCCESS;
1905}
1906
1907/**
1908 * Handle display resize event issued by the VGA device for the primary screen.
1909 *
1910 * @see PDMIDISPLAYCONNECTOR::pfnResize
1911 */
1912DECLCALLBACK(int) Display::displayResizeCallback(PPDMIDISPLAYCONNECTOR pInterface,
1913 uint32_t bpp, void *pvVRAM, uint32_t cbLine, uint32_t cx, uint32_t cy)
1914{
1915 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
1916
1917 LogFlowFunc (("bpp %d, pvVRAM %p, cbLine %d, cx %d, cy %d\n",
1918 bpp, pvVRAM, cbLine, cx, cy));
1919
1920 return pDrv->pDisplay->handleDisplayResize(VBOX_VIDEO_PRIMARY_SCREEN, bpp, pvVRAM, cbLine, cx, cy);
1921}
1922
1923/**
1924 * Handle display update.
1925 *
1926 * @see PDMIDISPLAYCONNECTOR::pfnUpdateRect
1927 */
1928DECLCALLBACK(void) Display::displayUpdateCallback(PPDMIDISPLAYCONNECTOR pInterface,
1929 uint32_t x, uint32_t y, uint32_t cx, uint32_t cy)
1930{
1931 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
1932
1933#ifdef DEBUG_sunlover
1934 LogFlowFunc (("pDrv->pDisplay->mfVideoAccelEnabled = %d, %d,%d %dx%d\n",
1935 pDrv->pDisplay->mfVideoAccelEnabled, x, y, cx, cy));
1936#endif /* DEBUG_sunlover */
1937
1938 /* This call does update regardless of VBVA status.
1939 * But in VBVA mode this is called only as result of
1940 * pfnUpdateDisplayAll in the VGA device.
1941 */
1942
1943 pDrv->pDisplay->handleDisplayUpdate(x, y, cx, cy);
1944}
1945
1946/**
1947 * Periodic display refresh callback.
1948 *
1949 * @see PDMIDISPLAYCONNECTOR::pfnRefresh
1950 */
1951DECLCALLBACK(void) Display::displayRefreshCallback(PPDMIDISPLAYCONNECTOR pInterface)
1952{
1953 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
1954
1955#ifdef DEBUG_sunlover
1956 STAM_PROFILE_START(&StatDisplayRefresh, a);
1957#endif /* DEBUG_sunlover */
1958
1959#ifdef DEBUG_sunlover
1960 LogFlowFunc (("pDrv->pDisplay->mfVideoAccelEnabled = %d\n",
1961 pDrv->pDisplay->mfVideoAccelEnabled));
1962#endif /* DEBUG_sunlover */
1963
1964 Display *pDisplay = pDrv->pDisplay;
1965
1966 unsigned uScreenId;
1967 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
1968 {
1969 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
1970
1971 /* Check the resize status. The status can be checked normally because
1972 * the status affects only the EMT.
1973 */
1974 uint32_t u32ResizeStatus = pFBInfo->u32ResizeStatus;
1975
1976 if (u32ResizeStatus == ResizeStatus_UpdateDisplayData)
1977 {
1978 LogFlowFunc (("ResizeStatus_UpdateDisplayData %d\n", uScreenId));
1979 /* The framebuffer was resized and display data need to be updated. */
1980 pDisplay->handleResizeCompletedEMT ();
1981 /* Continue with normal processing because the status here is ResizeStatus_Void. */
1982 Assert (pFBInfo->u32ResizeStatus == ResizeStatus_Void);
1983 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
1984 {
1985 /* Repaint the display because VM continued to run during the framebuffer resize. */
1986 if (!pFBInfo->pFramebuffer.isNull())
1987 pDrv->pUpPort->pfnUpdateDisplayAll(pDrv->pUpPort);
1988 }
1989 /* Ignore the refresh for the screen to replay the logic. */
1990 continue;
1991 }
1992 else if (u32ResizeStatus == ResizeStatus_InProgress)
1993 {
1994 /* The framebuffer is being resized. Do not call the VGA device back. Immediately return. */
1995 LogFlowFunc (("ResizeStatus_InProcess\n"));
1996 continue;
1997 }
1998
1999 if (pFBInfo->pFramebuffer.isNull())
2000 {
2001 /*
2002 * Do nothing in the "black hole" mode to avoid copying guest
2003 * video memory to the frame buffer
2004 */
2005 }
2006 else
2007 {
2008 if (pDisplay->mfPendingVideoAccelEnable)
2009 {
2010 /* Acceleration was enabled while machine was not yet running
2011 * due to restoring from saved state. Update entire display and
2012 * actually enable acceleration.
2013 */
2014 Assert(pDisplay->mpPendingVbvaMemory);
2015
2016 /* Acceleration can not be yet enabled.*/
2017 Assert(pDisplay->mpVbvaMemory == NULL);
2018 Assert(!pDisplay->mfVideoAccelEnabled);
2019
2020 if (pDisplay->mfMachineRunning)
2021 {
2022 pDisplay->VideoAccelEnable (pDisplay->mfPendingVideoAccelEnable,
2023 pDisplay->mpPendingVbvaMemory);
2024
2025 /* Reset the pending state. */
2026 pDisplay->mfPendingVideoAccelEnable = false;
2027 pDisplay->mpPendingVbvaMemory = NULL;
2028 }
2029 }
2030 else
2031 {
2032 Assert(pDisplay->mpPendingVbvaMemory == NULL);
2033
2034 if (pDisplay->mfVideoAccelEnabled)
2035 {
2036 Assert(pDisplay->mpVbvaMemory);
2037 pDisplay->VideoAccelFlush ();
2038 }
2039 else
2040 {
2041 Assert(pDrv->Connector.pu8Data);
2042 pDrv->pUpPort->pfnUpdateDisplay(pDrv->pUpPort);
2043 }
2044 }
2045 /* Inform the VRDP server that the current display update sequence is
2046 * completed. At this moment the framebuffer memory contains a definite
2047 * image, that is synchronized with the orders already sent to VRDP client.
2048 * The server can now process redraw requests from clients or initial
2049 * fullscreen updates for new clients.
2050 */
2051 Assert (pDisplay->mParent && pDisplay->mParent->consoleVRDPServer());
2052 pDisplay->mParent->consoleVRDPServer()->SendUpdate (uScreenId, NULL, 0);
2053 }
2054 }
2055
2056#ifdef DEBUG_sunlover
2057 STAM_PROFILE_STOP(&StatDisplayRefresh, a);
2058 LogFlowFunc (("leave\n"));
2059#endif /* DEBUG_sunlover */
2060}
2061
2062/**
2063 * Reset notification
2064 *
2065 * @see PDMIDISPLAYCONNECTOR::pfnReset
2066 */
2067DECLCALLBACK(void) Display::displayResetCallback(PPDMIDISPLAYCONNECTOR pInterface)
2068{
2069 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2070
2071 LogFlowFunc (("\n"));
2072
2073 /* Disable VBVA mode. */
2074 pDrv->pDisplay->VideoAccelEnable (false, NULL);
2075}
2076
2077/**
2078 * LFBModeChange notification
2079 *
2080 * @see PDMIDISPLAYCONNECTOR::pfnLFBModeChange
2081 */
2082DECLCALLBACK(void) Display::displayLFBModeChangeCallback(PPDMIDISPLAYCONNECTOR pInterface, bool fEnabled)
2083{
2084 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2085
2086 LogFlowFunc (("fEnabled=%d\n", fEnabled));
2087
2088 NOREF(fEnabled);
2089
2090 /* Disable VBVA mode in any case. The guest driver reenables VBVA mode if necessary. */
2091 pDrv->pDisplay->VideoAccelEnable (false, NULL);
2092}
2093
2094/**
2095 * Adapter information change notification.
2096 *
2097 * @see PDMIDISPLAYCONNECTOR::pfnProcessAdapterData
2098 */
2099DECLCALLBACK(void) Display::displayProcessAdapterDataCallback(PPDMIDISPLAYCONNECTOR pInterface, void *pvVRAM, uint32_t u32VRAMSize)
2100{
2101 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2102
2103 if (pvVRAM == NULL)
2104 {
2105 unsigned i;
2106 for (i = 0; i < pDrv->pDisplay->mcMonitors; i++)
2107 {
2108 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[i];
2109
2110 pFBInfo->u32Offset = 0;
2111 pFBInfo->u32MaxFramebufferSize = 0;
2112 pFBInfo->u32InformationSize = 0;
2113 }
2114 }
2115 else
2116 {
2117 uint8_t *pu8 = (uint8_t *)pvVRAM;
2118 pu8 += u32VRAMSize - VBOX_VIDEO_ADAPTER_INFORMATION_SIZE;
2119
2120 // @todo
2121 uint8_t *pu8End = pu8 + VBOX_VIDEO_ADAPTER_INFORMATION_SIZE;
2122
2123 VBOXVIDEOINFOHDR *pHdr;
2124
2125 for (;;)
2126 {
2127 pHdr = (VBOXVIDEOINFOHDR *)pu8;
2128 pu8 += sizeof (VBOXVIDEOINFOHDR);
2129
2130 if (pu8 >= pu8End)
2131 {
2132 LogRel(("VBoxVideo: Guest adapter information overflow!!!\n"));
2133 break;
2134 }
2135
2136 if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_DISPLAY)
2137 {
2138 if (pHdr->u16Length != sizeof (VBOXVIDEOINFODISPLAY))
2139 {
2140 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "DISPLAY", pHdr->u16Length));
2141 break;
2142 }
2143
2144 VBOXVIDEOINFODISPLAY *pDisplay = (VBOXVIDEOINFODISPLAY *)pu8;
2145
2146 if (pDisplay->u32Index >= pDrv->pDisplay->mcMonitors)
2147 {
2148 LogRel(("VBoxVideo: Guest adapter information invalid display index %d!!!\n", pDisplay->u32Index));
2149 break;
2150 }
2151
2152 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[pDisplay->u32Index];
2153
2154 pFBInfo->u32Offset = pDisplay->u32Offset;
2155 pFBInfo->u32MaxFramebufferSize = pDisplay->u32FramebufferSize;
2156 pFBInfo->u32InformationSize = pDisplay->u32InformationSize;
2157
2158 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));
2159 }
2160 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_END)
2161 {
2162 if (pHdr->u16Length != 0)
2163 {
2164 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "END", pHdr->u16Length));
2165 break;
2166 }
2167
2168 break;
2169 }
2170 else
2171 {
2172 LogRel(("Guest adapter information contains unsupported type %d\n", pHdr->u8Type));
2173 }
2174
2175 pu8 += pHdr->u16Length;
2176 }
2177 }
2178}
2179
2180/**
2181 * Display information change notification.
2182 *
2183 * @see PDMIDISPLAYCONNECTOR::pfnProcessDisplayData
2184 */
2185DECLCALLBACK(void) Display::displayProcessDisplayDataCallback(PPDMIDISPLAYCONNECTOR pInterface, void *pvVRAM, unsigned uScreenId)
2186{
2187 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2188
2189 if (uScreenId >= pDrv->pDisplay->mcMonitors)
2190 {
2191 LogRel(("VBoxVideo: Guest display information invalid display index %d!!!\n", uScreenId));
2192 return;
2193 }
2194
2195 /* Get the display information strcuture. */
2196 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[uScreenId];
2197
2198 uint8_t *pu8 = (uint8_t *)pvVRAM;
2199 pu8 += pFBInfo->u32Offset + pFBInfo->u32MaxFramebufferSize;
2200
2201 // @todo
2202 uint8_t *pu8End = pu8 + pFBInfo->u32InformationSize;
2203
2204 VBOXVIDEOINFOHDR *pHdr;
2205
2206 for (;;)
2207 {
2208 pHdr = (VBOXVIDEOINFOHDR *)pu8;
2209 pu8 += sizeof (VBOXVIDEOINFOHDR);
2210
2211 if (pu8 >= pu8End)
2212 {
2213 LogRel(("VBoxVideo: Guest display information overflow!!!\n"));
2214 break;
2215 }
2216
2217 if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_SCREEN)
2218 {
2219 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOSCREEN))
2220 {
2221 LogRel(("VBoxVideo: Guest display information %s invalid length %d!!!\n", "SCREEN", pHdr->u16Length));
2222 break;
2223 }
2224
2225 VBOXVIDEOINFOSCREEN *pScreen = (VBOXVIDEOINFOSCREEN *)pu8;
2226
2227 pFBInfo->xOrigin = pScreen->xOrigin;
2228 pFBInfo->yOrigin = pScreen->yOrigin;
2229
2230 pFBInfo->w = pScreen->u16Width;
2231 pFBInfo->h = pScreen->u16Height;
2232
2233 LogFlow(("VBOX_VIDEO_INFO_TYPE_SCREEN: (%p) %d: at %d,%d, linesize 0x%X, size %dx%d, bpp %d, flags 0x%02X\n",
2234 pHdr, uScreenId, pScreen->xOrigin, pScreen->yOrigin, pScreen->u32LineSize, pScreen->u16Width, pScreen->u16Height, pScreen->bitsPerPixel, pScreen->u8Flags));
2235
2236 if (uScreenId != VBOX_VIDEO_PRIMARY_SCREEN)
2237 {
2238 /* Primary screen resize is initiated by the VGA device. */
2239 pDrv->pDisplay->handleDisplayResize(uScreenId, pScreen->bitsPerPixel, (uint8_t *)pvVRAM + pFBInfo->u32Offset, pScreen->u32LineSize, pScreen->u16Width, pScreen->u16Height);
2240 }
2241 }
2242 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_END)
2243 {
2244 if (pHdr->u16Length != 0)
2245 {
2246 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "END", pHdr->u16Length));
2247 break;
2248 }
2249
2250 break;
2251 }
2252 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_HOST_EVENTS)
2253 {
2254 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOHOSTEVENTS))
2255 {
2256 LogRel(("VBoxVideo: Guest display information %s invalid length %d!!!\n", "HOST_EVENTS", pHdr->u16Length));
2257 break;
2258 }
2259
2260 VBOXVIDEOINFOHOSTEVENTS *pHostEvents = (VBOXVIDEOINFOHOSTEVENTS *)pu8;
2261
2262 pFBInfo->pHostEvents = pHostEvents;
2263
2264 LogFlow(("VBOX_VIDEO_INFO_TYPE_HOSTEVENTS: (%p)\n",
2265 pHostEvents));
2266 }
2267 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_LINK)
2268 {
2269 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOLINK))
2270 {
2271 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "LINK", pHdr->u16Length));
2272 break;
2273 }
2274
2275 VBOXVIDEOINFOLINK *pLink = (VBOXVIDEOINFOLINK *)pu8;
2276 pu8 += pLink->i32Offset;
2277 }
2278 else
2279 {
2280 LogRel(("Guest display information contains unsupported type %d\n", pHdr->u8Type));
2281 }
2282
2283 pu8 += pHdr->u16Length;
2284 }
2285}
2286
2287/**
2288 * Queries an interface to the driver.
2289 *
2290 * @returns Pointer to interface.
2291 * @returns NULL if the interface was not supported by the driver.
2292 * @param pInterface Pointer to this interface structure.
2293 * @param enmInterface The requested interface identification.
2294 */
2295DECLCALLBACK(void *) Display::drvQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
2296{
2297 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
2298 PDRVMAINDISPLAY pDrv = PDMINS2DATA(pDrvIns, PDRVMAINDISPLAY);
2299 switch (enmInterface)
2300 {
2301 case PDMINTERFACE_BASE:
2302 return &pDrvIns->IBase;
2303 case PDMINTERFACE_DISPLAY_CONNECTOR:
2304 return &pDrv->Connector;
2305 default:
2306 return NULL;
2307 }
2308}
2309
2310
2311/**
2312 * Destruct a display driver instance.
2313 *
2314 * @returns VBox status.
2315 * @param pDrvIns The driver instance data.
2316 */
2317DECLCALLBACK(void) Display::drvDestruct(PPDMDRVINS pDrvIns)
2318{
2319 PDRVMAINDISPLAY pData = PDMINS2DATA(pDrvIns, PDRVMAINDISPLAY);
2320 LogFlowFunc (("iInstance=%d\n", pDrvIns->iInstance));
2321 if (pData->pDisplay)
2322 {
2323 AutoLock displayLock (pData->pDisplay);
2324 pData->pDisplay->mpDrv = NULL;
2325 pData->pDisplay->mpVMMDev = NULL;
2326 pData->pDisplay->mLastAddress = NULL;
2327 pData->pDisplay->mLastLineSize = 0;
2328 pData->pDisplay->mLastColorDepth = 0,
2329 pData->pDisplay->mLastWidth = 0;
2330 pData->pDisplay->mLastHeight = 0;
2331 }
2332}
2333
2334
2335/**
2336 * Construct a display driver instance.
2337 *
2338 * @returns VBox status.
2339 * @param pDrvIns The driver instance data.
2340 * If the registration structure is needed, pDrvIns->pDrvReg points to it.
2341 * @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
2342 * of the driver instance. It's also found in pDrvIns->pCfgHandle, but like
2343 * iInstance it's expected to be used a bit in this function.
2344 */
2345DECLCALLBACK(int) Display::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle)
2346{
2347 PDRVMAINDISPLAY pData = PDMINS2DATA(pDrvIns, PDRVMAINDISPLAY);
2348 LogFlowFunc (("iInstance=%d\n", pDrvIns->iInstance));
2349
2350 /*
2351 * Validate configuration.
2352 */
2353 if (!CFGMR3AreValuesValid(pCfgHandle, "Object\0"))
2354 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
2355 PPDMIBASE pBaseIgnore;
2356 int rc = pDrvIns->pDrvHlp->pfnAttach(pDrvIns, &pBaseIgnore);
2357 if (rc != VERR_PDM_NO_ATTACHED_DRIVER)
2358 {
2359 AssertMsgFailed(("Configuration error: Not possible to attach anything to this driver!\n"));
2360 return VERR_PDM_DRVINS_NO_ATTACH;
2361 }
2362
2363 /*
2364 * Init Interfaces.
2365 */
2366 pDrvIns->IBase.pfnQueryInterface = Display::drvQueryInterface;
2367
2368 pData->Connector.pfnResize = Display::displayResizeCallback;
2369 pData->Connector.pfnUpdateRect = Display::displayUpdateCallback;
2370 pData->Connector.pfnRefresh = Display::displayRefreshCallback;
2371 pData->Connector.pfnReset = Display::displayResetCallback;
2372 pData->Connector.pfnLFBModeChange = Display::displayLFBModeChangeCallback;
2373 pData->Connector.pfnProcessAdapterData = Display::displayProcessAdapterDataCallback;
2374 pData->Connector.pfnProcessDisplayData = Display::displayProcessDisplayDataCallback;
2375
2376 /*
2377 * Get the IDisplayPort interface of the above driver/device.
2378 */
2379 pData->pUpPort = (PPDMIDISPLAYPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_DISPLAY_PORT);
2380 if (!pData->pUpPort)
2381 {
2382 AssertMsgFailed(("Configuration error: No display port interface above!\n"));
2383 return VERR_PDM_MISSING_INTERFACE_ABOVE;
2384 }
2385
2386 /*
2387 * Get the Display object pointer and update the mpDrv member.
2388 */
2389 void *pv;
2390 rc = CFGMR3QueryPtr(pCfgHandle, "Object", &pv);
2391 if (VBOX_FAILURE(rc))
2392 {
2393 AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Vrc\n", rc));
2394 return rc;
2395 }
2396 pData->pDisplay = (Display *)pv; /** @todo Check this cast! */
2397 pData->pDisplay->mpDrv = pData;
2398
2399 /*
2400 * Update our display information according to the framebuffer
2401 */
2402 pData->pDisplay->updateDisplayData();
2403
2404 /*
2405 * Start periodic screen refreshes
2406 */
2407 pData->pUpPort->pfnSetRefreshRate(pData->pUpPort, 20);
2408
2409 return VINF_SUCCESS;
2410}
2411
2412
2413/**
2414 * Display driver registration record.
2415 */
2416const PDMDRVREG Display::DrvReg =
2417{
2418 /* u32Version */
2419 PDM_DRVREG_VERSION,
2420 /* szDriverName */
2421 "MainDisplay",
2422 /* pszDescription */
2423 "Main display driver (Main as in the API).",
2424 /* fFlags */
2425 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
2426 /* fClass. */
2427 PDM_DRVREG_CLASS_DISPLAY,
2428 /* cMaxInstances */
2429 ~0,
2430 /* cbInstance */
2431 sizeof(DRVMAINDISPLAY),
2432 /* pfnConstruct */
2433 Display::drvConstruct,
2434 /* pfnDestruct */
2435 Display::drvDestruct,
2436 /* pfnIOCtl */
2437 NULL,
2438 /* pfnPowerOn */
2439 NULL,
2440 /* pfnReset */
2441 NULL,
2442 /* pfnSuspend */
2443 NULL,
2444 /* pfnResume */
2445 NULL,
2446 /* pfnDetach */
2447 NULL
2448};
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