1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:expandtab:shiftwidth=2:tabstop=2:
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"
16 # include "mozilla/Logging.h"
17 # include "nsTArray.h"
19 extern mozilla::LazyLogModule gDmabufLog
;
20 # define LOGDMABUF(args) MOZ_LOG(gDmabufLog, mozilla::LogLevel::Debug, args)
22 # define LOGDMABUF(args)
23 #endif /* MOZ_LOGGING */
25 #ifndef DRM_FORMAT_MOD_INVALID
26 # define DRM_FORMAT_MOD_INVALID ((1ULL << 56) - 1)
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,
36 typedef struct gbm_bo
* (*CreateWithModifiersFunc
)(struct gbm_device
*, uint32_t,
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,
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
*);
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
,
74 StaticMutexAutoLock
lockDRI(sDRILock
);
75 return sCreate(gbm
, width
, height
, format
, flags
);
77 static void Destroy(struct gbm_bo
* bo
) {
78 StaticMutexAutoLock
lockDRI(sDRILock
);
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
);
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
,
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
);
99 static struct gbm_bo
* CreateWithModifiers(struct gbm_device
* gbm
,
100 uint32_t width
, uint32_t height
,
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
,
129 StaticMutexAutoLock
lockDRI(sDRILock
);
130 return sDeviceIsFormatSupported(gbm
, format
, usage
);
132 static int DrmPrimeHandleToFD(int fd
, uint32_t handle
, uint32_t flags
,
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
);
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
;
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
;
172 static void* sGbmLibHandle
;
173 static void* sXf86DrmLibHandle
;
174 static mozilla::StaticMutex sDRILock MOZ_UNANNOTATED
;
181 nsTArray
<uint64_t> mModifiers
;
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();
202 void AddFormatModifier(bool aHasAlpha
, int aFormat
, uint32_t mModifierHi
,
203 uint32_t mModifierLo
);
205 GbmFormat
* GetGbmFormat(bool aHasAlpha
);
210 void LoadFormatModifiers();
211 void SetModifiersToGfxVars();
212 void GetModifiersFromGfxVars();
216 GbmFormat mXRGBFormat
;
217 GbmFormat mARGBFormat
;
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__