1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 4; -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /* SharedSurface abstracts an actual surface (can be a GL texture, but
7 * not necessarily) that handles sharing.
8 * Its specializations are:
9 * SharedSurface_Basic (client-side bitmap, does readback)
10 * SharedSurface_GLTexture
11 * SharedSurface_EGLImage
12 * SharedSurface_ANGLEShareHandle
15 #ifndef SHARED_SURFACE_H_
16 #define SHARED_SURFACE_H_
22 #include "GLContext.h" // Bug 1635644
23 #include "GLContextTypes.h"
25 #include "mozilla/Attributes.h"
26 #include "mozilla/DebugOnly.h"
27 #include "mozilla/gfx/Point.h"
28 #include "mozilla/Mutex.h"
29 #include "mozilla/UniquePtr.h"
30 #include "mozilla/WeakPtr.h"
31 #include "SurfaceTypes.h"
37 class DataSourceSurface
;
42 class KnowsCompositor
;
43 enum class LayersBackend
: int8_t;
44 class LayersIPCChannel
;
45 class SharedSurfaceTextureClient
;
46 class SurfaceDescriptor
;
48 enum class TextureFlags
: uint32_t;
49 enum class TextureType
: int8_t;
55 struct ScopedBindFramebuffer
;
58 struct PartialSharedSurfaceDesc
{
59 const WeakPtr
<GLContext
> gl
;
60 const SharedSurfaceType type
;
61 const layers::TextureType consumerType
;
62 const bool canRecycle
;
64 bool operator==(const PartialSharedSurfaceDesc
& rhs
) const {
65 return gl
== rhs
.gl
&& type
== rhs
.type
&&
66 consumerType
== rhs
.consumerType
&& canRecycle
== rhs
.canRecycle
;
69 struct SharedSurfaceDesc
: public PartialSharedSurfaceDesc
{
70 gfx::IntSize size
= {};
71 gfx::ColorSpace2 colorSpace
= gfx::ColorSpace2::UNKNOWN
;
73 bool operator==(const SharedSurfaceDesc
& rhs
) const {
74 return PartialSharedSurfaceDesc::operator==(rhs
) && size
== rhs
.size
&&
75 colorSpace
== rhs
.colorSpace
;
77 bool operator!=(const SharedSurfaceDesc
& rhs
) const {
78 return !(*this == rhs
);
84 const SharedSurfaceDesc mDesc
;
85 const UniquePtr
<MozFramebuffer
> mFb
; // null if we should use fb=0.
88 bool mIsLocked
= false;
89 bool mIsProducerAcquired
= false;
91 SharedSurface(const SharedSurfaceDesc
&, UniquePtr
<MozFramebuffer
>);
94 virtual ~SharedSurface();
96 bool IsLocked() const { return mIsLocked
; }
97 bool IsProducerAcquired() const { return mIsProducerAcquired
; }
99 // This locks the SharedSurface as the production buffer for the context.
100 // This is needed by backends which use PBuffers and/or EGLSurfaces.
103 // Unlocking is harmless if we're already unlocked.
106 // This surface has been moved to the front buffer and will not be locked
107 // again until it is recycled. Do any finalization steps here.
108 virtual void Commit() {}
111 virtual void LockProdImpl(){};
112 virtual void UnlockProdImpl(){};
114 virtual void ProducerAcquireImpl(){};
115 virtual void ProducerReleaseImpl(){};
117 virtual void ProducerReadAcquireImpl() { ProducerAcquireImpl(); }
118 virtual void ProducerReadReleaseImpl() { ProducerReleaseImpl(); }
121 void ProducerAcquire() {
122 MOZ_ASSERT(!mIsProducerAcquired
);
123 ProducerAcquireImpl();
124 mIsProducerAcquired
= true;
126 void ProducerRelease() {
127 MOZ_ASSERT(mIsProducerAcquired
);
128 ProducerReleaseImpl();
129 mIsProducerAcquired
= false;
131 void ProducerReadAcquire() {
132 MOZ_ASSERT(!mIsProducerAcquired
);
133 ProducerReadAcquireImpl();
134 mIsProducerAcquired
= true;
136 void ProducerReadRelease() {
137 MOZ_ASSERT(mIsProducerAcquired
);
138 ProducerReadReleaseImpl();
139 mIsProducerAcquired
= false;
142 // This function waits until the buffer is no longer being used.
143 // To optimize the performance, some implementaions recycle SharedSurfaces
144 // even when its buffer is still being used.
145 virtual void WaitForBufferOwnership() {}
147 // Returns true if the buffer is available.
148 // You can call WaitForBufferOwnership to wait for availability.
149 virtual bool IsBufferAvailable() const { return true; }
151 virtual bool NeedsIndirectReads() const { return false; }
153 // Returns true if the surface is still valid to use. If false, the underlying
154 // resource has been released and we must allocate a new surface instead.
155 virtual bool IsValid() const { return true; };
157 virtual Maybe
<layers::SurfaceDescriptor
> ToSurfaceDescriptor() = 0;
162 class SurfaceFactory
{
164 const PartialSharedSurfaceDesc mDesc
;
166 layers::TextureType
GetConsumerType() const { return mDesc
.consumerType
; }
169 Mutex mMutex MOZ_UNANNOTATED
;
172 static UniquePtr
<SurfaceFactory
> Create(GLContext
*, layers::TextureType
);
175 explicit SurfaceFactory(const PartialSharedSurfaceDesc
&);
178 virtual ~SurfaceFactory();
181 virtual UniquePtr
<SharedSurface
> CreateSharedImpl(
182 const SharedSurfaceDesc
&) = 0;
185 UniquePtr
<SharedSurface
> CreateShared(const gfx::IntSize
& size
,
186 gfx::ColorSpace2 cs
) {
187 return CreateSharedImpl({mDesc
, size
, cs
});
191 template <typename T
>
192 inline UniquePtr
<T
> AsUnique(T
* const p
) {
193 return UniquePtr
<T
>(p
);
197 } // namespace mozilla
199 #endif // SHARED_SURFACE_H_