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