Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "gfxReusableSharedImageSurfaceWrapper.h" |
michael@0 | 6 | #include "gfxSharedImageSurface.h" |
michael@0 | 7 | #include "mozilla/layers/ISurfaceAllocator.h" |
michael@0 | 8 | |
michael@0 | 9 | using mozilla::ipc::Shmem; |
michael@0 | 10 | using mozilla::layers::ISurfaceAllocator; |
michael@0 | 11 | |
michael@0 | 12 | gfxReusableSharedImageSurfaceWrapper::gfxReusableSharedImageSurfaceWrapper(ISurfaceAllocator* aAllocator, |
michael@0 | 13 | gfxSharedImageSurface* aSurface) |
michael@0 | 14 | : mAllocator(aAllocator) |
michael@0 | 15 | , mSurface(aSurface) |
michael@0 | 16 | { |
michael@0 | 17 | MOZ_COUNT_CTOR(gfxReusableSharedImageSurfaceWrapper); |
michael@0 | 18 | ReadLock(); |
michael@0 | 19 | } |
michael@0 | 20 | |
michael@0 | 21 | gfxReusableSharedImageSurfaceWrapper::~gfxReusableSharedImageSurfaceWrapper() |
michael@0 | 22 | { |
michael@0 | 23 | MOZ_COUNT_DTOR(gfxReusableSharedImageSurfaceWrapper); |
michael@0 | 24 | ReadUnlock(); |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | void |
michael@0 | 28 | gfxReusableSharedImageSurfaceWrapper::ReadLock() |
michael@0 | 29 | { |
michael@0 | 30 | NS_ASSERT_OWNINGTHREAD(gfxReusableSharedImageSurfaceWrapper); |
michael@0 | 31 | mSurface->ReadLock(); |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | void |
michael@0 | 35 | gfxReusableSharedImageSurfaceWrapper::ReadUnlock() |
michael@0 | 36 | { |
michael@0 | 37 | int32_t readCount = mSurface->ReadUnlock(); |
michael@0 | 38 | NS_ABORT_IF_FALSE(readCount >= 0, "Read count should not be negative"); |
michael@0 | 39 | |
michael@0 | 40 | if (readCount == 0) { |
michael@0 | 41 | mAllocator->DeallocShmem(mSurface->GetShmem()); |
michael@0 | 42 | } |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | gfxReusableSurfaceWrapper* |
michael@0 | 46 | gfxReusableSharedImageSurfaceWrapper::GetWritable(gfxImageSurface** aSurface) |
michael@0 | 47 | { |
michael@0 | 48 | NS_ASSERT_OWNINGTHREAD(gfxReusableSharedImageSurfaceWrapper); |
michael@0 | 49 | |
michael@0 | 50 | int32_t readCount = mSurface->GetReadCount(); |
michael@0 | 51 | NS_ABORT_IF_FALSE(readCount > 0, "A ReadLock must be held when calling GetWritable"); |
michael@0 | 52 | if (readCount == 1) { |
michael@0 | 53 | *aSurface = mSurface; |
michael@0 | 54 | return this; |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | // Something else is reading the surface, copy it |
michael@0 | 58 | nsRefPtr<gfxSharedImageSurface> copySurface = |
michael@0 | 59 | gfxSharedImageSurface::CreateUnsafe(mAllocator.get(), mSurface->GetSize(), mSurface->Format()); |
michael@0 | 60 | copySurface->CopyFrom(mSurface); |
michael@0 | 61 | *aSurface = copySurface; |
michael@0 | 62 | |
michael@0 | 63 | // We need to create a new wrapper since this wrapper has an external ReadLock |
michael@0 | 64 | gfxReusableSurfaceWrapper* wrapper = new gfxReusableSharedImageSurfaceWrapper(mAllocator, copySurface); |
michael@0 | 65 | |
michael@0 | 66 | // No need to release the ReadLock on the surface, this will happen when |
michael@0 | 67 | // the wrapper is destroyed. |
michael@0 | 68 | |
michael@0 | 69 | return wrapper; |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | const unsigned char* |
michael@0 | 73 | gfxReusableSharedImageSurfaceWrapper::GetReadOnlyData() const |
michael@0 | 74 | { |
michael@0 | 75 | NS_ABORT_IF_FALSE(mSurface->GetReadCount() > 0, "Should have read lock"); |
michael@0 | 76 | return mSurface->Data(); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | gfxImageFormat |
michael@0 | 80 | gfxReusableSharedImageSurfaceWrapper::Format() |
michael@0 | 81 | { |
michael@0 | 82 | return mSurface->Format(); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | Shmem& |
michael@0 | 86 | gfxReusableSharedImageSurfaceWrapper::GetShmem() |
michael@0 | 87 | { |
michael@0 | 88 | return mSurface->GetShmem(); |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | /* static */ already_AddRefed<gfxReusableSharedImageSurfaceWrapper> |
michael@0 | 92 | gfxReusableSharedImageSurfaceWrapper::Open(ISurfaceAllocator* aAllocator, const Shmem& aShmem) |
michael@0 | 93 | { |
michael@0 | 94 | nsRefPtr<gfxSharedImageSurface> sharedImage = gfxSharedImageSurface::Open(aShmem); |
michael@0 | 95 | nsRefPtr<gfxReusableSharedImageSurfaceWrapper> wrapper = new gfxReusableSharedImageSurfaceWrapper(aAllocator, sharedImage); |
michael@0 | 96 | wrapper->ReadUnlock(); |
michael@0 | 97 | return wrapper.forget(); |
michael@0 | 98 | } |