VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/GraphicsAdapterImpl.cpp@ 105659

Last change on this file since 105659 was 104819, checked in by vboxsync, 6 months ago

Main: Also make use of PlatformPropertiesImpl::s_getSupportedVRAMRange() in GraphicsAdapter::setVRAMSize(); also take the SchemaDefs in PlatformPropertiesImpl::s_getSupportedVRAMRange() into account. bugref:​10693

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.0 KB
Line 
1/* $Id: GraphicsAdapterImpl.cpp 104819 2024-05-30 09:17:47Z vboxsync $ */
2/** @file
3 * Implementation of IGraphicsAdapter in VBoxSVC.
4 */
5
6/*
7 * Copyright (C) 2004-2023 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
44GraphicsAdapter::GraphicsAdapter() :
45 mParent(NULL)
46{}
47
48GraphicsAdapter::~GraphicsAdapter()
49{}
50
51HRESULT GraphicsAdapter::FinalConstruct()
52{
53 LogFlowThisFunc(("\n"));
54 return BaseFinalConstruct();
55}
56
57void 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 */
72HRESULT 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 */
103HRESULT 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.hrc());
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 */
135HRESULT 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.hrc());
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 */
164void 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
182HRESULT 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
191HRESULT 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 case GraphicsControllerType_QemuRamFB:
202 break;
203 default:
204 return setError(E_INVALIDARG, tr("The graphics controller type (%d) is invalid"), aGraphicsControllerType);
205 }
206
207 /* the machine needs to be mutable */
208 AutoMutableStateDependency adep(mParent);
209 if (FAILED(adep.hrc())) return adep.hrc();
210
211 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
212
213 mParent->i_setModified(Machine::IsModified_GraphicsAdapter);
214 mData.backup();
215 mData->graphicsControllerType = aGraphicsControllerType;
216
217 return S_OK;
218}
219
220HRESULT GraphicsAdapter::getVRAMSize(ULONG *aVRAMSize)
221{
222 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
223
224 *aVRAMSize = mData->ulVRAMSizeMB;
225
226 return S_OK;
227}
228
229HRESULT GraphicsAdapter::setVRAMSize(ULONG aVRAMSize)
230{
231 /* the machine needs to be mutable */
232 AutoMutableStateDependency adep(mParent);
233 if (FAILED(adep.hrc())) return adep.hrc();
234
235 ULONG uMin, uMax;
236 HRESULT hrc = PlatformProperties::s_getSupportedVRAMRange(mData->graphicsControllerType, mData->fAccelerate3D,
237 &uMin, &uMax, NULL /* aStrideSizeMB */);
238 if (FAILED(hrc))
239 return setError(hrc,
240 tr("Error getting VRAM range for selected graphics controller"));
241
242 /* check VRAM limits */
243 if ( aVRAMSize < uMin
244 || aVRAMSize > uMax)
245 return setError(E_INVALIDARG,
246 tr("Invalid VRAM size: %lu MB (must be in range [%lu, %lu] MB)"),
247 aVRAMSize, uMin, uMax);
248
249 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
250
251 mParent->i_setModified(Machine::IsModified_GraphicsAdapter);
252 mData.backup();
253 mData->ulVRAMSizeMB = aVRAMSize;
254
255 return S_OK;
256}
257
258HRESULT GraphicsAdapter::getAccelerate3DEnabled(BOOL *aAccelerate3DEnabled)
259{
260 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
261
262 *aAccelerate3DEnabled = mData->fAccelerate3D;
263
264 return S_OK;
265}
266
267HRESULT GraphicsAdapter::setAccelerate3DEnabled(BOOL aAccelerate3DEnabled)
268{
269 /* the machine needs to be mutable */
270 AutoMutableStateDependency adep(mParent);
271 if (FAILED(adep.hrc())) return adep.hrc();
272
273 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
274
275 /** @todo check validity! */
276
277 mParent->i_setModified(Machine::IsModified_GraphicsAdapter);
278 mData.backup();
279 mData->fAccelerate3D = !!aAccelerate3DEnabled;
280
281 return S_OK;
282}
283
284
285HRESULT GraphicsAdapter::getAccelerate2DVideoEnabled(BOOL *aAccelerate2DVideoEnabled)
286{
287 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
288
289 /* bugref:9691 The legacy VHWA acceleration has been disabled completely. */
290 *aAccelerate2DVideoEnabled = FALSE;
291
292 return S_OK;
293}
294
295HRESULT GraphicsAdapter::setAccelerate2DVideoEnabled(BOOL aAccelerate2DVideoEnabled)
296{
297 /* the machine needs to be mutable */
298 AutoMutableStateDependency adep(mParent);
299 if (FAILED(adep.hrc())) return adep.hrc();
300
301 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
302
303 /** @todo check validity! */
304
305 mParent->i_setModified(Machine::IsModified_GraphicsAdapter);
306 mData.backup();
307 mData->fAccelerate2DVideo = !!aAccelerate2DVideoEnabled;
308
309 return S_OK;
310}
311
312HRESULT GraphicsAdapter::getMonitorCount(ULONG *aMonitorCount)
313{
314 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
315
316 *aMonitorCount = mData->cMonitors;
317
318 return S_OK;
319}
320
321HRESULT GraphicsAdapter::setMonitorCount(ULONG aMonitorCount)
322{
323 /* make sure monitor count is a sensible number */
324 if (aMonitorCount < 1 || aMonitorCount > SchemaDefs::MaxGuestMonitors)
325 return setError(E_INVALIDARG,
326 tr("Invalid monitor count: %lu (must be in range [%lu, %lu])"),
327 aMonitorCount, 1, SchemaDefs::MaxGuestMonitors);
328
329 /* the machine needs to be mutable */
330 AutoMutableStateDependency adep(mParent);
331 if (FAILED(adep.hrc())) return adep.hrc();
332
333 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
334
335 mParent->i_setModified(Machine::IsModified_GraphicsAdapter);
336 mData.backup();
337 mData->cMonitors = aMonitorCount;
338
339 return S_OK;
340}
341
342// Wrapped IGraphicsAdapter methods
343/////////////////////////////////////////////////////////////////////////////
344
345// public methods only for internal purposes
346/////////////////////////////////////////////////////////////////////////////
347
348/**
349 * Loads settings from the given machine node.
350 * May be called once right after this object creation.
351 *
352 * @param data Configuration settings.
353 *
354 * @note Locks this object for writing.
355 */
356HRESULT GraphicsAdapter::i_loadSettings(const settings::GraphicsAdapter &data)
357{
358 AutoCaller autoCaller(this);
359 AssertComRCReturnRC(autoCaller.hrc());
360
361 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
362
363 mData.assignCopy(&data);
364
365 return S_OK;
366}
367
368/**
369 * Saves settings to the given machine node.
370 *
371 * @param data Configuration settings.
372 *
373 * @note Locks this object for reading.
374 */
375HRESULT GraphicsAdapter::i_saveSettings(settings::GraphicsAdapter &data)
376{
377 AutoCaller autoCaller(this);
378 AssertComRCReturnRC(autoCaller.hrc());
379
380 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
381
382 data = *mData.data();
383
384 return S_OK;
385}
386
387/**
388 * @note Locks this object for writing.
389 */
390void GraphicsAdapter::i_rollback()
391{
392 /* sanity */
393 AutoCaller autoCaller(this);
394 AssertComRCReturnVoid(autoCaller.hrc());
395
396 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
397
398 mData.rollback();
399}
400
401/**
402 * @note Locks this object for writing, together with the peer object (also
403 * for writing) if there is one.
404 */
405void GraphicsAdapter::i_commit()
406{
407 /* sanity */
408 AutoCaller autoCaller(this);
409 AssertComRCReturnVoid(autoCaller.hrc());
410
411 /* sanity too */
412 AutoCaller peerCaller(mPeer);
413 AssertComRCReturnVoid(peerCaller.hrc());
414
415 /* lock both for writing since we modify both (mPeer is "master" so locked
416 * first) */
417 AutoMultiWriteLock2 alock(mPeer, this COMMA_LOCKVAL_SRC_POS);
418
419 if (mData.isBackedUp())
420 {
421 mData.commit();
422 if (mPeer)
423 {
424 /* attach new data to the peer and reshare it */
425 mPeer->mData.attach(mData);
426 }
427 }
428}
429
430/**
431 * @note Locks this object for writing, together with the peer object
432 * represented by @a aThat (locked for reading).
433 */
434void GraphicsAdapter::i_copyFrom(GraphicsAdapter *aThat)
435{
436 AssertReturnVoid(aThat != NULL);
437
438 /* sanity */
439 AutoCaller autoCaller(this);
440 AssertComRCReturnVoid(autoCaller.hrc());
441
442 /* sanity too */
443 AutoCaller thatCaller(aThat);
444 AssertComRCReturnVoid(thatCaller.hrc());
445
446 /* peer is not modified, lock it for reading (aThat is "master" so locked
447 * first) */
448 AutoReadLock rl(aThat COMMA_LOCKVAL_SRC_POS);
449 AutoWriteLock wl(this COMMA_LOCKVAL_SRC_POS);
450
451 /* this will back up current data */
452 mData.assignCopy(aThat->mData);
453}
454/* vi: set tabstop=4 shiftwidth=4 expandtab: */
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