Migrate Image to Image9 and add an abstract base

TRAC #22254

Author: Shannon Woods
Signed-off-by: Geoff Lang
Signed-off-by: Daniel Koch

git-svn-id: https://angleproject.googlecode.com/svn/branches/dx11proto@1572 736b8ea6-26fd-11df-bfd4-992fa37f6226
diff --git a/src/build_angle.gypi b/src/build_angle.gypi
index 7c10dec..76d5fb1 100644
--- a/src/build_angle.gypi
+++ b/src/build_angle.gypi
@@ -257,6 +257,8 @@
             'libGLESv2/renderer/Blit.h',
             'libGLESv2/renderer/Image.cpp',
             'libGLESv2/renderer/Image.h',
+            'libGLESv2/renderer/Image9.cpp',
+            'libGLESv2/renderer/Image9.h',
             'libGLESv2/renderer/Renderer.cpp',
             'libGLESv2/renderer/Renderer.h',
             'libGLESv2/renderer/Renderer11.cpp',
diff --git a/src/libGLESv2/Texture.cpp b/src/libGLESv2/Texture.cpp
index d182f74..10736be 100644
--- a/src/libGLESv2/Texture.cpp
+++ b/src/libGLESv2/Texture.cpp
@@ -487,8 +487,6 @@
 
 void Texture2D::commitRect(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
 {
-    ASSERT(mImageArray[level]->getSurface() != NULL);
-
     if (level < levelCount())
     {
         rx::Image *image = mImageArray[level];
@@ -822,7 +820,7 @@
     {
         for (unsigned int i = 1; i <= q; i++)
         {
-            rx::Image::generateMipmap(mImageArray[i], mImageArray[i - 1]);
+            mRenderer->generateMipmap(mImageArray[i], mImageArray[i - 1]);
         }
     }
 }
@@ -1041,8 +1039,6 @@
 
 void TextureCubeMap::commitRect(int face, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
 {
-    ASSERT(mImageArray[face][level]->getSurface() != NULL);
-
     if (level < levelCount())
     {
         rx::Image *image = mImageArray[face][level];
@@ -1448,7 +1444,7 @@
         {
             for (unsigned int i = 1; i <= q; i++)
             {
-                rx::Image::generateMipmap(mImageArray[f][i], mImageArray[f][i - 1]);
+                mRenderer->generateMipmap(mImageArray[f][i], mImageArray[f][i - 1]);
             }
         }
     }
diff --git a/src/libGLESv2/TextureSSE2.cpp b/src/libGLESv2/TextureSSE2.cpp
index f626c5d..f760354 100644
--- a/src/libGLESv2/TextureSSE2.cpp
+++ b/src/libGLESv2/TextureSSE2.cpp
@@ -9,6 +9,7 @@
 // not for code blocks that use SSE2 explicitly.
 
 #include "libGLESv2/Texture.h"
+#include "libGLESv2/renderer/Image.h"
 
 #include <intrin.h>
 
@@ -16,7 +17,7 @@
 {
 
 void Image::loadRGBAUByteDataSSE2(GLsizei width, GLsizei height,
-                                  int inputPitch, const void *input, size_t outputPitch, void *output) const
+                                  int inputPitch, const void *input, size_t outputPitch, void *output)
 {
     const unsigned int *source = NULL;
     unsigned int *dest = NULL;
@@ -58,7 +59,7 @@
 }
 
 void Image::loadAlphaDataSSE2(GLsizei width, GLsizei height,
-                              int inputPitch, const void *input, size_t outputPitch, void *output) const
+                              int inputPitch, const void *input, size_t outputPitch, void *output)
 {
     const unsigned char *source = NULL;
     unsigned int *dest = NULL;
diff --git a/src/libGLESv2/libGLESv2.vcxproj b/src/libGLESv2/libGLESv2.vcxproj
index 592130b..c8ff6a6 100644
--- a/src/libGLESv2/libGLESv2.vcxproj
+++ b/src/libGLESv2/libGLESv2.vcxproj
@@ -241,16 +241,17 @@
     <ClCompile Include="Program.cpp" />
     <ClCompile Include="ProgramBinary.cpp" />

     <ClCompile Include="Query.cpp" />

-    <ClCompile Include="..\common\RefCountObject.cpp" />
-    <ClCompile Include="Renderbuffer.cpp" />
-    <ClCompile Include="renderer\Blit.cpp" />
+    <ClCompile Include="..\common\RefCountObject.cpp" />

+    <ClCompile Include="Renderbuffer.cpp" />

+    <ClCompile Include="renderer\Blit.cpp" />

     <ClCompile Include="renderer\D3DConstantTable.cpp" />
-    <ClCompile Include="renderer\IndexDataManager.cpp" />
-    <ClCompile Include="renderer\Renderer.cpp" />
-    <ClCompile Include="renderer\Renderer11.cpp" />
-    <ClCompile Include="renderer\renderer11_utils.cpp" />
-    <ClCompile Include="renderer\Renderer9.cpp" />

     <ClCompile Include="renderer\Image.cpp" />

+    <ClCompile Include="renderer\Image9.cpp" />

+    <ClCompile Include="renderer\IndexDataManager.cpp" />
+    <ClCompile Include="renderer\Renderer.cpp" />

+    <ClCompile Include="renderer\Renderer11.cpp" />

+    <ClCompile Include="renderer\renderer11_utils.cpp" />

+    <ClCompile Include="renderer\Renderer9.cpp" />

     <ClCompile Include="renderer\renderer9_utils.cpp" />

     <ClCompile Include="renderer\RenderTarget11.cpp" />

     <ClCompile Include="renderer\RenderTarget9.cpp" />

@@ -285,15 +286,16 @@
     <ClInclude Include="Program.h" />
     <ClInclude Include="ProgramBinary.h" />

     <ClInclude Include="Query.h" />

-    <ClInclude Include="..\common\RefCountObject.h" />
-    <ClInclude Include="Renderbuffer.h" />
-    <ClInclude Include="renderer\Blit.h" />
+    <ClInclude Include="..\common\RefCountObject.h" />

+    <ClInclude Include="Renderbuffer.h" />

+    <ClInclude Include="renderer\Blit.h" />

     <ClInclude Include="renderer\D3DConstantTable.h" />
-    <ClInclude Include="renderer\Image.h" />
+    <ClInclude Include="renderer\Image.h" />

+    <ClInclude Include="renderer\Image9.h" />

     <ClInclude Include="renderer\IndexDataManager.h" />
-    <ClInclude Include="renderer\Renderer.h" />
-    <ClInclude Include="renderer\Renderer11.h" />
-    <ClInclude Include="renderer\renderer11_utils.h" />
+    <ClInclude Include="renderer\Renderer.h" />

+    <ClInclude Include="renderer\Renderer11.h" />

+    <ClInclude Include="renderer\renderer11_utils.h" />

     <ClInclude Include="renderer\Renderer9.h" />

     <ClInclude Include="renderer\renderer9_utils.h" />

     <ClInclude Include="renderer\RenderTarget.h" />

@@ -337,4 +339,4 @@
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

   <ImportGroup Label="ExtensionTargets">

   </ImportGroup>

-</Project>
\ No newline at end of file
+</Project>
diff --git a/src/libGLESv2/libGLESv2.vcxproj.filters b/src/libGLESv2/libGLESv2.vcxproj.filters
index 2d084f9..4bb6789 100644
--- a/src/libGLESv2/libGLESv2.vcxproj.filters
+++ b/src/libGLESv2/libGLESv2.vcxproj.filters
@@ -74,12 +74,12 @@
     <ClCompile Include="TextureSSE2.cpp">

       <Filter>Source Files</Filter>

     </ClCompile>

-    <ClCompile Include="utilities.cpp">
-      <Filter>Source Files</Filter>
-    </ClCompile>
-    <ClCompile Include="renderer\Renderer9.cpp">
-      <Filter>Renderer</Filter>
-    </ClCompile>
+    <ClCompile Include="utilities.cpp">

+      <Filter>Source Files</Filter>

+    </ClCompile>

+    <ClCompile Include="renderer\Renderer9.cpp">

+      <Filter>Renderer</Filter>

+    </ClCompile>

     <ClCompile Include="renderer\Image.cpp">

       <Filter>Renderer</Filter>

     </ClCompile>

@@ -122,9 +122,6 @@
     <ClCompile Include="renderer\VertexDeclarationCache.cpp">

       <Filter>Renderer</Filter>

     </ClCompile>
-    <ClCompile Include="renderer\ShaderExecutable9.cpp">
-      <Filter>Renderer</Filter>
-    </ClCompile>
     <ClCompile Include="renderer\D3DConstantTable.cpp">
       <Filter>Renderer</Filter>
     </ClCompile>
@@ -133,10 +130,22 @@
     </ClCompile>
     <ClCompile Include="renderer\VertexDataManager.cpp">
       <Filter>Renderer</Filter>
-    </ClCompile>
-  </ItemGroup>
-  <ItemGroup>
-    <ClInclude Include="BinaryStream.h">
+    </ClCompile>

+    <ClCompile Include="renderer\ShaderExecutable9.cpp">

+      <Filter>Renderer</Filter>

+    </ClCompile>

+    <ClCompile Include="renderer\ShaderExecutable11.cpp">

+      <Filter>Renderer</Filter>

+    </ClCompile>

+    <ClCompile Include="renderer\Image9.cpp">

+      <Filter>Renderer</Filter>

+    </ClCompile>

+    <ClCompile Include="renderer\Image.cpp">

+      <Filter>Renderer</Filter>

+    </ClCompile>

+  </ItemGroup>

+  <ItemGroup>

+    <ClInclude Include="BinaryStream.h">

       <Filter>Header Files</Filter>

     </ClInclude>

     <ClInclude Include="Buffer.h">

@@ -214,9 +223,6 @@
     <ClInclude Include="renderer\Renderer9.h">

       <Filter>Renderer</Filter>

     </ClInclude>

-    <ClInclude Include="renderer\Image.h">

-      <Filter>Renderer</Filter>

-    </ClInclude>

     <ClInclude Include="renderer\TextureStorage.h">

       <Filter>Renderer</Filter>

     </ClInclude>

@@ -262,9 +268,6 @@
     <ClInclude Include="renderer\ShaderExecutable.h">

       <Filter>Renderer</Filter>

     </ClInclude>
-    <ClInclude Include="renderer\ShaderExecutable9.h">
-      <Filter>Renderer</Filter>
-    </ClInclude>
     <ClInclude Include="renderer\VertexDataManager.h">
       <Filter>Renderer</Filter>
     </ClInclude>
@@ -276,14 +279,26 @@
     </ClInclude>
     <ClInclude Include="renderer\vertexconversion.h">
       <Filter>Renderer</Filter>
-    </ClInclude>
-  </ItemGroup>
-  <ItemGroup>
-    <None Include="libGLESv2.def">
+    </ClInclude>

+    <ClInclude Include="renderer\ShaderExecutable9.h">

+      <Filter>Renderer</Filter>

+    </ClInclude>

+    <ClInclude Include="renderer\ShaderExecutable11.h">

+      <Filter>Renderer</Filter>

+    </ClInclude>

+    <ClInclude Include="renderer\Image9.h">

+      <Filter>Renderer</Filter>

+    </ClInclude>

+    <ClInclude Include="renderer\Image.h">

+      <Filter>Renderer</Filter>

+    </ClInclude>

+  </ItemGroup>

+  <ItemGroup>

+    <None Include="libGLESv2.def">

       <Filter>Source Files</Filter>

     </None>

   </ItemGroup>

   <ItemGroup>

     <ResourceCompile Include="libGLESv2.rc" />

   </ItemGroup>

-</Project>
\ No newline at end of file
+</Project>
diff --git a/src/libGLESv2/renderer/Image.cpp b/src/libGLESv2/renderer/Image.cpp
index a5a7f9f..351fb8f 100644
--- a/src/libGLESv2/renderer/Image.cpp
+++ b/src/libGLESv2/renderer/Image.cpp
@@ -4,578 +4,29 @@
 // found in the LICENSE file.
 //
 
-// Image.cpp: Implements the rx::Image class, which acts as the interface to
-// the actual underlying surfaces of a Texture.
+// Image.h: Implements the rx::Image class, an abstract base class for the 
+// renderer-specific classes which will define the interface to the underlying
+// surfaces or resources.
+
+#include <GLES2/gl2.h>
+#include <string.h>
+#include <stdlib.h>
 
 #include "libGLESv2/renderer/Image.h"
 
-#include "libEGL/Display.h"
-
-#include "libGLESv2/main.h"
-#include "libGLESv2/mathutil.h"
-#include "libGLESv2/utilities.h"
-#include "libGLESv2/Texture.h"
-#include "libGLESv2/Framebuffer.h"
-#include "libGLESv2/renderer/RenderTarget9.h"
-
-#include "libGLESv2/renderer/renderer9_utils.h"
-
 namespace rx
 {
 
-namespace
-{
-struct L8
-{
-    unsigned char L;
-
-    static void average(L8 *dst, const L8 *src1, const L8 *src2)
-    {
-        dst->L = ((src1->L ^ src2->L) >> 1) + (src1->L & src2->L);
-    }
-};
-
-struct A8L8
-{
-    unsigned char L;
-    unsigned char A;
-
-    static void average(A8L8 *dst, const A8L8 *src1, const A8L8 *src2)
-    {
-        *(unsigned short*)dst = (((*(unsigned short*)src1 ^ *(unsigned short*)src2) & 0xFEFE) >> 1) + (*(unsigned short*)src1 & *(unsigned short*)src2);
-    }
-};
-
-struct A8R8G8B8
-{
-    unsigned char B;
-    unsigned char G;
-    unsigned char R;
-    unsigned char A;
-
-    static void average(A8R8G8B8 *dst, const A8R8G8B8 *src1, const A8R8G8B8 *src2)
-    {
-        *(unsigned int*)dst = (((*(unsigned int*)src1 ^ *(unsigned int*)src2) & 0xFEFEFEFE) >> 1) + (*(unsigned int*)src1 & *(unsigned int*)src2);
-    }
-};
-
-struct A16B16G16R16F
-{
-    unsigned short R;
-    unsigned short G;
-    unsigned short B;
-    unsigned short A;
-
-    static void average(A16B16G16R16F *dst, const A16B16G16R16F *src1, const A16B16G16R16F *src2)
-    {
-        dst->R = gl::float32ToFloat16((gl::float16ToFloat32(src1->R) + gl::float16ToFloat32(src2->R)) * 0.5f);
-        dst->G = gl::float32ToFloat16((gl::float16ToFloat32(src1->G) + gl::float16ToFloat32(src2->G)) * 0.5f);
-        dst->B = gl::float32ToFloat16((gl::float16ToFloat32(src1->B) + gl::float16ToFloat32(src2->B)) * 0.5f);
-        dst->A = gl::float32ToFloat16((gl::float16ToFloat32(src1->A) + gl::float16ToFloat32(src2->A)) * 0.5f);
-    }
-};
-
-struct A32B32G32R32F
-{
-    float R;
-    float G;
-    float B;
-    float A;
-
-    static void average(A32B32G32R32F *dst, const A32B32G32R32F *src1, const A32B32G32R32F *src2)
-    {
-        dst->R = (src1->R + src2->R) * 0.5f;
-        dst->G = (src1->G + src2->G) * 0.5f;
-        dst->B = (src1->B + src2->B) * 0.5f;
-        dst->A = (src1->A + src2->A) * 0.5f;
-    }
-};
-
-template <typename T>
-void GenerateMip(unsigned int sourceWidth, unsigned int sourceHeight,
-                 const unsigned char *sourceData, int sourcePitch,
-                 unsigned char *destData, int destPitch)
-{
-    unsigned int mipWidth = std::max(1U, sourceWidth >> 1);
-    unsigned int mipHeight = std::max(1U, sourceHeight >> 1);
-
-    if (sourceHeight == 1)
-    {
-        ASSERT(sourceWidth != 1);
-
-        const T *src = (const T*)sourceData;
-        T *dst = (T*)destData;
-
-        for (unsigned int x = 0; x < mipWidth; x++)
-        {
-            T::average(&dst[x], &src[x * 2], &src[x * 2 + 1]);
-        }
-    }
-    else if (sourceWidth == 1)
-    {
-        ASSERT(sourceHeight != 1);
-
-        for (unsigned int y = 0; y < mipHeight; y++)
-        {
-            const T *src0 = (const T*)(sourceData + y * 2 * sourcePitch);
-            const T *src1 = (const T*)(sourceData + y * 2 * sourcePitch + sourcePitch);
-            T *dst = (T*)(destData + y * destPitch);
-
-            T::average(dst, src0, src1);
-        }
-    }
-    else
-    {
-        for (unsigned int y = 0; y < mipHeight; y++)
-        {
-            const T *src0 = (const T*)(sourceData + y * 2 * sourcePitch);
-            const T *src1 = (const T*)(sourceData + y * 2 * sourcePitch + sourcePitch);
-            T *dst = (T*)(destData + y * destPitch);
-
-            for (unsigned int x = 0; x < mipWidth; x++)
-            {
-                T tmp0;
-                T tmp1;
-
-                T::average(&tmp0, &src0[x * 2], &src0[x * 2 + 1]);
-                T::average(&tmp1, &src1[x * 2], &src1[x * 2 + 1]);
-                T::average(&dst[x], &tmp0, &tmp1);
-            }
-        }
-    }
-}
-
-void GenerateMip(IDirect3DSurface9 *destSurface, IDirect3DSurface9 *sourceSurface)
-{
-    D3DSURFACE_DESC destDesc;
-    HRESULT result = destSurface->GetDesc(&destDesc);
-    ASSERT(SUCCEEDED(result));
-
-    D3DSURFACE_DESC sourceDesc;
-    result = sourceSurface->GetDesc(&sourceDesc);
-    ASSERT(SUCCEEDED(result));
-
-    ASSERT(sourceDesc.Format == destDesc.Format);
-    ASSERT(sourceDesc.Width == 1 || sourceDesc.Width / 2 == destDesc.Width);
-    ASSERT(sourceDesc.Height == 1 || sourceDesc.Height / 2 == destDesc.Height);
-
-    D3DLOCKED_RECT sourceLocked = {0};
-    result = sourceSurface->LockRect(&sourceLocked, NULL, D3DLOCK_READONLY);
-    ASSERT(SUCCEEDED(result));
-
-    D3DLOCKED_RECT destLocked = {0};
-    result = destSurface->LockRect(&destLocked, NULL, 0);
-    ASSERT(SUCCEEDED(result));
-
-    const unsigned char *sourceData = reinterpret_cast<const unsigned char*>(sourceLocked.pBits);
-    unsigned char *destData = reinterpret_cast<unsigned char*>(destLocked.pBits);
-
-    if (sourceData && destData)
-    {
-        switch (sourceDesc.Format)
-        {
-          case D3DFMT_L8:
-            GenerateMip<L8>(sourceDesc.Width, sourceDesc.Height, sourceData, sourceLocked.Pitch, destData, destLocked.Pitch);
-            break;
-          case D3DFMT_A8L8:
-            GenerateMip<A8L8>(sourceDesc.Width, sourceDesc.Height, sourceData, sourceLocked.Pitch, destData, destLocked.Pitch);
-            break;
-          case D3DFMT_A8R8G8B8:
-          case D3DFMT_X8R8G8B8:
-            GenerateMip<A8R8G8B8>(sourceDesc.Width, sourceDesc.Height, sourceData, sourceLocked.Pitch, destData, destLocked.Pitch);
-            break;
-          case D3DFMT_A16B16G16R16F:
-            GenerateMip<A16B16G16R16F>(sourceDesc.Width, sourceDesc.Height, sourceData, sourceLocked.Pitch, destData, destLocked.Pitch);
-            break;
-          case D3DFMT_A32B32G32R32F:
-            GenerateMip<A32B32G32R32F>(sourceDesc.Width, sourceDesc.Height, sourceData, sourceLocked.Pitch, destData, destLocked.Pitch);
-            break;
-          default:
-            UNREACHABLE();
-            break;
-        }
-
-        destSurface->UnlockRect();
-        sourceSurface->UnlockRect();
-    }
-}
-}
-
 Image::Image()
 {
     mWidth = 0; 
     mHeight = 0;
     mInternalFormat = GL_NONE;
-
-    mSurface = NULL;
-
-    mDirty = false;
-
-    mRenderer = NULL;
-
-    mD3DPool = D3DPOOL_SYSTEMMEM;
-    mD3DFormat = D3DFMT_UNKNOWN;
     mActualFormat = GL_NONE;
 }
 
-Image::~Image()
-{
-    if (mSurface)
-    {
-        mSurface->Release();
-    }
-}
-
-void Image::generateMipmap(Image *dest, Image *source)
-{
-    IDirect3DSurface9 *sourceSurface = source->getSurface();
-    if (sourceSurface == NULL)
-        return error(GL_OUT_OF_MEMORY);
-
-    IDirect3DSurface9 *destSurface = dest->getSurface();
-    GenerateMip(destSurface, sourceSurface);
-
-    source->markDirty();
-}
-
-void Image::copyLockableSurfaces(IDirect3DSurface9 *dest, IDirect3DSurface9 *source)
-{
-    D3DLOCKED_RECT sourceLock = {0};
-    D3DLOCKED_RECT destLock = {0};
-    
-    source->LockRect(&sourceLock, NULL, 0);
-    dest->LockRect(&destLock, NULL, 0);
-    
-    if (sourceLock.pBits && destLock.pBits)
-    {
-        D3DSURFACE_DESC desc;
-        source->GetDesc(&desc);
-
-        int rows = dx::IsCompressedFormat(desc.Format) ? desc.Height / 4 : desc.Height;
-        int bytes = dx::ComputeRowSize(desc.Format, desc.Width);
-        ASSERT(bytes <= sourceLock.Pitch && bytes <= destLock.Pitch);
-
-        for(int i = 0; i < rows; i++)
-        {
-            memcpy((char*)destLock.pBits + destLock.Pitch * i, (char*)sourceLock.pBits + sourceLock.Pitch * i, bytes);
-        }
-
-        source->UnlockRect();
-        dest->UnlockRect();
-    }
-    else UNREACHABLE();
-}
-
-bool Image::redefine(rx::Renderer *renderer, GLint internalformat, GLsizei width, GLsizei height, bool forceRelease)
-{
-    if (mWidth != width ||
-        mHeight != height ||
-        mInternalFormat != internalformat ||
-        forceRelease)
-    {
-        mRenderer = Renderer9::makeRenderer9(renderer);
-
-        mWidth = width;
-        mHeight = height;
-        mInternalFormat = internalformat;
-        // compute the d3d format that will be used
-        mD3DFormat = mRenderer->ConvertTextureInternalFormat(internalformat);
-        mActualFormat = d3d9_gl::GetEquivalentFormat(mD3DFormat);
-
-        if (mSurface)
-        {
-            mSurface->Release();
-            mSurface = NULL;
-        }
-
-        return true;
-    }
-
-    return false;
-}
-
-void Image::createSurface()
-{
-    if(mSurface)
-    {
-        return;
-    }
-
-    IDirect3DTexture9 *newTexture = NULL;
-    IDirect3DSurface9 *newSurface = NULL;
-    const D3DPOOL poolToUse = D3DPOOL_SYSTEMMEM;
-    const D3DFORMAT d3dFormat = getD3DFormat();
-    ASSERT(d3dFormat != D3DFMT_INTZ); // We should never get here for depth textures
-
-    if (mWidth != 0 && mHeight != 0)
-    {
-        int levelToFetch = 0;
-        GLsizei requestWidth = mWidth;
-        GLsizei requestHeight = mHeight;
-        gl::MakeValidSize(true, gl::IsCompressed(mInternalFormat), &requestWidth, &requestHeight, &levelToFetch);
-
-        IDirect3DDevice9 *device = mRenderer->getDevice(); // D3D9_REPLACE
-
-        HRESULT result = device->CreateTexture(requestWidth, requestHeight, levelToFetch + 1, 0, d3dFormat,
-                                                    poolToUse, &newTexture, NULL);
-
-        if (FAILED(result))
-        {
-            ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
-            ERR("Creating image surface failed.");
-            return error(GL_OUT_OF_MEMORY);
-        }
-
-        newTexture->GetSurfaceLevel(levelToFetch, &newSurface);
-        newTexture->Release();
-    }
-
-    mSurface = newSurface;
-    mDirty = false;
-    mD3DPool = poolToUse;
-}
-
-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::isRenderableFormat() const
-{    
-    return TextureStorage::IsTextureFormatRenderable(getD3DFormat());
-}
-
-GLenum Image::getActualFormat() const
-{
-    return mActualFormat;
-}
-
-D3DFORMAT Image::getD3DFormat() const
-{
-    // this should only happen if the image hasn't been redefined first
-    // which would be a bug by the caller
-    ASSERT(mD3DFormat != D3DFMT_UNKNOWN);
-
-    return mD3DFormat;
-}
-
-IDirect3DSurface9 *Image::getSurface()
-{
-    createSurface();
-
-    return mSurface;
-}
-
-void Image::setManagedSurface(TextureStorage2D *storage, int level)
-{
-    setManagedSurface(storage->getSurfaceLevel(level, false));
-}
-
-void Image::setManagedSurface(TextureStorageCubeMap *storage, int face, int level)
-{
-    setManagedSurface(storage->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, false));
-}
-
-void Image::setManagedSurface(IDirect3DSurface9 *surface)
-{
-    D3DSURFACE_DESC desc;
-    surface->GetDesc(&desc);
-    ASSERT(desc.Pool == D3DPOOL_MANAGED);
-
-    if ((GLsizei)desc.Width == mWidth && (GLsizei)desc.Height == mHeight)
-    {
-        if (mSurface)
-        {
-            copyLockableSurfaces(surface, mSurface);
-            mSurface->Release();
-        }
-
-        mSurface = surface;
-        mD3DPool = desc.Pool;
-    }
-}
-
-bool Image::updateSurface(TextureStorage2D *storage, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
-{
-    return updateSurface(storage->getSurfaceLevel(level, true), xoffset, yoffset, width, height);
-}
-
-bool Image::updateSurface(TextureStorageCubeMap *storage, int face, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
-{
-    return updateSurface(storage->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, true), xoffset, yoffset, width, height);
-}
-
-bool Image::updateSurface(IDirect3DSurface9 *destSurface, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
-{
-    if (!destSurface)
-        return false;
-
-    IDirect3DSurface9 *sourceSurface = getSurface();
-
-    if (sourceSurface && sourceSurface != destSurface)
-    {
-        RECT rect;
-        rect.left = xoffset;
-        rect.top = yoffset;
-        rect.right = xoffset + width;
-        rect.bottom = yoffset + height;
-
-        POINT point = {rect.left, rect.top};
-
-        IDirect3DDevice9 *device = mRenderer->getDevice(); // D3D9_REPLACE
-
-        if (mD3DPool == D3DPOOL_MANAGED)
-        {
-            D3DSURFACE_DESC desc;
-            sourceSurface->GetDesc(&desc);
-
-            IDirect3DSurface9 *surf = 0;
-            HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL);
-
-            if (SUCCEEDED(result))
-            {
-                copyLockableSurfaces(surf, sourceSurface);
-                result = device->UpdateSurface(surf, &rect, destSurface, &point);
-                ASSERT(SUCCEEDED(result));
-                surf->Release();
-            }
-        }
-        else
-        {
-            // UpdateSurface: source must be SYSTEMMEM, dest must be DEFAULT pools 
-            HRESULT result = device->UpdateSurface(sourceSurface, &rect, destSurface, &point);
-            ASSERT(SUCCEEDED(result));
-        }
-    }
-
-    destSurface->Release();
-    return true;
-}
-
-// Store the pixel rectangle designated by xoffset,yoffset,width,height with pixels stored as format/type at input
-// into the target pixel rectangle.
-void Image::loadData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
-                     GLint unpackAlignment, const void *input)
-{
-    RECT lockRect =
-    {
-        xoffset, yoffset,
-        xoffset + width, yoffset + height
-    };
-
-    D3DLOCKED_RECT locked;
-    HRESULT result = lock(&locked, &lockRect);
-    if (FAILED(result))
-    {
-        return;
-    }
-
-
-    GLsizei inputPitch = gl::ComputePitch(width, mInternalFormat, unpackAlignment);
-
-    switch (mInternalFormat)
-    {
-      case GL_ALPHA8_EXT:
-        if (gl::supportsSSE2())
-        {
-            loadAlphaDataSSE2(width, height, inputPitch, input, locked.Pitch, locked.pBits);
-        }
-        else
-        {
-            loadAlphaData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
-        }
-        break;
-      case GL_LUMINANCE8_EXT:
-        loadLuminanceData(width, height, inputPitch, input, locked.Pitch, locked.pBits, getD3DFormat() == D3DFMT_L8);
-        break;
-      case GL_ALPHA32F_EXT:
-        loadAlphaFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
-        break;
-      case GL_LUMINANCE32F_EXT:
-        loadLuminanceFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
-        break;
-      case GL_ALPHA16F_EXT:
-        loadAlphaHalfFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
-        break;
-      case GL_LUMINANCE16F_EXT:
-        loadLuminanceHalfFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
-        break;
-      case GL_LUMINANCE8_ALPHA8_EXT:
-        loadLuminanceAlphaData(width, height, inputPitch, input, locked.Pitch, locked.pBits, getD3DFormat() == D3DFMT_A8L8);
-        break;
-      case GL_LUMINANCE_ALPHA32F_EXT:
-        loadLuminanceAlphaFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
-        break;
-      case GL_LUMINANCE_ALPHA16F_EXT:
-        loadLuminanceAlphaHalfFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
-        break;
-      case GL_RGB8_OES:
-        loadRGBUByteData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
-        break;
-      case GL_RGB565:
-        loadRGB565Data(width, height, inputPitch, input, locked.Pitch, locked.pBits);
-        break;
-      case GL_RGBA8_OES:
-        if (gl::supportsSSE2())
-        {
-            loadRGBAUByteDataSSE2(width, height, inputPitch, input, locked.Pitch, locked.pBits);
-        }
-        else
-        {
-            loadRGBAUByteData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
-        }
-        break;
-      case GL_RGBA4:
-        loadRGBA4444Data(width, height, inputPitch, input, locked.Pitch, locked.pBits);
-        break;
-      case GL_RGB5_A1:
-        loadRGBA5551Data(width, height, inputPitch, input, locked.Pitch, locked.pBits);
-        break;
-      case GL_BGRA8_EXT:
-        loadBGRAData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
-        break;
-      // float textures are converted to RGBA, not BGRA, as they're stored that way in D3D
-      case GL_RGB32F_EXT:
-        loadRGBFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
-        break;
-      case GL_RGB16F_EXT:
-        loadRGBHalfFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
-        break;
-      case GL_RGBA32F_EXT:
-        loadRGBAFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
-        break;
-      case GL_RGBA16F_EXT:
-        loadRGBAHalfFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
-        break;
-      default: UNREACHABLE(); 
-    }
-
-    unlock();
-}
-
 void Image::loadAlphaData(GLsizei width, GLsizei height,
-                          int inputPitch, const void *input, size_t outputPitch, void *output) const
+                          int inputPitch, const void *input, size_t outputPitch, void *output)
 {
     const unsigned char *source = NULL;
     unsigned char *dest = NULL;
@@ -595,7 +46,7 @@
 }
 
 void Image::loadAlphaFloatData(GLsizei width, GLsizei height,
-                               int inputPitch, const void *input, size_t outputPitch, void *output) const
+                               int inputPitch, const void *input, size_t outputPitch, void *output)
 {
     const float *source = NULL;
     float *dest = NULL;
@@ -615,7 +66,7 @@
 }
 
 void Image::loadAlphaHalfFloatData(GLsizei width, GLsizei height,
-                                   int inputPitch, const void *input, size_t outputPitch, void *output) const
+                                   int inputPitch, const void *input, size_t outputPitch, void *output)
 {
     const unsigned short *source = NULL;
     unsigned short *dest = NULL;
@@ -635,7 +86,7 @@
 }
 
 void Image::loadLuminanceData(GLsizei width, GLsizei height,
-                              int inputPitch, const void *input, size_t outputPitch, void *output, bool native) const
+                              int inputPitch, const void *input, size_t outputPitch, void *output, bool native)
 {
     const unsigned char *source = NULL;
     unsigned char *dest = NULL;
@@ -663,7 +114,7 @@
 }
 
 void Image::loadLuminanceFloatData(GLsizei width, GLsizei height,
-                                   int inputPitch, const void *input, size_t outputPitch, void *output) const
+                                   int inputPitch, const void *input, size_t outputPitch, void *output)
 {
     const float *source = NULL;
     float *dest = NULL;
@@ -683,7 +134,7 @@
 }
 
 void Image::loadLuminanceHalfFloatData(GLsizei width, GLsizei height,
-                                       int inputPitch, const void *input, size_t outputPitch, void *output) const
+                                       int inputPitch, const void *input, size_t outputPitch, void *output)
 {
     const unsigned short *source = NULL;
     unsigned short *dest = NULL;
@@ -703,7 +154,7 @@
 }
 
 void Image::loadLuminanceAlphaData(GLsizei width, GLsizei height,
-                                   int inputPitch, const void *input, size_t outputPitch, void *output, bool native) const
+                                   int inputPitch, const void *input, size_t outputPitch, void *output, bool native)
 {
     const unsigned char *source = NULL;
     unsigned char *dest = NULL;
@@ -731,7 +182,7 @@
 }
 
 void Image::loadLuminanceAlphaFloatData(GLsizei width, GLsizei height,
-                                        int inputPitch, const void *input, size_t outputPitch, void *output) const
+                                        int inputPitch, const void *input, size_t outputPitch, void *output)
 {
     const float *source = NULL;
     float *dest = NULL;
@@ -751,7 +202,7 @@
 }
 
 void Image::loadLuminanceAlphaHalfFloatData(GLsizei width, GLsizei height,
-                                            int inputPitch, const void *input, size_t outputPitch, void *output) const
+                                            int inputPitch, const void *input, size_t outputPitch, void *output)
 {
     const unsigned short *source = NULL;
     unsigned short *dest = NULL;
@@ -771,7 +222,7 @@
 }
 
 void Image::loadRGBUByteData(GLsizei width, GLsizei height,
-                             int inputPitch, const void *input, size_t outputPitch, void *output) const
+                             int inputPitch, const void *input, size_t outputPitch, void *output)
 {
     const unsigned char *source = NULL;
     unsigned char *dest = NULL;
@@ -791,7 +242,7 @@
 }
 
 void Image::loadRGB565Data(GLsizei width, GLsizei height,
-                           int inputPitch, const void *input, size_t outputPitch, void *output) const
+                           int inputPitch, const void *input, size_t outputPitch, void *output)
 {
     const unsigned short *source = NULL;
     unsigned char *dest = NULL;
@@ -812,7 +263,7 @@
 }
 
 void Image::loadRGBFloatData(GLsizei width, GLsizei height,
-                             int inputPitch, const void *input, size_t outputPitch, void *output) const
+                             int inputPitch, const void *input, size_t outputPitch, void *output)
 {
     const float *source = NULL;
     float *dest = NULL;
@@ -832,7 +283,7 @@
 }
 
 void Image::loadRGBHalfFloatData(GLsizei width, GLsizei height,
-                                 int inputPitch, const void *input, size_t outputPitch, void *output) const
+                                 int inputPitch, const void *input, size_t outputPitch, void *output)
 {
     const unsigned short *source = NULL;
     unsigned short *dest = NULL;
@@ -852,7 +303,7 @@
 }
 
 void Image::loadRGBAUByteData(GLsizei width, GLsizei height,
-                              int inputPitch, const void *input, size_t outputPitch, void *output) const
+                              int inputPitch, const void *input, size_t outputPitch, void *output)
 {
     const unsigned int *source = NULL;
     unsigned int *dest = NULL;
@@ -870,7 +321,7 @@
 }
 
 void Image::loadRGBA4444Data(GLsizei width, GLsizei height,
-                             int inputPitch, const void *input, size_t outputPitch, void *output) const
+                             int inputPitch, const void *input, size_t outputPitch, void *output)
 {
     const unsigned short *source = NULL;
     unsigned char *dest = NULL;
@@ -891,7 +342,7 @@
 }
 
 void Image::loadRGBA5551Data(GLsizei width, GLsizei height,
-                             int inputPitch, const void *input, size_t outputPitch, void *output) const
+                             int inputPitch, const void *input, size_t outputPitch, void *output)
 {
     const unsigned short *source = NULL;
     unsigned char *dest = NULL;
@@ -912,7 +363,7 @@
 }
 
 void Image::loadRGBAFloatData(GLsizei width, GLsizei height,
-                              int inputPitch, const void *input, size_t outputPitch, void *output) const
+                              int inputPitch, const void *input, size_t outputPitch, void *output)
 {
     const float *source = NULL;
     float *dest = NULL;
@@ -926,7 +377,7 @@
 }
 
 void Image::loadRGBAHalfFloatData(GLsizei width, GLsizei height,
-                                  int inputPitch, const void *input, size_t outputPitch, void *output) const
+                                  int inputPitch, const void *input, size_t outputPitch, void *output)
 {
     const unsigned char *source = NULL;
     unsigned char *dest = NULL;
@@ -940,7 +391,7 @@
 }
 
 void Image::loadBGRAData(GLsizei width, GLsizei height,
-                         int inputPitch, const void *input, size_t outputPitch, void *output) const
+                         int inputPitch, const void *input, size_t outputPitch, void *output)
 {
     const unsigned char *source = NULL;
     unsigned char *dest = NULL;
@@ -953,282 +404,4 @@
     }
 }
 
