1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:expandtab:shiftwidth=4:tabstop=4:
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"
15 # include "mozilla/Logging.h"
16 # include "nsTArray.h"
18 extern mozilla::LazyLogModule gDmabufLog
;
19 # define LOGDMABUF(args) MOZ_LOG(gDmabufLog, mozilla::LogLevel::Debug, args)
21 # define LOGDMABUF(args)
22 #endif /* MOZ_LOGGING */
24 #ifndef DRM_FORMAT_MOD_INVALID
25 # define DRM_FORMAT_MOD_INVALID ((1ULL << 56) - 1)
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,
35 typedef struct gbm_bo
* (*CreateWithModifiersFunc
)(struct gbm_device
*, uint32_t,
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,
52 typedef int (*DrmPrimeHandleToFDFunc
)(int, uint32_t, uint32_t, int*);
57 static bool IsLoaded();
58 static bool IsAvailable();
59 static bool IsModifierAvailable();
61 static struct gbm_device
* CreateDevice(int fd
) {
62 StaticMutexAutoLock
lockDRI(sDRILock
);
63 return sCreateDevice(fd
);
65 static void DestroyDevice(struct gbm_device
* gdm
) {
66 StaticMutexAutoLock
lockDRI(sDRILock
);
67 return sDestroyDevice(gdm
);
69 static struct gbm_bo
* Create(struct gbm_device
* gbm
, uint32_t width
,
70 uint32_t height
, uint32_t format
,
72 StaticMutexAutoLock
lockDRI(sDRILock
);
73 return sCreate(gbm
, width
, height
, format
, flags
);
75 static void Destroy(struct gbm_bo
* bo
) {
76 StaticMutexAutoLock
lockDRI(sDRILock
);
79 static uint32_t GetStride(struct gbm_bo
* bo
) {
80 StaticMutexAutoLock
lockDRI(sDRILock
);
81 return sGetStride(bo
);
83 static int GetFd(struct gbm_bo
* bo
) {
84 StaticMutexAutoLock
lockDRI(sDRILock
);
87 static void* Map(struct gbm_bo
* bo
, uint32_t x
, uint32_t y
, uint32_t width
,
88 uint32_t height
, uint32_t flags
, uint32_t* stride
,
90 StaticMutexAutoLock
lockDRI(sDRILock
);
91 return sMap(bo
, x
, y
, width
, height
, flags
, stride
, map_data
);
93 static void Unmap(struct gbm_bo
* bo
, void* map_data
) {
94 StaticMutexAutoLock
lockDRI(sDRILock
);
97 static struct gbm_bo
* CreateWithModifiers(struct gbm_device
* gbm
,
98 uint32_t width
, uint32_t height
,
100 const uint64_t* modifiers
,
101 const unsigned int count
) {
102 StaticMutexAutoLock
lockDRI(sDRILock
);
103 return sCreateWithModifiers(gbm
, width
, height
, format
, modifiers
, count
);
105 static uint64_t GetModifier(struct gbm_bo
* bo
) {
106 StaticMutexAutoLock
lockDRI(sDRILock
);
107 return sGetModifier(bo
);
109 static int GetPlaneCount(struct gbm_bo
* bo
) {
110 StaticMutexAutoLock
lockDRI(sDRILock
);
111 return sGetPlaneCount(bo
);
113 static union gbm_bo_handle
GetHandleForPlane(struct gbm_bo
* bo
, int plane
) {
114 StaticMutexAutoLock
lockDRI(sDRILock
);
115 return sGetHandleForPlane(bo
, plane
);
117 static uint32_t GetStrideForPlane(struct gbm_bo
* bo
, int plane
) {
118 StaticMutexAutoLock
lockDRI(sDRILock
);
119 return sGetStrideForPlane(bo
, plane
);
121 static uint32_t GetOffset(struct gbm_bo
* bo
, int plane
) {
122 StaticMutexAutoLock
lockDRI(sDRILock
);
123 return sGetOffset(bo
, plane
);
125 static int DeviceIsFormatSupported(struct gbm_device
* gbm
, uint32_t format
,
127 StaticMutexAutoLock
lockDRI(sDRILock
);
128 return sDeviceIsFormatSupported(gbm
, format
, usage
);
130 static int DrmPrimeHandleToFD(int fd
, uint32_t handle
, uint32_t flags
,
132 StaticMutexAutoLock
lockDRI(sDRILock
);
133 return sDrmPrimeHandleToFD(fd
, handle
, flags
, prime_fd
);
137 static CreateDeviceFunc sCreateDevice
;
138 static DestroyDeviceFunc sDestroyDevice
;
139 static CreateFunc sCreate
;
140 static CreateWithModifiersFunc sCreateWithModifiers
;
141 static GetModifierFunc sGetModifier
;
142 static GetStrideFunc sGetStride
;
143 static GetFdFunc sGetFd
;
144 static DestroyFunc sDestroy
;
146 static UnmapFunc sUnmap
;
147 static GetPlaneCountFunc sGetPlaneCount
;
148 static GetHandleForPlaneFunc sGetHandleForPlane
;
149 static GetStrideForPlaneFunc sGetStrideForPlane
;
150 static GetOffsetFunc sGetOffset
;
151 static DeviceIsFormatSupportedFunc sDeviceIsFormatSupported
;
152 static DrmPrimeHandleToFDFunc sDrmPrimeHandleToFD
;
154 static void* sGbmLibHandle
;
155 static void* sXf86DrmLibHandle
;
156 static mozilla::StaticMutex sDRILock MOZ_UNANNOTATED
;
157 static bool sLibLoaded
;
164 uint64_t* mModifiers
;
168 class nsDMABufDevice
{
173 gbm_device
* GetGbmDevice();
175 // Use dmabuf for WebRender general web content
176 bool IsDMABufTexturesEnabled();
177 // Use dmabuf for VA-API video playback
178 bool IsDMABufVAAPIEnabled();
179 // Use dmabuf for WebGL content
180 bool IsDMABufWebGLEnabled();
181 void DisableDMABufWebGL();
184 GbmFormat
* GetGbmFormat(bool aHasAlpha
);
185 GbmFormat
* GetExactGbmFormat(int aFormat
);
186 void ResetFormatsModifiers();
187 void AddFormatModifier(bool aHasAlpha
, int aFormat
, uint32_t mModifierHi
,
188 uint32_t mModifierLo
);
189 bool Configure(nsACString
& aFailureId
);
192 bool mUseWebGLDmabufBackend
;
195 bool IsDMABufEnabled();
197 GbmFormat mXRGBFormat
;
198 GbmFormat mARGBFormat
;
201 gbm_device
* mGbmDevice
;
205 nsDMABufDevice
* GetDMABufDevice();
207 } // namespace widget
208 } // namespace mozilla
210 #endif // __MOZ_DMABUF_LIB_WRAPPER_H__