VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/widgets/UIVideoMemoryEditor.cpp@ 82781

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

FE/Qt,Config.kmk: Replace VBOX_WITH_CRHGSMI with a more generic VBOX_WITH_3D_ACCELERATION to indicate that VBox has at least one 3D capable graphics solution enabled to enable the settings in the Qt GUI, bugref:9529

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.1 KB
Line 
1/* $Id: UIVideoMemoryEditor.cpp 80394 2019-08-23 13:02:30Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - UIVideoMemoryEditor class implementation.
4 */
5
6/*
7 * Copyright (C) 2019 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/* Qt includes: */
19#include <QGridLayout>
20#include <QHBoxLayout>
21#include <QLabel>
22#include <QSpinBox>
23#include <QVBoxLayout>
24
25/* GUI includes: */
26#include "QIAdvancedSlider.h"
27#include "UICommon.h"
28#include "UIVideoMemoryEditor.h"
29#include "VBox2DHelpers.h"
30
31/* COM includes: */
32#include "CSystemProperties.h"
33
34
35UIVideoMemoryEditor::UIVideoMemoryEditor(QWidget *pParent /* = 0 */, bool fWithLabel /* = false */)
36 : QIWithRetranslateUI<QWidget>(pParent)
37 , m_fWithLabel(fWithLabel)
38 , m_comGuestOSType(CGuestOSType())
39 , m_cGuestScreenCount(1)
40 , m_enmGraphicsControllerType(KGraphicsControllerType_Null)
41#ifdef VBOX_WITH_3D_ACCELERATION
42 , m_f3DAccelerationSupported(false)
43 , m_f3DAccelerationEnabled(false)
44#endif
45#ifdef VBOX_WITH_VIDEOHWACCEL
46 , m_f2DVideoAccelerationSupported(false)
47 , m_f2DVideoAccelerationEnabled(false)
48#endif
49 , m_iMinVRAM(0)
50 , m_iMaxVRAM(0)
51 , m_iMaxVRAMVisible(0)
52 , m_iInitialVRAM(0)
53 , m_pLabelMemory(0)
54 , m_pSlider(0)
55 , m_pLabelMemoryMin(0)
56 , m_pLabelMemoryMax(0)
57 , m_pSpinBox(0)
58{
59 prepare();
60}
61
62void UIVideoMemoryEditor::setValue(int iValue)
63{
64 if (m_pSlider)
65 {
66 m_iInitialVRAM = RT_MIN(iValue, m_iMaxVRAM);
67 m_pSlider->setValue(m_iInitialVRAM);
68 }
69}
70
71int UIVideoMemoryEditor::value() const
72{
73 return m_pSlider ? m_pSlider->value() : 0;
74}
75
76void UIVideoMemoryEditor::setGuestOSType(const CGuestOSType &comGuestOSType)
77{
78 /* Check if guest OS type really changed: */
79 if (m_comGuestOSType == comGuestOSType)
80 return;
81
82 /* Remember new guest OS type: */
83 m_comGuestOSType = comGuestOSType;
84
85 /* Update requirements: */
86 updateRequirements();
87}
88
89void UIVideoMemoryEditor::setGuestScreenCount(int cGuestScreenCount)
90{
91 /* Check if guest screen count really changed: */
92 if (m_cGuestScreenCount == cGuestScreenCount)
93 return;
94
95 /* Remember new guest screen count: */
96 m_cGuestScreenCount = cGuestScreenCount;
97
98 /* Update requirements: */
99 updateRequirements();
100}
101
102void UIVideoMemoryEditor::setGraphicsControllerType(const KGraphicsControllerType &enmGraphicsControllerType)
103{
104 /* Check if graphics controller type really changed: */
105 if (m_enmGraphicsControllerType == enmGraphicsControllerType)
106 return;
107
108 /* Remember new graphics controller type: */
109 m_enmGraphicsControllerType = enmGraphicsControllerType;
110
111 /* Update requirements: */
112 updateRequirements();
113}
114
115#ifdef VBOX_WITH_3D_ACCELERATION
116void UIVideoMemoryEditor::set3DAccelerationSupported(bool fSupported)
117{
118 /* Check if 3D acceleration really changed: */
119 if (m_f3DAccelerationSupported == fSupported)
120 return;
121
122 /* Remember new 3D acceleration: */
123 m_f3DAccelerationSupported = fSupported;
124
125 /* Update requirements: */
126 updateRequirements();
127}
128
129void UIVideoMemoryEditor::set3DAccelerationEnabled(bool fEnabled)
130{
131 /* Check if 3D acceleration really changed: */
132 if (m_f3DAccelerationEnabled == fEnabled)
133 return;
134
135 /* Remember new 3D acceleration: */
136 m_f3DAccelerationEnabled = fEnabled;
137
138 /* Update requirements: */
139 updateRequirements();
140}
141#endif /* VBOX_WITH_3D_ACCELERATION */
142
143#ifdef VBOX_WITH_VIDEOHWACCEL
144void UIVideoMemoryEditor::set2DVideoAccelerationSupported(bool fSupported)
145{
146 /* Check if 2D video acceleration really changed: */
147 if (m_f2DVideoAccelerationSupported == fSupported)
148 return;
149
150 /* Remember new 2D video acceleration: */
151 m_f2DVideoAccelerationSupported = fSupported;
152
153 /* Update requirements: */
154 updateRequirements();
155}
156
157void UIVideoMemoryEditor::set2DVideoAccelerationEnabled(bool fEnabled)
158{
159 /* Check if 2D video acceleration really changed: */
160 if (m_f2DVideoAccelerationEnabled == fEnabled)
161 return;
162
163 /* Remember new 2D video acceleration: */
164 m_f2DVideoAccelerationEnabled = fEnabled;
165
166 /* Update requirements: */
167 updateRequirements();
168}
169#endif /* VBOX_WITH_VIDEOHWACCEL */
170
171void UIVideoMemoryEditor::retranslateUi()
172{
173 if (m_pLabelMemory)
174 m_pLabelMemory->setText(tr("Video &Memory:"));
175 if (m_pLabelMemoryMin)
176 m_pLabelMemoryMin->setText(tr("%1 MB").arg(m_iMinVRAM));
177 if (m_pLabelMemoryMax)
178 m_pLabelMemoryMax->setText(tr("%1 MB").arg(m_iMaxVRAMVisible));
179 if (m_pSpinBox)
180 m_pSpinBox->setSuffix(QString(" %1").arg(tr("MB")));
181}
182
183void UIVideoMemoryEditor::sltHandleSliderChange()
184{
185 /* Apply spin-box value keeping it's signals disabled: */
186 if (m_pSpinBox && m_pSlider)
187 {
188 m_pSpinBox->blockSignals(true);
189 m_pSpinBox->setValue(m_pSlider->value());
190 m_pSpinBox->blockSignals(false);
191 }
192
193 /* Revalidate to send signal to listener: */
194 revalidate();
195}
196
197void UIVideoMemoryEditor::sltHandleSpinBoxChange()
198{
199 /* Apply slider value keeping it's signals disabled: */
200 if (m_pSpinBox && m_pSlider)
201 {
202 m_pSlider->blockSignals(true);
203 m_pSlider->setValue(m_pSpinBox->value());
204 m_pSlider->blockSignals(false);
205 }
206
207 /* Revalidate to send signal to listener: */
208 revalidate();
209}
210
211void UIVideoMemoryEditor::prepare()
212{
213 /* Prepare common variables: */
214 const CSystemProperties comProperties = uiCommon().virtualBox().GetSystemProperties();
215 m_iMinVRAM = comProperties.GetMinGuestVRAM();
216 m_iMaxVRAM = comProperties.GetMaxGuestVRAM();
217 m_iMaxVRAMVisible = m_iMaxVRAM;
218
219 /* Create main layout: */
220 QGridLayout *pMainLayout = new QGridLayout(this);
221 if (pMainLayout)
222 {
223 pMainLayout->setContentsMargins(0, 0, 0, 0);
224 int iRow = 0;
225
226 /* Create memory label: */
227 if (m_fWithLabel)
228 m_pLabelMemory = new QLabel(this);
229 if (m_pLabelMemory)
230 pMainLayout->addWidget(m_pLabelMemory, 0, iRow++, 1, 1);
231
232 /* Create slider layout: */
233 QVBoxLayout *pSliderLayout = new QVBoxLayout;
234 if (pSliderLayout)
235 {
236 pSliderLayout->setContentsMargins(0, 0, 0, 0);
237
238 /* Create memory slider: */
239 m_pSlider = new QIAdvancedSlider(this);
240 if (m_pSlider)
241 {
242 m_pSlider->setMinimum(m_iMinVRAM);
243 m_pSlider->setMaximum(m_iMaxVRAMVisible);
244 m_pSlider->setPageStep(calculatePageStep(m_iMaxVRAMVisible));
245 m_pSlider->setSingleStep(m_pSlider->pageStep() / 4);
246 m_pSlider->setTickInterval(m_pSlider->pageStep());
247 m_pSlider->setSnappingEnabled(true);
248 m_pSlider->setErrorHint(0, 1);
249 m_pSlider->setMinimumWidth(150);
250 connect(m_pSlider, &QIAdvancedSlider::valueChanged,
251 this, &UIVideoMemoryEditor::sltHandleSliderChange);
252 pSliderLayout->addWidget(m_pSlider);
253 }
254
255 /* Create legend layout: */
256 QHBoxLayout *pLegendLayout = new QHBoxLayout;
257 if (pLegendLayout)
258 {
259 pLegendLayout->setContentsMargins(0, 0, 0, 0);
260
261 /* Create min label: */
262 m_pLabelMemoryMin = new QLabel(this);
263 if (m_pLabelMemoryMin)
264 pLegendLayout->addWidget(m_pLabelMemoryMin);
265
266 /* Push labels from each other: */
267 pLegendLayout->addStretch();
268
269 /* Create max label: */
270 m_pLabelMemoryMax = new QLabel(this);
271 if (m_pLabelMemoryMax)
272 pLegendLayout->addWidget(m_pLabelMemoryMax);
273
274 /* Add legend layout to slider layout: */
275 pSliderLayout->addLayout(pLegendLayout);
276 }
277
278 /* Add slider layout to main layout: */
279 pMainLayout->addLayout(pSliderLayout, 0, iRow++, 2, 1);
280 }
281
282 /* Create memory spin-box: */
283 m_pSpinBox = new QSpinBox(this);
284 if (m_pSpinBox)
285 {
286 setFocusProxy(m_pSpinBox);
287 if (m_pLabelMemory)
288 m_pLabelMemory->setBuddy(m_pSpinBox);
289 m_pSpinBox->setMinimum(m_iMinVRAM);
290 m_pSpinBox->setMaximum(m_iMaxVRAMVisible);
291 connect(m_pSpinBox, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged),
292 this, &UIVideoMemoryEditor::sltHandleSpinBoxChange);
293 pMainLayout->addWidget(m_pSpinBox, 0, iRow++, 1, 1);
294 }
295 }
296
297 /* Apply language settings: */
298 retranslateUi();
299}
300
301void UIVideoMemoryEditor::updateRequirements()
302{
303 /* Make sure guest OS type is set: */
304 if (m_comGuestOSType.isNull())
305 return;
306
307 /* Get monitors count and base video memory requirements: */
308 quint64 uNeedMBytes = UICommon::requiredVideoMemory(m_comGuestOSType.GetId(), m_cGuestScreenCount) / _1M;
309
310 /* Initial value: */
311 m_iMaxVRAMVisible = m_cGuestScreenCount * 32;
312
313 /* No more than m_iMaxVRAM: */
314 if (m_iMaxVRAMVisible > m_iMaxVRAM)
315 m_iMaxVRAMVisible = m_iMaxVRAM;
316
317 /* No less than 128MB (if possible): */
318 if (m_iMaxVRAMVisible < 128 && m_iMaxVRAM >= 128)
319 m_iMaxVRAMVisible = 128;
320
321 /* No less than initial VRAM size (wtf?): */
322 if (m_iMaxVRAMVisible < m_iInitialVRAM)
323 m_iMaxVRAMVisible = m_iInitialVRAM;
324
325#ifdef VBOX_WITH_3D_ACCELERATION
326 if (m_f3DAccelerationEnabled && m_f3DAccelerationSupported)
327 {
328 uNeedMBytes = qMax(uNeedMBytes, (quint64)128);
329 /* No less than 256MB (if possible): */
330 if (m_iMaxVRAMVisible < 256 && m_iMaxVRAM >= 256)
331 m_iMaxVRAMVisible = 256;
332 }
333#endif
334
335#ifdef VBOX_WITH_VIDEOHWACCEL
336 if (m_f2DVideoAccelerationEnabled && m_f2DVideoAccelerationSupported)
337 {
338 uNeedMBytes += VBox2DHelpers::required2DOffscreenVideoMemory() / _1M;
339 }
340#endif
341
342 if (m_pSpinBox)
343 m_pSpinBox->setMaximum(m_iMaxVRAMVisible);
344 if (m_pSlider)
345 {
346 m_pSlider->setMaximum(m_iMaxVRAMVisible);
347 m_pSlider->setPageStep(calculatePageStep(m_iMaxVRAMVisible));
348 m_pSlider->setWarningHint(1, qMin((int)uNeedMBytes, m_iMaxVRAMVisible));
349 m_pSlider->setOptimalHint(qMin((int)uNeedMBytes, m_iMaxVRAMVisible), m_iMaxVRAMVisible);
350 }
351 if (m_pLabelMemoryMax)
352 m_pLabelMemoryMax->setText(tr("%1 MB").arg(m_iMaxVRAMVisible));
353}
354
355void UIVideoMemoryEditor::revalidate()
356{
357 if (m_pSlider)
358 emit sigValidChanged( m_enmGraphicsControllerType == KGraphicsControllerType_Null
359 || m_pSlider->value() > 0);
360}
361
362/* static */
363int UIVideoMemoryEditor::calculatePageStep(int iMax)
364{
365 /* Reasonable max. number of page steps is 32. */
366 const uint uPage = ((uint)iMax + 31) / 32;
367 /* Make it a power of 2: */
368 uint uP = uPage, p2 = 0x1;
369 while ((uP >>= 1))
370 p2 <<= 1;
371 if (uPage != p2)
372 p2 <<= 1;
373 if (p2 < 4)
374 p2 = 4;
375 return (int)p2;
376}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use