2017-10-28 02:10:06 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2017-01-18 18:12:32 +03:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef MOZILLA_GFX_SOURCESURFACESHAREDDATA_H_
|
|
|
|
#define MOZILLA_GFX_SOURCESURFACESHAREDDATA_H_
|
|
|
|
|
|
|
|
#include "mozilla/gfx/2D.h"
|
|
|
|
#include "mozilla/Mutex.h"
|
|
|
|
#include "mozilla/ipc/SharedMemoryBasic.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
2017-10-30 20:10:28 +03:00
|
|
|
class SourceSurfaceSharedData;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class is used to wrap shared (as in process) data buffers allocated by
|
|
|
|
* a SourceSurfaceSharedData object. It may live in the same process or a
|
|
|
|
* different process from the actual SourceSurfaceSharedData object.
|
|
|
|
*
|
|
|
|
* If it is in the same process, mBuf is the same object as that in the surface.
|
|
|
|
* It is a useful abstraction over just using the surface directly, because it
|
|
|
|
* can have a different lifetime from the surface; if the surface gets freed,
|
|
|
|
* consumers may continue accessing the data in the buffer. Releasing the
|
|
|
|
* original surface is a signal which feeds into SharedSurfacesParent to decide
|
|
|
|
* to release the SourceSurfaceSharedDataWrapper.
|
|
|
|
*
|
|
|
|
* If it is in a different process, mBuf is a new SharedMemoryBasic object which
|
|
|
|
* mapped in the given shared memory handle as read only memory.
|
|
|
|
*/
|
|
|
|
class SourceSurfaceSharedDataWrapper final : public DataSourceSurface {
|
|
|
|
typedef mozilla::ipc::SharedMemoryBasic SharedMemoryBasic;
|
|
|
|
|
|
|
|
public:
|
|
|
|
MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(SourceSurfaceSharedDataWrapper,
|
|
|
|
override)
|
|
|
|
|
|
|
|
SourceSurfaceSharedDataWrapper()
|
|
|
|
: mStride(0),
|
2018-04-23 14:57:15 +03:00
|
|
|
mConsumers(0),
|
2017-10-30 20:10:28 +03:00
|
|
|
mFormat(SurfaceFormat::UNKNOWN),
|
2018-06-16 17:42:33 +03:00
|
|
|
mCreatorPid(0),
|
2018-09-25 16:13:51 +03:00
|
|
|
mCreatorRef(true) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-10-30 20:10:28 +03:00
|
|
|
bool Init(const IntSize& aSize, int32_t aStride, SurfaceFormat aFormat,
|
|
|
|
const SharedMemoryBasic::Handle& aHandle,
|
|
|
|
base::ProcessId aCreatorPid);
|
|
|
|
|
|
|
|
void Init(SourceSurfaceSharedData* aSurface);
|
|
|
|
|
|
|
|
base::ProcessId GetCreatorPid() const { return mCreatorPid; }
|
|
|
|
|
|
|
|
int32_t Stride() override { return mStride; }
|
|
|
|
|
|
|
|
SurfaceType GetType() const override { return SurfaceType::DATA; }
|
|
|
|
IntSize GetSize() const override { return mSize; }
|
|
|
|
SurfaceFormat GetFormat() const override { return mFormat; }
|
|
|
|
|
|
|
|
uint8_t* GetData() override { return static_cast<uint8_t*>(mBuf->memory()); }
|
|
|
|
|
|
|
|
bool OnHeap() const override { return false; }
|
|
|
|
|
2018-04-18 01:08:20 +03:00
|
|
|
bool AddConsumer() { return ++mConsumers == 1; }
|
|
|
|
|
2018-09-25 16:13:51 +03:00
|
|
|
bool RemoveConsumer(bool aForCreator) {
|
2018-04-18 01:08:20 +03:00
|
|
|
MOZ_ASSERT(mConsumers > 0);
|
2018-09-25 16:13:51 +03:00
|
|
|
if (aForCreator) {
|
|
|
|
if (!mCreatorRef) {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Already released creator reference!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mCreatorRef = false;
|
|
|
|
}
|
2018-04-18 01:08:20 +03:00
|
|
|
return --mConsumers == 0;
|
|
|
|
}
|
|
|
|
|
2018-09-25 16:13:51 +03:00
|
|
|
uint32_t GetConsumers() const {
|
|
|
|
MOZ_ASSERT(mConsumers > 0);
|
|
|
|
return mConsumers;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasCreatorRef() const { return mCreatorRef; }
|
|
|
|
|
2017-10-30 20:10:28 +03:00
|
|
|
private:
|
|
|
|
size_t GetDataLength() const {
|
|
|
|
return static_cast<size_t>(mStride) * mSize.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t GetAlignedDataLength() const {
|
|
|
|
return mozilla::ipc::SharedMemory::PageAlignedSize(GetDataLength());
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t mStride;
|
2018-04-18 01:08:20 +03:00
|
|
|
uint32_t mConsumers;
|
2017-10-30 20:10:28 +03:00
|
|
|
IntSize mSize;
|
|
|
|
RefPtr<SharedMemoryBasic> mBuf;
|
|
|
|
SurfaceFormat mFormat;
|
|
|
|
base::ProcessId mCreatorPid;
|
2018-09-25 16:13:51 +03:00
|
|
|
bool mCreatorRef;
|
2017-10-30 20:10:28 +03:00
|
|
|
};
|
|
|
|
|
2017-01-18 18:12:32 +03:00
|
|
|
/**
|
|
|
|
* This class is used to wrap shared (as in process) data buffers used by a
|
|
|
|
* source surface.
|
|
|
|
*/
|
|
|
|
class SourceSurfaceSharedData final : public DataSourceSurface {
|
|
|
|
typedef mozilla::ipc::SharedMemoryBasic SharedMemoryBasic;
|
|
|
|
|
|
|
|
public:
|
|
|
|
MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(SourceSurfaceSharedData, override)
|
|
|
|
|
|
|
|
SourceSurfaceSharedData()
|
|
|
|
: mMutex("SourceSurfaceSharedData"),
|
|
|
|
mStride(0),
|
2017-10-30 20:10:28 +03:00
|
|
|
mHandleCount(0),
|
2017-01-18 18:12:32 +03:00
|
|
|
mFormat(SurfaceFormat::UNKNOWN),
|
|
|
|
mClosed(false),
|
|
|
|
mFinalized(false),
|
|
|
|
mShared(false) {}
|
|
|
|
|
2018-02-12 15:59:58 +03:00
|
|
|
/**
|
|
|
|
* Initialize the surface by creating a shared memory buffer with a size
|
|
|
|
* determined by aSize, aStride and aFormat. If aShare is true, it will also
|
|
|
|
* immediately attempt to share the surface with the GPU process via
|
|
|
|
* SharedSurfacesChild.
|
|
|
|
*/
|
2017-10-30 20:10:28 +03:00
|
|
|
bool Init(const IntSize& aSize, int32_t aStride, SurfaceFormat aFormat,
|
2018-02-12 15:59:58 +03:00
|
|
|
bool aShare = true);
|
2017-01-18 18:12:32 +03:00
|
|
|
|
|
|
|
uint8_t* GetData() override {
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
return GetDataInternal();
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t Stride() override { return mStride; }
|
|
|
|
|
|
|
|
SurfaceType GetType() const override { return SurfaceType::DATA_SHARED; }
|
|
|
|
IntSize GetSize() const override { return mSize; }
|
|
|
|
SurfaceFormat GetFormat() const override { return mFormat; }
|
|
|
|
|
|
|
|
void GuaranteePersistance() override;
|
|
|
|
|
2020-02-06 01:22:13 +03:00
|
|
|
void SizeOfExcludingThis(MallocSizeOf aMallocSizeOf,
|
|
|
|
SizeOfInfo& aInfo) const override;
|
2017-01-18 18:12:32 +03:00
|
|
|
|
|
|
|
bool OnHeap() const override { return false; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Although Map (and Moz2D in general) isn't normally threadsafe,
|
|
|
|
* we want to allow it for SourceSurfaceSharedData since it should
|
|
|
|
* always be fine (for reading at least).
|
|
|
|
*
|
|
|
|
* This is the same as the base class implementation except using
|
|
|
|
* mMapCount instead of mIsMapped since that breaks for multithread.
|
|
|
|
*
|
|
|
|
* Additionally if a reallocation happened while there were active
|
|
|
|
* mappings, then we guarantee that GetData will continue to return
|
|
|
|
* the same data pointer by retaining the old shared buffer until
|
|
|
|
* the last mapping is freed via Unmap.
|
|
|
|
*/
|
|
|
|
bool Map(MapType, MappedSurface* aMappedSurface) override {
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
++mMapCount;
|
|
|
|
aMappedSurface->mData = GetDataInternal();
|
|
|
|
aMappedSurface->mStride = mStride;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Unmap() override {
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
MOZ_ASSERT(mMapCount > 0);
|
|
|
|
if (--mMapCount == 0) {
|
|
|
|
mOldBuf = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a handle to share to another process for this buffer. Returns:
|
|
|
|
* NS_OK -- success, aHandle is valid.
|
|
|
|
* NS_ERROR_NOT_AVAILABLE -- handle was closed, need to reallocate.
|
|
|
|
* NS_ERROR_FAILURE -- failed to create a handle to share.
|
|
|
|
*/
|
|
|
|
nsresult ShareToProcess(base::ProcessId aPid,
|
|
|
|
SharedMemoryBasic::Handle& aHandle);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates the buffer is not expected to be shared with any more processes.
|
2017-02-22 17:30:22 +03:00
|
|
|
* May release the handle if possible (see CloseHandleInternal).
|
|
|
|
*/
|
2017-01-18 18:12:32 +03:00
|
|
|
void FinishedSharing() {
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mShared = true;
|
|
|
|
CloseHandleInternal();
|
|
|
|
}
|
|
|
|
|
2017-02-22 17:30:22 +03:00
|
|
|
/**
|
|
|
|
* Indicates whether or not the buffer can be shared with another process
|
|
|
|
* without reallocating. Note that this is racy and should only be used for
|
|
|
|
* informational/reporting purposes.
|
|
|
|
*/
|
|
|
|
bool CanShare() const {
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
return !mClosed;
|
|
|
|
}
|
|
|
|
|
2017-01-18 18:12:32 +03:00
|
|
|
/**
|
|
|
|
* Allocate a new shared memory buffer so that we can get a new handle for
|
|
|
|
* sharing to new processes. ShareToProcess must have failed with
|
|
|
|
* NS_ERROR_NOT_AVAILABLE in order for this to be safe to call. Returns true
|
|
|
|
* if the operation succeeds. If it fails, there is no state change.
|
|
|
|
*/
|
|
|
|
bool ReallocHandle();
|
|
|
|
|
|
|
|
/**
|
2017-10-30 20:10:28 +03:00
|
|
|
* Signals we have finished writing to the buffer and it may be marked as
|
2018-02-12 15:59:58 +03:00
|
|
|
* read only.
|
2017-01-18 18:12:32 +03:00
|
|
|
*/
|
|
|
|
void Finalize();
|
|
|
|
|
2017-10-30 20:10:28 +03:00
|
|
|
/**
|
|
|
|
* Indicates whether or not the buffer can change. If this returns true, it is
|
|
|
|
* guaranteed to continue to do so for the remainder of the surface's life.
|
|
|
|
*/
|
|
|
|
bool IsFinalized() const {
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
return mFinalized;
|
|
|
|
}
|
|
|
|
|
2017-12-01 14:59:21 +03:00
|
|
|
/**
|
2018-07-12 18:43:12 +03:00
|
|
|
* Yields a dirty rect of what has changed since it was last called.
|
2017-12-01 14:59:21 +03:00
|
|
|
*/
|
2018-07-12 18:43:12 +03:00
|
|
|
Maybe<IntRect> TakeDirtyRect() override {
|
2017-12-01 14:59:21 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
2018-07-12 18:43:12 +03:00
|
|
|
if (mDirtyRect) {
|
|
|
|
Maybe<IntRect> ret = std::move(mDirtyRect);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return Nothing();
|
2017-12-01 14:59:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Increment the invalidation counter.
|
|
|
|
*/
|
2018-07-12 18:43:12 +03:00
|
|
|
void Invalidate(const IntRect& aDirtyRect) override {
|
2017-12-01 14:59:21 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
2018-07-12 18:43:12 +03:00
|
|
|
if (!aDirtyRect.IsEmpty()) {
|
|
|
|
if (mDirtyRect) {
|
|
|
|
mDirtyRect->UnionRect(mDirtyRect.ref(), aDirtyRect);
|
|
|
|
} else {
|
|
|
|
mDirtyRect = Some(aDirtyRect);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mDirtyRect = Some(IntRect(IntPoint(0, 0), mSize));
|
|
|
|
}
|
|
|
|
MOZ_ASSERT_IF(mDirtyRect, !mDirtyRect->IsEmpty());
|
2017-12-01 14:59:21 +03:00
|
|
|
}
|
|
|
|
|
2017-10-30 20:10:28 +03:00
|
|
|
/**
|
|
|
|
* While a HandleLock exists for the given surface, the shared memory handle
|
|
|
|
* cannot be released.
|
|
|
|
*/
|
|
|
|
class MOZ_STACK_CLASS HandleLock final {
|
|
|
|
public:
|
|
|
|
explicit HandleLock(SourceSurfaceSharedData* aSurface)
|
|
|
|
: mSurface(aSurface) {
|
|
|
|
mSurface->LockHandle();
|
|
|
|
}
|
|
|
|
|
|
|
|
~HandleLock() { mSurface->UnlockHandle(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<SourceSurfaceSharedData> mSurface;
|
|
|
|
};
|
|
|
|
|
2017-01-18 18:12:32 +03:00
|
|
|
private:
|
2017-10-30 20:10:28 +03:00
|
|
|
friend class SourceSurfaceSharedDataWrapper;
|
|
|
|
|
2019-04-11 15:36:51 +03:00
|
|
|
virtual ~SourceSurfaceSharedData() = default;
|
2017-01-18 18:12:32 +03:00
|
|
|
|
2017-10-30 20:10:28 +03:00
|
|
|
void LockHandle() {
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
++mHandleCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnlockHandle() {
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
MOZ_ASSERT(mHandleCount > 0);
|
|
|
|
--mHandleCount;
|
|
|
|
mShared = true;
|
|
|
|
CloseHandleInternal();
|
|
|
|
}
|
|
|
|
|
2017-01-18 18:12:32 +03:00
|
|
|
uint8_t* GetDataInternal() const;
|
|
|
|
|
|
|
|
size_t GetDataLength() const {
|
|
|
|
return static_cast<size_t>(mStride) * mSize.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t GetAlignedDataLength() const {
|
|
|
|
return mozilla::ipc::SharedMemory::PageAlignedSize(GetDataLength());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempt to close the handle. Only if the buffer has been both finalized
|
|
|
|
* and we have completed sharing will it be released.
|
|
|
|
*/
|
|
|
|
void CloseHandleInternal();
|
|
|
|
|
2017-02-22 17:30:22 +03:00
|
|
|
mutable Mutex mMutex;
|
2017-01-18 18:12:32 +03:00
|
|
|
int32_t mStride;
|
2017-10-30 20:10:28 +03:00
|
|
|
int32_t mHandleCount;
|
2018-07-12 18:43:12 +03:00
|
|
|
Maybe<IntRect> mDirtyRect;
|
2017-01-18 18:12:32 +03:00
|
|
|
IntSize mSize;
|
|
|
|
RefPtr<SharedMemoryBasic> mBuf;
|
|
|
|
RefPtr<SharedMemoryBasic> mOldBuf;
|
|
|
|
SurfaceFormat mFormat;
|
|
|
|
bool mClosed : 1;
|
|
|
|
bool mFinalized : 1;
|
|
|
|
bool mShared : 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif /* MOZILLA_GFX_SOURCESURFACESHAREDDATA_H_ */
|