VirtualBox

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

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

bugref:9637. Caching monitor information on the guest side to send DRM a more complete data.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.3 KB
Line 
1/* $Id: display-svga.cpp 82680 2020-01-08 20:06:11Z 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
91struct DISPLAYCACHE
92{
93 int fEnabled;
94 struct DRMVMWRECT rect;
95};
96
97#define DRM_IOCTL_VERSION _IOWR('d', 0x00, struct DRMVERSION)
98
99struct DRMCONTEXT
100{
101 RTFILE hDevice;
102};
103
104struct DISPLAYCACHE aRects[VMW_MAX_HEADS];
105
106static void drmConnect(struct DRMCONTEXT *pContext)
107{
108 unsigned i;
109 RTFILE hDevice;
110
111 if (pContext->hDevice != NIL_RTFILE)
112 VBClLogFatalError("%s called with bad argument\n", __func__);
113 /* Try to open the SVGA DRM device. */
114 for (i = 0; i < 128; ++i)
115 {
116 char szPath[64];
117 struct DRMVERSION version;
118 char szName[sizeof(DRM_DRIVER_NAME)];
119 int rc;
120
121 /* Control devices for drm graphics driver control devices go from
122 * controlD64 to controlD127. Render node devices go from renderD128
123 * to renderD192. The driver takes resize hints via the control device
124 * on pre-4.10 kernels and on the render device on newer ones. Try
125 * both types. */
126 if (i % 2 == 0)
127 rc = RTStrPrintf(szPath, sizeof(szPath), "/dev/dri/renderD%u", i / 2 + 128);
128 else
129 rc = RTStrPrintf(szPath, sizeof(szPath), "/dev/dri/controlD%u", i / 2 + 64);
130 if (RT_FAILURE(rc))
131 VBClLogFatalError("RTStrPrintf of device path failed, rc=%Rrc\n", rc);
132 rc = RTFileOpen(&hDevice, szPath, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
133 if (RT_FAILURE(rc))
134 continue;
135 RT_ZERO(version);
136 version.cbName = sizeof(szName);
137 version.pszName = szName;
138 rc = RTFileIoCtl(hDevice, DRM_IOCTL_VERSION, &version, sizeof(version), NULL);
139 if ( RT_SUCCESS(rc)
140 && !strncmp(szName, DRM_DRIVER_NAME, sizeof(DRM_DRIVER_NAME) - 1)
141 && ( version.cMajor > 2
142 || (version.cMajor == 2 && version.cMinor > 9)))
143 break;
144 hDevice = NIL_RTFILE;
145 }
146 pContext->hDevice = hDevice;
147}
148
149/** Preferred screen layout information for DRM_VMW_UPDATE_LAYOUT IoCtl. The
150 * rects argument is a cast pointer to an array of drm_vmw_rect. */
151struct DRMVMWUPDATELAYOUT {
152 uint32_t cOutputs;
153 uint32_t u32Pad;
154 uint64_t ptrRects;
155};
156AssertCompileSize(struct DRMVMWUPDATELAYOUT, 16);
157
158#define DRM_IOCTL_VMW_UPDATE_LAYOUT \
159 _IOW('d', 0x40 + 20, struct DRMVMWUPDATELAYOUT)
160
161static void drmSendHints(struct DRMCONTEXT *pContext, struct DRMVMWRECT *paRects,
162 unsigned cHeads)
163{
164 int rc;
165 struct DRMVMWUPDATELAYOUT ioctlLayout;
166
167 if (pContext->hDevice == NIL_RTFILE)
168 VBClLogFatalError("%s bad device argument\n", __func__);
169 ioctlLayout.cOutputs = cHeads;
170 ioctlLayout.ptrRects = (uint64_t)paRects;
171 rc = RTFileIoCtl(pContext->hDevice, DRM_IOCTL_VMW_UPDATE_LAYOUT,
172 &ioctlLayout, sizeof(ioctlLayout), NULL);
173 if (RT_FAILURE(rc) && rc != VERR_INVALID_PARAMETER)
174 VBClLogFatalError("Failure updating layout, rc=%Rrc\n", rc);
175}
176
177static const char *getName()
178{
179 return "Display SVGA";
180}
181
182static const char *getPidFilePath()
183{
184 return ".vboxclient-display-svga.pid";
185}
186
187static int run(struct VBCLSERVICE **ppInterface, bool fDaemonised)
188{
189 (void)ppInterface;
190 (void)fDaemonised;
191 struct DRMCONTEXT drmContext = { NIL_RTFILE };
192
193 int rc;
194 unsigned cHeads;
195 /* Do not acknowledge the first event we query for to pick up old events,
196 * e.g. from before a guest reboot. */
197 bool fAck = false;
198 drmConnect(&drmContext);
199 if (drmContext.hDevice == NIL_RTFILE)
200 return VINF_SUCCESS;
201 rc = VbglR3CtlFilterMask(VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST, 0);
202 if (RT_FAILURE(rc))
203 VBClLogFatalError("Failed to request display change events, rc=%Rrc\n", rc);
204 rc = VbglR3AcquireGuestCaps(VMMDEV_GUEST_SUPPORTS_GRAPHICS, 0, false);
205 if (rc == VERR_RESOURCE_BUSY) /* Someone else has already acquired it. */
206 return VINF_SUCCESS;
207 if (RT_FAILURE(rc))
208 VBClLogFatalError("Failed to register resizing support, rc=%Rrc\n", rc);
209
210 /* For the time being we initialize all the monitors as disabled as per VMM device initialization. */
211 for (int i = 0; i < VMW_MAX_HEADS; ++i)
212 aRects[i].fEnabled = 0;
213
214 for (;;)
215 {
216 uint32_t events;
217 struct VMMDevDisplayDef aDisplays[VMW_MAX_HEADS];
218 uint32_t cDisplaysOut;
219
220 /* Query the first size without waiting. This lets us e.g. pick up
221 * the last event before a guest reboot when we start again after. */
222 rc = VbglR3GetDisplayChangeRequestMulti(VMW_MAX_HEADS, &cDisplaysOut, aDisplays, fAck);
223 fAck = true;
224 if (RT_FAILURE(rc))
225 VBClLogFatalError("Failed to get display change request, rc=%Rrc\n", rc);
226 if (cDisplaysOut > VMW_MAX_HEADS)
227 VBClLogFatalError("Display change request contained, rc=%Rrc\n", rc);
228 if (cDisplaysOut > 0)
229 {
230 for (unsigned i = 0; i < cDisplaysOut && i < VMW_MAX_HEADS; ++i)
231 {
232 uint32_t idDisplay = aDisplays[i].idDisplay;
233 if (idDisplay >= VMW_MAX_HEADS)
234 continue;
235 if (!(aDisplays[i].fDisplayFlags & VMMDEV_DISPLAY_DISABLED))
236 {
237 if ((idDisplay == 0) || (aDisplays[i].fDisplayFlags & VMMDEV_DISPLAY_ORIGIN))
238 {
239 aRects[idDisplay].rect.x = aDisplays[i].xOrigin;
240 aRects[idDisplay].rect.y = aDisplays[i].yOrigin;
241 } else {
242 aRects[idDisplay].rect.x = aRects[idDisplay - 1].rect.x + aRects[idDisplay - 1].rect.w;
243 aRects[idDisplay].rect.y = aRects[idDisplay - 1].rect.y;
244 }
245 aRects[idDisplay].rect.w = aDisplays[i].cx;
246 aRects[idDisplay].rect.h = aDisplays[i].cy;
247 aRects[idDisplay].fEnabled = 1;
248 }
249 else
250 aRects[idDisplay].fEnabled = 0;
251 }
252 /* Create an dense (consisting of enable monitors only) array to pass to DRM. */
253 cHeads = 0;
254 struct DRMVMWRECT enabledMonitors[VMW_MAX_HEADS];
255
256 for (int j = 0; j < VMW_MAX_HEADS; ++j)
257 {
258 if (aRects[j].fEnabled)
259 {
260 enabledMonitors[cHeads] = aRects[j].rect;
261 if (cHeads > 0)
262 enabledMonitors[cHeads].x = enabledMonitors[cHeads - 1].x + enabledMonitors[cHeads - 1].w;
263 ++cHeads;
264 }
265 }
266
267 for (unsigned i = 0; i < cHeads; ++i)
268 printf("Monitor %u: %dx%d, (%d, %d)\n", i, (int)enabledMonitors[i].w, (int)enabledMonitors[i].h,
269 (int)enabledMonitors[i].x, (int)enabledMonitors[i].y);
270 drmSendHints(&drmContext, enabledMonitors, cHeads);
271 }
272 do
273 {
274 rc = VbglR3WaitEvent(VMMDEV_EVENT_DISPLAY_CHANGE_REQUEST, RT_INDEFINITE_WAIT, &events);
275 } while (rc == VERR_INTERRUPTED);
276 if (RT_FAILURE(rc))
277 VBClLogFatalError("Failure waiting for event, rc=%Rrc\n", rc);
278 }
279}
280
281static struct VBCLSERVICE interface =
282{
283 getName,
284 getPidFilePath,
285 VBClServiceDefaultHandler, /* Init */
286 run,
287 VBClServiceDefaultCleanup
288}, *pInterface = &interface;
289
290struct VBCLSERVICE **VBClDisplaySVGAService()
291{
292 return &pInterface;
293}
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