no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / widget / gtk / DMABufLibWrapper.h
blob9723083ad8d72d45ff108f03e303216eba65cef1
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 <mutex>
15 #ifdef MOZ_LOGGING
16 # include "mozilla/Logging.h"
17 # include "nsTArray.h"
18 # include "Units.h"
19 extern mozilla::LazyLogModule gDmabufLog;
20 # define LOGDMABUF(args) MOZ_LOG(gDmabufLog, mozilla::LogLevel::Debug, args)
21 #else
22 # define LOGDMABUF(args)
23 #endif /* MOZ_LOGGING */
25 #ifndef DRM_FORMAT_MOD_INVALID
26 # define DRM_FORMAT_MOD_INVALID ((1ULL << 56) - 1)
27 #endif
29 namespace mozilla {
30 namespace widget {
32 typedef struct gbm_device* (*CreateDeviceFunc)(int);
33 typedef void (*DestroyDeviceFunc)(struct gbm_device*);
34 typedef struct gbm_bo* (*CreateFunc)(struct gbm_device*, uint32_t, uint32_t,
35 uint32_t, uint32_t);
36 typedef struct gbm_bo* (*CreateWithModifiersFunc)(struct gbm_device*, uint32_t,
37 uint32_t, uint32_t,
38 const uint64_t*,
39 const unsigned int);
40 typedef uint64_t (*GetModifierFunc)(struct gbm_bo*);
41 typedef uint32_t (*GetStrideFunc)(struct gbm_bo*);
42 typedef int (*GetFdFunc)(struct gbm_bo*);
43 typedef void (*DestroyFunc)(struct gbm_bo*);
44 typedef void* (*MapFunc)(struct gbm_bo*, uint32_t, uint32_t, uint32_t, uint32_t,
45 uint32_t, uint32_t*, void**);
46 typedef void (*UnmapFunc)(struct gbm_bo*, void*);
47 typedef int (*GetPlaneCountFunc)(struct gbm_bo*);
48 typedef union gbm_bo_handle (*GetHandleForPlaneFunc)(struct gbm_bo*, int);
49 typedef uint32_t (*GetStrideForPlaneFunc)(struct gbm_bo*, int);
50 typedef uint32_t (*GetOffsetFunc)(struct gbm_bo*, int);
51 typedef int (*DeviceIsFormatSupportedFunc)(struct gbm_device*, uint32_t,
52 uint32_t);
53 typedef int (*DrmPrimeHandleToFDFunc)(int, uint32_t, uint32_t, int*);
54 typedef struct gbm_surface* (*CreateSurfaceFunc)(struct gbm_device*, uint32_t,
55 uint32_t, uint32_t, uint32_t);
56 typedef void (*DestroySurfaceFunc)(struct gbm_surface*);
58 class GbmLib {
59 public:
60 static bool IsAvailable() { return sLoaded || Load(); }
61 static bool IsModifierAvailable();
63 static struct gbm_device* CreateDevice(int fd) {
64 StaticMutexAutoLock lockDRI(sDRILock);
65 return sCreateDevice(fd);
67 static void DestroyDevice(struct gbm_device* gdm) {
68 StaticMutexAutoLock lockDRI(sDRILock);
69 return sDestroyDevice(gdm);
71 static struct gbm_bo* Create(struct gbm_device* gbm, uint32_t width,
72 uint32_t height, uint32_t format,
73 uint32_t flags) {
74 StaticMutexAutoLock lockDRI(sDRILock);
75 return sCreate(gbm, width, height, format, flags);
77 static void Destroy(struct gbm_bo* bo) {
78 StaticMutexAutoLock lockDRI(sDRILock);
79 sDestroy(bo);
81 static uint32_t GetStride(struct gbm_bo* bo) {
82 StaticMutexAutoLock lockDRI(sDRILock);
83 return sGetStride(bo);
85 static int GetFd(struct gbm_bo* bo) {
86 StaticMutexAutoLock lockDRI(sDRILock);
87 return sGetFd(bo);
89 static void* Map(struct gbm_bo* bo, uint32_t x, uint32_t y, uint32_t width,
90 uint32_t height, uint32_t flags, uint32_t* stride,
91 void** map_data) {
92 StaticMutexAutoLock lockDRI(sDRILock);
93 return sMap(bo, x, y, width, height, flags, stride, map_data);
95 static void Unmap(struct gbm_bo* bo, void* map_data) {
96 StaticMutexAutoLock lockDRI(sDRILock);
97 sUnmap(bo, map_data);
99 static struct gbm_bo* CreateWithModifiers(struct gbm_device* gbm,
100 uint32_t width, uint32_t height,
101 uint32_t format,
102 const uint64_t* modifiers,
103 const unsigned int count) {
104 StaticMutexAutoLock lockDRI(sDRILock);
105 return sCreateWithModifiers(gbm, width, height, format, modifiers, count);
107 static uint64_t GetModifier(struct gbm_bo* bo) {
108 StaticMutexAutoLock lockDRI(sDRILock);
109 return sGetModifier(bo);
111 static int GetPlaneCount(struct gbm_bo* bo) {
112 StaticMutexAutoLock lockDRI(sDRILock);
113 return sGetPlaneCount(bo);
115 static union gbm_bo_handle GetHandleForPlane(struct gbm_bo* bo, int plane) {
116 StaticMutexAutoLock lockDRI(sDRILock);
117 return sGetHandleForPlane(bo, plane);
119 static uint32_t GetStrideForPlane(struct gbm_bo* bo, int plane) {
120 StaticMutexAutoLock lockDRI(sDRILock);
121 return sGetStrideForPlane(bo, plane);
123 static uint32_t GetOffset(struct gbm_bo* bo, int plane) {
124 StaticMutexAutoLock lockDRI(sDRILock);
125 return sGetOffset(bo, plane);
127 static int DeviceIsFormatSupported(struct gbm_device* gbm, uint32_t format,
128 uint32_t usage) {
129 StaticMutexAutoLock lockDRI(sDRILock);
130 return sDeviceIsFormatSupported(gbm, format, usage);
132 static int DrmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags,
133 int* prime_fd) {
134 StaticMutexAutoLock lockDRI(sDRILock);
135 return sDrmPrimeHandleToFD(fd, handle, flags, prime_fd);
137 static struct gbm_surface* CreateSurface(struct gbm_device* gbm,
138 uint32_t width, uint32_t height,
139 uint32_t format, uint32_t flags) {
140 StaticMutexAutoLock lockDRI(sDRILock);
141 return sCreateSurface(gbm, width, height, format, flags);
143 static void DestroySurface(struct gbm_surface* surface) {
144 StaticMutexAutoLock lockDRI(sDRILock);
145 return sDestroySurface(surface);
148 private:
149 static bool Load();
150 static bool IsLoaded();
152 static CreateDeviceFunc sCreateDevice;
153 static DestroyDeviceFunc sDestroyDevice;
154 static CreateFunc sCreate;
155 static CreateWithModifiersFunc sCreateWithModifiers;
156 static GetModifierFunc sGetModifier;
157 static GetStrideFunc sGetStride;
158 static GetFdFunc sGetFd;
159 static DestroyFunc sDestroy;
160 static MapFunc sMap;
161 static UnmapFunc sUnmap;
162 static GetPlaneCountFunc sGetPlaneCount;
163 static GetHandleForPlaneFunc sGetHandleForPlane;
164 static GetStrideForPlaneFunc sGetStrideForPlane;
165 static GetOffsetFunc sGetOffset;
166 static DeviceIsFormatSupportedFunc sDeviceIsFormatSupported;
167 static DrmPrimeHandleToFDFunc sDrmPrimeHandleToFD;
168 static CreateSurfaceFunc sCreateSurface;
169 static DestroySurfaceFunc sDestroySurface;
170 static bool sLoaded;
172 static void* sGbmLibHandle;
173 static void* sXf86DrmLibHandle;
174 static mozilla::StaticMutex sDRILock MOZ_UNANNOTATED;
177 struct GbmFormat {
178 bool mIsSupported;
179 bool mHasAlpha;
180 int mFormat;
181 nsTArray<uint64_t> mModifiers;
184 class DMABufDevice {
185 public:
186 DMABufDevice();
187 ~DMABufDevice();
189 int OpenDRMFd();
190 gbm_device* GetGbmDevice();
191 int GetDmabufFD(uint32_t aGEMHandle);
193 bool IsEnabled(nsACString& aFailureId);
195 // Use dmabuf for WebRender general web content
196 static bool IsDMABufTexturesEnabled();
197 // Use dmabuf for WebGL content
198 static bool IsDMABufWebGLEnabled();
199 static void DisableDMABufWebGL();
201 #ifdef MOZ_WAYLAND
202 void AddFormatModifier(bool aHasAlpha, int aFormat, uint32_t mModifierHi,
203 uint32_t mModifierLo);
204 #endif
205 GbmFormat* GetGbmFormat(bool aHasAlpha);
207 private:
208 void Configure();
209 #ifdef MOZ_WAYLAND
210 void LoadFormatModifiers();
211 void SetModifiersToGfxVars();
212 void GetModifiersFromGfxVars();
213 #endif
215 private:
216 GbmFormat mXRGBFormat;
217 GbmFormat mARGBFormat;
219 int mDRMFd = -1;
220 std::once_flag mFlagGbmDevice;
221 gbm_device* mGbmDevice = nullptr;
222 const char* mFailureId = nullptr;
223 nsAutoCString mDrmRenderNode;
226 DMABufDevice* GetDMABufDevice();
228 } // namespace widget
229 } // namespace mozilla
231 #endif // __MOZ_DMABUF_LIB_WRAPPER_H__