Don't apply textures that are currently bound to the framebuffer.

BUG=496
Review URL=https://codereview.appspot.com/18690043/
diff --git a/src/libGLESv2/Context.cpp b/src/libGLESv2/Context.cpp
index e084db1..fba4104 100644
--- a/src/libGLESv2/Context.cpp
+++ b/src/libGLESv2/Context.cpp
@@ -1812,6 +1812,8 @@
 {
     ProgramBinary *programBinary = getCurrentProgramBinary();
 
+    FramebufferTextureSerialSet boundFramebufferTextures = getBoundFramebufferTextureSerials();
+
     // Range of Direct3D samplers of given sampler type
     int samplerCount = (type == SAMPLER_PIXEL) ? MAX_TEXTURE_IMAGE_UNITS : mRenderer->getMaxVertexTextureImageUnits();
     int samplerRange = programBinary->getUsedSamplerRange(type);
@@ -1825,7 +1827,8 @@
             TextureType textureType = programBinary->getSamplerTextureType(type, samplerIndex);
             Texture *texture = getSamplerTexture(textureUnit, textureType);
 
-            if (texture->isSamplerComplete())
+            if (texture->isSamplerComplete() &&
+                boundFramebufferTextures.find(texture->getTextureSerial()) == boundFramebufferTextures.end())
             {
                 SamplerState samplerState;
                 texture->getSamplerState(&samplerState);
@@ -2655,6 +2658,29 @@
     return mRendererString;
 }
 
+Context::FramebufferTextureSerialSet Context::getBoundFramebufferTextureSerials()
+{
+    FramebufferTextureSerialSet set;
+
+    Framebuffer *drawFramebuffer = getDrawFramebuffer();
+    for (unsigned int i = 0; i < IMPLEMENTATION_MAX_DRAW_BUFFERS; i++)
+    {
+        Renderbuffer *renderBuffer = drawFramebuffer->getColorbuffer(i);
+        if (renderBuffer && renderBuffer->getTextureSerial() != 0)
+        {
+            set.insert(renderBuffer->getTextureSerial());
+        }
+    }
+
+    Renderbuffer *depthStencilBuffer = drawFramebuffer->getDepthOrStencilbuffer();
+    if (depthStencilBuffer && depthStencilBuffer->getTextureSerial() != 0)
+    {
+        set.insert(depthStencilBuffer->getTextureSerial());
+    }
+
+    return set;
+}
+
 void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, 
                               GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
                               GLbitfield mask)
diff --git a/src/libGLESv2/Context.h b/src/libGLESv2/Context.h
index 349ec13..09eede9 100644
--- a/src/libGLESv2/Context.h
+++ b/src/libGLESv2/Context.h
@@ -18,6 +18,7 @@
 
 #include <string>
 #include <map>
+#include <set>
 #ifdef _MSC_VER
 #include <hash_map>
 #else
@@ -420,6 +421,9 @@
     void initExtensionString();
     void initRendererString();
 
+    typedef std::set<unsigned> FramebufferTextureSerialSet;
+    FramebufferTextureSerialSet getBoundFramebufferTextureSerials();
+
     rx::Renderer *const mRenderer;
 
     State mState;