|
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/. */ |
|
4 |
|
5 #include "gfxReusableSharedImageSurfaceWrapper.h" |
|
6 #include "gfxSharedImageSurface.h" |
|
7 #include "mozilla/layers/ISurfaceAllocator.h" |
|
8 |
|
9 using mozilla::ipc::Shmem; |
|
10 using mozilla::layers::ISurfaceAllocator; |
|
11 |
|
12 gfxReusableSharedImageSurfaceWrapper::gfxReusableSharedImageSurfaceWrapper(ISurfaceAllocator* aAllocator, |
|
13 gfxSharedImageSurface* aSurface) |
|
14 : mAllocator(aAllocator) |
|
15 , mSurface(aSurface) |
|
16 { |
|
17 MOZ_COUNT_CTOR(gfxReusableSharedImageSurfaceWrapper); |
|
18 ReadLock(); |
|
19 } |
|
20 |
|
21 gfxReusableSharedImageSurfaceWrapper::~gfxReusableSharedImageSurfaceWrapper() |
|
22 { |
|
23 MOZ_COUNT_DTOR(gfxReusableSharedImageSurfaceWrapper); |
|
24 ReadUnlock(); |
|
25 } |
|
26 |
|
27 void |
|
28 gfxReusableSharedImageSurfaceWrapper::ReadLock() |
|
29 { |
|
30 NS_ASSERT_OWNINGTHREAD(gfxReusableSharedImageSurfaceWrapper); |
|
31 mSurface->ReadLock(); |
|
32 } |
|
33 |
|
34 void |
|
35 gfxReusableSharedImageSurfaceWrapper::ReadUnlock() |
|
36 { |
|
37 int32_t readCount = mSurface->ReadUnlock(); |
|
38 NS_ABORT_IF_FALSE(readCount >= 0, "Read count should not be negative"); |
|
39 |
|
40 if (readCount == 0) { |
|
41 mAllocator->DeallocShmem(mSurface->GetShmem()); |
|
42 } |
|
43 } |
|
44 |
|
45 gfxReusableSurfaceWrapper* |
|
46 gfxReusableSharedImageSurfaceWrapper::GetWritable(gfxImageSurface** aSurface) |
|
47 { |
|
48 NS_ASSERT_OWNINGTHREAD(gfxReusableSharedImageSurfaceWrapper); |
|
49 |
|
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; |
|
55 } |
|
56 |
|
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; |
|
62 |
|
63 // We need to create a new wrapper since this wrapper has an external ReadLock |
|
64 gfxReusableSurfaceWrapper* wrapper = new gfxReusableSharedImageSurfaceWrapper(mAllocator, copySurface); |
|
65 |
|
66 // No need to release the ReadLock on the surface, this will happen when |
|
67 // the wrapper is destroyed. |
|
68 |
|
69 return wrapper; |
|
70 } |
|
71 |
|
72 const unsigned char* |
|
73 gfxReusableSharedImageSurfaceWrapper::GetReadOnlyData() const |
|
74 { |
|
75 NS_ABORT_IF_FALSE(mSurface->GetReadCount() > 0, "Should have read lock"); |
|
76 return mSurface->Data(); |
|
77 } |
|
78 |
|
79 gfxImageFormat |
|
80 gfxReusableSharedImageSurfaceWrapper::Format() |
|
81 { |
|
82 return mSurface->Format(); |
|
83 } |
|
84 |
|
85 Shmem& |
|
86 gfxReusableSharedImageSurfaceWrapper::GetShmem() |
|
87 { |
|
88 return mSurface->GetShmem(); |
|
89 } |
|
90 |
|
91 /* static */ already_AddRefed<gfxReusableSharedImageSurfaceWrapper> |
|
92 gfxReusableSharedImageSurfaceWrapper::Open(ISurfaceAllocator* aAllocator, const Shmem& aShmem) |
|
93 { |
|
94 nsRefPtr<gfxSharedImageSurface> sharedImage = gfxSharedImageSurface::Open(aShmem); |
|
95 nsRefPtr<gfxReusableSharedImageSurfaceWrapper> wrapper = new gfxReusableSharedImageSurfaceWrapper(aAllocator, sharedImage); |
|
96 wrapper->ReadUnlock(); |
|
97 return wrapper.forget(); |
|
98 } |