Index: /trunk/src/VBox/Frontends/VirtualBox/src/VBoxFBOverlay.cpp
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/VBoxFBOverlay.cpp	(revision 25256)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/VBoxFBOverlay.cpp	(revision 25257)
@@ -341,10 +341,6 @@
     {}
 
-//    virtual ~VBoxVHWAGlShaderComponent();
-
 
     int init();
-//    virtual int initUniforms(class VBoxVHWAGlProgram * pProgram){}
-//    void uninit();
 
     const char * contents() { return mSource.constData(); }
@@ -359,5 +355,4 @@
 int VBoxVHWAGlShaderComponent::init()
 {
-//    Assert(!isInitialized());
     if(isInitialized())
         return VINF_ALREADY_INITIALIZED;
@@ -1817,5 +1812,4 @@
     if(buf)
     {
-    //  updateBuffer((uchar*)buf, &mRect);
         memcpy(buf, mAddress, memSize());
 
@@ -1827,237 +1821,98 @@
 }
 
-#if 0
-void VBoxVHWASurfaceBase::synch(const QRect * aRect)
-{
-    synchFB(aRect);
-    synchTex(aRect);
-    synchMem(aRect);
-}
-
-void VBoxVHWASurfaceBase::synchFB(const QRect * pRect)
-{
-    Assert(isYInverted());
-
-    if(pRect)
-    {
-        Assert(mRect.contains(*pRect));
-    }
-
-    synchTexMem(pRect);
-
-    if(mUpdateTex2FBRect.isClear())
-        return;
-
-    if(pRect && !mUpdateTex2FBRect.rect().intersects(*pRect))
-        return;
-
-    mState->makeCurrent(this);
+uchar* VBoxVHWATextureNP2RectPBOMapped::mapAlignedBuffer()
+{
+    Assert(!mpMappedAllignedBuffer);
+    if(!mpMappedAllignedBuffer)
+    {
+        VBOXQGL_CHECKERR(
+                vboxglBindBuffer(GL_PIXEL_UNPACK_BUFFER, mPBO);
+            );
+
+        uchar* buf;
+        VBOXQGL_CHECKERR(
+                buf = (uchar*)vboxglMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_READ_WRITE);
+        );
+
+        Assert(buf);
+
+        VBOXQGL_CHECKERR(
+                vboxglBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
+            );
+
+        mpMappedAllignedBuffer = (uchar*)alignBuffer(buf);
+
+        mcbOffset = calcOffset(buf, mpMappedAllignedBuffer);
+    }
+    return mpMappedAllignedBuffer;
+}
+
+void   VBoxVHWATextureNP2RectPBOMapped::unmapBuffer()
+{
+    Assert(mpMappedAllignedBuffer);
+    if(mpMappedAllignedBuffer)
+    {
+        VBOXQGL_CHECKERR(
+                vboxglBindBuffer(GL_PIXEL_UNPACK_BUFFER, mPBO);
+        );
+
+        bool unmapped;
+        VBOXQGL_CHECKERR(
+                unmapped = vboxglUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
+                );
+
+        Assert(unmapped);
+
+        VBOXQGL_CHECKERR(
+                vboxglBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
+        );
+
+        mpMappedAllignedBuffer = NULL;
+    }
+}
+
+void VBoxVHWATextureNP2RectPBOMapped::load()
+{
+    VBoxVHWATextureNP2Rect::load();
 
     VBOXQGL_CHECKERR(
-            glBindTexture(GL_TEXTURE_2D, mTexture);
-            );
-
-    VBoxVHWAGlProgramMngr * pMngr = getGlProgramMngr();
-    pMngr->stopCurrentProgram();
-
-    doTex2FB(&mUpdateTex2FBRect.rect(), &mUpdateTex2FBRect.rect());
-
-    mUpdateTex2FBRect.clear();
-    Assert(mUpdateTex2FBRect.isClear());
-}
-
-void VBoxVHWASurfaceBase::synchMem(const QRect * pRect)
-{
-    if(pRect)
-    {
-        Assert(mRect.contains(*pRect));
-    }
-
-    if(mUpdateFB2MemRect.isClear())
-        return;
-
-    if(pRect && !mUpdateFB2MemRect.rect().intersects(*pRect))
-        return;
-
-    mState->makeYInvertedCurrent(this);
-//    mState->makeCurrent(this);
-
-    uchar * address = pointAddress(mUpdateFB2MemRect.rect().x(), mUpdateFB2MemRect.rect().y());
+            vboxglBindBuffer(GL_PIXEL_UNPACK_BUFFER, mPBO);
+        );
 
     VBOXQGL_CHECKERR(
-            glPixelStorei(GL_PACK_ROW_LENGTH, mRect.width());
-            );
+            vboxglBufferData(GL_PIXEL_UNPACK_BUFFER, mcbActualBufferSize, NULL, GL_STREAM_DRAW);
+        );
+
+    vboxglBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
+}
+
+void VBoxVHWATextureNP2RectPBOMapped::doUpdate(uchar * pAddress, const QRect * pRect)
+{
+    Q_UNUSED(pAddress);
+    Q_UNUSED(pRect);
+
     VBOXQGL_CHECKERR(
-            glReadPixels(
-                mUpdateFB2MemRect.rect().x(),
-                mUpdateFB2MemRect.rect().y(),
-                mUpdateFB2MemRect.rect().width(),
-                mUpdateFB2MemRect.rect().height(),
-                mColorFormat.format(),
-                mColorFormat.type(),
-                address);
-            );
-
-    mUpdateFB2MemRect.clear();
-    Assert(mUpdateFB2TexRect.isClear());
-}
-
-int VBoxVHWASurfaceBase::performBlt(const QRect * pDstRect, VBoxVHWASurfaceBase * pSrcSurface, const QRect * pSrcRect, const VBoxVHWAColorKey * pDstCKey, const VBoxVHWAColorKey * pSrcCKey, bool blt)
-{
-//    pDstCKey = NULL;
-//    pSrcCKey = NULL;
-
-    GLuint tex = pSrcSurface->textureSynched(pSrcRect);
-
-    if(pDstCKey)
-    {
-        synchTex(pDstRect);
-    }
-
-    mState->makeCurrent(this, blt);
-
-    VBoxVHWAGlProgramMngr * pMngr = getGlProgramMngr();
-    VBoxVHWAGlProgramVHWA * pProgram = pMngr->getProgram(pSrcCKey != NULL, &pSrcSurface->colorFormat(), &colorFormat());
-    if(pProgram)
-    {
-        if(pSrcCKey != NULL)
-        {
-            pProgram->start();
-            setCKey(pProgram, &pSrcSurface->colorFormat(), pSrcCKey);
-
-            vboxglActiveTexture(GL_TEXTURE0);
-        }
-    }
-    else
-    {
-        pMngr->stopCurrentProgram();
-    }
-
-//    if(blt)
-    {
+            vboxglBindBuffer(GL_PIXEL_UNPACK_BUFFER, mPBO);
+    );
+
+    if(mpMappedAllignedBuffer)
+    {
+        bool unmapped;
         VBOXQGL_CHECKERR(
-                glBindTexture(GL_TEXTURE_2D, tex);
+                unmapped = vboxglUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
                 );
 
-        //TODO: setup strething params
-        GLsizei wdt = pSrcSurface->mTexRect.width();
-        GLsizei hgt = pSrcSurface->mTexRect.height();
-
-        VBOXQGL_CHECKERR(
-                glMatrixMode(GL_TEXTURE);
-                );
-        VBOXQGL_CHECKERR(
-                glPushMatrix();
-            );
-
-        VBoxGLWidget::doSetupMatrix(QSize(wdt, hgt), true);
-        VBOXQGL_CHECKERR(
-                glMatrixMode(GL_MODELVIEW);
-                );
-
-        doTex2FB(pDstRect, pSrcRect);
-
-        VBOXQGL_CHECKERR(
-                glMatrixMode(GL_TEXTURE);
-                );
-        VBOXQGL_CHECKERR(
-                glPopMatrix();
-                );
-        VBOXQGL_CHECKERR(
-                glMatrixMode(GL_MODELVIEW);
-                );
-    }
-//    else
-//    {
-//
-//    }
-
-    /* if dst color key */
-    /* setup ckey shader */
-    if(pDstCKey)
-    {
-        VBOXQGL_CHECKERR(
-                glBindTexture(GL_TEXTURE_2D, mTexture);
-                );
-        pProgram = pMngr->getProgram(true, NULL, NULL);
-        /* setup ckey values*/
-        setCKey(pProgram, &colorFormat(), pDstCKey);
-        pProgram->start();
-        doTex2FB(pDstRect, pDstRect);
-    }
-
-    return VINF_SUCCESS;
-}
-
-int VBoxVHWASurfaceBase::overlay(VBoxVHWASurfaceBase * pOverlaySurface)
-{
-    VBOXQGLLOG(("overlay src(0x%x) ", pOverlaySurface));
-    VBOXQGLLOG_QRECT("dst: ", &pOverlaySurface->mTargRect, "\n");
-    VBOXQGLLOG_QRECT("src: ", &pOverlaySurface->mSrcRect,  "\n");
-    VBOXQGLLOG_METHODTIME("time:");
-
-    Assert(!pOverlaySurface->isHidden());
-
-    if(pOverlaySurface->isHidden())
-    {
-        VBOXQGLLOG(("!!!hidden!!!\n"));
-        return VINF_SUCCESS;
-    }
-
-    const QRect * pSrcRect = &pOverlaySurface->mSrcRect;
-    const QRect * pDstRect = &pOverlaySurface->mTargRect;
-    const VBoxVHWAColorKey * pSrcCKey = pOverlaySurface->srcOverlayCKey();
-    /* we use src (overlay) surface to maintain overridden dst ckey info
-     * to allow multiple overlays have different overridden dst keys for one primary surface */
-    /* non-null dstOverlayCKey for overlay would mean the overlay surface contains the overridden
-     * dst ckey value in defaultDstOverlayCKey
-     * this allows the NULL to be a valid overridden value as well */
-    const VBoxVHWAColorKey * pDstCKey = pOverlaySurface->dstOverlayCKey() ? pOverlaySurface->defaultDstOverlayCKey() : dstOverlayCKey();
-
-    return performBlt(pDstRect, pOverlaySurface, pSrcRect, pDstCKey, pSrcCKey, false);
-}
-
-int VBoxVHWASurfaceBase::blt(const QRect * pDstRect, VBoxVHWASurfaceBase * pSrcSurface, const QRect * pSrcRect, const VBoxVHWAColorKey * pDstCKey, const VBoxVHWAColorKey * pSrcCKey)
-{
-    if(pDstRect)
-    {
-        Assert(mRect.contains(*pDstRect));
-    }
-    else
-    {
-        pDstRect = &mRect;
-    }
-
-    if(pSrcRect)
-    {
-        Assert(pSrcSurface->mRect.contains(*pSrcRect));
-    }
-    else
-    {
-        pSrcRect = &pSrcSurface->mRect;
-    }
-
-    if(!pSrcCKey)
-        pSrcCKey = pSrcSurface->srcBltCKey();
-    if(!pDstCKey)
-        pDstCKey = dstBltCKey();
-
-    VBOXQGLLOG(("blt dst(0x%x), src(0x%x)", this, pSrcSurface));
-    VBOXQGLLOG_QRECT("dst: ", pDstRect, "\n");
-    VBOXQGLLOG_QRECT("src: ", pSrcRect, "\n");
-    VBOXQGLLOG_METHODTIME("time:");
-    int rc = performBlt(pDstRect, pSrcSurface, pSrcRect, pDstCKey, pSrcCKey, true);
-
-    mUpdateFB2TexRect.add(*pDstRect);
-    Assert(!mUpdateFB2TexRect.isClear());
-    Assert(mRect.contains(mUpdateFB2TexRect.rect()));
-//    synchTexture(pDstRect);
-    mUpdateFB2MemRect.add(*pDstRect);
-    Assert(!mUpdateFB2MemRect.isClear());
-    Assert(mRect.contains(mUpdateFB2MemRect.rect()));
-
-    return rc;
-}
-#endif
+        Assert(unmapped);
+
+        mpMappedAllignedBuffer = NULL;
+    }
+
+    VBoxVHWATextureNP2Rect::doUpdate((uchar *)mcbOffset, &mRect);
+
+    VBOXQGL_CHECKERR(
+            vboxglBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
+    );
+}
+
 void VBoxVHWASurfaceBase::doTex2FB(const QRect * pDstRect, const QRect * pSrcRect)
 {
@@ -2068,39 +1923,15 @@
     tx2++; ty2++;bx2++; by2++;
 
-#if 1
-//    VBOXQGL_CHECKERR(
-            VBOXQGLLOG_QRECT("texRect: ", &mpTex[0]->texRect(), "\n");
-            glBegin(GL_QUADS);
-//            glTexCoord2d(((double)tx1)/mpTex[0]->texRect().width(), ((double)ty1)/mpTex[0]->texRect().height());
-//            glVertex2i(bx1, by1);
-//            glTexCoord2d(((double)tx1)/mpTex[0]->texRect().width(), ((double)ty2)/mpTex[0]->texRect().height());
-//            glVertex2i(bx1, by2);
-//            glTexCoord2d(((double)tx2)/mpTex[0]->texRect().width(), ((double)ty2)/mpTex[0]->texRect().height());
-//            glVertex2i(bx2, by2);
-//            glTexCoord2d(((double)tx2)/mpTex[0]->texRect().width(), ((double)ty1)/mpTex[0]->texRect().height());
-//            glVertex2i(bx2, by1);
-            mpTex[0]->texCoord(tx1, ty1);
-            glVertex2i(bx1, by1);
-            mpTex[0]->texCoord(tx1, ty2);
-            glVertex2i(bx1, by2);
-            mpTex[0]->texCoord(tx2, ty2);
-            glVertex2i(bx2, by2);
-            mpTex[0]->texCoord(tx2, ty1);
-            glVertex2i(bx2, by1);
-
-            glEnd();
-//            );
-#else
-        glBegin(GL_QUADS);
-        glTexCoord2d(0.0, 0.0);
-        glVertex2i(0, 0);
-        glTexCoord2d(0.0, 1.0);
-        glVertex2i(0, mRect.height());
-        glTexCoord2d(1.0, 1.0);
-        glVertex2i(mRect.width(), mRect.height());
-        glTexCoord2d(1.0, 0.0);
-        glVertex2i(mRect.width(), 0);
-        glEnd();
-#endif
+    VBOXQGLLOG_QRECT("texRect: ", &mpTex[0]->texRect(), "\n");
+    glBegin(GL_QUADS);
+    mpTex[0]->texCoord(tx1, ty1);
+    glVertex2i(bx1, by1);
+    mpTex[0]->texCoord(tx1, ty2);
+    glVertex2i(bx1, by2);
+    mpTex[0]->texCoord(tx2, ty2);
+    glVertex2i(bx2, by2);
+    mpTex[0]->texCoord(tx2, ty1);
+    glVertex2i(bx2, by1);
+    glEnd();
 }
 