-void Image::loadCompressedData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
-                               const void *input) {
-    ASSERT(xoffset % 4 == 0);
-    ASSERT(yoffset % 4 == 0);
-
-    RECT lockRect = {
-        xoffset, yoffset,
-        xoffset + width, yoffset + height
-    };
-
-    D3DLOCKED_RECT locked;
-    HRESULT result = lock(&locked, &lockRect);
-    if (FAILED(result))
-    {
-        return;
-    }
-
-    GLsizei inputSize = gl::ComputeCompressedSize(width, height, mInternalFormat);
-    GLsizei inputPitch = gl::ComputeCompressedPitch(width, mInternalFormat);
-    int rows = inputSize / inputPitch;
-    for (int i = 0; i < rows; ++i)
-    {
-        memcpy((void*)((BYTE*)locked.pBits + i * locked.Pitch), (void*)((BYTE*)input + i * inputPitch), inputPitch);
-    }
-
-    unlock();
 }
-
-// This implements glCopyTex[Sub]Image2D for non-renderable internal texture formats and incomplete textures
-void Image::copy(GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, gl::Framebuffer *source)
-{
-    RenderTarget9 *renderTarget = NULL;
-    IDirect3DSurface9 *surface = NULL;
-    gl::Renderbuffer *colorbuffer = source->getColorbuffer();
-
-    if (colorbuffer)
-    {
-        renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget());
-    }
-    
-    if (renderTarget)
-    {
-        surface = renderTarget->getSurface();
-    }
-
-    if (!surface)
-    {
-        ERR("Failed to retrieve the render target.");
-        return error(GL_OUT_OF_MEMORY);
-    }
-
-    IDirect3DDevice9 *device = mRenderer->getDevice(); // D3D9_REPLACE
-
-    IDirect3DSurface9 *renderTargetData = NULL;
-    D3DSURFACE_DESC description;
-    surface->GetDesc(&description);
-    
-    HRESULT result = device->CreateOffscreenPlainSurface(description.Width, description.Height, description.Format, D3DPOOL_SYSTEMMEM, &renderTargetData, NULL);
-
-    if (FAILED(result))
-    {
-        ERR("Could not create matching destination surface.");
-        surface->Release();
-        return error(GL_OUT_OF_MEMORY);
-    }
-
-    result = device->GetRenderTargetData(surface, renderTargetData);
-
-    if (FAILED(result))
-    {
-        ERR("GetRenderTargetData unexpectedly failed.");
-        renderTargetData->Release();
-        surface->Release();
-        return error(GL_OUT_OF_MEMORY);
-    }
-
-    RECT sourceRect = {x, y, x + width, y + height};
-    RECT destRect = {xoffset, yoffset, xoffset + width, yoffset + height};
-
-    D3DLOCKED_RECT sourceLock = {0};
-    result = renderTargetData->LockRect(&sourceLock, &sourceRect, 0);
-
-    if (FAILED(result))
-    {
-        ERR("Failed to lock the source surface (rectangle might be invalid).");
-        renderTargetData->Release();
-        surface->Release();
-        return error(GL_OUT_OF_MEMORY);
-    }
-
-    D3DLOCKED_RECT destLock = {0};
-    result = lock(&destLock, &destRect);
-    
-    if (FAILED(result))
-    {
-        ERR("Failed to lock the destination surface (rectangle might be invalid).");
-        renderTargetData->UnlockRect();
-        renderTargetData->Release();
-        surface->Release();
-        return error(GL_OUT_OF_MEMORY);
-    }
-
-    if (destLock.pBits && sourceLock.pBits)
-    {
-        unsigned char *source = (unsigned char*)sourceLock.pBits;
-        unsigned char *dest = (unsigned char*)destLock.pBits;
-
-        switch (description.Format)
-        {
-          case D3DFMT_X8R8G8B8:
-          case D3DFMT_A8R8G8B8:
-            switch(getD3DFormat())
-            {
-              case D3DFMT_X8R8G8B8:
-              case D3DFMT_A8R8G8B8:
-                for(int y = 0; y < height; y++)
-                {
-                    memcpy(dest, source, 4 * width);
-
-                    source += sourceLock.Pitch;
-                    dest += destLock.Pitch;
-                }
-                break;
-              case D3DFMT_L8:
-                for(int y = 0; y < height; y++)
-                {
-                    for(int x = 0; x < width; x++)
-                    {
-                        dest[x] = source[x * 4 + 2];
-                    }
-
-                    source += sourceLock.Pitch;
-                    dest += destLock.Pitch;
-                }
-                break;
-              case D3DFMT_A8L8:
-                for(int y = 0; y < height; y++)
-                {
-                    for(int x = 0; x < width; x++)
-                    {
-                        dest[x * 2 + 0] = source[x * 4 + 2];
-                        dest[x * 2 + 1] = source[x * 4 + 3];
-                    }
-
-                    source += sourceLock.Pitch;
-                    dest += destLock.Pitch;
-                }
-                break;
-              default:
-                UNREACHABLE();
-            }
-            break;
-          case D3DFMT_R5G6B5:
-            switch(getD3DFormat())
-            {
-              case D3DFMT_X8R8G8B8:
-                for(int y = 0; y < height; y++)
-                {
-                    for(int x = 0; x < width; x++)
-                    {
-                        unsigned short rgb = ((unsigned short*)source)[x];
-                        unsigned char red = (rgb & 0xF800) >> 8;
-                        unsigned char green = (rgb & 0x07E0) >> 3;
-                        unsigned char blue = (rgb & 0x001F) << 3;
-                        dest[x + 0] = blue | (blue >> 5);
-                        dest[x + 1] = green | (green >> 6);
-                        dest[x + 2] = red | (red >> 5);
-                        dest[x + 3] = 0xFF;
-                    }
-
-                    source += sourceLock.Pitch;
-                    dest += destLock.Pitch;
-                }
-                break;
-              case D3DFMT_L8:
-                for(int y = 0; y < height; y++)
-                {
-                    for(int x = 0; x < width; x++)
-                    {
-                        unsigned char red = source[x * 2 + 1] & 0xF8;
-                        dest[x] = red | (red >> 5);
-                    }
-
-                    source += sourceLock.Pitch;
-                    dest += destLock.Pitch;
-                }
-                break;
-              default:
-                UNREACHABLE();
-            }
-            break;
-          case D3DFMT_A1R5G5B5:
-            switch(getD3DFormat())
-            {
-              case D3DFMT_X8R8G8B8:
-                for(int y = 0; y < height; y++)
-                {
-                    for(int x = 0; x < width; x++)
-                    {
-                        unsigned short argb = ((unsigned short*)source)[x];
-                        unsigned char red = (argb & 0x7C00) >> 7;
-                        unsigned char green = (argb & 0x03E0) >> 2;
-                        unsigned char blue = (argb & 0x001F) << 3;
-                        dest[x + 0] = blue | (blue >> 5);
-                        dest[x + 1] = green | (green >> 5);
-                        dest[x + 2] = red | (red >> 5);
-                        dest[x + 3] = 0xFF;
-                    }
-
-                    source += sourceLock.Pitch;
-                    dest += destLock.Pitch;
-                }
-                break;
-              case D3DFMT_A8R8G8B8:
-                for(int y = 0; y < height; y++)
-                {
-                    for(int x = 0; x < width; x++)
-                    {
-                        unsigned short argb = ((unsigned short*)source)[x];
-                        unsigned char red = (argb & 0x7C00) >> 7;
-                        unsigned char green = (argb & 0x03E0) >> 2;
-                        unsigned char blue = (argb & 0x001F) << 3;
-                        unsigned char alpha = (signed short)argb >> 15;
-                        dest[x + 0] = blue | (blue >> 5);
-                        dest[x + 1] = green | (green >> 5);
-                        dest[x + 2] = red | (red >> 5);
-                        dest[x + 3] = alpha;
-                    }
-
-                    source += sourceLock.Pitch;
-                    dest += destLock.Pitch;
-                }
-                break;
-              case D3DFMT_L8:
-                for(int y = 0; y < height; y++)
-                {
-                    for(int x = 0; x < width; x++)
-                    {
-                        unsigned char red = source[x * 2 + 1] & 0x7C;
-                        dest[x] = (red << 1) | (red >> 4);
-                    }
-
-                    source += sourceLock.Pitch;
-                    dest += destLock.Pitch;
-                }
-                break;
-              case D3DFMT_A8L8:
-                for(int y = 0; y < height; y++)
-                {
-                    for(int x = 0; x < width; x++)
-                    {
-                        unsigned char red = source[x * 2 + 1] & 0x7C;
-                        dest[x * 2 + 0] = (red << 1) | (red >> 4);
-                        dest[x * 2 + 1] = (signed char)source[x * 2 + 1] >> 7;
-                    }
-
-                    source += sourceLock.Pitch;
-                    dest += destLock.Pitch;
-                }
-                break;
-              default:
-                UNREACHABLE();
-            }
-            break;
-          default:
-            UNREACHABLE();
-        }
-    }
-
-    unlock();
-    renderTargetData->UnlockRect();
-
-    renderTargetData->Release();
-    surface->Release();
-
-    mDirty = true;
-}
-
-}
\ No newline at end of file
diff --git a/src/libGLESv2/renderer/Image.h b/src/libGLESv2/renderer/Image.h
index acbc57e..cb75f82 100644
--- a/src/libGLESv2/renderer/Image.h
+++ b/src/libGLESv2/renderer/Image.h
@@ -4,16 +4,13 @@
 // found in the LICENSE file.
 //
 
