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 struct SharedSurfaceDesc
: public PartialSharedSurfaceDesc
{
65 gfx::IntSize size
= {};
70 const SharedSurfaceDesc mDesc
;
71 const UniquePtr
<MozFramebuffer
> mFb
; // null if we should use fb=0.
74 bool mIsLocked
= false;
75 bool mIsProducerAcquired
= false;
77 SharedSurface(const SharedSurfaceDesc
&, UniquePtr
<MozFramebuffer
>);
80 virtual ~SharedSurface();
82 bool IsLocked() const { return mIsLocked
; }
83 bool IsProducerAcquired() const { return mIsProducerAcquired
; }
85 // This locks the SharedSurface as the production buffer for the context.
86 // This is needed by backends which use PBuffers and/or EGLSurfaces.
89 // Unlocking is harmless if we're already unlocked.
92 // This surface has been moved to the front buffer and will not be locked
93 // again until it is recycled. Do any finalization steps here.
94 virtual void Commit() {}
97 virtual void LockProdImpl(){};
98 virtual void UnlockProdImpl(){};
100 virtual void ProducerAcquireImpl(){};
101 virtual void ProducerReleaseImpl(){};
103 virtual void ProducerReadAcquireImpl() { ProducerAcquireImpl(); }
104 virtual void ProducerReadReleaseImpl() { ProducerReleaseImpl(); }
107 void ProducerAcquire() {
108 MOZ_ASSERT(!mIsProducerAcquired
);
109 ProducerAcquireImpl();
110 mIsProducerAcquired
= true;
112 void ProducerRelease() {
113 MOZ_ASSERT(mIsProducerAcquired
);
114 ProducerReleaseImpl();
115 mIsProducerAcquired
= false;
117 void ProducerReadAcquire() {
118 MOZ_ASSERT(!mIsProducerAcquired
);
119 ProducerReadAcquireImpl();
120 mIsProducerAcquired
= true;
122 void ProducerReadRelease() {
123 MOZ_ASSERT(mIsProducerAcquired
);
124 ProducerReadReleaseImpl();
125 mIsProducerAcquired
= false;
128 // This function waits until the buffer is no longer being used.
129 // To optimize the performance, some implementaions recycle SharedSurfaces
130 // even when its buffer is still being used.
131 virtual void WaitForBufferOwnership() {}
133 // Returns true if the buffer is available.
134 // You can call WaitForBufferOwnership to wait for availability.
135 virtual bool IsBufferAvailable() const { return true; }
137 virtual bool NeedsIndirectReads() const { return false; }
139 virtual Maybe
<layers::SurfaceDescriptor
> ToSurfaceDescriptor() = 0;
144 class SurfaceFactory
{
146 const PartialSharedSurfaceDesc mDesc
;
152 static UniquePtr
<SurfaceFactory
> Create(GLContext
*, layers::TextureType
);
155 explicit SurfaceFactory(const PartialSharedSurfaceDesc
&);
158 virtual ~SurfaceFactory();
161 virtual UniquePtr
<SharedSurface
> CreateSharedImpl(
162 const SharedSurfaceDesc
&) = 0;
165 UniquePtr
<SharedSurface
> CreateShared(const gfx::IntSize
& size
) {
166 return CreateSharedImpl({mDesc
, size
});
170 template <typename T
>
171 inline UniquePtr
<T
> AsUnique(T
* const p
) {
172 return UniquePtr
<T
>(p
);
176 } // namespace mozilla
178 #endif // SHARED_SURFACE_H_