974c67bcffdab244cef067b8063d98c328f68668
[platform/core/uifw/libtbm.git] / src / tbm_module.c
1 /**************************************************************************
2
3 libtbm
4
5 Copyright 2012-2021 Samsung Electronics co., Ltd. All Rights Reserved.
6
7 Contact: SooChan Lim <sc1.lim@samsung.com>
8          Changyeon Lee <cyeon.lee@samsung.com>
9
10 Permission is hereby granted, free of charge, to any person obtaining a
11 copy of this software and associated documentation files (the
12 "Software"), to deal in the Software without restriction, including
13 without limitation the rights to use, copy, modify, merge, publish,
14 distribute, sub license, and/or sell copies of the Software, and to
15 permit persons to whom the Software is furnished to do so, subject to
16 the following conditions:
17
18 The above copyright notice and this permission notice (including the
19 next paragraph) shall be included in all copies or substantial portions
20 of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
25 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 **************************************************************************/
31
32 #include "config.h"
33
34 #include "tbm_bufmgr_int.h"
35 #include "tbm_bufmgr_backend.h"
36 #include "tbm_drm_helper.h"
37
38 #define PREFIX_LIB    "libtbm_"
39 #define SUFFIX_LIB    ".so"
40 #define DEFAULT_LIB   PREFIX_LIB"default"SUFFIX_LIB
41
42 /* values to indicate unspecified fields in XF86ModReqInfo. */
43 #define MAJOR_UNSPEC      0xFF
44 #define MINOR_UNSPEC      0xFF
45 #define PATCH_UNSPEC      0xFFFF
46 #define ABI_VERS_UNSPEC   0xFFFFFFFF
47
48 #define MODULE_VERSION_NUMERIC(maj, min, patch) \
49                         ((((maj) & 0xFF) << 24) | (((min) & 0xFF) << 16) | (patch & 0xFFFF))
50 #define GET_MODULE_MAJOR_VERSION(vers)    (((vers) >> 24) & 0xFF)
51 #define GET_MODULE_MINOR_VERSION(vers)    (((vers) >> 16) & 0xFF)
52 #define GET_MODULE_PATCHLEVEL(vers)    ((vers) & 0xFFFF)
53
54 /* LCOV_EXCL_START */
55 static int
56 _tbm_backend_load_hal_tbm(tbm_module *module)
57 {
58         hal_tbm_backend *hal_backend = NULL;
59         hal_tbm_bufmgr *hal_bufmgr;
60         hal_tbm_error ret = HAL_TBM_ERROR_NONE;
61         hal_tbm_fd auth_drm_fd = -1;
62
63         hal_backend = hal_tbm_get_backend(&ret);
64         if (hal_backend == NULL || ret != HAL_TBM_ERROR_NONE) {
65                 TBM_ERR("get backend fail");
66                 return 0;
67         }
68
69         hal_bufmgr = hal_tbm_backend_get_bufmgr(hal_backend, &ret);
70         if (hal_bufmgr == NULL || ret != HAL_TBM_ERROR_NONE) {
71                 TBM_ERR("get hal_bufmgr fail");
72                 goto get_backend_fail;
73         }
74
75         if (hal_tbm_backend_has_drm_device(hal_backend, &ret)) {
76                 auth_drm_fd = hal_tbm_backend_get_master_drm_fd(hal_backend, &ret);
77                 if (auth_drm_fd < 0) {
78                         TBM_INFO("tbm_backend has no master drm_fd.");
79
80                         auth_drm_fd = tbm_drm_helper_get_master_fd();
81                         if (auth_drm_fd < 0) {
82                                 TBM_INFO("libtbm requests an authenticated drm_fd to a process(display server).");
83                                 if (!tbm_drm_helper_get_auth_info(&auth_drm_fd, NULL, NULL)) {
84                                         TBM_ERR("get auth drm_fd fail");
85                                         goto get_backend_fail;
86                                 }
87                         } else {
88                                 TBM_INFO("libtbm gets a master drm_fd from libtdm via tbm_drm_helper.");
89                         }
90
91                         TBM_INFO("libtbm sends a master drm_fd as an authentiated drm_fd to tbm_backend.");
92                         ret = hal_tbm_backend_set_authenticated_drm_fd(hal_backend, auth_drm_fd);
93                         if (ret != HAL_TBM_ERROR_NONE) {
94                                 TBM_ERR("hal_tbm_backend_set_authenticated_drm_fd failed.");
95                                 goto get_backend_fail;
96                         }
97                 } else {
98                         TBM_INFO("tbm_backend has a master drm_fd.");
99
100                         tbm_drm_helper_set_tbm_master_fd(auth_drm_fd);
101                 }
102                 tbm_drm_helper_set_fd(auth_drm_fd);
103         }
104
105         module->hal_backend = hal_backend;
106         module->hal_bufmgr = hal_bufmgr;
107
108         module->use_hal_tbm = 1;
109
110         TBM_INFO("use HAL-TBM_API");
111
112         return 1;
113
114 get_backend_fail:
115         if (auth_drm_fd >= 0)
116                 close(auth_drm_fd);
117         hal_tbm_put_backend(hal_backend);
118         return 0;
119 }
120
121 static int
122 _check_version(TBMModuleVersionInfo *data)
123 {
124         int backend_module_major, backend_module_minor;
125         int tbm_backend_major, tbm_backend_minor;
126
127         backend_module_major = GET_ABI_MAJOR(data->abiversion);
128         backend_module_minor = GET_ABI_MINOR(data->abiversion);
129
130         TBM_DBG("TBM module %s: vendor=\"%s\" ABI=%d,%d\n",
131             data->modname ? data->modname : "UNKNOWN!",
132             data->vendor ? data->vendor : "UNKNOWN!", backend_module_major, backend_module_minor);
133
134         tbm_backend_major = GET_ABI_MAJOR(TBM_ABI_VERSION);
135         tbm_backend_minor = GET_ABI_MINOR(TBM_ABI_VERSION);
136
137         TBM_DBG("TBM ABI version %d.%d\n",
138             tbm_backend_major, tbm_backend_minor);
139
140         if (backend_module_major != tbm_backend_major) {
141                 TBM_ERR("TBM module ABI major ver(%d) doesn't match the TBM's ver(%d)\n",
142                         backend_module_major, tbm_backend_major);
143                 return 0;
144         } else if (backend_module_minor > tbm_backend_minor) {
145                 TBM_ERR("TBM module ABI minor ver(%d) is newer than the TBM's ver(%d)\n",
146                         backend_module_minor, tbm_backend_minor);
147                 return 0;
148         }
149
150         return 1;
151 }
152
153 static int
154 _tbm_backend_check_bufmgr_func(tbm_backend_bufmgr_func *bufmgr_func)
155 {
156         TBM_RETURN_VAL_IF_FAIL(bufmgr_func, 0); /* mandatory symbol */
157         TBM_RETURN_VAL_IF_FAIL(bufmgr_func->bufmgr_get_capabilities, 0); /* mandatory symbol */
158         TBM_RETURN_VAL_IF_FAIL(bufmgr_func->bufmgr_bind_native_display, 0); /* mandatory symbol */
159         TBM_RETURN_VAL_IF_FAIL(bufmgr_func->bufmgr_get_supported_formats, 0); /* mandatory symbol */
160         TBM_RETURN_VAL_IF_FAIL(bufmgr_func->bufmgr_get_plane_data, 0); /* mandatory symbol */
161         TBM_RETURN_VAL_IF_FAIL(bufmgr_func->bufmgr_alloc_bo, 0); /* mandatory symbol */
162         if (!bufmgr_func->bufmgr_alloc_bo_with_format)
163                 TBM_DBG("No bufmgr_func->bufmgr_alloc_bo_with_format.");
164         TBM_RETURN_VAL_IF_FAIL(bufmgr_func->bufmgr_import_fd, 0); /* mandatory symbol */
165         if (!bufmgr_func->bufmgr_import_key)
166                 TBM_DBG("No bufmgr_func->bo_export_key.");
167
168         return 1;
169 }
170
171 static int
172 _tbm_backend_check_bufmgr_bo(tbm_backend_bo_func *bo_func)
173 {
174         TBM_RETURN_VAL_IF_FAIL(bo_func, 0); /* mandatory symbol */
175         TBM_RETURN_VAL_IF_FAIL(bo_func->bo_free, 0); /* mandatory symbol */
176         TBM_RETURN_VAL_IF_FAIL(bo_func->bo_get_size, 0); /* mandatory symbol */
177         TBM_RETURN_VAL_IF_FAIL(bo_func->bo_get_memory_types, 0); /* mandatory symbol */
178         TBM_RETURN_VAL_IF_FAIL(bo_func->bo_get_handle, 0); /* mandatory symbol */
179         TBM_RETURN_VAL_IF_FAIL(bo_func->bo_map, 0); /* mandatory symbol */
180         TBM_RETURN_VAL_IF_FAIL(bo_func->bo_unmap, 0); /* mandatory symbol */
181         if (!bo_func->bo_lock)
182                 TBM_DBG("No bo_func->bo_lock.");
183         if (!bo_func->bo_unlock)
184                 TBM_DBG("No bo_func->bo_unlock.");
185         TBM_RETURN_VAL_IF_FAIL(bo_func->bo_export_fd, 0); /* mandatory symbol */
186         if (!bo_func->bo_export_key)
187                 TBM_INFO("No bo_func->bo_export_key.");
188
189         return 1;
190 }
191
192 static int
193 _tbm_backend_load_module(tbm_module *module, const char *file)
194 {
195         char path[PATH_MAX] = {0, };
196         void *module_data = NULL;
197         tbm_backend_module *backend_module_data = NULL;
198         tbm_backend_bufmgr_data *bufmgr_data = NULL;
199         int backend_module_major, backend_module_minor;
200         int tbm_backend_major, tbm_backend_minor;
201         tbm_error_e error;
202
203         snprintf(path, sizeof(path), BUFMGR_MODULE_DIR "/%s", file);
204
205         module_data = dlopen(path, RTLD_LAZY);
206         if (!module_data) {
207                 TBM_ERR("failed to load module: %s(%s)\n", dlerror(), file);
208                 return 0;
209         }
210
211         backend_module_data = dlsym(module_data, "tbm_backend_module_data");
212         if (!backend_module_data) {
213                 TBM_ERR("Error: module does not have data object.\n");
214                 goto err;
215         }
216
217         tbm_backend_major = GET_ABI_MAJOR(TBM_BACKEND_ABI_LATEST_VERSION);
218         tbm_backend_minor = GET_ABI_MINOR(TBM_BACKEND_ABI_LATEST_VERSION);
219         TBM_INFO("TBM Backend ABI version %d.%d\n", tbm_backend_major, tbm_backend_minor);
220
221         backend_module_major = GET_ABI_MAJOR(backend_module_data->abi_version);
222         backend_module_minor = GET_ABI_MINOR(backend_module_data->abi_version);
223
224         TBM_INFO("TBM module %s: vendor=\"%s\" Backend ABI version=%d.%d\n",
225             backend_module_data->name ? backend_module_data->name : "UNKNOWN!",
226             backend_module_data->vendor ? backend_module_data->vendor : "UNKNOWN!",
227                 backend_module_major, backend_module_minor);
228
229         if (backend_module_major > tbm_backend_major) {
230                 TBM_ERR("TBM module ABI major ver(%d) is newer than the TBM's ver(%d)\n",
231                         backend_module_major, tbm_backend_major);
232                 goto err;
233         } else if (backend_module_minor > tbm_backend_minor) {
234                 TBM_ERR("TBM module ABI minor ver(%d) is newer than the TBM's ver(%d)\n",
235                         backend_module_minor, tbm_backend_minor);
236                 goto err;
237         }
238
239         if (!backend_module_data->init) {
240                 TBM_ERR("Error: module does not supply init symbol.\n");
241                 goto err;
242         }
243
244         if (!backend_module_data->deinit)       {
245                 TBM_ERR("Error: module does not supply deinit symbol.\n");
246                 goto err;
247         }
248
249         bufmgr_data = backend_module_data->init((tbm_bufmgr)module, &error);
250         if (!bufmgr_data) {
251                 TBM_ERR("Fail to init module(%s)\n", file);
252                 goto err;
253         }
254
255         /* check the mandatory symbols of the backend module */
256         if (!_tbm_backend_check_bufmgr_func(module->bufmgr_func)) {
257                 TBM_ERR("Fail to check the bufmgr_func symboles.");
258                 goto err;
259         }
260
261         if (!_tbm_backend_check_bufmgr_bo(module->bo_func)) {
262                 TBM_ERR("Fail to check the bufmgr_bo symboles.");
263                 goto err;
264         }
265
266         module->module_data = module_data;
267         module->backend_module_data = backend_module_data;
268         module->bufmgr_data = bufmgr_data;
269
270         TBM_INFO("Success to load module(%s)\n", file);
271
272         return 1;
273
274 err:
275         if (bufmgr_data)
276                 module->backend_module_data->deinit(bufmgr_data);
277         if (module_data)
278                 dlclose(module_data);
279
280         return 0;
281 }
282
283 static int
284 _tbm_backend_load_bufmgr_module(tbm_module *module, int fd, const char *file)
285 {
286         char path[PATH_MAX] = {0, };
287         TBMModuleVersionInfo *vers;
288         TBMModuleData *initdata;
289         ModuleInitProc init;
290         void *module_data;
291
292         snprintf(path, sizeof(path), BUFMGR_MODULE_DIR "/%s", file);
293
294         module_data = dlopen(path, RTLD_LAZY);
295         if (!module_data) {
296                 TBM_ERR("failed to load module: %s(%s)\n", dlerror(), file);
297                 return 0;
298         }
299
300         initdata = dlsym(module_data, "tbmModuleData");
301         if (!initdata) {
302                 TBM_ERR("Error: module does not have data object.\n");
303                 goto err;
304         }
305
306         vers = initdata->vers;
307         if (!vers) {
308                 TBM_ERR("Error: module does not supply version information.\n");
309                 goto err;
310         }
311
312         init = initdata->init;
313         if (!init) {
314                 TBM_ERR("Error: module does not supply init symbol.\n");
315                 goto err;
316         }
317
318         if (!_check_version(vers)) {
319                 TBM_ERR("Fail to check version.\n");
320                 goto err;
321         }
322
323         if (!init((tbm_bufmgr)module, fd)) {
324                 TBM_ERR("Fail to init module(%s)\n", file);
325                 goto err;
326         }
327
328         if (!module->backend || !module->backend->priv) {
329                 TBM_ERR("Error: module(%s) wrong operation. Check backend or backend's priv.\n", file);
330                 goto err;
331         }
332
333         module->module_data = module_data;
334
335         TBM_DBG("Success to load module(%s)\n", file);
336
337         return 1;
338
339 err:
340         dlclose(module_data);
341         return 0;
342 }
343 /* LCOV_EXCL_STOP */
344
345 tbm_module *
346 tbm_module_load(int fd)
347 {
348         tbm_module *module;
349         struct dirent **namelist;
350         int ret = 0, n;
351
352         module = calloc(1, sizeof(struct _tbm_module));
353         if (!module) {
354                 TBM_ERR("fail to allocate the memory");
355                 return NULL;
356         }
357
358         /* try to load the hal-tbm backend module */
359         ret = _tbm_backend_load_hal_tbm(module);
360         if (ret) {
361                 module->type = TBM_MODULE_TYPE_HAL_TBM;
362                 goto done;
363         }
364
365 /* LCOV_EXCL_START */
366         /* try to load the new backend module */
367         ret = _tbm_backend_load_module(module, DEFAULT_LIB);
368         if (ret) {
369                 module->type = TBM_MODULE_TYPE_TBM_BACKEND;
370                 goto done;
371         }
372
373         /* try to load the old(deprecated) backend mdoule */
374         ret = _tbm_backend_load_bufmgr_module(module, fd, DEFAULT_LIB);
375         if (ret) {
376                 module->type = TBM_MODULE_TYPE_BUFMGR_BACKEND;
377                 return module;
378         }
379
380         /* load backend_module from configured path */
381         n = scandir(BUFMGR_MODULE_DIR, &namelist, 0, alphasort);
382         if (n < 0) {
383                 TBM_ERR("no files : %s\n", BUFMGR_MODULE_DIR);
384                 tbm_module_unload(module);
385                 return NULL;
386         }
387
388         while (n--) {
389                 if (!ret && strstr(namelist[n]->d_name, PREFIX_LIB)) {
390                         const char *p = strstr(namelist[n]->d_name, SUFFIX_LIB);
391
392                         if (p && !strcmp(p, SUFFIX_LIB)) {
393                                 ret = _tbm_backend_load_module(module, namelist[n]->d_name);
394                                 if (ret)
395                                         module->type = TBM_MODULE_TYPE_TBM_BACKEND;
396                                 else {
397                                         ret = _tbm_backend_load_bufmgr_module(module, fd, namelist[n]->d_name);
398                                         module->type = TBM_MODULE_TYPE_BUFMGR_BACKEND;
399                                 }
400                         }
401                 }
402
403                 free(namelist[n]);
404         }
405
406         free(namelist);
407
408         if (!ret) {
409                 free(module);
410                 module = NULL;
411         }
412 /* LCOV_EXCL_STOP */
413
414 done:
415         return module;
416 }
417
418 void
419 tbm_module_unload(tbm_module *module)
420 {
421         switch (module->type) {
422         case TBM_MODULE_TYPE_HAL_TBM:
423                 if (module->auth_wl_socket_created) {
424                         tbm_drm_helper_wl_auth_server_deinit();
425                         close(module->auth_fd);
426                 }
427                 tbm_drm_helper_unset_tbm_master_fd();
428                 tbm_drm_helper_unset_fd();
429
430                 hal_tbm_put_backend(module->hal_backend);
431                 module->hal_backend = NULL;
432                 module->hal_bufmgr = NULL;
433                 module->use_hal_tbm = 0;
434                 break;
435 /* LCOV_EXCL_START */
436         case TBM_MODULE_TYPE_TBM_BACKEND:
437                 module->backend_module_data->deinit(module->bufmgr_data);
438                 module->bo_func = NULL;
439                 module->bufmgr_func = NULL;
440                 module->bufmgr_data = NULL;
441                 module->backend_module_data = NULL;
442
443                 dlclose(module->module_data);
444                 break;
445         case TBM_MODULE_TYPE_BUFMGR_BACKEND:
446                 module->backend->bufmgr_deinit(module->backend->priv);
447                 module->backend->priv = NULL;
448                 tbm_backend_free(module->backend);
449                 module->backend = NULL;
450
451                 dlclose(module->module_data);
452                 break;
453         default:
454                 TBM_ERR("Wrong module type:%d", module->type);
455                 break;
456 /* LCOV_EXCL_STOP */
457         }
458
459         free(module);
460 }
461
462 int
463 tbm_module_bufmgr_get_capabilities(tbm_module *module, tbm_error_e *error)
464 {
465         int capabilities = 0;
466         tbm_backend_bufmgr_func *bufmgr_func = NULL;
467
468         TBM_RETURN_VAL_SET_ERR_IF_FAIL(module, TBM_BUFMGR_CAPABILITY_NONE, *error, TBM_ERROR_INVALID_PARAMETER);
469
470         switch (module->type) {
471         case TBM_MODULE_TYPE_HAL_TBM:
472                 capabilities = hal_tbm_bufmgr_get_capabilities(module->hal_bufmgr, (hal_tbm_error *)error);
473                 break;
474 /* LCOV_EXCL_START */
475         case TBM_MODULE_TYPE_TBM_BACKEND:
476                 bufmgr_func = module->bufmgr_func;
477                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(bufmgr_func, 0, *error, TBM_ERROR_INVALID_OPERATION);
478                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(bufmgr_func->bufmgr_get_capabilities, 0, *error, TBM_ERROR_NOT_SUPPORTED);
479
480                 capabilities = module->bufmgr_func->bufmgr_get_capabilities(module->bufmgr_data, error);
481                 break;
482         case TBM_MODULE_TYPE_BUFMGR_BACKEND:
483                 TBM_WRN("!!WARNING: This backend interface will be DEPRECATED after Tizen 6.5.");
484                 TBM_ERR("Do not support at tbm_bufmgr_backend.");
485
486                 *error = TBM_ERROR_NOT_SUPPORTED;
487                 break;
488         default:
489                 TBM_ERR("Wrong module type:%d", module->type);
490                 *error = TBM_ERROR_INVALID_OPERATION;
491                 break;
492 /* LCOV_EXCL_STOP */
493         }
494
495         return capabilities;
496 }
497
498 tbm_error_e
499 tbm_module_bufmgr_bind_native_display(tbm_module *module, void *native_display)
500 {
501         tbm_error_e error = TBM_ERROR_NONE;
502         tbm_backend_bufmgr_func *bufmgr_func = NULL;
503         tbm_bufmgr_backend backend = NULL;
504         int ret = 0;
505
506         TBM_RETURN_VAL_IF_FAIL(module, TBM_ERROR_INVALID_PARAMETER);
507
508         switch (module->type) {
509         case TBM_MODULE_TYPE_HAL_TBM:
510                 if (hal_tbm_backend_has_drm_device(module->hal_backend, &ret)) {
511                         int fd = tbm_drm_helper_get_fd(); // this must be the auth drm_fd.(master drm_fd);
512                         if (fd < 0) {
513                                 TBM_ERR("error: module(%p) native_display(%p)\n", module, native_display);
514                                 return TBM_ERROR_INVALID_OPERATION;
515                         }
516
517                         // make the wayland server socket for sending the authenticated drm_fd to wayland clients.
518                         if (!tbm_drm_helper_wl_auth_server_init(native_display, fd, NULL, 0)) {
519                                 TBM_ERR("error: tbm_drm_helper_wl_auth_server_init failed\n", module, native_display);
520                                 close(fd);
521                                 return TBM_ERROR_INVALID_OPERATION;
522                         }
523                         TBM_INFO("tbm creates a wayland socket for authentication of drm_fd.");
524
525                         module->auth_wl_socket_created = 1;
526                         module->auth_fd = fd;
527                 } else {
528                         TBM_INFO("tbm_module has no drm device.");
529                         error = TBM_ERROR_NONE;
530                 }
531                 break;
532 /* LCOV_EXCL_START */
533         case TBM_MODULE_TYPE_TBM_BACKEND:
534                 bufmgr_func = module->bufmgr_func;
535                 TBM_RETURN_VAL_IF_FAIL(bufmgr_func, TBM_ERROR_INVALID_OPERATION);
536                 TBM_RETURN_VAL_IF_FAIL(bufmgr_func->bufmgr_bind_native_display, TBM_ERROR_NOT_SUPPORTED);
537
538                 error = bufmgr_func->bufmgr_bind_native_display(module->bufmgr_data, (tbm_native_display *)native_display);
539                 break;
540         case TBM_MODULE_TYPE_BUFMGR_BACKEND:
541                 TBM_WRN("!!WARNING: This backend interface will be DEPRECATED after Tizen 6.5.");
542                 backend = module->backend;
543                 TBM_RETURN_VAL_IF_FAIL(backend, TBM_ERROR_INVALID_OPERATION);
544                 TBM_RETURN_VAL_IF_FAIL(backend->bufmgr_bind_native_display, TBM_ERROR_NOT_SUPPORTED);
545
546                 ret = backend->bufmgr_bind_native_display((tbm_bufmgr)module, native_display);
547                 if (!ret)
548                         error = TBM_ERROR_INVALID_OPERATION;
549
550                 break;
551         default:
552                 TBM_ERR("Wrong module type:%d", module->type);
553                 error = TBM_ERROR_INVALID_OPERATION;
554                 break;
555 /* LCOV_EXCL_STOP */
556         }
557
558         return error;
559 }
560
561 tbm_error_e
562 tbm_module_bufmgr_get_supported_formats(tbm_module *module, uint32_t **formats, uint32_t *num)
563 {
564         tbm_error_e error = TBM_ERROR_NONE;
565         tbm_backend_bufmgr_func *bufmgr_func = NULL;
566         tbm_bufmgr_backend backend = NULL;
567         int ret = 0;
568
569         TBM_RETURN_VAL_IF_FAIL(module, TBM_ERROR_INVALID_PARAMETER);
570
571         switch (module->type) {
572         case TBM_MODULE_TYPE_HAL_TBM:
573                 error = (tbm_error_e)hal_tbm_bufmgr_get_supported_formats(module->hal_bufmgr, formats, num);
574                 break;
575 /* LCOV_EXCL_START */
576         case TBM_MODULE_TYPE_TBM_BACKEND:
577                 bufmgr_func = module->bufmgr_func;
578                 TBM_RETURN_VAL_IF_FAIL(bufmgr_func, TBM_ERROR_INVALID_OPERATION);
579                 TBM_RETURN_VAL_IF_FAIL(bufmgr_func->bufmgr_get_supported_formats, TBM_ERROR_NOT_SUPPORTED);
580
581                 error = bufmgr_func->bufmgr_get_supported_formats(module->bufmgr_data, formats, num);
582                 break;
583         case TBM_MODULE_TYPE_BUFMGR_BACKEND:
584                 TBM_WRN("!!WARNING: This backend interface will be DEPRECATED after Tizen 6.5.");
585                 backend = module->backend;
586                 TBM_RETURN_VAL_IF_FAIL(backend, TBM_ERROR_INVALID_OPERATION);
587                 TBM_RETURN_VAL_IF_FAIL(backend->surface_supported_format, TBM_ERROR_NOT_SUPPORTED);
588
589                 ret = backend->surface_supported_format(formats, num);
590                 if (!ret)
591                         error = TBM_ERROR_INVALID_OPERATION;
592
593                 break;
594         default:
595                 TBM_ERR("Wrong module type:%d", module->type);
596                 error = TBM_ERROR_INVALID_OPERATION;
597                 break;
598 /* LCOV_EXCL_STOP */
599         }
600
601         return error;
602 }
603
604 tbm_error_e
605 tbm_module_bufmgr_get_plane_data(tbm_module *module, int format, int plane_idx, uint32_t w, uint32_t h,
606                                         uint32_t *size, uint32_t *offset, uint32_t *pitch, int *bo_idx)
607 {
608         tbm_error_e error = TBM_ERROR_NONE;
609         tbm_backend_bufmgr_func *bufmgr_func = NULL;
610         tbm_bufmgr_backend backend = NULL;
611         int ret = 0;
612
613         TBM_RETURN_VAL_IF_FAIL(module, TBM_ERROR_INVALID_PARAMETER);
614
615         switch (module->type) {
616         case TBM_MODULE_TYPE_HAL_TBM:
617                 error = (tbm_error_e)hal_tbm_bufmgr_get_plane_data(module->hal_bufmgr, format, plane_idx, w, h, size, offset, pitch, bo_idx);
618                 break;
619 /* LCOV_EXCL_START */
620         case TBM_MODULE_TYPE_TBM_BACKEND:
621                 bufmgr_func = module->bufmgr_func;
622                 TBM_RETURN_VAL_IF_FAIL(bufmgr_func, TBM_ERROR_INVALID_OPERATION);
623                 TBM_RETURN_VAL_IF_FAIL(bufmgr_func->bufmgr_get_plane_data, TBM_ERROR_NOT_SUPPORTED);
624
625                 error = bufmgr_func->bufmgr_get_plane_data(module->bufmgr_data, format, plane_idx, w, h, size, offset, pitch, bo_idx);
626                 break;
627         case TBM_MODULE_TYPE_BUFMGR_BACKEND:
628                 TBM_WRN("!!WARNING: This backend interface will be DEPRECATED after Tizen 6.5.");
629                 backend = module->backend;
630                 TBM_RETURN_VAL_IF_FAIL(backend, TBM_ERROR_INVALID_OPERATION);
631                 TBM_RETURN_VAL_IF_FAIL(backend->surface_get_plane_data, TBM_ERROR_NOT_SUPPORTED);
632
633                 ret = backend->surface_get_plane_data(w, h, format, plane_idx, size, offset, pitch, bo_idx);
634                 if (!ret)
635                         error = TBM_ERROR_INVALID_OPERATION;
636                 break;
637         default:
638                 TBM_ERR("Wrong module type:%d", module->type);
639                 error = TBM_ERROR_INVALID_OPERATION;
640                 break;
641 /* LCOV_EXCL_STOP */
642         }
643
644         return error;
645 }
646
647 tbm_backend_bo_data *
648 tbm_module_bufmgr_bo_alloc(tbm_module *module, tbm_bo bo, int size, int flags, tbm_error_e *error)
649 {
650         tbm_backend_bo_data *bo_data = NULL;
651         tbm_backend_bufmgr_func *bufmgr_func = NULL;
652         tbm_bufmgr_backend backend = NULL;
653
654         TBM_RETURN_VAL_SET_ERR_IF_FAIL(module, NULL, *error, TBM_ERROR_INVALID_PARAMETER);
655
656         switch (module->type) {
657         case TBM_MODULE_TYPE_HAL_TBM:
658                 bo_data = (tbm_backend_bo_data *)hal_tbm_bufmgr_alloc_bo(module->hal_bufmgr, size, flags, (hal_tbm_error *)error);
659                 break;
660 /* LCOV_EXCL_START */
661         case TBM_MODULE_TYPE_TBM_BACKEND:
662                 bufmgr_func = module->bufmgr_func;
663                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(bufmgr_func, NULL, *error, TBM_ERROR_INVALID_OPERATION);
664                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(bufmgr_func->bufmgr_alloc_bo, NULL, *error, TBM_ERROR_NOT_SUPPORTED);
665
666                 bo_data = module->bufmgr_func->bufmgr_alloc_bo(module->bufmgr_data, (unsigned int)size, flags, error);
667                 break;
668         case TBM_MODULE_TYPE_BUFMGR_BACKEND:
669                 TBM_WRN("!!WARNING: This backend interface will be DEPRECATED after Tizen 6.5.");
670                 backend = module->backend;
671                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(backend, NULL, *error, TBM_ERROR_INVALID_OPERATION);
672                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(backend->bo_alloc, NULL, *error, TBM_ERROR_NOT_SUPPORTED);
673
674                 bo_data = (void *)module->backend->bo_alloc(bo, size, flags);
675                 *error = TBM_ERROR_NONE;
676                 break;
677         default:
678                 TBM_ERR("Wrong module type:%d", module->type);
679                 *error = TBM_ERROR_INVALID_OPERATION;
680                 break;
681 /* LCOV_EXCL_STOP */
682         }
683
684         return bo_data;
685 }
686
687 tbm_backend_bo_data *
688 tbm_module_bufmgr_bo_alloc_with_format(tbm_module *module, int format, int bo_idx, int width,
689                                                 int height, int bpp, tbm_bo_memory_type flags, tbm_error_e *error)
690 {
691         tbm_backend_bo_data *bo_data = NULL;
692         tbm_backend_bufmgr_func *bufmgr_func = NULL;
693
694         TBM_RETURN_VAL_SET_ERR_IF_FAIL(module, NULL, *error, TBM_ERROR_INVALID_PARAMETER);
695
696         switch (module->type) {
697         case TBM_MODULE_TYPE_HAL_TBM:
698                 bo_data = (tbm_backend_bo_data *)hal_tbm_bufmgr_alloc_bo_with_format(module->hal_bufmgr,
699                                                                                         format, bo_idx, width, height, bpp,
700                                                                                         (hal_tbm_bo_memory_type)flags, (hal_tbm_error *)error);
701                 break;
702 /* LCOV_EXCL_START */
703         case TBM_MODULE_TYPE_TBM_BACKEND:
704                 bufmgr_func = module->bufmgr_func;
705                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(bufmgr_func, NULL, *error, TBM_ERROR_INVALID_OPERATION);
706                 if (!bufmgr_func->bufmgr_alloc_bo_with_format) {
707                         *error = TBM_ERROR_NOT_SUPPORTED;
708                         return NULL;
709                 }
710
711                 bo_data = bufmgr_func->bufmgr_alloc_bo_with_format(module->bufmgr_data, format, bo_idx, width, height, flags, error);
712                 break;
713         case TBM_MODULE_TYPE_BUFMGR_BACKEND:
714                 TBM_WRN("!!WARNING: This backend interface will be DEPRECATED after Tizen 6.5.");
715                 TBM_ERR("error: not supported tbm_bufmgr_internal_alloc_bo_with_format.");
716
717                 *error = TBM_ERROR_NOT_SUPPORTED;
718                 break;
719         default:
720                 TBM_ERR("Wrong module type:%d", module->type);
721                 *error = TBM_ERROR_INVALID_OPERATION;
722                 break;
723 /* LCOV_EXCL_STOP */
724         }
725
726         return bo_data;
727 }
728
729
730 tbm_backend_bo_data *
731 tbm_module_bufmgr_bo_import_fd(tbm_module *module, tbm_bo bo, tbm_fd fd, tbm_error_e *error)
732 {
733         tbm_backend_bo_data *bo_data = NULL;
734         tbm_backend_bufmgr_func *bufmgr_func = NULL;
735         tbm_bufmgr_backend backend = NULL;
736
737         TBM_RETURN_VAL_SET_ERR_IF_FAIL(module, NULL, *error, TBM_ERROR_INVALID_PARAMETER);
738
739         switch (module->type) {
740         case TBM_MODULE_TYPE_HAL_TBM:
741                 bo_data = (tbm_backend_bo_data *)hal_tbm_bufmgr_import_fd(module->hal_bufmgr, (hal_tbm_fd)fd, (hal_tbm_error *)error);
742                 break;
743 /* LCOV_EXCL_START */
744         case TBM_MODULE_TYPE_TBM_BACKEND:
745                 bufmgr_func = module->bufmgr_func;
746                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(bufmgr_func, NULL, *error, TBM_ERROR_INVALID_OPERATION);
747                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(bufmgr_func->bufmgr_import_fd, NULL, *error, TBM_ERROR_NOT_SUPPORTED);
748
749                 bo_data = bufmgr_func->bufmgr_import_fd(module->bufmgr_data, fd, error);
750                 break;
751         case TBM_MODULE_TYPE_BUFMGR_BACKEND:
752                 TBM_WRN("!!WARNING: This backend interface will be DEPRECATED after Tizen 6.5.");
753                 backend = module->backend;
754                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(backend, NULL, *error, TBM_ERROR_INVALID_OPERATION);
755                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(backend->bo_import_fd, NULL, *error, TBM_ERROR_NOT_SUPPORTED);
756
757                 bo_data = (void *)backend->bo_import_fd(bo, fd);
758                 if (!bo_data)
759                         *error = TBM_ERROR_INVALID_OPERATION;
760                 else
761                         *error = TBM_ERROR_NONE;
762                 break;
763         default:
764                 TBM_ERR("Wrong module type:%d", module->type);
765                 *error = TBM_ERROR_INVALID_OPERATION;
766                 break;
767 /* LCOV_EXCL_STOP */
768         }
769
770         return bo_data;
771 }
772
773 tbm_backend_bo_data *
774 tbm_module_bufmgr_bo_import_key(tbm_module *module, tbm_bo bo, tbm_key key, tbm_error_e *error)
775 {
776         tbm_backend_bo_data *bo_data = NULL;
777         tbm_backend_bufmgr_func *bufmgr_func = NULL;
778         tbm_bufmgr_backend backend = NULL;
779
780         TBM_RETURN_VAL_SET_ERR_IF_FAIL(module, NULL, *error, TBM_ERROR_INVALID_PARAMETER);
781
782         switch (module->type) {
783         case TBM_MODULE_TYPE_HAL_TBM:
784                 bo_data = (tbm_backend_bo_data *)hal_tbm_bufmgr_import_key(module->hal_bufmgr, key, (hal_tbm_error *)error);
785                 break;
786 /* LCOV_EXCL_START */
787         case TBM_MODULE_TYPE_TBM_BACKEND:
788                 bufmgr_func = module->bufmgr_func;
789                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(bufmgr_func, NULL, *error, TBM_ERROR_INVALID_OPERATION);
790                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(bufmgr_func->bufmgr_import_key, NULL, *error, TBM_ERROR_NOT_SUPPORTED);
791
792                 bo_data = bufmgr_func->bufmgr_import_key(module->bufmgr_data, key, error);
793                 break;
794         case TBM_MODULE_TYPE_BUFMGR_BACKEND:
795                 TBM_WRN("!!WARNING: This backend interface will be DEPRECATED after Tizen 6.5.");
796                 backend = module->backend;
797                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(backend, NULL, *error, TBM_ERROR_INVALID_OPERATION);
798                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(backend->bo_import, NULL, *error, TBM_ERROR_NOT_SUPPORTED);
799
800                 bo_data = (void *)backend->bo_import(bo, key);
801                 if (!bo_data)
802                         *error = TBM_ERROR_INVALID_OPERATION;
803                 else
804                         *error = TBM_ERROR_NONE;
805                 break;
806         default:
807                 TBM_ERR("Wrong module type:%d", module->type);
808                 *error = TBM_ERROR_INVALID_OPERATION;
809                 break;
810 /* LCOV_EXCL_STOP */
811         }
812
813         return bo_data;
814 }
815
816 void
817 tbm_module_bo_free(tbm_module *module, tbm_bo bo, tbm_backend_bo_data *bo_data, int get_from_hal_surface)
818 {
819         tbm_backend_bo_func *bo_func = NULL;
820         tbm_bufmgr_backend backend = NULL;
821
822         TBM_RETURN_IF_FAIL(module);
823         TBM_RETURN_IF_FAIL(bo);
824         TBM_RETURN_IF_FAIL(bo_data);
825
826         switch (module->type) {
827         case TBM_MODULE_TYPE_HAL_TBM:
828                 // call hal_tbm_bo_free when bo is created by tbm_bo_alloc api.
829                 if (get_from_hal_surface)
830                         hal_tbm_bo_free(bo_data);
831                 break;
832 /* LCOV_EXCL_START */
833         case TBM_MODULE_TYPE_TBM_BACKEND:
834                 bo_func = module->bo_func;
835                 TBM_RETURN_IF_FAIL(bo_func);
836                 TBM_RETURN_IF_FAIL(bo_func->bo_free);
837
838                 bo_func->bo_free(bo_data);
839                 break;
840         case TBM_MODULE_TYPE_BUFMGR_BACKEND:
841                 TBM_WRN("!!WARNING: This backend interface will be DEPRECATED after Tizen 6.5.");
842                 backend = module->backend;
843                 TBM_RETURN_IF_FAIL(backend);
844                 TBM_RETURN_IF_FAIL(backend->bo_free);
845
846                 backend->bo_free(bo);
847                 break;
848         default:
849                 TBM_ERR("Wrong module type:%d", module->type);
850                 break;
851 /* LCOV_EXCL_STOP */
852         }
853 }
854
855 int
856 tbm_module_bo_get_size(tbm_module *module, tbm_bo bo, tbm_backend_bo_data *bo_data, tbm_error_e *error)
857 {
858         tbm_backend_bo_func *bo_func = NULL;
859         tbm_bufmgr_backend backend = NULL;
860         int size = 0;
861
862         TBM_RETURN_VAL_SET_ERR_IF_FAIL(module, 0, *error, TBM_ERROR_INVALID_PARAMETER);
863
864         switch (module->type) {
865         case TBM_MODULE_TYPE_HAL_TBM:
866                 size = hal_tbm_bo_get_size((hal_tbm_bo *)bo_data, (hal_tbm_error *)error);
867                 break;
868 /* LCOV_EXCL_START */
869         case TBM_MODULE_TYPE_TBM_BACKEND:
870                 bo_func = module->bo_func;
871                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(bo_func, 0, *error, TBM_ERROR_INVALID_OPERATION);
872                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(bo_func->bo_get_size, 0, *error, TBM_ERROR_NOT_SUPPORTED);
873
874                 size = bo_func->bo_get_size(bo_data, error);
875                 break;
876         case TBM_MODULE_TYPE_BUFMGR_BACKEND:
877                 TBM_WRN("!!WARNING: This backend interface will be DEPRECATED after Tizen 6.5.");
878                 backend = module->backend;
879                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(backend, 0, *error, TBM_ERROR_INVALID_OPERATION);
880                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(backend->bo_size, 0, *error, TBM_ERROR_NOT_SUPPORTED);
881
882                 size = backend->bo_size(bo);
883                 if (size == 0)
884                         *error = TBM_ERROR_INVALID_OPERATION;
885                 else
886                         *error = TBM_ERROR_NONE;
887                 break;
888         default:
889                 TBM_ERR("Wrong module type:%d", module->type);
890                 *error = TBM_ERROR_INVALID_OPERATION;
891                 break;
892 /* LCOV_EXCL_STOP */
893         }
894
895         return size;
896 }
897
898 int
899 tbm_module_bo_get_memory_types(tbm_module *module, tbm_bo bo, tbm_backend_bo_data *bo_data, tbm_error_e *error)
900 {
901         tbm_backend_bo_func *bo_func = NULL;
902         tbm_bufmgr_backend backend = NULL;
903         int memory_types = TBM_BO_DEFAULT;
904
905         TBM_RETURN_VAL_SET_ERR_IF_FAIL(module, 0, *error, TBM_ERROR_INVALID_PARAMETER);
906
907         switch (module->type) {
908         case TBM_MODULE_TYPE_HAL_TBM:
909                 memory_types = (tbm_bo_memory_type)hal_tbm_bo_get_memory_types((hal_tbm_bo *)bo_data, (hal_tbm_error *)error);
910                 break;
911 /* LCOV_EXCL_START */
912         case TBM_MODULE_TYPE_TBM_BACKEND:
913                 bo_func = module->bo_func;
914                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(bo_func, 0, *error, TBM_ERROR_INVALID_OPERATION);
915                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(bo_func->bo_get_memory_types, 0, *error, TBM_ERROR_NOT_SUPPORTED);
916
917                 memory_types = bo_func->bo_get_memory_types(bo_data, error);
918                 break;
919         case TBM_MODULE_TYPE_BUFMGR_BACKEND:
920                 TBM_WRN("!!WARNING: This backend interface will be DEPRECATED after Tizen 6.5.");
921                 backend = module->backend;
922                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(backend, 0, *error, TBM_ERROR_INVALID_OPERATION);
923                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(backend->bo_get_flags, 0, *error, TBM_ERROR_NOT_SUPPORTED);
924
925                 memory_types = backend->bo_get_flags(bo);
926                 *error = TBM_ERROR_NONE;
927                 break;
928         default:
929                 TBM_ERR("Wrong module type:%d", module->type);
930                 *error = TBM_ERROR_INVALID_OPERATION;
931                 break;
932 /* LCOV_EXCL_STOP */
933         }
934
935         return memory_types;
936 }
937
938 tbm_bo_handle
939 tbm_module_bo_get_handle(tbm_module *module, tbm_bo bo, tbm_backend_bo_data *bo_data, int device, tbm_error_e *error)
940 {
941         tbm_backend_bo_func *bo_func = NULL;
942         tbm_bufmgr_backend backend = NULL;
943         tbm_bo_handle bo_handle;
944         hal_tbm_bo_handle hbo_handle;
945
946         TBM_RETURN_VAL_SET_ERR_IF_FAIL(module, (tbm_bo_handle)NULL, *error, TBM_ERROR_INVALID_PARAMETER);
947
948         switch (module->type) {
949         case TBM_MODULE_TYPE_HAL_TBM:
950                 hbo_handle = hal_tbm_bo_get_handle((hal_tbm_bo *)bo_data, device, (hal_tbm_error *)error);
951                 if (hbo_handle.ptr != NULL)
952                         memcpy(&bo_handle.ptr, &hbo_handle.ptr, sizeof(tbm_bo_handle));
953                 break;
954 /* LCOV_EXCL_START */
955         case TBM_MODULE_TYPE_TBM_BACKEND:
956                 bo_func = module->bo_func;
957                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(bo_func, (tbm_bo_handle)NULL, *error, TBM_ERROR_INVALID_OPERATION);
958                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(bo_func->bo_get_handle, (tbm_bo_handle)NULL, *error, TBM_ERROR_NOT_SUPPORTED);
959
960                 bo_handle = bo_func->bo_get_handle(bo_data, device, error);
961                 break;
962         case TBM_MODULE_TYPE_BUFMGR_BACKEND:
963                 TBM_WRN("!!WARNING: This backend interface will be DEPRECATED after Tizen 6.5.");
964                 backend = module->backend;
965                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(backend, (tbm_bo_handle)NULL, *error, TBM_ERROR_INVALID_OPERATION);
966                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(backend->bo_get_handle, (tbm_bo_handle)NULL, *error, TBM_ERROR_NOT_SUPPORTED);
967
968                 bo_handle = backend->bo_get_handle(bo, device);
969                 if (!bo_handle.ptr)
970                         *error = TBM_ERROR_INVALID_OPERATION;
971                 else
972                         *error = TBM_ERROR_NONE;
973                 break;
974         default:
975                 TBM_ERR("Wrong module type:%d", module->type);
976                 bo_handle.ptr = NULL;
977                 *error = TBM_ERROR_INVALID_OPERATION;
978                 break;
979 /* LCOV_EXCL_STOP */
980         }
981
982         return bo_handle;
983 }
984
985 tbm_bo_handle
986 tbm_module_bo_map(tbm_module *module, tbm_bo bo, tbm_backend_bo_data *bo_data, int device, int opt, tbm_error_e *error)
987 {
988         tbm_backend_bo_func *bo_func = NULL;
989         tbm_bufmgr_backend backend = NULL;
990         tbm_bo_handle bo_handle;
991         hal_tbm_bo_handle hbo_handle;
992
993         TBM_RETURN_VAL_SET_ERR_IF_FAIL(module, (tbm_bo_handle)NULL, *error, TBM_ERROR_INVALID_PARAMETER);
994
995         switch (module->type) {
996         case TBM_MODULE_TYPE_HAL_TBM:
997                 hbo_handle = hal_tbm_bo_map((hal_tbm_bo *)bo_data, device, opt, (hal_tbm_error *)error);
998                 if (hbo_handle.ptr != NULL)
999                         memcpy(&bo_handle.ptr, &hbo_handle.ptr, sizeof(tbm_bo_handle));
1000                 break;
1001 /* LCOV_EXCL_START */
1002         case TBM_MODULE_TYPE_TBM_BACKEND:
1003                 bo_func = module->bo_func;
1004                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(bo_func, (tbm_bo_handle)NULL, *error, TBM_ERROR_INVALID_OPERATION);
1005                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(bo_func->bo_map, (tbm_bo_handle)NULL, *error, TBM_ERROR_NOT_SUPPORTED);
1006
1007                 bo_handle = bo_func->bo_map(bo_data, device, opt, error);
1008                 break;
1009         case TBM_MODULE_TYPE_BUFMGR_BACKEND:
1010                 TBM_WRN("!!WARNING: This backend interface will be DEPRECATED after Tizen 6.5.");
1011                 backend = module->backend;
1012                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(backend, (tbm_bo_handle)NULL, *error, TBM_ERROR_INVALID_OPERATION);
1013                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(backend->bo_map, (tbm_bo_handle)NULL, *error, TBM_ERROR_NOT_SUPPORTED);
1014
1015                 bo_handle = backend->bo_map(bo, device, opt);
1016                 if (!bo_handle.ptr)
1017                         *error = TBM_ERROR_INVALID_OPERATION;
1018                 else
1019                         *error = TBM_ERROR_NONE;
1020                 break;
1021         default:
1022                 TBM_ERR("Wrong module type:%d", module->type);
1023                 bo_handle.ptr = NULL;
1024                 *error = TBM_ERROR_INVALID_OPERATION;
1025                 break;
1026 /* LCOV_EXCL_STOP */
1027         }
1028
1029         return bo_handle;
1030 }
1031
1032 tbm_error_e
1033 tbm_module_bo_unmap(tbm_module *module, tbm_bo bo, tbm_backend_bo_data *bo_data)
1034 {
1035         tbm_backend_bo_func *bo_func = NULL;
1036         tbm_bufmgr_backend backend = NULL;
1037         tbm_error_e error;
1038         int ret = 0;
1039
1040         TBM_RETURN_VAL_IF_FAIL(module, TBM_ERROR_INVALID_PARAMETER);
1041
1042         switch (module->type) {
1043         case TBM_MODULE_TYPE_HAL_TBM:
1044                 error = (hal_tbm_error)hal_tbm_bo_unmap((hal_tbm_bo *)bo_data);
1045                 break;
1046 /* LCOV_EXCL_START */
1047         case TBM_MODULE_TYPE_TBM_BACKEND:
1048                 bo_func = module->bo_func;
1049                 TBM_RETURN_VAL_IF_FAIL(bo_func, TBM_ERROR_INVALID_OPERATION);
1050                 TBM_RETURN_VAL_IF_FAIL(bo_func->bo_unmap, TBM_ERROR_NOT_SUPPORTED);
1051
1052                 error = bo_func->bo_unmap(bo->bo_data);
1053                 break;
1054         case TBM_MODULE_TYPE_BUFMGR_BACKEND:
1055                 TBM_WRN("!!WARNING: This backend interface will be DEPRECATED after Tizen 6.5.");
1056                 backend = module->backend;
1057                 TBM_RETURN_VAL_IF_FAIL(backend, TBM_ERROR_INVALID_OPERATION);
1058                 TBM_RETURN_VAL_IF_FAIL(backend->bo_unmap, TBM_ERROR_NOT_SUPPORTED);
1059
1060                 ret = bo->bufmgr->backend->bo_unmap(bo);
1061                 if (!ret)
1062                         error = TBM_ERROR_INVALID_OPERATION;
1063                 else
1064                         error = TBM_ERROR_NONE;
1065                 break;
1066         default:
1067                 TBM_ERR("Wrong module type:%d", module->type);
1068                 error = TBM_ERROR_INVALID_OPERATION;
1069                 break;
1070 /* LCOV_EXCL_STOP */
1071         }
1072
1073         return error;
1074 }
1075
1076 tbm_error_e
1077 tbm_module_bo_lock(tbm_module *module, tbm_bo bo, tbm_backend_bo_data *bo_data, int device, int opt)
1078 {
1079         tbm_backend_bo_func *bo_func = NULL;
1080         tbm_bufmgr_backend backend = NULL;
1081         tbm_error_e error;
1082         int ret = 0;
1083
1084         TBM_RETURN_VAL_IF_FAIL(module, TBM_ERROR_INVALID_PARAMETER);
1085
1086         switch (module->type) {
1087         case TBM_MODULE_TYPE_HAL_TBM:
1088                 error = (tbm_error_e)hal_tbm_bo_lock((hal_tbm_bo *)bo_data, device, opt);
1089                 break;
1090 /* LCOV_EXCL_START */
1091         case TBM_MODULE_TYPE_TBM_BACKEND:
1092                 bo_func = module->bo_func;
1093                 TBM_RETURN_VAL_IF_FAIL(bo_func, TBM_ERROR_INVALID_OPERATION);
1094                 if (!bo_func->bo_lock)
1095                         return TBM_ERROR_NOT_SUPPORTED;
1096
1097                 error = bo_func->bo_lock(bo_data, device, opt);
1098                 break;
1099         case TBM_MODULE_TYPE_BUFMGR_BACKEND:
1100                 TBM_WRN("!!WARNING: This backend interface will be DEPRECATED after Tizen 6.5.");
1101                 backend = module->backend;
1102                 TBM_RETURN_VAL_IF_FAIL(backend, TBM_ERROR_INVALID_OPERATION);
1103                 if (!backend->bo_unmap)
1104                         return TBM_ERROR_NOT_SUPPORTED;
1105
1106                 ret = backend->bo_lock(bo, device, opt);
1107                 if (!ret)
1108                         error = TBM_ERROR_INVALID_OPERATION;
1109                 else
1110                         error = TBM_ERROR_NONE;
1111                 break;
1112         default:
1113                 TBM_ERR("Wrong module type:%d", module->type);
1114                 error = TBM_ERROR_INVALID_OPERATION;
1115                 break;
1116 /* LCOV_EXCL_STOP */
1117         }
1118
1119         return error;
1120 }
1121
1122 tbm_error_e
1123 tbm_module_bo_unlock(tbm_module *module, tbm_bo bo, tbm_backend_bo_data *bo_data)
1124 {
1125         tbm_backend_bo_func *bo_func = NULL;
1126         tbm_bufmgr_backend backend = NULL;
1127         tbm_error_e error;
1128
1129         TBM_RETURN_VAL_IF_FAIL(module, TBM_ERROR_INVALID_PARAMETER);
1130
1131         switch (module->type) {
1132         case TBM_MODULE_TYPE_HAL_TBM:
1133                 error = (tbm_error_e)hal_tbm_bo_unlock((hal_tbm_bo *)bo_data);
1134                 break;
1135 /* LCOV_EXCL_START */
1136         case TBM_MODULE_TYPE_TBM_BACKEND:
1137                 bo_func = module->bo_func;
1138                 TBM_RETURN_VAL_IF_FAIL(bo_func, TBM_ERROR_INVALID_OPERATION);
1139                 if (!bo_func->bo_unlock)
1140                         return TBM_ERROR_NOT_SUPPORTED;
1141
1142                 error = bo_func->bo_unlock(bo->bo_data);
1143                 break;
1144         case TBM_MODULE_TYPE_BUFMGR_BACKEND:
1145                 TBM_WRN("!!WARNING: This backend interface will be DEPRECATED after Tizen 6.5.");
1146                 backend = module->backend;
1147                 TBM_RETURN_VAL_IF_FAIL(backend, TBM_ERROR_INVALID_OPERATION);
1148                 if (!backend->bo_unlock)
1149                         return TBM_ERROR_NOT_SUPPORTED;
1150
1151                 backend->bo_unlock(bo);
1152                 error = TBM_ERROR_NONE;
1153                 break;
1154         default:
1155                 TBM_ERR("Wrong module type:%d", module->type);
1156                 error = TBM_ERROR_INVALID_OPERATION;
1157                 break;
1158 /* LCOV_EXCL_STOP */
1159         }
1160
1161         return error;
1162 }
1163
1164 tbm_fd
1165 tbm_module_bo_export_fd(tbm_module *module, tbm_bo bo, tbm_backend_bo_data *bo_data, tbm_error_e *error)
1166 {
1167         tbm_backend_bo_func *bo_func = NULL;
1168         tbm_bufmgr_backend backend = NULL;
1169         tbm_fd fd;
1170
1171         TBM_RETURN_VAL_SET_ERR_IF_FAIL(module, -1, *error, TBM_ERROR_INVALID_PARAMETER);
1172
1173         switch (module->type) {
1174         case TBM_MODULE_TYPE_HAL_TBM:
1175                 fd = (hal_tbm_fd)hal_tbm_bo_export_fd((hal_tbm_bo *)bo_data, (hal_tbm_error *)error);
1176                 break;
1177 /* LCOV_EXCL_START */
1178         case TBM_MODULE_TYPE_TBM_BACKEND:
1179                 bo_func = module->bo_func;
1180                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(bo_func, -1, *error, TBM_ERROR_INVALID_OPERATION);
1181                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(bo_func->bo_export_fd, -1, *error, TBM_ERROR_NOT_SUPPORTED);
1182
1183                 fd = bo_func->bo_export_fd(bo_data, error);
1184                 break;
1185         case TBM_MODULE_TYPE_BUFMGR_BACKEND:
1186                 TBM_WRN("!!WARNING: This backend interface will be DEPRECATED after Tizen 6.5.");
1187                 backend = module->backend;
1188                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(backend, -1, *error, TBM_ERROR_INVALID_OPERATION);
1189                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(backend->bo_export_fd, -1, *error, TBM_ERROR_NOT_SUPPORTED);
1190
1191                 fd = backend->bo_export_fd(bo);
1192                 if (fd < 0)
1193                         *error = TBM_ERROR_INVALID_OPERATION;
1194                 else
1195                         *error = TBM_ERROR_NONE;
1196                 break;
1197         default:
1198                 TBM_ERR("Wrong module type:%d", module->type);
1199                 fd = -1;
1200                 *error = TBM_ERROR_INVALID_OPERATION;
1201                 break;
1202 /* LCOV_EXCL_STOP */
1203         }
1204
1205         return fd;
1206 }
1207
1208 tbm_key
1209 tbm_module_bo_export_key(tbm_module *module, tbm_bo bo, tbm_backend_bo_data *bo_data, tbm_error_e *error)
1210 {
1211         tbm_backend_bo_func *bo_func = NULL;
1212         tbm_bufmgr_backend backend = NULL;
1213         tbm_key ret;
1214
1215         TBM_RETURN_VAL_SET_ERR_IF_FAIL(module, 0, *error, TBM_ERROR_INVALID_PARAMETER);
1216
1217         switch (module->type) {
1218         case TBM_MODULE_TYPE_HAL_TBM:
1219                 ret = (hal_tbm_fd)hal_tbm_bo_export_key((hal_tbm_bo *)bo_data, (hal_tbm_error *)error);
1220                 break;
1221 /* LCOV_EXCL_START */
1222         case TBM_MODULE_TYPE_TBM_BACKEND:
1223                 bo_func = module->bo_func;
1224                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(bo_func, 0, *error, TBM_ERROR_INVALID_OPERATION);
1225                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(bo_func->bo_export_key, 0, *error, TBM_ERROR_NOT_SUPPORTED);
1226
1227                 ret = bo_func->bo_export_key(bo_data, error);
1228                 break;
1229         case TBM_MODULE_TYPE_BUFMGR_BACKEND:
1230                 TBM_WRN("!!WARNING: This backend interface will be DEPRECATED after Tizen 6.5.");
1231                 backend = module->backend;
1232                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(backend, 0, *error, TBM_ERROR_INVALID_OPERATION);
1233                 TBM_RETURN_VAL_SET_ERR_IF_FAIL(backend->bo_export, 0, *error, TBM_ERROR_NOT_SUPPORTED);
1234
1235                 ret = backend->bo_export(bo);
1236                 if (!ret)
1237                         *error = TBM_ERROR_INVALID_OPERATION;
1238                 else
1239                         *error = TBM_ERROR_NONE;
1240                 break;
1241         default:
1242                 TBM_ERR("Wrong module type:%d", module->type);
1243                 ret = -1;
1244                 *error = TBM_ERROR_INVALID_OPERATION;
1245                 break;
1246 /* LCOV_EXCL_STOP */
1247         }
1248
1249         return ret;
1250 }