-// Image.h: Defines the rx::Image class, which acts as the interface to
-// the actual underlying surfaces of a Texture.
+// Image.h: Defines the rx::Image class, an abstract base class for the 
+// renderer-specific classes which will define the interface to the underlying
+// surfaces or resources.
 
 #ifndef LIBGLESV2_RENDERER_IMAGE_H_
 #define LIBGLESV2_RENDERER_IMAGE_H_
 
-#define GL_APICALL
-#include <GLES2/gl2.h>
-#include <d3d9.h>
-
 #include "common/debug.h"
 
 namespace gl
@@ -24,7 +21,6 @@
 namespace rx
 {
 class Renderer;
-class Renderer9;
 class TextureStorage2D;
 class TextureStorageCubeMap;
 
@@ -32,104 +28,88 @@
 {
   public:
     Image();
-    ~Image();
+    virtual ~Image() {};
 
-    static void generateMipmap(Image *dest, Image *source);
-    static void copyLockableSurfaces(IDirect3DSurface9 *dest, IDirect3DSurface9 *source);
+    GLsizei getWidth() const { return mWidth; }
+    GLsizei getHeight() const { return mHeight; }
+    GLenum getInternalFormat() const { return mInternalFormat; }
+    GLenum getActualFormat() const { return mActualFormat; }
 
-    bool redefine(Renderer *renderer, GLint internalformat, GLsizei width, GLsizei height, bool forceRelease);
     void markDirty() {mDirty = true;}
     void markClean() {mDirty = false;}
+    virtual bool isDirty() const = 0;
 
-    bool isRenderableFormat() const;
-    D3DFORMAT getD3DFormat() const;
-    GLenum getActualFormat() const;
+    virtual void setManagedSurface(TextureStorage2D *storage, int level) {};
+    virtual void setManagedSurface(TextureStorageCubeMap *storage, int face, int level) {};
+    virtual bool updateSurface(TextureStorage2D *storage, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height) = 0;
+    virtual bool updateSurface(TextureStorageCubeMap *storage, int face, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height) = 0;
 
-    GLsizei getWidth() const {return mWidth;}
-    GLsizei getHeight() const {return mHeight;}
-    GLenum getInternalFormat() const {return mInternalFormat;}
-    bool isDirty() const {return mSurface && mDirty;}
-    IDirect3DSurface9 *getSurface();
+    virtual bool redefine(Renderer *renderer, GLint internalformat, GLsizei width, GLsizei height, bool forceRelease) = 0;
 
-    void setManagedSurface(TextureStorage2D *storage, int level);
-    void setManagedSurface(TextureStorageCubeMap *storage, int face, int level);
-    bool updateSurface(TextureStorage2D *storage, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height);
-    bool updateSurface(TextureStorageCubeMap *storage, int face, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height);
+    virtual bool isRenderableFormat() const = 0;
+    
+    virtual void loadData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
+                  GLint unpackAlignment, const void *input) = 0;
+    virtual void loadCompressedData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
+                                    const void *input) = 0;
 
