2013-02-14 03:26:24 +04: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/. */
|
|
|
|
|
|
|
|
#ifndef SHARED_SURFACE_GL_H_
|
|
|
|
#define SHARED_SURFACE_GL_H_
|
|
|
|
|
2014-03-06 01:49:37 +04:00
|
|
|
#include "ScopedGLHelpers.h"
|
2013-02-14 03:26:24 +04:00
|
|
|
#include "SharedSurface.h"
|
|
|
|
#include "SurfaceFactory.h"
|
|
|
|
#include "SurfaceTypes.h"
|
|
|
|
#include "GLContextTypes.h"
|
|
|
|
#include "nsAutoPtr.h"
|
2013-09-25 00:45:14 +04:00
|
|
|
#include "gfxTypes.h"
|
2013-06-05 04:05:28 +04:00
|
|
|
#include "mozilla/Mutex.h"
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
#include <queue>
|
|
|
|
|
|
|
|
// Forwards:
|
|
|
|
class gfxImageSurface;
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gl {
|
|
|
|
class GLContext;
|
|
|
|
}
|
2013-12-10 20:11:58 +04:00
|
|
|
namespace gfx {
|
|
|
|
class DataSourceSurface;
|
|
|
|
}
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gl {
|
|
|
|
|
|
|
|
class SurfaceFactory_GL;
|
|
|
|
|
|
|
|
class SharedSurface_GL
|
|
|
|
: public gfx::SharedSurface
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
typedef class gfx::SharedSurface SharedSurface;
|
|
|
|
typedef gfx::SharedSurfaceType SharedSurfaceType;
|
|
|
|
typedef gfx::APITypeT APITypeT;
|
|
|
|
typedef gfx::AttachmentType AttachmentType;
|
|
|
|
|
|
|
|
GLContext* const mGL;
|
|
|
|
|
|
|
|
SharedSurface_GL(SharedSurfaceType type,
|
|
|
|
AttachmentType attachType,
|
|
|
|
GLContext* gl,
|
2013-12-10 20:11:58 +04:00
|
|
|
const gfx::IntSize& size,
|
2013-02-14 03:26:24 +04:00
|
|
|
bool hasAlpha)
|
|
|
|
: SharedSurface(type, APITypeT::OpenGL, attachType, size, hasAlpha)
|
|
|
|
, mGL(gl)
|
|
|
|
{}
|
|
|
|
|
|
|
|
public:
|
2014-03-11 06:08:49 +04:00
|
|
|
static void ProdCopy(SharedSurface_GL* src, SharedSurface_GL* dest,
|
|
|
|
SurfaceFactory_GL* factory);
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
static SharedSurface_GL* Cast(SharedSurface* surf) {
|
|
|
|
MOZ_ASSERT(surf->APIType() == APITypeT::OpenGL);
|
|
|
|
|
|
|
|
return (SharedSurface_GL*)surf;
|
|
|
|
}
|
|
|
|
|
2014-03-11 06:08:49 +04:00
|
|
|
// For use when AttachType is correct.
|
|
|
|
virtual GLuint ProdTexture() {
|
|
|
|
MOZ_ASSERT(AttachType() == AttachmentType::GLTexture);
|
|
|
|
MOZ_CRASH("Did you forget to override this function?");
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual GLenum ProdTextureTarget() const {
|
|
|
|
return LOCAL_GL_TEXTURE_2D;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual GLuint ProdRenderbuffer() {
|
|
|
|
MOZ_ASSERT(AttachType() == AttachmentType::GLRenderbuffer);
|
|
|
|
MOZ_CRASH("Did you forget to override this function?");
|
|
|
|
}
|
|
|
|
|
2013-07-18 07:24:15 +04:00
|
|
|
virtual bool ReadPixels(GLint x, GLint y, GLsizei width, GLsizei height,
|
|
|
|
GLenum format, GLenum type, GLvoid *pixels) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-11 06:08:49 +04:00
|
|
|
virtual void LockProd() MOZ_OVERRIDE;
|
|
|
|
virtual void UnlockProd() MOZ_OVERRIDE;
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
GLContext* GL() const {
|
|
|
|
return mGL;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class SurfaceFactory_GL
|
|
|
|
: public gfx::SurfaceFactory
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
typedef struct gfx::SurfaceCaps SurfaceCaps;
|
|
|
|
typedef class gfx::SurfaceFactory SurfaceFactory;
|
|
|
|
typedef class gfx::SharedSurface SharedSurface;
|
|
|
|
typedef gfx::SharedSurfaceType SharedSurfaceType;
|
|
|
|
|
|
|
|
GLContext* const mGL;
|
|
|
|
const GLFormats mFormats;
|
|
|
|
|
|
|
|
SurfaceCaps mDrawCaps;
|
|
|
|
SurfaceCaps mReadCaps;
|
|
|
|
|
|
|
|
// This uses ChooseBufferBits to pick drawBits/readBits.
|
|
|
|
SurfaceFactory_GL(GLContext* gl,
|
|
|
|
SharedSurfaceType type,
|
|
|
|
const SurfaceCaps& caps);
|
|
|
|
|
|
|
|
virtual void ChooseBufferBits(const SurfaceCaps& caps,
|
|
|
|
SurfaceCaps& drawCaps,
|
|
|
|
SurfaceCaps& readCaps) const;
|
|
|
|
|
|
|
|
public:
|
|
|
|
GLContext* GL() const {
|
|
|
|
return mGL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const GLFormats& Formats() const {
|
|
|
|
return mFormats;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SurfaceCaps& DrawCaps() const {
|
|
|
|
return mDrawCaps;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SurfaceCaps& ReadCaps() const {
|
|
|
|
return mReadCaps;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// For readback and bootstrapping:
|
|
|
|
class SharedSurface_Basic
|
|
|
|
: public SharedSurface_GL
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static SharedSurface_Basic* Create(GLContext* gl,
|
|
|
|
const GLFormats& formats,
|
2013-12-10 20:11:58 +04:00
|
|
|
const gfx::IntSize& size,
|
2013-02-14 03:26:24 +04:00
|
|
|
bool hasAlpha);
|
|
|
|
|
|
|
|
static SharedSurface_Basic* Cast(SharedSurface* surf) {
|
|
|
|
MOZ_ASSERT(surf->Type() == SharedSurfaceType::Basic);
|
|
|
|
|
|
|
|
return (SharedSurface_Basic*)surf;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const GLuint mTex;
|
2014-03-06 01:49:37 +04:00
|
|
|
GLuint mFB;
|
|
|
|
|
2013-12-10 20:11:58 +04:00
|
|
|
RefPtr<gfx::DataSourceSurface> mData;
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
SharedSurface_Basic(GLContext* gl,
|
2013-12-10 20:11:58 +04:00
|
|
|
const gfx::IntSize& size,
|
2013-02-14 03:26:24 +04:00
|
|
|
bool hasAlpha,
|
2013-12-10 20:11:58 +04:00
|
|
|
gfx::SurfaceFormat format,
|
2013-02-14 03:26:24 +04:00
|
|
|
GLuint tex);
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual ~SharedSurface_Basic();
|
|
|
|
|
2014-03-11 06:08:49 +04:00
|
|
|
virtual void LockProdImpl() MOZ_OVERRIDE {}
|
|
|
|
virtual void UnlockProdImpl() MOZ_OVERRIDE {}
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
|
2014-03-11 06:08:49 +04:00
|
|
|
virtual void Fence() MOZ_OVERRIDE;
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2014-03-11 06:08:49 +04:00
|
|
|
virtual bool WaitSync() MOZ_OVERRIDE {
|
2013-02-14 03:26:24 +04:00
|
|
|
// Since we already store the data in Fence, we're always done already.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-11 06:08:49 +04:00
|
|
|
virtual GLuint ProdTexture() MOZ_OVERRIDE {
|
2013-02-14 03:26:24 +04:00
|
|
|
return mTex;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implementation-specific functions below:
|
2013-12-10 20:11:58 +04:00
|
|
|
gfx::DataSourceSurface* GetData() {
|
2013-02-14 03:26:24 +04:00
|
|
|
return mData;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class SurfaceFactory_Basic
|
|
|
|
: public SurfaceFactory_GL
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SurfaceFactory_Basic(GLContext* gl, const SurfaceCaps& caps)
|
|
|
|
: SurfaceFactory_GL(gl, SharedSurfaceType::Basic, caps)
|
|
|
|
{}
|
|
|
|
|
2014-03-11 06:08:49 +04:00
|
|
|
virtual SharedSurface* CreateShared(const gfx::IntSize& size) MOZ_OVERRIDE {
|
2013-02-14 03:26:24 +04:00
|
|
|
bool hasAlpha = mReadCaps.alpha;
|
|
|
|
return SharedSurface_Basic::Create(mGL, mFormats, size, hasAlpha);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Using shared GL textures:
|
|
|
|
class SharedSurface_GLTexture
|
|
|
|
: public SharedSurface_GL
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static SharedSurface_GLTexture* Create(GLContext* prodGL,
|
|
|
|
GLContext* consGL,
|
|
|
|
const GLFormats& formats,
|
2013-12-10 20:11:58 +04:00
|
|
|
const gfx::IntSize& size,
|
2014-03-06 01:49:37 +04:00
|
|
|
bool hasAlpha,
|
|
|
|
GLuint texture = 0);
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
static SharedSurface_GLTexture* Cast(SharedSurface* surf) {
|
|
|
|
MOZ_ASSERT(surf->Type() == SharedSurfaceType::GLTextureShare);
|
|
|
|
|
|
|
|
return (SharedSurface_GLTexture*)surf;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2013-06-05 04:05:28 +04:00
|
|
|
GLContext* mConsGL;
|
2013-02-14 03:26:24 +04:00
|
|
|
const GLuint mTex;
|
2014-03-06 01:49:37 +04:00
|
|
|
const bool mOwnsTex;
|
2013-02-14 03:26:24 +04:00
|
|
|
GLsync mSync;
|
2013-06-05 04:05:28 +04:00
|
|
|
mutable Mutex mMutex;
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
SharedSurface_GLTexture(GLContext* prodGL,
|
|
|
|
GLContext* consGL,
|
2013-12-10 20:11:58 +04:00
|
|
|
const gfx::IntSize& size,
|
2013-02-14 03:26:24 +04:00
|
|
|
bool hasAlpha,
|
2014-03-06 01:49:37 +04:00
|
|
|
GLuint tex,
|
|
|
|
bool ownsTex)
|
2013-02-14 03:26:24 +04:00
|
|
|
: SharedSurface_GL(SharedSurfaceType::GLTextureShare,
|
|
|
|
AttachmentType::GLTexture,
|
|
|
|
prodGL,
|
|
|
|
size,
|
|
|
|
hasAlpha)
|
|
|
|
, mConsGL(consGL)
|
|
|
|
, mTex(tex)
|
2014-03-06 01:49:37 +04:00
|
|
|
, mOwnsTex(ownsTex)
|
2013-02-14 03:26:24 +04:00
|
|
|
, mSync(0)
|
2013-06-05 04:05:28 +04:00
|
|
|
, mMutex("SharedSurface_GLTexture mutex")
|
2013-02-14 03:26:24 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual ~SharedSurface_GLTexture();
|
|
|
|
|
2014-03-11 06:08:49 +04:00
|
|
|
virtual void LockProdImpl() MOZ_OVERRIDE {}
|
|
|
|
virtual void UnlockProdImpl() MOZ_OVERRIDE {}
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
|
2014-03-11 06:08:49 +04:00
|
|
|
virtual void Fence() MOZ_OVERRIDE;
|
|
|
|
virtual bool WaitSync() MOZ_OVERRIDE;
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
|
2014-03-11 06:08:49 +04:00
|
|
|
virtual GLuint ProdTexture() MOZ_OVERRIDE {
|
2013-02-14 03:26:24 +04:00
|
|
|
return mTex;
|
|
|
|
}
|
2013-06-05 04:05:28 +04:00
|
|
|
|
|
|
|
// Custom:
|
2014-03-11 06:08:50 +04:00
|
|
|
|
|
|
|
GLuint ConsTexture(GLContext* consGL);
|
|
|
|
|
|
|
|
GLenum ConsTextureTarget() const {
|
|
|
|
return ProdTextureTarget();
|
|
|
|
}
|
2013-02-14 03:26:24 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class SurfaceFactory_GLTexture
|
|
|
|
: public SurfaceFactory_GL
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
GLContext* const mConsGL;
|
|
|
|
|
|
|
|
public:
|
2013-06-05 04:05:28 +04:00
|
|
|
// If we don't know `consGL` at construction time, use `nullptr`, and call
|
|
|
|
// `SetConsumerGL()` on each `SharedSurface_GLTexture` before calling its
|
|
|
|
// `WaitSync()`.
|
2013-02-14 03:26:24 +04:00
|
|
|
SurfaceFactory_GLTexture(GLContext* prodGL,
|
|
|
|
GLContext* consGL,
|
|
|
|
const SurfaceCaps& caps)
|
|
|
|
: SurfaceFactory_GL(prodGL, SharedSurfaceType::GLTextureShare, caps)
|
|
|
|
, mConsGL(consGL)
|
2013-06-05 04:05:28 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(consGL != prodGL);
|
|
|
|
}
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2014-03-11 06:08:49 +04:00
|
|
|
virtual SharedSurface* CreateShared(const gfx::IntSize& size) MOZ_OVERRIDE {
|
2013-02-14 03:26:24 +04:00
|
|
|
bool hasAlpha = mReadCaps.alpha;
|
|
|
|
return SharedSurface_GLTexture::Create(mGL, mConsGL, mFormats, size, hasAlpha);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} /* namespace gfx */
|
|
|
|
} /* namespace mozilla */
|
|
|
|
|
|
|
|
#endif /* SHARED_SURFACE_GL_H_ */
|