1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2023 Roman Stratiienko ([email protected])
5 * SPDX-License-Identifier: MIT
6 */
7
8 #include "u_gralloc_libdrm.h"
9
10 #include <assert.h>
11 #include <dlfcn.h>
12 #include <errno.h>
13 #include <string.h>
14 #include <hardware/gralloc.h>
15
16 #include "util/log.h"
17 #include "util/u_memory.h"
18
19 #include "u_gralloc_internal.h"
20
21 /* Despite minigbm becoming standard gralloc for many distributions, some
22 * users still rely on legacy grallocs, like gbm_gralloc that use a native
23 * handle header that is located at libdrm/android/gralloc_handle.h.
24 * Using this gralloc is not recommended for new distributions.
25 */
26
27 struct libdrm_gralloc {
28 struct u_gralloc base;
29 gralloc_module_t *gralloc_module;
30 struct u_gralloc *fallback_gralloc;
31 };
32
33 static const char gbm_gralloc_module_name[] = "GBM Memory Allocator";
34
35 static int
get_buffer_info(struct u_gralloc * gralloc,struct u_gralloc_buffer_handle * hnd,struct u_gralloc_buffer_basic_info * out)36 get_buffer_info(struct u_gralloc *gralloc,
37 struct u_gralloc_buffer_handle *hnd,
38 struct u_gralloc_buffer_basic_info *out)
39 {
40 struct libdrm_gralloc *gr = (struct libdrm_gralloc *)gralloc;
41 struct gralloc_handle_t *handle = (struct gralloc_handle_t *)hnd->handle;
42 assert(handle->base.numFds == GRALLOC_HANDLE_NUM_FDS);
43 assert(handle->base.numInts == GRALLOC_HANDLE_NUM_INTS);
44 assert(handle->magic == GRALLOC_HANDLE_MAGIC);
45
46 if (handle->version != GRALLOC_HANDLE_VERSION)
47 mesa_loge("Unexpected gralloc handle version %d", handle->version);
48
49 assert(handle->version == GRALLOC_HANDLE_VERSION);
50
51 /* Query basic information using fallback gralloc */
52 u_gralloc_get_buffer_basic_info(gr->fallback_gralloc, hnd, out);
53
54 /* Fill the known data using libdrm gralloc handle */
55 out->modifier = handle->modifier;
56 out->strides[0] = handle->stride;
57
58 return 0;
59 }
60
61 static int
destroy(struct u_gralloc * gralloc)62 destroy(struct u_gralloc *gralloc)
63 {
64 struct libdrm_gralloc *gr = (struct libdrm_gralloc *)gralloc;
65 if (gr->gralloc_module)
66 dlclose(gr->gralloc_module->common.dso);
67
68 if (gr->fallback_gralloc)
69 gr->fallback_gralloc->ops.destroy(gr->fallback_gralloc);
70
71 FREE(gr);
72
73 return 0;
74 }
75
76 struct u_gralloc *
u_gralloc_libdrm_create()77 u_gralloc_libdrm_create()
78 {
79 struct libdrm_gralloc *gr = CALLOC_STRUCT(libdrm_gralloc);
80 int err = 0;
81
82 err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
83 (const hw_module_t **)&gr->gralloc_module);
84
85 if (err)
86 goto fail;
87
88 if (strcmp(gr->gralloc_module->common.name, gbm_gralloc_module_name) != 0)
89 goto fail;
90
91 gr->base.ops.get_buffer_basic_info = get_buffer_info;
92 gr->base.ops.destroy = destroy;
93
94 mesa_logw("Using gralloc header from libdrm/android/gralloc_handle.h. "
95 " This is not recommended for new distributions. "
96 " Initializing a fallback gralloc as a helper:");
97
98 gr->fallback_gralloc = u_gralloc_fallback_create();
99
100 return &gr->base;
101
102 fail:
103 destroy(&gr->base);
104
105 return NULL;
106 }
107