-    void loadData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
-                  GLint unpackAlignment, const void *input);
+    virtual void copy(GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, gl::Framebuffer *source) = 0;
 
-    void loadAlphaData(GLsizei width, GLsizei height,
-                       int inputPitch, const void *input, size_t outputPitch, void *output) const;
-    void loadAlphaDataSSE2(GLsizei width, GLsizei height,
-                           int inputPitch, const void *input, size_t outputPitch, void *output) const;
-    void loadAlphaFloatData(GLsizei width, GLsizei height,
-                            int inputPitch, const void *input, size_t outputPitch, void *output) const;
-    void loadAlphaHalfFloatData(GLsizei width, GLsizei height,
-                                int inputPitch, const void *input, size_t outputPitch, void *output) const;
-    void loadLuminanceData(GLsizei width, GLsizei height,
-                           int inputPitch, const void *input, size_t outputPitch, void *output, bool native) const;
-    void loadLuminanceFloatData(GLsizei width, GLsizei height,
-                                int inputPitch, const void *input, size_t outputPitch, void *output) const;
-    void loadLuminanceHalfFloatData(GLsizei width, GLsizei height,
-                                    int inputPitch, const void *input, size_t outputPitch, void *output) const;
-    void loadLuminanceAlphaData(GLsizei width, GLsizei height,
-                                int inputPitch, const void *input, size_t outputPitch, void *output, bool native) const;
-    void loadLuminanceAlphaFloatData(GLsizei width, GLsizei height,
-                                     int inputPitch, const void *input, size_t outputPitch, void *output) const;
-    void loadLuminanceAlphaHalfFloatData(GLsizei width, GLsizei height,
-                                         int inputPitch, const void *input, size_t outputPitch, void *output) const;
-    void loadRGBUByteData(GLsizei width, GLsizei height,
-                          int inputPitch, const void *input, size_t outputPitch, void *output) const;
-    void loadRGB565Data(GLsizei width, GLsizei height,
-                        int inputPitch, const void *input, size_t outputPitch, void *output) const;
-    void loadRGBFloatData(GLsizei width, GLsizei height,
-                          int inputPitch, const void *input, size_t outputPitch, void *output) const;
-    void loadRGBHalfFloatData(GLsizei width, GLsizei height,
-                              int inputPitch, const void *input, size_t outputPitch, void *output) const;
-    void loadRGBAUByteDataSSE2(GLsizei width, GLsizei height,
-                               int inputPitch, const void *input, size_t outputPitch, void *output) const;
-    void loadRGBAUByteData(GLsizei width, GLsizei height,
-                           int inputPitch, const void *input, size_t outputPitch, void *output) const;
-    void loadRGBA4444Data(GLsizei width, GLsizei height,
-                          int inputPitch, const void *input, size_t outputPitch, void *output) const;
-    void loadRGBA5551Data(GLsizei width, GLsizei height,
-                          int inputPitch, const void *input, size_t outputPitch, void *output) const;
-    void loadRGBAFloatData(GLsizei width, GLsizei height,
-                           int inputPitch, const void *input, size_t outputPitch, void *output) const;
-    void loadRGBAHalfFloatData(GLsizei width, GLsizei height,
-                               int inputPitch, const void *input, size_t outputPitch, void *output) const;
-    void loadBGRAData(GLsizei width, GLsizei height,
-                      int inputPitch, const void *input, size_t outputPitch, void *output) const;
-    void loadCompressedData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
-                            const void *input);
+    static void loadAlphaData(GLsizei width, GLsizei height,
+                              int inputPitch, const void *input, size_t outputPitch, void *output);
+    static void loadAlphaDataSSE2(GLsizei width, GLsizei height,
+                                  int inputPitch, const void *input, size_t outputPitch, void *output);
+    static void loadAlphaFloatData(GLsizei width, GLsizei height,
+                                   int inputPitch, const void *input, size_t outputPitch, void *output);
+    static void loadAlphaHalfFloatData(GLsizei width, GLsizei height,
+                                       int inputPitch, const void *input, size_t outputPitch, void *output);
+    static void loadLuminanceData(GLsizei width, GLsizei height,
+                                  int inputPitch, const void *input, size_t outputPitch, void *output, bool native);
+    static void loadLuminanceFloatData(GLsizei width, GLsizei height,
+                                       int inputPitch, const void *input, size_t outputPitch, void *output);
+    static void loadLuminanceHalfFloatData(GLsizei width, GLsizei height,
+                                           int inputPitch, const void *input, size_t outputPitch, void *output);
+    static void loadLuminanceAlphaData(GLsizei width, GLsizei height,
+                                       int inputPitch, const void *input, size_t outputPitch, void *output, bool native);
+    static void loadLuminanceAlphaFloatData(GLsizei width, GLsizei height,
+                                            int inputPitch, const void *input, size_t outputPitch, void *output);
+    static void loadLuminanceAlphaHalfFloatData(GLsizei width, GLsizei height,
+                                                int inputPitch, const void *input, size_t outputPitch, void *output);
+    static void loadRGBUByteData(GLsizei width, GLsizei height,
+                                 int inputPitch, const void *input, size_t outputPitch, void *output);
+    static void loadRGB565Data(GLsizei width, GLsizei height,
+                               int inputPitch, const void *input, size_t outputPitch, void *output);
+    static void loadRGBFloatData(GLsizei width, GLsizei height,
+                                 int inputPitch, const void *input, size_t outputPitch, void *output);
+    static void loadRGBHalfFloatData(GLsizei width, GLsizei height,
+                                     int inputPitch, const void *input, size_t outputPitch, void *output);
+    static void loadRGBAUByteDataSSE2(GLsizei width, GLsizei height,
+                                      int inputPitch, const void *input, size_t outputPitch, void *output);
+    static void loadRGBAUByteData(GLsizei width, GLsizei height,
+                                  int inputPitch, const void *input, size_t outputPitch, void *output);
+    static void loadRGBA4444Data(GLsizei width, GLsizei height,
+                                 int inputPitch, const void *input, size_t outputPitch, void *output);
+    static void loadRGBA5551Data(GLsizei width, GLsizei height,
+                                 int inputPitch, const void *input, size_t outputPitch, void *output);
+    static void loadRGBAFloatData(GLsizei width, GLsizei height,
+                                  int inputPitch, const void *input, size_t outputPitch, void *output);
+    static void loadRGBAHalfFloatData(GLsizei width, GLsizei height,
+                                      int inputPitch, const void *input, size_t outputPitch, void *output);
+    static void loadBGRAData(GLsizei width, GLsizei height,
+                             int inputPitch, const void *input, size_t outputPitch, void *output);
 
