Generalize Context scratch buffer errors.

This refactor will allow us to generate different error types in
different backends. This makes Vulkan happy because it won't have to
generate gl::Errors and can stay with vk::Error.

Bug: angleproject:2713
Change-Id: I981402450f3b519d4f79851982547695d583355a
Reviewed-on: https://chromium-review.googlesource.com/1128921
Commit-Queue: Jamie Madill <jmadill@chromium.org>
Reviewed-by: Geoff Lang <geofflang@chromium.org>
Reviewed-by: Frank Henigman <fjhenigman@chromium.org>
diff --git a/src/common/angleutils.h b/src/common/angleutils.h
index c3f8e9e..b2df679 100644
--- a/src/common/angleutils.h
+++ b/src/common/angleutils.h
@@ -259,6 +259,12 @@
         return gl::InternalError() << "Integer overflow."; \
     }
 
+#define ANGLE_TRY_ALLOCATION(result)                                       \
+    if (!result)                                                           \
+    {                                                                      \
+        return gl::OutOfMemory() << "Failed to allocate internal buffer."; \
+    }
+
 // The below inlining code lifted from V8.
 #if defined(__clang__) || (defined(__GNUC__) && defined(__has_attribute))
 #define ANGLE_HAS_ATTRIBUTE_ALWAYS_INLINE (__has_attribute(always_inline))
diff --git a/src/libANGLE/Buffer.cpp b/src/libANGLE/Buffer.cpp
index d308531..a339a90 100644
--- a/src/libANGLE/Buffer.cpp
+++ b/src/libANGLE/Buffer.cpp
@@ -79,7 +79,8 @@
     if (context && context->getGLState().isRobustResourceInitEnabled() && !data && size > 0)
     {
         angle::MemoryBuffer *scratchBuffer = nullptr;
-        ANGLE_TRY(context->getZeroFilledBuffer(static_cast<size_t>(size), &scratchBuffer));
+        ANGLE_TRY_ALLOCATION(
+            context->getZeroFilledBuffer(static_cast<size_t>(size), &scratchBuffer));
         dataForImpl = scratchBuffer->data();
     }
 
diff --git a/src/libANGLE/Context.cpp b/src/libANGLE/Context.cpp
index 881b75a..976fed2 100644
--- a/src/libANGLE/Context.cpp
+++ b/src/libANGLE/Context.cpp
@@ -5021,24 +5021,16 @@
     SetFramebufferParameteri(framebuffer, pname, param);
 }
 
-Error Context::getScratchBuffer(size_t requstedSizeBytes,
-                                angle::MemoryBuffer **scratchBufferOut) const
+bool Context::getScratchBuffer(size_t requstedSizeBytes,
+                               angle::MemoryBuffer **scratchBufferOut) const
 {
-    if (!mScratchBuffer.get(requstedSizeBytes, scratchBufferOut))
-    {
-        return OutOfMemory() << "Failed to allocate internal buffer.";
-    }
-    return NoError();
+    return mScratchBuffer.get(requstedSizeBytes, scratchBufferOut);
 }
 
-Error Context::getZeroFilledBuffer(size_t requstedSizeBytes,
-                                   angle::MemoryBuffer **zeroBufferOut) const
+bool Context::getZeroFilledBuffer(size_t requstedSizeBytes,
+                                  angle::MemoryBuffer **zeroBufferOut) const
 {
-    if (!mZeroFilledBuffer.getInitialized(requstedSizeBytes, zeroBufferOut, 0))
-    {
-        return OutOfMemory() << "Failed to allocate internal buffer.";
-    }
-    return NoError();
+    return mZeroFilledBuffer.getInitialized(requstedSizeBytes, zeroBufferOut, 0);
 }
 
 Error Context::prepareForDispatch()
