VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/include/VBoxFrameBuffer.h@ 22800

Last change on this file since 22800 was 22800, checked in by vboxsync, 16 years ago

video 2d accel: using ogl only when needed: saved state fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 47.8 KB
Line 
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//#define VBOXQGL_PROF_BASE 1
26//#define VBOXQGL_DBG_SURF 1
27#include "COMDefs.h"
28#include <iprt/critsect.h>
29
30/* Qt includes */
31#include <QImage>
32#include <QPixmap>
33#include <QMutex>
34#include <QPaintEvent>
35#include <QMoveEvent>
36#if defined (VBOX_GUI_USE_QGL)
37#include <QGLWidget>
38#endif
39
40#if defined (VBOX_GUI_USE_SDL)
41#include <SDL.h>
42#include <signal.h>
43#endif
44
45#if defined (Q_WS_WIN) && defined (VBOX_GUI_USE_DDRAW)
46// VBox/cdefs.h defines these:
47#undef LOWORD
48#undef HIWORD
49#undef LOBYTE
50#undef HIBYTE
51#include <ddraw.h>
52#endif
53
54class VBoxConsoleView;
55
56/////////////////////////////////////////////////////////////////////////////
57
58/**
59 * Frame buffer resize event.
60 */
61class VBoxResizeEvent : public QEvent
62{
63public:
64
65 VBoxResizeEvent (ulong aPixelFormat, uchar *aVRAM,
66 ulong aBitsPerPixel, ulong aBytesPerLine,
67 ulong aWidth, ulong aHeight) :
68 QEvent ((QEvent::Type) VBoxDefs::ResizeEventType),
69 mPixelFormat (aPixelFormat), mVRAM (aVRAM), mBitsPerPixel (aBitsPerPixel),
70 mBytesPerLine (aBytesPerLine), mWidth (aWidth), mHeight (aHeight) {}
71 ulong pixelFormat() { return mPixelFormat; }
72 uchar *VRAM() { return mVRAM; }
73 ulong bitsPerPixel() { return mBitsPerPixel; }
74 ulong bytesPerLine() { return mBytesPerLine; }
75 ulong width() { return mWidth; }
76 ulong height() { return mHeight; }
77
78private:
79
80 ulong mPixelFormat;
81 uchar *mVRAM;
82 ulong mBitsPerPixel;
83 ulong mBytesPerLine;
84 ulong mWidth;
85 ulong mHeight;
86};
87
88/**
89 * Frame buffer repaint event.
90 */
91class VBoxRepaintEvent : public QEvent
92{
93public:
94 VBoxRepaintEvent (int x, int y, int w, int h) :
95 QEvent ((QEvent::Type) VBoxDefs::RepaintEventType),
96 ex (x), ey (y), ew (w), eh (h)
97 {}
98 int x() { return ex; }
99 int y() { return ey; }
100 int width() { return ew; }
101 int height() { return eh; }
102private:
103 int ex, ey, ew, eh;
104};
105
106/**
107 * Frame buffer set region event.
108 */
109class VBoxSetRegionEvent : public QEvent
110{
111public:
112 VBoxSetRegionEvent (const QRegion &aReg)
113 : QEvent ((QEvent::Type) VBoxDefs::SetRegionEventType)
114 , mReg (aReg) {}
115 QRegion region() { return mReg; }
116private:
117 QRegion mReg;
118};
119
120/////////////////////////////////////////////////////////////////////////////
121
122/**
123 * Common IFramebuffer implementation for all methods used by GUI to maintain
124 * the VM display video memory.
125 *
126 * Note that although this class can be called from multiple threads
127 * (in particular, the GUI thread and EMT) it doesn't protect access to every
128 * data field using its mutex lock. This is because all synchronization between
129 * the GUI and the EMT thread is supposed to be done using the
130 * IFramebuffer::NotifyUpdate() and IFramebuffer::RequestResize() methods
131 * (in particular, the \a aFinished parameter of these methods is responsible
132 * for the synchronization). These methods are always called on EMT and
133 * therefore always follow one another but never in parallel.
134 *
135 * Using this object's mutex lock (exposed also in IFramebuffer::Lock() and
136 * IFramebuffer::Unlock() implementations) usually makes sense only if some
137 * third-party thread (i.e. other than GUI or EMT) needs to make sure that
138 * *no* VM display update or resize event can occur while it is accessing
139 * IFramebuffer properties or the underlying display memory storage area.
140 *
141 * See IFramebuffer documentation for more info.
142 */
143
144class VBoxFrameBuffer : VBOX_SCRIPTABLE_IMPL(IFramebuffer)
145{
146public:
147
148 VBoxFrameBuffer (VBoxConsoleView *aView);
149 virtual ~VBoxFrameBuffer();
150
151 NS_DECL_ISUPPORTS
152
153#if defined (Q_OS_WIN32)
154
155 STDMETHOD_(ULONG, AddRef)()
156 {
157 return ::InterlockedIncrement (&refcnt);
158 }
159
160 STDMETHOD_(ULONG, Release)()
161 {
162 long cnt = ::InterlockedDecrement (&refcnt);
163 if (cnt == 0)
164 delete this;
165 return cnt;
166 }
167#endif
168 VBOX_SCRIPTABLE_DISPATCH_IMPL(IFramebuffer)
169
170 // IFramebuffer COM methods
171 STDMETHOD(COMGETTER(Address)) (BYTE **aAddress);
172 STDMETHOD(COMGETTER(Width)) (ULONG *aWidth);
173 STDMETHOD(COMGETTER(Height)) (ULONG *aHeight);
174 STDMETHOD(COMGETTER(BitsPerPixel)) (ULONG *aBitsPerPixel);
175 STDMETHOD(COMGETTER(BytesPerLine)) (ULONG *aBytesPerLine);
176 STDMETHOD(COMGETTER(PixelFormat)) (ULONG *aPixelFormat);
177 STDMETHOD(COMGETTER(UsesGuestVRAM)) (BOOL *aUsesGuestVRAM);
178 STDMETHOD(COMGETTER(HeightReduction)) (ULONG *aHeightReduction);
179 STDMETHOD(COMGETTER(Overlay)) (IFramebufferOverlay **aOverlay);
180 STDMETHOD(COMGETTER(WinId)) (ULONG64 *winId);
181
182 STDMETHOD(Lock)();
183 STDMETHOD(Unlock)();
184
185 STDMETHOD(RequestResize) (ULONG aScreenId, ULONG aPixelFormat,
186 BYTE *aVRAM, ULONG aBitsPerPixel, ULONG aBytesPerLine,
187 ULONG aWidth, ULONG aHeight,
188 BOOL *aFinished);
189
190 STDMETHOD(VideoModeSupported) (ULONG aWidth, ULONG aHeight, ULONG aBPP,
191 BOOL *aSupported);
192
193 STDMETHOD(GetVisibleRegion)(BYTE *aRectangles, ULONG aCount, ULONG *aCountCopied);
194 STDMETHOD(SetVisibleRegion)(BYTE *aRectangles, ULONG aCount);
195
196 STDMETHOD(ProcessVHWACommand)(BYTE *pCommand);
197
198 ulong width() { return mWdt; }
199 ulong height() { return mHgt; }
200
201 virtual ulong pixelFormat()
202 {
203 return FramebufferPixelFormat_FOURCC_RGB;
204 }
205
206 virtual bool usesGuestVRAM()
207 {
208 return false;
209 }
210
211 void lock() { RTCritSectEnter(&mCritSect); }
212 void unlock() { RTCritSectLeave(&mCritSect); }
213
214 virtual uchar *address() = 0;
215 virtual ulong bitsPerPixel() = 0;
216 virtual ulong bytesPerLine() = 0;
217
218 /**
219 * Called on the GUI thread (from VBoxConsoleView) when some part of the
220 * VM display viewport needs to be repainted on the host screen.
221 */
222 virtual void paintEvent (QPaintEvent *pe) = 0;
223
224 /**
225 * Called on the GUI thread (from VBoxConsoleView) after it gets a
226 * VBoxResizeEvent posted from the RequestResize() method implementation.
227 */
228 virtual void resizeEvent (VBoxResizeEvent *re)
229 {
230 mWdt = re->width();
231 mHgt = re->height();
232 }
233
234 /**
235 * Called on the GUI thread (from VBoxConsoleView) when the VM console
236 * window is moved.
237 */
238 virtual void moveEvent (QMoveEvent * /*me*/ ) {}
239
240#ifdef VBOX_WITH_VIDEOHWACCEL
241 /* this method is called from the GUI thread
242 * to perform the actual Video HW Acceleration command processing
243 * the event is framebuffer implementation specific */
244 virtual void doProcessVHWACommand(QEvent * pEvent);
245#endif
246
247protected:
248
249 VBoxConsoleView *mView;
250 RTCRITSECT mCritSect;
251 int mWdt;
252 int mHgt;
253 uint64_t mWinId;
254
255#if defined (Q_OS_WIN32)
256private:
257 long refcnt;
258#endif
259};
260
261/////////////////////////////////////////////////////////////////////////////
262
263#if defined (VBOX_GUI_USE_QIMAGE)
264
265class VBoxQImageFrameBuffer : public VBoxFrameBuffer
266{
267public:
268
269 VBoxQImageFrameBuffer (VBoxConsoleView *aView);
270
271 STDMETHOD(NotifyUpdate) (ULONG aX, ULONG aY,
272 ULONG aW, ULONG aH);
273
274 ulong pixelFormat() { return mPixelFormat; }
275 bool usesGuestVRAM() { return mUsesGuestVRAM; }
276
277 uchar *address() { return mImg.bits(); }
278 ulong bitsPerPixel() { return mImg.depth(); }
279 ulong bytesPerLine() { return mImg.bytesPerLine(); }
280
281 void paintEvent (QPaintEvent *pe);
282 void resizeEvent (VBoxResizeEvent *re);
283
284private:
285
286 QPixmap mPM;
287 QImage mImg;
288 ulong mPixelFormat;
289 bool mUsesGuestVRAM;
290};
291
292#endif
293
294/////////////////////////////////////////////////////////////////////////////
295
296#if defined (VBOX_GUI_USE_QGL)
297
298#ifdef DEBUG
299#include "iprt/stream.h"
300#define VBOXQGLLOG(_m) RTPrintf _m
301#define VBOXQGLLOGREL(_m) do { RTPrintf _m ; LogRel( _m ); } while(0)
302#else
303#define VBOXQGLLOG(_m)
304#define VBOXQGLLOGREL(_m) LogRel( _m )
305#endif
306#define VBOXQGLLOG_ENTER(_m)
307//do{VBOXQGLLOG(("==>[%s]:", __FUNCTION__)); VBOXQGLLOG(_m);}while(0)
308#define VBOXQGLLOG_EXIT(_m)
309//do{VBOXQGLLOG(("<==[%s]:", __FUNCTION__)); VBOXQGLLOG(_m);}while(0)
310#ifdef DEBUG
311#define VBOXQGL_ASSERTNOERR() \
312 do { GLenum err = glGetError(); \
313 if(err != GL_NO_ERROR) VBOXQGLLOG(("gl error ocured (0x%x)\n", err)); \
314 Assert(err == GL_NO_ERROR); \
315 }while(0)
316
317#define VBOXQGL_CHECKERR(_op) \
318 do { \
319 glGetError(); \
320 _op \
321 VBOXQGL_ASSERTNOERR(); \
322 }while(0)
323#else
324#define VBOXQGL_ASSERTNOERR() \
325 do {}while(0)
326
327#define VBOXQGL_CHECKERR(_op) \
328 do { \
329 _op \
330 }while(0)
331#endif
332
333#ifdef DEBUG
334#include <iprt/time.h>
335
336#define VBOXGETTIME() RTTimeNanoTS()
337
338#define VBOXPRINTDIF(_nano, _m) do{\
339 uint64_t cur = VBOXGETTIME(); \
340 VBOXQGLLOG(_m); \
341 VBOXQGLLOG(("(%Lu)\n", cur - (_nano))); \
342 }while(0)
343
344class VBoxVHWADbgTimeCounter
345{
346public:
347 VBoxVHWADbgTimeCounter(const char* msg) {mTime = VBOXGETTIME(); mMsg=msg;}
348 ~VBoxVHWADbgTimeCounter() {VBOXPRINTDIF(mTime, (mMsg));}
349private:
350 uint64_t mTime;
351 const char* mMsg;
352};
353
354#define VBOXQGLLOG_METHODTIME(_m) VBoxVHWADbgTimeCounter _dbgTimeCounter(_m)
355#else
356#define VBOXQGLLOG_METHODTIME(_m)
357#endif
358
359#define VBOXQGLLOG_QRECT(_p, _pr, _s) do{\
360 VBOXQGLLOG((_p " x(%d), y(%d), w(%d), h(%d)" _s, (_pr)->x(), (_pr)->y(), (_pr)->width(), (_pr)->height()));\
361 }while(0)
362
363#define VBOXQGLLOG_CKEY(_p, _pck, _s) do{\
364 VBOXQGLLOG((_p " l(0x%x), u(0x%x)" _s, (_pck)->lower(), (_pck)->upper()));\
365 }while(0)
366
367class VBoxVHWADirtyRect
368{
369public:
370 VBoxVHWADirtyRect() :
371 mIsClear(true)
372 {}
373
374 VBoxVHWADirtyRect(const QRect & aRect)
375 {
376 if(aRect.isEmpty())
377 {
378 mIsClear = false;
379 mRect = aRect;
380 }
381 else
382 {
383 mIsClear = true;
384 }
385 }
386
387 bool isClear() const { return mIsClear; }
388
389 void add(const QRect & aRect)
390 {
391 if(aRect.isEmpty())
392 return;
393
394 mRect = mIsClear ? aRect : mRect.united(aRect);
395 mIsClear = false;
396 }
397
398 void add(const VBoxVHWADirtyRect & aRect)
399 {
400 if(aRect.isClear())
401 return;
402 add(aRect.rect());
403 }
404
405 void set(const QRect & aRect)
406 {
407 if(aRect.isEmpty())
408 {
409 mIsClear = true;
410 }
411 else
412 {
413 mRect = aRect;
414 mIsClear = false;
415 }
416 }
417
418 void clear() { mIsClear = true; }
419
420 const QRect & rect() const {return mRect;}
421
422 const QRect & toRect()
423 {
424 if(isClear())
425 {
426 mRect.setCoords(0, 0, -1, -1);
427 }
428 return mRect;
429 }
430
431 bool intersects(const QRect & aRect) const {return mIsClear ? false : mRect.intersects(aRect);}
432
433 bool intersects(const VBoxVHWADirtyRect & aRect) const {return mIsClear ? false : aRect.intersects(mRect);}
434
435 QRect united(const QRect & aRect) const {return mIsClear ? aRect : aRect.united(mRect);}
436
437 bool contains(const QRect & aRect) const {return mIsClear ? false : aRect.contains(mRect);}
438
439 void subst(const VBoxVHWADirtyRect & aRect) { if(!mIsClear && aRect.contains(mRect)) clear(); }
440
441private:
442 QRect mRect;
443 bool mIsClear;
444};
445
446class VBoxVHWAColorKey
447{
448public:
449 VBoxVHWAColorKey() :
450 mUpper(0),
451 mLower(0)
452 {}
453
454 VBoxVHWAColorKey(uint32_t aUpper, uint32_t aLower) :
455 mUpper(aUpper),
456 mLower(aLower)
457 {}
458
459 uint32_t upper() const {return mUpper; }
460 uint32_t lower() const {return mLower; }
461
462 bool operator==(const VBoxVHWAColorKey & other) const { return mUpper == other.mUpper && mLower == other.mLower; }
463private:
464 uint32_t mUpper;
465 uint32_t mLower;
466};
467
468class VBoxVHWAColorComponent
469{
470public:
471 VBoxVHWAColorComponent() :
472 mMask(0),
473 mRange(0),
474 mOffset(32),
475 mcBits(0)
476 {}
477
478 VBoxVHWAColorComponent(uint32_t aMask);
479
480 uint32_t mask() const { return mMask; }
481 uint32_t range() const { return mRange; }
482 uint32_t offset() const { return mOffset; }
483 uint32_t cBits() const { return mcBits; }
484 uint32_t colorVal(uint32_t col) const { return (col & mMask) >> mOffset; }
485 float colorValNorm(uint32_t col) const { return ((float)colorVal(col))/mRange; }
486private:
487 uint32_t mMask;
488 uint32_t mRange;
489 uint32_t mOffset;
490 uint32_t mcBits;
491};
492
493class VBoxVHWAColorFormat
494{
495public:
496
497// VBoxVHWAColorFormat(GLint aInternalFormat, GLenum aFormat, GLenum aType, uint32_t aDataFormat);
498 VBoxVHWAColorFormat(uint32_t bitsPerPixel, uint32_t r, uint32_t g, uint32_t b);
499 VBoxVHWAColorFormat(uint32_t fourcc);
500 VBoxVHWAColorFormat(){}
501 GLint internalFormat() const {return mInternalFormat; }
502 GLenum format() const {return mFormat; }
503 GLenum type() const {return mType; }
504 bool isValid() const {return mBitsPerPixel != 0; }
505 uint32_t fourcc() const {return mDataFormat;}
506 uint32_t bitsPerPixel() const { return mBitsPerPixel; }
507 uint32_t bitsPerPixelTex() const { return mBitsPerPixelTex; }
508// uint32_t bitsPerPixelDd() const { return mBitsPerPixelDd; }
509 void pixel2Normalized(uint32_t pix, float *r, float *g, float *b) const;
510 uint32_t widthCompression() const {return mWidthCompression;}
511 uint32_t heightCompression() const {return mHeightCompression;}
512 const VBoxVHWAColorComponent& r() const {return mR;}
513 const VBoxVHWAColorComponent& g() const {return mG;}
514 const VBoxVHWAColorComponent& b() const {return mB;}
515 const VBoxVHWAColorComponent& a() const {return mA;}
516
517 bool equals (const VBoxVHWAColorFormat & other) const;
518
519private:
520 void init(uint32_t bitsPerPixel, uint32_t r, uint32_t g, uint32_t b);
521 void init(uint32_t fourcc);
522
523 GLint mInternalFormat;
524 GLenum mFormat;
525 GLenum mType;
526 uint32_t mDataFormat;
527
528 uint32_t mBitsPerPixel;
529 uint32_t mBitsPerPixelTex;
530// uint32_t mBitsPerPixelDd;
531 uint32_t mWidthCompression;
532 uint32_t mHeightCompression;
533 VBoxVHWAColorComponent mR;
534 VBoxVHWAColorComponent mG;
535 VBoxVHWAColorComponent mB;
536 VBoxVHWAColorComponent mA;
537};
538
539class VBoxVHWATexture
540{
541public:
542 VBoxVHWATexture() {}
543 VBoxVHWATexture(const QRect & aRect, const VBoxVHWAColorFormat &aFormat);
544 virtual ~VBoxVHWATexture();
545 virtual void init(uchar *pvMem);
546 void setAddress(uchar *pvMem) {mAddress = pvMem;}
547 void update(const QRect * pRect) { doUpdate(mAddress, pRect);}
548 void bind() {glBindTexture(texTarget(), mTexture);}
549
550 virtual void texCoord(int x, int y);
551 virtual void multiTexCoord(GLenum texUnit, int x, int y);
552
553// GLuint texture() {return mTexture;}
554 const QRect & texRect() {return mTexRect;}
555 const QRect & rect() {return mRect;}
556 uchar * address(){ return mAddress; }
557 uint32_t rectSizeTex(const QRect * pRect) {return pRect->width() * pRect->height() * mBytesPerPixelTex;}
558 uchar * pointAddress(int x, int y)
559 {
560 x = toXTex(x);
561 y = toYTex(y);
562 return pointAddressTex(x, y);
563 }
564 uint32_t pointOffsetTex(int x, int y) { return y*mBytesPerLine + x*mBytesPerPixelTex; }
565 uchar * pointAddressTex(int x, int y) { return mAddress + pointOffsetTex(x, y); }
566 int toXTex(int x) {return x/mColorFormat.widthCompression();}
567 int toYTex(int y) {return y/mColorFormat.heightCompression();}
568 ulong memSize(){ return mBytesPerLine * mRect.height(); }
569 uint32_t bytesPerLine() {return mBytesPerLine; }
570
571protected:
572 virtual void doUpdate(uchar * pAddress, const QRect * pRect);
573 virtual void initParams();
574 virtual void load();
575 virtual GLenum texTarget() {return GL_TEXTURE_2D; }
576
577
578 QRect mTexRect; /* texture size */
579 QRect mRect; /* img size */
580 uchar * mAddress;
581 GLuint mTexture;
582 uint32_t mBytesPerPixel;
583 uint32_t mBytesPerPixelTex;
584 uint32_t mBytesPerLine;
585 VBoxVHWAColorFormat mColorFormat;
586private:
587 void uninit();
588};
589
590class VBoxVHWATextureNP2 : public VBoxVHWATexture
591{
592public:
593 VBoxVHWATextureNP2() : VBoxVHWATexture() {}
594 VBoxVHWATextureNP2(const QRect & aRect, const VBoxVHWAColorFormat &aFormat) :
595 VBoxVHWATexture(aRect, aFormat){
596 mTexRect = QRect(0, 0, aRect.width()/aFormat.widthCompression(), aRect.height()/aFormat.heightCompression());
597 }
598};
599
600class VBoxVHWATextureNP2Rect : public VBoxVHWATextureNP2
601{
602public:
603 VBoxVHWATextureNP2Rect() : VBoxVHWATextureNP2() {}
604 VBoxVHWATextureNP2Rect(const QRect & aRect, const VBoxVHWAColorFormat &aFormat) :
605 VBoxVHWATextureNP2(aRect, aFormat){}
606
607 virtual void texCoord(int x, int y);
608 virtual void multiTexCoord(GLenum texUnit, int x, int y);
609protected:
610 virtual GLenum texTarget();
611};
612
613class VBoxVHWATextureNP2RectPBO : public VBoxVHWATextureNP2Rect
614{
615public:
616 VBoxVHWATextureNP2RectPBO() : VBoxVHWATextureNP2Rect() {}
617 VBoxVHWATextureNP2RectPBO(const QRect & aRect, const VBoxVHWAColorFormat &aFormat) :
618 VBoxVHWATextureNP2Rect(aRect, aFormat){}
619 virtual ~VBoxVHWATextureNP2RectPBO();
620
621 virtual void init(uchar *pvMem);
622protected:
623 virtual void load();
624 virtual void doUpdate(uchar * pAddress, const QRect * pRect);
625private:
626 GLuint mPBO;
627};
628
629class VBoxVHWAHandleTable
630{
631public:
632 VBoxVHWAHandleTable(uint32_t initialSize);
633 ~VBoxVHWAHandleTable();
634 uint32_t put(void * data);
635 bool mapPut(uint32_t h, void * data);
636 void* get(uint32_t h);
637 void* remove(uint32_t h);
638private:
639 void doPut(uint32_t h, void * data);
640 void doRemove(uint32_t h);
641 void** mTable;
642 uint32_t mcSize;
643 uint32_t mcUsage;
644 uint32_t mCursor;
645};
646
647/* data flow:
648 * I. NON-Yinverted surface:
649 * 1.direct memory update (paint, lock/unlock):
650 * mem->tex->fb
651 * 2.blt
652 * srcTex->invFB->tex->fb
653 * |->mem
654 *
655 * II. Yinverted surface:
656 * 1.direct memory update (paint, lock/unlock):
657 * mem->tex->fb
658 * 2.blt
659 * srcTex->fb->tex
660 * |->mem
661 *
662 * III. flip support:
663 * 1. Yinverted<->NON-YInverted conversion :
664 * mem->tex-(rotate model view, force LAZY complete fb update)->invFB->tex
665 * fb-->| |->mem
666 * */
667class VBoxVHWASurfaceBase
668{
669public:
670 VBoxVHWASurfaceBase(
671 class VBoxGLWidget *mWidget,
672#if 0
673 class VBoxVHWAGlContextState *aState,
674 bool aIsYInverted,
675#endif
676 const QSize & aSize,
677 const QRect & aTargRect,
678 const QRect & aSrcRect,
679 const QRect & aVisTargRect,
680 VBoxVHWAColorFormat & aColorFormat,
681 VBoxVHWAColorKey * pSrcBltCKey, VBoxVHWAColorKey * pDstBltCKey,
682 VBoxVHWAColorKey * pSrcOverlayCKey, VBoxVHWAColorKey * pDstOverlayCKey,
683 bool bVGA);
684
685 virtual ~VBoxVHWASurfaceBase();
686
687 void init(VBoxVHWASurfaceBase * pPrimary, uchar *pvMem);
688
689 void uninit();
690
691 static void globalInit();
692
693// int blt(const QRect * aDstRect, VBoxVHWASurfaceBase * aSrtSurface, const QRect * aSrcRect, const VBoxVHWAColorKey * pDstCKeyOverride, const VBoxVHWAColorKey * pSrcCKeyOverride);
694
695 int lock(const QRect * pRect, uint32_t flags);
696
697 int unlock();
698
699 void updatedMem(const QRect * aRect);
700
701 void performDisplay(VBoxVHWASurfaceBase *pPrimary);
702
703 void setRects(VBoxVHWASurfaceBase *pPrimary, const QRect & aTargRect, const QRect & aSrcRect, const QRect & aVisibleTargRect, bool bForceReinit);
704 void setTargRectPosition(VBoxVHWASurfaceBase *pPrimary, const QPoint & aPoint, const QRect & aVisibleTargRect);
705 void updateVisibleTargRect(VBoxVHWASurfaceBase *pPrimary, const QRect & aVisibleTargRect);
706
707 static ulong calcBytesPerPixel(GLenum format, GLenum type);
708
709 static GLsizei makePowerOf2(GLsizei val);
710
711 bool addressAlocated() const { return mFreeAddress; }
712 uchar * address(){ return mAddress; }
713
714 ulong memSize();
715
716 ulong width() { return mRect.width(); }
717 ulong height() { return mRect.height(); }
718 const QSize size() {return mRect.size();}
719
720 GLenum format() {return mColorFormat.format(); }
721 GLint internalFormat() { return mColorFormat.internalFormat(); }
722 GLenum type() { return mColorFormat.type(); }
723 uint32_t fourcc() {return mColorFormat.fourcc(); }
724
725// ulong bytesPerPixel() { return mpTex[0]->bytesPerPixel(); }
726 ulong bitsPerPixel() { return mColorFormat.bitsPerPixel(); }
727// ulong bitsPerPixelDd() { return mColorFormat.bitsPerPixelDd(); }
728 ulong bytesPerLine() { return mpTex[0]->bytesPerLine(); }
729
730 const VBoxVHWAColorKey * dstBltCKey() const { return mpDstBltCKey; }
731 const VBoxVHWAColorKey * srcBltCKey() const { return mpSrcBltCKey; }
732 const VBoxVHWAColorKey * dstOverlayCKey() const { return mpDstOverlayCKey; }
733 const VBoxVHWAColorKey * defaultSrcOverlayCKey() const { return mpDefaultSrcOverlayCKey; }
734 const VBoxVHWAColorKey * defaultDstOverlayCKey() const { return mpDefaultDstOverlayCKey; }
735 const VBoxVHWAColorKey * srcOverlayCKey() const { return mpSrcOverlayCKey; }
736 void resetDefaultSrcOverlayCKey() { mpSrcOverlayCKey = mpDefaultSrcOverlayCKey; }
737 void resetDefaultDstOverlayCKey() { mpDstOverlayCKey = mpDefaultDstOverlayCKey; }
738
739 void setDstBltCKey(const VBoxVHWAColorKey * ckey)
740 {
741 if(ckey)
742 {
743 mDstBltCKey = *ckey;
744 mpDstBltCKey = &mDstBltCKey;
745 }
746 else
747 {
748 mpDstBltCKey = NULL;
749 }
750 }
751
752 void setSrcBltCKey(const VBoxVHWAColorKey * ckey)
753 {
754 if(ckey)
755 {
756 mSrcBltCKey = *ckey;
757 mpSrcBltCKey = &mSrcBltCKey;
758 }
759 else
760 {
761 mpSrcBltCKey = NULL;
762 }
763 }
764
765 void setDefaultDstOverlayCKey(const VBoxVHWAColorKey * ckey)
766 {
767 if(ckey)
768 {
769 mDefaultDstOverlayCKey = *ckey;
770 mpDefaultDstOverlayCKey = &mDefaultDstOverlayCKey;
771 }
772 else
773 {
774 mpDefaultDstOverlayCKey = NULL;
775 }
776 }
777
778 void setDefaultSrcOverlayCKey(const VBoxVHWAColorKey * ckey)
779 {
780 if(ckey)
781 {
782 mDefaultSrcOverlayCKey = *ckey;
783 mpDefaultSrcOverlayCKey = &mDefaultSrcOverlayCKey;
784 }
785 else
786 {
787 mpDefaultSrcOverlayCKey = NULL;
788 }
789 }
790
791 void setOverriddenDstOverlayCKey(const VBoxVHWAColorKey * ckey)
792 {
793 if(ckey)
794 {
795 mOverriddenDstOverlayCKey = *ckey;
796 mpDstOverlayCKey = &mOverriddenDstOverlayCKey;
797 }
798 else
799 {
800 mpDstOverlayCKey = NULL;
801 }
802 }
803
804 void setOverriddenSrcOverlayCKey(const VBoxVHWAColorKey * ckey)
805 {
806 if(ckey)
807 {
808 mOverriddenSrcOverlayCKey = *ckey;
809 mpSrcOverlayCKey = &mOverriddenSrcOverlayCKey;
810 }
811 else
812 {
813 mpSrcOverlayCKey = NULL;
814 }
815 }
816
817 const VBoxVHWAColorKey * getActiveSrcOverlayCKey()
818 {
819 return mpSrcOverlayCKey;
820 }
821
822 const VBoxVHWAColorKey * getActiveDstOverlayCKey(VBoxVHWASurfaceBase * pPrimary)
823 {
824 return mpDstOverlayCKey ? mpDefaultDstOverlayCKey : pPrimary->mpDstOverlayCKey;
825 }
826
827 const VBoxVHWAColorFormat & colorFormat() {return mColorFormat; }
828
829 void setAddress(uchar * addr);
830
831 const QRect& rect() const {return mRect;}
832 const QRect& srcRect() const {return mSrcRect; }
833 const QRect& targRect() const {return mTargRect; }
834 class VBoxVHWASurfList * getComplexList() {return mComplexList; }
835
836 class VBoxVHWAGlProgramMngr * getGlProgramMngr();
837 static int setCKey(class VBoxVHWAGlProgramVHWA * pProgram, const VBoxVHWAColorFormat * pFormat, const VBoxVHWAColorKey * pCKey, bool bDst);
838
839 uint32_t handle() const {return mHGHandle;}
840 void setHandle(uint32_t h) {mHGHandle = h;}
841
842private:
843 void doSetRectValuesInternal(const QRect & aTargRect, const QRect & aSrcRect, const QRect & aVisTargRect);
844
845 void setComplexList(VBoxVHWASurfList *aComplexList) { mComplexList = aComplexList; }
846 void initDisplay(VBoxVHWASurfaceBase *pPrimary);
847 void deleteDisplay();
848
849 GLuint createDisplay(VBoxVHWASurfaceBase *pPrimary);
850 void doDisplay(VBoxVHWASurfaceBase *pPrimary, VBoxVHWAGlProgramVHWA * pProgram, bool bBindDst);
851 void synchTexMem(const QRect * aRect);
852
853 int performBlt(const QRect * pDstRect, VBoxVHWASurfaceBase * pSrcSurface, const QRect * pSrcRect, const VBoxVHWAColorKey * pDstCKey, const VBoxVHWAColorKey * pSrcCKey, bool blt);
854
855 void doTex2FB(const QRect * pDstRect, const QRect * pSrcRect);
856 void doMultiTex2FB(const QRect * pDstRect, VBoxVHWATexture * pDstTex, const QRect * pSrcRect, int cSrcTex);
857 void doMultiTex2FB(const QRect * pDstRect, const QRect * pSrcRect, int cSrcTex);
858
859 QRect mRect; /* == Inv FB size */
860
861 QRect mSrcRect;
862 QRect mTargRect; /* == Vis FB size */
863
864 QRect mVisibleTargRect;
865 QRect mVisibleSrcRect;
866
867 GLuint mVisibleDisplay;
868
869 bool mVisibleDisplayInitialized;
870
871 uchar * mAddress;
872 VBoxVHWATexture *mpTex[3];
873
874 VBoxVHWAColorFormat mColorFormat;
875
876 VBoxVHWAColorKey *mpSrcBltCKey;
877 VBoxVHWAColorKey *mpDstBltCKey;
878 VBoxVHWAColorKey *mpSrcOverlayCKey;
879 VBoxVHWAColorKey *mpDstOverlayCKey;
880
881 VBoxVHWAColorKey *mpDefaultDstOverlayCKey;
882 VBoxVHWAColorKey *mpDefaultSrcOverlayCKey;
883
884 VBoxVHWAColorKey mSrcBltCKey;
885 VBoxVHWAColorKey mDstBltCKey;
886 VBoxVHWAColorKey mOverriddenSrcOverlayCKey;
887 VBoxVHWAColorKey mOverriddenDstOverlayCKey;
888 VBoxVHWAColorKey mDefaultDstOverlayCKey;
889 VBoxVHWAColorKey mDefaultSrcOverlayCKey;
890
891 int mLockCount;
892 /* memory buffer not reflected in fm and texture, e.g if memory buffer is replaced or in case of lock/unlock */
893 VBoxVHWADirtyRect mUpdateMem2TexRect;
894
895 bool mFreeAddress;
896
897 class VBoxVHWASurfList *mComplexList;
898
899 class VBoxGLWidget *mWidget;
900
901 uint32_t mHGHandle;
902
903#ifdef DEBUG
904public:
905 uint64_t cFlipsCurr;
906 uint64_t cFlipsTarg;
907#endif
908 friend class VBoxVHWASurfList;
909};
910
911typedef std::list <VBoxVHWASurfaceBase*> SurfList;
912typedef std::list <VBoxVHWASurfList*> OverlayList;
913typedef std::list <struct _VBOXVHWACMD *> VHWACommandList;
914
915class VBoxVHWASurfList
916{
917public:
918
919 VBoxVHWASurfList() : mCurrent(NULL) {}
920 void add(VBoxVHWASurfaceBase *pSurf)
921 {
922 VBoxVHWASurfList * pOld = pSurf->getComplexList();
923 if(pOld)
924 {
925 pOld->remove(pSurf);
926 }
927 mSurfaces.push_back(pSurf);
928 pSurf->setComplexList(this);
929 }
930
931 void clear()
932 {
933 for (SurfList::iterator it = mSurfaces.begin();
934 it != mSurfaces.end(); ++ it)
935 {
936 (*it)->setComplexList(NULL);
937 }
938 mSurfaces.clear();
939 mCurrent = NULL;
940 }
941
942 size_t size() const {return mSurfaces.size(); }
943
944 void remove(VBoxVHWASurfaceBase *pSurf)
945 {
946 mSurfaces.remove(pSurf);
947 pSurf->setComplexList(NULL);
948 if(mCurrent == pSurf)
949 mCurrent = NULL;
950 }
951
952 bool empty() { return mSurfaces.empty(); }
953
954 void setCurrentVisible(VBoxVHWASurfaceBase *pSurf)
955 {
956 mCurrent = pSurf;
957 }
958
959 VBoxVHWASurfaceBase * current() { return mCurrent; }
960 const SurfList & surfaces() const {return mSurfaces;}
961
962private:
963
964 SurfList mSurfaces;
965 VBoxVHWASurfaceBase* mCurrent;
966};
967
968class VBoxVHWADisplay
969{
970public:
971 VBoxVHWADisplay() :
972 mSurfVGA(NULL)
973// ,
974// mSurfPrimary(NULL)
975 {}
976
977 VBoxVHWASurfaceBase * setVGA(VBoxVHWASurfaceBase * pVga)
978 {
979 VBoxVHWASurfaceBase * old = mSurfVGA;
980 mSurfVGA = pVga;
981 mPrimary.clear();
982 if(pVga)
983 {
984 Assert(!pVga->getComplexList());
985 mPrimary.add(pVga);
986 mPrimary.setCurrentVisible(pVga);
987 }
988// mSurfPrimary = pVga;
989 mOverlays.clear();
990 return old;
991 }
992
993 VBoxVHWASurfaceBase * updateVGA(VBoxVHWASurfaceBase * pVga)
994 {
995 VBoxVHWASurfaceBase * old = mSurfVGA;
996 Assert(old);
997 mSurfVGA = pVga;
998 return old;
999 }
1000
1001 VBoxVHWASurfaceBase * getVGA() const
1002 {
1003 return mSurfVGA;
1004 }
1005
1006 VBoxVHWASurfaceBase * getPrimary()
1007 {
1008 return mPrimary.current();
1009 }
1010
1011 void addOverlay(VBoxVHWASurfList * pSurf)
1012 {
1013 mOverlays.push_back(pSurf);
1014 }
1015
1016 void checkAddOverlay(VBoxVHWASurfList * pSurf)
1017 {
1018 if(!hasOverlay(pSurf))
1019 addOverlay(pSurf);
1020 }
1021
1022 bool hasOverlay(VBoxVHWASurfList * pSurf)
1023 {
1024 for (OverlayList::iterator it = mOverlays.begin();
1025 it != mOverlays.end(); ++ it)
1026 {
1027 if((*it) == pSurf)
1028 {
1029 return true;
1030 }
1031 }
1032 return false;
1033 }
1034
1035 void removeOverlay(VBoxVHWASurfList * pSurf)
1036 {
1037 mOverlays.remove(pSurf);
1038 }
1039
1040 void performDisplay()
1041 {
1042 VBoxVHWASurfaceBase * pPrimary = mPrimary.current();
1043 pPrimary->performDisplay(NULL);
1044
1045 for (OverlayList::const_iterator it = mOverlays.begin();
1046 it != mOverlays.end(); ++ it)
1047 {
1048 VBoxVHWASurfaceBase * pOverlay = (*it)->current();
1049 if(pOverlay)
1050 {
1051 pOverlay->performDisplay(pPrimary);
1052 }
1053 }
1054 }
1055
1056 const OverlayList & overlays() const {return mOverlays;}
1057 const VBoxVHWASurfList & primaries() const { return mPrimary; }
1058
1059private:
1060 VBoxVHWASurfaceBase *mSurfVGA;
1061 VBoxVHWASurfList mPrimary;
1062
1063 OverlayList mOverlays;
1064};
1065
1066typedef void (VBoxGLWidget::*PFNVBOXQGLOP)(void* );
1067
1068typedef void (*PFNVBOXQGLFUNC)(void*, void*);
1069
1070typedef enum
1071{
1072 VBOXVHWA_PIPECMD_PAINT = 1,
1073 VBOXVHWA_PIPECMD_VHWA,
1074 VBOXVHWA_PIPECMD_OP,
1075 VBOXVHWA_PIPECMD_FUNC,
1076}VBOXVHWA_PIPECMD_TYPE;
1077
1078typedef struct VBOXVHWACALLBACKINFO
1079{
1080 VBoxGLWidget *pThis;
1081 PFNVBOXQGLOP pfnCallback;
1082 void * pContext;
1083}VBOXVHWACALLBACKINFO;
1084
1085typedef struct VBOXVHWAFUNCCALLBACKINFO
1086{
1087 PFNVBOXQGLFUNC pfnCallback;
1088 void * pContext1;
1089 void * pContext2;
1090}VBOXVHWAFUNCCALLBACKINFO;
1091
1092class VBoxVHWACommandElement
1093{
1094public:
1095 void setVHWACmd(struct _VBOXVHWACMD * pCmd)
1096 {
1097 mType = VBOXVHWA_PIPECMD_VHWA;
1098 u.mpCmd = pCmd;
1099 }
1100
1101 void setPaintCmd(const QRect & aRect)
1102 {
1103 mType = VBOXVHWA_PIPECMD_PAINT;
1104 mRect = aRect;
1105 }
1106
1107 void setOp(const VBOXVHWACALLBACKINFO & aOp)
1108 {
1109 mType = VBOXVHWA_PIPECMD_OP;
1110 u.mCallback = aOp;
1111 }
1112
1113 void setFunc(const VBOXVHWAFUNCCALLBACKINFO & aOp)
1114 {
1115 mType = VBOXVHWA_PIPECMD_FUNC;
1116 u.mFuncCallback = aOp;
1117 }
1118
1119 void setData(VBOXVHWA_PIPECMD_TYPE aType, void * pvData)
1120 {
1121 switch(aType)
1122 {
1123 case VBOXVHWA_PIPECMD_PAINT:
1124 setPaintCmd(*((QRect*)pvData));
1125 break;
1126 case VBOXVHWA_PIPECMD_VHWA:
1127 setVHWACmd((struct _VBOXVHWACMD *)pvData);
1128 break;
1129 case VBOXVHWA_PIPECMD_OP:
1130 setOp(*((VBOXVHWACALLBACKINFO *)pvData));
1131 break;
1132 case VBOXVHWA_PIPECMD_FUNC:
1133 setFunc(*((VBOXVHWAFUNCCALLBACKINFO *)pvData));
1134 break;
1135 default:
1136 Assert(0);
1137 break;
1138 }
1139 }
1140
1141 VBOXVHWA_PIPECMD_TYPE type() const {return mType;}
1142 const QRect & rect() const {return mRect;}
1143 struct _VBOXVHWACMD * vhwaCmd() const {return u.mpCmd;}
1144 const VBOXVHWACALLBACKINFO & op() const {return u.mCallback; }
1145 const VBOXVHWAFUNCCALLBACKINFO & func() const {return u.mFuncCallback; }
1146
1147 VBoxVHWACommandElement * mpNext;
1148private:
1149 VBOXVHWA_PIPECMD_TYPE mType;
1150 union
1151 {
1152 struct _VBOXVHWACMD * mpCmd;
1153 VBOXVHWACALLBACKINFO mCallback;
1154 VBOXVHWAFUNCCALLBACKINFO mFuncCallback;
1155 }u;
1156 QRect mRect;
1157};
1158
1159class VBoxVHWACommandElementPipe
1160{
1161public:
1162 VBoxVHWACommandElementPipe() :
1163 mpFirst(NULL),
1164 mpLast(NULL)
1165 {}
1166
1167 void put(VBoxVHWACommandElement *pCmd)
1168 {
1169 if(mpLast)
1170 {
1171 Assert(mpFirst);
1172 mpLast->mpNext = pCmd;
1173 mpLast = pCmd;
1174 }
1175 else
1176 {
1177 Assert(!mpFirst);
1178 mpFirst = pCmd;
1179 mpLast = pCmd;
1180 }
1181 pCmd->mpNext= NULL;
1182
1183 }
1184
1185 VBoxVHWACommandElement * detachList()
1186 {
1187 if(mpLast)
1188 {
1189 VBoxVHWACommandElement * pHead = mpFirst;
1190 mpFirst = NULL;
1191 mpLast = NULL;
1192 return pHead;
1193 }
1194 return NULL;
1195 }
1196private:
1197 VBoxVHWACommandElement *mpFirst;
1198 VBoxVHWACommandElement *mpLast;
1199};
1200
1201class VBoxVHWACommandElementStack
1202{
1203public:
1204 VBoxVHWACommandElementStack() :
1205 mpFirst(NULL) {}
1206
1207 void push(VBoxVHWACommandElement *pCmd)
1208 {
1209 pCmd->mpNext = mpFirst;
1210 mpFirst = pCmd;
1211 }
1212
1213 void pusha(VBoxVHWACommandElement *pFirst, VBoxVHWACommandElement *pLast)
1214 {
1215 pLast->mpNext = mpFirst;
1216 mpFirst = pFirst;
1217 }
1218
1219 VBoxVHWACommandElement * pop()
1220 {
1221 if(mpFirst)
1222 {
1223 VBoxVHWACommandElement * ret = mpFirst;
1224 mpFirst = ret->mpNext;
1225 return ret;
1226 }
1227 return NULL;
1228 }
1229private:
1230 VBoxVHWACommandElement *mpFirst;
1231};
1232
1233class VBoxVHWACommandElementProcessor
1234{
1235public:
1236 VBoxVHWACommandElementProcessor(VBoxConsoleView *aView);
1237 ~VBoxVHWACommandElementProcessor();
1238 void postCmd(VBOXVHWA_PIPECMD_TYPE aType, void * pvData);
1239 class VBoxVHWACommandElement * detachCmdList(class VBoxVHWACommandElement * pFirst2Free, VBoxVHWACommandElement * pLast2Free);
1240
1241private:
1242 RTCRITSECT mCritSect;
1243 class VBoxVHWACommandProcessEvent *mpFirstEvent;
1244 class VBoxVHWACommandProcessEvent *mpLastEvent;
1245 VBoxConsoleView *mView;
1246 bool mbNewEvent;
1247 VBoxVHWACommandElementStack mFreeElements;
1248 VBoxVHWACommandElement mElementsBuffer[2048];
1249};
1250
1251class VBoxVHWACommandsQueue
1252{
1253public:
1254 void enqueue(PFNVBOXQGLFUNC pfn, void* pContext1, void* pContext2);
1255
1256 VBoxVHWACommandElement * detachList();
1257
1258 void freeList(VBoxVHWACommandElement * pList);
1259
1260private:
1261 VBoxVHWACommandElementPipe mCmds;
1262};
1263
1264class VBoxGLWidget : public QGLWidget
1265{
1266public:
1267 VBoxGLWidget (VBoxConsoleView *aView, QWidget *aParent);
1268 ~VBoxGLWidget();
1269
1270 ulong vboxPixelFormat() { return mPixelFormat; }
1271 bool vboxUsesGuestVRAM() { return mUsesGuestVRAM; }
1272
1273 uchar *vboxAddress() { return mDisplay.getVGA() ? mDisplay.getVGA()->address() : NULL; }
1274
1275#ifdef VBOX_WITH_VIDEOHWACCEL
1276 uchar *vboxVRAMAddressFromOffset(uint64_t offset);
1277 uint64_t vboxVRAMOffsetFromAddress(uchar* addr);
1278 uint64_t vboxVRAMOffset(VBoxVHWASurfaceBase * pSurf);
1279
1280 void vhwaSaveExec(struct SSMHANDLE * pSSM);
1281 int vhwaLoadExec(VHWACommandList * pCmdList, struct SSMHANDLE * pSSM, uint32_t u32Version);
1282
1283 int vhwaSurfaceCanCreate(struct _VBOXVHWACMD_SURF_CANCREATE *pCmd);
1284 int vhwaSurfaceCreate(struct _VBOXVHWACMD_SURF_CREATE *pCmd);
1285 int vhwaSurfaceDestroy(struct _VBOXVHWACMD_SURF_DESTROY *pCmd);
1286 int vhwaSurfaceLock(struct _VBOXVHWACMD_SURF_LOCK *pCmd);
1287 int vhwaSurfaceUnlock(struct _VBOXVHWACMD_SURF_UNLOCK *pCmd);
1288 int vhwaSurfaceBlt(struct _VBOXVHWACMD_SURF_BLT *pCmd);
1289 int vhwaSurfaceFlip(struct _VBOXVHWACMD_SURF_FLIP *pCmd);
1290 int vhwaSurfaceOverlayUpdate(struct _VBOXVHWACMD_SURF_OVERLAY_UPDATE *pCmf);
1291 int vhwaSurfaceOverlaySetPosition(struct _VBOXVHWACMD_SURF_OVERLAY_SETPOSITION *pCmd);
1292 int vhwaSurfaceColorkeySet(struct _VBOXVHWACMD_SURF_COLORKEY_SET *pCmd);
1293 int vhwaQueryInfo1(struct _VBOXVHWACMD_QUERYINFO1 *pCmd);
1294 int vhwaQueryInfo2(struct _VBOXVHWACMD_QUERYINFO2 *pCmd);
1295 int vhwaConstruct(struct _VBOXVHWACMD_HH_CONSTRUCT *pCmd);
1296
1297 bool hasSurfaces() const;
1298 bool hasVisibleOverlays();
1299 const QRect & overlaysRectUnion();
1300#endif
1301
1302 ulong vboxBitsPerPixel() { return mDisplay.getVGA()->bitsPerPixel(); }
1303 ulong vboxBytesPerLine() { return mDisplay.getVGA() ? mDisplay.getVGA()->bytesPerLine() : 0; }
1304 int vboxFbWidth() {return mDisplay.getVGA()->width(); }
1305 int vboxFbHeight() {return mDisplay.getVGA()->height(); }
1306 bool vboxIsInitialized() {return mDisplay.getVGA() != NULL; }
1307
1308// void vboxPaintEvent (QPaintEvent *pe) {vboxPerformGLOp(&VBoxGLWidget::vboxDoPaint, pe); }
1309 void vboxResizeEvent (VBoxResizeEvent *re) {vboxPerformGLOp(&VBoxGLWidget::vboxDoResize, re); }
1310
1311 void vboxProcessVHWACommands(class VBoxVHWACommandElementProcessor * pPipe) {vboxPerformGLOp(&VBoxGLWidget::vboxDoProcessVHWACommands, pPipe);}
1312#ifdef VBOX_WITH_VIDEOHWACCEL
1313 void vboxVHWACmd (struct _VBOXVHWACMD * pCmd) {vboxPerformGLOp(&VBoxGLWidget::vboxDoVHWACmd, pCmd);}
1314#endif
1315 class VBoxVHWAGlProgramMngr * vboxVHWAGetGlProgramMngr() { return mpMngr; }
1316
1317 VBoxVHWASurfaceBase * vboxGetVGASurface() { return mDisplay.getVGA(); }
1318
1319 static void doSetupMatrix(const QSize & aSize, bool bInverted);
1320
1321 void vboxDoUpdateViewport(const QRect & aRect);
1322 void vboxDoUpdateRect(const QRect * pRect);
1323
1324 const QRect & vboxViewport() const {return mViewport;}
1325
1326 void performDisplay() { mDisplay.performDisplay(); }
1327protected:
1328
1329 void paintGL()
1330 {
1331 if(mpfnOp)
1332 {
1333 (this->*mpfnOp)(mOpContext);
1334 mpfnOp = NULL;
1335 }
1336// else
1337// {
1338 mDisplay.performDisplay();
1339// }
1340 }
1341
1342 void initializeGL();
1343
1344private:
1345 static void setupMatricies(const QSize &display);
1346 static void adjustViewport(const QSize &display, const QRect &viewport);
1347 void vboxDoResize(void *re);
1348// void vboxDoPaint(void *rec);
1349
1350
1351#ifdef VBOXQGL_DBG_SURF
1352 void vboxDoTestSurfaces(void *context);
1353#endif
1354#ifdef VBOX_WITH_VIDEOHWACCEL
1355 void vboxDoVHWACmdExec(void *cmd);
1356 void vboxDoVHWACmdAndFree(void *cmd);
1357 void vboxDoVHWACmd(void *cmd);
1358
1359 void vboxCheckUpdateAddress (VBoxVHWASurfaceBase * pSurface, uint64_t offset)
1360 {
1361 if (pSurface->addressAlocated())
1362 {
1363 uchar * addr = vboxVRAMAddressFromOffset(offset);
1364 if(addr)
1365 {
1366 pSurface->setAddress(addr);
1367 }
1368 }
1369 }
1370
1371 int vhwaSaveSurface(struct SSMHANDLE * pSSM, VBoxVHWASurfaceBase *pSurf, uint32_t surfCaps);
1372 int vhwaLoadSurface(VHWACommandList * pCmdList, struct SSMHANDLE * pSSM, uint32_t u32Version);
1373 int vhwaSaveOverlayData(struct SSMHANDLE * pSSM, VBoxVHWASurfaceBase *pSurf, bool bVisible);
1374 int vhwaLoadOverlayData(VHWACommandList * pCmdList, struct SSMHANDLE * pSSM, uint32_t u32Version);
1375 void vhwaDoSurfaceOverlayUpdate(VBoxVHWASurfaceBase *pDstSurf, VBoxVHWASurfaceBase *pSrcSurf, struct _VBOXVHWACMD_SURF_OVERLAY_UPDATE *pCmd);
1376#endif
1377 static const QGLFormat & vboxGLFormat();
1378
1379 VBoxVHWADisplay mDisplay;
1380
1381
1382 /* we do all opengl stuff in the paintGL context,
1383 * submit the operation to be performed
1384 * @todo: could be moved outside the updateGL */
1385 void vboxPerformGLOp(PFNVBOXQGLOP pfn, void* pContext)
1386 {
1387 mpfnOp = pfn;
1388 mOpContext = pContext;
1389 updateGL();
1390 }
1391
1392// /* posts op to UI thread */
1393// int vboxExecOpSynch(PFNVBOXQGLOP pfn, void* pContext);
1394// void vboxExecOnResize(PFNVBOXQGLOP pfn, void* pContext);
1395
1396 void vboxDoProcessVHWACommands(void *pContext);
1397
1398 class VBoxVHWACommandElement * processCmdList(class VBoxVHWACommandElement * pCmd);
1399
1400 VBoxVHWASurfaceBase* handle2Surface(uint32_t h)
1401 {
1402 VBoxVHWASurfaceBase* pSurf = (VBoxVHWASurfaceBase*)mSurfHandleTable.get(h);
1403 Assert(pSurf);
1404 return pSurf;
1405 }
1406
1407 VBoxVHWAHandleTable mSurfHandleTable;
1408
1409 PFNVBOXQGLOP mpfnOp;
1410 void *mOpContext;
1411
1412 ulong mPixelFormat;
1413 bool mUsesGuestVRAM;
1414// bool mbVGASurfCreated;
1415 QRect mViewport;
1416
1417 VBoxConsoleView *mView;
1418
1419 VBoxVHWASurfList *mConstructingList;
1420 int32_t mcRemaining2Contruct;
1421
1422 /* this is used in saved state restore to postpone surface restoration
1423 * till the framebuffer size is restored */
1424 VHWACommandList mOnResizeCmdList;
1425
1426 class VBoxVHWAGlProgramMngr *mpMngr;
1427};
1428
1429
1430class VBoxQGLFrameBuffer : public VBoxFrameBuffer
1431{
1432public:
1433
1434 VBoxQGLFrameBuffer (VBoxConsoleView *aView);
1435
1436 STDMETHOD(NotifyUpdate) (ULONG aX, ULONG aY,
1437 ULONG aW, ULONG aH);
1438#ifdef VBOXQGL_PROF_BASE
1439 STDMETHOD(RequestResize) (ULONG aScreenId, ULONG aPixelFormat,
1440 BYTE *aVRAM, ULONG aBitsPerPixel, ULONG aBytesPerLine,
1441 ULONG aWidth, ULONG aHeight,
1442 BOOL *aFinished);
1443#endif
1444
1445#ifdef VBOX_WITH_VIDEOHWACCEL
1446 STDMETHOD(ProcessVHWACommand)(BYTE *pCommand);
1447
1448
1449 static bool isAcceleration2DVideoAvailable();
1450#endif
1451
1452 ulong pixelFormat() { return vboxWidget()->vboxPixelFormat(); }
1453 bool usesGuestVRAM() { return vboxWidget()->vboxUsesGuestVRAM(); }
1454
1455 uchar *address() { return vboxWidget()->vboxAddress(); }
1456 ulong bitsPerPixel() { return vboxWidget()->vboxBitsPerPixel(); }
1457 ulong bytesPerLine() { return vboxWidget()->vboxBytesPerLine(); }
1458
1459 void paintEvent (QPaintEvent *pe);
1460 void resizeEvent (VBoxResizeEvent *re);
1461 void doProcessVHWACommand(QEvent * pEvent);
1462
1463private:
1464// void vboxMakeCurrent();
1465 VBoxGLWidget * vboxWidget();
1466
1467 VBoxVHWACommandElementProcessor mCmdPipe;
1468};
1469
1470#ifdef VBOX_WITH_VIDEOHWACCEL
1471class VBoxQGLOverlayFrameBuffer : public VBoxQImageFrameBuffer
1472{
1473public:
1474 VBoxQGLOverlayFrameBuffer (VBoxConsoleView *aView);
1475
1476
1477 STDMETHOD(ProcessVHWACommand)(BYTE *pCommand);
1478
1479 void doProcessVHWACommand(QEvent * pEvent);
1480
1481 STDMETHOD(NotifyUpdate) (ULONG aX, ULONG aY,
1482 ULONG aW, ULONG aH);
1483
1484 void paintEvent (QPaintEvent *pe);
1485 void resizeEvent (VBoxResizeEvent *re);
1486
1487 void vboxUpdateRect(const QRect * pRect);
1488
1489 int vhwaLoadExec(struct SSMHANDLE * pSSM, uint32_t u32Version);
1490 void vhwaSaveExec(struct SSMHANDLE * pSSM);
1491private:
1492 void makeCurrent()
1493 {
1494 if(!mGlCurrent)
1495 {
1496 mGlCurrent = true;
1497 mpOverlayWidget->makeCurrent();
1498 }
1499 }
1500
1501 void performDisplayOverlay()
1502 {
1503 if(mOverlayVisible)
1504 {
1505#if 0
1506 mpOverlayWidget->updateGL();
1507#else
1508 makeCurrent();
1509 mpOverlayWidget->performDisplay();
1510 mpOverlayWidget->swapBuffers();
1511#endif
1512 }
1513 }
1514
1515 void vboxOpExit()
1516 {
1517 performDisplayOverlay();
1518 mGlCurrent = false;
1519 }
1520
1521
1522 void vboxSetGlOn(bool on);
1523 bool vboxGetGlOn() { return mGlOn; }
1524 void vboxSynchGl();
1525 void vboxDoVHWACmdExec(void *cmd);
1526 void vboxShowOverlay(bool show);
1527 void vboxDoCheckUpdateViewport();
1528 void vboxDoVHWACmd(void *cmd);
1529 void vboxDoUpdateRect(const QRect * pRect);
1530// void vboxUpdateOverlayPosition(const QPoint & pos);
1531 void vboxCheckUpdateOverlay(const QRect & rect);
1532 VBoxVHWACommandElement * processCmdList(VBoxVHWACommandElement * pCmd);
1533
1534 int vhwaConstruct(struct _VBOXVHWACMD_HH_CONSTRUCT *pCmd);
1535
1536 VBoxGLWidget *mpOverlayWidget;
1537 bool mGlOn;
1538 bool mOverlayWidgetVisible;
1539 bool mOverlayVisible;
1540 bool mGlCurrent;
1541 bool mProcessingCommands;
1542 QRect mOverlayViewport;
1543 VBoxVHWADirtyRect mMainDirtyRect;
1544
1545 VBoxVHWACommandElementProcessor mCmdPipe;
1546
1547 /* this is used in saved state restore to postpone surface restoration
1548 * till the framebuffer size is restored */
1549 VHWACommandList mOnResizeCmdList;
1550};
1551#endif
1552
1553#endif
1554
1555/////////////////////////////////////////////////////////////////////////////
1556
1557#if defined (VBOX_GUI_USE_SDL)
1558
1559class VBoxSDLFrameBuffer : public VBoxFrameBuffer
1560{
1561public:
1562
1563 VBoxSDLFrameBuffer (VBoxConsoleView *aView);
1564 virtual ~VBoxSDLFrameBuffer();
1565
1566 STDMETHOD(NotifyUpdate) (ULONG aX, ULONG aY,
1567 ULONG aW, ULONG aH);
1568
1569 uchar *address()
1570 {
1571 SDL_Surface *surf = mSurfVRAM ? mSurfVRAM : mScreen;
1572 return surf ? (uchar *) (uintptr_t) surf->pixels : 0;
1573 }
1574
1575 ulong bitsPerPixel()
1576 {
1577 SDL_Surface *surf = mSurfVRAM ? mSurfVRAM : mScreen;
1578 return surf ? surf->format->BitsPerPixel : 0;
1579 }
1580
1581 ulong bytesPerLine()
1582 {
1583 SDL_Surface *surf = mSurfVRAM ? mSurfVRAM : mScreen;
1584 return surf ? surf->pitch : 0;
1585 }
1586
1587 ulong pixelFormat()
1588 {
1589 return mPixelFormat;
1590 }
1591
1592 bool usesGuestVRAM()
1593 {
1594 return mSurfVRAM != NULL;
1595 }
1596
1597 void paintEvent (QPaintEvent *pe);
1598 void resizeEvent (VBoxResizeEvent *re);
1599
1600private:
1601
1602 SDL_Surface *mScreen;
1603 SDL_Surface *mSurfVRAM;
1604
1605 ulong mPixelFormat;
1606};
1607
1608#endif
1609
1610/////////////////////////////////////////////////////////////////////////////
1611
1612#if defined (VBOX_GUI_USE_DDRAW)
1613
1614class VBoxDDRAWFrameBuffer : public VBoxFrameBuffer
1615{
1616public:
1617
1618 VBoxDDRAWFrameBuffer (VBoxConsoleView *aView);
1619 virtual ~VBoxDDRAWFrameBuffer();
1620
1621 STDMETHOD(NotifyUpdate) (ULONG aX, ULONG aY,
1622 ULONG aW, ULONG aH);
1623
1624 uchar *address() { return (uchar *) mSurfaceDesc.lpSurface; }
1625 ulong bitsPerPixel() { return mSurfaceDesc.ddpfPixelFormat.dwRGBBitCount; }
1626 ulong bytesPerLine() { return (ulong) mSurfaceDesc.lPitch; }
1627
1628 ulong pixelFormat() { return mPixelFormat; };
1629
1630 bool usesGuestVRAM() { return mUsesGuestVRAM; }
1631
1632 void paintEvent (QPaintEvent *pe);
1633 void resizeEvent (VBoxResizeEvent *re);
1634 void moveEvent (QMoveEvent *me);
1635
1636private:
1637
1638 void releaseObjects();
1639
1640 bool createSurface (ULONG aPixelFormat, uchar *pvVRAM,
1641 ULONG aBitsPerPixel, ULONG aBytesPerLine,
1642 ULONG aWidth, ULONG aHeight);
1643 void deleteSurface();
1644 void drawRect (ULONG x, ULONG y, ULONG w, ULONG h);
1645 void getWindowPosition (void);
1646
1647 LPDIRECTDRAW7 mDDRAW;
1648 LPDIRECTDRAWCLIPPER mClipper;
1649 LPDIRECTDRAWSURFACE7 mSurface;
1650 DDSURFACEDESC2 mSurfaceDesc;
1651 LPDIRECTDRAWSURFACE7 mPrimarySurface;
1652
1653 ulong mPixelFormat;
1654
1655 bool mUsesGuestVRAM;
1656
1657 int mWndX;
1658 int mWndY;
1659
1660 bool mSynchronousUpdates;
1661};
1662
1663#endif
1664
1665/////////////////////////////////////////////////////////////////////////////
1666
1667#if defined (Q_WS_MAC) && defined (VBOX_GUI_USE_QUARTZ2D)
1668
1669#include <Carbon/Carbon.h>
1670
1671class VBoxQuartz2DFrameBuffer : public VBoxFrameBuffer
1672{
1673public:
1674
1675 VBoxQuartz2DFrameBuffer (VBoxConsoleView *aView);
1676 virtual ~VBoxQuartz2DFrameBuffer ();
1677
1678 STDMETHOD (NotifyUpdate) (ULONG aX, ULONG aY,
1679 ULONG aW, ULONG aH);
1680 STDMETHOD (SetVisibleRegion) (BYTE *aRectangles, ULONG aCount);
1681
1682 uchar *address() { return mDataAddress; }
1683 ulong bitsPerPixel() { return CGImageGetBitsPerPixel (mImage); }
1684 ulong bytesPerLine() { return CGImageGetBytesPerRow (mImage); }
1685 ulong pixelFormat() { return mPixelFormat; };
1686 bool usesGuestVRAM() { return mBitmapData == NULL; }
1687
1688 const CGImageRef imageRef() const { return mImage; }
1689
1690 void paintEvent (QPaintEvent *pe);
1691 void resizeEvent (VBoxResizeEvent *re);
1692
1693private:
1694
1695 void clean();
1696
1697 uchar *mDataAddress;
1698 void *mBitmapData;
1699 ulong mPixelFormat;
1700 CGImageRef mImage;
1701 typedef struct
1702 {
1703 /** The size of this structure expressed in rcts entries. */
1704 ULONG allocated;
1705 /** The number of entries in the rcts array. */
1706 ULONG used;
1707 /** Variable sized array of the rectangle that makes up the region. */
1708 CGRect rcts[1];
1709 } RegionRects;
1710 /** The current valid region, all access is by atomic cmpxchg or atomic xchg.
1711 *
1712 * The protocol for updating and using this has to take into account that
1713 * the producer (SetVisibleRegion) and consumer (paintEvent) are running
1714 * on different threads. Therefore the producer will create a new RegionRects
1715 * structure before atomically replace the existing one. While the consumer
1716 * will read the value by atomically replace it by NULL, and then when its
1717 * done try restore it by cmpxchg. If the producer has already put a new
1718 * region there, it will be discarded (see mRegionUnused).
1719 */
1720 RegionRects volatile *mRegion;
1721 /** For keeping the unused region and thus avoid some RTMemAlloc/RTMemFree calls.
1722 * This is operated with atomic cmpxchg and atomic xchg. */
1723 RegionRects volatile *mRegionUnused;
1724};
1725
1726#endif /* Q_WS_MAC && VBOX_GUI_USE_QUARTZ2D */
1727
1728#endif // !___VBoxFrameBuffer_h___
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette