VirtualBox

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

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

video 2d accel: next step in using ogl only when overlay is enabled; bug-fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 45.5 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;
913
914class VBoxVHWASurfList
915{
916public:
917
918 VBoxVHWASurfList() : mCurrent(NULL) {}
919 void add(VBoxVHWASurfaceBase *pSurf)
920 {
921 VBoxVHWASurfList * pOld = pSurf->getComplexList();
922 if(pOld)
923 {
924 pOld->remove(pSurf);
925 }
926 mSurfaces.push_back(pSurf);
927 pSurf->setComplexList(this);
928 }
929
930 void clear()
931 {
932 for (SurfList::iterator it = mSurfaces.begin();
933 it != mSurfaces.end(); ++ it)
934 {
935 (*it)->setComplexList(NULL);
936 }
937 mSurfaces.clear();
938 mCurrent = NULL;
939 }
940
941 size_t size() {return mSurfaces.size(); }
942
943 void remove(VBoxVHWASurfaceBase *pSurf)
944 {
945 mSurfaces.remove(pSurf);
946 pSurf->setComplexList(NULL);
947 if(mCurrent == pSurf)
948 mCurrent = NULL;
949 }
950
951 bool empty() { return mSurfaces.empty(); }
952
953 void setCurrentVisible(VBoxVHWASurfaceBase *pSurf)
954 {
955 mCurrent = pSurf;
956 }
957
958 VBoxVHWASurfaceBase * current() { return mCurrent; }
959 const SurfList & surfaces() const {return mSurfaces;}
960
961private:
962
963 SurfList mSurfaces;
964 VBoxVHWASurfaceBase* mCurrent;
965};
966
967class VBoxVHWADisplay
968{
969public:
970 VBoxVHWADisplay() :
971 mSurfVGA(NULL)
972// ,
973// mSurfPrimary(NULL)
974 {}
975
976 VBoxVHWASurfaceBase * setVGA(VBoxVHWASurfaceBase * pVga)
977 {
978 VBoxVHWASurfaceBase * old = mSurfVGA;
979 mSurfVGA = pVga;
980 mPrimary.clear();
981 if(pVga)
982 {
983 Assert(!pVga->getComplexList());
984 mPrimary.add(pVga);
985 mPrimary.setCurrentVisible(pVga);
986 }
987// mSurfPrimary = pVga;
988 mOverlays.clear();
989 return old;
990 }
991
992 VBoxVHWASurfaceBase * updateVGA(VBoxVHWASurfaceBase * pVga)
993 {
994 VBoxVHWASurfaceBase * old = mSurfVGA;
995 Assert(old);
996 mSurfVGA = pVga;
997 return old;
998 }
999
1000 VBoxVHWASurfaceBase * getVGA()
1001 {
1002 return mSurfVGA;
1003 }
1004
1005 VBoxVHWASurfaceBase * getPrimary()
1006 {
1007 return mPrimary.current();
1008 }
1009
1010 void addOverlay(VBoxVHWASurfList * pSurf)
1011 {
1012 mOverlays.push_back(pSurf);
1013 }
1014
1015 void checkAddOverlay(VBoxVHWASurfList * pSurf)
1016 {
1017 if(!hasOverlay(pSurf))
1018 addOverlay(pSurf);
1019 }
1020
1021 bool hasOverlay(VBoxVHWASurfList * pSurf)
1022 {
1023 for (OverlayList::iterator it = mOverlays.begin();
1024 it != mOverlays.end(); ++ it)
1025 {
1026 if((*it) == pSurf)
1027 {
1028 return true;
1029 }
1030 }
1031 return false;
1032 }
1033
1034 void removeOverlay(VBoxVHWASurfList * pSurf)
1035 {
1036 mOverlays.remove(pSurf);
1037 }
1038
1039 void performDisplay()
1040 {
1041 VBoxVHWASurfaceBase * pPrimary = mPrimary.current();
1042 pPrimary->performDisplay(NULL);
1043
1044 for (OverlayList::const_iterator it = mOverlays.begin();
1045 it != mOverlays.end(); ++ it)
1046 {
1047 VBoxVHWASurfaceBase * pOverlay = (*it)->current();
1048 if(pOverlay)
1049 {
1050 pOverlay->performDisplay(pPrimary);
1051 }
1052 }
1053 }
1054
1055 const OverlayList & overlays() const {return mOverlays;}
1056 const VBoxVHWASurfList & primaries() const { return mPrimary; }
1057
1058private:
1059 VBoxVHWASurfaceBase *mSurfVGA;
1060 VBoxVHWASurfList mPrimary;
1061
1062 OverlayList mOverlays;
1063};
1064
1065typedef void (VBoxGLWidget::*PFNVBOXQGLOP)(void* );
1066
1067typedef enum
1068{
1069 VBOXVHWA_PIPECMD_PAINT = 1,
1070 VBOXVHWA_PIPECMD_VHWA,
1071 VBOXVHWA_PIPECMD_OP,
1072}VBOXVHWA_PIPECMD_TYPE;
1073
1074typedef struct VBOXVHWACALLBACKINFO
1075{
1076 VBoxGLWidget *pThis;
1077 PFNVBOXQGLOP pfnCallback;
1078 void * pContext;
1079}VBOXVHWACALLBACKINFO;
1080
1081class VBoxVHWACommandElement
1082{
1083public:
1084 void setVHWACmd(struct _VBOXVHWACMD * pCmd)
1085 {
1086 mType = VBOXVHWA_PIPECMD_VHWA;
1087 u.mpCmd = pCmd;
1088 }
1089
1090 void setPaintCmd(const QRect & aRect)
1091 {
1092 mType = VBOXVHWA_PIPECMD_PAINT;
1093 mRect = aRect;
1094 }
1095
1096 void setOp(const VBOXVHWACALLBACKINFO & aOp)
1097 {
1098 mType = VBOXVHWA_PIPECMD_OP;
1099 u.mCallback = aOp;
1100 }
1101
1102 void setData(VBOXVHWA_PIPECMD_TYPE aType, void * pvData)
1103 {
1104 switch(aType)
1105 {
1106 case VBOXVHWA_PIPECMD_PAINT:
1107 setPaintCmd(*((QRect*)pvData));
1108 break;
1109 case VBOXVHWA_PIPECMD_VHWA:
1110 setVHWACmd((struct _VBOXVHWACMD *)pvData);
1111 break;
1112 case VBOXVHWA_PIPECMD_OP:
1113 setOp(*((VBOXVHWACALLBACKINFO *)pvData));
1114 break;
1115 default:
1116 Assert(0);
1117 break;
1118 }
1119 }
1120
1121 VBOXVHWA_PIPECMD_TYPE type() const {return mType;}
1122 const QRect & rect() const {return mRect;}
1123 struct _VBOXVHWACMD * vhwaCmd() const {return u.mpCmd;}
1124 const VBOXVHWACALLBACKINFO & op() const {return u.mCallback; }
1125
1126 VBoxVHWACommandElement * mpNext;
1127private:
1128 VBOXVHWA_PIPECMD_TYPE mType;
1129 union
1130 {
1131 struct _VBOXVHWACMD * mpCmd;
1132 VBOXVHWACALLBACKINFO mCallback;
1133 }u;
1134 QRect mRect;
1135};
1136
1137class VBoxVHWACommandElementPipe
1138{
1139public:
1140 VBoxVHWACommandElementPipe() :
1141 mpFirst(NULL),
1142 mpLast(NULL)
1143 {}
1144
1145 void put(VBoxVHWACommandElement *pCmd)
1146 {
1147 if(mpLast)
1148 {
1149 Assert(mpFirst);
1150 mpLast->mpNext = pCmd;
1151 mpLast = pCmd;
1152 }
1153 else
1154 {
1155 Assert(!mpFirst);
1156 mpFirst = pCmd;
1157 mpLast = pCmd;
1158 }
1159 pCmd->mpNext= NULL;
1160
1161 }
1162
1163 VBoxVHWACommandElement * detachList()
1164 {
1165 if(mpLast)
1166 {
1167 VBoxVHWACommandElement * pHead = mpFirst;
1168 mpFirst = NULL;
1169 mpLast = NULL;
1170 return pHead;
1171 }
1172 return NULL;
1173 }
1174private:
1175 VBoxVHWACommandElement *mpFirst;
1176 VBoxVHWACommandElement *mpLast;
1177};
1178
1179class VBoxVHWACommandElementStack
1180{
1181public:
1182 VBoxVHWACommandElementStack() :
1183 mpFirst(NULL) {}
1184
1185 void push(VBoxVHWACommandElement *pCmd)
1186 {
1187 pCmd->mpNext = mpFirst;
1188 mpFirst = pCmd;
1189 }
1190
1191 void pusha(VBoxVHWACommandElement *pFirst, VBoxVHWACommandElement *pLast)
1192 {
1193 pLast->mpNext = mpFirst;
1194 mpFirst = pFirst;
1195 }
1196
1197 VBoxVHWACommandElement * pop()
1198 {
1199 if(mpFirst)
1200 {
1201 VBoxVHWACommandElement * ret = mpFirst;
1202 mpFirst = ret->mpNext;
1203 return ret;
1204 }
1205 return NULL;
1206 }
1207private:
1208 VBoxVHWACommandElement *mpFirst;
1209};
1210
1211class VBoxVHWACommandElementProcessor
1212{
1213public:
1214 VBoxVHWACommandElementProcessor(VBoxConsoleView *aView);
1215 ~VBoxVHWACommandElementProcessor();
1216 void postCmd(VBOXVHWA_PIPECMD_TYPE aType, void * pvData);
1217 class VBoxVHWACommandElement * detachCmdList(class VBoxVHWACommandElement * pFirst2Free, VBoxVHWACommandElement * pLast2Free);
1218
1219private:
1220 RTCRITSECT mCritSect;
1221 class VBoxVHWACommandProcessEvent *mpFirstEvent;
1222 class VBoxVHWACommandProcessEvent *mpLastEvent;
1223 VBoxConsoleView *mView;
1224 bool mbNewEvent;
1225 VBoxVHWACommandElementStack mFreeElements;
1226 VBoxVHWACommandElement mElementsBuffer[2048];
1227};
1228
1229
1230class VBoxGLWidget : public QGLWidget
1231{
1232public:
1233 VBoxGLWidget (VBoxConsoleView *aView, QWidget *aParent);
1234 ~VBoxGLWidget();
1235
1236 ulong vboxPixelFormat() { return mPixelFormat; }
1237 bool vboxUsesGuestVRAM() { return mUsesGuestVRAM; }
1238
1239 uchar *vboxAddress() { return mDisplay.getVGA() ? mDisplay.getVGA()->address() : NULL; }
1240
1241#ifdef VBOX_WITH_VIDEOHWACCEL
1242 uchar *vboxVRAMAddressFromOffset(uint64_t offset);
1243 uint64_t vboxVRAMOffsetFromAddress(uchar* addr);
1244 uint64_t vboxVRAMOffset(VBoxVHWASurfaceBase * pSurf);
1245
1246 void vhwaSaveExec(struct SSMHANDLE * pSSM);
1247 int vhwaLoadExec(struct SSMHANDLE * pSSM, uint32_t u32Version);
1248
1249 int vhwaSurfaceCanCreate(struct _VBOXVHWACMD_SURF_CANCREATE *pCmd);
1250 int vhwaSurfaceCreate(struct _VBOXVHWACMD_SURF_CREATE *pCmd);
1251 int vhwaSurfaceDestroy(struct _VBOXVHWACMD_SURF_DESTROY *pCmd);
1252 int vhwaSurfaceLock(struct _VBOXVHWACMD_SURF_LOCK *pCmd);
1253 int vhwaSurfaceUnlock(struct _VBOXVHWACMD_SURF_UNLOCK *pCmd);
1254 int vhwaSurfaceBlt(struct _VBOXVHWACMD_SURF_BLT *pCmd);
1255 int vhwaSurfaceFlip(struct _VBOXVHWACMD_SURF_FLIP *pCmd);
1256 int vhwaSurfaceOverlayUpdate(struct _VBOXVHWACMD_SURF_OVERLAY_UPDATE *pCmf);
1257 int vhwaSurfaceOverlaySetPosition(struct _VBOXVHWACMD_SURF_OVERLAY_SETPOSITION *pCmd);
1258 int vhwaSurfaceColorkeySet(struct _VBOXVHWACMD_SURF_COLORKEY_SET *pCmd);
1259 int vhwaQueryInfo1(struct _VBOXVHWACMD_QUERYINFO1 *pCmd);
1260 int vhwaQueryInfo2(struct _VBOXVHWACMD_QUERYINFO2 *pCmd);
1261 int vhwaConstruct(struct _VBOXVHWACMD_HH_CONSTRUCT *pCmd);
1262
1263 bool hasSurfaces() const;
1264 bool hasVisibleOverlays();
1265 const QRect & overlaysRectUnion();
1266#endif
1267
1268 ulong vboxBitsPerPixel() { return mDisplay.getVGA()->bitsPerPixel(); }
1269 ulong vboxBytesPerLine() { return mDisplay.getVGA() ? mDisplay.getVGA()->bytesPerLine() : 0; }
1270
1271// void vboxPaintEvent (QPaintEvent *pe) {vboxPerformGLOp(&VBoxGLWidget::vboxDoPaint, pe); }
1272 void vboxResizeEvent (VBoxResizeEvent *re) {vboxPerformGLOp(&VBoxGLWidget::vboxDoResize, re); }
1273
1274 void vboxProcessVHWACommands(class VBoxVHWACommandElementProcessor * pPipe) {vboxPerformGLOp(&VBoxGLWidget::vboxDoProcessVHWACommands, pPipe);}
1275#ifdef VBOX_WITH_VIDEOHWACCEL
1276 void vboxVHWACmd (struct _VBOXVHWACMD * pCmd) {vboxPerformGLOp(&VBoxGLWidget::vboxDoVHWACmd, pCmd);}
1277#endif
1278 class VBoxVHWAGlProgramMngr * vboxVHWAGetGlProgramMngr() { return mpMngr; }
1279
1280 VBoxVHWASurfaceBase * vboxGetVGASurface() { return mDisplay.getVGA(); }
1281
1282 static void doSetupMatrix(const QSize & aSize, bool bInverted);
1283
1284 void vboxDoUpdateViewport(const QRect * pRect);
1285 const QRect & vboxViewport() const {return mViewport;}
1286
1287 void performDisplay() { mDisplay.performDisplay(); }
1288protected:
1289
1290 void paintGL()
1291 {
1292 if(mpfnOp)
1293 {
1294 (this->*mpfnOp)(mOpContext);
1295 mpfnOp = NULL;
1296 }
1297// else
1298// {
1299 mDisplay.performDisplay();
1300// }
1301 }
1302
1303 void initializeGL();
1304
1305private:
1306 static void setupMatricies(const QSize &display);
1307 static void adjustViewport(const QSize &display, const QRect &viewport);
1308 void vboxDoResize(void *re);
1309// void vboxDoPaint(void *rec);
1310
1311
1312 void vboxDoUpdateRect(const QRect * pRect);
1313#ifdef VBOXQGL_DBG_SURF
1314 void vboxDoTestSurfaces(void *context);
1315#endif
1316#ifdef VBOX_WITH_VIDEOHWACCEL
1317 void vboxDoVHWACmdExec(void *cmd);
1318 void vboxDoVHWACmdAndFree(void *cmd);
1319 void vboxDoVHWACmd(void *cmd);
1320
1321 void vboxCheckUpdateAddress (VBoxVHWASurfaceBase * pSurface, uint64_t offset)
1322 {
1323 if (pSurface->addressAlocated())
1324 {
1325 uchar * addr = vboxVRAMAddressFromOffset(offset);
1326 if(addr)
1327 {
1328 pSurface->setAddress(addr);
1329 }
1330 }
1331 }
1332
1333 int vhwaSaveSurface(struct SSMHANDLE * pSSM, VBoxVHWASurfaceBase *pSurf, uint32_t surfCaps);
1334 int vhwaLoadSurface(struct SSMHANDLE * pSSM, uint32_t u32Version);
1335 int vhwaSaveOverlayData(struct SSMHANDLE * pSSM, VBoxVHWASurfaceBase *pSurf, bool bVisible);
1336 int vhwaLoadOverlayData(struct SSMHANDLE * pSSM, uint32_t u32Version);
1337 void vhwaDoSurfaceOverlayUpdate(VBoxVHWASurfaceBase *pDstSurf, VBoxVHWASurfaceBase *pSrcSurf, struct _VBOXVHWACMD_SURF_OVERLAY_UPDATE *pCmd);
1338#endif
1339 static const QGLFormat & vboxGLFormat();
1340
1341 VBoxVHWADisplay mDisplay;
1342
1343
1344 /* we do all opengl stuff in the paintGL context,
1345 * submit the operation to be performed
1346 * @todo: could be moved outside the updateGL */
1347 void vboxPerformGLOp(PFNVBOXQGLOP pfn, void* pContext)
1348 {
1349 mpfnOp = pfn;
1350 mOpContext = pContext;
1351 updateGL();
1352 }
1353
1354// /* posts op to UI thread */
1355// int vboxExecOpSynch(PFNVBOXQGLOP pfn, void* pContext);
1356 void vboxExecOnResize(PFNVBOXQGLOP pfn, void* pContext);
1357
1358 void vboxDoProcessVHWACommands(void *pContext);
1359
1360 class VBoxVHWACommandElement * processCmdList(class VBoxVHWACommandElement * pCmd);
1361
1362 VBoxVHWASurfaceBase* handle2Surface(uint32_t h)
1363 {
1364 VBoxVHWASurfaceBase* pSurf = (VBoxVHWASurfaceBase*)mSurfHandleTable.get(h);
1365 Assert(pSurf);
1366 return pSurf;
1367 }
1368
1369 VBoxVHWAHandleTable mSurfHandleTable;
1370
1371 PFNVBOXQGLOP mpfnOp;
1372 void *mOpContext;
1373
1374 ulong mPixelFormat;
1375 bool mUsesGuestVRAM;
1376// bool mbVGASurfCreated;
1377 QRect mViewport;
1378
1379 VBoxConsoleView *mView;
1380
1381 VBoxVHWASurfList *mConstructingList;
1382 int32_t mcRemaining2Contruct;
1383
1384 /* this is used in saved state restore to postpone surface restoration
1385 * till the framebuffer size is restored */
1386 VBoxVHWACommandElementPipe mResizePostProcessCmds;
1387
1388 class VBoxVHWAGlProgramMngr *mpMngr;
1389};
1390
1391
1392class VBoxQGLFrameBuffer : public VBoxFrameBuffer
1393{
1394public:
1395
1396 VBoxQGLFrameBuffer (VBoxConsoleView *aView);
1397
1398 STDMETHOD(NotifyUpdate) (ULONG aX, ULONG aY,
1399 ULONG aW, ULONG aH);
1400#ifdef VBOXQGL_PROF_BASE
1401 STDMETHOD(RequestResize) (ULONG aScreenId, ULONG aPixelFormat,
1402 BYTE *aVRAM, ULONG aBitsPerPixel, ULONG aBytesPerLine,
1403 ULONG aWidth, ULONG aHeight,
1404 BOOL *aFinished);
1405#endif
1406
1407#ifdef VBOX_WITH_VIDEOHWACCEL
1408 STDMETHOD(ProcessVHWACommand)(BYTE *pCommand);
1409
1410
1411 static bool isAcceleration2DVideoAvailable();
1412#endif
1413
1414 ulong pixelFormat() { return vboxWidget()->vboxPixelFormat(); }
1415 bool usesGuestVRAM() { return vboxWidget()->vboxUsesGuestVRAM(); }
1416
1417 uchar *address() { return vboxWidget()->vboxAddress(); }
1418 ulong bitsPerPixel() { return vboxWidget()->vboxBitsPerPixel(); }
1419 ulong bytesPerLine() { return vboxWidget()->vboxBytesPerLine(); }
1420
1421 void paintEvent (QPaintEvent *pe);
1422 void resizeEvent (VBoxResizeEvent *re);
1423 void doProcessVHWACommand(QEvent * pEvent);
1424
1425private:
1426// void vboxMakeCurrent();
1427 VBoxGLWidget * vboxWidget();
1428
1429 VBoxVHWACommandElementProcessor mCmdPipe;
1430};
1431
1432#ifdef VBOX_WITH_VIDEOHWACCEL
1433class VBoxQGLOverlayFrameBuffer : public VBoxQImageFrameBuffer
1434{
1435public:
1436 VBoxQGLOverlayFrameBuffer (VBoxConsoleView *aView);
1437
1438
1439 STDMETHOD(ProcessVHWACommand)(BYTE *pCommand);
1440
1441 void doProcessVHWACommand(QEvent * pEvent);
1442
1443 STDMETHOD(NotifyUpdate) (ULONG aX, ULONG aY,
1444 ULONG aW, ULONG aH);
1445
1446 void paintEvent (QPaintEvent *pe);
1447 void resizeEvent (VBoxResizeEvent *re);
1448
1449 void vboxUpdateRect(const QRect * pRect);
1450private:
1451 void vboxSetGlOn(bool on);
1452 bool vboxGetGlOn() { return mGlOn; }
1453 void vboxSynchGl();
1454 void vboxDoVHWACmdExec(void *cmd);
1455 void vboxShowOverlay(bool show);
1456 void vboxUpdateOverlayPosition(const QPoint & pos);
1457 void vboxUpdateOverlay(const QRect & rect, bool show);
1458 VBoxVHWACommandElement * processCmdList(VBoxVHWACommandElement * pCmd);
1459 VBoxGLWidget *mpOverlayWidget;
1460 bool mGlOn;
1461 bool mOverlayVisible;
1462 VBoxVHWADirtyRect mMainDirtyRect;
1463
1464 VBoxVHWACommandElementProcessor mCmdPipe;
1465};
1466#endif
1467
1468#endif
1469
1470/////////////////////////////////////////////////////////////////////////////
1471
1472#if defined (VBOX_GUI_USE_SDL)
1473
1474class VBoxSDLFrameBuffer : public VBoxFrameBuffer
1475{
1476public:
1477
1478 VBoxSDLFrameBuffer (VBoxConsoleView *aView);
1479 virtual ~VBoxSDLFrameBuffer();
1480
1481 STDMETHOD(NotifyUpdate) (ULONG aX, ULONG aY,
1482 ULONG aW, ULONG aH);
1483
1484 uchar *address()
1485 {
1486 SDL_Surface *surf = mSurfVRAM ? mSurfVRAM : mScreen;
1487 return surf ? (uchar *) (uintptr_t) surf->pixels : 0;
1488 }
1489
1490 ulong bitsPerPixel()
1491 {
1492 SDL_Surface *surf = mSurfVRAM ? mSurfVRAM : mScreen;
1493 return surf ? surf->format->BitsPerPixel : 0;
1494 }
1495
1496 ulong bytesPerLine()
1497 {
1498 SDL_Surface *surf = mSurfVRAM ? mSurfVRAM : mScreen;
1499 return surf ? surf->pitch : 0;
1500 }
1501
1502 ulong pixelFormat()
1503 {
1504 return mPixelFormat;
1505 }
1506
1507 bool usesGuestVRAM()
1508 {
1509 return mSurfVRAM != NULL;
1510 }
1511
1512 void paintEvent (QPaintEvent *pe);
1513 void resizeEvent (VBoxResizeEvent *re);
1514
1515private:
1516
1517 SDL_Surface *mScreen;
1518 SDL_Surface *mSurfVRAM;
1519
1520 ulong mPixelFormat;
1521};
1522
1523#endif
1524
1525/////////////////////////////////////////////////////////////////////////////
1526
1527#if defined (VBOX_GUI_USE_DDRAW)
1528
1529class VBoxDDRAWFrameBuffer : public VBoxFrameBuffer
1530{
1531public:
1532
1533 VBoxDDRAWFrameBuffer (VBoxConsoleView *aView);
1534 virtual ~VBoxDDRAWFrameBuffer();
1535
1536 STDMETHOD(NotifyUpdate) (ULONG aX, ULONG aY,
1537 ULONG aW, ULONG aH);
1538
1539 uchar *address() { return (uchar *) mSurfaceDesc.lpSurface; }
1540 ulong bitsPerPixel() { return mSurfaceDesc.ddpfPixelFormat.dwRGBBitCount; }
1541 ulong bytesPerLine() { return (ulong) mSurfaceDesc.lPitch; }
1542
1543 ulong pixelFormat() { return mPixelFormat; };
1544
1545 bool usesGuestVRAM() { return mUsesGuestVRAM; }
1546
1547 void paintEvent (QPaintEvent *pe);
1548 void resizeEvent (VBoxResizeEvent *re);
1549 void moveEvent (QMoveEvent *me);
1550
1551private:
1552
1553 void releaseObjects();
1554
1555 bool createSurface (ULONG aPixelFormat, uchar *pvVRAM,
1556 ULONG aBitsPerPixel, ULONG aBytesPerLine,
1557 ULONG aWidth, ULONG aHeight);
1558 void deleteSurface();
1559 void drawRect (ULONG x, ULONG y, ULONG w, ULONG h);
1560 void getWindowPosition (void);
1561
1562 LPDIRECTDRAW7 mDDRAW;
1563 LPDIRECTDRAWCLIPPER mClipper;
1564 LPDIRECTDRAWSURFACE7 mSurface;
1565 DDSURFACEDESC2 mSurfaceDesc;
1566 LPDIRECTDRAWSURFACE7 mPrimarySurface;
1567
1568 ulong mPixelFormat;
1569
1570 bool mUsesGuestVRAM;
1571
1572 int mWndX;
1573 int mWndY;
1574
1575 bool mSynchronousUpdates;
1576};
1577
1578#endif
1579
1580/////////////////////////////////////////////////////////////////////////////
1581
1582#if defined (Q_WS_MAC) && defined (VBOX_GUI_USE_QUARTZ2D)
1583
1584#include <Carbon/Carbon.h>
1585
1586class VBoxQuartz2DFrameBuffer : public VBoxFrameBuffer
1587{
1588public:
1589
1590 VBoxQuartz2DFrameBuffer (VBoxConsoleView *aView);
1591 virtual ~VBoxQuartz2DFrameBuffer ();
1592
1593 STDMETHOD (NotifyUpdate) (ULONG aX, ULONG aY,
1594 ULONG aW, ULONG aH);
1595 STDMETHOD (SetVisibleRegion) (BYTE *aRectangles, ULONG aCount);
1596
1597 uchar *address() { return mDataAddress; }
1598 ulong bitsPerPixel() { return CGImageGetBitsPerPixel (mImage); }
1599 ulong bytesPerLine() { return CGImageGetBytesPerRow (mImage); }
1600 ulong pixelFormat() { return mPixelFormat; };
1601 bool usesGuestVRAM() { return mBitmapData == NULL; }
1602
1603 const CGImageRef imageRef() const { return mImage; }
1604
1605 void paintEvent (QPaintEvent *pe);
1606 void resizeEvent (VBoxResizeEvent *re);
1607
1608private:
1609
1610 void clean();
1611
1612 uchar *mDataAddress;
1613 void *mBitmapData;
1614 ulong mPixelFormat;
1615 CGImageRef mImage;
1616 typedef struct
1617 {
1618 /** The size of this structure expressed in rcts entries. */
1619 ULONG allocated;
1620 /** The number of entries in the rcts array. */
1621 ULONG used;
1622 /** Variable sized array of the rectangle that makes up the region. */
1623 CGRect rcts[1];
1624 } RegionRects;
1625 /** The current valid region, all access is by atomic cmpxchg or atomic xchg.
1626 *
1627 * The protocol for updating and using this has to take into account that
1628 * the producer (SetVisibleRegion) and consumer (paintEvent) are running
1629 * on different threads. Therefore the producer will create a new RegionRects
1630 * structure before atomically replace the existing one. While the consumer
1631 * will read the value by atomically replace it by NULL, and then when its
1632 * done try restore it by cmpxchg. If the producer has already put a new
1633 * region there, it will be discarded (see mRegionUnused).
1634 */
1635 RegionRects volatile *mRegion;
1636 /** For keeping the unused region and thus avoid some RTMemAlloc/RTMemFree calls.
1637 * This is operated with atomic cmpxchg and atomic xchg. */
1638 RegionRects volatile *mRegionUnused;
1639};
1640
1641#endif /* Q_WS_MAC && VBOX_GUI_USE_QUARTZ2D */
1642
1643#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