sync with master
[platform/core/uifw/libtbm.git] / drm_slp / drm_slp_bufmgr.c
1 /**************************************************************************
2
3 xserver-xorg-video-sec
4
5 Copyright 2011 Samsung Electronics co., Ltd. All Rights Reserved.
6
7 Contact: SooChan Lim <sc1.lim@samsung.com>, Sangjin Lee <lsj119@samsung.com>
8
9 Permission is hereby granted, free of charge, to any person obtaining a
10 copy of this software and associated documentation files (the
11 "Software"), to deal in the Software without restriction, including
12 without limitation the rights to use, copy, modify, merge, publish,
13 distribute, sub license, and/or sell copies of the Software, and to
14 permit persons to whom the Software is furnished to do so, subject to
15 the following conditions:
16
17 The above copyright notice and this permission notice (including the
18 next paragraph) shall be included in all copies or substantial portions
19 of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
24 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
25 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
29 **************************************************************************/
30
31 #include "config.h"
32 #include <stdio.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include "drm_slp_bufmgr.h"
37 #include "tbm_bufmgr.h"
38
39 drm_slp_bufmgr
40 drm_slp_bufmgr_init(int fd, void *arg)
41 {
42     tbm_bufmgr bufmgr = NULL;
43
44     bufmgr = tbm_bufmgr_init (fd);
45     if (!bufmgr)
46     {
47         fprintf (stderr, "[libdrm_slp:%d]: error bufmgr is null\n", getpid());
48         return NULL;
49     }
50
51     return (drm_slp_bufmgr)bufmgr;
52 }
53
54 void
55 drm_slp_bufmgr_destroy(drm_slp_bufmgr bufmgr)
56 {
57     tbm_bufmgr_deinit ((tbm_bufmgr)bufmgr);
58 }
59
60 void
61 drm_slp_bo_unref(drm_slp_bo bo)
62 {
63     tbm_bo_unref ((tbm_bo)bo);
64 }
65
66 drm_slp_bo
67 drm_slp_bo_import(drm_slp_bufmgr bufmgr, unsigned int key)
68 {
69     tbm_bo bo = NULL;
70
71     bo = tbm_bo_import ((tbm_bufmgr)bufmgr, key);
72     if (!bo)
73     {
74         fprintf (stderr, "[libdrm_slp:%d]: error bo is null\n", getpid());
75         return NULL;
76     }
77
78     return (drm_slp_bo)bo;
79 }
80
81 unsigned int
82 drm_slp_bo_map(drm_slp_bo bo, int device, int opt)
83 {
84     tbm_bo_handle bo_handle;
85     unsigned int ret = 0;
86
87     bo_handle = tbm_bo_map ((tbm_bo)bo, device, opt);
88     if (bo_handle.ptr == NULL)
89     {
90         fprintf (stderr, "[libdrm_slp:%d]: error bo_handle is null\n", getpid());
91         return 0;
92     }
93
94     switch (device)
95     {
96         case TBM_DEVICE_DEFAULT:
97         case TBM_DEVICE_2D:
98         case TBM_DEVICE_3D:
99         case TBM_DEVICE_MM:
100             ret = (unsigned int)bo_handle.u32;
101             break;
102         case TBM_DEVICE_CPU:
103             ret = (unsigned int)bo_handle.ptr;
104             break;
105         default:
106             fprintf (stderr, "[libdrm_slp:%d]: error wrong device type\n", getpid());
107             return 0;
108     }
109
110     return ret;
111 }
112
113 int
114 drm_slp_bo_unmap(drm_slp_bo bo, int device)
115 {
116     tbm_bo_unmap ((tbm_bo)bo);
117
118     return 1;
119 }
120