1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/thebes/gfxReusableSharedImageSurfaceWrapper.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,98 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "gfxReusableSharedImageSurfaceWrapper.h" 1.9 +#include "gfxSharedImageSurface.h" 1.10 +#include "mozilla/layers/ISurfaceAllocator.h" 1.11 + 1.12 +using mozilla::ipc::Shmem; 1.13 +using mozilla::layers::ISurfaceAllocator; 1.14 + 1.15 +gfxReusableSharedImageSurfaceWrapper::gfxReusableSharedImageSurfaceWrapper(ISurfaceAllocator* aAllocator, 1.16 + gfxSharedImageSurface* aSurface) 1.17 + : mAllocator(aAllocator) 1.18 + , mSurface(aSurface) 1.19 +{ 1.20 + MOZ_COUNT_CTOR(gfxReusableSharedImageSurfaceWrapper); 1.21 + ReadLock(); 1.22 +} 1.23 + 1.24 +gfxReusableSharedImageSurfaceWrapper::~gfxReusableSharedImageSurfaceWrapper() 1.25 +{ 1.26 + MOZ_COUNT_DTOR(gfxReusableSharedImageSurfaceWrapper); 1.27 + ReadUnlock(); 1.28 +} 1.29 + 1.30 +void 1.31 +gfxReusableSharedImageSurfaceWrapper::ReadLock() 1.32 +{ 1.33 + NS_ASSERT_OWNINGTHREAD(gfxReusableSharedImageSurfaceWrapper); 1.34 + mSurface->ReadLock(); 1.35 +} 1.36 + 1.37 +void 1.38 +gfxReusableSharedImageSurfaceWrapper::ReadUnlock() 1.39 +{ 1.40 + int32_t readCount = mSurface->ReadUnlock(); 1.41 + NS_ABORT_IF_FALSE(readCount >= 0, "Read count should not be negative"); 1.42 + 1.43 + if (readCount == 0) { 1.44 + mAllocator->DeallocShmem(mSurface->GetShmem()); 1.45 + } 1.46 +} 1.47 + 1.48 +gfxReusableSurfaceWrapper* 1.49 +gfxReusableSharedImageSurfaceWrapper::GetWritable(gfxImageSurface** aSurface) 1.50 +{ 1.51 + NS_ASSERT_OWNINGTHREAD(gfxReusableSharedImageSurfaceWrapper); 1.52 + 1.53 + int32_t readCount = mSurface->GetReadCount(); 1.54 + NS_ABORT_IF_FALSE(readCount > 0, "A ReadLock must be held when calling GetWritable"); 1.55 + if (readCount == 1) { 1.56 + *aSurface = mSurface; 1.57 + return this; 1.58 + } 1.59 + 1.60 + // Something else is reading the surface, copy it 1.61 + nsRefPtr<gfxSharedImageSurface> copySurface = 1.62 + gfxSharedImageSurface::CreateUnsafe(mAllocator.get(), mSurface->GetSize(), mSurface->Format()); 1.63 + copySurface->CopyFrom(mSurface); 1.64 + *aSurface = copySurface; 1.65 + 1.66 + // We need to create a new wrapper since this wrapper has an external ReadLock 1.67 + gfxReusableSurfaceWrapper* wrapper = new gfxReusableSharedImageSurfaceWrapper(mAllocator, copySurface); 1.68 + 1.69 + // No need to release the ReadLock on the surface, this will happen when 1.70 + // the wrapper is destroyed. 1.71 + 1.72 + return wrapper; 1.73 +} 1.74 + 1.75 +const unsigned char* 1.76 +gfxReusableSharedImageSurfaceWrapper::GetReadOnlyData() const 1.77 +{ 1.78 + NS_ABORT_IF_FALSE(mSurface->GetReadCount() > 0, "Should have read lock"); 1.79 + return mSurface->Data(); 1.80 +} 1.81 + 1.82 +gfxImageFormat 1.83 +gfxReusableSharedImageSurfaceWrapper::Format() 1.84 +{ 1.85 + return mSurface->Format(); 1.86 +} 1.87 + 1.88 +Shmem& 1.89 +gfxReusableSharedImageSurfaceWrapper::GetShmem() 1.90 +{ 1.91 + return mSurface->GetShmem(); 1.92 +} 1.93 + 1.94 +/* static */ already_AddRefed<gfxReusableSharedImageSurfaceWrapper> 1.95 +gfxReusableSharedImageSurfaceWrapper::Open(ISurfaceAllocator* aAllocator, const Shmem& aShmem) 1.96 +{ 1.97 + nsRefPtr<gfxSharedImageSurface> sharedImage = gfxSharedImageSurface::Open(aShmem); 1.98 + nsRefPtr<gfxReusableSharedImageSurfaceWrapper> wrapper = new gfxReusableSharedImageSurfaceWrapper(aAllocator, sharedImage); 1.99 + wrapper->ReadUnlock(); 1.100 + return wrapper.forget(); 1.101 +}