Add an RAII class to lock and unlock textures. (bug 1222863 part 1, r=nical)

--HG--
extra : rebase_source : 730527b8495ea92a2d6fffc8bd6a6f97b2c3b6d3
This commit is contained in:
David Anderson 2015-11-10 23:58:21 -08:00
parent 609ea83a81
commit 9b96e4817d
3 changed files with 36 additions and 12 deletions

View File

@ -650,12 +650,11 @@ CairoImage::GetTextureClient(CompositableClient *aClient)
return nullptr;
}
if (!textureClient->Lock(OpenMode::OPEN_WRITE_ONLY)) {
TextureClientAutoLock autoLock(textureClient, OpenMode::OPEN_WRITE_ONLY);
if (!autoLock.Succeeded()) {
return nullptr;
}
TextureClientAutoUnlock autoUnlock(textureClient);
textureClient->UpdateFromSurface(surface);
textureClient->SyncWithObject(forwarder->GetSyncObject());

View File

@ -12,6 +12,7 @@
#include "ImageTypes.h" // for StereoMode
#include "mozilla/Assertions.h" // for MOZ_ASSERT, etc
#include "mozilla/Attributes.h" // for override
#include "mozilla/DebugOnly.h"
#include "mozilla/RefPtr.h" // for RefPtr, RefCounted
#include "mozilla/gfx/2D.h" // for DrawTarget
#include "mozilla/gfx/Point.h" // for IntSize
@ -720,17 +721,39 @@ protected:
size_t mBufSize;
};
struct TextureClientAutoUnlock
// Automatically lock and unlock a texture. Since texture locking is fallible,
// Succeeded() must be checked on the guard object before proceeding.
class MOZ_RAII TextureClientAutoLock
{
TextureClient* mTexture;
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER;
explicit TextureClientAutoUnlock(TextureClient* aTexture)
: mTexture(aTexture) {}
~TextureClientAutoUnlock()
public:
TextureClientAutoLock(TextureClient* aTexture, OpenMode aMode
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
: mTexture(aTexture),
mSucceeded(false)
{
mTexture->Unlock();
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
mSucceeded = mTexture->Lock(aMode);
mChecked = false;
}
~TextureClientAutoLock() {
MOZ_ASSERT(mChecked);
if (mSucceeded) {
mTexture->Unlock();
}
}
bool Succeeded() {
mChecked = true;
return mSucceeded;
}
private:
TextureClient* mTexture;
DebugOnly<bool> mChecked;
bool mSucceeded;
};
class KeepAlive

View File

@ -90,11 +90,13 @@ SharedPlanarYCbCrImage::SetData(const PlanarYCbCrData& aData)
}
MOZ_ASSERT(mTextureClient->AsTextureClientYCbCr());
if (!mTextureClient->Lock(OpenMode::OPEN_WRITE_ONLY)) {
TextureClientAutoLock autoLock(mTextureClient, OpenMode::OPEN_WRITE_ONLY);
if (!autoLock.Succeeded()) {
MOZ_ASSERT(false, "Failed to lock the texture.");
return false;
}
TextureClientAutoUnlock unlock(mTextureClient);
if (!mTextureClient->AsTextureClientYCbCr()->UpdateYCbCr(aData)) {
MOZ_ASSERT(false, "Failed to copy YCbCr data into the TextureClient");
return false;