Index: /trunk/src/VBox/Frontends/VirtualBox/src/VBoxFBOverlay.h
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/VBoxFBOverlay.h	(revision 25256)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/VBoxFBOverlay.h	(revision 25257)
@@ -192,5 +192,4 @@
 public:
 
-//    VBoxVHWAColorFormat(GLint aInternalFormat, GLenum aFormat, GLenum aType, uint32_t aDataFormat);
     VBoxVHWAColorFormat(uint32_t bitsPerPixel, uint32_t r, uint32_t g, uint32_t b);
     VBoxVHWAColorFormat(uint32_t fourcc);
@@ -203,5 +202,4 @@
     uint32_t bitsPerPixel() const { return mBitsPerPixel; }
     uint32_t bitsPerPixelTex() const { return mBitsPerPixelTex; }
-//    uint32_t bitsPerPixelDd() const { return mBitsPerPixelDd; }
     void pixel2Normalized(uint32_t pix, float *r, float *g, float *b) const;
     uint32_t widthCompression() const {return mWidthCompression;}
@@ -225,5 +223,4 @@
     uint32_t mBitsPerPixel;
     uint32_t mBitsPerPixelTex;
-//    uint32_t mBitsPerPixelDd;
     uint32_t mWidthCompression;
     uint32_t mHeightCompression;
