Index: /trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_main.c
===================================================================
--- /trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_main.c	(revision 55762)
+++ /trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_main.c	(revision 55763)
@@ -1220,4 +1220,35 @@
 }
 
+static int crVBoxAddFBDataElement(CRFBData *pData, GLint idFBO, GLenum enmBuffer, GLint width, GLint height, GLenum enmFormat, GLenum enmType)
+{
+    CRFBDataElement *pEl;
+
+    AssertCompile(sizeof (GLfloat) == 4);
+    AssertCompile(sizeof (GLuint) == 4);
+
+    pEl = &pData->aElements[pData->cElements];
+    pEl->idFBO = idFBO;
+    pEl->enmBuffer = enmBuffer;
+    pEl->posX = 0;
+    pEl->posY = 0;
+    pEl->width = width;
+    pEl->height = height;
+    pEl->enmFormat = enmFormat;
+    pEl->enmType = enmType;
+    pEl->cbData = width * height * 4;
+
+    pEl->pvData = crCalloc(pEl->cbData);
+    if (!pEl->pvData)
+    {
+        crVBoxServerFBImageDataTerm(pData);
+        crWarning(": crCalloc failed");
+        return VERR_NO_MEMORY;
+    }
+
+    ++pData->cElements;
+
+    return VINF_SUCCESS;
+}
+
 static int crVBoxServerFBImageDataInitEx(CRFBData *pData, CRContextInfo *pCtxInfo, CRMuralInfo *pMural, GLboolean fWrite, uint32_t version, GLuint overrideWidth, GLuint overrideHeight)
 {
@@ -1228,4 +1259,5 @@
     GLuint width;
     GLuint height;
+    int rc;
 
     crMemset(pData, 0, sizeof (*pData));
@@ -1258,105 +1290,53 @@
     pData->cElements = 0;
 
-    pEl = &pData->aElements[pData->cElements];
-    pEl->idFBO = pMural && pMural->fRedirected ? pMural->aidFBOs[CR_SERVER_FBO_FB_IDX(pMural)] : 0;
-    pEl->enmBuffer = pData->aElements[1].idFBO ? GL_COLOR_ATTACHMENT0 : GL_FRONT;
-    pEl->posX = 0;
-    pEl->posY = 0;
-    pEl->width = width;
-    pEl->height = height;
-    pEl->enmFormat = GL_RGBA;
-    pEl->enmType = GL_UNSIGNED_BYTE;
-    pEl->cbData = width * height * 4;
-    pEl->pvData = crCalloc(pEl->cbData);
-    if (!pEl->pvData)
-    {
-        crVBoxServerFBImageDataTerm(pData);
-        crWarning("crVBoxServerFBImageDataInit: crCalloc failed");
-        return VERR_NO_MEMORY;
-    }
-    ++pData->cElements;
-
-    /* there is a lot of code that assumes we have double buffering, just assert here to print a warning in the log
-     * so that we know that something irregular is going on */
+    rc = crVBoxAddFBDataElement(
+            pData,
+            pMural && pMural->fRedirected ? pMural->aidFBOs[CR_SERVER_FBO_FB_IDX(pMural)] : 0,
+            pData->aElements[1].idFBO ? GL_COLOR_ATTACHMENT0 : GL_FRONT,
+            width, height, GL_RGBA, GL_UNSIGNED_BYTE);
+    AssertReturn(rc == VINF_SUCCESS, rc);
+
+    /* There is a lot of code that assumes we have double buffering, just assert here to print a warning in the log
+     * so that we know that something irregular is going on. */
     CRASSERT(pCtxInfo->CreateInfo.requestedVisualBits & CR_DOUBLE_BIT);
-    if ((pCtxInfo->CreateInfo.requestedVisualBits & CR_DOUBLE_BIT)
-    		|| version < SHCROGL_SSM_VERSION_WITH_SINGLE_DEPTH_STENCIL /* <- older version had a typo which lead to back always being used,
-    																	* no matter what the visual bits are */
-    		)
-    {
-        pEl = &pData->aElements[pData->cElements];
-        pEl->idFBO = pMural && pMural->fRedirected ? pMural->aidFBOs[CR_SERVER_FBO_BB_IDX(pMural)] : 0;
-        pEl->enmBuffer = pData->aElements[1].idFBO ? GL_COLOR_ATTACHMENT0 : GL_BACK;
-        pEl->posX = 0;
-        pEl->posY = 0;
-        pEl->width = width;
-        pEl->height = height;
-        pEl->enmFormat = GL_RGBA;
-        pEl->enmType = GL_UNSIGNED_BYTE;
-        pEl->cbData = width * height * 4;
-        pEl->pvData = crCalloc(pEl->cbData);
-        if (!pEl->pvData)
-        {
-            crVBoxServerFBImageDataTerm(pData);
-            crWarning("crVBoxServerFBImageDataInit: crCalloc failed");
-            return VERR_NO_MEMORY;
-        }
-        ++pData->cElements;
+
+    if ((   pCtxInfo->CreateInfo.requestedVisualBits & CR_DOUBLE_BIT)
+         || version < SHCROGL_SSM_VERSION_WITH_SINGLE_DEPTH_STENCIL) /* <- Older version had a typo which lead to back always being used,
+                                                                      *    no matter what the visual bits are. */
+    {
+        rc = crVBoxAddFBDataElement(
+                pData,
+                pMural && pMural->fRedirected ? pMural->aidFBOs[CR_SERVER_FBO_BB_IDX(pMural)] : 0,
+                pData->aElements[1].idFBO ? GL_COLOR_ATTACHMENT0 : GL_BACK,
+                width, height, GL_RGBA, GL_UNSIGNED_BYTE);
+        AssertReturn(rc == VINF_SUCCESS, rc);
     }
 
     if (version < SHCROGL_SSM_VERSION_WITH_SAVED_DEPTH_STENCIL_BUFFER)
-    	return VINF_SUCCESS;
+        return VINF_SUCCESS;
 
     if (pCtxInfo->CreateInfo.requestedVisualBits & CR_DEPTH_BIT)
     {
-        AssertCompile(sizeof (GLfloat) == 4);
-        pEl = &pData->aElements[pData->cElements];
-        pEl->idFBO = pMural && pMural->fRedirected ? pMural->aidFBOs[CR_SERVER_FBO_FB_IDX(pMural)] : 0;
-        pEl->enmBuffer = pMural ? pMural->idDepthRB : 0;
-        pEl->posX = 0;
-        pEl->posY = 0;
-        pEl->width = width;
-        pEl->height = height;
-        pEl->enmFormat = GL_DEPTH_COMPONENT;
-        pEl->enmType = GL_FLOAT;
-        pEl->cbData = width * height * 4;
-        pEl->pvData = crCalloc(pEl->cbData);
-        if (!pEl->pvData)
-        {
-            crVBoxServerFBImageDataTerm(pData);
-            crWarning("crVBoxServerFBImageDataInit: crCalloc failed");
-            return VERR_NO_MEMORY;
-        }
-
-        /* init to default depth value, just in case */
+        rc = crVBoxAddFBDataElement(
+                pData,
+                pMural && pMural->fRedirected ? pMural->aidFBOs[CR_SERVER_FBO_FB_IDX(pMural)] : 0,
+                pMural ? pMural->idDepthRB : 0,
+                width, height, GL_DEPTH_COMPONENT, GL_FLOAT);
+        AssertReturn(rc == VINF_SUCCESS, rc);
+
+        /* Init to default depth value, just in case. */
         pF = (GLfloat*)pEl->pvData;
         for (i = 0; i < width * height; ++i)
-        {
             pF[i] = 1.;
-        }
-        ++pData->cElements;
     }
 
     if (pCtxInfo->CreateInfo.requestedVisualBits & CR_STENCIL_BIT)
     {
-        AssertCompile(sizeof (GLuint) == 4);
-        pEl = &pData->aElements[pData->cElements];
-        pEl->idFBO = pMural && pMural->fRedirected ? pMural->aidFBOs[CR_SERVER_FBO_FB_IDX(pMural)] : 0;
-        pEl->enmBuffer = pMural ? pMural->idDepthRB : 0;
-        pEl->posX = 0;
-        pEl->posY = 0;
-        pEl->width = width;
-        pEl->height = height;
-        pEl->enmFormat = GL_STENCIL_INDEX;
-        pEl->enmType = GL_UNSIGNED_INT;
-        pEl->cbData = width * height * 4;
-        pEl->pvData = crCalloc(pEl->cbData);
-        if (!pEl->pvData)
-        {
-            crVBoxServerFBImageDataTerm(pData);
-            crWarning("crVBoxServerFBImageDataInit: crCalloc failed");
-            return VERR_NO_MEMORY;
-        }
-        ++pData->cElements;
+        rc = crVBoxAddFBDataElement(
+                pData,
+                pMural && pMural->fRedirected ? pMural->aidFBOs[CR_SERVER_FBO_FB_IDX(pMural)] : 0,
+                pMural ? pMural->idDepthRB : 0,
+                width, height, GL_STENCIL_INDEX, GL_UNSIGNED_INT);
+        AssertReturn(rc == VINF_SUCCESS, rc);
     }
 
