VirtualBox

source: vbox/trunk/src/VBox/Additions/x11/VBoxClient/display-svga.cpp@ 82668

Last change on this file since 82668 was 82668, checked in by vboxsync, 5 years ago

VMSVGA/X11: bugref:9637. Caching monitor (display) information so that we can send the whole data (not just delta) to drm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.3 KB
Line 
1/* $Id: display-svga.cpp 82668 2020-01-08 10:52:32Z vboxsync $ */
2/** @file
3 * X11 guest client - VMSVGA emulation resize event pass-through to drm guest
4 * driver.
5 */
6
7/*
8 * Copyright (C) 2016-2019 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19/*
20 * Known things to test when changing this code. All assume a guest with VMSVGA
21 * active and controlled by X11 or Wayland, and Guest Additions installed and
22 * running, unless otherwise stated.
23 * - On Linux 4.6 and later guests, VBoxClient --vmsvga should be running as
24 * root and not as the logged-in user. Dynamic resizing should work for all
25 * screens in any environment which handles kernel resize notifications,
26 * including at log-in screens. Test GNOME Shell Wayland and GNOME Shell
27 * under X.Org or Unity or KDE at the log-in screen and after log-in.
28 * - Linux 4.10 changed the user-kernel-ABI introduced in 4.6: test both.
29 * - On other guests (than Linux 4.6 or later) running X.Org Server 1.3 or
30 * later, VBoxClient --vmsvga should never be running as root, and should run
31 * (and dynamic resizing and screen enable/disable should work for all
32 * screens) whenever a user is logged in to a supported desktop environment.
33 * - On guests running X.Org Server 1.2 or older, VBoxClient --vmsvga should
34 * never run as root and should run whenever a user is logged in to a
35 * supported desktop environment. Dynamic resizing should work for the first
36 * screen, and enabling others should not be possible.
37 * - When VMSVGA is not enabled, VBoxClient --vmsvga should never stay running.
38 */
39
40#include "VBoxClient.h"
41
42#include <VBox/VBoxGuestLib.h>
43
44#include <iprt/assert.h>
45#include <iprt/file.h>
46#include <iprt/err.h>
47#include <iprt/string.h>
48
49#include <stdio.h>
50
51/** Maximum number of supported screens. DRM and X11 both limit this to 32. */
52/** @todo if this ever changes, dynamically allocate resizeable arrays in the
53 * context structure. */
54#define VMW_MAX_HEADS 32
55
56/* VMWare kernel driver control parts definitions. */
57
58#ifdef RT_OS_LINUX
59# include <sys/ioctl.h>
60#else /* Solaris and BSDs, in case they ever adopt the DRM driver. */
61# include <sys/ioccom.h>
62#endif
63
64#define DRM_DRIVER_NAME "vmwgfx"
65
66/** DRM version structure. */
67struct DRMVERSION
68{
69 int cMajor;
70 int cMinor;
71 int cPatchLevel;
72 size_t cbName;
73 char *pszName;
74 size_t cbDate;
75 char *pszDate;
76 size_t cbDescription;
77 char *pszDescription;
78};
79AssertCompileSize(struct DRMVERSION, 8 + 7 * sizeof(void *));
80
81/** Rectangle structure for geometry of a single screen. */
82struct DRMVMWRECT
83{
84 int32_t x;
85 int32_t y;
86 uint32_t w;
87 uint32_t h;
88};
89AssertCompileSize(struct DRMVMWRECT, 16);
90
91#define DRM_IOCTL_VERSION _IOWR('d', 0x00, struct DRMVERSION)
92
93struct DRMCONTEXT
94{
95 RTFILE hDevice;
96};
97
98struct DRMVMWRECT aRects[VMW_MAX_HEADS];
99
100static void drmConnect(struct DRMCONTEXT *pContext)
101{
102 unsigned i;
103 RTFILE hDevice;
104
105 if (pContext->hDevice != NIL_RTFILE)
106 VBClLogFatalError("%s called with bad argument\n", __func__);
107 /* Try to open the SVGA DRM device. */
108 for (i = 0; i < 128; ++i)
109 {
110 char szPath[64];
111 struct DRMVERSION version;
112 char szName[sizeof(DRM_DRIVER_NAME)];
113 int rc;
114
115 /* Control devices for drm graphics driver control devices go from
116 * controlD64 to controlD127. Render node devices go from renderD128
117 * to renderD192. The driver takes resize hints via the control device
118 * on pre-4.10 kernels and on the render device on newer ones. Try
119 * both types. */
120 if (i % 2 == 0)
121 rc = RTStrPrintf(szPath, sizeof(szPath), "/dev/dri/renderD%u", i / 2 + 128);
122 else
123 rc = RTStrPrintf(szPath, sizeof(szPath), "/dev/dri/controlD%u", i / 2 + 64);
124 if (RT_FAILURE(rc))
125 VBClLogFatalError("RTStrPrintf of device path failed, rc=%Rrc\n", rc);
126 rc = RTFileOpen(&hDevice, szPath, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
127 if (RT_FAILURE(rc))
128 continue;
129 RT_ZERO(version);
130 version.cbName = sizeof(szName);
131 version.pszName = szName;
132 rc = RTFileIoCtl(hDevice, DRM_IOCTL_VERSION, &version, sizeof(version), NULL);
133 if ( RT_SUCCESS(rc)
134 && !strncmp(szName, DRM_DRIVER_NAME, sizeof(DRM_DRIVER_NAME) - 1)
135 && ( version.cMajor > 2
136 || (version.cMajor == 2 && version.cMinor > 9)))
137 break;
138 hDevice = NIL_RTFILE;
139 }
140 pContext->hDevice = hDevice;
141}
142
143/** Preferred screen layout information for DRM_VMW_UPDATE_LAYOUT IoCtl. The
144 * rects argument is a cast pointer to an array of drm_vmw_rect. */
145struct DRMVMWUPDATELAYOUT {
146 uint32_t cOutputs;
147 uint32_t u32Pad;
148 uint64_t ptrRects;
149};
150AssertCompileSize(struct DRMVMWUPDATELAYOUT, 16);
151
152#define DRM_IOCTL_VMW_UPDATE_LAYOUT \
153 _IOW('d', 0x40 + 20, struct DRMVMWUPDATELAYOUT)
154
155static void drmSendHints(struct DRMCONTEXT *pContext, struct DRMVMWRECT *paRects,
156 unsigned cHeads)
157{
158 int rc;
159 struct DRMVMWUPDATELAYOUT ioctlLayout;
160
161 if (pContext->hDevice == NIL_RTFILE)
162 VBClLogFatalError("%s bad device argument\n", __func__);
163 ioctlLayout.cOutputs = cHeads;
164 ioctlLayout.ptrRects = (uint64_t)paRects;
165 rc = RTFileIoCtl(pContext->hDevice, DRM_IOCTL_VMW_UPDATE_LAYOUT,
166 &ioctlLayout, sizeof(ioctlLayout), NULL);
167 if (RT_FAILURE(rc) && rc != VERR_INVALID_PARAMETER)
168 VBClLogFatalError("Failure updating layout, rc=%Rrc\n", rc);
169}
170
171static const char *getName()
172{
173 return "Display SVGA";
174}
175
176static const char *getPidFilePath()
177{
178 return ".vboxclient-display-svga.pid";
179}
180
181static int run(struct VBCLSERVICE **ppInterface, bool fDaemonised)
182{
183 (void)ppInterface;
184 (void)fDaemonised;
185 struct DRMCONTEXT drmContext = { NIL_RTFILE };
186 unsigned i;
187 int rc;
188 /* Do not acknowledge the first event we query for to pick up old events,
189 * e.g. from before a guest reboot. */
190 bool fAck = false;
191
192 drmConnect(&drmContext);
193 if (drmContext.hDevice == NIL_RTFILE)
194 return VINF_SUCCESS;
195 rc = VbglR3CtlFilterMask(VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST, 0);
196 if (RT_FAILURE(rc))
197 VBClLogFatalError("Failed to request display change events, rc=%Rrc\n", rc);
198 rc = VbglR3AcquireGuestCaps(VMMDEV_GUEST_SUPPORTS_GRAPHICS, 0, false);
199 if (rc == VERR_RESOURCE_BUSY) /* Someone else has already acquired it. */
200 return VINF_SUCCESS;
201 if (RT_FAILURE(rc))
202 VBClLogFatalError("Failed to register resizing support, rc=%Rrc\n", rc);
203 for (;;)
204 {
205 uint32_t events;
206 struct VMMDevDisplayDef aDisplays[VMW_MAX_HEADS];
207 uint32_t cDisplaysOut;
208
209 /* Query the first size without waiting. This lets us e.g. pick up
210 * the last event before a guest reboot when we start again after. */
211 rc = VbglR3GetDisplayChangeRequestMulti(VMW_MAX_HEADS, &cDisplaysOut, aDisplays, fAck);
212 fAck = true;
213 if (RT_FAILURE(rc))
214 VBClLogFatalError("Failed to get display change request, rc=%Rrc\n", rc);
215 if (cDisplaysOut > VMW_MAX_HEADS)
216 VBClLogFatalError("Display change request contained, rc=%Rrc\n", rc);
217 if (cDisplaysOut > 0)
218 {
219 for (i = 0; i < cDisplaysOut && i < VMW_MAX_HEADS; ++i)
220 {
221 if (!(aDisplays[i].fDisplayFlags & VMMDEV_DISPLAY_DISABLED))
222 {
223 uint32_t idDisplay = aDisplays[i].idDisplay;
224 if (idDisplay < VMW_MAX_HEADS)
225 {
226 if ((idDisplay == 0) || (aDisplays[i].fDisplayFlags & VMMDEV_DISPLAY_ORIGIN))
227 {
228 aRects[idDisplay].x = aDisplays[i].xOrigin;
229 aRects[idDisplay].y = aDisplays[i].yOrigin;
230 } else {
231 aRects[idDisplay].x = aRects[idDisplay - 1].x + aRects[idDisplay - 1].w;
232 aRects[idDisplay].y = aRects[idDisplay - 1].y;
233 }
234 aRects[idDisplay].w = aDisplays[i].cx;
235 aRects[idDisplay].h = aDisplays[i].cy;
236 }
237 }
238 }
239 for (i = 0; i < cDisplaysOut; ++i)
240 printf("Head %u: %dx%d, (%d, %d)\n", i, (int)aRects[i].w, (int)aRects[i].h,
241 (int)aRects[i].x, (int)aRects[i].y);
242 drmSendHints(&drmContext, aRects, VMW_MAX_HEADS);
243 }
244 do
245 {
246 rc = VbglR3WaitEvent(VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST, RT_INDEFINITE_WAIT, &events);
247 } while (rc == VERR_INTERRUPTED);
248 if (RT_FAILURE(rc))
249 VBClLogFatalError("Failure waiting for event, rc=%Rrc\n", rc);
250 }
251}
252
253static struct VBCLSERVICE interface =
254{
255 getName,
256 getPidFilePath,
257 VBClServiceDefaultHandler, /* Init */
258 run,
259 VBClServiceDefaultCleanup
260}, *pInterface = &interface;
261
262struct VBCLSERVICE **VBClDisplaySVGAService()
263{
264 return &pInterface;
265}
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