2013-02-13 15:26:24 -08:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40; -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* 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/. */
|
|
|
|
|
|
|
|
/* SharedSurface abstracts an actual surface (can be a GL texture, but
|
|
|
|
* not necessarily) that handles sharing.
|
|
|
|
* Its specializations are:
|
|
|
|
* SharedSurface_Basic (client-side bitmap, does readback)
|
|
|
|
* SharedSurface_GLTexture
|
|
|
|
* SharedSurface_EGLImage
|
|
|
|
* SharedSurface_ANGLEShareHandle
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SHARED_SURFACE_H_
|
|
|
|
#define SHARED_SURFACE_H_
|
|
|
|
|
2014-07-11 15:10:49 -07:00
|
|
|
#include <queue>
|
2013-07-30 07:25:31 -07:00
|
|
|
#include <stdint.h>
|
2014-07-11 15:10:49 -07:00
|
|
|
|
|
|
|
#include "GLContextTypes.h"
|
2013-02-13 15:26:24 -08:00
|
|
|
#include "GLDefs.h"
|
2014-07-11 15:10:49 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-09-02 15:15:41 -07:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2013-12-10 08:11:58 -08:00
|
|
|
#include "mozilla/gfx/Point.h"
|
2014-08-15 17:38:08 -07:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2014-08-15 17:38:08 -07:00
|
|
|
#include "mozilla/WeakPtr.h"
|
2014-10-07 21:01:51 -07:00
|
|
|
#include "ScopedGLHelpers.h"
|
2013-02-13 15:26:24 -08:00
|
|
|
#include "SurfaceTypes.h"
|
|
|
|
|
2014-09-02 15:15:41 -07:00
|
|
|
class nsIThread;
|
|
|
|
|
2013-02-13 15:26:24 -08:00
|
|
|
namespace mozilla {
|
2014-10-10 14:36:17 -07:00
|
|
|
namespace gfx {
|
|
|
|
class DrawTarget;
|
|
|
|
}
|
2014-07-11 15:10:49 -07:00
|
|
|
namespace gl {
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2014-07-11 15:10:49 -07:00
|
|
|
class GLContext;
|
2013-02-13 15:26:24 -08:00
|
|
|
class SurfaceFactory;
|
2014-10-07 20:59:37 -07:00
|
|
|
class ShSurfHandle;
|
2013-02-13 15:26:24 -08:00
|
|
|
|
|
|
|
class SharedSurface
|
|
|
|
{
|
2014-07-11 15:10:49 -07:00
|
|
|
public:
|
|
|
|
static void ProdCopy(SharedSurface* src, SharedSurface* dest,
|
|
|
|
SurfaceFactory* factory);
|
|
|
|
|
2013-02-13 15:26:24 -08:00
|
|
|
const SharedSurfaceType mType;
|
|
|
|
const AttachmentType mAttachType;
|
2014-07-11 15:10:49 -07:00
|
|
|
GLContext* const mGL;
|
2013-12-10 08:11:58 -08:00
|
|
|
const gfx::IntSize mSize;
|
2013-02-13 15:26:24 -08:00
|
|
|
const bool mHasAlpha;
|
2014-07-11 15:10:49 -07:00
|
|
|
protected:
|
2013-02-13 15:26:24 -08:00
|
|
|
bool mIsLocked;
|
2014-10-09 13:33:22 -07:00
|
|
|
bool mIsProducerAcquired;
|
|
|
|
bool mIsConsumerAcquired;
|
2014-09-02 15:15:41 -07:00
|
|
|
DebugOnly<nsIThread* const> mOwningThread;
|
2013-02-13 15:26:24 -08:00
|
|
|
|
|
|
|
SharedSurface(SharedSurfaceType type,
|
|
|
|
AttachmentType attachType,
|
2014-07-11 15:10:49 -07:00
|
|
|
GLContext* gl,
|
2013-12-10 08:11:58 -08:00
|
|
|
const gfx::IntSize& size,
|
2014-09-02 15:15:41 -07:00
|
|
|
bool hasAlpha);
|
2013-02-13 15:26:24 -08:00
|
|
|
|
|
|
|
public:
|
|
|
|
virtual ~SharedSurface() {
|
|
|
|
}
|
|
|
|
|
2014-07-11 15:10:49 -07:00
|
|
|
bool IsLocked() const {
|
|
|
|
return mIsLocked;
|
|
|
|
}
|
2013-02-13 15:26:24 -08:00
|
|
|
|
|
|
|
// This locks the SharedSurface as the production buffer for the context.
|
|
|
|
// This is needed by backends which use PBuffers and/or EGLSurfaces.
|
2014-07-11 15:10:49 -07:00
|
|
|
void LockProd();
|
2013-02-13 15:26:24 -08:00
|
|
|
|
|
|
|
// Unlocking is harmless if we're already unlocked.
|
2014-07-11 15:10:49 -07:00
|
|
|
void UnlockProd();
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2014-07-11 15:10:49 -07:00
|
|
|
protected:
|
2013-02-13 15:26:24 -08:00
|
|
|
virtual void LockProdImpl() = 0;
|
|
|
|
virtual void UnlockProdImpl() = 0;
|
|
|
|
|
2014-10-09 13:33:22 -07:00
|
|
|
virtual void ProducerAcquireImpl() {}
|
|
|
|
virtual void ProducerReleaseImpl() {
|
|
|
|
Fence();
|
|
|
|
}
|
|
|
|
virtual void ConsumerAcquireImpl() {
|
|
|
|
WaitSync();
|
|
|
|
}
|
|
|
|
virtual void ConsumerReleaseImpl() {}
|
|
|
|
|
2014-07-11 15:10:49 -07:00
|
|
|
public:
|
2014-10-09 13:33:22 -07:00
|
|
|
void ProducerAcquire() {
|
|
|
|
MOZ_ASSERT(!mIsProducerAcquired);
|
|
|
|
ProducerAcquireImpl();
|
|
|
|
mIsProducerAcquired = true;
|
|
|
|
}
|
|
|
|
void ProducerRelease() {
|
|
|
|
MOZ_ASSERT(mIsProducerAcquired);
|
|
|
|
ProducerReleaseImpl();
|
|
|
|
mIsProducerAcquired = false;
|
|
|
|
}
|
|
|
|
void ConsumerAcquire() {
|
|
|
|
MOZ_ASSERT(!mIsConsumerAcquired);
|
|
|
|
ConsumerAcquireImpl();
|
|
|
|
mIsConsumerAcquired = true;
|
|
|
|
}
|
|
|
|
void ConsumerRelease() {
|
|
|
|
MOZ_ASSERT(mIsConsumerAcquired);
|
|
|
|
ConsumerReleaseImpl();
|
|
|
|
mIsConsumerAcquired = false;
|
|
|
|
}
|
|
|
|
|
2013-02-13 15:26:24 -08:00
|
|
|
virtual void Fence() = 0;
|
|
|
|
virtual bool WaitSync() = 0;
|
2014-08-04 22:10:47 -07:00
|
|
|
virtual bool PollSync() = 0;
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2014-09-02 15:15:41 -07:00
|
|
|
// Use these if you can. They can only be called from the Content
|
|
|
|
// thread, though!
|
|
|
|
void Fence_ContentThread();
|
|
|
|
bool WaitSync_ContentThread();
|
|
|
|
bool PollSync_ContentThread();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void Fence_ContentThread_Impl() {
|
|
|
|
Fence();
|
|
|
|
}
|
|
|
|
virtual bool WaitSync_ContentThread_Impl() {
|
|
|
|
return WaitSync();
|
|
|
|
}
|
|
|
|
virtual bool PollSync_ContentThread_Impl() {
|
|
|
|
return PollSync();
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2014-05-14 15:40:15 -07:00
|
|
|
// This function waits until the buffer is no longer being used.
|
|
|
|
// To optimize the performance, some implementaions recycle SharedSurfaces
|
|
|
|
// even when its buffer is still being used.
|
|
|
|
virtual void WaitForBufferOwnership() {}
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2014-07-11 15:10:49 -07:00
|
|
|
// For use when AttachType is correct.
|
|
|
|
virtual GLenum ProdTextureTarget() const {
|
|
|
|
MOZ_ASSERT(mAttachType == AttachmentType::GLTexture);
|
|
|
|
return LOCAL_GL_TEXTURE_2D;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual GLuint ProdTexture() {
|
|
|
|
MOZ_ASSERT(mAttachType == AttachmentType::GLTexture);
|
|
|
|
MOZ_CRASH("Did you forget to override this function?");
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
|
2014-07-11 15:10:49 -07:00
|
|
|
virtual GLuint ProdRenderbuffer() {
|
|
|
|
MOZ_ASSERT(mAttachType == AttachmentType::GLRenderbuffer);
|
|
|
|
MOZ_CRASH("Did you forget to override this function?");
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
|
2014-07-11 15:10:49 -07:00
|
|
|
virtual bool ReadPixels(GLint x, GLint y,
|
|
|
|
GLsizei width, GLsizei height,
|
|
|
|
GLenum format, GLenum type,
|
|
|
|
GLvoid* pixels)
|
|
|
|
{
|
|
|
|
return false;
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
2014-10-07 21:01:51 -07:00
|
|
|
|
|
|
|
virtual bool NeedsIndirectReads() const {
|
|
|
|
return false;
|
|
|
|
}
|
2014-07-11 15:10:49 -07:00
|
|
|
};
|
|
|
|
|
2014-08-15 17:38:08 -07:00
|
|
|
template<typename T>
|
|
|
|
class UniquePtrQueue
|
|
|
|
{
|
|
|
|
std::queue<T*> mQueue;
|
|
|
|
|
|
|
|
public:
|
|
|
|
~UniquePtrQueue() {
|
|
|
|
MOZ_ASSERT(Empty());
|
|
|
|
}
|
2014-08-15 17:38:09 -07:00
|
|
|
|
2014-08-15 17:38:08 -07:00
|
|
|
bool Empty() const {
|
|
|
|
return mQueue.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Push(UniquePtr<T> up) {
|
|
|
|
T* p = up.release();
|
|
|
|
mQueue.push(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
UniquePtr<T> Pop() {
|
|
|
|
UniquePtr<T> ret;
|
|
|
|
|
2014-08-15 17:38:09 -07:00
|
|
|
if (!mQueue.empty()) {
|
|
|
|
ret.reset(mQueue.front());
|
|
|
|
mQueue.pop();
|
|
|
|
}
|
2014-08-15 17:38:08 -07:00
|
|
|
|
|
|
|
return Move(ret);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-10-07 20:59:37 -07:00
|
|
|
class SurfaceFactory : public SupportsWeakPtr<SurfaceFactory>
|
2014-07-11 15:10:49 -07:00
|
|
|
{
|
|
|
|
public:
|
2014-10-07 20:59:37 -07:00
|
|
|
// Should use the VIRTUAL version, but it's currently incompatible
|
|
|
|
// with SupportsWeakPtr. (bug 1049278)
|
|
|
|
MOZ_DECLARE_REFCOUNTED_TYPENAME(SurfaceFactory)
|
|
|
|
|
2014-07-11 15:10:49 -07:00
|
|
|
GLContext* const mGL;
|
|
|
|
const SurfaceCaps mCaps;
|
|
|
|
const SharedSurfaceType mType;
|
|
|
|
const GLFormats mFormats;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SurfaceCaps mDrawCaps;
|
|
|
|
SurfaceCaps mReadCaps;
|
|
|
|
|
|
|
|
SurfaceFactory(GLContext* gl,
|
|
|
|
SharedSurfaceType type,
|
|
|
|
const SurfaceCaps& caps);
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2014-07-11 15:10:49 -07:00
|
|
|
public:
|
|
|
|
virtual ~SurfaceFactory();
|
|
|
|
|
|
|
|
const SurfaceCaps& DrawCaps() const {
|
|
|
|
return mDrawCaps;
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
|
|
|
|
2014-07-11 15:10:49 -07:00
|
|
|
const SurfaceCaps& ReadCaps() const {
|
|
|
|
return mReadCaps;
|
2013-02-13 15:26:24 -08:00
|
|
|
}
|
2014-07-11 15:10:49 -07:00
|
|
|
|
|
|
|
protected:
|
2014-08-15 17:38:08 -07:00
|
|
|
virtual UniquePtr<SharedSurface> CreateShared(const gfx::IntSize& size) = 0;
|
2014-07-11 15:10:49 -07:00
|
|
|
|
2014-08-15 17:38:08 -07:00
|
|
|
UniquePtrQueue<SharedSurface> mScraps;
|
2014-07-11 15:10:49 -07:00
|
|
|
|
|
|
|
public:
|
2014-08-15 17:38:08 -07:00
|
|
|
UniquePtr<SharedSurface> NewSharedSurface(const gfx::IntSize& size);
|
2014-10-07 20:59:37 -07:00
|
|
|
TemporaryRef<ShSurfHandle> NewShSurfHandle(const gfx::IntSize& size);
|
2014-07-11 15:10:49 -07:00
|
|
|
|
|
|
|
// Auto-deletes surfs of the wrong type.
|
2014-08-15 17:38:08 -07:00
|
|
|
void Recycle(UniquePtr<SharedSurface> surf);
|
2013-02-13 15:26:24 -08:00
|
|
|
};
|
|
|
|
|
2014-10-07 20:59:37 -07:00
|
|
|
class ShSurfHandle : public RefCounted<ShSurfHandle>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MOZ_DECLARE_REFCOUNTED_TYPENAME(ShSurfHandle)
|
|
|
|
|
|
|
|
private:
|
|
|
|
const WeakPtr<SurfaceFactory> mFactory;
|
|
|
|
UniquePtr<SharedSurface> mSurf;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ShSurfHandle(SurfaceFactory* factory, UniquePtr<SharedSurface> surf)
|
|
|
|
: mFactory(factory)
|
|
|
|
, mSurf(Move(surf))
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mFactory);
|
|
|
|
MOZ_ASSERT(mSurf);
|
|
|
|
}
|
|
|
|
|
|
|
|
~ShSurfHandle() {
|
|
|
|
if (mFactory) {
|
|
|
|
mFactory->Recycle(Move(mSurf));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedSurface* Surf() const {
|
2014-10-07 21:15:39 -07:00
|
|
|
MOZ_ASSERT(mSurf.get());
|
2014-10-07 20:59:37 -07:00
|
|
|
return mSurf.get();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-10-07 21:01:51 -07:00
|
|
|
class ScopedReadbackFB
|
|
|
|
{
|
|
|
|
GLContext* const mGL;
|
|
|
|
ScopedBindFramebuffer mAutoFB;
|
|
|
|
GLuint mTempFB;
|
|
|
|
GLuint mTempTex;
|
|
|
|
SharedSurface* mSurfToUnlock;
|
|
|
|
SharedSurface* mSurfToLock;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ScopedReadbackFB(SharedSurface* src);
|
|
|
|
~ScopedReadbackFB();
|
|
|
|
};
|
|
|
|
|
2014-10-10 14:36:17 -07:00
|
|
|
bool ReadbackSharedSurface(SharedSurface* src, gfx::DrawTarget* dst);
|
|
|
|
|
2014-07-11 15:10:49 -07:00
|
|
|
} // namespace gl
|
|
|
|
} // namespace mozilla
|
2013-02-13 15:26:24 -08:00
|
|
|
|
2014-07-11 15:10:49 -07:00
|
|
|
#endif // SHARED_SURFACE_H_
|