gfx/thebes/gfxReusableSharedImageSurfaceWrapper.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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)
    16 {
    17   MOZ_COUNT_CTOR(gfxReusableSharedImageSurfaceWrapper);
    18   ReadLock();
    19 }
    21 gfxReusableSharedImageSurfaceWrapper::~gfxReusableSharedImageSurfaceWrapper()
    22 {
    23   MOZ_COUNT_DTOR(gfxReusableSharedImageSurfaceWrapper);
    24   ReadUnlock();
    25 }
    27 void
    28 gfxReusableSharedImageSurfaceWrapper::ReadLock()
    29 {
    30   NS_ASSERT_OWNINGTHREAD(gfxReusableSharedImageSurfaceWrapper);
    31   mSurface->ReadLock();
    32 }
    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");
    40   if (readCount == 0) {
    41     mAllocator->DeallocShmem(mSurface->GetShmem());
    42   }
    43 }
    45 gfxReusableSurfaceWrapper*
    46 gfxReusableSharedImageSurfaceWrapper::GetWritable(gfxImageSurface** aSurface)
    47 {
    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;
    55   }
    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;
    70 }
    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 }
    79 gfxImageFormat
    80 gfxReusableSharedImageSurfaceWrapper::Format()
    81 {
    82   return mSurface->Format();
    83 }
    85 Shmem&
    86 gfxReusableSharedImageSurfaceWrapper::GetShmem()
    87 {
    88   return mSurface->GetShmem();
    89 }
    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 }

mercurial