mozdrm.cpp (1744B)
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim:expandtab:shiftwidth=4:tabstop=4: 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/. */ 7 8 #include "prlink.h" 9 10 #include <xf86drm.h> 11 12 #define GET_FUNC(func, lib) \ 13 func##_fn = \ 14 (decltype(func##_fn))PR_FindFunctionSymbol(lib, #func) \ 15 16 #define IS_FUNC_LOADED(func) \ 17 (func != nullptr) \ 18 19 static int (*drmGetDevices2_fn)(uint32_t flags, drmDevicePtr devices[], int max_devices); 20 static void (*drmFreeDevices_fn)(drmDevicePtr devices[], int count); 21 22 bool IsDRMLibraryLoaded() { 23 static bool isLoaded = 24 (IS_FUNC_LOADED(drmGetDevices2_fn) && 25 IS_FUNC_LOADED(drmFreeDevices_fn)); 26 27 return isLoaded; 28 } 29 30 bool LoadDRMLibrary() { 31 static PRLibrary* drmLib = nullptr; 32 static bool drmInitialized = false; 33 34 //TODO Thread safe 35 if (!drmInitialized) { 36 drmInitialized = true; 37 drmLib = PR_LoadLibrary("libdrm.so.2"); 38 if (!drmLib) { 39 return false; 40 } 41 42 GET_FUNC(drmGetDevices2, drmLib); 43 GET_FUNC(drmFreeDevices, drmLib); 44 } 45 46 return IsDRMLibraryLoaded(); 47 } 48 49 int 50 drmGetDevices2(uint32_t flags, drmDevicePtr devices[], int max_devices) 51 { 52 if (!LoadDRMLibrary()) { 53 return 0; 54 } 55 return drmGetDevices2_fn(flags, devices, max_devices); 56 } 57 58 void 59 drmFreeDevices(drmDevicePtr devices[], int count) 60 { 61 if (!LoadDRMLibrary()) { 62 return; 63 } 64 return drmFreeDevices_fn(devices, count); 65 }