1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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 #ifndef mozilla_layers_SurfacePoolWayland_h
7 #define mozilla_layers_SurfacePoolWayland_h
9 #include <wayland-egl.h>
11 #include "GLContext.h"
12 #include "MozFramebuffer.h"
13 #include "mozilla/layers/SurfacePool.h"
14 #include "mozilla/widget/WaylandBuffer.h"
16 #include <unordered_map>
18 namespace mozilla::layers
{
20 class SurfacePoolWayland final
: public SurfacePool
{
22 // Get a handle for a new window. aGL can be nullptr.
23 RefPtr
<SurfacePoolHandle
> GetHandleForGL(gl::GLContext
* aGL
) override
;
25 // Destroy all GL resources associated with aGL managed by this pool.
26 void DestroyGLResourcesForContext(gl::GLContext
* aGL
) override
;
29 friend class SurfacePoolHandleWayland
;
30 friend RefPtr
<SurfacePool
> SurfacePool::Create(size_t aPoolSizeLimit
);
32 explicit SurfacePoolWayland(size_t aPoolSizeLimit
);
34 RefPtr
<widget::WaylandBuffer
> ObtainBufferFromPool(const gfx::IntSize
& aSize
,
36 void ReturnBufferToPool(const RefPtr
<widget::WaylandBuffer
>& aBuffer
);
37 void EnforcePoolSizeLimit();
38 void CollectPendingSurfaces();
39 Maybe
<GLuint
> GetFramebufferForBuffer(
40 const RefPtr
<widget::WaylandBuffer
>& aBuffer
, gl::GLContext
* aGL
,
41 bool aNeedsDepthBuffer
);
43 struct GLResourcesForBuffer final
{
44 RefPtr
<gl::GLContext
> mGL
; // non-null
45 UniquePtr
<gl::MozFramebuffer
> mFramebuffer
; // non-null
48 struct SurfacePoolEntry final
{
49 const gfx::IntSize mSize
;
50 const RefPtr
<widget::WaylandBuffer
> mWaylandBuffer
; // non-null
51 Maybe
<GLResourcesForBuffer
> mGLResources
;
54 bool CanRecycleSurfaceForRequest(const MutexAutoLock
& aProofOfLock
,
55 const SurfacePoolEntry
& aEntry
,
56 const gfx::IntSize
& aSize
,
59 RefPtr
<gl::DepthAndStencilBuffer
> GetDepthBufferForSharing(
60 const MutexAutoLock
& aProofOfLock
, gl::GLContext
* aGL
,
61 const gfx::IntSize
& aSize
);
62 UniquePtr
<gl::MozFramebuffer
> CreateFramebufferForTexture(
63 const MutexAutoLock
& aProofOfLock
, gl::GLContext
* aGL
,
64 const gfx::IntSize
& aSize
, GLuint aTexture
, bool aNeedsDepthBuffer
);
66 Mutex mMutex MOZ_UNANNOTATED
;
68 // Stores the entries for surfaces that are in use by NativeLayerWayland, i.e.
69 // an entry is inside mInUseEntries between calls to ObtainSurfaceFromPool()
70 // and ReturnSurfaceToPool().
71 std::unordered_map
<widget::WaylandBuffer
*, SurfacePoolEntry
> mInUseEntries
;
73 // Stores entries which are no longer in use by NativeLayerWayland but are
74 // still in use by the window server, i.e. for which
75 // WaylandBuffer::IsAttached() still returns true.
76 // These entries are checked once per frame inside
77 // CollectPendingSurfaces(), and returned to mAvailableEntries once the
78 // window server is done.
79 nsTArray
<SurfacePoolEntry
> mPendingEntries
;
81 // Stores entries which are available for recycling. These entries are not
82 // in use by a NativeLayerWayland or by the window server.
83 nsTArray
<SurfacePoolEntry
> mAvailableEntries
;
84 size_t mPoolSizeLimit
;
87 void ForEachEntry(F aFn
);
89 struct DepthBufferEntry final
{
90 RefPtr
<gl::GLContext
> mGL
;
92 WeakPtr
<gl::DepthAndStencilBuffer
> mBuffer
;
95 nsTArray
<DepthBufferEntry
> mDepthBuffers
;
98 // A surface pool handle that is stored on NativeLayerWayland and keeps the
100 class SurfacePoolHandleWayland final
: public SurfacePoolHandle
{
102 SurfacePoolHandleWayland
* AsSurfacePoolHandleWayland() override
{
106 RefPtr
<widget::WaylandBuffer
> ObtainBufferFromPool(const gfx::IntSize
& aSize
);
107 void ReturnBufferToPool(const RefPtr
<widget::WaylandBuffer
>& aBuffer
);
108 Maybe
<GLuint
> GetFramebufferForBuffer(
109 const RefPtr
<widget::WaylandBuffer
>& aBuffer
, bool aNeedsDepthBuffer
);
110 const auto& gl() { return mGL
; }
112 RefPtr
<SurfacePool
> Pool() override
{ return mPool
; }
113 void OnBeginFrame() override
;
114 void OnEndFrame() override
;
117 friend class SurfacePoolWayland
;
118 SurfacePoolHandleWayland(RefPtr
<SurfacePoolWayland
> aPool
,
121 const RefPtr
<SurfacePoolWayland
> mPool
;
122 const RefPtr
<gl::GLContext
> mGL
;
125 } // namespace mozilla::layers