VIGS: Implemented BO Lock/Unlock
[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_lock(tbm_bo bo)
222 {
223     TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
224     return 1;
225 }
226
227 static int tbm_bufmgr_emulator_bo_unlock(tbm_bo bo)
228 {
229     TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
230     return 1;
231 }
232
233 static int tbm_bufmgr_emulator_bo_get_global_key(tbm_bo bo)
234 {
235     struct vigs_drm_surface *sfc;
236     int ret;
237
238     TBM_EMULATOR_LOG_DEBUG("bo = %p", bo);
239
240     sfc = (struct vigs_drm_surface*)tbm_backend_get_bo_priv(bo);
241
242     ret = vigs_drm_gem_get_name(&sfc->gem);
243
244     if (ret != 0) {
245         TBM_EMULATOR_LOG_ERROR("vigs_drm_gem_get_name failed: %s",
246                                strerror(-ret));
247         return 0;
248     }
249
250     return sfc->gem.name;
251 }
252
253 MODULEINITPPROTO(tbm_bufmgr_emulator_init);
254
255 static TBMModuleVersionInfo EmulatorVersRec =
256 {
257     "emulator",
258     "Samsung",
259     TBM_ABI_VERSION,
260 };
261
262 TBMModuleData tbmModuleData = { &EmulatorVersRec, tbm_bufmgr_emulator_init };
263
264 int tbm_bufmgr_emulator_init(tbm_bufmgr bufmgr, int fd)
265 {
266     int ret = 0;
267     struct vigs_drm_device *drm_dev = NULL;
268     tbm_bufmgr_backend backend = NULL;
269
270     TBM_EMULATOR_LOG_DEBUG("enter");
271
272     if (!bufmgr) {
273         return 0;
274     }
275
276     ret = vigs_drm_device_create(fd, &drm_dev);
277
278     if (ret != 0) {
279         TBM_EMULATOR_LOG_ERROR("vigs_drm_device_create failed: %s", strerror(-ret));
280         goto fail;
281     }
282
283     backend = tbm_backend_alloc();
284
285     if (!backend) {
286         TBM_EMULATOR_LOG_ERROR("tbm_backend_alloc failed");
287         goto fail;
288     }
289
290     backend->flags = TBM_CACHE_CTRL_BACKEND|TBM_LOCK_CTRL_BACKEND;
291     backend->priv = (void*)drm_dev;
292     backend->bufmgr_deinit = tbm_bufmgr_emulator_deinit;
293     backend->bo_size = tbm_bufmgr_emulator_bo_size;
294     backend->bo_alloc = tbm_bufmgr_emulator_bo_alloc;
295     backend->bo_free = tbm_bufmgr_emulator_bo_free;
296     backend->bo_import = tbm_bufmgr_emulator_bo_import;
297     backend->bo_export = tbm_bufmgr_emulator_bo_export;
298     backend->bo_get_handle = tbm_bufmgr_emulator_bo_get_handle;
299     backend->bo_map = tbm_bufmgr_emulator_bo_map;
300     backend->bo_unmap = tbm_bufmgr_emulator_bo_unmap;
301     backend->bo_cache_flush = tbm_bufmgr_emulator_bo_cache_flush;
302     backend->bo_get_global_key = tbm_bufmgr_emulator_bo_get_global_key;
303     backend->bo_lock = tbm_bufmgr_emulator_bo_lock;
304     backend->bo_unlock = tbm_bufmgr_emulator_bo_unlock;
305
306     if (!tbm_backend_init(bufmgr, backend)) {
307         TBM_EMULATOR_LOG_ERROR("tbm_backend_init failed");
308         goto fail;
309     }
310
311     TBM_EMULATOR_LOG_INFO("initialized");
312
313     return 1;
314
315 fail:
316     if (backend) {
317         tbm_backend_free(backend);
318     }
319
320     if (drm_dev) {
321         vigs_drm_device_destroy(drm_dev);
322     }
323
324     return 0;
325 }