Abstracted image locking/unlocking.

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

git-svn-id: https://angleproject.googlecode.com/svn/trunk@827 736b8ea6-26fd-11df-bfd4-992fa37f6226
diff --git a/src/libGLESv2/Texture.cpp b/src/libGLESv2/Texture.cpp
index 8d63a9e..c633f1f 100644
--- a/src/libGLESv2/Texture.cpp
+++ b/src/libGLESv2/Texture.cpp
@@ -107,6 +107,32 @@
     mSurface = newSurface;
 }
 
+HRESULT Image::lock(D3DLOCKED_RECT *lockedRect, const RECT *rect)
+{
+    createSurface();
+
+    HRESULT result = D3DERR_INVALIDCALL;
+
+    if (mSurface)
+    {
+        result = mSurface->LockRect(lockedRect, rect, 0);
+        ASSERT(SUCCEEDED(result));
+
+        mDirty = true;
+    }
+
+    return result;
+}
+
+void Image::unlock()
+{
+    if (mSurface)
+    {
+        HRESULT result = mSurface->UnlockRect();
+        ASSERT(SUCCEEDED(result));
+    }
+}
+
 bool Image::isRenderable() const
 {    
     switch(getD3DFormat())
@@ -1125,14 +1151,12 @@
         image->getSurface()->GetDesc(&description);
 
         D3DLOCKED_RECT locked;
-        HRESULT result = image->getSurface()->LockRect(&locked, NULL, 0);
-
-        ASSERT(SUCCEEDED(result));
+        HRESULT result = image->lock(&locked, NULL);
 
         if (SUCCEEDED(result))
         {
             loadImageData(0, 0, image->getWidth(), image->getHeight(), image->getFormat(), image->getType(), unpackAlignment, pixels, locked.Pitch, locked.pBits, &description);
-            image->getSurface()->UnlockRect();
+            image->unlock();
         }
 
         image->markDirty();
@@ -1147,16 +1171,14 @@
     if (pixels != NULL && image->getSurface() != NULL)
     {
         D3DLOCKED_RECT locked;
-        HRESULT result = image->getSurface()->LockRect(&locked, NULL, 0);
-
-        ASSERT(SUCCEEDED(result));
+        HRESULT result = image->lock(&locked, NULL);
 
         if (SUCCEEDED(result))
         {
             int inputPitch = ComputeCompressedPitch(image->getWidth(), image->getFormat());
             int inputSize = ComputeCompressedSize(image->getWidth(), image->getHeight(), image->getFormat());
             loadCompressedImageData(0, 0, image->getWidth(), image->getHeight(), -inputPitch, static_cast<const char*>(pixels) + inputSize - inputPitch, locked.Pitch, locked.pBits);
-            image->getSurface()->UnlockRect();
+            image->unlock();
         }
 
         image->markDirty();
@@ -1192,14 +1214,12 @@
         image->getSurface()->GetDesc(&description);
 
         D3DLOCKED_RECT locked;
-        HRESULT result = image->getSurface()->LockRect(&locked, NULL, 0);
-
-        ASSERT(SUCCEEDED(result));
+        HRESULT result = image->lock(&locked, NULL);
 
         if (SUCCEEDED(result))
         {
             loadImageData(xoffset, transformPixelYOffset(yoffset, height, image->getHeight()), width, height, format, type, unpackAlignment, pixels, locked.Pitch, locked.pBits, &description);
-            image->getSurface()->UnlockRect();
+            image->unlock();
         }
 
         image->markDirty();
@@ -1223,9 +1243,7 @@
         return false;
     }
 
-    image->createSurface();
-
-    if (pixels != NULL && image->getSurface() != NULL)
+    if (pixels != NULL)
     {
         RECT updateRegion;
         updateRegion.left = xoffset;
@@ -1234,16 +1252,14 @@
         updateRegion.top = yoffset;
 
         D3DLOCKED_RECT locked;
-        HRESULT result = image->getSurface()->LockRect(&locked, &updateRegion, 0);
-
-        ASSERT(SUCCEEDED(result));
+        HRESULT result = image->lock(&locked, &updateRegion);
 
         if (SUCCEEDED(result))
         {
             int inputPitch = ComputeCompressedPitch(width, format);
             int inputSize = ComputeCompressedSize(width, height, format);
             loadCompressedImageData(xoffset, transformPixelYOffset(yoffset, height, image->getHeight()), width, height, -inputPitch, static_cast<const char*>(pixels) + inputSize - inputPitch, locked.Pitch, locked.pBits);
-            image->getSurface()->UnlockRect();
+            image->unlock();
         }
 
         image->markDirty();
@@ -1314,7 +1330,7 @@
         }
 
         D3DLOCKED_RECT destLock = {0};
-        result = image->getSurface()->LockRect(&destLock, &destRect, 0);
+        result = image->lock(&destLock, &destRect);
         
         if (FAILED(result))
         {
@@ -1423,7 +1439,7 @@
             }
         }
 
-        image->getSurface()->UnlockRect();
+        image->unlock();
         renderTargetData->UnlockRect();
     }
 
@@ -1905,7 +1921,7 @@
     {
         Image *image = &mImageArray[level];
 
-        if (image->getSurface() && image->isDirty())
+        if (image->isDirty())
         {
             commitRect(level, 0, 0, mImageArray[level].getWidth(), mImageArray[level].getHeight());
         }
@@ -2362,7 +2378,7 @@
         {
             Image *image = &mImageArray[face][level];
 
-            if (image->getSurface() && image->isDirty())
+            if (image->isDirty())
             {
                 commitRect(face, level, 0, 0, image->getWidth(), image->getHeight());
             }
diff --git a/src/libGLESv2/Texture.h b/src/libGLESv2/Texture.h
index 8296d84..0c9b586 100644
--- a/src/libGLESv2/Texture.h
+++ b/src/libGLESv2/Texture.h
@@ -54,6 +54,9 @@
     void markDirty() {mDirty = true;}
     void markClean() {mDirty = false;}
 
+    HRESULT lock(D3DLOCKED_RECT *lockedRect, const RECT *rect);
+    void unlock();
+
     bool isRenderable() const;
     D3DFORMAT getD3DFormat() const;
 
@@ -61,7 +64,7 @@
     GLsizei getHeight() const {return mHeight;}
     GLenum getFormat() const {return mFormat;}
     GLenum getType() const {return mType;}
-    bool isDirty() const {return mDirty;}
+    bool isDirty() const {return mSurface && mDirty;}
     IDirect3DSurface9 *getSurface() {return mSurface;}
 
   private: