1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef _MOZILLA_WIDGET_GTK_WAYLAND_BUFFER_H
8 #define _MOZILLA_WIDGET_GTK_WAYLAND_BUFFER_H
10 #include "DMABufSurface.h"
11 #include "GLContext.h"
12 #include "MozFramebuffer.h"
13 #include "mozilla/gfx/2D.h"
14 #include "mozilla/gfx/Types.h"
15 #include "mozilla/Mutex.h"
17 #include "nsWaylandDisplay.h"
18 #include "base/shared_memory.h"
20 namespace mozilla::widget
{
22 // Allocates and owns shared memory for Wayland drawing surface
23 class WaylandShmPool
{
25 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(WaylandShmPool
);
27 static RefPtr
<WaylandShmPool
> Create(nsWaylandDisplay
* aWaylandDisplay
,
30 wl_shm_pool
* GetShmPool() { return mShmPool
; };
34 WaylandShmPool() = default;
37 wl_shm_pool
* mShmPool
= nullptr;
38 void* mImageData
= nullptr;
39 UniquePtr
<base::SharedMemory
> mShm
;
45 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(WaylandBuffer
);
47 void AttachAndCommit(wl_surface
* aSurface
);
48 virtual wl_buffer
* GetWlBuffer() = 0;
49 virtual already_AddRefed
<gfx::DrawTarget
> Lock() { return nullptr; };
50 virtual void* GetImageData() { return nullptr; }
51 virtual GLuint
GetTexture() { return 0; }
52 virtual void DestroyGLResources(){};
54 LayoutDeviceIntSize
GetSize() { return mSize
; };
55 bool IsMatchingSize(const LayoutDeviceIntSize
& aSize
) {
56 return aSize
== mSize
;
58 bool IsAttached() { return mAttached
; }
59 static gfx::SurfaceFormat
GetSurfaceFormat() { return mFormat
; }
61 static void BufferReleaseCallbackHandler(void* aData
, wl_buffer
* aBuffer
);
62 void SetBufferReleaseFunc(void (*aBufferReleaseFunc
)(void* aData
,
63 wl_buffer
* aBuffer
)) {
64 mBufferReleaseFunc
= aBufferReleaseFunc
;
66 void SetBufferReleaseData(void* aBufferReleaseData
) {
67 mBufferReleaseData
= aBufferReleaseData
;
71 explicit WaylandBuffer(const LayoutDeviceIntSize
& aSize
);
72 virtual ~WaylandBuffer() = default;
74 void BufferReleaseCallbackHandler(wl_buffer
* aBuffer
);
76 LayoutDeviceIntSize mSize
;
77 bool mAttached
= false;
78 void (*mBufferReleaseFunc
)(void* aData
, wl_buffer
* aBuffer
) = nullptr;
79 void* mBufferReleaseData
= nullptr;
80 static gfx::SurfaceFormat mFormat
;
83 // Holds actual graphics data for wl_surface
84 class WaylandBufferSHM final
: public WaylandBuffer
{
86 static RefPtr
<WaylandBufferSHM
> Create(const LayoutDeviceIntSize
& aSize
);
88 wl_buffer
* GetWlBuffer() override
{ return mWLBuffer
; };
89 already_AddRefed
<gfx::DrawTarget
> Lock() override
;
90 void* GetImageData() override
{ return mShmPool
->GetImageData(); }
93 size_t GetBufferAge() { return mBufferAge
; };
94 RefPtr
<WaylandShmPool
> GetShmPool() { return mShmPool
; }
96 void IncrementBufferAge() { mBufferAge
++; };
97 void ResetBufferAge() { mBufferAge
= 0; };
100 void DumpToFile(const char* aHint
);
104 explicit WaylandBufferSHM(const LayoutDeviceIntSize
& aSize
);
105 ~WaylandBufferSHM() override
;
107 // WaylandShmPoolMB provides actual shared memory we draw into
108 RefPtr
<WaylandShmPool
> mShmPool
;
110 // wl_buffer is a wayland object that encapsulates the shared memory
111 // and passes it to wayland compositor by wl_surface object.
112 wl_buffer
* mWLBuffer
= nullptr;
114 size_t mBufferAge
= 0;
117 static int mDumpSerial
;
118 static char* mDumpDir
;
122 class WaylandBufferDMABUF final
: public WaylandBuffer
{
124 static RefPtr
<WaylandBufferDMABUF
> Create(const LayoutDeviceIntSize
& aSize
,
127 wl_buffer
* GetWlBuffer() override
{ return mDMABufSurface
->GetWlBuffer(); };
128 GLuint
GetTexture() override
{ return mDMABufSurface
->GetTexture(); };
129 void DestroyGLResources() override
{ mDMABufSurface
->ReleaseTextures(); };
132 explicit WaylandBufferDMABUF(const LayoutDeviceIntSize
& aSize
);
133 ~WaylandBufferDMABUF() = default;
135 RefPtr
<DMABufSurfaceRGBA
> mDMABufSurface
;
138 } // namespace mozilla::widget
140 #endif // _MOZILLA_WIDGET_GTK_WAYLAND_BUFFER_H