Sharing for buffer, texture and renderbuffer objects.

TRAC #12496

Derive Renderbuffer, Texture and Buffer from RefCountObject.
This class keeps a reference count for all objects that need cross-context
reference counting, and also the object id.
Restructure Renderbuffers to create a wrapper object and a storage object.
Use BindingPointer for all binding points instead of binding by object id.

Signed-off-by: Shannon Woods
Signed-off-by: Daniel Koch

git-svn-id: https://angleproject.googlecode.com/svn/trunk@364 736b8ea6-26fd-11df-bfd4-992fa37f6226
diff --git a/src/libGLESv2/Texture.cpp b/src/libGLESv2/Texture.cpp
index 305620c..6dfa7c8 100644
--- a/src/libGLESv2/Texture.cpp
+++ b/src/libGLESv2/Texture.cpp
@@ -32,7 +32,7 @@
   if (surface) surface->Release();
 }
 
-Texture::Texture()
+Texture::Texture(GLuint id) : RefCountObject(id)
 {
     mMinFilter = GL_NEAREST_MIPMAP_LINEAR;
     mMagFilter = GL_LINEAR;
@@ -443,7 +443,7 @@
     return mBaseTexture ? mBaseTexture->GetLevelCount() : 0;
 }
 
-Texture2D::Texture2D()
+Texture2D::Texture2D(GLuint id) : Texture(id)
 {
     mTexture = NULL;
     mColorbufferProxy = NULL;
@@ -564,7 +564,7 @@
     commitRect(level, xoffset, yoffset, width, height);
 }
 
-void Texture2D::copyImage(GLint level, GLenum internalFormat, GLint x, GLint y, GLsizei width, GLsizei height, Renderbuffer *source)
+void Texture2D::copyImage(GLint level, GLenum internalFormat, GLint x, GLint y, GLsizei width, GLsizei height, RenderbufferStorage *source)
 {
     if (redefineTexture(level, internalFormat, width, height))
     {
@@ -592,7 +592,7 @@
     mImageArray[level].format = internalFormat;
 }
 
-void Texture2D::copySubImage(GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Renderbuffer *source)
+void Texture2D::copySubImage(GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, RenderbufferStorage *source)
 {
     if (xoffset + width > mImageArray[level].width || yoffset + height > mImageArray[level].height)
     {
@@ -872,16 +872,17 @@
     }
 }
 
-Colorbuffer *Texture2D::getColorbuffer(GLenum target)
+Renderbuffer *Texture2D::getColorbuffer(GLenum target)
 {
     if (target != GL_TEXTURE_2D)
     {
-        return error(GL_INVALID_OPERATION, (Colorbuffer *)NULL);
+        return error(GL_INVALID_OPERATION, (Renderbuffer *)NULL);
     }
 
     if (mColorbufferProxy == NULL)
     {
-        mColorbufferProxy = new TextureColorbufferProxy(this, target);
+        mColorbufferProxy = new Renderbuffer(id(), new TextureColorbufferProxy(this, target));
+        mColorbufferProxy->addRef();
     }
 
     return mColorbufferProxy;
@@ -899,7 +900,7 @@
     return renderTarget;
 }
 
-TextureCubeMap::TextureCubeMap()
+TextureCubeMap::TextureCubeMap(GLuint id) : Texture(id)
 {
     mTexture = NULL;
 
@@ -1279,7 +1280,7 @@
     return !textureOkay;
 }
 
-void TextureCubeMap::copyImage(GLenum face, GLint level, GLenum internalFormat, GLint x, GLint y, GLsizei width, GLsizei height, Renderbuffer *source)
+void TextureCubeMap::copyImage(GLenum face, GLint level, GLenum internalFormat, GLint x, GLint y, GLsizei width, GLsizei height, RenderbufferStorage *source)
 {
     unsigned int faceindex = faceIndex(face);
 
@@ -1341,7 +1342,7 @@
     return (SUCCEEDED(hr)) ? surface : NULL;
 }
 
-void TextureCubeMap::copySubImage(GLenum face, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Renderbuffer *source)
+void TextureCubeMap::copySubImage(GLenum face, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, RenderbufferStorage *source)
 {
     GLsizei size = mImageArray[faceIndex(face)][level].width;
 
@@ -1441,18 +1442,19 @@
     }
 }
 
-Colorbuffer *TextureCubeMap::getColorbuffer(GLenum target)
+Renderbuffer *TextureCubeMap::getColorbuffer(GLenum target)
 {
     if (!IsCubemapTextureTarget(target))
     {
-        return error(GL_INVALID_OPERATION, (Colorbuffer *)NULL);
+        return error(GL_INVALID_OPERATION, (Renderbuffer *)NULL);
     }
 
     unsigned int face = faceIndex(target);
 
     if (mFaceProxies[face] == NULL)
     {
-        mFaceProxies[face] = new TextureColorbufferProxy(this, target);
+        mFaceProxies[face] = new Renderbuffer(id(), new TextureColorbufferProxy(this, target));
+        mFaceProxies[face]->addRef();
     }
 
     return mFaceProxies[face];
@@ -1476,6 +1478,16 @@
     ASSERT(target == GL_TEXTURE_2D || IsCubemapTextureTarget(target));
 }
 
+void Texture::TextureColorbufferProxy::addRef() const
+{
+    mTexture->addRef();
+}
+
+void Texture::TextureColorbufferProxy::release() const
+{
+    mTexture->release();
+}
+
 IDirect3DSurface9 *Texture::TextureColorbufferProxy::getRenderTarget()
 {
     if (mRenderTarget) mRenderTarget->Release();
@@ -1485,12 +1497,12 @@
     return mRenderTarget;
 }
 
-int Texture::TextureColorbufferProxy::getWidth()
+int Texture::TextureColorbufferProxy::getWidth() const
 {
     return mTexture->getWidth();
 }
 
-int Texture::TextureColorbufferProxy::getHeight()
+int Texture::TextureColorbufferProxy::getHeight() const
 {
     return mTexture->getHeight();
 }