Bumping manifests a=b2g-bump
[gecko.git] / gfx / thebes / gfxReusableSharedImageSurfaceWrapper.cpp
bloba02955572570b22cf2d9f2566e2f8ccf7ab5b983
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)
15 , mSurface(aSurface)
17 MOZ_COUNT_CTOR(gfxReusableSharedImageSurfaceWrapper);
18 ReadLock();
21 gfxReusableSharedImageSurfaceWrapper::~gfxReusableSharedImageSurfaceWrapper()
23 MOZ_COUNT_DTOR(gfxReusableSharedImageSurfaceWrapper);
24 ReadUnlock();
27 void
28 gfxReusableSharedImageSurfaceWrapper::ReadLock()
30 NS_ASSERT_OWNINGTHREAD(gfxReusableSharedImageSurfaceWrapper);
31 mSurface->ReadLock();
34 void
35 gfxReusableSharedImageSurfaceWrapper::ReadUnlock()
37 int32_t readCount = mSurface->ReadUnlock();
38 NS_ABORT_IF_FALSE(readCount >= 0, "Read count should not be negative");
40 if (readCount == 0) {
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");
52 if (readCount == 1) {
53 *aSurface = mSurface;
54 return this;
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.
69 return wrapper;
72 const unsigned char*
73 gfxReusableSharedImageSurfaceWrapper::GetReadOnlyData() const
75 NS_ABORT_IF_FALSE(mSurface->GetReadCount() > 0, "Should have read lock");
76 return mSurface->Data();
79 gfxImageFormat
80 gfxReusableSharedImageSurfaceWrapper::Format()
82 return mSurface->Format();
85 Shmem&
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();