Bug 1822331 [wpt PR 38990] - [@scope] Enable implicit :scope and relative selectors...
[gecko.git] / widget / gtk / DMABufLibWrapper.h
blobaa0a396aac753471da909b4e076feaa431cf2838
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"
14 #ifdef MOZ_LOGGING
15 # include "mozilla/Logging.h"
16 # include "nsTArray.h"
17 # include "Units.h"
18 extern mozilla::LazyLogModule gDmabufLog;
19 # define LOGDMABUF(args) MOZ_LOG(gDmabufLog, mozilla::LogLevel::Debug, args)
20 #else
21 # define LOGDMABUF(args)
22 #endif /* MOZ_LOGGING */
24 #ifndef DRM_FORMAT_MOD_INVALID
25 # define DRM_FORMAT_MOD_INVALID ((1ULL << 56) - 1)
26 #endif
28 namespace mozilla {
29 namespace widget {
31 typedef struct gbm_device* (*CreateDeviceFunc)(int);
32 typedef void (*DestroyDeviceFunc)(struct gbm_device*);
33 typedef struct gbm_bo* (*CreateFunc)(struct gbm_device*, uint32_t, uint32_t,
34 uint32_t, uint32_t);
35 typedef struct gbm_bo* (*CreateWithModifiersFunc)(struct gbm_device*, uint32_t,
36 uint32_t, uint32_t,
37 const uint64_t*,
38 const unsigned int);
39 typedef uint64_t (*GetModifierFunc)(struct gbm_bo*);
40 typedef uint32_t (*GetStrideFunc)(struct gbm_bo*);
41 typedef int (*GetFdFunc)(struct gbm_bo*);
42 typedef void (*DestroyFunc)(struct gbm_bo*);
43 typedef void* (*MapFunc)(struct gbm_bo*, uint32_t, uint32_t, uint32_t, uint32_t,
44 uint32_t, uint32_t*, void**);
45 typedef void (*UnmapFunc)(struct gbm_bo*, void*);
46 typedef int (*GetPlaneCountFunc)(struct gbm_bo*);
47 typedef union gbm_bo_handle (*GetHandleForPlaneFunc)(struct gbm_bo*, int);
48 typedef uint32_t (*GetStrideForPlaneFunc)(struct gbm_bo*, int);
49 typedef uint32_t (*GetOffsetFunc)(struct gbm_bo*, int);
50 typedef int (*DeviceIsFormatSupportedFunc)(struct gbm_device*, uint32_t,
51 uint32_t);
52 typedef int (*DrmPrimeHandleToFDFunc)(int, uint32_t, uint32_t, int*);
53 typedef struct gbm_surface* (*CreateSurfaceFunc)(struct gbm_device*, uint32_t,
54 uint32_t, uint32_t, uint32_t);
55 typedef void (*DestroySurfaceFunc)(struct gbm_surface*);
57 class nsGbmLib {
58 public:
59 static bool IsAvailable() { return sLoaded || Load(); }
60 static bool IsModifierAvailable();
62 static struct gbm_device* CreateDevice(int fd) {
63 StaticMutexAutoLock lockDRI(sDRILock);
64 return sCreateDevice(fd);
66 static void DestroyDevice(struct gbm_device* gdm) {
67 StaticMutexAutoLock lockDRI(sDRILock);
68 return sDestroyDevice(gdm);
70 static struct gbm_bo* Create(struct gbm_device* gbm, uint32_t width,
71 uint32_t height, uint32_t format,
72 uint32_t flags) {
73 StaticMutexAutoLock lockDRI(sDRILock);
74 return sCreate(gbm, width, height, format, flags);
76 static void Destroy(struct gbm_bo* bo) {
77 StaticMutexAutoLock lockDRI(sDRILock);
78 sDestroy(bo);
80 static uint32_t GetStride(struct gbm_bo* bo) {
81 StaticMutexAutoLock lockDRI(sDRILock);
82 return sGetStride(bo);
84 static int GetFd(struct gbm_bo* bo) {
85 StaticMutexAutoLock lockDRI(sDRILock);
86 return sGetFd(bo);
88 static void* Map(struct gbm_bo* bo, uint32_t x, uint32_t y, uint32_t width,
89 uint32_t height, uint32_t flags, uint32_t* stride,
90 void** map_data) {
91 StaticMutexAutoLock lockDRI(sDRILock);
92 return sMap(bo, x, y, width, height, flags, stride, map_data);
94 static void Unmap(struct gbm_bo* bo, void* map_data) {
95 StaticMutexAutoLock lockDRI(sDRILock);
96 sUnmap(bo, map_data);
98 static struct gbm_bo* CreateWithModifiers(struct gbm_device* gbm,
99 uint32_t width, uint32_t height,
100 uint32_t format,
101 const uint64_t* modifiers,
102 const unsigned int count) {
103 StaticMutexAutoLock lockDRI(sDRILock);
104 return sCreateWithModifiers(gbm, width, height, format, modifiers, count);
106 static uint64_t GetModifier(struct gbm_bo* bo) {
107 StaticMutexAutoLock lockDRI(sDRILock);
108 return sGetModifier(bo);
110 static int GetPlaneCount(struct gbm_bo* bo) {
111 StaticMutexAutoLock lockDRI(sDRILock);
112 return sGetPlaneCount(bo);
114 static union gbm_bo_handle GetHandleForPlane(struct gbm_bo* bo, int plane) {
115 StaticMutexAutoLock lockDRI(sDRILock);
116 return sGetHandleForPlane(bo, plane);
118 static uint32_t GetStrideForPlane(struct gbm_bo* bo, int plane) {
119 StaticMutexAutoLock lockDRI(sDRILock);
120 return sGetStrideForPlane(bo, plane);
122 static uint32_t GetOffset(struct gbm_bo* bo, int plane) {
123 StaticMutexAutoLock lockDRI(sDRILock);
124 return sGetOffset(bo, plane);
126 static int DeviceIsFormatSupported(struct gbm_device* gbm, uint32_t format,
127 uint32_t usage) {
128 StaticMutexAutoLock lockDRI(sDRILock);
129 return sDeviceIsFormatSupported(gbm, format, usage);
131 static int DrmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags,
132 int* prime_fd) {
133 StaticMutexAutoLock lockDRI(sDRILock);
134 return sDrmPrimeHandleToFD(fd, handle, flags, prime_fd);
136 static struct gbm_surface* CreateSurface(struct gbm_device* gbm,
137 uint32_t width, uint32_t height,
138 uint32_t format, uint32_t flags) {
139 StaticMutexAutoLock lockDRI(sDRILock);
140 return sCreateSurface(gbm, width, height, format, flags);
142 static void DestroySurface(struct gbm_surface* surface) {
143 StaticMutexAutoLock lockDRI(sDRILock);
144 return sDestroySurface(surface);
147 private:
148 static bool Load();
149 static bool IsLoaded();
151 static CreateDeviceFunc sCreateDevice;
152 static DestroyDeviceFunc sDestroyDevice;
153 static CreateFunc sCreate;
154 static CreateWithModifiersFunc sCreateWithModifiers;
155 static GetModifierFunc sGetModifier;
156 static GetStrideFunc sGetStride;
157 static GetFdFunc sGetFd;
158 static DestroyFunc sDestroy;
159 static MapFunc sMap;
160 static UnmapFunc sUnmap;
161 static GetPlaneCountFunc sGetPlaneCount;
162 static GetHandleForPlaneFunc sGetHandleForPlane;
163 static GetStrideForPlaneFunc sGetStrideForPlane;
164 static GetOffsetFunc sGetOffset;
165 static DeviceIsFormatSupportedFunc sDeviceIsFormatSupported;
166 static DrmPrimeHandleToFDFunc sDrmPrimeHandleToFD;
167 static CreateSurfaceFunc sCreateSurface;
168 static DestroySurfaceFunc sDestroySurface;
169 static bool sLoaded;
171 static void* sGbmLibHandle;
172 static void* sXf86DrmLibHandle;
173 static mozilla::StaticMutex sDRILock MOZ_UNANNOTATED;
176 struct GbmFormat {
177 bool mIsSupported;
178 bool mHasAlpha;
179 int mFormat;
180 uint64_t* mModifiers;
181 int mModifiersCount;
184 class nsDMABufDevice {
185 public:
186 nsDMABufDevice();
187 ~nsDMABufDevice();
189 gbm_device* GetGbmDevice();
191 // Use dmabuf for WebRender general web content
192 bool IsDMABufTexturesEnabled();
193 // Use dmabuf for WebGL content
194 bool IsDMABufWebGLEnabled();
195 void DisableDMABufWebGL();
197 int GetDRMFd();
198 GbmFormat* GetGbmFormat(bool aHasAlpha);
199 GbmFormat* GetExactGbmFormat(int aFormat);
200 void ResetFormatsModifiers();
201 void AddFormatModifier(bool aHasAlpha, int aFormat, uint32_t mModifierHi,
202 uint32_t mModifierLo);
203 bool Configure(nsACString& aFailureId);
205 private:
206 bool mUseWebGLDmabufBackend;
208 private:
209 GbmFormat mXRGBFormat;
210 GbmFormat mARGBFormat;
212 int mDRMFd;
213 gbm_device* mGbmDevice;
214 bool mInitialized;
217 nsDMABufDevice* GetDMABufDevice();
218 nsDMABufDevice* GetAndConfigureDMABufDevice();
220 } // namespace widget
221 } // namespace mozilla
223 #endif // __MOZ_DMABUF_LIB_WRAPPER_H__