@@ -311,7 +308,13 @@
 {
 public:
-    VBoxVHWATextureNP2RectPBO() : VBoxVHWATextureNP2Rect() {}
+    VBoxVHWATextureNP2RectPBO() :
+        VBoxVHWATextureNP2Rect(),
+        mPBO(0)
+    {}
     VBoxVHWATextureNP2RectPBO(const QRect & aRect, const VBoxVHWAColorFormat &aFormat) :
-        VBoxVHWATextureNP2Rect(aRect, aFormat){}
+        VBoxVHWATextureNP2Rect(aRect, aFormat),
+        mPBO(0)
+    {}
+
     virtual ~VBoxVHWATextureNP2RectPBO();
 
@@ -320,6 +323,67 @@
     virtual void load();
     virtual void doUpdate(uchar * pAddress, const QRect * pRect);
-private:
     GLuint mPBO;
+};
+
+class VBoxVHWATextureNP2RectPBOMapped : public VBoxVHWATextureNP2RectPBO
+{
+public:
+    VBoxVHWATextureNP2RectPBOMapped() :
+        VBoxVHWATextureNP2RectPBO(),
+        mpMappedAllignedBuffer(NULL),
+        mcbAllignedBufferSize(0),
+        mcbOffset(0)
+    {}
+    VBoxVHWATextureNP2RectPBOMapped(const QRect & aRect, const VBoxVHWAColorFormat &aFormat) :
+            VBoxVHWATextureNP2RectPBO(aRect, aFormat),
+            mpMappedAllignedBuffer(NULL),
+            mcbOffset(0)
+    {
+        mcbAllignedBufferSize = alignSize((size_t)memSize());
+        mcbActualBufferSize = mcbAllignedBufferSize + 0x1fff;
+    }
+
+    uchar* mapAlignedBuffer();
+    void   unmapBuffer();
+    size_t alignedBufferSize() { return mcbAllignedBufferSize; }
+
+    static size_t alignSize(size_t size)
+    {
+        size_t alSize = size & ~((size_t)0xfff);
+        return alSize == size ? alSize : alSize + 0x1000;
+    }
+
+    static void* alignBuffer(void* pvMem) { return (void*)(((uintptr_t)pvMem) & ~((uintptr_t)0xfff)); }
+    static size_t calcOffset(void* pvBase, void* pvOffset) { return (size_t)(((uintptr_t)pvBase) - ((uintptr_t)pvOffset)); }
+protected:
+    virtual void load();
+    virtual void doUpdate(uchar * pAddress, const QRect * pRect);
+private:
+    uchar* mpMappedAllignedBuffer;
+    size_t mcbAllignedBufferSize;
+    size_t mcbOffset;
+    size_t mcbActualBufferSize;
+};
+
+class VBoxVHWAAlignedPBO
+{
+public:
+    void init(size_t size)
+    {
+
+    }
+
+    size_t alignedSize() { return mAlignedSize; }
+    size_t actualSize() { return mActualSize; }
+    size_t requestedSize() { return mRequestedSize; }
+    void* alignedBuffer() { return mAlignedBuffer; }
+    void* actualBuffer() { return mActualBuffer; }
+
+private:
+    void* mAlignedBuffer;
+    void* mActualBuffer;
+    size_t mAlignedSize;
+    size_t mActualSize;
+    size_t mRequestedSize;
 };
 
