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/. */
|
|
|
|
|
|
|
|
/* 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-12 02:10:49 +04:00
|
|
|
#include <queue>
|
2015-06-05 03:15:38 +03:00
|
|
|
#include <set>
|
2013-07-30 18:25:31 +04:00
|
|
|
#include <stdint.h>
|
2014-07-12 02:10:49 +04:00
|
|
|
|
|
|
|
#include "GLContextTypes.h"
|
2013-02-14 03:26:24 +04:00
|
|
|
#include "GLDefs.h"
|
2014-07-12 02:10:49 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-09-03 02:15:41 +04:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2013-12-10 20:11:58 +04:00
|
|
|
#include "mozilla/gfx/Point.h"
|
2015-10-12 06:21:02 +03:00
|
|
|
#include "mozilla/Mutex.h"
|
2014-08-16 04:38:08 +04:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2014-08-16 04:38:08 +04:00
|
|
|
#include "mozilla/WeakPtr.h"
|
2014-10-08 08:01:51 +04:00
|
|
|
#include "ScopedGLHelpers.h"
|
2013-02-14 03:26:24 +04:00
|
|
|
#include "SurfaceTypes.h"
|
|
|
|
|
2014-09-03 02:15:41 +04:00
|
|
|
class nsIThread;
|
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
namespace mozilla {
|
2014-10-11 01:36:17 +04:00
|
|
|
namespace gfx {
|
2015-10-12 06:21:03 +03:00
|
|
|
class DataSourceSurface;
|
2014-10-11 01:36:17 +04:00
|
|
|
class DrawTarget;
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace gfx
|
2015-06-05 03:15:38 +03:00
|
|
|
|
|
|
|
namespace layers {
|
2016-03-21 17:18:20 +03:00
|
|
|
class ClientIPCAllocator;
|
2015-06-05 03:15:38 +03:00
|
|
|
class SharedSurfaceTextureClient;
|
|
|
|
enum class TextureFlags : uint32_t;
|
|
|
|
class SurfaceDescriptor;
|
|
|
|
class TextureClient;
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace layers
|
2015-06-05 03:15:38 +03:00
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
namespace gl {
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
class GLContext;
|
2013-02-14 03:26:24 +04:00
|
|
|
class SurfaceFactory;
|
2014-10-08 07:59:37 +04:00
|
|
|
class ShSurfHandle;
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
class SharedSurface
|
|
|
|
{
|
2014-07-12 02:10:49 +04:00
|
|
|
public:
|
|
|
|
static void ProdCopy(SharedSurface* src, SharedSurface* dest,
|
|
|
|
SurfaceFactory* factory);
|
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
const SharedSurfaceType mType;
|
|
|
|
const AttachmentType mAttachType;
|
2014-07-12 02:10:49 +04:00
|
|
|
GLContext* const mGL;
|
2013-12-10 20:11:58 +04:00
|
|
|
const gfx::IntSize mSize;
|
2013-02-14 03:26:24 +04:00
|
|
|
const bool mHasAlpha;
|
2015-06-05 03:15:38 +03:00
|
|
|
const bool mCanRecycle;
|
2014-07-12 02:10:49 +04:00
|
|
|
protected:
|
2013-02-14 03:26:24 +04:00
|
|
|
bool mIsLocked;
|
2014-10-10 00:33:22 +04:00
|
|
|
bool mIsProducerAcquired;
|
|
|
|
bool mIsConsumerAcquired;
|
2016-02-26 18:52:07 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
nsIThread* const mOwningThread;
|
|
|
|
#endif
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
SharedSurface(SharedSurfaceType type,
|
|
|
|
AttachmentType attachType,
|
2014-07-12 02:10:49 +04:00
|
|
|
GLContext* gl,
|
2013-12-10 20:11:58 +04:00
|
|
|
const gfx::IntSize& size,
|
2015-06-05 03:15:38 +03:00
|
|
|
bool hasAlpha,
|
|
|
|
bool canRecycle);
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
virtual ~SharedSurface() {
|
|
|
|
}
|
|
|
|
|
2015-10-19 19:26:26 +03:00
|
|
|
// Specifies to the TextureClient any flags which
|
|
|
|
// are required by the SharedSurface backend.
|
|
|
|
virtual layers::TextureFlags GetTextureFlags() const;
|
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
bool IsLocked() const {
|
|
|
|
return mIsLocked;
|
|
|
|
}
|
2013-02-14 03:26:24 +04: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-12 02:10:49 +04:00
|
|
|
void LockProd();
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
// Unlocking is harmless if we're already unlocked.
|
2014-07-12 02:10:49 +04:00
|
|
|
void UnlockProd();
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
protected:
|
2013-02-14 03:26:24 +04:00
|
|
|
virtual void LockProdImpl() = 0;
|
|
|
|
virtual void UnlockProdImpl() = 0;
|
|
|
|
|
2016-01-06 02:57:44 +03:00
|
|
|
virtual void ProducerAcquireImpl() = 0;
|
|
|
|
virtual void ProducerReleaseImpl() = 0;
|
|
|
|
virtual void ProducerReadAcquireImpl() { ProducerAcquireImpl(); }
|
|
|
|
virtual void ProducerReadReleaseImpl() { ProducerReleaseImpl(); }
|
2014-10-10 00:33:22 +04:00
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
public:
|
2014-10-10 00:33:22 +04:00
|
|
|
void ProducerAcquire() {
|
|
|
|
MOZ_ASSERT(!mIsProducerAcquired);
|
|
|
|
ProducerAcquireImpl();
|
|
|
|
mIsProducerAcquired = true;
|
|
|
|
}
|
|
|
|
void ProducerRelease() {
|
|
|
|
MOZ_ASSERT(mIsProducerAcquired);
|
|
|
|
ProducerReleaseImpl();
|
|
|
|
mIsProducerAcquired = false;
|
|
|
|
}
|
2015-06-05 03:15:38 +03:00
|
|
|
void ProducerReadAcquire() {
|
|
|
|
MOZ_ASSERT(!mIsProducerAcquired);
|
|
|
|
ProducerReadAcquireImpl();
|
|
|
|
mIsProducerAcquired = true;
|
|
|
|
}
|
|
|
|
void ProducerReadRelease() {
|
|
|
|
MOZ_ASSERT(mIsProducerAcquired);
|
|
|
|
ProducerReadReleaseImpl();
|
|
|
|
mIsProducerAcquired = false;
|
|
|
|
}
|
2014-09-03 02:15:41 +04:00
|
|
|
|
2014-05-15 02:40:15 +04: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-14 03:26:24 +04:00
|
|
|
|
2014-07-12 02:10:49 +04: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-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
virtual GLuint ProdRenderbuffer() {
|
|
|
|
MOZ_ASSERT(mAttachType == AttachmentType::GLRenderbuffer);
|
|
|
|
MOZ_CRASH("Did you forget to override this function?");
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
2015-03-23 06:55:26 +03:00
|
|
|
virtual bool CopyTexImage2D(GLenum target, GLint level, GLenum internalformat, GLint x,
|
|
|
|
GLint y, GLsizei width, GLsizei height, GLint border)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
virtual bool ReadPixels(GLint x, GLint y,
|
|
|
|
GLsizei width, GLsizei height,
|
|
|
|
GLenum format, GLenum type,
|
|
|
|
GLvoid* pixels)
|
|
|
|
{
|
|
|
|
return false;
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
2014-10-08 08:01:51 +04:00
|
|
|
|
|
|
|
virtual bool NeedsIndirectReads() const {
|
|
|
|
return false;
|
|
|
|
}
|
2015-06-05 03:15:38 +03:00
|
|
|
|
|
|
|
virtual bool ToSurfaceDescriptor(layers::SurfaceDescriptor* const out_descriptor) = 0;
|
2015-10-12 06:21:03 +03:00
|
|
|
|
|
|
|
virtual bool ReadbackBySharedHandle(gfx::DataSourceSurface* out_surface) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-06-05 03:15:38 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class RefSet
|
|
|
|
{
|
|
|
|
std::set<T*> mSet;
|
|
|
|
|
|
|
|
public:
|
|
|
|
~RefSet() {
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto begin() -> decltype(mSet.begin()) {
|
|
|
|
return mSet.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear() {
|
|
|
|
for (auto itr = mSet.begin(); itr != mSet.end(); ++itr) {
|
|
|
|
(*itr)->Release();
|
|
|
|
}
|
|
|
|
mSet.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool empty() const {
|
|
|
|
return mSet.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool insert(T* x) {
|
|
|
|
if (mSet.insert(x).second) {
|
|
|
|
x->AddRef();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool erase(T* x) {
|
|
|
|
if (mSet.erase(x)) {
|
|
|
|
x->Release();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2014-07-12 02:10:49 +04:00
|
|
|
};
|
|
|
|
|
2014-08-16 04:38:08 +04:00
|
|
|
template<typename T>
|
2015-06-05 03:15:38 +03:00
|
|
|
class RefQueue
|
2014-08-16 04:38:08 +04:00
|
|
|
{
|
|
|
|
std::queue<T*> mQueue;
|
|
|
|
|
|
|
|
public:
|
2015-06-05 03:15:38 +03:00
|
|
|
~RefQueue() {
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear() {
|
|
|
|
while (!empty()) {
|
|
|
|
pop();
|
|
|
|
}
|
2014-08-16 04:38:08 +04:00
|
|
|
}
|
2014-08-16 04:38:09 +04:00
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
bool empty() const {
|
2014-08-16 04:38:08 +04:00
|
|
|
return mQueue.empty();
|
|
|
|
}
|
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
size_t size() const {
|
|
|
|
return mQueue.size();
|
2014-08-16 04:38:08 +04:00
|
|
|
}
|
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
void push(T* x) {
|
|
|
|
mQueue.push(x);
|
|
|
|
x->AddRef();
|
|
|
|
}
|
2014-08-16 04:38:08 +04:00
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
T* front() const {
|
|
|
|
return mQueue.front();
|
|
|
|
}
|
2014-08-16 04:38:08 +04:00
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
void pop() {
|
|
|
|
T* x = mQueue.front();
|
|
|
|
x->Release();
|
|
|
|
mQueue.pop();
|
2014-08-16 04:38:08 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-10-08 07:59:37 +04:00
|
|
|
class SurfaceFactory : public SupportsWeakPtr<SurfaceFactory>
|
2014-07-12 02:10:49 +04:00
|
|
|
{
|
|
|
|
public:
|
2014-10-08 07:59:37 +04:00
|
|
|
// Should use the VIRTUAL version, but it's currently incompatible
|
|
|
|
// with SupportsWeakPtr. (bug 1049278)
|
2015-04-24 19:43:01 +03:00
|
|
|
MOZ_DECLARE_WEAKREFERENCE_TYPENAME(SurfaceFactory)
|
2014-10-08 07:59:37 +04:00
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
const SharedSurfaceType mType;
|
2014-07-12 02:10:49 +04:00
|
|
|
GLContext* const mGL;
|
|
|
|
const SurfaceCaps mCaps;
|
2016-03-21 17:18:20 +03:00
|
|
|
const RefPtr<layers::ClientIPCAllocator> mAllocator;
|
2015-06-05 03:15:38 +03:00
|
|
|
const layers::TextureFlags mFlags;
|
2014-07-12 02:10:49 +04:00
|
|
|
const GLFormats mFormats;
|
2015-10-12 06:21:02 +03:00
|
|
|
Mutex mMutex;
|
2014-07-12 02:10:49 +04:00
|
|
|
protected:
|
|
|
|
SurfaceCaps mDrawCaps;
|
|
|
|
SurfaceCaps mReadCaps;
|
2015-06-05 03:15:38 +03:00
|
|
|
RefQueue<layers::SharedSurfaceTextureClient> mRecycleFreePool;
|
|
|
|
RefSet<layers::SharedSurfaceTextureClient> mRecycleTotalPool;
|
2014-07-12 02:10:49 +04:00
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
SurfaceFactory(SharedSurfaceType type, GLContext* gl, const SurfaceCaps& caps,
|
2016-03-21 17:18:20 +03:00
|
|
|
const RefPtr<layers::ClientIPCAllocator>& allocator,
|
2015-06-05 03:15:38 +03:00
|
|
|
const layers::TextureFlags& flags);
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
public:
|
|
|
|
virtual ~SurfaceFactory();
|
|
|
|
|
|
|
|
const SurfaceCaps& DrawCaps() const {
|
|
|
|
return mDrawCaps;
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
const SurfaceCaps& ReadCaps() const {
|
|
|
|
return mReadCaps;
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
2014-07-12 02:10:49 +04:00
|
|
|
|
|
|
|
protected:
|
2014-08-16 04:38:08 +04:00
|
|
|
virtual UniquePtr<SharedSurface> CreateShared(const gfx::IntSize& size) = 0;
|
2014-07-12 02:10:49 +04:00
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
void StartRecycling(layers::SharedSurfaceTextureClient* tc);
|
|
|
|
void SetRecycleCallback(layers::SharedSurfaceTextureClient* tc);
|
|
|
|
void StopRecycling(layers::SharedSurfaceTextureClient* tc);
|
2014-07-12 02:10:49 +04:00
|
|
|
|
|
|
|
public:
|
2014-08-16 04:38:08 +04:00
|
|
|
UniquePtr<SharedSurface> NewSharedSurface(const gfx::IntSize& size);
|
2015-06-17 17:00:52 +03:00
|
|
|
//already_AddRefed<ShSurfHandle> NewShSurfHandle(const gfx::IntSize& size);
|
|
|
|
already_AddRefed<layers::SharedSurfaceTextureClient> NewTexClient(const gfx::IntSize& size);
|
2015-05-29 03:08:53 +03:00
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
static void RecycleCallback(layers::TextureClient* tc, void* /*closure*/);
|
2015-06-05 02:50:59 +03:00
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
// Auto-deletes surfs of the wrong type.
|
|
|
|
bool Recycle(layers::SharedSurfaceTextureClient* texClient);
|
2014-10-08 07:59:37 +04:00
|
|
|
};
|
|
|
|
|
2014-10-08 08:01:51 +04:00
|
|
|
class ScopedReadbackFB
|
|
|
|
{
|
|
|
|
GLContext* const mGL;
|
|
|
|
ScopedBindFramebuffer mAutoFB;
|
|
|
|
GLuint mTempFB;
|
|
|
|
GLuint mTempTex;
|
|
|
|
SharedSurface* mSurfToUnlock;
|
|
|
|
SharedSurface* mSurfToLock;
|
|
|
|
|
|
|
|
public:
|
2014-12-11 01:48:11 +03:00
|
|
|
explicit ScopedReadbackFB(SharedSurface* src);
|
2014-10-08 08:01:51 +04:00
|
|
|
~ScopedReadbackFB();
|
|
|
|
};
|
|
|
|
|
2014-10-11 01:36:17 +04:00
|
|
|
bool ReadbackSharedSurface(SharedSurface* src, gfx::DrawTarget* dst);
|
2015-01-15 05:04:50 +03:00
|
|
|
uint32_t ReadPixel(SharedSurface* src);
|
2014-10-11 01:36:17 +04:00
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
} // namespace gl
|
|
|
|
} // namespace mozilla
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
#endif // SHARED_SURFACE_H_
|