1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox frontends: Qt GUI ("VirtualBox"):
|
---|
4 | * VBoxFrameBuffer class and subclasses declarations
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | *
|
---|
18 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
19 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
20 | * additional information or have any questions.
|
---|
21 | */
|
---|
22 |
|
---|
23 | #ifndef ___VBoxFrameBuffer_h___
|
---|
24 | #define ___VBoxFrameBuffer_h___
|
---|
25 |
|
---|
26 | #include "COMDefs.h"
|
---|
27 |
|
---|
28 | /* Qt includes */
|
---|
29 | #include <QImage>
|
---|
30 | #include <QPixmap>
|
---|
31 | #include <QMutex>
|
---|
32 |
|
---|
33 | #if defined (VBOX_GUI_USE_SDL)
|
---|
34 | #include <SDL.h>
|
---|
35 | #include <signal.h>
|
---|
36 | #endif
|
---|
37 |
|
---|
38 | #if defined (Q_WS_WIN) && defined (VBOX_GUI_USE_DDRAW)
|
---|
39 | // VBox/cdefs.h defines these:
|
---|
40 | #undef LOWORD
|
---|
41 | #undef HIWORD
|
---|
42 | #undef LOBYTE
|
---|
43 | #undef HIBYTE
|
---|
44 | #include <ddraw.h>
|
---|
45 | #endif
|
---|
46 |
|
---|
47 | class VBoxConsoleView;
|
---|
48 |
|
---|
49 | /////////////////////////////////////////////////////////////////////////////
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Frame buffer resize event.
|
---|
53 | */
|
---|
54 | class VBoxResizeEvent : public QEvent
|
---|
55 | {
|
---|
56 | public:
|
---|
57 |
|
---|
58 | VBoxResizeEvent (ulong aPixelFormat, uchar *aVRAM,
|
---|
59 | ulong aBitsPerPixel, ulong aBytesPerLine,
|
---|
60 | ulong aWidth, ulong aHeight) :
|
---|
61 | QEvent ((QEvent::Type) VBoxDefs::ResizeEventType),
|
---|
62 | mPixelFormat (aPixelFormat), mVRAM (aVRAM), mBitsPerPixel (aBitsPerPixel),
|
---|
63 | mBytesPerLine (aBytesPerLine), mWidth (aWidth), mHeight (aHeight) {}
|
---|
64 | ulong pixelFormat() { return mPixelFormat; }
|
---|
65 | uchar *VRAM() { return mVRAM; }
|
---|
66 | ulong bitsPerPixel() { return mBitsPerPixel; }
|
---|
67 | ulong bytesPerLine() { return mBytesPerLine; }
|
---|
68 | ulong width() { return mWidth; }
|
---|
69 | ulong height() { return mHeight; }
|
---|
70 |
|
---|
71 | private:
|
---|
72 |
|
---|
73 | ulong mPixelFormat;
|
---|
74 | uchar *mVRAM;
|
---|
75 | ulong mBitsPerPixel;
|
---|
76 | ulong mBytesPerLine;
|
---|
77 | ulong mWidth;
|
---|
78 | ulong mHeight;
|
---|
79 | };
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Frame buffer repaint event.
|
---|
83 | */
|
---|
84 | class VBoxRepaintEvent : public QEvent
|
---|
85 | {
|
---|
86 | public:
|
---|
87 | VBoxRepaintEvent (int x, int y, int w, int h) :
|
---|
88 | QEvent ((QEvent::Type) VBoxDefs::RepaintEventType),
|
---|
89 | ex (x), ey (y), ew (w), eh (h)
|
---|
90 | {}
|
---|
91 | int x() { return ex; }
|
---|
92 | int y() { return ey; }
|
---|
93 | int width() { return ew; }
|
---|
94 | int height() { return eh; }
|
---|
95 | private:
|
---|
96 | int ex, ey, ew, eh;
|
---|
97 | };
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Frame buffer set region event.
|
---|
101 | */
|
---|
102 | class VBoxSetRegionEvent : public QEvent
|
---|
103 | {
|
---|
104 | public:
|
---|
105 | VBoxSetRegionEvent (const QRegion &aReg)
|
---|
106 | : QEvent ((QEvent::Type) VBoxDefs::SetRegionEventType)
|
---|
107 | , mReg (aReg) {}
|
---|
108 | QRegion region() { return mReg; }
|
---|
109 | private:
|
---|
110 | QRegion mReg;
|
---|
111 | };
|
---|
112 |
|
---|
113 | /////////////////////////////////////////////////////////////////////////////
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Common IFramebuffer implementation for all methods used by GUI to maintain
|
---|
117 | * the VM display video memory.
|
---|
118 | *
|
---|
119 | * Note that although this class can be called from multiple threads
|
---|
120 | * (in particular, the GUI thread and EMT) it doesn't protect access to every
|
---|
121 | * data field using its mutex lock. This is because all synchronization between
|
---|
122 | * the GUI and the EMT thread is supposed to be done using the
|
---|
123 | * IFramebuffer::NotifyUpdate() and IFramebuffer::RequestResize() methods
|
---|
124 | * (in particular, the \a aFinished parameter of these methods is responsible
|
---|
125 | * for the synchronization). These methods are always called on EMT and
|
---|
126 | * therefore always follow one another but never in parallel.
|
---|
127 | *
|
---|
128 | * Using this object's mutex lock (exposed also in IFramebuffer::Lock() and
|
---|
129 | * IFramebuffer::Unlock() implementations) usually makes sense only if some
|
---|
130 | * third-party thread (i.e. other than GUI or EMT) needs to make sure that
|
---|
131 | * *no* VM display update or resize event can occur while it is accessing
|
---|
132 | * IFramebuffer properties or the underlying display memory storage area.
|
---|
133 | *
|
---|
134 | * See IFramebuffer documentation for more info.
|
---|
135 | */
|
---|
136 |
|
---|
137 | class VBoxFrameBuffer : public IFramebuffer
|
---|
138 | {
|
---|
139 | public:
|
---|
140 |
|
---|
141 | VBoxFrameBuffer (VBoxConsoleView *aView);
|
---|
142 | virtual ~VBoxFrameBuffer();
|
---|
143 |
|
---|
144 | NS_DECL_ISUPPORTS
|
---|
145 |
|
---|
146 | #if defined (Q_OS_WIN32)
|
---|
147 |
|
---|
148 | STDMETHOD_(ULONG, AddRef)()
|
---|
149 | {
|
---|
150 | return ::InterlockedIncrement (&refcnt);
|
---|
151 | }
|
---|
152 |
|
---|
153 | STDMETHOD_(ULONG, Release)()
|
---|
154 | {
|
---|
155 | long cnt = ::InterlockedDecrement (&refcnt);
|
---|
156 | if (cnt == 0)
|
---|
157 | delete this;
|
---|
158 | return cnt;
|
---|
159 | }
|
---|
160 |
|
---|
161 | STDMETHOD(QueryInterface) (REFIID riid , void **ppObj)
|
---|
162 | {
|
---|
163 | if (riid == IID_IUnknown) {
|
---|
164 | *ppObj = this;
|
---|
165 | AddRef();
|
---|
166 | return S_OK;
|
---|
167 | }
|
---|
168 | if (riid == IID_IFramebuffer) {
|
---|
169 | *ppObj = this;
|
---|
170 | AddRef();
|
---|
171 | return S_OK;
|
---|
172 | }
|
---|
173 | *ppObj = NULL;
|
---|
174 | return E_NOINTERFACE;
|
---|
175 | }
|
---|
176 |
|
---|
177 | #endif
|
---|
178 |
|
---|
179 | // IFramebuffer COM methods
|
---|
180 | STDMETHOD(COMGETTER(Address)) (BYTE **aAddress);
|
---|
181 | STDMETHOD(COMGETTER(Width)) (ULONG *aWidth);
|
---|
182 | STDMETHOD(COMGETTER(Height)) (ULONG *aHeight);
|
---|
183 | STDMETHOD(COMGETTER(BitsPerPixel)) (ULONG *aBitsPerPixel);
|
---|
184 | STDMETHOD(COMGETTER(BytesPerLine)) (ULONG *aBytesPerLine);
|
---|
185 | STDMETHOD(COMGETTER(PixelFormat)) (ULONG *aPixelFormat);
|
---|
186 | STDMETHOD(COMGETTER(UsesGuestVRAM)) (BOOL *aUsesGuestVRAM);
|
---|
187 | STDMETHOD(COMGETTER(HeightReduction)) (ULONG *aHeightReduction);
|
---|
188 | STDMETHOD(COMGETTER(Overlay)) (IFramebufferOverlay **aOverlay);
|
---|
189 | STDMETHOD(COMGETTER(WinId)) (ULONG64 *winId);
|
---|
190 |
|
---|
191 | STDMETHOD(Lock)();
|
---|
192 | STDMETHOD(Unlock)();
|
---|
193 |
|
---|
194 | STDMETHOD(RequestResize) (ULONG aScreenId, ULONG aPixelFormat,
|
---|
195 | BYTE *aVRAM, ULONG aBitsPerPixel, ULONG aBytesPerLine,
|
---|
196 | ULONG aWidth, ULONG aHeight,
|
---|
197 | BOOL *aFinished);
|
---|
198 |
|
---|
199 | STDMETHOD(OperationSupported)(FramebufferAccelerationOperation_T aOperation,
|
---|
200 | BOOL *aSupported);
|
---|
201 | STDMETHOD(VideoModeSupported) (ULONG aWidth, ULONG aHeight, ULONG aBPP,
|
---|
202 | BOOL *aSupported);
|
---|
203 | STDMETHOD(SolidFill) (ULONG aX, ULONG aY, ULONG aWidth, ULONG aHeight,
|
---|
204 | ULONG aColor, BOOL *aHandled);
|
---|
205 | STDMETHOD(CopyScreenBits) (ULONG aXDst, ULONG aYDst, ULONG aXSrc, ULONG aYSrc,
|
---|
206 | ULONG aWidth, ULONG aHeight, BOOL *aHandled);
|
---|
207 |
|
---|
208 | STDMETHOD(GetVisibleRegion)(BYTE *aRectangles, ULONG aCount, ULONG *aCountCopied);
|
---|
209 | STDMETHOD(SetVisibleRegion)(BYTE *aRectangles, ULONG aCount);
|
---|
210 |
|
---|
211 | ulong width() { return mWdt; }
|
---|
212 | ulong height() { return mHgt; }
|
---|
213 |
|
---|
214 | virtual ulong pixelFormat()
|
---|
215 | {
|
---|
216 | return FramebufferPixelFormat_FOURCC_RGB;
|
---|
217 | }
|
---|
218 |
|
---|
219 | virtual bool usesGuestVRAM()
|
---|
220 | {
|
---|
221 | return false;
|
---|
222 | }
|
---|
223 |
|
---|
224 | void lock() { mMutex->lock(); }
|
---|
225 | void unlock() { mMutex->unlock(); }
|
---|
226 |
|
---|
227 | virtual uchar *address() = 0;
|
---|
228 | virtual ulong bitsPerPixel() = 0;
|
---|
229 | virtual ulong bytesPerLine() = 0;
|
---|
230 |
|
---|
231 | /**
|
---|
232 | * Called on the GUI thread (from VBoxConsoleView) when some part of the
|
---|
233 | * VM display viewport needs to be repainted on the host screen.
|
---|
234 | */
|
---|
235 | virtual void paintEvent (QPaintEvent *pe) = 0;
|
---|
236 |
|
---|
237 | /**
|
---|
238 | * Called on the GUI thread (from VBoxConsoleView) after it gets a
|
---|
239 | * VBoxResizeEvent posted from the RequestResize() method implementation.
|
---|
240 | */
|
---|
241 | virtual void resizeEvent (VBoxResizeEvent *re)
|
---|
242 | {
|
---|
243 | mWdt = re->width();
|
---|
244 | mHgt = re->height();
|
---|
245 | }
|
---|
246 |
|
---|
247 | /**
|
---|
248 | * Called on the GUI thread (from VBoxConsoleView) when the VM console
|
---|
249 | * window is moved.
|
---|
250 | */
|
---|
251 | virtual void moveEvent (QMoveEvent * /*me*/ ) {}
|
---|
252 |
|
---|
253 | protected:
|
---|
254 |
|
---|
255 | VBoxConsoleView *mView;
|
---|
256 | QMutex *mMutex;
|
---|
257 | int mWdt;
|
---|
258 | int mHgt;
|
---|
259 |
|
---|
260 | #if defined (Q_OS_WIN32)
|
---|
261 | private:
|
---|
262 | long refcnt;
|
---|
263 | #endif
|
---|
264 | };
|
---|
265 |
|
---|
266 | /////////////////////////////////////////////////////////////////////////////
|
---|
267 |
|
---|
268 | #if defined (VBOX_GUI_USE_QIMAGE)
|
---|
269 |
|
---|
270 | class VBoxQImageFrameBuffer : public VBoxFrameBuffer
|
---|
271 | {
|
---|
272 | public:
|
---|
273 |
|
---|
274 | VBoxQImageFrameBuffer (VBoxConsoleView *aView);
|
---|
275 |
|
---|
276 | STDMETHOD(NotifyUpdate) (ULONG aX, ULONG aY,
|
---|
277 | ULONG aW, ULONG aH,
|
---|
278 | BOOL *aFinished);
|
---|
279 |
|
---|
280 | ulong pixelFormat() { return mPixelFormat; }
|
---|
281 | bool usesGuestVRAM() { return mUsesGuestVRAM; }
|
---|
282 |
|
---|
283 | uchar *address() { return mImg.bits(); }
|
---|
284 | ulong bitsPerPixel() { return mImg.depth(); }
|
---|
285 | ulong bytesPerLine() { return mImg.bytesPerLine(); }
|
---|
286 |
|
---|
287 | void paintEvent (QPaintEvent *pe);
|
---|
288 | void resizeEvent (VBoxResizeEvent *re);
|
---|
289 |
|
---|
290 | private:
|
---|
291 |
|
---|
292 | QPixmap mPM;
|
---|
293 | QImage mImg;
|
---|
294 | ulong mPixelFormat;
|
---|
295 | bool mUsesGuestVRAM;
|
---|
296 | };
|
---|
297 |
|
---|
298 | #endif
|
---|
299 |
|
---|
300 | /////////////////////////////////////////////////////////////////////////////
|
---|
301 |
|
---|
302 | #if defined (VBOX_GUI_USE_SDL)
|
---|
303 |
|
---|
304 | class VBoxSDLFrameBuffer : public VBoxFrameBuffer
|
---|
305 | {
|
---|
306 | public:
|
---|
307 |
|
---|
308 | VBoxSDLFrameBuffer (VBoxConsoleView *aView);
|
---|
309 | virtual ~VBoxSDLFrameBuffer();
|
---|
310 |
|
---|
311 | STDMETHOD(NotifyUpdate) (ULONG aX, ULONG aY,
|
---|
312 | ULONG aW, ULONG aH,
|
---|
313 | BOOL *aFinished);
|
---|
314 |
|
---|
315 | uchar *address()
|
---|
316 | {
|
---|
317 | SDL_Surface *surf = mSurfVRAM ? mSurfVRAM : mScreen;
|
---|
318 | return surf ? (uchar *) (uintptr_t) surf->pixels : 0;
|
---|
319 | }
|
---|
320 |
|
---|
321 | ulong bitsPerPixel()
|
---|
322 | {
|
---|
323 | SDL_Surface *surf = mSurfVRAM ? mSurfVRAM : mScreen;
|
---|
324 | return surf ? surf->format->BitsPerPixel : 0;
|
---|
325 | }
|
---|
326 |
|
---|
327 | ulong bytesPerLine()
|
---|
328 | {
|
---|
329 | SDL_Surface *surf = mSurfVRAM ? mSurfVRAM : mScreen;
|
---|
330 | return surf ? surf->pitch : 0;
|
---|
331 | }
|
---|
332 |
|
---|
333 | ulong pixelFormat()
|
---|
334 | {
|
---|
335 | return mPixelFormat;
|
---|
336 | }
|
---|
337 |
|
---|
338 | bool usesGuestVRAM()
|
---|
339 | {
|
---|
340 | return mSurfVRAM != NULL;
|
---|
341 | }
|
---|
342 |
|
---|
343 | void paintEvent (QPaintEvent *pe);
|
---|
344 | void resizeEvent (VBoxResizeEvent *re);
|
---|
345 |
|
---|
346 | private:
|
---|
347 |
|
---|
348 | SDL_Surface *mScreen;
|
---|
349 | SDL_Surface *mSurfVRAM;
|
---|
350 |
|
---|
351 | ulong mPixelFormat;
|
---|
352 | };
|
---|
353 |
|
---|
354 | #endif
|
---|
355 |
|
---|
356 | /////////////////////////////////////////////////////////////////////////////
|
---|
357 |
|
---|
358 | #if defined (VBOX_GUI_USE_DDRAW)
|
---|
359 |
|
---|
360 | class VBoxDDRAWFrameBuffer : public VBoxFrameBuffer
|
---|
361 | {
|
---|
362 | public:
|
---|
363 |
|
---|
364 | VBoxDDRAWFrameBuffer (VBoxConsoleView *aView);
|
---|
365 | virtual ~VBoxDDRAWFrameBuffer();
|
---|
366 |
|
---|
367 | STDMETHOD(NotifyUpdate) (ULONG aX, ULONG aY,
|
---|
368 | ULONG aW, ULONG aH,
|
---|
369 | BOOL *aFinished);
|
---|
370 |
|
---|
371 | uchar *address() { return (uchar *) mSurfaceDesc.lpSurface; }
|
---|
372 | ulong bitsPerPixel() { return mSurfaceDesc.ddpfPixelFormat.dwRGBBitCount; }
|
---|
373 | ulong bytesPerLine() { return (ulong) mSurfaceDesc.lPitch; }
|
---|
374 |
|
---|
375 | ulong pixelFormat() { return mPixelFormat; };
|
---|
376 |
|
---|
377 | bool usesGuestVRAM() { return mUsesGuestVRAM; }
|
---|
378 |
|
---|
379 | void paintEvent (QPaintEvent *pe);
|
---|
380 | void resizeEvent (VBoxResizeEvent *re);
|
---|
381 | void moveEvent (QMoveEvent *me);
|
---|
382 |
|
---|
383 | private:
|
---|
384 |
|
---|
385 | void releaseObjects();
|
---|
386 |
|
---|
387 | bool createSurface (ULONG aPixelFormat, uchar *pvVRAM,
|
---|
388 | ULONG aBitsPerPixel, ULONG aBytesPerLine,
|
---|
389 | ULONG aWidth, ULONG aHeight);
|
---|
390 | void deleteSurface();
|
---|
391 | void drawRect (ULONG x, ULONG y, ULONG w, ULONG h);
|
---|
392 | void getWindowPosition (void);
|
---|
393 |
|
---|
394 | LPDIRECTDRAW7 mDDRAW;
|
---|
395 | LPDIRECTDRAWCLIPPER mClipper;
|
---|
396 | LPDIRECTDRAWSURFACE7 mSurface;
|
---|
397 | DDSURFACEDESC2 mSurfaceDesc;
|
---|
398 | LPDIRECTDRAWSURFACE7 mPrimarySurface;
|
---|
399 |
|
---|
400 | ulong mPixelFormat;
|
---|
401 |
|
---|
402 | bool mUsesGuestVRAM;
|
---|
403 |
|
---|
404 | int mWndX;
|
---|
405 | int mWndY;
|
---|
406 |
|
---|
407 | bool mSynchronousUpdates;
|
---|
408 | };
|
---|
409 |
|
---|
410 | #endif
|
---|
411 |
|
---|
412 | /////////////////////////////////////////////////////////////////////////////
|
---|
413 |
|
---|
414 | #if defined (Q_WS_MAC) && defined (VBOX_GUI_USE_QUARTZ2D)
|
---|
415 |
|
---|
416 | #include <Carbon/Carbon.h>
|
---|
417 |
|
---|
418 | class VBoxQuartz2DFrameBuffer : public VBoxFrameBuffer
|
---|
419 | {
|
---|
420 | public:
|
---|
421 |
|
---|
422 | VBoxQuartz2DFrameBuffer (VBoxConsoleView *aView);
|
---|
423 | virtual ~VBoxQuartz2DFrameBuffer ();
|
---|
424 |
|
---|
425 | STDMETHOD (NotifyUpdate) (ULONG aX, ULONG aY,
|
---|
426 | ULONG aW, ULONG aH,
|
---|
427 | BOOL *aFinished);
|
---|
428 | STDMETHOD (SetVisibleRegion) (BYTE *aRectangles, ULONG aCount);
|
---|
429 |
|
---|
430 | uchar *address() { return mDataAddress; }
|
---|
431 | ulong bitsPerPixel() { return CGImageGetBitsPerPixel (mImage); }
|
---|
432 | ulong bytesPerLine() { return CGImageGetBytesPerRow (mImage); }
|
---|
433 | ulong pixelFormat() { return mPixelFormat; };
|
---|
434 | bool usesGuestVRAM() { return mBitmapData == NULL; }
|
---|
435 |
|
---|
436 | const CGImageRef imageRef() const { return mImage; }
|
---|
437 |
|
---|
438 | void paintEvent (QPaintEvent *pe);
|
---|
439 | void resizeEvent (VBoxResizeEvent *re);
|
---|
440 |
|
---|
441 | private:
|
---|
442 |
|
---|
443 | void clean();
|
---|
444 |
|
---|
445 | uchar *mDataAddress;
|
---|
446 | void *mBitmapData;
|
---|
447 | ulong mPixelFormat;
|
---|
448 | CGImageRef mImage;
|
---|
449 | typedef struct
|
---|
450 | {
|
---|
451 | /** The size of this structure expressed in rcts entries. */
|
---|
452 | ULONG allocated;
|
---|
453 | /** The number of entries in the rcts array. */
|
---|
454 | ULONG used;
|
---|
455 | /** Variable sized array of the rectangle that makes up the region. */
|
---|
456 | CGRect rcts[1];
|
---|
457 | } RegionRects;
|
---|
458 | /** The current valid region, all access is by atomic cmpxchg or atomic xchg.
|
---|
459 | *
|
---|
460 | * The protocol for updating and using this has to take into account that
|
---|
461 | * the producer (SetVisibleRegion) and consumer (paintEvent) are running
|
---|
462 | * on different threads. Therefore the producer will create a new RegionRects
|
---|
463 | * structure before atomically replace the existing one. While the consumer
|
---|
464 | * will read the value by atomically replace it by NULL, and then when its
|
---|
465 | * done try restore it by cmpxchg. If the producer has already put a new
|
---|
466 | * region there, it will be discarded (see mRegionUnused).
|
---|
467 | */
|
---|
468 | RegionRects volatile *mRegion;
|
---|
469 | /** For keeping the unused region and thus avoid some RTMemAlloc/RTMemFree calls.
|
---|
470 | * This is operated with atomic cmpxchg and atomic xchg. */
|
---|
471 | RegionRects volatile *mRegionUnused;
|
---|
472 | };
|
---|
473 |
|
---|
474 | #endif /* Q_WS_MAC && VBOX_GUI_USE_QUARTZ2D */
|
---|
475 |
|
---|
476 | #endif // !___VBoxFrameBuffer_h___
|
---|