split hal_backend_tbm_funcs
[platform/hal/api/tbm.git] / src / hal-api-tbm.c
1 /**************************************************************************
2  *
3  * hal-api-tbm
4  *
5  * Copyright 2021 Samsung Electronics co., Ltd. All Rights Reserved.
6  *
7  * Contact: SooChan Lim <sc1.lim@samsung.com>,
8  *          Junkyeong Kim <jk0430.kim@samsung.com>,
9  *          Changyeon Lee <cyeon.lee@samsung.com>
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the
13  * "Software"), to deal in the Software without restriction, including
14  * without limitation the rights to use, copy, modify, merge, publish,
15  * distribute, sub license, and/or sell copies of the Software, and to
16  * permit persons to whom the Software is furnished to do so, subject to
17  * the following conditions:
18  *
19  * The above copyright notice and this permission notice (including the
20  * next paragraph) shall be included in all copies or substantial portions
21  * of the Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
26  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
27  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
28  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
29  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30  *
31 **************************************************************************/
32
33 #include <stdio.h>
34 #include <stdint.h>
35 #include <dlfcn.h>
36 #include <dlog.h>
37
38 #include <hal/hal-common.h>
39 #include "hal-tbm-interface.h"
40 #include "hal-tbm.h"
41 #include "common.h"
42
43 #if defined(__GNUC__) && __GNUC__ >= 4
44 #define EXTERN __attribute__ ((visibility("default")))
45 #else
46 #define EXTERN
47 #endif
48
49 #if defined(__GNUC__) && __GNUC__ >= 4
50 #define INTERN __attribute__ ((visibility("hidden")))
51 #else
52 #define INTERN
53 #endif
54
55 #define ARRAY_SIZE(name)        (sizeof(name)/sizeof(name[0]))
56
57 #define BUFMGR_FUNC_ENTRY(__func__) \
58         hal_tbm_backend_bufmgr_func *bufmgr_func; \
59         if (!g_tbm_funcs || !g_tbm_funcs->bufmgr_func) return TBM_ERROR_NOT_SUPPORTED; \
60         bufmgr_func = g_tbm_funcs->bufmgr_func; \
61         if (!bufmgr_func->__func__) return TBM_ERROR_NOT_SUPPORTED;
62
63 #define BUFMGR_FUNC_ENTRY_NULL(__func__) \
64         hal_tbm_backend_bufmgr_func *bufmgr_func; \
65         if (!g_tbm_funcs || !g_tbm_funcs->bufmgr_func) { if (error) *error = TBM_ERROR_NOT_SUPPORTED; return NULL; } \
66         bufmgr_func = g_tbm_funcs->bufmgr_func; \
67         if (!bufmgr_func->__func__) { if (error) *error = TBM_ERROR_NOT_SUPPORTED; return NULL; };
68
69 #define BUFMGR_FUNC_ENTRY_GOTO(__func__, __goto__) \
70         hal_tbm_backend_bufmgr_func *bufmgr_func; \
71         if (!g_tbm_funcs || !g_tbm_funcs->bufmgr_func) goto __goto__; \
72         bufmgr_func = g_tbm_funcs->bufmgr_func; \
73         if (!bufmgr_func->__func__) goto __goto__;
74
75 #define BO_FUNC_ENTRY(__func__) \
76         hal_tbm_backend_bo_func *bo_func; \
77         if (!g_tbm_funcs || !g_tbm_funcs->bo_func) return TBM_ERROR_NOT_SUPPORTED; \
78         bo_func = g_tbm_funcs->bo_func; \
79         if (!bo_func->__func__) return TBM_ERROR_NOT_SUPPORTED;
80
81 #define BO_FUNC_ENTRY_VOID(__func__) \
82         hal_tbm_backend_bo_func *bo_func; \
83         if (!g_tbm_funcs || !g_tbm_funcs->bo_func) return; \
84         bo_func = g_tbm_funcs->bo_func; \
85         if (!bo_func->__func__) return;
86
87 #define BO_FUNC_ENTRY_GOTO(__func__, __goto__) \
88         hal_tbm_backend_bo_func *bo_func; \
89         if (!g_tbm_funcs || !g_tbm_funcs->bo_func) goto __goto__; \
90         bo_func = g_tbm_funcs->bo_func; \
91         if (!bo_func->__func__) goto __goto__;
92
93 static hal_backend_tbm_funcs *g_tbm_funcs = NULL;
94
95 EXTERN
96 int hal_tbm_get_backend(void)
97 {
98         int ret;
99
100         if (g_tbm_funcs)
101                 return 0;
102
103         ret = hal_common_get_backend(HAL_MODULE_TBM, (void **)&g_tbm_funcs);
104         if (ret < 0) {
105                  _E("Failed to get backend\n");
106                 return -EINVAL;
107         }
108
109         return 0;
110 }
111
112 EXTERN
113 int hal_tbm_put_backend(void)
114 {
115         if (!g_tbm_funcs)
116                 return 0;
117
118         hal_common_put_backend(HAL_MODULE_TBM, (void *)g_tbm_funcs);
119         g_tbm_funcs = NULL;
120
121         return 0;
122 }
123
124
125 /* tbm_bufmgr_func */
126 EXTERN tbm_bufmgr_capability
127 hal_tbm_bufmgr_get_capabilities(tbm_backend_bufmgr_data *bufmgr_data, tbm_error_e *error)
128 {
129         BUFMGR_FUNC_ENTRY_GOTO(bufmgr_get_capabilities, fail);
130         return bufmgr_func->bufmgr_get_capabilities((hal_tbm_backend_bufmgr_data *)bufmgr_data, error);
131
132 fail:
133         if (error)
134                 *error = TBM_ERROR_NOT_SUPPORTED;
135         return TBM_BUFMGR_CAPABILITY_NONE;
136 }
137
138 EXTERN tbm_error_e
139 hal_tbm_bufmgr_bind_native_display(tbm_backend_bufmgr_data *bufmgr_data, tbm_native_display *native_display)
140 {
141         BUFMGR_FUNC_ENTRY(bufmgr_bind_native_display);
142         return bufmgr_func->bufmgr_bind_native_display((hal_tbm_backend_bufmgr_data *)bufmgr_data, native_display);
143 }
144
145 EXTERN tbm_error_e
146 hal_tbm_bufmgr_get_supported_formats(tbm_backend_bufmgr_data *bufmgr_data, uint32_t **formats, uint32_t *num)
147 {
148         BUFMGR_FUNC_ENTRY(bufmgr_get_supported_formats);
149         return bufmgr_func->bufmgr_get_supported_formats((hal_tbm_backend_bufmgr_data *)bufmgr_data, formats, num);
150 }
151
152 EXTERN tbm_error_e
153 hal_tbm_bufmgr_get_plane_data(tbm_backend_bufmgr_data *bufmgr_data, tbm_format format, int plane_idx, int width, int height,
154                                                                         uint32_t *size, uint32_t *offset, uint32_t *pitch, int *bo_idx)
155 {
156         BUFMGR_FUNC_ENTRY(bufmgr_get_plane_data);
157         return bufmgr_func->bufmgr_get_plane_data((hal_tbm_backend_bufmgr_data *)bufmgr_data, format, plane_idx, width, height, size, offset, pitch, bo_idx);
158 }
159
160 EXTERN tbm_backend_bo_data *
161 hal_tbm_bufmgr_alloc_bo(tbm_backend_bufmgr_data *bufmgr_data, unsigned int size, tbm_bo_memory_type mem_types, tbm_error_e *error)
162 {
163         BUFMGR_FUNC_ENTRY_NULL(bufmgr_alloc_bo);
164         return bufmgr_func->bufmgr_alloc_bo((hal_tbm_backend_bufmgr_data *)bufmgr_data, size, mem_types, error);
165 }
166
167 EXTERN tbm_backend_bo_data *
168 hal_tbm_bufmgr_alloc_bo_with_format(tbm_backend_bufmgr_data *bufmgr_data, int format, int bo_idx, int width, int height,
169                                                                         tbm_bo_memory_type mem_types, tbm_error_e *error)
170 {
171         BUFMGR_FUNC_ENTRY_NULL(bufmgr_alloc_bo_with_format);
172         return bufmgr_func->bufmgr_alloc_bo_with_format((hal_tbm_backend_bufmgr_data *)bufmgr_data, format, bo_idx, width, height, mem_types, error);
173 }
174
175 EXTERN tbm_backend_bo_data *
176 hal_tbm_bufmgr_alloc_bo_with_tiled_format(tbm_backend_bufmgr_data *bufmgr_data, int width, int height, int bpp, int format,
177                                                                         tbm_bo_memory_type flags, int bo_idx, tbm_error_e *error)
178 {
179         BUFMGR_FUNC_ENTRY_NULL(bufmgr_alloc_bo_with_tiled_format);
180         return bufmgr_func->bufmgr_alloc_bo_with_tiled_format((hal_tbm_backend_bufmgr_data *)bufmgr_data, width, height, bpp, format, flags, bo_idx, error);
181 }
182
183 EXTERN tbm_backend_bo_data *
184 hal_tbm_bufmgr_import_fd(tbm_backend_bufmgr_data *bufmgr_data, tbm_fd fd, tbm_error_e *error)
185 {
186         BUFMGR_FUNC_ENTRY_NULL(bufmgr_import_fd);
187         return bufmgr_func->bufmgr_import_fd((hal_tbm_backend_bufmgr_data *)bufmgr_data, fd, error);
188 }
189
190 EXTERN tbm_backend_bo_data *
191 hal_tbm_bufmgr_import_key(tbm_backend_bufmgr_data *bufmgr_data, tbm_key key, tbm_error_e *error)
192 {
193         BUFMGR_FUNC_ENTRY_NULL(bufmgr_import_key);
194         return bufmgr_func->bufmgr_import_key((hal_tbm_backend_bufmgr_data *)bufmgr_data, key, error);
195 }
196
197
198 /* tbm_bo_func*/
199 EXTERN void
200 hal_tbm_bo_free(tbm_backend_bo_data *bo_data)
201 {
202         BO_FUNC_ENTRY_VOID(bo_free);
203         bo_func->bo_free((hal_tbm_backend_bo_data *)bo_data);
204 }
205
206 EXTERN int
207 hal_tbm_bo_get_size(tbm_backend_bo_data *bo_data, tbm_error_e *error)
208 {
209         BO_FUNC_ENTRY_GOTO(bo_get_size, fail);
210         return bo_func->bo_get_size((hal_tbm_backend_bo_data *)bo_data, error);
211
212 fail:
213         if (error)
214                 *error = TBM_ERROR_NOT_SUPPORTED;
215         return 0;
216 }
217
218 EXTERN tbm_bo_memory_type
219 hal_tbm_bo_get_memory_types(tbm_backend_bo_data *bo_data, tbm_error_e *error)
220 {
221         BO_FUNC_ENTRY_GOTO(bo_get_memory_types, fail);
222         return bo_func->bo_get_memory_types((hal_tbm_backend_bo_data *)bo_data, error);
223
224 fail:
225         if (error)
226                 *error = TBM_ERROR_NOT_SUPPORTED;
227         return TBM_BO_DEFAULT;
228 }
229
230 EXTERN tbm_bo_handle
231 hal_tbm_bo_get_handle(tbm_backend_bo_data *bo_data, tbm_bo_device_type device, tbm_error_e *error)
232 {
233         BO_FUNC_ENTRY_GOTO(bo_get_handle, fail);
234         return bo_func->bo_get_handle((hal_tbm_backend_bo_data *)bo_data, device, error);
235
236 fail:
237         if (error)
238                 *error = TBM_ERROR_NOT_SUPPORTED;
239         return (tbm_bo_handle)NULL;
240 }
241
242 EXTERN tbm_bo_handle
243 hal_tbm_bo_map(tbm_backend_bo_data *bo_data, tbm_bo_device_type device, tbm_bo_access_option opt, tbm_error_e *error)
244 {
245         BO_FUNC_ENTRY_GOTO(bo_map, fail);
246         return bo_func->bo_map((hal_tbm_backend_bo_data *)bo_data, device, opt, error);
247
248 fail:
249         if (error)
250                 *error = TBM_ERROR_NOT_SUPPORTED;
251         return (tbm_bo_handle)NULL;
252 }
253
254 EXTERN tbm_error_e
255 hal_tbm_bo_unmap(tbm_backend_bo_data *bo_data)
256 {
257         BO_FUNC_ENTRY(bo_unmap);
258         return bo_func->bo_unmap((hal_tbm_backend_bo_data *)bo_data);
259 }
260
261 EXTERN tbm_error_e
262 hal_tbm_bo_lock(tbm_backend_bo_data *bo_data, tbm_bo_device_type device, tbm_bo_access_option opt)
263 {
264         BO_FUNC_ENTRY(bo_unmap);
265         return bo_func->bo_lock((hal_tbm_backend_bo_data *)bo_data, device, opt);
266 }
267
268 EXTERN tbm_error_e
269 hal_tbm_bo_unlock(tbm_backend_bo_data *bo_data)
270 {
271         BO_FUNC_ENTRY(bo_unmap);
272         return bo_func->bo_unlock((hal_tbm_backend_bo_data *)bo_data);
273 }
274
275 EXTERN tbm_fd
276 hal_tbm_bo_export_fd(tbm_backend_bo_data *bo_data, tbm_error_e *error)
277 {
278         BO_FUNC_ENTRY_GOTO(bo_export_fd, fail);
279         return bo_func->bo_export_fd((hal_tbm_backend_bo_data *)bo_data, error);
280
281 fail:
282         if (error)
283                 *error = TBM_ERROR_NOT_SUPPORTED;
284         return -1;
285 }
286
287 EXTERN tbm_key
288 hal_tbm_bo_export_key(tbm_backend_bo_data *bo_data, tbm_error_e *error)
289 {
290         BO_FUNC_ENTRY_GOTO(bo_export_key, fail);
291         return bo_func->bo_export_key((hal_tbm_backend_bo_data *)bo_data, error);
292
293 fail:
294         if (error)
295                 *error = TBM_ERROR_NOT_SUPPORTED;
296         return 0;
297 }
298
299
300 /* tbm_backend_func*/
301 EXTERN int
302 hal_tbm_backend_bufmgr_query_display_server(tbm_bufmgr bufmgr, tbm_error_e *error)
303 {
304         return tbm_backend_bufmgr_query_display_server(bufmgr, error);
305 }