2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 4; -*- */
|
2013-02-14 03:26:24 +04:00
|
|
|
/* 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-09-27 06:22:20 +03:00
|
|
|
class LayersIPCChannel;
|
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:
|
2019-06-17 07:40:13 +03:00
|
|
|
static bool ProdCopy(SharedSurface* src, SharedSurface* dest,
|
2014-07-12 02:10:49 +04:00
|
|
|
SurfaceFactory* factory);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
const SharedSurfaceType mType;
|
|
|
|
const AttachmentType mAttachType;
|
2016-10-24 18:35:31 +03:00
|
|
|
const WeakPtr<GLContext> 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;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
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;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
SharedSurface(SharedSurfaceType type, AttachmentType attachType,
|
2013-12-10 20:11:58 +04:00
|
|
|
GLContext* gl, const gfx::IntSize& size, bool hasAlpha,
|
2015-06-05 03:15:38 +03:00
|
|
|
bool canRecycle);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
|
|
|
public:
|
2014-08-16 04:38:08 +04:00
|
|
|
virtual ~SharedSurface();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
// Specifies to the TextureClient any flags which
|
2017-12-20 11:43:11 +03:00
|
|
|
// are required by the SharedSurface backend.
|
|
|
|
virtual layers::TextureFlags GetTextureFlags() const;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-07-09 03:44:17 +03:00
|
|
|
bool IsLocked() const { return mIsLocked; }
|
2015-10-19 19:26:26 +03:00
|
|
|
bool IsProducerAcquired() const { return mIsProducerAcquired; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-10-19 19:26:26 +03: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();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-10-19 19:26:26 +03:00
|
|
|
// Unlocking is harmless if we're already unlocked.
|
2013-02-14 03:26:24 +04:00
|
|
|
void UnlockProd();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-10-19 19:26:26 +03:00
|
|
|
// This surface has been moved to the front buffer and will not be locked
|
|
|
|
// again until it is recycled. Do any finalization steps here.
|
|
|
|
virtual void Commit() {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-10-19 19:26:26 +03:00
|
|
|
protected:
|
|
|
|
virtual void LockProdImpl() = 0;
|
2013-02-14 03:26:24 +04:00
|
|
|
virtual void UnlockProdImpl() = 0;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-10-19 19:26:26 +03:00
|
|
|
virtual void ProducerAcquireImpl() = 0;
|
2016-07-09 03:44:17 +03:00
|
|
|
virtual void ProducerReleaseImpl() = 0;
|
|
|
|
virtual void ProducerReadAcquireImpl() { ProducerAcquireImpl(); }
|
2016-01-06 02:57:44 +03:00
|
|
|
virtual void ProducerReadReleaseImpl() { ProducerReleaseImpl(); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
public:
|
2016-01-06 02:57:44 +03:00
|
|
|
void ProducerAcquire() {
|
2013-02-14 03:26:24 +04:00
|
|
|
MOZ_ASSERT(!mIsProducerAcquired);
|
|
|
|
ProducerAcquireImpl();
|
|
|
|
mIsProducerAcquired = true;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-06-05 03:15:38 +03:00
|
|
|
void ProducerRelease() {
|
2014-10-10 00:33:22 +04:00
|
|
|
MOZ_ASSERT(mIsProducerAcquired);
|
2015-06-05 03:15:38 +03:00
|
|
|
ProducerReleaseImpl();
|
2013-02-14 03:26:24 +04:00
|
|
|
mIsProducerAcquired = false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2014-07-12 02:10:49 +04:00
|
|
|
void ProducerReadAcquire() {
|
2015-06-05 03:15:38 +03:00
|
|
|
MOZ_ASSERT(!mIsProducerAcquired);
|
2014-07-12 02:10:49 +04:00
|
|
|
ProducerReadAcquireImpl();
|
2017-03-04 00:14:27 +03:00
|
|
|
mIsProducerAcquired = true;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2017-03-04 00:14:27 +03:00
|
|
|
void ProducerReadRelease() {
|
|
|
|
MOZ_ASSERT(mIsProducerAcquired);
|
2015-06-05 03:15:38 +03:00
|
|
|
ProducerReadReleaseImpl();
|
2017-03-04 00:14:27 +03:00
|
|
|
mIsProducerAcquired = false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2017-03-04 00:14:27 +03: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.
|
2016-01-06 02:57:44 +03:00
|
|
|
virtual void WaitForBufferOwnership() {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-01-06 02:57:44 +03:00
|
|
|
// Returns true if the buffer is available.
|
2018-07-28 13:33:55 +03:00
|
|
|
// You can call WaitForBufferOwnership to wait for availability.
|
2016-01-06 02:57:44 +03:00
|
|
|
virtual bool IsBufferAvailable() const { return true; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-01-06 02:57:44 +03:00
|
|
|
// For use when AttachType is correct.
|
|
|
|
virtual GLenum ProdTextureTarget() const {
|
|
|
|
MOZ_ASSERT(mAttachType == AttachmentType::GLTexture);
|
2014-07-12 02:10:49 +04:00
|
|
|
return LOCAL_GL_TEXTURE_2D;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2016-01-06 02:57:44 +03:00
|
|
|
virtual GLuint ProdTexture() {
|
|
|
|
MOZ_ASSERT(mAttachType == AttachmentType::GLTexture);
|
|
|
|
MOZ_CRASH("GFX: Did you forget to override this function?");
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-01-06 02:57:44 +03:00
|
|
|
virtual GLuint ProdRenderbuffer() {
|
|
|
|
MOZ_ASSERT(mAttachType == AttachmentType::GLRenderbuffer);
|
|
|
|
MOZ_CRASH("GFX: Did you forget to override this function?");
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2016-01-06 02:57:44 +03:00
|
|
|
virtual bool CopyTexImage2D(GLenum target, GLint level, GLenum internalformat,
|
2014-10-10 00:33:22 +04:00
|
|
|
GLint x, GLint y, GLsizei width, GLsizei height,
|
|
|
|
GLint border) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-10-10 00:33:22 +04:00
|
|
|
virtual bool ReadPixels(GLint x, GLint y, GLsizei width, GLsizei height,
|
|
|
|
GLenum format, GLenum type, GLvoid* pixels) {
|
|
|
|
return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2014-10-10 00:33:22 +04:00
|
|
|
virtual bool NeedsIndirectReads() const { return false; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
virtual bool ToSurfaceDescriptor(
|
2014-10-10 00:33:22 +04:00
|
|
|
layers::SurfaceDescriptor* const out_descriptor) = 0;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
virtual bool ReadbackBySharedHandle(gfx::DataSourceSurface* out_surface) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
};
|
2014-09-03 02:15:41 +04:00
|
|
|
|
2014-05-15 02:40:15 +04:00
|
|
|
template <typename T>
|
|
|
|
class RefSet {
|
|
|
|
std::set<T*> mSet;
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2018-07-28 13:33:55 +03:00
|
|
|
public:
|
|
|
|
~RefSet() { clear(); }
|
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
auto begin() -> decltype(mSet.begin()) { return mSet.begin(); }
|
|
|
|
|
|
|
|
void clear() {
|
2016-05-13 23:34:41 +03:00
|
|
|
for (auto itr = mSet.begin(); itr != mSet.end(); ++itr) {
|
|
|
|
(*itr)->Release();
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
2015-06-05 03:15:38 +03:00
|
|
|
mSet.clear();
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
bool empty() const { return mSet.empty(); }
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2015-03-23 06:55:26 +03:00
|
|
|
bool insert(T* x) {
|
|
|
|
if (mSet.insert(x).second) {
|
2015-06-05 03:15:38 +03:00
|
|
|
x->AddRef();
|
2015-03-23 06:55:26 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-12 02:10:49 +04:00
|
|
|
return false;
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
2014-10-08 08:01:51 +04:00
|
|
|
|
|
|
|
bool erase(T* x) {
|
2015-06-05 03:15:38 +03:00
|
|
|
if (mSet.erase(x)) {
|
2014-10-08 08:01:51 +04:00
|
|
|
x->Release();
|
|
|
|
return true;
|
|
|
|
}
|
2015-06-05 03:15:38 +03:00
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
return false;
|
|
|
|
}
|
2015-06-05 03:15:38 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class RefQueue {
|
|
|
|
std::queue<T*> mQueue;
|
|
|
|
|
|
|
|
public:
|
|
|
|
~RefQueue() { clear(); }
|
|
|
|
|
|
|
|
void clear() {
|
|
|
|
while (!empty()) {
|
|
|
|
pop();
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-06-05 03:15:38 +03:00
|
|
|
|
|
|
|
bool empty() const { return mQueue.empty(); }
|
|
|
|
|
|
|
|
size_t size() const { return mQueue.size(); }
|
|
|
|
|
|
|
|
void push(T* x) {
|
|
|
|
mQueue.push(x);
|
|
|
|
x->AddRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
T* front() const { return mQueue.front(); }
|
|
|
|
|
|
|
|
void pop() {
|
|
|
|
T* x = mQueue.front();
|
|
|
|
x->Release();
|
|
|
|
mQueue.pop();
|
|
|
|
}
|
2014-07-12 02:10:49 +04:00
|
|
|
};
|
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
class SurfaceFactory : public SupportsWeakPtr<SurfaceFactory> {
|
2014-08-16 04:38:08 +04:00
|
|
|
public:
|
2015-06-05 03:15:38 +03:00
|
|
|
// Should use the VIRTUAL version, but it's currently incompatible
|
|
|
|
// with SupportsWeakPtr. (bug 1049278)
|
2014-08-16 04:38:08 +04:00
|
|
|
MOZ_DECLARE_WEAKREFERENCE_TYPENAME(SurfaceFactory)
|
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
const SharedSurfaceType mType;
|
2014-07-12 02:10:49 +04:00
|
|
|
GLContext* const mGL;
|
2015-06-05 03:15:38 +03:00
|
|
|
const SurfaceCaps mCaps;
|
|
|
|
const RefPtr<layers::LayersIPCChannel> mAllocator;
|
|
|
|
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-08-16 04:38:08 +04:00
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
protected:
|
2014-07-12 02:10:49 +04:00
|
|
|
SurfaceCaps mDrawCaps;
|
|
|
|
SurfaceCaps mReadCaps;
|
2015-06-05 03:15:38 +03:00
|
|
|
RefQueue<layers::SharedSurfaceTextureClient> mRecycleFreePool;
|
|
|
|
RefSet<layers::SharedSurfaceTextureClient> mRecycleTotalPool;
|
2014-08-16 04:38:08 +04:00
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
SurfaceFactory(SharedSurfaceType type, GLContext* gl, const SurfaceCaps& caps,
|
|
|
|
const RefPtr<layers::LayersIPCChannel>& allocator,
|
|
|
|
const layers::TextureFlags& flags);
|
2014-08-16 04:38:08 +04:00
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
public:
|
|
|
|
virtual ~SurfaceFactory();
|
2014-08-16 04:38:08 +04:00
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
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; }
|
|
|
|
|
|
|
|
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);
|
2017-10-06 18:38:40 +03:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2019-04-11 15:36:51 +03:00
|
|
|
class ScopedReadbackFB final {
|
2014-10-08 08:01:51 +04:00
|
|
|
GLContext* const mGL;
|
|
|
|
ScopedBindFramebuffer mAutoFB;
|
2018-08-18 01:21:26 +03:00
|
|
|
GLuint mTempFB = 0;
|
|
|
|
GLuint mTempTex = 0;
|
|
|
|
SharedSurface* mSurfToUnlock = nullptr;
|
|
|
|
SharedSurface* mSurfToLock = nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-10-08 08:01:51 +04:00
|
|
|
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_
|