2018-11-30 19:52:05 +00:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 4; -*- */
|
2013-02-13 23:26:24 +00: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-11 22:10:49 +00:00
|
|
|
#include <queue>
|
2015-06-05 00:15:38 +00:00
|
|
|
#include <set>
|
2013-07-30 14:25:31 +00:00
|
|
|
#include <stdint.h>
|
2014-07-11 22:10:49 +00:00
|
|
|
|
2020-06-15 18:25:55 +00:00
|
|
|
#include "GLContext.h" // Bug 1635644
|
2014-07-11 22:10:49 +00:00
|
|
|
#include "GLContextTypes.h"
|
2013-02-13 23:26:24 +00:00
|
|
|
#include "GLDefs.h"
|
2014-07-11 22:10:49 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-09-02 22:15:41 +00:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2013-12-10 16:11:58 +00:00
|
|
|
#include "mozilla/gfx/Point.h"
|
2015-10-12 03:21:02 +00:00
|
|
|
#include "mozilla/Mutex.h"
|
2014-08-16 00:38:08 +00:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2014-08-16 00:38:08 +00:00
|
|
|
#include "mozilla/WeakPtr.h"
|
2013-02-13 23:26:24 +00:00
|
|
|
#include "SurfaceTypes.h"
|
|
|
|
|
2014-09-02 22:15:41 +00:00
|
|
|
class nsIThread;
|
|
|
|
|
2013-02-13 23:26:24 +00:00
|
|
|
namespace mozilla {
|
2014-10-10 21:36:17 +00:00
|
|
|
namespace gfx {
|
2015-10-12 03:21:03 +00:00
|
|
|
class DataSourceSurface;
|
2014-10-10 21:36:17 +00:00
|
|
|
class DrawTarget;
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace gfx
|
2015-06-05 00:15:38 +00:00
|
|
|
|
|
|
|
namespace layers {
|
2020-06-15 18:25:55 +00:00
|
|
|
class KnowsCompositor;
|
|
|
|
enum class LayersBackend : int8_t;
|
2016-09-27 03:22:20 +00:00
|
|
|
class LayersIPCChannel;
|
2015-06-05 00:15:38 +00:00
|
|
|
class SharedSurfaceTextureClient;
|
|
|
|
class SurfaceDescriptor;
|
|
|
|
class TextureClient;
|
2020-06-15 18:25:55 +00:00
|
|
|
enum class TextureFlags : uint32_t;
|
|
|
|
enum class TextureType : int8_t;
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace layers
|
2015-06-05 00:15:38 +00:00
|
|
|
|
2014-07-11 22:10:49 +00:00
|
|
|
namespace gl {
|
2013-02-13 23:26:24 +00:00
|
|
|
|
2020-06-15 18:25:55 +00:00
|
|
|
class MozFramebuffer;
|
|
|
|
struct ScopedBindFramebuffer;
|
2013-02-13 23:26:24 +00:00
|
|
|
class SurfaceFactory;
|
2020-06-15 18:25:55 +00:00
|
|
|
|
|
|
|
struct PartialSharedSurfaceDesc {
|
|
|
|
const WeakPtr<GLContext> gl;
|
|
|
|
const SharedSurfaceType type;
|
|
|
|
const layers::TextureType consumerType;
|
|
|
|
const bool canRecycle;
|
2022-05-02 23:54:09 +00:00
|
|
|
|
|
|
|
bool operator==(const PartialSharedSurfaceDesc& rhs) const {
|
|
|
|
return gl == rhs.gl && type == rhs.type &&
|
|
|
|
consumerType == rhs.consumerType && canRecycle == rhs.canRecycle;
|
|
|
|
}
|
2020-06-15 18:25:55 +00:00
|
|
|
};
|
|
|
|
struct SharedSurfaceDesc : public PartialSharedSurfaceDesc {
|
|
|
|
gfx::IntSize size = {};
|
2022-05-02 23:54:09 +00:00
|
|
|
gfx::ColorSpace2 colorSpace = gfx::ColorSpace2::UNKNOWN;
|
|
|
|
|
|
|
|
bool operator==(const SharedSurfaceDesc& rhs) const {
|
|
|
|
return PartialSharedSurfaceDesc::operator==(rhs) && size == rhs.size &&
|
|
|
|
colorSpace == rhs.colorSpace;
|
|
|
|
}
|
|
|
|
bool operator!=(const SharedSurfaceDesc& rhs) const {
|
|
|
|
return !(*this == rhs);
|
|
|
|
}
|
2020-06-15 18:25:55 +00:00
|
|
|
};
|
2013-02-13 23:26:24 +00:00
|
|
|
|
|
|
|
class SharedSurface {
|
2014-07-11 22:10:49 +00:00
|
|
|
public:
|
2020-06-15 18:25:55 +00:00
|
|
|
const SharedSurfaceDesc mDesc;
|
|
|
|
const UniquePtr<MozFramebuffer> mFb; // null if we should use fb=0.
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2014-07-11 22:10:49 +00:00
|
|
|
protected:
|
2020-06-15 18:25:55 +00:00
|
|
|
bool mIsLocked = false;
|
|
|
|
bool mIsProducerAcquired = false;
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2020-06-15 18:25:55 +00:00
|
|
|
SharedSurface(const SharedSurfaceDesc&, UniquePtr<MozFramebuffer>);
|
2018-11-30 10:46:48 +00:00
|
|
|
|
|
|
|
public:
|
2014-08-16 00:38:08 +00:00
|
|
|
virtual ~SharedSurface();
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2016-07-09 00:44:17 +00:00
|
|
|
bool IsLocked() const { return mIsLocked; }
|
2015-10-19 16:26:26 +00:00
|
|
|
bool IsProducerAcquired() const { return mIsProducerAcquired; }
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2015-10-19 16:26:26 +00: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 22:10:49 +00:00
|
|
|
void LockProd();
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2015-10-19 16:26:26 +00:00
|
|
|
// Unlocking is harmless if we're already unlocked.
|
2013-02-13 23:26:24 +00:00
|
|
|
void UnlockProd();
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2015-10-19 16:26:26 +00: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 10:46:48 +00:00
|
|
|
|
2015-10-19 16:26:26 +00:00
|
|
|
protected:
|
2020-06-15 18:25:55 +00:00
|
|
|
virtual void LockProdImpl(){};
|
|
|
|
virtual void UnlockProdImpl(){};
|
|
|
|
|
2024-02-13 04:25:35 +00:00
|
|
|
virtual void ProducerAcquireImpl(){};
|
2020-06-15 18:25:55 +00:00
|
|
|
virtual void ProducerReleaseImpl(){};
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2024-02-13 04:25:35 +00:00
|
|
|
virtual void ProducerReadAcquireImpl() { ProducerAcquireImpl(); }
|
2016-01-05 23:57:44 +00:00
|
|
|
virtual void ProducerReadReleaseImpl() { ProducerReleaseImpl(); }
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2013-02-13 23:26:24 +00:00
|
|
|
public:
|
2024-02-13 04:25:35 +00:00
|
|
|
void ProducerAcquire() {
|
2013-02-13 23:26:24 +00:00
|
|
|
MOZ_ASSERT(!mIsProducerAcquired);
|
2024-02-13 04:25:35 +00:00
|
|
|
ProducerAcquireImpl();
|
|
|
|
mIsProducerAcquired = true;
|
2018-11-30 10:46:48 +00:00
|
|
|
}
|
2015-06-05 00:15:38 +00:00
|
|
|
void ProducerRelease() {
|
2014-10-09 20:33:22 +00:00
|
|
|
MOZ_ASSERT(mIsProducerAcquired);
|
2015-06-05 00:15:38 +00:00
|
|
|
ProducerReleaseImpl();
|
2013-02-13 23:26:24 +00:00
|
|
|
mIsProducerAcquired = false;
|
2018-11-30 10:46:48 +00:00
|
|
|
}
|
2024-02-13 04:25:35 +00:00
|
|
|
void ProducerReadAcquire() {
|
2015-06-05 00:15:38 +00:00
|
|
|
MOZ_ASSERT(!mIsProducerAcquired);
|
2024-02-13 04:25:35 +00:00
|
|
|
ProducerReadAcquireImpl();
|
|
|
|
mIsProducerAcquired = true;
|
2018-11-30 10:46:48 +00:00
|
|
|
}
|
2017-03-03 21:14:27 +00:00
|
|
|
void ProducerReadRelease() {
|
|
|
|
MOZ_ASSERT(mIsProducerAcquired);
|
2015-06-05 00:15:38 +00:00
|
|
|
ProducerReadReleaseImpl();
|
2017-03-03 21:14:27 +00:00
|
|
|
mIsProducerAcquired = false;
|
2018-11-30 10:46:48 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 21:14:27 +00: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-05 23:57:44 +00:00
|
|
|
virtual void WaitForBufferOwnership() {}
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2016-01-05 23:57:44 +00:00
|
|
|
// Returns true if the buffer is available.
|
2018-07-28 10:33:55 +00:00
|
|
|
// You can call WaitForBufferOwnership to wait for availability.
|
2016-01-05 23:57:44 +00:00
|
|
|
virtual bool IsBufferAvailable() const { return true; }
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2014-10-09 20:33:22 +00:00
|
|
|
virtual bool NeedsIndirectReads() const { return false; }
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2022-02-02 17:01:02 +00:00
|
|
|
// Returns true if the surface is still valid to use. If false, the underlying
|
|
|
|
// resource has been released and we must allocate a new surface instead.
|
|
|
|
virtual bool IsValid() const { return true; };
|
|
|
|
|
2020-06-15 18:25:55 +00:00
|
|
|
virtual Maybe<layers::SurfaceDescriptor> ToSurfaceDescriptor() = 0;
|
2018-11-30 10:46:48 +00:00
|
|
|
};
|
2014-09-02 22:15:41 +00:00
|
|
|
|
2020-06-15 18:25:55 +00:00
|
|
|
// -
|
2020-06-10 23:43:35 +00:00
|
|
|
|
2020-06-15 18:25:55 +00:00
|
|
|
class SurfaceFactory {
|
2020-06-10 23:43:35 +00:00
|
|
|
public:
|
2020-06-15 18:25:55 +00:00
|
|
|
const PartialSharedSurfaceDesc mDesc;
|
2020-06-11 16:44:20 +00:00
|
|
|
|
2022-07-12 06:56:19 +00:00
|
|
|
layers::TextureType GetConsumerType() const { return mDesc.consumerType; }
|
|
|
|
|
2020-06-15 18:25:55 +00:00
|
|
|
protected:
|
2022-03-16 18:47:08 +00:00
|
|
|
Mutex mMutex MOZ_UNANNOTATED;
|
2020-06-11 16:44:20 +00:00
|
|
|
|
|
|
|
public:
|
2020-06-15 18:25:55 +00:00
|
|
|
static UniquePtr<SurfaceFactory> Create(GLContext*, layers::TextureType);
|
2014-08-16 00:38:08 +00:00
|
|
|
|
2015-06-05 00:15:38 +00:00
|
|
|
protected:
|
2020-06-15 18:25:55 +00:00
|
|
|
explicit SurfaceFactory(const PartialSharedSurfaceDesc&);
|
2014-08-16 00:38:08 +00:00
|
|
|
|
2015-06-05 00:15:38 +00:00
|
|
|
public:
|
|
|
|
virtual ~SurfaceFactory();
|
2014-08-16 00:38:08 +00:00
|
|
|
|
2014-07-11 22:10:49 +00:00
|
|
|
protected:
|
2020-06-15 18:25:55 +00:00
|
|
|
virtual UniquePtr<SharedSurface> CreateSharedImpl(
|
|
|
|
const SharedSurfaceDesc&) = 0;
|
2014-07-11 22:10:49 +00:00
|
|
|
|
|
|
|
public:
|
2022-05-02 23:54:09 +00:00
|
|
|
UniquePtr<SharedSurface> CreateShared(const gfx::IntSize& size,
|
|
|
|
gfx::ColorSpace2 cs) {
|
|
|
|
return CreateSharedImpl({mDesc, size, cs});
|
2020-06-15 18:25:55 +00:00
|
|
|
}
|
2020-06-11 16:44:20 +00:00
|
|
|
};
|
|
|
|
|
2020-06-15 18:25:55 +00:00
|
|
|
template <typename T>
|
|
|
|
inline UniquePtr<T> AsUnique(T* const p) {
|
|
|
|
return UniquePtr<T>(p);
|
|
|
|
}
|
2014-10-10 21:36:17 +00:00
|
|
|
|
2014-07-11 22:10:49 +00:00
|
|
|
} // namespace gl
|
|
|
|
} // namespace mozilla
|
2013-02-13 23:26:24 +00:00
|
|
|
|
2014-07-11 22:10:49 +00:00
|
|
|
#endif // SHARED_SURFACE_H_
|