2012-04-17 23:21:07 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2013-08-19 13:59:25 +00:00
|
|
|
#include "gfxReusableSharedImageSurfaceWrapper.h"
|
2013-08-19 13:59:22 +00:00
|
|
|
#include "gfxSharedImageSurface.h"
|
2013-10-07 23:15:59 +00:00
|
|
|
#include "mozilla/layers/ISurfaceAllocator.h"
|
2012-04-17 23:21:07 +00:00
|
|
|
|
2013-08-19 13:59:22 +00:00
|
|
|
using mozilla::ipc::Shmem;
|
|
|
|
using mozilla::layers::ISurfaceAllocator;
|
|
|
|
|
2013-08-19 13:59:25 +00:00
|
|
|
gfxReusableSharedImageSurfaceWrapper::gfxReusableSharedImageSurfaceWrapper(ISurfaceAllocator* aAllocator,
|
|
|
|
gfxSharedImageSurface* aSurface)
|
2013-08-19 13:59:22 +00:00
|
|
|
: mAllocator(aAllocator)
|
|
|
|
, mSurface(aSurface)
|
2012-04-17 23:21:07 +00:00
|
|
|
{
|
2013-08-19 13:59:25 +00:00
|
|
|
MOZ_COUNT_CTOR(gfxReusableSharedImageSurfaceWrapper);
|
2013-08-19 13:59:22 +00:00
|
|
|
ReadLock();
|
2012-04-17 23:21:07 +00:00
|
|
|
}
|
|
|
|
|
2013-08-19 13:59:25 +00:00
|
|
|
gfxReusableSharedImageSurfaceWrapper::~gfxReusableSharedImageSurfaceWrapper()
|
2012-04-17 23:21:07 +00:00
|
|
|
{
|
2013-08-19 13:59:25 +00:00
|
|
|
MOZ_COUNT_DTOR(gfxReusableSharedImageSurfaceWrapper);
|
2013-08-19 13:59:22 +00:00
|
|
|
ReadUnlock();
|
2012-04-17 23:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-08-19 13:59:25 +00:00
|
|
|
gfxReusableSharedImageSurfaceWrapper::ReadLock()
|
2012-04-17 23:21:07 +00:00
|
|
|
{
|
2013-09-17 16:34:47 +00:00
|
|
|
NS_ASSERT_OWNINGTHREAD(gfxReusableSharedImageSurfaceWrapper);
|
2013-08-19 13:59:22 +00:00
|
|
|
mSurface->ReadLock();
|
2012-04-17 23:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-08-19 13:59:25 +00:00
|
|
|
gfxReusableSharedImageSurfaceWrapper::ReadUnlock()
|
2012-04-17 23:21:07 +00:00
|
|
|
{
|
2013-08-19 13:59:22 +00:00
|
|
|
int32_t readCount = mSurface->ReadUnlock();
|
|
|
|
NS_ABORT_IF_FALSE(readCount >= 0, "Read count should not be negative");
|
|
|
|
|
|
|
|
if (readCount == 0) {
|
|
|
|
mAllocator->DeallocShmem(mSurface->GetShmem());
|
|
|
|
}
|
2012-04-17 23:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxReusableSurfaceWrapper*
|
2013-08-19 13:59:25 +00:00
|
|
|
gfxReusableSharedImageSurfaceWrapper::GetWritable(gfxImageSurface** aSurface)
|
2012-04-17 23:21:07 +00:00
|
|
|
{
|
2013-09-17 16:34:47 +00:00
|
|
|
NS_ASSERT_OWNINGTHREAD(gfxReusableSharedImageSurfaceWrapper);
|
2012-04-17 23:21:07 +00:00
|
|
|
|
2013-08-19 13:59:22 +00:00
|
|
|
int32_t readCount = mSurface->GetReadCount();
|
|
|
|
NS_ABORT_IF_FALSE(readCount > 0, "A ReadLock must be held when calling GetWritable");
|
|
|
|
if (readCount == 1) {
|
2012-04-17 23:21:07 +00:00
|
|
|
*aSurface = mSurface;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Something else is reading the surface, copy it
|
2013-08-19 13:59:22 +00:00
|
|
|
nsRefPtr<gfxSharedImageSurface> copySurface =
|
2013-12-03 18:44:38 +00:00
|
|
|
gfxSharedImageSurface::CreateUnsafe(mAllocator.get(), mSurface->GetSize(), mSurface->Format());
|
2012-04-17 23:21:07 +00:00
|
|
|
copySurface->CopyFrom(mSurface);
|
|
|
|
*aSurface = copySurface;
|
|
|
|
|
2013-08-19 13:59:22 +00:00
|
|
|
// We need to create a new wrapper since this wrapper has an external ReadLock
|
2013-08-19 13:59:25 +00:00
|
|
|
gfxReusableSurfaceWrapper* wrapper = new gfxReusableSharedImageSurfaceWrapper(mAllocator, copySurface);
|
2013-08-19 13:59:22 +00:00
|
|
|
|
|
|
|
// No need to release the ReadLock on the surface, this will happen when
|
|
|
|
// the wrapper is destroyed.
|
|
|
|
|
2012-04-17 23:21:07 +00:00
|
|
|
return wrapper;
|
|
|
|
}
|
|
|
|
|
2013-08-19 13:59:22 +00:00
|
|
|
const unsigned char*
|
2013-08-19 13:59:25 +00:00
|
|
|
gfxReusableSharedImageSurfaceWrapper::GetReadOnlyData() const
|
2013-08-19 13:59:22 +00:00
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(mSurface->GetReadCount() > 0, "Should have read lock");
|
|
|
|
return mSurface->Data();
|
|
|
|
}
|
|
|
|
|
2013-09-24 20:45:13 +00:00
|
|
|
gfxImageFormat
|
2013-08-19 13:59:25 +00:00
|
|
|
gfxReusableSharedImageSurfaceWrapper::Format()
|
2013-08-19 13:59:22 +00:00
|
|
|
{
|
|
|
|
return mSurface->Format();
|
|
|
|
}
|
|
|
|
|
|
|
|
Shmem&
|
2013-08-19 13:59:25 +00:00
|
|
|
gfxReusableSharedImageSurfaceWrapper::GetShmem()
|
2013-08-19 13:59:22 +00:00
|
|
|
{
|
|
|
|
return mSurface->GetShmem();
|
|
|
|
}
|
|
|
|
|
2013-08-19 13:59:25 +00:00
|
|
|
/* static */ already_AddRefed<gfxReusableSharedImageSurfaceWrapper>
|
|
|
|
gfxReusableSharedImageSurfaceWrapper::Open(ISurfaceAllocator* aAllocator, const Shmem& aShmem)
|
2013-08-19 13:59:22 +00:00
|
|
|
{
|
|
|
|
nsRefPtr<gfxSharedImageSurface> sharedImage = gfxSharedImageSurface::Open(aShmem);
|
2013-08-19 13:59:25 +00:00
|
|
|
nsRefPtr<gfxReusableSharedImageSurfaceWrapper> wrapper = new gfxReusableSharedImageSurfaceWrapper(aAllocator, sharedImage);
|
2013-08-19 13:59:22 +00:00
|
|
|
wrapper->ReadUnlock();
|
|
|
|
return wrapper.forget();
|
|
|
|
}
|