Bug 1931425 - Limit how often moz-label's #setStyles runs r=reusable-components-revie...
[gecko.git] / widget / gtk / DMABufLibWrapper.h
blob9ceb66801e6fc94e42445e9e72a7d581a733c934
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:expandtab:shiftwidth=2:tabstop=2:
3 */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #ifndef __MOZ_DMABUF_LIB_WRAPPER_H__
9 #define __MOZ_DMABUF_LIB_WRAPPER_H__
11 #include "mozilla/widget/gbm.h"
12 #include "mozilla/StaticMutex.h"
13 #include "mozilla/widget/DMABufFormats.h"
14 #include <mutex>
16 #ifdef MOZ_LOGGING
17 # include "mozilla/Logging.h"
18 # include "nsTArray.h"
19 # include "Units.h"
20 extern mozilla::LazyLogModule gDmabufLog;
21 # define LOGDMABUF(args) MOZ_LOG(gDmabufLog, mozilla::LogLevel::Debug, args)
22 #else
23 # define LOGDMABUF(args)
24 #endif /* MOZ_LOGGING */
26 #ifndef DRM_FORMAT_MOD_INVALID
27 # define DRM_FORMAT_MOD_INVALID ((1ULL << 56) - 1)
28 #endif
30 namespace mozilla {
31 namespace widget {
33 typedef struct gbm_device* (*CreateDeviceFunc)(int);
34 typedef void (*DestroyDeviceFunc)(struct gbm_device*);
35 typedef struct gbm_bo* (*CreateFunc)(struct gbm_device*, uint32_t, uint32_t,
36 uint32_t, uint32_t);
37 typedef struct gbm_bo* (*CreateWithModifiersFunc)(struct gbm_device*, uint32_t,
38 uint32_t, uint32_t,
39 const uint64_t*,
40 const unsigned int);
41 typedef struct gbm_bo* (*CreateWithModifiers2Func)(struct gbm_device*, uint32_t,
42 uint32_t, uint32_t,
43 const uint64_t*,
44 const unsigned int,
45 const uint32_t);
46 typedef uint64_t (*GetModifierFunc)(struct gbm_bo*);
47 typedef uint32_t (*GetStrideFunc)(struct gbm_bo*);
48 typedef int (*GetFdFunc)(struct gbm_bo*);
49 typedef void (*DestroyFunc)(struct gbm_bo*);
50 typedef void* (*MapFunc)(struct gbm_bo*, uint32_t, uint32_t, uint32_t, uint32_t,
51 uint32_t, uint32_t*, void**);
52 typedef void (*UnmapFunc)(struct gbm_bo*, void*);
53 typedef int (*GetPlaneCountFunc)(struct gbm_bo*);
54 typedef union gbm_bo_handle (*GetHandleForPlaneFunc)(struct gbm_bo*, int);
55 typedef uint32_t (*GetStrideForPlaneFunc)(struct gbm_bo*, int);
56 typedef uint32_t (*GetOffsetFunc)(struct gbm_bo*, int);
57 typedef int (*DeviceIsFormatSupportedFunc)(struct gbm_device*, uint32_t,
58 uint32_t);
59 typedef int (*DrmPrimeHandleToFDFunc)(int, uint32_t, uint32_t, int*);
60 typedef struct gbm_surface* (*CreateSurfaceFunc)(struct gbm_device*, uint32_t,
61 uint32_t, uint32_t, uint32_t);
62 typedef void (*DestroySurfaceFunc)(struct gbm_surface*);
64 class GbmLib {
65 public:
66 static bool IsAvailable() { return sLoaded || Load(); }
67 static bool IsModifierAvailable();
69 static struct gbm_device* CreateDevice(int fd) {
70 StaticMutexAutoLock lockDRI(sDRILock);
71 return sCreateDevice(fd);
73 static void DestroyDevice(struct gbm_device* gdm) {
74 StaticMutexAutoLock lockDRI(sDRILock);
75 return sDestroyDevice(gdm);
77 static struct gbm_bo* Create(struct gbm_device* gbm, uint32_t width,
78 uint32_t height, uint32_t format,
79 uint32_t flags) {
80 StaticMutexAutoLock lockDRI(sDRILock);
81 return sCreate(gbm, width, height, format, flags);
83 static void Destroy(struct gbm_bo* bo) {
84 StaticMutexAutoLock lockDRI(sDRILock);
85 sDestroy(bo);
87 static uint32_t GetStride(struct gbm_bo* bo) {
88 StaticMutexAutoLock lockDRI(sDRILock);
89 return sGetStride(bo);
91 static int GetFd(struct gbm_bo* bo) {
92 StaticMutexAutoLock lockDRI(sDRILock);
93 return sGetFd(bo);
95 static void* Map(struct gbm_bo* bo, uint32_t x, uint32_t y, uint32_t width,
96 uint32_t height, uint32_t flags, uint32_t* stride,
97 void** map_data) {
98 StaticMutexAutoLock lockDRI(sDRILock);
99 return sMap(bo, x, y, width, height, flags, stride, map_data);
101 static void Unmap(struct gbm_bo* bo, void* map_data) {
102 StaticMutexAutoLock lockDRI(sDRILock);
103 sUnmap(bo, map_data);
105 static struct gbm_bo* CreateWithModifiers(struct gbm_device* gbm,
106 uint32_t width, uint32_t height,
107 uint32_t format,
108 const uint64_t* modifiers,
109 const unsigned int count) {
110 StaticMutexAutoLock lockDRI(sDRILock);
111 return sCreateWithModifiers(gbm, width, height, format, modifiers, count);
113 static struct gbm_bo* CreateWithModifiers2(struct gbm_device* gbm,
114 uint32_t width, uint32_t height,
115 uint32_t format,
116 const uint64_t* modifiers,
117 const unsigned int count,
118 const uint32_t flags) {
119 StaticMutexAutoLock lockDRI(sDRILock);
120 if (!sCreateWithModifiers2) {
121 // CreateWithModifiers2 is optional and present in new GBM lib,
122 // if missing just fallback to old CreateWithModifiers as
123 // we want to anyway.
124 return sCreateWithModifiers(gbm, width, height, format, modifiers, count);
126 return sCreateWithModifiers2(gbm, width, height, format, modifiers, count,
127 flags);
129 static uint64_t GetModifier(struct gbm_bo* bo) {
130 StaticMutexAutoLock lockDRI(sDRILock);
131 return sGetModifier(bo);
133 static int GetPlaneCount(struct gbm_bo* bo) {
134 StaticMutexAutoLock lockDRI(sDRILock);
135 return sGetPlaneCount(bo);
137 static union gbm_bo_handle GetHandleForPlane(struct gbm_bo* bo, int plane) {
138 StaticMutexAutoLock lockDRI(sDRILock);
139 return sGetHandleForPlane(bo, plane);
141 static uint32_t GetStrideForPlane(struct gbm_bo* bo, int plane) {
142 StaticMutexAutoLock lockDRI(sDRILock);
143 return sGetStrideForPlane(bo, plane);
145 static uint32_t GetOffset(struct gbm_bo* bo, int plane) {
146 StaticMutexAutoLock lockDRI(sDRILock);
147 return sGetOffset(bo, plane);
149 static int DeviceIsFormatSupported(struct gbm_device* gbm, uint32_t format,
150 uint32_t usage) {
151 StaticMutexAutoLock lockDRI(sDRILock);
152 return sDeviceIsFormatSupported(gbm, format, usage);
154 static int DrmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags,
155 int* prime_fd) {
156 StaticMutexAutoLock lockDRI(sDRILock);
157 return sDrmPrimeHandleToFD(fd, handle, flags, prime_fd);
159 static struct gbm_surface* CreateSurface(struct gbm_device* gbm,
160 uint32_t width, uint32_t height,
161 uint32_t format, uint32_t flags) {
162 StaticMutexAutoLock lockDRI(sDRILock);
163 return sCreateSurface(gbm, width, height, format, flags);
165 static void DestroySurface(struct gbm_surface* surface) {
166 StaticMutexAutoLock lockDRI(sDRILock);
167 return sDestroySurface(surface);
170 private:
171 static bool Load();
172 static bool IsLoaded();
174 static CreateDeviceFunc sCreateDevice;
175 static DestroyDeviceFunc sDestroyDevice;
176 static CreateFunc sCreate;
177 static CreateWithModifiersFunc sCreateWithModifiers;
178 static CreateWithModifiers2Func sCreateWithModifiers2;
179 static GetModifierFunc sGetModifier;
180 static GetStrideFunc sGetStride;
181 static GetFdFunc sGetFd;
182 static DestroyFunc sDestroy;
183 static MapFunc sMap;
184 static UnmapFunc sUnmap;
185 static GetPlaneCountFunc sGetPlaneCount;
186 static GetHandleForPlaneFunc sGetHandleForPlane;
187 static GetStrideForPlaneFunc sGetStrideForPlane;
188 static GetOffsetFunc sGetOffset;
189 static DeviceIsFormatSupportedFunc sDeviceIsFormatSupported;
190 static DrmPrimeHandleToFDFunc sDrmPrimeHandleToFD;
191 static CreateSurfaceFunc sCreateSurface;
192 static DestroySurfaceFunc sDestroySurface;
193 static bool sLoaded;
195 static void* sGbmLibHandle;
196 static void* sXf86DrmLibHandle;
197 static mozilla::StaticMutex sDRILock MOZ_UNANNOTATED;
200 class DRMFormat;
202 class DMABufDevice {
203 public:
204 DMABufDevice();
205 ~DMABufDevice();
207 int OpenDRMFd();
208 gbm_device* GetGbmDevice();
209 int GetDmabufFD(uint32_t aGEMHandle);
211 bool IsEnabled(nsACString& aFailureId);
213 // Use dmabuf for WebRender general web content
214 static bool IsDMABufTexturesEnabled();
215 // Use dmabuf for WebGL content
216 static bool IsDMABufWebGLEnabled();
217 static void DisableDMABufWebGL();
219 RefPtr<DRMFormat> GetDRMFormat(int32_t aFOURCCFormat);
221 private:
222 void Configure();
224 #ifdef MOZ_WAYLAND
225 void LoadFormatModifiers();
226 void SetModifiersToGfxVars();
227 void GetModifiersFromGfxVars();
228 #endif
230 // Two basic formats, always present.
231 RefPtr<DRMFormat> mFormatRGBA;
232 RefPtr<DRMFormat> mFormatRGBX;
234 int mDRMFd = -1;
235 std::once_flag mFlagGbmDevice;
236 gbm_device* mGbmDevice = nullptr;
237 const char* mFailureId = nullptr;
238 nsAutoCString mDrmRenderNode;
241 DMABufDevice* GetDMABufDevice();
243 } // namespace widget
244 } // namespace mozilla
246 #endif // __MOZ_DMABUF_LIB_WRAPPER_H__