libtbm-vigs : Add the MIT bolierplate
[platform/adaptation/emulator/libtbm-vigs.git] / src / tbm_bufmgr_emulator.c
1 /**************************************************************************
2
3 libtbm-emulator
4
5 Copyright 2013 Samsung Electronics co., Ltd. All Rights Reserved.
6
7 Contact:
8 Stanislav Vorobiov <s.vorobiov@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 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include <tbm_bufmgr.h>
37 #include <tbm_bufmgr_backend.h>
38 #include "vigs.h"
39 #include "tbm_emulator_log.h"
40 #include <string.h>
41 #include <stdlib.h>
42
43 static tbm_bo_handle get_tbm_bo_handle(struct vigs_drm_gem *gem,
44                                        int device)
45 {
46     tbm_bo_handle bo_handle;
47     int ret;
48
49     memset(&bo_handle, 0, sizeof(bo_handle));
50
51     switch (device) {
52     case TBM_DEVICE_DEFAULT:
53     case TBM_DEVICE_2D:
54         bo_handle.u32 = gem->handle;
55         break;
56     case TBM_DEVICE_CPU:
57         ret = vigs_drm_gem_map(gem, 1);
58
59         if (ret == 0) {
60             bo_handle.ptr = gem->vaddr;
61         } else {
62             TBM_EMULATOR_LOG_ERROR("vigs_drm_gem_map failed: %s",
63                                    strerror(-ret));
64         }
65
66         break;
67     case TBM_DEVICE_3D:
68         TBM_EMULATOR_LOG_ERROR("TBM_DEVICE_3D not supported");
69         break;
70     case TBM_DEVICE_MM:
71         TBM_EMULATOR_LOG_ERROR("TBM_DEVICE_MM not supported");
72         break;
73     default:
74         TBM_EMULATOR_LOG_ERROR("%d not supported", device);
75         break;
76     }
77
78     return bo_handle;
79 }
80
81 static void tbm_bufmgr_emulator_deinit(void *priv)
82 {
83     struct vigs_drm_device *drm_dev = priv;
84
85     TBM_EMULATOR_LOG_DEBUG("enter");
86
87     vigs_drm_device_destroy(drm_dev);
88 }
89
90 static int tbm_bufmgr_emulator_bo_size(tbm_bo bo)
91 {
92     struct vigs_drm_surface *sfc;
93
94     TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
95
96     sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
97
98     return sfc->gem.size;
99 }
100
101 static void *tbm_bufmgr_emulator_bo_alloc(tbm_bo bo, int size, int flags)
102 {
103     TBM_EMULATOR_LOG_ERROR("not supported");
104     return NULL;
105 }
106
107 static void tbm_bufmgr_emulator_bo_free(tbm_bo bo)
108 {
109     struct vigs_drm_surface *sfc;
110
111     TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
112
113     sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
114
115     vigs_drm_gem_unref(&sfc->gem);
116 }
117
118 static void *tbm_bufmgr_emulator_bo_import(tbm_bo bo, unsigned int key)
119 {
120     struct vigs_drm_device *drm_dev;
121     int ret;
122     struct vigs_drm_surface *sfc;
123
124     TBM_EMULATOR_LOG_DEBUG("bo = %p, key = %u", bo, key);
125
126     drm_dev = (struct vigs_drm_device*)tbm_backend_get_bufmgr_priv(bo);
127
128     ret = vigs_drm_surface_open(drm_dev, key, &sfc);
129
130     if (ret != 0) {
131         TBM_EMULATOR_LOG_ERROR("vigs_drm_surface_open failed for key %u: %s",
132                                key,
133                                strerror(-ret));
134         return NULL;
135     }
136
137     TBM_EMULATOR_LOG_DEBUG("handle = %u", sfc->gem.handle);
138
139     return sfc;
140 }
141
142 static unsigned int tbm_bufmgr_emulator_bo_export(tbm_bo bo)
143 {
144     struct vigs_drm_surface *sfc;
145     int ret;
146
147     TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
148
149     sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
150
151     ret = vigs_drm_gem_get_name(&sfc->gem);
152
153     if (ret != 0) {
154         TBM_EMULATOR_LOG_ERROR("vigs_drm_gem_get_name failed: %s",
155                                strerror(-ret));
156         return 0;
157     }
158
159     return sfc->gem.name;
160 }
161
162 static tbm_bo_handle tbm_bufmgr_emulator_bo_get_handle(tbm_bo bo, int device)
163 {
164     struct vigs_drm_surface *sfc;
165
166     TBM_EMULATOR_LOG_DEBUG("bo = %p, device = %d", bo, device);
167
168     sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
169
170     return get_tbm_bo_handle(&sfc->gem, device);
171 }
172
173 static tbm_bo_handle tbm_bufmgr_emulator_bo_map(tbm_bo bo, int device, int opt)
174 {
175     struct vigs_drm_surface *sfc;
176     tbm_bo_handle handle;
177     uint32_t saf = 0;
178
179     TBM_EMULATOR_LOG_DEBUG("bo = %p, device = %d, opt = %d", bo, device, opt);
180
181     sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
182
183     handle = get_tbm_bo_handle(&sfc->gem, device);
184
185     if (!handle.ptr) {
186         return handle;
187     }
188
189     if ((opt & TBM_OPTION_READ) != 0) {
190         saf |= VIGS_DRM_SAF_READ;
191     }
192
193     if ((opt & TBM_OPTION_WRITE) != 0) {
194         saf |= VIGS_DRM_SAF_WRITE;
195     }
196
197     vigs_drm_surface_start_access(sfc, saf);
198
199     return handle;
200 }
201
202 static int tbm_bufmgr_emulator_bo_unmap(tbm_bo bo)
203 {
204     struct vigs_drm_surface *sfc;
205
206     TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
207
208     sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
209
210     vigs_drm_surface_end_access(sfc, 1);
211
212     return 1;
213 }
214
215 static int tbm_bufmgr_emulator_bo_cache_flush(tbm_bo bo, int flags)
216 {
217     TBM_EMULATOR_LOG_DEBUG("bo = %p, flags = %d", bo, flags);
218     return 1;
219 }
220
221 static int tbm_bufmgr_emulator_bo_get_global_key(tbm_bo bo)
222 {
223     struct vigs_drm_surface *sfc;
224     int ret;
225
226     TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
227
228     sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
229
230     ret = vigs_drm_gem_get_name(&sfc->gem);
231
232     if (ret != 0) {
233         TBM_EMULATOR_LOG_ERROR("vigs_drm_gem_get_name failed: %s",
234                                strerror(-ret));
235         return 0;
236     }
237
238     return sfc->gem.name;
239 }
240
241 MODULEINITPPROTO(tbm_bufmgr_emulator_init);
242
243 static TBMModuleVersionInfo EmulatorVersRec =
244 {
245     "emulator",
246     "Samsung",
247     TBM_ABI_VERSION,
248 };
249
250 TBMModuleData tbmModuleData = { &EmulatorVersRec, tbm_bufmgr_emulator_init };
251
252 int tbm_bufmgr_emulator_init(tbm_bufmgr bufmgr, int fd)
253 {
254     int ret = 0;
255     struct vigs_drm_device *drm_dev = NULL;
256     tbm_bufmgr_backend backend = NULL;
257
258     TBM_EMULATOR_LOG_DEBUG("enter");
259
260     if (!bufmgr) {
261         return 0;
262     }
263
264     ret = vigs_drm_device_create(fd, &drm_dev);
265
266     if (ret != 0) {
267         TBM_EMULATOR_LOG_ERROR("vigs_drm_device_create failed: %s", strerror(-ret));
268         goto fail;
269     }
270
271     backend = tbm_backend_alloc();
272
273     if (!backend) {
274         TBM_EMULATOR_LOG_ERROR("tbm_backend_alloc failed");
275         goto fail;
276     }
277
278     backend->flags = 0;
279     backend->priv = (void*)drm_dev;
280     backend->bufmgr_deinit = tbm_bufmgr_emulator_deinit;
281     backend->bo_size = tbm_bufmgr_emulator_bo_size;
282     backend->bo_alloc = tbm_bufmgr_emulator_bo_alloc;
283     backend->bo_free = tbm_bufmgr_emulator_bo_free;
284     backend->bo_import = tbm_bufmgr_emulator_bo_import;
285     backend->bo_export = tbm_bufmgr_emulator_bo_export;
286     backend->bo_get_handle = tbm_bufmgr_emulator_bo_get_handle;
287     backend->bo_map = tbm_bufmgr_emulator_bo_map;
288     backend->bo_unmap = tbm_bufmgr_emulator_bo_unmap;
289     backend->bo_cache_flush = tbm_bufmgr_emulator_bo_cache_flush;
290     backend->bo_get_global_key = tbm_bufmgr_emulator_bo_get_global_key;
291     backend->bo_lock = NULL;
292     backend->bo_unlock = NULL;
293
294     if (!tbm_backend_init(bufmgr, backend)) {
295         TBM_EMULATOR_LOG_ERROR("tbm_backend_init failed");
296         goto fail;
297     }
298
299     TBM_EMULATOR_LOG_INFO("initialized");
300
301     return 1;
302
303 fail:
304     if (backend) {
305         tbm_backend_free(backend);
306     }
307
308     if (drm_dev) {
309         vigs_drm_device_destroy(drm_dev);
310     }
311
312     return 0;
313 }