Move the index range cache to gl::Buffer.

Because we want to move all validation to the API layer, we need to
move the index validation to the API layer. This means exposing the
index cache to the validation layer.

In the future we will probably want a way to skip index validation
when it's not necessary, or the chosen back-end handles index
validation on its own.

BUG=angle:571

Change-Id: Iee1618e669bc28abf7e0709ca4a03b4727b1383e
Reviewed-on: https://chromium-review.googlesource.com/210646
Tested-by: Jamie Madill <jmadill@chromium.org>
Reviewed-by: Geoff Lang <geofflang@chromium.org>
diff --git a/src/libGLESv2/Buffer.cpp b/src/libGLESv2/Buffer.cpp
index 7d31a82..27b6fc3 100644
--- a/src/libGLESv2/Buffer.cpp
+++ b/src/libGLESv2/Buffer.cpp
@@ -31,11 +31,12 @@
 
 Buffer::~Buffer()
 {
-    delete mBuffer;
+    SafeDelete(mBuffer);
 }
 
 void Buffer::bufferData(const void *data, GLsizeiptr size, GLenum usage)
 {
+    mIndexRangeCache.clear();
     mUsage = usage;
     mSize = size;
     mBuffer->setData(data, size, usage);
@@ -43,11 +44,13 @@
 
 void Buffer::bufferSubData(const void *data, GLsizeiptr size, GLintptr offset)
 {
+    mIndexRangeCache.invalidateRange(offset, size);
     mBuffer->setSubData(data, size, offset);
 }
 
 void Buffer::copyBufferSubData(Buffer* source, GLintptr sourceOffset, GLintptr destOffset, GLsizeiptr size)
 {
+    mIndexRangeCache.invalidateRange(destOffset, size);
     mBuffer->copySubData(source->getImplementation(), sourceOffset, destOffset, size);
 }
 
@@ -64,6 +67,11 @@
     mMapLength = static_cast<GLint64>(length);
     mAccessFlags = static_cast<GLint>(access);
 
+    if ((access & GL_MAP_WRITE_BIT) > 0)
+    {
+        mIndexRangeCache.invalidateRange(offset, length);
+    }
+
     return mMapPointer;
 }
 
@@ -84,6 +92,7 @@
 {
     // TODO: Only used by the DX11 backend. Refactor to a more appropriate place.
     mBuffer->markTransformFeedbackUsage();
+    mIndexRangeCache.clear();
 }
 
 }
diff --git a/src/libGLESv2/Buffer.h b/src/libGLESv2/Buffer.h
index 389c3d4..37d30bc 100644
--- a/src/libGLESv2/Buffer.h
+++ b/src/libGLESv2/Buffer.h
@@ -13,6 +13,7 @@
 
 #include "common/angleutils.h"
 #include "common/RefCountObject.h"
+#include "libGLESv2/renderer/IndexRangeCache.h"
 
 namespace rx
 {
@@ -48,6 +49,8 @@
 
     void markTransformFeedbackUsage();
 
+    rx::IndexRangeCache *getIndexRangeCache() { return &mIndexRangeCache; }
+
   private:
     DISALLOW_COPY_AND_ASSIGN(Buffer);
 
@@ -60,6 +63,8 @@
     GLvoid *mMapPointer;
     GLint64 mMapOffset;
     GLint64 mMapLength;
+
+    rx::IndexRangeCache mIndexRangeCache;
 };
 
 }
diff --git a/src/libGLESv2/renderer/d3d/BufferD3D.h b/src/libGLESv2/renderer/d3d/BufferD3D.h
index 0d57338..44f14ce 100644
--- a/src/libGLESv2/renderer/d3d/BufferD3D.h
+++ b/src/libGLESv2/renderer/d3d/BufferD3D.h
@@ -11,7 +11,6 @@
 
 #include "libGLESv2/renderer/BufferImpl.h"
 #include "libGLESv2/angletypes.h"
-#include "libGLESv2/renderer/IndexRangeCache.h"
 
 namespace rx
 {
@@ -36,7 +35,6 @@
 
     rx::StaticVertexBufferInterface *getStaticVertexBuffer() { return mStaticVertexBuffer; }
     rx::StaticIndexBufferInterface *getStaticIndexBuffer() { return mStaticIndexBuffer; }
-    rx::IndexRangeCache *getIndexRangeCache() { return &mIndexRangeCache; }
 
     void initializeStaticData();
     void invalidateStaticData();
@@ -50,7 +48,6 @@
 
     rx::StaticVertexBufferInterface *mStaticVertexBuffer;
     rx::StaticIndexBufferInterface *mStaticIndexBuffer;
-    rx::IndexRangeCache mIndexRangeCache;
     unsigned int mUnmodifiedDataUse;
 };
 
