Encapsulate image surface creation.

TRAC #18714
Signed-off-by: Daniel Koch
Author: Nicolas Capens

git-svn-id: https://angleproject.googlecode.com/svn/trunk@829 736b8ea6-26fd-11df-bfd4-992fa37f6226
diff --git a/src/libGLESv2/Texture.cpp b/src/libGLESv2/Texture.cpp
index fb2aab0..29a32d7 100644
--- a/src/libGLESv2/Texture.cpp
+++ b/src/libGLESv2/Texture.cpp
@@ -100,6 +100,7 @@
         if (FAILED(result))
         {
             ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
+            ERR("Creating image surface failed.");
             return error(GL_OUT_OF_MEMORY);
         }
 
@@ -202,6 +203,13 @@
     return D3DFMT_A8R8G8B8;
 }
 
+IDirect3DSurface9 *Image::getSurface()
+{
+    createSurface();
+
+    return mSurface;
+}
+
 Texture::Texture(GLuint id) : RefCountObject(id), mSerial(issueSerial())
 {
     mMinFilter = GL_NEAREST_MIPMAP_LINEAR;
@@ -1146,18 +1154,16 @@
 
 void Texture::setImage(GLint unpackAlignment, const void *pixels, Image *image)
 {
-    image->createSurface();
-
-    if (pixels != NULL && image->getSurface() != NULL)
+    if (pixels != NULL)
     {
-        D3DSURFACE_DESC description;
-        image->getSurface()->GetDesc(&description);
-
         D3DLOCKED_RECT locked;
         HRESULT result = image->lock(&locked, NULL);
 
         if (SUCCEEDED(result))
         {
+            D3DSURFACE_DESC description;
+            image->getSurface()->GetDesc(&description);
+
             loadImageData(0, 0, image->getWidth(), image->getHeight(), image->getFormat(), image->getType(), unpackAlignment, pixels, locked.Pitch, locked.pBits, &description);
             image->unlock();
         }
@@ -1169,9 +1175,7 @@
 
 void Texture::setCompressedImage(GLsizei imageSize, const void *pixels, Image *image)
 {
-    image->createSurface();
-
-    if (pixels != NULL && image->getSurface() != NULL)
+    if (pixels != NULL)
     {
         D3DLOCKED_RECT locked;
         HRESULT result = image->lock(&locked, NULL);
@@ -1209,18 +1213,16 @@
         return false;
     }
 
-    image->createSurface();
-
-    if (pixels != NULL && image->getSurface() != NULL)
+    if (pixels != NULL)
     {
-        D3DSURFACE_DESC description;
-        image->getSurface()->GetDesc(&description);
-
         D3DLOCKED_RECT locked;
         HRESULT result = image->lock(&locked, NULL);
 
         if (SUCCEEDED(result))
         {
+            D3DSURFACE_DESC description;
+            image->getSurface()->GetDesc(&description);
+
             loadImageData(xoffset, transformPixelYOffset(yoffset, height, image->getHeight()), width, height, format, type, unpackAlignment, pixels, locked.Pitch, locked.pBits, &description);
             image->unlock();
         }
@@ -1275,14 +1277,6 @@
 // This implements glCopyTex[Sub]Image2D for non-renderable internal texture formats and incomplete textures
 void Texture::copyToImage(Image *image, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, IDirect3DSurface9 *renderTarget)
 {
-    image->createSurface();
-
-    if (!image->getSurface())
-    {
-        ERR("Failed to create an image surface.");
-        return error(GL_OUT_OF_MEMORY);
-    }
-
     IDirect3DDevice9 *device = getDevice();
     IDirect3DSurface9 *renderTargetData = NULL;
     D3DSURFACE_DESC description;
@@ -2061,8 +2055,6 @@
     {
         for (unsigned int i = 1; i <= q; i++)
         {
-            mImageArray[i].createSurface();
-            
             if (mImageArray[i].getSurface() == NULL)
             {
                 return error(GL_OUT_OF_MEMORY);
@@ -2713,7 +2705,6 @@
         {
             for (unsigned int i = 1; i <= q; i++)
             {
-                mImageArray[f][i].createSurface();
                 if (mImageArray[f][i].getSurface() == NULL)
                 {
                     return error(GL_OUT_OF_MEMORY);