diff --git a/src/libANGLE/Context.h b/src/libANGLE/Context.h
index d1b2e1a..c120f74 100644
--- a/src/libANGLE/Context.h
+++ b/src/libANGLE/Context.h
@@ -1404,8 +1404,8 @@
     rx::ContextImpl *getImplementation() const { return mImplementation.get(); }
     const Workarounds &getWorkarounds() const;
 
-    Error getScratchBuffer(size_t requestedSizeBytes, angle::MemoryBuffer **scratchBufferOut) const;
-    Error getZeroFilledBuffer(size_t requstedSizeBytes, angle::MemoryBuffer **zeroBufferOut) const;
+    bool getScratchBuffer(size_t requestedSizeBytes, angle::MemoryBuffer **scratchBufferOut) const;
+    bool getZeroFilledBuffer(size_t requstedSizeBytes, angle::MemoryBuffer **zeroBufferOut) const;
 
     Error prepareForDispatch();
 
diff --git a/src/libANGLE/renderer/d3d/TextureD3D.cpp b/src/libANGLE/renderer/d3d/TextureD3D.cpp
index 3d82eec..d016e4e 100644
--- a/src/libANGLE/renderer/d3d/TextureD3D.cpp
+++ b/src/libANGLE/renderer/d3d/TextureD3D.cpp
@@ -723,7 +723,8 @@
     zeroDataUnpackState.alignment = 1;
 
     angle::MemoryBuffer *zeroBuffer = nullptr;
