D3D11: Fix reserved space with large dynamic buffers.

We would end up with a large reserved space even though the
allocation failed. Insead set the reserved space size after
the allocation succedes.

This was showing up as angle_end2end_tests failures on
Windows 7 due to display reuse and buffer allocation.

Bug: chromium:944454
Change-Id: Idb3bd530fe7b9cc2fce9a579787684e632b1a637
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/1534684
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Commit-Queue: Jamie Madill <jmadill@chromium.org>
diff --git a/src/libANGLE/renderer/d3d/VertexBuffer.cpp b/src/libANGLE/renderer/d3d/VertexBuffer.cpp
index 58b63f0..78dd75b 100644
--- a/src/libANGLE/renderer/d3d/VertexBuffer.cpp
+++ b/src/libANGLE/renderer/d3d/VertexBuffer.cpp
@@ -159,6 +159,7 @@
         mWritePosition = 0;
     }
 
+    mReservedSpace = size;
     return angle::Result::Continue;
 }
 
@@ -181,7 +182,6 @@
     checkedPosition += spaceRequired;
     ANGLE_CHECK_GL_ALLOC(GetImplAs<ContextD3D>(context), checkedPosition.IsValid());
 
-    ANGLE_TRY(reserveSpace(context, mReservedSpace));
     mReservedSpace = 0;
 
     ANGLE_TRY(mVertexBuffer->storeVertexAttributes(context, attrib, binding, currentValueType,
@@ -215,7 +215,7 @@
     // Protect against integer overflow
     ANGLE_CHECK_GL_ALLOC(GetImplAs<ContextD3D>(context), alignedRequiredSpace.IsValid());
 
-    mReservedSpace = alignedRequiredSpace.ValueOrDie();
+    ANGLE_TRY(reserveSpace(context, alignedRequiredSpace.ValueOrDie()));
 
     return angle::Result::Continue;
 }
diff --git a/src/tests/gl_tests/BufferDataTest.cpp b/src/tests/gl_tests/BufferDataTest.cpp
index 4f3651c..27c556a 100644
--- a/src/tests/gl_tests/BufferDataTest.cpp
+++ b/src/tests/gl_tests/BufferDataTest.cpp
@@ -561,6 +561,19 @@
     EXPECT_GL_NO_ERROR();
     glDrawArrays(GL_TRIANGLES, 0, numItems);
     EXPECT_GL_ERROR(GL_OUT_OF_MEMORY);
+
+    // Test that a small draw still works.
+    for (GLsizei bufferIndex = 0; bufferIndex < bufferCnt; ++bufferIndex)
+    {
+        std::stringstream attribNameStr;
+        attribNameStr << "attrib" << bufferIndex;
+        GLint attribLocation = glGetAttribLocation(program.get(), attribNameStr.str().c_str());
+        ASSERT_NE(-1, attribLocation);
+        glDisableVertexAttribArray(attribLocation);
+    }
+
+    glDrawArrays(GL_TRIANGLES, 0, 3);
+    EXPECT_GL_ERROR(GL_NO_ERROR);
 }
 
 // Tests a security bug in our CopyBufferSubData validation (integer overflow).