SurfacePoolWayland.h (4650B)
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/. */ 5 6 #ifndef mozilla_layers_SurfacePoolWayland_h 7 #define mozilla_layers_SurfacePoolWayland_h 8 9 #include <wayland-egl.h> 10 11 #include "GLContext.h" 12 #include "MozFramebuffer.h" 13 #include "mozilla/layers/SurfacePool.h" 14 #include "mozilla/widget/WaylandBuffer.h" 15 16 #include <unordered_map> 17 18 namespace mozilla::layers { 19 20 class SurfacePoolWayland final : public SurfacePool { 21 public: 22 // Get a handle for a new window. aGL can be nullptr. 23 RefPtr<SurfacePoolHandle> GetHandleForGL(gl::GLContext* aGL) override; 24 25 // Destroy all GL resources associated with aGL managed by this pool. 26 void DestroyGLResourcesForContext(gl::GLContext* aGL) override; 27 28 private: 29 friend class SurfacePoolHandleWayland; 30 friend RefPtr<SurfacePool> SurfacePool::Create(size_t aPoolSizeLimit); 31 32 explicit SurfacePoolWayland(size_t aPoolSizeLimit); 33 34 RefPtr<widget::WaylandBuffer> ObtainBufferFromPool( 35 const gfx::IntSize& aSize, gl::GLContext* aGL, 36 RefPtr<widget::DRMFormat> aFormat); 37 void ReturnBufferToPool(const RefPtr<widget::WaylandBuffer>& aBuffer); 38 void EnforcePoolSizeLimit(); 39 void CollectPendingSurfaces(); 40 Maybe<GLuint> GetFramebufferForBuffer( 41 const RefPtr<widget::WaylandBuffer>& aBuffer, gl::GLContext* aGL, 42 bool aNeedsDepthBuffer); 43 44 struct GLResourcesForBuffer final { 45 RefPtr<gl::GLContext> mGL; // non-null 46 UniquePtr<gl::MozFramebuffer> mFramebuffer; // non-null 47 }; 48 49 struct SurfacePoolEntry final { 50 const gfx::IntSize mSize; 51 const RefPtr<widget::WaylandBuffer> mWaylandBuffer; // non-null 52 Maybe<GLResourcesForBuffer> mGLResources; 53 }; 54 55 bool CanRecycleSurfaceForRequest(const MutexAutoLock& aProofOfLock, 56 const SurfacePoolEntry& aEntry, 57 const gfx::IntSize& aSize, 58 gl::GLContext* aGL); 59 60 RefPtr<gl::DepthAndStencilBuffer> GetDepthBufferForSharing( 61 const MutexAutoLock& aProofOfLock, gl::GLContext* aGL, 62 const gfx::IntSize& aSize); 63 UniquePtr<gl::MozFramebuffer> CreateFramebufferForTexture( 64 const MutexAutoLock& aProofOfLock, gl::GLContext* aGL, 65 const gfx::IntSize& aSize, GLuint aTexture, bool aNeedsDepthBuffer); 66 67 Mutex mMutex MOZ_UNANNOTATED; 68 69 // Stores the entries for surfaces that are in use by NativeLayerWayland, i.e. 70 // an entry is inside mInUseEntries between calls to ObtainSurfaceFromPool() 71 // and ReturnSurfaceToPool(). 72 std::unordered_map<widget::WaylandBuffer*, SurfacePoolEntry> mInUseEntries; 73 74 // Stores entries which are no longer in use by NativeLayerWayland but are 75 // still in use by the window server, i.e. for which 76 // WaylandBuffer::IsAttached() still returns true. 77 // These entries are checked once per frame inside 78 // CollectPendingSurfaces(), and returned to mAvailableEntries once the 79 // window server is done. 80 nsTArray<SurfacePoolEntry> mPendingEntries; 81 82 // Stores entries which are available for recycling. These entries are not 83 // in use by a NativeLayerWayland or by the window server. 84 nsTArray<SurfacePoolEntry> mAvailableEntries; 85 size_t mPoolSizeLimit; 86 87 template <typename F> 88 void ForEachEntry(F aFn); 89 90 struct DepthBufferEntry final { 91 RefPtr<gl::GLContext> mGL; 92 gfx::IntSize mSize; 93 WeakPtr<gl::DepthAndStencilBuffer> mBuffer; 94 }; 95 96 nsTArray<DepthBufferEntry> mDepthBuffers; 97 }; 98 99 // A surface pool handle that is stored on NativeLayerWayland and keeps the 100 // SurfacePool alive. 101 class SurfacePoolHandleWayland final : public SurfacePoolHandle { 102 public: 103 SurfacePoolHandleWayland* AsSurfacePoolHandleWayland() override { 104 return this; 105 } 106 107 RefPtr<widget::WaylandBuffer> ObtainBufferFromPool( 108 const gfx::IntSize& aSize, RefPtr<widget::DRMFormat> aFormat); 109 void ReturnBufferToPool(const RefPtr<widget::WaylandBuffer>& aBuffer); 110 Maybe<GLuint> GetFramebufferForBuffer( 111 const RefPtr<widget::WaylandBuffer>& aBuffer, bool aNeedsDepthBuffer); 112 const auto& gl() { return mGL; } 113 114 RefPtr<SurfacePool> Pool() override { return mPool; } 115 void OnBeginFrame() override; 116 void OnEndFrame() override; 117 118 private: 119 friend class SurfacePoolWayland; 120 SurfacePoolHandleWayland(RefPtr<SurfacePoolWayland> aPool, 121 gl::GLContext* aGL); 122 123 const RefPtr<SurfacePoolWayland> mPool; 124 const RefPtr<gl::GLContext> mGL; 125 }; 126 127 } // namespace mozilla::layers 128 129 #endif