Commit a36f8bd4 by Geoff Lang Committed by Commit Bot

Mark MemoryBuffer allocation functions as NO_DISCARD

Not all call sites were checking the return value of MemoryBuffer::resize, mark the return value as NO_DISCARD and fix all the warnings. BUG=chromium:1030835 Change-Id: I762796e3d11efc131a814069d78a195b0d4c9f8f Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2028151 Commit-Queue: Geoff Lang <geofflang@chromium.org> Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org>
parent 1736c47b
...@@ -112,7 +112,7 @@ bool ScratchBuffer::getImpl(size_t requestedSize, ...@@ -112,7 +112,7 @@ bool ScratchBuffer::getImpl(size_t requestedSize,
if (mResetCounter == 0 || mScratchMemory.size() < requestedSize) if (mResetCounter == 0 || mScratchMemory.size() < requestedSize)
{ {
mScratchMemory.resize(0); clear();
if (!mScratchMemory.resize(requestedSize)) if (!mScratchMemory.resize(requestedSize))
{ {
return false; return false;
...@@ -141,7 +141,7 @@ void ScratchBuffer::tick() ...@@ -141,7 +141,7 @@ void ScratchBuffer::tick()
void ScratchBuffer::clear() void ScratchBuffer::clear()
{ {
mResetCounter = mLifetime; mResetCounter = mLifetime;
mScratchMemory.resize(0); mScratchMemory.clear();
} }
} // namespace angle } // namespace angle
...@@ -21,13 +21,13 @@ class MemoryBuffer final : NonCopyable ...@@ -21,13 +21,13 @@ class MemoryBuffer final : NonCopyable
{ {
public: public:
MemoryBuffer() = default; MemoryBuffer() = default;
MemoryBuffer(size_t size) { resize(size); }
~MemoryBuffer(); ~MemoryBuffer();
MemoryBuffer(MemoryBuffer &&other); MemoryBuffer(MemoryBuffer &&other);
MemoryBuffer &operator=(MemoryBuffer &&other); MemoryBuffer &operator=(MemoryBuffer &&other);
bool resize(size_t size); ANGLE_NO_DISCARD bool resize(size_t size);
void clear() { (void)resize(0); }
size_t size() const { return mSize; } size_t size() const { return mSize; }
bool empty() const { return mSize == 0; } bool empty() const { return mSize == 0; }
......
...@@ -31,7 +31,7 @@ void MakeSequence(T &seq, uint8_t start) ...@@ -31,7 +31,7 @@ void MakeSequence(T &seq, uint8_t start)
BlobPut MakeBlob(size_t size, uint8_t start = 0) BlobPut MakeBlob(size_t size, uint8_t start = 0)
{ {
BlobPut blob; BlobPut blob;
blob.resize(size); EXPECT_TRUE(blob.resize(size));
MakeSequence(blob, start); MakeSequence(blob, start);
return blob; return blob;
} }
......
...@@ -1586,8 +1586,12 @@ Error Display::programCachePopulate(const void *key, ...@@ -1586,8 +1586,12 @@ Error Display::programCachePopulate(const void *key,
BlobCache::Key programHash; BlobCache::Key programHash;
memcpy(programHash.data(), key, BlobCache::kKeyLength); memcpy(programHash.data(), key, BlobCache::kKeyLength);
mMemoryProgramCache.putBinary(programHash, reinterpret_cast<const uint8_t *>(binary), if (!mMemoryProgramCache.putBinary(programHash, reinterpret_cast<const uint8_t *>(binary),
static_cast<size_t>(binarysize)); static_cast<size_t>(binarysize)))
{
return EglBadAccess() << "Failed to copy program binary into the cache.";
}
return NoError(); return NoError();
} }
......
...@@ -187,18 +187,18 @@ void MemoryProgramCache::remove(const egl::BlobCache::Key &programHash) ...@@ -187,18 +187,18 @@ void MemoryProgramCache::remove(const egl::BlobCache::Key &programHash)
mBlobCache.remove(programHash); mBlobCache.remove(programHash);
} }
void MemoryProgramCache::putProgram(const egl::BlobCache::Key &programHash, angle::Result MemoryProgramCache::putProgram(const egl::BlobCache::Key &programHash,
const Context *context, const Context *context,
const Program *program) const Program *program)
{ {
// If caching is effectively disabled, don't bother serializing the program. // If caching is effectively disabled, don't bother serializing the program.
if (!mBlobCache.isCachingEnabled()) if (!mBlobCache.isCachingEnabled())
{ {
return; return angle::Result::Incomplete;
} }
angle::MemoryBuffer serializedProgram; angle::MemoryBuffer serializedProgram;
program->serialize(context, &serializedProgram); ANGLE_TRY(program->serialize(context, &serializedProgram));
ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramBinarySizeBytes", ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramBinarySizeBytes",
static_cast<int>(serializedProgram.size())); static_cast<int>(serializedProgram.size()));
...@@ -210,26 +210,32 @@ void MemoryProgramCache::putProgram(const egl::BlobCache::Key &programHash, ...@@ -210,26 +210,32 @@ void MemoryProgramCache::putProgram(const egl::BlobCache::Key &programHash,
serializedProgram.data()); serializedProgram.data());
mBlobCache.put(programHash, std::move(serializedProgram)); mBlobCache.put(programHash, std::move(serializedProgram));
return angle::Result::Continue;
} }
void MemoryProgramCache::updateProgram(const Context *context, const Program *program) angle::Result MemoryProgramCache::updateProgram(const Context *context, const Program *program)
{ {
egl::BlobCache::Key programHash; egl::BlobCache::Key programHash;
ComputeHash(context, program, &programHash); ComputeHash(context, program, &programHash);
putProgram(programHash, context, program); return putProgram(programHash, context, program);
} }
void MemoryProgramCache::putBinary(const egl::BlobCache::Key &programHash, bool MemoryProgramCache::putBinary(const egl::BlobCache::Key &programHash,
const uint8_t *binary, const uint8_t *binary,
size_t length) size_t length)
{ {
// Copy the binary. // Copy the binary.
angle::MemoryBuffer newEntry; angle::MemoryBuffer newEntry;
newEntry.resize(length); if (!newEntry.resize(length))
{
return false;
}
memcpy(newEntry.data(), binary, length); memcpy(newEntry.data(), binary, length);
// Store the binary. // Store the binary.
mBlobCache.populate(programHash, std::move(newEntry)); mBlobCache.populate(programHash, std::move(newEntry));
return true;
} }
void MemoryProgramCache::clear() void MemoryProgramCache::clear()
......
...@@ -46,16 +46,18 @@ class MemoryProgramCache final : angle::NonCopyable ...@@ -46,16 +46,18 @@ class MemoryProgramCache final : angle::NonCopyable
void remove(const egl::BlobCache::Key &programHash); void remove(const egl::BlobCache::Key &programHash);
// Helper method that serializes a program. // Helper method that serializes a program.
void putProgram(const egl::BlobCache::Key &programHash, angle::Result putProgram(const egl::BlobCache::Key &programHash,
const Context *context, const Context *context,
const Program *program); const Program *program);
// Same as putProgram but computes the hash. // Same as putProgram but computes the hash.
void updateProgram(const Context *context, const Program *program); angle::Result updateProgram(const Context *context, const Program *program);
// Store a binary directly. TODO(syoussefi): deprecated. Will be removed once Chrome supports // Store a binary directly. TODO(syoussefi): deprecated. Will be removed once Chrome supports
// EGL_ANDROID_blob_cache. http://anglebug.com/2516 // EGL_ANDROID_blob_cache. http://anglebug.com/2516
void putBinary(const egl::BlobCache::Key &programHash, const uint8_t *binary, size_t length); ANGLE_NO_DISCARD bool putBinary(const egl::BlobCache::Key &programHash,
const uint8_t *binary,
size_t length);
// Check the cache, and deserialize and load the program if found. Evict existing hash if load // Check the cache, and deserialize and load the program if found. Evict existing hash if load
// fails. // fails.
......
...@@ -1674,7 +1674,12 @@ void Program::resolveLinkImpl(const Context *context) ...@@ -1674,7 +1674,12 @@ void Program::resolveLinkImpl(const Context *context)
(mState.mLinkedTransformFeedbackVaryings.empty() || (mState.mLinkedTransformFeedbackVaryings.empty() ||
!context->getFrontendFeatures().disableProgramCachingForTransformFeedback.enabled)) !context->getFrontendFeatures().disableProgramCachingForTransformFeedback.enabled))
{ {
cache->putProgram(linkingState->programHash, context, this); if (cache->putProgram(linkingState->programHash, context, this) == angle::Result::Stop)
{
// Don't fail linking if putting the program binary into the cache fails, the program is
// still usable.
WARN() << "Failed to save linked program to memory program cache.";
}
} }
} }
...@@ -1944,7 +1949,7 @@ angle::Result Program::saveBinary(Context *context, ...@@ -1944,7 +1949,7 @@ angle::Result Program::saveBinary(Context *context,
} }
angle::MemoryBuffer memoryBuf; angle::MemoryBuffer memoryBuf;
serialize(context, &memoryBuf); ANGLE_TRY(serialize(context, &memoryBuf));
GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size()); GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
const uint8_t *streamState = memoryBuf.data(); const uint8_t *streamState = memoryBuf.data();
...@@ -5006,7 +5011,7 @@ angle::Result Program::syncState(const Context *context) ...@@ -5006,7 +5011,7 @@ angle::Result Program::syncState(const Context *context)
return angle::Result::Continue; return angle::Result::Continue;
} }
void Program::serialize(const Context *context, angle::MemoryBuffer *binaryOut) const angle::Result Program::serialize(const Context *context, angle::MemoryBuffer *binaryOut) const
{ {
BinaryOutputStream stream; BinaryOutputStream stream;
...@@ -5196,8 +5201,14 @@ void Program::serialize(const Context *context, angle::MemoryBuffer *binaryOut) ...@@ -5196,8 +5201,14 @@ void Program::serialize(const Context *context, angle::MemoryBuffer *binaryOut)
mProgram->save(context, &stream); mProgram->save(context, &stream);
ASSERT(binaryOut); ASSERT(binaryOut);
binaryOut->resize(stream.length()); if (!binaryOut->resize(stream.length()))
{
WARN() << "Failed to allocate enough memory to serialize a program. (" << stream.length()
<< " bytes )";
return angle::Result::Incomplete;
}
memcpy(binaryOut->data(), stream.data(), stream.length()); memcpy(binaryOut->data(), stream.data(), stream.length());
return angle::Result::Continue;
} }
angle::Result Program::deserialize(const Context *context, angle::Result Program::deserialize(const Context *context,
......
...@@ -972,7 +972,7 @@ class Program final : angle::NonCopyable, public LabeledObject ...@@ -972,7 +972,7 @@ class Program final : angle::NonCopyable, public LabeledObject
ANGLE_INLINE bool hasAnyDirtyBit() const { return mDirtyBits.any(); } ANGLE_INLINE bool hasAnyDirtyBit() const { return mDirtyBits.any(); }
// Writes a program's binary to the output memory buffer. // Writes a program's binary to the output memory buffer.
void serialize(const Context *context, angle::MemoryBuffer *binaryOut) const; angle::Result serialize(const Context *context, angle::MemoryBuffer *binaryOut) const;
rx::Serial serial() const { return mSerial; } rx::Serial serial() const { return mSerial; }
......
...@@ -667,7 +667,7 @@ angle::Result Context11::triggerDrawCallProgramRecompilation(const gl::Context * ...@@ -667,7 +667,7 @@ angle::Result Context11::triggerDrawCallProgramRecompilation(const gl::Context *
// Refresh the program cache entry. // Refresh the program cache entry.
if (mMemoryProgramCache) if (mMemoryProgramCache)
{ {
mMemoryProgramCache->updateProgram(context, program); ANGLE_TRY(mMemoryProgramCache->updateProgram(context, program));
} }
return angle::Result::Continue; return angle::Result::Continue;
...@@ -705,7 +705,7 @@ angle::Result Context11::triggerDispatchCallProgramRecompilation(const gl::Conte ...@@ -705,7 +705,7 @@ angle::Result Context11::triggerDispatchCallProgramRecompilation(const gl::Conte
// Refresh the program cache entry. // Refresh the program cache entry.
if (mMemoryProgramCache) if (mMemoryProgramCache)
{ {
mMemoryProgramCache->updateProgram(context, program); ANGLE_TRY(mMemoryProgramCache->updateProgram(context, program));
} }
return angle::Result::Continue; return angle::Result::Continue;
......
...@@ -457,7 +457,11 @@ const D3D11_SUBRESOURCE_DATA *ResourceManager11::createInitDataIfNeeded<ID3D11Te ...@@ -457,7 +457,11 @@ const D3D11_SUBRESOURCE_DATA *ResourceManager11::createInitDataIfNeeded<ID3D11Te
size_t requiredSize = static_cast<size_t>(ComputeMemoryUsage(desc)); size_t requiredSize = static_cast<size_t>(ComputeMemoryUsage(desc));
if (mZeroMemory.size() < requiredSize) if (mZeroMemory.size() < requiredSize)
{ {
mZeroMemory.resize(requiredSize); if (!mZeroMemory.resize(requiredSize))
{
ERR() << "Failed to allocate D3D texture initialization data.";
return nullptr;
}
mZeroMemory.fill(kDebugInitTextureDataValue); mZeroMemory.fill(kDebugInitTextureDataValue);
} }
...@@ -503,7 +507,11 @@ const D3D11_SUBRESOURCE_DATA *ResourceManager11::createInitDataIfNeeded<ID3D11Te ...@@ -503,7 +507,11 @@ const D3D11_SUBRESOURCE_DATA *ResourceManager11::createInitDataIfNeeded<ID3D11Te
size_t requiredSize = static_cast<size_t>(ComputeMemoryUsage(desc)); size_t requiredSize = static_cast<size_t>(ComputeMemoryUsage(desc));
if (mZeroMemory.size() < requiredSize) if (mZeroMemory.size() < requiredSize)
{ {
mZeroMemory.resize(requiredSize); if (!mZeroMemory.resize(requiredSize))
{
ERR() << "Failed to allocate D3D texture initialization data.";
return nullptr;
}
mZeroMemory.fill(kDebugInitTextureDataValue); mZeroMemory.fill(kDebugInitTextureDataValue);
} }
......
...@@ -88,7 +88,7 @@ BufferMtl::~BufferMtl() {} ...@@ -88,7 +88,7 @@ BufferMtl::~BufferMtl() {}
void BufferMtl::destroy(const gl::Context *context) void BufferMtl::destroy(const gl::Context *context)
{ {
ContextMtl *contextMtl = mtl::GetImpl(context); ContextMtl *contextMtl = mtl::GetImpl(context);
mShadowCopy.resize(0); mShadowCopy.clear();
mBufferPool.destroy(contextMtl); mBufferPool.destroy(contextMtl);
mBuffer = nullptr; mBuffer = nullptr;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment