1 | /* $Id: UIFrameBufferQImage.cpp 35638 2011-01-19 19:10:49Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * VBox frontends: Qt GUI ("VirtualBox"):
|
---|
5 | * UIFrameBufferQImage class implementation
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2010-2011 Oracle Corporation
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #ifdef VBOX_GUI_USE_QIMAGE
|
---|
21 |
|
---|
22 | #ifdef VBOX_WITH_PRECOMPILED_HEADERS
|
---|
23 | # include "precomp.h"
|
---|
24 | #else /* !VBOX_WITH_PRECOMPILED_HEADERS */
|
---|
25 |
|
---|
26 | /* Local includes */
|
---|
27 | # include "UIFrameBufferQImage.h"
|
---|
28 | # include "UIMachineView.h"
|
---|
29 | # include "VBoxProblemReporter.h"
|
---|
30 | # include "VBoxGlobal.h"
|
---|
31 |
|
---|
32 | /* Global includes */
|
---|
33 | # include <QPainter>
|
---|
34 | # include <QApplication>
|
---|
35 |
|
---|
36 | #endif /* !VBOX_WITH_PRECOMPILED_HEADERS */
|
---|
37 |
|
---|
38 | /** @class UIFrameBufferQImage
|
---|
39 | *
|
---|
40 | * The UIFrameBufferQImage class is a class that implements the IFrameBuffer
|
---|
41 | * interface and uses QImage as the direct storage for VM display data. QImage
|
---|
42 | * is then converted to QPixmap and blitted to the console view widget.
|
---|
43 | */
|
---|
44 | UIFrameBufferQImage::UIFrameBufferQImage(UIMachineView *pMachineView)
|
---|
45 | : UIFrameBuffer(pMachineView)
|
---|
46 | {
|
---|
47 | /* Initialize the framebuffer the first time */
|
---|
48 | UIResizeEvent event(FramebufferPixelFormat_Opaque, NULL, 0, 0, 640, 480);
|
---|
49 | resizeEvent(&event);
|
---|
50 | }
|
---|
51 |
|
---|
52 | /** @note This method is called on EMT from under this object's lock */
|
---|
53 | STDMETHODIMP UIFrameBufferQImage::NotifyUpdate(ULONG uX, ULONG uY, ULONG uW, ULONG uH)
|
---|
54 | {
|
---|
55 | /* We're not on the GUI thread and update() isn't thread safe in
|
---|
56 | * Qt 4.3.x on the Win, Qt 3.3.x on the Mac (4.2.x is),
|
---|
57 | * on Linux (didn't check Qt 4.x there) and probably on other
|
---|
58 | * non-DOS platforms, so post the event instead. */
|
---|
59 | QApplication::postEvent(m_pMachineView, new UIRepaintEvent(uX, uY, uW, uH));
|
---|
60 |
|
---|
61 | return S_OK;
|
---|
62 | }
|
---|
63 |
|
---|
64 | void UIFrameBufferQImage::paintEvent(QPaintEvent *pEvent)
|
---|
65 | {
|
---|
66 | /* Scaled image by default is empty: */
|
---|
67 | QImage scaledImage;
|
---|
68 |
|
---|
69 |
|
---|
70 | /* If scaled-factor is set and current image is NOT null: */
|
---|
71 | if (m_scaledSize.isValid() && !m_img.isNull())
|
---|
72 | {
|
---|
73 | /* We are doing a deep copy of image to make sure it will not be
|
---|
74 | * detached during scale process, otherwise we can get a frozen frame-buffer. */
|
---|
75 | scaledImage = m_img.copy();
|
---|
76 | /* Scale image to scaled-factor: */
|
---|
77 | scaledImage = scaledImage.scaled(m_scaledSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
|
---|
78 | }
|
---|
79 |
|
---|
80 | /* Choose required image: */
|
---|
81 | QImage *pSourceImage = scaledImage.isNull() ? &m_img : &scaledImage;
|
---|
82 |
|
---|
83 | /* Apply image-size restriction: */
|
---|
84 | const QRect &r = pEvent->rect().intersected(pSourceImage->rect());
|
---|
85 |
|
---|
86 | /* Some outdated rectangle during processing UIResizeEvent */
|
---|
87 | if (r.isEmpty())
|
---|
88 | return;
|
---|
89 |
|
---|
90 | #if 0
|
---|
91 | LogFlowFunc (("%dx%d-%dx%d (img=%dx%d)\n", r.x(), r.y(), r.width(), r.height(), img.width(), img.height()));
|
---|
92 | #endif
|
---|
93 |
|
---|
94 | QPainter painter(m_pMachineView->viewport());
|
---|
95 |
|
---|
96 |
|
---|
97 | if ((ulong)r.width() < m_width * 2 / 3)
|
---|
98 | {
|
---|
99 | /* This method is faster for narrow updates */
|
---|
100 | m_PM = QPixmap::fromImage(pSourceImage->copy(r.x() + m_pMachineView->contentsX(),
|
---|
101 | r.y() + m_pMachineView->contentsY(),
|
---|
102 | r.width(), r.height()));
|
---|
103 | painter.drawPixmap(r.x(), r.y(), m_PM);
|
---|
104 | }
|
---|
105 | else
|
---|
106 | {
|
---|
107 | /* This method is faster for wide updates */
|
---|
108 | m_PM = QPixmap::fromImage(QImage(pSourceImage->scanLine(r.y() + m_pMachineView->contentsY()),
|
---|
109 | pSourceImage->width(), r.height(), pSourceImage->bytesPerLine(),
|
---|
110 | QImage::Format_RGB32));
|
---|
111 | painter.drawPixmap(r.x(), r.y(), m_PM, r.x() + m_pMachineView->contentsX(), 0, 0, 0);
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | void UIFrameBufferQImage::resizeEvent(UIResizeEvent *pEvent)
|
---|
116 | {
|
---|
117 | #if 0
|
---|
118 | LogFlowFunc (("fmt=%d, vram=%p, bpp=%d, bpl=%d, width=%d, height=%d\n",
|
---|
119 | pEvent->pixelFormat(), pEvent->VRAM(),
|
---|
120 | pEvent->bitsPerPixel(), pEvent->bytesPerLine(),
|
---|
121 | pEvent->width(), pEvent->height()));
|
---|
122 | #endif
|
---|
123 |
|
---|
124 | m_width = pEvent->width();
|
---|
125 | m_height = pEvent->height();
|
---|
126 |
|
---|
127 | bool bRemind = false;
|
---|
128 | bool bFallback = false;
|
---|
129 | ulong bitsPerLine = pEvent->bytesPerLine() * 8;
|
---|
130 |
|
---|
131 | /* check if we support the pixel format and can use the guest VRAM directly */
|
---|
132 | if (pEvent->pixelFormat() == FramebufferPixelFormat_FOURCC_RGB)
|
---|
133 | {
|
---|
134 | QImage::Format format;
|
---|
135 | switch (pEvent->bitsPerPixel())
|
---|
136 | {
|
---|
137 | /* 32-, 8- and 1-bpp are the only depths supported by QImage */
|
---|
138 | case 32:
|
---|
139 | format = QImage::Format_RGB32;
|
---|
140 | break;
|
---|
141 | case 8:
|
---|
142 | format = QImage::Format_Indexed8;
|
---|
143 | bRemind = true;
|
---|
144 | break;
|
---|
145 | case 1:
|
---|
146 | format = QImage::Format_Mono;
|
---|
147 | bRemind = true;
|
---|
148 | break;
|
---|
149 | default:
|
---|
150 | format = QImage::Format_Invalid; /* set it to something so gcc keeps quiet. */
|
---|
151 | bRemind = true;
|
---|
152 | bFallback = true;
|
---|
153 | break;
|
---|
154 | }
|
---|
155 |
|
---|
156 | if (!bFallback)
|
---|
157 | {
|
---|
158 | /* QImage only supports 32-bit aligned scan lines... */
|
---|
159 | Assert ((pEvent->bytesPerLine() & 3) == 0);
|
---|
160 | bFallback = ((pEvent->bytesPerLine() & 3) != 0);
|
---|
161 | }
|
---|
162 | if (!bFallback)
|
---|
163 | {
|
---|
164 | /* ...and the scan lines ought to be a whole number of pixels. */
|
---|
165 | Assert ((bitsPerLine & (pEvent->bitsPerPixel() - 1)) == 0);
|
---|
166 | bFallback = ((bitsPerLine & (pEvent->bitsPerPixel() - 1)) != 0);
|
---|
167 | }
|
---|
168 | if (!bFallback)
|
---|
169 | {
|
---|
170 | Assert (bitsPerLine / pEvent->bitsPerPixel() >= m_width);
|
---|
171 | bFallback = RT_BOOL (bitsPerLine / pEvent->bitsPerPixel() < m_width);
|
---|
172 | }
|
---|
173 | if (!bFallback)
|
---|
174 | {
|
---|
175 | m_img = QImage ((uchar *) pEvent->VRAM(), m_width, m_height, bitsPerLine / 8, format);
|
---|
176 | m_uPixelFormat = FramebufferPixelFormat_FOURCC_RGB;
|
---|
177 | m_bUsesGuestVRAM = true;
|
---|
178 | }
|
---|
179 | }
|
---|
180 | else
|
---|
181 | {
|
---|
182 | bFallback = true;
|
---|
183 | }
|
---|
184 |
|
---|
185 | if (bFallback)
|
---|
186 | {
|
---|
187 | /* we don't support either the pixel format or the color depth,
|
---|
188 | * bFallback to a self-provided 32bpp RGB buffer */
|
---|
189 | m_img = QImage (m_width, m_height, QImage::Format_RGB32);
|
---|
190 | m_img.fill(0);
|
---|
191 | m_uPixelFormat = FramebufferPixelFormat_FOURCC_RGB;
|
---|
192 | m_bUsesGuestVRAM = false;
|
---|
193 | }
|
---|
194 |
|
---|
195 | if (bRemind)
|
---|
196 | vboxProblem().remindAboutWrongColorDepth(pEvent->bitsPerPixel(), 32);
|
---|
197 | }
|
---|
198 |
|
---|
199 | #endif /* VBOX_GUI_USE_QIMAGE */
|
---|
200 |
|
---|