diff --git a/src/libGLESv2/renderer/d3d/IndexDataManager.cpp b/src/libGLESv2/renderer/d3d/IndexDataManager.cpp
index 45d7d37..99f2f26 100644
--- a/src/libGLESv2/renderer/d3d/IndexDataManager.cpp
+++ b/src/libGLESv2/renderer/d3d/IndexDataManager.cpp
@@ -184,10 +184,10 @@
     {
         streamOffset = offset;
 
-        if (!storage->getIndexRangeCache()->findRange(type, offset, count, &translated->indexRange, NULL))
+        if (!buffer->getIndexRangeCache()->findRange(type, offset, count, &translated->indexRange, NULL))
         {
             translated->indexRange = computeRange(type, indices, count);
-            storage->getIndexRangeCache()->addRange(type, offset, count, translated->indexRange, offset);
+            buffer->getIndexRangeCache()->addRange(type, offset, count, translated->indexRange, offset);
         }
     }
     else if (staticBuffer && staticBuffer->getBufferSize() != 0 && staticBuffer->getIndexType() == type && alignedOffset)
diff --git a/src/libGLESv2/renderer/d3d/d3d11/Buffer11.cpp b/src/libGLESv2/renderer/d3d/d3d11/Buffer11.cpp
index 48514db..0b264f8 100644
--- a/src/libGLESv2/renderer/d3d/d3d11/Buffer11.cpp
+++ b/src/libGLESv2/renderer/d3d/d3d11/Buffer11.cpp
@@ -180,8 +180,6 @@
 
 void Buffer11::setData(const void* data, size_t size, GLenum usage)
 {
-    mIndexRangeCache.clear();
-
     setSubData(data, size, 0);
 
     if (usage == GL_STATIC_DRAW)
@@ -236,7 +234,6 @@
     size_t requiredSize = size + offset;
     mSize = std::max(mSize, requiredSize);
 
-    mIndexRangeCache.invalidateRange(offset, size);
     invalidateStaticData();
 
     if (data && size > 0)
@@ -326,7 +323,6 @@
         mSize = std::max<size_t>(mSize, destOffset + size);
     }
 
-    mIndexRangeCache.invalidateRange(destOffset, size);
     invalidateStaticData();
 }
 
@@ -357,8 +353,6 @@
 
     if ((access & GL_MAP_WRITE_BIT) > 0)
     {
-        mIndexRangeCache.invalidateRange(offset, length);
-
         // Update the data revision immediately, since the data might be changed at any time
         mMappedStorage->setDataRevision(mMappedStorage->getDataRevision() + 1);
     }
@@ -382,7 +376,6 @@
         transformFeedbackStorage->setDataRevision(transformFeedbackStorage->getDataRevision() + 1);
     }
 
-    mIndexRangeCache.clear();
     invalidateStaticData();
 }
 
@@ -480,8 +473,6 @@
         packStorage->packPixels(srcTexture, srcSubresource, params);
         packStorage->setDataRevision(latestStorage ? latestStorage->getDataRevision() + 1 : 1);
     }
-
-    mIndexRangeCache.clear();
 }
 
 Buffer11::BufferStorage11 *Buffer11::getBufferStorage(BufferUsage usage)
diff --git a/src/libGLESv2/renderer/d3d/d3d11/Renderer11.cpp b/src/libGLESv2/renderer/d3d/d3d11/Renderer11.cpp
index 2c2d776..9e66b69 100644
--- a/src/libGLESv2/renderer/d3d/d3d11/Renderer11.cpp
+++ b/src/libGLESv2/renderer/d3d/d3d11/Renderer11.cpp
@@ -2748,10 +2748,13 @@
         area.width = width;
         area.height = height;
 
-        if (pack.pixelBuffer.get() != NULL)
+        gl::Buffer *packBuffer = pack.pixelBuffer.get();
+        if (packBuffer != NULL)
         {
-            rx::Buffer11 *packBufferStorage = Buffer11::makeBuffer11(pack.pixelBuffer.get()->getImplementation());
+            rx::Buffer11 *packBufferStorage = Buffer11::makeBuffer11(packBuffer->getImplementation());
             PackPixelsParams packParams(area, format, type, outputPitch, pack, reinterpret_cast<ptrdiff_t>(pixels));
+
+            packBuffer->getIndexRangeCache()->clear();
             packBufferStorage->packPixels(colorBufferTexture, subresourceIndex, packParams);
         }
         else
diff --git a/src/libGLESv2/renderer/d3d/d3d9/Buffer9.cpp b/src/libGLESv2/renderer/d3d/d3d9/Buffer9.cpp
index 47198d4..a4cffb5 100644
--- a/src/libGLESv2/renderer/d3d/d3d9/Buffer9.cpp
+++ b/src/libGLESv2/renderer/d3d/d3d9/Buffer9.cpp
@@ -47,7 +47,6 @@
         memcpy(mMemory.data(), data, size);
     }
 
-    mIndexRangeCache.clear();
     invalidateStaticData();
 
     if (usage == GL_STATIC_DRAW)
@@ -77,8 +76,6 @@
         memcpy(mMemory.data() + offset, data, size);
     }
 
-    mIndexRangeCache.invalidateRange(offset, size);
-
     invalidateStaticData();
 }
 
@@ -91,7 +88,6 @@
         memcpy(mMemory.data() + destOffset, sourceBuffer->mMemory.data() + sourceOffset, size);
     }
 
-    mIndexRangeCache.invalidateRange(destOffset, size);
     invalidateStaticData();
 }