1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "gfxReusableSharedImageSurfaceWrapper.h"
6 #include "gfxSharedImageSurface.h"
7 #include "mozilla/layers/ISurfaceAllocator.h"
9 using mozilla::ipc::Shmem
;
10 using mozilla::layers::ISurfaceAllocator
;
12 gfxReusableSharedImageSurfaceWrapper::gfxReusableSharedImageSurfaceWrapper(ISurfaceAllocator
* aAllocator
,
13 gfxSharedImageSurface
* aSurface
)
14 : mAllocator(aAllocator
)
17 MOZ_COUNT_CTOR(gfxReusableSharedImageSurfaceWrapper
);
21 gfxReusableSharedImageSurfaceWrapper::~gfxReusableSharedImageSurfaceWrapper()
23 MOZ_COUNT_DTOR(gfxReusableSharedImageSurfaceWrapper
);
28 gfxReusableSharedImageSurfaceWrapper::ReadLock()
30 NS_ASSERT_OWNINGTHREAD(gfxReusableSharedImageSurfaceWrapper
);
35 gfxReusableSharedImageSurfaceWrapper::ReadUnlock()
37 int32_t readCount
= mSurface
->ReadUnlock();
38 NS_ABORT_IF_FALSE(readCount
>= 0, "Read count should not be negative");
41 mAllocator
->DeallocShmem(mSurface
->GetShmem());
45 gfxReusableSurfaceWrapper
*
46 gfxReusableSharedImageSurfaceWrapper::GetWritable(gfxImageSurface
** aSurface
)
48 NS_ASSERT_OWNINGTHREAD(gfxReusableSharedImageSurfaceWrapper
);
50 int32_t readCount
= mSurface
->GetReadCount();
51 NS_ABORT_IF_FALSE(readCount
> 0, "A ReadLock must be held when calling GetWritable");
57 // Something else is reading the surface, copy it
58 nsRefPtr
<gfxSharedImageSurface
> copySurface
=
59 gfxSharedImageSurface::CreateUnsafe(mAllocator
.get(), mSurface
->GetSize(), mSurface
->Format());
60 copySurface
->CopyFrom(mSurface
);
61 *aSurface
= copySurface
;
63 // We need to create a new wrapper since this wrapper has an external ReadLock
64 gfxReusableSurfaceWrapper
* wrapper
= new gfxReusableSharedImageSurfaceWrapper(mAllocator
, copySurface
);
66 // No need to release the ReadLock on the surface, this will happen when
67 // the wrapper is destroyed.
73 gfxReusableSharedImageSurfaceWrapper::GetReadOnlyData() const
75 NS_ABORT_IF_FALSE(mSurface
->GetReadCount() > 0, "Should have read lock");
76 return mSurface
->Data();
80 gfxReusableSharedImageSurfaceWrapper::Format()
82 return mSurface
->Format();
86 gfxReusableSharedImageSurfaceWrapper::GetShmem()
88 return mSurface
->GetShmem();
91 /* static */ already_AddRefed
<gfxReusableSharedImageSurfaceWrapper
>
92 gfxReusableSharedImageSurfaceWrapper::Open(ISurfaceAllocator
* aAllocator
, const Shmem
& aShmem
)
94 nsRefPtr
<gfxSharedImageSurface
> sharedImage
= gfxSharedImageSurface::Open(aShmem
);
95 nsRefPtr
<gfxReusableSharedImageSurfaceWrapper
> wrapper
= new gfxReusableSharedImageSurfaceWrapper(aAllocator
, sharedImage
);
96 wrapper
->ReadUnlock();
97 return wrapper
.forget();