-    void copy(GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, gl::Framebuffer *source);
-
-  private:
-    DISALLOW_COPY_AND_ASSIGN(Image);
-
-    void createSurface();
-    void setManagedSurface(IDirect3DSurface9 *surface);
-    bool updateSurface(IDirect3DSurface9 *dest, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height);
-
-    HRESULT lock(D3DLOCKED_RECT *lockedRect, const RECT *rect);
-    void unlock();
-
+  protected:
     GLsizei mWidth;
     GLsizei mHeight;
     GLint mInternalFormat;
+    GLenum mActualFormat;
 
     bool mDirty;
 
-    Renderer9 *mRenderer;
-
-    D3DPOOL mD3DPool;   // can only be D3DPOOL_SYSTEMMEM or D3DPOOL_MANAGED since it needs to be lockable.
-    D3DFORMAT mD3DFormat;
-    GLenum mActualFormat;
-
-    IDirect3DSurface9 *mSurface;
+  private:
+    DISALLOW_COPY_AND_ASSIGN(Image);
 };
+
 }
 
-#endif   // LIBGLESV2_RENDERER_IMAGE_H_
+#endif // LIBGLESV2_RENDERER_IMAGE_H_
diff --git a/src/libGLESv2/renderer/Image9.cpp b/src/libGLESv2/renderer/Image9.cpp
new file mode 100644
index 0000000..1007522
--- /dev/null
+++ b/src/libGLESv2/renderer/Image9.cpp
@@ -0,0 +1,851 @@
+//
+// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+//
+
+// Image9.cpp: Implements the rx::Image9 class, which acts as the interface to
+// the actual underlying surfaces of a Texture.
+
+#include "libGLESv2/renderer/Image9.h"
+
+#include "libEGL/Display.h"
+
+#include "libGLESv2/main.h"
+#include "libGLESv2/mathutil.h"
+#include "libGLESv2/utilities.h"
+#include "libGLESv2/Texture.h"
+#include "libGLESv2/Framebuffer.h"
+#include "libGLESv2/renderer/RenderTarget9.h"
+
+#include "libGLESv2/renderer/renderer9_utils.h"
+
+namespace rx
+{
+
+namespace
+{
+struct L8
+{
+    unsigned char L;
+
+    static void average(L8 *dst, const L8 *src1, const L8 *src2)
+    {
+        dst->L = ((src1->L ^ src2->L) >> 1) + (src1->L & src2->L);
+    }
+};
+
+struct A8L8
+{
+    unsigned char L;
+    unsigned char A;
+
+    static void average(A8L8 *dst, const A8L8 *src1, const A8L8 *src2)
+    {
+        *(unsigned short*)dst = (((*(unsigned short*)src1 ^ *(unsigned short*)src2) & 0xFEFE) >> 1) + (*(unsigned short*)src1 & *(unsigned short*)src2);
+    }
+};
+
+struct A8R8G8B8
+{
+    unsigned char B;
+    unsigned char G;
+    unsigned char R;
+    unsigned char A;
+
+    static void average(A8R8G8B8 *dst, const A8R8G8B8 *src1, const A8R8G8B8 *src2)
+    {
+        *(unsigned int*)dst = (((*(unsigned int*)src1 ^ *(unsigned int*)src2) & 0xFEFEFEFE) >> 1) + (*(unsigned int*)src1 & *(unsigned int*)src2);
+    }
+};
+
+struct A16B16G16R16F
+{
+    unsigned short R;
+    unsigned short G;
+    unsigned short B;
+    unsigned short A;
+
+    static void average(A16B16G16R16F *dst, const A16B16G16R16F *src1, const A16B16G16R16F *src2)
+    {
+        dst->R = gl::float32ToFloat16((gl::float16ToFloat32(src1->R) + gl::float16ToFloat32(src2->R)) * 0.5f);
+        dst->G = gl::float32ToFloat16((gl::float16ToFloat32(src1->G) + gl::float16ToFloat32(src2->G)) * 0.5f);
+        dst->B = gl::float32ToFloat16((gl::float16ToFloat32(src1->B) + gl::float16ToFloat32(src2->B)) * 0.5f);
+        dst->A = gl::float32ToFloat16((gl::float16ToFloat32(src1->A) + gl::float16ToFloat32(src2->A)) * 0.5f);
+    }
+};
+
+struct A32B32G32R32F
+{
+    float R;
+    float G;
+    float B;
+    float A;
+
+    static void average(A32B32G32R32F *dst, const A32B32G32R32F *src1, const A32B32G32R32F *src2)
+    {
+        dst->R = (src1->R + src2->R) * 0.5f;
+        dst->G = (src1->G + src2->G) * 0.5f;
+        dst->B = (src1->B + src2->B) * 0.5f;
+        dst->A = (src1->A + src2->A) * 0.5f;
+    }
+};
+
+template <typename T>
+void GenerateMip(unsigned int sourceWidth, unsigned int sourceHeight,
+                 const unsigned char *sourceData, int sourcePitch,
+                 unsigned char *destData, int destPitch)
+{
+    unsigned int mipWidth = std::max(1U, sourceWidth >> 1);
+    unsigned int mipHeight = std::max(1U, sourceHeight >> 1);
+
+    if (sourceHeight == 1)
+    {
+        ASSERT(sourceWidth != 1);
+
+        const T *src = (const T*)sourceData;
+        T *dst = (T*)destData;
+
+        for (unsigned int x = 0; x < mipWidth; x++)
+        {
+            T::average(&dst[x], &src[x * 2], &src[x * 2 + 1]);
+        }
+    }
+    else if (sourceWidth == 1)
+    {
+        ASSERT(sourceHeight != 1);
+
+        for (unsigned int y = 0; y < mipHeight; y++)
+        {
+            const T *src0 = (const T*)(sourceData + y * 2 * sourcePitch);
+            const T *src1 = (const T*)(sourceData + y * 2 * sourcePitch + sourcePitch);
+            T *dst = (T*)(destData + y * destPitch);
+
+            T::average(dst, src0, src1);
+        }
+    }
+    else
+    {
+        for (unsigned int y = 0; y < mipHeight; y++)
+        {
+            const T *src0 = (const T*)(sourceData + y * 2 * sourcePitch);
+            const T *src1 = (const T*)(sourceData + y * 2 * sourcePitch + sourcePitch);
+            T *dst = (T*)(destData + y * destPitch);
+
+            for (unsigned int x = 0; x < mipWidth; x++)
+            {
+                T tmp0;
+                T tmp1;
+
+                T::average(&tmp0, &src0[x * 2], &src0[x * 2 + 1]);
+                T::average(&tmp1, &src1[x * 2], &src1[x * 2 + 1]);
+                T::average(&dst[x], &tmp0, &tmp1);
+            }
+        }
+    }
+}
+
+void GenerateMip(IDirect3DSurface9 *destSurface, IDirect3DSurface9 *sourceSurface)
+{
+    D3DSURFACE_DESC destDesc;
+    HRESULT result = destSurface->GetDesc(&destDesc);
+    ASSERT(SUCCEEDED(result));
+
+    D3DSURFACE_DESC sourceDesc;
+    result = sourceSurface->GetDesc(&sourceDesc);
+    ASSERT(SUCCEEDED(result));
+
+    ASSERT(sourceDesc.Format == destDesc.Format);
+    ASSERT(sourceDesc.Width == 1 || sourceDesc.Width / 2 == destDesc.Width);
+    ASSERT(sourceDesc.Height == 1 || sourceDesc.Height / 2 == destDesc.Height);
+
+    D3DLOCKED_RECT sourceLocked = {0};
+    result = sourceSurface->LockRect(&sourceLocked, NULL, D3DLOCK_READONLY);
+    ASSERT(SUCCEEDED(result));
+
+    D3DLOCKED_RECT destLocked = {0};
+    result = destSurface->LockRect(&destLocked, NULL, 0);
+    ASSERT(SUCCEEDED(result));
+
+    const unsigned char *sourceData = reinterpret_cast<const unsigned char*>(sourceLocked.pBits);
+    unsigned char *destData = reinterpret_cast<unsigned char*>(destLocked.pBits);
+
+    if (sourceData && destData)
+    {
+        switch (sourceDesc.Format)
+        {
+          case D3DFMT_L8:
+            GenerateMip<L8>(sourceDesc.Width, sourceDesc.Height, sourceData, sourceLocked.Pitch, destData, destLocked.Pitch);
+            break;
+          case D3DFMT_A8L8:
+            GenerateMip<A8L8>(sourceDesc.Width, sourceDesc.Height, sourceData, sourceLocked.Pitch, destData, destLocked.Pitch);
+            break;
+          case D3DFMT_A8R8G8B8:
+          case D3DFMT_X8R8G8B8:
+            GenerateMip<A8R8G8B8>(sourceDesc.Width, sourceDesc.Height, sourceData, sourceLocked.Pitch, destData, destLocked.Pitch);
+            break;
+          case D3DFMT_A16B16G16R16F:
+            GenerateMip<A16B16G16R16F>(sourceDesc.Width, sourceDesc.Height, sourceData, sourceLocked.Pitch, destData, destLocked.Pitch);
+            break;
+          case D3DFMT_A32B32G32R32F:
+            GenerateMip<A32B32G32R32F>(sourceDesc.Width, sourceDesc.Height, sourceData, sourceLocked.Pitch, destData, destLocked.Pitch);
+            break;
+          default:
+            UNREACHABLE();
+            break;
+        }
+
+        destSurface->UnlockRect();
+        sourceSurface->UnlockRect();
+    }
+}
+}
+
+Image9::Image9()
+{
+    mSurface = NULL;
+    mRenderer = NULL;
+
+    mD3DPool = D3DPOOL_SYSTEMMEM;
+    mD3DFormat = D3DFMT_UNKNOWN;
+}
+
+Image9::~Image9()
+{
+    if (mSurface)
+    {
+        mSurface->Release();
+    }
+}
+
+Image9 *Image9::makeImage9(Image *img)
+{
+    ASSERT(dynamic_cast<rx::Image9*>(img) != NULL);
+    return static_cast<rx::Image9*>(img);
+}
+
+void Image9::generateMipmap(Image9 *dest, Image9 *source)
+{
+    IDirect3DSurface9 *sourceSurface = source->getSurface();
+    if (sourceSurface == NULL)
+        return error(GL_OUT_OF_MEMORY);
+
+    IDirect3DSurface9 *destSurface = dest->getSurface();
+    GenerateMip(destSurface, sourceSurface);
+
+    source->markDirty();
+}
+
+void Image9::copyLockableSurfaces(IDirect3DSurface9 *dest, IDirect3DSurface9 *source)
+{
+    D3DLOCKED_RECT sourceLock = {0};
+    D3DLOCKED_RECT destLock = {0};
+    
+    source->LockRect(&sourceLock, NULL, 0);
+    dest->LockRect(&destLock, NULL, 0);
+    
+    if (sourceLock.pBits && destLock.pBits)
+    {
+        D3DSURFACE_DESC desc;
+        source->GetDesc(&desc);
+
+        int rows = dx::IsCompressedFormat(desc.Format) ? desc.Height / 4 : desc.Height;
+        int bytes = dx::ComputeRowSize(desc.Format, desc.Width);
+        ASSERT(bytes <= sourceLock.Pitch && bytes <= destLock.Pitch);
+
+        for(int i = 0; i < rows; i++)
+        {
+            memcpy((char*)destLock.pBits + destLock.Pitch * i, (char*)sourceLock.pBits + sourceLock.Pitch * i, bytes);
+        }
+
+        source->UnlockRect();
+        dest->UnlockRect();
+    }
+    else UNREACHABLE();
+}
+
+bool Image9::redefine(rx::Renderer *renderer, GLint internalformat, GLsizei width, GLsizei height, bool forceRelease)
+{
+    if (mWidth != width ||
+        mHeight != height ||
+        mInternalFormat != internalformat ||
+        forceRelease)
+    {
+        mRenderer = Renderer9::makeRenderer9(renderer);
+
+        mWidth = width;
+        mHeight = height;
+        mInternalFormat = internalformat;
+        // compute the d3d format that will be used
+        mD3DFormat = mRenderer->ConvertTextureInternalFormat(internalformat);
+        mActualFormat = d3d9_gl::GetEquivalentFormat(mD3DFormat);
+
+        if (mSurface)
+        {
+            mSurface->Release();
+            mSurface = NULL;
+        }
+
+        return true;
+    }
+
+    return false;
+}
+
+void Image9::createSurface()
+{
+    if(mSurface)
+    {
+        return;
+    }
+
+    IDirect3DTexture9 *newTexture = NULL;
+    IDirect3DSurface9 *newSurface = NULL;
+    const D3DPOOL poolToUse = D3DPOOL_SYSTEMMEM;
+    const D3DFORMAT d3dFormat = getD3DFormat();
+    ASSERT(d3dFormat != D3DFMT_INTZ); // We should never get here for depth textures
+
+    if (mWidth != 0 && mHeight != 0)
+    {
+        int levelToFetch = 0;
+        GLsizei requestWidth = mWidth;
+        GLsizei requestHeight = mHeight;
+        gl::MakeValidSize(true, gl::IsCompressed(mInternalFormat), &requestWidth, &requestHeight, &levelToFetch);
+
+        IDirect3DDevice9 *device = mRenderer->getDevice(); // D3D9_REPLACE
+
+        HRESULT result = device->CreateTexture(requestWidth, requestHeight, levelToFetch + 1, 0, d3dFormat,
+                                                    poolToUse, &newTexture, NULL);
+
+        if (FAILED(result))
+        {
+            ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
+            ERR("Creating image surface failed.");
+            return error(GL_OUT_OF_MEMORY);
+        }
+
+        newTexture->GetSurfaceLevel(levelToFetch, &newSurface);
+        newTexture->Release();
+    }
+
+    mSurface = newSurface;
+    mDirty = false;
+    mD3DPool = poolToUse;
+}
+
+HRESULT Image9::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 Image9::unlock()
+{
+    if (mSurface)
+    {
+        HRESULT result = mSurface->UnlockRect();
+        ASSERT(SUCCEEDED(result));
+    }
+}
+
+bool Image9::isRenderableFormat() const
+{    
+    return TextureStorage::IsTextureFormatRenderable(getD3DFormat());
+}
+
+D3DFORMAT Image9::getD3DFormat() const
+{
+    // this should only happen if the image hasn't been redefined first
+    // which would be a bug by the caller
+    ASSERT(mD3DFormat != D3DFMT_UNKNOWN);
+
+    return mD3DFormat;
+}
+
+IDirect3DSurface9 *Image9::getSurface()
+{
+    createSurface();
+
+    return mSurface;
+}
+
+void Image9::setManagedSurface(TextureStorage2D *storage, int level)
+{
+    setManagedSurface(storage->getSurfaceLevel(level, false));
+}
+
+void Image9::setManagedSurface(TextureStorageCubeMap *storage, int face, int level)
+{
+    setManagedSurface(storage->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, false));
+}
+
+void Image9::setManagedSurface(IDirect3DSurface9 *surface)
+{
+    D3DSURFACE_DESC desc;
+    surface->GetDesc(&desc);
+    ASSERT(desc.Pool == D3DPOOL_MANAGED);
+
+    if ((GLsizei)desc.Width == mWidth && (GLsizei)desc.Height == mHeight)
+    {
+        if (mSurface)
+        {
+            copyLockableSurfaces(surface, mSurface);
+            mSurface->Release();
+        }
+
+        mSurface = surface;
+        mD3DPool = desc.Pool;
+    }
+}
+
+bool Image9::updateSurface(TextureStorage2D *storage, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
+{
+    ASSERT(getSurface() != NULL);
+    return updateSurface(storage->getSurfaceLevel(level, true), xoffset, yoffset, width, height);
+}
+
+bool Image9::updateSurface(TextureStorageCubeMap *storage, int face, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
+{
+    ASSERT(getSurface() != NULL);
+    return updateSurface(storage->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, true), xoffset, yoffset, width, height);
+}
+
+bool Image9::updateSurface(IDirect3DSurface9 *destSurface, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
+{
+    if (!destSurface)
+        return false;
+
+    IDirect3DSurface9 *sourceSurface = getSurface();
+
+    if (sourceSurface && sourceSurface != destSurface)
+    {
+        RECT rect;
+        rect.left = xoffset;
+        rect.top = yoffset;
+        rect.right = xoffset + width;
+        rect.bottom = yoffset + height;
+
+        POINT point = {rect.left, rect.top};
+
+        IDirect3DDevice9 *device = mRenderer->getDevice(); // D3D9_REPLACE
+
+        if (mD3DPool == D3DPOOL_MANAGED)
+        {
+            D3DSURFACE_DESC desc;
+            sourceSurface->GetDesc(&desc);
+
+            IDirect3DSurface9 *surf = 0;
+            HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL);
+
+            if (SUCCEEDED(result))
+            {
+                copyLockableSurfaces(surf, sourceSurface);
+                result = device->UpdateSurface(surf, &rect, destSurface, &point);
+                ASSERT(SUCCEEDED(result));
+                surf->Release();
+            }
+        }
+        else
+        {
+            // UpdateSurface: source must be SYSTEMMEM, dest must be DEFAULT pools 
+            HRESULT result = device->UpdateSurface(sourceSurface, &rect, destSurface, &point);
+            ASSERT(SUCCEEDED(result));
+        }
+    }
+
+    destSurface->Release();
+    return true;
+}
+
+// Store the pixel rectangle designated by xoffset,yoffset,width,height with pixels stored as format/type at input
+// into the target pixel rectangle.
+void Image9::loadData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
+                      GLint unpackAlignment, const void *input)
+{
+    RECT lockRect =
+    {
+        xoffset, yoffset,
+        xoffset + width, yoffset + height
+    };
+
+    D3DLOCKED_RECT locked;
+    HRESULT result = lock(&locked, &lockRect);
+    if (FAILED(result))
+    {
+        return;
+    }
+
+
+    GLsizei inputPitch = gl::ComputePitch(width, mInternalFormat, unpackAlignment);
+
+    switch (mInternalFormat)
+    {
+      case GL_ALPHA8_EXT:
+        if (gl::supportsSSE2())
+        {
+            loadAlphaDataSSE2(width, height, inputPitch, input, locked.Pitch, locked.pBits);
+        }
+        else
+        {
+            loadAlphaData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
+        }
+        break;
+      case GL_LUMINANCE8_EXT:
+        loadLuminanceData(width, height, inputPitch, input, locked.Pitch, locked.pBits, getD3DFormat() == D3DFMT_L8);
+        break;
+      case GL_ALPHA32F_EXT:
+        loadAlphaFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
+        break;
+      case GL_LUMINANCE32F_EXT:
+        loadLuminanceFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
+        break;
+      case GL_ALPHA16F_EXT:
+        loadAlphaHalfFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
+        break;
+      case GL_LUMINANCE16F_EXT:
+        loadLuminanceHalfFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
+        break;
+      case GL_LUMINANCE8_ALPHA8_EXT:
+        loadLuminanceAlphaData(width, height, inputPitch, input, locked.Pitch, locked.pBits, getD3DFormat() == D3DFMT_A8L8);
+        break;
+      case GL_LUMINANCE_ALPHA32F_EXT:
+        loadLuminanceAlphaFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
+        break;
+      case GL_LUMINANCE_ALPHA16F_EXT:
+        loadLuminanceAlphaHalfFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
+        break;
+      case GL_RGB8_OES:
+        loadRGBUByteData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
+        break;
+      case GL_RGB565:
+        loadRGB565Data(width, height, inputPitch, input, locked.Pitch, locked.pBits);
+        break;
+      case GL_RGBA8_OES:
+        if (gl::supportsSSE2())
+        {
+            loadRGBAUByteDataSSE2(width, height, inputPitch, input, locked.Pitch, locked.pBits);
+        }
+        else
+        {
+            loadRGBAUByteData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
+        }
+        break;
+      case GL_RGBA4:
+        loadRGBA4444Data(width, height, inputPitch, input, locked.Pitch, locked.pBits);
+        break;
+      case GL_RGB5_A1:
+        loadRGBA5551Data(width, height, inputPitch, input, locked.Pitch, locked.pBits);
+        break;
+      case GL_BGRA8_EXT:
+        loadBGRAData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
+        break;
+      // float textures are converted to RGBA, not BGRA, as they're stored that way in D3D
+      case GL_RGB32F_EXT:
+        loadRGBFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
+        break;
+      case GL_RGB16F_EXT:
+        loadRGBHalfFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
+        break;
+      case GL_RGBA32F_EXT:
+        loadRGBAFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
+        break;
+      case GL_RGBA16F_EXT:
+        loadRGBAHalfFloatData(width, height, inputPitch, input, locked.Pitch, locked.pBits);
+        break;
+      default: UNREACHABLE(); 
+    }
+
+    unlock();
+}
+
+void Image9::loadCompressedData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
+                                const void *input)
+{
+    ASSERT(xoffset % 4 == 0);
+    ASSERT(yoffset % 4 == 0);
+
+    RECT lockRect = {
+        xoffset, yoffset,
+        xoffset + width, yoffset + height
+    };
+
+    D3DLOCKED_RECT locked;
+    HRESULT result = lock(&locked, &lockRect);
+    if (FAILED(result))
+    {
+        return;
+    }
+
+    GLsizei inputSize = gl::ComputeCompressedSize(width, height, mInternalFormat);
+    GLsizei inputPitch = gl::ComputeCompressedPitch(width, mInternalFormat);
+    int rows = inputSize / inputPitch;
+    for (int i = 0; i < rows; ++i)
+    {
+        memcpy((void*)((BYTE*)locked.pBits + i * locked.Pitch), (void*)((BYTE*)input + i * inputPitch), inputPitch);
+    }
+
+    unlock();
+}
+
+// This implements glCopyTex[Sub]Image2D for non-renderable internal texture formats and incomplete textures
+void Image9::copy(GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, gl::Framebuffer *source)
+{
+    RenderTarget9 *renderTarget = NULL;
+    IDirect3DSurface9 *surface = NULL;
+    gl::Renderbuffer *colorbuffer = source->getColorbuffer();
+
+    if (colorbuffer)
+    {
+        renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget());
+    }
+    
+    if (renderTarget)
+    {
+        surface = renderTarget->getSurface();
+    }
+
+    if (!surface)
+    {
+        ERR("Failed to retrieve the render target.");
+        return error(GL_OUT_OF_MEMORY);
+    }
+
+    IDirect3DDevice9 *device = mRenderer->getDevice(); // D3D9_REPLACE
+
+    IDirect3DSurface9 *renderTargetData = NULL;
+    D3DSURFACE_DESC description;
+    surface->GetDesc(&description);
+    
+    HRESULT result = device->CreateOffscreenPlainSurface(description.Width, description.Height, description.Format, D3DPOOL_SYSTEMMEM, &renderTargetData, NULL);
+
+    if (FAILED(result))
+    {
+        ERR("Could not create matching destination surface.");
+        surface->Release();
+        return error(GL_OUT_OF_MEMORY);
+    }
+
+    result = device->GetRenderTargetData(surface, renderTargetData);
+
+    if (FAILED(result))
+    {
+        ERR("GetRenderTargetData unexpectedly failed.");
+        renderTargetData->Release();
+        surface->Release();
+        return error(GL_OUT_OF_MEMORY);
+    }
+
+    RECT sourceRect = {x, y, x + width, y + height};
+    RECT destRect = {xoffset, yoffset, xoffset + width, yoffset + height};
+
+    D3DLOCKED_RECT sourceLock = {0};
+    result = renderTargetData->LockRect(&sourceLock, &sourceRect, 0);
+
+    if (FAILED(result))
+    {
+        ERR("Failed to lock the source surface (rectangle might be invalid).");
+        renderTargetData->Release();
+        surface->Release();
+        return error(GL_OUT_OF_MEMORY);
+    }
+
+    D3DLOCKED_RECT destLock = {0};
+    result = lock(&destLock, &destRect);
+    
+    if (FAILED(result))
+    {
+        ERR("Failed to lock the destination surface (rectangle might be invalid).");
+        renderTargetData->UnlockRect();
+        renderTargetData->Release();
+        surface->Release();
+        return error(GL_OUT_OF_MEMORY);
+    }
+
+    if (destLock.pBits && sourceLock.pBits)
+    {
+        unsigned char *source = (unsigned char*)sourceLock.pBits;
+        unsigned char *dest = (unsigned char*)destLock.pBits;
+
+        switch (description.Format)
+        {
+          case D3DFMT_X8R8G8B8:
+          case D3DFMT_A8R8G8B8:
+            switch(getD3DFormat())
+            {
+              case D3DFMT_X8R8G8B8:
+              case D3DFMT_A8R8G8B8:
+                for(int y = 0; y < height; y++)
+                {
+                    memcpy(dest, source, 4 * width);
+
+                    source += sourceLock.Pitch;
+                    dest += destLock.Pitch;
+                }
+                break;
+              case D3DFMT_L8:
+                for(int y = 0; y < height; y++)
+                {
+                    for(int x = 0; x < width; x++)
+                    {
+                        dest[x] = source[x * 4 + 2];
+                    }
+
+                    source += sourceLock.Pitch;
+                    dest += destLock.Pitch;
+                }
+                break;
+              case D3DFMT_A8L8:
+                for(int y = 0; y < height; y++)
+                {
+                    for(int x = 0; x < width; x++)
+                    {
+                        dest[x * 2 + 0] = source[x * 4 + 2];
+                        dest[x * 2 + 1] = source[x * 4 + 3];
+                    }
+
+                    source += sourceLock.Pitch;
+                    dest += destLock.Pitch;
+                }
+                break;
+              default:
+                UNREACHABLE();
+            }
+            break;
+          case D3DFMT_R5G6B5:
+            switch(getD3DFormat())
+            {
+              case D3DFMT_X8R8G8B8:
+                for(int y = 0; y < height; y++)
+                {
+                    for(int x = 0; x < width; x++)
+                    {
+                        unsigned short rgb = ((unsigned short*)source)[x];
+                        unsigned char red = (rgb & 0xF800) >> 8;
+                        unsigned char green = (rgb & 0x07E0) >> 3;
+                        unsigned char blue = (rgb & 0x001F) << 3;
+                        dest[x + 0] = blue | (blue >> 5);
+                        dest[x + 1] = green | (green >> 6);
+                        dest[x + 2] = red | (red >> 5);
+                        dest[x + 3] = 0xFF;
+                    }
+
+                    source += sourceLock.Pitch;
+                    dest += destLock.Pitch;
+                }
+                break;
+              case D3DFMT_L8:
+                for(int y = 0; y < height; y++)
+                {
+                    for(int x = 0; x < width; x++)
+                    {
+                        unsigned char red = source[x * 2 + 1] & 0xF8;
+                        dest[x] = red | (red >> 5);
+                    }
+
+                    source += sourceLock.Pitch;
+                    dest += destLock.Pitch;
+                }
+                break;
+              default:
+                UNREACHABLE();
+            }
+            break;
+          case D3DFMT_A1R5G5B5:
+            switch(getD3DFormat())
+            {
+              case D3DFMT_X8R8G8B8:
+                for(int y = 0; y < height; y++)
+                {
+                    for(int x = 0; x < width; x++)
+                    {
+                        unsigned short argb = ((unsigned short*)source)[x];
+                        unsigned char red = (argb & 0x7C00) >> 7;
+                        unsigned char green = (argb & 0x03E0) >> 2;
+                        unsigned char blue = (argb & 0x001F) << 3;
+                        dest[x + 0] = blue | (blue >> 5);
+                        dest[x + 1] = green | (green >> 5);
+                        dest[x + 2] = red | (red >> 5);
+                        dest[x + 3] = 0xFF;
+                    }
+
+                    source += sourceLock.Pitch;
+                    dest += destLock.Pitch;
+                }
+                break;
+              case D3DFMT_A8R8G8B8:
+                for(int y = 0; y < height; y++)
+                {
+                    for(int x = 0; x < width; x++)
+                    {
+                        unsigned short argb = ((unsigned short*)source)[x];
+                        unsigned char red = (argb & 0x7C00) >> 7;
+                        unsigned char green = (argb & 0x03E0) >> 2;
+                        unsigned char blue = (argb & 0x001F) << 3;
+                        unsigned char alpha = (signed short)argb >> 15;
+                        dest[x + 0] = blue | (blue >> 5);
+                        dest[x + 1] = green | (green >> 5);
+                        dest[x + 2] = red | (red >> 5);
+                        dest[x + 3] = alpha;
+                    }
+
+                    source += sourceLock.Pitch;
+                    dest += destLock.Pitch;
+                }
+                break;
+              case D3DFMT_L8:
+                for(int y = 0; y < height; y++)
+                {
+                    for(int x = 0; x < width; x++)
+                    {
+                        unsigned char red = source[x * 2 + 1] & 0x7C;
+                        dest[x] = (red << 1) | (red >> 4);
+                    }
+
+                    source += sourceLock.Pitch;
+                    dest += destLock.Pitch;
+                }
+                break;
+              case D3DFMT_A8L8:
+                for(int y = 0; y < height; y++)
+                {
+                    for(int x = 0; x < width; x++)
+                    {
+                        unsigned char red = source[x * 2 + 1] & 0x7C;
+                        dest[x * 2 + 0] = (red << 1) | (red >> 4);
+                        dest[x * 2 + 1] = (signed char)source[x * 2 + 1] >> 7;
+                    }
+
+                    source += sourceLock.Pitch;
+                    dest += destLock.Pitch;
+                }
+                break;
+              default:
+                UNREACHABLE();
+            }
+            break;
+          default:
+            UNREACHABLE();
+        }
+    }
+
+    unlock();
+    renderTargetData->UnlockRect();
+
+    renderTargetData->Release();
+    surface->Release();
+
+    mDirty = true;
+}
+
+}
\ No newline at end of file
diff --git a/src/libGLESv2/renderer/Image9.h b/src/libGLESv2/renderer/Image9.h
new file mode 100644
index 0000000..b3b1e84
--- /dev/null
+++ b/src/libGLESv2/renderer/Image9.h
@@ -0,0 +1,82 @@
+//
+// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+//
+
+// Image9.h: Defines the rx::Image9 class, which acts as the interface to
+// the actual underlying surfaces of a Texture.
+
+#ifndef LIBGLESV2_RENDERER_IMAGE9_H_
+#define LIBGLESV2_RENDERER_IMAGE9_H_
+
+#define GL_APICALL
+#include <GLES2/gl2.h>
+#include <d3d9.h>
+
+#include "libGLESv2/renderer/Image.h"
+#include "common/debug.h"
+
+namespace gl
+{
+class Framebuffer;
+}
+
+namespace rx
+{
+class Renderer;
+class Renderer9;
+class TextureStorage2D;
+class TextureStorageCubeMap;
+
+class Image9 : public Image
+{
+  public:
+    Image9();
+    ~Image9();
+
+    static Image9 *makeImage9(Image *img);
+
+    static void generateMipmap(Image9 *dest, Image9 *source);
+    static void copyLockableSurfaces(IDirect3DSurface9 *dest, IDirect3DSurface9 *source);
+
+    virtual bool redefine(Renderer *renderer, GLint internalformat, GLsizei width, GLsizei height, bool forceRelease);
+
+    virtual bool isRenderableFormat() const;
+    D3DFORMAT getD3DFormat() const;
+
+    virtual bool isDirty() const {return mSurface && mDirty;}
+    IDirect3DSurface9 *getSurface();
+
+    virtual void setManagedSurface(TextureStorage2D *storage, int level);
+    virtual void setManagedSurface(TextureStorageCubeMap *storage, int face, int level);
+    virtual bool updateSurface(TextureStorage2D *storage, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height);
+    virtual bool updateSurface(TextureStorageCubeMap *storage, int face, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height);
+
+    virtual void loadData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
+                  GLint unpackAlignment, const void *input);
+    virtual void loadCompressedData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height,
+                                    const void *input);
+
+    virtual void copy(GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, gl::Framebuffer *source);
+
+  private:
+    DISALLOW_COPY_AND_ASSIGN(Image9);
+
+    void createSurface();
+    void setManagedSurface(IDirect3DSurface9 *surface);
+    bool updateSurface(IDirect3DSurface9 *dest, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height);
+
+    HRESULT lock(D3DLOCKED_RECT *lockedRect, const RECT *rect);
+    void unlock();
+    
+    Renderer9 *mRenderer;
+
+    D3DPOOL mD3DPool;   // can only be D3DPOOL_SYSTEMMEM or D3DPOOL_MANAGED since it needs to be lockable.
+    D3DFORMAT mD3DFormat;
+
+    IDirect3DSurface9 *mSurface;
+};
+}
+
+#endif   // LIBGLESV2_RENDERER_IMAGE9_H_
diff --git a/src/libGLESv2/renderer/Renderer9.cpp b/src/libGLESv2/renderer/Renderer9.cpp
index 706e792..8b4ff69 100644
--- a/src/libGLESv2/renderer/Renderer9.cpp
+++ b/src/libGLESv2/renderer/Renderer9.cpp
@@ -21,7 +21,7 @@
 #include "libGLESv2/renderer/ShaderExecutable9.h"
 #include "libGLESv2/renderer/SwapChain9.h"
 #include "libGLESv2/renderer/TextureStorage.h"
-#include "libGLESv2/renderer/Image.h"
+#include "libGLESv2/renderer/Image9.h"
 #include "libGLESv2/renderer/Blit.h"
 #include "libGLESv2/renderer/RenderTarget9.h"
 
@@ -2789,7 +2789,7 @@
 
             if (SUCCEEDED(result))
             {
-                Image::copyLockableSurfaces(surf, source);
+                Image9::copyLockableSurfaces(surf, source);
                 result = device->UpdateSurface(surf, NULL, dest, NULL);
                 surf->Release();
             }
@@ -2812,12 +2812,14 @@
 
 Image *Renderer9::createImage()
 {
-    return new Image();
+    return new Image9();
 }
 
 void Renderer9::generateMipmap(Image *dest, Image *src)
 {
-    Image::generateMipmap(dest, src);
+    Image9 *src9 = Image9::makeImage9(src);
+    Image9 *dst9 = Image9::makeImage9(dest);
+    Image9::generateMipmap(dst9, src9);
 }
 
 }
\ No newline at end of file