-    ANGLE_TRY(context->getZeroFilledBuffer(imageBytes, &zeroBuffer));
+    ANGLE_TRY_ALLOCATION(context->getZeroFilledBuffer(imageBytes, &zeroBuffer));
+
     if (shouldUseSetData(image))
     {
         ANGLE_TRY(mTexStorage->setData(context, imageIndex, image, nullptr, formatInfo.type,
diff --git a/src/libANGLE/renderer/d3d/VertexDataManager.cpp b/src/libANGLE/renderer/d3d/VertexDataManager.cpp
index ca9a116..8e53155 100644
--- a/src/libANGLE/renderer/d3d/VertexDataManager.cpp
+++ b/src/libANGLE/renderer/d3d/VertexDataManager.cpp
@@ -206,11 +206,7 @@
 {
     mStreamingBuffer.reset(
         new StreamingVertexBufferInterface(mFactory, INITIAL_STREAM_BUFFER_SIZE));
-    if (!mStreamingBuffer)
-    {
-        return gl::OutOfMemory() << "Failed to allocate the streaming vertex buffer.";
-    }
-
+    ANGLE_TRY_ALLOCATION(mStreamingBuffer);
     return gl::NoError();
 }
 
diff --git a/src/libANGLE/renderer/d3d/d3d11/Buffer11.cpp b/src/libANGLE/renderer/d3d/d3d11/Buffer11.cpp
index 8336932..6f816c2 100644
--- a/src/libANGLE/renderer/d3d/d3d11/Buffer11.cpp
+++ b/src/libANGLE/renderer/d3d/d3d11/Buffer11.cpp
@@ -502,10 +502,7 @@
         ANGLE_TRY_RESULT(getStagingStorage(context), mMappedStorage);
     }
 
-    if (!mMappedStorage)
-    {
-        return gl::OutOfMemory() << "Failed to allocate mappable internal buffer.";
-    }
+    ANGLE_TRY_ALLOCATION(mMappedStorage);
 
     if ((access & GL_MAP_WRITE_BIT) > 0)
     {
diff --git a/src/libANGLE/renderer/d3d/d3d11/Renderer11.cpp b/src/libANGLE/renderer/d3d/d3d11/Renderer11.cpp
index 37f5cf7..830d3cc 100644
--- a/src/libANGLE/renderer/d3d/d3d11/Renderer11.cpp
+++ b/src/libANGLE/renderer/d3d/d3d11/Renderer11.cpp
@@ -3649,10 +3649,7 @@
 
 gl::Error Renderer11::getScratchMemoryBuffer(size_t requestedSize, angle::MemoryBuffer **bufferOut)
 {
-    if (!mScratchMemoryBuffer.get(requestedSize, bufferOut))
-    {
-        return gl::OutOfMemory() << "Failed to allocate internal buffer.";
-    }
+    ANGLE_TRY_ALLOCATION(mScratchMemoryBuffer.get(requestedSize, bufferOut));
     return gl::NoError();
 }
 
diff --git a/src/libANGLE/renderer/gl/BlitGL.cpp b/src/libANGLE/renderer/gl/BlitGL.cpp
index 3fa8e6f..cc09945 100644
--- a/src/libANGLE/renderer/gl/BlitGL.cpp
+++ b/src/libANGLE/renderer/gl/BlitGL.cpp
@@ -601,7 +601,8 @@
     size_t destBufferSize =
         sourceArea.width * sourceArea.height * destInternalFormatInfo.pixelBytes;
     angle::MemoryBuffer *buffer = nullptr;
-    ANGLE_TRY(context->getScratchBuffer(sourceBufferSize + destBufferSize, &buffer));
+    ANGLE_TRY_ALLOCATION(context->getScratchBuffer(sourceBufferSize + destBufferSize, &buffer));
+
     uint8_t *sourceMemory = buffer->data();
     uint8_t *destMemory   = buffer->data() + sourceBufferSize;
 
diff --git a/src/libANGLE/renderer/gl/TextureGL.cpp b/src/libANGLE/renderer/gl/TextureGL.cpp
index 32627be..041ce76 100644
--- a/src/libANGLE/renderer/gl/TextureGL.cpp
+++ b/src/libANGLE/renderer/gl/TextureGL.cpp
@@ -600,7 +600,7 @@
         GLuint pixelBytes =
             gl::GetInternalFormatInfo(copyTexImageFormat.internalFormat, type).pixelBytes;
         angle::MemoryBuffer *zero;
-        ANGLE_TRY(context->getZeroFilledBuffer(
+        ANGLE_TRY_ALLOCATION(context->getZeroFilledBuffer(
             origSourceArea.width * origSourceArea.height * pixelBytes, &zero));
 
         gl::PixelUnpackState unpack;
@@ -1539,7 +1539,7 @@
             internalFormatInfo.computeCompressedImageSize(desc.size, &imageSize));
 
         angle::MemoryBuffer *zero;
-        ANGLE_TRY(context->getZeroFilledBuffer(imageSize, &zero));
+        ANGLE_TRY_ALLOCATION(context->getZeroFilledBuffer(imageSize, &zero));
 
         // WebGL spec requires that zero data is uploaded to compressed textures even if it might
         // not result in zero color data.
@@ -1569,7 +1569,7 @@
             &imageSize));
 
         angle::MemoryBuffer *zero;
-        ANGLE_TRY(context->getZeroFilledBuffer(imageSize, &zero));
+        ANGLE_TRY_ALLOCATION(context->getZeroFilledBuffer(imageSize, &zero));
 
         if (nativegl::UseTexImage2D(getType()))
         {
diff --git a/src/libANGLE/renderer/vulkan/TextureVk.cpp b/src/libANGLE/renderer/vulkan/TextureVk.cpp
index e3269c0..d08f830 100644
--- a/src/libANGLE/renderer/vulkan/TextureVk.cpp
+++ b/src/libANGLE/renderer/vulkan/TextureVk.cpp
@@ -229,7 +229,7 @@
         size_t bufferSize =
             storageFormat.pixelBytes * clippedRectangle.width * clippedRectangle.height;
         angle::MemoryBuffer *memoryBuffer = nullptr;
-        ANGLE_TRY(context->getScratchBuffer(bufferSize, &memoryBuffer));
+        ANGLE_TRY_ALLOCATION(context->getScratchBuffer(bufferSize, &memoryBuffer));
 
         // Read into the scratch buffer
         ANGLE_TRY(