1 | /* $Id: GraphicsAdapterImpl.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Implementation of IGraphicsAdapter in VBoxSVC.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2004-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | #define LOG_GROUP LOG_GROUP_MAIN_GRAPHICSADAPTER
|
---|
29 |
|
---|
30 | #include "LoggingNew.h"
|
---|
31 |
|
---|
32 | #include "GraphicsAdapterImpl.h"
|
---|
33 | #include "MachineImpl.h"
|
---|
34 |
|
---|
35 | #include "AutoStateDep.h"
|
---|
36 | #include "AutoCaller.h"
|
---|
37 |
|
---|
38 | #include <iprt/cpp/utils.h>
|
---|
39 |
|
---|
40 |
|
---|
41 | // constructor / destructor
|
---|
42 | /////////////////////////////////////////////////////////////////////////////
|
---|
43 |
|
---|
44 | GraphicsAdapter::GraphicsAdapter() :
|
---|
45 | mParent(NULL)
|
---|
46 | {}
|
---|
47 |
|
---|
48 | GraphicsAdapter::~GraphicsAdapter()
|
---|
49 | {}
|
---|
50 |
|
---|
51 | HRESULT GraphicsAdapter::FinalConstruct()
|
---|
52 | {
|
---|
53 | LogFlowThisFunc(("\n"));
|
---|
54 | return BaseFinalConstruct();
|
---|
55 | }
|
---|
56 |
|
---|
57 | void GraphicsAdapter::FinalRelease()
|
---|
58 | {
|
---|
59 | LogFlowThisFunc(("\n"));
|
---|
60 | uninit();
|
---|
61 | BaseFinalRelease();
|
---|
62 | }
|
---|
63 |
|
---|
64 | // public initializer/uninitializer for internal purposes only
|
---|
65 | /////////////////////////////////////////////////////////////////////////////
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Initializes the graphics adapter object.
|
---|
69 | *
|
---|
70 | * @param aParent Handle of the parent object.
|
---|
71 | */
|
---|
72 | HRESULT GraphicsAdapter::init(Machine *aParent)
|
---|
73 | {
|
---|
74 | LogFlowThisFunc(("aParent=%p\n", aParent));
|
---|
75 |
|
---|
76 | ComAssertRet(aParent, E_INVALIDARG);
|
---|
77 |
|
---|
78 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
79 | AutoInitSpan autoInitSpan(this);
|
---|
80 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
81 |
|
---|
82 | unconst(mParent) = aParent;
|
---|
83 | /* mPeer is left null */
|
---|
84 |
|
---|
85 | mData.allocate();
|
---|
86 |
|
---|
87 | /* Confirm a successful initialization */
|
---|
88 | autoInitSpan.setSucceeded();
|
---|
89 |
|
---|
90 | return S_OK;
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Initializes the graphics adapter object given another graphics adapter
|
---|
95 | * object (a kind of copy constructor). This object shares data with
|
---|
96 | * the object passed as an argument.
|
---|
97 | *
|
---|
98 | * @note This object must be destroyed before the original object
|
---|
99 | * it shares data with is destroyed.
|
---|
100 | *
|
---|
101 | * @note Locks @a aThat object for reading.
|
---|
102 | */
|
---|
103 | HRESULT GraphicsAdapter::init(Machine *aParent, GraphicsAdapter *aThat)
|
---|
104 | {
|
---|
105 | LogFlowThisFunc(("aParent=%p, aThat=%p\n", aParent, aThat));
|
---|
106 |
|
---|
107 | ComAssertRet(aParent && aThat, E_INVALIDARG);
|
---|
108 |
|
---|
109 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
110 | AutoInitSpan autoInitSpan(this);
|
---|
111 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
112 |
|
---|
113 | unconst(mParent) = aParent;
|
---|
114 | unconst(mPeer) = aThat;
|
---|
115 |
|
---|
116 | AutoCaller thatCaller(aThat);
|
---|
117 | AssertComRCReturnRC(thatCaller.rc());
|
---|
118 |
|
---|
119 | AutoReadLock thatLock(aThat COMMA_LOCKVAL_SRC_POS);
|
---|
120 | mData.share(aThat->mData);
|
---|
121 |
|
---|
122 | /* Confirm a successful initialization */
|
---|
123 | autoInitSpan.setSucceeded();
|
---|
124 |
|
---|
125 | return S_OK;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Initializes the graphics adapter object given another graphics adapter
|
---|
130 | * object (a kind of copy constructor). This object makes a private copy
|
---|
131 | * of data of the original object passed as an argument.
|
---|
132 | *
|
---|
133 | * @note Locks @a aThat object for reading.
|
---|
134 | */
|
---|
135 | HRESULT GraphicsAdapter::initCopy(Machine *aParent, GraphicsAdapter *aThat)
|
---|
136 | {
|
---|
137 | LogFlowThisFunc(("aParent=%p, aThat=%p\n", aParent, aThat));
|
---|
138 |
|
---|
139 | ComAssertRet(aParent && aThat, E_INVALIDARG);
|
---|
140 |
|
---|
141 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
142 | AutoInitSpan autoInitSpan(this);
|
---|
143 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
144 |
|
---|
145 | unconst(mParent) = aParent;
|
---|
146 | /* mPeer is left null */
|
---|
147 |
|
---|
148 | AutoCaller thatCaller(aThat);
|
---|
149 | AssertComRCReturnRC(thatCaller.rc());
|
---|
150 |
|
---|
151 | AutoReadLock thatLock(aThat COMMA_LOCKVAL_SRC_POS);
|
---|
152 | mData.attachCopy(aThat->mData);
|
---|
153 |
|
---|
154 | /* Confirm a successful initialization */
|
---|
155 | autoInitSpan.setSucceeded();
|
---|
156 |
|
---|
157 | return S_OK;
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Uninitializes the instance and sets the ready flag to FALSE.
|
---|
162 | * Called either from FinalRelease() or by the parent when it gets destroyed.
|
---|
163 | */
|
---|
164 | void GraphicsAdapter::uninit()
|
---|
165 | {
|
---|
166 | LogFlowThisFunc(("\n"));
|
---|
167 |
|
---|
168 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
169 | AutoUninitSpan autoUninitSpan(this);
|
---|
170 | if (autoUninitSpan.uninitDone())
|
---|
171 | return;
|
---|
172 |
|
---|
173 | mData.free();
|
---|
174 |
|
---|
175 | unconst(mPeer) = NULL;
|
---|
176 | unconst(mParent) = NULL;
|
---|
177 | }
|
---|
178 |
|
---|
179 | // Wrapped IGraphicsAdapter properties
|
---|
180 | /////////////////////////////////////////////////////////////////////////////
|
---|
181 |
|
---|
182 | HRESULT GraphicsAdapter::getGraphicsControllerType(GraphicsControllerType_T *aGraphicsControllerType)
|
---|
183 | {
|
---|
184 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
185 |
|
---|
186 | *aGraphicsControllerType = mData->graphicsControllerType;
|
---|
187 |
|
---|
188 | return S_OK;
|
---|
189 | }
|
---|
190 |
|
---|
191 | HRESULT GraphicsAdapter::setGraphicsControllerType(GraphicsControllerType_T aGraphicsControllerType)
|
---|
192 | {
|
---|
193 | switch (aGraphicsControllerType)
|
---|
194 | {
|
---|
195 | case GraphicsControllerType_Null:
|
---|
196 | case GraphicsControllerType_VBoxVGA:
|
---|
197 | #ifdef VBOX_WITH_VMSVGA
|
---|
198 | case GraphicsControllerType_VMSVGA:
|
---|
199 | case GraphicsControllerType_VBoxSVGA:
|
---|
200 | #endif
|
---|
201 | break;
|
---|
202 | default:
|
---|
203 | return setError(E_INVALIDARG, tr("The graphics controller type (%d) is invalid"), aGraphicsControllerType);
|
---|
204 | }
|
---|
205 |
|
---|
206 | /* the machine needs to be mutable */
|
---|
207 | AutoMutableStateDependency adep(mParent);
|
---|
208 | if (FAILED(adep.rc())) return adep.rc();
|
---|
209 |
|
---|
210 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
211 |
|
---|
212 | mParent->i_setModified(Machine::IsModified_GraphicsAdapter);
|
---|
213 | mData.backup();
|
---|
214 | mData->graphicsControllerType = aGraphicsControllerType;
|
---|
215 |
|
---|
216 | return S_OK;
|
---|
217 | }
|
---|
218 |
|
---|
219 | HRESULT GraphicsAdapter::getVRAMSize(ULONG *aVRAMSize)
|
---|
220 | {
|
---|
221 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
222 |
|
---|
223 | *aVRAMSize = mData->ulVRAMSizeMB;
|
---|
224 |
|
---|
225 | return S_OK;
|
---|
226 | }
|
---|
227 |
|
---|
228 | HRESULT GraphicsAdapter::setVRAMSize(ULONG aVRAMSize)
|
---|
229 | {
|
---|
230 | /* check VRAM limits */
|
---|
231 | if (aVRAMSize > SchemaDefs::MaxGuestVRAM)
|
---|
232 | return setError(E_INVALIDARG,
|
---|
233 | tr("Invalid VRAM size: %lu MB (must be in range [%lu, %lu] MB)"),
|
---|
234 | aVRAMSize, SchemaDefs::MinGuestVRAM, SchemaDefs::MaxGuestVRAM);
|
---|
235 |
|
---|
236 | /* the machine needs to be mutable */
|
---|
237 | AutoMutableStateDependency adep(mParent);
|
---|
238 | if (FAILED(adep.rc())) return adep.rc();
|
---|
239 |
|
---|
240 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
241 |
|
---|
242 | mParent->i_setModified(Machine::IsModified_GraphicsAdapter);
|
---|
243 | mData.backup();
|
---|
244 | mData->ulVRAMSizeMB = aVRAMSize;
|
---|
245 |
|
---|
246 | return S_OK;
|
---|
247 | }
|
---|
248 |
|
---|
249 | HRESULT GraphicsAdapter::getAccelerate3DEnabled(BOOL *aAccelerate3DEnabled)
|
---|
250 | {
|
---|
251 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
252 |
|
---|
253 | *aAccelerate3DEnabled = mData->fAccelerate3D;
|
---|
254 |
|
---|
255 | return S_OK;
|
---|
256 | }
|
---|
257 |
|
---|
258 | HRESULT GraphicsAdapter::setAccelerate3DEnabled(BOOL aAccelerate3DEnabled)
|
---|
259 | {
|
---|
260 | /* the machine needs to be mutable */
|
---|
261 | AutoMutableStateDependency adep(mParent);
|
---|
262 | if (FAILED(adep.rc())) return adep.rc();
|
---|
263 |
|
---|
264 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
265 |
|
---|
266 | /** @todo check validity! */
|
---|
267 |
|
---|
268 | mParent->i_setModified(Machine::IsModified_GraphicsAdapter);
|
---|
269 | mData.backup();
|
---|
270 | mData->fAccelerate3D = !!aAccelerate3DEnabled;
|
---|
271 |
|
---|
272 | return S_OK;
|
---|
273 | }
|
---|
274 |
|
---|
275 |
|
---|
276 | HRESULT GraphicsAdapter::getAccelerate2DVideoEnabled(BOOL *aAccelerate2DVideoEnabled)
|
---|
277 | {
|
---|
278 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
279 |
|
---|
280 | /* bugref:9691 The legacy VHWA acceleration has been disabled completely. */
|
---|
281 | *aAccelerate2DVideoEnabled = FALSE;
|
---|
282 |
|
---|
283 | return S_OK;
|
---|
284 | }
|
---|
285 |
|
---|
286 | HRESULT GraphicsAdapter::setAccelerate2DVideoEnabled(BOOL aAccelerate2DVideoEnabled)
|
---|
287 | {
|
---|
288 | /* the machine needs to be mutable */
|
---|
289 | AutoMutableStateDependency adep(mParent);
|
---|
290 | if (FAILED(adep.rc())) return adep.rc();
|
---|
291 |
|
---|
292 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
293 |
|
---|
294 | /** @todo check validity! */
|
---|
295 |
|
---|
296 | mParent->i_setModified(Machine::IsModified_GraphicsAdapter);
|
---|
297 | mData.backup();
|
---|
298 | mData->fAccelerate2DVideo = !!aAccelerate2DVideoEnabled;
|
---|
299 |
|
---|
300 | return S_OK;
|
---|
301 | }
|
---|
302 |
|
---|
303 | HRESULT GraphicsAdapter::getMonitorCount(ULONG *aMonitorCount)
|
---|
304 | {
|
---|
305 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
306 |
|
---|
307 | *aMonitorCount = mData->cMonitors;
|
---|
308 |
|
---|
309 | return S_OK;
|
---|
310 | }
|
---|
311 |
|
---|
312 | HRESULT GraphicsAdapter::setMonitorCount(ULONG aMonitorCount)
|
---|
313 | {
|
---|
314 | /* make sure monitor count is a sensible number */
|
---|
315 | if (aMonitorCount < 1 || aMonitorCount > SchemaDefs::MaxGuestMonitors)
|
---|
316 | return setError(E_INVALIDARG,
|
---|
317 | tr("Invalid monitor count: %lu (must be in range [%lu, %lu])"),
|
---|
318 | aMonitorCount, 1, SchemaDefs::MaxGuestMonitors);
|
---|
319 |
|
---|
320 | /* the machine needs to be mutable */
|
---|
321 | AutoMutableStateDependency adep(mParent);
|
---|
322 | if (FAILED(adep.rc())) return adep.rc();
|
---|
323 |
|
---|
324 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
325 |
|
---|
326 | mParent->i_setModified(Machine::IsModified_GraphicsAdapter);
|
---|
327 | mData.backup();
|
---|
328 | mData->cMonitors = aMonitorCount;
|
---|
329 |
|
---|
330 | return S_OK;
|
---|
331 | }
|
---|
332 |
|
---|
333 | // Wrapped IGraphicsAdapter methods
|
---|
334 | /////////////////////////////////////////////////////////////////////////////
|
---|
335 |
|
---|
336 | // public methods only for internal purposes
|
---|
337 | /////////////////////////////////////////////////////////////////////////////
|
---|
338 |
|
---|
339 | /**
|
---|
340 | * Loads settings from the given machine node.
|
---|
341 | * May be called once right after this object creation.
|
---|
342 | *
|
---|
343 | * @param data Configuration settings.
|
---|
344 | *
|
---|
345 | * @note Locks this object for writing.
|
---|
346 | */
|
---|
347 | HRESULT GraphicsAdapter::i_loadSettings(const settings::GraphicsAdapter &data)
|
---|
348 | {
|
---|
349 | AutoCaller autoCaller(this);
|
---|
350 | AssertComRCReturnRC(autoCaller.rc());
|
---|
351 |
|
---|
352 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
353 |
|
---|
354 | mData.assignCopy(&data);
|
---|
355 |
|
---|
356 | return S_OK;
|
---|
357 | }
|
---|
358 |
|
---|
359 | /**
|
---|
360 | * Saves settings to the given machine node.
|
---|
361 | *
|
---|
362 | * @param data Configuration settings.
|
---|
363 | *
|
---|
364 | * @note Locks this object for reading.
|
---|
365 | */
|
---|
366 | HRESULT GraphicsAdapter::i_saveSettings(settings::GraphicsAdapter &data)
|
---|
367 | {
|
---|
368 | AutoCaller autoCaller(this);
|
---|
369 | AssertComRCReturnRC(autoCaller.rc());
|
---|
370 |
|
---|
371 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
372 |
|
---|
373 | data = *mData.data();
|
---|
374 |
|
---|
375 | return S_OK;
|
---|
376 | }
|
---|
377 |
|
---|
378 | /**
|
---|
379 | * @note Locks this object for writing.
|
---|
380 | */
|
---|
381 | void GraphicsAdapter::i_rollback()
|
---|
382 | {
|
---|
383 | /* sanity */
|
---|
384 | AutoCaller autoCaller(this);
|
---|
385 | AssertComRCReturnVoid(autoCaller.rc());
|
---|
386 |
|
---|
387 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
388 |
|
---|
389 | mData.rollback();
|
---|
390 | }
|
---|
391 |
|
---|
392 | /**
|
---|
393 | * @note Locks this object for writing, together with the peer object (also
|
---|
394 | * for writing) if there is one.
|
---|
395 | */
|
---|
396 | void GraphicsAdapter::i_commit()
|
---|
397 | {
|
---|
398 | /* sanity */
|
---|
399 | AutoCaller autoCaller(this);
|
---|
400 | AssertComRCReturnVoid(autoCaller.rc());
|
---|
401 |
|
---|
402 | /* sanity too */
|
---|
403 | AutoCaller peerCaller(mPeer);
|
---|
404 | AssertComRCReturnVoid(peerCaller.rc());
|
---|
405 |
|
---|
406 | /* lock both for writing since we modify both (mPeer is "master" so locked
|
---|
407 | * first) */
|
---|
408 | AutoMultiWriteLock2 alock(mPeer, this COMMA_LOCKVAL_SRC_POS);
|
---|
409 |
|
---|
410 | if (mData.isBackedUp())
|
---|
411 | {
|
---|
412 | mData.commit();
|
---|
413 | if (mPeer)
|
---|
414 | {
|
---|
415 | /* attach new data to the peer and reshare it */
|
---|
416 | mPeer->mData.attach(mData);
|
---|
417 | }
|
---|
418 | }
|
---|
419 | }
|
---|
420 |
|
---|
421 | /**
|
---|
422 | * @note Locks this object for writing, together with the peer object
|
---|
423 | * represented by @a aThat (locked for reading).
|
---|
424 | */
|
---|
425 | void GraphicsAdapter::i_copyFrom(GraphicsAdapter *aThat)
|
---|
426 | {
|
---|
427 | AssertReturnVoid(aThat != NULL);
|
---|
428 |
|
---|
429 | /* sanity */
|
---|
430 | AutoCaller autoCaller(this);
|
---|
431 | AssertComRCReturnVoid(autoCaller.rc());
|
---|
432 |
|
---|
433 | /* sanity too */
|
---|
434 | AutoCaller thatCaller(aThat);
|
---|
435 | AssertComRCReturnVoid(thatCaller.rc());
|
---|
436 |
|
---|
437 | /* peer is not modified, lock it for reading (aThat is "master" so locked
|
---|
438 | * first) */
|
---|
439 | AutoReadLock rl(aThat COMMA_LOCKVAL_SRC_POS);
|
---|
440 | AutoWriteLock wl(this COMMA_LOCKVAL_SRC_POS);
|
---|
441 |
|
---|
442 | /* this will back up current data */
|
---|
443 | mData.assignCopy(aThat->mData);
|
---|
444 | }
|
---|
445 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|