Fix prevent issues
[platform/core/uifw/libtbm.git] / src / tbm_bufmgr.c
1 /**************************************************************************
2
3 libtbm
4
5 Copyright 2012 Samsung Electronics co., Ltd. All Rights Reserved.
6
7 Contact: SooChan Lim <sc1.lim@samsung.com>, Sangjin Lee <lsj119@samsung.com>
8 Boram Park <boram1288.park@samsung.com>, 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.h"
35 #include "tbm_bufmgr_int.h"
36 #include "tbm_bufmgr_backend.h"
37 #include "tbm_bufmgr_tgl.h"
38 #include "list.h"
39
40 #define DEBUG
41 #ifdef DEBUG
42 int bDebug = 0;
43 #define DBG(...) if(bDebug&0x1) TBM_LOG (__VA_ARGS__)
44 #define DBG_LOCK(...) if(bDebug&0x2) TBM_LOG (__VA_ARGS__)
45 #else
46 #define DBG(...)
47 #define DBG_LOCK(...)
48 #endif
49
50 #define PREFIX_LIB    "libtbm_"
51 #define SUFFIX_LIB    ".so"
52 #define DEFAULT_LIB   PREFIX_LIB"default"SUFFIX_LIB
53
54 #define BO_IS_CACHEABLE(bo) ((bo->flags & TBM_BO_NONCACHABLE)?0:1)
55 #define DEVICE_IS_CACHE_AWARE(device) ((device == TBM_DEVICE_CPU)?(1):(0))
56
57 /* tgl key values */
58 #define GLOBAL_KEY   ((unsigned int)(-1))
59 #define INITIAL_KEY  ((unsigned int)(-2))
60
61 #define CACHE_OP_CREATE     (-1)
62 #define CACHE_OP_ATTACH     (-2)
63 #define CACHE_OP_IMPORT     (-3)
64
65 /* values to indicate unspecified fields in XF86ModReqInfo. */
66 #define MAJOR_UNSPEC        0xFF
67 #define MINOR_UNSPEC        0xFF
68 #define PATCH_UNSPEC        0xFFFF
69 #define ABI_VERS_UNSPEC   0xFFFFFFFF
70
71 #define MODULE_VERSION_NUMERIC(maj, min, patch) \
72                ((((maj) & 0xFF) << 24) | (((min) & 0xFF) << 16) | (patch & 0xFFFF))
73 #define GET_MODULE_MAJOR_VERSION(vers)    (((vers) >> 24) & 0xFF)
74 #define GET_MODULE_MINOR_VERSION(vers)    (((vers) >> 16) & 0xFF)
75 #define GET_MODULE_PATCHLEVEL(vers)    ((vers) & 0xFFFF)
76
77 enum {
78     LOCK_TRY_ONCE,
79     LOCK_TRY_ALWAYS,
80     LOCK_TRY_NEVER
81 };
82
83 enum {
84     DEVICE_NONE = 0,
85     DEVICE_CA,       /* cache aware device */
86     DEVICE_CO        /* cache oblivious device */
87 };
88
89 typedef struct
90 {
91     unsigned long key;
92     void *data;
93     tbm_data_free free_func ;
94
95     /* link of user_data */
96     struct list_head item_link;
97 } tbm_user_data;
98
99 pthread_mutex_t gLock = PTHREAD_MUTEX_INITIALIZER;
100 tbm_bufmgr gBufMgr = NULL;
101
102 static __thread tbm_error_e tbm_last_error = TBM_ERROR_NONE;
103
104 static void
105 _tbm_set_last_result(tbm_error_e err)
106 {
107         tbm_last_error = err;
108 }
109
110 static inline int
111 _tgl_init (int fd, unsigned int key)
112 {
113     struct tgl_attribute attr;
114     int err;
115
116     attr.key = key;
117     attr.timeout_ms = 1000;
118
119     err = ioctl (fd, TGL_IOC_INIT_LOCK, &attr);
120     if (err)
121     {
122         TBM_LOG ( "[libtbm:%d] "
123                 "error(%s) %s:%d key:%d\n",
124                 getpid(), strerror(errno), __FUNCTION__, __LINE__, key);
125         return 0;
126     }
127
128     return 1;
129 }
130
131 static inline int
132 _tgl_destroy (int fd, unsigned int key)
133 {
134     int err;
135     err = ioctl (fd, TGL_IOC_DESTROY_LOCK, key);
136     if (err)
137     {
138         TBM_LOG ( "[libtbm:%d] "
139                 "error(%s) %s:%d key:%d\n",
140                 getpid(), strerror(errno), __FUNCTION__, __LINE__, key);
141         return 0;
142     }
143
144     return 1;
145 }
146
147 static inline int
148 _tgl_lock (int fd, unsigned int key)
149 {
150     int err;
151     err = ioctl (fd, TGL_IOC_LOCK_LOCK, key);
152     if (err)
153     {
154         TBM_LOG ("[libtbm:%d] "
155                 "error(%s) %s:%d key:%d\n",
156                 getpid(), strerror(errno), __FUNCTION__, __LINE__, key);
157         return 0;
158     }
159
160     return 1;
161 }
162
163 static inline int
164 _tgl_unlock (int fd, unsigned int key)
165 {
166     int err;
167     err = ioctl (fd, TGL_IOC_UNLOCK_LOCK, key);
168     if (err)
169     {
170         TBM_LOG ("[libtbm:%d] "
171                 "error(%s) %s:%d key:%d\n",
172                 getpid(), strerror(errno), __FUNCTION__, __LINE__, key);
173         return 0;
174     }
175
176     return 1;
177 }
178
179 static inline int
180 _tgl_set_data (int fd, unsigned int key, unsigned int val)
181 {
182     int err;
183     struct tgl_user_data arg;
184
185     arg.key = key;
186     arg.data1 = val;
187     err = ioctl (fd, TGL_IOC_SET_DATA, &arg);
188     if (err)
189     {
190         TBM_LOG ("[libtbm:%d] "
191                 "error(%s) %s:%d key:%d\n",
192                 getpid(), strerror(errno), __FUNCTION__, __LINE__, key);
193         return 0;
194     }
195
196     return 1;
197 }
198
199 static inline unsigned int
200 _tgl_get_data (int fd, unsigned int key, unsigned int *locked)
201 {
202     int err;
203     struct tgl_user_data arg = {0,};
204
205     arg.key = key;
206     err = ioctl (fd, TGL_IOC_GET_DATA, &arg);
207     if (err)
208     {
209         TBM_LOG ("[libtbm:%d] "
210                 "error(%s) %s:%d key:%d\n",
211                 getpid(), strerror(errno), __FUNCTION__, __LINE__, key);
212         return 0;
213     }
214
215     if (locked)
216         *locked = arg.locked;
217
218     return arg.data1;
219 }
220
221 static tbm_user_data *
222 _user_data_lookup (struct list_head *user_data_list, unsigned long key)
223 {
224     tbm_user_data *user_data = NULL;
225     tbm_user_data *old_data = NULL, *tmp = NULL;
226
227     if (!LIST_IS_EMPTY (user_data_list))
228     {
229         LIST_FOR_EACH_ENTRY_SAFE (old_data, tmp, user_data_list, item_link)
230         {
231             if (old_data->key == key)
232             {
233                 user_data = old_data;
234                 return user_data;
235             }
236         }
237     }
238
239     return user_data;
240 }
241
242 static tbm_user_data *
243 _user_data_create (unsigned long key, tbm_data_free data_free_func)
244 {
245     tbm_user_data * user_data = NULL;
246
247     user_data = calloc (1, sizeof (tbm_user_data));
248     if (!user_data)
249         return NULL;
250
251     user_data->key = key;
252     user_data->free_func = data_free_func;
253     user_data->data = (void *)0;
254
255     return user_data;
256 }
257
258 static void
259 _user_data_delete (tbm_user_data *user_data)
260 {
261     if (user_data->data && user_data->free_func)
262         user_data->free_func(user_data->data);
263
264     LIST_DEL (&user_data->item_link);
265
266     free(user_data);
267 }
268
269 static int
270 _bo_lock (tbm_bo bo, int device, int opt)
271 {
272     tbm_bufmgr bufmgr = bo->bufmgr;
273     int ret = 0;
274
275     if (TBM_LOCK_CTRL_BACKEND_VALID(bufmgr->backend->flags))
276     {
277         if (bufmgr->backend->bo_lock2)
278         {
279             /* use bo_lock2 backend lock */
280             ret = bufmgr->backend->bo_lock2 (bo, device, opt);
281         }
282         else if (bufmgr->backend->bo_lock)
283         {
284             /* use bo_lock backend lock */
285             ret = bufmgr->backend->bo_lock (bo);
286         }
287         else
288             TBM_LOG ("[libtbm:%d] "
289                 "error %s:%d no backend lock functions\n",
290                 getpid(), __FUNCTION__, __LINE__);
291     }
292     else
293     {
294         /* use tizen global lock */
295         ret = _tgl_lock (bufmgr->lock_fd, bo->tgl_key);
296     }
297
298     return ret;
299 }
300
301 static void
302 _bo_unlock (tbm_bo bo)
303 {
304     tbm_bufmgr bufmgr = bo->bufmgr;
305
306     if (TBM_LOCK_CTRL_BACKEND_VALID(bufmgr->backend->flags))
307     {
308         if (bufmgr->backend->bo_unlock)
309         {
310             /* use backend unlock */
311             bufmgr->backend->bo_unlock (bo);
312         }
313         else
314             TBM_LOG ("[libtbm:%d] "
315                 "error %s:%d no backend unlock functions\n",
316                 getpid(), __FUNCTION__, __LINE__);
317     }
318     else
319     {
320         /* use tizen global unlock */
321         _tgl_unlock (bufmgr->lock_fd, bo->tgl_key);
322     }
323 }
324
325 static int
326 _tbm_bo_init_state (tbm_bo bo, int opt)
327 {
328     tbm_bufmgr bufmgr = bo->bufmgr;
329     tbm_bo_cache_state cache_state;
330
331     if (bo->tgl_key == INITIAL_KEY)
332         bo->tgl_key = bufmgr->backend->bo_get_global_key (bo);
333
334     if (!bo->default_handle.u32)
335         bo->default_handle = bufmgr->backend->bo_get_handle (bo, TBM_DEVICE_DEFAULT);
336
337     RETURN_VAL_CHECK_FLAG (TBM_ALL_CTRL_BACKEND_VALID(bufmgr->backend->flags), 1);
338
339     cache_state.val = 0;
340     switch (opt)
341     {
342     case CACHE_OP_CREATE:    /*Create*/
343
344         _tgl_init (bufmgr->lock_fd, bo->tgl_key);
345
346         cache_state.data.isCacheable = BO_IS_CACHEABLE(bo);
347         cache_state.data.isDirtied = DEVICE_NONE;
348         cache_state.data.isCached = 0;
349         cache_state.data.cntFlush = 0;
350
351         _tgl_set_data (bufmgr->lock_fd, bo->tgl_key, cache_state.val);
352         break;
353     case CACHE_OP_IMPORT:    /*Import*/
354
355         _tgl_init (bufmgr->lock_fd, bo->tgl_key);
356         break;
357     default:
358         break;
359     }
360
361     return 1;
362 }
363
364 static void
365 _tbm_bo_destroy_state (tbm_bo bo)
366 {
367     tbm_bufmgr bufmgr = bo->bufmgr;
368
369     RETURN_CHECK_FLAG (TBM_ALL_CTRL_BACKEND_VALID(bufmgr->backend->flags));
370
371     _tgl_destroy (bufmgr->lock_fd, bo->tgl_key);
372 }
373
374 static int
375 _tbm_bo_set_state (tbm_bo bo, int device, int opt)
376 {
377     tbm_bufmgr bufmgr = bo->bufmgr;
378     char need_flush = 0;
379     unsigned short cntFlush = 0;
380     unsigned int is_locked;
381
382     RETURN_VAL_CHECK_FLAG (TBM_CACHE_CTRL_BACKEND_VALID(bufmgr->backend->flags), 1);
383
384     /* get cache state of a bo */
385     bo->cache_state.val = _tgl_get_data (bufmgr->lock_fd, bo->tgl_key, &is_locked);
386
387     if (!bo->cache_state.data.isCacheable)
388         return 1;
389
390     /* get global cache flush count */
391     cntFlush = (unsigned short)_tgl_get_data (bufmgr->lock_fd, GLOBAL_KEY, NULL);
392
393     if (DEVICE_IS_CACHE_AWARE (device))
394     {
395         if (bo->cache_state.data.isDirtied == DEVICE_CO &&
396             bo->cache_state.data.isCached)
397         {
398             need_flush = TBM_CACHE_INV;
399         }
400
401         bo->cache_state.data.isCached = 1;
402         if (opt & TBM_OPTION_WRITE)
403             bo->cache_state.data.isDirtied = DEVICE_CA;
404         else
405         {
406             if( bo->cache_state.data.isDirtied != DEVICE_CA )
407                 bo->cache_state.data.isDirtied = DEVICE_NONE;
408         }
409     }
410     else
411     {
412         if (bo->cache_state.data.isDirtied == DEVICE_CA &&
413             bo->cache_state.data.isCached &&
414             bo->cache_state.data.cntFlush == cntFlush)
415         {
416             need_flush = TBM_CACHE_CLN | TBM_CACHE_ALL;
417         }
418
419         if (opt & TBM_OPTION_WRITE)
420             bo->cache_state.data.isDirtied = DEVICE_CO;
421         else
422         {
423             if( bo->cache_state.data.isDirtied != DEVICE_CO )
424                 bo->cache_state.data.isDirtied = DEVICE_NONE;
425         }
426     }
427
428     if (need_flush)
429     {
430         /* set global cache flush count */
431         if (need_flush & TBM_CACHE_ALL)
432             _tgl_set_data (bufmgr->lock_fd, GLOBAL_KEY, (unsigned int)(++cntFlush));
433
434         /* call backend cache flush */
435         bufmgr->backend->bo_cache_flush (bo, need_flush);
436
437         DBG ("[libtbm:%d] \tcache(%d,%d,%d)....flush:0x%x, cntFlush(%d)\n", getpid(),
438              bo->cache_state.data.isCacheable,
439              bo->cache_state.data.isCached,
440              bo->cache_state.data.isDirtied,
441              need_flush, cntFlush);
442     }
443
444     return 1;
445 }
446
447 static void
448 _tbm_bo_save_state (tbm_bo bo)
449 {
450     tbm_bufmgr bufmgr = bo->bufmgr;
451     unsigned short cntFlush = 0;
452
453     RETURN_CHECK_FLAG (TBM_CACHE_CTRL_BACKEND_VALID(bufmgr->backend->flags));
454
455     /* get global cache flush count */
456     cntFlush = (unsigned short)_tgl_get_data (bufmgr->lock_fd, GLOBAL_KEY, NULL);
457
458     /* save global cache flush count */
459     bo->cache_state.data.cntFlush = cntFlush;
460     _tgl_set_data(bufmgr->lock_fd, bo->tgl_key, bo->cache_state.val);
461 }
462
463
464 static int
465 _tbm_bo_lock (tbm_bo bo, int device, int opt)
466 {
467     tbm_bufmgr bufmgr = NULL;
468     int old;
469     int ret = 0;
470
471     if (!bo)
472         return 0;
473
474     bufmgr = bo->bufmgr;
475
476     /* do not try to lock the bo */
477     if (bufmgr->lock_type == LOCK_TRY_NEVER)
478         return 1;
479
480     if (bo->lock_cnt < 0)
481     {
482         TBM_LOG ("[libtbm:%d] "
483                 "error %s:%d bo:%p(%d) LOCK_CNT=%d\n",
484                 getpid(), __FUNCTION__, __LINE__, bo, bo->tgl_key, bo->lock_cnt);
485     }
486
487     old = bo->lock_cnt;
488     if (bufmgr->lock_type == LOCK_TRY_ONCE)
489     {
490         if (bo->lock_cnt == 0)
491         {
492             pthread_mutex_unlock (&bufmgr->lock);
493             ret = _bo_lock (bo, device, opt);
494             pthread_mutex_lock (&bufmgr->lock);
495             if (ret)
496                 bo->lock_cnt++;
497         }
498         else
499             ret = 1;
500     }
501     else if (bufmgr->lock_type == LOCK_TRY_ALWAYS)
502     {
503         pthread_mutex_unlock (&bufmgr->lock);
504         ret = _bo_lock (bo, device, opt);
505         pthread_mutex_lock (&bufmgr->lock);
506         if(ret)
507             bo->lock_cnt++;
508     }
509     else
510         TBM_LOG ("[libtbm:%d] "
511                 "error %s:%d bo:%p lock_type is wrong.\n",
512                 getpid(), __FUNCTION__, __LINE__, bo);
513
514     DBG_LOCK ("[libtbm:%d] >> LOCK bo:%p(%d, %d->%d)\n", getpid(),
515             bo, bo->tgl_key, old, bo->lock_cnt);
516
517     return ret;
518 }
519
520 static void
521 _tbm_bo_unlock (tbm_bo bo)
522 {
523     tbm_bufmgr bufmgr = NULL;
524
525     int old;
526
527     if (!bo)
528         return;
529
530     bufmgr = bo->bufmgr;
531
532     /* do not try to unlock the bo */
533     if (bufmgr->lock_type == LOCK_TRY_NEVER)
534         return;
535
536     old = bo->lock_cnt;
537     if (bufmgr->lock_type == LOCK_TRY_ONCE)
538     {
539         if (bo->lock_cnt > 0)
540         {
541             bo->lock_cnt--;
542             if (bo->lock_cnt == 0)
543                _bo_unlock (bo);
544         }
545     }
546     else if (bufmgr->lock_type == LOCK_TRY_ALWAYS)
547     {
548         if (bo->lock_cnt > 0)
549         {
550             bo->lock_cnt--;
551             _bo_unlock (bo);
552         }
553     }
554     else
555         TBM_LOG ("[libtbm:%d] "
556                 "error %s:%d bo:%p lock_type is wrong.\n",
557                 getpid(), __FUNCTION__, __LINE__, bo);
558
559     if (bo->lock_cnt < 0)
560         bo->lock_cnt = 0;
561
562     DBG_LOCK ("[libtbm:%d] << unlock bo:%p(%d, %d->%d)\n", getpid(),
563              bo, bo->tgl_key, old, bo->lock_cnt);
564 }
565
566 static int
567 _tbm_bo_is_valid(tbm_bo bo)
568 {
569     tbm_bo old_data=NULL, tmp = NULL;;
570
571     if( bo == NULL )
572         return 0;
573
574     if(!LIST_IS_EMPTY (&gBufMgr->bo_list))
575     {
576                 LIST_FOR_EACH_ENTRY_SAFE (old_data, tmp, &gBufMgr->bo_list, item_link)
577                 {
578                         if(old_data == bo)
579                         {
580                                 return 1;
581                         }
582                 }
583
584     }
585     return 0;
586 }
587
588 static void
589 _tbm_bo_ref (tbm_bo bo)
590 {
591     bo->ref_cnt++;
592 }
593
594 static void
595 _tbm_bo_unref (tbm_bo bo)
596 {
597     tbm_bufmgr bufmgr = bo->bufmgr;
598     tbm_user_data *old_data = NULL, *tmp = NULL;
599
600     if (bo->ref_cnt <= 0)
601         return;
602
603     bo->ref_cnt--;
604     if (bo->ref_cnt == 0)
605     {
606         /* destory the user_data_list */
607         if (!LIST_IS_EMPTY (&bo->user_data_list))
608         {
609             LIST_FOR_EACH_ENTRY_SAFE (old_data, tmp, &bo->user_data_list, item_link)
610             {
611                 DBG ("[libtbm:%d] free user_data \n", getpid());
612                 _user_data_delete (old_data);
613             }
614         }
615
616         if (bo->lock_cnt > 0)
617         {
618             TBM_LOG ("[libtbm:%d] "
619                     "error %s:%d lock_cnt:%d\n",
620                     getpid(), __FUNCTION__, __LINE__, bo->lock_cnt);
621             _bo_unlock (bo);
622         }
623
624         /* Destroy Global Lock */
625         _tbm_bo_destroy_state (bo);
626
627         /* call the bo_free */
628         bufmgr->backend->bo_free (bo);
629         bo->priv = NULL;
630
631         LIST_DEL (&bo->item_link);
632         free(bo);
633         bo = NULL;
634     }
635
636 }
637
638 static int
639 _tbm_bufmgr_init_state (tbm_bufmgr bufmgr)
640 {
641     RETURN_VAL_CHECK_FLAG (TBM_ALL_CTRL_BACKEND_VALID(bufmgr->backend->flags), 1);
642
643     bufmgr->lock_fd = open (tgl_devfile, O_RDWR);
644
645     if(bufmgr->lock_fd < 0)
646     {
647         bufmgr->lock_fd = open (tgl_devfile1, O_RDWR);
648         if(bufmgr->lock_fd < 0)
649         {
650
651             TBM_LOG ("[libtbm:%d] "
652                     "error: Fail to open global_lock:%s\n",
653                     getpid(), tgl_devfile);
654             return 0;
655         }
656     }
657
658    if (!_tgl_init(bufmgr->lock_fd, GLOBAL_KEY))
659    {
660         TBM_LOG ("[libtbm:%d] "
661                 "error: Fail to initialize the tgl\n",
662                 getpid());
663         return 0;
664    }
665
666    return 1;
667 }
668
669 static void
670 _tbm_bufmgr_destroy_state (tbm_bufmgr bufmgr)
671 {
672     RETURN_CHECK_FLAG (TBM_ALL_CTRL_BACKEND_VALID(bufmgr->backend->flags));
673
674     close (bufmgr->lock_fd);
675 }
676
677 static int
678 _check_version (TBMModuleVersionInfo *data)
679 {
680     int abimaj, abimin;
681     int vermaj, vermin;
682
683     abimaj = GET_ABI_MAJOR (data->abiversion);
684     abimin = GET_ABI_MINOR (data->abiversion);
685
686     DBG ("[libtbm:%d] "
687             "TBM module %s: vendor=\"%s\" ABI=%d,%d\n",
688             getpid(), data->modname ? data->modname : "UNKNOWN!",
689             data->vendor ? data->vendor : "UNKNOWN!", abimaj, abimin);
690
691     vermaj = GET_ABI_MAJOR (TBM_ABI_VERSION);
692     vermin = GET_ABI_MINOR (TBM_ABI_VERSION);
693
694     DBG ("[libtbm:%d] " "TBM ABI version %d.%d\n", getpid(), vermaj, vermin);
695
696     if (abimaj != vermaj)
697     {
698         TBM_LOG ("[libtbm:%d] "
699                 "TBM module ABI major ver(%d) doesn't match the TBM's ver(%d)\n",
700                 getpid(), abimaj, vermaj);
701         return 0;
702     }
703     else if (abimin > vermin)
704     {
705         TBM_LOG ("[libtbm:%d] "
706                 "TBM module ABI minor ver(%d) is newer than the TBM's ver(%d)\n",
707                 getpid(), abimin, vermin);
708         return 0;
709     }
710     return 1;
711 }
712
713 static int
714 _tbm_bufmgr_load_module (tbm_bufmgr bufmgr, int fd, const char *file)
715 {
716     char path[PATH_MAX] = {0,};
717     TBMModuleData *initdata = NULL;
718     void * module_data;
719
720     snprintf(path, sizeof(path), BUFMGR_MODULE_DIR "/%s", file);
721
722     module_data = dlopen (path, RTLD_LAZY);
723     if (!module_data)
724     {
725         TBM_LOG ("[libtbm:%d] "
726                 "failed to load module: %s(%s)\n",
727                 getpid(), dlerror(), file);
728         return 0;
729     }
730
731     initdata = dlsym (module_data, "tbmModuleData");
732     if (initdata)
733     {
734         ModuleInitProc init;
735         TBMModuleVersionInfo *vers;
736
737         vers = initdata->vers;
738         init = initdata->init;
739
740         if (vers)
741         {
742             if (!_check_version (vers))
743             {
744                 dlclose (module_data);
745                 return 0;
746             }
747         }
748         else
749         {
750             TBM_LOG ("[libtbm:%d] "
751                     "Error: module does not supply version information.\n",
752                     getpid());
753
754             dlclose (module_data);
755             return 0;
756         }
757
758         if (init)
759         {
760             if(!init (bufmgr, fd))
761             {
762                 TBM_LOG ("[libtbm:%d] "
763                         "Fail to init module(%s)\n",
764                         getpid(), file);
765                 dlclose (module_data);
766                 return 0;
767             }
768
769             if (!bufmgr->backend || !bufmgr->backend->priv)
770             {
771                 TBM_LOG ("[libtbm:%d] "
772                         "Error: module(%s) wrong operation. Check backend or backend's priv.\n",
773                         getpid(), file);
774                 dlclose (module_data);
775                 return 0;
776             }
777         }
778         else
779         {
780             TBM_LOG ("[libtbm:%d] "
781                     "Error: module does not supply init symbol.\n", getpid());
782             dlclose (module_data);
783             return 0;
784         }
785     }
786     else
787     {
788         TBM_LOG ("[libtbm:%d] "
789                 "Error: module does not have data object.\n", getpid());
790         dlclose (module_data);
791         return 0;
792     }
793
794     bufmgr->module_data = module_data;
795
796     DBG ("[libtbm:%d] "
797             "Success to load module(%s)\n", getpid(), file);
798
799     return 1;
800 }
801
802 static int _tbm_load_module (tbm_bufmgr bufmgr, int fd)
803 {
804     struct dirent **namelist;
805     const char *p = NULL;
806     int n;
807     int ret = 0;
808
809     /* load bufmgr priv from default lib */
810     ret = _tbm_bufmgr_load_module (bufmgr, fd, DEFAULT_LIB);
811
812     /* load bufmgr priv from configured path */
813     if (!ret)
814     {
815         n = scandir (BUFMGR_MODULE_DIR, &namelist, 0, alphasort);
816         if (n < 0)
817             TBM_LOG ("[libtbm:%d] "
818                     "no files : %s\n", getpid(), BUFMGR_MODULE_DIR);
819         else
820         {
821             while(n--)
822             {
823                 if (!ret && strstr (namelist[n]->d_name, PREFIX_LIB))
824                 {
825                     p = strstr (namelist[n]->d_name, SUFFIX_LIB);
826                     if (p != NULL)
827                     {
828                         if (!strcmp (p, SUFFIX_LIB))
829                         {
830                             ret = _tbm_bufmgr_load_module (bufmgr, fd, namelist[n]->d_name);
831                         }
832                     }
833                 }
834                 free(namelist[n]);
835             }
836             free(namelist);
837         }
838     }
839
840     return ret;
841 }
842
843 tbm_bufmgr
844 tbm_bufmgr_init (int fd)
845 {
846     char *env;
847     int fd_flag = 0;
848     int backend_flag = 0;
849
850     pthread_mutex_lock (&gLock);
851
852 #ifdef DEBUG
853     env = getenv("GEM_DEBUG");
854     if(env)
855     {
856         bDebug = atoi(env);
857         TBM_LOG ("GEM_DEBUG=%s\n", env);
858     }
859     else
860         bDebug = 0;
861 #endif
862
863     /* initialize buffer manager */
864     if (gBufMgr)
865     {
866         DBG ("[libtbm:%d] use previous gBufMgr\n", getpid());
867         gBufMgr->ref_count++;
868
869         if (fd >= 0)
870         {
871             if (dup2(gBufMgr->fd, fd) < 0) {
872                 _tbm_set_last_result (TBM_BO_ERROR_DUP_FD_FAILED);
873                 DBG ("[libtbm:%d] Fail to duplicate(dup2) the drm fd\n", getpid());
874                 pthread_mutex_unlock (&gLock);
875                 return NULL;
876             }
877             DBG ("[libtbm:%d] duplicate the drm_fd(%d), new drm_fd(%d).\n",
878                         getpid(), gBufMgr->fd, fd);
879         }
880
881         DBG ("[libtbm:%d] bufmgr ref: fd=%d, ref_count:%d\n",
882                     getpid(), gBufMgr->fd, gBufMgr->ref_count);
883         pthread_mutex_unlock (&gLock);
884         return gBufMgr;
885     }
886
887     if (fd < 0)
888     {
889 #ifdef HAVE_X11
890         fd = tbm_bufmgr_get_drm_fd_x11();
891 #elif HAVE_WAYLAND
892         fd = tbm_bufmgr_get_drm_fd_wayland();
893 #endif
894         if (fd < 0)
895         {
896             _tbm_set_last_result (TBM_BO_ERROR_GET_FD_FAILED);
897             TBM_LOG ("[libtbm:%d] Fail get drm fd\n", getpid());
898             pthread_mutex_unlock (&gLock);
899             return NULL;
900         }
901         fd_flag = 1;
902     }
903
904     DBG ("[libtbm:%d] bufmgr init: fd=%d\n", getpid(), fd);
905
906     /* allocate bufmgr */
907     gBufMgr = calloc (1, sizeof(struct _tbm_bufmgr));
908     if (!gBufMgr)
909     {
910         _tbm_set_last_result (TBM_BO_ERROR_HEAP_ALLOC_FAILED);
911         pthread_mutex_unlock (&gLock);
912         return NULL;
913     }
914
915     gBufMgr->fd_flag = fd_flag;
916
917     if (fd_flag)
918     {
919         gBufMgr->fd = fd;
920     }
921     else
922     {
923         gBufMgr->fd = dup(fd);
924         if (gBufMgr->fd < 0)
925         {
926             _tbm_set_last_result (TBM_BO_ERROR_DUP_FD_FAILED);
927             TBM_LOG ("[libtbm:%d] Fail to duplicate(dup) the drm fd\n", getpid());
928             free (gBufMgr);
929             gBufMgr = NULL;
930             pthread_mutex_unlock (&gLock);
931             return NULL;
932         }
933         DBG ("[libtbm:%d] duplicate the drm_fd(%d), bufmgr use fd(%d).\n",
934                     getpid(), fd, gBufMgr->fd);
935     }
936
937     /* load bufmgr priv from env */
938     if (!_tbm_load_module(gBufMgr, gBufMgr->fd))
939     {
940         _tbm_set_last_result (TBM_BO_ERROR_LOAD_MODULE_FAILED);
941         TBM_LOG ("[libtbm:%d] "
942                 "error : Fail to load bufmgr backend\n",
943                 getpid());
944         free (gBufMgr);
945         gBufMgr = NULL;
946         pthread_mutex_unlock (&gLock);
947         return NULL;
948     }
949     else
950     {
951         backend_flag = gBufMgr->backend->flags;
952         /* log for tbm backend_flag */
953         DBG ("[libtbm:%d] ", getpid());
954         DBG ("cache_crtl:");
955         if (backend_flag&TBM_CACHE_CTRL_BACKEND) {
956             DBG ("BACKEND ");
957         } else {
958             DBG ("TBM ");
959         }
960         DBG ("lock_crtl:");
961         if (backend_flag&TBM_LOCK_CTRL_BACKEND) {
962             DBG ("BACKEND ");
963         } else {
964             DBG ("TBM ");
965         }
966         DBG ("\n");
967     }
968
969     gBufMgr->ref_count = 1;
970
971     DBG ("[libtbm:%d] create tizen bufmgr: ref_count:%d\n", getpid(), gBufMgr->ref_count);
972
973     if (pthread_mutex_init (&gBufMgr->lock, NULL) != 0)
974     {
975         _tbm_set_last_result (TBM_BO_ERROR_THREAD_INIT_FAILED);
976         gBufMgr->backend->bufmgr_deinit (gBufMgr->backend->priv);
977         tbm_backend_free (gBufMgr->backend);
978         dlclose (gBufMgr->module_data);
979         free (gBufMgr);
980         gBufMgr = NULL;
981         pthread_mutex_unlock (&gLock);
982         return NULL;
983     }
984
985     /* intialize the tizen global status */
986     if (!_tbm_bufmgr_init_state (gBufMgr))
987     {
988         _tbm_set_last_result (TBM_BO_ERROR_INIT_STATE_FAILED);
989         TBM_LOG ("[libtbm:%d] "
990                 "error: Fail to init state\n",
991                 getpid());
992         gBufMgr->backend->bufmgr_deinit (gBufMgr->backend->priv);
993         tbm_backend_free (gBufMgr->backend);
994         pthread_mutex_destroy (&gBufMgr->lock);
995         dlclose (gBufMgr->module_data);
996         free (gBufMgr);
997         gBufMgr = NULL;
998         pthread_mutex_unlock (&gLock);
999         return NULL;
1000     }
1001
1002     /* setup the lock_type */
1003     env = getenv ("BUFMGR_LOCK_TYPE");
1004     if (env && !strcmp (env, "always"))
1005         gBufMgr->lock_type = LOCK_TRY_ALWAYS;
1006     else if(env && !strcmp(env, "none"))
1007         gBufMgr->lock_type = LOCK_TRY_NEVER;
1008     else if(env && !strcmp(env, "once"))
1009          gBufMgr->lock_type = LOCK_TRY_ONCE;
1010     else
1011         gBufMgr->lock_type = LOCK_TRY_ALWAYS;
1012
1013     DBG ("[libtbm:%d] BUFMGR_LOCK_TYPE=%s\n", getpid(), env?env:"default:once");
1014
1015     /* setup the map_cache */
1016     env = getenv ("BUFMGR_MAP_CACHE");
1017     if (env && !strcmp (env, "false"))
1018         gBufMgr->use_map_cache = 0;
1019     else
1020         gBufMgr->use_map_cache = 1;
1021     DBG ("[libtbm:%d] BUFMGR_MAP_CACHE=%s\n", getpid(), env?env:"default:true");
1022
1023     /* intialize bo_list */
1024     LIST_INITHEAD (&gBufMgr->bo_list);
1025
1026     /* intialize surf_list */
1027     LIST_INITHEAD (&gBufMgr->surf_list);
1028
1029     pthread_mutex_unlock (&gLock);
1030     return gBufMgr;
1031 }
1032
1033 void
1034 tbm_bufmgr_deinit (tbm_bufmgr bufmgr)
1035 {
1036     TBM_RETURN_IF_FAIL (TBM_BUFMGR_IS_VALID(bufmgr));
1037
1038     tbm_bo bo = NULL;
1039     tbm_bo tmp = NULL;
1040
1041     tbm_surface_h surf = NULL;
1042     tbm_surface_h tmp_surf = NULL;
1043
1044     pthread_mutex_lock (&gLock);
1045
1046     bufmgr->ref_count--;
1047     if (bufmgr->ref_count > 0)
1048     {
1049         TBM_LOG ("[libtbm:%d] "
1050                 "tizen bufmgr destroy: bufmgr:%p, ref_count:%d\n",
1051                 getpid(), bufmgr, bufmgr->ref_count);
1052         pthread_mutex_unlock (&gLock);
1053         return;
1054     }
1055
1056     /* destroy bo_list */
1057     if(!LIST_IS_EMPTY (&bufmgr->bo_list))
1058     {
1059         LIST_FOR_EACH_ENTRY_SAFE (bo, tmp, &bufmgr->bo_list, item_link)
1060         {
1061             TBM_LOG ("[libtbm:%d] "
1062                     "Un-freed bo(%p, ref:%d) \n",
1063                     getpid(), bo, bo->ref_cnt);
1064             bo->ref_cnt = 1;
1065             tbm_bo_unref(bo);
1066         }
1067     }
1068
1069     /* destroy surf_list */
1070     if(!LIST_IS_EMPTY (&bufmgr->surf_list))
1071     {
1072         LIST_FOR_EACH_ENTRY_SAFE (surf, tmp_surf, &bufmgr->surf_list, item_link)
1073         {
1074             TBM_LOG ("[libtbm:%d] "
1075                     "Destroy surf(%p) \n",
1076                     getpid(), surf);
1077             tbm_surface_destroy(surf);
1078         }
1079     }
1080
1081     /* destroy the tizen global status */
1082     _tbm_bufmgr_destroy_state (bufmgr);
1083
1084     /* destroy bufmgr priv */
1085     bufmgr->backend->bufmgr_deinit (bufmgr->backend->priv);
1086     bufmgr->backend->priv = NULL;
1087     tbm_backend_free (bufmgr->backend);
1088     bufmgr->backend = NULL;
1089
1090     pthread_mutex_destroy (&bufmgr->lock);
1091
1092     DBG ("[libtbm:%d] "
1093             "tizen bufmgr destroy: bufmgr:%p\n",
1094             getpid(), bufmgr);
1095
1096     dlclose (bufmgr->module_data);
1097
1098     close(bufmgr->fd);
1099
1100     free (bufmgr);
1101     bufmgr = NULL;
1102     gBufMgr = NULL;
1103
1104     pthread_mutex_unlock (&gLock);
1105 }
1106
1107 int
1108 tbm_bo_size (tbm_bo bo)
1109 {
1110     TBM_RETURN_VAL_IF_FAIL (_tbm_bo_is_valid(bo), 0);
1111
1112     tbm_bufmgr bufmgr = bo->bufmgr;
1113     int size;
1114
1115     pthread_mutex_lock(&bufmgr->lock);
1116
1117     size = bufmgr->backend->bo_size(bo);
1118
1119     pthread_mutex_unlock(&bufmgr->lock);
1120
1121     return size;
1122 }
1123
1124 tbm_bo
1125 tbm_bo_ref (tbm_bo bo)
1126 {
1127     TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), NULL);
1128
1129     tbm_bufmgr bufmgr = bo->bufmgr;
1130
1131     pthread_mutex_lock(&bufmgr->lock);
1132
1133     _tbm_bo_ref (bo);
1134
1135     pthread_mutex_unlock(&bufmgr->lock);
1136
1137     return bo;
1138 }
1139
1140 void
1141 tbm_bo_unref (tbm_bo bo)
1142 {
1143     TBM_RETURN_IF_FAIL(_tbm_bo_is_valid(bo));
1144
1145     tbm_bufmgr bufmgr = bo->bufmgr;
1146
1147     pthread_mutex_lock (&bufmgr->lock);
1148
1149     _tbm_bo_unref (bo);
1150
1151     pthread_mutex_unlock(&bufmgr->lock);
1152 }
1153
1154 tbm_bo
1155 tbm_bo_alloc (tbm_bufmgr bufmgr, int size, int flags)
1156 {
1157     TBM_RETURN_VAL_IF_FAIL (TBM_BUFMGR_IS_VALID(bufmgr) && (size > 0), NULL);
1158
1159     tbm_bo bo = NULL;
1160     void * bo_priv = NULL;
1161
1162     bo = calloc (1, sizeof(struct _tbm_bo));
1163     if(!bo)
1164     {
1165         _tbm_set_last_result (TBM_BO_ERROR_HEAP_ALLOC_FAILED);
1166         return NULL;
1167     }
1168
1169     bo->bufmgr = bufmgr;
1170
1171     pthread_mutex_lock (&bufmgr->lock);
1172
1173     bo_priv = bufmgr->backend->bo_alloc (bo, size, flags);
1174     if (!bo_priv)
1175     {
1176         _tbm_set_last_result (TBM_BO_ERROR_BO_ALLOC_FAILED);
1177         free (bo);
1178         pthread_mutex_unlock (&bufmgr->lock);
1179         return NULL;
1180     }
1181
1182     bo->ref_cnt = 1;
1183     bo->flags = flags;
1184     bo->tgl_key = INITIAL_KEY;
1185     bo->priv = bo_priv;
1186
1187     /* init bo state */
1188     if (!_tbm_bo_init_state (bo, CACHE_OP_CREATE))
1189     {
1190         _tbm_set_last_result (TBM_BO_ERROR_INIT_STATE_FAILED);
1191         _tbm_bo_unref (bo);
1192         pthread_mutex_unlock (&bufmgr->lock);
1193         return NULL;
1194     }
1195
1196     LIST_INITHEAD (&bo->user_data_list);
1197
1198     LIST_ADD (&bo->item_link, &bufmgr->bo_list);
1199
1200     pthread_mutex_unlock(&bufmgr->lock);
1201
1202     return bo;
1203 }
1204
1205 tbm_bo
1206 tbm_bo_import (tbm_bufmgr bufmgr, unsigned int key)
1207 {
1208     TBM_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), NULL);
1209
1210     tbm_bo bo = NULL;
1211     tbm_bo bo2 = NULL;
1212     tbm_bo tmp = NULL;
1213     void * bo_priv = NULL;
1214
1215     pthread_mutex_lock (&bufmgr->lock);
1216
1217     /* find bo in list */
1218     if(!LIST_IS_EMPTY (&bufmgr->bo_list))
1219     {
1220         LIST_FOR_EACH_ENTRY_SAFE (bo2, tmp, &bufmgr->bo_list, item_link)
1221         {
1222             if (bo2->tgl_key == key)
1223             {
1224                 DBG ("[libtbm:%d] "
1225                         "find bo(%p, ref:%d key:%d) in list \n",
1226                         getpid(), bo2, bo2->ref_cnt, bo2->tgl_key);
1227
1228                 bo2->ref_cnt++;
1229                 pthread_mutex_unlock (&bufmgr->lock);
1230                 return bo2;
1231             }
1232         }
1233     }
1234
1235     bo = calloc (1, sizeof(struct _tbm_bo));
1236     if(!bo)
1237     {
1238         pthread_mutex_unlock (&bufmgr->lock);
1239         return NULL;
1240     }
1241
1242     bo->bufmgr = bufmgr;
1243
1244     bo_priv = bufmgr->backend->bo_import (bo, key);
1245     if (!bo_priv)
1246     {
1247         _tbm_set_last_result (TBM_BO_ERROR_IMPORT_FAILED);
1248         free (bo);
1249         pthread_mutex_unlock (&bufmgr->lock);
1250         return NULL;
1251     }
1252
1253     bo->ref_cnt = 1;
1254     bo->tgl_key = INITIAL_KEY;
1255     bo->priv = bo_priv;
1256
1257     /* init bo state */
1258     if (!_tbm_bo_init_state (bo, CACHE_OP_IMPORT))
1259     {
1260         _tbm_set_last_result (TBM_BO_ERROR_INIT_STATE_FAILED);
1261         _tbm_bo_unref (bo);
1262         pthread_mutex_unlock (&bufmgr->lock);
1263         return NULL;
1264     }
1265
1266     LIST_INITHEAD (&bo->user_data_list);
1267
1268     LIST_ADD (&bo->item_link, &bufmgr->bo_list);
1269
1270     pthread_mutex_unlock (&bufmgr->lock);
1271
1272     return bo;
1273 }
1274
1275 tbm_bo
1276 tbm_bo_import_fd  (tbm_bufmgr bufmgr, tbm_fd fd)
1277 {
1278     TBM_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), NULL);
1279
1280     tbm_bo bo = NULL;
1281     tbm_bo bo2 = NULL;
1282     tbm_bo tmp = NULL;
1283     void * bo_priv = NULL;
1284     tbm_bo_handle default_handle;
1285
1286     pthread_mutex_lock (&bufmgr->lock);
1287
1288     default_handle = bufmgr->backend->fd_to_handle (bufmgr, fd, TBM_DEVICE_DEFAULT);
1289
1290     /* find bo in list */
1291     if(!LIST_IS_EMPTY (&bufmgr->bo_list))
1292     {
1293         LIST_FOR_EACH_ENTRY_SAFE (bo2, tmp, &bufmgr->bo_list, item_link)
1294         {
1295             if (bo2->default_handle.u32 == default_handle.u32)
1296             {
1297                 DBG ("[libtbm:%d] "
1298                         "find bo(%p, ref:%d handle:%d) in list \n",
1299                         getpid(), bo2, bo2->ref_cnt, bo2->default_handle.u32);
1300
1301                 bo2->ref_cnt++;
1302                 pthread_mutex_unlock (&bufmgr->lock);
1303                 return bo2;
1304             }
1305         }
1306     }
1307
1308     bo = calloc (1, sizeof(struct _tbm_bo));
1309     if(!bo)
1310     {
1311         pthread_mutex_unlock (&bufmgr->lock);
1312         return NULL;
1313     }
1314
1315     bo->bufmgr = bufmgr;
1316
1317     bo_priv = bufmgr->backend->bo_import_fd(bo, fd);
1318     if (!bo_priv)
1319     {
1320         _tbm_set_last_result (TBM_BO_ERROR_IMPORT_FD_FAILED);
1321         free (bo);
1322         pthread_mutex_unlock (&bufmgr->lock);
1323         return NULL;
1324     }
1325
1326     bo->ref_cnt = 1;
1327     bo->tgl_key = INITIAL_KEY;
1328     bo->priv = bo_priv;
1329
1330     /* init bo state */
1331     if (!_tbm_bo_init_state (bo, CACHE_OP_IMPORT))
1332     {
1333         _tbm_set_last_result (TBM_BO_ERROR_INIT_STATE_FAILED);
1334         _tbm_bo_unref (bo);
1335         pthread_mutex_unlock (&bufmgr->lock);
1336         return NULL;
1337     }
1338
1339     LIST_INITHEAD (&bo->user_data_list);
1340
1341     LIST_ADD (&bo->item_link, &bufmgr->bo_list);
1342
1343     pthread_mutex_unlock (&bufmgr->lock);
1344
1345     return bo;
1346 }
1347
1348 unsigned int
1349 tbm_bo_export (tbm_bo bo)
1350 {
1351     TBM_RETURN_VAL_IF_FAIL (_tbm_bo_is_valid(bo), 0);
1352
1353     tbm_bufmgr bufmgr;
1354     int ret;
1355
1356     bufmgr = bo->bufmgr;
1357
1358     pthread_mutex_lock (&bufmgr->lock);
1359     ret = bufmgr->backend->bo_export (bo);
1360     if (!ret)
1361     {
1362         _tbm_set_last_result (TBM_BO_ERROR_EXPORT_FAILED);
1363         pthread_mutex_unlock (&bufmgr->lock);
1364         return ret;
1365     }
1366     pthread_mutex_unlock (&bufmgr->lock);
1367
1368     return ret;
1369 }
1370
1371 tbm_fd
1372 tbm_bo_export_fd (tbm_bo bo)
1373 {
1374     TBM_RETURN_VAL_IF_FAIL (_tbm_bo_is_valid(bo), 0);
1375
1376     tbm_bufmgr bufmgr;
1377     int ret;
1378
1379     bufmgr = bo->bufmgr;
1380
1381     pthread_mutex_lock (&bufmgr->lock);
1382     ret = bufmgr->backend->bo_export_fd (bo);
1383     if (!ret)
1384     {
1385         _tbm_set_last_result (TBM_BO_ERROR_EXPORT_FD_FAILED);
1386         pthread_mutex_unlock (&bufmgr->lock);
1387         return ret;
1388     }
1389     pthread_mutex_unlock (&bufmgr->lock);
1390
1391     return ret;
1392 }
1393
1394
1395 tbm_bo_handle
1396 tbm_bo_get_handle (tbm_bo bo, int device)
1397 {
1398     TBM_RETURN_VAL_IF_FAIL (_tbm_bo_is_valid(bo), (tbm_bo_handle)0);
1399
1400     tbm_bufmgr bufmgr;
1401     tbm_bo_handle bo_handle;
1402
1403     bufmgr = bo->bufmgr;
1404
1405     pthread_mutex_lock (&bufmgr->lock);
1406     bo_handle = bufmgr->backend->bo_get_handle (bo, device);
1407     if (bo_handle.ptr == NULL)
1408     {
1409         _tbm_set_last_result (TBM_BO_ERROR_GET_HANDLE_FAILED);
1410         pthread_mutex_unlock (&bufmgr->lock);
1411         return (tbm_bo_handle)NULL;
1412     }
1413     pthread_mutex_unlock (&bufmgr->lock);
1414
1415     return bo_handle;
1416 }
1417
1418 tbm_bo_handle
1419 tbm_bo_map (tbm_bo bo, int device, int opt)
1420 {
1421     TBM_RETURN_VAL_IF_FAIL (_tbm_bo_is_valid(bo), (tbm_bo_handle)0);
1422
1423     tbm_bufmgr bufmgr;
1424     tbm_bo_handle bo_handle;
1425
1426     bufmgr = bo->bufmgr;
1427
1428     pthread_mutex_lock (&bufmgr->lock);
1429
1430     bo_handle = bufmgr->backend->bo_get_handle (bo, device);
1431
1432     if (!_tbm_bo_lock (bo, device, opt))
1433     {
1434         _tbm_set_last_result (TBM_BO_ERROR_LOCK_FAILED);
1435         TBM_LOG ("[libtbm:%d] "
1436                         "error %s:%d fail to lock bo:%p)\n",
1437                         getpid(), __FUNCTION__, __LINE__, bo);
1438         pthread_mutex_unlock (&bufmgr->lock);
1439         return (tbm_bo_handle)NULL;
1440     }
1441
1442     bo_handle = bufmgr->backend->bo_map (bo, device, opt);
1443     if (bo_handle.ptr == NULL)
1444     {
1445         _tbm_set_last_result (TBM_BO_ERROR_MAP_FAILED);
1446         TBM_LOG ("[libtbm:%d] "
1447                         "error %s:%d fail to map bo:%p\n",
1448                         getpid(), __FUNCTION__, __LINE__, bo);
1449
1450         _tbm_bo_unlock(bo);
1451         pthread_mutex_unlock (&bufmgr->lock);
1452         return (tbm_bo_handle)NULL;
1453     }
1454
1455     if (bufmgr->use_map_cache == 1 && bo->map_cnt == 0)
1456         _tbm_bo_set_state (bo, device, opt);
1457
1458     /* increase the map_count */
1459     bo->map_cnt++;
1460
1461     pthread_mutex_unlock (&bufmgr->lock);
1462
1463     return bo_handle;
1464 }
1465
1466 int
1467 tbm_bo_unmap (tbm_bo bo)
1468 {
1469     TBM_RETURN_VAL_IF_FAIL (_tbm_bo_is_valid(bo), 0);
1470
1471     tbm_bufmgr bufmgr;
1472     int ret;
1473
1474     bufmgr = bo->bufmgr;
1475
1476     pthread_mutex_lock (&bufmgr->lock);
1477
1478     ret = bufmgr->backend->bo_unmap (bo);
1479     if (!ret)
1480     {
1481
1482         _tbm_set_last_result (TBM_BO_ERROR_UNMAP_FAILED);
1483         pthread_mutex_unlock (&bufmgr->lock);
1484         return ret;
1485     }
1486
1487     /* decrease the map_count */
1488     bo->map_cnt--;
1489
1490     if (bo->map_cnt == 0)
1491         _tbm_bo_save_state (bo);
1492
1493      _tbm_bo_unlock (bo);
1494
1495     pthread_mutex_unlock (&bufmgr->lock);
1496
1497     return ret;
1498 }
1499
1500 int
1501 tbm_bo_swap (tbm_bo bo1, tbm_bo bo2)
1502 {
1503     TBM_RETURN_VAL_IF_FAIL (_tbm_bo_is_valid(bo1), 0);
1504     TBM_RETURN_VAL_IF_FAIL (_tbm_bo_is_valid(bo2), 0);
1505
1506     void* temp;
1507     unsigned int tmp_key;
1508
1509     pthread_mutex_lock (&bo1->bufmgr->lock);
1510
1511     if (bo1->bufmgr->backend->bo_size (bo1) != bo2->bufmgr->backend->bo_size (bo2))
1512     {
1513         _tbm_set_last_result (TBM_BO_ERROR_SWAP_FAILED);
1514         pthread_mutex_unlock (&bo1->bufmgr->lock);
1515         return 0;
1516     }
1517
1518
1519     tmp_key = bo1->tgl_key;
1520     bo1->tgl_key = bo2->tgl_key;
1521     bo2->tgl_key = tmp_key;
1522
1523     temp = bo1->priv;
1524     bo1->priv = bo2->priv;
1525     bo2->priv = temp;
1526
1527     pthread_mutex_unlock (&bo1->bufmgr->lock);
1528
1529     return 1;
1530 }
1531
1532 int
1533 tbm_bo_locked (tbm_bo bo)
1534 {
1535     TBM_RETURN_VAL_IF_FAIL (_tbm_bo_is_valid(bo), 0);
1536
1537     tbm_bufmgr bufmgr;
1538
1539     bufmgr = bo->bufmgr;
1540
1541     if (bufmgr->lock_type == LOCK_TRY_NEVER)
1542         return 0;
1543
1544     pthread_mutex_lock (&bufmgr->lock);
1545
1546     if (bo->lock_cnt > 0)
1547     {
1548         pthread_mutex_unlock (&bufmgr->lock);
1549         return 1;
1550     }
1551
1552     pthread_mutex_unlock (&bufmgr->lock);
1553
1554     return 0;
1555 }
1556
1557
1558 int
1559 tbm_bo_add_user_data (tbm_bo bo, unsigned long key, tbm_data_free data_free_func)
1560 {
1561     TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
1562
1563     tbm_user_data *data;
1564
1565     /* check if the data according to the key exist if so, return false.*/
1566     data = _user_data_lookup (&bo->user_data_list, key);
1567     if (data)
1568     {
1569         TBM_LOG ("[libtbm:%d] "
1570                 "waring: %s:%d user data already exist. key:%ld\n",
1571                 getpid(), __FUNCTION__, __LINE__, key);
1572         return 0;
1573     }
1574
1575     data = _user_data_create (key, data_free_func);
1576     if (!data)
1577         return 0;
1578
1579     LIST_ADD (&data->item_link, &bo->user_data_list);
1580
1581     return 1;
1582 }
1583
1584 int
1585 tbm_bo_set_user_data (tbm_bo bo, unsigned long key, void* data)
1586 {
1587     TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
1588
1589     tbm_user_data *old_data;
1590
1591     if (LIST_IS_EMPTY (&bo->user_data_list))
1592         return 0;
1593
1594     old_data = _user_data_lookup (&bo->user_data_list, key);
1595     if (!old_data)
1596         return 0;
1597
1598     if (old_data->data && old_data->free_func)
1599         old_data->free_func(old_data->data);
1600
1601     old_data->data = data;
1602
1603     return 1;
1604 }
1605
1606 int
1607 tbm_bo_get_user_data (tbm_bo bo, unsigned long key, void** data)
1608 {
1609     TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
1610
1611     tbm_user_data* old_data;
1612
1613     if (!data || LIST_IS_EMPTY (&bo->user_data_list))
1614         return 0;
1615
1616     old_data = _user_data_lookup (&bo->user_data_list, key);
1617     if (!old_data)
1618     {
1619         *data = NULL;
1620         return 0;
1621     }
1622
1623     *data = old_data->data;
1624
1625     return 1;
1626 }
1627
1628 int
1629 tbm_bo_delete_user_data (tbm_bo bo, unsigned long key)
1630 {
1631     TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
1632
1633     tbm_user_data *old_data = (void *)0;
1634
1635     if (LIST_IS_EMPTY (&bo->user_data_list))
1636         return 0;
1637
1638     old_data = _user_data_lookup (&bo->user_data_list, key);
1639     if (!old_data)
1640         return 0;
1641
1642     _user_data_delete (old_data);
1643
1644     return 1;
1645 }
1646
1647 tbm_error_e
1648 tbm_get_last_error (void)
1649 {
1650     return tbm_last_error;
1651 }
1652